-dA enhancement
[official-gcc.git] / gcc / c-typeck.c
blob049a8af8330821d599981cab84dcc0a6e77ee854
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. */
510 /* Tell global_bindings_p to return false so that variable_size
511 doesn't die on VLAs in parameter types. */
512 c_override_global_bindings_to_false = true;
514 len = list_length (p1);
515 newargs = 0;
517 for (i = 0; i < len; i++)
518 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
520 n = newargs;
522 for (; p1;
523 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
525 /* A null type means arg type is not specified.
526 Take whatever the other function type has. */
527 if (TREE_VALUE (p1) == 0)
529 TREE_VALUE (n) = TREE_VALUE (p2);
530 goto parm_done;
532 if (TREE_VALUE (p2) == 0)
534 TREE_VALUE (n) = TREE_VALUE (p1);
535 goto parm_done;
538 /* Given wait (union {union wait *u; int *i} *)
539 and wait (union wait *),
540 prefer union wait * as type of parm. */
541 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
542 && TREE_VALUE (p1) != TREE_VALUE (p2))
544 tree memb;
545 tree mv2 = TREE_VALUE (p2);
546 if (mv2 && mv2 != error_mark_node
547 && TREE_CODE (mv2) != ARRAY_TYPE)
548 mv2 = TYPE_MAIN_VARIANT (mv2);
549 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
550 memb; memb = DECL_CHAIN (memb))
552 tree mv3 = TREE_TYPE (memb);
553 if (mv3 && mv3 != error_mark_node
554 && TREE_CODE (mv3) != ARRAY_TYPE)
555 mv3 = TYPE_MAIN_VARIANT (mv3);
556 if (comptypes (mv3, mv2))
558 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
559 TREE_VALUE (p2));
560 pedwarn (input_location, OPT_pedantic,
561 "function types not truly compatible in ISO C");
562 goto parm_done;
566 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
567 && TREE_VALUE (p2) != TREE_VALUE (p1))
569 tree memb;
570 tree mv1 = TREE_VALUE (p1);
571 if (mv1 && mv1 != error_mark_node
572 && TREE_CODE (mv1) != ARRAY_TYPE)
573 mv1 = TYPE_MAIN_VARIANT (mv1);
574 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
575 memb; memb = DECL_CHAIN (memb))
577 tree mv3 = TREE_TYPE (memb);
578 if (mv3 && mv3 != error_mark_node
579 && TREE_CODE (mv3) != ARRAY_TYPE)
580 mv3 = TYPE_MAIN_VARIANT (mv3);
581 if (comptypes (mv3, mv1))
583 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
584 TREE_VALUE (p1));
585 pedwarn (input_location, OPT_pedantic,
586 "function types not truly compatible in ISO C");
587 goto parm_done;
591 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
592 parm_done: ;
595 c_override_global_bindings_to_false = false;
596 t1 = build_function_type (valtype, newargs);
597 t1 = qualify_type (t1, t2);
598 /* ... falls through ... */
601 default:
602 return build_type_attribute_variant (t1, attributes);
607 /* Return the type of a conditional expression between pointers to
608 possibly differently qualified versions of compatible types.
610 We assume that comp_target_types has already been done and returned
611 nonzero; if that isn't so, this may crash. */
613 static tree
614 common_pointer_type (tree t1, tree t2)
616 tree attributes;
617 tree pointed_to_1, mv1;
618 tree pointed_to_2, mv2;
619 tree target;
620 unsigned target_quals;
621 addr_space_t as1, as2, as_common;
622 int quals1, quals2;
624 /* Save time if the two types are the same. */
626 if (t1 == t2) return t1;
628 /* If one type is nonsense, use the other. */
629 if (t1 == error_mark_node)
630 return t2;
631 if (t2 == error_mark_node)
632 return t1;
634 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
635 && TREE_CODE (t2) == POINTER_TYPE);
637 /* Merge the attributes. */
638 attributes = targetm.merge_type_attributes (t1, t2);
640 /* Find the composite type of the target types, and combine the
641 qualifiers of the two types' targets. Do not lose qualifiers on
642 array element types by taking the TYPE_MAIN_VARIANT. */
643 mv1 = pointed_to_1 = TREE_TYPE (t1);
644 mv2 = pointed_to_2 = TREE_TYPE (t2);
645 if (TREE_CODE (mv1) != ARRAY_TYPE)
646 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
647 if (TREE_CODE (mv2) != ARRAY_TYPE)
648 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
649 target = composite_type (mv1, mv2);
651 /* For function types do not merge const qualifiers, but drop them
652 if used inconsistently. The middle-end uses these to mark const
653 and noreturn functions. */
654 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
655 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
657 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
658 target_quals = (quals1 & quals2);
659 else
660 target_quals = (quals1 | quals2);
662 /* If the two named address spaces are different, determine the common
663 superset address space. This is guaranteed to exist due to the
664 assumption that comp_target_type returned non-zero. */
665 as1 = TYPE_ADDR_SPACE (pointed_to_1);
666 as2 = TYPE_ADDR_SPACE (pointed_to_2);
667 if (!addr_space_superset (as1, as2, &as_common))
668 gcc_unreachable ();
670 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
672 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
673 return build_type_attribute_variant (t1, attributes);
676 /* Return the common type for two arithmetic types under the usual
677 arithmetic conversions. The default conversions have already been
678 applied, and enumerated types converted to their compatible integer
679 types. The resulting type is unqualified and has no attributes.
681 This is the type for the result of most arithmetic operations
682 if the operands have the given two types. */
684 static tree
685 c_common_type (tree t1, tree t2)
687 enum tree_code code1;
688 enum tree_code code2;
690 /* If one type is nonsense, use the other. */
691 if (t1 == error_mark_node)
692 return t2;
693 if (t2 == error_mark_node)
694 return t1;
696 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
697 t1 = TYPE_MAIN_VARIANT (t1);
699 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
700 t2 = TYPE_MAIN_VARIANT (t2);
702 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
703 t1 = build_type_attribute_variant (t1, NULL_TREE);
705 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
706 t2 = build_type_attribute_variant (t2, NULL_TREE);
708 /* Save time if the two types are the same. */
710 if (t1 == t2) return t1;
712 code1 = TREE_CODE (t1);
713 code2 = TREE_CODE (t2);
715 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
716 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
717 || code1 == INTEGER_TYPE);
718 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
719 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
720 || code2 == INTEGER_TYPE);
722 /* When one operand is a decimal float type, the other operand cannot be
723 a generic float type or a complex type. We also disallow vector types
724 here. */
725 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
726 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
728 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
730 error ("can%'t mix operands of decimal float and vector types");
731 return error_mark_node;
733 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
735 error ("can%'t mix operands of decimal float and complex types");
736 return error_mark_node;
738 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
740 error ("can%'t mix operands of decimal float and other float types");
741 return error_mark_node;
745 /* If one type is a vector type, return that type. (How the usual
746 arithmetic conversions apply to the vector types extension is not
747 precisely specified.) */
748 if (code1 == VECTOR_TYPE)
749 return t1;
751 if (code2 == VECTOR_TYPE)
752 return t2;
754 /* If one type is complex, form the common type of the non-complex
755 components, then make that complex. Use T1 or T2 if it is the
756 required type. */
757 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
759 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
760 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
761 tree subtype = c_common_type (subtype1, subtype2);
763 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
764 return t1;
765 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
766 return t2;
767 else
768 return build_complex_type (subtype);
771 /* If only one is real, use it as the result. */
773 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
774 return t1;
776 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
777 return t2;
779 /* If both are real and either are decimal floating point types, use
780 the decimal floating point type with the greater precision. */
782 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
784 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
785 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
786 return dfloat128_type_node;
787 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
788 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
789 return dfloat64_type_node;
790 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
791 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
792 return dfloat32_type_node;
795 /* Deal with fixed-point types. */
796 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
798 unsigned int unsignedp = 0, satp = 0;
799 enum machine_mode m1, m2;
800 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
802 m1 = TYPE_MODE (t1);
803 m2 = TYPE_MODE (t2);
805 /* If one input type is saturating, the result type is saturating. */
806 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
807 satp = 1;
809 /* If both fixed-point types are unsigned, the result type is unsigned.
810 When mixing fixed-point and integer types, follow the sign of the
811 fixed-point type.
812 Otherwise, the result type is signed. */
813 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
814 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
815 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
816 && TYPE_UNSIGNED (t1))
817 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
818 && TYPE_UNSIGNED (t2)))
819 unsignedp = 1;
821 /* The result type is signed. */
822 if (unsignedp == 0)
824 /* If the input type is unsigned, we need to convert to the
825 signed type. */
826 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
828 enum mode_class mclass = (enum mode_class) 0;
829 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
830 mclass = MODE_FRACT;
831 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
832 mclass = MODE_ACCUM;
833 else
834 gcc_unreachable ();
835 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
837 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
839 enum mode_class mclass = (enum mode_class) 0;
840 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
841 mclass = MODE_FRACT;
842 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
843 mclass = MODE_ACCUM;
844 else
845 gcc_unreachable ();
846 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
850 if (code1 == FIXED_POINT_TYPE)
852 fbit1 = GET_MODE_FBIT (m1);
853 ibit1 = GET_MODE_IBIT (m1);
855 else
857 fbit1 = 0;
858 /* Signed integers need to subtract one sign bit. */
859 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
862 if (code2 == FIXED_POINT_TYPE)
864 fbit2 = GET_MODE_FBIT (m2);
865 ibit2 = GET_MODE_IBIT (m2);
867 else
869 fbit2 = 0;
870 /* Signed integers need to subtract one sign bit. */
871 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
874 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
875 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
876 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
877 satp);
880 /* Both real or both integers; use the one with greater precision. */
882 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
883 return t1;
884 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
885 return t2;
887 /* Same precision. Prefer long longs to longs to ints when the
888 same precision, following the C99 rules on integer type rank
889 (which are equivalent to the C90 rules for C90 types). */
891 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
892 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
893 return long_long_unsigned_type_node;
895 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
896 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
898 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
899 return long_long_unsigned_type_node;
900 else
901 return long_long_integer_type_node;
904 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
905 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
906 return long_unsigned_type_node;
908 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
911 /* But preserve unsignedness from the other type,
912 since long cannot hold all the values of an unsigned int. */
913 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
914 return long_unsigned_type_node;
915 else
916 return long_integer_type_node;
919 /* Likewise, prefer long double to double even if same size. */
920 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
921 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
922 return long_double_type_node;
924 /* Otherwise prefer the unsigned one. */
926 if (TYPE_UNSIGNED (t1))
927 return t1;
928 else
929 return t2;
932 /* Wrapper around c_common_type that is used by c-common.c and other
933 front end optimizations that remove promotions. ENUMERAL_TYPEs
934 are allowed here and are converted to their compatible integer types.
935 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
936 preferably a non-Boolean type as the common type. */
937 tree
938 common_type (tree t1, tree t2)
940 if (TREE_CODE (t1) == ENUMERAL_TYPE)
941 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
942 if (TREE_CODE (t2) == ENUMERAL_TYPE)
943 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
945 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
946 if (TREE_CODE (t1) == BOOLEAN_TYPE
947 && TREE_CODE (t2) == BOOLEAN_TYPE)
948 return boolean_type_node;
950 /* If either type is BOOLEAN_TYPE, then return the other. */
951 if (TREE_CODE (t1) == BOOLEAN_TYPE)
952 return t2;
953 if (TREE_CODE (t2) == BOOLEAN_TYPE)
954 return t1;
956 return c_common_type (t1, t2);
959 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
960 or various other operations. Return 2 if they are compatible
961 but a warning may be needed if you use them together. */
964 comptypes (tree type1, tree type2)
966 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
967 int val;
969 val = comptypes_internal (type1, type2, NULL, NULL);
970 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
972 return val;
975 /* Like comptypes, but if it returns non-zero because enum and int are
976 compatible, it sets *ENUM_AND_INT_P to true. */
978 static int
979 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
981 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
982 int val;
984 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
985 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
987 return val;
990 /* Like comptypes, but if it returns nonzero for different types, it
991 sets *DIFFERENT_TYPES_P to true. */
994 comptypes_check_different_types (tree type1, tree type2,
995 bool *different_types_p)
997 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
998 int val;
1000 val = comptypes_internal (type1, type2, NULL, different_types_p);
1001 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1003 return val;
1006 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1007 or various other operations. Return 2 if they are compatible
1008 but a warning may be needed if you use them together. If
1009 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1010 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1011 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1012 NULL, and the types are compatible but different enough not to be
1013 permitted in C1X typedef redeclarations, then this sets
1014 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1015 false, but may or may not be set if the types are incompatible.
1016 This differs from comptypes, in that we don't free the seen
1017 types. */
1019 static int
1020 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1021 bool *different_types_p)
1023 const_tree t1 = type1;
1024 const_tree t2 = type2;
1025 int attrval, val;
1027 /* Suppress errors caused by previously reported errors. */
1029 if (t1 == t2 || !t1 || !t2
1030 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1031 return 1;
1033 /* Enumerated types are compatible with integer types, but this is
1034 not transitive: two enumerated types in the same translation unit
1035 are compatible with each other only if they are the same type. */
1037 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1039 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1040 if (TREE_CODE (t2) != VOID_TYPE)
1042 if (enum_and_int_p != NULL)
1043 *enum_and_int_p = true;
1044 if (different_types_p != NULL)
1045 *different_types_p = true;
1048 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1050 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1051 if (TREE_CODE (t1) != VOID_TYPE)
1053 if (enum_and_int_p != NULL)
1054 *enum_and_int_p = true;
1055 if (different_types_p != NULL)
1056 *different_types_p = true;
1060 if (t1 == t2)
1061 return 1;
1063 /* Different classes of types can't be compatible. */
1065 if (TREE_CODE (t1) != TREE_CODE (t2))
1066 return 0;
1068 /* Qualifiers must match. C99 6.7.3p9 */
1070 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1071 return 0;
1073 /* Allow for two different type nodes which have essentially the same
1074 definition. Note that we already checked for equality of the type
1075 qualifiers (just above). */
1077 if (TREE_CODE (t1) != ARRAY_TYPE
1078 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1079 return 1;
1081 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1082 if (!(attrval = comp_type_attributes (t1, t2)))
1083 return 0;
1085 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1086 val = 0;
1088 switch (TREE_CODE (t1))
1090 case POINTER_TYPE:
1091 /* Do not remove mode or aliasing information. */
1092 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1093 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1094 break;
1095 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1096 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1097 enum_and_int_p, different_types_p));
1098 break;
1100 case FUNCTION_TYPE:
1101 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1102 different_types_p);
1103 break;
1105 case ARRAY_TYPE:
1107 tree d1 = TYPE_DOMAIN (t1);
1108 tree d2 = TYPE_DOMAIN (t2);
1109 bool d1_variable, d2_variable;
1110 bool d1_zero, d2_zero;
1111 val = 1;
1113 /* Target types must match incl. qualifiers. */
1114 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1115 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1116 enum_and_int_p,
1117 different_types_p)))
1118 return 0;
1120 if (different_types_p != NULL
1121 && (d1 == 0) != (d2 == 0))
1122 *different_types_p = true;
1123 /* Sizes must match unless one is missing or variable. */
1124 if (d1 == 0 || d2 == 0 || d1 == d2)
1125 break;
1127 d1_zero = !TYPE_MAX_VALUE (d1);
1128 d2_zero = !TYPE_MAX_VALUE (d2);
1130 d1_variable = (!d1_zero
1131 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1132 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1133 d2_variable = (!d2_zero
1134 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1135 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1136 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1137 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1139 if (different_types_p != NULL
1140 && d1_variable != d2_variable)
1141 *different_types_p = true;
1142 if (d1_variable || d2_variable)
1143 break;
1144 if (d1_zero && d2_zero)
1145 break;
1146 if (d1_zero || d2_zero
1147 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1148 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1149 val = 0;
1151 break;
1154 case ENUMERAL_TYPE:
1155 case RECORD_TYPE:
1156 case UNION_TYPE:
1157 if (val != 1 && !same_translation_unit_p (t1, t2))
1159 tree a1 = TYPE_ATTRIBUTES (t1);
1160 tree a2 = TYPE_ATTRIBUTES (t2);
1162 if (! attribute_list_contained (a1, a2)
1163 && ! attribute_list_contained (a2, a1))
1164 break;
1166 if (attrval != 2)
1167 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1168 different_types_p);
1169 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1170 different_types_p);
1172 break;
1174 case VECTOR_TYPE:
1175 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1176 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1177 enum_and_int_p, different_types_p));
1178 break;
1180 default:
1181 break;
1183 return attrval == 2 && val == 1 ? 2 : val;
1186 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1187 their qualifiers, except for named address spaces. If the pointers point to
1188 different named addresses, then we must determine if one address space is a
1189 subset of the other. */
1191 static int
1192 comp_target_types (location_t location, tree ttl, tree ttr)
1194 int val;
1195 tree mvl = TREE_TYPE (ttl);
1196 tree mvr = TREE_TYPE (ttr);
1197 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1198 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1199 addr_space_t as_common;
1200 bool enum_and_int_p;
1202 /* Fail if pointers point to incompatible address spaces. */
1203 if (!addr_space_superset (asl, asr, &as_common))
1204 return 0;
1206 /* Do not lose qualifiers on element types of array types that are
1207 pointer targets by taking their TYPE_MAIN_VARIANT. */
1208 if (TREE_CODE (mvl) != ARRAY_TYPE)
1209 mvl = TYPE_MAIN_VARIANT (mvl);
1210 if (TREE_CODE (mvr) != ARRAY_TYPE)
1211 mvr = TYPE_MAIN_VARIANT (mvr);
1212 enum_and_int_p = false;
1213 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1215 if (val == 2)
1216 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1218 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1219 warning_at (location, OPT_Wc___compat,
1220 "pointer target types incompatible in C++");
1222 return val;
1225 /* Subroutines of `comptypes'. */
1227 /* Determine whether two trees derive from the same translation unit.
1228 If the CONTEXT chain ends in a null, that tree's context is still
1229 being parsed, so if two trees have context chains ending in null,
1230 they're in the same translation unit. */
1232 same_translation_unit_p (const_tree t1, const_tree t2)
1234 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1235 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1237 case tcc_declaration:
1238 t1 = DECL_CONTEXT (t1); break;
1239 case tcc_type:
1240 t1 = TYPE_CONTEXT (t1); break;
1241 case tcc_exceptional:
1242 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1243 default: gcc_unreachable ();
1246 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1247 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1249 case tcc_declaration:
1250 t2 = DECL_CONTEXT (t2); break;
1251 case tcc_type:
1252 t2 = TYPE_CONTEXT (t2); break;
1253 case tcc_exceptional:
1254 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1255 default: gcc_unreachable ();
1258 return t1 == t2;
1261 /* Allocate the seen two types, assuming that they are compatible. */
1263 static struct tagged_tu_seen_cache *
1264 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1266 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1267 tu->next = tagged_tu_seen_base;
1268 tu->t1 = t1;
1269 tu->t2 = t2;
1271 tagged_tu_seen_base = tu;
1273 /* The C standard says that two structures in different translation
1274 units are compatible with each other only if the types of their
1275 fields are compatible (among other things). We assume that they
1276 are compatible until proven otherwise when building the cache.
1277 An example where this can occur is:
1278 struct a
1280 struct a *next;
1282 If we are comparing this against a similar struct in another TU,
1283 and did not assume they were compatible, we end up with an infinite
1284 loop. */
1285 tu->val = 1;
1286 return tu;
1289 /* Free the seen types until we get to TU_TIL. */
1291 static void
1292 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1294 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1295 while (tu != tu_til)
1297 const struct tagged_tu_seen_cache *const tu1
1298 = (const struct tagged_tu_seen_cache *) tu;
1299 tu = tu1->next;
1300 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1302 tagged_tu_seen_base = tu_til;
1305 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1306 compatible. If the two types are not the same (which has been
1307 checked earlier), this can only happen when multiple translation
1308 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1309 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1310 comptypes_internal. */
1312 static int
1313 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1314 bool *enum_and_int_p, bool *different_types_p)
1316 tree s1, s2;
1317 bool needs_warning = false;
1319 /* We have to verify that the tags of the types are the same. This
1320 is harder than it looks because this may be a typedef, so we have
1321 to go look at the original type. It may even be a typedef of a
1322 typedef...
1323 In the case of compiler-created builtin structs the TYPE_DECL
1324 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1325 while (TYPE_NAME (t1)
1326 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1327 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1328 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1330 while (TYPE_NAME (t2)
1331 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1332 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1333 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1335 /* C90 didn't have the requirement that the two tags be the same. */
1336 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1337 return 0;
1339 /* C90 didn't say what happened if one or both of the types were
1340 incomplete; we choose to follow C99 rules here, which is that they
1341 are compatible. */
1342 if (TYPE_SIZE (t1) == NULL
1343 || TYPE_SIZE (t2) == NULL)
1344 return 1;
1347 const struct tagged_tu_seen_cache * tts_i;
1348 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1349 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1350 return tts_i->val;
1353 switch (TREE_CODE (t1))
1355 case ENUMERAL_TYPE:
1357 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1358 /* Speed up the case where the type values are in the same order. */
1359 tree tv1 = TYPE_VALUES (t1);
1360 tree tv2 = TYPE_VALUES (t2);
1362 if (tv1 == tv2)
1364 return 1;
1367 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1369 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1370 break;
1371 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1373 tu->val = 0;
1374 return 0;
1378 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1380 return 1;
1382 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1384 tu->val = 0;
1385 return 0;
1388 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1390 tu->val = 0;
1391 return 0;
1394 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1396 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1397 if (s2 == NULL
1398 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1400 tu->val = 0;
1401 return 0;
1404 return 1;
1407 case UNION_TYPE:
1409 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1410 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1412 tu->val = 0;
1413 return 0;
1416 /* Speed up the common case where the fields are in the same order. */
1417 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1418 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1420 int result;
1422 if (DECL_NAME (s1) != DECL_NAME (s2))
1423 break;
1424 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1425 enum_and_int_p, different_types_p);
1427 if (result != 1 && !DECL_NAME (s1))
1428 break;
1429 if (result == 0)
1431 tu->val = 0;
1432 return 0;
1434 if (result == 2)
1435 needs_warning = true;
1437 if (TREE_CODE (s1) == FIELD_DECL
1438 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1439 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1441 tu->val = 0;
1442 return 0;
1445 if (!s1 && !s2)
1447 tu->val = needs_warning ? 2 : 1;
1448 return tu->val;
1451 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1453 bool ok = false;
1455 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1456 if (DECL_NAME (s1) == DECL_NAME (s2))
1458 int result;
1460 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1461 enum_and_int_p,
1462 different_types_p);
1464 if (result != 1 && !DECL_NAME (s1))
1465 continue;
1466 if (result == 0)
1468 tu->val = 0;
1469 return 0;
1471 if (result == 2)
1472 needs_warning = true;
1474 if (TREE_CODE (s1) == FIELD_DECL
1475 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1476 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1477 break;
1479 ok = true;
1480 break;
1482 if (!ok)
1484 tu->val = 0;
1485 return 0;
1488 tu->val = needs_warning ? 2 : 10;
1489 return tu->val;
1492 case RECORD_TYPE:
1494 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1496 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1497 s1 && s2;
1498 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1500 int result;
1501 if (TREE_CODE (s1) != TREE_CODE (s2)
1502 || DECL_NAME (s1) != DECL_NAME (s2))
1503 break;
1504 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1505 enum_and_int_p, different_types_p);
1506 if (result == 0)
1507 break;
1508 if (result == 2)
1509 needs_warning = true;
1511 if (TREE_CODE (s1) == FIELD_DECL
1512 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1513 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1514 break;
1516 if (s1 && s2)
1517 tu->val = 0;
1518 else
1519 tu->val = needs_warning ? 2 : 1;
1520 return tu->val;
1523 default:
1524 gcc_unreachable ();
1528 /* Return 1 if two function types F1 and F2 are compatible.
1529 If either type specifies no argument types,
1530 the other must specify a fixed number of self-promoting arg types.
1531 Otherwise, if one type specifies only the number of arguments,
1532 the other must specify that number of self-promoting arg types.
1533 Otherwise, the argument types must match.
1534 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1536 static int
1537 function_types_compatible_p (const_tree f1, const_tree f2,
1538 bool *enum_and_int_p, bool *different_types_p)
1540 tree args1, args2;
1541 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1542 int val = 1;
1543 int val1;
1544 tree ret1, ret2;
1546 ret1 = TREE_TYPE (f1);
1547 ret2 = TREE_TYPE (f2);
1549 /* 'volatile' qualifiers on a function's return type used to mean
1550 the function is noreturn. */
1551 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1552 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1553 if (TYPE_VOLATILE (ret1))
1554 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1555 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1556 if (TYPE_VOLATILE (ret2))
1557 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1558 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1559 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1560 if (val == 0)
1561 return 0;
1563 args1 = TYPE_ARG_TYPES (f1);
1564 args2 = TYPE_ARG_TYPES (f2);
1566 if (different_types_p != NULL
1567 && (args1 == 0) != (args2 == 0))
1568 *different_types_p = true;
1570 /* An unspecified parmlist matches any specified parmlist
1571 whose argument types don't need default promotions. */
1573 if (args1 == 0)
1575 if (!self_promoting_args_p (args2))
1576 return 0;
1577 /* If one of these types comes from a non-prototype fn definition,
1578 compare that with the other type's arglist.
1579 If they don't match, ask for a warning (but no error). */
1580 if (TYPE_ACTUAL_ARG_TYPES (f1)
1581 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1582 enum_and_int_p, different_types_p))
1583 val = 2;
1584 return val;
1586 if (args2 == 0)
1588 if (!self_promoting_args_p (args1))
1589 return 0;
1590 if (TYPE_ACTUAL_ARG_TYPES (f2)
1591 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1592 enum_and_int_p, different_types_p))
1593 val = 2;
1594 return val;
1597 /* Both types have argument lists: compare them and propagate results. */
1598 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1599 different_types_p);
1600 return val1 != 1 ? val1 : val;
1603 /* Check two lists of types for compatibility, returning 0 for
1604 incompatible, 1 for compatible, or 2 for compatible with
1605 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1606 comptypes_internal. */
1608 static int
1609 type_lists_compatible_p (const_tree args1, const_tree args2,
1610 bool *enum_and_int_p, bool *different_types_p)
1612 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1613 int val = 1;
1614 int newval = 0;
1616 while (1)
1618 tree a1, mv1, a2, mv2;
1619 if (args1 == 0 && args2 == 0)
1620 return val;
1621 /* If one list is shorter than the other,
1622 they fail to match. */
1623 if (args1 == 0 || args2 == 0)
1624 return 0;
1625 mv1 = a1 = TREE_VALUE (args1);
1626 mv2 = a2 = TREE_VALUE (args2);
1627 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1628 mv1 = TYPE_MAIN_VARIANT (mv1);
1629 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1630 mv2 = TYPE_MAIN_VARIANT (mv2);
1631 /* A null pointer instead of a type
1632 means there is supposed to be an argument
1633 but nothing is specified about what type it has.
1634 So match anything that self-promotes. */
1635 if (different_types_p != NULL
1636 && (a1 == 0) != (a2 == 0))
1637 *different_types_p = true;
1638 if (a1 == 0)
1640 if (c_type_promotes_to (a2) != a2)
1641 return 0;
1643 else if (a2 == 0)
1645 if (c_type_promotes_to (a1) != a1)
1646 return 0;
1648 /* If one of the lists has an error marker, ignore this arg. */
1649 else if (TREE_CODE (a1) == ERROR_MARK
1650 || TREE_CODE (a2) == ERROR_MARK)
1652 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1653 different_types_p)))
1655 if (different_types_p != NULL)
1656 *different_types_p = true;
1657 /* Allow wait (union {union wait *u; int *i} *)
1658 and wait (union wait *) to be compatible. */
1659 if (TREE_CODE (a1) == UNION_TYPE
1660 && (TYPE_NAME (a1) == 0
1661 || TYPE_TRANSPARENT_AGGR (a1))
1662 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1663 && tree_int_cst_equal (TYPE_SIZE (a1),
1664 TYPE_SIZE (a2)))
1666 tree memb;
1667 for (memb = TYPE_FIELDS (a1);
1668 memb; memb = DECL_CHAIN (memb))
1670 tree mv3 = TREE_TYPE (memb);
1671 if (mv3 && mv3 != error_mark_node
1672 && TREE_CODE (mv3) != ARRAY_TYPE)
1673 mv3 = TYPE_MAIN_VARIANT (mv3);
1674 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1675 different_types_p))
1676 break;
1678 if (memb == 0)
1679 return 0;
1681 else if (TREE_CODE (a2) == UNION_TYPE
1682 && (TYPE_NAME (a2) == 0
1683 || TYPE_TRANSPARENT_AGGR (a2))
1684 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1685 && tree_int_cst_equal (TYPE_SIZE (a2),
1686 TYPE_SIZE (a1)))
1688 tree memb;
1689 for (memb = TYPE_FIELDS (a2);
1690 memb; memb = DECL_CHAIN (memb))
1692 tree mv3 = TREE_TYPE (memb);
1693 if (mv3 && mv3 != error_mark_node
1694 && TREE_CODE (mv3) != ARRAY_TYPE)
1695 mv3 = TYPE_MAIN_VARIANT (mv3);
1696 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1697 different_types_p))
1698 break;
1700 if (memb == 0)
1701 return 0;
1703 else
1704 return 0;
1707 /* comptypes said ok, but record if it said to warn. */
1708 if (newval > val)
1709 val = newval;
1711 args1 = TREE_CHAIN (args1);
1712 args2 = TREE_CHAIN (args2);
1716 /* Compute the size to increment a pointer by. */
1718 static tree
1719 c_size_in_bytes (const_tree type)
1721 enum tree_code code = TREE_CODE (type);
1723 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1724 return size_one_node;
1726 if (!COMPLETE_OR_VOID_TYPE_P (type))
1728 error ("arithmetic on pointer to an incomplete type");
1729 return size_one_node;
1732 /* Convert in case a char is more than one unit. */
1733 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1734 size_int (TYPE_PRECISION (char_type_node)
1735 / BITS_PER_UNIT));
1738 /* Return either DECL or its known constant value (if it has one). */
1740 tree
1741 decl_constant_value (tree decl)
1743 if (/* Don't change a variable array bound or initial value to a constant
1744 in a place where a variable is invalid. Note that DECL_INITIAL
1745 isn't valid for a PARM_DECL. */
1746 current_function_decl != 0
1747 && TREE_CODE (decl) != PARM_DECL
1748 && !TREE_THIS_VOLATILE (decl)
1749 && TREE_READONLY (decl)
1750 && DECL_INITIAL (decl) != 0
1751 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1752 /* This is invalid if initial value is not constant.
1753 If it has either a function call, a memory reference,
1754 or a variable, then re-evaluating it could give different results. */
1755 && TREE_CONSTANT (DECL_INITIAL (decl))
1756 /* Check for cases where this is sub-optimal, even though valid. */
1757 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1758 return DECL_INITIAL (decl);
1759 return decl;
1762 /* Convert the array expression EXP to a pointer. */
1763 static tree
1764 array_to_pointer_conversion (location_t loc, tree exp)
1766 tree orig_exp = exp;
1767 tree type = TREE_TYPE (exp);
1768 tree adr;
1769 tree restype = TREE_TYPE (type);
1770 tree ptrtype;
1772 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1774 STRIP_TYPE_NOPS (exp);
1776 if (TREE_NO_WARNING (orig_exp))
1777 TREE_NO_WARNING (exp) = 1;
1779 ptrtype = build_pointer_type (restype);
1781 if (TREE_CODE (exp) == INDIRECT_REF)
1782 return convert (ptrtype, TREE_OPERAND (exp, 0));
1784 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1785 return convert (ptrtype, adr);
1788 /* Convert the function expression EXP to a pointer. */
1789 static tree
1790 function_to_pointer_conversion (location_t loc, tree exp)
1792 tree orig_exp = exp;
1794 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1796 STRIP_TYPE_NOPS (exp);
1798 if (TREE_NO_WARNING (orig_exp))
1799 TREE_NO_WARNING (exp) = 1;
1801 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1804 /* Mark EXP as read, not just set, for set but not used -Wunused
1805 warning purposes. */
1807 void
1808 mark_exp_read (tree exp)
1810 switch (TREE_CODE (exp))
1812 case VAR_DECL:
1813 case PARM_DECL:
1814 DECL_READ_P (exp) = 1;
1815 break;
1816 case ARRAY_REF:
1817 case COMPONENT_REF:
1818 case MODIFY_EXPR:
1819 case REALPART_EXPR:
1820 case IMAGPART_EXPR:
1821 CASE_CONVERT:
1822 case ADDR_EXPR:
1823 mark_exp_read (TREE_OPERAND (exp, 0));
1824 break;
1825 case COMPOUND_EXPR:
1826 case C_MAYBE_CONST_EXPR:
1827 mark_exp_read (TREE_OPERAND (exp, 1));
1828 break;
1829 default:
1830 break;
1834 /* Perform the default conversion of arrays and functions to pointers.
1835 Return the result of converting EXP. For any other expression, just
1836 return EXP.
1838 LOC is the location of the expression. */
1840 struct c_expr
1841 default_function_array_conversion (location_t loc, struct c_expr exp)
1843 tree orig_exp = exp.value;
1844 tree type = TREE_TYPE (exp.value);
1845 enum tree_code code = TREE_CODE (type);
1847 switch (code)
1849 case ARRAY_TYPE:
1851 bool not_lvalue = false;
1852 bool lvalue_array_p;
1854 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1855 || CONVERT_EXPR_P (exp.value))
1856 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1858 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1859 not_lvalue = true;
1860 exp.value = TREE_OPERAND (exp.value, 0);
1863 if (TREE_NO_WARNING (orig_exp))
1864 TREE_NO_WARNING (exp.value) = 1;
1866 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1867 if (!flag_isoc99 && !lvalue_array_p)
1869 /* Before C99, non-lvalue arrays do not decay to pointers.
1870 Normally, using such an array would be invalid; but it can
1871 be used correctly inside sizeof or as a statement expression.
1872 Thus, do not give an error here; an error will result later. */
1873 return exp;
1876 exp.value = array_to_pointer_conversion (loc, exp.value);
1878 break;
1879 case FUNCTION_TYPE:
1880 exp.value = function_to_pointer_conversion (loc, exp.value);
1881 break;
1882 default:
1883 break;
1886 return exp;
1889 struct c_expr
1890 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1892 mark_exp_read (exp.value);
1893 return default_function_array_conversion (loc, exp);
1896 /* EXP is an expression of integer type. Apply the integer promotions
1897 to it and return the promoted value. */
1899 tree
1900 perform_integral_promotions (tree exp)
1902 tree type = TREE_TYPE (exp);
1903 enum tree_code code = TREE_CODE (type);
1905 gcc_assert (INTEGRAL_TYPE_P (type));
1907 /* Normally convert enums to int,
1908 but convert wide enums to something wider. */
1909 if (code == ENUMERAL_TYPE)
1911 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1912 TYPE_PRECISION (integer_type_node)),
1913 ((TYPE_PRECISION (type)
1914 >= TYPE_PRECISION (integer_type_node))
1915 && TYPE_UNSIGNED (type)));
1917 return convert (type, exp);
1920 /* ??? This should no longer be needed now bit-fields have their
1921 proper types. */
1922 if (TREE_CODE (exp) == COMPONENT_REF
1923 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1924 /* If it's thinner than an int, promote it like a
1925 c_promoting_integer_type_p, otherwise leave it alone. */
1926 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1927 TYPE_PRECISION (integer_type_node)))
1928 return convert (integer_type_node, exp);
1930 if (c_promoting_integer_type_p (type))
1932 /* Preserve unsignedness if not really getting any wider. */
1933 if (TYPE_UNSIGNED (type)
1934 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1935 return convert (unsigned_type_node, exp);
1937 return convert (integer_type_node, exp);
1940 return exp;
1944 /* Perform default promotions for C data used in expressions.
1945 Enumeral types or short or char are converted to int.
1946 In addition, manifest constants symbols are replaced by their values. */
1948 tree
1949 default_conversion (tree exp)
1951 tree orig_exp;
1952 tree type = TREE_TYPE (exp);
1953 enum tree_code code = TREE_CODE (type);
1954 tree promoted_type;
1956 mark_exp_read (exp);
1958 /* Functions and arrays have been converted during parsing. */
1959 gcc_assert (code != FUNCTION_TYPE);
1960 if (code == ARRAY_TYPE)
1961 return exp;
1963 /* Constants can be used directly unless they're not loadable. */
1964 if (TREE_CODE (exp) == CONST_DECL)
1965 exp = DECL_INITIAL (exp);
1967 /* Strip no-op conversions. */
1968 orig_exp = exp;
1969 STRIP_TYPE_NOPS (exp);
1971 if (TREE_NO_WARNING (orig_exp))
1972 TREE_NO_WARNING (exp) = 1;
1974 if (code == VOID_TYPE)
1976 error ("void value not ignored as it ought to be");
1977 return error_mark_node;
1980 exp = require_complete_type (exp);
1981 if (exp == error_mark_node)
1982 return error_mark_node;
1984 promoted_type = targetm.promoted_type (type);
1985 if (promoted_type)
1986 return convert (promoted_type, exp);
1988 if (INTEGRAL_TYPE_P (type))
1989 return perform_integral_promotions (exp);
1991 return exp;
1994 /* Look up COMPONENT in a structure or union TYPE.
1996 If the component name is not found, returns NULL_TREE. Otherwise,
1997 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1998 stepping down the chain to the component, which is in the last
1999 TREE_VALUE of the list. Normally the list is of length one, but if
2000 the component is embedded within (nested) anonymous structures or
2001 unions, the list steps down the chain to the component. */
2003 static tree
2004 lookup_field (tree type, tree component)
2006 tree field;
2008 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2009 to the field elements. Use a binary search on this array to quickly
2010 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2011 will always be set for structures which have many elements. */
2013 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2015 int bot, top, half;
2016 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2018 field = TYPE_FIELDS (type);
2019 bot = 0;
2020 top = TYPE_LANG_SPECIFIC (type)->s->len;
2021 while (top - bot > 1)
2023 half = (top - bot + 1) >> 1;
2024 field = field_array[bot+half];
2026 if (DECL_NAME (field) == NULL_TREE)
2028 /* Step through all anon unions in linear fashion. */
2029 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2031 field = field_array[bot++];
2032 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2033 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2035 tree anon = lookup_field (TREE_TYPE (field), component);
2037 if (anon)
2038 return tree_cons (NULL_TREE, field, anon);
2040 /* The Plan 9 compiler permits referring
2041 directly to an anonymous struct/union field
2042 using a typedef name. */
2043 if (flag_plan9_extensions
2044 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2045 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2046 == TYPE_DECL)
2047 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2048 == component))
2049 break;
2053 /* Entire record is only anon unions. */
2054 if (bot > top)
2055 return NULL_TREE;
2057 /* Restart the binary search, with new lower bound. */
2058 continue;
2061 if (DECL_NAME (field) == component)
2062 break;
2063 if (DECL_NAME (field) < component)
2064 bot += half;
2065 else
2066 top = bot + half;
2069 if (DECL_NAME (field_array[bot]) == component)
2070 field = field_array[bot];
2071 else if (DECL_NAME (field) != component)
2072 return NULL_TREE;
2074 else
2076 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2078 if (DECL_NAME (field) == NULL_TREE
2079 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2080 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2082 tree anon = lookup_field (TREE_TYPE (field), component);
2084 if (anon)
2085 return tree_cons (NULL_TREE, field, anon);
2087 /* The Plan 9 compiler permits referring directly to an
2088 anonymous struct/union field using a typedef
2089 name. */
2090 if (flag_plan9_extensions
2091 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2092 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2093 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2094 == component))
2095 break;
2098 if (DECL_NAME (field) == component)
2099 break;
2102 if (field == NULL_TREE)
2103 return NULL_TREE;
2106 return tree_cons (NULL_TREE, field, NULL_TREE);
2109 /* Make an expression to refer to the COMPONENT field of structure or
2110 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2111 location of the COMPONENT_REF. */
2113 tree
2114 build_component_ref (location_t loc, tree datum, tree component)
2116 tree type = TREE_TYPE (datum);
2117 enum tree_code code = TREE_CODE (type);
2118 tree field = NULL;
2119 tree ref;
2120 bool datum_lvalue = lvalue_p (datum);
2122 if (!objc_is_public (datum, component))
2123 return error_mark_node;
2125 /* Detect Objective-C property syntax object.property. */
2126 if (c_dialect_objc ()
2127 && (ref = objc_maybe_build_component_ref (datum, component)))
2128 return ref;
2130 /* See if there is a field or component with name COMPONENT. */
2132 if (code == RECORD_TYPE || code == UNION_TYPE)
2134 if (!COMPLETE_TYPE_P (type))
2136 c_incomplete_type_error (NULL_TREE, type);
2137 return error_mark_node;
2140 field = lookup_field (type, component);
2142 if (!field)
2144 error_at (loc, "%qT has no member named %qE", type, component);
2145 return error_mark_node;
2148 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2149 This might be better solved in future the way the C++ front
2150 end does it - by giving the anonymous entities each a
2151 separate name and type, and then have build_component_ref
2152 recursively call itself. We can't do that here. */
2155 tree subdatum = TREE_VALUE (field);
2156 int quals;
2157 tree subtype;
2158 bool use_datum_quals;
2160 if (TREE_TYPE (subdatum) == error_mark_node)
2161 return error_mark_node;
2163 /* If this is an rvalue, it does not have qualifiers in C
2164 standard terms and we must avoid propagating such
2165 qualifiers down to a non-lvalue array that is then
2166 converted to a pointer. */
2167 use_datum_quals = (datum_lvalue
2168 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2170 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2171 if (use_datum_quals)
2172 quals |= TYPE_QUALS (TREE_TYPE (datum));
2173 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2175 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2176 NULL_TREE);
2177 SET_EXPR_LOCATION (ref, loc);
2178 if (TREE_READONLY (subdatum)
2179 || (use_datum_quals && TREE_READONLY (datum)))
2180 TREE_READONLY (ref) = 1;
2181 if (TREE_THIS_VOLATILE (subdatum)
2182 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2183 TREE_THIS_VOLATILE (ref) = 1;
2185 if (TREE_DEPRECATED (subdatum))
2186 warn_deprecated_use (subdatum, NULL_TREE);
2188 datum = ref;
2190 field = TREE_CHAIN (field);
2192 while (field);
2194 return ref;
2196 else if (code != ERROR_MARK)
2197 error_at (loc,
2198 "request for member %qE in something not a structure or union",
2199 component);
2201 return error_mark_node;
2204 /* Given an expression PTR for a pointer, return an expression
2205 for the value pointed to.
2206 ERRORSTRING is the name of the operator to appear in error messages.
2208 LOC is the location to use for the generated tree. */
2210 tree
2211 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2213 tree pointer = default_conversion (ptr);
2214 tree type = TREE_TYPE (pointer);
2215 tree ref;
2217 if (TREE_CODE (type) == POINTER_TYPE)
2219 if (CONVERT_EXPR_P (pointer)
2220 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2222 /* If a warning is issued, mark it to avoid duplicates from
2223 the backend. This only needs to be done at
2224 warn_strict_aliasing > 2. */
2225 if (warn_strict_aliasing > 2)
2226 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2227 type, TREE_OPERAND (pointer, 0)))
2228 TREE_NO_WARNING (pointer) = 1;
2231 if (TREE_CODE (pointer) == ADDR_EXPR
2232 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2233 == TREE_TYPE (type)))
2235 ref = TREE_OPERAND (pointer, 0);
2236 protected_set_expr_location (ref, loc);
2237 return ref;
2239 else
2241 tree t = TREE_TYPE (type);
2243 ref = build1 (INDIRECT_REF, t, pointer);
2245 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2247 error_at (loc, "dereferencing pointer to incomplete type");
2248 return error_mark_node;
2250 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2251 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2253 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2254 so that we get the proper error message if the result is used
2255 to assign to. Also, &* is supposed to be a no-op.
2256 And ANSI C seems to specify that the type of the result
2257 should be the const type. */
2258 /* A de-reference of a pointer to const is not a const. It is valid
2259 to change it via some other pointer. */
2260 TREE_READONLY (ref) = TYPE_READONLY (t);
2261 TREE_SIDE_EFFECTS (ref)
2262 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2263 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2264 protected_set_expr_location (ref, loc);
2265 return ref;
2268 else if (TREE_CODE (pointer) != ERROR_MARK)
2269 invalid_indirection_error (loc, type, errstring);
2271 return error_mark_node;
2274 /* This handles expressions of the form "a[i]", which denotes
2275 an array reference.
2277 This is logically equivalent in C to *(a+i), but we may do it differently.
2278 If A is a variable or a member, we generate a primitive ARRAY_REF.
2279 This avoids forcing the array out of registers, and can work on
2280 arrays that are not lvalues (for example, members of structures returned
2281 by functions).
2283 For vector types, allow vector[i] but not i[vector], and create
2284 *(((type*)&vectortype) + i) for the expression.
2286 LOC is the location to use for the returned expression. */
2288 tree
2289 build_array_ref (location_t loc, tree array, tree index)
2291 tree ret;
2292 bool swapped = false;
2293 if (TREE_TYPE (array) == error_mark_node
2294 || TREE_TYPE (index) == error_mark_node)
2295 return error_mark_node;
2297 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2298 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2299 /* Allow vector[index] but not index[vector]. */
2300 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2302 tree temp;
2303 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2304 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2306 error_at (loc,
2307 "subscripted value is neither array nor pointer nor vector");
2309 return error_mark_node;
2311 temp = array;
2312 array = index;
2313 index = temp;
2314 swapped = true;
2317 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2319 error_at (loc, "array subscript is not an integer");
2320 return error_mark_node;
2323 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2325 error_at (loc, "subscripted value is pointer to function");
2326 return error_mark_node;
2329 /* ??? Existing practice has been to warn only when the char
2330 index is syntactically the index, not for char[array]. */
2331 if (!swapped)
2332 warn_array_subscript_with_type_char (index);
2334 /* Apply default promotions *after* noticing character types. */
2335 index = default_conversion (index);
2337 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2339 /* For vector[index], convert the vector to a
2340 pointer of the underlying type. */
2341 if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2343 tree type = TREE_TYPE (array);
2344 tree type1;
2346 if (TREE_CODE (index) == INTEGER_CST)
2347 if (!host_integerp (index, 1)
2348 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2349 >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2350 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2352 c_common_mark_addressable_vec (array);
2353 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2354 type = build_pointer_type (type);
2355 type1 = build_pointer_type (TREE_TYPE (array));
2356 array = build1 (ADDR_EXPR, type1, array);
2357 array = convert (type, array);
2360 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2362 tree rval, type;
2364 /* An array that is indexed by a non-constant
2365 cannot be stored in a register; we must be able to do
2366 address arithmetic on its address.
2367 Likewise an array of elements of variable size. */
2368 if (TREE_CODE (index) != INTEGER_CST
2369 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2370 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2372 if (!c_mark_addressable (array))
2373 return error_mark_node;
2375 /* An array that is indexed by a constant value which is not within
2376 the array bounds cannot be stored in a register either; because we
2377 would get a crash in store_bit_field/extract_bit_field when trying
2378 to access a non-existent part of the register. */
2379 if (TREE_CODE (index) == INTEGER_CST
2380 && TYPE_DOMAIN (TREE_TYPE (array))
2381 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2383 if (!c_mark_addressable (array))
2384 return error_mark_node;
2387 if (pedantic)
2389 tree foo = array;
2390 while (TREE_CODE (foo) == COMPONENT_REF)
2391 foo = TREE_OPERAND (foo, 0);
2392 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2393 pedwarn (loc, OPT_pedantic,
2394 "ISO C forbids subscripting %<register%> array");
2395 else if (!flag_isoc99 && !lvalue_p (foo))
2396 pedwarn (loc, OPT_pedantic,
2397 "ISO C90 forbids subscripting non-lvalue array");
2400 type = TREE_TYPE (TREE_TYPE (array));
2401 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2402 /* Array ref is const/volatile if the array elements are
2403 or if the array is. */
2404 TREE_READONLY (rval)
2405 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2406 | TREE_READONLY (array));
2407 TREE_SIDE_EFFECTS (rval)
2408 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2409 | TREE_SIDE_EFFECTS (array));
2410 TREE_THIS_VOLATILE (rval)
2411 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2412 /* This was added by rms on 16 Nov 91.
2413 It fixes vol struct foo *a; a->elts[1]
2414 in an inline function.
2415 Hope it doesn't break something else. */
2416 | TREE_THIS_VOLATILE (array));
2417 ret = require_complete_type (rval);
2418 protected_set_expr_location (ret, loc);
2419 return ret;
2421 else
2423 tree ar = default_conversion (array);
2425 if (ar == error_mark_node)
2426 return ar;
2428 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2429 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2431 return build_indirect_ref
2432 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2433 RO_ARRAY_INDEXING);
2437 /* Build an external reference to identifier ID. FUN indicates
2438 whether this will be used for a function call. LOC is the source
2439 location of the identifier. This sets *TYPE to the type of the
2440 identifier, which is not the same as the type of the returned value
2441 for CONST_DECLs defined as enum constants. If the type of the
2442 identifier is not available, *TYPE is set to NULL. */
2443 tree
2444 build_external_ref (location_t loc, tree id, int fun, tree *type)
2446 tree ref;
2447 tree decl = lookup_name (id);
2449 /* In Objective-C, an instance variable (ivar) may be preferred to
2450 whatever lookup_name() found. */
2451 decl = objc_lookup_ivar (decl, id);
2453 *type = NULL;
2454 if (decl && decl != error_mark_node)
2456 ref = decl;
2457 *type = TREE_TYPE (ref);
2459 else if (fun)
2460 /* Implicit function declaration. */
2461 ref = implicitly_declare (loc, id);
2462 else if (decl == error_mark_node)
2463 /* Don't complain about something that's already been
2464 complained about. */
2465 return error_mark_node;
2466 else
2468 undeclared_variable (loc, id);
2469 return error_mark_node;
2472 if (TREE_TYPE (ref) == error_mark_node)
2473 return error_mark_node;
2475 if (TREE_DEPRECATED (ref))
2476 warn_deprecated_use (ref, NULL_TREE);
2478 /* Recursive call does not count as usage. */
2479 if (ref != current_function_decl)
2481 TREE_USED (ref) = 1;
2484 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2486 if (!in_sizeof && !in_typeof)
2487 C_DECL_USED (ref) = 1;
2488 else if (DECL_INITIAL (ref) == 0
2489 && DECL_EXTERNAL (ref)
2490 && !TREE_PUBLIC (ref))
2491 record_maybe_used_decl (ref);
2494 if (TREE_CODE (ref) == CONST_DECL)
2496 used_types_insert (TREE_TYPE (ref));
2498 if (warn_cxx_compat
2499 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2500 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2502 warning_at (loc, OPT_Wc___compat,
2503 ("enum constant defined in struct or union "
2504 "is not visible in C++"));
2505 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2508 ref = DECL_INITIAL (ref);
2509 TREE_CONSTANT (ref) = 1;
2511 else if (current_function_decl != 0
2512 && !DECL_FILE_SCOPE_P (current_function_decl)
2513 && (TREE_CODE (ref) == VAR_DECL
2514 || TREE_CODE (ref) == PARM_DECL
2515 || TREE_CODE (ref) == FUNCTION_DECL))
2517 tree context = decl_function_context (ref);
2519 if (context != 0 && context != current_function_decl)
2520 DECL_NONLOCAL (ref) = 1;
2522 /* C99 6.7.4p3: An inline definition of a function with external
2523 linkage ... shall not contain a reference to an identifier with
2524 internal linkage. */
2525 else if (current_function_decl != 0
2526 && DECL_DECLARED_INLINE_P (current_function_decl)
2527 && DECL_EXTERNAL (current_function_decl)
2528 && VAR_OR_FUNCTION_DECL_P (ref)
2529 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2530 && ! TREE_PUBLIC (ref)
2531 && DECL_CONTEXT (ref) != current_function_decl)
2532 record_inline_static (loc, current_function_decl, ref,
2533 csi_internal);
2535 return ref;
2538 /* Record details of decls possibly used inside sizeof or typeof. */
2539 struct maybe_used_decl
2541 /* The decl. */
2542 tree decl;
2543 /* The level seen at (in_sizeof + in_typeof). */
2544 int level;
2545 /* The next one at this level or above, or NULL. */
2546 struct maybe_used_decl *next;
2549 static struct maybe_used_decl *maybe_used_decls;
2551 /* Record that DECL, an undefined static function reference seen
2552 inside sizeof or typeof, might be used if the operand of sizeof is
2553 a VLA type or the operand of typeof is a variably modified
2554 type. */
2556 static void
2557 record_maybe_used_decl (tree decl)
2559 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2560 t->decl = decl;
2561 t->level = in_sizeof + in_typeof;
2562 t->next = maybe_used_decls;
2563 maybe_used_decls = t;
2566 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2567 USED is false, just discard them. If it is true, mark them used
2568 (if no longer inside sizeof or typeof) or move them to the next
2569 level up (if still inside sizeof or typeof). */
2571 void
2572 pop_maybe_used (bool used)
2574 struct maybe_used_decl *p = maybe_used_decls;
2575 int cur_level = in_sizeof + in_typeof;
2576 while (p && p->level > cur_level)
2578 if (used)
2580 if (cur_level == 0)
2581 C_DECL_USED (p->decl) = 1;
2582 else
2583 p->level = cur_level;
2585 p = p->next;
2587 if (!used || cur_level == 0)
2588 maybe_used_decls = p;
2591 /* Return the result of sizeof applied to EXPR. */
2593 struct c_expr
2594 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2596 struct c_expr ret;
2597 if (expr.value == error_mark_node)
2599 ret.value = error_mark_node;
2600 ret.original_code = ERROR_MARK;
2601 ret.original_type = NULL;
2602 pop_maybe_used (false);
2604 else
2606 bool expr_const_operands = true;
2607 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2608 &expr_const_operands);
2609 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2610 ret.original_code = ERROR_MARK;
2611 ret.original_type = NULL;
2612 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2614 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2615 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2616 folded_expr, ret.value);
2617 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2618 SET_EXPR_LOCATION (ret.value, loc);
2620 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2622 return ret;
2625 /* Return the result of sizeof applied to T, a structure for the type
2626 name passed to sizeof (rather than the type itself). LOC is the
2627 location of the original expression. */
2629 struct c_expr
2630 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2632 tree type;
2633 struct c_expr ret;
2634 tree type_expr = NULL_TREE;
2635 bool type_expr_const = true;
2636 type = groktypename (t, &type_expr, &type_expr_const);
2637 ret.value = c_sizeof (loc, type);
2638 ret.original_code = ERROR_MARK;
2639 ret.original_type = NULL;
2640 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2641 && c_vla_type_p (type))
2643 /* If the type is a [*] array, it is a VLA but is represented as
2644 having a size of zero. In such a case we must ensure that
2645 the result of sizeof does not get folded to a constant by
2646 c_fully_fold, because if the size is evaluated the result is
2647 not constant and so constraints on zero or negative size
2648 arrays must not be applied when this sizeof call is inside
2649 another array declarator. */
2650 if (!type_expr)
2651 type_expr = integer_zero_node;
2652 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2653 type_expr, ret.value);
2654 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2656 pop_maybe_used (type != error_mark_node
2657 ? C_TYPE_VARIABLE_SIZE (type) : false);
2658 return ret;
2661 /* Build a function call to function FUNCTION with parameters PARAMS.
2662 The function call is at LOC.
2663 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2664 TREE_VALUE of each node is a parameter-expression.
2665 FUNCTION's data type may be a function type or a pointer-to-function. */
2667 tree
2668 build_function_call (location_t loc, tree function, tree params)
2670 VEC(tree,gc) *vec;
2671 tree ret;
2673 vec = VEC_alloc (tree, gc, list_length (params));
2674 for (; params; params = TREE_CHAIN (params))
2675 VEC_quick_push (tree, vec, TREE_VALUE (params));
2676 ret = build_function_call_vec (loc, function, vec, NULL);
2677 VEC_free (tree, gc, vec);
2678 return ret;
2681 /* Build a function call to function FUNCTION with parameters PARAMS.
2682 ORIGTYPES, if not NULL, is a vector of types; each element is
2683 either NULL or the original type of the corresponding element in
2684 PARAMS. The original type may differ from TREE_TYPE of the
2685 parameter for enums. FUNCTION's data type may be a function type
2686 or pointer-to-function. This function changes the elements of
2687 PARAMS. */
2689 tree
2690 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2691 VEC(tree,gc) *origtypes)
2693 tree fntype, fundecl = 0;
2694 tree name = NULL_TREE, result;
2695 tree tem;
2696 int nargs;
2697 tree *argarray;
2700 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2701 STRIP_TYPE_NOPS (function);
2703 /* Convert anything with function type to a pointer-to-function. */
2704 if (TREE_CODE (function) == FUNCTION_DECL)
2706 /* Implement type-directed function overloading for builtins.
2707 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2708 handle all the type checking. The result is a complete expression
2709 that implements this function call. */
2710 tem = resolve_overloaded_builtin (loc, function, params);
2711 if (tem)
2712 return tem;
2714 name = DECL_NAME (function);
2715 fundecl = function;
2717 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2718 function = function_to_pointer_conversion (loc, function);
2720 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2721 expressions, like those used for ObjC messenger dispatches. */
2722 if (!VEC_empty (tree, params))
2723 function = objc_rewrite_function_call (function,
2724 VEC_index (tree, params, 0));
2726 function = c_fully_fold (function, false, NULL);
2728 fntype = TREE_TYPE (function);
2730 if (TREE_CODE (fntype) == ERROR_MARK)
2731 return error_mark_node;
2733 if (!(TREE_CODE (fntype) == POINTER_TYPE
2734 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2736 error_at (loc, "called object %qE is not a function", function);
2737 return error_mark_node;
2740 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2741 current_function_returns_abnormally = 1;
2743 /* fntype now gets the type of function pointed to. */
2744 fntype = TREE_TYPE (fntype);
2746 /* Convert the parameters to the types declared in the
2747 function prototype, or apply default promotions. */
2749 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2750 function, fundecl);
2751 if (nargs < 0)
2752 return error_mark_node;
2754 /* Check that the function is called through a compatible prototype.
2755 If it is not, replace the call by a trap, wrapped up in a compound
2756 expression if necessary. This has the nice side-effect to prevent
2757 the tree-inliner from generating invalid assignment trees which may
2758 blow up in the RTL expander later. */
2759 if (CONVERT_EXPR_P (function)
2760 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2761 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2762 && !comptypes (fntype, TREE_TYPE (tem)))
2764 tree return_type = TREE_TYPE (fntype);
2765 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2766 NULL_TREE);
2767 int i;
2769 /* This situation leads to run-time undefined behavior. We can't,
2770 therefore, simply error unless we can prove that all possible
2771 executions of the program must execute the code. */
2772 if (warning_at (loc, 0, "function called through a non-compatible type"))
2773 /* We can, however, treat "undefined" any way we please.
2774 Call abort to encourage the user to fix the program. */
2775 inform (loc, "if this code is reached, the program will abort");
2776 /* Before the abort, allow the function arguments to exit or
2777 call longjmp. */
2778 for (i = 0; i < nargs; i++)
2779 trap = build2 (COMPOUND_EXPR, void_type_node,
2780 VEC_index (tree, params, i), trap);
2782 if (VOID_TYPE_P (return_type))
2784 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2785 pedwarn (loc, 0,
2786 "function with qualified void return type called");
2787 return trap;
2789 else
2791 tree rhs;
2793 if (AGGREGATE_TYPE_P (return_type))
2794 rhs = build_compound_literal (loc, return_type,
2795 build_constructor (return_type, 0),
2796 false);
2797 else
2798 rhs = build_zero_cst (return_type);
2800 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2801 trap, rhs));
2805 argarray = VEC_address (tree, params);
2807 /* Check that arguments to builtin functions match the expectations. */
2808 if (fundecl
2809 && DECL_BUILT_IN (fundecl)
2810 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2811 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2812 return error_mark_node;
2814 /* Check that the arguments to the function are valid. */
2815 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2816 TYPE_ARG_TYPES (fntype));
2818 if (name != NULL_TREE
2819 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2821 if (require_constant_value)
2822 result =
2823 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2824 function, nargs, argarray);
2825 else
2826 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2827 function, nargs, argarray);
2828 if (TREE_CODE (result) == NOP_EXPR
2829 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2830 STRIP_TYPE_NOPS (result);
2832 else
2833 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2834 function, nargs, argarray);
2836 if (VOID_TYPE_P (TREE_TYPE (result)))
2838 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2839 pedwarn (loc, 0,
2840 "function with qualified void return type called");
2841 return result;
2843 return require_complete_type (result);
2846 /* Convert the argument expressions in the vector VALUES
2847 to the types in the list TYPELIST.
2849 If TYPELIST is exhausted, or when an element has NULL as its type,
2850 perform the default conversions.
2852 ORIGTYPES is the original types of the expressions in VALUES. This
2853 holds the type of enum values which have been converted to integral
2854 types. It may be NULL.
2856 FUNCTION is a tree for the called function. It is used only for
2857 error messages, where it is formatted with %qE.
2859 This is also where warnings about wrong number of args are generated.
2861 Returns the actual number of arguments processed (which may be less
2862 than the length of VALUES in some error situations), or -1 on
2863 failure. */
2865 static int
2866 convert_arguments (tree typelist, VEC(tree,gc) *values,
2867 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2869 tree typetail, val;
2870 unsigned int parmnum;
2871 bool error_args = false;
2872 const bool type_generic = fundecl
2873 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2874 bool type_generic_remove_excess_precision = false;
2875 tree selector;
2877 /* Change pointer to function to the function itself for
2878 diagnostics. */
2879 if (TREE_CODE (function) == ADDR_EXPR
2880 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2881 function = TREE_OPERAND (function, 0);
2883 /* Handle an ObjC selector specially for diagnostics. */
2884 selector = objc_message_selector ();
2886 /* For type-generic built-in functions, determine whether excess
2887 precision should be removed (classification) or not
2888 (comparison). */
2889 if (type_generic
2890 && DECL_BUILT_IN (fundecl)
2891 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2893 switch (DECL_FUNCTION_CODE (fundecl))
2895 case BUILT_IN_ISFINITE:
2896 case BUILT_IN_ISINF:
2897 case BUILT_IN_ISINF_SIGN:
2898 case BUILT_IN_ISNAN:
2899 case BUILT_IN_ISNORMAL:
2900 case BUILT_IN_FPCLASSIFY:
2901 type_generic_remove_excess_precision = true;
2902 break;
2904 default:
2905 type_generic_remove_excess_precision = false;
2906 break;
2910 /* Scan the given expressions and types, producing individual
2911 converted arguments. */
2913 for (typetail = typelist, parmnum = 0;
2914 VEC_iterate (tree, values, parmnum, val);
2915 ++parmnum)
2917 tree type = typetail ? TREE_VALUE (typetail) : 0;
2918 tree valtype = TREE_TYPE (val);
2919 tree rname = function;
2920 int argnum = parmnum + 1;
2921 const char *invalid_func_diag;
2922 bool excess_precision = false;
2923 bool npc;
2924 tree parmval;
2926 if (type == void_type_node)
2928 if (selector)
2929 error_at (input_location,
2930 "too many arguments to method %qE", selector);
2931 else
2932 error_at (input_location,
2933 "too many arguments to function %qE", function);
2935 if (fundecl && !DECL_BUILT_IN (fundecl))
2936 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2937 return parmnum;
2940 if (selector && argnum > 2)
2942 rname = selector;
2943 argnum -= 2;
2946 npc = null_pointer_constant_p (val);
2948 /* If there is excess precision and a prototype, convert once to
2949 the required type rather than converting via the semantic
2950 type. Likewise without a prototype a float value represented
2951 as long double should be converted once to double. But for
2952 type-generic classification functions excess precision must
2953 be removed here. */
2954 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2955 && (type || !type_generic || !type_generic_remove_excess_precision))
2957 val = TREE_OPERAND (val, 0);
2958 excess_precision = true;
2960 val = c_fully_fold (val, false, NULL);
2961 STRIP_TYPE_NOPS (val);
2963 val = require_complete_type (val);
2965 if (type != 0)
2967 /* Formal parm type is specified by a function prototype. */
2969 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2971 error ("type of formal parameter %d is incomplete", parmnum + 1);
2972 parmval = val;
2974 else
2976 tree origtype;
2978 /* Optionally warn about conversions that
2979 differ from the default conversions. */
2980 if (warn_traditional_conversion || warn_traditional)
2982 unsigned int formal_prec = TYPE_PRECISION (type);
2984 if (INTEGRAL_TYPE_P (type)
2985 && TREE_CODE (valtype) == REAL_TYPE)
2986 warning (0, "passing argument %d of %qE as integer "
2987 "rather than floating due to prototype",
2988 argnum, rname);
2989 if (INTEGRAL_TYPE_P (type)
2990 && TREE_CODE (valtype) == COMPLEX_TYPE)
2991 warning (0, "passing argument %d of %qE as integer "
2992 "rather than complex due to prototype",
2993 argnum, rname);
2994 else if (TREE_CODE (type) == COMPLEX_TYPE
2995 && TREE_CODE (valtype) == REAL_TYPE)
2996 warning (0, "passing argument %d of %qE as complex "
2997 "rather than floating due to prototype",
2998 argnum, rname);
2999 else if (TREE_CODE (type) == REAL_TYPE
3000 && INTEGRAL_TYPE_P (valtype))
3001 warning (0, "passing argument %d of %qE as floating "
3002 "rather than integer due to prototype",
3003 argnum, rname);
3004 else if (TREE_CODE (type) == COMPLEX_TYPE
3005 && INTEGRAL_TYPE_P (valtype))
3006 warning (0, "passing argument %d of %qE as complex "
3007 "rather than integer due to prototype",
3008 argnum, rname);
3009 else if (TREE_CODE (type) == REAL_TYPE
3010 && TREE_CODE (valtype) == COMPLEX_TYPE)
3011 warning (0, "passing argument %d of %qE as floating "
3012 "rather than complex due to prototype",
3013 argnum, rname);
3014 /* ??? At some point, messages should be written about
3015 conversions between complex types, but that's too messy
3016 to do now. */
3017 else if (TREE_CODE (type) == REAL_TYPE
3018 && TREE_CODE (valtype) == REAL_TYPE)
3020 /* Warn if any argument is passed as `float',
3021 since without a prototype it would be `double'. */
3022 if (formal_prec == TYPE_PRECISION (float_type_node)
3023 && type != dfloat32_type_node)
3024 warning (0, "passing argument %d of %qE as %<float%> "
3025 "rather than %<double%> due to prototype",
3026 argnum, rname);
3028 /* Warn if mismatch between argument and prototype
3029 for decimal float types. Warn of conversions with
3030 binary float types and of precision narrowing due to
3031 prototype. */
3032 else if (type != valtype
3033 && (type == dfloat32_type_node
3034 || type == dfloat64_type_node
3035 || type == dfloat128_type_node
3036 || valtype == dfloat32_type_node
3037 || valtype == dfloat64_type_node
3038 || valtype == dfloat128_type_node)
3039 && (formal_prec
3040 <= TYPE_PRECISION (valtype)
3041 || (type == dfloat128_type_node
3042 && (valtype
3043 != dfloat64_type_node
3044 && (valtype
3045 != dfloat32_type_node)))
3046 || (type == dfloat64_type_node
3047 && (valtype
3048 != dfloat32_type_node))))
3049 warning (0, "passing argument %d of %qE as %qT "
3050 "rather than %qT due to prototype",
3051 argnum, rname, type, valtype);
3054 /* Detect integer changing in width or signedness.
3055 These warnings are only activated with
3056 -Wtraditional-conversion, not with -Wtraditional. */
3057 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3058 && INTEGRAL_TYPE_P (valtype))
3060 tree would_have_been = default_conversion (val);
3061 tree type1 = TREE_TYPE (would_have_been);
3063 if (TREE_CODE (type) == ENUMERAL_TYPE
3064 && (TYPE_MAIN_VARIANT (type)
3065 == TYPE_MAIN_VARIANT (valtype)))
3066 /* No warning if function asks for enum
3067 and the actual arg is that enum type. */
3069 else if (formal_prec != TYPE_PRECISION (type1))
3070 warning (OPT_Wtraditional_conversion,
3071 "passing argument %d of %qE "
3072 "with different width due to prototype",
3073 argnum, rname);
3074 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3076 /* Don't complain if the formal parameter type
3077 is an enum, because we can't tell now whether
3078 the value was an enum--even the same enum. */
3079 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3081 else if (TREE_CODE (val) == INTEGER_CST
3082 && int_fits_type_p (val, type))
3083 /* Change in signedness doesn't matter
3084 if a constant value is unaffected. */
3086 /* If the value is extended from a narrower
3087 unsigned type, it doesn't matter whether we
3088 pass it as signed or unsigned; the value
3089 certainly is the same either way. */
3090 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3091 && TYPE_UNSIGNED (valtype))
3093 else if (TYPE_UNSIGNED (type))
3094 warning (OPT_Wtraditional_conversion,
3095 "passing argument %d of %qE "
3096 "as unsigned due to prototype",
3097 argnum, rname);
3098 else
3099 warning (OPT_Wtraditional_conversion,
3100 "passing argument %d of %qE "
3101 "as signed due to prototype", argnum, rname);
3105 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3106 sake of better warnings from convert_and_check. */
3107 if (excess_precision)
3108 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3109 origtype = (origtypes == NULL
3110 ? NULL_TREE
3111 : VEC_index (tree, origtypes, parmnum));
3112 parmval = convert_for_assignment (input_location, type, val,
3113 origtype, ic_argpass, npc,
3114 fundecl, function,
3115 parmnum + 1);
3117 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3118 && INTEGRAL_TYPE_P (type)
3119 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3120 parmval = default_conversion (parmval);
3123 else if (TREE_CODE (valtype) == REAL_TYPE
3124 && (TYPE_PRECISION (valtype)
3125 < TYPE_PRECISION (double_type_node))
3126 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3128 if (type_generic)
3129 parmval = val;
3130 else
3132 /* Convert `float' to `double'. */
3133 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3134 warning (OPT_Wdouble_promotion,
3135 "implicit conversion from %qT to %qT when passing "
3136 "argument to function",
3137 valtype, double_type_node);
3138 parmval = convert (double_type_node, val);
3141 else if (excess_precision && !type_generic)
3142 /* A "double" argument with excess precision being passed
3143 without a prototype or in variable arguments. */
3144 parmval = convert (valtype, val);
3145 else if ((invalid_func_diag =
3146 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3148 error (invalid_func_diag);
3149 return -1;
3151 else
3152 /* Convert `short' and `char' to full-size `int'. */
3153 parmval = default_conversion (val);
3155 VEC_replace (tree, values, parmnum, parmval);
3156 if (parmval == error_mark_node)
3157 error_args = true;
3159 if (typetail)
3160 typetail = TREE_CHAIN (typetail);
3163 gcc_assert (parmnum == VEC_length (tree, values));
3165 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3167 error_at (input_location,
3168 "too few arguments to function %qE", function);
3169 if (fundecl && !DECL_BUILT_IN (fundecl))
3170 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3171 return -1;
3174 return error_args ? -1 : (int) parmnum;
3177 /* This is the entry point used by the parser to build unary operators
3178 in the input. CODE, a tree_code, specifies the unary operator, and
3179 ARG is the operand. For unary plus, the C parser currently uses
3180 CONVERT_EXPR for code.
3182 LOC is the location to use for the tree generated.
3185 struct c_expr
3186 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3188 struct c_expr result;
3190 result.value = build_unary_op (loc, code, arg.value, 0);
3191 result.original_code = code;
3192 result.original_type = NULL;
3194 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3195 overflow_warning (loc, result.value);
3197 return result;
3200 /* This is the entry point used by the parser to build binary operators
3201 in the input. CODE, a tree_code, specifies the binary operator, and
3202 ARG1 and ARG2 are the operands. In addition to constructing the
3203 expression, we check for operands that were written with other binary
3204 operators in a way that is likely to confuse the user.
3206 LOCATION is the location of the binary operator. */
3208 struct c_expr
3209 parser_build_binary_op (location_t location, enum tree_code code,
3210 struct c_expr arg1, struct c_expr arg2)
3212 struct c_expr result;
3214 enum tree_code code1 = arg1.original_code;
3215 enum tree_code code2 = arg2.original_code;
3216 tree type1 = (arg1.original_type
3217 ? arg1.original_type
3218 : TREE_TYPE (arg1.value));
3219 tree type2 = (arg2.original_type
3220 ? arg2.original_type
3221 : TREE_TYPE (arg2.value));
3223 result.value = build_binary_op (location, code,
3224 arg1.value, arg2.value, 1);
3225 result.original_code = code;
3226 result.original_type = NULL;
3228 if (TREE_CODE (result.value) == ERROR_MARK)
3229 return result;
3231 if (location != UNKNOWN_LOCATION)
3232 protected_set_expr_location (result.value, location);
3234 /* Check for cases such as x+y<<z which users are likely
3235 to misinterpret. */
3236 if (warn_parentheses)
3237 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3239 if (warn_logical_op)
3240 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3241 code1, arg1.value, code2, arg2.value);
3243 /* Warn about comparisons against string literals, with the exception
3244 of testing for equality or inequality of a string literal with NULL. */
3245 if (code == EQ_EXPR || code == NE_EXPR)
3247 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3248 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3249 warning_at (location, OPT_Waddress,
3250 "comparison with string literal results in unspecified behavior");
3252 else if (TREE_CODE_CLASS (code) == tcc_comparison
3253 && (code1 == STRING_CST || code2 == STRING_CST))
3254 warning_at (location, OPT_Waddress,
3255 "comparison with string literal results in unspecified behavior");
3257 if (TREE_OVERFLOW_P (result.value)
3258 && !TREE_OVERFLOW_P (arg1.value)
3259 && !TREE_OVERFLOW_P (arg2.value))
3260 overflow_warning (location, result.value);
3262 /* Warn about comparisons of different enum types. */
3263 if (warn_enum_compare
3264 && TREE_CODE_CLASS (code) == tcc_comparison
3265 && TREE_CODE (type1) == ENUMERAL_TYPE
3266 && TREE_CODE (type2) == ENUMERAL_TYPE
3267 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3268 warning_at (location, OPT_Wenum_compare,
3269 "comparison between %qT and %qT",
3270 type1, type2);
3272 return result;
3275 /* Return a tree for the difference of pointers OP0 and OP1.
3276 The resulting tree has type int. */
3278 static tree
3279 pointer_diff (location_t loc, tree op0, tree op1)
3281 tree restype = ptrdiff_type_node;
3282 tree result, inttype;
3284 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3285 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3286 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3287 tree con0, con1, lit0, lit1;
3288 tree orig_op1 = op1;
3290 /* If the operands point into different address spaces, we need to
3291 explicitly convert them to pointers into the common address space
3292 before we can subtract the numerical address values. */
3293 if (as0 != as1)
3295 addr_space_t as_common;
3296 tree common_type;
3298 /* Determine the common superset address space. This is guaranteed
3299 to exist because the caller verified that comp_target_types
3300 returned non-zero. */
3301 if (!addr_space_superset (as0, as1, &as_common))
3302 gcc_unreachable ();
3304 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3305 op0 = convert (common_type, op0);
3306 op1 = convert (common_type, op1);
3309 /* Determine integer type to perform computations in. This will usually
3310 be the same as the result type (ptrdiff_t), but may need to be a wider
3311 type if pointers for the address space are wider than ptrdiff_t. */
3312 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3313 inttype = lang_hooks.types.type_for_size
3314 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3315 else
3316 inttype = restype;
3319 if (TREE_CODE (target_type) == VOID_TYPE)
3320 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3321 "pointer of type %<void *%> used in subtraction");
3322 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3323 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3324 "pointer to a function used in subtraction");
3326 /* If the conversion to ptrdiff_type does anything like widening or
3327 converting a partial to an integral mode, we get a convert_expression
3328 that is in the way to do any simplifications.
3329 (fold-const.c doesn't know that the extra bits won't be needed.
3330 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3331 different mode in place.)
3332 So first try to find a common term here 'by hand'; we want to cover
3333 at least the cases that occur in legal static initializers. */
3334 if (CONVERT_EXPR_P (op0)
3335 && (TYPE_PRECISION (TREE_TYPE (op0))
3336 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3337 con0 = TREE_OPERAND (op0, 0);
3338 else
3339 con0 = op0;
3340 if (CONVERT_EXPR_P (op1)
3341 && (TYPE_PRECISION (TREE_TYPE (op1))
3342 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3343 con1 = TREE_OPERAND (op1, 0);
3344 else
3345 con1 = op1;
3347 if (TREE_CODE (con0) == PLUS_EXPR)
3349 lit0 = TREE_OPERAND (con0, 1);
3350 con0 = TREE_OPERAND (con0, 0);
3352 else
3353 lit0 = integer_zero_node;
3355 if (TREE_CODE (con1) == PLUS_EXPR)
3357 lit1 = TREE_OPERAND (con1, 1);
3358 con1 = TREE_OPERAND (con1, 0);
3360 else
3361 lit1 = integer_zero_node;
3363 if (operand_equal_p (con0, con1, 0))
3365 op0 = lit0;
3366 op1 = lit1;
3370 /* First do the subtraction as integers;
3371 then drop through to build the divide operator.
3372 Do not do default conversions on the minus operator
3373 in case restype is a short type. */
3375 op0 = build_binary_op (loc,
3376 MINUS_EXPR, convert (inttype, op0),
3377 convert (inttype, op1), 0);
3378 /* This generates an error if op1 is pointer to incomplete type. */
3379 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3380 error_at (loc, "arithmetic on pointer to an incomplete type");
3382 /* This generates an error if op0 is pointer to incomplete type. */
3383 op1 = c_size_in_bytes (target_type);
3385 /* Divide by the size, in easiest possible way. */
3386 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3387 op0, convert (inttype, op1));
3389 /* Convert to final result type if necessary. */
3390 return convert (restype, result);
3393 /* Construct and perhaps optimize a tree representation
3394 for a unary operation. CODE, a tree_code, specifies the operation
3395 and XARG is the operand.
3396 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3397 the default promotions (such as from short to int).
3398 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3399 allows non-lvalues; this is only used to handle conversion of non-lvalue
3400 arrays to pointers in C99.
3402 LOCATION is the location of the operator. */
3404 tree
3405 build_unary_op (location_t location,
3406 enum tree_code code, tree xarg, int flag)
3408 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3409 tree arg = xarg;
3410 tree argtype = 0;
3411 enum tree_code typecode;
3412 tree val;
3413 tree ret = error_mark_node;
3414 tree eptype = NULL_TREE;
3415 int noconvert = flag;
3416 const char *invalid_op_diag;
3417 bool int_operands;
3419 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3420 if (int_operands)
3421 arg = remove_c_maybe_const_expr (arg);
3423 if (code != ADDR_EXPR)
3424 arg = require_complete_type (arg);
3426 typecode = TREE_CODE (TREE_TYPE (arg));
3427 if (typecode == ERROR_MARK)
3428 return error_mark_node;
3429 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3430 typecode = INTEGER_TYPE;
3432 if ((invalid_op_diag
3433 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3435 error_at (location, invalid_op_diag);
3436 return error_mark_node;
3439 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3441 eptype = TREE_TYPE (arg);
3442 arg = TREE_OPERAND (arg, 0);
3445 switch (code)
3447 case CONVERT_EXPR:
3448 /* This is used for unary plus, because a CONVERT_EXPR
3449 is enough to prevent anybody from looking inside for
3450 associativity, but won't generate any code. */
3451 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3452 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3453 || typecode == VECTOR_TYPE))
3455 error_at (location, "wrong type argument to unary plus");
3456 return error_mark_node;
3458 else if (!noconvert)
3459 arg = default_conversion (arg);
3460 arg = non_lvalue_loc (location, arg);
3461 break;
3463 case NEGATE_EXPR:
3464 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3465 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3466 || typecode == VECTOR_TYPE))
3468 error_at (location, "wrong type argument to unary minus");
3469 return error_mark_node;
3471 else if (!noconvert)
3472 arg = default_conversion (arg);
3473 break;
3475 case BIT_NOT_EXPR:
3476 /* ~ works on integer types and non float vectors. */
3477 if (typecode == INTEGER_TYPE
3478 || (typecode == VECTOR_TYPE
3479 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3481 if (!noconvert)
3482 arg = default_conversion (arg);
3484 else if (typecode == COMPLEX_TYPE)
3486 code = CONJ_EXPR;
3487 pedwarn (location, OPT_pedantic,
3488 "ISO C does not support %<~%> for complex conjugation");
3489 if (!noconvert)
3490 arg = default_conversion (arg);
3492 else
3494 error_at (location, "wrong type argument to bit-complement");
3495 return error_mark_node;
3497 break;
3499 case ABS_EXPR:
3500 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3502 error_at (location, "wrong type argument to abs");
3503 return error_mark_node;
3505 else if (!noconvert)
3506 arg = default_conversion (arg);
3507 break;
3509 case CONJ_EXPR:
3510 /* Conjugating a real value is a no-op, but allow it anyway. */
3511 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3512 || typecode == COMPLEX_TYPE))
3514 error_at (location, "wrong type argument to conjugation");
3515 return error_mark_node;
3517 else if (!noconvert)
3518 arg = default_conversion (arg);
3519 break;
3521 case TRUTH_NOT_EXPR:
3522 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3523 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3524 && typecode != COMPLEX_TYPE)
3526 error_at (location,
3527 "wrong type argument to unary exclamation mark");
3528 return error_mark_node;
3530 arg = c_objc_common_truthvalue_conversion (location, arg);
3531 ret = invert_truthvalue_loc (location, arg);
3532 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3533 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3534 location = EXPR_LOCATION (ret);
3535 goto return_build_unary_op;
3537 case REALPART_EXPR:
3538 case IMAGPART_EXPR:
3539 ret = build_real_imag_expr (location, code, arg);
3540 if (ret == error_mark_node)
3541 return error_mark_node;
3542 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3543 eptype = TREE_TYPE (eptype);
3544 goto return_build_unary_op;
3546 case PREINCREMENT_EXPR:
3547 case POSTINCREMENT_EXPR:
3548 case PREDECREMENT_EXPR:
3549 case POSTDECREMENT_EXPR:
3551 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3553 tree inner = build_unary_op (location, code,
3554 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3555 if (inner == error_mark_node)
3556 return error_mark_node;
3557 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3558 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3559 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3560 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3561 goto return_build_unary_op;
3564 /* Complain about anything that is not a true lvalue. In
3565 Objective-C, skip this check for property_refs. */
3566 if (!objc_is_property_ref (arg)
3567 && !lvalue_or_else (location,
3568 arg, ((code == PREINCREMENT_EXPR
3569 || code == POSTINCREMENT_EXPR)
3570 ? lv_increment
3571 : lv_decrement)))
3572 return error_mark_node;
3574 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3576 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3577 warning_at (location, OPT_Wc___compat,
3578 "increment of enumeration value is invalid in C++");
3579 else
3580 warning_at (location, OPT_Wc___compat,
3581 "decrement of enumeration value is invalid in C++");
3584 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3585 arg = c_fully_fold (arg, false, NULL);
3587 /* Increment or decrement the real part of the value,
3588 and don't change the imaginary part. */
3589 if (typecode == COMPLEX_TYPE)
3591 tree real, imag;
3593 pedwarn (location, OPT_pedantic,
3594 "ISO C does not support %<++%> and %<--%> on complex types");
3596 arg = stabilize_reference (arg);
3597 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3598 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3599 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3600 if (real == error_mark_node || imag == error_mark_node)
3601 return error_mark_node;
3602 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3603 real, imag);
3604 goto return_build_unary_op;
3607 /* Report invalid types. */
3609 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3610 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3612 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3613 error_at (location, "wrong type argument to increment");
3614 else
3615 error_at (location, "wrong type argument to decrement");
3617 return error_mark_node;
3621 tree inc;
3623 argtype = TREE_TYPE (arg);
3625 /* Compute the increment. */
3627 if (typecode == POINTER_TYPE)
3629 /* If pointer target is an undefined struct,
3630 we just cannot know how to do the arithmetic. */
3631 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3633 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3634 error_at (location,
3635 "increment of pointer to unknown structure");
3636 else
3637 error_at (location,
3638 "decrement of pointer to unknown structure");
3640 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3641 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3643 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3644 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3645 "wrong type argument to increment");
3646 else
3647 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3648 "wrong type argument to decrement");
3651 inc = c_size_in_bytes (TREE_TYPE (argtype));
3652 inc = fold_convert_loc (location, sizetype, inc);
3654 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3656 /* For signed fract types, we invert ++ to -- or
3657 -- to ++, and change inc from 1 to -1, because
3658 it is not possible to represent 1 in signed fract constants.
3659 For unsigned fract types, the result always overflows and
3660 we get an undefined (original) or the maximum value. */
3661 if (code == PREINCREMENT_EXPR)
3662 code = PREDECREMENT_EXPR;
3663 else if (code == PREDECREMENT_EXPR)
3664 code = PREINCREMENT_EXPR;
3665 else if (code == POSTINCREMENT_EXPR)
3666 code = POSTDECREMENT_EXPR;
3667 else /* code == POSTDECREMENT_EXPR */
3668 code = POSTINCREMENT_EXPR;
3670 inc = integer_minus_one_node;
3671 inc = convert (argtype, inc);
3673 else
3675 inc = integer_one_node;
3676 inc = convert (argtype, inc);
3679 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3680 need to ask Objective-C to build the increment or decrement
3681 expression for it. */
3682 if (objc_is_property_ref (arg))
3683 return objc_build_incr_expr_for_property_ref (location, code,
3684 arg, inc);
3686 /* Report a read-only lvalue. */
3687 if (TYPE_READONLY (argtype))
3689 readonly_error (arg,
3690 ((code == PREINCREMENT_EXPR
3691 || code == POSTINCREMENT_EXPR)
3692 ? lv_increment : lv_decrement));
3693 return error_mark_node;
3695 else if (TREE_READONLY (arg))
3696 readonly_warning (arg,
3697 ((code == PREINCREMENT_EXPR
3698 || code == POSTINCREMENT_EXPR)
3699 ? lv_increment : lv_decrement));
3701 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3702 val = boolean_increment (code, arg);
3703 else
3704 val = build2 (code, TREE_TYPE (arg), arg, inc);
3705 TREE_SIDE_EFFECTS (val) = 1;
3706 if (TREE_CODE (val) != code)
3707 TREE_NO_WARNING (val) = 1;
3708 ret = val;
3709 goto return_build_unary_op;
3712 case ADDR_EXPR:
3713 /* Note that this operation never does default_conversion. */
3715 /* The operand of unary '&' must be an lvalue (which excludes
3716 expressions of type void), or, in C99, the result of a [] or
3717 unary '*' operator. */
3718 if (VOID_TYPE_P (TREE_TYPE (arg))
3719 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3720 && (TREE_CODE (arg) != INDIRECT_REF
3721 || !flag_isoc99))
3722 pedwarn (location, 0, "taking address of expression of type %<void%>");
3724 /* Let &* cancel out to simplify resulting code. */
3725 if (TREE_CODE (arg) == INDIRECT_REF)
3727 /* Don't let this be an lvalue. */
3728 if (lvalue_p (TREE_OPERAND (arg, 0)))
3729 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3730 ret = TREE_OPERAND (arg, 0);
3731 goto return_build_unary_op;
3734 /* For &x[y], return x+y */
3735 if (TREE_CODE (arg) == ARRAY_REF)
3737 tree op0 = TREE_OPERAND (arg, 0);
3738 if (!c_mark_addressable (op0))
3739 return error_mark_node;
3740 return build_binary_op (location, PLUS_EXPR,
3741 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3742 ? array_to_pointer_conversion (location,
3743 op0)
3744 : op0),
3745 TREE_OPERAND (arg, 1), 1);
3748 /* Anything not already handled and not a true memory reference
3749 or a non-lvalue array is an error. */
3750 else if (typecode != FUNCTION_TYPE && !flag
3751 && !lvalue_or_else (location, arg, lv_addressof))
3752 return error_mark_node;
3754 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3755 folding later. */
3756 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3758 tree inner = build_unary_op (location, code,
3759 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3760 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3761 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3762 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3763 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3764 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3765 goto return_build_unary_op;
3768 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3769 argtype = TREE_TYPE (arg);
3771 /* If the lvalue is const or volatile, merge that into the type
3772 to which the address will point. This should only be needed
3773 for function types. */
3774 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3775 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3777 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3778 int quals = orig_quals;
3780 if (TREE_READONLY (arg))
3781 quals |= TYPE_QUAL_CONST;
3782 if (TREE_THIS_VOLATILE (arg))
3783 quals |= TYPE_QUAL_VOLATILE;
3785 gcc_assert (quals == orig_quals
3786 || TREE_CODE (argtype) == FUNCTION_TYPE);
3788 argtype = c_build_qualified_type (argtype, quals);
3791 if (!c_mark_addressable (arg))
3792 return error_mark_node;
3794 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3795 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3797 argtype = build_pointer_type (argtype);
3799 /* ??? Cope with user tricks that amount to offsetof. Delete this
3800 when we have proper support for integer constant expressions. */
3801 val = get_base_address (arg);
3802 if (val && TREE_CODE (val) == INDIRECT_REF
3803 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3805 tree op0 = fold_convert_loc (location, sizetype,
3806 fold_offsetof (arg, val)), op1;
3808 op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3809 ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0);
3810 goto return_build_unary_op;
3813 val = build1 (ADDR_EXPR, argtype, arg);
3815 ret = val;
3816 goto return_build_unary_op;
3818 default:
3819 gcc_unreachable ();
3822 if (argtype == 0)
3823 argtype = TREE_TYPE (arg);
3824 if (TREE_CODE (arg) == INTEGER_CST)
3825 ret = (require_constant_value
3826 ? fold_build1_initializer_loc (location, code, argtype, arg)
3827 : fold_build1_loc (location, code, argtype, arg));
3828 else
3829 ret = build1 (code, argtype, arg);
3830 return_build_unary_op:
3831 gcc_assert (ret != error_mark_node);
3832 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3833 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3834 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3835 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3836 ret = note_integer_operands (ret);
3837 if (eptype)
3838 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3839 protected_set_expr_location (ret, location);
3840 return ret;
3843 /* Return nonzero if REF is an lvalue valid for this language.
3844 Lvalues can be assigned, unless their type has TYPE_READONLY.
3845 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3847 bool
3848 lvalue_p (const_tree ref)
3850 const enum tree_code code = TREE_CODE (ref);
3852 switch (code)
3854 case REALPART_EXPR:
3855 case IMAGPART_EXPR:
3856 case COMPONENT_REF:
3857 return lvalue_p (TREE_OPERAND (ref, 0));
3859 case C_MAYBE_CONST_EXPR:
3860 return lvalue_p (TREE_OPERAND (ref, 1));
3862 case COMPOUND_LITERAL_EXPR:
3863 case STRING_CST:
3864 return 1;
3866 case INDIRECT_REF:
3867 case ARRAY_REF:
3868 case VAR_DECL:
3869 case PARM_DECL:
3870 case RESULT_DECL:
3871 case ERROR_MARK:
3872 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3873 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3875 case BIND_EXPR:
3876 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3878 default:
3879 return 0;
3883 /* Give a warning for storing in something that is read-only in GCC
3884 terms but not const in ISO C terms. */
3886 static void
3887 readonly_warning (tree arg, enum lvalue_use use)
3889 switch (use)
3891 case lv_assign:
3892 warning (0, "assignment of read-only location %qE", arg);
3893 break;
3894 case lv_increment:
3895 warning (0, "increment of read-only location %qE", arg);
3896 break;
3897 case lv_decrement:
3898 warning (0, "decrement of read-only location %qE", arg);
3899 break;
3900 default:
3901 gcc_unreachable ();
3903 return;
3907 /* Return nonzero if REF is an lvalue valid for this language;
3908 otherwise, print an error message and return zero. USE says
3909 how the lvalue is being used and so selects the error message.
3910 LOCATION is the location at which any error should be reported. */
3912 static int
3913 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
3915 int win = lvalue_p (ref);
3917 if (!win)
3918 lvalue_error (loc, use);
3920 return win;
3923 /* Mark EXP saying that we need to be able to take the
3924 address of it; it should not be allocated in a register.
3925 Returns true if successful. */
3927 bool
3928 c_mark_addressable (tree exp)
3930 tree x = exp;
3932 while (1)
3933 switch (TREE_CODE (x))
3935 case COMPONENT_REF:
3936 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3938 error
3939 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3940 return false;
3943 /* ... fall through ... */
3945 case ADDR_EXPR:
3946 case ARRAY_REF:
3947 case REALPART_EXPR:
3948 case IMAGPART_EXPR:
3949 x = TREE_OPERAND (x, 0);
3950 break;
3952 case COMPOUND_LITERAL_EXPR:
3953 case CONSTRUCTOR:
3954 TREE_ADDRESSABLE (x) = 1;
3955 return true;
3957 case VAR_DECL:
3958 case CONST_DECL:
3959 case PARM_DECL:
3960 case RESULT_DECL:
3961 if (C_DECL_REGISTER (x)
3962 && DECL_NONLOCAL (x))
3964 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3966 error
3967 ("global register variable %qD used in nested function", x);
3968 return false;
3970 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3972 else if (C_DECL_REGISTER (x))
3974 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3975 error ("address of global register variable %qD requested", x);
3976 else
3977 error ("address of register variable %qD requested", x);
3978 return false;
3981 /* drops in */
3982 case FUNCTION_DECL:
3983 TREE_ADDRESSABLE (x) = 1;
3984 /* drops out */
3985 default:
3986 return true;
3990 /* Convert EXPR to TYPE, warning about conversion problems with
3991 constants. SEMANTIC_TYPE is the type this conversion would use
3992 without excess precision. If SEMANTIC_TYPE is NULL, this function
3993 is equivalent to convert_and_check. This function is a wrapper that
3994 handles conversions that may be different than
3995 the usual ones because of excess precision. */
3997 static tree
3998 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4000 if (TREE_TYPE (expr) == type)
4001 return expr;
4003 if (!semantic_type)
4004 return convert_and_check (type, expr);
4006 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4007 && TREE_TYPE (expr) != semantic_type)
4009 /* For integers, we need to check the real conversion, not
4010 the conversion to the excess precision type. */
4011 expr = convert_and_check (semantic_type, expr);
4013 /* Result type is the excess precision type, which should be
4014 large enough, so do not check. */
4015 return convert (type, expr);
4018 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4019 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4020 if folded to an integer constant then the unselected half may
4021 contain arbitrary operations not normally permitted in constant
4022 expressions. Set the location of the expression to LOC. */
4024 tree
4025 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4026 tree op1, tree op1_original_type, tree op2,
4027 tree op2_original_type)
4029 tree type1;
4030 tree type2;
4031 enum tree_code code1;
4032 enum tree_code code2;
4033 tree result_type = NULL;
4034 tree semantic_result_type = NULL;
4035 tree orig_op1 = op1, orig_op2 = op2;
4036 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4037 bool ifexp_int_operands;
4038 tree ret;
4040 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4041 if (op1_int_operands)
4042 op1 = remove_c_maybe_const_expr (op1);
4043 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4044 if (op2_int_operands)
4045 op2 = remove_c_maybe_const_expr (op2);
4046 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4047 if (ifexp_int_operands)
4048 ifexp = remove_c_maybe_const_expr (ifexp);
4050 /* Promote both alternatives. */
4052 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4053 op1 = default_conversion (op1);
4054 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4055 op2 = default_conversion (op2);
4057 if (TREE_CODE (ifexp) == ERROR_MARK
4058 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4059 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4060 return error_mark_node;
4062 type1 = TREE_TYPE (op1);
4063 code1 = TREE_CODE (type1);
4064 type2 = TREE_TYPE (op2);
4065 code2 = TREE_CODE (type2);
4067 /* C90 does not permit non-lvalue arrays in conditional expressions.
4068 In C99 they will be pointers by now. */
4069 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4071 error_at (colon_loc, "non-lvalue array in conditional expression");
4072 return error_mark_node;
4075 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4076 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4077 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4078 || code1 == COMPLEX_TYPE)
4079 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4080 || code2 == COMPLEX_TYPE))
4082 semantic_result_type = c_common_type (type1, type2);
4083 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4085 op1 = TREE_OPERAND (op1, 0);
4086 type1 = TREE_TYPE (op1);
4087 gcc_assert (TREE_CODE (type1) == code1);
4089 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4091 op2 = TREE_OPERAND (op2, 0);
4092 type2 = TREE_TYPE (op2);
4093 gcc_assert (TREE_CODE (type2) == code2);
4097 if (warn_cxx_compat)
4099 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4100 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4102 if (TREE_CODE (t1) == ENUMERAL_TYPE
4103 && TREE_CODE (t2) == ENUMERAL_TYPE
4104 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4105 warning_at (colon_loc, OPT_Wc___compat,
4106 ("different enum types in conditional is "
4107 "invalid in C++: %qT vs %qT"),
4108 t1, t2);
4111 /* Quickly detect the usual case where op1 and op2 have the same type
4112 after promotion. */
4113 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4115 if (type1 == type2)
4116 result_type = type1;
4117 else
4118 result_type = TYPE_MAIN_VARIANT (type1);
4120 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4121 || code1 == COMPLEX_TYPE)
4122 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4123 || code2 == COMPLEX_TYPE))
4125 result_type = c_common_type (type1, type2);
4126 do_warn_double_promotion (result_type, type1, type2,
4127 "implicit conversion from %qT to %qT to "
4128 "match other result of conditional",
4129 colon_loc);
4131 /* If -Wsign-compare, warn here if type1 and type2 have
4132 different signedness. We'll promote the signed to unsigned
4133 and later code won't know it used to be different.
4134 Do this check on the original types, so that explicit casts
4135 will be considered, but default promotions won't. */
4136 if (c_inhibit_evaluation_warnings == 0)
4138 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4139 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4141 if (unsigned_op1 ^ unsigned_op2)
4143 bool ovf;
4145 /* Do not warn if the result type is signed, since the
4146 signed type will only be chosen if it can represent
4147 all the values of the unsigned type. */
4148 if (!TYPE_UNSIGNED (result_type))
4149 /* OK */;
4150 else
4152 bool op1_maybe_const = true;
4153 bool op2_maybe_const = true;
4155 /* Do not warn if the signed quantity is an
4156 unsuffixed integer literal (or some static
4157 constant expression involving such literals) and
4158 it is non-negative. This warning requires the
4159 operands to be folded for best results, so do
4160 that folding in this case even without
4161 warn_sign_compare to avoid warning options
4162 possibly affecting code generation. */
4163 c_inhibit_evaluation_warnings
4164 += (ifexp == truthvalue_false_node);
4165 op1 = c_fully_fold (op1, require_constant_value,
4166 &op1_maybe_const);
4167 c_inhibit_evaluation_warnings
4168 -= (ifexp == truthvalue_false_node);
4170 c_inhibit_evaluation_warnings
4171 += (ifexp == truthvalue_true_node);
4172 op2 = c_fully_fold (op2, require_constant_value,
4173 &op2_maybe_const);
4174 c_inhibit_evaluation_warnings
4175 -= (ifexp == truthvalue_true_node);
4177 if (warn_sign_compare)
4179 if ((unsigned_op2
4180 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4181 || (unsigned_op1
4182 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4183 /* OK */;
4184 else
4185 warning_at (colon_loc, OPT_Wsign_compare,
4186 ("signed and unsigned type in "
4187 "conditional expression"));
4189 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4190 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4191 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4192 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4197 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4199 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4200 pedwarn (colon_loc, OPT_pedantic,
4201 "ISO C forbids conditional expr with only one void side");
4202 result_type = void_type_node;
4204 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4206 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4207 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4208 addr_space_t as_common;
4210 if (comp_target_types (colon_loc, type1, type2))
4211 result_type = common_pointer_type (type1, type2);
4212 else if (null_pointer_constant_p (orig_op1))
4213 result_type = type2;
4214 else if (null_pointer_constant_p (orig_op2))
4215 result_type = type1;
4216 else if (!addr_space_superset (as1, as2, &as_common))
4218 error_at (colon_loc, "pointers to disjoint address spaces "
4219 "used in conditional expression");
4220 return error_mark_node;
4222 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4224 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4225 pedwarn (colon_loc, OPT_pedantic,
4226 "ISO C forbids conditional expr between "
4227 "%<void *%> and function pointer");
4228 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4229 TREE_TYPE (type2)));
4231 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4233 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4234 pedwarn (colon_loc, OPT_pedantic,
4235 "ISO C forbids conditional expr between "
4236 "%<void *%> and function pointer");
4237 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4238 TREE_TYPE (type1)));
4240 /* Objective-C pointer comparisons are a bit more lenient. */
4241 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4242 result_type = objc_common_type (type1, type2);
4243 else
4245 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4247 pedwarn (colon_loc, 0,
4248 "pointer type mismatch in conditional expression");
4249 result_type = build_pointer_type
4250 (build_qualified_type (void_type_node, qual));
4253 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4255 if (!null_pointer_constant_p (orig_op2))
4256 pedwarn (colon_loc, 0,
4257 "pointer/integer type mismatch in conditional expression");
4258 else
4260 op2 = null_pointer_node;
4262 result_type = type1;
4264 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4266 if (!null_pointer_constant_p (orig_op1))
4267 pedwarn (colon_loc, 0,
4268 "pointer/integer type mismatch in conditional expression");
4269 else
4271 op1 = null_pointer_node;
4273 result_type = type2;
4276 if (!result_type)
4278 if (flag_cond_mismatch)
4279 result_type = void_type_node;
4280 else
4282 error_at (colon_loc, "type mismatch in conditional expression");
4283 return error_mark_node;
4287 /* Merge const and volatile flags of the incoming types. */
4288 result_type
4289 = build_type_variant (result_type,
4290 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4291 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4293 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4294 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4296 if (ifexp_bcp && ifexp == truthvalue_true_node)
4298 op2_int_operands = true;
4299 op1 = c_fully_fold (op1, require_constant_value, NULL);
4301 if (ifexp_bcp && ifexp == truthvalue_false_node)
4303 op1_int_operands = true;
4304 op2 = c_fully_fold (op2, require_constant_value, NULL);
4306 int_const = int_operands = (ifexp_int_operands
4307 && op1_int_operands
4308 && op2_int_operands);
4309 if (int_operands)
4311 int_const = ((ifexp == truthvalue_true_node
4312 && TREE_CODE (orig_op1) == INTEGER_CST
4313 && !TREE_OVERFLOW (orig_op1))
4314 || (ifexp == truthvalue_false_node
4315 && TREE_CODE (orig_op2) == INTEGER_CST
4316 && !TREE_OVERFLOW (orig_op2)));
4318 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4319 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4320 else
4322 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4323 if (int_operands)
4324 ret = note_integer_operands (ret);
4326 if (semantic_result_type)
4327 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4329 protected_set_expr_location (ret, colon_loc);
4330 return ret;
4333 /* Return a compound expression that performs two expressions and
4334 returns the value of the second of them.
4336 LOC is the location of the COMPOUND_EXPR. */
4338 tree
4339 build_compound_expr (location_t loc, tree expr1, tree expr2)
4341 bool expr1_int_operands, expr2_int_operands;
4342 tree eptype = NULL_TREE;
4343 tree ret;
4345 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4346 if (expr1_int_operands)
4347 expr1 = remove_c_maybe_const_expr (expr1);
4348 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4349 if (expr2_int_operands)
4350 expr2 = remove_c_maybe_const_expr (expr2);
4352 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4353 expr1 = TREE_OPERAND (expr1, 0);
4354 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4356 eptype = TREE_TYPE (expr2);
4357 expr2 = TREE_OPERAND (expr2, 0);
4360 if (!TREE_SIDE_EFFECTS (expr1))
4362 /* The left-hand operand of a comma expression is like an expression
4363 statement: with -Wunused, we should warn if it doesn't have
4364 any side-effects, unless it was explicitly cast to (void). */
4365 if (warn_unused_value)
4367 if (VOID_TYPE_P (TREE_TYPE (expr1))
4368 && CONVERT_EXPR_P (expr1))
4369 ; /* (void) a, b */
4370 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4371 && TREE_CODE (expr1) == COMPOUND_EXPR
4372 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4373 ; /* (void) a, (void) b, c */
4374 else
4375 warning_at (loc, OPT_Wunused_value,
4376 "left-hand operand of comma expression has no effect");
4380 /* With -Wunused, we should also warn if the left-hand operand does have
4381 side-effects, but computes a value which is not used. For example, in
4382 `foo() + bar(), baz()' the result of the `+' operator is not used,
4383 so we should issue a warning. */
4384 else if (warn_unused_value)
4385 warn_if_unused_value (expr1, loc);
4387 if (expr2 == error_mark_node)
4388 return error_mark_node;
4390 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4392 if (flag_isoc99
4393 && expr1_int_operands
4394 && expr2_int_operands)
4395 ret = note_integer_operands (ret);
4397 if (eptype)
4398 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4400 protected_set_expr_location (ret, loc);
4401 return ret;
4404 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4405 which we are casting. OTYPE is the type of the expression being
4406 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4407 of the cast. -Wcast-qual appeared on the command line. Named
4408 address space qualifiers are not handled here, because they result
4409 in different warnings. */
4411 static void
4412 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4414 tree in_type = type;
4415 tree in_otype = otype;
4416 int added = 0;
4417 int discarded = 0;
4418 bool is_const;
4420 /* Check that the qualifiers on IN_TYPE are a superset of the
4421 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4422 nodes is uninteresting and we stop as soon as we hit a
4423 non-POINTER_TYPE node on either type. */
4426 in_otype = TREE_TYPE (in_otype);
4427 in_type = TREE_TYPE (in_type);
4429 /* GNU C allows cv-qualified function types. 'const' means the
4430 function is very pure, 'volatile' means it can't return. We
4431 need to warn when such qualifiers are added, not when they're
4432 taken away. */
4433 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4434 && TREE_CODE (in_type) == FUNCTION_TYPE)
4435 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4436 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4437 else
4438 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4439 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4441 while (TREE_CODE (in_type) == POINTER_TYPE
4442 && TREE_CODE (in_otype) == POINTER_TYPE);
4444 if (added)
4445 warning_at (loc, OPT_Wcast_qual,
4446 "cast adds %q#v qualifier to function type", added);
4448 if (discarded)
4449 /* There are qualifiers present in IN_OTYPE that are not present
4450 in IN_TYPE. */
4451 warning_at (loc, OPT_Wcast_qual,
4452 "cast discards %q#v qualifier from pointer target type",
4453 discarded);
4455 if (added || discarded)
4456 return;
4458 /* A cast from **T to const **T is unsafe, because it can cause a
4459 const value to be changed with no additional warning. We only
4460 issue this warning if T is the same on both sides, and we only
4461 issue the warning if there are the same number of pointers on
4462 both sides, as otherwise the cast is clearly unsafe anyhow. A
4463 cast is unsafe when a qualifier is added at one level and const
4464 is not present at all outer levels.
4466 To issue this warning, we check at each level whether the cast
4467 adds new qualifiers not already seen. We don't need to special
4468 case function types, as they won't have the same
4469 TYPE_MAIN_VARIANT. */
4471 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4472 return;
4473 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4474 return;
4476 in_type = type;
4477 in_otype = otype;
4478 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4481 in_type = TREE_TYPE (in_type);
4482 in_otype = TREE_TYPE (in_otype);
4483 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4484 && !is_const)
4486 warning_at (loc, OPT_Wcast_qual,
4487 "to be safe all intermediate pointers in cast from "
4488 "%qT to %qT must be %<const%> qualified",
4489 otype, type);
4490 break;
4492 if (is_const)
4493 is_const = TYPE_READONLY (in_type);
4495 while (TREE_CODE (in_type) == POINTER_TYPE);
4498 /* Build an expression representing a cast to type TYPE of expression EXPR.
4499 LOC is the location of the cast-- typically the open paren of the cast. */
4501 tree
4502 build_c_cast (location_t loc, tree type, tree expr)
4504 tree value;
4506 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4507 expr = TREE_OPERAND (expr, 0);
4509 value = expr;
4511 if (type == error_mark_node || expr == error_mark_node)
4512 return error_mark_node;
4514 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4515 only in <protocol> qualifications. But when constructing cast expressions,
4516 the protocols do matter and must be kept around. */
4517 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4518 return build1 (NOP_EXPR, type, expr);
4520 type = TYPE_MAIN_VARIANT (type);
4522 if (TREE_CODE (type) == ARRAY_TYPE)
4524 error_at (loc, "cast specifies array type");
4525 return error_mark_node;
4528 if (TREE_CODE (type) == FUNCTION_TYPE)
4530 error_at (loc, "cast specifies function type");
4531 return error_mark_node;
4534 if (!VOID_TYPE_P (type))
4536 value = require_complete_type (value);
4537 if (value == error_mark_node)
4538 return error_mark_node;
4541 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4543 if (TREE_CODE (type) == RECORD_TYPE
4544 || TREE_CODE (type) == UNION_TYPE)
4545 pedwarn (loc, OPT_pedantic,
4546 "ISO C forbids casting nonscalar to the same type");
4548 else if (TREE_CODE (type) == UNION_TYPE)
4550 tree field;
4552 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4553 if (TREE_TYPE (field) != error_mark_node
4554 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4555 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4556 break;
4558 if (field)
4560 tree t;
4561 bool maybe_const = true;
4563 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4564 t = c_fully_fold (value, false, &maybe_const);
4565 t = build_constructor_single (type, field, t);
4566 if (!maybe_const)
4567 t = c_wrap_maybe_const (t, true);
4568 t = digest_init (loc, type, t,
4569 NULL_TREE, false, true, 0);
4570 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4571 return t;
4573 error_at (loc, "cast to union type from type not present in union");
4574 return error_mark_node;
4576 else
4578 tree otype, ovalue;
4580 if (type == void_type_node)
4582 tree t = build1 (CONVERT_EXPR, type, value);
4583 SET_EXPR_LOCATION (t, loc);
4584 return t;
4587 otype = TREE_TYPE (value);
4589 /* Optionally warn about potentially worrisome casts. */
4590 if (warn_cast_qual
4591 && TREE_CODE (type) == POINTER_TYPE
4592 && TREE_CODE (otype) == POINTER_TYPE)
4593 handle_warn_cast_qual (loc, type, otype);
4595 /* Warn about conversions between pointers to disjoint
4596 address spaces. */
4597 if (TREE_CODE (type) == POINTER_TYPE
4598 && TREE_CODE (otype) == POINTER_TYPE
4599 && !null_pointer_constant_p (value))
4601 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4602 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4603 addr_space_t as_common;
4605 if (!addr_space_superset (as_to, as_from, &as_common))
4607 if (ADDR_SPACE_GENERIC_P (as_from))
4608 warning_at (loc, 0, "cast to %s address space pointer "
4609 "from disjoint generic address space pointer",
4610 c_addr_space_name (as_to));
4612 else if (ADDR_SPACE_GENERIC_P (as_to))
4613 warning_at (loc, 0, "cast to generic address space pointer "
4614 "from disjoint %s address space pointer",
4615 c_addr_space_name (as_from));
4617 else
4618 warning_at (loc, 0, "cast to %s address space pointer "
4619 "from disjoint %s address space pointer",
4620 c_addr_space_name (as_to),
4621 c_addr_space_name (as_from));
4625 /* Warn about possible alignment problems. */
4626 if (STRICT_ALIGNMENT
4627 && TREE_CODE (type) == POINTER_TYPE
4628 && TREE_CODE (otype) == POINTER_TYPE
4629 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4630 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4631 /* Don't warn about opaque types, where the actual alignment
4632 restriction is unknown. */
4633 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4634 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4635 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4636 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4637 warning_at (loc, OPT_Wcast_align,
4638 "cast increases required alignment of target type");
4640 if (TREE_CODE (type) == INTEGER_TYPE
4641 && TREE_CODE (otype) == POINTER_TYPE
4642 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4643 /* Unlike conversion of integers to pointers, where the
4644 warning is disabled for converting constants because
4645 of cases such as SIG_*, warn about converting constant
4646 pointers to integers. In some cases it may cause unwanted
4647 sign extension, and a warning is appropriate. */
4648 warning_at (loc, OPT_Wpointer_to_int_cast,
4649 "cast from pointer to integer of different size");
4651 if (TREE_CODE (value) == CALL_EXPR
4652 && TREE_CODE (type) != TREE_CODE (otype))
4653 warning_at (loc, OPT_Wbad_function_cast,
4654 "cast from function call of type %qT "
4655 "to non-matching type %qT", otype, type);
4657 if (TREE_CODE (type) == POINTER_TYPE
4658 && TREE_CODE (otype) == INTEGER_TYPE
4659 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4660 /* Don't warn about converting any constant. */
4661 && !TREE_CONSTANT (value))
4662 warning_at (loc,
4663 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4664 "of different size");
4666 if (warn_strict_aliasing <= 2)
4667 strict_aliasing_warning (otype, type, expr);
4669 /* If pedantic, warn for conversions between function and object
4670 pointer types, except for converting a null pointer constant
4671 to function pointer type. */
4672 if (pedantic
4673 && TREE_CODE (type) == POINTER_TYPE
4674 && TREE_CODE (otype) == POINTER_TYPE
4675 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4676 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4677 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4678 "conversion of function pointer to object pointer type");
4680 if (pedantic
4681 && TREE_CODE (type) == POINTER_TYPE
4682 && TREE_CODE (otype) == POINTER_TYPE
4683 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4684 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4685 && !null_pointer_constant_p (value))
4686 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4687 "conversion of object pointer to function pointer type");
4689 ovalue = value;
4690 value = convert (type, value);
4692 /* Ignore any integer overflow caused by the cast. */
4693 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4695 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4697 if (!TREE_OVERFLOW (value))
4699 /* Avoid clobbering a shared constant. */
4700 value = copy_node (value);
4701 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4704 else if (TREE_OVERFLOW (value))
4705 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4706 value = build_int_cst_wide (TREE_TYPE (value),
4707 TREE_INT_CST_LOW (value),
4708 TREE_INT_CST_HIGH (value));
4712 /* Don't let a cast be an lvalue. */
4713 if (value == expr)
4714 value = non_lvalue_loc (loc, value);
4716 /* Don't allow the results of casting to floating-point or complex
4717 types be confused with actual constants, or casts involving
4718 integer and pointer types other than direct integer-to-integer
4719 and integer-to-pointer be confused with integer constant
4720 expressions and null pointer constants. */
4721 if (TREE_CODE (value) == REAL_CST
4722 || TREE_CODE (value) == COMPLEX_CST
4723 || (TREE_CODE (value) == INTEGER_CST
4724 && !((TREE_CODE (expr) == INTEGER_CST
4725 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4726 || TREE_CODE (expr) == REAL_CST
4727 || TREE_CODE (expr) == COMPLEX_CST)))
4728 value = build1 (NOP_EXPR, type, value);
4730 if (CAN_HAVE_LOCATION_P (value))
4731 SET_EXPR_LOCATION (value, loc);
4732 return value;
4735 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4736 location of the open paren of the cast, or the position of the cast
4737 expr. */
4738 tree
4739 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4741 tree type;
4742 tree type_expr = NULL_TREE;
4743 bool type_expr_const = true;
4744 tree ret;
4745 int saved_wsp = warn_strict_prototypes;
4747 /* This avoids warnings about unprototyped casts on
4748 integers. E.g. "#define SIG_DFL (void(*)())0". */
4749 if (TREE_CODE (expr) == INTEGER_CST)
4750 warn_strict_prototypes = 0;
4751 type = groktypename (type_name, &type_expr, &type_expr_const);
4752 warn_strict_prototypes = saved_wsp;
4754 ret = build_c_cast (loc, type, expr);
4755 if (type_expr)
4757 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4758 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4759 SET_EXPR_LOCATION (ret, loc);
4762 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4763 SET_EXPR_LOCATION (ret, loc);
4765 /* C++ does not permits types to be defined in a cast, but it
4766 allows references to incomplete types. */
4767 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4768 warning_at (loc, OPT_Wc___compat,
4769 "defining a type in a cast is invalid in C++");
4771 return ret;
4774 /* Build an assignment expression of lvalue LHS from value RHS.
4775 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4776 may differ from TREE_TYPE (LHS) for an enum bitfield.
4777 MODIFYCODE is the code for a binary operator that we use
4778 to combine the old value of LHS with RHS to get the new value.
4779 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4780 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4781 which may differ from TREE_TYPE (RHS) for an enum value.
4783 LOCATION is the location of the MODIFYCODE operator.
4784 RHS_LOC is the location of the RHS. */
4786 tree
4787 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4788 enum tree_code modifycode,
4789 location_t rhs_loc, tree rhs, tree rhs_origtype)
4791 tree result;
4792 tree newrhs;
4793 tree rhs_semantic_type = NULL_TREE;
4794 tree lhstype = TREE_TYPE (lhs);
4795 tree olhstype = lhstype;
4796 bool npc;
4798 /* Types that aren't fully specified cannot be used in assignments. */
4799 lhs = require_complete_type (lhs);
4801 /* Avoid duplicate error messages from operands that had errors. */
4802 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4803 return error_mark_node;
4805 /* For ObjC properties, defer this check. */
4806 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
4807 return error_mark_node;
4809 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4811 rhs_semantic_type = TREE_TYPE (rhs);
4812 rhs = TREE_OPERAND (rhs, 0);
4815 newrhs = rhs;
4817 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4819 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4820 lhs_origtype, modifycode, rhs_loc, rhs,
4821 rhs_origtype);
4822 if (inner == error_mark_node)
4823 return error_mark_node;
4824 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4825 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4826 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4827 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4828 protected_set_expr_location (result, location);
4829 return result;
4832 /* If a binary op has been requested, combine the old LHS value with the RHS
4833 producing the value we should actually store into the LHS. */
4835 if (modifycode != NOP_EXPR)
4837 lhs = c_fully_fold (lhs, false, NULL);
4838 lhs = stabilize_reference (lhs);
4839 newrhs = build_binary_op (location,
4840 modifycode, lhs, rhs, 1);
4842 /* The original type of the right hand side is no longer
4843 meaningful. */
4844 rhs_origtype = NULL_TREE;
4847 if (c_dialect_objc ())
4849 /* Check if we are modifying an Objective-C property reference;
4850 if so, we need to generate setter calls. */
4851 result = objc_maybe_build_modify_expr (lhs, newrhs);
4852 if (result)
4853 return result;
4855 /* Else, do the check that we postponed for Objective-C. */
4856 if (!lvalue_or_else (location, lhs, lv_assign))
4857 return error_mark_node;
4860 /* Give an error for storing in something that is 'const'. */
4862 if (TYPE_READONLY (lhstype)
4863 || ((TREE_CODE (lhstype) == RECORD_TYPE
4864 || TREE_CODE (lhstype) == UNION_TYPE)
4865 && C_TYPE_FIELDS_READONLY (lhstype)))
4867 readonly_error (lhs, lv_assign);
4868 return error_mark_node;
4870 else if (TREE_READONLY (lhs))
4871 readonly_warning (lhs, lv_assign);
4873 /* If storing into a structure or union member,
4874 it has probably been given type `int'.
4875 Compute the type that would go with
4876 the actual amount of storage the member occupies. */
4878 if (TREE_CODE (lhs) == COMPONENT_REF
4879 && (TREE_CODE (lhstype) == INTEGER_TYPE
4880 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4881 || TREE_CODE (lhstype) == REAL_TYPE
4882 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4883 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4885 /* If storing in a field that is in actuality a short or narrower than one,
4886 we must store in the field in its actual type. */
4888 if (lhstype != TREE_TYPE (lhs))
4890 lhs = copy_node (lhs);
4891 TREE_TYPE (lhs) = lhstype;
4894 /* Issue -Wc++-compat warnings about an assignment to an enum type
4895 when LHS does not have its original type. This happens for,
4896 e.g., an enum bitfield in a struct. */
4897 if (warn_cxx_compat
4898 && lhs_origtype != NULL_TREE
4899 && lhs_origtype != lhstype
4900 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4902 tree checktype = (rhs_origtype != NULL_TREE
4903 ? rhs_origtype
4904 : TREE_TYPE (rhs));
4905 if (checktype != error_mark_node
4906 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4907 warning_at (location, OPT_Wc___compat,
4908 "enum conversion in assignment is invalid in C++");
4911 /* Convert new value to destination type. Fold it first, then
4912 restore any excess precision information, for the sake of
4913 conversion warnings. */
4915 npc = null_pointer_constant_p (newrhs);
4916 newrhs = c_fully_fold (newrhs, false, NULL);
4917 if (rhs_semantic_type)
4918 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4919 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4920 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4921 if (TREE_CODE (newrhs) == ERROR_MARK)
4922 return error_mark_node;
4924 /* Emit ObjC write barrier, if necessary. */
4925 if (c_dialect_objc () && flag_objc_gc)
4927 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4928 if (result)
4930 protected_set_expr_location (result, location);
4931 return result;
4935 /* Scan operands. */
4937 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4938 TREE_SIDE_EFFECTS (result) = 1;
4939 protected_set_expr_location (result, location);
4941 /* If we got the LHS in a different type for storing in,
4942 convert the result back to the nominal type of LHS
4943 so that the value we return always has the same type
4944 as the LHS argument. */
4946 if (olhstype == TREE_TYPE (result))
4947 return result;
4949 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
4950 ic_assign, false, NULL_TREE, NULL_TREE, 0);
4951 protected_set_expr_location (result, location);
4952 return result;
4955 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
4956 This is used to implement -fplan9-extensions. */
4958 static bool
4959 find_anonymous_field_with_type (tree struct_type, tree type)
4961 tree field;
4962 bool found;
4964 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
4965 || TREE_CODE (struct_type) == UNION_TYPE);
4966 found = false;
4967 for (field = TYPE_FIELDS (struct_type);
4968 field != NULL_TREE;
4969 field = TREE_CHAIN (field))
4971 if (DECL_NAME (field) == NULL
4972 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
4974 if (found)
4975 return false;
4976 found = true;
4978 else if (DECL_NAME (field) == NULL
4979 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
4980 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
4981 && find_anonymous_field_with_type (TREE_TYPE (field), type))
4983 if (found)
4984 return false;
4985 found = true;
4988 return found;
4991 /* RHS is an expression whose type is pointer to struct. If there is
4992 an anonymous field in RHS with type TYPE, then return a pointer to
4993 that field in RHS. This is used with -fplan9-extensions. This
4994 returns NULL if no conversion could be found. */
4996 static tree
4997 convert_to_anonymous_field (location_t location, tree type, tree rhs)
4999 tree rhs_struct_type, lhs_main_type;
5000 tree field, found_field;
5001 bool found_sub_field;
5002 tree ret;
5004 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5005 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5006 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5007 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5009 gcc_assert (POINTER_TYPE_P (type));
5010 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5012 found_field = NULL_TREE;
5013 found_sub_field = false;
5014 for (field = TYPE_FIELDS (rhs_struct_type);
5015 field != NULL_TREE;
5016 field = TREE_CHAIN (field))
5018 if (DECL_NAME (field) != NULL_TREE
5019 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5020 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5021 continue;
5022 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5024 if (found_field != NULL_TREE)
5025 return NULL_TREE;
5026 found_field = field;
5028 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5029 lhs_main_type))
5031 if (found_field != NULL_TREE)
5032 return NULL_TREE;
5033 found_field = field;
5034 found_sub_field = true;
5038 if (found_field == NULL_TREE)
5039 return NULL_TREE;
5041 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5042 build_fold_indirect_ref (rhs), found_field,
5043 NULL_TREE);
5044 ret = build_fold_addr_expr_loc (location, ret);
5046 if (found_sub_field)
5048 ret = convert_to_anonymous_field (location, type, ret);
5049 gcc_assert (ret != NULL_TREE);
5052 return ret;
5055 /* Convert value RHS to type TYPE as preparation for an assignment to
5056 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5057 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5058 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5059 constant before any folding.
5060 The real work of conversion is done by `convert'.
5061 The purpose of this function is to generate error messages
5062 for assignments that are not allowed in C.
5063 ERRTYPE says whether it is argument passing, assignment,
5064 initialization or return.
5066 LOCATION is the location of the RHS.
5067 FUNCTION is a tree for the function being called.
5068 PARMNUM is the number of the argument, for printing in error messages. */
5070 static tree
5071 convert_for_assignment (location_t location, tree type, tree rhs,
5072 tree origtype, enum impl_conv errtype,
5073 bool null_pointer_constant, tree fundecl,
5074 tree function, int parmnum)
5076 enum tree_code codel = TREE_CODE (type);
5077 tree orig_rhs = rhs;
5078 tree rhstype;
5079 enum tree_code coder;
5080 tree rname = NULL_TREE;
5081 bool objc_ok = false;
5083 if (errtype == ic_argpass)
5085 tree selector;
5086 /* Change pointer to function to the function itself for
5087 diagnostics. */
5088 if (TREE_CODE (function) == ADDR_EXPR
5089 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5090 function = TREE_OPERAND (function, 0);
5092 /* Handle an ObjC selector specially for diagnostics. */
5093 selector = objc_message_selector ();
5094 rname = function;
5095 if (selector && parmnum > 2)
5097 rname = selector;
5098 parmnum -= 2;
5102 /* This macro is used to emit diagnostics to ensure that all format
5103 strings are complete sentences, visible to gettext and checked at
5104 compile time. */
5105 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5106 do { \
5107 switch (errtype) \
5109 case ic_argpass: \
5110 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5111 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5112 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5113 "expected %qT but argument is of type %qT", \
5114 type, rhstype); \
5115 break; \
5116 case ic_assign: \
5117 pedwarn (LOCATION, OPT, AS); \
5118 break; \
5119 case ic_init: \
5120 pedwarn_init (LOCATION, OPT, IN); \
5121 break; \
5122 case ic_return: \
5123 pedwarn (LOCATION, OPT, RE); \
5124 break; \
5125 default: \
5126 gcc_unreachable (); \
5128 } while (0)
5130 /* This macro is used to emit diagnostics to ensure that all format
5131 strings are complete sentences, visible to gettext and checked at
5132 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5133 extra parameter to enumerate qualifiers. */
5135 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5136 do { \
5137 switch (errtype) \
5139 case ic_argpass: \
5140 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5141 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5142 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5143 "expected %qT but argument is of type %qT", \
5144 type, rhstype); \
5145 break; \
5146 case ic_assign: \
5147 pedwarn (LOCATION, OPT, AS, QUALS); \
5148 break; \
5149 case ic_init: \
5150 pedwarn (LOCATION, OPT, IN, QUALS); \
5151 break; \
5152 case ic_return: \
5153 pedwarn (LOCATION, OPT, RE, QUALS); \
5154 break; \
5155 default: \
5156 gcc_unreachable (); \
5158 } while (0)
5160 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5161 rhs = TREE_OPERAND (rhs, 0);
5163 rhstype = TREE_TYPE (rhs);
5164 coder = TREE_CODE (rhstype);
5166 if (coder == ERROR_MARK)
5167 return error_mark_node;
5169 if (c_dialect_objc ())
5171 int parmno;
5173 switch (errtype)
5175 case ic_return:
5176 parmno = 0;
5177 break;
5179 case ic_assign:
5180 parmno = -1;
5181 break;
5183 case ic_init:
5184 parmno = -2;
5185 break;
5187 default:
5188 parmno = parmnum;
5189 break;
5192 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5195 if (warn_cxx_compat)
5197 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5198 if (checktype != error_mark_node
5199 && TREE_CODE (type) == ENUMERAL_TYPE
5200 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5202 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5203 G_("enum conversion when passing argument "
5204 "%d of %qE is invalid in C++"),
5205 G_("enum conversion in assignment is "
5206 "invalid in C++"),
5207 G_("enum conversion in initialization is "
5208 "invalid in C++"),
5209 G_("enum conversion in return is "
5210 "invalid in C++"));
5214 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5215 return rhs;
5217 if (coder == VOID_TYPE)
5219 /* Except for passing an argument to an unprototyped function,
5220 this is a constraint violation. When passing an argument to
5221 an unprototyped function, it is compile-time undefined;
5222 making it a constraint in that case was rejected in
5223 DR#252. */
5224 error_at (location, "void value not ignored as it ought to be");
5225 return error_mark_node;
5227 rhs = require_complete_type (rhs);
5228 if (rhs == error_mark_node)
5229 return error_mark_node;
5230 /* A type converts to a reference to it.
5231 This code doesn't fully support references, it's just for the
5232 special case of va_start and va_copy. */
5233 if (codel == REFERENCE_TYPE
5234 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5236 if (!lvalue_p (rhs))
5238 error_at (location, "cannot pass rvalue to reference parameter");
5239 return error_mark_node;
5241 if (!c_mark_addressable (rhs))
5242 return error_mark_node;
5243 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5244 SET_EXPR_LOCATION (rhs, location);
5246 /* We already know that these two types are compatible, but they
5247 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5248 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5249 likely to be va_list, a typedef to __builtin_va_list, which
5250 is different enough that it will cause problems later. */
5251 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5253 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5254 SET_EXPR_LOCATION (rhs, location);
5257 rhs = build1 (NOP_EXPR, type, rhs);
5258 SET_EXPR_LOCATION (rhs, location);
5259 return rhs;
5261 /* Some types can interconvert without explicit casts. */
5262 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5263 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5264 return convert (type, rhs);
5265 /* Arithmetic types all interconvert, and enum is treated like int. */
5266 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5267 || codel == FIXED_POINT_TYPE
5268 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5269 || codel == BOOLEAN_TYPE)
5270 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5271 || coder == FIXED_POINT_TYPE
5272 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5273 || coder == BOOLEAN_TYPE))
5275 tree ret;
5276 bool save = in_late_binary_op;
5277 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5278 in_late_binary_op = true;
5279 ret = convert_and_check (type, orig_rhs);
5280 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5281 in_late_binary_op = save;
5282 return ret;
5285 /* Aggregates in different TUs might need conversion. */
5286 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5287 && codel == coder
5288 && comptypes (type, rhstype))
5289 return convert_and_check (type, rhs);
5291 /* Conversion to a transparent union or record from its member types.
5292 This applies only to function arguments. */
5293 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5294 && TYPE_TRANSPARENT_AGGR (type))
5295 && errtype == ic_argpass)
5297 tree memb, marginal_memb = NULL_TREE;
5299 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5301 tree memb_type = TREE_TYPE (memb);
5303 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5304 TYPE_MAIN_VARIANT (rhstype)))
5305 break;
5307 if (TREE_CODE (memb_type) != POINTER_TYPE)
5308 continue;
5310 if (coder == POINTER_TYPE)
5312 tree ttl = TREE_TYPE (memb_type);
5313 tree ttr = TREE_TYPE (rhstype);
5315 /* Any non-function converts to a [const][volatile] void *
5316 and vice versa; otherwise, targets must be the same.
5317 Meanwhile, the lhs target must have all the qualifiers of
5318 the rhs. */
5319 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5320 || comp_target_types (location, memb_type, rhstype))
5322 /* If this type won't generate any warnings, use it. */
5323 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5324 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5325 && TREE_CODE (ttl) == FUNCTION_TYPE)
5326 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5327 == TYPE_QUALS (ttr))
5328 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5329 == TYPE_QUALS (ttl))))
5330 break;
5332 /* Keep looking for a better type, but remember this one. */
5333 if (!marginal_memb)
5334 marginal_memb = memb;
5338 /* Can convert integer zero to any pointer type. */
5339 if (null_pointer_constant)
5341 rhs = null_pointer_node;
5342 break;
5346 if (memb || marginal_memb)
5348 if (!memb)
5350 /* We have only a marginally acceptable member type;
5351 it needs a warning. */
5352 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5353 tree ttr = TREE_TYPE (rhstype);
5355 /* Const and volatile mean something different for function
5356 types, so the usual warnings are not appropriate. */
5357 if (TREE_CODE (ttr) == FUNCTION_TYPE
5358 && TREE_CODE (ttl) == FUNCTION_TYPE)
5360 /* Because const and volatile on functions are
5361 restrictions that say the function will not do
5362 certain things, it is okay to use a const or volatile
5363 function where an ordinary one is wanted, but not
5364 vice-versa. */
5365 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5366 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5367 WARN_FOR_QUALIFIERS (location, 0,
5368 G_("passing argument %d of %qE "
5369 "makes %q#v qualified function "
5370 "pointer from unqualified"),
5371 G_("assignment makes %q#v qualified "
5372 "function pointer from "
5373 "unqualified"),
5374 G_("initialization makes %q#v qualified "
5375 "function pointer from "
5376 "unqualified"),
5377 G_("return makes %q#v qualified function "
5378 "pointer from unqualified"),
5379 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5381 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5382 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5383 WARN_FOR_QUALIFIERS (location, 0,
5384 G_("passing argument %d of %qE discards "
5385 "%qv qualifier from pointer target type"),
5386 G_("assignment discards %qv qualifier "
5387 "from pointer target type"),
5388 G_("initialization discards %qv qualifier "
5389 "from pointer target type"),
5390 G_("return discards %qv qualifier from "
5391 "pointer target type"),
5392 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5394 memb = marginal_memb;
5397 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5398 pedwarn (location, OPT_pedantic,
5399 "ISO C prohibits argument conversion to union type");
5401 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5402 return build_constructor_single (type, memb, rhs);
5406 /* Conversions among pointers */
5407 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5408 && (coder == codel))
5410 tree ttl = TREE_TYPE (type);
5411 tree ttr = TREE_TYPE (rhstype);
5412 tree mvl = ttl;
5413 tree mvr = ttr;
5414 bool is_opaque_pointer;
5415 int target_cmp = 0; /* Cache comp_target_types () result. */
5416 addr_space_t asl;
5417 addr_space_t asr;
5419 if (TREE_CODE (mvl) != ARRAY_TYPE)
5420 mvl = TYPE_MAIN_VARIANT (mvl);
5421 if (TREE_CODE (mvr) != ARRAY_TYPE)
5422 mvr = TYPE_MAIN_VARIANT (mvr);
5423 /* Opaque pointers are treated like void pointers. */
5424 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5426 /* The Plan 9 compiler permits a pointer to a struct to be
5427 automatically converted into a pointer to an anonymous field
5428 within the struct. */
5429 if (flag_plan9_extensions
5430 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5431 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5432 && mvl != mvr)
5434 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5435 if (new_rhs != NULL_TREE)
5437 rhs = new_rhs;
5438 rhstype = TREE_TYPE (rhs);
5439 coder = TREE_CODE (rhstype);
5440 ttr = TREE_TYPE (rhstype);
5441 mvr = TYPE_MAIN_VARIANT (ttr);
5445 /* C++ does not allow the implicit conversion void* -> T*. However,
5446 for the purpose of reducing the number of false positives, we
5447 tolerate the special case of
5449 int *p = NULL;
5451 where NULL is typically defined in C to be '(void *) 0'. */
5452 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5453 warning_at (location, OPT_Wc___compat,
5454 "request for implicit conversion "
5455 "from %qT to %qT not permitted in C++", rhstype, type);
5457 /* See if the pointers point to incompatible address spaces. */
5458 asl = TYPE_ADDR_SPACE (ttl);
5459 asr = TYPE_ADDR_SPACE (ttr);
5460 if (!null_pointer_constant_p (rhs)
5461 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5463 switch (errtype)
5465 case ic_argpass:
5466 error_at (location, "passing argument %d of %qE from pointer to "
5467 "non-enclosed address space", parmnum, rname);
5468 break;
5469 case ic_assign:
5470 error_at (location, "assignment from pointer to "
5471 "non-enclosed address space");
5472 break;
5473 case ic_init:
5474 error_at (location, "initialization from pointer to "
5475 "non-enclosed address space");
5476 break;
5477 case ic_return:
5478 error_at (location, "return from pointer to "
5479 "non-enclosed address space");
5480 break;
5481 default:
5482 gcc_unreachable ();
5484 return error_mark_node;
5487 /* Check if the right-hand side has a format attribute but the
5488 left-hand side doesn't. */
5489 if (warn_missing_format_attribute
5490 && check_missing_format_attribute (type, rhstype))
5492 switch (errtype)
5494 case ic_argpass:
5495 warning_at (location, OPT_Wmissing_format_attribute,
5496 "argument %d of %qE might be "
5497 "a candidate for a format attribute",
5498 parmnum, rname);
5499 break;
5500 case ic_assign:
5501 warning_at (location, OPT_Wmissing_format_attribute,
5502 "assignment left-hand side might be "
5503 "a candidate for a format attribute");
5504 break;
5505 case ic_init:
5506 warning_at (location, OPT_Wmissing_format_attribute,
5507 "initialization left-hand side might be "
5508 "a candidate for a format attribute");
5509 break;
5510 case ic_return:
5511 warning_at (location, OPT_Wmissing_format_attribute,
5512 "return type might be "
5513 "a candidate for a format attribute");
5514 break;
5515 default:
5516 gcc_unreachable ();
5520 /* Any non-function converts to a [const][volatile] void *
5521 and vice versa; otherwise, targets must be the same.
5522 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5523 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5524 || (target_cmp = comp_target_types (location, type, rhstype))
5525 || is_opaque_pointer
5526 || (c_common_unsigned_type (mvl)
5527 == c_common_unsigned_type (mvr)))
5529 if (pedantic
5530 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5532 (VOID_TYPE_P (ttr)
5533 && !null_pointer_constant
5534 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5535 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5536 G_("ISO C forbids passing argument %d of "
5537 "%qE between function pointer "
5538 "and %<void *%>"),
5539 G_("ISO C forbids assignment between "
5540 "function pointer and %<void *%>"),
5541 G_("ISO C forbids initialization between "
5542 "function pointer and %<void *%>"),
5543 G_("ISO C forbids return between function "
5544 "pointer and %<void *%>"));
5545 /* Const and volatile mean something different for function types,
5546 so the usual warnings are not appropriate. */
5547 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5548 && TREE_CODE (ttl) != FUNCTION_TYPE)
5550 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5551 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5553 WARN_FOR_QUALIFIERS (location, 0,
5554 G_("passing argument %d of %qE discards "
5555 "%qv qualifier from pointer target type"),
5556 G_("assignment discards %qv qualifier "
5557 "from pointer target type"),
5558 G_("initialization discards %qv qualifier "
5559 "from pointer target type"),
5560 G_("return discards %qv qualifier from "
5561 "pointer target type"),
5562 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5564 /* If this is not a case of ignoring a mismatch in signedness,
5565 no warning. */
5566 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5567 || target_cmp)
5569 /* If there is a mismatch, do warn. */
5570 else if (warn_pointer_sign)
5571 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5572 G_("pointer targets in passing argument "
5573 "%d of %qE differ in signedness"),
5574 G_("pointer targets in assignment "
5575 "differ in signedness"),
5576 G_("pointer targets in initialization "
5577 "differ in signedness"),
5578 G_("pointer targets in return differ "
5579 "in signedness"));
5581 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5582 && TREE_CODE (ttr) == FUNCTION_TYPE)
5584 /* Because const and volatile on functions are restrictions
5585 that say the function will not do certain things,
5586 it is okay to use a const or volatile function
5587 where an ordinary one is wanted, but not vice-versa. */
5588 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5589 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5590 WARN_FOR_QUALIFIERS (location, 0,
5591 G_("passing argument %d of %qE makes "
5592 "%q#v qualified function pointer "
5593 "from unqualified"),
5594 G_("assignment makes %q#v qualified function "
5595 "pointer from unqualified"),
5596 G_("initialization makes %q#v qualified "
5597 "function pointer from unqualified"),
5598 G_("return makes %q#v qualified function "
5599 "pointer from unqualified"),
5600 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5603 else
5604 /* Avoid warning about the volatile ObjC EH puts on decls. */
5605 if (!objc_ok)
5606 WARN_FOR_ASSIGNMENT (location, 0,
5607 G_("passing argument %d of %qE from "
5608 "incompatible pointer type"),
5609 G_("assignment from incompatible pointer type"),
5610 G_("initialization from incompatible "
5611 "pointer type"),
5612 G_("return from incompatible pointer type"));
5614 return convert (type, rhs);
5616 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5618 /* ??? This should not be an error when inlining calls to
5619 unprototyped functions. */
5620 error_at (location, "invalid use of non-lvalue array");
5621 return error_mark_node;
5623 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5625 /* An explicit constant 0 can convert to a pointer,
5626 or one that results from arithmetic, even including
5627 a cast to integer type. */
5628 if (!null_pointer_constant)
5629 WARN_FOR_ASSIGNMENT (location, 0,
5630 G_("passing argument %d of %qE makes "
5631 "pointer from integer without a cast"),
5632 G_("assignment makes pointer from integer "
5633 "without a cast"),
5634 G_("initialization makes pointer from "
5635 "integer without a cast"),
5636 G_("return makes pointer from integer "
5637 "without a cast"));
5639 return convert (type, rhs);
5641 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5643 WARN_FOR_ASSIGNMENT (location, 0,
5644 G_("passing argument %d of %qE makes integer "
5645 "from pointer without a cast"),
5646 G_("assignment makes integer from pointer "
5647 "without a cast"),
5648 G_("initialization makes integer from pointer "
5649 "without a cast"),
5650 G_("return makes integer from pointer "
5651 "without a cast"));
5652 return convert (type, rhs);
5654 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5656 tree ret;
5657 bool save = in_late_binary_op;
5658 in_late_binary_op = true;
5659 ret = convert (type, rhs);
5660 in_late_binary_op = save;
5661 return ret;
5664 switch (errtype)
5666 case ic_argpass:
5667 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5668 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5669 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5670 "expected %qT but argument is of type %qT", type, rhstype);
5671 break;
5672 case ic_assign:
5673 error_at (location, "incompatible types when assigning to type %qT from "
5674 "type %qT", type, rhstype);
5675 break;
5676 case ic_init:
5677 error_at (location,
5678 "incompatible types when initializing type %qT using type %qT",
5679 type, rhstype);
5680 break;
5681 case ic_return:
5682 error_at (location,
5683 "incompatible types when returning type %qT but %qT was "
5684 "expected", rhstype, type);
5685 break;
5686 default:
5687 gcc_unreachable ();
5690 return error_mark_node;
5693 /* If VALUE is a compound expr all of whose expressions are constant, then
5694 return its value. Otherwise, return error_mark_node.
5696 This is for handling COMPOUND_EXPRs as initializer elements
5697 which is allowed with a warning when -pedantic is specified. */
5699 static tree
5700 valid_compound_expr_initializer (tree value, tree endtype)
5702 if (TREE_CODE (value) == COMPOUND_EXPR)
5704 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5705 == error_mark_node)
5706 return error_mark_node;
5707 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5708 endtype);
5710 else if (!initializer_constant_valid_p (value, endtype))
5711 return error_mark_node;
5712 else
5713 return value;
5716 /* Perform appropriate conversions on the initial value of a variable,
5717 store it in the declaration DECL,
5718 and print any error messages that are appropriate.
5719 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5720 If the init is invalid, store an ERROR_MARK.
5722 INIT_LOC is the location of the initial value. */
5724 void
5725 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5727 tree value, type;
5728 bool npc = false;
5730 /* If variable's type was invalidly declared, just ignore it. */
5732 type = TREE_TYPE (decl);
5733 if (TREE_CODE (type) == ERROR_MARK)
5734 return;
5736 /* Digest the specified initializer into an expression. */
5738 if (init)
5739 npc = null_pointer_constant_p (init);
5740 value = digest_init (init_loc, type, init, origtype, npc,
5741 true, TREE_STATIC (decl));
5743 /* Store the expression if valid; else report error. */
5745 if (!in_system_header
5746 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5747 warning (OPT_Wtraditional, "traditional C rejects automatic "
5748 "aggregate initialization");
5750 DECL_INITIAL (decl) = value;
5752 /* ANSI wants warnings about out-of-range constant initializers. */
5753 STRIP_TYPE_NOPS (value);
5754 if (TREE_STATIC (decl))
5755 constant_expression_warning (value);
5757 /* Check if we need to set array size from compound literal size. */
5758 if (TREE_CODE (type) == ARRAY_TYPE
5759 && TYPE_DOMAIN (type) == 0
5760 && value != error_mark_node)
5762 tree inside_init = init;
5764 STRIP_TYPE_NOPS (inside_init);
5765 inside_init = fold (inside_init);
5767 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5769 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5771 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5773 /* For int foo[] = (int [3]){1}; we need to set array size
5774 now since later on array initializer will be just the
5775 brace enclosed list of the compound literal. */
5776 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5777 TREE_TYPE (decl) = type;
5778 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5779 layout_type (type);
5780 layout_decl (cldecl, 0);
5786 /* Methods for storing and printing names for error messages. */
5788 /* Implement a spelling stack that allows components of a name to be pushed
5789 and popped. Each element on the stack is this structure. */
5791 struct spelling
5793 int kind;
5794 union
5796 unsigned HOST_WIDE_INT i;
5797 const char *s;
5798 } u;
5801 #define SPELLING_STRING 1
5802 #define SPELLING_MEMBER 2
5803 #define SPELLING_BOUNDS 3
5805 static struct spelling *spelling; /* Next stack element (unused). */
5806 static struct spelling *spelling_base; /* Spelling stack base. */
5807 static int spelling_size; /* Size of the spelling stack. */
5809 /* Macros to save and restore the spelling stack around push_... functions.
5810 Alternative to SAVE_SPELLING_STACK. */
5812 #define SPELLING_DEPTH() (spelling - spelling_base)
5813 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5815 /* Push an element on the spelling stack with type KIND and assign VALUE
5816 to MEMBER. */
5818 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5820 int depth = SPELLING_DEPTH (); \
5822 if (depth >= spelling_size) \
5824 spelling_size += 10; \
5825 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5826 spelling_size); \
5827 RESTORE_SPELLING_DEPTH (depth); \
5830 spelling->kind = (KIND); \
5831 spelling->MEMBER = (VALUE); \
5832 spelling++; \
5835 /* Push STRING on the stack. Printed literally. */
5837 static void
5838 push_string (const char *string)
5840 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5843 /* Push a member name on the stack. Printed as '.' STRING. */
5845 static void
5846 push_member_name (tree decl)
5848 const char *const string
5849 = (DECL_NAME (decl)
5850 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5851 : _("<anonymous>"));
5852 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5855 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5857 static void
5858 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5860 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5863 /* Compute the maximum size in bytes of the printed spelling. */
5865 static int
5866 spelling_length (void)
5868 int size = 0;
5869 struct spelling *p;
5871 for (p = spelling_base; p < spelling; p++)
5873 if (p->kind == SPELLING_BOUNDS)
5874 size += 25;
5875 else
5876 size += strlen (p->u.s) + 1;
5879 return size;
5882 /* Print the spelling to BUFFER and return it. */
5884 static char *
5885 print_spelling (char *buffer)
5887 char *d = buffer;
5888 struct spelling *p;
5890 for (p = spelling_base; p < spelling; p++)
5891 if (p->kind == SPELLING_BOUNDS)
5893 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5894 d += strlen (d);
5896 else
5898 const char *s;
5899 if (p->kind == SPELLING_MEMBER)
5900 *d++ = '.';
5901 for (s = p->u.s; (*d = *s++); d++)
5904 *d++ = '\0';
5905 return buffer;
5908 /* Issue an error message for a bad initializer component.
5909 GMSGID identifies the message.
5910 The component name is taken from the spelling stack. */
5912 void
5913 error_init (const char *gmsgid)
5915 char *ofwhat;
5917 /* The gmsgid may be a format string with %< and %>. */
5918 error (gmsgid);
5919 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5920 if (*ofwhat)
5921 error ("(near initialization for %qs)", ofwhat);
5924 /* Issue a pedantic warning for a bad initializer component. OPT is
5925 the option OPT_* (from options.h) controlling this warning or 0 if
5926 it is unconditionally given. GMSGID identifies the message. The
5927 component name is taken from the spelling stack. */
5929 void
5930 pedwarn_init (location_t location, int opt, const char *gmsgid)
5932 char *ofwhat;
5934 /* The gmsgid may be a format string with %< and %>. */
5935 pedwarn (location, opt, gmsgid);
5936 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5937 if (*ofwhat)
5938 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5941 /* Issue a warning for a bad initializer component.
5943 OPT is the OPT_W* value corresponding to the warning option that
5944 controls this warning. GMSGID identifies the message. The
5945 component name is taken from the spelling stack. */
5947 static void
5948 warning_init (int opt, const char *gmsgid)
5950 char *ofwhat;
5952 /* The gmsgid may be a format string with %< and %>. */
5953 warning (opt, gmsgid);
5954 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5955 if (*ofwhat)
5956 warning (opt, "(near initialization for %qs)", ofwhat);
5959 /* If TYPE is an array type and EXPR is a parenthesized string
5960 constant, warn if pedantic that EXPR is being used to initialize an
5961 object of type TYPE. */
5963 void
5964 maybe_warn_string_init (tree type, struct c_expr expr)
5966 if (pedantic
5967 && TREE_CODE (type) == ARRAY_TYPE
5968 && TREE_CODE (expr.value) == STRING_CST
5969 && expr.original_code != STRING_CST)
5970 pedwarn_init (input_location, OPT_pedantic,
5971 "array initialized from parenthesized string constant");
5974 /* Digest the parser output INIT as an initializer for type TYPE.
5975 Return a C expression of type TYPE to represent the initial value.
5977 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5979 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5981 If INIT is a string constant, STRICT_STRING is true if it is
5982 unparenthesized or we should not warn here for it being parenthesized.
5983 For other types of INIT, STRICT_STRING is not used.
5985 INIT_LOC is the location of the INIT.
5987 REQUIRE_CONSTANT requests an error if non-constant initializers or
5988 elements are seen. */
5990 static tree
5991 digest_init (location_t init_loc, tree type, tree init, tree origtype,
5992 bool null_pointer_constant, bool strict_string,
5993 int require_constant)
5995 enum tree_code code = TREE_CODE (type);
5996 tree inside_init = init;
5997 tree semantic_type = NULL_TREE;
5998 bool maybe_const = true;
6000 if (type == error_mark_node
6001 || !init
6002 || init == error_mark_node
6003 || TREE_TYPE (init) == error_mark_node)
6004 return error_mark_node;
6006 STRIP_TYPE_NOPS (inside_init);
6008 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6010 semantic_type = TREE_TYPE (inside_init);
6011 inside_init = TREE_OPERAND (inside_init, 0);
6013 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6014 inside_init = decl_constant_value_for_optimization (inside_init);
6016 /* Initialization of an array of chars from a string constant
6017 optionally enclosed in braces. */
6019 if (code == ARRAY_TYPE && inside_init
6020 && TREE_CODE (inside_init) == STRING_CST)
6022 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6023 /* Note that an array could be both an array of character type
6024 and an array of wchar_t if wchar_t is signed char or unsigned
6025 char. */
6026 bool char_array = (typ1 == char_type_node
6027 || typ1 == signed_char_type_node
6028 || typ1 == unsigned_char_type_node);
6029 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6030 bool char16_array = !!comptypes (typ1, char16_type_node);
6031 bool char32_array = !!comptypes (typ1, char32_type_node);
6033 if (char_array || wchar_array || char16_array || char32_array)
6035 struct c_expr expr;
6036 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6037 expr.value = inside_init;
6038 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6039 expr.original_type = NULL;
6040 maybe_warn_string_init (type, expr);
6042 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6043 pedwarn_init (init_loc, OPT_pedantic,
6044 "initialization of a flexible array member");
6046 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6047 TYPE_MAIN_VARIANT (type)))
6048 return inside_init;
6050 if (char_array)
6052 if (typ2 != char_type_node)
6054 error_init ("char-array initialized from wide string");
6055 return error_mark_node;
6058 else
6060 if (typ2 == char_type_node)
6062 error_init ("wide character array initialized from non-wide "
6063 "string");
6064 return error_mark_node;
6066 else if (!comptypes(typ1, typ2))
6068 error_init ("wide character array initialized from "
6069 "incompatible wide string");
6070 return error_mark_node;
6074 TREE_TYPE (inside_init) = type;
6075 if (TYPE_DOMAIN (type) != 0
6076 && TYPE_SIZE (type) != 0
6077 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6079 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6081 /* Subtract the size of a single (possibly wide) character
6082 because it's ok to ignore the terminating null char
6083 that is counted in the length of the constant. */
6084 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6085 (len
6086 - (TYPE_PRECISION (typ1)
6087 / BITS_PER_UNIT))))
6088 pedwarn_init (init_loc, 0,
6089 ("initializer-string for array of chars "
6090 "is too long"));
6091 else if (warn_cxx_compat
6092 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6093 warning_at (init_loc, OPT_Wc___compat,
6094 ("initializer-string for array chars "
6095 "is too long for C++"));
6098 return inside_init;
6100 else if (INTEGRAL_TYPE_P (typ1))
6102 error_init ("array of inappropriate type initialized "
6103 "from string constant");
6104 return error_mark_node;
6108 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6109 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6110 below and handle as a constructor. */
6111 if (code == VECTOR_TYPE
6112 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6113 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6114 && TREE_CONSTANT (inside_init))
6116 if (TREE_CODE (inside_init) == VECTOR_CST
6117 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6118 TYPE_MAIN_VARIANT (type)))
6119 return inside_init;
6121 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6123 unsigned HOST_WIDE_INT ix;
6124 tree value;
6125 bool constant_p = true;
6127 /* Iterate through elements and check if all constructor
6128 elements are *_CSTs. */
6129 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6130 if (!CONSTANT_CLASS_P (value))
6132 constant_p = false;
6133 break;
6136 if (constant_p)
6137 return build_vector_from_ctor (type,
6138 CONSTRUCTOR_ELTS (inside_init));
6142 if (warn_sequence_point)
6143 verify_sequence_points (inside_init);
6145 /* Any type can be initialized
6146 from an expression of the same type, optionally with braces. */
6148 if (inside_init && TREE_TYPE (inside_init) != 0
6149 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6150 TYPE_MAIN_VARIANT (type))
6151 || (code == ARRAY_TYPE
6152 && comptypes (TREE_TYPE (inside_init), type))
6153 || (code == VECTOR_TYPE
6154 && comptypes (TREE_TYPE (inside_init), type))
6155 || (code == POINTER_TYPE
6156 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6157 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6158 TREE_TYPE (type)))))
6160 if (code == POINTER_TYPE)
6162 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6164 if (TREE_CODE (inside_init) == STRING_CST
6165 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6166 inside_init = array_to_pointer_conversion
6167 (init_loc, inside_init);
6168 else
6170 error_init ("invalid use of non-lvalue array");
6171 return error_mark_node;
6176 if (code == VECTOR_TYPE)
6177 /* Although the types are compatible, we may require a
6178 conversion. */
6179 inside_init = convert (type, inside_init);
6181 if (require_constant
6182 && (code == VECTOR_TYPE || !flag_isoc99)
6183 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6185 /* As an extension, allow initializing objects with static storage
6186 duration with compound literals (which are then treated just as
6187 the brace enclosed list they contain). Also allow this for
6188 vectors, as we can only assign them with compound literals. */
6189 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6190 inside_init = DECL_INITIAL (decl);
6193 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6194 && TREE_CODE (inside_init) != CONSTRUCTOR)
6196 error_init ("array initialized from non-constant array expression");
6197 return error_mark_node;
6200 /* Compound expressions can only occur here if -pedantic or
6201 -pedantic-errors is specified. In the later case, we always want
6202 an error. In the former case, we simply want a warning. */
6203 if (require_constant && pedantic
6204 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6206 inside_init
6207 = valid_compound_expr_initializer (inside_init,
6208 TREE_TYPE (inside_init));
6209 if (inside_init == error_mark_node)
6210 error_init ("initializer element is not constant");
6211 else
6212 pedwarn_init (init_loc, OPT_pedantic,
6213 "initializer element is not constant");
6214 if (flag_pedantic_errors)
6215 inside_init = error_mark_node;
6217 else if (require_constant
6218 && !initializer_constant_valid_p (inside_init,
6219 TREE_TYPE (inside_init)))
6221 error_init ("initializer element is not constant");
6222 inside_init = error_mark_node;
6224 else if (require_constant && !maybe_const)
6225 pedwarn_init (init_loc, 0,
6226 "initializer element is not a constant expression");
6228 /* Added to enable additional -Wmissing-format-attribute warnings. */
6229 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6230 inside_init = convert_for_assignment (init_loc, type, inside_init,
6231 origtype,
6232 ic_init, null_pointer_constant,
6233 NULL_TREE, NULL_TREE, 0);
6234 return inside_init;
6237 /* Handle scalar types, including conversions. */
6239 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6240 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6241 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6243 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6244 && (TREE_CODE (init) == STRING_CST
6245 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6246 inside_init = init = array_to_pointer_conversion (init_loc, init);
6247 if (semantic_type)
6248 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6249 inside_init);
6250 inside_init
6251 = convert_for_assignment (init_loc, type, inside_init, origtype,
6252 ic_init, null_pointer_constant,
6253 NULL_TREE, NULL_TREE, 0);
6255 /* Check to see if we have already given an error message. */
6256 if (inside_init == error_mark_node)
6258 else if (require_constant && !TREE_CONSTANT (inside_init))
6260 error_init ("initializer element is not constant");
6261 inside_init = error_mark_node;
6263 else if (require_constant
6264 && !initializer_constant_valid_p (inside_init,
6265 TREE_TYPE (inside_init)))
6267 error_init ("initializer element is not computable at load time");
6268 inside_init = error_mark_node;
6270 else if (require_constant && !maybe_const)
6271 pedwarn_init (init_loc, 0,
6272 "initializer element is not a constant expression");
6274 return inside_init;
6277 /* Come here only for records and arrays. */
6279 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6281 error_init ("variable-sized object may not be initialized");
6282 return error_mark_node;
6285 error_init ("invalid initializer");
6286 return error_mark_node;
6289 /* Handle initializers that use braces. */
6291 /* Type of object we are accumulating a constructor for.
6292 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6293 static tree constructor_type;
6295 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6296 left to fill. */
6297 static tree constructor_fields;
6299 /* For an ARRAY_TYPE, this is the specified index
6300 at which to store the next element we get. */
6301 static tree constructor_index;
6303 /* For an ARRAY_TYPE, this is the maximum index. */
6304 static tree constructor_max_index;
6306 /* For a RECORD_TYPE, this is the first field not yet written out. */
6307 static tree constructor_unfilled_fields;
6309 /* For an ARRAY_TYPE, this is the index of the first element
6310 not yet written out. */
6311 static tree constructor_unfilled_index;
6313 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6314 This is so we can generate gaps between fields, when appropriate. */
6315 static tree constructor_bit_index;
6317 /* If we are saving up the elements rather than allocating them,
6318 this is the list of elements so far (in reverse order,
6319 most recent first). */
6320 static VEC(constructor_elt,gc) *constructor_elements;
6322 /* 1 if constructor should be incrementally stored into a constructor chain,
6323 0 if all the elements should be kept in AVL tree. */
6324 static int constructor_incremental;
6326 /* 1 if so far this constructor's elements are all compile-time constants. */
6327 static int constructor_constant;
6329 /* 1 if so far this constructor's elements are all valid address constants. */
6330 static int constructor_simple;
6332 /* 1 if this constructor has an element that cannot be part of a
6333 constant expression. */
6334 static int constructor_nonconst;
6336 /* 1 if this constructor is erroneous so far. */
6337 static int constructor_erroneous;
6339 /* Structure for managing pending initializer elements, organized as an
6340 AVL tree. */
6342 struct init_node
6344 struct init_node *left, *right;
6345 struct init_node *parent;
6346 int balance;
6347 tree purpose;
6348 tree value;
6349 tree origtype;
6352 /* Tree of pending elements at this constructor level.
6353 These are elements encountered out of order
6354 which belong at places we haven't reached yet in actually
6355 writing the output.
6356 Will never hold tree nodes across GC runs. */
6357 static struct init_node *constructor_pending_elts;
6359 /* The SPELLING_DEPTH of this constructor. */
6360 static int constructor_depth;
6362 /* DECL node for which an initializer is being read.
6363 0 means we are reading a constructor expression
6364 such as (struct foo) {...}. */
6365 static tree constructor_decl;
6367 /* Nonzero if this is an initializer for a top-level decl. */
6368 static int constructor_top_level;
6370 /* Nonzero if there were any member designators in this initializer. */
6371 static int constructor_designated;
6373 /* Nesting depth of designator list. */
6374 static int designator_depth;
6376 /* Nonzero if there were diagnosed errors in this designator list. */
6377 static int designator_erroneous;
6380 /* This stack has a level for each implicit or explicit level of
6381 structuring in the initializer, including the outermost one. It
6382 saves the values of most of the variables above. */
6384 struct constructor_range_stack;
6386 struct constructor_stack
6388 struct constructor_stack *next;
6389 tree type;
6390 tree fields;
6391 tree index;
6392 tree max_index;
6393 tree unfilled_index;
6394 tree unfilled_fields;
6395 tree bit_index;
6396 VEC(constructor_elt,gc) *elements;
6397 struct init_node *pending_elts;
6398 int offset;
6399 int depth;
6400 /* If value nonzero, this value should replace the entire
6401 constructor at this level. */
6402 struct c_expr replacement_value;
6403 struct constructor_range_stack *range_stack;
6404 char constant;
6405 char simple;
6406 char nonconst;
6407 char implicit;
6408 char erroneous;
6409 char outer;
6410 char incremental;
6411 char designated;
6414 static struct constructor_stack *constructor_stack;
6416 /* This stack represents designators from some range designator up to
6417 the last designator in the list. */
6419 struct constructor_range_stack
6421 struct constructor_range_stack *next, *prev;
6422 struct constructor_stack *stack;
6423 tree range_start;
6424 tree index;
6425 tree range_end;
6426 tree fields;
6429 static struct constructor_range_stack *constructor_range_stack;
6431 /* This stack records separate initializers that are nested.
6432 Nested initializers can't happen in ANSI C, but GNU C allows them
6433 in cases like { ... (struct foo) { ... } ... }. */
6435 struct initializer_stack
6437 struct initializer_stack *next;
6438 tree decl;
6439 struct constructor_stack *constructor_stack;
6440 struct constructor_range_stack *constructor_range_stack;
6441 VEC(constructor_elt,gc) *elements;
6442 struct spelling *spelling;
6443 struct spelling *spelling_base;
6444 int spelling_size;
6445 char top_level;
6446 char require_constant_value;
6447 char require_constant_elements;
6450 static struct initializer_stack *initializer_stack;
6452 /* Prepare to parse and output the initializer for variable DECL. */
6454 void
6455 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6457 const char *locus;
6458 struct initializer_stack *p = XNEW (struct initializer_stack);
6460 p->decl = constructor_decl;
6461 p->require_constant_value = require_constant_value;
6462 p->require_constant_elements = require_constant_elements;
6463 p->constructor_stack = constructor_stack;
6464 p->constructor_range_stack = constructor_range_stack;
6465 p->elements = constructor_elements;
6466 p->spelling = spelling;
6467 p->spelling_base = spelling_base;
6468 p->spelling_size = spelling_size;
6469 p->top_level = constructor_top_level;
6470 p->next = initializer_stack;
6471 initializer_stack = p;
6473 constructor_decl = decl;
6474 constructor_designated = 0;
6475 constructor_top_level = top_level;
6477 if (decl != 0 && decl != error_mark_node)
6479 require_constant_value = TREE_STATIC (decl);
6480 require_constant_elements
6481 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6482 /* For a scalar, you can always use any value to initialize,
6483 even within braces. */
6484 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6485 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6486 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6487 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6488 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6490 else
6492 require_constant_value = 0;
6493 require_constant_elements = 0;
6494 locus = _("(anonymous)");
6497 constructor_stack = 0;
6498 constructor_range_stack = 0;
6500 missing_braces_mentioned = 0;
6502 spelling_base = 0;
6503 spelling_size = 0;
6504 RESTORE_SPELLING_DEPTH (0);
6506 if (locus)
6507 push_string (locus);
6510 void
6511 finish_init (void)
6513 struct initializer_stack *p = initializer_stack;
6515 /* Free the whole constructor stack of this initializer. */
6516 while (constructor_stack)
6518 struct constructor_stack *q = constructor_stack;
6519 constructor_stack = q->next;
6520 free (q);
6523 gcc_assert (!constructor_range_stack);
6525 /* Pop back to the data of the outer initializer (if any). */
6526 free (spelling_base);
6528 constructor_decl = p->decl;
6529 require_constant_value = p->require_constant_value;
6530 require_constant_elements = p->require_constant_elements;
6531 constructor_stack = p->constructor_stack;
6532 constructor_range_stack = p->constructor_range_stack;
6533 constructor_elements = p->elements;
6534 spelling = p->spelling;
6535 spelling_base = p->spelling_base;
6536 spelling_size = p->spelling_size;
6537 constructor_top_level = p->top_level;
6538 initializer_stack = p->next;
6539 free (p);
6542 /* Call here when we see the initializer is surrounded by braces.
6543 This is instead of a call to push_init_level;
6544 it is matched by a call to pop_init_level.
6546 TYPE is the type to initialize, for a constructor expression.
6547 For an initializer for a decl, TYPE is zero. */
6549 void
6550 really_start_incremental_init (tree type)
6552 struct constructor_stack *p = XNEW (struct constructor_stack);
6554 if (type == 0)
6555 type = TREE_TYPE (constructor_decl);
6557 if (TREE_CODE (type) == VECTOR_TYPE
6558 && TYPE_VECTOR_OPAQUE (type))
6559 error ("opaque vector types cannot be initialized");
6561 p->type = constructor_type;
6562 p->fields = constructor_fields;
6563 p->index = constructor_index;
6564 p->max_index = constructor_max_index;
6565 p->unfilled_index = constructor_unfilled_index;
6566 p->unfilled_fields = constructor_unfilled_fields;
6567 p->bit_index = constructor_bit_index;
6568 p->elements = constructor_elements;
6569 p->constant = constructor_constant;
6570 p->simple = constructor_simple;
6571 p->nonconst = constructor_nonconst;
6572 p->erroneous = constructor_erroneous;
6573 p->pending_elts = constructor_pending_elts;
6574 p->depth = constructor_depth;
6575 p->replacement_value.value = 0;
6576 p->replacement_value.original_code = ERROR_MARK;
6577 p->replacement_value.original_type = NULL;
6578 p->implicit = 0;
6579 p->range_stack = 0;
6580 p->outer = 0;
6581 p->incremental = constructor_incremental;
6582 p->designated = constructor_designated;
6583 p->next = 0;
6584 constructor_stack = p;
6586 constructor_constant = 1;
6587 constructor_simple = 1;
6588 constructor_nonconst = 0;
6589 constructor_depth = SPELLING_DEPTH ();
6590 constructor_elements = 0;
6591 constructor_pending_elts = 0;
6592 constructor_type = type;
6593 constructor_incremental = 1;
6594 constructor_designated = 0;
6595 designator_depth = 0;
6596 designator_erroneous = 0;
6598 if (TREE_CODE (constructor_type) == RECORD_TYPE
6599 || TREE_CODE (constructor_type) == UNION_TYPE)
6601 constructor_fields = TYPE_FIELDS (constructor_type);
6602 /* Skip any nameless bit fields at the beginning. */
6603 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6604 && DECL_NAME (constructor_fields) == 0)
6605 constructor_fields = DECL_CHAIN (constructor_fields);
6607 constructor_unfilled_fields = constructor_fields;
6608 constructor_bit_index = bitsize_zero_node;
6610 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6612 if (TYPE_DOMAIN (constructor_type))
6614 constructor_max_index
6615 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6617 /* Detect non-empty initializations of zero-length arrays. */
6618 if (constructor_max_index == NULL_TREE
6619 && TYPE_SIZE (constructor_type))
6620 constructor_max_index = integer_minus_one_node;
6622 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6623 to initialize VLAs will cause a proper error; avoid tree
6624 checking errors as well by setting a safe value. */
6625 if (constructor_max_index
6626 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6627 constructor_max_index = integer_minus_one_node;
6629 constructor_index
6630 = convert (bitsizetype,
6631 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6633 else
6635 constructor_index = bitsize_zero_node;
6636 constructor_max_index = NULL_TREE;
6639 constructor_unfilled_index = constructor_index;
6641 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6643 /* Vectors are like simple fixed-size arrays. */
6644 constructor_max_index =
6645 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6646 constructor_index = bitsize_zero_node;
6647 constructor_unfilled_index = constructor_index;
6649 else
6651 /* Handle the case of int x = {5}; */
6652 constructor_fields = constructor_type;
6653 constructor_unfilled_fields = constructor_type;
6657 /* Push down into a subobject, for initialization.
6658 If this is for an explicit set of braces, IMPLICIT is 0.
6659 If it is because the next element belongs at a lower level,
6660 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6662 void
6663 push_init_level (int implicit, struct obstack * braced_init_obstack)
6665 struct constructor_stack *p;
6666 tree value = NULL_TREE;
6668 /* If we've exhausted any levels that didn't have braces,
6669 pop them now. If implicit == 1, this will have been done in
6670 process_init_element; do not repeat it here because in the case
6671 of excess initializers for an empty aggregate this leads to an
6672 infinite cycle of popping a level and immediately recreating
6673 it. */
6674 if (implicit != 1)
6676 while (constructor_stack->implicit)
6678 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6679 || TREE_CODE (constructor_type) == UNION_TYPE)
6680 && constructor_fields == 0)
6681 process_init_element (pop_init_level (1, braced_init_obstack),
6682 true, braced_init_obstack);
6683 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6684 && constructor_max_index
6685 && tree_int_cst_lt (constructor_max_index,
6686 constructor_index))
6687 process_init_element (pop_init_level (1, braced_init_obstack),
6688 true, braced_init_obstack);
6689 else
6690 break;
6694 /* Unless this is an explicit brace, we need to preserve previous
6695 content if any. */
6696 if (implicit)
6698 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6699 || TREE_CODE (constructor_type) == UNION_TYPE)
6700 && constructor_fields)
6701 value = find_init_member (constructor_fields, braced_init_obstack);
6702 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6703 value = find_init_member (constructor_index, braced_init_obstack);
6706 p = XNEW (struct constructor_stack);
6707 p->type = constructor_type;
6708 p->fields = constructor_fields;
6709 p->index = constructor_index;
6710 p->max_index = constructor_max_index;
6711 p->unfilled_index = constructor_unfilled_index;
6712 p->unfilled_fields = constructor_unfilled_fields;
6713 p->bit_index = constructor_bit_index;
6714 p->elements = constructor_elements;
6715 p->constant = constructor_constant;
6716 p->simple = constructor_simple;
6717 p->nonconst = constructor_nonconst;
6718 p->erroneous = constructor_erroneous;
6719 p->pending_elts = constructor_pending_elts;
6720 p->depth = constructor_depth;
6721 p->replacement_value.value = 0;
6722 p->replacement_value.original_code = ERROR_MARK;
6723 p->replacement_value.original_type = NULL;
6724 p->implicit = implicit;
6725 p->outer = 0;
6726 p->incremental = constructor_incremental;
6727 p->designated = constructor_designated;
6728 p->next = constructor_stack;
6729 p->range_stack = 0;
6730 constructor_stack = p;
6732 constructor_constant = 1;
6733 constructor_simple = 1;
6734 constructor_nonconst = 0;
6735 constructor_depth = SPELLING_DEPTH ();
6736 constructor_elements = 0;
6737 constructor_incremental = 1;
6738 constructor_designated = 0;
6739 constructor_pending_elts = 0;
6740 if (!implicit)
6742 p->range_stack = constructor_range_stack;
6743 constructor_range_stack = 0;
6744 designator_depth = 0;
6745 designator_erroneous = 0;
6748 /* Don't die if an entire brace-pair level is superfluous
6749 in the containing level. */
6750 if (constructor_type == 0)
6752 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6753 || TREE_CODE (constructor_type) == UNION_TYPE)
6755 /* Don't die if there are extra init elts at the end. */
6756 if (constructor_fields == 0)
6757 constructor_type = 0;
6758 else
6760 constructor_type = TREE_TYPE (constructor_fields);
6761 push_member_name (constructor_fields);
6762 constructor_depth++;
6765 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6767 constructor_type = TREE_TYPE (constructor_type);
6768 push_array_bounds (tree_low_cst (constructor_index, 1));
6769 constructor_depth++;
6772 if (constructor_type == 0)
6774 error_init ("extra brace group at end of initializer");
6775 constructor_fields = 0;
6776 constructor_unfilled_fields = 0;
6777 return;
6780 if (value && TREE_CODE (value) == CONSTRUCTOR)
6782 constructor_constant = TREE_CONSTANT (value);
6783 constructor_simple = TREE_STATIC (value);
6784 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6785 constructor_elements = CONSTRUCTOR_ELTS (value);
6786 if (!VEC_empty (constructor_elt, constructor_elements)
6787 && (TREE_CODE (constructor_type) == RECORD_TYPE
6788 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6789 set_nonincremental_init (braced_init_obstack);
6792 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6794 missing_braces_mentioned = 1;
6795 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6798 if (TREE_CODE (constructor_type) == RECORD_TYPE
6799 || TREE_CODE (constructor_type) == UNION_TYPE)
6801 constructor_fields = TYPE_FIELDS (constructor_type);
6802 /* Skip any nameless bit fields at the beginning. */
6803 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6804 && DECL_NAME (constructor_fields) == 0)
6805 constructor_fields = DECL_CHAIN (constructor_fields);
6807 constructor_unfilled_fields = constructor_fields;
6808 constructor_bit_index = bitsize_zero_node;
6810 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6812 /* Vectors are like simple fixed-size arrays. */
6813 constructor_max_index =
6814 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6815 constructor_index = convert (bitsizetype, integer_zero_node);
6816 constructor_unfilled_index = constructor_index;
6818 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6820 if (TYPE_DOMAIN (constructor_type))
6822 constructor_max_index
6823 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6825 /* Detect non-empty initializations of zero-length arrays. */
6826 if (constructor_max_index == NULL_TREE
6827 && TYPE_SIZE (constructor_type))
6828 constructor_max_index = integer_minus_one_node;
6830 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6831 to initialize VLAs will cause a proper error; avoid tree
6832 checking errors as well by setting a safe value. */
6833 if (constructor_max_index
6834 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6835 constructor_max_index = integer_minus_one_node;
6837 constructor_index
6838 = convert (bitsizetype,
6839 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6841 else
6842 constructor_index = bitsize_zero_node;
6844 constructor_unfilled_index = constructor_index;
6845 if (value && TREE_CODE (value) == STRING_CST)
6847 /* We need to split the char/wchar array into individual
6848 characters, so that we don't have to special case it
6849 everywhere. */
6850 set_nonincremental_init_from_string (value, braced_init_obstack);
6853 else
6855 if (constructor_type != error_mark_node)
6856 warning_init (0, "braces around scalar initializer");
6857 constructor_fields = constructor_type;
6858 constructor_unfilled_fields = constructor_type;
6862 /* At the end of an implicit or explicit brace level,
6863 finish up that level of constructor. If a single expression
6864 with redundant braces initialized that level, return the
6865 c_expr structure for that expression. Otherwise, the original_code
6866 element is set to ERROR_MARK.
6867 If we were outputting the elements as they are read, return 0 as the value
6868 from inner levels (process_init_element ignores that),
6869 but return error_mark_node as the value from the outermost level
6870 (that's what we want to put in DECL_INITIAL).
6871 Otherwise, return a CONSTRUCTOR expression as the value. */
6873 struct c_expr
6874 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6876 struct constructor_stack *p;
6877 struct c_expr ret;
6878 ret.value = 0;
6879 ret.original_code = ERROR_MARK;
6880 ret.original_type = NULL;
6882 if (implicit == 0)
6884 /* When we come to an explicit close brace,
6885 pop any inner levels that didn't have explicit braces. */
6886 while (constructor_stack->implicit)
6888 process_init_element (pop_init_level (1, braced_init_obstack),
6889 true, braced_init_obstack);
6891 gcc_assert (!constructor_range_stack);
6894 /* Now output all pending elements. */
6895 constructor_incremental = 1;
6896 output_pending_init_elements (1, braced_init_obstack);
6898 p = constructor_stack;
6900 /* Error for initializing a flexible array member, or a zero-length
6901 array member in an inappropriate context. */
6902 if (constructor_type && constructor_fields
6903 && TREE_CODE (constructor_type) == ARRAY_TYPE
6904 && TYPE_DOMAIN (constructor_type)
6905 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6907 /* Silently discard empty initializations. The parser will
6908 already have pedwarned for empty brackets. */
6909 if (integer_zerop (constructor_unfilled_index))
6910 constructor_type = NULL_TREE;
6911 else
6913 gcc_assert (!TYPE_SIZE (constructor_type));
6915 if (constructor_depth > 2)
6916 error_init ("initialization of flexible array member in a nested context");
6917 else
6918 pedwarn_init (input_location, OPT_pedantic,
6919 "initialization of a flexible array member");
6921 /* We have already issued an error message for the existence
6922 of a flexible array member not at the end of the structure.
6923 Discard the initializer so that we do not die later. */
6924 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
6925 constructor_type = NULL_TREE;
6929 /* Warn when some struct elements are implicitly initialized to zero. */
6930 if (warn_missing_field_initializers
6931 && constructor_type
6932 && TREE_CODE (constructor_type) == RECORD_TYPE
6933 && constructor_unfilled_fields)
6935 /* Do not warn for flexible array members or zero-length arrays. */
6936 while (constructor_unfilled_fields
6937 && (!DECL_SIZE (constructor_unfilled_fields)
6938 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6939 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
6941 /* Do not warn if this level of the initializer uses member
6942 designators; it is likely to be deliberate. */
6943 if (constructor_unfilled_fields && !constructor_designated)
6945 push_member_name (constructor_unfilled_fields);
6946 warning_init (OPT_Wmissing_field_initializers,
6947 "missing initializer");
6948 RESTORE_SPELLING_DEPTH (constructor_depth);
6952 /* Pad out the end of the structure. */
6953 if (p->replacement_value.value)
6954 /* If this closes a superfluous brace pair,
6955 just pass out the element between them. */
6956 ret = p->replacement_value;
6957 else if (constructor_type == 0)
6959 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6960 && TREE_CODE (constructor_type) != UNION_TYPE
6961 && TREE_CODE (constructor_type) != ARRAY_TYPE
6962 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6964 /* A nonincremental scalar initializer--just return
6965 the element, after verifying there is just one. */
6966 if (VEC_empty (constructor_elt,constructor_elements))
6968 if (!constructor_erroneous)
6969 error_init ("empty scalar initializer");
6970 ret.value = error_mark_node;
6972 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6974 error_init ("extra elements in scalar initializer");
6975 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6977 else
6978 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6980 else
6982 if (constructor_erroneous)
6983 ret.value = error_mark_node;
6984 else
6986 ret.value = build_constructor (constructor_type,
6987 constructor_elements);
6988 if (constructor_constant)
6989 TREE_CONSTANT (ret.value) = 1;
6990 if (constructor_constant && constructor_simple)
6991 TREE_STATIC (ret.value) = 1;
6992 if (constructor_nonconst)
6993 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6997 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6999 if (constructor_nonconst)
7000 ret.original_code = C_MAYBE_CONST_EXPR;
7001 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7002 ret.original_code = ERROR_MARK;
7005 constructor_type = p->type;
7006 constructor_fields = p->fields;
7007 constructor_index = p->index;
7008 constructor_max_index = p->max_index;
7009 constructor_unfilled_index = p->unfilled_index;
7010 constructor_unfilled_fields = p->unfilled_fields;
7011 constructor_bit_index = p->bit_index;
7012 constructor_elements = p->elements;
7013 constructor_constant = p->constant;
7014 constructor_simple = p->simple;
7015 constructor_nonconst = p->nonconst;
7016 constructor_erroneous = p->erroneous;
7017 constructor_incremental = p->incremental;
7018 constructor_designated = p->designated;
7019 constructor_pending_elts = p->pending_elts;
7020 constructor_depth = p->depth;
7021 if (!p->implicit)
7022 constructor_range_stack = p->range_stack;
7023 RESTORE_SPELLING_DEPTH (constructor_depth);
7025 constructor_stack = p->next;
7026 free (p);
7028 if (ret.value == 0 && constructor_stack == 0)
7029 ret.value = error_mark_node;
7030 return ret;
7033 /* Common handling for both array range and field name designators.
7034 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7036 static int
7037 set_designator (int array, struct obstack * braced_init_obstack)
7039 tree subtype;
7040 enum tree_code subcode;
7042 /* Don't die if an entire brace-pair level is superfluous
7043 in the containing level. */
7044 if (constructor_type == 0)
7045 return 1;
7047 /* If there were errors in this designator list already, bail out
7048 silently. */
7049 if (designator_erroneous)
7050 return 1;
7052 if (!designator_depth)
7054 gcc_assert (!constructor_range_stack);
7056 /* Designator list starts at the level of closest explicit
7057 braces. */
7058 while (constructor_stack->implicit)
7060 process_init_element (pop_init_level (1, braced_init_obstack),
7061 true, braced_init_obstack);
7063 constructor_designated = 1;
7064 return 0;
7067 switch (TREE_CODE (constructor_type))
7069 case RECORD_TYPE:
7070 case UNION_TYPE:
7071 subtype = TREE_TYPE (constructor_fields);
7072 if (subtype != error_mark_node)
7073 subtype = TYPE_MAIN_VARIANT (subtype);
7074 break;
7075 case ARRAY_TYPE:
7076 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7077 break;
7078 default:
7079 gcc_unreachable ();
7082 subcode = TREE_CODE (subtype);
7083 if (array && subcode != ARRAY_TYPE)
7085 error_init ("array index in non-array initializer");
7086 return 1;
7088 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7090 error_init ("field name not in record or union initializer");
7091 return 1;
7094 constructor_designated = 1;
7095 push_init_level (2, braced_init_obstack);
7096 return 0;
7099 /* If there are range designators in designator list, push a new designator
7100 to constructor_range_stack. RANGE_END is end of such stack range or
7101 NULL_TREE if there is no range designator at this level. */
7103 static void
7104 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7106 struct constructor_range_stack *p;
7108 p = (struct constructor_range_stack *)
7109 obstack_alloc (braced_init_obstack,
7110 sizeof (struct constructor_range_stack));
7111 p->prev = constructor_range_stack;
7112 p->next = 0;
7113 p->fields = constructor_fields;
7114 p->range_start = constructor_index;
7115 p->index = constructor_index;
7116 p->stack = constructor_stack;
7117 p->range_end = range_end;
7118 if (constructor_range_stack)
7119 constructor_range_stack->next = p;
7120 constructor_range_stack = p;
7123 /* Within an array initializer, specify the next index to be initialized.
7124 FIRST is that index. If LAST is nonzero, then initialize a range
7125 of indices, running from FIRST through LAST. */
7127 void
7128 set_init_index (tree first, tree last,
7129 struct obstack * braced_init_obstack)
7131 if (set_designator (1, braced_init_obstack))
7132 return;
7134 designator_erroneous = 1;
7136 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7137 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7139 error_init ("array index in initializer not of integer type");
7140 return;
7143 if (TREE_CODE (first) != INTEGER_CST)
7145 first = c_fully_fold (first, false, NULL);
7146 if (TREE_CODE (first) == INTEGER_CST)
7147 pedwarn_init (input_location, OPT_pedantic,
7148 "array index in initializer is not "
7149 "an integer constant expression");
7152 if (last && TREE_CODE (last) != INTEGER_CST)
7154 last = c_fully_fold (last, false, NULL);
7155 if (TREE_CODE (last) == INTEGER_CST)
7156 pedwarn_init (input_location, OPT_pedantic,
7157 "array index in initializer is not "
7158 "an integer constant expression");
7161 if (TREE_CODE (first) != INTEGER_CST)
7162 error_init ("nonconstant array index in initializer");
7163 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7164 error_init ("nonconstant array index in initializer");
7165 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7166 error_init ("array index in non-array initializer");
7167 else if (tree_int_cst_sgn (first) == -1)
7168 error_init ("array index in initializer exceeds array bounds");
7169 else if (constructor_max_index
7170 && tree_int_cst_lt (constructor_max_index, first))
7171 error_init ("array index in initializer exceeds array bounds");
7172 else
7174 constant_expression_warning (first);
7175 if (last)
7176 constant_expression_warning (last);
7177 constructor_index = convert (bitsizetype, first);
7179 if (last)
7181 if (tree_int_cst_equal (first, last))
7182 last = 0;
7183 else if (tree_int_cst_lt (last, first))
7185 error_init ("empty index range in initializer");
7186 last = 0;
7188 else
7190 last = convert (bitsizetype, last);
7191 if (constructor_max_index != 0
7192 && tree_int_cst_lt (constructor_max_index, last))
7194 error_init ("array index range in initializer exceeds array bounds");
7195 last = 0;
7200 designator_depth++;
7201 designator_erroneous = 0;
7202 if (constructor_range_stack || last)
7203 push_range_stack (last, braced_init_obstack);
7207 /* Within a struct initializer, specify the next field to be initialized. */
7209 void
7210 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7212 tree field;
7214 if (set_designator (0, braced_init_obstack))
7215 return;
7217 designator_erroneous = 1;
7219 if (TREE_CODE (constructor_type) != RECORD_TYPE
7220 && TREE_CODE (constructor_type) != UNION_TYPE)
7222 error_init ("field name not in record or union initializer");
7223 return;
7226 field = lookup_field (constructor_type, fieldname);
7228 if (field == 0)
7229 error ("unknown field %qE specified in initializer", fieldname);
7230 else
7233 constructor_fields = TREE_VALUE (field);
7234 designator_depth++;
7235 designator_erroneous = 0;
7236 if (constructor_range_stack)
7237 push_range_stack (NULL_TREE, braced_init_obstack);
7238 field = TREE_CHAIN (field);
7239 if (field)
7241 if (set_designator (0, braced_init_obstack))
7242 return;
7245 while (field != NULL_TREE);
7248 /* Add a new initializer to the tree of pending initializers. PURPOSE
7249 identifies the initializer, either array index or field in a structure.
7250 VALUE is the value of that index or field. If ORIGTYPE is not
7251 NULL_TREE, it is the original type of VALUE.
7253 IMPLICIT is true if value comes from pop_init_level (1),
7254 the new initializer has been merged with the existing one
7255 and thus no warnings should be emitted about overriding an
7256 existing initializer. */
7258 static void
7259 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7260 struct obstack * braced_init_obstack)
7262 struct init_node *p, **q, *r;
7264 q = &constructor_pending_elts;
7265 p = 0;
7267 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7269 while (*q != 0)
7271 p = *q;
7272 if (tree_int_cst_lt (purpose, p->purpose))
7273 q = &p->left;
7274 else if (tree_int_cst_lt (p->purpose, purpose))
7275 q = &p->right;
7276 else
7278 if (!implicit)
7280 if (TREE_SIDE_EFFECTS (p->value))
7281 warning_init (0, "initialized field with side-effects overwritten");
7282 else if (warn_override_init)
7283 warning_init (OPT_Woverride_init, "initialized field overwritten");
7285 p->value = value;
7286 p->origtype = origtype;
7287 return;
7291 else
7293 tree bitpos;
7295 bitpos = bit_position (purpose);
7296 while (*q != NULL)
7298 p = *q;
7299 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7300 q = &p->left;
7301 else if (p->purpose != purpose)
7302 q = &p->right;
7303 else
7305 if (!implicit)
7307 if (TREE_SIDE_EFFECTS (p->value))
7308 warning_init (0, "initialized field with side-effects overwritten");
7309 else if (warn_override_init)
7310 warning_init (OPT_Woverride_init, "initialized field overwritten");
7312 p->value = value;
7313 p->origtype = origtype;
7314 return;
7319 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7320 sizeof (struct init_node));
7321 r->purpose = purpose;
7322 r->value = value;
7323 r->origtype = origtype;
7325 *q = r;
7326 r->parent = p;
7327 r->left = 0;
7328 r->right = 0;
7329 r->balance = 0;
7331 while (p)
7333 struct init_node *s;
7335 if (r == p->left)
7337 if (p->balance == 0)
7338 p->balance = -1;
7339 else if (p->balance < 0)
7341 if (r->balance < 0)
7343 /* L rotation. */
7344 p->left = r->right;
7345 if (p->left)
7346 p->left->parent = p;
7347 r->right = p;
7349 p->balance = 0;
7350 r->balance = 0;
7352 s = p->parent;
7353 p->parent = r;
7354 r->parent = s;
7355 if (s)
7357 if (s->left == p)
7358 s->left = r;
7359 else
7360 s->right = r;
7362 else
7363 constructor_pending_elts = r;
7365 else
7367 /* LR rotation. */
7368 struct init_node *t = r->right;
7370 r->right = t->left;
7371 if (r->right)
7372 r->right->parent = r;
7373 t->left = r;
7375 p->left = t->right;
7376 if (p->left)
7377 p->left->parent = p;
7378 t->right = p;
7380 p->balance = t->balance < 0;
7381 r->balance = -(t->balance > 0);
7382 t->balance = 0;
7384 s = p->parent;
7385 p->parent = t;
7386 r->parent = t;
7387 t->parent = s;
7388 if (s)
7390 if (s->left == p)
7391 s->left = t;
7392 else
7393 s->right = t;
7395 else
7396 constructor_pending_elts = t;
7398 break;
7400 else
7402 /* p->balance == +1; growth of left side balances the node. */
7403 p->balance = 0;
7404 break;
7407 else /* r == p->right */
7409 if (p->balance == 0)
7410 /* Growth propagation from right side. */
7411 p->balance++;
7412 else if (p->balance > 0)
7414 if (r->balance > 0)
7416 /* R rotation. */
7417 p->right = r->left;
7418 if (p->right)
7419 p->right->parent = p;
7420 r->left = p;
7422 p->balance = 0;
7423 r->balance = 0;
7425 s = p->parent;
7426 p->parent = r;
7427 r->parent = s;
7428 if (s)
7430 if (s->left == p)
7431 s->left = r;
7432 else
7433 s->right = r;
7435 else
7436 constructor_pending_elts = r;
7438 else /* r->balance == -1 */
7440 /* RL rotation */
7441 struct init_node *t = r->left;
7443 r->left = t->right;
7444 if (r->left)
7445 r->left->parent = r;
7446 t->right = r;
7448 p->right = t->left;
7449 if (p->right)
7450 p->right->parent = p;
7451 t->left = p;
7453 r->balance = (t->balance < 0);
7454 p->balance = -(t->balance > 0);
7455 t->balance = 0;
7457 s = p->parent;
7458 p->parent = t;
7459 r->parent = t;
7460 t->parent = s;
7461 if (s)
7463 if (s->left == p)
7464 s->left = t;
7465 else
7466 s->right = t;
7468 else
7469 constructor_pending_elts = t;
7471 break;
7473 else
7475 /* p->balance == -1; growth of right side balances the node. */
7476 p->balance = 0;
7477 break;
7481 r = p;
7482 p = p->parent;
7486 /* Build AVL tree from a sorted chain. */
7488 static void
7489 set_nonincremental_init (struct obstack * braced_init_obstack)
7491 unsigned HOST_WIDE_INT ix;
7492 tree index, value;
7494 if (TREE_CODE (constructor_type) != RECORD_TYPE
7495 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7496 return;
7498 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7500 add_pending_init (index, value, NULL_TREE, false,
7501 braced_init_obstack);
7503 constructor_elements = 0;
7504 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7506 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7507 /* Skip any nameless bit fields at the beginning. */
7508 while (constructor_unfilled_fields != 0
7509 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7510 && DECL_NAME (constructor_unfilled_fields) == 0)
7511 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7514 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7516 if (TYPE_DOMAIN (constructor_type))
7517 constructor_unfilled_index
7518 = convert (bitsizetype,
7519 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7520 else
7521 constructor_unfilled_index = bitsize_zero_node;
7523 constructor_incremental = 0;
7526 /* Build AVL tree from a string constant. */
7528 static void
7529 set_nonincremental_init_from_string (tree str,
7530 struct obstack * braced_init_obstack)
7532 tree value, purpose, type;
7533 HOST_WIDE_INT val[2];
7534 const char *p, *end;
7535 int byte, wchar_bytes, charwidth, bitpos;
7537 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7539 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7540 charwidth = TYPE_PRECISION (char_type_node);
7541 type = TREE_TYPE (constructor_type);
7542 p = TREE_STRING_POINTER (str);
7543 end = p + TREE_STRING_LENGTH (str);
7545 for (purpose = bitsize_zero_node;
7546 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7547 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7549 if (wchar_bytes == 1)
7551 val[1] = (unsigned char) *p++;
7552 val[0] = 0;
7554 else
7556 val[0] = 0;
7557 val[1] = 0;
7558 for (byte = 0; byte < wchar_bytes; byte++)
7560 if (BYTES_BIG_ENDIAN)
7561 bitpos = (wchar_bytes - byte - 1) * charwidth;
7562 else
7563 bitpos = byte * charwidth;
7564 val[bitpos < HOST_BITS_PER_WIDE_INT]
7565 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7566 << (bitpos % HOST_BITS_PER_WIDE_INT);
7570 if (!TYPE_UNSIGNED (type))
7572 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7573 if (bitpos < HOST_BITS_PER_WIDE_INT)
7575 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7577 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7578 val[0] = -1;
7581 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7583 if (val[1] < 0)
7584 val[0] = -1;
7586 else if (val[0] & (((HOST_WIDE_INT) 1)
7587 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7588 val[0] |= ((HOST_WIDE_INT) -1)
7589 << (bitpos - HOST_BITS_PER_WIDE_INT);
7592 value = build_int_cst_wide (type, val[1], val[0]);
7593 add_pending_init (purpose, value, NULL_TREE, false,
7594 braced_init_obstack);
7597 constructor_incremental = 0;
7600 /* Return value of FIELD in pending initializer or zero if the field was
7601 not initialized yet. */
7603 static tree
7604 find_init_member (tree field, struct obstack * braced_init_obstack)
7606 struct init_node *p;
7608 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7610 if (constructor_incremental
7611 && tree_int_cst_lt (field, constructor_unfilled_index))
7612 set_nonincremental_init (braced_init_obstack);
7614 p = constructor_pending_elts;
7615 while (p)
7617 if (tree_int_cst_lt (field, p->purpose))
7618 p = p->left;
7619 else if (tree_int_cst_lt (p->purpose, field))
7620 p = p->right;
7621 else
7622 return p->value;
7625 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7627 tree bitpos = bit_position (field);
7629 if (constructor_incremental
7630 && (!constructor_unfilled_fields
7631 || tree_int_cst_lt (bitpos,
7632 bit_position (constructor_unfilled_fields))))
7633 set_nonincremental_init (braced_init_obstack);
7635 p = constructor_pending_elts;
7636 while (p)
7638 if (field == p->purpose)
7639 return p->value;
7640 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7641 p = p->left;
7642 else
7643 p = p->right;
7646 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7648 if (!VEC_empty (constructor_elt, constructor_elements)
7649 && (VEC_last (constructor_elt, constructor_elements)->index
7650 == field))
7651 return VEC_last (constructor_elt, constructor_elements)->value;
7653 return 0;
7656 /* "Output" the next constructor element.
7657 At top level, really output it to assembler code now.
7658 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7659 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7660 TYPE is the data type that the containing data type wants here.
7661 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7662 If VALUE is a string constant, STRICT_STRING is true if it is
7663 unparenthesized or we should not warn here for it being parenthesized.
7664 For other types of VALUE, STRICT_STRING is not used.
7666 PENDING if non-nil means output pending elements that belong
7667 right after this element. (PENDING is normally 1;
7668 it is 0 while outputting pending elements, to avoid recursion.)
7670 IMPLICIT is true if value comes from pop_init_level (1),
7671 the new initializer has been merged with the existing one
7672 and thus no warnings should be emitted about overriding an
7673 existing initializer. */
7675 static void
7676 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7677 tree field, int pending, bool implicit,
7678 struct obstack * braced_init_obstack)
7680 tree semantic_type = NULL_TREE;
7681 constructor_elt *celt;
7682 bool maybe_const = true;
7683 bool npc;
7685 if (type == error_mark_node || value == error_mark_node)
7687 constructor_erroneous = 1;
7688 return;
7690 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7691 && (TREE_CODE (value) == STRING_CST
7692 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7693 && !(TREE_CODE (value) == STRING_CST
7694 && TREE_CODE (type) == ARRAY_TYPE
7695 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7696 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7697 TYPE_MAIN_VARIANT (type)))
7698 value = array_to_pointer_conversion (input_location, value);
7700 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7701 && require_constant_value && !flag_isoc99 && pending)
7703 /* As an extension, allow initializing objects with static storage
7704 duration with compound literals (which are then treated just as
7705 the brace enclosed list they contain). */
7706 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7707 value = DECL_INITIAL (decl);
7710 npc = null_pointer_constant_p (value);
7711 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7713 semantic_type = TREE_TYPE (value);
7714 value = TREE_OPERAND (value, 0);
7716 value = c_fully_fold (value, require_constant_value, &maybe_const);
7718 if (value == error_mark_node)
7719 constructor_erroneous = 1;
7720 else if (!TREE_CONSTANT (value))
7721 constructor_constant = 0;
7722 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7723 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7724 || TREE_CODE (constructor_type) == UNION_TYPE)
7725 && DECL_C_BIT_FIELD (field)
7726 && TREE_CODE (value) != INTEGER_CST))
7727 constructor_simple = 0;
7728 if (!maybe_const)
7729 constructor_nonconst = 1;
7731 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7733 if (require_constant_value)
7735 error_init ("initializer element is not constant");
7736 value = error_mark_node;
7738 else if (require_constant_elements)
7739 pedwarn (input_location, 0,
7740 "initializer element is not computable at load time");
7742 else if (!maybe_const
7743 && (require_constant_value || require_constant_elements))
7744 pedwarn_init (input_location, 0,
7745 "initializer element is not a constant expression");
7747 /* Issue -Wc++-compat warnings about initializing a bitfield with
7748 enum type. */
7749 if (warn_cxx_compat
7750 && field != NULL_TREE
7751 && TREE_CODE (field) == FIELD_DECL
7752 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7753 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7754 != TYPE_MAIN_VARIANT (type))
7755 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7757 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7758 if (checktype != error_mark_node
7759 && (TYPE_MAIN_VARIANT (checktype)
7760 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7761 warning_init (OPT_Wc___compat,
7762 "enum conversion in initialization is invalid in C++");
7765 /* If this field is empty (and not at the end of structure),
7766 don't do anything other than checking the initializer. */
7767 if (field
7768 && (TREE_TYPE (field) == error_mark_node
7769 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7770 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7771 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7772 || DECL_CHAIN (field)))))
7773 return;
7775 if (semantic_type)
7776 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7777 value = digest_init (input_location, type, value, origtype, npc,
7778 strict_string, require_constant_value);
7779 if (value == error_mark_node)
7781 constructor_erroneous = 1;
7782 return;
7784 if (require_constant_value || require_constant_elements)
7785 constant_expression_warning (value);
7787 /* If this element doesn't come next in sequence,
7788 put it on constructor_pending_elts. */
7789 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7790 && (!constructor_incremental
7791 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7793 if (constructor_incremental
7794 && tree_int_cst_lt (field, constructor_unfilled_index))
7795 set_nonincremental_init (braced_init_obstack);
7797 add_pending_init (field, value, origtype, implicit,
7798 braced_init_obstack);
7799 return;
7801 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7802 && (!constructor_incremental
7803 || field != constructor_unfilled_fields))
7805 /* We do this for records but not for unions. In a union,
7806 no matter which field is specified, it can be initialized
7807 right away since it starts at the beginning of the union. */
7808 if (constructor_incremental)
7810 if (!constructor_unfilled_fields)
7811 set_nonincremental_init (braced_init_obstack);
7812 else
7814 tree bitpos, unfillpos;
7816 bitpos = bit_position (field);
7817 unfillpos = bit_position (constructor_unfilled_fields);
7819 if (tree_int_cst_lt (bitpos, unfillpos))
7820 set_nonincremental_init (braced_init_obstack);
7824 add_pending_init (field, value, origtype, implicit,
7825 braced_init_obstack);
7826 return;
7828 else if (TREE_CODE (constructor_type) == UNION_TYPE
7829 && !VEC_empty (constructor_elt, constructor_elements))
7831 if (!implicit)
7833 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7834 constructor_elements)->value))
7835 warning_init (0,
7836 "initialized field with side-effects overwritten");
7837 else if (warn_override_init)
7838 warning_init (OPT_Woverride_init, "initialized field overwritten");
7841 /* We can have just one union field set. */
7842 constructor_elements = 0;
7845 /* Otherwise, output this element either to
7846 constructor_elements or to the assembler file. */
7848 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7849 celt->index = field;
7850 celt->value = value;
7852 /* Advance the variable that indicates sequential elements output. */
7853 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7854 constructor_unfilled_index
7855 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7856 bitsize_one_node);
7857 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7859 constructor_unfilled_fields
7860 = DECL_CHAIN (constructor_unfilled_fields);
7862 /* Skip any nameless bit fields. */
7863 while (constructor_unfilled_fields != 0
7864 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7865 && DECL_NAME (constructor_unfilled_fields) == 0)
7866 constructor_unfilled_fields =
7867 DECL_CHAIN (constructor_unfilled_fields);
7869 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7870 constructor_unfilled_fields = 0;
7872 /* Now output any pending elements which have become next. */
7873 if (pending)
7874 output_pending_init_elements (0, braced_init_obstack);
7877 /* Output any pending elements which have become next.
7878 As we output elements, constructor_unfilled_{fields,index}
7879 advances, which may cause other elements to become next;
7880 if so, they too are output.
7882 If ALL is 0, we return when there are
7883 no more pending elements to output now.
7885 If ALL is 1, we output space as necessary so that
7886 we can output all the pending elements. */
7887 static void
7888 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7890 struct init_node *elt = constructor_pending_elts;
7891 tree next;
7893 retry:
7895 /* Look through the whole pending tree.
7896 If we find an element that should be output now,
7897 output it. Otherwise, set NEXT to the element
7898 that comes first among those still pending. */
7900 next = 0;
7901 while (elt)
7903 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7905 if (tree_int_cst_equal (elt->purpose,
7906 constructor_unfilled_index))
7907 output_init_element (elt->value, elt->origtype, true,
7908 TREE_TYPE (constructor_type),
7909 constructor_unfilled_index, 0, false,
7910 braced_init_obstack);
7911 else if (tree_int_cst_lt (constructor_unfilled_index,
7912 elt->purpose))
7914 /* Advance to the next smaller node. */
7915 if (elt->left)
7916 elt = elt->left;
7917 else
7919 /* We have reached the smallest node bigger than the
7920 current unfilled index. Fill the space first. */
7921 next = elt->purpose;
7922 break;
7925 else
7927 /* Advance to the next bigger node. */
7928 if (elt->right)
7929 elt = elt->right;
7930 else
7932 /* We have reached the biggest node in a subtree. Find
7933 the parent of it, which is the next bigger node. */
7934 while (elt->parent && elt->parent->right == elt)
7935 elt = elt->parent;
7936 elt = elt->parent;
7937 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7938 elt->purpose))
7940 next = elt->purpose;
7941 break;
7946 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7947 || TREE_CODE (constructor_type) == UNION_TYPE)
7949 tree ctor_unfilled_bitpos, elt_bitpos;
7951 /* If the current record is complete we are done. */
7952 if (constructor_unfilled_fields == 0)
7953 break;
7955 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7956 elt_bitpos = bit_position (elt->purpose);
7957 /* We can't compare fields here because there might be empty
7958 fields in between. */
7959 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7961 constructor_unfilled_fields = elt->purpose;
7962 output_init_element (elt->value, elt->origtype, true,
7963 TREE_TYPE (elt->purpose),
7964 elt->purpose, 0, false,
7965 braced_init_obstack);
7967 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7969 /* Advance to the next smaller node. */
7970 if (elt->left)
7971 elt = elt->left;
7972 else
7974 /* We have reached the smallest node bigger than the
7975 current unfilled field. Fill the space first. */
7976 next = elt->purpose;
7977 break;
7980 else
7982 /* Advance to the next bigger node. */
7983 if (elt->right)
7984 elt = elt->right;
7985 else
7987 /* We have reached the biggest node in a subtree. Find
7988 the parent of it, which is the next bigger node. */
7989 while (elt->parent && elt->parent->right == elt)
7990 elt = elt->parent;
7991 elt = elt->parent;
7992 if (elt
7993 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7994 bit_position (elt->purpose))))
7996 next = elt->purpose;
7997 break;
8004 /* Ordinarily return, but not if we want to output all
8005 and there are elements left. */
8006 if (!(all && next != 0))
8007 return;
8009 /* If it's not incremental, just skip over the gap, so that after
8010 jumping to retry we will output the next successive element. */
8011 if (TREE_CODE (constructor_type) == RECORD_TYPE
8012 || TREE_CODE (constructor_type) == UNION_TYPE)
8013 constructor_unfilled_fields = next;
8014 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8015 constructor_unfilled_index = next;
8017 /* ELT now points to the node in the pending tree with the next
8018 initializer to output. */
8019 goto retry;
8022 /* Add one non-braced element to the current constructor level.
8023 This adjusts the current position within the constructor's type.
8024 This may also start or terminate implicit levels
8025 to handle a partly-braced initializer.
8027 Once this has found the correct level for the new element,
8028 it calls output_init_element.
8030 IMPLICIT is true if value comes from pop_init_level (1),
8031 the new initializer has been merged with the existing one
8032 and thus no warnings should be emitted about overriding an
8033 existing initializer. */
8035 void
8036 process_init_element (struct c_expr value, bool implicit,
8037 struct obstack * braced_init_obstack)
8039 tree orig_value = value.value;
8040 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8041 bool strict_string = value.original_code == STRING_CST;
8043 designator_depth = 0;
8044 designator_erroneous = 0;
8046 /* Handle superfluous braces around string cst as in
8047 char x[] = {"foo"}; */
8048 if (string_flag
8049 && constructor_type
8050 && TREE_CODE (constructor_type) == ARRAY_TYPE
8051 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8052 && integer_zerop (constructor_unfilled_index))
8054 if (constructor_stack->replacement_value.value)
8055 error_init ("excess elements in char array initializer");
8056 constructor_stack->replacement_value = value;
8057 return;
8060 if (constructor_stack->replacement_value.value != 0)
8062 error_init ("excess elements in struct initializer");
8063 return;
8066 /* Ignore elements of a brace group if it is entirely superfluous
8067 and has already been diagnosed. */
8068 if (constructor_type == 0)
8069 return;
8071 /* If we've exhausted any levels that didn't have braces,
8072 pop them now. */
8073 while (constructor_stack->implicit)
8075 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8076 || TREE_CODE (constructor_type) == UNION_TYPE)
8077 && constructor_fields == 0)
8078 process_init_element (pop_init_level (1, braced_init_obstack),
8079 true, braced_init_obstack);
8080 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8081 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8082 && (constructor_max_index == 0
8083 || tree_int_cst_lt (constructor_max_index,
8084 constructor_index)))
8085 process_init_element (pop_init_level (1, braced_init_obstack),
8086 true, braced_init_obstack);
8087 else
8088 break;
8091 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8092 if (constructor_range_stack)
8094 /* If value is a compound literal and we'll be just using its
8095 content, don't put it into a SAVE_EXPR. */
8096 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8097 || !require_constant_value
8098 || flag_isoc99)
8100 tree semantic_type = NULL_TREE;
8101 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8103 semantic_type = TREE_TYPE (value.value);
8104 value.value = TREE_OPERAND (value.value, 0);
8106 value.value = c_save_expr (value.value);
8107 if (semantic_type)
8108 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8109 value.value);
8113 while (1)
8115 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8117 tree fieldtype;
8118 enum tree_code fieldcode;
8120 if (constructor_fields == 0)
8122 pedwarn_init (input_location, 0,
8123 "excess elements in struct initializer");
8124 break;
8127 fieldtype = TREE_TYPE (constructor_fields);
8128 if (fieldtype != error_mark_node)
8129 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8130 fieldcode = TREE_CODE (fieldtype);
8132 /* Error for non-static initialization of a flexible array member. */
8133 if (fieldcode == ARRAY_TYPE
8134 && !require_constant_value
8135 && TYPE_SIZE (fieldtype) == NULL_TREE
8136 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8138 error_init ("non-static initialization of a flexible array member");
8139 break;
8142 /* Accept a string constant to initialize a subarray. */
8143 if (value.value != 0
8144 && fieldcode == ARRAY_TYPE
8145 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8146 && string_flag)
8147 value.value = orig_value;
8148 /* Otherwise, if we have come to a subaggregate,
8149 and we don't have an element of its type, push into it. */
8150 else if (value.value != 0
8151 && value.value != error_mark_node
8152 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8153 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8154 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8156 push_init_level (1, braced_init_obstack);
8157 continue;
8160 if (value.value)
8162 push_member_name (constructor_fields);
8163 output_init_element (value.value, value.original_type,
8164 strict_string, fieldtype,
8165 constructor_fields, 1, implicit,
8166 braced_init_obstack);
8167 RESTORE_SPELLING_DEPTH (constructor_depth);
8169 else
8170 /* Do the bookkeeping for an element that was
8171 directly output as a constructor. */
8173 /* For a record, keep track of end position of last field. */
8174 if (DECL_SIZE (constructor_fields))
8175 constructor_bit_index
8176 = size_binop_loc (input_location, PLUS_EXPR,
8177 bit_position (constructor_fields),
8178 DECL_SIZE (constructor_fields));
8180 /* If the current field was the first one not yet written out,
8181 it isn't now, so update. */
8182 if (constructor_unfilled_fields == constructor_fields)
8184 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8185 /* Skip any nameless bit fields. */
8186 while (constructor_unfilled_fields != 0
8187 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8188 && DECL_NAME (constructor_unfilled_fields) == 0)
8189 constructor_unfilled_fields =
8190 DECL_CHAIN (constructor_unfilled_fields);
8194 constructor_fields = DECL_CHAIN (constructor_fields);
8195 /* Skip any nameless bit fields at the beginning. */
8196 while (constructor_fields != 0
8197 && DECL_C_BIT_FIELD (constructor_fields)
8198 && DECL_NAME (constructor_fields) == 0)
8199 constructor_fields = DECL_CHAIN (constructor_fields);
8201 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8203 tree fieldtype;
8204 enum tree_code fieldcode;
8206 if (constructor_fields == 0)
8208 pedwarn_init (input_location, 0,
8209 "excess elements in union initializer");
8210 break;
8213 fieldtype = TREE_TYPE (constructor_fields);
8214 if (fieldtype != error_mark_node)
8215 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8216 fieldcode = TREE_CODE (fieldtype);
8218 /* Warn that traditional C rejects initialization of unions.
8219 We skip the warning if the value is zero. This is done
8220 under the assumption that the zero initializer in user
8221 code appears conditioned on e.g. __STDC__ to avoid
8222 "missing initializer" warnings and relies on default
8223 initialization to zero in the traditional C case.
8224 We also skip the warning if the initializer is designated,
8225 again on the assumption that this must be conditional on
8226 __STDC__ anyway (and we've already complained about the
8227 member-designator already). */
8228 if (!in_system_header && !constructor_designated
8229 && !(value.value && (integer_zerop (value.value)
8230 || real_zerop (value.value))))
8231 warning (OPT_Wtraditional, "traditional C rejects initialization "
8232 "of unions");
8234 /* Accept a string constant to initialize a subarray. */
8235 if (value.value != 0
8236 && fieldcode == ARRAY_TYPE
8237 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8238 && string_flag)
8239 value.value = orig_value;
8240 /* Otherwise, if we have come to a subaggregate,
8241 and we don't have an element of its type, push into it. */
8242 else if (value.value != 0
8243 && value.value != error_mark_node
8244 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8245 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8246 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8248 push_init_level (1, braced_init_obstack);
8249 continue;
8252 if (value.value)
8254 push_member_name (constructor_fields);
8255 output_init_element (value.value, value.original_type,
8256 strict_string, fieldtype,
8257 constructor_fields, 1, implicit,
8258 braced_init_obstack);
8259 RESTORE_SPELLING_DEPTH (constructor_depth);
8261 else
8262 /* Do the bookkeeping for an element that was
8263 directly output as a constructor. */
8265 constructor_bit_index = DECL_SIZE (constructor_fields);
8266 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8269 constructor_fields = 0;
8271 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8273 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8274 enum tree_code eltcode = TREE_CODE (elttype);
8276 /* Accept a string constant to initialize a subarray. */
8277 if (value.value != 0
8278 && eltcode == ARRAY_TYPE
8279 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8280 && string_flag)
8281 value.value = orig_value;
8282 /* Otherwise, if we have come to a subaggregate,
8283 and we don't have an element of its type, push into it. */
8284 else if (value.value != 0
8285 && value.value != error_mark_node
8286 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8287 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8288 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8290 push_init_level (1, braced_init_obstack);
8291 continue;
8294 if (constructor_max_index != 0
8295 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8296 || integer_all_onesp (constructor_max_index)))
8298 pedwarn_init (input_location, 0,
8299 "excess elements in array initializer");
8300 break;
8303 /* Now output the actual element. */
8304 if (value.value)
8306 push_array_bounds (tree_low_cst (constructor_index, 1));
8307 output_init_element (value.value, value.original_type,
8308 strict_string, elttype,
8309 constructor_index, 1, implicit,
8310 braced_init_obstack);
8311 RESTORE_SPELLING_DEPTH (constructor_depth);
8314 constructor_index
8315 = size_binop_loc (input_location, PLUS_EXPR,
8316 constructor_index, bitsize_one_node);
8318 if (!value.value)
8319 /* If we are doing the bookkeeping for an element that was
8320 directly output as a constructor, we must update
8321 constructor_unfilled_index. */
8322 constructor_unfilled_index = constructor_index;
8324 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8326 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8328 /* Do a basic check of initializer size. Note that vectors
8329 always have a fixed size derived from their type. */
8330 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8332 pedwarn_init (input_location, 0,
8333 "excess elements in vector initializer");
8334 break;
8337 /* Now output the actual element. */
8338 if (value.value)
8340 if (TREE_CODE (value.value) == VECTOR_CST)
8341 elttype = TYPE_MAIN_VARIANT (constructor_type);
8342 output_init_element (value.value, value.original_type,
8343 strict_string, elttype,
8344 constructor_index, 1, implicit,
8345 braced_init_obstack);
8348 constructor_index
8349 = size_binop_loc (input_location,
8350 PLUS_EXPR, constructor_index, bitsize_one_node);
8352 if (!value.value)
8353 /* If we are doing the bookkeeping for an element that was
8354 directly output as a constructor, we must update
8355 constructor_unfilled_index. */
8356 constructor_unfilled_index = constructor_index;
8359 /* Handle the sole element allowed in a braced initializer
8360 for a scalar variable. */
8361 else if (constructor_type != error_mark_node
8362 && constructor_fields == 0)
8364 pedwarn_init (input_location, 0,
8365 "excess elements in scalar initializer");
8366 break;
8368 else
8370 if (value.value)
8371 output_init_element (value.value, value.original_type,
8372 strict_string, constructor_type,
8373 NULL_TREE, 1, implicit,
8374 braced_init_obstack);
8375 constructor_fields = 0;
8378 /* Handle range initializers either at this level or anywhere higher
8379 in the designator stack. */
8380 if (constructor_range_stack)
8382 struct constructor_range_stack *p, *range_stack;
8383 int finish = 0;
8385 range_stack = constructor_range_stack;
8386 constructor_range_stack = 0;
8387 while (constructor_stack != range_stack->stack)
8389 gcc_assert (constructor_stack->implicit);
8390 process_init_element (pop_init_level (1,
8391 braced_init_obstack),
8392 true, braced_init_obstack);
8394 for (p = range_stack;
8395 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8396 p = p->prev)
8398 gcc_assert (constructor_stack->implicit);
8399 process_init_element (pop_init_level (1, braced_init_obstack),
8400 true, braced_init_obstack);
8403 p->index = size_binop_loc (input_location,
8404 PLUS_EXPR, p->index, bitsize_one_node);
8405 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8406 finish = 1;
8408 while (1)
8410 constructor_index = p->index;
8411 constructor_fields = p->fields;
8412 if (finish && p->range_end && p->index == p->range_start)
8414 finish = 0;
8415 p->prev = 0;
8417 p = p->next;
8418 if (!p)
8419 break;
8420 push_init_level (2, braced_init_obstack);
8421 p->stack = constructor_stack;
8422 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8423 p->index = p->range_start;
8426 if (!finish)
8427 constructor_range_stack = range_stack;
8428 continue;
8431 break;
8434 constructor_range_stack = 0;
8437 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8438 (guaranteed to be 'volatile' or null) and ARGS (represented using
8439 an ASM_EXPR node). */
8440 tree
8441 build_asm_stmt (tree cv_qualifier, tree args)
8443 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8444 ASM_VOLATILE_P (args) = 1;
8445 return add_stmt (args);
8448 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8449 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8450 SIMPLE indicates whether there was anything at all after the
8451 string in the asm expression -- asm("blah") and asm("blah" : )
8452 are subtly different. We use a ASM_EXPR node to represent this. */
8453 tree
8454 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8455 tree clobbers, tree labels, bool simple)
8457 tree tail;
8458 tree args;
8459 int i;
8460 const char *constraint;
8461 const char **oconstraints;
8462 bool allows_mem, allows_reg, is_inout;
8463 int ninputs, noutputs;
8465 ninputs = list_length (inputs);
8466 noutputs = list_length (outputs);
8467 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8469 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8471 /* Remove output conversions that change the type but not the mode. */
8472 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8474 tree output = TREE_VALUE (tail);
8476 /* ??? Really, this should not be here. Users should be using a
8477 proper lvalue, dammit. But there's a long history of using casts
8478 in the output operands. In cases like longlong.h, this becomes a
8479 primitive form of typechecking -- if the cast can be removed, then
8480 the output operand had a type of the proper width; otherwise we'll
8481 get an error. Gross, but ... */
8482 STRIP_NOPS (output);
8484 if (!lvalue_or_else (loc, output, lv_asm))
8485 output = error_mark_node;
8487 if (output != error_mark_node
8488 && (TREE_READONLY (output)
8489 || TYPE_READONLY (TREE_TYPE (output))
8490 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8491 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8492 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8493 readonly_error (output, lv_asm);
8495 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8496 oconstraints[i] = constraint;
8498 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8499 &allows_mem, &allows_reg, &is_inout))
8501 /* If the operand is going to end up in memory,
8502 mark it addressable. */
8503 if (!allows_reg && !c_mark_addressable (output))
8504 output = error_mark_node;
8506 else
8507 output = error_mark_node;
8509 TREE_VALUE (tail) = output;
8512 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8514 tree input;
8516 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8517 input = TREE_VALUE (tail);
8519 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8520 oconstraints, &allows_mem, &allows_reg))
8522 /* If the operand is going to end up in memory,
8523 mark it addressable. */
8524 if (!allows_reg && allows_mem)
8526 /* Strip the nops as we allow this case. FIXME, this really
8527 should be rejected or made deprecated. */
8528 STRIP_NOPS (input);
8529 if (!c_mark_addressable (input))
8530 input = error_mark_node;
8533 else
8534 input = error_mark_node;
8536 TREE_VALUE (tail) = input;
8539 /* ASMs with labels cannot have outputs. This should have been
8540 enforced by the parser. */
8541 gcc_assert (outputs == NULL || labels == NULL);
8543 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8545 /* asm statements without outputs, including simple ones, are treated
8546 as volatile. */
8547 ASM_INPUT_P (args) = simple;
8548 ASM_VOLATILE_P (args) = (noutputs == 0);
8550 return args;
8553 /* Generate a goto statement to LABEL. LOC is the location of the
8554 GOTO. */
8556 tree
8557 c_finish_goto_label (location_t loc, tree label)
8559 tree decl = lookup_label_for_goto (loc, label);
8560 if (!decl)
8561 return NULL_TREE;
8562 TREE_USED (decl) = 1;
8564 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8565 SET_EXPR_LOCATION (t, loc);
8566 return add_stmt (t);
8570 /* Generate a computed goto statement to EXPR. LOC is the location of
8571 the GOTO. */
8573 tree
8574 c_finish_goto_ptr (location_t loc, tree expr)
8576 tree t;
8577 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8578 expr = c_fully_fold (expr, false, NULL);
8579 expr = convert (ptr_type_node, expr);
8580 t = build1 (GOTO_EXPR, void_type_node, expr);
8581 SET_EXPR_LOCATION (t, loc);
8582 return add_stmt (t);
8585 /* Generate a C `return' statement. RETVAL is the expression for what
8586 to return, or a null pointer for `return;' with no value. LOC is
8587 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8588 is the original type of RETVAL. */
8590 tree
8591 c_finish_return (location_t loc, tree retval, tree origtype)
8593 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8594 bool no_warning = false;
8595 bool npc = false;
8597 if (TREE_THIS_VOLATILE (current_function_decl))
8598 warning_at (loc, 0,
8599 "function declared %<noreturn%> has a %<return%> statement");
8601 if (retval)
8603 tree semantic_type = NULL_TREE;
8604 npc = null_pointer_constant_p (retval);
8605 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8607 semantic_type = TREE_TYPE (retval);
8608 retval = TREE_OPERAND (retval, 0);
8610 retval = c_fully_fold (retval, false, NULL);
8611 if (semantic_type)
8612 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8615 if (!retval)
8617 current_function_returns_null = 1;
8618 if ((warn_return_type || flag_isoc99)
8619 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8621 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8622 "%<return%> with no value, in "
8623 "function returning non-void");
8624 no_warning = true;
8627 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8629 current_function_returns_null = 1;
8630 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8631 pedwarn (loc, 0,
8632 "%<return%> with a value, in function returning void");
8633 else
8634 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8635 "%<return%> with expression, in function returning void");
8637 else
8639 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8640 ic_return,
8641 npc, NULL_TREE, NULL_TREE, 0);
8642 tree res = DECL_RESULT (current_function_decl);
8643 tree inner;
8645 current_function_returns_value = 1;
8646 if (t == error_mark_node)
8647 return NULL_TREE;
8649 inner = t = convert (TREE_TYPE (res), t);
8651 /* Strip any conversions, additions, and subtractions, and see if
8652 we are returning the address of a local variable. Warn if so. */
8653 while (1)
8655 switch (TREE_CODE (inner))
8657 CASE_CONVERT:
8658 case NON_LVALUE_EXPR:
8659 case PLUS_EXPR:
8660 case POINTER_PLUS_EXPR:
8661 inner = TREE_OPERAND (inner, 0);
8662 continue;
8664 case MINUS_EXPR:
8665 /* If the second operand of the MINUS_EXPR has a pointer
8666 type (or is converted from it), this may be valid, so
8667 don't give a warning. */
8669 tree op1 = TREE_OPERAND (inner, 1);
8671 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8672 && (CONVERT_EXPR_P (op1)
8673 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8674 op1 = TREE_OPERAND (op1, 0);
8676 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8677 break;
8679 inner = TREE_OPERAND (inner, 0);
8680 continue;
8683 case ADDR_EXPR:
8684 inner = TREE_OPERAND (inner, 0);
8686 while (REFERENCE_CLASS_P (inner)
8687 && TREE_CODE (inner) != INDIRECT_REF)
8688 inner = TREE_OPERAND (inner, 0);
8690 if (DECL_P (inner)
8691 && !DECL_EXTERNAL (inner)
8692 && !TREE_STATIC (inner)
8693 && DECL_CONTEXT (inner) == current_function_decl)
8694 warning_at (loc,
8695 0, "function returns address of local variable");
8696 break;
8698 default:
8699 break;
8702 break;
8705 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8706 SET_EXPR_LOCATION (retval, loc);
8708 if (warn_sequence_point)
8709 verify_sequence_points (retval);
8712 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8713 TREE_NO_WARNING (ret_stmt) |= no_warning;
8714 return add_stmt (ret_stmt);
8717 struct c_switch {
8718 /* The SWITCH_EXPR being built. */
8719 tree switch_expr;
8721 /* The original type of the testing expression, i.e. before the
8722 default conversion is applied. */
8723 tree orig_type;
8725 /* A splay-tree mapping the low element of a case range to the high
8726 element, or NULL_TREE if there is no high element. Used to
8727 determine whether or not a new case label duplicates an old case
8728 label. We need a tree, rather than simply a hash table, because
8729 of the GNU case range extension. */
8730 splay_tree cases;
8732 /* The bindings at the point of the switch. This is used for
8733 warnings crossing decls when branching to a case label. */
8734 struct c_spot_bindings *bindings;
8736 /* The next node on the stack. */
8737 struct c_switch *next;
8740 /* A stack of the currently active switch statements. The innermost
8741 switch statement is on the top of the stack. There is no need to
8742 mark the stack for garbage collection because it is only active
8743 during the processing of the body of a function, and we never
8744 collect at that point. */
8746 struct c_switch *c_switch_stack;
8748 /* Start a C switch statement, testing expression EXP. Return the new
8749 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8750 SWITCH_COND_LOC is the location of the switch's condition. */
8752 tree
8753 c_start_case (location_t switch_loc,
8754 location_t switch_cond_loc,
8755 tree exp)
8757 tree orig_type = error_mark_node;
8758 struct c_switch *cs;
8760 if (exp != error_mark_node)
8762 orig_type = TREE_TYPE (exp);
8764 if (!INTEGRAL_TYPE_P (orig_type))
8766 if (orig_type != error_mark_node)
8768 error_at (switch_cond_loc, "switch quantity not an integer");
8769 orig_type = error_mark_node;
8771 exp = integer_zero_node;
8773 else
8775 tree type = TYPE_MAIN_VARIANT (orig_type);
8777 if (!in_system_header
8778 && (type == long_integer_type_node
8779 || type == long_unsigned_type_node))
8780 warning_at (switch_cond_loc,
8781 OPT_Wtraditional, "%<long%> switch expression not "
8782 "converted to %<int%> in ISO C");
8784 exp = c_fully_fold (exp, false, NULL);
8785 exp = default_conversion (exp);
8787 if (warn_sequence_point)
8788 verify_sequence_points (exp);
8792 /* Add this new SWITCH_EXPR to the stack. */
8793 cs = XNEW (struct c_switch);
8794 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8795 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8796 cs->orig_type = orig_type;
8797 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8798 cs->bindings = c_get_switch_bindings ();
8799 cs->next = c_switch_stack;
8800 c_switch_stack = cs;
8802 return add_stmt (cs->switch_expr);
8805 /* Process a case label at location LOC. */
8807 tree
8808 do_case (location_t loc, tree low_value, tree high_value)
8810 tree label = NULL_TREE;
8812 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8814 low_value = c_fully_fold (low_value, false, NULL);
8815 if (TREE_CODE (low_value) == INTEGER_CST)
8816 pedwarn (input_location, OPT_pedantic,
8817 "case label is not an integer constant expression");
8820 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8822 high_value = c_fully_fold (high_value, false, NULL);
8823 if (TREE_CODE (high_value) == INTEGER_CST)
8824 pedwarn (input_location, OPT_pedantic,
8825 "case label is not an integer constant expression");
8828 if (c_switch_stack == NULL)
8830 if (low_value)
8831 error_at (loc, "case label not within a switch statement");
8832 else
8833 error_at (loc, "%<default%> label not within a switch statement");
8834 return NULL_TREE;
8837 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8838 EXPR_LOCATION (c_switch_stack->switch_expr),
8839 loc))
8840 return NULL_TREE;
8842 label = c_add_case_label (loc, c_switch_stack->cases,
8843 SWITCH_COND (c_switch_stack->switch_expr),
8844 c_switch_stack->orig_type,
8845 low_value, high_value);
8846 if (label == error_mark_node)
8847 label = NULL_TREE;
8848 return label;
8851 /* Finish the switch statement. */
8853 void
8854 c_finish_case (tree body)
8856 struct c_switch *cs = c_switch_stack;
8857 location_t switch_location;
8859 SWITCH_BODY (cs->switch_expr) = body;
8861 /* Emit warnings as needed. */
8862 switch_location = EXPR_LOCATION (cs->switch_expr);
8863 c_do_switch_warnings (cs->cases, switch_location,
8864 TREE_TYPE (cs->switch_expr),
8865 SWITCH_COND (cs->switch_expr));
8867 /* Pop the stack. */
8868 c_switch_stack = cs->next;
8869 splay_tree_delete (cs->cases);
8870 c_release_switch_bindings (cs->bindings);
8871 XDELETE (cs);
8874 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8875 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8876 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8877 statement, and was not surrounded with parenthesis. */
8879 void
8880 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8881 tree else_block, bool nested_if)
8883 tree stmt;
8885 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8886 if (warn_parentheses && nested_if && else_block == NULL)
8888 tree inner_if = then_block;
8890 /* We know from the grammar productions that there is an IF nested
8891 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8892 it might not be exactly THEN_BLOCK, but should be the last
8893 non-container statement within. */
8894 while (1)
8895 switch (TREE_CODE (inner_if))
8897 case COND_EXPR:
8898 goto found;
8899 case BIND_EXPR:
8900 inner_if = BIND_EXPR_BODY (inner_if);
8901 break;
8902 case STATEMENT_LIST:
8903 inner_if = expr_last (then_block);
8904 break;
8905 case TRY_FINALLY_EXPR:
8906 case TRY_CATCH_EXPR:
8907 inner_if = TREE_OPERAND (inner_if, 0);
8908 break;
8909 default:
8910 gcc_unreachable ();
8912 found:
8914 if (COND_EXPR_ELSE (inner_if))
8915 warning_at (if_locus, OPT_Wparentheses,
8916 "suggest explicit braces to avoid ambiguous %<else%>");
8919 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8920 SET_EXPR_LOCATION (stmt, if_locus);
8921 add_stmt (stmt);
8924 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8925 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8926 is false for DO loops. INCR is the FOR increment expression. BODY is
8927 the statement controlled by the loop. BLAB is the break label. CLAB is
8928 the continue label. Everything is allowed to be NULL. */
8930 void
8931 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8932 tree blab, tree clab, bool cond_is_first)
8934 tree entry = NULL, exit = NULL, t;
8936 /* If the condition is zero don't generate a loop construct. */
8937 if (cond && integer_zerop (cond))
8939 if (cond_is_first)
8941 t = build_and_jump (&blab);
8942 SET_EXPR_LOCATION (t, start_locus);
8943 add_stmt (t);
8946 else
8948 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8950 /* If we have an exit condition, then we build an IF with gotos either
8951 out of the loop, or to the top of it. If there's no exit condition,
8952 then we just build a jump back to the top. */
8953 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8955 if (cond && !integer_nonzerop (cond))
8957 /* Canonicalize the loop condition to the end. This means
8958 generating a branch to the loop condition. Reuse the
8959 continue label, if possible. */
8960 if (cond_is_first)
8962 if (incr || !clab)
8964 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8965 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8967 else
8968 t = build1 (GOTO_EXPR, void_type_node, clab);
8969 SET_EXPR_LOCATION (t, start_locus);
8970 add_stmt (t);
8973 t = build_and_jump (&blab);
8974 if (cond_is_first)
8975 exit = fold_build3_loc (start_locus,
8976 COND_EXPR, void_type_node, cond, exit, t);
8977 else
8978 exit = fold_build3_loc (input_location,
8979 COND_EXPR, void_type_node, cond, exit, t);
8982 add_stmt (top);
8985 if (body)
8986 add_stmt (body);
8987 if (clab)
8988 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8989 if (incr)
8990 add_stmt (incr);
8991 if (entry)
8992 add_stmt (entry);
8993 if (exit)
8994 add_stmt (exit);
8995 if (blab)
8996 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
8999 tree
9000 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9002 bool skip;
9003 tree label = *label_p;
9005 /* In switch statements break is sometimes stylistically used after
9006 a return statement. This can lead to spurious warnings about
9007 control reaching the end of a non-void function when it is
9008 inlined. Note that we are calling block_may_fallthru with
9009 language specific tree nodes; this works because
9010 block_may_fallthru returns true when given something it does not
9011 understand. */
9012 skip = !block_may_fallthru (cur_stmt_list);
9014 if (!label)
9016 if (!skip)
9017 *label_p = label = create_artificial_label (loc);
9019 else if (TREE_CODE (label) == LABEL_DECL)
9021 else switch (TREE_INT_CST_LOW (label))
9023 case 0:
9024 if (is_break)
9025 error_at (loc, "break statement not within loop or switch");
9026 else
9027 error_at (loc, "continue statement not within a loop");
9028 return NULL_TREE;
9030 case 1:
9031 gcc_assert (is_break);
9032 error_at (loc, "break statement used with OpenMP for loop");
9033 return NULL_TREE;
9035 default:
9036 gcc_unreachable ();
9039 if (skip)
9040 return NULL_TREE;
9042 if (!is_break)
9043 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9045 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9048 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9050 static void
9051 emit_side_effect_warnings (location_t loc, tree expr)
9053 if (expr == error_mark_node)
9055 else if (!TREE_SIDE_EFFECTS (expr))
9057 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9058 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9060 else
9061 warn_if_unused_value (expr, loc);
9064 /* Process an expression as if it were a complete statement. Emit
9065 diagnostics, but do not call ADD_STMT. LOC is the location of the
9066 statement. */
9068 tree
9069 c_process_expr_stmt (location_t loc, tree expr)
9071 tree exprv;
9073 if (!expr)
9074 return NULL_TREE;
9076 expr = c_fully_fold (expr, false, NULL);
9078 if (warn_sequence_point)
9079 verify_sequence_points (expr);
9081 if (TREE_TYPE (expr) != error_mark_node
9082 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9083 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9084 error_at (loc, "expression statement has incomplete type");
9086 /* If we're not processing a statement expression, warn about unused values.
9087 Warnings for statement expressions will be emitted later, once we figure
9088 out which is the result. */
9089 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9090 && warn_unused_value)
9091 emit_side_effect_warnings (loc, expr);
9093 exprv = expr;
9094 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9095 exprv = TREE_OPERAND (exprv, 1);
9096 if (DECL_P (exprv) || handled_component_p (exprv))
9097 mark_exp_read (exprv);
9099 /* If the expression is not of a type to which we cannot assign a line
9100 number, wrap the thing in a no-op NOP_EXPR. */
9101 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9103 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9104 SET_EXPR_LOCATION (expr, loc);
9107 return expr;
9110 /* Emit an expression as a statement. LOC is the location of the
9111 expression. */
9113 tree
9114 c_finish_expr_stmt (location_t loc, tree expr)
9116 if (expr)
9117 return add_stmt (c_process_expr_stmt (loc, expr));
9118 else
9119 return NULL;
9122 /* Do the opposite and emit a statement as an expression. To begin,
9123 create a new binding level and return it. */
9125 tree
9126 c_begin_stmt_expr (void)
9128 tree ret;
9130 /* We must force a BLOCK for this level so that, if it is not expanded
9131 later, there is a way to turn off the entire subtree of blocks that
9132 are contained in it. */
9133 keep_next_level ();
9134 ret = c_begin_compound_stmt (true);
9136 c_bindings_start_stmt_expr (c_switch_stack == NULL
9137 ? NULL
9138 : c_switch_stack->bindings);
9140 /* Mark the current statement list as belonging to a statement list. */
9141 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9143 return ret;
9146 /* LOC is the location of the compound statement to which this body
9147 belongs. */
9149 tree
9150 c_finish_stmt_expr (location_t loc, tree body)
9152 tree last, type, tmp, val;
9153 tree *last_p;
9155 body = c_end_compound_stmt (loc, body, true);
9157 c_bindings_end_stmt_expr (c_switch_stack == NULL
9158 ? NULL
9159 : c_switch_stack->bindings);
9161 /* Locate the last statement in BODY. See c_end_compound_stmt
9162 about always returning a BIND_EXPR. */
9163 last_p = &BIND_EXPR_BODY (body);
9164 last = BIND_EXPR_BODY (body);
9166 continue_searching:
9167 if (TREE_CODE (last) == STATEMENT_LIST)
9169 tree_stmt_iterator i;
9171 /* This can happen with degenerate cases like ({ }). No value. */
9172 if (!TREE_SIDE_EFFECTS (last))
9173 return body;
9175 /* If we're supposed to generate side effects warnings, process
9176 all of the statements except the last. */
9177 if (warn_unused_value)
9179 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9181 location_t tloc;
9182 tree t = tsi_stmt (i);
9184 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9185 emit_side_effect_warnings (tloc, t);
9188 else
9189 i = tsi_last (last);
9190 last_p = tsi_stmt_ptr (i);
9191 last = *last_p;
9194 /* If the end of the list is exception related, then the list was split
9195 by a call to push_cleanup. Continue searching. */
9196 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9197 || TREE_CODE (last) == TRY_CATCH_EXPR)
9199 last_p = &TREE_OPERAND (last, 0);
9200 last = *last_p;
9201 goto continue_searching;
9204 if (last == error_mark_node)
9205 return last;
9207 /* In the case that the BIND_EXPR is not necessary, return the
9208 expression out from inside it. */
9209 if (last == BIND_EXPR_BODY (body)
9210 && BIND_EXPR_VARS (body) == NULL)
9212 /* Even if this looks constant, do not allow it in a constant
9213 expression. */
9214 last = c_wrap_maybe_const (last, true);
9215 /* Do not warn if the return value of a statement expression is
9216 unused. */
9217 TREE_NO_WARNING (last) = 1;
9218 return last;
9221 /* Extract the type of said expression. */
9222 type = TREE_TYPE (last);
9224 /* If we're not returning a value at all, then the BIND_EXPR that
9225 we already have is a fine expression to return. */
9226 if (!type || VOID_TYPE_P (type))
9227 return body;
9229 /* Now that we've located the expression containing the value, it seems
9230 silly to make voidify_wrapper_expr repeat the process. Create a
9231 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9232 tmp = create_tmp_var_raw (type, NULL);
9234 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9235 tree_expr_nonnegative_p giving up immediately. */
9236 val = last;
9237 if (TREE_CODE (val) == NOP_EXPR
9238 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9239 val = TREE_OPERAND (val, 0);
9241 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9242 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9245 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9246 SET_EXPR_LOCATION (t, loc);
9247 return t;
9251 /* Begin and end compound statements. This is as simple as pushing
9252 and popping new statement lists from the tree. */
9254 tree
9255 c_begin_compound_stmt (bool do_scope)
9257 tree stmt = push_stmt_list ();
9258 if (do_scope)
9259 push_scope ();
9260 return stmt;
9263 /* End a compound statement. STMT is the statement. LOC is the
9264 location of the compound statement-- this is usually the location
9265 of the opening brace. */
9267 tree
9268 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9270 tree block = NULL;
9272 if (do_scope)
9274 if (c_dialect_objc ())
9275 objc_clear_super_receiver ();
9276 block = pop_scope ();
9279 stmt = pop_stmt_list (stmt);
9280 stmt = c_build_bind_expr (loc, block, stmt);
9282 /* If this compound statement is nested immediately inside a statement
9283 expression, then force a BIND_EXPR to be created. Otherwise we'll
9284 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9285 STATEMENT_LISTs merge, and thus we can lose track of what statement
9286 was really last. */
9287 if (cur_stmt_list
9288 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9289 && TREE_CODE (stmt) != BIND_EXPR)
9291 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9292 TREE_SIDE_EFFECTS (stmt) = 1;
9293 SET_EXPR_LOCATION (stmt, loc);
9296 return stmt;
9299 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9300 when the current scope is exited. EH_ONLY is true when this is not
9301 meant to apply to normal control flow transfer. */
9303 void
9304 push_cleanup (tree decl, tree cleanup, bool eh_only)
9306 enum tree_code code;
9307 tree stmt, list;
9308 bool stmt_expr;
9310 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9311 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9312 add_stmt (stmt);
9313 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9314 list = push_stmt_list ();
9315 TREE_OPERAND (stmt, 0) = list;
9316 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9319 /* Build a binary-operation expression without default conversions.
9320 CODE is the kind of expression to build.
9321 LOCATION is the operator's location.
9322 This function differs from `build' in several ways:
9323 the data type of the result is computed and recorded in it,
9324 warnings are generated if arg data types are invalid,
9325 special handling for addition and subtraction of pointers is known,
9326 and some optimization is done (operations on narrow ints
9327 are done in the narrower type when that gives the same result).
9328 Constant folding is also done before the result is returned.
9330 Note that the operands will never have enumeral types, or function
9331 or array types, because either they will have the default conversions
9332 performed or they have both just been converted to some other type in which
9333 the arithmetic is to be done. */
9335 tree
9336 build_binary_op (location_t location, enum tree_code code,
9337 tree orig_op0, tree orig_op1, int convert_p)
9339 tree type0, type1, orig_type0, orig_type1;
9340 tree eptype;
9341 enum tree_code code0, code1;
9342 tree op0, op1;
9343 tree ret = error_mark_node;
9344 const char *invalid_op_diag;
9345 bool op0_int_operands, op1_int_operands;
9346 bool int_const, int_const_or_overflow, int_operands;
9348 /* Expression code to give to the expression when it is built.
9349 Normally this is CODE, which is what the caller asked for,
9350 but in some special cases we change it. */
9351 enum tree_code resultcode = code;
9353 /* Data type in which the computation is to be performed.
9354 In the simplest cases this is the common type of the arguments. */
9355 tree result_type = NULL;
9357 /* When the computation is in excess precision, the type of the
9358 final EXCESS_PRECISION_EXPR. */
9359 tree semantic_result_type = NULL;
9361 /* Nonzero means operands have already been type-converted
9362 in whatever way is necessary.
9363 Zero means they need to be converted to RESULT_TYPE. */
9364 int converted = 0;
9366 /* Nonzero means create the expression with this type, rather than
9367 RESULT_TYPE. */
9368 tree build_type = 0;
9370 /* Nonzero means after finally constructing the expression
9371 convert it to this type. */
9372 tree final_type = 0;
9374 /* Nonzero if this is an operation like MIN or MAX which can
9375 safely be computed in short if both args are promoted shorts.
9376 Also implies COMMON.
9377 -1 indicates a bitwise operation; this makes a difference
9378 in the exact conditions for when it is safe to do the operation
9379 in a narrower mode. */
9380 int shorten = 0;
9382 /* Nonzero if this is a comparison operation;
9383 if both args are promoted shorts, compare the original shorts.
9384 Also implies COMMON. */
9385 int short_compare = 0;
9387 /* Nonzero if this is a right-shift operation, which can be computed on the
9388 original short and then promoted if the operand is a promoted short. */
9389 int short_shift = 0;
9391 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9392 int common = 0;
9394 /* True means types are compatible as far as ObjC is concerned. */
9395 bool objc_ok;
9397 /* True means this is an arithmetic operation that may need excess
9398 precision. */
9399 bool may_need_excess_precision;
9401 /* True means this is a boolean operation that converts both its
9402 operands to truth-values. */
9403 bool boolean_op = false;
9405 if (location == UNKNOWN_LOCATION)
9406 location = input_location;
9408 op0 = orig_op0;
9409 op1 = orig_op1;
9411 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9412 if (op0_int_operands)
9413 op0 = remove_c_maybe_const_expr (op0);
9414 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9415 if (op1_int_operands)
9416 op1 = remove_c_maybe_const_expr (op1);
9417 int_operands = (op0_int_operands && op1_int_operands);
9418 if (int_operands)
9420 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9421 && TREE_CODE (orig_op1) == INTEGER_CST);
9422 int_const = (int_const_or_overflow
9423 && !TREE_OVERFLOW (orig_op0)
9424 && !TREE_OVERFLOW (orig_op1));
9426 else
9427 int_const = int_const_or_overflow = false;
9429 if (convert_p)
9431 op0 = default_conversion (op0);
9432 op1 = default_conversion (op1);
9435 orig_type0 = type0 = TREE_TYPE (op0);
9436 orig_type1 = type1 = TREE_TYPE (op1);
9438 /* The expression codes of the data types of the arguments tell us
9439 whether the arguments are integers, floating, pointers, etc. */
9440 code0 = TREE_CODE (type0);
9441 code1 = TREE_CODE (type1);
9443 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9444 STRIP_TYPE_NOPS (op0);
9445 STRIP_TYPE_NOPS (op1);
9447 /* If an error was already reported for one of the arguments,
9448 avoid reporting another error. */
9450 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9451 return error_mark_node;
9453 if ((invalid_op_diag
9454 = targetm.invalid_binary_op (code, type0, type1)))
9456 error_at (location, invalid_op_diag);
9457 return error_mark_node;
9460 switch (code)
9462 case PLUS_EXPR:
9463 case MINUS_EXPR:
9464 case MULT_EXPR:
9465 case TRUNC_DIV_EXPR:
9466 case CEIL_DIV_EXPR:
9467 case FLOOR_DIV_EXPR:
9468 case ROUND_DIV_EXPR:
9469 case EXACT_DIV_EXPR:
9470 may_need_excess_precision = true;
9471 break;
9472 default:
9473 may_need_excess_precision = false;
9474 break;
9476 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9478 op0 = TREE_OPERAND (op0, 0);
9479 type0 = TREE_TYPE (op0);
9481 else if (may_need_excess_precision
9482 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9484 type0 = eptype;
9485 op0 = convert (eptype, op0);
9487 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9489 op1 = TREE_OPERAND (op1, 0);
9490 type1 = TREE_TYPE (op1);
9492 else if (may_need_excess_precision
9493 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9495 type1 = eptype;
9496 op1 = convert (eptype, op1);
9499 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9501 switch (code)
9503 case PLUS_EXPR:
9504 /* Handle the pointer + int case. */
9505 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9507 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9508 goto return_build_binary_op;
9510 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9512 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9513 goto return_build_binary_op;
9515 else
9516 common = 1;
9517 break;
9519 case MINUS_EXPR:
9520 /* Subtraction of two similar pointers.
9521 We must subtract them as integers, then divide by object size. */
9522 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9523 && comp_target_types (location, type0, type1))
9525 ret = pointer_diff (location, op0, op1);
9526 goto return_build_binary_op;
9528 /* Handle pointer minus int. Just like pointer plus int. */
9529 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9531 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9532 goto return_build_binary_op;
9534 else
9535 common = 1;
9536 break;
9538 case MULT_EXPR:
9539 common = 1;
9540 break;
9542 case TRUNC_DIV_EXPR:
9543 case CEIL_DIV_EXPR:
9544 case FLOOR_DIV_EXPR:
9545 case ROUND_DIV_EXPR:
9546 case EXACT_DIV_EXPR:
9547 warn_for_div_by_zero (location, op1);
9549 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9550 || code0 == FIXED_POINT_TYPE
9551 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9552 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9553 || code1 == FIXED_POINT_TYPE
9554 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9556 enum tree_code tcode0 = code0, tcode1 = code1;
9558 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9559 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9560 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9561 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9563 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9564 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9565 resultcode = RDIV_EXPR;
9566 else
9567 /* Although it would be tempting to shorten always here, that
9568 loses on some targets, since the modulo instruction is
9569 undefined if the quotient can't be represented in the
9570 computation mode. We shorten only if unsigned or if
9571 dividing by something we know != -1. */
9572 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9573 || (TREE_CODE (op1) == INTEGER_CST
9574 && !integer_all_onesp (op1)));
9575 common = 1;
9577 break;
9579 case BIT_AND_EXPR:
9580 case BIT_IOR_EXPR:
9581 case BIT_XOR_EXPR:
9582 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9583 shorten = -1;
9584 /* Allow vector types which are not floating point types. */
9585 else if (code0 == VECTOR_TYPE
9586 && code1 == VECTOR_TYPE
9587 && !VECTOR_FLOAT_TYPE_P (type0)
9588 && !VECTOR_FLOAT_TYPE_P (type1))
9589 common = 1;
9590 break;
9592 case TRUNC_MOD_EXPR:
9593 case FLOOR_MOD_EXPR:
9594 warn_for_div_by_zero (location, op1);
9596 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9597 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9598 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9599 common = 1;
9600 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9602 /* Although it would be tempting to shorten always here, that loses
9603 on some targets, since the modulo instruction is undefined if the
9604 quotient can't be represented in the computation mode. We shorten
9605 only if unsigned or if dividing by something we know != -1. */
9606 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9607 || (TREE_CODE (op1) == INTEGER_CST
9608 && !integer_all_onesp (op1)));
9609 common = 1;
9611 break;
9613 case TRUTH_ANDIF_EXPR:
9614 case TRUTH_ORIF_EXPR:
9615 case TRUTH_AND_EXPR:
9616 case TRUTH_OR_EXPR:
9617 case TRUTH_XOR_EXPR:
9618 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9619 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9620 || code0 == FIXED_POINT_TYPE)
9621 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9622 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9623 || code1 == FIXED_POINT_TYPE))
9625 /* Result of these operations is always an int,
9626 but that does not mean the operands should be
9627 converted to ints! */
9628 result_type = integer_type_node;
9629 op0 = c_common_truthvalue_conversion (location, op0);
9630 op1 = c_common_truthvalue_conversion (location, op1);
9631 converted = 1;
9632 boolean_op = true;
9634 if (code == TRUTH_ANDIF_EXPR)
9636 int_const_or_overflow = (int_operands
9637 && TREE_CODE (orig_op0) == INTEGER_CST
9638 && (op0 == truthvalue_false_node
9639 || TREE_CODE (orig_op1) == INTEGER_CST));
9640 int_const = (int_const_or_overflow
9641 && !TREE_OVERFLOW (orig_op0)
9642 && (op0 == truthvalue_false_node
9643 || !TREE_OVERFLOW (orig_op1)));
9645 else if (code == TRUTH_ORIF_EXPR)
9647 int_const_or_overflow = (int_operands
9648 && TREE_CODE (orig_op0) == INTEGER_CST
9649 && (op0 == truthvalue_true_node
9650 || TREE_CODE (orig_op1) == INTEGER_CST));
9651 int_const = (int_const_or_overflow
9652 && !TREE_OVERFLOW (orig_op0)
9653 && (op0 == truthvalue_true_node
9654 || !TREE_OVERFLOW (orig_op1)));
9656 break;
9658 /* Shift operations: result has same type as first operand;
9659 always convert second operand to int.
9660 Also set SHORT_SHIFT if shifting rightward. */
9662 case RSHIFT_EXPR:
9663 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9664 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9666 result_type = type0;
9667 converted = 1;
9669 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9670 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9671 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9672 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9674 result_type = type0;
9675 converted = 1;
9677 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9678 && code1 == INTEGER_TYPE)
9680 if (TREE_CODE (op1) == INTEGER_CST)
9682 if (tree_int_cst_sgn (op1) < 0)
9684 int_const = false;
9685 if (c_inhibit_evaluation_warnings == 0)
9686 warning (0, "right shift count is negative");
9688 else
9690 if (!integer_zerop (op1))
9691 short_shift = 1;
9693 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9695 int_const = false;
9696 if (c_inhibit_evaluation_warnings == 0)
9697 warning (0, "right shift count >= width of type");
9702 /* Use the type of the value to be shifted. */
9703 result_type = type0;
9704 /* Convert the non vector shift-count to an integer, regardless
9705 of size of value being shifted. */
9706 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9707 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9708 op1 = convert (integer_type_node, op1);
9709 /* Avoid converting op1 to result_type later. */
9710 converted = 1;
9712 break;
9714 case LSHIFT_EXPR:
9715 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9716 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9718 result_type = type0;
9719 converted = 1;
9721 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9722 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9723 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9724 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9726 result_type = type0;
9727 converted = 1;
9729 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9730 && code1 == INTEGER_TYPE)
9732 if (TREE_CODE (op1) == INTEGER_CST)
9734 if (tree_int_cst_sgn (op1) < 0)
9736 int_const = false;
9737 if (c_inhibit_evaluation_warnings == 0)
9738 warning (0, "left shift count is negative");
9741 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9743 int_const = false;
9744 if (c_inhibit_evaluation_warnings == 0)
9745 warning (0, "left shift count >= width of type");
9749 /* Use the type of the value to be shifted. */
9750 result_type = type0;
9751 /* Convert the non vector shift-count to an integer, regardless
9752 of size of value being shifted. */
9753 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9754 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9755 op1 = convert (integer_type_node, op1);
9756 /* Avoid converting op1 to result_type later. */
9757 converted = 1;
9759 break;
9761 case EQ_EXPR:
9762 case NE_EXPR:
9763 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9764 warning_at (location,
9765 OPT_Wfloat_equal,
9766 "comparing floating point with == or != is unsafe");
9767 /* Result of comparison is always int,
9768 but don't convert the args to int! */
9769 build_type = integer_type_node;
9770 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9771 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9772 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9773 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9774 short_compare = 1;
9775 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9777 if (TREE_CODE (op0) == ADDR_EXPR
9778 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9780 if (code == EQ_EXPR)
9781 warning_at (location,
9782 OPT_Waddress,
9783 "the comparison will always evaluate as %<false%> "
9784 "for the address of %qD will never be NULL",
9785 TREE_OPERAND (op0, 0));
9786 else
9787 warning_at (location,
9788 OPT_Waddress,
9789 "the comparison will always evaluate as %<true%> "
9790 "for the address of %qD will never be NULL",
9791 TREE_OPERAND (op0, 0));
9793 result_type = type0;
9795 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9797 if (TREE_CODE (op1) == ADDR_EXPR
9798 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9800 if (code == EQ_EXPR)
9801 warning_at (location,
9802 OPT_Waddress,
9803 "the comparison will always evaluate as %<false%> "
9804 "for the address of %qD will never be NULL",
9805 TREE_OPERAND (op1, 0));
9806 else
9807 warning_at (location,
9808 OPT_Waddress,
9809 "the comparison will always evaluate as %<true%> "
9810 "for the address of %qD will never be NULL",
9811 TREE_OPERAND (op1, 0));
9813 result_type = type1;
9815 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9817 tree tt0 = TREE_TYPE (type0);
9818 tree tt1 = TREE_TYPE (type1);
9819 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
9820 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
9821 addr_space_t as_common = ADDR_SPACE_GENERIC;
9823 /* Anything compares with void *. void * compares with anything.
9824 Otherwise, the targets must be compatible
9825 and both must be object or both incomplete. */
9826 if (comp_target_types (location, type0, type1))
9827 result_type = common_pointer_type (type0, type1);
9828 else if (!addr_space_superset (as0, as1, &as_common))
9830 error_at (location, "comparison of pointers to "
9831 "disjoint address spaces");
9832 return error_mark_node;
9834 else if (VOID_TYPE_P (tt0))
9836 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
9837 pedwarn (location, OPT_pedantic, "ISO C forbids "
9838 "comparison of %<void *%> with function pointer");
9840 else if (VOID_TYPE_P (tt1))
9842 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
9843 pedwarn (location, OPT_pedantic, "ISO C forbids "
9844 "comparison of %<void *%> with function pointer");
9846 else
9847 /* Avoid warning about the volatile ObjC EH puts on decls. */
9848 if (!objc_ok)
9849 pedwarn (location, 0,
9850 "comparison of distinct pointer types lacks a cast");
9852 if (result_type == NULL_TREE)
9854 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9855 result_type = build_pointer_type
9856 (build_qualified_type (void_type_node, qual));
9859 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9861 result_type = type0;
9862 pedwarn (location, 0, "comparison between pointer and integer");
9864 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9866 result_type = type1;
9867 pedwarn (location, 0, "comparison between pointer and integer");
9869 break;
9871 case LE_EXPR:
9872 case GE_EXPR:
9873 case LT_EXPR:
9874 case GT_EXPR:
9875 build_type = integer_type_node;
9876 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9877 || code0 == FIXED_POINT_TYPE)
9878 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9879 || code1 == FIXED_POINT_TYPE))
9880 short_compare = 1;
9881 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9883 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
9884 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
9885 addr_space_t as_common;
9887 if (comp_target_types (location, type0, type1))
9889 result_type = common_pointer_type (type0, type1);
9890 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9891 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9892 pedwarn (location, 0,
9893 "comparison of complete and incomplete pointers");
9894 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9895 pedwarn (location, OPT_pedantic, "ISO C forbids "
9896 "ordered comparisons of pointers to functions");
9897 else if (null_pointer_constant_p (orig_op0)
9898 || null_pointer_constant_p (orig_op1))
9899 warning_at (location, OPT_Wextra,
9900 "ordered comparison of pointer with null pointer");
9903 else if (!addr_space_superset (as0, as1, &as_common))
9905 error_at (location, "comparison of pointers to "
9906 "disjoint address spaces");
9907 return error_mark_node;
9909 else
9911 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9912 result_type = build_pointer_type
9913 (build_qualified_type (void_type_node, qual));
9914 pedwarn (location, 0,
9915 "comparison of distinct pointer types lacks a cast");
9918 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9920 result_type = type0;
9921 if (pedantic)
9922 pedwarn (location, OPT_pedantic,
9923 "ordered comparison of pointer with integer zero");
9924 else if (extra_warnings)
9925 warning_at (location, OPT_Wextra,
9926 "ordered comparison of pointer with integer zero");
9928 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9930 result_type = type1;
9931 if (pedantic)
9932 pedwarn (location, OPT_pedantic,
9933 "ordered comparison of pointer with integer zero");
9934 else if (extra_warnings)
9935 warning_at (location, OPT_Wextra,
9936 "ordered comparison of pointer with integer zero");
9938 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9940 result_type = type0;
9941 pedwarn (location, 0, "comparison between pointer and integer");
9943 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9945 result_type = type1;
9946 pedwarn (location, 0, "comparison between pointer and integer");
9948 break;
9950 default:
9951 gcc_unreachable ();
9954 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9955 return error_mark_node;
9957 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9958 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9959 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9960 TREE_TYPE (type1))))
9962 binary_op_error (location, code, type0, type1);
9963 return error_mark_node;
9966 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9967 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9969 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9970 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9972 bool first_complex = (code0 == COMPLEX_TYPE);
9973 bool second_complex = (code1 == COMPLEX_TYPE);
9974 int none_complex = (!first_complex && !second_complex);
9976 if (shorten || common || short_compare)
9978 result_type = c_common_type (type0, type1);
9979 do_warn_double_promotion (result_type, type0, type1,
9980 "implicit conversion from %qT to %qT "
9981 "to match other operand of binary "
9982 "expression",
9983 location);
9984 if (result_type == error_mark_node)
9985 return error_mark_node;
9988 if (first_complex != second_complex
9989 && (code == PLUS_EXPR
9990 || code == MINUS_EXPR
9991 || code == MULT_EXPR
9992 || (code == TRUNC_DIV_EXPR && first_complex))
9993 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
9994 && flag_signed_zeros)
9996 /* An operation on mixed real/complex operands must be
9997 handled specially, but the language-independent code can
9998 more easily optimize the plain complex arithmetic if
9999 -fno-signed-zeros. */
10000 tree real_type = TREE_TYPE (result_type);
10001 tree real, imag;
10002 if (type0 != orig_type0 || type1 != orig_type1)
10004 gcc_assert (may_need_excess_precision && common);
10005 semantic_result_type = c_common_type (orig_type0, orig_type1);
10007 if (first_complex)
10009 if (TREE_TYPE (op0) != result_type)
10010 op0 = convert_and_check (result_type, op0);
10011 if (TREE_TYPE (op1) != real_type)
10012 op1 = convert_and_check (real_type, op1);
10014 else
10016 if (TREE_TYPE (op0) != real_type)
10017 op0 = convert_and_check (real_type, op0);
10018 if (TREE_TYPE (op1) != result_type)
10019 op1 = convert_and_check (result_type, op1);
10021 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10022 return error_mark_node;
10023 if (first_complex)
10025 op0 = c_save_expr (op0);
10026 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10027 op0, 1);
10028 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10029 op0, 1);
10030 switch (code)
10032 case MULT_EXPR:
10033 case TRUNC_DIV_EXPR:
10034 imag = build2 (resultcode, real_type, imag, op1);
10035 /* Fall through. */
10036 case PLUS_EXPR:
10037 case MINUS_EXPR:
10038 real = build2 (resultcode, real_type, real, op1);
10039 break;
10040 default:
10041 gcc_unreachable();
10044 else
10046 op1 = c_save_expr (op1);
10047 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10048 op1, 1);
10049 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10050 op1, 1);
10051 switch (code)
10053 case MULT_EXPR:
10054 imag = build2 (resultcode, real_type, op0, imag);
10055 /* Fall through. */
10056 case PLUS_EXPR:
10057 real = build2 (resultcode, real_type, op0, real);
10058 break;
10059 case MINUS_EXPR:
10060 real = build2 (resultcode, real_type, op0, real);
10061 imag = build1 (NEGATE_EXPR, real_type, imag);
10062 break;
10063 default:
10064 gcc_unreachable();
10067 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10068 goto return_build_binary_op;
10071 /* For certain operations (which identify themselves by shorten != 0)
10072 if both args were extended from the same smaller type,
10073 do the arithmetic in that type and then extend.
10075 shorten !=0 and !=1 indicates a bitwise operation.
10076 For them, this optimization is safe only if
10077 both args are zero-extended or both are sign-extended.
10078 Otherwise, we might change the result.
10079 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10080 but calculated in (unsigned short) it would be (unsigned short)-1. */
10082 if (shorten && none_complex)
10084 final_type = result_type;
10085 result_type = shorten_binary_op (result_type, op0, op1,
10086 shorten == -1);
10089 /* Shifts can be shortened if shifting right. */
10091 if (short_shift)
10093 int unsigned_arg;
10094 tree arg0 = get_narrower (op0, &unsigned_arg);
10096 final_type = result_type;
10098 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10099 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10101 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10102 && tree_int_cst_sgn (op1) > 0
10103 /* We can shorten only if the shift count is less than the
10104 number of bits in the smaller type size. */
10105 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10106 /* We cannot drop an unsigned shift after sign-extension. */
10107 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10109 /* Do an unsigned shift if the operand was zero-extended. */
10110 result_type
10111 = c_common_signed_or_unsigned_type (unsigned_arg,
10112 TREE_TYPE (arg0));
10113 /* Convert value-to-be-shifted to that type. */
10114 if (TREE_TYPE (op0) != result_type)
10115 op0 = convert (result_type, op0);
10116 converted = 1;
10120 /* Comparison operations are shortened too but differently.
10121 They identify themselves by setting short_compare = 1. */
10123 if (short_compare)
10125 /* Don't write &op0, etc., because that would prevent op0
10126 from being kept in a register.
10127 Instead, make copies of the our local variables and
10128 pass the copies by reference, then copy them back afterward. */
10129 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10130 enum tree_code xresultcode = resultcode;
10131 tree val
10132 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10134 if (val != 0)
10136 ret = val;
10137 goto return_build_binary_op;
10140 op0 = xop0, op1 = xop1;
10141 converted = 1;
10142 resultcode = xresultcode;
10144 if (c_inhibit_evaluation_warnings == 0)
10146 bool op0_maybe_const = true;
10147 bool op1_maybe_const = true;
10148 tree orig_op0_folded, orig_op1_folded;
10150 if (in_late_binary_op)
10152 orig_op0_folded = orig_op0;
10153 orig_op1_folded = orig_op1;
10155 else
10157 /* Fold for the sake of possible warnings, as in
10158 build_conditional_expr. This requires the
10159 "original" values to be folded, not just op0 and
10160 op1. */
10161 c_inhibit_evaluation_warnings++;
10162 op0 = c_fully_fold (op0, require_constant_value,
10163 &op0_maybe_const);
10164 op1 = c_fully_fold (op1, require_constant_value,
10165 &op1_maybe_const);
10166 c_inhibit_evaluation_warnings--;
10167 orig_op0_folded = c_fully_fold (orig_op0,
10168 require_constant_value,
10169 NULL);
10170 orig_op1_folded = c_fully_fold (orig_op1,
10171 require_constant_value,
10172 NULL);
10175 if (warn_sign_compare)
10176 warn_for_sign_compare (location, orig_op0_folded,
10177 orig_op1_folded, op0, op1,
10178 result_type, resultcode);
10179 if (!in_late_binary_op)
10181 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10182 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10183 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10184 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10190 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10191 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10192 Then the expression will be built.
10193 It will be given type FINAL_TYPE if that is nonzero;
10194 otherwise, it will be given type RESULT_TYPE. */
10196 if (!result_type)
10198 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10199 return error_mark_node;
10202 if (build_type == NULL_TREE)
10204 build_type = result_type;
10205 if ((type0 != orig_type0 || type1 != orig_type1)
10206 && !boolean_op)
10208 gcc_assert (may_need_excess_precision && common);
10209 semantic_result_type = c_common_type (orig_type0, orig_type1);
10213 if (!converted)
10215 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10216 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10218 /* This can happen if one operand has a vector type, and the other
10219 has a different type. */
10220 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10221 return error_mark_node;
10224 /* Treat expressions in initializers specially as they can't trap. */
10225 if (int_const_or_overflow)
10226 ret = (require_constant_value
10227 ? fold_build2_initializer_loc (location, resultcode, build_type,
10228 op0, op1)
10229 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10230 else
10231 ret = build2 (resultcode, build_type, op0, op1);
10232 if (final_type != 0)
10233 ret = convert (final_type, ret);
10235 return_build_binary_op:
10236 gcc_assert (ret != error_mark_node);
10237 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10238 ret = (int_operands
10239 ? note_integer_operands (ret)
10240 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10241 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10242 && !in_late_binary_op)
10243 ret = note_integer_operands (ret);
10244 if (semantic_result_type)
10245 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10246 protected_set_expr_location (ret, location);
10247 return ret;
10251 /* Convert EXPR to be a truth-value, validating its type for this
10252 purpose. LOCATION is the source location for the expression. */
10254 tree
10255 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10257 bool int_const, int_operands;
10259 switch (TREE_CODE (TREE_TYPE (expr)))
10261 case ARRAY_TYPE:
10262 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10263 return error_mark_node;
10265 case RECORD_TYPE:
10266 error_at (location, "used struct type value where scalar is required");
10267 return error_mark_node;
10269 case UNION_TYPE:
10270 error_at (location, "used union type value where scalar is required");
10271 return error_mark_node;
10273 case VOID_TYPE:
10274 error_at (location, "void value not ignored as it ought to be");
10275 return error_mark_node;
10277 case FUNCTION_TYPE:
10278 gcc_unreachable ();
10280 default:
10281 break;
10284 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10285 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10286 if (int_operands)
10287 expr = remove_c_maybe_const_expr (expr);
10289 /* ??? Should we also give an error for vectors rather than leaving
10290 those to give errors later? */
10291 expr = c_common_truthvalue_conversion (location, expr);
10293 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10295 if (TREE_OVERFLOW (expr))
10296 return expr;
10297 else
10298 return note_integer_operands (expr);
10300 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10301 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10302 return expr;
10306 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10307 required. */
10309 tree
10310 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10312 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10314 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10315 /* Executing a compound literal inside a function reinitializes
10316 it. */
10317 if (!TREE_STATIC (decl))
10318 *se = true;
10319 return decl;
10321 else
10322 return expr;
10325 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10327 tree
10328 c_begin_omp_parallel (void)
10330 tree block;
10332 keep_next_level ();
10333 block = c_begin_compound_stmt (true);
10335 return block;
10338 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10339 statement. LOC is the location of the OMP_PARALLEL. */
10341 tree
10342 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10344 tree stmt;
10346 block = c_end_compound_stmt (loc, block, true);
10348 stmt = make_node (OMP_PARALLEL);
10349 TREE_TYPE (stmt) = void_type_node;
10350 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10351 OMP_PARALLEL_BODY (stmt) = block;
10352 SET_EXPR_LOCATION (stmt, loc);
10354 return add_stmt (stmt);
10357 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10359 tree
10360 c_begin_omp_task (void)
10362 tree block;
10364 keep_next_level ();
10365 block = c_begin_compound_stmt (true);
10367 return block;
10370 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10371 statement. LOC is the location of the #pragma. */
10373 tree
10374 c_finish_omp_task (location_t loc, tree clauses, tree block)
10376 tree stmt;
10378 block = c_end_compound_stmt (loc, block, true);
10380 stmt = make_node (OMP_TASK);
10381 TREE_TYPE (stmt) = void_type_node;
10382 OMP_TASK_CLAUSES (stmt) = clauses;
10383 OMP_TASK_BODY (stmt) = block;
10384 SET_EXPR_LOCATION (stmt, loc);
10386 return add_stmt (stmt);
10389 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10390 Remove any elements from the list that are invalid. */
10392 tree
10393 c_finish_omp_clauses (tree clauses)
10395 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10396 tree c, t, *pc = &clauses;
10397 const char *name;
10399 bitmap_obstack_initialize (NULL);
10400 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10401 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10402 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10404 for (pc = &clauses, c = clauses; c ; c = *pc)
10406 bool remove = false;
10407 bool need_complete = false;
10408 bool need_implicitly_determined = false;
10410 switch (OMP_CLAUSE_CODE (c))
10412 case OMP_CLAUSE_SHARED:
10413 name = "shared";
10414 need_implicitly_determined = true;
10415 goto check_dup_generic;
10417 case OMP_CLAUSE_PRIVATE:
10418 name = "private";
10419 need_complete = true;
10420 need_implicitly_determined = true;
10421 goto check_dup_generic;
10423 case OMP_CLAUSE_REDUCTION:
10424 name = "reduction";
10425 need_implicitly_determined = true;
10426 t = OMP_CLAUSE_DECL (c);
10427 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10428 || POINTER_TYPE_P (TREE_TYPE (t)))
10430 error_at (OMP_CLAUSE_LOCATION (c),
10431 "%qE has invalid type for %<reduction%>", t);
10432 remove = true;
10434 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10436 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10437 const char *r_name = NULL;
10439 switch (r_code)
10441 case PLUS_EXPR:
10442 case MULT_EXPR:
10443 case MINUS_EXPR:
10444 break;
10445 case BIT_AND_EXPR:
10446 r_name = "&";
10447 break;
10448 case BIT_XOR_EXPR:
10449 r_name = "^";
10450 break;
10451 case BIT_IOR_EXPR:
10452 r_name = "|";
10453 break;
10454 case TRUTH_ANDIF_EXPR:
10455 r_name = "&&";
10456 break;
10457 case TRUTH_ORIF_EXPR:
10458 r_name = "||";
10459 break;
10460 default:
10461 gcc_unreachable ();
10463 if (r_name)
10465 error_at (OMP_CLAUSE_LOCATION (c),
10466 "%qE has invalid type for %<reduction(%s)%>",
10467 t, r_name);
10468 remove = true;
10471 goto check_dup_generic;
10473 case OMP_CLAUSE_COPYPRIVATE:
10474 name = "copyprivate";
10475 goto check_dup_generic;
10477 case OMP_CLAUSE_COPYIN:
10478 name = "copyin";
10479 t = OMP_CLAUSE_DECL (c);
10480 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10482 error_at (OMP_CLAUSE_LOCATION (c),
10483 "%qE must be %<threadprivate%> for %<copyin%>", t);
10484 remove = true;
10486 goto check_dup_generic;
10488 check_dup_generic:
10489 t = OMP_CLAUSE_DECL (c);
10490 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10492 error_at (OMP_CLAUSE_LOCATION (c),
10493 "%qE is not a variable in clause %qs", t, name);
10494 remove = true;
10496 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10497 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10498 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10500 error_at (OMP_CLAUSE_LOCATION (c),
10501 "%qE appears more than once in data clauses", t);
10502 remove = true;
10504 else
10505 bitmap_set_bit (&generic_head, DECL_UID (t));
10506 break;
10508 case OMP_CLAUSE_FIRSTPRIVATE:
10509 name = "firstprivate";
10510 t = OMP_CLAUSE_DECL (c);
10511 need_complete = true;
10512 need_implicitly_determined = true;
10513 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10515 error_at (OMP_CLAUSE_LOCATION (c),
10516 "%qE is not a variable in clause %<firstprivate%>", t);
10517 remove = true;
10519 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10520 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10522 error_at (OMP_CLAUSE_LOCATION (c),
10523 "%qE appears more than once in data clauses", t);
10524 remove = true;
10526 else
10527 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10528 break;
10530 case OMP_CLAUSE_LASTPRIVATE:
10531 name = "lastprivate";
10532 t = OMP_CLAUSE_DECL (c);
10533 need_complete = true;
10534 need_implicitly_determined = true;
10535 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10537 error_at (OMP_CLAUSE_LOCATION (c),
10538 "%qE is not a variable in clause %<lastprivate%>", t);
10539 remove = true;
10541 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10542 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10544 error_at (OMP_CLAUSE_LOCATION (c),
10545 "%qE appears more than once in data clauses", t);
10546 remove = true;
10548 else
10549 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10550 break;
10552 case OMP_CLAUSE_IF:
10553 case OMP_CLAUSE_NUM_THREADS:
10554 case OMP_CLAUSE_SCHEDULE:
10555 case OMP_CLAUSE_NOWAIT:
10556 case OMP_CLAUSE_ORDERED:
10557 case OMP_CLAUSE_DEFAULT:
10558 case OMP_CLAUSE_UNTIED:
10559 case OMP_CLAUSE_COLLAPSE:
10560 pc = &OMP_CLAUSE_CHAIN (c);
10561 continue;
10563 default:
10564 gcc_unreachable ();
10567 if (!remove)
10569 t = OMP_CLAUSE_DECL (c);
10571 if (need_complete)
10573 t = require_complete_type (t);
10574 if (t == error_mark_node)
10575 remove = true;
10578 if (need_implicitly_determined)
10580 const char *share_name = NULL;
10582 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10583 share_name = "threadprivate";
10584 else switch (c_omp_predetermined_sharing (t))
10586 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10587 break;
10588 case OMP_CLAUSE_DEFAULT_SHARED:
10589 share_name = "shared";
10590 break;
10591 case OMP_CLAUSE_DEFAULT_PRIVATE:
10592 share_name = "private";
10593 break;
10594 default:
10595 gcc_unreachable ();
10597 if (share_name)
10599 error_at (OMP_CLAUSE_LOCATION (c),
10600 "%qE is predetermined %qs for %qs",
10601 t, share_name, name);
10602 remove = true;
10607 if (remove)
10608 *pc = OMP_CLAUSE_CHAIN (c);
10609 else
10610 pc = &OMP_CLAUSE_CHAIN (c);
10613 bitmap_obstack_release (NULL);
10614 return clauses;
10617 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10618 down to the element type of an array. */
10620 tree
10621 c_build_qualified_type (tree type, int type_quals)
10623 if (type == error_mark_node)
10624 return type;
10626 if (TREE_CODE (type) == ARRAY_TYPE)
10628 tree t;
10629 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10630 type_quals);
10632 /* See if we already have an identically qualified type. */
10633 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10635 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10636 && TYPE_NAME (t) == TYPE_NAME (type)
10637 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10638 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10639 TYPE_ATTRIBUTES (type)))
10640 break;
10642 if (!t)
10644 tree domain = TYPE_DOMAIN (type);
10646 t = build_variant_type_copy (type);
10647 TREE_TYPE (t) = element_type;
10649 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10650 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10651 SET_TYPE_STRUCTURAL_EQUALITY (t);
10652 else if (TYPE_CANONICAL (element_type) != element_type
10653 || (domain && TYPE_CANONICAL (domain) != domain))
10655 tree unqualified_canon
10656 = build_array_type (TYPE_CANONICAL (element_type),
10657 domain? TYPE_CANONICAL (domain)
10658 : NULL_TREE);
10659 TYPE_CANONICAL (t)
10660 = c_build_qualified_type (unqualified_canon, type_quals);
10662 else
10663 TYPE_CANONICAL (t) = t;
10665 return t;
10668 /* A restrict-qualified pointer type must be a pointer to object or
10669 incomplete type. Note that the use of POINTER_TYPE_P also allows
10670 REFERENCE_TYPEs, which is appropriate for C++. */
10671 if ((type_quals & TYPE_QUAL_RESTRICT)
10672 && (!POINTER_TYPE_P (type)
10673 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10675 error ("invalid use of %<restrict%>");
10676 type_quals &= ~TYPE_QUAL_RESTRICT;
10679 return build_qualified_type (type, type_quals);
10682 /* Build a VA_ARG_EXPR for the C parser. */
10684 tree
10685 c_build_va_arg (location_t loc, tree expr, tree type)
10687 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10688 warning_at (loc, OPT_Wc___compat,
10689 "C++ requires promoted type, not enum type, in %<va_arg%>");
10690 return build_va_arg (loc, expr, type);