Merge trunk version 195164 into gupc branch.
[official-gcc.git] / gcc / c / c-typeck.c
blob4f3be28f908a4f44ec74ef2bc8fb93515654c7da
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2013 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 "langhooks.h"
32 #include "c-tree.h"
33 #include "c-lang.h"
34 #include "flags.h"
35 #include "intl.h"
36 #include "target.h"
37 #include "tree-iterator.h"
38 #include "bitmap.h"
39 #include "gimple.h"
40 #include "c-family/c-objc.h"
41 #include "c-family/c-upc.h"
42 #include "c-family/c-common.h"
44 /* Possible cases of implicit bad conversions. Used to select
45 diagnostic messages in convert_for_assignment. */
46 enum impl_conv {
47 ic_argpass,
48 ic_assign,
49 ic_init,
50 ic_return
53 /* The level of nesting inside "__alignof__". */
54 int in_alignof;
56 /* The level of nesting inside "sizeof". */
57 int in_sizeof;
59 /* The level of nesting inside "typeof". */
60 int in_typeof;
62 /* The argument of last parsed sizeof expression, only to be tested
63 if expr.original_code == SIZEOF_EXPR. */
64 tree c_last_sizeof_arg;
66 /* Nonzero if we've already printed a "missing braces around initializer"
67 message within this initializer. */
68 static int missing_braces_mentioned;
70 static int require_constant_value;
71 static int require_constant_elements;
73 static bool null_pointer_constant_p (const_tree);
74 static tree qualify_type (tree, tree);
75 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
76 bool *);
77 static int comp_target_types (location_t, tree, tree);
78 static int function_types_compatible_p (const_tree, const_tree, bool *,
79 bool *);
80 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
81 static tree lookup_field (tree, tree);
82 static int convert_arguments (tree, vec<tree, va_gc> *, vec<tree, va_gc> *,
83 tree, tree);
84 static tree c_pointer_int_sum (location_t, enum tree_code, tree, tree);
85 static tree pointer_diff (location_t, tree, tree);
86 static tree convert_for_assignment (location_t, tree, tree, tree,
87 enum impl_conv, bool, tree, tree, int);
88 static tree valid_compound_expr_initializer (tree, tree);
89 static void push_string (const char *);
90 static void push_member_name (tree);
91 static int spelling_length (void);
92 static char *print_spelling (char *);
93 static void warning_init (int, const char *);
94 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
95 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
96 struct obstack *);
97 static void output_pending_init_elements (int, struct obstack *);
98 static int set_designator (int, struct obstack *);
99 static void push_range_stack (tree, struct obstack *);
100 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
101 static void set_nonincremental_init (struct obstack *);
102 static void set_nonincremental_init_from_string (tree, struct obstack *);
103 static tree find_init_member (tree, struct obstack *);
104 static void readonly_warning (tree, enum lvalue_use);
105 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
106 static void record_maybe_used_decl (tree);
107 static int comptypes_internal (tree, tree, bool *, bool *);
109 /* Return true if EXP is a null pointer constant, false otherwise. */
111 static bool
112 null_pointer_constant_p (const_tree expr)
114 /* This should really operate on c_expr structures, but they aren't
115 yet available everywhere required. */
116 tree type = TREE_TYPE (expr);
117 return (TREE_CODE (expr) == INTEGER_CST
118 && !TREE_OVERFLOW (expr)
119 && integer_zerop (expr)
120 && (INTEGRAL_TYPE_P (type)
121 || (TREE_CODE (type) == POINTER_TYPE
122 && VOID_TYPE_P (TREE_TYPE (type))
123 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
126 /* EXPR may appear in an unevaluated part of an integer constant
127 expression, but not in an evaluated part. Wrap it in a
128 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
129 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
131 static tree
132 note_integer_operands (tree expr)
134 tree ret;
135 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
137 ret = copy_node (expr);
138 TREE_OVERFLOW (ret) = 1;
140 else
142 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
143 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
145 return ret;
148 /* Having checked whether EXPR may appear in an unevaluated part of an
149 integer constant expression and found that it may, remove any
150 C_MAYBE_CONST_EXPR noting this fact and return the resulting
151 expression. */
153 static inline tree
154 remove_c_maybe_const_expr (tree expr)
156 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
157 return C_MAYBE_CONST_EXPR_EXPR (expr);
158 else
159 return expr;
162 \f/* This is a cache to hold if two types are compatible or not. */
164 struct tagged_tu_seen_cache {
165 const struct tagged_tu_seen_cache * next;
166 const_tree t1;
167 const_tree t2;
168 /* The return value of tagged_types_tu_compatible_p if we had seen
169 these two types already. */
170 int val;
173 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
174 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
176 /* Do `exp = require_complete_type (exp);' to make sure exp
177 does not have an incomplete type. (That includes void types.) */
179 tree
180 require_complete_type (tree value)
182 tree type = TREE_TYPE (value);
184 if (value == error_mark_node || type == error_mark_node)
185 return error_mark_node;
187 /* First, detect a valid value with a complete type. */
188 if (COMPLETE_TYPE_P (type))
189 return value;
191 c_incomplete_type_error (value, type);
192 return error_mark_node;
195 /* Print an error message for invalid use of an incomplete type.
196 VALUE is the expression that was used (or 0 if that isn't known)
197 and TYPE is the type that was invalid. */
199 void
200 c_incomplete_type_error (const_tree value, const_tree type)
202 const char *type_code_string;
204 /* Avoid duplicate error message. */
205 if (TREE_CODE (type) == ERROR_MARK)
206 return;
208 if (value != 0 && (TREE_CODE (value) == VAR_DECL
209 || TREE_CODE (value) == PARM_DECL))
210 error ("%qD has an incomplete type", value);
211 else
213 retry:
214 /* We must print an error message. Be clever about what it says. */
216 switch (TREE_CODE (type))
218 case RECORD_TYPE:
219 type_code_string = "struct";
220 break;
222 case UNION_TYPE:
223 type_code_string = "union";
224 break;
226 case ENUMERAL_TYPE:
227 type_code_string = "enum";
228 break;
230 case VOID_TYPE:
231 error ("invalid use of void expression");
232 return;
234 case ARRAY_TYPE:
235 if (TYPE_DOMAIN (type))
237 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
239 error ("invalid use of flexible array member");
240 return;
242 type = TREE_TYPE (type);
243 goto retry;
245 error ("invalid use of array with unspecified bounds");
246 return;
248 default:
249 gcc_unreachable ();
252 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
253 error ("invalid use of undefined type %<%s %E%>",
254 type_code_string, TYPE_NAME (type));
255 else
256 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
257 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
261 /* Given a type, apply default promotions wrt unnamed function
262 arguments and return the new type. */
264 tree
265 c_type_promotes_to (tree type)
267 if (TYPE_MAIN_VARIANT (type) == float_type_node)
268 return double_type_node;
270 if (c_promoting_integer_type_p (type))
272 /* Preserve unsignedness if not really getting any wider. */
273 if (TYPE_UNSIGNED (type)
274 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
275 return unsigned_type_node;
276 return integer_type_node;
279 return type;
282 /* Return true if between two named address spaces, whether there is a superset
283 named address space that encompasses both address spaces. If there is a
284 superset, return which address space is the superset. */
286 static bool
287 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
289 if (as1 == as2)
291 *common = as1;
292 return true;
294 else if (targetm.addr_space.subset_p (as1, as2))
296 *common = as2;
297 return true;
299 else if (targetm.addr_space.subset_p (as2, as1))
301 *common = as1;
302 return true;
304 else
305 return false;
308 /* Return a variant of TYPE which has all the type qualifiers of LIKE
309 as well as those of TYPE. */
311 static tree
312 qualify_type (tree type, tree like)
314 tree result_type;
315 addr_space_t as_type = TYPE_ADDR_SPACE (type);
316 addr_space_t as_like = TYPE_ADDR_SPACE (like);
317 addr_space_t as_common;
318 int result_quals;
319 tree result_block_factor = NULL_TREE;
321 gcc_assert (type && TREE_CODE (type) != ARRAY_TYPE);
322 gcc_assert (like && TREE_CODE (like) != ARRAY_TYPE);
324 /* If the two named address spaces are different, determine the common
325 superset address space. If there isn't one, raise an error. */
326 if (!addr_space_superset (as_type, as_like, &as_common))
328 as_common = as_type;
329 error ("%qT and %qT are in disjoint named address spaces",
330 type, like);
333 result_quals = TYPE_QUALS_NO_ADDR_SPACE (type)
334 | TYPE_QUALS_NO_ADDR_SPACE (like)
335 | ENCODE_QUAL_ADDR_SPACE (as_common);
337 if (result_quals & TYPE_QUAL_SHARED)
339 tree b1 = TYPE_BLOCK_FACTOR (type);
340 tree b2 = TYPE_BLOCK_FACTOR (like);
341 /* We can merge in a new UPC blocking factor only
342 if one/other is NULL. Otherwise, they must match. */
343 if (b1 != b2)
345 if (b1 && !b2)
346 result_block_factor = b1;
347 else if (!b1 && b2)
348 result_block_factor = b2;
349 else
350 gcc_unreachable ();
354 result_type = c_build_qualified_type_1 (type, result_quals,
355 result_block_factor);
357 return result_type;
360 /* Return true iff the given tree T is a variable length array. */
362 bool
363 c_vla_type_p (const_tree t)
365 if (TREE_CODE (t) == ARRAY_TYPE
366 && C_TYPE_VARIABLE_SIZE (t))
367 return true;
368 return false;
371 /* Return the composite type of two compatible types.
373 We assume that comptypes has already been done and returned
374 nonzero; if that isn't so, this may crash. In particular, we
375 assume that qualifiers match. */
377 tree
378 composite_type (tree t1, tree t2)
380 enum tree_code code1;
381 enum tree_code code2;
382 tree attributes;
384 /* Save time if the two types are the same. */
386 if (t1 == t2) return t1;
388 /* If one type is nonsense, use the other. */
389 if (t1 == error_mark_node)
390 return t2;
391 if (t2 == error_mark_node)
392 return t1;
394 code1 = TREE_CODE (t1);
395 code2 = TREE_CODE (t2);
397 /* Merge the attributes. */
398 attributes = targetm.merge_type_attributes (t1, t2);
400 /* If one is an enumerated type and the other is the compatible
401 integer type, the composite type might be either of the two
402 (DR#013 question 3). For consistency, use the enumerated type as
403 the composite type. */
405 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
406 return t1;
407 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
408 return t2;
410 gcc_assert (code1 == code2);
412 switch (code1)
414 case POINTER_TYPE:
415 /* For two pointers, do this recursively on the target type. */
417 tree pointed_to_1 = TREE_TYPE (t1);
418 tree pointed_to_2 = TREE_TYPE (t2);
419 tree target = composite_type (pointed_to_1, pointed_to_2);
420 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
421 t1 = build_type_attribute_variant (t1, attributes);
422 return qualify_type (t1, t2);
425 case ARRAY_TYPE:
427 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
428 int quals;
429 tree unqual_elt;
430 tree d1 = TYPE_DOMAIN (t1);
431 tree d2 = TYPE_DOMAIN (t2);
432 bool d1_variable, d2_variable;
433 bool d1_zero, d2_zero;
434 bool t1_complete, t2_complete;
436 /* We should not have any type quals on arrays at all. */
437 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
438 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
440 t1_complete = COMPLETE_TYPE_P (t1);
441 t2_complete = COMPLETE_TYPE_P (t2);
443 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
444 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
446 d1_variable = (!d1_zero
447 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
448 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
449 d2_variable = (!d2_zero
450 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
451 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
452 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
453 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
455 /* Save space: see if the result is identical to one of the args. */
456 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
457 && (d2_variable || d2_zero || !d1_variable))
458 return build_type_attribute_variant (t1, attributes);
459 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
460 && (d1_variable || d1_zero || !d2_variable))
461 return build_type_attribute_variant (t2, attributes);
463 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
464 return build_type_attribute_variant (t1, attributes);
465 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
466 return build_type_attribute_variant (t2, attributes);
468 /* Merge the element types, and have a size if either arg has
469 one. We may have qualifiers on the element types. To set
470 up TYPE_MAIN_VARIANT correctly, we need to form the
471 composite of the unqualified types and add the qualifiers
472 back at the end. */
473 quals = TYPE_QUALS (strip_array_types (elt));
474 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
475 t1 = build_array_type (unqual_elt,
476 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
477 && (d2_variable
478 || d2_zero
479 || !d1_variable))
480 ? t1
481 : t2));
482 /* Ensure a composite type involving a zero-length array type
483 is a zero-length type not an incomplete type. */
484 if (d1_zero && d2_zero
485 && (t1_complete || t2_complete)
486 && !COMPLETE_TYPE_P (t1))
488 TYPE_SIZE (t1) = bitsize_zero_node;
489 TYPE_SIZE_UNIT (t1) = size_zero_node;
491 t1 = c_build_qualified_type (t1, quals);
492 return build_type_attribute_variant (t1, attributes);
495 case ENUMERAL_TYPE:
496 case RECORD_TYPE:
497 case UNION_TYPE:
498 if (attributes != NULL)
500 /* Try harder not to create a new aggregate type. */
501 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
502 return t1;
503 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
504 return t2;
506 return build_type_attribute_variant (t1, attributes);
508 case FUNCTION_TYPE:
509 /* Function types: prefer the one that specified arg types.
510 If both do, merge the arg types. Also merge the return types. */
512 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
513 tree p1 = TYPE_ARG_TYPES (t1);
514 tree p2 = TYPE_ARG_TYPES (t2);
515 int len;
516 tree newargs, n;
517 int i;
519 /* Save space: see if the result is identical to one of the args. */
520 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
521 return build_type_attribute_variant (t1, attributes);
522 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
523 return build_type_attribute_variant (t2, attributes);
525 /* Simple way if one arg fails to specify argument types. */
526 if (TYPE_ARG_TYPES (t1) == 0)
528 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
529 t1 = build_type_attribute_variant (t1, attributes);
530 return qualify_type (t1, t2);
532 if (TYPE_ARG_TYPES (t2) == 0)
534 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
535 t1 = build_type_attribute_variant (t1, attributes);
536 return qualify_type (t1, t2);
539 /* If both args specify argument types, we must merge the two
540 lists, argument by argument. */
542 len = list_length (p1);
543 newargs = 0;
545 for (i = 0; i < len; i++)
546 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
548 n = newargs;
550 for (; p1;
551 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
553 /* A null type means arg type is not specified.
554 Take whatever the other function type has. */
555 if (TREE_VALUE (p1) == 0)
557 TREE_VALUE (n) = TREE_VALUE (p2);
558 goto parm_done;
560 if (TREE_VALUE (p2) == 0)
562 TREE_VALUE (n) = TREE_VALUE (p1);
563 goto parm_done;
566 /* Given wait (union {union wait *u; int *i} *)
567 and wait (union wait *),
568 prefer union wait * as type of parm. */
569 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
570 && TREE_VALUE (p1) != TREE_VALUE (p2))
572 tree memb;
573 tree mv2 = TREE_VALUE (p2);
574 if (mv2 && mv2 != error_mark_node
575 && TREE_CODE (mv2) != ARRAY_TYPE)
576 mv2 = TYPE_MAIN_VARIANT (mv2);
577 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
578 memb; memb = DECL_CHAIN (memb))
580 tree mv3 = TREE_TYPE (memb);
581 if (mv3 && mv3 != error_mark_node
582 && TREE_CODE (mv3) != ARRAY_TYPE)
583 mv3 = TYPE_MAIN_VARIANT (mv3);
584 if (comptypes (mv3, mv2))
586 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
587 TREE_VALUE (p2));
588 pedwarn (input_location, OPT_Wpedantic,
589 "function types not truly compatible in ISO C");
590 goto parm_done;
594 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
595 && TREE_VALUE (p2) != TREE_VALUE (p1))
597 tree memb;
598 tree mv1 = TREE_VALUE (p1);
599 if (mv1 && mv1 != error_mark_node
600 && TREE_CODE (mv1) != ARRAY_TYPE)
601 mv1 = TYPE_MAIN_VARIANT (mv1);
602 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
603 memb; memb = DECL_CHAIN (memb))
605 tree mv3 = TREE_TYPE (memb);
606 if (mv3 && mv3 != error_mark_node
607 && TREE_CODE (mv3) != ARRAY_TYPE)
608 mv3 = TYPE_MAIN_VARIANT (mv3);
609 if (comptypes (mv3, mv1))
611 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
612 TREE_VALUE (p1));
613 pedwarn (input_location, OPT_Wpedantic,
614 "function types not truly compatible in ISO C");
615 goto parm_done;
619 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
620 parm_done: ;
623 t1 = build_function_type (valtype, newargs);
624 t1 = qualify_type (t1, t2);
625 /* ... falls through ... */
628 default:
629 return build_type_attribute_variant (t1, attributes);
634 /* Return the type of a conditional expression between pointers to
635 possibly differently qualified versions of compatible types.
637 We assume that comp_target_types has already been done and returned
638 nonzero; if that isn't so, this may crash. */
640 static tree
641 common_pointer_type (tree t1, tree t2)
643 tree attributes;
644 tree pointed_to_1, mv1;
645 tree pointed_to_2, mv2;
646 tree target;
647 unsigned target_quals;
648 tree target_block_factor = NULL_TREE;
649 addr_space_t as1, as2, as_common;
650 int quals1, quals2;
652 /* Save time if the two types are the same. */
654 if (t1 == t2) return t1;
656 /* If one type is nonsense, use the other. */
657 if (t1 == error_mark_node)
658 return t2;
659 if (t2 == error_mark_node)
660 return t1;
662 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
663 && TREE_CODE (t2) == POINTER_TYPE);
665 /* Merge the attributes. */
666 attributes = targetm.merge_type_attributes (t1, t2);
668 /* Find the composite type of the target types, and combine the
669 qualifiers of the two types' targets. Do not lose qualifiers on
670 array element types by taking the TYPE_MAIN_VARIANT. */
671 mv1 = pointed_to_1 = TREE_TYPE (t1);
672 mv2 = pointed_to_2 = TREE_TYPE (t2);
673 if (TREE_CODE (mv1) != ARRAY_TYPE)
674 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
675 if (TREE_CODE (mv2) != ARRAY_TYPE)
676 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
677 target = composite_type (mv1, mv2);
679 /* For function types do not merge const qualifiers, but drop them
680 if used inconsistently. The middle-end uses these to mark const
681 and noreturn functions. */
682 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
683 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
685 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
686 target_quals = (quals1 & quals2);
687 else
688 target_quals = (quals1 | quals2);
690 if (target_quals & TYPE_QUAL_SHARED)
691 target_block_factor = TYPE_BLOCK_FACTOR (
692 strip_array_types (pointed_to_1));
694 /* If the two named address spaces are different, determine the common
695 superset address space. This is guaranteed to exist due to the
696 assumption that comp_target_type returned non-zero. */
697 as1 = TYPE_ADDR_SPACE (pointed_to_1);
698 as2 = TYPE_ADDR_SPACE (pointed_to_2);
699 if (!addr_space_superset (as1, as2, &as_common))
700 gcc_unreachable ();
702 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
703 t2 = c_build_qualified_type_1 (target, target_quals, target_block_factor);
705 t1 = build_pointer_type (t2);
706 return build_type_attribute_variant (t1, attributes);
709 /* Return the common type for two arithmetic types under the usual
710 arithmetic conversions. The default conversions have already been
711 applied, and enumerated types converted to their compatible integer
712 types. The resulting type is unqualified and has no attributes.
714 This is the type for the result of most arithmetic operations
715 if the operands have the given two types. */
717 static tree
718 c_common_type (tree t1, tree t2)
720 enum tree_code code1;
721 enum tree_code code2;
723 /* If one type is nonsense, use the other. */
724 if (t1 == error_mark_node)
725 return t2;
726 if (t2 == error_mark_node)
727 return t1;
729 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
730 t1 = TYPE_MAIN_VARIANT (t1);
732 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
733 t2 = TYPE_MAIN_VARIANT (t2);
735 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
736 t1 = build_type_attribute_variant (t1, NULL_TREE);
738 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
739 t2 = build_type_attribute_variant (t2, NULL_TREE);
741 /* Save time if the two types are the same. */
743 if (t1 == t2) return t1;
745 code1 = TREE_CODE (t1);
746 code2 = TREE_CODE (t2);
748 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
749 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
750 || code1 == INTEGER_TYPE);
751 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
752 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
753 || code2 == INTEGER_TYPE);
755 /* When one operand is a decimal float type, the other operand cannot be
756 a generic float type or a complex type. We also disallow vector types
757 here. */
758 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
759 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
761 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
763 error ("can%'t mix operands of decimal float and vector types");
764 return error_mark_node;
766 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
768 error ("can%'t mix operands of decimal float and complex types");
769 return error_mark_node;
771 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
773 error ("can%'t mix operands of decimal float and other float types");
774 return error_mark_node;
778 /* If one type is a vector type, return that type. (How the usual
779 arithmetic conversions apply to the vector types extension is not
780 precisely specified.) */
781 if (code1 == VECTOR_TYPE)
782 return t1;
784 if (code2 == VECTOR_TYPE)
785 return t2;
787 /* If one type is complex, form the common type of the non-complex
788 components, then make that complex. Use T1 or T2 if it is the
789 required type. */
790 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
792 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
793 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
794 tree subtype = c_common_type (subtype1, subtype2);
796 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
797 return t1;
798 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
799 return t2;
800 else
801 return build_complex_type (subtype);
804 /* If only one is real, use it as the result. */
806 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
807 return t1;
809 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
810 return t2;
812 /* If both are real and either are decimal floating point types, use
813 the decimal floating point type with the greater precision. */
815 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
817 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
818 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
819 return dfloat128_type_node;
820 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
821 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
822 return dfloat64_type_node;
823 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
824 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
825 return dfloat32_type_node;
828 /* Deal with fixed-point types. */
829 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
831 unsigned int unsignedp = 0, satp = 0;
832 enum machine_mode m1, m2;
833 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
835 m1 = TYPE_MODE (t1);
836 m2 = TYPE_MODE (t2);
838 /* If one input type is saturating, the result type is saturating. */
839 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
840 satp = 1;
842 /* If both fixed-point types are unsigned, the result type is unsigned.
843 When mixing fixed-point and integer types, follow the sign of the
844 fixed-point type.
845 Otherwise, the result type is signed. */
846 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
847 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
848 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
849 && TYPE_UNSIGNED (t1))
850 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
851 && TYPE_UNSIGNED (t2)))
852 unsignedp = 1;
854 /* The result type is signed. */
855 if (unsignedp == 0)
857 /* If the input type is unsigned, we need to convert to the
858 signed type. */
859 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
861 enum mode_class mclass = (enum mode_class) 0;
862 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
863 mclass = MODE_FRACT;
864 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
865 mclass = MODE_ACCUM;
866 else
867 gcc_unreachable ();
868 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
870 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
872 enum mode_class mclass = (enum mode_class) 0;
873 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
874 mclass = MODE_FRACT;
875 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
876 mclass = MODE_ACCUM;
877 else
878 gcc_unreachable ();
879 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
883 if (code1 == FIXED_POINT_TYPE)
885 fbit1 = GET_MODE_FBIT (m1);
886 ibit1 = GET_MODE_IBIT (m1);
888 else
890 fbit1 = 0;
891 /* Signed integers need to subtract one sign bit. */
892 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
895 if (code2 == FIXED_POINT_TYPE)
897 fbit2 = GET_MODE_FBIT (m2);
898 ibit2 = GET_MODE_IBIT (m2);
900 else
902 fbit2 = 0;
903 /* Signed integers need to subtract one sign bit. */
904 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
907 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
908 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
909 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
910 satp);
913 /* Both real or both integers; use the one with greater precision. */
915 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
916 return t1;
917 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
918 return t2;
920 /* Same precision. Prefer long longs to longs to ints when the
921 same precision, following the C99 rules on integer type rank
922 (which are equivalent to the C90 rules for C90 types). */
924 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
925 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
926 return long_long_unsigned_type_node;
928 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
929 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
931 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
932 return long_long_unsigned_type_node;
933 else
934 return long_long_integer_type_node;
937 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
938 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
939 return long_unsigned_type_node;
941 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
942 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
944 /* But preserve unsignedness from the other type,
945 since long cannot hold all the values of an unsigned int. */
946 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
947 return long_unsigned_type_node;
948 else
949 return long_integer_type_node;
952 /* Likewise, prefer long double to double even if same size. */
953 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
954 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
955 return long_double_type_node;
957 /* Otherwise prefer the unsigned one. */
959 if (TYPE_UNSIGNED (t1))
960 return t1;
961 else
962 return t2;
965 /* Wrapper around c_common_type that is used by c-common.c and other
966 front end optimizations that remove promotions. ENUMERAL_TYPEs
967 are allowed here and are converted to their compatible integer types.
968 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
969 preferably a non-Boolean type as the common type. */
970 tree
971 common_type (tree t1, tree t2)
973 if (TREE_CODE (t1) == ENUMERAL_TYPE)
974 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
975 if (TREE_CODE (t2) == ENUMERAL_TYPE)
976 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
978 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
979 if (TREE_CODE (t1) == BOOLEAN_TYPE
980 && TREE_CODE (t2) == BOOLEAN_TYPE)
981 return boolean_type_node;
983 /* If either type is BOOLEAN_TYPE, then return the other. */
984 if (TREE_CODE (t1) == BOOLEAN_TYPE)
985 return t2;
986 if (TREE_CODE (t2) == BOOLEAN_TYPE)
987 return t1;
989 return c_common_type (t1, t2);
992 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
993 or various other operations. Return 2 if they are compatible
994 but a warning may be needed if you use them together. */
997 comptypes (tree type1, tree type2)
999 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1000 int val;
1002 val = comptypes_internal (type1, type2, NULL, NULL);
1003 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1005 return val;
1008 /* Like comptypes, but if it returns non-zero because enum and int are
1009 compatible, it sets *ENUM_AND_INT_P to true. */
1011 static int
1012 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1014 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1015 int val;
1017 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1018 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1020 return val;
1023 /* Like comptypes, but if it returns nonzero for different types, it
1024 sets *DIFFERENT_TYPES_P to true. */
1027 comptypes_check_different_types (tree type1, tree type2,
1028 bool *different_types_p)
1030 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1031 int val;
1033 val = comptypes_internal (type1, type2, NULL, different_types_p);
1034 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1036 return val;
1039 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1040 or various other operations. Return 2 if they are compatible
1041 but a warning may be needed if you use them together. If
1042 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1043 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1044 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1045 NULL, and the types are compatible but different enough not to be
1046 permitted in C11 typedef redeclarations, then this sets
1047 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1048 false, but may or may not be set if the types are incompatible.
1049 This differs from comptypes, in that we don't free the seen
1050 types. */
1052 static int
1053 comptypes_internal (tree type1, tree type2, bool *enum_and_int_p,
1054 bool *different_types_p)
1056 tree t1 = type1;
1057 tree t2 = type2;
1058 int attrval, val;
1060 /* Suppress errors caused by previously reported errors. */
1062 if (t1 == t2 || !t1 || !t2
1063 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1064 return 1;
1066 /* Enumerated types are compatible with integer types, but this is
1067 not transitive: two enumerated types in the same translation unit
1068 are compatible with each other only if they are the same type. */
1070 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1072 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1073 if (TREE_CODE (t2) != VOID_TYPE)
1075 if (enum_and_int_p != NULL)
1076 *enum_and_int_p = true;
1077 if (different_types_p != NULL)
1078 *different_types_p = true;
1081 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1083 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1084 if (TREE_CODE (t1) != VOID_TYPE)
1086 if (enum_and_int_p != NULL)
1087 *enum_and_int_p = true;
1088 if (different_types_p != NULL)
1089 *different_types_p = true;
1093 if (t1 == t2)
1094 return 1;
1096 /* Different classes of types can't be compatible. */
1098 if (TREE_CODE (t1) != TREE_CODE (t2))
1099 return 0;
1101 /* Qualifiers must match. C99 6.7.3p9 */
1103 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1104 return 0;
1106 /* If the type is UPC qualified, the block sizes have
1107 to be equal. The block sizes are either NULL
1108 or are the same integer constant. */
1109 if (TYPE_QUALS (t1) & TYPE_QUAL_SHARED)
1111 const_tree b1 = TYPE_BLOCK_FACTOR (t1);
1112 const_tree b2 = TYPE_BLOCK_FACTOR (t2);
1113 if (!(b1 == b2 || tree_int_cst_equal (b1, b2)))
1114 return 0;
1117 /* Allow for two different type nodes which have essentially the same
1118 definition. Note that we already checked for equality of the type
1119 qualifiers (just above). */
1121 if (TREE_CODE (t1) != ARRAY_TYPE
1122 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1123 return 1;
1125 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1126 if (!(attrval = comp_type_attributes (t1, t2)))
1127 return 0;
1129 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1130 val = 0;
1132 switch (TREE_CODE (t1))
1134 case POINTER_TYPE:
1135 /* Do not remove mode or aliasing information. */
1136 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1137 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1138 break;
1139 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1140 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1141 enum_and_int_p, different_types_p));
1142 break;
1144 case FUNCTION_TYPE:
1145 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1146 different_types_p);
1147 break;
1149 case ARRAY_TYPE:
1151 tree d1 = TYPE_DOMAIN (t1);
1152 tree d2 = TYPE_DOMAIN (t2);
1153 bool d1_variable, d2_variable;
1154 bool d1_zero, d2_zero;
1155 val = 1;
1157 /* Target types must match incl. qualifiers. */
1158 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1159 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1160 enum_and_int_p,
1161 different_types_p)))
1162 return 0;
1164 if (different_types_p != NULL
1165 && (d1 == 0) != (d2 == 0))
1166 *different_types_p = true;
1167 /* Sizes must match unless one is missing or variable. */
1168 if (d1 == 0 || d2 == 0 || d1 == d2)
1169 break;
1171 d1_zero = !TYPE_MAX_VALUE (d1);
1172 d2_zero = !TYPE_MAX_VALUE (d2);
1174 d1_variable = (!d1_zero
1175 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1176 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1177 d2_variable = (!d2_zero
1178 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1179 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1180 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1181 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1183 if (different_types_p != NULL
1184 && d1_variable != d2_variable)
1185 *different_types_p = true;
1186 if (d1_variable || d2_variable)
1187 break;
1188 if (d1_zero && d2_zero)
1189 break;
1190 if (d1_zero || d2_zero
1191 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1192 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1193 val = 0;
1195 break;
1198 case ENUMERAL_TYPE:
1199 case RECORD_TYPE:
1200 case UNION_TYPE:
1201 if (val != 1 && !same_translation_unit_p (t1, t2))
1203 tree a1 = TYPE_ATTRIBUTES (t1);
1204 tree a2 = TYPE_ATTRIBUTES (t2);
1206 if (! attribute_list_contained (a1, a2)
1207 && ! attribute_list_contained (a2, a1))
1208 break;
1210 if (attrval != 2)
1211 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1212 different_types_p);
1213 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1214 different_types_p);
1216 break;
1218 case VECTOR_TYPE:
1219 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1220 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1221 enum_and_int_p, different_types_p));
1222 break;
1224 default:
1225 break;
1227 return attrval == 2 && val == 1 ? 2 : val;
1230 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1231 their qualifiers, except for named address spaces. If the pointers point to
1232 different named addresses, then we must determine if one address space is a
1233 subset of the other. */
1235 static int
1236 comp_target_types (location_t location, tree ttl, tree ttr)
1238 int val;
1239 tree mvl = TREE_TYPE (ttl);
1240 tree mvr = TREE_TYPE (ttr);
1241 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1242 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1243 addr_space_t as_common;
1244 bool enum_and_int_p;
1246 /* Fail if pointers point to incompatible address spaces. */
1247 if (!addr_space_superset (asl, asr, &as_common))
1248 return 0;
1250 /* Do not lose qualifiers on element types of array types that are
1251 pointer targets by taking their TYPE_MAIN_VARIANT. */
1252 if (TREE_CODE (mvl) != ARRAY_TYPE)
1253 mvl = TYPE_MAIN_VARIANT (mvl);
1254 if (TREE_CODE (mvr) != ARRAY_TYPE)
1255 mvr = TYPE_MAIN_VARIANT (mvr);
1256 enum_and_int_p = false;
1257 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1259 if (val == 2)
1260 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1262 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1263 warning_at (location, OPT_Wc___compat,
1264 "pointer target types incompatible in C++");
1266 return val;
1269 /* Subroutines of `comptypes'. */
1271 /* Determine whether two trees derive from the same translation unit.
1272 If the CONTEXT chain ends in a null, that tree's context is still
1273 being parsed, so if two trees have context chains ending in null,
1274 they're in the same translation unit. */
1276 same_translation_unit_p (const_tree t1, const_tree t2)
1278 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1279 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1281 case tcc_declaration:
1282 t1 = DECL_CONTEXT (t1); break;
1283 case tcc_type:
1284 t1 = TYPE_CONTEXT (t1); break;
1285 case tcc_exceptional:
1286 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1287 default: gcc_unreachable ();
1290 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1291 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1293 case tcc_declaration:
1294 t2 = DECL_CONTEXT (t2); break;
1295 case tcc_type:
1296 t2 = TYPE_CONTEXT (t2); break;
1297 case tcc_exceptional:
1298 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1299 default: gcc_unreachable ();
1302 return t1 == t2;
1305 /* Allocate the seen two types, assuming that they are compatible. */
1307 static struct tagged_tu_seen_cache *
1308 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1310 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1311 tu->next = tagged_tu_seen_base;
1312 tu->t1 = t1;
1313 tu->t2 = t2;
1315 tagged_tu_seen_base = tu;
1317 /* The C standard says that two structures in different translation
1318 units are compatible with each other only if the types of their
1319 fields are compatible (among other things). We assume that they
1320 are compatible until proven otherwise when building the cache.
1321 An example where this can occur is:
1322 struct a
1324 struct a *next;
1326 If we are comparing this against a similar struct in another TU,
1327 and did not assume they were compatible, we end up with an infinite
1328 loop. */
1329 tu->val = 1;
1330 return tu;
1333 /* Free the seen types until we get to TU_TIL. */
1335 static void
1336 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1338 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1339 while (tu != tu_til)
1341 const struct tagged_tu_seen_cache *const tu1
1342 = (const struct tagged_tu_seen_cache *) tu;
1343 tu = tu1->next;
1344 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1346 tagged_tu_seen_base = tu_til;
1349 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1350 compatible. If the two types are not the same (which has been
1351 checked earlier), this can only happen when multiple translation
1352 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1353 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1354 comptypes_internal. */
1356 static int
1357 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1358 bool *enum_and_int_p, bool *different_types_p)
1360 tree s1, s2;
1361 bool needs_warning = false;
1363 /* We have to verify that the tags of the types are the same. This
1364 is harder than it looks because this may be a typedef, so we have
1365 to go look at the original type. It may even be a typedef of a
1366 typedef...
1367 In the case of compiler-created builtin structs the TYPE_DECL
1368 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1369 while (TYPE_NAME (t1)
1370 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1371 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1372 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1374 while (TYPE_NAME (t2)
1375 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1376 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1377 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1379 /* C90 didn't have the requirement that the two tags be the same. */
1380 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1381 return 0;
1383 /* C90 didn't say what happened if one or both of the types were
1384 incomplete; we choose to follow C99 rules here, which is that they
1385 are compatible. */
1386 if (TYPE_SIZE (t1) == NULL
1387 || TYPE_SIZE (t2) == NULL)
1388 return 1;
1391 const struct tagged_tu_seen_cache * tts_i;
1392 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1393 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1394 return tts_i->val;
1397 switch (TREE_CODE (t1))
1399 case ENUMERAL_TYPE:
1401 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1402 /* Speed up the case where the type values are in the same order. */
1403 tree tv1 = TYPE_VALUES (t1);
1404 tree tv2 = TYPE_VALUES (t2);
1406 if (tv1 == tv2)
1408 return 1;
1411 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1413 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1414 break;
1415 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1417 tu->val = 0;
1418 return 0;
1422 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1424 return 1;
1426 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1428 tu->val = 0;
1429 return 0;
1432 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1434 tu->val = 0;
1435 return 0;
1438 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1440 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1441 if (s2 == NULL
1442 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1444 tu->val = 0;
1445 return 0;
1448 return 1;
1451 case UNION_TYPE:
1453 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1454 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1456 tu->val = 0;
1457 return 0;
1460 /* Speed up the common case where the fields are in the same order. */
1461 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1462 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1464 int result;
1466 if (DECL_NAME (s1) != DECL_NAME (s2))
1467 break;
1468 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1469 enum_and_int_p, different_types_p);
1471 if (result != 1 && !DECL_NAME (s1))
1472 break;
1473 if (result == 0)
1475 tu->val = 0;
1476 return 0;
1478 if (result == 2)
1479 needs_warning = true;
1481 if (TREE_CODE (s1) == FIELD_DECL
1482 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1483 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1485 tu->val = 0;
1486 return 0;
1489 if (!s1 && !s2)
1491 tu->val = needs_warning ? 2 : 1;
1492 return tu->val;
1495 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1497 bool ok = false;
1499 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1500 if (DECL_NAME (s1) == DECL_NAME (s2))
1502 int result;
1504 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1505 enum_and_int_p,
1506 different_types_p);
1508 if (result != 1 && !DECL_NAME (s1))
1509 continue;
1510 if (result == 0)
1512 tu->val = 0;
1513 return 0;
1515 if (result == 2)
1516 needs_warning = true;
1518 if (TREE_CODE (s1) == FIELD_DECL
1519 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1520 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1521 break;
1523 ok = true;
1524 break;
1526 if (!ok)
1528 tu->val = 0;
1529 return 0;
1532 tu->val = needs_warning ? 2 : 10;
1533 return tu->val;
1536 case RECORD_TYPE:
1538 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1540 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1541 s1 && s2;
1542 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1544 int result;
1545 if (TREE_CODE (s1) != TREE_CODE (s2)
1546 || DECL_NAME (s1) != DECL_NAME (s2))
1547 break;
1548 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1549 enum_and_int_p, different_types_p);
1550 if (result == 0)
1551 break;
1552 if (result == 2)
1553 needs_warning = true;
1555 if (TREE_CODE (s1) == FIELD_DECL
1556 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1557 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1558 break;
1560 if (s1 && s2)
1561 tu->val = 0;
1562 else
1563 tu->val = needs_warning ? 2 : 1;
1564 return tu->val;
1567 default:
1568 gcc_unreachable ();
1572 /* Return 1 if two function types F1 and F2 are compatible.
1573 If either type specifies no argument types,
1574 the other must specify a fixed number of self-promoting arg types.
1575 Otherwise, if one type specifies only the number of arguments,
1576 the other must specify that number of self-promoting arg types.
1577 Otherwise, the argument types must match.
1578 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1580 static int
1581 function_types_compatible_p (const_tree f1, const_tree f2,
1582 bool *enum_and_int_p, bool *different_types_p)
1584 tree args1, args2;
1585 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1586 int val = 1;
1587 int val1;
1588 tree ret1, ret2;
1590 ret1 = TREE_TYPE (f1);
1591 ret2 = TREE_TYPE (f2);
1593 /* 'volatile' qualifiers on a function's return type used to mean
1594 the function is noreturn. */
1595 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1596 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1597 if (TYPE_VOLATILE (ret1))
1598 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1599 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1600 if (TYPE_VOLATILE (ret2))
1601 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1602 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1603 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1604 if (val == 0)
1605 return 0;
1607 args1 = TYPE_ARG_TYPES (f1);
1608 args2 = TYPE_ARG_TYPES (f2);
1610 if (different_types_p != NULL
1611 && (args1 == 0) != (args2 == 0))
1612 *different_types_p = true;
1614 /* An unspecified parmlist matches any specified parmlist
1615 whose argument types don't need default promotions. */
1617 if (args1 == 0)
1619 if (!self_promoting_args_p (args2))
1620 return 0;
1621 /* If one of these types comes from a non-prototype fn definition,
1622 compare that with the other type's arglist.
1623 If they don't match, ask for a warning (but no error). */
1624 if (TYPE_ACTUAL_ARG_TYPES (f1)
1625 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1626 enum_and_int_p, different_types_p))
1627 val = 2;
1628 return val;
1630 if (args2 == 0)
1632 if (!self_promoting_args_p (args1))
1633 return 0;
1634 if (TYPE_ACTUAL_ARG_TYPES (f2)
1635 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1636 enum_and_int_p, different_types_p))
1637 val = 2;
1638 return val;
1641 /* Both types have argument lists: compare them and propagate results. */
1642 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1643 different_types_p);
1644 return val1 != 1 ? val1 : val;
1647 /* Check two lists of types for compatibility, returning 0 for
1648 incompatible, 1 for compatible, or 2 for compatible with
1649 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1650 comptypes_internal. */
1652 static int
1653 type_lists_compatible_p (const_tree args1, const_tree args2,
1654 bool *enum_and_int_p, bool *different_types_p)
1656 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1657 int val = 1;
1658 int newval = 0;
1660 while (1)
1662 tree a1, mv1, a2, mv2;
1663 if (args1 == 0 && args2 == 0)
1664 return val;
1665 /* If one list is shorter than the other,
1666 they fail to match. */
1667 if (args1 == 0 || args2 == 0)
1668 return 0;
1669 mv1 = a1 = TREE_VALUE (args1);
1670 mv2 = a2 = TREE_VALUE (args2);
1671 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1672 mv1 = TYPE_MAIN_VARIANT (mv1);
1673 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1674 mv2 = TYPE_MAIN_VARIANT (mv2);
1675 /* A null pointer instead of a type
1676 means there is supposed to be an argument
1677 but nothing is specified about what type it has.
1678 So match anything that self-promotes. */
1679 if (different_types_p != NULL
1680 && (a1 == 0) != (a2 == 0))
1681 *different_types_p = true;
1682 if (a1 == 0)
1684 if (c_type_promotes_to (a2) != a2)
1685 return 0;
1687 else if (a2 == 0)
1689 if (c_type_promotes_to (a1) != a1)
1690 return 0;
1692 /* If one of the lists has an error marker, ignore this arg. */
1693 else if (TREE_CODE (a1) == ERROR_MARK
1694 || TREE_CODE (a2) == ERROR_MARK)
1696 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1697 different_types_p)))
1699 if (different_types_p != NULL)
1700 *different_types_p = true;
1701 /* Allow wait (union {union wait *u; int *i} *)
1702 and wait (union wait *) to be compatible. */
1703 if (TREE_CODE (a1) == UNION_TYPE
1704 && (TYPE_NAME (a1) == 0
1705 || TYPE_TRANSPARENT_AGGR (a1))
1706 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1707 && tree_int_cst_equal (TYPE_SIZE (a1),
1708 TYPE_SIZE (a2)))
1710 tree memb;
1711 for (memb = TYPE_FIELDS (a1);
1712 memb; memb = DECL_CHAIN (memb))
1714 tree mv3 = TREE_TYPE (memb);
1715 if (mv3 && mv3 != error_mark_node
1716 && TREE_CODE (mv3) != ARRAY_TYPE)
1717 mv3 = TYPE_MAIN_VARIANT (mv3);
1718 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1719 different_types_p))
1720 break;
1722 if (memb == 0)
1723 return 0;
1725 else if (TREE_CODE (a2) == UNION_TYPE
1726 && (TYPE_NAME (a2) == 0
1727 || TYPE_TRANSPARENT_AGGR (a2))
1728 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1729 && tree_int_cst_equal (TYPE_SIZE (a2),
1730 TYPE_SIZE (a1)))
1732 tree memb;
1733 for (memb = TYPE_FIELDS (a2);
1734 memb; memb = DECL_CHAIN (memb))
1736 tree mv3 = TREE_TYPE (memb);
1737 if (mv3 && mv3 != error_mark_node
1738 && TREE_CODE (mv3) != ARRAY_TYPE)
1739 mv3 = TYPE_MAIN_VARIANT (mv3);
1740 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1741 different_types_p))
1742 break;
1744 if (memb == 0)
1745 return 0;
1747 else
1748 return 0;
1751 /* comptypes said ok, but record if it said to warn. */
1752 if (newval > val)
1753 val = newval;
1755 args1 = TREE_CHAIN (args1);
1756 args2 = TREE_CHAIN (args2);
1760 /* Compute the size to increment a pointer by. */
1762 static tree
1763 c_size_in_bytes (const_tree type)
1765 enum tree_code code = TREE_CODE (type);
1767 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1768 return size_one_node;
1770 if (!COMPLETE_OR_VOID_TYPE_P (type))
1772 error ("arithmetic on pointer to an incomplete type");
1773 return size_one_node;
1776 /* Convert in case a char is more than one unit. */
1777 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1778 size_int (TYPE_PRECISION (char_type_node)
1779 / BITS_PER_UNIT));
1782 /* Return either DECL or its known constant value (if it has one). */
1784 tree
1785 decl_constant_value (tree decl)
1787 if (/* Don't change a variable array bound or initial value to a constant
1788 in a place where a variable is invalid. Note that DECL_INITIAL
1789 isn't valid for a PARM_DECL. */
1790 current_function_decl != 0
1791 && TREE_CODE (decl) != PARM_DECL
1792 && !TREE_THIS_VOLATILE (decl)
1793 && TREE_READONLY (decl)
1794 && DECL_INITIAL (decl) != 0
1795 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1796 /* This is invalid if initial value is not constant.
1797 If it has either a function call, a memory reference,
1798 or a variable, then re-evaluating it could give different results. */
1799 && TREE_CONSTANT (DECL_INITIAL (decl))
1800 /* Check for cases where this is sub-optimal, even though valid. */
1801 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1802 return DECL_INITIAL (decl);
1803 return decl;
1806 /* Convert the array expression EXP to a pointer. */
1807 static tree
1808 array_to_pointer_conversion (location_t loc, tree exp)
1810 tree orig_exp = exp;
1811 tree type = TREE_TYPE (exp);
1812 tree adr;
1813 tree restype = TREE_TYPE (type);
1814 tree ptrtype;
1816 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1818 STRIP_TYPE_NOPS (exp);
1820 if (TREE_NO_WARNING (orig_exp))
1821 TREE_NO_WARNING (exp) = 1;
1823 ptrtype = build_pointer_type (restype);
1825 if (TREE_CODE (exp) == INDIRECT_REF)
1826 return convert (ptrtype, TREE_OPERAND (exp, 0));
1828 /* In C++ array compound literals are temporary objects unless they are
1829 const or appear in namespace scope, so they are destroyed too soon
1830 to use them for much of anything (c++/53220). */
1831 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1833 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1834 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1835 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1836 "converting an array compound literal to a pointer "
1837 "is ill-formed in C++");
1840 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1841 return convert (ptrtype, adr);
1844 /* Convert the function expression EXP to a pointer. */
1845 static tree
1846 function_to_pointer_conversion (location_t loc, tree exp)
1848 tree orig_exp = exp;
1850 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1852 STRIP_TYPE_NOPS (exp);
1854 if (TREE_NO_WARNING (orig_exp))
1855 TREE_NO_WARNING (exp) = 1;
1857 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1860 /* Mark EXP as read, not just set, for set but not used -Wunused
1861 warning purposes. */
1863 void
1864 mark_exp_read (tree exp)
1866 switch (TREE_CODE (exp))
1868 case VAR_DECL:
1869 case PARM_DECL:
1870 DECL_READ_P (exp) = 1;
1871 break;
1872 case ARRAY_REF:
1873 case COMPONENT_REF:
1874 case MODIFY_EXPR:
1875 case REALPART_EXPR:
1876 case IMAGPART_EXPR:
1877 CASE_CONVERT:
1878 case ADDR_EXPR:
1879 mark_exp_read (TREE_OPERAND (exp, 0));
1880 break;
1881 case COMPOUND_EXPR:
1882 case C_MAYBE_CONST_EXPR:
1883 mark_exp_read (TREE_OPERAND (exp, 1));
1884 break;
1885 default:
1886 break;
1890 /* Perform the default conversion of arrays and functions to pointers.
1891 Return the result of converting EXP. For any other expression, just
1892 return EXP.
1894 LOC is the location of the expression. */
1896 struct c_expr
1897 default_function_array_conversion (location_t loc, struct c_expr exp)
1899 tree orig_exp = exp.value;
1900 tree type = TREE_TYPE (exp.value);
1901 enum tree_code code = TREE_CODE (type);
1903 switch (code)
1905 case ARRAY_TYPE:
1907 bool not_lvalue = false;
1908 bool lvalue_array_p;
1910 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1911 || CONVERT_EXPR_P (exp.value))
1912 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1914 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1915 not_lvalue = true;
1916 exp.value = TREE_OPERAND (exp.value, 0);
1919 if (TREE_NO_WARNING (orig_exp))
1920 TREE_NO_WARNING (exp.value) = 1;
1922 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1923 if (!flag_isoc99 && !lvalue_array_p)
1925 /* Before C99, non-lvalue arrays do not decay to pointers.
1926 Normally, using such an array would be invalid; but it can
1927 be used correctly inside sizeof or as a statement expression.
1928 Thus, do not give an error here; an error will result later. */
1929 return exp;
1932 exp.value = array_to_pointer_conversion (loc, exp.value);
1934 break;
1935 case FUNCTION_TYPE:
1936 exp.value = function_to_pointer_conversion (loc, exp.value);
1937 break;
1938 default:
1939 break;
1942 return exp;
1945 struct c_expr
1946 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1948 mark_exp_read (exp.value);
1949 return default_function_array_conversion (loc, exp);
1952 /* EXP is an expression of integer type. Apply the integer promotions
1953 to it and return the promoted value. */
1955 tree
1956 perform_integral_promotions (tree exp)
1958 tree type = TREE_TYPE (exp);
1959 enum tree_code code = TREE_CODE (type);
1961 gcc_assert (INTEGRAL_TYPE_P (type));
1963 /* Normally convert enums to int,
1964 but convert wide enums to something wider. */
1965 if (code == ENUMERAL_TYPE)
1967 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1968 TYPE_PRECISION (integer_type_node)),
1969 ((TYPE_PRECISION (type)
1970 >= TYPE_PRECISION (integer_type_node))
1971 && TYPE_UNSIGNED (type)));
1973 return convert (type, exp);
1976 /* ??? This should no longer be needed now bit-fields have their
1977 proper types. */
1978 if (TREE_CODE (exp) == COMPONENT_REF
1979 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1980 /* If it's thinner than an int, promote it like a
1981 c_promoting_integer_type_p, otherwise leave it alone. */
1982 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1983 TYPE_PRECISION (integer_type_node)))
1984 return convert (integer_type_node, exp);
1986 if (c_promoting_integer_type_p (type))
1988 /* Preserve unsignedness if not really getting any wider. */
1989 if (TYPE_UNSIGNED (type)
1990 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1991 return convert (unsigned_type_node, exp);
1993 return convert (integer_type_node, exp);
1996 return exp;
2000 /* Perform default promotions for C data used in expressions.
2001 Enumeral types or short or char are converted to int.
2002 In addition, manifest constants symbols are replaced by their values. */
2004 tree
2005 default_conversion (tree exp)
2007 tree orig_exp;
2008 tree type = TREE_TYPE (exp);
2009 enum tree_code code = TREE_CODE (type);
2010 tree promoted_type;
2012 mark_exp_read (exp);
2014 /* Functions and arrays have been converted during parsing. */
2015 gcc_assert (code != FUNCTION_TYPE);
2017 if (code == ARRAY_TYPE && upc_shared_type_p (type))
2018 return array_to_pointer_conversion (input_location, exp);
2020 if (code == ARRAY_TYPE)
2021 return exp;
2023 /* Constants can be used directly unless they're not loadable. */
2024 if (TREE_CODE (exp) == CONST_DECL)
2025 exp = DECL_INITIAL (exp);
2027 /* Strip no-op conversions. */
2028 orig_exp = exp;
2029 STRIP_TYPE_NOPS (exp);
2031 if (TREE_NO_WARNING (orig_exp))
2032 TREE_NO_WARNING (exp) = 1;
2034 if (code == VOID_TYPE)
2036 error ("void value not ignored as it ought to be");
2037 return error_mark_node;
2040 exp = require_complete_type (exp);
2041 if (exp == error_mark_node)
2042 return error_mark_node;
2044 promoted_type = targetm.promoted_type (type);
2045 if (promoted_type)
2046 return convert (promoted_type, exp);
2048 if (INTEGRAL_TYPE_P (type))
2049 return perform_integral_promotions (exp);
2051 return exp;
2054 /* Look up COMPONENT in a structure or union TYPE.
2056 If the component name is not found, returns NULL_TREE. Otherwise,
2057 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2058 stepping down the chain to the component, which is in the last
2059 TREE_VALUE of the list. Normally the list is of length one, but if
2060 the component is embedded within (nested) anonymous structures or
2061 unions, the list steps down the chain to the component. */
2063 static tree
2064 lookup_field (tree type, tree component)
2066 tree field;
2068 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2069 to the field elements. Use a binary search on this array to quickly
2070 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2071 will always be set for structures which have many elements. */
2073 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2075 int bot, top, half;
2076 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2078 field = TYPE_FIELDS (type);
2079 bot = 0;
2080 top = TYPE_LANG_SPECIFIC (type)->s->len;
2081 while (top - bot > 1)
2083 half = (top - bot + 1) >> 1;
2084 field = field_array[bot+half];
2086 if (DECL_NAME (field) == NULL_TREE)
2088 /* Step through all anon unions in linear fashion. */
2089 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2091 field = field_array[bot++];
2092 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2093 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2095 tree anon = lookup_field (TREE_TYPE (field), component);
2097 if (anon)
2098 return tree_cons (NULL_TREE, field, anon);
2100 /* The Plan 9 compiler permits referring
2101 directly to an anonymous struct/union field
2102 using a typedef name. */
2103 if (flag_plan9_extensions
2104 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2105 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2106 == TYPE_DECL)
2107 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2108 == component))
2109 break;
2113 /* Entire record is only anon unions. */
2114 if (bot > top)
2115 return NULL_TREE;
2117 /* Restart the binary search, with new lower bound. */
2118 continue;
2121 if (DECL_NAME (field) == component)
2122 break;
2123 if (DECL_NAME (field) < component)
2124 bot += half;
2125 else
2126 top = bot + half;
2129 if (DECL_NAME (field_array[bot]) == component)
2130 field = field_array[bot];
2131 else if (DECL_NAME (field) != component)
2132 return NULL_TREE;
2134 else
2136 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2138 if (DECL_NAME (field) == NULL_TREE
2139 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2140 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2142 tree anon = lookup_field (TREE_TYPE (field), component);
2144 if (anon)
2145 return tree_cons (NULL_TREE, field, anon);
2147 /* The Plan 9 compiler permits referring directly to an
2148 anonymous struct/union field using a typedef
2149 name. */
2150 if (flag_plan9_extensions
2151 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2152 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2153 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2154 == component))
2155 break;
2158 if (DECL_NAME (field) == component)
2159 break;
2162 if (field == NULL_TREE)
2163 return NULL_TREE;
2166 return tree_cons (NULL_TREE, field, NULL_TREE);
2169 /* Make an expression to refer to the COMPONENT field of structure or
2170 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2171 location of the COMPONENT_REF. */
2173 tree
2174 build_component_ref (location_t loc, tree datum, tree component)
2176 tree type = TREE_TYPE (datum);
2177 enum tree_code code = TREE_CODE (type);
2178 tree field = NULL;
2179 tree ref;
2180 bool datum_lvalue = lvalue_p (datum);
2182 if (!objc_is_public (datum, component))
2183 return error_mark_node;
2185 /* Detect Objective-C property syntax object.property. */
2186 if (c_dialect_objc ()
2187 && (ref = objc_maybe_build_component_ref (datum, component)))
2188 return ref;
2190 /* See if there is a field or component with name COMPONENT. */
2192 if (code == RECORD_TYPE || code == UNION_TYPE)
2194 if (!COMPLETE_TYPE_P (type))
2196 c_incomplete_type_error (NULL_TREE, type);
2197 return error_mark_node;
2200 field = lookup_field (type, component);
2202 if (!field)
2204 error_at (loc, "%qT has no member named %qE", type, component);
2205 return error_mark_node;
2207 gcc_assert (!TREE_SHARED (field));
2209 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2210 This might be better solved in future the way the C++ front
2211 end does it - by giving the anonymous entities each a
2212 separate name and type, and then have build_component_ref
2213 recursively call itself. We can't do that here. */
2216 tree subdatum = TREE_VALUE (field);
2217 tree sub_elem_type = strip_array_types (TREE_TYPE (subdatum));
2218 tree upc_block_factor = NULL_TREE;
2219 int quals;
2220 tree subtype;
2221 bool use_datum_quals;
2223 if (TREE_TYPE (subdatum) == error_mark_node)
2224 return error_mark_node;
2226 /* If this is an rvalue, it does not have qualifiers in C
2227 standard terms and we must avoid propagating such
2228 qualifiers down to a non-lvalue array that is then
2229 converted to a pointer. */
2230 use_datum_quals = (datum_lvalue
2231 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2233 quals = TYPE_QUALS (sub_elem_type);
2234 if (use_datum_quals)
2235 quals |= TYPE_QUALS (TREE_TYPE (datum));
2236 /* All references to UPC shared struct components
2237 are defined to have an indefinite (zero) blocking factor. */
2238 if (quals & TYPE_QUAL_SHARED)
2239 upc_block_factor = size_zero_node;
2240 subtype = c_build_qualified_type_1 (TREE_TYPE (subdatum),
2241 quals, upc_block_factor);
2243 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2244 NULL_TREE);
2245 SET_EXPR_LOCATION (ref, loc);
2246 if (TREE_READONLY (subdatum)
2247 || (use_datum_quals && TREE_READONLY (datum)))
2248 TREE_READONLY (ref) = 1;
2249 if (TREE_THIS_VOLATILE (subdatum)
2250 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2251 TREE_THIS_VOLATILE (ref) = 1;
2252 if (TREE_SHARED (datum))
2253 TREE_SHARED (ref) = 1;
2255 if (TREE_DEPRECATED (subdatum))
2256 warn_deprecated_use (subdatum, NULL_TREE);
2258 datum = ref;
2260 field = TREE_CHAIN (field);
2262 while (field);
2264 return ref;
2266 else if (code != ERROR_MARK)
2267 error_at (loc,
2268 "request for member %qE in something not a structure or union",
2269 component);
2271 return error_mark_node;
2274 /* Given an expression PTR for a pointer, return an expression
2275 for the value pointed to.
2276 ERRORSTRING is the name of the operator to appear in error messages.
2278 LOC is the location to use for the generated tree. */
2280 tree
2281 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2283 tree pointer = default_conversion (ptr);
2284 tree type = TREE_TYPE (pointer);
2285 tree ref;
2287 if (TREE_CODE (type) == POINTER_TYPE)
2289 if (CONVERT_EXPR_P (pointer)
2290 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2292 /* If a warning is issued, mark it to avoid duplicates from
2293 the backend. This only needs to be done at
2294 warn_strict_aliasing > 2. */
2295 if (warn_strict_aliasing > 2)
2296 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2297 type, TREE_OPERAND (pointer, 0)))
2298 TREE_NO_WARNING (pointer) = 1;
2301 if (TREE_CODE (pointer) == ADDR_EXPR
2302 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2303 == TREE_TYPE (type)))
2305 ref = TREE_OPERAND (pointer, 0);
2306 protected_set_expr_location (ref, loc);
2307 return ref;
2309 else
2311 tree t = TREE_TYPE (type);
2313 ref = build1 (INDIRECT_REF, t, pointer);
2315 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2317 error_at (loc, "dereferencing pointer to incomplete type");
2318 return error_mark_node;
2320 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2321 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2323 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2324 so that we get the proper error message if the result is used
2325 to assign to. Also, &* is supposed to be a no-op.
2326 And ANSI C seems to specify that the type of the result
2327 should be the const type. */
2328 /* A de-reference of a pointer to const is not a const. It is valid
2329 to change it via some other pointer. */
2330 TREE_READONLY (ref) = TYPE_READONLY (t);
2331 TREE_SIDE_EFFECTS (ref)
2332 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2333 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2334 TREE_SHARED (ref) = upc_shared_type_p (t);
2335 protected_set_expr_location (ref, loc);
2336 return ref;
2339 else if (TREE_CODE (pointer) != ERROR_MARK)
2340 invalid_indirection_error (loc, type, errstring);
2342 return error_mark_node;
2345 /* This handles expressions of the form "a[i]", which denotes
2346 an array reference.
2348 This is logically equivalent in C to *(a+i), but we may do it differently.
2349 If A is a variable or a member, we generate a primitive ARRAY_REF.
2350 This avoids forcing the array out of registers, and can work on
2351 arrays that are not lvalues (for example, members of structures returned
2352 by functions).
2354 For vector types, allow vector[i] but not i[vector], and create
2355 *(((type*)&vectortype) + i) for the expression.
2357 LOC is the location to use for the returned expression. */
2359 tree
2360 build_array_ref (location_t loc, tree array, tree index)
2362 tree ret;
2363 bool swapped = false;
2364 if (TREE_TYPE (array) == error_mark_node
2365 || TREE_TYPE (index) == error_mark_node)
2366 return error_mark_node;
2368 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2369 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2370 /* Allow vector[index] but not index[vector]. */
2371 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2373 tree temp;
2374 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2375 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2377 error_at (loc,
2378 "subscripted value is neither array nor pointer nor vector");
2380 return error_mark_node;
2382 temp = array;
2383 array = index;
2384 index = temp;
2385 swapped = true;
2388 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2390 error_at (loc, "array subscript is not an integer");
2391 return error_mark_node;
2394 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2396 error_at (loc, "subscripted value is pointer to function");
2397 return error_mark_node;
2400 /* ??? Existing practice has been to warn only when the char
2401 index is syntactically the index, not for char[array]. */
2402 if (!swapped)
2403 warn_array_subscript_with_type_char (index);
2405 /* Apply default promotions *after* noticing character types. */
2406 index = default_conversion (index);
2408 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2410 convert_vector_to_pointer_for_subscript (loc, &array, index);
2411 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2412 && !upc_shared_type_p (TREE_TYPE (array)))
2414 tree rval, type;
2416 /* An array that is indexed by a non-constant
2417 cannot be stored in a register; we must be able to do
2418 address arithmetic on its address.
2419 Likewise an array of elements of variable size. */
2420 if (TREE_CODE (index) != INTEGER_CST
2421 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2422 && TREE_CODE (TYPE_SIZE (TREE_TYPE (
2423 TREE_TYPE (array)))) != INTEGER_CST))
2425 if (!c_mark_addressable (array))
2426 return error_mark_node;
2428 /* An array that is indexed by a constant value which is not within
2429 the array bounds cannot be stored in a register either; because we
2430 would get a crash in store_bit_field/extract_bit_field when trying
2431 to access a non-existent part of the register. */
2432 if (TREE_CODE (index) == INTEGER_CST
2433 && TYPE_DOMAIN (TREE_TYPE (array))
2434 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2436 if (!c_mark_addressable (array))
2437 return error_mark_node;
2440 if (pedantic)
2442 tree foo = array;
2443 while (TREE_CODE (foo) == COMPONENT_REF)
2444 foo = TREE_OPERAND (foo, 0);
2445 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2446 pedwarn (loc, OPT_Wpedantic,
2447 "ISO C forbids subscripting %<register%> array");
2448 else if (!flag_isoc99 && !lvalue_p (foo))
2449 pedwarn (loc, OPT_Wpedantic,
2450 "ISO C90 forbids subscripting non-lvalue array");
2453 type = TREE_TYPE (TREE_TYPE (array));
2454 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2455 /* Array ref is const/volatile if the array elements are
2456 or if the array is. */
2457 TREE_READONLY (rval)
2458 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2459 | TREE_READONLY (array));
2460 TREE_SIDE_EFFECTS (rval)
2461 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2462 | TREE_SIDE_EFFECTS (array));
2463 TREE_THIS_VOLATILE (rval)
2464 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2465 /* This was added by rms on 16 Nov 91.
2466 It fixes vol struct foo *a; a->elts[1]
2467 in an inline function.
2468 Hope it doesn't break something else. */
2469 | TREE_THIS_VOLATILE (array));
2470 ret = require_complete_type (rval);
2471 protected_set_expr_location (ret, loc);
2472 return ret;
2474 else
2476 tree ar = default_conversion (array);
2478 if (ar == error_mark_node)
2479 return ar;
2481 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2482 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2484 return build_indirect_ref
2485 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2486 RO_ARRAY_INDEXING);
2490 /* Build an external reference to identifier ID. FUN indicates
2491 whether this will be used for a function call. LOC is the source
2492 location of the identifier. This sets *TYPE to the type of the
2493 identifier, which is not the same as the type of the returned value
2494 for CONST_DECLs defined as enum constants. If the type of the
2495 identifier is not available, *TYPE is set to NULL. */
2496 tree
2497 build_external_ref (location_t loc, tree id, int fun, tree *type)
2499 tree ref;
2500 tree decl = lookup_name (id);
2502 /* In Objective-C, an instance variable (ivar) may be preferred to
2503 whatever lookup_name() found. */
2504 decl = objc_lookup_ivar (decl, id);
2506 *type = NULL;
2507 if (decl && decl != error_mark_node)
2509 ref = decl;
2510 *type = TREE_TYPE (ref);
2512 else if (fun)
2513 /* Implicit function declaration. */
2514 ref = implicitly_declare (loc, id);
2515 else if (decl == error_mark_node)
2516 /* Don't complain about something that's already been
2517 complained about. */
2518 return error_mark_node;
2519 else
2521 undeclared_variable (loc, id);
2522 return error_mark_node;
2525 if (TREE_TYPE (ref) == error_mark_node)
2526 return error_mark_node;
2528 if (TREE_DEPRECATED (ref))
2529 warn_deprecated_use (ref, NULL_TREE);
2531 /* Recursive call does not count as usage. */
2532 if (ref != current_function_decl)
2534 TREE_USED (ref) = 1;
2537 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2539 if (!in_sizeof && !in_typeof)
2540 C_DECL_USED (ref) = 1;
2541 else if (DECL_INITIAL (ref) == 0
2542 && DECL_EXTERNAL (ref)
2543 && !TREE_PUBLIC (ref))
2544 record_maybe_used_decl (ref);
2547 if (TREE_CODE (ref) == CONST_DECL)
2549 used_types_insert (TREE_TYPE (ref));
2551 if (warn_cxx_compat
2552 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2553 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2555 warning_at (loc, OPT_Wc___compat,
2556 ("enum constant defined in struct or union "
2557 "is not visible in C++"));
2558 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2561 ref = DECL_INITIAL (ref);
2562 TREE_CONSTANT (ref) = 1;
2564 else if (current_function_decl != 0
2565 && !DECL_FILE_SCOPE_P (current_function_decl)
2566 && (TREE_CODE (ref) == VAR_DECL
2567 || TREE_CODE (ref) == PARM_DECL
2568 || TREE_CODE (ref) == FUNCTION_DECL))
2570 tree context = decl_function_context (ref);
2572 if (context != 0 && context != current_function_decl)
2573 DECL_NONLOCAL (ref) = 1;
2575 /* C99 6.7.4p3: An inline definition of a function with external
2576 linkage ... shall not contain a reference to an identifier with
2577 internal linkage. */
2578 else if (current_function_decl != 0
2579 && DECL_DECLARED_INLINE_P (current_function_decl)
2580 && DECL_EXTERNAL (current_function_decl)
2581 && VAR_OR_FUNCTION_DECL_P (ref)
2582 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2583 && ! TREE_PUBLIC (ref)
2584 && DECL_CONTEXT (ref) != current_function_decl)
2585 record_inline_static (loc, current_function_decl, ref,
2586 csi_internal);
2588 return ref;
2591 /* Record details of decls possibly used inside sizeof or typeof. */
2592 struct maybe_used_decl
2594 /* The decl. */
2595 tree decl;
2596 /* The level seen at (in_sizeof + in_typeof). */
2597 int level;
2598 /* The next one at this level or above, or NULL. */
2599 struct maybe_used_decl *next;
2602 static struct maybe_used_decl *maybe_used_decls;
2604 /* Record that DECL, an undefined static function reference seen
2605 inside sizeof or typeof, might be used if the operand of sizeof is
2606 a VLA type or the operand of typeof is a variably modified
2607 type. */
2609 static void
2610 record_maybe_used_decl (tree decl)
2612 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2613 t->decl = decl;
2614 t->level = in_sizeof + in_typeof;
2615 t->next = maybe_used_decls;
2616 maybe_used_decls = t;
2619 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2620 USED is false, just discard them. If it is true, mark them used
2621 (if no longer inside sizeof or typeof) or move them to the next
2622 level up (if still inside sizeof or typeof). */
2624 void
2625 pop_maybe_used (bool used)
2627 struct maybe_used_decl *p = maybe_used_decls;
2628 int cur_level = in_sizeof + in_typeof;
2629 while (p && p->level > cur_level)
2631 if (used)
2633 if (cur_level == 0)
2634 C_DECL_USED (p->decl) = 1;
2635 else
2636 p->level = cur_level;
2638 p = p->next;
2640 if (!used || cur_level == 0)
2641 maybe_used_decls = p;
2644 /* Return the result of sizeof applied to EXPR. */
2646 struct c_expr
2647 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2649 struct c_expr ret;
2650 if (expr.value == error_mark_node)
2652 ret.value = error_mark_node;
2653 ret.original_code = ERROR_MARK;
2654 ret.original_type = NULL;
2655 pop_maybe_used (false);
2657 else
2659 bool expr_const_operands = true;
2660 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2661 &expr_const_operands);
2662 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2663 c_last_sizeof_arg = expr.value;
2664 ret.original_code = SIZEOF_EXPR;
2665 ret.original_type = NULL;
2666 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2668 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2669 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2670 folded_expr, ret.value);
2671 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2672 SET_EXPR_LOCATION (ret.value, loc);
2674 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2676 return ret;
2679 /* Return the result of sizeof applied to T, a structure for the type
2680 name passed to sizeof (rather than the type itself). LOC is the
2681 location of the original expression. */
2683 struct c_expr
2684 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2686 tree type;
2687 struct c_expr ret;
2688 tree type_expr = NULL_TREE;
2689 bool type_expr_const = true;
2690 type = groktypename (t, &type_expr, &type_expr_const);
2691 ret.value = c_sizeof (loc, type);
2692 c_last_sizeof_arg = type;
2693 ret.original_code = SIZEOF_EXPR;
2694 ret.original_type = NULL;
2695 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2696 && c_vla_type_p (type))
2698 /* If the type is a [*] array, it is a VLA but is represented as
2699 having a size of zero. In such a case we must ensure that
2700 the result of sizeof does not get folded to a constant by
2701 c_fully_fold, because if the size is evaluated the result is
2702 not constant and so constraints on zero or negative size
2703 arrays must not be applied when this sizeof call is inside
2704 another array declarator. */
2705 if (!type_expr)
2706 type_expr = integer_zero_node;
2707 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2708 type_expr, ret.value);
2709 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2711 pop_maybe_used (type != error_mark_node
2712 ? C_TYPE_VARIABLE_SIZE (type) : false);
2713 return ret;
2716 /* Build a function call to function FUNCTION with parameters PARAMS.
2717 The function call is at LOC.
2718 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2719 TREE_VALUE of each node is a parameter-expression.
2720 FUNCTION's data type may be a function type or a pointer-to-function. */
2722 tree
2723 build_function_call (location_t loc, tree function, tree params)
2725 vec<tree, va_gc> *v;
2726 tree ret;
2728 vec_alloc (v, list_length (params));
2729 for (; params; params = TREE_CHAIN (params))
2730 v->quick_push (TREE_VALUE (params));
2731 ret = build_function_call_vec (loc, function, v, NULL);
2732 vec_free (v);
2733 return ret;
2736 /* Give a note about the location of the declaration of DECL. */
2738 static void inform_declaration (tree decl)
2740 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2741 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2744 /* Build a function call to function FUNCTION with parameters PARAMS.
2745 ORIGTYPES, if not NULL, is a vector of types; each element is
2746 either NULL or the original type of the corresponding element in
2747 PARAMS. The original type may differ from TREE_TYPE of the
2748 parameter for enums. FUNCTION's data type may be a function type
2749 or pointer-to-function. This function changes the elements of
2750 PARAMS. */
2752 tree
2753 build_function_call_vec (location_t loc, tree function,
2754 vec<tree, va_gc> *params,
2755 vec<tree, va_gc> *origtypes)
2757 tree fntype, fundecl = 0;
2758 tree name = NULL_TREE, result;
2759 tree tem;
2760 int nargs;
2761 tree *argarray;
2764 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2765 STRIP_TYPE_NOPS (function);
2767 /* Convert anything with function type to a pointer-to-function. */
2768 if (TREE_CODE (function) == FUNCTION_DECL)
2770 /* Implement type-directed function overloading for builtins.
2771 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2772 handle all the type checking. The result is a complete expression
2773 that implements this function call. */
2774 tem = resolve_overloaded_builtin (loc, function, params);
2775 if (tem)
2776 return tem;
2778 name = DECL_NAME (function);
2780 if (flag_tm)
2781 tm_malloc_replacement (function);
2782 fundecl = function;
2783 /* Atomic functions have type checking/casting already done. They are
2784 often rewritten and don't match the original parameter list. */
2785 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2786 origtypes = NULL;
2788 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2789 function = function_to_pointer_conversion (loc, function);
2791 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2792 expressions, like those used for ObjC messenger dispatches. */
2793 if (params && !params->is_empty ())
2794 function = objc_rewrite_function_call (function, (*params)[0]);
2796 function = c_fully_fold (function, false, NULL);
2798 fntype = TREE_TYPE (function);
2800 if (TREE_CODE (fntype) == ERROR_MARK)
2801 return error_mark_node;
2803 if (!(TREE_CODE (fntype) == POINTER_TYPE
2804 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2806 if (!flag_diagnostics_show_caret)
2807 error_at (loc,
2808 "called object %qE is not a function or function pointer",
2809 function);
2810 else if (DECL_P (function))
2812 error_at (loc,
2813 "called object %qD is not a function or function pointer",
2814 function);
2815 inform_declaration (function);
2817 else
2818 error_at (loc,
2819 "called object is not a function or function pointer");
2820 return error_mark_node;
2823 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2824 current_function_returns_abnormally = 1;
2826 /* fntype now gets the type of function pointed to. */
2827 fntype = TREE_TYPE (fntype);
2829 /* Convert the parameters to the types declared in the
2830 function prototype, or apply default promotions. */
2832 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2833 function, fundecl);
2834 if (nargs < 0)
2835 return error_mark_node;
2837 /* Check that the function is called through a compatible prototype.
2838 If it is not, replace the call by a trap, wrapped up in a compound
2839 expression if necessary. This has the nice side-effect to prevent
2840 the tree-inliner from generating invalid assignment trees which may
2841 blow up in the RTL expander later. */
2842 if (CONVERT_EXPR_P (function)
2843 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2844 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2845 && !comptypes (fntype, TREE_TYPE (tem)))
2847 tree return_type = TREE_TYPE (fntype);
2848 tree trap = build_function_call (loc,
2849 builtin_decl_explicit (BUILT_IN_TRAP),
2850 NULL_TREE);
2851 int i;
2853 /* This situation leads to run-time undefined behavior. We can't,
2854 therefore, simply error unless we can prove that all possible
2855 executions of the program must execute the code. */
2856 if (warning_at (loc, 0, "function called through a non-compatible type"))
2857 /* We can, however, treat "undefined" any way we please.
2858 Call abort to encourage the user to fix the program. */
2859 inform (loc, "if this code is reached, the program will abort");
2860 /* Before the abort, allow the function arguments to exit or
2861 call longjmp. */
2862 for (i = 0; i < nargs; i++)
2863 trap = build2 (COMPOUND_EXPR, void_type_node, (*params)[i], trap);
2865 if (VOID_TYPE_P (return_type))
2867 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2868 pedwarn (loc, 0,
2869 "function with qualified void return type called");
2870 return trap;
2872 else
2874 tree rhs;
2876 if (AGGREGATE_TYPE_P (return_type))
2877 rhs = build_compound_literal (loc, return_type,
2878 build_constructor (return_type,
2879 NULL),
2880 false);
2881 else
2882 rhs = build_zero_cst (return_type);
2884 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2885 trap, rhs));
2889 argarray = vec_safe_address (params);
2891 /* Check that arguments to builtin functions match the expectations. */
2892 if (fundecl
2893 && DECL_BUILT_IN (fundecl)
2894 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2895 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2896 return error_mark_node;
2898 /* Check that the arguments to the function are valid. */
2899 check_function_arguments (fntype, nargs, argarray);
2901 if (name != NULL_TREE
2902 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2904 if (require_constant_value)
2905 result =
2906 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2907 function, nargs, argarray);
2908 else
2909 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2910 function, nargs, argarray);
2911 if (TREE_CODE (result) == NOP_EXPR
2912 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2913 STRIP_TYPE_NOPS (result);
2915 else
2916 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2917 function, nargs, argarray);
2919 if (VOID_TYPE_P (TREE_TYPE (result)))
2921 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2922 pedwarn (loc, 0,
2923 "function with qualified void return type called");
2924 return result;
2926 return require_complete_type (result);
2929 /* Convert the argument expressions in the vector VALUES
2930 to the types in the list TYPELIST.
2932 If TYPELIST is exhausted, or when an element has NULL as its type,
2933 perform the default conversions.
2935 ORIGTYPES is the original types of the expressions in VALUES. This
2936 holds the type of enum values which have been converted to integral
2937 types. It may be NULL.
2939 FUNCTION is a tree for the called function. It is used only for
2940 error messages, where it is formatted with %qE.
2942 This is also where warnings about wrong number of args are generated.
2944 Returns the actual number of arguments processed (which may be less
2945 than the length of VALUES in some error situations), or -1 on
2946 failure. */
2948 static int
2949 convert_arguments (tree typelist, vec<tree, va_gc> *values,
2950 vec<tree, va_gc> *origtypes, tree function, tree fundecl)
2952 tree typetail, val;
2953 unsigned int parmnum;
2954 bool error_args = false;
2955 const bool type_generic = fundecl
2956 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2957 bool type_generic_remove_excess_precision = false;
2958 tree selector;
2960 /* Change pointer to function to the function itself for
2961 diagnostics. */
2962 if (TREE_CODE (function) == ADDR_EXPR
2963 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2964 function = TREE_OPERAND (function, 0);
2966 /* Handle an ObjC selector specially for diagnostics. */
2967 selector = objc_message_selector ();
2969 /* For type-generic built-in functions, determine whether excess
2970 precision should be removed (classification) or not
2971 (comparison). */
2972 if (type_generic
2973 && DECL_BUILT_IN (fundecl)
2974 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2976 switch (DECL_FUNCTION_CODE (fundecl))
2978 case BUILT_IN_ISFINITE:
2979 case BUILT_IN_ISINF:
2980 case BUILT_IN_ISINF_SIGN:
2981 case BUILT_IN_ISNAN:
2982 case BUILT_IN_ISNORMAL:
2983 case BUILT_IN_FPCLASSIFY:
2984 type_generic_remove_excess_precision = true;
2985 break;
2987 default:
2988 type_generic_remove_excess_precision = false;
2989 break;
2993 /* Scan the given expressions and types, producing individual
2994 converted arguments. */
2996 for (typetail = typelist, parmnum = 0;
2997 values && values->iterate (parmnum, &val);
2998 ++parmnum)
3000 tree type = typetail ? TREE_VALUE (typetail) : 0;
3001 tree valtype = TREE_TYPE (val);
3002 tree rname = function;
3003 int argnum = parmnum + 1;
3004 const char *invalid_func_diag;
3005 bool excess_precision = false;
3006 bool npc;
3007 tree parmval;
3009 if (type == void_type_node)
3011 if (selector)
3012 error_at (input_location,
3013 "too many arguments to method %qE", selector);
3014 else
3015 error_at (input_location,
3016 "too many arguments to function %qE", function);
3017 inform_declaration (fundecl);
3018 return parmnum;
3021 if (selector && argnum > 2)
3023 rname = selector;
3024 argnum -= 2;
3027 npc = null_pointer_constant_p (val);
3029 /* If there is excess precision and a prototype, convert once to
3030 the required type rather than converting via the semantic
3031 type. Likewise without a prototype a float value represented
3032 as long double should be converted once to double. But for
3033 type-generic classification functions excess precision must
3034 be removed here. */
3035 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3036 && (type || !type_generic || !type_generic_remove_excess_precision))
3038 val = TREE_OPERAND (val, 0);
3039 excess_precision = true;
3041 val = c_fully_fold (val, false, NULL);
3042 STRIP_TYPE_NOPS (val);
3044 val = require_complete_type (val);
3046 if (type != 0)
3048 /* Formal parm type is specified by a function prototype. */
3050 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3052 error ("type of formal parameter %d is incomplete", parmnum + 1);
3053 parmval = val;
3055 else
3057 tree origtype;
3059 /* Optionally warn about conversions that
3060 differ from the default conversions. */
3061 if (warn_traditional_conversion || warn_traditional)
3063 unsigned int formal_prec = TYPE_PRECISION (type);
3065 if (INTEGRAL_TYPE_P (type)
3066 && TREE_CODE (valtype) == REAL_TYPE)
3067 warning (0, "passing argument %d of %qE as integer "
3068 "rather than floating due to prototype",
3069 argnum, rname);
3070 if (INTEGRAL_TYPE_P (type)
3071 && TREE_CODE (valtype) == COMPLEX_TYPE)
3072 warning (0, "passing argument %d of %qE as integer "
3073 "rather than complex due to prototype",
3074 argnum, rname);
3075 else if (TREE_CODE (type) == COMPLEX_TYPE
3076 && TREE_CODE (valtype) == REAL_TYPE)
3077 warning (0, "passing argument %d of %qE as complex "
3078 "rather than floating due to prototype",
3079 argnum, rname);
3080 else if (TREE_CODE (type) == REAL_TYPE
3081 && INTEGRAL_TYPE_P (valtype))
3082 warning (0, "passing argument %d of %qE as floating "
3083 "rather than integer due to prototype",
3084 argnum, rname);
3085 else if (TREE_CODE (type) == COMPLEX_TYPE
3086 && INTEGRAL_TYPE_P (valtype))
3087 warning (0, "passing argument %d of %qE as complex "
3088 "rather than integer due to prototype",
3089 argnum, rname);
3090 else if (TREE_CODE (type) == REAL_TYPE
3091 && TREE_CODE (valtype) == COMPLEX_TYPE)
3092 warning (0, "passing argument %d of %qE as floating "
3093 "rather than complex due to prototype",
3094 argnum, rname);
3095 /* ??? At some point, messages should be written about
3096 conversions between complex types, but that's too messy
3097 to do now. */
3098 else if (TREE_CODE (type) == REAL_TYPE
3099 && TREE_CODE (valtype) == REAL_TYPE)
3101 /* Warn if any argument is passed as `float',
3102 since without a prototype it would be `double'. */
3103 if (formal_prec == TYPE_PRECISION (float_type_node)
3104 && type != dfloat32_type_node)
3105 warning (0, "passing argument %d of %qE as %<float%> "
3106 "rather than %<double%> due to prototype",
3107 argnum, rname);
3109 /* Warn if mismatch between argument and prototype
3110 for decimal float types. Warn of conversions with
3111 binary float types and of precision narrowing due to
3112 prototype. */
3113 else if (type != valtype
3114 && (type == dfloat32_type_node
3115 || type == dfloat64_type_node
3116 || type == dfloat128_type_node
3117 || valtype == dfloat32_type_node
3118 || valtype == dfloat64_type_node
3119 || valtype == dfloat128_type_node)
3120 && (formal_prec
3121 <= TYPE_PRECISION (valtype)
3122 || (type == dfloat128_type_node
3123 && (valtype
3124 != dfloat64_type_node
3125 && (valtype
3126 != dfloat32_type_node)))
3127 || (type == dfloat64_type_node
3128 && (valtype
3129 != dfloat32_type_node))))
3130 warning (0, "passing argument %d of %qE as %qT "
3131 "rather than %qT due to prototype",
3132 argnum, rname, type, valtype);
3135 /* Detect integer changing in width or signedness.
3136 These warnings are only activated with
3137 -Wtraditional-conversion, not with -Wtraditional. */
3138 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3139 && INTEGRAL_TYPE_P (valtype))
3141 tree would_have_been = default_conversion (val);
3142 tree type1 = TREE_TYPE (would_have_been);
3144 if (TREE_CODE (type) == ENUMERAL_TYPE
3145 && (TYPE_MAIN_VARIANT (type)
3146 == TYPE_MAIN_VARIANT (valtype)))
3147 /* No warning if function asks for enum
3148 and the actual arg is that enum type. */
3150 else if (formal_prec != TYPE_PRECISION (type1))
3151 warning (OPT_Wtraditional_conversion,
3152 "passing argument %d of %qE "
3153 "with different width due to prototype",
3154 argnum, rname);
3155 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3157 /* Don't complain if the formal parameter type
3158 is an enum, because we can't tell now whether
3159 the value was an enum--even the same enum. */
3160 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3162 else if (TREE_CODE (val) == INTEGER_CST
3163 && int_fits_type_p (val, type))
3164 /* Change in signedness doesn't matter
3165 if a constant value is unaffected. */
3167 /* If the value is extended from a narrower
3168 unsigned type, it doesn't matter whether we
3169 pass it as signed or unsigned; the value
3170 certainly is the same either way. */
3171 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3172 && TYPE_UNSIGNED (valtype))
3174 else if (TYPE_UNSIGNED (type))
3175 warning (OPT_Wtraditional_conversion,
3176 "passing argument %d of %qE "
3177 "as unsigned due to prototype",
3178 argnum, rname);
3179 else
3180 warning (OPT_Wtraditional_conversion,
3181 "passing argument %d of %qE "
3182 "as signed due to prototype", argnum, rname);
3186 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3187 sake of better warnings from convert_and_check. */
3188 if (excess_precision)
3189 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3190 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3191 parmval = convert_for_assignment (input_location, type, val,
3192 origtype, ic_argpass, npc,
3193 fundecl, function,
3194 parmnum + 1);
3196 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3197 && INTEGRAL_TYPE_P (type)
3198 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3199 parmval = default_conversion (parmval);
3202 else if (TREE_CODE (valtype) == REAL_TYPE
3203 && (TYPE_PRECISION (valtype)
3204 < TYPE_PRECISION (double_type_node))
3205 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3207 if (type_generic)
3208 parmval = val;
3209 else
3211 /* Convert `float' to `double'. */
3212 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3213 warning (OPT_Wdouble_promotion,
3214 "implicit conversion from %qT to %qT when passing "
3215 "argument to function",
3216 valtype, double_type_node);
3217 parmval = convert (double_type_node, val);
3220 else if (excess_precision && !type_generic)
3221 /* A "double" argument with excess precision being passed
3222 without a prototype or in variable arguments. */
3223 parmval = convert (valtype, val);
3224 else if ((invalid_func_diag =
3225 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3227 error (invalid_func_diag);
3228 return -1;
3230 else
3231 /* Convert `short' and `char' to full-size `int'. */
3232 parmval = default_conversion (val);
3234 (*values)[parmnum] = parmval;
3235 if (parmval == error_mark_node)
3236 error_args = true;
3238 if (typetail)
3239 typetail = TREE_CHAIN (typetail);
3242 gcc_assert (parmnum == vec_safe_length (values));
3244 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3246 error_at (input_location,
3247 "too few arguments to function %qE", function);
3248 inform_declaration (fundecl);
3249 return -1;
3252 return error_args ? -1 : (int) parmnum;
3255 /* This is the entry point used by the parser to build unary operators
3256 in the input. CODE, a tree_code, specifies the unary operator, and
3257 ARG is the operand. For unary plus, the C parser currently uses
3258 CONVERT_EXPR for code.
3260 LOC is the location to use for the tree generated.
3263 struct c_expr
3264 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3266 struct c_expr result;
3268 result.value = build_unary_op (loc, code, arg.value, 0);
3269 result.original_code = code;
3270 result.original_type = NULL;
3272 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3273 overflow_warning (loc, result.value);
3275 return result;
3278 /* This is the entry point used by the parser to build binary operators
3279 in the input. CODE, a tree_code, specifies the binary operator, and
3280 ARG1 and ARG2 are the operands. In addition to constructing the
3281 expression, we check for operands that were written with other binary
3282 operators in a way that is likely to confuse the user.
3284 LOCATION is the location of the binary operator. */
3286 struct c_expr
3287 parser_build_binary_op (location_t location, enum tree_code code,
3288 struct c_expr arg1, struct c_expr arg2)
3290 struct c_expr result;
3292 enum tree_code code1 = arg1.original_code;
3293 enum tree_code code2 = arg2.original_code;
3294 tree type1 = (arg1.original_type
3295 ? arg1.original_type
3296 : TREE_TYPE (arg1.value));
3297 tree type2 = (arg2.original_type
3298 ? arg2.original_type
3299 : TREE_TYPE (arg2.value));
3301 result.value = build_binary_op (location, code,
3302 arg1.value, arg2.value, 1);
3303 result.original_code = code;
3304 result.original_type = NULL;
3306 if (TREE_CODE (result.value) == ERROR_MARK)
3307 return result;
3309 if (location != UNKNOWN_LOCATION)
3310 protected_set_expr_location (result.value, location);
3312 /* Check for cases such as x+y<<z which users are likely
3313 to misinterpret. */
3314 if (warn_parentheses)
3315 warn_about_parentheses (input_location, code,
3316 code1, arg1.value, code2, arg2.value);
3318 if (warn_logical_op)
3319 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3320 code1, arg1.value, code2, arg2.value);
3322 /* Warn about comparisons against string literals, with the exception
3323 of testing for equality or inequality of a string literal with NULL. */
3324 if (code == EQ_EXPR || code == NE_EXPR)
3326 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3327 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3328 warning_at (location, OPT_Waddress,
3329 "comparison with string literal results in unspecified behavior");
3331 else if (TREE_CODE_CLASS (code) == tcc_comparison
3332 && (code1 == STRING_CST || code2 == STRING_CST))
3333 warning_at (location, OPT_Waddress,
3334 "comparison with string literal results in unspecified behavior");
3336 if (TREE_OVERFLOW_P (result.value)
3337 && !TREE_OVERFLOW_P (arg1.value)
3338 && !TREE_OVERFLOW_P (arg2.value))
3339 overflow_warning (location, result.value);
3341 /* Warn about comparisons of different enum types. */
3342 if (warn_enum_compare
3343 && TREE_CODE_CLASS (code) == tcc_comparison
3344 && TREE_CODE (type1) == ENUMERAL_TYPE
3345 && TREE_CODE (type2) == ENUMERAL_TYPE
3346 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3347 warning_at (location, OPT_Wenum_compare,
3348 "comparison between %qT and %qT",
3349 type1, type2);
3351 return result;
3354 /* Return a tree for the sum or difference (RESULTCODE says which)
3355 of pointer PTROP and integer INTOP. */
3357 static
3358 tree
3359 c_pointer_int_sum (location_t location, enum tree_code resultcode,
3360 tree ptrop, tree intop)
3362 /* The result is a pointer of the same type that is being added. */
3363 tree result_type = TREE_TYPE (ptrop);
3365 if (upc_shared_type_p (TREE_TYPE (result_type)))
3366 return upc_pts_int_sum (location, resultcode, ptrop, intop);
3368 return pointer_int_sum (location, resultcode, ptrop, intop);
3371 /* Return a tree for the difference of pointers OP0 and OP1.
3372 The resulting tree has type int. */
3374 static tree
3375 pointer_diff (location_t loc, tree op0, tree op1)
3377 tree restype = ptrdiff_type_node;
3378 tree result, inttype;
3380 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3381 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3382 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3383 tree subtrahend_type = TREE_TYPE (TREE_TYPE (op1));
3384 tree con0, con1, lit0, lit1;
3385 tree orig_op1 = op1;
3387 /* If the operands point into different address spaces, we need to
3388 explicitly convert them to pointers into the common address space
3389 before we can subtract the numerical address values. */
3390 if (as0 != as1)
3392 addr_space_t as_common;
3393 tree common_type;
3395 /* Determine the common superset address space. This is guaranteed
3396 to exist because the caller verified that comp_target_types
3397 returned non-zero. */
3398 if (!addr_space_superset (as0, as1, &as_common))
3399 gcc_unreachable ();
3401 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3402 op0 = convert (common_type, op0);
3403 op1 = convert (common_type, op1);
3406 /* Determine integer type to perform computations in. This will usually
3407 be the same as the result type (ptrdiff_t), but may need to be a wider
3408 type if pointers for the address space are wider than ptrdiff_t. */
3409 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3410 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3411 else
3412 inttype = restype;
3415 if (TREE_CODE (target_type) == VOID_TYPE)
3416 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3417 "pointer of type %<void *%> used in subtraction");
3418 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3419 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3420 "pointer to a function used in subtraction");
3422 if (upc_shared_type_p (target_type) || upc_shared_type_p (subtrahend_type))
3423 return upc_pts_diff (op0, op1);
3425 /* If the conversion to ptrdiff_type does anything like widening or
3426 converting a partial to an integral mode, we get a convert_expression
3427 that is in the way to do any simplifications.
3428 (fold-const.c doesn't know that the extra bits won't be needed.
3429 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3430 different mode in place.)
3431 So first try to find a common term here 'by hand'; we want to cover
3432 at least the cases that occur in legal static initializers. */
3433 if (CONVERT_EXPR_P (op0)
3434 && (TYPE_PRECISION (TREE_TYPE (op0))
3435 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3436 con0 = TREE_OPERAND (op0, 0);
3437 else
3438 con0 = op0;
3439 if (CONVERT_EXPR_P (op1)
3440 && (TYPE_PRECISION (TREE_TYPE (op1))
3441 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3442 con1 = TREE_OPERAND (op1, 0);
3443 else
3444 con1 = op1;
3446 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3448 lit0 = TREE_OPERAND (con0, 1);
3449 con0 = TREE_OPERAND (con0, 0);
3451 else
3452 lit0 = integer_zero_node;
3454 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
3456 lit1 = TREE_OPERAND (con1, 1);
3457 con1 = TREE_OPERAND (con1, 0);
3459 else
3460 lit1 = integer_zero_node;
3462 if (operand_equal_p (con0, con1, 0)
3463 && !upc_shared_type_p (TREE_TYPE (TREE_TYPE (con0)))
3464 && !upc_shared_type_p (TREE_TYPE (TREE_TYPE (con1))))
3466 op0 = lit0;
3467 op1 = lit1;
3471 /* First do the subtraction as integers;
3472 then drop through to build the divide operator.
3473 Do not do default conversions on the minus operator
3474 in case restype is a short type. */
3476 op0 = build_binary_op (loc,
3477 MINUS_EXPR, convert (inttype, op0),
3478 convert (inttype, op1), 0);
3479 /* This generates an error if op1 is pointer to incomplete type. */
3480 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3481 error_at (loc, "arithmetic on pointer to an incomplete type");
3483 /* This generates an error if op0 is pointer to incomplete type. */
3484 op1 = c_size_in_bytes (target_type);
3486 /* Divide by the size, in easiest possible way. */
3487 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3488 op0, convert (inttype, op1));
3490 /* Convert to final result type if necessary. */
3491 return convert (restype, result);
3494 /* Construct and perhaps optimize a tree representation
3495 for a unary operation. CODE, a tree_code, specifies the operation
3496 and XARG is the operand.
3497 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3498 the default promotions (such as from short to int).
3499 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3500 allows non-lvalues; this is only used to handle conversion of non-lvalue
3501 arrays to pointers in C99.
3503 LOCATION is the location of the operator. */
3505 tree
3506 build_unary_op (location_t location,
3507 enum tree_code code, tree xarg, int flag)
3509 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3510 tree arg = xarg;
3511 tree argtype = 0;
3512 enum tree_code typecode;
3513 tree val;
3514 tree ret = error_mark_node;
3515 tree eptype = NULL_TREE;
3516 int noconvert = flag;
3517 const char *invalid_op_diag;
3518 bool int_operands;
3520 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3521 if (int_operands)
3522 arg = remove_c_maybe_const_expr (arg);
3524 if (code != ADDR_EXPR)
3525 arg = require_complete_type (arg);
3527 typecode = TREE_CODE (TREE_TYPE (arg));
3528 if (typecode == ERROR_MARK)
3529 return error_mark_node;
3530 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3531 typecode = INTEGER_TYPE;
3533 if ((invalid_op_diag
3534 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3536 error_at (location, invalid_op_diag);
3537 return error_mark_node;
3540 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3542 eptype = TREE_TYPE (arg);
3543 arg = TREE_OPERAND (arg, 0);
3546 switch (code)
3548 case CONVERT_EXPR:
3549 /* This is used for unary plus, because a CONVERT_EXPR
3550 is enough to prevent anybody from looking inside for
3551 associativity, but won't generate any code. */
3552 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3553 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3554 || typecode == VECTOR_TYPE))
3556 error_at (location, "wrong type argument to unary plus");
3557 return error_mark_node;
3559 else if (!noconvert)
3560 arg = default_conversion (arg);
3561 arg = non_lvalue_loc (location, arg);
3562 break;
3564 case NEGATE_EXPR:
3565 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3566 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3567 || typecode == VECTOR_TYPE))
3569 error_at (location, "wrong type argument to unary minus");
3570 return error_mark_node;
3572 else if (!noconvert)
3573 arg = default_conversion (arg);
3574 break;
3576 case BIT_NOT_EXPR:
3577 /* ~ works on integer types and non float vectors. */
3578 if (typecode == INTEGER_TYPE
3579 || (typecode == VECTOR_TYPE
3580 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3582 if (!noconvert)
3583 arg = default_conversion (arg);
3585 else if (typecode == COMPLEX_TYPE)
3587 code = CONJ_EXPR;
3588 pedwarn (location, OPT_Wpedantic,
3589 "ISO C does not support %<~%> for complex conjugation");
3590 if (!noconvert)
3591 arg = default_conversion (arg);
3593 else
3595 error_at (location, "wrong type argument to bit-complement");
3596 return error_mark_node;
3598 break;
3600 case ABS_EXPR:
3601 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3603 error_at (location, "wrong type argument to abs");
3604 return error_mark_node;
3606 else if (!noconvert)
3607 arg = default_conversion (arg);
3608 break;
3610 case CONJ_EXPR:
3611 /* Conjugating a real value is a no-op, but allow it anyway. */
3612 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3613 || typecode == COMPLEX_TYPE))
3615 error_at (location, "wrong type argument to conjugation");
3616 return error_mark_node;
3618 else if (!noconvert)
3619 arg = default_conversion (arg);
3620 break;
3622 case TRUTH_NOT_EXPR:
3623 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3624 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3625 && typecode != COMPLEX_TYPE)
3627 error_at (location,
3628 "wrong type argument to unary exclamation mark");
3629 return error_mark_node;
3631 if (int_operands)
3633 arg = c_objc_common_truthvalue_conversion (location, xarg);
3634 arg = remove_c_maybe_const_expr (arg);
3636 else
3637 arg = c_objc_common_truthvalue_conversion (location, arg);
3638 ret = invert_truthvalue_loc (location, arg);
3639 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3640 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3641 location = EXPR_LOCATION (ret);
3642 goto return_build_unary_op;
3644 case REALPART_EXPR:
3645 case IMAGPART_EXPR:
3646 ret = build_real_imag_expr (location, code, arg);
3647 if (ret == error_mark_node)
3648 return error_mark_node;
3649 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3650 eptype = TREE_TYPE (eptype);
3651 goto return_build_unary_op;
3653 case PREINCREMENT_EXPR:
3654 case POSTINCREMENT_EXPR:
3655 case PREDECREMENT_EXPR:
3656 case POSTDECREMENT_EXPR:
3658 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3660 tree inner = build_unary_op (location, code,
3661 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3662 if (inner == error_mark_node)
3663 return error_mark_node;
3664 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3665 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3666 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3667 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3668 goto return_build_unary_op;
3671 /* Complain about anything that is not a true lvalue. In
3672 Objective-C, skip this check for property_refs. */
3673 if (!objc_is_property_ref (arg)
3674 && !lvalue_or_else (location,
3675 arg, ((code == PREINCREMENT_EXPR
3676 || code == POSTINCREMENT_EXPR)
3677 ? lv_increment
3678 : lv_decrement)))
3679 return error_mark_node;
3681 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3683 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3684 warning_at (location, OPT_Wc___compat,
3685 "increment of enumeration value is invalid in C++");
3686 else
3687 warning_at (location, OPT_Wc___compat,
3688 "decrement of enumeration value is invalid in C++");
3691 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3692 arg = c_fully_fold (arg, false, NULL);
3694 /* Increment or decrement the real part of the value,
3695 and don't change the imaginary part. */
3696 if (typecode == COMPLEX_TYPE)
3698 tree real, imag;
3700 pedwarn (location, OPT_Wpedantic,
3701 "ISO C does not support %<++%> and %<--%> on complex types");
3703 arg = stabilize_reference (arg);
3704 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3705 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3706 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3707 if (real == error_mark_node || imag == error_mark_node)
3708 return error_mark_node;
3709 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3710 real, imag);
3711 goto return_build_unary_op;
3714 /* Report invalid types. */
3716 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3717 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3719 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3720 error_at (location, "wrong type argument to increment");
3721 else
3722 error_at (location, "wrong type argument to decrement");
3724 return error_mark_node;
3728 tree inc;
3730 argtype = TREE_TYPE (arg);
3732 /* Compute the increment. */
3734 if (typecode == POINTER_TYPE)
3736 /* If pointer target is an undefined struct,
3737 we just cannot know how to do the arithmetic. */
3738 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3740 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3741 error_at (location,
3742 "increment of pointer to unknown structure");
3743 else
3744 error_at (location,
3745 "decrement of pointer to unknown structure");
3747 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3748 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3750 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3751 pedwarn (location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3752 "wrong type argument to increment");
3753 else
3754 pedwarn (location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3755 "wrong type argument to decrement");
3758 /* UPC pointer-to-shared types cannot be
3759 incremented/decrmented directly. */
3760 if (upc_shared_type_p (TREE_TYPE (argtype)))
3761 return upc_pts_increment (location, code, arg);
3763 inc = c_size_in_bytes (TREE_TYPE (argtype));
3764 inc = convert_to_ptrofftype_loc (location, inc);
3766 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3768 /* For signed fract types, we invert ++ to -- or
3769 -- to ++, and change inc from 1 to -1, because
3770 it is not possible to represent 1 in signed fract constants.
3771 For unsigned fract types, the result always overflows and
3772 we get an undefined (original) or the maximum value. */
3773 if (code == PREINCREMENT_EXPR)
3774 code = PREDECREMENT_EXPR;
3775 else if (code == PREDECREMENT_EXPR)
3776 code = PREINCREMENT_EXPR;
3777 else if (code == POSTINCREMENT_EXPR)
3778 code = POSTDECREMENT_EXPR;
3779 else /* code == POSTDECREMENT_EXPR */
3780 code = POSTINCREMENT_EXPR;
3782 inc = integer_minus_one_node;
3783 inc = convert (argtype, inc);
3785 else
3787 inc = integer_one_node;
3788 inc = convert (argtype, inc);
3791 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3792 need to ask Objective-C to build the increment or decrement
3793 expression for it. */
3794 if (objc_is_property_ref (arg))
3795 return objc_build_incr_expr_for_property_ref (location, code,
3796 arg, inc);
3798 /* Report a read-only lvalue. */
3799 if (TYPE_READONLY (argtype))
3801 readonly_error (arg,
3802 ((code == PREINCREMENT_EXPR
3803 || code == POSTINCREMENT_EXPR)
3804 ? lv_increment : lv_decrement));
3805 return error_mark_node;
3807 else if (TREE_READONLY (arg))
3808 readonly_warning (arg,
3809 ((code == PREINCREMENT_EXPR
3810 || code == POSTINCREMENT_EXPR)
3811 ? lv_increment : lv_decrement));
3813 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3814 val = boolean_increment (code, arg);
3815 else
3816 val = build2 (code, TREE_TYPE (arg), arg, inc);
3817 TREE_SIDE_EFFECTS (val) = 1;
3818 if (TREE_CODE (val) != code)
3819 TREE_NO_WARNING (val) = 1;
3820 ret = val;
3821 goto return_build_unary_op;
3824 case ADDR_EXPR:
3825 /* Note that this operation never does default_conversion. */
3827 /* The operand of unary '&' must be an lvalue (which excludes
3828 expressions of type void), or, in C99, the result of a [] or
3829 unary '*' operator. */
3830 if (VOID_TYPE_P (TREE_TYPE (arg))
3831 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3832 && (TREE_CODE (arg) != INDIRECT_REF
3833 || !flag_isoc99))
3834 pedwarn (location, 0, "taking address of expression of type %<void%>");
3836 /* Let &* cancel out to simplify resulting code. */
3837 if (TREE_CODE (arg) == INDIRECT_REF)
3839 /* Don't let this be an lvalue. */
3840 if (lvalue_p (TREE_OPERAND (arg, 0)))
3841 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3842 ret = TREE_OPERAND (arg, 0);
3843 goto return_build_unary_op;
3846 /* For &x[y], return x+y */
3847 if (TREE_CODE (arg) == ARRAY_REF)
3849 tree op0 = TREE_OPERAND (arg, 0);
3850 if (!c_mark_addressable (op0))
3851 return error_mark_node;
3852 /* Taking the address of a UPC shared array element
3853 cannot be performed as a simple addition.
3854 Return an ADDR_EXPR node, and let upc_genericize()
3855 implement the proper semantics. */
3856 if (TREE_SHARED (arg))
3857 return build1 (ADDR_EXPR, TREE_TYPE (arg), arg);
3860 /* Anything not already handled and not a true memory reference
3861 or a non-lvalue array is an error. */
3862 else if (typecode != FUNCTION_TYPE && !flag
3863 && !lvalue_or_else (location, arg, lv_addressof))
3864 return error_mark_node;
3866 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3867 folding later. */
3868 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3870 tree inner = build_unary_op (location, code,
3871 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3872 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3873 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3874 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3875 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3876 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3877 goto return_build_unary_op;
3880 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3881 argtype = TREE_TYPE (arg);
3883 /* If the lvalue is const or volatile, merge that into the type
3884 to which the address will point. This is only needed
3885 for function types. */
3886 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3887 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3888 && TREE_CODE (argtype) == FUNCTION_TYPE)
3890 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3891 int quals = orig_quals;
3893 if (TREE_READONLY (arg))
3894 quals |= TYPE_QUAL_CONST;
3895 if (TREE_THIS_VOLATILE (arg))
3896 quals |= TYPE_QUAL_VOLATILE;
3898 argtype = c_build_qualified_type (argtype, quals);
3901 if (!c_mark_addressable (arg))
3902 return error_mark_node;
3904 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3905 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3907 argtype = build_pointer_type (argtype);
3909 /* ??? Cope with user tricks that amount to offsetof. Delete this
3910 when we have proper support for integer constant expressions. */
3911 val = get_base_address (arg);
3912 if (val && TREE_CODE (val) == INDIRECT_REF
3913 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3915 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
3916 goto return_build_unary_op;
3919 val = build1 (ADDR_EXPR, argtype, arg);
3921 ret = val;
3922 goto return_build_unary_op;
3924 default:
3925 gcc_unreachable ();
3928 if (argtype == 0)
3929 argtype = TREE_TYPE (arg);
3930 if (TREE_CODE (arg) == INTEGER_CST)
3931 ret = (require_constant_value
3932 ? fold_build1_initializer_loc (location, code, argtype, arg)
3933 : fold_build1_loc (location, code, argtype, arg));
3934 else
3935 ret = build1 (code, argtype, arg);
3936 return_build_unary_op:
3937 gcc_assert (ret != error_mark_node);
3938 /* The result of an operation on objects that
3939 are UPC shared qualified, must not be shared qualified. */
3940 if (upc_shared_type_p (TREE_TYPE (ret)))
3941 TREE_TYPE (ret) = build_upc_unshared_type (TREE_TYPE (ret));
3942 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3943 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3944 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3945 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3946 ret = note_integer_operands (ret);
3947 if (eptype)
3948 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3949 protected_set_expr_location (ret, location);
3950 return ret;
3953 /* Return nonzero if REF is an lvalue valid for this language.
3954 Lvalues can be assigned, unless their type has TYPE_READONLY.
3955 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3957 bool
3958 lvalue_p (const_tree ref)
3960 const enum tree_code code = TREE_CODE (ref);
3962 switch (code)
3964 case REALPART_EXPR:
3965 case IMAGPART_EXPR:
3966 case COMPONENT_REF:
3967 return lvalue_p (TREE_OPERAND (ref, 0));
3969 case C_MAYBE_CONST_EXPR:
3970 return lvalue_p (TREE_OPERAND (ref, 1));
3972 case COMPOUND_LITERAL_EXPR:
3973 case STRING_CST:
3974 return 1;
3976 case INDIRECT_REF:
3977 case ARRAY_REF:
3978 case VAR_DECL:
3979 case PARM_DECL:
3980 case RESULT_DECL:
3981 case ERROR_MARK:
3982 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3983 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3985 case BIND_EXPR:
3986 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3988 default:
3989 return 0;
3993 /* Give a warning for storing in something that is read-only in GCC
3994 terms but not const in ISO C terms. */
3996 static void
3997 readonly_warning (tree arg, enum lvalue_use use)
3999 switch (use)
4001 case lv_assign:
4002 warning (0, "assignment of read-only location %qE", arg);
4003 break;
4004 case lv_increment:
4005 warning (0, "increment of read-only location %qE", arg);
4006 break;
4007 case lv_decrement:
4008 warning (0, "decrement of read-only location %qE", arg);
4009 break;
4010 default:
4011 gcc_unreachable ();
4013 return;
4017 /* Return nonzero if REF is an lvalue valid for this language;
4018 otherwise, print an error message and return zero. USE says
4019 how the lvalue is being used and so selects the error message.
4020 LOCATION is the location at which any error should be reported. */
4022 static int
4023 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4025 int win = lvalue_p (ref);
4027 if (!win)
4028 lvalue_error (loc, use);
4030 return win;
4033 /* Mark EXP saying that we need to be able to take the
4034 address of it; it should not be allocated in a register.
4035 Returns true if successful. */
4037 bool
4038 c_mark_addressable (tree exp)
4040 tree x = exp;
4042 while (1)
4043 switch (TREE_CODE (x))
4045 case COMPONENT_REF:
4046 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4048 error
4049 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4050 return false;
4053 /* ... fall through ... */
4055 case ADDR_EXPR:
4056 case ARRAY_REF:
4057 case REALPART_EXPR:
4058 case IMAGPART_EXPR:
4059 x = TREE_OPERAND (x, 0);
4060 break;
4062 case COMPOUND_LITERAL_EXPR:
4063 case CONSTRUCTOR:
4064 TREE_ADDRESSABLE (x) = 1;
4065 return true;
4067 case VAR_DECL:
4068 case CONST_DECL:
4069 case PARM_DECL:
4070 case RESULT_DECL:
4071 if (C_DECL_REGISTER (x)
4072 && DECL_NONLOCAL (x))
4074 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4076 error
4077 ("global register variable %qD used in nested function", x);
4078 return false;
4080 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4082 else if (C_DECL_REGISTER (x))
4084 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4085 error ("address of global register variable %qD requested", x);
4086 else
4087 error ("address of register variable %qD requested", x);
4088 return false;
4091 /* drops in */
4092 case FUNCTION_DECL:
4093 TREE_ADDRESSABLE (x) = 1;
4094 /* drops out */
4095 default:
4096 return true;
4100 /* Convert EXPR to TYPE, warning about conversion problems with
4101 constants. SEMANTIC_TYPE is the type this conversion would use
4102 without excess precision. If SEMANTIC_TYPE is NULL, this function
4103 is equivalent to convert_and_check. This function is a wrapper that
4104 handles conversions that may be different than
4105 the usual ones because of excess precision. */
4107 static tree
4108 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4110 if (TREE_TYPE (expr) == type)
4111 return expr;
4113 if (!semantic_type)
4114 return convert_and_check (type, expr);
4116 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4117 && TREE_TYPE (expr) != semantic_type)
4119 /* For integers, we need to check the real conversion, not
4120 the conversion to the excess precision type. */
4121 expr = convert_and_check (semantic_type, expr);
4123 /* Result type is the excess precision type, which should be
4124 large enough, so do not check. */
4125 return convert (type, expr);
4128 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4129 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4130 if folded to an integer constant then the unselected half may
4131 contain arbitrary operations not normally permitted in constant
4132 expressions. Set the location of the expression to LOC. */
4134 tree
4135 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4136 tree op1, tree op1_original_type, tree op2,
4137 tree op2_original_type)
4139 tree type1;
4140 tree type2;
4141 enum tree_code code1;
4142 enum tree_code code2;
4143 tree result_type = NULL;
4144 tree semantic_result_type = NULL;
4145 tree orig_op1 = op1, orig_op2 = op2;
4146 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4147 bool ifexp_int_operands;
4148 tree ret;
4150 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4151 if (op1_int_operands)
4152 op1 = remove_c_maybe_const_expr (op1);
4153 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4154 if (op2_int_operands)
4155 op2 = remove_c_maybe_const_expr (op2);
4156 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4157 if (ifexp_int_operands)
4158 ifexp = remove_c_maybe_const_expr (ifexp);
4160 /* Promote both alternatives. */
4162 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4163 op1 = default_conversion (op1);
4164 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4165 op2 = default_conversion (op2);
4167 if (TREE_CODE (ifexp) == ERROR_MARK
4168 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4169 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4170 return error_mark_node;
4172 type1 = TREE_TYPE (op1);
4173 code1 = TREE_CODE (type1);
4174 type2 = TREE_TYPE (op2);
4175 code2 = TREE_CODE (type2);
4177 /* C90 does not permit non-lvalue arrays in conditional expressions.
4178 In C99 they will be pointers by now. */
4179 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4181 error_at (colon_loc, "non-lvalue array in conditional expression");
4182 return error_mark_node;
4185 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4186 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4187 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4188 || code1 == COMPLEX_TYPE)
4189 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4190 || code2 == COMPLEX_TYPE))
4192 semantic_result_type = c_common_type (type1, type2);
4193 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4195 op1 = TREE_OPERAND (op1, 0);
4196 type1 = TREE_TYPE (op1);
4197 gcc_assert (TREE_CODE (type1) == code1);
4199 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4201 op2 = TREE_OPERAND (op2, 0);
4202 type2 = TREE_TYPE (op2);
4203 gcc_assert (TREE_CODE (type2) == code2);
4207 if (warn_cxx_compat)
4209 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4210 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4212 if (TREE_CODE (t1) == ENUMERAL_TYPE
4213 && TREE_CODE (t2) == ENUMERAL_TYPE
4214 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4215 warning_at (colon_loc, OPT_Wc___compat,
4216 ("different enum types in conditional is "
4217 "invalid in C++: %qT vs %qT"),
4218 t1, t2);
4221 /* Quickly detect the usual case where op1 and op2 have the same type
4222 after promotion. */
4223 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4225 if (type1 == type2)
4226 result_type = type1;
4227 else
4228 result_type = TYPE_MAIN_VARIANT (type1);
4230 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4231 || code1 == COMPLEX_TYPE)
4232 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4233 || code2 == COMPLEX_TYPE))
4235 result_type = c_common_type (type1, type2);
4236 do_warn_double_promotion (result_type, type1, type2,
4237 "implicit conversion from %qT to %qT to "
4238 "match other result of conditional",
4239 colon_loc);
4241 /* If -Wsign-compare, warn here if type1 and type2 have
4242 different signedness. We'll promote the signed to unsigned
4243 and later code won't know it used to be different.
4244 Do this check on the original types, so that explicit casts
4245 will be considered, but default promotions won't. */
4246 if (c_inhibit_evaluation_warnings == 0)
4248 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4249 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4251 if (unsigned_op1 ^ unsigned_op2)
4253 bool ovf;
4255 /* Do not warn if the result type is signed, since the
4256 signed type will only be chosen if it can represent
4257 all the values of the unsigned type. */
4258 if (!TYPE_UNSIGNED (result_type))
4259 /* OK */;
4260 else
4262 bool op1_maybe_const = true;
4263 bool op2_maybe_const = true;
4265 /* Do not warn if the signed quantity is an
4266 unsuffixed integer literal (or some static
4267 constant expression involving such literals) and
4268 it is non-negative. This warning requires the
4269 operands to be folded for best results, so do
4270 that folding in this case even without
4271 warn_sign_compare to avoid warning options
4272 possibly affecting code generation. */
4273 c_inhibit_evaluation_warnings
4274 += (ifexp == truthvalue_false_node);
4275 op1 = c_fully_fold (op1, require_constant_value,
4276 &op1_maybe_const);
4277 c_inhibit_evaluation_warnings
4278 -= (ifexp == truthvalue_false_node);
4280 c_inhibit_evaluation_warnings
4281 += (ifexp == truthvalue_true_node);
4282 op2 = c_fully_fold (op2, require_constant_value,
4283 &op2_maybe_const);
4284 c_inhibit_evaluation_warnings
4285 -= (ifexp == truthvalue_true_node);
4287 if (warn_sign_compare)
4289 if ((unsigned_op2
4290 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4291 || (unsigned_op1
4292 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4293 /* OK */;
4294 else
4295 warning_at (colon_loc, OPT_Wsign_compare,
4296 ("signed and unsigned type in "
4297 "conditional expression"));
4299 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4300 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4301 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4302 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4307 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4309 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4310 pedwarn (colon_loc, OPT_Wpedantic,
4311 "ISO C forbids conditional expr with only one void side");
4312 result_type = void_type_node;
4314 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4316 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4317 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4318 addr_space_t as_common;
4320 if (comp_target_types (colon_loc, type1, type2))
4321 result_type = common_pointer_type (type1, type2);
4322 else if (null_pointer_constant_p (orig_op1))
4323 result_type = type2;
4324 else if (null_pointer_constant_p (orig_op2))
4325 result_type = type1;
4326 else if (!addr_space_superset (as1, as2, &as_common))
4328 error_at (colon_loc, "pointers to disjoint address spaces "
4329 "used in conditional expression");
4330 return error_mark_node;
4332 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4334 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4335 pedwarn (colon_loc, OPT_Wpedantic,
4336 "ISO C forbids conditional expr between "
4337 "%<void *%> and function pointer");
4338 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4339 TREE_TYPE (type2)));
4341 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4343 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4344 pedwarn (colon_loc, OPT_Wpedantic,
4345 "ISO C forbids conditional expr between "
4346 "%<void *%> and function pointer");
4347 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4348 TREE_TYPE (type1)));
4350 /* Objective-C pointer comparisons are a bit more lenient. */
4351 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4352 result_type = objc_common_type (type1, type2);
4353 else
4355 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4357 pedwarn (colon_loc, 0,
4358 "pointer type mismatch in conditional expression");
4359 result_type = build_pointer_type
4360 (build_qualified_type (void_type_node, qual));
4363 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4365 if (!null_pointer_constant_p (orig_op2))
4366 pedwarn (colon_loc, 0,
4367 "pointer/integer type mismatch in conditional expression");
4368 else
4370 op2 = !upc_shared_type_p (TREE_TYPE (type1))
4371 ? null_pointer_node : upc_null_pts_node;
4373 result_type = type1;
4375 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4377 if (!null_pointer_constant_p (orig_op1))
4378 pedwarn (colon_loc, 0,
4379 "pointer/integer type mismatch in conditional expression");
4380 else
4382 op1 = !upc_shared_type_p (TREE_TYPE (type2))
4383 ? null_pointer_node : upc_null_pts_node;
4385 result_type = type2;
4388 if (!result_type)
4390 if (flag_cond_mismatch)
4391 result_type = void_type_node;
4392 else
4394 error_at (colon_loc, "type mismatch in conditional expression");
4395 return error_mark_node;
4399 /* Merge const and volatile flags of the incoming types. */
4400 result_type
4401 = build_type_variant (result_type,
4402 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4403 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4405 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4406 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4408 if (ifexp_bcp && ifexp == truthvalue_true_node)
4410 op2_int_operands = true;
4411 op1 = c_fully_fold (op1, require_constant_value, NULL);
4413 if (ifexp_bcp && ifexp == truthvalue_false_node)
4415 op1_int_operands = true;
4416 op2 = c_fully_fold (op2, require_constant_value, NULL);
4418 int_const = int_operands = (ifexp_int_operands
4419 && op1_int_operands
4420 && op2_int_operands);
4421 if (int_operands)
4423 int_const = ((ifexp == truthvalue_true_node
4424 && TREE_CODE (orig_op1) == INTEGER_CST
4425 && !TREE_OVERFLOW (orig_op1))
4426 || (ifexp == truthvalue_false_node
4427 && TREE_CODE (orig_op2) == INTEGER_CST
4428 && !TREE_OVERFLOW (orig_op2)));
4430 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4431 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4432 else
4434 if (int_operands)
4436 op1 = remove_c_maybe_const_expr (op1);
4437 op2 = remove_c_maybe_const_expr (op2);
4439 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4440 if (int_operands)
4441 ret = note_integer_operands (ret);
4443 if (semantic_result_type)
4444 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4446 protected_set_expr_location (ret, colon_loc);
4447 return ret;
4450 /* Return a compound expression that performs two expressions and
4451 returns the value of the second of them.
4453 LOC is the location of the COMPOUND_EXPR. */
4455 tree
4456 build_compound_expr (location_t loc, tree expr1, tree expr2)
4458 bool expr1_int_operands, expr2_int_operands;
4459 tree eptype = NULL_TREE;
4460 tree ret;
4462 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4463 if (expr1_int_operands)
4464 expr1 = remove_c_maybe_const_expr (expr1);
4465 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4466 if (expr2_int_operands)
4467 expr2 = remove_c_maybe_const_expr (expr2);
4469 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4470 expr1 = TREE_OPERAND (expr1, 0);
4471 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4473 eptype = TREE_TYPE (expr2);
4474 expr2 = TREE_OPERAND (expr2, 0);
4477 if (!TREE_SIDE_EFFECTS (expr1))
4479 /* The left-hand operand of a comma expression is like an expression
4480 statement: with -Wunused, we should warn if it doesn't have
4481 any side-effects, unless it was explicitly cast to (void). */
4482 if (warn_unused_value)
4484 if (VOID_TYPE_P (TREE_TYPE (expr1))
4485 && CONVERT_EXPR_P (expr1))
4486 ; /* (void) a, b */
4487 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4488 && TREE_CODE (expr1) == COMPOUND_EXPR
4489 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4490 ; /* (void) a, (void) b, c */
4491 else
4492 warning_at (loc, OPT_Wunused_value,
4493 "left-hand operand of comma expression has no effect");
4497 /* With -Wunused, we should also warn if the left-hand operand does have
4498 side-effects, but computes a value which is not used. For example, in
4499 `foo() + bar(), baz()' the result of the `+' operator is not used,
4500 so we should issue a warning. */
4501 else if (warn_unused_value)
4502 warn_if_unused_value (expr1, loc);
4504 if (expr2 == error_mark_node)
4505 return error_mark_node;
4507 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4509 if (flag_isoc99
4510 && expr1_int_operands
4511 && expr2_int_operands)
4512 ret = note_integer_operands (ret);
4514 if (eptype)
4515 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4517 protected_set_expr_location (ret, loc);
4518 return ret;
4521 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4522 which we are casting. OTYPE is the type of the expression being
4523 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4524 of the cast. -Wcast-qual appeared on the command line. Named
4525 address space qualifiers are not handled here, because they result
4526 in different warnings. */
4528 static void
4529 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4531 tree in_type = type;
4532 tree in_otype = otype;
4533 int added = 0;
4534 int discarded = 0;
4535 bool is_const;
4537 /* Check that the qualifiers on IN_TYPE are a superset of the
4538 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4539 nodes is uninteresting and we stop as soon as we hit a
4540 non-POINTER_TYPE node on either type. */
4543 in_otype = TREE_TYPE (in_otype);
4544 in_type = TREE_TYPE (in_type);
4546 /* GNU C allows cv-qualified function types. 'const' means the
4547 function is very pure, 'volatile' means it can't return. We
4548 need to warn when such qualifiers are added, not when they're
4549 taken away. */
4550 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4551 && TREE_CODE (in_type) == FUNCTION_TYPE)
4552 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4553 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4554 else
4555 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4556 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4558 while (TREE_CODE (in_type) == POINTER_TYPE
4559 && TREE_CODE (in_otype) == POINTER_TYPE);
4561 if (added)
4562 warning_at (loc, OPT_Wcast_qual,
4563 "cast adds %q#v qualifier to function type", added);
4565 if (discarded)
4566 /* There are qualifiers present in IN_OTYPE that are not present
4567 in IN_TYPE. */
4568 warning_at (loc, OPT_Wcast_qual,
4569 "cast discards %q#v qualifier from pointer target type",
4570 discarded);
4572 if (added || discarded)
4573 return;
4575 /* A cast from **T to const **T is unsafe, because it can cause a
4576 const value to be changed with no additional warning. We only
4577 issue this warning if T is the same on both sides, and we only
4578 issue the warning if there are the same number of pointers on
4579 both sides, as otherwise the cast is clearly unsafe anyhow. A
4580 cast is unsafe when a qualifier is added at one level and const
4581 is not present at all outer levels.
4583 To issue this warning, we check at each level whether the cast
4584 adds new qualifiers not already seen. We don't need to special
4585 case function types, as they won't have the same
4586 TYPE_MAIN_VARIANT. */
4588 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4589 return;
4590 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4591 return;
4593 in_type = type;
4594 in_otype = otype;
4595 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4598 in_type = TREE_TYPE (in_type);
4599 in_otype = TREE_TYPE (in_otype);
4600 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4601 && !is_const)
4603 warning_at (loc, OPT_Wcast_qual,
4604 "to be safe all intermediate pointers in cast from "
4605 "%qT to %qT must be %<const%> qualified",
4606 otype, type);
4607 break;
4609 if (is_const)
4610 is_const = TYPE_READONLY (in_type);
4612 while (TREE_CODE (in_type) == POINTER_TYPE);
4615 /* Build an expression representing a cast to type TYPE of expression EXPR.
4616 LOC is the location of the cast-- typically the open paren of the cast. */
4618 tree
4619 build_c_cast (location_t loc, tree type, tree expr)
4621 tree value;
4623 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4624 expr = TREE_OPERAND (expr, 0);
4626 value = expr;
4628 if (type == error_mark_node || expr == error_mark_node)
4629 return error_mark_node;
4631 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4632 only in <protocol> qualifications. But when constructing cast expressions,
4633 the protocols do matter and must be kept around. */
4634 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4635 return build1 (NOP_EXPR, type, expr);
4637 if (upc_shared_type_p (type))
4639 error ("UPC does not allow casts to a shared type");
4640 return error_mark_node;
4643 type = TYPE_MAIN_VARIANT (type);
4645 if (TREE_CODE (type) == ARRAY_TYPE)
4647 error_at (loc, "cast specifies array type");
4648 return error_mark_node;
4651 if (TREE_CODE (type) == FUNCTION_TYPE)
4653 error_at (loc, "cast specifies function type");
4654 return error_mark_node;
4657 if (!VOID_TYPE_P (type))
4659 value = require_complete_type (value);
4660 if (value == error_mark_node)
4661 return error_mark_node;
4664 if (integer_zerop (value)
4665 && POINTER_TYPE_P (type)
4666 && upc_shared_type_p (TREE_TYPE (type))
4667 && POINTER_TYPE_P (TREE_TYPE (expr))
4668 && ! upc_shared_type_p (TREE_TYPE (TREE_TYPE (expr))))
4670 value = upc_null_pts_node;
4673 if (!upc_shared_type_p (type) && upc_shared_type_p (TREE_TYPE (expr)))
4675 /* UPC disallows things like:
4676 (int)p = <expr>; (where p is a shared int) */
4677 value = non_lvalue (value);
4680 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4682 if (TREE_CODE (type) == RECORD_TYPE
4683 || TREE_CODE (type) == UNION_TYPE)
4684 pedwarn (loc, OPT_Wpedantic,
4685 "ISO C forbids casting nonscalar to the same type");
4687 else if (TREE_CODE (type) == UNION_TYPE)
4689 tree field;
4691 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4692 if (TREE_TYPE (field) != error_mark_node
4693 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4694 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4695 break;
4697 if (field)
4699 tree t;
4700 bool maybe_const = true;
4702 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
4703 t = c_fully_fold (value, false, &maybe_const);
4704 t = build_constructor_single (type, field, t);
4705 if (!maybe_const)
4706 t = c_wrap_maybe_const (t, true);
4707 t = digest_init (loc, type, t,
4708 NULL_TREE, false, true, 0);
4709 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4710 return t;
4712 error_at (loc, "cast to union type from type not present in union");
4713 return error_mark_node;
4715 else
4717 tree otype, ovalue;
4719 if (type == void_type_node)
4721 tree t = build1 (CONVERT_EXPR, type, value);
4722 SET_EXPR_LOCATION (t, loc);
4723 return t;
4726 otype = TREE_TYPE (value);
4728 if (TREE_CODE (type) == POINTER_TYPE
4729 && TREE_CODE (otype) == POINTER_TYPE)
4731 int t_shared = upc_shared_type_p (TREE_TYPE (type));
4732 int o_shared = upc_shared_type_p (TREE_TYPE (otype));
4733 if ((!t_shared && o_shared)
4734 || (t_shared && o_shared
4735 && !lang_hooks.types_compatible_p (type, otype)))
4736 return build1 (CONVERT_EXPR, type, value);
4739 /* Optionally warn about potentially worrisome casts. */
4740 if (warn_cast_qual
4741 && TREE_CODE (type) == POINTER_TYPE
4742 && TREE_CODE (otype) == POINTER_TYPE)
4743 handle_warn_cast_qual (loc, type, otype);
4745 /* Warn about conversions between pointers to disjoint
4746 address spaces. */
4747 if (TREE_CODE (type) == POINTER_TYPE
4748 && TREE_CODE (otype) == POINTER_TYPE
4749 && !null_pointer_constant_p (value))
4751 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4752 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4753 addr_space_t as_common;
4755 if (!addr_space_superset (as_to, as_from, &as_common))
4757 if (ADDR_SPACE_GENERIC_P (as_from))
4758 warning_at (loc, 0, "cast to %s address space pointer "
4759 "from disjoint generic address space pointer",
4760 c_addr_space_name (as_to));
4762 else if (ADDR_SPACE_GENERIC_P (as_to))
4763 warning_at (loc, 0, "cast to generic address space pointer "
4764 "from disjoint %s address space pointer",
4765 c_addr_space_name (as_from));
4767 else
4768 warning_at (loc, 0, "cast to %s address space pointer "
4769 "from disjoint %s address space pointer",
4770 c_addr_space_name (as_to),
4771 c_addr_space_name (as_from));
4775 /* Warn about possible alignment problems. */
4776 if (STRICT_ALIGNMENT
4777 && TREE_CODE (type) == POINTER_TYPE
4778 && TREE_CODE (otype) == POINTER_TYPE
4779 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4780 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4781 /* Don't warn about opaque types, where the actual alignment
4782 restriction is unknown. */
4783 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4784 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4785 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4786 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4787 warning_at (loc, OPT_Wcast_align,
4788 "cast increases required alignment of target type");
4790 if (POINTER_TYPE_P (type)
4791 && upc_shared_type_p (TREE_TYPE (type))
4792 && POINTER_TYPE_P (otype)
4793 && !upc_shared_type_p (TREE_TYPE (otype)))
4795 error ("UPC does not allow casts from a local pointer to a pointer-to-shared");
4796 return error_mark_node;
4799 if (TREE_CODE (type) == POINTER_TYPE
4800 && TREE_CODE (otype) == INTEGER_TYPE
4801 && upc_shared_type_p (TREE_TYPE (type))
4802 && !integer_zerop (value))
4804 error ("UPC does not allow casts from an integer to a pointer-to-shared");
4805 return error_mark_node;
4808 if (TREE_CODE (type) == INTEGER_TYPE
4809 && TREE_CODE (otype) == POINTER_TYPE
4810 && upc_shared_type_p (TREE_TYPE (otype)))
4812 error ("UPC does not allow casts from a pointer-to-shared to an integer");
4813 return error_mark_node;
4817 if (TREE_CODE (type) == INTEGER_TYPE
4818 && TREE_CODE (otype) == POINTER_TYPE
4819 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4820 /* Unlike conversion of integers to pointers, where the
4821 warning is disabled for converting constants because
4822 of cases such as SIG_*, warn about converting constant
4823 pointers to integers. In some cases it may cause unwanted
4824 sign extension, and a warning is appropriate. */
4825 warning_at (loc, OPT_Wpointer_to_int_cast,
4826 "cast from pointer to integer of different size");
4828 if (TREE_CODE (value) == CALL_EXPR
4829 && TREE_CODE (type) != TREE_CODE (otype))
4830 warning_at (loc, OPT_Wbad_function_cast,
4831 "cast from function call of type %qT "
4832 "to non-matching type %qT", otype, type);
4834 if (TREE_CODE (type) == POINTER_TYPE
4835 && TREE_CODE (otype) == INTEGER_TYPE
4836 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4837 /* Don't warn about converting any constant. */
4838 && !TREE_CONSTANT (value))
4839 warning_at (loc,
4840 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4841 "of different size");
4843 if (warn_strict_aliasing <= 2)
4844 strict_aliasing_warning (otype, type, expr);
4846 /* If pedantic, warn for conversions between function and object
4847 pointer types, except for converting a null pointer constant
4848 to function pointer type. */
4849 if (pedantic
4850 && TREE_CODE (type) == POINTER_TYPE
4851 && TREE_CODE (otype) == POINTER_TYPE
4852 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4853 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4854 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
4855 "conversion of function pointer to object pointer type");
4857 if (pedantic
4858 && TREE_CODE (type) == POINTER_TYPE
4859 && TREE_CODE (otype) == POINTER_TYPE
4860 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4861 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4862 && !null_pointer_constant_p (value))
4863 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
4864 "conversion of object pointer to function pointer type");
4866 ovalue = value;
4867 value = convert (type, value);
4869 /* Ignore any integer overflow caused by the cast. */
4870 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4872 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4874 if (!TREE_OVERFLOW (value))
4876 /* Avoid clobbering a shared constant. */
4877 value = copy_node (value);
4878 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4881 else if (TREE_OVERFLOW (value))
4882 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4883 value = build_int_cst_wide (TREE_TYPE (value),
4884 TREE_INT_CST_LOW (value),
4885 TREE_INT_CST_HIGH (value));
4889 /* Don't let a cast be an lvalue. */
4890 if (value == expr)
4891 value = non_lvalue_loc (loc, value);
4893 /* Don't allow the results of casting to floating-point or complex
4894 types be confused with actual constants, or casts involving
4895 integer and pointer types other than direct integer-to-integer
4896 and integer-to-pointer be confused with integer constant
4897 expressions and null pointer constants. */
4898 if (TREE_CODE (value) == REAL_CST
4899 || TREE_CODE (value) == COMPLEX_CST
4900 || (TREE_CODE (value) == INTEGER_CST
4901 && !((TREE_CODE (expr) == INTEGER_CST
4902 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4903 || TREE_CODE (expr) == REAL_CST
4904 || TREE_CODE (expr) == COMPLEX_CST)))
4905 value = build1 (NOP_EXPR, type, value);
4907 if (CAN_HAVE_LOCATION_P (value))
4908 SET_EXPR_LOCATION (value, loc);
4909 return value;
4912 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4913 location of the open paren of the cast, or the position of the cast
4914 expr. */
4915 tree
4916 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4918 tree type;
4919 tree type_expr = NULL_TREE;
4920 bool type_expr_const = true;
4921 tree ret;
4922 int saved_wsp = warn_strict_prototypes;
4924 /* This avoids warnings about unprototyped casts on
4925 integers. E.g. "#define SIG_DFL (void(*)())0". */
4926 if (TREE_CODE (expr) == INTEGER_CST)
4927 warn_strict_prototypes = 0;
4928 type = groktypename (type_name, &type_expr, &type_expr_const);
4929 warn_strict_prototypes = saved_wsp;
4931 ret = build_c_cast (loc, type, expr);
4932 if (type_expr)
4934 bool inner_expr_const = true;
4935 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
4936 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4937 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
4938 && inner_expr_const);
4939 SET_EXPR_LOCATION (ret, loc);
4942 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4943 SET_EXPR_LOCATION (ret, loc);
4945 /* C++ does not permits types to be defined in a cast, but it
4946 allows references to incomplete types. */
4947 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4948 warning_at (loc, OPT_Wc___compat,
4949 "defining a type in a cast is invalid in C++");
4951 return ret;
4954 /* Build an assignment expression of lvalue LHS from value RHS.
4955 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4956 may differ from TREE_TYPE (LHS) for an enum bitfield.
4957 MODIFYCODE is the code for a binary operator that we use
4958 to combine the old value of LHS with RHS to get the new value.
4959 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4960 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4961 which may differ from TREE_TYPE (RHS) for an enum value.
4963 LOCATION is the location of the MODIFYCODE operator.
4964 RHS_LOC is the location of the RHS. */
4966 tree
4967 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4968 enum tree_code modifycode,
4969 location_t rhs_loc, tree rhs, tree rhs_origtype)
4971 tree result;
4972 tree newrhs;
4973 tree rhs_semantic_type = NULL_TREE;
4974 tree lhstype = TREE_TYPE (lhs);
4975 tree olhstype = lhstype;
4976 bool npc;
4978 /* Types that aren't fully specified cannot be used in assignments. */
4979 lhs = require_complete_type (lhs);
4981 /* Avoid duplicate error messages from operands that had errors. */
4982 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4983 return error_mark_node;
4985 /* For ObjC properties, defer this check. */
4986 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
4987 return error_mark_node;
4989 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4991 rhs_semantic_type = TREE_TYPE (rhs);
4992 rhs = TREE_OPERAND (rhs, 0);
4995 newrhs = rhs;
4997 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4999 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5000 lhs_origtype, modifycode, rhs_loc, rhs,
5001 rhs_origtype);
5002 if (inner == error_mark_node)
5003 return error_mark_node;
5004 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5005 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5006 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5007 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5008 protected_set_expr_location (result, location);
5009 return result;
5012 /* If a binary op has been requested, combine the old LHS value with the RHS
5013 producing the value we should actually store into the LHS. */
5015 if (modifycode != NOP_EXPR)
5017 lhs = c_fully_fold (lhs, false, NULL);
5018 lhs = stabilize_reference (lhs);
5019 newrhs = build_binary_op (location,
5020 modifycode, lhs, rhs, 1);
5022 /* The original type of the right hand side is no longer
5023 meaningful. */
5024 rhs_origtype = NULL_TREE;
5027 if (c_dialect_objc ())
5029 /* Check if we are modifying an Objective-C property reference;
5030 if so, we need to generate setter calls. */
5031 result = objc_maybe_build_modify_expr (lhs, newrhs);
5032 if (result)
5033 return result;
5035 /* Else, do the check that we postponed for Objective-C. */
5036 if (!lvalue_or_else (location, lhs, lv_assign))
5037 return error_mark_node;
5040 /* Give an error for storing in something that is 'const'. */
5042 if (TYPE_READONLY (lhstype)
5043 || ((TREE_CODE (lhstype) == RECORD_TYPE
5044 || TREE_CODE (lhstype) == UNION_TYPE)
5045 && C_TYPE_FIELDS_READONLY (lhstype)))
5047 readonly_error (lhs, lv_assign);
5048 return error_mark_node;
5050 else if (TREE_READONLY (lhs))
5051 readonly_warning (lhs, lv_assign);
5053 /* If storing into a structure or union member,
5054 it has probably been given type `int'.
5055 Compute the type that would go with
5056 the actual amount of storage the member occupies. */
5058 if (TREE_CODE (lhs) == COMPONENT_REF
5059 && (TREE_CODE (lhstype) == INTEGER_TYPE
5060 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5061 || TREE_CODE (lhstype) == REAL_TYPE
5062 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5063 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5065 /* If storing in a field that is in actuality a short or narrower than one,
5066 we must store in the field in its actual type. */
5068 if (lhstype != TREE_TYPE (lhs))
5070 lhs = copy_node (lhs);
5071 TREE_TYPE (lhs) = lhstype;
5074 /* Issue -Wc++-compat warnings about an assignment to an enum type
5075 when LHS does not have its original type. This happens for,
5076 e.g., an enum bitfield in a struct. */
5077 if (warn_cxx_compat
5078 && lhs_origtype != NULL_TREE
5079 && lhs_origtype != lhstype
5080 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5082 tree checktype = (rhs_origtype != NULL_TREE
5083 ? rhs_origtype
5084 : TREE_TYPE (rhs));
5085 if (checktype != error_mark_node
5086 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
5087 warning_at (location, OPT_Wc___compat,
5088 "enum conversion in assignment is invalid in C++");
5091 /* Convert new value to destination type. Fold it first, then
5092 restore any excess precision information, for the sake of
5093 conversion warnings. */
5095 npc = null_pointer_constant_p (newrhs);
5096 newrhs = c_fully_fold (newrhs, false, NULL);
5097 if (rhs_semantic_type)
5098 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5100 /* If the lhs is UPC 'shared' qualified, we drop the qualifier
5101 for the purposes of conversions from rhstype to lhstype.
5102 This will prevent the inadvertent creation of temporaries
5103 with "shared asserted. */
5104 if (upc_shared_type_p (lhstype))
5105 lhstype = build_upc_unshared_type (lhstype);
5107 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
5108 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
5109 if (TREE_CODE (newrhs) == ERROR_MARK)
5110 return error_mark_node;
5112 /* Emit ObjC write barrier, if necessary. */
5113 if (c_dialect_objc () && flag_objc_gc)
5115 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5116 if (result)
5118 protected_set_expr_location (result, location);
5119 return result;
5123 /* Scan operands. */
5125 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5126 TREE_SIDE_EFFECTS (result) = 1;
5127 protected_set_expr_location (result, location);
5129 /* If we got the LHS in a different type for storing in,
5130 convert the result back to the nominal type of LHS
5131 so that the value we return always has the same type
5132 as the LHS argument. */
5134 if (olhstype == TREE_TYPE (result))
5135 return result;
5137 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5138 ic_assign, false, NULL_TREE, NULL_TREE, 0);
5139 protected_set_expr_location (result, location);
5140 return result;
5143 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5144 This is used to implement -fplan9-extensions. */
5146 static bool
5147 find_anonymous_field_with_type (tree struct_type, tree type)
5149 tree field;
5150 bool found;
5152 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5153 || TREE_CODE (struct_type) == UNION_TYPE);
5154 found = false;
5155 for (field = TYPE_FIELDS (struct_type);
5156 field != NULL_TREE;
5157 field = TREE_CHAIN (field))
5159 if (DECL_NAME (field) == NULL
5160 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5162 if (found)
5163 return false;
5164 found = true;
5166 else if (DECL_NAME (field) == NULL
5167 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5168 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5169 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5171 if (found)
5172 return false;
5173 found = true;
5176 return found;
5179 /* RHS is an expression whose type is pointer to struct. If there is
5180 an anonymous field in RHS with type TYPE, then return a pointer to
5181 that field in RHS. This is used with -fplan9-extensions. This
5182 returns NULL if no conversion could be found. */
5184 static tree
5185 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5187 tree rhs_struct_type, lhs_main_type;
5188 tree field, found_field;
5189 bool found_sub_field;
5190 tree ret;
5192 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5193 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5194 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5195 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5197 gcc_assert (POINTER_TYPE_P (type));
5198 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5200 found_field = NULL_TREE;
5201 found_sub_field = false;
5202 for (field = TYPE_FIELDS (rhs_struct_type);
5203 field != NULL_TREE;
5204 field = TREE_CHAIN (field))
5206 if (DECL_NAME (field) != NULL_TREE
5207 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5208 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5209 continue;
5210 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5212 if (found_field != NULL_TREE)
5213 return NULL_TREE;
5214 found_field = field;
5216 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5217 lhs_main_type))
5219 if (found_field != NULL_TREE)
5220 return NULL_TREE;
5221 found_field = field;
5222 found_sub_field = true;
5226 if (found_field == NULL_TREE)
5227 return NULL_TREE;
5229 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5230 build_fold_indirect_ref (rhs), found_field,
5231 NULL_TREE);
5232 ret = build_fold_addr_expr_loc (location, ret);
5234 if (found_sub_field)
5236 ret = convert_to_anonymous_field (location, type, ret);
5237 gcc_assert (ret != NULL_TREE);
5240 return ret;
5243 /* Convert value RHS to type TYPE as preparation for an assignment to
5244 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5245 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5246 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5247 constant before any folding.
5248 The real work of conversion is done by `convert'.
5249 The purpose of this function is to generate error messages
5250 for assignments that are not allowed in C.
5251 ERRTYPE says whether it is argument passing, assignment,
5252 initialization or return.
5254 LOCATION is the location of the RHS.
5255 FUNCTION is a tree for the function being called.
5256 PARMNUM is the number of the argument, for printing in error messages. */
5258 static tree
5259 convert_for_assignment (location_t location, tree type, tree rhs,
5260 tree origtype, enum impl_conv errtype,
5261 bool null_pointer_constant, tree fundecl,
5262 tree function, int parmnum)
5264 enum tree_code codel = TREE_CODE (type);
5265 tree orig_rhs = rhs;
5266 tree rhstype;
5267 enum tree_code coder;
5268 tree rname = NULL_TREE;
5269 bool objc_ok = false;
5271 if (errtype == ic_argpass)
5273 tree selector;
5274 /* Change pointer to function to the function itself for
5275 diagnostics. */
5276 if (TREE_CODE (function) == ADDR_EXPR
5277 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5278 function = TREE_OPERAND (function, 0);
5280 /* Handle an ObjC selector specially for diagnostics. */
5281 selector = objc_message_selector ();
5282 rname = function;
5283 if (selector && parmnum > 2)
5285 rname = selector;
5286 parmnum -= 2;
5290 /* This macro is used to emit diagnostics to ensure that all format
5291 strings are complete sentences, visible to gettext and checked at
5292 compile time. */
5293 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5294 do { \
5295 switch (errtype) \
5297 case ic_argpass: \
5298 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5299 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5300 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5301 "expected %qT but argument is of type %qT", \
5302 type, rhstype); \
5303 break; \
5304 case ic_assign: \
5305 pedwarn (LOCATION, OPT, AS); \
5306 break; \
5307 case ic_init: \
5308 pedwarn_init (LOCATION, OPT, IN); \
5309 break; \
5310 case ic_return: \
5311 pedwarn (LOCATION, OPT, RE); \
5312 break; \
5313 default: \
5314 gcc_unreachable (); \
5316 } while (0)
5318 /* Similar to WARN_FOR_ASSIGNMENT, but used to diagnose certain
5319 error conditions defined by the UPC language specification
5320 when converting between pointer-to-shared types and other types. */
5321 #define ERROR_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5322 do { \
5323 switch (errtype) \
5325 case ic_argpass: \
5326 error_at (LOCATION, AR, parmnum, rname); \
5327 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5328 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5329 "expected %qT but argument is of type %qT", \
5330 type, rhstype); \
5331 break; \
5332 case ic_assign: \
5333 error_at (LOCATION, AS); \
5334 break; \
5335 case ic_init: \
5336 error_at (LOCATION, IN); \
5337 break; \
5338 case ic_return: \
5339 error_at (LOCATION, RE); \
5340 break; \
5341 default: \
5342 gcc_unreachable (); \
5344 } while (0)
5346 /* This macro is used to emit diagnostics to ensure that all format
5347 strings are complete sentences, visible to gettext and checked at
5348 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5349 extra parameter to enumerate qualifiers. */
5351 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5352 do { \
5353 switch (errtype) \
5355 case ic_argpass: \
5356 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5357 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5358 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5359 "expected %qT but argument is of type %qT", \
5360 type, rhstype); \
5361 break; \
5362 case ic_assign: \
5363 pedwarn (LOCATION, OPT, AS, QUALS); \
5364 break; \
5365 case ic_init: \
5366 pedwarn (LOCATION, OPT, IN, QUALS); \
5367 break; \
5368 case ic_return: \
5369 pedwarn (LOCATION, OPT, RE, QUALS); \
5370 break; \
5371 default: \
5372 gcc_unreachable (); \
5374 } while (0)
5376 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5377 rhs = TREE_OPERAND (rhs, 0);
5379 rhstype = TREE_TYPE (rhs);
5380 coder = TREE_CODE (rhstype);
5382 if (coder == ERROR_MARK)
5383 return error_mark_node;
5385 if (c_dialect_objc ())
5387 int parmno;
5389 switch (errtype)
5391 case ic_return:
5392 parmno = 0;
5393 break;
5395 case ic_assign:
5396 parmno = -1;
5397 break;
5399 case ic_init:
5400 parmno = -2;
5401 break;
5403 default:
5404 parmno = parmnum;
5405 break;
5408 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5411 if (warn_cxx_compat)
5413 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5414 if (checktype != error_mark_node
5415 && TREE_CODE (type) == ENUMERAL_TYPE
5416 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5418 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5419 G_("enum conversion when passing argument "
5420 "%d of %qE is invalid in C++"),
5421 G_("enum conversion in assignment is "
5422 "invalid in C++"),
5423 G_("enum conversion in initialization is "
5424 "invalid in C++"),
5425 G_("enum conversion in return is "
5426 "invalid in C++"));
5430 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5431 return rhs;
5433 if (coder == VOID_TYPE)
5435 /* Except for passing an argument to an unprototyped function,
5436 this is a constraint violation. When passing an argument to
5437 an unprototyped function, it is compile-time undefined;
5438 making it a constraint in that case was rejected in
5439 DR#252. */
5440 error_at (location, "void value not ignored as it ought to be");
5441 return error_mark_node;
5443 rhs = require_complete_type (rhs);
5444 if (rhs == error_mark_node)
5445 return error_mark_node;
5446 /* A type converts to a reference to it.
5447 This code doesn't fully support references, it's just for the
5448 special case of va_start and va_copy. */
5449 if (codel == REFERENCE_TYPE
5450 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5452 if (!lvalue_p (rhs))
5454 error_at (location, "cannot pass rvalue to reference parameter");
5455 return error_mark_node;
5457 if (!c_mark_addressable (rhs))
5458 return error_mark_node;
5459 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5460 SET_EXPR_LOCATION (rhs, location);
5462 /* We already know that these two types are compatible, but they
5463 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5464 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5465 likely to be va_list, a typedef to __builtin_va_list, which
5466 is different enough that it will cause problems later. */
5467 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5469 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5470 SET_EXPR_LOCATION (rhs, location);
5473 rhs = build1 (NOP_EXPR, type, rhs);
5474 SET_EXPR_LOCATION (rhs, location);
5475 return rhs;
5477 /* Some types can interconvert without explicit casts. */
5478 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5479 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5480 return convert (type, rhs);
5481 /* Arithmetic types all interconvert, and enum is treated like int. */
5482 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5483 || codel == FIXED_POINT_TYPE
5484 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5485 || codel == BOOLEAN_TYPE)
5486 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5487 || coder == FIXED_POINT_TYPE
5488 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5489 || coder == BOOLEAN_TYPE))
5491 tree ret;
5492 bool save = in_late_binary_op;
5493 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5494 in_late_binary_op = true;
5495 ret = convert_and_check (type, orig_rhs);
5496 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5497 in_late_binary_op = save;
5498 return ret;
5501 /* Aggregates in different TUs might need conversion. */
5502 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5503 && codel == coder
5504 && comptypes (type, rhstype))
5505 return convert_and_check (type, rhs);
5507 /* Conversion to a transparent union or record from its member types.
5508 This applies only to function arguments. */
5509 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5510 && TYPE_TRANSPARENT_AGGR (type))
5511 && errtype == ic_argpass)
5513 tree memb, marginal_memb = NULL_TREE;
5515 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5517 tree memb_type = TREE_TYPE (memb);
5519 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5520 TYPE_MAIN_VARIANT (rhstype)))
5521 break;
5523 if (TREE_CODE (memb_type) != POINTER_TYPE)
5524 continue;
5526 if (coder == POINTER_TYPE)
5528 tree ttl = TREE_TYPE (memb_type);
5529 tree ttr = TREE_TYPE (rhstype);
5531 /* Any non-function converts to a [const][volatile] void *
5532 and vice versa; otherwise, targets must be the same.
5533 Meanwhile, the lhs target must have all the qualifiers of
5534 the rhs. */
5535 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5536 || comp_target_types (location, memb_type, rhstype))
5538 /* If this type won't generate any warnings, use it. */
5539 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5540 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5541 && TREE_CODE (ttl) == FUNCTION_TYPE)
5542 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5543 == TYPE_QUALS (ttr))
5544 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5545 == TYPE_QUALS (ttl))))
5546 break;
5548 /* Keep looking for a better type, but remember this one. */
5549 if (!marginal_memb)
5550 marginal_memb = memb;
5554 /* Can convert integer zero to any pointer type. */
5555 if (null_pointer_constant)
5557 tree ttl = TREE_TYPE (memb_type);
5558 rhs = !upc_shared_type_p (ttl)
5559 ? null_pointer_node : upc_null_pts_node;
5560 break;
5564 if (memb || marginal_memb)
5566 if (!memb)
5568 /* We have only a marginally acceptable member type;
5569 it needs a warning. */
5570 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5571 tree ttr = TREE_TYPE (rhstype);
5573 /* Const and volatile mean something different for function
5574 types, so the usual warnings are not appropriate. */
5575 if (TREE_CODE (ttr) == FUNCTION_TYPE
5576 && TREE_CODE (ttl) == FUNCTION_TYPE)
5578 /* Because const and volatile on functions are
5579 restrictions that say the function will not do
5580 certain things, it is okay to use a const or volatile
5581 function where an ordinary one is wanted, but not
5582 vice-versa. */
5583 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5584 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5585 WARN_FOR_QUALIFIERS (location, 0,
5586 G_("passing argument %d of %qE "
5587 "makes %q#v qualified function "
5588 "pointer from unqualified"),
5589 G_("assignment makes %q#v qualified "
5590 "function pointer from "
5591 "unqualified"),
5592 G_("initialization makes %q#v qualified "
5593 "function pointer from "
5594 "unqualified"),
5595 G_("return makes %q#v qualified function "
5596 "pointer from unqualified"),
5597 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5599 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5600 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5601 WARN_FOR_QUALIFIERS (location, 0,
5602 G_("passing argument %d of %qE discards "
5603 "%qv qualifier from pointer target type"),
5604 G_("assignment discards %qv qualifier "
5605 "from pointer target type"),
5606 G_("initialization discards %qv qualifier "
5607 "from pointer target type"),
5608 G_("return discards %qv qualifier from "
5609 "pointer target type"),
5610 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5612 memb = marginal_memb;
5615 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5616 pedwarn (location, OPT_Wpedantic,
5617 "ISO C prohibits argument conversion to union type");
5619 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5620 return build_constructor_single (type, memb, rhs);
5624 /* Conversions among pointers */
5625 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5626 && (coder == codel))
5628 tree ttl = TREE_TYPE (type);
5629 tree ttr = TREE_TYPE (rhstype);
5630 tree mvl = ttl;
5631 tree mvr = ttr;
5632 bool is_opaque_pointer;
5633 int target_cmp = 0; /* Cache comp_target_types () result. */
5634 addr_space_t asl;
5635 addr_space_t asr;
5637 if (TREE_CODE (mvl) != ARRAY_TYPE)
5638 mvl = TYPE_MAIN_VARIANT (mvl);
5639 if (TREE_CODE (mvr) != ARRAY_TYPE)
5640 mvr = TYPE_MAIN_VARIANT (mvr);
5641 if ((upc_shared_type_p (ttl) && !upc_shared_type_p (ttr))
5642 && !integer_zerop (rhs))
5644 error_at (location, "UPC does not allow assignments from a local pointer "
5645 "to a pointer-to-shared");
5646 return error_mark_node;
5648 if (!upc_shared_type_p (ttl) && upc_shared_type_p (ttr))
5650 if (upc_is_null_pts_p (rhs))
5652 return null_pointer_node;
5654 else
5656 error_at (location, "UPC does not allow assignments "
5657 "from a pointer-to-shared to a local pointer");
5658 return error_mark_node;
5661 if (upc_shared_type_p (ttl) && upc_shared_type_p (ttr) && (ttl != ttr)
5662 && !(VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)))
5664 const tree bs_l = upc_get_block_factor (ttl);
5665 const tree bs_r = upc_get_block_factor (ttr);
5666 /* Both source and destination are non-void pointers to shared,
5667 whose target types are not equal.
5668 UPC dictates that their blocking factors must be equal. */
5669 if (!tree_int_cst_equal (bs_l, bs_r))
5671 error_at (location, "UPC does not allow assignment "
5672 "between pointers to shared with "
5673 "differing block sizes without a cast");
5674 return error_mark_node;
5678 /* Opaque pointers are treated like void pointers. */
5679 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5681 /* The Plan 9 compiler permits a pointer to a struct to be
5682 automatically converted into a pointer to an anonymous field
5683 within the struct. */
5684 if (flag_plan9_extensions
5685 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5686 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5687 && mvl != mvr)
5689 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5690 if (new_rhs != NULL_TREE)
5692 rhs = new_rhs;
5693 rhstype = TREE_TYPE (rhs);
5694 coder = TREE_CODE (rhstype);
5695 ttr = TREE_TYPE (rhstype);
5696 mvr = TYPE_MAIN_VARIANT (ttr);
5700 /* C++ does not allow the implicit conversion void* -> T*. However,
5701 for the purpose of reducing the number of false positives, we
5702 tolerate the special case of
5704 int *p = NULL;
5706 where NULL is typically defined in C to be '(void *) 0'. */
5707 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5708 warning_at (location, OPT_Wc___compat,
5709 "request for implicit conversion "
5710 "from %qT to %qT not permitted in C++", rhstype, type);
5712 /* See if the pointers point to incompatible address spaces. */
5713 asl = TYPE_ADDR_SPACE (ttl);
5714 asr = TYPE_ADDR_SPACE (ttr);
5715 if (!null_pointer_constant_p (rhs)
5716 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5718 switch (errtype)
5720 case ic_argpass:
5721 error_at (location, "passing argument %d of %qE from pointer to "
5722 "non-enclosed address space", parmnum, rname);
5723 break;
5724 case ic_assign:
5725 error_at (location, "assignment from pointer to "
5726 "non-enclosed address space");
5727 break;
5728 case ic_init:
5729 error_at (location, "initialization from pointer to "
5730 "non-enclosed address space");
5731 break;
5732 case ic_return:
5733 error_at (location, "return from pointer to "
5734 "non-enclosed address space");
5735 break;
5736 default:
5737 gcc_unreachable ();
5739 return error_mark_node;
5742 /* Check if the right-hand side has a format attribute but the
5743 left-hand side doesn't. */
5744 if (warn_suggest_attribute_format
5745 && check_missing_format_attribute (type, rhstype))
5747 switch (errtype)
5749 case ic_argpass:
5750 warning_at (location, OPT_Wsuggest_attribute_format,
5751 "argument %d of %qE might be "
5752 "a candidate for a format attribute",
5753 parmnum, rname);
5754 break;
5755 case ic_assign:
5756 warning_at (location, OPT_Wsuggest_attribute_format,
5757 "assignment left-hand side might be "
5758 "a candidate for a format attribute");
5759 break;
5760 case ic_init:
5761 warning_at (location, OPT_Wsuggest_attribute_format,
5762 "initialization left-hand side might be "
5763 "a candidate for a format attribute");
5764 break;
5765 case ic_return:
5766 warning_at (location, OPT_Wsuggest_attribute_format,
5767 "return type might be "
5768 "a candidate for a format attribute");
5769 break;
5770 default:
5771 gcc_unreachable ();
5775 /* Any non-function converts to a [const][volatile] void *
5776 and vice versa; otherwise, targets must be the same.
5777 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5778 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5779 || (target_cmp = comp_target_types (location, type, rhstype))
5780 || is_opaque_pointer
5781 || ((c_common_unsigned_type (mvl)
5782 == c_common_unsigned_type (mvr))
5783 && c_common_signed_type (mvl)
5784 == c_common_signed_type (mvr)))
5786 if (pedantic
5787 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5789 (VOID_TYPE_P (ttr)
5790 && !null_pointer_constant
5791 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5792 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
5793 G_("ISO C forbids passing argument %d of "
5794 "%qE between function pointer "
5795 "and %<void *%>"),
5796 G_("ISO C forbids assignment between "
5797 "function pointer and %<void *%>"),
5798 G_("ISO C forbids initialization between "
5799 "function pointer and %<void *%>"),
5800 G_("ISO C forbids return between function "
5801 "pointer and %<void *%>"));
5802 /* Const and volatile mean something different for function types,
5803 so the usual warnings are not appropriate. */
5804 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5805 && TREE_CODE (ttl) != FUNCTION_TYPE)
5807 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5808 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5810 WARN_FOR_QUALIFIERS (location, 0,
5811 G_("passing argument %d of %qE discards "
5812 "%qv qualifier from pointer target type"),
5813 G_("assignment discards %qv qualifier "
5814 "from pointer target type"),
5815 G_("initialization discards %qv qualifier "
5816 "from pointer target type"),
5817 G_("return discards %qv qualifier from "
5818 "pointer target type"),
5819 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5821 /* If this is not a case of ignoring a mismatch in signedness,
5822 no warning. */
5823 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5824 || target_cmp)
5826 /* If there is a mismatch, do warn. */
5827 else if (warn_pointer_sign)
5828 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5829 G_("pointer targets in passing argument "
5830 "%d of %qE differ in signedness"),
5831 G_("pointer targets in assignment "
5832 "differ in signedness"),
5833 G_("pointer targets in initialization "
5834 "differ in signedness"),
5835 G_("pointer targets in return differ "
5836 "in signedness"));
5838 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5839 && TREE_CODE (ttr) == FUNCTION_TYPE)
5841 /* Because const and volatile on functions are restrictions
5842 that say the function will not do certain things,
5843 it is okay to use a const or volatile function
5844 where an ordinary one is wanted, but not vice-versa. */
5845 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5846 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5847 WARN_FOR_QUALIFIERS (location, 0,
5848 G_("passing argument %d of %qE makes "
5849 "%q#v qualified function pointer "
5850 "from unqualified"),
5851 G_("assignment makes %q#v qualified function "
5852 "pointer from unqualified"),
5853 G_("initialization makes %q#v qualified "
5854 "function pointer from unqualified"),
5855 G_("return makes %q#v qualified function "
5856 "pointer from unqualified"),
5857 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5860 else
5861 /* Avoid warning about the volatile ObjC EH puts on decls. */
5862 if (!objc_ok)
5863 WARN_FOR_ASSIGNMENT (location, 0,
5864 G_("passing argument %d of %qE from "
5865 "incompatible pointer type"),
5866 G_("assignment from incompatible pointer type"),
5867 G_("initialization from incompatible "
5868 "pointer type"),
5869 G_("return from incompatible pointer type"));
5871 return convert (type, rhs);
5873 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5875 /* ??? This should not be an error when inlining calls to
5876 unprototyped functions. */
5877 error_at (location, "invalid use of non-lvalue array");
5878 return error_mark_node;
5880 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5882 /* An explicit constant 0 can convert to a pointer,
5883 or one that results from arithmetic, even including
5884 a cast to integer type. */
5885 if (!null_pointer_constant)
5887 if (upc_shared_type_p (TREE_TYPE (type)))
5888 ERROR_FOR_ASSIGNMENT (location, 0,
5889 G_("passing argument %d of %qE attempts to make "
5890 "a UPC pointer-to-shared value from an integer"),
5891 G_("assignment attempts to make a UPC pointer-to-shared "
5892 "value from an integer"),
5893 G_("initialization attempts to make a UPC pointer-to-shared "
5894 "value from an integer without a cast"),
5895 G_("return makes a UPC pointer-to-shared value from an "
5896 "integer"));
5897 else
5898 WARN_FOR_ASSIGNMENT (location, 0,
5899 G_("passing argument %d of %qE makes "
5900 "pointer from integer without a cast"),
5901 G_("assignment makes pointer from integer "
5902 "without a cast"),
5903 G_("initialization makes pointer from "
5904 "integer without a cast"),
5905 G_("return makes pointer from integer "
5906 "without a cast"));
5909 return convert (type, rhs);
5911 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5913 WARN_FOR_ASSIGNMENT (location, 0,
5914 G_("passing argument %d of %qE makes integer "
5915 "from pointer without a cast"),
5916 G_("assignment makes integer from pointer "
5917 "without a cast"),
5918 G_("initialization makes integer from pointer "
5919 "without a cast"),
5920 G_("return makes integer from pointer "
5921 "without a cast"));
5922 return convert (type, rhs);
5924 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5926 tree ret;
5927 bool save = in_late_binary_op;
5928 in_late_binary_op = true;
5929 ret = convert (type, rhs);
5930 in_late_binary_op = save;
5931 return ret;
5934 switch (errtype)
5936 case ic_argpass:
5937 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5938 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5939 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5940 "expected %qT but argument is of type %qT", type, rhstype);
5941 break;
5942 case ic_assign:
5943 error_at (location, "incompatible types when assigning to type %qT from "
5944 "type %qT", type, rhstype);
5945 break;
5946 case ic_init:
5947 error_at (location,
5948 "incompatible types when initializing type %qT using type %qT",
5949 type, rhstype);
5950 break;
5951 case ic_return:
5952 error_at (location,
5953 "incompatible types when returning type %qT but %qT was "
5954 "expected", rhstype, type);
5955 break;
5956 default:
5957 gcc_unreachable ();
5960 return error_mark_node;
5963 /* If VALUE is a compound expr all of whose expressions are constant, then
5964 return its value. Otherwise, return error_mark_node.
5966 This is for handling COMPOUND_EXPRs as initializer elements
5967 which is allowed with a warning when -pedantic is specified. */
5969 static tree
5970 valid_compound_expr_initializer (tree value, tree endtype)
5972 if (TREE_CODE (value) == COMPOUND_EXPR)
5974 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5975 == error_mark_node)
5976 return error_mark_node;
5977 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5978 endtype);
5980 else if (!initializer_constant_valid_p (value, endtype))
5981 return error_mark_node;
5982 else
5983 return value;
5986 /* Perform appropriate conversions on the initial value of a variable,
5987 store it in the declaration DECL,
5988 and print any error messages that are appropriate.
5989 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5990 If the init is invalid, store an ERROR_MARK.
5992 INIT_LOC is the location of the initial value. */
5994 void
5995 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5997 const bool npc = init && null_pointer_constant_p (init);
5998 const bool is_upc_decl_init = upc_check_decl_init (decl, init);
5999 const bool require_constant = TREE_STATIC (decl) && !is_upc_decl_init;
6000 tree type = TREE_TYPE (decl);
6001 tree value;
6003 /* If variable's type was invalidly declared, just ignore it. */
6005 if (TREE_CODE (type) == ERROR_MARK)
6006 return;
6008 /* Digest the specified initializer into an expression. */
6010 value = digest_init (init_loc, type, init, origtype, npc,
6011 true, require_constant);
6013 /* UPC cannot initialize certain values at compile time.
6014 For example, the address of a UPC 'shared' variable must
6015 be evaluated at runtime. */
6017 if (is_upc_decl_init)
6019 upc_decl_init (decl, value);
6020 return;
6023 /* Store the expression if valid; else report error. */
6025 if (!in_system_header
6026 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6027 warning (OPT_Wtraditional, "traditional C rejects automatic "
6028 "aggregate initialization");
6030 DECL_INITIAL (decl) = value;
6032 /* ANSI wants warnings about out-of-range constant initializers. */
6033 STRIP_TYPE_NOPS (value);
6034 if (TREE_STATIC (decl))
6035 constant_expression_warning (value);
6037 /* Check if we need to set array size from compound literal size. */
6038 if (TREE_CODE (type) == ARRAY_TYPE
6039 && TYPE_DOMAIN (type) == 0
6040 && value != error_mark_node)
6042 tree inside_init = init;
6044 STRIP_TYPE_NOPS (inside_init);
6045 inside_init = fold (inside_init);
6047 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6049 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6051 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6053 /* For int foo[] = (int [3]){1}; we need to set array size
6054 now since later on array initializer will be just the
6055 brace enclosed list of the compound literal. */
6056 tree etype = strip_array_types (TREE_TYPE (decl));
6057 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6058 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6059 layout_type (type);
6060 layout_decl (cldecl, 0);
6061 TREE_TYPE (decl)
6062 = c_build_qualified_type (type, TYPE_QUALS (etype));
6068 /* Methods for storing and printing names for error messages. */
6070 /* Implement a spelling stack that allows components of a name to be pushed
6071 and popped. Each element on the stack is this structure. */
6073 struct spelling
6075 int kind;
6076 union
6078 unsigned HOST_WIDE_INT i;
6079 const char *s;
6080 } u;
6083 #define SPELLING_STRING 1
6084 #define SPELLING_MEMBER 2
6085 #define SPELLING_BOUNDS 3
6087 static struct spelling *spelling; /* Next stack element (unused). */
6088 static struct spelling *spelling_base; /* Spelling stack base. */
6089 static int spelling_size; /* Size of the spelling stack. */
6091 /* Macros to save and restore the spelling stack around push_... functions.
6092 Alternative to SAVE_SPELLING_STACK. */
6094 #define SPELLING_DEPTH() (spelling - spelling_base)
6095 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6097 /* Push an element on the spelling stack with type KIND and assign VALUE
6098 to MEMBER. */
6100 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6102 int depth = SPELLING_DEPTH (); \
6104 if (depth >= spelling_size) \
6106 spelling_size += 10; \
6107 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6108 spelling_size); \
6109 RESTORE_SPELLING_DEPTH (depth); \
6112 spelling->kind = (KIND); \
6113 spelling->MEMBER = (VALUE); \
6114 spelling++; \
6117 /* Push STRING on the stack. Printed literally. */
6119 static void
6120 push_string (const char *string)
6122 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6125 /* Push a member name on the stack. Printed as '.' STRING. */
6127 static void
6128 push_member_name (tree decl)
6130 const char *const string
6131 = (DECL_NAME (decl)
6132 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6133 : _("<anonymous>"));
6134 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6137 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6139 static void
6140 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6142 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6145 /* Compute the maximum size in bytes of the printed spelling. */
6147 static int
6148 spelling_length (void)
6150 int size = 0;
6151 struct spelling *p;
6153 for (p = spelling_base; p < spelling; p++)
6155 if (p->kind == SPELLING_BOUNDS)
6156 size += 25;
6157 else
6158 size += strlen (p->u.s) + 1;
6161 return size;
6164 /* Print the spelling to BUFFER and return it. */
6166 static char *
6167 print_spelling (char *buffer)
6169 char *d = buffer;
6170 struct spelling *p;
6172 for (p = spelling_base; p < spelling; p++)
6173 if (p->kind == SPELLING_BOUNDS)
6175 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6176 d += strlen (d);
6178 else
6180 const char *s;
6181 if (p->kind == SPELLING_MEMBER)
6182 *d++ = '.';
6183 for (s = p->u.s; (*d = *s++); d++)
6186 *d++ = '\0';
6187 return buffer;
6190 /* Issue an error message for a bad initializer component.
6191 GMSGID identifies the message.
6192 The component name is taken from the spelling stack. */
6194 void
6195 error_init (const char *gmsgid)
6197 char *ofwhat;
6199 /* The gmsgid may be a format string with %< and %>. */
6200 error (gmsgid);
6201 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6202 if (*ofwhat)
6203 error ("(near initialization for %qs)", ofwhat);
6206 /* Issue a pedantic warning for a bad initializer component. OPT is
6207 the option OPT_* (from options.h) controlling this warning or 0 if
6208 it is unconditionally given. GMSGID identifies the message. The
6209 component name is taken from the spelling stack. */
6211 void
6212 pedwarn_init (location_t location, int opt, const char *gmsgid)
6214 char *ofwhat;
6216 /* The gmsgid may be a format string with %< and %>. */
6217 pedwarn (location, opt, gmsgid);
6218 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6219 if (*ofwhat)
6220 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
6223 /* Issue a warning for a bad initializer component.
6225 OPT is the OPT_W* value corresponding to the warning option that
6226 controls this warning. GMSGID identifies the message. The
6227 component name is taken from the spelling stack. */
6229 static void
6230 warning_init (int opt, const char *gmsgid)
6232 char *ofwhat;
6234 /* The gmsgid may be a format string with %< and %>. */
6235 warning (opt, gmsgid);
6236 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6237 if (*ofwhat)
6238 warning (opt, "(near initialization for %qs)", ofwhat);
6241 /* If TYPE is an array type and EXPR is a parenthesized string
6242 constant, warn if pedantic that EXPR is being used to initialize an
6243 object of type TYPE. */
6245 void
6246 maybe_warn_string_init (tree type, struct c_expr expr)
6248 if (pedantic
6249 && TREE_CODE (type) == ARRAY_TYPE
6250 && TREE_CODE (expr.value) == STRING_CST
6251 && expr.original_code != STRING_CST)
6252 pedwarn_init (input_location, OPT_Wpedantic,
6253 "array initialized from parenthesized string constant");
6256 /* Digest the parser output INIT as an initializer for type TYPE.
6257 Return a C expression of type TYPE to represent the initial value.
6259 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6261 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6263 If INIT is a string constant, STRICT_STRING is true if it is
6264 unparenthesized or we should not warn here for it being parenthesized.
6265 For other types of INIT, STRICT_STRING is not used.
6267 INIT_LOC is the location of the INIT.
6269 REQUIRE_CONSTANT requests an error if non-constant initializers or
6270 elements are seen. */
6272 static tree
6273 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6274 bool null_pointer_constant, bool strict_string,
6275 int require_constant)
6277 enum tree_code code = TREE_CODE (type);
6278 tree inside_init = init;
6279 tree semantic_type = NULL_TREE;
6280 bool maybe_const = true;
6282 if (type == error_mark_node
6283 || !init
6284 || init == error_mark_node
6285 || TREE_TYPE (init) == error_mark_node)
6286 return error_mark_node;
6288 STRIP_TYPE_NOPS (inside_init);
6290 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6292 semantic_type = TREE_TYPE (inside_init);
6293 inside_init = TREE_OPERAND (inside_init, 0);
6295 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6296 inside_init = decl_constant_value_for_optimization (inside_init);
6298 /* Initialization of an array of chars from a string constant
6299 optionally enclosed in braces. */
6301 if (code == ARRAY_TYPE && inside_init
6302 && TREE_CODE (inside_init) == STRING_CST)
6304 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6305 /* Note that an array could be both an array of character type
6306 and an array of wchar_t if wchar_t is signed char or unsigned
6307 char. */
6308 bool char_array = (typ1 == char_type_node
6309 || typ1 == signed_char_type_node
6310 || typ1 == unsigned_char_type_node);
6311 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6312 bool char16_array = !!comptypes (typ1, char16_type_node);
6313 bool char32_array = !!comptypes (typ1, char32_type_node);
6315 if (char_array || wchar_array || char16_array || char32_array)
6317 struct c_expr expr;
6318 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6319 expr.value = inside_init;
6320 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6321 expr.original_type = NULL;
6322 maybe_warn_string_init (type, expr);
6324 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6325 pedwarn_init (init_loc, OPT_Wpedantic,
6326 "initialization of a flexible array member");
6328 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6329 TYPE_MAIN_VARIANT (type)))
6330 return inside_init;
6332 if (char_array)
6334 if (typ2 != char_type_node)
6336 error_init ("char-array initialized from wide string");
6337 return error_mark_node;
6340 else
6342 if (typ2 == char_type_node)
6344 error_init ("wide character array initialized from non-wide "
6345 "string");
6346 return error_mark_node;
6348 else if (!comptypes(typ1, typ2))
6350 error_init ("wide character array initialized from "
6351 "incompatible wide string");
6352 return error_mark_node;
6356 TREE_TYPE (inside_init) = type;
6357 if (TYPE_DOMAIN (type) != 0
6358 && TYPE_SIZE (type) != 0
6359 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6361 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6363 /* Subtract the size of a single (possibly wide) character
6364 because it's ok to ignore the terminating null char
6365 that is counted in the length of the constant. */
6366 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6367 (len
6368 - (TYPE_PRECISION (typ1)
6369 / BITS_PER_UNIT))))
6370 pedwarn_init (init_loc, 0,
6371 ("initializer-string for array of chars "
6372 "is too long"));
6373 else if (warn_cxx_compat
6374 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6375 warning_at (init_loc, OPT_Wc___compat,
6376 ("initializer-string for array chars "
6377 "is too long for C++"));
6380 return inside_init;
6382 else if (INTEGRAL_TYPE_P (typ1))
6384 error_init ("array of inappropriate type initialized "
6385 "from string constant");
6386 return error_mark_node;
6390 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6391 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6392 below and handle as a constructor. */
6393 if (code == VECTOR_TYPE
6394 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6395 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6396 && TREE_CONSTANT (inside_init))
6398 if (TREE_CODE (inside_init) == VECTOR_CST
6399 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6400 TYPE_MAIN_VARIANT (type)))
6401 return inside_init;
6403 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6405 unsigned HOST_WIDE_INT ix;
6406 tree value;
6407 bool constant_p = true;
6409 /* Iterate through elements and check if all constructor
6410 elements are *_CSTs. */
6411 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6412 if (!CONSTANT_CLASS_P (value))
6414 constant_p = false;
6415 break;
6418 if (constant_p)
6419 return build_vector_from_ctor (type,
6420 CONSTRUCTOR_ELTS (inside_init));
6424 if (warn_sequence_point)
6425 verify_sequence_points (inside_init);
6427 /* Any type can be initialized
6428 from an expression of the same type, optionally with braces. */
6430 if (inside_init && TREE_TYPE (inside_init) != 0
6431 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6432 TYPE_MAIN_VARIANT (type))
6433 || (code == ARRAY_TYPE
6434 && comptypes (TREE_TYPE (inside_init), type))
6435 || (code == VECTOR_TYPE
6436 && comptypes (TREE_TYPE (inside_init), type))
6437 || (code == POINTER_TYPE
6438 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6439 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6440 TREE_TYPE (type)))))
6442 if (code == POINTER_TYPE)
6444 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6446 if (TREE_CODE (inside_init) == STRING_CST
6447 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6448 inside_init = array_to_pointer_conversion
6449 (init_loc, inside_init);
6450 else
6452 error_init ("invalid use of non-lvalue array");
6453 return error_mark_node;
6458 if (code == VECTOR_TYPE)
6459 /* Although the types are compatible, we may require a
6460 conversion. */
6461 inside_init = convert (type, inside_init);
6463 if (require_constant
6464 && (code == VECTOR_TYPE || !flag_isoc99)
6465 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6467 /* As an extension, allow initializing objects with static storage
6468 duration with compound literals (which are then treated just as
6469 the brace enclosed list they contain). Also allow this for
6470 vectors, as we can only assign them with compound literals. */
6471 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6472 inside_init = DECL_INITIAL (decl);
6475 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6476 && TREE_CODE (inside_init) != CONSTRUCTOR)
6478 error_init ("array initialized from non-constant array expression");
6479 return error_mark_node;
6482 /* Compound expressions can only occur here if -Wpedantic or
6483 -pedantic-errors is specified. In the later case, we always want
6484 an error. In the former case, we simply want a warning. */
6485 if (require_constant && pedantic
6486 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6488 inside_init
6489 = valid_compound_expr_initializer (inside_init,
6490 TREE_TYPE (inside_init));
6491 if (inside_init == error_mark_node)
6492 error_init ("initializer element is not constant");
6493 else
6494 pedwarn_init (init_loc, OPT_Wpedantic,
6495 "initializer element is not constant");
6496 if (flag_pedantic_errors)
6497 inside_init = error_mark_node;
6499 else if (require_constant
6500 && !initializer_constant_valid_p (inside_init,
6501 TREE_TYPE (inside_init)))
6503 error_init ("initializer element is not constant");
6504 inside_init = error_mark_node;
6506 else if (require_constant && !maybe_const)
6507 pedwarn_init (init_loc, 0,
6508 "initializer element is not a constant expression");
6510 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6511 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6512 inside_init = convert_for_assignment (init_loc, type, inside_init,
6513 origtype,
6514 ic_init, null_pointer_constant,
6515 NULL_TREE, NULL_TREE, 0);
6516 return inside_init;
6519 /* Handle scalar types, including conversions. */
6521 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6522 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6523 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6525 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6526 && (TREE_CODE (init) == STRING_CST
6527 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6528 inside_init = init = array_to_pointer_conversion (init_loc, init);
6529 if (semantic_type)
6530 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6531 inside_init);
6532 inside_init
6533 = convert_for_assignment (init_loc, type, inside_init, origtype,
6534 ic_init, null_pointer_constant,
6535 NULL_TREE, NULL_TREE, 0);
6537 /* Check to see if we have already given an error message. */
6538 if (inside_init == error_mark_node)
6540 else if (require_constant && !TREE_CONSTANT (inside_init))
6542 error_init ("initializer element is not constant");
6543 inside_init = error_mark_node;
6545 else if (require_constant
6546 && !initializer_constant_valid_p (inside_init,
6547 TREE_TYPE (inside_init)))
6549 error_init ("initializer element is not computable at load time");
6550 inside_init = error_mark_node;
6552 else if (require_constant && !maybe_const)
6553 pedwarn_init (init_loc, 0,
6554 "initializer element is not a constant expression");
6556 return inside_init;
6559 /* Come here only for records and arrays. */
6561 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6563 error_init ("variable-sized object may not be initialized");
6564 return error_mark_node;
6567 error_init ("invalid initializer");
6568 return error_mark_node;
6571 /* Handle initializers that use braces. */
6573 /* Type of object we are accumulating a constructor for.
6574 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6575 static tree constructor_type;
6577 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6578 left to fill. */
6579 static tree constructor_fields;
6581 /* For an ARRAY_TYPE, this is the specified index
6582 at which to store the next element we get. */
6583 static tree constructor_index;
6585 /* For an ARRAY_TYPE, this is the maximum index. */
6586 static tree constructor_max_index;
6588 /* For a RECORD_TYPE, this is the first field not yet written out. */
6589 static tree constructor_unfilled_fields;
6591 /* For an ARRAY_TYPE, this is the index of the first element
6592 not yet written out. */
6593 static tree constructor_unfilled_index;
6595 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6596 This is so we can generate gaps between fields, when appropriate. */
6597 static tree constructor_bit_index;
6599 /* If we are saving up the elements rather than allocating them,
6600 this is the list of elements so far (in reverse order,
6601 most recent first). */
6602 static vec<constructor_elt, va_gc> *constructor_elements;
6604 /* 1 if constructor should be incrementally stored into a constructor chain,
6605 0 if all the elements should be kept in AVL tree. */
6606 static int constructor_incremental;
6608 /* 1 if so far this constructor's elements are all compile-time constants. */
6609 static int constructor_constant;
6611 /* 1 if so far this constructor's elements are all valid address constants. */
6612 static int constructor_simple;
6614 /* 1 if this constructor has an element that cannot be part of a
6615 constant expression. */
6616 static int constructor_nonconst;
6618 /* 1 if this constructor is erroneous so far. */
6619 static int constructor_erroneous;
6621 /* Structure for managing pending initializer elements, organized as an
6622 AVL tree. */
6624 struct init_node
6626 struct init_node *left, *right;
6627 struct init_node *parent;
6628 int balance;
6629 tree purpose;
6630 tree value;
6631 tree origtype;
6634 /* Tree of pending elements at this constructor level.
6635 These are elements encountered out of order
6636 which belong at places we haven't reached yet in actually
6637 writing the output.
6638 Will never hold tree nodes across GC runs. */
6639 static struct init_node *constructor_pending_elts;
6641 /* The SPELLING_DEPTH of this constructor. */
6642 static int constructor_depth;
6644 /* DECL node for which an initializer is being read.
6645 0 means we are reading a constructor expression
6646 such as (struct foo) {...}. */
6647 static tree constructor_decl;
6649 /* Nonzero if this is an initializer for a top-level decl. */
6650 static int constructor_top_level;
6652 /* Nonzero if there were any member designators in this initializer. */
6653 static int constructor_designated;
6655 /* Nesting depth of designator list. */
6656 static int designator_depth;
6658 /* Nonzero if there were diagnosed errors in this designator list. */
6659 static int designator_erroneous;
6662 /* This stack has a level for each implicit or explicit level of
6663 structuring in the initializer, including the outermost one. It
6664 saves the values of most of the variables above. */
6666 struct constructor_range_stack;
6668 struct constructor_stack
6670 struct constructor_stack *next;
6671 tree type;
6672 tree fields;
6673 tree index;
6674 tree max_index;
6675 tree unfilled_index;
6676 tree unfilled_fields;
6677 tree bit_index;
6678 vec<constructor_elt, va_gc> *elements;
6679 struct init_node *pending_elts;
6680 int offset;
6681 int depth;
6682 /* If value nonzero, this value should replace the entire
6683 constructor at this level. */
6684 struct c_expr replacement_value;
6685 struct constructor_range_stack *range_stack;
6686 char constant;
6687 char simple;
6688 char nonconst;
6689 char implicit;
6690 char erroneous;
6691 char outer;
6692 char incremental;
6693 char designated;
6696 static struct constructor_stack *constructor_stack;
6698 /* This stack represents designators from some range designator up to
6699 the last designator in the list. */
6701 struct constructor_range_stack
6703 struct constructor_range_stack *next, *prev;
6704 struct constructor_stack *stack;
6705 tree range_start;
6706 tree index;
6707 tree range_end;
6708 tree fields;
6711 static struct constructor_range_stack *constructor_range_stack;
6713 /* This stack records separate initializers that are nested.
6714 Nested initializers can't happen in ANSI C, but GNU C allows them
6715 in cases like { ... (struct foo) { ... } ... }. */
6717 struct initializer_stack
6719 struct initializer_stack *next;
6720 tree decl;
6721 struct constructor_stack *constructor_stack;
6722 struct constructor_range_stack *constructor_range_stack;
6723 vec<constructor_elt, va_gc> *elements;
6724 struct spelling *spelling;
6725 struct spelling *spelling_base;
6726 int spelling_size;
6727 char top_level;
6728 char require_constant_value;
6729 char require_constant_elements;
6732 static struct initializer_stack *initializer_stack;
6734 /* Prepare to parse and output the initializer for variable DECL. */
6736 void
6737 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6739 const char *locus;
6740 struct initializer_stack *p = XNEW (struct initializer_stack);
6742 p->decl = constructor_decl;
6743 p->require_constant_value = require_constant_value;
6744 p->require_constant_elements = require_constant_elements;
6745 p->constructor_stack = constructor_stack;
6746 p->constructor_range_stack = constructor_range_stack;
6747 p->elements = constructor_elements;
6748 p->spelling = spelling;
6749 p->spelling_base = spelling_base;
6750 p->spelling_size = spelling_size;
6751 p->top_level = constructor_top_level;
6752 p->next = initializer_stack;
6753 initializer_stack = p;
6755 constructor_decl = decl;
6756 constructor_designated = 0;
6757 constructor_top_level = top_level;
6759 if (decl != 0 && decl != error_mark_node)
6761 require_constant_value = TREE_STATIC (decl);
6762 require_constant_elements
6763 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6764 /* For a scalar, you can always use any value to initialize,
6765 even within braces. */
6766 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6767 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6768 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6769 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6770 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6772 else
6774 require_constant_value = 0;
6775 require_constant_elements = 0;
6776 locus = _("(anonymous)");
6779 constructor_stack = 0;
6780 constructor_range_stack = 0;
6782 missing_braces_mentioned = 0;
6784 spelling_base = 0;
6785 spelling_size = 0;
6786 RESTORE_SPELLING_DEPTH (0);
6788 if (locus)
6789 push_string (locus);
6792 void
6793 finish_init (void)
6795 struct initializer_stack *p = initializer_stack;
6797 /* Free the whole constructor stack of this initializer. */
6798 while (constructor_stack)
6800 struct constructor_stack *q = constructor_stack;
6801 constructor_stack = q->next;
6802 free (q);
6805 gcc_assert (!constructor_range_stack);
6807 /* Pop back to the data of the outer initializer (if any). */
6808 free (spelling_base);
6810 constructor_decl = p->decl;
6811 require_constant_value = p->require_constant_value;
6812 require_constant_elements = p->require_constant_elements;
6813 constructor_stack = p->constructor_stack;
6814 constructor_range_stack = p->constructor_range_stack;
6815 constructor_elements = p->elements;
6816 spelling = p->spelling;
6817 spelling_base = p->spelling_base;
6818 spelling_size = p->spelling_size;
6819 constructor_top_level = p->top_level;
6820 initializer_stack = p->next;
6821 free (p);
6824 /* Call here when we see the initializer is surrounded by braces.
6825 This is instead of a call to push_init_level;
6826 it is matched by a call to pop_init_level.
6828 TYPE is the type to initialize, for a constructor expression.
6829 For an initializer for a decl, TYPE is zero. */
6831 void
6832 really_start_incremental_init (tree type)
6834 struct constructor_stack *p = XNEW (struct constructor_stack);
6836 if (type == 0)
6837 type = TREE_TYPE (constructor_decl);
6839 if (TREE_CODE (type) == VECTOR_TYPE
6840 && TYPE_VECTOR_OPAQUE (type))
6841 error ("opaque vector types cannot be initialized");
6843 p->type = constructor_type;
6844 p->fields = constructor_fields;
6845 p->index = constructor_index;
6846 p->max_index = constructor_max_index;
6847 p->unfilled_index = constructor_unfilled_index;
6848 p->unfilled_fields = constructor_unfilled_fields;
6849 p->bit_index = constructor_bit_index;
6850 p->elements = constructor_elements;
6851 p->constant = constructor_constant;
6852 p->simple = constructor_simple;
6853 p->nonconst = constructor_nonconst;
6854 p->erroneous = constructor_erroneous;
6855 p->pending_elts = constructor_pending_elts;
6856 p->depth = constructor_depth;
6857 p->replacement_value.value = 0;
6858 p->replacement_value.original_code = ERROR_MARK;
6859 p->replacement_value.original_type = NULL;
6860 p->implicit = 0;
6861 p->range_stack = 0;
6862 p->outer = 0;
6863 p->incremental = constructor_incremental;
6864 p->designated = constructor_designated;
6865 p->next = 0;
6866 constructor_stack = p;
6868 constructor_constant = 1;
6869 constructor_simple = 1;
6870 constructor_nonconst = 0;
6871 constructor_depth = SPELLING_DEPTH ();
6872 constructor_elements = NULL;
6873 constructor_pending_elts = 0;
6874 constructor_type = type;
6875 constructor_incremental = 1;
6876 constructor_designated = 0;
6877 designator_depth = 0;
6878 designator_erroneous = 0;
6880 /* The result of the constructor must not be UPC shared qualified */
6881 if (upc_shared_type_p (constructor_type))
6882 constructor_type = build_upc_unshared_type (constructor_type);
6883 if (TREE_CODE (constructor_type) == RECORD_TYPE
6884 || TREE_CODE (constructor_type) == UNION_TYPE)
6886 constructor_fields = TYPE_FIELDS (constructor_type);
6887 /* Skip any nameless bit fields at the beginning. */
6888 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6889 && DECL_NAME (constructor_fields) == 0)
6890 constructor_fields = DECL_CHAIN (constructor_fields);
6892 constructor_unfilled_fields = constructor_fields;
6893 constructor_bit_index = bitsize_zero_node;
6895 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6897 if (TYPE_DOMAIN (constructor_type))
6899 constructor_max_index
6900 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6902 /* Detect non-empty initializations of zero-length arrays. */
6903 if (constructor_max_index == NULL_TREE
6904 && TYPE_SIZE (constructor_type))
6905 constructor_max_index = integer_minus_one_node;
6907 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6908 to initialize VLAs will cause a proper error; avoid tree
6909 checking errors as well by setting a safe value. */
6910 if (constructor_max_index
6911 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6912 constructor_max_index = integer_minus_one_node;
6914 constructor_index
6915 = convert (bitsizetype,
6916 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6918 else
6920 constructor_index = bitsize_zero_node;
6921 constructor_max_index = NULL_TREE;
6924 constructor_unfilled_index = constructor_index;
6926 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6928 /* Vectors are like simple fixed-size arrays. */
6929 constructor_max_index =
6930 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6931 constructor_index = bitsize_zero_node;
6932 constructor_unfilled_index = constructor_index;
6934 else
6936 /* Handle the case of int x = {5}; */
6937 constructor_fields = constructor_type;
6938 constructor_unfilled_fields = constructor_type;
6942 /* Push down into a subobject, for initialization.
6943 If this is for an explicit set of braces, IMPLICIT is 0.
6944 If it is because the next element belongs at a lower level,
6945 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6947 void
6948 push_init_level (int implicit, struct obstack * braced_init_obstack)
6950 struct constructor_stack *p;
6951 tree value = NULL_TREE;
6953 /* If we've exhausted any levels that didn't have braces,
6954 pop them now. If implicit == 1, this will have been done in
6955 process_init_element; do not repeat it here because in the case
6956 of excess initializers for an empty aggregate this leads to an
6957 infinite cycle of popping a level and immediately recreating
6958 it. */
6959 if (implicit != 1)
6961 while (constructor_stack->implicit)
6963 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6964 || TREE_CODE (constructor_type) == UNION_TYPE)
6965 && constructor_fields == 0)
6966 process_init_element (pop_init_level (1, braced_init_obstack),
6967 true, braced_init_obstack);
6968 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6969 && constructor_max_index
6970 && tree_int_cst_lt (constructor_max_index,
6971 constructor_index))
6972 process_init_element (pop_init_level (1, braced_init_obstack),
6973 true, braced_init_obstack);
6974 else
6975 break;
6979 /* Unless this is an explicit brace, we need to preserve previous
6980 content if any. */
6981 if (implicit)
6983 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6984 || TREE_CODE (constructor_type) == UNION_TYPE)
6985 && constructor_fields)
6986 value = find_init_member (constructor_fields, braced_init_obstack);
6987 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6988 value = find_init_member (constructor_index, braced_init_obstack);
6991 p = XNEW (struct constructor_stack);
6992 p->type = constructor_type;
6993 p->fields = constructor_fields;
6994 p->index = constructor_index;
6995 p->max_index = constructor_max_index;
6996 p->unfilled_index = constructor_unfilled_index;
6997 p->unfilled_fields = constructor_unfilled_fields;
6998 p->bit_index = constructor_bit_index;
6999 p->elements = constructor_elements;
7000 p->constant = constructor_constant;
7001 p->simple = constructor_simple;
7002 p->nonconst = constructor_nonconst;
7003 p->erroneous = constructor_erroneous;
7004 p->pending_elts = constructor_pending_elts;
7005 p->depth = constructor_depth;
7006 p->replacement_value.value = 0;
7007 p->replacement_value.original_code = ERROR_MARK;
7008 p->replacement_value.original_type = NULL;
7009 p->implicit = implicit;
7010 p->outer = 0;
7011 p->incremental = constructor_incremental;
7012 p->designated = constructor_designated;
7013 p->next = constructor_stack;
7014 p->range_stack = 0;
7015 constructor_stack = p;
7017 constructor_constant = 1;
7018 constructor_simple = 1;
7019 constructor_nonconst = 0;
7020 constructor_depth = SPELLING_DEPTH ();
7021 constructor_elements = NULL;
7022 constructor_incremental = 1;
7023 constructor_designated = 0;
7024 constructor_pending_elts = 0;
7025 if (!implicit)
7027 p->range_stack = constructor_range_stack;
7028 constructor_range_stack = 0;
7029 designator_depth = 0;
7030 designator_erroneous = 0;
7033 /* Don't die if an entire brace-pair level is superfluous
7034 in the containing level. */
7035 if (constructor_type == 0)
7037 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7038 || TREE_CODE (constructor_type) == UNION_TYPE)
7040 /* Don't die if there are extra init elts at the end. */
7041 if (constructor_fields == 0)
7042 constructor_type = 0;
7043 else
7045 constructor_type = TREE_TYPE (constructor_fields);
7046 push_member_name (constructor_fields);
7047 constructor_depth++;
7050 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7052 constructor_type = TREE_TYPE (constructor_type);
7053 push_array_bounds (tree_low_cst (constructor_index, 1));
7054 constructor_depth++;
7057 if (constructor_type == 0)
7059 error_init ("extra brace group at end of initializer");
7060 constructor_fields = 0;
7061 constructor_unfilled_fields = 0;
7062 return;
7065 if (value && TREE_CODE (value) == CONSTRUCTOR)
7067 constructor_constant = TREE_CONSTANT (value);
7068 constructor_simple = TREE_STATIC (value);
7069 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7070 constructor_elements = CONSTRUCTOR_ELTS (value);
7071 if (!vec_safe_is_empty (constructor_elements)
7072 && (TREE_CODE (constructor_type) == RECORD_TYPE
7073 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7074 set_nonincremental_init (braced_init_obstack);
7077 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
7079 missing_braces_mentioned = 1;
7080 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
7083 if (TREE_CODE (constructor_type) == RECORD_TYPE
7084 || TREE_CODE (constructor_type) == UNION_TYPE)
7086 constructor_fields = TYPE_FIELDS (constructor_type);
7087 /* Skip any nameless bit fields at the beginning. */
7088 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7089 && DECL_NAME (constructor_fields) == 0)
7090 constructor_fields = DECL_CHAIN (constructor_fields);
7092 constructor_unfilled_fields = constructor_fields;
7093 constructor_bit_index = bitsize_zero_node;
7095 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7097 /* Vectors are like simple fixed-size arrays. */
7098 constructor_max_index =
7099 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7100 constructor_index = bitsize_int (0);
7101 constructor_unfilled_index = constructor_index;
7103 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7105 if (TYPE_DOMAIN (constructor_type))
7107 constructor_max_index
7108 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7110 /* Detect non-empty initializations of zero-length arrays. */
7111 if (constructor_max_index == NULL_TREE
7112 && TYPE_SIZE (constructor_type))
7113 constructor_max_index = integer_minus_one_node;
7115 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7116 to initialize VLAs will cause a proper error; avoid tree
7117 checking errors as well by setting a safe value. */
7118 if (constructor_max_index
7119 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7120 constructor_max_index = integer_minus_one_node;
7122 constructor_index
7123 = convert (bitsizetype,
7124 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7126 else
7127 constructor_index = bitsize_zero_node;
7129 constructor_unfilled_index = constructor_index;
7130 if (value && TREE_CODE (value) == STRING_CST)
7132 /* We need to split the char/wchar array into individual
7133 characters, so that we don't have to special case it
7134 everywhere. */
7135 set_nonincremental_init_from_string (value, braced_init_obstack);
7138 else
7140 if (constructor_type != error_mark_node)
7141 warning_init (0, "braces around scalar initializer");
7142 constructor_fields = constructor_type;
7143 constructor_unfilled_fields = constructor_type;
7147 /* At the end of an implicit or explicit brace level,
7148 finish up that level of constructor. If a single expression
7149 with redundant braces initialized that level, return the
7150 c_expr structure for that expression. Otherwise, the original_code
7151 element is set to ERROR_MARK.
7152 If we were outputting the elements as they are read, return 0 as the value
7153 from inner levels (process_init_element ignores that),
7154 but return error_mark_node as the value from the outermost level
7155 (that's what we want to put in DECL_INITIAL).
7156 Otherwise, return a CONSTRUCTOR expression as the value. */
7158 struct c_expr
7159 pop_init_level (int implicit, struct obstack * braced_init_obstack)
7161 struct constructor_stack *p;
7162 struct c_expr ret;
7163 ret.value = 0;
7164 ret.original_code = ERROR_MARK;
7165 ret.original_type = NULL;
7167 if (implicit == 0)
7169 /* When we come to an explicit close brace,
7170 pop any inner levels that didn't have explicit braces. */
7171 while (constructor_stack->implicit)
7173 process_init_element (pop_init_level (1, braced_init_obstack),
7174 true, braced_init_obstack);
7176 gcc_assert (!constructor_range_stack);
7179 /* Now output all pending elements. */
7180 constructor_incremental = 1;
7181 output_pending_init_elements (1, braced_init_obstack);
7183 p = constructor_stack;
7185 /* Error for initializing a flexible array member, or a zero-length
7186 array member in an inappropriate context. */
7187 if (constructor_type && constructor_fields
7188 && TREE_CODE (constructor_type) == ARRAY_TYPE
7189 && TYPE_DOMAIN (constructor_type)
7190 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7192 /* Silently discard empty initializations. The parser will
7193 already have pedwarned for empty brackets. */
7194 if (integer_zerop (constructor_unfilled_index))
7195 constructor_type = NULL_TREE;
7196 else
7198 gcc_assert (!TYPE_SIZE (constructor_type));
7200 if (constructor_depth > 2)
7201 error_init ("initialization of flexible array member in a nested context");
7202 else
7203 pedwarn_init (input_location, OPT_Wpedantic,
7204 "initialization of a flexible array member");
7206 /* We have already issued an error message for the existence
7207 of a flexible array member not at the end of the structure.
7208 Discard the initializer so that we do not die later. */
7209 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7210 constructor_type = NULL_TREE;
7214 /* Warn when some struct elements are implicitly initialized to zero. */
7215 if (warn_missing_field_initializers
7216 && constructor_type
7217 && TREE_CODE (constructor_type) == RECORD_TYPE
7218 && constructor_unfilled_fields)
7220 bool constructor_zeroinit =
7221 (vec_safe_length (constructor_elements) == 1
7222 && integer_zerop ((*constructor_elements)[0].value));
7224 /* Do not warn for flexible array members or zero-length arrays. */
7225 while (constructor_unfilled_fields
7226 && (!DECL_SIZE (constructor_unfilled_fields)
7227 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7228 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7230 if (constructor_unfilled_fields
7231 /* Do not warn if this level of the initializer uses member
7232 designators; it is likely to be deliberate. */
7233 && !constructor_designated
7234 /* Do not warn about initializing with ` = {0}'. */
7235 && !constructor_zeroinit)
7237 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7238 "missing initializer for field %qD of %qT",
7239 constructor_unfilled_fields,
7240 constructor_type))
7241 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7242 "%qD declared here", constructor_unfilled_fields);
7246 /* Pad out the end of the structure. */
7247 if (p->replacement_value.value)
7248 /* If this closes a superfluous brace pair,
7249 just pass out the element between them. */
7250 ret = p->replacement_value;
7251 else if (constructor_type == 0)
7253 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7254 && TREE_CODE (constructor_type) != UNION_TYPE
7255 && TREE_CODE (constructor_type) != ARRAY_TYPE
7256 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7258 /* A nonincremental scalar initializer--just return
7259 the element, after verifying there is just one. */
7260 if (vec_safe_is_empty (constructor_elements))
7262 if (!constructor_erroneous)
7263 error_init ("empty scalar initializer");
7264 ret.value = error_mark_node;
7266 else if (vec_safe_length (constructor_elements) != 1)
7268 error_init ("extra elements in scalar initializer");
7269 ret.value = (*constructor_elements)[0].value;
7271 else
7272 ret.value = (*constructor_elements)[0].value;
7274 else
7276 if (constructor_erroneous)
7277 ret.value = error_mark_node;
7278 else
7280 ret.value = build_constructor (constructor_type,
7281 constructor_elements);
7282 if (constructor_constant)
7283 TREE_CONSTANT (ret.value) = 1;
7284 if (constructor_constant && constructor_simple)
7285 TREE_STATIC (ret.value) = 1;
7286 if (constructor_nonconst)
7287 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7291 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7293 if (constructor_nonconst)
7294 ret.original_code = C_MAYBE_CONST_EXPR;
7295 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7296 ret.original_code = ERROR_MARK;
7299 constructor_type = p->type;
7300 constructor_fields = p->fields;
7301 constructor_index = p->index;
7302 constructor_max_index = p->max_index;
7303 constructor_unfilled_index = p->unfilled_index;
7304 constructor_unfilled_fields = p->unfilled_fields;
7305 constructor_bit_index = p->bit_index;
7306 constructor_elements = p->elements;
7307 constructor_constant = p->constant;
7308 constructor_simple = p->simple;
7309 constructor_nonconst = p->nonconst;
7310 constructor_erroneous = p->erroneous;
7311 constructor_incremental = p->incremental;
7312 constructor_designated = p->designated;
7313 constructor_pending_elts = p->pending_elts;
7314 constructor_depth = p->depth;
7315 if (!p->implicit)
7316 constructor_range_stack = p->range_stack;
7317 RESTORE_SPELLING_DEPTH (constructor_depth);
7319 constructor_stack = p->next;
7320 free (p);
7322 if (ret.value == 0 && constructor_stack == 0)
7323 ret.value = error_mark_node;
7324 return ret;
7327 /* Common handling for both array range and field name designators.
7328 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7330 static int
7331 set_designator (int array, struct obstack * braced_init_obstack)
7333 tree subtype;
7334 enum tree_code subcode;
7336 /* Don't die if an entire brace-pair level is superfluous
7337 in the containing level. */
7338 if (constructor_type == 0)
7339 return 1;
7341 /* If there were errors in this designator list already, bail out
7342 silently. */
7343 if (designator_erroneous)
7344 return 1;
7346 if (!designator_depth)
7348 gcc_assert (!constructor_range_stack);
7350 /* Designator list starts at the level of closest explicit
7351 braces. */
7352 while (constructor_stack->implicit)
7354 process_init_element (pop_init_level (1, braced_init_obstack),
7355 true, braced_init_obstack);
7357 constructor_designated = 1;
7358 return 0;
7361 switch (TREE_CODE (constructor_type))
7363 case RECORD_TYPE:
7364 case UNION_TYPE:
7365 subtype = TREE_TYPE (constructor_fields);
7366 if (subtype != error_mark_node)
7367 subtype = TYPE_MAIN_VARIANT (subtype);
7368 break;
7369 case ARRAY_TYPE:
7370 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7371 break;
7372 default:
7373 gcc_unreachable ();
7376 subcode = TREE_CODE (subtype);
7377 if (array && subcode != ARRAY_TYPE)
7379 error_init ("array index in non-array initializer");
7380 return 1;
7382 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7384 error_init ("field name not in record or union initializer");
7385 return 1;
7388 constructor_designated = 1;
7389 push_init_level (2, braced_init_obstack);
7390 return 0;
7393 /* If there are range designators in designator list, push a new designator
7394 to constructor_range_stack. RANGE_END is end of such stack range or
7395 NULL_TREE if there is no range designator at this level. */
7397 static void
7398 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7400 struct constructor_range_stack *p;
7402 p = (struct constructor_range_stack *)
7403 obstack_alloc (braced_init_obstack,
7404 sizeof (struct constructor_range_stack));
7405 p->prev = constructor_range_stack;
7406 p->next = 0;
7407 p->fields = constructor_fields;
7408 p->range_start = constructor_index;
7409 p->index = constructor_index;
7410 p->stack = constructor_stack;
7411 p->range_end = range_end;
7412 if (constructor_range_stack)
7413 constructor_range_stack->next = p;
7414 constructor_range_stack = p;
7417 /* Within an array initializer, specify the next index to be initialized.
7418 FIRST is that index. If LAST is nonzero, then initialize a range
7419 of indices, running from FIRST through LAST. */
7421 void
7422 set_init_index (tree first, tree last,
7423 struct obstack * braced_init_obstack)
7425 if (set_designator (1, braced_init_obstack))
7426 return;
7428 designator_erroneous = 1;
7430 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7431 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7433 error_init ("array index in initializer not of integer type");
7434 return;
7437 if (TREE_CODE (first) != INTEGER_CST)
7439 first = c_fully_fold (first, false, NULL);
7440 if (TREE_CODE (first) == INTEGER_CST)
7441 pedwarn_init (input_location, OPT_Wpedantic,
7442 "array index in initializer is not "
7443 "an integer constant expression");
7446 if (last && TREE_CODE (last) != INTEGER_CST)
7448 last = c_fully_fold (last, false, NULL);
7449 if (TREE_CODE (last) == INTEGER_CST)
7450 pedwarn_init (input_location, OPT_Wpedantic,
7451 "array index in initializer is not "
7452 "an integer constant expression");
7455 if (TREE_CODE (first) != INTEGER_CST)
7456 error_init ("nonconstant array index in initializer");
7457 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7458 error_init ("nonconstant array index in initializer");
7459 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7460 error_init ("array index in non-array initializer");
7461 else if (tree_int_cst_sgn (first) == -1)
7462 error_init ("array index in initializer exceeds array bounds");
7463 else if (constructor_max_index
7464 && tree_int_cst_lt (constructor_max_index, first))
7465 error_init ("array index in initializer exceeds array bounds");
7466 else
7468 constant_expression_warning (first);
7469 if (last)
7470 constant_expression_warning (last);
7471 constructor_index = convert (bitsizetype, first);
7473 if (last)
7475 if (tree_int_cst_equal (first, last))
7476 last = 0;
7477 else if (tree_int_cst_lt (last, first))
7479 error_init ("empty index range in initializer");
7480 last = 0;
7482 else
7484 last = convert (bitsizetype, last);
7485 if (constructor_max_index != 0
7486 && tree_int_cst_lt (constructor_max_index, last))
7488 error_init ("array index range in initializer exceeds array bounds");
7489 last = 0;
7494 designator_depth++;
7495 designator_erroneous = 0;
7496 if (constructor_range_stack || last)
7497 push_range_stack (last, braced_init_obstack);
7501 /* Within a struct initializer, specify the next field to be initialized. */
7503 void
7504 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7506 tree field;
7508 if (set_designator (0, braced_init_obstack))
7509 return;
7511 designator_erroneous = 1;
7513 if (TREE_CODE (constructor_type) != RECORD_TYPE
7514 && TREE_CODE (constructor_type) != UNION_TYPE)
7516 error_init ("field name not in record or union initializer");
7517 return;
7520 field = lookup_field (constructor_type, fieldname);
7522 if (field == 0)
7523 error ("unknown field %qE specified in initializer", fieldname);
7524 else
7527 constructor_fields = TREE_VALUE (field);
7528 designator_depth++;
7529 designator_erroneous = 0;
7530 if (constructor_range_stack)
7531 push_range_stack (NULL_TREE, braced_init_obstack);
7532 field = TREE_CHAIN (field);
7533 if (field)
7535 if (set_designator (0, braced_init_obstack))
7536 return;
7539 while (field != NULL_TREE);
7542 /* Add a new initializer to the tree of pending initializers. PURPOSE
7543 identifies the initializer, either array index or field in a structure.
7544 VALUE is the value of that index or field. If ORIGTYPE is not
7545 NULL_TREE, it is the original type of VALUE.
7547 IMPLICIT is true if value comes from pop_init_level (1),
7548 the new initializer has been merged with the existing one
7549 and thus no warnings should be emitted about overriding an
7550 existing initializer. */
7552 static void
7553 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7554 struct obstack * braced_init_obstack)
7556 struct init_node *p, **q, *r;
7558 q = &constructor_pending_elts;
7559 p = 0;
7561 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7563 while (*q != 0)
7565 p = *q;
7566 if (tree_int_cst_lt (purpose, p->purpose))
7567 q = &p->left;
7568 else if (tree_int_cst_lt (p->purpose, purpose))
7569 q = &p->right;
7570 else
7572 if (!implicit)
7574 if (TREE_SIDE_EFFECTS (p->value))
7575 warning_init (0, "initialized field with side-effects overwritten");
7576 else if (warn_override_init)
7577 warning_init (OPT_Woverride_init, "initialized field overwritten");
7579 p->value = value;
7580 p->origtype = origtype;
7581 return;
7585 else
7587 tree bitpos;
7589 bitpos = bit_position (purpose);
7590 while (*q != NULL)
7592 p = *q;
7593 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7594 q = &p->left;
7595 else if (p->purpose != purpose)
7596 q = &p->right;
7597 else
7599 if (!implicit)
7601 if (TREE_SIDE_EFFECTS (p->value))
7602 warning_init (0, "initialized field with side-effects overwritten");
7603 else if (warn_override_init)
7604 warning_init (OPT_Woverride_init, "initialized field overwritten");
7606 p->value = value;
7607 p->origtype = origtype;
7608 return;
7613 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7614 sizeof (struct init_node));
7615 r->purpose = purpose;
7616 r->value = value;
7617 r->origtype = origtype;
7619 *q = r;
7620 r->parent = p;
7621 r->left = 0;
7622 r->right = 0;
7623 r->balance = 0;
7625 while (p)
7627 struct init_node *s;
7629 if (r == p->left)
7631 if (p->balance == 0)
7632 p->balance = -1;
7633 else if (p->balance < 0)
7635 if (r->balance < 0)
7637 /* L rotation. */
7638 p->left = r->right;
7639 if (p->left)
7640 p->left->parent = p;
7641 r->right = p;
7643 p->balance = 0;
7644 r->balance = 0;
7646 s = p->parent;
7647 p->parent = r;
7648 r->parent = s;
7649 if (s)
7651 if (s->left == p)
7652 s->left = r;
7653 else
7654 s->right = r;
7656 else
7657 constructor_pending_elts = r;
7659 else
7661 /* LR rotation. */
7662 struct init_node *t = r->right;
7664 r->right = t->left;
7665 if (r->right)
7666 r->right->parent = r;
7667 t->left = r;
7669 p->left = t->right;
7670 if (p->left)
7671 p->left->parent = p;
7672 t->right = p;
7674 p->balance = t->balance < 0;
7675 r->balance = -(t->balance > 0);
7676 t->balance = 0;
7678 s = p->parent;
7679 p->parent = t;
7680 r->parent = t;
7681 t->parent = s;
7682 if (s)
7684 if (s->left == p)
7685 s->left = t;
7686 else
7687 s->right = t;
7689 else
7690 constructor_pending_elts = t;
7692 break;
7694 else
7696 /* p->balance == +1; growth of left side balances the node. */
7697 p->balance = 0;
7698 break;
7701 else /* r == p->right */
7703 if (p->balance == 0)
7704 /* Growth propagation from right side. */
7705 p->balance++;
7706 else if (p->balance > 0)
7708 if (r->balance > 0)
7710 /* R rotation. */
7711 p->right = r->left;
7712 if (p->right)
7713 p->right->parent = p;
7714 r->left = p;
7716 p->balance = 0;
7717 r->balance = 0;
7719 s = p->parent;
7720 p->parent = r;
7721 r->parent = s;
7722 if (s)
7724 if (s->left == p)
7725 s->left = r;
7726 else
7727 s->right = r;
7729 else
7730 constructor_pending_elts = r;
7732 else /* r->balance == -1 */
7734 /* RL rotation */
7735 struct init_node *t = r->left;
7737 r->left = t->right;
7738 if (r->left)
7739 r->left->parent = r;
7740 t->right = r;
7742 p->right = t->left;
7743 if (p->right)
7744 p->right->parent = p;
7745 t->left = p;
7747 r->balance = (t->balance < 0);
7748 p->balance = -(t->balance > 0);
7749 t->balance = 0;
7751 s = p->parent;
7752 p->parent = t;
7753 r->parent = t;
7754 t->parent = s;
7755 if (s)
7757 if (s->left == p)
7758 s->left = t;
7759 else
7760 s->right = t;
7762 else
7763 constructor_pending_elts = t;
7765 break;
7767 else
7769 /* p->balance == -1; growth of right side balances the node. */
7770 p->balance = 0;
7771 break;
7775 r = p;
7776 p = p->parent;
7780 /* Build AVL tree from a sorted chain. */
7782 static void
7783 set_nonincremental_init (struct obstack * braced_init_obstack)
7785 unsigned HOST_WIDE_INT ix;
7786 tree index, value;
7788 if (TREE_CODE (constructor_type) != RECORD_TYPE
7789 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7790 return;
7792 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7794 add_pending_init (index, value, NULL_TREE, true,
7795 braced_init_obstack);
7797 constructor_elements = NULL;
7798 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7800 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7801 /* Skip any nameless bit fields at the beginning. */
7802 while (constructor_unfilled_fields != 0
7803 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7804 && DECL_NAME (constructor_unfilled_fields) == 0)
7805 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7808 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7810 if (TYPE_DOMAIN (constructor_type))
7811 constructor_unfilled_index
7812 = convert (bitsizetype,
7813 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7814 else
7815 constructor_unfilled_index = bitsize_zero_node;
7817 constructor_incremental = 0;
7820 /* Build AVL tree from a string constant. */
7822 static void
7823 set_nonincremental_init_from_string (tree str,
7824 struct obstack * braced_init_obstack)
7826 tree value, purpose, type;
7827 HOST_WIDE_INT val[2];
7828 const char *p, *end;
7829 int byte, wchar_bytes, charwidth, bitpos;
7831 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7833 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7834 charwidth = TYPE_PRECISION (char_type_node);
7835 type = TREE_TYPE (constructor_type);
7836 p = TREE_STRING_POINTER (str);
7837 end = p + TREE_STRING_LENGTH (str);
7839 for (purpose = bitsize_zero_node;
7840 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7841 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7843 if (wchar_bytes == 1)
7845 val[1] = (unsigned char) *p++;
7846 val[0] = 0;
7848 else
7850 val[0] = 0;
7851 val[1] = 0;
7852 for (byte = 0; byte < wchar_bytes; byte++)
7854 if (BYTES_BIG_ENDIAN)
7855 bitpos = (wchar_bytes - byte - 1) * charwidth;
7856 else
7857 bitpos = byte * charwidth;
7858 val[bitpos < HOST_BITS_PER_WIDE_INT]
7859 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7860 << (bitpos % HOST_BITS_PER_WIDE_INT);
7864 if (!TYPE_UNSIGNED (type))
7866 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7867 if (bitpos < HOST_BITS_PER_WIDE_INT)
7869 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7871 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7872 val[0] = -1;
7875 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7877 if (val[1] < 0)
7878 val[0] = -1;
7880 else if (val[0] & (((HOST_WIDE_INT) 1)
7881 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7882 val[0] |= ((HOST_WIDE_INT) -1)
7883 << (bitpos - HOST_BITS_PER_WIDE_INT);
7886 value = build_int_cst_wide (type, val[1], val[0]);
7887 add_pending_init (purpose, value, NULL_TREE, true,
7888 braced_init_obstack);
7891 constructor_incremental = 0;
7894 /* Return value of FIELD in pending initializer or zero if the field was
7895 not initialized yet. */
7897 static tree
7898 find_init_member (tree field, struct obstack * braced_init_obstack)
7900 struct init_node *p;
7902 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7904 if (constructor_incremental
7905 && tree_int_cst_lt (field, constructor_unfilled_index))
7906 set_nonincremental_init (braced_init_obstack);
7908 p = constructor_pending_elts;
7909 while (p)
7911 if (tree_int_cst_lt (field, p->purpose))
7912 p = p->left;
7913 else if (tree_int_cst_lt (p->purpose, field))
7914 p = p->right;
7915 else
7916 return p->value;
7919 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7921 tree bitpos = bit_position (field);
7923 if (constructor_incremental
7924 && (!constructor_unfilled_fields
7925 || tree_int_cst_lt (bitpos,
7926 bit_position (constructor_unfilled_fields))))
7927 set_nonincremental_init (braced_init_obstack);
7929 p = constructor_pending_elts;
7930 while (p)
7932 if (field == p->purpose)
7933 return p->value;
7934 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7935 p = p->left;
7936 else
7937 p = p->right;
7940 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7942 if (!vec_safe_is_empty (constructor_elements)
7943 && (constructor_elements->last ().index == field))
7944 return constructor_elements->last ().value;
7946 return 0;
7949 /* "Output" the next constructor element.
7950 At top level, really output it to assembler code now.
7951 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7952 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7953 TYPE is the data type that the containing data type wants here.
7954 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7955 If VALUE is a string constant, STRICT_STRING is true if it is
7956 unparenthesized or we should not warn here for it being parenthesized.
7957 For other types of VALUE, STRICT_STRING is not used.
7959 PENDING if non-nil means output pending elements that belong
7960 right after this element. (PENDING is normally 1;
7961 it is 0 while outputting pending elements, to avoid recursion.)
7963 IMPLICIT is true if value comes from pop_init_level (1),
7964 the new initializer has been merged with the existing one
7965 and thus no warnings should be emitted about overriding an
7966 existing initializer. */
7968 static void
7969 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7970 tree field, int pending, bool implicit,
7971 struct obstack * braced_init_obstack)
7973 tree semantic_type = NULL_TREE;
7974 bool maybe_const = true;
7975 bool npc;
7977 if (type == error_mark_node || value == error_mark_node)
7979 constructor_erroneous = 1;
7980 return;
7982 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7983 && (TREE_CODE (value) == STRING_CST
7984 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7985 && !(TREE_CODE (value) == STRING_CST
7986 && TREE_CODE (type) == ARRAY_TYPE
7987 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7988 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7989 TYPE_MAIN_VARIANT (type)))
7990 value = array_to_pointer_conversion (input_location, value);
7992 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7993 && require_constant_value && !flag_isoc99 && pending)
7995 /* As an extension, allow initializing objects with static storage
7996 duration with compound literals (which are then treated just as
7997 the brace enclosed list they contain). */
7998 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7999 value = DECL_INITIAL (decl);
8002 npc = null_pointer_constant_p (value);
8003 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8005 semantic_type = TREE_TYPE (value);
8006 value = TREE_OPERAND (value, 0);
8008 value = c_fully_fold (value, require_constant_value, &maybe_const);
8010 if (value == error_mark_node)
8011 constructor_erroneous = 1;
8012 else if (!TREE_CONSTANT (value))
8013 constructor_constant = 0;
8014 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8015 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8016 || TREE_CODE (constructor_type) == UNION_TYPE)
8017 && DECL_C_BIT_FIELD (field)
8018 && TREE_CODE (value) != INTEGER_CST))
8019 constructor_simple = 0;
8020 if (!maybe_const)
8021 constructor_nonconst = 1;
8023 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8025 if (require_constant_value)
8027 error_init ("initializer element is not constant");
8028 value = error_mark_node;
8030 else if (require_constant_elements)
8031 pedwarn (input_location, 0,
8032 "initializer element is not computable at load time");
8034 else if (!maybe_const
8035 && (require_constant_value || require_constant_elements))
8036 pedwarn_init (input_location, 0,
8037 "initializer element is not a constant expression");
8039 /* Issue -Wc++-compat warnings about initializing a bitfield with
8040 enum type. */
8041 if (warn_cxx_compat
8042 && field != NULL_TREE
8043 && TREE_CODE (field) == FIELD_DECL
8044 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8045 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8046 != TYPE_MAIN_VARIANT (type))
8047 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8049 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8050 if (checktype != error_mark_node
8051 && (TYPE_MAIN_VARIANT (checktype)
8052 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8053 warning_init (OPT_Wc___compat,
8054 "enum conversion in initialization is invalid in C++");
8057 /* If this field is empty (and not at the end of structure),
8058 don't do anything other than checking the initializer. */
8059 if (field
8060 && (TREE_TYPE (field) == error_mark_node
8061 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8062 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8063 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8064 || DECL_CHAIN (field)))))
8065 return;
8067 if (semantic_type)
8068 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8069 value = digest_init (input_location, type, value, origtype, npc,
8070 strict_string, require_constant_value);
8071 if (value == error_mark_node)
8073 constructor_erroneous = 1;
8074 return;
8076 if (require_constant_value || require_constant_elements)
8077 constant_expression_warning (value);
8079 /* If this element doesn't come next in sequence,
8080 put it on constructor_pending_elts. */
8081 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8082 && (!constructor_incremental
8083 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8085 if (constructor_incremental
8086 && tree_int_cst_lt (field, constructor_unfilled_index))
8087 set_nonincremental_init (braced_init_obstack);
8089 add_pending_init (field, value, origtype, implicit,
8090 braced_init_obstack);
8091 return;
8093 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8094 && (!constructor_incremental
8095 || field != constructor_unfilled_fields))
8097 /* We do this for records but not for unions. In a union,
8098 no matter which field is specified, it can be initialized
8099 right away since it starts at the beginning of the union. */
8100 if (constructor_incremental)
8102 if (!constructor_unfilled_fields)
8103 set_nonincremental_init (braced_init_obstack);
8104 else
8106 tree bitpos, unfillpos;
8108 bitpos = bit_position (field);
8109 unfillpos = bit_position (constructor_unfilled_fields);
8111 if (tree_int_cst_lt (bitpos, unfillpos))
8112 set_nonincremental_init (braced_init_obstack);
8116 add_pending_init (field, value, origtype, implicit,
8117 braced_init_obstack);
8118 return;
8120 else if (TREE_CODE (constructor_type) == UNION_TYPE
8121 && !vec_safe_is_empty (constructor_elements))
8123 if (!implicit)
8125 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8126 warning_init (0,
8127 "initialized field with side-effects overwritten");
8128 else if (warn_override_init)
8129 warning_init (OPT_Woverride_init, "initialized field overwritten");
8132 /* We can have just one union field set. */
8133 constructor_elements = NULL;
8136 /* Otherwise, output this element either to
8137 constructor_elements or to the assembler file. */
8139 constructor_elt celt = {field, value};
8140 vec_safe_push (constructor_elements, celt);
8142 /* Advance the variable that indicates sequential elements output. */
8143 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8144 constructor_unfilled_index
8145 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8146 bitsize_one_node);
8147 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8149 constructor_unfilled_fields
8150 = DECL_CHAIN (constructor_unfilled_fields);
8152 /* Skip any nameless bit fields. */
8153 while (constructor_unfilled_fields != 0
8154 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8155 && DECL_NAME (constructor_unfilled_fields) == 0)
8156 constructor_unfilled_fields =
8157 DECL_CHAIN (constructor_unfilled_fields);
8159 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8160 constructor_unfilled_fields = 0;
8162 /* Now output any pending elements which have become next. */
8163 if (pending)
8164 output_pending_init_elements (0, braced_init_obstack);
8167 /* Output any pending elements which have become next.
8168 As we output elements, constructor_unfilled_{fields,index}
8169 advances, which may cause other elements to become next;
8170 if so, they too are output.
8172 If ALL is 0, we return when there are
8173 no more pending elements to output now.
8175 If ALL is 1, we output space as necessary so that
8176 we can output all the pending elements. */
8177 static void
8178 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8180 struct init_node *elt = constructor_pending_elts;
8181 tree next;
8183 retry:
8185 /* Look through the whole pending tree.
8186 If we find an element that should be output now,
8187 output it. Otherwise, set NEXT to the element
8188 that comes first among those still pending. */
8190 next = 0;
8191 while (elt)
8193 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8195 if (tree_int_cst_equal (elt->purpose,
8196 constructor_unfilled_index))
8197 output_init_element (elt->value, elt->origtype, true,
8198 TREE_TYPE (constructor_type),
8199 constructor_unfilled_index, 0, false,
8200 braced_init_obstack);
8201 else if (tree_int_cst_lt (constructor_unfilled_index,
8202 elt->purpose))
8204 /* Advance to the next smaller node. */
8205 if (elt->left)
8206 elt = elt->left;
8207 else
8209 /* We have reached the smallest node bigger than the
8210 current unfilled index. Fill the space first. */
8211 next = elt->purpose;
8212 break;
8215 else
8217 /* Advance to the next bigger node. */
8218 if (elt->right)
8219 elt = elt->right;
8220 else
8222 /* We have reached the biggest node in a subtree. Find
8223 the parent of it, which is the next bigger node. */
8224 while (elt->parent && elt->parent->right == elt)
8225 elt = elt->parent;
8226 elt = elt->parent;
8227 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8228 elt->purpose))
8230 next = elt->purpose;
8231 break;
8236 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8237 || TREE_CODE (constructor_type) == UNION_TYPE)
8239 tree ctor_unfilled_bitpos, elt_bitpos;
8241 /* If the current record is complete we are done. */
8242 if (constructor_unfilled_fields == 0)
8243 break;
8245 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8246 elt_bitpos = bit_position (elt->purpose);
8247 /* We can't compare fields here because there might be empty
8248 fields in between. */
8249 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8251 constructor_unfilled_fields = elt->purpose;
8252 output_init_element (elt->value, elt->origtype, true,
8253 TREE_TYPE (elt->purpose),
8254 elt->purpose, 0, false,
8255 braced_init_obstack);
8257 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8259 /* Advance to the next smaller node. */
8260 if (elt->left)
8261 elt = elt->left;
8262 else
8264 /* We have reached the smallest node bigger than the
8265 current unfilled field. Fill the space first. */
8266 next = elt->purpose;
8267 break;
8270 else
8272 /* Advance to the next bigger node. */
8273 if (elt->right)
8274 elt = elt->right;
8275 else
8277 /* We have reached the biggest node in a subtree. Find
8278 the parent of it, which is the next bigger node. */
8279 while (elt->parent && elt->parent->right == elt)
8280 elt = elt->parent;
8281 elt = elt->parent;
8282 if (elt
8283 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8284 bit_position (elt->purpose))))
8286 next = elt->purpose;
8287 break;
8294 /* Ordinarily return, but not if we want to output all
8295 and there are elements left. */
8296 if (!(all && next != 0))
8297 return;
8299 /* If it's not incremental, just skip over the gap, so that after
8300 jumping to retry we will output the next successive element. */
8301 if (TREE_CODE (constructor_type) == RECORD_TYPE
8302 || TREE_CODE (constructor_type) == UNION_TYPE)
8303 constructor_unfilled_fields = next;
8304 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8305 constructor_unfilled_index = next;
8307 /* ELT now points to the node in the pending tree with the next
8308 initializer to output. */
8309 goto retry;
8312 /* Add one non-braced element to the current constructor level.
8313 This adjusts the current position within the constructor's type.
8314 This may also start or terminate implicit levels
8315 to handle a partly-braced initializer.
8317 Once this has found the correct level for the new element,
8318 it calls output_init_element.
8320 IMPLICIT is true if value comes from pop_init_level (1),
8321 the new initializer has been merged with the existing one
8322 and thus no warnings should be emitted about overriding an
8323 existing initializer. */
8325 void
8326 process_init_element (struct c_expr value, bool implicit,
8327 struct obstack * braced_init_obstack)
8329 tree orig_value = value.value;
8330 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8331 bool strict_string = value.original_code == STRING_CST;
8333 designator_depth = 0;
8334 designator_erroneous = 0;
8336 /* Handle superfluous braces around string cst as in
8337 char x[] = {"foo"}; */
8338 if (string_flag
8339 && constructor_type
8340 && TREE_CODE (constructor_type) == ARRAY_TYPE
8341 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8342 && integer_zerop (constructor_unfilled_index))
8344 if (constructor_stack->replacement_value.value)
8345 error_init ("excess elements in char array initializer");
8346 constructor_stack->replacement_value = value;
8347 return;
8350 if (constructor_stack->replacement_value.value != 0)
8352 error_init ("excess elements in struct initializer");
8353 return;
8356 /* Ignore elements of a brace group if it is entirely superfluous
8357 and has already been diagnosed. */
8358 if (constructor_type == 0)
8359 return;
8361 /* If we've exhausted any levels that didn't have braces,
8362 pop them now. */
8363 while (constructor_stack->implicit)
8365 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8366 || TREE_CODE (constructor_type) == UNION_TYPE)
8367 && constructor_fields == 0)
8368 process_init_element (pop_init_level (1, braced_init_obstack),
8369 true, braced_init_obstack);
8370 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8371 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8372 && (constructor_max_index == 0
8373 || tree_int_cst_lt (constructor_max_index,
8374 constructor_index)))
8375 process_init_element (pop_init_level (1, braced_init_obstack),
8376 true, braced_init_obstack);
8377 else
8378 break;
8381 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8382 if (constructor_range_stack)
8384 /* If value is a compound literal and we'll be just using its
8385 content, don't put it into a SAVE_EXPR. */
8386 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8387 || !require_constant_value
8388 || flag_isoc99)
8390 tree semantic_type = NULL_TREE;
8391 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8393 semantic_type = TREE_TYPE (value.value);
8394 value.value = TREE_OPERAND (value.value, 0);
8396 value.value = c_save_expr (value.value);
8397 if (semantic_type)
8398 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8399 value.value);
8403 while (1)
8405 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8407 tree fieldtype;
8408 enum tree_code fieldcode;
8410 if (constructor_fields == 0)
8412 pedwarn_init (input_location, 0,
8413 "excess elements in struct initializer");
8414 break;
8417 fieldtype = TREE_TYPE (constructor_fields);
8418 if (fieldtype != error_mark_node)
8419 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8420 fieldcode = TREE_CODE (fieldtype);
8422 /* Error for non-static initialization of a flexible array member. */
8423 if (fieldcode == ARRAY_TYPE
8424 && !require_constant_value
8425 && TYPE_SIZE (fieldtype) == NULL_TREE
8426 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8428 error_init ("non-static initialization of a flexible array member");
8429 break;
8432 /* Accept a string constant to initialize a subarray. */
8433 if (value.value != 0
8434 && fieldcode == ARRAY_TYPE
8435 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8436 && string_flag)
8437 value.value = orig_value;
8438 /* Otherwise, if we have come to a subaggregate,
8439 and we don't have an element of its type, push into it. */
8440 else if (value.value != 0
8441 && value.value != error_mark_node
8442 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8443 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8444 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8446 push_init_level (1, braced_init_obstack);
8447 continue;
8450 if (value.value)
8452 push_member_name (constructor_fields);
8453 output_init_element (value.value, value.original_type,
8454 strict_string, fieldtype,
8455 constructor_fields, 1, implicit,
8456 braced_init_obstack);
8457 RESTORE_SPELLING_DEPTH (constructor_depth);
8459 else
8460 /* Do the bookkeeping for an element that was
8461 directly output as a constructor. */
8463 /* For a record, keep track of end position of last field. */
8464 if (DECL_SIZE (constructor_fields))
8465 constructor_bit_index
8466 = size_binop_loc (input_location, PLUS_EXPR,
8467 bit_position (constructor_fields),
8468 DECL_SIZE (constructor_fields));
8470 /* If the current field was the first one not yet written out,
8471 it isn't now, so update. */
8472 if (constructor_unfilled_fields == constructor_fields)
8474 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8475 /* Skip any nameless bit fields. */
8476 while (constructor_unfilled_fields != 0
8477 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8478 && DECL_NAME (constructor_unfilled_fields) == 0)
8479 constructor_unfilled_fields =
8480 DECL_CHAIN (constructor_unfilled_fields);
8484 constructor_fields = DECL_CHAIN (constructor_fields);
8485 /* Skip any nameless bit fields at the beginning. */
8486 while (constructor_fields != 0
8487 && DECL_C_BIT_FIELD (constructor_fields)
8488 && DECL_NAME (constructor_fields) == 0)
8489 constructor_fields = DECL_CHAIN (constructor_fields);
8491 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8493 tree fieldtype;
8494 enum tree_code fieldcode;
8496 if (constructor_fields == 0)
8498 pedwarn_init (input_location, 0,
8499 "excess elements in union initializer");
8500 break;
8503 fieldtype = TREE_TYPE (constructor_fields);
8504 if (fieldtype != error_mark_node)
8505 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8506 fieldcode = TREE_CODE (fieldtype);
8508 /* Warn that traditional C rejects initialization of unions.
8509 We skip the warning if the value is zero. This is done
8510 under the assumption that the zero initializer in user
8511 code appears conditioned on e.g. __STDC__ to avoid
8512 "missing initializer" warnings and relies on default
8513 initialization to zero in the traditional C case.
8514 We also skip the warning if the initializer is designated,
8515 again on the assumption that this must be conditional on
8516 __STDC__ anyway (and we've already complained about the
8517 member-designator already). */
8518 if (!in_system_header && !constructor_designated
8519 && !(value.value && (integer_zerop (value.value)
8520 || real_zerop (value.value))))
8521 warning (OPT_Wtraditional, "traditional C rejects initialization "
8522 "of unions");
8524 /* Accept a string constant to initialize a subarray. */
8525 if (value.value != 0
8526 && fieldcode == ARRAY_TYPE
8527 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8528 && string_flag)
8529 value.value = orig_value;
8530 /* Otherwise, if we have come to a subaggregate,
8531 and we don't have an element of its type, push into it. */
8532 else if (value.value != 0
8533 && value.value != error_mark_node
8534 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8535 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8536 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8538 push_init_level (1, braced_init_obstack);
8539 continue;
8542 if (value.value)
8544 push_member_name (constructor_fields);
8545 output_init_element (value.value, value.original_type,
8546 strict_string, fieldtype,
8547 constructor_fields, 1, implicit,
8548 braced_init_obstack);
8549 RESTORE_SPELLING_DEPTH (constructor_depth);
8551 else
8552 /* Do the bookkeeping for an element that was
8553 directly output as a constructor. */
8555 constructor_bit_index = DECL_SIZE (constructor_fields);
8556 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8559 constructor_fields = 0;
8561 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8563 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8564 enum tree_code eltcode = TREE_CODE (elttype);
8566 /* Accept a string constant to initialize a subarray. */
8567 if (value.value != 0
8568 && eltcode == ARRAY_TYPE
8569 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8570 && string_flag)
8571 value.value = orig_value;
8572 /* Otherwise, if we have come to a subaggregate,
8573 and we don't have an element of its type, push into it. */
8574 else if (value.value != 0
8575 && value.value != error_mark_node
8576 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8577 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8578 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8580 push_init_level (1, braced_init_obstack);
8581 continue;
8584 if (constructor_max_index != 0
8585 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8586 || integer_all_onesp (constructor_max_index)))
8588 pedwarn_init (input_location, 0,
8589 "excess elements in array initializer");
8590 break;
8593 /* Now output the actual element. */
8594 if (value.value)
8596 push_array_bounds (tree_low_cst (constructor_index, 1));
8597 output_init_element (value.value, value.original_type,
8598 strict_string, elttype,
8599 constructor_index, 1, implicit,
8600 braced_init_obstack);
8601 RESTORE_SPELLING_DEPTH (constructor_depth);
8604 constructor_index
8605 = size_binop_loc (input_location, PLUS_EXPR,
8606 constructor_index, bitsize_one_node);
8608 if (!value.value)
8609 /* If we are doing the bookkeeping for an element that was
8610 directly output as a constructor, we must update
8611 constructor_unfilled_index. */
8612 constructor_unfilled_index = constructor_index;
8614 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8616 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8618 /* Do a basic check of initializer size. Note that vectors
8619 always have a fixed size derived from their type. */
8620 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8622 pedwarn_init (input_location, 0,
8623 "excess elements in vector initializer");
8624 break;
8627 /* Now output the actual element. */
8628 if (value.value)
8630 if (TREE_CODE (value.value) == VECTOR_CST)
8631 elttype = TYPE_MAIN_VARIANT (constructor_type);
8632 output_init_element (value.value, value.original_type,
8633 strict_string, elttype,
8634 constructor_index, 1, implicit,
8635 braced_init_obstack);
8638 constructor_index
8639 = size_binop_loc (input_location,
8640 PLUS_EXPR, constructor_index, bitsize_one_node);
8642 if (!value.value)
8643 /* If we are doing the bookkeeping for an element that was
8644 directly output as a constructor, we must update
8645 constructor_unfilled_index. */
8646 constructor_unfilled_index = constructor_index;
8649 /* Handle the sole element allowed in a braced initializer
8650 for a scalar variable. */
8651 else if (constructor_type != error_mark_node
8652 && constructor_fields == 0)
8654 pedwarn_init (input_location, 0,
8655 "excess elements in scalar initializer");
8656 break;
8658 else
8660 if (value.value)
8661 output_init_element (value.value, value.original_type,
8662 strict_string, constructor_type,
8663 NULL_TREE, 1, implicit,
8664 braced_init_obstack);
8665 constructor_fields = 0;
8668 /* Handle range initializers either at this level or anywhere higher
8669 in the designator stack. */
8670 if (constructor_range_stack)
8672 struct constructor_range_stack *p, *range_stack;
8673 int finish = 0;
8675 range_stack = constructor_range_stack;
8676 constructor_range_stack = 0;
8677 while (constructor_stack != range_stack->stack)
8679 gcc_assert (constructor_stack->implicit);
8680 process_init_element (pop_init_level (1,
8681 braced_init_obstack),
8682 true, braced_init_obstack);
8684 for (p = range_stack;
8685 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8686 p = p->prev)
8688 gcc_assert (constructor_stack->implicit);
8689 process_init_element (pop_init_level (1, braced_init_obstack),
8690 true, braced_init_obstack);
8693 p->index = size_binop_loc (input_location,
8694 PLUS_EXPR, p->index, bitsize_one_node);
8695 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8696 finish = 1;
8698 while (1)
8700 constructor_index = p->index;
8701 constructor_fields = p->fields;
8702 if (finish && p->range_end && p->index == p->range_start)
8704 finish = 0;
8705 p->prev = 0;
8707 p = p->next;
8708 if (!p)
8709 break;
8710 push_init_level (2, braced_init_obstack);
8711 p->stack = constructor_stack;
8712 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8713 p->index = p->range_start;
8716 if (!finish)
8717 constructor_range_stack = range_stack;
8718 continue;
8721 break;
8724 constructor_range_stack = 0;
8727 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8728 (guaranteed to be 'volatile' or null) and ARGS (represented using
8729 an ASM_EXPR node). */
8730 tree
8731 build_asm_stmt (tree cv_qualifier, tree args)
8733 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8734 ASM_VOLATILE_P (args) = 1;
8735 return add_stmt (args);
8738 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8739 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8740 SIMPLE indicates whether there was anything at all after the
8741 string in the asm expression -- asm("blah") and asm("blah" : )
8742 are subtly different. We use a ASM_EXPR node to represent this. */
8743 tree
8744 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8745 tree clobbers, tree labels, bool simple)
8747 tree tail;
8748 tree args;
8749 int i;
8750 const char *constraint;
8751 const char **oconstraints;
8752 bool allows_mem, allows_reg, is_inout;
8753 int ninputs, noutputs;
8755 ninputs = list_length (inputs);
8756 noutputs = list_length (outputs);
8757 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8759 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8761 /* Remove output conversions that change the type but not the mode. */
8762 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8764 tree output = TREE_VALUE (tail);
8766 output = c_fully_fold (output, false, NULL);
8768 /* ??? Really, this should not be here. Users should be using a
8769 proper lvalue, dammit. But there's a long history of using casts
8770 in the output operands. In cases like longlong.h, this becomes a
8771 primitive form of typechecking -- if the cast can be removed, then
8772 the output operand had a type of the proper width; otherwise we'll
8773 get an error. Gross, but ... */
8774 STRIP_NOPS (output);
8776 if (!lvalue_or_else (loc, output, lv_asm))
8777 output = error_mark_node;
8779 if (output != error_mark_node
8780 && (TREE_READONLY (output)
8781 || TYPE_READONLY (TREE_TYPE (output))
8782 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8783 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8784 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8785 readonly_error (output, lv_asm);
8787 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8788 oconstraints[i] = constraint;
8790 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8791 &allows_mem, &allows_reg, &is_inout))
8793 /* If the operand is going to end up in memory,
8794 mark it addressable. */
8795 if (!allows_reg && !c_mark_addressable (output))
8796 output = error_mark_node;
8797 if (!(!allows_reg && allows_mem)
8798 && output != error_mark_node
8799 && VOID_TYPE_P (TREE_TYPE (output)))
8801 error_at (loc, "invalid use of void expression");
8802 output = error_mark_node;
8805 else
8806 output = error_mark_node;
8808 TREE_VALUE (tail) = output;
8811 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8813 tree input;
8815 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8816 input = TREE_VALUE (tail);
8818 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8819 oconstraints, &allows_mem, &allows_reg))
8821 /* If the operand is going to end up in memory,
8822 mark it addressable. */
8823 if (!allows_reg && allows_mem)
8825 input = c_fully_fold (input, false, NULL);
8827 /* Strip the nops as we allow this case. FIXME, this really
8828 should be rejected or made deprecated. */
8829 STRIP_NOPS (input);
8830 if (!c_mark_addressable (input))
8831 input = error_mark_node;
8833 else
8835 struct c_expr expr;
8836 memset (&expr, 0, sizeof (expr));
8837 expr.value = input;
8838 expr = default_function_array_conversion (loc, expr);
8839 input = c_fully_fold (expr.value, false, NULL);
8841 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8843 error_at (loc, "invalid use of void expression");
8844 input = error_mark_node;
8848 else
8849 input = error_mark_node;
8851 TREE_VALUE (tail) = input;
8854 /* ASMs with labels cannot have outputs. This should have been
8855 enforced by the parser. */
8856 gcc_assert (outputs == NULL || labels == NULL);
8858 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8860 /* asm statements without outputs, including simple ones, are treated
8861 as volatile. */
8862 ASM_INPUT_P (args) = simple;
8863 ASM_VOLATILE_P (args) = (noutputs == 0);
8865 return args;
8868 /* Generate a goto statement to LABEL. LOC is the location of the
8869 GOTO. */
8871 tree
8872 c_finish_goto_label (location_t loc, tree label)
8874 tree decl = lookup_label_for_goto (loc, label);
8875 if (!decl)
8876 return NULL_TREE;
8877 TREE_USED (decl) = 1;
8879 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8880 SET_EXPR_LOCATION (t, loc);
8881 return add_stmt (t);
8885 /* Generate a computed goto statement to EXPR. LOC is the location of
8886 the GOTO. */
8888 tree
8889 c_finish_goto_ptr (location_t loc, tree expr)
8891 tree t;
8892 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
8893 expr = c_fully_fold (expr, false, NULL);
8894 expr = convert (ptr_type_node, expr);
8895 t = build1 (GOTO_EXPR, void_type_node, expr);
8896 SET_EXPR_LOCATION (t, loc);
8897 return add_stmt (t);
8900 /* Generate a C `return' statement. RETVAL is the expression for what
8901 to return, or a null pointer for `return;' with no value. LOC is
8902 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8903 is the original type of RETVAL. */
8905 tree
8906 c_finish_return (location_t loc, tree retval, tree origtype)
8908 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8909 bool no_warning = false;
8910 bool npc = false;
8912 if (TREE_THIS_VOLATILE (current_function_decl))
8913 warning_at (loc, 0,
8914 "function declared %<noreturn%> has a %<return%> statement");
8916 if (retval)
8918 tree semantic_type = NULL_TREE;
8919 npc = null_pointer_constant_p (retval);
8920 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8922 semantic_type = TREE_TYPE (retval);
8923 retval = TREE_OPERAND (retval, 0);
8925 retval = c_fully_fold (retval, false, NULL);
8926 if (semantic_type)
8927 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8930 if (!retval)
8932 current_function_returns_null = 1;
8933 if ((warn_return_type || flag_isoc99)
8934 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8936 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8937 "%<return%> with no value, in "
8938 "function returning non-void");
8939 no_warning = true;
8942 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8944 current_function_returns_null = 1;
8945 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8946 pedwarn (loc, 0,
8947 "%<return%> with a value, in function returning void");
8948 else
8949 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
8950 "%<return%> with expression, in function returning void");
8952 else
8954 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8955 ic_return,
8956 npc, NULL_TREE, NULL_TREE, 0);
8957 tree res = DECL_RESULT (current_function_decl);
8958 tree inner;
8959 bool save;
8961 current_function_returns_value = 1;
8962 if (t == error_mark_node)
8963 return NULL_TREE;
8965 save = in_late_binary_op;
8966 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
8967 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
8968 in_late_binary_op = true;
8969 inner = t = convert (TREE_TYPE (res), t);
8970 in_late_binary_op = save;
8972 /* Strip any conversions, additions, and subtractions, and see if
8973 we are returning the address of a local variable. Warn if so. */
8974 while (1)
8976 switch (TREE_CODE (inner))
8978 CASE_CONVERT:
8979 case NON_LVALUE_EXPR:
8980 case PLUS_EXPR:
8981 case POINTER_PLUS_EXPR:
8982 inner = TREE_OPERAND (inner, 0);
8983 continue;
8985 case MINUS_EXPR:
8986 /* If the second operand of the MINUS_EXPR has a pointer
8987 type (or is converted from it), this may be valid, so
8988 don't give a warning. */
8990 tree op1 = TREE_OPERAND (inner, 1);
8992 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8993 && (CONVERT_EXPR_P (op1)
8994 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8995 op1 = TREE_OPERAND (op1, 0);
8997 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8998 break;
9000 inner = TREE_OPERAND (inner, 0);
9001 continue;
9004 case ADDR_EXPR:
9005 inner = TREE_OPERAND (inner, 0);
9007 while (REFERENCE_CLASS_P (inner)
9008 && TREE_CODE (inner) != INDIRECT_REF)
9009 inner = TREE_OPERAND (inner, 0);
9011 if (DECL_P (inner)
9012 && !DECL_EXTERNAL (inner)
9013 && !TREE_STATIC (inner)
9014 && DECL_CONTEXT (inner) == current_function_decl)
9015 warning_at (loc,
9016 OPT_Wreturn_local_addr, "function returns address "
9017 "of local variable");
9018 break;
9020 default:
9021 break;
9024 break;
9027 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9028 SET_EXPR_LOCATION (retval, loc);
9030 if (warn_sequence_point)
9031 verify_sequence_points (retval);
9034 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9035 TREE_NO_WARNING (ret_stmt) |= no_warning;
9036 return add_stmt (ret_stmt);
9039 struct c_switch {
9040 /* The SWITCH_EXPR being built. */
9041 tree switch_expr;
9043 /* The original type of the testing expression, i.e. before the
9044 default conversion is applied. */
9045 tree orig_type;
9047 /* A splay-tree mapping the low element of a case range to the high
9048 element, or NULL_TREE if there is no high element. Used to
9049 determine whether or not a new case label duplicates an old case
9050 label. We need a tree, rather than simply a hash table, because
9051 of the GNU case range extension. */
9052 splay_tree cases;
9054 /* The bindings at the point of the switch. This is used for
9055 warnings crossing decls when branching to a case label. */
9056 struct c_spot_bindings *bindings;
9058 /* The next node on the stack. */
9059 struct c_switch *next;
9062 /* A stack of the currently active switch statements. The innermost
9063 switch statement is on the top of the stack. There is no need to
9064 mark the stack for garbage collection because it is only active
9065 during the processing of the body of a function, and we never
9066 collect at that point. */
9068 struct c_switch *c_switch_stack;
9070 /* Start a C switch statement, testing expression EXP. Return the new
9071 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9072 SWITCH_COND_LOC is the location of the switch's condition. */
9074 tree
9075 c_start_case (location_t switch_loc,
9076 location_t switch_cond_loc,
9077 tree exp)
9079 tree orig_type = error_mark_node;
9080 struct c_switch *cs;
9082 if (exp != error_mark_node)
9084 orig_type = TREE_TYPE (exp);
9086 if (!INTEGRAL_TYPE_P (orig_type))
9088 if (orig_type != error_mark_node)
9090 error_at (switch_cond_loc, "switch quantity not an integer");
9091 orig_type = error_mark_node;
9093 exp = integer_zero_node;
9095 else
9097 tree type = TYPE_MAIN_VARIANT (orig_type);
9099 if (!in_system_header
9100 && (type == long_integer_type_node
9101 || type == long_unsigned_type_node))
9102 warning_at (switch_cond_loc,
9103 OPT_Wtraditional, "%<long%> switch expression not "
9104 "converted to %<int%> in ISO C");
9106 exp = c_fully_fold (exp, false, NULL);
9107 exp = default_conversion (exp);
9109 if (warn_sequence_point)
9110 verify_sequence_points (exp);
9114 /* Add this new SWITCH_EXPR to the stack. */
9115 cs = XNEW (struct c_switch);
9116 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9117 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9118 cs->orig_type = orig_type;
9119 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9120 cs->bindings = c_get_switch_bindings ();
9121 cs->next = c_switch_stack;
9122 c_switch_stack = cs;
9124 return add_stmt (cs->switch_expr);
9127 /* Process a case label at location LOC. */
9129 tree
9130 do_case (location_t loc, tree low_value, tree high_value)
9132 tree label = NULL_TREE;
9134 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9136 low_value = c_fully_fold (low_value, false, NULL);
9137 if (TREE_CODE (low_value) == INTEGER_CST)
9138 pedwarn (input_location, OPT_Wpedantic,
9139 "case label is not an integer constant expression");
9142 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9144 high_value = c_fully_fold (high_value, false, NULL);
9145 if (TREE_CODE (high_value) == INTEGER_CST)
9146 pedwarn (input_location, OPT_Wpedantic,
9147 "case label is not an integer constant expression");
9150 if (c_switch_stack == NULL)
9152 if (low_value)
9153 error_at (loc, "case label not within a switch statement");
9154 else
9155 error_at (loc, "%<default%> label not within a switch statement");
9156 return NULL_TREE;
9159 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9160 EXPR_LOCATION (c_switch_stack->switch_expr),
9161 loc))
9162 return NULL_TREE;
9164 label = c_add_case_label (loc, c_switch_stack->cases,
9165 SWITCH_COND (c_switch_stack->switch_expr),
9166 c_switch_stack->orig_type,
9167 low_value, high_value);
9168 if (label == error_mark_node)
9169 label = NULL_TREE;
9170 return label;
9173 /* Finish the switch statement. */
9175 void
9176 c_finish_case (tree body)
9178 struct c_switch *cs = c_switch_stack;
9179 location_t switch_location;
9181 SWITCH_BODY (cs->switch_expr) = body;
9183 /* Emit warnings as needed. */
9184 switch_location = EXPR_LOCATION (cs->switch_expr);
9185 c_do_switch_warnings (cs->cases, switch_location,
9186 TREE_TYPE (cs->switch_expr),
9187 SWITCH_COND (cs->switch_expr));
9189 /* Pop the stack. */
9190 c_switch_stack = cs->next;
9191 splay_tree_delete (cs->cases);
9192 c_release_switch_bindings (cs->bindings);
9193 XDELETE (cs);
9196 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9197 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9198 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9199 statement, and was not surrounded with parenthesis. */
9201 void
9202 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9203 tree else_block, bool nested_if)
9205 tree stmt;
9207 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9208 if (warn_parentheses && nested_if && else_block == NULL)
9210 tree inner_if = then_block;
9212 /* We know from the grammar productions that there is an IF nested
9213 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9214 it might not be exactly THEN_BLOCK, but should be the last
9215 non-container statement within. */
9216 while (1)
9217 switch (TREE_CODE (inner_if))
9219 case COND_EXPR:
9220 goto found;
9221 case BIND_EXPR:
9222 inner_if = BIND_EXPR_BODY (inner_if);
9223 break;
9224 case STATEMENT_LIST:
9225 inner_if = expr_last (then_block);
9226 break;
9227 case TRY_FINALLY_EXPR:
9228 case TRY_CATCH_EXPR:
9229 inner_if = TREE_OPERAND (inner_if, 0);
9230 break;
9231 default:
9232 gcc_unreachable ();
9234 found:
9236 if (COND_EXPR_ELSE (inner_if))
9237 warning_at (if_locus, OPT_Wparentheses,
9238 "suggest explicit braces to avoid ambiguous %<else%>");
9241 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9242 SET_EXPR_LOCATION (stmt, if_locus);
9243 add_stmt (stmt);
9246 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9247 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9248 is false for DO loops. INCR is the FOR increment expression. BODY is
9249 the statement controlled by the loop. BLAB is the break label. CLAB is
9250 the continue label. Everything is allowed to be NULL. */
9252 void
9253 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9254 tree blab, tree clab, bool cond_is_first)
9256 tree entry = NULL, exit = NULL, t;
9258 /* If the condition is zero don't generate a loop construct. */
9259 if (cond && integer_zerop (cond))
9261 if (cond_is_first)
9263 t = build_and_jump (&blab);
9264 SET_EXPR_LOCATION (t, start_locus);
9265 add_stmt (t);
9268 else
9270 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9272 /* If we have an exit condition, then we build an IF with gotos either
9273 out of the loop, or to the top of it. If there's no exit condition,
9274 then we just build a jump back to the top. */
9275 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9277 if (cond && !integer_nonzerop (cond))
9279 /* Canonicalize the loop condition to the end. This means
9280 generating a branch to the loop condition. Reuse the
9281 continue label, if possible. */
9282 if (cond_is_first)
9284 if (incr || !clab)
9286 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9287 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9289 else
9290 t = build1 (GOTO_EXPR, void_type_node, clab);
9291 SET_EXPR_LOCATION (t, start_locus);
9292 add_stmt (t);
9295 t = build_and_jump (&blab);
9296 if (cond_is_first)
9297 exit = fold_build3_loc (start_locus,
9298 COND_EXPR, void_type_node, cond, exit, t);
9299 else
9300 exit = fold_build3_loc (input_location,
9301 COND_EXPR, void_type_node, cond, exit, t);
9304 add_stmt (top);
9307 if (body)
9308 add_stmt (body);
9309 if (clab)
9310 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9311 if (incr)
9312 add_stmt (incr);
9313 if (entry)
9314 add_stmt (entry);
9315 if (exit)
9316 add_stmt (exit);
9317 if (blab)
9318 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9321 tree
9322 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9324 bool skip;
9325 tree label = *label_p;
9327 /* In switch statements break is sometimes stylistically used after
9328 a return statement. This can lead to spurious warnings about
9329 control reaching the end of a non-void function when it is
9330 inlined. Note that we are calling block_may_fallthru with
9331 language specific tree nodes; this works because
9332 block_may_fallthru returns true when given something it does not
9333 understand. */
9334 skip = !block_may_fallthru (cur_stmt_list);
9336 if (!label)
9338 if (!skip)
9339 *label_p = label = create_artificial_label (loc);
9341 else if (TREE_CODE (label) == LABEL_DECL)
9343 else switch (TREE_INT_CST_LOW (label))
9345 case 0:
9346 if (is_break)
9347 error_at (loc, "break statement not within loop or switch");
9348 else
9349 error_at (loc, "continue statement not within a loop");
9350 return NULL_TREE;
9352 case 1:
9353 gcc_assert (is_break);
9354 error_at (loc, "break statement used with OpenMP for loop");
9355 return NULL_TREE;
9357 default:
9358 gcc_unreachable ();
9361 if (skip)
9362 return NULL_TREE;
9364 if (!is_break)
9365 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9367 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9370 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9372 static void
9373 emit_side_effect_warnings (location_t loc, tree expr)
9375 if (expr == error_mark_node)
9377 else if (!TREE_SIDE_EFFECTS (expr))
9379 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9380 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9382 else
9383 warn_if_unused_value (expr, loc);
9386 /* Process an expression as if it were a complete statement. Emit
9387 diagnostics, but do not call ADD_STMT. LOC is the location of the
9388 statement. */
9390 tree
9391 c_process_expr_stmt (location_t loc, tree expr)
9393 tree exprv;
9395 if (!expr)
9396 return NULL_TREE;
9398 expr = c_fully_fold (expr, false, NULL);
9400 if (warn_sequence_point)
9401 verify_sequence_points (expr);
9403 if (TREE_TYPE (expr) != error_mark_node
9404 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9405 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9406 error_at (loc, "expression statement has incomplete type");
9408 /* If we're not processing a statement expression, warn about unused values.
9409 Warnings for statement expressions will be emitted later, once we figure
9410 out which is the result. */
9411 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9412 && warn_unused_value)
9413 emit_side_effect_warnings (loc, expr);
9415 exprv = expr;
9416 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9417 exprv = TREE_OPERAND (exprv, 1);
9418 while (CONVERT_EXPR_P (exprv))
9419 exprv = TREE_OPERAND (exprv, 0);
9420 if (DECL_P (exprv)
9421 || handled_component_p (exprv)
9422 || TREE_CODE (exprv) == ADDR_EXPR)
9423 mark_exp_read (exprv);
9425 /* If the expression is not of a type to which we cannot assign a line
9426 number, wrap the thing in a no-op NOP_EXPR. */
9427 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9429 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9430 SET_EXPR_LOCATION (expr, loc);
9433 return expr;
9436 /* Emit an expression as a statement. LOC is the location of the
9437 expression. */
9439 tree
9440 c_finish_expr_stmt (location_t loc, tree expr)
9442 if (expr)
9443 return add_stmt (c_process_expr_stmt (loc, expr));
9444 else
9445 return NULL;
9448 /* Do the opposite and emit a statement as an expression. To begin,
9449 create a new binding level and return it. */
9451 tree
9452 c_begin_stmt_expr (void)
9454 tree ret;
9456 /* We must force a BLOCK for this level so that, if it is not expanded
9457 later, there is a way to turn off the entire subtree of blocks that
9458 are contained in it. */
9459 keep_next_level ();
9460 ret = c_begin_compound_stmt (true);
9462 c_bindings_start_stmt_expr (c_switch_stack == NULL
9463 ? NULL
9464 : c_switch_stack->bindings);
9466 /* Mark the current statement list as belonging to a statement list. */
9467 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9469 return ret;
9472 /* LOC is the location of the compound statement to which this body
9473 belongs. */
9475 tree
9476 c_finish_stmt_expr (location_t loc, tree body)
9478 tree last, type, tmp, val;
9479 tree *last_p;
9481 body = c_end_compound_stmt (loc, body, true);
9483 c_bindings_end_stmt_expr (c_switch_stack == NULL
9484 ? NULL
9485 : c_switch_stack->bindings);
9487 /* Locate the last statement in BODY. See c_end_compound_stmt
9488 about always returning a BIND_EXPR. */
9489 last_p = &BIND_EXPR_BODY (body);
9490 last = BIND_EXPR_BODY (body);
9492 continue_searching:
9493 if (TREE_CODE (last) == STATEMENT_LIST)
9495 tree_stmt_iterator i;
9497 /* This can happen with degenerate cases like ({ }). No value. */
9498 if (!TREE_SIDE_EFFECTS (last))
9499 return body;
9501 /* If we're supposed to generate side effects warnings, process
9502 all of the statements except the last. */
9503 if (warn_unused_value)
9505 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9507 location_t tloc;
9508 tree t = tsi_stmt (i);
9510 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9511 emit_side_effect_warnings (tloc, t);
9514 else
9515 i = tsi_last (last);
9516 last_p = tsi_stmt_ptr (i);
9517 last = *last_p;
9520 /* If the end of the list is exception related, then the list was split
9521 by a call to push_cleanup. Continue searching. */
9522 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9523 || TREE_CODE (last) == TRY_CATCH_EXPR)
9525 last_p = &TREE_OPERAND (last, 0);
9526 last = *last_p;
9527 goto continue_searching;
9530 if (last == error_mark_node)
9531 return last;
9533 /* In the case that the BIND_EXPR is not necessary, return the
9534 expression out from inside it. */
9535 if (last == BIND_EXPR_BODY (body)
9536 && BIND_EXPR_VARS (body) == NULL)
9538 /* Even if this looks constant, do not allow it in a constant
9539 expression. */
9540 last = c_wrap_maybe_const (last, true);
9541 /* Do not warn if the return value of a statement expression is
9542 unused. */
9543 TREE_NO_WARNING (last) = 1;
9544 return last;
9547 /* Extract the type of said expression. */
9548 type = TREE_TYPE (last);
9550 /* If we're not returning a value at all, then the BIND_EXPR that
9551 we already have is a fine expression to return. */
9552 if (!type || VOID_TYPE_P (type))
9553 return body;
9555 /* Now that we've located the expression containing the value, it seems
9556 silly to make voidify_wrapper_expr repeat the process. Create a
9557 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9558 tmp = create_tmp_var_raw (type, NULL);
9560 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9561 tree_expr_nonnegative_p giving up immediately. */
9562 val = last;
9563 if (TREE_CODE (val) == NOP_EXPR
9564 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9565 val = TREE_OPERAND (val, 0);
9567 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9568 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9571 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9572 SET_EXPR_LOCATION (t, loc);
9573 return t;
9577 /* Begin and end compound statements. This is as simple as pushing
9578 and popping new statement lists from the tree. */
9580 tree
9581 c_begin_compound_stmt (bool do_scope)
9583 tree stmt = push_stmt_list ();
9584 if (do_scope)
9585 push_scope ();
9586 return stmt;
9589 /* End a compound statement. STMT is the statement. LOC is the
9590 location of the compound statement-- this is usually the location
9591 of the opening brace. */
9593 tree
9594 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9596 tree block = NULL;
9598 if (do_scope)
9600 if (c_dialect_objc ())
9601 objc_clear_super_receiver ();
9602 block = pop_scope ();
9605 stmt = pop_stmt_list (stmt);
9606 stmt = c_build_bind_expr (loc, block, stmt);
9608 /* If this compound statement is nested immediately inside a statement
9609 expression, then force a BIND_EXPR to be created. Otherwise we'll
9610 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9611 STATEMENT_LISTs merge, and thus we can lose track of what statement
9612 was really last. */
9613 if (building_stmt_list_p ()
9614 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9615 && TREE_CODE (stmt) != BIND_EXPR)
9617 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9618 TREE_SIDE_EFFECTS (stmt) = 1;
9619 SET_EXPR_LOCATION (stmt, loc);
9622 return stmt;
9625 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9626 when the current scope is exited. EH_ONLY is true when this is not
9627 meant to apply to normal control flow transfer. */
9629 void
9630 push_cleanup (tree decl, tree cleanup, bool eh_only)
9632 enum tree_code code;
9633 tree stmt, list;
9634 bool stmt_expr;
9636 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9637 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9638 add_stmt (stmt);
9639 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9640 list = push_stmt_list ();
9641 TREE_OPERAND (stmt, 0) = list;
9642 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9645 /* Build a binary-operation expression without default conversions.
9646 CODE is the kind of expression to build.
9647 LOCATION is the operator's location.
9648 This function differs from `build' in several ways:
9649 the data type of the result is computed and recorded in it,
9650 warnings are generated if arg data types are invalid,
9651 special handling for addition and subtraction of pointers is known,
9652 and some optimization is done (operations on narrow ints
9653 are done in the narrower type when that gives the same result).
9654 Constant folding is also done before the result is returned.
9656 Note that the operands will never have enumeral types, or function
9657 or array types, because either they will have the default conversions
9658 performed or they have both just been converted to some other type in which
9659 the arithmetic is to be done. */
9661 tree
9662 build_binary_op (location_t location, enum tree_code code,
9663 tree orig_op0, tree orig_op1, int convert_p)
9665 tree type0, type1, orig_type0, orig_type1;
9666 tree eptype;
9667 enum tree_code code0, code1;
9668 tree op0, op1;
9669 tree ret = error_mark_node;
9670 const char *invalid_op_diag;
9671 bool op0_int_operands, op1_int_operands;
9672 bool int_const, int_const_or_overflow, int_operands;
9674 /* Expression code to give to the expression when it is built.
9675 Normally this is CODE, which is what the caller asked for,
9676 but in some special cases we change it. */
9677 enum tree_code resultcode = code;
9679 /* Data type in which the computation is to be performed.
9680 In the simplest cases this is the common type of the arguments. */
9681 tree result_type = NULL;
9683 /* When the computation is in excess precision, the type of the
9684 final EXCESS_PRECISION_EXPR. */
9685 tree semantic_result_type = NULL;
9687 /* Nonzero means operands have already been type-converted
9688 in whatever way is necessary.
9689 Zero means they need to be converted to RESULT_TYPE. */
9690 int converted = 0;
9692 /* Nonzero means create the expression with this type, rather than
9693 RESULT_TYPE. */
9694 tree build_type = 0;
9696 /* Nonzero means after finally constructing the expression
9697 convert it to this type. */
9698 tree final_type = 0;
9700 /* Nonzero if this is an operation like MIN or MAX which can
9701 safely be computed in short if both args are promoted shorts.
9702 Also implies COMMON.
9703 -1 indicates a bitwise operation; this makes a difference
9704 in the exact conditions for when it is safe to do the operation
9705 in a narrower mode. */
9706 int shorten = 0;
9708 /* Nonzero if this is a comparison operation;
9709 if both args are promoted shorts, compare the original shorts.
9710 Also implies COMMON. */
9711 int short_compare = 0;
9713 /* Nonzero if this is a right-shift operation, which can be computed on the
9714 original short and then promoted if the operand is a promoted short. */
9715 int short_shift = 0;
9717 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9718 int common = 0;
9720 /* True means types are compatible as far as ObjC is concerned. */
9721 bool objc_ok;
9723 /* True means this is an arithmetic operation that may need excess
9724 precision. */
9725 bool may_need_excess_precision;
9727 /* True means this is a boolean operation that converts both its
9728 operands to truth-values. */
9729 bool boolean_op = false;
9731 if (location == UNKNOWN_LOCATION)
9732 location = input_location;
9734 op0 = orig_op0;
9735 op1 = orig_op1;
9737 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9738 if (op0_int_operands)
9739 op0 = remove_c_maybe_const_expr (op0);
9740 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9741 if (op1_int_operands)
9742 op1 = remove_c_maybe_const_expr (op1);
9743 int_operands = (op0_int_operands && op1_int_operands);
9744 if (int_operands)
9746 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9747 && TREE_CODE (orig_op1) == INTEGER_CST);
9748 int_const = (int_const_or_overflow
9749 && !TREE_OVERFLOW (orig_op0)
9750 && !TREE_OVERFLOW (orig_op1));
9752 else
9753 int_const = int_const_or_overflow = false;
9755 /* Do not apply default conversion in mixed vector/scalar expression. */
9756 if (convert_p
9757 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
9758 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
9760 op0 = default_conversion (op0);
9761 op1 = default_conversion (op1);
9764 orig_type0 = type0 = TREE_TYPE (op0);
9765 orig_type1 = type1 = TREE_TYPE (op1);
9767 /* The expression codes of the data types of the arguments tell us
9768 whether the arguments are integers, floating, pointers, etc. */
9769 code0 = TREE_CODE (type0);
9770 code1 = TREE_CODE (type1);
9772 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9773 STRIP_TYPE_NOPS (op0);
9774 STRIP_TYPE_NOPS (op1);
9776 /* If an error was already reported for one of the arguments,
9777 avoid reporting another error. */
9779 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9780 return error_mark_node;
9782 if ((invalid_op_diag
9783 = targetm.invalid_binary_op (code, type0, type1)))
9785 error_at (location, invalid_op_diag);
9786 return error_mark_node;
9789 switch (code)
9791 case PLUS_EXPR:
9792 case MINUS_EXPR:
9793 case MULT_EXPR:
9794 case TRUNC_DIV_EXPR:
9795 case CEIL_DIV_EXPR:
9796 case FLOOR_DIV_EXPR:
9797 case ROUND_DIV_EXPR:
9798 case EXACT_DIV_EXPR:
9799 may_need_excess_precision = true;
9800 break;
9801 default:
9802 may_need_excess_precision = false;
9803 break;
9805 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9807 op0 = TREE_OPERAND (op0, 0);
9808 type0 = TREE_TYPE (op0);
9810 else if (may_need_excess_precision
9811 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9813 type0 = eptype;
9814 op0 = convert (eptype, op0);
9816 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9818 op1 = TREE_OPERAND (op1, 0);
9819 type1 = TREE_TYPE (op1);
9821 else if (may_need_excess_precision
9822 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9824 type1 = eptype;
9825 op1 = convert (eptype, op1);
9828 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9830 /* In case when one of the operands of the binary operation is
9831 a vector and another is a scalar -- convert scalar to vector. */
9832 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
9834 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
9835 true);
9837 switch (convert_flag)
9839 case stv_error:
9840 return error_mark_node;
9841 case stv_firstarg:
9843 bool maybe_const = true;
9844 tree sc;
9845 sc = c_fully_fold (op0, false, &maybe_const);
9846 sc = save_expr (sc);
9847 sc = convert (TREE_TYPE (type1), sc);
9848 op0 = build_vector_from_val (type1, sc);
9849 if (!maybe_const)
9850 op0 = c_wrap_maybe_const (op0, true);
9851 orig_type0 = type0 = TREE_TYPE (op0);
9852 code0 = TREE_CODE (type0);
9853 converted = 1;
9854 break;
9856 case stv_secondarg:
9858 bool maybe_const = true;
9859 tree sc;
9860 sc = c_fully_fold (op1, false, &maybe_const);
9861 sc = save_expr (sc);
9862 sc = convert (TREE_TYPE (type0), sc);
9863 op1 = build_vector_from_val (type0, sc);
9864 if (!maybe_const)
9865 op1 = c_wrap_maybe_const (op1, true);
9866 orig_type1 = type1 = TREE_TYPE (op1);
9867 code1 = TREE_CODE (type1);
9868 converted = 1;
9869 break;
9871 default:
9872 break;
9876 switch (code)
9878 case PLUS_EXPR:
9879 /* Handle the pointer + int case. */
9880 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9882 ret = c_pointer_int_sum (location, PLUS_EXPR, op0, op1);
9883 goto return_build_binary_op;
9885 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9887 ret = c_pointer_int_sum (location, PLUS_EXPR, op1, op0);
9888 goto return_build_binary_op;
9890 else
9891 common = 1;
9892 break;
9894 case MINUS_EXPR:
9895 /* Subtraction of two similar pointers.
9896 We must subtract them as integers, then divide by object size. */
9897 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9898 && comp_target_types (location, type0, type1))
9900 ret = pointer_diff (location, op0, op1);
9901 goto return_build_binary_op;
9903 /* Handle pointer minus int. Just like pointer plus int. */
9904 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9906 ret = c_pointer_int_sum (location, MINUS_EXPR, op0, op1);
9907 goto return_build_binary_op;
9909 else
9910 common = 1;
9911 break;
9913 case MULT_EXPR:
9914 common = 1;
9915 break;
9917 case TRUNC_DIV_EXPR:
9918 case CEIL_DIV_EXPR:
9919 case FLOOR_DIV_EXPR:
9920 case ROUND_DIV_EXPR:
9921 case EXACT_DIV_EXPR:
9922 warn_for_div_by_zero (location, op1);
9924 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9925 || code0 == FIXED_POINT_TYPE
9926 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9927 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9928 || code1 == FIXED_POINT_TYPE
9929 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9931 enum tree_code tcode0 = code0, tcode1 = code1;
9933 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9934 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9935 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9936 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9938 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9939 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9940 resultcode = RDIV_EXPR;
9941 else
9942 /* Although it would be tempting to shorten always here, that
9943 loses on some targets, since the modulo instruction is
9944 undefined if the quotient can't be represented in the
9945 computation mode. We shorten only if unsigned or if
9946 dividing by something we know != -1. */
9947 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9948 || (TREE_CODE (op1) == INTEGER_CST
9949 && !integer_all_onesp (op1)));
9950 common = 1;
9952 break;
9954 case BIT_AND_EXPR:
9955 case BIT_IOR_EXPR:
9956 case BIT_XOR_EXPR:
9957 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9958 shorten = -1;
9959 /* Allow vector types which are not floating point types. */
9960 else if (code0 == VECTOR_TYPE
9961 && code1 == VECTOR_TYPE
9962 && !VECTOR_FLOAT_TYPE_P (type0)
9963 && !VECTOR_FLOAT_TYPE_P (type1))
9964 common = 1;
9965 break;
9967 case TRUNC_MOD_EXPR:
9968 case FLOOR_MOD_EXPR:
9969 warn_for_div_by_zero (location, op1);
9971 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9972 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9973 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9974 common = 1;
9975 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9977 /* Although it would be tempting to shorten always here, that loses
9978 on some targets, since the modulo instruction is undefined if the
9979 quotient can't be represented in the computation mode. We shorten
9980 only if unsigned or if dividing by something we know != -1. */
9981 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9982 || (TREE_CODE (op1) == INTEGER_CST
9983 && !integer_all_onesp (op1)));
9984 common = 1;
9986 break;
9988 case TRUTH_ANDIF_EXPR:
9989 case TRUTH_ORIF_EXPR:
9990 case TRUTH_AND_EXPR:
9991 case TRUTH_OR_EXPR:
9992 case TRUTH_XOR_EXPR:
9993 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9994 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9995 || code0 == FIXED_POINT_TYPE)
9996 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9997 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9998 || code1 == FIXED_POINT_TYPE))
10000 /* Result of these operations is always an int,
10001 but that does not mean the operands should be
10002 converted to ints! */
10003 result_type = integer_type_node;
10004 if (op0_int_operands)
10006 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10007 op0 = remove_c_maybe_const_expr (op0);
10009 else
10010 op0 = c_objc_common_truthvalue_conversion (location, op0);
10011 if (op1_int_operands)
10013 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10014 op1 = remove_c_maybe_const_expr (op1);
10016 else
10017 op1 = c_objc_common_truthvalue_conversion (location, op1);
10018 converted = 1;
10019 boolean_op = true;
10021 if (code == TRUTH_ANDIF_EXPR)
10023 int_const_or_overflow = (int_operands
10024 && TREE_CODE (orig_op0) == INTEGER_CST
10025 && (op0 == truthvalue_false_node
10026 || TREE_CODE (orig_op1) == INTEGER_CST));
10027 int_const = (int_const_or_overflow
10028 && !TREE_OVERFLOW (orig_op0)
10029 && (op0 == truthvalue_false_node
10030 || !TREE_OVERFLOW (orig_op1)));
10032 else if (code == TRUTH_ORIF_EXPR)
10034 int_const_or_overflow = (int_operands
10035 && TREE_CODE (orig_op0) == INTEGER_CST
10036 && (op0 == truthvalue_true_node
10037 || TREE_CODE (orig_op1) == INTEGER_CST));
10038 int_const = (int_const_or_overflow
10039 && !TREE_OVERFLOW (orig_op0)
10040 && (op0 == truthvalue_true_node
10041 || !TREE_OVERFLOW (orig_op1)));
10043 break;
10045 /* Shift operations: result has same type as first operand;
10046 always convert second operand to int.
10047 Also set SHORT_SHIFT if shifting rightward. */
10049 case RSHIFT_EXPR:
10050 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10051 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10053 result_type = type0;
10054 converted = 1;
10056 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10057 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10058 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10059 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10061 result_type = type0;
10062 converted = 1;
10064 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10065 && code1 == INTEGER_TYPE)
10067 if (TREE_CODE (op1) == INTEGER_CST)
10069 if (tree_int_cst_sgn (op1) < 0)
10071 int_const = false;
10072 if (c_inhibit_evaluation_warnings == 0)
10073 warning (0, "right shift count is negative");
10075 else
10077 if (!integer_zerop (op1))
10078 short_shift = 1;
10080 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10082 int_const = false;
10083 if (c_inhibit_evaluation_warnings == 0)
10084 warning (0, "right shift count >= width of type");
10089 /* Use the type of the value to be shifted. */
10090 result_type = type0;
10091 /* Convert the non vector shift-count to an integer, regardless
10092 of size of value being shifted. */
10093 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10094 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10095 op1 = convert (integer_type_node, op1);
10096 /* Avoid converting op1 to result_type later. */
10097 converted = 1;
10099 break;
10101 case LSHIFT_EXPR:
10102 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10103 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10105 result_type = type0;
10106 converted = 1;
10108 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10109 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10110 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10111 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10113 result_type = type0;
10114 converted = 1;
10116 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10117 && code1 == INTEGER_TYPE)
10119 if (TREE_CODE (op1) == INTEGER_CST)
10121 if (tree_int_cst_sgn (op1) < 0)
10123 int_const = false;
10124 if (c_inhibit_evaluation_warnings == 0)
10125 warning (0, "left shift count is negative");
10128 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10130 int_const = false;
10131 if (c_inhibit_evaluation_warnings == 0)
10132 warning (0, "left shift count >= width of type");
10136 /* Use the type of the value to be shifted. */
10137 result_type = type0;
10138 /* Convert the non vector shift-count to an integer, regardless
10139 of size of value being shifted. */
10140 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10141 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10142 op1 = convert (integer_type_node, op1);
10143 /* Avoid converting op1 to result_type later. */
10144 converted = 1;
10146 break;
10148 case EQ_EXPR:
10149 case NE_EXPR:
10150 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10152 tree intt;
10153 if (TREE_TYPE (type0) != TREE_TYPE (type1))
10155 error_at (location, "comparing vectors with different "
10156 "element types");
10157 return error_mark_node;
10160 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10162 error_at (location, "comparing vectors with different "
10163 "number of elements");
10164 return error_mark_node;
10167 /* Always construct signed integer vector type. */
10168 intt = c_common_type_for_size (GET_MODE_BITSIZE
10169 (TYPE_MODE (TREE_TYPE (type0))), 0);
10170 result_type = build_opaque_vector_type (intt,
10171 TYPE_VECTOR_SUBPARTS (type0));
10172 converted = 1;
10173 break;
10175 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10176 warning_at (location,
10177 OPT_Wfloat_equal,
10178 "comparing floating point with == or != is unsafe");
10179 /* Result of comparison is always int,
10180 but don't convert the args to int! */
10181 build_type = integer_type_node;
10182 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10183 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10184 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10185 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10186 short_compare = 1;
10187 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10189 if (TREE_CODE (op0) == ADDR_EXPR
10190 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10192 if (code == EQ_EXPR)
10193 warning_at (location,
10194 OPT_Waddress,
10195 "the comparison will always evaluate as %<false%> "
10196 "for the address of %qD will never be NULL",
10197 TREE_OPERAND (op0, 0));
10198 else
10199 warning_at (location,
10200 OPT_Waddress,
10201 "the comparison will always evaluate as %<true%> "
10202 "for the address of %qD will never be NULL",
10203 TREE_OPERAND (op0, 0));
10205 result_type = type0;
10207 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10209 if (TREE_CODE (op1) == ADDR_EXPR
10210 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10212 if (code == EQ_EXPR)
10213 warning_at (location,
10214 OPT_Waddress,
10215 "the comparison will always evaluate as %<false%> "
10216 "for the address of %qD will never be NULL",
10217 TREE_OPERAND (op1, 0));
10218 else
10219 warning_at (location,
10220 OPT_Waddress,
10221 "the comparison will always evaluate as %<true%> "
10222 "for the address of %qD will never be NULL",
10223 TREE_OPERAND (op1, 0));
10225 result_type = type1;
10227 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10229 tree tt0 = TREE_TYPE (type0);
10230 tree tt1 = TREE_TYPE (type1);
10231 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10232 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10233 addr_space_t as_common = ADDR_SPACE_GENERIC;
10235 if ((upc_shared_type_p (tt0)
10236 && !(upc_shared_type_p (tt1) || integer_zerop(op1)))
10237 || (upc_shared_type_p (tt1)
10238 && !(upc_shared_type_p (tt0) || integer_zerop(op0))))
10240 error_at (location, "UPC does not allow comparisons "
10241 "between pointers to shared and "
10242 "local pointers");
10243 return error_mark_node;
10245 /* Anything compares with void *. void * compares with anything.
10246 Otherwise, the targets must be compatible
10247 and both must be object or both incomplete. */
10248 if (comp_target_types (location, type0, type1))
10249 result_type = common_pointer_type (type0, type1);
10250 else if (!addr_space_superset (as0, as1, &as_common))
10252 error_at (location, "comparison of pointers to "
10253 "disjoint address spaces");
10254 return error_mark_node;
10256 else if (VOID_TYPE_P (tt0))
10258 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10259 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10260 "comparison of %<void *%> with function pointer");
10262 else if (VOID_TYPE_P (tt1))
10264 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10265 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10266 "comparison of %<void *%> with function pointer");
10268 else
10269 /* Avoid warning about the volatile ObjC EH puts on decls. */
10270 if (!objc_ok)
10271 pedwarn (location, 0,
10272 "comparison of distinct pointer types lacks a cast");
10274 if (result_type == NULL_TREE)
10276 if (upc_shared_type_p(tt0) || upc_shared_type_p(tt1))
10278 result_type = upc_pts_type_node;
10280 else
10282 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10283 result_type = build_pointer_type
10284 (build_qualified_type (void_type_node,
10285 qual));
10289 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10291 result_type = type0;
10292 pedwarn (location, 0, "comparison between pointer and integer");
10294 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10296 result_type = type1;
10297 pedwarn (location, 0, "comparison between pointer and integer");
10299 break;
10301 case LE_EXPR:
10302 case GE_EXPR:
10303 case LT_EXPR:
10304 case GT_EXPR:
10305 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10307 tree intt;
10308 if (TREE_TYPE (type0) != TREE_TYPE (type1))
10310 error_at (location, "comparing vectors with different "
10311 "element types");
10312 return error_mark_node;
10315 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10317 error_at (location, "comparing vectors with different "
10318 "number of elements");
10319 return error_mark_node;
10322 /* Always construct signed integer vector type. */
10323 intt = c_common_type_for_size (GET_MODE_BITSIZE
10324 (TYPE_MODE (TREE_TYPE (type0))), 0);
10325 result_type = build_opaque_vector_type (intt,
10326 TYPE_VECTOR_SUBPARTS (type0));
10327 converted = 1;
10328 break;
10330 build_type = integer_type_node;
10331 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10332 || code0 == FIXED_POINT_TYPE)
10333 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10334 || code1 == FIXED_POINT_TYPE))
10335 short_compare = 1;
10336 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10338 const tree tt0 = TREE_TYPE (type0);
10339 const tree tt1 = TREE_TYPE (type1);
10340 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10341 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10342 addr_space_t as_common;
10344 if (upc_shared_type_p (tt0) != upc_shared_type_p (tt1))
10346 error_at (location, "UPC does not allow comparisons between "
10347 "pointers to shared and local pointers");
10348 return error_mark_node;
10350 if (comp_target_types (location, type0, type1))
10352 result_type = common_pointer_type (type0, type1);
10353 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10354 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10355 pedwarn (location, 0,
10356 "comparison of complete and incomplete pointers");
10357 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10358 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10359 "ordered comparisons of pointers to functions");
10360 else if (null_pointer_constant_p (orig_op0)
10361 || null_pointer_constant_p (orig_op1))
10362 warning_at (location, OPT_Wextra,
10363 "ordered comparison of pointer with null pointer");
10366 else if (!addr_space_superset (as0, as1, &as_common))
10368 error_at (location, "comparison of pointers to "
10369 "disjoint address spaces");
10370 return error_mark_node;
10372 else if (upc_shared_type_p (TREE_TYPE (type0))
10373 || upc_shared_type_p (TREE_TYPE (type1)))
10375 result_type = upc_pts_type_node;
10377 else
10379 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10380 result_type = build_pointer_type
10381 (build_qualified_type (void_type_node, qual));
10382 pedwarn (location, 0,
10383 "comparison of distinct pointer types lacks a cast");
10386 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10388 result_type = type0;
10389 if (pedantic)
10390 pedwarn (location, OPT_Wpedantic,
10391 "ordered comparison of pointer with integer zero");
10392 else if (extra_warnings)
10393 warning_at (location, OPT_Wextra,
10394 "ordered comparison of pointer with integer zero");
10396 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10398 result_type = type1;
10399 if (pedantic)
10400 pedwarn (location, OPT_Wpedantic,
10401 "ordered comparison of pointer with integer zero");
10402 else if (extra_warnings)
10403 warning_at (location, OPT_Wextra,
10404 "ordered comparison of pointer with integer zero");
10406 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10408 result_type = type0;
10409 pedwarn (location, 0, "comparison between pointer and integer");
10411 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10413 result_type = type1;
10414 pedwarn (location, 0, "comparison between pointer and integer");
10416 break;
10418 default:
10419 gcc_unreachable ();
10422 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10423 return error_mark_node;
10425 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10426 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10427 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
10428 TREE_TYPE (type1))))
10430 binary_op_error (location, code, type0, type1);
10431 return error_mark_node;
10434 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10435 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10437 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10438 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10440 bool first_complex = (code0 == COMPLEX_TYPE);
10441 bool second_complex = (code1 == COMPLEX_TYPE);
10442 int none_complex = (!first_complex && !second_complex);
10444 if (shorten || common || short_compare)
10446 result_type = c_common_type (type0, type1);
10447 do_warn_double_promotion (result_type, type0, type1,
10448 "implicit conversion from %qT to %qT "
10449 "to match other operand of binary "
10450 "expression",
10451 location);
10452 if (result_type == error_mark_node)
10453 return error_mark_node;
10456 if (first_complex != second_complex
10457 && (code == PLUS_EXPR
10458 || code == MINUS_EXPR
10459 || code == MULT_EXPR
10460 || (code == TRUNC_DIV_EXPR && first_complex))
10461 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10462 && flag_signed_zeros)
10464 /* An operation on mixed real/complex operands must be
10465 handled specially, but the language-independent code can
10466 more easily optimize the plain complex arithmetic if
10467 -fno-signed-zeros. */
10468 tree real_type = TREE_TYPE (result_type);
10469 tree real, imag;
10470 if (type0 != orig_type0 || type1 != orig_type1)
10472 gcc_assert (may_need_excess_precision && common);
10473 semantic_result_type = c_common_type (orig_type0, orig_type1);
10475 if (first_complex)
10477 if (TREE_TYPE (op0) != result_type)
10478 op0 = convert_and_check (result_type, op0);
10479 if (TREE_TYPE (op1) != real_type)
10480 op1 = convert_and_check (real_type, op1);
10482 else
10484 if (TREE_TYPE (op0) != real_type)
10485 op0 = convert_and_check (real_type, op0);
10486 if (TREE_TYPE (op1) != result_type)
10487 op1 = convert_and_check (result_type, op1);
10489 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10490 return error_mark_node;
10491 if (first_complex)
10493 op0 = c_save_expr (op0);
10494 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10495 op0, 1);
10496 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10497 op0, 1);
10498 switch (code)
10500 case MULT_EXPR:
10501 case TRUNC_DIV_EXPR:
10502 op1 = c_save_expr (op1);
10503 imag = build2 (resultcode, real_type, imag, op1);
10504 /* Fall through. */
10505 case PLUS_EXPR:
10506 case MINUS_EXPR:
10507 real = build2 (resultcode, real_type, real, op1);
10508 break;
10509 default:
10510 gcc_unreachable();
10513 else
10515 op1 = c_save_expr (op1);
10516 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10517 op1, 1);
10518 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10519 op1, 1);
10520 switch (code)
10522 case MULT_EXPR:
10523 op0 = c_save_expr (op0);
10524 imag = build2 (resultcode, real_type, op0, imag);
10525 /* Fall through. */
10526 case PLUS_EXPR:
10527 real = build2 (resultcode, real_type, op0, real);
10528 break;
10529 case MINUS_EXPR:
10530 real = build2 (resultcode, real_type, op0, real);
10531 imag = build1 (NEGATE_EXPR, real_type, imag);
10532 break;
10533 default:
10534 gcc_unreachable();
10537 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10538 goto return_build_binary_op;
10541 /* For certain operations (which identify themselves by shorten != 0)
10542 if both args were extended from the same smaller type,
10543 do the arithmetic in that type and then extend.
10545 shorten !=0 and !=1 indicates a bitwise operation.
10546 For them, this optimization is safe only if
10547 both args are zero-extended or both are sign-extended.
10548 Otherwise, we might change the result.
10549 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10550 but calculated in (unsigned short) it would be (unsigned short)-1. */
10552 if (shorten && none_complex)
10554 final_type = result_type;
10555 result_type = shorten_binary_op (result_type, op0, op1,
10556 shorten == -1);
10559 /* Shifts can be shortened if shifting right. */
10561 if (short_shift)
10563 int unsigned_arg;
10564 tree arg0 = get_narrower (op0, &unsigned_arg);
10566 final_type = result_type;
10568 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10569 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10571 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10572 && tree_int_cst_sgn (op1) > 0
10573 /* We can shorten only if the shift count is less than the
10574 number of bits in the smaller type size. */
10575 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10576 /* We cannot drop an unsigned shift after sign-extension. */
10577 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10579 /* Do an unsigned shift if the operand was zero-extended. */
10580 result_type
10581 = c_common_signed_or_unsigned_type (unsigned_arg,
10582 TREE_TYPE (arg0));
10583 /* Convert value-to-be-shifted to that type. */
10584 if (TREE_TYPE (op0) != result_type)
10585 op0 = convert (result_type, op0);
10586 converted = 1;
10590 /* Comparison operations are shortened too but differently.
10591 They identify themselves by setting short_compare = 1. */
10593 if (short_compare)
10595 /* Don't write &op0, etc., because that would prevent op0
10596 from being kept in a register.
10597 Instead, make copies of the our local variables and
10598 pass the copies by reference, then copy them back afterward. */
10599 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10600 enum tree_code xresultcode = resultcode;
10601 tree val
10602 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10604 if (val != 0)
10606 ret = val;
10607 goto return_build_binary_op;
10610 op0 = xop0, op1 = xop1;
10611 converted = 1;
10612 resultcode = xresultcode;
10614 if (c_inhibit_evaluation_warnings == 0)
10616 bool op0_maybe_const = true;
10617 bool op1_maybe_const = true;
10618 tree orig_op0_folded, orig_op1_folded;
10620 if (in_late_binary_op)
10622 orig_op0_folded = orig_op0;
10623 orig_op1_folded = orig_op1;
10625 else
10627 /* Fold for the sake of possible warnings, as in
10628 build_conditional_expr. This requires the
10629 "original" values to be folded, not just op0 and
10630 op1. */
10631 c_inhibit_evaluation_warnings++;
10632 op0 = c_fully_fold (op0, require_constant_value,
10633 &op0_maybe_const);
10634 op1 = c_fully_fold (op1, require_constant_value,
10635 &op1_maybe_const);
10636 c_inhibit_evaluation_warnings--;
10637 orig_op0_folded = c_fully_fold (orig_op0,
10638 require_constant_value,
10639 NULL);
10640 orig_op1_folded = c_fully_fold (orig_op1,
10641 require_constant_value,
10642 NULL);
10645 if (warn_sign_compare)
10646 warn_for_sign_compare (location, orig_op0_folded,
10647 orig_op1_folded, op0, op1,
10648 result_type, resultcode);
10649 if (!in_late_binary_op && !int_operands)
10651 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10652 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10653 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10654 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10660 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10661 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10662 Then the expression will be built.
10663 It will be given type FINAL_TYPE if that is nonzero;
10664 otherwise, it will be given type RESULT_TYPE. */
10666 if (!result_type)
10668 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10669 return error_mark_node;
10672 if (build_type == NULL_TREE)
10674 build_type = result_type;
10675 if ((type0 != orig_type0 || type1 != orig_type1)
10676 && !boolean_op)
10678 gcc_assert (may_need_excess_precision && common);
10679 semantic_result_type = c_common_type (orig_type0, orig_type1);
10683 if (!converted)
10685 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10686 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10688 /* This can happen if one operand has a vector type, and the other
10689 has a different type. */
10690 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10691 return error_mark_node;
10694 /* Treat expressions in initializers specially as they can't trap. */
10695 if (int_const_or_overflow)
10696 ret = (require_constant_value
10697 ? fold_build2_initializer_loc (location, resultcode, build_type,
10698 op0, op1)
10699 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10700 else
10701 ret = build2 (resultcode, build_type, op0, op1);
10702 if (final_type != 0)
10703 ret = convert (final_type, ret);
10705 return_build_binary_op:
10706 gcc_assert (ret != error_mark_node);
10707 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10708 ret = (int_operands
10709 ? note_integer_operands (ret)
10710 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10711 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10712 && !in_late_binary_op)
10713 ret = note_integer_operands (ret);
10714 if (semantic_result_type)
10715 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10716 protected_set_expr_location (ret, location);
10717 return ret;
10720 /* Convert EXPR to be a truth-value, validating its type for this
10721 purpose. LOCATION is the source location for the expression. */
10723 tree
10724 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10726 bool int_const, int_operands;
10728 switch (TREE_CODE (TREE_TYPE (expr)))
10730 case ARRAY_TYPE:
10731 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10732 return error_mark_node;
10734 case RECORD_TYPE:
10735 error_at (location, "used struct type value where scalar is required");
10736 return error_mark_node;
10738 case UNION_TYPE:
10739 error_at (location, "used union type value where scalar is required");
10740 return error_mark_node;
10742 case VOID_TYPE:
10743 error_at (location, "void value not ignored as it ought to be");
10744 return error_mark_node;
10746 case FUNCTION_TYPE:
10747 gcc_unreachable ();
10749 case VECTOR_TYPE:
10750 error_at (location, "used vector type where scalar is required");
10751 return error_mark_node;
10753 default:
10754 break;
10757 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10758 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10759 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
10761 expr = remove_c_maybe_const_expr (expr);
10762 expr = build2 (NE_EXPR, integer_type_node, expr,
10763 convert (TREE_TYPE (expr), integer_zero_node));
10764 expr = note_integer_operands (expr);
10766 else
10767 /* ??? Should we also give an error for vectors rather than leaving
10768 those to give errors later? */
10769 expr = c_common_truthvalue_conversion (location, expr);
10771 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10773 if (TREE_OVERFLOW (expr))
10774 return expr;
10775 else
10776 return note_integer_operands (expr);
10778 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10779 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10780 return expr;
10784 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10785 required. */
10787 tree
10788 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10790 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10792 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10793 /* Executing a compound literal inside a function reinitializes
10794 it. */
10795 if (!TREE_STATIC (decl))
10796 *se = true;
10797 return decl;
10799 else
10800 return expr;
10803 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10805 tree
10806 c_begin_omp_parallel (void)
10808 tree block;
10810 keep_next_level ();
10811 block = c_begin_compound_stmt (true);
10813 return block;
10816 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10817 statement. LOC is the location of the OMP_PARALLEL. */
10819 tree
10820 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10822 tree stmt;
10824 block = c_end_compound_stmt (loc, block, true);
10826 stmt = make_node (OMP_PARALLEL);
10827 TREE_TYPE (stmt) = void_type_node;
10828 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10829 OMP_PARALLEL_BODY (stmt) = block;
10830 SET_EXPR_LOCATION (stmt, loc);
10832 return add_stmt (stmt);
10835 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10837 tree
10838 c_begin_omp_task (void)
10840 tree block;
10842 keep_next_level ();
10843 block = c_begin_compound_stmt (true);
10845 return block;
10848 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10849 statement. LOC is the location of the #pragma. */
10851 tree
10852 c_finish_omp_task (location_t loc, tree clauses, tree block)
10854 tree stmt;
10856 block = c_end_compound_stmt (loc, block, true);
10858 stmt = make_node (OMP_TASK);
10859 TREE_TYPE (stmt) = void_type_node;
10860 OMP_TASK_CLAUSES (stmt) = clauses;
10861 OMP_TASK_BODY (stmt) = block;
10862 SET_EXPR_LOCATION (stmt, loc);
10864 return add_stmt (stmt);
10867 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10868 Remove any elements from the list that are invalid. */
10870 tree
10871 c_finish_omp_clauses (tree clauses)
10873 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10874 tree c, t, *pc = &clauses;
10875 const char *name;
10877 bitmap_obstack_initialize (NULL);
10878 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10879 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10880 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10882 for (pc = &clauses, c = clauses; c ; c = *pc)
10884 bool remove = false;
10885 bool need_complete = false;
10886 bool need_implicitly_determined = false;
10888 switch (OMP_CLAUSE_CODE (c))
10890 case OMP_CLAUSE_SHARED:
10891 name = "shared";
10892 need_implicitly_determined = true;
10893 goto check_dup_generic;
10895 case OMP_CLAUSE_PRIVATE:
10896 name = "private";
10897 need_complete = true;
10898 need_implicitly_determined = true;
10899 goto check_dup_generic;
10901 case OMP_CLAUSE_REDUCTION:
10902 name = "reduction";
10903 need_implicitly_determined = true;
10904 t = OMP_CLAUSE_DECL (c);
10905 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10906 || POINTER_TYPE_P (TREE_TYPE (t)))
10908 error_at (OMP_CLAUSE_LOCATION (c),
10909 "%qE has invalid type for %<reduction%>", t);
10910 remove = true;
10912 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10914 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10915 const char *r_name = NULL;
10917 switch (r_code)
10919 case PLUS_EXPR:
10920 case MULT_EXPR:
10921 case MINUS_EXPR:
10922 case MIN_EXPR:
10923 case MAX_EXPR:
10924 break;
10925 case BIT_AND_EXPR:
10926 r_name = "&";
10927 break;
10928 case BIT_XOR_EXPR:
10929 r_name = "^";
10930 break;
10931 case BIT_IOR_EXPR:
10932 r_name = "|";
10933 break;
10934 case TRUTH_ANDIF_EXPR:
10935 r_name = "&&";
10936 break;
10937 case TRUTH_ORIF_EXPR:
10938 r_name = "||";
10939 break;
10940 default:
10941 gcc_unreachable ();
10943 if (r_name)
10945 error_at (OMP_CLAUSE_LOCATION (c),
10946 "%qE has invalid type for %<reduction(%s)%>",
10947 t, r_name);
10948 remove = true;
10951 goto check_dup_generic;
10953 case OMP_CLAUSE_COPYPRIVATE:
10954 name = "copyprivate";
10955 goto check_dup_generic;
10957 case OMP_CLAUSE_COPYIN:
10958 name = "copyin";
10959 t = OMP_CLAUSE_DECL (c);
10960 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10962 error_at (OMP_CLAUSE_LOCATION (c),
10963 "%qE must be %<threadprivate%> for %<copyin%>", t);
10964 remove = true;
10966 goto check_dup_generic;
10968 check_dup_generic:
10969 t = OMP_CLAUSE_DECL (c);
10970 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10972 error_at (OMP_CLAUSE_LOCATION (c),
10973 "%qE is not a variable in clause %qs", t, name);
10974 remove = true;
10976 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10977 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10978 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10980 error_at (OMP_CLAUSE_LOCATION (c),
10981 "%qE appears more than once in data clauses", t);
10982 remove = true;
10984 else
10985 bitmap_set_bit (&generic_head, DECL_UID (t));
10986 break;
10988 case OMP_CLAUSE_FIRSTPRIVATE:
10989 name = "firstprivate";
10990 t = OMP_CLAUSE_DECL (c);
10991 need_complete = true;
10992 need_implicitly_determined = true;
10993 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10995 error_at (OMP_CLAUSE_LOCATION (c),
10996 "%qE is not a variable in clause %<firstprivate%>", t);
10997 remove = true;
10999 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11000 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
11002 error_at (OMP_CLAUSE_LOCATION (c),
11003 "%qE appears more than once in data clauses", t);
11004 remove = true;
11006 else
11007 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
11008 break;
11010 case OMP_CLAUSE_LASTPRIVATE:
11011 name = "lastprivate";
11012 t = OMP_CLAUSE_DECL (c);
11013 need_complete = true;
11014 need_implicitly_determined = true;
11015 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11017 error_at (OMP_CLAUSE_LOCATION (c),
11018 "%qE is not a variable in clause %<lastprivate%>", t);
11019 remove = true;
11021 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11022 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11024 error_at (OMP_CLAUSE_LOCATION (c),
11025 "%qE appears more than once in data clauses", t);
11026 remove = true;
11028 else
11029 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
11030 break;
11032 case OMP_CLAUSE_IF:
11033 case OMP_CLAUSE_NUM_THREADS:
11034 case OMP_CLAUSE_SCHEDULE:
11035 case OMP_CLAUSE_NOWAIT:
11036 case OMP_CLAUSE_ORDERED:
11037 case OMP_CLAUSE_DEFAULT:
11038 case OMP_CLAUSE_UNTIED:
11039 case OMP_CLAUSE_COLLAPSE:
11040 case OMP_CLAUSE_FINAL:
11041 case OMP_CLAUSE_MERGEABLE:
11042 pc = &OMP_CLAUSE_CHAIN (c);
11043 continue;
11045 default:
11046 gcc_unreachable ();
11049 if (!remove)
11051 t = OMP_CLAUSE_DECL (c);
11053 if (need_complete)
11055 t = require_complete_type (t);
11056 if (t == error_mark_node)
11057 remove = true;
11060 if (need_implicitly_determined)
11062 const char *share_name = NULL;
11064 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11065 share_name = "threadprivate";
11066 else switch (c_omp_predetermined_sharing (t))
11068 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
11069 break;
11070 case OMP_CLAUSE_DEFAULT_SHARED:
11071 /* const vars may be specified in firstprivate clause. */
11072 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11073 && TREE_READONLY (t))
11074 break;
11075 share_name = "shared";
11076 break;
11077 case OMP_CLAUSE_DEFAULT_PRIVATE:
11078 share_name = "private";
11079 break;
11080 default:
11081 gcc_unreachable ();
11083 if (share_name)
11085 error_at (OMP_CLAUSE_LOCATION (c),
11086 "%qE is predetermined %qs for %qs",
11087 t, share_name, name);
11088 remove = true;
11093 if (remove)
11094 *pc = OMP_CLAUSE_CHAIN (c);
11095 else
11096 pc = &OMP_CLAUSE_CHAIN (c);
11099 bitmap_obstack_release (NULL);
11100 return clauses;
11103 /* Create a transaction node. */
11105 tree
11106 c_finish_transaction (location_t loc, tree block, int flags)
11108 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
11109 if (flags & TM_STMT_ATTR_OUTER)
11110 TRANSACTION_EXPR_OUTER (stmt) = 1;
11111 if (flags & TM_STMT_ATTR_RELAXED)
11112 TRANSACTION_EXPR_RELAXED (stmt) = 1;
11113 return add_stmt (stmt);
11116 /* Make a variant type in the proper way for C/C++, propagating qualifiers
11117 down to the element type of an array. */
11119 tree
11120 c_build_qualified_type_1 (tree type, int type_quals, tree layout_qualifier)
11122 if (type == error_mark_node)
11123 return type;
11125 if (TREE_CODE (type) == ARRAY_TYPE)
11127 tree t;
11128 tree element_type = c_build_qualified_type_1 (TREE_TYPE (type),
11129 type_quals,
11130 layout_qualifier);
11132 /* See if we already have an identically qualified type. */
11133 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11135 const tree t_elem_type = strip_array_types (t);
11136 tree t_elem_block_factor = TYPE_BLOCK_FACTOR (t_elem_type);
11137 if (TYPE_QUALS (t_elem_type) == type_quals
11138 && (t_elem_block_factor == layout_qualifier
11139 || tree_int_cst_equal (t_elem_block_factor,
11140 layout_qualifier))
11141 && TYPE_NAME (t) == TYPE_NAME (type)
11142 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
11143 && attribute_list_equal (TYPE_ATTRIBUTES (t),
11144 TYPE_ATTRIBUTES (type)))
11145 break;
11147 if (!t)
11149 tree domain = TYPE_DOMAIN (type);
11151 t = build_variant_type_copy (type);
11152 TREE_TYPE (t) = element_type;
11154 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
11155 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
11156 SET_TYPE_STRUCTURAL_EQUALITY (t);
11157 else if (TYPE_CANONICAL (element_type) != element_type
11158 || (domain && TYPE_CANONICAL (domain) != domain))
11160 tree unqualified_canon
11161 = build_array_type (TYPE_CANONICAL (element_type),
11162 domain? TYPE_CANONICAL (domain)
11163 : NULL_TREE);
11164 TYPE_CANONICAL (t)
11165 = c_build_qualified_type_1 (unqualified_canon, type_quals,
11166 layout_qualifier);
11168 else
11169 TYPE_CANONICAL (t) = t;
11171 return t;
11174 /* A restrict-qualified pointer type must be a pointer to object or
11175 incomplete type. Note that the use of POINTER_TYPE_P also allows
11176 REFERENCE_TYPEs, which is appropriate for C++. */
11177 if ((type_quals & TYPE_QUAL_RESTRICT)
11178 && (!POINTER_TYPE_P (type)
11179 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
11181 error ("invalid use of %<restrict%>");
11182 type_quals &= ~TYPE_QUAL_RESTRICT;
11185 return build_qualified_type_1 (type, type_quals, layout_qualifier);
11188 /* Build a VA_ARG_EXPR for the C parser. */
11190 tree
11191 c_build_va_arg (location_t loc, tree expr, tree type)
11193 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
11194 warning_at (loc, OPT_Wc___compat,
11195 "C++ requires promoted type, not enum type, in %<va_arg%>");
11196 return build_va_arg (loc, expr, type);