2016-05-04 Thomas Preud'homme <thomas.preudhomme@arm.com>
[official-gcc.git] / gcc / c / c-typeck.c
blob204702e505e17fc2555e3e4c8b6d910a09e7da24
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2016 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 "target.h"
30 #include "function.h"
31 #include "bitmap.h"
32 #include "c-tree.h"
33 #include "gimple-expr.h"
34 #include "predict.h"
35 #include "stor-layout.h"
36 #include "trans-mem.h"
37 #include "varasm.h"
38 #include "stmt.h"
39 #include "langhooks.h"
40 #include "c-lang.h"
41 #include "intl.h"
42 #include "tree-iterator.h"
43 #include "gimplify.h"
44 #include "tree-inline.h"
45 #include "omp-low.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-ubsan.h"
48 #include "cilk.h"
49 #include "gomp-constants.h"
50 #include "spellcheck.h"
51 #include "gcc-rich-location.h"
53 /* Possible cases of implicit bad conversions. Used to select
54 diagnostic messages in convert_for_assignment. */
55 enum impl_conv {
56 ic_argpass,
57 ic_assign,
58 ic_init,
59 ic_return
62 /* The level of nesting inside "__alignof__". */
63 int in_alignof;
65 /* The level of nesting inside "sizeof". */
66 int in_sizeof;
68 /* The level of nesting inside "typeof". */
69 int in_typeof;
71 /* The argument of last parsed sizeof expression, only to be tested
72 if expr.original_code == SIZEOF_EXPR. */
73 tree c_last_sizeof_arg;
75 /* Nonzero if we might need to print a "missing braces around
76 initializer" message within this initializer. */
77 static int found_missing_braces;
79 static int require_constant_value;
80 static int require_constant_elements;
82 static bool null_pointer_constant_p (const_tree);
83 static tree qualify_type (tree, tree);
84 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
85 bool *);
86 static int comp_target_types (location_t, tree, tree);
87 static int function_types_compatible_p (const_tree, const_tree, bool *,
88 bool *);
89 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
90 static tree lookup_field (tree, tree);
91 static int convert_arguments (location_t, vec<location_t>, tree,
92 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
93 tree);
94 static tree pointer_diff (location_t, tree, tree);
95 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
96 enum impl_conv, bool, tree, tree, int);
97 static tree valid_compound_expr_initializer (tree, tree);
98 static void push_string (const char *);
99 static void push_member_name (tree);
100 static int spelling_length (void);
101 static char *print_spelling (char *);
102 static void warning_init (location_t, int, const char *);
103 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
104 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
105 bool, struct obstack *);
106 static void output_pending_init_elements (int, struct obstack *);
107 static int set_designator (location_t, int, struct obstack *);
108 static void push_range_stack (tree, struct obstack *);
109 static void add_pending_init (location_t, tree, tree, tree, bool,
110 struct obstack *);
111 static void set_nonincremental_init (struct obstack *);
112 static void set_nonincremental_init_from_string (tree, struct obstack *);
113 static tree find_init_member (tree, struct obstack *);
114 static void readonly_warning (tree, enum lvalue_use);
115 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
116 static void record_maybe_used_decl (tree);
117 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
119 /* Return true if EXP is a null pointer constant, false otherwise. */
121 static bool
122 null_pointer_constant_p (const_tree expr)
124 /* This should really operate on c_expr structures, but they aren't
125 yet available everywhere required. */
126 tree type = TREE_TYPE (expr);
127 return (TREE_CODE (expr) == INTEGER_CST
128 && !TREE_OVERFLOW (expr)
129 && integer_zerop (expr)
130 && (INTEGRAL_TYPE_P (type)
131 || (TREE_CODE (type) == POINTER_TYPE
132 && VOID_TYPE_P (TREE_TYPE (type))
133 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
136 /* EXPR may appear in an unevaluated part of an integer constant
137 expression, but not in an evaluated part. Wrap it in a
138 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
139 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
141 static tree
142 note_integer_operands (tree expr)
144 tree ret;
145 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
147 ret = copy_node (expr);
148 TREE_OVERFLOW (ret) = 1;
150 else
152 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
153 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
155 return ret;
158 /* Having checked whether EXPR may appear in an unevaluated part of an
159 integer constant expression and found that it may, remove any
160 C_MAYBE_CONST_EXPR noting this fact and return the resulting
161 expression. */
163 static inline tree
164 remove_c_maybe_const_expr (tree expr)
166 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
167 return C_MAYBE_CONST_EXPR_EXPR (expr);
168 else
169 return expr;
172 \f/* This is a cache to hold if two types are compatible or not. */
174 struct tagged_tu_seen_cache {
175 const struct tagged_tu_seen_cache * next;
176 const_tree t1;
177 const_tree t2;
178 /* The return value of tagged_types_tu_compatible_p if we had seen
179 these two types already. */
180 int val;
183 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
184 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
186 /* Do `exp = require_complete_type (exp);' to make sure exp
187 does not have an incomplete type. (That includes void types.) */
189 tree
190 require_complete_type (tree value)
192 tree type = TREE_TYPE (value);
194 if (error_operand_p (value))
195 return error_mark_node;
197 /* First, detect a valid value with a complete type. */
198 if (COMPLETE_TYPE_P (type))
199 return value;
201 c_incomplete_type_error (value, type);
202 return error_mark_node;
205 /* Print an error message for invalid use of an incomplete type.
206 VALUE is the expression that was used (or 0 if that isn't known)
207 and TYPE is the type that was invalid. */
209 void
210 c_incomplete_type_error (const_tree value, const_tree type)
212 /* Avoid duplicate error message. */
213 if (TREE_CODE (type) == ERROR_MARK)
214 return;
216 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
217 error ("%qD has an incomplete type %qT", value, type);
218 else
220 retry:
221 /* We must print an error message. Be clever about what it says. */
223 switch (TREE_CODE (type))
225 case RECORD_TYPE:
226 case UNION_TYPE:
227 case ENUMERAL_TYPE:
228 break;
230 case VOID_TYPE:
231 error ("invalid use of void expression");
232 return;
234 case ARRAY_TYPE:
235 if (TYPE_DOMAIN (type))
237 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
239 error ("invalid use of flexible array member");
240 return;
242 type = TREE_TYPE (type);
243 goto retry;
245 error ("invalid use of array with unspecified bounds");
246 return;
248 default:
249 gcc_unreachable ();
252 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
253 error ("invalid use of undefined type %qT", type);
254 else
255 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
256 error ("invalid use of incomplete typedef %qT", type);
260 /* Given a type, apply default promotions wrt unnamed function
261 arguments and return the new type. */
263 tree
264 c_type_promotes_to (tree type)
266 tree ret = NULL_TREE;
268 if (TYPE_MAIN_VARIANT (type) == float_type_node)
269 ret = double_type_node;
270 else if (c_promoting_integer_type_p (type))
272 /* Preserve unsignedness if not really getting any wider. */
273 if (TYPE_UNSIGNED (type)
274 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
275 ret = unsigned_type_node;
276 else
277 ret = integer_type_node;
280 if (ret != NULL_TREE)
281 return (TYPE_ATOMIC (type)
282 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
283 : ret);
285 return type;
288 /* Return true if between two named address spaces, whether there is a superset
289 named address space that encompasses both address spaces. If there is a
290 superset, return which address space is the superset. */
292 static bool
293 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
295 if (as1 == as2)
297 *common = as1;
298 return true;
300 else if (targetm.addr_space.subset_p (as1, as2))
302 *common = as2;
303 return true;
305 else if (targetm.addr_space.subset_p (as2, as1))
307 *common = as1;
308 return true;
310 else
311 return false;
314 /* Return a variant of TYPE which has all the type qualifiers of LIKE
315 as well as those of TYPE. */
317 static tree
318 qualify_type (tree type, tree like)
320 addr_space_t as_type = TYPE_ADDR_SPACE (type);
321 addr_space_t as_like = TYPE_ADDR_SPACE (like);
322 addr_space_t as_common;
324 /* If the two named address spaces are different, determine the common
325 superset address space. If there isn't one, raise an error. */
326 if (!addr_space_superset (as_type, as_like, &as_common))
328 as_common = as_type;
329 error ("%qT and %qT are in disjoint named address spaces",
330 type, like);
333 return c_build_qualified_type (type,
334 TYPE_QUALS_NO_ADDR_SPACE (type)
335 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
336 | ENCODE_QUAL_ADDR_SPACE (as_common));
339 /* Return true iff the given tree T is a variable length array. */
341 bool
342 c_vla_type_p (const_tree t)
344 if (TREE_CODE (t) == ARRAY_TYPE
345 && C_TYPE_VARIABLE_SIZE (t))
346 return true;
347 return false;
350 /* Return the composite type of two compatible types.
352 We assume that comptypes has already been done and returned
353 nonzero; if that isn't so, this may crash. In particular, we
354 assume that qualifiers match. */
356 tree
357 composite_type (tree t1, tree t2)
359 enum tree_code code1;
360 enum tree_code code2;
361 tree attributes;
363 /* Save time if the two types are the same. */
365 if (t1 == t2) return t1;
367 /* If one type is nonsense, use the other. */
368 if (t1 == error_mark_node)
369 return t2;
370 if (t2 == error_mark_node)
371 return t1;
373 code1 = TREE_CODE (t1);
374 code2 = TREE_CODE (t2);
376 /* Merge the attributes. */
377 attributes = targetm.merge_type_attributes (t1, t2);
379 /* If one is an enumerated type and the other is the compatible
380 integer type, the composite type might be either of the two
381 (DR#013 question 3). For consistency, use the enumerated type as
382 the composite type. */
384 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
385 return t1;
386 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
387 return t2;
389 gcc_assert (code1 == code2);
391 switch (code1)
393 case POINTER_TYPE:
394 /* For two pointers, do this recursively on the target type. */
396 tree pointed_to_1 = TREE_TYPE (t1);
397 tree pointed_to_2 = TREE_TYPE (t2);
398 tree target = composite_type (pointed_to_1, pointed_to_2);
399 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
400 t1 = build_type_attribute_variant (t1, attributes);
401 return qualify_type (t1, t2);
404 case ARRAY_TYPE:
406 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
407 int quals;
408 tree unqual_elt;
409 tree d1 = TYPE_DOMAIN (t1);
410 tree d2 = TYPE_DOMAIN (t2);
411 bool d1_variable, d2_variable;
412 bool d1_zero, d2_zero;
413 bool t1_complete, t2_complete;
415 /* We should not have any type quals on arrays at all. */
416 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
417 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
419 t1_complete = COMPLETE_TYPE_P (t1);
420 t2_complete = COMPLETE_TYPE_P (t2);
422 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
423 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
425 d1_variable = (!d1_zero
426 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
427 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
428 d2_variable = (!d2_zero
429 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
430 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
431 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
432 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
434 /* Save space: see if the result is identical to one of the args. */
435 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
436 && (d2_variable || d2_zero || !d1_variable))
437 return build_type_attribute_variant (t1, attributes);
438 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
439 && (d1_variable || d1_zero || !d2_variable))
440 return build_type_attribute_variant (t2, attributes);
442 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443 return build_type_attribute_variant (t1, attributes);
444 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
445 return build_type_attribute_variant (t2, attributes);
447 /* Merge the element types, and have a size if either arg has
448 one. We may have qualifiers on the element types. To set
449 up TYPE_MAIN_VARIANT correctly, we need to form the
450 composite of the unqualified types and add the qualifiers
451 back at the end. */
452 quals = TYPE_QUALS (strip_array_types (elt));
453 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
454 t1 = build_array_type (unqual_elt,
455 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
456 && (d2_variable
457 || d2_zero
458 || !d1_variable))
459 ? t1
460 : t2));
461 /* Ensure a composite type involving a zero-length array type
462 is a zero-length type not an incomplete type. */
463 if (d1_zero && d2_zero
464 && (t1_complete || t2_complete)
465 && !COMPLETE_TYPE_P (t1))
467 TYPE_SIZE (t1) = bitsize_zero_node;
468 TYPE_SIZE_UNIT (t1) = size_zero_node;
470 t1 = c_build_qualified_type (t1, quals);
471 return build_type_attribute_variant (t1, attributes);
474 case ENUMERAL_TYPE:
475 case RECORD_TYPE:
476 case UNION_TYPE:
477 if (attributes != NULL)
479 /* Try harder not to create a new aggregate type. */
480 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
481 return t1;
482 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
483 return t2;
485 return build_type_attribute_variant (t1, attributes);
487 case FUNCTION_TYPE:
488 /* Function types: prefer the one that specified arg types.
489 If both do, merge the arg types. Also merge the return types. */
491 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
492 tree p1 = TYPE_ARG_TYPES (t1);
493 tree p2 = TYPE_ARG_TYPES (t2);
494 int len;
495 tree newargs, n;
496 int i;
498 /* Save space: see if the result is identical to one of the args. */
499 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
500 return build_type_attribute_variant (t1, attributes);
501 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
502 return build_type_attribute_variant (t2, attributes);
504 /* Simple way if one arg fails to specify argument types. */
505 if (TYPE_ARG_TYPES (t1) == 0)
507 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
508 t1 = build_type_attribute_variant (t1, attributes);
509 return qualify_type (t1, t2);
511 if (TYPE_ARG_TYPES (t2) == 0)
513 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
514 t1 = build_type_attribute_variant (t1, attributes);
515 return qualify_type (t1, t2);
518 /* If both args specify argument types, we must merge the two
519 lists, argument by argument. */
521 for (len = 0, newargs = p1;
522 newargs && newargs != void_list_node;
523 len++, newargs = TREE_CHAIN (newargs))
526 for (i = 0; i < len; i++)
527 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
529 n = newargs;
531 for (; p1 && p1 != void_list_node;
532 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
534 /* A null type means arg type is not specified.
535 Take whatever the other function type has. */
536 if (TREE_VALUE (p1) == 0)
538 TREE_VALUE (n) = TREE_VALUE (p2);
539 goto parm_done;
541 if (TREE_VALUE (p2) == 0)
543 TREE_VALUE (n) = TREE_VALUE (p1);
544 goto parm_done;
547 /* Given wait (union {union wait *u; int *i} *)
548 and wait (union wait *),
549 prefer union wait * as type of parm. */
550 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
551 && TREE_VALUE (p1) != TREE_VALUE (p2))
553 tree memb;
554 tree mv2 = TREE_VALUE (p2);
555 if (mv2 && mv2 != error_mark_node
556 && TREE_CODE (mv2) != ARRAY_TYPE)
557 mv2 = TYPE_MAIN_VARIANT (mv2);
558 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
559 memb; memb = DECL_CHAIN (memb))
561 tree mv3 = TREE_TYPE (memb);
562 if (mv3 && mv3 != error_mark_node
563 && TREE_CODE (mv3) != ARRAY_TYPE)
564 mv3 = TYPE_MAIN_VARIANT (mv3);
565 if (comptypes (mv3, mv2))
567 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
568 TREE_VALUE (p2));
569 pedwarn (input_location, OPT_Wpedantic,
570 "function types not truly compatible in ISO C");
571 goto parm_done;
575 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
576 && TREE_VALUE (p2) != TREE_VALUE (p1))
578 tree memb;
579 tree mv1 = TREE_VALUE (p1);
580 if (mv1 && mv1 != error_mark_node
581 && TREE_CODE (mv1) != ARRAY_TYPE)
582 mv1 = TYPE_MAIN_VARIANT (mv1);
583 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
584 memb; memb = DECL_CHAIN (memb))
586 tree mv3 = TREE_TYPE (memb);
587 if (mv3 && mv3 != error_mark_node
588 && TREE_CODE (mv3) != ARRAY_TYPE)
589 mv3 = TYPE_MAIN_VARIANT (mv3);
590 if (comptypes (mv3, mv1))
592 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
593 TREE_VALUE (p1));
594 pedwarn (input_location, OPT_Wpedantic,
595 "function types not truly compatible in ISO C");
596 goto parm_done;
600 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
601 parm_done: ;
604 t1 = build_function_type (valtype, newargs);
605 t1 = qualify_type (t1, t2);
606 /* ... falls through ... */
609 default:
610 return build_type_attribute_variant (t1, attributes);
615 /* Return the type of a conditional expression between pointers to
616 possibly differently qualified versions of compatible types.
618 We assume that comp_target_types has already been done and returned
619 nonzero; if that isn't so, this may crash. */
621 static tree
622 common_pointer_type (tree t1, tree t2)
624 tree attributes;
625 tree pointed_to_1, mv1;
626 tree pointed_to_2, mv2;
627 tree target;
628 unsigned target_quals;
629 addr_space_t as1, as2, as_common;
630 int quals1, quals2;
632 /* Save time if the two types are the same. */
634 if (t1 == t2) return t1;
636 /* If one type is nonsense, use the other. */
637 if (t1 == error_mark_node)
638 return t2;
639 if (t2 == error_mark_node)
640 return t1;
642 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
643 && TREE_CODE (t2) == POINTER_TYPE);
645 /* Merge the attributes. */
646 attributes = targetm.merge_type_attributes (t1, t2);
648 /* Find the composite type of the target types, and combine the
649 qualifiers of the two types' targets. Do not lose qualifiers on
650 array element types by taking the TYPE_MAIN_VARIANT. */
651 mv1 = pointed_to_1 = TREE_TYPE (t1);
652 mv2 = pointed_to_2 = TREE_TYPE (t2);
653 if (TREE_CODE (mv1) != ARRAY_TYPE)
654 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
655 if (TREE_CODE (mv2) != ARRAY_TYPE)
656 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
657 target = composite_type (mv1, mv2);
659 /* Strip array types to get correct qualifier for pointers to arrays */
660 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
661 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
663 /* For function types do not merge const qualifiers, but drop them
664 if used inconsistently. The middle-end uses these to mark const
665 and noreturn functions. */
666 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
667 target_quals = (quals1 & quals2);
668 else
669 target_quals = (quals1 | quals2);
671 /* If the two named address spaces are different, determine the common
672 superset address space. This is guaranteed to exist due to the
673 assumption that comp_target_type returned non-zero. */
674 as1 = TYPE_ADDR_SPACE (pointed_to_1);
675 as2 = TYPE_ADDR_SPACE (pointed_to_2);
676 if (!addr_space_superset (as1, as2, &as_common))
677 gcc_unreachable ();
679 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
681 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
682 return build_type_attribute_variant (t1, attributes);
685 /* Return the common type for two arithmetic types under the usual
686 arithmetic conversions. The default conversions have already been
687 applied, and enumerated types converted to their compatible integer
688 types. The resulting type is unqualified and has no attributes.
690 This is the type for the result of most arithmetic operations
691 if the operands have the given two types. */
693 static tree
694 c_common_type (tree t1, tree t2)
696 enum tree_code code1;
697 enum tree_code code2;
699 /* If one type is nonsense, use the other. */
700 if (t1 == error_mark_node)
701 return t2;
702 if (t2 == error_mark_node)
703 return t1;
705 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
706 t1 = TYPE_MAIN_VARIANT (t1);
708 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
709 t2 = TYPE_MAIN_VARIANT (t2);
711 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
712 t1 = build_type_attribute_variant (t1, NULL_TREE);
714 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
715 t2 = build_type_attribute_variant (t2, NULL_TREE);
717 /* Save time if the two types are the same. */
719 if (t1 == t2) return t1;
721 code1 = TREE_CODE (t1);
722 code2 = TREE_CODE (t2);
724 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
725 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
726 || code1 == INTEGER_TYPE);
727 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
728 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
729 || code2 == INTEGER_TYPE);
731 /* When one operand is a decimal float type, the other operand cannot be
732 a generic float type or a complex type. We also disallow vector types
733 here. */
734 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
735 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
737 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
739 error ("can%'t mix operands of decimal float and vector types");
740 return error_mark_node;
742 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
744 error ("can%'t mix operands of decimal float and complex types");
745 return error_mark_node;
747 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
749 error ("can%'t mix operands of decimal float and other float types");
750 return error_mark_node;
754 /* If one type is a vector type, return that type. (How the usual
755 arithmetic conversions apply to the vector types extension is not
756 precisely specified.) */
757 if (code1 == VECTOR_TYPE)
758 return t1;
760 if (code2 == VECTOR_TYPE)
761 return t2;
763 /* If one type is complex, form the common type of the non-complex
764 components, then make that complex. Use T1 or T2 if it is the
765 required type. */
766 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
768 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
769 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
770 tree subtype = c_common_type (subtype1, subtype2);
772 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
773 return t1;
774 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
775 return t2;
776 else
777 return build_complex_type (subtype);
780 /* If only one is real, use it as the result. */
782 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
783 return t1;
785 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
786 return t2;
788 /* If both are real and either are decimal floating point types, use
789 the decimal floating point type with the greater precision. */
791 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
793 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
794 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
795 return dfloat128_type_node;
796 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
797 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
798 return dfloat64_type_node;
799 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
801 return dfloat32_type_node;
804 /* Deal with fixed-point types. */
805 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
807 unsigned int unsignedp = 0, satp = 0;
808 machine_mode m1, m2;
809 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
811 m1 = TYPE_MODE (t1);
812 m2 = TYPE_MODE (t2);
814 /* If one input type is saturating, the result type is saturating. */
815 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
816 satp = 1;
818 /* If both fixed-point types are unsigned, the result type is unsigned.
819 When mixing fixed-point and integer types, follow the sign of the
820 fixed-point type.
821 Otherwise, the result type is signed. */
822 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
823 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
824 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
825 && TYPE_UNSIGNED (t1))
826 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
827 && TYPE_UNSIGNED (t2)))
828 unsignedp = 1;
830 /* The result type is signed. */
831 if (unsignedp == 0)
833 /* If the input type is unsigned, we need to convert to the
834 signed type. */
835 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
837 enum mode_class mclass = (enum mode_class) 0;
838 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
839 mclass = MODE_FRACT;
840 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
841 mclass = MODE_ACCUM;
842 else
843 gcc_unreachable ();
844 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
846 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
848 enum mode_class mclass = (enum mode_class) 0;
849 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
850 mclass = MODE_FRACT;
851 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
852 mclass = MODE_ACCUM;
853 else
854 gcc_unreachable ();
855 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
859 if (code1 == FIXED_POINT_TYPE)
861 fbit1 = GET_MODE_FBIT (m1);
862 ibit1 = GET_MODE_IBIT (m1);
864 else
866 fbit1 = 0;
867 /* Signed integers need to subtract one sign bit. */
868 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
871 if (code2 == FIXED_POINT_TYPE)
873 fbit2 = GET_MODE_FBIT (m2);
874 ibit2 = GET_MODE_IBIT (m2);
876 else
878 fbit2 = 0;
879 /* Signed integers need to subtract one sign bit. */
880 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
883 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
884 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
885 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
886 satp);
889 /* Both real or both integers; use the one with greater precision. */
891 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
892 return t1;
893 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
894 return t2;
896 /* Same precision. Prefer long longs to longs to ints when the
897 same precision, following the C99 rules on integer type rank
898 (which are equivalent to the C90 rules for C90 types). */
900 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
901 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
902 return long_long_unsigned_type_node;
904 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
905 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
907 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
908 return long_long_unsigned_type_node;
909 else
910 return long_long_integer_type_node;
913 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
914 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
915 return long_unsigned_type_node;
917 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
918 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
920 /* But preserve unsignedness from the other type,
921 since long cannot hold all the values of an unsigned int. */
922 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
923 return long_unsigned_type_node;
924 else
925 return long_integer_type_node;
928 /* Likewise, prefer long double to double even if same size. */
929 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
930 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
931 return long_double_type_node;
933 /* Likewise, prefer double to float even if same size.
934 We got a couple of embedded targets with 32 bit doubles, and the
935 pdp11 might have 64 bit floats. */
936 if (TYPE_MAIN_VARIANT (t1) == double_type_node
937 || TYPE_MAIN_VARIANT (t2) == double_type_node)
938 return double_type_node;
940 /* Otherwise prefer the unsigned one. */
942 if (TYPE_UNSIGNED (t1))
943 return t1;
944 else
945 return t2;
948 /* Wrapper around c_common_type that is used by c-common.c and other
949 front end optimizations that remove promotions. ENUMERAL_TYPEs
950 are allowed here and are converted to their compatible integer types.
951 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
952 preferably a non-Boolean type as the common type. */
953 tree
954 common_type (tree t1, tree t2)
956 if (TREE_CODE (t1) == ENUMERAL_TYPE)
957 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
958 if (TREE_CODE (t2) == ENUMERAL_TYPE)
959 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
961 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
962 if (TREE_CODE (t1) == BOOLEAN_TYPE
963 && TREE_CODE (t2) == BOOLEAN_TYPE)
964 return boolean_type_node;
966 /* If either type is BOOLEAN_TYPE, then return the other. */
967 if (TREE_CODE (t1) == BOOLEAN_TYPE)
968 return t2;
969 if (TREE_CODE (t2) == BOOLEAN_TYPE)
970 return t1;
972 return c_common_type (t1, t2);
975 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
976 or various other operations. Return 2 if they are compatible
977 but a warning may be needed if you use them together. */
980 comptypes (tree type1, tree type2)
982 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
983 int val;
985 val = comptypes_internal (type1, type2, NULL, NULL);
986 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
988 return val;
991 /* Like comptypes, but if it returns non-zero because enum and int are
992 compatible, it sets *ENUM_AND_INT_P to true. */
994 static int
995 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
997 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
998 int val;
1000 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1001 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1003 return val;
1006 /* Like comptypes, but if it returns nonzero for different types, it
1007 sets *DIFFERENT_TYPES_P to true. */
1010 comptypes_check_different_types (tree type1, tree type2,
1011 bool *different_types_p)
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, different_types_p);
1017 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1019 return val;
1022 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1023 or various other operations. Return 2 if they are compatible
1024 but a warning may be needed if you use them together. If
1025 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1026 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1027 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1028 NULL, and the types are compatible but different enough not to be
1029 permitted in C11 typedef redeclarations, then this sets
1030 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1031 false, but may or may not be set if the types are incompatible.
1032 This differs from comptypes, in that we don't free the seen
1033 types. */
1035 static int
1036 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1037 bool *different_types_p)
1039 const_tree t1 = type1;
1040 const_tree t2 = type2;
1041 int attrval, val;
1043 /* Suppress errors caused by previously reported errors. */
1045 if (t1 == t2 || !t1 || !t2
1046 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1047 return 1;
1049 /* Enumerated types are compatible with integer types, but this is
1050 not transitive: two enumerated types in the same translation unit
1051 are compatible with each other only if they are the same type. */
1053 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1055 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1056 if (TREE_CODE (t2) != VOID_TYPE)
1058 if (enum_and_int_p != NULL)
1059 *enum_and_int_p = true;
1060 if (different_types_p != NULL)
1061 *different_types_p = true;
1064 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1066 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1067 if (TREE_CODE (t1) != VOID_TYPE)
1069 if (enum_and_int_p != NULL)
1070 *enum_and_int_p = true;
1071 if (different_types_p != NULL)
1072 *different_types_p = true;
1076 if (t1 == t2)
1077 return 1;
1079 /* Different classes of types can't be compatible. */
1081 if (TREE_CODE (t1) != TREE_CODE (t2))
1082 return 0;
1084 /* Qualifiers must match. C99 6.7.3p9 */
1086 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1087 return 0;
1089 /* Allow for two different type nodes which have essentially the same
1090 definition. Note that we already checked for equality of the type
1091 qualifiers (just above). */
1093 if (TREE_CODE (t1) != ARRAY_TYPE
1094 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1095 return 1;
1097 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1098 if (!(attrval = comp_type_attributes (t1, t2)))
1099 return 0;
1101 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1102 val = 0;
1104 switch (TREE_CODE (t1))
1106 case POINTER_TYPE:
1107 /* Do not remove mode or aliasing information. */
1108 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1109 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1110 break;
1111 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1112 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1113 enum_and_int_p, different_types_p));
1114 break;
1116 case FUNCTION_TYPE:
1117 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1118 different_types_p);
1119 break;
1121 case ARRAY_TYPE:
1123 tree d1 = TYPE_DOMAIN (t1);
1124 tree d2 = TYPE_DOMAIN (t2);
1125 bool d1_variable, d2_variable;
1126 bool d1_zero, d2_zero;
1127 val = 1;
1129 /* Target types must match incl. qualifiers. */
1130 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1131 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1132 enum_and_int_p,
1133 different_types_p)))
1134 return 0;
1136 if (different_types_p != NULL
1137 && (d1 == 0) != (d2 == 0))
1138 *different_types_p = true;
1139 /* Sizes must match unless one is missing or variable. */
1140 if (d1 == 0 || d2 == 0 || d1 == d2)
1141 break;
1143 d1_zero = !TYPE_MAX_VALUE (d1);
1144 d2_zero = !TYPE_MAX_VALUE (d2);
1146 d1_variable = (!d1_zero
1147 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1148 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1149 d2_variable = (!d2_zero
1150 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1151 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1152 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1153 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1155 if (different_types_p != NULL
1156 && d1_variable != d2_variable)
1157 *different_types_p = true;
1158 if (d1_variable || d2_variable)
1159 break;
1160 if (d1_zero && d2_zero)
1161 break;
1162 if (d1_zero || d2_zero
1163 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1164 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1165 val = 0;
1167 break;
1170 case ENUMERAL_TYPE:
1171 case RECORD_TYPE:
1172 case UNION_TYPE:
1173 if (val != 1 && !same_translation_unit_p (t1, t2))
1175 tree a1 = TYPE_ATTRIBUTES (t1);
1176 tree a2 = TYPE_ATTRIBUTES (t2);
1178 if (! attribute_list_contained (a1, a2)
1179 && ! attribute_list_contained (a2, a1))
1180 break;
1182 if (attrval != 2)
1183 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1184 different_types_p);
1185 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1186 different_types_p);
1188 break;
1190 case VECTOR_TYPE:
1191 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1192 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1193 enum_and_int_p, different_types_p));
1194 break;
1196 default:
1197 break;
1199 return attrval == 2 && val == 1 ? 2 : val;
1202 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1203 their qualifiers, except for named address spaces. If the pointers point to
1204 different named addresses, then we must determine if one address space is a
1205 subset of the other. */
1207 static int
1208 comp_target_types (location_t location, tree ttl, tree ttr)
1210 int val;
1211 int val_ped;
1212 tree mvl = TREE_TYPE (ttl);
1213 tree mvr = TREE_TYPE (ttr);
1214 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1215 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1216 addr_space_t as_common;
1217 bool enum_and_int_p;
1219 /* Fail if pointers point to incompatible address spaces. */
1220 if (!addr_space_superset (asl, asr, &as_common))
1221 return 0;
1223 /* For pedantic record result of comptypes on arrays before losing
1224 qualifiers on the element type below. */
1225 val_ped = 1;
1227 if (TREE_CODE (mvl) == ARRAY_TYPE
1228 && TREE_CODE (mvr) == ARRAY_TYPE)
1229 val_ped = comptypes (mvl, mvr);
1231 /* Qualifiers on element types of array types that are
1232 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1234 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1235 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1236 : TYPE_MAIN_VARIANT (mvl));
1238 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1239 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1240 : TYPE_MAIN_VARIANT (mvr));
1242 enum_and_int_p = false;
1243 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1245 if (val == 1 && val_ped != 1)
1246 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1247 "are incompatible in ISO C");
1249 if (val == 2)
1250 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1252 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1253 warning_at (location, OPT_Wc___compat,
1254 "pointer target types incompatible in C++");
1256 return val;
1259 /* Subroutines of `comptypes'. */
1261 /* Determine whether two trees derive from the same translation unit.
1262 If the CONTEXT chain ends in a null, that tree's context is still
1263 being parsed, so if two trees have context chains ending in null,
1264 they're in the same translation unit. */
1266 same_translation_unit_p (const_tree t1, const_tree t2)
1268 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1269 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1271 case tcc_declaration:
1272 t1 = DECL_CONTEXT (t1); break;
1273 case tcc_type:
1274 t1 = TYPE_CONTEXT (t1); break;
1275 case tcc_exceptional:
1276 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1277 default: gcc_unreachable ();
1280 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1281 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1283 case tcc_declaration:
1284 t2 = DECL_CONTEXT (t2); break;
1285 case tcc_type:
1286 t2 = TYPE_CONTEXT (t2); break;
1287 case tcc_exceptional:
1288 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1289 default: gcc_unreachable ();
1292 return t1 == t2;
1295 /* Allocate the seen two types, assuming that they are compatible. */
1297 static struct tagged_tu_seen_cache *
1298 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1300 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1301 tu->next = tagged_tu_seen_base;
1302 tu->t1 = t1;
1303 tu->t2 = t2;
1305 tagged_tu_seen_base = tu;
1307 /* The C standard says that two structures in different translation
1308 units are compatible with each other only if the types of their
1309 fields are compatible (among other things). We assume that they
1310 are compatible until proven otherwise when building the cache.
1311 An example where this can occur is:
1312 struct a
1314 struct a *next;
1316 If we are comparing this against a similar struct in another TU,
1317 and did not assume they were compatible, we end up with an infinite
1318 loop. */
1319 tu->val = 1;
1320 return tu;
1323 /* Free the seen types until we get to TU_TIL. */
1325 static void
1326 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1328 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1329 while (tu != tu_til)
1331 const struct tagged_tu_seen_cache *const tu1
1332 = (const struct tagged_tu_seen_cache *) tu;
1333 tu = tu1->next;
1334 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1336 tagged_tu_seen_base = tu_til;
1339 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1340 compatible. If the two types are not the same (which has been
1341 checked earlier), this can only happen when multiple translation
1342 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1343 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1344 comptypes_internal. */
1346 static int
1347 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1348 bool *enum_and_int_p, bool *different_types_p)
1350 tree s1, s2;
1351 bool needs_warning = false;
1353 /* We have to verify that the tags of the types are the same. This
1354 is harder than it looks because this may be a typedef, so we have
1355 to go look at the original type. It may even be a typedef of a
1356 typedef...
1357 In the case of compiler-created builtin structs the TYPE_DECL
1358 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1359 while (TYPE_NAME (t1)
1360 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1361 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1362 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1364 while (TYPE_NAME (t2)
1365 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1366 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1367 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1369 /* C90 didn't have the requirement that the two tags be the same. */
1370 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1371 return 0;
1373 /* C90 didn't say what happened if one or both of the types were
1374 incomplete; we choose to follow C99 rules here, which is that they
1375 are compatible. */
1376 if (TYPE_SIZE (t1) == NULL
1377 || TYPE_SIZE (t2) == NULL)
1378 return 1;
1381 const struct tagged_tu_seen_cache * tts_i;
1382 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1383 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1384 return tts_i->val;
1387 switch (TREE_CODE (t1))
1389 case ENUMERAL_TYPE:
1391 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1392 /* Speed up the case where the type values are in the same order. */
1393 tree tv1 = TYPE_VALUES (t1);
1394 tree tv2 = TYPE_VALUES (t2);
1396 if (tv1 == tv2)
1398 return 1;
1401 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1403 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1404 break;
1405 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1407 tu->val = 0;
1408 return 0;
1412 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1414 return 1;
1416 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1418 tu->val = 0;
1419 return 0;
1422 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1424 tu->val = 0;
1425 return 0;
1428 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1430 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1431 if (s2 == NULL
1432 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1434 tu->val = 0;
1435 return 0;
1438 return 1;
1441 case UNION_TYPE:
1443 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1444 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1446 tu->val = 0;
1447 return 0;
1450 /* Speed up the common case where the fields are in the same order. */
1451 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1452 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1454 int result;
1456 if (DECL_NAME (s1) != DECL_NAME (s2))
1457 break;
1458 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1459 enum_and_int_p, different_types_p);
1461 if (result != 1 && !DECL_NAME (s1))
1462 break;
1463 if (result == 0)
1465 tu->val = 0;
1466 return 0;
1468 if (result == 2)
1469 needs_warning = true;
1471 if (TREE_CODE (s1) == FIELD_DECL
1472 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1473 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1475 tu->val = 0;
1476 return 0;
1479 if (!s1 && !s2)
1481 tu->val = needs_warning ? 2 : 1;
1482 return tu->val;
1485 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1487 bool ok = false;
1489 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1490 if (DECL_NAME (s1) == DECL_NAME (s2))
1492 int result;
1494 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1495 enum_and_int_p,
1496 different_types_p);
1498 if (result != 1 && !DECL_NAME (s1))
1499 continue;
1500 if (result == 0)
1502 tu->val = 0;
1503 return 0;
1505 if (result == 2)
1506 needs_warning = true;
1508 if (TREE_CODE (s1) == FIELD_DECL
1509 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1510 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1511 break;
1513 ok = true;
1514 break;
1516 if (!ok)
1518 tu->val = 0;
1519 return 0;
1522 tu->val = needs_warning ? 2 : 10;
1523 return tu->val;
1526 case RECORD_TYPE:
1528 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1530 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1531 s1 && s2;
1532 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1534 int result;
1535 if (TREE_CODE (s1) != TREE_CODE (s2)
1536 || DECL_NAME (s1) != DECL_NAME (s2))
1537 break;
1538 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1539 enum_and_int_p, different_types_p);
1540 if (result == 0)
1541 break;
1542 if (result == 2)
1543 needs_warning = true;
1545 if (TREE_CODE (s1) == FIELD_DECL
1546 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1547 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1548 break;
1550 if (s1 && s2)
1551 tu->val = 0;
1552 else
1553 tu->val = needs_warning ? 2 : 1;
1554 return tu->val;
1557 default:
1558 gcc_unreachable ();
1562 /* Return 1 if two function types F1 and F2 are compatible.
1563 If either type specifies no argument types,
1564 the other must specify a fixed number of self-promoting arg types.
1565 Otherwise, if one type specifies only the number of arguments,
1566 the other must specify that number of self-promoting arg types.
1567 Otherwise, the argument types must match.
1568 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1570 static int
1571 function_types_compatible_p (const_tree f1, const_tree f2,
1572 bool *enum_and_int_p, bool *different_types_p)
1574 tree args1, args2;
1575 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1576 int val = 1;
1577 int val1;
1578 tree ret1, ret2;
1580 ret1 = TREE_TYPE (f1);
1581 ret2 = TREE_TYPE (f2);
1583 /* 'volatile' qualifiers on a function's return type used to mean
1584 the function is noreturn. */
1585 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1586 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1587 if (TYPE_VOLATILE (ret1))
1588 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1589 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1590 if (TYPE_VOLATILE (ret2))
1591 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1592 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1593 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1594 if (val == 0)
1595 return 0;
1597 args1 = TYPE_ARG_TYPES (f1);
1598 args2 = TYPE_ARG_TYPES (f2);
1600 if (different_types_p != NULL
1601 && (args1 == 0) != (args2 == 0))
1602 *different_types_p = true;
1604 /* An unspecified parmlist matches any specified parmlist
1605 whose argument types don't need default promotions. */
1607 if (args1 == 0)
1609 if (!self_promoting_args_p (args2))
1610 return 0;
1611 /* If one of these types comes from a non-prototype fn definition,
1612 compare that with the other type's arglist.
1613 If they don't match, ask for a warning (but no error). */
1614 if (TYPE_ACTUAL_ARG_TYPES (f1)
1615 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1616 enum_and_int_p, different_types_p))
1617 val = 2;
1618 return val;
1620 if (args2 == 0)
1622 if (!self_promoting_args_p (args1))
1623 return 0;
1624 if (TYPE_ACTUAL_ARG_TYPES (f2)
1625 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1626 enum_and_int_p, different_types_p))
1627 val = 2;
1628 return val;
1631 /* Both types have argument lists: compare them and propagate results. */
1632 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1633 different_types_p);
1634 return val1 != 1 ? val1 : val;
1637 /* Check two lists of types for compatibility, returning 0 for
1638 incompatible, 1 for compatible, or 2 for compatible with
1639 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1640 comptypes_internal. */
1642 static int
1643 type_lists_compatible_p (const_tree args1, const_tree args2,
1644 bool *enum_and_int_p, bool *different_types_p)
1646 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1647 int val = 1;
1648 int newval = 0;
1650 while (1)
1652 tree a1, mv1, a2, mv2;
1653 if (args1 == 0 && args2 == 0)
1654 return val;
1655 /* If one list is shorter than the other,
1656 they fail to match. */
1657 if (args1 == 0 || args2 == 0)
1658 return 0;
1659 mv1 = a1 = TREE_VALUE (args1);
1660 mv2 = a2 = TREE_VALUE (args2);
1661 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1662 mv1 = (TYPE_ATOMIC (mv1)
1663 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1664 TYPE_QUAL_ATOMIC)
1665 : TYPE_MAIN_VARIANT (mv1));
1666 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1667 mv2 = (TYPE_ATOMIC (mv2)
1668 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1669 TYPE_QUAL_ATOMIC)
1670 : TYPE_MAIN_VARIANT (mv2));
1671 /* A null pointer instead of a type
1672 means there is supposed to be an argument
1673 but nothing is specified about what type it has.
1674 So match anything that self-promotes. */
1675 if (different_types_p != NULL
1676 && (a1 == 0) != (a2 == 0))
1677 *different_types_p = true;
1678 if (a1 == 0)
1680 if (c_type_promotes_to (a2) != a2)
1681 return 0;
1683 else if (a2 == 0)
1685 if (c_type_promotes_to (a1) != a1)
1686 return 0;
1688 /* If one of the lists has an error marker, ignore this arg. */
1689 else if (TREE_CODE (a1) == ERROR_MARK
1690 || TREE_CODE (a2) == ERROR_MARK)
1692 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1693 different_types_p)))
1695 if (different_types_p != NULL)
1696 *different_types_p = true;
1697 /* Allow wait (union {union wait *u; int *i} *)
1698 and wait (union wait *) to be compatible. */
1699 if (TREE_CODE (a1) == UNION_TYPE
1700 && (TYPE_NAME (a1) == 0
1701 || TYPE_TRANSPARENT_AGGR (a1))
1702 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1703 && tree_int_cst_equal (TYPE_SIZE (a1),
1704 TYPE_SIZE (a2)))
1706 tree memb;
1707 for (memb = TYPE_FIELDS (a1);
1708 memb; memb = DECL_CHAIN (memb))
1710 tree mv3 = TREE_TYPE (memb);
1711 if (mv3 && mv3 != error_mark_node
1712 && TREE_CODE (mv3) != ARRAY_TYPE)
1713 mv3 = (TYPE_ATOMIC (mv3)
1714 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1715 TYPE_QUAL_ATOMIC)
1716 : TYPE_MAIN_VARIANT (mv3));
1717 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1718 different_types_p))
1719 break;
1721 if (memb == 0)
1722 return 0;
1724 else if (TREE_CODE (a2) == UNION_TYPE
1725 && (TYPE_NAME (a2) == 0
1726 || TYPE_TRANSPARENT_AGGR (a2))
1727 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1728 && tree_int_cst_equal (TYPE_SIZE (a2),
1729 TYPE_SIZE (a1)))
1731 tree memb;
1732 for (memb = TYPE_FIELDS (a2);
1733 memb; memb = DECL_CHAIN (memb))
1735 tree mv3 = TREE_TYPE (memb);
1736 if (mv3 && mv3 != error_mark_node
1737 && TREE_CODE (mv3) != ARRAY_TYPE)
1738 mv3 = (TYPE_ATOMIC (mv3)
1739 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1740 TYPE_QUAL_ATOMIC)
1741 : TYPE_MAIN_VARIANT (mv3));
1742 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1743 different_types_p))
1744 break;
1746 if (memb == 0)
1747 return 0;
1749 else
1750 return 0;
1753 /* comptypes said ok, but record if it said to warn. */
1754 if (newval > val)
1755 val = newval;
1757 args1 = TREE_CHAIN (args1);
1758 args2 = TREE_CHAIN (args2);
1762 /* Compute the size to increment a pointer by. When a function type or void
1763 type or incomplete type is passed, size_one_node is returned.
1764 This function does not emit any diagnostics; the caller is responsible
1765 for that. */
1767 static tree
1768 c_size_in_bytes (const_tree type)
1770 enum tree_code code = TREE_CODE (type);
1772 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1773 || !COMPLETE_TYPE_P (type))
1774 return size_one_node;
1776 /* Convert in case a char is more than one unit. */
1777 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1778 size_int (TYPE_PRECISION (char_type_node)
1779 / BITS_PER_UNIT));
1782 /* Return either DECL or its known constant value (if it has one). */
1784 tree
1785 decl_constant_value (tree decl)
1787 if (/* Don't change a variable array bound or initial value to a constant
1788 in a place where a variable is invalid. Note that DECL_INITIAL
1789 isn't valid for a PARM_DECL. */
1790 current_function_decl != 0
1791 && TREE_CODE (decl) != PARM_DECL
1792 && !TREE_THIS_VOLATILE (decl)
1793 && TREE_READONLY (decl)
1794 && DECL_INITIAL (decl) != 0
1795 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1796 /* This is invalid if initial value is not constant.
1797 If it has either a function call, a memory reference,
1798 or a variable, then re-evaluating it could give different results. */
1799 && TREE_CONSTANT (DECL_INITIAL (decl))
1800 /* Check for cases where this is sub-optimal, even though valid. */
1801 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1802 return DECL_INITIAL (decl);
1803 return decl;
1806 /* Convert the array expression EXP to a pointer. */
1807 static tree
1808 array_to_pointer_conversion (location_t loc, tree exp)
1810 tree orig_exp = exp;
1811 tree type = TREE_TYPE (exp);
1812 tree adr;
1813 tree restype = TREE_TYPE (type);
1814 tree ptrtype;
1816 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1818 STRIP_TYPE_NOPS (exp);
1820 if (TREE_NO_WARNING (orig_exp))
1821 TREE_NO_WARNING (exp) = 1;
1823 ptrtype = build_pointer_type (restype);
1825 if (INDIRECT_REF_P (exp))
1826 return convert (ptrtype, TREE_OPERAND (exp, 0));
1828 /* In C++ array compound literals are temporary objects unless they are
1829 const or appear in namespace scope, so they are destroyed too soon
1830 to use them for much of anything (c++/53220). */
1831 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1833 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1834 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1835 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1836 "converting an array compound literal to a pointer "
1837 "is ill-formed in C++");
1840 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1841 return convert (ptrtype, adr);
1844 /* Convert the function expression EXP to a pointer. */
1845 static tree
1846 function_to_pointer_conversion (location_t loc, tree exp)
1848 tree orig_exp = exp;
1850 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1852 STRIP_TYPE_NOPS (exp);
1854 if (TREE_NO_WARNING (orig_exp))
1855 TREE_NO_WARNING (exp) = 1;
1857 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1860 /* Mark EXP as read, not just set, for set but not used -Wunused
1861 warning purposes. */
1863 void
1864 mark_exp_read (tree exp)
1866 switch (TREE_CODE (exp))
1868 case VAR_DECL:
1869 case PARM_DECL:
1870 DECL_READ_P (exp) = 1;
1871 break;
1872 case ARRAY_REF:
1873 case COMPONENT_REF:
1874 case MODIFY_EXPR:
1875 case REALPART_EXPR:
1876 case IMAGPART_EXPR:
1877 CASE_CONVERT:
1878 case ADDR_EXPR:
1879 mark_exp_read (TREE_OPERAND (exp, 0));
1880 break;
1881 case COMPOUND_EXPR:
1882 case C_MAYBE_CONST_EXPR:
1883 mark_exp_read (TREE_OPERAND (exp, 1));
1884 break;
1885 default:
1886 break;
1890 /* Perform the default conversion of arrays and functions to pointers.
1891 Return the result of converting EXP. For any other expression, just
1892 return EXP.
1894 LOC is the location of the expression. */
1896 struct c_expr
1897 default_function_array_conversion (location_t loc, struct c_expr exp)
1899 tree orig_exp = exp.value;
1900 tree type = TREE_TYPE (exp.value);
1901 enum tree_code code = TREE_CODE (type);
1903 switch (code)
1905 case ARRAY_TYPE:
1907 bool not_lvalue = false;
1908 bool lvalue_array_p;
1910 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1911 || CONVERT_EXPR_P (exp.value))
1912 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1914 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1915 not_lvalue = true;
1916 exp.value = TREE_OPERAND (exp.value, 0);
1919 if (TREE_NO_WARNING (orig_exp))
1920 TREE_NO_WARNING (exp.value) = 1;
1922 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1923 if (!flag_isoc99 && !lvalue_array_p)
1925 /* Before C99, non-lvalue arrays do not decay to pointers.
1926 Normally, using such an array would be invalid; but it can
1927 be used correctly inside sizeof or as a statement expression.
1928 Thus, do not give an error here; an error will result later. */
1929 return exp;
1932 exp.value = array_to_pointer_conversion (loc, exp.value);
1934 break;
1935 case FUNCTION_TYPE:
1936 exp.value = function_to_pointer_conversion (loc, exp.value);
1937 break;
1938 default:
1939 break;
1942 return exp;
1945 struct c_expr
1946 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1948 mark_exp_read (exp.value);
1949 return default_function_array_conversion (loc, exp);
1952 /* Return whether EXPR should be treated as an atomic lvalue for the
1953 purposes of load and store handling. */
1955 static bool
1956 really_atomic_lvalue (tree expr)
1958 if (error_operand_p (expr))
1959 return false;
1960 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1961 return false;
1962 if (!lvalue_p (expr))
1963 return false;
1965 /* Ignore _Atomic on register variables, since their addresses can't
1966 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1967 sequences wouldn't work. Ignore _Atomic on structures containing
1968 bit-fields, since accessing elements of atomic structures or
1969 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1970 it's undefined at translation time or execution time, and the
1971 normal atomic sequences again wouldn't work. */
1972 while (handled_component_p (expr))
1974 if (TREE_CODE (expr) == COMPONENT_REF
1975 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1976 return false;
1977 expr = TREE_OPERAND (expr, 0);
1979 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1980 return false;
1981 return true;
1984 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1985 including converting functions and arrays to pointers if CONVERT_P.
1986 If READ_P, also mark the expression as having been read. */
1988 struct c_expr
1989 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1990 bool convert_p, bool read_p)
1992 if (read_p)
1993 mark_exp_read (exp.value);
1994 if (convert_p)
1995 exp = default_function_array_conversion (loc, exp);
1996 if (really_atomic_lvalue (exp.value))
1998 vec<tree, va_gc> *params;
1999 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2000 tree expr_type = TREE_TYPE (exp.value);
2001 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2002 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2004 gcc_assert (TYPE_ATOMIC (expr_type));
2006 /* Expansion of a generic atomic load may require an addition
2007 element, so allocate enough to prevent a resize. */
2008 vec_alloc (params, 4);
2010 /* Remove the qualifiers for the rest of the expressions and
2011 create the VAL temp variable to hold the RHS. */
2012 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2013 tmp = create_tmp_var_raw (nonatomic_type);
2014 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2015 TREE_ADDRESSABLE (tmp) = 1;
2016 TREE_NO_WARNING (tmp) = 1;
2018 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2019 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2020 params->quick_push (expr_addr);
2021 params->quick_push (tmp_addr);
2022 params->quick_push (seq_cst);
2023 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2025 /* EXPR is always read. */
2026 mark_exp_read (exp.value);
2028 /* Return tmp which contains the value loaded. */
2029 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2030 NULL_TREE, NULL_TREE);
2032 return exp;
2035 /* EXP is an expression of integer type. Apply the integer promotions
2036 to it and return the promoted value. */
2038 tree
2039 perform_integral_promotions (tree exp)
2041 tree type = TREE_TYPE (exp);
2042 enum tree_code code = TREE_CODE (type);
2044 gcc_assert (INTEGRAL_TYPE_P (type));
2046 /* Normally convert enums to int,
2047 but convert wide enums to something wider. */
2048 if (code == ENUMERAL_TYPE)
2050 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2051 TYPE_PRECISION (integer_type_node)),
2052 ((TYPE_PRECISION (type)
2053 >= TYPE_PRECISION (integer_type_node))
2054 && TYPE_UNSIGNED (type)));
2056 return convert (type, exp);
2059 /* ??? This should no longer be needed now bit-fields have their
2060 proper types. */
2061 if (TREE_CODE (exp) == COMPONENT_REF
2062 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2063 /* If it's thinner than an int, promote it like a
2064 c_promoting_integer_type_p, otherwise leave it alone. */
2065 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2066 TYPE_PRECISION (integer_type_node)))
2067 return convert (integer_type_node, exp);
2069 if (c_promoting_integer_type_p (type))
2071 /* Preserve unsignedness if not really getting any wider. */
2072 if (TYPE_UNSIGNED (type)
2073 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2074 return convert (unsigned_type_node, exp);
2076 return convert (integer_type_node, exp);
2079 return exp;
2083 /* Perform default promotions for C data used in expressions.
2084 Enumeral types or short or char are converted to int.
2085 In addition, manifest constants symbols are replaced by their values. */
2087 tree
2088 default_conversion (tree exp)
2090 tree orig_exp;
2091 tree type = TREE_TYPE (exp);
2092 enum tree_code code = TREE_CODE (type);
2093 tree promoted_type;
2095 mark_exp_read (exp);
2097 /* Functions and arrays have been converted during parsing. */
2098 gcc_assert (code != FUNCTION_TYPE);
2099 if (code == ARRAY_TYPE)
2100 return exp;
2102 /* Constants can be used directly unless they're not loadable. */
2103 if (TREE_CODE (exp) == CONST_DECL)
2104 exp = DECL_INITIAL (exp);
2106 /* Strip no-op conversions. */
2107 orig_exp = exp;
2108 STRIP_TYPE_NOPS (exp);
2110 if (TREE_NO_WARNING (orig_exp))
2111 TREE_NO_WARNING (exp) = 1;
2113 if (code == VOID_TYPE)
2115 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2116 "void value not ignored as it ought to be");
2117 return error_mark_node;
2120 exp = require_complete_type (exp);
2121 if (exp == error_mark_node)
2122 return error_mark_node;
2124 promoted_type = targetm.promoted_type (type);
2125 if (promoted_type)
2126 return convert (promoted_type, exp);
2128 if (INTEGRAL_TYPE_P (type))
2129 return perform_integral_promotions (exp);
2131 return exp;
2134 /* Look up COMPONENT in a structure or union TYPE.
2136 If the component name is not found, returns NULL_TREE. Otherwise,
2137 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2138 stepping down the chain to the component, which is in the last
2139 TREE_VALUE of the list. Normally the list is of length one, but if
2140 the component is embedded within (nested) anonymous structures or
2141 unions, the list steps down the chain to the component. */
2143 static tree
2144 lookup_field (tree type, tree component)
2146 tree field;
2148 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2149 to the field elements. Use a binary search on this array to quickly
2150 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2151 will always be set for structures which have many elements. */
2153 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2155 int bot, top, half;
2156 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2158 field = TYPE_FIELDS (type);
2159 bot = 0;
2160 top = TYPE_LANG_SPECIFIC (type)->s->len;
2161 while (top - bot > 1)
2163 half = (top - bot + 1) >> 1;
2164 field = field_array[bot+half];
2166 if (DECL_NAME (field) == NULL_TREE)
2168 /* Step through all anon unions in linear fashion. */
2169 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2171 field = field_array[bot++];
2172 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2174 tree anon = lookup_field (TREE_TYPE (field), component);
2176 if (anon)
2177 return tree_cons (NULL_TREE, field, anon);
2179 /* The Plan 9 compiler permits referring
2180 directly to an anonymous struct/union field
2181 using a typedef name. */
2182 if (flag_plan9_extensions
2183 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2184 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2185 == TYPE_DECL)
2186 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2187 == component))
2188 break;
2192 /* Entire record is only anon unions. */
2193 if (bot > top)
2194 return NULL_TREE;
2196 /* Restart the binary search, with new lower bound. */
2197 continue;
2200 if (DECL_NAME (field) == component)
2201 break;
2202 if (DECL_NAME (field) < component)
2203 bot += half;
2204 else
2205 top = bot + half;
2208 if (DECL_NAME (field_array[bot]) == component)
2209 field = field_array[bot];
2210 else if (DECL_NAME (field) != component)
2211 return NULL_TREE;
2213 else
2215 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2217 if (DECL_NAME (field) == NULL_TREE
2218 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2220 tree anon = lookup_field (TREE_TYPE (field), component);
2222 if (anon)
2223 return tree_cons (NULL_TREE, field, anon);
2225 /* The Plan 9 compiler permits referring directly to an
2226 anonymous struct/union field using a typedef
2227 name. */
2228 if (flag_plan9_extensions
2229 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2230 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2231 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2232 == component))
2233 break;
2236 if (DECL_NAME (field) == component)
2237 break;
2240 if (field == NULL_TREE)
2241 return NULL_TREE;
2244 return tree_cons (NULL_TREE, field, NULL_TREE);
2247 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2249 static void
2250 lookup_field_fuzzy_find_candidates (tree type, tree component,
2251 vec<tree> *candidates)
2253 tree field;
2254 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2256 if (DECL_NAME (field) == NULL_TREE
2257 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2258 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2259 candidates);
2261 if (DECL_NAME (field))
2262 candidates->safe_push (DECL_NAME (field));
2266 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2267 rather than returning a TREE_LIST for an exact match. */
2269 static tree
2270 lookup_field_fuzzy (tree type, tree component)
2272 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2274 /* First, gather a list of candidates. */
2275 auto_vec <tree> candidates;
2277 lookup_field_fuzzy_find_candidates (type, component,
2278 &candidates);
2280 return find_closest_identifier (component, &candidates);
2283 /* Support function for build_component_ref's error-handling.
2285 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2286 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2288 static bool
2289 should_suggest_deref_p (tree datum_type)
2291 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2292 allows "." for ptrs; we could be handling a failed attempt
2293 to access a property. */
2294 if (c_dialect_objc ())
2295 return false;
2297 /* Only suggest it for pointers... */
2298 if (TREE_CODE (datum_type) != POINTER_TYPE)
2299 return false;
2301 /* ...to structs/unions. */
2302 tree underlying_type = TREE_TYPE (datum_type);
2303 enum tree_code code = TREE_CODE (underlying_type);
2304 if (code == RECORD_TYPE || code == UNION_TYPE)
2305 return true;
2306 else
2307 return false;
2310 /* Make an expression to refer to the COMPONENT field of structure or
2311 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2312 location of the COMPONENT_REF. */
2314 tree
2315 build_component_ref (location_t loc, tree datum, tree component)
2317 tree type = TREE_TYPE (datum);
2318 enum tree_code code = TREE_CODE (type);
2319 tree field = NULL;
2320 tree ref;
2321 bool datum_lvalue = lvalue_p (datum);
2323 if (!objc_is_public (datum, component))
2324 return error_mark_node;
2326 /* Detect Objective-C property syntax object.property. */
2327 if (c_dialect_objc ()
2328 && (ref = objc_maybe_build_component_ref (datum, component)))
2329 return ref;
2331 /* See if there is a field or component with name COMPONENT. */
2333 if (code == RECORD_TYPE || code == UNION_TYPE)
2335 if (!COMPLETE_TYPE_P (type))
2337 c_incomplete_type_error (NULL_TREE, type);
2338 return error_mark_node;
2341 field = lookup_field (type, component);
2343 if (!field)
2345 tree guessed_id = lookup_field_fuzzy (type, component);
2346 if (guessed_id)
2347 error_at (loc, "%qT has no member named %qE; did you mean %qE?",
2348 type, component, guessed_id);
2349 else
2350 error_at (loc, "%qT has no member named %qE", type, component);
2351 return error_mark_node;
2354 /* Accessing elements of atomic structures or unions is undefined
2355 behavior (C11 6.5.2.3#5). */
2356 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2358 if (code == RECORD_TYPE)
2359 warning_at (loc, 0, "accessing a member %qE of an atomic "
2360 "structure %qE", component, datum);
2361 else
2362 warning_at (loc, 0, "accessing a member %qE of an atomic "
2363 "union %qE", component, datum);
2366 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2367 This might be better solved in future the way the C++ front
2368 end does it - by giving the anonymous entities each a
2369 separate name and type, and then have build_component_ref
2370 recursively call itself. We can't do that here. */
2373 tree subdatum = TREE_VALUE (field);
2374 int quals;
2375 tree subtype;
2376 bool use_datum_quals;
2378 if (TREE_TYPE (subdatum) == error_mark_node)
2379 return error_mark_node;
2381 /* If this is an rvalue, it does not have qualifiers in C
2382 standard terms and we must avoid propagating such
2383 qualifiers down to a non-lvalue array that is then
2384 converted to a pointer. */
2385 use_datum_quals = (datum_lvalue
2386 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2388 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2389 if (use_datum_quals)
2390 quals |= TYPE_QUALS (TREE_TYPE (datum));
2391 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2393 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2394 NULL_TREE);
2395 SET_EXPR_LOCATION (ref, loc);
2396 if (TREE_READONLY (subdatum)
2397 || (use_datum_quals && TREE_READONLY (datum)))
2398 TREE_READONLY (ref) = 1;
2399 if (TREE_THIS_VOLATILE (subdatum)
2400 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2401 TREE_THIS_VOLATILE (ref) = 1;
2403 if (TREE_DEPRECATED (subdatum))
2404 warn_deprecated_use (subdatum, NULL_TREE);
2406 datum = ref;
2408 field = TREE_CHAIN (field);
2410 while (field);
2412 return ref;
2414 else if (should_suggest_deref_p (type))
2416 /* Special-case the error message for "ptr.field" for the case
2417 where the user has confused "." vs "->". */
2418 rich_location richloc (line_table, loc);
2419 /* "loc" should be the "." token. */
2420 richloc.add_fixit_replace (source_range::from_location (loc), "->");
2421 error_at_rich_loc (&richloc,
2422 "%qE is a pointer; did you mean to use %<->%>?",
2423 datum);
2424 return error_mark_node;
2426 else if (code != ERROR_MARK)
2427 error_at (loc,
2428 "request for member %qE in something not a structure or union",
2429 component);
2431 return error_mark_node;
2434 /* Given an expression PTR for a pointer, return an expression
2435 for the value pointed to.
2436 ERRORSTRING is the name of the operator to appear in error messages.
2438 LOC is the location to use for the generated tree. */
2440 tree
2441 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2443 tree pointer = default_conversion (ptr);
2444 tree type = TREE_TYPE (pointer);
2445 tree ref;
2447 if (TREE_CODE (type) == POINTER_TYPE)
2449 if (CONVERT_EXPR_P (pointer)
2450 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2452 /* If a warning is issued, mark it to avoid duplicates from
2453 the backend. This only needs to be done at
2454 warn_strict_aliasing > 2. */
2455 if (warn_strict_aliasing > 2)
2456 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2457 type, TREE_OPERAND (pointer, 0)))
2458 TREE_NO_WARNING (pointer) = 1;
2461 if (TREE_CODE (pointer) == ADDR_EXPR
2462 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2463 == TREE_TYPE (type)))
2465 ref = TREE_OPERAND (pointer, 0);
2466 protected_set_expr_location (ref, loc);
2467 return ref;
2469 else
2471 tree t = TREE_TYPE (type);
2473 ref = build1 (INDIRECT_REF, t, pointer);
2475 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2477 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2479 error_at (loc, "dereferencing pointer to incomplete type "
2480 "%qT", t);
2481 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2483 return error_mark_node;
2485 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2486 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2488 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2489 so that we get the proper error message if the result is used
2490 to assign to. Also, &* is supposed to be a no-op.
2491 And ANSI C seems to specify that the type of the result
2492 should be the const type. */
2493 /* A de-reference of a pointer to const is not a const. It is valid
2494 to change it via some other pointer. */
2495 TREE_READONLY (ref) = TYPE_READONLY (t);
2496 TREE_SIDE_EFFECTS (ref)
2497 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2498 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2499 protected_set_expr_location (ref, loc);
2500 return ref;
2503 else if (TREE_CODE (pointer) != ERROR_MARK)
2504 invalid_indirection_error (loc, type, errstring);
2506 return error_mark_node;
2509 /* This handles expressions of the form "a[i]", which denotes
2510 an array reference.
2512 This is logically equivalent in C to *(a+i), but we may do it differently.
2513 If A is a variable or a member, we generate a primitive ARRAY_REF.
2514 This avoids forcing the array out of registers, and can work on
2515 arrays that are not lvalues (for example, members of structures returned
2516 by functions).
2518 For vector types, allow vector[i] but not i[vector], and create
2519 *(((type*)&vectortype) + i) for the expression.
2521 LOC is the location to use for the returned expression. */
2523 tree
2524 build_array_ref (location_t loc, tree array, tree index)
2526 tree ret;
2527 bool swapped = false;
2528 if (TREE_TYPE (array) == error_mark_node
2529 || TREE_TYPE (index) == error_mark_node)
2530 return error_mark_node;
2532 if (flag_cilkplus && contains_array_notation_expr (index))
2534 size_t rank = 0;
2535 if (!find_rank (loc, index, index, true, &rank))
2536 return error_mark_node;
2537 if (rank > 1)
2539 error_at (loc, "rank of the array's index is greater than 1");
2540 return error_mark_node;
2543 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2544 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2545 /* Allow vector[index] but not index[vector]. */
2546 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2548 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2549 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2551 error_at (loc,
2552 "subscripted value is neither array nor pointer nor vector");
2554 return error_mark_node;
2556 std::swap (array, index);
2557 swapped = true;
2560 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2562 error_at (loc, "array subscript is not an integer");
2563 return error_mark_node;
2566 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2568 error_at (loc, "subscripted value is pointer to function");
2569 return error_mark_node;
2572 /* ??? Existing practice has been to warn only when the char
2573 index is syntactically the index, not for char[array]. */
2574 if (!swapped)
2575 warn_array_subscript_with_type_char (loc, index);
2577 /* Apply default promotions *after* noticing character types. */
2578 index = default_conversion (index);
2579 if (index == error_mark_node)
2580 return error_mark_node;
2582 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2584 bool non_lvalue
2585 = convert_vector_to_pointer_for_subscript (loc, &array, index);
2587 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2589 tree rval, type;
2591 /* An array that is indexed by a non-constant
2592 cannot be stored in a register; we must be able to do
2593 address arithmetic on its address.
2594 Likewise an array of elements of variable size. */
2595 if (TREE_CODE (index) != INTEGER_CST
2596 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2597 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2599 if (!c_mark_addressable (array))
2600 return error_mark_node;
2602 /* An array that is indexed by a constant value which is not within
2603 the array bounds cannot be stored in a register either; because we
2604 would get a crash in store_bit_field/extract_bit_field when trying
2605 to access a non-existent part of the register. */
2606 if (TREE_CODE (index) == INTEGER_CST
2607 && TYPE_DOMAIN (TREE_TYPE (array))
2608 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2610 if (!c_mark_addressable (array))
2611 return error_mark_node;
2614 if (pedantic || warn_c90_c99_compat)
2616 tree foo = array;
2617 while (TREE_CODE (foo) == COMPONENT_REF)
2618 foo = TREE_OPERAND (foo, 0);
2619 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2620 pedwarn (loc, OPT_Wpedantic,
2621 "ISO C forbids subscripting %<register%> array");
2622 else if (!lvalue_p (foo))
2623 pedwarn_c90 (loc, OPT_Wpedantic,
2624 "ISO C90 forbids subscripting non-lvalue "
2625 "array");
2628 type = TREE_TYPE (TREE_TYPE (array));
2629 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2630 /* Array ref is const/volatile if the array elements are
2631 or if the array is. */
2632 TREE_READONLY (rval)
2633 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2634 | TREE_READONLY (array));
2635 TREE_SIDE_EFFECTS (rval)
2636 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2637 | TREE_SIDE_EFFECTS (array));
2638 TREE_THIS_VOLATILE (rval)
2639 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2640 /* This was added by rms on 16 Nov 91.
2641 It fixes vol struct foo *a; a->elts[1]
2642 in an inline function.
2643 Hope it doesn't break something else. */
2644 | TREE_THIS_VOLATILE (array));
2645 ret = require_complete_type (rval);
2646 protected_set_expr_location (ret, loc);
2647 if (non_lvalue)
2648 ret = non_lvalue_loc (loc, ret);
2649 return ret;
2651 else
2653 tree ar = default_conversion (array);
2655 if (ar == error_mark_node)
2656 return ar;
2658 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2659 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2661 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2662 index, 0),
2663 RO_ARRAY_INDEXING);
2664 if (non_lvalue)
2665 ret = non_lvalue_loc (loc, ret);
2666 return ret;
2670 /* Build an external reference to identifier ID. FUN indicates
2671 whether this will be used for a function call. LOC is the source
2672 location of the identifier. This sets *TYPE to the type of the
2673 identifier, which is not the same as the type of the returned value
2674 for CONST_DECLs defined as enum constants. If the type of the
2675 identifier is not available, *TYPE is set to NULL. */
2676 tree
2677 build_external_ref (location_t loc, tree id, int fun, tree *type)
2679 tree ref;
2680 tree decl = lookup_name (id);
2682 /* In Objective-C, an instance variable (ivar) may be preferred to
2683 whatever lookup_name() found. */
2684 decl = objc_lookup_ivar (decl, id);
2686 *type = NULL;
2687 if (decl && decl != error_mark_node)
2689 ref = decl;
2690 *type = TREE_TYPE (ref);
2692 else if (fun)
2693 /* Implicit function declaration. */
2694 ref = implicitly_declare (loc, id);
2695 else if (decl == error_mark_node)
2696 /* Don't complain about something that's already been
2697 complained about. */
2698 return error_mark_node;
2699 else
2701 undeclared_variable (loc, id);
2702 return error_mark_node;
2705 if (TREE_TYPE (ref) == error_mark_node)
2706 return error_mark_node;
2708 if (TREE_DEPRECATED (ref))
2709 warn_deprecated_use (ref, NULL_TREE);
2711 /* Recursive call does not count as usage. */
2712 if (ref != current_function_decl)
2714 TREE_USED (ref) = 1;
2717 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2719 if (!in_sizeof && !in_typeof)
2720 C_DECL_USED (ref) = 1;
2721 else if (DECL_INITIAL (ref) == 0
2722 && DECL_EXTERNAL (ref)
2723 && !TREE_PUBLIC (ref))
2724 record_maybe_used_decl (ref);
2727 if (TREE_CODE (ref) == CONST_DECL)
2729 used_types_insert (TREE_TYPE (ref));
2731 if (warn_cxx_compat
2732 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2733 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2735 warning_at (loc, OPT_Wc___compat,
2736 ("enum constant defined in struct or union "
2737 "is not visible in C++"));
2738 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2741 ref = DECL_INITIAL (ref);
2742 TREE_CONSTANT (ref) = 1;
2744 else if (current_function_decl != 0
2745 && !DECL_FILE_SCOPE_P (current_function_decl)
2746 && (VAR_OR_FUNCTION_DECL_P (ref)
2747 || TREE_CODE (ref) == PARM_DECL))
2749 tree context = decl_function_context (ref);
2751 if (context != 0 && context != current_function_decl)
2752 DECL_NONLOCAL (ref) = 1;
2754 /* C99 6.7.4p3: An inline definition of a function with external
2755 linkage ... shall not contain a reference to an identifier with
2756 internal linkage. */
2757 else if (current_function_decl != 0
2758 && DECL_DECLARED_INLINE_P (current_function_decl)
2759 && DECL_EXTERNAL (current_function_decl)
2760 && VAR_OR_FUNCTION_DECL_P (ref)
2761 && (!VAR_P (ref) || TREE_STATIC (ref))
2762 && ! TREE_PUBLIC (ref)
2763 && DECL_CONTEXT (ref) != current_function_decl)
2764 record_inline_static (loc, current_function_decl, ref,
2765 csi_internal);
2767 return ref;
2770 /* Record details of decls possibly used inside sizeof or typeof. */
2771 struct maybe_used_decl
2773 /* The decl. */
2774 tree decl;
2775 /* The level seen at (in_sizeof + in_typeof). */
2776 int level;
2777 /* The next one at this level or above, or NULL. */
2778 struct maybe_used_decl *next;
2781 static struct maybe_used_decl *maybe_used_decls;
2783 /* Record that DECL, an undefined static function reference seen
2784 inside sizeof or typeof, might be used if the operand of sizeof is
2785 a VLA type or the operand of typeof is a variably modified
2786 type. */
2788 static void
2789 record_maybe_used_decl (tree decl)
2791 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2792 t->decl = decl;
2793 t->level = in_sizeof + in_typeof;
2794 t->next = maybe_used_decls;
2795 maybe_used_decls = t;
2798 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2799 USED is false, just discard them. If it is true, mark them used
2800 (if no longer inside sizeof or typeof) or move them to the next
2801 level up (if still inside sizeof or typeof). */
2803 void
2804 pop_maybe_used (bool used)
2806 struct maybe_used_decl *p = maybe_used_decls;
2807 int cur_level = in_sizeof + in_typeof;
2808 while (p && p->level > cur_level)
2810 if (used)
2812 if (cur_level == 0)
2813 C_DECL_USED (p->decl) = 1;
2814 else
2815 p->level = cur_level;
2817 p = p->next;
2819 if (!used || cur_level == 0)
2820 maybe_used_decls = p;
2823 /* Return the result of sizeof applied to EXPR. */
2825 struct c_expr
2826 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2828 struct c_expr ret;
2829 if (expr.value == error_mark_node)
2831 ret.value = error_mark_node;
2832 ret.original_code = ERROR_MARK;
2833 ret.original_type = NULL;
2834 pop_maybe_used (false);
2836 else
2838 bool expr_const_operands = true;
2840 if (TREE_CODE (expr.value) == PARM_DECL
2841 && C_ARRAY_PARAMETER (expr.value))
2843 if (warning_at (loc, OPT_Wsizeof_array_argument,
2844 "%<sizeof%> on array function parameter %qE will "
2845 "return size of %qT", expr.value,
2846 expr.original_type))
2847 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2849 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2850 &expr_const_operands);
2851 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2852 c_last_sizeof_arg = expr.value;
2853 ret.original_code = SIZEOF_EXPR;
2854 ret.original_type = NULL;
2855 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2857 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2858 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2859 folded_expr, ret.value);
2860 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2861 SET_EXPR_LOCATION (ret.value, loc);
2863 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2865 return ret;
2868 /* Return the result of sizeof applied to T, a structure for the type
2869 name passed to sizeof (rather than the type itself). LOC is the
2870 location of the original expression. */
2872 struct c_expr
2873 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2875 tree type;
2876 struct c_expr ret;
2877 tree type_expr = NULL_TREE;
2878 bool type_expr_const = true;
2879 type = groktypename (t, &type_expr, &type_expr_const);
2880 ret.value = c_sizeof (loc, type);
2881 c_last_sizeof_arg = type;
2882 ret.original_code = SIZEOF_EXPR;
2883 ret.original_type = NULL;
2884 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2885 && c_vla_type_p (type))
2887 /* If the type is a [*] array, it is a VLA but is represented as
2888 having a size of zero. In such a case we must ensure that
2889 the result of sizeof does not get folded to a constant by
2890 c_fully_fold, because if the size is evaluated the result is
2891 not constant and so constraints on zero or negative size
2892 arrays must not be applied when this sizeof call is inside
2893 another array declarator. */
2894 if (!type_expr)
2895 type_expr = integer_zero_node;
2896 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2897 type_expr, ret.value);
2898 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2900 pop_maybe_used (type != error_mark_node
2901 ? C_TYPE_VARIABLE_SIZE (type) : false);
2902 return ret;
2905 /* Build a function call to function FUNCTION with parameters PARAMS.
2906 The function call is at LOC.
2907 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2908 TREE_VALUE of each node is a parameter-expression.
2909 FUNCTION's data type may be a function type or a pointer-to-function. */
2911 tree
2912 build_function_call (location_t loc, tree function, tree params)
2914 vec<tree, va_gc> *v;
2915 tree ret;
2917 vec_alloc (v, list_length (params));
2918 for (; params; params = TREE_CHAIN (params))
2919 v->quick_push (TREE_VALUE (params));
2920 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2921 vec_free (v);
2922 return ret;
2925 /* Give a note about the location of the declaration of DECL. */
2927 static void
2928 inform_declaration (tree decl)
2930 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2931 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2934 /* Build a function call to function FUNCTION with parameters PARAMS.
2935 ORIGTYPES, if not NULL, is a vector of types; each element is
2936 either NULL or the original type of the corresponding element in
2937 PARAMS. The original type may differ from TREE_TYPE of the
2938 parameter for enums. FUNCTION's data type may be a function type
2939 or pointer-to-function. This function changes the elements of
2940 PARAMS. */
2942 tree
2943 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2944 tree function, vec<tree, va_gc> *params,
2945 vec<tree, va_gc> *origtypes)
2947 tree fntype, fundecl = 0;
2948 tree name = NULL_TREE, result;
2949 tree tem;
2950 int nargs;
2951 tree *argarray;
2954 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2955 STRIP_TYPE_NOPS (function);
2957 /* Convert anything with function type to a pointer-to-function. */
2958 if (TREE_CODE (function) == FUNCTION_DECL)
2960 name = DECL_NAME (function);
2962 if (flag_tm)
2963 tm_malloc_replacement (function);
2964 fundecl = function;
2965 /* Atomic functions have type checking/casting already done. They are
2966 often rewritten and don't match the original parameter list. */
2967 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2968 origtypes = NULL;
2970 if (flag_cilkplus
2971 && is_cilkplus_reduce_builtin (function))
2972 origtypes = NULL;
2974 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2975 function = function_to_pointer_conversion (loc, function);
2977 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2978 expressions, like those used for ObjC messenger dispatches. */
2979 if (params && !params->is_empty ())
2980 function = objc_rewrite_function_call (function, (*params)[0]);
2982 function = c_fully_fold (function, false, NULL);
2984 fntype = TREE_TYPE (function);
2986 if (TREE_CODE (fntype) == ERROR_MARK)
2987 return error_mark_node;
2989 if (!(TREE_CODE (fntype) == POINTER_TYPE
2990 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2992 if (!flag_diagnostics_show_caret)
2993 error_at (loc,
2994 "called object %qE is not a function or function pointer",
2995 function);
2996 else if (DECL_P (function))
2998 error_at (loc,
2999 "called object %qD is not a function or function pointer",
3000 function);
3001 inform_declaration (function);
3003 else
3004 error_at (loc,
3005 "called object is not a function or function pointer");
3006 return error_mark_node;
3009 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3010 current_function_returns_abnormally = 1;
3012 /* fntype now gets the type of function pointed to. */
3013 fntype = TREE_TYPE (fntype);
3015 /* Convert the parameters to the types declared in the
3016 function prototype, or apply default promotions. */
3018 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3019 origtypes, function, fundecl);
3020 if (nargs < 0)
3021 return error_mark_node;
3023 /* Check that the function is called through a compatible prototype.
3024 If it is not, warn. */
3025 if (CONVERT_EXPR_P (function)
3026 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3027 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3028 && !comptypes (fntype, TREE_TYPE (tem)))
3030 tree return_type = TREE_TYPE (fntype);
3032 /* This situation leads to run-time undefined behavior. We can't,
3033 therefore, simply error unless we can prove that all possible
3034 executions of the program must execute the code. */
3035 warning_at (loc, 0, "function called through a non-compatible type");
3037 if (VOID_TYPE_P (return_type)
3038 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3039 pedwarn (loc, 0,
3040 "function with qualified void return type called");
3043 argarray = vec_safe_address (params);
3045 /* Check that arguments to builtin functions match the expectations. */
3046 if (fundecl
3047 && DECL_BUILT_IN (fundecl)
3048 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3049 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3050 argarray))
3051 return error_mark_node;
3053 /* Check that the arguments to the function are valid. */
3054 check_function_arguments (loc, fntype, nargs, argarray);
3056 if (name != NULL_TREE
3057 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3059 if (require_constant_value)
3060 result =
3061 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3062 function, nargs, argarray);
3063 else
3064 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3065 function, nargs, argarray);
3066 if (TREE_CODE (result) == NOP_EXPR
3067 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3068 STRIP_TYPE_NOPS (result);
3070 else
3071 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3072 function, nargs, argarray);
3074 /* In this improbable scenario, a nested function returns a VM type.
3075 Create a TARGET_EXPR so that the call always has a LHS, much as
3076 what the C++ FE does for functions returning non-PODs. */
3077 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3079 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3080 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3081 NULL_TREE, NULL_TREE);
3084 if (VOID_TYPE_P (TREE_TYPE (result)))
3086 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3087 pedwarn (loc, 0,
3088 "function with qualified void return type called");
3089 return result;
3091 return require_complete_type (result);
3094 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3096 tree
3097 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3098 tree function, vec<tree, va_gc> *params,
3099 vec<tree, va_gc> *origtypes)
3101 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3102 STRIP_TYPE_NOPS (function);
3104 /* Convert anything with function type to a pointer-to-function. */
3105 if (TREE_CODE (function) == FUNCTION_DECL)
3107 /* Implement type-directed function overloading for builtins.
3108 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3109 handle all the type checking. The result is a complete expression
3110 that implements this function call. */
3111 tree tem = resolve_overloaded_builtin (loc, function, params);
3112 if (tem)
3113 return tem;
3115 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3118 /* Convert the argument expressions in the vector VALUES
3119 to the types in the list TYPELIST.
3121 If TYPELIST is exhausted, or when an element has NULL as its type,
3122 perform the default conversions.
3124 ORIGTYPES is the original types of the expressions in VALUES. This
3125 holds the type of enum values which have been converted to integral
3126 types. It may be NULL.
3128 FUNCTION is a tree for the called function. It is used only for
3129 error messages, where it is formatted with %qE.
3131 This is also where warnings about wrong number of args are generated.
3133 ARG_LOC are locations of function arguments (if any).
3135 Returns the actual number of arguments processed (which may be less
3136 than the length of VALUES in some error situations), or -1 on
3137 failure. */
3139 static int
3140 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3141 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3142 tree function, tree fundecl)
3144 tree typetail, val;
3145 unsigned int parmnum;
3146 bool error_args = false;
3147 const bool type_generic = fundecl
3148 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3149 bool type_generic_remove_excess_precision = false;
3150 tree selector;
3152 /* Change pointer to function to the function itself for
3153 diagnostics. */
3154 if (TREE_CODE (function) == ADDR_EXPR
3155 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3156 function = TREE_OPERAND (function, 0);
3158 /* Handle an ObjC selector specially for diagnostics. */
3159 selector = objc_message_selector ();
3161 /* For type-generic built-in functions, determine whether excess
3162 precision should be removed (classification) or not
3163 (comparison). */
3164 if (type_generic
3165 && DECL_BUILT_IN (fundecl)
3166 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3168 switch (DECL_FUNCTION_CODE (fundecl))
3170 case BUILT_IN_ISFINITE:
3171 case BUILT_IN_ISINF:
3172 case BUILT_IN_ISINF_SIGN:
3173 case BUILT_IN_ISNAN:
3174 case BUILT_IN_ISNORMAL:
3175 case BUILT_IN_FPCLASSIFY:
3176 type_generic_remove_excess_precision = true;
3177 break;
3179 default:
3180 type_generic_remove_excess_precision = false;
3181 break;
3184 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3185 return vec_safe_length (values);
3187 /* Scan the given expressions and types, producing individual
3188 converted arguments. */
3190 for (typetail = typelist, parmnum = 0;
3191 values && values->iterate (parmnum, &val);
3192 ++parmnum)
3194 tree type = typetail ? TREE_VALUE (typetail) : 0;
3195 tree valtype = TREE_TYPE (val);
3196 tree rname = function;
3197 int argnum = parmnum + 1;
3198 const char *invalid_func_diag;
3199 bool excess_precision = false;
3200 bool npc;
3201 tree parmval;
3202 /* Some __atomic_* builtins have additional hidden argument at
3203 position 0. */
3204 location_t ploc
3205 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3206 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3207 : input_location;
3209 if (type == void_type_node)
3211 if (selector)
3212 error_at (loc, "too many arguments to method %qE", selector);
3213 else
3214 error_at (loc, "too many arguments to function %qE", function);
3215 inform_declaration (fundecl);
3216 return error_args ? -1 : (int) parmnum;
3219 if (selector && argnum > 2)
3221 rname = selector;
3222 argnum -= 2;
3225 npc = null_pointer_constant_p (val);
3227 /* If there is excess precision and a prototype, convert once to
3228 the required type rather than converting via the semantic
3229 type. Likewise without a prototype a float value represented
3230 as long double should be converted once to double. But for
3231 type-generic classification functions excess precision must
3232 be removed here. */
3233 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3234 && (type || !type_generic || !type_generic_remove_excess_precision))
3236 val = TREE_OPERAND (val, 0);
3237 excess_precision = true;
3239 val = c_fully_fold (val, false, NULL);
3240 STRIP_TYPE_NOPS (val);
3242 val = require_complete_type (val);
3244 if (type != 0)
3246 /* Formal parm type is specified by a function prototype. */
3248 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3250 error_at (ploc, "type of formal parameter %d is incomplete",
3251 parmnum + 1);
3252 parmval = val;
3254 else
3256 tree origtype;
3258 /* Optionally warn about conversions that
3259 differ from the default conversions. */
3260 if (warn_traditional_conversion || warn_traditional)
3262 unsigned int formal_prec = TYPE_PRECISION (type);
3264 if (INTEGRAL_TYPE_P (type)
3265 && TREE_CODE (valtype) == REAL_TYPE)
3266 warning_at (ploc, OPT_Wtraditional_conversion,
3267 "passing argument %d of %qE as integer rather "
3268 "than floating due to prototype",
3269 argnum, rname);
3270 if (INTEGRAL_TYPE_P (type)
3271 && TREE_CODE (valtype) == COMPLEX_TYPE)
3272 warning_at (ploc, OPT_Wtraditional_conversion,
3273 "passing argument %d of %qE as integer rather "
3274 "than complex due to prototype",
3275 argnum, rname);
3276 else if (TREE_CODE (type) == COMPLEX_TYPE
3277 && TREE_CODE (valtype) == REAL_TYPE)
3278 warning_at (ploc, OPT_Wtraditional_conversion,
3279 "passing argument %d of %qE as complex rather "
3280 "than floating due to prototype",
3281 argnum, rname);
3282 else if (TREE_CODE (type) == REAL_TYPE
3283 && INTEGRAL_TYPE_P (valtype))
3284 warning_at (ploc, OPT_Wtraditional_conversion,
3285 "passing argument %d of %qE as floating rather "
3286 "than integer due to prototype",
3287 argnum, rname);
3288 else if (TREE_CODE (type) == COMPLEX_TYPE
3289 && INTEGRAL_TYPE_P (valtype))
3290 warning_at (ploc, OPT_Wtraditional_conversion,
3291 "passing argument %d of %qE as complex rather "
3292 "than integer due to prototype",
3293 argnum, rname);
3294 else if (TREE_CODE (type) == REAL_TYPE
3295 && TREE_CODE (valtype) == COMPLEX_TYPE)
3296 warning_at (ploc, OPT_Wtraditional_conversion,
3297 "passing argument %d of %qE as floating rather "
3298 "than complex due to prototype",
3299 argnum, rname);
3300 /* ??? At some point, messages should be written about
3301 conversions between complex types, but that's too messy
3302 to do now. */
3303 else if (TREE_CODE (type) == REAL_TYPE
3304 && TREE_CODE (valtype) == REAL_TYPE)
3306 /* Warn if any argument is passed as `float',
3307 since without a prototype it would be `double'. */
3308 if (formal_prec == TYPE_PRECISION (float_type_node)
3309 && type != dfloat32_type_node)
3310 warning_at (ploc, 0,
3311 "passing argument %d of %qE as %<float%> "
3312 "rather than %<double%> due to prototype",
3313 argnum, rname);
3315 /* Warn if mismatch between argument and prototype
3316 for decimal float types. Warn of conversions with
3317 binary float types and of precision narrowing due to
3318 prototype. */
3319 else if (type != valtype
3320 && (type == dfloat32_type_node
3321 || type == dfloat64_type_node
3322 || type == dfloat128_type_node
3323 || valtype == dfloat32_type_node
3324 || valtype == dfloat64_type_node
3325 || valtype == dfloat128_type_node)
3326 && (formal_prec
3327 <= TYPE_PRECISION (valtype)
3328 || (type == dfloat128_type_node
3329 && (valtype
3330 != dfloat64_type_node
3331 && (valtype
3332 != dfloat32_type_node)))
3333 || (type == dfloat64_type_node
3334 && (valtype
3335 != dfloat32_type_node))))
3336 warning_at (ploc, 0,
3337 "passing argument %d of %qE as %qT "
3338 "rather than %qT due to prototype",
3339 argnum, rname, type, valtype);
3342 /* Detect integer changing in width or signedness.
3343 These warnings are only activated with
3344 -Wtraditional-conversion, not with -Wtraditional. */
3345 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3346 && INTEGRAL_TYPE_P (valtype))
3348 tree would_have_been = default_conversion (val);
3349 tree type1 = TREE_TYPE (would_have_been);
3351 if (TREE_CODE (type) == ENUMERAL_TYPE
3352 && (TYPE_MAIN_VARIANT (type)
3353 == TYPE_MAIN_VARIANT (valtype)))
3354 /* No warning if function asks for enum
3355 and the actual arg is that enum type. */
3357 else if (formal_prec != TYPE_PRECISION (type1))
3358 warning_at (ploc, OPT_Wtraditional_conversion,
3359 "passing argument %d of %qE "
3360 "with different width due to prototype",
3361 argnum, rname);
3362 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3364 /* Don't complain if the formal parameter type
3365 is an enum, because we can't tell now whether
3366 the value was an enum--even the same enum. */
3367 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3369 else if (TREE_CODE (val) == INTEGER_CST
3370 && int_fits_type_p (val, type))
3371 /* Change in signedness doesn't matter
3372 if a constant value is unaffected. */
3374 /* If the value is extended from a narrower
3375 unsigned type, it doesn't matter whether we
3376 pass it as signed or unsigned; the value
3377 certainly is the same either way. */
3378 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3379 && TYPE_UNSIGNED (valtype))
3381 else if (TYPE_UNSIGNED (type))
3382 warning_at (ploc, OPT_Wtraditional_conversion,
3383 "passing argument %d of %qE "
3384 "as unsigned due to prototype",
3385 argnum, rname);
3386 else
3387 warning_at (ploc, OPT_Wtraditional_conversion,
3388 "passing argument %d of %qE "
3389 "as signed due to prototype",
3390 argnum, rname);
3394 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3395 sake of better warnings from convert_and_check. */
3396 if (excess_precision)
3397 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3398 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3399 parmval = convert_for_assignment (loc, ploc, type,
3400 val, origtype, ic_argpass,
3401 npc, fundecl, function,
3402 parmnum + 1);
3404 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3405 && INTEGRAL_TYPE_P (type)
3406 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3407 parmval = default_conversion (parmval);
3410 else if (TREE_CODE (valtype) == REAL_TYPE
3411 && (TYPE_PRECISION (valtype)
3412 <= TYPE_PRECISION (double_type_node))
3413 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3414 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3415 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3417 if (type_generic)
3418 parmval = val;
3419 else
3421 /* Convert `float' to `double'. */
3422 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3423 warning_at (ploc, OPT_Wdouble_promotion,
3424 "implicit conversion from %qT to %qT when passing "
3425 "argument to function",
3426 valtype, double_type_node);
3427 parmval = convert (double_type_node, val);
3430 else if (excess_precision && !type_generic)
3431 /* A "double" argument with excess precision being passed
3432 without a prototype or in variable arguments. */
3433 parmval = convert (valtype, val);
3434 else if ((invalid_func_diag =
3435 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3437 error (invalid_func_diag);
3438 return -1;
3440 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3442 return -1;
3444 else
3445 /* Convert `short' and `char' to full-size `int'. */
3446 parmval = default_conversion (val);
3448 (*values)[parmnum] = parmval;
3449 if (parmval == error_mark_node)
3450 error_args = true;
3452 if (typetail)
3453 typetail = TREE_CHAIN (typetail);
3456 gcc_assert (parmnum == vec_safe_length (values));
3458 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3460 error_at (loc, "too few arguments to function %qE", function);
3461 inform_declaration (fundecl);
3462 return -1;
3465 return error_args ? -1 : (int) parmnum;
3468 /* This is the entry point used by the parser to build unary operators
3469 in the input. CODE, a tree_code, specifies the unary operator, and
3470 ARG is the operand. For unary plus, the C parser currently uses
3471 CONVERT_EXPR for code.
3473 LOC is the location to use for the tree generated.
3476 struct c_expr
3477 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3479 struct c_expr result;
3481 result.original_code = code;
3482 result.original_type = NULL;
3484 if (reject_gcc_builtin (arg.value))
3486 result.value = error_mark_node;
3488 else
3490 result.value = build_unary_op (loc, code, arg.value, 0);
3492 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3493 overflow_warning (loc, result.value);
3496 /* We are typically called when parsing a prefix token at LOC acting on
3497 ARG. Reflect this by updating the source range of the result to
3498 start at LOC and end at the end of ARG. */
3499 set_c_expr_source_range (&result,
3500 loc, arg.get_finish ());
3502 return result;
3505 /* This is the entry point used by the parser to build binary operators
3506 in the input. CODE, a tree_code, specifies the binary operator, and
3507 ARG1 and ARG2 are the operands. In addition to constructing the
3508 expression, we check for operands that were written with other binary
3509 operators in a way that is likely to confuse the user.
3511 LOCATION is the location of the binary operator. */
3513 struct c_expr
3514 parser_build_binary_op (location_t location, enum tree_code code,
3515 struct c_expr arg1, struct c_expr arg2)
3517 struct c_expr result;
3519 enum tree_code code1 = arg1.original_code;
3520 enum tree_code code2 = arg2.original_code;
3521 tree type1 = (arg1.original_type
3522 ? arg1.original_type
3523 : TREE_TYPE (arg1.value));
3524 tree type2 = (arg2.original_type
3525 ? arg2.original_type
3526 : TREE_TYPE (arg2.value));
3528 result.value = build_binary_op (location, code,
3529 arg1.value, arg2.value, 1);
3530 result.original_code = code;
3531 result.original_type = NULL;
3533 if (TREE_CODE (result.value) == ERROR_MARK)
3534 return result;
3536 if (location != UNKNOWN_LOCATION)
3537 protected_set_expr_location (result.value, location);
3539 set_c_expr_source_range (&result,
3540 arg1.get_start (),
3541 arg2.get_finish ());
3543 /* Check for cases such as x+y<<z which users are likely
3544 to misinterpret. */
3545 if (warn_parentheses)
3546 warn_about_parentheses (location, code, code1, arg1.value, code2,
3547 arg2.value);
3549 if (warn_logical_op)
3550 warn_logical_operator (location, code, TREE_TYPE (result.value),
3551 code1, arg1.value, code2, arg2.value);
3553 if (warn_tautological_compare)
3555 tree lhs = arg1.value;
3556 tree rhs = arg2.value;
3557 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3559 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3560 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3561 lhs = NULL_TREE;
3562 else
3563 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3565 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3567 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3568 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3569 rhs = NULL_TREE;
3570 else
3571 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3573 if (lhs != NULL_TREE && rhs != NULL_TREE)
3574 warn_tautological_cmp (location, code, lhs, rhs);
3577 if (warn_logical_not_paren
3578 && TREE_CODE_CLASS (code) == tcc_comparison
3579 && code1 == TRUTH_NOT_EXPR
3580 && code2 != TRUTH_NOT_EXPR
3581 /* Avoid warning for !!x == y. */
3582 && (TREE_CODE (arg1.value) != NE_EXPR
3583 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3585 /* Avoid warning for !b == y where b has _Bool type. */
3586 tree t = integer_zero_node;
3587 if (TREE_CODE (arg1.value) == EQ_EXPR
3588 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3589 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3591 t = TREE_OPERAND (arg1.value, 0);
3594 if (TREE_TYPE (t) != integer_type_node)
3595 break;
3596 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3597 t = C_MAYBE_CONST_EXPR_EXPR (t);
3598 else if (CONVERT_EXPR_P (t))
3599 t = TREE_OPERAND (t, 0);
3600 else
3601 break;
3603 while (1);
3605 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3606 warn_logical_not_parentheses (location, code, arg2.value);
3609 /* Warn about comparisons against string literals, with the exception
3610 of testing for equality or inequality of a string literal with NULL. */
3611 if (code == EQ_EXPR || code == NE_EXPR)
3613 if ((code1 == STRING_CST
3614 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3615 || (code2 == STRING_CST
3616 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3617 warning_at (location, OPT_Waddress,
3618 "comparison with string literal results in unspecified behavior");
3620 else if (TREE_CODE_CLASS (code) == tcc_comparison
3621 && (code1 == STRING_CST || code2 == STRING_CST))
3622 warning_at (location, OPT_Waddress,
3623 "comparison with string literal results in unspecified behavior");
3625 if (TREE_OVERFLOW_P (result.value)
3626 && !TREE_OVERFLOW_P (arg1.value)
3627 && !TREE_OVERFLOW_P (arg2.value))
3628 overflow_warning (location, result.value);
3630 /* Warn about comparisons of different enum types. */
3631 if (warn_enum_compare
3632 && TREE_CODE_CLASS (code) == tcc_comparison
3633 && TREE_CODE (type1) == ENUMERAL_TYPE
3634 && TREE_CODE (type2) == ENUMERAL_TYPE
3635 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3636 warning_at (location, OPT_Wenum_compare,
3637 "comparison between %qT and %qT",
3638 type1, type2);
3640 return result;
3643 /* Return a tree for the difference of pointers OP0 and OP1.
3644 The resulting tree has type int. */
3646 static tree
3647 pointer_diff (location_t loc, tree op0, tree op1)
3649 tree restype = ptrdiff_type_node;
3650 tree result, inttype;
3652 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3653 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3654 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3655 tree orig_op1 = op1;
3657 /* If the operands point into different address spaces, we need to
3658 explicitly convert them to pointers into the common address space
3659 before we can subtract the numerical address values. */
3660 if (as0 != as1)
3662 addr_space_t as_common;
3663 tree common_type;
3665 /* Determine the common superset address space. This is guaranteed
3666 to exist because the caller verified that comp_target_types
3667 returned non-zero. */
3668 if (!addr_space_superset (as0, as1, &as_common))
3669 gcc_unreachable ();
3671 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3672 op0 = convert (common_type, op0);
3673 op1 = convert (common_type, op1);
3676 /* Determine integer type to perform computations in. This will usually
3677 be the same as the result type (ptrdiff_t), but may need to be a wider
3678 type if pointers for the address space are wider than ptrdiff_t. */
3679 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3680 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3681 else
3682 inttype = restype;
3684 if (TREE_CODE (target_type) == VOID_TYPE)
3685 pedwarn (loc, OPT_Wpointer_arith,
3686 "pointer of type %<void *%> used in subtraction");
3687 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3688 pedwarn (loc, OPT_Wpointer_arith,
3689 "pointer to a function used in subtraction");
3691 /* First do the subtraction as integers;
3692 then drop through to build the divide operator.
3693 Do not do default conversions on the minus operator
3694 in case restype is a short type. */
3696 op0 = build_binary_op (loc,
3697 MINUS_EXPR, convert (inttype, op0),
3698 convert (inttype, op1), 0);
3699 /* This generates an error if op1 is pointer to incomplete type. */
3700 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3701 error_at (loc, "arithmetic on pointer to an incomplete type");
3703 op1 = c_size_in_bytes (target_type);
3705 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3706 error_at (loc, "arithmetic on pointer to an empty aggregate");
3708 /* Divide by the size, in easiest possible way. */
3709 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3710 op0, convert (inttype, op1));
3712 /* Convert to final result type if necessary. */
3713 return convert (restype, result);
3716 /* Expand atomic compound assignments into an appropriate sequence as
3717 specified by the C11 standard section 6.5.16.2.
3719 _Atomic T1 E1
3720 T2 E2
3721 E1 op= E2
3723 This sequence is used for all types for which these operations are
3724 supported.
3726 In addition, built-in versions of the 'fe' prefixed routines may
3727 need to be invoked for floating point (real, complex or vector) when
3728 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3730 T1 newval;
3731 T1 old;
3732 T1 *addr
3733 T2 val
3734 fenv_t fenv
3736 addr = &E1;
3737 val = (E2);
3738 __atomic_load (addr, &old, SEQ_CST);
3739 feholdexcept (&fenv);
3740 loop:
3741 newval = old op val;
3742 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3743 SEQ_CST))
3744 goto done;
3745 feclearexcept (FE_ALL_EXCEPT);
3746 goto loop:
3747 done:
3748 feupdateenv (&fenv);
3750 The compiler will issue the __atomic_fetch_* built-in when possible,
3751 otherwise it will generate the generic form of the atomic operations.
3752 This requires temp(s) and has their address taken. The atomic processing
3753 is smart enough to figure out when the size of an object can utilize
3754 a lock-free version, and convert the built-in call to the appropriate
3755 lock-free routine. The optimizers will then dispose of any temps that
3756 are no longer required, and lock-free implementations are utilized as
3757 long as there is target support for the required size.
3759 If the operator is NOP_EXPR, then this is a simple assignment, and
3760 an __atomic_store is issued to perform the assignment rather than
3761 the above loop. */
3763 /* Build an atomic assignment at LOC, expanding into the proper
3764 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3765 the result of the operation, unless RETURN_OLD_P, in which case
3766 return the old value of LHS (this is only for postincrement and
3767 postdecrement). */
3769 static tree
3770 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3771 tree rhs, bool return_old_p)
3773 tree fndecl, func_call;
3774 vec<tree, va_gc> *params;
3775 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3776 tree old, old_addr;
3777 tree compound_stmt;
3778 tree stmt, goto_stmt;
3779 tree loop_label, loop_decl, done_label, done_decl;
3781 tree lhs_type = TREE_TYPE (lhs);
3782 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3783 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3784 tree rhs_type = TREE_TYPE (rhs);
3786 gcc_assert (TYPE_ATOMIC (lhs_type));
3788 if (return_old_p)
3789 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3791 /* Allocate enough vector items for a compare_exchange. */
3792 vec_alloc (params, 6);
3794 /* Create a compound statement to hold the sequence of statements
3795 with a loop. */
3796 compound_stmt = c_begin_compound_stmt (false);
3798 /* Fold the RHS if it hasn't already been folded. */
3799 if (modifycode != NOP_EXPR)
3800 rhs = c_fully_fold (rhs, false, NULL);
3802 /* Remove the qualifiers for the rest of the expressions and create
3803 the VAL temp variable to hold the RHS. */
3804 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3805 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3806 val = create_tmp_var_raw (nonatomic_rhs_type);
3807 TREE_ADDRESSABLE (val) = 1;
3808 TREE_NO_WARNING (val) = 1;
3809 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3810 NULL_TREE);
3811 SET_EXPR_LOCATION (rhs, loc);
3812 add_stmt (rhs);
3814 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3815 an atomic_store. */
3816 if (modifycode == NOP_EXPR)
3818 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3819 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3820 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3821 params->quick_push (lhs_addr);
3822 params->quick_push (rhs);
3823 params->quick_push (seq_cst);
3824 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3825 add_stmt (func_call);
3827 /* Finish the compound statement. */
3828 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3830 /* VAL is the value which was stored, return a COMPOUND_STMT of
3831 the statement and that value. */
3832 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3835 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3836 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3837 isn't applicable for such builtins. ??? Do we want to handle enums? */
3838 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3839 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3841 built_in_function fncode;
3842 switch (modifycode)
3844 case PLUS_EXPR:
3845 case POINTER_PLUS_EXPR:
3846 fncode = (return_old_p
3847 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3848 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3849 break;
3850 case MINUS_EXPR:
3851 fncode = (return_old_p
3852 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3853 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3854 break;
3855 case BIT_AND_EXPR:
3856 fncode = (return_old_p
3857 ? BUILT_IN_ATOMIC_FETCH_AND_N
3858 : BUILT_IN_ATOMIC_AND_FETCH_N);
3859 break;
3860 case BIT_IOR_EXPR:
3861 fncode = (return_old_p
3862 ? BUILT_IN_ATOMIC_FETCH_OR_N
3863 : BUILT_IN_ATOMIC_OR_FETCH_N);
3864 break;
3865 case BIT_XOR_EXPR:
3866 fncode = (return_old_p
3867 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3868 : BUILT_IN_ATOMIC_XOR_FETCH_N);
3869 break;
3870 default:
3871 goto cas_loop;
3874 /* We can only use "_1" through "_16" variants of the atomic fetch
3875 built-ins. */
3876 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
3877 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
3878 goto cas_loop;
3880 /* If this is a pointer type, we need to multiply by the size of
3881 the pointer target type. */
3882 if (POINTER_TYPE_P (lhs_type))
3884 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
3885 /* ??? This would introduce -Wdiscarded-qualifiers
3886 warning: __atomic_fetch_* expect volatile void *
3887 type as the first argument. (Assignments between
3888 atomic and non-atomic objects are OK.) */
3889 || TYPE_RESTRICT (lhs_type))
3890 goto cas_loop;
3891 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
3892 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
3893 convert (ptrdiff_type_node, rhs),
3894 convert (ptrdiff_type_node, sz));
3897 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
3898 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
3899 fndecl = builtin_decl_explicit (fncode);
3900 params->quick_push (lhs_addr);
3901 params->quick_push (rhs);
3902 params->quick_push (seq_cst);
3903 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3905 newval = create_tmp_var_raw (nonatomic_lhs_type);
3906 TREE_ADDRESSABLE (newval) = 1;
3907 TREE_NO_WARNING (newval) = 1;
3908 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
3909 NULL_TREE, NULL_TREE);
3910 SET_EXPR_LOCATION (rhs, loc);
3911 add_stmt (rhs);
3913 /* Finish the compound statement. */
3914 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3916 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
3917 the statement and that value. */
3918 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
3921 cas_loop:
3922 /* Create the variables and labels required for the op= form. */
3923 old = create_tmp_var_raw (nonatomic_lhs_type);
3924 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3925 TREE_ADDRESSABLE (old) = 1;
3926 TREE_NO_WARNING (old) = 1;
3928 newval = create_tmp_var_raw (nonatomic_lhs_type);
3929 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3930 TREE_ADDRESSABLE (newval) = 1;
3931 TREE_NO_WARNING (newval) = 1;
3933 loop_decl = create_artificial_label (loc);
3934 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3936 done_decl = create_artificial_label (loc);
3937 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3939 /* __atomic_load (addr, &old, SEQ_CST). */
3940 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3941 params->quick_push (lhs_addr);
3942 params->quick_push (old_addr);
3943 params->quick_push (seq_cst);
3944 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3945 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3946 NULL_TREE);
3947 add_stmt (old);
3948 params->truncate (0);
3950 /* Create the expressions for floating-point environment
3951 manipulation, if required. */
3952 bool need_fenv = (flag_trapping_math
3953 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3954 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3955 if (need_fenv)
3956 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3958 if (hold_call)
3959 add_stmt (hold_call);
3961 /* loop: */
3962 add_stmt (loop_label);
3964 /* newval = old + val; */
3965 rhs = build_binary_op (loc, modifycode, old, val, 1);
3966 rhs = c_fully_fold (rhs, false, NULL);
3967 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3968 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3969 NULL_TREE, 0);
3970 if (rhs != error_mark_node)
3972 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3973 NULL_TREE);
3974 SET_EXPR_LOCATION (rhs, loc);
3975 add_stmt (rhs);
3978 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3979 goto done; */
3980 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3981 params->quick_push (lhs_addr);
3982 params->quick_push (old_addr);
3983 params->quick_push (newval_addr);
3984 params->quick_push (integer_zero_node);
3985 params->quick_push (seq_cst);
3986 params->quick_push (seq_cst);
3987 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3989 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3990 SET_EXPR_LOCATION (goto_stmt, loc);
3992 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3993 SET_EXPR_LOCATION (stmt, loc);
3994 add_stmt (stmt);
3996 if (clear_call)
3997 add_stmt (clear_call);
3999 /* goto loop; */
4000 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4001 SET_EXPR_LOCATION (goto_stmt, loc);
4002 add_stmt (goto_stmt);
4004 /* done: */
4005 add_stmt (done_label);
4007 if (update_call)
4008 add_stmt (update_call);
4010 /* Finish the compound statement. */
4011 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4013 /* NEWVAL is the value that was successfully stored, return a
4014 COMPOUND_EXPR of the statement and the appropriate value. */
4015 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4016 return_old_p ? old : newval);
4019 /* Construct and perhaps optimize a tree representation
4020 for a unary operation. CODE, a tree_code, specifies the operation
4021 and XARG is the operand.
4022 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
4023 the default promotions (such as from short to int).
4024 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
4025 allows non-lvalues; this is only used to handle conversion of non-lvalue
4026 arrays to pointers in C99.
4028 LOCATION is the location of the operator. */
4030 tree
4031 build_unary_op (location_t location,
4032 enum tree_code code, tree xarg, int flag)
4034 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4035 tree arg = xarg;
4036 tree argtype = 0;
4037 enum tree_code typecode;
4038 tree val;
4039 tree ret = error_mark_node;
4040 tree eptype = NULL_TREE;
4041 int noconvert = flag;
4042 const char *invalid_op_diag;
4043 bool int_operands;
4045 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4046 if (int_operands)
4047 arg = remove_c_maybe_const_expr (arg);
4049 if (code != ADDR_EXPR)
4050 arg = require_complete_type (arg);
4052 typecode = TREE_CODE (TREE_TYPE (arg));
4053 if (typecode == ERROR_MARK)
4054 return error_mark_node;
4055 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4056 typecode = INTEGER_TYPE;
4058 if ((invalid_op_diag
4059 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4061 error_at (location, invalid_op_diag);
4062 return error_mark_node;
4065 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4067 eptype = TREE_TYPE (arg);
4068 arg = TREE_OPERAND (arg, 0);
4071 switch (code)
4073 case CONVERT_EXPR:
4074 /* This is used for unary plus, because a CONVERT_EXPR
4075 is enough to prevent anybody from looking inside for
4076 associativity, but won't generate any code. */
4077 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4078 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4079 || typecode == VECTOR_TYPE))
4081 error_at (location, "wrong type argument to unary plus");
4082 return error_mark_node;
4084 else if (!noconvert)
4085 arg = default_conversion (arg);
4086 arg = non_lvalue_loc (location, arg);
4087 break;
4089 case NEGATE_EXPR:
4090 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4091 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4092 || typecode == VECTOR_TYPE))
4094 error_at (location, "wrong type argument to unary minus");
4095 return error_mark_node;
4097 else if (!noconvert)
4098 arg = default_conversion (arg);
4099 break;
4101 case BIT_NOT_EXPR:
4102 /* ~ works on integer types and non float vectors. */
4103 if (typecode == INTEGER_TYPE
4104 || (typecode == VECTOR_TYPE
4105 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4107 if (!noconvert)
4108 arg = default_conversion (arg);
4110 else if (typecode == COMPLEX_TYPE)
4112 code = CONJ_EXPR;
4113 pedwarn (location, OPT_Wpedantic,
4114 "ISO C does not support %<~%> for complex conjugation");
4115 if (!noconvert)
4116 arg = default_conversion (arg);
4118 else
4120 error_at (location, "wrong type argument to bit-complement");
4121 return error_mark_node;
4123 break;
4125 case ABS_EXPR:
4126 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4128 error_at (location, "wrong type argument to abs");
4129 return error_mark_node;
4131 else if (!noconvert)
4132 arg = default_conversion (arg);
4133 break;
4135 case CONJ_EXPR:
4136 /* Conjugating a real value is a no-op, but allow it anyway. */
4137 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4138 || typecode == COMPLEX_TYPE))
4140 error_at (location, "wrong type argument to conjugation");
4141 return error_mark_node;
4143 else if (!noconvert)
4144 arg = default_conversion (arg);
4145 break;
4147 case TRUTH_NOT_EXPR:
4148 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4149 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4150 && typecode != COMPLEX_TYPE)
4152 error_at (location,
4153 "wrong type argument to unary exclamation mark");
4154 return error_mark_node;
4156 if (int_operands)
4158 arg = c_objc_common_truthvalue_conversion (location, xarg);
4159 arg = remove_c_maybe_const_expr (arg);
4161 else
4162 arg = c_objc_common_truthvalue_conversion (location, arg);
4163 ret = invert_truthvalue_loc (location, arg);
4164 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4165 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4166 location = EXPR_LOCATION (ret);
4167 goto return_build_unary_op;
4169 case REALPART_EXPR:
4170 case IMAGPART_EXPR:
4171 ret = build_real_imag_expr (location, code, arg);
4172 if (ret == error_mark_node)
4173 return error_mark_node;
4174 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4175 eptype = TREE_TYPE (eptype);
4176 goto return_build_unary_op;
4178 case PREINCREMENT_EXPR:
4179 case POSTINCREMENT_EXPR:
4180 case PREDECREMENT_EXPR:
4181 case POSTDECREMENT_EXPR:
4183 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4185 tree inner = build_unary_op (location, code,
4186 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4187 if (inner == error_mark_node)
4188 return error_mark_node;
4189 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4190 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4191 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4192 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4193 goto return_build_unary_op;
4196 /* Complain about anything that is not a true lvalue. In
4197 Objective-C, skip this check for property_refs. */
4198 if (!objc_is_property_ref (arg)
4199 && !lvalue_or_else (location,
4200 arg, ((code == PREINCREMENT_EXPR
4201 || code == POSTINCREMENT_EXPR)
4202 ? lv_increment
4203 : lv_decrement)))
4204 return error_mark_node;
4206 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4208 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4209 warning_at (location, OPT_Wc___compat,
4210 "increment of enumeration value is invalid in C++");
4211 else
4212 warning_at (location, OPT_Wc___compat,
4213 "decrement of enumeration value is invalid in C++");
4216 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4217 arg = c_fully_fold (arg, false, NULL);
4219 bool atomic_op;
4220 atomic_op = really_atomic_lvalue (arg);
4222 /* Increment or decrement the real part of the value,
4223 and don't change the imaginary part. */
4224 if (typecode == COMPLEX_TYPE)
4226 tree real, imag;
4228 pedwarn (location, OPT_Wpedantic,
4229 "ISO C does not support %<++%> and %<--%> on complex types");
4231 if (!atomic_op)
4233 arg = stabilize_reference (arg);
4234 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4235 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4236 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4237 if (real == error_mark_node || imag == error_mark_node)
4238 return error_mark_node;
4239 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4240 real, imag);
4241 goto return_build_unary_op;
4245 /* Report invalid types. */
4247 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4248 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4249 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4251 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4252 error_at (location, "wrong type argument to increment");
4253 else
4254 error_at (location, "wrong type argument to decrement");
4256 return error_mark_node;
4260 tree inc;
4262 argtype = TREE_TYPE (arg);
4264 /* Compute the increment. */
4266 if (typecode == POINTER_TYPE)
4268 /* If pointer target is an incomplete type,
4269 we just cannot know how to do the arithmetic. */
4270 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4272 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4273 error_at (location,
4274 "increment of pointer to an incomplete type %qT",
4275 TREE_TYPE (argtype));
4276 else
4277 error_at (location,
4278 "decrement of pointer to an incomplete type %qT",
4279 TREE_TYPE (argtype));
4281 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4282 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4284 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4285 pedwarn (location, OPT_Wpointer_arith,
4286 "wrong type argument to increment");
4287 else
4288 pedwarn (location, OPT_Wpointer_arith,
4289 "wrong type argument to decrement");
4292 inc = c_size_in_bytes (TREE_TYPE (argtype));
4293 inc = convert_to_ptrofftype_loc (location, inc);
4295 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4297 /* For signed fract types, we invert ++ to -- or
4298 -- to ++, and change inc from 1 to -1, because
4299 it is not possible to represent 1 in signed fract constants.
4300 For unsigned fract types, the result always overflows and
4301 we get an undefined (original) or the maximum value. */
4302 if (code == PREINCREMENT_EXPR)
4303 code = PREDECREMENT_EXPR;
4304 else if (code == PREDECREMENT_EXPR)
4305 code = PREINCREMENT_EXPR;
4306 else if (code == POSTINCREMENT_EXPR)
4307 code = POSTDECREMENT_EXPR;
4308 else /* code == POSTDECREMENT_EXPR */
4309 code = POSTINCREMENT_EXPR;
4311 inc = integer_minus_one_node;
4312 inc = convert (argtype, inc);
4314 else
4316 inc = VECTOR_TYPE_P (argtype)
4317 ? build_one_cst (argtype)
4318 : integer_one_node;
4319 inc = convert (argtype, inc);
4322 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4323 need to ask Objective-C to build the increment or decrement
4324 expression for it. */
4325 if (objc_is_property_ref (arg))
4326 return objc_build_incr_expr_for_property_ref (location, code,
4327 arg, inc);
4329 /* Report a read-only lvalue. */
4330 if (TYPE_READONLY (argtype))
4332 readonly_error (location, arg,
4333 ((code == PREINCREMENT_EXPR
4334 || code == POSTINCREMENT_EXPR)
4335 ? lv_increment : lv_decrement));
4336 return error_mark_node;
4338 else if (TREE_READONLY (arg))
4339 readonly_warning (arg,
4340 ((code == PREINCREMENT_EXPR
4341 || code == POSTINCREMENT_EXPR)
4342 ? lv_increment : lv_decrement));
4344 /* If the argument is atomic, use the special code sequences for
4345 atomic compound assignment. */
4346 if (atomic_op)
4348 arg = stabilize_reference (arg);
4349 ret = build_atomic_assign (location, arg,
4350 ((code == PREINCREMENT_EXPR
4351 || code == POSTINCREMENT_EXPR)
4352 ? PLUS_EXPR
4353 : MINUS_EXPR),
4354 (FRACT_MODE_P (TYPE_MODE (argtype))
4355 ? inc
4356 : integer_one_node),
4357 (code == POSTINCREMENT_EXPR
4358 || code == POSTDECREMENT_EXPR));
4359 goto return_build_unary_op;
4362 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4363 val = boolean_increment (code, arg);
4364 else
4365 val = build2 (code, TREE_TYPE (arg), arg, inc);
4366 TREE_SIDE_EFFECTS (val) = 1;
4367 if (TREE_CODE (val) != code)
4368 TREE_NO_WARNING (val) = 1;
4369 ret = val;
4370 goto return_build_unary_op;
4373 case ADDR_EXPR:
4374 /* Note that this operation never does default_conversion. */
4376 /* The operand of unary '&' must be an lvalue (which excludes
4377 expressions of type void), or, in C99, the result of a [] or
4378 unary '*' operator. */
4379 if (VOID_TYPE_P (TREE_TYPE (arg))
4380 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4381 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4382 pedwarn (location, 0, "taking address of expression of type %<void%>");
4384 /* Let &* cancel out to simplify resulting code. */
4385 if (INDIRECT_REF_P (arg))
4387 /* Don't let this be an lvalue. */
4388 if (lvalue_p (TREE_OPERAND (arg, 0)))
4389 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4390 ret = TREE_OPERAND (arg, 0);
4391 goto return_build_unary_op;
4394 /* Anything not already handled and not a true memory reference
4395 or a non-lvalue array is an error. */
4396 if (typecode != FUNCTION_TYPE && !flag
4397 && !lvalue_or_else (location, arg, lv_addressof))
4398 return error_mark_node;
4400 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4401 folding later. */
4402 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4404 tree inner = build_unary_op (location, code,
4405 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4406 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4407 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4408 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4409 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4410 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4411 goto return_build_unary_op;
4414 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4415 argtype = TREE_TYPE (arg);
4417 /* If the lvalue is const or volatile, merge that into the type
4418 to which the address will point. This is only needed
4419 for function types. */
4420 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4421 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4422 && TREE_CODE (argtype) == FUNCTION_TYPE)
4424 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4425 int quals = orig_quals;
4427 if (TREE_READONLY (arg))
4428 quals |= TYPE_QUAL_CONST;
4429 if (TREE_THIS_VOLATILE (arg))
4430 quals |= TYPE_QUAL_VOLATILE;
4432 argtype = c_build_qualified_type (argtype, quals);
4435 switch (TREE_CODE (arg))
4437 case COMPONENT_REF:
4438 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4440 error_at (location, "cannot take address of bit-field %qD",
4441 TREE_OPERAND (arg, 1));
4442 return error_mark_node;
4445 /* ... fall through ... */
4447 case ARRAY_REF:
4448 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4450 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4451 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4453 error_at (location, "cannot take address of scalar with "
4454 "reverse storage order");
4455 return error_mark_node;
4458 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4459 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4460 warning_at (location, OPT_Wscalar_storage_order,
4461 "address of array with reverse scalar storage "
4462 "order requested");
4465 default:
4466 break;
4469 if (!c_mark_addressable (arg))
4470 return error_mark_node;
4472 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4473 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4475 argtype = build_pointer_type (argtype);
4477 /* ??? Cope with user tricks that amount to offsetof. Delete this
4478 when we have proper support for integer constant expressions. */
4479 val = get_base_address (arg);
4480 if (val && INDIRECT_REF_P (val)
4481 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4483 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4484 goto return_build_unary_op;
4487 val = build1 (ADDR_EXPR, argtype, arg);
4489 ret = val;
4490 goto return_build_unary_op;
4492 default:
4493 gcc_unreachable ();
4496 if (argtype == 0)
4497 argtype = TREE_TYPE (arg);
4498 if (TREE_CODE (arg) == INTEGER_CST)
4499 ret = (require_constant_value
4500 ? fold_build1_initializer_loc (location, code, argtype, arg)
4501 : fold_build1_loc (location, code, argtype, arg));
4502 else
4503 ret = build1 (code, argtype, arg);
4504 return_build_unary_op:
4505 gcc_assert (ret != error_mark_node);
4506 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4507 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4508 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4509 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4510 ret = note_integer_operands (ret);
4511 if (eptype)
4512 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4513 protected_set_expr_location (ret, location);
4514 return ret;
4517 /* Return nonzero if REF is an lvalue valid for this language.
4518 Lvalues can be assigned, unless their type has TYPE_READONLY.
4519 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4521 bool
4522 lvalue_p (const_tree ref)
4524 const enum tree_code code = TREE_CODE (ref);
4526 switch (code)
4528 case REALPART_EXPR:
4529 case IMAGPART_EXPR:
4530 case COMPONENT_REF:
4531 return lvalue_p (TREE_OPERAND (ref, 0));
4533 case C_MAYBE_CONST_EXPR:
4534 return lvalue_p (TREE_OPERAND (ref, 1));
4536 case COMPOUND_LITERAL_EXPR:
4537 case STRING_CST:
4538 return 1;
4540 case INDIRECT_REF:
4541 case ARRAY_REF:
4542 case ARRAY_NOTATION_REF:
4543 case VAR_DECL:
4544 case PARM_DECL:
4545 case RESULT_DECL:
4546 case ERROR_MARK:
4547 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4548 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4550 case BIND_EXPR:
4551 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4553 default:
4554 return 0;
4558 /* Give a warning for storing in something that is read-only in GCC
4559 terms but not const in ISO C terms. */
4561 static void
4562 readonly_warning (tree arg, enum lvalue_use use)
4564 switch (use)
4566 case lv_assign:
4567 warning (0, "assignment of read-only location %qE", arg);
4568 break;
4569 case lv_increment:
4570 warning (0, "increment of read-only location %qE", arg);
4571 break;
4572 case lv_decrement:
4573 warning (0, "decrement of read-only location %qE", arg);
4574 break;
4575 default:
4576 gcc_unreachable ();
4578 return;
4582 /* Return nonzero if REF is an lvalue valid for this language;
4583 otherwise, print an error message and return zero. USE says
4584 how the lvalue is being used and so selects the error message.
4585 LOCATION is the location at which any error should be reported. */
4587 static int
4588 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4590 int win = lvalue_p (ref);
4592 if (!win)
4593 lvalue_error (loc, use);
4595 return win;
4598 /* Mark EXP saying that we need to be able to take the
4599 address of it; it should not be allocated in a register.
4600 Returns true if successful. */
4602 bool
4603 c_mark_addressable (tree exp)
4605 tree x = exp;
4607 while (1)
4608 switch (TREE_CODE (x))
4610 case COMPONENT_REF:
4611 case ADDR_EXPR:
4612 case ARRAY_REF:
4613 case REALPART_EXPR:
4614 case IMAGPART_EXPR:
4615 x = TREE_OPERAND (x, 0);
4616 break;
4618 case COMPOUND_LITERAL_EXPR:
4619 case CONSTRUCTOR:
4620 TREE_ADDRESSABLE (x) = 1;
4621 return true;
4623 case VAR_DECL:
4624 case CONST_DECL:
4625 case PARM_DECL:
4626 case RESULT_DECL:
4627 if (C_DECL_REGISTER (x)
4628 && DECL_NONLOCAL (x))
4630 if (TREE_PUBLIC (x) || is_global_var (x))
4632 error
4633 ("global register variable %qD used in nested function", x);
4634 return false;
4636 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4638 else if (C_DECL_REGISTER (x))
4640 if (TREE_PUBLIC (x) || is_global_var (x))
4641 error ("address of global register variable %qD requested", x);
4642 else
4643 error ("address of register variable %qD requested", x);
4644 return false;
4647 /* drops in */
4648 case FUNCTION_DECL:
4649 TREE_ADDRESSABLE (x) = 1;
4650 /* drops out */
4651 default:
4652 return true;
4656 /* Convert EXPR to TYPE, warning about conversion problems with
4657 constants. SEMANTIC_TYPE is the type this conversion would use
4658 without excess precision. If SEMANTIC_TYPE is NULL, this function
4659 is equivalent to convert_and_check. This function is a wrapper that
4660 handles conversions that may be different than
4661 the usual ones because of excess precision. */
4663 static tree
4664 ep_convert_and_check (location_t loc, tree type, tree expr,
4665 tree semantic_type)
4667 if (TREE_TYPE (expr) == type)
4668 return expr;
4670 if (!semantic_type)
4671 return convert_and_check (loc, type, expr);
4673 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4674 && TREE_TYPE (expr) != semantic_type)
4676 /* For integers, we need to check the real conversion, not
4677 the conversion to the excess precision type. */
4678 expr = convert_and_check (loc, semantic_type, expr);
4680 /* Result type is the excess precision type, which should be
4681 large enough, so do not check. */
4682 return convert (type, expr);
4685 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4686 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4687 if folded to an integer constant then the unselected half may
4688 contain arbitrary operations not normally permitted in constant
4689 expressions. Set the location of the expression to LOC. */
4691 tree
4692 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4693 tree op1, tree op1_original_type, tree op2,
4694 tree op2_original_type)
4696 tree type1;
4697 tree type2;
4698 enum tree_code code1;
4699 enum tree_code code2;
4700 tree result_type = NULL;
4701 tree semantic_result_type = NULL;
4702 tree orig_op1 = op1, orig_op2 = op2;
4703 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4704 bool ifexp_int_operands;
4705 tree ret;
4707 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4708 if (op1_int_operands)
4709 op1 = remove_c_maybe_const_expr (op1);
4710 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4711 if (op2_int_operands)
4712 op2 = remove_c_maybe_const_expr (op2);
4713 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4714 if (ifexp_int_operands)
4715 ifexp = remove_c_maybe_const_expr (ifexp);
4717 /* Promote both alternatives. */
4719 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4720 op1 = default_conversion (op1);
4721 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4722 op2 = default_conversion (op2);
4724 if (TREE_CODE (ifexp) == ERROR_MARK
4725 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4726 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4727 return error_mark_node;
4729 type1 = TREE_TYPE (op1);
4730 code1 = TREE_CODE (type1);
4731 type2 = TREE_TYPE (op2);
4732 code2 = TREE_CODE (type2);
4734 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4735 return error_mark_node;
4737 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4738 return error_mark_node;
4740 /* C90 does not permit non-lvalue arrays in conditional expressions.
4741 In C99 they will be pointers by now. */
4742 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4744 error_at (colon_loc, "non-lvalue array in conditional expression");
4745 return error_mark_node;
4748 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4749 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4750 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4751 || code1 == COMPLEX_TYPE)
4752 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4753 || code2 == COMPLEX_TYPE))
4755 semantic_result_type = c_common_type (type1, type2);
4756 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4758 op1 = TREE_OPERAND (op1, 0);
4759 type1 = TREE_TYPE (op1);
4760 gcc_assert (TREE_CODE (type1) == code1);
4762 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4764 op2 = TREE_OPERAND (op2, 0);
4765 type2 = TREE_TYPE (op2);
4766 gcc_assert (TREE_CODE (type2) == code2);
4770 if (warn_cxx_compat)
4772 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4773 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4775 if (TREE_CODE (t1) == ENUMERAL_TYPE
4776 && TREE_CODE (t2) == ENUMERAL_TYPE
4777 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4778 warning_at (colon_loc, OPT_Wc___compat,
4779 ("different enum types in conditional is "
4780 "invalid in C++: %qT vs %qT"),
4781 t1, t2);
4784 /* Quickly detect the usual case where op1 and op2 have the same type
4785 after promotion. */
4786 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4788 if (type1 == type2)
4789 result_type = type1;
4790 else
4791 result_type = TYPE_MAIN_VARIANT (type1);
4793 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4794 || code1 == COMPLEX_TYPE)
4795 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4796 || code2 == COMPLEX_TYPE))
4798 result_type = c_common_type (type1, type2);
4799 do_warn_double_promotion (result_type, type1, type2,
4800 "implicit conversion from %qT to %qT to "
4801 "match other result of conditional",
4802 colon_loc);
4804 /* If -Wsign-compare, warn here if type1 and type2 have
4805 different signedness. We'll promote the signed to unsigned
4806 and later code won't know it used to be different.
4807 Do this check on the original types, so that explicit casts
4808 will be considered, but default promotions won't. */
4809 if (c_inhibit_evaluation_warnings == 0)
4811 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4812 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4814 if (unsigned_op1 ^ unsigned_op2)
4816 bool ovf;
4818 /* Do not warn if the result type is signed, since the
4819 signed type will only be chosen if it can represent
4820 all the values of the unsigned type. */
4821 if (!TYPE_UNSIGNED (result_type))
4822 /* OK */;
4823 else
4825 bool op1_maybe_const = true;
4826 bool op2_maybe_const = true;
4828 /* Do not warn if the signed quantity is an
4829 unsuffixed integer literal (or some static
4830 constant expression involving such literals) and
4831 it is non-negative. This warning requires the
4832 operands to be folded for best results, so do
4833 that folding in this case even without
4834 warn_sign_compare to avoid warning options
4835 possibly affecting code generation. */
4836 c_inhibit_evaluation_warnings
4837 += (ifexp == truthvalue_false_node);
4838 op1 = c_fully_fold (op1, require_constant_value,
4839 &op1_maybe_const);
4840 c_inhibit_evaluation_warnings
4841 -= (ifexp == truthvalue_false_node);
4843 c_inhibit_evaluation_warnings
4844 += (ifexp == truthvalue_true_node);
4845 op2 = c_fully_fold (op2, require_constant_value,
4846 &op2_maybe_const);
4847 c_inhibit_evaluation_warnings
4848 -= (ifexp == truthvalue_true_node);
4850 if (warn_sign_compare)
4852 if ((unsigned_op2
4853 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4854 || (unsigned_op1
4855 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4856 /* OK */;
4857 else
4858 warning_at (colon_loc, OPT_Wsign_compare,
4859 ("signed and unsigned type in "
4860 "conditional expression"));
4862 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4863 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4864 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4865 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4870 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4872 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4873 pedwarn (colon_loc, OPT_Wpedantic,
4874 "ISO C forbids conditional expr with only one void side");
4875 result_type = void_type_node;
4877 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4879 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4880 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4881 addr_space_t as_common;
4883 if (comp_target_types (colon_loc, type1, type2))
4884 result_type = common_pointer_type (type1, type2);
4885 else if (null_pointer_constant_p (orig_op1))
4886 result_type = type2;
4887 else if (null_pointer_constant_p (orig_op2))
4888 result_type = type1;
4889 else if (!addr_space_superset (as1, as2, &as_common))
4891 error_at (colon_loc, "pointers to disjoint address spaces "
4892 "used in conditional expression");
4893 return error_mark_node;
4895 else if (VOID_TYPE_P (TREE_TYPE (type1))
4896 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4898 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4899 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4900 & ~TYPE_QUALS (TREE_TYPE (type1))))
4901 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4902 "pointer to array loses qualifier "
4903 "in conditional expression");
4905 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4906 pedwarn (colon_loc, OPT_Wpedantic,
4907 "ISO C forbids conditional expr between "
4908 "%<void *%> and function pointer");
4909 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4910 TREE_TYPE (type2)));
4912 else if (VOID_TYPE_P (TREE_TYPE (type2))
4913 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4915 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4916 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4917 & ~TYPE_QUALS (TREE_TYPE (type2))))
4918 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4919 "pointer to array loses qualifier "
4920 "in conditional expression");
4922 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4923 pedwarn (colon_loc, OPT_Wpedantic,
4924 "ISO C forbids conditional expr between "
4925 "%<void *%> and function pointer");
4926 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4927 TREE_TYPE (type1)));
4929 /* Objective-C pointer comparisons are a bit more lenient. */
4930 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4931 result_type = objc_common_type (type1, type2);
4932 else
4934 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4936 pedwarn (colon_loc, 0,
4937 "pointer type mismatch in conditional expression");
4938 result_type = build_pointer_type
4939 (build_qualified_type (void_type_node, qual));
4942 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4944 if (!null_pointer_constant_p (orig_op2))
4945 pedwarn (colon_loc, 0,
4946 "pointer/integer type mismatch in conditional expression");
4947 else
4949 op2 = null_pointer_node;
4951 result_type = type1;
4953 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4955 if (!null_pointer_constant_p (orig_op1))
4956 pedwarn (colon_loc, 0,
4957 "pointer/integer type mismatch in conditional expression");
4958 else
4960 op1 = null_pointer_node;
4962 result_type = type2;
4965 if (!result_type)
4967 if (flag_cond_mismatch)
4968 result_type = void_type_node;
4969 else
4971 error_at (colon_loc, "type mismatch in conditional expression");
4972 return error_mark_node;
4976 /* Merge const and volatile flags of the incoming types. */
4977 result_type
4978 = build_type_variant (result_type,
4979 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4980 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4982 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4983 semantic_result_type);
4984 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4985 semantic_result_type);
4987 if (ifexp_bcp && ifexp == truthvalue_true_node)
4989 op2_int_operands = true;
4990 op1 = c_fully_fold (op1, require_constant_value, NULL);
4992 if (ifexp_bcp && ifexp == truthvalue_false_node)
4994 op1_int_operands = true;
4995 op2 = c_fully_fold (op2, require_constant_value, NULL);
4997 int_const = int_operands = (ifexp_int_operands
4998 && op1_int_operands
4999 && op2_int_operands);
5000 if (int_operands)
5002 int_const = ((ifexp == truthvalue_true_node
5003 && TREE_CODE (orig_op1) == INTEGER_CST
5004 && !TREE_OVERFLOW (orig_op1))
5005 || (ifexp == truthvalue_false_node
5006 && TREE_CODE (orig_op2) == INTEGER_CST
5007 && !TREE_OVERFLOW (orig_op2)));
5010 /* Need to convert condition operand into a vector mask. */
5011 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5013 tree vectype = TREE_TYPE (ifexp);
5014 tree elem_type = TREE_TYPE (vectype);
5015 tree zero = build_int_cst (elem_type, 0);
5016 tree zero_vec = build_vector_from_val (vectype, zero);
5017 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5018 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5021 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5022 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5023 else
5025 if (int_operands)
5027 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5028 nested inside of the expression. */
5029 op1 = c_fully_fold (op1, false, NULL);
5030 op2 = c_fully_fold (op2, false, NULL);
5032 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5033 if (int_operands)
5034 ret = note_integer_operands (ret);
5036 if (semantic_result_type)
5037 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5039 protected_set_expr_location (ret, colon_loc);
5040 return ret;
5043 /* Return a compound expression that performs two expressions and
5044 returns the value of the second of them.
5046 LOC is the location of the COMPOUND_EXPR. */
5048 tree
5049 build_compound_expr (location_t loc, tree expr1, tree expr2)
5051 bool expr1_int_operands, expr2_int_operands;
5052 tree eptype = NULL_TREE;
5053 tree ret;
5055 if (flag_cilkplus
5056 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5057 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5059 error_at (loc,
5060 "spawned function call cannot be part of a comma expression");
5061 return error_mark_node;
5063 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5064 if (expr1_int_operands)
5065 expr1 = remove_c_maybe_const_expr (expr1);
5066 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5067 if (expr2_int_operands)
5068 expr2 = remove_c_maybe_const_expr (expr2);
5070 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5071 expr1 = TREE_OPERAND (expr1, 0);
5072 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5074 eptype = TREE_TYPE (expr2);
5075 expr2 = TREE_OPERAND (expr2, 0);
5078 if (!TREE_SIDE_EFFECTS (expr1))
5080 /* The left-hand operand of a comma expression is like an expression
5081 statement: with -Wunused, we should warn if it doesn't have
5082 any side-effects, unless it was explicitly cast to (void). */
5083 if (warn_unused_value)
5085 if (VOID_TYPE_P (TREE_TYPE (expr1))
5086 && CONVERT_EXPR_P (expr1))
5087 ; /* (void) a, b */
5088 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5089 && TREE_CODE (expr1) == COMPOUND_EXPR
5090 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5091 ; /* (void) a, (void) b, c */
5092 else
5093 warning_at (loc, OPT_Wunused_value,
5094 "left-hand operand of comma expression has no effect");
5097 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5098 && warn_unused_value)
5100 tree r = expr1;
5101 location_t cloc = loc;
5102 while (TREE_CODE (r) == COMPOUND_EXPR)
5104 if (EXPR_HAS_LOCATION (r))
5105 cloc = EXPR_LOCATION (r);
5106 r = TREE_OPERAND (r, 1);
5108 if (!TREE_SIDE_EFFECTS (r)
5109 && !VOID_TYPE_P (TREE_TYPE (r))
5110 && !CONVERT_EXPR_P (r))
5111 warning_at (cloc, OPT_Wunused_value,
5112 "right-hand operand of comma expression has no effect");
5115 /* With -Wunused, we should also warn if the left-hand operand does have
5116 side-effects, but computes a value which is not used. For example, in
5117 `foo() + bar(), baz()' the result of the `+' operator is not used,
5118 so we should issue a warning. */
5119 else if (warn_unused_value)
5120 warn_if_unused_value (expr1, loc);
5122 if (expr2 == error_mark_node)
5123 return error_mark_node;
5125 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5127 if (flag_isoc99
5128 && expr1_int_operands
5129 && expr2_int_operands)
5130 ret = note_integer_operands (ret);
5132 if (eptype)
5133 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5135 protected_set_expr_location (ret, loc);
5136 return ret;
5139 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5140 which we are casting. OTYPE is the type of the expression being
5141 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5142 of the cast. -Wcast-qual appeared on the command line. Named
5143 address space qualifiers are not handled here, because they result
5144 in different warnings. */
5146 static void
5147 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5149 tree in_type = type;
5150 tree in_otype = otype;
5151 int added = 0;
5152 int discarded = 0;
5153 bool is_const;
5155 /* Check that the qualifiers on IN_TYPE are a superset of the
5156 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5157 nodes is uninteresting and we stop as soon as we hit a
5158 non-POINTER_TYPE node on either type. */
5161 in_otype = TREE_TYPE (in_otype);
5162 in_type = TREE_TYPE (in_type);
5164 /* GNU C allows cv-qualified function types. 'const' means the
5165 function is very pure, 'volatile' means it can't return. We
5166 need to warn when such qualifiers are added, not when they're
5167 taken away. */
5168 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5169 && TREE_CODE (in_type) == FUNCTION_TYPE)
5170 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5171 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5172 else
5173 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5174 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5176 while (TREE_CODE (in_type) == POINTER_TYPE
5177 && TREE_CODE (in_otype) == POINTER_TYPE);
5179 if (added)
5180 warning_at (loc, OPT_Wcast_qual,
5181 "cast adds %q#v qualifier to function type", added);
5183 if (discarded)
5184 /* There are qualifiers present in IN_OTYPE that are not present
5185 in IN_TYPE. */
5186 warning_at (loc, OPT_Wcast_qual,
5187 "cast discards %qv qualifier from pointer target type",
5188 discarded);
5190 if (added || discarded)
5191 return;
5193 /* A cast from **T to const **T is unsafe, because it can cause a
5194 const value to be changed with no additional warning. We only
5195 issue this warning if T is the same on both sides, and we only
5196 issue the warning if there are the same number of pointers on
5197 both sides, as otherwise the cast is clearly unsafe anyhow. A
5198 cast is unsafe when a qualifier is added at one level and const
5199 is not present at all outer levels.
5201 To issue this warning, we check at each level whether the cast
5202 adds new qualifiers not already seen. We don't need to special
5203 case function types, as they won't have the same
5204 TYPE_MAIN_VARIANT. */
5206 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5207 return;
5208 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5209 return;
5211 in_type = type;
5212 in_otype = otype;
5213 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5216 in_type = TREE_TYPE (in_type);
5217 in_otype = TREE_TYPE (in_otype);
5218 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5219 && !is_const)
5221 warning_at (loc, OPT_Wcast_qual,
5222 "to be safe all intermediate pointers in cast from "
5223 "%qT to %qT must be %<const%> qualified",
5224 otype, type);
5225 break;
5227 if (is_const)
5228 is_const = TYPE_READONLY (in_type);
5230 while (TREE_CODE (in_type) == POINTER_TYPE);
5233 /* Build an expression representing a cast to type TYPE of expression EXPR.
5234 LOC is the location of the cast-- typically the open paren of the cast. */
5236 tree
5237 build_c_cast (location_t loc, tree type, tree expr)
5239 tree value;
5241 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5242 expr = TREE_OPERAND (expr, 0);
5244 value = expr;
5246 if (type == error_mark_node || expr == error_mark_node)
5247 return error_mark_node;
5249 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5250 only in <protocol> qualifications. But when constructing cast expressions,
5251 the protocols do matter and must be kept around. */
5252 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5253 return build1 (NOP_EXPR, type, expr);
5255 type = TYPE_MAIN_VARIANT (type);
5257 if (TREE_CODE (type) == ARRAY_TYPE)
5259 error_at (loc, "cast specifies array type");
5260 return error_mark_node;
5263 if (TREE_CODE (type) == FUNCTION_TYPE)
5265 error_at (loc, "cast specifies function type");
5266 return error_mark_node;
5269 if (!VOID_TYPE_P (type))
5271 value = require_complete_type (value);
5272 if (value == error_mark_node)
5273 return error_mark_node;
5276 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5278 if (RECORD_OR_UNION_TYPE_P (type))
5279 pedwarn (loc, OPT_Wpedantic,
5280 "ISO C forbids casting nonscalar to the same type");
5282 /* Convert to remove any qualifiers from VALUE's type. */
5283 value = convert (type, value);
5285 else if (TREE_CODE (type) == UNION_TYPE)
5287 tree field;
5289 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5290 if (TREE_TYPE (field) != error_mark_node
5291 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5292 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5293 break;
5295 if (field)
5297 tree t;
5298 bool maybe_const = true;
5300 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5301 t = c_fully_fold (value, false, &maybe_const);
5302 t = build_constructor_single (type, field, t);
5303 if (!maybe_const)
5304 t = c_wrap_maybe_const (t, true);
5305 t = digest_init (loc, type, t,
5306 NULL_TREE, false, true, 0);
5307 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5308 return t;
5310 error_at (loc, "cast to union type from type not present in union");
5311 return error_mark_node;
5313 else
5315 tree otype, ovalue;
5317 if (type == void_type_node)
5319 tree t = build1 (CONVERT_EXPR, type, value);
5320 SET_EXPR_LOCATION (t, loc);
5321 return t;
5324 otype = TREE_TYPE (value);
5326 /* Optionally warn about potentially worrisome casts. */
5327 if (warn_cast_qual
5328 && TREE_CODE (type) == POINTER_TYPE
5329 && TREE_CODE (otype) == POINTER_TYPE)
5330 handle_warn_cast_qual (loc, type, otype);
5332 /* Warn about conversions between pointers to disjoint
5333 address spaces. */
5334 if (TREE_CODE (type) == POINTER_TYPE
5335 && TREE_CODE (otype) == POINTER_TYPE
5336 && !null_pointer_constant_p (value))
5338 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5339 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5340 addr_space_t as_common;
5342 if (!addr_space_superset (as_to, as_from, &as_common))
5344 if (ADDR_SPACE_GENERIC_P (as_from))
5345 warning_at (loc, 0, "cast to %s address space pointer "
5346 "from disjoint generic address space pointer",
5347 c_addr_space_name (as_to));
5349 else if (ADDR_SPACE_GENERIC_P (as_to))
5350 warning_at (loc, 0, "cast to generic address space pointer "
5351 "from disjoint %s address space pointer",
5352 c_addr_space_name (as_from));
5354 else
5355 warning_at (loc, 0, "cast to %s address space pointer "
5356 "from disjoint %s address space pointer",
5357 c_addr_space_name (as_to),
5358 c_addr_space_name (as_from));
5362 /* Warn about possible alignment problems. */
5363 if (STRICT_ALIGNMENT
5364 && TREE_CODE (type) == POINTER_TYPE
5365 && TREE_CODE (otype) == POINTER_TYPE
5366 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5367 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5368 /* Don't warn about opaque types, where the actual alignment
5369 restriction is unknown. */
5370 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5371 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5372 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5373 warning_at (loc, OPT_Wcast_align,
5374 "cast increases required alignment of target type");
5376 if (TREE_CODE (type) == INTEGER_TYPE
5377 && TREE_CODE (otype) == POINTER_TYPE
5378 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5379 /* Unlike conversion of integers to pointers, where the
5380 warning is disabled for converting constants because
5381 of cases such as SIG_*, warn about converting constant
5382 pointers to integers. In some cases it may cause unwanted
5383 sign extension, and a warning is appropriate. */
5384 warning_at (loc, OPT_Wpointer_to_int_cast,
5385 "cast from pointer to integer of different size");
5387 if (TREE_CODE (value) == CALL_EXPR
5388 && TREE_CODE (type) != TREE_CODE (otype))
5389 warning_at (loc, OPT_Wbad_function_cast,
5390 "cast from function call of type %qT "
5391 "to non-matching type %qT", otype, type);
5393 if (TREE_CODE (type) == POINTER_TYPE
5394 && TREE_CODE (otype) == INTEGER_TYPE
5395 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5396 /* Don't warn about converting any constant. */
5397 && !TREE_CONSTANT (value))
5398 warning_at (loc,
5399 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5400 "of different size");
5402 if (warn_strict_aliasing <= 2)
5403 strict_aliasing_warning (otype, type, expr);
5405 /* If pedantic, warn for conversions between function and object
5406 pointer types, except for converting a null pointer constant
5407 to function pointer type. */
5408 if (pedantic
5409 && TREE_CODE (type) == POINTER_TYPE
5410 && TREE_CODE (otype) == POINTER_TYPE
5411 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5412 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5413 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5414 "conversion of function pointer to object pointer type");
5416 if (pedantic
5417 && TREE_CODE (type) == POINTER_TYPE
5418 && TREE_CODE (otype) == POINTER_TYPE
5419 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5420 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5421 && !null_pointer_constant_p (value))
5422 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5423 "conversion of object pointer to function pointer type");
5425 ovalue = value;
5426 value = convert (type, value);
5428 /* Ignore any integer overflow caused by the cast. */
5429 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5431 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5433 if (!TREE_OVERFLOW (value))
5435 /* Avoid clobbering a shared constant. */
5436 value = copy_node (value);
5437 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5440 else if (TREE_OVERFLOW (value))
5441 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5442 value = wide_int_to_tree (TREE_TYPE (value), value);
5446 /* Don't let a cast be an lvalue. */
5447 if (lvalue_p (value))
5448 value = non_lvalue_loc (loc, value);
5450 /* Don't allow the results of casting to floating-point or complex
5451 types be confused with actual constants, or casts involving
5452 integer and pointer types other than direct integer-to-integer
5453 and integer-to-pointer be confused with integer constant
5454 expressions and null pointer constants. */
5455 if (TREE_CODE (value) == REAL_CST
5456 || TREE_CODE (value) == COMPLEX_CST
5457 || (TREE_CODE (value) == INTEGER_CST
5458 && !((TREE_CODE (expr) == INTEGER_CST
5459 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5460 || TREE_CODE (expr) == REAL_CST
5461 || TREE_CODE (expr) == COMPLEX_CST)))
5462 value = build1 (NOP_EXPR, type, value);
5464 protected_set_expr_location (value, loc);
5465 return value;
5468 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5469 location of the open paren of the cast, or the position of the cast
5470 expr. */
5471 tree
5472 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5474 tree type;
5475 tree type_expr = NULL_TREE;
5476 bool type_expr_const = true;
5477 tree ret;
5478 int saved_wsp = warn_strict_prototypes;
5480 /* This avoids warnings about unprototyped casts on
5481 integers. E.g. "#define SIG_DFL (void(*)())0". */
5482 if (TREE_CODE (expr) == INTEGER_CST)
5483 warn_strict_prototypes = 0;
5484 type = groktypename (type_name, &type_expr, &type_expr_const);
5485 warn_strict_prototypes = saved_wsp;
5487 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5488 && reject_gcc_builtin (expr))
5489 return error_mark_node;
5491 ret = build_c_cast (loc, type, expr);
5492 if (type_expr)
5494 bool inner_expr_const = true;
5495 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5496 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5497 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5498 && inner_expr_const);
5499 SET_EXPR_LOCATION (ret, loc);
5502 if (!EXPR_HAS_LOCATION (ret))
5503 protected_set_expr_location (ret, loc);
5505 /* C++ does not permits types to be defined in a cast, but it
5506 allows references to incomplete types. */
5507 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5508 warning_at (loc, OPT_Wc___compat,
5509 "defining a type in a cast is invalid in C++");
5511 return ret;
5514 /* Build an assignment expression of lvalue LHS from value RHS.
5515 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5516 may differ from TREE_TYPE (LHS) for an enum bitfield.
5517 MODIFYCODE is the code for a binary operator that we use
5518 to combine the old value of LHS with RHS to get the new value.
5519 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5520 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5521 which may differ from TREE_TYPE (RHS) for an enum value.
5523 LOCATION is the location of the MODIFYCODE operator.
5524 RHS_LOC is the location of the RHS. */
5526 tree
5527 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5528 enum tree_code modifycode,
5529 location_t rhs_loc, tree rhs, tree rhs_origtype)
5531 tree result;
5532 tree newrhs;
5533 tree rhseval = NULL_TREE;
5534 tree rhs_semantic_type = NULL_TREE;
5535 tree lhstype = TREE_TYPE (lhs);
5536 tree olhstype = lhstype;
5537 bool npc;
5538 bool is_atomic_op;
5540 /* Types that aren't fully specified cannot be used in assignments. */
5541 lhs = require_complete_type (lhs);
5543 /* Avoid duplicate error messages from operands that had errors. */
5544 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5545 return error_mark_node;
5547 /* Ensure an error for assigning a non-lvalue array to an array in
5548 C90. */
5549 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5551 error_at (location, "assignment to expression with array type");
5552 return error_mark_node;
5555 /* For ObjC properties, defer this check. */
5556 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5557 return error_mark_node;
5559 is_atomic_op = really_atomic_lvalue (lhs);
5561 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5563 rhs_semantic_type = TREE_TYPE (rhs);
5564 rhs = TREE_OPERAND (rhs, 0);
5567 newrhs = rhs;
5569 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5571 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5572 lhs_origtype, modifycode, rhs_loc, rhs,
5573 rhs_origtype);
5574 if (inner == error_mark_node)
5575 return error_mark_node;
5576 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5577 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5578 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5579 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5580 protected_set_expr_location (result, location);
5581 return result;
5584 /* If a binary op has been requested, combine the old LHS value with the RHS
5585 producing the value we should actually store into the LHS. */
5587 if (modifycode != NOP_EXPR)
5589 lhs = c_fully_fold (lhs, false, NULL);
5590 lhs = stabilize_reference (lhs);
5592 /* Construct the RHS for any non-atomic compound assignemnt. */
5593 if (!is_atomic_op)
5595 /* If in LHS op= RHS the RHS has side-effects, ensure they
5596 are preevaluated before the rest of the assignment expression's
5597 side-effects, because RHS could contain e.g. function calls
5598 that modify LHS. */
5599 if (TREE_SIDE_EFFECTS (rhs))
5601 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5602 rhseval = newrhs;
5604 newrhs = build_binary_op (location,
5605 modifycode, lhs, newrhs, 1);
5607 /* The original type of the right hand side is no longer
5608 meaningful. */
5609 rhs_origtype = NULL_TREE;
5613 if (c_dialect_objc ())
5615 /* Check if we are modifying an Objective-C property reference;
5616 if so, we need to generate setter calls. */
5617 result = objc_maybe_build_modify_expr (lhs, newrhs);
5618 if (result)
5619 goto return_result;
5621 /* Else, do the check that we postponed for Objective-C. */
5622 if (!lvalue_or_else (location, lhs, lv_assign))
5623 return error_mark_node;
5626 /* Give an error for storing in something that is 'const'. */
5628 if (TYPE_READONLY (lhstype)
5629 || (RECORD_OR_UNION_TYPE_P (lhstype)
5630 && C_TYPE_FIELDS_READONLY (lhstype)))
5632 readonly_error (location, lhs, lv_assign);
5633 return error_mark_node;
5635 else if (TREE_READONLY (lhs))
5636 readonly_warning (lhs, lv_assign);
5638 /* If storing into a structure or union member,
5639 it has probably been given type `int'.
5640 Compute the type that would go with
5641 the actual amount of storage the member occupies. */
5643 if (TREE_CODE (lhs) == COMPONENT_REF
5644 && (TREE_CODE (lhstype) == INTEGER_TYPE
5645 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5646 || TREE_CODE (lhstype) == REAL_TYPE
5647 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5648 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5650 /* If storing in a field that is in actuality a short or narrower than one,
5651 we must store in the field in its actual type. */
5653 if (lhstype != TREE_TYPE (lhs))
5655 lhs = copy_node (lhs);
5656 TREE_TYPE (lhs) = lhstype;
5659 /* Issue -Wc++-compat warnings about an assignment to an enum type
5660 when LHS does not have its original type. This happens for,
5661 e.g., an enum bitfield in a struct. */
5662 if (warn_cxx_compat
5663 && lhs_origtype != NULL_TREE
5664 && lhs_origtype != lhstype
5665 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5667 tree checktype = (rhs_origtype != NULL_TREE
5668 ? rhs_origtype
5669 : TREE_TYPE (rhs));
5670 if (checktype != error_mark_node
5671 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5672 || (is_atomic_op && modifycode != NOP_EXPR)))
5673 warning_at (location, OPT_Wc___compat,
5674 "enum conversion in assignment is invalid in C++");
5677 /* If the lhs is atomic, remove that qualifier. */
5678 if (is_atomic_op)
5680 lhstype = build_qualified_type (lhstype,
5681 (TYPE_QUALS (lhstype)
5682 & ~TYPE_QUAL_ATOMIC));
5683 olhstype = build_qualified_type (olhstype,
5684 (TYPE_QUALS (lhstype)
5685 & ~TYPE_QUAL_ATOMIC));
5688 /* Convert new value to destination type. Fold it first, then
5689 restore any excess precision information, for the sake of
5690 conversion warnings. */
5692 if (!(is_atomic_op && modifycode != NOP_EXPR))
5694 npc = null_pointer_constant_p (newrhs);
5695 newrhs = c_fully_fold (newrhs, false, NULL);
5696 if (rhs_semantic_type)
5697 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5698 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5699 rhs_origtype, ic_assign, npc,
5700 NULL_TREE, NULL_TREE, 0);
5701 if (TREE_CODE (newrhs) == ERROR_MARK)
5702 return error_mark_node;
5705 /* Emit ObjC write barrier, if necessary. */
5706 if (c_dialect_objc () && flag_objc_gc)
5708 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5709 if (result)
5711 protected_set_expr_location (result, location);
5712 goto return_result;
5716 /* Scan operands. */
5718 if (is_atomic_op)
5719 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5720 else
5722 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5723 TREE_SIDE_EFFECTS (result) = 1;
5724 protected_set_expr_location (result, location);
5727 /* If we got the LHS in a different type for storing in,
5728 convert the result back to the nominal type of LHS
5729 so that the value we return always has the same type
5730 as the LHS argument. */
5732 if (olhstype == TREE_TYPE (result))
5733 goto return_result;
5735 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5736 rhs_origtype, ic_assign, false, NULL_TREE,
5737 NULL_TREE, 0);
5738 protected_set_expr_location (result, location);
5740 return_result:
5741 if (rhseval)
5742 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5743 return result;
5746 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5747 This is used to implement -fplan9-extensions. */
5749 static bool
5750 find_anonymous_field_with_type (tree struct_type, tree type)
5752 tree field;
5753 bool found;
5755 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
5756 found = false;
5757 for (field = TYPE_FIELDS (struct_type);
5758 field != NULL_TREE;
5759 field = TREE_CHAIN (field))
5761 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5762 ? c_build_qualified_type (TREE_TYPE (field),
5763 TYPE_QUAL_ATOMIC)
5764 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5765 if (DECL_NAME (field) == NULL
5766 && comptypes (type, fieldtype))
5768 if (found)
5769 return false;
5770 found = true;
5772 else if (DECL_NAME (field) == NULL
5773 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5774 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5776 if (found)
5777 return false;
5778 found = true;
5781 return found;
5784 /* RHS is an expression whose type is pointer to struct. If there is
5785 an anonymous field in RHS with type TYPE, then return a pointer to
5786 that field in RHS. This is used with -fplan9-extensions. This
5787 returns NULL if no conversion could be found. */
5789 static tree
5790 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5792 tree rhs_struct_type, lhs_main_type;
5793 tree field, found_field;
5794 bool found_sub_field;
5795 tree ret;
5797 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5798 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5799 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
5801 gcc_assert (POINTER_TYPE_P (type));
5802 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5803 ? c_build_qualified_type (TREE_TYPE (type),
5804 TYPE_QUAL_ATOMIC)
5805 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5807 found_field = NULL_TREE;
5808 found_sub_field = false;
5809 for (field = TYPE_FIELDS (rhs_struct_type);
5810 field != NULL_TREE;
5811 field = TREE_CHAIN (field))
5813 if (DECL_NAME (field) != NULL_TREE
5814 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
5815 continue;
5816 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5817 ? c_build_qualified_type (TREE_TYPE (field),
5818 TYPE_QUAL_ATOMIC)
5819 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5820 if (comptypes (lhs_main_type, fieldtype))
5822 if (found_field != NULL_TREE)
5823 return NULL_TREE;
5824 found_field = field;
5826 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5827 lhs_main_type))
5829 if (found_field != NULL_TREE)
5830 return NULL_TREE;
5831 found_field = field;
5832 found_sub_field = true;
5836 if (found_field == NULL_TREE)
5837 return NULL_TREE;
5839 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5840 build_fold_indirect_ref (rhs), found_field,
5841 NULL_TREE);
5842 ret = build_fold_addr_expr_loc (location, ret);
5844 if (found_sub_field)
5846 ret = convert_to_anonymous_field (location, type, ret);
5847 gcc_assert (ret != NULL_TREE);
5850 return ret;
5853 /* Issue an error message for a bad initializer component.
5854 GMSGID identifies the message.
5855 The component name is taken from the spelling stack. */
5857 static void
5858 error_init (location_t loc, const char *gmsgid)
5860 char *ofwhat;
5862 /* The gmsgid may be a format string with %< and %>. */
5863 error_at (loc, gmsgid);
5864 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5865 if (*ofwhat)
5866 inform (loc, "(near initialization for %qs)", ofwhat);
5869 /* Issue a pedantic warning for a bad initializer component. OPT is
5870 the option OPT_* (from options.h) controlling this warning or 0 if
5871 it is unconditionally given. GMSGID identifies the message. The
5872 component name is taken from the spelling stack. */
5874 static void
5875 pedwarn_init (location_t location, int opt, const char *gmsgid)
5877 char *ofwhat;
5878 bool warned;
5880 /* The gmsgid may be a format string with %< and %>. */
5881 warned = pedwarn (location, opt, gmsgid);
5882 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5883 if (*ofwhat && warned)
5884 inform (location, "(near initialization for %qs)", ofwhat);
5887 /* Issue a warning for a bad initializer component.
5889 OPT is the OPT_W* value corresponding to the warning option that
5890 controls this warning. GMSGID identifies the message. The
5891 component name is taken from the spelling stack. */
5893 static void
5894 warning_init (location_t loc, int opt, const char *gmsgid)
5896 char *ofwhat;
5897 bool warned;
5899 /* The gmsgid may be a format string with %< and %>. */
5900 warned = warning_at (loc, opt, gmsgid);
5901 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5902 if (*ofwhat && warned)
5903 inform (loc, "(near initialization for %qs)", ofwhat);
5906 /* If TYPE is an array type and EXPR is a parenthesized string
5907 constant, warn if pedantic that EXPR is being used to initialize an
5908 object of type TYPE. */
5910 void
5911 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5913 if (pedantic
5914 && TREE_CODE (type) == ARRAY_TYPE
5915 && TREE_CODE (expr.value) == STRING_CST
5916 && expr.original_code != STRING_CST)
5917 pedwarn_init (loc, OPT_Wpedantic,
5918 "array initialized from parenthesized string constant");
5921 /* Convert value RHS to type TYPE as preparation for an assignment to
5922 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5923 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5924 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5925 constant before any folding.
5926 The real work of conversion is done by `convert'.
5927 The purpose of this function is to generate error messages
5928 for assignments that are not allowed in C.
5929 ERRTYPE says whether it is argument passing, assignment,
5930 initialization or return.
5932 In the following example, '~' denotes where EXPR_LOC and '^' where
5933 LOCATION point to:
5935 f (var); [ic_argpass]
5936 ^ ~~~
5937 x = var; [ic_assign]
5938 ^ ~~~;
5939 int x = var; [ic_init]
5941 return x; [ic_return]
5944 FUNCTION is a tree for the function being called.
5945 PARMNUM is the number of the argument, for printing in error messages. */
5947 static tree
5948 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5949 tree rhs, tree origtype, enum impl_conv errtype,
5950 bool null_pointer_constant, tree fundecl,
5951 tree function, int parmnum)
5953 enum tree_code codel = TREE_CODE (type);
5954 tree orig_rhs = rhs;
5955 tree rhstype;
5956 enum tree_code coder;
5957 tree rname = NULL_TREE;
5958 bool objc_ok = false;
5960 /* Use the expansion point location to handle cases such as user's
5961 function returning a wrong-type macro defined in a system header. */
5962 location = expansion_point_location_if_in_system_header (location);
5964 if (errtype == ic_argpass)
5966 tree selector;
5967 /* Change pointer to function to the function itself for
5968 diagnostics. */
5969 if (TREE_CODE (function) == ADDR_EXPR
5970 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5971 function = TREE_OPERAND (function, 0);
5973 /* Handle an ObjC selector specially for diagnostics. */
5974 selector = objc_message_selector ();
5975 rname = function;
5976 if (selector && parmnum > 2)
5978 rname = selector;
5979 parmnum -= 2;
5983 /* This macro is used to emit diagnostics to ensure that all format
5984 strings are complete sentences, visible to gettext and checked at
5985 compile time. */
5986 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5987 do { \
5988 switch (errtype) \
5990 case ic_argpass: \
5991 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5992 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5993 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5994 "expected %qT but argument is of type %qT", \
5995 type, rhstype); \
5996 break; \
5997 case ic_assign: \
5998 pedwarn (LOCATION, OPT, AS); \
5999 break; \
6000 case ic_init: \
6001 pedwarn_init (LOCATION, OPT, IN); \
6002 break; \
6003 case ic_return: \
6004 pedwarn (LOCATION, OPT, RE); \
6005 break; \
6006 default: \
6007 gcc_unreachable (); \
6009 } while (0)
6011 /* This macro is used to emit diagnostics to ensure that all format
6012 strings are complete sentences, visible to gettext and checked at
6013 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6014 extra parameter to enumerate qualifiers. */
6015 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6016 do { \
6017 switch (errtype) \
6019 case ic_argpass: \
6020 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6021 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6022 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6023 "expected %qT but argument is of type %qT", \
6024 type, rhstype); \
6025 break; \
6026 case ic_assign: \
6027 pedwarn (LOCATION, OPT, AS, QUALS); \
6028 break; \
6029 case ic_init: \
6030 pedwarn (LOCATION, OPT, IN, QUALS); \
6031 break; \
6032 case ic_return: \
6033 pedwarn (LOCATION, OPT, RE, QUALS); \
6034 break; \
6035 default: \
6036 gcc_unreachable (); \
6038 } while (0)
6040 /* This macro is used to emit diagnostics to ensure that all format
6041 strings are complete sentences, visible to gettext and checked at
6042 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6043 warning_at instead of pedwarn. */
6044 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6045 do { \
6046 switch (errtype) \
6048 case ic_argpass: \
6049 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6050 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6051 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6052 "expected %qT but argument is of type %qT", \
6053 type, rhstype); \
6054 break; \
6055 case ic_assign: \
6056 warning_at (LOCATION, OPT, AS, QUALS); \
6057 break; \
6058 case ic_init: \
6059 warning_at (LOCATION, OPT, IN, QUALS); \
6060 break; \
6061 case ic_return: \
6062 warning_at (LOCATION, OPT, RE, QUALS); \
6063 break; \
6064 default: \
6065 gcc_unreachable (); \
6067 } while (0)
6069 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6070 rhs = TREE_OPERAND (rhs, 0);
6072 rhstype = TREE_TYPE (rhs);
6073 coder = TREE_CODE (rhstype);
6075 if (coder == ERROR_MARK)
6076 return error_mark_node;
6078 if (c_dialect_objc ())
6080 int parmno;
6082 switch (errtype)
6084 case ic_return:
6085 parmno = 0;
6086 break;
6088 case ic_assign:
6089 parmno = -1;
6090 break;
6092 case ic_init:
6093 parmno = -2;
6094 break;
6096 default:
6097 parmno = parmnum;
6098 break;
6101 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6104 if (warn_cxx_compat)
6106 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6107 if (checktype != error_mark_node
6108 && TREE_CODE (type) == ENUMERAL_TYPE
6109 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6111 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6112 G_("enum conversion when passing argument "
6113 "%d of %qE is invalid in C++"),
6114 G_("enum conversion in assignment is "
6115 "invalid in C++"),
6116 G_("enum conversion in initialization is "
6117 "invalid in C++"),
6118 G_("enum conversion in return is "
6119 "invalid in C++"));
6123 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6124 return rhs;
6126 if (coder == VOID_TYPE)
6128 /* Except for passing an argument to an unprototyped function,
6129 this is a constraint violation. When passing an argument to
6130 an unprototyped function, it is compile-time undefined;
6131 making it a constraint in that case was rejected in
6132 DR#252. */
6133 error_at (location, "void value not ignored as it ought to be");
6134 return error_mark_node;
6136 rhs = require_complete_type (rhs);
6137 if (rhs == error_mark_node)
6138 return error_mark_node;
6140 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6141 return error_mark_node;
6143 /* A non-reference type can convert to a reference. This handles
6144 va_start, va_copy and possibly port built-ins. */
6145 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6147 if (!lvalue_p (rhs))
6149 error_at (location, "cannot pass rvalue to reference parameter");
6150 return error_mark_node;
6152 if (!c_mark_addressable (rhs))
6153 return error_mark_node;
6154 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6155 SET_EXPR_LOCATION (rhs, location);
6157 rhs = convert_for_assignment (location, expr_loc,
6158 build_pointer_type (TREE_TYPE (type)),
6159 rhs, origtype, errtype,
6160 null_pointer_constant, fundecl, function,
6161 parmnum);
6162 if (rhs == error_mark_node)
6163 return error_mark_node;
6165 rhs = build1 (NOP_EXPR, type, rhs);
6166 SET_EXPR_LOCATION (rhs, location);
6167 return rhs;
6169 /* Some types can interconvert without explicit casts. */
6170 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6171 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6172 return convert (type, rhs);
6173 /* Arithmetic types all interconvert, and enum is treated like int. */
6174 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6175 || codel == FIXED_POINT_TYPE
6176 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6177 || codel == BOOLEAN_TYPE)
6178 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6179 || coder == FIXED_POINT_TYPE
6180 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6181 || coder == BOOLEAN_TYPE))
6183 tree ret;
6184 bool save = in_late_binary_op;
6185 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6186 || (coder == REAL_TYPE
6187 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6188 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
6189 in_late_binary_op = true;
6190 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6191 ? expr_loc : location, type, orig_rhs);
6192 in_late_binary_op = save;
6193 return ret;
6196 /* Aggregates in different TUs might need conversion. */
6197 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6198 && codel == coder
6199 && comptypes (type, rhstype))
6200 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6201 ? expr_loc : location, type, rhs);
6203 /* Conversion to a transparent union or record from its member types.
6204 This applies only to function arguments. */
6205 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6206 && TYPE_TRANSPARENT_AGGR (type))
6207 && errtype == ic_argpass)
6209 tree memb, marginal_memb = NULL_TREE;
6211 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6213 tree memb_type = TREE_TYPE (memb);
6215 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6216 TYPE_MAIN_VARIANT (rhstype)))
6217 break;
6219 if (TREE_CODE (memb_type) != POINTER_TYPE)
6220 continue;
6222 if (coder == POINTER_TYPE)
6224 tree ttl = TREE_TYPE (memb_type);
6225 tree ttr = TREE_TYPE (rhstype);
6227 /* Any non-function converts to a [const][volatile] void *
6228 and vice versa; otherwise, targets must be the same.
6229 Meanwhile, the lhs target must have all the qualifiers of
6230 the rhs. */
6231 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6232 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6233 || comp_target_types (location, memb_type, rhstype))
6235 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6236 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6237 /* If this type won't generate any warnings, use it. */
6238 if (lquals == rquals
6239 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6240 && TREE_CODE (ttl) == FUNCTION_TYPE)
6241 ? ((lquals | rquals) == rquals)
6242 : ((lquals | rquals) == lquals)))
6243 break;
6245 /* Keep looking for a better type, but remember this one. */
6246 if (!marginal_memb)
6247 marginal_memb = memb;
6251 /* Can convert integer zero to any pointer type. */
6252 if (null_pointer_constant)
6254 rhs = null_pointer_node;
6255 break;
6259 if (memb || marginal_memb)
6261 if (!memb)
6263 /* We have only a marginally acceptable member type;
6264 it needs a warning. */
6265 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6266 tree ttr = TREE_TYPE (rhstype);
6268 /* Const and volatile mean something different for function
6269 types, so the usual warnings are not appropriate. */
6270 if (TREE_CODE (ttr) == FUNCTION_TYPE
6271 && TREE_CODE (ttl) == FUNCTION_TYPE)
6273 /* Because const and volatile on functions are
6274 restrictions that say the function will not do
6275 certain things, it is okay to use a const or volatile
6276 function where an ordinary one is wanted, but not
6277 vice-versa. */
6278 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6279 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6280 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6281 OPT_Wdiscarded_qualifiers,
6282 G_("passing argument %d of %qE "
6283 "makes %q#v qualified function "
6284 "pointer from unqualified"),
6285 G_("assignment makes %q#v qualified "
6286 "function pointer from "
6287 "unqualified"),
6288 G_("initialization makes %q#v qualified "
6289 "function pointer from "
6290 "unqualified"),
6291 G_("return makes %q#v qualified function "
6292 "pointer from unqualified"),
6293 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6295 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6296 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6297 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6298 OPT_Wdiscarded_qualifiers,
6299 G_("passing argument %d of %qE discards "
6300 "%qv qualifier from pointer target type"),
6301 G_("assignment discards %qv qualifier "
6302 "from pointer target type"),
6303 G_("initialization discards %qv qualifier "
6304 "from pointer target type"),
6305 G_("return discards %qv qualifier from "
6306 "pointer target type"),
6307 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6309 memb = marginal_memb;
6312 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6313 pedwarn (location, OPT_Wpedantic,
6314 "ISO C prohibits argument conversion to union type");
6316 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6317 return build_constructor_single (type, memb, rhs);
6321 /* Conversions among pointers */
6322 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6323 && (coder == codel))
6325 tree ttl = TREE_TYPE (type);
6326 tree ttr = TREE_TYPE (rhstype);
6327 tree mvl = ttl;
6328 tree mvr = ttr;
6329 bool is_opaque_pointer;
6330 int target_cmp = 0; /* Cache comp_target_types () result. */
6331 addr_space_t asl;
6332 addr_space_t asr;
6334 if (TREE_CODE (mvl) != ARRAY_TYPE)
6335 mvl = (TYPE_ATOMIC (mvl)
6336 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6337 TYPE_QUAL_ATOMIC)
6338 : TYPE_MAIN_VARIANT (mvl));
6339 if (TREE_CODE (mvr) != ARRAY_TYPE)
6340 mvr = (TYPE_ATOMIC (mvr)
6341 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6342 TYPE_QUAL_ATOMIC)
6343 : TYPE_MAIN_VARIANT (mvr));
6344 /* Opaque pointers are treated like void pointers. */
6345 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6347 /* The Plan 9 compiler permits a pointer to a struct to be
6348 automatically converted into a pointer to an anonymous field
6349 within the struct. */
6350 if (flag_plan9_extensions
6351 && RECORD_OR_UNION_TYPE_P (mvl)
6352 && RECORD_OR_UNION_TYPE_P (mvr)
6353 && mvl != mvr)
6355 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6356 if (new_rhs != NULL_TREE)
6358 rhs = new_rhs;
6359 rhstype = TREE_TYPE (rhs);
6360 coder = TREE_CODE (rhstype);
6361 ttr = TREE_TYPE (rhstype);
6362 mvr = TYPE_MAIN_VARIANT (ttr);
6366 /* C++ does not allow the implicit conversion void* -> T*. However,
6367 for the purpose of reducing the number of false positives, we
6368 tolerate the special case of
6370 int *p = NULL;
6372 where NULL is typically defined in C to be '(void *) 0'. */
6373 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6374 warning_at (errtype == ic_argpass ? expr_loc : location,
6375 OPT_Wc___compat,
6376 "request for implicit conversion "
6377 "from %qT to %qT not permitted in C++", rhstype, type);
6379 /* See if the pointers point to incompatible address spaces. */
6380 asl = TYPE_ADDR_SPACE (ttl);
6381 asr = TYPE_ADDR_SPACE (ttr);
6382 if (!null_pointer_constant_p (rhs)
6383 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6385 switch (errtype)
6387 case ic_argpass:
6388 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6389 "non-enclosed address space", parmnum, rname);
6390 break;
6391 case ic_assign:
6392 error_at (location, "assignment from pointer to "
6393 "non-enclosed address space");
6394 break;
6395 case ic_init:
6396 error_at (location, "initialization from pointer to "
6397 "non-enclosed address space");
6398 break;
6399 case ic_return:
6400 error_at (location, "return from pointer to "
6401 "non-enclosed address space");
6402 break;
6403 default:
6404 gcc_unreachable ();
6406 return error_mark_node;
6409 /* Check if the right-hand side has a format attribute but the
6410 left-hand side doesn't. */
6411 if (warn_suggest_attribute_format
6412 && check_missing_format_attribute (type, rhstype))
6414 switch (errtype)
6416 case ic_argpass:
6417 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6418 "argument %d of %qE might be "
6419 "a candidate for a format attribute",
6420 parmnum, rname);
6421 break;
6422 case ic_assign:
6423 warning_at (location, OPT_Wsuggest_attribute_format,
6424 "assignment left-hand side might be "
6425 "a candidate for a format attribute");
6426 break;
6427 case ic_init:
6428 warning_at (location, OPT_Wsuggest_attribute_format,
6429 "initialization left-hand side might be "
6430 "a candidate for a format attribute");
6431 break;
6432 case ic_return:
6433 warning_at (location, OPT_Wsuggest_attribute_format,
6434 "return type might be "
6435 "a candidate for a format attribute");
6436 break;
6437 default:
6438 gcc_unreachable ();
6442 /* Any non-function converts to a [const][volatile] void *
6443 and vice versa; otherwise, targets must be the same.
6444 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6445 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6446 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6447 || (target_cmp = comp_target_types (location, type, rhstype))
6448 || is_opaque_pointer
6449 || ((c_common_unsigned_type (mvl)
6450 == c_common_unsigned_type (mvr))
6451 && (c_common_signed_type (mvl)
6452 == c_common_signed_type (mvr))
6453 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6455 /* Warn about loss of qualifers from pointers to arrays with
6456 qualifiers on the element type. */
6457 if (TREE_CODE (ttr) == ARRAY_TYPE)
6459 ttr = strip_array_types (ttr);
6460 ttl = strip_array_types (ttl);
6462 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6463 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6464 WARNING_FOR_QUALIFIERS (location, expr_loc,
6465 OPT_Wdiscarded_array_qualifiers,
6466 G_("passing argument %d of %qE discards "
6467 "%qv qualifier from pointer target type"),
6468 G_("assignment discards %qv qualifier "
6469 "from pointer target type"),
6470 G_("initialization discards %qv qualifier "
6471 "from pointer target type"),
6472 G_("return discards %qv qualifier from "
6473 "pointer target type"),
6474 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6476 else if (pedantic
6477 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6479 (VOID_TYPE_P (ttr)
6480 && !null_pointer_constant
6481 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6482 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6483 G_("ISO C forbids passing argument %d of "
6484 "%qE between function pointer "
6485 "and %<void *%>"),
6486 G_("ISO C forbids assignment between "
6487 "function pointer and %<void *%>"),
6488 G_("ISO C forbids initialization between "
6489 "function pointer and %<void *%>"),
6490 G_("ISO C forbids return between function "
6491 "pointer and %<void *%>"));
6492 /* Const and volatile mean something different for function types,
6493 so the usual warnings are not appropriate. */
6494 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6495 && TREE_CODE (ttl) != FUNCTION_TYPE)
6497 /* Don't warn about loss of qualifier for conversions from
6498 qualified void* to pointers to arrays with corresponding
6499 qualifier on the element type. */
6500 if (!pedantic)
6501 ttl = strip_array_types (ttl);
6503 /* Assignments between atomic and non-atomic objects are OK. */
6504 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6505 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6507 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6508 OPT_Wdiscarded_qualifiers,
6509 G_("passing argument %d of %qE discards "
6510 "%qv qualifier from pointer target type"),
6511 G_("assignment discards %qv qualifier "
6512 "from pointer target type"),
6513 G_("initialization discards %qv qualifier "
6514 "from pointer target type"),
6515 G_("return discards %qv qualifier from "
6516 "pointer target type"),
6517 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6519 /* If this is not a case of ignoring a mismatch in signedness,
6520 no warning. */
6521 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6522 || target_cmp)
6524 /* If there is a mismatch, do warn. */
6525 else if (warn_pointer_sign)
6526 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6527 G_("pointer targets in passing argument "
6528 "%d of %qE differ in signedness"),
6529 G_("pointer targets in assignment "
6530 "differ in signedness"),
6531 G_("pointer targets in initialization "
6532 "differ in signedness"),
6533 G_("pointer targets in return differ "
6534 "in signedness"));
6536 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6537 && TREE_CODE (ttr) == FUNCTION_TYPE)
6539 /* Because const and volatile on functions are restrictions
6540 that say the function will not do certain things,
6541 it is okay to use a const or volatile function
6542 where an ordinary one is wanted, but not vice-versa. */
6543 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6544 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6545 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6546 OPT_Wdiscarded_qualifiers,
6547 G_("passing argument %d of %qE makes "
6548 "%q#v qualified function pointer "
6549 "from unqualified"),
6550 G_("assignment makes %q#v qualified function "
6551 "pointer from unqualified"),
6552 G_("initialization makes %q#v qualified "
6553 "function pointer from unqualified"),
6554 G_("return makes %q#v qualified function "
6555 "pointer from unqualified"),
6556 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6559 else
6560 /* Avoid warning about the volatile ObjC EH puts on decls. */
6561 if (!objc_ok)
6562 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6563 OPT_Wincompatible_pointer_types,
6564 G_("passing argument %d of %qE from "
6565 "incompatible pointer type"),
6566 G_("assignment from incompatible pointer type"),
6567 G_("initialization from incompatible "
6568 "pointer type"),
6569 G_("return from incompatible pointer type"));
6571 return convert (type, rhs);
6573 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6575 /* ??? This should not be an error when inlining calls to
6576 unprototyped functions. */
6577 error_at (location, "invalid use of non-lvalue array");
6578 return error_mark_node;
6580 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6582 /* An explicit constant 0 can convert to a pointer,
6583 or one that results from arithmetic, even including
6584 a cast to integer type. */
6585 if (!null_pointer_constant)
6586 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6587 OPT_Wint_conversion,
6588 G_("passing argument %d of %qE makes "
6589 "pointer from integer without a cast"),
6590 G_("assignment makes pointer from integer "
6591 "without a cast"),
6592 G_("initialization makes pointer from "
6593 "integer without a cast"),
6594 G_("return makes pointer from integer "
6595 "without a cast"));
6597 return convert (type, rhs);
6599 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6601 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6602 OPT_Wint_conversion,
6603 G_("passing argument %d of %qE makes integer "
6604 "from pointer without a cast"),
6605 G_("assignment makes integer from pointer "
6606 "without a cast"),
6607 G_("initialization makes integer from pointer "
6608 "without a cast"),
6609 G_("return makes integer from pointer "
6610 "without a cast"));
6611 return convert (type, rhs);
6613 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6615 tree ret;
6616 bool save = in_late_binary_op;
6617 in_late_binary_op = true;
6618 ret = convert (type, rhs);
6619 in_late_binary_op = save;
6620 return ret;
6623 switch (errtype)
6625 case ic_argpass:
6626 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6627 rname);
6628 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6629 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6630 "expected %qT but argument is of type %qT", type, rhstype);
6631 break;
6632 case ic_assign:
6633 error_at (location, "incompatible types when assigning to type %qT from "
6634 "type %qT", type, rhstype);
6635 break;
6636 case ic_init:
6637 error_at (location,
6638 "incompatible types when initializing type %qT using type %qT",
6639 type, rhstype);
6640 break;
6641 case ic_return:
6642 error_at (location,
6643 "incompatible types when returning type %qT but %qT was "
6644 "expected", rhstype, type);
6645 break;
6646 default:
6647 gcc_unreachable ();
6650 return error_mark_node;
6653 /* If VALUE is a compound expr all of whose expressions are constant, then
6654 return its value. Otherwise, return error_mark_node.
6656 This is for handling COMPOUND_EXPRs as initializer elements
6657 which is allowed with a warning when -pedantic is specified. */
6659 static tree
6660 valid_compound_expr_initializer (tree value, tree endtype)
6662 if (TREE_CODE (value) == COMPOUND_EXPR)
6664 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6665 == error_mark_node)
6666 return error_mark_node;
6667 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6668 endtype);
6670 else if (!initializer_constant_valid_p (value, endtype))
6671 return error_mark_node;
6672 else
6673 return value;
6676 /* Perform appropriate conversions on the initial value of a variable,
6677 store it in the declaration DECL,
6678 and print any error messages that are appropriate.
6679 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6680 If the init is invalid, store an ERROR_MARK.
6682 INIT_LOC is the location of the initial value. */
6684 void
6685 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6687 tree value, type;
6688 bool npc = false;
6690 /* If variable's type was invalidly declared, just ignore it. */
6692 type = TREE_TYPE (decl);
6693 if (TREE_CODE (type) == ERROR_MARK)
6694 return;
6696 /* Digest the specified initializer into an expression. */
6698 if (init)
6699 npc = null_pointer_constant_p (init);
6700 value = digest_init (init_loc, type, init, origtype, npc,
6701 true, TREE_STATIC (decl));
6703 /* Store the expression if valid; else report error. */
6705 if (!in_system_header_at (input_location)
6706 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6707 warning (OPT_Wtraditional, "traditional C rejects automatic "
6708 "aggregate initialization");
6710 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6711 DECL_INITIAL (decl) = value;
6713 /* ANSI wants warnings about out-of-range constant initializers. */
6714 STRIP_TYPE_NOPS (value);
6715 if (TREE_STATIC (decl))
6716 constant_expression_warning (value);
6718 /* Check if we need to set array size from compound literal size. */
6719 if (TREE_CODE (type) == ARRAY_TYPE
6720 && TYPE_DOMAIN (type) == 0
6721 && value != error_mark_node)
6723 tree inside_init = init;
6725 STRIP_TYPE_NOPS (inside_init);
6726 inside_init = fold (inside_init);
6728 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6730 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6732 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6734 /* For int foo[] = (int [3]){1}; we need to set array size
6735 now since later on array initializer will be just the
6736 brace enclosed list of the compound literal. */
6737 tree etype = strip_array_types (TREE_TYPE (decl));
6738 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6739 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6740 layout_type (type);
6741 layout_decl (cldecl, 0);
6742 TREE_TYPE (decl)
6743 = c_build_qualified_type (type, TYPE_QUALS (etype));
6749 /* Methods for storing and printing names for error messages. */
6751 /* Implement a spelling stack that allows components of a name to be pushed
6752 and popped. Each element on the stack is this structure. */
6754 struct spelling
6756 int kind;
6757 union
6759 unsigned HOST_WIDE_INT i;
6760 const char *s;
6761 } u;
6764 #define SPELLING_STRING 1
6765 #define SPELLING_MEMBER 2
6766 #define SPELLING_BOUNDS 3
6768 static struct spelling *spelling; /* Next stack element (unused). */
6769 static struct spelling *spelling_base; /* Spelling stack base. */
6770 static int spelling_size; /* Size of the spelling stack. */
6772 /* Macros to save and restore the spelling stack around push_... functions.
6773 Alternative to SAVE_SPELLING_STACK. */
6775 #define SPELLING_DEPTH() (spelling - spelling_base)
6776 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6778 /* Push an element on the spelling stack with type KIND and assign VALUE
6779 to MEMBER. */
6781 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6783 int depth = SPELLING_DEPTH (); \
6785 if (depth >= spelling_size) \
6787 spelling_size += 10; \
6788 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6789 spelling_size); \
6790 RESTORE_SPELLING_DEPTH (depth); \
6793 spelling->kind = (KIND); \
6794 spelling->MEMBER = (VALUE); \
6795 spelling++; \
6798 /* Push STRING on the stack. Printed literally. */
6800 static void
6801 push_string (const char *string)
6803 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6806 /* Push a member name on the stack. Printed as '.' STRING. */
6808 static void
6809 push_member_name (tree decl)
6811 const char *const string
6812 = (DECL_NAME (decl)
6813 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6814 : _("<anonymous>"));
6815 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6818 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6820 static void
6821 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6823 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6826 /* Compute the maximum size in bytes of the printed spelling. */
6828 static int
6829 spelling_length (void)
6831 int size = 0;
6832 struct spelling *p;
6834 for (p = spelling_base; p < spelling; p++)
6836 if (p->kind == SPELLING_BOUNDS)
6837 size += 25;
6838 else
6839 size += strlen (p->u.s) + 1;
6842 return size;
6845 /* Print the spelling to BUFFER and return it. */
6847 static char *
6848 print_spelling (char *buffer)
6850 char *d = buffer;
6851 struct spelling *p;
6853 for (p = spelling_base; p < spelling; p++)
6854 if (p->kind == SPELLING_BOUNDS)
6856 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6857 d += strlen (d);
6859 else
6861 const char *s;
6862 if (p->kind == SPELLING_MEMBER)
6863 *d++ = '.';
6864 for (s = p->u.s; (*d = *s++); d++)
6867 *d++ = '\0';
6868 return buffer;
6871 /* Digest the parser output INIT as an initializer for type TYPE.
6872 Return a C expression of type TYPE to represent the initial value.
6874 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6876 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6878 If INIT is a string constant, STRICT_STRING is true if it is
6879 unparenthesized or we should not warn here for it being parenthesized.
6880 For other types of INIT, STRICT_STRING is not used.
6882 INIT_LOC is the location of the INIT.
6884 REQUIRE_CONSTANT requests an error if non-constant initializers or
6885 elements are seen. */
6887 static tree
6888 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6889 bool null_pointer_constant, bool strict_string,
6890 int require_constant)
6892 enum tree_code code = TREE_CODE (type);
6893 tree inside_init = init;
6894 tree semantic_type = NULL_TREE;
6895 bool maybe_const = true;
6897 if (type == error_mark_node
6898 || !init
6899 || error_operand_p (init))
6900 return error_mark_node;
6902 STRIP_TYPE_NOPS (inside_init);
6904 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6906 semantic_type = TREE_TYPE (inside_init);
6907 inside_init = TREE_OPERAND (inside_init, 0);
6909 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6910 inside_init = decl_constant_value_for_optimization (inside_init);
6912 /* Initialization of an array of chars from a string constant
6913 optionally enclosed in braces. */
6915 if (code == ARRAY_TYPE && inside_init
6916 && TREE_CODE (inside_init) == STRING_CST)
6918 tree typ1
6919 = (TYPE_ATOMIC (TREE_TYPE (type))
6920 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6921 TYPE_QUAL_ATOMIC)
6922 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6923 /* Note that an array could be both an array of character type
6924 and an array of wchar_t if wchar_t is signed char or unsigned
6925 char. */
6926 bool char_array = (typ1 == char_type_node
6927 || typ1 == signed_char_type_node
6928 || typ1 == unsigned_char_type_node);
6929 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6930 bool char16_array = !!comptypes (typ1, char16_type_node);
6931 bool char32_array = !!comptypes (typ1, char32_type_node);
6933 if (char_array || wchar_array || char16_array || char32_array)
6935 struct c_expr expr;
6936 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6937 expr.value = inside_init;
6938 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6939 expr.original_type = NULL;
6940 maybe_warn_string_init (init_loc, type, expr);
6942 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6943 pedwarn_init (init_loc, OPT_Wpedantic,
6944 "initialization of a flexible array member");
6946 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6947 TYPE_MAIN_VARIANT (type)))
6948 return inside_init;
6950 if (char_array)
6952 if (typ2 != char_type_node)
6954 error_init (init_loc, "char-array initialized from wide "
6955 "string");
6956 return error_mark_node;
6959 else
6961 if (typ2 == char_type_node)
6963 error_init (init_loc, "wide character array initialized "
6964 "from non-wide string");
6965 return error_mark_node;
6967 else if (!comptypes(typ1, typ2))
6969 error_init (init_loc, "wide character array initialized "
6970 "from incompatible wide string");
6971 return error_mark_node;
6975 TREE_TYPE (inside_init) = type;
6976 if (TYPE_DOMAIN (type) != 0
6977 && TYPE_SIZE (type) != 0
6978 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6980 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6982 /* Subtract the size of a single (possibly wide) character
6983 because it's ok to ignore the terminating null char
6984 that is counted in the length of the constant. */
6985 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6986 (len
6987 - (TYPE_PRECISION (typ1)
6988 / BITS_PER_UNIT))))
6989 pedwarn_init (init_loc, 0,
6990 ("initializer-string for array of chars "
6991 "is too long"));
6992 else if (warn_cxx_compat
6993 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6994 warning_at (init_loc, OPT_Wc___compat,
6995 ("initializer-string for array chars "
6996 "is too long for C++"));
6999 return inside_init;
7001 else if (INTEGRAL_TYPE_P (typ1))
7003 error_init (init_loc, "array of inappropriate type initialized "
7004 "from string constant");
7005 return error_mark_node;
7009 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7010 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7011 below and handle as a constructor. */
7012 if (code == VECTOR_TYPE
7013 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7014 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7015 && TREE_CONSTANT (inside_init))
7017 if (TREE_CODE (inside_init) == VECTOR_CST
7018 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7019 TYPE_MAIN_VARIANT (type)))
7020 return inside_init;
7022 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7024 unsigned HOST_WIDE_INT ix;
7025 tree value;
7026 bool constant_p = true;
7028 /* Iterate through elements and check if all constructor
7029 elements are *_CSTs. */
7030 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7031 if (!CONSTANT_CLASS_P (value))
7033 constant_p = false;
7034 break;
7037 if (constant_p)
7038 return build_vector_from_ctor (type,
7039 CONSTRUCTOR_ELTS (inside_init));
7043 if (warn_sequence_point)
7044 verify_sequence_points (inside_init);
7046 /* Any type can be initialized
7047 from an expression of the same type, optionally with braces. */
7049 if (inside_init && TREE_TYPE (inside_init) != 0
7050 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7051 TYPE_MAIN_VARIANT (type))
7052 || (code == ARRAY_TYPE
7053 && comptypes (TREE_TYPE (inside_init), type))
7054 || (code == VECTOR_TYPE
7055 && comptypes (TREE_TYPE (inside_init), type))
7056 || (code == POINTER_TYPE
7057 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7058 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7059 TREE_TYPE (type)))))
7061 if (code == POINTER_TYPE)
7063 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7065 if (TREE_CODE (inside_init) == STRING_CST
7066 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7067 inside_init = array_to_pointer_conversion
7068 (init_loc, inside_init);
7069 else
7071 error_init (init_loc, "invalid use of non-lvalue array");
7072 return error_mark_node;
7077 if (code == VECTOR_TYPE)
7078 /* Although the types are compatible, we may require a
7079 conversion. */
7080 inside_init = convert (type, inside_init);
7082 if (require_constant
7083 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7085 /* As an extension, allow initializing objects with static storage
7086 duration with compound literals (which are then treated just as
7087 the brace enclosed list they contain). Also allow this for
7088 vectors, as we can only assign them with compound literals. */
7089 if (flag_isoc99 && code != VECTOR_TYPE)
7090 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7091 "is not constant");
7092 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7093 inside_init = DECL_INITIAL (decl);
7096 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7097 && TREE_CODE (inside_init) != CONSTRUCTOR)
7099 error_init (init_loc, "array initialized from non-constant array "
7100 "expression");
7101 return error_mark_node;
7104 /* Compound expressions can only occur here if -Wpedantic or
7105 -pedantic-errors is specified. In the later case, we always want
7106 an error. In the former case, we simply want a warning. */
7107 if (require_constant && pedantic
7108 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7110 inside_init
7111 = valid_compound_expr_initializer (inside_init,
7112 TREE_TYPE (inside_init));
7113 if (inside_init == error_mark_node)
7114 error_init (init_loc, "initializer element is not constant");
7115 else
7116 pedwarn_init (init_loc, OPT_Wpedantic,
7117 "initializer element is not constant");
7118 if (flag_pedantic_errors)
7119 inside_init = error_mark_node;
7121 else if (require_constant
7122 && !initializer_constant_valid_p (inside_init,
7123 TREE_TYPE (inside_init)))
7125 error_init (init_loc, "initializer element is not constant");
7126 inside_init = error_mark_node;
7128 else if (require_constant && !maybe_const)
7129 pedwarn_init (init_loc, OPT_Wpedantic,
7130 "initializer element is not a constant expression");
7132 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7133 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7134 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7135 type, inside_init, origtype,
7136 ic_init, null_pointer_constant,
7137 NULL_TREE, NULL_TREE, 0);
7138 return inside_init;
7141 /* Handle scalar types, including conversions. */
7143 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7144 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7145 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7147 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7148 && (TREE_CODE (init) == STRING_CST
7149 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7150 inside_init = init = array_to_pointer_conversion (init_loc, init);
7151 if (semantic_type)
7152 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7153 inside_init);
7154 inside_init
7155 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7156 inside_init, origtype, ic_init,
7157 null_pointer_constant, NULL_TREE, NULL_TREE,
7160 /* Check to see if we have already given an error message. */
7161 if (inside_init == error_mark_node)
7163 else if (require_constant && !TREE_CONSTANT (inside_init))
7165 error_init (init_loc, "initializer element is not constant");
7166 inside_init = error_mark_node;
7168 else if (require_constant
7169 && !initializer_constant_valid_p (inside_init,
7170 TREE_TYPE (inside_init)))
7172 error_init (init_loc, "initializer element is not computable at "
7173 "load time");
7174 inside_init = error_mark_node;
7176 else if (require_constant && !maybe_const)
7177 pedwarn_init (init_loc, OPT_Wpedantic,
7178 "initializer element is not a constant expression");
7180 return inside_init;
7183 /* Come here only for records and arrays. */
7185 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7187 error_init (init_loc, "variable-sized object may not be initialized");
7188 return error_mark_node;
7191 error_init (init_loc, "invalid initializer");
7192 return error_mark_node;
7195 /* Handle initializers that use braces. */
7197 /* Type of object we are accumulating a constructor for.
7198 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7199 static tree constructor_type;
7201 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7202 left to fill. */
7203 static tree constructor_fields;
7205 /* For an ARRAY_TYPE, this is the specified index
7206 at which to store the next element we get. */
7207 static tree constructor_index;
7209 /* For an ARRAY_TYPE, this is the maximum index. */
7210 static tree constructor_max_index;
7212 /* For a RECORD_TYPE, this is the first field not yet written out. */
7213 static tree constructor_unfilled_fields;
7215 /* For an ARRAY_TYPE, this is the index of the first element
7216 not yet written out. */
7217 static tree constructor_unfilled_index;
7219 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7220 This is so we can generate gaps between fields, when appropriate. */
7221 static tree constructor_bit_index;
7223 /* If we are saving up the elements rather than allocating them,
7224 this is the list of elements so far (in reverse order,
7225 most recent first). */
7226 static vec<constructor_elt, va_gc> *constructor_elements;
7228 /* 1 if constructor should be incrementally stored into a constructor chain,
7229 0 if all the elements should be kept in AVL tree. */
7230 static int constructor_incremental;
7232 /* 1 if so far this constructor's elements are all compile-time constants. */
7233 static int constructor_constant;
7235 /* 1 if so far this constructor's elements are all valid address constants. */
7236 static int constructor_simple;
7238 /* 1 if this constructor has an element that cannot be part of a
7239 constant expression. */
7240 static int constructor_nonconst;
7242 /* 1 if this constructor is erroneous so far. */
7243 static int constructor_erroneous;
7245 /* 1 if this constructor is the universal zero initializer { 0 }. */
7246 static int constructor_zeroinit;
7248 /* Structure for managing pending initializer elements, organized as an
7249 AVL tree. */
7251 struct init_node
7253 struct init_node *left, *right;
7254 struct init_node *parent;
7255 int balance;
7256 tree purpose;
7257 tree value;
7258 tree origtype;
7261 /* Tree of pending elements at this constructor level.
7262 These are elements encountered out of order
7263 which belong at places we haven't reached yet in actually
7264 writing the output.
7265 Will never hold tree nodes across GC runs. */
7266 static struct init_node *constructor_pending_elts;
7268 /* The SPELLING_DEPTH of this constructor. */
7269 static int constructor_depth;
7271 /* DECL node for which an initializer is being read.
7272 0 means we are reading a constructor expression
7273 such as (struct foo) {...}. */
7274 static tree constructor_decl;
7276 /* Nonzero if this is an initializer for a top-level decl. */
7277 static int constructor_top_level;
7279 /* Nonzero if there were any member designators in this initializer. */
7280 static int constructor_designated;
7282 /* Nesting depth of designator list. */
7283 static int designator_depth;
7285 /* Nonzero if there were diagnosed errors in this designator list. */
7286 static int designator_erroneous;
7289 /* This stack has a level for each implicit or explicit level of
7290 structuring in the initializer, including the outermost one. It
7291 saves the values of most of the variables above. */
7293 struct constructor_range_stack;
7295 struct constructor_stack
7297 struct constructor_stack *next;
7298 tree type;
7299 tree fields;
7300 tree index;
7301 tree max_index;
7302 tree unfilled_index;
7303 tree unfilled_fields;
7304 tree bit_index;
7305 vec<constructor_elt, va_gc> *elements;
7306 struct init_node *pending_elts;
7307 int offset;
7308 int depth;
7309 /* If value nonzero, this value should replace the entire
7310 constructor at this level. */
7311 struct c_expr replacement_value;
7312 struct constructor_range_stack *range_stack;
7313 char constant;
7314 char simple;
7315 char nonconst;
7316 char implicit;
7317 char erroneous;
7318 char outer;
7319 char incremental;
7320 char designated;
7321 int designator_depth;
7324 static struct constructor_stack *constructor_stack;
7326 /* This stack represents designators from some range designator up to
7327 the last designator in the list. */
7329 struct constructor_range_stack
7331 struct constructor_range_stack *next, *prev;
7332 struct constructor_stack *stack;
7333 tree range_start;
7334 tree index;
7335 tree range_end;
7336 tree fields;
7339 static struct constructor_range_stack *constructor_range_stack;
7341 /* This stack records separate initializers that are nested.
7342 Nested initializers can't happen in ANSI C, but GNU C allows them
7343 in cases like { ... (struct foo) { ... } ... }. */
7345 struct initializer_stack
7347 struct initializer_stack *next;
7348 tree decl;
7349 struct constructor_stack *constructor_stack;
7350 struct constructor_range_stack *constructor_range_stack;
7351 vec<constructor_elt, va_gc> *elements;
7352 struct spelling *spelling;
7353 struct spelling *spelling_base;
7354 int spelling_size;
7355 char top_level;
7356 char require_constant_value;
7357 char require_constant_elements;
7360 static struct initializer_stack *initializer_stack;
7362 /* Prepare to parse and output the initializer for variable DECL. */
7364 void
7365 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7367 const char *locus;
7368 struct initializer_stack *p = XNEW (struct initializer_stack);
7370 p->decl = constructor_decl;
7371 p->require_constant_value = require_constant_value;
7372 p->require_constant_elements = require_constant_elements;
7373 p->constructor_stack = constructor_stack;
7374 p->constructor_range_stack = constructor_range_stack;
7375 p->elements = constructor_elements;
7376 p->spelling = spelling;
7377 p->spelling_base = spelling_base;
7378 p->spelling_size = spelling_size;
7379 p->top_level = constructor_top_level;
7380 p->next = initializer_stack;
7381 initializer_stack = p;
7383 constructor_decl = decl;
7384 constructor_designated = 0;
7385 constructor_top_level = top_level;
7387 if (decl != 0 && decl != error_mark_node)
7389 require_constant_value = TREE_STATIC (decl);
7390 require_constant_elements
7391 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7392 /* For a scalar, you can always use any value to initialize,
7393 even within braces. */
7394 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7395 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7397 else
7399 require_constant_value = 0;
7400 require_constant_elements = 0;
7401 locus = _("(anonymous)");
7404 constructor_stack = 0;
7405 constructor_range_stack = 0;
7407 found_missing_braces = 0;
7409 spelling_base = 0;
7410 spelling_size = 0;
7411 RESTORE_SPELLING_DEPTH (0);
7413 if (locus)
7414 push_string (locus);
7417 void
7418 finish_init (void)
7420 struct initializer_stack *p = initializer_stack;
7422 /* Free the whole constructor stack of this initializer. */
7423 while (constructor_stack)
7425 struct constructor_stack *q = constructor_stack;
7426 constructor_stack = q->next;
7427 free (q);
7430 gcc_assert (!constructor_range_stack);
7432 /* Pop back to the data of the outer initializer (if any). */
7433 free (spelling_base);
7435 constructor_decl = p->decl;
7436 require_constant_value = p->require_constant_value;
7437 require_constant_elements = p->require_constant_elements;
7438 constructor_stack = p->constructor_stack;
7439 constructor_range_stack = p->constructor_range_stack;
7440 constructor_elements = p->elements;
7441 spelling = p->spelling;
7442 spelling_base = p->spelling_base;
7443 spelling_size = p->spelling_size;
7444 constructor_top_level = p->top_level;
7445 initializer_stack = p->next;
7446 free (p);
7449 /* Call here when we see the initializer is surrounded by braces.
7450 This is instead of a call to push_init_level;
7451 it is matched by a call to pop_init_level.
7453 TYPE is the type to initialize, for a constructor expression.
7454 For an initializer for a decl, TYPE is zero. */
7456 void
7457 really_start_incremental_init (tree type)
7459 struct constructor_stack *p = XNEW (struct constructor_stack);
7461 if (type == 0)
7462 type = TREE_TYPE (constructor_decl);
7464 if (VECTOR_TYPE_P (type)
7465 && TYPE_VECTOR_OPAQUE (type))
7466 error ("opaque vector types cannot be initialized");
7468 p->type = constructor_type;
7469 p->fields = constructor_fields;
7470 p->index = constructor_index;
7471 p->max_index = constructor_max_index;
7472 p->unfilled_index = constructor_unfilled_index;
7473 p->unfilled_fields = constructor_unfilled_fields;
7474 p->bit_index = constructor_bit_index;
7475 p->elements = constructor_elements;
7476 p->constant = constructor_constant;
7477 p->simple = constructor_simple;
7478 p->nonconst = constructor_nonconst;
7479 p->erroneous = constructor_erroneous;
7480 p->pending_elts = constructor_pending_elts;
7481 p->depth = constructor_depth;
7482 p->replacement_value.value = 0;
7483 p->replacement_value.original_code = ERROR_MARK;
7484 p->replacement_value.original_type = NULL;
7485 p->implicit = 0;
7486 p->range_stack = 0;
7487 p->outer = 0;
7488 p->incremental = constructor_incremental;
7489 p->designated = constructor_designated;
7490 p->designator_depth = designator_depth;
7491 p->next = 0;
7492 constructor_stack = p;
7494 constructor_constant = 1;
7495 constructor_simple = 1;
7496 constructor_nonconst = 0;
7497 constructor_depth = SPELLING_DEPTH ();
7498 constructor_elements = NULL;
7499 constructor_pending_elts = 0;
7500 constructor_type = type;
7501 constructor_incremental = 1;
7502 constructor_designated = 0;
7503 constructor_zeroinit = 1;
7504 designator_depth = 0;
7505 designator_erroneous = 0;
7507 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7509 constructor_fields = TYPE_FIELDS (constructor_type);
7510 /* Skip any nameless bit fields at the beginning. */
7511 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7512 && DECL_NAME (constructor_fields) == 0)
7513 constructor_fields = DECL_CHAIN (constructor_fields);
7515 constructor_unfilled_fields = constructor_fields;
7516 constructor_bit_index = bitsize_zero_node;
7518 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7520 if (TYPE_DOMAIN (constructor_type))
7522 constructor_max_index
7523 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7525 /* Detect non-empty initializations of zero-length arrays. */
7526 if (constructor_max_index == NULL_TREE
7527 && TYPE_SIZE (constructor_type))
7528 constructor_max_index = integer_minus_one_node;
7530 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7531 to initialize VLAs will cause a proper error; avoid tree
7532 checking errors as well by setting a safe value. */
7533 if (constructor_max_index
7534 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7535 constructor_max_index = integer_minus_one_node;
7537 constructor_index
7538 = convert (bitsizetype,
7539 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7541 else
7543 constructor_index = bitsize_zero_node;
7544 constructor_max_index = NULL_TREE;
7547 constructor_unfilled_index = constructor_index;
7549 else if (VECTOR_TYPE_P (constructor_type))
7551 /* Vectors are like simple fixed-size arrays. */
7552 constructor_max_index =
7553 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7554 constructor_index = bitsize_zero_node;
7555 constructor_unfilled_index = constructor_index;
7557 else
7559 /* Handle the case of int x = {5}; */
7560 constructor_fields = constructor_type;
7561 constructor_unfilled_fields = constructor_type;
7565 /* Called when we see an open brace for a nested initializer. Finish
7566 off any pending levels with implicit braces. */
7567 void
7568 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7570 while (constructor_stack->implicit)
7572 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7573 && constructor_fields == 0)
7574 process_init_element (input_location,
7575 pop_init_level (loc, 1, braced_init_obstack),
7576 true, braced_init_obstack);
7577 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7578 && constructor_max_index
7579 && tree_int_cst_lt (constructor_max_index,
7580 constructor_index))
7581 process_init_element (input_location,
7582 pop_init_level (loc, 1, braced_init_obstack),
7583 true, braced_init_obstack);
7584 else
7585 break;
7589 /* Push down into a subobject, for initialization.
7590 If this is for an explicit set of braces, IMPLICIT is 0.
7591 If it is because the next element belongs at a lower level,
7592 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7594 void
7595 push_init_level (location_t loc, int implicit,
7596 struct obstack *braced_init_obstack)
7598 struct constructor_stack *p;
7599 tree value = NULL_TREE;
7601 /* Unless this is an explicit brace, we need to preserve previous
7602 content if any. */
7603 if (implicit)
7605 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
7606 value = find_init_member (constructor_fields, braced_init_obstack);
7607 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7608 value = find_init_member (constructor_index, braced_init_obstack);
7611 p = XNEW (struct constructor_stack);
7612 p->type = constructor_type;
7613 p->fields = constructor_fields;
7614 p->index = constructor_index;
7615 p->max_index = constructor_max_index;
7616 p->unfilled_index = constructor_unfilled_index;
7617 p->unfilled_fields = constructor_unfilled_fields;
7618 p->bit_index = constructor_bit_index;
7619 p->elements = constructor_elements;
7620 p->constant = constructor_constant;
7621 p->simple = constructor_simple;
7622 p->nonconst = constructor_nonconst;
7623 p->erroneous = constructor_erroneous;
7624 p->pending_elts = constructor_pending_elts;
7625 p->depth = constructor_depth;
7626 p->replacement_value.value = 0;
7627 p->replacement_value.original_code = ERROR_MARK;
7628 p->replacement_value.original_type = NULL;
7629 p->implicit = implicit;
7630 p->outer = 0;
7631 p->incremental = constructor_incremental;
7632 p->designated = constructor_designated;
7633 p->designator_depth = designator_depth;
7634 p->next = constructor_stack;
7635 p->range_stack = 0;
7636 constructor_stack = p;
7638 constructor_constant = 1;
7639 constructor_simple = 1;
7640 constructor_nonconst = 0;
7641 constructor_depth = SPELLING_DEPTH ();
7642 constructor_elements = NULL;
7643 constructor_incremental = 1;
7644 constructor_designated = 0;
7645 constructor_pending_elts = 0;
7646 if (!implicit)
7648 p->range_stack = constructor_range_stack;
7649 constructor_range_stack = 0;
7650 designator_depth = 0;
7651 designator_erroneous = 0;
7654 /* Don't die if an entire brace-pair level is superfluous
7655 in the containing level. */
7656 if (constructor_type == 0)
7658 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
7660 /* Don't die if there are extra init elts at the end. */
7661 if (constructor_fields == 0)
7662 constructor_type = 0;
7663 else
7665 constructor_type = TREE_TYPE (constructor_fields);
7666 push_member_name (constructor_fields);
7667 constructor_depth++;
7669 /* If upper initializer is designated, then mark this as
7670 designated too to prevent bogus warnings. */
7671 constructor_designated = p->designated;
7673 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7675 constructor_type = TREE_TYPE (constructor_type);
7676 push_array_bounds (tree_to_uhwi (constructor_index));
7677 constructor_depth++;
7680 if (constructor_type == 0)
7682 error_init (loc, "extra brace group at end of initializer");
7683 constructor_fields = 0;
7684 constructor_unfilled_fields = 0;
7685 return;
7688 if (value && TREE_CODE (value) == CONSTRUCTOR)
7690 constructor_constant = TREE_CONSTANT (value);
7691 constructor_simple = TREE_STATIC (value);
7692 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7693 constructor_elements = CONSTRUCTOR_ELTS (value);
7694 if (!vec_safe_is_empty (constructor_elements)
7695 && (TREE_CODE (constructor_type) == RECORD_TYPE
7696 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7697 set_nonincremental_init (braced_init_obstack);
7700 if (implicit == 1)
7701 found_missing_braces = 1;
7703 if (RECORD_OR_UNION_TYPE_P (constructor_type))
7705 constructor_fields = TYPE_FIELDS (constructor_type);
7706 /* Skip any nameless bit fields at the beginning. */
7707 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7708 && DECL_NAME (constructor_fields) == 0)
7709 constructor_fields = DECL_CHAIN (constructor_fields);
7711 constructor_unfilled_fields = constructor_fields;
7712 constructor_bit_index = bitsize_zero_node;
7714 else if (VECTOR_TYPE_P (constructor_type))
7716 /* Vectors are like simple fixed-size arrays. */
7717 constructor_max_index =
7718 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7719 constructor_index = bitsize_int (0);
7720 constructor_unfilled_index = constructor_index;
7722 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7724 if (TYPE_DOMAIN (constructor_type))
7726 constructor_max_index
7727 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7729 /* Detect non-empty initializations of zero-length arrays. */
7730 if (constructor_max_index == NULL_TREE
7731 && TYPE_SIZE (constructor_type))
7732 constructor_max_index = integer_minus_one_node;
7734 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7735 to initialize VLAs will cause a proper error; avoid tree
7736 checking errors as well by setting a safe value. */
7737 if (constructor_max_index
7738 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7739 constructor_max_index = integer_minus_one_node;
7741 constructor_index
7742 = convert (bitsizetype,
7743 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7745 else
7746 constructor_index = bitsize_zero_node;
7748 constructor_unfilled_index = constructor_index;
7749 if (value && TREE_CODE (value) == STRING_CST)
7751 /* We need to split the char/wchar array into individual
7752 characters, so that we don't have to special case it
7753 everywhere. */
7754 set_nonincremental_init_from_string (value, braced_init_obstack);
7757 else
7759 if (constructor_type != error_mark_node)
7760 warning_init (input_location, 0, "braces around scalar initializer");
7761 constructor_fields = constructor_type;
7762 constructor_unfilled_fields = constructor_type;
7766 /* At the end of an implicit or explicit brace level,
7767 finish up that level of constructor. If a single expression
7768 with redundant braces initialized that level, return the
7769 c_expr structure for that expression. Otherwise, the original_code
7770 element is set to ERROR_MARK.
7771 If we were outputting the elements as they are read, return 0 as the value
7772 from inner levels (process_init_element ignores that),
7773 but return error_mark_node as the value from the outermost level
7774 (that's what we want to put in DECL_INITIAL).
7775 Otherwise, return a CONSTRUCTOR expression as the value. */
7777 struct c_expr
7778 pop_init_level (location_t loc, int implicit,
7779 struct obstack *braced_init_obstack)
7781 struct constructor_stack *p;
7782 struct c_expr ret;
7783 ret.value = 0;
7784 ret.original_code = ERROR_MARK;
7785 ret.original_type = NULL;
7787 if (implicit == 0)
7789 /* When we come to an explicit close brace,
7790 pop any inner levels that didn't have explicit braces. */
7791 while (constructor_stack->implicit)
7792 process_init_element (input_location,
7793 pop_init_level (loc, 1, braced_init_obstack),
7794 true, braced_init_obstack);
7795 gcc_assert (!constructor_range_stack);
7798 /* Now output all pending elements. */
7799 constructor_incremental = 1;
7800 output_pending_init_elements (1, braced_init_obstack);
7802 p = constructor_stack;
7804 /* Error for initializing a flexible array member, or a zero-length
7805 array member in an inappropriate context. */
7806 if (constructor_type && constructor_fields
7807 && TREE_CODE (constructor_type) == ARRAY_TYPE
7808 && TYPE_DOMAIN (constructor_type)
7809 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7811 /* Silently discard empty initializations. The parser will
7812 already have pedwarned for empty brackets. */
7813 if (integer_zerop (constructor_unfilled_index))
7814 constructor_type = NULL_TREE;
7815 else
7817 gcc_assert (!TYPE_SIZE (constructor_type));
7819 if (constructor_depth > 2)
7820 error_init (loc, "initialization of flexible array member in a nested context");
7821 else
7822 pedwarn_init (loc, OPT_Wpedantic,
7823 "initialization of a flexible array member");
7825 /* We have already issued an error message for the existence
7826 of a flexible array member not at the end of the structure.
7827 Discard the initializer so that we do not die later. */
7828 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7829 constructor_type = NULL_TREE;
7833 switch (vec_safe_length (constructor_elements))
7835 case 0:
7836 /* Initialization with { } counts as zeroinit. */
7837 constructor_zeroinit = 1;
7838 break;
7839 case 1:
7840 /* This might be zeroinit as well. */
7841 if (integer_zerop ((*constructor_elements)[0].value))
7842 constructor_zeroinit = 1;
7843 break;
7844 default:
7845 /* If the constructor has more than one element, it can't be { 0 }. */
7846 constructor_zeroinit = 0;
7847 break;
7850 /* Warn when some structs are initialized with direct aggregation. */
7851 if (!implicit && found_missing_braces && warn_missing_braces
7852 && !constructor_zeroinit)
7853 warning_init (loc, OPT_Wmissing_braces,
7854 "missing braces around initializer");
7856 /* Warn when some struct elements are implicitly initialized to zero. */
7857 if (warn_missing_field_initializers
7858 && constructor_type
7859 && TREE_CODE (constructor_type) == RECORD_TYPE
7860 && constructor_unfilled_fields)
7862 /* Do not warn for flexible array members or zero-length arrays. */
7863 while (constructor_unfilled_fields
7864 && (!DECL_SIZE (constructor_unfilled_fields)
7865 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7866 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7868 if (constructor_unfilled_fields
7869 /* Do not warn if this level of the initializer uses member
7870 designators; it is likely to be deliberate. */
7871 && !constructor_designated
7872 /* Do not warn about initializing with { 0 } or with { }. */
7873 && !constructor_zeroinit)
7875 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7876 "missing initializer for field %qD of %qT",
7877 constructor_unfilled_fields,
7878 constructor_type))
7879 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7880 "%qD declared here", constructor_unfilled_fields);
7884 /* Pad out the end of the structure. */
7885 if (p->replacement_value.value)
7886 /* If this closes a superfluous brace pair,
7887 just pass out the element between them. */
7888 ret = p->replacement_value;
7889 else if (constructor_type == 0)
7891 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
7892 && TREE_CODE (constructor_type) != ARRAY_TYPE
7893 && !VECTOR_TYPE_P (constructor_type))
7895 /* A nonincremental scalar initializer--just return
7896 the element, after verifying there is just one. */
7897 if (vec_safe_is_empty (constructor_elements))
7899 if (!constructor_erroneous)
7900 error_init (loc, "empty scalar initializer");
7901 ret.value = error_mark_node;
7903 else if (vec_safe_length (constructor_elements) != 1)
7905 error_init (loc, "extra elements in scalar initializer");
7906 ret.value = (*constructor_elements)[0].value;
7908 else
7909 ret.value = (*constructor_elements)[0].value;
7911 else
7913 if (constructor_erroneous)
7914 ret.value = error_mark_node;
7915 else
7917 ret.value = build_constructor (constructor_type,
7918 constructor_elements);
7919 if (constructor_constant)
7920 TREE_CONSTANT (ret.value) = 1;
7921 if (constructor_constant && constructor_simple)
7922 TREE_STATIC (ret.value) = 1;
7923 if (constructor_nonconst)
7924 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7928 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7930 if (constructor_nonconst)
7931 ret.original_code = C_MAYBE_CONST_EXPR;
7932 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7933 ret.original_code = ERROR_MARK;
7936 constructor_type = p->type;
7937 constructor_fields = p->fields;
7938 constructor_index = p->index;
7939 constructor_max_index = p->max_index;
7940 constructor_unfilled_index = p->unfilled_index;
7941 constructor_unfilled_fields = p->unfilled_fields;
7942 constructor_bit_index = p->bit_index;
7943 constructor_elements = p->elements;
7944 constructor_constant = p->constant;
7945 constructor_simple = p->simple;
7946 constructor_nonconst = p->nonconst;
7947 constructor_erroneous = p->erroneous;
7948 constructor_incremental = p->incremental;
7949 constructor_designated = p->designated;
7950 designator_depth = p->designator_depth;
7951 constructor_pending_elts = p->pending_elts;
7952 constructor_depth = p->depth;
7953 if (!p->implicit)
7954 constructor_range_stack = p->range_stack;
7955 RESTORE_SPELLING_DEPTH (constructor_depth);
7957 constructor_stack = p->next;
7958 free (p);
7960 if (ret.value == 0 && constructor_stack == 0)
7961 ret.value = error_mark_node;
7962 return ret;
7965 /* Common handling for both array range and field name designators.
7966 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7968 static int
7969 set_designator (location_t loc, int array,
7970 struct obstack *braced_init_obstack)
7972 tree subtype;
7973 enum tree_code subcode;
7975 /* Don't die if an entire brace-pair level is superfluous
7976 in the containing level. */
7977 if (constructor_type == 0)
7978 return 1;
7980 /* If there were errors in this designator list already, bail out
7981 silently. */
7982 if (designator_erroneous)
7983 return 1;
7985 if (!designator_depth)
7987 gcc_assert (!constructor_range_stack);
7989 /* Designator list starts at the level of closest explicit
7990 braces. */
7991 while (constructor_stack->implicit)
7992 process_init_element (input_location,
7993 pop_init_level (loc, 1, braced_init_obstack),
7994 true, braced_init_obstack);
7995 constructor_designated = 1;
7996 return 0;
7999 switch (TREE_CODE (constructor_type))
8001 case RECORD_TYPE:
8002 case UNION_TYPE:
8003 subtype = TREE_TYPE (constructor_fields);
8004 if (subtype != error_mark_node)
8005 subtype = TYPE_MAIN_VARIANT (subtype);
8006 break;
8007 case ARRAY_TYPE:
8008 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8009 break;
8010 default:
8011 gcc_unreachable ();
8014 subcode = TREE_CODE (subtype);
8015 if (array && subcode != ARRAY_TYPE)
8017 error_init (loc, "array index in non-array initializer");
8018 return 1;
8020 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8022 error_init (loc, "field name not in record or union initializer");
8023 return 1;
8026 constructor_designated = 1;
8027 finish_implicit_inits (loc, braced_init_obstack);
8028 push_init_level (loc, 2, braced_init_obstack);
8029 return 0;
8032 /* If there are range designators in designator list, push a new designator
8033 to constructor_range_stack. RANGE_END is end of such stack range or
8034 NULL_TREE if there is no range designator at this level. */
8036 static void
8037 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8039 struct constructor_range_stack *p;
8041 p = (struct constructor_range_stack *)
8042 obstack_alloc (braced_init_obstack,
8043 sizeof (struct constructor_range_stack));
8044 p->prev = constructor_range_stack;
8045 p->next = 0;
8046 p->fields = constructor_fields;
8047 p->range_start = constructor_index;
8048 p->index = constructor_index;
8049 p->stack = constructor_stack;
8050 p->range_end = range_end;
8051 if (constructor_range_stack)
8052 constructor_range_stack->next = p;
8053 constructor_range_stack = p;
8056 /* Within an array initializer, specify the next index to be initialized.
8057 FIRST is that index. If LAST is nonzero, then initialize a range
8058 of indices, running from FIRST through LAST. */
8060 void
8061 set_init_index (location_t loc, tree first, tree last,
8062 struct obstack *braced_init_obstack)
8064 if (set_designator (loc, 1, braced_init_obstack))
8065 return;
8067 designator_erroneous = 1;
8069 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8070 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8072 error_init (loc, "array index in initializer not of integer type");
8073 return;
8076 if (TREE_CODE (first) != INTEGER_CST)
8078 first = c_fully_fold (first, false, NULL);
8079 if (TREE_CODE (first) == INTEGER_CST)
8080 pedwarn_init (loc, OPT_Wpedantic,
8081 "array index in initializer is not "
8082 "an integer constant expression");
8085 if (last && TREE_CODE (last) != INTEGER_CST)
8087 last = c_fully_fold (last, false, NULL);
8088 if (TREE_CODE (last) == INTEGER_CST)
8089 pedwarn_init (loc, OPT_Wpedantic,
8090 "array index in initializer is not "
8091 "an integer constant expression");
8094 if (TREE_CODE (first) != INTEGER_CST)
8095 error_init (loc, "nonconstant array index in initializer");
8096 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
8097 error_init (loc, "nonconstant array index in initializer");
8098 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8099 error_init (loc, "array index in non-array initializer");
8100 else if (tree_int_cst_sgn (first) == -1)
8101 error_init (loc, "array index in initializer exceeds array bounds");
8102 else if (constructor_max_index
8103 && tree_int_cst_lt (constructor_max_index, first))
8104 error_init (loc, "array index in initializer exceeds array bounds");
8105 else
8107 constant_expression_warning (first);
8108 if (last)
8109 constant_expression_warning (last);
8110 constructor_index = convert (bitsizetype, first);
8111 if (tree_int_cst_lt (constructor_index, first))
8113 constructor_index = copy_node (constructor_index);
8114 TREE_OVERFLOW (constructor_index) = 1;
8117 if (last)
8119 if (tree_int_cst_equal (first, last))
8120 last = 0;
8121 else if (tree_int_cst_lt (last, first))
8123 error_init (loc, "empty index range in initializer");
8124 last = 0;
8126 else
8128 last = convert (bitsizetype, last);
8129 if (constructor_max_index != 0
8130 && tree_int_cst_lt (constructor_max_index, last))
8132 error_init (loc, "array index range in initializer exceeds "
8133 "array bounds");
8134 last = 0;
8139 designator_depth++;
8140 designator_erroneous = 0;
8141 if (constructor_range_stack || last)
8142 push_range_stack (last, braced_init_obstack);
8146 /* Within a struct initializer, specify the next field to be initialized. */
8148 void
8149 set_init_label (location_t loc, tree fieldname,
8150 struct obstack *braced_init_obstack)
8152 tree field;
8154 if (set_designator (loc, 0, braced_init_obstack))
8155 return;
8157 designator_erroneous = 1;
8159 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8161 error_init (loc, "field name not in record or union initializer");
8162 return;
8165 field = lookup_field (constructor_type, fieldname);
8167 if (field == 0)
8168 error_at (loc, "unknown field %qE specified in initializer", fieldname);
8169 else
8172 constructor_fields = TREE_VALUE (field);
8173 designator_depth++;
8174 designator_erroneous = 0;
8175 if (constructor_range_stack)
8176 push_range_stack (NULL_TREE, braced_init_obstack);
8177 field = TREE_CHAIN (field);
8178 if (field)
8180 if (set_designator (loc, 0, braced_init_obstack))
8181 return;
8184 while (field != NULL_TREE);
8187 /* Add a new initializer to the tree of pending initializers. PURPOSE
8188 identifies the initializer, either array index or field in a structure.
8189 VALUE is the value of that index or field. If ORIGTYPE is not
8190 NULL_TREE, it is the original type of VALUE.
8192 IMPLICIT is true if value comes from pop_init_level (1),
8193 the new initializer has been merged with the existing one
8194 and thus no warnings should be emitted about overriding an
8195 existing initializer. */
8197 static void
8198 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8199 bool implicit, struct obstack *braced_init_obstack)
8201 struct init_node *p, **q, *r;
8203 q = &constructor_pending_elts;
8204 p = 0;
8206 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8208 while (*q != 0)
8210 p = *q;
8211 if (tree_int_cst_lt (purpose, p->purpose))
8212 q = &p->left;
8213 else if (tree_int_cst_lt (p->purpose, purpose))
8214 q = &p->right;
8215 else
8217 if (!implicit)
8219 if (TREE_SIDE_EFFECTS (p->value))
8220 warning_init (loc, OPT_Woverride_init_side_effects,
8221 "initialized field with side-effects "
8222 "overwritten");
8223 else if (warn_override_init)
8224 warning_init (loc, OPT_Woverride_init,
8225 "initialized field overwritten");
8227 p->value = value;
8228 p->origtype = origtype;
8229 return;
8233 else
8235 tree bitpos;
8237 bitpos = bit_position (purpose);
8238 while (*q != NULL)
8240 p = *q;
8241 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8242 q = &p->left;
8243 else if (p->purpose != purpose)
8244 q = &p->right;
8245 else
8247 if (!implicit)
8249 if (TREE_SIDE_EFFECTS (p->value))
8250 warning_init (loc, OPT_Woverride_init_side_effects,
8251 "initialized field with side-effects "
8252 "overwritten");
8253 else if (warn_override_init)
8254 warning_init (loc, OPT_Woverride_init,
8255 "initialized field overwritten");
8257 p->value = value;
8258 p->origtype = origtype;
8259 return;
8264 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8265 sizeof (struct init_node));
8266 r->purpose = purpose;
8267 r->value = value;
8268 r->origtype = origtype;
8270 *q = r;
8271 r->parent = p;
8272 r->left = 0;
8273 r->right = 0;
8274 r->balance = 0;
8276 while (p)
8278 struct init_node *s;
8280 if (r == p->left)
8282 if (p->balance == 0)
8283 p->balance = -1;
8284 else if (p->balance < 0)
8286 if (r->balance < 0)
8288 /* L rotation. */
8289 p->left = r->right;
8290 if (p->left)
8291 p->left->parent = p;
8292 r->right = p;
8294 p->balance = 0;
8295 r->balance = 0;
8297 s = p->parent;
8298 p->parent = r;
8299 r->parent = s;
8300 if (s)
8302 if (s->left == p)
8303 s->left = r;
8304 else
8305 s->right = r;
8307 else
8308 constructor_pending_elts = r;
8310 else
8312 /* LR rotation. */
8313 struct init_node *t = r->right;
8315 r->right = t->left;
8316 if (r->right)
8317 r->right->parent = r;
8318 t->left = r;
8320 p->left = t->right;
8321 if (p->left)
8322 p->left->parent = p;
8323 t->right = p;
8325 p->balance = t->balance < 0;
8326 r->balance = -(t->balance > 0);
8327 t->balance = 0;
8329 s = p->parent;
8330 p->parent = t;
8331 r->parent = t;
8332 t->parent = s;
8333 if (s)
8335 if (s->left == p)
8336 s->left = t;
8337 else
8338 s->right = t;
8340 else
8341 constructor_pending_elts = t;
8343 break;
8345 else
8347 /* p->balance == +1; growth of left side balances the node. */
8348 p->balance = 0;
8349 break;
8352 else /* r == p->right */
8354 if (p->balance == 0)
8355 /* Growth propagation from right side. */
8356 p->balance++;
8357 else if (p->balance > 0)
8359 if (r->balance > 0)
8361 /* R rotation. */
8362 p->right = r->left;
8363 if (p->right)
8364 p->right->parent = p;
8365 r->left = p;
8367 p->balance = 0;
8368 r->balance = 0;
8370 s = p->parent;
8371 p->parent = r;
8372 r->parent = s;
8373 if (s)
8375 if (s->left == p)
8376 s->left = r;
8377 else
8378 s->right = r;
8380 else
8381 constructor_pending_elts = r;
8383 else /* r->balance == -1 */
8385 /* RL rotation */
8386 struct init_node *t = r->left;
8388 r->left = t->right;
8389 if (r->left)
8390 r->left->parent = r;
8391 t->right = r;
8393 p->right = t->left;
8394 if (p->right)
8395 p->right->parent = p;
8396 t->left = p;
8398 r->balance = (t->balance < 0);
8399 p->balance = -(t->balance > 0);
8400 t->balance = 0;
8402 s = p->parent;
8403 p->parent = t;
8404 r->parent = t;
8405 t->parent = s;
8406 if (s)
8408 if (s->left == p)
8409 s->left = t;
8410 else
8411 s->right = t;
8413 else
8414 constructor_pending_elts = t;
8416 break;
8418 else
8420 /* p->balance == -1; growth of right side balances the node. */
8421 p->balance = 0;
8422 break;
8426 r = p;
8427 p = p->parent;
8431 /* Build AVL tree from a sorted chain. */
8433 static void
8434 set_nonincremental_init (struct obstack * braced_init_obstack)
8436 unsigned HOST_WIDE_INT ix;
8437 tree index, value;
8439 if (TREE_CODE (constructor_type) != RECORD_TYPE
8440 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8441 return;
8443 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8444 add_pending_init (input_location, index, value, NULL_TREE, true,
8445 braced_init_obstack);
8446 constructor_elements = NULL;
8447 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8449 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8450 /* Skip any nameless bit fields at the beginning. */
8451 while (constructor_unfilled_fields != 0
8452 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8453 && DECL_NAME (constructor_unfilled_fields) == 0)
8454 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8457 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8459 if (TYPE_DOMAIN (constructor_type))
8460 constructor_unfilled_index
8461 = convert (bitsizetype,
8462 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8463 else
8464 constructor_unfilled_index = bitsize_zero_node;
8466 constructor_incremental = 0;
8469 /* Build AVL tree from a string constant. */
8471 static void
8472 set_nonincremental_init_from_string (tree str,
8473 struct obstack * braced_init_obstack)
8475 tree value, purpose, type;
8476 HOST_WIDE_INT val[2];
8477 const char *p, *end;
8478 int byte, wchar_bytes, charwidth, bitpos;
8480 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8482 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8483 charwidth = TYPE_PRECISION (char_type_node);
8484 type = TREE_TYPE (constructor_type);
8485 p = TREE_STRING_POINTER (str);
8486 end = p + TREE_STRING_LENGTH (str);
8488 for (purpose = bitsize_zero_node;
8489 p < end
8490 && !(constructor_max_index
8491 && tree_int_cst_lt (constructor_max_index, purpose));
8492 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8494 if (wchar_bytes == 1)
8496 val[0] = (unsigned char) *p++;
8497 val[1] = 0;
8499 else
8501 val[1] = 0;
8502 val[0] = 0;
8503 for (byte = 0; byte < wchar_bytes; byte++)
8505 if (BYTES_BIG_ENDIAN)
8506 bitpos = (wchar_bytes - byte - 1) * charwidth;
8507 else
8508 bitpos = byte * charwidth;
8509 val[bitpos % HOST_BITS_PER_WIDE_INT]
8510 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8511 << (bitpos % HOST_BITS_PER_WIDE_INT);
8515 if (!TYPE_UNSIGNED (type))
8517 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8518 if (bitpos < HOST_BITS_PER_WIDE_INT)
8520 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8522 val[0] |= HOST_WIDE_INT_M1U << bitpos;
8523 val[1] = -1;
8526 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8528 if (val[0] < 0)
8529 val[1] = -1;
8531 else if (val[1] & (((HOST_WIDE_INT) 1)
8532 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8533 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
8536 value = wide_int_to_tree (type,
8537 wide_int::from_array (val, 2,
8538 HOST_BITS_PER_WIDE_INT * 2));
8539 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8540 braced_init_obstack);
8543 constructor_incremental = 0;
8546 /* Return value of FIELD in pending initializer or zero if the field was
8547 not initialized yet. */
8549 static tree
8550 find_init_member (tree field, struct obstack * braced_init_obstack)
8552 struct init_node *p;
8554 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8556 if (constructor_incremental
8557 && tree_int_cst_lt (field, constructor_unfilled_index))
8558 set_nonincremental_init (braced_init_obstack);
8560 p = constructor_pending_elts;
8561 while (p)
8563 if (tree_int_cst_lt (field, p->purpose))
8564 p = p->left;
8565 else if (tree_int_cst_lt (p->purpose, field))
8566 p = p->right;
8567 else
8568 return p->value;
8571 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8573 tree bitpos = bit_position (field);
8575 if (constructor_incremental
8576 && (!constructor_unfilled_fields
8577 || tree_int_cst_lt (bitpos,
8578 bit_position (constructor_unfilled_fields))))
8579 set_nonincremental_init (braced_init_obstack);
8581 p = constructor_pending_elts;
8582 while (p)
8584 if (field == p->purpose)
8585 return p->value;
8586 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8587 p = p->left;
8588 else
8589 p = p->right;
8592 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8594 if (!vec_safe_is_empty (constructor_elements)
8595 && (constructor_elements->last ().index == field))
8596 return constructor_elements->last ().value;
8598 return 0;
8601 /* "Output" the next constructor element.
8602 At top level, really output it to assembler code now.
8603 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8604 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8605 TYPE is the data type that the containing data type wants here.
8606 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8607 If VALUE is a string constant, STRICT_STRING is true if it is
8608 unparenthesized or we should not warn here for it being parenthesized.
8609 For other types of VALUE, STRICT_STRING is not used.
8611 PENDING if non-nil means output pending elements that belong
8612 right after this element. (PENDING is normally 1;
8613 it is 0 while outputting pending elements, to avoid recursion.)
8615 IMPLICIT is true if value comes from pop_init_level (1),
8616 the new initializer has been merged with the existing one
8617 and thus no warnings should be emitted about overriding an
8618 existing initializer. */
8620 static void
8621 output_init_element (location_t loc, tree value, tree origtype,
8622 bool strict_string, tree type, tree field, int pending,
8623 bool implicit, struct obstack * braced_init_obstack)
8625 tree semantic_type = NULL_TREE;
8626 bool maybe_const = true;
8627 bool npc;
8629 if (type == error_mark_node || value == error_mark_node)
8631 constructor_erroneous = 1;
8632 return;
8634 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8635 && (TREE_CODE (value) == STRING_CST
8636 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8637 && !(TREE_CODE (value) == STRING_CST
8638 && TREE_CODE (type) == ARRAY_TYPE
8639 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8640 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8641 TYPE_MAIN_VARIANT (type)))
8642 value = array_to_pointer_conversion (input_location, value);
8644 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8645 && require_constant_value && pending)
8647 /* As an extension, allow initializing objects with static storage
8648 duration with compound literals (which are then treated just as
8649 the brace enclosed list they contain). */
8650 if (flag_isoc99)
8651 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8652 "constant");
8653 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8654 value = DECL_INITIAL (decl);
8657 npc = null_pointer_constant_p (value);
8658 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8660 semantic_type = TREE_TYPE (value);
8661 value = TREE_OPERAND (value, 0);
8663 value = c_fully_fold (value, require_constant_value, &maybe_const);
8665 if (value == error_mark_node)
8666 constructor_erroneous = 1;
8667 else if (!TREE_CONSTANT (value))
8668 constructor_constant = 0;
8669 else if (!initializer_constant_valid_p (value,
8670 TREE_TYPE (value),
8671 AGGREGATE_TYPE_P (constructor_type)
8672 && TYPE_REVERSE_STORAGE_ORDER
8673 (constructor_type))
8674 || (RECORD_OR_UNION_TYPE_P (constructor_type)
8675 && DECL_C_BIT_FIELD (field)
8676 && TREE_CODE (value) != INTEGER_CST))
8677 constructor_simple = 0;
8678 if (!maybe_const)
8679 constructor_nonconst = 1;
8681 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8683 if (require_constant_value)
8685 error_init (loc, "initializer element is not constant");
8686 value = error_mark_node;
8688 else if (require_constant_elements)
8689 pedwarn (loc, OPT_Wpedantic,
8690 "initializer element is not computable at load time");
8692 else if (!maybe_const
8693 && (require_constant_value || require_constant_elements))
8694 pedwarn_init (loc, OPT_Wpedantic,
8695 "initializer element is not a constant expression");
8697 /* Issue -Wc++-compat warnings about initializing a bitfield with
8698 enum type. */
8699 if (warn_cxx_compat
8700 && field != NULL_TREE
8701 && TREE_CODE (field) == FIELD_DECL
8702 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8703 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8704 != TYPE_MAIN_VARIANT (type))
8705 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8707 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8708 if (checktype != error_mark_node
8709 && (TYPE_MAIN_VARIANT (checktype)
8710 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8711 warning_init (loc, OPT_Wc___compat,
8712 "enum conversion in initialization is invalid in C++");
8715 /* If this field is empty (and not at the end of structure),
8716 don't do anything other than checking the initializer. */
8717 if (field
8718 && (TREE_TYPE (field) == error_mark_node
8719 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8720 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8721 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8722 || DECL_CHAIN (field)))))
8723 return;
8725 if (semantic_type)
8726 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8727 value = digest_init (loc, type, value, origtype, npc, strict_string,
8728 require_constant_value);
8729 if (value == error_mark_node)
8731 constructor_erroneous = 1;
8732 return;
8734 if (require_constant_value || require_constant_elements)
8735 constant_expression_warning (value);
8737 /* If this element doesn't come next in sequence,
8738 put it on constructor_pending_elts. */
8739 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8740 && (!constructor_incremental
8741 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8743 if (constructor_incremental
8744 && tree_int_cst_lt (field, constructor_unfilled_index))
8745 set_nonincremental_init (braced_init_obstack);
8747 add_pending_init (loc, field, value, origtype, implicit,
8748 braced_init_obstack);
8749 return;
8751 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8752 && (!constructor_incremental
8753 || field != constructor_unfilled_fields))
8755 /* We do this for records but not for unions. In a union,
8756 no matter which field is specified, it can be initialized
8757 right away since it starts at the beginning of the union. */
8758 if (constructor_incremental)
8760 if (!constructor_unfilled_fields)
8761 set_nonincremental_init (braced_init_obstack);
8762 else
8764 tree bitpos, unfillpos;
8766 bitpos = bit_position (field);
8767 unfillpos = bit_position (constructor_unfilled_fields);
8769 if (tree_int_cst_lt (bitpos, unfillpos))
8770 set_nonincremental_init (braced_init_obstack);
8774 add_pending_init (loc, field, value, origtype, implicit,
8775 braced_init_obstack);
8776 return;
8778 else if (TREE_CODE (constructor_type) == UNION_TYPE
8779 && !vec_safe_is_empty (constructor_elements))
8781 if (!implicit)
8783 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8784 warning_init (loc, OPT_Woverride_init_side_effects,
8785 "initialized field with side-effects overwritten");
8786 else if (warn_override_init)
8787 warning_init (loc, OPT_Woverride_init,
8788 "initialized field overwritten");
8791 /* We can have just one union field set. */
8792 constructor_elements = NULL;
8795 /* Otherwise, output this element either to
8796 constructor_elements or to the assembler file. */
8798 constructor_elt celt = {field, value};
8799 vec_safe_push (constructor_elements, celt);
8801 /* Advance the variable that indicates sequential elements output. */
8802 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8803 constructor_unfilled_index
8804 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8805 bitsize_one_node);
8806 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8808 constructor_unfilled_fields
8809 = DECL_CHAIN (constructor_unfilled_fields);
8811 /* Skip any nameless bit fields. */
8812 while (constructor_unfilled_fields != 0
8813 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8814 && DECL_NAME (constructor_unfilled_fields) == 0)
8815 constructor_unfilled_fields =
8816 DECL_CHAIN (constructor_unfilled_fields);
8818 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8819 constructor_unfilled_fields = 0;
8821 /* Now output any pending elements which have become next. */
8822 if (pending)
8823 output_pending_init_elements (0, braced_init_obstack);
8826 /* Output any pending elements which have become next.
8827 As we output elements, constructor_unfilled_{fields,index}
8828 advances, which may cause other elements to become next;
8829 if so, they too are output.
8831 If ALL is 0, we return when there are
8832 no more pending elements to output now.
8834 If ALL is 1, we output space as necessary so that
8835 we can output all the pending elements. */
8836 static void
8837 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8839 struct init_node *elt = constructor_pending_elts;
8840 tree next;
8842 retry:
8844 /* Look through the whole pending tree.
8845 If we find an element that should be output now,
8846 output it. Otherwise, set NEXT to the element
8847 that comes first among those still pending. */
8849 next = 0;
8850 while (elt)
8852 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8854 if (tree_int_cst_equal (elt->purpose,
8855 constructor_unfilled_index))
8856 output_init_element (input_location, elt->value, elt->origtype,
8857 true, TREE_TYPE (constructor_type),
8858 constructor_unfilled_index, 0, false,
8859 braced_init_obstack);
8860 else if (tree_int_cst_lt (constructor_unfilled_index,
8861 elt->purpose))
8863 /* Advance to the next smaller node. */
8864 if (elt->left)
8865 elt = elt->left;
8866 else
8868 /* We have reached the smallest node bigger than the
8869 current unfilled index. Fill the space first. */
8870 next = elt->purpose;
8871 break;
8874 else
8876 /* Advance to the next bigger node. */
8877 if (elt->right)
8878 elt = elt->right;
8879 else
8881 /* We have reached the biggest node in a subtree. Find
8882 the parent of it, which is the next bigger node. */
8883 while (elt->parent && elt->parent->right == elt)
8884 elt = elt->parent;
8885 elt = elt->parent;
8886 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8887 elt->purpose))
8889 next = elt->purpose;
8890 break;
8895 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8897 tree ctor_unfilled_bitpos, elt_bitpos;
8899 /* If the current record is complete we are done. */
8900 if (constructor_unfilled_fields == 0)
8901 break;
8903 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8904 elt_bitpos = bit_position (elt->purpose);
8905 /* We can't compare fields here because there might be empty
8906 fields in between. */
8907 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8909 constructor_unfilled_fields = elt->purpose;
8910 output_init_element (input_location, elt->value, elt->origtype,
8911 true, TREE_TYPE (elt->purpose),
8912 elt->purpose, 0, false,
8913 braced_init_obstack);
8915 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8917 /* Advance to the next smaller node. */
8918 if (elt->left)
8919 elt = elt->left;
8920 else
8922 /* We have reached the smallest node bigger than the
8923 current unfilled field. Fill the space first. */
8924 next = elt->purpose;
8925 break;
8928 else
8930 /* Advance to the next bigger node. */
8931 if (elt->right)
8932 elt = elt->right;
8933 else
8935 /* We have reached the biggest node in a subtree. Find
8936 the parent of it, which is the next bigger node. */
8937 while (elt->parent && elt->parent->right == elt)
8938 elt = elt->parent;
8939 elt = elt->parent;
8940 if (elt
8941 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8942 bit_position (elt->purpose))))
8944 next = elt->purpose;
8945 break;
8952 /* Ordinarily return, but not if we want to output all
8953 and there are elements left. */
8954 if (!(all && next != 0))
8955 return;
8957 /* If it's not incremental, just skip over the gap, so that after
8958 jumping to retry we will output the next successive element. */
8959 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8960 constructor_unfilled_fields = next;
8961 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8962 constructor_unfilled_index = next;
8964 /* ELT now points to the node in the pending tree with the next
8965 initializer to output. */
8966 goto retry;
8969 /* Add one non-braced element to the current constructor level.
8970 This adjusts the current position within the constructor's type.
8971 This may also start or terminate implicit levels
8972 to handle a partly-braced initializer.
8974 Once this has found the correct level for the new element,
8975 it calls output_init_element.
8977 IMPLICIT is true if value comes from pop_init_level (1),
8978 the new initializer has been merged with the existing one
8979 and thus no warnings should be emitted about overriding an
8980 existing initializer. */
8982 void
8983 process_init_element (location_t loc, struct c_expr value, bool implicit,
8984 struct obstack * braced_init_obstack)
8986 tree orig_value = value.value;
8987 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8988 bool strict_string = value.original_code == STRING_CST;
8989 bool was_designated = designator_depth != 0;
8991 designator_depth = 0;
8992 designator_erroneous = 0;
8994 if (!implicit && value.value && !integer_zerop (value.value))
8995 constructor_zeroinit = 0;
8997 /* Handle superfluous braces around string cst as in
8998 char x[] = {"foo"}; */
8999 if (string_flag
9000 && constructor_type
9001 && !was_designated
9002 && TREE_CODE (constructor_type) == ARRAY_TYPE
9003 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9004 && integer_zerop (constructor_unfilled_index))
9006 if (constructor_stack->replacement_value.value)
9007 error_init (loc, "excess elements in char array initializer");
9008 constructor_stack->replacement_value = value;
9009 return;
9012 if (constructor_stack->replacement_value.value != 0)
9014 error_init (loc, "excess elements in struct initializer");
9015 return;
9018 /* Ignore elements of a brace group if it is entirely superfluous
9019 and has already been diagnosed. */
9020 if (constructor_type == 0)
9021 return;
9023 if (!implicit && warn_designated_init && !was_designated
9024 && TREE_CODE (constructor_type) == RECORD_TYPE
9025 && lookup_attribute ("designated_init",
9026 TYPE_ATTRIBUTES (constructor_type)))
9027 warning_init (loc,
9028 OPT_Wdesignated_init,
9029 "positional initialization of field "
9030 "in %<struct%> declared with %<designated_init%> attribute");
9032 /* If we've exhausted any levels that didn't have braces,
9033 pop them now. */
9034 while (constructor_stack->implicit)
9036 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9037 && constructor_fields == 0)
9038 process_init_element (loc,
9039 pop_init_level (loc, 1, braced_init_obstack),
9040 true, braced_init_obstack);
9041 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9042 || VECTOR_TYPE_P (constructor_type))
9043 && constructor_max_index
9044 && tree_int_cst_lt (constructor_max_index,
9045 constructor_index))
9046 process_init_element (loc,
9047 pop_init_level (loc, 1, braced_init_obstack),
9048 true, braced_init_obstack);
9049 else
9050 break;
9053 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9054 if (constructor_range_stack)
9056 /* If value is a compound literal and we'll be just using its
9057 content, don't put it into a SAVE_EXPR. */
9058 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9059 || !require_constant_value)
9061 tree semantic_type = NULL_TREE;
9062 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9064 semantic_type = TREE_TYPE (value.value);
9065 value.value = TREE_OPERAND (value.value, 0);
9067 value.value = c_save_expr (value.value);
9068 if (semantic_type)
9069 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9070 value.value);
9074 while (1)
9076 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9078 tree fieldtype;
9079 enum tree_code fieldcode;
9081 if (constructor_fields == 0)
9083 pedwarn_init (loc, 0, "excess elements in struct initializer");
9084 break;
9087 fieldtype = TREE_TYPE (constructor_fields);
9088 if (fieldtype != error_mark_node)
9089 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9090 fieldcode = TREE_CODE (fieldtype);
9092 /* Error for non-static initialization of a flexible array member. */
9093 if (fieldcode == ARRAY_TYPE
9094 && !require_constant_value
9095 && TYPE_SIZE (fieldtype) == NULL_TREE
9096 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9098 error_init (loc, "non-static initialization of a flexible "
9099 "array member");
9100 break;
9103 /* Error for initialization of a flexible array member with
9104 a string constant if the structure is in an array. E.g.:
9105 struct S { int x; char y[]; };
9106 struct S s[] = { { 1, "foo" } };
9107 is invalid. */
9108 if (string_flag
9109 && fieldcode == ARRAY_TYPE
9110 && constructor_depth > 1
9111 && TYPE_SIZE (fieldtype) == NULL_TREE
9112 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9114 bool in_array_p = false;
9115 for (struct constructor_stack *p = constructor_stack;
9116 p && p->type; p = p->next)
9117 if (TREE_CODE (p->type) == ARRAY_TYPE)
9119 in_array_p = true;
9120 break;
9122 if (in_array_p)
9124 error_init (loc, "initialization of flexible array "
9125 "member in a nested context");
9126 break;
9130 /* Accept a string constant to initialize a subarray. */
9131 if (value.value != 0
9132 && fieldcode == ARRAY_TYPE
9133 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9134 && string_flag)
9135 value.value = orig_value;
9136 /* Otherwise, if we have come to a subaggregate,
9137 and we don't have an element of its type, push into it. */
9138 else if (value.value != 0
9139 && value.value != error_mark_node
9140 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9141 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9142 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9144 push_init_level (loc, 1, braced_init_obstack);
9145 continue;
9148 if (value.value)
9150 push_member_name (constructor_fields);
9151 output_init_element (loc, value.value, value.original_type,
9152 strict_string, fieldtype,
9153 constructor_fields, 1, implicit,
9154 braced_init_obstack);
9155 RESTORE_SPELLING_DEPTH (constructor_depth);
9157 else
9158 /* Do the bookkeeping for an element that was
9159 directly output as a constructor. */
9161 /* For a record, keep track of end position of last field. */
9162 if (DECL_SIZE (constructor_fields))
9163 constructor_bit_index
9164 = size_binop_loc (input_location, PLUS_EXPR,
9165 bit_position (constructor_fields),
9166 DECL_SIZE (constructor_fields));
9168 /* If the current field was the first one not yet written out,
9169 it isn't now, so update. */
9170 if (constructor_unfilled_fields == constructor_fields)
9172 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9173 /* Skip any nameless bit fields. */
9174 while (constructor_unfilled_fields != 0
9175 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9176 && DECL_NAME (constructor_unfilled_fields) == 0)
9177 constructor_unfilled_fields =
9178 DECL_CHAIN (constructor_unfilled_fields);
9182 constructor_fields = DECL_CHAIN (constructor_fields);
9183 /* Skip any nameless bit fields at the beginning. */
9184 while (constructor_fields != 0
9185 && DECL_C_BIT_FIELD (constructor_fields)
9186 && DECL_NAME (constructor_fields) == 0)
9187 constructor_fields = DECL_CHAIN (constructor_fields);
9189 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9191 tree fieldtype;
9192 enum tree_code fieldcode;
9194 if (constructor_fields == 0)
9196 pedwarn_init (loc, 0,
9197 "excess elements in union initializer");
9198 break;
9201 fieldtype = TREE_TYPE (constructor_fields);
9202 if (fieldtype != error_mark_node)
9203 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9204 fieldcode = TREE_CODE (fieldtype);
9206 /* Warn that traditional C rejects initialization of unions.
9207 We skip the warning if the value is zero. This is done
9208 under the assumption that the zero initializer in user
9209 code appears conditioned on e.g. __STDC__ to avoid
9210 "missing initializer" warnings and relies on default
9211 initialization to zero in the traditional C case.
9212 We also skip the warning if the initializer is designated,
9213 again on the assumption that this must be conditional on
9214 __STDC__ anyway (and we've already complained about the
9215 member-designator already). */
9216 if (!in_system_header_at (input_location) && !constructor_designated
9217 && !(value.value && (integer_zerop (value.value)
9218 || real_zerop (value.value))))
9219 warning (OPT_Wtraditional, "traditional C rejects initialization "
9220 "of unions");
9222 /* Accept a string constant to initialize a subarray. */
9223 if (value.value != 0
9224 && fieldcode == ARRAY_TYPE
9225 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9226 && string_flag)
9227 value.value = orig_value;
9228 /* Otherwise, if we have come to a subaggregate,
9229 and we don't have an element of its type, push into it. */
9230 else if (value.value != 0
9231 && value.value != error_mark_node
9232 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9233 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9234 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9236 push_init_level (loc, 1, braced_init_obstack);
9237 continue;
9240 if (value.value)
9242 push_member_name (constructor_fields);
9243 output_init_element (loc, value.value, value.original_type,
9244 strict_string, fieldtype,
9245 constructor_fields, 1, implicit,
9246 braced_init_obstack);
9247 RESTORE_SPELLING_DEPTH (constructor_depth);
9249 else
9250 /* Do the bookkeeping for an element that was
9251 directly output as a constructor. */
9253 constructor_bit_index = DECL_SIZE (constructor_fields);
9254 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9257 constructor_fields = 0;
9259 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9261 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9262 enum tree_code eltcode = TREE_CODE (elttype);
9264 /* Accept a string constant to initialize a subarray. */
9265 if (value.value != 0
9266 && eltcode == ARRAY_TYPE
9267 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9268 && string_flag)
9269 value.value = orig_value;
9270 /* Otherwise, if we have come to a subaggregate,
9271 and we don't have an element of its type, push into it. */
9272 else if (value.value != 0
9273 && value.value != error_mark_node
9274 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9275 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9276 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9278 push_init_level (loc, 1, braced_init_obstack);
9279 continue;
9282 if (constructor_max_index != 0
9283 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9284 || integer_all_onesp (constructor_max_index)))
9286 pedwarn_init (loc, 0,
9287 "excess elements in array initializer");
9288 break;
9291 /* Now output the actual element. */
9292 if (value.value)
9294 push_array_bounds (tree_to_uhwi (constructor_index));
9295 output_init_element (loc, value.value, value.original_type,
9296 strict_string, elttype,
9297 constructor_index, 1, implicit,
9298 braced_init_obstack);
9299 RESTORE_SPELLING_DEPTH (constructor_depth);
9302 constructor_index
9303 = size_binop_loc (input_location, PLUS_EXPR,
9304 constructor_index, bitsize_one_node);
9306 if (!value.value)
9307 /* If we are doing the bookkeeping for an element that was
9308 directly output as a constructor, we must update
9309 constructor_unfilled_index. */
9310 constructor_unfilled_index = constructor_index;
9312 else if (VECTOR_TYPE_P (constructor_type))
9314 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9316 /* Do a basic check of initializer size. Note that vectors
9317 always have a fixed size derived from their type. */
9318 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9320 pedwarn_init (loc, 0,
9321 "excess elements in vector initializer");
9322 break;
9325 /* Now output the actual element. */
9326 if (value.value)
9328 if (TREE_CODE (value.value) == VECTOR_CST)
9329 elttype = TYPE_MAIN_VARIANT (constructor_type);
9330 output_init_element (loc, value.value, value.original_type,
9331 strict_string, elttype,
9332 constructor_index, 1, implicit,
9333 braced_init_obstack);
9336 constructor_index
9337 = size_binop_loc (input_location,
9338 PLUS_EXPR, constructor_index, bitsize_one_node);
9340 if (!value.value)
9341 /* If we are doing the bookkeeping for an element that was
9342 directly output as a constructor, we must update
9343 constructor_unfilled_index. */
9344 constructor_unfilled_index = constructor_index;
9347 /* Handle the sole element allowed in a braced initializer
9348 for a scalar variable. */
9349 else if (constructor_type != error_mark_node
9350 && constructor_fields == 0)
9352 pedwarn_init (loc, 0,
9353 "excess elements in scalar initializer");
9354 break;
9356 else
9358 if (value.value)
9359 output_init_element (loc, value.value, value.original_type,
9360 strict_string, constructor_type,
9361 NULL_TREE, 1, implicit,
9362 braced_init_obstack);
9363 constructor_fields = 0;
9366 /* Handle range initializers either at this level or anywhere higher
9367 in the designator stack. */
9368 if (constructor_range_stack)
9370 struct constructor_range_stack *p, *range_stack;
9371 int finish = 0;
9373 range_stack = constructor_range_stack;
9374 constructor_range_stack = 0;
9375 while (constructor_stack != range_stack->stack)
9377 gcc_assert (constructor_stack->implicit);
9378 process_init_element (loc,
9379 pop_init_level (loc, 1,
9380 braced_init_obstack),
9381 true, braced_init_obstack);
9383 for (p = range_stack;
9384 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9385 p = p->prev)
9387 gcc_assert (constructor_stack->implicit);
9388 process_init_element (loc,
9389 pop_init_level (loc, 1,
9390 braced_init_obstack),
9391 true, braced_init_obstack);
9394 p->index = size_binop_loc (input_location,
9395 PLUS_EXPR, p->index, bitsize_one_node);
9396 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9397 finish = 1;
9399 while (1)
9401 constructor_index = p->index;
9402 constructor_fields = p->fields;
9403 if (finish && p->range_end && p->index == p->range_start)
9405 finish = 0;
9406 p->prev = 0;
9408 p = p->next;
9409 if (!p)
9410 break;
9411 finish_implicit_inits (loc, braced_init_obstack);
9412 push_init_level (loc, 2, braced_init_obstack);
9413 p->stack = constructor_stack;
9414 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9415 p->index = p->range_start;
9418 if (!finish)
9419 constructor_range_stack = range_stack;
9420 continue;
9423 break;
9426 constructor_range_stack = 0;
9429 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9430 (guaranteed to be 'volatile' or null) and ARGS (represented using
9431 an ASM_EXPR node). */
9432 tree
9433 build_asm_stmt (tree cv_qualifier, tree args)
9435 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9436 ASM_VOLATILE_P (args) = 1;
9437 return add_stmt (args);
9440 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9441 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9442 SIMPLE indicates whether there was anything at all after the
9443 string in the asm expression -- asm("blah") and asm("blah" : )
9444 are subtly different. We use a ASM_EXPR node to represent this. */
9445 tree
9446 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9447 tree clobbers, tree labels, bool simple)
9449 tree tail;
9450 tree args;
9451 int i;
9452 const char *constraint;
9453 const char **oconstraints;
9454 bool allows_mem, allows_reg, is_inout;
9455 int ninputs, noutputs;
9457 ninputs = list_length (inputs);
9458 noutputs = list_length (outputs);
9459 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9461 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9463 /* Remove output conversions that change the type but not the mode. */
9464 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9466 tree output = TREE_VALUE (tail);
9468 output = c_fully_fold (output, false, NULL);
9470 /* ??? Really, this should not be here. Users should be using a
9471 proper lvalue, dammit. But there's a long history of using casts
9472 in the output operands. In cases like longlong.h, this becomes a
9473 primitive form of typechecking -- if the cast can be removed, then
9474 the output operand had a type of the proper width; otherwise we'll
9475 get an error. Gross, but ... */
9476 STRIP_NOPS (output);
9478 if (!lvalue_or_else (loc, output, lv_asm))
9479 output = error_mark_node;
9481 if (output != error_mark_node
9482 && (TREE_READONLY (output)
9483 || TYPE_READONLY (TREE_TYPE (output))
9484 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
9485 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9486 readonly_error (loc, output, lv_asm);
9488 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9489 oconstraints[i] = constraint;
9491 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9492 &allows_mem, &allows_reg, &is_inout))
9494 /* If the operand is going to end up in memory,
9495 mark it addressable. */
9496 if (!allows_reg && !c_mark_addressable (output))
9497 output = error_mark_node;
9498 if (!(!allows_reg && allows_mem)
9499 && output != error_mark_node
9500 && VOID_TYPE_P (TREE_TYPE (output)))
9502 error_at (loc, "invalid use of void expression");
9503 output = error_mark_node;
9506 else
9507 output = error_mark_node;
9509 TREE_VALUE (tail) = output;
9512 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9514 tree input;
9516 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9517 input = TREE_VALUE (tail);
9519 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9520 oconstraints, &allows_mem, &allows_reg))
9522 /* If the operand is going to end up in memory,
9523 mark it addressable. */
9524 if (!allows_reg && allows_mem)
9526 input = c_fully_fold (input, false, NULL);
9528 /* Strip the nops as we allow this case. FIXME, this really
9529 should be rejected or made deprecated. */
9530 STRIP_NOPS (input);
9531 if (!c_mark_addressable (input))
9532 input = error_mark_node;
9534 else
9536 struct c_expr expr;
9537 memset (&expr, 0, sizeof (expr));
9538 expr.value = input;
9539 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9540 input = c_fully_fold (expr.value, false, NULL);
9542 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9544 error_at (loc, "invalid use of void expression");
9545 input = error_mark_node;
9549 else
9550 input = error_mark_node;
9552 TREE_VALUE (tail) = input;
9555 /* ASMs with labels cannot have outputs. This should have been
9556 enforced by the parser. */
9557 gcc_assert (outputs == NULL || labels == NULL);
9559 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9561 /* asm statements without outputs, including simple ones, are treated
9562 as volatile. */
9563 ASM_INPUT_P (args) = simple;
9564 ASM_VOLATILE_P (args) = (noutputs == 0);
9566 return args;
9569 /* Generate a goto statement to LABEL. LOC is the location of the
9570 GOTO. */
9572 tree
9573 c_finish_goto_label (location_t loc, tree label)
9575 tree decl = lookup_label_for_goto (loc, label);
9576 if (!decl)
9577 return NULL_TREE;
9578 TREE_USED (decl) = 1;
9580 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9581 SET_EXPR_LOCATION (t, loc);
9582 return add_stmt (t);
9586 /* Generate a computed goto statement to EXPR. LOC is the location of
9587 the GOTO. */
9589 tree
9590 c_finish_goto_ptr (location_t loc, tree expr)
9592 tree t;
9593 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9594 expr = c_fully_fold (expr, false, NULL);
9595 expr = convert (ptr_type_node, expr);
9596 t = build1 (GOTO_EXPR, void_type_node, expr);
9597 SET_EXPR_LOCATION (t, loc);
9598 return add_stmt (t);
9601 /* Generate a C `return' statement. RETVAL is the expression for what
9602 to return, or a null pointer for `return;' with no value. LOC is
9603 the location of the return statement, or the location of the expression,
9604 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9605 is the original type of RETVAL. */
9607 tree
9608 c_finish_return (location_t loc, tree retval, tree origtype)
9610 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9611 bool no_warning = false;
9612 bool npc = false;
9613 size_t rank = 0;
9615 /* Use the expansion point to handle cases such as returning NULL
9616 in a function returning void. */
9617 source_location xloc = expansion_point_location_if_in_system_header (loc);
9619 if (TREE_THIS_VOLATILE (current_function_decl))
9620 warning_at (xloc, 0,
9621 "function declared %<noreturn%> has a %<return%> statement");
9623 if (flag_cilkplus && contains_array_notation_expr (retval))
9625 /* Array notations are allowed in a return statement if it is inside a
9626 built-in array notation reduction function. */
9627 if (!find_rank (loc, retval, retval, false, &rank))
9628 return error_mark_node;
9629 if (rank >= 1)
9631 error_at (loc, "array notation expression cannot be used as a "
9632 "return value");
9633 return error_mark_node;
9636 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9638 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9639 "allowed");
9640 return error_mark_node;
9642 if (retval)
9644 tree semantic_type = NULL_TREE;
9645 npc = null_pointer_constant_p (retval);
9646 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9648 semantic_type = TREE_TYPE (retval);
9649 retval = TREE_OPERAND (retval, 0);
9651 retval = c_fully_fold (retval, false, NULL);
9652 if (semantic_type)
9653 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9656 if (!retval)
9658 current_function_returns_null = 1;
9659 if ((warn_return_type || flag_isoc99)
9660 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9662 bool warned_here;
9663 if (flag_isoc99)
9664 warned_here = pedwarn
9665 (loc, 0,
9666 "%<return%> with no value, in function returning non-void");
9667 else
9668 warned_here = warning_at
9669 (loc, OPT_Wreturn_type,
9670 "%<return%> with no value, in function returning non-void");
9671 no_warning = true;
9672 if (warned_here)
9673 inform (DECL_SOURCE_LOCATION (current_function_decl),
9674 "declared here");
9677 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9679 current_function_returns_null = 1;
9680 bool warned_here;
9681 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9682 warned_here = pedwarn
9683 (xloc, 0,
9684 "%<return%> with a value, in function returning void");
9685 else
9686 warned_here = pedwarn
9687 (xloc, OPT_Wpedantic, "ISO C forbids "
9688 "%<return%> with expression, in function returning void");
9689 if (warned_here)
9690 inform (DECL_SOURCE_LOCATION (current_function_decl),
9691 "declared here");
9693 else
9695 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9696 retval, origtype, ic_return,
9697 npc, NULL_TREE, NULL_TREE, 0);
9698 tree res = DECL_RESULT (current_function_decl);
9699 tree inner;
9700 bool save;
9702 current_function_returns_value = 1;
9703 if (t == error_mark_node)
9704 return NULL_TREE;
9706 save = in_late_binary_op;
9707 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9708 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9709 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9710 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9711 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9712 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9713 in_late_binary_op = true;
9714 inner = t = convert (TREE_TYPE (res), t);
9715 in_late_binary_op = save;
9717 /* Strip any conversions, additions, and subtractions, and see if
9718 we are returning the address of a local variable. Warn if so. */
9719 while (1)
9721 switch (TREE_CODE (inner))
9723 CASE_CONVERT:
9724 case NON_LVALUE_EXPR:
9725 case PLUS_EXPR:
9726 case POINTER_PLUS_EXPR:
9727 inner = TREE_OPERAND (inner, 0);
9728 continue;
9730 case MINUS_EXPR:
9731 /* If the second operand of the MINUS_EXPR has a pointer
9732 type (or is converted from it), this may be valid, so
9733 don't give a warning. */
9735 tree op1 = TREE_OPERAND (inner, 1);
9737 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9738 && (CONVERT_EXPR_P (op1)
9739 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9740 op1 = TREE_OPERAND (op1, 0);
9742 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9743 break;
9745 inner = TREE_OPERAND (inner, 0);
9746 continue;
9749 case ADDR_EXPR:
9750 inner = TREE_OPERAND (inner, 0);
9752 while (REFERENCE_CLASS_P (inner)
9753 && !INDIRECT_REF_P (inner))
9754 inner = TREE_OPERAND (inner, 0);
9756 if (DECL_P (inner)
9757 && !DECL_EXTERNAL (inner)
9758 && !TREE_STATIC (inner)
9759 && DECL_CONTEXT (inner) == current_function_decl)
9761 if (TREE_CODE (inner) == LABEL_DECL)
9762 warning_at (loc, OPT_Wreturn_local_addr,
9763 "function returns address of label");
9764 else
9766 warning_at (loc, OPT_Wreturn_local_addr,
9767 "function returns address of local variable");
9768 tree zero = build_zero_cst (TREE_TYPE (res));
9769 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9772 break;
9774 default:
9775 break;
9778 break;
9781 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9782 SET_EXPR_LOCATION (retval, loc);
9784 if (warn_sequence_point)
9785 verify_sequence_points (retval);
9788 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9789 TREE_NO_WARNING (ret_stmt) |= no_warning;
9790 return add_stmt (ret_stmt);
9793 struct c_switch {
9794 /* The SWITCH_EXPR being built. */
9795 tree switch_expr;
9797 /* The original type of the testing expression, i.e. before the
9798 default conversion is applied. */
9799 tree orig_type;
9801 /* A splay-tree mapping the low element of a case range to the high
9802 element, or NULL_TREE if there is no high element. Used to
9803 determine whether or not a new case label duplicates an old case
9804 label. We need a tree, rather than simply a hash table, because
9805 of the GNU case range extension. */
9806 splay_tree cases;
9808 /* The bindings at the point of the switch. This is used for
9809 warnings crossing decls when branching to a case label. */
9810 struct c_spot_bindings *bindings;
9812 /* The next node on the stack. */
9813 struct c_switch *next;
9815 /* Remember whether the controlling expression had boolean type
9816 before integer promotions for the sake of -Wswitch-bool. */
9817 bool bool_cond_p;
9819 /* Remember whether there was a case value that is outside the
9820 range of the ORIG_TYPE. */
9821 bool outside_range_p;
9824 /* A stack of the currently active switch statements. The innermost
9825 switch statement is on the top of the stack. There is no need to
9826 mark the stack for garbage collection because it is only active
9827 during the processing of the body of a function, and we never
9828 collect at that point. */
9830 struct c_switch *c_switch_stack;
9832 /* Start a C switch statement, testing expression EXP. Return the new
9833 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9834 SWITCH_COND_LOC is the location of the switch's condition.
9835 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
9837 tree
9838 c_start_case (location_t switch_loc,
9839 location_t switch_cond_loc,
9840 tree exp, bool explicit_cast_p)
9842 tree orig_type = error_mark_node;
9843 bool bool_cond_p = false;
9844 struct c_switch *cs;
9846 if (exp != error_mark_node)
9848 orig_type = TREE_TYPE (exp);
9850 if (!INTEGRAL_TYPE_P (orig_type))
9852 if (orig_type != error_mark_node)
9854 error_at (switch_cond_loc, "switch quantity not an integer");
9855 orig_type = error_mark_node;
9857 exp = integer_zero_node;
9859 else
9861 tree type = TYPE_MAIN_VARIANT (orig_type);
9862 tree e = exp;
9864 /* Warn if the condition has boolean value. */
9865 while (TREE_CODE (e) == COMPOUND_EXPR)
9866 e = TREE_OPERAND (e, 1);
9868 if ((TREE_CODE (type) == BOOLEAN_TYPE
9869 || truth_value_p (TREE_CODE (e)))
9870 /* Explicit cast to int suppresses this warning. */
9871 && !(TREE_CODE (type) == INTEGER_TYPE
9872 && explicit_cast_p))
9873 bool_cond_p = true;
9875 if (!in_system_header_at (input_location)
9876 && (type == long_integer_type_node
9877 || type == long_unsigned_type_node))
9878 warning_at (switch_cond_loc,
9879 OPT_Wtraditional, "%<long%> switch expression not "
9880 "converted to %<int%> in ISO C");
9882 exp = c_fully_fold (exp, false, NULL);
9883 exp = default_conversion (exp);
9885 if (warn_sequence_point)
9886 verify_sequence_points (exp);
9890 /* Add this new SWITCH_EXPR to the stack. */
9891 cs = XNEW (struct c_switch);
9892 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9893 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9894 cs->orig_type = orig_type;
9895 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9896 cs->bindings = c_get_switch_bindings ();
9897 cs->bool_cond_p = bool_cond_p;
9898 cs->outside_range_p = false;
9899 cs->next = c_switch_stack;
9900 c_switch_stack = cs;
9902 return add_stmt (cs->switch_expr);
9905 /* Process a case label at location LOC. */
9907 tree
9908 do_case (location_t loc, tree low_value, tree high_value)
9910 tree label = NULL_TREE;
9912 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9914 low_value = c_fully_fold (low_value, false, NULL);
9915 if (TREE_CODE (low_value) == INTEGER_CST)
9916 pedwarn (loc, OPT_Wpedantic,
9917 "case label is not an integer constant expression");
9920 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9922 high_value = c_fully_fold (high_value, false, NULL);
9923 if (TREE_CODE (high_value) == INTEGER_CST)
9924 pedwarn (input_location, OPT_Wpedantic,
9925 "case label is not an integer constant expression");
9928 if (c_switch_stack == NULL)
9930 if (low_value)
9931 error_at (loc, "case label not within a switch statement");
9932 else
9933 error_at (loc, "%<default%> label not within a switch statement");
9934 return NULL_TREE;
9937 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9938 EXPR_LOCATION (c_switch_stack->switch_expr),
9939 loc))
9940 return NULL_TREE;
9942 label = c_add_case_label (loc, c_switch_stack->cases,
9943 SWITCH_COND (c_switch_stack->switch_expr),
9944 c_switch_stack->orig_type,
9945 low_value, high_value,
9946 &c_switch_stack->outside_range_p);
9947 if (label == error_mark_node)
9948 label = NULL_TREE;
9949 return label;
9952 /* Finish the switch statement. TYPE is the original type of the
9953 controlling expression of the switch, or NULL_TREE. */
9955 void
9956 c_finish_case (tree body, tree type)
9958 struct c_switch *cs = c_switch_stack;
9959 location_t switch_location;
9961 SWITCH_BODY (cs->switch_expr) = body;
9963 /* Emit warnings as needed. */
9964 switch_location = EXPR_LOCATION (cs->switch_expr);
9965 c_do_switch_warnings (cs->cases, switch_location,
9966 type ? type : TREE_TYPE (cs->switch_expr),
9967 SWITCH_COND (cs->switch_expr),
9968 cs->bool_cond_p, cs->outside_range_p);
9970 /* Pop the stack. */
9971 c_switch_stack = cs->next;
9972 splay_tree_delete (cs->cases);
9973 c_release_switch_bindings (cs->bindings);
9974 XDELETE (cs);
9977 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9978 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9979 may be null. */
9981 void
9982 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9983 tree else_block)
9985 tree stmt;
9987 /* If the condition has array notations, then the rank of the then_block and
9988 else_block must be either 0 or be equal to the rank of the condition. If
9989 the condition does not have array notations then break them up as it is
9990 broken up in a normal expression. */
9991 if (flag_cilkplus && contains_array_notation_expr (cond))
9993 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9994 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9995 return;
9996 if (then_block
9997 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9998 return;
9999 if (else_block
10000 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10001 return;
10002 if (cond_rank != then_rank && then_rank != 0)
10004 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10005 " and the then-block");
10006 return;
10008 else if (cond_rank != else_rank && else_rank != 0)
10010 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10011 " and the else-block");
10012 return;
10016 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10017 SET_EXPR_LOCATION (stmt, if_locus);
10018 add_stmt (stmt);
10021 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10022 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10023 is false for DO loops. INCR is the FOR increment expression. BODY is
10024 the statement controlled by the loop. BLAB is the break label. CLAB is
10025 the continue label. Everything is allowed to be NULL. */
10027 void
10028 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10029 tree blab, tree clab, bool cond_is_first)
10031 tree entry = NULL, exit = NULL, t;
10033 /* In theory could forbid cilk spawn for loop increment expression,
10034 but it should work just fine. */
10036 /* If the condition is zero don't generate a loop construct. */
10037 if (cond && integer_zerop (cond))
10039 if (cond_is_first)
10041 t = build_and_jump (&blab);
10042 SET_EXPR_LOCATION (t, start_locus);
10043 add_stmt (t);
10046 else
10048 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10050 /* If we have an exit condition, then we build an IF with gotos either
10051 out of the loop, or to the top of it. If there's no exit condition,
10052 then we just build a jump back to the top. */
10053 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10055 if (cond && !integer_nonzerop (cond))
10057 /* Canonicalize the loop condition to the end. This means
10058 generating a branch to the loop condition. Reuse the
10059 continue label, if possible. */
10060 if (cond_is_first)
10062 if (incr || !clab)
10064 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10065 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10067 else
10068 t = build1 (GOTO_EXPR, void_type_node, clab);
10069 SET_EXPR_LOCATION (t, start_locus);
10070 add_stmt (t);
10073 t = build_and_jump (&blab);
10074 if (cond_is_first)
10075 exit = fold_build3_loc (start_locus,
10076 COND_EXPR, void_type_node, cond, exit, t);
10077 else
10078 exit = fold_build3_loc (input_location,
10079 COND_EXPR, void_type_node, cond, exit, t);
10081 else
10083 /* For the backward-goto's location of an unconditional loop
10084 use the beginning of the body, or, if there is none, the
10085 top of the loop. */
10086 location_t loc = EXPR_LOCATION (expr_first (body));
10087 if (loc == UNKNOWN_LOCATION)
10088 loc = start_locus;
10089 SET_EXPR_LOCATION (exit, loc);
10092 add_stmt (top);
10095 if (body)
10096 add_stmt (body);
10097 if (clab)
10098 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10099 if (incr)
10100 add_stmt (incr);
10101 if (entry)
10102 add_stmt (entry);
10103 if (exit)
10104 add_stmt (exit);
10105 if (blab)
10106 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10109 tree
10110 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10112 bool skip;
10113 tree label = *label_p;
10115 /* In switch statements break is sometimes stylistically used after
10116 a return statement. This can lead to spurious warnings about
10117 control reaching the end of a non-void function when it is
10118 inlined. Note that we are calling block_may_fallthru with
10119 language specific tree nodes; this works because
10120 block_may_fallthru returns true when given something it does not
10121 understand. */
10122 skip = !block_may_fallthru (cur_stmt_list);
10124 if (!label)
10126 if (!skip)
10127 *label_p = label = create_artificial_label (loc);
10129 else if (TREE_CODE (label) == LABEL_DECL)
10131 else switch (TREE_INT_CST_LOW (label))
10133 case 0:
10134 if (is_break)
10135 error_at (loc, "break statement not within loop or switch");
10136 else
10137 error_at (loc, "continue statement not within a loop");
10138 return NULL_TREE;
10140 case 1:
10141 gcc_assert (is_break);
10142 error_at (loc, "break statement used with OpenMP for loop");
10143 return NULL_TREE;
10145 case 2:
10146 if (is_break)
10147 error ("break statement within %<#pragma simd%> loop body");
10148 else
10149 error ("continue statement within %<#pragma simd%> loop body");
10150 return NULL_TREE;
10152 default:
10153 gcc_unreachable ();
10156 if (skip)
10157 return NULL_TREE;
10159 if (!is_break)
10160 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10162 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10165 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10167 static void
10168 emit_side_effect_warnings (location_t loc, tree expr)
10170 if (expr == error_mark_node)
10172 else if (!TREE_SIDE_EFFECTS (expr))
10174 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10175 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10177 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10179 tree r = expr;
10180 location_t cloc = loc;
10181 while (TREE_CODE (r) == COMPOUND_EXPR)
10183 if (EXPR_HAS_LOCATION (r))
10184 cloc = EXPR_LOCATION (r);
10185 r = TREE_OPERAND (r, 1);
10187 if (!TREE_SIDE_EFFECTS (r)
10188 && !VOID_TYPE_P (TREE_TYPE (r))
10189 && !CONVERT_EXPR_P (r)
10190 && !TREE_NO_WARNING (r)
10191 && !TREE_NO_WARNING (expr))
10192 warning_at (cloc, OPT_Wunused_value,
10193 "right-hand operand of comma expression has no effect");
10195 else
10196 warn_if_unused_value (expr, loc);
10199 /* Process an expression as if it were a complete statement. Emit
10200 diagnostics, but do not call ADD_STMT. LOC is the location of the
10201 statement. */
10203 tree
10204 c_process_expr_stmt (location_t loc, tree expr)
10206 tree exprv;
10208 if (!expr)
10209 return NULL_TREE;
10211 expr = c_fully_fold (expr, false, NULL);
10213 if (warn_sequence_point)
10214 verify_sequence_points (expr);
10216 if (TREE_TYPE (expr) != error_mark_node
10217 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10218 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10219 error_at (loc, "expression statement has incomplete type");
10221 /* If we're not processing a statement expression, warn about unused values.
10222 Warnings for statement expressions will be emitted later, once we figure
10223 out which is the result. */
10224 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10225 && warn_unused_value)
10226 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10228 exprv = expr;
10229 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10230 exprv = TREE_OPERAND (exprv, 1);
10231 while (CONVERT_EXPR_P (exprv))
10232 exprv = TREE_OPERAND (exprv, 0);
10233 if (DECL_P (exprv)
10234 || handled_component_p (exprv)
10235 || TREE_CODE (exprv) == ADDR_EXPR)
10236 mark_exp_read (exprv);
10238 /* If the expression is not of a type to which we cannot assign a line
10239 number, wrap the thing in a no-op NOP_EXPR. */
10240 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10242 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10243 SET_EXPR_LOCATION (expr, loc);
10246 return expr;
10249 /* Emit an expression as a statement. LOC is the location of the
10250 expression. */
10252 tree
10253 c_finish_expr_stmt (location_t loc, tree expr)
10255 if (expr)
10256 return add_stmt (c_process_expr_stmt (loc, expr));
10257 else
10258 return NULL;
10261 /* Do the opposite and emit a statement as an expression. To begin,
10262 create a new binding level and return it. */
10264 tree
10265 c_begin_stmt_expr (void)
10267 tree ret;
10269 /* We must force a BLOCK for this level so that, if it is not expanded
10270 later, there is a way to turn off the entire subtree of blocks that
10271 are contained in it. */
10272 keep_next_level ();
10273 ret = c_begin_compound_stmt (true);
10275 c_bindings_start_stmt_expr (c_switch_stack == NULL
10276 ? NULL
10277 : c_switch_stack->bindings);
10279 /* Mark the current statement list as belonging to a statement list. */
10280 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10282 return ret;
10285 /* LOC is the location of the compound statement to which this body
10286 belongs. */
10288 tree
10289 c_finish_stmt_expr (location_t loc, tree body)
10291 tree last, type, tmp, val;
10292 tree *last_p;
10294 body = c_end_compound_stmt (loc, body, true);
10296 c_bindings_end_stmt_expr (c_switch_stack == NULL
10297 ? NULL
10298 : c_switch_stack->bindings);
10300 /* Locate the last statement in BODY. See c_end_compound_stmt
10301 about always returning a BIND_EXPR. */
10302 last_p = &BIND_EXPR_BODY (body);
10303 last = BIND_EXPR_BODY (body);
10305 continue_searching:
10306 if (TREE_CODE (last) == STATEMENT_LIST)
10308 tree_stmt_iterator i;
10310 /* This can happen with degenerate cases like ({ }). No value. */
10311 if (!TREE_SIDE_EFFECTS (last))
10312 return body;
10314 /* If we're supposed to generate side effects warnings, process
10315 all of the statements except the last. */
10316 if (warn_unused_value)
10318 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10320 location_t tloc;
10321 tree t = tsi_stmt (i);
10323 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10324 emit_side_effect_warnings (tloc, t);
10327 else
10328 i = tsi_last (last);
10329 last_p = tsi_stmt_ptr (i);
10330 last = *last_p;
10333 /* If the end of the list is exception related, then the list was split
10334 by a call to push_cleanup. Continue searching. */
10335 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10336 || TREE_CODE (last) == TRY_CATCH_EXPR)
10338 last_p = &TREE_OPERAND (last, 0);
10339 last = *last_p;
10340 goto continue_searching;
10343 if (last == error_mark_node)
10344 return last;
10346 /* In the case that the BIND_EXPR is not necessary, return the
10347 expression out from inside it. */
10348 if (last == BIND_EXPR_BODY (body)
10349 && BIND_EXPR_VARS (body) == NULL)
10351 /* Even if this looks constant, do not allow it in a constant
10352 expression. */
10353 last = c_wrap_maybe_const (last, true);
10354 /* Do not warn if the return value of a statement expression is
10355 unused. */
10356 TREE_NO_WARNING (last) = 1;
10357 return last;
10360 /* Extract the type of said expression. */
10361 type = TREE_TYPE (last);
10363 /* If we're not returning a value at all, then the BIND_EXPR that
10364 we already have is a fine expression to return. */
10365 if (!type || VOID_TYPE_P (type))
10366 return body;
10368 /* Now that we've located the expression containing the value, it seems
10369 silly to make voidify_wrapper_expr repeat the process. Create a
10370 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10371 tmp = create_tmp_var_raw (type);
10373 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10374 tree_expr_nonnegative_p giving up immediately. */
10375 val = last;
10376 if (TREE_CODE (val) == NOP_EXPR
10377 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10378 val = TREE_OPERAND (val, 0);
10380 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10381 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10384 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10385 SET_EXPR_LOCATION (t, loc);
10386 return t;
10390 /* Begin and end compound statements. This is as simple as pushing
10391 and popping new statement lists from the tree. */
10393 tree
10394 c_begin_compound_stmt (bool do_scope)
10396 tree stmt = push_stmt_list ();
10397 if (do_scope)
10398 push_scope ();
10399 return stmt;
10402 /* End a compound statement. STMT is the statement. LOC is the
10403 location of the compound statement-- this is usually the location
10404 of the opening brace. */
10406 tree
10407 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10409 tree block = NULL;
10411 if (do_scope)
10413 if (c_dialect_objc ())
10414 objc_clear_super_receiver ();
10415 block = pop_scope ();
10418 stmt = pop_stmt_list (stmt);
10419 stmt = c_build_bind_expr (loc, block, stmt);
10421 /* If this compound statement is nested immediately inside a statement
10422 expression, then force a BIND_EXPR to be created. Otherwise we'll
10423 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10424 STATEMENT_LISTs merge, and thus we can lose track of what statement
10425 was really last. */
10426 if (building_stmt_list_p ()
10427 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10428 && TREE_CODE (stmt) != BIND_EXPR)
10430 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10431 TREE_SIDE_EFFECTS (stmt) = 1;
10432 SET_EXPR_LOCATION (stmt, loc);
10435 return stmt;
10438 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10439 when the current scope is exited. EH_ONLY is true when this is not
10440 meant to apply to normal control flow transfer. */
10442 void
10443 push_cleanup (tree decl, tree cleanup, bool eh_only)
10445 enum tree_code code;
10446 tree stmt, list;
10447 bool stmt_expr;
10449 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10450 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10451 add_stmt (stmt);
10452 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10453 list = push_stmt_list ();
10454 TREE_OPERAND (stmt, 0) = list;
10455 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10458 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10459 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10461 static tree
10462 build_vec_cmp (tree_code code, tree type,
10463 tree arg0, tree arg1)
10465 tree zero_vec = build_zero_cst (type);
10466 tree minus_one_vec = build_minus_one_cst (type);
10467 tree cmp_type = build_same_sized_truth_vector_type (type);
10468 tree cmp = build2 (code, cmp_type, arg0, arg1);
10469 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10472 /* Build a binary-operation expression without default conversions.
10473 CODE is the kind of expression to build.
10474 LOCATION is the operator's location.
10475 This function differs from `build' in several ways:
10476 the data type of the result is computed and recorded in it,
10477 warnings are generated if arg data types are invalid,
10478 special handling for addition and subtraction of pointers is known,
10479 and some optimization is done (operations on narrow ints
10480 are done in the narrower type when that gives the same result).
10481 Constant folding is also done before the result is returned.
10483 Note that the operands will never have enumeral types, or function
10484 or array types, because either they will have the default conversions
10485 performed or they have both just been converted to some other type in which
10486 the arithmetic is to be done. */
10488 tree
10489 build_binary_op (location_t location, enum tree_code code,
10490 tree orig_op0, tree orig_op1, int convert_p)
10492 tree type0, type1, orig_type0, orig_type1;
10493 tree eptype;
10494 enum tree_code code0, code1;
10495 tree op0, op1;
10496 tree ret = error_mark_node;
10497 const char *invalid_op_diag;
10498 bool op0_int_operands, op1_int_operands;
10499 bool int_const, int_const_or_overflow, int_operands;
10501 /* Expression code to give to the expression when it is built.
10502 Normally this is CODE, which is what the caller asked for,
10503 but in some special cases we change it. */
10504 enum tree_code resultcode = code;
10506 /* Data type in which the computation is to be performed.
10507 In the simplest cases this is the common type of the arguments. */
10508 tree result_type = NULL;
10510 /* When the computation is in excess precision, the type of the
10511 final EXCESS_PRECISION_EXPR. */
10512 tree semantic_result_type = NULL;
10514 /* Nonzero means operands have already been type-converted
10515 in whatever way is necessary.
10516 Zero means they need to be converted to RESULT_TYPE. */
10517 int converted = 0;
10519 /* Nonzero means create the expression with this type, rather than
10520 RESULT_TYPE. */
10521 tree build_type = 0;
10523 /* Nonzero means after finally constructing the expression
10524 convert it to this type. */
10525 tree final_type = 0;
10527 /* Nonzero if this is an operation like MIN or MAX which can
10528 safely be computed in short if both args are promoted shorts.
10529 Also implies COMMON.
10530 -1 indicates a bitwise operation; this makes a difference
10531 in the exact conditions for when it is safe to do the operation
10532 in a narrower mode. */
10533 int shorten = 0;
10535 /* Nonzero if this is a comparison operation;
10536 if both args are promoted shorts, compare the original shorts.
10537 Also implies COMMON. */
10538 int short_compare = 0;
10540 /* Nonzero if this is a right-shift operation, which can be computed on the
10541 original short and then promoted if the operand is a promoted short. */
10542 int short_shift = 0;
10544 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10545 int common = 0;
10547 /* True means types are compatible as far as ObjC is concerned. */
10548 bool objc_ok;
10550 /* True means this is an arithmetic operation that may need excess
10551 precision. */
10552 bool may_need_excess_precision;
10554 /* True means this is a boolean operation that converts both its
10555 operands to truth-values. */
10556 bool boolean_op = false;
10558 /* Remember whether we're doing / or %. */
10559 bool doing_div_or_mod = false;
10561 /* Remember whether we're doing << or >>. */
10562 bool doing_shift = false;
10564 /* Tree holding instrumentation expression. */
10565 tree instrument_expr = NULL;
10567 if (location == UNKNOWN_LOCATION)
10568 location = input_location;
10570 op0 = orig_op0;
10571 op1 = orig_op1;
10573 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10574 if (op0_int_operands)
10575 op0 = remove_c_maybe_const_expr (op0);
10576 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10577 if (op1_int_operands)
10578 op1 = remove_c_maybe_const_expr (op1);
10579 int_operands = (op0_int_operands && op1_int_operands);
10580 if (int_operands)
10582 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10583 && TREE_CODE (orig_op1) == INTEGER_CST);
10584 int_const = (int_const_or_overflow
10585 && !TREE_OVERFLOW (orig_op0)
10586 && !TREE_OVERFLOW (orig_op1));
10588 else
10589 int_const = int_const_or_overflow = false;
10591 /* Do not apply default conversion in mixed vector/scalar expression. */
10592 if (convert_p
10593 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10595 op0 = default_conversion (op0);
10596 op1 = default_conversion (op1);
10599 /* When Cilk Plus is enabled and there are array notations inside op0, then
10600 we check to see if there are builtin array notation functions. If
10601 so, then we take on the type of the array notation inside it. */
10602 if (flag_cilkplus && contains_array_notation_expr (op0))
10603 orig_type0 = type0 = find_correct_array_notation_type (op0);
10604 else
10605 orig_type0 = type0 = TREE_TYPE (op0);
10607 if (flag_cilkplus && contains_array_notation_expr (op1))
10608 orig_type1 = type1 = find_correct_array_notation_type (op1);
10609 else
10610 orig_type1 = type1 = TREE_TYPE (op1);
10612 /* The expression codes of the data types of the arguments tell us
10613 whether the arguments are integers, floating, pointers, etc. */
10614 code0 = TREE_CODE (type0);
10615 code1 = TREE_CODE (type1);
10617 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10618 STRIP_TYPE_NOPS (op0);
10619 STRIP_TYPE_NOPS (op1);
10621 /* If an error was already reported for one of the arguments,
10622 avoid reporting another error. */
10624 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10625 return error_mark_node;
10627 if (code0 == POINTER_TYPE
10628 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10629 return error_mark_node;
10631 if (code1 == POINTER_TYPE
10632 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10633 return error_mark_node;
10635 if ((invalid_op_diag
10636 = targetm.invalid_binary_op (code, type0, type1)))
10638 error_at (location, invalid_op_diag);
10639 return error_mark_node;
10642 switch (code)
10644 case PLUS_EXPR:
10645 case MINUS_EXPR:
10646 case MULT_EXPR:
10647 case TRUNC_DIV_EXPR:
10648 case CEIL_DIV_EXPR:
10649 case FLOOR_DIV_EXPR:
10650 case ROUND_DIV_EXPR:
10651 case EXACT_DIV_EXPR:
10652 may_need_excess_precision = true;
10653 break;
10654 default:
10655 may_need_excess_precision = false;
10656 break;
10658 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10660 op0 = TREE_OPERAND (op0, 0);
10661 type0 = TREE_TYPE (op0);
10663 else if (may_need_excess_precision
10664 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10666 type0 = eptype;
10667 op0 = convert (eptype, op0);
10669 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10671 op1 = TREE_OPERAND (op1, 0);
10672 type1 = TREE_TYPE (op1);
10674 else if (may_need_excess_precision
10675 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10677 type1 = eptype;
10678 op1 = convert (eptype, op1);
10681 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10683 /* In case when one of the operands of the binary operation is
10684 a vector and another is a scalar -- convert scalar to vector. */
10685 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10687 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10688 true);
10690 switch (convert_flag)
10692 case stv_error:
10693 return error_mark_node;
10694 case stv_firstarg:
10696 bool maybe_const = true;
10697 tree sc;
10698 sc = c_fully_fold (op0, false, &maybe_const);
10699 sc = save_expr (sc);
10700 sc = convert (TREE_TYPE (type1), sc);
10701 op0 = build_vector_from_val (type1, sc);
10702 if (!maybe_const)
10703 op0 = c_wrap_maybe_const (op0, true);
10704 orig_type0 = type0 = TREE_TYPE (op0);
10705 code0 = TREE_CODE (type0);
10706 converted = 1;
10707 break;
10709 case stv_secondarg:
10711 bool maybe_const = true;
10712 tree sc;
10713 sc = c_fully_fold (op1, false, &maybe_const);
10714 sc = save_expr (sc);
10715 sc = convert (TREE_TYPE (type0), sc);
10716 op1 = build_vector_from_val (type0, sc);
10717 if (!maybe_const)
10718 op1 = c_wrap_maybe_const (op1, true);
10719 orig_type1 = type1 = TREE_TYPE (op1);
10720 code1 = TREE_CODE (type1);
10721 converted = 1;
10722 break;
10724 default:
10725 break;
10729 switch (code)
10731 case PLUS_EXPR:
10732 /* Handle the pointer + int case. */
10733 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10735 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10736 goto return_build_binary_op;
10738 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10740 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10741 goto return_build_binary_op;
10743 else
10744 common = 1;
10745 break;
10747 case MINUS_EXPR:
10748 /* Subtraction of two similar pointers.
10749 We must subtract them as integers, then divide by object size. */
10750 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10751 && comp_target_types (location, type0, type1))
10753 ret = pointer_diff (location, op0, op1);
10754 goto return_build_binary_op;
10756 /* Handle pointer minus int. Just like pointer plus int. */
10757 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10759 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10760 goto return_build_binary_op;
10762 else
10763 common = 1;
10764 break;
10766 case MULT_EXPR:
10767 common = 1;
10768 break;
10770 case TRUNC_DIV_EXPR:
10771 case CEIL_DIV_EXPR:
10772 case FLOOR_DIV_EXPR:
10773 case ROUND_DIV_EXPR:
10774 case EXACT_DIV_EXPR:
10775 doing_div_or_mod = true;
10776 warn_for_div_by_zero (location, op1);
10778 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10779 || code0 == FIXED_POINT_TYPE
10780 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10781 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10782 || code1 == FIXED_POINT_TYPE
10783 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10785 enum tree_code tcode0 = code0, tcode1 = code1;
10787 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10788 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10789 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10790 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10792 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10793 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10794 resultcode = RDIV_EXPR;
10795 else
10796 /* Although it would be tempting to shorten always here, that
10797 loses on some targets, since the modulo instruction is
10798 undefined if the quotient can't be represented in the
10799 computation mode. We shorten only if unsigned or if
10800 dividing by something we know != -1. */
10801 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10802 || (TREE_CODE (op1) == INTEGER_CST
10803 && !integer_all_onesp (op1)));
10804 common = 1;
10806 break;
10808 case BIT_AND_EXPR:
10809 case BIT_IOR_EXPR:
10810 case BIT_XOR_EXPR:
10811 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10812 shorten = -1;
10813 /* Allow vector types which are not floating point types. */
10814 else if (code0 == VECTOR_TYPE
10815 && code1 == VECTOR_TYPE
10816 && !VECTOR_FLOAT_TYPE_P (type0)
10817 && !VECTOR_FLOAT_TYPE_P (type1))
10818 common = 1;
10819 break;
10821 case TRUNC_MOD_EXPR:
10822 case FLOOR_MOD_EXPR:
10823 doing_div_or_mod = true;
10824 warn_for_div_by_zero (location, op1);
10826 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10827 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10828 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10829 common = 1;
10830 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10832 /* Although it would be tempting to shorten always here, that loses
10833 on some targets, since the modulo instruction is undefined if the
10834 quotient can't be represented in the computation mode. We shorten
10835 only if unsigned or if dividing by something we know != -1. */
10836 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10837 || (TREE_CODE (op1) == INTEGER_CST
10838 && !integer_all_onesp (op1)));
10839 common = 1;
10841 break;
10843 case TRUTH_ANDIF_EXPR:
10844 case TRUTH_ORIF_EXPR:
10845 case TRUTH_AND_EXPR:
10846 case TRUTH_OR_EXPR:
10847 case TRUTH_XOR_EXPR:
10848 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10849 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10850 || code0 == FIXED_POINT_TYPE)
10851 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10852 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10853 || code1 == FIXED_POINT_TYPE))
10855 /* Result of these operations is always an int,
10856 but that does not mean the operands should be
10857 converted to ints! */
10858 result_type = integer_type_node;
10859 if (op0_int_operands)
10861 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10862 op0 = remove_c_maybe_const_expr (op0);
10864 else
10865 op0 = c_objc_common_truthvalue_conversion (location, op0);
10866 if (op1_int_operands)
10868 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10869 op1 = remove_c_maybe_const_expr (op1);
10871 else
10872 op1 = c_objc_common_truthvalue_conversion (location, op1);
10873 converted = 1;
10874 boolean_op = true;
10876 if (code == TRUTH_ANDIF_EXPR)
10878 int_const_or_overflow = (int_operands
10879 && TREE_CODE (orig_op0) == INTEGER_CST
10880 && (op0 == truthvalue_false_node
10881 || TREE_CODE (orig_op1) == INTEGER_CST));
10882 int_const = (int_const_or_overflow
10883 && !TREE_OVERFLOW (orig_op0)
10884 && (op0 == truthvalue_false_node
10885 || !TREE_OVERFLOW (orig_op1)));
10887 else if (code == TRUTH_ORIF_EXPR)
10889 int_const_or_overflow = (int_operands
10890 && TREE_CODE (orig_op0) == INTEGER_CST
10891 && (op0 == truthvalue_true_node
10892 || TREE_CODE (orig_op1) == INTEGER_CST));
10893 int_const = (int_const_or_overflow
10894 && !TREE_OVERFLOW (orig_op0)
10895 && (op0 == truthvalue_true_node
10896 || !TREE_OVERFLOW (orig_op1)));
10898 break;
10900 /* Shift operations: result has same type as first operand;
10901 always convert second operand to int.
10902 Also set SHORT_SHIFT if shifting rightward. */
10904 case RSHIFT_EXPR:
10905 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10906 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10908 result_type = type0;
10909 converted = 1;
10911 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10912 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10913 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10914 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10916 result_type = type0;
10917 converted = 1;
10919 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10920 && code1 == INTEGER_TYPE)
10922 doing_shift = true;
10923 if (TREE_CODE (op1) == INTEGER_CST)
10925 if (tree_int_cst_sgn (op1) < 0)
10927 int_const = false;
10928 if (c_inhibit_evaluation_warnings == 0)
10929 warning_at (location, OPT_Wshift_count_negative,
10930 "right shift count is negative");
10932 else
10934 if (!integer_zerop (op1))
10935 short_shift = 1;
10937 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10939 int_const = false;
10940 if (c_inhibit_evaluation_warnings == 0)
10941 warning_at (location, OPT_Wshift_count_overflow,
10942 "right shift count >= width of type");
10947 /* Use the type of the value to be shifted. */
10948 result_type = type0;
10949 /* Avoid converting op1 to result_type later. */
10950 converted = 1;
10952 break;
10954 case LSHIFT_EXPR:
10955 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10956 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10958 result_type = type0;
10959 converted = 1;
10961 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10962 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10963 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10964 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10966 result_type = type0;
10967 converted = 1;
10969 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10970 && code1 == INTEGER_TYPE)
10972 doing_shift = true;
10973 if (TREE_CODE (op0) == INTEGER_CST
10974 && tree_int_cst_sgn (op0) < 0)
10976 /* Don't reject a left shift of a negative value in a context
10977 where a constant expression is needed in C90. */
10978 if (flag_isoc99)
10979 int_const = false;
10980 if (c_inhibit_evaluation_warnings == 0)
10981 warning_at (location, OPT_Wshift_negative_value,
10982 "left shift of negative value");
10984 if (TREE_CODE (op1) == INTEGER_CST)
10986 if (tree_int_cst_sgn (op1) < 0)
10988 int_const = false;
10989 if (c_inhibit_evaluation_warnings == 0)
10990 warning_at (location, OPT_Wshift_count_negative,
10991 "left shift count is negative");
10993 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10995 int_const = false;
10996 if (c_inhibit_evaluation_warnings == 0)
10997 warning_at (location, OPT_Wshift_count_overflow,
10998 "left shift count >= width of type");
11000 else if (TREE_CODE (op0) == INTEGER_CST
11001 && maybe_warn_shift_overflow (location, op0, op1)
11002 && flag_isoc99)
11003 int_const = false;
11006 /* Use the type of the value to be shifted. */
11007 result_type = type0;
11008 /* Avoid converting op1 to result_type later. */
11009 converted = 1;
11011 break;
11013 case EQ_EXPR:
11014 case NE_EXPR:
11015 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11017 tree intt;
11018 if (!vector_types_compatible_elements_p (type0, type1))
11020 error_at (location, "comparing vectors with different "
11021 "element types");
11022 return error_mark_node;
11025 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11027 error_at (location, "comparing vectors with different "
11028 "number of elements");
11029 return error_mark_node;
11032 /* It's not precisely specified how the usual arithmetic
11033 conversions apply to the vector types. Here, we use
11034 the unsigned type if one of the operands is signed and
11035 the other one is unsigned. */
11036 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11038 if (!TYPE_UNSIGNED (type0))
11039 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11040 else
11041 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11042 warning_at (location, OPT_Wsign_compare, "comparison between "
11043 "types %qT and %qT", type0, type1);
11046 /* Always construct signed integer vector type. */
11047 intt = c_common_type_for_size (GET_MODE_BITSIZE
11048 (TYPE_MODE (TREE_TYPE (type0))), 0);
11049 result_type = build_opaque_vector_type (intt,
11050 TYPE_VECTOR_SUBPARTS (type0));
11051 converted = 1;
11052 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11053 goto return_build_binary_op;
11055 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11056 warning_at (location,
11057 OPT_Wfloat_equal,
11058 "comparing floating point with == or != is unsafe");
11059 /* Result of comparison is always int,
11060 but don't convert the args to int! */
11061 build_type = integer_type_node;
11062 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11063 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11064 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11065 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11066 short_compare = 1;
11067 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11069 if (TREE_CODE (op0) == ADDR_EXPR
11070 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
11072 if (code == EQ_EXPR)
11073 warning_at (location,
11074 OPT_Waddress,
11075 "the comparison will always evaluate as %<false%> "
11076 "for the address of %qD will never be NULL",
11077 TREE_OPERAND (op0, 0));
11078 else
11079 warning_at (location,
11080 OPT_Waddress,
11081 "the comparison will always evaluate as %<true%> "
11082 "for the address of %qD will never be NULL",
11083 TREE_OPERAND (op0, 0));
11085 result_type = type0;
11087 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11089 if (TREE_CODE (op1) == ADDR_EXPR
11090 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
11092 if (code == EQ_EXPR)
11093 warning_at (location,
11094 OPT_Waddress,
11095 "the comparison will always evaluate as %<false%> "
11096 "for the address of %qD will never be NULL",
11097 TREE_OPERAND (op1, 0));
11098 else
11099 warning_at (location,
11100 OPT_Waddress,
11101 "the comparison will always evaluate as %<true%> "
11102 "for the address of %qD will never be NULL",
11103 TREE_OPERAND (op1, 0));
11105 result_type = type1;
11107 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11109 tree tt0 = TREE_TYPE (type0);
11110 tree tt1 = TREE_TYPE (type1);
11111 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11112 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11113 addr_space_t as_common = ADDR_SPACE_GENERIC;
11115 /* Anything compares with void *. void * compares with anything.
11116 Otherwise, the targets must be compatible
11117 and both must be object or both incomplete. */
11118 if (comp_target_types (location, type0, type1))
11119 result_type = common_pointer_type (type0, type1);
11120 else if (!addr_space_superset (as0, as1, &as_common))
11122 error_at (location, "comparison of pointers to "
11123 "disjoint address spaces");
11124 return error_mark_node;
11126 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11128 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11129 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11130 "comparison of %<void *%> with function pointer");
11132 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11134 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11135 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11136 "comparison of %<void *%> with function pointer");
11138 else
11139 /* Avoid warning about the volatile ObjC EH puts on decls. */
11140 if (!objc_ok)
11141 pedwarn (location, 0,
11142 "comparison of distinct pointer types lacks a cast");
11144 if (result_type == NULL_TREE)
11146 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11147 result_type = build_pointer_type
11148 (build_qualified_type (void_type_node, qual));
11151 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11153 result_type = type0;
11154 pedwarn (location, 0, "comparison between pointer and integer");
11156 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11158 result_type = type1;
11159 pedwarn (location, 0, "comparison between pointer and integer");
11161 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11162 || truth_value_p (TREE_CODE (orig_op0)))
11163 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11164 || truth_value_p (TREE_CODE (orig_op1))))
11165 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11166 break;
11168 case LE_EXPR:
11169 case GE_EXPR:
11170 case LT_EXPR:
11171 case GT_EXPR:
11172 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11174 tree intt;
11175 if (!vector_types_compatible_elements_p (type0, type1))
11177 error_at (location, "comparing vectors with different "
11178 "element types");
11179 return error_mark_node;
11182 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11184 error_at (location, "comparing vectors with different "
11185 "number of elements");
11186 return error_mark_node;
11189 /* It's not precisely specified how the usual arithmetic
11190 conversions apply to the vector types. Here, we use
11191 the unsigned type if one of the operands is signed and
11192 the other one is unsigned. */
11193 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11195 if (!TYPE_UNSIGNED (type0))
11196 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11197 else
11198 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11199 warning_at (location, OPT_Wsign_compare, "comparison between "
11200 "types %qT and %qT", type0, type1);
11203 /* Always construct signed integer vector type. */
11204 intt = c_common_type_for_size (GET_MODE_BITSIZE
11205 (TYPE_MODE (TREE_TYPE (type0))), 0);
11206 result_type = build_opaque_vector_type (intt,
11207 TYPE_VECTOR_SUBPARTS (type0));
11208 converted = 1;
11209 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11210 goto return_build_binary_op;
11212 build_type = integer_type_node;
11213 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11214 || code0 == FIXED_POINT_TYPE)
11215 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11216 || code1 == FIXED_POINT_TYPE))
11217 short_compare = 1;
11218 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11220 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11221 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11222 addr_space_t as_common;
11224 if (comp_target_types (location, type0, type1))
11226 result_type = common_pointer_type (type0, type1);
11227 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11228 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11229 pedwarn (location, 0,
11230 "comparison of complete and incomplete pointers");
11231 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11232 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11233 "ordered comparisons of pointers to functions");
11234 else if (null_pointer_constant_p (orig_op0)
11235 || null_pointer_constant_p (orig_op1))
11236 warning_at (location, OPT_Wextra,
11237 "ordered comparison of pointer with null pointer");
11240 else if (!addr_space_superset (as0, as1, &as_common))
11242 error_at (location, "comparison of pointers to "
11243 "disjoint address spaces");
11244 return error_mark_node;
11246 else
11248 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11249 result_type = build_pointer_type
11250 (build_qualified_type (void_type_node, qual));
11251 pedwarn (location, 0,
11252 "comparison of distinct pointer types lacks a cast");
11255 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11257 result_type = type0;
11258 if (pedantic)
11259 pedwarn (location, OPT_Wpedantic,
11260 "ordered comparison of pointer with integer zero");
11261 else if (extra_warnings)
11262 warning_at (location, OPT_Wextra,
11263 "ordered comparison of pointer with integer zero");
11265 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11267 result_type = type1;
11268 if (pedantic)
11269 pedwarn (location, OPT_Wpedantic,
11270 "ordered comparison of pointer with integer zero");
11271 else if (extra_warnings)
11272 warning_at (location, OPT_Wextra,
11273 "ordered comparison of pointer with integer zero");
11275 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11277 result_type = type0;
11278 pedwarn (location, 0, "comparison between pointer and integer");
11280 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11282 result_type = type1;
11283 pedwarn (location, 0, "comparison between pointer and integer");
11285 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11286 || truth_value_p (TREE_CODE (orig_op0)))
11287 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11288 || truth_value_p (TREE_CODE (orig_op1))))
11289 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11290 break;
11292 default:
11293 gcc_unreachable ();
11296 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11297 return error_mark_node;
11299 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11300 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11301 || !vector_types_compatible_elements_p (type0, type1)))
11303 gcc_rich_location richloc (location);
11304 richloc.maybe_add_expr (orig_op0);
11305 richloc.maybe_add_expr (orig_op1);
11306 binary_op_error (&richloc, code, type0, type1);
11307 return error_mark_node;
11310 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11311 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11313 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11314 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11316 bool first_complex = (code0 == COMPLEX_TYPE);
11317 bool second_complex = (code1 == COMPLEX_TYPE);
11318 int none_complex = (!first_complex && !second_complex);
11320 if (shorten || common || short_compare)
11322 result_type = c_common_type (type0, type1);
11323 do_warn_double_promotion (result_type, type0, type1,
11324 "implicit conversion from %qT to %qT "
11325 "to match other operand of binary "
11326 "expression",
11327 location);
11328 if (result_type == error_mark_node)
11329 return error_mark_node;
11332 if (first_complex != second_complex
11333 && (code == PLUS_EXPR
11334 || code == MINUS_EXPR
11335 || code == MULT_EXPR
11336 || (code == TRUNC_DIV_EXPR && first_complex))
11337 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11338 && flag_signed_zeros)
11340 /* An operation on mixed real/complex operands must be
11341 handled specially, but the language-independent code can
11342 more easily optimize the plain complex arithmetic if
11343 -fno-signed-zeros. */
11344 tree real_type = TREE_TYPE (result_type);
11345 tree real, imag;
11346 if (type0 != orig_type0 || type1 != orig_type1)
11348 gcc_assert (may_need_excess_precision && common);
11349 semantic_result_type = c_common_type (orig_type0, orig_type1);
11351 if (first_complex)
11353 if (TREE_TYPE (op0) != result_type)
11354 op0 = convert_and_check (location, result_type, op0);
11355 if (TREE_TYPE (op1) != real_type)
11356 op1 = convert_and_check (location, real_type, op1);
11358 else
11360 if (TREE_TYPE (op0) != real_type)
11361 op0 = convert_and_check (location, real_type, op0);
11362 if (TREE_TYPE (op1) != result_type)
11363 op1 = convert_and_check (location, result_type, op1);
11365 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11366 return error_mark_node;
11367 if (first_complex)
11369 op0 = c_save_expr (op0);
11370 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11371 op0, 1);
11372 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11373 op0, 1);
11374 switch (code)
11376 case MULT_EXPR:
11377 case TRUNC_DIV_EXPR:
11378 op1 = c_save_expr (op1);
11379 imag = build2 (resultcode, real_type, imag, op1);
11380 /* Fall through. */
11381 case PLUS_EXPR:
11382 case MINUS_EXPR:
11383 real = build2 (resultcode, real_type, real, op1);
11384 break;
11385 default:
11386 gcc_unreachable();
11389 else
11391 op1 = c_save_expr (op1);
11392 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11393 op1, 1);
11394 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11395 op1, 1);
11396 switch (code)
11398 case MULT_EXPR:
11399 op0 = c_save_expr (op0);
11400 imag = build2 (resultcode, real_type, op0, imag);
11401 /* Fall through. */
11402 case PLUS_EXPR:
11403 real = build2 (resultcode, real_type, op0, real);
11404 break;
11405 case MINUS_EXPR:
11406 real = build2 (resultcode, real_type, op0, real);
11407 imag = build1 (NEGATE_EXPR, real_type, imag);
11408 break;
11409 default:
11410 gcc_unreachable();
11413 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11414 goto return_build_binary_op;
11417 /* For certain operations (which identify themselves by shorten != 0)
11418 if both args were extended from the same smaller type,
11419 do the arithmetic in that type and then extend.
11421 shorten !=0 and !=1 indicates a bitwise operation.
11422 For them, this optimization is safe only if
11423 both args are zero-extended or both are sign-extended.
11424 Otherwise, we might change the result.
11425 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11426 but calculated in (unsigned short) it would be (unsigned short)-1. */
11428 if (shorten && none_complex)
11430 final_type = result_type;
11431 result_type = shorten_binary_op (result_type, op0, op1,
11432 shorten == -1);
11435 /* Shifts can be shortened if shifting right. */
11437 if (short_shift)
11439 int unsigned_arg;
11440 tree arg0 = get_narrower (op0, &unsigned_arg);
11442 final_type = result_type;
11444 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11445 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11447 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11448 && tree_int_cst_sgn (op1) > 0
11449 /* We can shorten only if the shift count is less than the
11450 number of bits in the smaller type size. */
11451 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11452 /* We cannot drop an unsigned shift after sign-extension. */
11453 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11455 /* Do an unsigned shift if the operand was zero-extended. */
11456 result_type
11457 = c_common_signed_or_unsigned_type (unsigned_arg,
11458 TREE_TYPE (arg0));
11459 /* Convert value-to-be-shifted to that type. */
11460 if (TREE_TYPE (op0) != result_type)
11461 op0 = convert (result_type, op0);
11462 converted = 1;
11466 /* Comparison operations are shortened too but differently.
11467 They identify themselves by setting short_compare = 1. */
11469 if (short_compare)
11471 /* Don't write &op0, etc., because that would prevent op0
11472 from being kept in a register.
11473 Instead, make copies of the our local variables and
11474 pass the copies by reference, then copy them back afterward. */
11475 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11476 enum tree_code xresultcode = resultcode;
11477 tree val
11478 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11479 &xresultcode);
11481 if (val != 0)
11483 ret = val;
11484 goto return_build_binary_op;
11487 op0 = xop0, op1 = xop1;
11488 converted = 1;
11489 resultcode = xresultcode;
11491 if (c_inhibit_evaluation_warnings == 0)
11493 bool op0_maybe_const = true;
11494 bool op1_maybe_const = true;
11495 tree orig_op0_folded, orig_op1_folded;
11497 if (in_late_binary_op)
11499 orig_op0_folded = orig_op0;
11500 orig_op1_folded = orig_op1;
11502 else
11504 /* Fold for the sake of possible warnings, as in
11505 build_conditional_expr. This requires the
11506 "original" values to be folded, not just op0 and
11507 op1. */
11508 c_inhibit_evaluation_warnings++;
11509 op0 = c_fully_fold (op0, require_constant_value,
11510 &op0_maybe_const);
11511 op1 = c_fully_fold (op1, require_constant_value,
11512 &op1_maybe_const);
11513 c_inhibit_evaluation_warnings--;
11514 orig_op0_folded = c_fully_fold (orig_op0,
11515 require_constant_value,
11516 NULL);
11517 orig_op1_folded = c_fully_fold (orig_op1,
11518 require_constant_value,
11519 NULL);
11522 if (warn_sign_compare)
11523 warn_for_sign_compare (location, orig_op0_folded,
11524 orig_op1_folded, op0, op1,
11525 result_type, resultcode);
11526 if (!in_late_binary_op && !int_operands)
11528 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11529 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11530 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11531 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11537 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11538 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11539 Then the expression will be built.
11540 It will be given type FINAL_TYPE if that is nonzero;
11541 otherwise, it will be given type RESULT_TYPE. */
11543 if (!result_type)
11545 gcc_rich_location richloc (location);
11546 richloc.maybe_add_expr (orig_op0);
11547 richloc.maybe_add_expr (orig_op1);
11548 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
11549 return error_mark_node;
11552 if (build_type == NULL_TREE)
11554 build_type = result_type;
11555 if ((type0 != orig_type0 || type1 != orig_type1)
11556 && !boolean_op)
11558 gcc_assert (may_need_excess_precision && common);
11559 semantic_result_type = c_common_type (orig_type0, orig_type1);
11563 if (!converted)
11565 op0 = ep_convert_and_check (location, result_type, op0,
11566 semantic_result_type);
11567 op1 = ep_convert_and_check (location, result_type, op1,
11568 semantic_result_type);
11570 /* This can happen if one operand has a vector type, and the other
11571 has a different type. */
11572 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11573 return error_mark_node;
11576 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11577 | SANITIZE_FLOAT_DIVIDE))
11578 && do_ubsan_in_current_function ()
11579 && (doing_div_or_mod || doing_shift)
11580 && !require_constant_value)
11582 /* OP0 and/or OP1 might have side-effects. */
11583 op0 = c_save_expr (op0);
11584 op1 = c_save_expr (op1);
11585 op0 = c_fully_fold (op0, false, NULL);
11586 op1 = c_fully_fold (op1, false, NULL);
11587 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11588 | SANITIZE_FLOAT_DIVIDE)))
11589 instrument_expr = ubsan_instrument_division (location, op0, op1);
11590 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11591 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11594 /* Treat expressions in initializers specially as they can't trap. */
11595 if (int_const_or_overflow)
11596 ret = (require_constant_value
11597 ? fold_build2_initializer_loc (location, resultcode, build_type,
11598 op0, op1)
11599 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11600 else
11601 ret = build2 (resultcode, build_type, op0, op1);
11602 if (final_type != 0)
11603 ret = convert (final_type, ret);
11605 return_build_binary_op:
11606 gcc_assert (ret != error_mark_node);
11607 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11608 ret = (int_operands
11609 ? note_integer_operands (ret)
11610 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11611 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11612 && !in_late_binary_op)
11613 ret = note_integer_operands (ret);
11614 if (semantic_result_type)
11615 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11616 protected_set_expr_location (ret, location);
11618 if (instrument_expr != NULL)
11619 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11620 instrument_expr, ret);
11622 return ret;
11626 /* Convert EXPR to be a truth-value, validating its type for this
11627 purpose. LOCATION is the source location for the expression. */
11629 tree
11630 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11632 bool int_const, int_operands;
11634 switch (TREE_CODE (TREE_TYPE (expr)))
11636 case ARRAY_TYPE:
11637 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11638 return error_mark_node;
11640 case RECORD_TYPE:
11641 error_at (location, "used struct type value where scalar is required");
11642 return error_mark_node;
11644 case UNION_TYPE:
11645 error_at (location, "used union type value where scalar is required");
11646 return error_mark_node;
11648 case VOID_TYPE:
11649 error_at (location, "void value not ignored as it ought to be");
11650 return error_mark_node;
11652 case POINTER_TYPE:
11653 if (reject_gcc_builtin (expr))
11654 return error_mark_node;
11655 break;
11657 case FUNCTION_TYPE:
11658 gcc_unreachable ();
11660 case VECTOR_TYPE:
11661 error_at (location, "used vector type where scalar is required");
11662 return error_mark_node;
11664 default:
11665 break;
11668 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11669 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11670 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11672 expr = remove_c_maybe_const_expr (expr);
11673 expr = build2 (NE_EXPR, integer_type_node, expr,
11674 convert (TREE_TYPE (expr), integer_zero_node));
11675 expr = note_integer_operands (expr);
11677 else
11678 /* ??? Should we also give an error for vectors rather than leaving
11679 those to give errors later? */
11680 expr = c_common_truthvalue_conversion (location, expr);
11682 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11684 if (TREE_OVERFLOW (expr))
11685 return expr;
11686 else
11687 return note_integer_operands (expr);
11689 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11690 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11691 return expr;
11695 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11696 required. */
11698 tree
11699 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11701 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11703 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11704 /* Executing a compound literal inside a function reinitializes
11705 it. */
11706 if (!TREE_STATIC (decl))
11707 *se = true;
11708 return decl;
11710 else
11711 return expr;
11714 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11715 statement. LOC is the location of the construct. */
11717 tree
11718 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11719 tree clauses)
11721 body = c_end_compound_stmt (loc, body, true);
11723 tree stmt = make_node (code);
11724 TREE_TYPE (stmt) = void_type_node;
11725 OMP_BODY (stmt) = body;
11726 OMP_CLAUSES (stmt) = clauses;
11727 SET_EXPR_LOCATION (stmt, loc);
11729 return add_stmt (stmt);
11732 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11733 statement. LOC is the location of the OACC_DATA. */
11735 tree
11736 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11738 tree stmt;
11740 block = c_end_compound_stmt (loc, block, true);
11742 stmt = make_node (OACC_DATA);
11743 TREE_TYPE (stmt) = void_type_node;
11744 OACC_DATA_CLAUSES (stmt) = clauses;
11745 OACC_DATA_BODY (stmt) = block;
11746 SET_EXPR_LOCATION (stmt, loc);
11748 return add_stmt (stmt);
11751 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11752 statement. LOC is the location of the OACC_HOST_DATA. */
11754 tree
11755 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
11757 tree stmt;
11759 block = c_end_compound_stmt (loc, block, true);
11761 stmt = make_node (OACC_HOST_DATA);
11762 TREE_TYPE (stmt) = void_type_node;
11763 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
11764 OACC_HOST_DATA_BODY (stmt) = block;
11765 SET_EXPR_LOCATION (stmt, loc);
11767 return add_stmt (stmt);
11770 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11772 tree
11773 c_begin_omp_parallel (void)
11775 tree block;
11777 keep_next_level ();
11778 block = c_begin_compound_stmt (true);
11780 return block;
11783 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11784 statement. LOC is the location of the OMP_PARALLEL. */
11786 tree
11787 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11789 tree stmt;
11791 block = c_end_compound_stmt (loc, block, true);
11793 stmt = make_node (OMP_PARALLEL);
11794 TREE_TYPE (stmt) = void_type_node;
11795 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11796 OMP_PARALLEL_BODY (stmt) = block;
11797 SET_EXPR_LOCATION (stmt, loc);
11799 return add_stmt (stmt);
11802 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11804 tree
11805 c_begin_omp_task (void)
11807 tree block;
11809 keep_next_level ();
11810 block = c_begin_compound_stmt (true);
11812 return block;
11815 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11816 statement. LOC is the location of the #pragma. */
11818 tree
11819 c_finish_omp_task (location_t loc, tree clauses, tree block)
11821 tree stmt;
11823 block = c_end_compound_stmt (loc, block, true);
11825 stmt = make_node (OMP_TASK);
11826 TREE_TYPE (stmt) = void_type_node;
11827 OMP_TASK_CLAUSES (stmt) = clauses;
11828 OMP_TASK_BODY (stmt) = block;
11829 SET_EXPR_LOCATION (stmt, loc);
11831 return add_stmt (stmt);
11834 /* Generate GOMP_cancel call for #pragma omp cancel. */
11836 void
11837 c_finish_omp_cancel (location_t loc, tree clauses)
11839 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11840 int mask = 0;
11841 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11842 mask = 1;
11843 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11844 mask = 2;
11845 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11846 mask = 4;
11847 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11848 mask = 8;
11849 else
11851 error_at (loc, "%<#pragma omp cancel must specify one of "
11852 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11853 "clauses");
11854 return;
11856 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11857 if (ifc != NULL_TREE)
11859 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11860 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11861 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11862 build_zero_cst (type));
11864 else
11865 ifc = boolean_true_node;
11866 tree stmt = build_call_expr_loc (loc, fn, 2,
11867 build_int_cst (integer_type_node, mask),
11868 ifc);
11869 add_stmt (stmt);
11872 /* Generate GOMP_cancellation_point call for
11873 #pragma omp cancellation point. */
11875 void
11876 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11878 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11879 int mask = 0;
11880 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11881 mask = 1;
11882 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11883 mask = 2;
11884 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11885 mask = 4;
11886 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11887 mask = 8;
11888 else
11890 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11891 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11892 "clauses");
11893 return;
11895 tree stmt = build_call_expr_loc (loc, fn, 1,
11896 build_int_cst (integer_type_node, mask));
11897 add_stmt (stmt);
11900 /* Helper function for handle_omp_array_sections. Called recursively
11901 to handle multiple array-section-subscripts. C is the clause,
11902 T current expression (initially OMP_CLAUSE_DECL), which is either
11903 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11904 expression if specified, TREE_VALUE length expression if specified,
11905 TREE_CHAIN is what it has been specified after, or some decl.
11906 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11907 set to true if any of the array-section-subscript could have length
11908 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11909 first array-section-subscript which is known not to have length
11910 of one. Given say:
11911 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11912 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11913 all are or may have length of 1, array-section-subscript [:2] is the
11914 first one known not to have length 1. For array-section-subscript
11915 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11916 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11917 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11918 case though, as some lengths could be zero. */
11920 static tree
11921 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11922 bool &maybe_zero_len, unsigned int &first_non_one,
11923 bool is_omp)
11925 tree ret, low_bound, length, type;
11926 if (TREE_CODE (t) != TREE_LIST)
11928 if (error_operand_p (t))
11929 return error_mark_node;
11930 ret = t;
11931 if (TREE_CODE (t) == COMPONENT_REF
11932 && is_omp
11933 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
11934 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
11935 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
11937 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
11939 error_at (OMP_CLAUSE_LOCATION (c),
11940 "bit-field %qE in %qs clause",
11941 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11942 return error_mark_node;
11944 while (TREE_CODE (t) == COMPONENT_REF)
11946 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
11948 error_at (OMP_CLAUSE_LOCATION (c),
11949 "%qE is a member of a union", t);
11950 return error_mark_node;
11952 t = TREE_OPERAND (t, 0);
11955 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
11957 if (DECL_P (t))
11958 error_at (OMP_CLAUSE_LOCATION (c),
11959 "%qD is not a variable in %qs clause", t,
11960 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11961 else
11962 error_at (OMP_CLAUSE_LOCATION (c),
11963 "%qE is not a variable in %qs clause", t,
11964 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11965 return error_mark_node;
11967 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11968 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
11970 error_at (OMP_CLAUSE_LOCATION (c),
11971 "%qD is threadprivate variable in %qs clause", t,
11972 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11973 return error_mark_node;
11975 return ret;
11978 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11979 maybe_zero_len, first_non_one, is_omp);
11980 if (ret == error_mark_node || ret == NULL_TREE)
11981 return ret;
11983 type = TREE_TYPE (ret);
11984 low_bound = TREE_PURPOSE (t);
11985 length = TREE_VALUE (t);
11987 if (low_bound == error_mark_node || length == error_mark_node)
11988 return error_mark_node;
11990 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11992 error_at (OMP_CLAUSE_LOCATION (c),
11993 "low bound %qE of array section does not have integral type",
11994 low_bound);
11995 return error_mark_node;
11997 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11999 error_at (OMP_CLAUSE_LOCATION (c),
12000 "length %qE of array section does not have integral type",
12001 length);
12002 return error_mark_node;
12004 if (low_bound
12005 && TREE_CODE (low_bound) == INTEGER_CST
12006 && TYPE_PRECISION (TREE_TYPE (low_bound))
12007 > TYPE_PRECISION (sizetype))
12008 low_bound = fold_convert (sizetype, low_bound);
12009 if (length
12010 && TREE_CODE (length) == INTEGER_CST
12011 && TYPE_PRECISION (TREE_TYPE (length))
12012 > TYPE_PRECISION (sizetype))
12013 length = fold_convert (sizetype, length);
12014 if (low_bound == NULL_TREE)
12015 low_bound = integer_zero_node;
12017 if (length != NULL_TREE)
12019 if (!integer_nonzerop (length))
12021 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12022 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12024 if (integer_zerop (length))
12026 error_at (OMP_CLAUSE_LOCATION (c),
12027 "zero length array section in %qs clause",
12028 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12029 return error_mark_node;
12032 else
12033 maybe_zero_len = true;
12035 if (first_non_one == types.length ()
12036 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12037 first_non_one++;
12039 if (TREE_CODE (type) == ARRAY_TYPE)
12041 if (length == NULL_TREE
12042 && (TYPE_DOMAIN (type) == NULL_TREE
12043 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12045 error_at (OMP_CLAUSE_LOCATION (c),
12046 "for unknown bound array type length expression must "
12047 "be specified");
12048 return error_mark_node;
12050 if (TREE_CODE (low_bound) == INTEGER_CST
12051 && tree_int_cst_sgn (low_bound) == -1)
12053 error_at (OMP_CLAUSE_LOCATION (c),
12054 "negative low bound in array section in %qs clause",
12055 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12056 return error_mark_node;
12058 if (length != NULL_TREE
12059 && TREE_CODE (length) == INTEGER_CST
12060 && tree_int_cst_sgn (length) == -1)
12062 error_at (OMP_CLAUSE_LOCATION (c),
12063 "negative length in array section in %qs clause",
12064 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12065 return error_mark_node;
12067 if (TYPE_DOMAIN (type)
12068 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12069 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12070 == INTEGER_CST)
12072 tree size = size_binop (PLUS_EXPR,
12073 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12074 size_one_node);
12075 if (TREE_CODE (low_bound) == INTEGER_CST)
12077 if (tree_int_cst_lt (size, low_bound))
12079 error_at (OMP_CLAUSE_LOCATION (c),
12080 "low bound %qE above array section size "
12081 "in %qs clause", low_bound,
12082 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12083 return error_mark_node;
12085 if (tree_int_cst_equal (size, low_bound))
12087 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12088 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12090 error_at (OMP_CLAUSE_LOCATION (c),
12091 "zero length array section in %qs clause",
12092 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12093 return error_mark_node;
12095 maybe_zero_len = true;
12097 else if (length == NULL_TREE
12098 && first_non_one == types.length ()
12099 && tree_int_cst_equal
12100 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12101 low_bound))
12102 first_non_one++;
12104 else if (length == NULL_TREE)
12106 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12107 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12108 maybe_zero_len = true;
12109 if (first_non_one == types.length ())
12110 first_non_one++;
12112 if (length && TREE_CODE (length) == INTEGER_CST)
12114 if (tree_int_cst_lt (size, length))
12116 error_at (OMP_CLAUSE_LOCATION (c),
12117 "length %qE above array section size "
12118 "in %qs clause", length,
12119 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12120 return error_mark_node;
12122 if (TREE_CODE (low_bound) == INTEGER_CST)
12124 tree lbpluslen
12125 = size_binop (PLUS_EXPR,
12126 fold_convert (sizetype, low_bound),
12127 fold_convert (sizetype, length));
12128 if (TREE_CODE (lbpluslen) == INTEGER_CST
12129 && tree_int_cst_lt (size, lbpluslen))
12131 error_at (OMP_CLAUSE_LOCATION (c),
12132 "high bound %qE above array section size "
12133 "in %qs clause", lbpluslen,
12134 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12135 return error_mark_node;
12140 else if (length == NULL_TREE)
12142 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12143 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12144 maybe_zero_len = true;
12145 if (first_non_one == types.length ())
12146 first_non_one++;
12149 /* For [lb:] we will need to evaluate lb more than once. */
12150 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12152 tree lb = c_save_expr (low_bound);
12153 if (lb != low_bound)
12155 TREE_PURPOSE (t) = lb;
12156 low_bound = lb;
12160 else if (TREE_CODE (type) == POINTER_TYPE)
12162 if (length == NULL_TREE)
12164 error_at (OMP_CLAUSE_LOCATION (c),
12165 "for pointer type length expression must be specified");
12166 return error_mark_node;
12168 if (length != NULL_TREE
12169 && TREE_CODE (length) == INTEGER_CST
12170 && tree_int_cst_sgn (length) == -1)
12172 error_at (OMP_CLAUSE_LOCATION (c),
12173 "negative length in array section in %qs clause",
12174 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12175 return error_mark_node;
12177 /* If there is a pointer type anywhere but in the very first
12178 array-section-subscript, the array section can't be contiguous. */
12179 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12180 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12182 error_at (OMP_CLAUSE_LOCATION (c),
12183 "array section is not contiguous in %qs clause",
12184 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12185 return error_mark_node;
12188 else
12190 error_at (OMP_CLAUSE_LOCATION (c),
12191 "%qE does not have pointer or array type", ret);
12192 return error_mark_node;
12194 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12195 types.safe_push (TREE_TYPE (ret));
12196 /* We will need to evaluate lb more than once. */
12197 tree lb = c_save_expr (low_bound);
12198 if (lb != low_bound)
12200 TREE_PURPOSE (t) = lb;
12201 low_bound = lb;
12203 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12204 return ret;
12207 /* Handle array sections for clause C. */
12209 static bool
12210 handle_omp_array_sections (tree c, bool is_omp)
12212 bool maybe_zero_len = false;
12213 unsigned int first_non_one = 0;
12214 auto_vec<tree, 10> types;
12215 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12216 maybe_zero_len, first_non_one,
12217 is_omp);
12218 if (first == error_mark_node)
12219 return true;
12220 if (first == NULL_TREE)
12221 return false;
12222 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12224 tree t = OMP_CLAUSE_DECL (c);
12225 tree tem = NULL_TREE;
12226 /* Need to evaluate side effects in the length expressions
12227 if any. */
12228 while (TREE_CODE (t) == TREE_LIST)
12230 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12232 if (tem == NULL_TREE)
12233 tem = TREE_VALUE (t);
12234 else
12235 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12236 TREE_VALUE (t), tem);
12238 t = TREE_CHAIN (t);
12240 if (tem)
12241 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12242 first = c_fully_fold (first, false, NULL);
12243 OMP_CLAUSE_DECL (c) = first;
12245 else
12247 unsigned int num = types.length (), i;
12248 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12249 tree condition = NULL_TREE;
12251 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12252 maybe_zero_len = true;
12254 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12255 t = TREE_CHAIN (t))
12257 tree low_bound = TREE_PURPOSE (t);
12258 tree length = TREE_VALUE (t);
12260 i--;
12261 if (low_bound
12262 && TREE_CODE (low_bound) == INTEGER_CST
12263 && TYPE_PRECISION (TREE_TYPE (low_bound))
12264 > TYPE_PRECISION (sizetype))
12265 low_bound = fold_convert (sizetype, low_bound);
12266 if (length
12267 && TREE_CODE (length) == INTEGER_CST
12268 && TYPE_PRECISION (TREE_TYPE (length))
12269 > TYPE_PRECISION (sizetype))
12270 length = fold_convert (sizetype, length);
12271 if (low_bound == NULL_TREE)
12272 low_bound = integer_zero_node;
12273 if (!maybe_zero_len && i > first_non_one)
12275 if (integer_nonzerop (low_bound))
12276 goto do_warn_noncontiguous;
12277 if (length != NULL_TREE
12278 && TREE_CODE (length) == INTEGER_CST
12279 && TYPE_DOMAIN (types[i])
12280 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12281 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12282 == INTEGER_CST)
12284 tree size;
12285 size = size_binop (PLUS_EXPR,
12286 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12287 size_one_node);
12288 if (!tree_int_cst_equal (length, size))
12290 do_warn_noncontiguous:
12291 error_at (OMP_CLAUSE_LOCATION (c),
12292 "array section is not contiguous in %qs "
12293 "clause",
12294 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12295 return true;
12298 if (length != NULL_TREE
12299 && TREE_SIDE_EFFECTS (length))
12301 if (side_effects == NULL_TREE)
12302 side_effects = length;
12303 else
12304 side_effects = build2 (COMPOUND_EXPR,
12305 TREE_TYPE (side_effects),
12306 length, side_effects);
12309 else
12311 tree l;
12313 if (i > first_non_one
12314 && ((length && integer_nonzerop (length))
12315 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12316 continue;
12317 if (length)
12318 l = fold_convert (sizetype, length);
12319 else
12321 l = size_binop (PLUS_EXPR,
12322 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12323 size_one_node);
12324 l = size_binop (MINUS_EXPR, l,
12325 fold_convert (sizetype, low_bound));
12327 if (i > first_non_one)
12329 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12330 size_zero_node);
12331 if (condition == NULL_TREE)
12332 condition = l;
12333 else
12334 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12335 l, condition);
12337 else if (size == NULL_TREE)
12339 size = size_in_bytes (TREE_TYPE (types[i]));
12340 tree eltype = TREE_TYPE (types[num - 1]);
12341 while (TREE_CODE (eltype) == ARRAY_TYPE)
12342 eltype = TREE_TYPE (eltype);
12343 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12345 if (integer_zerop (size)
12346 || integer_zerop (size_in_bytes (eltype)))
12348 error_at (OMP_CLAUSE_LOCATION (c),
12349 "zero length array section in %qs clause",
12350 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12351 return error_mark_node;
12353 size = size_binop (EXACT_DIV_EXPR, size,
12354 size_in_bytes (eltype));
12356 size = size_binop (MULT_EXPR, size, l);
12357 if (condition)
12358 size = fold_build3 (COND_EXPR, sizetype, condition,
12359 size, size_zero_node);
12361 else
12362 size = size_binop (MULT_EXPR, size, l);
12365 if (side_effects)
12366 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12367 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12369 size = size_binop (MINUS_EXPR, size, size_one_node);
12370 size = c_fully_fold (size, false, NULL);
12371 tree index_type = build_index_type (size);
12372 tree eltype = TREE_TYPE (first);
12373 while (TREE_CODE (eltype) == ARRAY_TYPE)
12374 eltype = TREE_TYPE (eltype);
12375 tree type = build_array_type (eltype, index_type);
12376 tree ptype = build_pointer_type (eltype);
12377 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12378 t = build_fold_addr_expr (t);
12379 tree t2 = build_fold_addr_expr (first);
12380 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12381 ptrdiff_type_node, t2);
12382 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12383 ptrdiff_type_node, t2,
12384 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12385 ptrdiff_type_node, t));
12386 t2 = c_fully_fold (t2, false, NULL);
12387 if (tree_fits_shwi_p (t2))
12388 t = build2 (MEM_REF, type, t,
12389 build_int_cst (ptype, tree_to_shwi (t2)));
12390 else
12392 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12393 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12394 TREE_TYPE (t), t, t2);
12395 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12397 OMP_CLAUSE_DECL (c) = t;
12398 return false;
12400 first = c_fully_fold (first, false, NULL);
12401 OMP_CLAUSE_DECL (c) = first;
12402 if (size)
12403 size = c_fully_fold (size, false, NULL);
12404 OMP_CLAUSE_SIZE (c) = size;
12405 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12406 || (TREE_CODE (t) == COMPONENT_REF
12407 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
12408 return false;
12409 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
12410 if (is_omp)
12411 switch (OMP_CLAUSE_MAP_KIND (c))
12413 case GOMP_MAP_ALLOC:
12414 case GOMP_MAP_TO:
12415 case GOMP_MAP_FROM:
12416 case GOMP_MAP_TOFROM:
12417 case GOMP_MAP_ALWAYS_TO:
12418 case GOMP_MAP_ALWAYS_FROM:
12419 case GOMP_MAP_ALWAYS_TOFROM:
12420 case GOMP_MAP_RELEASE:
12421 case GOMP_MAP_DELETE:
12422 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12423 break;
12424 default:
12425 break;
12427 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12428 if (!is_omp)
12429 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12430 else if (TREE_CODE (t) == COMPONENT_REF)
12431 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12432 else
12433 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12434 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12435 && !c_mark_addressable (t))
12436 return false;
12437 OMP_CLAUSE_DECL (c2) = t;
12438 t = build_fold_addr_expr (first);
12439 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12440 tree ptr = OMP_CLAUSE_DECL (c2);
12441 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12442 ptr = build_fold_addr_expr (ptr);
12443 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12444 ptrdiff_type_node, t,
12445 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12446 ptrdiff_type_node, ptr));
12447 t = c_fully_fold (t, false, NULL);
12448 OMP_CLAUSE_SIZE (c2) = t;
12449 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12450 OMP_CLAUSE_CHAIN (c) = c2;
12452 return false;
12455 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12456 an inline call. But, remap
12457 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12458 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12460 static tree
12461 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12462 tree decl, tree placeholder)
12464 copy_body_data id;
12465 hash_map<tree, tree> decl_map;
12467 decl_map.put (omp_decl1, placeholder);
12468 decl_map.put (omp_decl2, decl);
12469 memset (&id, 0, sizeof (id));
12470 id.src_fn = DECL_CONTEXT (omp_decl1);
12471 id.dst_fn = current_function_decl;
12472 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12473 id.decl_map = &decl_map;
12475 id.copy_decl = copy_decl_no_change;
12476 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12477 id.transform_new_cfg = true;
12478 id.transform_return_to_modify = false;
12479 id.transform_lang_insert_block = NULL;
12480 id.eh_lp_nr = 0;
12481 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12482 return stmt;
12485 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12486 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12488 static tree
12489 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12491 if (*tp == (tree) data)
12492 return *tp;
12493 return NULL_TREE;
12496 /* For all elements of CLAUSES, validate them against their constraints.
12497 Remove any elements from the list that are invalid. */
12499 tree
12500 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
12502 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12503 bitmap_head aligned_head, map_head, map_field_head;
12504 tree c, t, type, *pc;
12505 tree simdlen = NULL_TREE, safelen = NULL_TREE;
12506 bool branch_seen = false;
12507 bool copyprivate_seen = false;
12508 bool linear_variable_step_check = false;
12509 tree *nowait_clause = NULL;
12510 bool ordered_seen = false;
12511 tree schedule_clause = NULL_TREE;
12513 bitmap_obstack_initialize (NULL);
12514 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12515 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12516 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12517 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12518 bitmap_initialize (&map_head, &bitmap_default_obstack);
12519 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
12521 for (pc = &clauses, c = clauses; c ; c = *pc)
12523 bool remove = false;
12524 bool need_complete = false;
12525 bool need_implicitly_determined = false;
12527 switch (OMP_CLAUSE_CODE (c))
12529 case OMP_CLAUSE_SHARED:
12530 need_implicitly_determined = true;
12531 goto check_dup_generic;
12533 case OMP_CLAUSE_PRIVATE:
12534 need_complete = true;
12535 need_implicitly_determined = true;
12536 goto check_dup_generic;
12538 case OMP_CLAUSE_REDUCTION:
12539 need_implicitly_determined = true;
12540 t = OMP_CLAUSE_DECL (c);
12541 if (TREE_CODE (t) == TREE_LIST)
12543 if (handle_omp_array_sections (c, ort & C_ORT_OMP))
12545 remove = true;
12546 break;
12549 t = OMP_CLAUSE_DECL (c);
12551 t = require_complete_type (t);
12552 if (t == error_mark_node)
12554 remove = true;
12555 break;
12557 type = TREE_TYPE (t);
12558 if (TREE_CODE (t) == MEM_REF)
12559 type = TREE_TYPE (type);
12560 if (TREE_CODE (type) == ARRAY_TYPE)
12562 tree oatype = type;
12563 gcc_assert (TREE_CODE (t) != MEM_REF);
12564 while (TREE_CODE (type) == ARRAY_TYPE)
12565 type = TREE_TYPE (type);
12566 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12568 error_at (OMP_CLAUSE_LOCATION (c),
12569 "%qD in %<reduction%> clause is a zero size array",
12571 remove = true;
12572 break;
12574 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12575 TYPE_SIZE_UNIT (type));
12576 if (integer_zerop (size))
12578 error_at (OMP_CLAUSE_LOCATION (c),
12579 "%qD in %<reduction%> clause is a zero size array",
12581 remove = true;
12582 break;
12584 size = size_binop (MINUS_EXPR, size, size_one_node);
12585 tree index_type = build_index_type (size);
12586 tree atype = build_array_type (type, index_type);
12587 tree ptype = build_pointer_type (type);
12588 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12589 t = build_fold_addr_expr (t);
12590 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12591 OMP_CLAUSE_DECL (c) = t;
12593 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12594 && (FLOAT_TYPE_P (type)
12595 || TREE_CODE (type) == COMPLEX_TYPE))
12597 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12598 const char *r_name = NULL;
12600 switch (r_code)
12602 case PLUS_EXPR:
12603 case MULT_EXPR:
12604 case MINUS_EXPR:
12605 break;
12606 case MIN_EXPR:
12607 if (TREE_CODE (type) == COMPLEX_TYPE)
12608 r_name = "min";
12609 break;
12610 case MAX_EXPR:
12611 if (TREE_CODE (type) == COMPLEX_TYPE)
12612 r_name = "max";
12613 break;
12614 case BIT_AND_EXPR:
12615 r_name = "&";
12616 break;
12617 case BIT_XOR_EXPR:
12618 r_name = "^";
12619 break;
12620 case BIT_IOR_EXPR:
12621 r_name = "|";
12622 break;
12623 case TRUTH_ANDIF_EXPR:
12624 if (FLOAT_TYPE_P (type))
12625 r_name = "&&";
12626 break;
12627 case TRUTH_ORIF_EXPR:
12628 if (FLOAT_TYPE_P (type))
12629 r_name = "||";
12630 break;
12631 default:
12632 gcc_unreachable ();
12634 if (r_name)
12636 error_at (OMP_CLAUSE_LOCATION (c),
12637 "%qE has invalid type for %<reduction(%s)%>",
12638 t, r_name);
12639 remove = true;
12640 break;
12643 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12645 error_at (OMP_CLAUSE_LOCATION (c),
12646 "user defined reduction not found for %qE", t);
12647 remove = true;
12648 break;
12650 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12652 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12653 type = TYPE_MAIN_VARIANT (type);
12654 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12655 VAR_DECL, NULL_TREE, type);
12656 tree decl_placeholder = NULL_TREE;
12657 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12658 DECL_ARTIFICIAL (placeholder) = 1;
12659 DECL_IGNORED_P (placeholder) = 1;
12660 if (TREE_CODE (t) == MEM_REF)
12662 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12663 VAR_DECL, NULL_TREE, type);
12664 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12665 DECL_ARTIFICIAL (decl_placeholder) = 1;
12666 DECL_IGNORED_P (decl_placeholder) = 1;
12668 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12669 c_mark_addressable (placeholder);
12670 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12671 c_mark_addressable (decl_placeholder ? decl_placeholder
12672 : OMP_CLAUSE_DECL (c));
12673 OMP_CLAUSE_REDUCTION_MERGE (c)
12674 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12675 TREE_VEC_ELT (list, 0),
12676 TREE_VEC_ELT (list, 1),
12677 decl_placeholder ? decl_placeholder
12678 : OMP_CLAUSE_DECL (c), placeholder);
12679 OMP_CLAUSE_REDUCTION_MERGE (c)
12680 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12681 void_type_node, NULL_TREE,
12682 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12683 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12684 if (TREE_VEC_LENGTH (list) == 6)
12686 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12687 c_mark_addressable (decl_placeholder ? decl_placeholder
12688 : OMP_CLAUSE_DECL (c));
12689 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12690 c_mark_addressable (placeholder);
12691 tree init = TREE_VEC_ELT (list, 5);
12692 if (init == error_mark_node)
12693 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12694 OMP_CLAUSE_REDUCTION_INIT (c)
12695 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12696 TREE_VEC_ELT (list, 3),
12697 decl_placeholder ? decl_placeholder
12698 : OMP_CLAUSE_DECL (c), placeholder);
12699 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12701 tree v = decl_placeholder ? decl_placeholder : t;
12702 OMP_CLAUSE_REDUCTION_INIT (c)
12703 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12704 OMP_CLAUSE_REDUCTION_INIT (c));
12706 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12707 c_find_omp_placeholder_r,
12708 placeholder, NULL))
12709 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12711 else
12713 tree init;
12714 tree v = decl_placeholder ? decl_placeholder : t;
12715 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12716 init = build_constructor (TREE_TYPE (v), NULL);
12717 else
12718 init = fold_convert (TREE_TYPE (v), integer_zero_node);
12719 OMP_CLAUSE_REDUCTION_INIT (c)
12720 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
12722 OMP_CLAUSE_REDUCTION_INIT (c)
12723 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12724 void_type_node, NULL_TREE,
12725 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12726 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12728 if (TREE_CODE (t) == MEM_REF)
12730 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12731 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12732 != INTEGER_CST)
12734 sorry ("variable length element type in array "
12735 "%<reduction%> clause");
12736 remove = true;
12737 break;
12739 t = TREE_OPERAND (t, 0);
12740 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12741 t = TREE_OPERAND (t, 0);
12742 if (TREE_CODE (t) == ADDR_EXPR)
12743 t = TREE_OPERAND (t, 0);
12745 goto check_dup_generic_t;
12747 case OMP_CLAUSE_COPYPRIVATE:
12748 copyprivate_seen = true;
12749 if (nowait_clause)
12751 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12752 "%<nowait%> clause must not be used together "
12753 "with %<copyprivate%>");
12754 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12755 nowait_clause = NULL;
12757 goto check_dup_generic;
12759 case OMP_CLAUSE_COPYIN:
12760 t = OMP_CLAUSE_DECL (c);
12761 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
12763 error_at (OMP_CLAUSE_LOCATION (c),
12764 "%qE must be %<threadprivate%> for %<copyin%>", t);
12765 remove = true;
12766 break;
12768 goto check_dup_generic;
12770 case OMP_CLAUSE_LINEAR:
12771 if (ort != C_ORT_OMP_DECLARE_SIMD)
12772 need_implicitly_determined = true;
12773 t = OMP_CLAUSE_DECL (c);
12774 if (ort != C_ORT_OMP_DECLARE_SIMD
12775 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
12777 error_at (OMP_CLAUSE_LOCATION (c),
12778 "modifier should not be specified in %<linear%> "
12779 "clause on %<simd%> or %<for%> constructs");
12780 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
12782 if (ort & C_ORT_CILK)
12784 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12785 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
12786 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12788 error_at (OMP_CLAUSE_LOCATION (c),
12789 "linear clause applied to non-integral, "
12790 "non-floating, non-pointer variable with type %qT",
12791 TREE_TYPE (t));
12792 remove = true;
12793 break;
12796 else
12798 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12799 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12801 error_at (OMP_CLAUSE_LOCATION (c),
12802 "linear clause applied to non-integral non-pointer "
12803 "variable with type %qT", TREE_TYPE (t));
12804 remove = true;
12805 break;
12808 if (ort == C_ORT_OMP_DECLARE_SIMD)
12810 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12811 if (TREE_CODE (s) == PARM_DECL)
12813 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
12814 /* map_head bitmap is used as uniform_head if
12815 declare_simd. */
12816 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
12817 linear_variable_step_check = true;
12818 goto check_dup_generic;
12820 if (TREE_CODE (s) != INTEGER_CST)
12822 error_at (OMP_CLAUSE_LOCATION (c),
12823 "%<linear%> clause step %qE is neither constant "
12824 "nor a parameter", s);
12825 remove = true;
12826 break;
12829 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12831 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12832 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12833 OMP_CLAUSE_DECL (c), s);
12834 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12835 sizetype, fold_convert (sizetype, s),
12836 fold_convert
12837 (sizetype, OMP_CLAUSE_DECL (c)));
12838 if (s == error_mark_node)
12839 s = size_one_node;
12840 OMP_CLAUSE_LINEAR_STEP (c) = s;
12842 else
12843 OMP_CLAUSE_LINEAR_STEP (c)
12844 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12845 goto check_dup_generic;
12847 check_dup_generic:
12848 t = OMP_CLAUSE_DECL (c);
12849 check_dup_generic_t:
12850 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12852 error_at (OMP_CLAUSE_LOCATION (c),
12853 "%qE is not a variable in clause %qs", t,
12854 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12855 remove = true;
12857 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12858 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12859 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12861 error_at (OMP_CLAUSE_LOCATION (c),
12862 "%qE appears more than once in data clauses", t);
12863 remove = true;
12865 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12866 && bitmap_bit_p (&map_head, DECL_UID (t)))
12868 error ("%qD appears both in data and map clauses", t);
12869 remove = true;
12871 else
12872 bitmap_set_bit (&generic_head, DECL_UID (t));
12873 break;
12875 case OMP_CLAUSE_FIRSTPRIVATE:
12876 t = OMP_CLAUSE_DECL (c);
12877 need_complete = true;
12878 need_implicitly_determined = true;
12879 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12881 error_at (OMP_CLAUSE_LOCATION (c),
12882 "%qE is not a variable in clause %<firstprivate%>", t);
12883 remove = true;
12885 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12886 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12888 error_at (OMP_CLAUSE_LOCATION (c),
12889 "%qE appears more than once in data clauses", t);
12890 remove = true;
12892 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
12894 error ("%qD appears both in data and map clauses", t);
12895 remove = true;
12897 else
12898 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12899 break;
12901 case OMP_CLAUSE_LASTPRIVATE:
12902 t = OMP_CLAUSE_DECL (c);
12903 need_complete = true;
12904 need_implicitly_determined = true;
12905 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12907 error_at (OMP_CLAUSE_LOCATION (c),
12908 "%qE is not a variable in clause %<lastprivate%>", t);
12909 remove = true;
12911 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12912 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12914 error_at (OMP_CLAUSE_LOCATION (c),
12915 "%qE appears more than once in data clauses", t);
12916 remove = true;
12918 else
12919 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12920 break;
12922 case OMP_CLAUSE_ALIGNED:
12923 t = OMP_CLAUSE_DECL (c);
12924 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12926 error_at (OMP_CLAUSE_LOCATION (c),
12927 "%qE is not a variable in %<aligned%> clause", t);
12928 remove = true;
12930 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12931 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12933 error_at (OMP_CLAUSE_LOCATION (c),
12934 "%qE in %<aligned%> clause is neither a pointer nor "
12935 "an array", t);
12936 remove = true;
12938 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12940 error_at (OMP_CLAUSE_LOCATION (c),
12941 "%qE appears more than once in %<aligned%> clauses",
12943 remove = true;
12945 else
12946 bitmap_set_bit (&aligned_head, DECL_UID (t));
12947 break;
12949 case OMP_CLAUSE_DEPEND:
12950 t = OMP_CLAUSE_DECL (c);
12951 if (t == NULL_TREE)
12953 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
12954 == OMP_CLAUSE_DEPEND_SOURCE);
12955 break;
12957 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
12959 gcc_assert (TREE_CODE (t) == TREE_LIST);
12960 for (; t; t = TREE_CHAIN (t))
12962 tree decl = TREE_VALUE (t);
12963 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
12965 tree offset = TREE_PURPOSE (t);
12966 bool neg = wi::neg_p ((wide_int) offset);
12967 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
12968 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
12969 neg ? MINUS_EXPR : PLUS_EXPR,
12970 decl, offset);
12971 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12972 sizetype,
12973 fold_convert (sizetype, t2),
12974 fold_convert (sizetype, decl));
12975 if (t2 == error_mark_node)
12977 remove = true;
12978 break;
12980 TREE_PURPOSE (t) = t2;
12983 break;
12985 if (TREE_CODE (t) == TREE_LIST)
12987 if (handle_omp_array_sections (c, ort & C_ORT_OMP))
12988 remove = true;
12989 break;
12991 if (t == error_mark_node)
12992 remove = true;
12993 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12995 error_at (OMP_CLAUSE_LOCATION (c),
12996 "%qE is not a variable in %<depend%> clause", t);
12997 remove = true;
12999 else if (!c_mark_addressable (t))
13000 remove = true;
13001 break;
13003 case OMP_CLAUSE_MAP:
13004 case OMP_CLAUSE_TO:
13005 case OMP_CLAUSE_FROM:
13006 case OMP_CLAUSE__CACHE_:
13007 t = OMP_CLAUSE_DECL (c);
13008 if (TREE_CODE (t) == TREE_LIST)
13010 if (handle_omp_array_sections (c, ort & C_ORT_OMP))
13011 remove = true;
13012 else
13014 t = OMP_CLAUSE_DECL (c);
13015 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13017 error_at (OMP_CLAUSE_LOCATION (c),
13018 "array section does not have mappable type "
13019 "in %qs clause",
13020 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13021 remove = true;
13023 while (TREE_CODE (t) == ARRAY_REF)
13024 t = TREE_OPERAND (t, 0);
13025 if (TREE_CODE (t) == COMPONENT_REF
13026 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13028 while (TREE_CODE (t) == COMPONENT_REF)
13029 t = TREE_OPERAND (t, 0);
13030 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13031 break;
13032 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13034 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13035 error ("%qD appears more than once in motion"
13036 " clauses", t);
13037 else
13038 error ("%qD appears more than once in map"
13039 " clauses", t);
13040 remove = true;
13042 else
13044 bitmap_set_bit (&map_head, DECL_UID (t));
13045 bitmap_set_bit (&map_field_head, DECL_UID (t));
13049 break;
13051 if (t == error_mark_node)
13053 remove = true;
13054 break;
13056 if (TREE_CODE (t) == COMPONENT_REF
13057 && (ort & C_ORT_OMP)
13058 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13060 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13062 error_at (OMP_CLAUSE_LOCATION (c),
13063 "bit-field %qE in %qs clause",
13064 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13065 remove = true;
13067 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13069 error_at (OMP_CLAUSE_LOCATION (c),
13070 "%qE does not have a mappable type in %qs clause",
13071 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13072 remove = true;
13074 while (TREE_CODE (t) == COMPONENT_REF)
13076 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13077 == UNION_TYPE)
13079 error_at (OMP_CLAUSE_LOCATION (c),
13080 "%qE is a member of a union", t);
13081 remove = true;
13082 break;
13084 t = TREE_OPERAND (t, 0);
13086 if (remove)
13087 break;
13088 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13090 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13091 break;
13094 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13096 error_at (OMP_CLAUSE_LOCATION (c),
13097 "%qE is not a variable in %qs clause", t,
13098 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13099 remove = true;
13101 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13103 error_at (OMP_CLAUSE_LOCATION (c),
13104 "%qD is threadprivate variable in %qs clause", t,
13105 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13106 remove = true;
13108 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13109 || (OMP_CLAUSE_MAP_KIND (c)
13110 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13111 && !c_mark_addressable (t))
13112 remove = true;
13113 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13114 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13115 || (OMP_CLAUSE_MAP_KIND (c)
13116 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13117 || (OMP_CLAUSE_MAP_KIND (c)
13118 == GOMP_MAP_FORCE_DEVICEPTR)))
13119 && t == OMP_CLAUSE_DECL (c)
13120 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13122 error_at (OMP_CLAUSE_LOCATION (c),
13123 "%qD does not have a mappable type in %qs clause", t,
13124 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13125 remove = true;
13127 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13128 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13130 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13131 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13133 error ("%qD appears more than once in data clauses", t);
13134 remove = true;
13136 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13138 error ("%qD appears both in data and map clauses", t);
13139 remove = true;
13141 else
13142 bitmap_set_bit (&generic_head, DECL_UID (t));
13144 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13146 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13147 error ("%qD appears more than once in motion clauses", t);
13148 else
13149 error ("%qD appears more than once in map clauses", t);
13150 remove = true;
13152 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13153 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13155 error ("%qD appears both in data and map clauses", t);
13156 remove = true;
13158 else
13160 bitmap_set_bit (&map_head, DECL_UID (t));
13161 if (t != OMP_CLAUSE_DECL (c)
13162 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13163 bitmap_set_bit (&map_field_head, DECL_UID (t));
13165 break;
13167 case OMP_CLAUSE_TO_DECLARE:
13168 case OMP_CLAUSE_LINK:
13169 t = OMP_CLAUSE_DECL (c);
13170 if (TREE_CODE (t) == FUNCTION_DECL
13171 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13173 else if (!VAR_P (t))
13175 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13176 error_at (OMP_CLAUSE_LOCATION (c),
13177 "%qE is neither a variable nor a function name in "
13178 "clause %qs", t,
13179 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13180 else
13181 error_at (OMP_CLAUSE_LOCATION (c),
13182 "%qE is not a variable in clause %qs", t,
13183 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13184 remove = true;
13186 else if (DECL_THREAD_LOCAL_P (t))
13188 error_at (OMP_CLAUSE_LOCATION (c),
13189 "%qD is threadprivate variable in %qs clause", t,
13190 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13191 remove = true;
13193 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13195 error_at (OMP_CLAUSE_LOCATION (c),
13196 "%qD does not have a mappable type in %qs clause", t,
13197 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13198 remove = true;
13200 if (remove)
13201 break;
13202 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13204 error_at (OMP_CLAUSE_LOCATION (c),
13205 "%qE appears more than once on the same "
13206 "%<declare target%> directive", t);
13207 remove = true;
13209 else
13210 bitmap_set_bit (&generic_head, DECL_UID (t));
13211 break;
13213 case OMP_CLAUSE_UNIFORM:
13214 t = OMP_CLAUSE_DECL (c);
13215 if (TREE_CODE (t) != PARM_DECL)
13217 if (DECL_P (t))
13218 error_at (OMP_CLAUSE_LOCATION (c),
13219 "%qD is not an argument in %<uniform%> clause", t);
13220 else
13221 error_at (OMP_CLAUSE_LOCATION (c),
13222 "%qE is not an argument in %<uniform%> clause", t);
13223 remove = true;
13224 break;
13226 /* map_head bitmap is used as uniform_head if declare_simd. */
13227 bitmap_set_bit (&map_head, DECL_UID (t));
13228 goto check_dup_generic;
13230 case OMP_CLAUSE_IS_DEVICE_PTR:
13231 case OMP_CLAUSE_USE_DEVICE_PTR:
13232 t = OMP_CLAUSE_DECL (c);
13233 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13234 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13236 error_at (OMP_CLAUSE_LOCATION (c),
13237 "%qs variable is neither a pointer nor an array",
13238 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13239 remove = true;
13241 goto check_dup_generic;
13243 case OMP_CLAUSE_NOWAIT:
13244 if (copyprivate_seen)
13246 error_at (OMP_CLAUSE_LOCATION (c),
13247 "%<nowait%> clause must not be used together "
13248 "with %<copyprivate%>");
13249 remove = true;
13250 break;
13252 nowait_clause = pc;
13253 pc = &OMP_CLAUSE_CHAIN (c);
13254 continue;
13256 case OMP_CLAUSE_IF:
13257 case OMP_CLAUSE_NUM_THREADS:
13258 case OMP_CLAUSE_NUM_TEAMS:
13259 case OMP_CLAUSE_THREAD_LIMIT:
13260 case OMP_CLAUSE_DEFAULT:
13261 case OMP_CLAUSE_UNTIED:
13262 case OMP_CLAUSE_COLLAPSE:
13263 case OMP_CLAUSE_FINAL:
13264 case OMP_CLAUSE_MERGEABLE:
13265 case OMP_CLAUSE_DEVICE:
13266 case OMP_CLAUSE_DIST_SCHEDULE:
13267 case OMP_CLAUSE_PARALLEL:
13268 case OMP_CLAUSE_FOR:
13269 case OMP_CLAUSE_SECTIONS:
13270 case OMP_CLAUSE_TASKGROUP:
13271 case OMP_CLAUSE_PROC_BIND:
13272 case OMP_CLAUSE_PRIORITY:
13273 case OMP_CLAUSE_GRAINSIZE:
13274 case OMP_CLAUSE_NUM_TASKS:
13275 case OMP_CLAUSE_NOGROUP:
13276 case OMP_CLAUSE_THREADS:
13277 case OMP_CLAUSE_SIMD:
13278 case OMP_CLAUSE_HINT:
13279 case OMP_CLAUSE_DEFAULTMAP:
13280 case OMP_CLAUSE__CILK_FOR_COUNT_:
13281 case OMP_CLAUSE_NUM_GANGS:
13282 case OMP_CLAUSE_NUM_WORKERS:
13283 case OMP_CLAUSE_VECTOR_LENGTH:
13284 case OMP_CLAUSE_ASYNC:
13285 case OMP_CLAUSE_WAIT:
13286 case OMP_CLAUSE_AUTO:
13287 case OMP_CLAUSE_INDEPENDENT:
13288 case OMP_CLAUSE_SEQ:
13289 case OMP_CLAUSE_GANG:
13290 case OMP_CLAUSE_WORKER:
13291 case OMP_CLAUSE_VECTOR:
13292 case OMP_CLAUSE_TILE:
13293 pc = &OMP_CLAUSE_CHAIN (c);
13294 continue;
13296 case OMP_CLAUSE_SCHEDULE:
13297 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13299 const char *p = NULL;
13300 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13302 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13303 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13304 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13305 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13306 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13307 default: gcc_unreachable ();
13309 if (p)
13311 error_at (OMP_CLAUSE_LOCATION (c),
13312 "%<nonmonotonic%> modifier specified for %qs "
13313 "schedule kind", p);
13314 OMP_CLAUSE_SCHEDULE_KIND (c)
13315 = (enum omp_clause_schedule_kind)
13316 (OMP_CLAUSE_SCHEDULE_KIND (c)
13317 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13320 schedule_clause = c;
13321 pc = &OMP_CLAUSE_CHAIN (c);
13322 continue;
13324 case OMP_CLAUSE_ORDERED:
13325 ordered_seen = true;
13326 pc = &OMP_CLAUSE_CHAIN (c);
13327 continue;
13329 case OMP_CLAUSE_SAFELEN:
13330 safelen = c;
13331 pc = &OMP_CLAUSE_CHAIN (c);
13332 continue;
13333 case OMP_CLAUSE_SIMDLEN:
13334 simdlen = c;
13335 pc = &OMP_CLAUSE_CHAIN (c);
13336 continue;
13338 case OMP_CLAUSE_INBRANCH:
13339 case OMP_CLAUSE_NOTINBRANCH:
13340 if (branch_seen)
13342 error_at (OMP_CLAUSE_LOCATION (c),
13343 "%<inbranch%> clause is incompatible with "
13344 "%<notinbranch%>");
13345 remove = true;
13346 break;
13348 branch_seen = true;
13349 pc = &OMP_CLAUSE_CHAIN (c);
13350 continue;
13352 default:
13353 gcc_unreachable ();
13356 if (!remove)
13358 t = OMP_CLAUSE_DECL (c);
13360 if (need_complete)
13362 t = require_complete_type (t);
13363 if (t == error_mark_node)
13364 remove = true;
13367 if (need_implicitly_determined)
13369 const char *share_name = NULL;
13371 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13372 share_name = "threadprivate";
13373 else switch (c_omp_predetermined_sharing (t))
13375 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13376 break;
13377 case OMP_CLAUSE_DEFAULT_SHARED:
13378 /* const vars may be specified in firstprivate clause. */
13379 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13380 && TREE_READONLY (t))
13381 break;
13382 share_name = "shared";
13383 break;
13384 case OMP_CLAUSE_DEFAULT_PRIVATE:
13385 share_name = "private";
13386 break;
13387 default:
13388 gcc_unreachable ();
13390 if (share_name)
13392 error_at (OMP_CLAUSE_LOCATION (c),
13393 "%qE is predetermined %qs for %qs",
13394 t, share_name,
13395 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13396 remove = true;
13401 if (remove)
13402 *pc = OMP_CLAUSE_CHAIN (c);
13403 else
13404 pc = &OMP_CLAUSE_CHAIN (c);
13407 if (simdlen
13408 && safelen
13409 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13410 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13412 error_at (OMP_CLAUSE_LOCATION (simdlen),
13413 "%<simdlen%> clause value is bigger than "
13414 "%<safelen%> clause value");
13415 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13416 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13419 if (ordered_seen
13420 && schedule_clause
13421 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13422 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13424 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13425 "%<nonmonotonic%> schedule modifier specified together "
13426 "with %<ordered%> clause");
13427 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13428 = (enum omp_clause_schedule_kind)
13429 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13430 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13433 if (linear_variable_step_check)
13434 for (pc = &clauses, c = clauses; c ; c = *pc)
13436 bool remove = false;
13437 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13438 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13439 && !bitmap_bit_p (&map_head,
13440 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13442 error_at (OMP_CLAUSE_LOCATION (c),
13443 "%<linear%> clause step is a parameter %qD not "
13444 "specified in %<uniform%> clause",
13445 OMP_CLAUSE_LINEAR_STEP (c));
13446 remove = true;
13449 if (remove)
13450 *pc = OMP_CLAUSE_CHAIN (c);
13451 else
13452 pc = &OMP_CLAUSE_CHAIN (c);
13455 bitmap_obstack_release (NULL);
13456 return clauses;
13459 /* Create a transaction node. */
13461 tree
13462 c_finish_transaction (location_t loc, tree block, int flags)
13464 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13465 if (flags & TM_STMT_ATTR_OUTER)
13466 TRANSACTION_EXPR_OUTER (stmt) = 1;
13467 if (flags & TM_STMT_ATTR_RELAXED)
13468 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13469 return add_stmt (stmt);
13472 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13473 down to the element type of an array. If ORIG_QUAL_TYPE is not
13474 NULL, then it should be used as the qualified type
13475 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13476 preserve information about the typedef name from which an array
13477 type was derived). */
13479 tree
13480 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13481 size_t orig_qual_indirect)
13483 if (type == error_mark_node)
13484 return type;
13486 if (TREE_CODE (type) == ARRAY_TYPE)
13488 tree t;
13489 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13490 type_quals, orig_qual_type,
13491 orig_qual_indirect - 1);
13493 /* See if we already have an identically qualified type. */
13494 if (orig_qual_type && orig_qual_indirect == 0)
13495 t = orig_qual_type;
13496 else
13497 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13499 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13500 && TYPE_NAME (t) == TYPE_NAME (type)
13501 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13502 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13503 TYPE_ATTRIBUTES (type)))
13504 break;
13506 if (!t)
13508 tree domain = TYPE_DOMAIN (type);
13510 t = build_variant_type_copy (type);
13511 TREE_TYPE (t) = element_type;
13513 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13514 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13515 SET_TYPE_STRUCTURAL_EQUALITY (t);
13516 else if (TYPE_CANONICAL (element_type) != element_type
13517 || (domain && TYPE_CANONICAL (domain) != domain))
13519 tree unqualified_canon
13520 = build_array_type (TYPE_CANONICAL (element_type),
13521 domain? TYPE_CANONICAL (domain)
13522 : NULL_TREE);
13523 if (TYPE_REVERSE_STORAGE_ORDER (type))
13525 unqualified_canon
13526 = build_distinct_type_copy (unqualified_canon);
13527 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13529 TYPE_CANONICAL (t)
13530 = c_build_qualified_type (unqualified_canon, type_quals);
13532 else
13533 TYPE_CANONICAL (t) = t;
13535 return t;
13538 /* A restrict-qualified pointer type must be a pointer to object or
13539 incomplete type. Note that the use of POINTER_TYPE_P also allows
13540 REFERENCE_TYPEs, which is appropriate for C++. */
13541 if ((type_quals & TYPE_QUAL_RESTRICT)
13542 && (!POINTER_TYPE_P (type)
13543 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13545 error ("invalid use of %<restrict%>");
13546 type_quals &= ~TYPE_QUAL_RESTRICT;
13549 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13550 ? orig_qual_type
13551 : build_qualified_type (type, type_quals));
13552 /* A variant type does not inherit the list of incomplete vars from the
13553 type main variant. */
13554 if (RECORD_OR_UNION_TYPE_P (var_type))
13555 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13556 return var_type;
13559 /* Build a VA_ARG_EXPR for the C parser. */
13561 tree
13562 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
13564 if (error_operand_p (type))
13565 return error_mark_node;
13566 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13567 order because it takes the address of the expression. */
13568 else if (handled_component_p (expr)
13569 && reverse_storage_order_for_component_p (expr))
13571 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13572 return error_mark_node;
13574 else if (!COMPLETE_TYPE_P (type))
13576 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
13577 "type %qT", type);
13578 return error_mark_node;
13580 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
13581 warning_at (loc2, OPT_Wc___compat,
13582 "C++ requires promoted type, not enum type, in %<va_arg%>");
13583 return build_va_arg (loc2, expr, type);
13586 /* Return truthvalue of whether T1 is the same tree structure as T2.
13587 Return 1 if they are the same. Return 0 if they are different. */
13589 bool
13590 c_tree_equal (tree t1, tree t2)
13592 enum tree_code code1, code2;
13594 if (t1 == t2)
13595 return true;
13596 if (!t1 || !t2)
13597 return false;
13599 for (code1 = TREE_CODE (t1);
13600 CONVERT_EXPR_CODE_P (code1)
13601 || code1 == NON_LVALUE_EXPR;
13602 code1 = TREE_CODE (t1))
13603 t1 = TREE_OPERAND (t1, 0);
13604 for (code2 = TREE_CODE (t2);
13605 CONVERT_EXPR_CODE_P (code2)
13606 || code2 == NON_LVALUE_EXPR;
13607 code2 = TREE_CODE (t2))
13608 t2 = TREE_OPERAND (t2, 0);
13610 /* They might have become equal now. */
13611 if (t1 == t2)
13612 return true;
13614 if (code1 != code2)
13615 return false;
13617 switch (code1)
13619 case INTEGER_CST:
13620 return wi::eq_p (t1, t2);
13622 case REAL_CST:
13623 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
13625 case STRING_CST:
13626 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13627 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13628 TREE_STRING_LENGTH (t1));
13630 case FIXED_CST:
13631 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13632 TREE_FIXED_CST (t2));
13634 case COMPLEX_CST:
13635 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13636 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13638 case VECTOR_CST:
13639 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13641 case CONSTRUCTOR:
13642 /* We need to do this when determining whether or not two
13643 non-type pointer to member function template arguments
13644 are the same. */
13645 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13646 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13647 return false;
13649 tree field, value;
13650 unsigned int i;
13651 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13653 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13654 if (!c_tree_equal (field, elt2->index)
13655 || !c_tree_equal (value, elt2->value))
13656 return false;
13659 return true;
13661 case TREE_LIST:
13662 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13663 return false;
13664 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
13665 return false;
13666 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
13668 case SAVE_EXPR:
13669 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13671 case CALL_EXPR:
13673 tree arg1, arg2;
13674 call_expr_arg_iterator iter1, iter2;
13675 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
13676 return false;
13677 for (arg1 = first_call_expr_arg (t1, &iter1),
13678 arg2 = first_call_expr_arg (t2, &iter2);
13679 arg1 && arg2;
13680 arg1 = next_call_expr_arg (&iter1),
13681 arg2 = next_call_expr_arg (&iter2))
13682 if (!c_tree_equal (arg1, arg2))
13683 return false;
13684 if (arg1 || arg2)
13685 return false;
13686 return true;
13689 case TARGET_EXPR:
13691 tree o1 = TREE_OPERAND (t1, 0);
13692 tree o2 = TREE_OPERAND (t2, 0);
13694 /* Special case: if either target is an unallocated VAR_DECL,
13695 it means that it's going to be unified with whatever the
13696 TARGET_EXPR is really supposed to initialize, so treat it
13697 as being equivalent to anything. */
13698 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
13699 && !DECL_RTL_SET_P (o1))
13700 /*Nop*/;
13701 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
13702 && !DECL_RTL_SET_P (o2))
13703 /*Nop*/;
13704 else if (!c_tree_equal (o1, o2))
13705 return false;
13707 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
13710 case COMPONENT_REF:
13711 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
13712 return false;
13713 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13715 case PARM_DECL:
13716 case VAR_DECL:
13717 case CONST_DECL:
13718 case FIELD_DECL:
13719 case FUNCTION_DECL:
13720 case IDENTIFIER_NODE:
13721 case SSA_NAME:
13722 return false;
13724 case TREE_VEC:
13726 unsigned ix;
13727 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
13728 return false;
13729 for (ix = TREE_VEC_LENGTH (t1); ix--;)
13730 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
13731 TREE_VEC_ELT (t2, ix)))
13732 return false;
13733 return true;
13736 default:
13737 break;
13740 switch (TREE_CODE_CLASS (code1))
13742 case tcc_unary:
13743 case tcc_binary:
13744 case tcc_comparison:
13745 case tcc_expression:
13746 case tcc_vl_exp:
13747 case tcc_reference:
13748 case tcc_statement:
13750 int i, n = TREE_OPERAND_LENGTH (t1);
13752 switch (code1)
13754 case PREINCREMENT_EXPR:
13755 case PREDECREMENT_EXPR:
13756 case POSTINCREMENT_EXPR:
13757 case POSTDECREMENT_EXPR:
13758 n = 1;
13759 break;
13760 case ARRAY_REF:
13761 n = 2;
13762 break;
13763 default:
13764 break;
13767 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
13768 && n != TREE_OPERAND_LENGTH (t2))
13769 return false;
13771 for (i = 0; i < n; ++i)
13772 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
13773 return false;
13775 return true;
13778 case tcc_type:
13779 return comptypes (t1, t2);
13780 default:
13781 gcc_unreachable ();
13783 /* We can get here with --disable-checking. */
13784 return false;
13787 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
13788 spawn-helper and BODY is the newly created body for FNDECL. */
13790 void
13791 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
13793 tree list = alloc_stmt_list ();
13794 tree frame = make_cilk_frame (fndecl);
13795 tree dtor = create_cilk_function_exit (frame, false, true);
13796 add_local_decl (cfun, frame);
13798 DECL_SAVED_TREE (fndecl) = list;
13799 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
13800 frame);
13801 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
13802 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
13804 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
13805 append_to_statement_list (detach_expr, &body_list);
13807 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
13808 body = fold_build_cleanup_point_expr (void_type_node, body);
13810 append_to_statement_list (body, &body_list);
13811 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
13812 body_list, dtor), &list);
13815 /* Returns true when the function declaration FNDECL is implicit,
13816 introduced as a result of a call to an otherwise undeclared
13817 function, and false otherwise. */
13819 bool
13820 c_decl_implicit (const_tree fndecl)
13822 return C_DECL_IMPLICIT (fndecl);