PR middle-end/66633
[official-gcc.git] / gcc / c / c-typeck.c
blob6ea35137d50ec62aad2d155c9d1937cd6ffc9c64
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2015 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 "tm.h"
30 #include "symtab.h"
31 #include "alias.h"
32 #include "tree.h"
33 #include "fold-const.h"
34 #include "stor-layout.h"
35 #include "trans-mem.h"
36 #include "varasm.h"
37 #include "stmt.h"
38 #include "langhooks.h"
39 #include "c-tree.h"
40 #include "c-lang.h"
41 #include "flags.h"
42 #include "intl.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "bitmap.h"
46 #include "predict.h"
47 #include "hard-reg-set.h"
48 #include "function.h"
49 #include "gimple-expr.h"
50 #include "gimplify.h"
51 #include "tree-inline.h"
52 #include "omp-low.h"
53 #include "c-family/c-objc.h"
54 #include "c-family/c-common.h"
55 #include "c-family/c-ubsan.h"
56 #include "cilk.h"
57 #include "gomp-constants.h"
59 /* Possible cases of implicit bad conversions. Used to select
60 diagnostic messages in convert_for_assignment. */
61 enum impl_conv {
62 ic_argpass,
63 ic_assign,
64 ic_init,
65 ic_return
68 /* The level of nesting inside "__alignof__". */
69 int in_alignof;
71 /* The level of nesting inside "sizeof". */
72 int in_sizeof;
74 /* The level of nesting inside "typeof". */
75 int in_typeof;
77 /* The argument of last parsed sizeof expression, only to be tested
78 if expr.original_code == SIZEOF_EXPR. */
79 tree c_last_sizeof_arg;
81 /* Nonzero if we might need to print a "missing braces around
82 initializer" message within this initializer. */
83 static int found_missing_braces;
85 static int require_constant_value;
86 static int require_constant_elements;
88 static bool null_pointer_constant_p (const_tree);
89 static tree qualify_type (tree, tree);
90 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
91 bool *);
92 static int comp_target_types (location_t, tree, tree);
93 static int function_types_compatible_p (const_tree, const_tree, bool *,
94 bool *);
95 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
96 static tree lookup_field (tree, tree);
97 static int convert_arguments (location_t, vec<location_t>, tree,
98 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
99 tree);
100 static tree pointer_diff (location_t, tree, tree);
101 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
102 enum impl_conv, bool, tree, tree, int);
103 static tree valid_compound_expr_initializer (tree, tree);
104 static void push_string (const char *);
105 static void push_member_name (tree);
106 static int spelling_length (void);
107 static char *print_spelling (char *);
108 static void warning_init (location_t, int, const char *);
109 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
110 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
111 bool, struct obstack *);
112 static void output_pending_init_elements (int, struct obstack *);
113 static int set_designator (location_t, int, struct obstack *);
114 static void push_range_stack (tree, struct obstack *);
115 static void add_pending_init (location_t, tree, tree, tree, bool,
116 struct obstack *);
117 static void set_nonincremental_init (struct obstack *);
118 static void set_nonincremental_init_from_string (tree, struct obstack *);
119 static tree find_init_member (tree, struct obstack *);
120 static void readonly_warning (tree, enum lvalue_use);
121 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
122 static void record_maybe_used_decl (tree);
123 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
125 /* Return true if EXP is a null pointer constant, false otherwise. */
127 static bool
128 null_pointer_constant_p (const_tree expr)
130 /* This should really operate on c_expr structures, but they aren't
131 yet available everywhere required. */
132 tree type = TREE_TYPE (expr);
133 return (TREE_CODE (expr) == INTEGER_CST
134 && !TREE_OVERFLOW (expr)
135 && integer_zerop (expr)
136 && (INTEGRAL_TYPE_P (type)
137 || (TREE_CODE (type) == POINTER_TYPE
138 && VOID_TYPE_P (TREE_TYPE (type))
139 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
142 /* EXPR may appear in an unevaluated part of an integer constant
143 expression, but not in an evaluated part. Wrap it in a
144 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
145 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
147 static tree
148 note_integer_operands (tree expr)
150 tree ret;
151 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
153 ret = copy_node (expr);
154 TREE_OVERFLOW (ret) = 1;
156 else
158 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
159 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
161 return ret;
164 /* Having checked whether EXPR may appear in an unevaluated part of an
165 integer constant expression and found that it may, remove any
166 C_MAYBE_CONST_EXPR noting this fact and return the resulting
167 expression. */
169 static inline tree
170 remove_c_maybe_const_expr (tree expr)
172 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
173 return C_MAYBE_CONST_EXPR_EXPR (expr);
174 else
175 return expr;
178 \f/* This is a cache to hold if two types are compatible or not. */
180 struct tagged_tu_seen_cache {
181 const struct tagged_tu_seen_cache * next;
182 const_tree t1;
183 const_tree t2;
184 /* The return value of tagged_types_tu_compatible_p if we had seen
185 these two types already. */
186 int val;
189 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
190 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
192 /* Do `exp = require_complete_type (exp);' to make sure exp
193 does not have an incomplete type. (That includes void types.) */
195 tree
196 require_complete_type (tree value)
198 tree type = TREE_TYPE (value);
200 if (error_operand_p (value))
201 return error_mark_node;
203 /* First, detect a valid value with a complete type. */
204 if (COMPLETE_TYPE_P (type))
205 return value;
207 c_incomplete_type_error (value, type);
208 return error_mark_node;
211 /* Print an error message for invalid use of an incomplete type.
212 VALUE is the expression that was used (or 0 if that isn't known)
213 and TYPE is the type that was invalid. */
215 void
216 c_incomplete_type_error (const_tree value, const_tree type)
218 /* Avoid duplicate error message. */
219 if (TREE_CODE (type) == ERROR_MARK)
220 return;
222 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
223 error ("%qD has an incomplete type %qT", value, type);
224 else
226 retry:
227 /* We must print an error message. Be clever about what it says. */
229 switch (TREE_CODE (type))
231 case RECORD_TYPE:
232 case UNION_TYPE:
233 case ENUMERAL_TYPE:
234 break;
236 case VOID_TYPE:
237 error ("invalid use of void expression");
238 return;
240 case ARRAY_TYPE:
241 if (TYPE_DOMAIN (type))
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
245 error ("invalid use of flexible array member");
246 return;
248 type = TREE_TYPE (type);
249 goto retry;
251 error ("invalid use of array with unspecified bounds");
252 return;
254 default:
255 gcc_unreachable ();
258 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
259 error ("invalid use of undefined type %qT", type);
260 else
261 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
262 error ("invalid use of incomplete typedef %qT", type);
266 /* Given a type, apply default promotions wrt unnamed function
267 arguments and return the new type. */
269 tree
270 c_type_promotes_to (tree type)
272 tree ret = NULL_TREE;
274 if (TYPE_MAIN_VARIANT (type) == float_type_node)
275 ret = double_type_node;
276 else if (c_promoting_integer_type_p (type))
278 /* Preserve unsignedness if not really getting any wider. */
279 if (TYPE_UNSIGNED (type)
280 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
281 ret = unsigned_type_node;
282 else
283 ret = integer_type_node;
286 if (ret != NULL_TREE)
287 return (TYPE_ATOMIC (type)
288 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
289 : ret);
291 return type;
294 /* Return true if between two named address spaces, whether there is a superset
295 named address space that encompasses both address spaces. If there is a
296 superset, return which address space is the superset. */
298 static bool
299 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
301 if (as1 == as2)
303 *common = as1;
304 return true;
306 else if (targetm.addr_space.subset_p (as1, as2))
308 *common = as2;
309 return true;
311 else if (targetm.addr_space.subset_p (as2, as1))
313 *common = as1;
314 return true;
316 else
317 return false;
320 /* Return a variant of TYPE which has all the type qualifiers of LIKE
321 as well as those of TYPE. */
323 static tree
324 qualify_type (tree type, tree like)
326 addr_space_t as_type = TYPE_ADDR_SPACE (type);
327 addr_space_t as_like = TYPE_ADDR_SPACE (like);
328 addr_space_t as_common;
330 /* If the two named address spaces are different, determine the common
331 superset address space. If there isn't one, raise an error. */
332 if (!addr_space_superset (as_type, as_like, &as_common))
334 as_common = as_type;
335 error ("%qT and %qT are in disjoint named address spaces",
336 type, like);
339 return c_build_qualified_type (type,
340 TYPE_QUALS_NO_ADDR_SPACE (type)
341 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
342 | ENCODE_QUAL_ADDR_SPACE (as_common));
345 /* Return true iff the given tree T is a variable length array. */
347 bool
348 c_vla_type_p (const_tree t)
350 if (TREE_CODE (t) == ARRAY_TYPE
351 && C_TYPE_VARIABLE_SIZE (t))
352 return true;
353 return false;
356 /* Return the composite type of two compatible types.
358 We assume that comptypes has already been done and returned
359 nonzero; if that isn't so, this may crash. In particular, we
360 assume that qualifiers match. */
362 tree
363 composite_type (tree t1, tree t2)
365 enum tree_code code1;
366 enum tree_code code2;
367 tree attributes;
369 /* Save time if the two types are the same. */
371 if (t1 == t2) return t1;
373 /* If one type is nonsense, use the other. */
374 if (t1 == error_mark_node)
375 return t2;
376 if (t2 == error_mark_node)
377 return t1;
379 code1 = TREE_CODE (t1);
380 code2 = TREE_CODE (t2);
382 /* Merge the attributes. */
383 attributes = targetm.merge_type_attributes (t1, t2);
385 /* If one is an enumerated type and the other is the compatible
386 integer type, the composite type might be either of the two
387 (DR#013 question 3). For consistency, use the enumerated type as
388 the composite type. */
390 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
391 return t1;
392 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
393 return t2;
395 gcc_assert (code1 == code2);
397 switch (code1)
399 case POINTER_TYPE:
400 /* For two pointers, do this recursively on the target type. */
402 tree pointed_to_1 = TREE_TYPE (t1);
403 tree pointed_to_2 = TREE_TYPE (t2);
404 tree target = composite_type (pointed_to_1, pointed_to_2);
405 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
406 t1 = build_type_attribute_variant (t1, attributes);
407 return qualify_type (t1, t2);
410 case ARRAY_TYPE:
412 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
413 int quals;
414 tree unqual_elt;
415 tree d1 = TYPE_DOMAIN (t1);
416 tree d2 = TYPE_DOMAIN (t2);
417 bool d1_variable, d2_variable;
418 bool d1_zero, d2_zero;
419 bool t1_complete, t2_complete;
421 /* We should not have any type quals on arrays at all. */
422 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
423 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
425 t1_complete = COMPLETE_TYPE_P (t1);
426 t2_complete = COMPLETE_TYPE_P (t2);
428 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
429 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
431 d1_variable = (!d1_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
434 d2_variable = (!d2_zero
435 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
436 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
437 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
438 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
440 /* Save space: see if the result is identical to one of the args. */
441 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
442 && (d2_variable || d2_zero || !d1_variable))
443 return build_type_attribute_variant (t1, attributes);
444 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
445 && (d1_variable || d1_zero || !d2_variable))
446 return build_type_attribute_variant (t2, attributes);
448 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
449 return build_type_attribute_variant (t1, attributes);
450 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
451 return build_type_attribute_variant (t2, attributes);
453 /* Merge the element types, and have a size if either arg has
454 one. We may have qualifiers on the element types. To set
455 up TYPE_MAIN_VARIANT correctly, we need to form the
456 composite of the unqualified types and add the qualifiers
457 back at the end. */
458 quals = TYPE_QUALS (strip_array_types (elt));
459 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
460 t1 = build_array_type (unqual_elt,
461 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
462 && (d2_variable
463 || d2_zero
464 || !d1_variable))
465 ? t1
466 : t2));
467 /* Ensure a composite type involving a zero-length array type
468 is a zero-length type not an incomplete type. */
469 if (d1_zero && d2_zero
470 && (t1_complete || t2_complete)
471 && !COMPLETE_TYPE_P (t1))
473 TYPE_SIZE (t1) = bitsize_zero_node;
474 TYPE_SIZE_UNIT (t1) = size_zero_node;
476 t1 = c_build_qualified_type (t1, quals);
477 return build_type_attribute_variant (t1, attributes);
480 case ENUMERAL_TYPE:
481 case RECORD_TYPE:
482 case UNION_TYPE:
483 if (attributes != NULL)
485 /* Try harder not to create a new aggregate type. */
486 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
487 return t1;
488 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
489 return t2;
491 return build_type_attribute_variant (t1, attributes);
493 case FUNCTION_TYPE:
494 /* Function types: prefer the one that specified arg types.
495 If both do, merge the arg types. Also merge the return types. */
497 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
498 tree p1 = TYPE_ARG_TYPES (t1);
499 tree p2 = TYPE_ARG_TYPES (t2);
500 int len;
501 tree newargs, n;
502 int i;
504 /* Save space: see if the result is identical to one of the args. */
505 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
506 return build_type_attribute_variant (t1, attributes);
507 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
508 return build_type_attribute_variant (t2, attributes);
510 /* Simple way if one arg fails to specify argument types. */
511 if (TYPE_ARG_TYPES (t1) == 0)
513 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
514 t1 = build_type_attribute_variant (t1, attributes);
515 return qualify_type (t1, t2);
517 if (TYPE_ARG_TYPES (t2) == 0)
519 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
520 t1 = build_type_attribute_variant (t1, attributes);
521 return qualify_type (t1, t2);
524 /* If both args specify argument types, we must merge the two
525 lists, argument by argument. */
527 len = list_length (p1);
528 newargs = 0;
530 for (i = 0; i < len; i++)
531 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
533 n = newargs;
535 for (; p1;
536 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
538 /* A null type means arg type is not specified.
539 Take whatever the other function type has. */
540 if (TREE_VALUE (p1) == 0)
542 TREE_VALUE (n) = TREE_VALUE (p2);
543 goto parm_done;
545 if (TREE_VALUE (p2) == 0)
547 TREE_VALUE (n) = TREE_VALUE (p1);
548 goto parm_done;
551 /* Given wait (union {union wait *u; int *i} *)
552 and wait (union wait *),
553 prefer union wait * as type of parm. */
554 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
555 && TREE_VALUE (p1) != TREE_VALUE (p2))
557 tree memb;
558 tree mv2 = TREE_VALUE (p2);
559 if (mv2 && mv2 != error_mark_node
560 && TREE_CODE (mv2) != ARRAY_TYPE)
561 mv2 = TYPE_MAIN_VARIANT (mv2);
562 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
563 memb; memb = DECL_CHAIN (memb))
565 tree mv3 = TREE_TYPE (memb);
566 if (mv3 && mv3 != error_mark_node
567 && TREE_CODE (mv3) != ARRAY_TYPE)
568 mv3 = TYPE_MAIN_VARIANT (mv3);
569 if (comptypes (mv3, mv2))
571 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
572 TREE_VALUE (p2));
573 pedwarn (input_location, OPT_Wpedantic,
574 "function types not truly compatible in ISO C");
575 goto parm_done;
579 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
580 && TREE_VALUE (p2) != TREE_VALUE (p1))
582 tree memb;
583 tree mv1 = TREE_VALUE (p1);
584 if (mv1 && mv1 != error_mark_node
585 && TREE_CODE (mv1) != ARRAY_TYPE)
586 mv1 = TYPE_MAIN_VARIANT (mv1);
587 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
588 memb; memb = DECL_CHAIN (memb))
590 tree mv3 = TREE_TYPE (memb);
591 if (mv3 && mv3 != error_mark_node
592 && TREE_CODE (mv3) != ARRAY_TYPE)
593 mv3 = TYPE_MAIN_VARIANT (mv3);
594 if (comptypes (mv3, mv1))
596 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
597 TREE_VALUE (p1));
598 pedwarn (input_location, OPT_Wpedantic,
599 "function types not truly compatible in ISO C");
600 goto parm_done;
604 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
605 parm_done: ;
608 t1 = build_function_type (valtype, newargs);
609 t1 = qualify_type (t1, t2);
610 /* ... falls through ... */
613 default:
614 return build_type_attribute_variant (t1, attributes);
619 /* Return the type of a conditional expression between pointers to
620 possibly differently qualified versions of compatible types.
622 We assume that comp_target_types has already been done and returned
623 nonzero; if that isn't so, this may crash. */
625 static tree
626 common_pointer_type (tree t1, tree t2)
628 tree attributes;
629 tree pointed_to_1, mv1;
630 tree pointed_to_2, mv2;
631 tree target;
632 unsigned target_quals;
633 addr_space_t as1, as2, as_common;
634 int quals1, quals2;
636 /* Save time if the two types are the same. */
638 if (t1 == t2) return t1;
640 /* If one type is nonsense, use the other. */
641 if (t1 == error_mark_node)
642 return t2;
643 if (t2 == error_mark_node)
644 return t1;
646 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
647 && TREE_CODE (t2) == POINTER_TYPE);
649 /* Merge the attributes. */
650 attributes = targetm.merge_type_attributes (t1, t2);
652 /* Find the composite type of the target types, and combine the
653 qualifiers of the two types' targets. Do not lose qualifiers on
654 array element types by taking the TYPE_MAIN_VARIANT. */
655 mv1 = pointed_to_1 = TREE_TYPE (t1);
656 mv2 = pointed_to_2 = TREE_TYPE (t2);
657 if (TREE_CODE (mv1) != ARRAY_TYPE)
658 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
659 if (TREE_CODE (mv2) != ARRAY_TYPE)
660 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
661 target = composite_type (mv1, mv2);
663 /* Strip array types to get correct qualifier for pointers to arrays */
664 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
665 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
667 /* For function types do not merge const qualifiers, but drop them
668 if used inconsistently. The middle-end uses these to mark const
669 and noreturn functions. */
670 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
671 target_quals = (quals1 & quals2);
672 else
673 target_quals = (quals1 | quals2);
675 /* If the two named address spaces are different, determine the common
676 superset address space. This is guaranteed to exist due to the
677 assumption that comp_target_type returned non-zero. */
678 as1 = TYPE_ADDR_SPACE (pointed_to_1);
679 as2 = TYPE_ADDR_SPACE (pointed_to_2);
680 if (!addr_space_superset (as1, as2, &as_common))
681 gcc_unreachable ();
683 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
685 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
686 return build_type_attribute_variant (t1, attributes);
689 /* Return the common type for two arithmetic types under the usual
690 arithmetic conversions. The default conversions have already been
691 applied, and enumerated types converted to their compatible integer
692 types. The resulting type is unqualified and has no attributes.
694 This is the type for the result of most arithmetic operations
695 if the operands have the given two types. */
697 static tree
698 c_common_type (tree t1, tree t2)
700 enum tree_code code1;
701 enum tree_code code2;
703 /* If one type is nonsense, use the other. */
704 if (t1 == error_mark_node)
705 return t2;
706 if (t2 == error_mark_node)
707 return t1;
709 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
710 t1 = TYPE_MAIN_VARIANT (t1);
712 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
713 t2 = TYPE_MAIN_VARIANT (t2);
715 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
716 t1 = build_type_attribute_variant (t1, NULL_TREE);
718 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
719 t2 = build_type_attribute_variant (t2, NULL_TREE);
721 /* Save time if the two types are the same. */
723 if (t1 == t2) return t1;
725 code1 = TREE_CODE (t1);
726 code2 = TREE_CODE (t2);
728 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
729 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
730 || code1 == INTEGER_TYPE);
731 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
732 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
733 || code2 == INTEGER_TYPE);
735 /* When one operand is a decimal float type, the other operand cannot be
736 a generic float type or a complex type. We also disallow vector types
737 here. */
738 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
739 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
741 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
743 error ("can%'t mix operands of decimal float and vector types");
744 return error_mark_node;
746 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
748 error ("can%'t mix operands of decimal float and complex types");
749 return error_mark_node;
751 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
753 error ("can%'t mix operands of decimal float and other float types");
754 return error_mark_node;
758 /* If one type is a vector type, return that type. (How the usual
759 arithmetic conversions apply to the vector types extension is not
760 precisely specified.) */
761 if (code1 == VECTOR_TYPE)
762 return t1;
764 if (code2 == VECTOR_TYPE)
765 return t2;
767 /* If one type is complex, form the common type of the non-complex
768 components, then make that complex. Use T1 or T2 if it is the
769 required type. */
770 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
772 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
773 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
774 tree subtype = c_common_type (subtype1, subtype2);
776 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
777 return t1;
778 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
779 return t2;
780 else
781 return build_complex_type (subtype);
784 /* If only one is real, use it as the result. */
786 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
787 return t1;
789 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
790 return t2;
792 /* If both are real and either are decimal floating point types, use
793 the decimal floating point type with the greater precision. */
795 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
797 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
798 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
799 return dfloat128_type_node;
800 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
801 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
802 return dfloat64_type_node;
803 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
804 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
805 return dfloat32_type_node;
808 /* Deal with fixed-point types. */
809 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
811 unsigned int unsignedp = 0, satp = 0;
812 machine_mode m1, m2;
813 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
815 m1 = TYPE_MODE (t1);
816 m2 = TYPE_MODE (t2);
818 /* If one input type is saturating, the result type is saturating. */
819 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
820 satp = 1;
822 /* If both fixed-point types are unsigned, the result type is unsigned.
823 When mixing fixed-point and integer types, follow the sign of the
824 fixed-point type.
825 Otherwise, the result type is signed. */
826 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
827 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
828 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
829 && TYPE_UNSIGNED (t1))
830 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
831 && TYPE_UNSIGNED (t2)))
832 unsignedp = 1;
834 /* The result type is signed. */
835 if (unsignedp == 0)
837 /* If the input type is unsigned, we need to convert to the
838 signed type. */
839 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
841 enum mode_class mclass = (enum mode_class) 0;
842 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
843 mclass = MODE_FRACT;
844 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
845 mclass = MODE_ACCUM;
846 else
847 gcc_unreachable ();
848 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
850 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
852 enum mode_class mclass = (enum mode_class) 0;
853 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
854 mclass = MODE_FRACT;
855 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
856 mclass = MODE_ACCUM;
857 else
858 gcc_unreachable ();
859 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
863 if (code1 == FIXED_POINT_TYPE)
865 fbit1 = GET_MODE_FBIT (m1);
866 ibit1 = GET_MODE_IBIT (m1);
868 else
870 fbit1 = 0;
871 /* Signed integers need to subtract one sign bit. */
872 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
875 if (code2 == FIXED_POINT_TYPE)
877 fbit2 = GET_MODE_FBIT (m2);
878 ibit2 = GET_MODE_IBIT (m2);
880 else
882 fbit2 = 0;
883 /* Signed integers need to subtract one sign bit. */
884 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
887 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
888 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
889 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
890 satp);
893 /* Both real or both integers; use the one with greater precision. */
895 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
896 return t1;
897 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
898 return t2;
900 /* Same precision. Prefer long longs to longs to ints when the
901 same precision, following the C99 rules on integer type rank
902 (which are equivalent to the C90 rules for C90 types). */
904 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
905 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
906 return long_long_unsigned_type_node;
908 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
911 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
912 return long_long_unsigned_type_node;
913 else
914 return long_long_integer_type_node;
917 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
918 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
919 return long_unsigned_type_node;
921 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
924 /* But preserve unsignedness from the other type,
925 since long cannot hold all the values of an unsigned int. */
926 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
927 return long_unsigned_type_node;
928 else
929 return long_integer_type_node;
932 /* Likewise, prefer long double to double even if same size. */
933 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
934 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
935 return long_double_type_node;
937 /* Likewise, prefer double to float even if same size.
938 We got a couple of embedded targets with 32 bit doubles, and the
939 pdp11 might have 64 bit floats. */
940 if (TYPE_MAIN_VARIANT (t1) == double_type_node
941 || TYPE_MAIN_VARIANT (t2) == double_type_node)
942 return double_type_node;
944 /* Otherwise prefer the unsigned one. */
946 if (TYPE_UNSIGNED (t1))
947 return t1;
948 else
949 return t2;
952 /* Wrapper around c_common_type that is used by c-common.c and other
953 front end optimizations that remove promotions. ENUMERAL_TYPEs
954 are allowed here and are converted to their compatible integer types.
955 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
956 preferably a non-Boolean type as the common type. */
957 tree
958 common_type (tree t1, tree t2)
960 if (TREE_CODE (t1) == ENUMERAL_TYPE)
961 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
962 if (TREE_CODE (t2) == ENUMERAL_TYPE)
963 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
965 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
966 if (TREE_CODE (t1) == BOOLEAN_TYPE
967 && TREE_CODE (t2) == BOOLEAN_TYPE)
968 return boolean_type_node;
970 /* If either type is BOOLEAN_TYPE, then return the other. */
971 if (TREE_CODE (t1) == BOOLEAN_TYPE)
972 return t2;
973 if (TREE_CODE (t2) == BOOLEAN_TYPE)
974 return t1;
976 return c_common_type (t1, t2);
979 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
980 or various other operations. Return 2 if they are compatible
981 but a warning may be needed if you use them together. */
984 comptypes (tree type1, tree type2)
986 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
987 int val;
989 val = comptypes_internal (type1, type2, NULL, NULL);
990 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
992 return val;
995 /* Like comptypes, but if it returns non-zero because enum and int are
996 compatible, it sets *ENUM_AND_INT_P to true. */
998 static int
999 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1001 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1002 int val;
1004 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1005 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1007 return val;
1010 /* Like comptypes, but if it returns nonzero for different types, it
1011 sets *DIFFERENT_TYPES_P to true. */
1014 comptypes_check_different_types (tree type1, tree type2,
1015 bool *different_types_p)
1017 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1018 int val;
1020 val = comptypes_internal (type1, type2, NULL, different_types_p);
1021 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1023 return val;
1026 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1027 or various other operations. Return 2 if they are compatible
1028 but a warning may be needed if you use them together. If
1029 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1030 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1031 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1032 NULL, and the types are compatible but different enough not to be
1033 permitted in C11 typedef redeclarations, then this sets
1034 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1035 false, but may or may not be set if the types are incompatible.
1036 This differs from comptypes, in that we don't free the seen
1037 types. */
1039 static int
1040 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1041 bool *different_types_p)
1043 const_tree t1 = type1;
1044 const_tree t2 = type2;
1045 int attrval, val;
1047 /* Suppress errors caused by previously reported errors. */
1049 if (t1 == t2 || !t1 || !t2
1050 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1051 return 1;
1053 /* Enumerated types are compatible with integer types, but this is
1054 not transitive: two enumerated types in the same translation unit
1055 are compatible with each other only if they are the same type. */
1057 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1059 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1060 if (TREE_CODE (t2) != VOID_TYPE)
1062 if (enum_and_int_p != NULL)
1063 *enum_and_int_p = true;
1064 if (different_types_p != NULL)
1065 *different_types_p = true;
1068 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1070 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1071 if (TREE_CODE (t1) != VOID_TYPE)
1073 if (enum_and_int_p != NULL)
1074 *enum_and_int_p = true;
1075 if (different_types_p != NULL)
1076 *different_types_p = true;
1080 if (t1 == t2)
1081 return 1;
1083 /* Different classes of types can't be compatible. */
1085 if (TREE_CODE (t1) != TREE_CODE (t2))
1086 return 0;
1088 /* Qualifiers must match. C99 6.7.3p9 */
1090 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1091 return 0;
1093 /* Allow for two different type nodes which have essentially the same
1094 definition. Note that we already checked for equality of the type
1095 qualifiers (just above). */
1097 if (TREE_CODE (t1) != ARRAY_TYPE
1098 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1099 return 1;
1101 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1102 if (!(attrval = comp_type_attributes (t1, t2)))
1103 return 0;
1105 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1106 val = 0;
1108 switch (TREE_CODE (t1))
1110 case POINTER_TYPE:
1111 /* Do not remove mode or aliasing information. */
1112 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1113 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1114 break;
1115 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1116 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1117 enum_and_int_p, different_types_p));
1118 break;
1120 case FUNCTION_TYPE:
1121 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1122 different_types_p);
1123 break;
1125 case ARRAY_TYPE:
1127 tree d1 = TYPE_DOMAIN (t1);
1128 tree d2 = TYPE_DOMAIN (t2);
1129 bool d1_variable, d2_variable;
1130 bool d1_zero, d2_zero;
1131 val = 1;
1133 /* Target types must match incl. qualifiers. */
1134 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1135 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1136 enum_and_int_p,
1137 different_types_p)))
1138 return 0;
1140 if (different_types_p != NULL
1141 && (d1 == 0) != (d2 == 0))
1142 *different_types_p = true;
1143 /* Sizes must match unless one is missing or variable. */
1144 if (d1 == 0 || d2 == 0 || d1 == d2)
1145 break;
1147 d1_zero = !TYPE_MAX_VALUE (d1);
1148 d2_zero = !TYPE_MAX_VALUE (d2);
1150 d1_variable = (!d1_zero
1151 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1152 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1153 d2_variable = (!d2_zero
1154 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1155 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1156 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1157 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1159 if (different_types_p != NULL
1160 && d1_variable != d2_variable)
1161 *different_types_p = true;
1162 if (d1_variable || d2_variable)
1163 break;
1164 if (d1_zero && d2_zero)
1165 break;
1166 if (d1_zero || d2_zero
1167 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1168 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1169 val = 0;
1171 break;
1174 case ENUMERAL_TYPE:
1175 case RECORD_TYPE:
1176 case UNION_TYPE:
1177 if (val != 1 && !same_translation_unit_p (t1, t2))
1179 tree a1 = TYPE_ATTRIBUTES (t1);
1180 tree a2 = TYPE_ATTRIBUTES (t2);
1182 if (! attribute_list_contained (a1, a2)
1183 && ! attribute_list_contained (a2, a1))
1184 break;
1186 if (attrval != 2)
1187 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1188 different_types_p);
1189 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1190 different_types_p);
1192 break;
1194 case VECTOR_TYPE:
1195 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1196 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1197 enum_and_int_p, different_types_p));
1198 break;
1200 default:
1201 break;
1203 return attrval == 2 && val == 1 ? 2 : val;
1206 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1207 their qualifiers, except for named address spaces. If the pointers point to
1208 different named addresses, then we must determine if one address space is a
1209 subset of the other. */
1211 static int
1212 comp_target_types (location_t location, tree ttl, tree ttr)
1214 int val;
1215 int val_ped;
1216 tree mvl = TREE_TYPE (ttl);
1217 tree mvr = TREE_TYPE (ttr);
1218 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1219 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1220 addr_space_t as_common;
1221 bool enum_and_int_p;
1223 /* Fail if pointers point to incompatible address spaces. */
1224 if (!addr_space_superset (asl, asr, &as_common))
1225 return 0;
1227 /* For pedantic record result of comptypes on arrays before losing
1228 qualifiers on the element type below. */
1229 val_ped = 1;
1231 if (TREE_CODE (mvl) == ARRAY_TYPE
1232 && TREE_CODE (mvr) == ARRAY_TYPE)
1233 val_ped = comptypes (mvl, mvr);
1235 /* Qualifiers on element types of array types that are
1236 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1238 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1239 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1240 : TYPE_MAIN_VARIANT (mvl));
1242 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1243 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1244 : TYPE_MAIN_VARIANT (mvr));
1246 enum_and_int_p = false;
1247 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1249 if (val == 1 && val_ped != 1)
1250 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1251 "are incompatible in ISO C");
1253 if (val == 2)
1254 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1256 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1257 warning_at (location, OPT_Wc___compat,
1258 "pointer target types incompatible in C++");
1260 return val;
1263 /* Subroutines of `comptypes'. */
1265 /* Determine whether two trees derive from the same translation unit.
1266 If the CONTEXT chain ends in a null, that tree's context is still
1267 being parsed, so if two trees have context chains ending in null,
1268 they're in the same translation unit. */
1270 same_translation_unit_p (const_tree t1, const_tree t2)
1272 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1273 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1275 case tcc_declaration:
1276 t1 = DECL_CONTEXT (t1); break;
1277 case tcc_type:
1278 t1 = TYPE_CONTEXT (t1); break;
1279 case tcc_exceptional:
1280 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1281 default: gcc_unreachable ();
1284 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1285 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1287 case tcc_declaration:
1288 t2 = DECL_CONTEXT (t2); break;
1289 case tcc_type:
1290 t2 = TYPE_CONTEXT (t2); break;
1291 case tcc_exceptional:
1292 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1293 default: gcc_unreachable ();
1296 return t1 == t2;
1299 /* Allocate the seen two types, assuming that they are compatible. */
1301 static struct tagged_tu_seen_cache *
1302 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1304 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1305 tu->next = tagged_tu_seen_base;
1306 tu->t1 = t1;
1307 tu->t2 = t2;
1309 tagged_tu_seen_base = tu;
1311 /* The C standard says that two structures in different translation
1312 units are compatible with each other only if the types of their
1313 fields are compatible (among other things). We assume that they
1314 are compatible until proven otherwise when building the cache.
1315 An example where this can occur is:
1316 struct a
1318 struct a *next;
1320 If we are comparing this against a similar struct in another TU,
1321 and did not assume they were compatible, we end up with an infinite
1322 loop. */
1323 tu->val = 1;
1324 return tu;
1327 /* Free the seen types until we get to TU_TIL. */
1329 static void
1330 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1332 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1333 while (tu != tu_til)
1335 const struct tagged_tu_seen_cache *const tu1
1336 = (const struct tagged_tu_seen_cache *) tu;
1337 tu = tu1->next;
1338 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1340 tagged_tu_seen_base = tu_til;
1343 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1344 compatible. If the two types are not the same (which has been
1345 checked earlier), this can only happen when multiple translation
1346 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1347 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1348 comptypes_internal. */
1350 static int
1351 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1352 bool *enum_and_int_p, bool *different_types_p)
1354 tree s1, s2;
1355 bool needs_warning = false;
1357 /* We have to verify that the tags of the types are the same. This
1358 is harder than it looks because this may be a typedef, so we have
1359 to go look at the original type. It may even be a typedef of a
1360 typedef...
1361 In the case of compiler-created builtin structs the TYPE_DECL
1362 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1363 while (TYPE_NAME (t1)
1364 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1365 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1366 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1368 while (TYPE_NAME (t2)
1369 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1370 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1371 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1373 /* C90 didn't have the requirement that the two tags be the same. */
1374 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1375 return 0;
1377 /* C90 didn't say what happened if one or both of the types were
1378 incomplete; we choose to follow C99 rules here, which is that they
1379 are compatible. */
1380 if (TYPE_SIZE (t1) == NULL
1381 || TYPE_SIZE (t2) == NULL)
1382 return 1;
1385 const struct tagged_tu_seen_cache * tts_i;
1386 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1387 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1388 return tts_i->val;
1391 switch (TREE_CODE (t1))
1393 case ENUMERAL_TYPE:
1395 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1396 /* Speed up the case where the type values are in the same order. */
1397 tree tv1 = TYPE_VALUES (t1);
1398 tree tv2 = TYPE_VALUES (t2);
1400 if (tv1 == tv2)
1402 return 1;
1405 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1407 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1408 break;
1409 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1411 tu->val = 0;
1412 return 0;
1416 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1418 return 1;
1420 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1422 tu->val = 0;
1423 return 0;
1426 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1428 tu->val = 0;
1429 return 0;
1432 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1434 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1435 if (s2 == NULL
1436 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1438 tu->val = 0;
1439 return 0;
1442 return 1;
1445 case UNION_TYPE:
1447 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1448 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1450 tu->val = 0;
1451 return 0;
1454 /* Speed up the common case where the fields are in the same order. */
1455 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1456 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1458 int result;
1460 if (DECL_NAME (s1) != DECL_NAME (s2))
1461 break;
1462 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1463 enum_and_int_p, different_types_p);
1465 if (result != 1 && !DECL_NAME (s1))
1466 break;
1467 if (result == 0)
1469 tu->val = 0;
1470 return 0;
1472 if (result == 2)
1473 needs_warning = true;
1475 if (TREE_CODE (s1) == FIELD_DECL
1476 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1477 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1479 tu->val = 0;
1480 return 0;
1483 if (!s1 && !s2)
1485 tu->val = needs_warning ? 2 : 1;
1486 return tu->val;
1489 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1491 bool ok = false;
1493 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1494 if (DECL_NAME (s1) == DECL_NAME (s2))
1496 int result;
1498 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1499 enum_and_int_p,
1500 different_types_p);
1502 if (result != 1 && !DECL_NAME (s1))
1503 continue;
1504 if (result == 0)
1506 tu->val = 0;
1507 return 0;
1509 if (result == 2)
1510 needs_warning = true;
1512 if (TREE_CODE (s1) == FIELD_DECL
1513 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1514 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1515 break;
1517 ok = true;
1518 break;
1520 if (!ok)
1522 tu->val = 0;
1523 return 0;
1526 tu->val = needs_warning ? 2 : 10;
1527 return tu->val;
1530 case RECORD_TYPE:
1532 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1534 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1535 s1 && s2;
1536 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1538 int result;
1539 if (TREE_CODE (s1) != TREE_CODE (s2)
1540 || DECL_NAME (s1) != DECL_NAME (s2))
1541 break;
1542 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1543 enum_and_int_p, different_types_p);
1544 if (result == 0)
1545 break;
1546 if (result == 2)
1547 needs_warning = true;
1549 if (TREE_CODE (s1) == FIELD_DECL
1550 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1551 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1552 break;
1554 if (s1 && s2)
1555 tu->val = 0;
1556 else
1557 tu->val = needs_warning ? 2 : 1;
1558 return tu->val;
1561 default:
1562 gcc_unreachable ();
1566 /* Return 1 if two function types F1 and F2 are compatible.
1567 If either type specifies no argument types,
1568 the other must specify a fixed number of self-promoting arg types.
1569 Otherwise, if one type specifies only the number of arguments,
1570 the other must specify that number of self-promoting arg types.
1571 Otherwise, the argument types must match.
1572 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1574 static int
1575 function_types_compatible_p (const_tree f1, const_tree f2,
1576 bool *enum_and_int_p, bool *different_types_p)
1578 tree args1, args2;
1579 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1580 int val = 1;
1581 int val1;
1582 tree ret1, ret2;
1584 ret1 = TREE_TYPE (f1);
1585 ret2 = TREE_TYPE (f2);
1587 /* 'volatile' qualifiers on a function's return type used to mean
1588 the function is noreturn. */
1589 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1590 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1591 if (TYPE_VOLATILE (ret1))
1592 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1593 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1594 if (TYPE_VOLATILE (ret2))
1595 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1596 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1597 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1598 if (val == 0)
1599 return 0;
1601 args1 = TYPE_ARG_TYPES (f1);
1602 args2 = TYPE_ARG_TYPES (f2);
1604 if (different_types_p != NULL
1605 && (args1 == 0) != (args2 == 0))
1606 *different_types_p = true;
1608 /* An unspecified parmlist matches any specified parmlist
1609 whose argument types don't need default promotions. */
1611 if (args1 == 0)
1613 if (!self_promoting_args_p (args2))
1614 return 0;
1615 /* If one of these types comes from a non-prototype fn definition,
1616 compare that with the other type's arglist.
1617 If they don't match, ask for a warning (but no error). */
1618 if (TYPE_ACTUAL_ARG_TYPES (f1)
1619 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1620 enum_and_int_p, different_types_p))
1621 val = 2;
1622 return val;
1624 if (args2 == 0)
1626 if (!self_promoting_args_p (args1))
1627 return 0;
1628 if (TYPE_ACTUAL_ARG_TYPES (f2)
1629 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1630 enum_and_int_p, different_types_p))
1631 val = 2;
1632 return val;
1635 /* Both types have argument lists: compare them and propagate results. */
1636 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1637 different_types_p);
1638 return val1 != 1 ? val1 : val;
1641 /* Check two lists of types for compatibility, returning 0 for
1642 incompatible, 1 for compatible, or 2 for compatible with
1643 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1644 comptypes_internal. */
1646 static int
1647 type_lists_compatible_p (const_tree args1, const_tree args2,
1648 bool *enum_and_int_p, bool *different_types_p)
1650 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1651 int val = 1;
1652 int newval = 0;
1654 while (1)
1656 tree a1, mv1, a2, mv2;
1657 if (args1 == 0 && args2 == 0)
1658 return val;
1659 /* If one list is shorter than the other,
1660 they fail to match. */
1661 if (args1 == 0 || args2 == 0)
1662 return 0;
1663 mv1 = a1 = TREE_VALUE (args1);
1664 mv2 = a2 = TREE_VALUE (args2);
1665 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1666 mv1 = (TYPE_ATOMIC (mv1)
1667 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1668 TYPE_QUAL_ATOMIC)
1669 : TYPE_MAIN_VARIANT (mv1));
1670 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1671 mv2 = (TYPE_ATOMIC (mv2)
1672 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1673 TYPE_QUAL_ATOMIC)
1674 : TYPE_MAIN_VARIANT (mv2));
1675 /* A null pointer instead of a type
1676 means there is supposed to be an argument
1677 but nothing is specified about what type it has.
1678 So match anything that self-promotes. */
1679 if (different_types_p != NULL
1680 && (a1 == 0) != (a2 == 0))
1681 *different_types_p = true;
1682 if (a1 == 0)
1684 if (c_type_promotes_to (a2) != a2)
1685 return 0;
1687 else if (a2 == 0)
1689 if (c_type_promotes_to (a1) != a1)
1690 return 0;
1692 /* If one of the lists has an error marker, ignore this arg. */
1693 else if (TREE_CODE (a1) == ERROR_MARK
1694 || TREE_CODE (a2) == ERROR_MARK)
1696 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1697 different_types_p)))
1699 if (different_types_p != NULL)
1700 *different_types_p = true;
1701 /* Allow wait (union {union wait *u; int *i} *)
1702 and wait (union wait *) to be compatible. */
1703 if (TREE_CODE (a1) == UNION_TYPE
1704 && (TYPE_NAME (a1) == 0
1705 || TYPE_TRANSPARENT_AGGR (a1))
1706 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1707 && tree_int_cst_equal (TYPE_SIZE (a1),
1708 TYPE_SIZE (a2)))
1710 tree memb;
1711 for (memb = TYPE_FIELDS (a1);
1712 memb; memb = DECL_CHAIN (memb))
1714 tree mv3 = TREE_TYPE (memb);
1715 if (mv3 && mv3 != error_mark_node
1716 && TREE_CODE (mv3) != ARRAY_TYPE)
1717 mv3 = (TYPE_ATOMIC (mv3)
1718 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1719 TYPE_QUAL_ATOMIC)
1720 : TYPE_MAIN_VARIANT (mv3));
1721 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1722 different_types_p))
1723 break;
1725 if (memb == 0)
1726 return 0;
1728 else if (TREE_CODE (a2) == UNION_TYPE
1729 && (TYPE_NAME (a2) == 0
1730 || TYPE_TRANSPARENT_AGGR (a2))
1731 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1732 && tree_int_cst_equal (TYPE_SIZE (a2),
1733 TYPE_SIZE (a1)))
1735 tree memb;
1736 for (memb = TYPE_FIELDS (a2);
1737 memb; memb = DECL_CHAIN (memb))
1739 tree mv3 = TREE_TYPE (memb);
1740 if (mv3 && mv3 != error_mark_node
1741 && TREE_CODE (mv3) != ARRAY_TYPE)
1742 mv3 = (TYPE_ATOMIC (mv3)
1743 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1744 TYPE_QUAL_ATOMIC)
1745 : TYPE_MAIN_VARIANT (mv3));
1746 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1747 different_types_p))
1748 break;
1750 if (memb == 0)
1751 return 0;
1753 else
1754 return 0;
1757 /* comptypes said ok, but record if it said to warn. */
1758 if (newval > val)
1759 val = newval;
1761 args1 = TREE_CHAIN (args1);
1762 args2 = TREE_CHAIN (args2);
1766 /* Compute the size to increment a pointer by. When a function type or void
1767 type or incomplete type is passed, size_one_node is returned.
1768 This function does not emit any diagnostics; the caller is responsible
1769 for that. */
1771 static tree
1772 c_size_in_bytes (const_tree type)
1774 enum tree_code code = TREE_CODE (type);
1776 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1777 || !COMPLETE_TYPE_P (type))
1778 return size_one_node;
1780 /* Convert in case a char is more than one unit. */
1781 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1782 size_int (TYPE_PRECISION (char_type_node)
1783 / BITS_PER_UNIT));
1786 /* Return either DECL or its known constant value (if it has one). */
1788 tree
1789 decl_constant_value (tree decl)
1791 if (/* Don't change a variable array bound or initial value to a constant
1792 in a place where a variable is invalid. Note that DECL_INITIAL
1793 isn't valid for a PARM_DECL. */
1794 current_function_decl != 0
1795 && TREE_CODE (decl) != PARM_DECL
1796 && !TREE_THIS_VOLATILE (decl)
1797 && TREE_READONLY (decl)
1798 && DECL_INITIAL (decl) != 0
1799 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1800 /* This is invalid if initial value is not constant.
1801 If it has either a function call, a memory reference,
1802 or a variable, then re-evaluating it could give different results. */
1803 && TREE_CONSTANT (DECL_INITIAL (decl))
1804 /* Check for cases where this is sub-optimal, even though valid. */
1805 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1806 return DECL_INITIAL (decl);
1807 return decl;
1810 /* Convert the array expression EXP to a pointer. */
1811 static tree
1812 array_to_pointer_conversion (location_t loc, tree exp)
1814 tree orig_exp = exp;
1815 tree type = TREE_TYPE (exp);
1816 tree adr;
1817 tree restype = TREE_TYPE (type);
1818 tree ptrtype;
1820 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1822 STRIP_TYPE_NOPS (exp);
1824 if (TREE_NO_WARNING (orig_exp))
1825 TREE_NO_WARNING (exp) = 1;
1827 ptrtype = build_pointer_type (restype);
1829 if (INDIRECT_REF_P (exp))
1830 return convert (ptrtype, TREE_OPERAND (exp, 0));
1832 /* In C++ array compound literals are temporary objects unless they are
1833 const or appear in namespace scope, so they are destroyed too soon
1834 to use them for much of anything (c++/53220). */
1835 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1837 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1838 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1839 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1840 "converting an array compound literal to a pointer "
1841 "is ill-formed in C++");
1844 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1845 return convert (ptrtype, adr);
1848 /* Convert the function expression EXP to a pointer. */
1849 static tree
1850 function_to_pointer_conversion (location_t loc, tree exp)
1852 tree orig_exp = exp;
1854 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1856 STRIP_TYPE_NOPS (exp);
1858 if (TREE_NO_WARNING (orig_exp))
1859 TREE_NO_WARNING (exp) = 1;
1861 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1864 /* Mark EXP as read, not just set, for set but not used -Wunused
1865 warning purposes. */
1867 void
1868 mark_exp_read (tree exp)
1870 switch (TREE_CODE (exp))
1872 case VAR_DECL:
1873 case PARM_DECL:
1874 DECL_READ_P (exp) = 1;
1875 break;
1876 case ARRAY_REF:
1877 case COMPONENT_REF:
1878 case MODIFY_EXPR:
1879 case REALPART_EXPR:
1880 case IMAGPART_EXPR:
1881 CASE_CONVERT:
1882 case ADDR_EXPR:
1883 mark_exp_read (TREE_OPERAND (exp, 0));
1884 break;
1885 case COMPOUND_EXPR:
1886 case C_MAYBE_CONST_EXPR:
1887 mark_exp_read (TREE_OPERAND (exp, 1));
1888 break;
1889 default:
1890 break;
1894 /* Perform the default conversion of arrays and functions to pointers.
1895 Return the result of converting EXP. For any other expression, just
1896 return EXP.
1898 LOC is the location of the expression. */
1900 struct c_expr
1901 default_function_array_conversion (location_t loc, struct c_expr exp)
1903 tree orig_exp = exp.value;
1904 tree type = TREE_TYPE (exp.value);
1905 enum tree_code code = TREE_CODE (type);
1907 switch (code)
1909 case ARRAY_TYPE:
1911 bool not_lvalue = false;
1912 bool lvalue_array_p;
1914 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1915 || CONVERT_EXPR_P (exp.value))
1916 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1918 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1919 not_lvalue = true;
1920 exp.value = TREE_OPERAND (exp.value, 0);
1923 if (TREE_NO_WARNING (orig_exp))
1924 TREE_NO_WARNING (exp.value) = 1;
1926 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1927 if (!flag_isoc99 && !lvalue_array_p)
1929 /* Before C99, non-lvalue arrays do not decay to pointers.
1930 Normally, using such an array would be invalid; but it can
1931 be used correctly inside sizeof or as a statement expression.
1932 Thus, do not give an error here; an error will result later. */
1933 return exp;
1936 exp.value = array_to_pointer_conversion (loc, exp.value);
1938 break;
1939 case FUNCTION_TYPE:
1940 exp.value = function_to_pointer_conversion (loc, exp.value);
1941 break;
1942 default:
1943 break;
1946 return exp;
1949 struct c_expr
1950 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1952 mark_exp_read (exp.value);
1953 return default_function_array_conversion (loc, exp);
1956 /* Return whether EXPR should be treated as an atomic lvalue for the
1957 purposes of load and store handling. */
1959 static bool
1960 really_atomic_lvalue (tree expr)
1962 if (error_operand_p (expr))
1963 return false;
1964 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1965 return false;
1966 if (!lvalue_p (expr))
1967 return false;
1969 /* Ignore _Atomic on register variables, since their addresses can't
1970 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1971 sequences wouldn't work. Ignore _Atomic on structures containing
1972 bit-fields, since accessing elements of atomic structures or
1973 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1974 it's undefined at translation time or execution time, and the
1975 normal atomic sequences again wouldn't work. */
1976 while (handled_component_p (expr))
1978 if (TREE_CODE (expr) == COMPONENT_REF
1979 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1980 return false;
1981 expr = TREE_OPERAND (expr, 0);
1983 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1984 return false;
1985 return true;
1988 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1989 including converting functions and arrays to pointers if CONVERT_P.
1990 If READ_P, also mark the expression as having been read. */
1992 struct c_expr
1993 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1994 bool convert_p, bool read_p)
1996 if (read_p)
1997 mark_exp_read (exp.value);
1998 if (convert_p)
1999 exp = default_function_array_conversion (loc, exp);
2000 if (really_atomic_lvalue (exp.value))
2002 vec<tree, va_gc> *params;
2003 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2004 tree expr_type = TREE_TYPE (exp.value);
2005 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2006 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2008 gcc_assert (TYPE_ATOMIC (expr_type));
2010 /* Expansion of a generic atomic load may require an addition
2011 element, so allocate enough to prevent a resize. */
2012 vec_alloc (params, 4);
2014 /* Remove the qualifiers for the rest of the expressions and
2015 create the VAL temp variable to hold the RHS. */
2016 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2017 tmp = create_tmp_var_raw (nonatomic_type);
2018 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2019 TREE_ADDRESSABLE (tmp) = 1;
2020 TREE_NO_WARNING (tmp) = 1;
2022 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2023 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2024 params->quick_push (expr_addr);
2025 params->quick_push (tmp_addr);
2026 params->quick_push (seq_cst);
2027 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2029 /* EXPR is always read. */
2030 mark_exp_read (exp.value);
2032 /* Return tmp which contains the value loaded. */
2033 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2034 NULL_TREE, NULL_TREE);
2036 return exp;
2039 /* EXP is an expression of integer type. Apply the integer promotions
2040 to it and return the promoted value. */
2042 tree
2043 perform_integral_promotions (tree exp)
2045 tree type = TREE_TYPE (exp);
2046 enum tree_code code = TREE_CODE (type);
2048 gcc_assert (INTEGRAL_TYPE_P (type));
2050 /* Normally convert enums to int,
2051 but convert wide enums to something wider. */
2052 if (code == ENUMERAL_TYPE)
2054 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2055 TYPE_PRECISION (integer_type_node)),
2056 ((TYPE_PRECISION (type)
2057 >= TYPE_PRECISION (integer_type_node))
2058 && TYPE_UNSIGNED (type)));
2060 return convert (type, exp);
2063 /* ??? This should no longer be needed now bit-fields have their
2064 proper types. */
2065 if (TREE_CODE (exp) == COMPONENT_REF
2066 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2067 /* If it's thinner than an int, promote it like a
2068 c_promoting_integer_type_p, otherwise leave it alone. */
2069 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2070 TYPE_PRECISION (integer_type_node)))
2071 return convert (integer_type_node, exp);
2073 if (c_promoting_integer_type_p (type))
2075 /* Preserve unsignedness if not really getting any wider. */
2076 if (TYPE_UNSIGNED (type)
2077 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2078 return convert (unsigned_type_node, exp);
2080 return convert (integer_type_node, exp);
2083 return exp;
2087 /* Perform default promotions for C data used in expressions.
2088 Enumeral types or short or char are converted to int.
2089 In addition, manifest constants symbols are replaced by their values. */
2091 tree
2092 default_conversion (tree exp)
2094 tree orig_exp;
2095 tree type = TREE_TYPE (exp);
2096 enum tree_code code = TREE_CODE (type);
2097 tree promoted_type;
2099 mark_exp_read (exp);
2101 /* Functions and arrays have been converted during parsing. */
2102 gcc_assert (code != FUNCTION_TYPE);
2103 if (code == ARRAY_TYPE)
2104 return exp;
2106 /* Constants can be used directly unless they're not loadable. */
2107 if (TREE_CODE (exp) == CONST_DECL)
2108 exp = DECL_INITIAL (exp);
2110 /* Strip no-op conversions. */
2111 orig_exp = exp;
2112 STRIP_TYPE_NOPS (exp);
2114 if (TREE_NO_WARNING (orig_exp))
2115 TREE_NO_WARNING (exp) = 1;
2117 if (code == VOID_TYPE)
2119 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2120 "void value not ignored as it ought to be");
2121 return error_mark_node;
2124 exp = require_complete_type (exp);
2125 if (exp == error_mark_node)
2126 return error_mark_node;
2128 promoted_type = targetm.promoted_type (type);
2129 if (promoted_type)
2130 return convert (promoted_type, exp);
2132 if (INTEGRAL_TYPE_P (type))
2133 return perform_integral_promotions (exp);
2135 return exp;
2138 /* Look up COMPONENT in a structure or union TYPE.
2140 If the component name is not found, returns NULL_TREE. Otherwise,
2141 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2142 stepping down the chain to the component, which is in the last
2143 TREE_VALUE of the list. Normally the list is of length one, but if
2144 the component is embedded within (nested) anonymous structures or
2145 unions, the list steps down the chain to the component. */
2147 static tree
2148 lookup_field (tree type, tree component)
2150 tree field;
2152 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2153 to the field elements. Use a binary search on this array to quickly
2154 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2155 will always be set for structures which have many elements. */
2157 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2159 int bot, top, half;
2160 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2162 field = TYPE_FIELDS (type);
2163 bot = 0;
2164 top = TYPE_LANG_SPECIFIC (type)->s->len;
2165 while (top - bot > 1)
2167 half = (top - bot + 1) >> 1;
2168 field = field_array[bot+half];
2170 if (DECL_NAME (field) == NULL_TREE)
2172 /* Step through all anon unions in linear fashion. */
2173 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2175 field = field_array[bot++];
2176 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2177 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2179 tree anon = lookup_field (TREE_TYPE (field), component);
2181 if (anon)
2182 return tree_cons (NULL_TREE, field, anon);
2184 /* The Plan 9 compiler permits referring
2185 directly to an anonymous struct/union field
2186 using a typedef name. */
2187 if (flag_plan9_extensions
2188 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2189 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2190 == TYPE_DECL)
2191 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2192 == component))
2193 break;
2197 /* Entire record is only anon unions. */
2198 if (bot > top)
2199 return NULL_TREE;
2201 /* Restart the binary search, with new lower bound. */
2202 continue;
2205 if (DECL_NAME (field) == component)
2206 break;
2207 if (DECL_NAME (field) < component)
2208 bot += half;
2209 else
2210 top = bot + half;
2213 if (DECL_NAME (field_array[bot]) == component)
2214 field = field_array[bot];
2215 else if (DECL_NAME (field) != component)
2216 return NULL_TREE;
2218 else
2220 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2222 if (DECL_NAME (field) == NULL_TREE
2223 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2224 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2226 tree anon = lookup_field (TREE_TYPE (field), component);
2228 if (anon)
2229 return tree_cons (NULL_TREE, field, anon);
2231 /* The Plan 9 compiler permits referring directly to an
2232 anonymous struct/union field using a typedef
2233 name. */
2234 if (flag_plan9_extensions
2235 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2236 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2237 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2238 == component))
2239 break;
2242 if (DECL_NAME (field) == component)
2243 break;
2246 if (field == NULL_TREE)
2247 return NULL_TREE;
2250 return tree_cons (NULL_TREE, field, NULL_TREE);
2253 /* Make an expression to refer to the COMPONENT field of structure or
2254 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2255 location of the COMPONENT_REF. */
2257 tree
2258 build_component_ref (location_t loc, tree datum, tree component)
2260 tree type = TREE_TYPE (datum);
2261 enum tree_code code = TREE_CODE (type);
2262 tree field = NULL;
2263 tree ref;
2264 bool datum_lvalue = lvalue_p (datum);
2266 if (!objc_is_public (datum, component))
2267 return error_mark_node;
2269 /* Detect Objective-C property syntax object.property. */
2270 if (c_dialect_objc ()
2271 && (ref = objc_maybe_build_component_ref (datum, component)))
2272 return ref;
2274 /* See if there is a field or component with name COMPONENT. */
2276 if (code == RECORD_TYPE || code == UNION_TYPE)
2278 if (!COMPLETE_TYPE_P (type))
2280 c_incomplete_type_error (NULL_TREE, type);
2281 return error_mark_node;
2284 field = lookup_field (type, component);
2286 if (!field)
2288 error_at (loc, "%qT has no member named %qE", type, component);
2289 return error_mark_node;
2292 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2293 This might be better solved in future the way the C++ front
2294 end does it - by giving the anonymous entities each a
2295 separate name and type, and then have build_component_ref
2296 recursively call itself. We can't do that here. */
2299 tree subdatum = TREE_VALUE (field);
2300 int quals;
2301 tree subtype;
2302 bool use_datum_quals;
2304 if (TREE_TYPE (subdatum) == error_mark_node)
2305 return error_mark_node;
2307 /* If this is an rvalue, it does not have qualifiers in C
2308 standard terms and we must avoid propagating such
2309 qualifiers down to a non-lvalue array that is then
2310 converted to a pointer. */
2311 use_datum_quals = (datum_lvalue
2312 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2314 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2315 if (use_datum_quals)
2316 quals |= TYPE_QUALS (TREE_TYPE (datum));
2317 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2319 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2320 NULL_TREE);
2321 SET_EXPR_LOCATION (ref, loc);
2322 if (TREE_READONLY (subdatum)
2323 || (use_datum_quals && TREE_READONLY (datum)))
2324 TREE_READONLY (ref) = 1;
2325 if (TREE_THIS_VOLATILE (subdatum)
2326 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2327 TREE_THIS_VOLATILE (ref) = 1;
2329 if (TREE_DEPRECATED (subdatum))
2330 warn_deprecated_use (subdatum, NULL_TREE);
2332 datum = ref;
2334 field = TREE_CHAIN (field);
2336 while (field);
2338 return ref;
2340 else if (code != ERROR_MARK)
2341 error_at (loc,
2342 "request for member %qE in something not a structure or union",
2343 component);
2345 return error_mark_node;
2348 /* Given an expression PTR for a pointer, return an expression
2349 for the value pointed to.
2350 ERRORSTRING is the name of the operator to appear in error messages.
2352 LOC is the location to use for the generated tree. */
2354 tree
2355 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2357 tree pointer = default_conversion (ptr);
2358 tree type = TREE_TYPE (pointer);
2359 tree ref;
2361 if (TREE_CODE (type) == POINTER_TYPE)
2363 if (CONVERT_EXPR_P (pointer)
2364 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2366 /* If a warning is issued, mark it to avoid duplicates from
2367 the backend. This only needs to be done at
2368 warn_strict_aliasing > 2. */
2369 if (warn_strict_aliasing > 2)
2370 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2371 type, TREE_OPERAND (pointer, 0)))
2372 TREE_NO_WARNING (pointer) = 1;
2375 if (TREE_CODE (pointer) == ADDR_EXPR
2376 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2377 == TREE_TYPE (type)))
2379 ref = TREE_OPERAND (pointer, 0);
2380 protected_set_expr_location (ref, loc);
2381 return ref;
2383 else
2385 tree t = TREE_TYPE (type);
2387 ref = build1 (INDIRECT_REF, t, pointer);
2389 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2391 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2393 error_at (loc, "dereferencing pointer to incomplete type "
2394 "%qT", t);
2395 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2397 return error_mark_node;
2399 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2400 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2402 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2403 so that we get the proper error message if the result is used
2404 to assign to. Also, &* is supposed to be a no-op.
2405 And ANSI C seems to specify that the type of the result
2406 should be the const type. */
2407 /* A de-reference of a pointer to const is not a const. It is valid
2408 to change it via some other pointer. */
2409 TREE_READONLY (ref) = TYPE_READONLY (t);
2410 TREE_SIDE_EFFECTS (ref)
2411 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2412 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2413 protected_set_expr_location (ref, loc);
2414 return ref;
2417 else if (TREE_CODE (pointer) != ERROR_MARK)
2418 invalid_indirection_error (loc, type, errstring);
2420 return error_mark_node;
2423 /* This handles expressions of the form "a[i]", which denotes
2424 an array reference.
2426 This is logically equivalent in C to *(a+i), but we may do it differently.
2427 If A is a variable or a member, we generate a primitive ARRAY_REF.
2428 This avoids forcing the array out of registers, and can work on
2429 arrays that are not lvalues (for example, members of structures returned
2430 by functions).
2432 For vector types, allow vector[i] but not i[vector], and create
2433 *(((type*)&vectortype) + i) for the expression.
2435 LOC is the location to use for the returned expression. */
2437 tree
2438 build_array_ref (location_t loc, tree array, tree index)
2440 tree ret;
2441 bool swapped = false;
2442 if (TREE_TYPE (array) == error_mark_node
2443 || TREE_TYPE (index) == error_mark_node)
2444 return error_mark_node;
2446 if (flag_cilkplus && contains_array_notation_expr (index))
2448 size_t rank = 0;
2449 if (!find_rank (loc, index, index, true, &rank))
2450 return error_mark_node;
2451 if (rank > 1)
2453 error_at (loc, "rank of the array's index is greater than 1");
2454 return error_mark_node;
2457 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2458 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2459 /* Allow vector[index] but not index[vector]. */
2460 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2462 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2463 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2465 error_at (loc,
2466 "subscripted value is neither array nor pointer nor vector");
2468 return error_mark_node;
2470 std::swap (array, index);
2471 swapped = true;
2474 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2476 error_at (loc, "array subscript is not an integer");
2477 return error_mark_node;
2480 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2482 error_at (loc, "subscripted value is pointer to function");
2483 return error_mark_node;
2486 /* ??? Existing practice has been to warn only when the char
2487 index is syntactically the index, not for char[array]. */
2488 if (!swapped)
2489 warn_array_subscript_with_type_char (loc, index);
2491 /* Apply default promotions *after* noticing character types. */
2492 index = default_conversion (index);
2493 if (index == error_mark_node)
2494 return error_mark_node;
2496 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2498 bool non_lvalue
2499 = convert_vector_to_pointer_for_subscript (loc, &array, index);
2501 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2503 tree rval, type;
2505 /* An array that is indexed by a non-constant
2506 cannot be stored in a register; we must be able to do
2507 address arithmetic on its address.
2508 Likewise an array of elements of variable size. */
2509 if (TREE_CODE (index) != INTEGER_CST
2510 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2511 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2513 if (!c_mark_addressable (array))
2514 return error_mark_node;
2516 /* An array that is indexed by a constant value which is not within
2517 the array bounds cannot be stored in a register either; because we
2518 would get a crash in store_bit_field/extract_bit_field when trying
2519 to access a non-existent part of the register. */
2520 if (TREE_CODE (index) == INTEGER_CST
2521 && TYPE_DOMAIN (TREE_TYPE (array))
2522 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2524 if (!c_mark_addressable (array))
2525 return error_mark_node;
2528 if (pedantic || warn_c90_c99_compat)
2530 tree foo = array;
2531 while (TREE_CODE (foo) == COMPONENT_REF)
2532 foo = TREE_OPERAND (foo, 0);
2533 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2534 pedwarn (loc, OPT_Wpedantic,
2535 "ISO C forbids subscripting %<register%> array");
2536 else if (!lvalue_p (foo))
2537 pedwarn_c90 (loc, OPT_Wpedantic,
2538 "ISO C90 forbids subscripting non-lvalue "
2539 "array");
2542 type = TREE_TYPE (TREE_TYPE (array));
2543 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2544 /* Array ref is const/volatile if the array elements are
2545 or if the array is. */
2546 TREE_READONLY (rval)
2547 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2548 | TREE_READONLY (array));
2549 TREE_SIDE_EFFECTS (rval)
2550 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2551 | TREE_SIDE_EFFECTS (array));
2552 TREE_THIS_VOLATILE (rval)
2553 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2554 /* This was added by rms on 16 Nov 91.
2555 It fixes vol struct foo *a; a->elts[1]
2556 in an inline function.
2557 Hope it doesn't break something else. */
2558 | TREE_THIS_VOLATILE (array));
2559 ret = require_complete_type (rval);
2560 protected_set_expr_location (ret, loc);
2561 if (non_lvalue)
2562 ret = non_lvalue_loc (loc, ret);
2563 return ret;
2565 else
2567 tree ar = default_conversion (array);
2569 if (ar == error_mark_node)
2570 return ar;
2572 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2573 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2575 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2576 index, 0),
2577 RO_ARRAY_INDEXING);
2578 if (non_lvalue)
2579 ret = non_lvalue_loc (loc, ret);
2580 return ret;
2584 /* Build an external reference to identifier ID. FUN indicates
2585 whether this will be used for a function call. LOC is the source
2586 location of the identifier. This sets *TYPE to the type of the
2587 identifier, which is not the same as the type of the returned value
2588 for CONST_DECLs defined as enum constants. If the type of the
2589 identifier is not available, *TYPE is set to NULL. */
2590 tree
2591 build_external_ref (location_t loc, tree id, int fun, tree *type)
2593 tree ref;
2594 tree decl = lookup_name (id);
2596 /* In Objective-C, an instance variable (ivar) may be preferred to
2597 whatever lookup_name() found. */
2598 decl = objc_lookup_ivar (decl, id);
2600 *type = NULL;
2601 if (decl && decl != error_mark_node)
2603 ref = decl;
2604 *type = TREE_TYPE (ref);
2606 else if (fun)
2607 /* Implicit function declaration. */
2608 ref = implicitly_declare (loc, id);
2609 else if (decl == error_mark_node)
2610 /* Don't complain about something that's already been
2611 complained about. */
2612 return error_mark_node;
2613 else
2615 undeclared_variable (loc, id);
2616 return error_mark_node;
2619 if (TREE_TYPE (ref) == error_mark_node)
2620 return error_mark_node;
2622 if (TREE_DEPRECATED (ref))
2623 warn_deprecated_use (ref, NULL_TREE);
2625 /* Recursive call does not count as usage. */
2626 if (ref != current_function_decl)
2628 TREE_USED (ref) = 1;
2631 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2633 if (!in_sizeof && !in_typeof)
2634 C_DECL_USED (ref) = 1;
2635 else if (DECL_INITIAL (ref) == 0
2636 && DECL_EXTERNAL (ref)
2637 && !TREE_PUBLIC (ref))
2638 record_maybe_used_decl (ref);
2641 if (TREE_CODE (ref) == CONST_DECL)
2643 used_types_insert (TREE_TYPE (ref));
2645 if (warn_cxx_compat
2646 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2647 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2649 warning_at (loc, OPT_Wc___compat,
2650 ("enum constant defined in struct or union "
2651 "is not visible in C++"));
2652 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2655 ref = DECL_INITIAL (ref);
2656 TREE_CONSTANT (ref) = 1;
2658 else if (current_function_decl != 0
2659 && !DECL_FILE_SCOPE_P (current_function_decl)
2660 && (VAR_OR_FUNCTION_DECL_P (ref)
2661 || TREE_CODE (ref) == PARM_DECL))
2663 tree context = decl_function_context (ref);
2665 if (context != 0 && context != current_function_decl)
2666 DECL_NONLOCAL (ref) = 1;
2668 /* C99 6.7.4p3: An inline definition of a function with external
2669 linkage ... shall not contain a reference to an identifier with
2670 internal linkage. */
2671 else if (current_function_decl != 0
2672 && DECL_DECLARED_INLINE_P (current_function_decl)
2673 && DECL_EXTERNAL (current_function_decl)
2674 && VAR_OR_FUNCTION_DECL_P (ref)
2675 && (!VAR_P (ref) || TREE_STATIC (ref))
2676 && ! TREE_PUBLIC (ref)
2677 && DECL_CONTEXT (ref) != current_function_decl)
2678 record_inline_static (loc, current_function_decl, ref,
2679 csi_internal);
2681 return ref;
2684 /* Record details of decls possibly used inside sizeof or typeof. */
2685 struct maybe_used_decl
2687 /* The decl. */
2688 tree decl;
2689 /* The level seen at (in_sizeof + in_typeof). */
2690 int level;
2691 /* The next one at this level or above, or NULL. */
2692 struct maybe_used_decl *next;
2695 static struct maybe_used_decl *maybe_used_decls;
2697 /* Record that DECL, an undefined static function reference seen
2698 inside sizeof or typeof, might be used if the operand of sizeof is
2699 a VLA type or the operand of typeof is a variably modified
2700 type. */
2702 static void
2703 record_maybe_used_decl (tree decl)
2705 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2706 t->decl = decl;
2707 t->level = in_sizeof + in_typeof;
2708 t->next = maybe_used_decls;
2709 maybe_used_decls = t;
2712 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2713 USED is false, just discard them. If it is true, mark them used
2714 (if no longer inside sizeof or typeof) or move them to the next
2715 level up (if still inside sizeof or typeof). */
2717 void
2718 pop_maybe_used (bool used)
2720 struct maybe_used_decl *p = maybe_used_decls;
2721 int cur_level = in_sizeof + in_typeof;
2722 while (p && p->level > cur_level)
2724 if (used)
2726 if (cur_level == 0)
2727 C_DECL_USED (p->decl) = 1;
2728 else
2729 p->level = cur_level;
2731 p = p->next;
2733 if (!used || cur_level == 0)
2734 maybe_used_decls = p;
2737 /* Return the result of sizeof applied to EXPR. */
2739 struct c_expr
2740 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2742 struct c_expr ret;
2743 if (expr.value == error_mark_node)
2745 ret.value = error_mark_node;
2746 ret.original_code = ERROR_MARK;
2747 ret.original_type = NULL;
2748 pop_maybe_used (false);
2750 else
2752 bool expr_const_operands = true;
2754 if (TREE_CODE (expr.value) == PARM_DECL
2755 && C_ARRAY_PARAMETER (expr.value))
2757 if (warning_at (loc, OPT_Wsizeof_array_argument,
2758 "%<sizeof%> on array function parameter %qE will "
2759 "return size of %qT", expr.value,
2760 expr.original_type))
2761 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2763 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2764 &expr_const_operands);
2765 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2766 c_last_sizeof_arg = expr.value;
2767 ret.original_code = SIZEOF_EXPR;
2768 ret.original_type = NULL;
2769 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2771 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2772 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2773 folded_expr, ret.value);
2774 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2775 SET_EXPR_LOCATION (ret.value, loc);
2777 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2779 return ret;
2782 /* Return the result of sizeof applied to T, a structure for the type
2783 name passed to sizeof (rather than the type itself). LOC is the
2784 location of the original expression. */
2786 struct c_expr
2787 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2789 tree type;
2790 struct c_expr ret;
2791 tree type_expr = NULL_TREE;
2792 bool type_expr_const = true;
2793 type = groktypename (t, &type_expr, &type_expr_const);
2794 ret.value = c_sizeof (loc, type);
2795 c_last_sizeof_arg = type;
2796 ret.original_code = SIZEOF_EXPR;
2797 ret.original_type = NULL;
2798 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2799 && c_vla_type_p (type))
2801 /* If the type is a [*] array, it is a VLA but is represented as
2802 having a size of zero. In such a case we must ensure that
2803 the result of sizeof does not get folded to a constant by
2804 c_fully_fold, because if the size is evaluated the result is
2805 not constant and so constraints on zero or negative size
2806 arrays must not be applied when this sizeof call is inside
2807 another array declarator. */
2808 if (!type_expr)
2809 type_expr = integer_zero_node;
2810 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2811 type_expr, ret.value);
2812 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2814 pop_maybe_used (type != error_mark_node
2815 ? C_TYPE_VARIABLE_SIZE (type) : false);
2816 return ret;
2819 /* Build a function call to function FUNCTION with parameters PARAMS.
2820 The function call is at LOC.
2821 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2822 TREE_VALUE of each node is a parameter-expression.
2823 FUNCTION's data type may be a function type or a pointer-to-function. */
2825 tree
2826 build_function_call (location_t loc, tree function, tree params)
2828 vec<tree, va_gc> *v;
2829 tree ret;
2831 vec_alloc (v, list_length (params));
2832 for (; params; params = TREE_CHAIN (params))
2833 v->quick_push (TREE_VALUE (params));
2834 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2835 vec_free (v);
2836 return ret;
2839 /* Give a note about the location of the declaration of DECL. */
2841 static void
2842 inform_declaration (tree decl)
2844 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2845 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2848 /* Build a function call to function FUNCTION with parameters PARAMS.
2849 ORIGTYPES, if not NULL, is a vector of types; each element is
2850 either NULL or the original type of the corresponding element in
2851 PARAMS. The original type may differ from TREE_TYPE of the
2852 parameter for enums. FUNCTION's data type may be a function type
2853 or pointer-to-function. This function changes the elements of
2854 PARAMS. */
2856 tree
2857 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2858 tree function, vec<tree, va_gc> *params,
2859 vec<tree, va_gc> *origtypes)
2861 tree fntype, fundecl = 0;
2862 tree name = NULL_TREE, result;
2863 tree tem;
2864 int nargs;
2865 tree *argarray;
2868 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2869 STRIP_TYPE_NOPS (function);
2871 /* Convert anything with function type to a pointer-to-function. */
2872 if (TREE_CODE (function) == FUNCTION_DECL)
2874 name = DECL_NAME (function);
2876 if (flag_tm)
2877 tm_malloc_replacement (function);
2878 fundecl = function;
2879 /* Atomic functions have type checking/casting already done. They are
2880 often rewritten and don't match the original parameter list. */
2881 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2882 origtypes = NULL;
2884 if (flag_cilkplus
2885 && is_cilkplus_reduce_builtin (function))
2886 origtypes = NULL;
2888 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2889 function = function_to_pointer_conversion (loc, function);
2891 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2892 expressions, like those used for ObjC messenger dispatches. */
2893 if (params && !params->is_empty ())
2894 function = objc_rewrite_function_call (function, (*params)[0]);
2896 function = c_fully_fold (function, false, NULL);
2898 fntype = TREE_TYPE (function);
2900 if (TREE_CODE (fntype) == ERROR_MARK)
2901 return error_mark_node;
2903 if (!(TREE_CODE (fntype) == POINTER_TYPE
2904 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2906 if (!flag_diagnostics_show_caret)
2907 error_at (loc,
2908 "called object %qE is not a function or function pointer",
2909 function);
2910 else if (DECL_P (function))
2912 error_at (loc,
2913 "called object %qD is not a function or function pointer",
2914 function);
2915 inform_declaration (function);
2917 else
2918 error_at (loc,
2919 "called object is not a function or function pointer");
2920 return error_mark_node;
2923 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2924 current_function_returns_abnormally = 1;
2926 /* fntype now gets the type of function pointed to. */
2927 fntype = TREE_TYPE (fntype);
2929 /* Convert the parameters to the types declared in the
2930 function prototype, or apply default promotions. */
2932 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2933 origtypes, function, fundecl);
2934 if (nargs < 0)
2935 return error_mark_node;
2937 /* Check that the function is called through a compatible prototype.
2938 If it is not, warn. */
2939 if (CONVERT_EXPR_P (function)
2940 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2941 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2942 && !comptypes (fntype, TREE_TYPE (tem)))
2944 tree return_type = TREE_TYPE (fntype);
2946 /* This situation leads to run-time undefined behavior. We can't,
2947 therefore, simply error unless we can prove that all possible
2948 executions of the program must execute the code. */
2949 warning_at (loc, 0, "function called through a non-compatible type");
2951 if (VOID_TYPE_P (return_type)
2952 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2953 pedwarn (loc, 0,
2954 "function with qualified void return type called");
2957 argarray = vec_safe_address (params);
2959 /* Check that arguments to builtin functions match the expectations. */
2960 if (fundecl
2961 && DECL_BUILT_IN (fundecl)
2962 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2963 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2964 return error_mark_node;
2966 /* Check that the arguments to the function are valid. */
2967 check_function_arguments (fntype, nargs, argarray);
2969 if (name != NULL_TREE
2970 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2972 if (require_constant_value)
2973 result =
2974 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2975 function, nargs, argarray);
2976 else
2977 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2978 function, nargs, argarray);
2979 if (TREE_CODE (result) == NOP_EXPR
2980 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2981 STRIP_TYPE_NOPS (result);
2983 else
2984 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2985 function, nargs, argarray);
2987 if (VOID_TYPE_P (TREE_TYPE (result)))
2989 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2990 pedwarn (loc, 0,
2991 "function with qualified void return type called");
2992 return result;
2994 return require_complete_type (result);
2997 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2999 tree
3000 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3001 tree function, vec<tree, va_gc> *params,
3002 vec<tree, va_gc> *origtypes)
3004 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3005 STRIP_TYPE_NOPS (function);
3007 /* Convert anything with function type to a pointer-to-function. */
3008 if (TREE_CODE (function) == FUNCTION_DECL)
3010 /* Implement type-directed function overloading for builtins.
3011 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3012 handle all the type checking. The result is a complete expression
3013 that implements this function call. */
3014 tree tem = resolve_overloaded_builtin (loc, function, params);
3015 if (tem)
3016 return tem;
3018 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3021 /* Convert the argument expressions in the vector VALUES
3022 to the types in the list TYPELIST.
3024 If TYPELIST is exhausted, or when an element has NULL as its type,
3025 perform the default conversions.
3027 ORIGTYPES is the original types of the expressions in VALUES. This
3028 holds the type of enum values which have been converted to integral
3029 types. It may be NULL.
3031 FUNCTION is a tree for the called function. It is used only for
3032 error messages, where it is formatted with %qE.
3034 This is also where warnings about wrong number of args are generated.
3036 ARG_LOC are locations of function arguments (if any).
3038 Returns the actual number of arguments processed (which may be less
3039 than the length of VALUES in some error situations), or -1 on
3040 failure. */
3042 static int
3043 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3044 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3045 tree function, tree fundecl)
3047 tree typetail, val;
3048 unsigned int parmnum;
3049 bool error_args = false;
3050 const bool type_generic = fundecl
3051 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3052 bool type_generic_remove_excess_precision = false;
3053 tree selector;
3055 /* Change pointer to function to the function itself for
3056 diagnostics. */
3057 if (TREE_CODE (function) == ADDR_EXPR
3058 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3059 function = TREE_OPERAND (function, 0);
3061 /* Handle an ObjC selector specially for diagnostics. */
3062 selector = objc_message_selector ();
3064 /* For type-generic built-in functions, determine whether excess
3065 precision should be removed (classification) or not
3066 (comparison). */
3067 if (type_generic
3068 && DECL_BUILT_IN (fundecl)
3069 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3071 switch (DECL_FUNCTION_CODE (fundecl))
3073 case BUILT_IN_ISFINITE:
3074 case BUILT_IN_ISINF:
3075 case BUILT_IN_ISINF_SIGN:
3076 case BUILT_IN_ISNAN:
3077 case BUILT_IN_ISNORMAL:
3078 case BUILT_IN_FPCLASSIFY:
3079 type_generic_remove_excess_precision = true;
3080 break;
3082 default:
3083 type_generic_remove_excess_precision = false;
3084 break;
3087 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3088 return vec_safe_length (values);
3090 /* Scan the given expressions and types, producing individual
3091 converted arguments. */
3093 for (typetail = typelist, parmnum = 0;
3094 values && values->iterate (parmnum, &val);
3095 ++parmnum)
3097 tree type = typetail ? TREE_VALUE (typetail) : 0;
3098 tree valtype = TREE_TYPE (val);
3099 tree rname = function;
3100 int argnum = parmnum + 1;
3101 const char *invalid_func_diag;
3102 bool excess_precision = false;
3103 bool npc;
3104 tree parmval;
3105 /* Some __atomic_* builtins have additional hidden argument at
3106 position 0. */
3107 location_t ploc
3108 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3109 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3110 : input_location;
3112 if (type == void_type_node)
3114 if (selector)
3115 error_at (loc, "too many arguments to method %qE", selector);
3116 else
3117 error_at (loc, "too many arguments to function %qE", function);
3118 inform_declaration (fundecl);
3119 return error_args ? -1 : (int) parmnum;
3122 if (selector && argnum > 2)
3124 rname = selector;
3125 argnum -= 2;
3128 npc = null_pointer_constant_p (val);
3130 /* If there is excess precision and a prototype, convert once to
3131 the required type rather than converting via the semantic
3132 type. Likewise without a prototype a float value represented
3133 as long double should be converted once to double. But for
3134 type-generic classification functions excess precision must
3135 be removed here. */
3136 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3137 && (type || !type_generic || !type_generic_remove_excess_precision))
3139 val = TREE_OPERAND (val, 0);
3140 excess_precision = true;
3142 val = c_fully_fold (val, false, NULL);
3143 STRIP_TYPE_NOPS (val);
3145 val = require_complete_type (val);
3147 if (type != 0)
3149 /* Formal parm type is specified by a function prototype. */
3151 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3153 error_at (ploc, "type of formal parameter %d is incomplete",
3154 parmnum + 1);
3155 parmval = val;
3157 else
3159 tree origtype;
3161 /* Optionally warn about conversions that
3162 differ from the default conversions. */
3163 if (warn_traditional_conversion || warn_traditional)
3165 unsigned int formal_prec = TYPE_PRECISION (type);
3167 if (INTEGRAL_TYPE_P (type)
3168 && TREE_CODE (valtype) == REAL_TYPE)
3169 warning_at (ploc, OPT_Wtraditional_conversion,
3170 "passing argument %d of %qE as integer rather "
3171 "than floating due to prototype",
3172 argnum, rname);
3173 if (INTEGRAL_TYPE_P (type)
3174 && TREE_CODE (valtype) == COMPLEX_TYPE)
3175 warning_at (ploc, OPT_Wtraditional_conversion,
3176 "passing argument %d of %qE as integer rather "
3177 "than complex due to prototype",
3178 argnum, rname);
3179 else if (TREE_CODE (type) == COMPLEX_TYPE
3180 && TREE_CODE (valtype) == REAL_TYPE)
3181 warning_at (ploc, OPT_Wtraditional_conversion,
3182 "passing argument %d of %qE as complex rather "
3183 "than floating due to prototype",
3184 argnum, rname);
3185 else if (TREE_CODE (type) == REAL_TYPE
3186 && INTEGRAL_TYPE_P (valtype))
3187 warning_at (ploc, OPT_Wtraditional_conversion,
3188 "passing argument %d of %qE as floating rather "
3189 "than integer due to prototype",
3190 argnum, rname);
3191 else if (TREE_CODE (type) == COMPLEX_TYPE
3192 && INTEGRAL_TYPE_P (valtype))
3193 warning_at (ploc, OPT_Wtraditional_conversion,
3194 "passing argument %d of %qE as complex rather "
3195 "than integer due to prototype",
3196 argnum, rname);
3197 else if (TREE_CODE (type) == REAL_TYPE
3198 && TREE_CODE (valtype) == COMPLEX_TYPE)
3199 warning_at (ploc, OPT_Wtraditional_conversion,
3200 "passing argument %d of %qE as floating rather "
3201 "than complex due to prototype",
3202 argnum, rname);
3203 /* ??? At some point, messages should be written about
3204 conversions between complex types, but that's too messy
3205 to do now. */
3206 else if (TREE_CODE (type) == REAL_TYPE
3207 && TREE_CODE (valtype) == REAL_TYPE)
3209 /* Warn if any argument is passed as `float',
3210 since without a prototype it would be `double'. */
3211 if (formal_prec == TYPE_PRECISION (float_type_node)
3212 && type != dfloat32_type_node)
3213 warning_at (ploc, 0,
3214 "passing argument %d of %qE as %<float%> "
3215 "rather than %<double%> due to prototype",
3216 argnum, rname);
3218 /* Warn if mismatch between argument and prototype
3219 for decimal float types. Warn of conversions with
3220 binary float types and of precision narrowing due to
3221 prototype. */
3222 else if (type != valtype
3223 && (type == dfloat32_type_node
3224 || type == dfloat64_type_node
3225 || type == dfloat128_type_node
3226 || valtype == dfloat32_type_node
3227 || valtype == dfloat64_type_node
3228 || valtype == dfloat128_type_node)
3229 && (formal_prec
3230 <= TYPE_PRECISION (valtype)
3231 || (type == dfloat128_type_node
3232 && (valtype
3233 != dfloat64_type_node
3234 && (valtype
3235 != dfloat32_type_node)))
3236 || (type == dfloat64_type_node
3237 && (valtype
3238 != dfloat32_type_node))))
3239 warning_at (ploc, 0,
3240 "passing argument %d of %qE as %qT "
3241 "rather than %qT due to prototype",
3242 argnum, rname, type, valtype);
3245 /* Detect integer changing in width or signedness.
3246 These warnings are only activated with
3247 -Wtraditional-conversion, not with -Wtraditional. */
3248 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3249 && INTEGRAL_TYPE_P (valtype))
3251 tree would_have_been = default_conversion (val);
3252 tree type1 = TREE_TYPE (would_have_been);
3254 if (TREE_CODE (type) == ENUMERAL_TYPE
3255 && (TYPE_MAIN_VARIANT (type)
3256 == TYPE_MAIN_VARIANT (valtype)))
3257 /* No warning if function asks for enum
3258 and the actual arg is that enum type. */
3260 else if (formal_prec != TYPE_PRECISION (type1))
3261 warning_at (ploc, OPT_Wtraditional_conversion,
3262 "passing argument %d of %qE "
3263 "with different width due to prototype",
3264 argnum, rname);
3265 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3267 /* Don't complain if the formal parameter type
3268 is an enum, because we can't tell now whether
3269 the value was an enum--even the same enum. */
3270 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3272 else if (TREE_CODE (val) == INTEGER_CST
3273 && int_fits_type_p (val, type))
3274 /* Change in signedness doesn't matter
3275 if a constant value is unaffected. */
3277 /* If the value is extended from a narrower
3278 unsigned type, it doesn't matter whether we
3279 pass it as signed or unsigned; the value
3280 certainly is the same either way. */
3281 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3282 && TYPE_UNSIGNED (valtype))
3284 else if (TYPE_UNSIGNED (type))
3285 warning_at (ploc, OPT_Wtraditional_conversion,
3286 "passing argument %d of %qE "
3287 "as unsigned due to prototype",
3288 argnum, rname);
3289 else
3290 warning_at (ploc, OPT_Wtraditional_conversion,
3291 "passing argument %d of %qE "
3292 "as signed due to prototype",
3293 argnum, rname);
3297 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3298 sake of better warnings from convert_and_check. */
3299 if (excess_precision)
3300 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3301 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3302 parmval = convert_for_assignment (loc, ploc, type,
3303 val, origtype, ic_argpass,
3304 npc, fundecl, function,
3305 parmnum + 1);
3307 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3308 && INTEGRAL_TYPE_P (type)
3309 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3310 parmval = default_conversion (parmval);
3313 else if (TREE_CODE (valtype) == REAL_TYPE
3314 && (TYPE_PRECISION (valtype)
3315 <= TYPE_PRECISION (double_type_node))
3316 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3317 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3318 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3320 if (type_generic)
3321 parmval = val;
3322 else
3324 /* Convert `float' to `double'. */
3325 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3326 warning_at (ploc, OPT_Wdouble_promotion,
3327 "implicit conversion from %qT to %qT when passing "
3328 "argument to function",
3329 valtype, double_type_node);
3330 parmval = convert (double_type_node, val);
3333 else if (excess_precision && !type_generic)
3334 /* A "double" argument with excess precision being passed
3335 without a prototype or in variable arguments. */
3336 parmval = convert (valtype, val);
3337 else if ((invalid_func_diag =
3338 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3340 error (invalid_func_diag);
3341 return -1;
3343 else
3344 /* Convert `short' and `char' to full-size `int'. */
3345 parmval = default_conversion (val);
3347 (*values)[parmnum] = parmval;
3348 if (parmval == error_mark_node)
3349 error_args = true;
3351 if (typetail)
3352 typetail = TREE_CHAIN (typetail);
3355 gcc_assert (parmnum == vec_safe_length (values));
3357 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3359 error_at (loc, "too few arguments to function %qE", function);
3360 inform_declaration (fundecl);
3361 return -1;
3364 return error_args ? -1 : (int) parmnum;
3367 /* This is the entry point used by the parser to build unary operators
3368 in the input. CODE, a tree_code, specifies the unary operator, and
3369 ARG is the operand. For unary plus, the C parser currently uses
3370 CONVERT_EXPR for code.
3372 LOC is the location to use for the tree generated.
3375 struct c_expr
3376 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3378 struct c_expr result;
3380 result.value = build_unary_op (loc, code, arg.value, 0);
3381 result.original_code = code;
3382 result.original_type = NULL;
3384 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3385 overflow_warning (loc, result.value);
3387 return result;
3390 /* This is the entry point used by the parser to build binary operators
3391 in the input. CODE, a tree_code, specifies the binary operator, and
3392 ARG1 and ARG2 are the operands. In addition to constructing the
3393 expression, we check for operands that were written with other binary
3394 operators in a way that is likely to confuse the user.
3396 LOCATION is the location of the binary operator. */
3398 struct c_expr
3399 parser_build_binary_op (location_t location, enum tree_code code,
3400 struct c_expr arg1, struct c_expr arg2)
3402 struct c_expr result;
3404 enum tree_code code1 = arg1.original_code;
3405 enum tree_code code2 = arg2.original_code;
3406 tree type1 = (arg1.original_type
3407 ? arg1.original_type
3408 : TREE_TYPE (arg1.value));
3409 tree type2 = (arg2.original_type
3410 ? arg2.original_type
3411 : TREE_TYPE (arg2.value));
3413 result.value = build_binary_op (location, code,
3414 arg1.value, arg2.value, 1);
3415 result.original_code = code;
3416 result.original_type = NULL;
3418 if (TREE_CODE (result.value) == ERROR_MARK)
3419 return result;
3421 if (location != UNKNOWN_LOCATION)
3422 protected_set_expr_location (result.value, location);
3424 /* Check for cases such as x+y<<z which users are likely
3425 to misinterpret. */
3426 if (warn_parentheses)
3427 warn_about_parentheses (location, code, code1, arg1.value, code2,
3428 arg2.value);
3430 if (warn_logical_op)
3431 warn_logical_operator (location, code, TREE_TYPE (result.value),
3432 code1, arg1.value, code2, arg2.value);
3434 if (warn_logical_not_paren
3435 && TREE_CODE_CLASS (code) == tcc_comparison
3436 && code1 == TRUTH_NOT_EXPR
3437 && code2 != TRUTH_NOT_EXPR
3438 /* Avoid warning for !!x == y. */
3439 && (TREE_CODE (arg1.value) != NE_EXPR
3440 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3442 /* Avoid warning for !b == y where b has _Bool type. */
3443 tree t = integer_zero_node;
3444 if (TREE_CODE (arg1.value) == EQ_EXPR
3445 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3446 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3448 t = TREE_OPERAND (arg1.value, 0);
3451 if (TREE_TYPE (t) != integer_type_node)
3452 break;
3453 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3454 t = C_MAYBE_CONST_EXPR_EXPR (t);
3455 else if (CONVERT_EXPR_P (t))
3456 t = TREE_OPERAND (t, 0);
3457 else
3458 break;
3460 while (1);
3462 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3463 warn_logical_not_parentheses (location, code, arg2.value);
3466 /* Warn about comparisons against string literals, with the exception
3467 of testing for equality or inequality of a string literal with NULL. */
3468 if (code == EQ_EXPR || code == NE_EXPR)
3470 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3471 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3472 warning_at (location, OPT_Waddress,
3473 "comparison with string literal results in unspecified behavior");
3475 else if (TREE_CODE_CLASS (code) == tcc_comparison
3476 && (code1 == STRING_CST || code2 == STRING_CST))
3477 warning_at (location, OPT_Waddress,
3478 "comparison with string literal results in unspecified behavior");
3480 if (TREE_OVERFLOW_P (result.value)
3481 && !TREE_OVERFLOW_P (arg1.value)
3482 && !TREE_OVERFLOW_P (arg2.value))
3483 overflow_warning (location, result.value);
3485 /* Warn about comparisons of different enum types. */
3486 if (warn_enum_compare
3487 && TREE_CODE_CLASS (code) == tcc_comparison
3488 && TREE_CODE (type1) == ENUMERAL_TYPE
3489 && TREE_CODE (type2) == ENUMERAL_TYPE
3490 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3491 warning_at (location, OPT_Wenum_compare,
3492 "comparison between %qT and %qT",
3493 type1, type2);
3495 return result;
3498 /* Return a tree for the difference of pointers OP0 and OP1.
3499 The resulting tree has type int. */
3501 static tree
3502 pointer_diff (location_t loc, tree op0, tree op1)
3504 tree restype = ptrdiff_type_node;
3505 tree result, inttype;
3507 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3508 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3509 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3510 tree orig_op1 = op1;
3512 /* If the operands point into different address spaces, we need to
3513 explicitly convert them to pointers into the common address space
3514 before we can subtract the numerical address values. */
3515 if (as0 != as1)
3517 addr_space_t as_common;
3518 tree common_type;
3520 /* Determine the common superset address space. This is guaranteed
3521 to exist because the caller verified that comp_target_types
3522 returned non-zero. */
3523 if (!addr_space_superset (as0, as1, &as_common))
3524 gcc_unreachable ();
3526 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3527 op0 = convert (common_type, op0);
3528 op1 = convert (common_type, op1);
3531 /* Determine integer type to perform computations in. This will usually
3532 be the same as the result type (ptrdiff_t), but may need to be a wider
3533 type if pointers for the address space are wider than ptrdiff_t. */
3534 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3535 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3536 else
3537 inttype = restype;
3539 if (TREE_CODE (target_type) == VOID_TYPE)
3540 pedwarn (loc, OPT_Wpointer_arith,
3541 "pointer of type %<void *%> used in subtraction");
3542 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3543 pedwarn (loc, OPT_Wpointer_arith,
3544 "pointer to a function used in subtraction");
3546 /* First do the subtraction as integers;
3547 then drop through to build the divide operator.
3548 Do not do default conversions on the minus operator
3549 in case restype is a short type. */
3551 op0 = build_binary_op (loc,
3552 MINUS_EXPR, convert (inttype, op0),
3553 convert (inttype, op1), 0);
3554 /* This generates an error if op1 is pointer to incomplete type. */
3555 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3556 error_at (loc, "arithmetic on pointer to an incomplete type");
3558 op1 = c_size_in_bytes (target_type);
3560 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3561 error_at (loc, "arithmetic on pointer to an empty aggregate");
3563 /* Divide by the size, in easiest possible way. */
3564 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3565 op0, convert (inttype, op1));
3567 /* Convert to final result type if necessary. */
3568 return convert (restype, result);
3571 /* Expand atomic compound assignments into an approriate sequence as
3572 specified by the C11 standard section 6.5.16.2.
3573 given
3574 _Atomic T1 E1
3575 T2 E2
3576 E1 op= E2
3578 This sequence is used for all types for which these operations are
3579 supported.
3581 In addition, built-in versions of the 'fe' prefixed routines may
3582 need to be invoked for floating point (real, complex or vector) when
3583 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3585 T1 newval;
3586 T1 old;
3587 T1 *addr
3588 T2 val
3589 fenv_t fenv
3591 addr = &E1;
3592 val = (E2);
3593 __atomic_load (addr, &old, SEQ_CST);
3594 feholdexcept (&fenv);
3595 loop:
3596 newval = old op val;
3597 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3598 SEQ_CST))
3599 goto done;
3600 feclearexcept (FE_ALL_EXCEPT);
3601 goto loop:
3602 done:
3603 feupdateenv (&fenv);
3605 Also note that the compiler is simply issuing the generic form of
3606 the atomic operations. This requires temp(s) and has their address
3607 taken. The atomic processing is smart enough to figure out when the
3608 size of an object can utilize a lock-free version, and convert the
3609 built-in call to the appropriate lock-free routine. The optimizers
3610 will then dispose of any temps that are no longer required, and
3611 lock-free implementations are utilized as long as there is target
3612 support for the required size.
3614 If the operator is NOP_EXPR, then this is a simple assignment, and
3615 an __atomic_store is issued to perform the assignment rather than
3616 the above loop.
3620 /* Build an atomic assignment at LOC, expanding into the proper
3621 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3622 the result of the operation, unless RETURN_OLD_P in which case
3623 return the old value of LHS (this is only for postincrement and
3624 postdecrement). */
3625 static tree
3626 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3627 tree rhs, bool return_old_p)
3629 tree fndecl, func_call;
3630 vec<tree, va_gc> *params;
3631 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3632 tree old, old_addr;
3633 tree compound_stmt;
3634 tree stmt, goto_stmt;
3635 tree loop_label, loop_decl, done_label, done_decl;
3637 tree lhs_type = TREE_TYPE (lhs);
3638 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3639 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3640 tree rhs_type = TREE_TYPE (rhs);
3642 gcc_assert (TYPE_ATOMIC (lhs_type));
3644 if (return_old_p)
3645 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3647 /* Allocate enough vector items for a compare_exchange. */
3648 vec_alloc (params, 6);
3650 /* Create a compound statement to hold the sequence of statements
3651 with a loop. */
3652 compound_stmt = c_begin_compound_stmt (false);
3654 /* Fold the RHS if it hasn't already been folded. */
3655 if (modifycode != NOP_EXPR)
3656 rhs = c_fully_fold (rhs, false, NULL);
3658 /* Remove the qualifiers for the rest of the expressions and create
3659 the VAL temp variable to hold the RHS. */
3660 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3661 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3662 val = create_tmp_var_raw (nonatomic_rhs_type);
3663 TREE_ADDRESSABLE (val) = 1;
3664 TREE_NO_WARNING (val) = 1;
3665 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3666 NULL_TREE);
3667 SET_EXPR_LOCATION (rhs, loc);
3668 add_stmt (rhs);
3670 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3671 an atomic_store. */
3672 if (modifycode == NOP_EXPR)
3674 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3675 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3676 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3677 params->quick_push (lhs_addr);
3678 params->quick_push (rhs);
3679 params->quick_push (seq_cst);
3680 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3681 add_stmt (func_call);
3683 /* Finish the compound statement. */
3684 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3686 /* VAL is the value which was stored, return a COMPOUND_STMT of
3687 the statement and that value. */
3688 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3691 /* Create the variables and labels required for the op= form. */
3692 old = create_tmp_var_raw (nonatomic_lhs_type);
3693 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3694 TREE_ADDRESSABLE (old) = 1;
3695 TREE_NO_WARNING (old) = 1;
3697 newval = create_tmp_var_raw (nonatomic_lhs_type);
3698 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3699 TREE_ADDRESSABLE (newval) = 1;
3701 loop_decl = create_artificial_label (loc);
3702 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3704 done_decl = create_artificial_label (loc);
3705 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3707 /* __atomic_load (addr, &old, SEQ_CST). */
3708 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3709 params->quick_push (lhs_addr);
3710 params->quick_push (old_addr);
3711 params->quick_push (seq_cst);
3712 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3713 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3714 NULL_TREE);
3715 add_stmt (old);
3716 params->truncate (0);
3718 /* Create the expressions for floating-point environment
3719 manipulation, if required. */
3720 bool need_fenv = (flag_trapping_math
3721 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3722 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3723 if (need_fenv)
3724 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3726 if (hold_call)
3727 add_stmt (hold_call);
3729 /* loop: */
3730 add_stmt (loop_label);
3732 /* newval = old + val; */
3733 rhs = build_binary_op (loc, modifycode, old, val, 1);
3734 rhs = c_fully_fold (rhs, false, NULL);
3735 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3736 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3737 NULL_TREE, 0);
3738 if (rhs != error_mark_node)
3740 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3741 NULL_TREE);
3742 SET_EXPR_LOCATION (rhs, loc);
3743 add_stmt (rhs);
3746 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3747 goto done; */
3748 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3749 params->quick_push (lhs_addr);
3750 params->quick_push (old_addr);
3751 params->quick_push (newval_addr);
3752 params->quick_push (integer_zero_node);
3753 params->quick_push (seq_cst);
3754 params->quick_push (seq_cst);
3755 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3757 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3758 SET_EXPR_LOCATION (goto_stmt, loc);
3760 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3761 SET_EXPR_LOCATION (stmt, loc);
3762 add_stmt (stmt);
3764 if (clear_call)
3765 add_stmt (clear_call);
3767 /* goto loop; */
3768 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3769 SET_EXPR_LOCATION (goto_stmt, loc);
3770 add_stmt (goto_stmt);
3772 /* done: */
3773 add_stmt (done_label);
3775 if (update_call)
3776 add_stmt (update_call);
3778 /* Finish the compound statement. */
3779 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3781 /* NEWVAL is the value that was successfully stored, return a
3782 COMPOUND_EXPR of the statement and the appropriate value. */
3783 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3784 return_old_p ? old : newval);
3787 /* Construct and perhaps optimize a tree representation
3788 for a unary operation. CODE, a tree_code, specifies the operation
3789 and XARG is the operand.
3790 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3791 the default promotions (such as from short to int).
3792 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3793 allows non-lvalues; this is only used to handle conversion of non-lvalue
3794 arrays to pointers in C99.
3796 LOCATION is the location of the operator. */
3798 tree
3799 build_unary_op (location_t location,
3800 enum tree_code code, tree xarg, int flag)
3802 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3803 tree arg = xarg;
3804 tree argtype = 0;
3805 enum tree_code typecode;
3806 tree val;
3807 tree ret = error_mark_node;
3808 tree eptype = NULL_TREE;
3809 int noconvert = flag;
3810 const char *invalid_op_diag;
3811 bool int_operands;
3813 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3814 if (int_operands)
3815 arg = remove_c_maybe_const_expr (arg);
3817 if (code != ADDR_EXPR)
3818 arg = require_complete_type (arg);
3820 typecode = TREE_CODE (TREE_TYPE (arg));
3821 if (typecode == ERROR_MARK)
3822 return error_mark_node;
3823 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3824 typecode = INTEGER_TYPE;
3826 if ((invalid_op_diag
3827 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3829 error_at (location, invalid_op_diag);
3830 return error_mark_node;
3833 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3835 eptype = TREE_TYPE (arg);
3836 arg = TREE_OPERAND (arg, 0);
3839 switch (code)
3841 case CONVERT_EXPR:
3842 /* This is used for unary plus, because a CONVERT_EXPR
3843 is enough to prevent anybody from looking inside for
3844 associativity, but won't generate any code. */
3845 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3846 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3847 || typecode == VECTOR_TYPE))
3849 error_at (location, "wrong type argument to unary plus");
3850 return error_mark_node;
3852 else if (!noconvert)
3853 arg = default_conversion (arg);
3854 arg = non_lvalue_loc (location, arg);
3855 break;
3857 case NEGATE_EXPR:
3858 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3859 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3860 || typecode == VECTOR_TYPE))
3862 error_at (location, "wrong type argument to unary minus");
3863 return error_mark_node;
3865 else if (!noconvert)
3866 arg = default_conversion (arg);
3867 break;
3869 case BIT_NOT_EXPR:
3870 /* ~ works on integer types and non float vectors. */
3871 if (typecode == INTEGER_TYPE
3872 || (typecode == VECTOR_TYPE
3873 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3875 if (!noconvert)
3876 arg = default_conversion (arg);
3878 else if (typecode == COMPLEX_TYPE)
3880 code = CONJ_EXPR;
3881 pedwarn (location, OPT_Wpedantic,
3882 "ISO C does not support %<~%> for complex conjugation");
3883 if (!noconvert)
3884 arg = default_conversion (arg);
3886 else
3888 error_at (location, "wrong type argument to bit-complement");
3889 return error_mark_node;
3891 break;
3893 case ABS_EXPR:
3894 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3896 error_at (location, "wrong type argument to abs");
3897 return error_mark_node;
3899 else if (!noconvert)
3900 arg = default_conversion (arg);
3901 break;
3903 case CONJ_EXPR:
3904 /* Conjugating a real value is a no-op, but allow it anyway. */
3905 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3906 || typecode == COMPLEX_TYPE))
3908 error_at (location, "wrong type argument to conjugation");
3909 return error_mark_node;
3911 else if (!noconvert)
3912 arg = default_conversion (arg);
3913 break;
3915 case TRUTH_NOT_EXPR:
3916 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3917 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3918 && typecode != COMPLEX_TYPE)
3920 error_at (location,
3921 "wrong type argument to unary exclamation mark");
3922 return error_mark_node;
3924 if (int_operands)
3926 arg = c_objc_common_truthvalue_conversion (location, xarg);
3927 arg = remove_c_maybe_const_expr (arg);
3929 else
3930 arg = c_objc_common_truthvalue_conversion (location, arg);
3931 ret = invert_truthvalue_loc (location, arg);
3932 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3933 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3934 location = EXPR_LOCATION (ret);
3935 goto return_build_unary_op;
3937 case REALPART_EXPR:
3938 case IMAGPART_EXPR:
3939 ret = build_real_imag_expr (location, code, arg);
3940 if (ret == error_mark_node)
3941 return error_mark_node;
3942 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3943 eptype = TREE_TYPE (eptype);
3944 goto return_build_unary_op;
3946 case PREINCREMENT_EXPR:
3947 case POSTINCREMENT_EXPR:
3948 case PREDECREMENT_EXPR:
3949 case POSTDECREMENT_EXPR:
3951 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3953 tree inner = build_unary_op (location, code,
3954 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3955 if (inner == error_mark_node)
3956 return error_mark_node;
3957 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3958 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3959 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3960 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3961 goto return_build_unary_op;
3964 /* Complain about anything that is not a true lvalue. In
3965 Objective-C, skip this check for property_refs. */
3966 if (!objc_is_property_ref (arg)
3967 && !lvalue_or_else (location,
3968 arg, ((code == PREINCREMENT_EXPR
3969 || code == POSTINCREMENT_EXPR)
3970 ? lv_increment
3971 : lv_decrement)))
3972 return error_mark_node;
3974 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3976 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3977 warning_at (location, OPT_Wc___compat,
3978 "increment of enumeration value is invalid in C++");
3979 else
3980 warning_at (location, OPT_Wc___compat,
3981 "decrement of enumeration value is invalid in C++");
3984 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3985 arg = c_fully_fold (arg, false, NULL);
3987 bool atomic_op;
3988 atomic_op = really_atomic_lvalue (arg);
3990 /* Increment or decrement the real part of the value,
3991 and don't change the imaginary part. */
3992 if (typecode == COMPLEX_TYPE)
3994 tree real, imag;
3996 pedwarn (location, OPT_Wpedantic,
3997 "ISO C does not support %<++%> and %<--%> on complex types");
3999 if (!atomic_op)
4001 arg = stabilize_reference (arg);
4002 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4003 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4004 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4005 if (real == error_mark_node || imag == error_mark_node)
4006 return error_mark_node;
4007 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4008 real, imag);
4009 goto return_build_unary_op;
4013 /* Report invalid types. */
4015 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4016 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4017 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4019 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4020 error_at (location, "wrong type argument to increment");
4021 else
4022 error_at (location, "wrong type argument to decrement");
4024 return error_mark_node;
4028 tree inc;
4030 argtype = TREE_TYPE (arg);
4032 /* Compute the increment. */
4034 if (typecode == POINTER_TYPE)
4036 /* If pointer target is an incomplete type,
4037 we just cannot know how to do the arithmetic. */
4038 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4040 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4041 error_at (location,
4042 "increment of pointer to an incomplete type %qT",
4043 TREE_TYPE (argtype));
4044 else
4045 error_at (location,
4046 "decrement of pointer to an incomplete type %qT",
4047 TREE_TYPE (argtype));
4049 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4050 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4052 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4053 pedwarn (location, OPT_Wpointer_arith,
4054 "wrong type argument to increment");
4055 else
4056 pedwarn (location, OPT_Wpointer_arith,
4057 "wrong type argument to decrement");
4060 inc = c_size_in_bytes (TREE_TYPE (argtype));
4061 inc = convert_to_ptrofftype_loc (location, inc);
4063 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4065 /* For signed fract types, we invert ++ to -- or
4066 -- to ++, and change inc from 1 to -1, because
4067 it is not possible to represent 1 in signed fract constants.
4068 For unsigned fract types, the result always overflows and
4069 we get an undefined (original) or the maximum value. */
4070 if (code == PREINCREMENT_EXPR)
4071 code = PREDECREMENT_EXPR;
4072 else if (code == PREDECREMENT_EXPR)
4073 code = PREINCREMENT_EXPR;
4074 else if (code == POSTINCREMENT_EXPR)
4075 code = POSTDECREMENT_EXPR;
4076 else /* code == POSTDECREMENT_EXPR */
4077 code = POSTINCREMENT_EXPR;
4079 inc = integer_minus_one_node;
4080 inc = convert (argtype, inc);
4082 else
4084 inc = VECTOR_TYPE_P (argtype)
4085 ? build_one_cst (argtype)
4086 : integer_one_node;
4087 inc = convert (argtype, inc);
4090 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4091 need to ask Objective-C to build the increment or decrement
4092 expression for it. */
4093 if (objc_is_property_ref (arg))
4094 return objc_build_incr_expr_for_property_ref (location, code,
4095 arg, inc);
4097 /* Report a read-only lvalue. */
4098 if (TYPE_READONLY (argtype))
4100 readonly_error (location, arg,
4101 ((code == PREINCREMENT_EXPR
4102 || code == POSTINCREMENT_EXPR)
4103 ? lv_increment : lv_decrement));
4104 return error_mark_node;
4106 else if (TREE_READONLY (arg))
4107 readonly_warning (arg,
4108 ((code == PREINCREMENT_EXPR
4109 || code == POSTINCREMENT_EXPR)
4110 ? lv_increment : lv_decrement));
4112 /* If the argument is atomic, use the special code sequences for
4113 atomic compound assignment. */
4114 if (atomic_op)
4116 arg = stabilize_reference (arg);
4117 ret = build_atomic_assign (location, arg,
4118 ((code == PREINCREMENT_EXPR
4119 || code == POSTINCREMENT_EXPR)
4120 ? PLUS_EXPR
4121 : MINUS_EXPR),
4122 (FRACT_MODE_P (TYPE_MODE (argtype))
4123 ? inc
4124 : integer_one_node),
4125 (code == POSTINCREMENT_EXPR
4126 || code == POSTDECREMENT_EXPR));
4127 goto return_build_unary_op;
4130 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4131 val = boolean_increment (code, arg);
4132 else
4133 val = build2 (code, TREE_TYPE (arg), arg, inc);
4134 TREE_SIDE_EFFECTS (val) = 1;
4135 if (TREE_CODE (val) != code)
4136 TREE_NO_WARNING (val) = 1;
4137 ret = val;
4138 goto return_build_unary_op;
4141 case ADDR_EXPR:
4142 /* Note that this operation never does default_conversion. */
4144 /* The operand of unary '&' must be an lvalue (which excludes
4145 expressions of type void), or, in C99, the result of a [] or
4146 unary '*' operator. */
4147 if (VOID_TYPE_P (TREE_TYPE (arg))
4148 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4149 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4150 pedwarn (location, 0, "taking address of expression of type %<void%>");
4152 /* Let &* cancel out to simplify resulting code. */
4153 if (INDIRECT_REF_P (arg))
4155 /* Don't let this be an lvalue. */
4156 if (lvalue_p (TREE_OPERAND (arg, 0)))
4157 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4158 ret = TREE_OPERAND (arg, 0);
4159 goto return_build_unary_op;
4162 /* For &x[y], return x+y */
4163 if (TREE_CODE (arg) == ARRAY_REF)
4165 tree op0 = TREE_OPERAND (arg, 0);
4166 if (!c_mark_addressable (op0))
4167 return error_mark_node;
4170 /* Anything not already handled and not a true memory reference
4171 or a non-lvalue array is an error. */
4172 else if (typecode != FUNCTION_TYPE && !flag
4173 && !lvalue_or_else (location, arg, lv_addressof))
4174 return error_mark_node;
4176 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4177 folding later. */
4178 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4180 tree inner = build_unary_op (location, code,
4181 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4182 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4183 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4184 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4185 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4186 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4187 goto return_build_unary_op;
4190 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4191 argtype = TREE_TYPE (arg);
4193 /* If the lvalue is const or volatile, merge that into the type
4194 to which the address will point. This is only needed
4195 for function types. */
4196 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4197 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4198 && TREE_CODE (argtype) == FUNCTION_TYPE)
4200 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4201 int quals = orig_quals;
4203 if (TREE_READONLY (arg))
4204 quals |= TYPE_QUAL_CONST;
4205 if (TREE_THIS_VOLATILE (arg))
4206 quals |= TYPE_QUAL_VOLATILE;
4208 argtype = c_build_qualified_type (argtype, quals);
4211 if (!c_mark_addressable (arg))
4212 return error_mark_node;
4214 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4215 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4217 argtype = build_pointer_type (argtype);
4219 /* ??? Cope with user tricks that amount to offsetof. Delete this
4220 when we have proper support for integer constant expressions. */
4221 val = get_base_address (arg);
4222 if (val && INDIRECT_REF_P (val)
4223 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4225 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4226 goto return_build_unary_op;
4229 val = build1 (ADDR_EXPR, argtype, arg);
4231 ret = val;
4232 goto return_build_unary_op;
4234 default:
4235 gcc_unreachable ();
4238 if (argtype == 0)
4239 argtype = TREE_TYPE (arg);
4240 if (TREE_CODE (arg) == INTEGER_CST)
4241 ret = (require_constant_value
4242 ? fold_build1_initializer_loc (location, code, argtype, arg)
4243 : fold_build1_loc (location, code, argtype, arg));
4244 else
4245 ret = build1 (code, argtype, arg);
4246 return_build_unary_op:
4247 gcc_assert (ret != error_mark_node);
4248 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4249 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4250 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4251 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4252 ret = note_integer_operands (ret);
4253 if (eptype)
4254 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4255 protected_set_expr_location (ret, location);
4256 return ret;
4259 /* Return nonzero if REF is an lvalue valid for this language.
4260 Lvalues can be assigned, unless their type has TYPE_READONLY.
4261 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4263 bool
4264 lvalue_p (const_tree ref)
4266 const enum tree_code code = TREE_CODE (ref);
4268 switch (code)
4270 case REALPART_EXPR:
4271 case IMAGPART_EXPR:
4272 case COMPONENT_REF:
4273 return lvalue_p (TREE_OPERAND (ref, 0));
4275 case C_MAYBE_CONST_EXPR:
4276 return lvalue_p (TREE_OPERAND (ref, 1));
4278 case COMPOUND_LITERAL_EXPR:
4279 case STRING_CST:
4280 return 1;
4282 case INDIRECT_REF:
4283 case ARRAY_REF:
4284 case ARRAY_NOTATION_REF:
4285 case VAR_DECL:
4286 case PARM_DECL:
4287 case RESULT_DECL:
4288 case ERROR_MARK:
4289 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4290 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4292 case BIND_EXPR:
4293 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4295 default:
4296 return 0;
4300 /* Give a warning for storing in something that is read-only in GCC
4301 terms but not const in ISO C terms. */
4303 static void
4304 readonly_warning (tree arg, enum lvalue_use use)
4306 switch (use)
4308 case lv_assign:
4309 warning (0, "assignment of read-only location %qE", arg);
4310 break;
4311 case lv_increment:
4312 warning (0, "increment of read-only location %qE", arg);
4313 break;
4314 case lv_decrement:
4315 warning (0, "decrement of read-only location %qE", arg);
4316 break;
4317 default:
4318 gcc_unreachable ();
4320 return;
4324 /* Return nonzero if REF is an lvalue valid for this language;
4325 otherwise, print an error message and return zero. USE says
4326 how the lvalue is being used and so selects the error message.
4327 LOCATION is the location at which any error should be reported. */
4329 static int
4330 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4332 int win = lvalue_p (ref);
4334 if (!win)
4335 lvalue_error (loc, use);
4337 return win;
4340 /* Mark EXP saying that we need to be able to take the
4341 address of it; it should not be allocated in a register.
4342 Returns true if successful. */
4344 bool
4345 c_mark_addressable (tree exp)
4347 tree x = exp;
4349 while (1)
4350 switch (TREE_CODE (x))
4352 case COMPONENT_REF:
4353 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4355 error
4356 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4357 return false;
4360 /* ... fall through ... */
4362 case ADDR_EXPR:
4363 case ARRAY_REF:
4364 case REALPART_EXPR:
4365 case IMAGPART_EXPR:
4366 x = TREE_OPERAND (x, 0);
4367 break;
4369 case COMPOUND_LITERAL_EXPR:
4370 case CONSTRUCTOR:
4371 TREE_ADDRESSABLE (x) = 1;
4372 return true;
4374 case VAR_DECL:
4375 case CONST_DECL:
4376 case PARM_DECL:
4377 case RESULT_DECL:
4378 if (C_DECL_REGISTER (x)
4379 && DECL_NONLOCAL (x))
4381 if (TREE_PUBLIC (x) || is_global_var (x))
4383 error
4384 ("global register variable %qD used in nested function", x);
4385 return false;
4387 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4389 else if (C_DECL_REGISTER (x))
4391 if (TREE_PUBLIC (x) || is_global_var (x))
4392 error ("address of global register variable %qD requested", x);
4393 else
4394 error ("address of register variable %qD requested", x);
4395 return false;
4398 /* drops in */
4399 case FUNCTION_DECL:
4400 TREE_ADDRESSABLE (x) = 1;
4401 /* drops out */
4402 default:
4403 return true;
4407 /* Convert EXPR to TYPE, warning about conversion problems with
4408 constants. SEMANTIC_TYPE is the type this conversion would use
4409 without excess precision. If SEMANTIC_TYPE is NULL, this function
4410 is equivalent to convert_and_check. This function is a wrapper that
4411 handles conversions that may be different than
4412 the usual ones because of excess precision. */
4414 static tree
4415 ep_convert_and_check (location_t loc, tree type, tree expr,
4416 tree semantic_type)
4418 if (TREE_TYPE (expr) == type)
4419 return expr;
4421 if (!semantic_type)
4422 return convert_and_check (loc, type, expr);
4424 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4425 && TREE_TYPE (expr) != semantic_type)
4427 /* For integers, we need to check the real conversion, not
4428 the conversion to the excess precision type. */
4429 expr = convert_and_check (loc, semantic_type, expr);
4431 /* Result type is the excess precision type, which should be
4432 large enough, so do not check. */
4433 return convert (type, expr);
4436 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4437 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4438 if folded to an integer constant then the unselected half may
4439 contain arbitrary operations not normally permitted in constant
4440 expressions. Set the location of the expression to LOC. */
4442 tree
4443 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4444 tree op1, tree op1_original_type, tree op2,
4445 tree op2_original_type)
4447 tree type1;
4448 tree type2;
4449 enum tree_code code1;
4450 enum tree_code code2;
4451 tree result_type = NULL;
4452 tree semantic_result_type = NULL;
4453 tree orig_op1 = op1, orig_op2 = op2;
4454 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4455 bool ifexp_int_operands;
4456 tree ret;
4458 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4459 if (op1_int_operands)
4460 op1 = remove_c_maybe_const_expr (op1);
4461 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4462 if (op2_int_operands)
4463 op2 = remove_c_maybe_const_expr (op2);
4464 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4465 if (ifexp_int_operands)
4466 ifexp = remove_c_maybe_const_expr (ifexp);
4468 /* Promote both alternatives. */
4470 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4471 op1 = default_conversion (op1);
4472 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4473 op2 = default_conversion (op2);
4475 if (TREE_CODE (ifexp) == ERROR_MARK
4476 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4477 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4478 return error_mark_node;
4480 type1 = TREE_TYPE (op1);
4481 code1 = TREE_CODE (type1);
4482 type2 = TREE_TYPE (op2);
4483 code2 = TREE_CODE (type2);
4485 /* C90 does not permit non-lvalue arrays in conditional expressions.
4486 In C99 they will be pointers by now. */
4487 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4489 error_at (colon_loc, "non-lvalue array in conditional expression");
4490 return error_mark_node;
4493 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4494 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4495 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4496 || code1 == COMPLEX_TYPE)
4497 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4498 || code2 == COMPLEX_TYPE))
4500 semantic_result_type = c_common_type (type1, type2);
4501 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4503 op1 = TREE_OPERAND (op1, 0);
4504 type1 = TREE_TYPE (op1);
4505 gcc_assert (TREE_CODE (type1) == code1);
4507 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4509 op2 = TREE_OPERAND (op2, 0);
4510 type2 = TREE_TYPE (op2);
4511 gcc_assert (TREE_CODE (type2) == code2);
4515 if (warn_cxx_compat)
4517 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4518 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4520 if (TREE_CODE (t1) == ENUMERAL_TYPE
4521 && TREE_CODE (t2) == ENUMERAL_TYPE
4522 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4523 warning_at (colon_loc, OPT_Wc___compat,
4524 ("different enum types in conditional is "
4525 "invalid in C++: %qT vs %qT"),
4526 t1, t2);
4529 /* Quickly detect the usual case where op1 and op2 have the same type
4530 after promotion. */
4531 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4533 if (type1 == type2)
4534 result_type = type1;
4535 else
4536 result_type = TYPE_MAIN_VARIANT (type1);
4538 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4539 || code1 == COMPLEX_TYPE)
4540 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4541 || code2 == COMPLEX_TYPE))
4543 result_type = c_common_type (type1, type2);
4544 do_warn_double_promotion (result_type, type1, type2,
4545 "implicit conversion from %qT to %qT to "
4546 "match other result of conditional",
4547 colon_loc);
4549 /* If -Wsign-compare, warn here if type1 and type2 have
4550 different signedness. We'll promote the signed to unsigned
4551 and later code won't know it used to be different.
4552 Do this check on the original types, so that explicit casts
4553 will be considered, but default promotions won't. */
4554 if (c_inhibit_evaluation_warnings == 0)
4556 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4557 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4559 if (unsigned_op1 ^ unsigned_op2)
4561 bool ovf;
4563 /* Do not warn if the result type is signed, since the
4564 signed type will only be chosen if it can represent
4565 all the values of the unsigned type. */
4566 if (!TYPE_UNSIGNED (result_type))
4567 /* OK */;
4568 else
4570 bool op1_maybe_const = true;
4571 bool op2_maybe_const = true;
4573 /* Do not warn if the signed quantity is an
4574 unsuffixed integer literal (or some static
4575 constant expression involving such literals) and
4576 it is non-negative. This warning requires the
4577 operands to be folded for best results, so do
4578 that folding in this case even without
4579 warn_sign_compare to avoid warning options
4580 possibly affecting code generation. */
4581 c_inhibit_evaluation_warnings
4582 += (ifexp == truthvalue_false_node);
4583 op1 = c_fully_fold (op1, require_constant_value,
4584 &op1_maybe_const);
4585 c_inhibit_evaluation_warnings
4586 -= (ifexp == truthvalue_false_node);
4588 c_inhibit_evaluation_warnings
4589 += (ifexp == truthvalue_true_node);
4590 op2 = c_fully_fold (op2, require_constant_value,
4591 &op2_maybe_const);
4592 c_inhibit_evaluation_warnings
4593 -= (ifexp == truthvalue_true_node);
4595 if (warn_sign_compare)
4597 if ((unsigned_op2
4598 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4599 || (unsigned_op1
4600 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4601 /* OK */;
4602 else
4603 warning_at (colon_loc, OPT_Wsign_compare,
4604 ("signed and unsigned type in "
4605 "conditional expression"));
4607 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4608 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4609 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4610 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4615 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4617 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4618 pedwarn (colon_loc, OPT_Wpedantic,
4619 "ISO C forbids conditional expr with only one void side");
4620 result_type = void_type_node;
4622 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4624 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4625 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4626 addr_space_t as_common;
4628 if (comp_target_types (colon_loc, type1, type2))
4629 result_type = common_pointer_type (type1, type2);
4630 else if (null_pointer_constant_p (orig_op1))
4631 result_type = type2;
4632 else if (null_pointer_constant_p (orig_op2))
4633 result_type = type1;
4634 else if (!addr_space_superset (as1, as2, &as_common))
4636 error_at (colon_loc, "pointers to disjoint address spaces "
4637 "used in conditional expression");
4638 return error_mark_node;
4640 else if (VOID_TYPE_P (TREE_TYPE (type1))
4641 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4643 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4644 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4645 & ~TYPE_QUALS (TREE_TYPE (type1))))
4646 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4647 "pointer to array loses qualifier "
4648 "in conditional expression");
4650 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4651 pedwarn (colon_loc, OPT_Wpedantic,
4652 "ISO C forbids conditional expr between "
4653 "%<void *%> and function pointer");
4654 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4655 TREE_TYPE (type2)));
4657 else if (VOID_TYPE_P (TREE_TYPE (type2))
4658 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4660 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4661 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4662 & ~TYPE_QUALS (TREE_TYPE (type2))))
4663 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4664 "pointer to array loses qualifier "
4665 "in conditional expression");
4667 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4668 pedwarn (colon_loc, OPT_Wpedantic,
4669 "ISO C forbids conditional expr between "
4670 "%<void *%> and function pointer");
4671 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4672 TREE_TYPE (type1)));
4674 /* Objective-C pointer comparisons are a bit more lenient. */
4675 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4676 result_type = objc_common_type (type1, type2);
4677 else
4679 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4681 pedwarn (colon_loc, 0,
4682 "pointer type mismatch in conditional expression");
4683 result_type = build_pointer_type
4684 (build_qualified_type (void_type_node, qual));
4687 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4689 if (!null_pointer_constant_p (orig_op2))
4690 pedwarn (colon_loc, 0,
4691 "pointer/integer type mismatch in conditional expression");
4692 else
4694 op2 = null_pointer_node;
4696 result_type = type1;
4698 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4700 if (!null_pointer_constant_p (orig_op1))
4701 pedwarn (colon_loc, 0,
4702 "pointer/integer type mismatch in conditional expression");
4703 else
4705 op1 = null_pointer_node;
4707 result_type = type2;
4710 if (!result_type)
4712 if (flag_cond_mismatch)
4713 result_type = void_type_node;
4714 else
4716 error_at (colon_loc, "type mismatch in conditional expression");
4717 return error_mark_node;
4721 /* Merge const and volatile flags of the incoming types. */
4722 result_type
4723 = build_type_variant (result_type,
4724 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4725 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4727 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4728 semantic_result_type);
4729 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4730 semantic_result_type);
4732 if (ifexp_bcp && ifexp == truthvalue_true_node)
4734 op2_int_operands = true;
4735 op1 = c_fully_fold (op1, require_constant_value, NULL);
4737 if (ifexp_bcp && ifexp == truthvalue_false_node)
4739 op1_int_operands = true;
4740 op2 = c_fully_fold (op2, require_constant_value, NULL);
4742 int_const = int_operands = (ifexp_int_operands
4743 && op1_int_operands
4744 && op2_int_operands);
4745 if (int_operands)
4747 int_const = ((ifexp == truthvalue_true_node
4748 && TREE_CODE (orig_op1) == INTEGER_CST
4749 && !TREE_OVERFLOW (orig_op1))
4750 || (ifexp == truthvalue_false_node
4751 && TREE_CODE (orig_op2) == INTEGER_CST
4752 && !TREE_OVERFLOW (orig_op2)));
4754 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4755 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4756 else
4758 if (int_operands)
4760 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4761 nested inside of the expression. */
4762 op1 = c_fully_fold (op1, false, NULL);
4763 op2 = c_fully_fold (op2, false, NULL);
4765 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4766 if (int_operands)
4767 ret = note_integer_operands (ret);
4769 if (semantic_result_type)
4770 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4772 protected_set_expr_location (ret, colon_loc);
4773 return ret;
4776 /* Return a compound expression that performs two expressions and
4777 returns the value of the second of them.
4779 LOC is the location of the COMPOUND_EXPR. */
4781 tree
4782 build_compound_expr (location_t loc, tree expr1, tree expr2)
4784 bool expr1_int_operands, expr2_int_operands;
4785 tree eptype = NULL_TREE;
4786 tree ret;
4788 if (flag_cilkplus
4789 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4790 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4792 error_at (loc,
4793 "spawned function call cannot be part of a comma expression");
4794 return error_mark_node;
4796 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4797 if (expr1_int_operands)
4798 expr1 = remove_c_maybe_const_expr (expr1);
4799 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4800 if (expr2_int_operands)
4801 expr2 = remove_c_maybe_const_expr (expr2);
4803 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4804 expr1 = TREE_OPERAND (expr1, 0);
4805 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4807 eptype = TREE_TYPE (expr2);
4808 expr2 = TREE_OPERAND (expr2, 0);
4811 if (!TREE_SIDE_EFFECTS (expr1))
4813 /* The left-hand operand of a comma expression is like an expression
4814 statement: with -Wunused, we should warn if it doesn't have
4815 any side-effects, unless it was explicitly cast to (void). */
4816 if (warn_unused_value)
4818 if (VOID_TYPE_P (TREE_TYPE (expr1))
4819 && CONVERT_EXPR_P (expr1))
4820 ; /* (void) a, b */
4821 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4822 && TREE_CODE (expr1) == COMPOUND_EXPR
4823 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4824 ; /* (void) a, (void) b, c */
4825 else
4826 warning_at (loc, OPT_Wunused_value,
4827 "left-hand operand of comma expression has no effect");
4830 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4831 && warn_unused_value)
4833 tree r = expr1;
4834 location_t cloc = loc;
4835 while (TREE_CODE (r) == COMPOUND_EXPR)
4837 if (EXPR_HAS_LOCATION (r))
4838 cloc = EXPR_LOCATION (r);
4839 r = TREE_OPERAND (r, 1);
4841 if (!TREE_SIDE_EFFECTS (r)
4842 && !VOID_TYPE_P (TREE_TYPE (r))
4843 && !CONVERT_EXPR_P (r))
4844 warning_at (cloc, OPT_Wunused_value,
4845 "right-hand operand of comma expression has no effect");
4848 /* With -Wunused, we should also warn if the left-hand operand does have
4849 side-effects, but computes a value which is not used. For example, in
4850 `foo() + bar(), baz()' the result of the `+' operator is not used,
4851 so we should issue a warning. */
4852 else if (warn_unused_value)
4853 warn_if_unused_value (expr1, loc);
4855 if (expr2 == error_mark_node)
4856 return error_mark_node;
4858 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4860 if (flag_isoc99
4861 && expr1_int_operands
4862 && expr2_int_operands)
4863 ret = note_integer_operands (ret);
4865 if (eptype)
4866 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4868 protected_set_expr_location (ret, loc);
4869 return ret;
4872 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4873 which we are casting. OTYPE is the type of the expression being
4874 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4875 of the cast. -Wcast-qual appeared on the command line. Named
4876 address space qualifiers are not handled here, because they result
4877 in different warnings. */
4879 static void
4880 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4882 tree in_type = type;
4883 tree in_otype = otype;
4884 int added = 0;
4885 int discarded = 0;
4886 bool is_const;
4888 /* Check that the qualifiers on IN_TYPE are a superset of the
4889 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4890 nodes is uninteresting and we stop as soon as we hit a
4891 non-POINTER_TYPE node on either type. */
4894 in_otype = TREE_TYPE (in_otype);
4895 in_type = TREE_TYPE (in_type);
4897 /* GNU C allows cv-qualified function types. 'const' means the
4898 function is very pure, 'volatile' means it can't return. We
4899 need to warn when such qualifiers are added, not when they're
4900 taken away. */
4901 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4902 && TREE_CODE (in_type) == FUNCTION_TYPE)
4903 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4904 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4905 else
4906 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4907 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4909 while (TREE_CODE (in_type) == POINTER_TYPE
4910 && TREE_CODE (in_otype) == POINTER_TYPE);
4912 if (added)
4913 warning_at (loc, OPT_Wcast_qual,
4914 "cast adds %q#v qualifier to function type", added);
4916 if (discarded)
4917 /* There are qualifiers present in IN_OTYPE that are not present
4918 in IN_TYPE. */
4919 warning_at (loc, OPT_Wcast_qual,
4920 "cast discards %qv qualifier from pointer target type",
4921 discarded);
4923 if (added || discarded)
4924 return;
4926 /* A cast from **T to const **T is unsafe, because it can cause a
4927 const value to be changed with no additional warning. We only
4928 issue this warning if T is the same on both sides, and we only
4929 issue the warning if there are the same number of pointers on
4930 both sides, as otherwise the cast is clearly unsafe anyhow. A
4931 cast is unsafe when a qualifier is added at one level and const
4932 is not present at all outer levels.
4934 To issue this warning, we check at each level whether the cast
4935 adds new qualifiers not already seen. We don't need to special
4936 case function types, as they won't have the same
4937 TYPE_MAIN_VARIANT. */
4939 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4940 return;
4941 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4942 return;
4944 in_type = type;
4945 in_otype = otype;
4946 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4949 in_type = TREE_TYPE (in_type);
4950 in_otype = TREE_TYPE (in_otype);
4951 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4952 && !is_const)
4954 warning_at (loc, OPT_Wcast_qual,
4955 "to be safe all intermediate pointers in cast from "
4956 "%qT to %qT must be %<const%> qualified",
4957 otype, type);
4958 break;
4960 if (is_const)
4961 is_const = TYPE_READONLY (in_type);
4963 while (TREE_CODE (in_type) == POINTER_TYPE);
4966 /* Build an expression representing a cast to type TYPE of expression EXPR.
4967 LOC is the location of the cast-- typically the open paren of the cast. */
4969 tree
4970 build_c_cast (location_t loc, tree type, tree expr)
4972 tree value;
4974 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4975 expr = TREE_OPERAND (expr, 0);
4977 value = expr;
4979 if (type == error_mark_node || expr == error_mark_node)
4980 return error_mark_node;
4982 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4983 only in <protocol> qualifications. But when constructing cast expressions,
4984 the protocols do matter and must be kept around. */
4985 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4986 return build1 (NOP_EXPR, type, expr);
4988 type = TYPE_MAIN_VARIANT (type);
4990 if (TREE_CODE (type) == ARRAY_TYPE)
4992 error_at (loc, "cast specifies array type");
4993 return error_mark_node;
4996 if (TREE_CODE (type) == FUNCTION_TYPE)
4998 error_at (loc, "cast specifies function type");
4999 return error_mark_node;
5002 if (!VOID_TYPE_P (type))
5004 value = require_complete_type (value);
5005 if (value == error_mark_node)
5006 return error_mark_node;
5009 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5011 if (TREE_CODE (type) == RECORD_TYPE
5012 || TREE_CODE (type) == UNION_TYPE)
5013 pedwarn (loc, OPT_Wpedantic,
5014 "ISO C forbids casting nonscalar to the same type");
5016 /* Convert to remove any qualifiers from VALUE's type. */
5017 value = convert (type, value);
5019 else if (TREE_CODE (type) == UNION_TYPE)
5021 tree field;
5023 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5024 if (TREE_TYPE (field) != error_mark_node
5025 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5026 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5027 break;
5029 if (field)
5031 tree t;
5032 bool maybe_const = true;
5034 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5035 t = c_fully_fold (value, false, &maybe_const);
5036 t = build_constructor_single (type, field, t);
5037 if (!maybe_const)
5038 t = c_wrap_maybe_const (t, true);
5039 t = digest_init (loc, type, t,
5040 NULL_TREE, false, true, 0);
5041 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5042 return t;
5044 error_at (loc, "cast to union type from type not present in union");
5045 return error_mark_node;
5047 else
5049 tree otype, ovalue;
5051 if (type == void_type_node)
5053 tree t = build1 (CONVERT_EXPR, type, value);
5054 SET_EXPR_LOCATION (t, loc);
5055 return t;
5058 otype = TREE_TYPE (value);
5060 /* Optionally warn about potentially worrisome casts. */
5061 if (warn_cast_qual
5062 && TREE_CODE (type) == POINTER_TYPE
5063 && TREE_CODE (otype) == POINTER_TYPE)
5064 handle_warn_cast_qual (loc, type, otype);
5066 /* Warn about conversions between pointers to disjoint
5067 address spaces. */
5068 if (TREE_CODE (type) == POINTER_TYPE
5069 && TREE_CODE (otype) == POINTER_TYPE
5070 && !null_pointer_constant_p (value))
5072 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5073 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5074 addr_space_t as_common;
5076 if (!addr_space_superset (as_to, as_from, &as_common))
5078 if (ADDR_SPACE_GENERIC_P (as_from))
5079 warning_at (loc, 0, "cast to %s address space pointer "
5080 "from disjoint generic address space pointer",
5081 c_addr_space_name (as_to));
5083 else if (ADDR_SPACE_GENERIC_P (as_to))
5084 warning_at (loc, 0, "cast to generic address space pointer "
5085 "from disjoint %s address space pointer",
5086 c_addr_space_name (as_from));
5088 else
5089 warning_at (loc, 0, "cast to %s address space pointer "
5090 "from disjoint %s address space pointer",
5091 c_addr_space_name (as_to),
5092 c_addr_space_name (as_from));
5096 /* Warn about possible alignment problems. */
5097 if (STRICT_ALIGNMENT
5098 && TREE_CODE (type) == POINTER_TYPE
5099 && TREE_CODE (otype) == POINTER_TYPE
5100 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5101 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5102 /* Don't warn about opaque types, where the actual alignment
5103 restriction is unknown. */
5104 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5105 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5106 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5107 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5108 warning_at (loc, OPT_Wcast_align,
5109 "cast increases required alignment of target type");
5111 if (TREE_CODE (type) == INTEGER_TYPE
5112 && TREE_CODE (otype) == POINTER_TYPE
5113 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5114 /* Unlike conversion of integers to pointers, where the
5115 warning is disabled for converting constants because
5116 of cases such as SIG_*, warn about converting constant
5117 pointers to integers. In some cases it may cause unwanted
5118 sign extension, and a warning is appropriate. */
5119 warning_at (loc, OPT_Wpointer_to_int_cast,
5120 "cast from pointer to integer of different size");
5122 if (TREE_CODE (value) == CALL_EXPR
5123 && TREE_CODE (type) != TREE_CODE (otype))
5124 warning_at (loc, OPT_Wbad_function_cast,
5125 "cast from function call of type %qT "
5126 "to non-matching type %qT", otype, type);
5128 if (TREE_CODE (type) == POINTER_TYPE
5129 && TREE_CODE (otype) == INTEGER_TYPE
5130 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5131 /* Don't warn about converting any constant. */
5132 && !TREE_CONSTANT (value))
5133 warning_at (loc,
5134 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5135 "of different size");
5137 if (warn_strict_aliasing <= 2)
5138 strict_aliasing_warning (otype, type, expr);
5140 /* If pedantic, warn for conversions between function and object
5141 pointer types, except for converting a null pointer constant
5142 to function pointer type. */
5143 if (pedantic
5144 && TREE_CODE (type) == POINTER_TYPE
5145 && TREE_CODE (otype) == POINTER_TYPE
5146 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5147 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5148 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5149 "conversion of function pointer to object pointer type");
5151 if (pedantic
5152 && TREE_CODE (type) == POINTER_TYPE
5153 && TREE_CODE (otype) == POINTER_TYPE
5154 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5155 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5156 && !null_pointer_constant_p (value))
5157 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5158 "conversion of object pointer to function pointer type");
5160 ovalue = value;
5161 value = convert (type, value);
5163 /* Ignore any integer overflow caused by the cast. */
5164 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5166 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5168 if (!TREE_OVERFLOW (value))
5170 /* Avoid clobbering a shared constant. */
5171 value = copy_node (value);
5172 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5175 else if (TREE_OVERFLOW (value))
5176 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5177 value = wide_int_to_tree (TREE_TYPE (value), value);
5181 /* Don't let a cast be an lvalue. */
5182 if (lvalue_p (value))
5183 value = non_lvalue_loc (loc, value);
5185 /* Don't allow the results of casting to floating-point or complex
5186 types be confused with actual constants, or casts involving
5187 integer and pointer types other than direct integer-to-integer
5188 and integer-to-pointer be confused with integer constant
5189 expressions and null pointer constants. */
5190 if (TREE_CODE (value) == REAL_CST
5191 || TREE_CODE (value) == COMPLEX_CST
5192 || (TREE_CODE (value) == INTEGER_CST
5193 && !((TREE_CODE (expr) == INTEGER_CST
5194 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5195 || TREE_CODE (expr) == REAL_CST
5196 || TREE_CODE (expr) == COMPLEX_CST)))
5197 value = build1 (NOP_EXPR, type, value);
5199 if (CAN_HAVE_LOCATION_P (value))
5200 SET_EXPR_LOCATION (value, loc);
5201 return value;
5204 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5205 location of the open paren of the cast, or the position of the cast
5206 expr. */
5207 tree
5208 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5210 tree type;
5211 tree type_expr = NULL_TREE;
5212 bool type_expr_const = true;
5213 tree ret;
5214 int saved_wsp = warn_strict_prototypes;
5216 /* This avoids warnings about unprototyped casts on
5217 integers. E.g. "#define SIG_DFL (void(*)())0". */
5218 if (TREE_CODE (expr) == INTEGER_CST)
5219 warn_strict_prototypes = 0;
5220 type = groktypename (type_name, &type_expr, &type_expr_const);
5221 warn_strict_prototypes = saved_wsp;
5223 ret = build_c_cast (loc, type, expr);
5224 if (type_expr)
5226 bool inner_expr_const = true;
5227 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5228 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5229 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5230 && inner_expr_const);
5231 SET_EXPR_LOCATION (ret, loc);
5234 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5235 SET_EXPR_LOCATION (ret, loc);
5237 /* C++ does not permits types to be defined in a cast, but it
5238 allows references to incomplete types. */
5239 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5240 warning_at (loc, OPT_Wc___compat,
5241 "defining a type in a cast is invalid in C++");
5243 return ret;
5246 /* Build an assignment expression of lvalue LHS from value RHS.
5247 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5248 may differ from TREE_TYPE (LHS) for an enum bitfield.
5249 MODIFYCODE is the code for a binary operator that we use
5250 to combine the old value of LHS with RHS to get the new value.
5251 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5252 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5253 which may differ from TREE_TYPE (RHS) for an enum value.
5255 LOCATION is the location of the MODIFYCODE operator.
5256 RHS_LOC is the location of the RHS. */
5258 tree
5259 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5260 enum tree_code modifycode,
5261 location_t rhs_loc, tree rhs, tree rhs_origtype)
5263 tree result;
5264 tree newrhs;
5265 tree rhseval = NULL_TREE;
5266 tree rhs_semantic_type = NULL_TREE;
5267 tree lhstype = TREE_TYPE (lhs);
5268 tree olhstype = lhstype;
5269 bool npc;
5270 bool is_atomic_op;
5272 /* Types that aren't fully specified cannot be used in assignments. */
5273 lhs = require_complete_type (lhs);
5275 /* Avoid duplicate error messages from operands that had errors. */
5276 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5277 return error_mark_node;
5279 /* Ensure an error for assigning a non-lvalue array to an array in
5280 C90. */
5281 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5283 error_at (location, "assignment to expression with array type");
5284 return error_mark_node;
5287 /* For ObjC properties, defer this check. */
5288 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5289 return error_mark_node;
5291 is_atomic_op = really_atomic_lvalue (lhs);
5293 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5295 rhs_semantic_type = TREE_TYPE (rhs);
5296 rhs = TREE_OPERAND (rhs, 0);
5299 newrhs = rhs;
5301 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5303 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5304 lhs_origtype, modifycode, rhs_loc, rhs,
5305 rhs_origtype);
5306 if (inner == error_mark_node)
5307 return error_mark_node;
5308 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5309 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5310 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5311 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5312 protected_set_expr_location (result, location);
5313 return result;
5316 /* If a binary op has been requested, combine the old LHS value with the RHS
5317 producing the value we should actually store into the LHS. */
5319 if (modifycode != NOP_EXPR)
5321 lhs = c_fully_fold (lhs, false, NULL);
5322 lhs = stabilize_reference (lhs);
5324 /* Construct the RHS for any non-atomic compound assignemnt. */
5325 if (!is_atomic_op)
5327 /* If in LHS op= RHS the RHS has side-effects, ensure they
5328 are preevaluated before the rest of the assignment expression's
5329 side-effects, because RHS could contain e.g. function calls
5330 that modify LHS. */
5331 if (TREE_SIDE_EFFECTS (rhs))
5333 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5334 rhseval = newrhs;
5336 newrhs = build_binary_op (location,
5337 modifycode, lhs, newrhs, 1);
5339 /* The original type of the right hand side is no longer
5340 meaningful. */
5341 rhs_origtype = NULL_TREE;
5345 if (c_dialect_objc ())
5347 /* Check if we are modifying an Objective-C property reference;
5348 if so, we need to generate setter calls. */
5349 result = objc_maybe_build_modify_expr (lhs, newrhs);
5350 if (result)
5351 goto return_result;
5353 /* Else, do the check that we postponed for Objective-C. */
5354 if (!lvalue_or_else (location, lhs, lv_assign))
5355 return error_mark_node;
5358 /* Give an error for storing in something that is 'const'. */
5360 if (TYPE_READONLY (lhstype)
5361 || ((TREE_CODE (lhstype) == RECORD_TYPE
5362 || TREE_CODE (lhstype) == UNION_TYPE)
5363 && C_TYPE_FIELDS_READONLY (lhstype)))
5365 readonly_error (location, lhs, lv_assign);
5366 return error_mark_node;
5368 else if (TREE_READONLY (lhs))
5369 readonly_warning (lhs, lv_assign);
5371 /* If storing into a structure or union member,
5372 it has probably been given type `int'.
5373 Compute the type that would go with
5374 the actual amount of storage the member occupies. */
5376 if (TREE_CODE (lhs) == COMPONENT_REF
5377 && (TREE_CODE (lhstype) == INTEGER_TYPE
5378 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5379 || TREE_CODE (lhstype) == REAL_TYPE
5380 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5381 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5383 /* If storing in a field that is in actuality a short or narrower than one,
5384 we must store in the field in its actual type. */
5386 if (lhstype != TREE_TYPE (lhs))
5388 lhs = copy_node (lhs);
5389 TREE_TYPE (lhs) = lhstype;
5392 /* Issue -Wc++-compat warnings about an assignment to an enum type
5393 when LHS does not have its original type. This happens for,
5394 e.g., an enum bitfield in a struct. */
5395 if (warn_cxx_compat
5396 && lhs_origtype != NULL_TREE
5397 && lhs_origtype != lhstype
5398 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5400 tree checktype = (rhs_origtype != NULL_TREE
5401 ? rhs_origtype
5402 : TREE_TYPE (rhs));
5403 if (checktype != error_mark_node
5404 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5405 || (is_atomic_op && modifycode != NOP_EXPR)))
5406 warning_at (location, OPT_Wc___compat,
5407 "enum conversion in assignment is invalid in C++");
5410 /* If the lhs is atomic, remove that qualifier. */
5411 if (is_atomic_op)
5413 lhstype = build_qualified_type (lhstype,
5414 (TYPE_QUALS (lhstype)
5415 & ~TYPE_QUAL_ATOMIC));
5416 olhstype = build_qualified_type (olhstype,
5417 (TYPE_QUALS (lhstype)
5418 & ~TYPE_QUAL_ATOMIC));
5421 /* Convert new value to destination type. Fold it first, then
5422 restore any excess precision information, for the sake of
5423 conversion warnings. */
5425 if (!(is_atomic_op && modifycode != NOP_EXPR))
5427 npc = null_pointer_constant_p (newrhs);
5428 newrhs = c_fully_fold (newrhs, false, NULL);
5429 if (rhs_semantic_type)
5430 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5431 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5432 rhs_origtype, ic_assign, npc,
5433 NULL_TREE, NULL_TREE, 0);
5434 if (TREE_CODE (newrhs) == ERROR_MARK)
5435 return error_mark_node;
5438 /* Emit ObjC write barrier, if necessary. */
5439 if (c_dialect_objc () && flag_objc_gc)
5441 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5442 if (result)
5444 protected_set_expr_location (result, location);
5445 goto return_result;
5449 /* Scan operands. */
5451 if (is_atomic_op)
5452 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5453 else
5455 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5456 TREE_SIDE_EFFECTS (result) = 1;
5457 protected_set_expr_location (result, location);
5460 /* If we got the LHS in a different type for storing in,
5461 convert the result back to the nominal type of LHS
5462 so that the value we return always has the same type
5463 as the LHS argument. */
5465 if (olhstype == TREE_TYPE (result))
5466 goto return_result;
5468 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5469 rhs_origtype, ic_assign, false, NULL_TREE,
5470 NULL_TREE, 0);
5471 protected_set_expr_location (result, location);
5473 return_result:
5474 if (rhseval)
5475 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5476 return result;
5479 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5480 This is used to implement -fplan9-extensions. */
5482 static bool
5483 find_anonymous_field_with_type (tree struct_type, tree type)
5485 tree field;
5486 bool found;
5488 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5489 || TREE_CODE (struct_type) == UNION_TYPE);
5490 found = false;
5491 for (field = TYPE_FIELDS (struct_type);
5492 field != NULL_TREE;
5493 field = TREE_CHAIN (field))
5495 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5496 ? c_build_qualified_type (TREE_TYPE (field),
5497 TYPE_QUAL_ATOMIC)
5498 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5499 if (DECL_NAME (field) == NULL
5500 && comptypes (type, fieldtype))
5502 if (found)
5503 return false;
5504 found = true;
5506 else if (DECL_NAME (field) == NULL
5507 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5508 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5509 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5511 if (found)
5512 return false;
5513 found = true;
5516 return found;
5519 /* RHS is an expression whose type is pointer to struct. If there is
5520 an anonymous field in RHS with type TYPE, then return a pointer to
5521 that field in RHS. This is used with -fplan9-extensions. This
5522 returns NULL if no conversion could be found. */
5524 static tree
5525 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5527 tree rhs_struct_type, lhs_main_type;
5528 tree field, found_field;
5529 bool found_sub_field;
5530 tree ret;
5532 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5533 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5534 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5535 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5537 gcc_assert (POINTER_TYPE_P (type));
5538 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5539 ? c_build_qualified_type (TREE_TYPE (type),
5540 TYPE_QUAL_ATOMIC)
5541 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5543 found_field = NULL_TREE;
5544 found_sub_field = false;
5545 for (field = TYPE_FIELDS (rhs_struct_type);
5546 field != NULL_TREE;
5547 field = TREE_CHAIN (field))
5549 if (DECL_NAME (field) != NULL_TREE
5550 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5551 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5552 continue;
5553 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5554 ? c_build_qualified_type (TREE_TYPE (field),
5555 TYPE_QUAL_ATOMIC)
5556 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5557 if (comptypes (lhs_main_type, fieldtype))
5559 if (found_field != NULL_TREE)
5560 return NULL_TREE;
5561 found_field = field;
5563 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5564 lhs_main_type))
5566 if (found_field != NULL_TREE)
5567 return NULL_TREE;
5568 found_field = field;
5569 found_sub_field = true;
5573 if (found_field == NULL_TREE)
5574 return NULL_TREE;
5576 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5577 build_fold_indirect_ref (rhs), found_field,
5578 NULL_TREE);
5579 ret = build_fold_addr_expr_loc (location, ret);
5581 if (found_sub_field)
5583 ret = convert_to_anonymous_field (location, type, ret);
5584 gcc_assert (ret != NULL_TREE);
5587 return ret;
5590 /* Issue an error message for a bad initializer component.
5591 GMSGID identifies the message.
5592 The component name is taken from the spelling stack. */
5594 static void
5595 error_init (location_t loc, const char *gmsgid)
5597 char *ofwhat;
5599 /* The gmsgid may be a format string with %< and %>. */
5600 error_at (loc, gmsgid);
5601 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5602 if (*ofwhat)
5603 inform (loc, "(near initialization for %qs)", ofwhat);
5606 /* Issue a pedantic warning for a bad initializer component. OPT is
5607 the option OPT_* (from options.h) controlling this warning or 0 if
5608 it is unconditionally given. GMSGID identifies the message. The
5609 component name is taken from the spelling stack. */
5611 static void
5612 pedwarn_init (location_t location, int opt, const char *gmsgid)
5614 char *ofwhat;
5615 bool warned;
5617 /* The gmsgid may be a format string with %< and %>. */
5618 warned = pedwarn (location, opt, gmsgid);
5619 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5620 if (*ofwhat && warned)
5621 inform (location, "(near initialization for %qs)", ofwhat);
5624 /* Issue a warning for a bad initializer component.
5626 OPT is the OPT_W* value corresponding to the warning option that
5627 controls this warning. GMSGID identifies the message. The
5628 component name is taken from the spelling stack. */
5630 static void
5631 warning_init (location_t loc, int opt, const char *gmsgid)
5633 char *ofwhat;
5634 bool warned;
5636 /* The gmsgid may be a format string with %< and %>. */
5637 warned = warning_at (loc, opt, gmsgid);
5638 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5639 if (*ofwhat && warned)
5640 inform (loc, "(near initialization for %qs)", ofwhat);
5643 /* If TYPE is an array type and EXPR is a parenthesized string
5644 constant, warn if pedantic that EXPR is being used to initialize an
5645 object of type TYPE. */
5647 void
5648 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5650 if (pedantic
5651 && TREE_CODE (type) == ARRAY_TYPE
5652 && TREE_CODE (expr.value) == STRING_CST
5653 && expr.original_code != STRING_CST)
5654 pedwarn_init (loc, OPT_Wpedantic,
5655 "array initialized from parenthesized string constant");
5658 /* Convert value RHS to type TYPE as preparation for an assignment to
5659 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5660 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5661 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5662 constant before any folding.
5663 The real work of conversion is done by `convert'.
5664 The purpose of this function is to generate error messages
5665 for assignments that are not allowed in C.
5666 ERRTYPE says whether it is argument passing, assignment,
5667 initialization or return.
5669 LOCATION is the location of the assignment, EXPR_LOC is the location of
5670 the RHS or, for a function, location of an argument.
5671 FUNCTION is a tree for the function being called.
5672 PARMNUM is the number of the argument, for printing in error messages. */
5674 static tree
5675 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5676 tree rhs, tree origtype, enum impl_conv errtype,
5677 bool null_pointer_constant, tree fundecl,
5678 tree function, int parmnum)
5680 enum tree_code codel = TREE_CODE (type);
5681 tree orig_rhs = rhs;
5682 tree rhstype;
5683 enum tree_code coder;
5684 tree rname = NULL_TREE;
5685 bool objc_ok = false;
5687 if (errtype == ic_argpass)
5689 tree selector;
5690 /* Change pointer to function to the function itself for
5691 diagnostics. */
5692 if (TREE_CODE (function) == ADDR_EXPR
5693 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5694 function = TREE_OPERAND (function, 0);
5696 /* Handle an ObjC selector specially for diagnostics. */
5697 selector = objc_message_selector ();
5698 rname = function;
5699 if (selector && parmnum > 2)
5701 rname = selector;
5702 parmnum -= 2;
5706 /* This macro is used to emit diagnostics to ensure that all format
5707 strings are complete sentences, visible to gettext and checked at
5708 compile time. */
5709 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5710 do { \
5711 switch (errtype) \
5713 case ic_argpass: \
5714 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5715 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5716 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5717 "expected %qT but argument is of type %qT", \
5718 type, rhstype); \
5719 break; \
5720 case ic_assign: \
5721 pedwarn (LOCATION, OPT, AS); \
5722 break; \
5723 case ic_init: \
5724 pedwarn_init (LOCATION, OPT, IN); \
5725 break; \
5726 case ic_return: \
5727 pedwarn (LOCATION, OPT, RE); \
5728 break; \
5729 default: \
5730 gcc_unreachable (); \
5732 } while (0)
5734 /* This macro is used to emit diagnostics to ensure that all format
5735 strings are complete sentences, visible to gettext and checked at
5736 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5737 extra parameter to enumerate qualifiers. */
5738 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5739 do { \
5740 switch (errtype) \
5742 case ic_argpass: \
5743 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5744 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5745 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5746 "expected %qT but argument is of type %qT", \
5747 type, rhstype); \
5748 break; \
5749 case ic_assign: \
5750 pedwarn (LOCATION, OPT, AS, QUALS); \
5751 break; \
5752 case ic_init: \
5753 pedwarn (LOCATION, OPT, IN, QUALS); \
5754 break; \
5755 case ic_return: \
5756 pedwarn (LOCATION, OPT, RE, QUALS); \
5757 break; \
5758 default: \
5759 gcc_unreachable (); \
5761 } while (0)
5763 /* This macro is used to emit diagnostics to ensure that all format
5764 strings are complete sentences, visible to gettext and checked at
5765 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5766 warning_at instead of pedwarn. */
5767 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5768 do { \
5769 switch (errtype) \
5771 case ic_argpass: \
5772 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5773 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5774 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5775 "expected %qT but argument is of type %qT", \
5776 type, rhstype); \
5777 break; \
5778 case ic_assign: \
5779 warning_at (LOCATION, OPT, AS, QUALS); \
5780 break; \
5781 case ic_init: \
5782 warning_at (LOCATION, OPT, IN, QUALS); \
5783 break; \
5784 case ic_return: \
5785 warning_at (LOCATION, OPT, RE, QUALS); \
5786 break; \
5787 default: \
5788 gcc_unreachable (); \
5790 } while (0)
5792 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5793 rhs = TREE_OPERAND (rhs, 0);
5795 rhstype = TREE_TYPE (rhs);
5796 coder = TREE_CODE (rhstype);
5798 if (coder == ERROR_MARK)
5799 return error_mark_node;
5801 if (c_dialect_objc ())
5803 int parmno;
5805 switch (errtype)
5807 case ic_return:
5808 parmno = 0;
5809 break;
5811 case ic_assign:
5812 parmno = -1;
5813 break;
5815 case ic_init:
5816 parmno = -2;
5817 break;
5819 default:
5820 parmno = parmnum;
5821 break;
5824 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5827 if (warn_cxx_compat)
5829 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5830 if (checktype != error_mark_node
5831 && TREE_CODE (type) == ENUMERAL_TYPE
5832 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5834 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5835 G_("enum conversion when passing argument "
5836 "%d of %qE is invalid in C++"),
5837 G_("enum conversion in assignment is "
5838 "invalid in C++"),
5839 G_("enum conversion in initialization is "
5840 "invalid in C++"),
5841 G_("enum conversion in return is "
5842 "invalid in C++"));
5846 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5847 return rhs;
5849 if (coder == VOID_TYPE)
5851 /* Except for passing an argument to an unprototyped function,
5852 this is a constraint violation. When passing an argument to
5853 an unprototyped function, it is compile-time undefined;
5854 making it a constraint in that case was rejected in
5855 DR#252. */
5856 error_at (location, "void value not ignored as it ought to be");
5857 return error_mark_node;
5859 rhs = require_complete_type (rhs);
5860 if (rhs == error_mark_node)
5861 return error_mark_node;
5862 /* A non-reference type can convert to a reference. This handles
5863 va_start, va_copy and possibly port built-ins. */
5864 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5866 if (!lvalue_p (rhs))
5868 error_at (location, "cannot pass rvalue to reference parameter");
5869 return error_mark_node;
5871 if (!c_mark_addressable (rhs))
5872 return error_mark_node;
5873 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5874 SET_EXPR_LOCATION (rhs, location);
5876 rhs = convert_for_assignment (location, expr_loc,
5877 build_pointer_type (TREE_TYPE (type)),
5878 rhs, origtype, errtype,
5879 null_pointer_constant, fundecl, function,
5880 parmnum);
5881 if (rhs == error_mark_node)
5882 return error_mark_node;
5884 rhs = build1 (NOP_EXPR, type, rhs);
5885 SET_EXPR_LOCATION (rhs, location);
5886 return rhs;
5888 /* Some types can interconvert without explicit casts. */
5889 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5890 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5891 return convert (type, rhs);
5892 /* Arithmetic types all interconvert, and enum is treated like int. */
5893 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5894 || codel == FIXED_POINT_TYPE
5895 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5896 || codel == BOOLEAN_TYPE)
5897 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5898 || coder == FIXED_POINT_TYPE
5899 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5900 || coder == BOOLEAN_TYPE))
5902 tree ret;
5903 bool save = in_late_binary_op;
5904 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5905 || (coder == REAL_TYPE
5906 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5907 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
5908 in_late_binary_op = true;
5909 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5910 ? expr_loc : location, type, orig_rhs);
5911 in_late_binary_op = save;
5912 return ret;
5915 /* Aggregates in different TUs might need conversion. */
5916 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5917 && codel == coder
5918 && comptypes (type, rhstype))
5919 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5920 ? expr_loc : location, type, rhs);
5922 /* Conversion to a transparent union or record from its member types.
5923 This applies only to function arguments. */
5924 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5925 && TYPE_TRANSPARENT_AGGR (type))
5926 && errtype == ic_argpass)
5928 tree memb, marginal_memb = NULL_TREE;
5930 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5932 tree memb_type = TREE_TYPE (memb);
5934 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5935 TYPE_MAIN_VARIANT (rhstype)))
5936 break;
5938 if (TREE_CODE (memb_type) != POINTER_TYPE)
5939 continue;
5941 if (coder == POINTER_TYPE)
5943 tree ttl = TREE_TYPE (memb_type);
5944 tree ttr = TREE_TYPE (rhstype);
5946 /* Any non-function converts to a [const][volatile] void *
5947 and vice versa; otherwise, targets must be the same.
5948 Meanwhile, the lhs target must have all the qualifiers of
5949 the rhs. */
5950 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5951 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5952 || comp_target_types (location, memb_type, rhstype))
5954 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5955 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5956 /* If this type won't generate any warnings, use it. */
5957 if (lquals == rquals
5958 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5959 && TREE_CODE (ttl) == FUNCTION_TYPE)
5960 ? ((lquals | rquals) == rquals)
5961 : ((lquals | rquals) == lquals)))
5962 break;
5964 /* Keep looking for a better type, but remember this one. */
5965 if (!marginal_memb)
5966 marginal_memb = memb;
5970 /* Can convert integer zero to any pointer type. */
5971 if (null_pointer_constant)
5973 rhs = null_pointer_node;
5974 break;
5978 if (memb || marginal_memb)
5980 if (!memb)
5982 /* We have only a marginally acceptable member type;
5983 it needs a warning. */
5984 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5985 tree ttr = TREE_TYPE (rhstype);
5987 /* Const and volatile mean something different for function
5988 types, so the usual warnings are not appropriate. */
5989 if (TREE_CODE (ttr) == FUNCTION_TYPE
5990 && TREE_CODE (ttl) == FUNCTION_TYPE)
5992 /* Because const and volatile on functions are
5993 restrictions that say the function will not do
5994 certain things, it is okay to use a const or volatile
5995 function where an ordinary one is wanted, but not
5996 vice-versa. */
5997 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5998 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5999 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6000 OPT_Wdiscarded_qualifiers,
6001 G_("passing argument %d of %qE "
6002 "makes %q#v qualified function "
6003 "pointer from unqualified"),
6004 G_("assignment makes %q#v qualified "
6005 "function pointer from "
6006 "unqualified"),
6007 G_("initialization makes %q#v qualified "
6008 "function pointer from "
6009 "unqualified"),
6010 G_("return makes %q#v qualified function "
6011 "pointer from unqualified"),
6012 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6014 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6015 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6016 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6017 OPT_Wdiscarded_qualifiers,
6018 G_("passing argument %d of %qE discards "
6019 "%qv qualifier from pointer target type"),
6020 G_("assignment discards %qv qualifier "
6021 "from pointer target type"),
6022 G_("initialization discards %qv qualifier "
6023 "from pointer target type"),
6024 G_("return discards %qv qualifier from "
6025 "pointer target type"),
6026 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6028 memb = marginal_memb;
6031 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6032 pedwarn (location, OPT_Wpedantic,
6033 "ISO C prohibits argument conversion to union type");
6035 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6036 return build_constructor_single (type, memb, rhs);
6040 /* Conversions among pointers */
6041 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6042 && (coder == codel))
6044 tree ttl = TREE_TYPE (type);
6045 tree ttr = TREE_TYPE (rhstype);
6046 tree mvl = ttl;
6047 tree mvr = ttr;
6048 bool is_opaque_pointer;
6049 int target_cmp = 0; /* Cache comp_target_types () result. */
6050 addr_space_t asl;
6051 addr_space_t asr;
6053 if (TREE_CODE (mvl) != ARRAY_TYPE)
6054 mvl = (TYPE_ATOMIC (mvl)
6055 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6056 TYPE_QUAL_ATOMIC)
6057 : TYPE_MAIN_VARIANT (mvl));
6058 if (TREE_CODE (mvr) != ARRAY_TYPE)
6059 mvr = (TYPE_ATOMIC (mvr)
6060 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6061 TYPE_QUAL_ATOMIC)
6062 : TYPE_MAIN_VARIANT (mvr));
6063 /* Opaque pointers are treated like void pointers. */
6064 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6066 /* The Plan 9 compiler permits a pointer to a struct to be
6067 automatically converted into a pointer to an anonymous field
6068 within the struct. */
6069 if (flag_plan9_extensions
6070 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6071 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6072 && mvl != mvr)
6074 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6075 if (new_rhs != NULL_TREE)
6077 rhs = new_rhs;
6078 rhstype = TREE_TYPE (rhs);
6079 coder = TREE_CODE (rhstype);
6080 ttr = TREE_TYPE (rhstype);
6081 mvr = TYPE_MAIN_VARIANT (ttr);
6085 /* C++ does not allow the implicit conversion void* -> T*. However,
6086 for the purpose of reducing the number of false positives, we
6087 tolerate the special case of
6089 int *p = NULL;
6091 where NULL is typically defined in C to be '(void *) 0'. */
6092 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6093 warning_at (errtype == ic_argpass ? expr_loc : location,
6094 OPT_Wc___compat,
6095 "request for implicit conversion "
6096 "from %qT to %qT not permitted in C++", rhstype, type);
6098 /* See if the pointers point to incompatible address spaces. */
6099 asl = TYPE_ADDR_SPACE (ttl);
6100 asr = TYPE_ADDR_SPACE (ttr);
6101 if (!null_pointer_constant_p (rhs)
6102 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6104 switch (errtype)
6106 case ic_argpass:
6107 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6108 "non-enclosed address space", parmnum, rname);
6109 break;
6110 case ic_assign:
6111 error_at (location, "assignment from pointer to "
6112 "non-enclosed address space");
6113 break;
6114 case ic_init:
6115 error_at (location, "initialization from pointer to "
6116 "non-enclosed address space");
6117 break;
6118 case ic_return:
6119 error_at (location, "return from pointer to "
6120 "non-enclosed address space");
6121 break;
6122 default:
6123 gcc_unreachable ();
6125 return error_mark_node;
6128 /* Check if the right-hand side has a format attribute but the
6129 left-hand side doesn't. */
6130 if (warn_suggest_attribute_format
6131 && check_missing_format_attribute (type, rhstype))
6133 switch (errtype)
6135 case ic_argpass:
6136 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6137 "argument %d of %qE might be "
6138 "a candidate for a format attribute",
6139 parmnum, rname);
6140 break;
6141 case ic_assign:
6142 warning_at (location, OPT_Wsuggest_attribute_format,
6143 "assignment left-hand side might be "
6144 "a candidate for a format attribute");
6145 break;
6146 case ic_init:
6147 warning_at (location, OPT_Wsuggest_attribute_format,
6148 "initialization left-hand side might be "
6149 "a candidate for a format attribute");
6150 break;
6151 case ic_return:
6152 warning_at (location, OPT_Wsuggest_attribute_format,
6153 "return type might be "
6154 "a candidate for a format attribute");
6155 break;
6156 default:
6157 gcc_unreachable ();
6161 /* Any non-function converts to a [const][volatile] void *
6162 and vice versa; otherwise, targets must be the same.
6163 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6164 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6165 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6166 || (target_cmp = comp_target_types (location, type, rhstype))
6167 || is_opaque_pointer
6168 || ((c_common_unsigned_type (mvl)
6169 == c_common_unsigned_type (mvr))
6170 && (c_common_signed_type (mvl)
6171 == c_common_signed_type (mvr))
6172 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6174 /* Warn about loss of qualifers from pointers to arrays with
6175 qualifiers on the element type. */
6176 if (TREE_CODE (ttr) == ARRAY_TYPE)
6178 ttr = strip_array_types (ttr);
6179 ttl = strip_array_types (ttl);
6181 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6182 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6183 WARNING_FOR_QUALIFIERS (location, expr_loc,
6184 OPT_Wdiscarded_array_qualifiers,
6185 G_("passing argument %d of %qE discards "
6186 "%qv qualifier from pointer target type"),
6187 G_("assignment discards %qv qualifier "
6188 "from pointer target type"),
6189 G_("initialization discards %qv qualifier "
6190 "from pointer target type"),
6191 G_("return discards %qv qualifier from "
6192 "pointer target type"),
6193 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6195 else if (pedantic
6196 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6198 (VOID_TYPE_P (ttr)
6199 && !null_pointer_constant
6200 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6201 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6202 G_("ISO C forbids passing argument %d of "
6203 "%qE between function pointer "
6204 "and %<void *%>"),
6205 G_("ISO C forbids assignment between "
6206 "function pointer and %<void *%>"),
6207 G_("ISO C forbids initialization between "
6208 "function pointer and %<void *%>"),
6209 G_("ISO C forbids return between function "
6210 "pointer and %<void *%>"));
6211 /* Const and volatile mean something different for function types,
6212 so the usual warnings are not appropriate. */
6213 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6214 && TREE_CODE (ttl) != FUNCTION_TYPE)
6216 /* Don't warn about loss of qualifier for conversions from
6217 qualified void* to pointers to arrays with corresponding
6218 qualifier on the element type. */
6219 if (!pedantic)
6220 ttl = strip_array_types (ttl);
6222 /* Assignments between atomic and non-atomic objects are OK. */
6223 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6224 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6226 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6227 OPT_Wdiscarded_qualifiers,
6228 G_("passing argument %d of %qE discards "
6229 "%qv qualifier from pointer target type"),
6230 G_("assignment discards %qv qualifier "
6231 "from pointer target type"),
6232 G_("initialization discards %qv qualifier "
6233 "from pointer target type"),
6234 G_("return discards %qv qualifier from "
6235 "pointer target type"),
6236 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6238 /* If this is not a case of ignoring a mismatch in signedness,
6239 no warning. */
6240 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6241 || target_cmp)
6243 /* If there is a mismatch, do warn. */
6244 else if (warn_pointer_sign)
6245 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6246 G_("pointer targets in passing argument "
6247 "%d of %qE differ in signedness"),
6248 G_("pointer targets in assignment "
6249 "differ in signedness"),
6250 G_("pointer targets in initialization "
6251 "differ in signedness"),
6252 G_("pointer targets in return differ "
6253 "in signedness"));
6255 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6256 && TREE_CODE (ttr) == FUNCTION_TYPE)
6258 /* Because const and volatile on functions are restrictions
6259 that say the function will not do certain things,
6260 it is okay to use a const or volatile function
6261 where an ordinary one is wanted, but not vice-versa. */
6262 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6263 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6264 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6265 OPT_Wdiscarded_qualifiers,
6266 G_("passing argument %d of %qE makes "
6267 "%q#v qualified function pointer "
6268 "from unqualified"),
6269 G_("assignment makes %q#v qualified function "
6270 "pointer from unqualified"),
6271 G_("initialization makes %q#v qualified "
6272 "function pointer from unqualified"),
6273 G_("return makes %q#v qualified function "
6274 "pointer from unqualified"),
6275 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6278 else
6279 /* Avoid warning about the volatile ObjC EH puts on decls. */
6280 if (!objc_ok)
6281 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6282 OPT_Wincompatible_pointer_types,
6283 G_("passing argument %d of %qE from "
6284 "incompatible pointer type"),
6285 G_("assignment from incompatible pointer type"),
6286 G_("initialization from incompatible "
6287 "pointer type"),
6288 G_("return from incompatible pointer type"));
6290 return convert (type, rhs);
6292 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6294 /* ??? This should not be an error when inlining calls to
6295 unprototyped functions. */
6296 error_at (location, "invalid use of non-lvalue array");
6297 return error_mark_node;
6299 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6301 /* An explicit constant 0 can convert to a pointer,
6302 or one that results from arithmetic, even including
6303 a cast to integer type. */
6304 if (!null_pointer_constant)
6305 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6306 OPT_Wint_conversion,
6307 G_("passing argument %d of %qE makes "
6308 "pointer from integer without a cast"),
6309 G_("assignment makes pointer from integer "
6310 "without a cast"),
6311 G_("initialization makes pointer from "
6312 "integer without a cast"),
6313 G_("return makes pointer from integer "
6314 "without a cast"));
6316 return convert (type, rhs);
6318 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6320 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6321 OPT_Wint_conversion,
6322 G_("passing argument %d of %qE makes integer "
6323 "from pointer without a cast"),
6324 G_("assignment makes integer from pointer "
6325 "without a cast"),
6326 G_("initialization makes integer from pointer "
6327 "without a cast"),
6328 G_("return makes integer from pointer "
6329 "without a cast"));
6330 return convert (type, rhs);
6332 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6334 tree ret;
6335 bool save = in_late_binary_op;
6336 in_late_binary_op = true;
6337 ret = convert (type, rhs);
6338 in_late_binary_op = save;
6339 return ret;
6342 switch (errtype)
6344 case ic_argpass:
6345 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6346 rname);
6347 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6348 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6349 "expected %qT but argument is of type %qT", type, rhstype);
6350 break;
6351 case ic_assign:
6352 error_at (location, "incompatible types when assigning to type %qT from "
6353 "type %qT", type, rhstype);
6354 break;
6355 case ic_init:
6356 error_at (location,
6357 "incompatible types when initializing type %qT using type %qT",
6358 type, rhstype);
6359 break;
6360 case ic_return:
6361 error_at (location,
6362 "incompatible types when returning type %qT but %qT was "
6363 "expected", rhstype, type);
6364 break;
6365 default:
6366 gcc_unreachable ();
6369 return error_mark_node;
6372 /* If VALUE is a compound expr all of whose expressions are constant, then
6373 return its value. Otherwise, return error_mark_node.
6375 This is for handling COMPOUND_EXPRs as initializer elements
6376 which is allowed with a warning when -pedantic is specified. */
6378 static tree
6379 valid_compound_expr_initializer (tree value, tree endtype)
6381 if (TREE_CODE (value) == COMPOUND_EXPR)
6383 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6384 == error_mark_node)
6385 return error_mark_node;
6386 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6387 endtype);
6389 else if (!initializer_constant_valid_p (value, endtype))
6390 return error_mark_node;
6391 else
6392 return value;
6395 /* Perform appropriate conversions on the initial value of a variable,
6396 store it in the declaration DECL,
6397 and print any error messages that are appropriate.
6398 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6399 If the init is invalid, store an ERROR_MARK.
6401 INIT_LOC is the location of the initial value. */
6403 void
6404 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6406 tree value, type;
6407 bool npc = false;
6409 /* If variable's type was invalidly declared, just ignore it. */
6411 type = TREE_TYPE (decl);
6412 if (TREE_CODE (type) == ERROR_MARK)
6413 return;
6415 /* Digest the specified initializer into an expression. */
6417 if (init)
6418 npc = null_pointer_constant_p (init);
6419 value = digest_init (init_loc, type, init, origtype, npc,
6420 true, TREE_STATIC (decl));
6422 /* Store the expression if valid; else report error. */
6424 if (!in_system_header_at (input_location)
6425 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6426 warning (OPT_Wtraditional, "traditional C rejects automatic "
6427 "aggregate initialization");
6429 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6430 DECL_INITIAL (decl) = value;
6432 /* ANSI wants warnings about out-of-range constant initializers. */
6433 STRIP_TYPE_NOPS (value);
6434 if (TREE_STATIC (decl))
6435 constant_expression_warning (value);
6437 /* Check if we need to set array size from compound literal size. */
6438 if (TREE_CODE (type) == ARRAY_TYPE
6439 && TYPE_DOMAIN (type) == 0
6440 && value != error_mark_node)
6442 tree inside_init = init;
6444 STRIP_TYPE_NOPS (inside_init);
6445 inside_init = fold (inside_init);
6447 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6449 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6451 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6453 /* For int foo[] = (int [3]){1}; we need to set array size
6454 now since later on array initializer will be just the
6455 brace enclosed list of the compound literal. */
6456 tree etype = strip_array_types (TREE_TYPE (decl));
6457 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6458 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6459 layout_type (type);
6460 layout_decl (cldecl, 0);
6461 TREE_TYPE (decl)
6462 = c_build_qualified_type (type, TYPE_QUALS (etype));
6468 /* Methods for storing and printing names for error messages. */
6470 /* Implement a spelling stack that allows components of a name to be pushed
6471 and popped. Each element on the stack is this structure. */
6473 struct spelling
6475 int kind;
6476 union
6478 unsigned HOST_WIDE_INT i;
6479 const char *s;
6480 } u;
6483 #define SPELLING_STRING 1
6484 #define SPELLING_MEMBER 2
6485 #define SPELLING_BOUNDS 3
6487 static struct spelling *spelling; /* Next stack element (unused). */
6488 static struct spelling *spelling_base; /* Spelling stack base. */
6489 static int spelling_size; /* Size of the spelling stack. */
6491 /* Macros to save and restore the spelling stack around push_... functions.
6492 Alternative to SAVE_SPELLING_STACK. */
6494 #define SPELLING_DEPTH() (spelling - spelling_base)
6495 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6497 /* Push an element on the spelling stack with type KIND and assign VALUE
6498 to MEMBER. */
6500 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6502 int depth = SPELLING_DEPTH (); \
6504 if (depth >= spelling_size) \
6506 spelling_size += 10; \
6507 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6508 spelling_size); \
6509 RESTORE_SPELLING_DEPTH (depth); \
6512 spelling->kind = (KIND); \
6513 spelling->MEMBER = (VALUE); \
6514 spelling++; \
6517 /* Push STRING on the stack. Printed literally. */
6519 static void
6520 push_string (const char *string)
6522 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6525 /* Push a member name on the stack. Printed as '.' STRING. */
6527 static void
6528 push_member_name (tree decl)
6530 const char *const string
6531 = (DECL_NAME (decl)
6532 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6533 : _("<anonymous>"));
6534 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6537 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6539 static void
6540 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6542 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6545 /* Compute the maximum size in bytes of the printed spelling. */
6547 static int
6548 spelling_length (void)
6550 int size = 0;
6551 struct spelling *p;
6553 for (p = spelling_base; p < spelling; p++)
6555 if (p->kind == SPELLING_BOUNDS)
6556 size += 25;
6557 else
6558 size += strlen (p->u.s) + 1;
6561 return size;
6564 /* Print the spelling to BUFFER and return it. */
6566 static char *
6567 print_spelling (char *buffer)
6569 char *d = buffer;
6570 struct spelling *p;
6572 for (p = spelling_base; p < spelling; p++)
6573 if (p->kind == SPELLING_BOUNDS)
6575 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6576 d += strlen (d);
6578 else
6580 const char *s;
6581 if (p->kind == SPELLING_MEMBER)
6582 *d++ = '.';
6583 for (s = p->u.s; (*d = *s++); d++)
6586 *d++ = '\0';
6587 return buffer;
6590 /* Digest the parser output INIT as an initializer for type TYPE.
6591 Return a C expression of type TYPE to represent the initial value.
6593 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6595 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6597 If INIT is a string constant, STRICT_STRING is true if it is
6598 unparenthesized or we should not warn here for it being parenthesized.
6599 For other types of INIT, STRICT_STRING is not used.
6601 INIT_LOC is the location of the INIT.
6603 REQUIRE_CONSTANT requests an error if non-constant initializers or
6604 elements are seen. */
6606 static tree
6607 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6608 bool null_pointer_constant, bool strict_string,
6609 int require_constant)
6611 enum tree_code code = TREE_CODE (type);
6612 tree inside_init = init;
6613 tree semantic_type = NULL_TREE;
6614 bool maybe_const = true;
6616 if (type == error_mark_node
6617 || !init
6618 || error_operand_p (init))
6619 return error_mark_node;
6621 STRIP_TYPE_NOPS (inside_init);
6623 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6625 semantic_type = TREE_TYPE (inside_init);
6626 inside_init = TREE_OPERAND (inside_init, 0);
6628 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6629 inside_init = decl_constant_value_for_optimization (inside_init);
6631 /* Initialization of an array of chars from a string constant
6632 optionally enclosed in braces. */
6634 if (code == ARRAY_TYPE && inside_init
6635 && TREE_CODE (inside_init) == STRING_CST)
6637 tree typ1
6638 = (TYPE_ATOMIC (TREE_TYPE (type))
6639 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6640 TYPE_QUAL_ATOMIC)
6641 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6642 /* Note that an array could be both an array of character type
6643 and an array of wchar_t if wchar_t is signed char or unsigned
6644 char. */
6645 bool char_array = (typ1 == char_type_node
6646 || typ1 == signed_char_type_node
6647 || typ1 == unsigned_char_type_node);
6648 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6649 bool char16_array = !!comptypes (typ1, char16_type_node);
6650 bool char32_array = !!comptypes (typ1, char32_type_node);
6652 if (char_array || wchar_array || char16_array || char32_array)
6654 struct c_expr expr;
6655 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6656 expr.value = inside_init;
6657 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6658 expr.original_type = NULL;
6659 maybe_warn_string_init (init_loc, type, expr);
6661 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6662 pedwarn_init (init_loc, OPT_Wpedantic,
6663 "initialization of a flexible array member");
6665 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6666 TYPE_MAIN_VARIANT (type)))
6667 return inside_init;
6669 if (char_array)
6671 if (typ2 != char_type_node)
6673 error_init (init_loc, "char-array initialized from wide "
6674 "string");
6675 return error_mark_node;
6678 else
6680 if (typ2 == char_type_node)
6682 error_init (init_loc, "wide character array initialized "
6683 "from non-wide string");
6684 return error_mark_node;
6686 else if (!comptypes(typ1, typ2))
6688 error_init (init_loc, "wide character array initialized "
6689 "from incompatible wide string");
6690 return error_mark_node;
6694 TREE_TYPE (inside_init) = type;
6695 if (TYPE_DOMAIN (type) != 0
6696 && TYPE_SIZE (type) != 0
6697 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6699 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6701 /* Subtract the size of a single (possibly wide) character
6702 because it's ok to ignore the terminating null char
6703 that is counted in the length of the constant. */
6704 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6705 (len
6706 - (TYPE_PRECISION (typ1)
6707 / BITS_PER_UNIT))))
6708 pedwarn_init (init_loc, 0,
6709 ("initializer-string for array of chars "
6710 "is too long"));
6711 else if (warn_cxx_compat
6712 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6713 warning_at (init_loc, OPT_Wc___compat,
6714 ("initializer-string for array chars "
6715 "is too long for C++"));
6718 return inside_init;
6720 else if (INTEGRAL_TYPE_P (typ1))
6722 error_init (init_loc, "array of inappropriate type initialized "
6723 "from string constant");
6724 return error_mark_node;
6728 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6729 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6730 below and handle as a constructor. */
6731 if (code == VECTOR_TYPE
6732 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
6733 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6734 && TREE_CONSTANT (inside_init))
6736 if (TREE_CODE (inside_init) == VECTOR_CST
6737 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6738 TYPE_MAIN_VARIANT (type)))
6739 return inside_init;
6741 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6743 unsigned HOST_WIDE_INT ix;
6744 tree value;
6745 bool constant_p = true;
6747 /* Iterate through elements and check if all constructor
6748 elements are *_CSTs. */
6749 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6750 if (!CONSTANT_CLASS_P (value))
6752 constant_p = false;
6753 break;
6756 if (constant_p)
6757 return build_vector_from_ctor (type,
6758 CONSTRUCTOR_ELTS (inside_init));
6762 if (warn_sequence_point)
6763 verify_sequence_points (inside_init);
6765 /* Any type can be initialized
6766 from an expression of the same type, optionally with braces. */
6768 if (inside_init && TREE_TYPE (inside_init) != 0
6769 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6770 TYPE_MAIN_VARIANT (type))
6771 || (code == ARRAY_TYPE
6772 && comptypes (TREE_TYPE (inside_init), type))
6773 || (code == VECTOR_TYPE
6774 && comptypes (TREE_TYPE (inside_init), type))
6775 || (code == POINTER_TYPE
6776 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6777 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6778 TREE_TYPE (type)))))
6780 if (code == POINTER_TYPE)
6782 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6784 if (TREE_CODE (inside_init) == STRING_CST
6785 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6786 inside_init = array_to_pointer_conversion
6787 (init_loc, inside_init);
6788 else
6790 error_init (init_loc, "invalid use of non-lvalue array");
6791 return error_mark_node;
6796 if (code == VECTOR_TYPE)
6797 /* Although the types are compatible, we may require a
6798 conversion. */
6799 inside_init = convert (type, inside_init);
6801 if (require_constant
6802 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6804 /* As an extension, allow initializing objects with static storage
6805 duration with compound literals (which are then treated just as
6806 the brace enclosed list they contain). Also allow this for
6807 vectors, as we can only assign them with compound literals. */
6808 if (flag_isoc99 && code != VECTOR_TYPE)
6809 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6810 "is not constant");
6811 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6812 inside_init = DECL_INITIAL (decl);
6815 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6816 && TREE_CODE (inside_init) != CONSTRUCTOR)
6818 error_init (init_loc, "array initialized from non-constant array "
6819 "expression");
6820 return error_mark_node;
6823 /* Compound expressions can only occur here if -Wpedantic or
6824 -pedantic-errors is specified. In the later case, we always want
6825 an error. In the former case, we simply want a warning. */
6826 if (require_constant && pedantic
6827 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6829 inside_init
6830 = valid_compound_expr_initializer (inside_init,
6831 TREE_TYPE (inside_init));
6832 if (inside_init == error_mark_node)
6833 error_init (init_loc, "initializer element is not constant");
6834 else
6835 pedwarn_init (init_loc, OPT_Wpedantic,
6836 "initializer element is not constant");
6837 if (flag_pedantic_errors)
6838 inside_init = error_mark_node;
6840 else if (require_constant
6841 && !initializer_constant_valid_p (inside_init,
6842 TREE_TYPE (inside_init)))
6844 error_init (init_loc, "initializer element is not constant");
6845 inside_init = error_mark_node;
6847 else if (require_constant && !maybe_const)
6848 pedwarn_init (init_loc, OPT_Wpedantic,
6849 "initializer element is not a constant expression");
6851 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6852 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6853 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6854 type, inside_init, origtype,
6855 ic_init, null_pointer_constant,
6856 NULL_TREE, NULL_TREE, 0);
6857 return inside_init;
6860 /* Handle scalar types, including conversions. */
6862 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6863 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6864 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6866 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6867 && (TREE_CODE (init) == STRING_CST
6868 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6869 inside_init = init = array_to_pointer_conversion (init_loc, init);
6870 if (semantic_type)
6871 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6872 inside_init);
6873 inside_init
6874 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6875 inside_init, origtype, ic_init,
6876 null_pointer_constant, NULL_TREE, NULL_TREE,
6879 /* Check to see if we have already given an error message. */
6880 if (inside_init == error_mark_node)
6882 else if (require_constant && !TREE_CONSTANT (inside_init))
6884 error_init (init_loc, "initializer element is not constant");
6885 inside_init = error_mark_node;
6887 else if (require_constant
6888 && !initializer_constant_valid_p (inside_init,
6889 TREE_TYPE (inside_init)))
6891 error_init (init_loc, "initializer element is not computable at "
6892 "load time");
6893 inside_init = error_mark_node;
6895 else if (require_constant && !maybe_const)
6896 pedwarn_init (init_loc, 0,
6897 "initializer element is not a constant expression");
6899 return inside_init;
6902 /* Come here only for records and arrays. */
6904 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6906 error_init (init_loc, "variable-sized object may not be initialized");
6907 return error_mark_node;
6910 error_init (init_loc, "invalid initializer");
6911 return error_mark_node;
6914 /* Handle initializers that use braces. */
6916 /* Type of object we are accumulating a constructor for.
6917 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6918 static tree constructor_type;
6920 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6921 left to fill. */
6922 static tree constructor_fields;
6924 /* For an ARRAY_TYPE, this is the specified index
6925 at which to store the next element we get. */
6926 static tree constructor_index;
6928 /* For an ARRAY_TYPE, this is the maximum index. */
6929 static tree constructor_max_index;
6931 /* For a RECORD_TYPE, this is the first field not yet written out. */
6932 static tree constructor_unfilled_fields;
6934 /* For an ARRAY_TYPE, this is the index of the first element
6935 not yet written out. */
6936 static tree constructor_unfilled_index;
6938 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6939 This is so we can generate gaps between fields, when appropriate. */
6940 static tree constructor_bit_index;
6942 /* If we are saving up the elements rather than allocating them,
6943 this is the list of elements so far (in reverse order,
6944 most recent first). */
6945 static vec<constructor_elt, va_gc> *constructor_elements;
6947 /* 1 if constructor should be incrementally stored into a constructor chain,
6948 0 if all the elements should be kept in AVL tree. */
6949 static int constructor_incremental;
6951 /* 1 if so far this constructor's elements are all compile-time constants. */
6952 static int constructor_constant;
6954 /* 1 if so far this constructor's elements are all valid address constants. */
6955 static int constructor_simple;
6957 /* 1 if this constructor has an element that cannot be part of a
6958 constant expression. */
6959 static int constructor_nonconst;
6961 /* 1 if this constructor is erroneous so far. */
6962 static int constructor_erroneous;
6964 /* 1 if this constructor is the universal zero initializer { 0 }. */
6965 static int constructor_zeroinit;
6967 /* Structure for managing pending initializer elements, organized as an
6968 AVL tree. */
6970 struct init_node
6972 struct init_node *left, *right;
6973 struct init_node *parent;
6974 int balance;
6975 tree purpose;
6976 tree value;
6977 tree origtype;
6980 /* Tree of pending elements at this constructor level.
6981 These are elements encountered out of order
6982 which belong at places we haven't reached yet in actually
6983 writing the output.
6984 Will never hold tree nodes across GC runs. */
6985 static struct init_node *constructor_pending_elts;
6987 /* The SPELLING_DEPTH of this constructor. */
6988 static int constructor_depth;
6990 /* DECL node for which an initializer is being read.
6991 0 means we are reading a constructor expression
6992 such as (struct foo) {...}. */
6993 static tree constructor_decl;
6995 /* Nonzero if this is an initializer for a top-level decl. */
6996 static int constructor_top_level;
6998 /* Nonzero if there were any member designators in this initializer. */
6999 static int constructor_designated;
7001 /* Nesting depth of designator list. */
7002 static int designator_depth;
7004 /* Nonzero if there were diagnosed errors in this designator list. */
7005 static int designator_erroneous;
7008 /* This stack has a level for each implicit or explicit level of
7009 structuring in the initializer, including the outermost one. It
7010 saves the values of most of the variables above. */
7012 struct constructor_range_stack;
7014 struct constructor_stack
7016 struct constructor_stack *next;
7017 tree type;
7018 tree fields;
7019 tree index;
7020 tree max_index;
7021 tree unfilled_index;
7022 tree unfilled_fields;
7023 tree bit_index;
7024 vec<constructor_elt, va_gc> *elements;
7025 struct init_node *pending_elts;
7026 int offset;
7027 int depth;
7028 /* If value nonzero, this value should replace the entire
7029 constructor at this level. */
7030 struct c_expr replacement_value;
7031 struct constructor_range_stack *range_stack;
7032 char constant;
7033 char simple;
7034 char nonconst;
7035 char implicit;
7036 char erroneous;
7037 char outer;
7038 char incremental;
7039 char designated;
7040 int designator_depth;
7043 static struct constructor_stack *constructor_stack;
7045 /* This stack represents designators from some range designator up to
7046 the last designator in the list. */
7048 struct constructor_range_stack
7050 struct constructor_range_stack *next, *prev;
7051 struct constructor_stack *stack;
7052 tree range_start;
7053 tree index;
7054 tree range_end;
7055 tree fields;
7058 static struct constructor_range_stack *constructor_range_stack;
7060 /* This stack records separate initializers that are nested.
7061 Nested initializers can't happen in ANSI C, but GNU C allows them
7062 in cases like { ... (struct foo) { ... } ... }. */
7064 struct initializer_stack
7066 struct initializer_stack *next;
7067 tree decl;
7068 struct constructor_stack *constructor_stack;
7069 struct constructor_range_stack *constructor_range_stack;
7070 vec<constructor_elt, va_gc> *elements;
7071 struct spelling *spelling;
7072 struct spelling *spelling_base;
7073 int spelling_size;
7074 char top_level;
7075 char require_constant_value;
7076 char require_constant_elements;
7079 static struct initializer_stack *initializer_stack;
7081 /* Prepare to parse and output the initializer for variable DECL. */
7083 void
7084 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7086 const char *locus;
7087 struct initializer_stack *p = XNEW (struct initializer_stack);
7089 p->decl = constructor_decl;
7090 p->require_constant_value = require_constant_value;
7091 p->require_constant_elements = require_constant_elements;
7092 p->constructor_stack = constructor_stack;
7093 p->constructor_range_stack = constructor_range_stack;
7094 p->elements = constructor_elements;
7095 p->spelling = spelling;
7096 p->spelling_base = spelling_base;
7097 p->spelling_size = spelling_size;
7098 p->top_level = constructor_top_level;
7099 p->next = initializer_stack;
7100 initializer_stack = p;
7102 constructor_decl = decl;
7103 constructor_designated = 0;
7104 constructor_top_level = top_level;
7106 if (decl != 0 && decl != error_mark_node)
7108 require_constant_value = TREE_STATIC (decl);
7109 require_constant_elements
7110 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7111 /* For a scalar, you can always use any value to initialize,
7112 even within braces. */
7113 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7114 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7116 else
7118 require_constant_value = 0;
7119 require_constant_elements = 0;
7120 locus = _("(anonymous)");
7123 constructor_stack = 0;
7124 constructor_range_stack = 0;
7126 found_missing_braces = 0;
7128 spelling_base = 0;
7129 spelling_size = 0;
7130 RESTORE_SPELLING_DEPTH (0);
7132 if (locus)
7133 push_string (locus);
7136 void
7137 finish_init (void)
7139 struct initializer_stack *p = initializer_stack;
7141 /* Free the whole constructor stack of this initializer. */
7142 while (constructor_stack)
7144 struct constructor_stack *q = constructor_stack;
7145 constructor_stack = q->next;
7146 free (q);
7149 gcc_assert (!constructor_range_stack);
7151 /* Pop back to the data of the outer initializer (if any). */
7152 free (spelling_base);
7154 constructor_decl = p->decl;
7155 require_constant_value = p->require_constant_value;
7156 require_constant_elements = p->require_constant_elements;
7157 constructor_stack = p->constructor_stack;
7158 constructor_range_stack = p->constructor_range_stack;
7159 constructor_elements = p->elements;
7160 spelling = p->spelling;
7161 spelling_base = p->spelling_base;
7162 spelling_size = p->spelling_size;
7163 constructor_top_level = p->top_level;
7164 initializer_stack = p->next;
7165 free (p);
7168 /* Call here when we see the initializer is surrounded by braces.
7169 This is instead of a call to push_init_level;
7170 it is matched by a call to pop_init_level.
7172 TYPE is the type to initialize, for a constructor expression.
7173 For an initializer for a decl, TYPE is zero. */
7175 void
7176 really_start_incremental_init (tree type)
7178 struct constructor_stack *p = XNEW (struct constructor_stack);
7180 if (type == 0)
7181 type = TREE_TYPE (constructor_decl);
7183 if (VECTOR_TYPE_P (type)
7184 && TYPE_VECTOR_OPAQUE (type))
7185 error ("opaque vector types cannot be initialized");
7187 p->type = constructor_type;
7188 p->fields = constructor_fields;
7189 p->index = constructor_index;
7190 p->max_index = constructor_max_index;
7191 p->unfilled_index = constructor_unfilled_index;
7192 p->unfilled_fields = constructor_unfilled_fields;
7193 p->bit_index = constructor_bit_index;
7194 p->elements = constructor_elements;
7195 p->constant = constructor_constant;
7196 p->simple = constructor_simple;
7197 p->nonconst = constructor_nonconst;
7198 p->erroneous = constructor_erroneous;
7199 p->pending_elts = constructor_pending_elts;
7200 p->depth = constructor_depth;
7201 p->replacement_value.value = 0;
7202 p->replacement_value.original_code = ERROR_MARK;
7203 p->replacement_value.original_type = NULL;
7204 p->implicit = 0;
7205 p->range_stack = 0;
7206 p->outer = 0;
7207 p->incremental = constructor_incremental;
7208 p->designated = constructor_designated;
7209 p->designator_depth = designator_depth;
7210 p->next = 0;
7211 constructor_stack = p;
7213 constructor_constant = 1;
7214 constructor_simple = 1;
7215 constructor_nonconst = 0;
7216 constructor_depth = SPELLING_DEPTH ();
7217 constructor_elements = NULL;
7218 constructor_pending_elts = 0;
7219 constructor_type = type;
7220 constructor_incremental = 1;
7221 constructor_designated = 0;
7222 constructor_zeroinit = 1;
7223 designator_depth = 0;
7224 designator_erroneous = 0;
7226 if (TREE_CODE (constructor_type) == RECORD_TYPE
7227 || TREE_CODE (constructor_type) == UNION_TYPE)
7229 constructor_fields = TYPE_FIELDS (constructor_type);
7230 /* Skip any nameless bit fields at the beginning. */
7231 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7232 && DECL_NAME (constructor_fields) == 0)
7233 constructor_fields = DECL_CHAIN (constructor_fields);
7235 constructor_unfilled_fields = constructor_fields;
7236 constructor_bit_index = bitsize_zero_node;
7238 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7240 if (TYPE_DOMAIN (constructor_type))
7242 constructor_max_index
7243 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7245 /* Detect non-empty initializations of zero-length arrays. */
7246 if (constructor_max_index == NULL_TREE
7247 && TYPE_SIZE (constructor_type))
7248 constructor_max_index = integer_minus_one_node;
7250 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7251 to initialize VLAs will cause a proper error; avoid tree
7252 checking errors as well by setting a safe value. */
7253 if (constructor_max_index
7254 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7255 constructor_max_index = integer_minus_one_node;
7257 constructor_index
7258 = convert (bitsizetype,
7259 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7261 else
7263 constructor_index = bitsize_zero_node;
7264 constructor_max_index = NULL_TREE;
7267 constructor_unfilled_index = constructor_index;
7269 else if (VECTOR_TYPE_P (constructor_type))
7271 /* Vectors are like simple fixed-size arrays. */
7272 constructor_max_index =
7273 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7274 constructor_index = bitsize_zero_node;
7275 constructor_unfilled_index = constructor_index;
7277 else
7279 /* Handle the case of int x = {5}; */
7280 constructor_fields = constructor_type;
7281 constructor_unfilled_fields = constructor_type;
7285 /* Push down into a subobject, for initialization.
7286 If this is for an explicit set of braces, IMPLICIT is 0.
7287 If it is because the next element belongs at a lower level,
7288 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7290 void
7291 push_init_level (location_t loc, int implicit,
7292 struct obstack *braced_init_obstack)
7294 struct constructor_stack *p;
7295 tree value = NULL_TREE;
7297 /* If we've exhausted any levels that didn't have braces,
7298 pop them now. If implicit == 1, this will have been done in
7299 process_init_element; do not repeat it here because in the case
7300 of excess initializers for an empty aggregate this leads to an
7301 infinite cycle of popping a level and immediately recreating
7302 it. */
7303 if (implicit != 1)
7305 while (constructor_stack->implicit)
7307 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7308 || TREE_CODE (constructor_type) == UNION_TYPE)
7309 && constructor_fields == 0)
7310 process_init_element (input_location,
7311 pop_init_level (loc, 1, braced_init_obstack),
7312 true, braced_init_obstack);
7313 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7314 && constructor_max_index
7315 && tree_int_cst_lt (constructor_max_index,
7316 constructor_index))
7317 process_init_element (input_location,
7318 pop_init_level (loc, 1, braced_init_obstack),
7319 true, braced_init_obstack);
7320 else
7321 break;
7325 /* Unless this is an explicit brace, we need to preserve previous
7326 content if any. */
7327 if (implicit)
7329 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7330 || TREE_CODE (constructor_type) == UNION_TYPE)
7331 && constructor_fields)
7332 value = find_init_member (constructor_fields, braced_init_obstack);
7333 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7334 value = find_init_member (constructor_index, braced_init_obstack);
7337 p = XNEW (struct constructor_stack);
7338 p->type = constructor_type;
7339 p->fields = constructor_fields;
7340 p->index = constructor_index;
7341 p->max_index = constructor_max_index;
7342 p->unfilled_index = constructor_unfilled_index;
7343 p->unfilled_fields = constructor_unfilled_fields;
7344 p->bit_index = constructor_bit_index;
7345 p->elements = constructor_elements;
7346 p->constant = constructor_constant;
7347 p->simple = constructor_simple;
7348 p->nonconst = constructor_nonconst;
7349 p->erroneous = constructor_erroneous;
7350 p->pending_elts = constructor_pending_elts;
7351 p->depth = constructor_depth;
7352 p->replacement_value.value = 0;
7353 p->replacement_value.original_code = ERROR_MARK;
7354 p->replacement_value.original_type = NULL;
7355 p->implicit = implicit;
7356 p->outer = 0;
7357 p->incremental = constructor_incremental;
7358 p->designated = constructor_designated;
7359 p->designator_depth = designator_depth;
7360 p->next = constructor_stack;
7361 p->range_stack = 0;
7362 constructor_stack = p;
7364 constructor_constant = 1;
7365 constructor_simple = 1;
7366 constructor_nonconst = 0;
7367 constructor_depth = SPELLING_DEPTH ();
7368 constructor_elements = NULL;
7369 constructor_incremental = 1;
7370 constructor_designated = 0;
7371 constructor_pending_elts = 0;
7372 if (!implicit)
7374 p->range_stack = constructor_range_stack;
7375 constructor_range_stack = 0;
7376 designator_depth = 0;
7377 designator_erroneous = 0;
7380 /* Don't die if an entire brace-pair level is superfluous
7381 in the containing level. */
7382 if (constructor_type == 0)
7384 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7385 || TREE_CODE (constructor_type) == UNION_TYPE)
7387 /* Don't die if there are extra init elts at the end. */
7388 if (constructor_fields == 0)
7389 constructor_type = 0;
7390 else
7392 constructor_type = TREE_TYPE (constructor_fields);
7393 push_member_name (constructor_fields);
7394 constructor_depth++;
7396 /* If upper initializer is designated, then mark this as
7397 designated too to prevent bogus warnings. */
7398 constructor_designated = p->designated;
7400 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7402 constructor_type = TREE_TYPE (constructor_type);
7403 push_array_bounds (tree_to_uhwi (constructor_index));
7404 constructor_depth++;
7407 if (constructor_type == 0)
7409 error_init (loc, "extra brace group at end of initializer");
7410 constructor_fields = 0;
7411 constructor_unfilled_fields = 0;
7412 return;
7415 if (value && TREE_CODE (value) == CONSTRUCTOR)
7417 constructor_constant = TREE_CONSTANT (value);
7418 constructor_simple = TREE_STATIC (value);
7419 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7420 constructor_elements = CONSTRUCTOR_ELTS (value);
7421 if (!vec_safe_is_empty (constructor_elements)
7422 && (TREE_CODE (constructor_type) == RECORD_TYPE
7423 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7424 set_nonincremental_init (braced_init_obstack);
7427 if (implicit == 1)
7428 found_missing_braces = 1;
7430 if (TREE_CODE (constructor_type) == RECORD_TYPE
7431 || TREE_CODE (constructor_type) == UNION_TYPE)
7433 constructor_fields = TYPE_FIELDS (constructor_type);
7434 /* Skip any nameless bit fields at the beginning. */
7435 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7436 && DECL_NAME (constructor_fields) == 0)
7437 constructor_fields = DECL_CHAIN (constructor_fields);
7439 constructor_unfilled_fields = constructor_fields;
7440 constructor_bit_index = bitsize_zero_node;
7442 else if (VECTOR_TYPE_P (constructor_type))
7444 /* Vectors are like simple fixed-size arrays. */
7445 constructor_max_index =
7446 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7447 constructor_index = bitsize_int (0);
7448 constructor_unfilled_index = constructor_index;
7450 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7452 if (TYPE_DOMAIN (constructor_type))
7454 constructor_max_index
7455 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7457 /* Detect non-empty initializations of zero-length arrays. */
7458 if (constructor_max_index == NULL_TREE
7459 && TYPE_SIZE (constructor_type))
7460 constructor_max_index = integer_minus_one_node;
7462 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7463 to initialize VLAs will cause a proper error; avoid tree
7464 checking errors as well by setting a safe value. */
7465 if (constructor_max_index
7466 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7467 constructor_max_index = integer_minus_one_node;
7469 constructor_index
7470 = convert (bitsizetype,
7471 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7473 else
7474 constructor_index = bitsize_zero_node;
7476 constructor_unfilled_index = constructor_index;
7477 if (value && TREE_CODE (value) == STRING_CST)
7479 /* We need to split the char/wchar array into individual
7480 characters, so that we don't have to special case it
7481 everywhere. */
7482 set_nonincremental_init_from_string (value, braced_init_obstack);
7485 else
7487 if (constructor_type != error_mark_node)
7488 warning_init (input_location, 0, "braces around scalar initializer");
7489 constructor_fields = constructor_type;
7490 constructor_unfilled_fields = constructor_type;
7494 /* At the end of an implicit or explicit brace level,
7495 finish up that level of constructor. If a single expression
7496 with redundant braces initialized that level, return the
7497 c_expr structure for that expression. Otherwise, the original_code
7498 element is set to ERROR_MARK.
7499 If we were outputting the elements as they are read, return 0 as the value
7500 from inner levels (process_init_element ignores that),
7501 but return error_mark_node as the value from the outermost level
7502 (that's what we want to put in DECL_INITIAL).
7503 Otherwise, return a CONSTRUCTOR expression as the value. */
7505 struct c_expr
7506 pop_init_level (location_t loc, int implicit,
7507 struct obstack *braced_init_obstack)
7509 struct constructor_stack *p;
7510 struct c_expr ret;
7511 ret.value = 0;
7512 ret.original_code = ERROR_MARK;
7513 ret.original_type = NULL;
7515 if (implicit == 0)
7517 /* When we come to an explicit close brace,
7518 pop any inner levels that didn't have explicit braces. */
7519 while (constructor_stack->implicit)
7520 process_init_element (input_location,
7521 pop_init_level (loc, 1, braced_init_obstack),
7522 true, braced_init_obstack);
7523 gcc_assert (!constructor_range_stack);
7526 /* Now output all pending elements. */
7527 constructor_incremental = 1;
7528 output_pending_init_elements (1, braced_init_obstack);
7530 p = constructor_stack;
7532 /* Error for initializing a flexible array member, or a zero-length
7533 array member in an inappropriate context. */
7534 if (constructor_type && constructor_fields
7535 && TREE_CODE (constructor_type) == ARRAY_TYPE
7536 && TYPE_DOMAIN (constructor_type)
7537 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7539 /* Silently discard empty initializations. The parser will
7540 already have pedwarned for empty brackets. */
7541 if (integer_zerop (constructor_unfilled_index))
7542 constructor_type = NULL_TREE;
7543 else
7545 gcc_assert (!TYPE_SIZE (constructor_type));
7547 if (constructor_depth > 2)
7548 error_init (loc, "initialization of flexible array member in a nested context");
7549 else
7550 pedwarn_init (loc, OPT_Wpedantic,
7551 "initialization of a flexible array member");
7553 /* We have already issued an error message for the existence
7554 of a flexible array member not at the end of the structure.
7555 Discard the initializer so that we do not die later. */
7556 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7557 constructor_type = NULL_TREE;
7561 switch (vec_safe_length (constructor_elements))
7563 case 0:
7564 /* Initialization with { } counts as zeroinit. */
7565 constructor_zeroinit = 1;
7566 break;
7567 case 1:
7568 /* This might be zeroinit as well. */
7569 if (integer_zerop ((*constructor_elements)[0].value))
7570 constructor_zeroinit = 1;
7571 break;
7572 default:
7573 /* If the constructor has more than one element, it can't be { 0 }. */
7574 constructor_zeroinit = 0;
7575 break;
7578 /* Warn when some structs are initialized with direct aggregation. */
7579 if (!implicit && found_missing_braces && warn_missing_braces
7580 && !constructor_zeroinit)
7581 warning_init (loc, OPT_Wmissing_braces,
7582 "missing braces around initializer");
7584 /* Warn when some struct elements are implicitly initialized to zero. */
7585 if (warn_missing_field_initializers
7586 && constructor_type
7587 && TREE_CODE (constructor_type) == RECORD_TYPE
7588 && constructor_unfilled_fields)
7590 /* Do not warn for flexible array members or zero-length arrays. */
7591 while (constructor_unfilled_fields
7592 && (!DECL_SIZE (constructor_unfilled_fields)
7593 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7594 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7596 if (constructor_unfilled_fields
7597 /* Do not warn if this level of the initializer uses member
7598 designators; it is likely to be deliberate. */
7599 && !constructor_designated
7600 /* Do not warn about initializing with { 0 } or with { }. */
7601 && !constructor_zeroinit)
7603 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7604 "missing initializer for field %qD of %qT",
7605 constructor_unfilled_fields,
7606 constructor_type))
7607 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7608 "%qD declared here", constructor_unfilled_fields);
7612 /* Pad out the end of the structure. */
7613 if (p->replacement_value.value)
7614 /* If this closes a superfluous brace pair,
7615 just pass out the element between them. */
7616 ret = p->replacement_value;
7617 else if (constructor_type == 0)
7619 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7620 && TREE_CODE (constructor_type) != UNION_TYPE
7621 && TREE_CODE (constructor_type) != ARRAY_TYPE
7622 && !VECTOR_TYPE_P (constructor_type))
7624 /* A nonincremental scalar initializer--just return
7625 the element, after verifying there is just one. */
7626 if (vec_safe_is_empty (constructor_elements))
7628 if (!constructor_erroneous)
7629 error_init (loc, "empty scalar initializer");
7630 ret.value = error_mark_node;
7632 else if (vec_safe_length (constructor_elements) != 1)
7634 error_init (loc, "extra elements in scalar initializer");
7635 ret.value = (*constructor_elements)[0].value;
7637 else
7638 ret.value = (*constructor_elements)[0].value;
7640 else
7642 if (constructor_erroneous)
7643 ret.value = error_mark_node;
7644 else
7646 ret.value = build_constructor (constructor_type,
7647 constructor_elements);
7648 if (constructor_constant)
7649 TREE_CONSTANT (ret.value) = 1;
7650 if (constructor_constant && constructor_simple)
7651 TREE_STATIC (ret.value) = 1;
7652 if (constructor_nonconst)
7653 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7657 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7659 if (constructor_nonconst)
7660 ret.original_code = C_MAYBE_CONST_EXPR;
7661 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7662 ret.original_code = ERROR_MARK;
7665 constructor_type = p->type;
7666 constructor_fields = p->fields;
7667 constructor_index = p->index;
7668 constructor_max_index = p->max_index;
7669 constructor_unfilled_index = p->unfilled_index;
7670 constructor_unfilled_fields = p->unfilled_fields;
7671 constructor_bit_index = p->bit_index;
7672 constructor_elements = p->elements;
7673 constructor_constant = p->constant;
7674 constructor_simple = p->simple;
7675 constructor_nonconst = p->nonconst;
7676 constructor_erroneous = p->erroneous;
7677 constructor_incremental = p->incremental;
7678 constructor_designated = p->designated;
7679 designator_depth = p->designator_depth;
7680 constructor_pending_elts = p->pending_elts;
7681 constructor_depth = p->depth;
7682 if (!p->implicit)
7683 constructor_range_stack = p->range_stack;
7684 RESTORE_SPELLING_DEPTH (constructor_depth);
7686 constructor_stack = p->next;
7687 free (p);
7689 if (ret.value == 0 && constructor_stack == 0)
7690 ret.value = error_mark_node;
7691 return ret;
7694 /* Common handling for both array range and field name designators.
7695 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7697 static int
7698 set_designator (location_t loc, int array,
7699 struct obstack *braced_init_obstack)
7701 tree subtype;
7702 enum tree_code subcode;
7704 /* Don't die if an entire brace-pair level is superfluous
7705 in the containing level. */
7706 if (constructor_type == 0)
7707 return 1;
7709 /* If there were errors in this designator list already, bail out
7710 silently. */
7711 if (designator_erroneous)
7712 return 1;
7714 if (!designator_depth)
7716 gcc_assert (!constructor_range_stack);
7718 /* Designator list starts at the level of closest explicit
7719 braces. */
7720 while (constructor_stack->implicit)
7721 process_init_element (input_location,
7722 pop_init_level (loc, 1, braced_init_obstack),
7723 true, braced_init_obstack);
7724 constructor_designated = 1;
7725 return 0;
7728 switch (TREE_CODE (constructor_type))
7730 case RECORD_TYPE:
7731 case UNION_TYPE:
7732 subtype = TREE_TYPE (constructor_fields);
7733 if (subtype != error_mark_node)
7734 subtype = TYPE_MAIN_VARIANT (subtype);
7735 break;
7736 case ARRAY_TYPE:
7737 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7738 break;
7739 default:
7740 gcc_unreachable ();
7743 subcode = TREE_CODE (subtype);
7744 if (array && subcode != ARRAY_TYPE)
7746 error_init (loc, "array index in non-array initializer");
7747 return 1;
7749 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7751 error_init (loc, "field name not in record or union initializer");
7752 return 1;
7755 constructor_designated = 1;
7756 push_init_level (loc, 2, braced_init_obstack);
7757 return 0;
7760 /* If there are range designators in designator list, push a new designator
7761 to constructor_range_stack. RANGE_END is end of such stack range or
7762 NULL_TREE if there is no range designator at this level. */
7764 static void
7765 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7767 struct constructor_range_stack *p;
7769 p = (struct constructor_range_stack *)
7770 obstack_alloc (braced_init_obstack,
7771 sizeof (struct constructor_range_stack));
7772 p->prev = constructor_range_stack;
7773 p->next = 0;
7774 p->fields = constructor_fields;
7775 p->range_start = constructor_index;
7776 p->index = constructor_index;
7777 p->stack = constructor_stack;
7778 p->range_end = range_end;
7779 if (constructor_range_stack)
7780 constructor_range_stack->next = p;
7781 constructor_range_stack = p;
7784 /* Within an array initializer, specify the next index to be initialized.
7785 FIRST is that index. If LAST is nonzero, then initialize a range
7786 of indices, running from FIRST through LAST. */
7788 void
7789 set_init_index (location_t loc, tree first, tree last,
7790 struct obstack *braced_init_obstack)
7792 if (set_designator (loc, 1, braced_init_obstack))
7793 return;
7795 designator_erroneous = 1;
7797 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7798 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7800 error_init (loc, "array index in initializer not of integer type");
7801 return;
7804 if (TREE_CODE (first) != INTEGER_CST)
7806 first = c_fully_fold (first, false, NULL);
7807 if (TREE_CODE (first) == INTEGER_CST)
7808 pedwarn_init (loc, OPT_Wpedantic,
7809 "array index in initializer is not "
7810 "an integer constant expression");
7813 if (last && TREE_CODE (last) != INTEGER_CST)
7815 last = c_fully_fold (last, false, NULL);
7816 if (TREE_CODE (last) == INTEGER_CST)
7817 pedwarn_init (loc, OPT_Wpedantic,
7818 "array index in initializer is not "
7819 "an integer constant expression");
7822 if (TREE_CODE (first) != INTEGER_CST)
7823 error_init (loc, "nonconstant array index in initializer");
7824 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7825 error_init (loc, "nonconstant array index in initializer");
7826 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7827 error_init (loc, "array index in non-array initializer");
7828 else if (tree_int_cst_sgn (first) == -1)
7829 error_init (loc, "array index in initializer exceeds array bounds");
7830 else if (constructor_max_index
7831 && tree_int_cst_lt (constructor_max_index, first))
7832 error_init (loc, "array index in initializer exceeds array bounds");
7833 else
7835 constant_expression_warning (first);
7836 if (last)
7837 constant_expression_warning (last);
7838 constructor_index = convert (bitsizetype, first);
7839 if (tree_int_cst_lt (constructor_index, first))
7841 constructor_index = copy_node (constructor_index);
7842 TREE_OVERFLOW (constructor_index) = 1;
7845 if (last)
7847 if (tree_int_cst_equal (first, last))
7848 last = 0;
7849 else if (tree_int_cst_lt (last, first))
7851 error_init (loc, "empty index range in initializer");
7852 last = 0;
7854 else
7856 last = convert (bitsizetype, last);
7857 if (constructor_max_index != 0
7858 && tree_int_cst_lt (constructor_max_index, last))
7860 error_init (loc, "array index range in initializer exceeds "
7861 "array bounds");
7862 last = 0;
7867 designator_depth++;
7868 designator_erroneous = 0;
7869 if (constructor_range_stack || last)
7870 push_range_stack (last, braced_init_obstack);
7874 /* Within a struct initializer, specify the next field to be initialized. */
7876 void
7877 set_init_label (location_t loc, tree fieldname,
7878 struct obstack *braced_init_obstack)
7880 tree field;
7882 if (set_designator (loc, 0, braced_init_obstack))
7883 return;
7885 designator_erroneous = 1;
7887 if (TREE_CODE (constructor_type) != RECORD_TYPE
7888 && TREE_CODE (constructor_type) != UNION_TYPE)
7890 error_init (loc, "field name not in record or union initializer");
7891 return;
7894 field = lookup_field (constructor_type, fieldname);
7896 if (field == 0)
7897 error_at (loc, "unknown field %qE specified in initializer", fieldname);
7898 else
7901 constructor_fields = TREE_VALUE (field);
7902 designator_depth++;
7903 designator_erroneous = 0;
7904 if (constructor_range_stack)
7905 push_range_stack (NULL_TREE, braced_init_obstack);
7906 field = TREE_CHAIN (field);
7907 if (field)
7909 if (set_designator (loc, 0, braced_init_obstack))
7910 return;
7913 while (field != NULL_TREE);
7916 /* Add a new initializer to the tree of pending initializers. PURPOSE
7917 identifies the initializer, either array index or field in a structure.
7918 VALUE is the value of that index or field. If ORIGTYPE is not
7919 NULL_TREE, it is the original type of VALUE.
7921 IMPLICIT is true if value comes from pop_init_level (1),
7922 the new initializer has been merged with the existing one
7923 and thus no warnings should be emitted about overriding an
7924 existing initializer. */
7926 static void
7927 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7928 bool implicit, struct obstack *braced_init_obstack)
7930 struct init_node *p, **q, *r;
7932 q = &constructor_pending_elts;
7933 p = 0;
7935 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7937 while (*q != 0)
7939 p = *q;
7940 if (tree_int_cst_lt (purpose, p->purpose))
7941 q = &p->left;
7942 else if (tree_int_cst_lt (p->purpose, purpose))
7943 q = &p->right;
7944 else
7946 if (!implicit)
7948 if (TREE_SIDE_EFFECTS (p->value))
7949 warning_init (loc, OPT_Woverride_init_side_effects,
7950 "initialized field with side-effects "
7951 "overwritten");
7952 else if (warn_override_init)
7953 warning_init (loc, OPT_Woverride_init,
7954 "initialized field overwritten");
7956 p->value = value;
7957 p->origtype = origtype;
7958 return;
7962 else
7964 tree bitpos;
7966 bitpos = bit_position (purpose);
7967 while (*q != NULL)
7969 p = *q;
7970 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7971 q = &p->left;
7972 else if (p->purpose != purpose)
7973 q = &p->right;
7974 else
7976 if (!implicit)
7978 if (TREE_SIDE_EFFECTS (p->value))
7979 warning_init (loc, OPT_Woverride_init_side_effects,
7980 "initialized field with side-effects "
7981 "overwritten");
7982 else if (warn_override_init)
7983 warning_init (loc, OPT_Woverride_init,
7984 "initialized field overwritten");
7986 p->value = value;
7987 p->origtype = origtype;
7988 return;
7993 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7994 sizeof (struct init_node));
7995 r->purpose = purpose;
7996 r->value = value;
7997 r->origtype = origtype;
7999 *q = r;
8000 r->parent = p;
8001 r->left = 0;
8002 r->right = 0;
8003 r->balance = 0;
8005 while (p)
8007 struct init_node *s;
8009 if (r == p->left)
8011 if (p->balance == 0)
8012 p->balance = -1;
8013 else if (p->balance < 0)
8015 if (r->balance < 0)
8017 /* L rotation. */
8018 p->left = r->right;
8019 if (p->left)
8020 p->left->parent = p;
8021 r->right = p;
8023 p->balance = 0;
8024 r->balance = 0;
8026 s = p->parent;
8027 p->parent = r;
8028 r->parent = s;
8029 if (s)
8031 if (s->left == p)
8032 s->left = r;
8033 else
8034 s->right = r;
8036 else
8037 constructor_pending_elts = r;
8039 else
8041 /* LR rotation. */
8042 struct init_node *t = r->right;
8044 r->right = t->left;
8045 if (r->right)
8046 r->right->parent = r;
8047 t->left = r;
8049 p->left = t->right;
8050 if (p->left)
8051 p->left->parent = p;
8052 t->right = p;
8054 p->balance = t->balance < 0;
8055 r->balance = -(t->balance > 0);
8056 t->balance = 0;
8058 s = p->parent;
8059 p->parent = t;
8060 r->parent = t;
8061 t->parent = s;
8062 if (s)
8064 if (s->left == p)
8065 s->left = t;
8066 else
8067 s->right = t;
8069 else
8070 constructor_pending_elts = t;
8072 break;
8074 else
8076 /* p->balance == +1; growth of left side balances the node. */
8077 p->balance = 0;
8078 break;
8081 else /* r == p->right */
8083 if (p->balance == 0)
8084 /* Growth propagation from right side. */
8085 p->balance++;
8086 else if (p->balance > 0)
8088 if (r->balance > 0)
8090 /* R rotation. */
8091 p->right = r->left;
8092 if (p->right)
8093 p->right->parent = p;
8094 r->left = p;
8096 p->balance = 0;
8097 r->balance = 0;
8099 s = p->parent;
8100 p->parent = r;
8101 r->parent = s;
8102 if (s)
8104 if (s->left == p)
8105 s->left = r;
8106 else
8107 s->right = r;
8109 else
8110 constructor_pending_elts = r;
8112 else /* r->balance == -1 */
8114 /* RL rotation */
8115 struct init_node *t = r->left;
8117 r->left = t->right;
8118 if (r->left)
8119 r->left->parent = r;
8120 t->right = r;
8122 p->right = t->left;
8123 if (p->right)
8124 p->right->parent = p;
8125 t->left = p;
8127 r->balance = (t->balance < 0);
8128 p->balance = -(t->balance > 0);
8129 t->balance = 0;
8131 s = p->parent;
8132 p->parent = t;
8133 r->parent = t;
8134 t->parent = s;
8135 if (s)
8137 if (s->left == p)
8138 s->left = t;
8139 else
8140 s->right = t;
8142 else
8143 constructor_pending_elts = t;
8145 break;
8147 else
8149 /* p->balance == -1; growth of right side balances the node. */
8150 p->balance = 0;
8151 break;
8155 r = p;
8156 p = p->parent;
8160 /* Build AVL tree from a sorted chain. */
8162 static void
8163 set_nonincremental_init (struct obstack * braced_init_obstack)
8165 unsigned HOST_WIDE_INT ix;
8166 tree index, value;
8168 if (TREE_CODE (constructor_type) != RECORD_TYPE
8169 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8170 return;
8172 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8173 add_pending_init (input_location, index, value, NULL_TREE, true,
8174 braced_init_obstack);
8175 constructor_elements = NULL;
8176 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8178 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8179 /* Skip any nameless bit fields at the beginning. */
8180 while (constructor_unfilled_fields != 0
8181 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8182 && DECL_NAME (constructor_unfilled_fields) == 0)
8183 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8186 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8188 if (TYPE_DOMAIN (constructor_type))
8189 constructor_unfilled_index
8190 = convert (bitsizetype,
8191 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8192 else
8193 constructor_unfilled_index = bitsize_zero_node;
8195 constructor_incremental = 0;
8198 /* Build AVL tree from a string constant. */
8200 static void
8201 set_nonincremental_init_from_string (tree str,
8202 struct obstack * braced_init_obstack)
8204 tree value, purpose, type;
8205 HOST_WIDE_INT val[2];
8206 const char *p, *end;
8207 int byte, wchar_bytes, charwidth, bitpos;
8209 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8211 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8212 charwidth = TYPE_PRECISION (char_type_node);
8213 type = TREE_TYPE (constructor_type);
8214 p = TREE_STRING_POINTER (str);
8215 end = p + TREE_STRING_LENGTH (str);
8217 for (purpose = bitsize_zero_node;
8218 p < end
8219 && !(constructor_max_index
8220 && tree_int_cst_lt (constructor_max_index, purpose));
8221 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8223 if (wchar_bytes == 1)
8225 val[0] = (unsigned char) *p++;
8226 val[1] = 0;
8228 else
8230 val[1] = 0;
8231 val[0] = 0;
8232 for (byte = 0; byte < wchar_bytes; byte++)
8234 if (BYTES_BIG_ENDIAN)
8235 bitpos = (wchar_bytes - byte - 1) * charwidth;
8236 else
8237 bitpos = byte * charwidth;
8238 val[bitpos % HOST_BITS_PER_WIDE_INT]
8239 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8240 << (bitpos % HOST_BITS_PER_WIDE_INT);
8244 if (!TYPE_UNSIGNED (type))
8246 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8247 if (bitpos < HOST_BITS_PER_WIDE_INT)
8249 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8251 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8252 val[1] = -1;
8255 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8257 if (val[0] < 0)
8258 val[1] = -1;
8260 else if (val[1] & (((HOST_WIDE_INT) 1)
8261 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8262 val[1] |= ((HOST_WIDE_INT) -1)
8263 << (bitpos - HOST_BITS_PER_WIDE_INT);
8266 value = wide_int_to_tree (type,
8267 wide_int::from_array (val, 2,
8268 HOST_BITS_PER_WIDE_INT * 2));
8269 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8270 braced_init_obstack);
8273 constructor_incremental = 0;
8276 /* Return value of FIELD in pending initializer or zero if the field was
8277 not initialized yet. */
8279 static tree
8280 find_init_member (tree field, struct obstack * braced_init_obstack)
8282 struct init_node *p;
8284 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8286 if (constructor_incremental
8287 && tree_int_cst_lt (field, constructor_unfilled_index))
8288 set_nonincremental_init (braced_init_obstack);
8290 p = constructor_pending_elts;
8291 while (p)
8293 if (tree_int_cst_lt (field, p->purpose))
8294 p = p->left;
8295 else if (tree_int_cst_lt (p->purpose, field))
8296 p = p->right;
8297 else
8298 return p->value;
8301 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8303 tree bitpos = bit_position (field);
8305 if (constructor_incremental
8306 && (!constructor_unfilled_fields
8307 || tree_int_cst_lt (bitpos,
8308 bit_position (constructor_unfilled_fields))))
8309 set_nonincremental_init (braced_init_obstack);
8311 p = constructor_pending_elts;
8312 while (p)
8314 if (field == p->purpose)
8315 return p->value;
8316 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8317 p = p->left;
8318 else
8319 p = p->right;
8322 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8324 if (!vec_safe_is_empty (constructor_elements)
8325 && (constructor_elements->last ().index == field))
8326 return constructor_elements->last ().value;
8328 return 0;
8331 /* "Output" the next constructor element.
8332 At top level, really output it to assembler code now.
8333 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8334 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8335 TYPE is the data type that the containing data type wants here.
8336 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8337 If VALUE is a string constant, STRICT_STRING is true if it is
8338 unparenthesized or we should not warn here for it being parenthesized.
8339 For other types of VALUE, STRICT_STRING is not used.
8341 PENDING if non-nil means output pending elements that belong
8342 right after this element. (PENDING is normally 1;
8343 it is 0 while outputting pending elements, to avoid recursion.)
8345 IMPLICIT is true if value comes from pop_init_level (1),
8346 the new initializer has been merged with the existing one
8347 and thus no warnings should be emitted about overriding an
8348 existing initializer. */
8350 static void
8351 output_init_element (location_t loc, tree value, tree origtype,
8352 bool strict_string, tree type, tree field, int pending,
8353 bool implicit, struct obstack * braced_init_obstack)
8355 tree semantic_type = NULL_TREE;
8356 bool maybe_const = true;
8357 bool npc;
8359 if (type == error_mark_node || value == error_mark_node)
8361 constructor_erroneous = 1;
8362 return;
8364 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8365 && (TREE_CODE (value) == STRING_CST
8366 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8367 && !(TREE_CODE (value) == STRING_CST
8368 && TREE_CODE (type) == ARRAY_TYPE
8369 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8370 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8371 TYPE_MAIN_VARIANT (type)))
8372 value = array_to_pointer_conversion (input_location, value);
8374 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8375 && require_constant_value && pending)
8377 /* As an extension, allow initializing objects with static storage
8378 duration with compound literals (which are then treated just as
8379 the brace enclosed list they contain). */
8380 if (flag_isoc99)
8381 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8382 "constant");
8383 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8384 value = DECL_INITIAL (decl);
8387 npc = null_pointer_constant_p (value);
8388 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8390 semantic_type = TREE_TYPE (value);
8391 value = TREE_OPERAND (value, 0);
8393 value = c_fully_fold (value, require_constant_value, &maybe_const);
8395 if (value == error_mark_node)
8396 constructor_erroneous = 1;
8397 else if (!TREE_CONSTANT (value))
8398 constructor_constant = 0;
8399 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8400 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8401 || TREE_CODE (constructor_type) == UNION_TYPE)
8402 && DECL_C_BIT_FIELD (field)
8403 && TREE_CODE (value) != INTEGER_CST))
8404 constructor_simple = 0;
8405 if (!maybe_const)
8406 constructor_nonconst = 1;
8408 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8410 if (require_constant_value)
8412 error_init (loc, "initializer element is not constant");
8413 value = error_mark_node;
8415 else if (require_constant_elements)
8416 pedwarn (loc, OPT_Wpedantic,
8417 "initializer element is not computable at load time");
8419 else if (!maybe_const
8420 && (require_constant_value || require_constant_elements))
8421 pedwarn_init (loc, OPT_Wpedantic,
8422 "initializer element is not a constant expression");
8424 /* Issue -Wc++-compat warnings about initializing a bitfield with
8425 enum type. */
8426 if (warn_cxx_compat
8427 && field != NULL_TREE
8428 && TREE_CODE (field) == FIELD_DECL
8429 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8430 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8431 != TYPE_MAIN_VARIANT (type))
8432 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8434 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8435 if (checktype != error_mark_node
8436 && (TYPE_MAIN_VARIANT (checktype)
8437 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8438 warning_init (loc, OPT_Wc___compat,
8439 "enum conversion in initialization is invalid in C++");
8442 /* If this field is empty (and not at the end of structure),
8443 don't do anything other than checking the initializer. */
8444 if (field
8445 && (TREE_TYPE (field) == error_mark_node
8446 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8447 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8448 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8449 || DECL_CHAIN (field)))))
8450 return;
8452 if (semantic_type)
8453 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8454 value = digest_init (loc, type, value, origtype, npc, strict_string,
8455 require_constant_value);
8456 if (value == error_mark_node)
8458 constructor_erroneous = 1;
8459 return;
8461 if (require_constant_value || require_constant_elements)
8462 constant_expression_warning (value);
8464 /* If this element doesn't come next in sequence,
8465 put it on constructor_pending_elts. */
8466 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8467 && (!constructor_incremental
8468 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8470 if (constructor_incremental
8471 && tree_int_cst_lt (field, constructor_unfilled_index))
8472 set_nonincremental_init (braced_init_obstack);
8474 add_pending_init (loc, field, value, origtype, implicit,
8475 braced_init_obstack);
8476 return;
8478 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8479 && (!constructor_incremental
8480 || field != constructor_unfilled_fields))
8482 /* We do this for records but not for unions. In a union,
8483 no matter which field is specified, it can be initialized
8484 right away since it starts at the beginning of the union. */
8485 if (constructor_incremental)
8487 if (!constructor_unfilled_fields)
8488 set_nonincremental_init (braced_init_obstack);
8489 else
8491 tree bitpos, unfillpos;
8493 bitpos = bit_position (field);
8494 unfillpos = bit_position (constructor_unfilled_fields);
8496 if (tree_int_cst_lt (bitpos, unfillpos))
8497 set_nonincremental_init (braced_init_obstack);
8501 add_pending_init (loc, field, value, origtype, implicit,
8502 braced_init_obstack);
8503 return;
8505 else if (TREE_CODE (constructor_type) == UNION_TYPE
8506 && !vec_safe_is_empty (constructor_elements))
8508 if (!implicit)
8510 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8511 warning_init (loc, OPT_Woverride_init_side_effects,
8512 "initialized field with side-effects overwritten");
8513 else if (warn_override_init)
8514 warning_init (loc, OPT_Woverride_init,
8515 "initialized field overwritten");
8518 /* We can have just one union field set. */
8519 constructor_elements = NULL;
8522 /* Otherwise, output this element either to
8523 constructor_elements or to the assembler file. */
8525 constructor_elt celt = {field, value};
8526 vec_safe_push (constructor_elements, celt);
8528 /* Advance the variable that indicates sequential elements output. */
8529 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8530 constructor_unfilled_index
8531 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8532 bitsize_one_node);
8533 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8535 constructor_unfilled_fields
8536 = DECL_CHAIN (constructor_unfilled_fields);
8538 /* Skip any nameless bit fields. */
8539 while (constructor_unfilled_fields != 0
8540 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8541 && DECL_NAME (constructor_unfilled_fields) == 0)
8542 constructor_unfilled_fields =
8543 DECL_CHAIN (constructor_unfilled_fields);
8545 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8546 constructor_unfilled_fields = 0;
8548 /* Now output any pending elements which have become next. */
8549 if (pending)
8550 output_pending_init_elements (0, braced_init_obstack);
8553 /* Output any pending elements which have become next.
8554 As we output elements, constructor_unfilled_{fields,index}
8555 advances, which may cause other elements to become next;
8556 if so, they too are output.
8558 If ALL is 0, we return when there are
8559 no more pending elements to output now.
8561 If ALL is 1, we output space as necessary so that
8562 we can output all the pending elements. */
8563 static void
8564 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8566 struct init_node *elt = constructor_pending_elts;
8567 tree next;
8569 retry:
8571 /* Look through the whole pending tree.
8572 If we find an element that should be output now,
8573 output it. Otherwise, set NEXT to the element
8574 that comes first among those still pending. */
8576 next = 0;
8577 while (elt)
8579 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8581 if (tree_int_cst_equal (elt->purpose,
8582 constructor_unfilled_index))
8583 output_init_element (input_location, elt->value, elt->origtype,
8584 true, TREE_TYPE (constructor_type),
8585 constructor_unfilled_index, 0, false,
8586 braced_init_obstack);
8587 else if (tree_int_cst_lt (constructor_unfilled_index,
8588 elt->purpose))
8590 /* Advance to the next smaller node. */
8591 if (elt->left)
8592 elt = elt->left;
8593 else
8595 /* We have reached the smallest node bigger than the
8596 current unfilled index. Fill the space first. */
8597 next = elt->purpose;
8598 break;
8601 else
8603 /* Advance to the next bigger node. */
8604 if (elt->right)
8605 elt = elt->right;
8606 else
8608 /* We have reached the biggest node in a subtree. Find
8609 the parent of it, which is the next bigger node. */
8610 while (elt->parent && elt->parent->right == elt)
8611 elt = elt->parent;
8612 elt = elt->parent;
8613 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8614 elt->purpose))
8616 next = elt->purpose;
8617 break;
8622 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8623 || TREE_CODE (constructor_type) == UNION_TYPE)
8625 tree ctor_unfilled_bitpos, elt_bitpos;
8627 /* If the current record is complete we are done. */
8628 if (constructor_unfilled_fields == 0)
8629 break;
8631 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8632 elt_bitpos = bit_position (elt->purpose);
8633 /* We can't compare fields here because there might be empty
8634 fields in between. */
8635 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8637 constructor_unfilled_fields = elt->purpose;
8638 output_init_element (input_location, elt->value, elt->origtype,
8639 true, TREE_TYPE (elt->purpose),
8640 elt->purpose, 0, false,
8641 braced_init_obstack);
8643 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8645 /* Advance to the next smaller node. */
8646 if (elt->left)
8647 elt = elt->left;
8648 else
8650 /* We have reached the smallest node bigger than the
8651 current unfilled field. Fill the space first. */
8652 next = elt->purpose;
8653 break;
8656 else
8658 /* Advance to the next bigger node. */
8659 if (elt->right)
8660 elt = elt->right;
8661 else
8663 /* We have reached the biggest node in a subtree. Find
8664 the parent of it, which is the next bigger node. */
8665 while (elt->parent && elt->parent->right == elt)
8666 elt = elt->parent;
8667 elt = elt->parent;
8668 if (elt
8669 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8670 bit_position (elt->purpose))))
8672 next = elt->purpose;
8673 break;
8680 /* Ordinarily return, but not if we want to output all
8681 and there are elements left. */
8682 if (!(all && next != 0))
8683 return;
8685 /* If it's not incremental, just skip over the gap, so that after
8686 jumping to retry we will output the next successive element. */
8687 if (TREE_CODE (constructor_type) == RECORD_TYPE
8688 || TREE_CODE (constructor_type) == UNION_TYPE)
8689 constructor_unfilled_fields = next;
8690 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8691 constructor_unfilled_index = next;
8693 /* ELT now points to the node in the pending tree with the next
8694 initializer to output. */
8695 goto retry;
8698 /* Add one non-braced element to the current constructor level.
8699 This adjusts the current position within the constructor's type.
8700 This may also start or terminate implicit levels
8701 to handle a partly-braced initializer.
8703 Once this has found the correct level for the new element,
8704 it calls output_init_element.
8706 IMPLICIT is true if value comes from pop_init_level (1),
8707 the new initializer has been merged with the existing one
8708 and thus no warnings should be emitted about overriding an
8709 existing initializer. */
8711 void
8712 process_init_element (location_t loc, struct c_expr value, bool implicit,
8713 struct obstack * braced_init_obstack)
8715 tree orig_value = value.value;
8716 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8717 bool strict_string = value.original_code == STRING_CST;
8718 bool was_designated = designator_depth != 0;
8720 designator_depth = 0;
8721 designator_erroneous = 0;
8723 if (!implicit && value.value && !integer_zerop (value.value))
8724 constructor_zeroinit = 0;
8726 /* Handle superfluous braces around string cst as in
8727 char x[] = {"foo"}; */
8728 if (string_flag
8729 && constructor_type
8730 && !was_designated
8731 && TREE_CODE (constructor_type) == ARRAY_TYPE
8732 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8733 && integer_zerop (constructor_unfilled_index))
8735 if (constructor_stack->replacement_value.value)
8736 error_init (loc, "excess elements in char array initializer");
8737 constructor_stack->replacement_value = value;
8738 return;
8741 if (constructor_stack->replacement_value.value != 0)
8743 error_init (loc, "excess elements in struct initializer");
8744 return;
8747 /* Ignore elements of a brace group if it is entirely superfluous
8748 and has already been diagnosed. */
8749 if (constructor_type == 0)
8750 return;
8752 if (!implicit && warn_designated_init && !was_designated
8753 && TREE_CODE (constructor_type) == RECORD_TYPE
8754 && lookup_attribute ("designated_init",
8755 TYPE_ATTRIBUTES (constructor_type)))
8756 warning_init (loc,
8757 OPT_Wdesignated_init,
8758 "positional initialization of field "
8759 "in %<struct%> declared with %<designated_init%> attribute");
8761 /* If we've exhausted any levels that didn't have braces,
8762 pop them now. */
8763 while (constructor_stack->implicit)
8765 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8766 || TREE_CODE (constructor_type) == UNION_TYPE)
8767 && constructor_fields == 0)
8768 process_init_element (loc,
8769 pop_init_level (loc, 1, braced_init_obstack),
8770 true, braced_init_obstack);
8771 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8772 || VECTOR_TYPE_P (constructor_type))
8773 && constructor_max_index
8774 && tree_int_cst_lt (constructor_max_index,
8775 constructor_index))
8776 process_init_element (loc,
8777 pop_init_level (loc, 1, braced_init_obstack),
8778 true, braced_init_obstack);
8779 else
8780 break;
8783 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8784 if (constructor_range_stack)
8786 /* If value is a compound literal and we'll be just using its
8787 content, don't put it into a SAVE_EXPR. */
8788 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8789 || !require_constant_value)
8791 tree semantic_type = NULL_TREE;
8792 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8794 semantic_type = TREE_TYPE (value.value);
8795 value.value = TREE_OPERAND (value.value, 0);
8797 value.value = c_save_expr (value.value);
8798 if (semantic_type)
8799 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8800 value.value);
8804 while (1)
8806 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8808 tree fieldtype;
8809 enum tree_code fieldcode;
8811 if (constructor_fields == 0)
8813 pedwarn_init (loc, 0, "excess elements in struct initializer");
8814 break;
8817 fieldtype = TREE_TYPE (constructor_fields);
8818 if (fieldtype != error_mark_node)
8819 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8820 fieldcode = TREE_CODE (fieldtype);
8822 /* Error for non-static initialization of a flexible array member. */
8823 if (fieldcode == ARRAY_TYPE
8824 && !require_constant_value
8825 && TYPE_SIZE (fieldtype) == NULL_TREE
8826 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8828 error_init (loc, "non-static initialization of a flexible "
8829 "array member");
8830 break;
8833 /* Error for initialization of a flexible array member with
8834 a string constant if the structure is in an array. E.g.:
8835 struct S { int x; char y[]; };
8836 struct S s[] = { { 1, "foo" } };
8837 is invalid. */
8838 if (string_flag
8839 && fieldcode == ARRAY_TYPE
8840 && constructor_depth > 1
8841 && TYPE_SIZE (fieldtype) == NULL_TREE
8842 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8844 bool in_array_p = false;
8845 for (struct constructor_stack *p = constructor_stack;
8846 p && p->type; p = p->next)
8847 if (TREE_CODE (p->type) == ARRAY_TYPE)
8849 in_array_p = true;
8850 break;
8852 if (in_array_p)
8854 error_init (loc, "initialization of flexible array "
8855 "member in a nested context");
8856 break;
8860 /* Accept a string constant to initialize a subarray. */
8861 if (value.value != 0
8862 && fieldcode == ARRAY_TYPE
8863 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8864 && string_flag)
8865 value.value = orig_value;
8866 /* Otherwise, if we have come to a subaggregate,
8867 and we don't have an element of its type, push into it. */
8868 else if (value.value != 0
8869 && value.value != error_mark_node
8870 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8871 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8872 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8874 push_init_level (loc, 1, braced_init_obstack);
8875 continue;
8878 if (value.value)
8880 push_member_name (constructor_fields);
8881 output_init_element (loc, value.value, value.original_type,
8882 strict_string, fieldtype,
8883 constructor_fields, 1, implicit,
8884 braced_init_obstack);
8885 RESTORE_SPELLING_DEPTH (constructor_depth);
8887 else
8888 /* Do the bookkeeping for an element that was
8889 directly output as a constructor. */
8891 /* For a record, keep track of end position of last field. */
8892 if (DECL_SIZE (constructor_fields))
8893 constructor_bit_index
8894 = size_binop_loc (input_location, PLUS_EXPR,
8895 bit_position (constructor_fields),
8896 DECL_SIZE (constructor_fields));
8898 /* If the current field was the first one not yet written out,
8899 it isn't now, so update. */
8900 if (constructor_unfilled_fields == constructor_fields)
8902 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8903 /* Skip any nameless bit fields. */
8904 while (constructor_unfilled_fields != 0
8905 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8906 && DECL_NAME (constructor_unfilled_fields) == 0)
8907 constructor_unfilled_fields =
8908 DECL_CHAIN (constructor_unfilled_fields);
8912 constructor_fields = DECL_CHAIN (constructor_fields);
8913 /* Skip any nameless bit fields at the beginning. */
8914 while (constructor_fields != 0
8915 && DECL_C_BIT_FIELD (constructor_fields)
8916 && DECL_NAME (constructor_fields) == 0)
8917 constructor_fields = DECL_CHAIN (constructor_fields);
8919 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8921 tree fieldtype;
8922 enum tree_code fieldcode;
8924 if (constructor_fields == 0)
8926 pedwarn_init (loc, 0,
8927 "excess elements in union initializer");
8928 break;
8931 fieldtype = TREE_TYPE (constructor_fields);
8932 if (fieldtype != error_mark_node)
8933 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8934 fieldcode = TREE_CODE (fieldtype);
8936 /* Warn that traditional C rejects initialization of unions.
8937 We skip the warning if the value is zero. This is done
8938 under the assumption that the zero initializer in user
8939 code appears conditioned on e.g. __STDC__ to avoid
8940 "missing initializer" warnings and relies on default
8941 initialization to zero in the traditional C case.
8942 We also skip the warning if the initializer is designated,
8943 again on the assumption that this must be conditional on
8944 __STDC__ anyway (and we've already complained about the
8945 member-designator already). */
8946 if (!in_system_header_at (input_location) && !constructor_designated
8947 && !(value.value && (integer_zerop (value.value)
8948 || real_zerop (value.value))))
8949 warning (OPT_Wtraditional, "traditional C rejects initialization "
8950 "of unions");
8952 /* Accept a string constant to initialize a subarray. */
8953 if (value.value != 0
8954 && fieldcode == ARRAY_TYPE
8955 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8956 && string_flag)
8957 value.value = orig_value;
8958 /* Otherwise, if we have come to a subaggregate,
8959 and we don't have an element of its type, push into it. */
8960 else if (value.value != 0
8961 && value.value != error_mark_node
8962 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8963 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8964 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8966 push_init_level (loc, 1, braced_init_obstack);
8967 continue;
8970 if (value.value)
8972 push_member_name (constructor_fields);
8973 output_init_element (loc, value.value, value.original_type,
8974 strict_string, fieldtype,
8975 constructor_fields, 1, implicit,
8976 braced_init_obstack);
8977 RESTORE_SPELLING_DEPTH (constructor_depth);
8979 else
8980 /* Do the bookkeeping for an element that was
8981 directly output as a constructor. */
8983 constructor_bit_index = DECL_SIZE (constructor_fields);
8984 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8987 constructor_fields = 0;
8989 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8991 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8992 enum tree_code eltcode = TREE_CODE (elttype);
8994 /* Accept a string constant to initialize a subarray. */
8995 if (value.value != 0
8996 && eltcode == ARRAY_TYPE
8997 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8998 && string_flag)
8999 value.value = orig_value;
9000 /* Otherwise, if we have come to a subaggregate,
9001 and we don't have an element of its type, push into it. */
9002 else if (value.value != 0
9003 && value.value != error_mark_node
9004 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9005 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9006 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9008 push_init_level (loc, 1, braced_init_obstack);
9009 continue;
9012 if (constructor_max_index != 0
9013 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9014 || integer_all_onesp (constructor_max_index)))
9016 pedwarn_init (loc, 0,
9017 "excess elements in array initializer");
9018 break;
9021 /* Now output the actual element. */
9022 if (value.value)
9024 push_array_bounds (tree_to_uhwi (constructor_index));
9025 output_init_element (loc, value.value, value.original_type,
9026 strict_string, elttype,
9027 constructor_index, 1, implicit,
9028 braced_init_obstack);
9029 RESTORE_SPELLING_DEPTH (constructor_depth);
9032 constructor_index
9033 = size_binop_loc (input_location, PLUS_EXPR,
9034 constructor_index, bitsize_one_node);
9036 if (!value.value)
9037 /* If we are doing the bookkeeping for an element that was
9038 directly output as a constructor, we must update
9039 constructor_unfilled_index. */
9040 constructor_unfilled_index = constructor_index;
9042 else if (VECTOR_TYPE_P (constructor_type))
9044 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9046 /* Do a basic check of initializer size. Note that vectors
9047 always have a fixed size derived from their type. */
9048 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9050 pedwarn_init (loc, 0,
9051 "excess elements in vector initializer");
9052 break;
9055 /* Now output the actual element. */
9056 if (value.value)
9058 if (TREE_CODE (value.value) == VECTOR_CST)
9059 elttype = TYPE_MAIN_VARIANT (constructor_type);
9060 output_init_element (loc, value.value, value.original_type,
9061 strict_string, elttype,
9062 constructor_index, 1, implicit,
9063 braced_init_obstack);
9066 constructor_index
9067 = size_binop_loc (input_location,
9068 PLUS_EXPR, constructor_index, bitsize_one_node);
9070 if (!value.value)
9071 /* If we are doing the bookkeeping for an element that was
9072 directly output as a constructor, we must update
9073 constructor_unfilled_index. */
9074 constructor_unfilled_index = constructor_index;
9077 /* Handle the sole element allowed in a braced initializer
9078 for a scalar variable. */
9079 else if (constructor_type != error_mark_node
9080 && constructor_fields == 0)
9082 pedwarn_init (loc, 0,
9083 "excess elements in scalar initializer");
9084 break;
9086 else
9088 if (value.value)
9089 output_init_element (loc, value.value, value.original_type,
9090 strict_string, constructor_type,
9091 NULL_TREE, 1, implicit,
9092 braced_init_obstack);
9093 constructor_fields = 0;
9096 /* Handle range initializers either at this level or anywhere higher
9097 in the designator stack. */
9098 if (constructor_range_stack)
9100 struct constructor_range_stack *p, *range_stack;
9101 int finish = 0;
9103 range_stack = constructor_range_stack;
9104 constructor_range_stack = 0;
9105 while (constructor_stack != range_stack->stack)
9107 gcc_assert (constructor_stack->implicit);
9108 process_init_element (loc,
9109 pop_init_level (loc, 1,
9110 braced_init_obstack),
9111 true, braced_init_obstack);
9113 for (p = range_stack;
9114 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9115 p = p->prev)
9117 gcc_assert (constructor_stack->implicit);
9118 process_init_element (loc,
9119 pop_init_level (loc, 1,
9120 braced_init_obstack),
9121 true, braced_init_obstack);
9124 p->index = size_binop_loc (input_location,
9125 PLUS_EXPR, p->index, bitsize_one_node);
9126 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9127 finish = 1;
9129 while (1)
9131 constructor_index = p->index;
9132 constructor_fields = p->fields;
9133 if (finish && p->range_end && p->index == p->range_start)
9135 finish = 0;
9136 p->prev = 0;
9138 p = p->next;
9139 if (!p)
9140 break;
9141 push_init_level (loc, 2, braced_init_obstack);
9142 p->stack = constructor_stack;
9143 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9144 p->index = p->range_start;
9147 if (!finish)
9148 constructor_range_stack = range_stack;
9149 continue;
9152 break;
9155 constructor_range_stack = 0;
9158 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9159 (guaranteed to be 'volatile' or null) and ARGS (represented using
9160 an ASM_EXPR node). */
9161 tree
9162 build_asm_stmt (tree cv_qualifier, tree args)
9164 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9165 ASM_VOLATILE_P (args) = 1;
9166 return add_stmt (args);
9169 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9170 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9171 SIMPLE indicates whether there was anything at all after the
9172 string in the asm expression -- asm("blah") and asm("blah" : )
9173 are subtly different. We use a ASM_EXPR node to represent this. */
9174 tree
9175 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9176 tree clobbers, tree labels, bool simple)
9178 tree tail;
9179 tree args;
9180 int i;
9181 const char *constraint;
9182 const char **oconstraints;
9183 bool allows_mem, allows_reg, is_inout;
9184 int ninputs, noutputs;
9186 ninputs = list_length (inputs);
9187 noutputs = list_length (outputs);
9188 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9190 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9192 /* Remove output conversions that change the type but not the mode. */
9193 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9195 tree output = TREE_VALUE (tail);
9197 output = c_fully_fold (output, false, NULL);
9199 /* ??? Really, this should not be here. Users should be using a
9200 proper lvalue, dammit. But there's a long history of using casts
9201 in the output operands. In cases like longlong.h, this becomes a
9202 primitive form of typechecking -- if the cast can be removed, then
9203 the output operand had a type of the proper width; otherwise we'll
9204 get an error. Gross, but ... */
9205 STRIP_NOPS (output);
9207 if (!lvalue_or_else (loc, output, lv_asm))
9208 output = error_mark_node;
9210 if (output != error_mark_node
9211 && (TREE_READONLY (output)
9212 || TYPE_READONLY (TREE_TYPE (output))
9213 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9214 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9215 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9216 readonly_error (loc, output, lv_asm);
9218 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9219 oconstraints[i] = constraint;
9221 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9222 &allows_mem, &allows_reg, &is_inout))
9224 /* If the operand is going to end up in memory,
9225 mark it addressable. */
9226 if (!allows_reg && !c_mark_addressable (output))
9227 output = error_mark_node;
9228 if (!(!allows_reg && allows_mem)
9229 && output != error_mark_node
9230 && VOID_TYPE_P (TREE_TYPE (output)))
9232 error_at (loc, "invalid use of void expression");
9233 output = error_mark_node;
9236 else
9237 output = error_mark_node;
9239 TREE_VALUE (tail) = output;
9242 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9244 tree input;
9246 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9247 input = TREE_VALUE (tail);
9249 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9250 oconstraints, &allows_mem, &allows_reg))
9252 /* If the operand is going to end up in memory,
9253 mark it addressable. */
9254 if (!allows_reg && allows_mem)
9256 input = c_fully_fold (input, false, NULL);
9258 /* Strip the nops as we allow this case. FIXME, this really
9259 should be rejected or made deprecated. */
9260 STRIP_NOPS (input);
9261 if (!c_mark_addressable (input))
9262 input = error_mark_node;
9264 else
9266 struct c_expr expr;
9267 memset (&expr, 0, sizeof (expr));
9268 expr.value = input;
9269 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9270 input = c_fully_fold (expr.value, false, NULL);
9272 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9274 error_at (loc, "invalid use of void expression");
9275 input = error_mark_node;
9279 else
9280 input = error_mark_node;
9282 TREE_VALUE (tail) = input;
9285 /* ASMs with labels cannot have outputs. This should have been
9286 enforced by the parser. */
9287 gcc_assert (outputs == NULL || labels == NULL);
9289 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9291 /* asm statements without outputs, including simple ones, are treated
9292 as volatile. */
9293 ASM_INPUT_P (args) = simple;
9294 ASM_VOLATILE_P (args) = (noutputs == 0);
9296 return args;
9299 /* Generate a goto statement to LABEL. LOC is the location of the
9300 GOTO. */
9302 tree
9303 c_finish_goto_label (location_t loc, tree label)
9305 tree decl = lookup_label_for_goto (loc, label);
9306 if (!decl)
9307 return NULL_TREE;
9308 TREE_USED (decl) = 1;
9310 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9311 SET_EXPR_LOCATION (t, loc);
9312 return add_stmt (t);
9316 /* Generate a computed goto statement to EXPR. LOC is the location of
9317 the GOTO. */
9319 tree
9320 c_finish_goto_ptr (location_t loc, tree expr)
9322 tree t;
9323 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9324 expr = c_fully_fold (expr, false, NULL);
9325 expr = convert (ptr_type_node, expr);
9326 t = build1 (GOTO_EXPR, void_type_node, expr);
9327 SET_EXPR_LOCATION (t, loc);
9328 return add_stmt (t);
9331 /* Generate a C `return' statement. RETVAL is the expression for what
9332 to return, or a null pointer for `return;' with no value. LOC is
9333 the location of the return statement, or the location of the expression,
9334 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9335 is the original type of RETVAL. */
9337 tree
9338 c_finish_return (location_t loc, tree retval, tree origtype)
9340 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9341 bool no_warning = false;
9342 bool npc = false;
9343 size_t rank = 0;
9345 if (TREE_THIS_VOLATILE (current_function_decl))
9346 warning_at (loc, 0,
9347 "function declared %<noreturn%> has a %<return%> statement");
9349 if (flag_cilkplus && contains_array_notation_expr (retval))
9351 /* Array notations are allowed in a return statement if it is inside a
9352 built-in array notation reduction function. */
9353 if (!find_rank (loc, retval, retval, false, &rank))
9354 return error_mark_node;
9355 if (rank >= 1)
9357 error_at (loc, "array notation expression cannot be used as a "
9358 "return value");
9359 return error_mark_node;
9362 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9364 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9365 "allowed");
9366 return error_mark_node;
9368 if (retval)
9370 tree semantic_type = NULL_TREE;
9371 npc = null_pointer_constant_p (retval);
9372 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9374 semantic_type = TREE_TYPE (retval);
9375 retval = TREE_OPERAND (retval, 0);
9377 retval = c_fully_fold (retval, false, NULL);
9378 if (semantic_type)
9379 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9382 if (!retval)
9384 current_function_returns_null = 1;
9385 if ((warn_return_type || flag_isoc99)
9386 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9388 if (flag_isoc99)
9389 pedwarn (loc, 0, "%<return%> with no value, in "
9390 "function returning non-void");
9391 else
9392 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9393 "in function returning non-void");
9394 no_warning = true;
9397 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9399 current_function_returns_null = 1;
9400 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9401 pedwarn (loc, 0,
9402 "%<return%> with a value, in function returning void");
9403 else
9404 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9405 "%<return%> with expression, in function returning void");
9407 else
9409 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9410 retval, origtype, ic_return,
9411 npc, NULL_TREE, NULL_TREE, 0);
9412 tree res = DECL_RESULT (current_function_decl);
9413 tree inner;
9414 bool save;
9416 current_function_returns_value = 1;
9417 if (t == error_mark_node)
9418 return NULL_TREE;
9420 save = in_late_binary_op;
9421 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9422 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9423 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9424 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9425 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9426 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9427 in_late_binary_op = true;
9428 inner = t = convert (TREE_TYPE (res), t);
9429 in_late_binary_op = save;
9431 /* Strip any conversions, additions, and subtractions, and see if
9432 we are returning the address of a local variable. Warn if so. */
9433 while (1)
9435 switch (TREE_CODE (inner))
9437 CASE_CONVERT:
9438 case NON_LVALUE_EXPR:
9439 case PLUS_EXPR:
9440 case POINTER_PLUS_EXPR:
9441 inner = TREE_OPERAND (inner, 0);
9442 continue;
9444 case MINUS_EXPR:
9445 /* If the second operand of the MINUS_EXPR has a pointer
9446 type (or is converted from it), this may be valid, so
9447 don't give a warning. */
9449 tree op1 = TREE_OPERAND (inner, 1);
9451 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9452 && (CONVERT_EXPR_P (op1)
9453 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9454 op1 = TREE_OPERAND (op1, 0);
9456 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9457 break;
9459 inner = TREE_OPERAND (inner, 0);
9460 continue;
9463 case ADDR_EXPR:
9464 inner = TREE_OPERAND (inner, 0);
9466 while (REFERENCE_CLASS_P (inner)
9467 && !INDIRECT_REF_P (inner))
9468 inner = TREE_OPERAND (inner, 0);
9470 if (DECL_P (inner)
9471 && !DECL_EXTERNAL (inner)
9472 && !TREE_STATIC (inner)
9473 && DECL_CONTEXT (inner) == current_function_decl)
9475 if (TREE_CODE (inner) == LABEL_DECL)
9476 warning_at (loc, OPT_Wreturn_local_addr,
9477 "function returns address of label");
9478 else
9480 warning_at (loc, OPT_Wreturn_local_addr,
9481 "function returns address of local variable");
9482 tree zero = build_zero_cst (TREE_TYPE (res));
9483 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9486 break;
9488 default:
9489 break;
9492 break;
9495 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9496 SET_EXPR_LOCATION (retval, loc);
9498 if (warn_sequence_point)
9499 verify_sequence_points (retval);
9502 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9503 TREE_NO_WARNING (ret_stmt) |= no_warning;
9504 return add_stmt (ret_stmt);
9507 struct c_switch {
9508 /* The SWITCH_EXPR being built. */
9509 tree switch_expr;
9511 /* The original type of the testing expression, i.e. before the
9512 default conversion is applied. */
9513 tree orig_type;
9515 /* A splay-tree mapping the low element of a case range to the high
9516 element, or NULL_TREE if there is no high element. Used to
9517 determine whether or not a new case label duplicates an old case
9518 label. We need a tree, rather than simply a hash table, because
9519 of the GNU case range extension. */
9520 splay_tree cases;
9522 /* The bindings at the point of the switch. This is used for
9523 warnings crossing decls when branching to a case label. */
9524 struct c_spot_bindings *bindings;
9526 /* The next node on the stack. */
9527 struct c_switch *next;
9529 /* Remember whether the controlling expression had boolean type
9530 before integer promotions for the sake of -Wswitch-bool. */
9531 bool bool_cond_p;
9533 /* Remember whether there was a case value that is outside the
9534 range of the ORIG_TYPE. */
9535 bool outside_range_p;
9538 /* A stack of the currently active switch statements. The innermost
9539 switch statement is on the top of the stack. There is no need to
9540 mark the stack for garbage collection because it is only active
9541 during the processing of the body of a function, and we never
9542 collect at that point. */
9544 struct c_switch *c_switch_stack;
9546 /* Start a C switch statement, testing expression EXP. Return the new
9547 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9548 SWITCH_COND_LOC is the location of the switch's condition.
9549 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
9551 tree
9552 c_start_case (location_t switch_loc,
9553 location_t switch_cond_loc,
9554 tree exp, bool explicit_cast_p)
9556 tree orig_type = error_mark_node;
9557 bool bool_cond_p = false;
9558 struct c_switch *cs;
9560 if (exp != error_mark_node)
9562 orig_type = TREE_TYPE (exp);
9564 if (!INTEGRAL_TYPE_P (orig_type))
9566 if (orig_type != error_mark_node)
9568 error_at (switch_cond_loc, "switch quantity not an integer");
9569 orig_type = error_mark_node;
9571 exp = integer_zero_node;
9573 else
9575 tree type = TYPE_MAIN_VARIANT (orig_type);
9576 tree e = exp;
9578 /* Warn if the condition has boolean value. */
9579 while (TREE_CODE (e) == COMPOUND_EXPR)
9580 e = TREE_OPERAND (e, 1);
9582 if ((TREE_CODE (type) == BOOLEAN_TYPE
9583 || truth_value_p (TREE_CODE (e)))
9584 /* Explicit cast to int suppresses this warning. */
9585 && !(TREE_CODE (type) == INTEGER_TYPE
9586 && explicit_cast_p))
9587 bool_cond_p = true;
9589 if (!in_system_header_at (input_location)
9590 && (type == long_integer_type_node
9591 || type == long_unsigned_type_node))
9592 warning_at (switch_cond_loc,
9593 OPT_Wtraditional, "%<long%> switch expression not "
9594 "converted to %<int%> in ISO C");
9596 exp = c_fully_fold (exp, false, NULL);
9597 exp = default_conversion (exp);
9599 if (warn_sequence_point)
9600 verify_sequence_points (exp);
9604 /* Add this new SWITCH_EXPR to the stack. */
9605 cs = XNEW (struct c_switch);
9606 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9607 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9608 cs->orig_type = orig_type;
9609 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9610 cs->bindings = c_get_switch_bindings ();
9611 cs->bool_cond_p = bool_cond_p;
9612 cs->outside_range_p = false;
9613 cs->next = c_switch_stack;
9614 c_switch_stack = cs;
9616 return add_stmt (cs->switch_expr);
9619 /* Process a case label at location LOC. */
9621 tree
9622 do_case (location_t loc, tree low_value, tree high_value)
9624 tree label = NULL_TREE;
9626 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9628 low_value = c_fully_fold (low_value, false, NULL);
9629 if (TREE_CODE (low_value) == INTEGER_CST)
9630 pedwarn (loc, OPT_Wpedantic,
9631 "case label is not an integer constant expression");
9634 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9636 high_value = c_fully_fold (high_value, false, NULL);
9637 if (TREE_CODE (high_value) == INTEGER_CST)
9638 pedwarn (input_location, OPT_Wpedantic,
9639 "case label is not an integer constant expression");
9642 if (c_switch_stack == NULL)
9644 if (low_value)
9645 error_at (loc, "case label not within a switch statement");
9646 else
9647 error_at (loc, "%<default%> label not within a switch statement");
9648 return NULL_TREE;
9651 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9652 EXPR_LOCATION (c_switch_stack->switch_expr),
9653 loc))
9654 return NULL_TREE;
9656 label = c_add_case_label (loc, c_switch_stack->cases,
9657 SWITCH_COND (c_switch_stack->switch_expr),
9658 c_switch_stack->orig_type,
9659 low_value, high_value,
9660 &c_switch_stack->outside_range_p);
9661 if (label == error_mark_node)
9662 label = NULL_TREE;
9663 return label;
9666 /* Finish the switch statement. TYPE is the original type of the
9667 controlling expression of the switch, or NULL_TREE. */
9669 void
9670 c_finish_case (tree body, tree type)
9672 struct c_switch *cs = c_switch_stack;
9673 location_t switch_location;
9675 SWITCH_BODY (cs->switch_expr) = body;
9677 /* Emit warnings as needed. */
9678 switch_location = EXPR_LOCATION (cs->switch_expr);
9679 c_do_switch_warnings (cs->cases, switch_location,
9680 type ? type : TREE_TYPE (cs->switch_expr),
9681 SWITCH_COND (cs->switch_expr),
9682 cs->bool_cond_p, cs->outside_range_p);
9684 /* Pop the stack. */
9685 c_switch_stack = cs->next;
9686 splay_tree_delete (cs->cases);
9687 c_release_switch_bindings (cs->bindings);
9688 XDELETE (cs);
9691 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9692 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9693 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9694 statement, and was not surrounded with parenthesis. */
9696 void
9697 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9698 tree else_block, bool nested_if)
9700 tree stmt;
9702 /* If the condition has array notations, then the rank of the then_block and
9703 else_block must be either 0 or be equal to the rank of the condition. If
9704 the condition does not have array notations then break them up as it is
9705 broken up in a normal expression. */
9706 if (flag_cilkplus && contains_array_notation_expr (cond))
9708 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9709 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9710 return;
9711 if (then_block
9712 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9713 return;
9714 if (else_block
9715 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9716 return;
9717 if (cond_rank != then_rank && then_rank != 0)
9719 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9720 " and the then-block");
9721 return;
9723 else if (cond_rank != else_rank && else_rank != 0)
9725 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9726 " and the else-block");
9727 return;
9730 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9731 if (warn_parentheses && nested_if && else_block == NULL)
9733 tree inner_if = then_block;
9735 /* We know from the grammar productions that there is an IF nested
9736 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9737 it might not be exactly THEN_BLOCK, but should be the last
9738 non-container statement within. */
9739 while (1)
9740 switch (TREE_CODE (inner_if))
9742 case COND_EXPR:
9743 goto found;
9744 case BIND_EXPR:
9745 inner_if = BIND_EXPR_BODY (inner_if);
9746 break;
9747 case STATEMENT_LIST:
9748 inner_if = expr_last (then_block);
9749 break;
9750 case TRY_FINALLY_EXPR:
9751 case TRY_CATCH_EXPR:
9752 inner_if = TREE_OPERAND (inner_if, 0);
9753 break;
9754 default:
9755 gcc_unreachable ();
9757 found:
9759 if (COND_EXPR_ELSE (inner_if))
9760 warning_at (if_locus, OPT_Wparentheses,
9761 "suggest explicit braces to avoid ambiguous %<else%>");
9764 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9765 SET_EXPR_LOCATION (stmt, if_locus);
9766 add_stmt (stmt);
9769 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9770 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9771 is false for DO loops. INCR is the FOR increment expression. BODY is
9772 the statement controlled by the loop. BLAB is the break label. CLAB is
9773 the continue label. Everything is allowed to be NULL. */
9775 void
9776 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9777 tree blab, tree clab, bool cond_is_first)
9779 tree entry = NULL, exit = NULL, t;
9781 /* In theory could forbid cilk spawn for loop increment expression,
9782 but it should work just fine. */
9784 /* If the condition is zero don't generate a loop construct. */
9785 if (cond && integer_zerop (cond))
9787 if (cond_is_first)
9789 t = build_and_jump (&blab);
9790 SET_EXPR_LOCATION (t, start_locus);
9791 add_stmt (t);
9794 else
9796 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9798 /* If we have an exit condition, then we build an IF with gotos either
9799 out of the loop, or to the top of it. If there's no exit condition,
9800 then we just build a jump back to the top. */
9801 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9803 if (cond && !integer_nonzerop (cond))
9805 /* Canonicalize the loop condition to the end. This means
9806 generating a branch to the loop condition. Reuse the
9807 continue label, if possible. */
9808 if (cond_is_first)
9810 if (incr || !clab)
9812 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9813 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9815 else
9816 t = build1 (GOTO_EXPR, void_type_node, clab);
9817 SET_EXPR_LOCATION (t, start_locus);
9818 add_stmt (t);
9821 t = build_and_jump (&blab);
9822 if (cond_is_first)
9823 exit = fold_build3_loc (start_locus,
9824 COND_EXPR, void_type_node, cond, exit, t);
9825 else
9826 exit = fold_build3_loc (input_location,
9827 COND_EXPR, void_type_node, cond, exit, t);
9830 add_stmt (top);
9833 if (body)
9834 add_stmt (body);
9835 if (clab)
9836 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9837 if (incr)
9838 add_stmt (incr);
9839 if (entry)
9840 add_stmt (entry);
9841 if (exit)
9842 add_stmt (exit);
9843 if (blab)
9844 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9847 tree
9848 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9850 bool skip;
9851 tree label = *label_p;
9853 /* In switch statements break is sometimes stylistically used after
9854 a return statement. This can lead to spurious warnings about
9855 control reaching the end of a non-void function when it is
9856 inlined. Note that we are calling block_may_fallthru with
9857 language specific tree nodes; this works because
9858 block_may_fallthru returns true when given something it does not
9859 understand. */
9860 skip = !block_may_fallthru (cur_stmt_list);
9862 if (!label)
9864 if (!skip)
9865 *label_p = label = create_artificial_label (loc);
9867 else if (TREE_CODE (label) == LABEL_DECL)
9869 else switch (TREE_INT_CST_LOW (label))
9871 case 0:
9872 if (is_break)
9873 error_at (loc, "break statement not within loop or switch");
9874 else
9875 error_at (loc, "continue statement not within a loop");
9876 return NULL_TREE;
9878 case 1:
9879 gcc_assert (is_break);
9880 error_at (loc, "break statement used with OpenMP for loop");
9881 return NULL_TREE;
9883 case 2:
9884 if (is_break)
9885 error ("break statement within %<#pragma simd%> loop body");
9886 else
9887 error ("continue statement within %<#pragma simd%> loop body");
9888 return NULL_TREE;
9890 default:
9891 gcc_unreachable ();
9894 if (skip)
9895 return NULL_TREE;
9897 if (!is_break)
9898 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9900 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9903 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9905 static void
9906 emit_side_effect_warnings (location_t loc, tree expr)
9908 if (expr == error_mark_node)
9910 else if (!TREE_SIDE_EFFECTS (expr))
9912 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9913 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9915 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9917 tree r = expr;
9918 location_t cloc = loc;
9919 while (TREE_CODE (r) == COMPOUND_EXPR)
9921 if (EXPR_HAS_LOCATION (r))
9922 cloc = EXPR_LOCATION (r);
9923 r = TREE_OPERAND (r, 1);
9925 if (!TREE_SIDE_EFFECTS (r)
9926 && !VOID_TYPE_P (TREE_TYPE (r))
9927 && !CONVERT_EXPR_P (r)
9928 && !TREE_NO_WARNING (r)
9929 && !TREE_NO_WARNING (expr))
9930 warning_at (cloc, OPT_Wunused_value,
9931 "right-hand operand of comma expression has no effect");
9933 else
9934 warn_if_unused_value (expr, loc);
9937 /* Process an expression as if it were a complete statement. Emit
9938 diagnostics, but do not call ADD_STMT. LOC is the location of the
9939 statement. */
9941 tree
9942 c_process_expr_stmt (location_t loc, tree expr)
9944 tree exprv;
9946 if (!expr)
9947 return NULL_TREE;
9949 expr = c_fully_fold (expr, false, NULL);
9951 if (warn_sequence_point)
9952 verify_sequence_points (expr);
9954 if (TREE_TYPE (expr) != error_mark_node
9955 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9956 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9957 error_at (loc, "expression statement has incomplete type");
9959 /* If we're not processing a statement expression, warn about unused values.
9960 Warnings for statement expressions will be emitted later, once we figure
9961 out which is the result. */
9962 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9963 && warn_unused_value)
9964 emit_side_effect_warnings (loc, expr);
9966 exprv = expr;
9967 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9968 exprv = TREE_OPERAND (exprv, 1);
9969 while (CONVERT_EXPR_P (exprv))
9970 exprv = TREE_OPERAND (exprv, 0);
9971 if (DECL_P (exprv)
9972 || handled_component_p (exprv)
9973 || TREE_CODE (exprv) == ADDR_EXPR)
9974 mark_exp_read (exprv);
9976 /* If the expression is not of a type to which we cannot assign a line
9977 number, wrap the thing in a no-op NOP_EXPR. */
9978 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9980 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9981 SET_EXPR_LOCATION (expr, loc);
9984 return expr;
9987 /* Emit an expression as a statement. LOC is the location of the
9988 expression. */
9990 tree
9991 c_finish_expr_stmt (location_t loc, tree expr)
9993 if (expr)
9994 return add_stmt (c_process_expr_stmt (loc, expr));
9995 else
9996 return NULL;
9999 /* Do the opposite and emit a statement as an expression. To begin,
10000 create a new binding level and return it. */
10002 tree
10003 c_begin_stmt_expr (void)
10005 tree ret;
10007 /* We must force a BLOCK for this level so that, if it is not expanded
10008 later, there is a way to turn off the entire subtree of blocks that
10009 are contained in it. */
10010 keep_next_level ();
10011 ret = c_begin_compound_stmt (true);
10013 c_bindings_start_stmt_expr (c_switch_stack == NULL
10014 ? NULL
10015 : c_switch_stack->bindings);
10017 /* Mark the current statement list as belonging to a statement list. */
10018 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10020 return ret;
10023 /* LOC is the location of the compound statement to which this body
10024 belongs. */
10026 tree
10027 c_finish_stmt_expr (location_t loc, tree body)
10029 tree last, type, tmp, val;
10030 tree *last_p;
10032 body = c_end_compound_stmt (loc, body, true);
10034 c_bindings_end_stmt_expr (c_switch_stack == NULL
10035 ? NULL
10036 : c_switch_stack->bindings);
10038 /* Locate the last statement in BODY. See c_end_compound_stmt
10039 about always returning a BIND_EXPR. */
10040 last_p = &BIND_EXPR_BODY (body);
10041 last = BIND_EXPR_BODY (body);
10043 continue_searching:
10044 if (TREE_CODE (last) == STATEMENT_LIST)
10046 tree_stmt_iterator i;
10048 /* This can happen with degenerate cases like ({ }). No value. */
10049 if (!TREE_SIDE_EFFECTS (last))
10050 return body;
10052 /* If we're supposed to generate side effects warnings, process
10053 all of the statements except the last. */
10054 if (warn_unused_value)
10056 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10058 location_t tloc;
10059 tree t = tsi_stmt (i);
10061 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10062 emit_side_effect_warnings (tloc, t);
10065 else
10066 i = tsi_last (last);
10067 last_p = tsi_stmt_ptr (i);
10068 last = *last_p;
10071 /* If the end of the list is exception related, then the list was split
10072 by a call to push_cleanup. Continue searching. */
10073 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10074 || TREE_CODE (last) == TRY_CATCH_EXPR)
10076 last_p = &TREE_OPERAND (last, 0);
10077 last = *last_p;
10078 goto continue_searching;
10081 if (last == error_mark_node)
10082 return last;
10084 /* In the case that the BIND_EXPR is not necessary, return the
10085 expression out from inside it. */
10086 if (last == BIND_EXPR_BODY (body)
10087 && BIND_EXPR_VARS (body) == NULL)
10089 /* Even if this looks constant, do not allow it in a constant
10090 expression. */
10091 last = c_wrap_maybe_const (last, true);
10092 /* Do not warn if the return value of a statement expression is
10093 unused. */
10094 TREE_NO_WARNING (last) = 1;
10095 return last;
10098 /* Extract the type of said expression. */
10099 type = TREE_TYPE (last);
10101 /* If we're not returning a value at all, then the BIND_EXPR that
10102 we already have is a fine expression to return. */
10103 if (!type || VOID_TYPE_P (type))
10104 return body;
10106 /* Now that we've located the expression containing the value, it seems
10107 silly to make voidify_wrapper_expr repeat the process. Create a
10108 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10109 tmp = create_tmp_var_raw (type);
10111 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10112 tree_expr_nonnegative_p giving up immediately. */
10113 val = last;
10114 if (TREE_CODE (val) == NOP_EXPR
10115 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10116 val = TREE_OPERAND (val, 0);
10118 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10119 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10122 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10123 SET_EXPR_LOCATION (t, loc);
10124 return t;
10128 /* Begin and end compound statements. This is as simple as pushing
10129 and popping new statement lists from the tree. */
10131 tree
10132 c_begin_compound_stmt (bool do_scope)
10134 tree stmt = push_stmt_list ();
10135 if (do_scope)
10136 push_scope ();
10137 return stmt;
10140 /* End a compound statement. STMT is the statement. LOC is the
10141 location of the compound statement-- this is usually the location
10142 of the opening brace. */
10144 tree
10145 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10147 tree block = NULL;
10149 if (do_scope)
10151 if (c_dialect_objc ())
10152 objc_clear_super_receiver ();
10153 block = pop_scope ();
10156 stmt = pop_stmt_list (stmt);
10157 stmt = c_build_bind_expr (loc, block, stmt);
10159 /* If this compound statement is nested immediately inside a statement
10160 expression, then force a BIND_EXPR to be created. Otherwise we'll
10161 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10162 STATEMENT_LISTs merge, and thus we can lose track of what statement
10163 was really last. */
10164 if (building_stmt_list_p ()
10165 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10166 && TREE_CODE (stmt) != BIND_EXPR)
10168 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10169 TREE_SIDE_EFFECTS (stmt) = 1;
10170 SET_EXPR_LOCATION (stmt, loc);
10173 return stmt;
10176 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10177 when the current scope is exited. EH_ONLY is true when this is not
10178 meant to apply to normal control flow transfer. */
10180 void
10181 push_cleanup (tree decl, tree cleanup, bool eh_only)
10183 enum tree_code code;
10184 tree stmt, list;
10185 bool stmt_expr;
10187 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10188 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10189 add_stmt (stmt);
10190 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10191 list = push_stmt_list ();
10192 TREE_OPERAND (stmt, 0) = list;
10193 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10196 /* Build a binary-operation expression without default conversions.
10197 CODE is the kind of expression to build.
10198 LOCATION is the operator's location.
10199 This function differs from `build' in several ways:
10200 the data type of the result is computed and recorded in it,
10201 warnings are generated if arg data types are invalid,
10202 special handling for addition and subtraction of pointers is known,
10203 and some optimization is done (operations on narrow ints
10204 are done in the narrower type when that gives the same result).
10205 Constant folding is also done before the result is returned.
10207 Note that the operands will never have enumeral types, or function
10208 or array types, because either they will have the default conversions
10209 performed or they have both just been converted to some other type in which
10210 the arithmetic is to be done. */
10212 tree
10213 build_binary_op (location_t location, enum tree_code code,
10214 tree orig_op0, tree orig_op1, int convert_p)
10216 tree type0, type1, orig_type0, orig_type1;
10217 tree eptype;
10218 enum tree_code code0, code1;
10219 tree op0, op1;
10220 tree ret = error_mark_node;
10221 const char *invalid_op_diag;
10222 bool op0_int_operands, op1_int_operands;
10223 bool int_const, int_const_or_overflow, int_operands;
10225 /* Expression code to give to the expression when it is built.
10226 Normally this is CODE, which is what the caller asked for,
10227 but in some special cases we change it. */
10228 enum tree_code resultcode = code;
10230 /* Data type in which the computation is to be performed.
10231 In the simplest cases this is the common type of the arguments. */
10232 tree result_type = NULL;
10234 /* When the computation is in excess precision, the type of the
10235 final EXCESS_PRECISION_EXPR. */
10236 tree semantic_result_type = NULL;
10238 /* Nonzero means operands have already been type-converted
10239 in whatever way is necessary.
10240 Zero means they need to be converted to RESULT_TYPE. */
10241 int converted = 0;
10243 /* Nonzero means create the expression with this type, rather than
10244 RESULT_TYPE. */
10245 tree build_type = 0;
10247 /* Nonzero means after finally constructing the expression
10248 convert it to this type. */
10249 tree final_type = 0;
10251 /* Nonzero if this is an operation like MIN or MAX which can
10252 safely be computed in short if both args are promoted shorts.
10253 Also implies COMMON.
10254 -1 indicates a bitwise operation; this makes a difference
10255 in the exact conditions for when it is safe to do the operation
10256 in a narrower mode. */
10257 int shorten = 0;
10259 /* Nonzero if this is a comparison operation;
10260 if both args are promoted shorts, compare the original shorts.
10261 Also implies COMMON. */
10262 int short_compare = 0;
10264 /* Nonzero if this is a right-shift operation, which can be computed on the
10265 original short and then promoted if the operand is a promoted short. */
10266 int short_shift = 0;
10268 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10269 int common = 0;
10271 /* True means types are compatible as far as ObjC is concerned. */
10272 bool objc_ok;
10274 /* True means this is an arithmetic operation that may need excess
10275 precision. */
10276 bool may_need_excess_precision;
10278 /* True means this is a boolean operation that converts both its
10279 operands to truth-values. */
10280 bool boolean_op = false;
10282 /* Remember whether we're doing / or %. */
10283 bool doing_div_or_mod = false;
10285 /* Remember whether we're doing << or >>. */
10286 bool doing_shift = false;
10288 /* Tree holding instrumentation expression. */
10289 tree instrument_expr = NULL;
10291 if (location == UNKNOWN_LOCATION)
10292 location = input_location;
10294 op0 = orig_op0;
10295 op1 = orig_op1;
10297 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10298 if (op0_int_operands)
10299 op0 = remove_c_maybe_const_expr (op0);
10300 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10301 if (op1_int_operands)
10302 op1 = remove_c_maybe_const_expr (op1);
10303 int_operands = (op0_int_operands && op1_int_operands);
10304 if (int_operands)
10306 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10307 && TREE_CODE (orig_op1) == INTEGER_CST);
10308 int_const = (int_const_or_overflow
10309 && !TREE_OVERFLOW (orig_op0)
10310 && !TREE_OVERFLOW (orig_op1));
10312 else
10313 int_const = int_const_or_overflow = false;
10315 /* Do not apply default conversion in mixed vector/scalar expression. */
10316 if (convert_p
10317 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
10319 op0 = default_conversion (op0);
10320 op1 = default_conversion (op1);
10323 /* When Cilk Plus is enabled and there are array notations inside op0, then
10324 we check to see if there are builtin array notation functions. If
10325 so, then we take on the type of the array notation inside it. */
10326 if (flag_cilkplus && contains_array_notation_expr (op0))
10327 orig_type0 = type0 = find_correct_array_notation_type (op0);
10328 else
10329 orig_type0 = type0 = TREE_TYPE (op0);
10331 if (flag_cilkplus && contains_array_notation_expr (op1))
10332 orig_type1 = type1 = find_correct_array_notation_type (op1);
10333 else
10334 orig_type1 = type1 = TREE_TYPE (op1);
10336 /* The expression codes of the data types of the arguments tell us
10337 whether the arguments are integers, floating, pointers, etc. */
10338 code0 = TREE_CODE (type0);
10339 code1 = TREE_CODE (type1);
10341 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10342 STRIP_TYPE_NOPS (op0);
10343 STRIP_TYPE_NOPS (op1);
10345 /* If an error was already reported for one of the arguments,
10346 avoid reporting another error. */
10348 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10349 return error_mark_node;
10351 if ((invalid_op_diag
10352 = targetm.invalid_binary_op (code, type0, type1)))
10354 error_at (location, invalid_op_diag);
10355 return error_mark_node;
10358 switch (code)
10360 case PLUS_EXPR:
10361 case MINUS_EXPR:
10362 case MULT_EXPR:
10363 case TRUNC_DIV_EXPR:
10364 case CEIL_DIV_EXPR:
10365 case FLOOR_DIV_EXPR:
10366 case ROUND_DIV_EXPR:
10367 case EXACT_DIV_EXPR:
10368 may_need_excess_precision = true;
10369 break;
10370 default:
10371 may_need_excess_precision = false;
10372 break;
10374 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10376 op0 = TREE_OPERAND (op0, 0);
10377 type0 = TREE_TYPE (op0);
10379 else if (may_need_excess_precision
10380 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10382 type0 = eptype;
10383 op0 = convert (eptype, op0);
10385 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10387 op1 = TREE_OPERAND (op1, 0);
10388 type1 = TREE_TYPE (op1);
10390 else if (may_need_excess_precision
10391 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10393 type1 = eptype;
10394 op1 = convert (eptype, op1);
10397 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10399 /* In case when one of the operands of the binary operation is
10400 a vector and another is a scalar -- convert scalar to vector. */
10401 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10403 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10404 true);
10406 switch (convert_flag)
10408 case stv_error:
10409 return error_mark_node;
10410 case stv_firstarg:
10412 bool maybe_const = true;
10413 tree sc;
10414 sc = c_fully_fold (op0, false, &maybe_const);
10415 sc = save_expr (sc);
10416 sc = convert (TREE_TYPE (type1), sc);
10417 op0 = build_vector_from_val (type1, sc);
10418 if (!maybe_const)
10419 op0 = c_wrap_maybe_const (op0, true);
10420 orig_type0 = type0 = TREE_TYPE (op0);
10421 code0 = TREE_CODE (type0);
10422 converted = 1;
10423 break;
10425 case stv_secondarg:
10427 bool maybe_const = true;
10428 tree sc;
10429 sc = c_fully_fold (op1, false, &maybe_const);
10430 sc = save_expr (sc);
10431 sc = convert (TREE_TYPE (type0), sc);
10432 op1 = build_vector_from_val (type0, sc);
10433 if (!maybe_const)
10434 op1 = c_wrap_maybe_const (op1, true);
10435 orig_type1 = type1 = TREE_TYPE (op1);
10436 code1 = TREE_CODE (type1);
10437 converted = 1;
10438 break;
10440 default:
10441 break;
10445 switch (code)
10447 case PLUS_EXPR:
10448 /* Handle the pointer + int case. */
10449 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10451 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10452 goto return_build_binary_op;
10454 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10456 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10457 goto return_build_binary_op;
10459 else
10460 common = 1;
10461 break;
10463 case MINUS_EXPR:
10464 /* Subtraction of two similar pointers.
10465 We must subtract them as integers, then divide by object size. */
10466 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10467 && comp_target_types (location, type0, type1))
10469 ret = pointer_diff (location, op0, op1);
10470 goto return_build_binary_op;
10472 /* Handle pointer minus int. Just like pointer plus int. */
10473 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10475 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10476 goto return_build_binary_op;
10478 else
10479 common = 1;
10480 break;
10482 case MULT_EXPR:
10483 common = 1;
10484 break;
10486 case TRUNC_DIV_EXPR:
10487 case CEIL_DIV_EXPR:
10488 case FLOOR_DIV_EXPR:
10489 case ROUND_DIV_EXPR:
10490 case EXACT_DIV_EXPR:
10491 doing_div_or_mod = true;
10492 warn_for_div_by_zero (location, op1);
10494 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10495 || code0 == FIXED_POINT_TYPE
10496 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10497 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10498 || code1 == FIXED_POINT_TYPE
10499 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10501 enum tree_code tcode0 = code0, tcode1 = code1;
10503 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10504 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10505 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10506 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10508 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10509 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10510 resultcode = RDIV_EXPR;
10511 else
10512 /* Although it would be tempting to shorten always here, that
10513 loses on some targets, since the modulo instruction is
10514 undefined if the quotient can't be represented in the
10515 computation mode. We shorten only if unsigned or if
10516 dividing by something we know != -1. */
10517 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10518 || (TREE_CODE (op1) == INTEGER_CST
10519 && !integer_all_onesp (op1)));
10520 common = 1;
10522 break;
10524 case BIT_AND_EXPR:
10525 case BIT_IOR_EXPR:
10526 case BIT_XOR_EXPR:
10527 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10528 shorten = -1;
10529 /* Allow vector types which are not floating point types. */
10530 else if (code0 == VECTOR_TYPE
10531 && code1 == VECTOR_TYPE
10532 && !VECTOR_FLOAT_TYPE_P (type0)
10533 && !VECTOR_FLOAT_TYPE_P (type1))
10534 common = 1;
10535 break;
10537 case TRUNC_MOD_EXPR:
10538 case FLOOR_MOD_EXPR:
10539 doing_div_or_mod = true;
10540 warn_for_div_by_zero (location, op1);
10542 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10543 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10544 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10545 common = 1;
10546 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10548 /* Although it would be tempting to shorten always here, that loses
10549 on some targets, since the modulo instruction is undefined if the
10550 quotient can't be represented in the computation mode. We shorten
10551 only if unsigned or if dividing by something we know != -1. */
10552 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10553 || (TREE_CODE (op1) == INTEGER_CST
10554 && !integer_all_onesp (op1)));
10555 common = 1;
10557 break;
10559 case TRUTH_ANDIF_EXPR:
10560 case TRUTH_ORIF_EXPR:
10561 case TRUTH_AND_EXPR:
10562 case TRUTH_OR_EXPR:
10563 case TRUTH_XOR_EXPR:
10564 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10565 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10566 || code0 == FIXED_POINT_TYPE)
10567 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10568 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10569 || code1 == FIXED_POINT_TYPE))
10571 /* Result of these operations is always an int,
10572 but that does not mean the operands should be
10573 converted to ints! */
10574 result_type = integer_type_node;
10575 if (op0_int_operands)
10577 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10578 op0 = remove_c_maybe_const_expr (op0);
10580 else
10581 op0 = c_objc_common_truthvalue_conversion (location, op0);
10582 if (op1_int_operands)
10584 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10585 op1 = remove_c_maybe_const_expr (op1);
10587 else
10588 op1 = c_objc_common_truthvalue_conversion (location, op1);
10589 converted = 1;
10590 boolean_op = true;
10592 if (code == TRUTH_ANDIF_EXPR)
10594 int_const_or_overflow = (int_operands
10595 && TREE_CODE (orig_op0) == INTEGER_CST
10596 && (op0 == truthvalue_false_node
10597 || TREE_CODE (orig_op1) == INTEGER_CST));
10598 int_const = (int_const_or_overflow
10599 && !TREE_OVERFLOW (orig_op0)
10600 && (op0 == truthvalue_false_node
10601 || !TREE_OVERFLOW (orig_op1)));
10603 else if (code == TRUTH_ORIF_EXPR)
10605 int_const_or_overflow = (int_operands
10606 && TREE_CODE (orig_op0) == INTEGER_CST
10607 && (op0 == truthvalue_true_node
10608 || TREE_CODE (orig_op1) == INTEGER_CST));
10609 int_const = (int_const_or_overflow
10610 && !TREE_OVERFLOW (orig_op0)
10611 && (op0 == truthvalue_true_node
10612 || !TREE_OVERFLOW (orig_op1)));
10614 break;
10616 /* Shift operations: result has same type as first operand;
10617 always convert second operand to int.
10618 Also set SHORT_SHIFT if shifting rightward. */
10620 case RSHIFT_EXPR:
10621 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10622 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10624 result_type = type0;
10625 converted = 1;
10627 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10628 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10629 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10630 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10632 result_type = type0;
10633 converted = 1;
10635 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10636 && code1 == INTEGER_TYPE)
10638 doing_shift = true;
10639 if (TREE_CODE (op1) == INTEGER_CST)
10641 if (tree_int_cst_sgn (op1) < 0)
10643 int_const = false;
10644 if (c_inhibit_evaluation_warnings == 0)
10645 warning_at (location, OPT_Wshift_count_negative,
10646 "right shift count is negative");
10648 else
10650 if (!integer_zerop (op1))
10651 short_shift = 1;
10653 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10655 int_const = false;
10656 if (c_inhibit_evaluation_warnings == 0)
10657 warning_at (location, OPT_Wshift_count_overflow,
10658 "right shift count >= width of type");
10663 /* Use the type of the value to be shifted. */
10664 result_type = type0;
10665 /* Avoid converting op1 to result_type later. */
10666 converted = 1;
10668 break;
10670 case LSHIFT_EXPR:
10671 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10672 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10674 result_type = type0;
10675 converted = 1;
10677 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10678 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10679 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10680 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10682 result_type = type0;
10683 converted = 1;
10685 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10686 && code1 == INTEGER_TYPE)
10688 doing_shift = true;
10689 if (TREE_CODE (op0) == INTEGER_CST
10690 && tree_int_cst_sgn (op0) < 0)
10692 /* Don't reject a left shift of a negative value in a context
10693 where a constant expression is needed in C90. */
10694 if (flag_isoc99)
10695 int_const = false;
10696 if (c_inhibit_evaluation_warnings == 0)
10697 warning_at (location, OPT_Wshift_negative_value,
10698 "left shift of negative value");
10700 if (TREE_CODE (op1) == INTEGER_CST)
10702 if (tree_int_cst_sgn (op1) < 0)
10704 int_const = false;
10705 if (c_inhibit_evaluation_warnings == 0)
10706 warning_at (location, OPT_Wshift_count_negative,
10707 "left shift count is negative");
10710 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10712 int_const = false;
10713 if (c_inhibit_evaluation_warnings == 0)
10714 warning_at (location, OPT_Wshift_count_overflow,
10715 "left shift count >= width of type");
10719 /* Use the type of the value to be shifted. */
10720 result_type = type0;
10721 /* Avoid converting op1 to result_type later. */
10722 converted = 1;
10724 break;
10726 case EQ_EXPR:
10727 case NE_EXPR:
10728 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10730 tree intt;
10731 if (!vector_types_compatible_elements_p (type0, type1))
10733 error_at (location, "comparing vectors with different "
10734 "element types");
10735 return error_mark_node;
10738 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10740 error_at (location, "comparing vectors with different "
10741 "number of elements");
10742 return error_mark_node;
10745 /* Always construct signed integer vector type. */
10746 intt = c_common_type_for_size (GET_MODE_BITSIZE
10747 (TYPE_MODE (TREE_TYPE (type0))), 0);
10748 result_type = build_opaque_vector_type (intt,
10749 TYPE_VECTOR_SUBPARTS (type0));
10750 converted = 1;
10751 break;
10753 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10754 warning_at (location,
10755 OPT_Wfloat_equal,
10756 "comparing floating point with == or != is unsafe");
10757 /* Result of comparison is always int,
10758 but don't convert the args to int! */
10759 build_type = integer_type_node;
10760 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10761 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10762 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10763 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10764 short_compare = 1;
10765 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10767 if (TREE_CODE (op0) == ADDR_EXPR
10768 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10770 if (code == EQ_EXPR)
10771 warning_at (location,
10772 OPT_Waddress,
10773 "the comparison will always evaluate as %<false%> "
10774 "for the address of %qD will never be NULL",
10775 TREE_OPERAND (op0, 0));
10776 else
10777 warning_at (location,
10778 OPT_Waddress,
10779 "the comparison will always evaluate as %<true%> "
10780 "for the address of %qD will never be NULL",
10781 TREE_OPERAND (op0, 0));
10783 result_type = type0;
10785 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10787 if (TREE_CODE (op1) == ADDR_EXPR
10788 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10790 if (code == EQ_EXPR)
10791 warning_at (location,
10792 OPT_Waddress,
10793 "the comparison will always evaluate as %<false%> "
10794 "for the address of %qD will never be NULL",
10795 TREE_OPERAND (op1, 0));
10796 else
10797 warning_at (location,
10798 OPT_Waddress,
10799 "the comparison will always evaluate as %<true%> "
10800 "for the address of %qD will never be NULL",
10801 TREE_OPERAND (op1, 0));
10803 result_type = type1;
10805 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10807 tree tt0 = TREE_TYPE (type0);
10808 tree tt1 = TREE_TYPE (type1);
10809 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10810 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10811 addr_space_t as_common = ADDR_SPACE_GENERIC;
10813 /* Anything compares with void *. void * compares with anything.
10814 Otherwise, the targets must be compatible
10815 and both must be object or both incomplete. */
10816 if (comp_target_types (location, type0, type1))
10817 result_type = common_pointer_type (type0, type1);
10818 else if (!addr_space_superset (as0, as1, &as_common))
10820 error_at (location, "comparison of pointers to "
10821 "disjoint address spaces");
10822 return error_mark_node;
10824 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10826 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10827 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10828 "comparison of %<void *%> with function pointer");
10830 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10832 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10833 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10834 "comparison of %<void *%> with function pointer");
10836 else
10837 /* Avoid warning about the volatile ObjC EH puts on decls. */
10838 if (!objc_ok)
10839 pedwarn (location, 0,
10840 "comparison of distinct pointer types lacks a cast");
10842 if (result_type == NULL_TREE)
10844 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10845 result_type = build_pointer_type
10846 (build_qualified_type (void_type_node, qual));
10849 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10851 result_type = type0;
10852 pedwarn (location, 0, "comparison between pointer and integer");
10854 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10856 result_type = type1;
10857 pedwarn (location, 0, "comparison between pointer and integer");
10859 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10860 || truth_value_p (TREE_CODE (orig_op0)))
10861 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10862 || truth_value_p (TREE_CODE (orig_op1))))
10863 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10864 break;
10866 case LE_EXPR:
10867 case GE_EXPR:
10868 case LT_EXPR:
10869 case GT_EXPR:
10870 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10872 tree intt;
10873 if (!vector_types_compatible_elements_p (type0, type1))
10875 error_at (location, "comparing vectors with different "
10876 "element types");
10877 return error_mark_node;
10880 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10882 error_at (location, "comparing vectors with different "
10883 "number of elements");
10884 return error_mark_node;
10887 /* Always construct signed integer vector type. */
10888 intt = c_common_type_for_size (GET_MODE_BITSIZE
10889 (TYPE_MODE (TREE_TYPE (type0))), 0);
10890 result_type = build_opaque_vector_type (intt,
10891 TYPE_VECTOR_SUBPARTS (type0));
10892 converted = 1;
10893 break;
10895 build_type = integer_type_node;
10896 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10897 || code0 == FIXED_POINT_TYPE)
10898 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10899 || code1 == FIXED_POINT_TYPE))
10900 short_compare = 1;
10901 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10903 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10904 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10905 addr_space_t as_common;
10907 if (comp_target_types (location, type0, type1))
10909 result_type = common_pointer_type (type0, type1);
10910 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10911 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10912 pedwarn (location, 0,
10913 "comparison of complete and incomplete pointers");
10914 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10915 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10916 "ordered comparisons of pointers to functions");
10917 else if (null_pointer_constant_p (orig_op0)
10918 || null_pointer_constant_p (orig_op1))
10919 warning_at (location, OPT_Wextra,
10920 "ordered comparison of pointer with null pointer");
10923 else if (!addr_space_superset (as0, as1, &as_common))
10925 error_at (location, "comparison of pointers to "
10926 "disjoint address spaces");
10927 return error_mark_node;
10929 else
10931 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10932 result_type = build_pointer_type
10933 (build_qualified_type (void_type_node, qual));
10934 pedwarn (location, 0,
10935 "comparison of distinct pointer types lacks a cast");
10938 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10940 result_type = type0;
10941 if (pedantic)
10942 pedwarn (location, OPT_Wpedantic,
10943 "ordered comparison of pointer with integer zero");
10944 else if (extra_warnings)
10945 warning_at (location, OPT_Wextra,
10946 "ordered comparison of pointer with integer zero");
10948 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10950 result_type = type1;
10951 if (pedantic)
10952 pedwarn (location, OPT_Wpedantic,
10953 "ordered comparison of pointer with integer zero");
10954 else if (extra_warnings)
10955 warning_at (location, OPT_Wextra,
10956 "ordered comparison of pointer with integer zero");
10958 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10960 result_type = type0;
10961 pedwarn (location, 0, "comparison between pointer and integer");
10963 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10965 result_type = type1;
10966 pedwarn (location, 0, "comparison between pointer and integer");
10968 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10969 || truth_value_p (TREE_CODE (orig_op0)))
10970 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10971 || truth_value_p (TREE_CODE (orig_op1))))
10972 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10973 break;
10975 default:
10976 gcc_unreachable ();
10979 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10980 return error_mark_node;
10982 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10983 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10984 || !vector_types_compatible_elements_p (type0, type1)))
10986 binary_op_error (location, code, type0, type1);
10987 return error_mark_node;
10990 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10991 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10993 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10994 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10996 bool first_complex = (code0 == COMPLEX_TYPE);
10997 bool second_complex = (code1 == COMPLEX_TYPE);
10998 int none_complex = (!first_complex && !second_complex);
11000 if (shorten || common || short_compare)
11002 result_type = c_common_type (type0, type1);
11003 do_warn_double_promotion (result_type, type0, type1,
11004 "implicit conversion from %qT to %qT "
11005 "to match other operand of binary "
11006 "expression",
11007 location);
11008 if (result_type == error_mark_node)
11009 return error_mark_node;
11012 if (first_complex != second_complex
11013 && (code == PLUS_EXPR
11014 || code == MINUS_EXPR
11015 || code == MULT_EXPR
11016 || (code == TRUNC_DIV_EXPR && first_complex))
11017 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11018 && flag_signed_zeros)
11020 /* An operation on mixed real/complex operands must be
11021 handled specially, but the language-independent code can
11022 more easily optimize the plain complex arithmetic if
11023 -fno-signed-zeros. */
11024 tree real_type = TREE_TYPE (result_type);
11025 tree real, imag;
11026 if (type0 != orig_type0 || type1 != orig_type1)
11028 gcc_assert (may_need_excess_precision && common);
11029 semantic_result_type = c_common_type (orig_type0, orig_type1);
11031 if (first_complex)
11033 if (TREE_TYPE (op0) != result_type)
11034 op0 = convert_and_check (location, result_type, op0);
11035 if (TREE_TYPE (op1) != real_type)
11036 op1 = convert_and_check (location, real_type, op1);
11038 else
11040 if (TREE_TYPE (op0) != real_type)
11041 op0 = convert_and_check (location, real_type, op0);
11042 if (TREE_TYPE (op1) != result_type)
11043 op1 = convert_and_check (location, result_type, op1);
11045 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11046 return error_mark_node;
11047 if (first_complex)
11049 op0 = c_save_expr (op0);
11050 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11051 op0, 1);
11052 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11053 op0, 1);
11054 switch (code)
11056 case MULT_EXPR:
11057 case TRUNC_DIV_EXPR:
11058 op1 = c_save_expr (op1);
11059 imag = build2 (resultcode, real_type, imag, op1);
11060 /* Fall through. */
11061 case PLUS_EXPR:
11062 case MINUS_EXPR:
11063 real = build2 (resultcode, real_type, real, op1);
11064 break;
11065 default:
11066 gcc_unreachable();
11069 else
11071 op1 = c_save_expr (op1);
11072 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11073 op1, 1);
11074 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11075 op1, 1);
11076 switch (code)
11078 case MULT_EXPR:
11079 op0 = c_save_expr (op0);
11080 imag = build2 (resultcode, real_type, op0, imag);
11081 /* Fall through. */
11082 case PLUS_EXPR:
11083 real = build2 (resultcode, real_type, op0, real);
11084 break;
11085 case MINUS_EXPR:
11086 real = build2 (resultcode, real_type, op0, real);
11087 imag = build1 (NEGATE_EXPR, real_type, imag);
11088 break;
11089 default:
11090 gcc_unreachable();
11093 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11094 goto return_build_binary_op;
11097 /* For certain operations (which identify themselves by shorten != 0)
11098 if both args were extended from the same smaller type,
11099 do the arithmetic in that type and then extend.
11101 shorten !=0 and !=1 indicates a bitwise operation.
11102 For them, this optimization is safe only if
11103 both args are zero-extended or both are sign-extended.
11104 Otherwise, we might change the result.
11105 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11106 but calculated in (unsigned short) it would be (unsigned short)-1. */
11108 if (shorten && none_complex)
11110 final_type = result_type;
11111 result_type = shorten_binary_op (result_type, op0, op1,
11112 shorten == -1);
11115 /* Shifts can be shortened if shifting right. */
11117 if (short_shift)
11119 int unsigned_arg;
11120 tree arg0 = get_narrower (op0, &unsigned_arg);
11122 final_type = result_type;
11124 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11125 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11127 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11128 && tree_int_cst_sgn (op1) > 0
11129 /* We can shorten only if the shift count is less than the
11130 number of bits in the smaller type size. */
11131 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11132 /* We cannot drop an unsigned shift after sign-extension. */
11133 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11135 /* Do an unsigned shift if the operand was zero-extended. */
11136 result_type
11137 = c_common_signed_or_unsigned_type (unsigned_arg,
11138 TREE_TYPE (arg0));
11139 /* Convert value-to-be-shifted to that type. */
11140 if (TREE_TYPE (op0) != result_type)
11141 op0 = convert (result_type, op0);
11142 converted = 1;
11146 /* Comparison operations are shortened too but differently.
11147 They identify themselves by setting short_compare = 1. */
11149 if (short_compare)
11151 /* Don't write &op0, etc., because that would prevent op0
11152 from being kept in a register.
11153 Instead, make copies of the our local variables and
11154 pass the copies by reference, then copy them back afterward. */
11155 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11156 enum tree_code xresultcode = resultcode;
11157 tree val
11158 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11159 &xresultcode);
11161 if (val != 0)
11163 ret = val;
11164 goto return_build_binary_op;
11167 op0 = xop0, op1 = xop1;
11168 converted = 1;
11169 resultcode = xresultcode;
11171 if (c_inhibit_evaluation_warnings == 0)
11173 bool op0_maybe_const = true;
11174 bool op1_maybe_const = true;
11175 tree orig_op0_folded, orig_op1_folded;
11177 if (in_late_binary_op)
11179 orig_op0_folded = orig_op0;
11180 orig_op1_folded = orig_op1;
11182 else
11184 /* Fold for the sake of possible warnings, as in
11185 build_conditional_expr. This requires the
11186 "original" values to be folded, not just op0 and
11187 op1. */
11188 c_inhibit_evaluation_warnings++;
11189 op0 = c_fully_fold (op0, require_constant_value,
11190 &op0_maybe_const);
11191 op1 = c_fully_fold (op1, require_constant_value,
11192 &op1_maybe_const);
11193 c_inhibit_evaluation_warnings--;
11194 orig_op0_folded = c_fully_fold (orig_op0,
11195 require_constant_value,
11196 NULL);
11197 orig_op1_folded = c_fully_fold (orig_op1,
11198 require_constant_value,
11199 NULL);
11202 if (warn_sign_compare)
11203 warn_for_sign_compare (location, orig_op0_folded,
11204 orig_op1_folded, op0, op1,
11205 result_type, resultcode);
11206 if (!in_late_binary_op && !int_operands)
11208 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11209 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11210 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11211 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11217 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11218 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11219 Then the expression will be built.
11220 It will be given type FINAL_TYPE if that is nonzero;
11221 otherwise, it will be given type RESULT_TYPE. */
11223 if (!result_type)
11225 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11226 return error_mark_node;
11229 if (build_type == NULL_TREE)
11231 build_type = result_type;
11232 if ((type0 != orig_type0 || type1 != orig_type1)
11233 && !boolean_op)
11235 gcc_assert (may_need_excess_precision && common);
11236 semantic_result_type = c_common_type (orig_type0, orig_type1);
11240 if (!converted)
11242 op0 = ep_convert_and_check (location, result_type, op0,
11243 semantic_result_type);
11244 op1 = ep_convert_and_check (location, result_type, op1,
11245 semantic_result_type);
11247 /* This can happen if one operand has a vector type, and the other
11248 has a different type. */
11249 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11250 return error_mark_node;
11253 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11254 | SANITIZE_FLOAT_DIVIDE))
11255 && do_ubsan_in_current_function ()
11256 && (doing_div_or_mod || doing_shift))
11258 /* OP0 and/or OP1 might have side-effects. */
11259 op0 = c_save_expr (op0);
11260 op1 = c_save_expr (op1);
11261 op0 = c_fully_fold (op0, false, NULL);
11262 op1 = c_fully_fold (op1, false, NULL);
11263 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11264 | SANITIZE_FLOAT_DIVIDE)))
11265 instrument_expr = ubsan_instrument_division (location, op0, op1);
11266 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11267 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11270 /* Treat expressions in initializers specially as they can't trap. */
11271 if (int_const_or_overflow)
11272 ret = (require_constant_value
11273 ? fold_build2_initializer_loc (location, resultcode, build_type,
11274 op0, op1)
11275 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11276 else
11277 ret = build2 (resultcode, build_type, op0, op1);
11278 if (final_type != 0)
11279 ret = convert (final_type, ret);
11281 return_build_binary_op:
11282 gcc_assert (ret != error_mark_node);
11283 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11284 ret = (int_operands
11285 ? note_integer_operands (ret)
11286 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11287 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11288 && !in_late_binary_op)
11289 ret = note_integer_operands (ret);
11290 if (semantic_result_type)
11291 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11292 protected_set_expr_location (ret, location);
11294 if (instrument_expr != NULL)
11295 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11296 instrument_expr, ret);
11298 return ret;
11302 /* Convert EXPR to be a truth-value, validating its type for this
11303 purpose. LOCATION is the source location for the expression. */
11305 tree
11306 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11308 bool int_const, int_operands;
11310 switch (TREE_CODE (TREE_TYPE (expr)))
11312 case ARRAY_TYPE:
11313 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11314 return error_mark_node;
11316 case RECORD_TYPE:
11317 error_at (location, "used struct type value where scalar is required");
11318 return error_mark_node;
11320 case UNION_TYPE:
11321 error_at (location, "used union type value where scalar is required");
11322 return error_mark_node;
11324 case VOID_TYPE:
11325 error_at (location, "void value not ignored as it ought to be");
11326 return error_mark_node;
11328 case FUNCTION_TYPE:
11329 gcc_unreachable ();
11331 case VECTOR_TYPE:
11332 error_at (location, "used vector type where scalar is required");
11333 return error_mark_node;
11335 default:
11336 break;
11339 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11340 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11341 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11343 expr = remove_c_maybe_const_expr (expr);
11344 expr = build2 (NE_EXPR, integer_type_node, expr,
11345 convert (TREE_TYPE (expr), integer_zero_node));
11346 expr = note_integer_operands (expr);
11348 else
11349 /* ??? Should we also give an error for vectors rather than leaving
11350 those to give errors later? */
11351 expr = c_common_truthvalue_conversion (location, expr);
11353 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11355 if (TREE_OVERFLOW (expr))
11356 return expr;
11357 else
11358 return note_integer_operands (expr);
11360 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11361 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11362 return expr;
11366 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11367 required. */
11369 tree
11370 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11372 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11374 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11375 /* Executing a compound literal inside a function reinitializes
11376 it. */
11377 if (!TREE_STATIC (decl))
11378 *se = true;
11379 return decl;
11381 else
11382 return expr;
11385 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11386 statement. LOC is the location of the OACC_PARALLEL. */
11388 tree
11389 c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11391 tree stmt;
11393 block = c_end_compound_stmt (loc, block, true);
11395 stmt = make_node (OACC_PARALLEL);
11396 TREE_TYPE (stmt) = void_type_node;
11397 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11398 OACC_PARALLEL_BODY (stmt) = block;
11399 SET_EXPR_LOCATION (stmt, loc);
11401 return add_stmt (stmt);
11404 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11405 statement. LOC is the location of the OACC_KERNELS. */
11407 tree
11408 c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11410 tree stmt;
11412 block = c_end_compound_stmt (loc, block, true);
11414 stmt = make_node (OACC_KERNELS);
11415 TREE_TYPE (stmt) = void_type_node;
11416 OACC_KERNELS_CLAUSES (stmt) = clauses;
11417 OACC_KERNELS_BODY (stmt) = block;
11418 SET_EXPR_LOCATION (stmt, loc);
11420 return add_stmt (stmt);
11423 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11424 statement. LOC is the location of the OACC_DATA. */
11426 tree
11427 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11429 tree stmt;
11431 block = c_end_compound_stmt (loc, block, true);
11433 stmt = make_node (OACC_DATA);
11434 TREE_TYPE (stmt) = void_type_node;
11435 OACC_DATA_CLAUSES (stmt) = clauses;
11436 OACC_DATA_BODY (stmt) = block;
11437 SET_EXPR_LOCATION (stmt, loc);
11439 return add_stmt (stmt);
11442 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11444 tree
11445 c_begin_omp_parallel (void)
11447 tree block;
11449 keep_next_level ();
11450 block = c_begin_compound_stmt (true);
11452 return block;
11455 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11456 statement. LOC is the location of the OMP_PARALLEL. */
11458 tree
11459 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11461 tree stmt;
11463 block = c_end_compound_stmt (loc, block, true);
11465 stmt = make_node (OMP_PARALLEL);
11466 TREE_TYPE (stmt) = void_type_node;
11467 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11468 OMP_PARALLEL_BODY (stmt) = block;
11469 SET_EXPR_LOCATION (stmt, loc);
11471 return add_stmt (stmt);
11474 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11476 tree
11477 c_begin_omp_task (void)
11479 tree block;
11481 keep_next_level ();
11482 block = c_begin_compound_stmt (true);
11484 return block;
11487 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11488 statement. LOC is the location of the #pragma. */
11490 tree
11491 c_finish_omp_task (location_t loc, tree clauses, tree block)
11493 tree stmt;
11495 block = c_end_compound_stmt (loc, block, true);
11497 stmt = make_node (OMP_TASK);
11498 TREE_TYPE (stmt) = void_type_node;
11499 OMP_TASK_CLAUSES (stmt) = clauses;
11500 OMP_TASK_BODY (stmt) = block;
11501 SET_EXPR_LOCATION (stmt, loc);
11503 return add_stmt (stmt);
11506 /* Generate GOMP_cancel call for #pragma omp cancel. */
11508 void
11509 c_finish_omp_cancel (location_t loc, tree clauses)
11511 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11512 int mask = 0;
11513 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11514 mask = 1;
11515 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11516 mask = 2;
11517 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11518 mask = 4;
11519 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11520 mask = 8;
11521 else
11523 error_at (loc, "%<#pragma omp cancel must specify one of "
11524 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11525 "clauses");
11526 return;
11528 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11529 if (ifc != NULL_TREE)
11531 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11532 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11533 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11534 build_zero_cst (type));
11536 else
11537 ifc = boolean_true_node;
11538 tree stmt = build_call_expr_loc (loc, fn, 2,
11539 build_int_cst (integer_type_node, mask),
11540 ifc);
11541 add_stmt (stmt);
11544 /* Generate GOMP_cancellation_point call for
11545 #pragma omp cancellation point. */
11547 void
11548 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11550 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11551 int mask = 0;
11552 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11553 mask = 1;
11554 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11555 mask = 2;
11556 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11557 mask = 4;
11558 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11559 mask = 8;
11560 else
11562 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11563 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11564 "clauses");
11565 return;
11567 tree stmt = build_call_expr_loc (loc, fn, 1,
11568 build_int_cst (integer_type_node, mask));
11569 add_stmt (stmt);
11572 /* Helper function for handle_omp_array_sections. Called recursively
11573 to handle multiple array-section-subscripts. C is the clause,
11574 T current expression (initially OMP_CLAUSE_DECL), which is either
11575 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11576 expression if specified, TREE_VALUE length expression if specified,
11577 TREE_CHAIN is what it has been specified after, or some decl.
11578 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11579 set to true if any of the array-section-subscript could have length
11580 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11581 first array-section-subscript which is known not to have length
11582 of one. Given say:
11583 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11584 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11585 all are or may have length of 1, array-section-subscript [:2] is the
11586 first one knonwn not to have length 1. For array-section-subscript
11587 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11588 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11589 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11590 case though, as some lengths could be zero. */
11592 static tree
11593 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11594 bool &maybe_zero_len, unsigned int &first_non_one)
11596 tree ret, low_bound, length, type;
11597 if (TREE_CODE (t) != TREE_LIST)
11599 if (error_operand_p (t))
11600 return error_mark_node;
11601 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
11603 if (DECL_P (t))
11604 error_at (OMP_CLAUSE_LOCATION (c),
11605 "%qD is not a variable in %qs clause", t,
11606 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11607 else
11608 error_at (OMP_CLAUSE_LOCATION (c),
11609 "%qE is not a variable in %qs clause", t,
11610 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11611 return error_mark_node;
11613 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11614 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
11616 error_at (OMP_CLAUSE_LOCATION (c),
11617 "%qD is threadprivate variable in %qs clause", t,
11618 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11619 return error_mark_node;
11621 return t;
11624 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11625 maybe_zero_len, first_non_one);
11626 if (ret == error_mark_node || ret == NULL_TREE)
11627 return ret;
11629 type = TREE_TYPE (ret);
11630 low_bound = TREE_PURPOSE (t);
11631 length = TREE_VALUE (t);
11633 if (low_bound == error_mark_node || length == error_mark_node)
11634 return error_mark_node;
11636 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11638 error_at (OMP_CLAUSE_LOCATION (c),
11639 "low bound %qE of array section does not have integral type",
11640 low_bound);
11641 return error_mark_node;
11643 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11645 error_at (OMP_CLAUSE_LOCATION (c),
11646 "length %qE of array section does not have integral type",
11647 length);
11648 return error_mark_node;
11650 if (low_bound
11651 && TREE_CODE (low_bound) == INTEGER_CST
11652 && TYPE_PRECISION (TREE_TYPE (low_bound))
11653 > TYPE_PRECISION (sizetype))
11654 low_bound = fold_convert (sizetype, low_bound);
11655 if (length
11656 && TREE_CODE (length) == INTEGER_CST
11657 && TYPE_PRECISION (TREE_TYPE (length))
11658 > TYPE_PRECISION (sizetype))
11659 length = fold_convert (sizetype, length);
11660 if (low_bound == NULL_TREE)
11661 low_bound = integer_zero_node;
11663 if (length != NULL_TREE)
11665 if (!integer_nonzerop (length))
11666 maybe_zero_len = true;
11667 if (first_non_one == types.length ()
11668 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11669 first_non_one++;
11671 if (TREE_CODE (type) == ARRAY_TYPE)
11673 if (length == NULL_TREE
11674 && (TYPE_DOMAIN (type) == NULL_TREE
11675 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11677 error_at (OMP_CLAUSE_LOCATION (c),
11678 "for unknown bound array type length expression must "
11679 "be specified");
11680 return error_mark_node;
11682 if (TREE_CODE (low_bound) == INTEGER_CST
11683 && tree_int_cst_sgn (low_bound) == -1)
11685 error_at (OMP_CLAUSE_LOCATION (c),
11686 "negative low bound in array section in %qs clause",
11687 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11688 return error_mark_node;
11690 if (length != NULL_TREE
11691 && TREE_CODE (length) == INTEGER_CST
11692 && tree_int_cst_sgn (length) == -1)
11694 error_at (OMP_CLAUSE_LOCATION (c),
11695 "negative length in array section in %qs clause",
11696 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11697 return error_mark_node;
11699 if (TYPE_DOMAIN (type)
11700 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11701 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11702 == INTEGER_CST)
11704 tree size = size_binop (PLUS_EXPR,
11705 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11706 size_one_node);
11707 if (TREE_CODE (low_bound) == INTEGER_CST)
11709 if (tree_int_cst_lt (size, low_bound))
11711 error_at (OMP_CLAUSE_LOCATION (c),
11712 "low bound %qE above array section size "
11713 "in %qs clause", low_bound,
11714 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11715 return error_mark_node;
11717 if (tree_int_cst_equal (size, low_bound))
11718 maybe_zero_len = true;
11719 else if (length == NULL_TREE
11720 && first_non_one == types.length ()
11721 && tree_int_cst_equal
11722 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11723 low_bound))
11724 first_non_one++;
11726 else if (length == NULL_TREE)
11728 maybe_zero_len = true;
11729 if (first_non_one == types.length ())
11730 first_non_one++;
11732 if (length && TREE_CODE (length) == INTEGER_CST)
11734 if (tree_int_cst_lt (size, length))
11736 error_at (OMP_CLAUSE_LOCATION (c),
11737 "length %qE above array section size "
11738 "in %qs clause", length,
11739 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11740 return error_mark_node;
11742 if (TREE_CODE (low_bound) == INTEGER_CST)
11744 tree lbpluslen
11745 = size_binop (PLUS_EXPR,
11746 fold_convert (sizetype, low_bound),
11747 fold_convert (sizetype, length));
11748 if (TREE_CODE (lbpluslen) == INTEGER_CST
11749 && tree_int_cst_lt (size, lbpluslen))
11751 error_at (OMP_CLAUSE_LOCATION (c),
11752 "high bound %qE above array section size "
11753 "in %qs clause", lbpluslen,
11754 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11755 return error_mark_node;
11760 else if (length == NULL_TREE)
11762 maybe_zero_len = true;
11763 if (first_non_one == types.length ())
11764 first_non_one++;
11767 /* For [lb:] we will need to evaluate lb more than once. */
11768 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11770 tree lb = c_save_expr (low_bound);
11771 if (lb != low_bound)
11773 TREE_PURPOSE (t) = lb;
11774 low_bound = lb;
11778 else if (TREE_CODE (type) == POINTER_TYPE)
11780 if (length == NULL_TREE)
11782 error_at (OMP_CLAUSE_LOCATION (c),
11783 "for pointer type length expression must be specified");
11784 return error_mark_node;
11786 /* If there is a pointer type anywhere but in the very first
11787 array-section-subscript, the array section can't be contiguous. */
11788 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11789 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11791 error_at (OMP_CLAUSE_LOCATION (c),
11792 "array section is not contiguous in %qs clause",
11793 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11794 return error_mark_node;
11797 else
11799 error_at (OMP_CLAUSE_LOCATION (c),
11800 "%qE does not have pointer or array type", ret);
11801 return error_mark_node;
11803 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11804 types.safe_push (TREE_TYPE (ret));
11805 /* We will need to evaluate lb more than once. */
11806 tree lb = c_save_expr (low_bound);
11807 if (lb != low_bound)
11809 TREE_PURPOSE (t) = lb;
11810 low_bound = lb;
11812 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11813 return ret;
11816 /* Handle array sections for clause C. */
11818 static bool
11819 handle_omp_array_sections (tree c)
11821 bool maybe_zero_len = false;
11822 unsigned int first_non_one = 0;
11823 vec<tree> types = vNULL;
11824 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11825 maybe_zero_len, first_non_one);
11826 if (first == error_mark_node)
11828 types.release ();
11829 return true;
11831 if (first == NULL_TREE)
11833 types.release ();
11834 return false;
11836 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11838 tree t = OMP_CLAUSE_DECL (c);
11839 tree tem = NULL_TREE;
11840 types.release ();
11841 /* Need to evaluate side effects in the length expressions
11842 if any. */
11843 while (TREE_CODE (t) == TREE_LIST)
11845 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11847 if (tem == NULL_TREE)
11848 tem = TREE_VALUE (t);
11849 else
11850 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11851 TREE_VALUE (t), tem);
11853 t = TREE_CHAIN (t);
11855 if (tem)
11856 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11857 first = c_fully_fold (first, false, NULL);
11858 OMP_CLAUSE_DECL (c) = first;
11860 else
11862 unsigned int num = types.length (), i;
11863 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11864 tree condition = NULL_TREE;
11866 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11867 maybe_zero_len = true;
11869 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11870 t = TREE_CHAIN (t))
11872 tree low_bound = TREE_PURPOSE (t);
11873 tree length = TREE_VALUE (t);
11875 i--;
11876 if (low_bound
11877 && TREE_CODE (low_bound) == INTEGER_CST
11878 && TYPE_PRECISION (TREE_TYPE (low_bound))
11879 > TYPE_PRECISION (sizetype))
11880 low_bound = fold_convert (sizetype, low_bound);
11881 if (length
11882 && TREE_CODE (length) == INTEGER_CST
11883 && TYPE_PRECISION (TREE_TYPE (length))
11884 > TYPE_PRECISION (sizetype))
11885 length = fold_convert (sizetype, length);
11886 if (low_bound == NULL_TREE)
11887 low_bound = integer_zero_node;
11888 if (!maybe_zero_len && i > first_non_one)
11890 if (integer_nonzerop (low_bound))
11891 goto do_warn_noncontiguous;
11892 if (length != NULL_TREE
11893 && TREE_CODE (length) == INTEGER_CST
11894 && TYPE_DOMAIN (types[i])
11895 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11896 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11897 == INTEGER_CST)
11899 tree size;
11900 size = size_binop (PLUS_EXPR,
11901 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11902 size_one_node);
11903 if (!tree_int_cst_equal (length, size))
11905 do_warn_noncontiguous:
11906 error_at (OMP_CLAUSE_LOCATION (c),
11907 "array section is not contiguous in %qs "
11908 "clause",
11909 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11910 types.release ();
11911 return true;
11914 if (length != NULL_TREE
11915 && TREE_SIDE_EFFECTS (length))
11917 if (side_effects == NULL_TREE)
11918 side_effects = length;
11919 else
11920 side_effects = build2 (COMPOUND_EXPR,
11921 TREE_TYPE (side_effects),
11922 length, side_effects);
11925 else
11927 tree l;
11929 if (i > first_non_one && length && integer_nonzerop (length))
11930 continue;
11931 if (length)
11932 l = fold_convert (sizetype, length);
11933 else
11935 l = size_binop (PLUS_EXPR,
11936 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11937 size_one_node);
11938 l = size_binop (MINUS_EXPR, l,
11939 fold_convert (sizetype, low_bound));
11941 if (i > first_non_one)
11943 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11944 size_zero_node);
11945 if (condition == NULL_TREE)
11946 condition = l;
11947 else
11948 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11949 l, condition);
11951 else if (size == NULL_TREE)
11953 size = size_in_bytes (TREE_TYPE (types[i]));
11954 size = size_binop (MULT_EXPR, size, l);
11955 if (condition)
11956 size = fold_build3 (COND_EXPR, sizetype, condition,
11957 size, size_zero_node);
11959 else
11960 size = size_binop (MULT_EXPR, size, l);
11963 types.release ();
11964 if (side_effects)
11965 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11966 first = c_fully_fold (first, false, NULL);
11967 OMP_CLAUSE_DECL (c) = first;
11968 if (size)
11969 size = c_fully_fold (size, false, NULL);
11970 OMP_CLAUSE_SIZE (c) = size;
11971 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11972 return false;
11973 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
11974 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11975 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
11976 if (!c_mark_addressable (t))
11977 return false;
11978 OMP_CLAUSE_DECL (c2) = t;
11979 t = build_fold_addr_expr (first);
11980 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11981 tree ptr = OMP_CLAUSE_DECL (c2);
11982 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11983 ptr = build_fold_addr_expr (ptr);
11984 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11985 ptrdiff_type_node, t,
11986 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11987 ptrdiff_type_node, ptr));
11988 t = c_fully_fold (t, false, NULL);
11989 OMP_CLAUSE_SIZE (c2) = t;
11990 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11991 OMP_CLAUSE_CHAIN (c) = c2;
11993 return false;
11996 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11997 an inline call. But, remap
11998 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11999 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12001 static tree
12002 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12003 tree decl, tree placeholder)
12005 copy_body_data id;
12006 hash_map<tree, tree> decl_map;
12008 decl_map.put (omp_decl1, placeholder);
12009 decl_map.put (omp_decl2, decl);
12010 memset (&id, 0, sizeof (id));
12011 id.src_fn = DECL_CONTEXT (omp_decl1);
12012 id.dst_fn = current_function_decl;
12013 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12014 id.decl_map = &decl_map;
12016 id.copy_decl = copy_decl_no_change;
12017 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12018 id.transform_new_cfg = true;
12019 id.transform_return_to_modify = false;
12020 id.transform_lang_insert_block = NULL;
12021 id.eh_lp_nr = 0;
12022 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12023 return stmt;
12026 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12027 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12029 static tree
12030 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12032 if (*tp == (tree) data)
12033 return *tp;
12034 return NULL_TREE;
12037 /* For all elements of CLAUSES, validate them against their constraints.
12038 Remove any elements from the list that are invalid. */
12040 tree
12041 c_finish_omp_clauses (tree clauses)
12043 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12044 bitmap_head aligned_head;
12045 tree c, t, *pc;
12046 bool branch_seen = false;
12047 bool copyprivate_seen = false;
12048 tree *nowait_clause = NULL;
12050 bitmap_obstack_initialize (NULL);
12051 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12052 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12053 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12054 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12056 for (pc = &clauses, c = clauses; c ; c = *pc)
12058 bool remove = false;
12059 bool need_complete = false;
12060 bool need_implicitly_determined = false;
12062 switch (OMP_CLAUSE_CODE (c))
12064 case OMP_CLAUSE_SHARED:
12065 need_implicitly_determined = true;
12066 goto check_dup_generic;
12068 case OMP_CLAUSE_PRIVATE:
12069 need_complete = true;
12070 need_implicitly_determined = true;
12071 goto check_dup_generic;
12073 case OMP_CLAUSE_REDUCTION:
12074 need_implicitly_determined = true;
12075 t = OMP_CLAUSE_DECL (c);
12076 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12077 && (FLOAT_TYPE_P (TREE_TYPE (t))
12078 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
12080 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12081 const char *r_name = NULL;
12083 switch (r_code)
12085 case PLUS_EXPR:
12086 case MULT_EXPR:
12087 case MINUS_EXPR:
12088 break;
12089 case MIN_EXPR:
12090 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12091 r_name = "min";
12092 break;
12093 case MAX_EXPR:
12094 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12095 r_name = "max";
12096 break;
12097 case BIT_AND_EXPR:
12098 r_name = "&";
12099 break;
12100 case BIT_XOR_EXPR:
12101 r_name = "^";
12102 break;
12103 case BIT_IOR_EXPR:
12104 r_name = "|";
12105 break;
12106 case TRUTH_ANDIF_EXPR:
12107 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12108 r_name = "&&";
12109 break;
12110 case TRUTH_ORIF_EXPR:
12111 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12112 r_name = "||";
12113 break;
12114 default:
12115 gcc_unreachable ();
12117 if (r_name)
12119 error_at (OMP_CLAUSE_LOCATION (c),
12120 "%qE has invalid type for %<reduction(%s)%>",
12121 t, r_name);
12122 remove = true;
12123 break;
12126 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12128 error_at (OMP_CLAUSE_LOCATION (c),
12129 "user defined reduction not found for %qD", t);
12130 remove = true;
12131 break;
12133 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12135 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12136 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12137 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12138 VAR_DECL, NULL_TREE, type);
12139 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12140 DECL_ARTIFICIAL (placeholder) = 1;
12141 DECL_IGNORED_P (placeholder) = 1;
12142 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12143 c_mark_addressable (placeholder);
12144 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12145 c_mark_addressable (OMP_CLAUSE_DECL (c));
12146 OMP_CLAUSE_REDUCTION_MERGE (c)
12147 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12148 TREE_VEC_ELT (list, 0),
12149 TREE_VEC_ELT (list, 1),
12150 OMP_CLAUSE_DECL (c), placeholder);
12151 OMP_CLAUSE_REDUCTION_MERGE (c)
12152 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12153 void_type_node, NULL_TREE,
12154 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12155 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12156 if (TREE_VEC_LENGTH (list) == 6)
12158 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12159 c_mark_addressable (OMP_CLAUSE_DECL (c));
12160 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12161 c_mark_addressable (placeholder);
12162 tree init = TREE_VEC_ELT (list, 5);
12163 if (init == error_mark_node)
12164 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12165 OMP_CLAUSE_REDUCTION_INIT (c)
12166 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12167 TREE_VEC_ELT (list, 3),
12168 OMP_CLAUSE_DECL (c), placeholder);
12169 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12170 OMP_CLAUSE_REDUCTION_INIT (c)
12171 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12172 OMP_CLAUSE_REDUCTION_INIT (c));
12173 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12174 c_find_omp_placeholder_r,
12175 placeholder, NULL))
12176 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12178 else
12180 tree init;
12181 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12182 init = build_constructor (TREE_TYPE (t), NULL);
12183 else
12184 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12185 OMP_CLAUSE_REDUCTION_INIT (c)
12186 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12188 OMP_CLAUSE_REDUCTION_INIT (c)
12189 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12190 void_type_node, NULL_TREE,
12191 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12192 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12194 goto check_dup_generic;
12196 case OMP_CLAUSE_COPYPRIVATE:
12197 copyprivate_seen = true;
12198 if (nowait_clause)
12200 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12201 "%<nowait%> clause must not be used together "
12202 "with %<copyprivate%>");
12203 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12204 nowait_clause = NULL;
12206 goto check_dup_generic;
12208 case OMP_CLAUSE_COPYIN:
12209 t = OMP_CLAUSE_DECL (c);
12210 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
12212 error_at (OMP_CLAUSE_LOCATION (c),
12213 "%qE must be %<threadprivate%> for %<copyin%>", t);
12214 remove = true;
12215 break;
12217 goto check_dup_generic;
12219 case OMP_CLAUSE_LINEAR:
12220 t = OMP_CLAUSE_DECL (c);
12221 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12222 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12224 error_at (OMP_CLAUSE_LOCATION (c),
12225 "linear clause applied to non-integral non-pointer "
12226 "variable with type %qT", TREE_TYPE (t));
12227 remove = true;
12228 break;
12230 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12232 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12233 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12234 OMP_CLAUSE_DECL (c), s);
12235 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12236 sizetype, s, OMP_CLAUSE_DECL (c));
12237 if (s == error_mark_node)
12238 s = size_one_node;
12239 OMP_CLAUSE_LINEAR_STEP (c) = s;
12241 else
12242 OMP_CLAUSE_LINEAR_STEP (c)
12243 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12244 goto check_dup_generic;
12246 check_dup_generic:
12247 t = OMP_CLAUSE_DECL (c);
12248 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12250 error_at (OMP_CLAUSE_LOCATION (c),
12251 "%qE is not a variable in clause %qs", t,
12252 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12253 remove = true;
12255 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12256 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12257 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12259 error_at (OMP_CLAUSE_LOCATION (c),
12260 "%qE appears more than once in data clauses", t);
12261 remove = true;
12263 else
12264 bitmap_set_bit (&generic_head, DECL_UID (t));
12265 break;
12267 case OMP_CLAUSE_FIRSTPRIVATE:
12268 t = OMP_CLAUSE_DECL (c);
12269 need_complete = true;
12270 need_implicitly_determined = true;
12271 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12273 error_at (OMP_CLAUSE_LOCATION (c),
12274 "%qE is not a variable in clause %<firstprivate%>", t);
12275 remove = true;
12277 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12278 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12280 error_at (OMP_CLAUSE_LOCATION (c),
12281 "%qE appears more than once in data clauses", t);
12282 remove = true;
12284 else
12285 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12286 break;
12288 case OMP_CLAUSE_LASTPRIVATE:
12289 t = OMP_CLAUSE_DECL (c);
12290 need_complete = true;
12291 need_implicitly_determined = true;
12292 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12294 error_at (OMP_CLAUSE_LOCATION (c),
12295 "%qE is not a variable in clause %<lastprivate%>", t);
12296 remove = true;
12298 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12299 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12301 error_at (OMP_CLAUSE_LOCATION (c),
12302 "%qE appears more than once in data clauses", t);
12303 remove = true;
12305 else
12306 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12307 break;
12309 case OMP_CLAUSE_ALIGNED:
12310 t = OMP_CLAUSE_DECL (c);
12311 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12313 error_at (OMP_CLAUSE_LOCATION (c),
12314 "%qE is not a variable in %<aligned%> clause", t);
12315 remove = true;
12317 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12318 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12320 error_at (OMP_CLAUSE_LOCATION (c),
12321 "%qE in %<aligned%> clause is neither a pointer nor "
12322 "an array", t);
12323 remove = true;
12325 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12327 error_at (OMP_CLAUSE_LOCATION (c),
12328 "%qE appears more than once in %<aligned%> clauses",
12330 remove = true;
12332 else
12333 bitmap_set_bit (&aligned_head, DECL_UID (t));
12334 break;
12336 case OMP_CLAUSE_DEPEND:
12337 t = OMP_CLAUSE_DECL (c);
12338 if (TREE_CODE (t) == TREE_LIST)
12340 if (handle_omp_array_sections (c))
12341 remove = true;
12342 break;
12344 if (t == error_mark_node)
12345 remove = true;
12346 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12348 error_at (OMP_CLAUSE_LOCATION (c),
12349 "%qE is not a variable in %<depend%> clause", t);
12350 remove = true;
12352 else if (!c_mark_addressable (t))
12353 remove = true;
12354 break;
12356 case OMP_CLAUSE_MAP:
12357 case OMP_CLAUSE_TO:
12358 case OMP_CLAUSE_FROM:
12359 case OMP_CLAUSE__CACHE_:
12360 t = OMP_CLAUSE_DECL (c);
12361 if (TREE_CODE (t) == TREE_LIST)
12363 if (handle_omp_array_sections (c))
12364 remove = true;
12365 else
12367 t = OMP_CLAUSE_DECL (c);
12368 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12370 error_at (OMP_CLAUSE_LOCATION (c),
12371 "array section does not have mappable type "
12372 "in %qs clause",
12373 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12374 remove = true;
12377 break;
12379 if (t == error_mark_node)
12380 remove = true;
12381 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12383 error_at (OMP_CLAUSE_LOCATION (c),
12384 "%qE is not a variable in %qs clause", t,
12385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12386 remove = true;
12388 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
12390 error_at (OMP_CLAUSE_LOCATION (c),
12391 "%qD is threadprivate variable in %qs clause", t,
12392 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12393 remove = true;
12395 else if (!c_mark_addressable (t))
12396 remove = true;
12397 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12398 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12399 || (OMP_CLAUSE_MAP_KIND (c)
12400 == GOMP_MAP_FORCE_DEVICEPTR)))
12401 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12403 error_at (OMP_CLAUSE_LOCATION (c),
12404 "%qD does not have a mappable type in %qs clause", t,
12405 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12406 remove = true;
12408 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12410 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12411 error ("%qD appears more than once in motion clauses", t);
12412 else
12413 error ("%qD appears more than once in map clauses", t);
12414 remove = true;
12416 else
12417 bitmap_set_bit (&generic_head, DECL_UID (t));
12418 break;
12420 case OMP_CLAUSE_UNIFORM:
12421 t = OMP_CLAUSE_DECL (c);
12422 if (TREE_CODE (t) != PARM_DECL)
12424 if (DECL_P (t))
12425 error_at (OMP_CLAUSE_LOCATION (c),
12426 "%qD is not an argument in %<uniform%> clause", t);
12427 else
12428 error_at (OMP_CLAUSE_LOCATION (c),
12429 "%qE is not an argument in %<uniform%> clause", t);
12430 remove = true;
12431 break;
12433 goto check_dup_generic;
12435 case OMP_CLAUSE_NOWAIT:
12436 if (copyprivate_seen)
12438 error_at (OMP_CLAUSE_LOCATION (c),
12439 "%<nowait%> clause must not be used together "
12440 "with %<copyprivate%>");
12441 remove = true;
12442 break;
12444 nowait_clause = pc;
12445 pc = &OMP_CLAUSE_CHAIN (c);
12446 continue;
12448 case OMP_CLAUSE_IF:
12449 case OMP_CLAUSE_NUM_THREADS:
12450 case OMP_CLAUSE_NUM_TEAMS:
12451 case OMP_CLAUSE_THREAD_LIMIT:
12452 case OMP_CLAUSE_SCHEDULE:
12453 case OMP_CLAUSE_ORDERED:
12454 case OMP_CLAUSE_DEFAULT:
12455 case OMP_CLAUSE_UNTIED:
12456 case OMP_CLAUSE_COLLAPSE:
12457 case OMP_CLAUSE_FINAL:
12458 case OMP_CLAUSE_MERGEABLE:
12459 case OMP_CLAUSE_SAFELEN:
12460 case OMP_CLAUSE_SIMDLEN:
12461 case OMP_CLAUSE_DEVICE:
12462 case OMP_CLAUSE_DIST_SCHEDULE:
12463 case OMP_CLAUSE_PARALLEL:
12464 case OMP_CLAUSE_FOR:
12465 case OMP_CLAUSE_SECTIONS:
12466 case OMP_CLAUSE_TASKGROUP:
12467 case OMP_CLAUSE_PROC_BIND:
12468 case OMP_CLAUSE__CILK_FOR_COUNT_:
12469 case OMP_CLAUSE_NUM_GANGS:
12470 case OMP_CLAUSE_NUM_WORKERS:
12471 case OMP_CLAUSE_VECTOR_LENGTH:
12472 case OMP_CLAUSE_ASYNC:
12473 case OMP_CLAUSE_WAIT:
12474 case OMP_CLAUSE_AUTO:
12475 case OMP_CLAUSE_SEQ:
12476 case OMP_CLAUSE_GANG:
12477 case OMP_CLAUSE_WORKER:
12478 case OMP_CLAUSE_VECTOR:
12479 pc = &OMP_CLAUSE_CHAIN (c);
12480 continue;
12482 case OMP_CLAUSE_INBRANCH:
12483 case OMP_CLAUSE_NOTINBRANCH:
12484 if (branch_seen)
12486 error_at (OMP_CLAUSE_LOCATION (c),
12487 "%<inbranch%> clause is incompatible with "
12488 "%<notinbranch%>");
12489 remove = true;
12490 break;
12492 branch_seen = true;
12493 pc = &OMP_CLAUSE_CHAIN (c);
12494 continue;
12496 default:
12497 gcc_unreachable ();
12500 if (!remove)
12502 t = OMP_CLAUSE_DECL (c);
12504 if (need_complete)
12506 t = require_complete_type (t);
12507 if (t == error_mark_node)
12508 remove = true;
12511 if (need_implicitly_determined)
12513 const char *share_name = NULL;
12515 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
12516 share_name = "threadprivate";
12517 else switch (c_omp_predetermined_sharing (t))
12519 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12520 break;
12521 case OMP_CLAUSE_DEFAULT_SHARED:
12522 /* const vars may be specified in firstprivate clause. */
12523 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12524 && TREE_READONLY (t))
12525 break;
12526 share_name = "shared";
12527 break;
12528 case OMP_CLAUSE_DEFAULT_PRIVATE:
12529 share_name = "private";
12530 break;
12531 default:
12532 gcc_unreachable ();
12534 if (share_name)
12536 error_at (OMP_CLAUSE_LOCATION (c),
12537 "%qE is predetermined %qs for %qs",
12538 t, share_name,
12539 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12540 remove = true;
12545 if (remove)
12546 *pc = OMP_CLAUSE_CHAIN (c);
12547 else
12548 pc = &OMP_CLAUSE_CHAIN (c);
12551 bitmap_obstack_release (NULL);
12552 return clauses;
12555 /* Create a transaction node. */
12557 tree
12558 c_finish_transaction (location_t loc, tree block, int flags)
12560 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12561 if (flags & TM_STMT_ATTR_OUTER)
12562 TRANSACTION_EXPR_OUTER (stmt) = 1;
12563 if (flags & TM_STMT_ATTR_RELAXED)
12564 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12565 return add_stmt (stmt);
12568 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12569 down to the element type of an array. */
12571 tree
12572 c_build_qualified_type (tree type, int type_quals)
12574 if (type == error_mark_node)
12575 return type;
12577 if (TREE_CODE (type) == ARRAY_TYPE)
12579 tree t;
12580 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12581 type_quals);
12583 /* See if we already have an identically qualified type. */
12584 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12586 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12587 && TYPE_NAME (t) == TYPE_NAME (type)
12588 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12589 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12590 TYPE_ATTRIBUTES (type)))
12591 break;
12593 if (!t)
12595 tree domain = TYPE_DOMAIN (type);
12597 t = build_variant_type_copy (type);
12598 TREE_TYPE (t) = element_type;
12600 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12601 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12602 SET_TYPE_STRUCTURAL_EQUALITY (t);
12603 else if (TYPE_CANONICAL (element_type) != element_type
12604 || (domain && TYPE_CANONICAL (domain) != domain))
12606 tree unqualified_canon
12607 = build_array_type (TYPE_CANONICAL (element_type),
12608 domain? TYPE_CANONICAL (domain)
12609 : NULL_TREE);
12610 TYPE_CANONICAL (t)
12611 = c_build_qualified_type (unqualified_canon, type_quals);
12613 else
12614 TYPE_CANONICAL (t) = t;
12616 return t;
12619 /* A restrict-qualified pointer type must be a pointer to object or
12620 incomplete type. Note that the use of POINTER_TYPE_P also allows
12621 REFERENCE_TYPEs, which is appropriate for C++. */
12622 if ((type_quals & TYPE_QUAL_RESTRICT)
12623 && (!POINTER_TYPE_P (type)
12624 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12626 error ("invalid use of %<restrict%>");
12627 type_quals &= ~TYPE_QUAL_RESTRICT;
12630 return build_qualified_type (type, type_quals);
12633 /* Build a VA_ARG_EXPR for the C parser. */
12635 tree
12636 c_build_va_arg (location_t loc, tree expr, tree type)
12638 if (error_operand_p (type))
12639 return error_mark_node;
12640 else if (!COMPLETE_TYPE_P (type))
12642 error_at (loc, "second argument to %<va_arg%> is of incomplete "
12643 "type %qT", type);
12644 return error_mark_node;
12646 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12647 warning_at (loc, OPT_Wc___compat,
12648 "C++ requires promoted type, not enum type, in %<va_arg%>");
12649 return build_va_arg (loc, expr, type);
12652 /* Return truthvalue of whether T1 is the same tree structure as T2.
12653 Return 1 if they are the same. Return 0 if they are different. */
12655 bool
12656 c_tree_equal (tree t1, tree t2)
12658 enum tree_code code1, code2;
12660 if (t1 == t2)
12661 return true;
12662 if (!t1 || !t2)
12663 return false;
12665 for (code1 = TREE_CODE (t1);
12666 CONVERT_EXPR_CODE_P (code1)
12667 || code1 == NON_LVALUE_EXPR;
12668 code1 = TREE_CODE (t1))
12669 t1 = TREE_OPERAND (t1, 0);
12670 for (code2 = TREE_CODE (t2);
12671 CONVERT_EXPR_CODE_P (code2)
12672 || code2 == NON_LVALUE_EXPR;
12673 code2 = TREE_CODE (t2))
12674 t2 = TREE_OPERAND (t2, 0);
12676 /* They might have become equal now. */
12677 if (t1 == t2)
12678 return true;
12680 if (code1 != code2)
12681 return false;
12683 switch (code1)
12685 case INTEGER_CST:
12686 return wi::eq_p (t1, t2);
12688 case REAL_CST:
12689 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12691 case STRING_CST:
12692 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12693 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12694 TREE_STRING_LENGTH (t1));
12696 case FIXED_CST:
12697 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12698 TREE_FIXED_CST (t2));
12700 case COMPLEX_CST:
12701 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12702 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12704 case VECTOR_CST:
12705 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12707 case CONSTRUCTOR:
12708 /* We need to do this when determining whether or not two
12709 non-type pointer to member function template arguments
12710 are the same. */
12711 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12712 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12713 return false;
12715 tree field, value;
12716 unsigned int i;
12717 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12719 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12720 if (!c_tree_equal (field, elt2->index)
12721 || !c_tree_equal (value, elt2->value))
12722 return false;
12725 return true;
12727 case TREE_LIST:
12728 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12729 return false;
12730 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12731 return false;
12732 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12734 case SAVE_EXPR:
12735 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12737 case CALL_EXPR:
12739 tree arg1, arg2;
12740 call_expr_arg_iterator iter1, iter2;
12741 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12742 return false;
12743 for (arg1 = first_call_expr_arg (t1, &iter1),
12744 arg2 = first_call_expr_arg (t2, &iter2);
12745 arg1 && arg2;
12746 arg1 = next_call_expr_arg (&iter1),
12747 arg2 = next_call_expr_arg (&iter2))
12748 if (!c_tree_equal (arg1, arg2))
12749 return false;
12750 if (arg1 || arg2)
12751 return false;
12752 return true;
12755 case TARGET_EXPR:
12757 tree o1 = TREE_OPERAND (t1, 0);
12758 tree o2 = TREE_OPERAND (t2, 0);
12760 /* Special case: if either target is an unallocated VAR_DECL,
12761 it means that it's going to be unified with whatever the
12762 TARGET_EXPR is really supposed to initialize, so treat it
12763 as being equivalent to anything. */
12764 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
12765 && !DECL_RTL_SET_P (o1))
12766 /*Nop*/;
12767 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
12768 && !DECL_RTL_SET_P (o2))
12769 /*Nop*/;
12770 else if (!c_tree_equal (o1, o2))
12771 return false;
12773 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12776 case COMPONENT_REF:
12777 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12778 return false;
12779 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12781 case PARM_DECL:
12782 case VAR_DECL:
12783 case CONST_DECL:
12784 case FIELD_DECL:
12785 case FUNCTION_DECL:
12786 case IDENTIFIER_NODE:
12787 case SSA_NAME:
12788 return false;
12790 case TREE_VEC:
12792 unsigned ix;
12793 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12794 return false;
12795 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12796 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12797 TREE_VEC_ELT (t2, ix)))
12798 return false;
12799 return true;
12802 default:
12803 break;
12806 switch (TREE_CODE_CLASS (code1))
12808 case tcc_unary:
12809 case tcc_binary:
12810 case tcc_comparison:
12811 case tcc_expression:
12812 case tcc_vl_exp:
12813 case tcc_reference:
12814 case tcc_statement:
12816 int i, n = TREE_OPERAND_LENGTH (t1);
12818 switch (code1)
12820 case PREINCREMENT_EXPR:
12821 case PREDECREMENT_EXPR:
12822 case POSTINCREMENT_EXPR:
12823 case POSTDECREMENT_EXPR:
12824 n = 1;
12825 break;
12826 case ARRAY_REF:
12827 n = 2;
12828 break;
12829 default:
12830 break;
12833 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12834 && n != TREE_OPERAND_LENGTH (t2))
12835 return false;
12837 for (i = 0; i < n; ++i)
12838 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12839 return false;
12841 return true;
12844 case tcc_type:
12845 return comptypes (t1, t2);
12846 default:
12847 gcc_unreachable ();
12849 /* We can get here with --disable-checking. */
12850 return false;
12853 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12854 spawn-helper and BODY is the newly created body for FNDECL. */
12856 void
12857 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12859 tree list = alloc_stmt_list ();
12860 tree frame = make_cilk_frame (fndecl);
12861 tree dtor = create_cilk_function_exit (frame, false, true);
12862 add_local_decl (cfun, frame);
12864 DECL_SAVED_TREE (fndecl) = list;
12865 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12866 frame);
12867 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12868 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12870 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12871 append_to_statement_list (detach_expr, &body_list);
12873 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12874 body = fold_build_cleanup_point_expr (void_type_node, body);
12876 append_to_statement_list (body, &body_list);
12877 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12878 body_list, dtor), &list);