[PATCH] Line map table allocation
[official-gcc.git] / gcc / c / c-typeck.c
blob2e9338e3458b4ccfc07fb5f9a21795321bf2167d
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2018 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, 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 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1181 enum_and_int_p,
1182 different_types_p)) == 0)
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 = (known_eq (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 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1666 enum_and_int_p, different_types_p) != 1)
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 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1676 enum_and_int_p, different_types_p) != 1)
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, bool in_init)
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 && !error_operand_p (DECL_INITIAL (decl))
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 && (in_init || 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, false) : 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 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2125 TYPE_PRECISION (integer_type_node)) < 0)
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 Duplicate field checking replaces duplicates with NULL_TREE so
2213 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2214 case just iterate using DECL_CHAIN. */
2216 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2217 && !seen_error ())
2219 int bot, top, half;
2220 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2222 field = TYPE_FIELDS (type);
2223 bot = 0;
2224 top = TYPE_LANG_SPECIFIC (type)->s->len;
2225 while (top - bot > 1)
2227 half = (top - bot + 1) >> 1;
2228 field = field_array[bot+half];
2230 if (DECL_NAME (field) == NULL_TREE)
2232 /* Step through all anon unions in linear fashion. */
2233 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2235 field = field_array[bot++];
2236 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2238 tree anon = lookup_field (TREE_TYPE (field), component);
2240 if (anon)
2241 return tree_cons (NULL_TREE, field, anon);
2243 /* The Plan 9 compiler permits referring
2244 directly to an anonymous struct/union field
2245 using a typedef name. */
2246 if (flag_plan9_extensions
2247 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2248 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2249 == TYPE_DECL)
2250 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2251 == component))
2252 break;
2256 /* Entire record is only anon unions. */
2257 if (bot > top)
2258 return NULL_TREE;
2260 /* Restart the binary search, with new lower bound. */
2261 continue;
2264 if (DECL_NAME (field) == component)
2265 break;
2266 if (DECL_NAME (field) < component)
2267 bot += half;
2268 else
2269 top = bot + half;
2272 if (DECL_NAME (field_array[bot]) == component)
2273 field = field_array[bot];
2274 else if (DECL_NAME (field) != component)
2275 return NULL_TREE;
2277 else
2279 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2281 if (DECL_NAME (field) == NULL_TREE
2282 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2284 tree anon = lookup_field (TREE_TYPE (field), component);
2286 if (anon)
2287 return tree_cons (NULL_TREE, field, anon);
2289 /* The Plan 9 compiler permits referring directly to an
2290 anonymous struct/union field using a typedef
2291 name. */
2292 if (flag_plan9_extensions
2293 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2294 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2295 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2296 == component))
2297 break;
2300 if (DECL_NAME (field) == component)
2301 break;
2304 if (field == NULL_TREE)
2305 return NULL_TREE;
2308 return tree_cons (NULL_TREE, field, NULL_TREE);
2311 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2313 static void
2314 lookup_field_fuzzy_find_candidates (tree type, tree component,
2315 vec<tree> *candidates)
2317 tree field;
2318 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2320 if (DECL_NAME (field) == NULL_TREE
2321 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2322 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2323 candidates);
2325 if (DECL_NAME (field))
2326 candidates->safe_push (DECL_NAME (field));
2330 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2331 rather than returning a TREE_LIST for an exact match. */
2333 static tree
2334 lookup_field_fuzzy (tree type, tree component)
2336 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2338 /* First, gather a list of candidates. */
2339 auto_vec <tree> candidates;
2341 lookup_field_fuzzy_find_candidates (type, component,
2342 &candidates);
2344 return find_closest_identifier (component, &candidates);
2347 /* Support function for build_component_ref's error-handling.
2349 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2350 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2352 static bool
2353 should_suggest_deref_p (tree datum_type)
2355 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2356 allows "." for ptrs; we could be handling a failed attempt
2357 to access a property. */
2358 if (c_dialect_objc ())
2359 return false;
2361 /* Only suggest it for pointers... */
2362 if (TREE_CODE (datum_type) != POINTER_TYPE)
2363 return false;
2365 /* ...to structs/unions. */
2366 tree underlying_type = TREE_TYPE (datum_type);
2367 enum tree_code code = TREE_CODE (underlying_type);
2368 if (code == RECORD_TYPE || code == UNION_TYPE)
2369 return true;
2370 else
2371 return false;
2374 /* Make an expression to refer to the COMPONENT field of structure or
2375 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2376 location of the COMPONENT_REF. COMPONENT_LOC is the location
2377 of COMPONENT. */
2379 tree
2380 build_component_ref (location_t loc, tree datum, tree component,
2381 location_t component_loc)
2383 tree type = TREE_TYPE (datum);
2384 enum tree_code code = TREE_CODE (type);
2385 tree field = NULL;
2386 tree ref;
2387 bool datum_lvalue = lvalue_p (datum);
2389 if (!objc_is_public (datum, component))
2390 return error_mark_node;
2392 /* Detect Objective-C property syntax object.property. */
2393 if (c_dialect_objc ()
2394 && (ref = objc_maybe_build_component_ref (datum, component)))
2395 return ref;
2397 /* See if there is a field or component with name COMPONENT. */
2399 if (code == RECORD_TYPE || code == UNION_TYPE)
2401 if (!COMPLETE_TYPE_P (type))
2403 c_incomplete_type_error (loc, NULL_TREE, type);
2404 return error_mark_node;
2407 field = lookup_field (type, component);
2409 if (!field)
2411 tree guessed_id = lookup_field_fuzzy (type, component);
2412 if (guessed_id)
2414 /* Attempt to provide a fixit replacement hint, if
2415 we have a valid range for the component. */
2416 location_t reported_loc
2417 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2418 gcc_rich_location rich_loc (reported_loc);
2419 if (component_loc != UNKNOWN_LOCATION)
2420 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2421 error_at (&rich_loc,
2422 "%qT has no member named %qE; did you mean %qE?",
2423 type, component, guessed_id);
2425 else
2426 error_at (loc, "%qT has no member named %qE", type, component);
2427 return error_mark_node;
2430 /* Accessing elements of atomic structures or unions is undefined
2431 behavior (C11 6.5.2.3#5). */
2432 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2434 if (code == RECORD_TYPE)
2435 warning_at (loc, 0, "accessing a member %qE of an atomic "
2436 "structure %qE", component, datum);
2437 else
2438 warning_at (loc, 0, "accessing a member %qE of an atomic "
2439 "union %qE", component, datum);
2442 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2443 This might be better solved in future the way the C++ front
2444 end does it - by giving the anonymous entities each a
2445 separate name and type, and then have build_component_ref
2446 recursively call itself. We can't do that here. */
2449 tree subdatum = TREE_VALUE (field);
2450 int quals;
2451 tree subtype;
2452 bool use_datum_quals;
2454 if (TREE_TYPE (subdatum) == error_mark_node)
2455 return error_mark_node;
2457 /* If this is an rvalue, it does not have qualifiers in C
2458 standard terms and we must avoid propagating such
2459 qualifiers down to a non-lvalue array that is then
2460 converted to a pointer. */
2461 use_datum_quals = (datum_lvalue
2462 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2464 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2465 if (use_datum_quals)
2466 quals |= TYPE_QUALS (TREE_TYPE (datum));
2467 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2469 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2470 NULL_TREE);
2471 SET_EXPR_LOCATION (ref, loc);
2472 if (TREE_READONLY (subdatum)
2473 || (use_datum_quals && TREE_READONLY (datum)))
2474 TREE_READONLY (ref) = 1;
2475 if (TREE_THIS_VOLATILE (subdatum)
2476 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2477 TREE_THIS_VOLATILE (ref) = 1;
2479 if (TREE_DEPRECATED (subdatum))
2480 warn_deprecated_use (subdatum, NULL_TREE);
2482 datum = ref;
2484 field = TREE_CHAIN (field);
2486 while (field);
2488 return ref;
2490 else if (should_suggest_deref_p (type))
2492 /* Special-case the error message for "ptr.field" for the case
2493 where the user has confused "." vs "->". */
2494 rich_location richloc (line_table, loc);
2495 /* "loc" should be the "." token. */
2496 richloc.add_fixit_replace ("->");
2497 error_at (&richloc,
2498 "%qE is a pointer; did you mean to use %<->%>?",
2499 datum);
2500 return error_mark_node;
2502 else if (code != ERROR_MARK)
2503 error_at (loc,
2504 "request for member %qE in something not a structure or union",
2505 component);
2507 return error_mark_node;
2510 /* Given an expression PTR for a pointer, return an expression
2511 for the value pointed to.
2512 ERRORSTRING is the name of the operator to appear in error messages.
2514 LOC is the location to use for the generated tree. */
2516 tree
2517 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2519 tree pointer = default_conversion (ptr);
2520 tree type = TREE_TYPE (pointer);
2521 tree ref;
2523 if (TREE_CODE (type) == POINTER_TYPE)
2525 if (CONVERT_EXPR_P (pointer)
2526 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2528 /* If a warning is issued, mark it to avoid duplicates from
2529 the backend. This only needs to be done at
2530 warn_strict_aliasing > 2. */
2531 if (warn_strict_aliasing > 2)
2532 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2533 type, TREE_OPERAND (pointer, 0)))
2534 TREE_NO_WARNING (pointer) = 1;
2537 if (TREE_CODE (pointer) == ADDR_EXPR
2538 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2539 == TREE_TYPE (type)))
2541 ref = TREE_OPERAND (pointer, 0);
2542 protected_set_expr_location (ref, loc);
2543 return ref;
2545 else
2547 tree t = TREE_TYPE (type);
2549 ref = build1 (INDIRECT_REF, t, pointer);
2551 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2553 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2555 error_at (loc, "dereferencing pointer to incomplete type "
2556 "%qT", t);
2557 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2559 return error_mark_node;
2561 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2562 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2564 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2565 so that we get the proper error message if the result is used
2566 to assign to. Also, &* is supposed to be a no-op.
2567 And ANSI C seems to specify that the type of the result
2568 should be the const type. */
2569 /* A de-reference of a pointer to const is not a const. It is valid
2570 to change it via some other pointer. */
2571 TREE_READONLY (ref) = TYPE_READONLY (t);
2572 TREE_SIDE_EFFECTS (ref)
2573 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2574 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2575 protected_set_expr_location (ref, loc);
2576 return ref;
2579 else if (TREE_CODE (pointer) != ERROR_MARK)
2580 invalid_indirection_error (loc, type, errstring);
2582 return error_mark_node;
2585 /* This handles expressions of the form "a[i]", which denotes
2586 an array reference.
2588 This is logically equivalent in C to *(a+i), but we may do it differently.
2589 If A is a variable or a member, we generate a primitive ARRAY_REF.
2590 This avoids forcing the array out of registers, and can work on
2591 arrays that are not lvalues (for example, members of structures returned
2592 by functions).
2594 For vector types, allow vector[i] but not i[vector], and create
2595 *(((type*)&vectortype) + i) for the expression.
2597 LOC is the location to use for the returned expression. */
2599 tree
2600 build_array_ref (location_t loc, tree array, tree index)
2602 tree ret;
2603 bool swapped = false;
2604 if (TREE_TYPE (array) == error_mark_node
2605 || TREE_TYPE (index) == error_mark_node)
2606 return error_mark_node;
2608 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2609 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2610 /* Allow vector[index] but not index[vector]. */
2611 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2613 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2614 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2616 error_at (loc,
2617 "subscripted value is neither array nor pointer nor vector");
2619 return error_mark_node;
2621 std::swap (array, index);
2622 swapped = true;
2625 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2627 error_at (loc, "array subscript is not an integer");
2628 return error_mark_node;
2631 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2633 error_at (loc, "subscripted value is pointer to function");
2634 return error_mark_node;
2637 /* ??? Existing practice has been to warn only when the char
2638 index is syntactically the index, not for char[array]. */
2639 if (!swapped)
2640 warn_array_subscript_with_type_char (loc, index);
2642 /* Apply default promotions *after* noticing character types. */
2643 index = default_conversion (index);
2644 if (index == error_mark_node)
2645 return error_mark_node;
2647 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2649 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2650 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2652 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2654 tree rval, type;
2656 /* An array that is indexed by a non-constant
2657 cannot be stored in a register; we must be able to do
2658 address arithmetic on its address.
2659 Likewise an array of elements of variable size. */
2660 if (TREE_CODE (index) != INTEGER_CST
2661 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2662 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2664 if (!c_mark_addressable (array, true))
2665 return error_mark_node;
2667 /* An array that is indexed by a constant value which is not within
2668 the array bounds cannot be stored in a register either; because we
2669 would get a crash in store_bit_field/extract_bit_field when trying
2670 to access a non-existent part of the register. */
2671 if (TREE_CODE (index) == INTEGER_CST
2672 && TYPE_DOMAIN (TREE_TYPE (array))
2673 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2675 if (!c_mark_addressable (array))
2676 return error_mark_node;
2679 if ((pedantic || warn_c90_c99_compat)
2680 && ! was_vector)
2682 tree foo = array;
2683 while (TREE_CODE (foo) == COMPONENT_REF)
2684 foo = TREE_OPERAND (foo, 0);
2685 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2686 pedwarn (loc, OPT_Wpedantic,
2687 "ISO C forbids subscripting %<register%> array");
2688 else if (!lvalue_p (foo))
2689 pedwarn_c90 (loc, OPT_Wpedantic,
2690 "ISO C90 forbids subscripting non-lvalue "
2691 "array");
2694 type = TREE_TYPE (TREE_TYPE (array));
2695 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2696 /* Array ref is const/volatile if the array elements are
2697 or if the array is. */
2698 TREE_READONLY (rval)
2699 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2700 | TREE_READONLY (array));
2701 TREE_SIDE_EFFECTS (rval)
2702 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2703 | TREE_SIDE_EFFECTS (array));
2704 TREE_THIS_VOLATILE (rval)
2705 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2706 /* This was added by rms on 16 Nov 91.
2707 It fixes vol struct foo *a; a->elts[1]
2708 in an inline function.
2709 Hope it doesn't break something else. */
2710 | TREE_THIS_VOLATILE (array));
2711 ret = require_complete_type (loc, rval);
2712 protected_set_expr_location (ret, loc);
2713 if (non_lvalue)
2714 ret = non_lvalue_loc (loc, ret);
2715 return ret;
2717 else
2719 tree ar = default_conversion (array);
2721 if (ar == error_mark_node)
2722 return ar;
2724 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2725 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2727 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2728 index, false),
2729 RO_ARRAY_INDEXING);
2730 if (non_lvalue)
2731 ret = non_lvalue_loc (loc, ret);
2732 return ret;
2736 /* Build an external reference to identifier ID. FUN indicates
2737 whether this will be used for a function call. LOC is the source
2738 location of the identifier. This sets *TYPE to the type of the
2739 identifier, which is not the same as the type of the returned value
2740 for CONST_DECLs defined as enum constants. If the type of the
2741 identifier is not available, *TYPE is set to NULL. */
2742 tree
2743 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2745 tree ref;
2746 tree decl = lookup_name (id);
2748 /* In Objective-C, an instance variable (ivar) may be preferred to
2749 whatever lookup_name() found. */
2750 decl = objc_lookup_ivar (decl, id);
2752 *type = NULL;
2753 if (decl && decl != error_mark_node)
2755 ref = decl;
2756 *type = TREE_TYPE (ref);
2758 else if (fun)
2759 /* Implicit function declaration. */
2760 ref = implicitly_declare (loc, id);
2761 else if (decl == error_mark_node)
2762 /* Don't complain about something that's already been
2763 complained about. */
2764 return error_mark_node;
2765 else
2767 undeclared_variable (loc, id);
2768 return error_mark_node;
2771 if (TREE_TYPE (ref) == error_mark_node)
2772 return error_mark_node;
2774 if (TREE_DEPRECATED (ref))
2775 warn_deprecated_use (ref, NULL_TREE);
2777 /* Recursive call does not count as usage. */
2778 if (ref != current_function_decl)
2780 TREE_USED (ref) = 1;
2783 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2785 if (!in_sizeof && !in_typeof)
2786 C_DECL_USED (ref) = 1;
2787 else if (DECL_INITIAL (ref) == NULL_TREE
2788 && DECL_EXTERNAL (ref)
2789 && !TREE_PUBLIC (ref))
2790 record_maybe_used_decl (ref);
2793 if (TREE_CODE (ref) == CONST_DECL)
2795 used_types_insert (TREE_TYPE (ref));
2797 if (warn_cxx_compat
2798 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2799 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2801 warning_at (loc, OPT_Wc___compat,
2802 ("enum constant defined in struct or union "
2803 "is not visible in C++"));
2804 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2807 ref = DECL_INITIAL (ref);
2808 TREE_CONSTANT (ref) = 1;
2810 else if (current_function_decl != NULL_TREE
2811 && !DECL_FILE_SCOPE_P (current_function_decl)
2812 && (VAR_OR_FUNCTION_DECL_P (ref)
2813 || TREE_CODE (ref) == PARM_DECL))
2815 tree context = decl_function_context (ref);
2817 if (context != NULL_TREE && context != current_function_decl)
2818 DECL_NONLOCAL (ref) = 1;
2820 /* C99 6.7.4p3: An inline definition of a function with external
2821 linkage ... shall not contain a reference to an identifier with
2822 internal linkage. */
2823 else if (current_function_decl != NULL_TREE
2824 && DECL_DECLARED_INLINE_P (current_function_decl)
2825 && DECL_EXTERNAL (current_function_decl)
2826 && VAR_OR_FUNCTION_DECL_P (ref)
2827 && (!VAR_P (ref) || TREE_STATIC (ref))
2828 && ! TREE_PUBLIC (ref)
2829 && DECL_CONTEXT (ref) != current_function_decl)
2830 record_inline_static (loc, current_function_decl, ref,
2831 csi_internal);
2833 return ref;
2836 /* Record details of decls possibly used inside sizeof or typeof. */
2837 struct maybe_used_decl
2839 /* The decl. */
2840 tree decl;
2841 /* The level seen at (in_sizeof + in_typeof). */
2842 int level;
2843 /* The next one at this level or above, or NULL. */
2844 struct maybe_used_decl *next;
2847 static struct maybe_used_decl *maybe_used_decls;
2849 /* Record that DECL, an undefined static function reference seen
2850 inside sizeof or typeof, might be used if the operand of sizeof is
2851 a VLA type or the operand of typeof is a variably modified
2852 type. */
2854 static void
2855 record_maybe_used_decl (tree decl)
2857 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2858 t->decl = decl;
2859 t->level = in_sizeof + in_typeof;
2860 t->next = maybe_used_decls;
2861 maybe_used_decls = t;
2864 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2865 USED is false, just discard them. If it is true, mark them used
2866 (if no longer inside sizeof or typeof) or move them to the next
2867 level up (if still inside sizeof or typeof). */
2869 void
2870 pop_maybe_used (bool used)
2872 struct maybe_used_decl *p = maybe_used_decls;
2873 int cur_level = in_sizeof + in_typeof;
2874 while (p && p->level > cur_level)
2876 if (used)
2878 if (cur_level == 0)
2879 C_DECL_USED (p->decl) = 1;
2880 else
2881 p->level = cur_level;
2883 p = p->next;
2885 if (!used || cur_level == 0)
2886 maybe_used_decls = p;
2889 /* Return the result of sizeof applied to EXPR. */
2891 struct c_expr
2892 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2894 struct c_expr ret;
2895 if (expr.value == error_mark_node)
2897 ret.value = error_mark_node;
2898 ret.original_code = ERROR_MARK;
2899 ret.original_type = NULL;
2900 pop_maybe_used (false);
2902 else
2904 bool expr_const_operands = true;
2906 if (TREE_CODE (expr.value) == PARM_DECL
2907 && C_ARRAY_PARAMETER (expr.value))
2909 if (warning_at (loc, OPT_Wsizeof_array_argument,
2910 "%<sizeof%> on array function parameter %qE will "
2911 "return size of %qT", expr.value,
2912 TREE_TYPE (expr.value)))
2913 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2915 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2916 &expr_const_operands);
2917 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2918 c_last_sizeof_arg = expr.value;
2919 c_last_sizeof_loc = loc;
2920 ret.original_code = SIZEOF_EXPR;
2921 ret.original_type = NULL;
2922 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2924 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2925 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2926 folded_expr, ret.value);
2927 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2928 SET_EXPR_LOCATION (ret.value, loc);
2930 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2932 return ret;
2935 /* Return the result of sizeof applied to T, a structure for the type
2936 name passed to sizeof (rather than the type itself). LOC is the
2937 location of the original expression. */
2939 struct c_expr
2940 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2942 tree type;
2943 struct c_expr ret;
2944 tree type_expr = NULL_TREE;
2945 bool type_expr_const = true;
2946 type = groktypename (t, &type_expr, &type_expr_const);
2947 ret.value = c_sizeof (loc, type);
2948 c_last_sizeof_arg = type;
2949 c_last_sizeof_loc = loc;
2950 ret.original_code = SIZEOF_EXPR;
2951 ret.original_type = NULL;
2952 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2953 && c_vla_type_p (type))
2955 /* If the type is a [*] array, it is a VLA but is represented as
2956 having a size of zero. In such a case we must ensure that
2957 the result of sizeof does not get folded to a constant by
2958 c_fully_fold, because if the size is evaluated the result is
2959 not constant and so constraints on zero or negative size
2960 arrays must not be applied when this sizeof call is inside
2961 another array declarator. */
2962 if (!type_expr)
2963 type_expr = integer_zero_node;
2964 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2965 type_expr, ret.value);
2966 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2968 pop_maybe_used (type != error_mark_node
2969 ? C_TYPE_VARIABLE_SIZE (type) : false);
2970 return ret;
2973 /* Build a function call to function FUNCTION with parameters PARAMS.
2974 The function call is at LOC.
2975 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2976 TREE_VALUE of each node is a parameter-expression.
2977 FUNCTION's data type may be a function type or a pointer-to-function. */
2979 tree
2980 build_function_call (location_t loc, tree function, tree params)
2982 vec<tree, va_gc> *v;
2983 tree ret;
2985 vec_alloc (v, list_length (params));
2986 for (; params; params = TREE_CHAIN (params))
2987 v->quick_push (TREE_VALUE (params));
2988 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2989 vec_free (v);
2990 return ret;
2993 /* Give a note about the location of the declaration of DECL. */
2995 static void
2996 inform_declaration (tree decl)
2998 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2999 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3002 /* Build a function call to function FUNCTION with parameters PARAMS.
3003 ORIGTYPES, if not NULL, is a vector of types; each element is
3004 either NULL or the original type of the corresponding element in
3005 PARAMS. The original type may differ from TREE_TYPE of the
3006 parameter for enums. FUNCTION's data type may be a function type
3007 or pointer-to-function. This function changes the elements of
3008 PARAMS. */
3010 tree
3011 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3012 tree function, vec<tree, va_gc> *params,
3013 vec<tree, va_gc> *origtypes)
3015 tree fntype, fundecl = NULL_TREE;
3016 tree name = NULL_TREE, result;
3017 tree tem;
3018 int nargs;
3019 tree *argarray;
3022 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3023 STRIP_TYPE_NOPS (function);
3025 /* Convert anything with function type to a pointer-to-function. */
3026 if (TREE_CODE (function) == FUNCTION_DECL)
3028 name = DECL_NAME (function);
3030 if (flag_tm)
3031 tm_malloc_replacement (function);
3032 fundecl = function;
3033 /* Atomic functions have type checking/casting already done. They are
3034 often rewritten and don't match the original parameter list. */
3035 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3036 origtypes = NULL;
3038 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3039 function = function_to_pointer_conversion (loc, function);
3041 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3042 expressions, like those used for ObjC messenger dispatches. */
3043 if (params && !params->is_empty ())
3044 function = objc_rewrite_function_call (function, (*params)[0]);
3046 function = c_fully_fold (function, false, NULL);
3048 fntype = TREE_TYPE (function);
3050 if (TREE_CODE (fntype) == ERROR_MARK)
3051 return error_mark_node;
3053 if (!(TREE_CODE (fntype) == POINTER_TYPE
3054 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3056 if (!flag_diagnostics_show_caret)
3057 error_at (loc,
3058 "called object %qE is not a function or function pointer",
3059 function);
3060 else if (DECL_P (function))
3062 error_at (loc,
3063 "called object %qD is not a function or function pointer",
3064 function);
3065 inform_declaration (function);
3067 else
3068 error_at (loc,
3069 "called object is not a function or function pointer");
3070 return error_mark_node;
3073 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3074 current_function_returns_abnormally = 1;
3076 /* fntype now gets the type of function pointed to. */
3077 fntype = TREE_TYPE (fntype);
3079 /* Convert the parameters to the types declared in the
3080 function prototype, or apply default promotions. */
3082 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3083 origtypes, function, fundecl);
3084 if (nargs < 0)
3085 return error_mark_node;
3087 /* Check that the function is called through a compatible prototype.
3088 If it is not, warn. */
3089 if (CONVERT_EXPR_P (function)
3090 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3091 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3092 && !comptypes (fntype, TREE_TYPE (tem)))
3094 tree return_type = TREE_TYPE (fntype);
3096 /* This situation leads to run-time undefined behavior. We can't,
3097 therefore, simply error unless we can prove that all possible
3098 executions of the program must execute the code. */
3099 warning_at (loc, 0, "function called through a non-compatible type");
3101 if (VOID_TYPE_P (return_type)
3102 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3103 pedwarn (loc, 0,
3104 "function with qualified void return type called");
3107 argarray = vec_safe_address (params);
3109 /* Check that arguments to builtin functions match the expectations. */
3110 if (fundecl
3111 && DECL_BUILT_IN (fundecl)
3112 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3113 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3114 argarray))
3115 return error_mark_node;
3117 /* Check that the arguments to the function are valid. */
3118 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3119 nargs, argarray, &arg_loc);
3121 if (name != NULL_TREE
3122 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3124 if (require_constant_value)
3125 result
3126 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3127 function, nargs, argarray);
3128 else
3129 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3130 function, nargs, argarray);
3131 if (TREE_CODE (result) == NOP_EXPR
3132 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3133 STRIP_TYPE_NOPS (result);
3135 else
3136 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3137 function, nargs, argarray);
3138 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3139 later. */
3140 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3141 TREE_NO_WARNING (result) = 1;
3143 /* In this improbable scenario, a nested function returns a VM type.
3144 Create a TARGET_EXPR so that the call always has a LHS, much as
3145 what the C++ FE does for functions returning non-PODs. */
3146 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3148 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3149 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3150 NULL_TREE, NULL_TREE);
3153 if (VOID_TYPE_P (TREE_TYPE (result)))
3155 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3156 pedwarn (loc, 0,
3157 "function with qualified void return type called");
3158 return result;
3160 return require_complete_type (loc, result);
3163 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3165 tree
3166 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3167 tree function, vec<tree, va_gc> *params,
3168 vec<tree, va_gc> *origtypes)
3170 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3171 STRIP_TYPE_NOPS (function);
3173 /* Convert anything with function type to a pointer-to-function. */
3174 if (TREE_CODE (function) == FUNCTION_DECL)
3176 /* Implement type-directed function overloading for builtins.
3177 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3178 handle all the type checking. The result is a complete expression
3179 that implements this function call. */
3180 tree tem = resolve_overloaded_builtin (loc, function, params);
3181 if (tem)
3182 return tem;
3184 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3187 /* Convert the argument expressions in the vector VALUES
3188 to the types in the list TYPELIST.
3190 If TYPELIST is exhausted, or when an element has NULL as its type,
3191 perform the default conversions.
3193 ORIGTYPES is the original types of the expressions in VALUES. This
3194 holds the type of enum values which have been converted to integral
3195 types. It may be NULL.
3197 FUNCTION is a tree for the called function. It is used only for
3198 error messages, where it is formatted with %qE.
3200 This is also where warnings about wrong number of args are generated.
3202 ARG_LOC are locations of function arguments (if any).
3204 Returns the actual number of arguments processed (which may be less
3205 than the length of VALUES in some error situations), or -1 on
3206 failure. */
3208 static int
3209 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3210 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3211 tree function, tree fundecl)
3213 tree typetail, val;
3214 unsigned int parmnum;
3215 bool error_args = false;
3216 const bool type_generic = fundecl
3217 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3218 bool type_generic_remove_excess_precision = false;
3219 bool type_generic_overflow_p = false;
3220 tree selector;
3222 /* Change pointer to function to the function itself for
3223 diagnostics. */
3224 if (TREE_CODE (function) == ADDR_EXPR
3225 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3226 function = TREE_OPERAND (function, 0);
3228 /* Handle an ObjC selector specially for diagnostics. */
3229 selector = objc_message_selector ();
3231 /* For type-generic built-in functions, determine whether excess
3232 precision should be removed (classification) or not
3233 (comparison). */
3234 if (type_generic
3235 && DECL_BUILT_IN (fundecl)
3236 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3238 switch (DECL_FUNCTION_CODE (fundecl))
3240 case BUILT_IN_ISFINITE:
3241 case BUILT_IN_ISINF:
3242 case BUILT_IN_ISINF_SIGN:
3243 case BUILT_IN_ISNAN:
3244 case BUILT_IN_ISNORMAL:
3245 case BUILT_IN_FPCLASSIFY:
3246 type_generic_remove_excess_precision = true;
3247 break;
3249 case BUILT_IN_ADD_OVERFLOW_P:
3250 case BUILT_IN_SUB_OVERFLOW_P:
3251 case BUILT_IN_MUL_OVERFLOW_P:
3252 /* The last argument of these type-generic builtins
3253 should not be promoted. */
3254 type_generic_overflow_p = true;
3255 break;
3257 default:
3258 break;
3262 /* Scan the given expressions and types, producing individual
3263 converted arguments. */
3265 for (typetail = typelist, parmnum = 0;
3266 values && values->iterate (parmnum, &val);
3267 ++parmnum)
3269 tree type = typetail ? TREE_VALUE (typetail) : 0;
3270 tree valtype = TREE_TYPE (val);
3271 tree rname = function;
3272 int argnum = parmnum + 1;
3273 const char *invalid_func_diag;
3274 bool excess_precision = false;
3275 bool npc;
3276 tree parmval;
3277 /* Some __atomic_* builtins have additional hidden argument at
3278 position 0. */
3279 location_t ploc
3280 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3281 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3282 : input_location;
3284 if (type == void_type_node)
3286 if (selector)
3287 error_at (loc, "too many arguments to method %qE", selector);
3288 else
3289 error_at (loc, "too many arguments to function %qE", function);
3290 inform_declaration (fundecl);
3291 return error_args ? -1 : (int) parmnum;
3294 if (selector && argnum > 2)
3296 rname = selector;
3297 argnum -= 2;
3300 npc = null_pointer_constant_p (val);
3302 /* If there is excess precision and a prototype, convert once to
3303 the required type rather than converting via the semantic
3304 type. Likewise without a prototype a float value represented
3305 as long double should be converted once to double. But for
3306 type-generic classification functions excess precision must
3307 be removed here. */
3308 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3309 && (type || !type_generic || !type_generic_remove_excess_precision))
3311 val = TREE_OPERAND (val, 0);
3312 excess_precision = true;
3314 val = c_fully_fold (val, false, NULL);
3315 STRIP_TYPE_NOPS (val);
3317 val = require_complete_type (ploc, val);
3319 /* Some floating-point arguments must be promoted to double when
3320 no type is specified by a prototype. This applies to
3321 arguments of type float, and to architecture-specific types
3322 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3323 bool promote_float_arg = false;
3324 if (type == NULL_TREE
3325 && TREE_CODE (valtype) == REAL_TYPE
3326 && (TYPE_PRECISION (valtype)
3327 <= TYPE_PRECISION (double_type_node))
3328 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3329 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3330 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3332 /* Promote this argument, unless it has a _FloatN or
3333 _FloatNx type. */
3334 promote_float_arg = true;
3335 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3336 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3338 promote_float_arg = false;
3339 break;
3343 if (type != NULL_TREE)
3345 /* Formal parm type is specified by a function prototype. */
3347 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3349 error_at (ploc, "type of formal parameter %d is incomplete",
3350 parmnum + 1);
3351 parmval = val;
3353 else
3355 tree origtype;
3357 /* Optionally warn about conversions that
3358 differ from the default conversions. */
3359 if (warn_traditional_conversion || warn_traditional)
3361 unsigned int formal_prec = TYPE_PRECISION (type);
3363 if (INTEGRAL_TYPE_P (type)
3364 && TREE_CODE (valtype) == REAL_TYPE)
3365 warning_at (ploc, OPT_Wtraditional_conversion,
3366 "passing argument %d of %qE as integer rather "
3367 "than floating due to prototype",
3368 argnum, rname);
3369 if (INTEGRAL_TYPE_P (type)
3370 && TREE_CODE (valtype) == COMPLEX_TYPE)
3371 warning_at (ploc, OPT_Wtraditional_conversion,
3372 "passing argument %d of %qE as integer rather "
3373 "than complex due to prototype",
3374 argnum, rname);
3375 else if (TREE_CODE (type) == COMPLEX_TYPE
3376 && TREE_CODE (valtype) == REAL_TYPE)
3377 warning_at (ploc, OPT_Wtraditional_conversion,
3378 "passing argument %d of %qE as complex rather "
3379 "than floating due to prototype",
3380 argnum, rname);
3381 else if (TREE_CODE (type) == REAL_TYPE
3382 && INTEGRAL_TYPE_P (valtype))
3383 warning_at (ploc, OPT_Wtraditional_conversion,
3384 "passing argument %d of %qE as floating rather "
3385 "than integer due to prototype",
3386 argnum, rname);
3387 else if (TREE_CODE (type) == COMPLEX_TYPE
3388 && INTEGRAL_TYPE_P (valtype))
3389 warning_at (ploc, OPT_Wtraditional_conversion,
3390 "passing argument %d of %qE as complex rather "
3391 "than integer due to prototype",
3392 argnum, rname);
3393 else if (TREE_CODE (type) == REAL_TYPE
3394 && TREE_CODE (valtype) == COMPLEX_TYPE)
3395 warning_at (ploc, OPT_Wtraditional_conversion,
3396 "passing argument %d of %qE as floating rather "
3397 "than complex due to prototype",
3398 argnum, rname);
3399 /* ??? At some point, messages should be written about
3400 conversions between complex types, but that's too messy
3401 to do now. */
3402 else if (TREE_CODE (type) == REAL_TYPE
3403 && TREE_CODE (valtype) == REAL_TYPE)
3405 /* Warn if any argument is passed as `float',
3406 since without a prototype it would be `double'. */
3407 if (formal_prec == TYPE_PRECISION (float_type_node)
3408 && type != dfloat32_type_node)
3409 warning_at (ploc, 0,
3410 "passing argument %d of %qE as %<float%> "
3411 "rather than %<double%> due to prototype",
3412 argnum, rname);
3414 /* Warn if mismatch between argument and prototype
3415 for decimal float types. Warn of conversions with
3416 binary float types and of precision narrowing due to
3417 prototype. */
3418 else if (type != valtype
3419 && (type == dfloat32_type_node
3420 || type == dfloat64_type_node
3421 || type == dfloat128_type_node
3422 || valtype == dfloat32_type_node
3423 || valtype == dfloat64_type_node
3424 || valtype == dfloat128_type_node)
3425 && (formal_prec
3426 <= TYPE_PRECISION (valtype)
3427 || (type == dfloat128_type_node
3428 && (valtype
3429 != dfloat64_type_node
3430 && (valtype
3431 != dfloat32_type_node)))
3432 || (type == dfloat64_type_node
3433 && (valtype
3434 != dfloat32_type_node))))
3435 warning_at (ploc, 0,
3436 "passing argument %d of %qE as %qT "
3437 "rather than %qT due to prototype",
3438 argnum, rname, type, valtype);
3441 /* Detect integer changing in width or signedness.
3442 These warnings are only activated with
3443 -Wtraditional-conversion, not with -Wtraditional. */
3444 else if (warn_traditional_conversion
3445 && INTEGRAL_TYPE_P (type)
3446 && INTEGRAL_TYPE_P (valtype))
3448 tree would_have_been = default_conversion (val);
3449 tree type1 = TREE_TYPE (would_have_been);
3451 if (val == error_mark_node)
3452 /* VAL could have been of incomplete type. */;
3453 else if (TREE_CODE (type) == ENUMERAL_TYPE
3454 && (TYPE_MAIN_VARIANT (type)
3455 == TYPE_MAIN_VARIANT (valtype)))
3456 /* No warning if function asks for enum
3457 and the actual arg is that enum type. */
3459 else if (formal_prec != TYPE_PRECISION (type1))
3460 warning_at (ploc, OPT_Wtraditional_conversion,
3461 "passing argument %d of %qE "
3462 "with different width due to prototype",
3463 argnum, rname);
3464 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3466 /* Don't complain if the formal parameter type
3467 is an enum, because we can't tell now whether
3468 the value was an enum--even the same enum. */
3469 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3471 else if (TREE_CODE (val) == INTEGER_CST
3472 && int_fits_type_p (val, type))
3473 /* Change in signedness doesn't matter
3474 if a constant value is unaffected. */
3476 /* If the value is extended from a narrower
3477 unsigned type, it doesn't matter whether we
3478 pass it as signed or unsigned; the value
3479 certainly is the same either way. */
3480 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3481 && TYPE_UNSIGNED (valtype))
3483 else if (TYPE_UNSIGNED (type))
3484 warning_at (ploc, OPT_Wtraditional_conversion,
3485 "passing argument %d of %qE "
3486 "as unsigned due to prototype",
3487 argnum, rname);
3488 else
3489 warning_at (ploc, OPT_Wtraditional_conversion,
3490 "passing argument %d of %qE "
3491 "as signed due to prototype",
3492 argnum, rname);
3496 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3497 sake of better warnings from convert_and_check. */
3498 if (excess_precision)
3499 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3500 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3501 parmval = convert_for_assignment (loc, ploc, type,
3502 val, origtype, ic_argpass,
3503 npc, fundecl, function,
3504 parmnum + 1);
3506 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3507 && INTEGRAL_TYPE_P (type)
3508 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3509 parmval = default_conversion (parmval);
3512 else if (promote_float_arg)
3514 if (type_generic)
3515 parmval = val;
3516 else
3518 /* Convert `float' to `double'. */
3519 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3520 warning_at (ploc, OPT_Wdouble_promotion,
3521 "implicit conversion from %qT to %qT when passing "
3522 "argument to function",
3523 valtype, double_type_node);
3524 parmval = convert (double_type_node, val);
3527 else if ((excess_precision && !type_generic)
3528 || (type_generic_overflow_p && parmnum == 2))
3529 /* A "double" argument with excess precision being passed
3530 without a prototype or in variable arguments.
3531 The last argument of __builtin_*_overflow_p should not be
3532 promoted. */
3533 parmval = convert (valtype, val);
3534 else if ((invalid_func_diag =
3535 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3537 error (invalid_func_diag);
3538 return -1;
3540 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3542 return -1;
3544 else
3545 /* Convert `short' and `char' to full-size `int'. */
3546 parmval = default_conversion (val);
3548 (*values)[parmnum] = parmval;
3549 if (parmval == error_mark_node)
3550 error_args = true;
3552 if (typetail)
3553 typetail = TREE_CHAIN (typetail);
3556 gcc_assert (parmnum == vec_safe_length (values));
3558 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3560 error_at (loc, "too few arguments to function %qE", function);
3561 inform_declaration (fundecl);
3562 return -1;
3565 return error_args ? -1 : (int) parmnum;
3568 /* This is the entry point used by the parser to build unary operators
3569 in the input. CODE, a tree_code, specifies the unary operator, and
3570 ARG is the operand. For unary plus, the C parser currently uses
3571 CONVERT_EXPR for code.
3573 LOC is the location to use for the tree generated.
3576 struct c_expr
3577 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3579 struct c_expr result;
3581 result.original_code = code;
3582 result.original_type = NULL;
3584 if (reject_gcc_builtin (arg.value))
3586 result.value = error_mark_node;
3588 else
3590 result.value = build_unary_op (loc, code, arg.value, false);
3592 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3593 overflow_warning (loc, result.value, arg.value);
3596 /* We are typically called when parsing a prefix token at LOC acting on
3597 ARG. Reflect this by updating the source range of the result to
3598 start at LOC and end at the end of ARG. */
3599 set_c_expr_source_range (&result,
3600 loc, arg.get_finish ());
3602 return result;
3605 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3607 static bool
3608 char_type_p (tree type)
3610 return (type == char_type_node
3611 || type == unsigned_char_type_node
3612 || type == signed_char_type_node
3613 || type == char16_type_node
3614 || type == char32_type_node);
3617 /* This is the entry point used by the parser to build binary operators
3618 in the input. CODE, a tree_code, specifies the binary operator, and
3619 ARG1 and ARG2 are the operands. In addition to constructing the
3620 expression, we check for operands that were written with other binary
3621 operators in a way that is likely to confuse the user.
3623 LOCATION is the location of the binary operator. */
3625 struct c_expr
3626 parser_build_binary_op (location_t location, enum tree_code code,
3627 struct c_expr arg1, struct c_expr arg2)
3629 struct c_expr result;
3631 enum tree_code code1 = arg1.original_code;
3632 enum tree_code code2 = arg2.original_code;
3633 tree type1 = (arg1.original_type
3634 ? arg1.original_type
3635 : TREE_TYPE (arg1.value));
3636 tree type2 = (arg2.original_type
3637 ? arg2.original_type
3638 : TREE_TYPE (arg2.value));
3640 result.value = build_binary_op (location, code,
3641 arg1.value, arg2.value, true);
3642 result.original_code = code;
3643 result.original_type = NULL;
3645 if (TREE_CODE (result.value) == ERROR_MARK)
3647 set_c_expr_source_range (&result,
3648 arg1.get_start (),
3649 arg2.get_finish ());
3650 return result;
3653 if (location != UNKNOWN_LOCATION)
3654 protected_set_expr_location (result.value, location);
3656 set_c_expr_source_range (&result,
3657 arg1.get_start (),
3658 arg2.get_finish ());
3660 /* Check for cases such as x+y<<z which users are likely
3661 to misinterpret. */
3662 if (warn_parentheses)
3663 warn_about_parentheses (location, code, code1, arg1.value, code2,
3664 arg2.value);
3666 if (warn_logical_op)
3667 warn_logical_operator (location, code, TREE_TYPE (result.value),
3668 code1, arg1.value, code2, arg2.value);
3670 if (warn_tautological_compare)
3672 tree lhs = arg1.value;
3673 tree rhs = arg2.value;
3674 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3676 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3677 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3678 lhs = NULL_TREE;
3679 else
3680 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3682 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3684 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3685 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3686 rhs = NULL_TREE;
3687 else
3688 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3690 if (lhs != NULL_TREE && rhs != NULL_TREE)
3691 warn_tautological_cmp (location, code, lhs, rhs);
3694 if (warn_logical_not_paren
3695 && TREE_CODE_CLASS (code) == tcc_comparison
3696 && code1 == TRUTH_NOT_EXPR
3697 && code2 != TRUTH_NOT_EXPR
3698 /* Avoid warning for !!x == y. */
3699 && (TREE_CODE (arg1.value) != NE_EXPR
3700 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3702 /* Avoid warning for !b == y where b has _Bool type. */
3703 tree t = integer_zero_node;
3704 if (TREE_CODE (arg1.value) == EQ_EXPR
3705 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3706 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3708 t = TREE_OPERAND (arg1.value, 0);
3711 if (TREE_TYPE (t) != integer_type_node)
3712 break;
3713 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3714 t = C_MAYBE_CONST_EXPR_EXPR (t);
3715 else if (CONVERT_EXPR_P (t))
3716 t = TREE_OPERAND (t, 0);
3717 else
3718 break;
3720 while (1);
3722 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3723 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3726 /* Warn about comparisons against string literals, with the exception
3727 of testing for equality or inequality of a string literal with NULL. */
3728 if (code == EQ_EXPR || code == NE_EXPR)
3730 if ((code1 == STRING_CST
3731 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3732 || (code2 == STRING_CST
3733 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3734 warning_at (location, OPT_Waddress,
3735 "comparison with string literal results in unspecified behavior");
3736 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3737 if (POINTER_TYPE_P (type1)
3738 && null_pointer_constant_p (arg2.value)
3739 && char_type_p (type2)
3740 && warning_at (location, OPT_Wpointer_compare,
3741 "comparison between pointer and zero character "
3742 "constant"))
3743 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3744 else if (POINTER_TYPE_P (type2)
3745 && null_pointer_constant_p (arg1.value)
3746 && char_type_p (type1)
3747 && warning_at (location, OPT_Wpointer_compare,
3748 "comparison between pointer and zero character "
3749 "constant"))
3750 inform (arg2.get_start (), "did you mean to dereference the pointer?");
3752 else if (TREE_CODE_CLASS (code) == tcc_comparison
3753 && (code1 == STRING_CST || code2 == STRING_CST))
3754 warning_at (location, OPT_Waddress,
3755 "comparison with string literal results in unspecified behavior");
3757 if (TREE_OVERFLOW_P (result.value)
3758 && !TREE_OVERFLOW_P (arg1.value)
3759 && !TREE_OVERFLOW_P (arg2.value))
3760 overflow_warning (location, result.value);
3762 /* Warn about comparisons of different enum types. */
3763 if (warn_enum_compare
3764 && TREE_CODE_CLASS (code) == tcc_comparison
3765 && TREE_CODE (type1) == ENUMERAL_TYPE
3766 && TREE_CODE (type2) == ENUMERAL_TYPE
3767 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3768 warning_at (location, OPT_Wenum_compare,
3769 "comparison between %qT and %qT",
3770 type1, type2);
3772 return result;
3775 /* Return a tree for the difference of pointers OP0 and OP1.
3776 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3777 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3779 static tree
3780 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3782 tree restype = ptrdiff_type_node;
3783 tree result, inttype;
3785 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3786 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3787 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3788 tree orig_op1 = op1;
3790 /* If the operands point into different address spaces, we need to
3791 explicitly convert them to pointers into the common address space
3792 before we can subtract the numerical address values. */
3793 if (as0 != as1)
3795 addr_space_t as_common;
3796 tree common_type;
3798 /* Determine the common superset address space. This is guaranteed
3799 to exist because the caller verified that comp_target_types
3800 returned non-zero. */
3801 if (!addr_space_superset (as0, as1, &as_common))
3802 gcc_unreachable ();
3804 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3805 op0 = convert (common_type, op0);
3806 op1 = convert (common_type, op1);
3809 /* Determine integer type result of the subtraction. This will usually
3810 be the same as the result type (ptrdiff_t), but may need to be a wider
3811 type if pointers for the address space are wider than ptrdiff_t. */
3812 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3813 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3814 else
3815 inttype = restype;
3817 if (TREE_CODE (target_type) == VOID_TYPE)
3818 pedwarn (loc, OPT_Wpointer_arith,
3819 "pointer of type %<void *%> used in subtraction");
3820 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3821 pedwarn (loc, OPT_Wpointer_arith,
3822 "pointer to a function used in subtraction");
3824 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3826 gcc_assert (current_function_decl != NULL_TREE);
3828 op0 = save_expr (op0);
3829 op1 = save_expr (op1);
3831 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3832 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3835 /* First do the subtraction, then build the divide operator
3836 and only convert at the very end.
3837 Do not do default conversions in case restype is a short type. */
3839 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3840 pointers. If some platform cannot provide that, or has a larger
3841 ptrdiff_type to support differences larger than half the address
3842 space, cast the pointers to some larger integer type and do the
3843 computations in that type. */
3844 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
3845 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3846 convert (inttype, op1), false);
3847 else
3849 /* Cast away qualifiers. */
3850 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
3851 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
3852 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3855 /* This generates an error if op1 is pointer to incomplete type. */
3856 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3857 error_at (loc, "arithmetic on pointer to an incomplete type");
3859 op1 = c_size_in_bytes (target_type);
3861 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3862 error_at (loc, "arithmetic on pointer to an empty aggregate");
3864 /* Divide by the size, in easiest possible way. */
3865 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3866 op0, convert (inttype, op1));
3868 /* Convert to final result type if necessary. */
3869 return convert (restype, result);
3872 /* Expand atomic compound assignments into an appropriate sequence as
3873 specified by the C11 standard section 6.5.16.2.
3875 _Atomic T1 E1
3876 T2 E2
3877 E1 op= E2
3879 This sequence is used for all types for which these operations are
3880 supported.
3882 In addition, built-in versions of the 'fe' prefixed routines may
3883 need to be invoked for floating point (real, complex or vector) when
3884 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3886 T1 newval;
3887 T1 old;
3888 T1 *addr
3889 T2 val
3890 fenv_t fenv
3892 addr = &E1;
3893 val = (E2);
3894 __atomic_load (addr, &old, SEQ_CST);
3895 feholdexcept (&fenv);
3896 loop:
3897 newval = old op val;
3898 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3899 SEQ_CST))
3900 goto done;
3901 feclearexcept (FE_ALL_EXCEPT);
3902 goto loop:
3903 done:
3904 feupdateenv (&fenv);
3906 The compiler will issue the __atomic_fetch_* built-in when possible,
3907 otherwise it will generate the generic form of the atomic operations.
3908 This requires temp(s) and has their address taken. The atomic processing
3909 is smart enough to figure out when the size of an object can utilize
3910 a lock-free version, and convert the built-in call to the appropriate
3911 lock-free routine. The optimizers will then dispose of any temps that
3912 are no longer required, and lock-free implementations are utilized as
3913 long as there is target support for the required size.
3915 If the operator is NOP_EXPR, then this is a simple assignment, and
3916 an __atomic_store is issued to perform the assignment rather than
3917 the above loop. */
3919 /* Build an atomic assignment at LOC, expanding into the proper
3920 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3921 the result of the operation, unless RETURN_OLD_P, in which case
3922 return the old value of LHS (this is only for postincrement and
3923 postdecrement). */
3925 static tree
3926 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3927 tree rhs, bool return_old_p)
3929 tree fndecl, func_call;
3930 vec<tree, va_gc> *params;
3931 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3932 tree old, old_addr;
3933 tree compound_stmt;
3934 tree stmt, goto_stmt;
3935 tree loop_label, loop_decl, done_label, done_decl;
3937 tree lhs_type = TREE_TYPE (lhs);
3938 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3939 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3940 tree rhs_semantic_type = TREE_TYPE (rhs);
3941 tree nonatomic_rhs_semantic_type;
3942 tree rhs_type;
3944 gcc_assert (TYPE_ATOMIC (lhs_type));
3946 if (return_old_p)
3947 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3949 /* Allocate enough vector items for a compare_exchange. */
3950 vec_alloc (params, 6);
3952 /* Create a compound statement to hold the sequence of statements
3953 with a loop. */
3954 compound_stmt = c_begin_compound_stmt (false);
3956 /* Remove any excess precision (which is only present here in the
3957 case of compound assignments). */
3958 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
3960 gcc_assert (modifycode != NOP_EXPR);
3961 rhs = TREE_OPERAND (rhs, 0);
3963 rhs_type = TREE_TYPE (rhs);
3965 /* Fold the RHS if it hasn't already been folded. */
3966 if (modifycode != NOP_EXPR)
3967 rhs = c_fully_fold (rhs, false, NULL);
3969 /* Remove the qualifiers for the rest of the expressions and create
3970 the VAL temp variable to hold the RHS. */
3971 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3972 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3973 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
3974 TYPE_UNQUALIFIED);
3975 val = create_tmp_var_raw (nonatomic_rhs_type);
3976 TREE_ADDRESSABLE (val) = 1;
3977 TREE_NO_WARNING (val) = 1;
3978 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3979 NULL_TREE);
3980 SET_EXPR_LOCATION (rhs, loc);
3981 add_stmt (rhs);
3983 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3984 an atomic_store. */
3985 if (modifycode == NOP_EXPR)
3987 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3988 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3989 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3990 params->quick_push (lhs_addr);
3991 params->quick_push (rhs);
3992 params->quick_push (seq_cst);
3993 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3994 add_stmt (func_call);
3996 /* Finish the compound statement. */
3997 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3999 /* VAL is the value which was stored, return a COMPOUND_STMT of
4000 the statement and that value. */
4001 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4004 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4005 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4006 isn't applicable for such builtins. ??? Do we want to handle enums? */
4007 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4008 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4010 built_in_function fncode;
4011 switch (modifycode)
4013 case PLUS_EXPR:
4014 case POINTER_PLUS_EXPR:
4015 fncode = (return_old_p
4016 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4017 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4018 break;
4019 case MINUS_EXPR:
4020 fncode = (return_old_p
4021 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4022 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4023 break;
4024 case BIT_AND_EXPR:
4025 fncode = (return_old_p
4026 ? BUILT_IN_ATOMIC_FETCH_AND_N
4027 : BUILT_IN_ATOMIC_AND_FETCH_N);
4028 break;
4029 case BIT_IOR_EXPR:
4030 fncode = (return_old_p
4031 ? BUILT_IN_ATOMIC_FETCH_OR_N
4032 : BUILT_IN_ATOMIC_OR_FETCH_N);
4033 break;
4034 case BIT_XOR_EXPR:
4035 fncode = (return_old_p
4036 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4037 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4038 break;
4039 default:
4040 goto cas_loop;
4043 /* We can only use "_1" through "_16" variants of the atomic fetch
4044 built-ins. */
4045 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4046 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4047 goto cas_loop;
4049 /* If this is a pointer type, we need to multiply by the size of
4050 the pointer target type. */
4051 if (POINTER_TYPE_P (lhs_type))
4053 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4054 /* ??? This would introduce -Wdiscarded-qualifiers
4055 warning: __atomic_fetch_* expect volatile void *
4056 type as the first argument. (Assignments between
4057 atomic and non-atomic objects are OK.) */
4058 || TYPE_RESTRICT (lhs_type))
4059 goto cas_loop;
4060 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4061 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4062 convert (ptrdiff_type_node, rhs),
4063 convert (ptrdiff_type_node, sz));
4066 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4067 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4068 fndecl = builtin_decl_explicit (fncode);
4069 params->quick_push (lhs_addr);
4070 params->quick_push (rhs);
4071 params->quick_push (seq_cst);
4072 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4074 newval = create_tmp_var_raw (nonatomic_lhs_type);
4075 TREE_ADDRESSABLE (newval) = 1;
4076 TREE_NO_WARNING (newval) = 1;
4077 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4078 NULL_TREE, NULL_TREE);
4079 SET_EXPR_LOCATION (rhs, loc);
4080 add_stmt (rhs);
4082 /* Finish the compound statement. */
4083 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4085 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4086 the statement and that value. */
4087 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4090 cas_loop:
4091 /* Create the variables and labels required for the op= form. */
4092 old = create_tmp_var_raw (nonatomic_lhs_type);
4093 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4094 TREE_ADDRESSABLE (old) = 1;
4095 TREE_NO_WARNING (old) = 1;
4097 newval = create_tmp_var_raw (nonatomic_lhs_type);
4098 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4099 TREE_ADDRESSABLE (newval) = 1;
4100 TREE_NO_WARNING (newval) = 1;
4102 loop_decl = create_artificial_label (loc);
4103 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4105 done_decl = create_artificial_label (loc);
4106 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4108 /* __atomic_load (addr, &old, SEQ_CST). */
4109 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4110 params->quick_push (lhs_addr);
4111 params->quick_push (old_addr);
4112 params->quick_push (seq_cst);
4113 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4114 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4115 NULL_TREE);
4116 add_stmt (old);
4117 params->truncate (0);
4119 /* Create the expressions for floating-point environment
4120 manipulation, if required. */
4121 bool need_fenv = (flag_trapping_math
4122 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4123 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4124 if (need_fenv)
4125 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4127 if (hold_call)
4128 add_stmt (hold_call);
4130 /* loop: */
4131 add_stmt (loop_label);
4133 /* newval = old + val; */
4134 if (rhs_type != rhs_semantic_type)
4135 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4136 rhs = build_binary_op (loc, modifycode, old, val, true);
4137 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4139 tree eptype = TREE_TYPE (rhs);
4140 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4141 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4143 else
4144 rhs = c_fully_fold (rhs, false, NULL);
4145 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4146 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4147 NULL_TREE, 0);
4148 if (rhs != error_mark_node)
4150 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4151 NULL_TREE);
4152 SET_EXPR_LOCATION (rhs, loc);
4153 add_stmt (rhs);
4156 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4157 goto done; */
4158 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4159 params->quick_push (lhs_addr);
4160 params->quick_push (old_addr);
4161 params->quick_push (newval_addr);
4162 params->quick_push (integer_zero_node);
4163 params->quick_push (seq_cst);
4164 params->quick_push (seq_cst);
4165 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4167 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4168 SET_EXPR_LOCATION (goto_stmt, loc);
4170 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4171 SET_EXPR_LOCATION (stmt, loc);
4172 add_stmt (stmt);
4174 if (clear_call)
4175 add_stmt (clear_call);
4177 /* goto loop; */
4178 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4179 SET_EXPR_LOCATION (goto_stmt, loc);
4180 add_stmt (goto_stmt);
4182 /* done: */
4183 add_stmt (done_label);
4185 if (update_call)
4186 add_stmt (update_call);
4188 /* Finish the compound statement. */
4189 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4191 /* NEWVAL is the value that was successfully stored, return a
4192 COMPOUND_EXPR of the statement and the appropriate value. */
4193 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4194 return_old_p ? old : newval);
4197 /* Construct and perhaps optimize a tree representation
4198 for a unary operation. CODE, a tree_code, specifies the operation
4199 and XARG is the operand.
4200 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4201 promotions (such as from short to int).
4202 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4203 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4204 to pointers in C99.
4206 LOCATION is the location of the operator. */
4208 tree
4209 build_unary_op (location_t location, enum tree_code code, tree xarg,
4210 bool noconvert)
4212 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4213 tree arg = xarg;
4214 tree argtype = NULL_TREE;
4215 enum tree_code typecode;
4216 tree val;
4217 tree ret = error_mark_node;
4218 tree eptype = NULL_TREE;
4219 const char *invalid_op_diag;
4220 bool int_operands;
4222 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4223 if (int_operands)
4224 arg = remove_c_maybe_const_expr (arg);
4226 if (code != ADDR_EXPR)
4227 arg = require_complete_type (location, arg);
4229 typecode = TREE_CODE (TREE_TYPE (arg));
4230 if (typecode == ERROR_MARK)
4231 return error_mark_node;
4232 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4233 typecode = INTEGER_TYPE;
4235 if ((invalid_op_diag
4236 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4238 error_at (location, invalid_op_diag);
4239 return error_mark_node;
4242 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4244 eptype = TREE_TYPE (arg);
4245 arg = TREE_OPERAND (arg, 0);
4248 switch (code)
4250 case CONVERT_EXPR:
4251 /* This is used for unary plus, because a CONVERT_EXPR
4252 is enough to prevent anybody from looking inside for
4253 associativity, but won't generate any code. */
4254 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4255 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4256 || typecode == VECTOR_TYPE))
4258 error_at (location, "wrong type argument to unary plus");
4259 return error_mark_node;
4261 else if (!noconvert)
4262 arg = default_conversion (arg);
4263 arg = non_lvalue_loc (location, arg);
4264 break;
4266 case NEGATE_EXPR:
4267 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4268 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4269 || typecode == VECTOR_TYPE))
4271 error_at (location, "wrong type argument to unary minus");
4272 return error_mark_node;
4274 else if (!noconvert)
4275 arg = default_conversion (arg);
4276 break;
4278 case BIT_NOT_EXPR:
4279 /* ~ works on integer types and non float vectors. */
4280 if (typecode == INTEGER_TYPE
4281 || (typecode == VECTOR_TYPE
4282 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4284 tree e = arg;
4286 /* Warn if the expression has boolean value. */
4287 while (TREE_CODE (e) == COMPOUND_EXPR)
4288 e = TREE_OPERAND (e, 1);
4290 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4291 || truth_value_p (TREE_CODE (e)))
4292 && warning_at (location, OPT_Wbool_operation,
4293 "%<~%> on a boolean expression"))
4295 gcc_rich_location richloc (location);
4296 richloc.add_fixit_insert_before (location, "!");
4297 inform (&richloc, "did you mean to use logical not?");
4299 if (!noconvert)
4300 arg = default_conversion (arg);
4302 else if (typecode == COMPLEX_TYPE)
4304 code = CONJ_EXPR;
4305 pedwarn (location, OPT_Wpedantic,
4306 "ISO C does not support %<~%> for complex conjugation");
4307 if (!noconvert)
4308 arg = default_conversion (arg);
4310 else
4312 error_at (location, "wrong type argument to bit-complement");
4313 return error_mark_node;
4315 break;
4317 case ABS_EXPR:
4318 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4320 error_at (location, "wrong type argument to abs");
4321 return error_mark_node;
4323 else if (!noconvert)
4324 arg = default_conversion (arg);
4325 break;
4327 case ABSU_EXPR:
4328 if (!(typecode == INTEGER_TYPE))
4330 error_at (location, "wrong type argument to absu");
4331 return error_mark_node;
4333 else if (!noconvert)
4334 arg = default_conversion (arg);
4335 break;
4337 case CONJ_EXPR:
4338 /* Conjugating a real value is a no-op, but allow it anyway. */
4339 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4340 || typecode == COMPLEX_TYPE))
4342 error_at (location, "wrong type argument to conjugation");
4343 return error_mark_node;
4345 else if (!noconvert)
4346 arg = default_conversion (arg);
4347 break;
4349 case TRUTH_NOT_EXPR:
4350 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4351 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4352 && typecode != COMPLEX_TYPE)
4354 error_at (location,
4355 "wrong type argument to unary exclamation mark");
4356 return error_mark_node;
4358 if (int_operands)
4360 arg = c_objc_common_truthvalue_conversion (location, xarg);
4361 arg = remove_c_maybe_const_expr (arg);
4363 else
4364 arg = c_objc_common_truthvalue_conversion (location, arg);
4365 ret = invert_truthvalue_loc (location, arg);
4366 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4367 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4368 location = EXPR_LOCATION (ret);
4369 goto return_build_unary_op;
4371 case REALPART_EXPR:
4372 case IMAGPART_EXPR:
4373 ret = build_real_imag_expr (location, code, arg);
4374 if (ret == error_mark_node)
4375 return error_mark_node;
4376 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4377 eptype = TREE_TYPE (eptype);
4378 goto return_build_unary_op;
4380 case PREINCREMENT_EXPR:
4381 case POSTINCREMENT_EXPR:
4382 case PREDECREMENT_EXPR:
4383 case POSTDECREMENT_EXPR:
4385 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4387 tree inner = build_unary_op (location, code,
4388 C_MAYBE_CONST_EXPR_EXPR (arg),
4389 noconvert);
4390 if (inner == error_mark_node)
4391 return error_mark_node;
4392 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4393 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4394 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4395 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4396 goto return_build_unary_op;
4399 /* Complain about anything that is not a true lvalue. In
4400 Objective-C, skip this check for property_refs. */
4401 if (!objc_is_property_ref (arg)
4402 && !lvalue_or_else (location,
4403 arg, ((code == PREINCREMENT_EXPR
4404 || code == POSTINCREMENT_EXPR)
4405 ? lv_increment
4406 : lv_decrement)))
4407 return error_mark_node;
4409 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4411 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4412 warning_at (location, OPT_Wc___compat,
4413 "increment of enumeration value is invalid in C++");
4414 else
4415 warning_at (location, OPT_Wc___compat,
4416 "decrement of enumeration value is invalid in C++");
4419 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4421 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4422 warning_at (location, OPT_Wbool_operation,
4423 "increment of a boolean expression");
4424 else
4425 warning_at (location, OPT_Wbool_operation,
4426 "decrement of a boolean expression");
4429 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4430 arg = c_fully_fold (arg, false, NULL, true);
4432 bool atomic_op;
4433 atomic_op = really_atomic_lvalue (arg);
4435 /* Increment or decrement the real part of the value,
4436 and don't change the imaginary part. */
4437 if (typecode == COMPLEX_TYPE)
4439 tree real, imag;
4441 pedwarn (location, OPT_Wpedantic,
4442 "ISO C does not support %<++%> and %<--%> on complex types");
4444 if (!atomic_op)
4446 arg = stabilize_reference (arg);
4447 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4448 true);
4449 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4450 true);
4451 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4452 if (real == error_mark_node || imag == error_mark_node)
4453 return error_mark_node;
4454 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4455 real, imag);
4456 goto return_build_unary_op;
4460 /* Report invalid types. */
4462 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4463 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4464 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4466 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4467 error_at (location, "wrong type argument to increment");
4468 else
4469 error_at (location, "wrong type argument to decrement");
4471 return error_mark_node;
4475 tree inc;
4477 argtype = TREE_TYPE (arg);
4479 /* Compute the increment. */
4481 if (typecode == POINTER_TYPE)
4483 /* If pointer target is an incomplete type,
4484 we just cannot know how to do the arithmetic. */
4485 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4487 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4488 error_at (location,
4489 "increment of pointer to an incomplete type %qT",
4490 TREE_TYPE (argtype));
4491 else
4492 error_at (location,
4493 "decrement of pointer to an incomplete type %qT",
4494 TREE_TYPE (argtype));
4496 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4497 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4499 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4500 pedwarn (location, OPT_Wpointer_arith,
4501 "wrong type argument to increment");
4502 else
4503 pedwarn (location, OPT_Wpointer_arith,
4504 "wrong type argument to decrement");
4507 inc = c_size_in_bytes (TREE_TYPE (argtype));
4508 inc = convert_to_ptrofftype_loc (location, inc);
4510 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4512 /* For signed fract types, we invert ++ to -- or
4513 -- to ++, and change inc from 1 to -1, because
4514 it is not possible to represent 1 in signed fract constants.
4515 For unsigned fract types, the result always overflows and
4516 we get an undefined (original) or the maximum value. */
4517 if (code == PREINCREMENT_EXPR)
4518 code = PREDECREMENT_EXPR;
4519 else if (code == PREDECREMENT_EXPR)
4520 code = PREINCREMENT_EXPR;
4521 else if (code == POSTINCREMENT_EXPR)
4522 code = POSTDECREMENT_EXPR;
4523 else /* code == POSTDECREMENT_EXPR */
4524 code = POSTINCREMENT_EXPR;
4526 inc = integer_minus_one_node;
4527 inc = convert (argtype, inc);
4529 else
4531 inc = VECTOR_TYPE_P (argtype)
4532 ? build_one_cst (argtype)
4533 : integer_one_node;
4534 inc = convert (argtype, inc);
4537 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4538 need to ask Objective-C to build the increment or decrement
4539 expression for it. */
4540 if (objc_is_property_ref (arg))
4541 return objc_build_incr_expr_for_property_ref (location, code,
4542 arg, inc);
4544 /* Report a read-only lvalue. */
4545 if (TYPE_READONLY (argtype))
4547 readonly_error (location, arg,
4548 ((code == PREINCREMENT_EXPR
4549 || code == POSTINCREMENT_EXPR)
4550 ? lv_increment : lv_decrement));
4551 return error_mark_node;
4553 else if (TREE_READONLY (arg))
4554 readonly_warning (arg,
4555 ((code == PREINCREMENT_EXPR
4556 || code == POSTINCREMENT_EXPR)
4557 ? lv_increment : lv_decrement));
4559 /* If the argument is atomic, use the special code sequences for
4560 atomic compound assignment. */
4561 if (atomic_op)
4563 arg = stabilize_reference (arg);
4564 ret = build_atomic_assign (location, arg,
4565 ((code == PREINCREMENT_EXPR
4566 || code == POSTINCREMENT_EXPR)
4567 ? PLUS_EXPR
4568 : MINUS_EXPR),
4569 (FRACT_MODE_P (TYPE_MODE (argtype))
4570 ? inc
4571 : integer_one_node),
4572 (code == POSTINCREMENT_EXPR
4573 || code == POSTDECREMENT_EXPR));
4574 goto return_build_unary_op;
4577 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4578 val = boolean_increment (code, arg);
4579 else
4580 val = build2 (code, TREE_TYPE (arg), arg, inc);
4581 TREE_SIDE_EFFECTS (val) = 1;
4582 if (TREE_CODE (val) != code)
4583 TREE_NO_WARNING (val) = 1;
4584 ret = val;
4585 goto return_build_unary_op;
4588 case ADDR_EXPR:
4589 /* Note that this operation never does default_conversion. */
4591 /* The operand of unary '&' must be an lvalue (which excludes
4592 expressions of type void), or, in C99, the result of a [] or
4593 unary '*' operator. */
4594 if (VOID_TYPE_P (TREE_TYPE (arg))
4595 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4596 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4597 pedwarn (location, 0, "taking address of expression of type %<void%>");
4599 /* Let &* cancel out to simplify resulting code. */
4600 if (INDIRECT_REF_P (arg))
4602 /* Don't let this be an lvalue. */
4603 if (lvalue_p (TREE_OPERAND (arg, 0)))
4604 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4605 ret = TREE_OPERAND (arg, 0);
4606 goto return_build_unary_op;
4609 /* Anything not already handled and not a true memory reference
4610 or a non-lvalue array is an error. */
4611 if (typecode != FUNCTION_TYPE && !noconvert
4612 && !lvalue_or_else (location, arg, lv_addressof))
4613 return error_mark_node;
4615 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4616 folding later. */
4617 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4619 tree inner = build_unary_op (location, code,
4620 C_MAYBE_CONST_EXPR_EXPR (arg),
4621 noconvert);
4622 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4623 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4624 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4625 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4626 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4627 goto return_build_unary_op;
4630 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4631 argtype = TREE_TYPE (arg);
4633 /* If the lvalue is const or volatile, merge that into the type
4634 to which the address will point. This is only needed
4635 for function types. */
4636 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4637 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4638 && TREE_CODE (argtype) == FUNCTION_TYPE)
4640 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4641 int quals = orig_quals;
4643 if (TREE_READONLY (arg))
4644 quals |= TYPE_QUAL_CONST;
4645 if (TREE_THIS_VOLATILE (arg))
4646 quals |= TYPE_QUAL_VOLATILE;
4648 argtype = c_build_qualified_type (argtype, quals);
4651 switch (TREE_CODE (arg))
4653 case COMPONENT_REF:
4654 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4656 error_at (location, "cannot take address of bit-field %qD",
4657 TREE_OPERAND (arg, 1));
4658 return error_mark_node;
4661 /* fall through */
4663 case ARRAY_REF:
4664 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4666 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4667 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4669 error_at (location, "cannot take address of scalar with "
4670 "reverse storage order");
4671 return error_mark_node;
4674 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4675 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4676 warning_at (location, OPT_Wscalar_storage_order,
4677 "address of array with reverse scalar storage "
4678 "order requested");
4681 default:
4682 break;
4685 if (!c_mark_addressable (arg))
4686 return error_mark_node;
4688 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4689 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4691 argtype = build_pointer_type (argtype);
4693 /* ??? Cope with user tricks that amount to offsetof. Delete this
4694 when we have proper support for integer constant expressions. */
4695 val = get_base_address (arg);
4696 if (val && INDIRECT_REF_P (val)
4697 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4699 ret = fold_offsetof (arg, argtype);
4700 goto return_build_unary_op;
4703 val = build1 (ADDR_EXPR, argtype, arg);
4705 ret = val;
4706 goto return_build_unary_op;
4708 default:
4709 gcc_unreachable ();
4712 if (argtype == NULL_TREE)
4713 argtype = TREE_TYPE (arg);
4714 if (TREE_CODE (arg) == INTEGER_CST)
4715 ret = (require_constant_value
4716 ? fold_build1_initializer_loc (location, code, argtype, arg)
4717 : fold_build1_loc (location, code, argtype, arg));
4718 else
4719 ret = build1 (code, argtype, arg);
4720 return_build_unary_op:
4721 gcc_assert (ret != error_mark_node);
4722 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4723 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4724 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4725 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4726 ret = note_integer_operands (ret);
4727 if (eptype)
4728 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4729 protected_set_expr_location (ret, location);
4730 return ret;
4733 /* Return nonzero if REF is an lvalue valid for this language.
4734 Lvalues can be assigned, unless their type has TYPE_READONLY.
4735 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4737 bool
4738 lvalue_p (const_tree ref)
4740 const enum tree_code code = TREE_CODE (ref);
4742 switch (code)
4744 case REALPART_EXPR:
4745 case IMAGPART_EXPR:
4746 case COMPONENT_REF:
4747 return lvalue_p (TREE_OPERAND (ref, 0));
4749 case C_MAYBE_CONST_EXPR:
4750 return lvalue_p (TREE_OPERAND (ref, 1));
4752 case COMPOUND_LITERAL_EXPR:
4753 case STRING_CST:
4754 return true;
4756 case INDIRECT_REF:
4757 case ARRAY_REF:
4758 case VAR_DECL:
4759 case PARM_DECL:
4760 case RESULT_DECL:
4761 case ERROR_MARK:
4762 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4763 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4765 case BIND_EXPR:
4766 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4768 default:
4769 return false;
4773 /* Give a warning for storing in something that is read-only in GCC
4774 terms but not const in ISO C terms. */
4776 static void
4777 readonly_warning (tree arg, enum lvalue_use use)
4779 switch (use)
4781 case lv_assign:
4782 warning (0, "assignment of read-only location %qE", arg);
4783 break;
4784 case lv_increment:
4785 warning (0, "increment of read-only location %qE", arg);
4786 break;
4787 case lv_decrement:
4788 warning (0, "decrement of read-only location %qE", arg);
4789 break;
4790 default:
4791 gcc_unreachable ();
4793 return;
4797 /* Return nonzero if REF is an lvalue valid for this language;
4798 otherwise, print an error message and return zero. USE says
4799 how the lvalue is being used and so selects the error message.
4800 LOCATION is the location at which any error should be reported. */
4802 static int
4803 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4805 int win = lvalue_p (ref);
4807 if (!win)
4808 lvalue_error (loc, use);
4810 return win;
4813 /* Mark EXP saying that we need to be able to take the
4814 address of it; it should not be allocated in a register.
4815 Returns true if successful. ARRAY_REF_P is true if this
4816 is for ARRAY_REF construction - in that case we don't want
4817 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4818 it is fine to use ARRAY_REFs for vector subscripts on vector
4819 register variables. */
4821 bool
4822 c_mark_addressable (tree exp, bool array_ref_p)
4824 tree x = exp;
4826 while (1)
4827 switch (TREE_CODE (x))
4829 case VIEW_CONVERT_EXPR:
4830 if (array_ref_p
4831 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4832 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4833 return true;
4834 /* FALLTHRU */
4835 case COMPONENT_REF:
4836 case ADDR_EXPR:
4837 case ARRAY_REF:
4838 case REALPART_EXPR:
4839 case IMAGPART_EXPR:
4840 x = TREE_OPERAND (x, 0);
4841 break;
4843 case COMPOUND_LITERAL_EXPR:
4844 TREE_ADDRESSABLE (x) = 1;
4845 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
4846 return true;
4848 case CONSTRUCTOR:
4849 TREE_ADDRESSABLE (x) = 1;
4850 return true;
4852 case VAR_DECL:
4853 case CONST_DECL:
4854 case PARM_DECL:
4855 case RESULT_DECL:
4856 if (C_DECL_REGISTER (x)
4857 && DECL_NONLOCAL (x))
4859 if (TREE_PUBLIC (x) || is_global_var (x))
4861 error
4862 ("global register variable %qD used in nested function", x);
4863 return false;
4865 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4867 else if (C_DECL_REGISTER (x))
4869 if (TREE_PUBLIC (x) || is_global_var (x))
4870 error ("address of global register variable %qD requested", x);
4871 else
4872 error ("address of register variable %qD requested", x);
4873 return false;
4876 /* FALLTHRU */
4877 case FUNCTION_DECL:
4878 TREE_ADDRESSABLE (x) = 1;
4879 /* FALLTHRU */
4880 default:
4881 return true;
4885 /* Convert EXPR to TYPE, warning about conversion problems with
4886 constants. SEMANTIC_TYPE is the type this conversion would use
4887 without excess precision. If SEMANTIC_TYPE is NULL, this function
4888 is equivalent to convert_and_check. This function is a wrapper that
4889 handles conversions that may be different than
4890 the usual ones because of excess precision. */
4892 static tree
4893 ep_convert_and_check (location_t loc, tree type, tree expr,
4894 tree semantic_type)
4896 if (TREE_TYPE (expr) == type)
4897 return expr;
4899 /* For C11, integer conversions may have results with excess
4900 precision. */
4901 if (flag_isoc11 || !semantic_type)
4902 return convert_and_check (loc, type, expr);
4904 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4905 && TREE_TYPE (expr) != semantic_type)
4907 /* For integers, we need to check the real conversion, not
4908 the conversion to the excess precision type. */
4909 expr = convert_and_check (loc, semantic_type, expr);
4911 /* Result type is the excess precision type, which should be
4912 large enough, so do not check. */
4913 return convert (type, expr);
4916 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4917 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4918 if folded to an integer constant then the unselected half may
4919 contain arbitrary operations not normally permitted in constant
4920 expressions. Set the location of the expression to LOC. */
4922 tree
4923 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4924 tree op1, tree op1_original_type, location_t op1_loc,
4925 tree op2, tree op2_original_type, location_t op2_loc)
4927 tree type1;
4928 tree type2;
4929 enum tree_code code1;
4930 enum tree_code code2;
4931 tree result_type = NULL;
4932 tree semantic_result_type = NULL;
4933 tree orig_op1 = op1, orig_op2 = op2;
4934 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4935 bool ifexp_int_operands;
4936 tree ret;
4938 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4939 if (op1_int_operands)
4940 op1 = remove_c_maybe_const_expr (op1);
4941 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4942 if (op2_int_operands)
4943 op2 = remove_c_maybe_const_expr (op2);
4944 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4945 if (ifexp_int_operands)
4946 ifexp = remove_c_maybe_const_expr (ifexp);
4948 /* Promote both alternatives. */
4950 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4951 op1 = default_conversion (op1);
4952 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4953 op2 = default_conversion (op2);
4955 if (TREE_CODE (ifexp) == ERROR_MARK
4956 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4957 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4958 return error_mark_node;
4960 type1 = TREE_TYPE (op1);
4961 code1 = TREE_CODE (type1);
4962 type2 = TREE_TYPE (op2);
4963 code2 = TREE_CODE (type2);
4965 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4966 return error_mark_node;
4968 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4969 return error_mark_node;
4971 /* C90 does not permit non-lvalue arrays in conditional expressions.
4972 In C99 they will be pointers by now. */
4973 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4975 error_at (colon_loc, "non-lvalue array in conditional expression");
4976 return error_mark_node;
4979 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4980 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4981 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4982 || code1 == COMPLEX_TYPE)
4983 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4984 || code2 == COMPLEX_TYPE))
4986 semantic_result_type = c_common_type (type1, type2);
4987 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4989 op1 = TREE_OPERAND (op1, 0);
4990 type1 = TREE_TYPE (op1);
4991 gcc_assert (TREE_CODE (type1) == code1);
4993 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4995 op2 = TREE_OPERAND (op2, 0);
4996 type2 = TREE_TYPE (op2);
4997 gcc_assert (TREE_CODE (type2) == code2);
5001 if (warn_cxx_compat)
5003 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5004 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5006 if (TREE_CODE (t1) == ENUMERAL_TYPE
5007 && TREE_CODE (t2) == ENUMERAL_TYPE
5008 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5009 warning_at (colon_loc, OPT_Wc___compat,
5010 ("different enum types in conditional is "
5011 "invalid in C++: %qT vs %qT"),
5012 t1, t2);
5015 /* Quickly detect the usual case where op1 and op2 have the same type
5016 after promotion. */
5017 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5019 if (type1 == type2)
5020 result_type = type1;
5021 else
5022 result_type = TYPE_MAIN_VARIANT (type1);
5024 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5025 || code1 == COMPLEX_TYPE)
5026 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5027 || code2 == COMPLEX_TYPE))
5029 /* In C11, a conditional expression between a floating-point
5030 type and an integer type should convert the integer type to
5031 the evaluation format of the floating-point type, with
5032 possible excess precision. */
5033 tree eptype1 = type1;
5034 tree eptype2 = type2;
5035 if (flag_isoc11)
5037 tree eptype;
5038 if (ANY_INTEGRAL_TYPE_P (type1)
5039 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5041 eptype2 = eptype;
5042 if (!semantic_result_type)
5043 semantic_result_type = c_common_type (type1, type2);
5045 else if (ANY_INTEGRAL_TYPE_P (type2)
5046 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5048 eptype1 = eptype;
5049 if (!semantic_result_type)
5050 semantic_result_type = c_common_type (type1, type2);
5053 result_type = c_common_type (eptype1, eptype2);
5054 if (result_type == error_mark_node)
5055 return error_mark_node;
5056 do_warn_double_promotion (result_type, type1, type2,
5057 "implicit conversion from %qT to %qT to "
5058 "match other result of conditional",
5059 colon_loc);
5061 /* If -Wsign-compare, warn here if type1 and type2 have
5062 different signedness. We'll promote the signed to unsigned
5063 and later code won't know it used to be different.
5064 Do this check on the original types, so that explicit casts
5065 will be considered, but default promotions won't. */
5066 if (c_inhibit_evaluation_warnings == 0)
5068 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5069 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5071 if (unsigned_op1 ^ unsigned_op2)
5073 bool ovf;
5075 /* Do not warn if the result type is signed, since the
5076 signed type will only be chosen if it can represent
5077 all the values of the unsigned type. */
5078 if (!TYPE_UNSIGNED (result_type))
5079 /* OK */;
5080 else
5082 bool op1_maybe_const = true;
5083 bool op2_maybe_const = true;
5085 /* Do not warn if the signed quantity is an
5086 unsuffixed integer literal (or some static
5087 constant expression involving such literals) and
5088 it is non-negative. This warning requires the
5089 operands to be folded for best results, so do
5090 that folding in this case even without
5091 warn_sign_compare to avoid warning options
5092 possibly affecting code generation. */
5093 c_inhibit_evaluation_warnings
5094 += (ifexp == truthvalue_false_node);
5095 op1 = c_fully_fold (op1, require_constant_value,
5096 &op1_maybe_const);
5097 c_inhibit_evaluation_warnings
5098 -= (ifexp == truthvalue_false_node);
5100 c_inhibit_evaluation_warnings
5101 += (ifexp == truthvalue_true_node);
5102 op2 = c_fully_fold (op2, require_constant_value,
5103 &op2_maybe_const);
5104 c_inhibit_evaluation_warnings
5105 -= (ifexp == truthvalue_true_node);
5107 if (warn_sign_compare)
5109 if ((unsigned_op2
5110 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5111 || (unsigned_op1
5112 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5113 /* OK */;
5114 else if (unsigned_op2)
5115 warning_at (op1_loc, OPT_Wsign_compare,
5116 "operand of ?: changes signedness from "
5117 "%qT to %qT due to unsignedness of other "
5118 "operand", TREE_TYPE (orig_op1),
5119 TREE_TYPE (orig_op2));
5120 else
5121 warning_at (op2_loc, OPT_Wsign_compare,
5122 "operand of ?: changes signedness from "
5123 "%qT to %qT due to unsignedness of other "
5124 "operand", TREE_TYPE (orig_op2),
5125 TREE_TYPE (orig_op1));
5127 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5128 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5129 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5130 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5135 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5137 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5138 pedwarn (colon_loc, OPT_Wpedantic,
5139 "ISO C forbids conditional expr with only one void side");
5140 result_type = void_type_node;
5142 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5144 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5145 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5146 addr_space_t as_common;
5148 if (comp_target_types (colon_loc, type1, type2))
5149 result_type = common_pointer_type (type1, type2);
5150 else if (null_pointer_constant_p (orig_op1))
5151 result_type = type2;
5152 else if (null_pointer_constant_p (orig_op2))
5153 result_type = type1;
5154 else if (!addr_space_superset (as1, as2, &as_common))
5156 error_at (colon_loc, "pointers to disjoint address spaces "
5157 "used in conditional expression");
5158 return error_mark_node;
5160 else if (VOID_TYPE_P (TREE_TYPE (type1))
5161 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5163 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5164 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5165 & ~TYPE_QUALS (TREE_TYPE (type1))))
5166 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5167 "pointer to array loses qualifier "
5168 "in conditional expression");
5170 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5171 pedwarn (colon_loc, OPT_Wpedantic,
5172 "ISO C forbids conditional expr between "
5173 "%<void *%> and function pointer");
5174 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5175 TREE_TYPE (type2)));
5177 else if (VOID_TYPE_P (TREE_TYPE (type2))
5178 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5180 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5181 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5182 & ~TYPE_QUALS (TREE_TYPE (type2))))
5183 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5184 "pointer to array loses qualifier "
5185 "in conditional expression");
5187 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5188 pedwarn (colon_loc, OPT_Wpedantic,
5189 "ISO C forbids conditional expr between "
5190 "%<void *%> and function pointer");
5191 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5192 TREE_TYPE (type1)));
5194 /* Objective-C pointer comparisons are a bit more lenient. */
5195 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5196 result_type = objc_common_type (type1, type2);
5197 else
5199 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5201 pedwarn (colon_loc, 0,
5202 "pointer type mismatch in conditional expression");
5203 result_type = build_pointer_type
5204 (build_qualified_type (void_type_node, qual));
5207 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5209 if (!null_pointer_constant_p (orig_op2))
5210 pedwarn (colon_loc, 0,
5211 "pointer/integer type mismatch in conditional expression");
5212 else
5214 op2 = null_pointer_node;
5216 result_type = type1;
5218 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5220 if (!null_pointer_constant_p (orig_op1))
5221 pedwarn (colon_loc, 0,
5222 "pointer/integer type mismatch in conditional expression");
5223 else
5225 op1 = null_pointer_node;
5227 result_type = type2;
5230 if (!result_type)
5232 if (flag_cond_mismatch)
5233 result_type = void_type_node;
5234 else
5236 error_at (colon_loc, "type mismatch in conditional expression");
5237 return error_mark_node;
5241 /* Merge const and volatile flags of the incoming types. */
5242 result_type
5243 = build_type_variant (result_type,
5244 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5245 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5247 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5248 semantic_result_type);
5249 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5250 semantic_result_type);
5252 if (ifexp_bcp && ifexp == truthvalue_true_node)
5254 op2_int_operands = true;
5255 op1 = c_fully_fold (op1, require_constant_value, NULL);
5257 if (ifexp_bcp && ifexp == truthvalue_false_node)
5259 op1_int_operands = true;
5260 op2 = c_fully_fold (op2, require_constant_value, NULL);
5262 int_const = int_operands = (ifexp_int_operands
5263 && op1_int_operands
5264 && op2_int_operands);
5265 if (int_operands)
5267 int_const = ((ifexp == truthvalue_true_node
5268 && TREE_CODE (orig_op1) == INTEGER_CST
5269 && !TREE_OVERFLOW (orig_op1))
5270 || (ifexp == truthvalue_false_node
5271 && TREE_CODE (orig_op2) == INTEGER_CST
5272 && !TREE_OVERFLOW (orig_op2)));
5275 /* Need to convert condition operand into a vector mask. */
5276 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5278 tree vectype = TREE_TYPE (ifexp);
5279 tree elem_type = TREE_TYPE (vectype);
5280 tree zero = build_int_cst (elem_type, 0);
5281 tree zero_vec = build_vector_from_val (vectype, zero);
5282 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5283 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5286 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5287 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5288 else
5290 if (int_operands)
5292 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5293 nested inside of the expression. */
5294 op1 = c_fully_fold (op1, false, NULL);
5295 op2 = c_fully_fold (op2, false, NULL);
5297 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5298 if (int_operands)
5299 ret = note_integer_operands (ret);
5301 if (semantic_result_type)
5302 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5304 protected_set_expr_location (ret, colon_loc);
5306 /* If the OP1 and OP2 are the same and don't have side-effects,
5307 warn here, because the COND_EXPR will be turned into OP1. */
5308 if (warn_duplicated_branches
5309 && TREE_CODE (ret) == COND_EXPR
5310 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5311 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5312 "this condition has identical branches");
5314 return ret;
5317 /* Return a compound expression that performs two expressions and
5318 returns the value of the second of them.
5320 LOC is the location of the COMPOUND_EXPR. */
5322 tree
5323 build_compound_expr (location_t loc, tree expr1, tree expr2)
5325 bool expr1_int_operands, expr2_int_operands;
5326 tree eptype = NULL_TREE;
5327 tree ret;
5329 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5330 if (expr1_int_operands)
5331 expr1 = remove_c_maybe_const_expr (expr1);
5332 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5333 if (expr2_int_operands)
5334 expr2 = remove_c_maybe_const_expr (expr2);
5336 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5337 expr1 = TREE_OPERAND (expr1, 0);
5338 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5340 eptype = TREE_TYPE (expr2);
5341 expr2 = TREE_OPERAND (expr2, 0);
5344 if (!TREE_SIDE_EFFECTS (expr1))
5346 /* The left-hand operand of a comma expression is like an expression
5347 statement: with -Wunused, we should warn if it doesn't have
5348 any side-effects, unless it was explicitly cast to (void). */
5349 if (warn_unused_value)
5351 if (VOID_TYPE_P (TREE_TYPE (expr1))
5352 && CONVERT_EXPR_P (expr1))
5353 ; /* (void) a, b */
5354 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5355 && TREE_CODE (expr1) == COMPOUND_EXPR
5356 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5357 ; /* (void) a, (void) b, c */
5358 else
5359 warning_at (loc, OPT_Wunused_value,
5360 "left-hand operand of comma expression has no effect");
5363 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5364 && warn_unused_value)
5366 tree r = expr1;
5367 location_t cloc = loc;
5368 while (TREE_CODE (r) == COMPOUND_EXPR)
5370 if (EXPR_HAS_LOCATION (r))
5371 cloc = EXPR_LOCATION (r);
5372 r = TREE_OPERAND (r, 1);
5374 if (!TREE_SIDE_EFFECTS (r)
5375 && !VOID_TYPE_P (TREE_TYPE (r))
5376 && !CONVERT_EXPR_P (r))
5377 warning_at (cloc, OPT_Wunused_value,
5378 "right-hand operand of comma expression has no effect");
5381 /* With -Wunused, we should also warn if the left-hand operand does have
5382 side-effects, but computes a value which is not used. For example, in
5383 `foo() + bar(), baz()' the result of the `+' operator is not used,
5384 so we should issue a warning. */
5385 else if (warn_unused_value)
5386 warn_if_unused_value (expr1, loc);
5388 if (expr2 == error_mark_node)
5389 return error_mark_node;
5391 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5393 if (flag_isoc99
5394 && expr1_int_operands
5395 && expr2_int_operands)
5396 ret = note_integer_operands (ret);
5398 if (eptype)
5399 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5401 protected_set_expr_location (ret, loc);
5402 return ret;
5405 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5406 which we are casting. OTYPE is the type of the expression being
5407 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5408 of the cast. -Wcast-qual appeared on the command line. Named
5409 address space qualifiers are not handled here, because they result
5410 in different warnings. */
5412 static void
5413 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5415 tree in_type = type;
5416 tree in_otype = otype;
5417 int added = 0;
5418 int discarded = 0;
5419 bool is_const;
5421 /* Check that the qualifiers on IN_TYPE are a superset of the
5422 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5423 nodes is uninteresting and we stop as soon as we hit a
5424 non-POINTER_TYPE node on either type. */
5427 in_otype = TREE_TYPE (in_otype);
5428 in_type = TREE_TYPE (in_type);
5430 /* GNU C allows cv-qualified function types. 'const' means the
5431 function is very pure, 'volatile' means it can't return. We
5432 need to warn when such qualifiers are added, not when they're
5433 taken away. */
5434 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5435 && TREE_CODE (in_type) == FUNCTION_TYPE)
5436 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5437 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5438 else
5439 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5440 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5442 while (TREE_CODE (in_type) == POINTER_TYPE
5443 && TREE_CODE (in_otype) == POINTER_TYPE);
5445 if (added)
5446 warning_at (loc, OPT_Wcast_qual,
5447 "cast adds %q#v qualifier to function type", added);
5449 if (discarded)
5450 /* There are qualifiers present in IN_OTYPE that are not present
5451 in IN_TYPE. */
5452 warning_at (loc, OPT_Wcast_qual,
5453 "cast discards %qv qualifier from pointer target type",
5454 discarded);
5456 if (added || discarded)
5457 return;
5459 /* A cast from **T to const **T is unsafe, because it can cause a
5460 const value to be changed with no additional warning. We only
5461 issue this warning if T is the same on both sides, and we only
5462 issue the warning if there are the same number of pointers on
5463 both sides, as otherwise the cast is clearly unsafe anyhow. A
5464 cast is unsafe when a qualifier is added at one level and const
5465 is not present at all outer levels.
5467 To issue this warning, we check at each level whether the cast
5468 adds new qualifiers not already seen. We don't need to special
5469 case function types, as they won't have the same
5470 TYPE_MAIN_VARIANT. */
5472 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5473 return;
5474 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5475 return;
5477 in_type = type;
5478 in_otype = otype;
5479 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5482 in_type = TREE_TYPE (in_type);
5483 in_otype = TREE_TYPE (in_otype);
5484 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5485 && !is_const)
5487 warning_at (loc, OPT_Wcast_qual,
5488 "to be safe all intermediate pointers in cast from "
5489 "%qT to %qT must be %<const%> qualified",
5490 otype, type);
5491 break;
5493 if (is_const)
5494 is_const = TYPE_READONLY (in_type);
5496 while (TREE_CODE (in_type) == POINTER_TYPE);
5499 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5501 static bool
5502 c_safe_arg_type_equiv_p (tree t1, tree t2)
5504 t1 = TYPE_MAIN_VARIANT (t1);
5505 t2 = TYPE_MAIN_VARIANT (t2);
5507 if (TREE_CODE (t1) == POINTER_TYPE
5508 && TREE_CODE (t2) == POINTER_TYPE)
5509 return true;
5511 /* The signedness of the parameter matters only when an integral
5512 type smaller than int is promoted to int, otherwise only the
5513 precision of the parameter matters.
5514 This check should make sure that the callee does not see
5515 undefined values in argument registers. */
5516 if (INTEGRAL_TYPE_P (t1)
5517 && INTEGRAL_TYPE_P (t2)
5518 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5519 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5520 || !targetm.calls.promote_prototypes (NULL_TREE)
5521 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5522 return true;
5524 return comptypes (t1, t2);
5527 /* Check if a type cast between two function types can be considered safe. */
5529 static bool
5530 c_safe_function_type_cast_p (tree t1, tree t2)
5532 if (TREE_TYPE (t1) == void_type_node &&
5533 TYPE_ARG_TYPES (t1) == void_list_node)
5534 return true;
5536 if (TREE_TYPE (t2) == void_type_node &&
5537 TYPE_ARG_TYPES (t2) == void_list_node)
5538 return true;
5540 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5541 return false;
5543 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5544 t1 && t2;
5545 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5546 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5547 return false;
5549 return true;
5552 /* Build an expression representing a cast to type TYPE of expression EXPR.
5553 LOC is the location of the cast-- typically the open paren of the cast. */
5555 tree
5556 build_c_cast (location_t loc, tree type, tree expr)
5558 tree value;
5560 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5561 expr = TREE_OPERAND (expr, 0);
5563 value = expr;
5565 if (type == error_mark_node || expr == error_mark_node)
5566 return error_mark_node;
5568 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5569 only in <protocol> qualifications. But when constructing cast expressions,
5570 the protocols do matter and must be kept around. */
5571 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5572 return build1 (NOP_EXPR, type, expr);
5574 type = TYPE_MAIN_VARIANT (type);
5576 if (TREE_CODE (type) == ARRAY_TYPE)
5578 error_at (loc, "cast specifies array type");
5579 return error_mark_node;
5582 if (TREE_CODE (type) == FUNCTION_TYPE)
5584 error_at (loc, "cast specifies function type");
5585 return error_mark_node;
5588 if (!VOID_TYPE_P (type))
5590 value = require_complete_type (loc, value);
5591 if (value == error_mark_node)
5592 return error_mark_node;
5595 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5597 if (RECORD_OR_UNION_TYPE_P (type))
5598 pedwarn (loc, OPT_Wpedantic,
5599 "ISO C forbids casting nonscalar to the same type");
5601 /* Convert to remove any qualifiers from VALUE's type. */
5602 value = convert (type, value);
5604 else if (TREE_CODE (type) == UNION_TYPE)
5606 tree field;
5608 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5609 if (TREE_TYPE (field) != error_mark_node
5610 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5611 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5612 break;
5614 if (field)
5616 tree t;
5617 bool maybe_const = true;
5619 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5620 t = c_fully_fold (value, false, &maybe_const);
5621 t = build_constructor_single (type, field, t);
5622 if (!maybe_const)
5623 t = c_wrap_maybe_const (t, true);
5624 t = digest_init (loc, type, t,
5625 NULL_TREE, false, true, 0);
5626 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5627 return t;
5629 error_at (loc, "cast to union type from type not present in union");
5630 return error_mark_node;
5632 else
5634 tree otype, ovalue;
5636 if (type == void_type_node)
5638 tree t = build1 (CONVERT_EXPR, type, value);
5639 SET_EXPR_LOCATION (t, loc);
5640 return t;
5643 otype = TREE_TYPE (value);
5645 /* Optionally warn about potentially worrisome casts. */
5646 if (warn_cast_qual
5647 && TREE_CODE (type) == POINTER_TYPE
5648 && TREE_CODE (otype) == POINTER_TYPE)
5649 handle_warn_cast_qual (loc, type, otype);
5651 /* Warn about conversions between pointers to disjoint
5652 address spaces. */
5653 if (TREE_CODE (type) == POINTER_TYPE
5654 && TREE_CODE (otype) == POINTER_TYPE
5655 && !null_pointer_constant_p (value))
5657 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5658 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5659 addr_space_t as_common;
5661 if (!addr_space_superset (as_to, as_from, &as_common))
5663 if (ADDR_SPACE_GENERIC_P (as_from))
5664 warning_at (loc, 0, "cast to %s address space pointer "
5665 "from disjoint generic address space pointer",
5666 c_addr_space_name (as_to));
5668 else if (ADDR_SPACE_GENERIC_P (as_to))
5669 warning_at (loc, 0, "cast to generic address space pointer "
5670 "from disjoint %s address space pointer",
5671 c_addr_space_name (as_from));
5673 else
5674 warning_at (loc, 0, "cast to %s address space pointer "
5675 "from disjoint %s address space pointer",
5676 c_addr_space_name (as_to),
5677 c_addr_space_name (as_from));
5681 /* Warn about possible alignment problems. */
5682 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5683 && TREE_CODE (type) == POINTER_TYPE
5684 && TREE_CODE (otype) == POINTER_TYPE
5685 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5686 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5687 /* Don't warn about opaque types, where the actual alignment
5688 restriction is unknown. */
5689 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5690 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5691 && min_align_of_type (TREE_TYPE (type))
5692 > min_align_of_type (TREE_TYPE (otype)))
5693 warning_at (loc, OPT_Wcast_align,
5694 "cast increases required alignment of target type");
5696 if (TREE_CODE (type) == INTEGER_TYPE
5697 && TREE_CODE (otype) == POINTER_TYPE
5698 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5699 /* Unlike conversion of integers to pointers, where the
5700 warning is disabled for converting constants because
5701 of cases such as SIG_*, warn about converting constant
5702 pointers to integers. In some cases it may cause unwanted
5703 sign extension, and a warning is appropriate. */
5704 warning_at (loc, OPT_Wpointer_to_int_cast,
5705 "cast from pointer to integer of different size");
5707 if (TREE_CODE (value) == CALL_EXPR
5708 && TREE_CODE (type) != TREE_CODE (otype))
5709 warning_at (loc, OPT_Wbad_function_cast,
5710 "cast from function call of type %qT "
5711 "to non-matching type %qT", otype, type);
5713 if (TREE_CODE (type) == POINTER_TYPE
5714 && TREE_CODE (otype) == INTEGER_TYPE
5715 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5716 /* Don't warn about converting any constant. */
5717 && !TREE_CONSTANT (value))
5718 warning_at (loc,
5719 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5720 "of different size");
5722 if (warn_strict_aliasing <= 2)
5723 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
5725 /* If pedantic, warn for conversions between function and object
5726 pointer types, except for converting a null pointer constant
5727 to function pointer type. */
5728 if (pedantic
5729 && TREE_CODE (type) == POINTER_TYPE
5730 && TREE_CODE (otype) == POINTER_TYPE
5731 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5732 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5733 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5734 "conversion of function pointer to object pointer type");
5736 if (pedantic
5737 && TREE_CODE (type) == POINTER_TYPE
5738 && TREE_CODE (otype) == POINTER_TYPE
5739 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5740 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5741 && !null_pointer_constant_p (value))
5742 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5743 "conversion of object pointer to function pointer type");
5745 if (TREE_CODE (type) == POINTER_TYPE
5746 && TREE_CODE (otype) == POINTER_TYPE
5747 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5748 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5749 && !c_safe_function_type_cast_p (TREE_TYPE (type),
5750 TREE_TYPE (otype)))
5751 warning_at (loc, OPT_Wcast_function_type,
5752 "cast between incompatible function types"
5753 " from %qT to %qT", otype, type);
5755 ovalue = value;
5756 value = convert (type, value);
5758 /* Ignore any integer overflow caused by the cast. */
5759 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5761 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5763 if (!TREE_OVERFLOW (value))
5765 /* Avoid clobbering a shared constant. */
5766 value = copy_node (value);
5767 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5770 else if (TREE_OVERFLOW (value))
5771 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5772 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
5776 /* Don't let a cast be an lvalue. */
5777 if (lvalue_p (value))
5778 value = non_lvalue_loc (loc, value);
5780 /* Don't allow the results of casting to floating-point or complex
5781 types be confused with actual constants, or casts involving
5782 integer and pointer types other than direct integer-to-integer
5783 and integer-to-pointer be confused with integer constant
5784 expressions and null pointer constants. */
5785 if (TREE_CODE (value) == REAL_CST
5786 || TREE_CODE (value) == COMPLEX_CST
5787 || (TREE_CODE (value) == INTEGER_CST
5788 && !((TREE_CODE (expr) == INTEGER_CST
5789 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5790 || TREE_CODE (expr) == REAL_CST
5791 || TREE_CODE (expr) == COMPLEX_CST)))
5792 value = build1 (NOP_EXPR, type, value);
5794 protected_set_expr_location (value, loc);
5795 return value;
5798 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5799 location of the open paren of the cast, or the position of the cast
5800 expr. */
5801 tree
5802 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5804 tree type;
5805 tree type_expr = NULL_TREE;
5806 bool type_expr_const = true;
5807 tree ret;
5808 int saved_wsp = warn_strict_prototypes;
5810 /* This avoids warnings about unprototyped casts on
5811 integers. E.g. "#define SIG_DFL (void(*)())0". */
5812 if (TREE_CODE (expr) == INTEGER_CST)
5813 warn_strict_prototypes = 0;
5814 type = groktypename (type_name, &type_expr, &type_expr_const);
5815 warn_strict_prototypes = saved_wsp;
5817 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5818 && reject_gcc_builtin (expr))
5819 return error_mark_node;
5821 ret = build_c_cast (loc, type, expr);
5822 if (type_expr)
5824 bool inner_expr_const = true;
5825 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5826 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5827 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5828 && inner_expr_const);
5829 SET_EXPR_LOCATION (ret, loc);
5832 if (!EXPR_HAS_LOCATION (ret))
5833 protected_set_expr_location (ret, loc);
5835 /* C++ does not permits types to be defined in a cast, but it
5836 allows references to incomplete types. */
5837 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5838 warning_at (loc, OPT_Wc___compat,
5839 "defining a type in a cast is invalid in C++");
5841 return ret;
5844 /* Build an assignment expression of lvalue LHS from value RHS.
5845 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5846 may differ from TREE_TYPE (LHS) for an enum bitfield.
5847 MODIFYCODE is the code for a binary operator that we use
5848 to combine the old value of LHS with RHS to get the new value.
5849 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5850 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5851 which may differ from TREE_TYPE (RHS) for an enum value.
5853 LOCATION is the location of the MODIFYCODE operator.
5854 RHS_LOC is the location of the RHS. */
5856 tree
5857 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5858 enum tree_code modifycode,
5859 location_t rhs_loc, tree rhs, tree rhs_origtype)
5861 tree result;
5862 tree newrhs;
5863 tree rhseval = NULL_TREE;
5864 tree lhstype = TREE_TYPE (lhs);
5865 tree olhstype = lhstype;
5866 bool npc;
5867 bool is_atomic_op;
5869 /* Types that aren't fully specified cannot be used in assignments. */
5870 lhs = require_complete_type (location, lhs);
5872 /* Avoid duplicate error messages from operands that had errors. */
5873 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5874 return error_mark_node;
5876 /* Ensure an error for assigning a non-lvalue array to an array in
5877 C90. */
5878 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5880 error_at (location, "assignment to expression with array type");
5881 return error_mark_node;
5884 /* For ObjC properties, defer this check. */
5885 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5886 return error_mark_node;
5888 is_atomic_op = really_atomic_lvalue (lhs);
5890 newrhs = rhs;
5892 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5894 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5895 lhs_origtype, modifycode, rhs_loc, rhs,
5896 rhs_origtype);
5897 if (inner == error_mark_node)
5898 return error_mark_node;
5899 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5900 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5901 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5902 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5903 protected_set_expr_location (result, location);
5904 return result;
5907 /* If a binary op has been requested, combine the old LHS value with the RHS
5908 producing the value we should actually store into the LHS. */
5910 if (modifycode != NOP_EXPR)
5912 lhs = c_fully_fold (lhs, false, NULL, true);
5913 lhs = stabilize_reference (lhs);
5915 /* Construct the RHS for any non-atomic compound assignemnt. */
5916 if (!is_atomic_op)
5918 /* If in LHS op= RHS the RHS has side-effects, ensure they
5919 are preevaluated before the rest of the assignment expression's
5920 side-effects, because RHS could contain e.g. function calls
5921 that modify LHS. */
5922 if (TREE_SIDE_EFFECTS (rhs))
5924 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5925 newrhs = save_expr (TREE_OPERAND (rhs, 0));
5926 else
5927 newrhs = save_expr (rhs);
5928 rhseval = newrhs;
5929 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5930 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
5931 newrhs);
5933 newrhs = build_binary_op (location,
5934 modifycode, lhs, newrhs, true);
5936 /* The original type of the right hand side is no longer
5937 meaningful. */
5938 rhs_origtype = NULL_TREE;
5942 if (c_dialect_objc ())
5944 /* Check if we are modifying an Objective-C property reference;
5945 if so, we need to generate setter calls. */
5946 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5947 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
5948 else
5949 result = objc_maybe_build_modify_expr (lhs, newrhs);
5950 if (result)
5951 goto return_result;
5953 /* Else, do the check that we postponed for Objective-C. */
5954 if (!lvalue_or_else (location, lhs, lv_assign))
5955 return error_mark_node;
5958 /* Give an error for storing in something that is 'const'. */
5960 if (TYPE_READONLY (lhstype)
5961 || (RECORD_OR_UNION_TYPE_P (lhstype)
5962 && C_TYPE_FIELDS_READONLY (lhstype)))
5964 readonly_error (location, lhs, lv_assign);
5965 return error_mark_node;
5967 else if (TREE_READONLY (lhs))
5968 readonly_warning (lhs, lv_assign);
5970 /* If storing into a structure or union member,
5971 it has probably been given type `int'.
5972 Compute the type that would go with
5973 the actual amount of storage the member occupies. */
5975 if (TREE_CODE (lhs) == COMPONENT_REF
5976 && (TREE_CODE (lhstype) == INTEGER_TYPE
5977 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5978 || TREE_CODE (lhstype) == REAL_TYPE
5979 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5980 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5982 /* If storing in a field that is in actuality a short or narrower than one,
5983 we must store in the field in its actual type. */
5985 if (lhstype != TREE_TYPE (lhs))
5987 lhs = copy_node (lhs);
5988 TREE_TYPE (lhs) = lhstype;
5991 /* Issue -Wc++-compat warnings about an assignment to an enum type
5992 when LHS does not have its original type. This happens for,
5993 e.g., an enum bitfield in a struct. */
5994 if (warn_cxx_compat
5995 && lhs_origtype != NULL_TREE
5996 && lhs_origtype != lhstype
5997 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5999 tree checktype = (rhs_origtype != NULL_TREE
6000 ? rhs_origtype
6001 : TREE_TYPE (rhs));
6002 if (checktype != error_mark_node
6003 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6004 || (is_atomic_op && modifycode != NOP_EXPR)))
6005 warning_at (location, OPT_Wc___compat,
6006 "enum conversion in assignment is invalid in C++");
6009 /* If the lhs is atomic, remove that qualifier. */
6010 if (is_atomic_op)
6012 lhstype = build_qualified_type (lhstype,
6013 (TYPE_QUALS (lhstype)
6014 & ~TYPE_QUAL_ATOMIC));
6015 olhstype = build_qualified_type (olhstype,
6016 (TYPE_QUALS (lhstype)
6017 & ~TYPE_QUAL_ATOMIC));
6020 /* Convert new value to destination type. Fold it first, then
6021 restore any excess precision information, for the sake of
6022 conversion warnings. */
6024 if (!(is_atomic_op && modifycode != NOP_EXPR))
6026 tree rhs_semantic_type = NULL_TREE;
6027 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6029 rhs_semantic_type = TREE_TYPE (newrhs);
6030 newrhs = TREE_OPERAND (newrhs, 0);
6032 npc = null_pointer_constant_p (newrhs);
6033 newrhs = c_fully_fold (newrhs, false, NULL);
6034 if (rhs_semantic_type)
6035 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6036 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6037 rhs_origtype, ic_assign, npc,
6038 NULL_TREE, NULL_TREE, 0);
6039 if (TREE_CODE (newrhs) == ERROR_MARK)
6040 return error_mark_node;
6043 /* Emit ObjC write barrier, if necessary. */
6044 if (c_dialect_objc () && flag_objc_gc)
6046 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6047 if (result)
6049 protected_set_expr_location (result, location);
6050 goto return_result;
6054 /* Scan operands. */
6056 if (is_atomic_op)
6057 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6058 else
6060 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6061 TREE_SIDE_EFFECTS (result) = 1;
6062 protected_set_expr_location (result, location);
6065 /* If we got the LHS in a different type for storing in,
6066 convert the result back to the nominal type of LHS
6067 so that the value we return always has the same type
6068 as the LHS argument. */
6070 if (olhstype == TREE_TYPE (result))
6071 goto return_result;
6073 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6074 rhs_origtype, ic_assign, false, NULL_TREE,
6075 NULL_TREE, 0);
6076 protected_set_expr_location (result, location);
6078 return_result:
6079 if (rhseval)
6080 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6081 return result;
6084 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6085 This is used to implement -fplan9-extensions. */
6087 static bool
6088 find_anonymous_field_with_type (tree struct_type, tree type)
6090 tree field;
6091 bool found;
6093 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6094 found = false;
6095 for (field = TYPE_FIELDS (struct_type);
6096 field != NULL_TREE;
6097 field = TREE_CHAIN (field))
6099 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6100 ? c_build_qualified_type (TREE_TYPE (field),
6101 TYPE_QUAL_ATOMIC)
6102 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6103 if (DECL_NAME (field) == NULL
6104 && comptypes (type, fieldtype))
6106 if (found)
6107 return false;
6108 found = true;
6110 else if (DECL_NAME (field) == NULL
6111 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6112 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6114 if (found)
6115 return false;
6116 found = true;
6119 return found;
6122 /* RHS is an expression whose type is pointer to struct. If there is
6123 an anonymous field in RHS with type TYPE, then return a pointer to
6124 that field in RHS. This is used with -fplan9-extensions. This
6125 returns NULL if no conversion could be found. */
6127 static tree
6128 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6130 tree rhs_struct_type, lhs_main_type;
6131 tree field, found_field;
6132 bool found_sub_field;
6133 tree ret;
6135 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6136 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6137 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6139 gcc_assert (POINTER_TYPE_P (type));
6140 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6141 ? c_build_qualified_type (TREE_TYPE (type),
6142 TYPE_QUAL_ATOMIC)
6143 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6145 found_field = NULL_TREE;
6146 found_sub_field = false;
6147 for (field = TYPE_FIELDS (rhs_struct_type);
6148 field != NULL_TREE;
6149 field = TREE_CHAIN (field))
6151 if (DECL_NAME (field) != NULL_TREE
6152 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6153 continue;
6154 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6155 ? c_build_qualified_type (TREE_TYPE (field),
6156 TYPE_QUAL_ATOMIC)
6157 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6158 if (comptypes (lhs_main_type, fieldtype))
6160 if (found_field != NULL_TREE)
6161 return NULL_TREE;
6162 found_field = field;
6164 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6165 lhs_main_type))
6167 if (found_field != NULL_TREE)
6168 return NULL_TREE;
6169 found_field = field;
6170 found_sub_field = true;
6174 if (found_field == NULL_TREE)
6175 return NULL_TREE;
6177 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6178 build_fold_indirect_ref (rhs), found_field,
6179 NULL_TREE);
6180 ret = build_fold_addr_expr_loc (location, ret);
6182 if (found_sub_field)
6184 ret = convert_to_anonymous_field (location, type, ret);
6185 gcc_assert (ret != NULL_TREE);
6188 return ret;
6191 /* Issue an error message for a bad initializer component.
6192 GMSGID identifies the message.
6193 The component name is taken from the spelling stack. */
6195 static void
6196 error_init (location_t loc, const char *gmsgid)
6198 char *ofwhat;
6200 /* The gmsgid may be a format string with %< and %>. */
6201 error_at (loc, gmsgid);
6202 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6203 if (*ofwhat)
6204 inform (loc, "(near initialization for %qs)", ofwhat);
6207 /* Issue a pedantic warning for a bad initializer component. OPT is
6208 the option OPT_* (from options.h) controlling this warning or 0 if
6209 it is unconditionally given. GMSGID identifies the message. The
6210 component name is taken from the spelling stack. */
6212 static void ATTRIBUTE_GCC_DIAG (3,0)
6213 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6215 /* Use the location where a macro was expanded rather than where
6216 it was defined to make sure macros defined in system headers
6217 but used incorrectly elsewhere are diagnosed. */
6218 source_location exploc = expansion_point_location_if_in_system_header (loc);
6220 va_list ap;
6221 va_start (ap, gmsgid);
6222 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6223 va_end (ap);
6224 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6225 if (*ofwhat && warned)
6226 inform (exploc, "(near initialization for %qs)", ofwhat);
6229 /* Issue a warning for a bad initializer component.
6231 OPT is the OPT_W* value corresponding to the warning option that
6232 controls this warning. GMSGID identifies the message. The
6233 component name is taken from the spelling stack. */
6235 static void
6236 warning_init (location_t loc, int opt, const char *gmsgid)
6238 char *ofwhat;
6239 bool warned;
6241 /* Use the location where a macro was expanded rather than where
6242 it was defined to make sure macros defined in system headers
6243 but used incorrectly elsewhere are diagnosed. */
6244 source_location exploc = expansion_point_location_if_in_system_header (loc);
6246 /* The gmsgid may be a format string with %< and %>. */
6247 warned = warning_at (exploc, opt, gmsgid);
6248 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6249 if (*ofwhat && warned)
6250 inform (exploc, "(near initialization for %qs)", ofwhat);
6253 /* If TYPE is an array type and EXPR is a parenthesized string
6254 constant, warn if pedantic that EXPR is being used to initialize an
6255 object of type TYPE. */
6257 void
6258 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6260 if (pedantic
6261 && TREE_CODE (type) == ARRAY_TYPE
6262 && TREE_CODE (expr.value) == STRING_CST
6263 && expr.original_code != STRING_CST)
6264 pedwarn_init (loc, OPT_Wpedantic,
6265 "array initialized from parenthesized string constant");
6268 /* Attempt to locate the parameter with the given index within FNDECL,
6269 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6271 static location_t
6272 get_fndecl_argument_location (tree fndecl, int argnum)
6274 int i;
6275 tree param;
6277 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6278 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6279 i < argnum && param;
6280 i++, param = TREE_CHAIN (param))
6283 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6284 return DECL_SOURCE_LOCATION (FNDECL). */
6285 if (param == NULL)
6286 return DECL_SOURCE_LOCATION (fndecl);
6288 return DECL_SOURCE_LOCATION (param);
6291 /* Issue a note about a mismatching argument for parameter PARMNUM
6292 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6293 Attempt to issue the note at the pertinent parameter of the decl;
6294 failing that issue it at the location of FUNDECL; failing that
6295 issue it at PLOC. */
6297 static void
6298 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6299 tree expected_type, tree actual_type)
6301 location_t loc;
6302 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6303 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6304 else
6305 loc = ploc;
6307 inform (loc,
6308 "expected %qT but argument is of type %qT",
6309 expected_type, actual_type);
6312 /* Convert value RHS to type TYPE as preparation for an assignment to
6313 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6314 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6315 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6316 constant before any folding.
6317 The real work of conversion is done by `convert'.
6318 The purpose of this function is to generate error messages
6319 for assignments that are not allowed in C.
6320 ERRTYPE says whether it is argument passing, assignment,
6321 initialization or return.
6323 In the following example, '~' denotes where EXPR_LOC and '^' where
6324 LOCATION point to:
6326 f (var); [ic_argpass]
6327 ^ ~~~
6328 x = var; [ic_assign]
6329 ^ ~~~;
6330 int x = var; [ic_init]
6332 return x; [ic_return]
6335 FUNCTION is a tree for the function being called.
6336 PARMNUM is the number of the argument, for printing in error messages. */
6338 static tree
6339 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6340 tree rhs, tree origtype, enum impl_conv errtype,
6341 bool null_pointer_constant, tree fundecl,
6342 tree function, int parmnum)
6344 enum tree_code codel = TREE_CODE (type);
6345 tree orig_rhs = rhs;
6346 tree rhstype;
6347 enum tree_code coder;
6348 tree rname = NULL_TREE;
6349 bool objc_ok = false;
6351 /* Use the expansion point location to handle cases such as user's
6352 function returning a wrong-type macro defined in a system header. */
6353 location = expansion_point_location_if_in_system_header (location);
6355 if (errtype == ic_argpass)
6357 tree selector;
6358 /* Change pointer to function to the function itself for
6359 diagnostics. */
6360 if (TREE_CODE (function) == ADDR_EXPR
6361 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6362 function = TREE_OPERAND (function, 0);
6364 /* Handle an ObjC selector specially for diagnostics. */
6365 selector = objc_message_selector ();
6366 rname = function;
6367 if (selector && parmnum > 2)
6369 rname = selector;
6370 parmnum -= 2;
6374 /* This macro is used to emit diagnostics to ensure that all format
6375 strings are complete sentences, visible to gettext and checked at
6376 compile time. */
6377 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6378 do { \
6379 switch (errtype) \
6381 case ic_argpass: \
6382 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6383 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6384 break; \
6385 case ic_assign: \
6386 pedwarn (LOCATION, OPT, AS); \
6387 break; \
6388 case ic_init: \
6389 pedwarn_init (LOCATION, OPT, IN); \
6390 break; \
6391 case ic_return: \
6392 pedwarn (LOCATION, OPT, RE); \
6393 break; \
6394 default: \
6395 gcc_unreachable (); \
6397 } while (0)
6399 /* This macro is used to emit diagnostics to ensure that all format
6400 strings are complete sentences, visible to gettext and checked at
6401 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6402 extra parameter to enumerate qualifiers. */
6403 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6404 do { \
6405 switch (errtype) \
6407 case ic_argpass: \
6408 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6409 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6410 break; \
6411 case ic_assign: \
6412 pedwarn (LOCATION, OPT, AS, QUALS); \
6413 break; \
6414 case ic_init: \
6415 pedwarn (LOCATION, OPT, IN, QUALS); \
6416 break; \
6417 case ic_return: \
6418 pedwarn (LOCATION, OPT, RE, QUALS); \
6419 break; \
6420 default: \
6421 gcc_unreachable (); \
6423 } while (0)
6425 /* This macro is used to emit diagnostics to ensure that all format
6426 strings are complete sentences, visible to gettext and checked at
6427 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6428 warning_at instead of pedwarn. */
6429 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6430 do { \
6431 switch (errtype) \
6433 case ic_argpass: \
6434 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6435 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6436 break; \
6437 case ic_assign: \
6438 warning_at (LOCATION, OPT, AS, QUALS); \
6439 break; \
6440 case ic_init: \
6441 warning_at (LOCATION, OPT, IN, QUALS); \
6442 break; \
6443 case ic_return: \
6444 warning_at (LOCATION, OPT, RE, QUALS); \
6445 break; \
6446 default: \
6447 gcc_unreachable (); \
6449 } while (0)
6451 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6452 rhs = TREE_OPERAND (rhs, 0);
6454 rhstype = TREE_TYPE (rhs);
6455 coder = TREE_CODE (rhstype);
6457 if (coder == ERROR_MARK)
6458 return error_mark_node;
6460 if (c_dialect_objc ())
6462 int parmno;
6464 switch (errtype)
6466 case ic_return:
6467 parmno = 0;
6468 break;
6470 case ic_assign:
6471 parmno = -1;
6472 break;
6474 case ic_init:
6475 parmno = -2;
6476 break;
6478 default:
6479 parmno = parmnum;
6480 break;
6483 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6486 if (warn_cxx_compat)
6488 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6489 if (checktype != error_mark_node
6490 && TREE_CODE (type) == ENUMERAL_TYPE
6491 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6492 switch (errtype)
6494 case ic_argpass:
6495 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6496 "passing argument %d of %qE is invalid in C++",
6497 parmnum, rname))
6498 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6499 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6500 "expected %qT but argument is of type %qT",
6501 type, rhstype);
6502 break;
6503 case ic_assign:
6504 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6505 "%qT in assignment is invalid in C++", rhstype, type);
6506 break;
6507 case ic_init:
6508 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6509 "%qT to %qT in initialization is invalid in C++",
6510 rhstype, type);
6511 break;
6512 case ic_return:
6513 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6514 "%qT in return is invalid in C++", rhstype, type);
6515 break;
6516 default:
6517 gcc_unreachable ();
6521 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6522 return rhs;
6524 if (coder == VOID_TYPE)
6526 /* Except for passing an argument to an unprototyped function,
6527 this is a constraint violation. When passing an argument to
6528 an unprototyped function, it is compile-time undefined;
6529 making it a constraint in that case was rejected in
6530 DR#252. */
6531 error_at (location, "void value not ignored as it ought to be");
6532 return error_mark_node;
6534 rhs = require_complete_type (location, rhs);
6535 if (rhs == error_mark_node)
6536 return error_mark_node;
6538 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6539 return error_mark_node;
6541 /* A non-reference type can convert to a reference. This handles
6542 va_start, va_copy and possibly port built-ins. */
6543 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6545 if (!lvalue_p (rhs))
6547 error_at (location, "cannot pass rvalue to reference parameter");
6548 return error_mark_node;
6550 if (!c_mark_addressable (rhs))
6551 return error_mark_node;
6552 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6553 SET_EXPR_LOCATION (rhs, location);
6555 rhs = convert_for_assignment (location, expr_loc,
6556 build_pointer_type (TREE_TYPE (type)),
6557 rhs, origtype, errtype,
6558 null_pointer_constant, fundecl, function,
6559 parmnum);
6560 if (rhs == error_mark_node)
6561 return error_mark_node;
6563 rhs = build1 (NOP_EXPR, type, rhs);
6564 SET_EXPR_LOCATION (rhs, location);
6565 return rhs;
6567 /* Some types can interconvert without explicit casts. */
6568 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6569 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6570 return convert (type, rhs);
6571 /* Arithmetic types all interconvert, and enum is treated like int. */
6572 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6573 || codel == FIXED_POINT_TYPE
6574 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6575 || codel == BOOLEAN_TYPE)
6576 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6577 || coder == FIXED_POINT_TYPE
6578 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6579 || coder == BOOLEAN_TYPE))
6581 tree ret;
6582 bool save = in_late_binary_op;
6583 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6584 || (coder == REAL_TYPE
6585 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6586 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6587 in_late_binary_op = true;
6588 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6589 ? expr_loc : location, type, orig_rhs);
6590 in_late_binary_op = save;
6591 return ret;
6594 /* Aggregates in different TUs might need conversion. */
6595 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6596 && codel == coder
6597 && comptypes (type, rhstype))
6598 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6599 ? expr_loc : location, type, rhs);
6601 /* Conversion to a transparent union or record from its member types.
6602 This applies only to function arguments. */
6603 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6604 && TYPE_TRANSPARENT_AGGR (type))
6605 && errtype == ic_argpass)
6607 tree memb, marginal_memb = NULL_TREE;
6609 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6611 tree memb_type = TREE_TYPE (memb);
6613 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6614 TYPE_MAIN_VARIANT (rhstype)))
6615 break;
6617 if (TREE_CODE (memb_type) != POINTER_TYPE)
6618 continue;
6620 if (coder == POINTER_TYPE)
6622 tree ttl = TREE_TYPE (memb_type);
6623 tree ttr = TREE_TYPE (rhstype);
6625 /* Any non-function converts to a [const][volatile] void *
6626 and vice versa; otherwise, targets must be the same.
6627 Meanwhile, the lhs target must have all the qualifiers of
6628 the rhs. */
6629 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6630 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6631 || comp_target_types (location, memb_type, rhstype))
6633 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6634 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6635 /* If this type won't generate any warnings, use it. */
6636 if (lquals == rquals
6637 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6638 && TREE_CODE (ttl) == FUNCTION_TYPE)
6639 ? ((lquals | rquals) == rquals)
6640 : ((lquals | rquals) == lquals)))
6641 break;
6643 /* Keep looking for a better type, but remember this one. */
6644 if (!marginal_memb)
6645 marginal_memb = memb;
6649 /* Can convert integer zero to any pointer type. */
6650 if (null_pointer_constant)
6652 rhs = null_pointer_node;
6653 break;
6657 if (memb || marginal_memb)
6659 if (!memb)
6661 /* We have only a marginally acceptable member type;
6662 it needs a warning. */
6663 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6664 tree ttr = TREE_TYPE (rhstype);
6666 /* Const and volatile mean something different for function
6667 types, so the usual warnings are not appropriate. */
6668 if (TREE_CODE (ttr) == FUNCTION_TYPE
6669 && TREE_CODE (ttl) == FUNCTION_TYPE)
6671 /* Because const and volatile on functions are
6672 restrictions that say the function will not do
6673 certain things, it is okay to use a const or volatile
6674 function where an ordinary one is wanted, but not
6675 vice-versa. */
6676 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6677 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6678 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6679 OPT_Wdiscarded_qualifiers,
6680 G_("passing argument %d of %qE "
6681 "makes %q#v qualified function "
6682 "pointer from unqualified"),
6683 G_("assignment makes %q#v qualified "
6684 "function pointer from "
6685 "unqualified"),
6686 G_("initialization makes %q#v qualified "
6687 "function pointer from "
6688 "unqualified"),
6689 G_("return makes %q#v qualified function "
6690 "pointer from unqualified"),
6691 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6693 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6694 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6695 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6696 OPT_Wdiscarded_qualifiers,
6697 G_("passing argument %d of %qE discards "
6698 "%qv qualifier from pointer target type"),
6699 G_("assignment discards %qv qualifier "
6700 "from pointer target type"),
6701 G_("initialization discards %qv qualifier "
6702 "from pointer target type"),
6703 G_("return discards %qv qualifier from "
6704 "pointer target type"),
6705 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6707 memb = marginal_memb;
6710 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6711 pedwarn (location, OPT_Wpedantic,
6712 "ISO C prohibits argument conversion to union type");
6714 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6715 return build_constructor_single (type, memb, rhs);
6719 /* Conversions among pointers */
6720 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6721 && (coder == codel))
6723 tree ttl = TREE_TYPE (type);
6724 tree ttr = TREE_TYPE (rhstype);
6725 tree mvl = ttl;
6726 tree mvr = ttr;
6727 bool is_opaque_pointer;
6728 int target_cmp = 0; /* Cache comp_target_types () result. */
6729 addr_space_t asl;
6730 addr_space_t asr;
6732 if (TREE_CODE (mvl) != ARRAY_TYPE)
6733 mvl = (TYPE_ATOMIC (mvl)
6734 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6735 TYPE_QUAL_ATOMIC)
6736 : TYPE_MAIN_VARIANT (mvl));
6737 if (TREE_CODE (mvr) != ARRAY_TYPE)
6738 mvr = (TYPE_ATOMIC (mvr)
6739 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6740 TYPE_QUAL_ATOMIC)
6741 : TYPE_MAIN_VARIANT (mvr));
6742 /* Opaque pointers are treated like void pointers. */
6743 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6745 /* The Plan 9 compiler permits a pointer to a struct to be
6746 automatically converted into a pointer to an anonymous field
6747 within the struct. */
6748 if (flag_plan9_extensions
6749 && RECORD_OR_UNION_TYPE_P (mvl)
6750 && RECORD_OR_UNION_TYPE_P (mvr)
6751 && mvl != mvr)
6753 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6754 if (new_rhs != NULL_TREE)
6756 rhs = new_rhs;
6757 rhstype = TREE_TYPE (rhs);
6758 coder = TREE_CODE (rhstype);
6759 ttr = TREE_TYPE (rhstype);
6760 mvr = TYPE_MAIN_VARIANT (ttr);
6764 /* C++ does not allow the implicit conversion void* -> T*. However,
6765 for the purpose of reducing the number of false positives, we
6766 tolerate the special case of
6768 int *p = NULL;
6770 where NULL is typically defined in C to be '(void *) 0'. */
6771 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6772 warning_at (errtype == ic_argpass ? expr_loc : location,
6773 OPT_Wc___compat,
6774 "request for implicit conversion "
6775 "from %qT to %qT not permitted in C++", rhstype, type);
6777 /* See if the pointers point to incompatible address spaces. */
6778 asl = TYPE_ADDR_SPACE (ttl);
6779 asr = TYPE_ADDR_SPACE (ttr);
6780 if (!null_pointer_constant_p (rhs)
6781 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6783 switch (errtype)
6785 case ic_argpass:
6786 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6787 "non-enclosed address space", parmnum, rname);
6788 break;
6789 case ic_assign:
6790 error_at (location, "assignment from pointer to "
6791 "non-enclosed address space");
6792 break;
6793 case ic_init:
6794 error_at (location, "initialization from pointer to "
6795 "non-enclosed address space");
6796 break;
6797 case ic_return:
6798 error_at (location, "return from pointer to "
6799 "non-enclosed address space");
6800 break;
6801 default:
6802 gcc_unreachable ();
6804 return error_mark_node;
6807 /* Check if the right-hand side has a format attribute but the
6808 left-hand side doesn't. */
6809 if (warn_suggest_attribute_format
6810 && check_missing_format_attribute (type, rhstype))
6812 switch (errtype)
6814 case ic_argpass:
6815 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6816 "argument %d of %qE might be "
6817 "a candidate for a format attribute",
6818 parmnum, rname);
6819 break;
6820 case ic_assign:
6821 warning_at (location, OPT_Wsuggest_attribute_format,
6822 "assignment left-hand side might be "
6823 "a candidate for a format attribute");
6824 break;
6825 case ic_init:
6826 warning_at (location, OPT_Wsuggest_attribute_format,
6827 "initialization left-hand side might be "
6828 "a candidate for a format attribute");
6829 break;
6830 case ic_return:
6831 warning_at (location, OPT_Wsuggest_attribute_format,
6832 "return type might be "
6833 "a candidate for a format attribute");
6834 break;
6835 default:
6836 gcc_unreachable ();
6840 /* Any non-function converts to a [const][volatile] void *
6841 and vice versa; otherwise, targets must be the same.
6842 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6843 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6844 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6845 || (target_cmp = comp_target_types (location, type, rhstype))
6846 || is_opaque_pointer
6847 || ((c_common_unsigned_type (mvl)
6848 == c_common_unsigned_type (mvr))
6849 && (c_common_signed_type (mvl)
6850 == c_common_signed_type (mvr))
6851 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6853 /* Warn about loss of qualifers from pointers to arrays with
6854 qualifiers on the element type. */
6855 if (TREE_CODE (ttr) == ARRAY_TYPE)
6857 ttr = strip_array_types (ttr);
6858 ttl = strip_array_types (ttl);
6860 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6861 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6862 WARNING_FOR_QUALIFIERS (location, expr_loc,
6863 OPT_Wdiscarded_array_qualifiers,
6864 G_("passing argument %d of %qE discards "
6865 "%qv qualifier from pointer target type"),
6866 G_("assignment discards %qv qualifier "
6867 "from pointer target type"),
6868 G_("initialization discards %qv qualifier "
6869 "from pointer target type"),
6870 G_("return discards %qv qualifier from "
6871 "pointer target type"),
6872 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6874 else if (pedantic
6875 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6877 (VOID_TYPE_P (ttr)
6878 && !null_pointer_constant
6879 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6880 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6881 G_("ISO C forbids passing argument %d of "
6882 "%qE between function pointer "
6883 "and %<void *%>"),
6884 G_("ISO C forbids assignment between "
6885 "function pointer and %<void *%>"),
6886 G_("ISO C forbids initialization between "
6887 "function pointer and %<void *%>"),
6888 G_("ISO C forbids return between function "
6889 "pointer and %<void *%>"));
6890 /* Const and volatile mean something different for function types,
6891 so the usual warnings are not appropriate. */
6892 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6893 && TREE_CODE (ttl) != FUNCTION_TYPE)
6895 /* Don't warn about loss of qualifier for conversions from
6896 qualified void* to pointers to arrays with corresponding
6897 qualifier on the element type. */
6898 if (!pedantic)
6899 ttl = strip_array_types (ttl);
6901 /* Assignments between atomic and non-atomic objects are OK. */
6902 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6903 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6905 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6906 OPT_Wdiscarded_qualifiers,
6907 G_("passing argument %d of %qE discards "
6908 "%qv qualifier from pointer target type"),
6909 G_("assignment discards %qv qualifier "
6910 "from pointer target type"),
6911 G_("initialization discards %qv qualifier "
6912 "from pointer target type"),
6913 G_("return discards %qv qualifier from "
6914 "pointer target type"),
6915 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6917 /* If this is not a case of ignoring a mismatch in signedness,
6918 no warning. */
6919 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6920 || target_cmp)
6922 /* If there is a mismatch, do warn. */
6923 else if (warn_pointer_sign)
6924 switch (errtype)
6926 case ic_argpass:
6927 if (pedwarn (expr_loc, OPT_Wpointer_sign,
6928 "pointer targets in passing argument %d of "
6929 "%qE differ in signedness", parmnum, rname))
6930 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6931 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6932 "expected %qT but argument is of type %qT",
6933 type, rhstype);
6934 break;
6935 case ic_assign:
6936 pedwarn (location, OPT_Wpointer_sign,
6937 "pointer targets in assignment from %qT to %qT "
6938 "differ in signedness", rhstype, type);
6939 break;
6940 case ic_init:
6941 pedwarn_init (location, OPT_Wpointer_sign,
6942 "pointer targets in initialization of %qT "
6943 "from %qT differ in signedness", type,
6944 rhstype);
6945 break;
6946 case ic_return:
6947 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
6948 "returning %qT from a function with return type "
6949 "%qT differ in signedness", rhstype, type);
6950 break;
6951 default:
6952 gcc_unreachable ();
6955 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6956 && TREE_CODE (ttr) == FUNCTION_TYPE)
6958 /* Because const and volatile on functions are restrictions
6959 that say the function will not do certain things,
6960 it is okay to use a const or volatile function
6961 where an ordinary one is wanted, but not vice-versa. */
6962 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6963 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6964 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6965 OPT_Wdiscarded_qualifiers,
6966 G_("passing argument %d of %qE makes "
6967 "%q#v qualified function pointer "
6968 "from unqualified"),
6969 G_("assignment makes %q#v qualified function "
6970 "pointer from unqualified"),
6971 G_("initialization makes %q#v qualified "
6972 "function pointer from unqualified"),
6973 G_("return makes %q#v qualified function "
6974 "pointer from unqualified"),
6975 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6978 /* Avoid warning about the volatile ObjC EH puts on decls. */
6979 else if (!objc_ok)
6981 switch (errtype)
6983 case ic_argpass:
6984 if (pedwarn (expr_loc, OPT_Wincompatible_pointer_types,
6985 "passing argument %d of %qE from incompatible "
6986 "pointer type", parmnum, rname))
6987 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
6988 break;
6989 case ic_assign:
6990 pedwarn (location, OPT_Wincompatible_pointer_types,
6991 "assignment to %qT from incompatible pointer type %qT",
6992 type, rhstype);
6993 break;
6994 case ic_init:
6995 pedwarn_init (location, OPT_Wincompatible_pointer_types,
6996 "initialization of %qT from incompatible pointer "
6997 "type %qT", type, rhstype);
6998 break;
6999 case ic_return:
7000 pedwarn (location, OPT_Wincompatible_pointer_types,
7001 "returning %qT from a function with incompatible "
7002 "return type %qT", rhstype, type);
7003 break;
7004 default:
7005 gcc_unreachable ();
7009 return convert (type, rhs);
7011 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7013 /* ??? This should not be an error when inlining calls to
7014 unprototyped functions. */
7015 error_at (location, "invalid use of non-lvalue array");
7016 return error_mark_node;
7018 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7020 /* An explicit constant 0 can convert to a pointer,
7021 or one that results from arithmetic, even including
7022 a cast to integer type. */
7023 if (!null_pointer_constant)
7024 switch (errtype)
7026 case ic_argpass:
7027 if (pedwarn (expr_loc, OPT_Wint_conversion,
7028 "passing argument %d of %qE makes pointer from "
7029 "integer without a cast", parmnum, rname))
7030 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7031 break;
7032 case ic_assign:
7033 pedwarn (location, OPT_Wint_conversion,
7034 "assignment to %qT from %qT makes pointer from integer "
7035 "without a cast", type, rhstype);
7036 break;
7037 case ic_init:
7038 pedwarn_init (location, OPT_Wint_conversion,
7039 "initialization of %qT from %qT makes pointer from "
7040 "integer without a cast", type, rhstype);
7041 break;
7042 case ic_return:
7043 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7044 "function with return type %qT makes pointer from "
7045 "integer without a cast", rhstype, type);
7046 break;
7047 default:
7048 gcc_unreachable ();
7051 return convert (type, rhs);
7053 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7055 switch (errtype)
7057 case ic_argpass:
7058 if (pedwarn (expr_loc, OPT_Wint_conversion,
7059 "passing argument %d of %qE makes integer from "
7060 "pointer without a cast", parmnum, rname))
7061 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7062 break;
7063 case ic_assign:
7064 pedwarn (location, OPT_Wint_conversion,
7065 "assignment to %qT from %qT makes integer from pointer "
7066 "without a cast", type, rhstype);
7067 break;
7068 case ic_init:
7069 pedwarn_init (location, OPT_Wint_conversion,
7070 "initialization of %qT from %qT makes integer from "
7071 "pointer without a cast", type, rhstype);
7072 break;
7073 case ic_return:
7074 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7075 "function with return type %qT makes integer from "
7076 "pointer without a cast", rhstype, type);
7077 break;
7078 default:
7079 gcc_unreachable ();
7082 return convert (type, rhs);
7084 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7086 tree ret;
7087 bool save = in_late_binary_op;
7088 in_late_binary_op = true;
7089 ret = convert (type, rhs);
7090 in_late_binary_op = save;
7091 return ret;
7094 switch (errtype)
7096 case ic_argpass:
7097 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
7098 rname);
7099 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7100 break;
7101 case ic_assign:
7102 error_at (location, "incompatible types when assigning to type %qT from "
7103 "type %qT", type, rhstype);
7104 break;
7105 case ic_init:
7106 error_at (location,
7107 "incompatible types when initializing type %qT using type %qT",
7108 type, rhstype);
7109 break;
7110 case ic_return:
7111 error_at (location,
7112 "incompatible types when returning type %qT but %qT was "
7113 "expected", rhstype, type);
7114 break;
7115 default:
7116 gcc_unreachable ();
7119 return error_mark_node;
7122 /* If VALUE is a compound expr all of whose expressions are constant, then
7123 return its value. Otherwise, return error_mark_node.
7125 This is for handling COMPOUND_EXPRs as initializer elements
7126 which is allowed with a warning when -pedantic is specified. */
7128 static tree
7129 valid_compound_expr_initializer (tree value, tree endtype)
7131 if (TREE_CODE (value) == COMPOUND_EXPR)
7133 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7134 == error_mark_node)
7135 return error_mark_node;
7136 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7137 endtype);
7139 else if (!initializer_constant_valid_p (value, endtype))
7140 return error_mark_node;
7141 else
7142 return value;
7145 /* Perform appropriate conversions on the initial value of a variable,
7146 store it in the declaration DECL,
7147 and print any error messages that are appropriate.
7148 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7149 If the init is invalid, store an ERROR_MARK.
7151 INIT_LOC is the location of the initial value. */
7153 void
7154 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7156 tree value, type;
7157 bool npc = false;
7159 /* If variable's type was invalidly declared, just ignore it. */
7161 type = TREE_TYPE (decl);
7162 if (TREE_CODE (type) == ERROR_MARK)
7163 return;
7165 /* Digest the specified initializer into an expression. */
7167 if (init)
7168 npc = null_pointer_constant_p (init);
7169 value = digest_init (init_loc, type, init, origtype, npc,
7170 true, TREE_STATIC (decl));
7172 /* Store the expression if valid; else report error. */
7174 if (!in_system_header_at (input_location)
7175 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7176 warning (OPT_Wtraditional, "traditional C rejects automatic "
7177 "aggregate initialization");
7179 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7180 DECL_INITIAL (decl) = value;
7182 /* ANSI wants warnings about out-of-range constant initializers. */
7183 STRIP_TYPE_NOPS (value);
7184 if (TREE_STATIC (decl))
7185 constant_expression_warning (value);
7187 /* Check if we need to set array size from compound literal size. */
7188 if (TREE_CODE (type) == ARRAY_TYPE
7189 && TYPE_DOMAIN (type) == NULL_TREE
7190 && value != error_mark_node)
7192 tree inside_init = init;
7194 STRIP_TYPE_NOPS (inside_init);
7195 inside_init = fold (inside_init);
7197 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7199 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7201 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7203 /* For int foo[] = (int [3]){1}; we need to set array size
7204 now since later on array initializer will be just the
7205 brace enclosed list of the compound literal. */
7206 tree etype = strip_array_types (TREE_TYPE (decl));
7207 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7208 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7209 layout_type (type);
7210 layout_decl (cldecl, 0);
7211 TREE_TYPE (decl)
7212 = c_build_qualified_type (type, TYPE_QUALS (etype));
7218 /* Methods for storing and printing names for error messages. */
7220 /* Implement a spelling stack that allows components of a name to be pushed
7221 and popped. Each element on the stack is this structure. */
7223 struct spelling
7225 int kind;
7226 union
7228 unsigned HOST_WIDE_INT i;
7229 const char *s;
7230 } u;
7233 #define SPELLING_STRING 1
7234 #define SPELLING_MEMBER 2
7235 #define SPELLING_BOUNDS 3
7237 static struct spelling *spelling; /* Next stack element (unused). */
7238 static struct spelling *spelling_base; /* Spelling stack base. */
7239 static int spelling_size; /* Size of the spelling stack. */
7241 /* Macros to save and restore the spelling stack around push_... functions.
7242 Alternative to SAVE_SPELLING_STACK. */
7244 #define SPELLING_DEPTH() (spelling - spelling_base)
7245 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7247 /* Push an element on the spelling stack with type KIND and assign VALUE
7248 to MEMBER. */
7250 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7252 int depth = SPELLING_DEPTH (); \
7254 if (depth >= spelling_size) \
7256 spelling_size += 10; \
7257 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7258 spelling_size); \
7259 RESTORE_SPELLING_DEPTH (depth); \
7262 spelling->kind = (KIND); \
7263 spelling->MEMBER = (VALUE); \
7264 spelling++; \
7267 /* Push STRING on the stack. Printed literally. */
7269 static void
7270 push_string (const char *string)
7272 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7275 /* Push a member name on the stack. Printed as '.' STRING. */
7277 static void
7278 push_member_name (tree decl)
7280 const char *const string
7281 = (DECL_NAME (decl)
7282 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7283 : _("<anonymous>"));
7284 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7287 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7289 static void
7290 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7292 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7295 /* Compute the maximum size in bytes of the printed spelling. */
7297 static int
7298 spelling_length (void)
7300 int size = 0;
7301 struct spelling *p;
7303 for (p = spelling_base; p < spelling; p++)
7305 if (p->kind == SPELLING_BOUNDS)
7306 size += 25;
7307 else
7308 size += strlen (p->u.s) + 1;
7311 return size;
7314 /* Print the spelling to BUFFER and return it. */
7316 static char *
7317 print_spelling (char *buffer)
7319 char *d = buffer;
7320 struct spelling *p;
7322 for (p = spelling_base; p < spelling; p++)
7323 if (p->kind == SPELLING_BOUNDS)
7325 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7326 d += strlen (d);
7328 else
7330 const char *s;
7331 if (p->kind == SPELLING_MEMBER)
7332 *d++ = '.';
7333 for (s = p->u.s; (*d = *s++); d++)
7336 *d++ = '\0';
7337 return buffer;
7340 /* Digest the parser output INIT as an initializer for type TYPE.
7341 Return a C expression of type TYPE to represent the initial value.
7343 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7345 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7347 If INIT is a string constant, STRICT_STRING is true if it is
7348 unparenthesized or we should not warn here for it being parenthesized.
7349 For other types of INIT, STRICT_STRING is not used.
7351 INIT_LOC is the location of the INIT.
7353 REQUIRE_CONSTANT requests an error if non-constant initializers or
7354 elements are seen. */
7356 static tree
7357 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7358 bool null_pointer_constant, bool strict_string,
7359 int require_constant)
7361 enum tree_code code = TREE_CODE (type);
7362 tree inside_init = init;
7363 tree semantic_type = NULL_TREE;
7364 bool maybe_const = true;
7366 if (type == error_mark_node
7367 || !init
7368 || error_operand_p (init))
7369 return error_mark_node;
7371 STRIP_TYPE_NOPS (inside_init);
7373 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7375 semantic_type = TREE_TYPE (inside_init);
7376 inside_init = TREE_OPERAND (inside_init, 0);
7378 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7380 /* Initialization of an array of chars from a string constant
7381 optionally enclosed in braces. */
7383 if (code == ARRAY_TYPE && inside_init
7384 && TREE_CODE (inside_init) == STRING_CST)
7386 tree typ1
7387 = (TYPE_ATOMIC (TREE_TYPE (type))
7388 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7389 TYPE_QUAL_ATOMIC)
7390 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7391 /* Note that an array could be both an array of character type
7392 and an array of wchar_t if wchar_t is signed char or unsigned
7393 char. */
7394 bool char_array = (typ1 == char_type_node
7395 || typ1 == signed_char_type_node
7396 || typ1 == unsigned_char_type_node);
7397 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7398 bool char16_array = !!comptypes (typ1, char16_type_node);
7399 bool char32_array = !!comptypes (typ1, char32_type_node);
7401 if (char_array || wchar_array || char16_array || char32_array)
7403 struct c_expr expr;
7404 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7405 expr.value = inside_init;
7406 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7407 expr.original_type = NULL;
7408 maybe_warn_string_init (init_loc, type, expr);
7410 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7411 pedwarn_init (init_loc, OPT_Wpedantic,
7412 "initialization of a flexible array member");
7414 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7415 TYPE_MAIN_VARIANT (type)))
7416 return inside_init;
7418 if (char_array)
7420 if (typ2 != char_type_node)
7422 error_init (init_loc, "char-array initialized from wide "
7423 "string");
7424 return error_mark_node;
7427 else
7429 if (typ2 == char_type_node)
7431 error_init (init_loc, "wide character array initialized "
7432 "from non-wide string");
7433 return error_mark_node;
7435 else if (!comptypes(typ1, typ2))
7437 error_init (init_loc, "wide character array initialized "
7438 "from incompatible wide string");
7439 return error_mark_node;
7443 TREE_TYPE (inside_init) = type;
7444 if (TYPE_DOMAIN (type) != NULL_TREE
7445 && TYPE_SIZE (type) != NULL_TREE
7446 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7448 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7450 /* Subtract the size of a single (possibly wide) character
7451 because it's ok to ignore the terminating null char
7452 that is counted in the length of the constant. */
7453 if (compare_tree_int (TYPE_SIZE_UNIT (type),
7454 (len - (TYPE_PRECISION (typ1)
7455 / BITS_PER_UNIT))) < 0)
7456 pedwarn_init (init_loc, 0,
7457 ("initializer-string for array of chars "
7458 "is too long"));
7459 else if (warn_cxx_compat
7460 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7461 warning_at (init_loc, OPT_Wc___compat,
7462 ("initializer-string for array chars "
7463 "is too long for C++"));
7466 return inside_init;
7468 else if (INTEGRAL_TYPE_P (typ1))
7470 error_init (init_loc, "array of inappropriate type initialized "
7471 "from string constant");
7472 return error_mark_node;
7476 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7477 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7478 below and handle as a constructor. */
7479 if (code == VECTOR_TYPE
7480 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7481 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7482 && TREE_CONSTANT (inside_init))
7484 if (TREE_CODE (inside_init) == VECTOR_CST
7485 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7486 TYPE_MAIN_VARIANT (type)))
7487 return inside_init;
7489 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7491 unsigned HOST_WIDE_INT ix;
7492 tree value;
7493 bool constant_p = true;
7495 /* Iterate through elements and check if all constructor
7496 elements are *_CSTs. */
7497 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7498 if (!CONSTANT_CLASS_P (value))
7500 constant_p = false;
7501 break;
7504 if (constant_p)
7505 return build_vector_from_ctor (type,
7506 CONSTRUCTOR_ELTS (inside_init));
7510 if (warn_sequence_point)
7511 verify_sequence_points (inside_init);
7513 /* Any type can be initialized
7514 from an expression of the same type, optionally with braces. */
7516 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7517 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7518 TYPE_MAIN_VARIANT (type))
7519 || (code == ARRAY_TYPE
7520 && comptypes (TREE_TYPE (inside_init), type))
7521 || (code == VECTOR_TYPE
7522 && comptypes (TREE_TYPE (inside_init), type))
7523 || (code == POINTER_TYPE
7524 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7525 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7526 TREE_TYPE (type)))))
7528 if (code == POINTER_TYPE)
7530 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7532 if (TREE_CODE (inside_init) == STRING_CST
7533 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7534 inside_init = array_to_pointer_conversion
7535 (init_loc, inside_init);
7536 else
7538 error_init (init_loc, "invalid use of non-lvalue array");
7539 return error_mark_node;
7544 if (code == VECTOR_TYPE)
7545 /* Although the types are compatible, we may require a
7546 conversion. */
7547 inside_init = convert (type, inside_init);
7549 if (require_constant
7550 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7552 /* As an extension, allow initializing objects with static storage
7553 duration with compound literals (which are then treated just as
7554 the brace enclosed list they contain). Also allow this for
7555 vectors, as we can only assign them with compound literals. */
7556 if (flag_isoc99 && code != VECTOR_TYPE)
7557 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7558 "is not constant");
7559 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7560 inside_init = DECL_INITIAL (decl);
7563 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7564 && TREE_CODE (inside_init) != CONSTRUCTOR)
7566 error_init (init_loc, "array initialized from non-constant array "
7567 "expression");
7568 return error_mark_node;
7571 /* Compound expressions can only occur here if -Wpedantic or
7572 -pedantic-errors is specified. In the later case, we always want
7573 an error. In the former case, we simply want a warning. */
7574 if (require_constant && pedantic
7575 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7577 inside_init
7578 = valid_compound_expr_initializer (inside_init,
7579 TREE_TYPE (inside_init));
7580 if (inside_init == error_mark_node)
7581 error_init (init_loc, "initializer element is not constant");
7582 else
7583 pedwarn_init (init_loc, OPT_Wpedantic,
7584 "initializer element is not constant");
7585 if (flag_pedantic_errors)
7586 inside_init = error_mark_node;
7588 else if (require_constant
7589 && !initializer_constant_valid_p (inside_init,
7590 TREE_TYPE (inside_init)))
7592 error_init (init_loc, "initializer element is not constant");
7593 inside_init = error_mark_node;
7595 else if (require_constant && !maybe_const)
7596 pedwarn_init (init_loc, OPT_Wpedantic,
7597 "initializer element is not a constant expression");
7599 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7600 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7601 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7602 type, inside_init, origtype,
7603 ic_init, null_pointer_constant,
7604 NULL_TREE, NULL_TREE, 0);
7605 return inside_init;
7608 /* Handle scalar types, including conversions. */
7610 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7611 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7612 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7614 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7615 && (TREE_CODE (init) == STRING_CST
7616 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7617 inside_init = init = array_to_pointer_conversion (init_loc, init);
7618 if (semantic_type)
7619 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7620 inside_init);
7621 inside_init
7622 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7623 inside_init, origtype, ic_init,
7624 null_pointer_constant, NULL_TREE, NULL_TREE,
7627 /* Check to see if we have already given an error message. */
7628 if (inside_init == error_mark_node)
7630 else if (require_constant && !TREE_CONSTANT (inside_init))
7632 error_init (init_loc, "initializer element is not constant");
7633 inside_init = error_mark_node;
7635 else if (require_constant
7636 && !initializer_constant_valid_p (inside_init,
7637 TREE_TYPE (inside_init)))
7639 error_init (init_loc, "initializer element is not computable at "
7640 "load time");
7641 inside_init = error_mark_node;
7643 else if (require_constant && !maybe_const)
7644 pedwarn_init (init_loc, OPT_Wpedantic,
7645 "initializer element is not a constant expression");
7647 return inside_init;
7650 /* Come here only for records and arrays. */
7652 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7654 error_init (init_loc, "variable-sized object may not be initialized");
7655 return error_mark_node;
7658 error_init (init_loc, "invalid initializer");
7659 return error_mark_node;
7662 /* Handle initializers that use braces. */
7664 /* Type of object we are accumulating a constructor for.
7665 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7666 static tree constructor_type;
7668 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7669 left to fill. */
7670 static tree constructor_fields;
7672 /* For an ARRAY_TYPE, this is the specified index
7673 at which to store the next element we get. */
7674 static tree constructor_index;
7676 /* For an ARRAY_TYPE, this is the maximum index. */
7677 static tree constructor_max_index;
7679 /* For a RECORD_TYPE, this is the first field not yet written out. */
7680 static tree constructor_unfilled_fields;
7682 /* For an ARRAY_TYPE, this is the index of the first element
7683 not yet written out. */
7684 static tree constructor_unfilled_index;
7686 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7687 This is so we can generate gaps between fields, when appropriate. */
7688 static tree constructor_bit_index;
7690 /* If we are saving up the elements rather than allocating them,
7691 this is the list of elements so far (in reverse order,
7692 most recent first). */
7693 static vec<constructor_elt, va_gc> *constructor_elements;
7695 /* 1 if constructor should be incrementally stored into a constructor chain,
7696 0 if all the elements should be kept in AVL tree. */
7697 static int constructor_incremental;
7699 /* 1 if so far this constructor's elements are all compile-time constants. */
7700 static int constructor_constant;
7702 /* 1 if so far this constructor's elements are all valid address constants. */
7703 static int constructor_simple;
7705 /* 1 if this constructor has an element that cannot be part of a
7706 constant expression. */
7707 static int constructor_nonconst;
7709 /* 1 if this constructor is erroneous so far. */
7710 static int constructor_erroneous;
7712 /* 1 if this constructor is the universal zero initializer { 0 }. */
7713 static int constructor_zeroinit;
7715 /* Structure for managing pending initializer elements, organized as an
7716 AVL tree. */
7718 struct init_node
7720 struct init_node *left, *right;
7721 struct init_node *parent;
7722 int balance;
7723 tree purpose;
7724 tree value;
7725 tree origtype;
7728 /* Tree of pending elements at this constructor level.
7729 These are elements encountered out of order
7730 which belong at places we haven't reached yet in actually
7731 writing the output.
7732 Will never hold tree nodes across GC runs. */
7733 static struct init_node *constructor_pending_elts;
7735 /* The SPELLING_DEPTH of this constructor. */
7736 static int constructor_depth;
7738 /* DECL node for which an initializer is being read.
7739 0 means we are reading a constructor expression
7740 such as (struct foo) {...}. */
7741 static tree constructor_decl;
7743 /* Nonzero if this is an initializer for a top-level decl. */
7744 static int constructor_top_level;
7746 /* Nonzero if there were any member designators in this initializer. */
7747 static int constructor_designated;
7749 /* Nesting depth of designator list. */
7750 static int designator_depth;
7752 /* Nonzero if there were diagnosed errors in this designator list. */
7753 static int designator_erroneous;
7756 /* This stack has a level for each implicit or explicit level of
7757 structuring in the initializer, including the outermost one. It
7758 saves the values of most of the variables above. */
7760 struct constructor_range_stack;
7762 struct constructor_stack
7764 struct constructor_stack *next;
7765 tree type;
7766 tree fields;
7767 tree index;
7768 tree max_index;
7769 tree unfilled_index;
7770 tree unfilled_fields;
7771 tree bit_index;
7772 vec<constructor_elt, va_gc> *elements;
7773 struct init_node *pending_elts;
7774 int offset;
7775 int depth;
7776 /* If value nonzero, this value should replace the entire
7777 constructor at this level. */
7778 struct c_expr replacement_value;
7779 struct constructor_range_stack *range_stack;
7780 char constant;
7781 char simple;
7782 char nonconst;
7783 char implicit;
7784 char erroneous;
7785 char outer;
7786 char incremental;
7787 char designated;
7788 int designator_depth;
7791 static struct constructor_stack *constructor_stack;
7793 /* This stack represents designators from some range designator up to
7794 the last designator in the list. */
7796 struct constructor_range_stack
7798 struct constructor_range_stack *next, *prev;
7799 struct constructor_stack *stack;
7800 tree range_start;
7801 tree index;
7802 tree range_end;
7803 tree fields;
7806 static struct constructor_range_stack *constructor_range_stack;
7808 /* This stack records separate initializers that are nested.
7809 Nested initializers can't happen in ANSI C, but GNU C allows them
7810 in cases like { ... (struct foo) { ... } ... }. */
7812 struct initializer_stack
7814 struct initializer_stack *next;
7815 tree decl;
7816 struct constructor_stack *constructor_stack;
7817 struct constructor_range_stack *constructor_range_stack;
7818 vec<constructor_elt, va_gc> *elements;
7819 struct spelling *spelling;
7820 struct spelling *spelling_base;
7821 int spelling_size;
7822 char top_level;
7823 char require_constant_value;
7824 char require_constant_elements;
7825 rich_location *missing_brace_richloc;
7828 static struct initializer_stack *initializer_stack;
7830 /* Prepare to parse and output the initializer for variable DECL. */
7832 void
7833 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7834 rich_location *richloc)
7836 const char *locus;
7837 struct initializer_stack *p = XNEW (struct initializer_stack);
7839 p->decl = constructor_decl;
7840 p->require_constant_value = require_constant_value;
7841 p->require_constant_elements = require_constant_elements;
7842 p->constructor_stack = constructor_stack;
7843 p->constructor_range_stack = constructor_range_stack;
7844 p->elements = constructor_elements;
7845 p->spelling = spelling;
7846 p->spelling_base = spelling_base;
7847 p->spelling_size = spelling_size;
7848 p->top_level = constructor_top_level;
7849 p->next = initializer_stack;
7850 p->missing_brace_richloc = richloc;
7851 initializer_stack = p;
7853 constructor_decl = decl;
7854 constructor_designated = 0;
7855 constructor_top_level = top_level;
7857 if (decl != NULL_TREE && decl != error_mark_node)
7859 require_constant_value = TREE_STATIC (decl);
7860 require_constant_elements
7861 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7862 /* For a scalar, you can always use any value to initialize,
7863 even within braces. */
7864 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7865 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7867 else
7869 require_constant_value = 0;
7870 require_constant_elements = 0;
7871 locus = _("(anonymous)");
7874 constructor_stack = 0;
7875 constructor_range_stack = 0;
7877 found_missing_braces = 0;
7879 spelling_base = 0;
7880 spelling_size = 0;
7881 RESTORE_SPELLING_DEPTH (0);
7883 if (locus)
7884 push_string (locus);
7887 void
7888 finish_init (void)
7890 struct initializer_stack *p = initializer_stack;
7892 /* Free the whole constructor stack of this initializer. */
7893 while (constructor_stack)
7895 struct constructor_stack *q = constructor_stack;
7896 constructor_stack = q->next;
7897 free (q);
7900 gcc_assert (!constructor_range_stack);
7902 /* Pop back to the data of the outer initializer (if any). */
7903 free (spelling_base);
7905 constructor_decl = p->decl;
7906 require_constant_value = p->require_constant_value;
7907 require_constant_elements = p->require_constant_elements;
7908 constructor_stack = p->constructor_stack;
7909 constructor_range_stack = p->constructor_range_stack;
7910 constructor_elements = p->elements;
7911 spelling = p->spelling;
7912 spelling_base = p->spelling_base;
7913 spelling_size = p->spelling_size;
7914 constructor_top_level = p->top_level;
7915 initializer_stack = p->next;
7916 free (p);
7919 /* Call here when we see the initializer is surrounded by braces.
7920 This is instead of a call to push_init_level;
7921 it is matched by a call to pop_init_level.
7923 TYPE is the type to initialize, for a constructor expression.
7924 For an initializer for a decl, TYPE is zero. */
7926 void
7927 really_start_incremental_init (tree type)
7929 struct constructor_stack *p = XNEW (struct constructor_stack);
7931 if (type == NULL_TREE)
7932 type = TREE_TYPE (constructor_decl);
7934 if (VECTOR_TYPE_P (type)
7935 && TYPE_VECTOR_OPAQUE (type))
7936 error ("opaque vector types cannot be initialized");
7938 p->type = constructor_type;
7939 p->fields = constructor_fields;
7940 p->index = constructor_index;
7941 p->max_index = constructor_max_index;
7942 p->unfilled_index = constructor_unfilled_index;
7943 p->unfilled_fields = constructor_unfilled_fields;
7944 p->bit_index = constructor_bit_index;
7945 p->elements = constructor_elements;
7946 p->constant = constructor_constant;
7947 p->simple = constructor_simple;
7948 p->nonconst = constructor_nonconst;
7949 p->erroneous = constructor_erroneous;
7950 p->pending_elts = constructor_pending_elts;
7951 p->depth = constructor_depth;
7952 p->replacement_value.value = 0;
7953 p->replacement_value.original_code = ERROR_MARK;
7954 p->replacement_value.original_type = NULL;
7955 p->implicit = 0;
7956 p->range_stack = 0;
7957 p->outer = 0;
7958 p->incremental = constructor_incremental;
7959 p->designated = constructor_designated;
7960 p->designator_depth = designator_depth;
7961 p->next = 0;
7962 constructor_stack = p;
7964 constructor_constant = 1;
7965 constructor_simple = 1;
7966 constructor_nonconst = 0;
7967 constructor_depth = SPELLING_DEPTH ();
7968 constructor_elements = NULL;
7969 constructor_pending_elts = 0;
7970 constructor_type = type;
7971 constructor_incremental = 1;
7972 constructor_designated = 0;
7973 constructor_zeroinit = 1;
7974 designator_depth = 0;
7975 designator_erroneous = 0;
7977 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7979 constructor_fields = TYPE_FIELDS (constructor_type);
7980 /* Skip any nameless bit fields at the beginning. */
7981 while (constructor_fields != NULL_TREE
7982 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
7983 constructor_fields = DECL_CHAIN (constructor_fields);
7985 constructor_unfilled_fields = constructor_fields;
7986 constructor_bit_index = bitsize_zero_node;
7988 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7990 if (TYPE_DOMAIN (constructor_type))
7992 constructor_max_index
7993 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7995 /* Detect non-empty initializations of zero-length arrays. */
7996 if (constructor_max_index == NULL_TREE
7997 && TYPE_SIZE (constructor_type))
7998 constructor_max_index = integer_minus_one_node;
8000 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8001 to initialize VLAs will cause a proper error; avoid tree
8002 checking errors as well by setting a safe value. */
8003 if (constructor_max_index
8004 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8005 constructor_max_index = integer_minus_one_node;
8007 constructor_index
8008 = convert (bitsizetype,
8009 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8011 else
8013 constructor_index = bitsize_zero_node;
8014 constructor_max_index = NULL_TREE;
8017 constructor_unfilled_index = constructor_index;
8019 else if (VECTOR_TYPE_P (constructor_type))
8021 /* Vectors are like simple fixed-size arrays. */
8022 constructor_max_index =
8023 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8024 constructor_index = bitsize_zero_node;
8025 constructor_unfilled_index = constructor_index;
8027 else
8029 /* Handle the case of int x = {5}; */
8030 constructor_fields = constructor_type;
8031 constructor_unfilled_fields = constructor_type;
8035 extern location_t last_init_list_comma;
8037 /* Called when we see an open brace for a nested initializer. Finish
8038 off any pending levels with implicit braces. */
8039 void
8040 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8042 while (constructor_stack->implicit)
8044 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8045 && constructor_fields == NULL_TREE)
8046 process_init_element (input_location,
8047 pop_init_level (loc, 1, braced_init_obstack,
8048 last_init_list_comma),
8049 true, braced_init_obstack);
8050 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8051 && constructor_max_index
8052 && tree_int_cst_lt (constructor_max_index,
8053 constructor_index))
8054 process_init_element (input_location,
8055 pop_init_level (loc, 1, braced_init_obstack,
8056 last_init_list_comma),
8057 true, braced_init_obstack);
8058 else
8059 break;
8063 /* Push down into a subobject, for initialization.
8064 If this is for an explicit set of braces, IMPLICIT is 0.
8065 If it is because the next element belongs at a lower level,
8066 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8068 void
8069 push_init_level (location_t loc, int implicit,
8070 struct obstack *braced_init_obstack)
8072 struct constructor_stack *p;
8073 tree value = NULL_TREE;
8075 /* Unless this is an explicit brace, we need to preserve previous
8076 content if any. */
8077 if (implicit)
8079 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8080 value = find_init_member (constructor_fields, braced_init_obstack);
8081 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8082 value = find_init_member (constructor_index, braced_init_obstack);
8085 p = XNEW (struct constructor_stack);
8086 p->type = constructor_type;
8087 p->fields = constructor_fields;
8088 p->index = constructor_index;
8089 p->max_index = constructor_max_index;
8090 p->unfilled_index = constructor_unfilled_index;
8091 p->unfilled_fields = constructor_unfilled_fields;
8092 p->bit_index = constructor_bit_index;
8093 p->elements = constructor_elements;
8094 p->constant = constructor_constant;
8095 p->simple = constructor_simple;
8096 p->nonconst = constructor_nonconst;
8097 p->erroneous = constructor_erroneous;
8098 p->pending_elts = constructor_pending_elts;
8099 p->depth = constructor_depth;
8100 p->replacement_value.value = NULL_TREE;
8101 p->replacement_value.original_code = ERROR_MARK;
8102 p->replacement_value.original_type = NULL;
8103 p->implicit = implicit;
8104 p->outer = 0;
8105 p->incremental = constructor_incremental;
8106 p->designated = constructor_designated;
8107 p->designator_depth = designator_depth;
8108 p->next = constructor_stack;
8109 p->range_stack = 0;
8110 constructor_stack = p;
8112 constructor_constant = 1;
8113 constructor_simple = 1;
8114 constructor_nonconst = 0;
8115 constructor_depth = SPELLING_DEPTH ();
8116 constructor_elements = NULL;
8117 constructor_incremental = 1;
8118 constructor_designated = 0;
8119 constructor_pending_elts = 0;
8120 if (!implicit)
8122 p->range_stack = constructor_range_stack;
8123 constructor_range_stack = 0;
8124 designator_depth = 0;
8125 designator_erroneous = 0;
8128 /* Don't die if an entire brace-pair level is superfluous
8129 in the containing level. */
8130 if (constructor_type == NULL_TREE)
8132 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8134 /* Don't die if there are extra init elts at the end. */
8135 if (constructor_fields == NULL_TREE)
8136 constructor_type = NULL_TREE;
8137 else
8139 constructor_type = TREE_TYPE (constructor_fields);
8140 push_member_name (constructor_fields);
8141 constructor_depth++;
8143 /* If upper initializer is designated, then mark this as
8144 designated too to prevent bogus warnings. */
8145 constructor_designated = p->designated;
8147 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8149 constructor_type = TREE_TYPE (constructor_type);
8150 push_array_bounds (tree_to_uhwi (constructor_index));
8151 constructor_depth++;
8154 if (constructor_type == NULL_TREE)
8156 error_init (loc, "extra brace group at end of initializer");
8157 constructor_fields = NULL_TREE;
8158 constructor_unfilled_fields = NULL_TREE;
8159 return;
8162 if (value && TREE_CODE (value) == CONSTRUCTOR)
8164 constructor_constant = TREE_CONSTANT (value);
8165 constructor_simple = TREE_STATIC (value);
8166 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8167 constructor_elements = CONSTRUCTOR_ELTS (value);
8168 if (!vec_safe_is_empty (constructor_elements)
8169 && (TREE_CODE (constructor_type) == RECORD_TYPE
8170 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8171 set_nonincremental_init (braced_init_obstack);
8174 if (implicit == 1)
8176 found_missing_braces = 1;
8177 if (initializer_stack->missing_brace_richloc)
8178 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8179 (loc, "{");
8182 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8184 constructor_fields = TYPE_FIELDS (constructor_type);
8185 /* Skip any nameless bit fields at the beginning. */
8186 while (constructor_fields != NULL_TREE
8187 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8188 constructor_fields = DECL_CHAIN (constructor_fields);
8190 constructor_unfilled_fields = constructor_fields;
8191 constructor_bit_index = bitsize_zero_node;
8193 else if (VECTOR_TYPE_P (constructor_type))
8195 /* Vectors are like simple fixed-size arrays. */
8196 constructor_max_index =
8197 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8198 constructor_index = bitsize_int (0);
8199 constructor_unfilled_index = constructor_index;
8201 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8203 if (TYPE_DOMAIN (constructor_type))
8205 constructor_max_index
8206 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8208 /* Detect non-empty initializations of zero-length arrays. */
8209 if (constructor_max_index == NULL_TREE
8210 && TYPE_SIZE (constructor_type))
8211 constructor_max_index = integer_minus_one_node;
8213 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8214 to initialize VLAs will cause a proper error; avoid tree
8215 checking errors as well by setting a safe value. */
8216 if (constructor_max_index
8217 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8218 constructor_max_index = integer_minus_one_node;
8220 constructor_index
8221 = convert (bitsizetype,
8222 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8224 else
8225 constructor_index = bitsize_zero_node;
8227 constructor_unfilled_index = constructor_index;
8228 if (value && TREE_CODE (value) == STRING_CST)
8230 /* We need to split the char/wchar array into individual
8231 characters, so that we don't have to special case it
8232 everywhere. */
8233 set_nonincremental_init_from_string (value, braced_init_obstack);
8236 else
8238 if (constructor_type != error_mark_node)
8239 warning_init (input_location, 0, "braces around scalar initializer");
8240 constructor_fields = constructor_type;
8241 constructor_unfilled_fields = constructor_type;
8245 /* At the end of an implicit or explicit brace level,
8246 finish up that level of constructor. If a single expression
8247 with redundant braces initialized that level, return the
8248 c_expr structure for that expression. Otherwise, the original_code
8249 element is set to ERROR_MARK.
8250 If we were outputting the elements as they are read, return 0 as the value
8251 from inner levels (process_init_element ignores that),
8252 but return error_mark_node as the value from the outermost level
8253 (that's what we want to put in DECL_INITIAL).
8254 Otherwise, return a CONSTRUCTOR expression as the value. */
8256 struct c_expr
8257 pop_init_level (location_t loc, int implicit,
8258 struct obstack *braced_init_obstack,
8259 location_t insert_before)
8261 struct constructor_stack *p;
8262 struct c_expr ret;
8263 ret.value = NULL_TREE;
8264 ret.original_code = ERROR_MARK;
8265 ret.original_type = NULL;
8267 if (implicit == 0)
8269 /* When we come to an explicit close brace,
8270 pop any inner levels that didn't have explicit braces. */
8271 while (constructor_stack->implicit)
8272 process_init_element (input_location,
8273 pop_init_level (loc, 1, braced_init_obstack,
8274 insert_before),
8275 true, braced_init_obstack);
8276 gcc_assert (!constructor_range_stack);
8278 else
8279 if (initializer_stack->missing_brace_richloc)
8280 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8281 (insert_before, "}");
8283 /* Now output all pending elements. */
8284 constructor_incremental = 1;
8285 output_pending_init_elements (1, braced_init_obstack);
8287 p = constructor_stack;
8289 /* Error for initializing a flexible array member, or a zero-length
8290 array member in an inappropriate context. */
8291 if (constructor_type && constructor_fields
8292 && TREE_CODE (constructor_type) == ARRAY_TYPE
8293 && TYPE_DOMAIN (constructor_type)
8294 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8296 /* Silently discard empty initializations. The parser will
8297 already have pedwarned for empty brackets. */
8298 if (integer_zerop (constructor_unfilled_index))
8299 constructor_type = NULL_TREE;
8300 else
8302 gcc_assert (!TYPE_SIZE (constructor_type));
8304 if (constructor_depth > 2)
8305 error_init (loc, "initialization of flexible array member in a nested context");
8306 else
8307 pedwarn_init (loc, OPT_Wpedantic,
8308 "initialization of a flexible array member");
8310 /* We have already issued an error message for the existence
8311 of a flexible array member not at the end of the structure.
8312 Discard the initializer so that we do not die later. */
8313 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8314 constructor_type = NULL_TREE;
8318 switch (vec_safe_length (constructor_elements))
8320 case 0:
8321 /* Initialization with { } counts as zeroinit. */
8322 constructor_zeroinit = 1;
8323 break;
8324 case 1:
8325 /* This might be zeroinit as well. */
8326 if (integer_zerop ((*constructor_elements)[0].value))
8327 constructor_zeroinit = 1;
8328 break;
8329 default:
8330 /* If the constructor has more than one element, it can't be { 0 }. */
8331 constructor_zeroinit = 0;
8332 break;
8335 /* Warn when some structs are initialized with direct aggregation. */
8336 if (!implicit && found_missing_braces && warn_missing_braces
8337 && !constructor_zeroinit)
8339 gcc_assert (initializer_stack->missing_brace_richloc);
8340 warning_at (initializer_stack->missing_brace_richloc,
8341 OPT_Wmissing_braces,
8342 "missing braces around initializer");
8345 /* Warn when some struct elements are implicitly initialized to zero. */
8346 if (warn_missing_field_initializers
8347 && constructor_type
8348 && TREE_CODE (constructor_type) == RECORD_TYPE
8349 && constructor_unfilled_fields)
8351 /* Do not warn for flexible array members or zero-length arrays. */
8352 while (constructor_unfilled_fields
8353 && (!DECL_SIZE (constructor_unfilled_fields)
8354 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8355 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8357 if (constructor_unfilled_fields
8358 /* Do not warn if this level of the initializer uses member
8359 designators; it is likely to be deliberate. */
8360 && !constructor_designated
8361 /* Do not warn about initializing with { 0 } or with { }. */
8362 && !constructor_zeroinit)
8364 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8365 "missing initializer for field %qD of %qT",
8366 constructor_unfilled_fields,
8367 constructor_type))
8368 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8369 "%qD declared here", constructor_unfilled_fields);
8373 /* Pad out the end of the structure. */
8374 if (p->replacement_value.value)
8375 /* If this closes a superfluous brace pair,
8376 just pass out the element between them. */
8377 ret = p->replacement_value;
8378 else if (constructor_type == NULL_TREE)
8380 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8381 && TREE_CODE (constructor_type) != ARRAY_TYPE
8382 && !VECTOR_TYPE_P (constructor_type))
8384 /* A nonincremental scalar initializer--just return
8385 the element, after verifying there is just one. */
8386 if (vec_safe_is_empty (constructor_elements))
8388 if (!constructor_erroneous)
8389 error_init (loc, "empty scalar initializer");
8390 ret.value = error_mark_node;
8392 else if (vec_safe_length (constructor_elements) != 1)
8394 error_init (loc, "extra elements in scalar initializer");
8395 ret.value = (*constructor_elements)[0].value;
8397 else
8398 ret.value = (*constructor_elements)[0].value;
8400 else
8402 if (constructor_erroneous)
8403 ret.value = error_mark_node;
8404 else
8406 ret.value = build_constructor (constructor_type,
8407 constructor_elements);
8408 if (constructor_constant)
8409 TREE_CONSTANT (ret.value) = 1;
8410 if (constructor_constant && constructor_simple)
8411 TREE_STATIC (ret.value) = 1;
8412 if (constructor_nonconst)
8413 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8417 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8419 if (constructor_nonconst)
8420 ret.original_code = C_MAYBE_CONST_EXPR;
8421 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8422 ret.original_code = ERROR_MARK;
8425 constructor_type = p->type;
8426 constructor_fields = p->fields;
8427 constructor_index = p->index;
8428 constructor_max_index = p->max_index;
8429 constructor_unfilled_index = p->unfilled_index;
8430 constructor_unfilled_fields = p->unfilled_fields;
8431 constructor_bit_index = p->bit_index;
8432 constructor_elements = p->elements;
8433 constructor_constant = p->constant;
8434 constructor_simple = p->simple;
8435 constructor_nonconst = p->nonconst;
8436 constructor_erroneous = p->erroneous;
8437 constructor_incremental = p->incremental;
8438 constructor_designated = p->designated;
8439 designator_depth = p->designator_depth;
8440 constructor_pending_elts = p->pending_elts;
8441 constructor_depth = p->depth;
8442 if (!p->implicit)
8443 constructor_range_stack = p->range_stack;
8444 RESTORE_SPELLING_DEPTH (constructor_depth);
8446 constructor_stack = p->next;
8447 free (p);
8449 if (ret.value == NULL_TREE && constructor_stack == 0)
8450 ret.value = error_mark_node;
8451 return ret;
8454 /* Common handling for both array range and field name designators.
8455 ARRAY argument is nonzero for array ranges. Returns false for success. */
8457 static bool
8458 set_designator (location_t loc, bool array,
8459 struct obstack *braced_init_obstack)
8461 tree subtype;
8462 enum tree_code subcode;
8464 /* Don't die if an entire brace-pair level is superfluous
8465 in the containing level. */
8466 if (constructor_type == NULL_TREE)
8467 return true;
8469 /* If there were errors in this designator list already, bail out
8470 silently. */
8471 if (designator_erroneous)
8472 return true;
8474 if (!designator_depth)
8476 gcc_assert (!constructor_range_stack);
8478 /* Designator list starts at the level of closest explicit
8479 braces. */
8480 while (constructor_stack->implicit)
8481 process_init_element (input_location,
8482 pop_init_level (loc, 1, braced_init_obstack,
8483 last_init_list_comma),
8484 true, braced_init_obstack);
8485 constructor_designated = 1;
8486 return false;
8489 switch (TREE_CODE (constructor_type))
8491 case RECORD_TYPE:
8492 case UNION_TYPE:
8493 subtype = TREE_TYPE (constructor_fields);
8494 if (subtype != error_mark_node)
8495 subtype = TYPE_MAIN_VARIANT (subtype);
8496 break;
8497 case ARRAY_TYPE:
8498 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8499 break;
8500 default:
8501 gcc_unreachable ();
8504 subcode = TREE_CODE (subtype);
8505 if (array && subcode != ARRAY_TYPE)
8507 error_init (loc, "array index in non-array initializer");
8508 return true;
8510 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8512 error_init (loc, "field name not in record or union initializer");
8513 return true;
8516 constructor_designated = 1;
8517 finish_implicit_inits (loc, braced_init_obstack);
8518 push_init_level (loc, 2, braced_init_obstack);
8519 return false;
8522 /* If there are range designators in designator list, push a new designator
8523 to constructor_range_stack. RANGE_END is end of such stack range or
8524 NULL_TREE if there is no range designator at this level. */
8526 static void
8527 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8529 struct constructor_range_stack *p;
8531 p = (struct constructor_range_stack *)
8532 obstack_alloc (braced_init_obstack,
8533 sizeof (struct constructor_range_stack));
8534 p->prev = constructor_range_stack;
8535 p->next = 0;
8536 p->fields = constructor_fields;
8537 p->range_start = constructor_index;
8538 p->index = constructor_index;
8539 p->stack = constructor_stack;
8540 p->range_end = range_end;
8541 if (constructor_range_stack)
8542 constructor_range_stack->next = p;
8543 constructor_range_stack = p;
8546 /* Within an array initializer, specify the next index to be initialized.
8547 FIRST is that index. If LAST is nonzero, then initialize a range
8548 of indices, running from FIRST through LAST. */
8550 void
8551 set_init_index (location_t loc, tree first, tree last,
8552 struct obstack *braced_init_obstack)
8554 if (set_designator (loc, true, braced_init_obstack))
8555 return;
8557 designator_erroneous = 1;
8559 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8560 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8562 error_init (loc, "array index in initializer not of integer type");
8563 return;
8566 if (TREE_CODE (first) != INTEGER_CST)
8568 first = c_fully_fold (first, false, NULL);
8569 if (TREE_CODE (first) == INTEGER_CST)
8570 pedwarn_init (loc, OPT_Wpedantic,
8571 "array index in initializer is not "
8572 "an integer constant expression");
8575 if (last && TREE_CODE (last) != INTEGER_CST)
8577 last = c_fully_fold (last, false, NULL);
8578 if (TREE_CODE (last) == INTEGER_CST)
8579 pedwarn_init (loc, OPT_Wpedantic,
8580 "array index in initializer is not "
8581 "an integer constant expression");
8584 if (TREE_CODE (first) != INTEGER_CST)
8585 error_init (loc, "nonconstant array index in initializer");
8586 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8587 error_init (loc, "nonconstant array index in initializer");
8588 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8589 error_init (loc, "array index in non-array initializer");
8590 else if (tree_int_cst_sgn (first) == -1)
8591 error_init (loc, "array index in initializer exceeds array bounds");
8592 else if (constructor_max_index
8593 && tree_int_cst_lt (constructor_max_index, first))
8594 error_init (loc, "array index in initializer exceeds array bounds");
8595 else
8597 constant_expression_warning (first);
8598 if (last)
8599 constant_expression_warning (last);
8600 constructor_index = convert (bitsizetype, first);
8601 if (tree_int_cst_lt (constructor_index, first))
8603 constructor_index = copy_node (constructor_index);
8604 TREE_OVERFLOW (constructor_index) = 1;
8607 if (last)
8609 if (tree_int_cst_equal (first, last))
8610 last = NULL_TREE;
8611 else if (tree_int_cst_lt (last, first))
8613 error_init (loc, "empty index range in initializer");
8614 last = NULL_TREE;
8616 else
8618 last = convert (bitsizetype, last);
8619 if (constructor_max_index != NULL_TREE
8620 && tree_int_cst_lt (constructor_max_index, last))
8622 error_init (loc, "array index range in initializer exceeds "
8623 "array bounds");
8624 last = NULL_TREE;
8629 designator_depth++;
8630 designator_erroneous = 0;
8631 if (constructor_range_stack || last)
8632 push_range_stack (last, braced_init_obstack);
8636 /* Within a struct initializer, specify the next field to be initialized. */
8638 void
8639 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8640 struct obstack *braced_init_obstack)
8642 tree field;
8644 if (set_designator (loc, false, braced_init_obstack))
8645 return;
8647 designator_erroneous = 1;
8649 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8651 error_init (loc, "field name not in record or union initializer");
8652 return;
8655 field = lookup_field (constructor_type, fieldname);
8657 if (field == NULL_TREE)
8659 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8660 if (guessed_id)
8662 gcc_rich_location rich_loc (fieldname_loc);
8663 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8664 error_at (&rich_loc,
8665 "%qT has no member named %qE; did you mean %qE?",
8666 constructor_type, fieldname, guessed_id);
8668 else
8669 error_at (fieldname_loc, "%qT has no member named %qE",
8670 constructor_type, fieldname);
8672 else
8675 constructor_fields = TREE_VALUE (field);
8676 designator_depth++;
8677 designator_erroneous = 0;
8678 if (constructor_range_stack)
8679 push_range_stack (NULL_TREE, braced_init_obstack);
8680 field = TREE_CHAIN (field);
8681 if (field)
8683 if (set_designator (loc, false, braced_init_obstack))
8684 return;
8687 while (field != NULL_TREE);
8690 /* Add a new initializer to the tree of pending initializers. PURPOSE
8691 identifies the initializer, either array index or field in a structure.
8692 VALUE is the value of that index or field. If ORIGTYPE is not
8693 NULL_TREE, it is the original type of VALUE.
8695 IMPLICIT is true if value comes from pop_init_level (1),
8696 the new initializer has been merged with the existing one
8697 and thus no warnings should be emitted about overriding an
8698 existing initializer. */
8700 static void
8701 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8702 bool implicit, struct obstack *braced_init_obstack)
8704 struct init_node *p, **q, *r;
8706 q = &constructor_pending_elts;
8707 p = 0;
8709 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8711 while (*q != 0)
8713 p = *q;
8714 if (tree_int_cst_lt (purpose, p->purpose))
8715 q = &p->left;
8716 else if (tree_int_cst_lt (p->purpose, purpose))
8717 q = &p->right;
8718 else
8720 if (!implicit)
8722 if (TREE_SIDE_EFFECTS (p->value))
8723 warning_init (loc, OPT_Woverride_init_side_effects,
8724 "initialized field with side-effects "
8725 "overwritten");
8726 else if (warn_override_init)
8727 warning_init (loc, OPT_Woverride_init,
8728 "initialized field overwritten");
8730 p->value = value;
8731 p->origtype = origtype;
8732 return;
8736 else
8738 tree bitpos;
8740 bitpos = bit_position (purpose);
8741 while (*q != NULL)
8743 p = *q;
8744 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8745 q = &p->left;
8746 else if (p->purpose != purpose)
8747 q = &p->right;
8748 else
8750 if (!implicit)
8752 if (TREE_SIDE_EFFECTS (p->value))
8753 warning_init (loc, OPT_Woverride_init_side_effects,
8754 "initialized field with side-effects "
8755 "overwritten");
8756 else if (warn_override_init)
8757 warning_init (loc, OPT_Woverride_init,
8758 "initialized field overwritten");
8760 p->value = value;
8761 p->origtype = origtype;
8762 return;
8767 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8768 sizeof (struct init_node));
8769 r->purpose = purpose;
8770 r->value = value;
8771 r->origtype = origtype;
8773 *q = r;
8774 r->parent = p;
8775 r->left = 0;
8776 r->right = 0;
8777 r->balance = 0;
8779 while (p)
8781 struct init_node *s;
8783 if (r == p->left)
8785 if (p->balance == 0)
8786 p->balance = -1;
8787 else if (p->balance < 0)
8789 if (r->balance < 0)
8791 /* L rotation. */
8792 p->left = r->right;
8793 if (p->left)
8794 p->left->parent = p;
8795 r->right = p;
8797 p->balance = 0;
8798 r->balance = 0;
8800 s = p->parent;
8801 p->parent = r;
8802 r->parent = s;
8803 if (s)
8805 if (s->left == p)
8806 s->left = r;
8807 else
8808 s->right = r;
8810 else
8811 constructor_pending_elts = r;
8813 else
8815 /* LR rotation. */
8816 struct init_node *t = r->right;
8818 r->right = t->left;
8819 if (r->right)
8820 r->right->parent = r;
8821 t->left = r;
8823 p->left = t->right;
8824 if (p->left)
8825 p->left->parent = p;
8826 t->right = p;
8828 p->balance = t->balance < 0;
8829 r->balance = -(t->balance > 0);
8830 t->balance = 0;
8832 s = p->parent;
8833 p->parent = t;
8834 r->parent = t;
8835 t->parent = s;
8836 if (s)
8838 if (s->left == p)
8839 s->left = t;
8840 else
8841 s->right = t;
8843 else
8844 constructor_pending_elts = t;
8846 break;
8848 else
8850 /* p->balance == +1; growth of left side balances the node. */
8851 p->balance = 0;
8852 break;
8855 else /* r == p->right */
8857 if (p->balance == 0)
8858 /* Growth propagation from right side. */
8859 p->balance++;
8860 else if (p->balance > 0)
8862 if (r->balance > 0)
8864 /* R rotation. */
8865 p->right = r->left;
8866 if (p->right)
8867 p->right->parent = p;
8868 r->left = p;
8870 p->balance = 0;
8871 r->balance = 0;
8873 s = p->parent;
8874 p->parent = r;
8875 r->parent = s;
8876 if (s)
8878 if (s->left == p)
8879 s->left = r;
8880 else
8881 s->right = r;
8883 else
8884 constructor_pending_elts = r;
8886 else /* r->balance == -1 */
8888 /* RL rotation */
8889 struct init_node *t = r->left;
8891 r->left = t->right;
8892 if (r->left)
8893 r->left->parent = r;
8894 t->right = r;
8896 p->right = t->left;
8897 if (p->right)
8898 p->right->parent = p;
8899 t->left = p;
8901 r->balance = (t->balance < 0);
8902 p->balance = -(t->balance > 0);
8903 t->balance = 0;
8905 s = p->parent;
8906 p->parent = t;
8907 r->parent = t;
8908 t->parent = s;
8909 if (s)
8911 if (s->left == p)
8912 s->left = t;
8913 else
8914 s->right = t;
8916 else
8917 constructor_pending_elts = t;
8919 break;
8921 else
8923 /* p->balance == -1; growth of right side balances the node. */
8924 p->balance = 0;
8925 break;
8929 r = p;
8930 p = p->parent;
8934 /* Build AVL tree from a sorted chain. */
8936 static void
8937 set_nonincremental_init (struct obstack * braced_init_obstack)
8939 unsigned HOST_WIDE_INT ix;
8940 tree index, value;
8942 if (TREE_CODE (constructor_type) != RECORD_TYPE
8943 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8944 return;
8946 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8947 add_pending_init (input_location, index, value, NULL_TREE, true,
8948 braced_init_obstack);
8949 constructor_elements = NULL;
8950 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8952 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8953 /* Skip any nameless bit fields at the beginning. */
8954 while (constructor_unfilled_fields != NULL_TREE
8955 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
8956 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8959 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8961 if (TYPE_DOMAIN (constructor_type))
8962 constructor_unfilled_index
8963 = convert (bitsizetype,
8964 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8965 else
8966 constructor_unfilled_index = bitsize_zero_node;
8968 constructor_incremental = 0;
8971 /* Build AVL tree from a string constant. */
8973 static void
8974 set_nonincremental_init_from_string (tree str,
8975 struct obstack * braced_init_obstack)
8977 tree value, purpose, type;
8978 HOST_WIDE_INT val[2];
8979 const char *p, *end;
8980 int byte, wchar_bytes, charwidth, bitpos;
8982 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8984 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8985 charwidth = TYPE_PRECISION (char_type_node);
8986 gcc_assert ((size_t) wchar_bytes * charwidth
8987 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
8988 type = TREE_TYPE (constructor_type);
8989 p = TREE_STRING_POINTER (str);
8990 end = p + TREE_STRING_LENGTH (str);
8992 for (purpose = bitsize_zero_node;
8993 p < end
8994 && !(constructor_max_index
8995 && tree_int_cst_lt (constructor_max_index, purpose));
8996 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8998 if (wchar_bytes == 1)
9000 val[0] = (unsigned char) *p++;
9001 val[1] = 0;
9003 else
9005 val[1] = 0;
9006 val[0] = 0;
9007 for (byte = 0; byte < wchar_bytes; byte++)
9009 if (BYTES_BIG_ENDIAN)
9010 bitpos = (wchar_bytes - byte - 1) * charwidth;
9011 else
9012 bitpos = byte * charwidth;
9013 val[bitpos / HOST_BITS_PER_WIDE_INT]
9014 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9015 << (bitpos % HOST_BITS_PER_WIDE_INT);
9019 if (!TYPE_UNSIGNED (type))
9021 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9022 if (bitpos < HOST_BITS_PER_WIDE_INT)
9024 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9026 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9027 val[1] = -1;
9030 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9032 if (val[0] < 0)
9033 val[1] = -1;
9035 else if (val[1] & (HOST_WIDE_INT_1
9036 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9037 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9040 value = wide_int_to_tree (type,
9041 wide_int::from_array (val, 2,
9042 HOST_BITS_PER_WIDE_INT * 2));
9043 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9044 braced_init_obstack);
9047 constructor_incremental = 0;
9050 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9051 not initialized yet. */
9053 static tree
9054 find_init_member (tree field, struct obstack * braced_init_obstack)
9056 struct init_node *p;
9058 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9060 if (constructor_incremental
9061 && tree_int_cst_lt (field, constructor_unfilled_index))
9062 set_nonincremental_init (braced_init_obstack);
9064 p = constructor_pending_elts;
9065 while (p)
9067 if (tree_int_cst_lt (field, p->purpose))
9068 p = p->left;
9069 else if (tree_int_cst_lt (p->purpose, field))
9070 p = p->right;
9071 else
9072 return p->value;
9075 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9077 tree bitpos = bit_position (field);
9079 if (constructor_incremental
9080 && (!constructor_unfilled_fields
9081 || tree_int_cst_lt (bitpos,
9082 bit_position (constructor_unfilled_fields))))
9083 set_nonincremental_init (braced_init_obstack);
9085 p = constructor_pending_elts;
9086 while (p)
9088 if (field == p->purpose)
9089 return p->value;
9090 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9091 p = p->left;
9092 else
9093 p = p->right;
9096 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9098 if (!vec_safe_is_empty (constructor_elements)
9099 && (constructor_elements->last ().index == field))
9100 return constructor_elements->last ().value;
9102 return NULL_TREE;
9105 /* "Output" the next constructor element.
9106 At top level, really output it to assembler code now.
9107 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9108 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9109 TYPE is the data type that the containing data type wants here.
9110 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9111 If VALUE is a string constant, STRICT_STRING is true if it is
9112 unparenthesized or we should not warn here for it being parenthesized.
9113 For other types of VALUE, STRICT_STRING is not used.
9115 PENDING if true means output pending elements that belong
9116 right after this element. (PENDING is normally true;
9117 it is false while outputting pending elements, to avoid recursion.)
9119 IMPLICIT is true if value comes from pop_init_level (1),
9120 the new initializer has been merged with the existing one
9121 and thus no warnings should be emitted about overriding an
9122 existing initializer. */
9124 static void
9125 output_init_element (location_t loc, tree value, tree origtype,
9126 bool strict_string, tree type, tree field, bool pending,
9127 bool implicit, struct obstack * braced_init_obstack)
9129 tree semantic_type = NULL_TREE;
9130 bool maybe_const = true;
9131 bool npc;
9133 if (type == error_mark_node || value == error_mark_node)
9135 constructor_erroneous = 1;
9136 return;
9138 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9139 && (TREE_CODE (value) == STRING_CST
9140 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9141 && !(TREE_CODE (value) == STRING_CST
9142 && TREE_CODE (type) == ARRAY_TYPE
9143 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9144 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9145 TYPE_MAIN_VARIANT (type)))
9146 value = array_to_pointer_conversion (input_location, value);
9148 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9149 && require_constant_value && pending)
9151 /* As an extension, allow initializing objects with static storage
9152 duration with compound literals (which are then treated just as
9153 the brace enclosed list they contain). */
9154 if (flag_isoc99)
9155 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9156 "constant");
9157 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9158 value = DECL_INITIAL (decl);
9161 npc = null_pointer_constant_p (value);
9162 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9164 semantic_type = TREE_TYPE (value);
9165 value = TREE_OPERAND (value, 0);
9167 value = c_fully_fold (value, require_constant_value, &maybe_const);
9169 if (value == error_mark_node)
9170 constructor_erroneous = 1;
9171 else if (!TREE_CONSTANT (value))
9172 constructor_constant = 0;
9173 else if (!initializer_constant_valid_p (value,
9174 TREE_TYPE (value),
9175 AGGREGATE_TYPE_P (constructor_type)
9176 && TYPE_REVERSE_STORAGE_ORDER
9177 (constructor_type))
9178 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9179 && DECL_C_BIT_FIELD (field)
9180 && TREE_CODE (value) != INTEGER_CST))
9181 constructor_simple = 0;
9182 if (!maybe_const)
9183 constructor_nonconst = 1;
9185 /* Digest the initializer and issue any errors about incompatible
9186 types before issuing errors about non-constant initializers. */
9187 tree new_value = value;
9188 if (semantic_type)
9189 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9190 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9191 require_constant_value);
9192 if (new_value == error_mark_node)
9194 constructor_erroneous = 1;
9195 return;
9197 if (require_constant_value || require_constant_elements)
9198 constant_expression_warning (new_value);
9200 /* Proceed to check the constness of the original initializer. */
9201 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9203 if (require_constant_value)
9205 error_init (loc, "initializer element is not constant");
9206 value = error_mark_node;
9208 else if (require_constant_elements)
9209 pedwarn (loc, OPT_Wpedantic,
9210 "initializer element is not computable at load time");
9212 else if (!maybe_const
9213 && (require_constant_value || require_constant_elements))
9214 pedwarn_init (loc, OPT_Wpedantic,
9215 "initializer element is not a constant expression");
9217 /* Issue -Wc++-compat warnings about initializing a bitfield with
9218 enum type. */
9219 if (warn_cxx_compat
9220 && field != NULL_TREE
9221 && TREE_CODE (field) == FIELD_DECL
9222 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9223 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9224 != TYPE_MAIN_VARIANT (type))
9225 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9227 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9228 if (checktype != error_mark_node
9229 && (TYPE_MAIN_VARIANT (checktype)
9230 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9231 warning_init (loc, OPT_Wc___compat,
9232 "enum conversion in initialization is invalid in C++");
9235 /* If this field is empty and does not have side effects (and is not at
9236 the end of structure), don't do anything other than checking the
9237 initializer. */
9238 if (field
9239 && (TREE_TYPE (field) == error_mark_node
9240 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9241 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9242 && !TREE_SIDE_EFFECTS (new_value)
9243 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9244 || DECL_CHAIN (field)))))
9245 return;
9247 /* Finally, set VALUE to the initializer value digested above. */
9248 value = new_value;
9250 /* If this element doesn't come next in sequence,
9251 put it on constructor_pending_elts. */
9252 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9253 && (!constructor_incremental
9254 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9256 if (constructor_incremental
9257 && tree_int_cst_lt (field, constructor_unfilled_index))
9258 set_nonincremental_init (braced_init_obstack);
9260 add_pending_init (loc, field, value, origtype, implicit,
9261 braced_init_obstack);
9262 return;
9264 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9265 && (!constructor_incremental
9266 || field != constructor_unfilled_fields))
9268 /* We do this for records but not for unions. In a union,
9269 no matter which field is specified, it can be initialized
9270 right away since it starts at the beginning of the union. */
9271 if (constructor_incremental)
9273 if (!constructor_unfilled_fields)
9274 set_nonincremental_init (braced_init_obstack);
9275 else
9277 tree bitpos, unfillpos;
9279 bitpos = bit_position (field);
9280 unfillpos = bit_position (constructor_unfilled_fields);
9282 if (tree_int_cst_lt (bitpos, unfillpos))
9283 set_nonincremental_init (braced_init_obstack);
9287 add_pending_init (loc, field, value, origtype, implicit,
9288 braced_init_obstack);
9289 return;
9291 else if (TREE_CODE (constructor_type) == UNION_TYPE
9292 && !vec_safe_is_empty (constructor_elements))
9294 if (!implicit)
9296 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9297 warning_init (loc, OPT_Woverride_init_side_effects,
9298 "initialized field with side-effects overwritten");
9299 else if (warn_override_init)
9300 warning_init (loc, OPT_Woverride_init,
9301 "initialized field overwritten");
9304 /* We can have just one union field set. */
9305 constructor_elements = NULL;
9308 /* Otherwise, output this element either to
9309 constructor_elements or to the assembler file. */
9311 constructor_elt celt = {field, value};
9312 vec_safe_push (constructor_elements, celt);
9314 /* Advance the variable that indicates sequential elements output. */
9315 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9316 constructor_unfilled_index
9317 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9318 bitsize_one_node);
9319 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9321 constructor_unfilled_fields
9322 = DECL_CHAIN (constructor_unfilled_fields);
9324 /* Skip any nameless bit fields. */
9325 while (constructor_unfilled_fields != NULL_TREE
9326 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9327 constructor_unfilled_fields =
9328 DECL_CHAIN (constructor_unfilled_fields);
9330 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9331 constructor_unfilled_fields = NULL_TREE;
9333 /* Now output any pending elements which have become next. */
9334 if (pending)
9335 output_pending_init_elements (0, braced_init_obstack);
9338 /* For two FIELD_DECLs in the same chain, return -1 if field1
9339 comes before field2, 1 if field1 comes after field2 and
9340 0 if field1 == field2. */
9342 static int
9343 init_field_decl_cmp (tree field1, tree field2)
9345 if (field1 == field2)
9346 return 0;
9348 tree bitpos1 = bit_position (field1);
9349 tree bitpos2 = bit_position (field2);
9350 if (tree_int_cst_equal (bitpos1, bitpos2))
9352 /* If one of the fields has non-zero bitsize, then that
9353 field must be the last one in a sequence of zero
9354 sized fields, fields after it will have bigger
9355 bit_position. */
9356 if (TREE_TYPE (field1) != error_mark_node
9357 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9358 && integer_nonzerop (TREE_TYPE (field1)))
9359 return 1;
9360 if (TREE_TYPE (field2) != error_mark_node
9361 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9362 && integer_nonzerop (TREE_TYPE (field2)))
9363 return -1;
9364 /* Otherwise, fallback to DECL_CHAIN walk to find out
9365 which field comes earlier. Walk chains of both
9366 fields, so that if field1 and field2 are close to each
9367 other in either order, it is found soon even for large
9368 sequences of zero sized fields. */
9369 tree f1 = field1, f2 = field2;
9370 while (1)
9372 f1 = DECL_CHAIN (f1);
9373 f2 = DECL_CHAIN (f2);
9374 if (f1 == NULL_TREE)
9376 gcc_assert (f2);
9377 return 1;
9379 if (f2 == NULL_TREE)
9380 return -1;
9381 if (f1 == field2)
9382 return -1;
9383 if (f2 == field1)
9384 return 1;
9385 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9386 return 1;
9387 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9388 return -1;
9391 else if (tree_int_cst_lt (bitpos1, bitpos2))
9392 return -1;
9393 else
9394 return 1;
9397 /* Output any pending elements which have become next.
9398 As we output elements, constructor_unfilled_{fields,index}
9399 advances, which may cause other elements to become next;
9400 if so, they too are output.
9402 If ALL is 0, we return when there are
9403 no more pending elements to output now.
9405 If ALL is 1, we output space as necessary so that
9406 we can output all the pending elements. */
9407 static void
9408 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9410 struct init_node *elt = constructor_pending_elts;
9411 tree next;
9413 retry:
9415 /* Look through the whole pending tree.
9416 If we find an element that should be output now,
9417 output it. Otherwise, set NEXT to the element
9418 that comes first among those still pending. */
9420 next = NULL_TREE;
9421 while (elt)
9423 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9425 if (tree_int_cst_equal (elt->purpose,
9426 constructor_unfilled_index))
9427 output_init_element (input_location, elt->value, elt->origtype,
9428 true, TREE_TYPE (constructor_type),
9429 constructor_unfilled_index, false, false,
9430 braced_init_obstack);
9431 else if (tree_int_cst_lt (constructor_unfilled_index,
9432 elt->purpose))
9434 /* Advance to the next smaller node. */
9435 if (elt->left)
9436 elt = elt->left;
9437 else
9439 /* We have reached the smallest node bigger than the
9440 current unfilled index. Fill the space first. */
9441 next = elt->purpose;
9442 break;
9445 else
9447 /* Advance to the next bigger node. */
9448 if (elt->right)
9449 elt = elt->right;
9450 else
9452 /* We have reached the biggest node in a subtree. Find
9453 the parent of it, which is the next bigger node. */
9454 while (elt->parent && elt->parent->right == elt)
9455 elt = elt->parent;
9456 elt = elt->parent;
9457 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9458 elt->purpose))
9460 next = elt->purpose;
9461 break;
9466 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9468 /* If the current record is complete we are done. */
9469 if (constructor_unfilled_fields == NULL_TREE)
9470 break;
9472 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
9473 elt->purpose);
9474 if (cmp == 0)
9475 output_init_element (input_location, elt->value, elt->origtype,
9476 true, TREE_TYPE (elt->purpose),
9477 elt->purpose, false, false,
9478 braced_init_obstack);
9479 else if (cmp < 0)
9481 /* Advance to the next smaller node. */
9482 if (elt->left)
9483 elt = elt->left;
9484 else
9486 /* We have reached the smallest node bigger than the
9487 current unfilled field. Fill the space first. */
9488 next = elt->purpose;
9489 break;
9492 else
9494 /* Advance to the next bigger node. */
9495 if (elt->right)
9496 elt = elt->right;
9497 else
9499 /* We have reached the biggest node in a subtree. Find
9500 the parent of it, which is the next bigger node. */
9501 while (elt->parent && elt->parent->right == elt)
9502 elt = elt->parent;
9503 elt = elt->parent;
9504 if (elt
9505 && init_field_decl_cmp (constructor_unfilled_fields,
9506 elt->purpose) < 0)
9508 next = elt->purpose;
9509 break;
9516 /* Ordinarily return, but not if we want to output all
9517 and there are elements left. */
9518 if (!(all && next != NULL_TREE))
9519 return;
9521 /* If it's not incremental, just skip over the gap, so that after
9522 jumping to retry we will output the next successive element. */
9523 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9524 constructor_unfilled_fields = next;
9525 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9526 constructor_unfilled_index = next;
9528 /* ELT now points to the node in the pending tree with the next
9529 initializer to output. */
9530 goto retry;
9533 /* Add one non-braced element to the current constructor level.
9534 This adjusts the current position within the constructor's type.
9535 This may also start or terminate implicit levels
9536 to handle a partly-braced initializer.
9538 Once this has found the correct level for the new element,
9539 it calls output_init_element.
9541 IMPLICIT is true if value comes from pop_init_level (1),
9542 the new initializer has been merged with the existing one
9543 and thus no warnings should be emitted about overriding an
9544 existing initializer. */
9546 void
9547 process_init_element (location_t loc, struct c_expr value, bool implicit,
9548 struct obstack * braced_init_obstack)
9550 tree orig_value = value.value;
9551 int string_flag
9552 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9553 bool strict_string = value.original_code == STRING_CST;
9554 bool was_designated = designator_depth != 0;
9556 designator_depth = 0;
9557 designator_erroneous = 0;
9559 if (!implicit && value.value && !integer_zerop (value.value))
9560 constructor_zeroinit = 0;
9562 /* Handle superfluous braces around string cst as in
9563 char x[] = {"foo"}; */
9564 if (string_flag
9565 && constructor_type
9566 && !was_designated
9567 && TREE_CODE (constructor_type) == ARRAY_TYPE
9568 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9569 && integer_zerop (constructor_unfilled_index))
9571 if (constructor_stack->replacement_value.value)
9572 error_init (loc, "excess elements in char array initializer");
9573 constructor_stack->replacement_value = value;
9574 return;
9577 if (constructor_stack->replacement_value.value != NULL_TREE)
9579 error_init (loc, "excess elements in struct initializer");
9580 return;
9583 /* Ignore elements of a brace group if it is entirely superfluous
9584 and has already been diagnosed. */
9585 if (constructor_type == NULL_TREE)
9586 return;
9588 if (!implicit && warn_designated_init && !was_designated
9589 && TREE_CODE (constructor_type) == RECORD_TYPE
9590 && lookup_attribute ("designated_init",
9591 TYPE_ATTRIBUTES (constructor_type)))
9592 warning_init (loc,
9593 OPT_Wdesignated_init,
9594 "positional initialization of field "
9595 "in %<struct%> declared with %<designated_init%> attribute");
9597 /* If we've exhausted any levels that didn't have braces,
9598 pop them now. */
9599 while (constructor_stack->implicit)
9601 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9602 && constructor_fields == NULL_TREE)
9603 process_init_element (loc,
9604 pop_init_level (loc, 1, braced_init_obstack,
9605 last_init_list_comma),
9606 true, braced_init_obstack);
9607 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9608 || VECTOR_TYPE_P (constructor_type))
9609 && constructor_max_index
9610 && tree_int_cst_lt (constructor_max_index,
9611 constructor_index))
9612 process_init_element (loc,
9613 pop_init_level (loc, 1, braced_init_obstack,
9614 last_init_list_comma),
9615 true, braced_init_obstack);
9616 else
9617 break;
9620 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9621 if (constructor_range_stack)
9623 /* If value is a compound literal and we'll be just using its
9624 content, don't put it into a SAVE_EXPR. */
9625 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9626 || !require_constant_value)
9628 tree semantic_type = NULL_TREE;
9629 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9631 semantic_type = TREE_TYPE (value.value);
9632 value.value = TREE_OPERAND (value.value, 0);
9634 value.value = save_expr (value.value);
9635 if (semantic_type)
9636 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9637 value.value);
9641 while (1)
9643 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9645 tree fieldtype;
9646 enum tree_code fieldcode;
9648 if (constructor_fields == NULL_TREE)
9650 pedwarn_init (loc, 0, "excess elements in struct initializer");
9651 break;
9654 fieldtype = TREE_TYPE (constructor_fields);
9655 if (fieldtype != error_mark_node)
9656 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9657 fieldcode = TREE_CODE (fieldtype);
9659 /* Error for non-static initialization of a flexible array member. */
9660 if (fieldcode == ARRAY_TYPE
9661 && !require_constant_value
9662 && TYPE_SIZE (fieldtype) == NULL_TREE
9663 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9665 error_init (loc, "non-static initialization of a flexible "
9666 "array member");
9667 break;
9670 /* Error for initialization of a flexible array member with
9671 a string constant if the structure is in an array. E.g.:
9672 struct S { int x; char y[]; };
9673 struct S s[] = { { 1, "foo" } };
9674 is invalid. */
9675 if (string_flag
9676 && fieldcode == ARRAY_TYPE
9677 && constructor_depth > 1
9678 && TYPE_SIZE (fieldtype) == NULL_TREE
9679 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9681 bool in_array_p = false;
9682 for (struct constructor_stack *p = constructor_stack;
9683 p && p->type; p = p->next)
9684 if (TREE_CODE (p->type) == ARRAY_TYPE)
9686 in_array_p = true;
9687 break;
9689 if (in_array_p)
9691 error_init (loc, "initialization of flexible array "
9692 "member in a nested context");
9693 break;
9697 /* Accept a string constant to initialize a subarray. */
9698 if (value.value != NULL_TREE
9699 && fieldcode == ARRAY_TYPE
9700 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9701 && string_flag)
9702 value.value = orig_value;
9703 /* Otherwise, if we have come to a subaggregate,
9704 and we don't have an element of its type, push into it. */
9705 else if (value.value != NULL_TREE
9706 && value.value != error_mark_node
9707 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9708 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9709 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9711 push_init_level (loc, 1, braced_init_obstack);
9712 continue;
9715 if (value.value)
9717 push_member_name (constructor_fields);
9718 output_init_element (loc, value.value, value.original_type,
9719 strict_string, fieldtype,
9720 constructor_fields, true, implicit,
9721 braced_init_obstack);
9722 RESTORE_SPELLING_DEPTH (constructor_depth);
9724 else
9725 /* Do the bookkeeping for an element that was
9726 directly output as a constructor. */
9728 /* For a record, keep track of end position of last field. */
9729 if (DECL_SIZE (constructor_fields))
9730 constructor_bit_index
9731 = size_binop_loc (input_location, PLUS_EXPR,
9732 bit_position (constructor_fields),
9733 DECL_SIZE (constructor_fields));
9735 /* If the current field was the first one not yet written out,
9736 it isn't now, so update. */
9737 if (constructor_unfilled_fields == constructor_fields)
9739 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9740 /* Skip any nameless bit fields. */
9741 while (constructor_unfilled_fields != 0
9742 && (DECL_UNNAMED_BIT_FIELD
9743 (constructor_unfilled_fields)))
9744 constructor_unfilled_fields =
9745 DECL_CHAIN (constructor_unfilled_fields);
9749 constructor_fields = DECL_CHAIN (constructor_fields);
9750 /* Skip any nameless bit fields at the beginning. */
9751 while (constructor_fields != NULL_TREE
9752 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
9753 constructor_fields = DECL_CHAIN (constructor_fields);
9755 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9757 tree fieldtype;
9758 enum tree_code fieldcode;
9760 if (constructor_fields == NULL_TREE)
9762 pedwarn_init (loc, 0,
9763 "excess elements in union initializer");
9764 break;
9767 fieldtype = TREE_TYPE (constructor_fields);
9768 if (fieldtype != error_mark_node)
9769 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9770 fieldcode = TREE_CODE (fieldtype);
9772 /* Warn that traditional C rejects initialization of unions.
9773 We skip the warning if the value is zero. This is done
9774 under the assumption that the zero initializer in user
9775 code appears conditioned on e.g. __STDC__ to avoid
9776 "missing initializer" warnings and relies on default
9777 initialization to zero in the traditional C case.
9778 We also skip the warning if the initializer is designated,
9779 again on the assumption that this must be conditional on
9780 __STDC__ anyway (and we've already complained about the
9781 member-designator already). */
9782 if (!in_system_header_at (input_location) && !constructor_designated
9783 && !(value.value && (integer_zerop (value.value)
9784 || real_zerop (value.value))))
9785 warning (OPT_Wtraditional, "traditional C rejects initialization "
9786 "of unions");
9788 /* Accept a string constant to initialize a subarray. */
9789 if (value.value != NULL_TREE
9790 && fieldcode == ARRAY_TYPE
9791 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9792 && string_flag)
9793 value.value = orig_value;
9794 /* Otherwise, if we have come to a subaggregate,
9795 and we don't have an element of its type, push into it. */
9796 else if (value.value != NULL_TREE
9797 && value.value != error_mark_node
9798 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9799 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9800 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9802 push_init_level (loc, 1, braced_init_obstack);
9803 continue;
9806 if (value.value)
9808 push_member_name (constructor_fields);
9809 output_init_element (loc, value.value, value.original_type,
9810 strict_string, fieldtype,
9811 constructor_fields, true, implicit,
9812 braced_init_obstack);
9813 RESTORE_SPELLING_DEPTH (constructor_depth);
9815 else
9816 /* Do the bookkeeping for an element that was
9817 directly output as a constructor. */
9819 constructor_bit_index = DECL_SIZE (constructor_fields);
9820 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9823 constructor_fields = NULL_TREE;
9825 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9827 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9828 enum tree_code eltcode = TREE_CODE (elttype);
9830 /* Accept a string constant to initialize a subarray. */
9831 if (value.value != NULL_TREE
9832 && eltcode == ARRAY_TYPE
9833 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9834 && string_flag)
9835 value.value = orig_value;
9836 /* Otherwise, if we have come to a subaggregate,
9837 and we don't have an element of its type, push into it. */
9838 else if (value.value != NULL_TREE
9839 && value.value != error_mark_node
9840 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9841 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9842 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9844 push_init_level (loc, 1, braced_init_obstack);
9845 continue;
9848 if (constructor_max_index != NULL_TREE
9849 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9850 || integer_all_onesp (constructor_max_index)))
9852 pedwarn_init (loc, 0,
9853 "excess elements in array initializer");
9854 break;
9857 /* Now output the actual element. */
9858 if (value.value)
9860 push_array_bounds (tree_to_uhwi (constructor_index));
9861 output_init_element (loc, value.value, value.original_type,
9862 strict_string, elttype,
9863 constructor_index, true, implicit,
9864 braced_init_obstack);
9865 RESTORE_SPELLING_DEPTH (constructor_depth);
9868 constructor_index
9869 = size_binop_loc (input_location, PLUS_EXPR,
9870 constructor_index, bitsize_one_node);
9872 if (!value.value)
9873 /* If we are doing the bookkeeping for an element that was
9874 directly output as a constructor, we must update
9875 constructor_unfilled_index. */
9876 constructor_unfilled_index = constructor_index;
9878 else if (VECTOR_TYPE_P (constructor_type))
9880 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9882 /* Do a basic check of initializer size. Note that vectors
9883 always have a fixed size derived from their type. */
9884 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9886 pedwarn_init (loc, 0,
9887 "excess elements in vector initializer");
9888 break;
9891 /* Now output the actual element. */
9892 if (value.value)
9894 if (TREE_CODE (value.value) == VECTOR_CST)
9895 elttype = TYPE_MAIN_VARIANT (constructor_type);
9896 output_init_element (loc, value.value, value.original_type,
9897 strict_string, elttype,
9898 constructor_index, true, implicit,
9899 braced_init_obstack);
9902 constructor_index
9903 = size_binop_loc (input_location,
9904 PLUS_EXPR, constructor_index, bitsize_one_node);
9906 if (!value.value)
9907 /* If we are doing the bookkeeping for an element that was
9908 directly output as a constructor, we must update
9909 constructor_unfilled_index. */
9910 constructor_unfilled_index = constructor_index;
9913 /* Handle the sole element allowed in a braced initializer
9914 for a scalar variable. */
9915 else if (constructor_type != error_mark_node
9916 && constructor_fields == NULL_TREE)
9918 pedwarn_init (loc, 0,
9919 "excess elements in scalar initializer");
9920 break;
9922 else
9924 if (value.value)
9925 output_init_element (loc, value.value, value.original_type,
9926 strict_string, constructor_type,
9927 NULL_TREE, true, implicit,
9928 braced_init_obstack);
9929 constructor_fields = NULL_TREE;
9932 /* Handle range initializers either at this level or anywhere higher
9933 in the designator stack. */
9934 if (constructor_range_stack)
9936 struct constructor_range_stack *p, *range_stack;
9937 int finish = 0;
9939 range_stack = constructor_range_stack;
9940 constructor_range_stack = 0;
9941 while (constructor_stack != range_stack->stack)
9943 gcc_assert (constructor_stack->implicit);
9944 process_init_element (loc,
9945 pop_init_level (loc, 1,
9946 braced_init_obstack,
9947 last_init_list_comma),
9948 true, braced_init_obstack);
9950 for (p = range_stack;
9951 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9952 p = p->prev)
9954 gcc_assert (constructor_stack->implicit);
9955 process_init_element (loc,
9956 pop_init_level (loc, 1,
9957 braced_init_obstack,
9958 last_init_list_comma),
9959 true, braced_init_obstack);
9962 p->index = size_binop_loc (input_location,
9963 PLUS_EXPR, p->index, bitsize_one_node);
9964 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9965 finish = 1;
9967 while (1)
9969 constructor_index = p->index;
9970 constructor_fields = p->fields;
9971 if (finish && p->range_end && p->index == p->range_start)
9973 finish = 0;
9974 p->prev = 0;
9976 p = p->next;
9977 if (!p)
9978 break;
9979 finish_implicit_inits (loc, braced_init_obstack);
9980 push_init_level (loc, 2, braced_init_obstack);
9981 p->stack = constructor_stack;
9982 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9983 p->index = p->range_start;
9986 if (!finish)
9987 constructor_range_stack = range_stack;
9988 continue;
9991 break;
9994 constructor_range_stack = 0;
9997 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9998 (guaranteed to be 'volatile' or null) and ARGS (represented using
9999 an ASM_EXPR node). */
10000 tree
10001 build_asm_stmt (tree cv_qualifier, tree args)
10003 if (!ASM_VOLATILE_P (args) && cv_qualifier)
10004 ASM_VOLATILE_P (args) = 1;
10005 return add_stmt (args);
10008 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10009 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10010 SIMPLE indicates whether there was anything at all after the
10011 string in the asm expression -- asm("blah") and asm("blah" : )
10012 are subtly different. We use a ASM_EXPR node to represent this. */
10013 tree
10014 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10015 tree clobbers, tree labels, bool simple)
10017 tree tail;
10018 tree args;
10019 int i;
10020 const char *constraint;
10021 const char **oconstraints;
10022 bool allows_mem, allows_reg, is_inout;
10023 int ninputs, noutputs;
10025 ninputs = list_length (inputs);
10026 noutputs = list_length (outputs);
10027 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10029 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10031 /* Remove output conversions that change the type but not the mode. */
10032 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10034 tree output = TREE_VALUE (tail);
10036 output = c_fully_fold (output, false, NULL, true);
10038 /* ??? Really, this should not be here. Users should be using a
10039 proper lvalue, dammit. But there's a long history of using casts
10040 in the output operands. In cases like longlong.h, this becomes a
10041 primitive form of typechecking -- if the cast can be removed, then
10042 the output operand had a type of the proper width; otherwise we'll
10043 get an error. Gross, but ... */
10044 STRIP_NOPS (output);
10046 if (!lvalue_or_else (loc, output, lv_asm))
10047 output = error_mark_node;
10049 if (output != error_mark_node
10050 && (TREE_READONLY (output)
10051 || TYPE_READONLY (TREE_TYPE (output))
10052 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10053 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10054 readonly_error (loc, output, lv_asm);
10056 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10057 oconstraints[i] = constraint;
10059 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10060 &allows_mem, &allows_reg, &is_inout))
10062 /* If the operand is going to end up in memory,
10063 mark it addressable. */
10064 if (!allows_reg && !c_mark_addressable (output))
10065 output = error_mark_node;
10066 if (!(!allows_reg && allows_mem)
10067 && output != error_mark_node
10068 && VOID_TYPE_P (TREE_TYPE (output)))
10070 error_at (loc, "invalid use of void expression");
10071 output = error_mark_node;
10074 else
10075 output = error_mark_node;
10077 TREE_VALUE (tail) = output;
10080 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10082 tree input;
10084 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10085 input = TREE_VALUE (tail);
10087 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10088 oconstraints, &allows_mem, &allows_reg))
10090 /* If the operand is going to end up in memory,
10091 mark it addressable. */
10092 if (!allows_reg && allows_mem)
10094 input = c_fully_fold (input, false, NULL, true);
10096 /* Strip the nops as we allow this case. FIXME, this really
10097 should be rejected or made deprecated. */
10098 STRIP_NOPS (input);
10099 if (!c_mark_addressable (input))
10100 input = error_mark_node;
10102 else
10104 struct c_expr expr;
10105 memset (&expr, 0, sizeof (expr));
10106 expr.value = input;
10107 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10108 input = c_fully_fold (expr.value, false, NULL);
10110 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10112 error_at (loc, "invalid use of void expression");
10113 input = error_mark_node;
10117 else
10118 input = error_mark_node;
10120 TREE_VALUE (tail) = input;
10123 /* ASMs with labels cannot have outputs. This should have been
10124 enforced by the parser. */
10125 gcc_assert (outputs == NULL || labels == NULL);
10127 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10129 /* asm statements without outputs, including simple ones, are treated
10130 as volatile. */
10131 ASM_INPUT_P (args) = simple;
10132 ASM_VOLATILE_P (args) = (noutputs == 0);
10134 return args;
10137 /* Generate a goto statement to LABEL. LOC is the location of the
10138 GOTO. */
10140 tree
10141 c_finish_goto_label (location_t loc, tree label)
10143 tree decl = lookup_label_for_goto (loc, label);
10144 if (!decl)
10145 return NULL_TREE;
10146 TREE_USED (decl) = 1;
10148 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10149 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10150 SET_EXPR_LOCATION (t, loc);
10151 return add_stmt (t);
10155 /* Generate a computed goto statement to EXPR. LOC is the location of
10156 the GOTO. */
10158 tree
10159 c_finish_goto_ptr (location_t loc, tree expr)
10161 tree t;
10162 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10163 expr = c_fully_fold (expr, false, NULL);
10164 expr = convert (ptr_type_node, expr);
10165 t = build1 (GOTO_EXPR, void_type_node, expr);
10166 SET_EXPR_LOCATION (t, loc);
10167 return add_stmt (t);
10170 /* Generate a C `return' statement. RETVAL is the expression for what
10171 to return, or a null pointer for `return;' with no value. LOC is
10172 the location of the return statement, or the location of the expression,
10173 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10174 is the original type of RETVAL. */
10176 tree
10177 c_finish_return (location_t loc, tree retval, tree origtype)
10179 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10180 bool no_warning = false;
10181 bool npc = false;
10183 /* Use the expansion point to handle cases such as returning NULL
10184 in a function returning void. */
10185 source_location xloc = expansion_point_location_if_in_system_header (loc);
10187 if (TREE_THIS_VOLATILE (current_function_decl))
10188 warning_at (xloc, 0,
10189 "function declared %<noreturn%> has a %<return%> statement");
10191 if (retval)
10193 tree semantic_type = NULL_TREE;
10194 npc = null_pointer_constant_p (retval);
10195 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10197 semantic_type = TREE_TYPE (retval);
10198 retval = TREE_OPERAND (retval, 0);
10200 retval = c_fully_fold (retval, false, NULL);
10201 if (semantic_type)
10202 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10205 if (!retval)
10207 current_function_returns_null = 1;
10208 if ((warn_return_type >= 0 || flag_isoc99)
10209 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10211 bool warned_here;
10212 if (flag_isoc99)
10213 warned_here = pedwarn
10214 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10215 "%<return%> with no value, in function returning non-void");
10216 else
10217 warned_here = warning_at
10218 (loc, OPT_Wreturn_type,
10219 "%<return%> with no value, in function returning non-void");
10220 no_warning = true;
10221 if (warned_here)
10222 inform (DECL_SOURCE_LOCATION (current_function_decl),
10223 "declared here");
10226 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10228 current_function_returns_null = 1;
10229 bool warned_here;
10230 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10231 warned_here = pedwarn
10232 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10233 "%<return%> with a value, in function returning void");
10234 else
10235 warned_here = pedwarn
10236 (xloc, OPT_Wpedantic, "ISO C forbids "
10237 "%<return%> with expression, in function returning void");
10238 if (warned_here)
10239 inform (DECL_SOURCE_LOCATION (current_function_decl),
10240 "declared here");
10242 else
10244 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10245 retval, origtype, ic_return,
10246 npc, NULL_TREE, NULL_TREE, 0);
10247 tree res = DECL_RESULT (current_function_decl);
10248 tree inner;
10249 bool save;
10251 current_function_returns_value = 1;
10252 if (t == error_mark_node)
10253 return NULL_TREE;
10255 save = in_late_binary_op;
10256 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10257 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10258 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10259 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10260 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10261 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10262 in_late_binary_op = true;
10263 inner = t = convert (TREE_TYPE (res), t);
10264 in_late_binary_op = save;
10266 /* Strip any conversions, additions, and subtractions, and see if
10267 we are returning the address of a local variable. Warn if so. */
10268 while (1)
10270 switch (TREE_CODE (inner))
10272 CASE_CONVERT:
10273 case NON_LVALUE_EXPR:
10274 case PLUS_EXPR:
10275 case POINTER_PLUS_EXPR:
10276 inner = TREE_OPERAND (inner, 0);
10277 continue;
10279 case MINUS_EXPR:
10280 /* If the second operand of the MINUS_EXPR has a pointer
10281 type (or is converted from it), this may be valid, so
10282 don't give a warning. */
10284 tree op1 = TREE_OPERAND (inner, 1);
10286 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10287 && (CONVERT_EXPR_P (op1)
10288 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10289 op1 = TREE_OPERAND (op1, 0);
10291 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10292 break;
10294 inner = TREE_OPERAND (inner, 0);
10295 continue;
10298 case ADDR_EXPR:
10299 inner = TREE_OPERAND (inner, 0);
10301 while (REFERENCE_CLASS_P (inner)
10302 && !INDIRECT_REF_P (inner))
10303 inner = TREE_OPERAND (inner, 0);
10305 if (DECL_P (inner)
10306 && !DECL_EXTERNAL (inner)
10307 && !TREE_STATIC (inner)
10308 && DECL_CONTEXT (inner) == current_function_decl)
10310 if (TREE_CODE (inner) == LABEL_DECL)
10311 warning_at (loc, OPT_Wreturn_local_addr,
10312 "function returns address of label");
10313 else
10315 warning_at (loc, OPT_Wreturn_local_addr,
10316 "function returns address of local variable");
10317 tree zero = build_zero_cst (TREE_TYPE (res));
10318 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10321 break;
10323 default:
10324 break;
10327 break;
10330 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10331 SET_EXPR_LOCATION (retval, loc);
10333 if (warn_sequence_point)
10334 verify_sequence_points (retval);
10337 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10338 TREE_NO_WARNING (ret_stmt) |= no_warning;
10339 return add_stmt (ret_stmt);
10342 struct c_switch {
10343 /* The SWITCH_EXPR being built. */
10344 tree switch_expr;
10346 /* The original type of the testing expression, i.e. before the
10347 default conversion is applied. */
10348 tree orig_type;
10350 /* A splay-tree mapping the low element of a case range to the high
10351 element, or NULL_TREE if there is no high element. Used to
10352 determine whether or not a new case label duplicates an old case
10353 label. We need a tree, rather than simply a hash table, because
10354 of the GNU case range extension. */
10355 splay_tree cases;
10357 /* The bindings at the point of the switch. This is used for
10358 warnings crossing decls when branching to a case label. */
10359 struct c_spot_bindings *bindings;
10361 /* The next node on the stack. */
10362 struct c_switch *next;
10364 /* Remember whether the controlling expression had boolean type
10365 before integer promotions for the sake of -Wswitch-bool. */
10366 bool bool_cond_p;
10368 /* Remember whether there was a case value that is outside the
10369 range of the ORIG_TYPE. */
10370 bool outside_range_p;
10373 /* A stack of the currently active switch statements. The innermost
10374 switch statement is on the top of the stack. There is no need to
10375 mark the stack for garbage collection because it is only active
10376 during the processing of the body of a function, and we never
10377 collect at that point. */
10379 struct c_switch *c_switch_stack;
10381 /* Start a C switch statement, testing expression EXP. Return the new
10382 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10383 SWITCH_COND_LOC is the location of the switch's condition.
10384 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10386 tree
10387 c_start_case (location_t switch_loc,
10388 location_t switch_cond_loc,
10389 tree exp, bool explicit_cast_p)
10391 tree orig_type = error_mark_node;
10392 bool bool_cond_p = false;
10393 struct c_switch *cs;
10395 if (exp != error_mark_node)
10397 orig_type = TREE_TYPE (exp);
10399 if (!INTEGRAL_TYPE_P (orig_type))
10401 if (orig_type != error_mark_node)
10403 error_at (switch_cond_loc, "switch quantity not an integer");
10404 orig_type = error_mark_node;
10406 exp = integer_zero_node;
10408 else
10410 tree type = TYPE_MAIN_VARIANT (orig_type);
10411 tree e = exp;
10413 /* Warn if the condition has boolean value. */
10414 while (TREE_CODE (e) == COMPOUND_EXPR)
10415 e = TREE_OPERAND (e, 1);
10417 if ((TREE_CODE (type) == BOOLEAN_TYPE
10418 || truth_value_p (TREE_CODE (e)))
10419 /* Explicit cast to int suppresses this warning. */
10420 && !(TREE_CODE (type) == INTEGER_TYPE
10421 && explicit_cast_p))
10422 bool_cond_p = true;
10424 if (!in_system_header_at (input_location)
10425 && (type == long_integer_type_node
10426 || type == long_unsigned_type_node))
10427 warning_at (switch_cond_loc,
10428 OPT_Wtraditional, "%<long%> switch expression not "
10429 "converted to %<int%> in ISO C");
10431 exp = c_fully_fold (exp, false, NULL);
10432 exp = default_conversion (exp);
10434 if (warn_sequence_point)
10435 verify_sequence_points (exp);
10439 /* Add this new SWITCH_EXPR to the stack. */
10440 cs = XNEW (struct c_switch);
10441 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
10442 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10443 cs->orig_type = orig_type;
10444 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10445 cs->bindings = c_get_switch_bindings ();
10446 cs->bool_cond_p = bool_cond_p;
10447 cs->outside_range_p = false;
10448 cs->next = c_switch_stack;
10449 c_switch_stack = cs;
10451 return add_stmt (cs->switch_expr);
10454 /* Process a case label at location LOC. */
10456 tree
10457 do_case (location_t loc, tree low_value, tree high_value)
10459 tree label = NULL_TREE;
10461 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10463 low_value = c_fully_fold (low_value, false, NULL);
10464 if (TREE_CODE (low_value) == INTEGER_CST)
10465 pedwarn (loc, OPT_Wpedantic,
10466 "case label is not an integer constant expression");
10469 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10471 high_value = c_fully_fold (high_value, false, NULL);
10472 if (TREE_CODE (high_value) == INTEGER_CST)
10473 pedwarn (input_location, OPT_Wpedantic,
10474 "case label is not an integer constant expression");
10477 if (c_switch_stack == NULL)
10479 if (low_value)
10480 error_at (loc, "case label not within a switch statement");
10481 else
10482 error_at (loc, "%<default%> label not within a switch statement");
10483 return NULL_TREE;
10486 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10487 EXPR_LOCATION (c_switch_stack->switch_expr),
10488 loc))
10489 return NULL_TREE;
10491 label = c_add_case_label (loc, c_switch_stack->cases,
10492 SWITCH_COND (c_switch_stack->switch_expr),
10493 c_switch_stack->orig_type,
10494 low_value, high_value,
10495 &c_switch_stack->outside_range_p);
10496 if (label == error_mark_node)
10497 label = NULL_TREE;
10498 return label;
10501 /* Finish the switch statement. TYPE is the original type of the
10502 controlling expression of the switch, or NULL_TREE. */
10504 void
10505 c_finish_case (tree body, tree type)
10507 struct c_switch *cs = c_switch_stack;
10508 location_t switch_location;
10510 SWITCH_BODY (cs->switch_expr) = body;
10512 /* Emit warnings as needed. */
10513 switch_location = EXPR_LOCATION (cs->switch_expr);
10514 c_do_switch_warnings (cs->cases, switch_location,
10515 type ? type : TREE_TYPE (cs->switch_expr),
10516 SWITCH_COND (cs->switch_expr),
10517 cs->bool_cond_p, cs->outside_range_p);
10518 if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10519 SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
10521 /* Pop the stack. */
10522 c_switch_stack = cs->next;
10523 splay_tree_delete (cs->cases);
10524 c_release_switch_bindings (cs->bindings);
10525 XDELETE (cs);
10528 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10529 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10530 may be null. */
10532 void
10533 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10534 tree else_block)
10536 tree stmt;
10538 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10539 SET_EXPR_LOCATION (stmt, if_locus);
10540 add_stmt (stmt);
10543 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10544 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10545 is false for DO loops. INCR is the FOR increment expression. BODY is
10546 the statement controlled by the loop. BLAB is the break label. CLAB is
10547 the continue label. Everything is allowed to be NULL. */
10549 void
10550 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10551 tree blab, tree clab, bool cond_is_first)
10553 tree entry = NULL, exit = NULL, t;
10555 /* If the condition is zero don't generate a loop construct. */
10556 if (cond && integer_zerop (cond))
10558 if (cond_is_first)
10560 t = build_and_jump (&blab);
10561 SET_EXPR_LOCATION (t, start_locus);
10562 add_stmt (t);
10565 else
10567 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10569 /* If we have an exit condition, then we build an IF with gotos either
10570 out of the loop, or to the top of it. If there's no exit condition,
10571 then we just build a jump back to the top. */
10572 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10574 if (cond && !integer_nonzerop (cond))
10576 /* Canonicalize the loop condition to the end. This means
10577 generating a branch to the loop condition. Reuse the
10578 continue label, if possible. */
10579 if (cond_is_first)
10581 if (incr || !clab)
10583 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10584 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10586 else
10587 t = build1 (GOTO_EXPR, void_type_node, clab);
10588 SET_EXPR_LOCATION (t, start_locus);
10589 add_stmt (t);
10592 t = build_and_jump (&blab);
10593 if (cond_is_first)
10594 exit = fold_build3_loc (start_locus,
10595 COND_EXPR, void_type_node, cond, exit, t);
10596 else
10597 exit = fold_build3_loc (input_location,
10598 COND_EXPR, void_type_node, cond, exit, t);
10600 else
10602 /* For the backward-goto's location of an unconditional loop
10603 use the beginning of the body, or, if there is none, the
10604 top of the loop. */
10605 location_t loc = EXPR_LOCATION (expr_first (body));
10606 if (loc == UNKNOWN_LOCATION)
10607 loc = start_locus;
10608 SET_EXPR_LOCATION (exit, loc);
10611 add_stmt (top);
10614 if (body)
10615 add_stmt (body);
10616 if (clab)
10617 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10618 if (incr)
10619 add_stmt (incr);
10620 if (entry)
10621 add_stmt (entry);
10622 if (exit)
10623 add_stmt (exit);
10624 if (blab)
10625 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10628 tree
10629 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10631 bool skip;
10632 tree label = *label_p;
10634 /* In switch statements break is sometimes stylistically used after
10635 a return statement. This can lead to spurious warnings about
10636 control reaching the end of a non-void function when it is
10637 inlined. Note that we are calling block_may_fallthru with
10638 language specific tree nodes; this works because
10639 block_may_fallthru returns true when given something it does not
10640 understand. */
10641 skip = !block_may_fallthru (cur_stmt_list);
10643 if (!label)
10645 if (!skip)
10646 *label_p = label = create_artificial_label (loc);
10648 else if (TREE_CODE (label) == LABEL_DECL)
10650 else switch (TREE_INT_CST_LOW (label))
10652 case 0:
10653 if (is_break)
10654 error_at (loc, "break statement not within loop or switch");
10655 else
10656 error_at (loc, "continue statement not within a loop");
10657 return NULL_TREE;
10659 case 1:
10660 gcc_assert (is_break);
10661 error_at (loc, "break statement used with OpenMP for loop");
10662 return NULL_TREE;
10664 case 2:
10665 if (is_break)
10666 error ("break statement within %<#pragma simd%> loop body");
10667 else
10668 error ("continue statement within %<#pragma simd%> loop body");
10669 return NULL_TREE;
10671 default:
10672 gcc_unreachable ();
10675 if (skip)
10676 return NULL_TREE;
10678 if (!is_break)
10679 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10681 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10684 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10686 static void
10687 emit_side_effect_warnings (location_t loc, tree expr)
10689 if (expr == error_mark_node)
10691 else if (!TREE_SIDE_EFFECTS (expr))
10693 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10694 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10696 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10698 tree r = expr;
10699 location_t cloc = loc;
10700 while (TREE_CODE (r) == COMPOUND_EXPR)
10702 if (EXPR_HAS_LOCATION (r))
10703 cloc = EXPR_LOCATION (r);
10704 r = TREE_OPERAND (r, 1);
10706 if (!TREE_SIDE_EFFECTS (r)
10707 && !VOID_TYPE_P (TREE_TYPE (r))
10708 && !CONVERT_EXPR_P (r)
10709 && !TREE_NO_WARNING (r)
10710 && !TREE_NO_WARNING (expr))
10711 warning_at (cloc, OPT_Wunused_value,
10712 "right-hand operand of comma expression has no effect");
10714 else
10715 warn_if_unused_value (expr, loc);
10718 /* Process an expression as if it were a complete statement. Emit
10719 diagnostics, but do not call ADD_STMT. LOC is the location of the
10720 statement. */
10722 tree
10723 c_process_expr_stmt (location_t loc, tree expr)
10725 tree exprv;
10727 if (!expr)
10728 return NULL_TREE;
10730 expr = c_fully_fold (expr, false, NULL);
10732 if (warn_sequence_point)
10733 verify_sequence_points (expr);
10735 if (TREE_TYPE (expr) != error_mark_node
10736 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10737 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10738 error_at (loc, "expression statement has incomplete type");
10740 /* If we're not processing a statement expression, warn about unused values.
10741 Warnings for statement expressions will be emitted later, once we figure
10742 out which is the result. */
10743 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10744 && warn_unused_value)
10745 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10747 exprv = expr;
10748 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10749 exprv = TREE_OPERAND (exprv, 1);
10750 while (CONVERT_EXPR_P (exprv))
10751 exprv = TREE_OPERAND (exprv, 0);
10752 if (DECL_P (exprv)
10753 || handled_component_p (exprv)
10754 || TREE_CODE (exprv) == ADDR_EXPR)
10755 mark_exp_read (exprv);
10757 /* If the expression is not of a type to which we cannot assign a line
10758 number, wrap the thing in a no-op NOP_EXPR. */
10759 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10761 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10762 SET_EXPR_LOCATION (expr, loc);
10765 return expr;
10768 /* Emit an expression as a statement. LOC is the location of the
10769 expression. */
10771 tree
10772 c_finish_expr_stmt (location_t loc, tree expr)
10774 if (expr)
10775 return add_stmt (c_process_expr_stmt (loc, expr));
10776 else
10777 return NULL;
10780 /* Do the opposite and emit a statement as an expression. To begin,
10781 create a new binding level and return it. */
10783 tree
10784 c_begin_stmt_expr (void)
10786 tree ret;
10788 /* We must force a BLOCK for this level so that, if it is not expanded
10789 later, there is a way to turn off the entire subtree of blocks that
10790 are contained in it. */
10791 keep_next_level ();
10792 ret = c_begin_compound_stmt (true);
10794 c_bindings_start_stmt_expr (c_switch_stack == NULL
10795 ? NULL
10796 : c_switch_stack->bindings);
10798 /* Mark the current statement list as belonging to a statement list. */
10799 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10801 return ret;
10804 /* LOC is the location of the compound statement to which this body
10805 belongs. */
10807 tree
10808 c_finish_stmt_expr (location_t loc, tree body)
10810 tree last, type, tmp, val;
10811 tree *last_p;
10813 body = c_end_compound_stmt (loc, body, true);
10815 c_bindings_end_stmt_expr (c_switch_stack == NULL
10816 ? NULL
10817 : c_switch_stack->bindings);
10819 /* Locate the last statement in BODY. See c_end_compound_stmt
10820 about always returning a BIND_EXPR. */
10821 last_p = &BIND_EXPR_BODY (body);
10822 last = BIND_EXPR_BODY (body);
10824 continue_searching:
10825 if (TREE_CODE (last) == STATEMENT_LIST)
10827 tree_stmt_iterator l = tsi_last (last);
10829 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
10830 tsi_prev (&l);
10832 /* This can happen with degenerate cases like ({ }). No value. */
10833 if (tsi_end_p (l))
10834 return body;
10836 /* If we're supposed to generate side effects warnings, process
10837 all of the statements except the last. */
10838 if (warn_unused_value)
10840 for (tree_stmt_iterator i = tsi_start (last);
10841 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
10843 location_t tloc;
10844 tree t = tsi_stmt (i);
10846 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10847 emit_side_effect_warnings (tloc, t);
10850 last_p = tsi_stmt_ptr (l);
10851 last = *last_p;
10854 /* If the end of the list is exception related, then the list was split
10855 by a call to push_cleanup. Continue searching. */
10856 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10857 || TREE_CODE (last) == TRY_CATCH_EXPR)
10859 last_p = &TREE_OPERAND (last, 0);
10860 last = *last_p;
10861 goto continue_searching;
10864 if (last == error_mark_node)
10865 return last;
10867 /* In the case that the BIND_EXPR is not necessary, return the
10868 expression out from inside it. */
10869 if ((last == BIND_EXPR_BODY (body)
10870 /* Skip nested debug stmts. */
10871 || last == expr_first (BIND_EXPR_BODY (body)))
10872 && BIND_EXPR_VARS (body) == NULL)
10874 /* Even if this looks constant, do not allow it in a constant
10875 expression. */
10876 last = c_wrap_maybe_const (last, true);
10877 /* Do not warn if the return value of a statement expression is
10878 unused. */
10879 TREE_NO_WARNING (last) = 1;
10880 return last;
10883 /* Extract the type of said expression. */
10884 type = TREE_TYPE (last);
10886 /* If we're not returning a value at all, then the BIND_EXPR that
10887 we already have is a fine expression to return. */
10888 if (!type || VOID_TYPE_P (type))
10889 return body;
10891 /* Now that we've located the expression containing the value, it seems
10892 silly to make voidify_wrapper_expr repeat the process. Create a
10893 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10894 tmp = create_tmp_var_raw (type);
10896 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10897 tree_expr_nonnegative_p giving up immediately. */
10898 val = last;
10899 if (TREE_CODE (val) == NOP_EXPR
10900 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10901 val = TREE_OPERAND (val, 0);
10903 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10904 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10907 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10908 SET_EXPR_LOCATION (t, loc);
10909 return t;
10913 /* Begin and end compound statements. This is as simple as pushing
10914 and popping new statement lists from the tree. */
10916 tree
10917 c_begin_compound_stmt (bool do_scope)
10919 tree stmt = push_stmt_list ();
10920 if (do_scope)
10921 push_scope ();
10922 return stmt;
10925 /* End a compound statement. STMT is the statement. LOC is the
10926 location of the compound statement-- this is usually the location
10927 of the opening brace. */
10929 tree
10930 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10932 tree block = NULL;
10934 if (do_scope)
10936 if (c_dialect_objc ())
10937 objc_clear_super_receiver ();
10938 block = pop_scope ();
10941 stmt = pop_stmt_list (stmt);
10942 stmt = c_build_bind_expr (loc, block, stmt);
10944 /* If this compound statement is nested immediately inside a statement
10945 expression, then force a BIND_EXPR to be created. Otherwise we'll
10946 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10947 STATEMENT_LISTs merge, and thus we can lose track of what statement
10948 was really last. */
10949 if (building_stmt_list_p ()
10950 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10951 && TREE_CODE (stmt) != BIND_EXPR)
10953 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10954 TREE_SIDE_EFFECTS (stmt) = 1;
10955 SET_EXPR_LOCATION (stmt, loc);
10958 return stmt;
10961 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10962 when the current scope is exited. EH_ONLY is true when this is not
10963 meant to apply to normal control flow transfer. */
10965 void
10966 push_cleanup (tree decl, tree cleanup, bool eh_only)
10968 enum tree_code code;
10969 tree stmt, list;
10970 bool stmt_expr;
10972 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10973 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10974 add_stmt (stmt);
10975 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10976 list = push_stmt_list ();
10977 TREE_OPERAND (stmt, 0) = list;
10978 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10981 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10982 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10984 static tree
10985 build_vec_cmp (tree_code code, tree type,
10986 tree arg0, tree arg1)
10988 tree zero_vec = build_zero_cst (type);
10989 tree minus_one_vec = build_minus_one_cst (type);
10990 tree cmp_type = build_same_sized_truth_vector_type (type);
10991 tree cmp = build2 (code, cmp_type, arg0, arg1);
10992 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10995 /* Build a binary-operation expression without default conversions.
10996 CODE is the kind of expression to build.
10997 LOCATION is the operator's location.
10998 This function differs from `build' in several ways:
10999 the data type of the result is computed and recorded in it,
11000 warnings are generated if arg data types are invalid,
11001 special handling for addition and subtraction of pointers is known,
11002 and some optimization is done (operations on narrow ints
11003 are done in the narrower type when that gives the same result).
11004 Constant folding is also done before the result is returned.
11006 Note that the operands will never have enumeral types, or function
11007 or array types, because either they will have the default conversions
11008 performed or they have both just been converted to some other type in which
11009 the arithmetic is to be done. */
11011 tree
11012 build_binary_op (location_t location, enum tree_code code,
11013 tree orig_op0, tree orig_op1, bool convert_p)
11015 tree type0, type1, orig_type0, orig_type1;
11016 tree eptype;
11017 enum tree_code code0, code1;
11018 tree op0, op1;
11019 tree ret = error_mark_node;
11020 const char *invalid_op_diag;
11021 bool op0_int_operands, op1_int_operands;
11022 bool int_const, int_const_or_overflow, int_operands;
11024 /* Expression code to give to the expression when it is built.
11025 Normally this is CODE, which is what the caller asked for,
11026 but in some special cases we change it. */
11027 enum tree_code resultcode = code;
11029 /* Data type in which the computation is to be performed.
11030 In the simplest cases this is the common type of the arguments. */
11031 tree result_type = NULL;
11033 /* When the computation is in excess precision, the type of the
11034 final EXCESS_PRECISION_EXPR. */
11035 tree semantic_result_type = NULL;
11037 /* Nonzero means operands have already been type-converted
11038 in whatever way is necessary.
11039 Zero means they need to be converted to RESULT_TYPE. */
11040 int converted = 0;
11042 /* Nonzero means create the expression with this type, rather than
11043 RESULT_TYPE. */
11044 tree build_type = NULL_TREE;
11046 /* Nonzero means after finally constructing the expression
11047 convert it to this type. */
11048 tree final_type = NULL_TREE;
11050 /* Nonzero if this is an operation like MIN or MAX which can
11051 safely be computed in short if both args are promoted shorts.
11052 Also implies COMMON.
11053 -1 indicates a bitwise operation; this makes a difference
11054 in the exact conditions for when it is safe to do the operation
11055 in a narrower mode. */
11056 int shorten = 0;
11058 /* Nonzero if this is a comparison operation;
11059 if both args are promoted shorts, compare the original shorts.
11060 Also implies COMMON. */
11061 int short_compare = 0;
11063 /* Nonzero if this is a right-shift operation, which can be computed on the
11064 original short and then promoted if the operand is a promoted short. */
11065 int short_shift = 0;
11067 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11068 int common = 0;
11070 /* True means types are compatible as far as ObjC is concerned. */
11071 bool objc_ok;
11073 /* True means this is an arithmetic operation that may need excess
11074 precision. */
11075 bool may_need_excess_precision;
11077 /* True means this is a boolean operation that converts both its
11078 operands to truth-values. */
11079 bool boolean_op = false;
11081 /* Remember whether we're doing / or %. */
11082 bool doing_div_or_mod = false;
11084 /* Remember whether we're doing << or >>. */
11085 bool doing_shift = false;
11087 /* Tree holding instrumentation expression. */
11088 tree instrument_expr = NULL;
11090 if (location == UNKNOWN_LOCATION)
11091 location = input_location;
11093 op0 = orig_op0;
11094 op1 = orig_op1;
11096 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11097 if (op0_int_operands)
11098 op0 = remove_c_maybe_const_expr (op0);
11099 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11100 if (op1_int_operands)
11101 op1 = remove_c_maybe_const_expr (op1);
11102 int_operands = (op0_int_operands && op1_int_operands);
11103 if (int_operands)
11105 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11106 && TREE_CODE (orig_op1) == INTEGER_CST);
11107 int_const = (int_const_or_overflow
11108 && !TREE_OVERFLOW (orig_op0)
11109 && !TREE_OVERFLOW (orig_op1));
11111 else
11112 int_const = int_const_or_overflow = false;
11114 /* Do not apply default conversion in mixed vector/scalar expression. */
11115 if (convert_p
11116 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11118 op0 = default_conversion (op0);
11119 op1 = default_conversion (op1);
11122 orig_type0 = type0 = TREE_TYPE (op0);
11124 orig_type1 = type1 = TREE_TYPE (op1);
11126 /* The expression codes of the data types of the arguments tell us
11127 whether the arguments are integers, floating, pointers, etc. */
11128 code0 = TREE_CODE (type0);
11129 code1 = TREE_CODE (type1);
11131 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11132 STRIP_TYPE_NOPS (op0);
11133 STRIP_TYPE_NOPS (op1);
11135 /* If an error was already reported for one of the arguments,
11136 avoid reporting another error. */
11138 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11139 return error_mark_node;
11141 if (code0 == POINTER_TYPE
11142 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11143 return error_mark_node;
11145 if (code1 == POINTER_TYPE
11146 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11147 return error_mark_node;
11149 if ((invalid_op_diag
11150 = targetm.invalid_binary_op (code, type0, type1)))
11152 error_at (location, invalid_op_diag);
11153 return error_mark_node;
11156 switch (code)
11158 case PLUS_EXPR:
11159 case MINUS_EXPR:
11160 case MULT_EXPR:
11161 case TRUNC_DIV_EXPR:
11162 case CEIL_DIV_EXPR:
11163 case FLOOR_DIV_EXPR:
11164 case ROUND_DIV_EXPR:
11165 case EXACT_DIV_EXPR:
11166 may_need_excess_precision = true;
11167 break;
11168 default:
11169 may_need_excess_precision = false;
11170 break;
11172 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11174 op0 = TREE_OPERAND (op0, 0);
11175 type0 = TREE_TYPE (op0);
11177 else if (may_need_excess_precision
11178 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11180 type0 = eptype;
11181 op0 = convert (eptype, op0);
11183 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11185 op1 = TREE_OPERAND (op1, 0);
11186 type1 = TREE_TYPE (op1);
11188 else if (may_need_excess_precision
11189 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11191 type1 = eptype;
11192 op1 = convert (eptype, op1);
11195 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11197 /* In case when one of the operands of the binary operation is
11198 a vector and another is a scalar -- convert scalar to vector. */
11199 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11201 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11202 true);
11204 switch (convert_flag)
11206 case stv_error:
11207 return error_mark_node;
11208 case stv_firstarg:
11210 bool maybe_const = true;
11211 tree sc;
11212 sc = c_fully_fold (op0, false, &maybe_const);
11213 sc = save_expr (sc);
11214 sc = convert (TREE_TYPE (type1), sc);
11215 op0 = build_vector_from_val (type1, sc);
11216 if (!maybe_const)
11217 op0 = c_wrap_maybe_const (op0, true);
11218 orig_type0 = type0 = TREE_TYPE (op0);
11219 code0 = TREE_CODE (type0);
11220 converted = 1;
11221 break;
11223 case stv_secondarg:
11225 bool maybe_const = true;
11226 tree sc;
11227 sc = c_fully_fold (op1, false, &maybe_const);
11228 sc = save_expr (sc);
11229 sc = convert (TREE_TYPE (type0), sc);
11230 op1 = build_vector_from_val (type0, sc);
11231 if (!maybe_const)
11232 op1 = c_wrap_maybe_const (op1, true);
11233 orig_type1 = type1 = TREE_TYPE (op1);
11234 code1 = TREE_CODE (type1);
11235 converted = 1;
11236 break;
11238 default:
11239 break;
11243 switch (code)
11245 case PLUS_EXPR:
11246 /* Handle the pointer + int case. */
11247 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11249 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11250 goto return_build_binary_op;
11252 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11254 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11255 goto return_build_binary_op;
11257 else
11258 common = 1;
11259 break;
11261 case MINUS_EXPR:
11262 /* Subtraction of two similar pointers.
11263 We must subtract them as integers, then divide by object size. */
11264 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11265 && comp_target_types (location, type0, type1))
11267 ret = pointer_diff (location, op0, op1, &instrument_expr);
11268 goto return_build_binary_op;
11270 /* Handle pointer minus int. Just like pointer plus int. */
11271 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11273 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11274 goto return_build_binary_op;
11276 else
11277 common = 1;
11278 break;
11280 case MULT_EXPR:
11281 common = 1;
11282 break;
11284 case TRUNC_DIV_EXPR:
11285 case CEIL_DIV_EXPR:
11286 case FLOOR_DIV_EXPR:
11287 case ROUND_DIV_EXPR:
11288 case EXACT_DIV_EXPR:
11289 doing_div_or_mod = true;
11290 warn_for_div_by_zero (location, op1);
11292 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11293 || code0 == FIXED_POINT_TYPE
11294 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11295 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11296 || code1 == FIXED_POINT_TYPE
11297 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11299 enum tree_code tcode0 = code0, tcode1 = code1;
11301 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11302 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11303 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11304 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11306 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11307 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11308 resultcode = RDIV_EXPR;
11309 else
11310 /* Although it would be tempting to shorten always here, that
11311 loses on some targets, since the modulo instruction is
11312 undefined if the quotient can't be represented in the
11313 computation mode. We shorten only if unsigned or if
11314 dividing by something we know != -1. */
11315 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11316 || (TREE_CODE (op1) == INTEGER_CST
11317 && !integer_all_onesp (op1)));
11318 common = 1;
11320 break;
11322 case BIT_AND_EXPR:
11323 case BIT_IOR_EXPR:
11324 case BIT_XOR_EXPR:
11325 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11326 shorten = -1;
11327 /* Allow vector types which are not floating point types. */
11328 else if (code0 == VECTOR_TYPE
11329 && code1 == VECTOR_TYPE
11330 && !VECTOR_FLOAT_TYPE_P (type0)
11331 && !VECTOR_FLOAT_TYPE_P (type1))
11332 common = 1;
11333 break;
11335 case TRUNC_MOD_EXPR:
11336 case FLOOR_MOD_EXPR:
11337 doing_div_or_mod = true;
11338 warn_for_div_by_zero (location, op1);
11340 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11341 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11342 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11343 common = 1;
11344 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11346 /* Although it would be tempting to shorten always here, that loses
11347 on some targets, since the modulo instruction is undefined if the
11348 quotient can't be represented in the computation mode. We shorten
11349 only if unsigned or if dividing by something we know != -1. */
11350 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11351 || (TREE_CODE (op1) == INTEGER_CST
11352 && !integer_all_onesp (op1)));
11353 common = 1;
11355 break;
11357 case TRUTH_ANDIF_EXPR:
11358 case TRUTH_ORIF_EXPR:
11359 case TRUTH_AND_EXPR:
11360 case TRUTH_OR_EXPR:
11361 case TRUTH_XOR_EXPR:
11362 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11363 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11364 || code0 == FIXED_POINT_TYPE)
11365 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11366 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11367 || code1 == FIXED_POINT_TYPE))
11369 /* Result of these operations is always an int,
11370 but that does not mean the operands should be
11371 converted to ints! */
11372 result_type = integer_type_node;
11373 if (op0_int_operands)
11375 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11376 op0 = remove_c_maybe_const_expr (op0);
11378 else
11379 op0 = c_objc_common_truthvalue_conversion (location, op0);
11380 if (op1_int_operands)
11382 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11383 op1 = remove_c_maybe_const_expr (op1);
11385 else
11386 op1 = c_objc_common_truthvalue_conversion (location, op1);
11387 converted = 1;
11388 boolean_op = true;
11390 if (code == TRUTH_ANDIF_EXPR)
11392 int_const_or_overflow = (int_operands
11393 && TREE_CODE (orig_op0) == INTEGER_CST
11394 && (op0 == truthvalue_false_node
11395 || TREE_CODE (orig_op1) == INTEGER_CST));
11396 int_const = (int_const_or_overflow
11397 && !TREE_OVERFLOW (orig_op0)
11398 && (op0 == truthvalue_false_node
11399 || !TREE_OVERFLOW (orig_op1)));
11401 else if (code == TRUTH_ORIF_EXPR)
11403 int_const_or_overflow = (int_operands
11404 && TREE_CODE (orig_op0) == INTEGER_CST
11405 && (op0 == truthvalue_true_node
11406 || TREE_CODE (orig_op1) == INTEGER_CST));
11407 int_const = (int_const_or_overflow
11408 && !TREE_OVERFLOW (orig_op0)
11409 && (op0 == truthvalue_true_node
11410 || !TREE_OVERFLOW (orig_op1)));
11412 break;
11414 /* Shift operations: result has same type as first operand;
11415 always convert second operand to int.
11416 Also set SHORT_SHIFT if shifting rightward. */
11418 case RSHIFT_EXPR:
11419 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11420 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11421 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11422 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11423 TYPE_VECTOR_SUBPARTS (type1)))
11425 result_type = type0;
11426 converted = 1;
11428 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11429 || (code0 == VECTOR_TYPE
11430 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11431 && code1 == INTEGER_TYPE)
11433 doing_shift = true;
11434 if (TREE_CODE (op1) == INTEGER_CST)
11436 if (tree_int_cst_sgn (op1) < 0)
11438 int_const = false;
11439 if (c_inhibit_evaluation_warnings == 0)
11440 warning_at (location, OPT_Wshift_count_negative,
11441 "right shift count is negative");
11443 else if (code0 == VECTOR_TYPE)
11445 if (compare_tree_int (op1,
11446 TYPE_PRECISION (TREE_TYPE (type0)))
11447 >= 0)
11449 int_const = false;
11450 if (c_inhibit_evaluation_warnings == 0)
11451 warning_at (location, OPT_Wshift_count_overflow,
11452 "right shift count >= width of vector element");
11455 else
11457 if (!integer_zerop (op1))
11458 short_shift = 1;
11460 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11462 int_const = false;
11463 if (c_inhibit_evaluation_warnings == 0)
11464 warning_at (location, OPT_Wshift_count_overflow,
11465 "right shift count >= width of type");
11470 /* Use the type of the value to be shifted. */
11471 result_type = type0;
11472 /* Avoid converting op1 to result_type later. */
11473 converted = 1;
11475 break;
11477 case LSHIFT_EXPR:
11478 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11479 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11480 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11481 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11482 TYPE_VECTOR_SUBPARTS (type1)))
11484 result_type = type0;
11485 converted = 1;
11487 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11488 || (code0 == VECTOR_TYPE
11489 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11490 && code1 == INTEGER_TYPE)
11492 doing_shift = true;
11493 if (TREE_CODE (op0) == INTEGER_CST
11494 && tree_int_cst_sgn (op0) < 0)
11496 /* Don't reject a left shift of a negative value in a context
11497 where a constant expression is needed in C90. */
11498 if (flag_isoc99)
11499 int_const = false;
11500 if (c_inhibit_evaluation_warnings == 0)
11501 warning_at (location, OPT_Wshift_negative_value,
11502 "left shift of negative value");
11504 if (TREE_CODE (op1) == INTEGER_CST)
11506 if (tree_int_cst_sgn (op1) < 0)
11508 int_const = false;
11509 if (c_inhibit_evaluation_warnings == 0)
11510 warning_at (location, OPT_Wshift_count_negative,
11511 "left shift count is negative");
11513 else if (code0 == VECTOR_TYPE)
11515 if (compare_tree_int (op1,
11516 TYPE_PRECISION (TREE_TYPE (type0)))
11517 >= 0)
11519 int_const = false;
11520 if (c_inhibit_evaluation_warnings == 0)
11521 warning_at (location, OPT_Wshift_count_overflow,
11522 "left shift count >= width of vector element");
11525 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11527 int_const = false;
11528 if (c_inhibit_evaluation_warnings == 0)
11529 warning_at (location, OPT_Wshift_count_overflow,
11530 "left shift count >= width of type");
11532 else if (TREE_CODE (op0) == INTEGER_CST
11533 && maybe_warn_shift_overflow (location, op0, op1)
11534 && flag_isoc99)
11535 int_const = false;
11538 /* Use the type of the value to be shifted. */
11539 result_type = type0;
11540 /* Avoid converting op1 to result_type later. */
11541 converted = 1;
11543 break;
11545 case EQ_EXPR:
11546 case NE_EXPR:
11547 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11549 tree intt;
11550 if (!vector_types_compatible_elements_p (type0, type1))
11552 error_at (location, "comparing vectors with different "
11553 "element types");
11554 return error_mark_node;
11557 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11558 TYPE_VECTOR_SUBPARTS (type1)))
11560 error_at (location, "comparing vectors with different "
11561 "number of elements");
11562 return error_mark_node;
11565 /* It's not precisely specified how the usual arithmetic
11566 conversions apply to the vector types. Here, we use
11567 the unsigned type if one of the operands is signed and
11568 the other one is unsigned. */
11569 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11571 if (!TYPE_UNSIGNED (type0))
11572 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11573 else
11574 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11575 warning_at (location, OPT_Wsign_compare, "comparison between "
11576 "types %qT and %qT", type0, type1);
11579 /* Always construct signed integer vector type. */
11580 intt = c_common_type_for_size (GET_MODE_BITSIZE
11581 (SCALAR_TYPE_MODE
11582 (TREE_TYPE (type0))), 0);
11583 if (!intt)
11585 error_at (location, "could not find an integer type "
11586 "of the same size as %qT",
11587 TREE_TYPE (type0));
11588 return error_mark_node;
11590 result_type = build_opaque_vector_type (intt,
11591 TYPE_VECTOR_SUBPARTS (type0));
11592 converted = 1;
11593 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11594 goto return_build_binary_op;
11596 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11597 warning_at (location,
11598 OPT_Wfloat_equal,
11599 "comparing floating point with == or != is unsafe");
11600 /* Result of comparison is always int,
11601 but don't convert the args to int! */
11602 build_type = integer_type_node;
11603 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11604 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11605 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11606 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11607 short_compare = 1;
11608 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11610 if (TREE_CODE (op0) == ADDR_EXPR
11611 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11612 && !from_macro_expansion_at (location))
11614 if (code == EQ_EXPR)
11615 warning_at (location,
11616 OPT_Waddress,
11617 "the comparison will always evaluate as %<false%> "
11618 "for the address of %qD will never be NULL",
11619 TREE_OPERAND (op0, 0));
11620 else
11621 warning_at (location,
11622 OPT_Waddress,
11623 "the comparison will always evaluate as %<true%> "
11624 "for the address of %qD will never be NULL",
11625 TREE_OPERAND (op0, 0));
11627 result_type = type0;
11629 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11631 if (TREE_CODE (op1) == ADDR_EXPR
11632 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11633 && !from_macro_expansion_at (location))
11635 if (code == EQ_EXPR)
11636 warning_at (location,
11637 OPT_Waddress,
11638 "the comparison will always evaluate as %<false%> "
11639 "for the address of %qD will never be NULL",
11640 TREE_OPERAND (op1, 0));
11641 else
11642 warning_at (location,
11643 OPT_Waddress,
11644 "the comparison will always evaluate as %<true%> "
11645 "for the address of %qD will never be NULL",
11646 TREE_OPERAND (op1, 0));
11648 result_type = type1;
11650 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11652 tree tt0 = TREE_TYPE (type0);
11653 tree tt1 = TREE_TYPE (type1);
11654 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11655 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11656 addr_space_t as_common = ADDR_SPACE_GENERIC;
11658 /* Anything compares with void *. void * compares with anything.
11659 Otherwise, the targets must be compatible
11660 and both must be object or both incomplete. */
11661 if (comp_target_types (location, type0, type1))
11662 result_type = common_pointer_type (type0, type1);
11663 else if (!addr_space_superset (as0, as1, &as_common))
11665 error_at (location, "comparison of pointers to "
11666 "disjoint address spaces");
11667 return error_mark_node;
11669 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11671 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11672 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11673 "comparison of %<void *%> with function pointer");
11675 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11677 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11678 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11679 "comparison of %<void *%> with function pointer");
11681 else
11682 /* Avoid warning about the volatile ObjC EH puts on decls. */
11683 if (!objc_ok)
11684 pedwarn (location, 0,
11685 "comparison of distinct pointer types lacks a cast");
11687 if (result_type == NULL_TREE)
11689 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11690 result_type = build_pointer_type
11691 (build_qualified_type (void_type_node, qual));
11694 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11696 result_type = type0;
11697 pedwarn (location, 0, "comparison between pointer and integer");
11699 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11701 result_type = type1;
11702 pedwarn (location, 0, "comparison between pointer and integer");
11704 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11705 || truth_value_p (TREE_CODE (orig_op0)))
11706 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11707 || truth_value_p (TREE_CODE (orig_op1))))
11708 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11709 break;
11711 case LE_EXPR:
11712 case GE_EXPR:
11713 case LT_EXPR:
11714 case GT_EXPR:
11715 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11717 tree intt;
11718 if (!vector_types_compatible_elements_p (type0, type1))
11720 error_at (location, "comparing vectors with different "
11721 "element types");
11722 return error_mark_node;
11725 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11726 TYPE_VECTOR_SUBPARTS (type1)))
11728 error_at (location, "comparing vectors with different "
11729 "number of elements");
11730 return error_mark_node;
11733 /* It's not precisely specified how the usual arithmetic
11734 conversions apply to the vector types. Here, we use
11735 the unsigned type if one of the operands is signed and
11736 the other one is unsigned. */
11737 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11739 if (!TYPE_UNSIGNED (type0))
11740 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11741 else
11742 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11743 warning_at (location, OPT_Wsign_compare, "comparison between "
11744 "types %qT and %qT", type0, type1);
11747 /* Always construct signed integer vector type. */
11748 intt = c_common_type_for_size (GET_MODE_BITSIZE
11749 (SCALAR_TYPE_MODE
11750 (TREE_TYPE (type0))), 0);
11751 if (!intt)
11753 error_at (location, "could not find an integer type "
11754 "of the same size as %qT",
11755 TREE_TYPE (type0));
11756 return error_mark_node;
11758 result_type = build_opaque_vector_type (intt,
11759 TYPE_VECTOR_SUBPARTS (type0));
11760 converted = 1;
11761 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11762 goto return_build_binary_op;
11764 build_type = integer_type_node;
11765 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11766 || code0 == FIXED_POINT_TYPE)
11767 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11768 || code1 == FIXED_POINT_TYPE))
11769 short_compare = 1;
11770 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11772 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11773 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11774 addr_space_t as_common;
11776 if (comp_target_types (location, type0, type1))
11778 result_type = common_pointer_type (type0, type1);
11779 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11780 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11781 pedwarn (location, 0,
11782 "comparison of complete and incomplete pointers");
11783 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11784 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11785 "ordered comparisons of pointers to functions");
11786 else if (null_pointer_constant_p (orig_op0)
11787 || null_pointer_constant_p (orig_op1))
11788 warning_at (location, OPT_Wextra,
11789 "ordered comparison of pointer with null pointer");
11792 else if (!addr_space_superset (as0, as1, &as_common))
11794 error_at (location, "comparison of pointers to "
11795 "disjoint address spaces");
11796 return error_mark_node;
11798 else
11800 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11801 result_type = build_pointer_type
11802 (build_qualified_type (void_type_node, qual));
11803 pedwarn (location, 0,
11804 "comparison of distinct pointer types lacks a cast");
11807 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11809 result_type = type0;
11810 if (pedantic)
11811 pedwarn (location, OPT_Wpedantic,
11812 "ordered comparison of pointer with integer zero");
11813 else if (extra_warnings)
11814 warning_at (location, OPT_Wextra,
11815 "ordered comparison of pointer with integer zero");
11817 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11819 result_type = type1;
11820 if (pedantic)
11821 pedwarn (location, OPT_Wpedantic,
11822 "ordered comparison of pointer with integer zero");
11823 else if (extra_warnings)
11824 warning_at (location, OPT_Wextra,
11825 "ordered comparison of pointer with integer zero");
11827 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11829 result_type = type0;
11830 pedwarn (location, 0, "comparison between pointer and integer");
11832 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11834 result_type = type1;
11835 pedwarn (location, 0, "comparison between pointer and integer");
11838 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
11839 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
11841 op0 = save_expr (op0);
11842 op1 = save_expr (op1);
11844 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
11845 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
11848 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11849 || truth_value_p (TREE_CODE (orig_op0)))
11850 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11851 || truth_value_p (TREE_CODE (orig_op1))))
11852 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11853 break;
11855 default:
11856 gcc_unreachable ();
11859 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11860 return error_mark_node;
11862 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11863 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11864 || !vector_types_compatible_elements_p (type0, type1)))
11866 gcc_rich_location richloc (location);
11867 richloc.maybe_add_expr (orig_op0);
11868 richloc.maybe_add_expr (orig_op1);
11869 binary_op_error (&richloc, code, type0, type1);
11870 return error_mark_node;
11873 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11874 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11876 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11877 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11879 bool first_complex = (code0 == COMPLEX_TYPE);
11880 bool second_complex = (code1 == COMPLEX_TYPE);
11881 int none_complex = (!first_complex && !second_complex);
11883 if (shorten || common || short_compare)
11885 result_type = c_common_type (type0, type1);
11886 do_warn_double_promotion (result_type, type0, type1,
11887 "implicit conversion from %qT to %qT "
11888 "to match other operand of binary "
11889 "expression",
11890 location);
11891 if (result_type == error_mark_node)
11892 return error_mark_node;
11895 if (first_complex != second_complex
11896 && (code == PLUS_EXPR
11897 || code == MINUS_EXPR
11898 || code == MULT_EXPR
11899 || (code == TRUNC_DIV_EXPR && first_complex))
11900 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11901 && flag_signed_zeros)
11903 /* An operation on mixed real/complex operands must be
11904 handled specially, but the language-independent code can
11905 more easily optimize the plain complex arithmetic if
11906 -fno-signed-zeros. */
11907 tree real_type = TREE_TYPE (result_type);
11908 tree real, imag;
11909 if (type0 != orig_type0 || type1 != orig_type1)
11911 gcc_assert (may_need_excess_precision && common);
11912 semantic_result_type = c_common_type (orig_type0, orig_type1);
11914 if (first_complex)
11916 if (TREE_TYPE (op0) != result_type)
11917 op0 = convert_and_check (location, result_type, op0);
11918 if (TREE_TYPE (op1) != real_type)
11919 op1 = convert_and_check (location, real_type, op1);
11921 else
11923 if (TREE_TYPE (op0) != real_type)
11924 op0 = convert_and_check (location, real_type, op0);
11925 if (TREE_TYPE (op1) != result_type)
11926 op1 = convert_and_check (location, result_type, op1);
11928 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11929 return error_mark_node;
11930 if (first_complex)
11932 op0 = save_expr (op0);
11933 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11934 op0, true);
11935 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11936 op0, true);
11937 switch (code)
11939 case MULT_EXPR:
11940 case TRUNC_DIV_EXPR:
11941 op1 = save_expr (op1);
11942 imag = build2 (resultcode, real_type, imag, op1);
11943 /* Fall through. */
11944 case PLUS_EXPR:
11945 case MINUS_EXPR:
11946 real = build2 (resultcode, real_type, real, op1);
11947 break;
11948 default:
11949 gcc_unreachable();
11952 else
11954 op1 = save_expr (op1);
11955 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11956 op1, true);
11957 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11958 op1, true);
11959 switch (code)
11961 case MULT_EXPR:
11962 op0 = save_expr (op0);
11963 imag = build2 (resultcode, real_type, op0, imag);
11964 /* Fall through. */
11965 case PLUS_EXPR:
11966 real = build2 (resultcode, real_type, op0, real);
11967 break;
11968 case MINUS_EXPR:
11969 real = build2 (resultcode, real_type, op0, real);
11970 imag = build1 (NEGATE_EXPR, real_type, imag);
11971 break;
11972 default:
11973 gcc_unreachable();
11976 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11977 goto return_build_binary_op;
11980 /* For certain operations (which identify themselves by shorten != 0)
11981 if both args were extended from the same smaller type,
11982 do the arithmetic in that type and then extend.
11984 shorten !=0 and !=1 indicates a bitwise operation.
11985 For them, this optimization is safe only if
11986 both args are zero-extended or both are sign-extended.
11987 Otherwise, we might change the result.
11988 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11989 but calculated in (unsigned short) it would be (unsigned short)-1. */
11991 if (shorten && none_complex)
11993 final_type = result_type;
11994 result_type = shorten_binary_op (result_type, op0, op1,
11995 shorten == -1);
11998 /* Shifts can be shortened if shifting right. */
12000 if (short_shift)
12002 int unsigned_arg;
12003 tree arg0 = get_narrower (op0, &unsigned_arg);
12005 final_type = result_type;
12007 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12008 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12010 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12011 && tree_int_cst_sgn (op1) > 0
12012 /* We can shorten only if the shift count is less than the
12013 number of bits in the smaller type size. */
12014 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12015 /* We cannot drop an unsigned shift after sign-extension. */
12016 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12018 /* Do an unsigned shift if the operand was zero-extended. */
12019 result_type
12020 = c_common_signed_or_unsigned_type (unsigned_arg,
12021 TREE_TYPE (arg0));
12022 /* Convert value-to-be-shifted to that type. */
12023 if (TREE_TYPE (op0) != result_type)
12024 op0 = convert (result_type, op0);
12025 converted = 1;
12029 /* Comparison operations are shortened too but differently.
12030 They identify themselves by setting short_compare = 1. */
12032 if (short_compare)
12034 /* Don't write &op0, etc., because that would prevent op0
12035 from being kept in a register.
12036 Instead, make copies of the our local variables and
12037 pass the copies by reference, then copy them back afterward. */
12038 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12039 enum tree_code xresultcode = resultcode;
12040 tree val
12041 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12042 &xresultcode);
12044 if (val != NULL_TREE)
12046 ret = val;
12047 goto return_build_binary_op;
12050 op0 = xop0, op1 = xop1;
12051 converted = 1;
12052 resultcode = xresultcode;
12054 if (c_inhibit_evaluation_warnings == 0)
12056 bool op0_maybe_const = true;
12057 bool op1_maybe_const = true;
12058 tree orig_op0_folded, orig_op1_folded;
12060 if (in_late_binary_op)
12062 orig_op0_folded = orig_op0;
12063 orig_op1_folded = orig_op1;
12065 else
12067 /* Fold for the sake of possible warnings, as in
12068 build_conditional_expr. This requires the
12069 "original" values to be folded, not just op0 and
12070 op1. */
12071 c_inhibit_evaluation_warnings++;
12072 op0 = c_fully_fold (op0, require_constant_value,
12073 &op0_maybe_const);
12074 op1 = c_fully_fold (op1, require_constant_value,
12075 &op1_maybe_const);
12076 c_inhibit_evaluation_warnings--;
12077 orig_op0_folded = c_fully_fold (orig_op0,
12078 require_constant_value,
12079 NULL);
12080 orig_op1_folded = c_fully_fold (orig_op1,
12081 require_constant_value,
12082 NULL);
12085 if (warn_sign_compare)
12086 warn_for_sign_compare (location, orig_op0_folded,
12087 orig_op1_folded, op0, op1,
12088 result_type, resultcode);
12089 if (!in_late_binary_op && !int_operands)
12091 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12092 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12093 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12094 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12100 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12101 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12102 Then the expression will be built.
12103 It will be given type FINAL_TYPE if that is nonzero;
12104 otherwise, it will be given type RESULT_TYPE. */
12106 if (!result_type)
12108 gcc_rich_location richloc (location);
12109 richloc.maybe_add_expr (orig_op0);
12110 richloc.maybe_add_expr (orig_op1);
12111 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12112 return error_mark_node;
12115 if (build_type == NULL_TREE)
12117 build_type = result_type;
12118 if ((type0 != orig_type0 || type1 != orig_type1)
12119 && !boolean_op)
12121 gcc_assert (may_need_excess_precision && common);
12122 semantic_result_type = c_common_type (orig_type0, orig_type1);
12126 if (!converted)
12128 op0 = ep_convert_and_check (location, result_type, op0,
12129 semantic_result_type);
12130 op1 = ep_convert_and_check (location, result_type, op1,
12131 semantic_result_type);
12133 /* This can happen if one operand has a vector type, and the other
12134 has a different type. */
12135 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12136 return error_mark_node;
12139 if (sanitize_flags_p ((SANITIZE_SHIFT
12140 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12141 && current_function_decl != NULL_TREE
12142 && (doing_div_or_mod || doing_shift)
12143 && !require_constant_value)
12145 /* OP0 and/or OP1 might have side-effects. */
12146 op0 = save_expr (op0);
12147 op1 = save_expr (op1);
12148 op0 = c_fully_fold (op0, false, NULL);
12149 op1 = c_fully_fold (op1, false, NULL);
12150 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12151 | SANITIZE_FLOAT_DIVIDE))))
12152 instrument_expr = ubsan_instrument_division (location, op0, op1);
12153 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12154 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12157 /* Treat expressions in initializers specially as they can't trap. */
12158 if (int_const_or_overflow)
12159 ret = (require_constant_value
12160 ? fold_build2_initializer_loc (location, resultcode, build_type,
12161 op0, op1)
12162 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12163 else
12164 ret = build2 (resultcode, build_type, op0, op1);
12165 if (final_type != NULL_TREE)
12166 ret = convert (final_type, ret);
12168 return_build_binary_op:
12169 gcc_assert (ret != error_mark_node);
12170 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12171 ret = (int_operands
12172 ? note_integer_operands (ret)
12173 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12174 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12175 && !in_late_binary_op)
12176 ret = note_integer_operands (ret);
12177 protected_set_expr_location (ret, location);
12179 if (instrument_expr != NULL)
12180 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12181 instrument_expr, ret);
12183 if (semantic_result_type)
12184 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12185 semantic_result_type, ret);
12187 return ret;
12191 /* Convert EXPR to be a truth-value, validating its type for this
12192 purpose. LOCATION is the source location for the expression. */
12194 tree
12195 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12197 bool int_const, int_operands;
12199 switch (TREE_CODE (TREE_TYPE (expr)))
12201 case ARRAY_TYPE:
12202 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12203 return error_mark_node;
12205 case RECORD_TYPE:
12206 error_at (location, "used struct type value where scalar is required");
12207 return error_mark_node;
12209 case UNION_TYPE:
12210 error_at (location, "used union type value where scalar is required");
12211 return error_mark_node;
12213 case VOID_TYPE:
12214 error_at (location, "void value not ignored as it ought to be");
12215 return error_mark_node;
12217 case POINTER_TYPE:
12218 if (reject_gcc_builtin (expr))
12219 return error_mark_node;
12220 break;
12222 case FUNCTION_TYPE:
12223 gcc_unreachable ();
12225 case VECTOR_TYPE:
12226 error_at (location, "used vector type where scalar is required");
12227 return error_mark_node;
12229 default:
12230 break;
12233 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12234 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12235 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12237 expr = remove_c_maybe_const_expr (expr);
12238 expr = build2 (NE_EXPR, integer_type_node, expr,
12239 convert (TREE_TYPE (expr), integer_zero_node));
12240 expr = note_integer_operands (expr);
12242 else
12243 /* ??? Should we also give an error for vectors rather than leaving
12244 those to give errors later? */
12245 expr = c_common_truthvalue_conversion (location, expr);
12247 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12249 if (TREE_OVERFLOW (expr))
12250 return expr;
12251 else
12252 return note_integer_operands (expr);
12254 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12255 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12256 return expr;
12260 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12261 required. */
12263 tree
12264 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12266 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12268 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12269 /* Executing a compound literal inside a function reinitializes
12270 it. */
12271 if (!TREE_STATIC (decl))
12272 *se = true;
12273 return decl;
12275 else
12276 return expr;
12279 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12280 statement. LOC is the location of the construct. */
12282 tree
12283 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12284 tree clauses)
12286 body = c_end_compound_stmt (loc, body, true);
12288 tree stmt = make_node (code);
12289 TREE_TYPE (stmt) = void_type_node;
12290 OMP_BODY (stmt) = body;
12291 OMP_CLAUSES (stmt) = clauses;
12292 SET_EXPR_LOCATION (stmt, loc);
12294 return add_stmt (stmt);
12297 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12298 statement. LOC is the location of the OACC_DATA. */
12300 tree
12301 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12303 tree stmt;
12305 block = c_end_compound_stmt (loc, block, true);
12307 stmt = make_node (OACC_DATA);
12308 TREE_TYPE (stmt) = void_type_node;
12309 OACC_DATA_CLAUSES (stmt) = clauses;
12310 OACC_DATA_BODY (stmt) = block;
12311 SET_EXPR_LOCATION (stmt, loc);
12313 return add_stmt (stmt);
12316 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12317 statement. LOC is the location of the OACC_HOST_DATA. */
12319 tree
12320 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12322 tree stmt;
12324 block = c_end_compound_stmt (loc, block, true);
12326 stmt = make_node (OACC_HOST_DATA);
12327 TREE_TYPE (stmt) = void_type_node;
12328 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12329 OACC_HOST_DATA_BODY (stmt) = block;
12330 SET_EXPR_LOCATION (stmt, loc);
12332 return add_stmt (stmt);
12335 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12337 tree
12338 c_begin_omp_parallel (void)
12340 tree block;
12342 keep_next_level ();
12343 block = c_begin_compound_stmt (true);
12345 return block;
12348 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12349 statement. LOC is the location of the OMP_PARALLEL. */
12351 tree
12352 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12354 tree stmt;
12356 block = c_end_compound_stmt (loc, block, true);
12358 stmt = make_node (OMP_PARALLEL);
12359 TREE_TYPE (stmt) = void_type_node;
12360 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12361 OMP_PARALLEL_BODY (stmt) = block;
12362 SET_EXPR_LOCATION (stmt, loc);
12364 return add_stmt (stmt);
12367 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12369 tree
12370 c_begin_omp_task (void)
12372 tree block;
12374 keep_next_level ();
12375 block = c_begin_compound_stmt (true);
12377 return block;
12380 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12381 statement. LOC is the location of the #pragma. */
12383 tree
12384 c_finish_omp_task (location_t loc, tree clauses, tree block)
12386 tree stmt;
12388 block = c_end_compound_stmt (loc, block, true);
12390 stmt = make_node (OMP_TASK);
12391 TREE_TYPE (stmt) = void_type_node;
12392 OMP_TASK_CLAUSES (stmt) = clauses;
12393 OMP_TASK_BODY (stmt) = block;
12394 SET_EXPR_LOCATION (stmt, loc);
12396 return add_stmt (stmt);
12399 /* Generate GOMP_cancel call for #pragma omp cancel. */
12401 void
12402 c_finish_omp_cancel (location_t loc, tree clauses)
12404 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12405 int mask = 0;
12406 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12407 mask = 1;
12408 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12409 mask = 2;
12410 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12411 mask = 4;
12412 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12413 mask = 8;
12414 else
12416 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12417 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12418 "clauses");
12419 return;
12421 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12422 if (ifc != NULL_TREE)
12424 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12425 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12426 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12427 build_zero_cst (type));
12429 else
12430 ifc = boolean_true_node;
12431 tree stmt = build_call_expr_loc (loc, fn, 2,
12432 build_int_cst (integer_type_node, mask),
12433 ifc);
12434 add_stmt (stmt);
12437 /* Generate GOMP_cancellation_point call for
12438 #pragma omp cancellation point. */
12440 void
12441 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12443 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12444 int mask = 0;
12445 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12446 mask = 1;
12447 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12448 mask = 2;
12449 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12450 mask = 4;
12451 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12452 mask = 8;
12453 else
12455 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12456 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12457 "clauses");
12458 return;
12460 tree stmt = build_call_expr_loc (loc, fn, 1,
12461 build_int_cst (integer_type_node, mask));
12462 add_stmt (stmt);
12465 /* Helper function for handle_omp_array_sections. Called recursively
12466 to handle multiple array-section-subscripts. C is the clause,
12467 T current expression (initially OMP_CLAUSE_DECL), which is either
12468 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12469 expression if specified, TREE_VALUE length expression if specified,
12470 TREE_CHAIN is what it has been specified after, or some decl.
12471 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12472 set to true if any of the array-section-subscript could have length
12473 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12474 first array-section-subscript which is known not to have length
12475 of one. Given say:
12476 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12477 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12478 all are or may have length of 1, array-section-subscript [:2] is the
12479 first one known not to have length 1. For array-section-subscript
12480 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12481 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12482 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12483 case though, as some lengths could be zero. */
12485 static tree
12486 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12487 bool &maybe_zero_len, unsigned int &first_non_one,
12488 enum c_omp_region_type ort)
12490 tree ret, low_bound, length, type;
12491 if (TREE_CODE (t) != TREE_LIST)
12493 if (error_operand_p (t))
12494 return error_mark_node;
12495 ret = t;
12496 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12497 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12499 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12500 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12501 return error_mark_node;
12503 if (TREE_CODE (t) == COMPONENT_REF
12504 && ort == C_ORT_OMP
12505 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12506 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12507 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12509 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12511 error_at (OMP_CLAUSE_LOCATION (c),
12512 "bit-field %qE in %qs clause",
12513 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12514 return error_mark_node;
12516 while (TREE_CODE (t) == COMPONENT_REF)
12518 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12520 error_at (OMP_CLAUSE_LOCATION (c),
12521 "%qE is a member of a union", t);
12522 return error_mark_node;
12524 t = TREE_OPERAND (t, 0);
12527 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12529 if (DECL_P (t))
12530 error_at (OMP_CLAUSE_LOCATION (c),
12531 "%qD is not a variable in %qs clause", t,
12532 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12533 else
12534 error_at (OMP_CLAUSE_LOCATION (c),
12535 "%qE is not a variable in %qs clause", t,
12536 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12537 return error_mark_node;
12539 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12540 && TYPE_ATOMIC (TREE_TYPE (t)))
12542 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12543 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12544 return error_mark_node;
12546 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12547 && VAR_P (t)
12548 && DECL_THREAD_LOCAL_P (t))
12550 error_at (OMP_CLAUSE_LOCATION (c),
12551 "%qD is threadprivate variable in %qs clause", t,
12552 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12553 return error_mark_node;
12555 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12556 && TYPE_ATOMIC (TREE_TYPE (t))
12557 && POINTER_TYPE_P (TREE_TYPE (t)))
12559 /* If the array section is pointer based and the pointer
12560 itself is _Atomic qualified, we need to atomically load
12561 the pointer. */
12562 c_expr expr;
12563 memset (&expr, 0, sizeof (expr));
12564 expr.value = ret;
12565 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12566 expr, false, false);
12567 ret = expr.value;
12569 return ret;
12572 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12573 maybe_zero_len, first_non_one, ort);
12574 if (ret == error_mark_node || ret == NULL_TREE)
12575 return ret;
12577 type = TREE_TYPE (ret);
12578 low_bound = TREE_PURPOSE (t);
12579 length = TREE_VALUE (t);
12581 if (low_bound == error_mark_node || length == error_mark_node)
12582 return error_mark_node;
12584 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12586 error_at (OMP_CLAUSE_LOCATION (c),
12587 "low bound %qE of array section does not have integral type",
12588 low_bound);
12589 return error_mark_node;
12591 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12593 error_at (OMP_CLAUSE_LOCATION (c),
12594 "length %qE of array section does not have integral type",
12595 length);
12596 return error_mark_node;
12598 if (low_bound
12599 && TREE_CODE (low_bound) == INTEGER_CST
12600 && TYPE_PRECISION (TREE_TYPE (low_bound))
12601 > TYPE_PRECISION (sizetype))
12602 low_bound = fold_convert (sizetype, low_bound);
12603 if (length
12604 && TREE_CODE (length) == INTEGER_CST
12605 && TYPE_PRECISION (TREE_TYPE (length))
12606 > TYPE_PRECISION (sizetype))
12607 length = fold_convert (sizetype, length);
12608 if (low_bound == NULL_TREE)
12609 low_bound = integer_zero_node;
12611 if (length != NULL_TREE)
12613 if (!integer_nonzerop (length))
12615 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12616 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12618 if (integer_zerop (length))
12620 error_at (OMP_CLAUSE_LOCATION (c),
12621 "zero length array section in %qs clause",
12622 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12623 return error_mark_node;
12626 else
12627 maybe_zero_len = true;
12629 if (first_non_one == types.length ()
12630 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12631 first_non_one++;
12633 if (TREE_CODE (type) == ARRAY_TYPE)
12635 if (length == NULL_TREE
12636 && (TYPE_DOMAIN (type) == NULL_TREE
12637 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12639 error_at (OMP_CLAUSE_LOCATION (c),
12640 "for unknown bound array type length expression must "
12641 "be specified");
12642 return error_mark_node;
12644 if (TREE_CODE (low_bound) == INTEGER_CST
12645 && tree_int_cst_sgn (low_bound) == -1)
12647 error_at (OMP_CLAUSE_LOCATION (c),
12648 "negative low bound in array section in %qs clause",
12649 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12650 return error_mark_node;
12652 if (length != NULL_TREE
12653 && TREE_CODE (length) == INTEGER_CST
12654 && tree_int_cst_sgn (length) == -1)
12656 error_at (OMP_CLAUSE_LOCATION (c),
12657 "negative length in array section in %qs clause",
12658 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12659 return error_mark_node;
12661 if (TYPE_DOMAIN (type)
12662 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12663 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12664 == INTEGER_CST)
12666 tree size
12667 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12668 size = size_binop (PLUS_EXPR, size, size_one_node);
12669 if (TREE_CODE (low_bound) == INTEGER_CST)
12671 if (tree_int_cst_lt (size, low_bound))
12673 error_at (OMP_CLAUSE_LOCATION (c),
12674 "low bound %qE above array section size "
12675 "in %qs clause", low_bound,
12676 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12677 return error_mark_node;
12679 if (tree_int_cst_equal (size, low_bound))
12681 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12682 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12684 error_at (OMP_CLAUSE_LOCATION (c),
12685 "zero length array section in %qs clause",
12686 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12687 return error_mark_node;
12689 maybe_zero_len = true;
12691 else if (length == NULL_TREE
12692 && first_non_one == types.length ()
12693 && tree_int_cst_equal
12694 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12695 low_bound))
12696 first_non_one++;
12698 else if (length == NULL_TREE)
12700 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12701 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12702 maybe_zero_len = true;
12703 if (first_non_one == types.length ())
12704 first_non_one++;
12706 if (length && TREE_CODE (length) == INTEGER_CST)
12708 if (tree_int_cst_lt (size, length))
12710 error_at (OMP_CLAUSE_LOCATION (c),
12711 "length %qE above array section size "
12712 "in %qs clause", length,
12713 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12714 return error_mark_node;
12716 if (TREE_CODE (low_bound) == INTEGER_CST)
12718 tree lbpluslen
12719 = size_binop (PLUS_EXPR,
12720 fold_convert (sizetype, low_bound),
12721 fold_convert (sizetype, length));
12722 if (TREE_CODE (lbpluslen) == INTEGER_CST
12723 && tree_int_cst_lt (size, lbpluslen))
12725 error_at (OMP_CLAUSE_LOCATION (c),
12726 "high bound %qE above array section size "
12727 "in %qs clause", lbpluslen,
12728 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12729 return error_mark_node;
12734 else if (length == NULL_TREE)
12736 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12737 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12738 maybe_zero_len = true;
12739 if (first_non_one == types.length ())
12740 first_non_one++;
12743 /* For [lb:] we will need to evaluate lb more than once. */
12744 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12746 tree lb = save_expr (low_bound);
12747 if (lb != low_bound)
12749 TREE_PURPOSE (t) = lb;
12750 low_bound = lb;
12754 else if (TREE_CODE (type) == POINTER_TYPE)
12756 if (length == NULL_TREE)
12758 error_at (OMP_CLAUSE_LOCATION (c),
12759 "for pointer type length expression must be specified");
12760 return error_mark_node;
12762 if (length != NULL_TREE
12763 && TREE_CODE (length) == INTEGER_CST
12764 && tree_int_cst_sgn (length) == -1)
12766 error_at (OMP_CLAUSE_LOCATION (c),
12767 "negative length in array section in %qs clause",
12768 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12769 return error_mark_node;
12771 /* If there is a pointer type anywhere but in the very first
12772 array-section-subscript, the array section can't be contiguous. */
12773 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12774 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12776 error_at (OMP_CLAUSE_LOCATION (c),
12777 "array section is not contiguous in %qs clause",
12778 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12779 return error_mark_node;
12782 else
12784 error_at (OMP_CLAUSE_LOCATION (c),
12785 "%qE does not have pointer or array type", ret);
12786 return error_mark_node;
12788 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12789 types.safe_push (TREE_TYPE (ret));
12790 /* We will need to evaluate lb more than once. */
12791 tree lb = save_expr (low_bound);
12792 if (lb != low_bound)
12794 TREE_PURPOSE (t) = lb;
12795 low_bound = lb;
12797 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12798 return ret;
12801 /* Handle array sections for clause C. */
12803 static bool
12804 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12806 bool maybe_zero_len = false;
12807 unsigned int first_non_one = 0;
12808 auto_vec<tree, 10> types;
12809 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12810 maybe_zero_len, first_non_one,
12811 ort);
12812 if (first == error_mark_node)
12813 return true;
12814 if (first == NULL_TREE)
12815 return false;
12816 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12818 tree t = OMP_CLAUSE_DECL (c);
12819 tree tem = NULL_TREE;
12820 /* Need to evaluate side effects in the length expressions
12821 if any. */
12822 while (TREE_CODE (t) == TREE_LIST)
12824 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12826 if (tem == NULL_TREE)
12827 tem = TREE_VALUE (t);
12828 else
12829 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12830 TREE_VALUE (t), tem);
12832 t = TREE_CHAIN (t);
12834 if (tem)
12835 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12836 first = c_fully_fold (first, false, NULL, true);
12837 OMP_CLAUSE_DECL (c) = first;
12839 else
12841 unsigned int num = types.length (), i;
12842 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12843 tree condition = NULL_TREE;
12845 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12846 maybe_zero_len = true;
12848 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12849 t = TREE_CHAIN (t))
12851 tree low_bound = TREE_PURPOSE (t);
12852 tree length = TREE_VALUE (t);
12854 i--;
12855 if (low_bound
12856 && TREE_CODE (low_bound) == INTEGER_CST
12857 && TYPE_PRECISION (TREE_TYPE (low_bound))
12858 > TYPE_PRECISION (sizetype))
12859 low_bound = fold_convert (sizetype, low_bound);
12860 if (length
12861 && TREE_CODE (length) == INTEGER_CST
12862 && TYPE_PRECISION (TREE_TYPE (length))
12863 > TYPE_PRECISION (sizetype))
12864 length = fold_convert (sizetype, length);
12865 if (low_bound == NULL_TREE)
12866 low_bound = integer_zero_node;
12867 if (!maybe_zero_len && i > first_non_one)
12869 if (integer_nonzerop (low_bound))
12870 goto do_warn_noncontiguous;
12871 if (length != NULL_TREE
12872 && TREE_CODE (length) == INTEGER_CST
12873 && TYPE_DOMAIN (types[i])
12874 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12875 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12876 == INTEGER_CST)
12878 tree size;
12879 size = size_binop (PLUS_EXPR,
12880 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12881 size_one_node);
12882 if (!tree_int_cst_equal (length, size))
12884 do_warn_noncontiguous:
12885 error_at (OMP_CLAUSE_LOCATION (c),
12886 "array section is not contiguous in %qs "
12887 "clause",
12888 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12889 return true;
12892 if (length != NULL_TREE
12893 && TREE_SIDE_EFFECTS (length))
12895 if (side_effects == NULL_TREE)
12896 side_effects = length;
12897 else
12898 side_effects = build2 (COMPOUND_EXPR,
12899 TREE_TYPE (side_effects),
12900 length, side_effects);
12903 else
12905 tree l;
12907 if (i > first_non_one
12908 && ((length && integer_nonzerop (length))
12909 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12910 continue;
12911 if (length)
12912 l = fold_convert (sizetype, length);
12913 else
12915 l = size_binop (PLUS_EXPR,
12916 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12917 size_one_node);
12918 l = size_binop (MINUS_EXPR, l,
12919 fold_convert (sizetype, low_bound));
12921 if (i > first_non_one)
12923 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12924 size_zero_node);
12925 if (condition == NULL_TREE)
12926 condition = l;
12927 else
12928 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12929 l, condition);
12931 else if (size == NULL_TREE)
12933 size = size_in_bytes (TREE_TYPE (types[i]));
12934 tree eltype = TREE_TYPE (types[num - 1]);
12935 while (TREE_CODE (eltype) == ARRAY_TYPE)
12936 eltype = TREE_TYPE (eltype);
12937 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12939 if (integer_zerop (size)
12940 || integer_zerop (size_in_bytes (eltype)))
12942 error_at (OMP_CLAUSE_LOCATION (c),
12943 "zero length array section in %qs clause",
12944 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12945 return error_mark_node;
12947 size = size_binop (EXACT_DIV_EXPR, size,
12948 size_in_bytes (eltype));
12950 size = size_binop (MULT_EXPR, size, l);
12951 if (condition)
12952 size = fold_build3 (COND_EXPR, sizetype, condition,
12953 size, size_zero_node);
12955 else
12956 size = size_binop (MULT_EXPR, size, l);
12959 if (side_effects)
12960 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12961 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12963 size = size_binop (MINUS_EXPR, size, size_one_node);
12964 size = c_fully_fold (size, false, NULL);
12965 tree index_type = build_index_type (size);
12966 tree eltype = TREE_TYPE (first);
12967 while (TREE_CODE (eltype) == ARRAY_TYPE)
12968 eltype = TREE_TYPE (eltype);
12969 tree type = build_array_type (eltype, index_type);
12970 tree ptype = build_pointer_type (eltype);
12971 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12972 t = build_fold_addr_expr (t);
12973 tree t2 = build_fold_addr_expr (first);
12974 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12975 ptrdiff_type_node, t2);
12976 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12977 ptrdiff_type_node, t2,
12978 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12979 ptrdiff_type_node, t));
12980 t2 = c_fully_fold (t2, false, NULL);
12981 if (tree_fits_shwi_p (t2))
12982 t = build2 (MEM_REF, type, t,
12983 build_int_cst (ptype, tree_to_shwi (t2)));
12984 else
12986 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12987 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12988 TREE_TYPE (t), t, t2);
12989 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12991 OMP_CLAUSE_DECL (c) = t;
12992 return false;
12994 first = c_fully_fold (first, false, NULL);
12995 OMP_CLAUSE_DECL (c) = first;
12996 if (size)
12997 size = c_fully_fold (size, false, NULL);
12998 OMP_CLAUSE_SIZE (c) = size;
12999 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13000 || (TREE_CODE (t) == COMPONENT_REF
13001 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13002 return false;
13003 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13004 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
13005 switch (OMP_CLAUSE_MAP_KIND (c))
13007 case GOMP_MAP_ALLOC:
13008 case GOMP_MAP_TO:
13009 case GOMP_MAP_FROM:
13010 case GOMP_MAP_TOFROM:
13011 case GOMP_MAP_ALWAYS_TO:
13012 case GOMP_MAP_ALWAYS_FROM:
13013 case GOMP_MAP_ALWAYS_TOFROM:
13014 case GOMP_MAP_RELEASE:
13015 case GOMP_MAP_DELETE:
13016 case GOMP_MAP_FORCE_TO:
13017 case GOMP_MAP_FORCE_FROM:
13018 case GOMP_MAP_FORCE_TOFROM:
13019 case GOMP_MAP_FORCE_PRESENT:
13020 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13021 break;
13022 default:
13023 break;
13025 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13026 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
13027 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13028 else if (TREE_CODE (t) == COMPONENT_REF)
13029 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
13030 else
13031 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13032 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13033 && !c_mark_addressable (t))
13034 return false;
13035 OMP_CLAUSE_DECL (c2) = t;
13036 t = build_fold_addr_expr (first);
13037 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13038 tree ptr = OMP_CLAUSE_DECL (c2);
13039 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13040 ptr = build_fold_addr_expr (ptr);
13041 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13042 ptrdiff_type_node, t,
13043 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13044 ptrdiff_type_node, ptr));
13045 t = c_fully_fold (t, false, NULL);
13046 OMP_CLAUSE_SIZE (c2) = t;
13047 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13048 OMP_CLAUSE_CHAIN (c) = c2;
13050 return false;
13053 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13054 an inline call. But, remap
13055 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13056 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13058 static tree
13059 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13060 tree decl, tree placeholder)
13062 copy_body_data id;
13063 hash_map<tree, tree> decl_map;
13065 decl_map.put (omp_decl1, placeholder);
13066 decl_map.put (omp_decl2, decl);
13067 memset (&id, 0, sizeof (id));
13068 id.src_fn = DECL_CONTEXT (omp_decl1);
13069 id.dst_fn = current_function_decl;
13070 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13071 id.decl_map = &decl_map;
13073 id.copy_decl = copy_decl_no_change;
13074 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13075 id.transform_new_cfg = true;
13076 id.transform_return_to_modify = false;
13077 id.transform_lang_insert_block = NULL;
13078 id.eh_lp_nr = 0;
13079 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13080 return stmt;
13083 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13084 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13086 static tree
13087 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13089 if (*tp == (tree) data)
13090 return *tp;
13091 return NULL_TREE;
13094 /* For all elements of CLAUSES, validate them against their constraints.
13095 Remove any elements from the list that are invalid. */
13097 tree
13098 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13100 bitmap_head generic_head, firstprivate_head, lastprivate_head;
13101 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
13102 tree c, t, type, *pc;
13103 tree simdlen = NULL_TREE, safelen = NULL_TREE;
13104 bool branch_seen = false;
13105 bool copyprivate_seen = false;
13106 bool linear_variable_step_check = false;
13107 tree *nowait_clause = NULL;
13108 bool ordered_seen = false;
13109 tree schedule_clause = NULL_TREE;
13110 bool oacc_async = false;
13112 bitmap_obstack_initialize (NULL);
13113 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13114 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13115 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13116 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13117 bitmap_initialize (&map_head, &bitmap_default_obstack);
13118 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13119 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13121 if (ort & C_ORT_ACC)
13122 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13123 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13125 oacc_async = true;
13126 break;
13129 for (pc = &clauses, c = clauses; c ; c = *pc)
13131 bool remove = false;
13132 bool need_complete = false;
13133 bool need_implicitly_determined = false;
13135 switch (OMP_CLAUSE_CODE (c))
13137 case OMP_CLAUSE_SHARED:
13138 need_implicitly_determined = true;
13139 goto check_dup_generic;
13141 case OMP_CLAUSE_PRIVATE:
13142 need_complete = true;
13143 need_implicitly_determined = true;
13144 goto check_dup_generic;
13146 case OMP_CLAUSE_REDUCTION:
13147 need_implicitly_determined = true;
13148 t = OMP_CLAUSE_DECL (c);
13149 if (TREE_CODE (t) == TREE_LIST)
13151 if (handle_omp_array_sections (c, ort))
13153 remove = true;
13154 break;
13157 t = OMP_CLAUSE_DECL (c);
13159 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13160 if (t == error_mark_node)
13162 remove = true;
13163 break;
13165 if (oacc_async)
13166 c_mark_addressable (t);
13167 type = TREE_TYPE (t);
13168 if (TREE_CODE (t) == MEM_REF)
13169 type = TREE_TYPE (type);
13170 if (TREE_CODE (type) == ARRAY_TYPE)
13172 tree oatype = type;
13173 gcc_assert (TREE_CODE (t) != MEM_REF);
13174 while (TREE_CODE (type) == ARRAY_TYPE)
13175 type = TREE_TYPE (type);
13176 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13178 error_at (OMP_CLAUSE_LOCATION (c),
13179 "%qD in %<reduction%> clause is a zero size array",
13181 remove = true;
13182 break;
13184 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13185 TYPE_SIZE_UNIT (type));
13186 if (integer_zerop (size))
13188 error_at (OMP_CLAUSE_LOCATION (c),
13189 "%qD in %<reduction%> clause is a zero size array",
13191 remove = true;
13192 break;
13194 size = size_binop (MINUS_EXPR, size, size_one_node);
13195 tree index_type = build_index_type (size);
13196 tree atype = build_array_type (type, index_type);
13197 tree ptype = build_pointer_type (type);
13198 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13199 t = build_fold_addr_expr (t);
13200 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13201 OMP_CLAUSE_DECL (c) = t;
13203 if (TYPE_ATOMIC (type))
13205 error_at (OMP_CLAUSE_LOCATION (c),
13206 "%<_Atomic%> %qE in %<reduction%> clause", t);
13207 remove = true;
13208 break;
13210 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
13211 && (FLOAT_TYPE_P (type)
13212 || TREE_CODE (type) == COMPLEX_TYPE))
13214 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13215 const char *r_name = NULL;
13217 switch (r_code)
13219 case PLUS_EXPR:
13220 case MULT_EXPR:
13221 case MINUS_EXPR:
13222 break;
13223 case MIN_EXPR:
13224 if (TREE_CODE (type) == COMPLEX_TYPE)
13225 r_name = "min";
13226 break;
13227 case MAX_EXPR:
13228 if (TREE_CODE (type) == COMPLEX_TYPE)
13229 r_name = "max";
13230 break;
13231 case BIT_AND_EXPR:
13232 r_name = "&";
13233 break;
13234 case BIT_XOR_EXPR:
13235 r_name = "^";
13236 break;
13237 case BIT_IOR_EXPR:
13238 r_name = "|";
13239 break;
13240 case TRUTH_ANDIF_EXPR:
13241 if (FLOAT_TYPE_P (type))
13242 r_name = "&&";
13243 break;
13244 case TRUTH_ORIF_EXPR:
13245 if (FLOAT_TYPE_P (type))
13246 r_name = "||";
13247 break;
13248 default:
13249 gcc_unreachable ();
13251 if (r_name)
13253 error_at (OMP_CLAUSE_LOCATION (c),
13254 "%qE has invalid type for %<reduction(%s)%>",
13255 t, r_name);
13256 remove = true;
13257 break;
13260 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13262 error_at (OMP_CLAUSE_LOCATION (c),
13263 "user defined reduction not found for %qE", t);
13264 remove = true;
13265 break;
13267 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13269 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
13270 type = TYPE_MAIN_VARIANT (type);
13271 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13272 VAR_DECL, NULL_TREE, type);
13273 tree decl_placeholder = NULL_TREE;
13274 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13275 DECL_ARTIFICIAL (placeholder) = 1;
13276 DECL_IGNORED_P (placeholder) = 1;
13277 if (TREE_CODE (t) == MEM_REF)
13279 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13280 VAR_DECL, NULL_TREE, type);
13281 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13282 DECL_ARTIFICIAL (decl_placeholder) = 1;
13283 DECL_IGNORED_P (decl_placeholder) = 1;
13285 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13286 c_mark_addressable (placeholder);
13287 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
13288 c_mark_addressable (decl_placeholder ? decl_placeholder
13289 : OMP_CLAUSE_DECL (c));
13290 OMP_CLAUSE_REDUCTION_MERGE (c)
13291 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13292 TREE_VEC_ELT (list, 0),
13293 TREE_VEC_ELT (list, 1),
13294 decl_placeholder ? decl_placeholder
13295 : OMP_CLAUSE_DECL (c), placeholder);
13296 OMP_CLAUSE_REDUCTION_MERGE (c)
13297 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13298 void_type_node, NULL_TREE,
13299 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13300 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13301 if (TREE_VEC_LENGTH (list) == 6)
13303 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13304 c_mark_addressable (decl_placeholder ? decl_placeholder
13305 : OMP_CLAUSE_DECL (c));
13306 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13307 c_mark_addressable (placeholder);
13308 tree init = TREE_VEC_ELT (list, 5);
13309 if (init == error_mark_node)
13310 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13311 OMP_CLAUSE_REDUCTION_INIT (c)
13312 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13313 TREE_VEC_ELT (list, 3),
13314 decl_placeholder ? decl_placeholder
13315 : OMP_CLAUSE_DECL (c), placeholder);
13316 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13318 tree v = decl_placeholder ? decl_placeholder : t;
13319 OMP_CLAUSE_REDUCTION_INIT (c)
13320 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13321 OMP_CLAUSE_REDUCTION_INIT (c));
13323 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13324 c_find_omp_placeholder_r,
13325 placeholder, NULL))
13326 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13328 else
13330 tree init;
13331 tree v = decl_placeholder ? decl_placeholder : t;
13332 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13333 init = build_constructor (TREE_TYPE (v), NULL);
13334 else
13335 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13336 OMP_CLAUSE_REDUCTION_INIT (c)
13337 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13339 OMP_CLAUSE_REDUCTION_INIT (c)
13340 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13341 void_type_node, NULL_TREE,
13342 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13343 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13345 if (TREE_CODE (t) == MEM_REF)
13347 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13348 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13349 != INTEGER_CST)
13351 sorry ("variable length element type in array "
13352 "%<reduction%> clause");
13353 remove = true;
13354 break;
13356 t = TREE_OPERAND (t, 0);
13357 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13358 t = TREE_OPERAND (t, 0);
13359 if (TREE_CODE (t) == ADDR_EXPR)
13360 t = TREE_OPERAND (t, 0);
13362 goto check_dup_generic_t;
13364 case OMP_CLAUSE_COPYPRIVATE:
13365 copyprivate_seen = true;
13366 if (nowait_clause)
13368 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13369 "%<nowait%> clause must not be used together "
13370 "with %<copyprivate%>");
13371 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13372 nowait_clause = NULL;
13374 goto check_dup_generic;
13376 case OMP_CLAUSE_COPYIN:
13377 t = OMP_CLAUSE_DECL (c);
13378 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13380 error_at (OMP_CLAUSE_LOCATION (c),
13381 "%qE must be %<threadprivate%> for %<copyin%>", t);
13382 remove = true;
13383 break;
13385 goto check_dup_generic;
13387 case OMP_CLAUSE_LINEAR:
13388 if (ort != C_ORT_OMP_DECLARE_SIMD)
13389 need_implicitly_determined = true;
13390 t = OMP_CLAUSE_DECL (c);
13391 if (ort != C_ORT_OMP_DECLARE_SIMD
13392 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13394 error_at (OMP_CLAUSE_LOCATION (c),
13395 "modifier should not be specified in %<linear%> "
13396 "clause on %<simd%> or %<for%> constructs");
13397 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13399 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13400 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13402 error_at (OMP_CLAUSE_LOCATION (c),
13403 "linear clause applied to non-integral non-pointer "
13404 "variable with type %qT", TREE_TYPE (t));
13405 remove = true;
13406 break;
13408 if (TYPE_ATOMIC (TREE_TYPE (t)))
13410 error_at (OMP_CLAUSE_LOCATION (c),
13411 "%<_Atomic%> %qD in %<linear%> clause", t);
13412 remove = true;
13413 break;
13415 if (ort == C_ORT_OMP_DECLARE_SIMD)
13417 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13418 if (TREE_CODE (s) == PARM_DECL)
13420 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13421 /* map_head bitmap is used as uniform_head if
13422 declare_simd. */
13423 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13424 linear_variable_step_check = true;
13425 goto check_dup_generic;
13427 if (TREE_CODE (s) != INTEGER_CST)
13429 error_at (OMP_CLAUSE_LOCATION (c),
13430 "%<linear%> clause step %qE is neither constant "
13431 "nor a parameter", s);
13432 remove = true;
13433 break;
13436 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13438 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13439 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13440 OMP_CLAUSE_DECL (c), s);
13441 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13442 sizetype, fold_convert (sizetype, s),
13443 fold_convert
13444 (sizetype, OMP_CLAUSE_DECL (c)));
13445 if (s == error_mark_node)
13446 s = size_one_node;
13447 OMP_CLAUSE_LINEAR_STEP (c) = s;
13449 else
13450 OMP_CLAUSE_LINEAR_STEP (c)
13451 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13452 goto check_dup_generic;
13454 check_dup_generic:
13455 t = OMP_CLAUSE_DECL (c);
13456 check_dup_generic_t:
13457 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13459 error_at (OMP_CLAUSE_LOCATION (c),
13460 "%qE is not a variable in clause %qs", t,
13461 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13462 remove = true;
13464 else if (ort == C_ORT_ACC
13465 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13467 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13469 error ("%qD appears more than once in reduction clauses", t);
13470 remove = true;
13472 else
13473 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13475 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13476 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13477 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13479 error_at (OMP_CLAUSE_LOCATION (c),
13480 "%qE appears more than once in data clauses", t);
13481 remove = true;
13483 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13484 && bitmap_bit_p (&map_head, DECL_UID (t)))
13486 if (ort == C_ORT_ACC)
13487 error ("%qD appears more than once in data clauses", t);
13488 else
13489 error ("%qD appears both in data and map clauses", t);
13490 remove = true;
13492 else
13493 bitmap_set_bit (&generic_head, DECL_UID (t));
13494 break;
13496 case OMP_CLAUSE_FIRSTPRIVATE:
13497 t = OMP_CLAUSE_DECL (c);
13498 need_complete = true;
13499 need_implicitly_determined = true;
13500 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13502 error_at (OMP_CLAUSE_LOCATION (c),
13503 "%qE is not a variable in clause %<firstprivate%>", t);
13504 remove = true;
13506 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13507 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13509 error_at (OMP_CLAUSE_LOCATION (c),
13510 "%qE appears more than once in data clauses", t);
13511 remove = true;
13513 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13515 if (ort == C_ORT_ACC)
13516 error ("%qD appears more than once in data clauses", t);
13517 else
13518 error ("%qD appears both in data and map clauses", t);
13519 remove = true;
13521 else
13522 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13523 break;
13525 case OMP_CLAUSE_LASTPRIVATE:
13526 t = OMP_CLAUSE_DECL (c);
13527 need_complete = true;
13528 need_implicitly_determined = true;
13529 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13531 error_at (OMP_CLAUSE_LOCATION (c),
13532 "%qE is not a variable in clause %<lastprivate%>", t);
13533 remove = true;
13535 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13536 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13538 error_at (OMP_CLAUSE_LOCATION (c),
13539 "%qE appears more than once in data clauses", t);
13540 remove = true;
13542 else
13543 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13544 break;
13546 case OMP_CLAUSE_ALIGNED:
13547 t = OMP_CLAUSE_DECL (c);
13548 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13550 error_at (OMP_CLAUSE_LOCATION (c),
13551 "%qE is not a variable in %<aligned%> clause", t);
13552 remove = true;
13554 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13555 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13557 error_at (OMP_CLAUSE_LOCATION (c),
13558 "%qE in %<aligned%> clause is neither a pointer nor "
13559 "an array", t);
13560 remove = true;
13562 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13564 error_at (OMP_CLAUSE_LOCATION (c),
13565 "%<_Atomic%> %qD in %<aligned%> clause", t);
13566 remove = true;
13567 break;
13569 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13571 error_at (OMP_CLAUSE_LOCATION (c),
13572 "%qE appears more than once in %<aligned%> clauses",
13574 remove = true;
13576 else
13577 bitmap_set_bit (&aligned_head, DECL_UID (t));
13578 break;
13580 case OMP_CLAUSE_DEPEND:
13581 t = OMP_CLAUSE_DECL (c);
13582 if (t == NULL_TREE)
13584 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13585 == OMP_CLAUSE_DEPEND_SOURCE);
13586 break;
13588 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13590 gcc_assert (TREE_CODE (t) == TREE_LIST);
13591 for (; t; t = TREE_CHAIN (t))
13593 tree decl = TREE_VALUE (t);
13594 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13596 tree offset = TREE_PURPOSE (t);
13597 bool neg = wi::neg_p (wi::to_wide (offset));
13598 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13599 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13600 neg ? MINUS_EXPR : PLUS_EXPR,
13601 decl, offset);
13602 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13603 sizetype,
13604 fold_convert (sizetype, t2),
13605 fold_convert (sizetype, decl));
13606 if (t2 == error_mark_node)
13608 remove = true;
13609 break;
13611 TREE_PURPOSE (t) = t2;
13614 break;
13616 if (TREE_CODE (t) == TREE_LIST)
13618 if (handle_omp_array_sections (c, ort))
13619 remove = true;
13620 break;
13622 if (t == error_mark_node)
13623 remove = true;
13624 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13626 error_at (OMP_CLAUSE_LOCATION (c),
13627 "%qE is not a variable in %<depend%> clause", t);
13628 remove = true;
13630 else if (!c_mark_addressable (t))
13631 remove = true;
13632 break;
13634 case OMP_CLAUSE_MAP:
13635 case OMP_CLAUSE_TO:
13636 case OMP_CLAUSE_FROM:
13637 case OMP_CLAUSE__CACHE_:
13638 t = OMP_CLAUSE_DECL (c);
13639 if (TREE_CODE (t) == TREE_LIST)
13641 if (handle_omp_array_sections (c, ort))
13642 remove = true;
13643 else
13645 t = OMP_CLAUSE_DECL (c);
13646 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13648 error_at (OMP_CLAUSE_LOCATION (c),
13649 "array section does not have mappable type "
13650 "in %qs clause",
13651 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13652 remove = true;
13654 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13656 error_at (OMP_CLAUSE_LOCATION (c),
13657 "%<_Atomic%> %qE in %qs clause", t,
13658 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13659 remove = true;
13661 while (TREE_CODE (t) == ARRAY_REF)
13662 t = TREE_OPERAND (t, 0);
13663 if (TREE_CODE (t) == COMPONENT_REF
13664 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13666 while (TREE_CODE (t) == COMPONENT_REF)
13667 t = TREE_OPERAND (t, 0);
13668 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13669 break;
13670 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13672 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13673 error ("%qD appears more than once in motion"
13674 " clauses", t);
13675 else if (ort == C_ORT_ACC)
13676 error ("%qD appears more than once in data"
13677 " clauses", t);
13678 else
13679 error ("%qD appears more than once in map"
13680 " clauses", t);
13681 remove = true;
13683 else
13685 bitmap_set_bit (&map_head, DECL_UID (t));
13686 bitmap_set_bit (&map_field_head, DECL_UID (t));
13690 break;
13692 if (t == error_mark_node)
13694 remove = true;
13695 break;
13697 if (TREE_CODE (t) == COMPONENT_REF
13698 && (ort & C_ORT_OMP)
13699 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13701 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13703 error_at (OMP_CLAUSE_LOCATION (c),
13704 "bit-field %qE in %qs clause",
13705 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13706 remove = true;
13708 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13710 error_at (OMP_CLAUSE_LOCATION (c),
13711 "%qE does not have a mappable type in %qs clause",
13712 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13713 remove = true;
13715 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13717 error_at (OMP_CLAUSE_LOCATION (c),
13718 "%<_Atomic%> %qE in %qs clause", t,
13719 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13720 remove = true;
13722 while (TREE_CODE (t) == COMPONENT_REF)
13724 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13725 == UNION_TYPE)
13727 error_at (OMP_CLAUSE_LOCATION (c),
13728 "%qE is a member of a union", t);
13729 remove = true;
13730 break;
13732 t = TREE_OPERAND (t, 0);
13734 if (remove)
13735 break;
13736 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13738 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13739 break;
13742 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13744 error_at (OMP_CLAUSE_LOCATION (c),
13745 "%qE is not a variable in %qs clause", t,
13746 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13747 remove = true;
13749 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13751 error_at (OMP_CLAUSE_LOCATION (c),
13752 "%qD is threadprivate variable in %qs clause", t,
13753 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13754 remove = true;
13756 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13757 || (OMP_CLAUSE_MAP_KIND (c)
13758 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13759 && !c_mark_addressable (t))
13760 remove = true;
13761 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13762 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13763 || (OMP_CLAUSE_MAP_KIND (c)
13764 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13765 || (OMP_CLAUSE_MAP_KIND (c)
13766 == GOMP_MAP_FORCE_DEVICEPTR)))
13767 && t == OMP_CLAUSE_DECL (c)
13768 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13770 error_at (OMP_CLAUSE_LOCATION (c),
13771 "%qD does not have a mappable type in %qs clause", t,
13772 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13773 remove = true;
13775 else if (TREE_TYPE (t) == error_mark_node)
13776 remove = true;
13777 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13779 error_at (OMP_CLAUSE_LOCATION (c),
13780 "%<_Atomic%> %qE in %qs clause", t,
13781 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13782 remove = true;
13784 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13785 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13787 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13788 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13790 error ("%qD appears more than once in data clauses", t);
13791 remove = true;
13793 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13795 if (ort == C_ORT_ACC)
13796 error ("%qD appears more than once in data clauses", t);
13797 else
13798 error ("%qD appears both in data and map clauses", t);
13799 remove = true;
13801 else
13802 bitmap_set_bit (&generic_head, DECL_UID (t));
13804 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13806 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13807 error ("%qD appears more than once in motion clauses", t);
13808 else if (ort == C_ORT_ACC)
13809 error ("%qD appears more than once in data clauses", t);
13810 else
13811 error ("%qD appears more than once in map clauses", t);
13812 remove = true;
13814 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13815 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13817 if (ort == C_ORT_ACC)
13818 error ("%qD appears more than once in data clauses", t);
13819 else
13820 error ("%qD appears both in data and map clauses", t);
13821 remove = true;
13823 else
13825 bitmap_set_bit (&map_head, DECL_UID (t));
13826 if (t != OMP_CLAUSE_DECL (c)
13827 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13828 bitmap_set_bit (&map_field_head, DECL_UID (t));
13830 break;
13832 case OMP_CLAUSE_TO_DECLARE:
13833 case OMP_CLAUSE_LINK:
13834 t = OMP_CLAUSE_DECL (c);
13835 if (TREE_CODE (t) == FUNCTION_DECL
13836 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13838 else if (!VAR_P (t))
13840 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13841 error_at (OMP_CLAUSE_LOCATION (c),
13842 "%qE is neither a variable nor a function name in "
13843 "clause %qs", t,
13844 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13845 else
13846 error_at (OMP_CLAUSE_LOCATION (c),
13847 "%qE is not a variable in clause %qs", t,
13848 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13849 remove = true;
13851 else if (DECL_THREAD_LOCAL_P (t))
13853 error_at (OMP_CLAUSE_LOCATION (c),
13854 "%qD is threadprivate variable in %qs clause", t,
13855 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13856 remove = true;
13858 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13860 error_at (OMP_CLAUSE_LOCATION (c),
13861 "%qD does not have a mappable type in %qs clause", t,
13862 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13863 remove = true;
13865 if (remove)
13866 break;
13867 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13869 error_at (OMP_CLAUSE_LOCATION (c),
13870 "%qE appears more than once on the same "
13871 "%<declare target%> directive", t);
13872 remove = true;
13874 else
13875 bitmap_set_bit (&generic_head, DECL_UID (t));
13876 break;
13878 case OMP_CLAUSE_UNIFORM:
13879 t = OMP_CLAUSE_DECL (c);
13880 if (TREE_CODE (t) != PARM_DECL)
13882 if (DECL_P (t))
13883 error_at (OMP_CLAUSE_LOCATION (c),
13884 "%qD is not an argument in %<uniform%> clause", t);
13885 else
13886 error_at (OMP_CLAUSE_LOCATION (c),
13887 "%qE is not an argument in %<uniform%> clause", t);
13888 remove = true;
13889 break;
13891 /* map_head bitmap is used as uniform_head if declare_simd. */
13892 bitmap_set_bit (&map_head, DECL_UID (t));
13893 goto check_dup_generic;
13895 case OMP_CLAUSE_IS_DEVICE_PTR:
13896 case OMP_CLAUSE_USE_DEVICE_PTR:
13897 t = OMP_CLAUSE_DECL (c);
13898 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13899 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13901 error_at (OMP_CLAUSE_LOCATION (c),
13902 "%qs variable is neither a pointer nor an array",
13903 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13904 remove = true;
13906 goto check_dup_generic;
13908 case OMP_CLAUSE_NOWAIT:
13909 if (copyprivate_seen)
13911 error_at (OMP_CLAUSE_LOCATION (c),
13912 "%<nowait%> clause must not be used together "
13913 "with %<copyprivate%>");
13914 remove = true;
13915 break;
13917 nowait_clause = pc;
13918 pc = &OMP_CLAUSE_CHAIN (c);
13919 continue;
13921 case OMP_CLAUSE_IF:
13922 case OMP_CLAUSE_NUM_THREADS:
13923 case OMP_CLAUSE_NUM_TEAMS:
13924 case OMP_CLAUSE_THREAD_LIMIT:
13925 case OMP_CLAUSE_DEFAULT:
13926 case OMP_CLAUSE_UNTIED:
13927 case OMP_CLAUSE_COLLAPSE:
13928 case OMP_CLAUSE_FINAL:
13929 case OMP_CLAUSE_MERGEABLE:
13930 case OMP_CLAUSE_DEVICE:
13931 case OMP_CLAUSE_DIST_SCHEDULE:
13932 case OMP_CLAUSE_PARALLEL:
13933 case OMP_CLAUSE_FOR:
13934 case OMP_CLAUSE_SECTIONS:
13935 case OMP_CLAUSE_TASKGROUP:
13936 case OMP_CLAUSE_PROC_BIND:
13937 case OMP_CLAUSE_PRIORITY:
13938 case OMP_CLAUSE_GRAINSIZE:
13939 case OMP_CLAUSE_NUM_TASKS:
13940 case OMP_CLAUSE_NOGROUP:
13941 case OMP_CLAUSE_THREADS:
13942 case OMP_CLAUSE_SIMD:
13943 case OMP_CLAUSE_HINT:
13944 case OMP_CLAUSE_DEFAULTMAP:
13945 case OMP_CLAUSE_NUM_GANGS:
13946 case OMP_CLAUSE_NUM_WORKERS:
13947 case OMP_CLAUSE_VECTOR_LENGTH:
13948 case OMP_CLAUSE_ASYNC:
13949 case OMP_CLAUSE_WAIT:
13950 case OMP_CLAUSE_AUTO:
13951 case OMP_CLAUSE_INDEPENDENT:
13952 case OMP_CLAUSE_SEQ:
13953 case OMP_CLAUSE_GANG:
13954 case OMP_CLAUSE_WORKER:
13955 case OMP_CLAUSE_VECTOR:
13956 case OMP_CLAUSE_TILE:
13957 case OMP_CLAUSE_IF_PRESENT:
13958 case OMP_CLAUSE_FINALIZE:
13959 pc = &OMP_CLAUSE_CHAIN (c);
13960 continue;
13962 case OMP_CLAUSE_SCHEDULE:
13963 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13965 const char *p = NULL;
13966 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13968 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13969 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13970 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13971 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13972 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13973 default: gcc_unreachable ();
13975 if (p)
13977 error_at (OMP_CLAUSE_LOCATION (c),
13978 "%<nonmonotonic%> modifier specified for %qs "
13979 "schedule kind", p);
13980 OMP_CLAUSE_SCHEDULE_KIND (c)
13981 = (enum omp_clause_schedule_kind)
13982 (OMP_CLAUSE_SCHEDULE_KIND (c)
13983 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13986 schedule_clause = c;
13987 pc = &OMP_CLAUSE_CHAIN (c);
13988 continue;
13990 case OMP_CLAUSE_ORDERED:
13991 ordered_seen = true;
13992 pc = &OMP_CLAUSE_CHAIN (c);
13993 continue;
13995 case OMP_CLAUSE_SAFELEN:
13996 safelen = c;
13997 pc = &OMP_CLAUSE_CHAIN (c);
13998 continue;
13999 case OMP_CLAUSE_SIMDLEN:
14000 simdlen = c;
14001 pc = &OMP_CLAUSE_CHAIN (c);
14002 continue;
14004 case OMP_CLAUSE_INBRANCH:
14005 case OMP_CLAUSE_NOTINBRANCH:
14006 if (branch_seen)
14008 error_at (OMP_CLAUSE_LOCATION (c),
14009 "%<inbranch%> clause is incompatible with "
14010 "%<notinbranch%>");
14011 remove = true;
14012 break;
14014 branch_seen = true;
14015 pc = &OMP_CLAUSE_CHAIN (c);
14016 continue;
14018 default:
14019 gcc_unreachable ();
14022 if (!remove)
14024 t = OMP_CLAUSE_DECL (c);
14026 if (need_complete)
14028 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14029 if (t == error_mark_node)
14030 remove = true;
14033 if (need_implicitly_determined)
14035 const char *share_name = NULL;
14037 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14038 share_name = "threadprivate";
14039 else switch (c_omp_predetermined_sharing (t))
14041 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
14042 break;
14043 case OMP_CLAUSE_DEFAULT_SHARED:
14044 /* const vars may be specified in firstprivate clause. */
14045 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
14046 && TREE_READONLY (t))
14047 break;
14048 share_name = "shared";
14049 break;
14050 case OMP_CLAUSE_DEFAULT_PRIVATE:
14051 share_name = "private";
14052 break;
14053 default:
14054 gcc_unreachable ();
14056 if (share_name)
14058 error_at (OMP_CLAUSE_LOCATION (c),
14059 "%qE is predetermined %qs for %qs",
14060 t, share_name,
14061 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14062 remove = true;
14067 if (remove)
14068 *pc = OMP_CLAUSE_CHAIN (c);
14069 else
14070 pc = &OMP_CLAUSE_CHAIN (c);
14073 if (simdlen
14074 && safelen
14075 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
14076 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
14078 error_at (OMP_CLAUSE_LOCATION (simdlen),
14079 "%<simdlen%> clause value is bigger than "
14080 "%<safelen%> clause value");
14081 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
14082 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
14085 if (ordered_seen
14086 && schedule_clause
14087 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14088 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
14090 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
14091 "%<nonmonotonic%> schedule modifier specified together "
14092 "with %<ordered%> clause");
14093 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14094 = (enum omp_clause_schedule_kind)
14095 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14096 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
14099 if (linear_variable_step_check)
14100 for (pc = &clauses, c = clauses; c ; c = *pc)
14102 bool remove = false;
14103 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14104 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
14105 && !bitmap_bit_p (&map_head,
14106 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
14108 error_at (OMP_CLAUSE_LOCATION (c),
14109 "%<linear%> clause step is a parameter %qD not "
14110 "specified in %<uniform%> clause",
14111 OMP_CLAUSE_LINEAR_STEP (c));
14112 remove = true;
14115 if (remove)
14116 *pc = OMP_CLAUSE_CHAIN (c);
14117 else
14118 pc = &OMP_CLAUSE_CHAIN (c);
14121 bitmap_obstack_release (NULL);
14122 return clauses;
14125 /* Return code to initialize DST with a copy constructor from SRC.
14126 C doesn't have copy constructors nor assignment operators, only for
14127 _Atomic vars we need to perform __atomic_load from src into a temporary
14128 followed by __atomic_store of the temporary to dst. */
14130 tree
14131 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
14133 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
14134 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
14136 location_t loc = OMP_CLAUSE_LOCATION (clause);
14137 tree type = TREE_TYPE (dst);
14138 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
14139 tree tmp = create_tmp_var (nonatomic_type);
14140 tree tmp_addr = build_fold_addr_expr (tmp);
14141 TREE_ADDRESSABLE (tmp) = 1;
14142 TREE_NO_WARNING (tmp) = 1;
14143 tree src_addr = build_fold_addr_expr (src);
14144 tree dst_addr = build_fold_addr_expr (dst);
14145 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
14146 vec<tree, va_gc> *params;
14147 /* Expansion of a generic atomic load may require an addition
14148 element, so allocate enough to prevent a resize. */
14149 vec_alloc (params, 4);
14151 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14152 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
14153 params->quick_push (src_addr);
14154 params->quick_push (tmp_addr);
14155 params->quick_push (seq_cst);
14156 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14158 vec_alloc (params, 4);
14160 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14161 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14162 params->quick_push (dst_addr);
14163 params->quick_push (tmp_addr);
14164 params->quick_push (seq_cst);
14165 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14166 return build2 (COMPOUND_EXPR, void_type_node, load, store);
14169 /* Create a transaction node. */
14171 tree
14172 c_finish_transaction (location_t loc, tree block, int flags)
14174 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14175 if (flags & TM_STMT_ATTR_OUTER)
14176 TRANSACTION_EXPR_OUTER (stmt) = 1;
14177 if (flags & TM_STMT_ATTR_RELAXED)
14178 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14179 return add_stmt (stmt);
14182 /* Make a variant type in the proper way for C/C++, propagating qualifiers
14183 down to the element type of an array. If ORIG_QUAL_TYPE is not
14184 NULL, then it should be used as the qualified type
14185 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14186 preserve information about the typedef name from which an array
14187 type was derived). */
14189 tree
14190 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14191 size_t orig_qual_indirect)
14193 if (type == error_mark_node)
14194 return type;
14196 if (TREE_CODE (type) == ARRAY_TYPE)
14198 tree t;
14199 tree element_type = c_build_qualified_type (TREE_TYPE (type),
14200 type_quals, orig_qual_type,
14201 orig_qual_indirect - 1);
14203 /* See if we already have an identically qualified type. */
14204 if (orig_qual_type && orig_qual_indirect == 0)
14205 t = orig_qual_type;
14206 else
14207 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14209 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14210 && TYPE_NAME (t) == TYPE_NAME (type)
14211 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14212 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14213 TYPE_ATTRIBUTES (type)))
14214 break;
14216 if (!t)
14218 tree domain = TYPE_DOMAIN (type);
14220 t = build_variant_type_copy (type);
14221 TREE_TYPE (t) = element_type;
14223 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14224 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14225 SET_TYPE_STRUCTURAL_EQUALITY (t);
14226 else if (TYPE_CANONICAL (element_type) != element_type
14227 || (domain && TYPE_CANONICAL (domain) != domain))
14229 tree unqualified_canon
14230 = build_array_type (TYPE_CANONICAL (element_type),
14231 domain? TYPE_CANONICAL (domain)
14232 : NULL_TREE);
14233 if (TYPE_REVERSE_STORAGE_ORDER (type))
14235 unqualified_canon
14236 = build_distinct_type_copy (unqualified_canon);
14237 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14239 TYPE_CANONICAL (t)
14240 = c_build_qualified_type (unqualified_canon, type_quals);
14242 else
14243 TYPE_CANONICAL (t) = t;
14245 return t;
14248 /* A restrict-qualified pointer type must be a pointer to object or
14249 incomplete type. Note that the use of POINTER_TYPE_P also allows
14250 REFERENCE_TYPEs, which is appropriate for C++. */
14251 if ((type_quals & TYPE_QUAL_RESTRICT)
14252 && (!POINTER_TYPE_P (type)
14253 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14255 error ("invalid use of %<restrict%>");
14256 type_quals &= ~TYPE_QUAL_RESTRICT;
14259 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14260 ? orig_qual_type
14261 : build_qualified_type (type, type_quals));
14262 /* A variant type does not inherit the list of incomplete vars from the
14263 type main variant. */
14264 if (RECORD_OR_UNION_TYPE_P (var_type)
14265 && TYPE_MAIN_VARIANT (var_type) != var_type)
14266 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14267 return var_type;
14270 /* Build a VA_ARG_EXPR for the C parser. */
14272 tree
14273 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
14275 if (error_operand_p (type))
14276 return error_mark_node;
14277 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14278 order because it takes the address of the expression. */
14279 else if (handled_component_p (expr)
14280 && reverse_storage_order_for_component_p (expr))
14282 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14283 return error_mark_node;
14285 else if (!COMPLETE_TYPE_P (type))
14287 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
14288 "type %qT", type);
14289 return error_mark_node;
14291 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14292 warning_at (loc2, OPT_Wc___compat,
14293 "C++ requires promoted type, not enum type, in %<va_arg%>");
14294 return build_va_arg (loc2, expr, type);
14297 /* Return truthvalue of whether T1 is the same tree structure as T2.
14298 Return 1 if they are the same. Return false if they are different. */
14300 bool
14301 c_tree_equal (tree t1, tree t2)
14303 enum tree_code code1, code2;
14305 if (t1 == t2)
14306 return true;
14307 if (!t1 || !t2)
14308 return false;
14310 for (code1 = TREE_CODE (t1);
14311 CONVERT_EXPR_CODE_P (code1)
14312 || code1 == NON_LVALUE_EXPR;
14313 code1 = TREE_CODE (t1))
14314 t1 = TREE_OPERAND (t1, 0);
14315 for (code2 = TREE_CODE (t2);
14316 CONVERT_EXPR_CODE_P (code2)
14317 || code2 == NON_LVALUE_EXPR;
14318 code2 = TREE_CODE (t2))
14319 t2 = TREE_OPERAND (t2, 0);
14321 /* They might have become equal now. */
14322 if (t1 == t2)
14323 return true;
14325 if (code1 != code2)
14326 return false;
14328 switch (code1)
14330 case INTEGER_CST:
14331 return wi::to_wide (t1) == wi::to_wide (t2);
14333 case REAL_CST:
14334 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14336 case STRING_CST:
14337 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14338 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14339 TREE_STRING_LENGTH (t1));
14341 case FIXED_CST:
14342 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14343 TREE_FIXED_CST (t2));
14345 case COMPLEX_CST:
14346 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14347 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14349 case VECTOR_CST:
14350 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14352 case CONSTRUCTOR:
14353 /* We need to do this when determining whether or not two
14354 non-type pointer to member function template arguments
14355 are the same. */
14356 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14357 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14358 return false;
14360 tree field, value;
14361 unsigned int i;
14362 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14364 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14365 if (!c_tree_equal (field, elt2->index)
14366 || !c_tree_equal (value, elt2->value))
14367 return false;
14370 return true;
14372 case TREE_LIST:
14373 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14374 return false;
14375 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14376 return false;
14377 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14379 case SAVE_EXPR:
14380 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14382 case CALL_EXPR:
14384 tree arg1, arg2;
14385 call_expr_arg_iterator iter1, iter2;
14386 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14387 return false;
14388 for (arg1 = first_call_expr_arg (t1, &iter1),
14389 arg2 = first_call_expr_arg (t2, &iter2);
14390 arg1 && arg2;
14391 arg1 = next_call_expr_arg (&iter1),
14392 arg2 = next_call_expr_arg (&iter2))
14393 if (!c_tree_equal (arg1, arg2))
14394 return false;
14395 if (arg1 || arg2)
14396 return false;
14397 return true;
14400 case TARGET_EXPR:
14402 tree o1 = TREE_OPERAND (t1, 0);
14403 tree o2 = TREE_OPERAND (t2, 0);
14405 /* Special case: if either target is an unallocated VAR_DECL,
14406 it means that it's going to be unified with whatever the
14407 TARGET_EXPR is really supposed to initialize, so treat it
14408 as being equivalent to anything. */
14409 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14410 && !DECL_RTL_SET_P (o1))
14411 /*Nop*/;
14412 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14413 && !DECL_RTL_SET_P (o2))
14414 /*Nop*/;
14415 else if (!c_tree_equal (o1, o2))
14416 return false;
14418 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14421 case COMPONENT_REF:
14422 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14423 return false;
14424 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14426 case PARM_DECL:
14427 case VAR_DECL:
14428 case CONST_DECL:
14429 case FIELD_DECL:
14430 case FUNCTION_DECL:
14431 case IDENTIFIER_NODE:
14432 case SSA_NAME:
14433 return false;
14435 case TREE_VEC:
14437 unsigned ix;
14438 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14439 return false;
14440 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14441 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14442 TREE_VEC_ELT (t2, ix)))
14443 return false;
14444 return true;
14447 default:
14448 break;
14451 switch (TREE_CODE_CLASS (code1))
14453 case tcc_unary:
14454 case tcc_binary:
14455 case tcc_comparison:
14456 case tcc_expression:
14457 case tcc_vl_exp:
14458 case tcc_reference:
14459 case tcc_statement:
14461 int i, n = TREE_OPERAND_LENGTH (t1);
14463 switch (code1)
14465 case PREINCREMENT_EXPR:
14466 case PREDECREMENT_EXPR:
14467 case POSTINCREMENT_EXPR:
14468 case POSTDECREMENT_EXPR:
14469 n = 1;
14470 break;
14471 case ARRAY_REF:
14472 n = 2;
14473 break;
14474 default:
14475 break;
14478 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14479 && n != TREE_OPERAND_LENGTH (t2))
14480 return false;
14482 for (i = 0; i < n; ++i)
14483 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14484 return false;
14486 return true;
14489 case tcc_type:
14490 return comptypes (t1, t2);
14491 default:
14492 gcc_unreachable ();
14494 /* We can get here with --disable-checking. */
14495 return false;
14498 /* Returns true when the function declaration FNDECL is implicit,
14499 introduced as a result of a call to an otherwise undeclared
14500 function, and false otherwise. */
14502 bool
14503 c_decl_implicit (const_tree fndecl)
14505 return C_DECL_IMPLICIT (fndecl);