Revert wrong checkin
[official-gcc.git] / gcc / c-typeck.c
bloba4516064e509f1243d9e1b6ccfb9e5f818caf052
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "langhooks.h"
34 #include "c-tree.h"
35 #include "c-lang.h"
36 #include "flags.h"
37 #include "output.h"
38 #include "intl.h"
39 #include "target.h"
40 #include "tree-iterator.h"
41 #include "bitmap.h"
42 #include "gimple.h"
43 #include "c-family/c-objc.h"
45 /* Possible cases of implicit bad conversions. Used to select
46 diagnostic messages in convert_for_assignment. */
47 enum impl_conv {
48 ic_argpass,
49 ic_assign,
50 ic_init,
51 ic_return
54 /* The level of nesting inside "__alignof__". */
55 int in_alignof;
57 /* The level of nesting inside "sizeof". */
58 int in_sizeof;
60 /* The level of nesting inside "typeof". */
61 int in_typeof;
63 /* Nonzero if we've already printed a "missing braces around initializer"
64 message within this initializer. */
65 static int missing_braces_mentioned;
67 static int require_constant_value;
68 static int require_constant_elements;
70 static bool null_pointer_constant_p (const_tree);
71 static tree qualify_type (tree, tree);
72 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
73 bool *);
74 static int comp_target_types (location_t, tree, tree);
75 static int function_types_compatible_p (const_tree, const_tree, bool *,
76 bool *);
77 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
78 static tree lookup_field (tree, tree);
79 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
80 tree);
81 static tree pointer_diff (location_t, tree, tree);
82 static tree convert_for_assignment (location_t, tree, tree, tree,
83 enum impl_conv, bool, tree, tree, int);
84 static tree valid_compound_expr_initializer (tree, tree);
85 static void push_string (const char *);
86 static void push_member_name (tree);
87 static int spelling_length (void);
88 static char *print_spelling (char *);
89 static void warning_init (int, const char *);
90 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
91 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
92 struct obstack *);
93 static void output_pending_init_elements (int, struct obstack *);
94 static int set_designator (int, struct obstack *);
95 static void push_range_stack (tree, struct obstack *);
96 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
97 static void set_nonincremental_init (struct obstack *);
98 static void set_nonincremental_init_from_string (tree, struct obstack *);
99 static tree find_init_member (tree, struct obstack *);
100 static void readonly_warning (tree, enum lvalue_use);
101 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
102 static void record_maybe_used_decl (tree);
103 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
105 /* Return true if EXP is a null pointer constant, false otherwise. */
107 static bool
108 null_pointer_constant_p (const_tree expr)
110 /* This should really operate on c_expr structures, but they aren't
111 yet available everywhere required. */
112 tree type = TREE_TYPE (expr);
113 return (TREE_CODE (expr) == INTEGER_CST
114 && !TREE_OVERFLOW (expr)
115 && integer_zerop (expr)
116 && (INTEGRAL_TYPE_P (type)
117 || (TREE_CODE (type) == POINTER_TYPE
118 && VOID_TYPE_P (TREE_TYPE (type))
119 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
122 /* EXPR may appear in an unevaluated part of an integer constant
123 expression, but not in an evaluated part. Wrap it in a
124 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
125 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
127 static tree
128 note_integer_operands (tree expr)
130 tree ret;
131 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
133 ret = copy_node (expr);
134 TREE_OVERFLOW (ret) = 1;
136 else
138 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
139 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
141 return ret;
144 /* Having checked whether EXPR may appear in an unevaluated part of an
145 integer constant expression and found that it may, remove any
146 C_MAYBE_CONST_EXPR noting this fact and return the resulting
147 expression. */
149 static inline tree
150 remove_c_maybe_const_expr (tree expr)
152 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
153 return C_MAYBE_CONST_EXPR_EXPR (expr);
154 else
155 return expr;
158 \f/* This is a cache to hold if two types are compatible or not. */
160 struct tagged_tu_seen_cache {
161 const struct tagged_tu_seen_cache * next;
162 const_tree t1;
163 const_tree t2;
164 /* The return value of tagged_types_tu_compatible_p if we had seen
165 these two types already. */
166 int val;
169 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
170 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
172 /* Do `exp = require_complete_type (exp);' to make sure exp
173 does not have an incomplete type. (That includes void types.) */
175 tree
176 require_complete_type (tree value)
178 tree type = TREE_TYPE (value);
180 if (value == error_mark_node || type == error_mark_node)
181 return error_mark_node;
183 /* First, detect a valid value with a complete type. */
184 if (COMPLETE_TYPE_P (type))
185 return value;
187 c_incomplete_type_error (value, type);
188 return error_mark_node;
191 /* Print an error message for invalid use of an incomplete type.
192 VALUE is the expression that was used (or 0 if that isn't known)
193 and TYPE is the type that was invalid. */
195 void
196 c_incomplete_type_error (const_tree value, const_tree type)
198 const char *type_code_string;
200 /* Avoid duplicate error message. */
201 if (TREE_CODE (type) == ERROR_MARK)
202 return;
204 if (value != 0 && (TREE_CODE (value) == VAR_DECL
205 || TREE_CODE (value) == PARM_DECL))
206 error ("%qD has an incomplete type", value);
207 else
209 retry:
210 /* We must print an error message. Be clever about what it says. */
212 switch (TREE_CODE (type))
214 case RECORD_TYPE:
215 type_code_string = "struct";
216 break;
218 case UNION_TYPE:
219 type_code_string = "union";
220 break;
222 case ENUMERAL_TYPE:
223 type_code_string = "enum";
224 break;
226 case VOID_TYPE:
227 error ("invalid use of void expression");
228 return;
230 case ARRAY_TYPE:
231 if (TYPE_DOMAIN (type))
233 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
235 error ("invalid use of flexible array member");
236 return;
238 type = TREE_TYPE (type);
239 goto retry;
241 error ("invalid use of array with unspecified bounds");
242 return;
244 default:
245 gcc_unreachable ();
248 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
249 error ("invalid use of undefined type %<%s %E%>",
250 type_code_string, TYPE_NAME (type));
251 else
252 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
253 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
257 /* Given a type, apply default promotions wrt unnamed function
258 arguments and return the new type. */
260 tree
261 c_type_promotes_to (tree type)
263 if (TYPE_MAIN_VARIANT (type) == float_type_node)
264 return double_type_node;
266 if (c_promoting_integer_type_p (type))
268 /* Preserve unsignedness if not really getting any wider. */
269 if (TYPE_UNSIGNED (type)
270 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
271 return unsigned_type_node;
272 return integer_type_node;
275 return type;
278 /* Return true if between two named address spaces, whether there is a superset
279 named address space that encompasses both address spaces. If there is a
280 superset, return which address space is the superset. */
282 static bool
283 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
285 if (as1 == as2)
287 *common = as1;
288 return true;
290 else if (targetm.addr_space.subset_p (as1, as2))
292 *common = as2;
293 return true;
295 else if (targetm.addr_space.subset_p (as2, as1))
297 *common = as1;
298 return true;
300 else
301 return false;
304 /* Return a variant of TYPE which has all the type qualifiers of LIKE
305 as well as those of TYPE. */
307 static tree
308 qualify_type (tree type, tree like)
310 addr_space_t as_type = TYPE_ADDR_SPACE (type);
311 addr_space_t as_like = TYPE_ADDR_SPACE (like);
312 addr_space_t as_common;
314 /* If the two named address spaces are different, determine the common
315 superset address space. If there isn't one, raise an error. */
316 if (!addr_space_superset (as_type, as_like, &as_common))
318 as_common = as_type;
319 error ("%qT and %qT are in disjoint named address spaces",
320 type, like);
323 return c_build_qualified_type (type,
324 TYPE_QUALS_NO_ADDR_SPACE (type)
325 | TYPE_QUALS_NO_ADDR_SPACE (like)
326 | ENCODE_QUAL_ADDR_SPACE (as_common));
329 /* Return true iff the given tree T is a variable length array. */
331 bool
332 c_vla_type_p (const_tree t)
334 if (TREE_CODE (t) == ARRAY_TYPE
335 && C_TYPE_VARIABLE_SIZE (t))
336 return true;
337 return false;
340 /* Return the composite type of two compatible types.
342 We assume that comptypes has already been done and returned
343 nonzero; if that isn't so, this may crash. In particular, we
344 assume that qualifiers match. */
346 tree
347 composite_type (tree t1, tree t2)
349 enum tree_code code1;
350 enum tree_code code2;
351 tree attributes;
353 /* Save time if the two types are the same. */
355 if (t1 == t2) return t1;
357 /* If one type is nonsense, use the other. */
358 if (t1 == error_mark_node)
359 return t2;
360 if (t2 == error_mark_node)
361 return t1;
363 code1 = TREE_CODE (t1);
364 code2 = TREE_CODE (t2);
366 /* Merge the attributes. */
367 attributes = targetm.merge_type_attributes (t1, t2);
369 /* If one is an enumerated type and the other is the compatible
370 integer type, the composite type might be either of the two
371 (DR#013 question 3). For consistency, use the enumerated type as
372 the composite type. */
374 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
375 return t1;
376 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
377 return t2;
379 gcc_assert (code1 == code2);
381 switch (code1)
383 case POINTER_TYPE:
384 /* For two pointers, do this recursively on the target type. */
386 tree pointed_to_1 = TREE_TYPE (t1);
387 tree pointed_to_2 = TREE_TYPE (t2);
388 tree target = composite_type (pointed_to_1, pointed_to_2);
389 t1 = build_pointer_type (target);
390 t1 = build_type_attribute_variant (t1, attributes);
391 return qualify_type (t1, t2);
394 case ARRAY_TYPE:
396 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
397 int quals;
398 tree unqual_elt;
399 tree d1 = TYPE_DOMAIN (t1);
400 tree d2 = TYPE_DOMAIN (t2);
401 bool d1_variable, d2_variable;
402 bool d1_zero, d2_zero;
403 bool t1_complete, t2_complete;
405 /* We should not have any type quals on arrays at all. */
406 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
407 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
409 t1_complete = COMPLETE_TYPE_P (t1);
410 t2_complete = COMPLETE_TYPE_P (t2);
412 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
413 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
415 d1_variable = (!d1_zero
416 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
417 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
418 d2_variable = (!d2_zero
419 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
420 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
421 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
422 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
424 /* Save space: see if the result is identical to one of the args. */
425 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
426 && (d2_variable || d2_zero || !d1_variable))
427 return build_type_attribute_variant (t1, attributes);
428 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
429 && (d1_variable || d1_zero || !d2_variable))
430 return build_type_attribute_variant (t2, attributes);
432 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
433 return build_type_attribute_variant (t1, attributes);
434 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
435 return build_type_attribute_variant (t2, attributes);
437 /* Merge the element types, and have a size if either arg has
438 one. We may have qualifiers on the element types. To set
439 up TYPE_MAIN_VARIANT correctly, we need to form the
440 composite of the unqualified types and add the qualifiers
441 back at the end. */
442 quals = TYPE_QUALS (strip_array_types (elt));
443 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
444 t1 = build_array_type (unqual_elt,
445 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
446 && (d2_variable
447 || d2_zero
448 || !d1_variable))
449 ? t1
450 : t2));
451 /* Ensure a composite type involving a zero-length array type
452 is a zero-length type not an incomplete type. */
453 if (d1_zero && d2_zero
454 && (t1_complete || t2_complete)
455 && !COMPLETE_TYPE_P (t1))
457 TYPE_SIZE (t1) = bitsize_zero_node;
458 TYPE_SIZE_UNIT (t1) = size_zero_node;
460 t1 = c_build_qualified_type (t1, quals);
461 return build_type_attribute_variant (t1, attributes);
464 case ENUMERAL_TYPE:
465 case RECORD_TYPE:
466 case UNION_TYPE:
467 if (attributes != NULL)
469 /* Try harder not to create a new aggregate type. */
470 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
471 return t1;
472 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
473 return t2;
475 return build_type_attribute_variant (t1, attributes);
477 case FUNCTION_TYPE:
478 /* Function types: prefer the one that specified arg types.
479 If both do, merge the arg types. Also merge the return types. */
481 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
482 tree p1 = TYPE_ARG_TYPES (t1);
483 tree p2 = TYPE_ARG_TYPES (t2);
484 int len;
485 tree newargs, n;
486 int i;
488 /* Save space: see if the result is identical to one of the args. */
489 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
490 return build_type_attribute_variant (t1, attributes);
491 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
492 return build_type_attribute_variant (t2, attributes);
494 /* Simple way if one arg fails to specify argument types. */
495 if (TYPE_ARG_TYPES (t1) == 0)
497 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
498 t1 = build_type_attribute_variant (t1, attributes);
499 return qualify_type (t1, t2);
501 if (TYPE_ARG_TYPES (t2) == 0)
503 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
504 t1 = build_type_attribute_variant (t1, attributes);
505 return qualify_type (t1, t2);
508 /* If both args specify argument types, we must merge the two
509 lists, argument by argument. */
511 len = list_length (p1);
512 newargs = 0;
514 for (i = 0; i < len; i++)
515 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
517 n = newargs;
519 for (; p1;
520 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
522 /* A null type means arg type is not specified.
523 Take whatever the other function type has. */
524 if (TREE_VALUE (p1) == 0)
526 TREE_VALUE (n) = TREE_VALUE (p2);
527 goto parm_done;
529 if (TREE_VALUE (p2) == 0)
531 TREE_VALUE (n) = TREE_VALUE (p1);
532 goto parm_done;
535 /* Given wait (union {union wait *u; int *i} *)
536 and wait (union wait *),
537 prefer union wait * as type of parm. */
538 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
539 && TREE_VALUE (p1) != TREE_VALUE (p2))
541 tree memb;
542 tree mv2 = TREE_VALUE (p2);
543 if (mv2 && mv2 != error_mark_node
544 && TREE_CODE (mv2) != ARRAY_TYPE)
545 mv2 = TYPE_MAIN_VARIANT (mv2);
546 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
547 memb; memb = DECL_CHAIN (memb))
549 tree mv3 = TREE_TYPE (memb);
550 if (mv3 && mv3 != error_mark_node
551 && TREE_CODE (mv3) != ARRAY_TYPE)
552 mv3 = TYPE_MAIN_VARIANT (mv3);
553 if (comptypes (mv3, mv2))
555 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
556 TREE_VALUE (p2));
557 pedwarn (input_location, OPT_pedantic,
558 "function types not truly compatible in ISO C");
559 goto parm_done;
563 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
564 && TREE_VALUE (p2) != TREE_VALUE (p1))
566 tree memb;
567 tree mv1 = TREE_VALUE (p1);
568 if (mv1 && mv1 != error_mark_node
569 && TREE_CODE (mv1) != ARRAY_TYPE)
570 mv1 = TYPE_MAIN_VARIANT (mv1);
571 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
572 memb; memb = DECL_CHAIN (memb))
574 tree mv3 = TREE_TYPE (memb);
575 if (mv3 && mv3 != error_mark_node
576 && TREE_CODE (mv3) != ARRAY_TYPE)
577 mv3 = TYPE_MAIN_VARIANT (mv3);
578 if (comptypes (mv3, mv1))
580 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
581 TREE_VALUE (p1));
582 pedwarn (input_location, OPT_pedantic,
583 "function types not truly compatible in ISO C");
584 goto parm_done;
588 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
589 parm_done: ;
592 t1 = build_function_type (valtype, newargs);
593 t1 = qualify_type (t1, t2);
594 /* ... falls through ... */
597 default:
598 return build_type_attribute_variant (t1, attributes);
603 /* Return the type of a conditional expression between pointers to
604 possibly differently qualified versions of compatible types.
606 We assume that comp_target_types has already been done and returned
607 nonzero; if that isn't so, this may crash. */
609 static tree
610 common_pointer_type (tree t1, tree t2)
612 tree attributes;
613 tree pointed_to_1, mv1;
614 tree pointed_to_2, mv2;
615 tree target;
616 unsigned target_quals;
617 addr_space_t as1, as2, as_common;
618 int quals1, quals2;
620 /* Save time if the two types are the same. */
622 if (t1 == t2) return t1;
624 /* If one type is nonsense, use the other. */
625 if (t1 == error_mark_node)
626 return t2;
627 if (t2 == error_mark_node)
628 return t1;
630 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
631 && TREE_CODE (t2) == POINTER_TYPE);
633 /* Merge the attributes. */
634 attributes = targetm.merge_type_attributes (t1, t2);
636 /* Find the composite type of the target types, and combine the
637 qualifiers of the two types' targets. Do not lose qualifiers on
638 array element types by taking the TYPE_MAIN_VARIANT. */
639 mv1 = pointed_to_1 = TREE_TYPE (t1);
640 mv2 = pointed_to_2 = TREE_TYPE (t2);
641 if (TREE_CODE (mv1) != ARRAY_TYPE)
642 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
643 if (TREE_CODE (mv2) != ARRAY_TYPE)
644 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
645 target = composite_type (mv1, mv2);
647 /* For function types do not merge const qualifiers, but drop them
648 if used inconsistently. The middle-end uses these to mark const
649 and noreturn functions. */
650 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
651 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
653 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
654 target_quals = (quals1 & quals2);
655 else
656 target_quals = (quals1 | quals2);
658 /* If the two named address spaces are different, determine the common
659 superset address space. This is guaranteed to exist due to the
660 assumption that comp_target_type returned non-zero. */
661 as1 = TYPE_ADDR_SPACE (pointed_to_1);
662 as2 = TYPE_ADDR_SPACE (pointed_to_2);
663 if (!addr_space_superset (as1, as2, &as_common))
664 gcc_unreachable ();
666 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
668 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
669 return build_type_attribute_variant (t1, attributes);
672 /* Return the common type for two arithmetic types under the usual
673 arithmetic conversions. The default conversions have already been
674 applied, and enumerated types converted to their compatible integer
675 types. The resulting type is unqualified and has no attributes.
677 This is the type for the result of most arithmetic operations
678 if the operands have the given two types. */
680 static tree
681 c_common_type (tree t1, tree t2)
683 enum tree_code code1;
684 enum tree_code code2;
686 /* If one type is nonsense, use the other. */
687 if (t1 == error_mark_node)
688 return t2;
689 if (t2 == error_mark_node)
690 return t1;
692 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
693 t1 = TYPE_MAIN_VARIANT (t1);
695 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
696 t2 = TYPE_MAIN_VARIANT (t2);
698 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
699 t1 = build_type_attribute_variant (t1, NULL_TREE);
701 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
702 t2 = build_type_attribute_variant (t2, NULL_TREE);
704 /* Save time if the two types are the same. */
706 if (t1 == t2) return t1;
708 code1 = TREE_CODE (t1);
709 code2 = TREE_CODE (t2);
711 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
712 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
713 || code1 == INTEGER_TYPE);
714 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
715 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
716 || code2 == INTEGER_TYPE);
718 /* When one operand is a decimal float type, the other operand cannot be
719 a generic float type or a complex type. We also disallow vector types
720 here. */
721 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
722 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
724 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
726 error ("can%'t mix operands of decimal float and vector types");
727 return error_mark_node;
729 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
731 error ("can%'t mix operands of decimal float and complex types");
732 return error_mark_node;
734 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
736 error ("can%'t mix operands of decimal float and other float types");
737 return error_mark_node;
741 /* If one type is a vector type, return that type. (How the usual
742 arithmetic conversions apply to the vector types extension is not
743 precisely specified.) */
744 if (code1 == VECTOR_TYPE)
745 return t1;
747 if (code2 == VECTOR_TYPE)
748 return t2;
750 /* If one type is complex, form the common type of the non-complex
751 components, then make that complex. Use T1 or T2 if it is the
752 required type. */
753 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
755 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
756 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
757 tree subtype = c_common_type (subtype1, subtype2);
759 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
760 return t1;
761 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
762 return t2;
763 else
764 return build_complex_type (subtype);
767 /* If only one is real, use it as the result. */
769 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
770 return t1;
772 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
773 return t2;
775 /* If both are real and either are decimal floating point types, use
776 the decimal floating point type with the greater precision. */
778 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
780 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
781 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
782 return dfloat128_type_node;
783 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
784 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
785 return dfloat64_type_node;
786 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
787 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
788 return dfloat32_type_node;
791 /* Deal with fixed-point types. */
792 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
794 unsigned int unsignedp = 0, satp = 0;
795 enum machine_mode m1, m2;
796 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
798 m1 = TYPE_MODE (t1);
799 m2 = TYPE_MODE (t2);
801 /* If one input type is saturating, the result type is saturating. */
802 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
803 satp = 1;
805 /* If both fixed-point types are unsigned, the result type is unsigned.
806 When mixing fixed-point and integer types, follow the sign of the
807 fixed-point type.
808 Otherwise, the result type is signed. */
809 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
810 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
811 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
812 && TYPE_UNSIGNED (t1))
813 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
814 && TYPE_UNSIGNED (t2)))
815 unsignedp = 1;
817 /* The result type is signed. */
818 if (unsignedp == 0)
820 /* If the input type is unsigned, we need to convert to the
821 signed type. */
822 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
824 enum mode_class mclass = (enum mode_class) 0;
825 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
826 mclass = MODE_FRACT;
827 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
828 mclass = MODE_ACCUM;
829 else
830 gcc_unreachable ();
831 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
833 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
835 enum mode_class mclass = (enum mode_class) 0;
836 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
837 mclass = MODE_FRACT;
838 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
839 mclass = MODE_ACCUM;
840 else
841 gcc_unreachable ();
842 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
846 if (code1 == FIXED_POINT_TYPE)
848 fbit1 = GET_MODE_FBIT (m1);
849 ibit1 = GET_MODE_IBIT (m1);
851 else
853 fbit1 = 0;
854 /* Signed integers need to subtract one sign bit. */
855 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
858 if (code2 == FIXED_POINT_TYPE)
860 fbit2 = GET_MODE_FBIT (m2);
861 ibit2 = GET_MODE_IBIT (m2);
863 else
865 fbit2 = 0;
866 /* Signed integers need to subtract one sign bit. */
867 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
870 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
871 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
872 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
873 satp);
876 /* Both real or both integers; use the one with greater precision. */
878 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
879 return t1;
880 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
881 return t2;
883 /* Same precision. Prefer long longs to longs to ints when the
884 same precision, following the C99 rules on integer type rank
885 (which are equivalent to the C90 rules for C90 types). */
887 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
888 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
889 return long_long_unsigned_type_node;
891 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
892 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
894 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
895 return long_long_unsigned_type_node;
896 else
897 return long_long_integer_type_node;
900 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
901 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
902 return long_unsigned_type_node;
904 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
905 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
907 /* But preserve unsignedness from the other type,
908 since long cannot hold all the values of an unsigned int. */
909 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
910 return long_unsigned_type_node;
911 else
912 return long_integer_type_node;
915 /* Likewise, prefer long double to double even if same size. */
916 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
918 return long_double_type_node;
920 /* Otherwise prefer the unsigned one. */
922 if (TYPE_UNSIGNED (t1))
923 return t1;
924 else
925 return t2;
928 /* Wrapper around c_common_type that is used by c-common.c and other
929 front end optimizations that remove promotions. ENUMERAL_TYPEs
930 are allowed here and are converted to their compatible integer types.
931 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
932 preferably a non-Boolean type as the common type. */
933 tree
934 common_type (tree t1, tree t2)
936 if (TREE_CODE (t1) == ENUMERAL_TYPE)
937 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
938 if (TREE_CODE (t2) == ENUMERAL_TYPE)
939 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
941 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
942 if (TREE_CODE (t1) == BOOLEAN_TYPE
943 && TREE_CODE (t2) == BOOLEAN_TYPE)
944 return boolean_type_node;
946 /* If either type is BOOLEAN_TYPE, then return the other. */
947 if (TREE_CODE (t1) == BOOLEAN_TYPE)
948 return t2;
949 if (TREE_CODE (t2) == BOOLEAN_TYPE)
950 return t1;
952 return c_common_type (t1, t2);
955 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
956 or various other operations. Return 2 if they are compatible
957 but a warning may be needed if you use them together. */
960 comptypes (tree type1, tree type2)
962 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
963 int val;
965 val = comptypes_internal (type1, type2, NULL, NULL);
966 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
968 return val;
971 /* Like comptypes, but if it returns non-zero because enum and int are
972 compatible, it sets *ENUM_AND_INT_P to true. */
974 static int
975 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
977 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
978 int val;
980 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
981 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
983 return val;
986 /* Like comptypes, but if it returns nonzero for different types, it
987 sets *DIFFERENT_TYPES_P to true. */
990 comptypes_check_different_types (tree type1, tree type2,
991 bool *different_types_p)
993 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
994 int val;
996 val = comptypes_internal (type1, type2, NULL, different_types_p);
997 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
999 return val;
1002 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1003 or various other operations. Return 2 if they are compatible
1004 but a warning may be needed if you use them together. If
1005 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1006 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1007 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1008 NULL, and the types are compatible but different enough not to be
1009 permitted in C1X typedef redeclarations, then this sets
1010 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1011 false, but may or may not be set if the types are incompatible.
1012 This differs from comptypes, in that we don't free the seen
1013 types. */
1015 static int
1016 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1017 bool *different_types_p)
1019 const_tree t1 = type1;
1020 const_tree t2 = type2;
1021 int attrval, val;
1023 /* Suppress errors caused by previously reported errors. */
1025 if (t1 == t2 || !t1 || !t2
1026 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1027 return 1;
1029 /* Enumerated types are compatible with integer types, but this is
1030 not transitive: two enumerated types in the same translation unit
1031 are compatible with each other only if they are the same type. */
1033 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1035 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1036 if (TREE_CODE (t2) != VOID_TYPE)
1038 if (enum_and_int_p != NULL)
1039 *enum_and_int_p = true;
1040 if (different_types_p != NULL)
1041 *different_types_p = true;
1044 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1046 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1047 if (TREE_CODE (t1) != VOID_TYPE)
1049 if (enum_and_int_p != NULL)
1050 *enum_and_int_p = true;
1051 if (different_types_p != NULL)
1052 *different_types_p = true;
1056 if (t1 == t2)
1057 return 1;
1059 /* Different classes of types can't be compatible. */
1061 if (TREE_CODE (t1) != TREE_CODE (t2))
1062 return 0;
1064 /* Qualifiers must match. C99 6.7.3p9 */
1066 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1067 return 0;
1069 /* Allow for two different type nodes which have essentially the same
1070 definition. Note that we already checked for equality of the type
1071 qualifiers (just above). */
1073 if (TREE_CODE (t1) != ARRAY_TYPE
1074 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1075 return 1;
1077 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1078 if (!(attrval = comp_type_attributes (t1, t2)))
1079 return 0;
1081 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1082 val = 0;
1084 switch (TREE_CODE (t1))
1086 case POINTER_TYPE:
1087 /* Do not remove mode or aliasing information. */
1088 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1089 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1090 break;
1091 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1092 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1093 enum_and_int_p, different_types_p));
1094 break;
1096 case FUNCTION_TYPE:
1097 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1098 different_types_p);
1099 break;
1101 case ARRAY_TYPE:
1103 tree d1 = TYPE_DOMAIN (t1);
1104 tree d2 = TYPE_DOMAIN (t2);
1105 bool d1_variable, d2_variable;
1106 bool d1_zero, d2_zero;
1107 val = 1;
1109 /* Target types must match incl. qualifiers. */
1110 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1111 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1112 enum_and_int_p,
1113 different_types_p)))
1114 return 0;
1116 if (different_types_p != NULL
1117 && (d1 == 0) != (d2 == 0))
1118 *different_types_p = true;
1119 /* Sizes must match unless one is missing or variable. */
1120 if (d1 == 0 || d2 == 0 || d1 == d2)
1121 break;
1123 d1_zero = !TYPE_MAX_VALUE (d1);
1124 d2_zero = !TYPE_MAX_VALUE (d2);
1126 d1_variable = (!d1_zero
1127 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1128 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1129 d2_variable = (!d2_zero
1130 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1131 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1132 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1133 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1135 if (different_types_p != NULL
1136 && d1_variable != d2_variable)
1137 *different_types_p = true;
1138 if (d1_variable || d2_variable)
1139 break;
1140 if (d1_zero && d2_zero)
1141 break;
1142 if (d1_zero || d2_zero
1143 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1144 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1145 val = 0;
1147 break;
1150 case ENUMERAL_TYPE:
1151 case RECORD_TYPE:
1152 case UNION_TYPE:
1153 if (val != 1 && !same_translation_unit_p (t1, t2))
1155 tree a1 = TYPE_ATTRIBUTES (t1);
1156 tree a2 = TYPE_ATTRIBUTES (t2);
1158 if (! attribute_list_contained (a1, a2)
1159 && ! attribute_list_contained (a2, a1))
1160 break;
1162 if (attrval != 2)
1163 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1164 different_types_p);
1165 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1166 different_types_p);
1168 break;
1170 case VECTOR_TYPE:
1171 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1172 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1173 enum_and_int_p, different_types_p));
1174 break;
1176 default:
1177 break;
1179 return attrval == 2 && val == 1 ? 2 : val;
1182 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1183 their qualifiers, except for named address spaces. If the pointers point to
1184 different named addresses, then we must determine if one address space is a
1185 subset of the other. */
1187 static int
1188 comp_target_types (location_t location, tree ttl, tree ttr)
1190 int val;
1191 tree mvl = TREE_TYPE (ttl);
1192 tree mvr = TREE_TYPE (ttr);
1193 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1194 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1195 addr_space_t as_common;
1196 bool enum_and_int_p;
1198 /* Fail if pointers point to incompatible address spaces. */
1199 if (!addr_space_superset (asl, asr, &as_common))
1200 return 0;
1202 /* Do not lose qualifiers on element types of array types that are
1203 pointer targets by taking their TYPE_MAIN_VARIANT. */
1204 if (TREE_CODE (mvl) != ARRAY_TYPE)
1205 mvl = TYPE_MAIN_VARIANT (mvl);
1206 if (TREE_CODE (mvr) != ARRAY_TYPE)
1207 mvr = TYPE_MAIN_VARIANT (mvr);
1208 enum_and_int_p = false;
1209 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1211 if (val == 2)
1212 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1214 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1215 warning_at (location, OPT_Wc___compat,
1216 "pointer target types incompatible in C++");
1218 return val;
1221 /* Subroutines of `comptypes'. */
1223 /* Determine whether two trees derive from the same translation unit.
1224 If the CONTEXT chain ends in a null, that tree's context is still
1225 being parsed, so if two trees have context chains ending in null,
1226 they're in the same translation unit. */
1228 same_translation_unit_p (const_tree t1, const_tree t2)
1230 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1231 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1233 case tcc_declaration:
1234 t1 = DECL_CONTEXT (t1); break;
1235 case tcc_type:
1236 t1 = TYPE_CONTEXT (t1); break;
1237 case tcc_exceptional:
1238 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1239 default: gcc_unreachable ();
1242 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1243 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1245 case tcc_declaration:
1246 t2 = DECL_CONTEXT (t2); break;
1247 case tcc_type:
1248 t2 = TYPE_CONTEXT (t2); break;
1249 case tcc_exceptional:
1250 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1251 default: gcc_unreachable ();
1254 return t1 == t2;
1257 /* Allocate the seen two types, assuming that they are compatible. */
1259 static struct tagged_tu_seen_cache *
1260 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1262 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1263 tu->next = tagged_tu_seen_base;
1264 tu->t1 = t1;
1265 tu->t2 = t2;
1267 tagged_tu_seen_base = tu;
1269 /* The C standard says that two structures in different translation
1270 units are compatible with each other only if the types of their
1271 fields are compatible (among other things). We assume that they
1272 are compatible until proven otherwise when building the cache.
1273 An example where this can occur is:
1274 struct a
1276 struct a *next;
1278 If we are comparing this against a similar struct in another TU,
1279 and did not assume they were compatible, we end up with an infinite
1280 loop. */
1281 tu->val = 1;
1282 return tu;
1285 /* Free the seen types until we get to TU_TIL. */
1287 static void
1288 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1290 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1291 while (tu != tu_til)
1293 const struct tagged_tu_seen_cache *const tu1
1294 = (const struct tagged_tu_seen_cache *) tu;
1295 tu = tu1->next;
1296 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1298 tagged_tu_seen_base = tu_til;
1301 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1302 compatible. If the two types are not the same (which has been
1303 checked earlier), this can only happen when multiple translation
1304 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1305 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1306 comptypes_internal. */
1308 static int
1309 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1310 bool *enum_and_int_p, bool *different_types_p)
1312 tree s1, s2;
1313 bool needs_warning = false;
1315 /* We have to verify that the tags of the types are the same. This
1316 is harder than it looks because this may be a typedef, so we have
1317 to go look at the original type. It may even be a typedef of a
1318 typedef...
1319 In the case of compiler-created builtin structs the TYPE_DECL
1320 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1321 while (TYPE_NAME (t1)
1322 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1323 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1324 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1326 while (TYPE_NAME (t2)
1327 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1328 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1329 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1331 /* C90 didn't have the requirement that the two tags be the same. */
1332 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1333 return 0;
1335 /* C90 didn't say what happened if one or both of the types were
1336 incomplete; we choose to follow C99 rules here, which is that they
1337 are compatible. */
1338 if (TYPE_SIZE (t1) == NULL
1339 || TYPE_SIZE (t2) == NULL)
1340 return 1;
1343 const struct tagged_tu_seen_cache * tts_i;
1344 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1345 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1346 return tts_i->val;
1349 switch (TREE_CODE (t1))
1351 case ENUMERAL_TYPE:
1353 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1354 /* Speed up the case where the type values are in the same order. */
1355 tree tv1 = TYPE_VALUES (t1);
1356 tree tv2 = TYPE_VALUES (t2);
1358 if (tv1 == tv2)
1360 return 1;
1363 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1365 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1366 break;
1367 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1369 tu->val = 0;
1370 return 0;
1374 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1376 return 1;
1378 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1380 tu->val = 0;
1381 return 0;
1384 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1386 tu->val = 0;
1387 return 0;
1390 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1392 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1393 if (s2 == NULL
1394 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1396 tu->val = 0;
1397 return 0;
1400 return 1;
1403 case UNION_TYPE:
1405 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1406 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1408 tu->val = 0;
1409 return 0;
1412 /* Speed up the common case where the fields are in the same order. */
1413 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1414 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1416 int result;
1418 if (DECL_NAME (s1) != DECL_NAME (s2))
1419 break;
1420 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1421 enum_and_int_p, different_types_p);
1423 if (result != 1 && !DECL_NAME (s1))
1424 break;
1425 if (result == 0)
1427 tu->val = 0;
1428 return 0;
1430 if (result == 2)
1431 needs_warning = true;
1433 if (TREE_CODE (s1) == FIELD_DECL
1434 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1435 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1437 tu->val = 0;
1438 return 0;
1441 if (!s1 && !s2)
1443 tu->val = needs_warning ? 2 : 1;
1444 return tu->val;
1447 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1449 bool ok = false;
1451 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1452 if (DECL_NAME (s1) == DECL_NAME (s2))
1454 int result;
1456 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1457 enum_and_int_p,
1458 different_types_p);
1460 if (result != 1 && !DECL_NAME (s1))
1461 continue;
1462 if (result == 0)
1464 tu->val = 0;
1465 return 0;
1467 if (result == 2)
1468 needs_warning = true;
1470 if (TREE_CODE (s1) == FIELD_DECL
1471 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1472 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1473 break;
1475 ok = true;
1476 break;
1478 if (!ok)
1480 tu->val = 0;
1481 return 0;
1484 tu->val = needs_warning ? 2 : 10;
1485 return tu->val;
1488 case RECORD_TYPE:
1490 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1492 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1493 s1 && s2;
1494 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1496 int result;
1497 if (TREE_CODE (s1) != TREE_CODE (s2)
1498 || DECL_NAME (s1) != DECL_NAME (s2))
1499 break;
1500 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1501 enum_and_int_p, different_types_p);
1502 if (result == 0)
1503 break;
1504 if (result == 2)
1505 needs_warning = true;
1507 if (TREE_CODE (s1) == FIELD_DECL
1508 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1509 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1510 break;
1512 if (s1 && s2)
1513 tu->val = 0;
1514 else
1515 tu->val = needs_warning ? 2 : 1;
1516 return tu->val;
1519 default:
1520 gcc_unreachable ();
1524 /* Return 1 if two function types F1 and F2 are compatible.
1525 If either type specifies no argument types,
1526 the other must specify a fixed number of self-promoting arg types.
1527 Otherwise, if one type specifies only the number of arguments,
1528 the other must specify that number of self-promoting arg types.
1529 Otherwise, the argument types must match.
1530 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1532 static int
1533 function_types_compatible_p (const_tree f1, const_tree f2,
1534 bool *enum_and_int_p, bool *different_types_p)
1536 tree args1, args2;
1537 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1538 int val = 1;
1539 int val1;
1540 tree ret1, ret2;
1542 ret1 = TREE_TYPE (f1);
1543 ret2 = TREE_TYPE (f2);
1545 /* 'volatile' qualifiers on a function's return type used to mean
1546 the function is noreturn. */
1547 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1548 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1549 if (TYPE_VOLATILE (ret1))
1550 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1551 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1552 if (TYPE_VOLATILE (ret2))
1553 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1554 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1555 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1556 if (val == 0)
1557 return 0;
1559 args1 = TYPE_ARG_TYPES (f1);
1560 args2 = TYPE_ARG_TYPES (f2);
1562 if (different_types_p != NULL
1563 && (args1 == 0) != (args2 == 0))
1564 *different_types_p = true;
1566 /* An unspecified parmlist matches any specified parmlist
1567 whose argument types don't need default promotions. */
1569 if (args1 == 0)
1571 if (!self_promoting_args_p (args2))
1572 return 0;
1573 /* If one of these types comes from a non-prototype fn definition,
1574 compare that with the other type's arglist.
1575 If they don't match, ask for a warning (but no error). */
1576 if (TYPE_ACTUAL_ARG_TYPES (f1)
1577 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1578 enum_and_int_p, different_types_p))
1579 val = 2;
1580 return val;
1582 if (args2 == 0)
1584 if (!self_promoting_args_p (args1))
1585 return 0;
1586 if (TYPE_ACTUAL_ARG_TYPES (f2)
1587 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1588 enum_and_int_p, different_types_p))
1589 val = 2;
1590 return val;
1593 /* Both types have argument lists: compare them and propagate results. */
1594 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1595 different_types_p);
1596 return val1 != 1 ? val1 : val;
1599 /* Check two lists of types for compatibility, returning 0 for
1600 incompatible, 1 for compatible, or 2 for compatible with
1601 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1602 comptypes_internal. */
1604 static int
1605 type_lists_compatible_p (const_tree args1, const_tree args2,
1606 bool *enum_and_int_p, bool *different_types_p)
1608 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1609 int val = 1;
1610 int newval = 0;
1612 while (1)
1614 tree a1, mv1, a2, mv2;
1615 if (args1 == 0 && args2 == 0)
1616 return val;
1617 /* If one list is shorter than the other,
1618 they fail to match. */
1619 if (args1 == 0 || args2 == 0)
1620 return 0;
1621 mv1 = a1 = TREE_VALUE (args1);
1622 mv2 = a2 = TREE_VALUE (args2);
1623 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1624 mv1 = TYPE_MAIN_VARIANT (mv1);
1625 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1626 mv2 = TYPE_MAIN_VARIANT (mv2);
1627 /* A null pointer instead of a type
1628 means there is supposed to be an argument
1629 but nothing is specified about what type it has.
1630 So match anything that self-promotes. */
1631 if (different_types_p != NULL
1632 && (a1 == 0) != (a2 == 0))
1633 *different_types_p = true;
1634 if (a1 == 0)
1636 if (c_type_promotes_to (a2) != a2)
1637 return 0;
1639 else if (a2 == 0)
1641 if (c_type_promotes_to (a1) != a1)
1642 return 0;
1644 /* If one of the lists has an error marker, ignore this arg. */
1645 else if (TREE_CODE (a1) == ERROR_MARK
1646 || TREE_CODE (a2) == ERROR_MARK)
1648 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1649 different_types_p)))
1651 if (different_types_p != NULL)
1652 *different_types_p = true;
1653 /* Allow wait (union {union wait *u; int *i} *)
1654 and wait (union wait *) to be compatible. */
1655 if (TREE_CODE (a1) == UNION_TYPE
1656 && (TYPE_NAME (a1) == 0
1657 || TYPE_TRANSPARENT_AGGR (a1))
1658 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1659 && tree_int_cst_equal (TYPE_SIZE (a1),
1660 TYPE_SIZE (a2)))
1662 tree memb;
1663 for (memb = TYPE_FIELDS (a1);
1664 memb; memb = DECL_CHAIN (memb))
1666 tree mv3 = TREE_TYPE (memb);
1667 if (mv3 && mv3 != error_mark_node
1668 && TREE_CODE (mv3) != ARRAY_TYPE)
1669 mv3 = TYPE_MAIN_VARIANT (mv3);
1670 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1671 different_types_p))
1672 break;
1674 if (memb == 0)
1675 return 0;
1677 else if (TREE_CODE (a2) == UNION_TYPE
1678 && (TYPE_NAME (a2) == 0
1679 || TYPE_TRANSPARENT_AGGR (a2))
1680 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1681 && tree_int_cst_equal (TYPE_SIZE (a2),
1682 TYPE_SIZE (a1)))
1684 tree memb;
1685 for (memb = TYPE_FIELDS (a2);
1686 memb; memb = DECL_CHAIN (memb))
1688 tree mv3 = TREE_TYPE (memb);
1689 if (mv3 && mv3 != error_mark_node
1690 && TREE_CODE (mv3) != ARRAY_TYPE)
1691 mv3 = TYPE_MAIN_VARIANT (mv3);
1692 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1693 different_types_p))
1694 break;
1696 if (memb == 0)
1697 return 0;
1699 else
1700 return 0;
1703 /* comptypes said ok, but record if it said to warn. */
1704 if (newval > val)
1705 val = newval;
1707 args1 = TREE_CHAIN (args1);
1708 args2 = TREE_CHAIN (args2);
1712 /* Compute the size to increment a pointer by. */
1714 static tree
1715 c_size_in_bytes (const_tree type)
1717 enum tree_code code = TREE_CODE (type);
1719 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1720 return size_one_node;
1722 if (!COMPLETE_OR_VOID_TYPE_P (type))
1724 error ("arithmetic on pointer to an incomplete type");
1725 return size_one_node;
1728 /* Convert in case a char is more than one unit. */
1729 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1730 size_int (TYPE_PRECISION (char_type_node)
1731 / BITS_PER_UNIT));
1734 /* Return either DECL or its known constant value (if it has one). */
1736 tree
1737 decl_constant_value (tree decl)
1739 if (/* Don't change a variable array bound or initial value to a constant
1740 in a place where a variable is invalid. Note that DECL_INITIAL
1741 isn't valid for a PARM_DECL. */
1742 current_function_decl != 0
1743 && TREE_CODE (decl) != PARM_DECL
1744 && !TREE_THIS_VOLATILE (decl)
1745 && TREE_READONLY (decl)
1746 && DECL_INITIAL (decl) != 0
1747 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1748 /* This is invalid if initial value is not constant.
1749 If it has either a function call, a memory reference,
1750 or a variable, then re-evaluating it could give different results. */
1751 && TREE_CONSTANT (DECL_INITIAL (decl))
1752 /* Check for cases where this is sub-optimal, even though valid. */
1753 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1754 return DECL_INITIAL (decl);
1755 return decl;
1758 /* Convert the array expression EXP to a pointer. */
1759 static tree
1760 array_to_pointer_conversion (location_t loc, tree exp)
1762 tree orig_exp = exp;
1763 tree type = TREE_TYPE (exp);
1764 tree adr;
1765 tree restype = TREE_TYPE (type);
1766 tree ptrtype;
1768 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1770 STRIP_TYPE_NOPS (exp);
1772 if (TREE_NO_WARNING (orig_exp))
1773 TREE_NO_WARNING (exp) = 1;
1775 ptrtype = build_pointer_type (restype);
1777 if (TREE_CODE (exp) == INDIRECT_REF)
1778 return convert (ptrtype, TREE_OPERAND (exp, 0));
1780 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1781 return convert (ptrtype, adr);
1784 /* Convert the function expression EXP to a pointer. */
1785 static tree
1786 function_to_pointer_conversion (location_t loc, tree exp)
1788 tree orig_exp = exp;
1790 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1792 STRIP_TYPE_NOPS (exp);
1794 if (TREE_NO_WARNING (orig_exp))
1795 TREE_NO_WARNING (exp) = 1;
1797 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1800 /* Mark EXP as read, not just set, for set but not used -Wunused
1801 warning purposes. */
1803 void
1804 mark_exp_read (tree exp)
1806 switch (TREE_CODE (exp))
1808 case VAR_DECL:
1809 case PARM_DECL:
1810 DECL_READ_P (exp) = 1;
1811 break;
1812 case ARRAY_REF:
1813 case COMPONENT_REF:
1814 case MODIFY_EXPR:
1815 case REALPART_EXPR:
1816 case IMAGPART_EXPR:
1817 CASE_CONVERT:
1818 case ADDR_EXPR:
1819 mark_exp_read (TREE_OPERAND (exp, 0));
1820 break;
1821 case COMPOUND_EXPR:
1822 case C_MAYBE_CONST_EXPR:
1823 mark_exp_read (TREE_OPERAND (exp, 1));
1824 break;
1825 default:
1826 break;
1830 /* Perform the default conversion of arrays and functions to pointers.
1831 Return the result of converting EXP. For any other expression, just
1832 return EXP.
1834 LOC is the location of the expression. */
1836 struct c_expr
1837 default_function_array_conversion (location_t loc, struct c_expr exp)
1839 tree orig_exp = exp.value;
1840 tree type = TREE_TYPE (exp.value);
1841 enum tree_code code = TREE_CODE (type);
1843 switch (code)
1845 case ARRAY_TYPE:
1847 bool not_lvalue = false;
1848 bool lvalue_array_p;
1850 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1851 || CONVERT_EXPR_P (exp.value))
1852 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1854 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1855 not_lvalue = true;
1856 exp.value = TREE_OPERAND (exp.value, 0);
1859 if (TREE_NO_WARNING (orig_exp))
1860 TREE_NO_WARNING (exp.value) = 1;
1862 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1863 if (!flag_isoc99 && !lvalue_array_p)
1865 /* Before C99, non-lvalue arrays do not decay to pointers.
1866 Normally, using such an array would be invalid; but it can
1867 be used correctly inside sizeof or as a statement expression.
1868 Thus, do not give an error here; an error will result later. */
1869 return exp;
1872 exp.value = array_to_pointer_conversion (loc, exp.value);
1874 break;
1875 case FUNCTION_TYPE:
1876 exp.value = function_to_pointer_conversion (loc, exp.value);
1877 break;
1878 default:
1879 break;
1882 return exp;
1885 struct c_expr
1886 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1888 mark_exp_read (exp.value);
1889 return default_function_array_conversion (loc, exp);
1892 /* EXP is an expression of integer type. Apply the integer promotions
1893 to it and return the promoted value. */
1895 tree
1896 perform_integral_promotions (tree exp)
1898 tree type = TREE_TYPE (exp);
1899 enum tree_code code = TREE_CODE (type);
1901 gcc_assert (INTEGRAL_TYPE_P (type));
1903 /* Normally convert enums to int,
1904 but convert wide enums to something wider. */
1905 if (code == ENUMERAL_TYPE)
1907 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1908 TYPE_PRECISION (integer_type_node)),
1909 ((TYPE_PRECISION (type)
1910 >= TYPE_PRECISION (integer_type_node))
1911 && TYPE_UNSIGNED (type)));
1913 return convert (type, exp);
1916 /* ??? This should no longer be needed now bit-fields have their
1917 proper types. */
1918 if (TREE_CODE (exp) == COMPONENT_REF
1919 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1920 /* If it's thinner than an int, promote it like a
1921 c_promoting_integer_type_p, otherwise leave it alone. */
1922 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1923 TYPE_PRECISION (integer_type_node)))
1924 return convert (integer_type_node, exp);
1926 if (c_promoting_integer_type_p (type))
1928 /* Preserve unsignedness if not really getting any wider. */
1929 if (TYPE_UNSIGNED (type)
1930 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1931 return convert (unsigned_type_node, exp);
1933 return convert (integer_type_node, exp);
1936 return exp;
1940 /* Perform default promotions for C data used in expressions.
1941 Enumeral types or short or char are converted to int.
1942 In addition, manifest constants symbols are replaced by their values. */
1944 tree
1945 default_conversion (tree exp)
1947 tree orig_exp;
1948 tree type = TREE_TYPE (exp);
1949 enum tree_code code = TREE_CODE (type);
1950 tree promoted_type;
1952 mark_exp_read (exp);
1954 /* Functions and arrays have been converted during parsing. */
1955 gcc_assert (code != FUNCTION_TYPE);
1956 if (code == ARRAY_TYPE)
1957 return exp;
1959 /* Constants can be used directly unless they're not loadable. */
1960 if (TREE_CODE (exp) == CONST_DECL)
1961 exp = DECL_INITIAL (exp);
1963 /* Strip no-op conversions. */
1964 orig_exp = exp;
1965 STRIP_TYPE_NOPS (exp);
1967 if (TREE_NO_WARNING (orig_exp))
1968 TREE_NO_WARNING (exp) = 1;
1970 if (code == VOID_TYPE)
1972 error ("void value not ignored as it ought to be");
1973 return error_mark_node;
1976 exp = require_complete_type (exp);
1977 if (exp == error_mark_node)
1978 return error_mark_node;
1980 promoted_type = targetm.promoted_type (type);
1981 if (promoted_type)
1982 return convert (promoted_type, exp);
1984 if (INTEGRAL_TYPE_P (type))
1985 return perform_integral_promotions (exp);
1987 return exp;
1990 /* Look up COMPONENT in a structure or union TYPE.
1992 If the component name is not found, returns NULL_TREE. Otherwise,
1993 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1994 stepping down the chain to the component, which is in the last
1995 TREE_VALUE of the list. Normally the list is of length one, but if
1996 the component is embedded within (nested) anonymous structures or
1997 unions, the list steps down the chain to the component. */
1999 static tree
2000 lookup_field (tree type, tree component)
2002 tree field;
2004 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2005 to the field elements. Use a binary search on this array to quickly
2006 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2007 will always be set for structures which have many elements. */
2009 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2011 int bot, top, half;
2012 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2014 field = TYPE_FIELDS (type);
2015 bot = 0;
2016 top = TYPE_LANG_SPECIFIC (type)->s->len;
2017 while (top - bot > 1)
2019 half = (top - bot + 1) >> 1;
2020 field = field_array[bot+half];
2022 if (DECL_NAME (field) == NULL_TREE)
2024 /* Step through all anon unions in linear fashion. */
2025 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2027 field = field_array[bot++];
2028 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2029 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2031 tree anon = lookup_field (TREE_TYPE (field), component);
2033 if (anon)
2034 return tree_cons (NULL_TREE, field, anon);
2036 /* The Plan 9 compiler permits referring
2037 directly to an anonymous struct/union field
2038 using a typedef name. */
2039 if (flag_plan9_extensions
2040 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2041 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2042 == TYPE_DECL)
2043 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2044 == component))
2045 break;
2049 /* Entire record is only anon unions. */
2050 if (bot > top)
2051 return NULL_TREE;
2053 /* Restart the binary search, with new lower bound. */
2054 continue;
2057 if (DECL_NAME (field) == component)
2058 break;
2059 if (DECL_NAME (field) < component)
2060 bot += half;
2061 else
2062 top = bot + half;
2065 if (DECL_NAME (field_array[bot]) == component)
2066 field = field_array[bot];
2067 else if (DECL_NAME (field) != component)
2068 return NULL_TREE;
2070 else
2072 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2074 if (DECL_NAME (field) == NULL_TREE
2075 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2076 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2078 tree anon = lookup_field (TREE_TYPE (field), component);
2080 if (anon)
2081 return tree_cons (NULL_TREE, field, anon);
2083 /* The Plan 9 compiler permits referring directly to an
2084 anonymous struct/union field using a typedef
2085 name. */
2086 if (flag_plan9_extensions
2087 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2088 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2089 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2090 == component))
2091 break;
2094 if (DECL_NAME (field) == component)
2095 break;
2098 if (field == NULL_TREE)
2099 return NULL_TREE;
2102 return tree_cons (NULL_TREE, field, NULL_TREE);
2105 /* Make an expression to refer to the COMPONENT field of structure or
2106 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2107 location of the COMPONENT_REF. */
2109 tree
2110 build_component_ref (location_t loc, tree datum, tree component)
2112 tree type = TREE_TYPE (datum);
2113 enum tree_code code = TREE_CODE (type);
2114 tree field = NULL;
2115 tree ref;
2116 bool datum_lvalue = lvalue_p (datum);
2118 if (!objc_is_public (datum, component))
2119 return error_mark_node;
2121 /* Detect Objective-C property syntax object.property. */
2122 if (c_dialect_objc ()
2123 && (ref = objc_maybe_build_component_ref (datum, component)))
2124 return ref;
2126 /* See if there is a field or component with name COMPONENT. */
2128 if (code == RECORD_TYPE || code == UNION_TYPE)
2130 if (!COMPLETE_TYPE_P (type))
2132 c_incomplete_type_error (NULL_TREE, type);
2133 return error_mark_node;
2136 field = lookup_field (type, component);
2138 if (!field)
2140 error_at (loc, "%qT has no member named %qE", type, component);
2141 return error_mark_node;
2144 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2145 This might be better solved in future the way the C++ front
2146 end does it - by giving the anonymous entities each a
2147 separate name and type, and then have build_component_ref
2148 recursively call itself. We can't do that here. */
2151 tree subdatum = TREE_VALUE (field);
2152 int quals;
2153 tree subtype;
2154 bool use_datum_quals;
2156 if (TREE_TYPE (subdatum) == error_mark_node)
2157 return error_mark_node;
2159 /* If this is an rvalue, it does not have qualifiers in C
2160 standard terms and we must avoid propagating such
2161 qualifiers down to a non-lvalue array that is then
2162 converted to a pointer. */
2163 use_datum_quals = (datum_lvalue
2164 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2166 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2167 if (use_datum_quals)
2168 quals |= TYPE_QUALS (TREE_TYPE (datum));
2169 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2171 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2172 NULL_TREE);
2173 SET_EXPR_LOCATION (ref, loc);
2174 if (TREE_READONLY (subdatum)
2175 || (use_datum_quals && TREE_READONLY (datum)))
2176 TREE_READONLY (ref) = 1;
2177 if (TREE_THIS_VOLATILE (subdatum)
2178 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2179 TREE_THIS_VOLATILE (ref) = 1;
2181 if (TREE_DEPRECATED (subdatum))
2182 warn_deprecated_use (subdatum, NULL_TREE);
2184 datum = ref;
2186 field = TREE_CHAIN (field);
2188 while (field);
2190 return ref;
2192 else if (code != ERROR_MARK)
2193 error_at (loc,
2194 "request for member %qE in something not a structure or union",
2195 component);
2197 return error_mark_node;
2200 /* Given an expression PTR for a pointer, return an expression
2201 for the value pointed to.
2202 ERRORSTRING is the name of the operator to appear in error messages.
2204 LOC is the location to use for the generated tree. */
2206 tree
2207 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2209 tree pointer = default_conversion (ptr);
2210 tree type = TREE_TYPE (pointer);
2211 tree ref;
2213 if (TREE_CODE (type) == POINTER_TYPE)
2215 if (CONVERT_EXPR_P (pointer)
2216 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2218 /* If a warning is issued, mark it to avoid duplicates from
2219 the backend. This only needs to be done at
2220 warn_strict_aliasing > 2. */
2221 if (warn_strict_aliasing > 2)
2222 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2223 type, TREE_OPERAND (pointer, 0)))
2224 TREE_NO_WARNING (pointer) = 1;
2227 if (TREE_CODE (pointer) == ADDR_EXPR
2228 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2229 == TREE_TYPE (type)))
2231 ref = TREE_OPERAND (pointer, 0);
2232 protected_set_expr_location (ref, loc);
2233 return ref;
2235 else
2237 tree t = TREE_TYPE (type);
2239 ref = build1 (INDIRECT_REF, t, pointer);
2241 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2243 error_at (loc, "dereferencing pointer to incomplete type");
2244 return error_mark_node;
2246 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2247 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2249 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2250 so that we get the proper error message if the result is used
2251 to assign to. Also, &* is supposed to be a no-op.
2252 And ANSI C seems to specify that the type of the result
2253 should be the const type. */
2254 /* A de-reference of a pointer to const is not a const. It is valid
2255 to change it via some other pointer. */
2256 TREE_READONLY (ref) = TYPE_READONLY (t);
2257 TREE_SIDE_EFFECTS (ref)
2258 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2259 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2260 protected_set_expr_location (ref, loc);
2261 return ref;
2264 else if (TREE_CODE (pointer) != ERROR_MARK)
2265 invalid_indirection_error (loc, type, errstring);
2267 return error_mark_node;
2270 /* This handles expressions of the form "a[i]", which denotes
2271 an array reference.
2273 This is logically equivalent in C to *(a+i), but we may do it differently.
2274 If A is a variable or a member, we generate a primitive ARRAY_REF.
2275 This avoids forcing the array out of registers, and can work on
2276 arrays that are not lvalues (for example, members of structures returned
2277 by functions).
2279 For vector types, allow vector[i] but not i[vector], and create
2280 *(((type*)&vectortype) + i) for the expression.
2282 LOC is the location to use for the returned expression. */
2284 tree
2285 build_array_ref (location_t loc, tree array, tree index)
2287 tree ret;
2288 bool swapped = false;
2289 if (TREE_TYPE (array) == error_mark_node
2290 || TREE_TYPE (index) == error_mark_node)
2291 return error_mark_node;
2293 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2294 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2295 /* Allow vector[index] but not index[vector]. */
2296 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2298 tree temp;
2299 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2300 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2302 error_at (loc,
2303 "subscripted value is neither array nor pointer nor vector");
2305 return error_mark_node;
2307 temp = array;
2308 array = index;
2309 index = temp;
2310 swapped = true;
2313 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2315 error_at (loc, "array subscript is not an integer");
2316 return error_mark_node;
2319 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2321 error_at (loc, "subscripted value is pointer to function");
2322 return error_mark_node;
2325 /* ??? Existing practice has been to warn only when the char
2326 index is syntactically the index, not for char[array]. */
2327 if (!swapped)
2328 warn_array_subscript_with_type_char (index);
2330 /* Apply default promotions *after* noticing character types. */
2331 index = default_conversion (index);
2333 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2335 /* For vector[index], convert the vector to a
2336 pointer of the underlying type. */
2337 if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2339 tree type = TREE_TYPE (array);
2340 tree type1;
2342 if (TREE_CODE (index) == INTEGER_CST)
2343 if (!host_integerp (index, 1)
2344 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2345 >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2346 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2348 c_common_mark_addressable_vec (array);
2349 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2350 type = build_pointer_type (type);
2351 type1 = build_pointer_type (TREE_TYPE (array));
2352 array = build1 (ADDR_EXPR, type1, array);
2353 array = convert (type, array);
2356 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2358 tree rval, type;
2360 /* An array that is indexed by a non-constant
2361 cannot be stored in a register; we must be able to do
2362 address arithmetic on its address.
2363 Likewise an array of elements of variable size. */
2364 if (TREE_CODE (index) != INTEGER_CST
2365 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2366 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2368 if (!c_mark_addressable (array))
2369 return error_mark_node;
2371 /* An array that is indexed by a constant value which is not within
2372 the array bounds cannot be stored in a register either; because we
2373 would get a crash in store_bit_field/extract_bit_field when trying
2374 to access a non-existent part of the register. */
2375 if (TREE_CODE (index) == INTEGER_CST
2376 && TYPE_DOMAIN (TREE_TYPE (array))
2377 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2379 if (!c_mark_addressable (array))
2380 return error_mark_node;
2383 if (pedantic)
2385 tree foo = array;
2386 while (TREE_CODE (foo) == COMPONENT_REF)
2387 foo = TREE_OPERAND (foo, 0);
2388 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2389 pedwarn (loc, OPT_pedantic,
2390 "ISO C forbids subscripting %<register%> array");
2391 else if (!flag_isoc99 && !lvalue_p (foo))
2392 pedwarn (loc, OPT_pedantic,
2393 "ISO C90 forbids subscripting non-lvalue array");
2396 type = TREE_TYPE (TREE_TYPE (array));
2397 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2398 /* Array ref is const/volatile if the array elements are
2399 or if the array is. */
2400 TREE_READONLY (rval)
2401 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2402 | TREE_READONLY (array));
2403 TREE_SIDE_EFFECTS (rval)
2404 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2405 | TREE_SIDE_EFFECTS (array));
2406 TREE_THIS_VOLATILE (rval)
2407 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2408 /* This was added by rms on 16 Nov 91.
2409 It fixes vol struct foo *a; a->elts[1]
2410 in an inline function.
2411 Hope it doesn't break something else. */
2412 | TREE_THIS_VOLATILE (array));
2413 ret = require_complete_type (rval);
2414 protected_set_expr_location (ret, loc);
2415 return ret;
2417 else
2419 tree ar = default_conversion (array);
2421 if (ar == error_mark_node)
2422 return ar;
2424 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2425 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2427 return build_indirect_ref
2428 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2429 RO_ARRAY_INDEXING);
2433 /* Build an external reference to identifier ID. FUN indicates
2434 whether this will be used for a function call. LOC is the source
2435 location of the identifier. This sets *TYPE to the type of the
2436 identifier, which is not the same as the type of the returned value
2437 for CONST_DECLs defined as enum constants. If the type of the
2438 identifier is not available, *TYPE is set to NULL. */
2439 tree
2440 build_external_ref (location_t loc, tree id, int fun, tree *type)
2442 tree ref;
2443 tree decl = lookup_name (id);
2445 /* In Objective-C, an instance variable (ivar) may be preferred to
2446 whatever lookup_name() found. */
2447 decl = objc_lookup_ivar (decl, id);
2449 *type = NULL;
2450 if (decl && decl != error_mark_node)
2452 ref = decl;
2453 *type = TREE_TYPE (ref);
2455 else if (fun)
2456 /* Implicit function declaration. */
2457 ref = implicitly_declare (loc, id);
2458 else if (decl == error_mark_node)
2459 /* Don't complain about something that's already been
2460 complained about. */
2461 return error_mark_node;
2462 else
2464 undeclared_variable (loc, id);
2465 return error_mark_node;
2468 if (TREE_TYPE (ref) == error_mark_node)
2469 return error_mark_node;
2471 if (TREE_DEPRECATED (ref))
2472 warn_deprecated_use (ref, NULL_TREE);
2474 /* Recursive call does not count as usage. */
2475 if (ref != current_function_decl)
2477 TREE_USED (ref) = 1;
2480 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2482 if (!in_sizeof && !in_typeof)
2483 C_DECL_USED (ref) = 1;
2484 else if (DECL_INITIAL (ref) == 0
2485 && DECL_EXTERNAL (ref)
2486 && !TREE_PUBLIC (ref))
2487 record_maybe_used_decl (ref);
2490 if (TREE_CODE (ref) == CONST_DECL)
2492 used_types_insert (TREE_TYPE (ref));
2494 if (warn_cxx_compat
2495 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2496 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2498 warning_at (loc, OPT_Wc___compat,
2499 ("enum constant defined in struct or union "
2500 "is not visible in C++"));
2501 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2504 ref = DECL_INITIAL (ref);
2505 TREE_CONSTANT (ref) = 1;
2507 else if (current_function_decl != 0
2508 && !DECL_FILE_SCOPE_P (current_function_decl)
2509 && (TREE_CODE (ref) == VAR_DECL
2510 || TREE_CODE (ref) == PARM_DECL
2511 || TREE_CODE (ref) == FUNCTION_DECL))
2513 tree context = decl_function_context (ref);
2515 if (context != 0 && context != current_function_decl)
2516 DECL_NONLOCAL (ref) = 1;
2518 /* C99 6.7.4p3: An inline definition of a function with external
2519 linkage ... shall not contain a reference to an identifier with
2520 internal linkage. */
2521 else if (current_function_decl != 0
2522 && DECL_DECLARED_INLINE_P (current_function_decl)
2523 && DECL_EXTERNAL (current_function_decl)
2524 && VAR_OR_FUNCTION_DECL_P (ref)
2525 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2526 && ! TREE_PUBLIC (ref)
2527 && DECL_CONTEXT (ref) != current_function_decl)
2528 record_inline_static (loc, current_function_decl, ref,
2529 csi_internal);
2531 return ref;
2534 /* Record details of decls possibly used inside sizeof or typeof. */
2535 struct maybe_used_decl
2537 /* The decl. */
2538 tree decl;
2539 /* The level seen at (in_sizeof + in_typeof). */
2540 int level;
2541 /* The next one at this level or above, or NULL. */
2542 struct maybe_used_decl *next;
2545 static struct maybe_used_decl *maybe_used_decls;
2547 /* Record that DECL, an undefined static function reference seen
2548 inside sizeof or typeof, might be used if the operand of sizeof is
2549 a VLA type or the operand of typeof is a variably modified
2550 type. */
2552 static void
2553 record_maybe_used_decl (tree decl)
2555 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2556 t->decl = decl;
2557 t->level = in_sizeof + in_typeof;
2558 t->next = maybe_used_decls;
2559 maybe_used_decls = t;
2562 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2563 USED is false, just discard them. If it is true, mark them used
2564 (if no longer inside sizeof or typeof) or move them to the next
2565 level up (if still inside sizeof or typeof). */
2567 void
2568 pop_maybe_used (bool used)
2570 struct maybe_used_decl *p = maybe_used_decls;
2571 int cur_level = in_sizeof + in_typeof;
2572 while (p && p->level > cur_level)
2574 if (used)
2576 if (cur_level == 0)
2577 C_DECL_USED (p->decl) = 1;
2578 else
2579 p->level = cur_level;
2581 p = p->next;
2583 if (!used || cur_level == 0)
2584 maybe_used_decls = p;
2587 /* Return the result of sizeof applied to EXPR. */
2589 struct c_expr
2590 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2592 struct c_expr ret;
2593 if (expr.value == error_mark_node)
2595 ret.value = error_mark_node;
2596 ret.original_code = ERROR_MARK;
2597 ret.original_type = NULL;
2598 pop_maybe_used (false);
2600 else
2602 bool expr_const_operands = true;
2603 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2604 &expr_const_operands);
2605 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2606 ret.original_code = ERROR_MARK;
2607 ret.original_type = NULL;
2608 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2610 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2611 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2612 folded_expr, ret.value);
2613 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2614 SET_EXPR_LOCATION (ret.value, loc);
2616 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2618 return ret;
2621 /* Return the result of sizeof applied to T, a structure for the type
2622 name passed to sizeof (rather than the type itself). LOC is the
2623 location of the original expression. */
2625 struct c_expr
2626 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2628 tree type;
2629 struct c_expr ret;
2630 tree type_expr = NULL_TREE;
2631 bool type_expr_const = true;
2632 type = groktypename (t, &type_expr, &type_expr_const);
2633 ret.value = c_sizeof (loc, type);
2634 ret.original_code = ERROR_MARK;
2635 ret.original_type = NULL;
2636 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2637 && c_vla_type_p (type))
2639 /* If the type is a [*] array, it is a VLA but is represented as
2640 having a size of zero. In such a case we must ensure that
2641 the result of sizeof does not get folded to a constant by
2642 c_fully_fold, because if the size is evaluated the result is
2643 not constant and so constraints on zero or negative size
2644 arrays must not be applied when this sizeof call is inside
2645 another array declarator. */
2646 if (!type_expr)
2647 type_expr = integer_zero_node;
2648 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2649 type_expr, ret.value);
2650 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2652 pop_maybe_used (type != error_mark_node
2653 ? C_TYPE_VARIABLE_SIZE (type) : false);
2654 return ret;
2657 /* Build a function call to function FUNCTION with parameters PARAMS.
2658 The function call is at LOC.
2659 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2660 TREE_VALUE of each node is a parameter-expression.
2661 FUNCTION's data type may be a function type or a pointer-to-function. */
2663 tree
2664 build_function_call (location_t loc, tree function, tree params)
2666 VEC(tree,gc) *vec;
2667 tree ret;
2669 vec = VEC_alloc (tree, gc, list_length (params));
2670 for (; params; params = TREE_CHAIN (params))
2671 VEC_quick_push (tree, vec, TREE_VALUE (params));
2672 ret = build_function_call_vec (loc, function, vec, NULL);
2673 VEC_free (tree, gc, vec);
2674 return ret;
2677 /* Build a function call to function FUNCTION with parameters PARAMS.
2678 ORIGTYPES, if not NULL, is a vector of types; each element is
2679 either NULL or the original type of the corresponding element in
2680 PARAMS. The original type may differ from TREE_TYPE of the
2681 parameter for enums. FUNCTION's data type may be a function type
2682 or pointer-to-function. This function changes the elements of
2683 PARAMS. */
2685 tree
2686 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2687 VEC(tree,gc) *origtypes)
2689 tree fntype, fundecl = 0;
2690 tree name = NULL_TREE, result;
2691 tree tem;
2692 int nargs;
2693 tree *argarray;
2696 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2697 STRIP_TYPE_NOPS (function);
2699 /* Convert anything with function type to a pointer-to-function. */
2700 if (TREE_CODE (function) == FUNCTION_DECL)
2702 /* Implement type-directed function overloading for builtins.
2703 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2704 handle all the type checking. The result is a complete expression
2705 that implements this function call. */
2706 tem = resolve_overloaded_builtin (loc, function, params);
2707 if (tem)
2708 return tem;
2710 name = DECL_NAME (function);
2711 fundecl = function;
2713 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2714 function = function_to_pointer_conversion (loc, function);
2716 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2717 expressions, like those used for ObjC messenger dispatches. */
2718 if (!VEC_empty (tree, params))
2719 function = objc_rewrite_function_call (function,
2720 VEC_index (tree, params, 0));
2722 function = c_fully_fold (function, false, NULL);
2724 fntype = TREE_TYPE (function);
2726 if (TREE_CODE (fntype) == ERROR_MARK)
2727 return error_mark_node;
2729 if (!(TREE_CODE (fntype) == POINTER_TYPE
2730 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2732 error_at (loc, "called object %qE is not a function", function);
2733 return error_mark_node;
2736 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2737 current_function_returns_abnormally = 1;
2739 /* fntype now gets the type of function pointed to. */
2740 fntype = TREE_TYPE (fntype);
2742 /* Convert the parameters to the types declared in the
2743 function prototype, or apply default promotions. */
2745 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2746 function, fundecl);
2747 if (nargs < 0)
2748 return error_mark_node;
2750 /* Check that the function is called through a compatible prototype.
2751 If it is not, replace the call by a trap, wrapped up in a compound
2752 expression if necessary. This has the nice side-effect to prevent
2753 the tree-inliner from generating invalid assignment trees which may
2754 blow up in the RTL expander later. */
2755 if (CONVERT_EXPR_P (function)
2756 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2757 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2758 && !comptypes (fntype, TREE_TYPE (tem)))
2760 tree return_type = TREE_TYPE (fntype);
2761 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2762 NULL_TREE);
2763 int i;
2765 /* This situation leads to run-time undefined behavior. We can't,
2766 therefore, simply error unless we can prove that all possible
2767 executions of the program must execute the code. */
2768 if (warning_at (loc, 0, "function called through a non-compatible type"))
2769 /* We can, however, treat "undefined" any way we please.
2770 Call abort to encourage the user to fix the program. */
2771 inform (loc, "if this code is reached, the program will abort");
2772 /* Before the abort, allow the function arguments to exit or
2773 call longjmp. */
2774 for (i = 0; i < nargs; i++)
2775 trap = build2 (COMPOUND_EXPR, void_type_node,
2776 VEC_index (tree, params, i), trap);
2778 if (VOID_TYPE_P (return_type))
2780 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2781 pedwarn (loc, 0,
2782 "function with qualified void return type called");
2783 return trap;
2785 else
2787 tree rhs;
2789 if (AGGREGATE_TYPE_P (return_type))
2790 rhs = build_compound_literal (loc, return_type,
2791 build_constructor (return_type, 0),
2792 false);
2793 else
2794 rhs = build_zero_cst (return_type);
2796 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2797 trap, rhs));
2801 argarray = VEC_address (tree, params);
2803 /* Check that arguments to builtin functions match the expectations. */
2804 if (fundecl
2805 && DECL_BUILT_IN (fundecl)
2806 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2807 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2808 return error_mark_node;
2810 /* Check that the arguments to the function are valid. */
2811 check_function_arguments (fntype, nargs, argarray);
2813 if (name != NULL_TREE
2814 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2816 if (require_constant_value)
2817 result =
2818 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2819 function, nargs, argarray);
2820 else
2821 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2822 function, nargs, argarray);
2823 if (TREE_CODE (result) == NOP_EXPR
2824 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2825 STRIP_TYPE_NOPS (result);
2827 else
2828 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2829 function, nargs, argarray);
2831 if (VOID_TYPE_P (TREE_TYPE (result)))
2833 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2834 pedwarn (loc, 0,
2835 "function with qualified void return type called");
2836 return result;
2838 return require_complete_type (result);
2841 /* Convert the argument expressions in the vector VALUES
2842 to the types in the list TYPELIST.
2844 If TYPELIST is exhausted, or when an element has NULL as its type,
2845 perform the default conversions.
2847 ORIGTYPES is the original types of the expressions in VALUES. This
2848 holds the type of enum values which have been converted to integral
2849 types. It may be NULL.
2851 FUNCTION is a tree for the called function. It is used only for
2852 error messages, where it is formatted with %qE.
2854 This is also where warnings about wrong number of args are generated.
2856 Returns the actual number of arguments processed (which may be less
2857 than the length of VALUES in some error situations), or -1 on
2858 failure. */
2860 static int
2861 convert_arguments (tree typelist, VEC(tree,gc) *values,
2862 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2864 tree typetail, val;
2865 unsigned int parmnum;
2866 bool error_args = false;
2867 const bool type_generic = fundecl
2868 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2869 bool type_generic_remove_excess_precision = false;
2870 tree selector;
2872 /* Change pointer to function to the function itself for
2873 diagnostics. */
2874 if (TREE_CODE (function) == ADDR_EXPR
2875 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2876 function = TREE_OPERAND (function, 0);
2878 /* Handle an ObjC selector specially for diagnostics. */
2879 selector = objc_message_selector ();
2881 /* For type-generic built-in functions, determine whether excess
2882 precision should be removed (classification) or not
2883 (comparison). */
2884 if (type_generic
2885 && DECL_BUILT_IN (fundecl)
2886 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2888 switch (DECL_FUNCTION_CODE (fundecl))
2890 case BUILT_IN_ISFINITE:
2891 case BUILT_IN_ISINF:
2892 case BUILT_IN_ISINF_SIGN:
2893 case BUILT_IN_ISNAN:
2894 case BUILT_IN_ISNORMAL:
2895 case BUILT_IN_FPCLASSIFY:
2896 type_generic_remove_excess_precision = true;
2897 break;
2899 default:
2900 type_generic_remove_excess_precision = false;
2901 break;
2905 /* Scan the given expressions and types, producing individual
2906 converted arguments. */
2908 for (typetail = typelist, parmnum = 0;
2909 VEC_iterate (tree, values, parmnum, val);
2910 ++parmnum)
2912 tree type = typetail ? TREE_VALUE (typetail) : 0;
2913 tree valtype = TREE_TYPE (val);
2914 tree rname = function;
2915 int argnum = parmnum + 1;
2916 const char *invalid_func_diag;
2917 bool excess_precision = false;
2918 bool npc;
2919 tree parmval;
2921 if (type == void_type_node)
2923 if (selector)
2924 error_at (input_location,
2925 "too many arguments to method %qE", selector);
2926 else
2927 error_at (input_location,
2928 "too many arguments to function %qE", function);
2930 if (fundecl && !DECL_BUILT_IN (fundecl))
2931 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2932 return parmnum;
2935 if (selector && argnum > 2)
2937 rname = selector;
2938 argnum -= 2;
2941 npc = null_pointer_constant_p (val);
2943 /* If there is excess precision and a prototype, convert once to
2944 the required type rather than converting via the semantic
2945 type. Likewise without a prototype a float value represented
2946 as long double should be converted once to double. But for
2947 type-generic classification functions excess precision must
2948 be removed here. */
2949 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2950 && (type || !type_generic || !type_generic_remove_excess_precision))
2952 val = TREE_OPERAND (val, 0);
2953 excess_precision = true;
2955 val = c_fully_fold (val, false, NULL);
2956 STRIP_TYPE_NOPS (val);
2958 val = require_complete_type (val);
2960 if (type != 0)
2962 /* Formal parm type is specified by a function prototype. */
2964 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2966 error ("type of formal parameter %d is incomplete", parmnum + 1);
2967 parmval = val;
2969 else
2971 tree origtype;
2973 /* Optionally warn about conversions that
2974 differ from the default conversions. */
2975 if (warn_traditional_conversion || warn_traditional)
2977 unsigned int formal_prec = TYPE_PRECISION (type);
2979 if (INTEGRAL_TYPE_P (type)
2980 && TREE_CODE (valtype) == REAL_TYPE)
2981 warning (0, "passing argument %d of %qE as integer "
2982 "rather than floating due to prototype",
2983 argnum, rname);
2984 if (INTEGRAL_TYPE_P (type)
2985 && TREE_CODE (valtype) == COMPLEX_TYPE)
2986 warning (0, "passing argument %d of %qE as integer "
2987 "rather than complex due to prototype",
2988 argnum, rname);
2989 else if (TREE_CODE (type) == COMPLEX_TYPE
2990 && TREE_CODE (valtype) == REAL_TYPE)
2991 warning (0, "passing argument %d of %qE as complex "
2992 "rather than floating due to prototype",
2993 argnum, rname);
2994 else if (TREE_CODE (type) == REAL_TYPE
2995 && INTEGRAL_TYPE_P (valtype))
2996 warning (0, "passing argument %d of %qE as floating "
2997 "rather than integer due to prototype",
2998 argnum, rname);
2999 else if (TREE_CODE (type) == COMPLEX_TYPE
3000 && INTEGRAL_TYPE_P (valtype))
3001 warning (0, "passing argument %d of %qE as complex "
3002 "rather than integer due to prototype",
3003 argnum, rname);
3004 else if (TREE_CODE (type) == REAL_TYPE
3005 && TREE_CODE (valtype) == COMPLEX_TYPE)
3006 warning (0, "passing argument %d of %qE as floating "
3007 "rather than complex due to prototype",
3008 argnum, rname);
3009 /* ??? At some point, messages should be written about
3010 conversions between complex types, but that's too messy
3011 to do now. */
3012 else if (TREE_CODE (type) == REAL_TYPE
3013 && TREE_CODE (valtype) == REAL_TYPE)
3015 /* Warn if any argument is passed as `float',
3016 since without a prototype it would be `double'. */
3017 if (formal_prec == TYPE_PRECISION (float_type_node)
3018 && type != dfloat32_type_node)
3019 warning (0, "passing argument %d of %qE as %<float%> "
3020 "rather than %<double%> due to prototype",
3021 argnum, rname);
3023 /* Warn if mismatch between argument and prototype
3024 for decimal float types. Warn of conversions with
3025 binary float types and of precision narrowing due to
3026 prototype. */
3027 else if (type != valtype
3028 && (type == dfloat32_type_node
3029 || type == dfloat64_type_node
3030 || type == dfloat128_type_node
3031 || valtype == dfloat32_type_node
3032 || valtype == dfloat64_type_node
3033 || valtype == dfloat128_type_node)
3034 && (formal_prec
3035 <= TYPE_PRECISION (valtype)
3036 || (type == dfloat128_type_node
3037 && (valtype
3038 != dfloat64_type_node
3039 && (valtype
3040 != dfloat32_type_node)))
3041 || (type == dfloat64_type_node
3042 && (valtype
3043 != dfloat32_type_node))))
3044 warning (0, "passing argument %d of %qE as %qT "
3045 "rather than %qT due to prototype",
3046 argnum, rname, type, valtype);
3049 /* Detect integer changing in width or signedness.
3050 These warnings are only activated with
3051 -Wtraditional-conversion, not with -Wtraditional. */
3052 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3053 && INTEGRAL_TYPE_P (valtype))
3055 tree would_have_been = default_conversion (val);
3056 tree type1 = TREE_TYPE (would_have_been);
3058 if (TREE_CODE (type) == ENUMERAL_TYPE
3059 && (TYPE_MAIN_VARIANT (type)
3060 == TYPE_MAIN_VARIANT (valtype)))
3061 /* No warning if function asks for enum
3062 and the actual arg is that enum type. */
3064 else if (formal_prec != TYPE_PRECISION (type1))
3065 warning (OPT_Wtraditional_conversion,
3066 "passing argument %d of %qE "
3067 "with different width due to prototype",
3068 argnum, rname);
3069 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3071 /* Don't complain if the formal parameter type
3072 is an enum, because we can't tell now whether
3073 the value was an enum--even the same enum. */
3074 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3076 else if (TREE_CODE (val) == INTEGER_CST
3077 && int_fits_type_p (val, type))
3078 /* Change in signedness doesn't matter
3079 if a constant value is unaffected. */
3081 /* If the value is extended from a narrower
3082 unsigned type, it doesn't matter whether we
3083 pass it as signed or unsigned; the value
3084 certainly is the same either way. */
3085 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3086 && TYPE_UNSIGNED (valtype))
3088 else if (TYPE_UNSIGNED (type))
3089 warning (OPT_Wtraditional_conversion,
3090 "passing argument %d of %qE "
3091 "as unsigned due to prototype",
3092 argnum, rname);
3093 else
3094 warning (OPT_Wtraditional_conversion,
3095 "passing argument %d of %qE "
3096 "as signed due to prototype", argnum, rname);
3100 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3101 sake of better warnings from convert_and_check. */
3102 if (excess_precision)
3103 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3104 origtype = (origtypes == NULL
3105 ? NULL_TREE
3106 : VEC_index (tree, origtypes, parmnum));
3107 parmval = convert_for_assignment (input_location, type, val,
3108 origtype, ic_argpass, npc,
3109 fundecl, function,
3110 parmnum + 1);
3112 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3113 && INTEGRAL_TYPE_P (type)
3114 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3115 parmval = default_conversion (parmval);
3118 else if (TREE_CODE (valtype) == REAL_TYPE
3119 && (TYPE_PRECISION (valtype)
3120 < TYPE_PRECISION (double_type_node))
3121 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3123 if (type_generic)
3124 parmval = val;
3125 else
3127 /* Convert `float' to `double'. */
3128 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3129 warning (OPT_Wdouble_promotion,
3130 "implicit conversion from %qT to %qT when passing "
3131 "argument to function",
3132 valtype, double_type_node);
3133 parmval = convert (double_type_node, val);
3136 else if (excess_precision && !type_generic)
3137 /* A "double" argument with excess precision being passed
3138 without a prototype or in variable arguments. */
3139 parmval = convert (valtype, val);
3140 else if ((invalid_func_diag =
3141 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3143 error (invalid_func_diag);
3144 return -1;
3146 else
3147 /* Convert `short' and `char' to full-size `int'. */
3148 parmval = default_conversion (val);
3150 VEC_replace (tree, values, parmnum, parmval);
3151 if (parmval == error_mark_node)
3152 error_args = true;
3154 if (typetail)
3155 typetail = TREE_CHAIN (typetail);
3158 gcc_assert (parmnum == VEC_length (tree, values));
3160 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3162 error_at (input_location,
3163 "too few arguments to function %qE", function);
3164 if (fundecl && !DECL_BUILT_IN (fundecl))
3165 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3166 return -1;
3169 return error_args ? -1 : (int) parmnum;
3172 /* This is the entry point used by the parser to build unary operators
3173 in the input. CODE, a tree_code, specifies the unary operator, and
3174 ARG is the operand. For unary plus, the C parser currently uses
3175 CONVERT_EXPR for code.
3177 LOC is the location to use for the tree generated.
3180 struct c_expr
3181 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3183 struct c_expr result;
3185 result.value = build_unary_op (loc, code, arg.value, 0);
3186 result.original_code = code;
3187 result.original_type = NULL;
3189 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3190 overflow_warning (loc, result.value);
3192 return result;
3195 /* This is the entry point used by the parser to build binary operators
3196 in the input. CODE, a tree_code, specifies the binary operator, and
3197 ARG1 and ARG2 are the operands. In addition to constructing the
3198 expression, we check for operands that were written with other binary
3199 operators in a way that is likely to confuse the user.
3201 LOCATION is the location of the binary operator. */
3203 struct c_expr
3204 parser_build_binary_op (location_t location, enum tree_code code,
3205 struct c_expr arg1, struct c_expr arg2)
3207 struct c_expr result;
3209 enum tree_code code1 = arg1.original_code;
3210 enum tree_code code2 = arg2.original_code;
3211 tree type1 = (arg1.original_type
3212 ? arg1.original_type
3213 : TREE_TYPE (arg1.value));
3214 tree type2 = (arg2.original_type
3215 ? arg2.original_type
3216 : TREE_TYPE (arg2.value));
3218 result.value = build_binary_op (location, code,
3219 arg1.value, arg2.value, 1);
3220 result.original_code = code;
3221 result.original_type = NULL;
3223 if (TREE_CODE (result.value) == ERROR_MARK)
3224 return result;
3226 if (location != UNKNOWN_LOCATION)
3227 protected_set_expr_location (result.value, location);
3229 /* Check for cases such as x+y<<z which users are likely
3230 to misinterpret. */
3231 if (warn_parentheses)
3232 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3234 if (warn_logical_op)
3235 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3236 code1, arg1.value, code2, arg2.value);
3238 /* Warn about comparisons against string literals, with the exception
3239 of testing for equality or inequality of a string literal with NULL. */
3240 if (code == EQ_EXPR || code == NE_EXPR)
3242 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3243 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3244 warning_at (location, OPT_Waddress,
3245 "comparison with string literal results in unspecified behavior");
3247 else if (TREE_CODE_CLASS (code) == tcc_comparison
3248 && (code1 == STRING_CST || code2 == STRING_CST))
3249 warning_at (location, OPT_Waddress,
3250 "comparison with string literal results in unspecified behavior");
3252 if (TREE_OVERFLOW_P (result.value)
3253 && !TREE_OVERFLOW_P (arg1.value)
3254 && !TREE_OVERFLOW_P (arg2.value))
3255 overflow_warning (location, result.value);
3257 /* Warn about comparisons of different enum types. */
3258 if (warn_enum_compare
3259 && TREE_CODE_CLASS (code) == tcc_comparison
3260 && TREE_CODE (type1) == ENUMERAL_TYPE
3261 && TREE_CODE (type2) == ENUMERAL_TYPE
3262 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3263 warning_at (location, OPT_Wenum_compare,
3264 "comparison between %qT and %qT",
3265 type1, type2);
3267 return result;
3270 /* Return a tree for the difference of pointers OP0 and OP1.
3271 The resulting tree has type int. */
3273 static tree
3274 pointer_diff (location_t loc, tree op0, tree op1)
3276 tree restype = ptrdiff_type_node;
3277 tree result, inttype;
3279 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3280 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3281 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3282 tree con0, con1, lit0, lit1;
3283 tree orig_op1 = op1;
3285 /* If the operands point into different address spaces, we need to
3286 explicitly convert them to pointers into the common address space
3287 before we can subtract the numerical address values. */
3288 if (as0 != as1)
3290 addr_space_t as_common;
3291 tree common_type;
3293 /* Determine the common superset address space. This is guaranteed
3294 to exist because the caller verified that comp_target_types
3295 returned non-zero. */
3296 if (!addr_space_superset (as0, as1, &as_common))
3297 gcc_unreachable ();
3299 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3300 op0 = convert (common_type, op0);
3301 op1 = convert (common_type, op1);
3304 /* Determine integer type to perform computations in. This will usually
3305 be the same as the result type (ptrdiff_t), but may need to be a wider
3306 type if pointers for the address space are wider than ptrdiff_t. */
3307 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3308 inttype = lang_hooks.types.type_for_size
3309 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3310 else
3311 inttype = restype;
3314 if (TREE_CODE (target_type) == VOID_TYPE)
3315 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3316 "pointer of type %<void *%> used in subtraction");
3317 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3318 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3319 "pointer to a function used in subtraction");
3321 /* If the conversion to ptrdiff_type does anything like widening or
3322 converting a partial to an integral mode, we get a convert_expression
3323 that is in the way to do any simplifications.
3324 (fold-const.c doesn't know that the extra bits won't be needed.
3325 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3326 different mode in place.)
3327 So first try to find a common term here 'by hand'; we want to cover
3328 at least the cases that occur in legal static initializers. */
3329 if (CONVERT_EXPR_P (op0)
3330 && (TYPE_PRECISION (TREE_TYPE (op0))
3331 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3332 con0 = TREE_OPERAND (op0, 0);
3333 else
3334 con0 = op0;
3335 if (CONVERT_EXPR_P (op1)
3336 && (TYPE_PRECISION (TREE_TYPE (op1))
3337 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3338 con1 = TREE_OPERAND (op1, 0);
3339 else
3340 con1 = op1;
3342 if (TREE_CODE (con0) == PLUS_EXPR)
3344 lit0 = TREE_OPERAND (con0, 1);
3345 con0 = TREE_OPERAND (con0, 0);
3347 else
3348 lit0 = integer_zero_node;
3350 if (TREE_CODE (con1) == PLUS_EXPR)
3352 lit1 = TREE_OPERAND (con1, 1);
3353 con1 = TREE_OPERAND (con1, 0);
3355 else
3356 lit1 = integer_zero_node;
3358 if (operand_equal_p (con0, con1, 0))
3360 op0 = lit0;
3361 op1 = lit1;
3365 /* First do the subtraction as integers;
3366 then drop through to build the divide operator.
3367 Do not do default conversions on the minus operator
3368 in case restype is a short type. */
3370 op0 = build_binary_op (loc,
3371 MINUS_EXPR, convert (inttype, op0),
3372 convert (inttype, op1), 0);
3373 /* This generates an error if op1 is pointer to incomplete type. */
3374 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3375 error_at (loc, "arithmetic on pointer to an incomplete type");
3377 /* This generates an error if op0 is pointer to incomplete type. */
3378 op1 = c_size_in_bytes (target_type);
3380 /* Divide by the size, in easiest possible way. */
3381 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3382 op0, convert (inttype, op1));
3384 /* Convert to final result type if necessary. */
3385 return convert (restype, result);
3388 /* Construct and perhaps optimize a tree representation
3389 for a unary operation. CODE, a tree_code, specifies the operation
3390 and XARG is the operand.
3391 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3392 the default promotions (such as from short to int).
3393 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3394 allows non-lvalues; this is only used to handle conversion of non-lvalue
3395 arrays to pointers in C99.
3397 LOCATION is the location of the operator. */
3399 tree
3400 build_unary_op (location_t location,
3401 enum tree_code code, tree xarg, int flag)
3403 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3404 tree arg = xarg;
3405 tree argtype = 0;
3406 enum tree_code typecode;
3407 tree val;
3408 tree ret = error_mark_node;
3409 tree eptype = NULL_TREE;
3410 int noconvert = flag;
3411 const char *invalid_op_diag;
3412 bool int_operands;
3414 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3415 if (int_operands)
3416 arg = remove_c_maybe_const_expr (arg);
3418 if (code != ADDR_EXPR)
3419 arg = require_complete_type (arg);
3421 typecode = TREE_CODE (TREE_TYPE (arg));
3422 if (typecode == ERROR_MARK)
3423 return error_mark_node;
3424 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3425 typecode = INTEGER_TYPE;
3427 if ((invalid_op_diag
3428 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3430 error_at (location, invalid_op_diag);
3431 return error_mark_node;
3434 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3436 eptype = TREE_TYPE (arg);
3437 arg = TREE_OPERAND (arg, 0);
3440 switch (code)
3442 case CONVERT_EXPR:
3443 /* This is used for unary plus, because a CONVERT_EXPR
3444 is enough to prevent anybody from looking inside for
3445 associativity, but won't generate any code. */
3446 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3447 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3448 || typecode == VECTOR_TYPE))
3450 error_at (location, "wrong type argument to unary plus");
3451 return error_mark_node;
3453 else if (!noconvert)
3454 arg = default_conversion (arg);
3455 arg = non_lvalue_loc (location, arg);
3456 break;
3458 case NEGATE_EXPR:
3459 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3460 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3461 || typecode == VECTOR_TYPE))
3463 error_at (location, "wrong type argument to unary minus");
3464 return error_mark_node;
3466 else if (!noconvert)
3467 arg = default_conversion (arg);
3468 break;
3470 case BIT_NOT_EXPR:
3471 /* ~ works on integer types and non float vectors. */
3472 if (typecode == INTEGER_TYPE
3473 || (typecode == VECTOR_TYPE
3474 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3476 if (!noconvert)
3477 arg = default_conversion (arg);
3479 else if (typecode == COMPLEX_TYPE)
3481 code = CONJ_EXPR;
3482 pedwarn (location, OPT_pedantic,
3483 "ISO C does not support %<~%> for complex conjugation");
3484 if (!noconvert)
3485 arg = default_conversion (arg);
3487 else
3489 error_at (location, "wrong type argument to bit-complement");
3490 return error_mark_node;
3492 break;
3494 case ABS_EXPR:
3495 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3497 error_at (location, "wrong type argument to abs");
3498 return error_mark_node;
3500 else if (!noconvert)
3501 arg = default_conversion (arg);
3502 break;
3504 case CONJ_EXPR:
3505 /* Conjugating a real value is a no-op, but allow it anyway. */
3506 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3507 || typecode == COMPLEX_TYPE))
3509 error_at (location, "wrong type argument to conjugation");
3510 return error_mark_node;
3512 else if (!noconvert)
3513 arg = default_conversion (arg);
3514 break;
3516 case TRUTH_NOT_EXPR:
3517 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3518 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3519 && typecode != COMPLEX_TYPE)
3521 error_at (location,
3522 "wrong type argument to unary exclamation mark");
3523 return error_mark_node;
3525 arg = c_objc_common_truthvalue_conversion (location, arg);
3526 ret = invert_truthvalue_loc (location, arg);
3527 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3528 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3529 location = EXPR_LOCATION (ret);
3530 goto return_build_unary_op;
3532 case REALPART_EXPR:
3533 case IMAGPART_EXPR:
3534 ret = build_real_imag_expr (location, code, arg);
3535 if (ret == error_mark_node)
3536 return error_mark_node;
3537 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3538 eptype = TREE_TYPE (eptype);
3539 goto return_build_unary_op;
3541 case PREINCREMENT_EXPR:
3542 case POSTINCREMENT_EXPR:
3543 case PREDECREMENT_EXPR:
3544 case POSTDECREMENT_EXPR:
3546 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3548 tree inner = build_unary_op (location, code,
3549 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3550 if (inner == error_mark_node)
3551 return error_mark_node;
3552 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3553 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3554 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3555 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3556 goto return_build_unary_op;
3559 /* Complain about anything that is not a true lvalue. In
3560 Objective-C, skip this check for property_refs. */
3561 if (!objc_is_property_ref (arg)
3562 && !lvalue_or_else (location,
3563 arg, ((code == PREINCREMENT_EXPR
3564 || code == POSTINCREMENT_EXPR)
3565 ? lv_increment
3566 : lv_decrement)))
3567 return error_mark_node;
3569 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3571 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3572 warning_at (location, OPT_Wc___compat,
3573 "increment of enumeration value is invalid in C++");
3574 else
3575 warning_at (location, OPT_Wc___compat,
3576 "decrement of enumeration value is invalid in C++");
3579 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3580 arg = c_fully_fold (arg, false, NULL);
3582 /* Increment or decrement the real part of the value,
3583 and don't change the imaginary part. */
3584 if (typecode == COMPLEX_TYPE)
3586 tree real, imag;
3588 pedwarn (location, OPT_pedantic,
3589 "ISO C does not support %<++%> and %<--%> on complex types");
3591 arg = stabilize_reference (arg);
3592 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3593 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3594 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3595 if (real == error_mark_node || imag == error_mark_node)
3596 return error_mark_node;
3597 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3598 real, imag);
3599 goto return_build_unary_op;
3602 /* Report invalid types. */
3604 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3605 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3607 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3608 error_at (location, "wrong type argument to increment");
3609 else
3610 error_at (location, "wrong type argument to decrement");
3612 return error_mark_node;
3616 tree inc;
3618 argtype = TREE_TYPE (arg);
3620 /* Compute the increment. */
3622 if (typecode == POINTER_TYPE)
3624 /* If pointer target is an undefined struct,
3625 we just cannot know how to do the arithmetic. */
3626 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3628 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3629 error_at (location,
3630 "increment of pointer to unknown structure");
3631 else
3632 error_at (location,
3633 "decrement of pointer to unknown structure");
3635 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3636 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3638 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3639 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3640 "wrong type argument to increment");
3641 else
3642 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3643 "wrong type argument to decrement");
3646 inc = c_size_in_bytes (TREE_TYPE (argtype));
3647 inc = fold_convert_loc (location, sizetype, inc);
3649 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3651 /* For signed fract types, we invert ++ to -- or
3652 -- to ++, and change inc from 1 to -1, because
3653 it is not possible to represent 1 in signed fract constants.
3654 For unsigned fract types, the result always overflows and
3655 we get an undefined (original) or the maximum value. */
3656 if (code == PREINCREMENT_EXPR)
3657 code = PREDECREMENT_EXPR;
3658 else if (code == PREDECREMENT_EXPR)
3659 code = PREINCREMENT_EXPR;
3660 else if (code == POSTINCREMENT_EXPR)
3661 code = POSTDECREMENT_EXPR;
3662 else /* code == POSTDECREMENT_EXPR */
3663 code = POSTINCREMENT_EXPR;
3665 inc = integer_minus_one_node;
3666 inc = convert (argtype, inc);
3668 else
3670 inc = integer_one_node;
3671 inc = convert (argtype, inc);
3674 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3675 need to ask Objective-C to build the increment or decrement
3676 expression for it. */
3677 if (objc_is_property_ref (arg))
3678 return objc_build_incr_expr_for_property_ref (location, code,
3679 arg, inc);
3681 /* Report a read-only lvalue. */
3682 if (TYPE_READONLY (argtype))
3684 readonly_error (arg,
3685 ((code == PREINCREMENT_EXPR
3686 || code == POSTINCREMENT_EXPR)
3687 ? lv_increment : lv_decrement));
3688 return error_mark_node;
3690 else if (TREE_READONLY (arg))
3691 readonly_warning (arg,
3692 ((code == PREINCREMENT_EXPR
3693 || code == POSTINCREMENT_EXPR)
3694 ? lv_increment : lv_decrement));
3696 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3697 val = boolean_increment (code, arg);
3698 else
3699 val = build2 (code, TREE_TYPE (arg), arg, inc);
3700 TREE_SIDE_EFFECTS (val) = 1;
3701 if (TREE_CODE (val) != code)
3702 TREE_NO_WARNING (val) = 1;
3703 ret = val;
3704 goto return_build_unary_op;
3707 case ADDR_EXPR:
3708 /* Note that this operation never does default_conversion. */
3710 /* The operand of unary '&' must be an lvalue (which excludes
3711 expressions of type void), or, in C99, the result of a [] or
3712 unary '*' operator. */
3713 if (VOID_TYPE_P (TREE_TYPE (arg))
3714 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3715 && (TREE_CODE (arg) != INDIRECT_REF
3716 || !flag_isoc99))
3717 pedwarn (location, 0, "taking address of expression of type %<void%>");
3719 /* Let &* cancel out to simplify resulting code. */
3720 if (TREE_CODE (arg) == INDIRECT_REF)
3722 /* Don't let this be an lvalue. */
3723 if (lvalue_p (TREE_OPERAND (arg, 0)))
3724 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3725 ret = TREE_OPERAND (arg, 0);
3726 goto return_build_unary_op;
3729 /* For &x[y], return x+y */
3730 if (TREE_CODE (arg) == ARRAY_REF)
3732 tree op0 = TREE_OPERAND (arg, 0);
3733 if (!c_mark_addressable (op0))
3734 return error_mark_node;
3737 /* Anything not already handled and not a true memory reference
3738 or a non-lvalue array is an error. */
3739 else if (typecode != FUNCTION_TYPE && !flag
3740 && !lvalue_or_else (location, arg, lv_addressof))
3741 return error_mark_node;
3743 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3744 folding later. */
3745 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3747 tree inner = build_unary_op (location, code,
3748 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3749 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3750 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3751 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3752 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3753 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3754 goto return_build_unary_op;
3757 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3758 argtype = TREE_TYPE (arg);
3760 /* If the lvalue is const or volatile, merge that into the type
3761 to which the address will point. This is only needed
3762 for function types. */
3763 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3764 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3765 && TREE_CODE (argtype) == FUNCTION_TYPE)
3767 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3768 int quals = orig_quals;
3770 if (TREE_READONLY (arg))
3771 quals |= TYPE_QUAL_CONST;
3772 if (TREE_THIS_VOLATILE (arg))
3773 quals |= TYPE_QUAL_VOLATILE;
3775 argtype = c_build_qualified_type (argtype, quals);
3778 if (!c_mark_addressable (arg))
3779 return error_mark_node;
3781 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3782 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3784 argtype = build_pointer_type (argtype);
3786 /* ??? Cope with user tricks that amount to offsetof. Delete this
3787 when we have proper support for integer constant expressions. */
3788 val = get_base_address (arg);
3789 if (val && TREE_CODE (val) == INDIRECT_REF
3790 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3792 tree op0 = fold_convert_loc (location, sizetype,
3793 fold_offsetof (arg, val)), op1;
3795 op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3796 ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0);
3797 goto return_build_unary_op;
3800 val = build1 (ADDR_EXPR, argtype, arg);
3802 ret = val;
3803 goto return_build_unary_op;
3805 default:
3806 gcc_unreachable ();
3809 if (argtype == 0)
3810 argtype = TREE_TYPE (arg);
3811 if (TREE_CODE (arg) == INTEGER_CST)
3812 ret = (require_constant_value
3813 ? fold_build1_initializer_loc (location, code, argtype, arg)
3814 : fold_build1_loc (location, code, argtype, arg));
3815 else
3816 ret = build1 (code, argtype, arg);
3817 return_build_unary_op:
3818 gcc_assert (ret != error_mark_node);
3819 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3820 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3821 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3822 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3823 ret = note_integer_operands (ret);
3824 if (eptype)
3825 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3826 protected_set_expr_location (ret, location);
3827 return ret;
3830 /* Return nonzero if REF is an lvalue valid for this language.
3831 Lvalues can be assigned, unless their type has TYPE_READONLY.
3832 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3834 bool
3835 lvalue_p (const_tree ref)
3837 const enum tree_code code = TREE_CODE (ref);
3839 switch (code)
3841 case REALPART_EXPR:
3842 case IMAGPART_EXPR:
3843 case COMPONENT_REF:
3844 return lvalue_p (TREE_OPERAND (ref, 0));
3846 case C_MAYBE_CONST_EXPR:
3847 return lvalue_p (TREE_OPERAND (ref, 1));
3849 case COMPOUND_LITERAL_EXPR:
3850 case STRING_CST:
3851 return 1;
3853 case INDIRECT_REF:
3854 case ARRAY_REF:
3855 case VAR_DECL:
3856 case PARM_DECL:
3857 case RESULT_DECL:
3858 case ERROR_MARK:
3859 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3860 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3862 case BIND_EXPR:
3863 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3865 default:
3866 return 0;
3870 /* Give a warning for storing in something that is read-only in GCC
3871 terms but not const in ISO C terms. */
3873 static void
3874 readonly_warning (tree arg, enum lvalue_use use)
3876 switch (use)
3878 case lv_assign:
3879 warning (0, "assignment of read-only location %qE", arg);
3880 break;
3881 case lv_increment:
3882 warning (0, "increment of read-only location %qE", arg);
3883 break;
3884 case lv_decrement:
3885 warning (0, "decrement of read-only location %qE", arg);
3886 break;
3887 default:
3888 gcc_unreachable ();
3890 return;
3894 /* Return nonzero if REF is an lvalue valid for this language;
3895 otherwise, print an error message and return zero. USE says
3896 how the lvalue is being used and so selects the error message.
3897 LOCATION is the location at which any error should be reported. */
3899 static int
3900 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
3902 int win = lvalue_p (ref);
3904 if (!win)
3905 lvalue_error (loc, use);
3907 return win;
3910 /* Mark EXP saying that we need to be able to take the
3911 address of it; it should not be allocated in a register.
3912 Returns true if successful. */
3914 bool
3915 c_mark_addressable (tree exp)
3917 tree x = exp;
3919 while (1)
3920 switch (TREE_CODE (x))
3922 case COMPONENT_REF:
3923 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3925 error
3926 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3927 return false;
3930 /* ... fall through ... */
3932 case ADDR_EXPR:
3933 case ARRAY_REF:
3934 case REALPART_EXPR:
3935 case IMAGPART_EXPR:
3936 x = TREE_OPERAND (x, 0);
3937 break;
3939 case COMPOUND_LITERAL_EXPR:
3940 case CONSTRUCTOR:
3941 TREE_ADDRESSABLE (x) = 1;
3942 return true;
3944 case VAR_DECL:
3945 case CONST_DECL:
3946 case PARM_DECL:
3947 case RESULT_DECL:
3948 if (C_DECL_REGISTER (x)
3949 && DECL_NONLOCAL (x))
3951 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3953 error
3954 ("global register variable %qD used in nested function", x);
3955 return false;
3957 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3959 else if (C_DECL_REGISTER (x))
3961 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3962 error ("address of global register variable %qD requested", x);
3963 else
3964 error ("address of register variable %qD requested", x);
3965 return false;
3968 /* drops in */
3969 case FUNCTION_DECL:
3970 TREE_ADDRESSABLE (x) = 1;
3971 /* drops out */
3972 default:
3973 return true;
3977 /* Convert EXPR to TYPE, warning about conversion problems with
3978 constants. SEMANTIC_TYPE is the type this conversion would use
3979 without excess precision. If SEMANTIC_TYPE is NULL, this function
3980 is equivalent to convert_and_check. This function is a wrapper that
3981 handles conversions that may be different than
3982 the usual ones because of excess precision. */
3984 static tree
3985 ep_convert_and_check (tree type, tree expr, tree semantic_type)
3987 if (TREE_TYPE (expr) == type)
3988 return expr;
3990 if (!semantic_type)
3991 return convert_and_check (type, expr);
3993 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3994 && TREE_TYPE (expr) != semantic_type)
3996 /* For integers, we need to check the real conversion, not
3997 the conversion to the excess precision type. */
3998 expr = convert_and_check (semantic_type, expr);
4000 /* Result type is the excess precision type, which should be
4001 large enough, so do not check. */
4002 return convert (type, expr);
4005 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4006 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4007 if folded to an integer constant then the unselected half may
4008 contain arbitrary operations not normally permitted in constant
4009 expressions. Set the location of the expression to LOC. */
4011 tree
4012 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4013 tree op1, tree op1_original_type, tree op2,
4014 tree op2_original_type)
4016 tree type1;
4017 tree type2;
4018 enum tree_code code1;
4019 enum tree_code code2;
4020 tree result_type = NULL;
4021 tree semantic_result_type = NULL;
4022 tree orig_op1 = op1, orig_op2 = op2;
4023 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4024 bool ifexp_int_operands;
4025 tree ret;
4027 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4028 if (op1_int_operands)
4029 op1 = remove_c_maybe_const_expr (op1);
4030 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4031 if (op2_int_operands)
4032 op2 = remove_c_maybe_const_expr (op2);
4033 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4034 if (ifexp_int_operands)
4035 ifexp = remove_c_maybe_const_expr (ifexp);
4037 /* Promote both alternatives. */
4039 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4040 op1 = default_conversion (op1);
4041 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4042 op2 = default_conversion (op2);
4044 if (TREE_CODE (ifexp) == ERROR_MARK
4045 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4046 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4047 return error_mark_node;
4049 type1 = TREE_TYPE (op1);
4050 code1 = TREE_CODE (type1);
4051 type2 = TREE_TYPE (op2);
4052 code2 = TREE_CODE (type2);
4054 /* C90 does not permit non-lvalue arrays in conditional expressions.
4055 In C99 they will be pointers by now. */
4056 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4058 error_at (colon_loc, "non-lvalue array in conditional expression");
4059 return error_mark_node;
4062 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4063 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4064 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4065 || code1 == COMPLEX_TYPE)
4066 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4067 || code2 == COMPLEX_TYPE))
4069 semantic_result_type = c_common_type (type1, type2);
4070 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4072 op1 = TREE_OPERAND (op1, 0);
4073 type1 = TREE_TYPE (op1);
4074 gcc_assert (TREE_CODE (type1) == code1);
4076 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4078 op2 = TREE_OPERAND (op2, 0);
4079 type2 = TREE_TYPE (op2);
4080 gcc_assert (TREE_CODE (type2) == code2);
4084 if (warn_cxx_compat)
4086 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4087 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4089 if (TREE_CODE (t1) == ENUMERAL_TYPE
4090 && TREE_CODE (t2) == ENUMERAL_TYPE
4091 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4092 warning_at (colon_loc, OPT_Wc___compat,
4093 ("different enum types in conditional is "
4094 "invalid in C++: %qT vs %qT"),
4095 t1, t2);
4098 /* Quickly detect the usual case where op1 and op2 have the same type
4099 after promotion. */
4100 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4102 if (type1 == type2)
4103 result_type = type1;
4104 else
4105 result_type = TYPE_MAIN_VARIANT (type1);
4107 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4108 || code1 == COMPLEX_TYPE)
4109 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4110 || code2 == COMPLEX_TYPE))
4112 result_type = c_common_type (type1, type2);
4113 do_warn_double_promotion (result_type, type1, type2,
4114 "implicit conversion from %qT to %qT to "
4115 "match other result of conditional",
4116 colon_loc);
4118 /* If -Wsign-compare, warn here if type1 and type2 have
4119 different signedness. We'll promote the signed to unsigned
4120 and later code won't know it used to be different.
4121 Do this check on the original types, so that explicit casts
4122 will be considered, but default promotions won't. */
4123 if (c_inhibit_evaluation_warnings == 0)
4125 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4126 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4128 if (unsigned_op1 ^ unsigned_op2)
4130 bool ovf;
4132 /* Do not warn if the result type is signed, since the
4133 signed type will only be chosen if it can represent
4134 all the values of the unsigned type. */
4135 if (!TYPE_UNSIGNED (result_type))
4136 /* OK */;
4137 else
4139 bool op1_maybe_const = true;
4140 bool op2_maybe_const = true;
4142 /* Do not warn if the signed quantity is an
4143 unsuffixed integer literal (or some static
4144 constant expression involving such literals) and
4145 it is non-negative. This warning requires the
4146 operands to be folded for best results, so do
4147 that folding in this case even without
4148 warn_sign_compare to avoid warning options
4149 possibly affecting code generation. */
4150 c_inhibit_evaluation_warnings
4151 += (ifexp == truthvalue_false_node);
4152 op1 = c_fully_fold (op1, require_constant_value,
4153 &op1_maybe_const);
4154 c_inhibit_evaluation_warnings
4155 -= (ifexp == truthvalue_false_node);
4157 c_inhibit_evaluation_warnings
4158 += (ifexp == truthvalue_true_node);
4159 op2 = c_fully_fold (op2, require_constant_value,
4160 &op2_maybe_const);
4161 c_inhibit_evaluation_warnings
4162 -= (ifexp == truthvalue_true_node);
4164 if (warn_sign_compare)
4166 if ((unsigned_op2
4167 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4168 || (unsigned_op1
4169 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4170 /* OK */;
4171 else
4172 warning_at (colon_loc, OPT_Wsign_compare,
4173 ("signed and unsigned type in "
4174 "conditional expression"));
4176 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4177 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4178 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4179 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4184 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4186 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4187 pedwarn (colon_loc, OPT_pedantic,
4188 "ISO C forbids conditional expr with only one void side");
4189 result_type = void_type_node;
4191 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4193 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4194 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4195 addr_space_t as_common;
4197 if (comp_target_types (colon_loc, type1, type2))
4198 result_type = common_pointer_type (type1, type2);
4199 else if (null_pointer_constant_p (orig_op1))
4200 result_type = type2;
4201 else if (null_pointer_constant_p (orig_op2))
4202 result_type = type1;
4203 else if (!addr_space_superset (as1, as2, &as_common))
4205 error_at (colon_loc, "pointers to disjoint address spaces "
4206 "used in conditional expression");
4207 return error_mark_node;
4209 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4211 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4212 pedwarn (colon_loc, OPT_pedantic,
4213 "ISO C forbids conditional expr between "
4214 "%<void *%> and function pointer");
4215 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4216 TREE_TYPE (type2)));
4218 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4220 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4221 pedwarn (colon_loc, OPT_pedantic,
4222 "ISO C forbids conditional expr between "
4223 "%<void *%> and function pointer");
4224 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4225 TREE_TYPE (type1)));
4227 /* Objective-C pointer comparisons are a bit more lenient. */
4228 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4229 result_type = objc_common_type (type1, type2);
4230 else
4232 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4234 pedwarn (colon_loc, 0,
4235 "pointer type mismatch in conditional expression");
4236 result_type = build_pointer_type
4237 (build_qualified_type (void_type_node, qual));
4240 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4242 if (!null_pointer_constant_p (orig_op2))
4243 pedwarn (colon_loc, 0,
4244 "pointer/integer type mismatch in conditional expression");
4245 else
4247 op2 = null_pointer_node;
4249 result_type = type1;
4251 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4253 if (!null_pointer_constant_p (orig_op1))
4254 pedwarn (colon_loc, 0,
4255 "pointer/integer type mismatch in conditional expression");
4256 else
4258 op1 = null_pointer_node;
4260 result_type = type2;
4263 if (!result_type)
4265 if (flag_cond_mismatch)
4266 result_type = void_type_node;
4267 else
4269 error_at (colon_loc, "type mismatch in conditional expression");
4270 return error_mark_node;
4274 /* Merge const and volatile flags of the incoming types. */
4275 result_type
4276 = build_type_variant (result_type,
4277 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4278 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4280 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4281 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4283 if (ifexp_bcp && ifexp == truthvalue_true_node)
4285 op2_int_operands = true;
4286 op1 = c_fully_fold (op1, require_constant_value, NULL);
4288 if (ifexp_bcp && ifexp == truthvalue_false_node)
4290 op1_int_operands = true;
4291 op2 = c_fully_fold (op2, require_constant_value, NULL);
4293 int_const = int_operands = (ifexp_int_operands
4294 && op1_int_operands
4295 && op2_int_operands);
4296 if (int_operands)
4298 int_const = ((ifexp == truthvalue_true_node
4299 && TREE_CODE (orig_op1) == INTEGER_CST
4300 && !TREE_OVERFLOW (orig_op1))
4301 || (ifexp == truthvalue_false_node
4302 && TREE_CODE (orig_op2) == INTEGER_CST
4303 && !TREE_OVERFLOW (orig_op2)));
4305 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4306 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4307 else
4309 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4310 if (int_operands)
4311 ret = note_integer_operands (ret);
4313 if (semantic_result_type)
4314 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4316 protected_set_expr_location (ret, colon_loc);
4317 return ret;
4320 /* Return a compound expression that performs two expressions and
4321 returns the value of the second of them.
4323 LOC is the location of the COMPOUND_EXPR. */
4325 tree
4326 build_compound_expr (location_t loc, tree expr1, tree expr2)
4328 bool expr1_int_operands, expr2_int_operands;
4329 tree eptype = NULL_TREE;
4330 tree ret;
4332 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4333 if (expr1_int_operands)
4334 expr1 = remove_c_maybe_const_expr (expr1);
4335 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4336 if (expr2_int_operands)
4337 expr2 = remove_c_maybe_const_expr (expr2);
4339 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4340 expr1 = TREE_OPERAND (expr1, 0);
4341 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4343 eptype = TREE_TYPE (expr2);
4344 expr2 = TREE_OPERAND (expr2, 0);
4347 if (!TREE_SIDE_EFFECTS (expr1))
4349 /* The left-hand operand of a comma expression is like an expression
4350 statement: with -Wunused, we should warn if it doesn't have
4351 any side-effects, unless it was explicitly cast to (void). */
4352 if (warn_unused_value)
4354 if (VOID_TYPE_P (TREE_TYPE (expr1))
4355 && CONVERT_EXPR_P (expr1))
4356 ; /* (void) a, b */
4357 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4358 && TREE_CODE (expr1) == COMPOUND_EXPR
4359 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4360 ; /* (void) a, (void) b, c */
4361 else
4362 warning_at (loc, OPT_Wunused_value,
4363 "left-hand operand of comma expression has no effect");
4367 /* With -Wunused, we should also warn if the left-hand operand does have
4368 side-effects, but computes a value which is not used. For example, in
4369 `foo() + bar(), baz()' the result of the `+' operator is not used,
4370 so we should issue a warning. */
4371 else if (warn_unused_value)
4372 warn_if_unused_value (expr1, loc);
4374 if (expr2 == error_mark_node)
4375 return error_mark_node;
4377 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4379 if (flag_isoc99
4380 && expr1_int_operands
4381 && expr2_int_operands)
4382 ret = note_integer_operands (ret);
4384 if (eptype)
4385 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4387 protected_set_expr_location (ret, loc);
4388 return ret;
4391 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4392 which we are casting. OTYPE is the type of the expression being
4393 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4394 of the cast. -Wcast-qual appeared on the command line. Named
4395 address space qualifiers are not handled here, because they result
4396 in different warnings. */
4398 static void
4399 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4401 tree in_type = type;
4402 tree in_otype = otype;
4403 int added = 0;
4404 int discarded = 0;
4405 bool is_const;
4407 /* Check that the qualifiers on IN_TYPE are a superset of the
4408 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4409 nodes is uninteresting and we stop as soon as we hit a
4410 non-POINTER_TYPE node on either type. */
4413 in_otype = TREE_TYPE (in_otype);
4414 in_type = TREE_TYPE (in_type);
4416 /* GNU C allows cv-qualified function types. 'const' means the
4417 function is very pure, 'volatile' means it can't return. We
4418 need to warn when such qualifiers are added, not when they're
4419 taken away. */
4420 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4421 && TREE_CODE (in_type) == FUNCTION_TYPE)
4422 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4423 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4424 else
4425 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4426 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4428 while (TREE_CODE (in_type) == POINTER_TYPE
4429 && TREE_CODE (in_otype) == POINTER_TYPE);
4431 if (added)
4432 warning_at (loc, OPT_Wcast_qual,
4433 "cast adds %q#v qualifier to function type", added);
4435 if (discarded)
4436 /* There are qualifiers present in IN_OTYPE that are not present
4437 in IN_TYPE. */
4438 warning_at (loc, OPT_Wcast_qual,
4439 "cast discards %q#v qualifier from pointer target type",
4440 discarded);
4442 if (added || discarded)
4443 return;
4445 /* A cast from **T to const **T is unsafe, because it can cause a
4446 const value to be changed with no additional warning. We only
4447 issue this warning if T is the same on both sides, and we only
4448 issue the warning if there are the same number of pointers on
4449 both sides, as otherwise the cast is clearly unsafe anyhow. A
4450 cast is unsafe when a qualifier is added at one level and const
4451 is not present at all outer levels.
4453 To issue this warning, we check at each level whether the cast
4454 adds new qualifiers not already seen. We don't need to special
4455 case function types, as they won't have the same
4456 TYPE_MAIN_VARIANT. */
4458 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4459 return;
4460 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4461 return;
4463 in_type = type;
4464 in_otype = otype;
4465 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4468 in_type = TREE_TYPE (in_type);
4469 in_otype = TREE_TYPE (in_otype);
4470 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4471 && !is_const)
4473 warning_at (loc, OPT_Wcast_qual,
4474 "to be safe all intermediate pointers in cast from "
4475 "%qT to %qT must be %<const%> qualified",
4476 otype, type);
4477 break;
4479 if (is_const)
4480 is_const = TYPE_READONLY (in_type);
4482 while (TREE_CODE (in_type) == POINTER_TYPE);
4485 /* Build an expression representing a cast to type TYPE of expression EXPR.
4486 LOC is the location of the cast-- typically the open paren of the cast. */
4488 tree
4489 build_c_cast (location_t loc, tree type, tree expr)
4491 tree value;
4493 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4494 expr = TREE_OPERAND (expr, 0);
4496 value = expr;
4498 if (type == error_mark_node || expr == error_mark_node)
4499 return error_mark_node;
4501 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4502 only in <protocol> qualifications. But when constructing cast expressions,
4503 the protocols do matter and must be kept around. */
4504 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4505 return build1 (NOP_EXPR, type, expr);
4507 type = TYPE_MAIN_VARIANT (type);
4509 if (TREE_CODE (type) == ARRAY_TYPE)
4511 error_at (loc, "cast specifies array type");
4512 return error_mark_node;
4515 if (TREE_CODE (type) == FUNCTION_TYPE)
4517 error_at (loc, "cast specifies function type");
4518 return error_mark_node;
4521 if (!VOID_TYPE_P (type))
4523 value = require_complete_type (value);
4524 if (value == error_mark_node)
4525 return error_mark_node;
4528 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4530 if (TREE_CODE (type) == RECORD_TYPE
4531 || TREE_CODE (type) == UNION_TYPE)
4532 pedwarn (loc, OPT_pedantic,
4533 "ISO C forbids casting nonscalar to the same type");
4535 else if (TREE_CODE (type) == UNION_TYPE)
4537 tree field;
4539 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4540 if (TREE_TYPE (field) != error_mark_node
4541 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4542 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4543 break;
4545 if (field)
4547 tree t;
4548 bool maybe_const = true;
4550 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4551 t = c_fully_fold (value, false, &maybe_const);
4552 t = build_constructor_single (type, field, t);
4553 if (!maybe_const)
4554 t = c_wrap_maybe_const (t, true);
4555 t = digest_init (loc, type, t,
4556 NULL_TREE, false, true, 0);
4557 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4558 return t;
4560 error_at (loc, "cast to union type from type not present in union");
4561 return error_mark_node;
4563 else
4565 tree otype, ovalue;
4567 if (type == void_type_node)
4569 tree t = build1 (CONVERT_EXPR, type, value);
4570 SET_EXPR_LOCATION (t, loc);
4571 return t;
4574 otype = TREE_TYPE (value);
4576 /* Optionally warn about potentially worrisome casts. */
4577 if (warn_cast_qual
4578 && TREE_CODE (type) == POINTER_TYPE
4579 && TREE_CODE (otype) == POINTER_TYPE)
4580 handle_warn_cast_qual (loc, type, otype);
4582 /* Warn about conversions between pointers to disjoint
4583 address spaces. */
4584 if (TREE_CODE (type) == POINTER_TYPE
4585 && TREE_CODE (otype) == POINTER_TYPE
4586 && !null_pointer_constant_p (value))
4588 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4589 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4590 addr_space_t as_common;
4592 if (!addr_space_superset (as_to, as_from, &as_common))
4594 if (ADDR_SPACE_GENERIC_P (as_from))
4595 warning_at (loc, 0, "cast to %s address space pointer "
4596 "from disjoint generic address space pointer",
4597 c_addr_space_name (as_to));
4599 else if (ADDR_SPACE_GENERIC_P (as_to))
4600 warning_at (loc, 0, "cast to generic address space pointer "
4601 "from disjoint %s address space pointer",
4602 c_addr_space_name (as_from));
4604 else
4605 warning_at (loc, 0, "cast to %s address space pointer "
4606 "from disjoint %s address space pointer",
4607 c_addr_space_name (as_to),
4608 c_addr_space_name (as_from));
4612 /* Warn about possible alignment problems. */
4613 if (STRICT_ALIGNMENT
4614 && TREE_CODE (type) == POINTER_TYPE
4615 && TREE_CODE (otype) == POINTER_TYPE
4616 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4617 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4618 /* Don't warn about opaque types, where the actual alignment
4619 restriction is unknown. */
4620 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4621 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4622 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4623 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4624 warning_at (loc, OPT_Wcast_align,
4625 "cast increases required alignment of target type");
4627 if (TREE_CODE (type) == INTEGER_TYPE
4628 && TREE_CODE (otype) == POINTER_TYPE
4629 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4630 /* Unlike conversion of integers to pointers, where the
4631 warning is disabled for converting constants because
4632 of cases such as SIG_*, warn about converting constant
4633 pointers to integers. In some cases it may cause unwanted
4634 sign extension, and a warning is appropriate. */
4635 warning_at (loc, OPT_Wpointer_to_int_cast,
4636 "cast from pointer to integer of different size");
4638 if (TREE_CODE (value) == CALL_EXPR
4639 && TREE_CODE (type) != TREE_CODE (otype))
4640 warning_at (loc, OPT_Wbad_function_cast,
4641 "cast from function call of type %qT "
4642 "to non-matching type %qT", otype, type);
4644 if (TREE_CODE (type) == POINTER_TYPE
4645 && TREE_CODE (otype) == INTEGER_TYPE
4646 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4647 /* Don't warn about converting any constant. */
4648 && !TREE_CONSTANT (value))
4649 warning_at (loc,
4650 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4651 "of different size");
4653 if (warn_strict_aliasing <= 2)
4654 strict_aliasing_warning (otype, type, expr);
4656 /* If pedantic, warn for conversions between function and object
4657 pointer types, except for converting a null pointer constant
4658 to function pointer type. */
4659 if (pedantic
4660 && TREE_CODE (type) == POINTER_TYPE
4661 && TREE_CODE (otype) == POINTER_TYPE
4662 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4663 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4664 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4665 "conversion of function pointer to object pointer type");
4667 if (pedantic
4668 && TREE_CODE (type) == POINTER_TYPE
4669 && TREE_CODE (otype) == POINTER_TYPE
4670 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4671 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4672 && !null_pointer_constant_p (value))
4673 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4674 "conversion of object pointer to function pointer type");
4676 ovalue = value;
4677 value = convert (type, value);
4679 /* Ignore any integer overflow caused by the cast. */
4680 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4682 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4684 if (!TREE_OVERFLOW (value))
4686 /* Avoid clobbering a shared constant. */
4687 value = copy_node (value);
4688 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4691 else if (TREE_OVERFLOW (value))
4692 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4693 value = build_int_cst_wide (TREE_TYPE (value),
4694 TREE_INT_CST_LOW (value),
4695 TREE_INT_CST_HIGH (value));
4699 /* Don't let a cast be an lvalue. */
4700 if (value == expr)
4701 value = non_lvalue_loc (loc, value);
4703 /* Don't allow the results of casting to floating-point or complex
4704 types be confused with actual constants, or casts involving
4705 integer and pointer types other than direct integer-to-integer
4706 and integer-to-pointer be confused with integer constant
4707 expressions and null pointer constants. */
4708 if (TREE_CODE (value) == REAL_CST
4709 || TREE_CODE (value) == COMPLEX_CST
4710 || (TREE_CODE (value) == INTEGER_CST
4711 && !((TREE_CODE (expr) == INTEGER_CST
4712 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4713 || TREE_CODE (expr) == REAL_CST
4714 || TREE_CODE (expr) == COMPLEX_CST)))
4715 value = build1 (NOP_EXPR, type, value);
4717 if (CAN_HAVE_LOCATION_P (value))
4718 SET_EXPR_LOCATION (value, loc);
4719 return value;
4722 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4723 location of the open paren of the cast, or the position of the cast
4724 expr. */
4725 tree
4726 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4728 tree type;
4729 tree type_expr = NULL_TREE;
4730 bool type_expr_const = true;
4731 tree ret;
4732 int saved_wsp = warn_strict_prototypes;
4734 /* This avoids warnings about unprototyped casts on
4735 integers. E.g. "#define SIG_DFL (void(*)())0". */
4736 if (TREE_CODE (expr) == INTEGER_CST)
4737 warn_strict_prototypes = 0;
4738 type = groktypename (type_name, &type_expr, &type_expr_const);
4739 warn_strict_prototypes = saved_wsp;
4741 ret = build_c_cast (loc, type, expr);
4742 if (type_expr)
4744 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4745 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4746 SET_EXPR_LOCATION (ret, loc);
4749 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4750 SET_EXPR_LOCATION (ret, loc);
4752 /* C++ does not permits types to be defined in a cast, but it
4753 allows references to incomplete types. */
4754 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4755 warning_at (loc, OPT_Wc___compat,
4756 "defining a type in a cast is invalid in C++");
4758 return ret;
4761 /* Build an assignment expression of lvalue LHS from value RHS.
4762 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4763 may differ from TREE_TYPE (LHS) for an enum bitfield.
4764 MODIFYCODE is the code for a binary operator that we use
4765 to combine the old value of LHS with RHS to get the new value.
4766 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4767 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4768 which may differ from TREE_TYPE (RHS) for an enum value.
4770 LOCATION is the location of the MODIFYCODE operator.
4771 RHS_LOC is the location of the RHS. */
4773 tree
4774 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4775 enum tree_code modifycode,
4776 location_t rhs_loc, tree rhs, tree rhs_origtype)
4778 tree result;
4779 tree newrhs;
4780 tree rhs_semantic_type = NULL_TREE;
4781 tree lhstype = TREE_TYPE (lhs);
4782 tree olhstype = lhstype;
4783 bool npc;
4785 /* Types that aren't fully specified cannot be used in assignments. */
4786 lhs = require_complete_type (lhs);
4788 /* Avoid duplicate error messages from operands that had errors. */
4789 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4790 return error_mark_node;
4792 /* For ObjC properties, defer this check. */
4793 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
4794 return error_mark_node;
4796 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4798 rhs_semantic_type = TREE_TYPE (rhs);
4799 rhs = TREE_OPERAND (rhs, 0);
4802 newrhs = rhs;
4804 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4806 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4807 lhs_origtype, modifycode, rhs_loc, rhs,
4808 rhs_origtype);
4809 if (inner == error_mark_node)
4810 return error_mark_node;
4811 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4812 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4813 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4814 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4815 protected_set_expr_location (result, location);
4816 return result;
4819 /* If a binary op has been requested, combine the old LHS value with the RHS
4820 producing the value we should actually store into the LHS. */
4822 if (modifycode != NOP_EXPR)
4824 lhs = c_fully_fold (lhs, false, NULL);
4825 lhs = stabilize_reference (lhs);
4826 newrhs = build_binary_op (location,
4827 modifycode, lhs, rhs, 1);
4829 /* The original type of the right hand side is no longer
4830 meaningful. */
4831 rhs_origtype = NULL_TREE;
4834 if (c_dialect_objc ())
4836 /* Check if we are modifying an Objective-C property reference;
4837 if so, we need to generate setter calls. */
4838 result = objc_maybe_build_modify_expr (lhs, newrhs);
4839 if (result)
4840 return result;
4842 /* Else, do the check that we postponed for Objective-C. */
4843 if (!lvalue_or_else (location, lhs, lv_assign))
4844 return error_mark_node;
4847 /* Give an error for storing in something that is 'const'. */
4849 if (TYPE_READONLY (lhstype)
4850 || ((TREE_CODE (lhstype) == RECORD_TYPE
4851 || TREE_CODE (lhstype) == UNION_TYPE)
4852 && C_TYPE_FIELDS_READONLY (lhstype)))
4854 readonly_error (lhs, lv_assign);
4855 return error_mark_node;
4857 else if (TREE_READONLY (lhs))
4858 readonly_warning (lhs, lv_assign);
4860 /* If storing into a structure or union member,
4861 it has probably been given type `int'.
4862 Compute the type that would go with
4863 the actual amount of storage the member occupies. */
4865 if (TREE_CODE (lhs) == COMPONENT_REF
4866 && (TREE_CODE (lhstype) == INTEGER_TYPE
4867 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4868 || TREE_CODE (lhstype) == REAL_TYPE
4869 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4870 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4872 /* If storing in a field that is in actuality a short or narrower than one,
4873 we must store in the field in its actual type. */
4875 if (lhstype != TREE_TYPE (lhs))
4877 lhs = copy_node (lhs);
4878 TREE_TYPE (lhs) = lhstype;
4881 /* Issue -Wc++-compat warnings about an assignment to an enum type
4882 when LHS does not have its original type. This happens for,
4883 e.g., an enum bitfield in a struct. */
4884 if (warn_cxx_compat
4885 && lhs_origtype != NULL_TREE
4886 && lhs_origtype != lhstype
4887 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4889 tree checktype = (rhs_origtype != NULL_TREE
4890 ? rhs_origtype
4891 : TREE_TYPE (rhs));
4892 if (checktype != error_mark_node
4893 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4894 warning_at (location, OPT_Wc___compat,
4895 "enum conversion in assignment is invalid in C++");
4898 /* Convert new value to destination type. Fold it first, then
4899 restore any excess precision information, for the sake of
4900 conversion warnings. */
4902 npc = null_pointer_constant_p (newrhs);
4903 newrhs = c_fully_fold (newrhs, false, NULL);
4904 if (rhs_semantic_type)
4905 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4906 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4907 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4908 if (TREE_CODE (newrhs) == ERROR_MARK)
4909 return error_mark_node;
4911 /* Emit ObjC write barrier, if necessary. */
4912 if (c_dialect_objc () && flag_objc_gc)
4914 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4915 if (result)
4917 protected_set_expr_location (result, location);
4918 return result;
4922 /* Scan operands. */
4924 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4925 TREE_SIDE_EFFECTS (result) = 1;
4926 protected_set_expr_location (result, location);
4928 /* If we got the LHS in a different type for storing in,
4929 convert the result back to the nominal type of LHS
4930 so that the value we return always has the same type
4931 as the LHS argument. */
4933 if (olhstype == TREE_TYPE (result))
4934 return result;
4936 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
4937 ic_assign, false, NULL_TREE, NULL_TREE, 0);
4938 protected_set_expr_location (result, location);
4939 return result;
4942 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
4943 This is used to implement -fplan9-extensions. */
4945 static bool
4946 find_anonymous_field_with_type (tree struct_type, tree type)
4948 tree field;
4949 bool found;
4951 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
4952 || TREE_CODE (struct_type) == UNION_TYPE);
4953 found = false;
4954 for (field = TYPE_FIELDS (struct_type);
4955 field != NULL_TREE;
4956 field = TREE_CHAIN (field))
4958 if (DECL_NAME (field) == NULL
4959 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
4961 if (found)
4962 return false;
4963 found = true;
4965 else if (DECL_NAME (field) == NULL
4966 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
4967 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
4968 && find_anonymous_field_with_type (TREE_TYPE (field), type))
4970 if (found)
4971 return false;
4972 found = true;
4975 return found;
4978 /* RHS is an expression whose type is pointer to struct. If there is
4979 an anonymous field in RHS with type TYPE, then return a pointer to
4980 that field in RHS. This is used with -fplan9-extensions. This
4981 returns NULL if no conversion could be found. */
4983 static tree
4984 convert_to_anonymous_field (location_t location, tree type, tree rhs)
4986 tree rhs_struct_type, lhs_main_type;
4987 tree field, found_field;
4988 bool found_sub_field;
4989 tree ret;
4991 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
4992 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
4993 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
4994 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
4996 gcc_assert (POINTER_TYPE_P (type));
4997 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4999 found_field = NULL_TREE;
5000 found_sub_field = false;
5001 for (field = TYPE_FIELDS (rhs_struct_type);
5002 field != NULL_TREE;
5003 field = TREE_CHAIN (field))
5005 if (DECL_NAME (field) != NULL_TREE
5006 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5007 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5008 continue;
5009 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5011 if (found_field != NULL_TREE)
5012 return NULL_TREE;
5013 found_field = field;
5015 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5016 lhs_main_type))
5018 if (found_field != NULL_TREE)
5019 return NULL_TREE;
5020 found_field = field;
5021 found_sub_field = true;
5025 if (found_field == NULL_TREE)
5026 return NULL_TREE;
5028 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5029 build_fold_indirect_ref (rhs), found_field,
5030 NULL_TREE);
5031 ret = build_fold_addr_expr_loc (location, ret);
5033 if (found_sub_field)
5035 ret = convert_to_anonymous_field (location, type, ret);
5036 gcc_assert (ret != NULL_TREE);
5039 return ret;
5042 /* Convert value RHS to type TYPE as preparation for an assignment to
5043 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5044 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5045 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5046 constant before any folding.
5047 The real work of conversion is done by `convert'.
5048 The purpose of this function is to generate error messages
5049 for assignments that are not allowed in C.
5050 ERRTYPE says whether it is argument passing, assignment,
5051 initialization or return.
5053 LOCATION is the location of the RHS.
5054 FUNCTION is a tree for the function being called.
5055 PARMNUM is the number of the argument, for printing in error messages. */
5057 static tree
5058 convert_for_assignment (location_t location, tree type, tree rhs,
5059 tree origtype, enum impl_conv errtype,
5060 bool null_pointer_constant, tree fundecl,
5061 tree function, int parmnum)
5063 enum tree_code codel = TREE_CODE (type);
5064 tree orig_rhs = rhs;
5065 tree rhstype;
5066 enum tree_code coder;
5067 tree rname = NULL_TREE;
5068 bool objc_ok = false;
5070 if (errtype == ic_argpass)
5072 tree selector;
5073 /* Change pointer to function to the function itself for
5074 diagnostics. */
5075 if (TREE_CODE (function) == ADDR_EXPR
5076 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5077 function = TREE_OPERAND (function, 0);
5079 /* Handle an ObjC selector specially for diagnostics. */
5080 selector = objc_message_selector ();
5081 rname = function;
5082 if (selector && parmnum > 2)
5084 rname = selector;
5085 parmnum -= 2;
5089 /* This macro is used to emit diagnostics to ensure that all format
5090 strings are complete sentences, visible to gettext and checked at
5091 compile time. */
5092 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5093 do { \
5094 switch (errtype) \
5096 case ic_argpass: \
5097 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5098 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5099 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5100 "expected %qT but argument is of type %qT", \
5101 type, rhstype); \
5102 break; \
5103 case ic_assign: \
5104 pedwarn (LOCATION, OPT, AS); \
5105 break; \
5106 case ic_init: \
5107 pedwarn_init (LOCATION, OPT, IN); \
5108 break; \
5109 case ic_return: \
5110 pedwarn (LOCATION, OPT, RE); \
5111 break; \
5112 default: \
5113 gcc_unreachable (); \
5115 } while (0)
5117 /* This macro is used to emit diagnostics to ensure that all format
5118 strings are complete sentences, visible to gettext and checked at
5119 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5120 extra parameter to enumerate qualifiers. */
5122 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5123 do { \
5124 switch (errtype) \
5126 case ic_argpass: \
5127 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5128 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5129 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5130 "expected %qT but argument is of type %qT", \
5131 type, rhstype); \
5132 break; \
5133 case ic_assign: \
5134 pedwarn (LOCATION, OPT, AS, QUALS); \
5135 break; \
5136 case ic_init: \
5137 pedwarn (LOCATION, OPT, IN, QUALS); \
5138 break; \
5139 case ic_return: \
5140 pedwarn (LOCATION, OPT, RE, QUALS); \
5141 break; \
5142 default: \
5143 gcc_unreachable (); \
5145 } while (0)
5147 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5148 rhs = TREE_OPERAND (rhs, 0);
5150 rhstype = TREE_TYPE (rhs);
5151 coder = TREE_CODE (rhstype);
5153 if (coder == ERROR_MARK)
5154 return error_mark_node;
5156 if (c_dialect_objc ())
5158 int parmno;
5160 switch (errtype)
5162 case ic_return:
5163 parmno = 0;
5164 break;
5166 case ic_assign:
5167 parmno = -1;
5168 break;
5170 case ic_init:
5171 parmno = -2;
5172 break;
5174 default:
5175 parmno = parmnum;
5176 break;
5179 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5182 if (warn_cxx_compat)
5184 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5185 if (checktype != error_mark_node
5186 && TREE_CODE (type) == ENUMERAL_TYPE
5187 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5189 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5190 G_("enum conversion when passing argument "
5191 "%d of %qE is invalid in C++"),
5192 G_("enum conversion in assignment is "
5193 "invalid in C++"),
5194 G_("enum conversion in initialization is "
5195 "invalid in C++"),
5196 G_("enum conversion in return is "
5197 "invalid in C++"));
5201 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5202 return rhs;
5204 if (coder == VOID_TYPE)
5206 /* Except for passing an argument to an unprototyped function,
5207 this is a constraint violation. When passing an argument to
5208 an unprototyped function, it is compile-time undefined;
5209 making it a constraint in that case was rejected in
5210 DR#252. */
5211 error_at (location, "void value not ignored as it ought to be");
5212 return error_mark_node;
5214 rhs = require_complete_type (rhs);
5215 if (rhs == error_mark_node)
5216 return error_mark_node;
5217 /* A type converts to a reference to it.
5218 This code doesn't fully support references, it's just for the
5219 special case of va_start and va_copy. */
5220 if (codel == REFERENCE_TYPE
5221 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5223 if (!lvalue_p (rhs))
5225 error_at (location, "cannot pass rvalue to reference parameter");
5226 return error_mark_node;
5228 if (!c_mark_addressable (rhs))
5229 return error_mark_node;
5230 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5231 SET_EXPR_LOCATION (rhs, location);
5233 /* We already know that these two types are compatible, but they
5234 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5235 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5236 likely to be va_list, a typedef to __builtin_va_list, which
5237 is different enough that it will cause problems later. */
5238 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5240 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5241 SET_EXPR_LOCATION (rhs, location);
5244 rhs = build1 (NOP_EXPR, type, rhs);
5245 SET_EXPR_LOCATION (rhs, location);
5246 return rhs;
5248 /* Some types can interconvert without explicit casts. */
5249 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5250 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5251 return convert (type, rhs);
5252 /* Arithmetic types all interconvert, and enum is treated like int. */
5253 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5254 || codel == FIXED_POINT_TYPE
5255 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5256 || codel == BOOLEAN_TYPE)
5257 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5258 || coder == FIXED_POINT_TYPE
5259 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5260 || coder == BOOLEAN_TYPE))
5262 tree ret;
5263 bool save = in_late_binary_op;
5264 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5265 in_late_binary_op = true;
5266 ret = convert_and_check (type, orig_rhs);
5267 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5268 in_late_binary_op = save;
5269 return ret;
5272 /* Aggregates in different TUs might need conversion. */
5273 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5274 && codel == coder
5275 && comptypes (type, rhstype))
5276 return convert_and_check (type, rhs);
5278 /* Conversion to a transparent union or record from its member types.
5279 This applies only to function arguments. */
5280 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5281 && TYPE_TRANSPARENT_AGGR (type))
5282 && errtype == ic_argpass)
5284 tree memb, marginal_memb = NULL_TREE;
5286 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5288 tree memb_type = TREE_TYPE (memb);
5290 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5291 TYPE_MAIN_VARIANT (rhstype)))
5292 break;
5294 if (TREE_CODE (memb_type) != POINTER_TYPE)
5295 continue;
5297 if (coder == POINTER_TYPE)
5299 tree ttl = TREE_TYPE (memb_type);
5300 tree ttr = TREE_TYPE (rhstype);
5302 /* Any non-function converts to a [const][volatile] void *
5303 and vice versa; otherwise, targets must be the same.
5304 Meanwhile, the lhs target must have all the qualifiers of
5305 the rhs. */
5306 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5307 || comp_target_types (location, memb_type, rhstype))
5309 /* If this type won't generate any warnings, use it. */
5310 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5311 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5312 && TREE_CODE (ttl) == FUNCTION_TYPE)
5313 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5314 == TYPE_QUALS (ttr))
5315 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5316 == TYPE_QUALS (ttl))))
5317 break;
5319 /* Keep looking for a better type, but remember this one. */
5320 if (!marginal_memb)
5321 marginal_memb = memb;
5325 /* Can convert integer zero to any pointer type. */
5326 if (null_pointer_constant)
5328 rhs = null_pointer_node;
5329 break;
5333 if (memb || marginal_memb)
5335 if (!memb)
5337 /* We have only a marginally acceptable member type;
5338 it needs a warning. */
5339 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5340 tree ttr = TREE_TYPE (rhstype);
5342 /* Const and volatile mean something different for function
5343 types, so the usual warnings are not appropriate. */
5344 if (TREE_CODE (ttr) == FUNCTION_TYPE
5345 && TREE_CODE (ttl) == FUNCTION_TYPE)
5347 /* Because const and volatile on functions are
5348 restrictions that say the function will not do
5349 certain things, it is okay to use a const or volatile
5350 function where an ordinary one is wanted, but not
5351 vice-versa. */
5352 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5353 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5354 WARN_FOR_QUALIFIERS (location, 0,
5355 G_("passing argument %d of %qE "
5356 "makes %q#v qualified function "
5357 "pointer from unqualified"),
5358 G_("assignment makes %q#v qualified "
5359 "function pointer from "
5360 "unqualified"),
5361 G_("initialization makes %q#v qualified "
5362 "function pointer from "
5363 "unqualified"),
5364 G_("return makes %q#v qualified function "
5365 "pointer from unqualified"),
5366 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5368 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5369 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5370 WARN_FOR_QUALIFIERS (location, 0,
5371 G_("passing argument %d of %qE discards "
5372 "%qv qualifier from pointer target type"),
5373 G_("assignment discards %qv qualifier "
5374 "from pointer target type"),
5375 G_("initialization discards %qv qualifier "
5376 "from pointer target type"),
5377 G_("return discards %qv qualifier from "
5378 "pointer target type"),
5379 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5381 memb = marginal_memb;
5384 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5385 pedwarn (location, OPT_pedantic,
5386 "ISO C prohibits argument conversion to union type");
5388 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5389 return build_constructor_single (type, memb, rhs);
5393 /* Conversions among pointers */
5394 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5395 && (coder == codel))
5397 tree ttl = TREE_TYPE (type);
5398 tree ttr = TREE_TYPE (rhstype);
5399 tree mvl = ttl;
5400 tree mvr = ttr;
5401 bool is_opaque_pointer;
5402 int target_cmp = 0; /* Cache comp_target_types () result. */
5403 addr_space_t asl;
5404 addr_space_t asr;
5406 if (TREE_CODE (mvl) != ARRAY_TYPE)
5407 mvl = TYPE_MAIN_VARIANT (mvl);
5408 if (TREE_CODE (mvr) != ARRAY_TYPE)
5409 mvr = TYPE_MAIN_VARIANT (mvr);
5410 /* Opaque pointers are treated like void pointers. */
5411 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5413 /* The Plan 9 compiler permits a pointer to a struct to be
5414 automatically converted into a pointer to an anonymous field
5415 within the struct. */
5416 if (flag_plan9_extensions
5417 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5418 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5419 && mvl != mvr)
5421 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5422 if (new_rhs != NULL_TREE)
5424 rhs = new_rhs;
5425 rhstype = TREE_TYPE (rhs);
5426 coder = TREE_CODE (rhstype);
5427 ttr = TREE_TYPE (rhstype);
5428 mvr = TYPE_MAIN_VARIANT (ttr);
5432 /* C++ does not allow the implicit conversion void* -> T*. However,
5433 for the purpose of reducing the number of false positives, we
5434 tolerate the special case of
5436 int *p = NULL;
5438 where NULL is typically defined in C to be '(void *) 0'. */
5439 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5440 warning_at (location, OPT_Wc___compat,
5441 "request for implicit conversion "
5442 "from %qT to %qT not permitted in C++", rhstype, type);
5444 /* See if the pointers point to incompatible address spaces. */
5445 asl = TYPE_ADDR_SPACE (ttl);
5446 asr = TYPE_ADDR_SPACE (ttr);
5447 if (!null_pointer_constant_p (rhs)
5448 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5450 switch (errtype)
5452 case ic_argpass:
5453 error_at (location, "passing argument %d of %qE from pointer to "
5454 "non-enclosed address space", parmnum, rname);
5455 break;
5456 case ic_assign:
5457 error_at (location, "assignment from pointer to "
5458 "non-enclosed address space");
5459 break;
5460 case ic_init:
5461 error_at (location, "initialization from pointer to "
5462 "non-enclosed address space");
5463 break;
5464 case ic_return:
5465 error_at (location, "return from pointer to "
5466 "non-enclosed address space");
5467 break;
5468 default:
5469 gcc_unreachable ();
5471 return error_mark_node;
5474 /* Check if the right-hand side has a format attribute but the
5475 left-hand side doesn't. */
5476 if (warn_missing_format_attribute
5477 && check_missing_format_attribute (type, rhstype))
5479 switch (errtype)
5481 case ic_argpass:
5482 warning_at (location, OPT_Wmissing_format_attribute,
5483 "argument %d of %qE might be "
5484 "a candidate for a format attribute",
5485 parmnum, rname);
5486 break;
5487 case ic_assign:
5488 warning_at (location, OPT_Wmissing_format_attribute,
5489 "assignment left-hand side might be "
5490 "a candidate for a format attribute");
5491 break;
5492 case ic_init:
5493 warning_at (location, OPT_Wmissing_format_attribute,
5494 "initialization left-hand side might be "
5495 "a candidate for a format attribute");
5496 break;
5497 case ic_return:
5498 warning_at (location, OPT_Wmissing_format_attribute,
5499 "return type might be "
5500 "a candidate for a format attribute");
5501 break;
5502 default:
5503 gcc_unreachable ();
5507 /* Any non-function converts to a [const][volatile] void *
5508 and vice versa; otherwise, targets must be the same.
5509 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5510 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5511 || (target_cmp = comp_target_types (location, type, rhstype))
5512 || is_opaque_pointer
5513 || (c_common_unsigned_type (mvl)
5514 == c_common_unsigned_type (mvr)))
5516 if (pedantic
5517 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5519 (VOID_TYPE_P (ttr)
5520 && !null_pointer_constant
5521 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5522 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5523 G_("ISO C forbids passing argument %d of "
5524 "%qE between function pointer "
5525 "and %<void *%>"),
5526 G_("ISO C forbids assignment between "
5527 "function pointer and %<void *%>"),
5528 G_("ISO C forbids initialization between "
5529 "function pointer and %<void *%>"),
5530 G_("ISO C forbids return between function "
5531 "pointer and %<void *%>"));
5532 /* Const and volatile mean something different for function types,
5533 so the usual warnings are not appropriate. */
5534 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5535 && TREE_CODE (ttl) != FUNCTION_TYPE)
5537 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5538 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5540 WARN_FOR_QUALIFIERS (location, 0,
5541 G_("passing argument %d of %qE discards "
5542 "%qv qualifier from pointer target type"),
5543 G_("assignment discards %qv qualifier "
5544 "from pointer target type"),
5545 G_("initialization discards %qv qualifier "
5546 "from pointer target type"),
5547 G_("return discards %qv qualifier from "
5548 "pointer target type"),
5549 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5551 /* If this is not a case of ignoring a mismatch in signedness,
5552 no warning. */
5553 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5554 || target_cmp)
5556 /* If there is a mismatch, do warn. */
5557 else if (warn_pointer_sign)
5558 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5559 G_("pointer targets in passing argument "
5560 "%d of %qE differ in signedness"),
5561 G_("pointer targets in assignment "
5562 "differ in signedness"),
5563 G_("pointer targets in initialization "
5564 "differ in signedness"),
5565 G_("pointer targets in return differ "
5566 "in signedness"));
5568 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5569 && TREE_CODE (ttr) == FUNCTION_TYPE)
5571 /* Because const and volatile on functions are restrictions
5572 that say the function will not do certain things,
5573 it is okay to use a const or volatile function
5574 where an ordinary one is wanted, but not vice-versa. */
5575 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5576 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5577 WARN_FOR_QUALIFIERS (location, 0,
5578 G_("passing argument %d of %qE makes "
5579 "%q#v qualified function pointer "
5580 "from unqualified"),
5581 G_("assignment makes %q#v qualified function "
5582 "pointer from unqualified"),
5583 G_("initialization makes %q#v qualified "
5584 "function pointer from unqualified"),
5585 G_("return makes %q#v qualified function "
5586 "pointer from unqualified"),
5587 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5590 else
5591 /* Avoid warning about the volatile ObjC EH puts on decls. */
5592 if (!objc_ok)
5593 WARN_FOR_ASSIGNMENT (location, 0,
5594 G_("passing argument %d of %qE from "
5595 "incompatible pointer type"),
5596 G_("assignment from incompatible pointer type"),
5597 G_("initialization from incompatible "
5598 "pointer type"),
5599 G_("return from incompatible pointer type"));
5601 return convert (type, rhs);
5603 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5605 /* ??? This should not be an error when inlining calls to
5606 unprototyped functions. */
5607 error_at (location, "invalid use of non-lvalue array");
5608 return error_mark_node;
5610 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5612 /* An explicit constant 0 can convert to a pointer,
5613 or one that results from arithmetic, even including
5614 a cast to integer type. */
5615 if (!null_pointer_constant)
5616 WARN_FOR_ASSIGNMENT (location, 0,
5617 G_("passing argument %d of %qE makes "
5618 "pointer from integer without a cast"),
5619 G_("assignment makes pointer from integer "
5620 "without a cast"),
5621 G_("initialization makes pointer from "
5622 "integer without a cast"),
5623 G_("return makes pointer from integer "
5624 "without a cast"));
5626 return convert (type, rhs);
5628 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5630 WARN_FOR_ASSIGNMENT (location, 0,
5631 G_("passing argument %d of %qE makes integer "
5632 "from pointer without a cast"),
5633 G_("assignment makes integer from pointer "
5634 "without a cast"),
5635 G_("initialization makes integer from pointer "
5636 "without a cast"),
5637 G_("return makes integer from pointer "
5638 "without a cast"));
5639 return convert (type, rhs);
5641 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5643 tree ret;
5644 bool save = in_late_binary_op;
5645 in_late_binary_op = true;
5646 ret = convert (type, rhs);
5647 in_late_binary_op = save;
5648 return ret;
5651 switch (errtype)
5653 case ic_argpass:
5654 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5655 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5656 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5657 "expected %qT but argument is of type %qT", type, rhstype);
5658 break;
5659 case ic_assign:
5660 error_at (location, "incompatible types when assigning to type %qT from "
5661 "type %qT", type, rhstype);
5662 break;
5663 case ic_init:
5664 error_at (location,
5665 "incompatible types when initializing type %qT using type %qT",
5666 type, rhstype);
5667 break;
5668 case ic_return:
5669 error_at (location,
5670 "incompatible types when returning type %qT but %qT was "
5671 "expected", rhstype, type);
5672 break;
5673 default:
5674 gcc_unreachable ();
5677 return error_mark_node;
5680 /* If VALUE is a compound expr all of whose expressions are constant, then
5681 return its value. Otherwise, return error_mark_node.
5683 This is for handling COMPOUND_EXPRs as initializer elements
5684 which is allowed with a warning when -pedantic is specified. */
5686 static tree
5687 valid_compound_expr_initializer (tree value, tree endtype)
5689 if (TREE_CODE (value) == COMPOUND_EXPR)
5691 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5692 == error_mark_node)
5693 return error_mark_node;
5694 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5695 endtype);
5697 else if (!initializer_constant_valid_p (value, endtype))
5698 return error_mark_node;
5699 else
5700 return value;
5703 /* Perform appropriate conversions on the initial value of a variable,
5704 store it in the declaration DECL,
5705 and print any error messages that are appropriate.
5706 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5707 If the init is invalid, store an ERROR_MARK.
5709 INIT_LOC is the location of the initial value. */
5711 void
5712 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5714 tree value, type;
5715 bool npc = false;
5717 /* If variable's type was invalidly declared, just ignore it. */
5719 type = TREE_TYPE (decl);
5720 if (TREE_CODE (type) == ERROR_MARK)
5721 return;
5723 /* Digest the specified initializer into an expression. */
5725 if (init)
5726 npc = null_pointer_constant_p (init);
5727 value = digest_init (init_loc, type, init, origtype, npc,
5728 true, TREE_STATIC (decl));
5730 /* Store the expression if valid; else report error. */
5732 if (!in_system_header
5733 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5734 warning (OPT_Wtraditional, "traditional C rejects automatic "
5735 "aggregate initialization");
5737 DECL_INITIAL (decl) = value;
5739 /* ANSI wants warnings about out-of-range constant initializers. */
5740 STRIP_TYPE_NOPS (value);
5741 if (TREE_STATIC (decl))
5742 constant_expression_warning (value);
5744 /* Check if we need to set array size from compound literal size. */
5745 if (TREE_CODE (type) == ARRAY_TYPE
5746 && TYPE_DOMAIN (type) == 0
5747 && value != error_mark_node)
5749 tree inside_init = init;
5751 STRIP_TYPE_NOPS (inside_init);
5752 inside_init = fold (inside_init);
5754 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5756 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5758 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5760 /* For int foo[] = (int [3]){1}; we need to set array size
5761 now since later on array initializer will be just the
5762 brace enclosed list of the compound literal. */
5763 tree etype = strip_array_types (TREE_TYPE (decl));
5764 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5765 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5766 layout_type (type);
5767 layout_decl (cldecl, 0);
5768 TREE_TYPE (decl)
5769 = c_build_qualified_type (type, TYPE_QUALS (etype));
5775 /* Methods for storing and printing names for error messages. */
5777 /* Implement a spelling stack that allows components of a name to be pushed
5778 and popped. Each element on the stack is this structure. */
5780 struct spelling
5782 int kind;
5783 union
5785 unsigned HOST_WIDE_INT i;
5786 const char *s;
5787 } u;
5790 #define SPELLING_STRING 1
5791 #define SPELLING_MEMBER 2
5792 #define SPELLING_BOUNDS 3
5794 static struct spelling *spelling; /* Next stack element (unused). */
5795 static struct spelling *spelling_base; /* Spelling stack base. */
5796 static int spelling_size; /* Size of the spelling stack. */
5798 /* Macros to save and restore the spelling stack around push_... functions.
5799 Alternative to SAVE_SPELLING_STACK. */
5801 #define SPELLING_DEPTH() (spelling - spelling_base)
5802 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5804 /* Push an element on the spelling stack with type KIND and assign VALUE
5805 to MEMBER. */
5807 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5809 int depth = SPELLING_DEPTH (); \
5811 if (depth >= spelling_size) \
5813 spelling_size += 10; \
5814 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5815 spelling_size); \
5816 RESTORE_SPELLING_DEPTH (depth); \
5819 spelling->kind = (KIND); \
5820 spelling->MEMBER = (VALUE); \
5821 spelling++; \
5824 /* Push STRING on the stack. Printed literally. */
5826 static void
5827 push_string (const char *string)
5829 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5832 /* Push a member name on the stack. Printed as '.' STRING. */
5834 static void
5835 push_member_name (tree decl)
5837 const char *const string
5838 = (DECL_NAME (decl)
5839 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5840 : _("<anonymous>"));
5841 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5844 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5846 static void
5847 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5849 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5852 /* Compute the maximum size in bytes of the printed spelling. */
5854 static int
5855 spelling_length (void)
5857 int size = 0;
5858 struct spelling *p;
5860 for (p = spelling_base; p < spelling; p++)
5862 if (p->kind == SPELLING_BOUNDS)
5863 size += 25;
5864 else
5865 size += strlen (p->u.s) + 1;
5868 return size;
5871 /* Print the spelling to BUFFER and return it. */
5873 static char *
5874 print_spelling (char *buffer)
5876 char *d = buffer;
5877 struct spelling *p;
5879 for (p = spelling_base; p < spelling; p++)
5880 if (p->kind == SPELLING_BOUNDS)
5882 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5883 d += strlen (d);
5885 else
5887 const char *s;
5888 if (p->kind == SPELLING_MEMBER)
5889 *d++ = '.';
5890 for (s = p->u.s; (*d = *s++); d++)
5893 *d++ = '\0';
5894 return buffer;
5897 /* Issue an error message for a bad initializer component.
5898 GMSGID identifies the message.
5899 The component name is taken from the spelling stack. */
5901 void
5902 error_init (const char *gmsgid)
5904 char *ofwhat;
5906 /* The gmsgid may be a format string with %< and %>. */
5907 error (gmsgid);
5908 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5909 if (*ofwhat)
5910 error ("(near initialization for %qs)", ofwhat);
5913 /* Issue a pedantic warning for a bad initializer component. OPT is
5914 the option OPT_* (from options.h) controlling this warning or 0 if
5915 it is unconditionally given. GMSGID identifies the message. The
5916 component name is taken from the spelling stack. */
5918 void
5919 pedwarn_init (location_t location, int opt, const char *gmsgid)
5921 char *ofwhat;
5923 /* The gmsgid may be a format string with %< and %>. */
5924 pedwarn (location, opt, gmsgid);
5925 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5926 if (*ofwhat)
5927 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5930 /* Issue a warning for a bad initializer component.
5932 OPT is the OPT_W* value corresponding to the warning option that
5933 controls this warning. GMSGID identifies the message. The
5934 component name is taken from the spelling stack. */
5936 static void
5937 warning_init (int opt, const char *gmsgid)
5939 char *ofwhat;
5941 /* The gmsgid may be a format string with %< and %>. */
5942 warning (opt, gmsgid);
5943 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5944 if (*ofwhat)
5945 warning (opt, "(near initialization for %qs)", ofwhat);
5948 /* If TYPE is an array type and EXPR is a parenthesized string
5949 constant, warn if pedantic that EXPR is being used to initialize an
5950 object of type TYPE. */
5952 void
5953 maybe_warn_string_init (tree type, struct c_expr expr)
5955 if (pedantic
5956 && TREE_CODE (type) == ARRAY_TYPE
5957 && TREE_CODE (expr.value) == STRING_CST
5958 && expr.original_code != STRING_CST)
5959 pedwarn_init (input_location, OPT_pedantic,
5960 "array initialized from parenthesized string constant");
5963 /* Digest the parser output INIT as an initializer for type TYPE.
5964 Return a C expression of type TYPE to represent the initial value.
5966 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5968 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5970 If INIT is a string constant, STRICT_STRING is true if it is
5971 unparenthesized or we should not warn here for it being parenthesized.
5972 For other types of INIT, STRICT_STRING is not used.
5974 INIT_LOC is the location of the INIT.
5976 REQUIRE_CONSTANT requests an error if non-constant initializers or
5977 elements are seen. */
5979 static tree
5980 digest_init (location_t init_loc, tree type, tree init, tree origtype,
5981 bool null_pointer_constant, bool strict_string,
5982 int require_constant)
5984 enum tree_code code = TREE_CODE (type);
5985 tree inside_init = init;
5986 tree semantic_type = NULL_TREE;
5987 bool maybe_const = true;
5989 if (type == error_mark_node
5990 || !init
5991 || init == error_mark_node
5992 || TREE_TYPE (init) == error_mark_node)
5993 return error_mark_node;
5995 STRIP_TYPE_NOPS (inside_init);
5997 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5999 semantic_type = TREE_TYPE (inside_init);
6000 inside_init = TREE_OPERAND (inside_init, 0);
6002 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6003 inside_init = decl_constant_value_for_optimization (inside_init);
6005 /* Initialization of an array of chars from a string constant
6006 optionally enclosed in braces. */
6008 if (code == ARRAY_TYPE && inside_init
6009 && TREE_CODE (inside_init) == STRING_CST)
6011 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6012 /* Note that an array could be both an array of character type
6013 and an array of wchar_t if wchar_t is signed char or unsigned
6014 char. */
6015 bool char_array = (typ1 == char_type_node
6016 || typ1 == signed_char_type_node
6017 || typ1 == unsigned_char_type_node);
6018 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6019 bool char16_array = !!comptypes (typ1, char16_type_node);
6020 bool char32_array = !!comptypes (typ1, char32_type_node);
6022 if (char_array || wchar_array || char16_array || char32_array)
6024 struct c_expr expr;
6025 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6026 expr.value = inside_init;
6027 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6028 expr.original_type = NULL;
6029 maybe_warn_string_init (type, expr);
6031 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6032 pedwarn_init (init_loc, OPT_pedantic,
6033 "initialization of a flexible array member");
6035 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6036 TYPE_MAIN_VARIANT (type)))
6037 return inside_init;
6039 if (char_array)
6041 if (typ2 != char_type_node)
6043 error_init ("char-array initialized from wide string");
6044 return error_mark_node;
6047 else
6049 if (typ2 == char_type_node)
6051 error_init ("wide character array initialized from non-wide "
6052 "string");
6053 return error_mark_node;
6055 else if (!comptypes(typ1, typ2))
6057 error_init ("wide character array initialized from "
6058 "incompatible wide string");
6059 return error_mark_node;
6063 TREE_TYPE (inside_init) = type;
6064 if (TYPE_DOMAIN (type) != 0
6065 && TYPE_SIZE (type) != 0
6066 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6068 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6070 /* Subtract the size of a single (possibly wide) character
6071 because it's ok to ignore the terminating null char
6072 that is counted in the length of the constant. */
6073 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6074 (len
6075 - (TYPE_PRECISION (typ1)
6076 / BITS_PER_UNIT))))
6077 pedwarn_init (init_loc, 0,
6078 ("initializer-string for array of chars "
6079 "is too long"));
6080 else if (warn_cxx_compat
6081 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6082 warning_at (init_loc, OPT_Wc___compat,
6083 ("initializer-string for array chars "
6084 "is too long for C++"));
6087 return inside_init;
6089 else if (INTEGRAL_TYPE_P (typ1))
6091 error_init ("array of inappropriate type initialized "
6092 "from string constant");
6093 return error_mark_node;
6097 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6098 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6099 below and handle as a constructor. */
6100 if (code == VECTOR_TYPE
6101 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6102 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6103 && TREE_CONSTANT (inside_init))
6105 if (TREE_CODE (inside_init) == VECTOR_CST
6106 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6107 TYPE_MAIN_VARIANT (type)))
6108 return inside_init;
6110 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6112 unsigned HOST_WIDE_INT ix;
6113 tree value;
6114 bool constant_p = true;
6116 /* Iterate through elements and check if all constructor
6117 elements are *_CSTs. */
6118 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6119 if (!CONSTANT_CLASS_P (value))
6121 constant_p = false;
6122 break;
6125 if (constant_p)
6126 return build_vector_from_ctor (type,
6127 CONSTRUCTOR_ELTS (inside_init));
6131 if (warn_sequence_point)
6132 verify_sequence_points (inside_init);
6134 /* Any type can be initialized
6135 from an expression of the same type, optionally with braces. */
6137 if (inside_init && TREE_TYPE (inside_init) != 0
6138 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6139 TYPE_MAIN_VARIANT (type))
6140 || (code == ARRAY_TYPE
6141 && comptypes (TREE_TYPE (inside_init), type))
6142 || (code == VECTOR_TYPE
6143 && comptypes (TREE_TYPE (inside_init), type))
6144 || (code == POINTER_TYPE
6145 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6146 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6147 TREE_TYPE (type)))))
6149 if (code == POINTER_TYPE)
6151 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6153 if (TREE_CODE (inside_init) == STRING_CST
6154 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6155 inside_init = array_to_pointer_conversion
6156 (init_loc, inside_init);
6157 else
6159 error_init ("invalid use of non-lvalue array");
6160 return error_mark_node;
6165 if (code == VECTOR_TYPE)
6166 /* Although the types are compatible, we may require a
6167 conversion. */
6168 inside_init = convert (type, inside_init);
6170 if (require_constant
6171 && (code == VECTOR_TYPE || !flag_isoc99)
6172 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6174 /* As an extension, allow initializing objects with static storage
6175 duration with compound literals (which are then treated just as
6176 the brace enclosed list they contain). Also allow this for
6177 vectors, as we can only assign them with compound literals. */
6178 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6179 inside_init = DECL_INITIAL (decl);
6182 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6183 && TREE_CODE (inside_init) != CONSTRUCTOR)
6185 error_init ("array initialized from non-constant array expression");
6186 return error_mark_node;
6189 /* Compound expressions can only occur here if -pedantic or
6190 -pedantic-errors is specified. In the later case, we always want
6191 an error. In the former case, we simply want a warning. */
6192 if (require_constant && pedantic
6193 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6195 inside_init
6196 = valid_compound_expr_initializer (inside_init,
6197 TREE_TYPE (inside_init));
6198 if (inside_init == error_mark_node)
6199 error_init ("initializer element is not constant");
6200 else
6201 pedwarn_init (init_loc, OPT_pedantic,
6202 "initializer element is not constant");
6203 if (flag_pedantic_errors)
6204 inside_init = error_mark_node;
6206 else if (require_constant
6207 && !initializer_constant_valid_p (inside_init,
6208 TREE_TYPE (inside_init)))
6210 error_init ("initializer element is not constant");
6211 inside_init = error_mark_node;
6213 else if (require_constant && !maybe_const)
6214 pedwarn_init (init_loc, 0,
6215 "initializer element is not a constant expression");
6217 /* Added to enable additional -Wmissing-format-attribute warnings. */
6218 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6219 inside_init = convert_for_assignment (init_loc, type, inside_init,
6220 origtype,
6221 ic_init, null_pointer_constant,
6222 NULL_TREE, NULL_TREE, 0);
6223 return inside_init;
6226 /* Handle scalar types, including conversions. */
6228 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6229 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6230 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6232 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6233 && (TREE_CODE (init) == STRING_CST
6234 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6235 inside_init = init = array_to_pointer_conversion (init_loc, init);
6236 if (semantic_type)
6237 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6238 inside_init);
6239 inside_init
6240 = convert_for_assignment (init_loc, type, inside_init, origtype,
6241 ic_init, null_pointer_constant,
6242 NULL_TREE, NULL_TREE, 0);
6244 /* Check to see if we have already given an error message. */
6245 if (inside_init == error_mark_node)
6247 else if (require_constant && !TREE_CONSTANT (inside_init))
6249 error_init ("initializer element is not constant");
6250 inside_init = error_mark_node;
6252 else if (require_constant
6253 && !initializer_constant_valid_p (inside_init,
6254 TREE_TYPE (inside_init)))
6256 error_init ("initializer element is not computable at load time");
6257 inside_init = error_mark_node;
6259 else if (require_constant && !maybe_const)
6260 pedwarn_init (init_loc, 0,
6261 "initializer element is not a constant expression");
6263 return inside_init;
6266 /* Come here only for records and arrays. */
6268 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6270 error_init ("variable-sized object may not be initialized");
6271 return error_mark_node;
6274 error_init ("invalid initializer");
6275 return error_mark_node;
6278 /* Handle initializers that use braces. */
6280 /* Type of object we are accumulating a constructor for.
6281 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6282 static tree constructor_type;
6284 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6285 left to fill. */
6286 static tree constructor_fields;
6288 /* For an ARRAY_TYPE, this is the specified index
6289 at which to store the next element we get. */
6290 static tree constructor_index;
6292 /* For an ARRAY_TYPE, this is the maximum index. */
6293 static tree constructor_max_index;
6295 /* For a RECORD_TYPE, this is the first field not yet written out. */
6296 static tree constructor_unfilled_fields;
6298 /* For an ARRAY_TYPE, this is the index of the first element
6299 not yet written out. */
6300 static tree constructor_unfilled_index;
6302 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6303 This is so we can generate gaps between fields, when appropriate. */
6304 static tree constructor_bit_index;
6306 /* If we are saving up the elements rather than allocating them,
6307 this is the list of elements so far (in reverse order,
6308 most recent first). */
6309 static VEC(constructor_elt,gc) *constructor_elements;
6311 /* 1 if constructor should be incrementally stored into a constructor chain,
6312 0 if all the elements should be kept in AVL tree. */
6313 static int constructor_incremental;
6315 /* 1 if so far this constructor's elements are all compile-time constants. */
6316 static int constructor_constant;
6318 /* 1 if so far this constructor's elements are all valid address constants. */
6319 static int constructor_simple;
6321 /* 1 if this constructor has an element that cannot be part of a
6322 constant expression. */
6323 static int constructor_nonconst;
6325 /* 1 if this constructor is erroneous so far. */
6326 static int constructor_erroneous;
6328 /* Structure for managing pending initializer elements, organized as an
6329 AVL tree. */
6331 struct init_node
6333 struct init_node *left, *right;
6334 struct init_node *parent;
6335 int balance;
6336 tree purpose;
6337 tree value;
6338 tree origtype;
6341 /* Tree of pending elements at this constructor level.
6342 These are elements encountered out of order
6343 which belong at places we haven't reached yet in actually
6344 writing the output.
6345 Will never hold tree nodes across GC runs. */
6346 static struct init_node *constructor_pending_elts;
6348 /* The SPELLING_DEPTH of this constructor. */
6349 static int constructor_depth;
6351 /* DECL node for which an initializer is being read.
6352 0 means we are reading a constructor expression
6353 such as (struct foo) {...}. */
6354 static tree constructor_decl;
6356 /* Nonzero if this is an initializer for a top-level decl. */
6357 static int constructor_top_level;
6359 /* Nonzero if there were any member designators in this initializer. */
6360 static int constructor_designated;
6362 /* Nesting depth of designator list. */
6363 static int designator_depth;
6365 /* Nonzero if there were diagnosed errors in this designator list. */
6366 static int designator_erroneous;
6369 /* This stack has a level for each implicit or explicit level of
6370 structuring in the initializer, including the outermost one. It
6371 saves the values of most of the variables above. */
6373 struct constructor_range_stack;
6375 struct constructor_stack
6377 struct constructor_stack *next;
6378 tree type;
6379 tree fields;
6380 tree index;
6381 tree max_index;
6382 tree unfilled_index;
6383 tree unfilled_fields;
6384 tree bit_index;
6385 VEC(constructor_elt,gc) *elements;
6386 struct init_node *pending_elts;
6387 int offset;
6388 int depth;
6389 /* If value nonzero, this value should replace the entire
6390 constructor at this level. */
6391 struct c_expr replacement_value;
6392 struct constructor_range_stack *range_stack;
6393 char constant;
6394 char simple;
6395 char nonconst;
6396 char implicit;
6397 char erroneous;
6398 char outer;
6399 char incremental;
6400 char designated;
6403 static struct constructor_stack *constructor_stack;
6405 /* This stack represents designators from some range designator up to
6406 the last designator in the list. */
6408 struct constructor_range_stack
6410 struct constructor_range_stack *next, *prev;
6411 struct constructor_stack *stack;
6412 tree range_start;
6413 tree index;
6414 tree range_end;
6415 tree fields;
6418 static struct constructor_range_stack *constructor_range_stack;
6420 /* This stack records separate initializers that are nested.
6421 Nested initializers can't happen in ANSI C, but GNU C allows them
6422 in cases like { ... (struct foo) { ... } ... }. */
6424 struct initializer_stack
6426 struct initializer_stack *next;
6427 tree decl;
6428 struct constructor_stack *constructor_stack;
6429 struct constructor_range_stack *constructor_range_stack;
6430 VEC(constructor_elt,gc) *elements;
6431 struct spelling *spelling;
6432 struct spelling *spelling_base;
6433 int spelling_size;
6434 char top_level;
6435 char require_constant_value;
6436 char require_constant_elements;
6439 static struct initializer_stack *initializer_stack;
6441 /* Prepare to parse and output the initializer for variable DECL. */
6443 void
6444 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6446 const char *locus;
6447 struct initializer_stack *p = XNEW (struct initializer_stack);
6449 p->decl = constructor_decl;
6450 p->require_constant_value = require_constant_value;
6451 p->require_constant_elements = require_constant_elements;
6452 p->constructor_stack = constructor_stack;
6453 p->constructor_range_stack = constructor_range_stack;
6454 p->elements = constructor_elements;
6455 p->spelling = spelling;
6456 p->spelling_base = spelling_base;
6457 p->spelling_size = spelling_size;
6458 p->top_level = constructor_top_level;
6459 p->next = initializer_stack;
6460 initializer_stack = p;
6462 constructor_decl = decl;
6463 constructor_designated = 0;
6464 constructor_top_level = top_level;
6466 if (decl != 0 && decl != error_mark_node)
6468 require_constant_value = TREE_STATIC (decl);
6469 require_constant_elements
6470 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6471 /* For a scalar, you can always use any value to initialize,
6472 even within braces. */
6473 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6474 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6475 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6476 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6477 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6479 else
6481 require_constant_value = 0;
6482 require_constant_elements = 0;
6483 locus = _("(anonymous)");
6486 constructor_stack = 0;
6487 constructor_range_stack = 0;
6489 missing_braces_mentioned = 0;
6491 spelling_base = 0;
6492 spelling_size = 0;
6493 RESTORE_SPELLING_DEPTH (0);
6495 if (locus)
6496 push_string (locus);
6499 void
6500 finish_init (void)
6502 struct initializer_stack *p = initializer_stack;
6504 /* Free the whole constructor stack of this initializer. */
6505 while (constructor_stack)
6507 struct constructor_stack *q = constructor_stack;
6508 constructor_stack = q->next;
6509 free (q);
6512 gcc_assert (!constructor_range_stack);
6514 /* Pop back to the data of the outer initializer (if any). */
6515 free (spelling_base);
6517 constructor_decl = p->decl;
6518 require_constant_value = p->require_constant_value;
6519 require_constant_elements = p->require_constant_elements;
6520 constructor_stack = p->constructor_stack;
6521 constructor_range_stack = p->constructor_range_stack;
6522 constructor_elements = p->elements;
6523 spelling = p->spelling;
6524 spelling_base = p->spelling_base;
6525 spelling_size = p->spelling_size;
6526 constructor_top_level = p->top_level;
6527 initializer_stack = p->next;
6528 free (p);
6531 /* Call here when we see the initializer is surrounded by braces.
6532 This is instead of a call to push_init_level;
6533 it is matched by a call to pop_init_level.
6535 TYPE is the type to initialize, for a constructor expression.
6536 For an initializer for a decl, TYPE is zero. */
6538 void
6539 really_start_incremental_init (tree type)
6541 struct constructor_stack *p = XNEW (struct constructor_stack);
6543 if (type == 0)
6544 type = TREE_TYPE (constructor_decl);
6546 if (TREE_CODE (type) == VECTOR_TYPE
6547 && TYPE_VECTOR_OPAQUE (type))
6548 error ("opaque vector types cannot be initialized");
6550 p->type = constructor_type;
6551 p->fields = constructor_fields;
6552 p->index = constructor_index;
6553 p->max_index = constructor_max_index;
6554 p->unfilled_index = constructor_unfilled_index;
6555 p->unfilled_fields = constructor_unfilled_fields;
6556 p->bit_index = constructor_bit_index;
6557 p->elements = constructor_elements;
6558 p->constant = constructor_constant;
6559 p->simple = constructor_simple;
6560 p->nonconst = constructor_nonconst;
6561 p->erroneous = constructor_erroneous;
6562 p->pending_elts = constructor_pending_elts;
6563 p->depth = constructor_depth;
6564 p->replacement_value.value = 0;
6565 p->replacement_value.original_code = ERROR_MARK;
6566 p->replacement_value.original_type = NULL;
6567 p->implicit = 0;
6568 p->range_stack = 0;
6569 p->outer = 0;
6570 p->incremental = constructor_incremental;
6571 p->designated = constructor_designated;
6572 p->next = 0;
6573 constructor_stack = p;
6575 constructor_constant = 1;
6576 constructor_simple = 1;
6577 constructor_nonconst = 0;
6578 constructor_depth = SPELLING_DEPTH ();
6579 constructor_elements = 0;
6580 constructor_pending_elts = 0;
6581 constructor_type = type;
6582 constructor_incremental = 1;
6583 constructor_designated = 0;
6584 designator_depth = 0;
6585 designator_erroneous = 0;
6587 if (TREE_CODE (constructor_type) == RECORD_TYPE
6588 || TREE_CODE (constructor_type) == UNION_TYPE)
6590 constructor_fields = TYPE_FIELDS (constructor_type);
6591 /* Skip any nameless bit fields at the beginning. */
6592 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6593 && DECL_NAME (constructor_fields) == 0)
6594 constructor_fields = DECL_CHAIN (constructor_fields);
6596 constructor_unfilled_fields = constructor_fields;
6597 constructor_bit_index = bitsize_zero_node;
6599 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6601 if (TYPE_DOMAIN (constructor_type))
6603 constructor_max_index
6604 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6606 /* Detect non-empty initializations of zero-length arrays. */
6607 if (constructor_max_index == NULL_TREE
6608 && TYPE_SIZE (constructor_type))
6609 constructor_max_index = integer_minus_one_node;
6611 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6612 to initialize VLAs will cause a proper error; avoid tree
6613 checking errors as well by setting a safe value. */
6614 if (constructor_max_index
6615 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6616 constructor_max_index = integer_minus_one_node;
6618 constructor_index
6619 = convert (bitsizetype,
6620 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6622 else
6624 constructor_index = bitsize_zero_node;
6625 constructor_max_index = NULL_TREE;
6628 constructor_unfilled_index = constructor_index;
6630 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6632 /* Vectors are like simple fixed-size arrays. */
6633 constructor_max_index =
6634 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6635 constructor_index = bitsize_zero_node;
6636 constructor_unfilled_index = constructor_index;
6638 else
6640 /* Handle the case of int x = {5}; */
6641 constructor_fields = constructor_type;
6642 constructor_unfilled_fields = constructor_type;
6646 /* Push down into a subobject, for initialization.
6647 If this is for an explicit set of braces, IMPLICIT is 0.
6648 If it is because the next element belongs at a lower level,
6649 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6651 void
6652 push_init_level (int implicit, struct obstack * braced_init_obstack)
6654 struct constructor_stack *p;
6655 tree value = NULL_TREE;
6657 /* If we've exhausted any levels that didn't have braces,
6658 pop them now. If implicit == 1, this will have been done in
6659 process_init_element; do not repeat it here because in the case
6660 of excess initializers for an empty aggregate this leads to an
6661 infinite cycle of popping a level and immediately recreating
6662 it. */
6663 if (implicit != 1)
6665 while (constructor_stack->implicit)
6667 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6668 || TREE_CODE (constructor_type) == UNION_TYPE)
6669 && constructor_fields == 0)
6670 process_init_element (pop_init_level (1, braced_init_obstack),
6671 true, braced_init_obstack);
6672 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6673 && constructor_max_index
6674 && tree_int_cst_lt (constructor_max_index,
6675 constructor_index))
6676 process_init_element (pop_init_level (1, braced_init_obstack),
6677 true, braced_init_obstack);
6678 else
6679 break;
6683 /* Unless this is an explicit brace, we need to preserve previous
6684 content if any. */
6685 if (implicit)
6687 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6688 || TREE_CODE (constructor_type) == UNION_TYPE)
6689 && constructor_fields)
6690 value = find_init_member (constructor_fields, braced_init_obstack);
6691 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6692 value = find_init_member (constructor_index, braced_init_obstack);
6695 p = XNEW (struct constructor_stack);
6696 p->type = constructor_type;
6697 p->fields = constructor_fields;
6698 p->index = constructor_index;
6699 p->max_index = constructor_max_index;
6700 p->unfilled_index = constructor_unfilled_index;
6701 p->unfilled_fields = constructor_unfilled_fields;
6702 p->bit_index = constructor_bit_index;
6703 p->elements = constructor_elements;
6704 p->constant = constructor_constant;
6705 p->simple = constructor_simple;
6706 p->nonconst = constructor_nonconst;
6707 p->erroneous = constructor_erroneous;
6708 p->pending_elts = constructor_pending_elts;
6709 p->depth = constructor_depth;
6710 p->replacement_value.value = 0;
6711 p->replacement_value.original_code = ERROR_MARK;
6712 p->replacement_value.original_type = NULL;
6713 p->implicit = implicit;
6714 p->outer = 0;
6715 p->incremental = constructor_incremental;
6716 p->designated = constructor_designated;
6717 p->next = constructor_stack;
6718 p->range_stack = 0;
6719 constructor_stack = p;
6721 constructor_constant = 1;
6722 constructor_simple = 1;
6723 constructor_nonconst = 0;
6724 constructor_depth = SPELLING_DEPTH ();
6725 constructor_elements = 0;
6726 constructor_incremental = 1;
6727 constructor_designated = 0;
6728 constructor_pending_elts = 0;
6729 if (!implicit)
6731 p->range_stack = constructor_range_stack;
6732 constructor_range_stack = 0;
6733 designator_depth = 0;
6734 designator_erroneous = 0;
6737 /* Don't die if an entire brace-pair level is superfluous
6738 in the containing level. */
6739 if (constructor_type == 0)
6741 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6742 || TREE_CODE (constructor_type) == UNION_TYPE)
6744 /* Don't die if there are extra init elts at the end. */
6745 if (constructor_fields == 0)
6746 constructor_type = 0;
6747 else
6749 constructor_type = TREE_TYPE (constructor_fields);
6750 push_member_name (constructor_fields);
6751 constructor_depth++;
6754 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6756 constructor_type = TREE_TYPE (constructor_type);
6757 push_array_bounds (tree_low_cst (constructor_index, 1));
6758 constructor_depth++;
6761 if (constructor_type == 0)
6763 error_init ("extra brace group at end of initializer");
6764 constructor_fields = 0;
6765 constructor_unfilled_fields = 0;
6766 return;
6769 if (value && TREE_CODE (value) == CONSTRUCTOR)
6771 constructor_constant = TREE_CONSTANT (value);
6772 constructor_simple = TREE_STATIC (value);
6773 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6774 constructor_elements = CONSTRUCTOR_ELTS (value);
6775 if (!VEC_empty (constructor_elt, constructor_elements)
6776 && (TREE_CODE (constructor_type) == RECORD_TYPE
6777 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6778 set_nonincremental_init (braced_init_obstack);
6781 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6783 missing_braces_mentioned = 1;
6784 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6787 if (TREE_CODE (constructor_type) == RECORD_TYPE
6788 || TREE_CODE (constructor_type) == UNION_TYPE)
6790 constructor_fields = TYPE_FIELDS (constructor_type);
6791 /* Skip any nameless bit fields at the beginning. */
6792 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6793 && DECL_NAME (constructor_fields) == 0)
6794 constructor_fields = DECL_CHAIN (constructor_fields);
6796 constructor_unfilled_fields = constructor_fields;
6797 constructor_bit_index = bitsize_zero_node;
6799 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6801 /* Vectors are like simple fixed-size arrays. */
6802 constructor_max_index =
6803 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6804 constructor_index = bitsize_int (0);
6805 constructor_unfilled_index = constructor_index;
6807 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6809 if (TYPE_DOMAIN (constructor_type))
6811 constructor_max_index
6812 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6814 /* Detect non-empty initializations of zero-length arrays. */
6815 if (constructor_max_index == NULL_TREE
6816 && TYPE_SIZE (constructor_type))
6817 constructor_max_index = integer_minus_one_node;
6819 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6820 to initialize VLAs will cause a proper error; avoid tree
6821 checking errors as well by setting a safe value. */
6822 if (constructor_max_index
6823 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6824 constructor_max_index = integer_minus_one_node;
6826 constructor_index
6827 = convert (bitsizetype,
6828 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6830 else
6831 constructor_index = bitsize_zero_node;
6833 constructor_unfilled_index = constructor_index;
6834 if (value && TREE_CODE (value) == STRING_CST)
6836 /* We need to split the char/wchar array into individual
6837 characters, so that we don't have to special case it
6838 everywhere. */
6839 set_nonincremental_init_from_string (value, braced_init_obstack);
6842 else
6844 if (constructor_type != error_mark_node)
6845 warning_init (0, "braces around scalar initializer");
6846 constructor_fields = constructor_type;
6847 constructor_unfilled_fields = constructor_type;
6851 /* At the end of an implicit or explicit brace level,
6852 finish up that level of constructor. If a single expression
6853 with redundant braces initialized that level, return the
6854 c_expr structure for that expression. Otherwise, the original_code
6855 element is set to ERROR_MARK.
6856 If we were outputting the elements as they are read, return 0 as the value
6857 from inner levels (process_init_element ignores that),
6858 but return error_mark_node as the value from the outermost level
6859 (that's what we want to put in DECL_INITIAL).
6860 Otherwise, return a CONSTRUCTOR expression as the value. */
6862 struct c_expr
6863 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6865 struct constructor_stack *p;
6866 struct c_expr ret;
6867 ret.value = 0;
6868 ret.original_code = ERROR_MARK;
6869 ret.original_type = NULL;
6871 if (implicit == 0)
6873 /* When we come to an explicit close brace,
6874 pop any inner levels that didn't have explicit braces. */
6875 while (constructor_stack->implicit)
6877 process_init_element (pop_init_level (1, braced_init_obstack),
6878 true, braced_init_obstack);
6880 gcc_assert (!constructor_range_stack);
6883 /* Now output all pending elements. */
6884 constructor_incremental = 1;
6885 output_pending_init_elements (1, braced_init_obstack);
6887 p = constructor_stack;
6889 /* Error for initializing a flexible array member, or a zero-length
6890 array member in an inappropriate context. */
6891 if (constructor_type && constructor_fields
6892 && TREE_CODE (constructor_type) == ARRAY_TYPE
6893 && TYPE_DOMAIN (constructor_type)
6894 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6896 /* Silently discard empty initializations. The parser will
6897 already have pedwarned for empty brackets. */
6898 if (integer_zerop (constructor_unfilled_index))
6899 constructor_type = NULL_TREE;
6900 else
6902 gcc_assert (!TYPE_SIZE (constructor_type));
6904 if (constructor_depth > 2)
6905 error_init ("initialization of flexible array member in a nested context");
6906 else
6907 pedwarn_init (input_location, OPT_pedantic,
6908 "initialization of a flexible array member");
6910 /* We have already issued an error message for the existence
6911 of a flexible array member not at the end of the structure.
6912 Discard the initializer so that we do not die later. */
6913 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
6914 constructor_type = NULL_TREE;
6918 /* Warn when some struct elements are implicitly initialized to zero. */
6919 if (warn_missing_field_initializers
6920 && constructor_type
6921 && TREE_CODE (constructor_type) == RECORD_TYPE
6922 && constructor_unfilled_fields)
6924 bool constructor_zeroinit =
6925 (VEC_length (constructor_elt, constructor_elements) == 1
6926 && integer_zerop
6927 (VEC_index (constructor_elt, constructor_elements, 0)->value));
6929 /* Do not warn for flexible array members or zero-length arrays. */
6930 while (constructor_unfilled_fields
6931 && (!DECL_SIZE (constructor_unfilled_fields)
6932 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6933 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
6935 if (constructor_unfilled_fields
6936 /* Do not warn if this level of the initializer uses member
6937 designators; it is likely to be deliberate. */
6938 && !constructor_designated
6939 /* Do not warn about initializing with ` = {0}'. */
6940 && !constructor_zeroinit)
6942 push_member_name (constructor_unfilled_fields);
6943 warning_init (OPT_Wmissing_field_initializers,
6944 "missing initializer");
6945 RESTORE_SPELLING_DEPTH (constructor_depth);
6949 /* Pad out the end of the structure. */
6950 if (p->replacement_value.value)
6951 /* If this closes a superfluous brace pair,
6952 just pass out the element between them. */
6953 ret = p->replacement_value;
6954 else if (constructor_type == 0)
6956 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6957 && TREE_CODE (constructor_type) != UNION_TYPE
6958 && TREE_CODE (constructor_type) != ARRAY_TYPE
6959 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6961 /* A nonincremental scalar initializer--just return
6962 the element, after verifying there is just one. */
6963 if (VEC_empty (constructor_elt,constructor_elements))
6965 if (!constructor_erroneous)
6966 error_init ("empty scalar initializer");
6967 ret.value = error_mark_node;
6969 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6971 error_init ("extra elements in scalar initializer");
6972 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6974 else
6975 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6977 else
6979 if (constructor_erroneous)
6980 ret.value = error_mark_node;
6981 else
6983 ret.value = build_constructor (constructor_type,
6984 constructor_elements);
6985 if (constructor_constant)
6986 TREE_CONSTANT (ret.value) = 1;
6987 if (constructor_constant && constructor_simple)
6988 TREE_STATIC (ret.value) = 1;
6989 if (constructor_nonconst)
6990 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6994 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6996 if (constructor_nonconst)
6997 ret.original_code = C_MAYBE_CONST_EXPR;
6998 else if (ret.original_code == C_MAYBE_CONST_EXPR)
6999 ret.original_code = ERROR_MARK;
7002 constructor_type = p->type;
7003 constructor_fields = p->fields;
7004 constructor_index = p->index;
7005 constructor_max_index = p->max_index;
7006 constructor_unfilled_index = p->unfilled_index;
7007 constructor_unfilled_fields = p->unfilled_fields;
7008 constructor_bit_index = p->bit_index;
7009 constructor_elements = p->elements;
7010 constructor_constant = p->constant;
7011 constructor_simple = p->simple;
7012 constructor_nonconst = p->nonconst;
7013 constructor_erroneous = p->erroneous;
7014 constructor_incremental = p->incremental;
7015 constructor_designated = p->designated;
7016 constructor_pending_elts = p->pending_elts;
7017 constructor_depth = p->depth;
7018 if (!p->implicit)
7019 constructor_range_stack = p->range_stack;
7020 RESTORE_SPELLING_DEPTH (constructor_depth);
7022 constructor_stack = p->next;
7023 free (p);
7025 if (ret.value == 0 && constructor_stack == 0)
7026 ret.value = error_mark_node;
7027 return ret;
7030 /* Common handling for both array range and field name designators.
7031 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7033 static int
7034 set_designator (int array, struct obstack * braced_init_obstack)
7036 tree subtype;
7037 enum tree_code subcode;
7039 /* Don't die if an entire brace-pair level is superfluous
7040 in the containing level. */
7041 if (constructor_type == 0)
7042 return 1;
7044 /* If there were errors in this designator list already, bail out
7045 silently. */
7046 if (designator_erroneous)
7047 return 1;
7049 if (!designator_depth)
7051 gcc_assert (!constructor_range_stack);
7053 /* Designator list starts at the level of closest explicit
7054 braces. */
7055 while (constructor_stack->implicit)
7057 process_init_element (pop_init_level (1, braced_init_obstack),
7058 true, braced_init_obstack);
7060 constructor_designated = 1;
7061 return 0;
7064 switch (TREE_CODE (constructor_type))
7066 case RECORD_TYPE:
7067 case UNION_TYPE:
7068 subtype = TREE_TYPE (constructor_fields);
7069 if (subtype != error_mark_node)
7070 subtype = TYPE_MAIN_VARIANT (subtype);
7071 break;
7072 case ARRAY_TYPE:
7073 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7074 break;
7075 default:
7076 gcc_unreachable ();
7079 subcode = TREE_CODE (subtype);
7080 if (array && subcode != ARRAY_TYPE)
7082 error_init ("array index in non-array initializer");
7083 return 1;
7085 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7087 error_init ("field name not in record or union initializer");
7088 return 1;
7091 constructor_designated = 1;
7092 push_init_level (2, braced_init_obstack);
7093 return 0;
7096 /* If there are range designators in designator list, push a new designator
7097 to constructor_range_stack. RANGE_END is end of such stack range or
7098 NULL_TREE if there is no range designator at this level. */
7100 static void
7101 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7103 struct constructor_range_stack *p;
7105 p = (struct constructor_range_stack *)
7106 obstack_alloc (braced_init_obstack,
7107 sizeof (struct constructor_range_stack));
7108 p->prev = constructor_range_stack;
7109 p->next = 0;
7110 p->fields = constructor_fields;
7111 p->range_start = constructor_index;
7112 p->index = constructor_index;
7113 p->stack = constructor_stack;
7114 p->range_end = range_end;
7115 if (constructor_range_stack)
7116 constructor_range_stack->next = p;
7117 constructor_range_stack = p;
7120 /* Within an array initializer, specify the next index to be initialized.
7121 FIRST is that index. If LAST is nonzero, then initialize a range
7122 of indices, running from FIRST through LAST. */
7124 void
7125 set_init_index (tree first, tree last,
7126 struct obstack * braced_init_obstack)
7128 if (set_designator (1, braced_init_obstack))
7129 return;
7131 designator_erroneous = 1;
7133 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7134 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7136 error_init ("array index in initializer not of integer type");
7137 return;
7140 if (TREE_CODE (first) != INTEGER_CST)
7142 first = c_fully_fold (first, false, NULL);
7143 if (TREE_CODE (first) == INTEGER_CST)
7144 pedwarn_init (input_location, OPT_pedantic,
7145 "array index in initializer is not "
7146 "an integer constant expression");
7149 if (last && TREE_CODE (last) != INTEGER_CST)
7151 last = c_fully_fold (last, false, NULL);
7152 if (TREE_CODE (last) == INTEGER_CST)
7153 pedwarn_init (input_location, OPT_pedantic,
7154 "array index in initializer is not "
7155 "an integer constant expression");
7158 if (TREE_CODE (first) != INTEGER_CST)
7159 error_init ("nonconstant array index in initializer");
7160 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7161 error_init ("nonconstant array index in initializer");
7162 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7163 error_init ("array index in non-array initializer");
7164 else if (tree_int_cst_sgn (first) == -1)
7165 error_init ("array index in initializer exceeds array bounds");
7166 else if (constructor_max_index
7167 && tree_int_cst_lt (constructor_max_index, first))
7168 error_init ("array index in initializer exceeds array bounds");
7169 else
7171 constant_expression_warning (first);
7172 if (last)
7173 constant_expression_warning (last);
7174 constructor_index = convert (bitsizetype, first);
7176 if (last)
7178 if (tree_int_cst_equal (first, last))
7179 last = 0;
7180 else if (tree_int_cst_lt (last, first))
7182 error_init ("empty index range in initializer");
7183 last = 0;
7185 else
7187 last = convert (bitsizetype, last);
7188 if (constructor_max_index != 0
7189 && tree_int_cst_lt (constructor_max_index, last))
7191 error_init ("array index range in initializer exceeds array bounds");
7192 last = 0;
7197 designator_depth++;
7198 designator_erroneous = 0;
7199 if (constructor_range_stack || last)
7200 push_range_stack (last, braced_init_obstack);
7204 /* Within a struct initializer, specify the next field to be initialized. */
7206 void
7207 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7209 tree field;
7211 if (set_designator (0, braced_init_obstack))
7212 return;
7214 designator_erroneous = 1;
7216 if (TREE_CODE (constructor_type) != RECORD_TYPE
7217 && TREE_CODE (constructor_type) != UNION_TYPE)
7219 error_init ("field name not in record or union initializer");
7220 return;
7223 field = lookup_field (constructor_type, fieldname);
7225 if (field == 0)
7226 error ("unknown field %qE specified in initializer", fieldname);
7227 else
7230 constructor_fields = TREE_VALUE (field);
7231 designator_depth++;
7232 designator_erroneous = 0;
7233 if (constructor_range_stack)
7234 push_range_stack (NULL_TREE, braced_init_obstack);
7235 field = TREE_CHAIN (field);
7236 if (field)
7238 if (set_designator (0, braced_init_obstack))
7239 return;
7242 while (field != NULL_TREE);
7245 /* Add a new initializer to the tree of pending initializers. PURPOSE
7246 identifies the initializer, either array index or field in a structure.
7247 VALUE is the value of that index or field. If ORIGTYPE is not
7248 NULL_TREE, it is the original type of VALUE.
7250 IMPLICIT is true if value comes from pop_init_level (1),
7251 the new initializer has been merged with the existing one
7252 and thus no warnings should be emitted about overriding an
7253 existing initializer. */
7255 static void
7256 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7257 struct obstack * braced_init_obstack)
7259 struct init_node *p, **q, *r;
7261 q = &constructor_pending_elts;
7262 p = 0;
7264 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7266 while (*q != 0)
7268 p = *q;
7269 if (tree_int_cst_lt (purpose, p->purpose))
7270 q = &p->left;
7271 else if (tree_int_cst_lt (p->purpose, purpose))
7272 q = &p->right;
7273 else
7275 if (!implicit)
7277 if (TREE_SIDE_EFFECTS (p->value))
7278 warning_init (0, "initialized field with side-effects overwritten");
7279 else if (warn_override_init)
7280 warning_init (OPT_Woverride_init, "initialized field overwritten");
7282 p->value = value;
7283 p->origtype = origtype;
7284 return;
7288 else
7290 tree bitpos;
7292 bitpos = bit_position (purpose);
7293 while (*q != NULL)
7295 p = *q;
7296 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7297 q = &p->left;
7298 else if (p->purpose != purpose)
7299 q = &p->right;
7300 else
7302 if (!implicit)
7304 if (TREE_SIDE_EFFECTS (p->value))
7305 warning_init (0, "initialized field with side-effects overwritten");
7306 else if (warn_override_init)
7307 warning_init (OPT_Woverride_init, "initialized field overwritten");
7309 p->value = value;
7310 p->origtype = origtype;
7311 return;
7316 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7317 sizeof (struct init_node));
7318 r->purpose = purpose;
7319 r->value = value;
7320 r->origtype = origtype;
7322 *q = r;
7323 r->parent = p;
7324 r->left = 0;
7325 r->right = 0;
7326 r->balance = 0;
7328 while (p)
7330 struct init_node *s;
7332 if (r == p->left)
7334 if (p->balance == 0)
7335 p->balance = -1;
7336 else if (p->balance < 0)
7338 if (r->balance < 0)
7340 /* L rotation. */
7341 p->left = r->right;
7342 if (p->left)
7343 p->left->parent = p;
7344 r->right = p;
7346 p->balance = 0;
7347 r->balance = 0;
7349 s = p->parent;
7350 p->parent = r;
7351 r->parent = s;
7352 if (s)
7354 if (s->left == p)
7355 s->left = r;
7356 else
7357 s->right = r;
7359 else
7360 constructor_pending_elts = r;
7362 else
7364 /* LR rotation. */
7365 struct init_node *t = r->right;
7367 r->right = t->left;
7368 if (r->right)
7369 r->right->parent = r;
7370 t->left = r;
7372 p->left = t->right;
7373 if (p->left)
7374 p->left->parent = p;
7375 t->right = p;
7377 p->balance = t->balance < 0;
7378 r->balance = -(t->balance > 0);
7379 t->balance = 0;
7381 s = p->parent;
7382 p->parent = t;
7383 r->parent = t;
7384 t->parent = s;
7385 if (s)
7387 if (s->left == p)
7388 s->left = t;
7389 else
7390 s->right = t;
7392 else
7393 constructor_pending_elts = t;
7395 break;
7397 else
7399 /* p->balance == +1; growth of left side balances the node. */
7400 p->balance = 0;
7401 break;
7404 else /* r == p->right */
7406 if (p->balance == 0)
7407 /* Growth propagation from right side. */
7408 p->balance++;
7409 else if (p->balance > 0)
7411 if (r->balance > 0)
7413 /* R rotation. */
7414 p->right = r->left;
7415 if (p->right)
7416 p->right->parent = p;
7417 r->left = p;
7419 p->balance = 0;
7420 r->balance = 0;
7422 s = p->parent;
7423 p->parent = r;
7424 r->parent = s;
7425 if (s)
7427 if (s->left == p)
7428 s->left = r;
7429 else
7430 s->right = r;
7432 else
7433 constructor_pending_elts = r;
7435 else /* r->balance == -1 */
7437 /* RL rotation */
7438 struct init_node *t = r->left;
7440 r->left = t->right;
7441 if (r->left)
7442 r->left->parent = r;
7443 t->right = r;
7445 p->right = t->left;
7446 if (p->right)
7447 p->right->parent = p;
7448 t->left = p;
7450 r->balance = (t->balance < 0);
7451 p->balance = -(t->balance > 0);
7452 t->balance = 0;
7454 s = p->parent;
7455 p->parent = t;
7456 r->parent = t;
7457 t->parent = s;
7458 if (s)
7460 if (s->left == p)
7461 s->left = t;
7462 else
7463 s->right = t;
7465 else
7466 constructor_pending_elts = t;
7468 break;
7470 else
7472 /* p->balance == -1; growth of right side balances the node. */
7473 p->balance = 0;
7474 break;
7478 r = p;
7479 p = p->parent;
7483 /* Build AVL tree from a sorted chain. */
7485 static void
7486 set_nonincremental_init (struct obstack * braced_init_obstack)
7488 unsigned HOST_WIDE_INT ix;
7489 tree index, value;
7491 if (TREE_CODE (constructor_type) != RECORD_TYPE
7492 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7493 return;
7495 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7497 add_pending_init (index, value, NULL_TREE, false,
7498 braced_init_obstack);
7500 constructor_elements = 0;
7501 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7503 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7504 /* Skip any nameless bit fields at the beginning. */
7505 while (constructor_unfilled_fields != 0
7506 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7507 && DECL_NAME (constructor_unfilled_fields) == 0)
7508 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7511 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7513 if (TYPE_DOMAIN (constructor_type))
7514 constructor_unfilled_index
7515 = convert (bitsizetype,
7516 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7517 else
7518 constructor_unfilled_index = bitsize_zero_node;
7520 constructor_incremental = 0;
7523 /* Build AVL tree from a string constant. */
7525 static void
7526 set_nonincremental_init_from_string (tree str,
7527 struct obstack * braced_init_obstack)
7529 tree value, purpose, type;
7530 HOST_WIDE_INT val[2];
7531 const char *p, *end;
7532 int byte, wchar_bytes, charwidth, bitpos;
7534 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7536 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7537 charwidth = TYPE_PRECISION (char_type_node);
7538 type = TREE_TYPE (constructor_type);
7539 p = TREE_STRING_POINTER (str);
7540 end = p + TREE_STRING_LENGTH (str);
7542 for (purpose = bitsize_zero_node;
7543 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7544 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7546 if (wchar_bytes == 1)
7548 val[1] = (unsigned char) *p++;
7549 val[0] = 0;
7551 else
7553 val[0] = 0;
7554 val[1] = 0;
7555 for (byte = 0; byte < wchar_bytes; byte++)
7557 if (BYTES_BIG_ENDIAN)
7558 bitpos = (wchar_bytes - byte - 1) * charwidth;
7559 else
7560 bitpos = byte * charwidth;
7561 val[bitpos < HOST_BITS_PER_WIDE_INT]
7562 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7563 << (bitpos % HOST_BITS_PER_WIDE_INT);
7567 if (!TYPE_UNSIGNED (type))
7569 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7570 if (bitpos < HOST_BITS_PER_WIDE_INT)
7572 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7574 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7575 val[0] = -1;
7578 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7580 if (val[1] < 0)
7581 val[0] = -1;
7583 else if (val[0] & (((HOST_WIDE_INT) 1)
7584 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7585 val[0] |= ((HOST_WIDE_INT) -1)
7586 << (bitpos - HOST_BITS_PER_WIDE_INT);
7589 value = build_int_cst_wide (type, val[1], val[0]);
7590 add_pending_init (purpose, value, NULL_TREE, false,
7591 braced_init_obstack);
7594 constructor_incremental = 0;
7597 /* Return value of FIELD in pending initializer or zero if the field was
7598 not initialized yet. */
7600 static tree
7601 find_init_member (tree field, struct obstack * braced_init_obstack)
7603 struct init_node *p;
7605 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7607 if (constructor_incremental
7608 && tree_int_cst_lt (field, constructor_unfilled_index))
7609 set_nonincremental_init (braced_init_obstack);
7611 p = constructor_pending_elts;
7612 while (p)
7614 if (tree_int_cst_lt (field, p->purpose))
7615 p = p->left;
7616 else if (tree_int_cst_lt (p->purpose, field))
7617 p = p->right;
7618 else
7619 return p->value;
7622 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7624 tree bitpos = bit_position (field);
7626 if (constructor_incremental
7627 && (!constructor_unfilled_fields
7628 || tree_int_cst_lt (bitpos,
7629 bit_position (constructor_unfilled_fields))))
7630 set_nonincremental_init (braced_init_obstack);
7632 p = constructor_pending_elts;
7633 while (p)
7635 if (field == p->purpose)
7636 return p->value;
7637 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7638 p = p->left;
7639 else
7640 p = p->right;
7643 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7645 if (!VEC_empty (constructor_elt, constructor_elements)
7646 && (VEC_last (constructor_elt, constructor_elements)->index
7647 == field))
7648 return VEC_last (constructor_elt, constructor_elements)->value;
7650 return 0;
7653 /* "Output" the next constructor element.
7654 At top level, really output it to assembler code now.
7655 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7656 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7657 TYPE is the data type that the containing data type wants here.
7658 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7659 If VALUE is a string constant, STRICT_STRING is true if it is
7660 unparenthesized or we should not warn here for it being parenthesized.
7661 For other types of VALUE, STRICT_STRING is not used.
7663 PENDING if non-nil means output pending elements that belong
7664 right after this element. (PENDING is normally 1;
7665 it is 0 while outputting pending elements, to avoid recursion.)
7667 IMPLICIT is true if value comes from pop_init_level (1),
7668 the new initializer has been merged with the existing one
7669 and thus no warnings should be emitted about overriding an
7670 existing initializer. */
7672 static void
7673 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7674 tree field, int pending, bool implicit,
7675 struct obstack * braced_init_obstack)
7677 tree semantic_type = NULL_TREE;
7678 constructor_elt *celt;
7679 bool maybe_const = true;
7680 bool npc;
7682 if (type == error_mark_node || value == error_mark_node)
7684 constructor_erroneous = 1;
7685 return;
7687 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7688 && (TREE_CODE (value) == STRING_CST
7689 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7690 && !(TREE_CODE (value) == STRING_CST
7691 && TREE_CODE (type) == ARRAY_TYPE
7692 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7693 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7694 TYPE_MAIN_VARIANT (type)))
7695 value = array_to_pointer_conversion (input_location, value);
7697 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7698 && require_constant_value && !flag_isoc99 && pending)
7700 /* As an extension, allow initializing objects with static storage
7701 duration with compound literals (which are then treated just as
7702 the brace enclosed list they contain). */
7703 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7704 value = DECL_INITIAL (decl);
7707 npc = null_pointer_constant_p (value);
7708 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7710 semantic_type = TREE_TYPE (value);
7711 value = TREE_OPERAND (value, 0);
7713 value = c_fully_fold (value, require_constant_value, &maybe_const);
7715 if (value == error_mark_node)
7716 constructor_erroneous = 1;
7717 else if (!TREE_CONSTANT (value))
7718 constructor_constant = 0;
7719 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7720 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7721 || TREE_CODE (constructor_type) == UNION_TYPE)
7722 && DECL_C_BIT_FIELD (field)
7723 && TREE_CODE (value) != INTEGER_CST))
7724 constructor_simple = 0;
7725 if (!maybe_const)
7726 constructor_nonconst = 1;
7728 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7730 if (require_constant_value)
7732 error_init ("initializer element is not constant");
7733 value = error_mark_node;
7735 else if (require_constant_elements)
7736 pedwarn (input_location, 0,
7737 "initializer element is not computable at load time");
7739 else if (!maybe_const
7740 && (require_constant_value || require_constant_elements))
7741 pedwarn_init (input_location, 0,
7742 "initializer element is not a constant expression");
7744 /* Issue -Wc++-compat warnings about initializing a bitfield with
7745 enum type. */
7746 if (warn_cxx_compat
7747 && field != NULL_TREE
7748 && TREE_CODE (field) == FIELD_DECL
7749 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7750 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7751 != TYPE_MAIN_VARIANT (type))
7752 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7754 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7755 if (checktype != error_mark_node
7756 && (TYPE_MAIN_VARIANT (checktype)
7757 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7758 warning_init (OPT_Wc___compat,
7759 "enum conversion in initialization is invalid in C++");
7762 /* If this field is empty (and not at the end of structure),
7763 don't do anything other than checking the initializer. */
7764 if (field
7765 && (TREE_TYPE (field) == error_mark_node
7766 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7767 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7768 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7769 || DECL_CHAIN (field)))))
7770 return;
7772 if (semantic_type)
7773 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7774 value = digest_init (input_location, type, value, origtype, npc,
7775 strict_string, require_constant_value);
7776 if (value == error_mark_node)
7778 constructor_erroneous = 1;
7779 return;
7781 if (require_constant_value || require_constant_elements)
7782 constant_expression_warning (value);
7784 /* If this element doesn't come next in sequence,
7785 put it on constructor_pending_elts. */
7786 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7787 && (!constructor_incremental
7788 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7790 if (constructor_incremental
7791 && tree_int_cst_lt (field, constructor_unfilled_index))
7792 set_nonincremental_init (braced_init_obstack);
7794 add_pending_init (field, value, origtype, implicit,
7795 braced_init_obstack);
7796 return;
7798 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7799 && (!constructor_incremental
7800 || field != constructor_unfilled_fields))
7802 /* We do this for records but not for unions. In a union,
7803 no matter which field is specified, it can be initialized
7804 right away since it starts at the beginning of the union. */
7805 if (constructor_incremental)
7807 if (!constructor_unfilled_fields)
7808 set_nonincremental_init (braced_init_obstack);
7809 else
7811 tree bitpos, unfillpos;
7813 bitpos = bit_position (field);
7814 unfillpos = bit_position (constructor_unfilled_fields);
7816 if (tree_int_cst_lt (bitpos, unfillpos))
7817 set_nonincremental_init (braced_init_obstack);
7821 add_pending_init (field, value, origtype, implicit,
7822 braced_init_obstack);
7823 return;
7825 else if (TREE_CODE (constructor_type) == UNION_TYPE
7826 && !VEC_empty (constructor_elt, constructor_elements))
7828 if (!implicit)
7830 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7831 constructor_elements)->value))
7832 warning_init (0,
7833 "initialized field with side-effects overwritten");
7834 else if (warn_override_init)
7835 warning_init (OPT_Woverride_init, "initialized field overwritten");
7838 /* We can have just one union field set. */
7839 constructor_elements = 0;
7842 /* Otherwise, output this element either to
7843 constructor_elements or to the assembler file. */
7845 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7846 celt->index = field;
7847 celt->value = value;
7849 /* Advance the variable that indicates sequential elements output. */
7850 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7851 constructor_unfilled_index
7852 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7853 bitsize_one_node);
7854 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7856 constructor_unfilled_fields
7857 = DECL_CHAIN (constructor_unfilled_fields);
7859 /* Skip any nameless bit fields. */
7860 while (constructor_unfilled_fields != 0
7861 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7862 && DECL_NAME (constructor_unfilled_fields) == 0)
7863 constructor_unfilled_fields =
7864 DECL_CHAIN (constructor_unfilled_fields);
7866 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7867 constructor_unfilled_fields = 0;
7869 /* Now output any pending elements which have become next. */
7870 if (pending)
7871 output_pending_init_elements (0, braced_init_obstack);
7874 /* Output any pending elements which have become next.
7875 As we output elements, constructor_unfilled_{fields,index}
7876 advances, which may cause other elements to become next;
7877 if so, they too are output.
7879 If ALL is 0, we return when there are
7880 no more pending elements to output now.
7882 If ALL is 1, we output space as necessary so that
7883 we can output all the pending elements. */
7884 static void
7885 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7887 struct init_node *elt = constructor_pending_elts;
7888 tree next;
7890 retry:
7892 /* Look through the whole pending tree.
7893 If we find an element that should be output now,
7894 output it. Otherwise, set NEXT to the element
7895 that comes first among those still pending. */
7897 next = 0;
7898 while (elt)
7900 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7902 if (tree_int_cst_equal (elt->purpose,
7903 constructor_unfilled_index))
7904 output_init_element (elt->value, elt->origtype, true,
7905 TREE_TYPE (constructor_type),
7906 constructor_unfilled_index, 0, false,
7907 braced_init_obstack);
7908 else if (tree_int_cst_lt (constructor_unfilled_index,
7909 elt->purpose))
7911 /* Advance to the next smaller node. */
7912 if (elt->left)
7913 elt = elt->left;
7914 else
7916 /* We have reached the smallest node bigger than the
7917 current unfilled index. Fill the space first. */
7918 next = elt->purpose;
7919 break;
7922 else
7924 /* Advance to the next bigger node. */
7925 if (elt->right)
7926 elt = elt->right;
7927 else
7929 /* We have reached the biggest node in a subtree. Find
7930 the parent of it, which is the next bigger node. */
7931 while (elt->parent && elt->parent->right == elt)
7932 elt = elt->parent;
7933 elt = elt->parent;
7934 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7935 elt->purpose))
7937 next = elt->purpose;
7938 break;
7943 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7944 || TREE_CODE (constructor_type) == UNION_TYPE)
7946 tree ctor_unfilled_bitpos, elt_bitpos;
7948 /* If the current record is complete we are done. */
7949 if (constructor_unfilled_fields == 0)
7950 break;
7952 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7953 elt_bitpos = bit_position (elt->purpose);
7954 /* We can't compare fields here because there might be empty
7955 fields in between. */
7956 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7958 constructor_unfilled_fields = elt->purpose;
7959 output_init_element (elt->value, elt->origtype, true,
7960 TREE_TYPE (elt->purpose),
7961 elt->purpose, 0, false,
7962 braced_init_obstack);
7964 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7966 /* Advance to the next smaller node. */
7967 if (elt->left)
7968 elt = elt->left;
7969 else
7971 /* We have reached the smallest node bigger than the
7972 current unfilled field. Fill the space first. */
7973 next = elt->purpose;
7974 break;
7977 else
7979 /* Advance to the next bigger node. */
7980 if (elt->right)
7981 elt = elt->right;
7982 else
7984 /* We have reached the biggest node in a subtree. Find
7985 the parent of it, which is the next bigger node. */
7986 while (elt->parent && elt->parent->right == elt)
7987 elt = elt->parent;
7988 elt = elt->parent;
7989 if (elt
7990 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7991 bit_position (elt->purpose))))
7993 next = elt->purpose;
7994 break;
8001 /* Ordinarily return, but not if we want to output all
8002 and there are elements left. */
8003 if (!(all && next != 0))
8004 return;
8006 /* If it's not incremental, just skip over the gap, so that after
8007 jumping to retry we will output the next successive element. */
8008 if (TREE_CODE (constructor_type) == RECORD_TYPE
8009 || TREE_CODE (constructor_type) == UNION_TYPE)
8010 constructor_unfilled_fields = next;
8011 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8012 constructor_unfilled_index = next;
8014 /* ELT now points to the node in the pending tree with the next
8015 initializer to output. */
8016 goto retry;
8019 /* Add one non-braced element to the current constructor level.
8020 This adjusts the current position within the constructor's type.
8021 This may also start or terminate implicit levels
8022 to handle a partly-braced initializer.
8024 Once this has found the correct level for the new element,
8025 it calls output_init_element.
8027 IMPLICIT is true if value comes from pop_init_level (1),
8028 the new initializer has been merged with the existing one
8029 and thus no warnings should be emitted about overriding an
8030 existing initializer. */
8032 void
8033 process_init_element (struct c_expr value, bool implicit,
8034 struct obstack * braced_init_obstack)
8036 tree orig_value = value.value;
8037 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8038 bool strict_string = value.original_code == STRING_CST;
8040 designator_depth = 0;
8041 designator_erroneous = 0;
8043 /* Handle superfluous braces around string cst as in
8044 char x[] = {"foo"}; */
8045 if (string_flag
8046 && constructor_type
8047 && TREE_CODE (constructor_type) == ARRAY_TYPE
8048 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8049 && integer_zerop (constructor_unfilled_index))
8051 if (constructor_stack->replacement_value.value)
8052 error_init ("excess elements in char array initializer");
8053 constructor_stack->replacement_value = value;
8054 return;
8057 if (constructor_stack->replacement_value.value != 0)
8059 error_init ("excess elements in struct initializer");
8060 return;
8063 /* Ignore elements of a brace group if it is entirely superfluous
8064 and has already been diagnosed. */
8065 if (constructor_type == 0)
8066 return;
8068 /* If we've exhausted any levels that didn't have braces,
8069 pop them now. */
8070 while (constructor_stack->implicit)
8072 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8073 || TREE_CODE (constructor_type) == UNION_TYPE)
8074 && constructor_fields == 0)
8075 process_init_element (pop_init_level (1, braced_init_obstack),
8076 true, braced_init_obstack);
8077 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8078 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8079 && (constructor_max_index == 0
8080 || tree_int_cst_lt (constructor_max_index,
8081 constructor_index)))
8082 process_init_element (pop_init_level (1, braced_init_obstack),
8083 true, braced_init_obstack);
8084 else
8085 break;
8088 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8089 if (constructor_range_stack)
8091 /* If value is a compound literal and we'll be just using its
8092 content, don't put it into a SAVE_EXPR. */
8093 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8094 || !require_constant_value
8095 || flag_isoc99)
8097 tree semantic_type = NULL_TREE;
8098 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8100 semantic_type = TREE_TYPE (value.value);
8101 value.value = TREE_OPERAND (value.value, 0);
8103 value.value = c_save_expr (value.value);
8104 if (semantic_type)
8105 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8106 value.value);
8110 while (1)
8112 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8114 tree fieldtype;
8115 enum tree_code fieldcode;
8117 if (constructor_fields == 0)
8119 pedwarn_init (input_location, 0,
8120 "excess elements in struct initializer");
8121 break;
8124 fieldtype = TREE_TYPE (constructor_fields);
8125 if (fieldtype != error_mark_node)
8126 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8127 fieldcode = TREE_CODE (fieldtype);
8129 /* Error for non-static initialization of a flexible array member. */
8130 if (fieldcode == ARRAY_TYPE
8131 && !require_constant_value
8132 && TYPE_SIZE (fieldtype) == NULL_TREE
8133 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8135 error_init ("non-static initialization of a flexible array member");
8136 break;
8139 /* Accept a string constant to initialize a subarray. */
8140 if (value.value != 0
8141 && fieldcode == ARRAY_TYPE
8142 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8143 && string_flag)
8144 value.value = orig_value;
8145 /* Otherwise, if we have come to a subaggregate,
8146 and we don't have an element of its type, push into it. */
8147 else if (value.value != 0
8148 && value.value != error_mark_node
8149 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8150 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8151 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8153 push_init_level (1, braced_init_obstack);
8154 continue;
8157 if (value.value)
8159 push_member_name (constructor_fields);
8160 output_init_element (value.value, value.original_type,
8161 strict_string, fieldtype,
8162 constructor_fields, 1, implicit,
8163 braced_init_obstack);
8164 RESTORE_SPELLING_DEPTH (constructor_depth);
8166 else
8167 /* Do the bookkeeping for an element that was
8168 directly output as a constructor. */
8170 /* For a record, keep track of end position of last field. */
8171 if (DECL_SIZE (constructor_fields))
8172 constructor_bit_index
8173 = size_binop_loc (input_location, PLUS_EXPR,
8174 bit_position (constructor_fields),
8175 DECL_SIZE (constructor_fields));
8177 /* If the current field was the first one not yet written out,
8178 it isn't now, so update. */
8179 if (constructor_unfilled_fields == constructor_fields)
8181 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8182 /* Skip any nameless bit fields. */
8183 while (constructor_unfilled_fields != 0
8184 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8185 && DECL_NAME (constructor_unfilled_fields) == 0)
8186 constructor_unfilled_fields =
8187 DECL_CHAIN (constructor_unfilled_fields);
8191 constructor_fields = DECL_CHAIN (constructor_fields);
8192 /* Skip any nameless bit fields at the beginning. */
8193 while (constructor_fields != 0
8194 && DECL_C_BIT_FIELD (constructor_fields)
8195 && DECL_NAME (constructor_fields) == 0)
8196 constructor_fields = DECL_CHAIN (constructor_fields);
8198 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8200 tree fieldtype;
8201 enum tree_code fieldcode;
8203 if (constructor_fields == 0)
8205 pedwarn_init (input_location, 0,
8206 "excess elements in union initializer");
8207 break;
8210 fieldtype = TREE_TYPE (constructor_fields);
8211 if (fieldtype != error_mark_node)
8212 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8213 fieldcode = TREE_CODE (fieldtype);
8215 /* Warn that traditional C rejects initialization of unions.
8216 We skip the warning if the value is zero. This is done
8217 under the assumption that the zero initializer in user
8218 code appears conditioned on e.g. __STDC__ to avoid
8219 "missing initializer" warnings and relies on default
8220 initialization to zero in the traditional C case.
8221 We also skip the warning if the initializer is designated,
8222 again on the assumption that this must be conditional on
8223 __STDC__ anyway (and we've already complained about the
8224 member-designator already). */
8225 if (!in_system_header && !constructor_designated
8226 && !(value.value && (integer_zerop (value.value)
8227 || real_zerop (value.value))))
8228 warning (OPT_Wtraditional, "traditional C rejects initialization "
8229 "of unions");
8231 /* Accept a string constant to initialize a subarray. */
8232 if (value.value != 0
8233 && fieldcode == ARRAY_TYPE
8234 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8235 && string_flag)
8236 value.value = orig_value;
8237 /* Otherwise, if we have come to a subaggregate,
8238 and we don't have an element of its type, push into it. */
8239 else if (value.value != 0
8240 && value.value != error_mark_node
8241 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8242 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8243 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8245 push_init_level (1, braced_init_obstack);
8246 continue;
8249 if (value.value)
8251 push_member_name (constructor_fields);
8252 output_init_element (value.value, value.original_type,
8253 strict_string, fieldtype,
8254 constructor_fields, 1, implicit,
8255 braced_init_obstack);
8256 RESTORE_SPELLING_DEPTH (constructor_depth);
8258 else
8259 /* Do the bookkeeping for an element that was
8260 directly output as a constructor. */
8262 constructor_bit_index = DECL_SIZE (constructor_fields);
8263 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8266 constructor_fields = 0;
8268 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8270 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8271 enum tree_code eltcode = TREE_CODE (elttype);
8273 /* Accept a string constant to initialize a subarray. */
8274 if (value.value != 0
8275 && eltcode == ARRAY_TYPE
8276 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8277 && string_flag)
8278 value.value = orig_value;
8279 /* Otherwise, if we have come to a subaggregate,
8280 and we don't have an element of its type, push into it. */
8281 else if (value.value != 0
8282 && value.value != error_mark_node
8283 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8284 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8285 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8287 push_init_level (1, braced_init_obstack);
8288 continue;
8291 if (constructor_max_index != 0
8292 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8293 || integer_all_onesp (constructor_max_index)))
8295 pedwarn_init (input_location, 0,
8296 "excess elements in array initializer");
8297 break;
8300 /* Now output the actual element. */
8301 if (value.value)
8303 push_array_bounds (tree_low_cst (constructor_index, 1));
8304 output_init_element (value.value, value.original_type,
8305 strict_string, elttype,
8306 constructor_index, 1, implicit,
8307 braced_init_obstack);
8308 RESTORE_SPELLING_DEPTH (constructor_depth);
8311 constructor_index
8312 = size_binop_loc (input_location, PLUS_EXPR,
8313 constructor_index, bitsize_one_node);
8315 if (!value.value)
8316 /* If we are doing the bookkeeping for an element that was
8317 directly output as a constructor, we must update
8318 constructor_unfilled_index. */
8319 constructor_unfilled_index = constructor_index;
8321 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8323 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8325 /* Do a basic check of initializer size. Note that vectors
8326 always have a fixed size derived from their type. */
8327 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8329 pedwarn_init (input_location, 0,
8330 "excess elements in vector initializer");
8331 break;
8334 /* Now output the actual element. */
8335 if (value.value)
8337 if (TREE_CODE (value.value) == VECTOR_CST)
8338 elttype = TYPE_MAIN_VARIANT (constructor_type);
8339 output_init_element (value.value, value.original_type,
8340 strict_string, elttype,
8341 constructor_index, 1, implicit,
8342 braced_init_obstack);
8345 constructor_index
8346 = size_binop_loc (input_location,
8347 PLUS_EXPR, constructor_index, bitsize_one_node);
8349 if (!value.value)
8350 /* If we are doing the bookkeeping for an element that was
8351 directly output as a constructor, we must update
8352 constructor_unfilled_index. */
8353 constructor_unfilled_index = constructor_index;
8356 /* Handle the sole element allowed in a braced initializer
8357 for a scalar variable. */
8358 else if (constructor_type != error_mark_node
8359 && constructor_fields == 0)
8361 pedwarn_init (input_location, 0,
8362 "excess elements in scalar initializer");
8363 break;
8365 else
8367 if (value.value)
8368 output_init_element (value.value, value.original_type,
8369 strict_string, constructor_type,
8370 NULL_TREE, 1, implicit,
8371 braced_init_obstack);
8372 constructor_fields = 0;
8375 /* Handle range initializers either at this level or anywhere higher
8376 in the designator stack. */
8377 if (constructor_range_stack)
8379 struct constructor_range_stack *p, *range_stack;
8380 int finish = 0;
8382 range_stack = constructor_range_stack;
8383 constructor_range_stack = 0;
8384 while (constructor_stack != range_stack->stack)
8386 gcc_assert (constructor_stack->implicit);
8387 process_init_element (pop_init_level (1,
8388 braced_init_obstack),
8389 true, braced_init_obstack);
8391 for (p = range_stack;
8392 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8393 p = p->prev)
8395 gcc_assert (constructor_stack->implicit);
8396 process_init_element (pop_init_level (1, braced_init_obstack),
8397 true, braced_init_obstack);
8400 p->index = size_binop_loc (input_location,
8401 PLUS_EXPR, p->index, bitsize_one_node);
8402 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8403 finish = 1;
8405 while (1)
8407 constructor_index = p->index;
8408 constructor_fields = p->fields;
8409 if (finish && p->range_end && p->index == p->range_start)
8411 finish = 0;
8412 p->prev = 0;
8414 p = p->next;
8415 if (!p)
8416 break;
8417 push_init_level (2, braced_init_obstack);
8418 p->stack = constructor_stack;
8419 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8420 p->index = p->range_start;
8423 if (!finish)
8424 constructor_range_stack = range_stack;
8425 continue;
8428 break;
8431 constructor_range_stack = 0;
8434 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8435 (guaranteed to be 'volatile' or null) and ARGS (represented using
8436 an ASM_EXPR node). */
8437 tree
8438 build_asm_stmt (tree cv_qualifier, tree args)
8440 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8441 ASM_VOLATILE_P (args) = 1;
8442 return add_stmt (args);
8445 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8446 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8447 SIMPLE indicates whether there was anything at all after the
8448 string in the asm expression -- asm("blah") and asm("blah" : )
8449 are subtly different. We use a ASM_EXPR node to represent this. */
8450 tree
8451 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8452 tree clobbers, tree labels, bool simple)
8454 tree tail;
8455 tree args;
8456 int i;
8457 const char *constraint;
8458 const char **oconstraints;
8459 bool allows_mem, allows_reg, is_inout;
8460 int ninputs, noutputs;
8462 ninputs = list_length (inputs);
8463 noutputs = list_length (outputs);
8464 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8466 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8468 /* Remove output conversions that change the type but not the mode. */
8469 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8471 tree output = TREE_VALUE (tail);
8473 /* ??? Really, this should not be here. Users should be using a
8474 proper lvalue, dammit. But there's a long history of using casts
8475 in the output operands. In cases like longlong.h, this becomes a
8476 primitive form of typechecking -- if the cast can be removed, then
8477 the output operand had a type of the proper width; otherwise we'll
8478 get an error. Gross, but ... */
8479 STRIP_NOPS (output);
8481 if (!lvalue_or_else (loc, output, lv_asm))
8482 output = error_mark_node;
8484 if (output != error_mark_node
8485 && (TREE_READONLY (output)
8486 || TYPE_READONLY (TREE_TYPE (output))
8487 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8488 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8489 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8490 readonly_error (output, lv_asm);
8492 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8493 oconstraints[i] = constraint;
8495 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8496 &allows_mem, &allows_reg, &is_inout))
8498 /* If the operand is going to end up in memory,
8499 mark it addressable. */
8500 if (!allows_reg && !c_mark_addressable (output))
8501 output = error_mark_node;
8502 if (!(!allows_reg && allows_mem)
8503 && output != error_mark_node
8504 && VOID_TYPE_P (TREE_TYPE (output)))
8506 error_at (loc, "invalid use of void expression");
8507 output = error_mark_node;
8510 else
8511 output = error_mark_node;
8513 TREE_VALUE (tail) = output;
8516 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8518 tree input;
8520 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8521 input = TREE_VALUE (tail);
8523 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8524 oconstraints, &allows_mem, &allows_reg))
8526 /* If the operand is going to end up in memory,
8527 mark it addressable. */
8528 if (!allows_reg && allows_mem)
8530 /* Strip the nops as we allow this case. FIXME, this really
8531 should be rejected or made deprecated. */
8532 STRIP_NOPS (input);
8533 if (!c_mark_addressable (input))
8534 input = error_mark_node;
8536 else if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8538 error_at (loc, "invalid use of void expression");
8539 input = error_mark_node;
8542 else
8543 input = error_mark_node;
8545 TREE_VALUE (tail) = input;
8548 /* ASMs with labels cannot have outputs. This should have been
8549 enforced by the parser. */
8550 gcc_assert (outputs == NULL || labels == NULL);
8552 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8554 /* asm statements without outputs, including simple ones, are treated
8555 as volatile. */
8556 ASM_INPUT_P (args) = simple;
8557 ASM_VOLATILE_P (args) = (noutputs == 0);
8559 return args;
8562 /* Generate a goto statement to LABEL. LOC is the location of the
8563 GOTO. */
8565 tree
8566 c_finish_goto_label (location_t loc, tree label)
8568 tree decl = lookup_label_for_goto (loc, label);
8569 if (!decl)
8570 return NULL_TREE;
8571 TREE_USED (decl) = 1;
8573 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8574 SET_EXPR_LOCATION (t, loc);
8575 return add_stmt (t);
8579 /* Generate a computed goto statement to EXPR. LOC is the location of
8580 the GOTO. */
8582 tree
8583 c_finish_goto_ptr (location_t loc, tree expr)
8585 tree t;
8586 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8587 expr = c_fully_fold (expr, false, NULL);
8588 expr = convert (ptr_type_node, expr);
8589 t = build1 (GOTO_EXPR, void_type_node, expr);
8590 SET_EXPR_LOCATION (t, loc);
8591 return add_stmt (t);
8594 /* Generate a C `return' statement. RETVAL is the expression for what
8595 to return, or a null pointer for `return;' with no value. LOC is
8596 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8597 is the original type of RETVAL. */
8599 tree
8600 c_finish_return (location_t loc, tree retval, tree origtype)
8602 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8603 bool no_warning = false;
8604 bool npc = false;
8606 if (TREE_THIS_VOLATILE (current_function_decl))
8607 warning_at (loc, 0,
8608 "function declared %<noreturn%> has a %<return%> statement");
8610 if (retval)
8612 tree semantic_type = NULL_TREE;
8613 npc = null_pointer_constant_p (retval);
8614 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8616 semantic_type = TREE_TYPE (retval);
8617 retval = TREE_OPERAND (retval, 0);
8619 retval = c_fully_fold (retval, false, NULL);
8620 if (semantic_type)
8621 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8624 if (!retval)
8626 current_function_returns_null = 1;
8627 if ((warn_return_type || flag_isoc99)
8628 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8630 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8631 "%<return%> with no value, in "
8632 "function returning non-void");
8633 no_warning = true;
8636 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8638 current_function_returns_null = 1;
8639 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8640 pedwarn (loc, 0,
8641 "%<return%> with a value, in function returning void");
8642 else
8643 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8644 "%<return%> with expression, in function returning void");
8646 else
8648 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8649 ic_return,
8650 npc, NULL_TREE, NULL_TREE, 0);
8651 tree res = DECL_RESULT (current_function_decl);
8652 tree inner;
8654 current_function_returns_value = 1;
8655 if (t == error_mark_node)
8656 return NULL_TREE;
8658 inner = t = convert (TREE_TYPE (res), t);
8660 /* Strip any conversions, additions, and subtractions, and see if
8661 we are returning the address of a local variable. Warn if so. */
8662 while (1)
8664 switch (TREE_CODE (inner))
8666 CASE_CONVERT:
8667 case NON_LVALUE_EXPR:
8668 case PLUS_EXPR:
8669 case POINTER_PLUS_EXPR:
8670 inner = TREE_OPERAND (inner, 0);
8671 continue;
8673 case MINUS_EXPR:
8674 /* If the second operand of the MINUS_EXPR has a pointer
8675 type (or is converted from it), this may be valid, so
8676 don't give a warning. */
8678 tree op1 = TREE_OPERAND (inner, 1);
8680 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8681 && (CONVERT_EXPR_P (op1)
8682 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8683 op1 = TREE_OPERAND (op1, 0);
8685 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8686 break;
8688 inner = TREE_OPERAND (inner, 0);
8689 continue;
8692 case ADDR_EXPR:
8693 inner = TREE_OPERAND (inner, 0);
8695 while (REFERENCE_CLASS_P (inner)
8696 && TREE_CODE (inner) != INDIRECT_REF)
8697 inner = TREE_OPERAND (inner, 0);
8699 if (DECL_P (inner)
8700 && !DECL_EXTERNAL (inner)
8701 && !TREE_STATIC (inner)
8702 && DECL_CONTEXT (inner) == current_function_decl)
8703 warning_at (loc,
8704 0, "function returns address of local variable");
8705 break;
8707 default:
8708 break;
8711 break;
8714 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8715 SET_EXPR_LOCATION (retval, loc);
8717 if (warn_sequence_point)
8718 verify_sequence_points (retval);
8721 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8722 TREE_NO_WARNING (ret_stmt) |= no_warning;
8723 return add_stmt (ret_stmt);
8726 struct c_switch {
8727 /* The SWITCH_EXPR being built. */
8728 tree switch_expr;
8730 /* The original type of the testing expression, i.e. before the
8731 default conversion is applied. */
8732 tree orig_type;
8734 /* A splay-tree mapping the low element of a case range to the high
8735 element, or NULL_TREE if there is no high element. Used to
8736 determine whether or not a new case label duplicates an old case
8737 label. We need a tree, rather than simply a hash table, because
8738 of the GNU case range extension. */
8739 splay_tree cases;
8741 /* The bindings at the point of the switch. This is used for
8742 warnings crossing decls when branching to a case label. */
8743 struct c_spot_bindings *bindings;
8745 /* The next node on the stack. */
8746 struct c_switch *next;
8749 /* A stack of the currently active switch statements. The innermost
8750 switch statement is on the top of the stack. There is no need to
8751 mark the stack for garbage collection because it is only active
8752 during the processing of the body of a function, and we never
8753 collect at that point. */
8755 struct c_switch *c_switch_stack;
8757 /* Start a C switch statement, testing expression EXP. Return the new
8758 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8759 SWITCH_COND_LOC is the location of the switch's condition. */
8761 tree
8762 c_start_case (location_t switch_loc,
8763 location_t switch_cond_loc,
8764 tree exp)
8766 tree orig_type = error_mark_node;
8767 struct c_switch *cs;
8769 if (exp != error_mark_node)
8771 orig_type = TREE_TYPE (exp);
8773 if (!INTEGRAL_TYPE_P (orig_type))
8775 if (orig_type != error_mark_node)
8777 error_at (switch_cond_loc, "switch quantity not an integer");
8778 orig_type = error_mark_node;
8780 exp = integer_zero_node;
8782 else
8784 tree type = TYPE_MAIN_VARIANT (orig_type);
8786 if (!in_system_header
8787 && (type == long_integer_type_node
8788 || type == long_unsigned_type_node))
8789 warning_at (switch_cond_loc,
8790 OPT_Wtraditional, "%<long%> switch expression not "
8791 "converted to %<int%> in ISO C");
8793 exp = c_fully_fold (exp, false, NULL);
8794 exp = default_conversion (exp);
8796 if (warn_sequence_point)
8797 verify_sequence_points (exp);
8801 /* Add this new SWITCH_EXPR to the stack. */
8802 cs = XNEW (struct c_switch);
8803 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8804 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8805 cs->orig_type = orig_type;
8806 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8807 cs->bindings = c_get_switch_bindings ();
8808 cs->next = c_switch_stack;
8809 c_switch_stack = cs;
8811 return add_stmt (cs->switch_expr);
8814 /* Process a case label at location LOC. */
8816 tree
8817 do_case (location_t loc, tree low_value, tree high_value)
8819 tree label = NULL_TREE;
8821 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8823 low_value = c_fully_fold (low_value, false, NULL);
8824 if (TREE_CODE (low_value) == INTEGER_CST)
8825 pedwarn (input_location, OPT_pedantic,
8826 "case label is not an integer constant expression");
8829 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8831 high_value = c_fully_fold (high_value, false, NULL);
8832 if (TREE_CODE (high_value) == INTEGER_CST)
8833 pedwarn (input_location, OPT_pedantic,
8834 "case label is not an integer constant expression");
8837 if (c_switch_stack == NULL)
8839 if (low_value)
8840 error_at (loc, "case label not within a switch statement");
8841 else
8842 error_at (loc, "%<default%> label not within a switch statement");
8843 return NULL_TREE;
8846 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8847 EXPR_LOCATION (c_switch_stack->switch_expr),
8848 loc))
8849 return NULL_TREE;
8851 label = c_add_case_label (loc, c_switch_stack->cases,
8852 SWITCH_COND (c_switch_stack->switch_expr),
8853 c_switch_stack->orig_type,
8854 low_value, high_value);
8855 if (label == error_mark_node)
8856 label = NULL_TREE;
8857 return label;
8860 /* Finish the switch statement. */
8862 void
8863 c_finish_case (tree body)
8865 struct c_switch *cs = c_switch_stack;
8866 location_t switch_location;
8868 SWITCH_BODY (cs->switch_expr) = body;
8870 /* Emit warnings as needed. */
8871 switch_location = EXPR_LOCATION (cs->switch_expr);
8872 c_do_switch_warnings (cs->cases, switch_location,
8873 TREE_TYPE (cs->switch_expr),
8874 SWITCH_COND (cs->switch_expr));
8876 /* Pop the stack. */
8877 c_switch_stack = cs->next;
8878 splay_tree_delete (cs->cases);
8879 c_release_switch_bindings (cs->bindings);
8880 XDELETE (cs);
8883 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8884 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8885 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8886 statement, and was not surrounded with parenthesis. */
8888 void
8889 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8890 tree else_block, bool nested_if)
8892 tree stmt;
8894 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8895 if (warn_parentheses && nested_if && else_block == NULL)
8897 tree inner_if = then_block;
8899 /* We know from the grammar productions that there is an IF nested
8900 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8901 it might not be exactly THEN_BLOCK, but should be the last
8902 non-container statement within. */
8903 while (1)
8904 switch (TREE_CODE (inner_if))
8906 case COND_EXPR:
8907 goto found;
8908 case BIND_EXPR:
8909 inner_if = BIND_EXPR_BODY (inner_if);
8910 break;
8911 case STATEMENT_LIST:
8912 inner_if = expr_last (then_block);
8913 break;
8914 case TRY_FINALLY_EXPR:
8915 case TRY_CATCH_EXPR:
8916 inner_if = TREE_OPERAND (inner_if, 0);
8917 break;
8918 default:
8919 gcc_unreachable ();
8921 found:
8923 if (COND_EXPR_ELSE (inner_if))
8924 warning_at (if_locus, OPT_Wparentheses,
8925 "suggest explicit braces to avoid ambiguous %<else%>");
8928 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8929 SET_EXPR_LOCATION (stmt, if_locus);
8930 add_stmt (stmt);
8933 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8934 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8935 is false for DO loops. INCR is the FOR increment expression. BODY is
8936 the statement controlled by the loop. BLAB is the break label. CLAB is
8937 the continue label. Everything is allowed to be NULL. */
8939 void
8940 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8941 tree blab, tree clab, bool cond_is_first)
8943 tree entry = NULL, exit = NULL, t;
8945 /* If the condition is zero don't generate a loop construct. */
8946 if (cond && integer_zerop (cond))
8948 if (cond_is_first)
8950 t = build_and_jump (&blab);
8951 SET_EXPR_LOCATION (t, start_locus);
8952 add_stmt (t);
8955 else
8957 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8959 /* If we have an exit condition, then we build an IF with gotos either
8960 out of the loop, or to the top of it. If there's no exit condition,
8961 then we just build a jump back to the top. */
8962 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8964 if (cond && !integer_nonzerop (cond))
8966 /* Canonicalize the loop condition to the end. This means
8967 generating a branch to the loop condition. Reuse the
8968 continue label, if possible. */
8969 if (cond_is_first)
8971 if (incr || !clab)
8973 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8974 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8976 else
8977 t = build1 (GOTO_EXPR, void_type_node, clab);
8978 SET_EXPR_LOCATION (t, start_locus);
8979 add_stmt (t);
8982 t = build_and_jump (&blab);
8983 if (cond_is_first)
8984 exit = fold_build3_loc (start_locus,
8985 COND_EXPR, void_type_node, cond, exit, t);
8986 else
8987 exit = fold_build3_loc (input_location,
8988 COND_EXPR, void_type_node, cond, exit, t);
8991 add_stmt (top);
8994 if (body)
8995 add_stmt (body);
8996 if (clab)
8997 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8998 if (incr)
8999 add_stmt (incr);
9000 if (entry)
9001 add_stmt (entry);
9002 if (exit)
9003 add_stmt (exit);
9004 if (blab)
9005 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9008 tree
9009 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9011 bool skip;
9012 tree label = *label_p;
9014 /* In switch statements break is sometimes stylistically used after
9015 a return statement. This can lead to spurious warnings about
9016 control reaching the end of a non-void function when it is
9017 inlined. Note that we are calling block_may_fallthru with
9018 language specific tree nodes; this works because
9019 block_may_fallthru returns true when given something it does not
9020 understand. */
9021 skip = !block_may_fallthru (cur_stmt_list);
9023 if (!label)
9025 if (!skip)
9026 *label_p = label = create_artificial_label (loc);
9028 else if (TREE_CODE (label) == LABEL_DECL)
9030 else switch (TREE_INT_CST_LOW (label))
9032 case 0:
9033 if (is_break)
9034 error_at (loc, "break statement not within loop or switch");
9035 else
9036 error_at (loc, "continue statement not within a loop");
9037 return NULL_TREE;
9039 case 1:
9040 gcc_assert (is_break);
9041 error_at (loc, "break statement used with OpenMP for loop");
9042 return NULL_TREE;
9044 default:
9045 gcc_unreachable ();
9048 if (skip)
9049 return NULL_TREE;
9051 if (!is_break)
9052 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9054 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9057 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9059 static void
9060 emit_side_effect_warnings (location_t loc, tree expr)
9062 if (expr == error_mark_node)
9064 else if (!TREE_SIDE_EFFECTS (expr))
9066 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9067 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9069 else
9070 warn_if_unused_value (expr, loc);
9073 /* Process an expression as if it were a complete statement. Emit
9074 diagnostics, but do not call ADD_STMT. LOC is the location of the
9075 statement. */
9077 tree
9078 c_process_expr_stmt (location_t loc, tree expr)
9080 tree exprv;
9082 if (!expr)
9083 return NULL_TREE;
9085 expr = c_fully_fold (expr, false, NULL);
9087 if (warn_sequence_point)
9088 verify_sequence_points (expr);
9090 if (TREE_TYPE (expr) != error_mark_node
9091 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9092 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9093 error_at (loc, "expression statement has incomplete type");
9095 /* If we're not processing a statement expression, warn about unused values.
9096 Warnings for statement expressions will be emitted later, once we figure
9097 out which is the result. */
9098 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9099 && warn_unused_value)
9100 emit_side_effect_warnings (loc, expr);
9102 exprv = expr;
9103 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9104 exprv = TREE_OPERAND (exprv, 1);
9105 if (DECL_P (exprv) || handled_component_p (exprv))
9106 mark_exp_read (exprv);
9108 /* If the expression is not of a type to which we cannot assign a line
9109 number, wrap the thing in a no-op NOP_EXPR. */
9110 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9112 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9113 SET_EXPR_LOCATION (expr, loc);
9116 return expr;
9119 /* Emit an expression as a statement. LOC is the location of the
9120 expression. */
9122 tree
9123 c_finish_expr_stmt (location_t loc, tree expr)
9125 if (expr)
9126 return add_stmt (c_process_expr_stmt (loc, expr));
9127 else
9128 return NULL;
9131 /* Do the opposite and emit a statement as an expression. To begin,
9132 create a new binding level and return it. */
9134 tree
9135 c_begin_stmt_expr (void)
9137 tree ret;
9139 /* We must force a BLOCK for this level so that, if it is not expanded
9140 later, there is a way to turn off the entire subtree of blocks that
9141 are contained in it. */
9142 keep_next_level ();
9143 ret = c_begin_compound_stmt (true);
9145 c_bindings_start_stmt_expr (c_switch_stack == NULL
9146 ? NULL
9147 : c_switch_stack->bindings);
9149 /* Mark the current statement list as belonging to a statement list. */
9150 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9152 return ret;
9155 /* LOC is the location of the compound statement to which this body
9156 belongs. */
9158 tree
9159 c_finish_stmt_expr (location_t loc, tree body)
9161 tree last, type, tmp, val;
9162 tree *last_p;
9164 body = c_end_compound_stmt (loc, body, true);
9166 c_bindings_end_stmt_expr (c_switch_stack == NULL
9167 ? NULL
9168 : c_switch_stack->bindings);
9170 /* Locate the last statement in BODY. See c_end_compound_stmt
9171 about always returning a BIND_EXPR. */
9172 last_p = &BIND_EXPR_BODY (body);
9173 last = BIND_EXPR_BODY (body);
9175 continue_searching:
9176 if (TREE_CODE (last) == STATEMENT_LIST)
9178 tree_stmt_iterator i;
9180 /* This can happen with degenerate cases like ({ }). No value. */
9181 if (!TREE_SIDE_EFFECTS (last))
9182 return body;
9184 /* If we're supposed to generate side effects warnings, process
9185 all of the statements except the last. */
9186 if (warn_unused_value)
9188 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9190 location_t tloc;
9191 tree t = tsi_stmt (i);
9193 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9194 emit_side_effect_warnings (tloc, t);
9197 else
9198 i = tsi_last (last);
9199 last_p = tsi_stmt_ptr (i);
9200 last = *last_p;
9203 /* If the end of the list is exception related, then the list was split
9204 by a call to push_cleanup. Continue searching. */
9205 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9206 || TREE_CODE (last) == TRY_CATCH_EXPR)
9208 last_p = &TREE_OPERAND (last, 0);
9209 last = *last_p;
9210 goto continue_searching;
9213 if (last == error_mark_node)
9214 return last;
9216 /* In the case that the BIND_EXPR is not necessary, return the
9217 expression out from inside it. */
9218 if (last == BIND_EXPR_BODY (body)
9219 && BIND_EXPR_VARS (body) == NULL)
9221 /* Even if this looks constant, do not allow it in a constant
9222 expression. */
9223 last = c_wrap_maybe_const (last, true);
9224 /* Do not warn if the return value of a statement expression is
9225 unused. */
9226 TREE_NO_WARNING (last) = 1;
9227 return last;
9230 /* Extract the type of said expression. */
9231 type = TREE_TYPE (last);
9233 /* If we're not returning a value at all, then the BIND_EXPR that
9234 we already have is a fine expression to return. */
9235 if (!type || VOID_TYPE_P (type))
9236 return body;
9238 /* Now that we've located the expression containing the value, it seems
9239 silly to make voidify_wrapper_expr repeat the process. Create a
9240 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9241 tmp = create_tmp_var_raw (type, NULL);
9243 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9244 tree_expr_nonnegative_p giving up immediately. */
9245 val = last;
9246 if (TREE_CODE (val) == NOP_EXPR
9247 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9248 val = TREE_OPERAND (val, 0);
9250 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9251 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9254 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9255 SET_EXPR_LOCATION (t, loc);
9256 return t;
9260 /* Begin and end compound statements. This is as simple as pushing
9261 and popping new statement lists from the tree. */
9263 tree
9264 c_begin_compound_stmt (bool do_scope)
9266 tree stmt = push_stmt_list ();
9267 if (do_scope)
9268 push_scope ();
9269 return stmt;
9272 /* End a compound statement. STMT is the statement. LOC is the
9273 location of the compound statement-- this is usually the location
9274 of the opening brace. */
9276 tree
9277 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9279 tree block = NULL;
9281 if (do_scope)
9283 if (c_dialect_objc ())
9284 objc_clear_super_receiver ();
9285 block = pop_scope ();
9288 stmt = pop_stmt_list (stmt);
9289 stmt = c_build_bind_expr (loc, block, stmt);
9291 /* If this compound statement is nested immediately inside a statement
9292 expression, then force a BIND_EXPR to be created. Otherwise we'll
9293 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9294 STATEMENT_LISTs merge, and thus we can lose track of what statement
9295 was really last. */
9296 if (building_stmt_list_p ()
9297 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9298 && TREE_CODE (stmt) != BIND_EXPR)
9300 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9301 TREE_SIDE_EFFECTS (stmt) = 1;
9302 SET_EXPR_LOCATION (stmt, loc);
9305 return stmt;
9308 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9309 when the current scope is exited. EH_ONLY is true when this is not
9310 meant to apply to normal control flow transfer. */
9312 void
9313 push_cleanup (tree decl, tree cleanup, bool eh_only)
9315 enum tree_code code;
9316 tree stmt, list;
9317 bool stmt_expr;
9319 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9320 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9321 add_stmt (stmt);
9322 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9323 list = push_stmt_list ();
9324 TREE_OPERAND (stmt, 0) = list;
9325 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9328 /* Build a binary-operation expression without default conversions.
9329 CODE is the kind of expression to build.
9330 LOCATION is the operator's location.
9331 This function differs from `build' in several ways:
9332 the data type of the result is computed and recorded in it,
9333 warnings are generated if arg data types are invalid,
9334 special handling for addition and subtraction of pointers is known,
9335 and some optimization is done (operations on narrow ints
9336 are done in the narrower type when that gives the same result).
9337 Constant folding is also done before the result is returned.
9339 Note that the operands will never have enumeral types, or function
9340 or array types, because either they will have the default conversions
9341 performed or they have both just been converted to some other type in which
9342 the arithmetic is to be done. */
9344 tree
9345 build_binary_op (location_t location, enum tree_code code,
9346 tree orig_op0, tree orig_op1, int convert_p)
9348 tree type0, type1, orig_type0, orig_type1;
9349 tree eptype;
9350 enum tree_code code0, code1;
9351 tree op0, op1;
9352 tree ret = error_mark_node;
9353 const char *invalid_op_diag;
9354 bool op0_int_operands, op1_int_operands;
9355 bool int_const, int_const_or_overflow, int_operands;
9357 /* Expression code to give to the expression when it is built.
9358 Normally this is CODE, which is what the caller asked for,
9359 but in some special cases we change it. */
9360 enum tree_code resultcode = code;
9362 /* Data type in which the computation is to be performed.
9363 In the simplest cases this is the common type of the arguments. */
9364 tree result_type = NULL;
9366 /* When the computation is in excess precision, the type of the
9367 final EXCESS_PRECISION_EXPR. */
9368 tree semantic_result_type = NULL;
9370 /* Nonzero means operands have already been type-converted
9371 in whatever way is necessary.
9372 Zero means they need to be converted to RESULT_TYPE. */
9373 int converted = 0;
9375 /* Nonzero means create the expression with this type, rather than
9376 RESULT_TYPE. */
9377 tree build_type = 0;
9379 /* Nonzero means after finally constructing the expression
9380 convert it to this type. */
9381 tree final_type = 0;
9383 /* Nonzero if this is an operation like MIN or MAX which can
9384 safely be computed in short if both args are promoted shorts.
9385 Also implies COMMON.
9386 -1 indicates a bitwise operation; this makes a difference
9387 in the exact conditions for when it is safe to do the operation
9388 in a narrower mode. */
9389 int shorten = 0;
9391 /* Nonzero if this is a comparison operation;
9392 if both args are promoted shorts, compare the original shorts.
9393 Also implies COMMON. */
9394 int short_compare = 0;
9396 /* Nonzero if this is a right-shift operation, which can be computed on the
9397 original short and then promoted if the operand is a promoted short. */
9398 int short_shift = 0;
9400 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9401 int common = 0;
9403 /* True means types are compatible as far as ObjC is concerned. */
9404 bool objc_ok;
9406 /* True means this is an arithmetic operation that may need excess
9407 precision. */
9408 bool may_need_excess_precision;
9410 /* True means this is a boolean operation that converts both its
9411 operands to truth-values. */
9412 bool boolean_op = false;
9414 if (location == UNKNOWN_LOCATION)
9415 location = input_location;
9417 op0 = orig_op0;
9418 op1 = orig_op1;
9420 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9421 if (op0_int_operands)
9422 op0 = remove_c_maybe_const_expr (op0);
9423 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9424 if (op1_int_operands)
9425 op1 = remove_c_maybe_const_expr (op1);
9426 int_operands = (op0_int_operands && op1_int_operands);
9427 if (int_operands)
9429 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9430 && TREE_CODE (orig_op1) == INTEGER_CST);
9431 int_const = (int_const_or_overflow
9432 && !TREE_OVERFLOW (orig_op0)
9433 && !TREE_OVERFLOW (orig_op1));
9435 else
9436 int_const = int_const_or_overflow = false;
9438 if (convert_p)
9440 op0 = default_conversion (op0);
9441 op1 = default_conversion (op1);
9444 orig_type0 = type0 = TREE_TYPE (op0);
9445 orig_type1 = type1 = TREE_TYPE (op1);
9447 /* The expression codes of the data types of the arguments tell us
9448 whether the arguments are integers, floating, pointers, etc. */
9449 code0 = TREE_CODE (type0);
9450 code1 = TREE_CODE (type1);
9452 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9453 STRIP_TYPE_NOPS (op0);
9454 STRIP_TYPE_NOPS (op1);
9456 /* If an error was already reported for one of the arguments,
9457 avoid reporting another error. */
9459 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9460 return error_mark_node;
9462 if ((invalid_op_diag
9463 = targetm.invalid_binary_op (code, type0, type1)))
9465 error_at (location, invalid_op_diag);
9466 return error_mark_node;
9469 switch (code)
9471 case PLUS_EXPR:
9472 case MINUS_EXPR:
9473 case MULT_EXPR:
9474 case TRUNC_DIV_EXPR:
9475 case CEIL_DIV_EXPR:
9476 case FLOOR_DIV_EXPR:
9477 case ROUND_DIV_EXPR:
9478 case EXACT_DIV_EXPR:
9479 may_need_excess_precision = true;
9480 break;
9481 default:
9482 may_need_excess_precision = false;
9483 break;
9485 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9487 op0 = TREE_OPERAND (op0, 0);
9488 type0 = TREE_TYPE (op0);
9490 else if (may_need_excess_precision
9491 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9493 type0 = eptype;
9494 op0 = convert (eptype, op0);
9496 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9498 op1 = TREE_OPERAND (op1, 0);
9499 type1 = TREE_TYPE (op1);
9501 else if (may_need_excess_precision
9502 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9504 type1 = eptype;
9505 op1 = convert (eptype, op1);
9508 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9510 switch (code)
9512 case PLUS_EXPR:
9513 /* Handle the pointer + int case. */
9514 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9516 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9517 goto return_build_binary_op;
9519 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9521 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9522 goto return_build_binary_op;
9524 else
9525 common = 1;
9526 break;
9528 case MINUS_EXPR:
9529 /* Subtraction of two similar pointers.
9530 We must subtract them as integers, then divide by object size. */
9531 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9532 && comp_target_types (location, type0, type1))
9534 ret = pointer_diff (location, op0, op1);
9535 goto return_build_binary_op;
9537 /* Handle pointer minus int. Just like pointer plus int. */
9538 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9540 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9541 goto return_build_binary_op;
9543 else
9544 common = 1;
9545 break;
9547 case MULT_EXPR:
9548 common = 1;
9549 break;
9551 case TRUNC_DIV_EXPR:
9552 case CEIL_DIV_EXPR:
9553 case FLOOR_DIV_EXPR:
9554 case ROUND_DIV_EXPR:
9555 case EXACT_DIV_EXPR:
9556 warn_for_div_by_zero (location, op1);
9558 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9559 || code0 == FIXED_POINT_TYPE
9560 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9561 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9562 || code1 == FIXED_POINT_TYPE
9563 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9565 enum tree_code tcode0 = code0, tcode1 = code1;
9567 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9568 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9569 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9570 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9572 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9573 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9574 resultcode = RDIV_EXPR;
9575 else
9576 /* Although it would be tempting to shorten always here, that
9577 loses on some targets, since the modulo instruction is
9578 undefined if the quotient can't be represented in the
9579 computation mode. We shorten only if unsigned or if
9580 dividing by something we know != -1. */
9581 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9582 || (TREE_CODE (op1) == INTEGER_CST
9583 && !integer_all_onesp (op1)));
9584 common = 1;
9586 break;
9588 case BIT_AND_EXPR:
9589 case BIT_IOR_EXPR:
9590 case BIT_XOR_EXPR:
9591 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9592 shorten = -1;
9593 /* Allow vector types which are not floating point types. */
9594 else if (code0 == VECTOR_TYPE
9595 && code1 == VECTOR_TYPE
9596 && !VECTOR_FLOAT_TYPE_P (type0)
9597 && !VECTOR_FLOAT_TYPE_P (type1))
9598 common = 1;
9599 break;
9601 case TRUNC_MOD_EXPR:
9602 case FLOOR_MOD_EXPR:
9603 warn_for_div_by_zero (location, op1);
9605 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9606 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9607 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9608 common = 1;
9609 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9611 /* Although it would be tempting to shorten always here, that loses
9612 on some targets, since the modulo instruction is undefined if the
9613 quotient can't be represented in the computation mode. We shorten
9614 only if unsigned or if dividing by something we know != -1. */
9615 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9616 || (TREE_CODE (op1) == INTEGER_CST
9617 && !integer_all_onesp (op1)));
9618 common = 1;
9620 break;
9622 case TRUTH_ANDIF_EXPR:
9623 case TRUTH_ORIF_EXPR:
9624 case TRUTH_AND_EXPR:
9625 case TRUTH_OR_EXPR:
9626 case TRUTH_XOR_EXPR:
9627 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9628 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9629 || code0 == FIXED_POINT_TYPE)
9630 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9631 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9632 || code1 == FIXED_POINT_TYPE))
9634 /* Result of these operations is always an int,
9635 but that does not mean the operands should be
9636 converted to ints! */
9637 result_type = integer_type_node;
9638 op0 = c_common_truthvalue_conversion (location, op0);
9639 op1 = c_common_truthvalue_conversion (location, op1);
9640 converted = 1;
9641 boolean_op = true;
9643 if (code == TRUTH_ANDIF_EXPR)
9645 int_const_or_overflow = (int_operands
9646 && TREE_CODE (orig_op0) == INTEGER_CST
9647 && (op0 == truthvalue_false_node
9648 || TREE_CODE (orig_op1) == INTEGER_CST));
9649 int_const = (int_const_or_overflow
9650 && !TREE_OVERFLOW (orig_op0)
9651 && (op0 == truthvalue_false_node
9652 || !TREE_OVERFLOW (orig_op1)));
9654 else if (code == TRUTH_ORIF_EXPR)
9656 int_const_or_overflow = (int_operands
9657 && TREE_CODE (orig_op0) == INTEGER_CST
9658 && (op0 == truthvalue_true_node
9659 || TREE_CODE (orig_op1) == INTEGER_CST));
9660 int_const = (int_const_or_overflow
9661 && !TREE_OVERFLOW (orig_op0)
9662 && (op0 == truthvalue_true_node
9663 || !TREE_OVERFLOW (orig_op1)));
9665 break;
9667 /* Shift operations: result has same type as first operand;
9668 always convert second operand to int.
9669 Also set SHORT_SHIFT if shifting rightward. */
9671 case RSHIFT_EXPR:
9672 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9673 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9675 result_type = type0;
9676 converted = 1;
9678 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9679 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9680 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9681 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9683 result_type = type0;
9684 converted = 1;
9686 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9687 && code1 == INTEGER_TYPE)
9689 if (TREE_CODE (op1) == INTEGER_CST)
9691 if (tree_int_cst_sgn (op1) < 0)
9693 int_const = false;
9694 if (c_inhibit_evaluation_warnings == 0)
9695 warning (0, "right shift count is negative");
9697 else
9699 if (!integer_zerop (op1))
9700 short_shift = 1;
9702 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9704 int_const = false;
9705 if (c_inhibit_evaluation_warnings == 0)
9706 warning (0, "right shift count >= width of type");
9711 /* Use the type of the value to be shifted. */
9712 result_type = type0;
9713 /* Convert the non vector shift-count to an integer, regardless
9714 of size of value being shifted. */
9715 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9716 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9717 op1 = convert (integer_type_node, op1);
9718 /* Avoid converting op1 to result_type later. */
9719 converted = 1;
9721 break;
9723 case LSHIFT_EXPR:
9724 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9725 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9727 result_type = type0;
9728 converted = 1;
9730 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9731 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9732 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9733 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9735 result_type = type0;
9736 converted = 1;
9738 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9739 && code1 == INTEGER_TYPE)
9741 if (TREE_CODE (op1) == INTEGER_CST)
9743 if (tree_int_cst_sgn (op1) < 0)
9745 int_const = false;
9746 if (c_inhibit_evaluation_warnings == 0)
9747 warning (0, "left shift count is negative");
9750 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9752 int_const = false;
9753 if (c_inhibit_evaluation_warnings == 0)
9754 warning (0, "left shift count >= width of type");
9758 /* Use the type of the value to be shifted. */
9759 result_type = type0;
9760 /* Convert the non vector shift-count to an integer, regardless
9761 of size of value being shifted. */
9762 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9763 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9764 op1 = convert (integer_type_node, op1);
9765 /* Avoid converting op1 to result_type later. */
9766 converted = 1;
9768 break;
9770 case EQ_EXPR:
9771 case NE_EXPR:
9772 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9773 warning_at (location,
9774 OPT_Wfloat_equal,
9775 "comparing floating point with == or != is unsafe");
9776 /* Result of comparison is always int,
9777 but don't convert the args to int! */
9778 build_type = integer_type_node;
9779 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9780 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9781 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9782 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9783 short_compare = 1;
9784 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9786 if (TREE_CODE (op0) == ADDR_EXPR
9787 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9789 if (code == EQ_EXPR)
9790 warning_at (location,
9791 OPT_Waddress,
9792 "the comparison will always evaluate as %<false%> "
9793 "for the address of %qD will never be NULL",
9794 TREE_OPERAND (op0, 0));
9795 else
9796 warning_at (location,
9797 OPT_Waddress,
9798 "the comparison will always evaluate as %<true%> "
9799 "for the address of %qD will never be NULL",
9800 TREE_OPERAND (op0, 0));
9802 result_type = type0;
9804 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9806 if (TREE_CODE (op1) == ADDR_EXPR
9807 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9809 if (code == EQ_EXPR)
9810 warning_at (location,
9811 OPT_Waddress,
9812 "the comparison will always evaluate as %<false%> "
9813 "for the address of %qD will never be NULL",
9814 TREE_OPERAND (op1, 0));
9815 else
9816 warning_at (location,
9817 OPT_Waddress,
9818 "the comparison will always evaluate as %<true%> "
9819 "for the address of %qD will never be NULL",
9820 TREE_OPERAND (op1, 0));
9822 result_type = type1;
9824 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9826 tree tt0 = TREE_TYPE (type0);
9827 tree tt1 = TREE_TYPE (type1);
9828 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
9829 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
9830 addr_space_t as_common = ADDR_SPACE_GENERIC;
9832 /* Anything compares with void *. void * compares with anything.
9833 Otherwise, the targets must be compatible
9834 and both must be object or both incomplete. */
9835 if (comp_target_types (location, type0, type1))
9836 result_type = common_pointer_type (type0, type1);
9837 else if (!addr_space_superset (as0, as1, &as_common))
9839 error_at (location, "comparison of pointers to "
9840 "disjoint address spaces");
9841 return error_mark_node;
9843 else if (VOID_TYPE_P (tt0))
9845 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
9846 pedwarn (location, OPT_pedantic, "ISO C forbids "
9847 "comparison of %<void *%> with function pointer");
9849 else if (VOID_TYPE_P (tt1))
9851 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
9852 pedwarn (location, OPT_pedantic, "ISO C forbids "
9853 "comparison of %<void *%> with function pointer");
9855 else
9856 /* Avoid warning about the volatile ObjC EH puts on decls. */
9857 if (!objc_ok)
9858 pedwarn (location, 0,
9859 "comparison of distinct pointer types lacks a cast");
9861 if (result_type == NULL_TREE)
9863 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9864 result_type = build_pointer_type
9865 (build_qualified_type (void_type_node, qual));
9868 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9870 result_type = type0;
9871 pedwarn (location, 0, "comparison between pointer and integer");
9873 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9875 result_type = type1;
9876 pedwarn (location, 0, "comparison between pointer and integer");
9878 break;
9880 case LE_EXPR:
9881 case GE_EXPR:
9882 case LT_EXPR:
9883 case GT_EXPR:
9884 build_type = integer_type_node;
9885 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9886 || code0 == FIXED_POINT_TYPE)
9887 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9888 || code1 == FIXED_POINT_TYPE))
9889 short_compare = 1;
9890 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9892 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
9893 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
9894 addr_space_t as_common;
9896 if (comp_target_types (location, type0, type1))
9898 result_type = common_pointer_type (type0, type1);
9899 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9900 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9901 pedwarn (location, 0,
9902 "comparison of complete and incomplete pointers");
9903 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9904 pedwarn (location, OPT_pedantic, "ISO C forbids "
9905 "ordered comparisons of pointers to functions");
9906 else if (null_pointer_constant_p (orig_op0)
9907 || null_pointer_constant_p (orig_op1))
9908 warning_at (location, OPT_Wextra,
9909 "ordered comparison of pointer with null pointer");
9912 else if (!addr_space_superset (as0, as1, &as_common))
9914 error_at (location, "comparison of pointers to "
9915 "disjoint address spaces");
9916 return error_mark_node;
9918 else
9920 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9921 result_type = build_pointer_type
9922 (build_qualified_type (void_type_node, qual));
9923 pedwarn (location, 0,
9924 "comparison of distinct pointer types lacks a cast");
9927 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9929 result_type = type0;
9930 if (pedantic)
9931 pedwarn (location, OPT_pedantic,
9932 "ordered comparison of pointer with integer zero");
9933 else if (extra_warnings)
9934 warning_at (location, OPT_Wextra,
9935 "ordered comparison of pointer with integer zero");
9937 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9939 result_type = type1;
9940 if (pedantic)
9941 pedwarn (location, OPT_pedantic,
9942 "ordered comparison of pointer with integer zero");
9943 else if (extra_warnings)
9944 warning_at (location, OPT_Wextra,
9945 "ordered comparison of pointer with integer zero");
9947 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9949 result_type = type0;
9950 pedwarn (location, 0, "comparison between pointer and integer");
9952 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9954 result_type = type1;
9955 pedwarn (location, 0, "comparison between pointer and integer");
9957 break;
9959 default:
9960 gcc_unreachable ();
9963 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9964 return error_mark_node;
9966 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9967 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9968 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9969 TREE_TYPE (type1))))
9971 binary_op_error (location, code, type0, type1);
9972 return error_mark_node;
9975 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9976 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9978 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9979 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9981 bool first_complex = (code0 == COMPLEX_TYPE);
9982 bool second_complex = (code1 == COMPLEX_TYPE);
9983 int none_complex = (!first_complex && !second_complex);
9985 if (shorten || common || short_compare)
9987 result_type = c_common_type (type0, type1);
9988 do_warn_double_promotion (result_type, type0, type1,
9989 "implicit conversion from %qT to %qT "
9990 "to match other operand of binary "
9991 "expression",
9992 location);
9993 if (result_type == error_mark_node)
9994 return error_mark_node;
9997 if (first_complex != second_complex
9998 && (code == PLUS_EXPR
9999 || code == MINUS_EXPR
10000 || code == MULT_EXPR
10001 || (code == TRUNC_DIV_EXPR && first_complex))
10002 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10003 && flag_signed_zeros)
10005 /* An operation on mixed real/complex operands must be
10006 handled specially, but the language-independent code can
10007 more easily optimize the plain complex arithmetic if
10008 -fno-signed-zeros. */
10009 tree real_type = TREE_TYPE (result_type);
10010 tree real, imag;
10011 if (type0 != orig_type0 || type1 != orig_type1)
10013 gcc_assert (may_need_excess_precision && common);
10014 semantic_result_type = c_common_type (orig_type0, orig_type1);
10016 if (first_complex)
10018 if (TREE_TYPE (op0) != result_type)
10019 op0 = convert_and_check (result_type, op0);
10020 if (TREE_TYPE (op1) != real_type)
10021 op1 = convert_and_check (real_type, op1);
10023 else
10025 if (TREE_TYPE (op0) != real_type)
10026 op0 = convert_and_check (real_type, op0);
10027 if (TREE_TYPE (op1) != result_type)
10028 op1 = convert_and_check (result_type, op1);
10030 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10031 return error_mark_node;
10032 if (first_complex)
10034 op0 = c_save_expr (op0);
10035 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10036 op0, 1);
10037 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10038 op0, 1);
10039 switch (code)
10041 case MULT_EXPR:
10042 case TRUNC_DIV_EXPR:
10043 imag = build2 (resultcode, real_type, imag, op1);
10044 /* Fall through. */
10045 case PLUS_EXPR:
10046 case MINUS_EXPR:
10047 real = build2 (resultcode, real_type, real, op1);
10048 break;
10049 default:
10050 gcc_unreachable();
10053 else
10055 op1 = c_save_expr (op1);
10056 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10057 op1, 1);
10058 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10059 op1, 1);
10060 switch (code)
10062 case MULT_EXPR:
10063 imag = build2 (resultcode, real_type, op0, imag);
10064 /* Fall through. */
10065 case PLUS_EXPR:
10066 real = build2 (resultcode, real_type, op0, real);
10067 break;
10068 case MINUS_EXPR:
10069 real = build2 (resultcode, real_type, op0, real);
10070 imag = build1 (NEGATE_EXPR, real_type, imag);
10071 break;
10072 default:
10073 gcc_unreachable();
10076 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10077 goto return_build_binary_op;
10080 /* For certain operations (which identify themselves by shorten != 0)
10081 if both args were extended from the same smaller type,
10082 do the arithmetic in that type and then extend.
10084 shorten !=0 and !=1 indicates a bitwise operation.
10085 For them, this optimization is safe only if
10086 both args are zero-extended or both are sign-extended.
10087 Otherwise, we might change the result.
10088 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10089 but calculated in (unsigned short) it would be (unsigned short)-1. */
10091 if (shorten && none_complex)
10093 final_type = result_type;
10094 result_type = shorten_binary_op (result_type, op0, op1,
10095 shorten == -1);
10098 /* Shifts can be shortened if shifting right. */
10100 if (short_shift)
10102 int unsigned_arg;
10103 tree arg0 = get_narrower (op0, &unsigned_arg);
10105 final_type = result_type;
10107 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10108 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10110 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10111 && tree_int_cst_sgn (op1) > 0
10112 /* We can shorten only if the shift count is less than the
10113 number of bits in the smaller type size. */
10114 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10115 /* We cannot drop an unsigned shift after sign-extension. */
10116 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10118 /* Do an unsigned shift if the operand was zero-extended. */
10119 result_type
10120 = c_common_signed_or_unsigned_type (unsigned_arg,
10121 TREE_TYPE (arg0));
10122 /* Convert value-to-be-shifted to that type. */
10123 if (TREE_TYPE (op0) != result_type)
10124 op0 = convert (result_type, op0);
10125 converted = 1;
10129 /* Comparison operations are shortened too but differently.
10130 They identify themselves by setting short_compare = 1. */
10132 if (short_compare)
10134 /* Don't write &op0, etc., because that would prevent op0
10135 from being kept in a register.
10136 Instead, make copies of the our local variables and
10137 pass the copies by reference, then copy them back afterward. */
10138 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10139 enum tree_code xresultcode = resultcode;
10140 tree val
10141 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10143 if (val != 0)
10145 ret = val;
10146 goto return_build_binary_op;
10149 op0 = xop0, op1 = xop1;
10150 converted = 1;
10151 resultcode = xresultcode;
10153 if (c_inhibit_evaluation_warnings == 0)
10155 bool op0_maybe_const = true;
10156 bool op1_maybe_const = true;
10157 tree orig_op0_folded, orig_op1_folded;
10159 if (in_late_binary_op)
10161 orig_op0_folded = orig_op0;
10162 orig_op1_folded = orig_op1;
10164 else
10166 /* Fold for the sake of possible warnings, as in
10167 build_conditional_expr. This requires the
10168 "original" values to be folded, not just op0 and
10169 op1. */
10170 c_inhibit_evaluation_warnings++;
10171 op0 = c_fully_fold (op0, require_constant_value,
10172 &op0_maybe_const);
10173 op1 = c_fully_fold (op1, require_constant_value,
10174 &op1_maybe_const);
10175 c_inhibit_evaluation_warnings--;
10176 orig_op0_folded = c_fully_fold (orig_op0,
10177 require_constant_value,
10178 NULL);
10179 orig_op1_folded = c_fully_fold (orig_op1,
10180 require_constant_value,
10181 NULL);
10184 if (warn_sign_compare)
10185 warn_for_sign_compare (location, orig_op0_folded,
10186 orig_op1_folded, op0, op1,
10187 result_type, resultcode);
10188 if (!in_late_binary_op && !int_operands)
10190 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10191 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10192 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10193 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10199 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10200 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10201 Then the expression will be built.
10202 It will be given type FINAL_TYPE if that is nonzero;
10203 otherwise, it will be given type RESULT_TYPE. */
10205 if (!result_type)
10207 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10208 return error_mark_node;
10211 if (build_type == NULL_TREE)
10213 build_type = result_type;
10214 if ((type0 != orig_type0 || type1 != orig_type1)
10215 && !boolean_op)
10217 gcc_assert (may_need_excess_precision && common);
10218 semantic_result_type = c_common_type (orig_type0, orig_type1);
10222 if (!converted)
10224 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10225 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10227 /* This can happen if one operand has a vector type, and the other
10228 has a different type. */
10229 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10230 return error_mark_node;
10233 /* Treat expressions in initializers specially as they can't trap. */
10234 if (int_const_or_overflow)
10235 ret = (require_constant_value
10236 ? fold_build2_initializer_loc (location, resultcode, build_type,
10237 op0, op1)
10238 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10239 else
10240 ret = build2 (resultcode, build_type, op0, op1);
10241 if (final_type != 0)
10242 ret = convert (final_type, ret);
10244 return_build_binary_op:
10245 gcc_assert (ret != error_mark_node);
10246 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10247 ret = (int_operands
10248 ? note_integer_operands (ret)
10249 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10250 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10251 && !in_late_binary_op)
10252 ret = note_integer_operands (ret);
10253 if (semantic_result_type)
10254 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10255 protected_set_expr_location (ret, location);
10256 return ret;
10260 /* Convert EXPR to be a truth-value, validating its type for this
10261 purpose. LOCATION is the source location for the expression. */
10263 tree
10264 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10266 bool int_const, int_operands;
10268 switch (TREE_CODE (TREE_TYPE (expr)))
10270 case ARRAY_TYPE:
10271 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10272 return error_mark_node;
10274 case RECORD_TYPE:
10275 error_at (location, "used struct type value where scalar is required");
10276 return error_mark_node;
10278 case UNION_TYPE:
10279 error_at (location, "used union type value where scalar is required");
10280 return error_mark_node;
10282 case VOID_TYPE:
10283 error_at (location, "void value not ignored as it ought to be");
10284 return error_mark_node;
10286 case FUNCTION_TYPE:
10287 gcc_unreachable ();
10289 default:
10290 break;
10293 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10294 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10295 if (int_operands)
10296 expr = remove_c_maybe_const_expr (expr);
10298 /* ??? Should we also give an error for vectors rather than leaving
10299 those to give errors later? */
10300 expr = c_common_truthvalue_conversion (location, expr);
10302 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10304 if (TREE_OVERFLOW (expr))
10305 return expr;
10306 else
10307 return note_integer_operands (expr);
10309 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10310 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10311 return expr;
10315 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10316 required. */
10318 tree
10319 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10321 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10323 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10324 /* Executing a compound literal inside a function reinitializes
10325 it. */
10326 if (!TREE_STATIC (decl))
10327 *se = true;
10328 return decl;
10330 else
10331 return expr;
10334 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10336 tree
10337 c_begin_omp_parallel (void)
10339 tree block;
10341 keep_next_level ();
10342 block = c_begin_compound_stmt (true);
10344 return block;
10347 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10348 statement. LOC is the location of the OMP_PARALLEL. */
10350 tree
10351 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10353 tree stmt;
10355 block = c_end_compound_stmt (loc, block, true);
10357 stmt = make_node (OMP_PARALLEL);
10358 TREE_TYPE (stmt) = void_type_node;
10359 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10360 OMP_PARALLEL_BODY (stmt) = block;
10361 SET_EXPR_LOCATION (stmt, loc);
10363 return add_stmt (stmt);
10366 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10368 tree
10369 c_begin_omp_task (void)
10371 tree block;
10373 keep_next_level ();
10374 block = c_begin_compound_stmt (true);
10376 return block;
10379 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10380 statement. LOC is the location of the #pragma. */
10382 tree
10383 c_finish_omp_task (location_t loc, tree clauses, tree block)
10385 tree stmt;
10387 block = c_end_compound_stmt (loc, block, true);
10389 stmt = make_node (OMP_TASK);
10390 TREE_TYPE (stmt) = void_type_node;
10391 OMP_TASK_CLAUSES (stmt) = clauses;
10392 OMP_TASK_BODY (stmt) = block;
10393 SET_EXPR_LOCATION (stmt, loc);
10395 return add_stmt (stmt);
10398 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10399 Remove any elements from the list that are invalid. */
10401 tree
10402 c_finish_omp_clauses (tree clauses)
10404 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10405 tree c, t, *pc = &clauses;
10406 const char *name;
10408 bitmap_obstack_initialize (NULL);
10409 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10410 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10411 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10413 for (pc = &clauses, c = clauses; c ; c = *pc)
10415 bool remove = false;
10416 bool need_complete = false;
10417 bool need_implicitly_determined = false;
10419 switch (OMP_CLAUSE_CODE (c))
10421 case OMP_CLAUSE_SHARED:
10422 name = "shared";
10423 need_implicitly_determined = true;
10424 goto check_dup_generic;
10426 case OMP_CLAUSE_PRIVATE:
10427 name = "private";
10428 need_complete = true;
10429 need_implicitly_determined = true;
10430 goto check_dup_generic;
10432 case OMP_CLAUSE_REDUCTION:
10433 name = "reduction";
10434 need_implicitly_determined = true;
10435 t = OMP_CLAUSE_DECL (c);
10436 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10437 || POINTER_TYPE_P (TREE_TYPE (t)))
10439 error_at (OMP_CLAUSE_LOCATION (c),
10440 "%qE has invalid type for %<reduction%>", t);
10441 remove = true;
10443 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10445 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10446 const char *r_name = NULL;
10448 switch (r_code)
10450 case PLUS_EXPR:
10451 case MULT_EXPR:
10452 case MINUS_EXPR:
10453 break;
10454 case BIT_AND_EXPR:
10455 r_name = "&";
10456 break;
10457 case BIT_XOR_EXPR:
10458 r_name = "^";
10459 break;
10460 case BIT_IOR_EXPR:
10461 r_name = "|";
10462 break;
10463 case TRUTH_ANDIF_EXPR:
10464 r_name = "&&";
10465 break;
10466 case TRUTH_ORIF_EXPR:
10467 r_name = "||";
10468 break;
10469 default:
10470 gcc_unreachable ();
10472 if (r_name)
10474 error_at (OMP_CLAUSE_LOCATION (c),
10475 "%qE has invalid type for %<reduction(%s)%>",
10476 t, r_name);
10477 remove = true;
10480 goto check_dup_generic;
10482 case OMP_CLAUSE_COPYPRIVATE:
10483 name = "copyprivate";
10484 goto check_dup_generic;
10486 case OMP_CLAUSE_COPYIN:
10487 name = "copyin";
10488 t = OMP_CLAUSE_DECL (c);
10489 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10491 error_at (OMP_CLAUSE_LOCATION (c),
10492 "%qE must be %<threadprivate%> for %<copyin%>", t);
10493 remove = true;
10495 goto check_dup_generic;
10497 check_dup_generic:
10498 t = OMP_CLAUSE_DECL (c);
10499 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10501 error_at (OMP_CLAUSE_LOCATION (c),
10502 "%qE is not a variable in clause %qs", t, name);
10503 remove = true;
10505 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10506 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10507 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10509 error_at (OMP_CLAUSE_LOCATION (c),
10510 "%qE appears more than once in data clauses", t);
10511 remove = true;
10513 else
10514 bitmap_set_bit (&generic_head, DECL_UID (t));
10515 break;
10517 case OMP_CLAUSE_FIRSTPRIVATE:
10518 name = "firstprivate";
10519 t = OMP_CLAUSE_DECL (c);
10520 need_complete = true;
10521 need_implicitly_determined = true;
10522 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10524 error_at (OMP_CLAUSE_LOCATION (c),
10525 "%qE is not a variable in clause %<firstprivate%>", t);
10526 remove = true;
10528 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10529 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10531 error_at (OMP_CLAUSE_LOCATION (c),
10532 "%qE appears more than once in data clauses", t);
10533 remove = true;
10535 else
10536 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10537 break;
10539 case OMP_CLAUSE_LASTPRIVATE:
10540 name = "lastprivate";
10541 t = OMP_CLAUSE_DECL (c);
10542 need_complete = true;
10543 need_implicitly_determined = true;
10544 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10546 error_at (OMP_CLAUSE_LOCATION (c),
10547 "%qE is not a variable in clause %<lastprivate%>", t);
10548 remove = true;
10550 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10551 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10553 error_at (OMP_CLAUSE_LOCATION (c),
10554 "%qE appears more than once in data clauses", t);
10555 remove = true;
10557 else
10558 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10559 break;
10561 case OMP_CLAUSE_IF:
10562 case OMP_CLAUSE_NUM_THREADS:
10563 case OMP_CLAUSE_SCHEDULE:
10564 case OMP_CLAUSE_NOWAIT:
10565 case OMP_CLAUSE_ORDERED:
10566 case OMP_CLAUSE_DEFAULT:
10567 case OMP_CLAUSE_UNTIED:
10568 case OMP_CLAUSE_COLLAPSE:
10569 pc = &OMP_CLAUSE_CHAIN (c);
10570 continue;
10572 default:
10573 gcc_unreachable ();
10576 if (!remove)
10578 t = OMP_CLAUSE_DECL (c);
10580 if (need_complete)
10582 t = require_complete_type (t);
10583 if (t == error_mark_node)
10584 remove = true;
10587 if (need_implicitly_determined)
10589 const char *share_name = NULL;
10591 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10592 share_name = "threadprivate";
10593 else switch (c_omp_predetermined_sharing (t))
10595 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10596 break;
10597 case OMP_CLAUSE_DEFAULT_SHARED:
10598 share_name = "shared";
10599 break;
10600 case OMP_CLAUSE_DEFAULT_PRIVATE:
10601 share_name = "private";
10602 break;
10603 default:
10604 gcc_unreachable ();
10606 if (share_name)
10608 error_at (OMP_CLAUSE_LOCATION (c),
10609 "%qE is predetermined %qs for %qs",
10610 t, share_name, name);
10611 remove = true;
10616 if (remove)
10617 *pc = OMP_CLAUSE_CHAIN (c);
10618 else
10619 pc = &OMP_CLAUSE_CHAIN (c);
10622 bitmap_obstack_release (NULL);
10623 return clauses;
10626 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10627 down to the element type of an array. */
10629 tree
10630 c_build_qualified_type (tree type, int type_quals)
10632 if (type == error_mark_node)
10633 return type;
10635 if (TREE_CODE (type) == ARRAY_TYPE)
10637 tree t;
10638 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10639 type_quals);
10641 /* See if we already have an identically qualified type. */
10642 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10644 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10645 && TYPE_NAME (t) == TYPE_NAME (type)
10646 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10647 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10648 TYPE_ATTRIBUTES (type)))
10649 break;
10651 if (!t)
10653 tree domain = TYPE_DOMAIN (type);
10655 t = build_variant_type_copy (type);
10656 TREE_TYPE (t) = element_type;
10658 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10659 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10660 SET_TYPE_STRUCTURAL_EQUALITY (t);
10661 else if (TYPE_CANONICAL (element_type) != element_type
10662 || (domain && TYPE_CANONICAL (domain) != domain))
10664 tree unqualified_canon
10665 = build_array_type (TYPE_CANONICAL (element_type),
10666 domain? TYPE_CANONICAL (domain)
10667 : NULL_TREE);
10668 TYPE_CANONICAL (t)
10669 = c_build_qualified_type (unqualified_canon, type_quals);
10671 else
10672 TYPE_CANONICAL (t) = t;
10674 return t;
10677 /* A restrict-qualified pointer type must be a pointer to object or
10678 incomplete type. Note that the use of POINTER_TYPE_P also allows
10679 REFERENCE_TYPEs, which is appropriate for C++. */
10680 if ((type_quals & TYPE_QUAL_RESTRICT)
10681 && (!POINTER_TYPE_P (type)
10682 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10684 error ("invalid use of %<restrict%>");
10685 type_quals &= ~TYPE_QUAL_RESTRICT;
10688 return build_qualified_type (type, type_quals);
10691 /* Build a VA_ARG_EXPR for the C parser. */
10693 tree
10694 c_build_va_arg (location_t loc, tree expr, tree type)
10696 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10697 warning_at (loc, OPT_Wc___compat,
10698 "C++ requires promoted type, not enum type, in %<va_arg%>");
10699 return build_va_arg (loc, expr, type);