Merge trunk version 212325 into gupc branch.
[official-gcc.git] / gcc / c / c-typeck.c
blobd2433b0890794c0fbaf429e968dec7ff2e67d6fc
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2014 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 "tree.h"
31 #include "tree-upc.h"
32 #include "stor-layout.h"
33 #include "trans-mem.h"
34 #include "varasm.h"
35 #include "stmt.h"
36 #include "langhooks.h"
37 #include "c-tree.h"
38 #include "c-lang.h"
39 #include "flags.h"
40 #include "intl.h"
41 #include "target.h"
42 #include "tree-iterator.h"
43 #include "bitmap.h"
44 #include "pointer-set.h"
45 #include "basic-block.h"
46 #include "gimple-expr.h"
47 #include "gimplify.h"
48 #include "tree-inline.h"
49 #include "omp-low.h"
50 #include "c-family/c-upc-low.h"
51 #include "c-family/c-objc.h"
52 #include "c-family/c-upc.h"
53 #include "c-family/c-common.h"
54 #include "c-family/c-ubsan.h"
55 #include "cilk.h"
56 #include "wide-int.h"
58 /* Possible cases of implicit bad conversions. Used to select
59 diagnostic messages in convert_for_assignment. */
60 enum impl_conv {
61 ic_argpass,
62 ic_assign,
63 ic_init,
64 ic_return
67 /* The level of nesting inside "__alignof__". */
68 int in_alignof;
70 /* The level of nesting inside "sizeof". */
71 int in_sizeof;
73 /* The level of nesting inside "typeof". */
74 int in_typeof;
76 /* The argument of last parsed sizeof expression, only to be tested
77 if expr.original_code == SIZEOF_EXPR. */
78 tree c_last_sizeof_arg;
80 /* Nonzero if we might need to print a "missing braces around
81 initializer" message within this initializer. */
82 static int found_missing_braces;
84 static int require_constant_value;
85 static int require_constant_elements;
87 static bool null_pointer_constant_p (const_tree);
88 static tree qualify_type (tree, tree);
89 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
90 bool *);
91 static int comp_target_types (location_t, tree, tree);
92 static int function_types_compatible_p (const_tree, const_tree, bool *,
93 bool *);
94 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
95 static tree lookup_field (tree, tree);
96 static int convert_arguments (location_t, vec<location_t>, tree,
97 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
98 tree);
99 static tree c_pointer_int_sum (location_t, enum tree_code, tree, 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 (value == error_mark_node || type == error_mark_node)
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 const char *type_code_string;
220 /* Avoid duplicate error message. */
221 if (TREE_CODE (type) == ERROR_MARK)
222 return;
224 if (value != 0 && (TREE_CODE (value) == VAR_DECL
225 || TREE_CODE (value) == PARM_DECL))
226 error ("%qD has an incomplete type", value);
227 else
229 retry:
230 /* We must print an error message. Be clever about what it says. */
232 switch (TREE_CODE (type))
234 case RECORD_TYPE:
235 type_code_string = "struct";
236 break;
238 case UNION_TYPE:
239 type_code_string = "union";
240 break;
242 case ENUMERAL_TYPE:
243 type_code_string = "enum";
244 break;
246 case VOID_TYPE:
247 error ("invalid use of void expression");
248 return;
250 case ARRAY_TYPE:
251 if (TYPE_DOMAIN (type))
253 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
255 error ("invalid use of flexible array member");
256 return;
258 type = TREE_TYPE (type);
259 goto retry;
261 error ("invalid use of array with unspecified bounds");
262 return;
264 default:
265 gcc_unreachable ();
268 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
269 error ("invalid use of undefined type %<%s %E%>",
270 type_code_string, TYPE_NAME (type));
271 else
272 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
273 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
277 /* Given a type, apply default promotions wrt unnamed function
278 arguments and return the new type. */
280 tree
281 c_type_promotes_to (tree type)
283 tree ret = NULL_TREE;
285 if (TYPE_MAIN_VARIANT (type) == float_type_node)
286 ret = double_type_node;
287 else if (c_promoting_integer_type_p (type))
289 /* Preserve unsignedness if not really getting any wider. */
290 if (TYPE_UNSIGNED (type)
291 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
292 ret = unsigned_type_node;
293 else
294 ret = integer_type_node;
297 if (ret != NULL_TREE)
298 return (TYPE_ATOMIC (type)
299 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
300 : ret);
302 return type;
305 /* Return true if between two named address spaces, whether there is a superset
306 named address space that encompasses both address spaces. If there is a
307 superset, return which address space is the superset. */
309 static bool
310 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
312 if (as1 == as2)
314 *common = as1;
315 return true;
317 else if (targetm.addr_space.subset_p (as1, as2))
319 *common = as2;
320 return true;
322 else if (targetm.addr_space.subset_p (as2, as1))
324 *common = as1;
325 return true;
327 else
328 return false;
331 /* Return a variant of TYPE which has all the type qualifiers of LIKE
332 as well as those of TYPE. */
334 static tree
335 qualify_type (tree type, tree like)
337 tree result_type;
338 addr_space_t as_type = TYPE_ADDR_SPACE (type);
339 addr_space_t as_like = TYPE_ADDR_SPACE (like);
340 addr_space_t as_common;
341 int result_quals;
342 tree result_block_factor = NULL_TREE;
344 gcc_assert (type && TREE_CODE (type) != ARRAY_TYPE);
345 gcc_assert (like && TREE_CODE (like) != ARRAY_TYPE);
347 /* If the two named address spaces are different, determine the common
348 superset address space. If there isn't one, raise an error. */
349 if (!addr_space_superset (as_type, as_like, &as_common))
351 as_common = as_type;
352 error ("%qT and %qT are in disjoint named address spaces",
353 type, like);
356 result_quals = TYPE_QUALS_NO_ADDR_SPACE (type)
357 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
358 | ENCODE_QUAL_ADDR_SPACE (as_common);
360 if (result_quals & TYPE_QUAL_UPC_SHARED)
362 tree b1 = TYPE_UPC_BLOCK_FACTOR (type);
363 tree b2 = TYPE_UPC_BLOCK_FACTOR (like);
364 /* We can merge in a new UPC blocking factor only
365 if one/other is NULL. Otherwise, they must match. */
366 if (b1 != b2)
368 if (b1 && !b2)
369 result_block_factor = b1;
370 else if (!b1 && b2)
371 result_block_factor = b2;
372 else
373 gcc_unreachable ();
377 result_type = c_build_qualified_type_1 (type, result_quals,
378 result_block_factor);
380 return result_type;
383 /* Return true iff the given tree T is a variable length array. */
385 bool
386 c_vla_type_p (const_tree t)
388 if (TREE_CODE (t) == ARRAY_TYPE
389 && C_TYPE_VARIABLE_SIZE (t))
390 return true;
391 return false;
394 /* Return the composite type of two compatible types.
396 We assume that comptypes has already been done and returned
397 nonzero; if that isn't so, this may crash. In particular, we
398 assume that qualifiers match. */
400 tree
401 composite_type (tree t1, tree t2)
403 enum tree_code code1;
404 enum tree_code code2;
405 tree attributes;
407 /* Save time if the two types are the same. */
409 if (t1 == t2) return t1;
411 /* If one type is nonsense, use the other. */
412 if (t1 == error_mark_node)
413 return t2;
414 if (t2 == error_mark_node)
415 return t1;
417 code1 = TREE_CODE (t1);
418 code2 = TREE_CODE (t2);
420 /* Merge the attributes. */
421 attributes = targetm.merge_type_attributes (t1, t2);
423 /* If one is an enumerated type and the other is the compatible
424 integer type, the composite type might be either of the two
425 (DR#013 question 3). For consistency, use the enumerated type as
426 the composite type. */
428 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
429 return t1;
430 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
431 return t2;
433 gcc_assert (code1 == code2);
435 switch (code1)
437 case POINTER_TYPE:
438 /* For two pointers, do this recursively on the target type. */
440 tree pointed_to_1 = TREE_TYPE (t1);
441 tree pointed_to_2 = TREE_TYPE (t2);
442 tree target = composite_type (pointed_to_1, pointed_to_2);
443 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
444 t1 = build_type_attribute_variant (t1, attributes);
445 return qualify_type (t1, t2);
448 case ARRAY_TYPE:
450 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
451 int quals;
452 tree unqual_elt;
453 tree d1 = TYPE_DOMAIN (t1);
454 tree d2 = TYPE_DOMAIN (t2);
455 bool d1_variable, d2_variable;
456 bool d1_zero, d2_zero;
457 bool t1_complete, t2_complete;
459 /* We should not have any type quals on arrays at all. */
460 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
461 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
463 t1_complete = COMPLETE_TYPE_P (t1);
464 t2_complete = COMPLETE_TYPE_P (t2);
466 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
467 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
469 d1_variable = (!d1_zero
470 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
471 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
472 d2_variable = (!d2_zero
473 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
474 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
475 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
476 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
478 /* Save space: see if the result is identical to one of the args. */
479 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
480 && (d2_variable || d2_zero || !d1_variable))
481 return build_type_attribute_variant (t1, attributes);
482 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
483 && (d1_variable || d1_zero || !d2_variable))
484 return build_type_attribute_variant (t2, attributes);
486 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
487 return build_type_attribute_variant (t1, attributes);
488 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
489 return build_type_attribute_variant (t2, attributes);
491 /* Merge the element types, and have a size if either arg has
492 one. We may have qualifiers on the element types. To set
493 up TYPE_MAIN_VARIANT correctly, we need to form the
494 composite of the unqualified types and add the qualifiers
495 back at the end. */
496 quals = TYPE_QUALS (strip_array_types (elt));
497 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
498 t1 = build_array_type (unqual_elt,
499 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
500 && (d2_variable
501 || d2_zero
502 || !d1_variable))
503 ? t1
504 : t2));
505 /* Ensure a composite type involving a zero-length array type
506 is a zero-length type not an incomplete type. */
507 if (d1_zero && d2_zero
508 && (t1_complete || t2_complete)
509 && !COMPLETE_TYPE_P (t1))
511 TYPE_SIZE (t1) = bitsize_zero_node;
512 TYPE_SIZE_UNIT (t1) = size_zero_node;
514 t1 = c_build_qualified_type (t1, quals);
515 return build_type_attribute_variant (t1, attributes);
518 case ENUMERAL_TYPE:
519 case RECORD_TYPE:
520 case UNION_TYPE:
521 if (attributes != NULL)
523 /* Try harder not to create a new aggregate type. */
524 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
525 return t1;
526 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
527 return t2;
529 return build_type_attribute_variant (t1, attributes);
531 case FUNCTION_TYPE:
532 /* Function types: prefer the one that specified arg types.
533 If both do, merge the arg types. Also merge the return types. */
535 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
536 tree p1 = TYPE_ARG_TYPES (t1);
537 tree p2 = TYPE_ARG_TYPES (t2);
538 int len;
539 tree newargs, n;
540 int i;
542 /* Save space: see if the result is identical to one of the args. */
543 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
544 return build_type_attribute_variant (t1, attributes);
545 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
546 return build_type_attribute_variant (t2, attributes);
548 /* Simple way if one arg fails to specify argument types. */
549 if (TYPE_ARG_TYPES (t1) == 0)
551 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
552 t1 = build_type_attribute_variant (t1, attributes);
553 return qualify_type (t1, t2);
555 if (TYPE_ARG_TYPES (t2) == 0)
557 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
558 t1 = build_type_attribute_variant (t1, attributes);
559 return qualify_type (t1, t2);
562 /* If both args specify argument types, we must merge the two
563 lists, argument by argument. */
565 len = list_length (p1);
566 newargs = 0;
568 for (i = 0; i < len; i++)
569 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
571 n = newargs;
573 for (; p1;
574 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
576 /* A null type means arg type is not specified.
577 Take whatever the other function type has. */
578 if (TREE_VALUE (p1) == 0)
580 TREE_VALUE (n) = TREE_VALUE (p2);
581 goto parm_done;
583 if (TREE_VALUE (p2) == 0)
585 TREE_VALUE (n) = TREE_VALUE (p1);
586 goto parm_done;
589 /* Given wait (union {union wait *u; int *i} *)
590 and wait (union wait *),
591 prefer union wait * as type of parm. */
592 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
593 && TREE_VALUE (p1) != TREE_VALUE (p2))
595 tree memb;
596 tree mv2 = TREE_VALUE (p2);
597 if (mv2 && mv2 != error_mark_node
598 && TREE_CODE (mv2) != ARRAY_TYPE)
599 mv2 = TYPE_MAIN_VARIANT (mv2);
600 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
601 memb; memb = DECL_CHAIN (memb))
603 tree mv3 = TREE_TYPE (memb);
604 if (mv3 && mv3 != error_mark_node
605 && TREE_CODE (mv3) != ARRAY_TYPE)
606 mv3 = TYPE_MAIN_VARIANT (mv3);
607 if (comptypes (mv3, mv2))
609 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
610 TREE_VALUE (p2));
611 pedwarn (input_location, OPT_Wpedantic,
612 "function types not truly compatible in ISO C");
613 goto parm_done;
617 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
618 && TREE_VALUE (p2) != TREE_VALUE (p1))
620 tree memb;
621 tree mv1 = TREE_VALUE (p1);
622 if (mv1 && mv1 != error_mark_node
623 && TREE_CODE (mv1) != ARRAY_TYPE)
624 mv1 = TYPE_MAIN_VARIANT (mv1);
625 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
626 memb; memb = DECL_CHAIN (memb))
628 tree mv3 = TREE_TYPE (memb);
629 if (mv3 && mv3 != error_mark_node
630 && TREE_CODE (mv3) != ARRAY_TYPE)
631 mv3 = TYPE_MAIN_VARIANT (mv3);
632 if (comptypes (mv3, mv1))
634 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
635 TREE_VALUE (p1));
636 pedwarn (input_location, OPT_Wpedantic,
637 "function types not truly compatible in ISO C");
638 goto parm_done;
642 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
643 parm_done: ;
646 t1 = build_function_type (valtype, newargs);
647 t1 = qualify_type (t1, t2);
648 /* ... falls through ... */
651 default:
652 return build_type_attribute_variant (t1, attributes);
657 /* Return the type of a conditional expression between pointers to
658 possibly differently qualified versions of compatible types.
660 We assume that comp_target_types has already been done and returned
661 nonzero; if that isn't so, this may crash. */
663 static tree
664 common_pointer_type (tree t1, tree t2)
666 tree attributes;
667 tree pointed_to_1, mv1;
668 tree pointed_to_2, mv2;
669 tree target;
670 unsigned target_quals;
671 tree target_block_factor = NULL_TREE;
672 addr_space_t as1, as2, as_common;
673 int quals1, quals2;
675 /* Save time if the two types are the same. */
677 if (t1 == t2) return t1;
679 /* If one type is nonsense, use the other. */
680 if (t1 == error_mark_node)
681 return t2;
682 if (t2 == error_mark_node)
683 return t1;
685 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
686 && TREE_CODE (t2) == POINTER_TYPE);
688 /* Merge the attributes. */
689 attributes = targetm.merge_type_attributes (t1, t2);
691 /* Find the composite type of the target types, and combine the
692 qualifiers of the two types' targets. Do not lose qualifiers on
693 array element types by taking the TYPE_MAIN_VARIANT. */
694 mv1 = pointed_to_1 = TREE_TYPE (t1);
695 mv2 = pointed_to_2 = TREE_TYPE (t2);
696 if (TREE_CODE (mv1) != ARRAY_TYPE)
697 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
698 if (TREE_CODE (mv2) != ARRAY_TYPE)
699 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
700 target = composite_type (mv1, mv2);
702 /* For function types do not merge const qualifiers, but drop them
703 if used inconsistently. The middle-end uses these to mark const
704 and noreturn functions. */
705 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
706 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
708 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
709 target_quals = (quals1 & quals2);
710 else
711 target_quals = (quals1 | quals2);
713 if (target_quals & TYPE_QUAL_UPC_SHARED)
714 target_block_factor = TYPE_UPC_BLOCK_FACTOR (
715 strip_array_types (pointed_to_1));
717 /* If the two named address spaces are different, determine the common
718 superset address space. This is guaranteed to exist due to the
719 assumption that comp_target_type returned non-zero. */
720 as1 = TYPE_ADDR_SPACE (pointed_to_1);
721 as2 = TYPE_ADDR_SPACE (pointed_to_2);
722 if (!addr_space_superset (as1, as2, &as_common))
723 gcc_unreachable ();
725 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
726 t2 = c_build_qualified_type_1 (target, target_quals, target_block_factor);
728 t1 = build_pointer_type (t2);
729 return build_type_attribute_variant (t1, attributes);
732 /* Return the common type for two arithmetic types under the usual
733 arithmetic conversions. The default conversions have already been
734 applied, and enumerated types converted to their compatible integer
735 types. The resulting type is unqualified and has no attributes.
737 This is the type for the result of most arithmetic operations
738 if the operands have the given two types. */
740 static tree
741 c_common_type (tree t1, tree t2)
743 enum tree_code code1;
744 enum tree_code code2;
746 /* If one type is nonsense, use the other. */
747 if (t1 == error_mark_node)
748 return t2;
749 if (t2 == error_mark_node)
750 return t1;
752 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
753 t1 = TYPE_MAIN_VARIANT (t1);
755 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
756 t2 = TYPE_MAIN_VARIANT (t2);
758 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
759 t1 = build_type_attribute_variant (t1, NULL_TREE);
761 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
762 t2 = build_type_attribute_variant (t2, NULL_TREE);
764 /* Save time if the two types are the same. */
766 if (t1 == t2) return t1;
768 code1 = TREE_CODE (t1);
769 code2 = TREE_CODE (t2);
771 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
772 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
773 || code1 == INTEGER_TYPE);
774 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
775 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
776 || code2 == INTEGER_TYPE);
778 /* When one operand is a decimal float type, the other operand cannot be
779 a generic float type or a complex type. We also disallow vector types
780 here. */
781 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
782 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
784 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
786 error ("can%'t mix operands of decimal float and vector types");
787 return error_mark_node;
789 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
791 error ("can%'t mix operands of decimal float and complex types");
792 return error_mark_node;
794 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
796 error ("can%'t mix operands of decimal float and other float types");
797 return error_mark_node;
801 /* If one type is a vector type, return that type. (How the usual
802 arithmetic conversions apply to the vector types extension is not
803 precisely specified.) */
804 if (code1 == VECTOR_TYPE)
805 return t1;
807 if (code2 == VECTOR_TYPE)
808 return t2;
810 /* If one type is complex, form the common type of the non-complex
811 components, then make that complex. Use T1 or T2 if it is the
812 required type. */
813 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
815 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
816 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
817 tree subtype = c_common_type (subtype1, subtype2);
819 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
820 return t1;
821 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
822 return t2;
823 else
824 return build_complex_type (subtype);
827 /* If only one is real, use it as the result. */
829 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
830 return t1;
832 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
833 return t2;
835 /* If both are real and either are decimal floating point types, use
836 the decimal floating point type with the greater precision. */
838 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
840 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
841 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
842 return dfloat128_type_node;
843 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
844 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
845 return dfloat64_type_node;
846 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
847 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
848 return dfloat32_type_node;
851 /* Deal with fixed-point types. */
852 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
854 unsigned int unsignedp = 0, satp = 0;
855 enum machine_mode m1, m2;
856 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
858 m1 = TYPE_MODE (t1);
859 m2 = TYPE_MODE (t2);
861 /* If one input type is saturating, the result type is saturating. */
862 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
863 satp = 1;
865 /* If both fixed-point types are unsigned, the result type is unsigned.
866 When mixing fixed-point and integer types, follow the sign of the
867 fixed-point type.
868 Otherwise, the result type is signed. */
869 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
870 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
871 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
872 && TYPE_UNSIGNED (t1))
873 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
874 && TYPE_UNSIGNED (t2)))
875 unsignedp = 1;
877 /* The result type is signed. */
878 if (unsignedp == 0)
880 /* If the input type is unsigned, we need to convert to the
881 signed type. */
882 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
884 enum mode_class mclass = (enum mode_class) 0;
885 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
886 mclass = MODE_FRACT;
887 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
888 mclass = MODE_ACCUM;
889 else
890 gcc_unreachable ();
891 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
893 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
895 enum mode_class mclass = (enum mode_class) 0;
896 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
897 mclass = MODE_FRACT;
898 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
899 mclass = MODE_ACCUM;
900 else
901 gcc_unreachable ();
902 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
906 if (code1 == FIXED_POINT_TYPE)
908 fbit1 = GET_MODE_FBIT (m1);
909 ibit1 = GET_MODE_IBIT (m1);
911 else
913 fbit1 = 0;
914 /* Signed integers need to subtract one sign bit. */
915 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
918 if (code2 == FIXED_POINT_TYPE)
920 fbit2 = GET_MODE_FBIT (m2);
921 ibit2 = GET_MODE_IBIT (m2);
923 else
925 fbit2 = 0;
926 /* Signed integers need to subtract one sign bit. */
927 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
930 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
931 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
932 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
933 satp);
936 /* Both real or both integers; use the one with greater precision. */
938 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
939 return t1;
940 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
941 return t2;
943 /* Same precision. Prefer long longs to longs to ints when the
944 same precision, following the C99 rules on integer type rank
945 (which are equivalent to the C90 rules for C90 types). */
947 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
948 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
949 return long_long_unsigned_type_node;
951 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
952 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
954 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
955 return long_long_unsigned_type_node;
956 else
957 return long_long_integer_type_node;
960 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
961 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
962 return long_unsigned_type_node;
964 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
965 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
967 /* But preserve unsignedness from the other type,
968 since long cannot hold all the values of an unsigned int. */
969 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
970 return long_unsigned_type_node;
971 else
972 return long_integer_type_node;
975 /* Likewise, prefer long double to double even if same size. */
976 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
977 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
978 return long_double_type_node;
980 /* Likewise, prefer double to float even if same size.
981 We got a couple of embedded targets with 32 bit doubles, and the
982 pdp11 might have 64 bit floats. */
983 if (TYPE_MAIN_VARIANT (t1) == double_type_node
984 || TYPE_MAIN_VARIANT (t2) == double_type_node)
985 return double_type_node;
987 /* Otherwise prefer the unsigned one. */
989 if (TYPE_UNSIGNED (t1))
990 return t1;
991 else
992 return t2;
995 /* Wrapper around c_common_type that is used by c-common.c and other
996 front end optimizations that remove promotions. ENUMERAL_TYPEs
997 are allowed here and are converted to their compatible integer types.
998 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
999 preferably a non-Boolean type as the common type. */
1000 tree
1001 common_type (tree t1, tree t2)
1003 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1004 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
1005 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1006 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
1008 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1009 if (TREE_CODE (t1) == BOOLEAN_TYPE
1010 && TREE_CODE (t2) == BOOLEAN_TYPE)
1011 return boolean_type_node;
1013 /* If either type is BOOLEAN_TYPE, then return the other. */
1014 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1015 return t2;
1016 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1017 return t1;
1019 return c_common_type (t1, t2);
1022 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1023 or various other operations. Return 2 if they are compatible
1024 but a warning may be needed if you use them together. */
1027 comptypes (tree type1, tree type2)
1029 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1030 int val;
1032 val = comptypes_internal (type1, type2, NULL, NULL);
1033 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1035 return val;
1038 /* Like comptypes, but if it returns non-zero because enum and int are
1039 compatible, it sets *ENUM_AND_INT_P to true. */
1041 static int
1042 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1044 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1045 int val;
1047 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1048 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1050 return val;
1053 /* Like comptypes, but if it returns nonzero for different types, it
1054 sets *DIFFERENT_TYPES_P to true. */
1057 comptypes_check_different_types (tree type1, tree type2,
1058 bool *different_types_p)
1060 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1061 int val;
1063 val = comptypes_internal (type1, type2, NULL, different_types_p);
1064 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1066 return val;
1069 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1070 or various other operations. Return 2 if they are compatible
1071 but a warning may be needed if you use them together. If
1072 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1073 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1074 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1075 NULL, and the types are compatible but different enough not to be
1076 permitted in C11 typedef redeclarations, then this sets
1077 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1078 false, but may or may not be set if the types are incompatible.
1079 This differs from comptypes, in that we don't free the seen
1080 types. */
1082 static int
1083 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1084 bool *different_types_p)
1086 const_tree t1 = type1;
1087 const_tree t2 = type2;
1088 int attrval, val;
1090 /* Suppress errors caused by previously reported errors. */
1092 if (t1 == t2 || !t1 || !t2
1093 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1094 return 1;
1096 /* Enumerated types are compatible with integer types, but this is
1097 not transitive: two enumerated types in the same translation unit
1098 are compatible with each other only if they are the same type. */
1100 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1102 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1103 if (TREE_CODE (t2) != VOID_TYPE)
1105 if (enum_and_int_p != NULL)
1106 *enum_and_int_p = true;
1107 if (different_types_p != NULL)
1108 *different_types_p = true;
1111 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1113 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1114 if (TREE_CODE (t1) != VOID_TYPE)
1116 if (enum_and_int_p != NULL)
1117 *enum_and_int_p = true;
1118 if (different_types_p != NULL)
1119 *different_types_p = true;
1123 if (t1 == t2)
1124 return 1;
1126 /* Different classes of types can't be compatible. */
1128 if (TREE_CODE (t1) != TREE_CODE (t2))
1129 return 0;
1131 /* Qualifiers must match. C99 6.7.3p9 */
1133 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1134 return 0;
1136 /* If the type is UPC qualified, the block sizes have
1137 to be equal. The block sizes are either NULL
1138 or are the same integer constant. */
1139 if ((TYPE_QUALS (t1) & TYPE_QUAL_UPC_SHARED)
1140 && (TYPE_UPC_BLOCK_FACTOR (t1) != TYPE_UPC_BLOCK_FACTOR (t2)))
1141 return 0;
1143 /* Allow for two different type nodes which have essentially the same
1144 definition. Note that we already checked for equality of the type
1145 qualifiers (just above). */
1147 if (TREE_CODE (t1) != ARRAY_TYPE
1148 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1149 return 1;
1151 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1152 if (!(attrval = comp_type_attributes (t1, t2)))
1153 return 0;
1155 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1156 val = 0;
1158 switch (TREE_CODE (t1))
1160 case POINTER_TYPE:
1161 /* Do not remove mode or aliasing information. */
1162 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1163 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1164 break;
1165 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1166 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1167 enum_and_int_p, different_types_p));
1168 break;
1170 case FUNCTION_TYPE:
1171 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1172 different_types_p);
1173 break;
1175 case ARRAY_TYPE:
1177 tree d1 = TYPE_DOMAIN (t1);
1178 tree d2 = TYPE_DOMAIN (t2);
1179 bool d1_variable, d2_variable;
1180 bool d1_zero, d2_zero;
1181 val = 1;
1183 /* Target types must match incl. qualifiers. */
1184 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1185 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1186 enum_and_int_p,
1187 different_types_p)))
1188 return 0;
1190 if (different_types_p != NULL
1191 && (d1 == 0) != (d2 == 0))
1192 *different_types_p = true;
1193 /* Sizes must match unless one is missing or variable. */
1194 if (d1 == 0 || d2 == 0 || d1 == d2)
1195 break;
1197 d1_zero = !TYPE_MAX_VALUE (d1);
1198 d2_zero = !TYPE_MAX_VALUE (d2);
1200 d1_variable = (!d1_zero
1201 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1202 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1203 d2_variable = (!d2_zero
1204 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1205 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1206 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1207 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1209 if (different_types_p != NULL
1210 && d1_variable != d2_variable)
1211 *different_types_p = true;
1212 if (d1_variable || d2_variable)
1213 break;
1214 if (d1_zero && d2_zero)
1215 break;
1216 if (d1_zero || d2_zero
1217 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1218 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1219 val = 0;
1221 break;
1224 case ENUMERAL_TYPE:
1225 case RECORD_TYPE:
1226 case UNION_TYPE:
1227 if (val != 1 && !same_translation_unit_p (t1, t2))
1229 tree a1 = TYPE_ATTRIBUTES (t1);
1230 tree a2 = TYPE_ATTRIBUTES (t2);
1232 if (! attribute_list_contained (a1, a2)
1233 && ! attribute_list_contained (a2, a1))
1234 break;
1236 if (attrval != 2)
1237 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1238 different_types_p);
1239 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1240 different_types_p);
1242 break;
1244 case VECTOR_TYPE:
1245 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1246 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1247 enum_and_int_p, different_types_p));
1248 break;
1250 default:
1251 break;
1253 return attrval == 2 && val == 1 ? 2 : val;
1256 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1257 their qualifiers, except for named address spaces. If the pointers point to
1258 different named addresses, then we must determine if one address space is a
1259 subset of the other. */
1261 static int
1262 comp_target_types (location_t location, tree ttl, tree ttr)
1264 int val;
1265 tree mvl = TREE_TYPE (ttl);
1266 tree mvr = TREE_TYPE (ttr);
1267 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1268 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1269 addr_space_t as_common;
1270 bool enum_and_int_p;
1272 /* Fail if pointers point to incompatible address spaces. */
1273 if (!addr_space_superset (asl, asr, &as_common))
1274 return 0;
1276 /* Do not lose qualifiers on element types of array types that are
1277 pointer targets by taking their TYPE_MAIN_VARIANT. */
1278 if (TREE_CODE (mvl) != ARRAY_TYPE)
1279 mvl = (TYPE_ATOMIC (mvl)
1280 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1281 : TYPE_MAIN_VARIANT (mvl));
1282 if (TREE_CODE (mvr) != ARRAY_TYPE)
1283 mvr = (TYPE_ATOMIC (mvr)
1284 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1285 : TYPE_MAIN_VARIANT (mvr));
1286 enum_and_int_p = false;
1287 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1289 if (val == 2)
1290 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1292 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1293 warning_at (location, OPT_Wc___compat,
1294 "pointer target types incompatible in C++");
1296 return val;
1299 /* Subroutines of `comptypes'. */
1301 /* Determine whether two trees derive from the same translation unit.
1302 If the CONTEXT chain ends in a null, that tree's context is still
1303 being parsed, so if two trees have context chains ending in null,
1304 they're in the same translation unit. */
1306 same_translation_unit_p (const_tree t1, const_tree t2)
1308 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1309 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1311 case tcc_declaration:
1312 t1 = DECL_CONTEXT (t1); break;
1313 case tcc_type:
1314 t1 = TYPE_CONTEXT (t1); break;
1315 case tcc_exceptional:
1316 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1317 default: gcc_unreachable ();
1320 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1321 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1323 case tcc_declaration:
1324 t2 = DECL_CONTEXT (t2); break;
1325 case tcc_type:
1326 t2 = TYPE_CONTEXT (t2); break;
1327 case tcc_exceptional:
1328 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1329 default: gcc_unreachable ();
1332 return t1 == t2;
1335 /* Allocate the seen two types, assuming that they are compatible. */
1337 static struct tagged_tu_seen_cache *
1338 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1340 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1341 tu->next = tagged_tu_seen_base;
1342 tu->t1 = t1;
1343 tu->t2 = t2;
1345 tagged_tu_seen_base = tu;
1347 /* The C standard says that two structures in different translation
1348 units are compatible with each other only if the types of their
1349 fields are compatible (among other things). We assume that they
1350 are compatible until proven otherwise when building the cache.
1351 An example where this can occur is:
1352 struct a
1354 struct a *next;
1356 If we are comparing this against a similar struct in another TU,
1357 and did not assume they were compatible, we end up with an infinite
1358 loop. */
1359 tu->val = 1;
1360 return tu;
1363 /* Free the seen types until we get to TU_TIL. */
1365 static void
1366 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1368 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1369 while (tu != tu_til)
1371 const struct tagged_tu_seen_cache *const tu1
1372 = (const struct tagged_tu_seen_cache *) tu;
1373 tu = tu1->next;
1374 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1376 tagged_tu_seen_base = tu_til;
1379 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1380 compatible. If the two types are not the same (which has been
1381 checked earlier), this can only happen when multiple translation
1382 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1383 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1384 comptypes_internal. */
1386 static int
1387 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1388 bool *enum_and_int_p, bool *different_types_p)
1390 tree s1, s2;
1391 bool needs_warning = false;
1393 /* We have to verify that the tags of the types are the same. This
1394 is harder than it looks because this may be a typedef, so we have
1395 to go look at the original type. It may even be a typedef of a
1396 typedef...
1397 In the case of compiler-created builtin structs the TYPE_DECL
1398 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1399 while (TYPE_NAME (t1)
1400 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1401 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1402 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1404 while (TYPE_NAME (t2)
1405 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1406 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1407 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1409 /* C90 didn't have the requirement that the two tags be the same. */
1410 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1411 return 0;
1413 /* C90 didn't say what happened if one or both of the types were
1414 incomplete; we choose to follow C99 rules here, which is that they
1415 are compatible. */
1416 if (TYPE_SIZE (t1) == NULL
1417 || TYPE_SIZE (t2) == NULL)
1418 return 1;
1421 const struct tagged_tu_seen_cache * tts_i;
1422 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1423 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1424 return tts_i->val;
1427 switch (TREE_CODE (t1))
1429 case ENUMERAL_TYPE:
1431 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1432 /* Speed up the case where the type values are in the same order. */
1433 tree tv1 = TYPE_VALUES (t1);
1434 tree tv2 = TYPE_VALUES (t2);
1436 if (tv1 == tv2)
1438 return 1;
1441 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1443 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1444 break;
1445 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1447 tu->val = 0;
1448 return 0;
1452 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1454 return 1;
1456 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1458 tu->val = 0;
1459 return 0;
1462 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1464 tu->val = 0;
1465 return 0;
1468 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1470 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1471 if (s2 == NULL
1472 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1474 tu->val = 0;
1475 return 0;
1478 return 1;
1481 case UNION_TYPE:
1483 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1484 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1486 tu->val = 0;
1487 return 0;
1490 /* Speed up the common case where the fields are in the same order. */
1491 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1492 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1494 int result;
1496 if (DECL_NAME (s1) != DECL_NAME (s2))
1497 break;
1498 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1499 enum_and_int_p, different_types_p);
1501 if (result != 1 && !DECL_NAME (s1))
1502 break;
1503 if (result == 0)
1505 tu->val = 0;
1506 return 0;
1508 if (result == 2)
1509 needs_warning = true;
1511 if (TREE_CODE (s1) == FIELD_DECL
1512 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1513 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1515 tu->val = 0;
1516 return 0;
1519 if (!s1 && !s2)
1521 tu->val = needs_warning ? 2 : 1;
1522 return tu->val;
1525 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1527 bool ok = false;
1529 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1530 if (DECL_NAME (s1) == DECL_NAME (s2))
1532 int result;
1534 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1535 enum_and_int_p,
1536 different_types_p);
1538 if (result != 1 && !DECL_NAME (s1))
1539 continue;
1540 if (result == 0)
1542 tu->val = 0;
1543 return 0;
1545 if (result == 2)
1546 needs_warning = true;
1548 if (TREE_CODE (s1) == FIELD_DECL
1549 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1550 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1551 break;
1553 ok = true;
1554 break;
1556 if (!ok)
1558 tu->val = 0;
1559 return 0;
1562 tu->val = needs_warning ? 2 : 10;
1563 return tu->val;
1566 case RECORD_TYPE:
1568 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1570 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1571 s1 && s2;
1572 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1574 int result;
1575 if (TREE_CODE (s1) != TREE_CODE (s2)
1576 || DECL_NAME (s1) != DECL_NAME (s2))
1577 break;
1578 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1579 enum_and_int_p, different_types_p);
1580 if (result == 0)
1581 break;
1582 if (result == 2)
1583 needs_warning = true;
1585 if (TREE_CODE (s1) == FIELD_DECL
1586 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1587 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1588 break;
1590 if (s1 && s2)
1591 tu->val = 0;
1592 else
1593 tu->val = needs_warning ? 2 : 1;
1594 return tu->val;
1597 default:
1598 gcc_unreachable ();
1602 /* Return 1 if two function types F1 and F2 are compatible.
1603 If either type specifies no argument types,
1604 the other must specify a fixed number of self-promoting arg types.
1605 Otherwise, if one type specifies only the number of arguments,
1606 the other must specify that number of self-promoting arg types.
1607 Otherwise, the argument types must match.
1608 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1610 static int
1611 function_types_compatible_p (const_tree f1, const_tree f2,
1612 bool *enum_and_int_p, bool *different_types_p)
1614 tree args1, args2;
1615 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1616 int val = 1;
1617 int val1;
1618 tree ret1, ret2;
1620 ret1 = TREE_TYPE (f1);
1621 ret2 = TREE_TYPE (f2);
1623 /* 'volatile' qualifiers on a function's return type used to mean
1624 the function is noreturn. */
1625 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1626 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1627 if (TYPE_VOLATILE (ret1))
1628 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1629 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1630 if (TYPE_VOLATILE (ret2))
1631 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1632 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1633 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1634 if (val == 0)
1635 return 0;
1637 args1 = TYPE_ARG_TYPES (f1);
1638 args2 = TYPE_ARG_TYPES (f2);
1640 if (different_types_p != NULL
1641 && (args1 == 0) != (args2 == 0))
1642 *different_types_p = true;
1644 /* An unspecified parmlist matches any specified parmlist
1645 whose argument types don't need default promotions. */
1647 if (args1 == 0)
1649 if (!self_promoting_args_p (args2))
1650 return 0;
1651 /* If one of these types comes from a non-prototype fn definition,
1652 compare that with the other type's arglist.
1653 If they don't match, ask for a warning (but no error). */
1654 if (TYPE_ACTUAL_ARG_TYPES (f1)
1655 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1656 enum_and_int_p, different_types_p))
1657 val = 2;
1658 return val;
1660 if (args2 == 0)
1662 if (!self_promoting_args_p (args1))
1663 return 0;
1664 if (TYPE_ACTUAL_ARG_TYPES (f2)
1665 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1666 enum_and_int_p, different_types_p))
1667 val = 2;
1668 return val;
1671 /* Both types have argument lists: compare them and propagate results. */
1672 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1673 different_types_p);
1674 return val1 != 1 ? val1 : val;
1677 /* Check two lists of types for compatibility, returning 0 for
1678 incompatible, 1 for compatible, or 2 for compatible with
1679 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1680 comptypes_internal. */
1682 static int
1683 type_lists_compatible_p (const_tree args1, const_tree args2,
1684 bool *enum_and_int_p, bool *different_types_p)
1686 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1687 int val = 1;
1688 int newval = 0;
1690 while (1)
1692 tree a1, mv1, a2, mv2;
1693 if (args1 == 0 && args2 == 0)
1694 return val;
1695 /* If one list is shorter than the other,
1696 they fail to match. */
1697 if (args1 == 0 || args2 == 0)
1698 return 0;
1699 mv1 = a1 = TREE_VALUE (args1);
1700 mv2 = a2 = TREE_VALUE (args2);
1701 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1702 mv1 = (TYPE_ATOMIC (mv1)
1703 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1704 TYPE_QUAL_ATOMIC)
1705 : TYPE_MAIN_VARIANT (mv1));
1706 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1707 mv2 = (TYPE_ATOMIC (mv2)
1708 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1709 TYPE_QUAL_ATOMIC)
1710 : TYPE_MAIN_VARIANT (mv2));
1711 /* A null pointer instead of a type
1712 means there is supposed to be an argument
1713 but nothing is specified about what type it has.
1714 So match anything that self-promotes. */
1715 if (different_types_p != NULL
1716 && (a1 == 0) != (a2 == 0))
1717 *different_types_p = true;
1718 if (a1 == 0)
1720 if (c_type_promotes_to (a2) != a2)
1721 return 0;
1723 else if (a2 == 0)
1725 if (c_type_promotes_to (a1) != a1)
1726 return 0;
1728 /* If one of the lists has an error marker, ignore this arg. */
1729 else if (TREE_CODE (a1) == ERROR_MARK
1730 || TREE_CODE (a2) == ERROR_MARK)
1732 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1733 different_types_p)))
1735 if (different_types_p != NULL)
1736 *different_types_p = true;
1737 /* Allow wait (union {union wait *u; int *i} *)
1738 and wait (union wait *) to be compatible. */
1739 if (TREE_CODE (a1) == UNION_TYPE
1740 && (TYPE_NAME (a1) == 0
1741 || TYPE_TRANSPARENT_AGGR (a1))
1742 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1743 && tree_int_cst_equal (TYPE_SIZE (a1),
1744 TYPE_SIZE (a2)))
1746 tree memb;
1747 for (memb = TYPE_FIELDS (a1);
1748 memb; memb = DECL_CHAIN (memb))
1750 tree mv3 = TREE_TYPE (memb);
1751 if (mv3 && mv3 != error_mark_node
1752 && TREE_CODE (mv3) != ARRAY_TYPE)
1753 mv3 = (TYPE_ATOMIC (mv3)
1754 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1755 TYPE_QUAL_ATOMIC)
1756 : TYPE_MAIN_VARIANT (mv3));
1757 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1758 different_types_p))
1759 break;
1761 if (memb == 0)
1762 return 0;
1764 else if (TREE_CODE (a2) == UNION_TYPE
1765 && (TYPE_NAME (a2) == 0
1766 || TYPE_TRANSPARENT_AGGR (a2))
1767 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1768 && tree_int_cst_equal (TYPE_SIZE (a2),
1769 TYPE_SIZE (a1)))
1771 tree memb;
1772 for (memb = TYPE_FIELDS (a2);
1773 memb; memb = DECL_CHAIN (memb))
1775 tree mv3 = TREE_TYPE (memb);
1776 if (mv3 && mv3 != error_mark_node
1777 && TREE_CODE (mv3) != ARRAY_TYPE)
1778 mv3 = (TYPE_ATOMIC (mv3)
1779 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1780 TYPE_QUAL_ATOMIC)
1781 : TYPE_MAIN_VARIANT (mv3));
1782 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1783 different_types_p))
1784 break;
1786 if (memb == 0)
1787 return 0;
1789 else
1790 return 0;
1793 /* comptypes said ok, but record if it said to warn. */
1794 if (newval > val)
1795 val = newval;
1797 args1 = TREE_CHAIN (args1);
1798 args2 = TREE_CHAIN (args2);
1802 /* Compute the size to increment a pointer by. When a function type or void
1803 type or incomplete type is passed, size_one_node is returned.
1804 This function does not emit any diagnostics; the caller is responsible
1805 for that. */
1807 static tree
1808 c_size_in_bytes (const_tree type)
1810 enum tree_code code = TREE_CODE (type);
1812 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1813 || !COMPLETE_TYPE_P (type))
1814 return size_one_node;
1816 /* Convert in case a char is more than one unit. */
1817 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1818 size_int (TYPE_PRECISION (char_type_node)
1819 / BITS_PER_UNIT));
1822 /* Return either DECL or its known constant value (if it has one). */
1824 tree
1825 decl_constant_value (tree decl)
1827 if (/* Don't change a variable array bound or initial value to a constant
1828 in a place where a variable is invalid. Note that DECL_INITIAL
1829 isn't valid for a PARM_DECL. */
1830 current_function_decl != 0
1831 && TREE_CODE (decl) != PARM_DECL
1832 && !TREE_THIS_VOLATILE (decl)
1833 && TREE_READONLY (decl)
1834 && DECL_INITIAL (decl) != 0
1835 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1836 /* This is invalid if initial value is not constant.
1837 If it has either a function call, a memory reference,
1838 or a variable, then re-evaluating it could give different results. */
1839 && TREE_CONSTANT (DECL_INITIAL (decl))
1840 /* Check for cases where this is sub-optimal, even though valid. */
1841 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1842 return DECL_INITIAL (decl);
1843 return decl;
1846 /* Convert the array expression EXP to a pointer. */
1847 static tree
1848 array_to_pointer_conversion (location_t loc, tree exp)
1850 tree orig_exp = exp;
1851 tree type = TREE_TYPE (exp);
1852 tree adr;
1853 tree restype = TREE_TYPE (type);
1854 tree ptrtype;
1856 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1858 STRIP_TYPE_NOPS (exp);
1860 if (TREE_NO_WARNING (orig_exp))
1861 TREE_NO_WARNING (exp) = 1;
1863 ptrtype = build_pointer_type (restype);
1865 if (TREE_CODE (exp) == INDIRECT_REF)
1866 return convert (ptrtype, TREE_OPERAND (exp, 0));
1868 /* In C++ array compound literals are temporary objects unless they are
1869 const or appear in namespace scope, so they are destroyed too soon
1870 to use them for much of anything (c++/53220). */
1871 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1873 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1874 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1875 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1876 "converting an array compound literal to a pointer "
1877 "is ill-formed in C++");
1880 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1881 return convert (ptrtype, adr);
1884 /* Convert the function expression EXP to a pointer. */
1885 static tree
1886 function_to_pointer_conversion (location_t loc, tree exp)
1888 tree orig_exp = exp;
1890 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1892 STRIP_TYPE_NOPS (exp);
1894 if (TREE_NO_WARNING (orig_exp))
1895 TREE_NO_WARNING (exp) = 1;
1897 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1900 /* Mark EXP as read, not just set, for set but not used -Wunused
1901 warning purposes. */
1903 void
1904 mark_exp_read (tree exp)
1906 switch (TREE_CODE (exp))
1908 case VAR_DECL:
1909 case PARM_DECL:
1910 DECL_READ_P (exp) = 1;
1911 break;
1912 case ARRAY_REF:
1913 case COMPONENT_REF:
1914 case MODIFY_EXPR:
1915 case REALPART_EXPR:
1916 case IMAGPART_EXPR:
1917 CASE_CONVERT:
1918 case ADDR_EXPR:
1919 mark_exp_read (TREE_OPERAND (exp, 0));
1920 break;
1921 case COMPOUND_EXPR:
1922 case C_MAYBE_CONST_EXPR:
1923 mark_exp_read (TREE_OPERAND (exp, 1));
1924 break;
1925 default:
1926 break;
1930 /* Perform the default conversion of arrays and functions to pointers.
1931 Return the result of converting EXP. For any other expression, just
1932 return EXP.
1934 LOC is the location of the expression. */
1936 struct c_expr
1937 default_function_array_conversion (location_t loc, struct c_expr exp)
1939 tree orig_exp = exp.value;
1940 tree type = TREE_TYPE (exp.value);
1941 enum tree_code code = TREE_CODE (type);
1943 switch (code)
1945 case ARRAY_TYPE:
1947 bool not_lvalue = false;
1948 bool lvalue_array_p;
1950 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1951 || CONVERT_EXPR_P (exp.value))
1952 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1954 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1955 not_lvalue = true;
1956 exp.value = TREE_OPERAND (exp.value, 0);
1959 if (TREE_NO_WARNING (orig_exp))
1960 TREE_NO_WARNING (exp.value) = 1;
1962 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1963 if (!flag_isoc99 && !lvalue_array_p)
1965 /* Before C99, non-lvalue arrays do not decay to pointers.
1966 Normally, using such an array would be invalid; but it can
1967 be used correctly inside sizeof or as a statement expression.
1968 Thus, do not give an error here; an error will result later. */
1969 return exp;
1972 exp.value = array_to_pointer_conversion (loc, exp.value);
1974 break;
1975 case FUNCTION_TYPE:
1976 exp.value = function_to_pointer_conversion (loc, exp.value);
1977 break;
1978 default:
1979 break;
1982 return exp;
1985 struct c_expr
1986 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1988 mark_exp_read (exp.value);
1989 return default_function_array_conversion (loc, exp);
1992 /* Return whether EXPR should be treated as an atomic lvalue for the
1993 purposes of load and store handling. */
1995 static bool
1996 really_atomic_lvalue (tree expr)
1998 if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node)
1999 return false;
2000 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2001 return false;
2002 if (!lvalue_p (expr))
2003 return false;
2005 /* Ignore _Atomic on register variables, since their addresses can't
2006 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2007 sequences wouldn't work. Ignore _Atomic on structures containing
2008 bit-fields, since accessing elements of atomic structures or
2009 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2010 it's undefined at translation time or execution time, and the
2011 normal atomic sequences again wouldn't work. */
2012 while (handled_component_p (expr))
2014 if (TREE_CODE (expr) == COMPONENT_REF
2015 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2016 return false;
2017 expr = TREE_OPERAND (expr, 0);
2019 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2020 return false;
2021 return true;
2024 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2025 including converting functions and arrays to pointers if CONVERT_P.
2026 If READ_P, also mark the expression as having been read. */
2028 struct c_expr
2029 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2030 bool convert_p, bool read_p)
2032 if (read_p)
2033 mark_exp_read (exp.value);
2034 if (convert_p)
2035 exp = default_function_array_conversion (loc, exp);
2036 if (really_atomic_lvalue (exp.value))
2038 vec<tree, va_gc> *params;
2039 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2040 tree expr_type = TREE_TYPE (exp.value);
2041 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2042 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2044 gcc_assert (TYPE_ATOMIC (expr_type));
2046 /* Expansion of a generic atomic load may require an addition
2047 element, so allocate enough to prevent a resize. */
2048 vec_alloc (params, 4);
2050 /* Remove the qualifiers for the rest of the expressions and
2051 create the VAL temp variable to hold the RHS. */
2052 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2053 tmp = create_tmp_var (nonatomic_type, NULL);
2054 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2055 TREE_ADDRESSABLE (tmp) = 1;
2056 TREE_NO_WARNING (tmp) = 1;
2058 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2059 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2060 params->quick_push (expr_addr);
2061 params->quick_push (tmp_addr);
2062 params->quick_push (seq_cst);
2063 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2065 /* EXPR is always read. */
2066 mark_exp_read (exp.value);
2068 /* Return tmp which contains the value loaded. */
2069 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2071 return exp;
2074 /* EXP is an expression of integer type. Apply the integer promotions
2075 to it and return the promoted value. */
2077 tree
2078 perform_integral_promotions (tree exp)
2080 tree type = TREE_TYPE (exp);
2081 enum tree_code code = TREE_CODE (type);
2083 gcc_assert (INTEGRAL_TYPE_P (type));
2085 /* Normally convert enums to int,
2086 but convert wide enums to something wider. */
2087 if (code == ENUMERAL_TYPE)
2089 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2090 TYPE_PRECISION (integer_type_node)),
2091 ((TYPE_PRECISION (type)
2092 >= TYPE_PRECISION (integer_type_node))
2093 && TYPE_UNSIGNED (type)));
2095 return convert (type, exp);
2098 /* ??? This should no longer be needed now bit-fields have their
2099 proper types. */
2100 if (TREE_CODE (exp) == COMPONENT_REF
2101 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2102 /* If it's thinner than an int, promote it like a
2103 c_promoting_integer_type_p, otherwise leave it alone. */
2104 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2105 TYPE_PRECISION (integer_type_node)))
2106 return convert (integer_type_node, exp);
2108 if (c_promoting_integer_type_p (type))
2110 /* Preserve unsignedness if not really getting any wider. */
2111 if (TYPE_UNSIGNED (type)
2112 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2113 return convert (unsigned_type_node, exp);
2115 return convert (integer_type_node, exp);
2118 return exp;
2122 /* Perform default promotions for C data used in expressions.
2123 Enumeral types or short or char are converted to int.
2124 In addition, manifest constants symbols are replaced by their values. */
2126 tree
2127 default_conversion (tree exp)
2129 tree orig_exp;
2130 tree type = TREE_TYPE (exp);
2131 enum tree_code code = TREE_CODE (type);
2132 tree promoted_type;
2134 mark_exp_read (exp);
2136 /* Functions and arrays have been converted during parsing. */
2137 gcc_assert (code != FUNCTION_TYPE);
2139 if (code == ARRAY_TYPE && upc_shared_type_p (type))
2140 return array_to_pointer_conversion (input_location, exp);
2142 if (code == ARRAY_TYPE)
2143 return exp;
2145 /* Constants can be used directly unless they're not loadable. */
2146 if (TREE_CODE (exp) == CONST_DECL)
2147 exp = DECL_INITIAL (exp);
2149 /* Strip no-op conversions. */
2150 orig_exp = exp;
2151 STRIP_TYPE_NOPS (exp);
2153 if (TREE_NO_WARNING (orig_exp))
2154 TREE_NO_WARNING (exp) = 1;
2156 if (code == VOID_TYPE)
2158 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2159 "void value not ignored as it ought to be");
2160 return error_mark_node;
2163 exp = require_complete_type (exp);
2164 if (exp == error_mark_node)
2165 return error_mark_node;
2167 promoted_type = targetm.promoted_type (type);
2168 if (promoted_type)
2169 return convert (promoted_type, exp);
2171 if (INTEGRAL_TYPE_P (type))
2172 return perform_integral_promotions (exp);
2174 return exp;
2177 /* Look up COMPONENT in a structure or union TYPE.
2179 If the component name is not found, returns NULL_TREE. Otherwise,
2180 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2181 stepping down the chain to the component, which is in the last
2182 TREE_VALUE of the list. Normally the list is of length one, but if
2183 the component is embedded within (nested) anonymous structures or
2184 unions, the list steps down the chain to the component. */
2186 static tree
2187 lookup_field (tree type, tree component)
2189 tree field;
2191 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2192 to the field elements. Use a binary search on this array to quickly
2193 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2194 will always be set for structures which have many elements. */
2196 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2198 int bot, top, half;
2199 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2201 field = TYPE_FIELDS (type);
2202 bot = 0;
2203 top = TYPE_LANG_SPECIFIC (type)->s->len;
2204 while (top - bot > 1)
2206 half = (top - bot + 1) >> 1;
2207 field = field_array[bot+half];
2209 if (DECL_NAME (field) == NULL_TREE)
2211 /* Step through all anon unions in linear fashion. */
2212 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2214 field = field_array[bot++];
2215 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2216 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2218 tree anon = lookup_field (TREE_TYPE (field), component);
2220 if (anon)
2221 return tree_cons (NULL_TREE, field, anon);
2223 /* The Plan 9 compiler permits referring
2224 directly to an anonymous struct/union field
2225 using a typedef name. */
2226 if (flag_plan9_extensions
2227 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2228 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2229 == TYPE_DECL)
2230 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2231 == component))
2232 break;
2236 /* Entire record is only anon unions. */
2237 if (bot > top)
2238 return NULL_TREE;
2240 /* Restart the binary search, with new lower bound. */
2241 continue;
2244 if (DECL_NAME (field) == component)
2245 break;
2246 if (DECL_NAME (field) < component)
2247 bot += half;
2248 else
2249 top = bot + half;
2252 if (DECL_NAME (field_array[bot]) == component)
2253 field = field_array[bot];
2254 else if (DECL_NAME (field) != component)
2255 return NULL_TREE;
2257 else
2259 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2261 if (DECL_NAME (field) == NULL_TREE
2262 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2263 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2265 tree anon = lookup_field (TREE_TYPE (field), component);
2267 if (anon)
2268 return tree_cons (NULL_TREE, field, anon);
2270 /* The Plan 9 compiler permits referring directly to an
2271 anonymous struct/union field using a typedef
2272 name. */
2273 if (flag_plan9_extensions
2274 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2275 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2276 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2277 == component))
2278 break;
2281 if (DECL_NAME (field) == component)
2282 break;
2285 if (field == NULL_TREE)
2286 return NULL_TREE;
2289 return tree_cons (NULL_TREE, field, NULL_TREE);
2292 /* Make an expression to refer to the COMPONENT field of structure or
2293 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2294 location of the COMPONENT_REF. */
2296 tree
2297 build_component_ref (location_t loc, tree datum, tree component)
2299 tree type = TREE_TYPE (datum);
2300 enum tree_code code = TREE_CODE (type);
2301 tree field = NULL;
2302 tree ref;
2303 bool datum_lvalue = lvalue_p (datum);
2305 if (!objc_is_public (datum, component))
2306 return error_mark_node;
2308 /* Detect Objective-C property syntax object.property. */
2309 if (c_dialect_objc ()
2310 && (ref = objc_maybe_build_component_ref (datum, component)))
2311 return ref;
2313 /* See if there is a field or component with name COMPONENT. */
2315 if (code == RECORD_TYPE || code == UNION_TYPE)
2317 if (!COMPLETE_TYPE_P (type))
2319 c_incomplete_type_error (NULL_TREE, type);
2320 return error_mark_node;
2323 field = lookup_field (type, component);
2325 if (!field)
2327 error_at (loc, "%qT has no member named %qE", type, component);
2328 return error_mark_node;
2330 gcc_assert (!TREE_SHARED (field));
2332 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2333 This might be better solved in future the way the C++ front
2334 end does it - by giving the anonymous entities each a
2335 separate name and type, and then have build_component_ref
2336 recursively call itself. We can't do that here. */
2339 tree subdatum = TREE_VALUE (field);
2340 tree sub_elem_type = strip_array_types (TREE_TYPE (subdatum));
2341 tree upc_block_factor = NULL_TREE;
2342 int quals;
2343 tree subtype;
2344 bool use_datum_quals;
2346 if (TREE_TYPE (subdatum) == error_mark_node)
2347 return error_mark_node;
2349 /* If this is an rvalue, it does not have qualifiers in C
2350 standard terms and we must avoid propagating such
2351 qualifiers down to a non-lvalue array that is then
2352 converted to a pointer. */
2353 use_datum_quals = (datum_lvalue
2354 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2356 quals = TYPE_QUALS (sub_elem_type);
2357 if (use_datum_quals)
2358 quals |= TYPE_QUALS (TREE_TYPE (datum));
2359 /* All references to UPC shared struct components
2360 are defined to have an indefinite (zero) blocking factor. */
2361 if (quals & TYPE_QUAL_UPC_SHARED)
2362 upc_block_factor = size_zero_node;
2363 subtype = c_build_qualified_type_1 (TREE_TYPE (subdatum),
2364 quals, upc_block_factor);
2366 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2367 NULL_TREE);
2368 SET_EXPR_LOCATION (ref, loc);
2369 if (TREE_READONLY (subdatum)
2370 || (use_datum_quals && TREE_READONLY (datum)))
2371 TREE_READONLY (ref) = 1;
2372 if (TREE_THIS_VOLATILE (subdatum)
2373 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2374 TREE_THIS_VOLATILE (ref) = 1;
2375 if (TREE_SHARED (datum))
2376 TREE_SHARED (ref) = 1;
2378 if (TREE_DEPRECATED (subdatum))
2379 warn_deprecated_use (subdatum, NULL_TREE);
2381 datum = ref;
2383 field = TREE_CHAIN (field);
2385 while (field);
2387 return ref;
2389 else if (code != ERROR_MARK)
2390 error_at (loc,
2391 "request for member %qE in something not a structure or union",
2392 component);
2394 return error_mark_node;
2397 /* Given an expression PTR for a pointer, return an expression
2398 for the value pointed to.
2399 ERRORSTRING is the name of the operator to appear in error messages.
2401 LOC is the location to use for the generated tree. */
2403 tree
2404 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2406 tree pointer = default_conversion (ptr);
2407 tree type = TREE_TYPE (pointer);
2408 tree ref;
2410 if (TREE_CODE (type) == POINTER_TYPE)
2412 if (CONVERT_EXPR_P (pointer)
2413 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2415 /* If a warning is issued, mark it to avoid duplicates from
2416 the backend. This only needs to be done at
2417 warn_strict_aliasing > 2. */
2418 if (warn_strict_aliasing > 2)
2419 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2420 type, TREE_OPERAND (pointer, 0)))
2421 TREE_NO_WARNING (pointer) = 1;
2424 if (TREE_CODE (pointer) == ADDR_EXPR
2425 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2426 == TREE_TYPE (type)))
2428 ref = TREE_OPERAND (pointer, 0);
2429 protected_set_expr_location (ref, loc);
2430 return ref;
2432 else
2434 tree t = TREE_TYPE (type);
2436 ref = build1 (INDIRECT_REF, t, pointer);
2438 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2440 error_at (loc, "dereferencing pointer to incomplete type");
2441 return error_mark_node;
2443 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2444 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2446 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2447 so that we get the proper error message if the result is used
2448 to assign to. Also, &* is supposed to be a no-op.
2449 And ANSI C seems to specify that the type of the result
2450 should be the const type. */
2451 /* A de-reference of a pointer to const is not a const. It is valid
2452 to change it via some other pointer. */
2453 TREE_READONLY (ref) = TYPE_READONLY (t);
2454 TREE_SIDE_EFFECTS (ref)
2455 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2456 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2457 TREE_SHARED (ref) = upc_shared_type_p (t);
2458 protected_set_expr_location (ref, loc);
2459 return ref;
2462 else if (TREE_CODE (pointer) != ERROR_MARK)
2463 invalid_indirection_error (loc, type, errstring);
2465 return error_mark_node;
2468 /* This handles expressions of the form "a[i]", which denotes
2469 an array reference.
2471 This is logically equivalent in C to *(a+i), but we may do it differently.
2472 If A is a variable or a member, we generate a primitive ARRAY_REF.
2473 This avoids forcing the array out of registers, and can work on
2474 arrays that are not lvalues (for example, members of structures returned
2475 by functions).
2477 For vector types, allow vector[i] but not i[vector], and create
2478 *(((type*)&vectortype) + i) for the expression.
2480 LOC is the location to use for the returned expression. */
2482 tree
2483 build_array_ref (location_t loc, tree array, tree index)
2485 tree ret;
2486 bool swapped = false;
2487 if (TREE_TYPE (array) == error_mark_node
2488 || TREE_TYPE (index) == error_mark_node)
2489 return error_mark_node;
2491 if (flag_cilkplus && contains_array_notation_expr (index))
2493 size_t rank = 0;
2494 if (!find_rank (loc, index, index, true, &rank))
2495 return error_mark_node;
2496 if (rank > 1)
2498 error_at (loc, "rank of the array's index is greater than 1");
2499 return error_mark_node;
2502 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2503 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2504 /* Allow vector[index] but not index[vector]. */
2505 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2507 tree temp;
2508 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2509 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2511 error_at (loc,
2512 "subscripted value is neither array nor pointer nor vector");
2514 return error_mark_node;
2516 temp = array;
2517 array = index;
2518 index = temp;
2519 swapped = true;
2522 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2524 error_at (loc, "array subscript is not an integer");
2525 return error_mark_node;
2528 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2530 error_at (loc, "subscripted value is pointer to function");
2531 return error_mark_node;
2534 /* ??? Existing practice has been to warn only when the char
2535 index is syntactically the index, not for char[array]. */
2536 if (!swapped)
2537 warn_array_subscript_with_type_char (index);
2539 /* Apply default promotions *after* noticing character types. */
2540 index = default_conversion (index);
2542 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2544 convert_vector_to_pointer_for_subscript (loc, &array, index);
2545 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2546 && !upc_shared_type_p (TREE_TYPE (array)))
2548 tree rval, type;
2550 /* An array that is indexed by a non-constant
2551 cannot be stored in a register; we must be able to do
2552 address arithmetic on its address.
2553 Likewise an array of elements of variable size. */
2554 if (TREE_CODE (index) != INTEGER_CST
2555 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2556 && TREE_CODE (TYPE_SIZE (TREE_TYPE (
2557 TREE_TYPE (array)))) != INTEGER_CST))
2559 if (!c_mark_addressable (array))
2560 return error_mark_node;
2562 /* An array that is indexed by a constant value which is not within
2563 the array bounds cannot be stored in a register either; because we
2564 would get a crash in store_bit_field/extract_bit_field when trying
2565 to access a non-existent part of the register. */
2566 if (TREE_CODE (index) == INTEGER_CST
2567 && TYPE_DOMAIN (TREE_TYPE (array))
2568 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2570 if (!c_mark_addressable (array))
2571 return error_mark_node;
2574 if (pedantic)
2576 tree foo = array;
2577 while (TREE_CODE (foo) == COMPONENT_REF)
2578 foo = TREE_OPERAND (foo, 0);
2579 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2580 pedwarn (loc, OPT_Wpedantic,
2581 "ISO C forbids subscripting %<register%> array");
2582 else if (!flag_isoc99 && !lvalue_p (foo))
2583 pedwarn (loc, OPT_Wpedantic,
2584 "ISO C90 forbids subscripting non-lvalue array");
2587 type = TREE_TYPE (TREE_TYPE (array));
2588 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2589 /* Array ref is const/volatile if the array elements are
2590 or if the array is. */
2591 TREE_READONLY (rval)
2592 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2593 | TREE_READONLY (array));
2594 TREE_SIDE_EFFECTS (rval)
2595 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2596 | TREE_SIDE_EFFECTS (array));
2597 TREE_THIS_VOLATILE (rval)
2598 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2599 /* This was added by rms on 16 Nov 91.
2600 It fixes vol struct foo *a; a->elts[1]
2601 in an inline function.
2602 Hope it doesn't break something else. */
2603 | TREE_THIS_VOLATILE (array));
2604 ret = require_complete_type (rval);
2605 protected_set_expr_location (ret, loc);
2606 return ret;
2608 else
2610 tree ar = default_conversion (array);
2612 if (ar == error_mark_node)
2613 return ar;
2615 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2616 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2618 return build_indirect_ref
2619 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2620 RO_ARRAY_INDEXING);
2624 /* Build an external reference to identifier ID. FUN indicates
2625 whether this will be used for a function call. LOC is the source
2626 location of the identifier. This sets *TYPE to the type of the
2627 identifier, which is not the same as the type of the returned value
2628 for CONST_DECLs defined as enum constants. If the type of the
2629 identifier is not available, *TYPE is set to NULL. */
2630 tree
2631 build_external_ref (location_t loc, tree id, int fun, tree *type)
2633 tree ref;
2634 tree decl = lookup_name (id);
2636 /* In Objective-C, an instance variable (ivar) may be preferred to
2637 whatever lookup_name() found. */
2638 decl = objc_lookup_ivar (decl, id);
2640 *type = NULL;
2641 if (decl && decl != error_mark_node)
2643 ref = decl;
2644 *type = TREE_TYPE (ref);
2646 else if (fun)
2647 /* Implicit function declaration. */
2648 ref = implicitly_declare (loc, id);
2649 else if (decl == error_mark_node)
2650 /* Don't complain about something that's already been
2651 complained about. */
2652 return error_mark_node;
2653 else
2655 undeclared_variable (loc, id);
2656 return error_mark_node;
2659 if (TREE_TYPE (ref) == error_mark_node)
2660 return error_mark_node;
2662 if (TREE_DEPRECATED (ref))
2663 warn_deprecated_use (ref, NULL_TREE);
2665 /* Recursive call does not count as usage. */
2666 if (ref != current_function_decl)
2668 TREE_USED (ref) = 1;
2671 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2673 if (!in_sizeof && !in_typeof)
2674 C_DECL_USED (ref) = 1;
2675 else if (DECL_INITIAL (ref) == 0
2676 && DECL_EXTERNAL (ref)
2677 && !TREE_PUBLIC (ref))
2678 record_maybe_used_decl (ref);
2681 if (TREE_CODE (ref) == CONST_DECL)
2683 used_types_insert (TREE_TYPE (ref));
2685 if (warn_cxx_compat
2686 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2687 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2689 warning_at (loc, OPT_Wc___compat,
2690 ("enum constant defined in struct or union "
2691 "is not visible in C++"));
2692 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2695 ref = DECL_INITIAL (ref);
2696 TREE_CONSTANT (ref) = 1;
2698 else if (current_function_decl != 0
2699 && !DECL_FILE_SCOPE_P (current_function_decl)
2700 && (TREE_CODE (ref) == VAR_DECL
2701 || TREE_CODE (ref) == PARM_DECL
2702 || TREE_CODE (ref) == FUNCTION_DECL))
2704 tree context = decl_function_context (ref);
2706 if (context != 0 && context != current_function_decl)
2707 DECL_NONLOCAL (ref) = 1;
2709 /* C99 6.7.4p3: An inline definition of a function with external
2710 linkage ... shall not contain a reference to an identifier with
2711 internal linkage. */
2712 else if (current_function_decl != 0
2713 && DECL_DECLARED_INLINE_P (current_function_decl)
2714 && DECL_EXTERNAL (current_function_decl)
2715 && VAR_OR_FUNCTION_DECL_P (ref)
2716 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2717 && ! TREE_PUBLIC (ref)
2718 && DECL_CONTEXT (ref) != current_function_decl)
2719 record_inline_static (loc, current_function_decl, ref,
2720 csi_internal);
2722 return ref;
2725 /* Record details of decls possibly used inside sizeof or typeof. */
2726 struct maybe_used_decl
2728 /* The decl. */
2729 tree decl;
2730 /* The level seen at (in_sizeof + in_typeof). */
2731 int level;
2732 /* The next one at this level or above, or NULL. */
2733 struct maybe_used_decl *next;
2736 static struct maybe_used_decl *maybe_used_decls;
2738 /* Record that DECL, an undefined static function reference seen
2739 inside sizeof or typeof, might be used if the operand of sizeof is
2740 a VLA type or the operand of typeof is a variably modified
2741 type. */
2743 static void
2744 record_maybe_used_decl (tree decl)
2746 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2747 t->decl = decl;
2748 t->level = in_sizeof + in_typeof;
2749 t->next = maybe_used_decls;
2750 maybe_used_decls = t;
2753 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2754 USED is false, just discard them. If it is true, mark them used
2755 (if no longer inside sizeof or typeof) or move them to the next
2756 level up (if still inside sizeof or typeof). */
2758 void
2759 pop_maybe_used (bool used)
2761 struct maybe_used_decl *p = maybe_used_decls;
2762 int cur_level = in_sizeof + in_typeof;
2763 while (p && p->level > cur_level)
2765 if (used)
2767 if (cur_level == 0)
2768 C_DECL_USED (p->decl) = 1;
2769 else
2770 p->level = cur_level;
2772 p = p->next;
2774 if (!used || cur_level == 0)
2775 maybe_used_decls = p;
2778 /* Return the result of sizeof applied to EXPR. */
2780 struct c_expr
2781 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2783 struct c_expr ret;
2784 if (expr.value == error_mark_node)
2786 ret.value = error_mark_node;
2787 ret.original_code = ERROR_MARK;
2788 ret.original_type = NULL;
2789 pop_maybe_used (false);
2791 else
2793 bool expr_const_operands = true;
2795 if (TREE_CODE (expr.value) == PARM_DECL
2796 && C_ARRAY_PARAMETER (expr.value))
2798 if (warning_at (loc, OPT_Wsizeof_array_argument,
2799 "%<sizeof%> on array function parameter %qE will "
2800 "return size of %qT", expr.value,
2801 expr.original_type))
2802 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2804 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2805 &expr_const_operands);
2806 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2807 c_last_sizeof_arg = expr.value;
2808 ret.original_code = SIZEOF_EXPR;
2809 ret.original_type = NULL;
2810 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2812 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2813 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2814 folded_expr, ret.value);
2815 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2816 SET_EXPR_LOCATION (ret.value, loc);
2818 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2820 return ret;
2823 /* Return the result of sizeof applied to T, a structure for the type
2824 name passed to sizeof (rather than the type itself). LOC is the
2825 location of the original expression. */
2827 struct c_expr
2828 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2830 tree type;
2831 struct c_expr ret;
2832 tree type_expr = NULL_TREE;
2833 bool type_expr_const = true;
2834 type = groktypename (t, &type_expr, &type_expr_const);
2835 ret.value = c_sizeof (loc, type);
2836 c_last_sizeof_arg = type;
2837 ret.original_code = SIZEOF_EXPR;
2838 ret.original_type = NULL;
2839 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2840 && c_vla_type_p (type))
2842 /* If the type is a [*] array, it is a VLA but is represented as
2843 having a size of zero. In such a case we must ensure that
2844 the result of sizeof does not get folded to a constant by
2845 c_fully_fold, because if the size is evaluated the result is
2846 not constant and so constraints on zero or negative size
2847 arrays must not be applied when this sizeof call is inside
2848 another array declarator. */
2849 if (!type_expr)
2850 type_expr = integer_zero_node;
2851 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2852 type_expr, ret.value);
2853 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2855 pop_maybe_used (type != error_mark_node
2856 ? C_TYPE_VARIABLE_SIZE (type) : false);
2857 return ret;
2860 /* Build a function call to function FUNCTION with parameters PARAMS.
2861 The function call is at LOC.
2862 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2863 TREE_VALUE of each node is a parameter-expression.
2864 FUNCTION's data type may be a function type or a pointer-to-function. */
2866 tree
2867 build_function_call (location_t loc, tree function, tree params)
2869 vec<tree, va_gc> *v;
2870 tree ret;
2872 vec_alloc (v, list_length (params));
2873 for (; params; params = TREE_CHAIN (params))
2874 v->quick_push (TREE_VALUE (params));
2875 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2876 vec_free (v);
2877 return ret;
2880 /* Give a note about the location of the declaration of DECL. */
2882 static void inform_declaration (tree decl)
2884 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2885 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2888 /* Build a function call to function FUNCTION with parameters PARAMS.
2889 ORIGTYPES, if not NULL, is a vector of types; each element is
2890 either NULL or the original type of the corresponding element in
2891 PARAMS. The original type may differ from TREE_TYPE of the
2892 parameter for enums. FUNCTION's data type may be a function type
2893 or pointer-to-function. This function changes the elements of
2894 PARAMS. */
2896 tree
2897 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2898 tree function, vec<tree, va_gc> *params,
2899 vec<tree, va_gc> *origtypes)
2901 tree fntype, fundecl = 0;
2902 tree name = NULL_TREE, result;
2903 tree tem;
2904 int nargs;
2905 tree *argarray;
2908 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2909 STRIP_TYPE_NOPS (function);
2911 /* Convert anything with function type to a pointer-to-function. */
2912 if (TREE_CODE (function) == FUNCTION_DECL)
2914 name = DECL_NAME (function);
2916 if (flag_tm)
2917 tm_malloc_replacement (function);
2918 fundecl = function;
2919 /* Atomic functions have type checking/casting already done. They are
2920 often rewritten and don't match the original parameter list. */
2921 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2922 origtypes = NULL;
2924 if (flag_cilkplus
2925 && is_cilkplus_reduce_builtin (function))
2926 origtypes = NULL;
2928 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2929 function = function_to_pointer_conversion (loc, function);
2931 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2932 expressions, like those used for ObjC messenger dispatches. */
2933 if (params && !params->is_empty ())
2934 function = objc_rewrite_function_call (function, (*params)[0]);
2936 function = c_fully_fold (function, false, NULL);
2938 fntype = TREE_TYPE (function);
2940 if (TREE_CODE (fntype) == ERROR_MARK)
2941 return error_mark_node;
2943 if (!(TREE_CODE (fntype) == POINTER_TYPE
2944 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2946 if (!flag_diagnostics_show_caret)
2947 error_at (loc,
2948 "called object %qE is not a function or function pointer",
2949 function);
2950 else if (DECL_P (function))
2952 error_at (loc,
2953 "called object %qD is not a function or function pointer",
2954 function);
2955 inform_declaration (function);
2957 else
2958 error_at (loc,
2959 "called object is not a function or function pointer");
2960 return error_mark_node;
2963 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2964 current_function_returns_abnormally = 1;
2966 /* fntype now gets the type of function pointed to. */
2967 fntype = TREE_TYPE (fntype);
2969 /* Convert the parameters to the types declared in the
2970 function prototype, or apply default promotions. */
2972 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2973 origtypes, function, fundecl);
2974 if (nargs < 0)
2975 return error_mark_node;
2977 /* Check that the function is called through a compatible prototype.
2978 If it is not, warn. */
2979 if (CONVERT_EXPR_P (function)
2980 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2981 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2982 && !comptypes (fntype, TREE_TYPE (tem)))
2984 tree return_type = TREE_TYPE (fntype);
2986 /* This situation leads to run-time undefined behavior. We can't,
2987 therefore, simply error unless we can prove that all possible
2988 executions of the program must execute the code. */
2989 warning_at (loc, 0, "function called through a non-compatible type");
2991 if (VOID_TYPE_P (return_type)
2992 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2993 pedwarn (loc, 0,
2994 "function with qualified void return type called");
2997 argarray = vec_safe_address (params);
2999 /* Check that arguments to builtin functions match the expectations. */
3000 if (fundecl
3001 && DECL_BUILT_IN (fundecl)
3002 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3003 && !check_builtin_function_arguments (fundecl, nargs, argarray))
3004 return error_mark_node;
3006 /* Check that the arguments to the function are valid. */
3007 check_function_arguments (fntype, nargs, argarray);
3009 if (name != NULL_TREE
3010 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3012 if (require_constant_value)
3013 result =
3014 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3015 function, nargs, argarray);
3016 else
3017 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3018 function, nargs, argarray);
3019 if (TREE_CODE (result) == NOP_EXPR
3020 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3021 STRIP_TYPE_NOPS (result);
3023 else
3024 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3025 function, nargs, argarray);
3027 if (VOID_TYPE_P (TREE_TYPE (result)))
3029 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3030 pedwarn (loc, 0,
3031 "function with qualified void return type called");
3032 return result;
3034 return require_complete_type (result);
3037 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3039 tree
3040 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3041 tree function, vec<tree, va_gc> *params,
3042 vec<tree, va_gc> *origtypes)
3044 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3045 STRIP_TYPE_NOPS (function);
3047 /* Convert anything with function type to a pointer-to-function. */
3048 if (TREE_CODE (function) == FUNCTION_DECL)
3050 /* Implement type-directed function overloading for builtins.
3051 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3052 handle all the type checking. The result is a complete expression
3053 that implements this function call. */
3054 tree tem = resolve_overloaded_builtin (loc, function, params);
3055 if (tem)
3056 return tem;
3058 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3061 /* Convert the argument expressions in the vector VALUES
3062 to the types in the list TYPELIST.
3064 If TYPELIST is exhausted, or when an element has NULL as its type,
3065 perform the default conversions.
3067 ORIGTYPES is the original types of the expressions in VALUES. This
3068 holds the type of enum values which have been converted to integral
3069 types. It may be NULL.
3071 FUNCTION is a tree for the called function. It is used only for
3072 error messages, where it is formatted with %qE.
3074 This is also where warnings about wrong number of args are generated.
3076 ARG_LOC are locations of function arguments (if any).
3078 Returns the actual number of arguments processed (which may be less
3079 than the length of VALUES in some error situations), or -1 on
3080 failure. */
3082 static int
3083 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3084 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3085 tree function, tree fundecl)
3087 tree typetail, val;
3088 unsigned int parmnum;
3089 bool error_args = false;
3090 const bool type_generic = fundecl
3091 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3092 bool type_generic_remove_excess_precision = false;
3093 tree selector;
3095 /* Change pointer to function to the function itself for
3096 diagnostics. */
3097 if (TREE_CODE (function) == ADDR_EXPR
3098 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3099 function = TREE_OPERAND (function, 0);
3101 /* Handle an ObjC selector specially for diagnostics. */
3102 selector = objc_message_selector ();
3104 /* For type-generic built-in functions, determine whether excess
3105 precision should be removed (classification) or not
3106 (comparison). */
3107 if (type_generic
3108 && DECL_BUILT_IN (fundecl)
3109 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3111 switch (DECL_FUNCTION_CODE (fundecl))
3113 case BUILT_IN_ISFINITE:
3114 case BUILT_IN_ISINF:
3115 case BUILT_IN_ISINF_SIGN:
3116 case BUILT_IN_ISNAN:
3117 case BUILT_IN_ISNORMAL:
3118 case BUILT_IN_FPCLASSIFY:
3119 type_generic_remove_excess_precision = true;
3120 break;
3122 default:
3123 type_generic_remove_excess_precision = false;
3124 break;
3127 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3128 return vec_safe_length (values);
3130 /* Scan the given expressions and types, producing individual
3131 converted arguments. */
3133 for (typetail = typelist, parmnum = 0;
3134 values && values->iterate (parmnum, &val);
3135 ++parmnum)
3137 tree type = typetail ? TREE_VALUE (typetail) : 0;
3138 tree valtype = TREE_TYPE (val);
3139 tree rname = function;
3140 int argnum = parmnum + 1;
3141 const char *invalid_func_diag;
3142 bool excess_precision = false;
3143 bool npc;
3144 tree parmval;
3145 /* Some __atomic_* builtins have additional hidden argument at
3146 position 0. */
3147 location_t ploc
3148 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3149 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3150 : input_location;
3152 if (type == void_type_node)
3154 if (selector)
3155 error_at (loc, "too many arguments to method %qE", selector);
3156 else
3157 error_at (loc, "too many arguments to function %qE", function);
3158 inform_declaration (fundecl);
3159 return parmnum;
3162 if (selector && argnum > 2)
3164 rname = selector;
3165 argnum -= 2;
3168 npc = null_pointer_constant_p (val);
3170 /* If there is excess precision and a prototype, convert once to
3171 the required type rather than converting via the semantic
3172 type. Likewise without a prototype a float value represented
3173 as long double should be converted once to double. But for
3174 type-generic classification functions excess precision must
3175 be removed here. */
3176 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3177 && (type || !type_generic || !type_generic_remove_excess_precision))
3179 val = TREE_OPERAND (val, 0);
3180 excess_precision = true;
3182 val = c_fully_fold (val, false, NULL);
3183 STRIP_TYPE_NOPS (val);
3185 val = require_complete_type (val);
3187 if (type != 0)
3189 /* Formal parm type is specified by a function prototype. */
3191 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3193 error_at (ploc, "type of formal parameter %d is incomplete",
3194 parmnum + 1);
3195 parmval = val;
3197 else
3199 tree origtype;
3201 /* Optionally warn about conversions that
3202 differ from the default conversions. */
3203 if (warn_traditional_conversion || warn_traditional)
3205 unsigned int formal_prec = TYPE_PRECISION (type);
3207 if (INTEGRAL_TYPE_P (type)
3208 && TREE_CODE (valtype) == REAL_TYPE)
3209 warning_at (ploc, OPT_Wtraditional_conversion,
3210 "passing argument %d of %qE as integer rather "
3211 "than floating due to prototype",
3212 argnum, rname);
3213 if (INTEGRAL_TYPE_P (type)
3214 && TREE_CODE (valtype) == COMPLEX_TYPE)
3215 warning_at (ploc, OPT_Wtraditional_conversion,
3216 "passing argument %d of %qE as integer rather "
3217 "than complex due to prototype",
3218 argnum, rname);
3219 else if (TREE_CODE (type) == COMPLEX_TYPE
3220 && TREE_CODE (valtype) == REAL_TYPE)
3221 warning_at (ploc, OPT_Wtraditional_conversion,
3222 "passing argument %d of %qE as complex rather "
3223 "than floating due to prototype",
3224 argnum, rname);
3225 else if (TREE_CODE (type) == REAL_TYPE
3226 && INTEGRAL_TYPE_P (valtype))
3227 warning_at (ploc, OPT_Wtraditional_conversion,
3228 "passing argument %d of %qE as floating rather "
3229 "than integer due to prototype",
3230 argnum, rname);
3231 else if (TREE_CODE (type) == COMPLEX_TYPE
3232 && INTEGRAL_TYPE_P (valtype))
3233 warning_at (ploc, OPT_Wtraditional_conversion,
3234 "passing argument %d of %qE as complex rather "
3235 "than integer due to prototype",
3236 argnum, rname);
3237 else if (TREE_CODE (type) == REAL_TYPE
3238 && TREE_CODE (valtype) == COMPLEX_TYPE)
3239 warning_at (ploc, OPT_Wtraditional_conversion,
3240 "passing argument %d of %qE as floating rather "
3241 "than complex due to prototype",
3242 argnum, rname);
3243 /* ??? At some point, messages should be written about
3244 conversions between complex types, but that's too messy
3245 to do now. */
3246 else if (TREE_CODE (type) == REAL_TYPE
3247 && TREE_CODE (valtype) == REAL_TYPE)
3249 /* Warn if any argument is passed as `float',
3250 since without a prototype it would be `double'. */
3251 if (formal_prec == TYPE_PRECISION (float_type_node)
3252 && type != dfloat32_type_node)
3253 warning_at (ploc, 0,
3254 "passing argument %d of %qE as %<float%> "
3255 "rather than %<double%> due to prototype",
3256 argnum, rname);
3258 /* Warn if mismatch between argument and prototype
3259 for decimal float types. Warn of conversions with
3260 binary float types and of precision narrowing due to
3261 prototype. */
3262 else if (type != valtype
3263 && (type == dfloat32_type_node
3264 || type == dfloat64_type_node
3265 || type == dfloat128_type_node
3266 || valtype == dfloat32_type_node
3267 || valtype == dfloat64_type_node
3268 || valtype == dfloat128_type_node)
3269 && (formal_prec
3270 <= TYPE_PRECISION (valtype)
3271 || (type == dfloat128_type_node
3272 && (valtype
3273 != dfloat64_type_node
3274 && (valtype
3275 != dfloat32_type_node)))
3276 || (type == dfloat64_type_node
3277 && (valtype
3278 != dfloat32_type_node))))
3279 warning_at (ploc, 0,
3280 "passing argument %d of %qE as %qT "
3281 "rather than %qT due to prototype",
3282 argnum, rname, type, valtype);
3285 /* Detect integer changing in width or signedness.
3286 These warnings are only activated with
3287 -Wtraditional-conversion, not with -Wtraditional. */
3288 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3289 && INTEGRAL_TYPE_P (valtype))
3291 tree would_have_been = default_conversion (val);
3292 tree type1 = TREE_TYPE (would_have_been);
3294 if (TREE_CODE (type) == ENUMERAL_TYPE
3295 && (TYPE_MAIN_VARIANT (type)
3296 == TYPE_MAIN_VARIANT (valtype)))
3297 /* No warning if function asks for enum
3298 and the actual arg is that enum type. */
3300 else if (formal_prec != TYPE_PRECISION (type1))
3301 warning_at (ploc, OPT_Wtraditional_conversion,
3302 "passing argument %d of %qE "
3303 "with different width due to prototype",
3304 argnum, rname);
3305 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3307 /* Don't complain if the formal parameter type
3308 is an enum, because we can't tell now whether
3309 the value was an enum--even the same enum. */
3310 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3312 else if (TREE_CODE (val) == INTEGER_CST
3313 && int_fits_type_p (val, type))
3314 /* Change in signedness doesn't matter
3315 if a constant value is unaffected. */
3317 /* If the value is extended from a narrower
3318 unsigned type, it doesn't matter whether we
3319 pass it as signed or unsigned; the value
3320 certainly is the same either way. */
3321 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3322 && TYPE_UNSIGNED (valtype))
3324 else if (TYPE_UNSIGNED (type))
3325 warning_at (ploc, OPT_Wtraditional_conversion,
3326 "passing argument %d of %qE "
3327 "as unsigned due to prototype",
3328 argnum, rname);
3329 else
3330 warning_at (ploc, OPT_Wtraditional_conversion,
3331 "passing argument %d of %qE "
3332 "as signed due to prototype",
3333 argnum, rname);
3337 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3338 sake of better warnings from convert_and_check. */
3339 if (excess_precision)
3340 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3341 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3342 parmval = convert_for_assignment (loc, ploc, type,
3343 val, origtype, ic_argpass,
3344 npc, fundecl, function,
3345 parmnum + 1);
3347 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3348 && INTEGRAL_TYPE_P (type)
3349 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3350 parmval = default_conversion (parmval);
3353 else if (TREE_CODE (valtype) == REAL_TYPE
3354 && (TYPE_PRECISION (valtype)
3355 <= TYPE_PRECISION (double_type_node))
3356 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3357 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3358 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3360 if (type_generic)
3361 parmval = val;
3362 else
3364 /* Convert `float' to `double'. */
3365 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3366 warning_at (ploc, OPT_Wdouble_promotion,
3367 "implicit conversion from %qT to %qT when passing "
3368 "argument to function",
3369 valtype, double_type_node);
3370 parmval = convert (double_type_node, val);
3373 else if (excess_precision && !type_generic)
3374 /* A "double" argument with excess precision being passed
3375 without a prototype or in variable arguments. */
3376 parmval = convert (valtype, val);
3377 else if ((invalid_func_diag =
3378 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3380 error (invalid_func_diag);
3381 return -1;
3383 else
3384 /* Convert `short' and `char' to full-size `int'. */
3385 parmval = default_conversion (val);
3387 (*values)[parmnum] = parmval;
3388 if (parmval == error_mark_node)
3389 error_args = true;
3391 if (typetail)
3392 typetail = TREE_CHAIN (typetail);
3395 gcc_assert (parmnum == vec_safe_length (values));
3397 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3399 error_at (loc, "too few arguments to function %qE", function);
3400 inform_declaration (fundecl);
3401 return -1;
3404 return error_args ? -1 : (int) parmnum;
3407 /* This is the entry point used by the parser to build unary operators
3408 in the input. CODE, a tree_code, specifies the unary operator, and
3409 ARG is the operand. For unary plus, the C parser currently uses
3410 CONVERT_EXPR for code.
3412 LOC is the location to use for the tree generated.
3415 struct c_expr
3416 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3418 struct c_expr result;
3420 result.value = build_unary_op (loc, code, arg.value, 0);
3421 result.original_code = code;
3422 result.original_type = NULL;
3424 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3425 overflow_warning (loc, result.value);
3427 return result;
3430 /* This is the entry point used by the parser to build binary operators
3431 in the input. CODE, a tree_code, specifies the binary operator, and
3432 ARG1 and ARG2 are the operands. In addition to constructing the
3433 expression, we check for operands that were written with other binary
3434 operators in a way that is likely to confuse the user.
3436 LOCATION is the location of the binary operator. */
3438 struct c_expr
3439 parser_build_binary_op (location_t location, enum tree_code code,
3440 struct c_expr arg1, struct c_expr arg2)
3442 struct c_expr result;
3444 enum tree_code code1 = arg1.original_code;
3445 enum tree_code code2 = arg2.original_code;
3446 tree type1 = (arg1.original_type
3447 ? arg1.original_type
3448 : TREE_TYPE (arg1.value));
3449 tree type2 = (arg2.original_type
3450 ? arg2.original_type
3451 : TREE_TYPE (arg2.value));
3453 result.value = build_binary_op (location, code,
3454 arg1.value, arg2.value, 1);
3455 result.original_code = code;
3456 result.original_type = NULL;
3458 if (TREE_CODE (result.value) == ERROR_MARK)
3459 return result;
3461 if (location != UNKNOWN_LOCATION)
3462 protected_set_expr_location (result.value, location);
3464 /* Check for cases such as x+y<<z which users are likely
3465 to misinterpret. */
3466 if (warn_parentheses)
3467 warn_about_parentheses (location, code, code1, arg1.value, code2,
3468 arg2.value);
3470 if (warn_logical_op)
3471 warn_logical_operator (location, code, TREE_TYPE (result.value),
3472 code1, arg1.value, code2, arg2.value);
3474 if (warn_logical_not_paren
3475 && code1 == TRUTH_NOT_EXPR
3476 && code2 != TRUTH_NOT_EXPR)
3477 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3479 /* Warn about comparisons against string literals, with the exception
3480 of testing for equality or inequality of a string literal with NULL. */
3481 if (code == EQ_EXPR || code == NE_EXPR)
3483 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3484 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3485 warning_at (location, OPT_Waddress,
3486 "comparison with string literal results in unspecified behavior");
3488 else if (TREE_CODE_CLASS (code) == tcc_comparison
3489 && (code1 == STRING_CST || code2 == STRING_CST))
3490 warning_at (location, OPT_Waddress,
3491 "comparison with string literal results in unspecified behavior");
3493 if (TREE_OVERFLOW_P (result.value)
3494 && !TREE_OVERFLOW_P (arg1.value)
3495 && !TREE_OVERFLOW_P (arg2.value))
3496 overflow_warning (location, result.value);
3498 /* Warn about comparisons of different enum types. */
3499 if (warn_enum_compare
3500 && TREE_CODE_CLASS (code) == tcc_comparison
3501 && TREE_CODE (type1) == ENUMERAL_TYPE
3502 && TREE_CODE (type2) == ENUMERAL_TYPE
3503 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3504 warning_at (location, OPT_Wenum_compare,
3505 "comparison between %qT and %qT",
3506 type1, type2);
3508 return result;
3511 /* Return a tree for the sum or difference (RESULTCODE says which)
3512 of pointer PTROP and integer INTOP. */
3514 static
3515 tree
3516 c_pointer_int_sum (location_t location, enum tree_code resultcode,
3517 tree ptrop, tree intop)
3519 /* The result is a pointer of the same type that is being added. */
3520 tree result_type = TREE_TYPE (ptrop);
3522 if (upc_shared_type_p (TREE_TYPE (result_type)))
3523 return upc_pts_int_sum (location, resultcode, ptrop, intop);
3525 return pointer_int_sum (location, resultcode, ptrop, intop);
3528 /* Return a tree for the difference of pointers OP0 and OP1.
3529 The resulting tree has type int. */
3531 static tree
3532 pointer_diff (location_t loc, tree op0, tree op1)
3534 tree restype = ptrdiff_type_node;
3535 tree result, inttype;
3537 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3538 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3539 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3540 tree subtrahend_type = TREE_TYPE (TREE_TYPE (op1));
3541 tree con0, con1, lit0, lit1;
3542 tree orig_op1 = op1;
3544 /* If the operands point into different address spaces, we need to
3545 explicitly convert them to pointers into the common address space
3546 before we can subtract the numerical address values. */
3547 if (as0 != as1)
3549 addr_space_t as_common;
3550 tree common_type;
3552 /* Determine the common superset address space. This is guaranteed
3553 to exist because the caller verified that comp_target_types
3554 returned non-zero. */
3555 if (!addr_space_superset (as0, as1, &as_common))
3556 gcc_unreachable ();
3558 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3559 op0 = convert (common_type, op0);
3560 op1 = convert (common_type, op1);
3563 /* Determine integer type to perform computations in. This will usually
3564 be the same as the result type (ptrdiff_t), but may need to be a wider
3565 type if pointers for the address space are wider than ptrdiff_t. */
3566 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3567 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3568 else
3569 inttype = restype;
3572 if (TREE_CODE (target_type) == VOID_TYPE)
3573 pedwarn (loc, OPT_Wpointer_arith,
3574 "pointer of type %<void *%> used in subtraction");
3575 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3576 pedwarn (loc, OPT_Wpointer_arith,
3577 "pointer to a function used in subtraction");
3579 if (upc_shared_type_p (target_type) || upc_shared_type_p (subtrahend_type))
3580 return upc_pts_diff (op0, op1);
3582 /* If the conversion to ptrdiff_type does anything like widening or
3583 converting a partial to an integral mode, we get a convert_expression
3584 that is in the way to do any simplifications.
3585 (fold-const.c doesn't know that the extra bits won't be needed.
3586 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3587 different mode in place.)
3588 So first try to find a common term here 'by hand'; we want to cover
3589 at least the cases that occur in legal static initializers. */
3590 if (CONVERT_EXPR_P (op0)
3591 && (TYPE_PRECISION (TREE_TYPE (op0))
3592 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3593 con0 = TREE_OPERAND (op0, 0);
3594 else
3595 con0 = op0;
3596 if (CONVERT_EXPR_P (op1)
3597 && (TYPE_PRECISION (TREE_TYPE (op1))
3598 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3599 con1 = TREE_OPERAND (op1, 0);
3600 else
3601 con1 = op1;
3603 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3605 lit0 = TREE_OPERAND (con0, 1);
3606 con0 = TREE_OPERAND (con0, 0);
3608 else
3609 lit0 = integer_zero_node;
3611 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
3613 lit1 = TREE_OPERAND (con1, 1);
3614 con1 = TREE_OPERAND (con1, 0);
3616 else
3617 lit1 = integer_zero_node;
3619 if (operand_equal_p (con0, con1, 0)
3620 && !upc_shared_type_p (TREE_TYPE (TREE_TYPE (con0)))
3621 && !upc_shared_type_p (TREE_TYPE (TREE_TYPE (con1))))
3623 op0 = lit0;
3624 op1 = lit1;
3628 /* First do the subtraction as integers;
3629 then drop through to build the divide operator.
3630 Do not do default conversions on the minus operator
3631 in case restype is a short type. */
3633 op0 = build_binary_op (loc,
3634 MINUS_EXPR, convert (inttype, op0),
3635 convert (inttype, op1), 0);
3636 /* This generates an error if op1 is pointer to incomplete type. */
3637 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3638 error_at (loc, "arithmetic on pointer to an incomplete type");
3640 op1 = c_size_in_bytes (target_type);
3642 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3643 error_at (loc, "arithmetic on pointer to an empty aggregate");
3645 /* Divide by the size, in easiest possible way. */
3646 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3647 op0, convert (inttype, op1));
3649 /* Convert to final result type if necessary. */
3650 return convert (restype, result);
3653 /* Expand atomic compound assignments into an approriate sequence as
3654 specified by the C11 standard section 6.5.16.2.
3655 given
3656 _Atomic T1 E1
3657 T2 E2
3658 E1 op= E2
3660 This sequence is used for all types for which these operations are
3661 supported.
3663 In addition, built-in versions of the 'fe' prefixed routines may
3664 need to be invoked for floating point (real, complex or vector) when
3665 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3667 T1 newval;
3668 T1 old;
3669 T1 *addr
3670 T2 val
3671 fenv_t fenv
3673 addr = &E1;
3674 val = (E2);
3675 __atomic_load (addr, &old, SEQ_CST);
3676 feholdexcept (&fenv);
3677 loop:
3678 newval = old op val;
3679 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3680 SEQ_CST))
3681 goto done;
3682 feclearexcept (FE_ALL_EXCEPT);
3683 goto loop:
3684 done:
3685 feupdateenv (&fenv);
3687 Also note that the compiler is simply issuing the generic form of
3688 the atomic operations. This requires temp(s) and has their address
3689 taken. The atomic processing is smart enough to figure out when the
3690 size of an object can utilize a lock-free version, and convert the
3691 built-in call to the appropriate lock-free routine. The optimizers
3692 will then dispose of any temps that are no longer required, and
3693 lock-free implementations are utilized as long as there is target
3694 support for the required size.
3696 If the operator is NOP_EXPR, then this is a simple assignment, and
3697 an __atomic_store is issued to perform the assignment rather than
3698 the above loop.
3702 /* Build an atomic assignment at LOC, expanding into the proper
3703 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3704 the result of the operation, unless RETURN_OLD_P in which case
3705 return the old value of LHS (this is only for postincrement and
3706 postdecrement). */
3707 static tree
3708 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3709 tree rhs, bool return_old_p)
3711 tree fndecl, func_call;
3712 vec<tree, va_gc> *params;
3713 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3714 tree old, old_addr;
3715 tree compound_stmt;
3716 tree stmt, goto_stmt;
3717 tree loop_label, loop_decl, done_label, done_decl;
3719 tree lhs_type = TREE_TYPE (lhs);
3720 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3721 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3722 tree rhs_type = TREE_TYPE (rhs);
3724 gcc_assert (TYPE_ATOMIC (lhs_type));
3726 if (return_old_p)
3727 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3729 /* Allocate enough vector items for a compare_exchange. */
3730 vec_alloc (params, 6);
3732 /* Create a compound statement to hold the sequence of statements
3733 with a loop. */
3734 compound_stmt = c_begin_compound_stmt (false);
3736 /* Fold the RHS if it hasn't already been folded. */
3737 if (modifycode != NOP_EXPR)
3738 rhs = c_fully_fold (rhs, false, NULL);
3740 /* Remove the qualifiers for the rest of the expressions and create
3741 the VAL temp variable to hold the RHS. */
3742 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3743 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3744 val = create_tmp_var (nonatomic_rhs_type, NULL);
3745 TREE_ADDRESSABLE (val) = 1;
3746 TREE_NO_WARNING (val) = 1;
3747 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3748 SET_EXPR_LOCATION (rhs, loc);
3749 add_stmt (rhs);
3751 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3752 an atomic_store. */
3753 if (modifycode == NOP_EXPR)
3755 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3756 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3757 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3758 params->quick_push (lhs_addr);
3759 params->quick_push (rhs);
3760 params->quick_push (seq_cst);
3761 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3762 add_stmt (func_call);
3764 /* Finish the compound statement. */
3765 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3767 /* VAL is the value which was stored, return a COMPOUND_STMT of
3768 the statement and that value. */
3769 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3772 /* Create the variables and labels required for the op= form. */
3773 old = create_tmp_var (nonatomic_lhs_type, NULL);
3774 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3775 TREE_ADDRESSABLE (old) = 1;
3776 TREE_NO_WARNING (old) = 1;
3778 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3779 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3780 TREE_ADDRESSABLE (newval) = 1;
3782 loop_decl = create_artificial_label (loc);
3783 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3785 done_decl = create_artificial_label (loc);
3786 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3788 /* __atomic_load (addr, &old, SEQ_CST). */
3789 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3790 params->quick_push (lhs_addr);
3791 params->quick_push (old_addr);
3792 params->quick_push (seq_cst);
3793 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3794 add_stmt (func_call);
3795 params->truncate (0);
3797 /* Create the expressions for floating-point environment
3798 manipulation, if required. */
3799 bool need_fenv = (flag_trapping_math
3800 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3801 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3802 if (need_fenv)
3803 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3805 if (hold_call)
3806 add_stmt (hold_call);
3808 /* loop: */
3809 add_stmt (loop_label);
3811 /* newval = old + val; */
3812 rhs = build_binary_op (loc, modifycode, old, val, 1);
3813 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3814 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3815 NULL_TREE, 0);
3816 if (rhs != error_mark_node)
3818 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3819 SET_EXPR_LOCATION (rhs, loc);
3820 add_stmt (rhs);
3823 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3824 goto done; */
3825 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3826 params->quick_push (lhs_addr);
3827 params->quick_push (old_addr);
3828 params->quick_push (newval_addr);
3829 params->quick_push (integer_zero_node);
3830 params->quick_push (seq_cst);
3831 params->quick_push (seq_cst);
3832 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3834 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3835 SET_EXPR_LOCATION (goto_stmt, loc);
3837 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3838 SET_EXPR_LOCATION (stmt, loc);
3839 add_stmt (stmt);
3841 if (clear_call)
3842 add_stmt (clear_call);
3844 /* goto loop; */
3845 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3846 SET_EXPR_LOCATION (goto_stmt, loc);
3847 add_stmt (goto_stmt);
3849 /* done: */
3850 add_stmt (done_label);
3852 if (update_call)
3853 add_stmt (update_call);
3855 /* Finish the compound statement. */
3856 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3858 /* NEWVAL is the value that was successfully stored, return a
3859 COMPOUND_EXPR of the statement and the appropriate value. */
3860 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3861 return_old_p ? old : newval);
3864 /* Construct and perhaps optimize a tree representation
3865 for a unary operation. CODE, a tree_code, specifies the operation
3866 and XARG is the operand.
3867 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3868 the default promotions (such as from short to int).
3869 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3870 allows non-lvalues; this is only used to handle conversion of non-lvalue
3871 arrays to pointers in C99.
3873 LOCATION is the location of the operator. */
3875 tree
3876 build_unary_op (location_t location,
3877 enum tree_code code, tree xarg, int flag)
3879 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3880 tree arg = xarg;
3881 tree argtype = 0;
3882 enum tree_code typecode;
3883 tree val;
3884 tree ret = error_mark_node;
3885 tree eptype = NULL_TREE;
3886 int noconvert = flag;
3887 const char *invalid_op_diag;
3888 bool int_operands;
3890 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3891 if (int_operands)
3892 arg = remove_c_maybe_const_expr (arg);
3894 if (code != ADDR_EXPR)
3895 arg = require_complete_type (arg);
3897 typecode = TREE_CODE (TREE_TYPE (arg));
3898 if (typecode == ERROR_MARK)
3899 return error_mark_node;
3900 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3901 typecode = INTEGER_TYPE;
3903 if ((invalid_op_diag
3904 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3906 error_at (location, invalid_op_diag);
3907 return error_mark_node;
3910 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3912 eptype = TREE_TYPE (arg);
3913 arg = TREE_OPERAND (arg, 0);
3916 switch (code)
3918 case CONVERT_EXPR:
3919 /* This is used for unary plus, because a CONVERT_EXPR
3920 is enough to prevent anybody from looking inside for
3921 associativity, but won't generate any code. */
3922 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3923 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3924 || typecode == VECTOR_TYPE))
3926 error_at (location, "wrong type argument to unary plus");
3927 return error_mark_node;
3929 else if (!noconvert)
3930 arg = default_conversion (arg);
3931 arg = non_lvalue_loc (location, arg);
3932 break;
3934 case NEGATE_EXPR:
3935 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3936 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3937 || typecode == VECTOR_TYPE))
3939 error_at (location, "wrong type argument to unary minus");
3940 return error_mark_node;
3942 else if (!noconvert)
3943 arg = default_conversion (arg);
3944 break;
3946 case BIT_NOT_EXPR:
3947 /* ~ works on integer types and non float vectors. */
3948 if (typecode == INTEGER_TYPE
3949 || (typecode == VECTOR_TYPE
3950 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3952 if (!noconvert)
3953 arg = default_conversion (arg);
3955 else if (typecode == COMPLEX_TYPE)
3957 code = CONJ_EXPR;
3958 pedwarn (location, OPT_Wpedantic,
3959 "ISO C does not support %<~%> for complex conjugation");
3960 if (!noconvert)
3961 arg = default_conversion (arg);
3963 else
3965 error_at (location, "wrong type argument to bit-complement");
3966 return error_mark_node;
3968 break;
3970 case ABS_EXPR:
3971 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3973 error_at (location, "wrong type argument to abs");
3974 return error_mark_node;
3976 else if (!noconvert)
3977 arg = default_conversion (arg);
3978 break;
3980 case CONJ_EXPR:
3981 /* Conjugating a real value is a no-op, but allow it anyway. */
3982 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3983 || typecode == COMPLEX_TYPE))
3985 error_at (location, "wrong type argument to conjugation");
3986 return error_mark_node;
3988 else if (!noconvert)
3989 arg = default_conversion (arg);
3990 break;
3992 case TRUTH_NOT_EXPR:
3993 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3994 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3995 && typecode != COMPLEX_TYPE)
3997 error_at (location,
3998 "wrong type argument to unary exclamation mark");
3999 return error_mark_node;
4001 if (int_operands)
4003 arg = c_objc_common_truthvalue_conversion (location, xarg);
4004 arg = remove_c_maybe_const_expr (arg);
4006 else
4007 arg = c_objc_common_truthvalue_conversion (location, arg);
4008 ret = invert_truthvalue_loc (location, arg);
4009 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4010 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4011 location = EXPR_LOCATION (ret);
4012 goto return_build_unary_op;
4014 case REALPART_EXPR:
4015 case IMAGPART_EXPR:
4016 ret = build_real_imag_expr (location, code, arg);
4017 if (ret == error_mark_node)
4018 return error_mark_node;
4019 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4020 eptype = TREE_TYPE (eptype);
4021 goto return_build_unary_op;
4023 case PREINCREMENT_EXPR:
4024 case POSTINCREMENT_EXPR:
4025 case PREDECREMENT_EXPR:
4026 case POSTDECREMENT_EXPR:
4028 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4030 tree inner = build_unary_op (location, code,
4031 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4032 if (inner == error_mark_node)
4033 return error_mark_node;
4034 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4035 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4036 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4037 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4038 goto return_build_unary_op;
4041 /* Complain about anything that is not a true lvalue. In
4042 Objective-C, skip this check for property_refs. */
4043 if (!objc_is_property_ref (arg)
4044 && !lvalue_or_else (location,
4045 arg, ((code == PREINCREMENT_EXPR
4046 || code == POSTINCREMENT_EXPR)
4047 ? lv_increment
4048 : lv_decrement)))
4049 return error_mark_node;
4051 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4053 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4054 warning_at (location, OPT_Wc___compat,
4055 "increment of enumeration value is invalid in C++");
4056 else
4057 warning_at (location, OPT_Wc___compat,
4058 "decrement of enumeration value is invalid in C++");
4061 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4062 arg = c_fully_fold (arg, false, NULL);
4064 bool atomic_op;
4065 atomic_op = really_atomic_lvalue (arg);
4067 /* Increment or decrement the real part of the value,
4068 and don't change the imaginary part. */
4069 if (typecode == COMPLEX_TYPE)
4071 tree real, imag;
4073 pedwarn (location, OPT_Wpedantic,
4074 "ISO C does not support %<++%> and %<--%> on complex types");
4076 if (!atomic_op)
4078 arg = stabilize_reference (arg);
4079 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4080 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4081 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4082 if (real == error_mark_node || imag == error_mark_node)
4083 return error_mark_node;
4084 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4085 real, imag);
4086 goto return_build_unary_op;
4090 /* Report invalid types. */
4092 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4093 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4094 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4096 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4097 error_at (location, "wrong type argument to increment");
4098 else
4099 error_at (location, "wrong type argument to decrement");
4101 return error_mark_node;
4105 tree inc;
4107 argtype = TREE_TYPE (arg);
4109 /* Compute the increment. */
4111 if (typecode == POINTER_TYPE)
4113 /* If pointer target is an incomplete type,
4114 we just cannot know how to do the arithmetic. */
4115 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4117 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4118 error_at (location,
4119 "increment of pointer to an incomplete type %qT",
4120 TREE_TYPE (argtype));
4121 else
4122 error_at (location,
4123 "decrement of pointer to an incomplete type %qT",
4124 TREE_TYPE (argtype));
4126 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4127 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4129 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4130 pedwarn (location, OPT_Wpointer_arith,
4131 "wrong type argument to increment");
4132 else
4133 pedwarn (location, OPT_Wpointer_arith,
4134 "wrong type argument to decrement");
4137 /* UPC pointer-to-shared types cannot be
4138 incremented/decrmented directly. */
4139 if (upc_shared_type_p (TREE_TYPE (argtype)))
4140 return upc_pts_increment (location, code, arg);
4142 inc = c_size_in_bytes (TREE_TYPE (argtype));
4143 inc = convert_to_ptrofftype_loc (location, inc);
4145 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4147 /* For signed fract types, we invert ++ to -- or
4148 -- to ++, and change inc from 1 to -1, because
4149 it is not possible to represent 1 in signed fract constants.
4150 For unsigned fract types, the result always overflows and
4151 we get an undefined (original) or the maximum value. */
4152 if (code == PREINCREMENT_EXPR)
4153 code = PREDECREMENT_EXPR;
4154 else if (code == PREDECREMENT_EXPR)
4155 code = PREINCREMENT_EXPR;
4156 else if (code == POSTINCREMENT_EXPR)
4157 code = POSTDECREMENT_EXPR;
4158 else /* code == POSTDECREMENT_EXPR */
4159 code = POSTINCREMENT_EXPR;
4161 inc = integer_minus_one_node;
4162 inc = convert (argtype, inc);
4164 else
4166 inc = VECTOR_TYPE_P (argtype)
4167 ? build_one_cst (argtype)
4168 : integer_one_node;
4169 inc = convert (argtype, inc);
4172 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4173 need to ask Objective-C to build the increment or decrement
4174 expression for it. */
4175 if (objc_is_property_ref (arg))
4176 return objc_build_incr_expr_for_property_ref (location, code,
4177 arg, inc);
4179 /* Report a read-only lvalue. */
4180 if (TYPE_READONLY (argtype))
4182 readonly_error (location, arg,
4183 ((code == PREINCREMENT_EXPR
4184 || code == POSTINCREMENT_EXPR)
4185 ? lv_increment : lv_decrement));
4186 return error_mark_node;
4188 else if (TREE_READONLY (arg))
4189 readonly_warning (arg,
4190 ((code == PREINCREMENT_EXPR
4191 || code == POSTINCREMENT_EXPR)
4192 ? lv_increment : lv_decrement));
4194 /* If the argument is atomic, use the special code sequences for
4195 atomic compound assignment. */
4196 if (atomic_op)
4198 arg = stabilize_reference (arg);
4199 ret = build_atomic_assign (location, arg,
4200 ((code == PREINCREMENT_EXPR
4201 || code == POSTINCREMENT_EXPR)
4202 ? PLUS_EXPR
4203 : MINUS_EXPR),
4204 (FRACT_MODE_P (TYPE_MODE (argtype))
4205 ? inc
4206 : integer_one_node),
4207 (code == POSTINCREMENT_EXPR
4208 || code == POSTDECREMENT_EXPR));
4209 goto return_build_unary_op;
4212 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4213 val = boolean_increment (code, arg);
4214 else
4215 val = build2 (code, TREE_TYPE (arg), arg, inc);
4216 TREE_SIDE_EFFECTS (val) = 1;
4217 if (TREE_CODE (val) != code)
4218 TREE_NO_WARNING (val) = 1;
4219 ret = val;
4220 goto return_build_unary_op;
4223 case ADDR_EXPR:
4224 /* Note that this operation never does default_conversion. */
4226 /* The operand of unary '&' must be an lvalue (which excludes
4227 expressions of type void), or, in C99, the result of a [] or
4228 unary '*' operator. */
4229 if (VOID_TYPE_P (TREE_TYPE (arg))
4230 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4231 && (TREE_CODE (arg) != INDIRECT_REF
4232 || !flag_isoc99))
4233 pedwarn (location, 0, "taking address of expression of type %<void%>");
4235 /* Let &* cancel out to simplify resulting code. */
4236 if (TREE_CODE (arg) == INDIRECT_REF)
4238 /* Don't let this be an lvalue. */
4239 if (lvalue_p (TREE_OPERAND (arg, 0)))
4240 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4241 ret = TREE_OPERAND (arg, 0);
4242 goto return_build_unary_op;
4245 /* For &x[y], return x+y */
4246 if (TREE_CODE (arg) == ARRAY_REF)
4248 tree op0 = TREE_OPERAND (arg, 0);
4249 if (!c_mark_addressable (op0))
4250 return error_mark_node;
4251 /* Taking the address of a UPC shared array element
4252 cannot be performed as a simple addition.
4253 Return an ADDR_EXPR node, and let upc_genericize()
4254 implement the proper semantics. */
4255 if (TREE_SHARED (arg))
4256 return build1 (ADDR_EXPR, TREE_TYPE (arg), arg);
4259 /* Anything not already handled and not a true memory reference
4260 or a non-lvalue array is an error. */
4261 else if (typecode != FUNCTION_TYPE && !flag
4262 && !lvalue_or_else (location, arg, lv_addressof))
4263 return error_mark_node;
4265 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4266 folding later. */
4267 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4269 tree inner = build_unary_op (location, code,
4270 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4271 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4272 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4273 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4274 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4275 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4276 goto return_build_unary_op;
4279 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4280 argtype = TREE_TYPE (arg);
4282 /* If the lvalue is const or volatile, merge that into the type
4283 to which the address will point. This is only needed
4284 for function types. */
4285 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4286 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4287 && TREE_CODE (argtype) == FUNCTION_TYPE)
4289 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4290 int quals = orig_quals;
4292 if (TREE_READONLY (arg))
4293 quals |= TYPE_QUAL_CONST;
4294 if (TREE_THIS_VOLATILE (arg))
4295 quals |= TYPE_QUAL_VOLATILE;
4297 argtype = c_build_qualified_type (argtype, quals);
4300 if (!c_mark_addressable (arg))
4301 return error_mark_node;
4303 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4304 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4306 argtype = build_pointer_type (argtype);
4308 /* ??? Cope with user tricks that amount to offsetof. Delete this
4309 when we have proper support for integer constant expressions. */
4310 val = get_base_address (arg);
4311 if (val && TREE_CODE (val) == INDIRECT_REF
4312 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4314 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4315 goto return_build_unary_op;
4318 val = build1 (ADDR_EXPR, argtype, arg);
4320 ret = val;
4321 goto return_build_unary_op;
4323 default:
4324 gcc_unreachable ();
4327 if (argtype == 0)
4328 argtype = TREE_TYPE (arg);
4329 if (TREE_CODE (arg) == INTEGER_CST)
4330 ret = (require_constant_value
4331 ? fold_build1_initializer_loc (location, code, argtype, arg)
4332 : fold_build1_loc (location, code, argtype, arg));
4333 else
4334 ret = build1 (code, argtype, arg);
4335 return_build_unary_op:
4336 gcc_assert (ret != error_mark_node);
4337 /* The result of an operation on objects that
4338 are UPC shared qualified, must not be shared qualified. */
4339 if (upc_shared_type_p (TREE_TYPE (ret)))
4340 TREE_TYPE (ret) = build_upc_unshared_type (TREE_TYPE (ret));
4341 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4342 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4343 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4344 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4345 ret = note_integer_operands (ret);
4346 if (eptype)
4347 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4348 protected_set_expr_location (ret, location);
4349 return ret;
4352 /* Return nonzero if REF is an lvalue valid for this language.
4353 Lvalues can be assigned, unless their type has TYPE_READONLY.
4354 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4356 bool
4357 lvalue_p (const_tree ref)
4359 const enum tree_code code = TREE_CODE (ref);
4361 switch (code)
4363 case REALPART_EXPR:
4364 case IMAGPART_EXPR:
4365 case COMPONENT_REF:
4366 return lvalue_p (TREE_OPERAND (ref, 0));
4368 case C_MAYBE_CONST_EXPR:
4369 return lvalue_p (TREE_OPERAND (ref, 1));
4371 case COMPOUND_LITERAL_EXPR:
4372 case STRING_CST:
4373 return 1;
4375 case INDIRECT_REF:
4376 case ARRAY_REF:
4377 case ARRAY_NOTATION_REF:
4378 case VAR_DECL:
4379 case PARM_DECL:
4380 case RESULT_DECL:
4381 case ERROR_MARK:
4382 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4383 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4385 case BIND_EXPR:
4386 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4388 default:
4389 return 0;
4393 /* Give a warning for storing in something that is read-only in GCC
4394 terms but not const in ISO C terms. */
4396 static void
4397 readonly_warning (tree arg, enum lvalue_use use)
4399 switch (use)
4401 case lv_assign:
4402 warning (0, "assignment of read-only location %qE", arg);
4403 break;
4404 case lv_increment:
4405 warning (0, "increment of read-only location %qE", arg);
4406 break;
4407 case lv_decrement:
4408 warning (0, "decrement of read-only location %qE", arg);
4409 break;
4410 default:
4411 gcc_unreachable ();
4413 return;
4417 /* Return nonzero if REF is an lvalue valid for this language;
4418 otherwise, print an error message and return zero. USE says
4419 how the lvalue is being used and so selects the error message.
4420 LOCATION is the location at which any error should be reported. */
4422 static int
4423 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4425 int win = lvalue_p (ref);
4427 if (!win)
4428 lvalue_error (loc, use);
4430 return win;
4433 /* Mark EXP saying that we need to be able to take the
4434 address of it; it should not be allocated in a register.
4435 Returns true if successful. */
4437 bool
4438 c_mark_addressable (tree exp)
4440 tree x = exp;
4442 while (1)
4443 switch (TREE_CODE (x))
4445 case COMPONENT_REF:
4446 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4448 error
4449 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4450 return false;
4453 /* ... fall through ... */
4455 case ADDR_EXPR:
4456 case ARRAY_REF:
4457 case REALPART_EXPR:
4458 case IMAGPART_EXPR:
4459 x = TREE_OPERAND (x, 0);
4460 break;
4462 case COMPOUND_LITERAL_EXPR:
4463 case CONSTRUCTOR:
4464 TREE_ADDRESSABLE (x) = 1;
4465 return true;
4467 case VAR_DECL:
4468 case CONST_DECL:
4469 case PARM_DECL:
4470 case RESULT_DECL:
4471 if (C_DECL_REGISTER (x)
4472 && DECL_NONLOCAL (x))
4474 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4476 error
4477 ("global register variable %qD used in nested function", x);
4478 return false;
4480 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4482 else if (C_DECL_REGISTER (x))
4484 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4485 error ("address of global register variable %qD requested", x);
4486 else
4487 error ("address of register variable %qD requested", x);
4488 return false;
4491 /* drops in */
4492 case FUNCTION_DECL:
4493 TREE_ADDRESSABLE (x) = 1;
4494 /* drops out */
4495 default:
4496 return true;
4500 /* Convert EXPR to TYPE, warning about conversion problems with
4501 constants. SEMANTIC_TYPE is the type this conversion would use
4502 without excess precision. If SEMANTIC_TYPE is NULL, this function
4503 is equivalent to convert_and_check. This function is a wrapper that
4504 handles conversions that may be different than
4505 the usual ones because of excess precision. */
4507 static tree
4508 ep_convert_and_check (location_t loc, tree type, tree expr,
4509 tree semantic_type)
4511 if (TREE_TYPE (expr) == type)
4512 return expr;
4514 if (!semantic_type)
4515 return convert_and_check (loc, type, expr);
4517 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4518 && TREE_TYPE (expr) != semantic_type)
4520 /* For integers, we need to check the real conversion, not
4521 the conversion to the excess precision type. */
4522 expr = convert_and_check (loc, semantic_type, expr);
4524 /* Result type is the excess precision type, which should be
4525 large enough, so do not check. */
4526 return convert (type, expr);
4529 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4530 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4531 if folded to an integer constant then the unselected half may
4532 contain arbitrary operations not normally permitted in constant
4533 expressions. Set the location of the expression to LOC. */
4535 tree
4536 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4537 tree op1, tree op1_original_type, tree op2,
4538 tree op2_original_type)
4540 tree type1;
4541 tree type2;
4542 enum tree_code code1;
4543 enum tree_code code2;
4544 tree result_type = NULL;
4545 tree semantic_result_type = NULL;
4546 tree orig_op1 = op1, orig_op2 = op2;
4547 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4548 bool ifexp_int_operands;
4549 tree ret;
4551 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4552 if (op1_int_operands)
4553 op1 = remove_c_maybe_const_expr (op1);
4554 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4555 if (op2_int_operands)
4556 op2 = remove_c_maybe_const_expr (op2);
4557 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4558 if (ifexp_int_operands)
4559 ifexp = remove_c_maybe_const_expr (ifexp);
4561 /* Promote both alternatives. */
4563 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4564 op1 = default_conversion (op1);
4565 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4566 op2 = default_conversion (op2);
4568 if (TREE_CODE (ifexp) == ERROR_MARK
4569 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4570 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4571 return error_mark_node;
4573 type1 = TREE_TYPE (op1);
4574 code1 = TREE_CODE (type1);
4575 type2 = TREE_TYPE (op2);
4576 code2 = TREE_CODE (type2);
4578 /* C90 does not permit non-lvalue arrays in conditional expressions.
4579 In C99 they will be pointers by now. */
4580 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4582 error_at (colon_loc, "non-lvalue array in conditional expression");
4583 return error_mark_node;
4586 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4587 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4588 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4589 || code1 == COMPLEX_TYPE)
4590 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4591 || code2 == COMPLEX_TYPE))
4593 semantic_result_type = c_common_type (type1, type2);
4594 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4596 op1 = TREE_OPERAND (op1, 0);
4597 type1 = TREE_TYPE (op1);
4598 gcc_assert (TREE_CODE (type1) == code1);
4600 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4602 op2 = TREE_OPERAND (op2, 0);
4603 type2 = TREE_TYPE (op2);
4604 gcc_assert (TREE_CODE (type2) == code2);
4608 if (warn_cxx_compat)
4610 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4611 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4613 if (TREE_CODE (t1) == ENUMERAL_TYPE
4614 && TREE_CODE (t2) == ENUMERAL_TYPE
4615 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4616 warning_at (colon_loc, OPT_Wc___compat,
4617 ("different enum types in conditional is "
4618 "invalid in C++: %qT vs %qT"),
4619 t1, t2);
4622 /* Quickly detect the usual case where op1 and op2 have the same type
4623 after promotion. */
4624 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4626 if (type1 == type2)
4627 result_type = type1;
4628 else
4629 result_type = TYPE_MAIN_VARIANT (type1);
4631 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4632 || code1 == COMPLEX_TYPE)
4633 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4634 || code2 == COMPLEX_TYPE))
4636 result_type = c_common_type (type1, type2);
4637 do_warn_double_promotion (result_type, type1, type2,
4638 "implicit conversion from %qT to %qT to "
4639 "match other result of conditional",
4640 colon_loc);
4642 /* If -Wsign-compare, warn here if type1 and type2 have
4643 different signedness. We'll promote the signed to unsigned
4644 and later code won't know it used to be different.
4645 Do this check on the original types, so that explicit casts
4646 will be considered, but default promotions won't. */
4647 if (c_inhibit_evaluation_warnings == 0)
4649 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4650 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4652 if (unsigned_op1 ^ unsigned_op2)
4654 bool ovf;
4656 /* Do not warn if the result type is signed, since the
4657 signed type will only be chosen if it can represent
4658 all the values of the unsigned type. */
4659 if (!TYPE_UNSIGNED (result_type))
4660 /* OK */;
4661 else
4663 bool op1_maybe_const = true;
4664 bool op2_maybe_const = true;
4666 /* Do not warn if the signed quantity is an
4667 unsuffixed integer literal (or some static
4668 constant expression involving such literals) and
4669 it is non-negative. This warning requires the
4670 operands to be folded for best results, so do
4671 that folding in this case even without
4672 warn_sign_compare to avoid warning options
4673 possibly affecting code generation. */
4674 c_inhibit_evaluation_warnings
4675 += (ifexp == truthvalue_false_node);
4676 op1 = c_fully_fold (op1, require_constant_value,
4677 &op1_maybe_const);
4678 c_inhibit_evaluation_warnings
4679 -= (ifexp == truthvalue_false_node);
4681 c_inhibit_evaluation_warnings
4682 += (ifexp == truthvalue_true_node);
4683 op2 = c_fully_fold (op2, require_constant_value,
4684 &op2_maybe_const);
4685 c_inhibit_evaluation_warnings
4686 -= (ifexp == truthvalue_true_node);
4688 if (warn_sign_compare)
4690 if ((unsigned_op2
4691 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4692 || (unsigned_op1
4693 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4694 /* OK */;
4695 else
4696 warning_at (colon_loc, OPT_Wsign_compare,
4697 ("signed and unsigned type in "
4698 "conditional expression"));
4700 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4701 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4702 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4703 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4708 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4710 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4711 pedwarn (colon_loc, OPT_Wpedantic,
4712 "ISO C forbids conditional expr with only one void side");
4713 result_type = void_type_node;
4715 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4717 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4718 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4719 addr_space_t as_common;
4721 if (comp_target_types (colon_loc, type1, type2))
4722 result_type = common_pointer_type (type1, type2);
4723 else if (null_pointer_constant_p (orig_op1))
4724 result_type = type2;
4725 else if (null_pointer_constant_p (orig_op2))
4726 result_type = type1;
4727 else if (!addr_space_superset (as1, as2, &as_common))
4729 error_at (colon_loc, "pointers to disjoint address spaces "
4730 "used in conditional expression");
4731 return error_mark_node;
4733 else if (VOID_TYPE_P (TREE_TYPE (type1))
4734 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4736 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4737 pedwarn (colon_loc, OPT_Wpedantic,
4738 "ISO C forbids conditional expr between "
4739 "%<void *%> and function pointer");
4740 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4741 TREE_TYPE (type2)));
4743 else if (VOID_TYPE_P (TREE_TYPE (type2))
4744 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4746 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4747 pedwarn (colon_loc, OPT_Wpedantic,
4748 "ISO C forbids conditional expr between "
4749 "%<void *%> and function pointer");
4750 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4751 TREE_TYPE (type1)));
4753 /* Objective-C pointer comparisons are a bit more lenient. */
4754 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4755 result_type = objc_common_type (type1, type2);
4756 else
4758 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4760 pedwarn (colon_loc, 0,
4761 "pointer type mismatch in conditional expression");
4762 result_type = build_pointer_type
4763 (build_qualified_type (void_type_node, qual));
4766 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4768 if (!null_pointer_constant_p (orig_op2))
4769 pedwarn (colon_loc, 0,
4770 "pointer/integer type mismatch in conditional expression");
4771 else
4773 op2 = !upc_shared_type_p (TREE_TYPE (type1))
4774 ? null_pointer_node : upc_null_pts_node;
4776 result_type = type1;
4778 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4780 if (!null_pointer_constant_p (orig_op1))
4781 pedwarn (colon_loc, 0,
4782 "pointer/integer type mismatch in conditional expression");
4783 else
4785 op1 = !upc_shared_type_p (TREE_TYPE (type2))
4786 ? null_pointer_node : upc_null_pts_node;
4788 result_type = type2;
4791 if (!result_type)
4793 if (flag_cond_mismatch)
4794 result_type = void_type_node;
4795 else
4797 error_at (colon_loc, "type mismatch in conditional expression");
4798 return error_mark_node;
4802 /* Merge const and volatile flags of the incoming types. */
4803 result_type
4804 = build_type_variant (result_type,
4805 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4806 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4808 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4809 semantic_result_type);
4810 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4811 semantic_result_type);
4813 if (ifexp_bcp && ifexp == truthvalue_true_node)
4815 op2_int_operands = true;
4816 op1 = c_fully_fold (op1, require_constant_value, NULL);
4818 if (ifexp_bcp && ifexp == truthvalue_false_node)
4820 op1_int_operands = true;
4821 op2 = c_fully_fold (op2, require_constant_value, NULL);
4823 int_const = int_operands = (ifexp_int_operands
4824 && op1_int_operands
4825 && op2_int_operands);
4826 if (int_operands)
4828 int_const = ((ifexp == truthvalue_true_node
4829 && TREE_CODE (orig_op1) == INTEGER_CST
4830 && !TREE_OVERFLOW (orig_op1))
4831 || (ifexp == truthvalue_false_node
4832 && TREE_CODE (orig_op2) == INTEGER_CST
4833 && !TREE_OVERFLOW (orig_op2)));
4835 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4836 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4837 else
4839 if (int_operands)
4841 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4842 nested inside of the expression. */
4843 op1 = c_fully_fold (op1, false, NULL);
4844 op2 = c_fully_fold (op2, false, NULL);
4846 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4847 if (int_operands)
4848 ret = note_integer_operands (ret);
4850 if (semantic_result_type)
4851 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4853 protected_set_expr_location (ret, colon_loc);
4854 return ret;
4857 /* Return a compound expression that performs two expressions and
4858 returns the value of the second of them.
4860 LOC is the location of the COMPOUND_EXPR. */
4862 tree
4863 build_compound_expr (location_t loc, tree expr1, tree expr2)
4865 bool expr1_int_operands, expr2_int_operands;
4866 tree eptype = NULL_TREE;
4867 tree ret;
4869 if (flag_cilkplus
4870 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4871 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4873 error_at (loc,
4874 "spawned function call cannot be part of a comma expression");
4875 return error_mark_node;
4877 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4878 if (expr1_int_operands)
4879 expr1 = remove_c_maybe_const_expr (expr1);
4880 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4881 if (expr2_int_operands)
4882 expr2 = remove_c_maybe_const_expr (expr2);
4884 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4885 expr1 = TREE_OPERAND (expr1, 0);
4886 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4888 eptype = TREE_TYPE (expr2);
4889 expr2 = TREE_OPERAND (expr2, 0);
4892 if (!TREE_SIDE_EFFECTS (expr1))
4894 /* The left-hand operand of a comma expression is like an expression
4895 statement: with -Wunused, we should warn if it doesn't have
4896 any side-effects, unless it was explicitly cast to (void). */
4897 if (warn_unused_value)
4899 if (VOID_TYPE_P (TREE_TYPE (expr1))
4900 && CONVERT_EXPR_P (expr1))
4901 ; /* (void) a, b */
4902 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4903 && TREE_CODE (expr1) == COMPOUND_EXPR
4904 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4905 ; /* (void) a, (void) b, c */
4906 else
4907 warning_at (loc, OPT_Wunused_value,
4908 "left-hand operand of comma expression has no effect");
4911 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4912 && warn_unused_value)
4914 tree r = expr1;
4915 location_t cloc = loc;
4916 while (TREE_CODE (r) == COMPOUND_EXPR)
4918 if (EXPR_HAS_LOCATION (r))
4919 cloc = EXPR_LOCATION (r);
4920 r = TREE_OPERAND (r, 1);
4922 if (!TREE_SIDE_EFFECTS (r)
4923 && !VOID_TYPE_P (TREE_TYPE (r))
4924 && !CONVERT_EXPR_P (r))
4925 warning_at (cloc, OPT_Wunused_value,
4926 "right-hand operand of comma expression has no effect");
4929 /* With -Wunused, we should also warn if the left-hand operand does have
4930 side-effects, but computes a value which is not used. For example, in
4931 `foo() + bar(), baz()' the result of the `+' operator is not used,
4932 so we should issue a warning. */
4933 else if (warn_unused_value)
4934 warn_if_unused_value (expr1, loc);
4936 if (expr2 == error_mark_node)
4937 return error_mark_node;
4939 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4941 if (flag_isoc99
4942 && expr1_int_operands
4943 && expr2_int_operands)
4944 ret = note_integer_operands (ret);
4946 if (eptype)
4947 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4949 protected_set_expr_location (ret, loc);
4950 return ret;
4953 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4954 which we are casting. OTYPE is the type of the expression being
4955 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4956 of the cast. -Wcast-qual appeared on the command line. Named
4957 address space qualifiers are not handled here, because they result
4958 in different warnings. */
4960 static void
4961 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4963 tree in_type = type;
4964 tree in_otype = otype;
4965 int added = 0;
4966 int discarded = 0;
4967 bool is_const;
4969 /* Check that the qualifiers on IN_TYPE are a superset of the
4970 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4971 nodes is uninteresting and we stop as soon as we hit a
4972 non-POINTER_TYPE node on either type. */
4975 in_otype = TREE_TYPE (in_otype);
4976 in_type = TREE_TYPE (in_type);
4978 /* GNU C allows cv-qualified function types. 'const' means the
4979 function is very pure, 'volatile' means it can't return. We
4980 need to warn when such qualifiers are added, not when they're
4981 taken away. */
4982 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4983 && TREE_CODE (in_type) == FUNCTION_TYPE)
4984 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4985 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4986 else
4987 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4988 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4990 while (TREE_CODE (in_type) == POINTER_TYPE
4991 && TREE_CODE (in_otype) == POINTER_TYPE);
4993 if (added)
4994 warning_at (loc, OPT_Wcast_qual,
4995 "cast adds %q#v qualifier to function type", added);
4997 if (discarded)
4998 /* There are qualifiers present in IN_OTYPE that are not present
4999 in IN_TYPE. */
5000 warning_at (loc, OPT_Wcast_qual,
5001 "cast discards %qv qualifier from pointer target type",
5002 discarded);
5004 if (added || discarded)
5005 return;
5007 /* A cast from **T to const **T is unsafe, because it can cause a
5008 const value to be changed with no additional warning. We only
5009 issue this warning if T is the same on both sides, and we only
5010 issue the warning if there are the same number of pointers on
5011 both sides, as otherwise the cast is clearly unsafe anyhow. A
5012 cast is unsafe when a qualifier is added at one level and const
5013 is not present at all outer levels.
5015 To issue this warning, we check at each level whether the cast
5016 adds new qualifiers not already seen. We don't need to special
5017 case function types, as they won't have the same
5018 TYPE_MAIN_VARIANT. */
5020 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5021 return;
5022 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5023 return;
5025 in_type = type;
5026 in_otype = otype;
5027 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5030 in_type = TREE_TYPE (in_type);
5031 in_otype = TREE_TYPE (in_otype);
5032 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5033 && !is_const)
5035 warning_at (loc, OPT_Wcast_qual,
5036 "to be safe all intermediate pointers in cast from "
5037 "%qT to %qT must be %<const%> qualified",
5038 otype, type);
5039 break;
5041 if (is_const)
5042 is_const = TYPE_READONLY (in_type);
5044 while (TREE_CODE (in_type) == POINTER_TYPE);
5047 /* Build an expression representing a cast to type TYPE of expression EXPR.
5048 LOC is the location of the cast-- typically the open paren of the cast. */
5050 tree
5051 build_c_cast (location_t loc, tree type, tree expr)
5053 tree value;
5055 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5056 expr = TREE_OPERAND (expr, 0);
5058 value = expr;
5060 if (type == error_mark_node || expr == error_mark_node)
5061 return error_mark_node;
5063 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5064 only in <protocol> qualifications. But when constructing cast expressions,
5065 the protocols do matter and must be kept around. */
5066 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5067 return build1 (NOP_EXPR, type, expr);
5069 if (upc_shared_type_p (type))
5071 error ("UPC does not allow casts to a shared type");
5072 return error_mark_node;
5075 type = TYPE_MAIN_VARIANT (type);
5077 if (TREE_CODE (type) == ARRAY_TYPE)
5079 error_at (loc, "cast specifies array type");
5080 return error_mark_node;
5083 if (TREE_CODE (type) == FUNCTION_TYPE)
5085 error_at (loc, "cast specifies function type");
5086 return error_mark_node;
5089 if (!VOID_TYPE_P (type))
5091 value = require_complete_type (value);
5092 if (value == error_mark_node)
5093 return error_mark_node;
5096 if (integer_zerop (value)
5097 && POINTER_TYPE_P (type)
5098 && upc_shared_type_p (TREE_TYPE (type))
5099 && POINTER_TYPE_P (TREE_TYPE (expr))
5100 && ! upc_shared_type_p (TREE_TYPE (TREE_TYPE (expr))))
5102 value = upc_null_pts_node;
5105 if (!upc_shared_type_p (type) && upc_shared_type_p (TREE_TYPE (expr)))
5107 /* UPC disallows things like:
5108 (int)p = <expr>; (where p is a shared int) */
5109 value = non_lvalue (value);
5112 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5114 if (TREE_CODE (type) == RECORD_TYPE
5115 || TREE_CODE (type) == UNION_TYPE)
5116 pedwarn (loc, OPT_Wpedantic,
5117 "ISO C forbids casting nonscalar to the same type");
5119 else if (TREE_CODE (type) == UNION_TYPE)
5121 tree field;
5123 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5124 if (TREE_TYPE (field) != error_mark_node
5125 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5126 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5127 break;
5129 if (field)
5131 tree t;
5132 bool maybe_const = true;
5134 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5135 t = c_fully_fold (value, false, &maybe_const);
5136 t = build_constructor_single (type, field, t);
5137 if (!maybe_const)
5138 t = c_wrap_maybe_const (t, true);
5139 t = digest_init (loc, type, t,
5140 NULL_TREE, false, true, 0);
5141 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5142 return t;
5144 error_at (loc, "cast to union type from type not present in union");
5145 return error_mark_node;
5147 else
5149 tree otype, ovalue;
5151 if (type == void_type_node)
5153 tree t = build1 (CONVERT_EXPR, type, value);
5154 SET_EXPR_LOCATION (t, loc);
5155 return t;
5158 otype = TREE_TYPE (value);
5160 if (TREE_CODE (type) == POINTER_TYPE
5161 && TREE_CODE (otype) == POINTER_TYPE)
5163 int t_shared = upc_shared_type_p (TREE_TYPE (type));
5164 int o_shared = upc_shared_type_p (TREE_TYPE (otype));
5165 if ((!t_shared && o_shared)
5166 || (t_shared && o_shared
5167 && !lang_hooks.types_compatible_p (type, otype)))
5168 return build1 (CONVERT_EXPR, type, value);
5171 /* Optionally warn about potentially worrisome casts. */
5172 if (warn_cast_qual
5173 && TREE_CODE (type) == POINTER_TYPE
5174 && TREE_CODE (otype) == POINTER_TYPE)
5175 handle_warn_cast_qual (loc, type, otype);
5177 /* Warn about conversions between pointers to disjoint
5178 address spaces. */
5179 if (TREE_CODE (type) == POINTER_TYPE
5180 && TREE_CODE (otype) == POINTER_TYPE
5181 && !null_pointer_constant_p (value))
5183 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5184 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5185 addr_space_t as_common;
5187 if (!addr_space_superset (as_to, as_from, &as_common))
5189 if (ADDR_SPACE_GENERIC_P (as_from))
5190 warning_at (loc, 0, "cast to %s address space pointer "
5191 "from disjoint generic address space pointer",
5192 c_addr_space_name (as_to));
5194 else if (ADDR_SPACE_GENERIC_P (as_to))
5195 warning_at (loc, 0, "cast to generic address space pointer "
5196 "from disjoint %s address space pointer",
5197 c_addr_space_name (as_from));
5199 else
5200 warning_at (loc, 0, "cast to %s address space pointer "
5201 "from disjoint %s address space pointer",
5202 c_addr_space_name (as_to),
5203 c_addr_space_name (as_from));
5207 /* Warn about possible alignment problems. */
5208 if (STRICT_ALIGNMENT
5209 && TREE_CODE (type) == POINTER_TYPE
5210 && TREE_CODE (otype) == POINTER_TYPE
5211 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5212 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5213 /* Don't warn about opaque types, where the actual alignment
5214 restriction is unknown. */
5215 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5216 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5217 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5218 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5219 warning_at (loc, OPT_Wcast_align,
5220 "cast increases required alignment of target type");
5222 if (POINTER_TYPE_P (type)
5223 && upc_shared_type_p (TREE_TYPE (type))
5224 && POINTER_TYPE_P (otype)
5225 && !upc_shared_type_p (TREE_TYPE (otype)))
5227 error_at (loc, "UPC does not allow casts from a local pointer to a pointer-to-shared");
5228 return error_mark_node;
5231 if (TREE_CODE (type) == POINTER_TYPE
5232 && TREE_CODE (otype) == INTEGER_TYPE
5233 && upc_shared_type_p (TREE_TYPE (type))
5234 && !integer_zerop (value))
5236 error_at (loc, "UPC does not allow casts from an integer to a pointer-to-shared");
5237 return error_mark_node;
5240 if (TREE_CODE (type) == INTEGER_TYPE
5241 && TREE_CODE (otype) == POINTER_TYPE
5242 && upc_shared_type_p (TREE_TYPE (otype)))
5244 /* UPC pointer-to-shared -> integer
5245 This will be lowered by the genericize pass. */
5246 return build1 (CONVERT_EXPR, type, value);
5249 if (TREE_CODE (type) == INTEGER_TYPE
5250 && TREE_CODE (otype) == POINTER_TYPE
5251 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5252 /* Unlike conversion of integers to pointers, where the
5253 warning is disabled for converting constants because
5254 of cases such as SIG_*, warn about converting constant
5255 pointers to integers. In some cases it may cause unwanted
5256 sign extension, and a warning is appropriate. */
5257 warning_at (loc, OPT_Wpointer_to_int_cast,
5258 "cast from pointer to integer of different size");
5260 if (TREE_CODE (value) == CALL_EXPR
5261 && TREE_CODE (type) != TREE_CODE (otype))
5262 warning_at (loc, OPT_Wbad_function_cast,
5263 "cast from function call of type %qT "
5264 "to non-matching type %qT", otype, type);
5266 if (TREE_CODE (type) == POINTER_TYPE
5267 && TREE_CODE (otype) == INTEGER_TYPE
5268 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5269 /* Don't warn about converting any constant. */
5270 && !TREE_CONSTANT (value))
5271 warning_at (loc,
5272 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5273 "of different size");
5275 if (warn_strict_aliasing <= 2)
5276 strict_aliasing_warning (otype, type, expr);
5278 /* If pedantic, warn for conversions between function and object
5279 pointer types, except for converting a null pointer constant
5280 to function pointer type. */
5281 if (pedantic
5282 && TREE_CODE (type) == POINTER_TYPE
5283 && TREE_CODE (otype) == POINTER_TYPE
5284 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5285 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5286 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5287 "conversion of function pointer to object pointer type");
5289 if (pedantic
5290 && TREE_CODE (type) == POINTER_TYPE
5291 && TREE_CODE (otype) == POINTER_TYPE
5292 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5293 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5294 && !null_pointer_constant_p (value))
5295 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5296 "conversion of object pointer to function pointer type");
5298 ovalue = value;
5299 value = convert (type, value);
5301 /* Ignore any integer overflow caused by the cast. */
5302 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5304 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5306 if (!TREE_OVERFLOW (value))
5308 /* Avoid clobbering a shared constant. */
5309 value = copy_node (value);
5310 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5313 else if (TREE_OVERFLOW (value))
5314 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5315 value = wide_int_to_tree (TREE_TYPE (value), value);
5319 /* Don't let a cast be an lvalue. */
5320 if (value == expr)
5321 value = non_lvalue_loc (loc, value);
5323 /* Don't allow the results of casting to floating-point or complex
5324 types be confused with actual constants, or casts involving
5325 integer and pointer types other than direct integer-to-integer
5326 and integer-to-pointer be confused with integer constant
5327 expressions and null pointer constants. */
5328 if (TREE_CODE (value) == REAL_CST
5329 || TREE_CODE (value) == COMPLEX_CST
5330 || (TREE_CODE (value) == INTEGER_CST
5331 && !((TREE_CODE (expr) == INTEGER_CST
5332 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5333 || TREE_CODE (expr) == REAL_CST
5334 || TREE_CODE (expr) == COMPLEX_CST)))
5335 value = build1 (NOP_EXPR, type, value);
5337 if (CAN_HAVE_LOCATION_P (value))
5338 SET_EXPR_LOCATION (value, loc);
5339 return value;
5342 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5343 location of the open paren of the cast, or the position of the cast
5344 expr. */
5345 tree
5346 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5348 tree type;
5349 tree type_expr = NULL_TREE;
5350 bool type_expr_const = true;
5351 tree ret;
5352 int saved_wsp = warn_strict_prototypes;
5354 /* This avoids warnings about unprototyped casts on
5355 integers. E.g. "#define SIG_DFL (void(*)())0". */
5356 if (TREE_CODE (expr) == INTEGER_CST)
5357 warn_strict_prototypes = 0;
5358 type = groktypename (type_name, &type_expr, &type_expr_const);
5359 warn_strict_prototypes = saved_wsp;
5361 ret = build_c_cast (loc, type, expr);
5362 if (type_expr)
5364 bool inner_expr_const = true;
5365 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5366 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5367 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5368 && inner_expr_const);
5369 SET_EXPR_LOCATION (ret, loc);
5372 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5373 SET_EXPR_LOCATION (ret, loc);
5375 /* C++ does not permits types to be defined in a cast, but it
5376 allows references to incomplete types. */
5377 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5378 warning_at (loc, OPT_Wc___compat,
5379 "defining a type in a cast is invalid in C++");
5381 return ret;
5384 /* Build an assignment expression of lvalue LHS from value RHS.
5385 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5386 may differ from TREE_TYPE (LHS) for an enum bitfield.
5387 MODIFYCODE is the code for a binary operator that we use
5388 to combine the old value of LHS with RHS to get the new value.
5389 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5390 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5391 which may differ from TREE_TYPE (RHS) for an enum value.
5393 LOCATION is the location of the MODIFYCODE operator.
5394 RHS_LOC is the location of the RHS. */
5396 tree
5397 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5398 enum tree_code modifycode,
5399 location_t rhs_loc, tree rhs, tree rhs_origtype)
5401 tree result;
5402 tree newrhs;
5403 tree rhseval = NULL_TREE;
5404 tree rhs_semantic_type = NULL_TREE;
5405 tree lhstype = TREE_TYPE (lhs);
5406 tree olhstype = lhstype;
5407 bool npc;
5408 bool is_atomic_op;
5410 /* Types that aren't fully specified cannot be used in assignments. */
5411 lhs = require_complete_type (lhs);
5413 /* Avoid duplicate error messages from operands that had errors. */
5414 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5415 return error_mark_node;
5417 /* Ensure an error for assigning a non-lvalue array to an array in
5418 C90. */
5419 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5421 error_at (location, "assignment to expression with array type");
5422 return error_mark_node;
5425 /* For ObjC properties, defer this check. */
5426 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5427 return error_mark_node;
5429 is_atomic_op = really_atomic_lvalue (lhs);
5431 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5433 rhs_semantic_type = TREE_TYPE (rhs);
5434 rhs = TREE_OPERAND (rhs, 0);
5437 newrhs = rhs;
5439 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5441 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5442 lhs_origtype, modifycode, rhs_loc, rhs,
5443 rhs_origtype);
5444 if (inner == error_mark_node)
5445 return error_mark_node;
5446 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5447 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5448 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5449 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5450 protected_set_expr_location (result, location);
5451 return result;
5454 /* If a binary op has been requested, combine the old LHS value with the RHS
5455 producing the value we should actually store into the LHS. */
5457 if (modifycode != NOP_EXPR)
5459 lhs = c_fully_fold (lhs, false, NULL);
5460 lhs = stabilize_reference (lhs);
5462 /* Construct the RHS for any non-atomic compound assignemnt. */
5463 if (!is_atomic_op)
5465 /* If in LHS op= RHS the RHS has side-effects, ensure they
5466 are preevaluated before the rest of the assignment expression's
5467 side-effects, because RHS could contain e.g. function calls
5468 that modify LHS. */
5469 if (TREE_SIDE_EFFECTS (rhs))
5471 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5472 rhseval = newrhs;
5474 newrhs = build_binary_op (location,
5475 modifycode, lhs, newrhs, 1);
5477 /* The original type of the right hand side is no longer
5478 meaningful. */
5479 rhs_origtype = NULL_TREE;
5483 if (c_dialect_objc ())
5485 /* Check if we are modifying an Objective-C property reference;
5486 if so, we need to generate setter calls. */
5487 result = objc_maybe_build_modify_expr (lhs, newrhs);
5488 if (result)
5489 goto return_result;
5491 /* Else, do the check that we postponed for Objective-C. */
5492 if (!lvalue_or_else (location, lhs, lv_assign))
5493 return error_mark_node;
5496 /* Give an error for storing in something that is 'const'. */
5498 if (TYPE_READONLY (lhstype)
5499 || ((TREE_CODE (lhstype) == RECORD_TYPE
5500 || TREE_CODE (lhstype) == UNION_TYPE)
5501 && C_TYPE_FIELDS_READONLY (lhstype)))
5503 readonly_error (location, lhs, lv_assign);
5504 return error_mark_node;
5506 else if (TREE_READONLY (lhs))
5507 readonly_warning (lhs, lv_assign);
5509 /* If storing into a structure or union member,
5510 it has probably been given type `int'.
5511 Compute the type that would go with
5512 the actual amount of storage the member occupies. */
5514 if (TREE_CODE (lhs) == COMPONENT_REF
5515 && (TREE_CODE (lhstype) == INTEGER_TYPE
5516 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5517 || TREE_CODE (lhstype) == REAL_TYPE
5518 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5519 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5521 /* If storing in a field that is in actuality a short or narrower than one,
5522 we must store in the field in its actual type. */
5524 if (lhstype != TREE_TYPE (lhs))
5526 lhs = copy_node (lhs);
5527 TREE_TYPE (lhs) = lhstype;
5530 /* Issue -Wc++-compat warnings about an assignment to an enum type
5531 when LHS does not have its original type. This happens for,
5532 e.g., an enum bitfield in a struct. */
5533 if (warn_cxx_compat
5534 && lhs_origtype != NULL_TREE
5535 && lhs_origtype != lhstype
5536 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5538 tree checktype = (rhs_origtype != NULL_TREE
5539 ? rhs_origtype
5540 : TREE_TYPE (rhs));
5541 if (checktype != error_mark_node
5542 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5543 || (is_atomic_op && modifycode != NOP_EXPR)))
5544 warning_at (location, OPT_Wc___compat,
5545 "enum conversion in assignment is invalid in C++");
5548 /* If the lhs is atomic, remove that qualifier. */
5549 if (is_atomic_op)
5551 lhstype = build_qualified_type (lhstype,
5552 (TYPE_QUALS (lhstype)
5553 & ~TYPE_QUAL_ATOMIC));
5554 olhstype = build_qualified_type (olhstype,
5555 (TYPE_QUALS (lhstype)
5556 & ~TYPE_QUAL_ATOMIC));
5559 /* Convert new value to destination type. Fold it first, then
5560 restore any excess precision information, for the sake of
5561 conversion warnings. */
5563 if (!(is_atomic_op && modifycode != NOP_EXPR))
5565 npc = null_pointer_constant_p (newrhs);
5566 newrhs = c_fully_fold (newrhs, false, NULL);
5567 if (rhs_semantic_type)
5568 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5569 /* If the lhs is UPC 'shared' qualified, we drop the qualifier
5570 for the purposes of conversions from rhstype to lhstype.
5571 This will prevent the inadvertent creation of temporaries
5572 with "shared" asserted. */
5573 if (upc_shared_type_p (lhstype))
5574 lhstype = build_upc_unshared_type (lhstype);
5575 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5576 rhs_origtype, ic_assign, npc,
5577 NULL_TREE, NULL_TREE, 0);
5578 if (TREE_CODE (newrhs) == ERROR_MARK)
5579 return error_mark_node;
5582 /* Emit ObjC write barrier, if necessary. */
5583 if (c_dialect_objc () && flag_objc_gc)
5585 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5586 if (result)
5588 protected_set_expr_location (result, location);
5589 goto return_result;
5593 /* Scan operands. */
5595 if (is_atomic_op)
5596 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5597 else
5599 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5600 TREE_SIDE_EFFECTS (result) = 1;
5601 protected_set_expr_location (result, location);
5604 /* If we got the LHS in a different type for storing in,
5605 convert the result back to the nominal type of LHS
5606 so that the value we return always has the same type
5607 as the LHS argument. */
5609 if (olhstype == TREE_TYPE (result))
5610 goto return_result;
5612 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5613 rhs_origtype, ic_assign, false, NULL_TREE,
5614 NULL_TREE, 0);
5615 protected_set_expr_location (result, location);
5617 return_result:
5618 if (rhseval)
5619 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5620 return result;
5623 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5624 This is used to implement -fplan9-extensions. */
5626 static bool
5627 find_anonymous_field_with_type (tree struct_type, tree type)
5629 tree field;
5630 bool found;
5632 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5633 || TREE_CODE (struct_type) == UNION_TYPE);
5634 found = false;
5635 for (field = TYPE_FIELDS (struct_type);
5636 field != NULL_TREE;
5637 field = TREE_CHAIN (field))
5639 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5640 ? c_build_qualified_type (TREE_TYPE (field),
5641 TYPE_QUAL_ATOMIC)
5642 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5643 if (DECL_NAME (field) == NULL
5644 && comptypes (type, fieldtype))
5646 if (found)
5647 return false;
5648 found = true;
5650 else if (DECL_NAME (field) == NULL
5651 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5652 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5653 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5655 if (found)
5656 return false;
5657 found = true;
5660 return found;
5663 /* RHS is an expression whose type is pointer to struct. If there is
5664 an anonymous field in RHS with type TYPE, then return a pointer to
5665 that field in RHS. This is used with -fplan9-extensions. This
5666 returns NULL if no conversion could be found. */
5668 static tree
5669 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5671 tree rhs_struct_type, lhs_main_type;
5672 tree field, found_field;
5673 bool found_sub_field;
5674 tree ret;
5676 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5677 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5678 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5679 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5681 gcc_assert (POINTER_TYPE_P (type));
5682 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5683 ? c_build_qualified_type (TREE_TYPE (type),
5684 TYPE_QUAL_ATOMIC)
5685 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5687 found_field = NULL_TREE;
5688 found_sub_field = false;
5689 for (field = TYPE_FIELDS (rhs_struct_type);
5690 field != NULL_TREE;
5691 field = TREE_CHAIN (field))
5693 if (DECL_NAME (field) != NULL_TREE
5694 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5695 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5696 continue;
5697 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5698 ? c_build_qualified_type (TREE_TYPE (field),
5699 TYPE_QUAL_ATOMIC)
5700 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5701 if (comptypes (lhs_main_type, fieldtype))
5703 if (found_field != NULL_TREE)
5704 return NULL_TREE;
5705 found_field = field;
5707 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5708 lhs_main_type))
5710 if (found_field != NULL_TREE)
5711 return NULL_TREE;
5712 found_field = field;
5713 found_sub_field = true;
5717 if (found_field == NULL_TREE)
5718 return NULL_TREE;
5720 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5721 build_fold_indirect_ref (rhs), found_field,
5722 NULL_TREE);
5723 ret = build_fold_addr_expr_loc (location, ret);
5725 if (found_sub_field)
5727 ret = convert_to_anonymous_field (location, type, ret);
5728 gcc_assert (ret != NULL_TREE);
5731 return ret;
5734 /* Issue an error message for a bad initializer component.
5735 GMSGID identifies the message.
5736 The component name is taken from the spelling stack. */
5738 static void
5739 error_init (location_t loc, const char *gmsgid)
5741 char *ofwhat;
5743 /* The gmsgid may be a format string with %< and %>. */
5744 error_at (loc, gmsgid);
5745 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5746 if (*ofwhat)
5747 inform (loc, "(near initialization for %qs)", ofwhat);
5750 /* Issue a pedantic warning for a bad initializer component. OPT is
5751 the option OPT_* (from options.h) controlling this warning or 0 if
5752 it is unconditionally given. GMSGID identifies the message. The
5753 component name is taken from the spelling stack. */
5755 static void
5756 pedwarn_init (location_t location, int opt, const char *gmsgid)
5758 char *ofwhat;
5759 bool warned;
5761 /* The gmsgid may be a format string with %< and %>. */
5762 warned = pedwarn (location, opt, gmsgid);
5763 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5764 if (*ofwhat && warned)
5765 inform (location, "(near initialization for %qs)", ofwhat);
5768 /* Issue a warning for a bad initializer component.
5770 OPT is the OPT_W* value corresponding to the warning option that
5771 controls this warning. GMSGID identifies the message. The
5772 component name is taken from the spelling stack. */
5774 static void
5775 warning_init (location_t loc, int opt, const char *gmsgid)
5777 char *ofwhat;
5778 bool warned;
5780 /* The gmsgid may be a format string with %< and %>. */
5781 warned = warning_at (loc, opt, gmsgid);
5782 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5783 if (*ofwhat && warned)
5784 inform (loc, "(near initialization for %qs)", ofwhat);
5787 /* If TYPE is an array type and EXPR is a parenthesized string
5788 constant, warn if pedantic that EXPR is being used to initialize an
5789 object of type TYPE. */
5791 void
5792 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5794 if (pedantic
5795 && TREE_CODE (type) == ARRAY_TYPE
5796 && TREE_CODE (expr.value) == STRING_CST
5797 && expr.original_code != STRING_CST)
5798 pedwarn_init (loc, OPT_Wpedantic,
5799 "array initialized from parenthesized string constant");
5802 /* Convert value RHS to type TYPE as preparation for an assignment to
5803 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5804 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5805 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5806 constant before any folding.
5807 The real work of conversion is done by `convert'.
5808 The purpose of this function is to generate error messages
5809 for assignments that are not allowed in C.
5810 ERRTYPE says whether it is argument passing, assignment,
5811 initialization or return.
5813 LOCATION is the location of the assignment, EXPR_LOC is the location of
5814 the RHS or, for a function, location of an argument.
5815 FUNCTION is a tree for the function being called.
5816 PARMNUM is the number of the argument, for printing in error messages. */
5818 static tree
5819 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5820 tree rhs, tree origtype, enum impl_conv errtype,
5821 bool null_pointer_constant, tree fundecl,
5822 tree function, int parmnum)
5824 enum tree_code codel = TREE_CODE (type);
5825 tree orig_rhs = rhs;
5826 tree rhstype;
5827 enum tree_code coder;
5828 tree rname = NULL_TREE;
5829 bool objc_ok = false;
5831 if (errtype == ic_argpass)
5833 tree selector;
5834 /* Change pointer to function to the function itself for
5835 diagnostics. */
5836 if (TREE_CODE (function) == ADDR_EXPR
5837 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5838 function = TREE_OPERAND (function, 0);
5840 /* Handle an ObjC selector specially for diagnostics. */
5841 selector = objc_message_selector ();
5842 rname = function;
5843 if (selector && parmnum > 2)
5845 rname = selector;
5846 parmnum -= 2;
5850 /* This macro is used to emit diagnostics to ensure that all format
5851 strings are complete sentences, visible to gettext and checked at
5852 compile time. */
5853 #define WARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5854 do { \
5855 switch (errtype) \
5857 case ic_argpass: \
5858 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5859 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5860 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5861 "expected %qT but argument is of type %qT", \
5862 type, rhstype); \
5863 break; \
5864 case ic_assign: \
5865 pedwarn (LOCATION, OPT, AS); \
5866 break; \
5867 case ic_init: \
5868 pedwarn_init (LOCATION, OPT, IN); \
5869 break; \
5870 case ic_return: \
5871 pedwarn (LOCATION, OPT, RE); \
5872 break; \
5873 default: \
5874 gcc_unreachable (); \
5876 } while (0)
5878 /* Similar to WARN_FOR_ASSIGNMENT, but used to diagnose certain
5879 error conditions defined by the UPC language specification
5880 when converting between pointer-to-shared types and other types. */
5881 #define ERROR_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5882 do { \
5883 switch (errtype) \
5885 case ic_argpass: \
5886 error_at (LOCATION, AR, parmnum, rname); \
5887 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5888 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5889 "expected %qT but argument is of type %qT", \
5890 type, rhstype); \
5891 break; \
5892 case ic_assign: \
5893 error_at (LOCATION, AS); \
5894 break; \
5895 case ic_init: \
5896 error_at (LOCATION, IN); \
5897 break; \
5898 case ic_return: \
5899 error_at (LOCATION, RE); \
5900 break; \
5901 default: \
5902 gcc_unreachable (); \
5904 } while (0)
5906 /* This macro is used to emit diagnostics to ensure that all format
5907 strings are complete sentences, visible to gettext and checked at
5908 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5909 extra parameter to enumerate qualifiers. */
5911 #define WARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5912 do { \
5913 switch (errtype) \
5915 case ic_argpass: \
5916 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5917 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5918 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5919 "expected %qT but argument is of type %qT", \
5920 type, rhstype); \
5921 break; \
5922 case ic_assign: \
5923 pedwarn (LOCATION, OPT, AS, QUALS); \
5924 break; \
5925 case ic_init: \
5926 pedwarn (LOCATION, OPT, IN, QUALS); \
5927 break; \
5928 case ic_return: \
5929 pedwarn (LOCATION, OPT, RE, QUALS); \
5930 break; \
5931 default: \
5932 gcc_unreachable (); \
5934 } while (0)
5936 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5937 rhs = TREE_OPERAND (rhs, 0);
5939 rhstype = TREE_TYPE (rhs);
5940 coder = TREE_CODE (rhstype);
5942 if (coder == ERROR_MARK)
5943 return error_mark_node;
5945 if (c_dialect_objc ())
5947 int parmno;
5949 switch (errtype)
5951 case ic_return:
5952 parmno = 0;
5953 break;
5955 case ic_assign:
5956 parmno = -1;
5957 break;
5959 case ic_init:
5960 parmno = -2;
5961 break;
5963 default:
5964 parmno = parmnum;
5965 break;
5968 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5971 if (warn_cxx_compat)
5973 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5974 if (checktype != error_mark_node
5975 && TREE_CODE (type) == ENUMERAL_TYPE
5976 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5978 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5979 G_("enum conversion when passing argument "
5980 "%d of %qE is invalid in C++"),
5981 G_("enum conversion in assignment is "
5982 "invalid in C++"),
5983 G_("enum conversion in initialization is "
5984 "invalid in C++"),
5985 G_("enum conversion in return is "
5986 "invalid in C++"));
5990 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5991 return rhs;
5993 if (coder == VOID_TYPE)
5995 /* Except for passing an argument to an unprototyped function,
5996 this is a constraint violation. When passing an argument to
5997 an unprototyped function, it is compile-time undefined;
5998 making it a constraint in that case was rejected in
5999 DR#252. */
6000 error_at (location, "void value not ignored as it ought to be");
6001 return error_mark_node;
6003 rhs = require_complete_type (rhs);
6004 if (rhs == error_mark_node)
6005 return error_mark_node;
6006 /* A non-reference type can convert to a reference. This handles
6007 va_start, va_copy and possibly port built-ins. */
6008 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6010 if (!lvalue_p (rhs))
6012 error_at (location, "cannot pass rvalue to reference parameter");
6013 return error_mark_node;
6015 if (!c_mark_addressable (rhs))
6016 return error_mark_node;
6017 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6018 SET_EXPR_LOCATION (rhs, location);
6020 rhs = convert_for_assignment (location, expr_loc,
6021 build_pointer_type (TREE_TYPE (type)),
6022 rhs, origtype, errtype,
6023 null_pointer_constant, fundecl, function,
6024 parmnum);
6025 if (rhs == error_mark_node)
6026 return error_mark_node;
6028 rhs = build1 (NOP_EXPR, type, rhs);
6029 SET_EXPR_LOCATION (rhs, location);
6030 return rhs;
6032 /* Some types can interconvert without explicit casts. */
6033 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6034 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6035 return convert (type, rhs);
6036 /* Arithmetic types all interconvert, and enum is treated like int. */
6037 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6038 || codel == FIXED_POINT_TYPE
6039 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6040 || codel == BOOLEAN_TYPE)
6041 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6042 || coder == FIXED_POINT_TYPE
6043 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6044 || coder == BOOLEAN_TYPE))
6046 tree ret;
6047 bool save = in_late_binary_op;
6048 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
6049 in_late_binary_op = true;
6050 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6051 ? expr_loc : location, type, orig_rhs);
6052 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
6053 in_late_binary_op = save;
6054 return ret;
6057 /* Aggregates in different TUs might need conversion. */
6058 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6059 && codel == coder
6060 && comptypes (type, rhstype))
6061 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6062 ? expr_loc : location, type, rhs);
6064 /* Conversion to a transparent union or record from its member types.
6065 This applies only to function arguments. */
6066 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6067 && TYPE_TRANSPARENT_AGGR (type))
6068 && errtype == ic_argpass)
6070 tree memb, marginal_memb = NULL_TREE;
6072 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6074 tree memb_type = TREE_TYPE (memb);
6076 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6077 TYPE_MAIN_VARIANT (rhstype)))
6078 break;
6080 if (TREE_CODE (memb_type) != POINTER_TYPE)
6081 continue;
6083 if (coder == POINTER_TYPE)
6085 tree ttl = TREE_TYPE (memb_type);
6086 tree ttr = TREE_TYPE (rhstype);
6088 /* Any non-function converts to a [const][volatile] void *
6089 and vice versa; otherwise, targets must be the same.
6090 Meanwhile, the lhs target must have all the qualifiers of
6091 the rhs. */
6092 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6093 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6094 || comp_target_types (location, memb_type, rhstype))
6096 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6097 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6098 /* If this type won't generate any warnings, use it. */
6099 if (lquals == rquals
6100 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6101 && TREE_CODE (ttl) == FUNCTION_TYPE)
6102 ? ((lquals | rquals) == rquals)
6103 : ((lquals | rquals) == lquals)))
6104 break;
6106 /* Keep looking for a better type, but remember this one. */
6107 if (!marginal_memb)
6108 marginal_memb = memb;
6112 /* Can convert integer zero to any pointer type. */
6113 if (null_pointer_constant)
6115 tree ttl = TREE_TYPE (memb_type);
6116 rhs = !upc_shared_type_p (ttl)
6117 ? null_pointer_node : upc_null_pts_node;
6118 break;
6122 if (memb || marginal_memb)
6124 if (!memb)
6126 /* We have only a marginally acceptable member type;
6127 it needs a warning. */
6128 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6129 tree ttr = TREE_TYPE (rhstype);
6131 /* Const and volatile mean something different for function
6132 types, so the usual warnings are not appropriate. */
6133 if (TREE_CODE (ttr) == FUNCTION_TYPE
6134 && TREE_CODE (ttl) == FUNCTION_TYPE)
6136 /* Because const and volatile on functions are
6137 restrictions that say the function will not do
6138 certain things, it is okay to use a const or volatile
6139 function where an ordinary one is wanted, but not
6140 vice-versa. */
6141 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6142 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6143 WARN_FOR_QUALIFIERS (location, expr_loc,
6144 OPT_Wdiscarded_qualifiers,
6145 G_("passing argument %d of %qE "
6146 "makes %q#v qualified function "
6147 "pointer from unqualified"),
6148 G_("assignment makes %q#v qualified "
6149 "function pointer from "
6150 "unqualified"),
6151 G_("initialization makes %q#v qualified "
6152 "function pointer from "
6153 "unqualified"),
6154 G_("return makes %q#v qualified function "
6155 "pointer from unqualified"),
6156 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6158 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6159 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6160 WARN_FOR_QUALIFIERS (location, expr_loc,
6161 OPT_Wdiscarded_qualifiers,
6162 G_("passing argument %d of %qE discards "
6163 "%qv qualifier from pointer target type"),
6164 G_("assignment discards %qv qualifier "
6165 "from pointer target type"),
6166 G_("initialization discards %qv qualifier "
6167 "from pointer target type"),
6168 G_("return discards %qv qualifier from "
6169 "pointer target type"),
6170 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6172 memb = marginal_memb;
6175 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6176 pedwarn (location, OPT_Wpedantic,
6177 "ISO C prohibits argument conversion to union type");
6179 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6180 return build_constructor_single (type, memb, rhs);
6184 /* Conversions among pointers */
6185 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6186 && (coder == codel))
6188 tree ttl = TREE_TYPE (type);
6189 tree ttr = TREE_TYPE (rhstype);
6190 tree mvl = ttl;
6191 tree mvr = ttr;
6192 bool is_opaque_pointer;
6193 int target_cmp = 0; /* Cache comp_target_types () result. */
6194 addr_space_t asl;
6195 addr_space_t asr;
6197 if (TREE_CODE (mvl) != ARRAY_TYPE)
6198 mvl = (TYPE_ATOMIC (mvl)
6199 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6200 TYPE_QUAL_ATOMIC)
6201 : TYPE_MAIN_VARIANT (mvl));
6202 if (TREE_CODE (mvr) != ARRAY_TYPE)
6203 mvr = (TYPE_ATOMIC (mvr)
6204 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6205 TYPE_QUAL_ATOMIC)
6206 : TYPE_MAIN_VARIANT (mvr));
6207 if ((upc_shared_type_p (ttl) && !upc_shared_type_p (ttr))
6208 && !integer_zerop (rhs))
6210 error_at (location, "UPC does not allow assignments from a local pointer "
6211 "to a pointer-to-shared");
6212 return error_mark_node;
6214 if (!upc_shared_type_p (ttl) && upc_shared_type_p (ttr))
6216 if (upc_is_null_pts_p (rhs))
6218 return null_pointer_node;
6220 else
6222 error_at (location, "UPC does not allow assignments "
6223 "from a pointer-to-shared to a local pointer");
6224 return error_mark_node;
6227 if (upc_shared_type_p (ttl) && upc_shared_type_p (ttr) && (ttl != ttr)
6228 && !(VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)))
6230 const tree bs_l = upc_get_block_factor (ttl);
6231 const tree bs_r = upc_get_block_factor (ttr);
6232 /* Both source and destination are non-void pointers to shared,
6233 whose target types are not equal.
6234 UPC dictates that their blocking factors must be equal. */
6235 if (!tree_int_cst_equal (bs_l, bs_r))
6237 error_at (location, "UPC does not allow assignment "
6238 "between pointers to shared with "
6239 "differing block sizes without a cast");
6240 return error_mark_node;
6244 /* Opaque pointers are treated like void pointers. */
6245 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6247 /* The Plan 9 compiler permits a pointer to a struct to be
6248 automatically converted into a pointer to an anonymous field
6249 within the struct. */
6250 if (flag_plan9_extensions
6251 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6252 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6253 && mvl != mvr)
6255 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6256 if (new_rhs != NULL_TREE)
6258 rhs = new_rhs;
6259 rhstype = TREE_TYPE (rhs);
6260 coder = TREE_CODE (rhstype);
6261 ttr = TREE_TYPE (rhstype);
6262 mvr = TYPE_MAIN_VARIANT (ttr);
6266 /* C++ does not allow the implicit conversion void* -> T*. However,
6267 for the purpose of reducing the number of false positives, we
6268 tolerate the special case of
6270 int *p = NULL;
6272 where NULL is typically defined in C to be '(void *) 0'. */
6273 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6274 warning_at (errtype == ic_argpass ? expr_loc : location,
6275 OPT_Wc___compat,
6276 "request for implicit conversion "
6277 "from %qT to %qT not permitted in C++", rhstype, type);
6279 /* See if the pointers point to incompatible address spaces. */
6280 asl = TYPE_ADDR_SPACE (ttl);
6281 asr = TYPE_ADDR_SPACE (ttr);
6282 if (!null_pointer_constant_p (rhs)
6283 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6285 switch (errtype)
6287 case ic_argpass:
6288 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6289 "non-enclosed address space", parmnum, rname);
6290 break;
6291 case ic_assign:
6292 error_at (location, "assignment from pointer to "
6293 "non-enclosed address space");
6294 break;
6295 case ic_init:
6296 error_at (location, "initialization from pointer to "
6297 "non-enclosed address space");
6298 break;
6299 case ic_return:
6300 error_at (location, "return from pointer to "
6301 "non-enclosed address space");
6302 break;
6303 default:
6304 gcc_unreachable ();
6306 return error_mark_node;
6309 /* Check if the right-hand side has a format attribute but the
6310 left-hand side doesn't. */
6311 if (warn_suggest_attribute_format
6312 && check_missing_format_attribute (type, rhstype))
6314 switch (errtype)
6316 case ic_argpass:
6317 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6318 "argument %d of %qE might be "
6319 "a candidate for a format attribute",
6320 parmnum, rname);
6321 break;
6322 case ic_assign:
6323 warning_at (location, OPT_Wsuggest_attribute_format,
6324 "assignment left-hand side might be "
6325 "a candidate for a format attribute");
6326 break;
6327 case ic_init:
6328 warning_at (location, OPT_Wsuggest_attribute_format,
6329 "initialization left-hand side might be "
6330 "a candidate for a format attribute");
6331 break;
6332 case ic_return:
6333 warning_at (location, OPT_Wsuggest_attribute_format,
6334 "return type might be "
6335 "a candidate for a format attribute");
6336 break;
6337 default:
6338 gcc_unreachable ();
6342 /* Any non-function converts to a [const][volatile] void *
6343 and vice versa; otherwise, targets must be the same.
6344 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6345 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6346 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6347 || (target_cmp = comp_target_types (location, type, rhstype))
6348 || is_opaque_pointer
6349 || ((c_common_unsigned_type (mvl)
6350 == c_common_unsigned_type (mvr))
6351 && (c_common_signed_type (mvl)
6352 == c_common_signed_type (mvr))
6353 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6355 if (pedantic
6356 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6358 (VOID_TYPE_P (ttr)
6359 && !null_pointer_constant
6360 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6361 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6362 G_("ISO C forbids passing argument %d of "
6363 "%qE between function pointer "
6364 "and %<void *%>"),
6365 G_("ISO C forbids assignment between "
6366 "function pointer and %<void *%>"),
6367 G_("ISO C forbids initialization between "
6368 "function pointer and %<void *%>"),
6369 G_("ISO C forbids return between function "
6370 "pointer and %<void *%>"));
6371 /* Const and volatile mean something different for function types,
6372 so the usual warnings are not appropriate. */
6373 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6374 && TREE_CODE (ttl) != FUNCTION_TYPE)
6376 /* Assignments between atomic and non-atomic objects are OK. */
6377 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6378 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6380 WARN_FOR_QUALIFIERS (location, expr_loc,
6381 OPT_Wdiscarded_qualifiers,
6382 G_("passing argument %d of %qE discards "
6383 "%qv qualifier from pointer target type"),
6384 G_("assignment discards %qv qualifier "
6385 "from pointer target type"),
6386 G_("initialization discards %qv qualifier "
6387 "from pointer target type"),
6388 G_("return discards %qv qualifier from "
6389 "pointer target type"),
6390 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6392 /* If this is not a case of ignoring a mismatch in signedness,
6393 no warning. */
6394 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6395 || target_cmp)
6397 /* If there is a mismatch, do warn. */
6398 else if (warn_pointer_sign)
6399 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6400 G_("pointer targets in passing argument "
6401 "%d of %qE differ in signedness"),
6402 G_("pointer targets in assignment "
6403 "differ in signedness"),
6404 G_("pointer targets in initialization "
6405 "differ in signedness"),
6406 G_("pointer targets in return differ "
6407 "in signedness"));
6409 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6410 && TREE_CODE (ttr) == FUNCTION_TYPE)
6412 /* Because const and volatile on functions are restrictions
6413 that say the function will not do certain things,
6414 it is okay to use a const or volatile function
6415 where an ordinary one is wanted, but not vice-versa. */
6416 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6417 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6418 WARN_FOR_QUALIFIERS (location, expr_loc,
6419 OPT_Wdiscarded_qualifiers,
6420 G_("passing argument %d of %qE makes "
6421 "%q#v qualified function pointer "
6422 "from unqualified"),
6423 G_("assignment makes %q#v qualified function "
6424 "pointer from unqualified"),
6425 G_("initialization makes %q#v qualified "
6426 "function pointer from unqualified"),
6427 G_("return makes %q#v qualified function "
6428 "pointer from unqualified"),
6429 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6432 else
6433 /* Avoid warning about the volatile ObjC EH puts on decls. */
6434 if (!objc_ok)
6435 WARN_FOR_ASSIGNMENT (location, expr_loc,
6436 OPT_Wincompatible_pointer_types,
6437 G_("passing argument %d of %qE from "
6438 "incompatible pointer type"),
6439 G_("assignment from incompatible pointer type"),
6440 G_("initialization from incompatible "
6441 "pointer type"),
6442 G_("return from incompatible pointer type"));
6444 return convert (type, rhs);
6446 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6448 /* ??? This should not be an error when inlining calls to
6449 unprototyped functions. */
6450 error_at (location, "invalid use of non-lvalue array");
6451 return error_mark_node;
6453 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6455 if (!null_pointer_constant && upc_shared_type_p (TREE_TYPE (type)))
6457 ERROR_FOR_ASSIGNMENT (location, 0,
6458 G_("passing argument %d of %qE attempts to make "
6459 "a UPC pointer-to-shared value from an integer"),
6460 G_("assignment attempts to make a UPC pointer-to-shared "
6461 "value from an integer"),
6462 G_("initialization attempts to make a UPC pointer-to-shared "
6463 "value from an integer without a cast"),
6464 G_("return makes a UPC pointer-to-shared value from an "
6465 "integer"));
6466 return error_mark_node;
6468 /* An explicit constant 0 can convert to a pointer,
6469 or one that results from arithmetic, even including
6470 a cast to integer type. */
6471 if (!null_pointer_constant)
6472 WARN_FOR_ASSIGNMENT (location, expr_loc,
6473 OPT_Wint_conversion,
6474 G_("passing argument %d of %qE makes "
6475 "pointer from integer without a cast"),
6476 G_("assignment makes pointer from integer "
6477 "without a cast"),
6478 G_("initialization makes pointer from "
6479 "integer without a cast"),
6480 G_("return makes pointer from integer "
6481 "without a cast"));
6483 return convert (type, rhs);
6485 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6487 WARN_FOR_ASSIGNMENT (location, expr_loc,
6488 OPT_Wint_conversion,
6489 G_("passing argument %d of %qE makes integer "
6490 "from pointer without a cast"),
6491 G_("assignment makes integer from pointer "
6492 "without a cast"),
6493 G_("initialization makes integer from pointer "
6494 "without a cast"),
6495 G_("return makes integer from pointer "
6496 "without a cast"));
6497 return convert (type, rhs);
6499 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6501 tree ret;
6502 bool save = in_late_binary_op;
6503 in_late_binary_op = true;
6504 ret = convert (type, rhs);
6505 in_late_binary_op = save;
6506 return ret;
6509 switch (errtype)
6511 case ic_argpass:
6512 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6513 rname);
6514 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6515 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6516 "expected %qT but argument is of type %qT", type, rhstype);
6517 break;
6518 case ic_assign:
6519 error_at (location, "incompatible types when assigning to type %qT from "
6520 "type %qT", type, rhstype);
6521 break;
6522 case ic_init:
6523 error_at (location,
6524 "incompatible types when initializing type %qT using type %qT",
6525 type, rhstype);
6526 break;
6527 case ic_return:
6528 error_at (location,
6529 "incompatible types when returning type %qT but %qT was "
6530 "expected", rhstype, type);
6531 break;
6532 default:
6533 gcc_unreachable ();
6536 return error_mark_node;
6539 /* If VALUE is a compound expr all of whose expressions are constant, then
6540 return its value. Otherwise, return error_mark_node.
6542 This is for handling COMPOUND_EXPRs as initializer elements
6543 which is allowed with a warning when -pedantic is specified. */
6545 static tree
6546 valid_compound_expr_initializer (tree value, tree endtype)
6548 if (TREE_CODE (value) == COMPOUND_EXPR)
6550 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6551 == error_mark_node)
6552 return error_mark_node;
6553 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6554 endtype);
6556 else if (!initializer_constant_valid_p (value, endtype))
6557 return error_mark_node;
6558 else
6559 return value;
6562 /* Perform appropriate conversions on the initial value of a variable,
6563 store it in the declaration DECL,
6564 and print any error messages that are appropriate.
6565 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6566 If the init is invalid, store an ERROR_MARK.
6568 INIT_LOC is the location of the initial value. */
6570 void
6571 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6573 const bool npc = init && null_pointer_constant_p (init);
6574 const bool is_upc_decl_init = upc_check_decl_init (decl, init);
6575 const bool require_constant = TREE_STATIC (decl) && !is_upc_decl_init;
6576 tree type = TREE_TYPE (decl);
6577 tree value;
6579 /* If variable's type was invalidly declared, just ignore it. */
6581 if (TREE_CODE (type) == ERROR_MARK)
6582 return;
6584 /* Digest the specified initializer into an expression. */
6586 value = digest_init (init_loc, type, init, origtype, npc,
6587 true, require_constant);
6589 /* UPC cannot initialize certain values at compile time.
6590 For example, the address of a UPC 'shared' variable must
6591 be evaluated at runtime. */
6593 if (is_upc_decl_init)
6595 upc_decl_init (decl, value);
6596 return;
6599 /* Store the expression if valid; else report error. */
6601 if (!in_system_header_at (input_location)
6602 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6603 warning (OPT_Wtraditional, "traditional C rejects automatic "
6604 "aggregate initialization");
6606 DECL_INITIAL (decl) = value;
6608 /* ANSI wants warnings about out-of-range constant initializers. */
6609 STRIP_TYPE_NOPS (value);
6610 if (TREE_STATIC (decl))
6611 constant_expression_warning (value);
6613 /* Check if we need to set array size from compound literal size. */
6614 if (TREE_CODE (type) == ARRAY_TYPE
6615 && TYPE_DOMAIN (type) == 0
6616 && value != error_mark_node)
6618 tree inside_init = init;
6620 STRIP_TYPE_NOPS (inside_init);
6621 inside_init = fold (inside_init);
6623 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6625 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6627 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6629 /* For int foo[] = (int [3]){1}; we need to set array size
6630 now since later on array initializer will be just the
6631 brace enclosed list of the compound literal. */
6632 tree etype = strip_array_types (TREE_TYPE (decl));
6633 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6634 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6635 layout_type (type);
6636 layout_decl (cldecl, 0);
6637 TREE_TYPE (decl)
6638 = c_build_qualified_type (type, TYPE_QUALS (etype));
6644 /* Methods for storing and printing names for error messages. */
6646 /* Implement a spelling stack that allows components of a name to be pushed
6647 and popped. Each element on the stack is this structure. */
6649 struct spelling
6651 int kind;
6652 union
6654 unsigned HOST_WIDE_INT i;
6655 const char *s;
6656 } u;
6659 #define SPELLING_STRING 1
6660 #define SPELLING_MEMBER 2
6661 #define SPELLING_BOUNDS 3
6663 static struct spelling *spelling; /* Next stack element (unused). */
6664 static struct spelling *spelling_base; /* Spelling stack base. */
6665 static int spelling_size; /* Size of the spelling stack. */
6667 /* Macros to save and restore the spelling stack around push_... functions.
6668 Alternative to SAVE_SPELLING_STACK. */
6670 #define SPELLING_DEPTH() (spelling - spelling_base)
6671 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6673 /* Push an element on the spelling stack with type KIND and assign VALUE
6674 to MEMBER. */
6676 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6678 int depth = SPELLING_DEPTH (); \
6680 if (depth >= spelling_size) \
6682 spelling_size += 10; \
6683 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6684 spelling_size); \
6685 RESTORE_SPELLING_DEPTH (depth); \
6688 spelling->kind = (KIND); \
6689 spelling->MEMBER = (VALUE); \
6690 spelling++; \
6693 /* Push STRING on the stack. Printed literally. */
6695 static void
6696 push_string (const char *string)
6698 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6701 /* Push a member name on the stack. Printed as '.' STRING. */
6703 static void
6704 push_member_name (tree decl)
6706 const char *const string
6707 = (DECL_NAME (decl)
6708 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6709 : _("<anonymous>"));
6710 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6713 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6715 static void
6716 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6718 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6721 /* Compute the maximum size in bytes of the printed spelling. */
6723 static int
6724 spelling_length (void)
6726 int size = 0;
6727 struct spelling *p;
6729 for (p = spelling_base; p < spelling; p++)
6731 if (p->kind == SPELLING_BOUNDS)
6732 size += 25;
6733 else
6734 size += strlen (p->u.s) + 1;
6737 return size;
6740 /* Print the spelling to BUFFER and return it. */
6742 static char *
6743 print_spelling (char *buffer)
6745 char *d = buffer;
6746 struct spelling *p;
6748 for (p = spelling_base; p < spelling; p++)
6749 if (p->kind == SPELLING_BOUNDS)
6751 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6752 d += strlen (d);
6754 else
6756 const char *s;
6757 if (p->kind == SPELLING_MEMBER)
6758 *d++ = '.';
6759 for (s = p->u.s; (*d = *s++); d++)
6762 *d++ = '\0';
6763 return buffer;
6766 /* Digest the parser output INIT as an initializer for type TYPE.
6767 Return a C expression of type TYPE to represent the initial value.
6769 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6771 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6773 If INIT is a string constant, STRICT_STRING is true if it is
6774 unparenthesized or we should not warn here for it being parenthesized.
6775 For other types of INIT, STRICT_STRING is not used.
6777 INIT_LOC is the location of the INIT.
6779 REQUIRE_CONSTANT requests an error if non-constant initializers or
6780 elements are seen. */
6782 static tree
6783 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6784 bool null_pointer_constant, bool strict_string,
6785 int require_constant)
6787 enum tree_code code = TREE_CODE (type);
6788 tree inside_init = init;
6789 tree semantic_type = NULL_TREE;
6790 bool maybe_const = true;
6792 if (type == error_mark_node
6793 || !init
6794 || init == error_mark_node
6795 || TREE_TYPE (init) == error_mark_node)
6796 return error_mark_node;
6798 STRIP_TYPE_NOPS (inside_init);
6800 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6802 semantic_type = TREE_TYPE (inside_init);
6803 inside_init = TREE_OPERAND (inside_init, 0);
6805 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6806 inside_init = decl_constant_value_for_optimization (inside_init);
6808 /* Initialization of an array of chars from a string constant
6809 optionally enclosed in braces. */
6811 if (code == ARRAY_TYPE && inside_init
6812 && TREE_CODE (inside_init) == STRING_CST)
6814 tree typ1
6815 = (TYPE_ATOMIC (TREE_TYPE (type))
6816 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6817 TYPE_QUAL_ATOMIC)
6818 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6819 /* Note that an array could be both an array of character type
6820 and an array of wchar_t if wchar_t is signed char or unsigned
6821 char. */
6822 bool char_array = (typ1 == char_type_node
6823 || typ1 == signed_char_type_node
6824 || typ1 == unsigned_char_type_node);
6825 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6826 bool char16_array = !!comptypes (typ1, char16_type_node);
6827 bool char32_array = !!comptypes (typ1, char32_type_node);
6829 if (char_array || wchar_array || char16_array || char32_array)
6831 struct c_expr expr;
6832 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6833 expr.value = inside_init;
6834 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6835 expr.original_type = NULL;
6836 maybe_warn_string_init (init_loc, type, expr);
6838 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6839 pedwarn_init (init_loc, OPT_Wpedantic,
6840 "initialization of a flexible array member");
6842 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6843 TYPE_MAIN_VARIANT (type)))
6844 return inside_init;
6846 if (char_array)
6848 if (typ2 != char_type_node)
6850 error_init (init_loc, "char-array initialized from wide "
6851 "string");
6852 return error_mark_node;
6855 else
6857 if (typ2 == char_type_node)
6859 error_init (init_loc, "wide character array initialized "
6860 "from non-wide string");
6861 return error_mark_node;
6863 else if (!comptypes(typ1, typ2))
6865 error_init (init_loc, "wide character array initialized "
6866 "from incompatible wide string");
6867 return error_mark_node;
6871 TREE_TYPE (inside_init) = type;
6872 if (TYPE_DOMAIN (type) != 0
6873 && TYPE_SIZE (type) != 0
6874 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6876 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6878 /* Subtract the size of a single (possibly wide) character
6879 because it's ok to ignore the terminating null char
6880 that is counted in the length of the constant. */
6881 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6882 (len
6883 - (TYPE_PRECISION (typ1)
6884 / BITS_PER_UNIT))))
6885 pedwarn_init (init_loc, 0,
6886 ("initializer-string for array of chars "
6887 "is too long"));
6888 else if (warn_cxx_compat
6889 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6890 warning_at (init_loc, OPT_Wc___compat,
6891 ("initializer-string for array chars "
6892 "is too long for C++"));
6895 return inside_init;
6897 else if (INTEGRAL_TYPE_P (typ1))
6899 error_init (init_loc, "array of inappropriate type initialized "
6900 "from string constant");
6901 return error_mark_node;
6905 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6906 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6907 below and handle as a constructor. */
6908 if (code == VECTOR_TYPE
6909 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6910 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6911 && TREE_CONSTANT (inside_init))
6913 if (TREE_CODE (inside_init) == VECTOR_CST
6914 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6915 TYPE_MAIN_VARIANT (type)))
6916 return inside_init;
6918 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6920 unsigned HOST_WIDE_INT ix;
6921 tree value;
6922 bool constant_p = true;
6924 /* Iterate through elements and check if all constructor
6925 elements are *_CSTs. */
6926 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6927 if (!CONSTANT_CLASS_P (value))
6929 constant_p = false;
6930 break;
6933 if (constant_p)
6934 return build_vector_from_ctor (type,
6935 CONSTRUCTOR_ELTS (inside_init));
6939 if (warn_sequence_point)
6940 verify_sequence_points (inside_init);
6942 /* Any type can be initialized
6943 from an expression of the same type, optionally with braces. */
6945 if (inside_init && TREE_TYPE (inside_init) != 0
6946 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6947 TYPE_MAIN_VARIANT (type))
6948 || (code == ARRAY_TYPE
6949 && comptypes (TREE_TYPE (inside_init), type))
6950 || (code == VECTOR_TYPE
6951 && comptypes (TREE_TYPE (inside_init), type))
6952 || (code == POINTER_TYPE
6953 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6954 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6955 TREE_TYPE (type)))))
6957 if (code == POINTER_TYPE)
6959 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6961 if (TREE_CODE (inside_init) == STRING_CST
6962 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6963 inside_init = array_to_pointer_conversion
6964 (init_loc, inside_init);
6965 else
6967 error_init (init_loc, "invalid use of non-lvalue array");
6968 return error_mark_node;
6973 if (code == VECTOR_TYPE)
6974 /* Although the types are compatible, we may require a
6975 conversion. */
6976 inside_init = convert (type, inside_init);
6978 if (require_constant
6979 && (code == VECTOR_TYPE || !flag_isoc99)
6980 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6982 /* As an extension, allow initializing objects with static storage
6983 duration with compound literals (which are then treated just as
6984 the brace enclosed list they contain). Also allow this for
6985 vectors, as we can only assign them with compound literals. */
6986 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6987 inside_init = DECL_INITIAL (decl);
6990 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6991 && TREE_CODE (inside_init) != CONSTRUCTOR)
6993 error_init (init_loc, "array initialized from non-constant array "
6994 "expression");
6995 return error_mark_node;
6998 /* Compound expressions can only occur here if -Wpedantic or
6999 -pedantic-errors is specified. In the later case, we always want
7000 an error. In the former case, we simply want a warning. */
7001 if (require_constant && pedantic
7002 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7004 inside_init
7005 = valid_compound_expr_initializer (inside_init,
7006 TREE_TYPE (inside_init));
7007 if (inside_init == error_mark_node)
7008 error_init (init_loc, "initializer element is not constant");
7009 else
7010 pedwarn_init (init_loc, OPT_Wpedantic,
7011 "initializer element is not constant");
7012 if (flag_pedantic_errors)
7013 inside_init = error_mark_node;
7015 else if (require_constant
7016 && !initializer_constant_valid_p (inside_init,
7017 TREE_TYPE (inside_init)))
7019 error_init (init_loc, "initializer element is not constant");
7020 inside_init = error_mark_node;
7022 else if (require_constant && !maybe_const)
7023 pedwarn_init (init_loc, 0,
7024 "initializer element is not a constant expression");
7026 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7027 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7028 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7029 type, inside_init, origtype,
7030 ic_init, null_pointer_constant,
7031 NULL_TREE, NULL_TREE, 0);
7032 return inside_init;
7035 /* Handle scalar types, including conversions. */
7037 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7038 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7039 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7041 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7042 && (TREE_CODE (init) == STRING_CST
7043 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7044 inside_init = init = array_to_pointer_conversion (init_loc, init);
7045 if (semantic_type)
7046 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7047 inside_init);
7048 inside_init
7049 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7050 inside_init, origtype, ic_init,
7051 null_pointer_constant, NULL_TREE, NULL_TREE,
7054 /* Check to see if we have already given an error message. */
7055 if (inside_init == error_mark_node)
7057 else if (require_constant && !TREE_CONSTANT (inside_init))
7059 error_init (init_loc, "initializer element is not constant");
7060 inside_init = error_mark_node;
7062 else if (require_constant
7063 && !initializer_constant_valid_p (inside_init,
7064 TREE_TYPE (inside_init)))
7066 error_init (init_loc, "initializer element is not computable at "
7067 "load time");
7068 inside_init = error_mark_node;
7070 else if (require_constant && !maybe_const)
7071 pedwarn_init (init_loc, 0,
7072 "initializer element is not a constant expression");
7074 return inside_init;
7077 /* Come here only for records and arrays. */
7079 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7081 error_init (init_loc, "variable-sized object may not be initialized");
7082 return error_mark_node;
7085 error_init (init_loc, "invalid initializer");
7086 return error_mark_node;
7089 /* Handle initializers that use braces. */
7091 /* Type of object we are accumulating a constructor for.
7092 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7093 static tree constructor_type;
7095 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7096 left to fill. */
7097 static tree constructor_fields;
7099 /* For an ARRAY_TYPE, this is the specified index
7100 at which to store the next element we get. */
7101 static tree constructor_index;
7103 /* For an ARRAY_TYPE, this is the maximum index. */
7104 static tree constructor_max_index;
7106 /* For a RECORD_TYPE, this is the first field not yet written out. */
7107 static tree constructor_unfilled_fields;
7109 /* For an ARRAY_TYPE, this is the index of the first element
7110 not yet written out. */
7111 static tree constructor_unfilled_index;
7113 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7114 This is so we can generate gaps between fields, when appropriate. */
7115 static tree constructor_bit_index;
7117 /* If we are saving up the elements rather than allocating them,
7118 this is the list of elements so far (in reverse order,
7119 most recent first). */
7120 static vec<constructor_elt, va_gc> *constructor_elements;
7122 /* 1 if constructor should be incrementally stored into a constructor chain,
7123 0 if all the elements should be kept in AVL tree. */
7124 static int constructor_incremental;
7126 /* 1 if so far this constructor's elements are all compile-time constants. */
7127 static int constructor_constant;
7129 /* 1 if so far this constructor's elements are all valid address constants. */
7130 static int constructor_simple;
7132 /* 1 if this constructor has an element that cannot be part of a
7133 constant expression. */
7134 static int constructor_nonconst;
7136 /* 1 if this constructor is erroneous so far. */
7137 static int constructor_erroneous;
7139 /* 1 if this constructor is the universal zero initializer { 0 }. */
7140 static int constructor_zeroinit;
7142 /* Structure for managing pending initializer elements, organized as an
7143 AVL tree. */
7145 struct init_node
7147 struct init_node *left, *right;
7148 struct init_node *parent;
7149 int balance;
7150 tree purpose;
7151 tree value;
7152 tree origtype;
7155 /* Tree of pending elements at this constructor level.
7156 These are elements encountered out of order
7157 which belong at places we haven't reached yet in actually
7158 writing the output.
7159 Will never hold tree nodes across GC runs. */
7160 static struct init_node *constructor_pending_elts;
7162 /* The SPELLING_DEPTH of this constructor. */
7163 static int constructor_depth;
7165 /* DECL node for which an initializer is being read.
7166 0 means we are reading a constructor expression
7167 such as (struct foo) {...}. */
7168 static tree constructor_decl;
7170 /* Nonzero if this is an initializer for a top-level decl. */
7171 static int constructor_top_level;
7173 /* Nonzero if there were any member designators in this initializer. */
7174 static int constructor_designated;
7176 /* Nesting depth of designator list. */
7177 static int designator_depth;
7179 /* Nonzero if there were diagnosed errors in this designator list. */
7180 static int designator_erroneous;
7183 /* This stack has a level for each implicit or explicit level of
7184 structuring in the initializer, including the outermost one. It
7185 saves the values of most of the variables above. */
7187 struct constructor_range_stack;
7189 struct constructor_stack
7191 struct constructor_stack *next;
7192 tree type;
7193 tree fields;
7194 tree index;
7195 tree max_index;
7196 tree unfilled_index;
7197 tree unfilled_fields;
7198 tree bit_index;
7199 vec<constructor_elt, va_gc> *elements;
7200 struct init_node *pending_elts;
7201 int offset;
7202 int depth;
7203 /* If value nonzero, this value should replace the entire
7204 constructor at this level. */
7205 struct c_expr replacement_value;
7206 struct constructor_range_stack *range_stack;
7207 char constant;
7208 char simple;
7209 char nonconst;
7210 char implicit;
7211 char erroneous;
7212 char outer;
7213 char incremental;
7214 char designated;
7217 static struct constructor_stack *constructor_stack;
7219 /* This stack represents designators from some range designator up to
7220 the last designator in the list. */
7222 struct constructor_range_stack
7224 struct constructor_range_stack *next, *prev;
7225 struct constructor_stack *stack;
7226 tree range_start;
7227 tree index;
7228 tree range_end;
7229 tree fields;
7232 static struct constructor_range_stack *constructor_range_stack;
7234 /* This stack records separate initializers that are nested.
7235 Nested initializers can't happen in ANSI C, but GNU C allows them
7236 in cases like { ... (struct foo) { ... } ... }. */
7238 struct initializer_stack
7240 struct initializer_stack *next;
7241 tree decl;
7242 struct constructor_stack *constructor_stack;
7243 struct constructor_range_stack *constructor_range_stack;
7244 vec<constructor_elt, va_gc> *elements;
7245 struct spelling *spelling;
7246 struct spelling *spelling_base;
7247 int spelling_size;
7248 char top_level;
7249 char require_constant_value;
7250 char require_constant_elements;
7253 static struct initializer_stack *initializer_stack;
7255 /* Prepare to parse and output the initializer for variable DECL. */
7257 void
7258 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7260 const char *locus;
7261 struct initializer_stack *p = XNEW (struct initializer_stack);
7263 p->decl = constructor_decl;
7264 p->require_constant_value = require_constant_value;
7265 p->require_constant_elements = require_constant_elements;
7266 p->constructor_stack = constructor_stack;
7267 p->constructor_range_stack = constructor_range_stack;
7268 p->elements = constructor_elements;
7269 p->spelling = spelling;
7270 p->spelling_base = spelling_base;
7271 p->spelling_size = spelling_size;
7272 p->top_level = constructor_top_level;
7273 p->next = initializer_stack;
7274 initializer_stack = p;
7276 constructor_decl = decl;
7277 constructor_designated = 0;
7278 constructor_top_level = top_level;
7280 if (decl != 0 && decl != error_mark_node)
7282 require_constant_value = TREE_STATIC (decl);
7283 require_constant_elements
7284 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7285 /* For a scalar, you can always use any value to initialize,
7286 even within braces. */
7287 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7288 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7289 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7290 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
7291 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7293 else
7295 require_constant_value = 0;
7296 require_constant_elements = 0;
7297 locus = _("(anonymous)");
7300 constructor_stack = 0;
7301 constructor_range_stack = 0;
7303 found_missing_braces = 0;
7305 spelling_base = 0;
7306 spelling_size = 0;
7307 RESTORE_SPELLING_DEPTH (0);
7309 if (locus)
7310 push_string (locus);
7313 void
7314 finish_init (void)
7316 struct initializer_stack *p = initializer_stack;
7318 /* Free the whole constructor stack of this initializer. */
7319 while (constructor_stack)
7321 struct constructor_stack *q = constructor_stack;
7322 constructor_stack = q->next;
7323 free (q);
7326 gcc_assert (!constructor_range_stack);
7328 /* Pop back to the data of the outer initializer (if any). */
7329 free (spelling_base);
7331 constructor_decl = p->decl;
7332 require_constant_value = p->require_constant_value;
7333 require_constant_elements = p->require_constant_elements;
7334 constructor_stack = p->constructor_stack;
7335 constructor_range_stack = p->constructor_range_stack;
7336 constructor_elements = p->elements;
7337 spelling = p->spelling;
7338 spelling_base = p->spelling_base;
7339 spelling_size = p->spelling_size;
7340 constructor_top_level = p->top_level;
7341 initializer_stack = p->next;
7342 free (p);
7345 /* Call here when we see the initializer is surrounded by braces.
7346 This is instead of a call to push_init_level;
7347 it is matched by a call to pop_init_level.
7349 TYPE is the type to initialize, for a constructor expression.
7350 For an initializer for a decl, TYPE is zero. */
7352 void
7353 really_start_incremental_init (tree type)
7355 struct constructor_stack *p = XNEW (struct constructor_stack);
7357 if (type == 0)
7358 type = TREE_TYPE (constructor_decl);
7360 if (TREE_CODE (type) == VECTOR_TYPE
7361 && TYPE_VECTOR_OPAQUE (type))
7362 error ("opaque vector types cannot be initialized");
7364 p->type = constructor_type;
7365 p->fields = constructor_fields;
7366 p->index = constructor_index;
7367 p->max_index = constructor_max_index;
7368 p->unfilled_index = constructor_unfilled_index;
7369 p->unfilled_fields = constructor_unfilled_fields;
7370 p->bit_index = constructor_bit_index;
7371 p->elements = constructor_elements;
7372 p->constant = constructor_constant;
7373 p->simple = constructor_simple;
7374 p->nonconst = constructor_nonconst;
7375 p->erroneous = constructor_erroneous;
7376 p->pending_elts = constructor_pending_elts;
7377 p->depth = constructor_depth;
7378 p->replacement_value.value = 0;
7379 p->replacement_value.original_code = ERROR_MARK;
7380 p->replacement_value.original_type = NULL;
7381 p->implicit = 0;
7382 p->range_stack = 0;
7383 p->outer = 0;
7384 p->incremental = constructor_incremental;
7385 p->designated = constructor_designated;
7386 p->next = 0;
7387 constructor_stack = p;
7389 constructor_constant = 1;
7390 constructor_simple = 1;
7391 constructor_nonconst = 0;
7392 constructor_depth = SPELLING_DEPTH ();
7393 constructor_elements = NULL;
7394 constructor_pending_elts = 0;
7395 constructor_type = type;
7396 constructor_incremental = 1;
7397 constructor_designated = 0;
7398 constructor_zeroinit = 1;
7399 designator_depth = 0;
7400 designator_erroneous = 0;
7402 /* The result of the constructor must not be UPC shared qualified */
7403 if (upc_shared_type_p (constructor_type))
7404 constructor_type = build_upc_unshared_type (constructor_type);
7405 if (TREE_CODE (constructor_type) == RECORD_TYPE
7406 || TREE_CODE (constructor_type) == UNION_TYPE)
7408 constructor_fields = TYPE_FIELDS (constructor_type);
7409 /* Skip any nameless bit fields at the beginning. */
7410 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7411 && DECL_NAME (constructor_fields) == 0)
7412 constructor_fields = DECL_CHAIN (constructor_fields);
7414 constructor_unfilled_fields = constructor_fields;
7415 constructor_bit_index = bitsize_zero_node;
7417 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7419 if (TYPE_DOMAIN (constructor_type))
7421 constructor_max_index
7422 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7424 /* Detect non-empty initializations of zero-length arrays. */
7425 if (constructor_max_index == NULL_TREE
7426 && TYPE_SIZE (constructor_type))
7427 constructor_max_index = integer_minus_one_node;
7429 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7430 to initialize VLAs will cause a proper error; avoid tree
7431 checking errors as well by setting a safe value. */
7432 if (constructor_max_index
7433 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7434 constructor_max_index = integer_minus_one_node;
7436 constructor_index
7437 = convert (bitsizetype,
7438 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7440 else
7442 constructor_index = bitsize_zero_node;
7443 constructor_max_index = NULL_TREE;
7446 constructor_unfilled_index = constructor_index;
7448 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7450 /* Vectors are like simple fixed-size arrays. */
7451 constructor_max_index =
7452 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7453 constructor_index = bitsize_zero_node;
7454 constructor_unfilled_index = constructor_index;
7456 else
7458 /* Handle the case of int x = {5}; */
7459 constructor_fields = constructor_type;
7460 constructor_unfilled_fields = constructor_type;
7464 /* Push down into a subobject, for initialization.
7465 If this is for an explicit set of braces, IMPLICIT is 0.
7466 If it is because the next element belongs at a lower level,
7467 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7469 void
7470 push_init_level (location_t loc, int implicit,
7471 struct obstack *braced_init_obstack)
7473 struct constructor_stack *p;
7474 tree value = NULL_TREE;
7476 /* If we've exhausted any levels that didn't have braces,
7477 pop them now. If implicit == 1, this will have been done in
7478 process_init_element; do not repeat it here because in the case
7479 of excess initializers for an empty aggregate this leads to an
7480 infinite cycle of popping a level and immediately recreating
7481 it. */
7482 if (implicit != 1)
7484 while (constructor_stack->implicit)
7486 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7487 || TREE_CODE (constructor_type) == UNION_TYPE)
7488 && constructor_fields == 0)
7489 process_init_element (input_location,
7490 pop_init_level (loc, 1, braced_init_obstack),
7491 true, braced_init_obstack);
7492 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7493 && constructor_max_index
7494 && tree_int_cst_lt (constructor_max_index,
7495 constructor_index))
7496 process_init_element (input_location,
7497 pop_init_level (loc, 1, braced_init_obstack),
7498 true, braced_init_obstack);
7499 else
7500 break;
7504 /* Unless this is an explicit brace, we need to preserve previous
7505 content if any. */
7506 if (implicit)
7508 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7509 || TREE_CODE (constructor_type) == UNION_TYPE)
7510 && constructor_fields)
7511 value = find_init_member (constructor_fields, braced_init_obstack);
7512 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7513 value = find_init_member (constructor_index, braced_init_obstack);
7516 p = XNEW (struct constructor_stack);
7517 p->type = constructor_type;
7518 p->fields = constructor_fields;
7519 p->index = constructor_index;
7520 p->max_index = constructor_max_index;
7521 p->unfilled_index = constructor_unfilled_index;
7522 p->unfilled_fields = constructor_unfilled_fields;
7523 p->bit_index = constructor_bit_index;
7524 p->elements = constructor_elements;
7525 p->constant = constructor_constant;
7526 p->simple = constructor_simple;
7527 p->nonconst = constructor_nonconst;
7528 p->erroneous = constructor_erroneous;
7529 p->pending_elts = constructor_pending_elts;
7530 p->depth = constructor_depth;
7531 p->replacement_value.value = 0;
7532 p->replacement_value.original_code = ERROR_MARK;
7533 p->replacement_value.original_type = NULL;
7534 p->implicit = implicit;
7535 p->outer = 0;
7536 p->incremental = constructor_incremental;
7537 p->designated = constructor_designated;
7538 p->next = constructor_stack;
7539 p->range_stack = 0;
7540 constructor_stack = p;
7542 constructor_constant = 1;
7543 constructor_simple = 1;
7544 constructor_nonconst = 0;
7545 constructor_depth = SPELLING_DEPTH ();
7546 constructor_elements = NULL;
7547 constructor_incremental = 1;
7548 constructor_designated = 0;
7549 constructor_pending_elts = 0;
7550 if (!implicit)
7552 p->range_stack = constructor_range_stack;
7553 constructor_range_stack = 0;
7554 designator_depth = 0;
7555 designator_erroneous = 0;
7558 /* Don't die if an entire brace-pair level is superfluous
7559 in the containing level. */
7560 if (constructor_type == 0)
7562 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7563 || TREE_CODE (constructor_type) == UNION_TYPE)
7565 /* Don't die if there are extra init elts at the end. */
7566 if (constructor_fields == 0)
7567 constructor_type = 0;
7568 else
7570 constructor_type = TREE_TYPE (constructor_fields);
7571 push_member_name (constructor_fields);
7572 constructor_depth++;
7574 /* If upper initializer is designated, then mark this as
7575 designated too to prevent bogus warnings. */
7576 constructor_designated = p->designated;
7578 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7580 constructor_type = TREE_TYPE (constructor_type);
7581 push_array_bounds (tree_to_uhwi (constructor_index));
7582 constructor_depth++;
7585 if (constructor_type == 0)
7587 error_init (loc, "extra brace group at end of initializer");
7588 constructor_fields = 0;
7589 constructor_unfilled_fields = 0;
7590 return;
7593 if (value && TREE_CODE (value) == CONSTRUCTOR)
7595 constructor_constant = TREE_CONSTANT (value);
7596 constructor_simple = TREE_STATIC (value);
7597 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7598 constructor_elements = CONSTRUCTOR_ELTS (value);
7599 if (!vec_safe_is_empty (constructor_elements)
7600 && (TREE_CODE (constructor_type) == RECORD_TYPE
7601 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7602 set_nonincremental_init (braced_init_obstack);
7605 if (implicit == 1)
7606 found_missing_braces = 1;
7608 if (TREE_CODE (constructor_type) == RECORD_TYPE
7609 || TREE_CODE (constructor_type) == UNION_TYPE)
7611 constructor_fields = TYPE_FIELDS (constructor_type);
7612 /* Skip any nameless bit fields at the beginning. */
7613 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7614 && DECL_NAME (constructor_fields) == 0)
7615 constructor_fields = DECL_CHAIN (constructor_fields);
7617 constructor_unfilled_fields = constructor_fields;
7618 constructor_bit_index = bitsize_zero_node;
7620 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7622 /* Vectors are like simple fixed-size arrays. */
7623 constructor_max_index =
7624 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7625 constructor_index = bitsize_int (0);
7626 constructor_unfilled_index = constructor_index;
7628 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7630 if (TYPE_DOMAIN (constructor_type))
7632 constructor_max_index
7633 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7635 /* Detect non-empty initializations of zero-length arrays. */
7636 if (constructor_max_index == NULL_TREE
7637 && TYPE_SIZE (constructor_type))
7638 constructor_max_index = integer_minus_one_node;
7640 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7641 to initialize VLAs will cause a proper error; avoid tree
7642 checking errors as well by setting a safe value. */
7643 if (constructor_max_index
7644 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7645 constructor_max_index = integer_minus_one_node;
7647 constructor_index
7648 = convert (bitsizetype,
7649 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7651 else
7652 constructor_index = bitsize_zero_node;
7654 constructor_unfilled_index = constructor_index;
7655 if (value && TREE_CODE (value) == STRING_CST)
7657 /* We need to split the char/wchar array into individual
7658 characters, so that we don't have to special case it
7659 everywhere. */
7660 set_nonincremental_init_from_string (value, braced_init_obstack);
7663 else
7665 if (constructor_type != error_mark_node)
7666 warning_init (input_location, 0, "braces around scalar initializer");
7667 constructor_fields = constructor_type;
7668 constructor_unfilled_fields = constructor_type;
7672 /* At the end of an implicit or explicit brace level,
7673 finish up that level of constructor. If a single expression
7674 with redundant braces initialized that level, return the
7675 c_expr structure for that expression. Otherwise, the original_code
7676 element is set to ERROR_MARK.
7677 If we were outputting the elements as they are read, return 0 as the value
7678 from inner levels (process_init_element ignores that),
7679 but return error_mark_node as the value from the outermost level
7680 (that's what we want to put in DECL_INITIAL).
7681 Otherwise, return a CONSTRUCTOR expression as the value. */
7683 struct c_expr
7684 pop_init_level (location_t loc, int implicit,
7685 struct obstack *braced_init_obstack)
7687 struct constructor_stack *p;
7688 struct c_expr ret;
7689 ret.value = 0;
7690 ret.original_code = ERROR_MARK;
7691 ret.original_type = NULL;
7693 if (implicit == 0)
7695 /* When we come to an explicit close brace,
7696 pop any inner levels that didn't have explicit braces. */
7697 while (constructor_stack->implicit)
7698 process_init_element (input_location,
7699 pop_init_level (loc, 1, braced_init_obstack),
7700 true, braced_init_obstack);
7701 gcc_assert (!constructor_range_stack);
7704 /* Now output all pending elements. */
7705 constructor_incremental = 1;
7706 output_pending_init_elements (1, braced_init_obstack);
7708 p = constructor_stack;
7710 /* Error for initializing a flexible array member, or a zero-length
7711 array member in an inappropriate context. */
7712 if (constructor_type && constructor_fields
7713 && TREE_CODE (constructor_type) == ARRAY_TYPE
7714 && TYPE_DOMAIN (constructor_type)
7715 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7717 /* Silently discard empty initializations. The parser will
7718 already have pedwarned for empty brackets. */
7719 if (integer_zerop (constructor_unfilled_index))
7720 constructor_type = NULL_TREE;
7721 else
7723 gcc_assert (!TYPE_SIZE (constructor_type));
7725 if (constructor_depth > 2)
7726 error_init (loc, "initialization of flexible array member in a nested context");
7727 else
7728 pedwarn_init (loc, OPT_Wpedantic,
7729 "initialization of a flexible array member");
7731 /* We have already issued an error message for the existence
7732 of a flexible array member not at the end of the structure.
7733 Discard the initializer so that we do not die later. */
7734 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7735 constructor_type = NULL_TREE;
7739 if (vec_safe_length (constructor_elements) != 1)
7740 constructor_zeroinit = 0;
7742 /* Warn when some structs are initialized with direct aggregation. */
7743 if (!implicit && found_missing_braces && warn_missing_braces
7744 && !constructor_zeroinit)
7746 warning_init (loc, OPT_Wmissing_braces,
7747 "missing braces around initializer");
7750 /* Warn when some struct elements are implicitly initialized to zero. */
7751 if (warn_missing_field_initializers
7752 && constructor_type
7753 && TREE_CODE (constructor_type) == RECORD_TYPE
7754 && constructor_unfilled_fields)
7756 /* Do not warn for flexible array members or zero-length arrays. */
7757 while (constructor_unfilled_fields
7758 && (!DECL_SIZE (constructor_unfilled_fields)
7759 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7760 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7762 if (constructor_unfilled_fields
7763 /* Do not warn if this level of the initializer uses member
7764 designators; it is likely to be deliberate. */
7765 && !constructor_designated
7766 /* Do not warn about initializing with ` = {0}'. */
7767 && !constructor_zeroinit)
7769 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7770 "missing initializer for field %qD of %qT",
7771 constructor_unfilled_fields,
7772 constructor_type))
7773 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7774 "%qD declared here", constructor_unfilled_fields);
7778 /* Pad out the end of the structure. */
7779 if (p->replacement_value.value)
7780 /* If this closes a superfluous brace pair,
7781 just pass out the element between them. */
7782 ret = p->replacement_value;
7783 else if (constructor_type == 0)
7785 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7786 && TREE_CODE (constructor_type) != UNION_TYPE
7787 && TREE_CODE (constructor_type) != ARRAY_TYPE
7788 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7790 /* A nonincremental scalar initializer--just return
7791 the element, after verifying there is just one. */
7792 if (vec_safe_is_empty (constructor_elements))
7794 if (!constructor_erroneous)
7795 error_init (loc, "empty scalar initializer");
7796 ret.value = error_mark_node;
7798 else if (vec_safe_length (constructor_elements) != 1)
7800 error_init (loc, "extra elements in scalar initializer");
7801 ret.value = (*constructor_elements)[0].value;
7803 else
7804 ret.value = (*constructor_elements)[0].value;
7806 else
7808 if (constructor_erroneous)
7809 ret.value = error_mark_node;
7810 else
7812 ret.value = build_constructor (constructor_type,
7813 constructor_elements);
7814 if (constructor_constant)
7815 TREE_CONSTANT (ret.value) = 1;
7816 if (constructor_constant && constructor_simple)
7817 TREE_STATIC (ret.value) = 1;
7818 if (constructor_nonconst)
7819 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7823 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7825 if (constructor_nonconst)
7826 ret.original_code = C_MAYBE_CONST_EXPR;
7827 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7828 ret.original_code = ERROR_MARK;
7831 constructor_type = p->type;
7832 constructor_fields = p->fields;
7833 constructor_index = p->index;
7834 constructor_max_index = p->max_index;
7835 constructor_unfilled_index = p->unfilled_index;
7836 constructor_unfilled_fields = p->unfilled_fields;
7837 constructor_bit_index = p->bit_index;
7838 constructor_elements = p->elements;
7839 constructor_constant = p->constant;
7840 constructor_simple = p->simple;
7841 constructor_nonconst = p->nonconst;
7842 constructor_erroneous = p->erroneous;
7843 constructor_incremental = p->incremental;
7844 constructor_designated = p->designated;
7845 constructor_pending_elts = p->pending_elts;
7846 constructor_depth = p->depth;
7847 if (!p->implicit)
7848 constructor_range_stack = p->range_stack;
7849 RESTORE_SPELLING_DEPTH (constructor_depth);
7851 constructor_stack = p->next;
7852 free (p);
7854 if (ret.value == 0 && constructor_stack == 0)
7855 ret.value = error_mark_node;
7856 return ret;
7859 /* Common handling for both array range and field name designators.
7860 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7862 static int
7863 set_designator (location_t loc, int array,
7864 struct obstack *braced_init_obstack)
7866 tree subtype;
7867 enum tree_code subcode;
7869 /* Don't die if an entire brace-pair level is superfluous
7870 in the containing level. */
7871 if (constructor_type == 0)
7872 return 1;
7874 /* If there were errors in this designator list already, bail out
7875 silently. */
7876 if (designator_erroneous)
7877 return 1;
7879 if (!designator_depth)
7881 gcc_assert (!constructor_range_stack);
7883 /* Designator list starts at the level of closest explicit
7884 braces. */
7885 while (constructor_stack->implicit)
7886 process_init_element (input_location,
7887 pop_init_level (loc, 1, braced_init_obstack),
7888 true, braced_init_obstack);
7889 constructor_designated = 1;
7890 return 0;
7893 switch (TREE_CODE (constructor_type))
7895 case RECORD_TYPE:
7896 case UNION_TYPE:
7897 subtype = TREE_TYPE (constructor_fields);
7898 if (subtype != error_mark_node)
7899 subtype = TYPE_MAIN_VARIANT (subtype);
7900 break;
7901 case ARRAY_TYPE:
7902 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7903 break;
7904 default:
7905 gcc_unreachable ();
7908 subcode = TREE_CODE (subtype);
7909 if (array && subcode != ARRAY_TYPE)
7911 error_init (loc, "array index in non-array initializer");
7912 return 1;
7914 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7916 error_init (loc, "field name not in record or union initializer");
7917 return 1;
7920 constructor_designated = 1;
7921 push_init_level (loc, 2, braced_init_obstack);
7922 return 0;
7925 /* If there are range designators in designator list, push a new designator
7926 to constructor_range_stack. RANGE_END is end of such stack range or
7927 NULL_TREE if there is no range designator at this level. */
7929 static void
7930 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7932 struct constructor_range_stack *p;
7934 p = (struct constructor_range_stack *)
7935 obstack_alloc (braced_init_obstack,
7936 sizeof (struct constructor_range_stack));
7937 p->prev = constructor_range_stack;
7938 p->next = 0;
7939 p->fields = constructor_fields;
7940 p->range_start = constructor_index;
7941 p->index = constructor_index;
7942 p->stack = constructor_stack;
7943 p->range_end = range_end;
7944 if (constructor_range_stack)
7945 constructor_range_stack->next = p;
7946 constructor_range_stack = p;
7949 /* Within an array initializer, specify the next index to be initialized.
7950 FIRST is that index. If LAST is nonzero, then initialize a range
7951 of indices, running from FIRST through LAST. */
7953 void
7954 set_init_index (location_t loc, tree first, tree last,
7955 struct obstack *braced_init_obstack)
7957 if (set_designator (loc, 1, braced_init_obstack))
7958 return;
7960 designator_erroneous = 1;
7962 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7963 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7965 error_init (loc, "array index in initializer not of integer type");
7966 return;
7969 if (TREE_CODE (first) != INTEGER_CST)
7971 first = c_fully_fold (first, false, NULL);
7972 if (TREE_CODE (first) == INTEGER_CST)
7973 pedwarn_init (loc, OPT_Wpedantic,
7974 "array index in initializer is not "
7975 "an integer constant expression");
7978 if (last && TREE_CODE (last) != INTEGER_CST)
7980 last = c_fully_fold (last, false, NULL);
7981 if (TREE_CODE (last) == INTEGER_CST)
7982 pedwarn_init (loc, OPT_Wpedantic,
7983 "array index in initializer is not "
7984 "an integer constant expression");
7987 if (TREE_CODE (first) != INTEGER_CST)
7988 error_init (loc, "nonconstant array index in initializer");
7989 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7990 error_init (loc, "nonconstant array index in initializer");
7991 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7992 error_init (loc, "array index in non-array initializer");
7993 else if (tree_int_cst_sgn (first) == -1)
7994 error_init (loc, "array index in initializer exceeds array bounds");
7995 else if (constructor_max_index
7996 && tree_int_cst_lt (constructor_max_index, first))
7997 error_init (loc, "array index in initializer exceeds array bounds");
7998 else
8000 constant_expression_warning (first);
8001 if (last)
8002 constant_expression_warning (last);
8003 constructor_index = convert (bitsizetype, first);
8004 if (tree_int_cst_lt (constructor_index, first))
8006 constructor_index = copy_node (constructor_index);
8007 TREE_OVERFLOW (constructor_index) = 1;
8010 if (last)
8012 if (tree_int_cst_equal (first, last))
8013 last = 0;
8014 else if (tree_int_cst_lt (last, first))
8016 error_init (loc, "empty index range in initializer");
8017 last = 0;
8019 else
8021 last = convert (bitsizetype, last);
8022 if (constructor_max_index != 0
8023 && tree_int_cst_lt (constructor_max_index, last))
8025 error_init (loc, "array index range in initializer exceeds "
8026 "array bounds");
8027 last = 0;
8032 designator_depth++;
8033 designator_erroneous = 0;
8034 if (constructor_range_stack || last)
8035 push_range_stack (last, braced_init_obstack);
8039 /* Within a struct initializer, specify the next field to be initialized. */
8041 void
8042 set_init_label (location_t loc, tree fieldname,
8043 struct obstack *braced_init_obstack)
8045 tree field;
8047 if (set_designator (loc, 0, braced_init_obstack))
8048 return;
8050 designator_erroneous = 1;
8052 if (TREE_CODE (constructor_type) != RECORD_TYPE
8053 && TREE_CODE (constructor_type) != UNION_TYPE)
8055 error_init (loc, "field name not in record or union initializer");
8056 return;
8059 field = lookup_field (constructor_type, fieldname);
8061 if (field == 0)
8062 error ("unknown field %qE specified in initializer", fieldname);
8063 else
8066 constructor_fields = TREE_VALUE (field);
8067 designator_depth++;
8068 designator_erroneous = 0;
8069 if (constructor_range_stack)
8070 push_range_stack (NULL_TREE, braced_init_obstack);
8071 field = TREE_CHAIN (field);
8072 if (field)
8074 if (set_designator (loc, 0, braced_init_obstack))
8075 return;
8078 while (field != NULL_TREE);
8081 /* Add a new initializer to the tree of pending initializers. PURPOSE
8082 identifies the initializer, either array index or field in a structure.
8083 VALUE is the value of that index or field. If ORIGTYPE is not
8084 NULL_TREE, it is the original type of VALUE.
8086 IMPLICIT is true if value comes from pop_init_level (1),
8087 the new initializer has been merged with the existing one
8088 and thus no warnings should be emitted about overriding an
8089 existing initializer. */
8091 static void
8092 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8093 bool implicit, struct obstack *braced_init_obstack)
8095 struct init_node *p, **q, *r;
8097 q = &constructor_pending_elts;
8098 p = 0;
8100 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8102 while (*q != 0)
8104 p = *q;
8105 if (tree_int_cst_lt (purpose, p->purpose))
8106 q = &p->left;
8107 else if (tree_int_cst_lt (p->purpose, purpose))
8108 q = &p->right;
8109 else
8111 if (!implicit)
8113 if (TREE_SIDE_EFFECTS (p->value))
8114 warning_init (loc, 0,
8115 "initialized field with side-effects "
8116 "overwritten");
8117 else if (warn_override_init)
8118 warning_init (loc, OPT_Woverride_init,
8119 "initialized field overwritten");
8121 p->value = value;
8122 p->origtype = origtype;
8123 return;
8127 else
8129 tree bitpos;
8131 bitpos = bit_position (purpose);
8132 while (*q != NULL)
8134 p = *q;
8135 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8136 q = &p->left;
8137 else if (p->purpose != purpose)
8138 q = &p->right;
8139 else
8141 if (!implicit)
8143 if (TREE_SIDE_EFFECTS (p->value))
8144 warning_init (loc, 0,
8145 "initialized field with side-effects "
8146 "overwritten");
8147 else if (warn_override_init)
8148 warning_init (loc, OPT_Woverride_init,
8149 "initialized field overwritten");
8151 p->value = value;
8152 p->origtype = origtype;
8153 return;
8158 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8159 sizeof (struct init_node));
8160 r->purpose = purpose;
8161 r->value = value;
8162 r->origtype = origtype;
8164 *q = r;
8165 r->parent = p;
8166 r->left = 0;
8167 r->right = 0;
8168 r->balance = 0;
8170 while (p)
8172 struct init_node *s;
8174 if (r == p->left)
8176 if (p->balance == 0)
8177 p->balance = -1;
8178 else if (p->balance < 0)
8180 if (r->balance < 0)
8182 /* L rotation. */
8183 p->left = r->right;
8184 if (p->left)
8185 p->left->parent = p;
8186 r->right = p;
8188 p->balance = 0;
8189 r->balance = 0;
8191 s = p->parent;
8192 p->parent = r;
8193 r->parent = s;
8194 if (s)
8196 if (s->left == p)
8197 s->left = r;
8198 else
8199 s->right = r;
8201 else
8202 constructor_pending_elts = r;
8204 else
8206 /* LR rotation. */
8207 struct init_node *t = r->right;
8209 r->right = t->left;
8210 if (r->right)
8211 r->right->parent = r;
8212 t->left = r;
8214 p->left = t->right;
8215 if (p->left)
8216 p->left->parent = p;
8217 t->right = p;
8219 p->balance = t->balance < 0;
8220 r->balance = -(t->balance > 0);
8221 t->balance = 0;
8223 s = p->parent;
8224 p->parent = t;
8225 r->parent = t;
8226 t->parent = s;
8227 if (s)
8229 if (s->left == p)
8230 s->left = t;
8231 else
8232 s->right = t;
8234 else
8235 constructor_pending_elts = t;
8237 break;
8239 else
8241 /* p->balance == +1; growth of left side balances the node. */
8242 p->balance = 0;
8243 break;
8246 else /* r == p->right */
8248 if (p->balance == 0)
8249 /* Growth propagation from right side. */
8250 p->balance++;
8251 else if (p->balance > 0)
8253 if (r->balance > 0)
8255 /* R rotation. */
8256 p->right = r->left;
8257 if (p->right)
8258 p->right->parent = p;
8259 r->left = p;
8261 p->balance = 0;
8262 r->balance = 0;
8264 s = p->parent;
8265 p->parent = r;
8266 r->parent = s;
8267 if (s)
8269 if (s->left == p)
8270 s->left = r;
8271 else
8272 s->right = r;
8274 else
8275 constructor_pending_elts = r;
8277 else /* r->balance == -1 */
8279 /* RL rotation */
8280 struct init_node *t = r->left;
8282 r->left = t->right;
8283 if (r->left)
8284 r->left->parent = r;
8285 t->right = r;
8287 p->right = t->left;
8288 if (p->right)
8289 p->right->parent = p;
8290 t->left = p;
8292 r->balance = (t->balance < 0);
8293 p->balance = -(t->balance > 0);
8294 t->balance = 0;
8296 s = p->parent;
8297 p->parent = t;
8298 r->parent = t;
8299 t->parent = s;
8300 if (s)
8302 if (s->left == p)
8303 s->left = t;
8304 else
8305 s->right = t;
8307 else
8308 constructor_pending_elts = t;
8310 break;
8312 else
8314 /* p->balance == -1; growth of right side balances the node. */
8315 p->balance = 0;
8316 break;
8320 r = p;
8321 p = p->parent;
8325 /* Build AVL tree from a sorted chain. */
8327 static void
8328 set_nonincremental_init (struct obstack * braced_init_obstack)
8330 unsigned HOST_WIDE_INT ix;
8331 tree index, value;
8333 if (TREE_CODE (constructor_type) != RECORD_TYPE
8334 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8335 return;
8337 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8338 add_pending_init (input_location, index, value, NULL_TREE, true,
8339 braced_init_obstack);
8340 constructor_elements = NULL;
8341 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8343 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8344 /* Skip any nameless bit fields at the beginning. */
8345 while (constructor_unfilled_fields != 0
8346 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8347 && DECL_NAME (constructor_unfilled_fields) == 0)
8348 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8351 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8353 if (TYPE_DOMAIN (constructor_type))
8354 constructor_unfilled_index
8355 = convert (bitsizetype,
8356 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8357 else
8358 constructor_unfilled_index = bitsize_zero_node;
8360 constructor_incremental = 0;
8363 /* Build AVL tree from a string constant. */
8365 static void
8366 set_nonincremental_init_from_string (tree str,
8367 struct obstack * braced_init_obstack)
8369 tree value, purpose, type;
8370 HOST_WIDE_INT val[2];
8371 const char *p, *end;
8372 int byte, wchar_bytes, charwidth, bitpos;
8374 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8376 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8377 charwidth = TYPE_PRECISION (char_type_node);
8378 type = TREE_TYPE (constructor_type);
8379 p = TREE_STRING_POINTER (str);
8380 end = p + TREE_STRING_LENGTH (str);
8382 for (purpose = bitsize_zero_node;
8383 p < end
8384 && !(constructor_max_index
8385 && tree_int_cst_lt (constructor_max_index, purpose));
8386 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8388 if (wchar_bytes == 1)
8390 val[0] = (unsigned char) *p++;
8391 val[1] = 0;
8393 else
8395 val[1] = 0;
8396 val[0] = 0;
8397 for (byte = 0; byte < wchar_bytes; byte++)
8399 if (BYTES_BIG_ENDIAN)
8400 bitpos = (wchar_bytes - byte - 1) * charwidth;
8401 else
8402 bitpos = byte * charwidth;
8403 val[bitpos % HOST_BITS_PER_WIDE_INT]
8404 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8405 << (bitpos % HOST_BITS_PER_WIDE_INT);
8409 if (!TYPE_UNSIGNED (type))
8411 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8412 if (bitpos < HOST_BITS_PER_WIDE_INT)
8414 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8416 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8417 val[1] = -1;
8420 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8422 if (val[0] < 0)
8423 val[1] = -1;
8425 else if (val[1] & (((HOST_WIDE_INT) 1)
8426 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8427 val[1] |= ((HOST_WIDE_INT) -1)
8428 << (bitpos - HOST_BITS_PER_WIDE_INT);
8431 value = wide_int_to_tree (type,
8432 wide_int::from_array (val, 2,
8433 HOST_BITS_PER_WIDE_INT * 2));
8434 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8435 braced_init_obstack);
8438 constructor_incremental = 0;
8441 /* Return value of FIELD in pending initializer or zero if the field was
8442 not initialized yet. */
8444 static tree
8445 find_init_member (tree field, struct obstack * braced_init_obstack)
8447 struct init_node *p;
8449 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8451 if (constructor_incremental
8452 && tree_int_cst_lt (field, constructor_unfilled_index))
8453 set_nonincremental_init (braced_init_obstack);
8455 p = constructor_pending_elts;
8456 while (p)
8458 if (tree_int_cst_lt (field, p->purpose))
8459 p = p->left;
8460 else if (tree_int_cst_lt (p->purpose, field))
8461 p = p->right;
8462 else
8463 return p->value;
8466 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8468 tree bitpos = bit_position (field);
8470 if (constructor_incremental
8471 && (!constructor_unfilled_fields
8472 || tree_int_cst_lt (bitpos,
8473 bit_position (constructor_unfilled_fields))))
8474 set_nonincremental_init (braced_init_obstack);
8476 p = constructor_pending_elts;
8477 while (p)
8479 if (field == p->purpose)
8480 return p->value;
8481 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8482 p = p->left;
8483 else
8484 p = p->right;
8487 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8489 if (!vec_safe_is_empty (constructor_elements)
8490 && (constructor_elements->last ().index == field))
8491 return constructor_elements->last ().value;
8493 return 0;
8496 /* "Output" the next constructor element.
8497 At top level, really output it to assembler code now.
8498 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8499 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8500 TYPE is the data type that the containing data type wants here.
8501 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8502 If VALUE is a string constant, STRICT_STRING is true if it is
8503 unparenthesized or we should not warn here for it being parenthesized.
8504 For other types of VALUE, STRICT_STRING is not used.
8506 PENDING if non-nil means output pending elements that belong
8507 right after this element. (PENDING is normally 1;
8508 it is 0 while outputting pending elements, to avoid recursion.)
8510 IMPLICIT is true if value comes from pop_init_level (1),
8511 the new initializer has been merged with the existing one
8512 and thus no warnings should be emitted about overriding an
8513 existing initializer. */
8515 static void
8516 output_init_element (location_t loc, tree value, tree origtype,
8517 bool strict_string, tree type, tree field, int pending,
8518 bool implicit, struct obstack * braced_init_obstack)
8520 tree semantic_type = NULL_TREE;
8521 bool maybe_const = true;
8522 bool npc;
8524 if (type == error_mark_node || value == error_mark_node)
8526 constructor_erroneous = 1;
8527 return;
8529 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8530 && (TREE_CODE (value) == STRING_CST
8531 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8532 && !(TREE_CODE (value) == STRING_CST
8533 && TREE_CODE (type) == ARRAY_TYPE
8534 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8535 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8536 TYPE_MAIN_VARIANT (type)))
8537 value = array_to_pointer_conversion (input_location, value);
8539 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8540 && require_constant_value && !flag_isoc99 && pending)
8542 /* As an extension, allow initializing objects with static storage
8543 duration with compound literals (which are then treated just as
8544 the brace enclosed list they contain). */
8545 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8546 value = DECL_INITIAL (decl);
8549 npc = null_pointer_constant_p (value);
8550 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8552 semantic_type = TREE_TYPE (value);
8553 value = TREE_OPERAND (value, 0);
8555 value = c_fully_fold (value, require_constant_value, &maybe_const);
8557 if (value == error_mark_node)
8558 constructor_erroneous = 1;
8559 else if (!TREE_CONSTANT (value))
8560 constructor_constant = 0;
8561 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8562 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8563 || TREE_CODE (constructor_type) == UNION_TYPE)
8564 && DECL_C_BIT_FIELD (field)
8565 && TREE_CODE (value) != INTEGER_CST))
8566 constructor_simple = 0;
8567 if (!maybe_const)
8568 constructor_nonconst = 1;
8570 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8572 if (require_constant_value)
8574 error_init (loc, "initializer element is not constant");
8575 value = error_mark_node;
8577 else if (require_constant_elements)
8578 pedwarn (loc, OPT_Wpedantic,
8579 "initializer element is not computable at load time");
8581 else if (!maybe_const
8582 && (require_constant_value || require_constant_elements))
8583 pedwarn_init (loc, OPT_Wpedantic,
8584 "initializer element is not a constant expression");
8586 /* Issue -Wc++-compat warnings about initializing a bitfield with
8587 enum type. */
8588 if (warn_cxx_compat
8589 && field != NULL_TREE
8590 && TREE_CODE (field) == FIELD_DECL
8591 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8592 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8593 != TYPE_MAIN_VARIANT (type))
8594 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8596 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8597 if (checktype != error_mark_node
8598 && (TYPE_MAIN_VARIANT (checktype)
8599 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8600 warning_init (loc, OPT_Wc___compat,
8601 "enum conversion in initialization is invalid in C++");
8604 /* If this field is empty (and not at the end of structure),
8605 don't do anything other than checking the initializer. */
8606 if (field
8607 && (TREE_TYPE (field) == error_mark_node
8608 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8609 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8610 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8611 || DECL_CHAIN (field)))))
8612 return;
8614 if (semantic_type)
8615 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8616 value = digest_init (loc, type, value, origtype, npc, strict_string,
8617 require_constant_value);
8618 if (value == error_mark_node)
8620 constructor_erroneous = 1;
8621 return;
8623 if (require_constant_value || require_constant_elements)
8624 constant_expression_warning (value);
8626 /* If this element doesn't come next in sequence,
8627 put it on constructor_pending_elts. */
8628 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8629 && (!constructor_incremental
8630 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8632 if (constructor_incremental
8633 && tree_int_cst_lt (field, constructor_unfilled_index))
8634 set_nonincremental_init (braced_init_obstack);
8636 add_pending_init (loc, field, value, origtype, implicit,
8637 braced_init_obstack);
8638 return;
8640 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8641 && (!constructor_incremental
8642 || field != constructor_unfilled_fields))
8644 /* We do this for records but not for unions. In a union,
8645 no matter which field is specified, it can be initialized
8646 right away since it starts at the beginning of the union. */
8647 if (constructor_incremental)
8649 if (!constructor_unfilled_fields)
8650 set_nonincremental_init (braced_init_obstack);
8651 else
8653 tree bitpos, unfillpos;
8655 bitpos = bit_position (field);
8656 unfillpos = bit_position (constructor_unfilled_fields);
8658 if (tree_int_cst_lt (bitpos, unfillpos))
8659 set_nonincremental_init (braced_init_obstack);
8663 add_pending_init (loc, field, value, origtype, implicit,
8664 braced_init_obstack);
8665 return;
8667 else if (TREE_CODE (constructor_type) == UNION_TYPE
8668 && !vec_safe_is_empty (constructor_elements))
8670 if (!implicit)
8672 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8673 warning_init (loc, 0,
8674 "initialized field with side-effects overwritten");
8675 else if (warn_override_init)
8676 warning_init (loc, OPT_Woverride_init,
8677 "initialized field overwritten");
8680 /* We can have just one union field set. */
8681 constructor_elements = NULL;
8684 /* Otherwise, output this element either to
8685 constructor_elements or to the assembler file. */
8687 constructor_elt celt = {field, value};
8688 vec_safe_push (constructor_elements, celt);
8690 /* Advance the variable that indicates sequential elements output. */
8691 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8692 constructor_unfilled_index
8693 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8694 bitsize_one_node);
8695 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8697 constructor_unfilled_fields
8698 = DECL_CHAIN (constructor_unfilled_fields);
8700 /* Skip any nameless bit fields. */
8701 while (constructor_unfilled_fields != 0
8702 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8703 && DECL_NAME (constructor_unfilled_fields) == 0)
8704 constructor_unfilled_fields =
8705 DECL_CHAIN (constructor_unfilled_fields);
8707 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8708 constructor_unfilled_fields = 0;
8710 /* Now output any pending elements which have become next. */
8711 if (pending)
8712 output_pending_init_elements (0, braced_init_obstack);
8715 /* Output any pending elements which have become next.
8716 As we output elements, constructor_unfilled_{fields,index}
8717 advances, which may cause other elements to become next;
8718 if so, they too are output.
8720 If ALL is 0, we return when there are
8721 no more pending elements to output now.
8723 If ALL is 1, we output space as necessary so that
8724 we can output all the pending elements. */
8725 static void
8726 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8728 struct init_node *elt = constructor_pending_elts;
8729 tree next;
8731 retry:
8733 /* Look through the whole pending tree.
8734 If we find an element that should be output now,
8735 output it. Otherwise, set NEXT to the element
8736 that comes first among those still pending. */
8738 next = 0;
8739 while (elt)
8741 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8743 if (tree_int_cst_equal (elt->purpose,
8744 constructor_unfilled_index))
8745 output_init_element (input_location, elt->value, elt->origtype,
8746 true, TREE_TYPE (constructor_type),
8747 constructor_unfilled_index, 0, false,
8748 braced_init_obstack);
8749 else if (tree_int_cst_lt (constructor_unfilled_index,
8750 elt->purpose))
8752 /* Advance to the next smaller node. */
8753 if (elt->left)
8754 elt = elt->left;
8755 else
8757 /* We have reached the smallest node bigger than the
8758 current unfilled index. Fill the space first. */
8759 next = elt->purpose;
8760 break;
8763 else
8765 /* Advance to the next bigger node. */
8766 if (elt->right)
8767 elt = elt->right;
8768 else
8770 /* We have reached the biggest node in a subtree. Find
8771 the parent of it, which is the next bigger node. */
8772 while (elt->parent && elt->parent->right == elt)
8773 elt = elt->parent;
8774 elt = elt->parent;
8775 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8776 elt->purpose))
8778 next = elt->purpose;
8779 break;
8784 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8785 || TREE_CODE (constructor_type) == UNION_TYPE)
8787 tree ctor_unfilled_bitpos, elt_bitpos;
8789 /* If the current record is complete we are done. */
8790 if (constructor_unfilled_fields == 0)
8791 break;
8793 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8794 elt_bitpos = bit_position (elt->purpose);
8795 /* We can't compare fields here because there might be empty
8796 fields in between. */
8797 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8799 constructor_unfilled_fields = elt->purpose;
8800 output_init_element (input_location, elt->value, elt->origtype,
8801 true, TREE_TYPE (elt->purpose),
8802 elt->purpose, 0, false,
8803 braced_init_obstack);
8805 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8807 /* Advance to the next smaller node. */
8808 if (elt->left)
8809 elt = elt->left;
8810 else
8812 /* We have reached the smallest node bigger than the
8813 current unfilled field. Fill the space first. */
8814 next = elt->purpose;
8815 break;
8818 else
8820 /* Advance to the next bigger node. */
8821 if (elt->right)
8822 elt = elt->right;
8823 else
8825 /* We have reached the biggest node in a subtree. Find
8826 the parent of it, which is the next bigger node. */
8827 while (elt->parent && elt->parent->right == elt)
8828 elt = elt->parent;
8829 elt = elt->parent;
8830 if (elt
8831 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8832 bit_position (elt->purpose))))
8834 next = elt->purpose;
8835 break;
8842 /* Ordinarily return, but not if we want to output all
8843 and there are elements left. */
8844 if (!(all && next != 0))
8845 return;
8847 /* If it's not incremental, just skip over the gap, so that after
8848 jumping to retry we will output the next successive element. */
8849 if (TREE_CODE (constructor_type) == RECORD_TYPE
8850 || TREE_CODE (constructor_type) == UNION_TYPE)
8851 constructor_unfilled_fields = next;
8852 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8853 constructor_unfilled_index = next;
8855 /* ELT now points to the node in the pending tree with the next
8856 initializer to output. */
8857 goto retry;
8860 /* Add one non-braced element to the current constructor level.
8861 This adjusts the current position within the constructor's type.
8862 This may also start or terminate implicit levels
8863 to handle a partly-braced initializer.
8865 Once this has found the correct level for the new element,
8866 it calls output_init_element.
8868 IMPLICIT is true if value comes from pop_init_level (1),
8869 the new initializer has been merged with the existing one
8870 and thus no warnings should be emitted about overriding an
8871 existing initializer. */
8873 void
8874 process_init_element (location_t loc, struct c_expr value, bool implicit,
8875 struct obstack * braced_init_obstack)
8877 tree orig_value = value.value;
8878 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8879 bool strict_string = value.original_code == STRING_CST;
8880 bool was_designated = designator_depth != 0;
8882 designator_depth = 0;
8883 designator_erroneous = 0;
8885 if (!implicit && value.value && !integer_zerop (value.value))
8886 constructor_zeroinit = 0;
8888 /* Handle superfluous braces around string cst as in
8889 char x[] = {"foo"}; */
8890 if (string_flag
8891 && constructor_type
8892 && !was_designated
8893 && TREE_CODE (constructor_type) == ARRAY_TYPE
8894 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8895 && integer_zerop (constructor_unfilled_index))
8897 if (constructor_stack->replacement_value.value)
8898 error_init (loc, "excess elements in char array initializer");
8899 constructor_stack->replacement_value = value;
8900 return;
8903 if (constructor_stack->replacement_value.value != 0)
8905 error_init (loc, "excess elements in struct initializer");
8906 return;
8909 /* Ignore elements of a brace group if it is entirely superfluous
8910 and has already been diagnosed. */
8911 if (constructor_type == 0)
8912 return;
8914 /* If we've exhausted any levels that didn't have braces,
8915 pop them now. */
8916 while (constructor_stack->implicit)
8918 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8919 || TREE_CODE (constructor_type) == UNION_TYPE)
8920 && constructor_fields == 0)
8921 process_init_element (loc,
8922 pop_init_level (loc, 1, braced_init_obstack),
8923 true, braced_init_obstack);
8924 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8925 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8926 && constructor_max_index
8927 && tree_int_cst_lt (constructor_max_index,
8928 constructor_index))
8929 process_init_element (loc,
8930 pop_init_level (loc, 1, braced_init_obstack),
8931 true, braced_init_obstack);
8932 else
8933 break;
8936 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8937 if (constructor_range_stack)
8939 /* If value is a compound literal and we'll be just using its
8940 content, don't put it into a SAVE_EXPR. */
8941 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8942 || !require_constant_value
8943 || flag_isoc99)
8945 tree semantic_type = NULL_TREE;
8946 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8948 semantic_type = TREE_TYPE (value.value);
8949 value.value = TREE_OPERAND (value.value, 0);
8951 value.value = c_save_expr (value.value);
8952 if (semantic_type)
8953 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8954 value.value);
8958 while (1)
8960 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8962 tree fieldtype;
8963 enum tree_code fieldcode;
8965 if (constructor_fields == 0)
8967 pedwarn_init (loc, 0, "excess elements in struct initializer");
8968 break;
8971 fieldtype = TREE_TYPE (constructor_fields);
8972 if (fieldtype != error_mark_node)
8973 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8974 fieldcode = TREE_CODE (fieldtype);
8976 /* Error for non-static initialization of a flexible array member. */
8977 if (fieldcode == ARRAY_TYPE
8978 && !require_constant_value
8979 && TYPE_SIZE (fieldtype) == NULL_TREE
8980 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8982 error_init (loc, "non-static initialization of a flexible "
8983 "array member");
8984 break;
8987 /* Accept a string constant to initialize a subarray. */
8988 if (value.value != 0
8989 && fieldcode == ARRAY_TYPE
8990 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8991 && string_flag)
8992 value.value = orig_value;
8993 /* Otherwise, if we have come to a subaggregate,
8994 and we don't have an element of its type, push into it. */
8995 else if (value.value != 0
8996 && value.value != error_mark_node
8997 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8998 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8999 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9001 push_init_level (loc, 1, braced_init_obstack);
9002 continue;
9005 if (value.value)
9007 push_member_name (constructor_fields);
9008 output_init_element (loc, value.value, value.original_type,
9009 strict_string, fieldtype,
9010 constructor_fields, 1, implicit,
9011 braced_init_obstack);
9012 RESTORE_SPELLING_DEPTH (constructor_depth);
9014 else
9015 /* Do the bookkeeping for an element that was
9016 directly output as a constructor. */
9018 /* For a record, keep track of end position of last field. */
9019 if (DECL_SIZE (constructor_fields))
9020 constructor_bit_index
9021 = size_binop_loc (input_location, PLUS_EXPR,
9022 bit_position (constructor_fields),
9023 DECL_SIZE (constructor_fields));
9025 /* If the current field was the first one not yet written out,
9026 it isn't now, so update. */
9027 if (constructor_unfilled_fields == constructor_fields)
9029 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9030 /* Skip any nameless bit fields. */
9031 while (constructor_unfilled_fields != 0
9032 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9033 && DECL_NAME (constructor_unfilled_fields) == 0)
9034 constructor_unfilled_fields =
9035 DECL_CHAIN (constructor_unfilled_fields);
9039 constructor_fields = DECL_CHAIN (constructor_fields);
9040 /* Skip any nameless bit fields at the beginning. */
9041 while (constructor_fields != 0
9042 && DECL_C_BIT_FIELD (constructor_fields)
9043 && DECL_NAME (constructor_fields) == 0)
9044 constructor_fields = DECL_CHAIN (constructor_fields);
9046 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9048 tree fieldtype;
9049 enum tree_code fieldcode;
9051 if (constructor_fields == 0)
9053 pedwarn_init (loc, 0,
9054 "excess elements in union initializer");
9055 break;
9058 fieldtype = TREE_TYPE (constructor_fields);
9059 if (fieldtype != error_mark_node)
9060 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9061 fieldcode = TREE_CODE (fieldtype);
9063 /* Warn that traditional C rejects initialization of unions.
9064 We skip the warning if the value is zero. This is done
9065 under the assumption that the zero initializer in user
9066 code appears conditioned on e.g. __STDC__ to avoid
9067 "missing initializer" warnings and relies on default
9068 initialization to zero in the traditional C case.
9069 We also skip the warning if the initializer is designated,
9070 again on the assumption that this must be conditional on
9071 __STDC__ anyway (and we've already complained about the
9072 member-designator already). */
9073 if (!in_system_header_at (input_location) && !constructor_designated
9074 && !(value.value && (integer_zerop (value.value)
9075 || real_zerop (value.value))))
9076 warning (OPT_Wtraditional, "traditional C rejects initialization "
9077 "of unions");
9079 /* Accept a string constant to initialize a subarray. */
9080 if (value.value != 0
9081 && fieldcode == ARRAY_TYPE
9082 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9083 && string_flag)
9084 value.value = orig_value;
9085 /* Otherwise, if we have come to a subaggregate,
9086 and we don't have an element of its type, push into it. */
9087 else if (value.value != 0
9088 && value.value != error_mark_node
9089 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9090 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9091 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9093 push_init_level (loc, 1, braced_init_obstack);
9094 continue;
9097 if (value.value)
9099 push_member_name (constructor_fields);
9100 output_init_element (loc, value.value, value.original_type,
9101 strict_string, fieldtype,
9102 constructor_fields, 1, implicit,
9103 braced_init_obstack);
9104 RESTORE_SPELLING_DEPTH (constructor_depth);
9106 else
9107 /* Do the bookkeeping for an element that was
9108 directly output as a constructor. */
9110 constructor_bit_index = DECL_SIZE (constructor_fields);
9111 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9114 constructor_fields = 0;
9116 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9118 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9119 enum tree_code eltcode = TREE_CODE (elttype);
9121 /* Accept a string constant to initialize a subarray. */
9122 if (value.value != 0
9123 && eltcode == ARRAY_TYPE
9124 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9125 && string_flag)
9126 value.value = orig_value;
9127 /* Otherwise, if we have come to a subaggregate,
9128 and we don't have an element of its type, push into it. */
9129 else if (value.value != 0
9130 && value.value != error_mark_node
9131 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9132 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9133 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9135 push_init_level (loc, 1, braced_init_obstack);
9136 continue;
9139 if (constructor_max_index != 0
9140 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9141 || integer_all_onesp (constructor_max_index)))
9143 pedwarn_init (loc, 0,
9144 "excess elements in array initializer");
9145 break;
9148 /* Now output the actual element. */
9149 if (value.value)
9151 push_array_bounds (tree_to_uhwi (constructor_index));
9152 output_init_element (loc, value.value, value.original_type,
9153 strict_string, elttype,
9154 constructor_index, 1, implicit,
9155 braced_init_obstack);
9156 RESTORE_SPELLING_DEPTH (constructor_depth);
9159 constructor_index
9160 = size_binop_loc (input_location, PLUS_EXPR,
9161 constructor_index, bitsize_one_node);
9163 if (!value.value)
9164 /* If we are doing the bookkeeping for an element that was
9165 directly output as a constructor, we must update
9166 constructor_unfilled_index. */
9167 constructor_unfilled_index = constructor_index;
9169 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9171 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9173 /* Do a basic check of initializer size. Note that vectors
9174 always have a fixed size derived from their type. */
9175 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9177 pedwarn_init (loc, 0,
9178 "excess elements in vector initializer");
9179 break;
9182 /* Now output the actual element. */
9183 if (value.value)
9185 if (TREE_CODE (value.value) == VECTOR_CST)
9186 elttype = TYPE_MAIN_VARIANT (constructor_type);
9187 output_init_element (loc, value.value, value.original_type,
9188 strict_string, elttype,
9189 constructor_index, 1, implicit,
9190 braced_init_obstack);
9193 constructor_index
9194 = size_binop_loc (input_location,
9195 PLUS_EXPR, constructor_index, bitsize_one_node);
9197 if (!value.value)
9198 /* If we are doing the bookkeeping for an element that was
9199 directly output as a constructor, we must update
9200 constructor_unfilled_index. */
9201 constructor_unfilled_index = constructor_index;
9204 /* Handle the sole element allowed in a braced initializer
9205 for a scalar variable. */
9206 else if (constructor_type != error_mark_node
9207 && constructor_fields == 0)
9209 pedwarn_init (loc, 0,
9210 "excess elements in scalar initializer");
9211 break;
9213 else
9215 if (value.value)
9216 output_init_element (loc, value.value, value.original_type,
9217 strict_string, constructor_type,
9218 NULL_TREE, 1, implicit,
9219 braced_init_obstack);
9220 constructor_fields = 0;
9223 /* Handle range initializers either at this level or anywhere higher
9224 in the designator stack. */
9225 if (constructor_range_stack)
9227 struct constructor_range_stack *p, *range_stack;
9228 int finish = 0;
9230 range_stack = constructor_range_stack;
9231 constructor_range_stack = 0;
9232 while (constructor_stack != range_stack->stack)
9234 gcc_assert (constructor_stack->implicit);
9235 process_init_element (loc,
9236 pop_init_level (loc, 1,
9237 braced_init_obstack),
9238 true, braced_init_obstack);
9240 for (p = range_stack;
9241 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9242 p = p->prev)
9244 gcc_assert (constructor_stack->implicit);
9245 process_init_element (loc,
9246 pop_init_level (loc, 1,
9247 braced_init_obstack),
9248 true, braced_init_obstack);
9251 p->index = size_binop_loc (input_location,
9252 PLUS_EXPR, p->index, bitsize_one_node);
9253 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9254 finish = 1;
9256 while (1)
9258 constructor_index = p->index;
9259 constructor_fields = p->fields;
9260 if (finish && p->range_end && p->index == p->range_start)
9262 finish = 0;
9263 p->prev = 0;
9265 p = p->next;
9266 if (!p)
9267 break;
9268 push_init_level (loc, 2, braced_init_obstack);
9269 p->stack = constructor_stack;
9270 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9271 p->index = p->range_start;
9274 if (!finish)
9275 constructor_range_stack = range_stack;
9276 continue;
9279 break;
9282 constructor_range_stack = 0;
9285 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9286 (guaranteed to be 'volatile' or null) and ARGS (represented using
9287 an ASM_EXPR node). */
9288 tree
9289 build_asm_stmt (tree cv_qualifier, tree args)
9291 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9292 ASM_VOLATILE_P (args) = 1;
9293 return add_stmt (args);
9296 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9297 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9298 SIMPLE indicates whether there was anything at all after the
9299 string in the asm expression -- asm("blah") and asm("blah" : )
9300 are subtly different. We use a ASM_EXPR node to represent this. */
9301 tree
9302 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9303 tree clobbers, tree labels, bool simple)
9305 tree tail;
9306 tree args;
9307 int i;
9308 const char *constraint;
9309 const char **oconstraints;
9310 bool allows_mem, allows_reg, is_inout;
9311 int ninputs, noutputs;
9313 ninputs = list_length (inputs);
9314 noutputs = list_length (outputs);
9315 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9317 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9319 /* Remove output conversions that change the type but not the mode. */
9320 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9322 tree output = TREE_VALUE (tail);
9324 output = c_fully_fold (output, false, NULL);
9326 /* ??? Really, this should not be here. Users should be using a
9327 proper lvalue, dammit. But there's a long history of using casts
9328 in the output operands. In cases like longlong.h, this becomes a
9329 primitive form of typechecking -- if the cast can be removed, then
9330 the output operand had a type of the proper width; otherwise we'll
9331 get an error. Gross, but ... */
9332 STRIP_NOPS (output);
9334 if (!lvalue_or_else (loc, output, lv_asm))
9335 output = error_mark_node;
9337 if (output != error_mark_node
9338 && (TREE_READONLY (output)
9339 || TYPE_READONLY (TREE_TYPE (output))
9340 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9341 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9342 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9343 readonly_error (loc, output, lv_asm);
9345 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9346 oconstraints[i] = constraint;
9348 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9349 &allows_mem, &allows_reg, &is_inout))
9351 /* If the operand is going to end up in memory,
9352 mark it addressable. */
9353 if (!allows_reg && !c_mark_addressable (output))
9354 output = error_mark_node;
9355 if (!(!allows_reg && allows_mem)
9356 && output != error_mark_node
9357 && VOID_TYPE_P (TREE_TYPE (output)))
9359 error_at (loc, "invalid use of void expression");
9360 output = error_mark_node;
9363 else
9364 output = error_mark_node;
9366 TREE_VALUE (tail) = output;
9369 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9371 tree input;
9373 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9374 input = TREE_VALUE (tail);
9376 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9377 oconstraints, &allows_mem, &allows_reg))
9379 /* If the operand is going to end up in memory,
9380 mark it addressable. */
9381 if (!allows_reg && allows_mem)
9383 input = c_fully_fold (input, false, NULL);
9385 /* Strip the nops as we allow this case. FIXME, this really
9386 should be rejected or made deprecated. */
9387 STRIP_NOPS (input);
9388 if (!c_mark_addressable (input))
9389 input = error_mark_node;
9391 else
9393 struct c_expr expr;
9394 memset (&expr, 0, sizeof (expr));
9395 expr.value = input;
9396 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9397 input = c_fully_fold (expr.value, false, NULL);
9399 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9401 error_at (loc, "invalid use of void expression");
9402 input = error_mark_node;
9406 else
9407 input = error_mark_node;
9409 TREE_VALUE (tail) = input;
9412 /* ASMs with labels cannot have outputs. This should have been
9413 enforced by the parser. */
9414 gcc_assert (outputs == NULL || labels == NULL);
9416 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9418 /* asm statements without outputs, including simple ones, are treated
9419 as volatile. */
9420 ASM_INPUT_P (args) = simple;
9421 ASM_VOLATILE_P (args) = (noutputs == 0);
9423 return args;
9426 /* Generate a goto statement to LABEL. LOC is the location of the
9427 GOTO. */
9429 tree
9430 c_finish_goto_label (location_t loc, tree label)
9432 tree decl = lookup_label_for_goto (loc, label);
9433 if (!decl)
9434 return NULL_TREE;
9435 TREE_USED (decl) = 1;
9437 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9438 SET_EXPR_LOCATION (t, loc);
9439 return add_stmt (t);
9443 /* Generate a computed goto statement to EXPR. LOC is the location of
9444 the GOTO. */
9446 tree
9447 c_finish_goto_ptr (location_t loc, tree expr)
9449 tree t;
9450 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9451 expr = c_fully_fold (expr, false, NULL);
9452 expr = convert (ptr_type_node, expr);
9453 t = build1 (GOTO_EXPR, void_type_node, expr);
9454 SET_EXPR_LOCATION (t, loc);
9455 return add_stmt (t);
9458 /* Generate a C `return' statement. RETVAL is the expression for what
9459 to return, or a null pointer for `return;' with no value. LOC is
9460 the location of the return statement, or the location of the expression,
9461 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9462 is the original type of RETVAL. */
9464 tree
9465 c_finish_return (location_t loc, tree retval, tree origtype)
9467 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9468 bool no_warning = false;
9469 bool npc = false;
9470 size_t rank = 0;
9472 if (TREE_THIS_VOLATILE (current_function_decl))
9473 warning_at (loc, 0,
9474 "function declared %<noreturn%> has a %<return%> statement");
9476 if (flag_cilkplus && contains_array_notation_expr (retval))
9478 /* Array notations are allowed in a return statement if it is inside a
9479 built-in array notation reduction function. */
9480 if (!find_rank (loc, retval, retval, false, &rank))
9481 return error_mark_node;
9482 if (rank >= 1)
9484 error_at (loc, "array notation expression cannot be used as a "
9485 "return value");
9486 return error_mark_node;
9489 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9491 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9492 "allowed");
9493 return error_mark_node;
9495 if (retval)
9497 tree semantic_type = NULL_TREE;
9498 npc = null_pointer_constant_p (retval);
9499 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9501 semantic_type = TREE_TYPE (retval);
9502 retval = TREE_OPERAND (retval, 0);
9504 retval = c_fully_fold (retval, false, NULL);
9505 if (semantic_type)
9506 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9509 if (!retval)
9511 current_function_returns_null = 1;
9512 if ((warn_return_type || flag_isoc99)
9513 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9515 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
9516 "%<return%> with no value, in "
9517 "function returning non-void");
9518 no_warning = true;
9521 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9523 current_function_returns_null = 1;
9524 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9525 pedwarn (loc, 0,
9526 "%<return%> with a value, in function returning void");
9527 else
9528 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9529 "%<return%> with expression, in function returning void");
9531 else
9533 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9534 retval, origtype, ic_return,
9535 npc, NULL_TREE, NULL_TREE, 0);
9536 tree res = DECL_RESULT (current_function_decl);
9537 tree inner;
9538 bool save;
9540 current_function_returns_value = 1;
9541 if (t == error_mark_node)
9542 return NULL_TREE;
9544 save = in_late_binary_op;
9545 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9546 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9547 in_late_binary_op = true;
9548 inner = t = convert (TREE_TYPE (res), t);
9549 in_late_binary_op = save;
9551 /* Strip any conversions, additions, and subtractions, and see if
9552 we are returning the address of a local variable. Warn if so. */
9553 while (1)
9555 switch (TREE_CODE (inner))
9557 CASE_CONVERT:
9558 case NON_LVALUE_EXPR:
9559 case PLUS_EXPR:
9560 case POINTER_PLUS_EXPR:
9561 inner = TREE_OPERAND (inner, 0);
9562 continue;
9564 case MINUS_EXPR:
9565 /* If the second operand of the MINUS_EXPR has a pointer
9566 type (or is converted from it), this may be valid, so
9567 don't give a warning. */
9569 tree op1 = TREE_OPERAND (inner, 1);
9571 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9572 && (CONVERT_EXPR_P (op1)
9573 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9574 op1 = TREE_OPERAND (op1, 0);
9576 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9577 break;
9579 inner = TREE_OPERAND (inner, 0);
9580 continue;
9583 case ADDR_EXPR:
9584 inner = TREE_OPERAND (inner, 0);
9586 while (REFERENCE_CLASS_P (inner)
9587 && TREE_CODE (inner) != INDIRECT_REF)
9588 inner = TREE_OPERAND (inner, 0);
9590 if (DECL_P (inner)
9591 && !DECL_EXTERNAL (inner)
9592 && !TREE_STATIC (inner)
9593 && DECL_CONTEXT (inner) == current_function_decl)
9595 if (TREE_CODE (inner) == LABEL_DECL)
9596 warning_at (loc, OPT_Wreturn_local_addr,
9597 "function returns address of label");
9598 else
9599 warning_at (loc, OPT_Wreturn_local_addr,
9600 "function returns address of local variable");
9602 break;
9604 default:
9605 break;
9608 break;
9611 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9612 SET_EXPR_LOCATION (retval, loc);
9614 if (warn_sequence_point)
9615 verify_sequence_points (retval);
9618 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9619 TREE_NO_WARNING (ret_stmt) |= no_warning;
9620 return add_stmt (ret_stmt);
9623 /* Convert EXPR to TYPE if the type of EXPR is
9624 assignment compatible with TYPE.
9625 Otherwise, issue an error (or warning) as appropriate. */
9627 tree
9628 c_cvt_expr_for_assign (location_t loc, tree type, tree expr)
9630 if (expr == NULL_TREE || expr == error_mark_node)
9631 return expr;
9632 return convert_for_assignment (loc, UNKNOWN_LOCATION, type,
9633 expr, TREE_TYPE (expr),
9634 ic_assign, false, NULL_TREE, NULL_TREE, 0);
9637 struct c_switch {
9638 /* The SWITCH_EXPR being built. */
9639 tree switch_expr;
9641 /* The original type of the testing expression, i.e. before the
9642 default conversion is applied. */
9643 tree orig_type;
9645 /* A splay-tree mapping the low element of a case range to the high
9646 element, or NULL_TREE if there is no high element. Used to
9647 determine whether or not a new case label duplicates an old case
9648 label. We need a tree, rather than simply a hash table, because
9649 of the GNU case range extension. */
9650 splay_tree cases;
9652 /* The bindings at the point of the switch. This is used for
9653 warnings crossing decls when branching to a case label. */
9654 struct c_spot_bindings *bindings;
9656 /* The next node on the stack. */
9657 struct c_switch *next;
9660 /* A stack of the currently active switch statements. The innermost
9661 switch statement is on the top of the stack. There is no need to
9662 mark the stack for garbage collection because it is only active
9663 during the processing of the body of a function, and we never
9664 collect at that point. */
9666 struct c_switch *c_switch_stack;
9668 /* Start a C switch statement, testing expression EXP. Return the new
9669 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9670 SWITCH_COND_LOC is the location of the switch's condition.
9671 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9673 tree
9674 c_start_case (location_t switch_loc,
9675 location_t switch_cond_loc,
9676 tree exp, bool explicit_cast_p)
9678 tree orig_type = error_mark_node;
9679 struct c_switch *cs;
9681 if (exp != error_mark_node)
9683 orig_type = TREE_TYPE (exp);
9685 if (!INTEGRAL_TYPE_P (orig_type))
9687 if (orig_type != error_mark_node)
9689 error_at (switch_cond_loc, "switch quantity not an integer");
9690 orig_type = error_mark_node;
9692 exp = integer_zero_node;
9694 else
9696 tree type = TYPE_MAIN_VARIANT (orig_type);
9697 tree e = exp;
9699 /* Warn if the condition has boolean value. */
9700 while (TREE_CODE (e) == COMPOUND_EXPR)
9701 e = TREE_OPERAND (e, 1);
9703 if ((TREE_CODE (type) == BOOLEAN_TYPE
9704 || truth_value_p (TREE_CODE (e)))
9705 /* Explicit cast to int suppresses this warning. */
9706 && !(TREE_CODE (type) == INTEGER_TYPE
9707 && explicit_cast_p))
9708 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9709 "switch condition has boolean value");
9711 if (!in_system_header_at (input_location)
9712 && (type == long_integer_type_node
9713 || type == long_unsigned_type_node))
9714 warning_at (switch_cond_loc,
9715 OPT_Wtraditional, "%<long%> switch expression not "
9716 "converted to %<int%> in ISO C");
9718 exp = c_fully_fold (exp, false, NULL);
9719 exp = default_conversion (exp);
9721 if (warn_sequence_point)
9722 verify_sequence_points (exp);
9726 /* Add this new SWITCH_EXPR to the stack. */
9727 cs = XNEW (struct c_switch);
9728 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9729 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9730 cs->orig_type = orig_type;
9731 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9732 cs->bindings = c_get_switch_bindings ();
9733 cs->next = c_switch_stack;
9734 c_switch_stack = cs;
9736 return add_stmt (cs->switch_expr);
9739 /* Process a case label at location LOC. */
9741 tree
9742 do_case (location_t loc, tree low_value, tree high_value)
9744 tree label = NULL_TREE;
9746 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9748 low_value = c_fully_fold (low_value, false, NULL);
9749 if (TREE_CODE (low_value) == INTEGER_CST)
9750 pedwarn (loc, OPT_Wpedantic,
9751 "case label is not an integer constant expression");
9754 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9756 high_value = c_fully_fold (high_value, false, NULL);
9757 if (TREE_CODE (high_value) == INTEGER_CST)
9758 pedwarn (input_location, OPT_Wpedantic,
9759 "case label is not an integer constant expression");
9762 if (c_switch_stack == NULL)
9764 if (low_value)
9765 error_at (loc, "case label not within a switch statement");
9766 else
9767 error_at (loc, "%<default%> label not within a switch statement");
9768 return NULL_TREE;
9771 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9772 EXPR_LOCATION (c_switch_stack->switch_expr),
9773 loc))
9774 return NULL_TREE;
9776 label = c_add_case_label (loc, c_switch_stack->cases,
9777 SWITCH_COND (c_switch_stack->switch_expr),
9778 c_switch_stack->orig_type,
9779 low_value, high_value);
9780 if (label == error_mark_node)
9781 label = NULL_TREE;
9782 return label;
9785 /* Finish the switch statement. */
9787 void
9788 c_finish_case (tree body)
9790 struct c_switch *cs = c_switch_stack;
9791 location_t switch_location;
9793 SWITCH_BODY (cs->switch_expr) = body;
9795 /* Emit warnings as needed. */
9796 switch_location = EXPR_LOCATION (cs->switch_expr);
9797 c_do_switch_warnings (cs->cases, switch_location,
9798 TREE_TYPE (cs->switch_expr),
9799 SWITCH_COND (cs->switch_expr));
9801 /* Pop the stack. */
9802 c_switch_stack = cs->next;
9803 splay_tree_delete (cs->cases);
9804 c_release_switch_bindings (cs->bindings);
9805 XDELETE (cs);
9808 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9809 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9810 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9811 statement, and was not surrounded with parenthesis. */
9813 void
9814 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9815 tree else_block, bool nested_if)
9817 tree stmt;
9819 /* If the condition has array notations, then the rank of the then_block and
9820 else_block must be either 0 or be equal to the rank of the condition. If
9821 the condition does not have array notations then break them up as it is
9822 broken up in a normal expression. */
9823 if (flag_cilkplus && contains_array_notation_expr (cond))
9825 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9826 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9827 return;
9828 if (then_block
9829 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9830 return;
9831 if (else_block
9832 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9833 return;
9834 if (cond_rank != then_rank && then_rank != 0)
9836 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9837 " and the then-block");
9838 return;
9840 else if (cond_rank != else_rank && else_rank != 0)
9842 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9843 " and the else-block");
9844 return;
9847 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9848 if (warn_parentheses && nested_if && else_block == NULL)
9850 tree inner_if = then_block;
9852 /* We know from the grammar productions that there is an IF nested
9853 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9854 it might not be exactly THEN_BLOCK, but should be the last
9855 non-container statement within. */
9856 while (1)
9857 switch (TREE_CODE (inner_if))
9859 case COND_EXPR:
9860 goto found;
9861 case BIND_EXPR:
9862 inner_if = BIND_EXPR_BODY (inner_if);
9863 break;
9864 case STATEMENT_LIST:
9865 inner_if = expr_last (then_block);
9866 break;
9867 case TRY_FINALLY_EXPR:
9868 case TRY_CATCH_EXPR:
9869 inner_if = TREE_OPERAND (inner_if, 0);
9870 break;
9871 default:
9872 gcc_unreachable ();
9874 found:
9876 if (COND_EXPR_ELSE (inner_if))
9877 warning_at (if_locus, OPT_Wparentheses,
9878 "suggest explicit braces to avoid ambiguous %<else%>");
9881 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9882 SET_EXPR_LOCATION (stmt, if_locus);
9883 add_stmt (stmt);
9886 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9887 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9888 is false for DO loops. INCR is the FOR increment expression. BODY is
9889 the statement controlled by the loop. BLAB is the break label. CLAB is
9890 the continue label. Everything is allowed to be NULL. */
9892 void
9893 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9894 tree blab, tree clab, bool cond_is_first)
9896 tree entry = NULL, exit = NULL, t;
9898 if (flag_cilkplus && contains_array_notation_expr (cond))
9900 error_at (start_locus, "array notation expression cannot be used in a "
9901 "loop%'s condition");
9902 return;
9905 /* If the condition is zero don't generate a loop construct. */
9906 if (cond && integer_zerop (cond))
9908 if (cond_is_first)
9910 t = build_and_jump (&blab);
9911 SET_EXPR_LOCATION (t, start_locus);
9912 add_stmt (t);
9915 else
9917 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9919 /* If we have an exit condition, then we build an IF with gotos either
9920 out of the loop, or to the top of it. If there's no exit condition,
9921 then we just build a jump back to the top. */
9922 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9924 if (cond && !integer_nonzerop (cond))
9926 /* Canonicalize the loop condition to the end. This means
9927 generating a branch to the loop condition. Reuse the
9928 continue label, if possible. */
9929 if (cond_is_first)
9931 if (incr || !clab)
9933 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9934 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9936 else
9937 t = build1 (GOTO_EXPR, void_type_node, clab);
9938 SET_EXPR_LOCATION (t, start_locus);
9939 add_stmt (t);
9942 t = build_and_jump (&blab);
9943 if (cond_is_first)
9944 exit = fold_build3_loc (start_locus,
9945 COND_EXPR, void_type_node, cond, exit, t);
9946 else
9947 exit = fold_build3_loc (input_location,
9948 COND_EXPR, void_type_node, cond, exit, t);
9951 add_stmt (top);
9954 if (body)
9955 add_stmt (body);
9956 if (clab)
9957 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9958 if (incr)
9959 add_stmt (incr);
9960 if (entry)
9961 add_stmt (entry);
9962 if (exit)
9963 add_stmt (exit);
9964 if (blab)
9965 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9968 tree
9969 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9971 bool skip;
9972 tree label = *label_p;
9974 /* In switch statements break is sometimes stylistically used after
9975 a return statement. This can lead to spurious warnings about
9976 control reaching the end of a non-void function when it is
9977 inlined. Note that we are calling block_may_fallthru with
9978 language specific tree nodes; this works because
9979 block_may_fallthru returns true when given something it does not
9980 understand. */
9981 skip = !block_may_fallthru (cur_stmt_list);
9983 if (!label)
9985 if (!skip)
9986 *label_p = label = create_artificial_label (loc);
9988 else if (TREE_CODE (label) == LABEL_DECL)
9990 else switch (TREE_INT_CST_LOW (label))
9992 case 0:
9993 if (is_break)
9994 error_at (loc, "break statement not within loop or switch");
9995 else
9996 error_at (loc, "continue statement not within a loop");
9997 return NULL_TREE;
9999 case 1:
10000 gcc_assert (is_break);
10001 error_at (loc, "break statement used with OpenMP for loop");
10002 return NULL_TREE;
10004 case 2:
10005 if (is_break)
10006 error ("break statement within %<#pragma simd%> loop body");
10007 else
10008 error ("continue statement within %<#pragma simd%> loop body");
10009 return NULL_TREE;
10011 default:
10012 gcc_unreachable ();
10015 if (skip)
10016 return NULL_TREE;
10018 if (!is_break)
10019 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10021 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10024 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10026 static void
10027 emit_side_effect_warnings (location_t loc, tree expr)
10029 if (expr == error_mark_node)
10031 else if (!TREE_SIDE_EFFECTS (expr))
10033 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10034 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10036 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10038 tree r = expr;
10039 location_t cloc = loc;
10040 while (TREE_CODE (r) == COMPOUND_EXPR)
10042 if (EXPR_HAS_LOCATION (r))
10043 cloc = EXPR_LOCATION (r);
10044 r = TREE_OPERAND (r, 1);
10046 if (!TREE_SIDE_EFFECTS (r)
10047 && !VOID_TYPE_P (TREE_TYPE (r))
10048 && !CONVERT_EXPR_P (r)
10049 && !TREE_NO_WARNING (r)
10050 && !TREE_NO_WARNING (expr))
10051 warning_at (cloc, OPT_Wunused_value,
10052 "right-hand operand of comma expression has no effect");
10054 else
10055 warn_if_unused_value (expr, loc);
10058 /* Process an expression as if it were a complete statement. Emit
10059 diagnostics, but do not call ADD_STMT. LOC is the location of the
10060 statement. */
10062 tree
10063 c_process_expr_stmt (location_t loc, tree expr)
10065 tree exprv;
10067 if (!expr)
10068 return NULL_TREE;
10070 expr = c_fully_fold (expr, false, NULL);
10072 if (warn_sequence_point)
10073 verify_sequence_points (expr);
10075 if (TREE_TYPE (expr) != error_mark_node
10076 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10077 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10078 error_at (loc, "expression statement has incomplete type");
10080 /* If we're not processing a statement expression, warn about unused values.
10081 Warnings for statement expressions will be emitted later, once we figure
10082 out which is the result. */
10083 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10084 && warn_unused_value)
10085 emit_side_effect_warnings (loc, expr);
10087 exprv = expr;
10088 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10089 exprv = TREE_OPERAND (exprv, 1);
10090 while (CONVERT_EXPR_P (exprv))
10091 exprv = TREE_OPERAND (exprv, 0);
10092 if (DECL_P (exprv)
10093 || handled_component_p (exprv)
10094 || TREE_CODE (exprv) == ADDR_EXPR)
10095 mark_exp_read (exprv);
10097 /* If the expression is not of a type to which we cannot assign a line
10098 number, wrap the thing in a no-op NOP_EXPR. */
10099 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10101 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10102 SET_EXPR_LOCATION (expr, loc);
10105 return expr;
10108 /* Emit an expression as a statement. LOC is the location of the
10109 expression. */
10111 tree
10112 c_finish_expr_stmt (location_t loc, tree expr)
10114 if (expr)
10115 return add_stmt (c_process_expr_stmt (loc, expr));
10116 else
10117 return NULL;
10120 /* Do the opposite and emit a statement as an expression. To begin,
10121 create a new binding level and return it. */
10123 tree
10124 c_begin_stmt_expr (void)
10126 tree ret;
10128 /* We must force a BLOCK for this level so that, if it is not expanded
10129 later, there is a way to turn off the entire subtree of blocks that
10130 are contained in it. */
10131 keep_next_level ();
10132 ret = c_begin_compound_stmt (true);
10134 c_bindings_start_stmt_expr (c_switch_stack == NULL
10135 ? NULL
10136 : c_switch_stack->bindings);
10138 /* Mark the current statement list as belonging to a statement list. */
10139 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10141 return ret;
10144 /* LOC is the location of the compound statement to which this body
10145 belongs. */
10147 tree
10148 c_finish_stmt_expr (location_t loc, tree body)
10150 tree last, type, tmp, val;
10151 tree *last_p;
10153 body = c_end_compound_stmt (loc, body, true);
10155 c_bindings_end_stmt_expr (c_switch_stack == NULL
10156 ? NULL
10157 : c_switch_stack->bindings);
10159 /* Locate the last statement in BODY. See c_end_compound_stmt
10160 about always returning a BIND_EXPR. */
10161 last_p = &BIND_EXPR_BODY (body);
10162 last = BIND_EXPR_BODY (body);
10164 continue_searching:
10165 if (TREE_CODE (last) == STATEMENT_LIST)
10167 tree_stmt_iterator i;
10169 /* This can happen with degenerate cases like ({ }). No value. */
10170 if (!TREE_SIDE_EFFECTS (last))
10171 return body;
10173 /* If we're supposed to generate side effects warnings, process
10174 all of the statements except the last. */
10175 if (warn_unused_value)
10177 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10179 location_t tloc;
10180 tree t = tsi_stmt (i);
10182 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10183 emit_side_effect_warnings (tloc, t);
10186 else
10187 i = tsi_last (last);
10188 last_p = tsi_stmt_ptr (i);
10189 last = *last_p;
10192 /* If the end of the list is exception related, then the list was split
10193 by a call to push_cleanup. Continue searching. */
10194 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10195 || TREE_CODE (last) == TRY_CATCH_EXPR)
10197 last_p = &TREE_OPERAND (last, 0);
10198 last = *last_p;
10199 goto continue_searching;
10202 if (last == error_mark_node)
10203 return last;
10205 /* In the case that the BIND_EXPR is not necessary, return the
10206 expression out from inside it. */
10207 if (last == BIND_EXPR_BODY (body)
10208 && BIND_EXPR_VARS (body) == NULL)
10210 /* Even if this looks constant, do not allow it in a constant
10211 expression. */
10212 last = c_wrap_maybe_const (last, true);
10213 /* Do not warn if the return value of a statement expression is
10214 unused. */
10215 TREE_NO_WARNING (last) = 1;
10216 return last;
10219 /* Extract the type of said expression. */
10220 type = TREE_TYPE (last);
10222 /* If we're not returning a value at all, then the BIND_EXPR that
10223 we already have is a fine expression to return. */
10224 if (!type || VOID_TYPE_P (type))
10225 return body;
10227 /* Now that we've located the expression containing the value, it seems
10228 silly to make voidify_wrapper_expr repeat the process. Create a
10229 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10230 tmp = create_tmp_var_raw (type, NULL);
10232 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10233 tree_expr_nonnegative_p giving up immediately. */
10234 val = last;
10235 if (TREE_CODE (val) == NOP_EXPR
10236 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10237 val = TREE_OPERAND (val, 0);
10239 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10240 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10243 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10244 SET_EXPR_LOCATION (t, loc);
10245 return t;
10249 /* Begin and end compound statements. This is as simple as pushing
10250 and popping new statement lists from the tree. */
10252 tree
10253 c_begin_compound_stmt (bool do_scope)
10255 tree stmt = push_stmt_list ();
10256 if (do_scope)
10257 push_scope ();
10258 return stmt;
10261 /* End a compound statement. STMT is the statement. LOC is the
10262 location of the compound statement-- this is usually the location
10263 of the opening brace. */
10265 tree
10266 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10268 tree block = NULL;
10270 if (do_scope)
10272 if (c_dialect_objc ())
10273 objc_clear_super_receiver ();
10274 block = pop_scope ();
10277 stmt = pop_stmt_list (stmt);
10278 stmt = c_build_bind_expr (loc, block, stmt);
10280 /* If this compound statement is nested immediately inside a statement
10281 expression, then force a BIND_EXPR to be created. Otherwise we'll
10282 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10283 STATEMENT_LISTs merge, and thus we can lose track of what statement
10284 was really last. */
10285 if (building_stmt_list_p ()
10286 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10287 && TREE_CODE (stmt) != BIND_EXPR)
10289 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10290 TREE_SIDE_EFFECTS (stmt) = 1;
10291 SET_EXPR_LOCATION (stmt, loc);
10294 return stmt;
10297 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10298 when the current scope is exited. EH_ONLY is true when this is not
10299 meant to apply to normal control flow transfer. */
10301 void
10302 push_cleanup (tree decl, tree cleanup, bool eh_only)
10304 enum tree_code code;
10305 tree stmt, list;
10306 bool stmt_expr;
10308 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10309 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10310 add_stmt (stmt);
10311 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10312 list = push_stmt_list ();
10313 TREE_OPERAND (stmt, 0) = list;
10314 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10317 /* Build a binary-operation expression without default conversions.
10318 CODE is the kind of expression to build.
10319 LOCATION is the operator's location.
10320 This function differs from `build' in several ways:
10321 the data type of the result is computed and recorded in it,
10322 warnings are generated if arg data types are invalid,
10323 special handling for addition and subtraction of pointers is known,
10324 and some optimization is done (operations on narrow ints
10325 are done in the narrower type when that gives the same result).
10326 Constant folding is also done before the result is returned.
10328 Note that the operands will never have enumeral types, or function
10329 or array types, because either they will have the default conversions
10330 performed or they have both just been converted to some other type in which
10331 the arithmetic is to be done. */
10333 tree
10334 build_binary_op (location_t location, enum tree_code code,
10335 tree orig_op0, tree orig_op1, int convert_p)
10337 tree type0, type1, orig_type0, orig_type1;
10338 tree eptype;
10339 enum tree_code code0, code1;
10340 tree op0, op1;
10341 tree ret = error_mark_node;
10342 const char *invalid_op_diag;
10343 bool op0_int_operands, op1_int_operands;
10344 bool int_const, int_const_or_overflow, int_operands;
10346 /* Expression code to give to the expression when it is built.
10347 Normally this is CODE, which is what the caller asked for,
10348 but in some special cases we change it. */
10349 enum tree_code resultcode = code;
10351 /* Data type in which the computation is to be performed.
10352 In the simplest cases this is the common type of the arguments. */
10353 tree result_type = NULL;
10355 /* When the computation is in excess precision, the type of the
10356 final EXCESS_PRECISION_EXPR. */
10357 tree semantic_result_type = NULL;
10359 /* Nonzero means operands have already been type-converted
10360 in whatever way is necessary.
10361 Zero means they need to be converted to RESULT_TYPE. */
10362 int converted = 0;
10364 /* Nonzero means create the expression with this type, rather than
10365 RESULT_TYPE. */
10366 tree build_type = 0;
10368 /* Nonzero means after finally constructing the expression
10369 convert it to this type. */
10370 tree final_type = 0;
10372 /* Nonzero if this is an operation like MIN or MAX which can
10373 safely be computed in short if both args are promoted shorts.
10374 Also implies COMMON.
10375 -1 indicates a bitwise operation; this makes a difference
10376 in the exact conditions for when it is safe to do the operation
10377 in a narrower mode. */
10378 int shorten = 0;
10380 /* Nonzero if this is a comparison operation;
10381 if both args are promoted shorts, compare the original shorts.
10382 Also implies COMMON. */
10383 int short_compare = 0;
10385 /* Nonzero if this is a right-shift operation, which can be computed on the
10386 original short and then promoted if the operand is a promoted short. */
10387 int short_shift = 0;
10389 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10390 int common = 0;
10392 /* True means types are compatible as far as ObjC is concerned. */
10393 bool objc_ok;
10395 /* True means this is an arithmetic operation that may need excess
10396 precision. */
10397 bool may_need_excess_precision;
10399 /* True means this is a boolean operation that converts both its
10400 operands to truth-values. */
10401 bool boolean_op = false;
10403 /* Remember whether we're doing / or %. */
10404 bool doing_div_or_mod = false;
10406 /* Remember whether we're doing << or >>. */
10407 bool doing_shift = false;
10409 /* Tree holding instrumentation expression. */
10410 tree instrument_expr = NULL;
10412 if (location == UNKNOWN_LOCATION)
10413 location = input_location;
10415 op0 = orig_op0;
10416 op1 = orig_op1;
10418 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10419 if (op0_int_operands)
10420 op0 = remove_c_maybe_const_expr (op0);
10421 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10422 if (op1_int_operands)
10423 op1 = remove_c_maybe_const_expr (op1);
10424 int_operands = (op0_int_operands && op1_int_operands);
10425 if (int_operands)
10427 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10428 && TREE_CODE (orig_op1) == INTEGER_CST);
10429 int_const = (int_const_or_overflow
10430 && !TREE_OVERFLOW (orig_op0)
10431 && !TREE_OVERFLOW (orig_op1));
10433 else
10434 int_const = int_const_or_overflow = false;
10436 /* Do not apply default conversion in mixed vector/scalar expression. */
10437 if (convert_p
10438 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10439 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10441 op0 = default_conversion (op0);
10442 op1 = default_conversion (op1);
10445 /* When Cilk Plus is enabled and there are array notations inside op0, then
10446 we check to see if there are builtin array notation functions. If
10447 so, then we take on the type of the array notation inside it. */
10448 if (flag_cilkplus && contains_array_notation_expr (op0))
10449 orig_type0 = type0 = find_correct_array_notation_type (op0);
10450 else
10451 orig_type0 = type0 = TREE_TYPE (op0);
10453 if (flag_cilkplus && contains_array_notation_expr (op1))
10454 orig_type1 = type1 = find_correct_array_notation_type (op1);
10455 else
10456 orig_type1 = type1 = TREE_TYPE (op1);
10458 /* The expression codes of the data types of the arguments tell us
10459 whether the arguments are integers, floating, pointers, etc. */
10460 code0 = TREE_CODE (type0);
10461 code1 = TREE_CODE (type1);
10463 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10464 STRIP_TYPE_NOPS (op0);
10465 STRIP_TYPE_NOPS (op1);
10467 /* If an error was already reported for one of the arguments,
10468 avoid reporting another error. */
10470 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10471 return error_mark_node;
10473 if ((invalid_op_diag
10474 = targetm.invalid_binary_op (code, type0, type1)))
10476 error_at (location, invalid_op_diag);
10477 return error_mark_node;
10480 switch (code)
10482 case PLUS_EXPR:
10483 case MINUS_EXPR:
10484 case MULT_EXPR:
10485 case TRUNC_DIV_EXPR:
10486 case CEIL_DIV_EXPR:
10487 case FLOOR_DIV_EXPR:
10488 case ROUND_DIV_EXPR:
10489 case EXACT_DIV_EXPR:
10490 may_need_excess_precision = true;
10491 break;
10492 default:
10493 may_need_excess_precision = false;
10494 break;
10496 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10498 op0 = TREE_OPERAND (op0, 0);
10499 type0 = TREE_TYPE (op0);
10501 else if (may_need_excess_precision
10502 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10504 type0 = eptype;
10505 op0 = convert (eptype, op0);
10507 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10509 op1 = TREE_OPERAND (op1, 0);
10510 type1 = TREE_TYPE (op1);
10512 else if (may_need_excess_precision
10513 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10515 type1 = eptype;
10516 op1 = convert (eptype, op1);
10519 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10521 /* In case when one of the operands of the binary operation is
10522 a vector and another is a scalar -- convert scalar to vector. */
10523 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10525 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10526 true);
10528 switch (convert_flag)
10530 case stv_error:
10531 return error_mark_node;
10532 case stv_firstarg:
10534 bool maybe_const = true;
10535 tree sc;
10536 sc = c_fully_fold (op0, false, &maybe_const);
10537 sc = save_expr (sc);
10538 sc = convert (TREE_TYPE (type1), sc);
10539 op0 = build_vector_from_val (type1, sc);
10540 if (!maybe_const)
10541 op0 = c_wrap_maybe_const (op0, true);
10542 orig_type0 = type0 = TREE_TYPE (op0);
10543 code0 = TREE_CODE (type0);
10544 converted = 1;
10545 break;
10547 case stv_secondarg:
10549 bool maybe_const = true;
10550 tree sc;
10551 sc = c_fully_fold (op1, false, &maybe_const);
10552 sc = save_expr (sc);
10553 sc = convert (TREE_TYPE (type0), sc);
10554 op1 = build_vector_from_val (type0, sc);
10555 if (!maybe_const)
10556 op1 = c_wrap_maybe_const (op1, true);
10557 orig_type1 = type1 = TREE_TYPE (op1);
10558 code1 = TREE_CODE (type1);
10559 converted = 1;
10560 break;
10562 default:
10563 break;
10567 switch (code)
10569 case PLUS_EXPR:
10570 /* Handle the pointer + int case. */
10571 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10573 ret = c_pointer_int_sum (location, PLUS_EXPR, op0, op1);
10574 goto return_build_binary_op;
10576 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10578 ret = c_pointer_int_sum (location, PLUS_EXPR, op1, op0);
10579 goto return_build_binary_op;
10581 else
10582 common = 1;
10583 break;
10585 case MINUS_EXPR:
10586 /* Subtraction of two similar pointers.
10587 We must subtract them as integers, then divide by object size. */
10588 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10589 && comp_target_types (location, type0, type1))
10591 ret = pointer_diff (location, op0, op1);
10592 goto return_build_binary_op;
10594 /* Handle pointer minus int. Just like pointer plus int. */
10595 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10597 ret = c_pointer_int_sum (location, MINUS_EXPR, op0, op1);
10598 goto return_build_binary_op;
10600 else
10601 common = 1;
10602 break;
10604 case MULT_EXPR:
10605 common = 1;
10606 break;
10608 case TRUNC_DIV_EXPR:
10609 case CEIL_DIV_EXPR:
10610 case FLOOR_DIV_EXPR:
10611 case ROUND_DIV_EXPR:
10612 case EXACT_DIV_EXPR:
10613 doing_div_or_mod = true;
10614 warn_for_div_by_zero (location, op1);
10616 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10617 || code0 == FIXED_POINT_TYPE
10618 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10619 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10620 || code1 == FIXED_POINT_TYPE
10621 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10623 enum tree_code tcode0 = code0, tcode1 = code1;
10625 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10626 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10627 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10628 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10630 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10631 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10632 resultcode = RDIV_EXPR;
10633 else
10634 /* Although it would be tempting to shorten always here, that
10635 loses on some targets, since the modulo instruction is
10636 undefined if the quotient can't be represented in the
10637 computation mode. We shorten only if unsigned or if
10638 dividing by something we know != -1. */
10639 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10640 || (TREE_CODE (op1) == INTEGER_CST
10641 && !integer_all_onesp (op1)));
10642 common = 1;
10644 break;
10646 case BIT_AND_EXPR:
10647 case BIT_IOR_EXPR:
10648 case BIT_XOR_EXPR:
10649 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10650 shorten = -1;
10651 /* Allow vector types which are not floating point types. */
10652 else if (code0 == VECTOR_TYPE
10653 && code1 == VECTOR_TYPE
10654 && !VECTOR_FLOAT_TYPE_P (type0)
10655 && !VECTOR_FLOAT_TYPE_P (type1))
10656 common = 1;
10657 break;
10659 case TRUNC_MOD_EXPR:
10660 case FLOOR_MOD_EXPR:
10661 doing_div_or_mod = true;
10662 warn_for_div_by_zero (location, op1);
10664 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10665 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10666 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10667 common = 1;
10668 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10670 /* Although it would be tempting to shorten always here, that loses
10671 on some targets, since the modulo instruction is undefined if the
10672 quotient can't be represented in the computation mode. We shorten
10673 only if unsigned or if dividing by something we know != -1. */
10674 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10675 || (TREE_CODE (op1) == INTEGER_CST
10676 && !integer_all_onesp (op1)));
10677 common = 1;
10679 break;
10681 case TRUTH_ANDIF_EXPR:
10682 case TRUTH_ORIF_EXPR:
10683 case TRUTH_AND_EXPR:
10684 case TRUTH_OR_EXPR:
10685 case TRUTH_XOR_EXPR:
10686 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10687 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10688 || code0 == FIXED_POINT_TYPE)
10689 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10690 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10691 || code1 == FIXED_POINT_TYPE))
10693 /* Result of these operations is always an int,
10694 but that does not mean the operands should be
10695 converted to ints! */
10696 result_type = integer_type_node;
10697 if (op0_int_operands)
10699 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10700 op0 = remove_c_maybe_const_expr (op0);
10702 else
10703 op0 = c_objc_common_truthvalue_conversion (location, op0);
10704 if (op1_int_operands)
10706 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10707 op1 = remove_c_maybe_const_expr (op1);
10709 else
10710 op1 = c_objc_common_truthvalue_conversion (location, op1);
10711 converted = 1;
10712 boolean_op = true;
10714 if (code == TRUTH_ANDIF_EXPR)
10716 int_const_or_overflow = (int_operands
10717 && TREE_CODE (orig_op0) == INTEGER_CST
10718 && (op0 == truthvalue_false_node
10719 || TREE_CODE (orig_op1) == INTEGER_CST));
10720 int_const = (int_const_or_overflow
10721 && !TREE_OVERFLOW (orig_op0)
10722 && (op0 == truthvalue_false_node
10723 || !TREE_OVERFLOW (orig_op1)));
10725 else if (code == TRUTH_ORIF_EXPR)
10727 int_const_or_overflow = (int_operands
10728 && TREE_CODE (orig_op0) == INTEGER_CST
10729 && (op0 == truthvalue_true_node
10730 || TREE_CODE (orig_op1) == INTEGER_CST));
10731 int_const = (int_const_or_overflow
10732 && !TREE_OVERFLOW (orig_op0)
10733 && (op0 == truthvalue_true_node
10734 || !TREE_OVERFLOW (orig_op1)));
10736 break;
10738 /* Shift operations: result has same type as first operand;
10739 always convert second operand to int.
10740 Also set SHORT_SHIFT if shifting rightward. */
10742 case RSHIFT_EXPR:
10743 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10744 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10746 result_type = type0;
10747 converted = 1;
10749 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10750 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10751 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10752 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10754 result_type = type0;
10755 converted = 1;
10757 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10758 && code1 == INTEGER_TYPE)
10760 doing_shift = true;
10761 if (TREE_CODE (op1) == INTEGER_CST)
10763 if (tree_int_cst_sgn (op1) < 0)
10765 int_const = false;
10766 if (c_inhibit_evaluation_warnings == 0)
10767 warning_at (location, 0, "right shift count is negative");
10769 else
10771 if (!integer_zerop (op1))
10772 short_shift = 1;
10774 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10776 int_const = false;
10777 if (c_inhibit_evaluation_warnings == 0)
10778 warning_at (location, 0, "right shift count >= width "
10779 "of type");
10784 /* Use the type of the value to be shifted. */
10785 result_type = type0;
10786 /* Convert the non vector shift-count to an integer, regardless
10787 of size of value being shifted. */
10788 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10789 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10790 op1 = convert (integer_type_node, op1);
10791 /* Avoid converting op1 to result_type later. */
10792 converted = 1;
10794 break;
10796 case LSHIFT_EXPR:
10797 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10798 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10800 result_type = type0;
10801 converted = 1;
10803 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10804 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10805 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10806 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10808 result_type = type0;
10809 converted = 1;
10811 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10812 && code1 == INTEGER_TYPE)
10814 doing_shift = true;
10815 if (TREE_CODE (op1) == INTEGER_CST)
10817 if (tree_int_cst_sgn (op1) < 0)
10819 int_const = false;
10820 if (c_inhibit_evaluation_warnings == 0)
10821 warning_at (location, 0, "left shift count is negative");
10824 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10826 int_const = false;
10827 if (c_inhibit_evaluation_warnings == 0)
10828 warning_at (location, 0, "left shift count >= width of "
10829 "type");
10833 /* Use the type of the value to be shifted. */
10834 result_type = type0;
10835 /* Convert the non vector shift-count to an integer, regardless
10836 of size of value being shifted. */
10837 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10838 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10839 op1 = convert (integer_type_node, op1);
10840 /* Avoid converting op1 to result_type later. */
10841 converted = 1;
10843 break;
10845 case EQ_EXPR:
10846 case NE_EXPR:
10847 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10849 tree intt;
10850 if (!vector_types_compatible_elements_p (type0, type1))
10852 error_at (location, "comparing vectors with different "
10853 "element types");
10854 return error_mark_node;
10857 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10859 error_at (location, "comparing vectors with different "
10860 "number of elements");
10861 return error_mark_node;
10864 /* Always construct signed integer vector type. */
10865 intt = c_common_type_for_size (GET_MODE_BITSIZE
10866 (TYPE_MODE (TREE_TYPE (type0))), 0);
10867 result_type = build_opaque_vector_type (intt,
10868 TYPE_VECTOR_SUBPARTS (type0));
10869 converted = 1;
10870 break;
10872 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10873 warning_at (location,
10874 OPT_Wfloat_equal,
10875 "comparing floating point with == or != is unsafe");
10876 /* Result of comparison is always int,
10877 but don't convert the args to int! */
10878 build_type = integer_type_node;
10879 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10880 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10881 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10882 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10883 short_compare = 1;
10884 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10886 if (TREE_CODE (op0) == ADDR_EXPR
10887 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10889 if (code == EQ_EXPR)
10890 warning_at (location,
10891 OPT_Waddress,
10892 "the comparison will always evaluate as %<false%> "
10893 "for the address of %qD will never be NULL",
10894 TREE_OPERAND (op0, 0));
10895 else
10896 warning_at (location,
10897 OPT_Waddress,
10898 "the comparison will always evaluate as %<true%> "
10899 "for the address of %qD will never be NULL",
10900 TREE_OPERAND (op0, 0));
10902 result_type = type0;
10904 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10906 if (TREE_CODE (op1) == ADDR_EXPR
10907 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10909 if (code == EQ_EXPR)
10910 warning_at (location,
10911 OPT_Waddress,
10912 "the comparison will always evaluate as %<false%> "
10913 "for the address of %qD will never be NULL",
10914 TREE_OPERAND (op1, 0));
10915 else
10916 warning_at (location,
10917 OPT_Waddress,
10918 "the comparison will always evaluate as %<true%> "
10919 "for the address of %qD will never be NULL",
10920 TREE_OPERAND (op1, 0));
10922 result_type = type1;
10924 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10926 tree tt0 = TREE_TYPE (type0);
10927 tree tt1 = TREE_TYPE (type1);
10928 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10929 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10930 addr_space_t as_common = ADDR_SPACE_GENERIC;
10932 if ((upc_shared_type_p (tt0)
10933 && !(upc_shared_type_p (tt1) || integer_zerop(op1)))
10934 || (upc_shared_type_p (tt1)
10935 && !(upc_shared_type_p (tt0) || integer_zerop(op0))))
10937 error_at (location, "UPC does not allow comparisons "
10938 "between pointers to shared and "
10939 "local pointers");
10940 return error_mark_node;
10942 if (upc_shared_type_p (tt0)
10943 && upc_shared_type_p (tt1) && (tt0 != tt1)
10944 && !(VOID_TYPE_P (tt0) || VOID_TYPE_P (tt1)))
10946 const tree bs_0 = upc_get_block_factor (tt0);
10947 const tree bs_1 = upc_get_block_factor (tt1);
10948 /* Both source and destination are non-void pointers to shared,
10949 whose target types are not equal.
10950 UPC dictates that their blocking factors must be equal. */
10951 if (!tree_int_cst_equal (bs_0, bs_1))
10953 error_at (location, "UPC does not allow comparison "
10954 "between pointers to shared with "
10955 "differing block sizes without a cast");
10956 return error_mark_node;
10959 /* Anything compares with void *. void * compares with anything.
10960 Otherwise, the targets must be compatible
10961 and both must be object or both incomplete. */
10962 if (comp_target_types (location, type0, type1))
10963 result_type = common_pointer_type (type0, type1);
10964 else if (!addr_space_superset (as0, as1, &as_common))
10966 error_at (location, "comparison of pointers to "
10967 "disjoint address spaces");
10968 return error_mark_node;
10970 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10972 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10973 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10974 "comparison of %<void *%> with function pointer");
10976 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10978 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10979 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10980 "comparison of %<void *%> with function pointer");
10982 else
10983 /* Avoid warning about the volatile ObjC EH puts on decls. */
10984 if (!objc_ok)
10985 pedwarn (location, 0,
10986 "comparison of distinct pointer types lacks a cast");
10988 if (result_type == NULL_TREE)
10990 if (upc_shared_type_p(tt0) || upc_shared_type_p(tt1))
10992 result_type = upc_pts_type_node;
10994 else
10996 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10997 result_type = build_pointer_type
10998 (build_qualified_type (void_type_node,
10999 qual));
11003 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11005 result_type = type0;
11006 pedwarn (location, 0, "comparison between pointer and integer");
11008 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11010 result_type = type1;
11011 pedwarn (location, 0, "comparison between pointer and integer");
11013 break;
11015 case LE_EXPR:
11016 case GE_EXPR:
11017 case LT_EXPR:
11018 case GT_EXPR:
11019 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11021 tree intt;
11022 if (!vector_types_compatible_elements_p (type0, type1))
11024 error_at (location, "comparing vectors with different "
11025 "element types");
11026 return error_mark_node;
11029 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11031 error_at (location, "comparing vectors with different "
11032 "number of elements");
11033 return error_mark_node;
11036 /* Always construct signed integer vector type. */
11037 intt = c_common_type_for_size (GET_MODE_BITSIZE
11038 (TYPE_MODE (TREE_TYPE (type0))), 0);
11039 result_type = build_opaque_vector_type (intt,
11040 TYPE_VECTOR_SUBPARTS (type0));
11041 converted = 1;
11042 break;
11044 build_type = integer_type_node;
11045 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11046 || code0 == FIXED_POINT_TYPE)
11047 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11048 || code1 == FIXED_POINT_TYPE))
11049 short_compare = 1;
11050 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11052 const tree tt0 = TREE_TYPE (type0);
11053 const tree tt1 = TREE_TYPE (type1);
11054 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11055 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11056 addr_space_t as_common;
11058 if (upc_shared_type_p (tt0) != upc_shared_type_p (tt1))
11060 error_at (location, "UPC does not allow comparisons between "
11061 "pointers to shared and local pointers");
11062 return error_mark_node;
11064 if (upc_shared_type_p (tt0)
11065 && upc_shared_type_p (tt1) && (tt0 != tt1)
11066 && !(VOID_TYPE_P (tt0) || VOID_TYPE_P (tt1)))
11068 const tree bs_0 = upc_get_block_factor (tt0);
11069 const tree bs_1 = upc_get_block_factor (tt1);
11070 /* Both source and destination are non-void pointers to shared,
11071 whose target types are not equal.
11072 UPC dictates that their blocking factors must be equal. */
11073 if (!tree_int_cst_equal (bs_0, bs_1))
11075 error_at (location, "UPC does not allow comparison "
11076 "between pointers to shared with "
11077 "differing block sizes without a cast");
11078 return error_mark_node;
11081 if (comp_target_types (location, type0, type1))
11083 result_type = common_pointer_type (type0, type1);
11084 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11085 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11086 pedwarn (location, 0,
11087 "comparison of complete and incomplete pointers");
11088 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11089 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11090 "ordered comparisons of pointers to functions");
11091 else if (null_pointer_constant_p (orig_op0)
11092 || null_pointer_constant_p (orig_op1))
11093 warning_at (location, OPT_Wextra,
11094 "ordered comparison of pointer with null pointer");
11097 else if (!addr_space_superset (as0, as1, &as_common))
11099 error_at (location, "comparison of pointers to "
11100 "disjoint address spaces");
11101 return error_mark_node;
11103 else if (upc_shared_type_p (TREE_TYPE (type0))
11104 || upc_shared_type_p (TREE_TYPE (type1)))
11106 result_type = upc_pts_type_node;
11108 else
11110 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11111 result_type = build_pointer_type
11112 (build_qualified_type (void_type_node, qual));
11113 pedwarn (location, 0,
11114 "comparison of distinct pointer types lacks a cast");
11117 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11119 result_type = type0;
11120 if (pedantic)
11121 pedwarn (location, OPT_Wpedantic,
11122 "ordered comparison of pointer with integer zero");
11123 else if (extra_warnings)
11124 warning_at (location, OPT_Wextra,
11125 "ordered comparison of pointer with integer zero");
11127 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11129 result_type = type1;
11130 if (pedantic)
11131 pedwarn (location, OPT_Wpedantic,
11132 "ordered comparison of pointer with integer zero");
11133 else if (extra_warnings)
11134 warning_at (location, OPT_Wextra,
11135 "ordered comparison of pointer with integer zero");
11137 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11139 result_type = type0;
11140 pedwarn (location, 0, "comparison between pointer and integer");
11142 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11144 result_type = type1;
11145 pedwarn (location, 0, "comparison between pointer and integer");
11147 break;
11149 default:
11150 gcc_unreachable ();
11153 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11154 return error_mark_node;
11156 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11157 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11158 || !vector_types_compatible_elements_p (type0, type1)))
11160 binary_op_error (location, code, type0, type1);
11161 return error_mark_node;
11164 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11165 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11167 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11168 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11170 bool first_complex = (code0 == COMPLEX_TYPE);
11171 bool second_complex = (code1 == COMPLEX_TYPE);
11172 int none_complex = (!first_complex && !second_complex);
11174 if (shorten || common || short_compare)
11176 result_type = c_common_type (type0, type1);
11177 do_warn_double_promotion (result_type, type0, type1,
11178 "implicit conversion from %qT to %qT "
11179 "to match other operand of binary "
11180 "expression",
11181 location);
11182 if (result_type == error_mark_node)
11183 return error_mark_node;
11186 if (first_complex != second_complex
11187 && (code == PLUS_EXPR
11188 || code == MINUS_EXPR
11189 || code == MULT_EXPR
11190 || (code == TRUNC_DIV_EXPR && first_complex))
11191 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11192 && flag_signed_zeros)
11194 /* An operation on mixed real/complex operands must be
11195 handled specially, but the language-independent code can
11196 more easily optimize the plain complex arithmetic if
11197 -fno-signed-zeros. */
11198 tree real_type = TREE_TYPE (result_type);
11199 tree real, imag;
11200 if (type0 != orig_type0 || type1 != orig_type1)
11202 gcc_assert (may_need_excess_precision && common);
11203 semantic_result_type = c_common_type (orig_type0, orig_type1);
11205 if (first_complex)
11207 if (TREE_TYPE (op0) != result_type)
11208 op0 = convert_and_check (location, result_type, op0);
11209 if (TREE_TYPE (op1) != real_type)
11210 op1 = convert_and_check (location, real_type, op1);
11212 else
11214 if (TREE_TYPE (op0) != real_type)
11215 op0 = convert_and_check (location, real_type, op0);
11216 if (TREE_TYPE (op1) != result_type)
11217 op1 = convert_and_check (location, result_type, op1);
11219 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11220 return error_mark_node;
11221 if (first_complex)
11223 op0 = c_save_expr (op0);
11224 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11225 op0, 1);
11226 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11227 op0, 1);
11228 switch (code)
11230 case MULT_EXPR:
11231 case TRUNC_DIV_EXPR:
11232 op1 = c_save_expr (op1);
11233 imag = build2 (resultcode, real_type, imag, op1);
11234 /* Fall through. */
11235 case PLUS_EXPR:
11236 case MINUS_EXPR:
11237 real = build2 (resultcode, real_type, real, op1);
11238 break;
11239 default:
11240 gcc_unreachable();
11243 else
11245 op1 = c_save_expr (op1);
11246 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11247 op1, 1);
11248 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11249 op1, 1);
11250 switch (code)
11252 case MULT_EXPR:
11253 op0 = c_save_expr (op0);
11254 imag = build2 (resultcode, real_type, op0, imag);
11255 /* Fall through. */
11256 case PLUS_EXPR:
11257 real = build2 (resultcode, real_type, op0, real);
11258 break;
11259 case MINUS_EXPR:
11260 real = build2 (resultcode, real_type, op0, real);
11261 imag = build1 (NEGATE_EXPR, real_type, imag);
11262 break;
11263 default:
11264 gcc_unreachable();
11267 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11268 goto return_build_binary_op;
11271 /* For certain operations (which identify themselves by shorten != 0)
11272 if both args were extended from the same smaller type,
11273 do the arithmetic in that type and then extend.
11275 shorten !=0 and !=1 indicates a bitwise operation.
11276 For them, this optimization is safe only if
11277 both args are zero-extended or both are sign-extended.
11278 Otherwise, we might change the result.
11279 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11280 but calculated in (unsigned short) it would be (unsigned short)-1. */
11282 if (shorten && none_complex)
11284 final_type = result_type;
11285 result_type = shorten_binary_op (result_type, op0, op1,
11286 shorten == -1);
11289 /* Shifts can be shortened if shifting right. */
11291 if (short_shift)
11293 int unsigned_arg;
11294 tree arg0 = get_narrower (op0, &unsigned_arg);
11296 final_type = result_type;
11298 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11299 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11301 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11302 && tree_int_cst_sgn (op1) > 0
11303 /* We can shorten only if the shift count is less than the
11304 number of bits in the smaller type size. */
11305 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11306 /* We cannot drop an unsigned shift after sign-extension. */
11307 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11309 /* Do an unsigned shift if the operand was zero-extended. */
11310 result_type
11311 = c_common_signed_or_unsigned_type (unsigned_arg,
11312 TREE_TYPE (arg0));
11313 /* Convert value-to-be-shifted to that type. */
11314 if (TREE_TYPE (op0) != result_type)
11315 op0 = convert (result_type, op0);
11316 converted = 1;
11320 /* Comparison operations are shortened too but differently.
11321 They identify themselves by setting short_compare = 1. */
11323 if (short_compare)
11325 /* Don't write &op0, etc., because that would prevent op0
11326 from being kept in a register.
11327 Instead, make copies of the our local variables and
11328 pass the copies by reference, then copy them back afterward. */
11329 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11330 enum tree_code xresultcode = resultcode;
11331 tree val
11332 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11333 &xresultcode);
11335 if (val != 0)
11337 ret = val;
11338 goto return_build_binary_op;
11341 op0 = xop0, op1 = xop1;
11342 converted = 1;
11343 resultcode = xresultcode;
11345 if (c_inhibit_evaluation_warnings == 0)
11347 bool op0_maybe_const = true;
11348 bool op1_maybe_const = true;
11349 tree orig_op0_folded, orig_op1_folded;
11351 if (in_late_binary_op)
11353 orig_op0_folded = orig_op0;
11354 orig_op1_folded = orig_op1;
11356 else
11358 /* Fold for the sake of possible warnings, as in
11359 build_conditional_expr. This requires the
11360 "original" values to be folded, not just op0 and
11361 op1. */
11362 c_inhibit_evaluation_warnings++;
11363 op0 = c_fully_fold (op0, require_constant_value,
11364 &op0_maybe_const);
11365 op1 = c_fully_fold (op1, require_constant_value,
11366 &op1_maybe_const);
11367 c_inhibit_evaluation_warnings--;
11368 orig_op0_folded = c_fully_fold (orig_op0,
11369 require_constant_value,
11370 NULL);
11371 orig_op1_folded = c_fully_fold (orig_op1,
11372 require_constant_value,
11373 NULL);
11376 if (warn_sign_compare)
11377 warn_for_sign_compare (location, orig_op0_folded,
11378 orig_op1_folded, op0, op1,
11379 result_type, resultcode);
11380 if (!in_late_binary_op && !int_operands)
11382 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11383 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11384 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11385 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11391 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11392 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11393 Then the expression will be built.
11394 It will be given type FINAL_TYPE if that is nonzero;
11395 otherwise, it will be given type RESULT_TYPE. */
11397 if (!result_type)
11399 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11400 return error_mark_node;
11403 if (build_type == NULL_TREE)
11405 build_type = result_type;
11406 if ((type0 != orig_type0 || type1 != orig_type1)
11407 && !boolean_op)
11409 gcc_assert (may_need_excess_precision && common);
11410 semantic_result_type = c_common_type (orig_type0, orig_type1);
11414 if (!converted)
11416 op0 = ep_convert_and_check (location, result_type, op0,
11417 semantic_result_type);
11418 op1 = ep_convert_and_check (location, result_type, op1,
11419 semantic_result_type);
11421 /* This can happen if one operand has a vector type, and the other
11422 has a different type. */
11423 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11424 return error_mark_node;
11427 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11428 | SANITIZE_FLOAT_DIVIDE))
11429 && current_function_decl != 0
11430 && !lookup_attribute ("no_sanitize_undefined",
11431 DECL_ATTRIBUTES (current_function_decl))
11432 && (doing_div_or_mod || doing_shift))
11434 /* OP0 and/or OP1 might have side-effects. */
11435 op0 = c_save_expr (op0);
11436 op1 = c_save_expr (op1);
11437 op0 = c_fully_fold (op0, false, NULL);
11438 op1 = c_fully_fold (op1, false, NULL);
11439 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11440 | SANITIZE_FLOAT_DIVIDE)))
11441 instrument_expr = ubsan_instrument_division (location, op0, op1);
11442 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11443 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11446 /* Treat expressions in initializers specially as they can't trap. */
11447 if (int_const_or_overflow)
11448 ret = (require_constant_value
11449 ? fold_build2_initializer_loc (location, resultcode, build_type,
11450 op0, op1)
11451 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11452 else
11453 ret = build2 (resultcode, build_type, op0, op1);
11454 if (final_type != 0)
11455 ret = convert (final_type, ret);
11457 return_build_binary_op:
11458 gcc_assert (ret != error_mark_node);
11459 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11460 ret = (int_operands
11461 ? note_integer_operands (ret)
11462 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11463 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11464 && !in_late_binary_op)
11465 ret = note_integer_operands (ret);
11466 if (semantic_result_type)
11467 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11468 protected_set_expr_location (ret, location);
11470 if (instrument_expr != NULL)
11471 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11472 instrument_expr, ret);
11474 return ret;
11477 /* Convert EXPR to be a truth-value, validating its type for this
11478 purpose. LOCATION is the source location for the expression. */
11480 tree
11481 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11483 bool int_const, int_operands;
11485 switch (TREE_CODE (TREE_TYPE (expr)))
11487 case ARRAY_TYPE:
11488 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11489 return error_mark_node;
11491 case RECORD_TYPE:
11492 error_at (location, "used struct type value where scalar is required");
11493 return error_mark_node;
11495 case UNION_TYPE:
11496 error_at (location, "used union type value where scalar is required");
11497 return error_mark_node;
11499 case VOID_TYPE:
11500 error_at (location, "void value not ignored as it ought to be");
11501 return error_mark_node;
11503 case FUNCTION_TYPE:
11504 gcc_unreachable ();
11506 case VECTOR_TYPE:
11507 error_at (location, "used vector type where scalar is required");
11508 return error_mark_node;
11510 default:
11511 break;
11514 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11515 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11516 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11518 expr = remove_c_maybe_const_expr (expr);
11519 expr = build2 (NE_EXPR, integer_type_node, expr,
11520 convert (TREE_TYPE (expr), integer_zero_node));
11521 expr = note_integer_operands (expr);
11523 else
11524 /* ??? Should we also give an error for vectors rather than leaving
11525 those to give errors later? */
11526 expr = c_common_truthvalue_conversion (location, expr);
11528 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11530 if (TREE_OVERFLOW (expr))
11531 return expr;
11532 else
11533 return note_integer_operands (expr);
11535 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11536 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11537 return expr;
11541 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11542 required. */
11544 tree
11545 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11547 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11549 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11550 /* Executing a compound literal inside a function reinitializes
11551 it. */
11552 if (!TREE_STATIC (decl))
11553 *se = true;
11554 return decl;
11556 else
11557 return expr;
11560 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11562 tree
11563 c_begin_omp_parallel (void)
11565 tree block;
11567 keep_next_level ();
11568 block = c_begin_compound_stmt (true);
11570 return block;
11573 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11574 statement. LOC is the location of the OMP_PARALLEL. */
11576 tree
11577 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11579 tree stmt;
11581 block = c_end_compound_stmt (loc, block, true);
11583 stmt = make_node (OMP_PARALLEL);
11584 TREE_TYPE (stmt) = void_type_node;
11585 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11586 OMP_PARALLEL_BODY (stmt) = block;
11587 SET_EXPR_LOCATION (stmt, loc);
11589 return add_stmt (stmt);
11592 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11594 tree
11595 c_begin_omp_task (void)
11597 tree block;
11599 keep_next_level ();
11600 block = c_begin_compound_stmt (true);
11602 return block;
11605 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11606 statement. LOC is the location of the #pragma. */
11608 tree
11609 c_finish_omp_task (location_t loc, tree clauses, tree block)
11611 tree stmt;
11613 block = c_end_compound_stmt (loc, block, true);
11615 stmt = make_node (OMP_TASK);
11616 TREE_TYPE (stmt) = void_type_node;
11617 OMP_TASK_CLAUSES (stmt) = clauses;
11618 OMP_TASK_BODY (stmt) = block;
11619 SET_EXPR_LOCATION (stmt, loc);
11621 return add_stmt (stmt);
11624 /* Generate GOMP_cancel call for #pragma omp cancel. */
11626 void
11627 c_finish_omp_cancel (location_t loc, tree clauses)
11629 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11630 int mask = 0;
11631 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11632 mask = 1;
11633 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11634 mask = 2;
11635 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11636 mask = 4;
11637 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11638 mask = 8;
11639 else
11641 error_at (loc, "%<#pragma omp cancel must specify one of "
11642 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11643 "clauses");
11644 return;
11646 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11647 if (ifc != NULL_TREE)
11649 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11650 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11651 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11652 build_zero_cst (type));
11654 else
11655 ifc = boolean_true_node;
11656 tree stmt = build_call_expr_loc (loc, fn, 2,
11657 build_int_cst (integer_type_node, mask),
11658 ifc);
11659 add_stmt (stmt);
11662 /* Generate GOMP_cancellation_point call for
11663 #pragma omp cancellation point. */
11665 void
11666 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11668 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11669 int mask = 0;
11670 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11671 mask = 1;
11672 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11673 mask = 2;
11674 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11675 mask = 4;
11676 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11677 mask = 8;
11678 else
11680 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11681 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11682 "clauses");
11683 return;
11685 tree stmt = build_call_expr_loc (loc, fn, 1,
11686 build_int_cst (integer_type_node, mask));
11687 add_stmt (stmt);
11690 /* Helper function for handle_omp_array_sections. Called recursively
11691 to handle multiple array-section-subscripts. C is the clause,
11692 T current expression (initially OMP_CLAUSE_DECL), which is either
11693 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11694 expression if specified, TREE_VALUE length expression if specified,
11695 TREE_CHAIN is what it has been specified after, or some decl.
11696 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11697 set to true if any of the array-section-subscript could have length
11698 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11699 first array-section-subscript which is known not to have length
11700 of one. Given say:
11701 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11702 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11703 all are or may have length of 1, array-section-subscript [:2] is the
11704 first one knonwn not to have length 1. For array-section-subscript
11705 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11706 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11707 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11708 case though, as some lengths could be zero. */
11710 static tree
11711 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11712 bool &maybe_zero_len, unsigned int &first_non_one)
11714 tree ret, low_bound, length, type;
11715 if (TREE_CODE (t) != TREE_LIST)
11717 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
11718 return error_mark_node;
11719 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11721 if (DECL_P (t))
11722 error_at (OMP_CLAUSE_LOCATION (c),
11723 "%qD is not a variable in %qs clause", t,
11724 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11725 else
11726 error_at (OMP_CLAUSE_LOCATION (c),
11727 "%qE is not a variable in %qs clause", t,
11728 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11729 return error_mark_node;
11731 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11732 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11734 error_at (OMP_CLAUSE_LOCATION (c),
11735 "%qD is threadprivate variable in %qs clause", t,
11736 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11737 return error_mark_node;
11739 return t;
11742 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11743 maybe_zero_len, first_non_one);
11744 if (ret == error_mark_node || ret == NULL_TREE)
11745 return ret;
11747 type = TREE_TYPE (ret);
11748 low_bound = TREE_PURPOSE (t);
11749 length = TREE_VALUE (t);
11751 if (low_bound == error_mark_node || length == error_mark_node)
11752 return error_mark_node;
11754 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11756 error_at (OMP_CLAUSE_LOCATION (c),
11757 "low bound %qE of array section does not have integral type",
11758 low_bound);
11759 return error_mark_node;
11761 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11763 error_at (OMP_CLAUSE_LOCATION (c),
11764 "length %qE of array section does not have integral type",
11765 length);
11766 return error_mark_node;
11768 if (low_bound
11769 && TREE_CODE (low_bound) == INTEGER_CST
11770 && TYPE_PRECISION (TREE_TYPE (low_bound))
11771 > TYPE_PRECISION (sizetype))
11772 low_bound = fold_convert (sizetype, low_bound);
11773 if (length
11774 && TREE_CODE (length) == INTEGER_CST
11775 && TYPE_PRECISION (TREE_TYPE (length))
11776 > TYPE_PRECISION (sizetype))
11777 length = fold_convert (sizetype, length);
11778 if (low_bound == NULL_TREE)
11779 low_bound = integer_zero_node;
11781 if (length != NULL_TREE)
11783 if (!integer_nonzerop (length))
11784 maybe_zero_len = true;
11785 if (first_non_one == types.length ()
11786 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11787 first_non_one++;
11789 if (TREE_CODE (type) == ARRAY_TYPE)
11791 if (length == NULL_TREE
11792 && (TYPE_DOMAIN (type) == NULL_TREE
11793 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11795 error_at (OMP_CLAUSE_LOCATION (c),
11796 "for unknown bound array type length expression must "
11797 "be specified");
11798 return error_mark_node;
11800 if (TREE_CODE (low_bound) == INTEGER_CST
11801 && tree_int_cst_sgn (low_bound) == -1)
11803 error_at (OMP_CLAUSE_LOCATION (c),
11804 "negative low bound in array section in %qs clause",
11805 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11806 return error_mark_node;
11808 if (length != NULL_TREE
11809 && TREE_CODE (length) == INTEGER_CST
11810 && tree_int_cst_sgn (length) == -1)
11812 error_at (OMP_CLAUSE_LOCATION (c),
11813 "negative length in array section in %qs clause",
11814 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11815 return error_mark_node;
11817 if (TYPE_DOMAIN (type)
11818 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11819 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11820 == INTEGER_CST)
11822 tree size = size_binop (PLUS_EXPR,
11823 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11824 size_one_node);
11825 if (TREE_CODE (low_bound) == INTEGER_CST)
11827 if (tree_int_cst_lt (size, low_bound))
11829 error_at (OMP_CLAUSE_LOCATION (c),
11830 "low bound %qE above array section size "
11831 "in %qs clause", low_bound,
11832 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11833 return error_mark_node;
11835 if (tree_int_cst_equal (size, low_bound))
11836 maybe_zero_len = true;
11837 else if (length == NULL_TREE
11838 && first_non_one == types.length ()
11839 && tree_int_cst_equal
11840 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11841 low_bound))
11842 first_non_one++;
11844 else if (length == NULL_TREE)
11846 maybe_zero_len = true;
11847 if (first_non_one == types.length ())
11848 first_non_one++;
11850 if (length && TREE_CODE (length) == INTEGER_CST)
11852 if (tree_int_cst_lt (size, length))
11854 error_at (OMP_CLAUSE_LOCATION (c),
11855 "length %qE above array section size "
11856 "in %qs clause", length,
11857 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11858 return error_mark_node;
11860 if (TREE_CODE (low_bound) == INTEGER_CST)
11862 tree lbpluslen
11863 = size_binop (PLUS_EXPR,
11864 fold_convert (sizetype, low_bound),
11865 fold_convert (sizetype, length));
11866 if (TREE_CODE (lbpluslen) == INTEGER_CST
11867 && tree_int_cst_lt (size, lbpluslen))
11869 error_at (OMP_CLAUSE_LOCATION (c),
11870 "high bound %qE above array section size "
11871 "in %qs clause", lbpluslen,
11872 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11873 return error_mark_node;
11878 else if (length == NULL_TREE)
11880 maybe_zero_len = true;
11881 if (first_non_one == types.length ())
11882 first_non_one++;
11885 /* For [lb:] we will need to evaluate lb more than once. */
11886 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11888 tree lb = c_save_expr (low_bound);
11889 if (lb != low_bound)
11891 TREE_PURPOSE (t) = lb;
11892 low_bound = lb;
11896 else if (TREE_CODE (type) == POINTER_TYPE)
11898 if (length == NULL_TREE)
11900 error_at (OMP_CLAUSE_LOCATION (c),
11901 "for pointer type length expression must be specified");
11902 return error_mark_node;
11904 /* If there is a pointer type anywhere but in the very first
11905 array-section-subscript, the array section can't be contiguous. */
11906 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11907 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11909 error_at (OMP_CLAUSE_LOCATION (c),
11910 "array section is not contiguous in %qs clause",
11911 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11912 return error_mark_node;
11915 else
11917 error_at (OMP_CLAUSE_LOCATION (c),
11918 "%qE does not have pointer or array type", ret);
11919 return error_mark_node;
11921 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11922 types.safe_push (TREE_TYPE (ret));
11923 /* We will need to evaluate lb more than once. */
11924 tree lb = c_save_expr (low_bound);
11925 if (lb != low_bound)
11927 TREE_PURPOSE (t) = lb;
11928 low_bound = lb;
11930 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11931 return ret;
11934 /* Handle array sections for clause C. */
11936 static bool
11937 handle_omp_array_sections (tree c)
11939 bool maybe_zero_len = false;
11940 unsigned int first_non_one = 0;
11941 vec<tree> types = vNULL;
11942 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11943 maybe_zero_len, first_non_one);
11944 if (first == error_mark_node)
11946 types.release ();
11947 return true;
11949 if (first == NULL_TREE)
11951 types.release ();
11952 return false;
11954 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11956 tree t = OMP_CLAUSE_DECL (c);
11957 tree tem = NULL_TREE;
11958 types.release ();
11959 /* Need to evaluate side effects in the length expressions
11960 if any. */
11961 while (TREE_CODE (t) == TREE_LIST)
11963 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11965 if (tem == NULL_TREE)
11966 tem = TREE_VALUE (t);
11967 else
11968 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11969 TREE_VALUE (t), tem);
11971 t = TREE_CHAIN (t);
11973 if (tem)
11974 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11975 first = c_fully_fold (first, false, NULL);
11976 OMP_CLAUSE_DECL (c) = first;
11978 else
11980 unsigned int num = types.length (), i;
11981 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11982 tree condition = NULL_TREE;
11984 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11985 maybe_zero_len = true;
11987 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11988 t = TREE_CHAIN (t))
11990 tree low_bound = TREE_PURPOSE (t);
11991 tree length = TREE_VALUE (t);
11993 i--;
11994 if (low_bound
11995 && TREE_CODE (low_bound) == INTEGER_CST
11996 && TYPE_PRECISION (TREE_TYPE (low_bound))
11997 > TYPE_PRECISION (sizetype))
11998 low_bound = fold_convert (sizetype, low_bound);
11999 if (length
12000 && TREE_CODE (length) == INTEGER_CST
12001 && TYPE_PRECISION (TREE_TYPE (length))
12002 > TYPE_PRECISION (sizetype))
12003 length = fold_convert (sizetype, length);
12004 if (low_bound == NULL_TREE)
12005 low_bound = integer_zero_node;
12006 if (!maybe_zero_len && i > first_non_one)
12008 if (integer_nonzerop (low_bound))
12009 goto do_warn_noncontiguous;
12010 if (length != NULL_TREE
12011 && TREE_CODE (length) == INTEGER_CST
12012 && TYPE_DOMAIN (types[i])
12013 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12014 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12015 == INTEGER_CST)
12017 tree size;
12018 size = size_binop (PLUS_EXPR,
12019 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12020 size_one_node);
12021 if (!tree_int_cst_equal (length, size))
12023 do_warn_noncontiguous:
12024 error_at (OMP_CLAUSE_LOCATION (c),
12025 "array section is not contiguous in %qs "
12026 "clause",
12027 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12028 types.release ();
12029 return true;
12032 if (length != NULL_TREE
12033 && TREE_SIDE_EFFECTS (length))
12035 if (side_effects == NULL_TREE)
12036 side_effects = length;
12037 else
12038 side_effects = build2 (COMPOUND_EXPR,
12039 TREE_TYPE (side_effects),
12040 length, side_effects);
12043 else
12045 tree l;
12047 if (i > first_non_one && length && integer_nonzerop (length))
12048 continue;
12049 if (length)
12050 l = fold_convert (sizetype, length);
12051 else
12053 l = size_binop (PLUS_EXPR,
12054 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12055 size_one_node);
12056 l = size_binop (MINUS_EXPR, l,
12057 fold_convert (sizetype, low_bound));
12059 if (i > first_non_one)
12061 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12062 size_zero_node);
12063 if (condition == NULL_TREE)
12064 condition = l;
12065 else
12066 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12067 l, condition);
12069 else if (size == NULL_TREE)
12071 size = size_in_bytes (TREE_TYPE (types[i]));
12072 size = size_binop (MULT_EXPR, size, l);
12073 if (condition)
12074 size = fold_build3 (COND_EXPR, sizetype, condition,
12075 size, size_zero_node);
12077 else
12078 size = size_binop (MULT_EXPR, size, l);
12081 types.release ();
12082 if (side_effects)
12083 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
12084 first = c_fully_fold (first, false, NULL);
12085 OMP_CLAUSE_DECL (c) = first;
12086 if (size)
12087 size = c_fully_fold (size, false, NULL);
12088 OMP_CLAUSE_SIZE (c) = size;
12089 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12090 return false;
12091 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
12092 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
12093 if (!c_mark_addressable (t))
12094 return false;
12095 OMP_CLAUSE_DECL (c2) = t;
12096 t = build_fold_addr_expr (first);
12097 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12098 tree ptr = OMP_CLAUSE_DECL (c2);
12099 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12100 ptr = build_fold_addr_expr (ptr);
12101 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12102 ptrdiff_type_node, t,
12103 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12104 ptrdiff_type_node, ptr));
12105 t = c_fully_fold (t, false, NULL);
12106 OMP_CLAUSE_SIZE (c2) = t;
12107 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12108 OMP_CLAUSE_CHAIN (c) = c2;
12110 return false;
12113 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12114 an inline call. But, remap
12115 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12116 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12118 static tree
12119 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12120 tree decl, tree placeholder)
12122 copy_body_data id;
12123 struct pointer_map_t *decl_map = pointer_map_create ();
12125 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
12126 *pointer_map_insert (decl_map, omp_decl2) = decl;
12127 memset (&id, 0, sizeof (id));
12128 id.src_fn = DECL_CONTEXT (omp_decl1);
12129 id.dst_fn = current_function_decl;
12130 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12131 id.decl_map = decl_map;
12133 id.copy_decl = copy_decl_no_change;
12134 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12135 id.transform_new_cfg = true;
12136 id.transform_return_to_modify = false;
12137 id.transform_lang_insert_block = NULL;
12138 id.eh_lp_nr = 0;
12139 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12140 pointer_map_destroy (decl_map);
12141 return stmt;
12144 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12145 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12147 static tree
12148 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12150 if (*tp == (tree) data)
12151 return *tp;
12152 return NULL_TREE;
12155 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
12156 Remove any elements from the list that are invalid. */
12158 tree
12159 c_finish_omp_clauses (tree clauses)
12161 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12162 bitmap_head aligned_head;
12163 tree c, t, *pc;
12164 bool branch_seen = false;
12165 bool copyprivate_seen = false;
12166 tree *nowait_clause = NULL;
12168 bitmap_obstack_initialize (NULL);
12169 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12170 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12171 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12172 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12174 for (pc = &clauses, c = clauses; c ; c = *pc)
12176 bool remove = false;
12177 bool need_complete = false;
12178 bool need_implicitly_determined = false;
12180 switch (OMP_CLAUSE_CODE (c))
12182 case OMP_CLAUSE_SHARED:
12183 need_implicitly_determined = true;
12184 goto check_dup_generic;
12186 case OMP_CLAUSE_PRIVATE:
12187 need_complete = true;
12188 need_implicitly_determined = true;
12189 goto check_dup_generic;
12191 case OMP_CLAUSE_REDUCTION:
12192 need_implicitly_determined = true;
12193 t = OMP_CLAUSE_DECL (c);
12194 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12195 && (FLOAT_TYPE_P (TREE_TYPE (t))
12196 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
12198 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12199 const char *r_name = NULL;
12201 switch (r_code)
12203 case PLUS_EXPR:
12204 case MULT_EXPR:
12205 case MINUS_EXPR:
12206 break;
12207 case MIN_EXPR:
12208 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12209 r_name = "min";
12210 break;
12211 case MAX_EXPR:
12212 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12213 r_name = "max";
12214 break;
12215 case BIT_AND_EXPR:
12216 r_name = "&";
12217 break;
12218 case BIT_XOR_EXPR:
12219 r_name = "^";
12220 break;
12221 case BIT_IOR_EXPR:
12222 r_name = "|";
12223 break;
12224 case TRUTH_ANDIF_EXPR:
12225 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12226 r_name = "&&";
12227 break;
12228 case TRUTH_ORIF_EXPR:
12229 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12230 r_name = "||";
12231 break;
12232 default:
12233 gcc_unreachable ();
12235 if (r_name)
12237 error_at (OMP_CLAUSE_LOCATION (c),
12238 "%qE has invalid type for %<reduction(%s)%>",
12239 t, r_name);
12240 remove = true;
12241 break;
12244 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12246 error_at (OMP_CLAUSE_LOCATION (c),
12247 "user defined reduction not found for %qD", t);
12248 remove = true;
12249 break;
12251 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12253 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12254 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12255 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12256 VAR_DECL, NULL_TREE, type);
12257 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12258 DECL_ARTIFICIAL (placeholder) = 1;
12259 DECL_IGNORED_P (placeholder) = 1;
12260 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12261 c_mark_addressable (placeholder);
12262 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12263 c_mark_addressable (OMP_CLAUSE_DECL (c));
12264 OMP_CLAUSE_REDUCTION_MERGE (c)
12265 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12266 TREE_VEC_ELT (list, 0),
12267 TREE_VEC_ELT (list, 1),
12268 OMP_CLAUSE_DECL (c), placeholder);
12269 OMP_CLAUSE_REDUCTION_MERGE (c)
12270 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12271 void_type_node, NULL_TREE,
12272 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12273 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12274 if (TREE_VEC_LENGTH (list) == 6)
12276 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12277 c_mark_addressable (OMP_CLAUSE_DECL (c));
12278 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12279 c_mark_addressable (placeholder);
12280 tree init = TREE_VEC_ELT (list, 5);
12281 if (init == error_mark_node)
12282 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12283 OMP_CLAUSE_REDUCTION_INIT (c)
12284 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12285 TREE_VEC_ELT (list, 3),
12286 OMP_CLAUSE_DECL (c), placeholder);
12287 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12288 OMP_CLAUSE_REDUCTION_INIT (c)
12289 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12290 OMP_CLAUSE_REDUCTION_INIT (c));
12291 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12292 c_find_omp_placeholder_r,
12293 placeholder, NULL))
12294 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12296 else
12298 tree init;
12299 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12300 init = build_constructor (TREE_TYPE (t), NULL);
12301 else
12302 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12303 OMP_CLAUSE_REDUCTION_INIT (c)
12304 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12306 OMP_CLAUSE_REDUCTION_INIT (c)
12307 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12308 void_type_node, NULL_TREE,
12309 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12310 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12312 goto check_dup_generic;
12314 case OMP_CLAUSE_COPYPRIVATE:
12315 copyprivate_seen = true;
12316 if (nowait_clause)
12318 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12319 "%<nowait%> clause must not be used together "
12320 "with %<copyprivate%>");
12321 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12322 nowait_clause = NULL;
12324 goto check_dup_generic;
12326 case OMP_CLAUSE_COPYIN:
12327 t = OMP_CLAUSE_DECL (c);
12328 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12330 error_at (OMP_CLAUSE_LOCATION (c),
12331 "%qE must be %<threadprivate%> for %<copyin%>", t);
12332 remove = true;
12333 break;
12335 goto check_dup_generic;
12337 case OMP_CLAUSE_LINEAR:
12338 t = OMP_CLAUSE_DECL (c);
12339 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12340 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12342 error_at (OMP_CLAUSE_LOCATION (c),
12343 "linear clause applied to non-integral non-pointer "
12344 "variable with type %qT", TREE_TYPE (t));
12345 remove = true;
12346 break;
12348 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12350 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12351 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12352 OMP_CLAUSE_DECL (c), s);
12353 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12354 sizetype, s, OMP_CLAUSE_DECL (c));
12355 if (s == error_mark_node)
12356 s = size_one_node;
12357 OMP_CLAUSE_LINEAR_STEP (c) = s;
12359 else
12360 OMP_CLAUSE_LINEAR_STEP (c)
12361 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12362 goto check_dup_generic;
12364 check_dup_generic:
12365 t = OMP_CLAUSE_DECL (c);
12366 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12368 error_at (OMP_CLAUSE_LOCATION (c),
12369 "%qE is not a variable in clause %qs", t,
12370 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12371 remove = true;
12373 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12374 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12375 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12377 error_at (OMP_CLAUSE_LOCATION (c),
12378 "%qE appears more than once in data clauses", t);
12379 remove = true;
12381 else
12382 bitmap_set_bit (&generic_head, DECL_UID (t));
12383 break;
12385 case OMP_CLAUSE_FIRSTPRIVATE:
12386 t = OMP_CLAUSE_DECL (c);
12387 need_complete = true;
12388 need_implicitly_determined = true;
12389 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12391 error_at (OMP_CLAUSE_LOCATION (c),
12392 "%qE is not a variable in clause %<firstprivate%>", t);
12393 remove = true;
12395 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12396 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12398 error_at (OMP_CLAUSE_LOCATION (c),
12399 "%qE appears more than once in data clauses", t);
12400 remove = true;
12402 else
12403 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12404 break;
12406 case OMP_CLAUSE_LASTPRIVATE:
12407 t = OMP_CLAUSE_DECL (c);
12408 need_complete = true;
12409 need_implicitly_determined = true;
12410 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12412 error_at (OMP_CLAUSE_LOCATION (c),
12413 "%qE is not a variable in clause %<lastprivate%>", t);
12414 remove = true;
12416 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12417 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12419 error_at (OMP_CLAUSE_LOCATION (c),
12420 "%qE appears more than once in data clauses", t);
12421 remove = true;
12423 else
12424 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12425 break;
12427 case OMP_CLAUSE_ALIGNED:
12428 t = OMP_CLAUSE_DECL (c);
12429 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12431 error_at (OMP_CLAUSE_LOCATION (c),
12432 "%qE is not a variable in %<aligned%> clause", t);
12433 remove = true;
12435 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12436 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12438 error_at (OMP_CLAUSE_LOCATION (c),
12439 "%qE in %<aligned%> clause is neither a pointer nor "
12440 "an array", t);
12441 remove = true;
12443 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12445 error_at (OMP_CLAUSE_LOCATION (c),
12446 "%qE appears more than once in %<aligned%> clauses",
12448 remove = true;
12450 else
12451 bitmap_set_bit (&aligned_head, DECL_UID (t));
12452 break;
12454 case OMP_CLAUSE_DEPEND:
12455 t = OMP_CLAUSE_DECL (c);
12456 if (TREE_CODE (t) == TREE_LIST)
12458 if (handle_omp_array_sections (c))
12459 remove = true;
12460 break;
12462 if (t == error_mark_node)
12463 remove = true;
12464 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12466 error_at (OMP_CLAUSE_LOCATION (c),
12467 "%qE is not a variable in %<depend%> clause", t);
12468 remove = true;
12470 else if (!c_mark_addressable (t))
12471 remove = true;
12472 break;
12474 case OMP_CLAUSE_MAP:
12475 case OMP_CLAUSE_TO:
12476 case OMP_CLAUSE_FROM:
12477 t = OMP_CLAUSE_DECL (c);
12478 if (TREE_CODE (t) == TREE_LIST)
12480 if (handle_omp_array_sections (c))
12481 remove = true;
12482 else
12484 t = OMP_CLAUSE_DECL (c);
12485 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12487 error_at (OMP_CLAUSE_LOCATION (c),
12488 "array section does not have mappable type "
12489 "in %qs clause",
12490 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12491 remove = true;
12494 break;
12496 if (t == error_mark_node)
12497 remove = true;
12498 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12500 error_at (OMP_CLAUSE_LOCATION (c),
12501 "%qE is not a variable in %qs clause", t,
12502 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12503 remove = true;
12505 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12507 error_at (OMP_CLAUSE_LOCATION (c),
12508 "%qD is threadprivate variable in %qs clause", t,
12509 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12510 remove = true;
12512 else if (!c_mark_addressable (t))
12513 remove = true;
12514 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12515 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12516 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12518 error_at (OMP_CLAUSE_LOCATION (c),
12519 "%qD does not have a mappable type in %qs clause", t,
12520 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12521 remove = true;
12523 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12525 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12526 error ("%qD appears more than once in motion clauses", t);
12527 else
12528 error ("%qD appears more than once in map clauses", t);
12529 remove = true;
12531 else
12532 bitmap_set_bit (&generic_head, DECL_UID (t));
12533 break;
12535 case OMP_CLAUSE_UNIFORM:
12536 t = OMP_CLAUSE_DECL (c);
12537 if (TREE_CODE (t) != PARM_DECL)
12539 if (DECL_P (t))
12540 error_at (OMP_CLAUSE_LOCATION (c),
12541 "%qD is not an argument in %<uniform%> clause", t);
12542 else
12543 error_at (OMP_CLAUSE_LOCATION (c),
12544 "%qE is not an argument in %<uniform%> clause", t);
12545 remove = true;
12546 break;
12548 goto check_dup_generic;
12550 case OMP_CLAUSE_NOWAIT:
12551 if (copyprivate_seen)
12553 error_at (OMP_CLAUSE_LOCATION (c),
12554 "%<nowait%> clause must not be used together "
12555 "with %<copyprivate%>");
12556 remove = true;
12557 break;
12559 nowait_clause = pc;
12560 pc = &OMP_CLAUSE_CHAIN (c);
12561 continue;
12563 case OMP_CLAUSE_IF:
12564 case OMP_CLAUSE_NUM_THREADS:
12565 case OMP_CLAUSE_NUM_TEAMS:
12566 case OMP_CLAUSE_THREAD_LIMIT:
12567 case OMP_CLAUSE_SCHEDULE:
12568 case OMP_CLAUSE_ORDERED:
12569 case OMP_CLAUSE_DEFAULT:
12570 case OMP_CLAUSE_UNTIED:
12571 case OMP_CLAUSE_COLLAPSE:
12572 case OMP_CLAUSE_FINAL:
12573 case OMP_CLAUSE_MERGEABLE:
12574 case OMP_CLAUSE_SAFELEN:
12575 case OMP_CLAUSE_SIMDLEN:
12576 case OMP_CLAUSE_DEVICE:
12577 case OMP_CLAUSE_DIST_SCHEDULE:
12578 case OMP_CLAUSE_PARALLEL:
12579 case OMP_CLAUSE_FOR:
12580 case OMP_CLAUSE_SECTIONS:
12581 case OMP_CLAUSE_TASKGROUP:
12582 case OMP_CLAUSE_PROC_BIND:
12583 pc = &OMP_CLAUSE_CHAIN (c);
12584 continue;
12586 case OMP_CLAUSE_INBRANCH:
12587 case OMP_CLAUSE_NOTINBRANCH:
12588 if (branch_seen)
12590 error_at (OMP_CLAUSE_LOCATION (c),
12591 "%<inbranch%> clause is incompatible with "
12592 "%<notinbranch%>");
12593 remove = true;
12594 break;
12596 branch_seen = true;
12597 pc = &OMP_CLAUSE_CHAIN (c);
12598 continue;
12600 default:
12601 gcc_unreachable ();
12604 if (!remove)
12606 t = OMP_CLAUSE_DECL (c);
12608 if (need_complete)
12610 t = require_complete_type (t);
12611 if (t == error_mark_node)
12612 remove = true;
12615 if (need_implicitly_determined)
12617 const char *share_name = NULL;
12619 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12620 share_name = "threadprivate";
12621 else switch (c_omp_predetermined_sharing (t))
12623 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12624 break;
12625 case OMP_CLAUSE_DEFAULT_SHARED:
12626 /* const vars may be specified in firstprivate clause. */
12627 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12628 && TREE_READONLY (t))
12629 break;
12630 share_name = "shared";
12631 break;
12632 case OMP_CLAUSE_DEFAULT_PRIVATE:
12633 share_name = "private";
12634 break;
12635 default:
12636 gcc_unreachable ();
12638 if (share_name)
12640 error_at (OMP_CLAUSE_LOCATION (c),
12641 "%qE is predetermined %qs for %qs",
12642 t, share_name,
12643 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12644 remove = true;
12649 if (remove)
12650 *pc = OMP_CLAUSE_CHAIN (c);
12651 else
12652 pc = &OMP_CLAUSE_CHAIN (c);
12655 bitmap_obstack_release (NULL);
12656 return clauses;
12659 /* Create a transaction node. */
12661 tree
12662 c_finish_transaction (location_t loc, tree block, int flags)
12664 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12665 if (flags & TM_STMT_ATTR_OUTER)
12666 TRANSACTION_EXPR_OUTER (stmt) = 1;
12667 if (flags & TM_STMT_ATTR_RELAXED)
12668 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12669 return add_stmt (stmt);
12672 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12673 down to the element type of an array. */
12675 tree
12676 c_build_qualified_type_1 (tree type, int type_quals, tree layout_qualifier)
12678 if (type == error_mark_node)
12679 return type;
12681 if (TREE_CODE (type) == ARRAY_TYPE)
12683 tree t;
12684 tree element_type = c_build_qualified_type_1 (TREE_TYPE (type),
12685 type_quals,
12686 layout_qualifier);
12688 /* See if we already have an identically qualified type. */
12689 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12691 const tree t_elem_type = strip_array_types (t);
12692 tree t_elem_block_factor = TYPE_UPC_BLOCK_FACTOR (t_elem_type);
12693 if (TYPE_QUALS (t_elem_type) == type_quals
12694 && t_elem_block_factor == layout_qualifier
12695 && TYPE_NAME (t) == TYPE_NAME (type)
12696 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12697 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12698 TYPE_ATTRIBUTES (type)))
12699 break;
12701 if (!t)
12703 tree domain = TYPE_DOMAIN (type);
12705 t = build_variant_type_copy (type);
12706 TREE_TYPE (t) = element_type;
12708 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12709 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12710 SET_TYPE_STRUCTURAL_EQUALITY (t);
12711 else if (TYPE_CANONICAL (element_type) != element_type
12712 || (domain && TYPE_CANONICAL (domain) != domain))
12714 tree unqualified_canon
12715 = build_array_type (TYPE_CANONICAL (element_type),
12716 domain? TYPE_CANONICAL (domain)
12717 : NULL_TREE);
12718 TYPE_CANONICAL (t)
12719 = c_build_qualified_type_1 (unqualified_canon, type_quals,
12720 layout_qualifier);
12722 else
12723 TYPE_CANONICAL (t) = t;
12725 return t;
12728 /* A restrict-qualified pointer type must be a pointer to object or
12729 incomplete type. Note that the use of POINTER_TYPE_P also allows
12730 REFERENCE_TYPEs, which is appropriate for C++. */
12731 if ((type_quals & TYPE_QUAL_RESTRICT)
12732 && (!POINTER_TYPE_P (type)
12733 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12735 error ("invalid use of %<restrict%>");
12736 type_quals &= ~TYPE_QUAL_RESTRICT;
12739 return build_qualified_type_1 (type, type_quals, layout_qualifier);
12742 /* Build a VA_ARG_EXPR for the C parser. */
12744 tree
12745 c_build_va_arg (location_t loc, tree expr, tree type)
12747 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12748 warning_at (loc, OPT_Wc___compat,
12749 "C++ requires promoted type, not enum type, in %<va_arg%>");
12750 return build_va_arg (loc, expr, type);
12753 /* Return truthvalue of whether T1 is the same tree structure as T2.
12754 Return 1 if they are the same. Return 0 if they are different. */
12756 bool
12757 c_tree_equal (tree t1, tree t2)
12759 enum tree_code code1, code2;
12761 if (t1 == t2)
12762 return true;
12763 if (!t1 || !t2)
12764 return false;
12766 for (code1 = TREE_CODE (t1);
12767 CONVERT_EXPR_CODE_P (code1)
12768 || code1 == NON_LVALUE_EXPR;
12769 code1 = TREE_CODE (t1))
12770 t1 = TREE_OPERAND (t1, 0);
12771 for (code2 = TREE_CODE (t2);
12772 CONVERT_EXPR_CODE_P (code2)
12773 || code2 == NON_LVALUE_EXPR;
12774 code2 = TREE_CODE (t2))
12775 t2 = TREE_OPERAND (t2, 0);
12777 /* They might have become equal now. */
12778 if (t1 == t2)
12779 return true;
12781 if (code1 != code2)
12782 return false;
12784 switch (code1)
12786 case INTEGER_CST:
12787 return wi::eq_p (t1, t2);
12789 case REAL_CST:
12790 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12792 case STRING_CST:
12793 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12794 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12795 TREE_STRING_LENGTH (t1));
12797 case FIXED_CST:
12798 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12799 TREE_FIXED_CST (t2));
12801 case COMPLEX_CST:
12802 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12803 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12805 case VECTOR_CST:
12806 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12808 case CONSTRUCTOR:
12809 /* We need to do this when determining whether or not two
12810 non-type pointer to member function template arguments
12811 are the same. */
12812 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12813 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12814 return false;
12816 tree field, value;
12817 unsigned int i;
12818 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12820 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12821 if (!c_tree_equal (field, elt2->index)
12822 || !c_tree_equal (value, elt2->value))
12823 return false;
12826 return true;
12828 case TREE_LIST:
12829 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12830 return false;
12831 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12832 return false;
12833 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12835 case SAVE_EXPR:
12836 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12838 case CALL_EXPR:
12840 tree arg1, arg2;
12841 call_expr_arg_iterator iter1, iter2;
12842 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12843 return false;
12844 for (arg1 = first_call_expr_arg (t1, &iter1),
12845 arg2 = first_call_expr_arg (t2, &iter2);
12846 arg1 && arg2;
12847 arg1 = next_call_expr_arg (&iter1),
12848 arg2 = next_call_expr_arg (&iter2))
12849 if (!c_tree_equal (arg1, arg2))
12850 return false;
12851 if (arg1 || arg2)
12852 return false;
12853 return true;
12856 case TARGET_EXPR:
12858 tree o1 = TREE_OPERAND (t1, 0);
12859 tree o2 = TREE_OPERAND (t2, 0);
12861 /* Special case: if either target is an unallocated VAR_DECL,
12862 it means that it's going to be unified with whatever the
12863 TARGET_EXPR is really supposed to initialize, so treat it
12864 as being equivalent to anything. */
12865 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12866 && !DECL_RTL_SET_P (o1))
12867 /*Nop*/;
12868 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12869 && !DECL_RTL_SET_P (o2))
12870 /*Nop*/;
12871 else if (!c_tree_equal (o1, o2))
12872 return false;
12874 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12877 case COMPONENT_REF:
12878 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12879 return false;
12880 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12882 case PARM_DECL:
12883 case VAR_DECL:
12884 case CONST_DECL:
12885 case FIELD_DECL:
12886 case FUNCTION_DECL:
12887 case IDENTIFIER_NODE:
12888 case SSA_NAME:
12889 return false;
12891 case TREE_VEC:
12893 unsigned ix;
12894 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12895 return false;
12896 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12897 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12898 TREE_VEC_ELT (t2, ix)))
12899 return false;
12900 return true;
12903 default:
12904 break;
12907 switch (TREE_CODE_CLASS (code1))
12909 case tcc_unary:
12910 case tcc_binary:
12911 case tcc_comparison:
12912 case tcc_expression:
12913 case tcc_vl_exp:
12914 case tcc_reference:
12915 case tcc_statement:
12917 int i, n = TREE_OPERAND_LENGTH (t1);
12919 switch (code1)
12921 case PREINCREMENT_EXPR:
12922 case PREDECREMENT_EXPR:
12923 case POSTINCREMENT_EXPR:
12924 case POSTDECREMENT_EXPR:
12925 n = 1;
12926 break;
12927 case ARRAY_REF:
12928 n = 2;
12929 break;
12930 default:
12931 break;
12934 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12935 && n != TREE_OPERAND_LENGTH (t2))
12936 return false;
12938 for (i = 0; i < n; ++i)
12939 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12940 return false;
12942 return true;
12945 case tcc_type:
12946 return comptypes (t1, t2);
12947 default:
12948 gcc_unreachable ();
12950 /* We can get here with --disable-checking. */
12951 return false;
12954 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12955 spawn-helper and BODY is the newly created body for FNDECL. */
12957 void
12958 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12960 tree list = alloc_stmt_list ();
12961 tree frame = make_cilk_frame (fndecl);
12962 tree dtor = create_cilk_function_exit (frame, false, true);
12963 add_local_decl (cfun, frame);
12965 DECL_SAVED_TREE (fndecl) = list;
12966 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12967 frame);
12968 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12969 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12971 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12972 append_to_statement_list (detach_expr, &body_list);
12974 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12975 body = fold_build_cleanup_point_expr (void_type_node, body);
12977 append_to_statement_list (body, &body_list);
12978 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12979 body_list, dtor), &list);