2017-12-01 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / c / c-typeck.c
blob922266038afb6d3d410ef97b5d61837146631a5d
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 "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
52 #include "stringpool.h"
53 #include "attribs.h"
54 #include "asan.h"
56 /* Possible cases of implicit bad conversions. Used to select
57 diagnostic messages in convert_for_assignment. */
58 enum impl_conv {
59 ic_argpass,
60 ic_assign,
61 ic_init,
62 ic_return
65 /* The level of nesting inside "__alignof__". */
66 int in_alignof;
68 /* The level of nesting inside "sizeof". */
69 int in_sizeof;
71 /* The level of nesting inside "typeof". */
72 int in_typeof;
74 /* The argument of last parsed sizeof expression, only to be tested
75 if expr.original_code == SIZEOF_EXPR. */
76 tree c_last_sizeof_arg;
77 location_t c_last_sizeof_loc;
79 /* Nonzero if we might need to print a "missing braces around
80 initializer" message within this initializer. */
81 static int found_missing_braces;
83 static int require_constant_value;
84 static int require_constant_elements;
86 static bool null_pointer_constant_p (const_tree);
87 static tree qualify_type (tree, tree);
88 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90 static int comp_target_types (location_t, tree, tree);
91 static int function_types_compatible_p (const_tree, const_tree, bool *,
92 bool *);
93 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
94 static tree lookup_field (tree, tree);
95 static int convert_arguments (location_t, vec<location_t>, tree,
96 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
97 tree);
98 static tree pointer_diff (location_t, tree, tree);
99 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
100 enum impl_conv, bool, tree, tree, int);
101 static tree valid_compound_expr_initializer (tree, tree);
102 static void push_string (const char *);
103 static void push_member_name (tree);
104 static int spelling_length (void);
105 static char *print_spelling (char *);
106 static void warning_init (location_t, int, const char *);
107 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
108 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
109 bool, struct obstack *);
110 static void output_pending_init_elements (int, struct obstack *);
111 static bool set_designator (location_t, bool, struct obstack *);
112 static void push_range_stack (tree, struct obstack *);
113 static void add_pending_init (location_t, tree, tree, tree, bool,
114 struct obstack *);
115 static void set_nonincremental_init (struct obstack *);
116 static void set_nonincremental_init_from_string (tree, struct obstack *);
117 static tree find_init_member (tree, struct obstack *);
118 static void readonly_warning (tree, enum lvalue_use);
119 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
120 static void record_maybe_used_decl (tree);
121 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
123 /* Return true if EXP is a null pointer constant, false otherwise. */
125 static bool
126 null_pointer_constant_p (const_tree expr)
128 /* This should really operate on c_expr structures, but they aren't
129 yet available everywhere required. */
130 tree type = TREE_TYPE (expr);
131 return (TREE_CODE (expr) == INTEGER_CST
132 && !TREE_OVERFLOW (expr)
133 && integer_zerop (expr)
134 && (INTEGRAL_TYPE_P (type)
135 || (TREE_CODE (type) == POINTER_TYPE
136 && VOID_TYPE_P (TREE_TYPE (type))
137 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
140 /* EXPR may appear in an unevaluated part of an integer constant
141 expression, but not in an evaluated part. Wrap it in a
142 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
143 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
145 static tree
146 note_integer_operands (tree expr)
148 tree ret;
149 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
151 ret = copy_node (expr);
152 TREE_OVERFLOW (ret) = 1;
154 else
156 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
157 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
159 return ret;
162 /* Having checked whether EXPR may appear in an unevaluated part of an
163 integer constant expression and found that it may, remove any
164 C_MAYBE_CONST_EXPR noting this fact and return the resulting
165 expression. */
167 static inline tree
168 remove_c_maybe_const_expr (tree expr)
170 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
171 return C_MAYBE_CONST_EXPR_EXPR (expr);
172 else
173 return expr;
176 \f/* This is a cache to hold if two types are compatible or not. */
178 struct tagged_tu_seen_cache {
179 const struct tagged_tu_seen_cache * next;
180 const_tree t1;
181 const_tree t2;
182 /* The return value of tagged_types_tu_compatible_p if we had seen
183 these two types already. */
184 int val;
187 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
188 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
190 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
191 does not have an incomplete type. (That includes void types.)
192 LOC is the location of the use. */
194 tree
195 require_complete_type (location_t loc, tree value)
197 tree type = TREE_TYPE (value);
199 if (error_operand_p (value))
200 return error_mark_node;
202 /* First, detect a valid value with a complete type. */
203 if (COMPLETE_TYPE_P (type))
204 return value;
206 c_incomplete_type_error (loc, value, type);
207 return error_mark_node;
210 /* Print an error message for invalid use of an incomplete type.
211 VALUE is the expression that was used (or 0 if that isn't known)
212 and TYPE is the type that was invalid. LOC is the location for
213 the error. */
215 void
216 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
218 /* Avoid duplicate error message. */
219 if (TREE_CODE (type) == ERROR_MARK)
220 return;
222 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
223 error_at (loc, "%qD has an incomplete type %qT", value, type);
224 else
226 retry:
227 /* We must print an error message. Be clever about what it says. */
229 switch (TREE_CODE (type))
231 case RECORD_TYPE:
232 case UNION_TYPE:
233 case ENUMERAL_TYPE:
234 break;
236 case VOID_TYPE:
237 error_at (loc, "invalid use of void expression");
238 return;
240 case ARRAY_TYPE:
241 if (TYPE_DOMAIN (type))
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
245 error_at (loc, "invalid use of flexible array member");
246 return;
248 type = TREE_TYPE (type);
249 goto retry;
251 error_at (loc, "invalid use of array with unspecified bounds");
252 return;
254 default:
255 gcc_unreachable ();
258 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
259 error_at (loc, "invalid use of undefined type %qT", type);
260 else
261 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
262 error_at (loc, "invalid use of incomplete typedef %qT", type);
266 /* Given a type, apply default promotions wrt unnamed function
267 arguments and return the new type. */
269 tree
270 c_type_promotes_to (tree type)
272 tree ret = NULL_TREE;
274 if (TYPE_MAIN_VARIANT (type) == float_type_node)
275 ret = double_type_node;
276 else if (c_promoting_integer_type_p (type))
278 /* Preserve unsignedness if not really getting any wider. */
279 if (TYPE_UNSIGNED (type)
280 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
281 ret = unsigned_type_node;
282 else
283 ret = integer_type_node;
286 if (ret != NULL_TREE)
287 return (TYPE_ATOMIC (type)
288 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
289 : ret);
291 return type;
294 /* Return true if between two named address spaces, whether there is a superset
295 named address space that encompasses both address spaces. If there is a
296 superset, return which address space is the superset. */
298 static bool
299 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
301 if (as1 == as2)
303 *common = as1;
304 return true;
306 else if (targetm.addr_space.subset_p (as1, as2))
308 *common = as2;
309 return true;
311 else if (targetm.addr_space.subset_p (as2, as1))
313 *common = as1;
314 return true;
316 else
317 return false;
320 /* Return a variant of TYPE which has all the type qualifiers of LIKE
321 as well as those of TYPE. */
323 static tree
324 qualify_type (tree type, tree like)
326 addr_space_t as_type = TYPE_ADDR_SPACE (type);
327 addr_space_t as_like = TYPE_ADDR_SPACE (like);
328 addr_space_t as_common;
330 /* If the two named address spaces are different, determine the common
331 superset address space. If there isn't one, raise an error. */
332 if (!addr_space_superset (as_type, as_like, &as_common))
334 as_common = as_type;
335 error ("%qT and %qT are in disjoint named address spaces",
336 type, like);
339 return c_build_qualified_type (type,
340 TYPE_QUALS_NO_ADDR_SPACE (type)
341 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
342 | ENCODE_QUAL_ADDR_SPACE (as_common));
345 /* Return true iff the given tree T is a variable length array. */
347 bool
348 c_vla_type_p (const_tree t)
350 if (TREE_CODE (t) == ARRAY_TYPE
351 && C_TYPE_VARIABLE_SIZE (t))
352 return true;
353 return false;
356 /* Return the composite type of two compatible types.
358 We assume that comptypes has already been done and returned
359 nonzero; if that isn't so, this may crash. In particular, we
360 assume that qualifiers match. */
362 tree
363 composite_type (tree t1, tree t2)
365 enum tree_code code1;
366 enum tree_code code2;
367 tree attributes;
369 /* Save time if the two types are the same. */
371 if (t1 == t2) return t1;
373 /* If one type is nonsense, use the other. */
374 if (t1 == error_mark_node)
375 return t2;
376 if (t2 == error_mark_node)
377 return t1;
379 code1 = TREE_CODE (t1);
380 code2 = TREE_CODE (t2);
382 /* Merge the attributes. */
383 attributes = targetm.merge_type_attributes (t1, t2);
385 /* If one is an enumerated type and the other is the compatible
386 integer type, the composite type might be either of the two
387 (DR#013 question 3). For consistency, use the enumerated type as
388 the composite type. */
390 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
391 return t1;
392 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
393 return t2;
395 gcc_assert (code1 == code2);
397 switch (code1)
399 case POINTER_TYPE:
400 /* For two pointers, do this recursively on the target type. */
402 tree pointed_to_1 = TREE_TYPE (t1);
403 tree pointed_to_2 = TREE_TYPE (t2);
404 tree target = composite_type (pointed_to_1, pointed_to_2);
405 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
406 t1 = build_type_attribute_variant (t1, attributes);
407 return qualify_type (t1, t2);
410 case ARRAY_TYPE:
412 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
413 int quals;
414 tree unqual_elt;
415 tree d1 = TYPE_DOMAIN (t1);
416 tree d2 = TYPE_DOMAIN (t2);
417 bool d1_variable, d2_variable;
418 bool d1_zero, d2_zero;
419 bool t1_complete, t2_complete;
421 /* We should not have any type quals on arrays at all. */
422 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
423 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
425 t1_complete = COMPLETE_TYPE_P (t1);
426 t2_complete = COMPLETE_TYPE_P (t2);
428 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
429 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
431 d1_variable = (!d1_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
434 d2_variable = (!d2_zero
435 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
436 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
437 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
438 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
440 /* Save space: see if the result is identical to one of the args. */
441 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
442 && (d2_variable || d2_zero || !d1_variable))
443 return build_type_attribute_variant (t1, attributes);
444 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
445 && (d1_variable || d1_zero || !d2_variable))
446 return build_type_attribute_variant (t2, attributes);
448 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
449 return build_type_attribute_variant (t1, attributes);
450 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
451 return build_type_attribute_variant (t2, attributes);
453 /* Merge the element types, and have a size if either arg has
454 one. We may have qualifiers on the element types. To set
455 up TYPE_MAIN_VARIANT correctly, we need to form the
456 composite of the unqualified types and add the qualifiers
457 back at the end. */
458 quals = TYPE_QUALS (strip_array_types (elt));
459 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
460 t1 = build_array_type (unqual_elt,
461 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
462 && (d2_variable
463 || d2_zero
464 || !d1_variable))
465 ? t1
466 : t2));
467 /* Ensure a composite type involving a zero-length array type
468 is a zero-length type not an incomplete type. */
469 if (d1_zero && d2_zero
470 && (t1_complete || t2_complete)
471 && !COMPLETE_TYPE_P (t1))
473 TYPE_SIZE (t1) = bitsize_zero_node;
474 TYPE_SIZE_UNIT (t1) = size_zero_node;
476 t1 = c_build_qualified_type (t1, quals);
477 return build_type_attribute_variant (t1, attributes);
480 case ENUMERAL_TYPE:
481 case RECORD_TYPE:
482 case UNION_TYPE:
483 if (attributes != NULL)
485 /* Try harder not to create a new aggregate type. */
486 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
487 return t1;
488 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
489 return t2;
491 return build_type_attribute_variant (t1, attributes);
493 case FUNCTION_TYPE:
494 /* Function types: prefer the one that specified arg types.
495 If both do, merge the arg types. Also merge the return types. */
497 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
498 tree p1 = TYPE_ARG_TYPES (t1);
499 tree p2 = TYPE_ARG_TYPES (t2);
500 int len;
501 tree newargs, n;
502 int i;
504 /* Save space: see if the result is identical to one of the args. */
505 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
506 return build_type_attribute_variant (t1, attributes);
507 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
508 return build_type_attribute_variant (t2, attributes);
510 /* Simple way if one arg fails to specify argument types. */
511 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
513 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
514 t1 = build_type_attribute_variant (t1, attributes);
515 return qualify_type (t1, t2);
517 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
519 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
520 t1 = build_type_attribute_variant (t1, attributes);
521 return qualify_type (t1, t2);
524 /* If both args specify argument types, we must merge the two
525 lists, argument by argument. */
527 for (len = 0, newargs = p1;
528 newargs && newargs != void_list_node;
529 len++, newargs = TREE_CHAIN (newargs))
532 for (i = 0; i < len; i++)
533 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
535 n = newargs;
537 for (; p1 && p1 != void_list_node;
538 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
540 /* A null type means arg type is not specified.
541 Take whatever the other function type has. */
542 if (TREE_VALUE (p1) == NULL_TREE)
544 TREE_VALUE (n) = TREE_VALUE (p2);
545 goto parm_done;
547 if (TREE_VALUE (p2) == NULL_TREE)
549 TREE_VALUE (n) = TREE_VALUE (p1);
550 goto parm_done;
553 /* Given wait (union {union wait *u; int *i} *)
554 and wait (union wait *),
555 prefer union wait * as type of parm. */
556 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
557 && TREE_VALUE (p1) != TREE_VALUE (p2))
559 tree memb;
560 tree mv2 = TREE_VALUE (p2);
561 if (mv2 && mv2 != error_mark_node
562 && TREE_CODE (mv2) != ARRAY_TYPE)
563 mv2 = TYPE_MAIN_VARIANT (mv2);
564 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
565 memb; memb = DECL_CHAIN (memb))
567 tree mv3 = TREE_TYPE (memb);
568 if (mv3 && mv3 != error_mark_node
569 && TREE_CODE (mv3) != ARRAY_TYPE)
570 mv3 = TYPE_MAIN_VARIANT (mv3);
571 if (comptypes (mv3, mv2))
573 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
574 TREE_VALUE (p2));
575 pedwarn (input_location, OPT_Wpedantic,
576 "function types not truly compatible in ISO C");
577 goto parm_done;
581 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
582 && TREE_VALUE (p2) != TREE_VALUE (p1))
584 tree memb;
585 tree mv1 = TREE_VALUE (p1);
586 if (mv1 && mv1 != error_mark_node
587 && TREE_CODE (mv1) != ARRAY_TYPE)
588 mv1 = TYPE_MAIN_VARIANT (mv1);
589 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
590 memb; memb = DECL_CHAIN (memb))
592 tree mv3 = TREE_TYPE (memb);
593 if (mv3 && mv3 != error_mark_node
594 && TREE_CODE (mv3) != ARRAY_TYPE)
595 mv3 = TYPE_MAIN_VARIANT (mv3);
596 if (comptypes (mv3, mv1))
598 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
599 TREE_VALUE (p1));
600 pedwarn (input_location, OPT_Wpedantic,
601 "function types not truly compatible in ISO C");
602 goto parm_done;
606 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
607 parm_done: ;
610 t1 = build_function_type (valtype, newargs);
611 t1 = qualify_type (t1, t2);
613 /* FALLTHRU */
615 default:
616 return build_type_attribute_variant (t1, attributes);
621 /* Return the type of a conditional expression between pointers to
622 possibly differently qualified versions of compatible types.
624 We assume that comp_target_types has already been done and returned
625 nonzero; if that isn't so, this may crash. */
627 static tree
628 common_pointer_type (tree t1, tree t2)
630 tree attributes;
631 tree pointed_to_1, mv1;
632 tree pointed_to_2, mv2;
633 tree target;
634 unsigned target_quals;
635 addr_space_t as1, as2, as_common;
636 int quals1, quals2;
638 /* Save time if the two types are the same. */
640 if (t1 == t2) return t1;
642 /* If one type is nonsense, use the other. */
643 if (t1 == error_mark_node)
644 return t2;
645 if (t2 == error_mark_node)
646 return t1;
648 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
649 && TREE_CODE (t2) == POINTER_TYPE);
651 /* Merge the attributes. */
652 attributes = targetm.merge_type_attributes (t1, t2);
654 /* Find the composite type of the target types, and combine the
655 qualifiers of the two types' targets. Do not lose qualifiers on
656 array element types by taking the TYPE_MAIN_VARIANT. */
657 mv1 = pointed_to_1 = TREE_TYPE (t1);
658 mv2 = pointed_to_2 = TREE_TYPE (t2);
659 if (TREE_CODE (mv1) != ARRAY_TYPE)
660 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
661 if (TREE_CODE (mv2) != ARRAY_TYPE)
662 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
663 target = composite_type (mv1, mv2);
665 /* Strip array types to get correct qualifier for pointers to arrays */
666 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
667 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
669 /* For function types do not merge const qualifiers, but drop them
670 if used inconsistently. The middle-end uses these to mark const
671 and noreturn functions. */
672 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
673 target_quals = (quals1 & quals2);
674 else
675 target_quals = (quals1 | quals2);
677 /* If the two named address spaces are different, determine the common
678 superset address space. This is guaranteed to exist due to the
679 assumption that comp_target_type returned non-zero. */
680 as1 = TYPE_ADDR_SPACE (pointed_to_1);
681 as2 = TYPE_ADDR_SPACE (pointed_to_2);
682 if (!addr_space_superset (as1, as2, &as_common))
683 gcc_unreachable ();
685 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
687 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
688 return build_type_attribute_variant (t1, attributes);
691 /* Return the common type for two arithmetic types under the usual
692 arithmetic conversions. The default conversions have already been
693 applied, and enumerated types converted to their compatible integer
694 types. The resulting type is unqualified and has no attributes.
696 This is the type for the result of most arithmetic operations
697 if the operands have the given two types. */
699 static tree
700 c_common_type (tree t1, tree t2)
702 enum tree_code code1;
703 enum tree_code code2;
705 /* If one type is nonsense, use the other. */
706 if (t1 == error_mark_node)
707 return t2;
708 if (t2 == error_mark_node)
709 return t1;
711 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
712 t1 = TYPE_MAIN_VARIANT (t1);
714 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
715 t2 = TYPE_MAIN_VARIANT (t2);
717 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
718 t1 = build_type_attribute_variant (t1, NULL_TREE);
720 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
721 t2 = build_type_attribute_variant (t2, NULL_TREE);
723 /* Save time if the two types are the same. */
725 if (t1 == t2) return t1;
727 code1 = TREE_CODE (t1);
728 code2 = TREE_CODE (t2);
730 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
731 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
732 || code1 == INTEGER_TYPE);
733 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
734 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
735 || code2 == INTEGER_TYPE);
737 /* When one operand is a decimal float type, the other operand cannot be
738 a generic float type or a complex type. We also disallow vector types
739 here. */
740 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
741 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
743 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
745 error ("can%'t mix operands of decimal float and vector types");
746 return error_mark_node;
748 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
750 error ("can%'t mix operands of decimal float and complex types");
751 return error_mark_node;
753 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
755 error ("can%'t mix operands of decimal float and other float types");
756 return error_mark_node;
760 /* If one type is a vector type, return that type. (How the usual
761 arithmetic conversions apply to the vector types extension is not
762 precisely specified.) */
763 if (code1 == VECTOR_TYPE)
764 return t1;
766 if (code2 == VECTOR_TYPE)
767 return t2;
769 /* If one type is complex, form the common type of the non-complex
770 components, then make that complex. Use T1 or T2 if it is the
771 required type. */
772 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
774 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
775 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
776 tree subtype = c_common_type (subtype1, subtype2);
778 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
779 return t1;
780 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
781 return t2;
782 else
783 return build_complex_type (subtype);
786 /* If only one is real, use it as the result. */
788 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
789 return t1;
791 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
792 return t2;
794 /* If both are real and either are decimal floating point types, use
795 the decimal floating point type with the greater precision. */
797 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
799 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
801 return dfloat128_type_node;
802 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
804 return dfloat64_type_node;
805 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
806 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
807 return dfloat32_type_node;
810 /* Deal with fixed-point types. */
811 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
813 unsigned int unsignedp = 0, satp = 0;
814 scalar_mode m1, m2;
815 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
817 m1 = SCALAR_TYPE_MODE (t1);
818 m2 = SCALAR_TYPE_MODE (t2);
820 /* If one input type is saturating, the result type is saturating. */
821 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
822 satp = 1;
824 /* If both fixed-point types are unsigned, the result type is unsigned.
825 When mixing fixed-point and integer types, follow the sign of the
826 fixed-point type.
827 Otherwise, the result type is signed. */
828 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
829 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
830 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
831 && TYPE_UNSIGNED (t1))
832 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
833 && TYPE_UNSIGNED (t2)))
834 unsignedp = 1;
836 /* The result type is signed. */
837 if (unsignedp == 0)
839 /* If the input type is unsigned, we need to convert to the
840 signed type. */
841 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
843 enum mode_class mclass = (enum mode_class) 0;
844 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
845 mclass = MODE_FRACT;
846 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
847 mclass = MODE_ACCUM;
848 else
849 gcc_unreachable ();
850 m1 = as_a <scalar_mode>
851 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
853 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
855 enum mode_class mclass = (enum mode_class) 0;
856 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
857 mclass = MODE_FRACT;
858 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
859 mclass = MODE_ACCUM;
860 else
861 gcc_unreachable ();
862 m2 = as_a <scalar_mode>
863 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
867 if (code1 == FIXED_POINT_TYPE)
869 fbit1 = GET_MODE_FBIT (m1);
870 ibit1 = GET_MODE_IBIT (m1);
872 else
874 fbit1 = 0;
875 /* Signed integers need to subtract one sign bit. */
876 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
879 if (code2 == FIXED_POINT_TYPE)
881 fbit2 = GET_MODE_FBIT (m2);
882 ibit2 = GET_MODE_IBIT (m2);
884 else
886 fbit2 = 0;
887 /* Signed integers need to subtract one sign bit. */
888 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
891 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
892 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
893 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
894 satp);
897 /* Both real or both integers; use the one with greater precision. */
899 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
900 return t1;
901 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
902 return t2;
904 /* Same precision. Prefer long longs to longs to ints when the
905 same precision, following the C99 rules on integer type rank
906 (which are equivalent to the C90 rules for C90 types). */
908 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
910 return long_long_unsigned_type_node;
912 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
915 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
916 return long_long_unsigned_type_node;
917 else
918 return long_long_integer_type_node;
921 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
923 return long_unsigned_type_node;
925 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
926 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
928 /* But preserve unsignedness from the other type,
929 since long cannot hold all the values of an unsigned int. */
930 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
931 return long_unsigned_type_node;
932 else
933 return long_integer_type_node;
936 /* For floating types of the same TYPE_PRECISION (which we here
937 assume means either the same set of values, or sets of values
938 neither a subset of the other, with behavior being undefined in
939 the latter case), follow the rules from TS 18661-3: prefer
940 interchange types _FloatN, then standard types long double,
941 double, float, then extended types _FloatNx. For extended types,
942 check them starting with _Float128x as that seems most consistent
943 in spirit with preferring long double to double; for interchange
944 types, also check in that order for consistency although it's not
945 possible for more than one of them to have the same
946 precision. */
947 tree mv1 = TYPE_MAIN_VARIANT (t1);
948 tree mv2 = TYPE_MAIN_VARIANT (t2);
950 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
951 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
952 return FLOATN_TYPE_NODE (i);
954 /* Likewise, prefer long double to double even if same size. */
955 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
956 return long_double_type_node;
958 /* Likewise, prefer double to float even if same size.
959 We got a couple of embedded targets with 32 bit doubles, and the
960 pdp11 might have 64 bit floats. */
961 if (mv1 == double_type_node || mv2 == double_type_node)
962 return double_type_node;
964 if (mv1 == float_type_node || mv2 == float_type_node)
965 return float_type_node;
967 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
968 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
969 return FLOATNX_TYPE_NODE (i);
971 /* Otherwise prefer the unsigned one. */
973 if (TYPE_UNSIGNED (t1))
974 return t1;
975 else
976 return t2;
979 /* Wrapper around c_common_type that is used by c-common.c and other
980 front end optimizations that remove promotions. ENUMERAL_TYPEs
981 are allowed here and are converted to their compatible integer types.
982 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
983 preferably a non-Boolean type as the common type. */
984 tree
985 common_type (tree t1, tree t2)
987 if (TREE_CODE (t1) == ENUMERAL_TYPE)
988 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
989 if (TREE_CODE (t2) == ENUMERAL_TYPE)
990 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
992 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
993 if (TREE_CODE (t1) == BOOLEAN_TYPE
994 && TREE_CODE (t2) == BOOLEAN_TYPE)
995 return boolean_type_node;
997 /* If either type is BOOLEAN_TYPE, then return the other. */
998 if (TREE_CODE (t1) == BOOLEAN_TYPE)
999 return t2;
1000 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1001 return t1;
1003 return c_common_type (t1, t2);
1006 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1007 or various other operations. Return 2 if they are compatible
1008 but a warning may be needed if you use them together. */
1011 comptypes (tree type1, tree type2)
1013 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1014 int val;
1016 val = comptypes_internal (type1, type2, NULL, NULL);
1017 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1019 return val;
1022 /* Like comptypes, but if it returns non-zero because enum and int are
1023 compatible, it sets *ENUM_AND_INT_P to true. */
1025 static int
1026 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1028 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1029 int val;
1031 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1032 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1034 return val;
1037 /* Like comptypes, but if it returns nonzero for different types, it
1038 sets *DIFFERENT_TYPES_P to true. */
1041 comptypes_check_different_types (tree type1, tree type2,
1042 bool *different_types_p)
1044 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1045 int val;
1047 val = comptypes_internal (type1, type2, NULL, different_types_p);
1048 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1050 return val;
1053 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1054 or various other operations. Return 2 if they are compatible
1055 but a warning may be needed if you use them together. If
1056 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1057 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1058 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1059 NULL, and the types are compatible but different enough not to be
1060 permitted in C11 typedef redeclarations, then this sets
1061 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1062 false, but may or may not be set if the types are incompatible.
1063 This differs from comptypes, in that we don't free the seen
1064 types. */
1066 static int
1067 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1068 bool *different_types_p)
1070 const_tree t1 = type1;
1071 const_tree t2 = type2;
1072 int attrval, val;
1074 /* Suppress errors caused by previously reported errors. */
1076 if (t1 == t2 || !t1 || !t2
1077 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1078 return 1;
1080 /* Enumerated types are compatible with integer types, but this is
1081 not transitive: two enumerated types in the same translation unit
1082 are compatible with each other only if they are the same type. */
1084 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1086 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1087 if (TREE_CODE (t2) != VOID_TYPE)
1089 if (enum_and_int_p != NULL)
1090 *enum_and_int_p = true;
1091 if (different_types_p != NULL)
1092 *different_types_p = true;
1095 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1097 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1098 if (TREE_CODE (t1) != VOID_TYPE)
1100 if (enum_and_int_p != NULL)
1101 *enum_and_int_p = true;
1102 if (different_types_p != NULL)
1103 *different_types_p = true;
1107 if (t1 == t2)
1108 return 1;
1110 /* Different classes of types can't be compatible. */
1112 if (TREE_CODE (t1) != TREE_CODE (t2))
1113 return 0;
1115 /* Qualifiers must match. C99 6.7.3p9 */
1117 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1118 return 0;
1120 /* Allow for two different type nodes which have essentially the same
1121 definition. Note that we already checked for equality of the type
1122 qualifiers (just above). */
1124 if (TREE_CODE (t1) != ARRAY_TYPE
1125 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1126 return 1;
1128 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1129 if (!(attrval = comp_type_attributes (t1, t2)))
1130 return 0;
1132 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1133 val = 0;
1135 switch (TREE_CODE (t1))
1137 case INTEGER_TYPE:
1138 case FIXED_POINT_TYPE:
1139 case REAL_TYPE:
1140 /* With these nodes, we can't determine type equivalence by
1141 looking at what is stored in the nodes themselves, because
1142 two nodes might have different TYPE_MAIN_VARIANTs but still
1143 represent the same type. For example, wchar_t and int could
1144 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1145 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1146 and are distinct types. On the other hand, int and the
1147 following typedef
1149 typedef int INT __attribute((may_alias));
1151 have identical properties, different TYPE_MAIN_VARIANTs, but
1152 represent the same type. The canonical type system keeps
1153 track of equivalence in this case, so we fall back on it. */
1154 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1156 case POINTER_TYPE:
1157 /* Do not remove mode information. */
1158 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1159 break;
1160 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1161 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1162 enum_and_int_p, different_types_p));
1163 break;
1165 case FUNCTION_TYPE:
1166 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1167 different_types_p);
1168 break;
1170 case ARRAY_TYPE:
1172 tree d1 = TYPE_DOMAIN (t1);
1173 tree d2 = TYPE_DOMAIN (t2);
1174 bool d1_variable, d2_variable;
1175 bool d1_zero, d2_zero;
1176 val = 1;
1178 /* Target types must match incl. qualifiers. */
1179 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1180 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1181 enum_and_int_p,
1182 different_types_p)))
1183 return 0;
1185 if (different_types_p != NULL
1186 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1187 *different_types_p = true;
1188 /* Sizes must match unless one is missing or variable. */
1189 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1190 break;
1192 d1_zero = !TYPE_MAX_VALUE (d1);
1193 d2_zero = !TYPE_MAX_VALUE (d2);
1195 d1_variable = (!d1_zero
1196 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1197 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1198 d2_variable = (!d2_zero
1199 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1200 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1201 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1202 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1204 if (different_types_p != NULL
1205 && d1_variable != d2_variable)
1206 *different_types_p = true;
1207 if (d1_variable || d2_variable)
1208 break;
1209 if (d1_zero && d2_zero)
1210 break;
1211 if (d1_zero || d2_zero
1212 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1213 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1214 val = 0;
1216 break;
1219 case ENUMERAL_TYPE:
1220 case RECORD_TYPE:
1221 case UNION_TYPE:
1222 if (val != 1 && !same_translation_unit_p (t1, t2))
1224 tree a1 = TYPE_ATTRIBUTES (t1);
1225 tree a2 = TYPE_ATTRIBUTES (t2);
1227 if (! attribute_list_contained (a1, a2)
1228 && ! attribute_list_contained (a2, a1))
1229 break;
1231 if (attrval != 2)
1232 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1233 different_types_p);
1234 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1235 different_types_p);
1237 break;
1239 case VECTOR_TYPE:
1240 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1241 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1242 enum_and_int_p, different_types_p));
1243 break;
1245 default:
1246 break;
1248 return attrval == 2 && val == 1 ? 2 : val;
1251 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1252 their qualifiers, except for named address spaces. If the pointers point to
1253 different named addresses, then we must determine if one address space is a
1254 subset of the other. */
1256 static int
1257 comp_target_types (location_t location, tree ttl, tree ttr)
1259 int val;
1260 int val_ped;
1261 tree mvl = TREE_TYPE (ttl);
1262 tree mvr = TREE_TYPE (ttr);
1263 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1264 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1265 addr_space_t as_common;
1266 bool enum_and_int_p;
1268 /* Fail if pointers point to incompatible address spaces. */
1269 if (!addr_space_superset (asl, asr, &as_common))
1270 return 0;
1272 /* For pedantic record result of comptypes on arrays before losing
1273 qualifiers on the element type below. */
1274 val_ped = 1;
1276 if (TREE_CODE (mvl) == ARRAY_TYPE
1277 && TREE_CODE (mvr) == ARRAY_TYPE)
1278 val_ped = comptypes (mvl, mvr);
1280 /* Qualifiers on element types of array types that are
1281 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1283 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1284 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1285 : TYPE_MAIN_VARIANT (mvl));
1287 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1288 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1289 : TYPE_MAIN_VARIANT (mvr));
1291 enum_and_int_p = false;
1292 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1294 if (val == 1 && val_ped != 1)
1295 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1296 "are incompatible in ISO C");
1298 if (val == 2)
1299 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1301 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1302 warning_at (location, OPT_Wc___compat,
1303 "pointer target types incompatible in C++");
1305 return val;
1308 /* Subroutines of `comptypes'. */
1310 /* Determine whether two trees derive from the same translation unit.
1311 If the CONTEXT chain ends in a null, that tree's context is still
1312 being parsed, so if two trees have context chains ending in null,
1313 they're in the same translation unit. */
1315 bool
1316 same_translation_unit_p (const_tree t1, const_tree t2)
1318 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1319 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1321 case tcc_declaration:
1322 t1 = DECL_CONTEXT (t1); break;
1323 case tcc_type:
1324 t1 = TYPE_CONTEXT (t1); break;
1325 case tcc_exceptional:
1326 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1327 default: gcc_unreachable ();
1330 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1331 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1333 case tcc_declaration:
1334 t2 = DECL_CONTEXT (t2); break;
1335 case tcc_type:
1336 t2 = TYPE_CONTEXT (t2); break;
1337 case tcc_exceptional:
1338 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1339 default: gcc_unreachable ();
1342 return t1 == t2;
1345 /* Allocate the seen two types, assuming that they are compatible. */
1347 static struct tagged_tu_seen_cache *
1348 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1350 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1351 tu->next = tagged_tu_seen_base;
1352 tu->t1 = t1;
1353 tu->t2 = t2;
1355 tagged_tu_seen_base = tu;
1357 /* The C standard says that two structures in different translation
1358 units are compatible with each other only if the types of their
1359 fields are compatible (among other things). We assume that they
1360 are compatible until proven otherwise when building the cache.
1361 An example where this can occur is:
1362 struct a
1364 struct a *next;
1366 If we are comparing this against a similar struct in another TU,
1367 and did not assume they were compatible, we end up with an infinite
1368 loop. */
1369 tu->val = 1;
1370 return tu;
1373 /* Free the seen types until we get to TU_TIL. */
1375 static void
1376 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1378 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1379 while (tu != tu_til)
1381 const struct tagged_tu_seen_cache *const tu1
1382 = (const struct tagged_tu_seen_cache *) tu;
1383 tu = tu1->next;
1384 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1386 tagged_tu_seen_base = tu_til;
1389 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1390 compatible. If the two types are not the same (which has been
1391 checked earlier), this can only happen when multiple translation
1392 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1393 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1394 comptypes_internal. */
1396 static int
1397 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1398 bool *enum_and_int_p, bool *different_types_p)
1400 tree s1, s2;
1401 bool needs_warning = false;
1403 /* We have to verify that the tags of the types are the same. This
1404 is harder than it looks because this may be a typedef, so we have
1405 to go look at the original type. It may even be a typedef of a
1406 typedef...
1407 In the case of compiler-created builtin structs the TYPE_DECL
1408 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1409 while (TYPE_NAME (t1)
1410 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1411 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1412 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1414 while (TYPE_NAME (t2)
1415 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1416 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1417 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1419 /* C90 didn't have the requirement that the two tags be the same. */
1420 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1421 return 0;
1423 /* C90 didn't say what happened if one or both of the types were
1424 incomplete; we choose to follow C99 rules here, which is that they
1425 are compatible. */
1426 if (TYPE_SIZE (t1) == NULL
1427 || TYPE_SIZE (t2) == NULL)
1428 return 1;
1431 const struct tagged_tu_seen_cache * tts_i;
1432 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1433 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1434 return tts_i->val;
1437 switch (TREE_CODE (t1))
1439 case ENUMERAL_TYPE:
1441 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1442 /* Speed up the case where the type values are in the same order. */
1443 tree tv1 = TYPE_VALUES (t1);
1444 tree tv2 = TYPE_VALUES (t2);
1446 if (tv1 == tv2)
1448 return 1;
1451 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1453 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1454 break;
1455 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1457 tu->val = 0;
1458 return 0;
1462 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1464 return 1;
1466 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1468 tu->val = 0;
1469 return 0;
1472 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1474 tu->val = 0;
1475 return 0;
1478 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1480 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1481 if (s2 == NULL
1482 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1484 tu->val = 0;
1485 return 0;
1488 return 1;
1491 case UNION_TYPE:
1493 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1494 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1496 tu->val = 0;
1497 return 0;
1500 /* Speed up the common case where the fields are in the same order. */
1501 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1502 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1504 int result;
1506 if (DECL_NAME (s1) != DECL_NAME (s2))
1507 break;
1508 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1509 enum_and_int_p, different_types_p);
1511 if (result != 1 && !DECL_NAME (s1))
1512 break;
1513 if (result == 0)
1515 tu->val = 0;
1516 return 0;
1518 if (result == 2)
1519 needs_warning = true;
1521 if (TREE_CODE (s1) == FIELD_DECL
1522 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1523 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1525 tu->val = 0;
1526 return 0;
1529 if (!s1 && !s2)
1531 tu->val = needs_warning ? 2 : 1;
1532 return tu->val;
1535 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1537 bool ok = false;
1539 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1540 if (DECL_NAME (s1) == DECL_NAME (s2))
1542 int result;
1544 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1545 enum_and_int_p,
1546 different_types_p);
1548 if (result != 1 && !DECL_NAME (s1))
1549 continue;
1550 if (result == 0)
1552 tu->val = 0;
1553 return 0;
1555 if (result == 2)
1556 needs_warning = true;
1558 if (TREE_CODE (s1) == FIELD_DECL
1559 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1560 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1561 break;
1563 ok = true;
1564 break;
1566 if (!ok)
1568 tu->val = 0;
1569 return 0;
1572 tu->val = needs_warning ? 2 : 10;
1573 return tu->val;
1576 case RECORD_TYPE:
1578 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1580 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1581 s1 && s2;
1582 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1584 int result;
1585 if (TREE_CODE (s1) != TREE_CODE (s2)
1586 || DECL_NAME (s1) != DECL_NAME (s2))
1587 break;
1588 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1589 enum_and_int_p, different_types_p);
1590 if (result == 0)
1591 break;
1592 if (result == 2)
1593 needs_warning = true;
1595 if (TREE_CODE (s1) == FIELD_DECL
1596 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1597 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1598 break;
1600 if (s1 && s2)
1601 tu->val = 0;
1602 else
1603 tu->val = needs_warning ? 2 : 1;
1604 return tu->val;
1607 default:
1608 gcc_unreachable ();
1612 /* Return 1 if two function types F1 and F2 are compatible.
1613 If either type specifies no argument types,
1614 the other must specify a fixed number of self-promoting arg types.
1615 Otherwise, if one type specifies only the number of arguments,
1616 the other must specify that number of self-promoting arg types.
1617 Otherwise, the argument types must match.
1618 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1620 static int
1621 function_types_compatible_p (const_tree f1, const_tree f2,
1622 bool *enum_and_int_p, bool *different_types_p)
1624 tree args1, args2;
1625 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1626 int val = 1;
1627 int val1;
1628 tree ret1, ret2;
1630 ret1 = TREE_TYPE (f1);
1631 ret2 = TREE_TYPE (f2);
1633 /* 'volatile' qualifiers on a function's return type used to mean
1634 the function is noreturn. */
1635 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1636 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1637 if (TYPE_VOLATILE (ret1))
1638 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1639 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1640 if (TYPE_VOLATILE (ret2))
1641 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1642 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1643 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1644 if (val == 0)
1645 return 0;
1647 args1 = TYPE_ARG_TYPES (f1);
1648 args2 = TYPE_ARG_TYPES (f2);
1650 if (different_types_p != NULL
1651 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1652 *different_types_p = true;
1654 /* An unspecified parmlist matches any specified parmlist
1655 whose argument types don't need default promotions. */
1657 if (args1 == NULL_TREE)
1659 if (!self_promoting_args_p (args2))
1660 return 0;
1661 /* If one of these types comes from a non-prototype fn definition,
1662 compare that with the other type's arglist.
1663 If they don't match, ask for a warning (but no error). */
1664 if (TYPE_ACTUAL_ARG_TYPES (f1)
1665 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1666 enum_and_int_p, different_types_p))
1667 val = 2;
1668 return val;
1670 if (args2 == NULL_TREE)
1672 if (!self_promoting_args_p (args1))
1673 return 0;
1674 if (TYPE_ACTUAL_ARG_TYPES (f2)
1675 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1676 enum_and_int_p, different_types_p))
1677 val = 2;
1678 return val;
1681 /* Both types have argument lists: compare them and propagate results. */
1682 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1683 different_types_p);
1684 return val1 != 1 ? val1 : val;
1687 /* Check two lists of types for compatibility, returning 0 for
1688 incompatible, 1 for compatible, or 2 for compatible with
1689 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1690 comptypes_internal. */
1692 static int
1693 type_lists_compatible_p (const_tree args1, const_tree args2,
1694 bool *enum_and_int_p, bool *different_types_p)
1696 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1697 int val = 1;
1698 int newval = 0;
1700 while (1)
1702 tree a1, mv1, a2, mv2;
1703 if (args1 == NULL_TREE && args2 == NULL_TREE)
1704 return val;
1705 /* If one list is shorter than the other,
1706 they fail to match. */
1707 if (args1 == NULL_TREE || args2 == NULL_TREE)
1708 return 0;
1709 mv1 = a1 = TREE_VALUE (args1);
1710 mv2 = a2 = TREE_VALUE (args2);
1711 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1712 mv1 = (TYPE_ATOMIC (mv1)
1713 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1714 TYPE_QUAL_ATOMIC)
1715 : TYPE_MAIN_VARIANT (mv1));
1716 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1717 mv2 = (TYPE_ATOMIC (mv2)
1718 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1719 TYPE_QUAL_ATOMIC)
1720 : TYPE_MAIN_VARIANT (mv2));
1721 /* A null pointer instead of a type
1722 means there is supposed to be an argument
1723 but nothing is specified about what type it has.
1724 So match anything that self-promotes. */
1725 if (different_types_p != NULL
1726 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1727 *different_types_p = true;
1728 if (a1 == NULL_TREE)
1730 if (c_type_promotes_to (a2) != a2)
1731 return 0;
1733 else if (a2 == NULL_TREE)
1735 if (c_type_promotes_to (a1) != a1)
1736 return 0;
1738 /* If one of the lists has an error marker, ignore this arg. */
1739 else if (TREE_CODE (a1) == ERROR_MARK
1740 || TREE_CODE (a2) == ERROR_MARK)
1742 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1743 different_types_p)))
1745 if (different_types_p != NULL)
1746 *different_types_p = true;
1747 /* Allow wait (union {union wait *u; int *i} *)
1748 and wait (union wait *) to be compatible. */
1749 if (TREE_CODE (a1) == UNION_TYPE
1750 && (TYPE_NAME (a1) == NULL_TREE
1751 || TYPE_TRANSPARENT_AGGR (a1))
1752 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1753 && tree_int_cst_equal (TYPE_SIZE (a1),
1754 TYPE_SIZE (a2)))
1756 tree memb;
1757 for (memb = TYPE_FIELDS (a1);
1758 memb; memb = DECL_CHAIN (memb))
1760 tree mv3 = TREE_TYPE (memb);
1761 if (mv3 && mv3 != error_mark_node
1762 && TREE_CODE (mv3) != ARRAY_TYPE)
1763 mv3 = (TYPE_ATOMIC (mv3)
1764 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1765 TYPE_QUAL_ATOMIC)
1766 : TYPE_MAIN_VARIANT (mv3));
1767 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1768 different_types_p))
1769 break;
1771 if (memb == NULL_TREE)
1772 return 0;
1774 else if (TREE_CODE (a2) == UNION_TYPE
1775 && (TYPE_NAME (a2) == NULL_TREE
1776 || TYPE_TRANSPARENT_AGGR (a2))
1777 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1778 && tree_int_cst_equal (TYPE_SIZE (a2),
1779 TYPE_SIZE (a1)))
1781 tree memb;
1782 for (memb = TYPE_FIELDS (a2);
1783 memb; memb = DECL_CHAIN (memb))
1785 tree mv3 = TREE_TYPE (memb);
1786 if (mv3 && mv3 != error_mark_node
1787 && TREE_CODE (mv3) != ARRAY_TYPE)
1788 mv3 = (TYPE_ATOMIC (mv3)
1789 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1790 TYPE_QUAL_ATOMIC)
1791 : TYPE_MAIN_VARIANT (mv3));
1792 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1793 different_types_p))
1794 break;
1796 if (memb == NULL_TREE)
1797 return 0;
1799 else
1800 return 0;
1803 /* comptypes said ok, but record if it said to warn. */
1804 if (newval > val)
1805 val = newval;
1807 args1 = TREE_CHAIN (args1);
1808 args2 = TREE_CHAIN (args2);
1812 /* Compute the size to increment a pointer by. When a function type or void
1813 type or incomplete type is passed, size_one_node is returned.
1814 This function does not emit any diagnostics; the caller is responsible
1815 for that. */
1817 static tree
1818 c_size_in_bytes (const_tree type)
1820 enum tree_code code = TREE_CODE (type);
1822 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1823 || !COMPLETE_TYPE_P (type))
1824 return size_one_node;
1826 /* Convert in case a char is more than one unit. */
1827 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1828 size_int (TYPE_PRECISION (char_type_node)
1829 / BITS_PER_UNIT));
1832 /* Return either DECL or its known constant value (if it has one). */
1834 tree
1835 decl_constant_value_1 (tree decl)
1837 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1838 TREE_CODE (decl) != PARM_DECL
1839 && !TREE_THIS_VOLATILE (decl)
1840 && TREE_READONLY (decl)
1841 && DECL_INITIAL (decl) != NULL_TREE
1842 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1843 /* This is invalid if initial value is not constant.
1844 If it has either a function call, a memory reference,
1845 or a variable, then re-evaluating it could give different results. */
1846 && TREE_CONSTANT (DECL_INITIAL (decl))
1847 /* Check for cases where this is sub-optimal, even though valid. */
1848 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1849 return DECL_INITIAL (decl);
1850 return decl;
1853 /* Return either DECL or its known constant value (if it has one).
1854 Like the above, but always return decl outside of functions. */
1856 tree
1857 decl_constant_value (tree decl)
1859 /* Don't change a variable array bound or initial value to a constant
1860 in a place where a variable is invalid. */
1861 return current_function_decl ? decl_constant_value_1 (decl) : decl;
1864 /* Convert the array expression EXP to a pointer. */
1865 static tree
1866 array_to_pointer_conversion (location_t loc, tree exp)
1868 tree orig_exp = exp;
1869 tree type = TREE_TYPE (exp);
1870 tree adr;
1871 tree restype = TREE_TYPE (type);
1872 tree ptrtype;
1874 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1876 STRIP_TYPE_NOPS (exp);
1878 if (TREE_NO_WARNING (orig_exp))
1879 TREE_NO_WARNING (exp) = 1;
1881 ptrtype = build_pointer_type (restype);
1883 if (INDIRECT_REF_P (exp))
1884 return convert (ptrtype, TREE_OPERAND (exp, 0));
1886 /* In C++ array compound literals are temporary objects unless they are
1887 const or appear in namespace scope, so they are destroyed too soon
1888 to use them for much of anything (c++/53220). */
1889 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1891 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1892 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1893 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1894 "converting an array compound literal to a pointer "
1895 "is ill-formed in C++");
1898 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1899 return convert (ptrtype, adr);
1902 /* Convert the function expression EXP to a pointer. */
1903 static tree
1904 function_to_pointer_conversion (location_t loc, tree exp)
1906 tree orig_exp = exp;
1908 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1910 STRIP_TYPE_NOPS (exp);
1912 if (TREE_NO_WARNING (orig_exp))
1913 TREE_NO_WARNING (exp) = 1;
1915 return build_unary_op (loc, ADDR_EXPR, exp, false);
1918 /* Mark EXP as read, not just set, for set but not used -Wunused
1919 warning purposes. */
1921 void
1922 mark_exp_read (tree exp)
1924 switch (TREE_CODE (exp))
1926 case VAR_DECL:
1927 case PARM_DECL:
1928 DECL_READ_P (exp) = 1;
1929 break;
1930 case ARRAY_REF:
1931 case COMPONENT_REF:
1932 case MODIFY_EXPR:
1933 case REALPART_EXPR:
1934 case IMAGPART_EXPR:
1935 CASE_CONVERT:
1936 case ADDR_EXPR:
1937 case VIEW_CONVERT_EXPR:
1938 mark_exp_read (TREE_OPERAND (exp, 0));
1939 break;
1940 case COMPOUND_EXPR:
1941 case C_MAYBE_CONST_EXPR:
1942 mark_exp_read (TREE_OPERAND (exp, 1));
1943 break;
1944 default:
1945 break;
1949 /* Perform the default conversion of arrays and functions to pointers.
1950 Return the result of converting EXP. For any other expression, just
1951 return EXP.
1953 LOC is the location of the expression. */
1955 struct c_expr
1956 default_function_array_conversion (location_t loc, struct c_expr exp)
1958 tree orig_exp = exp.value;
1959 tree type = TREE_TYPE (exp.value);
1960 enum tree_code code = TREE_CODE (type);
1962 switch (code)
1964 case ARRAY_TYPE:
1966 bool not_lvalue = false;
1967 bool lvalue_array_p;
1969 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1970 || CONVERT_EXPR_P (exp.value))
1971 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1973 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1974 not_lvalue = true;
1975 exp.value = TREE_OPERAND (exp.value, 0);
1978 if (TREE_NO_WARNING (orig_exp))
1979 TREE_NO_WARNING (exp.value) = 1;
1981 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1982 if (!flag_isoc99 && !lvalue_array_p)
1984 /* Before C99, non-lvalue arrays do not decay to pointers.
1985 Normally, using such an array would be invalid; but it can
1986 be used correctly inside sizeof or as a statement expression.
1987 Thus, do not give an error here; an error will result later. */
1988 return exp;
1991 exp.value = array_to_pointer_conversion (loc, exp.value);
1993 break;
1994 case FUNCTION_TYPE:
1995 exp.value = function_to_pointer_conversion (loc, exp.value);
1996 break;
1997 default:
1998 break;
2001 return exp;
2004 struct c_expr
2005 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2007 mark_exp_read (exp.value);
2008 return default_function_array_conversion (loc, exp);
2011 /* Return whether EXPR should be treated as an atomic lvalue for the
2012 purposes of load and store handling. */
2014 static bool
2015 really_atomic_lvalue (tree expr)
2017 if (error_operand_p (expr))
2018 return false;
2019 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2020 return false;
2021 if (!lvalue_p (expr))
2022 return false;
2024 /* Ignore _Atomic on register variables, since their addresses can't
2025 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2026 sequences wouldn't work. Ignore _Atomic on structures containing
2027 bit-fields, since accessing elements of atomic structures or
2028 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2029 it's undefined at translation time or execution time, and the
2030 normal atomic sequences again wouldn't work. */
2031 while (handled_component_p (expr))
2033 if (TREE_CODE (expr) == COMPONENT_REF
2034 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2035 return false;
2036 expr = TREE_OPERAND (expr, 0);
2038 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2039 return false;
2040 return true;
2043 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2044 including converting functions and arrays to pointers if CONVERT_P.
2045 If READ_P, also mark the expression as having been read. */
2047 struct c_expr
2048 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2049 bool convert_p, bool read_p)
2051 if (read_p)
2052 mark_exp_read (exp.value);
2053 if (convert_p)
2054 exp = default_function_array_conversion (loc, exp);
2055 if (really_atomic_lvalue (exp.value))
2057 vec<tree, va_gc> *params;
2058 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2059 tree expr_type = TREE_TYPE (exp.value);
2060 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2061 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2063 gcc_assert (TYPE_ATOMIC (expr_type));
2065 /* Expansion of a generic atomic load may require an addition
2066 element, so allocate enough to prevent a resize. */
2067 vec_alloc (params, 4);
2069 /* Remove the qualifiers for the rest of the expressions and
2070 create the VAL temp variable to hold the RHS. */
2071 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2072 tmp = create_tmp_var_raw (nonatomic_type);
2073 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2074 TREE_ADDRESSABLE (tmp) = 1;
2075 TREE_NO_WARNING (tmp) = 1;
2077 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2078 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2079 params->quick_push (expr_addr);
2080 params->quick_push (tmp_addr);
2081 params->quick_push (seq_cst);
2082 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2084 /* EXPR is always read. */
2085 mark_exp_read (exp.value);
2087 /* Return tmp which contains the value loaded. */
2088 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2089 NULL_TREE, NULL_TREE);
2091 return exp;
2094 /* EXP is an expression of integer type. Apply the integer promotions
2095 to it and return the promoted value. */
2097 tree
2098 perform_integral_promotions (tree exp)
2100 tree type = TREE_TYPE (exp);
2101 enum tree_code code = TREE_CODE (type);
2103 gcc_assert (INTEGRAL_TYPE_P (type));
2105 /* Normally convert enums to int,
2106 but convert wide enums to something wider. */
2107 if (code == ENUMERAL_TYPE)
2109 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2110 TYPE_PRECISION (integer_type_node)),
2111 ((TYPE_PRECISION (type)
2112 >= TYPE_PRECISION (integer_type_node))
2113 && TYPE_UNSIGNED (type)));
2115 return convert (type, exp);
2118 /* ??? This should no longer be needed now bit-fields have their
2119 proper types. */
2120 if (TREE_CODE (exp) == COMPONENT_REF
2121 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2122 /* If it's thinner than an int, promote it like a
2123 c_promoting_integer_type_p, otherwise leave it alone. */
2124 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2125 TYPE_PRECISION (integer_type_node)))
2126 return convert (integer_type_node, exp);
2128 if (c_promoting_integer_type_p (type))
2130 /* Preserve unsignedness if not really getting any wider. */
2131 if (TYPE_UNSIGNED (type)
2132 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2133 return convert (unsigned_type_node, exp);
2135 return convert (integer_type_node, exp);
2138 return exp;
2142 /* Perform default promotions for C data used in expressions.
2143 Enumeral types or short or char are converted to int.
2144 In addition, manifest constants symbols are replaced by their values. */
2146 tree
2147 default_conversion (tree exp)
2149 tree orig_exp;
2150 tree type = TREE_TYPE (exp);
2151 enum tree_code code = TREE_CODE (type);
2152 tree promoted_type;
2154 mark_exp_read (exp);
2156 /* Functions and arrays have been converted during parsing. */
2157 gcc_assert (code != FUNCTION_TYPE);
2158 if (code == ARRAY_TYPE)
2159 return exp;
2161 /* Constants can be used directly unless they're not loadable. */
2162 if (TREE_CODE (exp) == CONST_DECL)
2163 exp = DECL_INITIAL (exp);
2165 /* Strip no-op conversions. */
2166 orig_exp = exp;
2167 STRIP_TYPE_NOPS (exp);
2169 if (TREE_NO_WARNING (orig_exp))
2170 TREE_NO_WARNING (exp) = 1;
2172 if (code == VOID_TYPE)
2174 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2175 "void value not ignored as it ought to be");
2176 return error_mark_node;
2179 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2180 if (exp == error_mark_node)
2181 return error_mark_node;
2183 promoted_type = targetm.promoted_type (type);
2184 if (promoted_type)
2185 return convert (promoted_type, exp);
2187 if (INTEGRAL_TYPE_P (type))
2188 return perform_integral_promotions (exp);
2190 return exp;
2193 /* Look up COMPONENT in a structure or union TYPE.
2195 If the component name is not found, returns NULL_TREE. Otherwise,
2196 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2197 stepping down the chain to the component, which is in the last
2198 TREE_VALUE of the list. Normally the list is of length one, but if
2199 the component is embedded within (nested) anonymous structures or
2200 unions, the list steps down the chain to the component. */
2202 static tree
2203 lookup_field (tree type, tree component)
2205 tree field;
2207 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2208 to the field elements. Use a binary search on this array to quickly
2209 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2210 will always be set for structures which have many elements. */
2212 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2214 int bot, top, half;
2215 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2217 field = TYPE_FIELDS (type);
2218 bot = 0;
2219 top = TYPE_LANG_SPECIFIC (type)->s->len;
2220 while (top - bot > 1)
2222 half = (top - bot + 1) >> 1;
2223 field = field_array[bot+half];
2225 if (DECL_NAME (field) == NULL_TREE)
2227 /* Step through all anon unions in linear fashion. */
2228 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2230 field = field_array[bot++];
2231 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2233 tree anon = lookup_field (TREE_TYPE (field), component);
2235 if (anon)
2236 return tree_cons (NULL_TREE, field, anon);
2238 /* The Plan 9 compiler permits referring
2239 directly to an anonymous struct/union field
2240 using a typedef name. */
2241 if (flag_plan9_extensions
2242 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2243 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2244 == TYPE_DECL)
2245 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2246 == component))
2247 break;
2251 /* Entire record is only anon unions. */
2252 if (bot > top)
2253 return NULL_TREE;
2255 /* Restart the binary search, with new lower bound. */
2256 continue;
2259 if (DECL_NAME (field) == component)
2260 break;
2261 if (DECL_NAME (field) < component)
2262 bot += half;
2263 else
2264 top = bot + half;
2267 if (DECL_NAME (field_array[bot]) == component)
2268 field = field_array[bot];
2269 else if (DECL_NAME (field) != component)
2270 return NULL_TREE;
2272 else
2274 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2276 if (DECL_NAME (field) == NULL_TREE
2277 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2279 tree anon = lookup_field (TREE_TYPE (field), component);
2281 if (anon)
2282 return tree_cons (NULL_TREE, field, anon);
2284 /* The Plan 9 compiler permits referring directly to an
2285 anonymous struct/union field using a typedef
2286 name. */
2287 if (flag_plan9_extensions
2288 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2289 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2290 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2291 == component))
2292 break;
2295 if (DECL_NAME (field) == component)
2296 break;
2299 if (field == NULL_TREE)
2300 return NULL_TREE;
2303 return tree_cons (NULL_TREE, field, NULL_TREE);
2306 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2308 static void
2309 lookup_field_fuzzy_find_candidates (tree type, tree component,
2310 vec<tree> *candidates)
2312 tree field;
2313 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2315 if (DECL_NAME (field) == NULL_TREE
2316 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2317 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2318 candidates);
2320 if (DECL_NAME (field))
2321 candidates->safe_push (DECL_NAME (field));
2325 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2326 rather than returning a TREE_LIST for an exact match. */
2328 static tree
2329 lookup_field_fuzzy (tree type, tree component)
2331 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2333 /* First, gather a list of candidates. */
2334 auto_vec <tree> candidates;
2336 lookup_field_fuzzy_find_candidates (type, component,
2337 &candidates);
2339 return find_closest_identifier (component, &candidates);
2342 /* Support function for build_component_ref's error-handling.
2344 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2345 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2347 static bool
2348 should_suggest_deref_p (tree datum_type)
2350 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2351 allows "." for ptrs; we could be handling a failed attempt
2352 to access a property. */
2353 if (c_dialect_objc ())
2354 return false;
2356 /* Only suggest it for pointers... */
2357 if (TREE_CODE (datum_type) != POINTER_TYPE)
2358 return false;
2360 /* ...to structs/unions. */
2361 tree underlying_type = TREE_TYPE (datum_type);
2362 enum tree_code code = TREE_CODE (underlying_type);
2363 if (code == RECORD_TYPE || code == UNION_TYPE)
2364 return true;
2365 else
2366 return false;
2369 /* Make an expression to refer to the COMPONENT field of structure or
2370 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2371 location of the COMPONENT_REF. COMPONENT_LOC is the location
2372 of COMPONENT. */
2374 tree
2375 build_component_ref (location_t loc, tree datum, tree component,
2376 location_t component_loc)
2378 tree type = TREE_TYPE (datum);
2379 enum tree_code code = TREE_CODE (type);
2380 tree field = NULL;
2381 tree ref;
2382 bool datum_lvalue = lvalue_p (datum);
2384 if (!objc_is_public (datum, component))
2385 return error_mark_node;
2387 /* Detect Objective-C property syntax object.property. */
2388 if (c_dialect_objc ()
2389 && (ref = objc_maybe_build_component_ref (datum, component)))
2390 return ref;
2392 /* See if there is a field or component with name COMPONENT. */
2394 if (code == RECORD_TYPE || code == UNION_TYPE)
2396 if (!COMPLETE_TYPE_P (type))
2398 c_incomplete_type_error (loc, NULL_TREE, type);
2399 return error_mark_node;
2402 field = lookup_field (type, component);
2404 if (!field)
2406 tree guessed_id = lookup_field_fuzzy (type, component);
2407 if (guessed_id)
2409 /* Attempt to provide a fixit replacement hint, if
2410 we have a valid range for the component. */
2411 location_t reported_loc
2412 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2413 gcc_rich_location rich_loc (reported_loc);
2414 if (component_loc != UNKNOWN_LOCATION)
2415 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2416 error_at (&rich_loc,
2417 "%qT has no member named %qE; did you mean %qE?",
2418 type, component, guessed_id);
2420 else
2421 error_at (loc, "%qT has no member named %qE", type, component);
2422 return error_mark_node;
2425 /* Accessing elements of atomic structures or unions is undefined
2426 behavior (C11 6.5.2.3#5). */
2427 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2429 if (code == RECORD_TYPE)
2430 warning_at (loc, 0, "accessing a member %qE of an atomic "
2431 "structure %qE", component, datum);
2432 else
2433 warning_at (loc, 0, "accessing a member %qE of an atomic "
2434 "union %qE", component, datum);
2437 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2438 This might be better solved in future the way the C++ front
2439 end does it - by giving the anonymous entities each a
2440 separate name and type, and then have build_component_ref
2441 recursively call itself. We can't do that here. */
2444 tree subdatum = TREE_VALUE (field);
2445 int quals;
2446 tree subtype;
2447 bool use_datum_quals;
2449 if (TREE_TYPE (subdatum) == error_mark_node)
2450 return error_mark_node;
2452 /* If this is an rvalue, it does not have qualifiers in C
2453 standard terms and we must avoid propagating such
2454 qualifiers down to a non-lvalue array that is then
2455 converted to a pointer. */
2456 use_datum_quals = (datum_lvalue
2457 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2459 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2460 if (use_datum_quals)
2461 quals |= TYPE_QUALS (TREE_TYPE (datum));
2462 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2464 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2465 NULL_TREE);
2466 SET_EXPR_LOCATION (ref, loc);
2467 if (TREE_READONLY (subdatum)
2468 || (use_datum_quals && TREE_READONLY (datum)))
2469 TREE_READONLY (ref) = 1;
2470 if (TREE_THIS_VOLATILE (subdatum)
2471 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2472 TREE_THIS_VOLATILE (ref) = 1;
2474 if (TREE_DEPRECATED (subdatum))
2475 warn_deprecated_use (subdatum, NULL_TREE);
2477 datum = ref;
2479 field = TREE_CHAIN (field);
2481 while (field);
2483 return ref;
2485 else if (should_suggest_deref_p (type))
2487 /* Special-case the error message for "ptr.field" for the case
2488 where the user has confused "." vs "->". */
2489 rich_location richloc (line_table, loc);
2490 /* "loc" should be the "." token. */
2491 richloc.add_fixit_replace ("->");
2492 error_at (&richloc,
2493 "%qE is a pointer; did you mean to use %<->%>?",
2494 datum);
2495 return error_mark_node;
2497 else if (code != ERROR_MARK)
2498 error_at (loc,
2499 "request for member %qE in something not a structure or union",
2500 component);
2502 return error_mark_node;
2505 /* Given an expression PTR for a pointer, return an expression
2506 for the value pointed to.
2507 ERRORSTRING is the name of the operator to appear in error messages.
2509 LOC is the location to use for the generated tree. */
2511 tree
2512 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2514 tree pointer = default_conversion (ptr);
2515 tree type = TREE_TYPE (pointer);
2516 tree ref;
2518 if (TREE_CODE (type) == POINTER_TYPE)
2520 if (CONVERT_EXPR_P (pointer)
2521 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2523 /* If a warning is issued, mark it to avoid duplicates from
2524 the backend. This only needs to be done at
2525 warn_strict_aliasing > 2. */
2526 if (warn_strict_aliasing > 2)
2527 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2528 type, TREE_OPERAND (pointer, 0)))
2529 TREE_NO_WARNING (pointer) = 1;
2532 if (TREE_CODE (pointer) == ADDR_EXPR
2533 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2534 == TREE_TYPE (type)))
2536 ref = TREE_OPERAND (pointer, 0);
2537 protected_set_expr_location (ref, loc);
2538 return ref;
2540 else
2542 tree t = TREE_TYPE (type);
2544 ref = build1 (INDIRECT_REF, t, pointer);
2546 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2548 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2550 error_at (loc, "dereferencing pointer to incomplete type "
2551 "%qT", t);
2552 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2554 return error_mark_node;
2556 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2557 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2559 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2560 so that we get the proper error message if the result is used
2561 to assign to. Also, &* is supposed to be a no-op.
2562 And ANSI C seems to specify that the type of the result
2563 should be the const type. */
2564 /* A de-reference of a pointer to const is not a const. It is valid
2565 to change it via some other pointer. */
2566 TREE_READONLY (ref) = TYPE_READONLY (t);
2567 TREE_SIDE_EFFECTS (ref)
2568 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2569 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2570 protected_set_expr_location (ref, loc);
2571 return ref;
2574 else if (TREE_CODE (pointer) != ERROR_MARK)
2575 invalid_indirection_error (loc, type, errstring);
2577 return error_mark_node;
2580 /* This handles expressions of the form "a[i]", which denotes
2581 an array reference.
2583 This is logically equivalent in C to *(a+i), but we may do it differently.
2584 If A is a variable or a member, we generate a primitive ARRAY_REF.
2585 This avoids forcing the array out of registers, and can work on
2586 arrays that are not lvalues (for example, members of structures returned
2587 by functions).
2589 For vector types, allow vector[i] but not i[vector], and create
2590 *(((type*)&vectortype) + i) for the expression.
2592 LOC is the location to use for the returned expression. */
2594 tree
2595 build_array_ref (location_t loc, tree array, tree index)
2597 tree ret;
2598 bool swapped = false;
2599 if (TREE_TYPE (array) == error_mark_node
2600 || TREE_TYPE (index) == error_mark_node)
2601 return error_mark_node;
2603 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2604 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2605 /* Allow vector[index] but not index[vector]. */
2606 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2608 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2609 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2611 error_at (loc,
2612 "subscripted value is neither array nor pointer nor vector");
2614 return error_mark_node;
2616 std::swap (array, index);
2617 swapped = true;
2620 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2622 error_at (loc, "array subscript is not an integer");
2623 return error_mark_node;
2626 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2628 error_at (loc, "subscripted value is pointer to function");
2629 return error_mark_node;
2632 /* ??? Existing practice has been to warn only when the char
2633 index is syntactically the index, not for char[array]. */
2634 if (!swapped)
2635 warn_array_subscript_with_type_char (loc, index);
2637 /* Apply default promotions *after* noticing character types. */
2638 index = default_conversion (index);
2639 if (index == error_mark_node)
2640 return error_mark_node;
2642 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2644 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2645 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2647 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2649 tree rval, type;
2651 /* An array that is indexed by a non-constant
2652 cannot be stored in a register; we must be able to do
2653 address arithmetic on its address.
2654 Likewise an array of elements of variable size. */
2655 if (TREE_CODE (index) != INTEGER_CST
2656 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2657 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2659 if (!c_mark_addressable (array, true))
2660 return error_mark_node;
2662 /* An array that is indexed by a constant value which is not within
2663 the array bounds cannot be stored in a register either; because we
2664 would get a crash in store_bit_field/extract_bit_field when trying
2665 to access a non-existent part of the register. */
2666 if (TREE_CODE (index) == INTEGER_CST
2667 && TYPE_DOMAIN (TREE_TYPE (array))
2668 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2670 if (!c_mark_addressable (array))
2671 return error_mark_node;
2674 if ((pedantic || warn_c90_c99_compat)
2675 && ! was_vector)
2677 tree foo = array;
2678 while (TREE_CODE (foo) == COMPONENT_REF)
2679 foo = TREE_OPERAND (foo, 0);
2680 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2681 pedwarn (loc, OPT_Wpedantic,
2682 "ISO C forbids subscripting %<register%> array");
2683 else if (!lvalue_p (foo))
2684 pedwarn_c90 (loc, OPT_Wpedantic,
2685 "ISO C90 forbids subscripting non-lvalue "
2686 "array");
2689 type = TREE_TYPE (TREE_TYPE (array));
2690 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2691 /* Array ref is const/volatile if the array elements are
2692 or if the array is. */
2693 TREE_READONLY (rval)
2694 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2695 | TREE_READONLY (array));
2696 TREE_SIDE_EFFECTS (rval)
2697 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2698 | TREE_SIDE_EFFECTS (array));
2699 TREE_THIS_VOLATILE (rval)
2700 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2701 /* This was added by rms on 16 Nov 91.
2702 It fixes vol struct foo *a; a->elts[1]
2703 in an inline function.
2704 Hope it doesn't break something else. */
2705 | TREE_THIS_VOLATILE (array));
2706 ret = require_complete_type (loc, rval);
2707 protected_set_expr_location (ret, loc);
2708 if (non_lvalue)
2709 ret = non_lvalue_loc (loc, ret);
2710 return ret;
2712 else
2714 tree ar = default_conversion (array);
2716 if (ar == error_mark_node)
2717 return ar;
2719 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2720 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2722 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2723 index, false),
2724 RO_ARRAY_INDEXING);
2725 if (non_lvalue)
2726 ret = non_lvalue_loc (loc, ret);
2727 return ret;
2731 /* Build an external reference to identifier ID. FUN indicates
2732 whether this will be used for a function call. LOC is the source
2733 location of the identifier. This sets *TYPE to the type of the
2734 identifier, which is not the same as the type of the returned value
2735 for CONST_DECLs defined as enum constants. If the type of the
2736 identifier is not available, *TYPE is set to NULL. */
2737 tree
2738 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2740 tree ref;
2741 tree decl = lookup_name (id);
2743 /* In Objective-C, an instance variable (ivar) may be preferred to
2744 whatever lookup_name() found. */
2745 decl = objc_lookup_ivar (decl, id);
2747 *type = NULL;
2748 if (decl && decl != error_mark_node)
2750 ref = decl;
2751 *type = TREE_TYPE (ref);
2753 else if (fun)
2754 /* Implicit function declaration. */
2755 ref = implicitly_declare (loc, id);
2756 else if (decl == error_mark_node)
2757 /* Don't complain about something that's already been
2758 complained about. */
2759 return error_mark_node;
2760 else
2762 undeclared_variable (loc, id);
2763 return error_mark_node;
2766 if (TREE_TYPE (ref) == error_mark_node)
2767 return error_mark_node;
2769 if (TREE_DEPRECATED (ref))
2770 warn_deprecated_use (ref, NULL_TREE);
2772 /* Recursive call does not count as usage. */
2773 if (ref != current_function_decl)
2775 TREE_USED (ref) = 1;
2778 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2780 if (!in_sizeof && !in_typeof)
2781 C_DECL_USED (ref) = 1;
2782 else if (DECL_INITIAL (ref) == NULL_TREE
2783 && DECL_EXTERNAL (ref)
2784 && !TREE_PUBLIC (ref))
2785 record_maybe_used_decl (ref);
2788 if (TREE_CODE (ref) == CONST_DECL)
2790 used_types_insert (TREE_TYPE (ref));
2792 if (warn_cxx_compat
2793 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2794 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2796 warning_at (loc, OPT_Wc___compat,
2797 ("enum constant defined in struct or union "
2798 "is not visible in C++"));
2799 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2802 ref = DECL_INITIAL (ref);
2803 TREE_CONSTANT (ref) = 1;
2805 else if (current_function_decl != NULL_TREE
2806 && !DECL_FILE_SCOPE_P (current_function_decl)
2807 && (VAR_OR_FUNCTION_DECL_P (ref)
2808 || TREE_CODE (ref) == PARM_DECL))
2810 tree context = decl_function_context (ref);
2812 if (context != NULL_TREE && context != current_function_decl)
2813 DECL_NONLOCAL (ref) = 1;
2815 /* C99 6.7.4p3: An inline definition of a function with external
2816 linkage ... shall not contain a reference to an identifier with
2817 internal linkage. */
2818 else if (current_function_decl != NULL_TREE
2819 && DECL_DECLARED_INLINE_P (current_function_decl)
2820 && DECL_EXTERNAL (current_function_decl)
2821 && VAR_OR_FUNCTION_DECL_P (ref)
2822 && (!VAR_P (ref) || TREE_STATIC (ref))
2823 && ! TREE_PUBLIC (ref)
2824 && DECL_CONTEXT (ref) != current_function_decl)
2825 record_inline_static (loc, current_function_decl, ref,
2826 csi_internal);
2828 return ref;
2831 /* Record details of decls possibly used inside sizeof or typeof. */
2832 struct maybe_used_decl
2834 /* The decl. */
2835 tree decl;
2836 /* The level seen at (in_sizeof + in_typeof). */
2837 int level;
2838 /* The next one at this level or above, or NULL. */
2839 struct maybe_used_decl *next;
2842 static struct maybe_used_decl *maybe_used_decls;
2844 /* Record that DECL, an undefined static function reference seen
2845 inside sizeof or typeof, might be used if the operand of sizeof is
2846 a VLA type or the operand of typeof is a variably modified
2847 type. */
2849 static void
2850 record_maybe_used_decl (tree decl)
2852 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2853 t->decl = decl;
2854 t->level = in_sizeof + in_typeof;
2855 t->next = maybe_used_decls;
2856 maybe_used_decls = t;
2859 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2860 USED is false, just discard them. If it is true, mark them used
2861 (if no longer inside sizeof or typeof) or move them to the next
2862 level up (if still inside sizeof or typeof). */
2864 void
2865 pop_maybe_used (bool used)
2867 struct maybe_used_decl *p = maybe_used_decls;
2868 int cur_level = in_sizeof + in_typeof;
2869 while (p && p->level > cur_level)
2871 if (used)
2873 if (cur_level == 0)
2874 C_DECL_USED (p->decl) = 1;
2875 else
2876 p->level = cur_level;
2878 p = p->next;
2880 if (!used || cur_level == 0)
2881 maybe_used_decls = p;
2884 /* Return the result of sizeof applied to EXPR. */
2886 struct c_expr
2887 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2889 struct c_expr ret;
2890 if (expr.value == error_mark_node)
2892 ret.value = error_mark_node;
2893 ret.original_code = ERROR_MARK;
2894 ret.original_type = NULL;
2895 pop_maybe_used (false);
2897 else
2899 bool expr_const_operands = true;
2901 if (TREE_CODE (expr.value) == PARM_DECL
2902 && C_ARRAY_PARAMETER (expr.value))
2904 if (warning_at (loc, OPT_Wsizeof_array_argument,
2905 "%<sizeof%> on array function parameter %qE will "
2906 "return size of %qT", expr.value,
2907 TREE_TYPE (expr.value)))
2908 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2910 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2911 &expr_const_operands);
2912 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2913 c_last_sizeof_arg = expr.value;
2914 c_last_sizeof_loc = loc;
2915 ret.original_code = SIZEOF_EXPR;
2916 ret.original_type = NULL;
2917 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2919 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2920 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2921 folded_expr, ret.value);
2922 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2923 SET_EXPR_LOCATION (ret.value, loc);
2925 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2927 return ret;
2930 /* Return the result of sizeof applied to T, a structure for the type
2931 name passed to sizeof (rather than the type itself). LOC is the
2932 location of the original expression. */
2934 struct c_expr
2935 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2937 tree type;
2938 struct c_expr ret;
2939 tree type_expr = NULL_TREE;
2940 bool type_expr_const = true;
2941 type = groktypename (t, &type_expr, &type_expr_const);
2942 ret.value = c_sizeof (loc, type);
2943 c_last_sizeof_arg = type;
2944 c_last_sizeof_loc = loc;
2945 ret.original_code = SIZEOF_EXPR;
2946 ret.original_type = NULL;
2947 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2948 && c_vla_type_p (type))
2950 /* If the type is a [*] array, it is a VLA but is represented as
2951 having a size of zero. In such a case we must ensure that
2952 the result of sizeof does not get folded to a constant by
2953 c_fully_fold, because if the size is evaluated the result is
2954 not constant and so constraints on zero or negative size
2955 arrays must not be applied when this sizeof call is inside
2956 another array declarator. */
2957 if (!type_expr)
2958 type_expr = integer_zero_node;
2959 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2960 type_expr, ret.value);
2961 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2963 pop_maybe_used (type != error_mark_node
2964 ? C_TYPE_VARIABLE_SIZE (type) : false);
2965 return ret;
2968 /* Build a function call to function FUNCTION with parameters PARAMS.
2969 The function call is at LOC.
2970 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2971 TREE_VALUE of each node is a parameter-expression.
2972 FUNCTION's data type may be a function type or a pointer-to-function. */
2974 tree
2975 build_function_call (location_t loc, tree function, tree params)
2977 vec<tree, va_gc> *v;
2978 tree ret;
2980 vec_alloc (v, list_length (params));
2981 for (; params; params = TREE_CHAIN (params))
2982 v->quick_push (TREE_VALUE (params));
2983 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2984 vec_free (v);
2985 return ret;
2988 /* Give a note about the location of the declaration of DECL. */
2990 static void
2991 inform_declaration (tree decl)
2993 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2994 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2997 /* Build a function call to function FUNCTION with parameters PARAMS.
2998 ORIGTYPES, if not NULL, is a vector of types; each element is
2999 either NULL or the original type of the corresponding element in
3000 PARAMS. The original type may differ from TREE_TYPE of the
3001 parameter for enums. FUNCTION's data type may be a function type
3002 or pointer-to-function. This function changes the elements of
3003 PARAMS. */
3005 tree
3006 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3007 tree function, vec<tree, va_gc> *params,
3008 vec<tree, va_gc> *origtypes)
3010 tree fntype, fundecl = NULL_TREE;
3011 tree name = NULL_TREE, result;
3012 tree tem;
3013 int nargs;
3014 tree *argarray;
3017 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3018 STRIP_TYPE_NOPS (function);
3020 /* Convert anything with function type to a pointer-to-function. */
3021 if (TREE_CODE (function) == FUNCTION_DECL)
3023 name = DECL_NAME (function);
3025 if (flag_tm)
3026 tm_malloc_replacement (function);
3027 fundecl = function;
3028 /* Atomic functions have type checking/casting already done. They are
3029 often rewritten and don't match the original parameter list. */
3030 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3031 origtypes = NULL;
3033 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3034 function = function_to_pointer_conversion (loc, function);
3036 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3037 expressions, like those used for ObjC messenger dispatches. */
3038 if (params && !params->is_empty ())
3039 function = objc_rewrite_function_call (function, (*params)[0]);
3041 function = c_fully_fold (function, false, NULL);
3043 fntype = TREE_TYPE (function);
3045 if (TREE_CODE (fntype) == ERROR_MARK)
3046 return error_mark_node;
3048 if (!(TREE_CODE (fntype) == POINTER_TYPE
3049 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3051 if (!flag_diagnostics_show_caret)
3052 error_at (loc,
3053 "called object %qE is not a function or function pointer",
3054 function);
3055 else if (DECL_P (function))
3057 error_at (loc,
3058 "called object %qD is not a function or function pointer",
3059 function);
3060 inform_declaration (function);
3062 else
3063 error_at (loc,
3064 "called object is not a function or function pointer");
3065 return error_mark_node;
3068 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3069 current_function_returns_abnormally = 1;
3071 /* fntype now gets the type of function pointed to. */
3072 fntype = TREE_TYPE (fntype);
3074 /* Convert the parameters to the types declared in the
3075 function prototype, or apply default promotions. */
3077 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3078 origtypes, function, fundecl);
3079 if (nargs < 0)
3080 return error_mark_node;
3082 /* Check that the function is called through a compatible prototype.
3083 If it is not, warn. */
3084 if (CONVERT_EXPR_P (function)
3085 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3086 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3087 && !comptypes (fntype, TREE_TYPE (tem)))
3089 tree return_type = TREE_TYPE (fntype);
3091 /* This situation leads to run-time undefined behavior. We can't,
3092 therefore, simply error unless we can prove that all possible
3093 executions of the program must execute the code. */
3094 warning_at (loc, 0, "function called through a non-compatible type");
3096 if (VOID_TYPE_P (return_type)
3097 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3098 pedwarn (loc, 0,
3099 "function with qualified void return type called");
3102 argarray = vec_safe_address (params);
3104 /* Check that arguments to builtin functions match the expectations. */
3105 if (fundecl
3106 && DECL_BUILT_IN (fundecl)
3107 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3108 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3109 argarray))
3110 return error_mark_node;
3112 /* Check that the arguments to the function are valid. */
3113 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3114 nargs, argarray, &arg_loc);
3116 if (name != NULL_TREE
3117 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3119 if (require_constant_value)
3120 result
3121 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3122 function, nargs, argarray);
3123 else
3124 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3125 function, nargs, argarray);
3126 if (TREE_CODE (result) == NOP_EXPR
3127 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3128 STRIP_TYPE_NOPS (result);
3130 else
3131 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3132 function, nargs, argarray);
3133 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3134 later. */
3135 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3136 TREE_NO_WARNING (result) = 1;
3138 /* In this improbable scenario, a nested function returns a VM type.
3139 Create a TARGET_EXPR so that the call always has a LHS, much as
3140 what the C++ FE does for functions returning non-PODs. */
3141 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3143 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3144 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3145 NULL_TREE, NULL_TREE);
3148 if (VOID_TYPE_P (TREE_TYPE (result)))
3150 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3151 pedwarn (loc, 0,
3152 "function with qualified void return type called");
3153 return result;
3155 return require_complete_type (loc, result);
3158 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3160 tree
3161 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3162 tree function, vec<tree, va_gc> *params,
3163 vec<tree, va_gc> *origtypes)
3165 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3166 STRIP_TYPE_NOPS (function);
3168 /* Convert anything with function type to a pointer-to-function. */
3169 if (TREE_CODE (function) == FUNCTION_DECL)
3171 /* Implement type-directed function overloading for builtins.
3172 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3173 handle all the type checking. The result is a complete expression
3174 that implements this function call. */
3175 tree tem = resolve_overloaded_builtin (loc, function, params);
3176 if (tem)
3177 return tem;
3179 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3182 /* Convert the argument expressions in the vector VALUES
3183 to the types in the list TYPELIST.
3185 If TYPELIST is exhausted, or when an element has NULL as its type,
3186 perform the default conversions.
3188 ORIGTYPES is the original types of the expressions in VALUES. This
3189 holds the type of enum values which have been converted to integral
3190 types. It may be NULL.
3192 FUNCTION is a tree for the called function. It is used only for
3193 error messages, where it is formatted with %qE.
3195 This is also where warnings about wrong number of args are generated.
3197 ARG_LOC are locations of function arguments (if any).
3199 Returns the actual number of arguments processed (which may be less
3200 than the length of VALUES in some error situations), or -1 on
3201 failure. */
3203 static int
3204 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3205 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3206 tree function, tree fundecl)
3208 tree typetail, val;
3209 unsigned int parmnum;
3210 bool error_args = false;
3211 const bool type_generic = fundecl
3212 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3213 bool type_generic_remove_excess_precision = false;
3214 bool type_generic_overflow_p = false;
3215 tree selector;
3217 /* Change pointer to function to the function itself for
3218 diagnostics. */
3219 if (TREE_CODE (function) == ADDR_EXPR
3220 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3221 function = TREE_OPERAND (function, 0);
3223 /* Handle an ObjC selector specially for diagnostics. */
3224 selector = objc_message_selector ();
3226 /* For type-generic built-in functions, determine whether excess
3227 precision should be removed (classification) or not
3228 (comparison). */
3229 if (type_generic
3230 && DECL_BUILT_IN (fundecl)
3231 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3233 switch (DECL_FUNCTION_CODE (fundecl))
3235 case BUILT_IN_ISFINITE:
3236 case BUILT_IN_ISINF:
3237 case BUILT_IN_ISINF_SIGN:
3238 case BUILT_IN_ISNAN:
3239 case BUILT_IN_ISNORMAL:
3240 case BUILT_IN_FPCLASSIFY:
3241 type_generic_remove_excess_precision = true;
3242 break;
3244 case BUILT_IN_ADD_OVERFLOW_P:
3245 case BUILT_IN_SUB_OVERFLOW_P:
3246 case BUILT_IN_MUL_OVERFLOW_P:
3247 /* The last argument of these type-generic builtins
3248 should not be promoted. */
3249 type_generic_overflow_p = true;
3250 break;
3252 default:
3253 break;
3257 /* Scan the given expressions and types, producing individual
3258 converted arguments. */
3260 for (typetail = typelist, parmnum = 0;
3261 values && values->iterate (parmnum, &val);
3262 ++parmnum)
3264 tree type = typetail ? TREE_VALUE (typetail) : 0;
3265 tree valtype = TREE_TYPE (val);
3266 tree rname = function;
3267 int argnum = parmnum + 1;
3268 const char *invalid_func_diag;
3269 bool excess_precision = false;
3270 bool npc;
3271 tree parmval;
3272 /* Some __atomic_* builtins have additional hidden argument at
3273 position 0. */
3274 location_t ploc
3275 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3276 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3277 : input_location;
3279 if (type == void_type_node)
3281 if (selector)
3282 error_at (loc, "too many arguments to method %qE", selector);
3283 else
3284 error_at (loc, "too many arguments to function %qE", function);
3285 inform_declaration (fundecl);
3286 return error_args ? -1 : (int) parmnum;
3289 if (selector && argnum > 2)
3291 rname = selector;
3292 argnum -= 2;
3295 npc = null_pointer_constant_p (val);
3297 /* If there is excess precision and a prototype, convert once to
3298 the required type rather than converting via the semantic
3299 type. Likewise without a prototype a float value represented
3300 as long double should be converted once to double. But for
3301 type-generic classification functions excess precision must
3302 be removed here. */
3303 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3304 && (type || !type_generic || !type_generic_remove_excess_precision))
3306 val = TREE_OPERAND (val, 0);
3307 excess_precision = true;
3309 val = c_fully_fold (val, false, NULL);
3310 STRIP_TYPE_NOPS (val);
3312 val = require_complete_type (ploc, val);
3314 /* Some floating-point arguments must be promoted to double when
3315 no type is specified by a prototype. This applies to
3316 arguments of type float, and to architecture-specific types
3317 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3318 bool promote_float_arg = false;
3319 if (type == NULL_TREE
3320 && TREE_CODE (valtype) == REAL_TYPE
3321 && (TYPE_PRECISION (valtype)
3322 <= TYPE_PRECISION (double_type_node))
3323 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3324 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3325 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3327 /* Promote this argument, unless it has a _FloatN or
3328 _FloatNx type. */
3329 promote_float_arg = true;
3330 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3331 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3333 promote_float_arg = false;
3334 break;
3338 if (type != NULL_TREE)
3340 /* Formal parm type is specified by a function prototype. */
3342 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3344 error_at (ploc, "type of formal parameter %d is incomplete",
3345 parmnum + 1);
3346 parmval = val;
3348 else
3350 tree origtype;
3352 /* Optionally warn about conversions that
3353 differ from the default conversions. */
3354 if (warn_traditional_conversion || warn_traditional)
3356 unsigned int formal_prec = TYPE_PRECISION (type);
3358 if (INTEGRAL_TYPE_P (type)
3359 && TREE_CODE (valtype) == REAL_TYPE)
3360 warning_at (ploc, OPT_Wtraditional_conversion,
3361 "passing argument %d of %qE as integer rather "
3362 "than floating due to prototype",
3363 argnum, rname);
3364 if (INTEGRAL_TYPE_P (type)
3365 && TREE_CODE (valtype) == COMPLEX_TYPE)
3366 warning_at (ploc, OPT_Wtraditional_conversion,
3367 "passing argument %d of %qE as integer rather "
3368 "than complex due to prototype",
3369 argnum, rname);
3370 else if (TREE_CODE (type) == COMPLEX_TYPE
3371 && TREE_CODE (valtype) == REAL_TYPE)
3372 warning_at (ploc, OPT_Wtraditional_conversion,
3373 "passing argument %d of %qE as complex rather "
3374 "than floating due to prototype",
3375 argnum, rname);
3376 else if (TREE_CODE (type) == REAL_TYPE
3377 && INTEGRAL_TYPE_P (valtype))
3378 warning_at (ploc, OPT_Wtraditional_conversion,
3379 "passing argument %d of %qE as floating rather "
3380 "than integer due to prototype",
3381 argnum, rname);
3382 else if (TREE_CODE (type) == COMPLEX_TYPE
3383 && INTEGRAL_TYPE_P (valtype))
3384 warning_at (ploc, OPT_Wtraditional_conversion,
3385 "passing argument %d of %qE as complex rather "
3386 "than integer due to prototype",
3387 argnum, rname);
3388 else if (TREE_CODE (type) == REAL_TYPE
3389 && TREE_CODE (valtype) == COMPLEX_TYPE)
3390 warning_at (ploc, OPT_Wtraditional_conversion,
3391 "passing argument %d of %qE as floating rather "
3392 "than complex due to prototype",
3393 argnum, rname);
3394 /* ??? At some point, messages should be written about
3395 conversions between complex types, but that's too messy
3396 to do now. */
3397 else if (TREE_CODE (type) == REAL_TYPE
3398 && TREE_CODE (valtype) == REAL_TYPE)
3400 /* Warn if any argument is passed as `float',
3401 since without a prototype it would be `double'. */
3402 if (formal_prec == TYPE_PRECISION (float_type_node)
3403 && type != dfloat32_type_node)
3404 warning_at (ploc, 0,
3405 "passing argument %d of %qE as %<float%> "
3406 "rather than %<double%> due to prototype",
3407 argnum, rname);
3409 /* Warn if mismatch between argument and prototype
3410 for decimal float types. Warn of conversions with
3411 binary float types and of precision narrowing due to
3412 prototype. */
3413 else if (type != valtype
3414 && (type == dfloat32_type_node
3415 || type == dfloat64_type_node
3416 || type == dfloat128_type_node
3417 || valtype == dfloat32_type_node
3418 || valtype == dfloat64_type_node
3419 || valtype == dfloat128_type_node)
3420 && (formal_prec
3421 <= TYPE_PRECISION (valtype)
3422 || (type == dfloat128_type_node
3423 && (valtype
3424 != dfloat64_type_node
3425 && (valtype
3426 != dfloat32_type_node)))
3427 || (type == dfloat64_type_node
3428 && (valtype
3429 != dfloat32_type_node))))
3430 warning_at (ploc, 0,
3431 "passing argument %d of %qE as %qT "
3432 "rather than %qT due to prototype",
3433 argnum, rname, type, valtype);
3436 /* Detect integer changing in width or signedness.
3437 These warnings are only activated with
3438 -Wtraditional-conversion, not with -Wtraditional. */
3439 else if (warn_traditional_conversion
3440 && INTEGRAL_TYPE_P (type)
3441 && INTEGRAL_TYPE_P (valtype))
3443 tree would_have_been = default_conversion (val);
3444 tree type1 = TREE_TYPE (would_have_been);
3446 if (val == error_mark_node)
3447 /* VAL could have been of incomplete type. */;
3448 else if (TREE_CODE (type) == ENUMERAL_TYPE
3449 && (TYPE_MAIN_VARIANT (type)
3450 == TYPE_MAIN_VARIANT (valtype)))
3451 /* No warning if function asks for enum
3452 and the actual arg is that enum type. */
3454 else if (formal_prec != TYPE_PRECISION (type1))
3455 warning_at (ploc, OPT_Wtraditional_conversion,
3456 "passing argument %d of %qE "
3457 "with different width due to prototype",
3458 argnum, rname);
3459 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3461 /* Don't complain if the formal parameter type
3462 is an enum, because we can't tell now whether
3463 the value was an enum--even the same enum. */
3464 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3466 else if (TREE_CODE (val) == INTEGER_CST
3467 && int_fits_type_p (val, type))
3468 /* Change in signedness doesn't matter
3469 if a constant value is unaffected. */
3471 /* If the value is extended from a narrower
3472 unsigned type, it doesn't matter whether we
3473 pass it as signed or unsigned; the value
3474 certainly is the same either way. */
3475 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3476 && TYPE_UNSIGNED (valtype))
3478 else if (TYPE_UNSIGNED (type))
3479 warning_at (ploc, OPT_Wtraditional_conversion,
3480 "passing argument %d of %qE "
3481 "as unsigned due to prototype",
3482 argnum, rname);
3483 else
3484 warning_at (ploc, OPT_Wtraditional_conversion,
3485 "passing argument %d of %qE "
3486 "as signed due to prototype",
3487 argnum, rname);
3491 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3492 sake of better warnings from convert_and_check. */
3493 if (excess_precision)
3494 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3495 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3496 parmval = convert_for_assignment (loc, ploc, type,
3497 val, origtype, ic_argpass,
3498 npc, fundecl, function,
3499 parmnum + 1);
3501 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3502 && INTEGRAL_TYPE_P (type)
3503 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3504 parmval = default_conversion (parmval);
3507 else if (promote_float_arg)
3509 if (type_generic)
3510 parmval = val;
3511 else
3513 /* Convert `float' to `double'. */
3514 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3515 warning_at (ploc, OPT_Wdouble_promotion,
3516 "implicit conversion from %qT to %qT when passing "
3517 "argument to function",
3518 valtype, double_type_node);
3519 parmval = convert (double_type_node, val);
3522 else if ((excess_precision && !type_generic)
3523 || (type_generic_overflow_p && parmnum == 2))
3524 /* A "double" argument with excess precision being passed
3525 without a prototype or in variable arguments.
3526 The last argument of __builtin_*_overflow_p should not be
3527 promoted. */
3528 parmval = convert (valtype, val);
3529 else if ((invalid_func_diag =
3530 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3532 error (invalid_func_diag);
3533 return -1;
3535 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3537 return -1;
3539 else
3540 /* Convert `short' and `char' to full-size `int'. */
3541 parmval = default_conversion (val);
3543 (*values)[parmnum] = parmval;
3544 if (parmval == error_mark_node)
3545 error_args = true;
3547 if (typetail)
3548 typetail = TREE_CHAIN (typetail);
3551 gcc_assert (parmnum == vec_safe_length (values));
3553 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3555 error_at (loc, "too few arguments to function %qE", function);
3556 inform_declaration (fundecl);
3557 return -1;
3560 return error_args ? -1 : (int) parmnum;
3563 /* This is the entry point used by the parser to build unary operators
3564 in the input. CODE, a tree_code, specifies the unary operator, and
3565 ARG is the operand. For unary plus, the C parser currently uses
3566 CONVERT_EXPR for code.
3568 LOC is the location to use for the tree generated.
3571 struct c_expr
3572 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3574 struct c_expr result;
3576 result.original_code = code;
3577 result.original_type = NULL;
3579 if (reject_gcc_builtin (arg.value))
3581 result.value = error_mark_node;
3583 else
3585 result.value = build_unary_op (loc, code, arg.value, false);
3587 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3588 overflow_warning (loc, result.value, arg.value);
3591 /* We are typically called when parsing a prefix token at LOC acting on
3592 ARG. Reflect this by updating the source range of the result to
3593 start at LOC and end at the end of ARG. */
3594 set_c_expr_source_range (&result,
3595 loc, arg.get_finish ());
3597 return result;
3600 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3602 static bool
3603 char_type_p (tree type)
3605 return (type == char_type_node
3606 || type == unsigned_char_type_node
3607 || type == signed_char_type_node
3608 || type == char16_type_node
3609 || type == char32_type_node);
3612 /* This is the entry point used by the parser to build binary operators
3613 in the input. CODE, a tree_code, specifies the binary operator, and
3614 ARG1 and ARG2 are the operands. In addition to constructing the
3615 expression, we check for operands that were written with other binary
3616 operators in a way that is likely to confuse the user.
3618 LOCATION is the location of the binary operator. */
3620 struct c_expr
3621 parser_build_binary_op (location_t location, enum tree_code code,
3622 struct c_expr arg1, struct c_expr arg2)
3624 struct c_expr result;
3626 enum tree_code code1 = arg1.original_code;
3627 enum tree_code code2 = arg2.original_code;
3628 tree type1 = (arg1.original_type
3629 ? arg1.original_type
3630 : TREE_TYPE (arg1.value));
3631 tree type2 = (arg2.original_type
3632 ? arg2.original_type
3633 : TREE_TYPE (arg2.value));
3635 result.value = build_binary_op (location, code,
3636 arg1.value, arg2.value, true);
3637 result.original_code = code;
3638 result.original_type = NULL;
3640 if (TREE_CODE (result.value) == ERROR_MARK)
3642 set_c_expr_source_range (&result,
3643 arg1.get_start (),
3644 arg2.get_finish ());
3645 return result;
3648 if (location != UNKNOWN_LOCATION)
3649 protected_set_expr_location (result.value, location);
3651 set_c_expr_source_range (&result,
3652 arg1.get_start (),
3653 arg2.get_finish ());
3655 /* Check for cases such as x+y<<z which users are likely
3656 to misinterpret. */
3657 if (warn_parentheses)
3658 warn_about_parentheses (location, code, code1, arg1.value, code2,
3659 arg2.value);
3661 if (warn_logical_op)
3662 warn_logical_operator (location, code, TREE_TYPE (result.value),
3663 code1, arg1.value, code2, arg2.value);
3665 if (warn_tautological_compare)
3667 tree lhs = arg1.value;
3668 tree rhs = arg2.value;
3669 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3671 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3672 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3673 lhs = NULL_TREE;
3674 else
3675 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3677 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3679 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3680 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3681 rhs = NULL_TREE;
3682 else
3683 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3685 if (lhs != NULL_TREE && rhs != NULL_TREE)
3686 warn_tautological_cmp (location, code, lhs, rhs);
3689 if (warn_logical_not_paren
3690 && TREE_CODE_CLASS (code) == tcc_comparison
3691 && code1 == TRUTH_NOT_EXPR
3692 && code2 != TRUTH_NOT_EXPR
3693 /* Avoid warning for !!x == y. */
3694 && (TREE_CODE (arg1.value) != NE_EXPR
3695 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3697 /* Avoid warning for !b == y where b has _Bool type. */
3698 tree t = integer_zero_node;
3699 if (TREE_CODE (arg1.value) == EQ_EXPR
3700 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3701 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3703 t = TREE_OPERAND (arg1.value, 0);
3706 if (TREE_TYPE (t) != integer_type_node)
3707 break;
3708 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3709 t = C_MAYBE_CONST_EXPR_EXPR (t);
3710 else if (CONVERT_EXPR_P (t))
3711 t = TREE_OPERAND (t, 0);
3712 else
3713 break;
3715 while (1);
3717 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3718 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3721 /* Warn about comparisons against string literals, with the exception
3722 of testing for equality or inequality of a string literal with NULL. */
3723 if (code == EQ_EXPR || code == NE_EXPR)
3725 if ((code1 == STRING_CST
3726 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3727 || (code2 == STRING_CST
3728 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3729 warning_at (location, OPT_Waddress,
3730 "comparison with string literal results in unspecified behavior");
3731 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3732 if (POINTER_TYPE_P (type1)
3733 && null_pointer_constant_p (arg2.value)
3734 && char_type_p (type2)
3735 && warning_at (location, OPT_Wpointer_compare,
3736 "comparison between pointer and zero character "
3737 "constant"))
3738 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3739 else if (POINTER_TYPE_P (type2)
3740 && null_pointer_constant_p (arg1.value)
3741 && char_type_p (type1)
3742 && warning_at (location, OPT_Wpointer_compare,
3743 "comparison between pointer and zero character "
3744 "constant"))
3745 inform (arg2.get_start (), "did you mean to dereference the pointer?");
3747 else if (TREE_CODE_CLASS (code) == tcc_comparison
3748 && (code1 == STRING_CST || code2 == STRING_CST))
3749 warning_at (location, OPT_Waddress,
3750 "comparison with string literal results in unspecified behavior");
3752 if (TREE_OVERFLOW_P (result.value)
3753 && !TREE_OVERFLOW_P (arg1.value)
3754 && !TREE_OVERFLOW_P (arg2.value))
3755 overflow_warning (location, result.value);
3757 /* Warn about comparisons of different enum types. */
3758 if (warn_enum_compare
3759 && TREE_CODE_CLASS (code) == tcc_comparison
3760 && TREE_CODE (type1) == ENUMERAL_TYPE
3761 && TREE_CODE (type2) == ENUMERAL_TYPE
3762 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3763 warning_at (location, OPT_Wenum_compare,
3764 "comparison between %qT and %qT",
3765 type1, type2);
3767 return result;
3770 /* Return a tree for the difference of pointers OP0 and OP1.
3771 The resulting tree has type ptrdiff_t. */
3773 static tree
3774 pointer_diff (location_t loc, tree op0, tree op1)
3776 tree restype = ptrdiff_type_node;
3777 tree result, inttype;
3779 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3780 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3781 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3782 tree orig_op1 = op1;
3784 /* If the operands point into different address spaces, we need to
3785 explicitly convert them to pointers into the common address space
3786 before we can subtract the numerical address values. */
3787 if (as0 != as1)
3789 addr_space_t as_common;
3790 tree common_type;
3792 /* Determine the common superset address space. This is guaranteed
3793 to exist because the caller verified that comp_target_types
3794 returned non-zero. */
3795 if (!addr_space_superset (as0, as1, &as_common))
3796 gcc_unreachable ();
3798 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3799 op0 = convert (common_type, op0);
3800 op1 = convert (common_type, op1);
3803 /* Determine integer type result of the subtraction. This will usually
3804 be the same as the result type (ptrdiff_t), but may need to be a wider
3805 type if pointers for the address space are wider than ptrdiff_t. */
3806 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3807 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3808 else
3809 inttype = restype;
3811 if (TREE_CODE (target_type) == VOID_TYPE)
3812 pedwarn (loc, OPT_Wpointer_arith,
3813 "pointer of type %<void *%> used in subtraction");
3814 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3815 pedwarn (loc, OPT_Wpointer_arith,
3816 "pointer to a function used in subtraction");
3818 /* First do the subtraction, then build the divide operator
3819 and only convert at the very end.
3820 Do not do default conversions in case restype is a short type. */
3822 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3823 pointers. If some platform cannot provide that, or has a larger
3824 ptrdiff_type to support differences larger than half the address
3825 space, cast the pointers to some larger integer type and do the
3826 computations in that type. */
3827 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
3828 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3829 convert (inttype, op1), false);
3830 else
3831 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3833 /* This generates an error if op1 is pointer to incomplete type. */
3834 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3835 error_at (loc, "arithmetic on pointer to an incomplete type");
3837 op1 = c_size_in_bytes (target_type);
3839 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3840 error_at (loc, "arithmetic on pointer to an empty aggregate");
3842 /* Divide by the size, in easiest possible way. */
3843 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3844 op0, convert (inttype, op1));
3846 /* Convert to final result type if necessary. */
3847 return convert (restype, result);
3850 /* Expand atomic compound assignments into an appropriate sequence as
3851 specified by the C11 standard section 6.5.16.2.
3853 _Atomic T1 E1
3854 T2 E2
3855 E1 op= E2
3857 This sequence is used for all types for which these operations are
3858 supported.
3860 In addition, built-in versions of the 'fe' prefixed routines may
3861 need to be invoked for floating point (real, complex or vector) when
3862 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3864 T1 newval;
3865 T1 old;
3866 T1 *addr
3867 T2 val
3868 fenv_t fenv
3870 addr = &E1;
3871 val = (E2);
3872 __atomic_load (addr, &old, SEQ_CST);
3873 feholdexcept (&fenv);
3874 loop:
3875 newval = old op val;
3876 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3877 SEQ_CST))
3878 goto done;
3879 feclearexcept (FE_ALL_EXCEPT);
3880 goto loop:
3881 done:
3882 feupdateenv (&fenv);
3884 The compiler will issue the __atomic_fetch_* built-in when possible,
3885 otherwise it will generate the generic form of the atomic operations.
3886 This requires temp(s) and has their address taken. The atomic processing
3887 is smart enough to figure out when the size of an object can utilize
3888 a lock-free version, and convert the built-in call to the appropriate
3889 lock-free routine. The optimizers will then dispose of any temps that
3890 are no longer required, and lock-free implementations are utilized as
3891 long as there is target support for the required size.
3893 If the operator is NOP_EXPR, then this is a simple assignment, and
3894 an __atomic_store is issued to perform the assignment rather than
3895 the above loop. */
3897 /* Build an atomic assignment at LOC, expanding into the proper
3898 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3899 the result of the operation, unless RETURN_OLD_P, in which case
3900 return the old value of LHS (this is only for postincrement and
3901 postdecrement). */
3903 static tree
3904 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3905 tree rhs, bool return_old_p)
3907 tree fndecl, func_call;
3908 vec<tree, va_gc> *params;
3909 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3910 tree old, old_addr;
3911 tree compound_stmt;
3912 tree stmt, goto_stmt;
3913 tree loop_label, loop_decl, done_label, done_decl;
3915 tree lhs_type = TREE_TYPE (lhs);
3916 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3917 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3918 tree rhs_semantic_type = TREE_TYPE (rhs);
3919 tree nonatomic_rhs_semantic_type;
3920 tree rhs_type;
3922 gcc_assert (TYPE_ATOMIC (lhs_type));
3924 if (return_old_p)
3925 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3927 /* Allocate enough vector items for a compare_exchange. */
3928 vec_alloc (params, 6);
3930 /* Create a compound statement to hold the sequence of statements
3931 with a loop. */
3932 compound_stmt = c_begin_compound_stmt (false);
3934 /* Remove any excess precision (which is only present here in the
3935 case of compound assignments). */
3936 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
3938 gcc_assert (modifycode != NOP_EXPR);
3939 rhs = TREE_OPERAND (rhs, 0);
3941 rhs_type = TREE_TYPE (rhs);
3943 /* Fold the RHS if it hasn't already been folded. */
3944 if (modifycode != NOP_EXPR)
3945 rhs = c_fully_fold (rhs, false, NULL);
3947 /* Remove the qualifiers for the rest of the expressions and create
3948 the VAL temp variable to hold the RHS. */
3949 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3950 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3951 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
3952 TYPE_UNQUALIFIED);
3953 val = create_tmp_var_raw (nonatomic_rhs_type);
3954 TREE_ADDRESSABLE (val) = 1;
3955 TREE_NO_WARNING (val) = 1;
3956 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3957 NULL_TREE);
3958 SET_EXPR_LOCATION (rhs, loc);
3959 add_stmt (rhs);
3961 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3962 an atomic_store. */
3963 if (modifycode == NOP_EXPR)
3965 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3966 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3967 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3968 params->quick_push (lhs_addr);
3969 params->quick_push (rhs);
3970 params->quick_push (seq_cst);
3971 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3972 add_stmt (func_call);
3974 /* Finish the compound statement. */
3975 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3977 /* VAL is the value which was stored, return a COMPOUND_STMT of
3978 the statement and that value. */
3979 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3982 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3983 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3984 isn't applicable for such builtins. ??? Do we want to handle enums? */
3985 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3986 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3988 built_in_function fncode;
3989 switch (modifycode)
3991 case PLUS_EXPR:
3992 case POINTER_PLUS_EXPR:
3993 fncode = (return_old_p
3994 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3995 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3996 break;
3997 case MINUS_EXPR:
3998 fncode = (return_old_p
3999 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4000 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4001 break;
4002 case BIT_AND_EXPR:
4003 fncode = (return_old_p
4004 ? BUILT_IN_ATOMIC_FETCH_AND_N
4005 : BUILT_IN_ATOMIC_AND_FETCH_N);
4006 break;
4007 case BIT_IOR_EXPR:
4008 fncode = (return_old_p
4009 ? BUILT_IN_ATOMIC_FETCH_OR_N
4010 : BUILT_IN_ATOMIC_OR_FETCH_N);
4011 break;
4012 case BIT_XOR_EXPR:
4013 fncode = (return_old_p
4014 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4015 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4016 break;
4017 default:
4018 goto cas_loop;
4021 /* We can only use "_1" through "_16" variants of the atomic fetch
4022 built-ins. */
4023 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4024 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4025 goto cas_loop;
4027 /* If this is a pointer type, we need to multiply by the size of
4028 the pointer target type. */
4029 if (POINTER_TYPE_P (lhs_type))
4031 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4032 /* ??? This would introduce -Wdiscarded-qualifiers
4033 warning: __atomic_fetch_* expect volatile void *
4034 type as the first argument. (Assignments between
4035 atomic and non-atomic objects are OK.) */
4036 || TYPE_RESTRICT (lhs_type))
4037 goto cas_loop;
4038 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4039 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4040 convert (ptrdiff_type_node, rhs),
4041 convert (ptrdiff_type_node, sz));
4044 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4045 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4046 fndecl = builtin_decl_explicit (fncode);
4047 params->quick_push (lhs_addr);
4048 params->quick_push (rhs);
4049 params->quick_push (seq_cst);
4050 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4052 newval = create_tmp_var_raw (nonatomic_lhs_type);
4053 TREE_ADDRESSABLE (newval) = 1;
4054 TREE_NO_WARNING (newval) = 1;
4055 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4056 NULL_TREE, NULL_TREE);
4057 SET_EXPR_LOCATION (rhs, loc);
4058 add_stmt (rhs);
4060 /* Finish the compound statement. */
4061 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4063 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4064 the statement and that value. */
4065 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4068 cas_loop:
4069 /* Create the variables and labels required for the op= form. */
4070 old = create_tmp_var_raw (nonatomic_lhs_type);
4071 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4072 TREE_ADDRESSABLE (old) = 1;
4073 TREE_NO_WARNING (old) = 1;
4075 newval = create_tmp_var_raw (nonatomic_lhs_type);
4076 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4077 TREE_ADDRESSABLE (newval) = 1;
4078 TREE_NO_WARNING (newval) = 1;
4080 loop_decl = create_artificial_label (loc);
4081 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4083 done_decl = create_artificial_label (loc);
4084 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4086 /* __atomic_load (addr, &old, SEQ_CST). */
4087 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4088 params->quick_push (lhs_addr);
4089 params->quick_push (old_addr);
4090 params->quick_push (seq_cst);
4091 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4092 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4093 NULL_TREE);
4094 add_stmt (old);
4095 params->truncate (0);
4097 /* Create the expressions for floating-point environment
4098 manipulation, if required. */
4099 bool need_fenv = (flag_trapping_math
4100 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4101 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4102 if (need_fenv)
4103 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4105 if (hold_call)
4106 add_stmt (hold_call);
4108 /* loop: */
4109 add_stmt (loop_label);
4111 /* newval = old + val; */
4112 if (rhs_type != rhs_semantic_type)
4113 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4114 rhs = build_binary_op (loc, modifycode, old, val, true);
4115 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4117 tree eptype = TREE_TYPE (rhs);
4118 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4119 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4121 else
4122 rhs = c_fully_fold (rhs, false, NULL);
4123 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4124 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4125 NULL_TREE, 0);
4126 if (rhs != error_mark_node)
4128 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4129 NULL_TREE);
4130 SET_EXPR_LOCATION (rhs, loc);
4131 add_stmt (rhs);
4134 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4135 goto done; */
4136 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4137 params->quick_push (lhs_addr);
4138 params->quick_push (old_addr);
4139 params->quick_push (newval_addr);
4140 params->quick_push (integer_zero_node);
4141 params->quick_push (seq_cst);
4142 params->quick_push (seq_cst);
4143 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4145 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4146 SET_EXPR_LOCATION (goto_stmt, loc);
4148 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4149 SET_EXPR_LOCATION (stmt, loc);
4150 add_stmt (stmt);
4152 if (clear_call)
4153 add_stmt (clear_call);
4155 /* goto loop; */
4156 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4157 SET_EXPR_LOCATION (goto_stmt, loc);
4158 add_stmt (goto_stmt);
4160 /* done: */
4161 add_stmt (done_label);
4163 if (update_call)
4164 add_stmt (update_call);
4166 /* Finish the compound statement. */
4167 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4169 /* NEWVAL is the value that was successfully stored, return a
4170 COMPOUND_EXPR of the statement and the appropriate value. */
4171 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4172 return_old_p ? old : newval);
4175 /* Construct and perhaps optimize a tree representation
4176 for a unary operation. CODE, a tree_code, specifies the operation
4177 and XARG is the operand.
4178 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4179 promotions (such as from short to int).
4180 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4181 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4182 to pointers in C99.
4184 LOCATION is the location of the operator. */
4186 tree
4187 build_unary_op (location_t location, enum tree_code code, tree xarg,
4188 bool noconvert)
4190 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4191 tree arg = xarg;
4192 tree argtype = NULL_TREE;
4193 enum tree_code typecode;
4194 tree val;
4195 tree ret = error_mark_node;
4196 tree eptype = NULL_TREE;
4197 const char *invalid_op_diag;
4198 bool int_operands;
4200 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4201 if (int_operands)
4202 arg = remove_c_maybe_const_expr (arg);
4204 if (code != ADDR_EXPR)
4205 arg = require_complete_type (location, arg);
4207 typecode = TREE_CODE (TREE_TYPE (arg));
4208 if (typecode == ERROR_MARK)
4209 return error_mark_node;
4210 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4211 typecode = INTEGER_TYPE;
4213 if ((invalid_op_diag
4214 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4216 error_at (location, invalid_op_diag);
4217 return error_mark_node;
4220 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4222 eptype = TREE_TYPE (arg);
4223 arg = TREE_OPERAND (arg, 0);
4226 switch (code)
4228 case CONVERT_EXPR:
4229 /* This is used for unary plus, because a CONVERT_EXPR
4230 is enough to prevent anybody from looking inside for
4231 associativity, but won't generate any code. */
4232 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4233 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4234 || typecode == VECTOR_TYPE))
4236 error_at (location, "wrong type argument to unary plus");
4237 return error_mark_node;
4239 else if (!noconvert)
4240 arg = default_conversion (arg);
4241 arg = non_lvalue_loc (location, arg);
4242 break;
4244 case NEGATE_EXPR:
4245 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4246 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4247 || typecode == VECTOR_TYPE))
4249 error_at (location, "wrong type argument to unary minus");
4250 return error_mark_node;
4252 else if (!noconvert)
4253 arg = default_conversion (arg);
4254 break;
4256 case BIT_NOT_EXPR:
4257 /* ~ works on integer types and non float vectors. */
4258 if (typecode == INTEGER_TYPE
4259 || (typecode == VECTOR_TYPE
4260 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4262 tree e = arg;
4264 /* Warn if the expression has boolean value. */
4265 while (TREE_CODE (e) == COMPOUND_EXPR)
4266 e = TREE_OPERAND (e, 1);
4268 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4269 || truth_value_p (TREE_CODE (e)))
4270 && warning_at (location, OPT_Wbool_operation,
4271 "%<~%> on a boolean expression"))
4273 gcc_rich_location richloc (location);
4274 richloc.add_fixit_insert_before (location, "!");
4275 inform (&richloc, "did you mean to use logical not?");
4277 if (!noconvert)
4278 arg = default_conversion (arg);
4280 else if (typecode == COMPLEX_TYPE)
4282 code = CONJ_EXPR;
4283 pedwarn (location, OPT_Wpedantic,
4284 "ISO C does not support %<~%> for complex conjugation");
4285 if (!noconvert)
4286 arg = default_conversion (arg);
4288 else
4290 error_at (location, "wrong type argument to bit-complement");
4291 return error_mark_node;
4293 break;
4295 case ABS_EXPR:
4296 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4298 error_at (location, "wrong type argument to abs");
4299 return error_mark_node;
4301 else if (!noconvert)
4302 arg = default_conversion (arg);
4303 break;
4305 case CONJ_EXPR:
4306 /* Conjugating a real value is a no-op, but allow it anyway. */
4307 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4308 || typecode == COMPLEX_TYPE))
4310 error_at (location, "wrong type argument to conjugation");
4311 return error_mark_node;
4313 else if (!noconvert)
4314 arg = default_conversion (arg);
4315 break;
4317 case TRUTH_NOT_EXPR:
4318 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4319 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4320 && typecode != COMPLEX_TYPE)
4322 error_at (location,
4323 "wrong type argument to unary exclamation mark");
4324 return error_mark_node;
4326 if (int_operands)
4328 arg = c_objc_common_truthvalue_conversion (location, xarg);
4329 arg = remove_c_maybe_const_expr (arg);
4331 else
4332 arg = c_objc_common_truthvalue_conversion (location, arg);
4333 ret = invert_truthvalue_loc (location, arg);
4334 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4335 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4336 location = EXPR_LOCATION (ret);
4337 goto return_build_unary_op;
4339 case REALPART_EXPR:
4340 case IMAGPART_EXPR:
4341 ret = build_real_imag_expr (location, code, arg);
4342 if (ret == error_mark_node)
4343 return error_mark_node;
4344 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4345 eptype = TREE_TYPE (eptype);
4346 goto return_build_unary_op;
4348 case PREINCREMENT_EXPR:
4349 case POSTINCREMENT_EXPR:
4350 case PREDECREMENT_EXPR:
4351 case POSTDECREMENT_EXPR:
4353 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4355 tree inner = build_unary_op (location, code,
4356 C_MAYBE_CONST_EXPR_EXPR (arg),
4357 noconvert);
4358 if (inner == error_mark_node)
4359 return error_mark_node;
4360 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4361 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4362 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4363 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4364 goto return_build_unary_op;
4367 /* Complain about anything that is not a true lvalue. In
4368 Objective-C, skip this check for property_refs. */
4369 if (!objc_is_property_ref (arg)
4370 && !lvalue_or_else (location,
4371 arg, ((code == PREINCREMENT_EXPR
4372 || code == POSTINCREMENT_EXPR)
4373 ? lv_increment
4374 : lv_decrement)))
4375 return error_mark_node;
4377 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4379 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4380 warning_at (location, OPT_Wc___compat,
4381 "increment of enumeration value is invalid in C++");
4382 else
4383 warning_at (location, OPT_Wc___compat,
4384 "decrement of enumeration value is invalid in C++");
4387 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4389 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4390 warning_at (location, OPT_Wbool_operation,
4391 "increment of a boolean expression");
4392 else
4393 warning_at (location, OPT_Wbool_operation,
4394 "decrement of a boolean expression");
4397 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4398 arg = c_fully_fold (arg, false, NULL, true);
4400 bool atomic_op;
4401 atomic_op = really_atomic_lvalue (arg);
4403 /* Increment or decrement the real part of the value,
4404 and don't change the imaginary part. */
4405 if (typecode == COMPLEX_TYPE)
4407 tree real, imag;
4409 pedwarn (location, OPT_Wpedantic,
4410 "ISO C does not support %<++%> and %<--%> on complex types");
4412 if (!atomic_op)
4414 arg = stabilize_reference (arg);
4415 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4416 true);
4417 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4418 true);
4419 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4420 if (real == error_mark_node || imag == error_mark_node)
4421 return error_mark_node;
4422 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4423 real, imag);
4424 goto return_build_unary_op;
4428 /* Report invalid types. */
4430 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4431 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4432 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4434 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4435 error_at (location, "wrong type argument to increment");
4436 else
4437 error_at (location, "wrong type argument to decrement");
4439 return error_mark_node;
4443 tree inc;
4445 argtype = TREE_TYPE (arg);
4447 /* Compute the increment. */
4449 if (typecode == POINTER_TYPE)
4451 /* If pointer target is an incomplete type,
4452 we just cannot know how to do the arithmetic. */
4453 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4455 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4456 error_at (location,
4457 "increment of pointer to an incomplete type %qT",
4458 TREE_TYPE (argtype));
4459 else
4460 error_at (location,
4461 "decrement of pointer to an incomplete type %qT",
4462 TREE_TYPE (argtype));
4464 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4465 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4467 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4468 pedwarn (location, OPT_Wpointer_arith,
4469 "wrong type argument to increment");
4470 else
4471 pedwarn (location, OPT_Wpointer_arith,
4472 "wrong type argument to decrement");
4475 inc = c_size_in_bytes (TREE_TYPE (argtype));
4476 inc = convert_to_ptrofftype_loc (location, inc);
4478 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4480 /* For signed fract types, we invert ++ to -- or
4481 -- to ++, and change inc from 1 to -1, because
4482 it is not possible to represent 1 in signed fract constants.
4483 For unsigned fract types, the result always overflows and
4484 we get an undefined (original) or the maximum value. */
4485 if (code == PREINCREMENT_EXPR)
4486 code = PREDECREMENT_EXPR;
4487 else if (code == PREDECREMENT_EXPR)
4488 code = PREINCREMENT_EXPR;
4489 else if (code == POSTINCREMENT_EXPR)
4490 code = POSTDECREMENT_EXPR;
4491 else /* code == POSTDECREMENT_EXPR */
4492 code = POSTINCREMENT_EXPR;
4494 inc = integer_minus_one_node;
4495 inc = convert (argtype, inc);
4497 else
4499 inc = VECTOR_TYPE_P (argtype)
4500 ? build_one_cst (argtype)
4501 : integer_one_node;
4502 inc = convert (argtype, inc);
4505 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4506 need to ask Objective-C to build the increment or decrement
4507 expression for it. */
4508 if (objc_is_property_ref (arg))
4509 return objc_build_incr_expr_for_property_ref (location, code,
4510 arg, inc);
4512 /* Report a read-only lvalue. */
4513 if (TYPE_READONLY (argtype))
4515 readonly_error (location, arg,
4516 ((code == PREINCREMENT_EXPR
4517 || code == POSTINCREMENT_EXPR)
4518 ? lv_increment : lv_decrement));
4519 return error_mark_node;
4521 else if (TREE_READONLY (arg))
4522 readonly_warning (arg,
4523 ((code == PREINCREMENT_EXPR
4524 || code == POSTINCREMENT_EXPR)
4525 ? lv_increment : lv_decrement));
4527 /* If the argument is atomic, use the special code sequences for
4528 atomic compound assignment. */
4529 if (atomic_op)
4531 arg = stabilize_reference (arg);
4532 ret = build_atomic_assign (location, arg,
4533 ((code == PREINCREMENT_EXPR
4534 || code == POSTINCREMENT_EXPR)
4535 ? PLUS_EXPR
4536 : MINUS_EXPR),
4537 (FRACT_MODE_P (TYPE_MODE (argtype))
4538 ? inc
4539 : integer_one_node),
4540 (code == POSTINCREMENT_EXPR
4541 || code == POSTDECREMENT_EXPR));
4542 goto return_build_unary_op;
4545 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4546 val = boolean_increment (code, arg);
4547 else
4548 val = build2 (code, TREE_TYPE (arg), arg, inc);
4549 TREE_SIDE_EFFECTS (val) = 1;
4550 if (TREE_CODE (val) != code)
4551 TREE_NO_WARNING (val) = 1;
4552 ret = val;
4553 goto return_build_unary_op;
4556 case ADDR_EXPR:
4557 /* Note that this operation never does default_conversion. */
4559 /* The operand of unary '&' must be an lvalue (which excludes
4560 expressions of type void), or, in C99, the result of a [] or
4561 unary '*' operator. */
4562 if (VOID_TYPE_P (TREE_TYPE (arg))
4563 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4564 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4565 pedwarn (location, 0, "taking address of expression of type %<void%>");
4567 /* Let &* cancel out to simplify resulting code. */
4568 if (INDIRECT_REF_P (arg))
4570 /* Don't let this be an lvalue. */
4571 if (lvalue_p (TREE_OPERAND (arg, 0)))
4572 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4573 ret = TREE_OPERAND (arg, 0);
4574 goto return_build_unary_op;
4577 /* Anything not already handled and not a true memory reference
4578 or a non-lvalue array is an error. */
4579 if (typecode != FUNCTION_TYPE && !noconvert
4580 && !lvalue_or_else (location, arg, lv_addressof))
4581 return error_mark_node;
4583 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4584 folding later. */
4585 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4587 tree inner = build_unary_op (location, code,
4588 C_MAYBE_CONST_EXPR_EXPR (arg),
4589 noconvert);
4590 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4591 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4592 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4593 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4594 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4595 goto return_build_unary_op;
4598 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4599 argtype = TREE_TYPE (arg);
4601 /* If the lvalue is const or volatile, merge that into the type
4602 to which the address will point. This is only needed
4603 for function types. */
4604 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4605 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4606 && TREE_CODE (argtype) == FUNCTION_TYPE)
4608 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4609 int quals = orig_quals;
4611 if (TREE_READONLY (arg))
4612 quals |= TYPE_QUAL_CONST;
4613 if (TREE_THIS_VOLATILE (arg))
4614 quals |= TYPE_QUAL_VOLATILE;
4616 argtype = c_build_qualified_type (argtype, quals);
4619 switch (TREE_CODE (arg))
4621 case COMPONENT_REF:
4622 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4624 error_at (location, "cannot take address of bit-field %qD",
4625 TREE_OPERAND (arg, 1));
4626 return error_mark_node;
4629 /* fall through */
4631 case ARRAY_REF:
4632 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4634 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4635 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4637 error_at (location, "cannot take address of scalar with "
4638 "reverse storage order");
4639 return error_mark_node;
4642 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4643 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4644 warning_at (location, OPT_Wscalar_storage_order,
4645 "address of array with reverse scalar storage "
4646 "order requested");
4649 default:
4650 break;
4653 if (!c_mark_addressable (arg))
4654 return error_mark_node;
4656 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4657 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4659 argtype = build_pointer_type (argtype);
4661 /* ??? Cope with user tricks that amount to offsetof. Delete this
4662 when we have proper support for integer constant expressions. */
4663 val = get_base_address (arg);
4664 if (val && INDIRECT_REF_P (val)
4665 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4667 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4668 goto return_build_unary_op;
4671 val = build1 (ADDR_EXPR, argtype, arg);
4673 ret = val;
4674 goto return_build_unary_op;
4676 default:
4677 gcc_unreachable ();
4680 if (argtype == NULL_TREE)
4681 argtype = TREE_TYPE (arg);
4682 if (TREE_CODE (arg) == INTEGER_CST)
4683 ret = (require_constant_value
4684 ? fold_build1_initializer_loc (location, code, argtype, arg)
4685 : fold_build1_loc (location, code, argtype, arg));
4686 else
4687 ret = build1 (code, argtype, arg);
4688 return_build_unary_op:
4689 gcc_assert (ret != error_mark_node);
4690 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4691 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4692 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4693 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4694 ret = note_integer_operands (ret);
4695 if (eptype)
4696 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4697 protected_set_expr_location (ret, location);
4698 return ret;
4701 /* Return nonzero if REF is an lvalue valid for this language.
4702 Lvalues can be assigned, unless their type has TYPE_READONLY.
4703 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4705 bool
4706 lvalue_p (const_tree ref)
4708 const enum tree_code code = TREE_CODE (ref);
4710 switch (code)
4712 case REALPART_EXPR:
4713 case IMAGPART_EXPR:
4714 case COMPONENT_REF:
4715 return lvalue_p (TREE_OPERAND (ref, 0));
4717 case C_MAYBE_CONST_EXPR:
4718 return lvalue_p (TREE_OPERAND (ref, 1));
4720 case COMPOUND_LITERAL_EXPR:
4721 case STRING_CST:
4722 return true;
4724 case INDIRECT_REF:
4725 case ARRAY_REF:
4726 case VAR_DECL:
4727 case PARM_DECL:
4728 case RESULT_DECL:
4729 case ERROR_MARK:
4730 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4731 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4733 case BIND_EXPR:
4734 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4736 default:
4737 return false;
4741 /* Give a warning for storing in something that is read-only in GCC
4742 terms but not const in ISO C terms. */
4744 static void
4745 readonly_warning (tree arg, enum lvalue_use use)
4747 switch (use)
4749 case lv_assign:
4750 warning (0, "assignment of read-only location %qE", arg);
4751 break;
4752 case lv_increment:
4753 warning (0, "increment of read-only location %qE", arg);
4754 break;
4755 case lv_decrement:
4756 warning (0, "decrement of read-only location %qE", arg);
4757 break;
4758 default:
4759 gcc_unreachable ();
4761 return;
4765 /* Return nonzero if REF is an lvalue valid for this language;
4766 otherwise, print an error message and return zero. USE says
4767 how the lvalue is being used and so selects the error message.
4768 LOCATION is the location at which any error should be reported. */
4770 static int
4771 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4773 int win = lvalue_p (ref);
4775 if (!win)
4776 lvalue_error (loc, use);
4778 return win;
4781 /* Mark EXP saying that we need to be able to take the
4782 address of it; it should not be allocated in a register.
4783 Returns true if successful. ARRAY_REF_P is true if this
4784 is for ARRAY_REF construction - in that case we don't want
4785 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4786 it is fine to use ARRAY_REFs for vector subscripts on vector
4787 register variables. */
4789 bool
4790 c_mark_addressable (tree exp, bool array_ref_p)
4792 tree x = exp;
4794 while (1)
4795 switch (TREE_CODE (x))
4797 case VIEW_CONVERT_EXPR:
4798 if (array_ref_p
4799 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4800 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4801 return true;
4802 /* FALLTHRU */
4803 case COMPONENT_REF:
4804 case ADDR_EXPR:
4805 case ARRAY_REF:
4806 case REALPART_EXPR:
4807 case IMAGPART_EXPR:
4808 x = TREE_OPERAND (x, 0);
4809 break;
4811 case COMPOUND_LITERAL_EXPR:
4812 case CONSTRUCTOR:
4813 TREE_ADDRESSABLE (x) = 1;
4814 return true;
4816 case VAR_DECL:
4817 case CONST_DECL:
4818 case PARM_DECL:
4819 case RESULT_DECL:
4820 if (C_DECL_REGISTER (x)
4821 && DECL_NONLOCAL (x))
4823 if (TREE_PUBLIC (x) || is_global_var (x))
4825 error
4826 ("global register variable %qD used in nested function", x);
4827 return false;
4829 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4831 else if (C_DECL_REGISTER (x))
4833 if (TREE_PUBLIC (x) || is_global_var (x))
4834 error ("address of global register variable %qD requested", x);
4835 else
4836 error ("address of register variable %qD requested", x);
4837 return false;
4840 /* FALLTHRU */
4841 case FUNCTION_DECL:
4842 TREE_ADDRESSABLE (x) = 1;
4843 /* FALLTHRU */
4844 default:
4845 return true;
4849 /* Convert EXPR to TYPE, warning about conversion problems with
4850 constants. SEMANTIC_TYPE is the type this conversion would use
4851 without excess precision. If SEMANTIC_TYPE is NULL, this function
4852 is equivalent to convert_and_check. This function is a wrapper that
4853 handles conversions that may be different than
4854 the usual ones because of excess precision. */
4856 static tree
4857 ep_convert_and_check (location_t loc, tree type, tree expr,
4858 tree semantic_type)
4860 if (TREE_TYPE (expr) == type)
4861 return expr;
4863 /* For C11, integer conversions may have results with excess
4864 precision. */
4865 if (flag_isoc11 || !semantic_type)
4866 return convert_and_check (loc, type, expr);
4868 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4869 && TREE_TYPE (expr) != semantic_type)
4871 /* For integers, we need to check the real conversion, not
4872 the conversion to the excess precision type. */
4873 expr = convert_and_check (loc, semantic_type, expr);
4875 /* Result type is the excess precision type, which should be
4876 large enough, so do not check. */
4877 return convert (type, expr);
4880 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4881 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4882 if folded to an integer constant then the unselected half may
4883 contain arbitrary operations not normally permitted in constant
4884 expressions. Set the location of the expression to LOC. */
4886 tree
4887 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4888 tree op1, tree op1_original_type, location_t op1_loc,
4889 tree op2, tree op2_original_type, location_t op2_loc)
4891 tree type1;
4892 tree type2;
4893 enum tree_code code1;
4894 enum tree_code code2;
4895 tree result_type = NULL;
4896 tree semantic_result_type = NULL;
4897 tree orig_op1 = op1, orig_op2 = op2;
4898 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4899 bool ifexp_int_operands;
4900 tree ret;
4902 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4903 if (op1_int_operands)
4904 op1 = remove_c_maybe_const_expr (op1);
4905 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4906 if (op2_int_operands)
4907 op2 = remove_c_maybe_const_expr (op2);
4908 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4909 if (ifexp_int_operands)
4910 ifexp = remove_c_maybe_const_expr (ifexp);
4912 /* Promote both alternatives. */
4914 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4915 op1 = default_conversion (op1);
4916 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4917 op2 = default_conversion (op2);
4919 if (TREE_CODE (ifexp) == ERROR_MARK
4920 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4921 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4922 return error_mark_node;
4924 type1 = TREE_TYPE (op1);
4925 code1 = TREE_CODE (type1);
4926 type2 = TREE_TYPE (op2);
4927 code2 = TREE_CODE (type2);
4929 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4930 return error_mark_node;
4932 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4933 return error_mark_node;
4935 /* C90 does not permit non-lvalue arrays in conditional expressions.
4936 In C99 they will be pointers by now. */
4937 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4939 error_at (colon_loc, "non-lvalue array in conditional expression");
4940 return error_mark_node;
4943 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4944 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4945 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4946 || code1 == COMPLEX_TYPE)
4947 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4948 || code2 == COMPLEX_TYPE))
4950 semantic_result_type = c_common_type (type1, type2);
4951 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4953 op1 = TREE_OPERAND (op1, 0);
4954 type1 = TREE_TYPE (op1);
4955 gcc_assert (TREE_CODE (type1) == code1);
4957 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4959 op2 = TREE_OPERAND (op2, 0);
4960 type2 = TREE_TYPE (op2);
4961 gcc_assert (TREE_CODE (type2) == code2);
4965 if (warn_cxx_compat)
4967 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4968 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4970 if (TREE_CODE (t1) == ENUMERAL_TYPE
4971 && TREE_CODE (t2) == ENUMERAL_TYPE
4972 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4973 warning_at (colon_loc, OPT_Wc___compat,
4974 ("different enum types in conditional is "
4975 "invalid in C++: %qT vs %qT"),
4976 t1, t2);
4979 /* Quickly detect the usual case where op1 and op2 have the same type
4980 after promotion. */
4981 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4983 if (type1 == type2)
4984 result_type = type1;
4985 else
4986 result_type = TYPE_MAIN_VARIANT (type1);
4988 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4989 || code1 == COMPLEX_TYPE)
4990 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4991 || code2 == COMPLEX_TYPE))
4993 /* In C11, a conditional expression between a floating-point
4994 type and an integer type should convert the integer type to
4995 the evaluation format of the floating-point type, with
4996 possible excess precision. */
4997 tree eptype1 = type1;
4998 tree eptype2 = type2;
4999 if (flag_isoc11)
5001 tree eptype;
5002 if (ANY_INTEGRAL_TYPE_P (type1)
5003 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5005 eptype2 = eptype;
5006 if (!semantic_result_type)
5007 semantic_result_type = c_common_type (type1, type2);
5009 else if (ANY_INTEGRAL_TYPE_P (type2)
5010 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5012 eptype1 = eptype;
5013 if (!semantic_result_type)
5014 semantic_result_type = c_common_type (type1, type2);
5017 result_type = c_common_type (eptype1, eptype2);
5018 if (result_type == error_mark_node)
5019 return error_mark_node;
5020 do_warn_double_promotion (result_type, type1, type2,
5021 "implicit conversion from %qT to %qT to "
5022 "match other result of conditional",
5023 colon_loc);
5025 /* If -Wsign-compare, warn here if type1 and type2 have
5026 different signedness. We'll promote the signed to unsigned
5027 and later code won't know it used to be different.
5028 Do this check on the original types, so that explicit casts
5029 will be considered, but default promotions won't. */
5030 if (c_inhibit_evaluation_warnings == 0)
5032 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5033 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5035 if (unsigned_op1 ^ unsigned_op2)
5037 bool ovf;
5039 /* Do not warn if the result type is signed, since the
5040 signed type will only be chosen if it can represent
5041 all the values of the unsigned type. */
5042 if (!TYPE_UNSIGNED (result_type))
5043 /* OK */;
5044 else
5046 bool op1_maybe_const = true;
5047 bool op2_maybe_const = true;
5049 /* Do not warn if the signed quantity is an
5050 unsuffixed integer literal (or some static
5051 constant expression involving such literals) and
5052 it is non-negative. This warning requires the
5053 operands to be folded for best results, so do
5054 that folding in this case even without
5055 warn_sign_compare to avoid warning options
5056 possibly affecting code generation. */
5057 c_inhibit_evaluation_warnings
5058 += (ifexp == truthvalue_false_node);
5059 op1 = c_fully_fold (op1, require_constant_value,
5060 &op1_maybe_const);
5061 c_inhibit_evaluation_warnings
5062 -= (ifexp == truthvalue_false_node);
5064 c_inhibit_evaluation_warnings
5065 += (ifexp == truthvalue_true_node);
5066 op2 = c_fully_fold (op2, require_constant_value,
5067 &op2_maybe_const);
5068 c_inhibit_evaluation_warnings
5069 -= (ifexp == truthvalue_true_node);
5071 if (warn_sign_compare)
5073 if ((unsigned_op2
5074 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5075 || (unsigned_op1
5076 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5077 /* OK */;
5078 else if (unsigned_op2)
5079 warning_at (op1_loc, OPT_Wsign_compare,
5080 "operand of ?: changes signedness from "
5081 "%qT to %qT due to unsignedness of other "
5082 "operand", TREE_TYPE (orig_op1),
5083 TREE_TYPE (orig_op2));
5084 else
5085 warning_at (op2_loc, OPT_Wsign_compare,
5086 "operand of ?: changes signedness from "
5087 "%qT to %qT due to unsignedness of other "
5088 "operand", TREE_TYPE (orig_op2),
5089 TREE_TYPE (orig_op1));
5091 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5092 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5093 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5094 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5099 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5101 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5102 pedwarn (colon_loc, OPT_Wpedantic,
5103 "ISO C forbids conditional expr with only one void side");
5104 result_type = void_type_node;
5106 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5108 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5109 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5110 addr_space_t as_common;
5112 if (comp_target_types (colon_loc, type1, type2))
5113 result_type = common_pointer_type (type1, type2);
5114 else if (null_pointer_constant_p (orig_op1))
5115 result_type = type2;
5116 else if (null_pointer_constant_p (orig_op2))
5117 result_type = type1;
5118 else if (!addr_space_superset (as1, as2, &as_common))
5120 error_at (colon_loc, "pointers to disjoint address spaces "
5121 "used in conditional expression");
5122 return error_mark_node;
5124 else if (VOID_TYPE_P (TREE_TYPE (type1))
5125 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5127 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5128 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5129 & ~TYPE_QUALS (TREE_TYPE (type1))))
5130 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5131 "pointer to array loses qualifier "
5132 "in conditional expression");
5134 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5135 pedwarn (colon_loc, OPT_Wpedantic,
5136 "ISO C forbids conditional expr between "
5137 "%<void *%> and function pointer");
5138 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5139 TREE_TYPE (type2)));
5141 else if (VOID_TYPE_P (TREE_TYPE (type2))
5142 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5144 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5145 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5146 & ~TYPE_QUALS (TREE_TYPE (type2))))
5147 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5148 "pointer to array loses qualifier "
5149 "in conditional expression");
5151 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5152 pedwarn (colon_loc, OPT_Wpedantic,
5153 "ISO C forbids conditional expr between "
5154 "%<void *%> and function pointer");
5155 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5156 TREE_TYPE (type1)));
5158 /* Objective-C pointer comparisons are a bit more lenient. */
5159 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5160 result_type = objc_common_type (type1, type2);
5161 else
5163 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5165 pedwarn (colon_loc, 0,
5166 "pointer type mismatch in conditional expression");
5167 result_type = build_pointer_type
5168 (build_qualified_type (void_type_node, qual));
5171 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5173 if (!null_pointer_constant_p (orig_op2))
5174 pedwarn (colon_loc, 0,
5175 "pointer/integer type mismatch in conditional expression");
5176 else
5178 op2 = null_pointer_node;
5180 result_type = type1;
5182 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5184 if (!null_pointer_constant_p (orig_op1))
5185 pedwarn (colon_loc, 0,
5186 "pointer/integer type mismatch in conditional expression");
5187 else
5189 op1 = null_pointer_node;
5191 result_type = type2;
5194 if (!result_type)
5196 if (flag_cond_mismatch)
5197 result_type = void_type_node;
5198 else
5200 error_at (colon_loc, "type mismatch in conditional expression");
5201 return error_mark_node;
5205 /* Merge const and volatile flags of the incoming types. */
5206 result_type
5207 = build_type_variant (result_type,
5208 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5209 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5211 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5212 semantic_result_type);
5213 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5214 semantic_result_type);
5216 if (ifexp_bcp && ifexp == truthvalue_true_node)
5218 op2_int_operands = true;
5219 op1 = c_fully_fold (op1, require_constant_value, NULL);
5221 if (ifexp_bcp && ifexp == truthvalue_false_node)
5223 op1_int_operands = true;
5224 op2 = c_fully_fold (op2, require_constant_value, NULL);
5226 int_const = int_operands = (ifexp_int_operands
5227 && op1_int_operands
5228 && op2_int_operands);
5229 if (int_operands)
5231 int_const = ((ifexp == truthvalue_true_node
5232 && TREE_CODE (orig_op1) == INTEGER_CST
5233 && !TREE_OVERFLOW (orig_op1))
5234 || (ifexp == truthvalue_false_node
5235 && TREE_CODE (orig_op2) == INTEGER_CST
5236 && !TREE_OVERFLOW (orig_op2)));
5239 /* Need to convert condition operand into a vector mask. */
5240 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5242 tree vectype = TREE_TYPE (ifexp);
5243 tree elem_type = TREE_TYPE (vectype);
5244 tree zero = build_int_cst (elem_type, 0);
5245 tree zero_vec = build_vector_from_val (vectype, zero);
5246 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5247 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5250 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5251 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5252 else
5254 if (int_operands)
5256 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5257 nested inside of the expression. */
5258 op1 = c_fully_fold (op1, false, NULL);
5259 op2 = c_fully_fold (op2, false, NULL);
5261 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5262 if (int_operands)
5263 ret = note_integer_operands (ret);
5265 if (semantic_result_type)
5266 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5268 protected_set_expr_location (ret, colon_loc);
5270 /* If the OP1 and OP2 are the same and don't have side-effects,
5271 warn here, because the COND_EXPR will be turned into OP1. */
5272 if (warn_duplicated_branches
5273 && TREE_CODE (ret) == COND_EXPR
5274 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5275 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5276 "this condition has identical branches");
5278 return ret;
5281 /* Return a compound expression that performs two expressions and
5282 returns the value of the second of them.
5284 LOC is the location of the COMPOUND_EXPR. */
5286 tree
5287 build_compound_expr (location_t loc, tree expr1, tree expr2)
5289 bool expr1_int_operands, expr2_int_operands;
5290 tree eptype = NULL_TREE;
5291 tree ret;
5293 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5294 if (expr1_int_operands)
5295 expr1 = remove_c_maybe_const_expr (expr1);
5296 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5297 if (expr2_int_operands)
5298 expr2 = remove_c_maybe_const_expr (expr2);
5300 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5301 expr1 = TREE_OPERAND (expr1, 0);
5302 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5304 eptype = TREE_TYPE (expr2);
5305 expr2 = TREE_OPERAND (expr2, 0);
5308 if (!TREE_SIDE_EFFECTS (expr1))
5310 /* The left-hand operand of a comma expression is like an expression
5311 statement: with -Wunused, we should warn if it doesn't have
5312 any side-effects, unless it was explicitly cast to (void). */
5313 if (warn_unused_value)
5315 if (VOID_TYPE_P (TREE_TYPE (expr1))
5316 && CONVERT_EXPR_P (expr1))
5317 ; /* (void) a, b */
5318 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5319 && TREE_CODE (expr1) == COMPOUND_EXPR
5320 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5321 ; /* (void) a, (void) b, c */
5322 else
5323 warning_at (loc, OPT_Wunused_value,
5324 "left-hand operand of comma expression has no effect");
5327 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5328 && warn_unused_value)
5330 tree r = expr1;
5331 location_t cloc = loc;
5332 while (TREE_CODE (r) == COMPOUND_EXPR)
5334 if (EXPR_HAS_LOCATION (r))
5335 cloc = EXPR_LOCATION (r);
5336 r = TREE_OPERAND (r, 1);
5338 if (!TREE_SIDE_EFFECTS (r)
5339 && !VOID_TYPE_P (TREE_TYPE (r))
5340 && !CONVERT_EXPR_P (r))
5341 warning_at (cloc, OPT_Wunused_value,
5342 "right-hand operand of comma expression has no effect");
5345 /* With -Wunused, we should also warn if the left-hand operand does have
5346 side-effects, but computes a value which is not used. For example, in
5347 `foo() + bar(), baz()' the result of the `+' operator is not used,
5348 so we should issue a warning. */
5349 else if (warn_unused_value)
5350 warn_if_unused_value (expr1, loc);
5352 if (expr2 == error_mark_node)
5353 return error_mark_node;
5355 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5357 if (flag_isoc99
5358 && expr1_int_operands
5359 && expr2_int_operands)
5360 ret = note_integer_operands (ret);
5362 if (eptype)
5363 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5365 protected_set_expr_location (ret, loc);
5366 return ret;
5369 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5370 which we are casting. OTYPE is the type of the expression being
5371 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5372 of the cast. -Wcast-qual appeared on the command line. Named
5373 address space qualifiers are not handled here, because they result
5374 in different warnings. */
5376 static void
5377 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5379 tree in_type = type;
5380 tree in_otype = otype;
5381 int added = 0;
5382 int discarded = 0;
5383 bool is_const;
5385 /* Check that the qualifiers on IN_TYPE are a superset of the
5386 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5387 nodes is uninteresting and we stop as soon as we hit a
5388 non-POINTER_TYPE node on either type. */
5391 in_otype = TREE_TYPE (in_otype);
5392 in_type = TREE_TYPE (in_type);
5394 /* GNU C allows cv-qualified function types. 'const' means the
5395 function is very pure, 'volatile' means it can't return. We
5396 need to warn when such qualifiers are added, not when they're
5397 taken away. */
5398 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5399 && TREE_CODE (in_type) == FUNCTION_TYPE)
5400 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5401 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5402 else
5403 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5404 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5406 while (TREE_CODE (in_type) == POINTER_TYPE
5407 && TREE_CODE (in_otype) == POINTER_TYPE);
5409 if (added)
5410 warning_at (loc, OPT_Wcast_qual,
5411 "cast adds %q#v qualifier to function type", added);
5413 if (discarded)
5414 /* There are qualifiers present in IN_OTYPE that are not present
5415 in IN_TYPE. */
5416 warning_at (loc, OPT_Wcast_qual,
5417 "cast discards %qv qualifier from pointer target type",
5418 discarded);
5420 if (added || discarded)
5421 return;
5423 /* A cast from **T to const **T is unsafe, because it can cause a
5424 const value to be changed with no additional warning. We only
5425 issue this warning if T is the same on both sides, and we only
5426 issue the warning if there are the same number of pointers on
5427 both sides, as otherwise the cast is clearly unsafe anyhow. A
5428 cast is unsafe when a qualifier is added at one level and const
5429 is not present at all outer levels.
5431 To issue this warning, we check at each level whether the cast
5432 adds new qualifiers not already seen. We don't need to special
5433 case function types, as they won't have the same
5434 TYPE_MAIN_VARIANT. */
5436 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5437 return;
5438 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5439 return;
5441 in_type = type;
5442 in_otype = otype;
5443 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5446 in_type = TREE_TYPE (in_type);
5447 in_otype = TREE_TYPE (in_otype);
5448 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5449 && !is_const)
5451 warning_at (loc, OPT_Wcast_qual,
5452 "to be safe all intermediate pointers in cast from "
5453 "%qT to %qT must be %<const%> qualified",
5454 otype, type);
5455 break;
5457 if (is_const)
5458 is_const = TYPE_READONLY (in_type);
5460 while (TREE_CODE (in_type) == POINTER_TYPE);
5463 /* Build an expression representing a cast to type TYPE of expression EXPR.
5464 LOC is the location of the cast-- typically the open paren of the cast. */
5466 tree
5467 build_c_cast (location_t loc, tree type, tree expr)
5469 tree value;
5471 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5472 expr = TREE_OPERAND (expr, 0);
5474 value = expr;
5476 if (type == error_mark_node || expr == error_mark_node)
5477 return error_mark_node;
5479 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5480 only in <protocol> qualifications. But when constructing cast expressions,
5481 the protocols do matter and must be kept around. */
5482 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5483 return build1 (NOP_EXPR, type, expr);
5485 type = TYPE_MAIN_VARIANT (type);
5487 if (TREE_CODE (type) == ARRAY_TYPE)
5489 error_at (loc, "cast specifies array type");
5490 return error_mark_node;
5493 if (TREE_CODE (type) == FUNCTION_TYPE)
5495 error_at (loc, "cast specifies function type");
5496 return error_mark_node;
5499 if (!VOID_TYPE_P (type))
5501 value = require_complete_type (loc, value);
5502 if (value == error_mark_node)
5503 return error_mark_node;
5506 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5508 if (RECORD_OR_UNION_TYPE_P (type))
5509 pedwarn (loc, OPT_Wpedantic,
5510 "ISO C forbids casting nonscalar to the same type");
5512 /* Convert to remove any qualifiers from VALUE's type. */
5513 value = convert (type, value);
5515 else if (TREE_CODE (type) == UNION_TYPE)
5517 tree field;
5519 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5520 if (TREE_TYPE (field) != error_mark_node
5521 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5522 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5523 break;
5525 if (field)
5527 tree t;
5528 bool maybe_const = true;
5530 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5531 t = c_fully_fold (value, false, &maybe_const);
5532 t = build_constructor_single (type, field, t);
5533 if (!maybe_const)
5534 t = c_wrap_maybe_const (t, true);
5535 t = digest_init (loc, type, t,
5536 NULL_TREE, false, true, 0);
5537 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5538 return t;
5540 error_at (loc, "cast to union type from type not present in union");
5541 return error_mark_node;
5543 else
5545 tree otype, ovalue;
5547 if (type == void_type_node)
5549 tree t = build1 (CONVERT_EXPR, type, value);
5550 SET_EXPR_LOCATION (t, loc);
5551 return t;
5554 otype = TREE_TYPE (value);
5556 /* Optionally warn about potentially worrisome casts. */
5557 if (warn_cast_qual
5558 && TREE_CODE (type) == POINTER_TYPE
5559 && TREE_CODE (otype) == POINTER_TYPE)
5560 handle_warn_cast_qual (loc, type, otype);
5562 /* Warn about conversions between pointers to disjoint
5563 address spaces. */
5564 if (TREE_CODE (type) == POINTER_TYPE
5565 && TREE_CODE (otype) == POINTER_TYPE
5566 && !null_pointer_constant_p (value))
5568 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5569 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5570 addr_space_t as_common;
5572 if (!addr_space_superset (as_to, as_from, &as_common))
5574 if (ADDR_SPACE_GENERIC_P (as_from))
5575 warning_at (loc, 0, "cast to %s address space pointer "
5576 "from disjoint generic address space pointer",
5577 c_addr_space_name (as_to));
5579 else if (ADDR_SPACE_GENERIC_P (as_to))
5580 warning_at (loc, 0, "cast to generic address space pointer "
5581 "from disjoint %s address space pointer",
5582 c_addr_space_name (as_from));
5584 else
5585 warning_at (loc, 0, "cast to %s address space pointer "
5586 "from disjoint %s address space pointer",
5587 c_addr_space_name (as_to),
5588 c_addr_space_name (as_from));
5592 /* Warn about possible alignment problems. */
5593 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5594 && TREE_CODE (type) == POINTER_TYPE
5595 && TREE_CODE (otype) == POINTER_TYPE
5596 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5597 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5598 /* Don't warn about opaque types, where the actual alignment
5599 restriction is unknown. */
5600 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5601 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5602 && min_align_of_type (TREE_TYPE (type))
5603 > min_align_of_type (TREE_TYPE (otype)))
5604 warning_at (loc, OPT_Wcast_align,
5605 "cast increases required alignment of target type");
5607 if (TREE_CODE (type) == INTEGER_TYPE
5608 && TREE_CODE (otype) == POINTER_TYPE
5609 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5610 /* Unlike conversion of integers to pointers, where the
5611 warning is disabled for converting constants because
5612 of cases such as SIG_*, warn about converting constant
5613 pointers to integers. In some cases it may cause unwanted
5614 sign extension, and a warning is appropriate. */
5615 warning_at (loc, OPT_Wpointer_to_int_cast,
5616 "cast from pointer to integer of different size");
5618 if (TREE_CODE (value) == CALL_EXPR
5619 && TREE_CODE (type) != TREE_CODE (otype))
5620 warning_at (loc, OPT_Wbad_function_cast,
5621 "cast from function call of type %qT "
5622 "to non-matching type %qT", otype, type);
5624 if (TREE_CODE (type) == POINTER_TYPE
5625 && TREE_CODE (otype) == INTEGER_TYPE
5626 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5627 /* Don't warn about converting any constant. */
5628 && !TREE_CONSTANT (value))
5629 warning_at (loc,
5630 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5631 "of different size");
5633 if (warn_strict_aliasing <= 2)
5634 strict_aliasing_warning (otype, type, expr);
5636 /* If pedantic, warn for conversions between function and object
5637 pointer types, except for converting a null pointer constant
5638 to function pointer type. */
5639 if (pedantic
5640 && TREE_CODE (type) == POINTER_TYPE
5641 && TREE_CODE (otype) == POINTER_TYPE
5642 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5643 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5644 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5645 "conversion of function pointer to object pointer type");
5647 if (pedantic
5648 && TREE_CODE (type) == POINTER_TYPE
5649 && TREE_CODE (otype) == POINTER_TYPE
5650 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5651 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5652 && !null_pointer_constant_p (value))
5653 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5654 "conversion of object pointer to function pointer type");
5656 ovalue = value;
5657 value = convert (type, value);
5659 /* Ignore any integer overflow caused by the cast. */
5660 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5662 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5664 if (!TREE_OVERFLOW (value))
5666 /* Avoid clobbering a shared constant. */
5667 value = copy_node (value);
5668 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5671 else if (TREE_OVERFLOW (value))
5672 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5673 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
5677 /* Don't let a cast be an lvalue. */
5678 if (lvalue_p (value))
5679 value = non_lvalue_loc (loc, value);
5681 /* Don't allow the results of casting to floating-point or complex
5682 types be confused with actual constants, or casts involving
5683 integer and pointer types other than direct integer-to-integer
5684 and integer-to-pointer be confused with integer constant
5685 expressions and null pointer constants. */
5686 if (TREE_CODE (value) == REAL_CST
5687 || TREE_CODE (value) == COMPLEX_CST
5688 || (TREE_CODE (value) == INTEGER_CST
5689 && !((TREE_CODE (expr) == INTEGER_CST
5690 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5691 || TREE_CODE (expr) == REAL_CST
5692 || TREE_CODE (expr) == COMPLEX_CST)))
5693 value = build1 (NOP_EXPR, type, value);
5695 protected_set_expr_location (value, loc);
5696 return value;
5699 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5700 location of the open paren of the cast, or the position of the cast
5701 expr. */
5702 tree
5703 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5705 tree type;
5706 tree type_expr = NULL_TREE;
5707 bool type_expr_const = true;
5708 tree ret;
5709 int saved_wsp = warn_strict_prototypes;
5711 /* This avoids warnings about unprototyped casts on
5712 integers. E.g. "#define SIG_DFL (void(*)())0". */
5713 if (TREE_CODE (expr) == INTEGER_CST)
5714 warn_strict_prototypes = 0;
5715 type = groktypename (type_name, &type_expr, &type_expr_const);
5716 warn_strict_prototypes = saved_wsp;
5718 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5719 && reject_gcc_builtin (expr))
5720 return error_mark_node;
5722 ret = build_c_cast (loc, type, expr);
5723 if (type_expr)
5725 bool inner_expr_const = true;
5726 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5727 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5728 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5729 && inner_expr_const);
5730 SET_EXPR_LOCATION (ret, loc);
5733 if (!EXPR_HAS_LOCATION (ret))
5734 protected_set_expr_location (ret, loc);
5736 /* C++ does not permits types to be defined in a cast, but it
5737 allows references to incomplete types. */
5738 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5739 warning_at (loc, OPT_Wc___compat,
5740 "defining a type in a cast is invalid in C++");
5742 return ret;
5745 /* Build an assignment expression of lvalue LHS from value RHS.
5746 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5747 may differ from TREE_TYPE (LHS) for an enum bitfield.
5748 MODIFYCODE is the code for a binary operator that we use
5749 to combine the old value of LHS with RHS to get the new value.
5750 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5751 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5752 which may differ from TREE_TYPE (RHS) for an enum value.
5754 LOCATION is the location of the MODIFYCODE operator.
5755 RHS_LOC is the location of the RHS. */
5757 tree
5758 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5759 enum tree_code modifycode,
5760 location_t rhs_loc, tree rhs, tree rhs_origtype)
5762 tree result;
5763 tree newrhs;
5764 tree rhseval = NULL_TREE;
5765 tree lhstype = TREE_TYPE (lhs);
5766 tree olhstype = lhstype;
5767 bool npc;
5768 bool is_atomic_op;
5770 /* Types that aren't fully specified cannot be used in assignments. */
5771 lhs = require_complete_type (location, lhs);
5773 /* Avoid duplicate error messages from operands that had errors. */
5774 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5775 return error_mark_node;
5777 /* Ensure an error for assigning a non-lvalue array to an array in
5778 C90. */
5779 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5781 error_at (location, "assignment to expression with array type");
5782 return error_mark_node;
5785 /* For ObjC properties, defer this check. */
5786 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5787 return error_mark_node;
5789 is_atomic_op = really_atomic_lvalue (lhs);
5791 newrhs = rhs;
5793 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5795 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5796 lhs_origtype, modifycode, rhs_loc, rhs,
5797 rhs_origtype);
5798 if (inner == error_mark_node)
5799 return error_mark_node;
5800 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5801 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5802 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5803 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5804 protected_set_expr_location (result, location);
5805 return result;
5808 /* If a binary op has been requested, combine the old LHS value with the RHS
5809 producing the value we should actually store into the LHS. */
5811 if (modifycode != NOP_EXPR)
5813 lhs = c_fully_fold (lhs, false, NULL, true);
5814 lhs = stabilize_reference (lhs);
5816 /* Construct the RHS for any non-atomic compound assignemnt. */
5817 if (!is_atomic_op)
5819 /* If in LHS op= RHS the RHS has side-effects, ensure they
5820 are preevaluated before the rest of the assignment expression's
5821 side-effects, because RHS could contain e.g. function calls
5822 that modify LHS. */
5823 if (TREE_SIDE_EFFECTS (rhs))
5825 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5826 newrhs = save_expr (TREE_OPERAND (rhs, 0));
5827 else
5828 newrhs = save_expr (rhs);
5829 rhseval = newrhs;
5830 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5831 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
5832 newrhs);
5834 newrhs = build_binary_op (location,
5835 modifycode, lhs, newrhs, true);
5837 /* The original type of the right hand side is no longer
5838 meaningful. */
5839 rhs_origtype = NULL_TREE;
5843 if (c_dialect_objc ())
5845 /* Check if we are modifying an Objective-C property reference;
5846 if so, we need to generate setter calls. */
5847 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5848 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
5849 else
5850 result = objc_maybe_build_modify_expr (lhs, newrhs);
5851 if (result)
5852 goto return_result;
5854 /* Else, do the check that we postponed for Objective-C. */
5855 if (!lvalue_or_else (location, lhs, lv_assign))
5856 return error_mark_node;
5859 /* Give an error for storing in something that is 'const'. */
5861 if (TYPE_READONLY (lhstype)
5862 || (RECORD_OR_UNION_TYPE_P (lhstype)
5863 && C_TYPE_FIELDS_READONLY (lhstype)))
5865 readonly_error (location, lhs, lv_assign);
5866 return error_mark_node;
5868 else if (TREE_READONLY (lhs))
5869 readonly_warning (lhs, lv_assign);
5871 /* If storing into a structure or union member,
5872 it has probably been given type `int'.
5873 Compute the type that would go with
5874 the actual amount of storage the member occupies. */
5876 if (TREE_CODE (lhs) == COMPONENT_REF
5877 && (TREE_CODE (lhstype) == INTEGER_TYPE
5878 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5879 || TREE_CODE (lhstype) == REAL_TYPE
5880 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5881 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5883 /* If storing in a field that is in actuality a short or narrower than one,
5884 we must store in the field in its actual type. */
5886 if (lhstype != TREE_TYPE (lhs))
5888 lhs = copy_node (lhs);
5889 TREE_TYPE (lhs) = lhstype;
5892 /* Issue -Wc++-compat warnings about an assignment to an enum type
5893 when LHS does not have its original type. This happens for,
5894 e.g., an enum bitfield in a struct. */
5895 if (warn_cxx_compat
5896 && lhs_origtype != NULL_TREE
5897 && lhs_origtype != lhstype
5898 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5900 tree checktype = (rhs_origtype != NULL_TREE
5901 ? rhs_origtype
5902 : TREE_TYPE (rhs));
5903 if (checktype != error_mark_node
5904 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5905 || (is_atomic_op && modifycode != NOP_EXPR)))
5906 warning_at (location, OPT_Wc___compat,
5907 "enum conversion in assignment is invalid in C++");
5910 /* If the lhs is atomic, remove that qualifier. */
5911 if (is_atomic_op)
5913 lhstype = build_qualified_type (lhstype,
5914 (TYPE_QUALS (lhstype)
5915 & ~TYPE_QUAL_ATOMIC));
5916 olhstype = build_qualified_type (olhstype,
5917 (TYPE_QUALS (lhstype)
5918 & ~TYPE_QUAL_ATOMIC));
5921 /* Convert new value to destination type. Fold it first, then
5922 restore any excess precision information, for the sake of
5923 conversion warnings. */
5925 if (!(is_atomic_op && modifycode != NOP_EXPR))
5927 tree rhs_semantic_type = NULL_TREE;
5928 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5930 rhs_semantic_type = TREE_TYPE (newrhs);
5931 newrhs = TREE_OPERAND (newrhs, 0);
5933 npc = null_pointer_constant_p (newrhs);
5934 newrhs = c_fully_fold (newrhs, false, NULL);
5935 if (rhs_semantic_type)
5936 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5937 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5938 rhs_origtype, ic_assign, npc,
5939 NULL_TREE, NULL_TREE, 0);
5940 if (TREE_CODE (newrhs) == ERROR_MARK)
5941 return error_mark_node;
5944 /* Emit ObjC write barrier, if necessary. */
5945 if (c_dialect_objc () && flag_objc_gc)
5947 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5948 if (result)
5950 protected_set_expr_location (result, location);
5951 goto return_result;
5955 /* Scan operands. */
5957 if (is_atomic_op)
5958 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5959 else
5961 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5962 TREE_SIDE_EFFECTS (result) = 1;
5963 protected_set_expr_location (result, location);
5966 /* If we got the LHS in a different type for storing in,
5967 convert the result back to the nominal type of LHS
5968 so that the value we return always has the same type
5969 as the LHS argument. */
5971 if (olhstype == TREE_TYPE (result))
5972 goto return_result;
5974 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5975 rhs_origtype, ic_assign, false, NULL_TREE,
5976 NULL_TREE, 0);
5977 protected_set_expr_location (result, location);
5979 return_result:
5980 if (rhseval)
5981 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5982 return result;
5985 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5986 This is used to implement -fplan9-extensions. */
5988 static bool
5989 find_anonymous_field_with_type (tree struct_type, tree type)
5991 tree field;
5992 bool found;
5994 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5995 found = false;
5996 for (field = TYPE_FIELDS (struct_type);
5997 field != NULL_TREE;
5998 field = TREE_CHAIN (field))
6000 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6001 ? c_build_qualified_type (TREE_TYPE (field),
6002 TYPE_QUAL_ATOMIC)
6003 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6004 if (DECL_NAME (field) == NULL
6005 && comptypes (type, fieldtype))
6007 if (found)
6008 return false;
6009 found = true;
6011 else if (DECL_NAME (field) == NULL
6012 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6013 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6015 if (found)
6016 return false;
6017 found = true;
6020 return found;
6023 /* RHS is an expression whose type is pointer to struct. If there is
6024 an anonymous field in RHS with type TYPE, then return a pointer to
6025 that field in RHS. This is used with -fplan9-extensions. This
6026 returns NULL if no conversion could be found. */
6028 static tree
6029 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6031 tree rhs_struct_type, lhs_main_type;
6032 tree field, found_field;
6033 bool found_sub_field;
6034 tree ret;
6036 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6037 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6038 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6040 gcc_assert (POINTER_TYPE_P (type));
6041 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6042 ? c_build_qualified_type (TREE_TYPE (type),
6043 TYPE_QUAL_ATOMIC)
6044 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6046 found_field = NULL_TREE;
6047 found_sub_field = false;
6048 for (field = TYPE_FIELDS (rhs_struct_type);
6049 field != NULL_TREE;
6050 field = TREE_CHAIN (field))
6052 if (DECL_NAME (field) != NULL_TREE
6053 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6054 continue;
6055 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6056 ? c_build_qualified_type (TREE_TYPE (field),
6057 TYPE_QUAL_ATOMIC)
6058 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6059 if (comptypes (lhs_main_type, fieldtype))
6061 if (found_field != NULL_TREE)
6062 return NULL_TREE;
6063 found_field = field;
6065 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6066 lhs_main_type))
6068 if (found_field != NULL_TREE)
6069 return NULL_TREE;
6070 found_field = field;
6071 found_sub_field = true;
6075 if (found_field == NULL_TREE)
6076 return NULL_TREE;
6078 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6079 build_fold_indirect_ref (rhs), found_field,
6080 NULL_TREE);
6081 ret = build_fold_addr_expr_loc (location, ret);
6083 if (found_sub_field)
6085 ret = convert_to_anonymous_field (location, type, ret);
6086 gcc_assert (ret != NULL_TREE);
6089 return ret;
6092 /* Issue an error message for a bad initializer component.
6093 GMSGID identifies the message.
6094 The component name is taken from the spelling stack. */
6096 static void
6097 error_init (location_t loc, const char *gmsgid)
6099 char *ofwhat;
6101 /* The gmsgid may be a format string with %< and %>. */
6102 error_at (loc, gmsgid);
6103 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6104 if (*ofwhat)
6105 inform (loc, "(near initialization for %qs)", ofwhat);
6108 /* Issue a pedantic warning for a bad initializer component. OPT is
6109 the option OPT_* (from options.h) controlling this warning or 0 if
6110 it is unconditionally given. GMSGID identifies the message. The
6111 component name is taken from the spelling stack. */
6113 static void ATTRIBUTE_GCC_DIAG (3,0)
6114 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6116 /* Use the location where a macro was expanded rather than where
6117 it was defined to make sure macros defined in system headers
6118 but used incorrectly elsewhere are diagnosed. */
6119 source_location exploc = expansion_point_location_if_in_system_header (loc);
6121 va_list ap;
6122 va_start (ap, gmsgid);
6123 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6124 va_end (ap);
6125 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6126 if (*ofwhat && warned)
6127 inform (exploc, "(near initialization for %qs)", ofwhat);
6130 /* Issue a warning for a bad initializer component.
6132 OPT is the OPT_W* value corresponding to the warning option that
6133 controls this warning. GMSGID identifies the message. The
6134 component name is taken from the spelling stack. */
6136 static void
6137 warning_init (location_t loc, int opt, const char *gmsgid)
6139 char *ofwhat;
6140 bool warned;
6142 /* Use the location where a macro was expanded rather than where
6143 it was defined to make sure macros defined in system headers
6144 but used incorrectly elsewhere are diagnosed. */
6145 source_location exploc = expansion_point_location_if_in_system_header (loc);
6147 /* The gmsgid may be a format string with %< and %>. */
6148 warned = warning_at (exploc, opt, gmsgid);
6149 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6150 if (*ofwhat && warned)
6151 inform (exploc, "(near initialization for %qs)", ofwhat);
6154 /* If TYPE is an array type and EXPR is a parenthesized string
6155 constant, warn if pedantic that EXPR is being used to initialize an
6156 object of type TYPE. */
6158 void
6159 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6161 if (pedantic
6162 && TREE_CODE (type) == ARRAY_TYPE
6163 && TREE_CODE (expr.value) == STRING_CST
6164 && expr.original_code != STRING_CST)
6165 pedwarn_init (loc, OPT_Wpedantic,
6166 "array initialized from parenthesized string constant");
6169 /* Attempt to locate the parameter with the given index within FNDECL,
6170 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6172 static location_t
6173 get_fndecl_argument_location (tree fndecl, int argnum)
6175 int i;
6176 tree param;
6178 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6179 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6180 i < argnum && param;
6181 i++, param = TREE_CHAIN (param))
6184 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6185 return DECL_SOURCE_LOCATION (FNDECL). */
6186 if (param == NULL)
6187 return DECL_SOURCE_LOCATION (fndecl);
6189 return DECL_SOURCE_LOCATION (param);
6192 /* Issue a note about a mismatching argument for parameter PARMNUM
6193 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6194 Attempt to issue the note at the pertinent parameter of the decl;
6195 failing that issue it at the location of FUNDECL; failing that
6196 issue it at PLOC. */
6198 static void
6199 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6200 tree expected_type, tree actual_type)
6202 location_t loc;
6203 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6204 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6205 else
6206 loc = ploc;
6208 inform (loc,
6209 "expected %qT but argument is of type %qT",
6210 expected_type, actual_type);
6213 /* Convert value RHS to type TYPE as preparation for an assignment to
6214 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6215 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6216 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6217 constant before any folding.
6218 The real work of conversion is done by `convert'.
6219 The purpose of this function is to generate error messages
6220 for assignments that are not allowed in C.
6221 ERRTYPE says whether it is argument passing, assignment,
6222 initialization or return.
6224 In the following example, '~' denotes where EXPR_LOC and '^' where
6225 LOCATION point to:
6227 f (var); [ic_argpass]
6228 ^ ~~~
6229 x = var; [ic_assign]
6230 ^ ~~~;
6231 int x = var; [ic_init]
6233 return x; [ic_return]
6236 FUNCTION is a tree for the function being called.
6237 PARMNUM is the number of the argument, for printing in error messages. */
6239 static tree
6240 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6241 tree rhs, tree origtype, enum impl_conv errtype,
6242 bool null_pointer_constant, tree fundecl,
6243 tree function, int parmnum)
6245 enum tree_code codel = TREE_CODE (type);
6246 tree orig_rhs = rhs;
6247 tree rhstype;
6248 enum tree_code coder;
6249 tree rname = NULL_TREE;
6250 bool objc_ok = false;
6252 /* Use the expansion point location to handle cases such as user's
6253 function returning a wrong-type macro defined in a system header. */
6254 location = expansion_point_location_if_in_system_header (location);
6256 if (errtype == ic_argpass)
6258 tree selector;
6259 /* Change pointer to function to the function itself for
6260 diagnostics. */
6261 if (TREE_CODE (function) == ADDR_EXPR
6262 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6263 function = TREE_OPERAND (function, 0);
6265 /* Handle an ObjC selector specially for diagnostics. */
6266 selector = objc_message_selector ();
6267 rname = function;
6268 if (selector && parmnum > 2)
6270 rname = selector;
6271 parmnum -= 2;
6275 /* This macro is used to emit diagnostics to ensure that all format
6276 strings are complete sentences, visible to gettext and checked at
6277 compile time. */
6278 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6279 do { \
6280 switch (errtype) \
6282 case ic_argpass: \
6283 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6284 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6285 break; \
6286 case ic_assign: \
6287 pedwarn (LOCATION, OPT, AS); \
6288 break; \
6289 case ic_init: \
6290 pedwarn_init (LOCATION, OPT, IN); \
6291 break; \
6292 case ic_return: \
6293 pedwarn (LOCATION, OPT, RE); \
6294 break; \
6295 default: \
6296 gcc_unreachable (); \
6298 } while (0)
6300 /* This macro is used to emit diagnostics to ensure that all format
6301 strings are complete sentences, visible to gettext and checked at
6302 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6303 extra parameter to enumerate qualifiers. */
6304 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6305 do { \
6306 switch (errtype) \
6308 case ic_argpass: \
6309 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6310 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6311 break; \
6312 case ic_assign: \
6313 pedwarn (LOCATION, OPT, AS, QUALS); \
6314 break; \
6315 case ic_init: \
6316 pedwarn (LOCATION, OPT, IN, QUALS); \
6317 break; \
6318 case ic_return: \
6319 pedwarn (LOCATION, OPT, RE, QUALS); \
6320 break; \
6321 default: \
6322 gcc_unreachable (); \
6324 } while (0)
6326 /* This macro is used to emit diagnostics to ensure that all format
6327 strings are complete sentences, visible to gettext and checked at
6328 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6329 warning_at instead of pedwarn. */
6330 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6331 do { \
6332 switch (errtype) \
6334 case ic_argpass: \
6335 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6336 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6337 break; \
6338 case ic_assign: \
6339 warning_at (LOCATION, OPT, AS, QUALS); \
6340 break; \
6341 case ic_init: \
6342 warning_at (LOCATION, OPT, IN, QUALS); \
6343 break; \
6344 case ic_return: \
6345 warning_at (LOCATION, OPT, RE, QUALS); \
6346 break; \
6347 default: \
6348 gcc_unreachable (); \
6350 } while (0)
6352 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6353 rhs = TREE_OPERAND (rhs, 0);
6355 rhstype = TREE_TYPE (rhs);
6356 coder = TREE_CODE (rhstype);
6358 if (coder == ERROR_MARK)
6359 return error_mark_node;
6361 if (c_dialect_objc ())
6363 int parmno;
6365 switch (errtype)
6367 case ic_return:
6368 parmno = 0;
6369 break;
6371 case ic_assign:
6372 parmno = -1;
6373 break;
6375 case ic_init:
6376 parmno = -2;
6377 break;
6379 default:
6380 parmno = parmnum;
6381 break;
6384 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6387 if (warn_cxx_compat)
6389 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6390 if (checktype != error_mark_node
6391 && TREE_CODE (type) == ENUMERAL_TYPE
6392 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6393 switch (errtype)
6395 case ic_argpass:
6396 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6397 "passing argument %d of %qE is invalid in C++",
6398 parmnum, rname))
6399 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6400 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6401 "expected %qT but argument is of type %qT",
6402 type, rhstype);
6403 break;
6404 case ic_assign:
6405 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6406 "%qT in assignment is invalid in C++", rhstype, type);
6407 break;
6408 case ic_init:
6409 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6410 "%qT to %qT in initialization is invalid in C++",
6411 rhstype, type);
6412 break;
6413 case ic_return:
6414 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6415 "%qT in return is invalid in C++", rhstype, type);
6416 break;
6417 default:
6418 gcc_unreachable ();
6422 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6423 return rhs;
6425 if (coder == VOID_TYPE)
6427 /* Except for passing an argument to an unprototyped function,
6428 this is a constraint violation. When passing an argument to
6429 an unprototyped function, it is compile-time undefined;
6430 making it a constraint in that case was rejected in
6431 DR#252. */
6432 error_at (location, "void value not ignored as it ought to be");
6433 return error_mark_node;
6435 rhs = require_complete_type (location, rhs);
6436 if (rhs == error_mark_node)
6437 return error_mark_node;
6439 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6440 return error_mark_node;
6442 /* A non-reference type can convert to a reference. This handles
6443 va_start, va_copy and possibly port built-ins. */
6444 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6446 if (!lvalue_p (rhs))
6448 error_at (location, "cannot pass rvalue to reference parameter");
6449 return error_mark_node;
6451 if (!c_mark_addressable (rhs))
6452 return error_mark_node;
6453 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6454 SET_EXPR_LOCATION (rhs, location);
6456 rhs = convert_for_assignment (location, expr_loc,
6457 build_pointer_type (TREE_TYPE (type)),
6458 rhs, origtype, errtype,
6459 null_pointer_constant, fundecl, function,
6460 parmnum);
6461 if (rhs == error_mark_node)
6462 return error_mark_node;
6464 rhs = build1 (NOP_EXPR, type, rhs);
6465 SET_EXPR_LOCATION (rhs, location);
6466 return rhs;
6468 /* Some types can interconvert without explicit casts. */
6469 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6470 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6471 return convert (type, rhs);
6472 /* Arithmetic types all interconvert, and enum is treated like int. */
6473 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6474 || codel == FIXED_POINT_TYPE
6475 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6476 || codel == BOOLEAN_TYPE)
6477 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6478 || coder == FIXED_POINT_TYPE
6479 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6480 || coder == BOOLEAN_TYPE))
6482 tree ret;
6483 bool save = in_late_binary_op;
6484 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6485 || (coder == REAL_TYPE
6486 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6487 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6488 in_late_binary_op = true;
6489 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6490 ? expr_loc : location, type, orig_rhs);
6491 in_late_binary_op = save;
6492 return ret;
6495 /* Aggregates in different TUs might need conversion. */
6496 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6497 && codel == coder
6498 && comptypes (type, rhstype))
6499 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6500 ? expr_loc : location, type, rhs);
6502 /* Conversion to a transparent union or record from its member types.
6503 This applies only to function arguments. */
6504 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6505 && TYPE_TRANSPARENT_AGGR (type))
6506 && errtype == ic_argpass)
6508 tree memb, marginal_memb = NULL_TREE;
6510 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6512 tree memb_type = TREE_TYPE (memb);
6514 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6515 TYPE_MAIN_VARIANT (rhstype)))
6516 break;
6518 if (TREE_CODE (memb_type) != POINTER_TYPE)
6519 continue;
6521 if (coder == POINTER_TYPE)
6523 tree ttl = TREE_TYPE (memb_type);
6524 tree ttr = TREE_TYPE (rhstype);
6526 /* Any non-function converts to a [const][volatile] void *
6527 and vice versa; otherwise, targets must be the same.
6528 Meanwhile, the lhs target must have all the qualifiers of
6529 the rhs. */
6530 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6531 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6532 || comp_target_types (location, memb_type, rhstype))
6534 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6535 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6536 /* If this type won't generate any warnings, use it. */
6537 if (lquals == rquals
6538 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6539 && TREE_CODE (ttl) == FUNCTION_TYPE)
6540 ? ((lquals | rquals) == rquals)
6541 : ((lquals | rquals) == lquals)))
6542 break;
6544 /* Keep looking for a better type, but remember this one. */
6545 if (!marginal_memb)
6546 marginal_memb = memb;
6550 /* Can convert integer zero to any pointer type. */
6551 if (null_pointer_constant)
6553 rhs = null_pointer_node;
6554 break;
6558 if (memb || marginal_memb)
6560 if (!memb)
6562 /* We have only a marginally acceptable member type;
6563 it needs a warning. */
6564 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6565 tree ttr = TREE_TYPE (rhstype);
6567 /* Const and volatile mean something different for function
6568 types, so the usual warnings are not appropriate. */
6569 if (TREE_CODE (ttr) == FUNCTION_TYPE
6570 && TREE_CODE (ttl) == FUNCTION_TYPE)
6572 /* Because const and volatile on functions are
6573 restrictions that say the function will not do
6574 certain things, it is okay to use a const or volatile
6575 function where an ordinary one is wanted, but not
6576 vice-versa. */
6577 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6578 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6579 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6580 OPT_Wdiscarded_qualifiers,
6581 G_("passing argument %d of %qE "
6582 "makes %q#v qualified function "
6583 "pointer from unqualified"),
6584 G_("assignment makes %q#v qualified "
6585 "function pointer from "
6586 "unqualified"),
6587 G_("initialization makes %q#v qualified "
6588 "function pointer from "
6589 "unqualified"),
6590 G_("return makes %q#v qualified function "
6591 "pointer from unqualified"),
6592 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6594 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6595 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6596 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6597 OPT_Wdiscarded_qualifiers,
6598 G_("passing argument %d of %qE discards "
6599 "%qv qualifier from pointer target type"),
6600 G_("assignment discards %qv qualifier "
6601 "from pointer target type"),
6602 G_("initialization discards %qv qualifier "
6603 "from pointer target type"),
6604 G_("return discards %qv qualifier from "
6605 "pointer target type"),
6606 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6608 memb = marginal_memb;
6611 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6612 pedwarn (location, OPT_Wpedantic,
6613 "ISO C prohibits argument conversion to union type");
6615 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6616 return build_constructor_single (type, memb, rhs);
6620 /* Conversions among pointers */
6621 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6622 && (coder == codel))
6624 tree ttl = TREE_TYPE (type);
6625 tree ttr = TREE_TYPE (rhstype);
6626 tree mvl = ttl;
6627 tree mvr = ttr;
6628 bool is_opaque_pointer;
6629 int target_cmp = 0; /* Cache comp_target_types () result. */
6630 addr_space_t asl;
6631 addr_space_t asr;
6633 if (TREE_CODE (mvl) != ARRAY_TYPE)
6634 mvl = (TYPE_ATOMIC (mvl)
6635 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6636 TYPE_QUAL_ATOMIC)
6637 : TYPE_MAIN_VARIANT (mvl));
6638 if (TREE_CODE (mvr) != ARRAY_TYPE)
6639 mvr = (TYPE_ATOMIC (mvr)
6640 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6641 TYPE_QUAL_ATOMIC)
6642 : TYPE_MAIN_VARIANT (mvr));
6643 /* Opaque pointers are treated like void pointers. */
6644 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6646 /* The Plan 9 compiler permits a pointer to a struct to be
6647 automatically converted into a pointer to an anonymous field
6648 within the struct. */
6649 if (flag_plan9_extensions
6650 && RECORD_OR_UNION_TYPE_P (mvl)
6651 && RECORD_OR_UNION_TYPE_P (mvr)
6652 && mvl != mvr)
6654 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6655 if (new_rhs != NULL_TREE)
6657 rhs = new_rhs;
6658 rhstype = TREE_TYPE (rhs);
6659 coder = TREE_CODE (rhstype);
6660 ttr = TREE_TYPE (rhstype);
6661 mvr = TYPE_MAIN_VARIANT (ttr);
6665 /* C++ does not allow the implicit conversion void* -> T*. However,
6666 for the purpose of reducing the number of false positives, we
6667 tolerate the special case of
6669 int *p = NULL;
6671 where NULL is typically defined in C to be '(void *) 0'. */
6672 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6673 warning_at (errtype == ic_argpass ? expr_loc : location,
6674 OPT_Wc___compat,
6675 "request for implicit conversion "
6676 "from %qT to %qT not permitted in C++", rhstype, type);
6678 /* See if the pointers point to incompatible address spaces. */
6679 asl = TYPE_ADDR_SPACE (ttl);
6680 asr = TYPE_ADDR_SPACE (ttr);
6681 if (!null_pointer_constant_p (rhs)
6682 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6684 switch (errtype)
6686 case ic_argpass:
6687 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6688 "non-enclosed address space", parmnum, rname);
6689 break;
6690 case ic_assign:
6691 error_at (location, "assignment from pointer to "
6692 "non-enclosed address space");
6693 break;
6694 case ic_init:
6695 error_at (location, "initialization from pointer to "
6696 "non-enclosed address space");
6697 break;
6698 case ic_return:
6699 error_at (location, "return from pointer to "
6700 "non-enclosed address space");
6701 break;
6702 default:
6703 gcc_unreachable ();
6705 return error_mark_node;
6708 /* Check if the right-hand side has a format attribute but the
6709 left-hand side doesn't. */
6710 if (warn_suggest_attribute_format
6711 && check_missing_format_attribute (type, rhstype))
6713 switch (errtype)
6715 case ic_argpass:
6716 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6717 "argument %d of %qE might be "
6718 "a candidate for a format attribute",
6719 parmnum, rname);
6720 break;
6721 case ic_assign:
6722 warning_at (location, OPT_Wsuggest_attribute_format,
6723 "assignment left-hand side might be "
6724 "a candidate for a format attribute");
6725 break;
6726 case ic_init:
6727 warning_at (location, OPT_Wsuggest_attribute_format,
6728 "initialization left-hand side might be "
6729 "a candidate for a format attribute");
6730 break;
6731 case ic_return:
6732 warning_at (location, OPT_Wsuggest_attribute_format,
6733 "return type might be "
6734 "a candidate for a format attribute");
6735 break;
6736 default:
6737 gcc_unreachable ();
6741 /* Any non-function converts to a [const][volatile] void *
6742 and vice versa; otherwise, targets must be the same.
6743 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6744 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6745 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6746 || (target_cmp = comp_target_types (location, type, rhstype))
6747 || is_opaque_pointer
6748 || ((c_common_unsigned_type (mvl)
6749 == c_common_unsigned_type (mvr))
6750 && (c_common_signed_type (mvl)
6751 == c_common_signed_type (mvr))
6752 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6754 /* Warn about loss of qualifers from pointers to arrays with
6755 qualifiers on the element type. */
6756 if (TREE_CODE (ttr) == ARRAY_TYPE)
6758 ttr = strip_array_types (ttr);
6759 ttl = strip_array_types (ttl);
6761 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6762 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6763 WARNING_FOR_QUALIFIERS (location, expr_loc,
6764 OPT_Wdiscarded_array_qualifiers,
6765 G_("passing argument %d of %qE discards "
6766 "%qv qualifier from pointer target type"),
6767 G_("assignment discards %qv qualifier "
6768 "from pointer target type"),
6769 G_("initialization discards %qv qualifier "
6770 "from pointer target type"),
6771 G_("return discards %qv qualifier from "
6772 "pointer target type"),
6773 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6775 else if (pedantic
6776 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6778 (VOID_TYPE_P (ttr)
6779 && !null_pointer_constant
6780 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6781 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6782 G_("ISO C forbids passing argument %d of "
6783 "%qE between function pointer "
6784 "and %<void *%>"),
6785 G_("ISO C forbids assignment between "
6786 "function pointer and %<void *%>"),
6787 G_("ISO C forbids initialization between "
6788 "function pointer and %<void *%>"),
6789 G_("ISO C forbids return between function "
6790 "pointer and %<void *%>"));
6791 /* Const and volatile mean something different for function types,
6792 so the usual warnings are not appropriate. */
6793 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6794 && TREE_CODE (ttl) != FUNCTION_TYPE)
6796 /* Don't warn about loss of qualifier for conversions from
6797 qualified void* to pointers to arrays with corresponding
6798 qualifier on the element type. */
6799 if (!pedantic)
6800 ttl = strip_array_types (ttl);
6802 /* Assignments between atomic and non-atomic objects are OK. */
6803 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6804 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6806 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6807 OPT_Wdiscarded_qualifiers,
6808 G_("passing argument %d of %qE discards "
6809 "%qv qualifier from pointer target type"),
6810 G_("assignment discards %qv qualifier "
6811 "from pointer target type"),
6812 G_("initialization discards %qv qualifier "
6813 "from pointer target type"),
6814 G_("return discards %qv qualifier from "
6815 "pointer target type"),
6816 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6818 /* If this is not a case of ignoring a mismatch in signedness,
6819 no warning. */
6820 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6821 || target_cmp)
6823 /* If there is a mismatch, do warn. */
6824 else if (warn_pointer_sign)
6825 switch (errtype)
6827 case ic_argpass:
6828 if (pedwarn (expr_loc, OPT_Wpointer_sign,
6829 "pointer targets in passing argument %d of "
6830 "%qE differ in signedness", parmnum, rname))
6831 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6832 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6833 "expected %qT but argument is of type %qT",
6834 type, rhstype);
6835 break;
6836 case ic_assign:
6837 pedwarn (location, OPT_Wpointer_sign,
6838 "pointer targets in assignment from %qT to %qT "
6839 "differ in signedness", rhstype, type);
6840 break;
6841 case ic_init:
6842 pedwarn_init (location, OPT_Wpointer_sign,
6843 "pointer targets in initialization of %qT "
6844 "from %qT differ in signedness", type,
6845 rhstype);
6846 break;
6847 case ic_return:
6848 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
6849 "returning %qT from a function with return type "
6850 "%qT differ in signedness", rhstype, type);
6851 break;
6852 default:
6853 gcc_unreachable ();
6856 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6857 && TREE_CODE (ttr) == FUNCTION_TYPE)
6859 /* Because const and volatile on functions are restrictions
6860 that say the function will not do certain things,
6861 it is okay to use a const or volatile function
6862 where an ordinary one is wanted, but not vice-versa. */
6863 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6864 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6865 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6866 OPT_Wdiscarded_qualifiers,
6867 G_("passing argument %d of %qE makes "
6868 "%q#v qualified function pointer "
6869 "from unqualified"),
6870 G_("assignment makes %q#v qualified function "
6871 "pointer from unqualified"),
6872 G_("initialization makes %q#v qualified "
6873 "function pointer from unqualified"),
6874 G_("return makes %q#v qualified function "
6875 "pointer from unqualified"),
6876 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6879 /* Avoid warning about the volatile ObjC EH puts on decls. */
6880 else if (!objc_ok)
6882 switch (errtype)
6884 case ic_argpass:
6885 if (pedwarn (expr_loc, OPT_Wincompatible_pointer_types,
6886 "passing argument %d of %qE from incompatible "
6887 "pointer type", parmnum, rname))
6888 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
6889 break;
6890 case ic_assign:
6891 pedwarn (location, OPT_Wincompatible_pointer_types,
6892 "assignment to %qT from incompatible pointer type %qT",
6893 type, rhstype);
6894 break;
6895 case ic_init:
6896 pedwarn_init (location, OPT_Wincompatible_pointer_types,
6897 "initialization of %qT from incompatible pointer "
6898 "type %qT", type, rhstype);
6899 break;
6900 case ic_return:
6901 pedwarn (location, OPT_Wincompatible_pointer_types,
6902 "returning %qT from a function with incompatible "
6903 "return type %qT", rhstype, type);
6904 break;
6905 default:
6906 gcc_unreachable ();
6910 return convert (type, rhs);
6912 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6914 /* ??? This should not be an error when inlining calls to
6915 unprototyped functions. */
6916 error_at (location, "invalid use of non-lvalue array");
6917 return error_mark_node;
6919 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6921 /* An explicit constant 0 can convert to a pointer,
6922 or one that results from arithmetic, even including
6923 a cast to integer type. */
6924 if (!null_pointer_constant)
6925 switch (errtype)
6927 case ic_argpass:
6928 if (pedwarn (expr_loc, OPT_Wint_conversion,
6929 "passing argument %d of %qE makes pointer from "
6930 "integer without a cast", parmnum, rname))
6931 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
6932 break;
6933 case ic_assign:
6934 pedwarn (location, OPT_Wint_conversion,
6935 "assignment to %qT from %qT makes pointer from integer "
6936 "without a cast", type, rhstype);
6937 break;
6938 case ic_init:
6939 pedwarn_init (location, OPT_Wint_conversion,
6940 "initialization of %qT from %qT makes pointer from "
6941 "integer without a cast", type, rhstype);
6942 break;
6943 case ic_return:
6944 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6945 "function with return type %qT makes pointer from "
6946 "integer without a cast", rhstype, type);
6947 break;
6948 default:
6949 gcc_unreachable ();
6952 return convert (type, rhs);
6954 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6956 switch (errtype)
6958 case ic_argpass:
6959 if (pedwarn (expr_loc, OPT_Wint_conversion,
6960 "passing argument %d of %qE makes integer from "
6961 "pointer without a cast", parmnum, rname))
6962 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
6963 break;
6964 case ic_assign:
6965 pedwarn (location, OPT_Wint_conversion,
6966 "assignment to %qT from %qT makes integer from pointer "
6967 "without a cast", type, rhstype);
6968 break;
6969 case ic_init:
6970 pedwarn_init (location, OPT_Wint_conversion,
6971 "initialization of %qT from %qT makes integer from "
6972 "pointer without a cast", type, rhstype);
6973 break;
6974 case ic_return:
6975 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6976 "function with return type %qT makes integer from "
6977 "pointer without a cast", rhstype, type);
6978 break;
6979 default:
6980 gcc_unreachable ();
6983 return convert (type, rhs);
6985 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6987 tree ret;
6988 bool save = in_late_binary_op;
6989 in_late_binary_op = true;
6990 ret = convert (type, rhs);
6991 in_late_binary_op = save;
6992 return ret;
6995 switch (errtype)
6997 case ic_argpass:
6998 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6999 rname);
7000 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7001 break;
7002 case ic_assign:
7003 error_at (location, "incompatible types when assigning to type %qT from "
7004 "type %qT", type, rhstype);
7005 break;
7006 case ic_init:
7007 error_at (location,
7008 "incompatible types when initializing type %qT using type %qT",
7009 type, rhstype);
7010 break;
7011 case ic_return:
7012 error_at (location,
7013 "incompatible types when returning type %qT but %qT was "
7014 "expected", rhstype, type);
7015 break;
7016 default:
7017 gcc_unreachable ();
7020 return error_mark_node;
7023 /* If VALUE is a compound expr all of whose expressions are constant, then
7024 return its value. Otherwise, return error_mark_node.
7026 This is for handling COMPOUND_EXPRs as initializer elements
7027 which is allowed with a warning when -pedantic is specified. */
7029 static tree
7030 valid_compound_expr_initializer (tree value, tree endtype)
7032 if (TREE_CODE (value) == COMPOUND_EXPR)
7034 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7035 == error_mark_node)
7036 return error_mark_node;
7037 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7038 endtype);
7040 else if (!initializer_constant_valid_p (value, endtype))
7041 return error_mark_node;
7042 else
7043 return value;
7046 /* Perform appropriate conversions on the initial value of a variable,
7047 store it in the declaration DECL,
7048 and print any error messages that are appropriate.
7049 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7050 If the init is invalid, store an ERROR_MARK.
7052 INIT_LOC is the location of the initial value. */
7054 void
7055 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7057 tree value, type;
7058 bool npc = false;
7060 /* If variable's type was invalidly declared, just ignore it. */
7062 type = TREE_TYPE (decl);
7063 if (TREE_CODE (type) == ERROR_MARK)
7064 return;
7066 /* Digest the specified initializer into an expression. */
7068 if (init)
7069 npc = null_pointer_constant_p (init);
7070 value = digest_init (init_loc, type, init, origtype, npc,
7071 true, TREE_STATIC (decl));
7073 /* Store the expression if valid; else report error. */
7075 if (!in_system_header_at (input_location)
7076 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7077 warning (OPT_Wtraditional, "traditional C rejects automatic "
7078 "aggregate initialization");
7080 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7081 DECL_INITIAL (decl) = value;
7083 /* ANSI wants warnings about out-of-range constant initializers. */
7084 STRIP_TYPE_NOPS (value);
7085 if (TREE_STATIC (decl))
7086 constant_expression_warning (value);
7088 /* Check if we need to set array size from compound literal size. */
7089 if (TREE_CODE (type) == ARRAY_TYPE
7090 && TYPE_DOMAIN (type) == NULL_TREE
7091 && value != error_mark_node)
7093 tree inside_init = init;
7095 STRIP_TYPE_NOPS (inside_init);
7096 inside_init = fold (inside_init);
7098 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7100 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7102 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7104 /* For int foo[] = (int [3]){1}; we need to set array size
7105 now since later on array initializer will be just the
7106 brace enclosed list of the compound literal. */
7107 tree etype = strip_array_types (TREE_TYPE (decl));
7108 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7109 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7110 layout_type (type);
7111 layout_decl (cldecl, 0);
7112 TREE_TYPE (decl)
7113 = c_build_qualified_type (type, TYPE_QUALS (etype));
7119 /* Methods for storing and printing names for error messages. */
7121 /* Implement a spelling stack that allows components of a name to be pushed
7122 and popped. Each element on the stack is this structure. */
7124 struct spelling
7126 int kind;
7127 union
7129 unsigned HOST_WIDE_INT i;
7130 const char *s;
7131 } u;
7134 #define SPELLING_STRING 1
7135 #define SPELLING_MEMBER 2
7136 #define SPELLING_BOUNDS 3
7138 static struct spelling *spelling; /* Next stack element (unused). */
7139 static struct spelling *spelling_base; /* Spelling stack base. */
7140 static int spelling_size; /* Size of the spelling stack. */
7142 /* Macros to save and restore the spelling stack around push_... functions.
7143 Alternative to SAVE_SPELLING_STACK. */
7145 #define SPELLING_DEPTH() (spelling - spelling_base)
7146 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7148 /* Push an element on the spelling stack with type KIND and assign VALUE
7149 to MEMBER. */
7151 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7153 int depth = SPELLING_DEPTH (); \
7155 if (depth >= spelling_size) \
7157 spelling_size += 10; \
7158 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7159 spelling_size); \
7160 RESTORE_SPELLING_DEPTH (depth); \
7163 spelling->kind = (KIND); \
7164 spelling->MEMBER = (VALUE); \
7165 spelling++; \
7168 /* Push STRING on the stack. Printed literally. */
7170 static void
7171 push_string (const char *string)
7173 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7176 /* Push a member name on the stack. Printed as '.' STRING. */
7178 static void
7179 push_member_name (tree decl)
7181 const char *const string
7182 = (DECL_NAME (decl)
7183 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7184 : _("<anonymous>"));
7185 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7188 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7190 static void
7191 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7193 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7196 /* Compute the maximum size in bytes of the printed spelling. */
7198 static int
7199 spelling_length (void)
7201 int size = 0;
7202 struct spelling *p;
7204 for (p = spelling_base; p < spelling; p++)
7206 if (p->kind == SPELLING_BOUNDS)
7207 size += 25;
7208 else
7209 size += strlen (p->u.s) + 1;
7212 return size;
7215 /* Print the spelling to BUFFER and return it. */
7217 static char *
7218 print_spelling (char *buffer)
7220 char *d = buffer;
7221 struct spelling *p;
7223 for (p = spelling_base; p < spelling; p++)
7224 if (p->kind == SPELLING_BOUNDS)
7226 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7227 d += strlen (d);
7229 else
7231 const char *s;
7232 if (p->kind == SPELLING_MEMBER)
7233 *d++ = '.';
7234 for (s = p->u.s; (*d = *s++); d++)
7237 *d++ = '\0';
7238 return buffer;
7241 /* Digest the parser output INIT as an initializer for type TYPE.
7242 Return a C expression of type TYPE to represent the initial value.
7244 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7246 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7248 If INIT is a string constant, STRICT_STRING is true if it is
7249 unparenthesized or we should not warn here for it being parenthesized.
7250 For other types of INIT, STRICT_STRING is not used.
7252 INIT_LOC is the location of the INIT.
7254 REQUIRE_CONSTANT requests an error if non-constant initializers or
7255 elements are seen. */
7257 static tree
7258 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7259 bool null_pointer_constant, bool strict_string,
7260 int require_constant)
7262 enum tree_code code = TREE_CODE (type);
7263 tree inside_init = init;
7264 tree semantic_type = NULL_TREE;
7265 bool maybe_const = true;
7267 if (type == error_mark_node
7268 || !init
7269 || error_operand_p (init))
7270 return error_mark_node;
7272 STRIP_TYPE_NOPS (inside_init);
7274 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7276 semantic_type = TREE_TYPE (inside_init);
7277 inside_init = TREE_OPERAND (inside_init, 0);
7279 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7281 /* Initialization of an array of chars from a string constant
7282 optionally enclosed in braces. */
7284 if (code == ARRAY_TYPE && inside_init
7285 && TREE_CODE (inside_init) == STRING_CST)
7287 tree typ1
7288 = (TYPE_ATOMIC (TREE_TYPE (type))
7289 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7290 TYPE_QUAL_ATOMIC)
7291 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7292 /* Note that an array could be both an array of character type
7293 and an array of wchar_t if wchar_t is signed char or unsigned
7294 char. */
7295 bool char_array = (typ1 == char_type_node
7296 || typ1 == signed_char_type_node
7297 || typ1 == unsigned_char_type_node);
7298 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7299 bool char16_array = !!comptypes (typ1, char16_type_node);
7300 bool char32_array = !!comptypes (typ1, char32_type_node);
7302 if (char_array || wchar_array || char16_array || char32_array)
7304 struct c_expr expr;
7305 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7306 expr.value = inside_init;
7307 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7308 expr.original_type = NULL;
7309 maybe_warn_string_init (init_loc, type, expr);
7311 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7312 pedwarn_init (init_loc, OPT_Wpedantic,
7313 "initialization of a flexible array member");
7315 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7316 TYPE_MAIN_VARIANT (type)))
7317 return inside_init;
7319 if (char_array)
7321 if (typ2 != char_type_node)
7323 error_init (init_loc, "char-array initialized from wide "
7324 "string");
7325 return error_mark_node;
7328 else
7330 if (typ2 == char_type_node)
7332 error_init (init_loc, "wide character array initialized "
7333 "from non-wide string");
7334 return error_mark_node;
7336 else if (!comptypes(typ1, typ2))
7338 error_init (init_loc, "wide character array initialized "
7339 "from incompatible wide string");
7340 return error_mark_node;
7344 TREE_TYPE (inside_init) = type;
7345 if (TYPE_DOMAIN (type) != NULL_TREE
7346 && TYPE_SIZE (type) != NULL_TREE
7347 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7349 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7351 /* Subtract the size of a single (possibly wide) character
7352 because it's ok to ignore the terminating null char
7353 that is counted in the length of the constant. */
7354 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7355 (len
7356 - (TYPE_PRECISION (typ1)
7357 / BITS_PER_UNIT))))
7358 pedwarn_init (init_loc, 0,
7359 ("initializer-string for array of chars "
7360 "is too long"));
7361 else if (warn_cxx_compat
7362 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7363 warning_at (init_loc, OPT_Wc___compat,
7364 ("initializer-string for array chars "
7365 "is too long for C++"));
7368 return inside_init;
7370 else if (INTEGRAL_TYPE_P (typ1))
7372 error_init (init_loc, "array of inappropriate type initialized "
7373 "from string constant");
7374 return error_mark_node;
7378 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7379 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7380 below and handle as a constructor. */
7381 if (code == VECTOR_TYPE
7382 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7383 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7384 && TREE_CONSTANT (inside_init))
7386 if (TREE_CODE (inside_init) == VECTOR_CST
7387 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7388 TYPE_MAIN_VARIANT (type)))
7389 return inside_init;
7391 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7393 unsigned HOST_WIDE_INT ix;
7394 tree value;
7395 bool constant_p = true;
7397 /* Iterate through elements and check if all constructor
7398 elements are *_CSTs. */
7399 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7400 if (!CONSTANT_CLASS_P (value))
7402 constant_p = false;
7403 break;
7406 if (constant_p)
7407 return build_vector_from_ctor (type,
7408 CONSTRUCTOR_ELTS (inside_init));
7412 if (warn_sequence_point)
7413 verify_sequence_points (inside_init);
7415 /* Any type can be initialized
7416 from an expression of the same type, optionally with braces. */
7418 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7419 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7420 TYPE_MAIN_VARIANT (type))
7421 || (code == ARRAY_TYPE
7422 && comptypes (TREE_TYPE (inside_init), type))
7423 || (code == VECTOR_TYPE
7424 && comptypes (TREE_TYPE (inside_init), type))
7425 || (code == POINTER_TYPE
7426 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7427 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7428 TREE_TYPE (type)))))
7430 if (code == POINTER_TYPE)
7432 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7434 if (TREE_CODE (inside_init) == STRING_CST
7435 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7436 inside_init = array_to_pointer_conversion
7437 (init_loc, inside_init);
7438 else
7440 error_init (init_loc, "invalid use of non-lvalue array");
7441 return error_mark_node;
7446 if (code == VECTOR_TYPE)
7447 /* Although the types are compatible, we may require a
7448 conversion. */
7449 inside_init = convert (type, inside_init);
7451 if (require_constant
7452 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7454 /* As an extension, allow initializing objects with static storage
7455 duration with compound literals (which are then treated just as
7456 the brace enclosed list they contain). Also allow this for
7457 vectors, as we can only assign them with compound literals. */
7458 if (flag_isoc99 && code != VECTOR_TYPE)
7459 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7460 "is not constant");
7461 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7462 inside_init = DECL_INITIAL (decl);
7465 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7466 && TREE_CODE (inside_init) != CONSTRUCTOR)
7468 error_init (init_loc, "array initialized from non-constant array "
7469 "expression");
7470 return error_mark_node;
7473 /* Compound expressions can only occur here if -Wpedantic or
7474 -pedantic-errors is specified. In the later case, we always want
7475 an error. In the former case, we simply want a warning. */
7476 if (require_constant && pedantic
7477 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7479 inside_init
7480 = valid_compound_expr_initializer (inside_init,
7481 TREE_TYPE (inside_init));
7482 if (inside_init == error_mark_node)
7483 error_init (init_loc, "initializer element is not constant");
7484 else
7485 pedwarn_init (init_loc, OPT_Wpedantic,
7486 "initializer element is not constant");
7487 if (flag_pedantic_errors)
7488 inside_init = error_mark_node;
7490 else if (require_constant
7491 && !initializer_constant_valid_p (inside_init,
7492 TREE_TYPE (inside_init)))
7494 error_init (init_loc, "initializer element is not constant");
7495 inside_init = error_mark_node;
7497 else if (require_constant && !maybe_const)
7498 pedwarn_init (init_loc, OPT_Wpedantic,
7499 "initializer element is not a constant expression");
7501 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7502 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7503 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7504 type, inside_init, origtype,
7505 ic_init, null_pointer_constant,
7506 NULL_TREE, NULL_TREE, 0);
7507 return inside_init;
7510 /* Handle scalar types, including conversions. */
7512 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7513 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7514 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7516 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7517 && (TREE_CODE (init) == STRING_CST
7518 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7519 inside_init = init = array_to_pointer_conversion (init_loc, init);
7520 if (semantic_type)
7521 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7522 inside_init);
7523 inside_init
7524 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7525 inside_init, origtype, ic_init,
7526 null_pointer_constant, NULL_TREE, NULL_TREE,
7529 /* Check to see if we have already given an error message. */
7530 if (inside_init == error_mark_node)
7532 else if (require_constant && !TREE_CONSTANT (inside_init))
7534 error_init (init_loc, "initializer element is not constant");
7535 inside_init = error_mark_node;
7537 else if (require_constant
7538 && !initializer_constant_valid_p (inside_init,
7539 TREE_TYPE (inside_init)))
7541 error_init (init_loc, "initializer element is not computable at "
7542 "load time");
7543 inside_init = error_mark_node;
7545 else if (require_constant && !maybe_const)
7546 pedwarn_init (init_loc, OPT_Wpedantic,
7547 "initializer element is not a constant expression");
7549 return inside_init;
7552 /* Come here only for records and arrays. */
7554 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7556 error_init (init_loc, "variable-sized object may not be initialized");
7557 return error_mark_node;
7560 error_init (init_loc, "invalid initializer");
7561 return error_mark_node;
7564 /* Handle initializers that use braces. */
7566 /* Type of object we are accumulating a constructor for.
7567 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7568 static tree constructor_type;
7570 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7571 left to fill. */
7572 static tree constructor_fields;
7574 /* For an ARRAY_TYPE, this is the specified index
7575 at which to store the next element we get. */
7576 static tree constructor_index;
7578 /* For an ARRAY_TYPE, this is the maximum index. */
7579 static tree constructor_max_index;
7581 /* For a RECORD_TYPE, this is the first field not yet written out. */
7582 static tree constructor_unfilled_fields;
7584 /* For an ARRAY_TYPE, this is the index of the first element
7585 not yet written out. */
7586 static tree constructor_unfilled_index;
7588 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7589 This is so we can generate gaps between fields, when appropriate. */
7590 static tree constructor_bit_index;
7592 /* If we are saving up the elements rather than allocating them,
7593 this is the list of elements so far (in reverse order,
7594 most recent first). */
7595 static vec<constructor_elt, va_gc> *constructor_elements;
7597 /* 1 if constructor should be incrementally stored into a constructor chain,
7598 0 if all the elements should be kept in AVL tree. */
7599 static int constructor_incremental;
7601 /* 1 if so far this constructor's elements are all compile-time constants. */
7602 static int constructor_constant;
7604 /* 1 if so far this constructor's elements are all valid address constants. */
7605 static int constructor_simple;
7607 /* 1 if this constructor has an element that cannot be part of a
7608 constant expression. */
7609 static int constructor_nonconst;
7611 /* 1 if this constructor is erroneous so far. */
7612 static int constructor_erroneous;
7614 /* 1 if this constructor is the universal zero initializer { 0 }. */
7615 static int constructor_zeroinit;
7617 /* Structure for managing pending initializer elements, organized as an
7618 AVL tree. */
7620 struct init_node
7622 struct init_node *left, *right;
7623 struct init_node *parent;
7624 int balance;
7625 tree purpose;
7626 tree value;
7627 tree origtype;
7630 /* Tree of pending elements at this constructor level.
7631 These are elements encountered out of order
7632 which belong at places we haven't reached yet in actually
7633 writing the output.
7634 Will never hold tree nodes across GC runs. */
7635 static struct init_node *constructor_pending_elts;
7637 /* The SPELLING_DEPTH of this constructor. */
7638 static int constructor_depth;
7640 /* DECL node for which an initializer is being read.
7641 0 means we are reading a constructor expression
7642 such as (struct foo) {...}. */
7643 static tree constructor_decl;
7645 /* Nonzero if this is an initializer for a top-level decl. */
7646 static int constructor_top_level;
7648 /* Nonzero if there were any member designators in this initializer. */
7649 static int constructor_designated;
7651 /* Nesting depth of designator list. */
7652 static int designator_depth;
7654 /* Nonzero if there were diagnosed errors in this designator list. */
7655 static int designator_erroneous;
7658 /* This stack has a level for each implicit or explicit level of
7659 structuring in the initializer, including the outermost one. It
7660 saves the values of most of the variables above. */
7662 struct constructor_range_stack;
7664 struct constructor_stack
7666 struct constructor_stack *next;
7667 tree type;
7668 tree fields;
7669 tree index;
7670 tree max_index;
7671 tree unfilled_index;
7672 tree unfilled_fields;
7673 tree bit_index;
7674 vec<constructor_elt, va_gc> *elements;
7675 struct init_node *pending_elts;
7676 int offset;
7677 int depth;
7678 /* If value nonzero, this value should replace the entire
7679 constructor at this level. */
7680 struct c_expr replacement_value;
7681 struct constructor_range_stack *range_stack;
7682 char constant;
7683 char simple;
7684 char nonconst;
7685 char implicit;
7686 char erroneous;
7687 char outer;
7688 char incremental;
7689 char designated;
7690 int designator_depth;
7693 static struct constructor_stack *constructor_stack;
7695 /* This stack represents designators from some range designator up to
7696 the last designator in the list. */
7698 struct constructor_range_stack
7700 struct constructor_range_stack *next, *prev;
7701 struct constructor_stack *stack;
7702 tree range_start;
7703 tree index;
7704 tree range_end;
7705 tree fields;
7708 static struct constructor_range_stack *constructor_range_stack;
7710 /* This stack records separate initializers that are nested.
7711 Nested initializers can't happen in ANSI C, but GNU C allows them
7712 in cases like { ... (struct foo) { ... } ... }. */
7714 struct initializer_stack
7716 struct initializer_stack *next;
7717 tree decl;
7718 struct constructor_stack *constructor_stack;
7719 struct constructor_range_stack *constructor_range_stack;
7720 vec<constructor_elt, va_gc> *elements;
7721 struct spelling *spelling;
7722 struct spelling *spelling_base;
7723 int spelling_size;
7724 char top_level;
7725 char require_constant_value;
7726 char require_constant_elements;
7727 rich_location *missing_brace_richloc;
7730 static struct initializer_stack *initializer_stack;
7732 /* Prepare to parse and output the initializer for variable DECL. */
7734 void
7735 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7736 rich_location *richloc)
7738 const char *locus;
7739 struct initializer_stack *p = XNEW (struct initializer_stack);
7741 p->decl = constructor_decl;
7742 p->require_constant_value = require_constant_value;
7743 p->require_constant_elements = require_constant_elements;
7744 p->constructor_stack = constructor_stack;
7745 p->constructor_range_stack = constructor_range_stack;
7746 p->elements = constructor_elements;
7747 p->spelling = spelling;
7748 p->spelling_base = spelling_base;
7749 p->spelling_size = spelling_size;
7750 p->top_level = constructor_top_level;
7751 p->next = initializer_stack;
7752 p->missing_brace_richloc = richloc;
7753 initializer_stack = p;
7755 constructor_decl = decl;
7756 constructor_designated = 0;
7757 constructor_top_level = top_level;
7759 if (decl != NULL_TREE && decl != error_mark_node)
7761 require_constant_value = TREE_STATIC (decl);
7762 require_constant_elements
7763 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7764 /* For a scalar, you can always use any value to initialize,
7765 even within braces. */
7766 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7767 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7769 else
7771 require_constant_value = 0;
7772 require_constant_elements = 0;
7773 locus = _("(anonymous)");
7776 constructor_stack = 0;
7777 constructor_range_stack = 0;
7779 found_missing_braces = 0;
7781 spelling_base = 0;
7782 spelling_size = 0;
7783 RESTORE_SPELLING_DEPTH (0);
7785 if (locus)
7786 push_string (locus);
7789 void
7790 finish_init (void)
7792 struct initializer_stack *p = initializer_stack;
7794 /* Free the whole constructor stack of this initializer. */
7795 while (constructor_stack)
7797 struct constructor_stack *q = constructor_stack;
7798 constructor_stack = q->next;
7799 free (q);
7802 gcc_assert (!constructor_range_stack);
7804 /* Pop back to the data of the outer initializer (if any). */
7805 free (spelling_base);
7807 constructor_decl = p->decl;
7808 require_constant_value = p->require_constant_value;
7809 require_constant_elements = p->require_constant_elements;
7810 constructor_stack = p->constructor_stack;
7811 constructor_range_stack = p->constructor_range_stack;
7812 constructor_elements = p->elements;
7813 spelling = p->spelling;
7814 spelling_base = p->spelling_base;
7815 spelling_size = p->spelling_size;
7816 constructor_top_level = p->top_level;
7817 initializer_stack = p->next;
7818 free (p);
7821 /* Call here when we see the initializer is surrounded by braces.
7822 This is instead of a call to push_init_level;
7823 it is matched by a call to pop_init_level.
7825 TYPE is the type to initialize, for a constructor expression.
7826 For an initializer for a decl, TYPE is zero. */
7828 void
7829 really_start_incremental_init (tree type)
7831 struct constructor_stack *p = XNEW (struct constructor_stack);
7833 if (type == NULL_TREE)
7834 type = TREE_TYPE (constructor_decl);
7836 if (VECTOR_TYPE_P (type)
7837 && TYPE_VECTOR_OPAQUE (type))
7838 error ("opaque vector types cannot be initialized");
7840 p->type = constructor_type;
7841 p->fields = constructor_fields;
7842 p->index = constructor_index;
7843 p->max_index = constructor_max_index;
7844 p->unfilled_index = constructor_unfilled_index;
7845 p->unfilled_fields = constructor_unfilled_fields;
7846 p->bit_index = constructor_bit_index;
7847 p->elements = constructor_elements;
7848 p->constant = constructor_constant;
7849 p->simple = constructor_simple;
7850 p->nonconst = constructor_nonconst;
7851 p->erroneous = constructor_erroneous;
7852 p->pending_elts = constructor_pending_elts;
7853 p->depth = constructor_depth;
7854 p->replacement_value.value = 0;
7855 p->replacement_value.original_code = ERROR_MARK;
7856 p->replacement_value.original_type = NULL;
7857 p->implicit = 0;
7858 p->range_stack = 0;
7859 p->outer = 0;
7860 p->incremental = constructor_incremental;
7861 p->designated = constructor_designated;
7862 p->designator_depth = designator_depth;
7863 p->next = 0;
7864 constructor_stack = p;
7866 constructor_constant = 1;
7867 constructor_simple = 1;
7868 constructor_nonconst = 0;
7869 constructor_depth = SPELLING_DEPTH ();
7870 constructor_elements = NULL;
7871 constructor_pending_elts = 0;
7872 constructor_type = type;
7873 constructor_incremental = 1;
7874 constructor_designated = 0;
7875 constructor_zeroinit = 1;
7876 designator_depth = 0;
7877 designator_erroneous = 0;
7879 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7881 constructor_fields = TYPE_FIELDS (constructor_type);
7882 /* Skip any nameless bit fields at the beginning. */
7883 while (constructor_fields != NULL_TREE
7884 && DECL_C_BIT_FIELD (constructor_fields)
7885 && DECL_NAME (constructor_fields) == NULL_TREE)
7886 constructor_fields = DECL_CHAIN (constructor_fields);
7888 constructor_unfilled_fields = constructor_fields;
7889 constructor_bit_index = bitsize_zero_node;
7891 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7893 if (TYPE_DOMAIN (constructor_type))
7895 constructor_max_index
7896 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7898 /* Detect non-empty initializations of zero-length arrays. */
7899 if (constructor_max_index == NULL_TREE
7900 && TYPE_SIZE (constructor_type))
7901 constructor_max_index = integer_minus_one_node;
7903 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7904 to initialize VLAs will cause a proper error; avoid tree
7905 checking errors as well by setting a safe value. */
7906 if (constructor_max_index
7907 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7908 constructor_max_index = integer_minus_one_node;
7910 constructor_index
7911 = convert (bitsizetype,
7912 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7914 else
7916 constructor_index = bitsize_zero_node;
7917 constructor_max_index = NULL_TREE;
7920 constructor_unfilled_index = constructor_index;
7922 else if (VECTOR_TYPE_P (constructor_type))
7924 /* Vectors are like simple fixed-size arrays. */
7925 constructor_max_index =
7926 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7927 constructor_index = bitsize_zero_node;
7928 constructor_unfilled_index = constructor_index;
7930 else
7932 /* Handle the case of int x = {5}; */
7933 constructor_fields = constructor_type;
7934 constructor_unfilled_fields = constructor_type;
7938 extern location_t last_init_list_comma;
7940 /* Called when we see an open brace for a nested initializer. Finish
7941 off any pending levels with implicit braces. */
7942 void
7943 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7945 while (constructor_stack->implicit)
7947 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7948 && constructor_fields == NULL_TREE)
7949 process_init_element (input_location,
7950 pop_init_level (loc, 1, braced_init_obstack,
7951 last_init_list_comma),
7952 true, braced_init_obstack);
7953 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7954 && constructor_max_index
7955 && tree_int_cst_lt (constructor_max_index,
7956 constructor_index))
7957 process_init_element (input_location,
7958 pop_init_level (loc, 1, braced_init_obstack,
7959 last_init_list_comma),
7960 true, braced_init_obstack);
7961 else
7962 break;
7966 /* Push down into a subobject, for initialization.
7967 If this is for an explicit set of braces, IMPLICIT is 0.
7968 If it is because the next element belongs at a lower level,
7969 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7971 void
7972 push_init_level (location_t loc, int implicit,
7973 struct obstack *braced_init_obstack)
7975 struct constructor_stack *p;
7976 tree value = NULL_TREE;
7978 /* Unless this is an explicit brace, we need to preserve previous
7979 content if any. */
7980 if (implicit)
7982 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7983 value = find_init_member (constructor_fields, braced_init_obstack);
7984 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7985 value = find_init_member (constructor_index, braced_init_obstack);
7988 p = XNEW (struct constructor_stack);
7989 p->type = constructor_type;
7990 p->fields = constructor_fields;
7991 p->index = constructor_index;
7992 p->max_index = constructor_max_index;
7993 p->unfilled_index = constructor_unfilled_index;
7994 p->unfilled_fields = constructor_unfilled_fields;
7995 p->bit_index = constructor_bit_index;
7996 p->elements = constructor_elements;
7997 p->constant = constructor_constant;
7998 p->simple = constructor_simple;
7999 p->nonconst = constructor_nonconst;
8000 p->erroneous = constructor_erroneous;
8001 p->pending_elts = constructor_pending_elts;
8002 p->depth = constructor_depth;
8003 p->replacement_value.value = NULL_TREE;
8004 p->replacement_value.original_code = ERROR_MARK;
8005 p->replacement_value.original_type = NULL;
8006 p->implicit = implicit;
8007 p->outer = 0;
8008 p->incremental = constructor_incremental;
8009 p->designated = constructor_designated;
8010 p->designator_depth = designator_depth;
8011 p->next = constructor_stack;
8012 p->range_stack = 0;
8013 constructor_stack = p;
8015 constructor_constant = 1;
8016 constructor_simple = 1;
8017 constructor_nonconst = 0;
8018 constructor_depth = SPELLING_DEPTH ();
8019 constructor_elements = NULL;
8020 constructor_incremental = 1;
8021 constructor_designated = 0;
8022 constructor_pending_elts = 0;
8023 if (!implicit)
8025 p->range_stack = constructor_range_stack;
8026 constructor_range_stack = 0;
8027 designator_depth = 0;
8028 designator_erroneous = 0;
8031 /* Don't die if an entire brace-pair level is superfluous
8032 in the containing level. */
8033 if (constructor_type == NULL_TREE)
8035 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8037 /* Don't die if there are extra init elts at the end. */
8038 if (constructor_fields == NULL_TREE)
8039 constructor_type = NULL_TREE;
8040 else
8042 constructor_type = TREE_TYPE (constructor_fields);
8043 push_member_name (constructor_fields);
8044 constructor_depth++;
8046 /* If upper initializer is designated, then mark this as
8047 designated too to prevent bogus warnings. */
8048 constructor_designated = p->designated;
8050 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8052 constructor_type = TREE_TYPE (constructor_type);
8053 push_array_bounds (tree_to_uhwi (constructor_index));
8054 constructor_depth++;
8057 if (constructor_type == NULL_TREE)
8059 error_init (loc, "extra brace group at end of initializer");
8060 constructor_fields = NULL_TREE;
8061 constructor_unfilled_fields = NULL_TREE;
8062 return;
8065 if (value && TREE_CODE (value) == CONSTRUCTOR)
8067 constructor_constant = TREE_CONSTANT (value);
8068 constructor_simple = TREE_STATIC (value);
8069 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8070 constructor_elements = CONSTRUCTOR_ELTS (value);
8071 if (!vec_safe_is_empty (constructor_elements)
8072 && (TREE_CODE (constructor_type) == RECORD_TYPE
8073 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8074 set_nonincremental_init (braced_init_obstack);
8077 if (implicit == 1)
8079 found_missing_braces = 1;
8080 if (initializer_stack->missing_brace_richloc)
8081 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8082 (loc, "{");
8085 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8087 constructor_fields = TYPE_FIELDS (constructor_type);
8088 /* Skip any nameless bit fields at the beginning. */
8089 while (constructor_fields != NULL_TREE
8090 && DECL_C_BIT_FIELD (constructor_fields)
8091 && DECL_NAME (constructor_fields) == NULL_TREE)
8092 constructor_fields = DECL_CHAIN (constructor_fields);
8094 constructor_unfilled_fields = constructor_fields;
8095 constructor_bit_index = bitsize_zero_node;
8097 else if (VECTOR_TYPE_P (constructor_type))
8099 /* Vectors are like simple fixed-size arrays. */
8100 constructor_max_index =
8101 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8102 constructor_index = bitsize_int (0);
8103 constructor_unfilled_index = constructor_index;
8105 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8107 if (TYPE_DOMAIN (constructor_type))
8109 constructor_max_index
8110 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8112 /* Detect non-empty initializations of zero-length arrays. */
8113 if (constructor_max_index == NULL_TREE
8114 && TYPE_SIZE (constructor_type))
8115 constructor_max_index = integer_minus_one_node;
8117 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8118 to initialize VLAs will cause a proper error; avoid tree
8119 checking errors as well by setting a safe value. */
8120 if (constructor_max_index
8121 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8122 constructor_max_index = integer_minus_one_node;
8124 constructor_index
8125 = convert (bitsizetype,
8126 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8128 else
8129 constructor_index = bitsize_zero_node;
8131 constructor_unfilled_index = constructor_index;
8132 if (value && TREE_CODE (value) == STRING_CST)
8134 /* We need to split the char/wchar array into individual
8135 characters, so that we don't have to special case it
8136 everywhere. */
8137 set_nonincremental_init_from_string (value, braced_init_obstack);
8140 else
8142 if (constructor_type != error_mark_node)
8143 warning_init (input_location, 0, "braces around scalar initializer");
8144 constructor_fields = constructor_type;
8145 constructor_unfilled_fields = constructor_type;
8149 /* At the end of an implicit or explicit brace level,
8150 finish up that level of constructor. If a single expression
8151 with redundant braces initialized that level, return the
8152 c_expr structure for that expression. Otherwise, the original_code
8153 element is set to ERROR_MARK.
8154 If we were outputting the elements as they are read, return 0 as the value
8155 from inner levels (process_init_element ignores that),
8156 but return error_mark_node as the value from the outermost level
8157 (that's what we want to put in DECL_INITIAL).
8158 Otherwise, return a CONSTRUCTOR expression as the value. */
8160 struct c_expr
8161 pop_init_level (location_t loc, int implicit,
8162 struct obstack *braced_init_obstack,
8163 location_t insert_before)
8165 struct constructor_stack *p;
8166 struct c_expr ret;
8167 ret.value = NULL_TREE;
8168 ret.original_code = ERROR_MARK;
8169 ret.original_type = NULL;
8171 if (implicit == 0)
8173 /* When we come to an explicit close brace,
8174 pop any inner levels that didn't have explicit braces. */
8175 while (constructor_stack->implicit)
8176 process_init_element (input_location,
8177 pop_init_level (loc, 1, braced_init_obstack,
8178 insert_before),
8179 true, braced_init_obstack);
8180 gcc_assert (!constructor_range_stack);
8182 else
8183 if (initializer_stack->missing_brace_richloc)
8184 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8185 (insert_before, "}");
8187 /* Now output all pending elements. */
8188 constructor_incremental = 1;
8189 output_pending_init_elements (1, braced_init_obstack);
8191 p = constructor_stack;
8193 /* Error for initializing a flexible array member, or a zero-length
8194 array member in an inappropriate context. */
8195 if (constructor_type && constructor_fields
8196 && TREE_CODE (constructor_type) == ARRAY_TYPE
8197 && TYPE_DOMAIN (constructor_type)
8198 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8200 /* Silently discard empty initializations. The parser will
8201 already have pedwarned for empty brackets. */
8202 if (integer_zerop (constructor_unfilled_index))
8203 constructor_type = NULL_TREE;
8204 else
8206 gcc_assert (!TYPE_SIZE (constructor_type));
8208 if (constructor_depth > 2)
8209 error_init (loc, "initialization of flexible array member in a nested context");
8210 else
8211 pedwarn_init (loc, OPT_Wpedantic,
8212 "initialization of a flexible array member");
8214 /* We have already issued an error message for the existence
8215 of a flexible array member not at the end of the structure.
8216 Discard the initializer so that we do not die later. */
8217 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8218 constructor_type = NULL_TREE;
8222 switch (vec_safe_length (constructor_elements))
8224 case 0:
8225 /* Initialization with { } counts as zeroinit. */
8226 constructor_zeroinit = 1;
8227 break;
8228 case 1:
8229 /* This might be zeroinit as well. */
8230 if (integer_zerop ((*constructor_elements)[0].value))
8231 constructor_zeroinit = 1;
8232 break;
8233 default:
8234 /* If the constructor has more than one element, it can't be { 0 }. */
8235 constructor_zeroinit = 0;
8236 break;
8239 /* Warn when some structs are initialized with direct aggregation. */
8240 if (!implicit && found_missing_braces && warn_missing_braces
8241 && !constructor_zeroinit)
8243 gcc_assert (initializer_stack->missing_brace_richloc);
8244 warning_at (initializer_stack->missing_brace_richloc,
8245 OPT_Wmissing_braces,
8246 "missing braces around initializer");
8249 /* Warn when some struct elements are implicitly initialized to zero. */
8250 if (warn_missing_field_initializers
8251 && constructor_type
8252 && TREE_CODE (constructor_type) == RECORD_TYPE
8253 && constructor_unfilled_fields)
8255 /* Do not warn for flexible array members or zero-length arrays. */
8256 while (constructor_unfilled_fields
8257 && (!DECL_SIZE (constructor_unfilled_fields)
8258 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8259 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8261 if (constructor_unfilled_fields
8262 /* Do not warn if this level of the initializer uses member
8263 designators; it is likely to be deliberate. */
8264 && !constructor_designated
8265 /* Do not warn about initializing with { 0 } or with { }. */
8266 && !constructor_zeroinit)
8268 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8269 "missing initializer for field %qD of %qT",
8270 constructor_unfilled_fields,
8271 constructor_type))
8272 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8273 "%qD declared here", constructor_unfilled_fields);
8277 /* Pad out the end of the structure. */
8278 if (p->replacement_value.value)
8279 /* If this closes a superfluous brace pair,
8280 just pass out the element between them. */
8281 ret = p->replacement_value;
8282 else if (constructor_type == NULL_TREE)
8284 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8285 && TREE_CODE (constructor_type) != ARRAY_TYPE
8286 && !VECTOR_TYPE_P (constructor_type))
8288 /* A nonincremental scalar initializer--just return
8289 the element, after verifying there is just one. */
8290 if (vec_safe_is_empty (constructor_elements))
8292 if (!constructor_erroneous)
8293 error_init (loc, "empty scalar initializer");
8294 ret.value = error_mark_node;
8296 else if (vec_safe_length (constructor_elements) != 1)
8298 error_init (loc, "extra elements in scalar initializer");
8299 ret.value = (*constructor_elements)[0].value;
8301 else
8302 ret.value = (*constructor_elements)[0].value;
8304 else
8306 if (constructor_erroneous)
8307 ret.value = error_mark_node;
8308 else
8310 ret.value = build_constructor (constructor_type,
8311 constructor_elements);
8312 if (constructor_constant)
8313 TREE_CONSTANT (ret.value) = 1;
8314 if (constructor_constant && constructor_simple)
8315 TREE_STATIC (ret.value) = 1;
8316 if (constructor_nonconst)
8317 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8321 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8323 if (constructor_nonconst)
8324 ret.original_code = C_MAYBE_CONST_EXPR;
8325 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8326 ret.original_code = ERROR_MARK;
8329 constructor_type = p->type;
8330 constructor_fields = p->fields;
8331 constructor_index = p->index;
8332 constructor_max_index = p->max_index;
8333 constructor_unfilled_index = p->unfilled_index;
8334 constructor_unfilled_fields = p->unfilled_fields;
8335 constructor_bit_index = p->bit_index;
8336 constructor_elements = p->elements;
8337 constructor_constant = p->constant;
8338 constructor_simple = p->simple;
8339 constructor_nonconst = p->nonconst;
8340 constructor_erroneous = p->erroneous;
8341 constructor_incremental = p->incremental;
8342 constructor_designated = p->designated;
8343 designator_depth = p->designator_depth;
8344 constructor_pending_elts = p->pending_elts;
8345 constructor_depth = p->depth;
8346 if (!p->implicit)
8347 constructor_range_stack = p->range_stack;
8348 RESTORE_SPELLING_DEPTH (constructor_depth);
8350 constructor_stack = p->next;
8351 free (p);
8353 if (ret.value == NULL_TREE && constructor_stack == 0)
8354 ret.value = error_mark_node;
8355 return ret;
8358 /* Common handling for both array range and field name designators.
8359 ARRAY argument is nonzero for array ranges. Returns false for success. */
8361 static bool
8362 set_designator (location_t loc, bool array,
8363 struct obstack *braced_init_obstack)
8365 tree subtype;
8366 enum tree_code subcode;
8368 /* Don't die if an entire brace-pair level is superfluous
8369 in the containing level. */
8370 if (constructor_type == NULL_TREE)
8371 return true;
8373 /* If there were errors in this designator list already, bail out
8374 silently. */
8375 if (designator_erroneous)
8376 return true;
8378 if (!designator_depth)
8380 gcc_assert (!constructor_range_stack);
8382 /* Designator list starts at the level of closest explicit
8383 braces. */
8384 while (constructor_stack->implicit)
8385 process_init_element (input_location,
8386 pop_init_level (loc, 1, braced_init_obstack,
8387 last_init_list_comma),
8388 true, braced_init_obstack);
8389 constructor_designated = 1;
8390 return false;
8393 switch (TREE_CODE (constructor_type))
8395 case RECORD_TYPE:
8396 case UNION_TYPE:
8397 subtype = TREE_TYPE (constructor_fields);
8398 if (subtype != error_mark_node)
8399 subtype = TYPE_MAIN_VARIANT (subtype);
8400 break;
8401 case ARRAY_TYPE:
8402 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8403 break;
8404 default:
8405 gcc_unreachable ();
8408 subcode = TREE_CODE (subtype);
8409 if (array && subcode != ARRAY_TYPE)
8411 error_init (loc, "array index in non-array initializer");
8412 return true;
8414 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8416 error_init (loc, "field name not in record or union initializer");
8417 return true;
8420 constructor_designated = 1;
8421 finish_implicit_inits (loc, braced_init_obstack);
8422 push_init_level (loc, 2, braced_init_obstack);
8423 return false;
8426 /* If there are range designators in designator list, push a new designator
8427 to constructor_range_stack. RANGE_END is end of such stack range or
8428 NULL_TREE if there is no range designator at this level. */
8430 static void
8431 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8433 struct constructor_range_stack *p;
8435 p = (struct constructor_range_stack *)
8436 obstack_alloc (braced_init_obstack,
8437 sizeof (struct constructor_range_stack));
8438 p->prev = constructor_range_stack;
8439 p->next = 0;
8440 p->fields = constructor_fields;
8441 p->range_start = constructor_index;
8442 p->index = constructor_index;
8443 p->stack = constructor_stack;
8444 p->range_end = range_end;
8445 if (constructor_range_stack)
8446 constructor_range_stack->next = p;
8447 constructor_range_stack = p;
8450 /* Within an array initializer, specify the next index to be initialized.
8451 FIRST is that index. If LAST is nonzero, then initialize a range
8452 of indices, running from FIRST through LAST. */
8454 void
8455 set_init_index (location_t loc, tree first, tree last,
8456 struct obstack *braced_init_obstack)
8458 if (set_designator (loc, true, braced_init_obstack))
8459 return;
8461 designator_erroneous = 1;
8463 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8464 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8466 error_init (loc, "array index in initializer not of integer type");
8467 return;
8470 if (TREE_CODE (first) != INTEGER_CST)
8472 first = c_fully_fold (first, false, NULL);
8473 if (TREE_CODE (first) == INTEGER_CST)
8474 pedwarn_init (loc, OPT_Wpedantic,
8475 "array index in initializer is not "
8476 "an integer constant expression");
8479 if (last && TREE_CODE (last) != INTEGER_CST)
8481 last = c_fully_fold (last, false, NULL);
8482 if (TREE_CODE (last) == INTEGER_CST)
8483 pedwarn_init (loc, OPT_Wpedantic,
8484 "array index in initializer is not "
8485 "an integer constant expression");
8488 if (TREE_CODE (first) != INTEGER_CST)
8489 error_init (loc, "nonconstant array index in initializer");
8490 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8491 error_init (loc, "nonconstant array index in initializer");
8492 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8493 error_init (loc, "array index in non-array initializer");
8494 else if (tree_int_cst_sgn (first) == -1)
8495 error_init (loc, "array index in initializer exceeds array bounds");
8496 else if (constructor_max_index
8497 && tree_int_cst_lt (constructor_max_index, first))
8498 error_init (loc, "array index in initializer exceeds array bounds");
8499 else
8501 constant_expression_warning (first);
8502 if (last)
8503 constant_expression_warning (last);
8504 constructor_index = convert (bitsizetype, first);
8505 if (tree_int_cst_lt (constructor_index, first))
8507 constructor_index = copy_node (constructor_index);
8508 TREE_OVERFLOW (constructor_index) = 1;
8511 if (last)
8513 if (tree_int_cst_equal (first, last))
8514 last = NULL_TREE;
8515 else if (tree_int_cst_lt (last, first))
8517 error_init (loc, "empty index range in initializer");
8518 last = NULL_TREE;
8520 else
8522 last = convert (bitsizetype, last);
8523 if (constructor_max_index != NULL_TREE
8524 && tree_int_cst_lt (constructor_max_index, last))
8526 error_init (loc, "array index range in initializer exceeds "
8527 "array bounds");
8528 last = NULL_TREE;
8533 designator_depth++;
8534 designator_erroneous = 0;
8535 if (constructor_range_stack || last)
8536 push_range_stack (last, braced_init_obstack);
8540 /* Within a struct initializer, specify the next field to be initialized. */
8542 void
8543 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8544 struct obstack *braced_init_obstack)
8546 tree field;
8548 if (set_designator (loc, false, braced_init_obstack))
8549 return;
8551 designator_erroneous = 1;
8553 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8555 error_init (loc, "field name not in record or union initializer");
8556 return;
8559 field = lookup_field (constructor_type, fieldname);
8561 if (field == NULL_TREE)
8563 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8564 if (guessed_id)
8566 gcc_rich_location rich_loc (fieldname_loc);
8567 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8568 error_at (&rich_loc,
8569 "%qT has no member named %qE; did you mean %qE?",
8570 constructor_type, fieldname, guessed_id);
8572 else
8573 error_at (fieldname_loc, "%qT has no member named %qE",
8574 constructor_type, fieldname);
8576 else
8579 constructor_fields = TREE_VALUE (field);
8580 designator_depth++;
8581 designator_erroneous = 0;
8582 if (constructor_range_stack)
8583 push_range_stack (NULL_TREE, braced_init_obstack);
8584 field = TREE_CHAIN (field);
8585 if (field)
8587 if (set_designator (loc, false, braced_init_obstack))
8588 return;
8591 while (field != NULL_TREE);
8594 /* Add a new initializer to the tree of pending initializers. PURPOSE
8595 identifies the initializer, either array index or field in a structure.
8596 VALUE is the value of that index or field. If ORIGTYPE is not
8597 NULL_TREE, it is the original type of VALUE.
8599 IMPLICIT is true if value comes from pop_init_level (1),
8600 the new initializer has been merged with the existing one
8601 and thus no warnings should be emitted about overriding an
8602 existing initializer. */
8604 static void
8605 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8606 bool implicit, struct obstack *braced_init_obstack)
8608 struct init_node *p, **q, *r;
8610 q = &constructor_pending_elts;
8611 p = 0;
8613 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8615 while (*q != 0)
8617 p = *q;
8618 if (tree_int_cst_lt (purpose, p->purpose))
8619 q = &p->left;
8620 else if (tree_int_cst_lt (p->purpose, purpose))
8621 q = &p->right;
8622 else
8624 if (!implicit)
8626 if (TREE_SIDE_EFFECTS (p->value))
8627 warning_init (loc, OPT_Woverride_init_side_effects,
8628 "initialized field with side-effects "
8629 "overwritten");
8630 else if (warn_override_init)
8631 warning_init (loc, OPT_Woverride_init,
8632 "initialized field overwritten");
8634 p->value = value;
8635 p->origtype = origtype;
8636 return;
8640 else
8642 tree bitpos;
8644 bitpos = bit_position (purpose);
8645 while (*q != NULL)
8647 p = *q;
8648 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8649 q = &p->left;
8650 else if (p->purpose != purpose)
8651 q = &p->right;
8652 else
8654 if (!implicit)
8656 if (TREE_SIDE_EFFECTS (p->value))
8657 warning_init (loc, OPT_Woverride_init_side_effects,
8658 "initialized field with side-effects "
8659 "overwritten");
8660 else if (warn_override_init)
8661 warning_init (loc, OPT_Woverride_init,
8662 "initialized field overwritten");
8664 p->value = value;
8665 p->origtype = origtype;
8666 return;
8671 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8672 sizeof (struct init_node));
8673 r->purpose = purpose;
8674 r->value = value;
8675 r->origtype = origtype;
8677 *q = r;
8678 r->parent = p;
8679 r->left = 0;
8680 r->right = 0;
8681 r->balance = 0;
8683 while (p)
8685 struct init_node *s;
8687 if (r == p->left)
8689 if (p->balance == 0)
8690 p->balance = -1;
8691 else if (p->balance < 0)
8693 if (r->balance < 0)
8695 /* L rotation. */
8696 p->left = r->right;
8697 if (p->left)
8698 p->left->parent = p;
8699 r->right = p;
8701 p->balance = 0;
8702 r->balance = 0;
8704 s = p->parent;
8705 p->parent = r;
8706 r->parent = s;
8707 if (s)
8709 if (s->left == p)
8710 s->left = r;
8711 else
8712 s->right = r;
8714 else
8715 constructor_pending_elts = r;
8717 else
8719 /* LR rotation. */
8720 struct init_node *t = r->right;
8722 r->right = t->left;
8723 if (r->right)
8724 r->right->parent = r;
8725 t->left = r;
8727 p->left = t->right;
8728 if (p->left)
8729 p->left->parent = p;
8730 t->right = p;
8732 p->balance = t->balance < 0;
8733 r->balance = -(t->balance > 0);
8734 t->balance = 0;
8736 s = p->parent;
8737 p->parent = t;
8738 r->parent = t;
8739 t->parent = s;
8740 if (s)
8742 if (s->left == p)
8743 s->left = t;
8744 else
8745 s->right = t;
8747 else
8748 constructor_pending_elts = t;
8750 break;
8752 else
8754 /* p->balance == +1; growth of left side balances the node. */
8755 p->balance = 0;
8756 break;
8759 else /* r == p->right */
8761 if (p->balance == 0)
8762 /* Growth propagation from right side. */
8763 p->balance++;
8764 else if (p->balance > 0)
8766 if (r->balance > 0)
8768 /* R rotation. */
8769 p->right = r->left;
8770 if (p->right)
8771 p->right->parent = p;
8772 r->left = p;
8774 p->balance = 0;
8775 r->balance = 0;
8777 s = p->parent;
8778 p->parent = r;
8779 r->parent = s;
8780 if (s)
8782 if (s->left == p)
8783 s->left = r;
8784 else
8785 s->right = r;
8787 else
8788 constructor_pending_elts = r;
8790 else /* r->balance == -1 */
8792 /* RL rotation */
8793 struct init_node *t = r->left;
8795 r->left = t->right;
8796 if (r->left)
8797 r->left->parent = r;
8798 t->right = r;
8800 p->right = t->left;
8801 if (p->right)
8802 p->right->parent = p;
8803 t->left = p;
8805 r->balance = (t->balance < 0);
8806 p->balance = -(t->balance > 0);
8807 t->balance = 0;
8809 s = p->parent;
8810 p->parent = t;
8811 r->parent = t;
8812 t->parent = s;
8813 if (s)
8815 if (s->left == p)
8816 s->left = t;
8817 else
8818 s->right = t;
8820 else
8821 constructor_pending_elts = t;
8823 break;
8825 else
8827 /* p->balance == -1; growth of right side balances the node. */
8828 p->balance = 0;
8829 break;
8833 r = p;
8834 p = p->parent;
8838 /* Build AVL tree from a sorted chain. */
8840 static void
8841 set_nonincremental_init (struct obstack * braced_init_obstack)
8843 unsigned HOST_WIDE_INT ix;
8844 tree index, value;
8846 if (TREE_CODE (constructor_type) != RECORD_TYPE
8847 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8848 return;
8850 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8851 add_pending_init (input_location, index, value, NULL_TREE, true,
8852 braced_init_obstack);
8853 constructor_elements = NULL;
8854 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8856 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8857 /* Skip any nameless bit fields at the beginning. */
8858 while (constructor_unfilled_fields != NULL_TREE
8859 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8860 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
8861 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8864 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8866 if (TYPE_DOMAIN (constructor_type))
8867 constructor_unfilled_index
8868 = convert (bitsizetype,
8869 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8870 else
8871 constructor_unfilled_index = bitsize_zero_node;
8873 constructor_incremental = 0;
8876 /* Build AVL tree from a string constant. */
8878 static void
8879 set_nonincremental_init_from_string (tree str,
8880 struct obstack * braced_init_obstack)
8882 tree value, purpose, type;
8883 HOST_WIDE_INT val[2];
8884 const char *p, *end;
8885 int byte, wchar_bytes, charwidth, bitpos;
8887 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8889 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8890 charwidth = TYPE_PRECISION (char_type_node);
8891 gcc_assert ((size_t) wchar_bytes * charwidth
8892 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8893 type = TREE_TYPE (constructor_type);
8894 p = TREE_STRING_POINTER (str);
8895 end = p + TREE_STRING_LENGTH (str);
8897 for (purpose = bitsize_zero_node;
8898 p < end
8899 && !(constructor_max_index
8900 && tree_int_cst_lt (constructor_max_index, purpose));
8901 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8903 if (wchar_bytes == 1)
8905 val[0] = (unsigned char) *p++;
8906 val[1] = 0;
8908 else
8910 val[1] = 0;
8911 val[0] = 0;
8912 for (byte = 0; byte < wchar_bytes; byte++)
8914 if (BYTES_BIG_ENDIAN)
8915 bitpos = (wchar_bytes - byte - 1) * charwidth;
8916 else
8917 bitpos = byte * charwidth;
8918 val[bitpos / HOST_BITS_PER_WIDE_INT]
8919 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8920 << (bitpos % HOST_BITS_PER_WIDE_INT);
8924 if (!TYPE_UNSIGNED (type))
8926 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8927 if (bitpos < HOST_BITS_PER_WIDE_INT)
8929 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
8931 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8932 val[1] = -1;
8935 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8937 if (val[0] < 0)
8938 val[1] = -1;
8940 else if (val[1] & (HOST_WIDE_INT_1
8941 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8942 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8945 value = wide_int_to_tree (type,
8946 wide_int::from_array (val, 2,
8947 HOST_BITS_PER_WIDE_INT * 2));
8948 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8949 braced_init_obstack);
8952 constructor_incremental = 0;
8955 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
8956 not initialized yet. */
8958 static tree
8959 find_init_member (tree field, struct obstack * braced_init_obstack)
8961 struct init_node *p;
8963 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8965 if (constructor_incremental
8966 && tree_int_cst_lt (field, constructor_unfilled_index))
8967 set_nonincremental_init (braced_init_obstack);
8969 p = constructor_pending_elts;
8970 while (p)
8972 if (tree_int_cst_lt (field, p->purpose))
8973 p = p->left;
8974 else if (tree_int_cst_lt (p->purpose, field))
8975 p = p->right;
8976 else
8977 return p->value;
8980 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8982 tree bitpos = bit_position (field);
8984 if (constructor_incremental
8985 && (!constructor_unfilled_fields
8986 || tree_int_cst_lt (bitpos,
8987 bit_position (constructor_unfilled_fields))))
8988 set_nonincremental_init (braced_init_obstack);
8990 p = constructor_pending_elts;
8991 while (p)
8993 if (field == p->purpose)
8994 return p->value;
8995 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8996 p = p->left;
8997 else
8998 p = p->right;
9001 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9003 if (!vec_safe_is_empty (constructor_elements)
9004 && (constructor_elements->last ().index == field))
9005 return constructor_elements->last ().value;
9007 return NULL_TREE;
9010 /* "Output" the next constructor element.
9011 At top level, really output it to assembler code now.
9012 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9013 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9014 TYPE is the data type that the containing data type wants here.
9015 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9016 If VALUE is a string constant, STRICT_STRING is true if it is
9017 unparenthesized or we should not warn here for it being parenthesized.
9018 For other types of VALUE, STRICT_STRING is not used.
9020 PENDING if true means output pending elements that belong
9021 right after this element. (PENDING is normally true;
9022 it is false while outputting pending elements, to avoid recursion.)
9024 IMPLICIT is true if value comes from pop_init_level (1),
9025 the new initializer has been merged with the existing one
9026 and thus no warnings should be emitted about overriding an
9027 existing initializer. */
9029 static void
9030 output_init_element (location_t loc, tree value, tree origtype,
9031 bool strict_string, tree type, tree field, bool pending,
9032 bool implicit, struct obstack * braced_init_obstack)
9034 tree semantic_type = NULL_TREE;
9035 bool maybe_const = true;
9036 bool npc;
9038 if (type == error_mark_node || value == error_mark_node)
9040 constructor_erroneous = 1;
9041 return;
9043 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9044 && (TREE_CODE (value) == STRING_CST
9045 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9046 && !(TREE_CODE (value) == STRING_CST
9047 && TREE_CODE (type) == ARRAY_TYPE
9048 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9049 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9050 TYPE_MAIN_VARIANT (type)))
9051 value = array_to_pointer_conversion (input_location, value);
9053 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9054 && require_constant_value && pending)
9056 /* As an extension, allow initializing objects with static storage
9057 duration with compound literals (which are then treated just as
9058 the brace enclosed list they contain). */
9059 if (flag_isoc99)
9060 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9061 "constant");
9062 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9063 value = DECL_INITIAL (decl);
9066 npc = null_pointer_constant_p (value);
9067 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9069 semantic_type = TREE_TYPE (value);
9070 value = TREE_OPERAND (value, 0);
9072 value = c_fully_fold (value, require_constant_value, &maybe_const);
9074 if (value == error_mark_node)
9075 constructor_erroneous = 1;
9076 else if (!TREE_CONSTANT (value))
9077 constructor_constant = 0;
9078 else if (!initializer_constant_valid_p (value,
9079 TREE_TYPE (value),
9080 AGGREGATE_TYPE_P (constructor_type)
9081 && TYPE_REVERSE_STORAGE_ORDER
9082 (constructor_type))
9083 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9084 && DECL_C_BIT_FIELD (field)
9085 && TREE_CODE (value) != INTEGER_CST))
9086 constructor_simple = 0;
9087 if (!maybe_const)
9088 constructor_nonconst = 1;
9090 /* Digest the initializer and issue any errors about incompatible
9091 types before issuing errors about non-constant initializers. */
9092 tree new_value = value;
9093 if (semantic_type)
9094 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9095 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9096 require_constant_value);
9097 if (new_value == error_mark_node)
9099 constructor_erroneous = 1;
9100 return;
9102 if (require_constant_value || require_constant_elements)
9103 constant_expression_warning (new_value);
9105 /* Proceed to check the constness of the original initializer. */
9106 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9108 if (require_constant_value)
9110 error_init (loc, "initializer element is not constant");
9111 value = error_mark_node;
9113 else if (require_constant_elements)
9114 pedwarn (loc, OPT_Wpedantic,
9115 "initializer element is not computable at load time");
9117 else if (!maybe_const
9118 && (require_constant_value || require_constant_elements))
9119 pedwarn_init (loc, OPT_Wpedantic,
9120 "initializer element is not a constant expression");
9122 /* Issue -Wc++-compat warnings about initializing a bitfield with
9123 enum type. */
9124 if (warn_cxx_compat
9125 && field != NULL_TREE
9126 && TREE_CODE (field) == FIELD_DECL
9127 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9128 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9129 != TYPE_MAIN_VARIANT (type))
9130 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9132 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9133 if (checktype != error_mark_node
9134 && (TYPE_MAIN_VARIANT (checktype)
9135 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9136 warning_init (loc, OPT_Wc___compat,
9137 "enum conversion in initialization is invalid in C++");
9140 /* If this field is empty (and not at the end of structure),
9141 don't do anything other than checking the initializer. */
9142 if (field
9143 && (TREE_TYPE (field) == error_mark_node
9144 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9145 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9146 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9147 || DECL_CHAIN (field)))))
9148 return;
9150 /* Finally, set VALUE to the initializer value digested above. */
9151 value = new_value;
9153 /* If this element doesn't come next in sequence,
9154 put it on constructor_pending_elts. */
9155 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9156 && (!constructor_incremental
9157 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9159 if (constructor_incremental
9160 && tree_int_cst_lt (field, constructor_unfilled_index))
9161 set_nonincremental_init (braced_init_obstack);
9163 add_pending_init (loc, field, value, origtype, implicit,
9164 braced_init_obstack);
9165 return;
9167 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9168 && (!constructor_incremental
9169 || field != constructor_unfilled_fields))
9171 /* We do this for records but not for unions. In a union,
9172 no matter which field is specified, it can be initialized
9173 right away since it starts at the beginning of the union. */
9174 if (constructor_incremental)
9176 if (!constructor_unfilled_fields)
9177 set_nonincremental_init (braced_init_obstack);
9178 else
9180 tree bitpos, unfillpos;
9182 bitpos = bit_position (field);
9183 unfillpos = bit_position (constructor_unfilled_fields);
9185 if (tree_int_cst_lt (bitpos, unfillpos))
9186 set_nonincremental_init (braced_init_obstack);
9190 add_pending_init (loc, field, value, origtype, implicit,
9191 braced_init_obstack);
9192 return;
9194 else if (TREE_CODE (constructor_type) == UNION_TYPE
9195 && !vec_safe_is_empty (constructor_elements))
9197 if (!implicit)
9199 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9200 warning_init (loc, OPT_Woverride_init_side_effects,
9201 "initialized field with side-effects overwritten");
9202 else if (warn_override_init)
9203 warning_init (loc, OPT_Woverride_init,
9204 "initialized field overwritten");
9207 /* We can have just one union field set. */
9208 constructor_elements = NULL;
9211 /* Otherwise, output this element either to
9212 constructor_elements or to the assembler file. */
9214 constructor_elt celt = {field, value};
9215 vec_safe_push (constructor_elements, celt);
9217 /* Advance the variable that indicates sequential elements output. */
9218 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9219 constructor_unfilled_index
9220 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9221 bitsize_one_node);
9222 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9224 constructor_unfilled_fields
9225 = DECL_CHAIN (constructor_unfilled_fields);
9227 /* Skip any nameless bit fields. */
9228 while (constructor_unfilled_fields != NULL_TREE
9229 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9230 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
9231 constructor_unfilled_fields =
9232 DECL_CHAIN (constructor_unfilled_fields);
9234 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9235 constructor_unfilled_fields = NULL_TREE;
9237 /* Now output any pending elements which have become next. */
9238 if (pending)
9239 output_pending_init_elements (0, braced_init_obstack);
9242 /* Output any pending elements which have become next.
9243 As we output elements, constructor_unfilled_{fields,index}
9244 advances, which may cause other elements to become next;
9245 if so, they too are output.
9247 If ALL is 0, we return when there are
9248 no more pending elements to output now.
9250 If ALL is 1, we output space as necessary so that
9251 we can output all the pending elements. */
9252 static void
9253 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9255 struct init_node *elt = constructor_pending_elts;
9256 tree next;
9258 retry:
9260 /* Look through the whole pending tree.
9261 If we find an element that should be output now,
9262 output it. Otherwise, set NEXT to the element
9263 that comes first among those still pending. */
9265 next = NULL_TREE;
9266 while (elt)
9268 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9270 if (tree_int_cst_equal (elt->purpose,
9271 constructor_unfilled_index))
9272 output_init_element (input_location, elt->value, elt->origtype,
9273 true, TREE_TYPE (constructor_type),
9274 constructor_unfilled_index, false, false,
9275 braced_init_obstack);
9276 else if (tree_int_cst_lt (constructor_unfilled_index,
9277 elt->purpose))
9279 /* Advance to the next smaller node. */
9280 if (elt->left)
9281 elt = elt->left;
9282 else
9284 /* We have reached the smallest node bigger than the
9285 current unfilled index. Fill the space first. */
9286 next = elt->purpose;
9287 break;
9290 else
9292 /* Advance to the next bigger node. */
9293 if (elt->right)
9294 elt = elt->right;
9295 else
9297 /* We have reached the biggest node in a subtree. Find
9298 the parent of it, which is the next bigger node. */
9299 while (elt->parent && elt->parent->right == elt)
9300 elt = elt->parent;
9301 elt = elt->parent;
9302 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9303 elt->purpose))
9305 next = elt->purpose;
9306 break;
9311 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9313 tree ctor_unfilled_bitpos, elt_bitpos;
9315 /* If the current record is complete we are done. */
9316 if (constructor_unfilled_fields == NULL_TREE)
9317 break;
9319 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9320 elt_bitpos = bit_position (elt->purpose);
9321 /* We can't compare fields here because there might be empty
9322 fields in between. */
9323 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9325 constructor_unfilled_fields = elt->purpose;
9326 output_init_element (input_location, elt->value, elt->origtype,
9327 true, TREE_TYPE (elt->purpose),
9328 elt->purpose, false, false,
9329 braced_init_obstack);
9331 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9333 /* Advance to the next smaller node. */
9334 if (elt->left)
9335 elt = elt->left;
9336 else
9338 /* We have reached the smallest node bigger than the
9339 current unfilled field. Fill the space first. */
9340 next = elt->purpose;
9341 break;
9344 else
9346 /* Advance to the next bigger node. */
9347 if (elt->right)
9348 elt = elt->right;
9349 else
9351 /* We have reached the biggest node in a subtree. Find
9352 the parent of it, which is the next bigger node. */
9353 while (elt->parent && elt->parent->right == elt)
9354 elt = elt->parent;
9355 elt = elt->parent;
9356 if (elt
9357 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9358 bit_position (elt->purpose))))
9360 next = elt->purpose;
9361 break;
9368 /* Ordinarily return, but not if we want to output all
9369 and there are elements left. */
9370 if (!(all && next != NULL_TREE))
9371 return;
9373 /* If it's not incremental, just skip over the gap, so that after
9374 jumping to retry we will output the next successive element. */
9375 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9376 constructor_unfilled_fields = next;
9377 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9378 constructor_unfilled_index = next;
9380 /* ELT now points to the node in the pending tree with the next
9381 initializer to output. */
9382 goto retry;
9385 /* Add one non-braced element to the current constructor level.
9386 This adjusts the current position within the constructor's type.
9387 This may also start or terminate implicit levels
9388 to handle a partly-braced initializer.
9390 Once this has found the correct level for the new element,
9391 it calls output_init_element.
9393 IMPLICIT is true if value comes from pop_init_level (1),
9394 the new initializer has been merged with the existing one
9395 and thus no warnings should be emitted about overriding an
9396 existing initializer. */
9398 void
9399 process_init_element (location_t loc, struct c_expr value, bool implicit,
9400 struct obstack * braced_init_obstack)
9402 tree orig_value = value.value;
9403 int string_flag
9404 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9405 bool strict_string = value.original_code == STRING_CST;
9406 bool was_designated = designator_depth != 0;
9408 designator_depth = 0;
9409 designator_erroneous = 0;
9411 if (!implicit && value.value && !integer_zerop (value.value))
9412 constructor_zeroinit = 0;
9414 /* Handle superfluous braces around string cst as in
9415 char x[] = {"foo"}; */
9416 if (string_flag
9417 && constructor_type
9418 && !was_designated
9419 && TREE_CODE (constructor_type) == ARRAY_TYPE
9420 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9421 && integer_zerop (constructor_unfilled_index))
9423 if (constructor_stack->replacement_value.value)
9424 error_init (loc, "excess elements in char array initializer");
9425 constructor_stack->replacement_value = value;
9426 return;
9429 if (constructor_stack->replacement_value.value != NULL_TREE)
9431 error_init (loc, "excess elements in struct initializer");
9432 return;
9435 /* Ignore elements of a brace group if it is entirely superfluous
9436 and has already been diagnosed. */
9437 if (constructor_type == NULL_TREE)
9438 return;
9440 if (!implicit && warn_designated_init && !was_designated
9441 && TREE_CODE (constructor_type) == RECORD_TYPE
9442 && lookup_attribute ("designated_init",
9443 TYPE_ATTRIBUTES (constructor_type)))
9444 warning_init (loc,
9445 OPT_Wdesignated_init,
9446 "positional initialization of field "
9447 "in %<struct%> declared with %<designated_init%> attribute");
9449 /* If we've exhausted any levels that didn't have braces,
9450 pop them now. */
9451 while (constructor_stack->implicit)
9453 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9454 && constructor_fields == NULL_TREE)
9455 process_init_element (loc,
9456 pop_init_level (loc, 1, braced_init_obstack,
9457 last_init_list_comma),
9458 true, braced_init_obstack);
9459 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9460 || VECTOR_TYPE_P (constructor_type))
9461 && constructor_max_index
9462 && tree_int_cst_lt (constructor_max_index,
9463 constructor_index))
9464 process_init_element (loc,
9465 pop_init_level (loc, 1, braced_init_obstack,
9466 last_init_list_comma),
9467 true, braced_init_obstack);
9468 else
9469 break;
9472 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9473 if (constructor_range_stack)
9475 /* If value is a compound literal and we'll be just using its
9476 content, don't put it into a SAVE_EXPR. */
9477 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9478 || !require_constant_value)
9480 tree semantic_type = NULL_TREE;
9481 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9483 semantic_type = TREE_TYPE (value.value);
9484 value.value = TREE_OPERAND (value.value, 0);
9486 value.value = save_expr (value.value);
9487 if (semantic_type)
9488 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9489 value.value);
9493 while (1)
9495 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9497 tree fieldtype;
9498 enum tree_code fieldcode;
9500 if (constructor_fields == NULL_TREE)
9502 pedwarn_init (loc, 0, "excess elements in struct initializer");
9503 break;
9506 fieldtype = TREE_TYPE (constructor_fields);
9507 if (fieldtype != error_mark_node)
9508 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9509 fieldcode = TREE_CODE (fieldtype);
9511 /* Error for non-static initialization of a flexible array member. */
9512 if (fieldcode == ARRAY_TYPE
9513 && !require_constant_value
9514 && TYPE_SIZE (fieldtype) == NULL_TREE
9515 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9517 error_init (loc, "non-static initialization of a flexible "
9518 "array member");
9519 break;
9522 /* Error for initialization of a flexible array member with
9523 a string constant if the structure is in an array. E.g.:
9524 struct S { int x; char y[]; };
9525 struct S s[] = { { 1, "foo" } };
9526 is invalid. */
9527 if (string_flag
9528 && fieldcode == ARRAY_TYPE
9529 && constructor_depth > 1
9530 && TYPE_SIZE (fieldtype) == NULL_TREE
9531 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9533 bool in_array_p = false;
9534 for (struct constructor_stack *p = constructor_stack;
9535 p && p->type; p = p->next)
9536 if (TREE_CODE (p->type) == ARRAY_TYPE)
9538 in_array_p = true;
9539 break;
9541 if (in_array_p)
9543 error_init (loc, "initialization of flexible array "
9544 "member in a nested context");
9545 break;
9549 /* Accept a string constant to initialize a subarray. */
9550 if (value.value != NULL_TREE
9551 && fieldcode == ARRAY_TYPE
9552 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9553 && string_flag)
9554 value.value = orig_value;
9555 /* Otherwise, if we have come to a subaggregate,
9556 and we don't have an element of its type, push into it. */
9557 else if (value.value != NULL_TREE
9558 && value.value != error_mark_node
9559 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9560 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9561 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9563 push_init_level (loc, 1, braced_init_obstack);
9564 continue;
9567 if (value.value)
9569 push_member_name (constructor_fields);
9570 output_init_element (loc, value.value, value.original_type,
9571 strict_string, fieldtype,
9572 constructor_fields, true, implicit,
9573 braced_init_obstack);
9574 RESTORE_SPELLING_DEPTH (constructor_depth);
9576 else
9577 /* Do the bookkeeping for an element that was
9578 directly output as a constructor. */
9580 /* For a record, keep track of end position of last field. */
9581 if (DECL_SIZE (constructor_fields))
9582 constructor_bit_index
9583 = size_binop_loc (input_location, PLUS_EXPR,
9584 bit_position (constructor_fields),
9585 DECL_SIZE (constructor_fields));
9587 /* If the current field was the first one not yet written out,
9588 it isn't now, so update. */
9589 if (constructor_unfilled_fields == constructor_fields)
9591 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9592 /* Skip any nameless bit fields. */
9593 while (constructor_unfilled_fields != 0
9594 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9595 && DECL_NAME (constructor_unfilled_fields) == 0)
9596 constructor_unfilled_fields =
9597 DECL_CHAIN (constructor_unfilled_fields);
9601 constructor_fields = DECL_CHAIN (constructor_fields);
9602 /* Skip any nameless bit fields at the beginning. */
9603 while (constructor_fields != NULL_TREE
9604 && DECL_C_BIT_FIELD (constructor_fields)
9605 && DECL_NAME (constructor_fields) == NULL_TREE)
9606 constructor_fields = DECL_CHAIN (constructor_fields);
9608 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9610 tree fieldtype;
9611 enum tree_code fieldcode;
9613 if (constructor_fields == NULL_TREE)
9615 pedwarn_init (loc, 0,
9616 "excess elements in union initializer");
9617 break;
9620 fieldtype = TREE_TYPE (constructor_fields);
9621 if (fieldtype != error_mark_node)
9622 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9623 fieldcode = TREE_CODE (fieldtype);
9625 /* Warn that traditional C rejects initialization of unions.
9626 We skip the warning if the value is zero. This is done
9627 under the assumption that the zero initializer in user
9628 code appears conditioned on e.g. __STDC__ to avoid
9629 "missing initializer" warnings and relies on default
9630 initialization to zero in the traditional C case.
9631 We also skip the warning if the initializer is designated,
9632 again on the assumption that this must be conditional on
9633 __STDC__ anyway (and we've already complained about the
9634 member-designator already). */
9635 if (!in_system_header_at (input_location) && !constructor_designated
9636 && !(value.value && (integer_zerop (value.value)
9637 || real_zerop (value.value))))
9638 warning (OPT_Wtraditional, "traditional C rejects initialization "
9639 "of unions");
9641 /* Accept a string constant to initialize a subarray. */
9642 if (value.value != NULL_TREE
9643 && fieldcode == ARRAY_TYPE
9644 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9645 && string_flag)
9646 value.value = orig_value;
9647 /* Otherwise, if we have come to a subaggregate,
9648 and we don't have an element of its type, push into it. */
9649 else if (value.value != NULL_TREE
9650 && value.value != error_mark_node
9651 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9652 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9653 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9655 push_init_level (loc, 1, braced_init_obstack);
9656 continue;
9659 if (value.value)
9661 push_member_name (constructor_fields);
9662 output_init_element (loc, value.value, value.original_type,
9663 strict_string, fieldtype,
9664 constructor_fields, true, implicit,
9665 braced_init_obstack);
9666 RESTORE_SPELLING_DEPTH (constructor_depth);
9668 else
9669 /* Do the bookkeeping for an element that was
9670 directly output as a constructor. */
9672 constructor_bit_index = DECL_SIZE (constructor_fields);
9673 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9676 constructor_fields = NULL_TREE;
9678 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9680 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9681 enum tree_code eltcode = TREE_CODE (elttype);
9683 /* Accept a string constant to initialize a subarray. */
9684 if (value.value != NULL_TREE
9685 && eltcode == ARRAY_TYPE
9686 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9687 && string_flag)
9688 value.value = orig_value;
9689 /* Otherwise, if we have come to a subaggregate,
9690 and we don't have an element of its type, push into it. */
9691 else if (value.value != NULL_TREE
9692 && value.value != error_mark_node
9693 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9694 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9695 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9697 push_init_level (loc, 1, braced_init_obstack);
9698 continue;
9701 if (constructor_max_index != NULL_TREE
9702 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9703 || integer_all_onesp (constructor_max_index)))
9705 pedwarn_init (loc, 0,
9706 "excess elements in array initializer");
9707 break;
9710 /* Now output the actual element. */
9711 if (value.value)
9713 push_array_bounds (tree_to_uhwi (constructor_index));
9714 output_init_element (loc, value.value, value.original_type,
9715 strict_string, elttype,
9716 constructor_index, true, implicit,
9717 braced_init_obstack);
9718 RESTORE_SPELLING_DEPTH (constructor_depth);
9721 constructor_index
9722 = size_binop_loc (input_location, PLUS_EXPR,
9723 constructor_index, bitsize_one_node);
9725 if (!value.value)
9726 /* If we are doing the bookkeeping for an element that was
9727 directly output as a constructor, we must update
9728 constructor_unfilled_index. */
9729 constructor_unfilled_index = constructor_index;
9731 else if (VECTOR_TYPE_P (constructor_type))
9733 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9735 /* Do a basic check of initializer size. Note that vectors
9736 always have a fixed size derived from their type. */
9737 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9739 pedwarn_init (loc, 0,
9740 "excess elements in vector initializer");
9741 break;
9744 /* Now output the actual element. */
9745 if (value.value)
9747 if (TREE_CODE (value.value) == VECTOR_CST)
9748 elttype = TYPE_MAIN_VARIANT (constructor_type);
9749 output_init_element (loc, value.value, value.original_type,
9750 strict_string, elttype,
9751 constructor_index, true, implicit,
9752 braced_init_obstack);
9755 constructor_index
9756 = size_binop_loc (input_location,
9757 PLUS_EXPR, constructor_index, bitsize_one_node);
9759 if (!value.value)
9760 /* If we are doing the bookkeeping for an element that was
9761 directly output as a constructor, we must update
9762 constructor_unfilled_index. */
9763 constructor_unfilled_index = constructor_index;
9766 /* Handle the sole element allowed in a braced initializer
9767 for a scalar variable. */
9768 else if (constructor_type != error_mark_node
9769 && constructor_fields == NULL_TREE)
9771 pedwarn_init (loc, 0,
9772 "excess elements in scalar initializer");
9773 break;
9775 else
9777 if (value.value)
9778 output_init_element (loc, value.value, value.original_type,
9779 strict_string, constructor_type,
9780 NULL_TREE, true, implicit,
9781 braced_init_obstack);
9782 constructor_fields = NULL_TREE;
9785 /* Handle range initializers either at this level or anywhere higher
9786 in the designator stack. */
9787 if (constructor_range_stack)
9789 struct constructor_range_stack *p, *range_stack;
9790 int finish = 0;
9792 range_stack = constructor_range_stack;
9793 constructor_range_stack = 0;
9794 while (constructor_stack != range_stack->stack)
9796 gcc_assert (constructor_stack->implicit);
9797 process_init_element (loc,
9798 pop_init_level (loc, 1,
9799 braced_init_obstack,
9800 last_init_list_comma),
9801 true, braced_init_obstack);
9803 for (p = range_stack;
9804 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9805 p = p->prev)
9807 gcc_assert (constructor_stack->implicit);
9808 process_init_element (loc,
9809 pop_init_level (loc, 1,
9810 braced_init_obstack,
9811 last_init_list_comma),
9812 true, braced_init_obstack);
9815 p->index = size_binop_loc (input_location,
9816 PLUS_EXPR, p->index, bitsize_one_node);
9817 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9818 finish = 1;
9820 while (1)
9822 constructor_index = p->index;
9823 constructor_fields = p->fields;
9824 if (finish && p->range_end && p->index == p->range_start)
9826 finish = 0;
9827 p->prev = 0;
9829 p = p->next;
9830 if (!p)
9831 break;
9832 finish_implicit_inits (loc, braced_init_obstack);
9833 push_init_level (loc, 2, braced_init_obstack);
9834 p->stack = constructor_stack;
9835 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9836 p->index = p->range_start;
9839 if (!finish)
9840 constructor_range_stack = range_stack;
9841 continue;
9844 break;
9847 constructor_range_stack = 0;
9850 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9851 (guaranteed to be 'volatile' or null) and ARGS (represented using
9852 an ASM_EXPR node). */
9853 tree
9854 build_asm_stmt (tree cv_qualifier, tree args)
9856 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9857 ASM_VOLATILE_P (args) = 1;
9858 return add_stmt (args);
9861 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9862 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9863 SIMPLE indicates whether there was anything at all after the
9864 string in the asm expression -- asm("blah") and asm("blah" : )
9865 are subtly different. We use a ASM_EXPR node to represent this. */
9866 tree
9867 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9868 tree clobbers, tree labels, bool simple)
9870 tree tail;
9871 tree args;
9872 int i;
9873 const char *constraint;
9874 const char **oconstraints;
9875 bool allows_mem, allows_reg, is_inout;
9876 int ninputs, noutputs;
9878 ninputs = list_length (inputs);
9879 noutputs = list_length (outputs);
9880 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9882 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9884 /* Remove output conversions that change the type but not the mode. */
9885 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9887 tree output = TREE_VALUE (tail);
9889 output = c_fully_fold (output, false, NULL, true);
9891 /* ??? Really, this should not be here. Users should be using a
9892 proper lvalue, dammit. But there's a long history of using casts
9893 in the output operands. In cases like longlong.h, this becomes a
9894 primitive form of typechecking -- if the cast can be removed, then
9895 the output operand had a type of the proper width; otherwise we'll
9896 get an error. Gross, but ... */
9897 STRIP_NOPS (output);
9899 if (!lvalue_or_else (loc, output, lv_asm))
9900 output = error_mark_node;
9902 if (output != error_mark_node
9903 && (TREE_READONLY (output)
9904 || TYPE_READONLY (TREE_TYPE (output))
9905 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9906 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9907 readonly_error (loc, output, lv_asm);
9909 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9910 oconstraints[i] = constraint;
9912 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9913 &allows_mem, &allows_reg, &is_inout))
9915 /* If the operand is going to end up in memory,
9916 mark it addressable. */
9917 if (!allows_reg && !c_mark_addressable (output))
9918 output = error_mark_node;
9919 if (!(!allows_reg && allows_mem)
9920 && output != error_mark_node
9921 && VOID_TYPE_P (TREE_TYPE (output)))
9923 error_at (loc, "invalid use of void expression");
9924 output = error_mark_node;
9927 else
9928 output = error_mark_node;
9930 TREE_VALUE (tail) = output;
9933 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9935 tree input;
9937 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9938 input = TREE_VALUE (tail);
9940 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9941 oconstraints, &allows_mem, &allows_reg))
9943 /* If the operand is going to end up in memory,
9944 mark it addressable. */
9945 if (!allows_reg && allows_mem)
9947 input = c_fully_fold (input, false, NULL, true);
9949 /* Strip the nops as we allow this case. FIXME, this really
9950 should be rejected or made deprecated. */
9951 STRIP_NOPS (input);
9952 if (!c_mark_addressable (input))
9953 input = error_mark_node;
9955 else
9957 struct c_expr expr;
9958 memset (&expr, 0, sizeof (expr));
9959 expr.value = input;
9960 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9961 input = c_fully_fold (expr.value, false, NULL);
9963 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9965 error_at (loc, "invalid use of void expression");
9966 input = error_mark_node;
9970 else
9971 input = error_mark_node;
9973 TREE_VALUE (tail) = input;
9976 /* ASMs with labels cannot have outputs. This should have been
9977 enforced by the parser. */
9978 gcc_assert (outputs == NULL || labels == NULL);
9980 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9982 /* asm statements without outputs, including simple ones, are treated
9983 as volatile. */
9984 ASM_INPUT_P (args) = simple;
9985 ASM_VOLATILE_P (args) = (noutputs == 0);
9987 return args;
9990 /* Generate a goto statement to LABEL. LOC is the location of the
9991 GOTO. */
9993 tree
9994 c_finish_goto_label (location_t loc, tree label)
9996 tree decl = lookup_label_for_goto (loc, label);
9997 if (!decl)
9998 return NULL_TREE;
9999 TREE_USED (decl) = 1;
10001 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10002 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10003 SET_EXPR_LOCATION (t, loc);
10004 return add_stmt (t);
10008 /* Generate a computed goto statement to EXPR. LOC is the location of
10009 the GOTO. */
10011 tree
10012 c_finish_goto_ptr (location_t loc, tree expr)
10014 tree t;
10015 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10016 expr = c_fully_fold (expr, false, NULL);
10017 expr = convert (ptr_type_node, expr);
10018 t = build1 (GOTO_EXPR, void_type_node, expr);
10019 SET_EXPR_LOCATION (t, loc);
10020 return add_stmt (t);
10023 /* Generate a C `return' statement. RETVAL is the expression for what
10024 to return, or a null pointer for `return;' with no value. LOC is
10025 the location of the return statement, or the location of the expression,
10026 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10027 is the original type of RETVAL. */
10029 tree
10030 c_finish_return (location_t loc, tree retval, tree origtype)
10032 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10033 bool no_warning = false;
10034 bool npc = false;
10036 /* Use the expansion point to handle cases such as returning NULL
10037 in a function returning void. */
10038 source_location xloc = expansion_point_location_if_in_system_header (loc);
10040 if (TREE_THIS_VOLATILE (current_function_decl))
10041 warning_at (xloc, 0,
10042 "function declared %<noreturn%> has a %<return%> statement");
10044 if (retval)
10046 tree semantic_type = NULL_TREE;
10047 npc = null_pointer_constant_p (retval);
10048 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10050 semantic_type = TREE_TYPE (retval);
10051 retval = TREE_OPERAND (retval, 0);
10053 retval = c_fully_fold (retval, false, NULL);
10054 if (semantic_type)
10055 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10058 if (!retval)
10060 current_function_returns_null = 1;
10061 if ((warn_return_type || flag_isoc99)
10062 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10064 bool warned_here;
10065 if (flag_isoc99)
10066 warned_here = pedwarn
10067 (loc, 0,
10068 "%<return%> with no value, in function returning non-void");
10069 else
10070 warned_here = warning_at
10071 (loc, OPT_Wreturn_type,
10072 "%<return%> with no value, in function returning non-void");
10073 no_warning = true;
10074 if (warned_here)
10075 inform (DECL_SOURCE_LOCATION (current_function_decl),
10076 "declared here");
10079 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10081 current_function_returns_null = 1;
10082 bool warned_here;
10083 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10084 warned_here = pedwarn
10085 (xloc, 0,
10086 "%<return%> with a value, in function returning void");
10087 else
10088 warned_here = pedwarn
10089 (xloc, OPT_Wpedantic, "ISO C forbids "
10090 "%<return%> with expression, in function returning void");
10091 if (warned_here)
10092 inform (DECL_SOURCE_LOCATION (current_function_decl),
10093 "declared here");
10095 else
10097 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10098 retval, origtype, ic_return,
10099 npc, NULL_TREE, NULL_TREE, 0);
10100 tree res = DECL_RESULT (current_function_decl);
10101 tree inner;
10102 bool save;
10104 current_function_returns_value = 1;
10105 if (t == error_mark_node)
10106 return NULL_TREE;
10108 save = in_late_binary_op;
10109 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10110 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10111 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10112 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10113 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10114 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10115 in_late_binary_op = true;
10116 inner = t = convert (TREE_TYPE (res), t);
10117 in_late_binary_op = save;
10119 /* Strip any conversions, additions, and subtractions, and see if
10120 we are returning the address of a local variable. Warn if so. */
10121 while (1)
10123 switch (TREE_CODE (inner))
10125 CASE_CONVERT:
10126 case NON_LVALUE_EXPR:
10127 case PLUS_EXPR:
10128 case POINTER_PLUS_EXPR:
10129 inner = TREE_OPERAND (inner, 0);
10130 continue;
10132 case MINUS_EXPR:
10133 /* If the second operand of the MINUS_EXPR has a pointer
10134 type (or is converted from it), this may be valid, so
10135 don't give a warning. */
10137 tree op1 = TREE_OPERAND (inner, 1);
10139 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10140 && (CONVERT_EXPR_P (op1)
10141 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10142 op1 = TREE_OPERAND (op1, 0);
10144 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10145 break;
10147 inner = TREE_OPERAND (inner, 0);
10148 continue;
10151 case ADDR_EXPR:
10152 inner = TREE_OPERAND (inner, 0);
10154 while (REFERENCE_CLASS_P (inner)
10155 && !INDIRECT_REF_P (inner))
10156 inner = TREE_OPERAND (inner, 0);
10158 if (DECL_P (inner)
10159 && !DECL_EXTERNAL (inner)
10160 && !TREE_STATIC (inner)
10161 && DECL_CONTEXT (inner) == current_function_decl)
10163 if (TREE_CODE (inner) == LABEL_DECL)
10164 warning_at (loc, OPT_Wreturn_local_addr,
10165 "function returns address of label");
10166 else
10168 warning_at (loc, OPT_Wreturn_local_addr,
10169 "function returns address of local variable");
10170 tree zero = build_zero_cst (TREE_TYPE (res));
10171 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10174 break;
10176 default:
10177 break;
10180 break;
10183 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10184 SET_EXPR_LOCATION (retval, loc);
10186 if (warn_sequence_point)
10187 verify_sequence_points (retval);
10190 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10191 TREE_NO_WARNING (ret_stmt) |= no_warning;
10192 return add_stmt (ret_stmt);
10195 struct c_switch {
10196 /* The SWITCH_EXPR being built. */
10197 tree switch_expr;
10199 /* The original type of the testing expression, i.e. before the
10200 default conversion is applied. */
10201 tree orig_type;
10203 /* A splay-tree mapping the low element of a case range to the high
10204 element, or NULL_TREE if there is no high element. Used to
10205 determine whether or not a new case label duplicates an old case
10206 label. We need a tree, rather than simply a hash table, because
10207 of the GNU case range extension. */
10208 splay_tree cases;
10210 /* The bindings at the point of the switch. This is used for
10211 warnings crossing decls when branching to a case label. */
10212 struct c_spot_bindings *bindings;
10214 /* The next node on the stack. */
10215 struct c_switch *next;
10217 /* Remember whether the controlling expression had boolean type
10218 before integer promotions for the sake of -Wswitch-bool. */
10219 bool bool_cond_p;
10221 /* Remember whether there was a case value that is outside the
10222 range of the ORIG_TYPE. */
10223 bool outside_range_p;
10226 /* A stack of the currently active switch statements. The innermost
10227 switch statement is on the top of the stack. There is no need to
10228 mark the stack for garbage collection because it is only active
10229 during the processing of the body of a function, and we never
10230 collect at that point. */
10232 struct c_switch *c_switch_stack;
10234 /* Start a C switch statement, testing expression EXP. Return the new
10235 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10236 SWITCH_COND_LOC is the location of the switch's condition.
10237 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10239 tree
10240 c_start_case (location_t switch_loc,
10241 location_t switch_cond_loc,
10242 tree exp, bool explicit_cast_p)
10244 tree orig_type = error_mark_node;
10245 bool bool_cond_p = false;
10246 struct c_switch *cs;
10248 if (exp != error_mark_node)
10250 orig_type = TREE_TYPE (exp);
10252 if (!INTEGRAL_TYPE_P (orig_type))
10254 if (orig_type != error_mark_node)
10256 error_at (switch_cond_loc, "switch quantity not an integer");
10257 orig_type = error_mark_node;
10259 exp = integer_zero_node;
10261 else
10263 tree type = TYPE_MAIN_VARIANT (orig_type);
10264 tree e = exp;
10266 /* Warn if the condition has boolean value. */
10267 while (TREE_CODE (e) == COMPOUND_EXPR)
10268 e = TREE_OPERAND (e, 1);
10270 if ((TREE_CODE (type) == BOOLEAN_TYPE
10271 || truth_value_p (TREE_CODE (e)))
10272 /* Explicit cast to int suppresses this warning. */
10273 && !(TREE_CODE (type) == INTEGER_TYPE
10274 && explicit_cast_p))
10275 bool_cond_p = true;
10277 if (!in_system_header_at (input_location)
10278 && (type == long_integer_type_node
10279 || type == long_unsigned_type_node))
10280 warning_at (switch_cond_loc,
10281 OPT_Wtraditional, "%<long%> switch expression not "
10282 "converted to %<int%> in ISO C");
10284 exp = c_fully_fold (exp, false, NULL);
10285 exp = default_conversion (exp);
10287 if (warn_sequence_point)
10288 verify_sequence_points (exp);
10292 /* Add this new SWITCH_EXPR to the stack. */
10293 cs = XNEW (struct c_switch);
10294 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
10295 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10296 cs->orig_type = orig_type;
10297 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10298 cs->bindings = c_get_switch_bindings ();
10299 cs->bool_cond_p = bool_cond_p;
10300 cs->outside_range_p = false;
10301 cs->next = c_switch_stack;
10302 c_switch_stack = cs;
10304 return add_stmt (cs->switch_expr);
10307 /* Process a case label at location LOC. */
10309 tree
10310 do_case (location_t loc, tree low_value, tree high_value)
10312 tree label = NULL_TREE;
10314 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10316 low_value = c_fully_fold (low_value, false, NULL);
10317 if (TREE_CODE (low_value) == INTEGER_CST)
10318 pedwarn (loc, OPT_Wpedantic,
10319 "case label is not an integer constant expression");
10322 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10324 high_value = c_fully_fold (high_value, false, NULL);
10325 if (TREE_CODE (high_value) == INTEGER_CST)
10326 pedwarn (input_location, OPT_Wpedantic,
10327 "case label is not an integer constant expression");
10330 if (c_switch_stack == NULL)
10332 if (low_value)
10333 error_at (loc, "case label not within a switch statement");
10334 else
10335 error_at (loc, "%<default%> label not within a switch statement");
10336 return NULL_TREE;
10339 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10340 EXPR_LOCATION (c_switch_stack->switch_expr),
10341 loc))
10342 return NULL_TREE;
10344 label = c_add_case_label (loc, c_switch_stack->cases,
10345 SWITCH_COND (c_switch_stack->switch_expr),
10346 c_switch_stack->orig_type,
10347 low_value, high_value,
10348 &c_switch_stack->outside_range_p);
10349 if (label == error_mark_node)
10350 label = NULL_TREE;
10351 return label;
10354 /* Finish the switch statement. TYPE is the original type of the
10355 controlling expression of the switch, or NULL_TREE. */
10357 void
10358 c_finish_case (tree body, tree type)
10360 struct c_switch *cs = c_switch_stack;
10361 location_t switch_location;
10363 SWITCH_BODY (cs->switch_expr) = body;
10365 /* Emit warnings as needed. */
10366 switch_location = EXPR_LOCATION (cs->switch_expr);
10367 c_do_switch_warnings (cs->cases, switch_location,
10368 type ? type : TREE_TYPE (cs->switch_expr),
10369 SWITCH_COND (cs->switch_expr),
10370 cs->bool_cond_p, cs->outside_range_p);
10371 if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10372 SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
10374 /* Pop the stack. */
10375 c_switch_stack = cs->next;
10376 splay_tree_delete (cs->cases);
10377 c_release_switch_bindings (cs->bindings);
10378 XDELETE (cs);
10381 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10382 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10383 may be null. */
10385 void
10386 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10387 tree else_block)
10389 tree stmt;
10391 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10392 SET_EXPR_LOCATION (stmt, if_locus);
10393 add_stmt (stmt);
10396 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10397 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10398 is false for DO loops. INCR is the FOR increment expression. BODY is
10399 the statement controlled by the loop. BLAB is the break label. CLAB is
10400 the continue label. Everything is allowed to be NULL. */
10402 void
10403 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10404 tree blab, tree clab, bool cond_is_first)
10406 tree entry = NULL, exit = NULL, t;
10408 /* If the condition is zero don't generate a loop construct. */
10409 if (cond && integer_zerop (cond))
10411 if (cond_is_first)
10413 t = build_and_jump (&blab);
10414 SET_EXPR_LOCATION (t, start_locus);
10415 add_stmt (t);
10418 else
10420 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10422 /* If we have an exit condition, then we build an IF with gotos either
10423 out of the loop, or to the top of it. If there's no exit condition,
10424 then we just build a jump back to the top. */
10425 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10427 if (cond && !integer_nonzerop (cond))
10429 /* Canonicalize the loop condition to the end. This means
10430 generating a branch to the loop condition. Reuse the
10431 continue label, if possible. */
10432 if (cond_is_first)
10434 if (incr || !clab)
10436 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10437 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10439 else
10440 t = build1 (GOTO_EXPR, void_type_node, clab);
10441 SET_EXPR_LOCATION (t, start_locus);
10442 add_stmt (t);
10445 t = build_and_jump (&blab);
10446 if (cond_is_first)
10447 exit = fold_build3_loc (start_locus,
10448 COND_EXPR, void_type_node, cond, exit, t);
10449 else
10450 exit = fold_build3_loc (input_location,
10451 COND_EXPR, void_type_node, cond, exit, t);
10453 else
10455 /* For the backward-goto's location of an unconditional loop
10456 use the beginning of the body, or, if there is none, the
10457 top of the loop. */
10458 location_t loc = EXPR_LOCATION (expr_first (body));
10459 if (loc == UNKNOWN_LOCATION)
10460 loc = start_locus;
10461 SET_EXPR_LOCATION (exit, loc);
10464 add_stmt (top);
10467 if (body)
10468 add_stmt (body);
10469 if (clab)
10470 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10471 if (incr)
10472 add_stmt (incr);
10473 if (entry)
10474 add_stmt (entry);
10475 if (exit)
10476 add_stmt (exit);
10477 if (blab)
10478 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10481 tree
10482 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10484 bool skip;
10485 tree label = *label_p;
10487 /* In switch statements break is sometimes stylistically used after
10488 a return statement. This can lead to spurious warnings about
10489 control reaching the end of a non-void function when it is
10490 inlined. Note that we are calling block_may_fallthru with
10491 language specific tree nodes; this works because
10492 block_may_fallthru returns true when given something it does not
10493 understand. */
10494 skip = !block_may_fallthru (cur_stmt_list);
10496 if (!label)
10498 if (!skip)
10499 *label_p = label = create_artificial_label (loc);
10501 else if (TREE_CODE (label) == LABEL_DECL)
10503 else switch (TREE_INT_CST_LOW (label))
10505 case 0:
10506 if (is_break)
10507 error_at (loc, "break statement not within loop or switch");
10508 else
10509 error_at (loc, "continue statement not within a loop");
10510 return NULL_TREE;
10512 case 1:
10513 gcc_assert (is_break);
10514 error_at (loc, "break statement used with OpenMP for loop");
10515 return NULL_TREE;
10517 case 2:
10518 if (is_break)
10519 error ("break statement within %<#pragma simd%> loop body");
10520 else
10521 error ("continue statement within %<#pragma simd%> loop body");
10522 return NULL_TREE;
10524 default:
10525 gcc_unreachable ();
10528 if (skip)
10529 return NULL_TREE;
10531 if (!is_break)
10532 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10534 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10537 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10539 static void
10540 emit_side_effect_warnings (location_t loc, tree expr)
10542 if (expr == error_mark_node)
10544 else if (!TREE_SIDE_EFFECTS (expr))
10546 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10547 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10549 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10551 tree r = expr;
10552 location_t cloc = loc;
10553 while (TREE_CODE (r) == COMPOUND_EXPR)
10555 if (EXPR_HAS_LOCATION (r))
10556 cloc = EXPR_LOCATION (r);
10557 r = TREE_OPERAND (r, 1);
10559 if (!TREE_SIDE_EFFECTS (r)
10560 && !VOID_TYPE_P (TREE_TYPE (r))
10561 && !CONVERT_EXPR_P (r)
10562 && !TREE_NO_WARNING (r)
10563 && !TREE_NO_WARNING (expr))
10564 warning_at (cloc, OPT_Wunused_value,
10565 "right-hand operand of comma expression has no effect");
10567 else
10568 warn_if_unused_value (expr, loc);
10571 /* Process an expression as if it were a complete statement. Emit
10572 diagnostics, but do not call ADD_STMT. LOC is the location of the
10573 statement. */
10575 tree
10576 c_process_expr_stmt (location_t loc, tree expr)
10578 tree exprv;
10580 if (!expr)
10581 return NULL_TREE;
10583 expr = c_fully_fold (expr, false, NULL);
10585 if (warn_sequence_point)
10586 verify_sequence_points (expr);
10588 if (TREE_TYPE (expr) != error_mark_node
10589 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10590 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10591 error_at (loc, "expression statement has incomplete type");
10593 /* If we're not processing a statement expression, warn about unused values.
10594 Warnings for statement expressions will be emitted later, once we figure
10595 out which is the result. */
10596 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10597 && warn_unused_value)
10598 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10600 exprv = expr;
10601 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10602 exprv = TREE_OPERAND (exprv, 1);
10603 while (CONVERT_EXPR_P (exprv))
10604 exprv = TREE_OPERAND (exprv, 0);
10605 if (DECL_P (exprv)
10606 || handled_component_p (exprv)
10607 || TREE_CODE (exprv) == ADDR_EXPR)
10608 mark_exp_read (exprv);
10610 /* If the expression is not of a type to which we cannot assign a line
10611 number, wrap the thing in a no-op NOP_EXPR. */
10612 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10614 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10615 SET_EXPR_LOCATION (expr, loc);
10618 return expr;
10621 /* Emit an expression as a statement. LOC is the location of the
10622 expression. */
10624 tree
10625 c_finish_expr_stmt (location_t loc, tree expr)
10627 if (expr)
10628 return add_stmt (c_process_expr_stmt (loc, expr));
10629 else
10630 return NULL;
10633 /* Do the opposite and emit a statement as an expression. To begin,
10634 create a new binding level and return it. */
10636 tree
10637 c_begin_stmt_expr (void)
10639 tree ret;
10641 /* We must force a BLOCK for this level so that, if it is not expanded
10642 later, there is a way to turn off the entire subtree of blocks that
10643 are contained in it. */
10644 keep_next_level ();
10645 ret = c_begin_compound_stmt (true);
10647 c_bindings_start_stmt_expr (c_switch_stack == NULL
10648 ? NULL
10649 : c_switch_stack->bindings);
10651 /* Mark the current statement list as belonging to a statement list. */
10652 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10654 return ret;
10657 /* LOC is the location of the compound statement to which this body
10658 belongs. */
10660 tree
10661 c_finish_stmt_expr (location_t loc, tree body)
10663 tree last, type, tmp, val;
10664 tree *last_p;
10666 body = c_end_compound_stmt (loc, body, true);
10668 c_bindings_end_stmt_expr (c_switch_stack == NULL
10669 ? NULL
10670 : c_switch_stack->bindings);
10672 /* Locate the last statement in BODY. See c_end_compound_stmt
10673 about always returning a BIND_EXPR. */
10674 last_p = &BIND_EXPR_BODY (body);
10675 last = BIND_EXPR_BODY (body);
10677 continue_searching:
10678 if (TREE_CODE (last) == STATEMENT_LIST)
10680 tree_stmt_iterator i;
10682 /* This can happen with degenerate cases like ({ }). No value. */
10683 if (!TREE_SIDE_EFFECTS (last))
10684 return body;
10686 /* If we're supposed to generate side effects warnings, process
10687 all of the statements except the last. */
10688 if (warn_unused_value)
10690 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10692 location_t tloc;
10693 tree t = tsi_stmt (i);
10695 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10696 emit_side_effect_warnings (tloc, t);
10699 else
10700 i = tsi_last (last);
10701 last_p = tsi_stmt_ptr (i);
10702 last = *last_p;
10705 /* If the end of the list is exception related, then the list was split
10706 by a call to push_cleanup. Continue searching. */
10707 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10708 || TREE_CODE (last) == TRY_CATCH_EXPR)
10710 last_p = &TREE_OPERAND (last, 0);
10711 last = *last_p;
10712 goto continue_searching;
10715 if (last == error_mark_node)
10716 return last;
10718 /* In the case that the BIND_EXPR is not necessary, return the
10719 expression out from inside it. */
10720 if (last == BIND_EXPR_BODY (body)
10721 && BIND_EXPR_VARS (body) == NULL)
10723 /* Even if this looks constant, do not allow it in a constant
10724 expression. */
10725 last = c_wrap_maybe_const (last, true);
10726 /* Do not warn if the return value of a statement expression is
10727 unused. */
10728 TREE_NO_WARNING (last) = 1;
10729 return last;
10732 /* Extract the type of said expression. */
10733 type = TREE_TYPE (last);
10735 /* If we're not returning a value at all, then the BIND_EXPR that
10736 we already have is a fine expression to return. */
10737 if (!type || VOID_TYPE_P (type))
10738 return body;
10740 /* Now that we've located the expression containing the value, it seems
10741 silly to make voidify_wrapper_expr repeat the process. Create a
10742 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10743 tmp = create_tmp_var_raw (type);
10745 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10746 tree_expr_nonnegative_p giving up immediately. */
10747 val = last;
10748 if (TREE_CODE (val) == NOP_EXPR
10749 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10750 val = TREE_OPERAND (val, 0);
10752 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10753 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10756 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10757 SET_EXPR_LOCATION (t, loc);
10758 return t;
10762 /* Begin and end compound statements. This is as simple as pushing
10763 and popping new statement lists from the tree. */
10765 tree
10766 c_begin_compound_stmt (bool do_scope)
10768 tree stmt = push_stmt_list ();
10769 if (do_scope)
10770 push_scope ();
10771 return stmt;
10774 /* End a compound statement. STMT is the statement. LOC is the
10775 location of the compound statement-- this is usually the location
10776 of the opening brace. */
10778 tree
10779 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10781 tree block = NULL;
10783 if (do_scope)
10785 if (c_dialect_objc ())
10786 objc_clear_super_receiver ();
10787 block = pop_scope ();
10790 stmt = pop_stmt_list (stmt);
10791 stmt = c_build_bind_expr (loc, block, stmt);
10793 /* If this compound statement is nested immediately inside a statement
10794 expression, then force a BIND_EXPR to be created. Otherwise we'll
10795 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10796 STATEMENT_LISTs merge, and thus we can lose track of what statement
10797 was really last. */
10798 if (building_stmt_list_p ()
10799 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10800 && TREE_CODE (stmt) != BIND_EXPR)
10802 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10803 TREE_SIDE_EFFECTS (stmt) = 1;
10804 SET_EXPR_LOCATION (stmt, loc);
10807 return stmt;
10810 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10811 when the current scope is exited. EH_ONLY is true when this is not
10812 meant to apply to normal control flow transfer. */
10814 void
10815 push_cleanup (tree decl, tree cleanup, bool eh_only)
10817 enum tree_code code;
10818 tree stmt, list;
10819 bool stmt_expr;
10821 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10822 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10823 add_stmt (stmt);
10824 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10825 list = push_stmt_list ();
10826 TREE_OPERAND (stmt, 0) = list;
10827 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10830 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10831 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10833 static tree
10834 build_vec_cmp (tree_code code, tree type,
10835 tree arg0, tree arg1)
10837 tree zero_vec = build_zero_cst (type);
10838 tree minus_one_vec = build_minus_one_cst (type);
10839 tree cmp_type = build_same_sized_truth_vector_type (type);
10840 tree cmp = build2 (code, cmp_type, arg0, arg1);
10841 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10844 /* Build a binary-operation expression without default conversions.
10845 CODE is the kind of expression to build.
10846 LOCATION is the operator's location.
10847 This function differs from `build' in several ways:
10848 the data type of the result is computed and recorded in it,
10849 warnings are generated if arg data types are invalid,
10850 special handling for addition and subtraction of pointers is known,
10851 and some optimization is done (operations on narrow ints
10852 are done in the narrower type when that gives the same result).
10853 Constant folding is also done before the result is returned.
10855 Note that the operands will never have enumeral types, or function
10856 or array types, because either they will have the default conversions
10857 performed or they have both just been converted to some other type in which
10858 the arithmetic is to be done. */
10860 tree
10861 build_binary_op (location_t location, enum tree_code code,
10862 tree orig_op0, tree orig_op1, bool convert_p)
10864 tree type0, type1, orig_type0, orig_type1;
10865 tree eptype;
10866 enum tree_code code0, code1;
10867 tree op0, op1;
10868 tree ret = error_mark_node;
10869 const char *invalid_op_diag;
10870 bool op0_int_operands, op1_int_operands;
10871 bool int_const, int_const_or_overflow, int_operands;
10873 /* Expression code to give to the expression when it is built.
10874 Normally this is CODE, which is what the caller asked for,
10875 but in some special cases we change it. */
10876 enum tree_code resultcode = code;
10878 /* Data type in which the computation is to be performed.
10879 In the simplest cases this is the common type of the arguments. */
10880 tree result_type = NULL;
10882 /* When the computation is in excess precision, the type of the
10883 final EXCESS_PRECISION_EXPR. */
10884 tree semantic_result_type = NULL;
10886 /* Nonzero means operands have already been type-converted
10887 in whatever way is necessary.
10888 Zero means they need to be converted to RESULT_TYPE. */
10889 int converted = 0;
10891 /* Nonzero means create the expression with this type, rather than
10892 RESULT_TYPE. */
10893 tree build_type = NULL_TREE;
10895 /* Nonzero means after finally constructing the expression
10896 convert it to this type. */
10897 tree final_type = NULL_TREE;
10899 /* Nonzero if this is an operation like MIN or MAX which can
10900 safely be computed in short if both args are promoted shorts.
10901 Also implies COMMON.
10902 -1 indicates a bitwise operation; this makes a difference
10903 in the exact conditions for when it is safe to do the operation
10904 in a narrower mode. */
10905 int shorten = 0;
10907 /* Nonzero if this is a comparison operation;
10908 if both args are promoted shorts, compare the original shorts.
10909 Also implies COMMON. */
10910 int short_compare = 0;
10912 /* Nonzero if this is a right-shift operation, which can be computed on the
10913 original short and then promoted if the operand is a promoted short. */
10914 int short_shift = 0;
10916 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10917 int common = 0;
10919 /* True means types are compatible as far as ObjC is concerned. */
10920 bool objc_ok;
10922 /* True means this is an arithmetic operation that may need excess
10923 precision. */
10924 bool may_need_excess_precision;
10926 /* True means this is a boolean operation that converts both its
10927 operands to truth-values. */
10928 bool boolean_op = false;
10930 /* Remember whether we're doing / or %. */
10931 bool doing_div_or_mod = false;
10933 /* Remember whether we're doing << or >>. */
10934 bool doing_shift = false;
10936 /* Tree holding instrumentation expression. */
10937 tree instrument_expr = NULL;
10939 if (location == UNKNOWN_LOCATION)
10940 location = input_location;
10942 op0 = orig_op0;
10943 op1 = orig_op1;
10945 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10946 if (op0_int_operands)
10947 op0 = remove_c_maybe_const_expr (op0);
10948 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10949 if (op1_int_operands)
10950 op1 = remove_c_maybe_const_expr (op1);
10951 int_operands = (op0_int_operands && op1_int_operands);
10952 if (int_operands)
10954 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10955 && TREE_CODE (orig_op1) == INTEGER_CST);
10956 int_const = (int_const_or_overflow
10957 && !TREE_OVERFLOW (orig_op0)
10958 && !TREE_OVERFLOW (orig_op1));
10960 else
10961 int_const = int_const_or_overflow = false;
10963 /* Do not apply default conversion in mixed vector/scalar expression. */
10964 if (convert_p
10965 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10967 op0 = default_conversion (op0);
10968 op1 = default_conversion (op1);
10971 orig_type0 = type0 = TREE_TYPE (op0);
10973 orig_type1 = type1 = TREE_TYPE (op1);
10975 /* The expression codes of the data types of the arguments tell us
10976 whether the arguments are integers, floating, pointers, etc. */
10977 code0 = TREE_CODE (type0);
10978 code1 = TREE_CODE (type1);
10980 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10981 STRIP_TYPE_NOPS (op0);
10982 STRIP_TYPE_NOPS (op1);
10984 /* If an error was already reported for one of the arguments,
10985 avoid reporting another error. */
10987 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10988 return error_mark_node;
10990 if (code0 == POINTER_TYPE
10991 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10992 return error_mark_node;
10994 if (code1 == POINTER_TYPE
10995 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10996 return error_mark_node;
10998 if ((invalid_op_diag
10999 = targetm.invalid_binary_op (code, type0, type1)))
11001 error_at (location, invalid_op_diag);
11002 return error_mark_node;
11005 switch (code)
11007 case PLUS_EXPR:
11008 case MINUS_EXPR:
11009 case MULT_EXPR:
11010 case TRUNC_DIV_EXPR:
11011 case CEIL_DIV_EXPR:
11012 case FLOOR_DIV_EXPR:
11013 case ROUND_DIV_EXPR:
11014 case EXACT_DIV_EXPR:
11015 may_need_excess_precision = true;
11016 break;
11017 default:
11018 may_need_excess_precision = false;
11019 break;
11021 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11023 op0 = TREE_OPERAND (op0, 0);
11024 type0 = TREE_TYPE (op0);
11026 else if (may_need_excess_precision
11027 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11029 type0 = eptype;
11030 op0 = convert (eptype, op0);
11032 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11034 op1 = TREE_OPERAND (op1, 0);
11035 type1 = TREE_TYPE (op1);
11037 else if (may_need_excess_precision
11038 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11040 type1 = eptype;
11041 op1 = convert (eptype, op1);
11044 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11046 /* In case when one of the operands of the binary operation is
11047 a vector and another is a scalar -- convert scalar to vector. */
11048 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11050 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11051 true);
11053 switch (convert_flag)
11055 case stv_error:
11056 return error_mark_node;
11057 case stv_firstarg:
11059 bool maybe_const = true;
11060 tree sc;
11061 sc = c_fully_fold (op0, false, &maybe_const);
11062 sc = save_expr (sc);
11063 sc = convert (TREE_TYPE (type1), sc);
11064 op0 = build_vector_from_val (type1, sc);
11065 if (!maybe_const)
11066 op0 = c_wrap_maybe_const (op0, true);
11067 orig_type0 = type0 = TREE_TYPE (op0);
11068 code0 = TREE_CODE (type0);
11069 converted = 1;
11070 break;
11072 case stv_secondarg:
11074 bool maybe_const = true;
11075 tree sc;
11076 sc = c_fully_fold (op1, false, &maybe_const);
11077 sc = save_expr (sc);
11078 sc = convert (TREE_TYPE (type0), sc);
11079 op1 = build_vector_from_val (type0, sc);
11080 if (!maybe_const)
11081 op1 = c_wrap_maybe_const (op1, true);
11082 orig_type1 = type1 = TREE_TYPE (op1);
11083 code1 = TREE_CODE (type1);
11084 converted = 1;
11085 break;
11087 default:
11088 break;
11092 switch (code)
11094 case PLUS_EXPR:
11095 /* Handle the pointer + int case. */
11096 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11098 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11099 goto return_build_binary_op;
11101 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11103 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11104 goto return_build_binary_op;
11106 else
11107 common = 1;
11108 break;
11110 case MINUS_EXPR:
11111 /* Subtraction of two similar pointers.
11112 We must subtract them as integers, then divide by object size. */
11113 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11114 && comp_target_types (location, type0, type1))
11116 ret = pointer_diff (location, op0, op1);
11117 goto return_build_binary_op;
11119 /* Handle pointer minus int. Just like pointer plus int. */
11120 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11122 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11123 goto return_build_binary_op;
11125 else
11126 common = 1;
11127 break;
11129 case MULT_EXPR:
11130 common = 1;
11131 break;
11133 case TRUNC_DIV_EXPR:
11134 case CEIL_DIV_EXPR:
11135 case FLOOR_DIV_EXPR:
11136 case ROUND_DIV_EXPR:
11137 case EXACT_DIV_EXPR:
11138 doing_div_or_mod = true;
11139 warn_for_div_by_zero (location, op1);
11141 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11142 || code0 == FIXED_POINT_TYPE
11143 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11144 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11145 || code1 == FIXED_POINT_TYPE
11146 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11148 enum tree_code tcode0 = code0, tcode1 = code1;
11150 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11151 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11152 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11153 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11155 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11156 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11157 resultcode = RDIV_EXPR;
11158 else
11159 /* Although it would be tempting to shorten always here, that
11160 loses on some targets, since the modulo instruction is
11161 undefined if the quotient can't be represented in the
11162 computation mode. We shorten only if unsigned or if
11163 dividing by something we know != -1. */
11164 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11165 || (TREE_CODE (op1) == INTEGER_CST
11166 && !integer_all_onesp (op1)));
11167 common = 1;
11169 break;
11171 case BIT_AND_EXPR:
11172 case BIT_IOR_EXPR:
11173 case BIT_XOR_EXPR:
11174 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11175 shorten = -1;
11176 /* Allow vector types which are not floating point types. */
11177 else if (code0 == VECTOR_TYPE
11178 && code1 == VECTOR_TYPE
11179 && !VECTOR_FLOAT_TYPE_P (type0)
11180 && !VECTOR_FLOAT_TYPE_P (type1))
11181 common = 1;
11182 break;
11184 case TRUNC_MOD_EXPR:
11185 case FLOOR_MOD_EXPR:
11186 doing_div_or_mod = true;
11187 warn_for_div_by_zero (location, op1);
11189 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11190 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11191 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11192 common = 1;
11193 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11195 /* Although it would be tempting to shorten always here, that loses
11196 on some targets, since the modulo instruction is undefined if the
11197 quotient can't be represented in the computation mode. We shorten
11198 only if unsigned or if dividing by something we know != -1. */
11199 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11200 || (TREE_CODE (op1) == INTEGER_CST
11201 && !integer_all_onesp (op1)));
11202 common = 1;
11204 break;
11206 case TRUTH_ANDIF_EXPR:
11207 case TRUTH_ORIF_EXPR:
11208 case TRUTH_AND_EXPR:
11209 case TRUTH_OR_EXPR:
11210 case TRUTH_XOR_EXPR:
11211 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11212 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11213 || code0 == FIXED_POINT_TYPE)
11214 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11215 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11216 || code1 == FIXED_POINT_TYPE))
11218 /* Result of these operations is always an int,
11219 but that does not mean the operands should be
11220 converted to ints! */
11221 result_type = integer_type_node;
11222 if (op0_int_operands)
11224 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11225 op0 = remove_c_maybe_const_expr (op0);
11227 else
11228 op0 = c_objc_common_truthvalue_conversion (location, op0);
11229 if (op1_int_operands)
11231 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11232 op1 = remove_c_maybe_const_expr (op1);
11234 else
11235 op1 = c_objc_common_truthvalue_conversion (location, op1);
11236 converted = 1;
11237 boolean_op = true;
11239 if (code == TRUTH_ANDIF_EXPR)
11241 int_const_or_overflow = (int_operands
11242 && TREE_CODE (orig_op0) == INTEGER_CST
11243 && (op0 == truthvalue_false_node
11244 || TREE_CODE (orig_op1) == INTEGER_CST));
11245 int_const = (int_const_or_overflow
11246 && !TREE_OVERFLOW (orig_op0)
11247 && (op0 == truthvalue_false_node
11248 || !TREE_OVERFLOW (orig_op1)));
11250 else if (code == TRUTH_ORIF_EXPR)
11252 int_const_or_overflow = (int_operands
11253 && TREE_CODE (orig_op0) == INTEGER_CST
11254 && (op0 == truthvalue_true_node
11255 || TREE_CODE (orig_op1) == INTEGER_CST));
11256 int_const = (int_const_or_overflow
11257 && !TREE_OVERFLOW (orig_op0)
11258 && (op0 == truthvalue_true_node
11259 || !TREE_OVERFLOW (orig_op1)));
11261 break;
11263 /* Shift operations: result has same type as first operand;
11264 always convert second operand to int.
11265 Also set SHORT_SHIFT if shifting rightward. */
11267 case RSHIFT_EXPR:
11268 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11269 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11270 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11271 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11273 result_type = type0;
11274 converted = 1;
11276 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11277 || code0 == VECTOR_TYPE)
11278 && code1 == INTEGER_TYPE)
11280 doing_shift = true;
11281 if (TREE_CODE (op1) == INTEGER_CST)
11283 if (tree_int_cst_sgn (op1) < 0)
11285 int_const = false;
11286 if (c_inhibit_evaluation_warnings == 0)
11287 warning_at (location, OPT_Wshift_count_negative,
11288 "right shift count is negative");
11290 else if (code0 == VECTOR_TYPE)
11292 if (compare_tree_int (op1,
11293 TYPE_PRECISION (TREE_TYPE (type0)))
11294 >= 0)
11296 int_const = false;
11297 if (c_inhibit_evaluation_warnings == 0)
11298 warning_at (location, OPT_Wshift_count_overflow,
11299 "right shift count >= width of vector element");
11302 else
11304 if (!integer_zerop (op1))
11305 short_shift = 1;
11307 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11309 int_const = false;
11310 if (c_inhibit_evaluation_warnings == 0)
11311 warning_at (location, OPT_Wshift_count_overflow,
11312 "right shift count >= width of type");
11317 /* Use the type of the value to be shifted. */
11318 result_type = type0;
11319 /* Avoid converting op1 to result_type later. */
11320 converted = 1;
11322 break;
11324 case LSHIFT_EXPR:
11325 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11326 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11327 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11328 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
11330 result_type = type0;
11331 converted = 1;
11333 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11334 || code0 == VECTOR_TYPE)
11335 && code1 == INTEGER_TYPE)
11337 doing_shift = true;
11338 if (TREE_CODE (op0) == INTEGER_CST
11339 && tree_int_cst_sgn (op0) < 0)
11341 /* Don't reject a left shift of a negative value in a context
11342 where a constant expression is needed in C90. */
11343 if (flag_isoc99)
11344 int_const = false;
11345 if (c_inhibit_evaluation_warnings == 0)
11346 warning_at (location, OPT_Wshift_negative_value,
11347 "left shift of negative value");
11349 if (TREE_CODE (op1) == INTEGER_CST)
11351 if (tree_int_cst_sgn (op1) < 0)
11353 int_const = false;
11354 if (c_inhibit_evaluation_warnings == 0)
11355 warning_at (location, OPT_Wshift_count_negative,
11356 "left shift count is negative");
11358 else if (code0 == VECTOR_TYPE)
11360 if (compare_tree_int (op1,
11361 TYPE_PRECISION (TREE_TYPE (type0)))
11362 >= 0)
11364 int_const = false;
11365 if (c_inhibit_evaluation_warnings == 0)
11366 warning_at (location, OPT_Wshift_count_overflow,
11367 "left shift count >= width of vector element");
11370 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11372 int_const = false;
11373 if (c_inhibit_evaluation_warnings == 0)
11374 warning_at (location, OPT_Wshift_count_overflow,
11375 "left shift count >= width of type");
11377 else if (TREE_CODE (op0) == INTEGER_CST
11378 && maybe_warn_shift_overflow (location, op0, op1)
11379 && flag_isoc99)
11380 int_const = false;
11383 /* Use the type of the value to be shifted. */
11384 result_type = type0;
11385 /* Avoid converting op1 to result_type later. */
11386 converted = 1;
11388 break;
11390 case EQ_EXPR:
11391 case NE_EXPR:
11392 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11394 tree intt;
11395 if (!vector_types_compatible_elements_p (type0, type1))
11397 error_at (location, "comparing vectors with different "
11398 "element types");
11399 return error_mark_node;
11402 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11404 error_at (location, "comparing vectors with different "
11405 "number of elements");
11406 return error_mark_node;
11409 /* It's not precisely specified how the usual arithmetic
11410 conversions apply to the vector types. Here, we use
11411 the unsigned type if one of the operands is signed and
11412 the other one is unsigned. */
11413 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11415 if (!TYPE_UNSIGNED (type0))
11416 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11417 else
11418 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11419 warning_at (location, OPT_Wsign_compare, "comparison between "
11420 "types %qT and %qT", type0, type1);
11423 /* Always construct signed integer vector type. */
11424 intt = c_common_type_for_size (GET_MODE_BITSIZE
11425 (SCALAR_TYPE_MODE
11426 (TREE_TYPE (type0))), 0);
11427 result_type = build_opaque_vector_type (intt,
11428 TYPE_VECTOR_SUBPARTS (type0));
11429 converted = 1;
11430 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11431 goto return_build_binary_op;
11433 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11434 warning_at (location,
11435 OPT_Wfloat_equal,
11436 "comparing floating point with == or != is unsafe");
11437 /* Result of comparison is always int,
11438 but don't convert the args to int! */
11439 build_type = integer_type_node;
11440 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11441 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11442 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11443 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11444 short_compare = 1;
11445 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11447 if (TREE_CODE (op0) == ADDR_EXPR
11448 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11449 && !from_macro_expansion_at (location))
11451 if (code == EQ_EXPR)
11452 warning_at (location,
11453 OPT_Waddress,
11454 "the comparison will always evaluate as %<false%> "
11455 "for the address of %qD will never be NULL",
11456 TREE_OPERAND (op0, 0));
11457 else
11458 warning_at (location,
11459 OPT_Waddress,
11460 "the comparison will always evaluate as %<true%> "
11461 "for the address of %qD will never be NULL",
11462 TREE_OPERAND (op0, 0));
11464 result_type = type0;
11466 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11468 if (TREE_CODE (op1) == ADDR_EXPR
11469 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11470 && !from_macro_expansion_at (location))
11472 if (code == EQ_EXPR)
11473 warning_at (location,
11474 OPT_Waddress,
11475 "the comparison will always evaluate as %<false%> "
11476 "for the address of %qD will never be NULL",
11477 TREE_OPERAND (op1, 0));
11478 else
11479 warning_at (location,
11480 OPT_Waddress,
11481 "the comparison will always evaluate as %<true%> "
11482 "for the address of %qD will never be NULL",
11483 TREE_OPERAND (op1, 0));
11485 result_type = type1;
11487 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11489 tree tt0 = TREE_TYPE (type0);
11490 tree tt1 = TREE_TYPE (type1);
11491 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11492 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11493 addr_space_t as_common = ADDR_SPACE_GENERIC;
11495 /* Anything compares with void *. void * compares with anything.
11496 Otherwise, the targets must be compatible
11497 and both must be object or both incomplete. */
11498 if (comp_target_types (location, type0, type1))
11499 result_type = common_pointer_type (type0, type1);
11500 else if (!addr_space_superset (as0, as1, &as_common))
11502 error_at (location, "comparison of pointers to "
11503 "disjoint address spaces");
11504 return error_mark_node;
11506 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11508 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11509 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11510 "comparison of %<void *%> with function pointer");
11512 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11514 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11515 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11516 "comparison of %<void *%> with function pointer");
11518 else
11519 /* Avoid warning about the volatile ObjC EH puts on decls. */
11520 if (!objc_ok)
11521 pedwarn (location, 0,
11522 "comparison of distinct pointer types lacks a cast");
11524 if (result_type == NULL_TREE)
11526 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11527 result_type = build_pointer_type
11528 (build_qualified_type (void_type_node, qual));
11531 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11533 result_type = type0;
11534 pedwarn (location, 0, "comparison between pointer and integer");
11536 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11538 result_type = type1;
11539 pedwarn (location, 0, "comparison between pointer and integer");
11541 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11542 || truth_value_p (TREE_CODE (orig_op0)))
11543 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11544 || truth_value_p (TREE_CODE (orig_op1))))
11545 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11546 break;
11548 case LE_EXPR:
11549 case GE_EXPR:
11550 case LT_EXPR:
11551 case GT_EXPR:
11552 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11554 tree intt;
11555 if (!vector_types_compatible_elements_p (type0, type1))
11557 error_at (location, "comparing vectors with different "
11558 "element types");
11559 return error_mark_node;
11562 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11564 error_at (location, "comparing vectors with different "
11565 "number of elements");
11566 return error_mark_node;
11569 /* It's not precisely specified how the usual arithmetic
11570 conversions apply to the vector types. Here, we use
11571 the unsigned type if one of the operands is signed and
11572 the other one is unsigned. */
11573 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11575 if (!TYPE_UNSIGNED (type0))
11576 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11577 else
11578 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11579 warning_at (location, OPT_Wsign_compare, "comparison between "
11580 "types %qT and %qT", type0, type1);
11583 /* Always construct signed integer vector type. */
11584 intt = c_common_type_for_size (GET_MODE_BITSIZE
11585 (SCALAR_TYPE_MODE
11586 (TREE_TYPE (type0))), 0);
11587 result_type = build_opaque_vector_type (intt,
11588 TYPE_VECTOR_SUBPARTS (type0));
11589 converted = 1;
11590 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11591 goto return_build_binary_op;
11593 build_type = integer_type_node;
11594 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11595 || code0 == FIXED_POINT_TYPE)
11596 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11597 || code1 == FIXED_POINT_TYPE))
11598 short_compare = 1;
11599 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11601 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11602 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11603 addr_space_t as_common;
11605 if (comp_target_types (location, type0, type1))
11607 result_type = common_pointer_type (type0, type1);
11608 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11609 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11610 pedwarn (location, 0,
11611 "comparison of complete and incomplete pointers");
11612 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11613 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11614 "ordered comparisons of pointers to functions");
11615 else if (null_pointer_constant_p (orig_op0)
11616 || null_pointer_constant_p (orig_op1))
11617 warning_at (location, OPT_Wextra,
11618 "ordered comparison of pointer with null pointer");
11621 else if (!addr_space_superset (as0, as1, &as_common))
11623 error_at (location, "comparison of pointers to "
11624 "disjoint address spaces");
11625 return error_mark_node;
11627 else
11629 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11630 result_type = build_pointer_type
11631 (build_qualified_type (void_type_node, qual));
11632 pedwarn (location, 0,
11633 "comparison of distinct pointer types lacks a cast");
11636 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11638 result_type = type0;
11639 if (pedantic)
11640 pedwarn (location, OPT_Wpedantic,
11641 "ordered comparison of pointer with integer zero");
11642 else if (extra_warnings)
11643 warning_at (location, OPT_Wextra,
11644 "ordered comparison of pointer with integer zero");
11646 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11648 result_type = type1;
11649 if (pedantic)
11650 pedwarn (location, OPT_Wpedantic,
11651 "ordered comparison of pointer with integer zero");
11652 else if (extra_warnings)
11653 warning_at (location, OPT_Wextra,
11654 "ordered comparison of pointer with integer zero");
11656 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11658 result_type = type0;
11659 pedwarn (location, 0, "comparison between pointer and integer");
11661 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11663 result_type = type1;
11664 pedwarn (location, 0, "comparison between pointer and integer");
11666 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11667 || truth_value_p (TREE_CODE (orig_op0)))
11668 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11669 || truth_value_p (TREE_CODE (orig_op1))))
11670 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11671 break;
11673 default:
11674 gcc_unreachable ();
11677 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11678 return error_mark_node;
11680 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11681 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11682 || !vector_types_compatible_elements_p (type0, type1)))
11684 gcc_rich_location richloc (location);
11685 richloc.maybe_add_expr (orig_op0);
11686 richloc.maybe_add_expr (orig_op1);
11687 binary_op_error (&richloc, code, type0, type1);
11688 return error_mark_node;
11691 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11692 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11694 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11695 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11697 bool first_complex = (code0 == COMPLEX_TYPE);
11698 bool second_complex = (code1 == COMPLEX_TYPE);
11699 int none_complex = (!first_complex && !second_complex);
11701 if (shorten || common || short_compare)
11703 result_type = c_common_type (type0, type1);
11704 do_warn_double_promotion (result_type, type0, type1,
11705 "implicit conversion from %qT to %qT "
11706 "to match other operand of binary "
11707 "expression",
11708 location);
11709 if (result_type == error_mark_node)
11710 return error_mark_node;
11713 if (first_complex != second_complex
11714 && (code == PLUS_EXPR
11715 || code == MINUS_EXPR
11716 || code == MULT_EXPR
11717 || (code == TRUNC_DIV_EXPR && first_complex))
11718 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11719 && flag_signed_zeros)
11721 /* An operation on mixed real/complex operands must be
11722 handled specially, but the language-independent code can
11723 more easily optimize the plain complex arithmetic if
11724 -fno-signed-zeros. */
11725 tree real_type = TREE_TYPE (result_type);
11726 tree real, imag;
11727 if (type0 != orig_type0 || type1 != orig_type1)
11729 gcc_assert (may_need_excess_precision && common);
11730 semantic_result_type = c_common_type (orig_type0, orig_type1);
11732 if (first_complex)
11734 if (TREE_TYPE (op0) != result_type)
11735 op0 = convert_and_check (location, result_type, op0);
11736 if (TREE_TYPE (op1) != real_type)
11737 op1 = convert_and_check (location, real_type, op1);
11739 else
11741 if (TREE_TYPE (op0) != real_type)
11742 op0 = convert_and_check (location, real_type, op0);
11743 if (TREE_TYPE (op1) != result_type)
11744 op1 = convert_and_check (location, result_type, op1);
11746 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11747 return error_mark_node;
11748 if (first_complex)
11750 op0 = save_expr (op0);
11751 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11752 op0, true);
11753 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11754 op0, true);
11755 switch (code)
11757 case MULT_EXPR:
11758 case TRUNC_DIV_EXPR:
11759 op1 = save_expr (op1);
11760 imag = build2 (resultcode, real_type, imag, op1);
11761 /* Fall through. */
11762 case PLUS_EXPR:
11763 case MINUS_EXPR:
11764 real = build2 (resultcode, real_type, real, op1);
11765 break;
11766 default:
11767 gcc_unreachable();
11770 else
11772 op1 = save_expr (op1);
11773 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11774 op1, true);
11775 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11776 op1, true);
11777 switch (code)
11779 case MULT_EXPR:
11780 op0 = save_expr (op0);
11781 imag = build2 (resultcode, real_type, op0, imag);
11782 /* Fall through. */
11783 case PLUS_EXPR:
11784 real = build2 (resultcode, real_type, op0, real);
11785 break;
11786 case MINUS_EXPR:
11787 real = build2 (resultcode, real_type, op0, real);
11788 imag = build1 (NEGATE_EXPR, real_type, imag);
11789 break;
11790 default:
11791 gcc_unreachable();
11794 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11795 goto return_build_binary_op;
11798 /* For certain operations (which identify themselves by shorten != 0)
11799 if both args were extended from the same smaller type,
11800 do the arithmetic in that type and then extend.
11802 shorten !=0 and !=1 indicates a bitwise operation.
11803 For them, this optimization is safe only if
11804 both args are zero-extended or both are sign-extended.
11805 Otherwise, we might change the result.
11806 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11807 but calculated in (unsigned short) it would be (unsigned short)-1. */
11809 if (shorten && none_complex)
11811 final_type = result_type;
11812 result_type = shorten_binary_op (result_type, op0, op1,
11813 shorten == -1);
11816 /* Shifts can be shortened if shifting right. */
11818 if (short_shift)
11820 int unsigned_arg;
11821 tree arg0 = get_narrower (op0, &unsigned_arg);
11823 final_type = result_type;
11825 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11826 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11828 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11829 && tree_int_cst_sgn (op1) > 0
11830 /* We can shorten only if the shift count is less than the
11831 number of bits in the smaller type size. */
11832 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11833 /* We cannot drop an unsigned shift after sign-extension. */
11834 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11836 /* Do an unsigned shift if the operand was zero-extended. */
11837 result_type
11838 = c_common_signed_or_unsigned_type (unsigned_arg,
11839 TREE_TYPE (arg0));
11840 /* Convert value-to-be-shifted to that type. */
11841 if (TREE_TYPE (op0) != result_type)
11842 op0 = convert (result_type, op0);
11843 converted = 1;
11847 /* Comparison operations are shortened too but differently.
11848 They identify themselves by setting short_compare = 1. */
11850 if (short_compare)
11852 /* Don't write &op0, etc., because that would prevent op0
11853 from being kept in a register.
11854 Instead, make copies of the our local variables and
11855 pass the copies by reference, then copy them back afterward. */
11856 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11857 enum tree_code xresultcode = resultcode;
11858 tree val
11859 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11860 &xresultcode);
11862 if (val != NULL_TREE)
11864 ret = val;
11865 goto return_build_binary_op;
11868 op0 = xop0, op1 = xop1;
11869 converted = 1;
11870 resultcode = xresultcode;
11872 if (c_inhibit_evaluation_warnings == 0)
11874 bool op0_maybe_const = true;
11875 bool op1_maybe_const = true;
11876 tree orig_op0_folded, orig_op1_folded;
11878 if (in_late_binary_op)
11880 orig_op0_folded = orig_op0;
11881 orig_op1_folded = orig_op1;
11883 else
11885 /* Fold for the sake of possible warnings, as in
11886 build_conditional_expr. This requires the
11887 "original" values to be folded, not just op0 and
11888 op1. */
11889 c_inhibit_evaluation_warnings++;
11890 op0 = c_fully_fold (op0, require_constant_value,
11891 &op0_maybe_const);
11892 op1 = c_fully_fold (op1, require_constant_value,
11893 &op1_maybe_const);
11894 c_inhibit_evaluation_warnings--;
11895 orig_op0_folded = c_fully_fold (orig_op0,
11896 require_constant_value,
11897 NULL);
11898 orig_op1_folded = c_fully_fold (orig_op1,
11899 require_constant_value,
11900 NULL);
11903 if (warn_sign_compare)
11904 warn_for_sign_compare (location, orig_op0_folded,
11905 orig_op1_folded, op0, op1,
11906 result_type, resultcode);
11907 if (!in_late_binary_op && !int_operands)
11909 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11910 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11911 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11912 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11918 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11919 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11920 Then the expression will be built.
11921 It will be given type FINAL_TYPE if that is nonzero;
11922 otherwise, it will be given type RESULT_TYPE. */
11924 if (!result_type)
11926 gcc_rich_location richloc (location);
11927 richloc.maybe_add_expr (orig_op0);
11928 richloc.maybe_add_expr (orig_op1);
11929 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11930 return error_mark_node;
11933 if (build_type == NULL_TREE)
11935 build_type = result_type;
11936 if ((type0 != orig_type0 || type1 != orig_type1)
11937 && !boolean_op)
11939 gcc_assert (may_need_excess_precision && common);
11940 semantic_result_type = c_common_type (orig_type0, orig_type1);
11944 if (!converted)
11946 op0 = ep_convert_and_check (location, result_type, op0,
11947 semantic_result_type);
11948 op1 = ep_convert_and_check (location, result_type, op1,
11949 semantic_result_type);
11951 /* This can happen if one operand has a vector type, and the other
11952 has a different type. */
11953 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11954 return error_mark_node;
11957 if (sanitize_flags_p ((SANITIZE_SHIFT
11958 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
11959 && current_function_decl != NULL_TREE
11960 && (doing_div_or_mod || doing_shift)
11961 && !require_constant_value)
11963 /* OP0 and/or OP1 might have side-effects. */
11964 op0 = save_expr (op0);
11965 op1 = save_expr (op1);
11966 op0 = c_fully_fold (op0, false, NULL);
11967 op1 = c_fully_fold (op1, false, NULL);
11968 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
11969 | SANITIZE_FLOAT_DIVIDE))))
11970 instrument_expr = ubsan_instrument_division (location, op0, op1);
11971 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
11972 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11975 /* Treat expressions in initializers specially as they can't trap. */
11976 if (int_const_or_overflow)
11977 ret = (require_constant_value
11978 ? fold_build2_initializer_loc (location, resultcode, build_type,
11979 op0, op1)
11980 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11981 else
11982 ret = build2 (resultcode, build_type, op0, op1);
11983 if (final_type != NULL_TREE)
11984 ret = convert (final_type, ret);
11986 return_build_binary_op:
11987 gcc_assert (ret != error_mark_node);
11988 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11989 ret = (int_operands
11990 ? note_integer_operands (ret)
11991 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11992 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11993 && !in_late_binary_op)
11994 ret = note_integer_operands (ret);
11995 protected_set_expr_location (ret, location);
11997 if (instrument_expr != NULL)
11998 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11999 instrument_expr, ret);
12001 if (semantic_result_type)
12002 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12003 semantic_result_type, ret);
12005 return ret;
12009 /* Convert EXPR to be a truth-value, validating its type for this
12010 purpose. LOCATION is the source location for the expression. */
12012 tree
12013 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12015 bool int_const, int_operands;
12017 switch (TREE_CODE (TREE_TYPE (expr)))
12019 case ARRAY_TYPE:
12020 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12021 return error_mark_node;
12023 case RECORD_TYPE:
12024 error_at (location, "used struct type value where scalar is required");
12025 return error_mark_node;
12027 case UNION_TYPE:
12028 error_at (location, "used union type value where scalar is required");
12029 return error_mark_node;
12031 case VOID_TYPE:
12032 error_at (location, "void value not ignored as it ought to be");
12033 return error_mark_node;
12035 case POINTER_TYPE:
12036 if (reject_gcc_builtin (expr))
12037 return error_mark_node;
12038 break;
12040 case FUNCTION_TYPE:
12041 gcc_unreachable ();
12043 case VECTOR_TYPE:
12044 error_at (location, "used vector type where scalar is required");
12045 return error_mark_node;
12047 default:
12048 break;
12051 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12052 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12053 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12055 expr = remove_c_maybe_const_expr (expr);
12056 expr = build2 (NE_EXPR, integer_type_node, expr,
12057 convert (TREE_TYPE (expr), integer_zero_node));
12058 expr = note_integer_operands (expr);
12060 else
12061 /* ??? Should we also give an error for vectors rather than leaving
12062 those to give errors later? */
12063 expr = c_common_truthvalue_conversion (location, expr);
12065 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12067 if (TREE_OVERFLOW (expr))
12068 return expr;
12069 else
12070 return note_integer_operands (expr);
12072 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12073 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12074 return expr;
12078 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12079 required. */
12081 tree
12082 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12084 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12086 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12087 /* Executing a compound literal inside a function reinitializes
12088 it. */
12089 if (!TREE_STATIC (decl))
12090 *se = true;
12091 return decl;
12093 else
12094 return expr;
12097 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12098 statement. LOC is the location of the construct. */
12100 tree
12101 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12102 tree clauses)
12104 body = c_end_compound_stmt (loc, body, true);
12106 tree stmt = make_node (code);
12107 TREE_TYPE (stmt) = void_type_node;
12108 OMP_BODY (stmt) = body;
12109 OMP_CLAUSES (stmt) = clauses;
12110 SET_EXPR_LOCATION (stmt, loc);
12112 return add_stmt (stmt);
12115 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12116 statement. LOC is the location of the OACC_DATA. */
12118 tree
12119 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12121 tree stmt;
12123 block = c_end_compound_stmt (loc, block, true);
12125 stmt = make_node (OACC_DATA);
12126 TREE_TYPE (stmt) = void_type_node;
12127 OACC_DATA_CLAUSES (stmt) = clauses;
12128 OACC_DATA_BODY (stmt) = block;
12129 SET_EXPR_LOCATION (stmt, loc);
12131 return add_stmt (stmt);
12134 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12135 statement. LOC is the location of the OACC_HOST_DATA. */
12137 tree
12138 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12140 tree stmt;
12142 block = c_end_compound_stmt (loc, block, true);
12144 stmt = make_node (OACC_HOST_DATA);
12145 TREE_TYPE (stmt) = void_type_node;
12146 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12147 OACC_HOST_DATA_BODY (stmt) = block;
12148 SET_EXPR_LOCATION (stmt, loc);
12150 return add_stmt (stmt);
12153 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12155 tree
12156 c_begin_omp_parallel (void)
12158 tree block;
12160 keep_next_level ();
12161 block = c_begin_compound_stmt (true);
12163 return block;
12166 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12167 statement. LOC is the location of the OMP_PARALLEL. */
12169 tree
12170 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12172 tree stmt;
12174 block = c_end_compound_stmt (loc, block, true);
12176 stmt = make_node (OMP_PARALLEL);
12177 TREE_TYPE (stmt) = void_type_node;
12178 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12179 OMP_PARALLEL_BODY (stmt) = block;
12180 SET_EXPR_LOCATION (stmt, loc);
12182 return add_stmt (stmt);
12185 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12187 tree
12188 c_begin_omp_task (void)
12190 tree block;
12192 keep_next_level ();
12193 block = c_begin_compound_stmt (true);
12195 return block;
12198 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12199 statement. LOC is the location of the #pragma. */
12201 tree
12202 c_finish_omp_task (location_t loc, tree clauses, tree block)
12204 tree stmt;
12206 block = c_end_compound_stmt (loc, block, true);
12208 stmt = make_node (OMP_TASK);
12209 TREE_TYPE (stmt) = void_type_node;
12210 OMP_TASK_CLAUSES (stmt) = clauses;
12211 OMP_TASK_BODY (stmt) = block;
12212 SET_EXPR_LOCATION (stmt, loc);
12214 return add_stmt (stmt);
12217 /* Generate GOMP_cancel call for #pragma omp cancel. */
12219 void
12220 c_finish_omp_cancel (location_t loc, tree clauses)
12222 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12223 int mask = 0;
12224 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12225 mask = 1;
12226 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12227 mask = 2;
12228 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12229 mask = 4;
12230 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12231 mask = 8;
12232 else
12234 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12235 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12236 "clauses");
12237 return;
12239 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12240 if (ifc != NULL_TREE)
12242 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12243 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12244 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12245 build_zero_cst (type));
12247 else
12248 ifc = boolean_true_node;
12249 tree stmt = build_call_expr_loc (loc, fn, 2,
12250 build_int_cst (integer_type_node, mask),
12251 ifc);
12252 add_stmt (stmt);
12255 /* Generate GOMP_cancellation_point call for
12256 #pragma omp cancellation point. */
12258 void
12259 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12261 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12262 int mask = 0;
12263 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12264 mask = 1;
12265 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12266 mask = 2;
12267 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12268 mask = 4;
12269 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12270 mask = 8;
12271 else
12273 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12274 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12275 "clauses");
12276 return;
12278 tree stmt = build_call_expr_loc (loc, fn, 1,
12279 build_int_cst (integer_type_node, mask));
12280 add_stmt (stmt);
12283 /* Helper function for handle_omp_array_sections. Called recursively
12284 to handle multiple array-section-subscripts. C is the clause,
12285 T current expression (initially OMP_CLAUSE_DECL), which is either
12286 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12287 expression if specified, TREE_VALUE length expression if specified,
12288 TREE_CHAIN is what it has been specified after, or some decl.
12289 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12290 set to true if any of the array-section-subscript could have length
12291 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12292 first array-section-subscript which is known not to have length
12293 of one. Given say:
12294 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12295 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12296 all are or may have length of 1, array-section-subscript [:2] is the
12297 first one known not to have length 1. For array-section-subscript
12298 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12299 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12300 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12301 case though, as some lengths could be zero. */
12303 static tree
12304 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12305 bool &maybe_zero_len, unsigned int &first_non_one,
12306 enum c_omp_region_type ort)
12308 tree ret, low_bound, length, type;
12309 if (TREE_CODE (t) != TREE_LIST)
12311 if (error_operand_p (t))
12312 return error_mark_node;
12313 ret = t;
12314 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12315 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12317 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12318 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12319 return error_mark_node;
12321 if (TREE_CODE (t) == COMPONENT_REF
12322 && ort == C_ORT_OMP
12323 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12324 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12325 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12327 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12329 error_at (OMP_CLAUSE_LOCATION (c),
12330 "bit-field %qE in %qs clause",
12331 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12332 return error_mark_node;
12334 while (TREE_CODE (t) == COMPONENT_REF)
12336 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12338 error_at (OMP_CLAUSE_LOCATION (c),
12339 "%qE is a member of a union", t);
12340 return error_mark_node;
12342 t = TREE_OPERAND (t, 0);
12345 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12347 if (DECL_P (t))
12348 error_at (OMP_CLAUSE_LOCATION (c),
12349 "%qD is not a variable in %qs clause", t,
12350 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12351 else
12352 error_at (OMP_CLAUSE_LOCATION (c),
12353 "%qE is not a variable in %qs clause", t,
12354 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12355 return error_mark_node;
12357 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12358 && TYPE_ATOMIC (TREE_TYPE (t)))
12360 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12361 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12362 return error_mark_node;
12364 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12365 && VAR_P (t)
12366 && DECL_THREAD_LOCAL_P (t))
12368 error_at (OMP_CLAUSE_LOCATION (c),
12369 "%qD is threadprivate variable in %qs clause", t,
12370 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12371 return error_mark_node;
12373 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12374 && TYPE_ATOMIC (TREE_TYPE (t))
12375 && POINTER_TYPE_P (TREE_TYPE (t)))
12377 /* If the array section is pointer based and the pointer
12378 itself is _Atomic qualified, we need to atomically load
12379 the pointer. */
12380 c_expr expr;
12381 memset (&expr, 0, sizeof (expr));
12382 expr.value = ret;
12383 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12384 expr, false, false);
12385 ret = expr.value;
12387 return ret;
12390 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12391 maybe_zero_len, first_non_one, ort);
12392 if (ret == error_mark_node || ret == NULL_TREE)
12393 return ret;
12395 type = TREE_TYPE (ret);
12396 low_bound = TREE_PURPOSE (t);
12397 length = TREE_VALUE (t);
12399 if (low_bound == error_mark_node || length == error_mark_node)
12400 return error_mark_node;
12402 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12404 error_at (OMP_CLAUSE_LOCATION (c),
12405 "low bound %qE of array section does not have integral type",
12406 low_bound);
12407 return error_mark_node;
12409 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12411 error_at (OMP_CLAUSE_LOCATION (c),
12412 "length %qE of array section does not have integral type",
12413 length);
12414 return error_mark_node;
12416 if (low_bound
12417 && TREE_CODE (low_bound) == INTEGER_CST
12418 && TYPE_PRECISION (TREE_TYPE (low_bound))
12419 > TYPE_PRECISION (sizetype))
12420 low_bound = fold_convert (sizetype, low_bound);
12421 if (length
12422 && TREE_CODE (length) == INTEGER_CST
12423 && TYPE_PRECISION (TREE_TYPE (length))
12424 > TYPE_PRECISION (sizetype))
12425 length = fold_convert (sizetype, length);
12426 if (low_bound == NULL_TREE)
12427 low_bound = integer_zero_node;
12429 if (length != NULL_TREE)
12431 if (!integer_nonzerop (length))
12433 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12434 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12436 if (integer_zerop (length))
12438 error_at (OMP_CLAUSE_LOCATION (c),
12439 "zero length array section in %qs clause",
12440 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12441 return error_mark_node;
12444 else
12445 maybe_zero_len = true;
12447 if (first_non_one == types.length ()
12448 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12449 first_non_one++;
12451 if (TREE_CODE (type) == ARRAY_TYPE)
12453 if (length == NULL_TREE
12454 && (TYPE_DOMAIN (type) == NULL_TREE
12455 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12457 error_at (OMP_CLAUSE_LOCATION (c),
12458 "for unknown bound array type length expression must "
12459 "be specified");
12460 return error_mark_node;
12462 if (TREE_CODE (low_bound) == INTEGER_CST
12463 && tree_int_cst_sgn (low_bound) == -1)
12465 error_at (OMP_CLAUSE_LOCATION (c),
12466 "negative low bound in array section in %qs clause",
12467 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12468 return error_mark_node;
12470 if (length != NULL_TREE
12471 && TREE_CODE (length) == INTEGER_CST
12472 && tree_int_cst_sgn (length) == -1)
12474 error_at (OMP_CLAUSE_LOCATION (c),
12475 "negative length in array section in %qs clause",
12476 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12477 return error_mark_node;
12479 if (TYPE_DOMAIN (type)
12480 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12481 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12482 == INTEGER_CST)
12484 tree size
12485 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12486 size = size_binop (PLUS_EXPR, size, size_one_node);
12487 if (TREE_CODE (low_bound) == INTEGER_CST)
12489 if (tree_int_cst_lt (size, low_bound))
12491 error_at (OMP_CLAUSE_LOCATION (c),
12492 "low bound %qE above array section size "
12493 "in %qs clause", low_bound,
12494 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12495 return error_mark_node;
12497 if (tree_int_cst_equal (size, low_bound))
12499 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12500 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12502 error_at (OMP_CLAUSE_LOCATION (c),
12503 "zero length array section in %qs clause",
12504 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12505 return error_mark_node;
12507 maybe_zero_len = true;
12509 else if (length == NULL_TREE
12510 && first_non_one == types.length ()
12511 && tree_int_cst_equal
12512 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12513 low_bound))
12514 first_non_one++;
12516 else if (length == NULL_TREE)
12518 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12519 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12520 maybe_zero_len = true;
12521 if (first_non_one == types.length ())
12522 first_non_one++;
12524 if (length && TREE_CODE (length) == INTEGER_CST)
12526 if (tree_int_cst_lt (size, length))
12528 error_at (OMP_CLAUSE_LOCATION (c),
12529 "length %qE above array section size "
12530 "in %qs clause", length,
12531 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12532 return error_mark_node;
12534 if (TREE_CODE (low_bound) == INTEGER_CST)
12536 tree lbpluslen
12537 = size_binop (PLUS_EXPR,
12538 fold_convert (sizetype, low_bound),
12539 fold_convert (sizetype, length));
12540 if (TREE_CODE (lbpluslen) == INTEGER_CST
12541 && tree_int_cst_lt (size, lbpluslen))
12543 error_at (OMP_CLAUSE_LOCATION (c),
12544 "high bound %qE above array section size "
12545 "in %qs clause", lbpluslen,
12546 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12547 return error_mark_node;
12552 else if (length == NULL_TREE)
12554 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12555 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12556 maybe_zero_len = true;
12557 if (first_non_one == types.length ())
12558 first_non_one++;
12561 /* For [lb:] we will need to evaluate lb more than once. */
12562 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12564 tree lb = save_expr (low_bound);
12565 if (lb != low_bound)
12567 TREE_PURPOSE (t) = lb;
12568 low_bound = lb;
12572 else if (TREE_CODE (type) == POINTER_TYPE)
12574 if (length == NULL_TREE)
12576 error_at (OMP_CLAUSE_LOCATION (c),
12577 "for pointer type length expression must be specified");
12578 return error_mark_node;
12580 if (length != NULL_TREE
12581 && TREE_CODE (length) == INTEGER_CST
12582 && tree_int_cst_sgn (length) == -1)
12584 error_at (OMP_CLAUSE_LOCATION (c),
12585 "negative length in array section in %qs clause",
12586 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12587 return error_mark_node;
12589 /* If there is a pointer type anywhere but in the very first
12590 array-section-subscript, the array section can't be contiguous. */
12591 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12592 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12594 error_at (OMP_CLAUSE_LOCATION (c),
12595 "array section is not contiguous in %qs clause",
12596 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12597 return error_mark_node;
12600 else
12602 error_at (OMP_CLAUSE_LOCATION (c),
12603 "%qE does not have pointer or array type", ret);
12604 return error_mark_node;
12606 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12607 types.safe_push (TREE_TYPE (ret));
12608 /* We will need to evaluate lb more than once. */
12609 tree lb = save_expr (low_bound);
12610 if (lb != low_bound)
12612 TREE_PURPOSE (t) = lb;
12613 low_bound = lb;
12615 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12616 return ret;
12619 /* Handle array sections for clause C. */
12621 static bool
12622 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12624 bool maybe_zero_len = false;
12625 unsigned int first_non_one = 0;
12626 auto_vec<tree, 10> types;
12627 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12628 maybe_zero_len, first_non_one,
12629 ort);
12630 if (first == error_mark_node)
12631 return true;
12632 if (first == NULL_TREE)
12633 return false;
12634 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12636 tree t = OMP_CLAUSE_DECL (c);
12637 tree tem = NULL_TREE;
12638 /* Need to evaluate side effects in the length expressions
12639 if any. */
12640 while (TREE_CODE (t) == TREE_LIST)
12642 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12644 if (tem == NULL_TREE)
12645 tem = TREE_VALUE (t);
12646 else
12647 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12648 TREE_VALUE (t), tem);
12650 t = TREE_CHAIN (t);
12652 if (tem)
12653 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12654 first = c_fully_fold (first, false, NULL, true);
12655 OMP_CLAUSE_DECL (c) = first;
12657 else
12659 unsigned int num = types.length (), i;
12660 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12661 tree condition = NULL_TREE;
12663 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12664 maybe_zero_len = true;
12666 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12667 t = TREE_CHAIN (t))
12669 tree low_bound = TREE_PURPOSE (t);
12670 tree length = TREE_VALUE (t);
12672 i--;
12673 if (low_bound
12674 && TREE_CODE (low_bound) == INTEGER_CST
12675 && TYPE_PRECISION (TREE_TYPE (low_bound))
12676 > TYPE_PRECISION (sizetype))
12677 low_bound = fold_convert (sizetype, low_bound);
12678 if (length
12679 && TREE_CODE (length) == INTEGER_CST
12680 && TYPE_PRECISION (TREE_TYPE (length))
12681 > TYPE_PRECISION (sizetype))
12682 length = fold_convert (sizetype, length);
12683 if (low_bound == NULL_TREE)
12684 low_bound = integer_zero_node;
12685 if (!maybe_zero_len && i > first_non_one)
12687 if (integer_nonzerop (low_bound))
12688 goto do_warn_noncontiguous;
12689 if (length != NULL_TREE
12690 && TREE_CODE (length) == INTEGER_CST
12691 && TYPE_DOMAIN (types[i])
12692 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12693 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12694 == INTEGER_CST)
12696 tree size;
12697 size = size_binop (PLUS_EXPR,
12698 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12699 size_one_node);
12700 if (!tree_int_cst_equal (length, size))
12702 do_warn_noncontiguous:
12703 error_at (OMP_CLAUSE_LOCATION (c),
12704 "array section is not contiguous in %qs "
12705 "clause",
12706 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12707 return true;
12710 if (length != NULL_TREE
12711 && TREE_SIDE_EFFECTS (length))
12713 if (side_effects == NULL_TREE)
12714 side_effects = length;
12715 else
12716 side_effects = build2 (COMPOUND_EXPR,
12717 TREE_TYPE (side_effects),
12718 length, side_effects);
12721 else
12723 tree l;
12725 if (i > first_non_one
12726 && ((length && integer_nonzerop (length))
12727 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12728 continue;
12729 if (length)
12730 l = fold_convert (sizetype, length);
12731 else
12733 l = size_binop (PLUS_EXPR,
12734 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12735 size_one_node);
12736 l = size_binop (MINUS_EXPR, l,
12737 fold_convert (sizetype, low_bound));
12739 if (i > first_non_one)
12741 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12742 size_zero_node);
12743 if (condition == NULL_TREE)
12744 condition = l;
12745 else
12746 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12747 l, condition);
12749 else if (size == NULL_TREE)
12751 size = size_in_bytes (TREE_TYPE (types[i]));
12752 tree eltype = TREE_TYPE (types[num - 1]);
12753 while (TREE_CODE (eltype) == ARRAY_TYPE)
12754 eltype = TREE_TYPE (eltype);
12755 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12757 if (integer_zerop (size)
12758 || integer_zerop (size_in_bytes (eltype)))
12760 error_at (OMP_CLAUSE_LOCATION (c),
12761 "zero length array section in %qs clause",
12762 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12763 return error_mark_node;
12765 size = size_binop (EXACT_DIV_EXPR, size,
12766 size_in_bytes (eltype));
12768 size = size_binop (MULT_EXPR, size, l);
12769 if (condition)
12770 size = fold_build3 (COND_EXPR, sizetype, condition,
12771 size, size_zero_node);
12773 else
12774 size = size_binop (MULT_EXPR, size, l);
12777 if (side_effects)
12778 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12779 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12781 size = size_binop (MINUS_EXPR, size, size_one_node);
12782 size = c_fully_fold (size, false, NULL);
12783 tree index_type = build_index_type (size);
12784 tree eltype = TREE_TYPE (first);
12785 while (TREE_CODE (eltype) == ARRAY_TYPE)
12786 eltype = TREE_TYPE (eltype);
12787 tree type = build_array_type (eltype, index_type);
12788 tree ptype = build_pointer_type (eltype);
12789 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12790 t = build_fold_addr_expr (t);
12791 tree t2 = build_fold_addr_expr (first);
12792 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12793 ptrdiff_type_node, t2);
12794 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12795 ptrdiff_type_node, t2,
12796 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12797 ptrdiff_type_node, t));
12798 t2 = c_fully_fold (t2, false, NULL);
12799 if (tree_fits_shwi_p (t2))
12800 t = build2 (MEM_REF, type, t,
12801 build_int_cst (ptype, tree_to_shwi (t2)));
12802 else
12804 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12805 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12806 TREE_TYPE (t), t, t2);
12807 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12809 OMP_CLAUSE_DECL (c) = t;
12810 return false;
12812 first = c_fully_fold (first, false, NULL);
12813 OMP_CLAUSE_DECL (c) = first;
12814 if (size)
12815 size = c_fully_fold (size, false, NULL);
12816 OMP_CLAUSE_SIZE (c) = size;
12817 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12818 || (TREE_CODE (t) == COMPONENT_REF
12819 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12820 return false;
12821 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12822 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
12823 switch (OMP_CLAUSE_MAP_KIND (c))
12825 case GOMP_MAP_ALLOC:
12826 case GOMP_MAP_TO:
12827 case GOMP_MAP_FROM:
12828 case GOMP_MAP_TOFROM:
12829 case GOMP_MAP_ALWAYS_TO:
12830 case GOMP_MAP_ALWAYS_FROM:
12831 case GOMP_MAP_ALWAYS_TOFROM:
12832 case GOMP_MAP_RELEASE:
12833 case GOMP_MAP_DELETE:
12834 case GOMP_MAP_FORCE_TO:
12835 case GOMP_MAP_FORCE_FROM:
12836 case GOMP_MAP_FORCE_TOFROM:
12837 case GOMP_MAP_FORCE_PRESENT:
12838 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12839 break;
12840 default:
12841 break;
12843 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12844 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
12845 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12846 else if (TREE_CODE (t) == COMPONENT_REF)
12847 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12848 else
12849 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12850 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12851 && !c_mark_addressable (t))
12852 return false;
12853 OMP_CLAUSE_DECL (c2) = t;
12854 t = build_fold_addr_expr (first);
12855 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12856 tree ptr = OMP_CLAUSE_DECL (c2);
12857 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12858 ptr = build_fold_addr_expr (ptr);
12859 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12860 ptrdiff_type_node, t,
12861 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12862 ptrdiff_type_node, ptr));
12863 t = c_fully_fold (t, false, NULL);
12864 OMP_CLAUSE_SIZE (c2) = t;
12865 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12866 OMP_CLAUSE_CHAIN (c) = c2;
12868 return false;
12871 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12872 an inline call. But, remap
12873 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12874 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12876 static tree
12877 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12878 tree decl, tree placeholder)
12880 copy_body_data id;
12881 hash_map<tree, tree> decl_map;
12883 decl_map.put (omp_decl1, placeholder);
12884 decl_map.put (omp_decl2, decl);
12885 memset (&id, 0, sizeof (id));
12886 id.src_fn = DECL_CONTEXT (omp_decl1);
12887 id.dst_fn = current_function_decl;
12888 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12889 id.decl_map = &decl_map;
12891 id.copy_decl = copy_decl_no_change;
12892 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12893 id.transform_new_cfg = true;
12894 id.transform_return_to_modify = false;
12895 id.transform_lang_insert_block = NULL;
12896 id.eh_lp_nr = 0;
12897 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12898 return stmt;
12901 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12902 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12904 static tree
12905 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12907 if (*tp == (tree) data)
12908 return *tp;
12909 return NULL_TREE;
12912 /* For all elements of CLAUSES, validate them against their constraints.
12913 Remove any elements from the list that are invalid. */
12915 tree
12916 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12918 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12919 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
12920 tree c, t, type, *pc;
12921 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12922 bool branch_seen = false;
12923 bool copyprivate_seen = false;
12924 bool linear_variable_step_check = false;
12925 tree *nowait_clause = NULL;
12926 bool ordered_seen = false;
12927 tree schedule_clause = NULL_TREE;
12928 bool oacc_async = false;
12930 bitmap_obstack_initialize (NULL);
12931 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12932 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12933 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12934 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12935 bitmap_initialize (&map_head, &bitmap_default_obstack);
12936 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12937 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
12939 if (ort & C_ORT_ACC)
12940 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12941 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12943 oacc_async = true;
12944 break;
12947 for (pc = &clauses, c = clauses; c ; c = *pc)
12949 bool remove = false;
12950 bool need_complete = false;
12951 bool need_implicitly_determined = false;
12953 switch (OMP_CLAUSE_CODE (c))
12955 case OMP_CLAUSE_SHARED:
12956 need_implicitly_determined = true;
12957 goto check_dup_generic;
12959 case OMP_CLAUSE_PRIVATE:
12960 need_complete = true;
12961 need_implicitly_determined = true;
12962 goto check_dup_generic;
12964 case OMP_CLAUSE_REDUCTION:
12965 need_implicitly_determined = true;
12966 t = OMP_CLAUSE_DECL (c);
12967 if (TREE_CODE (t) == TREE_LIST)
12969 if (handle_omp_array_sections (c, ort))
12971 remove = true;
12972 break;
12975 t = OMP_CLAUSE_DECL (c);
12977 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
12978 if (t == error_mark_node)
12980 remove = true;
12981 break;
12983 if (oacc_async)
12984 c_mark_addressable (t);
12985 type = TREE_TYPE (t);
12986 if (TREE_CODE (t) == MEM_REF)
12987 type = TREE_TYPE (type);
12988 if (TREE_CODE (type) == ARRAY_TYPE)
12990 tree oatype = type;
12991 gcc_assert (TREE_CODE (t) != MEM_REF);
12992 while (TREE_CODE (type) == ARRAY_TYPE)
12993 type = TREE_TYPE (type);
12994 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12996 error_at (OMP_CLAUSE_LOCATION (c),
12997 "%qD in %<reduction%> clause is a zero size array",
12999 remove = true;
13000 break;
13002 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13003 TYPE_SIZE_UNIT (type));
13004 if (integer_zerop (size))
13006 error_at (OMP_CLAUSE_LOCATION (c),
13007 "%qD in %<reduction%> clause is a zero size array",
13009 remove = true;
13010 break;
13012 size = size_binop (MINUS_EXPR, size, size_one_node);
13013 tree index_type = build_index_type (size);
13014 tree atype = build_array_type (type, index_type);
13015 tree ptype = build_pointer_type (type);
13016 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13017 t = build_fold_addr_expr (t);
13018 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13019 OMP_CLAUSE_DECL (c) = t;
13021 if (TYPE_ATOMIC (type))
13023 error_at (OMP_CLAUSE_LOCATION (c),
13024 "%<_Atomic%> %qE in %<reduction%> clause", t);
13025 remove = true;
13026 break;
13028 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
13029 && (FLOAT_TYPE_P (type)
13030 || TREE_CODE (type) == COMPLEX_TYPE))
13032 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13033 const char *r_name = NULL;
13035 switch (r_code)
13037 case PLUS_EXPR:
13038 case MULT_EXPR:
13039 case MINUS_EXPR:
13040 break;
13041 case MIN_EXPR:
13042 if (TREE_CODE (type) == COMPLEX_TYPE)
13043 r_name = "min";
13044 break;
13045 case MAX_EXPR:
13046 if (TREE_CODE (type) == COMPLEX_TYPE)
13047 r_name = "max";
13048 break;
13049 case BIT_AND_EXPR:
13050 r_name = "&";
13051 break;
13052 case BIT_XOR_EXPR:
13053 r_name = "^";
13054 break;
13055 case BIT_IOR_EXPR:
13056 r_name = "|";
13057 break;
13058 case TRUTH_ANDIF_EXPR:
13059 if (FLOAT_TYPE_P (type))
13060 r_name = "&&";
13061 break;
13062 case TRUTH_ORIF_EXPR:
13063 if (FLOAT_TYPE_P (type))
13064 r_name = "||";
13065 break;
13066 default:
13067 gcc_unreachable ();
13069 if (r_name)
13071 error_at (OMP_CLAUSE_LOCATION (c),
13072 "%qE has invalid type for %<reduction(%s)%>",
13073 t, r_name);
13074 remove = true;
13075 break;
13078 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13080 error_at (OMP_CLAUSE_LOCATION (c),
13081 "user defined reduction not found for %qE", t);
13082 remove = true;
13083 break;
13085 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13087 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
13088 type = TYPE_MAIN_VARIANT (type);
13089 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13090 VAR_DECL, NULL_TREE, type);
13091 tree decl_placeholder = NULL_TREE;
13092 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13093 DECL_ARTIFICIAL (placeholder) = 1;
13094 DECL_IGNORED_P (placeholder) = 1;
13095 if (TREE_CODE (t) == MEM_REF)
13097 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13098 VAR_DECL, NULL_TREE, type);
13099 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13100 DECL_ARTIFICIAL (decl_placeholder) = 1;
13101 DECL_IGNORED_P (decl_placeholder) = 1;
13103 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13104 c_mark_addressable (placeholder);
13105 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
13106 c_mark_addressable (decl_placeholder ? decl_placeholder
13107 : OMP_CLAUSE_DECL (c));
13108 OMP_CLAUSE_REDUCTION_MERGE (c)
13109 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13110 TREE_VEC_ELT (list, 0),
13111 TREE_VEC_ELT (list, 1),
13112 decl_placeholder ? decl_placeholder
13113 : OMP_CLAUSE_DECL (c), placeholder);
13114 OMP_CLAUSE_REDUCTION_MERGE (c)
13115 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13116 void_type_node, NULL_TREE,
13117 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13118 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13119 if (TREE_VEC_LENGTH (list) == 6)
13121 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13122 c_mark_addressable (decl_placeholder ? decl_placeholder
13123 : OMP_CLAUSE_DECL (c));
13124 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13125 c_mark_addressable (placeholder);
13126 tree init = TREE_VEC_ELT (list, 5);
13127 if (init == error_mark_node)
13128 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13129 OMP_CLAUSE_REDUCTION_INIT (c)
13130 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13131 TREE_VEC_ELT (list, 3),
13132 decl_placeholder ? decl_placeholder
13133 : OMP_CLAUSE_DECL (c), placeholder);
13134 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13136 tree v = decl_placeholder ? decl_placeholder : t;
13137 OMP_CLAUSE_REDUCTION_INIT (c)
13138 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13139 OMP_CLAUSE_REDUCTION_INIT (c));
13141 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13142 c_find_omp_placeholder_r,
13143 placeholder, NULL))
13144 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13146 else
13148 tree init;
13149 tree v = decl_placeholder ? decl_placeholder : t;
13150 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13151 init = build_constructor (TREE_TYPE (v), NULL);
13152 else
13153 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13154 OMP_CLAUSE_REDUCTION_INIT (c)
13155 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13157 OMP_CLAUSE_REDUCTION_INIT (c)
13158 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13159 void_type_node, NULL_TREE,
13160 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13161 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13163 if (TREE_CODE (t) == MEM_REF)
13165 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13166 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13167 != INTEGER_CST)
13169 sorry ("variable length element type in array "
13170 "%<reduction%> clause");
13171 remove = true;
13172 break;
13174 t = TREE_OPERAND (t, 0);
13175 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13176 t = TREE_OPERAND (t, 0);
13177 if (TREE_CODE (t) == ADDR_EXPR)
13178 t = TREE_OPERAND (t, 0);
13180 goto check_dup_generic_t;
13182 case OMP_CLAUSE_COPYPRIVATE:
13183 copyprivate_seen = true;
13184 if (nowait_clause)
13186 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13187 "%<nowait%> clause must not be used together "
13188 "with %<copyprivate%>");
13189 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13190 nowait_clause = NULL;
13192 goto check_dup_generic;
13194 case OMP_CLAUSE_COPYIN:
13195 t = OMP_CLAUSE_DECL (c);
13196 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13198 error_at (OMP_CLAUSE_LOCATION (c),
13199 "%qE must be %<threadprivate%> for %<copyin%>", t);
13200 remove = true;
13201 break;
13203 goto check_dup_generic;
13205 case OMP_CLAUSE_LINEAR:
13206 if (ort != C_ORT_OMP_DECLARE_SIMD)
13207 need_implicitly_determined = true;
13208 t = OMP_CLAUSE_DECL (c);
13209 if (ort != C_ORT_OMP_DECLARE_SIMD
13210 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13212 error_at (OMP_CLAUSE_LOCATION (c),
13213 "modifier should not be specified in %<linear%> "
13214 "clause on %<simd%> or %<for%> constructs");
13215 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13217 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13218 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13220 error_at (OMP_CLAUSE_LOCATION (c),
13221 "linear clause applied to non-integral non-pointer "
13222 "variable with type %qT", TREE_TYPE (t));
13223 remove = true;
13224 break;
13226 if (TYPE_ATOMIC (TREE_TYPE (t)))
13228 error_at (OMP_CLAUSE_LOCATION (c),
13229 "%<_Atomic%> %qD in %<linear%> clause", t);
13230 remove = true;
13231 break;
13233 if (ort == C_ORT_OMP_DECLARE_SIMD)
13235 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13236 if (TREE_CODE (s) == PARM_DECL)
13238 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13239 /* map_head bitmap is used as uniform_head if
13240 declare_simd. */
13241 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13242 linear_variable_step_check = true;
13243 goto check_dup_generic;
13245 if (TREE_CODE (s) != INTEGER_CST)
13247 error_at (OMP_CLAUSE_LOCATION (c),
13248 "%<linear%> clause step %qE is neither constant "
13249 "nor a parameter", s);
13250 remove = true;
13251 break;
13254 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13256 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13257 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13258 OMP_CLAUSE_DECL (c), s);
13259 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13260 sizetype, fold_convert (sizetype, s),
13261 fold_convert
13262 (sizetype, OMP_CLAUSE_DECL (c)));
13263 if (s == error_mark_node)
13264 s = size_one_node;
13265 OMP_CLAUSE_LINEAR_STEP (c) = s;
13267 else
13268 OMP_CLAUSE_LINEAR_STEP (c)
13269 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13270 goto check_dup_generic;
13272 check_dup_generic:
13273 t = OMP_CLAUSE_DECL (c);
13274 check_dup_generic_t:
13275 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13277 error_at (OMP_CLAUSE_LOCATION (c),
13278 "%qE is not a variable in clause %qs", t,
13279 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13280 remove = true;
13282 else if (ort == C_ORT_ACC
13283 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13285 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13287 error ("%qD appears more than once in reduction clauses", t);
13288 remove = true;
13290 else
13291 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13293 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13294 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13295 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13297 error_at (OMP_CLAUSE_LOCATION (c),
13298 "%qE appears more than once in data clauses", t);
13299 remove = true;
13301 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13302 && bitmap_bit_p (&map_head, DECL_UID (t)))
13304 if (ort == C_ORT_ACC)
13305 error ("%qD appears more than once in data clauses", t);
13306 else
13307 error ("%qD appears both in data and map clauses", t);
13308 remove = true;
13310 else
13311 bitmap_set_bit (&generic_head, DECL_UID (t));
13312 break;
13314 case OMP_CLAUSE_FIRSTPRIVATE:
13315 t = OMP_CLAUSE_DECL (c);
13316 need_complete = true;
13317 need_implicitly_determined = true;
13318 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13320 error_at (OMP_CLAUSE_LOCATION (c),
13321 "%qE is not a variable in clause %<firstprivate%>", t);
13322 remove = true;
13324 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13325 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13327 error_at (OMP_CLAUSE_LOCATION (c),
13328 "%qE appears more than once in data clauses", t);
13329 remove = true;
13331 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13333 if (ort == C_ORT_ACC)
13334 error ("%qD appears more than once in data clauses", t);
13335 else
13336 error ("%qD appears both in data and map clauses", t);
13337 remove = true;
13339 else
13340 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13341 break;
13343 case OMP_CLAUSE_LASTPRIVATE:
13344 t = OMP_CLAUSE_DECL (c);
13345 need_complete = true;
13346 need_implicitly_determined = true;
13347 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13349 error_at (OMP_CLAUSE_LOCATION (c),
13350 "%qE is not a variable in clause %<lastprivate%>", t);
13351 remove = true;
13353 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13354 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13356 error_at (OMP_CLAUSE_LOCATION (c),
13357 "%qE appears more than once in data clauses", t);
13358 remove = true;
13360 else
13361 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13362 break;
13364 case OMP_CLAUSE_ALIGNED:
13365 t = OMP_CLAUSE_DECL (c);
13366 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13368 error_at (OMP_CLAUSE_LOCATION (c),
13369 "%qE is not a variable in %<aligned%> clause", t);
13370 remove = true;
13372 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13373 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13375 error_at (OMP_CLAUSE_LOCATION (c),
13376 "%qE in %<aligned%> clause is neither a pointer nor "
13377 "an array", t);
13378 remove = true;
13380 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13382 error_at (OMP_CLAUSE_LOCATION (c),
13383 "%<_Atomic%> %qD in %<aligned%> clause", t);
13384 remove = true;
13385 break;
13387 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13389 error_at (OMP_CLAUSE_LOCATION (c),
13390 "%qE appears more than once in %<aligned%> clauses",
13392 remove = true;
13394 else
13395 bitmap_set_bit (&aligned_head, DECL_UID (t));
13396 break;
13398 case OMP_CLAUSE_DEPEND:
13399 t = OMP_CLAUSE_DECL (c);
13400 if (t == NULL_TREE)
13402 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13403 == OMP_CLAUSE_DEPEND_SOURCE);
13404 break;
13406 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13408 gcc_assert (TREE_CODE (t) == TREE_LIST);
13409 for (; t; t = TREE_CHAIN (t))
13411 tree decl = TREE_VALUE (t);
13412 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13414 tree offset = TREE_PURPOSE (t);
13415 bool neg = wi::neg_p (wi::to_wide (offset));
13416 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13417 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13418 neg ? MINUS_EXPR : PLUS_EXPR,
13419 decl, offset);
13420 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13421 sizetype,
13422 fold_convert (sizetype, t2),
13423 fold_convert (sizetype, decl));
13424 if (t2 == error_mark_node)
13426 remove = true;
13427 break;
13429 TREE_PURPOSE (t) = t2;
13432 break;
13434 if (TREE_CODE (t) == TREE_LIST)
13436 if (handle_omp_array_sections (c, ort))
13437 remove = true;
13438 break;
13440 if (t == error_mark_node)
13441 remove = true;
13442 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13444 error_at (OMP_CLAUSE_LOCATION (c),
13445 "%qE is not a variable in %<depend%> clause", t);
13446 remove = true;
13448 else if (!c_mark_addressable (t))
13449 remove = true;
13450 break;
13452 case OMP_CLAUSE_MAP:
13453 case OMP_CLAUSE_TO:
13454 case OMP_CLAUSE_FROM:
13455 case OMP_CLAUSE__CACHE_:
13456 t = OMP_CLAUSE_DECL (c);
13457 if (TREE_CODE (t) == TREE_LIST)
13459 if (handle_omp_array_sections (c, ort))
13460 remove = true;
13461 else
13463 t = OMP_CLAUSE_DECL (c);
13464 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13466 error_at (OMP_CLAUSE_LOCATION (c),
13467 "array section does not have mappable type "
13468 "in %qs clause",
13469 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13470 remove = true;
13472 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13474 error_at (OMP_CLAUSE_LOCATION (c),
13475 "%<_Atomic%> %qE in %qs clause", t,
13476 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13477 remove = true;
13479 while (TREE_CODE (t) == ARRAY_REF)
13480 t = TREE_OPERAND (t, 0);
13481 if (TREE_CODE (t) == COMPONENT_REF
13482 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13484 while (TREE_CODE (t) == COMPONENT_REF)
13485 t = TREE_OPERAND (t, 0);
13486 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13487 break;
13488 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13490 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13491 error ("%qD appears more than once in motion"
13492 " clauses", t);
13493 else if (ort == C_ORT_ACC)
13494 error ("%qD appears more than once in data"
13495 " clauses", t);
13496 else
13497 error ("%qD appears more than once in map"
13498 " clauses", t);
13499 remove = true;
13501 else
13503 bitmap_set_bit (&map_head, DECL_UID (t));
13504 bitmap_set_bit (&map_field_head, DECL_UID (t));
13508 break;
13510 if (t == error_mark_node)
13512 remove = true;
13513 break;
13515 if (TREE_CODE (t) == COMPONENT_REF
13516 && (ort & C_ORT_OMP)
13517 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13519 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13521 error_at (OMP_CLAUSE_LOCATION (c),
13522 "bit-field %qE in %qs clause",
13523 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13524 remove = true;
13526 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13528 error_at (OMP_CLAUSE_LOCATION (c),
13529 "%qE does not have a mappable type in %qs clause",
13530 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13531 remove = true;
13533 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13535 error_at (OMP_CLAUSE_LOCATION (c),
13536 "%<_Atomic%> %qE in %qs clause", t,
13537 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13538 remove = true;
13540 while (TREE_CODE (t) == COMPONENT_REF)
13542 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13543 == UNION_TYPE)
13545 error_at (OMP_CLAUSE_LOCATION (c),
13546 "%qE is a member of a union", t);
13547 remove = true;
13548 break;
13550 t = TREE_OPERAND (t, 0);
13552 if (remove)
13553 break;
13554 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13556 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13557 break;
13560 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13562 error_at (OMP_CLAUSE_LOCATION (c),
13563 "%qE is not a variable in %qs clause", t,
13564 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13565 remove = true;
13567 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13569 error_at (OMP_CLAUSE_LOCATION (c),
13570 "%qD is threadprivate variable in %qs clause", t,
13571 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13572 remove = true;
13574 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13575 || (OMP_CLAUSE_MAP_KIND (c)
13576 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13577 && !c_mark_addressable (t))
13578 remove = true;
13579 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13580 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13581 || (OMP_CLAUSE_MAP_KIND (c)
13582 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13583 || (OMP_CLAUSE_MAP_KIND (c)
13584 == GOMP_MAP_FORCE_DEVICEPTR)))
13585 && t == OMP_CLAUSE_DECL (c)
13586 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13588 error_at (OMP_CLAUSE_LOCATION (c),
13589 "%qD does not have a mappable type in %qs clause", t,
13590 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13591 remove = true;
13593 else if (TREE_TYPE (t) == error_mark_node)
13594 remove = true;
13595 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13597 error_at (OMP_CLAUSE_LOCATION (c),
13598 "%<_Atomic%> %qE in %qs clause", t,
13599 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13600 remove = true;
13602 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13603 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13605 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13606 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13608 error ("%qD appears more than once in data clauses", t);
13609 remove = true;
13611 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13613 if (ort == C_ORT_ACC)
13614 error ("%qD appears more than once in data clauses", t);
13615 else
13616 error ("%qD appears both in data and map clauses", t);
13617 remove = true;
13619 else
13620 bitmap_set_bit (&generic_head, DECL_UID (t));
13622 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13624 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13625 error ("%qD appears more than once in motion clauses", t);
13626 else if (ort == C_ORT_ACC)
13627 error ("%qD appears more than once in data clauses", t);
13628 else
13629 error ("%qD appears more than once in map clauses", t);
13630 remove = true;
13632 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13633 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13635 if (ort == C_ORT_ACC)
13636 error ("%qD appears more than once in data clauses", t);
13637 else
13638 error ("%qD appears both in data and map clauses", t);
13639 remove = true;
13641 else
13643 bitmap_set_bit (&map_head, DECL_UID (t));
13644 if (t != OMP_CLAUSE_DECL (c)
13645 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13646 bitmap_set_bit (&map_field_head, DECL_UID (t));
13648 break;
13650 case OMP_CLAUSE_TO_DECLARE:
13651 case OMP_CLAUSE_LINK:
13652 t = OMP_CLAUSE_DECL (c);
13653 if (TREE_CODE (t) == FUNCTION_DECL
13654 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13656 else if (!VAR_P (t))
13658 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13659 error_at (OMP_CLAUSE_LOCATION (c),
13660 "%qE is neither a variable nor a function name in "
13661 "clause %qs", t,
13662 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13663 else
13664 error_at (OMP_CLAUSE_LOCATION (c),
13665 "%qE is not a variable in clause %qs", t,
13666 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13667 remove = true;
13669 else if (DECL_THREAD_LOCAL_P (t))
13671 error_at (OMP_CLAUSE_LOCATION (c),
13672 "%qD is threadprivate variable in %qs clause", t,
13673 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13674 remove = true;
13676 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13678 error_at (OMP_CLAUSE_LOCATION (c),
13679 "%qD does not have a mappable type in %qs clause", t,
13680 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13681 remove = true;
13683 if (remove)
13684 break;
13685 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13687 error_at (OMP_CLAUSE_LOCATION (c),
13688 "%qE appears more than once on the same "
13689 "%<declare target%> directive", t);
13690 remove = true;
13692 else
13693 bitmap_set_bit (&generic_head, DECL_UID (t));
13694 break;
13696 case OMP_CLAUSE_UNIFORM:
13697 t = OMP_CLAUSE_DECL (c);
13698 if (TREE_CODE (t) != PARM_DECL)
13700 if (DECL_P (t))
13701 error_at (OMP_CLAUSE_LOCATION (c),
13702 "%qD is not an argument in %<uniform%> clause", t);
13703 else
13704 error_at (OMP_CLAUSE_LOCATION (c),
13705 "%qE is not an argument in %<uniform%> clause", t);
13706 remove = true;
13707 break;
13709 /* map_head bitmap is used as uniform_head if declare_simd. */
13710 bitmap_set_bit (&map_head, DECL_UID (t));
13711 goto check_dup_generic;
13713 case OMP_CLAUSE_IS_DEVICE_PTR:
13714 case OMP_CLAUSE_USE_DEVICE_PTR:
13715 t = OMP_CLAUSE_DECL (c);
13716 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13717 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13719 error_at (OMP_CLAUSE_LOCATION (c),
13720 "%qs variable is neither a pointer nor an array",
13721 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13722 remove = true;
13724 goto check_dup_generic;
13726 case OMP_CLAUSE_NOWAIT:
13727 if (copyprivate_seen)
13729 error_at (OMP_CLAUSE_LOCATION (c),
13730 "%<nowait%> clause must not be used together "
13731 "with %<copyprivate%>");
13732 remove = true;
13733 break;
13735 nowait_clause = pc;
13736 pc = &OMP_CLAUSE_CHAIN (c);
13737 continue;
13739 case OMP_CLAUSE_IF:
13740 case OMP_CLAUSE_NUM_THREADS:
13741 case OMP_CLAUSE_NUM_TEAMS:
13742 case OMP_CLAUSE_THREAD_LIMIT:
13743 case OMP_CLAUSE_DEFAULT:
13744 case OMP_CLAUSE_UNTIED:
13745 case OMP_CLAUSE_COLLAPSE:
13746 case OMP_CLAUSE_FINAL:
13747 case OMP_CLAUSE_MERGEABLE:
13748 case OMP_CLAUSE_DEVICE:
13749 case OMP_CLAUSE_DIST_SCHEDULE:
13750 case OMP_CLAUSE_PARALLEL:
13751 case OMP_CLAUSE_FOR:
13752 case OMP_CLAUSE_SECTIONS:
13753 case OMP_CLAUSE_TASKGROUP:
13754 case OMP_CLAUSE_PROC_BIND:
13755 case OMP_CLAUSE_PRIORITY:
13756 case OMP_CLAUSE_GRAINSIZE:
13757 case OMP_CLAUSE_NUM_TASKS:
13758 case OMP_CLAUSE_NOGROUP:
13759 case OMP_CLAUSE_THREADS:
13760 case OMP_CLAUSE_SIMD:
13761 case OMP_CLAUSE_HINT:
13762 case OMP_CLAUSE_DEFAULTMAP:
13763 case OMP_CLAUSE_NUM_GANGS:
13764 case OMP_CLAUSE_NUM_WORKERS:
13765 case OMP_CLAUSE_VECTOR_LENGTH:
13766 case OMP_CLAUSE_ASYNC:
13767 case OMP_CLAUSE_WAIT:
13768 case OMP_CLAUSE_AUTO:
13769 case OMP_CLAUSE_INDEPENDENT:
13770 case OMP_CLAUSE_SEQ:
13771 case OMP_CLAUSE_GANG:
13772 case OMP_CLAUSE_WORKER:
13773 case OMP_CLAUSE_VECTOR:
13774 case OMP_CLAUSE_TILE:
13775 pc = &OMP_CLAUSE_CHAIN (c);
13776 continue;
13778 case OMP_CLAUSE_SCHEDULE:
13779 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13781 const char *p = NULL;
13782 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13784 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13785 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13786 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13787 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13788 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13789 default: gcc_unreachable ();
13791 if (p)
13793 error_at (OMP_CLAUSE_LOCATION (c),
13794 "%<nonmonotonic%> modifier specified for %qs "
13795 "schedule kind", p);
13796 OMP_CLAUSE_SCHEDULE_KIND (c)
13797 = (enum omp_clause_schedule_kind)
13798 (OMP_CLAUSE_SCHEDULE_KIND (c)
13799 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13802 schedule_clause = c;
13803 pc = &OMP_CLAUSE_CHAIN (c);
13804 continue;
13806 case OMP_CLAUSE_ORDERED:
13807 ordered_seen = true;
13808 pc = &OMP_CLAUSE_CHAIN (c);
13809 continue;
13811 case OMP_CLAUSE_SAFELEN:
13812 safelen = c;
13813 pc = &OMP_CLAUSE_CHAIN (c);
13814 continue;
13815 case OMP_CLAUSE_SIMDLEN:
13816 simdlen = c;
13817 pc = &OMP_CLAUSE_CHAIN (c);
13818 continue;
13820 case OMP_CLAUSE_INBRANCH:
13821 case OMP_CLAUSE_NOTINBRANCH:
13822 if (branch_seen)
13824 error_at (OMP_CLAUSE_LOCATION (c),
13825 "%<inbranch%> clause is incompatible with "
13826 "%<notinbranch%>");
13827 remove = true;
13828 break;
13830 branch_seen = true;
13831 pc = &OMP_CLAUSE_CHAIN (c);
13832 continue;
13834 default:
13835 gcc_unreachable ();
13838 if (!remove)
13840 t = OMP_CLAUSE_DECL (c);
13842 if (need_complete)
13844 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13845 if (t == error_mark_node)
13846 remove = true;
13849 if (need_implicitly_determined)
13851 const char *share_name = NULL;
13853 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13854 share_name = "threadprivate";
13855 else switch (c_omp_predetermined_sharing (t))
13857 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13858 break;
13859 case OMP_CLAUSE_DEFAULT_SHARED:
13860 /* const vars may be specified in firstprivate clause. */
13861 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13862 && TREE_READONLY (t))
13863 break;
13864 share_name = "shared";
13865 break;
13866 case OMP_CLAUSE_DEFAULT_PRIVATE:
13867 share_name = "private";
13868 break;
13869 default:
13870 gcc_unreachable ();
13872 if (share_name)
13874 error_at (OMP_CLAUSE_LOCATION (c),
13875 "%qE is predetermined %qs for %qs",
13876 t, share_name,
13877 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13878 remove = true;
13883 if (remove)
13884 *pc = OMP_CLAUSE_CHAIN (c);
13885 else
13886 pc = &OMP_CLAUSE_CHAIN (c);
13889 if (simdlen
13890 && safelen
13891 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13892 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13894 error_at (OMP_CLAUSE_LOCATION (simdlen),
13895 "%<simdlen%> clause value is bigger than "
13896 "%<safelen%> clause value");
13897 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13898 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13901 if (ordered_seen
13902 && schedule_clause
13903 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13904 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13906 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13907 "%<nonmonotonic%> schedule modifier specified together "
13908 "with %<ordered%> clause");
13909 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13910 = (enum omp_clause_schedule_kind)
13911 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13912 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13915 if (linear_variable_step_check)
13916 for (pc = &clauses, c = clauses; c ; c = *pc)
13918 bool remove = false;
13919 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13920 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13921 && !bitmap_bit_p (&map_head,
13922 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13924 error_at (OMP_CLAUSE_LOCATION (c),
13925 "%<linear%> clause step is a parameter %qD not "
13926 "specified in %<uniform%> clause",
13927 OMP_CLAUSE_LINEAR_STEP (c));
13928 remove = true;
13931 if (remove)
13932 *pc = OMP_CLAUSE_CHAIN (c);
13933 else
13934 pc = &OMP_CLAUSE_CHAIN (c);
13937 bitmap_obstack_release (NULL);
13938 return clauses;
13941 /* Return code to initialize DST with a copy constructor from SRC.
13942 C doesn't have copy constructors nor assignment operators, only for
13943 _Atomic vars we need to perform __atomic_load from src into a temporary
13944 followed by __atomic_store of the temporary to dst. */
13946 tree
13947 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13949 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13950 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13952 location_t loc = OMP_CLAUSE_LOCATION (clause);
13953 tree type = TREE_TYPE (dst);
13954 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13955 tree tmp = create_tmp_var (nonatomic_type);
13956 tree tmp_addr = build_fold_addr_expr (tmp);
13957 TREE_ADDRESSABLE (tmp) = 1;
13958 TREE_NO_WARNING (tmp) = 1;
13959 tree src_addr = build_fold_addr_expr (src);
13960 tree dst_addr = build_fold_addr_expr (dst);
13961 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13962 vec<tree, va_gc> *params;
13963 /* Expansion of a generic atomic load may require an addition
13964 element, so allocate enough to prevent a resize. */
13965 vec_alloc (params, 4);
13967 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13968 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13969 params->quick_push (src_addr);
13970 params->quick_push (tmp_addr);
13971 params->quick_push (seq_cst);
13972 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13974 vec_alloc (params, 4);
13976 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13977 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
13978 params->quick_push (dst_addr);
13979 params->quick_push (tmp_addr);
13980 params->quick_push (seq_cst);
13981 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13982 return build2 (COMPOUND_EXPR, void_type_node, load, store);
13985 /* Create a transaction node. */
13987 tree
13988 c_finish_transaction (location_t loc, tree block, int flags)
13990 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13991 if (flags & TM_STMT_ATTR_OUTER)
13992 TRANSACTION_EXPR_OUTER (stmt) = 1;
13993 if (flags & TM_STMT_ATTR_RELAXED)
13994 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13995 return add_stmt (stmt);
13998 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13999 down to the element type of an array. If ORIG_QUAL_TYPE is not
14000 NULL, then it should be used as the qualified type
14001 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14002 preserve information about the typedef name from which an array
14003 type was derived). */
14005 tree
14006 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14007 size_t orig_qual_indirect)
14009 if (type == error_mark_node)
14010 return type;
14012 if (TREE_CODE (type) == ARRAY_TYPE)
14014 tree t;
14015 tree element_type = c_build_qualified_type (TREE_TYPE (type),
14016 type_quals, orig_qual_type,
14017 orig_qual_indirect - 1);
14019 /* See if we already have an identically qualified type. */
14020 if (orig_qual_type && orig_qual_indirect == 0)
14021 t = orig_qual_type;
14022 else
14023 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14025 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14026 && TYPE_NAME (t) == TYPE_NAME (type)
14027 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14028 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14029 TYPE_ATTRIBUTES (type)))
14030 break;
14032 if (!t)
14034 tree domain = TYPE_DOMAIN (type);
14036 t = build_variant_type_copy (type);
14037 TREE_TYPE (t) = element_type;
14039 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14040 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14041 SET_TYPE_STRUCTURAL_EQUALITY (t);
14042 else if (TYPE_CANONICAL (element_type) != element_type
14043 || (domain && TYPE_CANONICAL (domain) != domain))
14045 tree unqualified_canon
14046 = build_array_type (TYPE_CANONICAL (element_type),
14047 domain? TYPE_CANONICAL (domain)
14048 : NULL_TREE);
14049 if (TYPE_REVERSE_STORAGE_ORDER (type))
14051 unqualified_canon
14052 = build_distinct_type_copy (unqualified_canon);
14053 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14055 TYPE_CANONICAL (t)
14056 = c_build_qualified_type (unqualified_canon, type_quals);
14058 else
14059 TYPE_CANONICAL (t) = t;
14061 return t;
14064 /* A restrict-qualified pointer type must be a pointer to object or
14065 incomplete type. Note that the use of POINTER_TYPE_P also allows
14066 REFERENCE_TYPEs, which is appropriate for C++. */
14067 if ((type_quals & TYPE_QUAL_RESTRICT)
14068 && (!POINTER_TYPE_P (type)
14069 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14071 error ("invalid use of %<restrict%>");
14072 type_quals &= ~TYPE_QUAL_RESTRICT;
14075 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14076 ? orig_qual_type
14077 : build_qualified_type (type, type_quals));
14078 /* A variant type does not inherit the list of incomplete vars from the
14079 type main variant. */
14080 if (RECORD_OR_UNION_TYPE_P (var_type)
14081 && TYPE_MAIN_VARIANT (var_type) != var_type)
14082 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14083 return var_type;
14086 /* Build a VA_ARG_EXPR for the C parser. */
14088 tree
14089 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
14091 if (error_operand_p (type))
14092 return error_mark_node;
14093 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14094 order because it takes the address of the expression. */
14095 else if (handled_component_p (expr)
14096 && reverse_storage_order_for_component_p (expr))
14098 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14099 return error_mark_node;
14101 else if (!COMPLETE_TYPE_P (type))
14103 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
14104 "type %qT", type);
14105 return error_mark_node;
14107 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14108 warning_at (loc2, OPT_Wc___compat,
14109 "C++ requires promoted type, not enum type, in %<va_arg%>");
14110 return build_va_arg (loc2, expr, type);
14113 /* Return truthvalue of whether T1 is the same tree structure as T2.
14114 Return 1 if they are the same. Return false if they are different. */
14116 bool
14117 c_tree_equal (tree t1, tree t2)
14119 enum tree_code code1, code2;
14121 if (t1 == t2)
14122 return true;
14123 if (!t1 || !t2)
14124 return false;
14126 for (code1 = TREE_CODE (t1);
14127 CONVERT_EXPR_CODE_P (code1)
14128 || code1 == NON_LVALUE_EXPR;
14129 code1 = TREE_CODE (t1))
14130 t1 = TREE_OPERAND (t1, 0);
14131 for (code2 = TREE_CODE (t2);
14132 CONVERT_EXPR_CODE_P (code2)
14133 || code2 == NON_LVALUE_EXPR;
14134 code2 = TREE_CODE (t2))
14135 t2 = TREE_OPERAND (t2, 0);
14137 /* They might have become equal now. */
14138 if (t1 == t2)
14139 return true;
14141 if (code1 != code2)
14142 return false;
14144 switch (code1)
14146 case INTEGER_CST:
14147 return wi::to_wide (t1) == wi::to_wide (t2);
14149 case REAL_CST:
14150 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14152 case STRING_CST:
14153 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14154 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14155 TREE_STRING_LENGTH (t1));
14157 case FIXED_CST:
14158 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14159 TREE_FIXED_CST (t2));
14161 case COMPLEX_CST:
14162 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14163 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14165 case VECTOR_CST:
14166 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14168 case CONSTRUCTOR:
14169 /* We need to do this when determining whether or not two
14170 non-type pointer to member function template arguments
14171 are the same. */
14172 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14173 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14174 return false;
14176 tree field, value;
14177 unsigned int i;
14178 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14180 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14181 if (!c_tree_equal (field, elt2->index)
14182 || !c_tree_equal (value, elt2->value))
14183 return false;
14186 return true;
14188 case TREE_LIST:
14189 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14190 return false;
14191 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14192 return false;
14193 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14195 case SAVE_EXPR:
14196 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14198 case CALL_EXPR:
14200 tree arg1, arg2;
14201 call_expr_arg_iterator iter1, iter2;
14202 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14203 return false;
14204 for (arg1 = first_call_expr_arg (t1, &iter1),
14205 arg2 = first_call_expr_arg (t2, &iter2);
14206 arg1 && arg2;
14207 arg1 = next_call_expr_arg (&iter1),
14208 arg2 = next_call_expr_arg (&iter2))
14209 if (!c_tree_equal (arg1, arg2))
14210 return false;
14211 if (arg1 || arg2)
14212 return false;
14213 return true;
14216 case TARGET_EXPR:
14218 tree o1 = TREE_OPERAND (t1, 0);
14219 tree o2 = TREE_OPERAND (t2, 0);
14221 /* Special case: if either target is an unallocated VAR_DECL,
14222 it means that it's going to be unified with whatever the
14223 TARGET_EXPR is really supposed to initialize, so treat it
14224 as being equivalent to anything. */
14225 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14226 && !DECL_RTL_SET_P (o1))
14227 /*Nop*/;
14228 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14229 && !DECL_RTL_SET_P (o2))
14230 /*Nop*/;
14231 else if (!c_tree_equal (o1, o2))
14232 return false;
14234 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14237 case COMPONENT_REF:
14238 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14239 return false;
14240 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14242 case PARM_DECL:
14243 case VAR_DECL:
14244 case CONST_DECL:
14245 case FIELD_DECL:
14246 case FUNCTION_DECL:
14247 case IDENTIFIER_NODE:
14248 case SSA_NAME:
14249 return false;
14251 case TREE_VEC:
14253 unsigned ix;
14254 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14255 return false;
14256 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14257 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14258 TREE_VEC_ELT (t2, ix)))
14259 return false;
14260 return true;
14263 default:
14264 break;
14267 switch (TREE_CODE_CLASS (code1))
14269 case tcc_unary:
14270 case tcc_binary:
14271 case tcc_comparison:
14272 case tcc_expression:
14273 case tcc_vl_exp:
14274 case tcc_reference:
14275 case tcc_statement:
14277 int i, n = TREE_OPERAND_LENGTH (t1);
14279 switch (code1)
14281 case PREINCREMENT_EXPR:
14282 case PREDECREMENT_EXPR:
14283 case POSTINCREMENT_EXPR:
14284 case POSTDECREMENT_EXPR:
14285 n = 1;
14286 break;
14287 case ARRAY_REF:
14288 n = 2;
14289 break;
14290 default:
14291 break;
14294 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14295 && n != TREE_OPERAND_LENGTH (t2))
14296 return false;
14298 for (i = 0; i < n; ++i)
14299 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14300 return false;
14302 return true;
14305 case tcc_type:
14306 return comptypes (t1, t2);
14307 default:
14308 gcc_unreachable ();
14310 /* We can get here with --disable-checking. */
14311 return false;
14314 /* Returns true when the function declaration FNDECL is implicit,
14315 introduced as a result of a call to an otherwise undeclared
14316 function, and false otherwise. */
14318 bool
14319 c_decl_implicit (const_tree fndecl)
14321 return C_DECL_IMPLICIT (fndecl);