Merge from mainline (165734:167278).
[official-gcc/graphite-test-results.git] / gcc / c-typeck.c
blob09e9ca730394e49722bd71a8e1eba6733cf18517
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
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 "toplev.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "tree-iterator.h"
42 #include "bitmap.h"
43 #include "gimple.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_error (tree, enum lvalue_use);
101 static void readonly_warning (tree, enum lvalue_use);
102 static int lvalue_or_else (const_tree, enum lvalue_use);
103 static void record_maybe_used_decl (tree);
104 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
106 /* Return true if EXP is a null pointer constant, false otherwise. */
108 static bool
109 null_pointer_constant_p (const_tree expr)
111 /* This should really operate on c_expr structures, but they aren't
112 yet available everywhere required. */
113 tree type = TREE_TYPE (expr);
114 return (TREE_CODE (expr) == INTEGER_CST
115 && !TREE_OVERFLOW (expr)
116 && integer_zerop (expr)
117 && (INTEGRAL_TYPE_P (type)
118 || (TREE_CODE (type) == POINTER_TYPE
119 && VOID_TYPE_P (TREE_TYPE (type))
120 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
123 /* EXPR may appear in an unevaluated part of an integer constant
124 expression, but not in an evaluated part. Wrap it in a
125 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
126 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
128 static tree
129 note_integer_operands (tree expr)
131 tree ret;
132 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
134 ret = copy_node (expr);
135 TREE_OVERFLOW (ret) = 1;
137 else
139 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
140 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
142 return ret;
145 /* Having checked whether EXPR may appear in an unevaluated part of an
146 integer constant expression and found that it may, remove any
147 C_MAYBE_CONST_EXPR noting this fact and return the resulting
148 expression. */
150 static inline tree
151 remove_c_maybe_const_expr (tree expr)
153 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
154 return C_MAYBE_CONST_EXPR_EXPR (expr);
155 else
156 return expr;
159 \f/* This is a cache to hold if two types are compatible or not. */
161 struct tagged_tu_seen_cache {
162 const struct tagged_tu_seen_cache * next;
163 const_tree t1;
164 const_tree t2;
165 /* The return value of tagged_types_tu_compatible_p if we had seen
166 these two types already. */
167 int val;
170 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
171 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
173 /* Do `exp = require_complete_type (exp);' to make sure exp
174 does not have an incomplete type. (That includes void types.) */
176 tree
177 require_complete_type (tree value)
179 tree type = TREE_TYPE (value);
181 if (value == error_mark_node || type == error_mark_node)
182 return error_mark_node;
184 /* First, detect a valid value with a complete type. */
185 if (COMPLETE_TYPE_P (type))
186 return value;
188 c_incomplete_type_error (value, type);
189 return error_mark_node;
192 /* Print an error message for invalid use of an incomplete type.
193 VALUE is the expression that was used (or 0 if that isn't known)
194 and TYPE is the type that was invalid. */
196 void
197 c_incomplete_type_error (const_tree value, const_tree type)
199 const char *type_code_string;
201 /* Avoid duplicate error message. */
202 if (TREE_CODE (type) == ERROR_MARK)
203 return;
205 if (value != 0 && (TREE_CODE (value) == VAR_DECL
206 || TREE_CODE (value) == PARM_DECL))
207 error ("%qD has an incomplete type", value);
208 else
210 retry:
211 /* We must print an error message. Be clever about what it says. */
213 switch (TREE_CODE (type))
215 case RECORD_TYPE:
216 type_code_string = "struct";
217 break;
219 case UNION_TYPE:
220 type_code_string = "union";
221 break;
223 case ENUMERAL_TYPE:
224 type_code_string = "enum";
225 break;
227 case VOID_TYPE:
228 error ("invalid use of void expression");
229 return;
231 case ARRAY_TYPE:
232 if (TYPE_DOMAIN (type))
234 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
236 error ("invalid use of flexible array member");
237 return;
239 type = TREE_TYPE (type);
240 goto retry;
242 error ("invalid use of array with unspecified bounds");
243 return;
245 default:
246 gcc_unreachable ();
249 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
250 error ("invalid use of undefined type %<%s %E%>",
251 type_code_string, TYPE_NAME (type));
252 else
253 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
254 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
258 /* Given a type, apply default promotions wrt unnamed function
259 arguments and return the new type. */
261 tree
262 c_type_promotes_to (tree type)
264 if (TYPE_MAIN_VARIANT (type) == float_type_node)
265 return double_type_node;
267 if (c_promoting_integer_type_p (type))
269 /* Preserve unsignedness if not really getting any wider. */
270 if (TYPE_UNSIGNED (type)
271 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
272 return unsigned_type_node;
273 return integer_type_node;
276 return type;
279 /* Return true if between two named address spaces, whether there is a superset
280 named address space that encompasses both address spaces. If there is a
281 superset, return which address space is the superset. */
283 static bool
284 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
286 if (as1 == as2)
288 *common = as1;
289 return true;
291 else if (targetm.addr_space.subset_p (as1, as2))
293 *common = as2;
294 return true;
296 else if (targetm.addr_space.subset_p (as2, as1))
298 *common = as1;
299 return true;
301 else
302 return false;
305 /* Return a variant of TYPE which has all the type qualifiers of LIKE
306 as well as those of TYPE. */
308 static tree
309 qualify_type (tree type, tree like)
311 addr_space_t as_type = TYPE_ADDR_SPACE (type);
312 addr_space_t as_like = TYPE_ADDR_SPACE (like);
313 addr_space_t as_common;
315 /* If the two named address spaces are different, determine the common
316 superset address space. If there isn't one, raise an error. */
317 if (!addr_space_superset (as_type, as_like, &as_common))
319 as_common = as_type;
320 error ("%qT and %qT are in disjoint named address spaces",
321 type, like);
324 return c_build_qualified_type (type,
325 TYPE_QUALS_NO_ADDR_SPACE (type)
326 | TYPE_QUALS_NO_ADDR_SPACE (like)
327 | ENCODE_QUAL_ADDR_SPACE (as_common));
330 /* Return true iff the given tree T is a variable length array. */
332 bool
333 c_vla_type_p (const_tree t)
335 if (TREE_CODE (t) == ARRAY_TYPE
336 && C_TYPE_VARIABLE_SIZE (t))
337 return true;
338 return false;
341 /* Return the composite type of two compatible types.
343 We assume that comptypes has already been done and returned
344 nonzero; if that isn't so, this may crash. In particular, we
345 assume that qualifiers match. */
347 tree
348 composite_type (tree t1, tree t2)
350 enum tree_code code1;
351 enum tree_code code2;
352 tree attributes;
354 /* Save time if the two types are the same. */
356 if (t1 == t2) return t1;
358 /* If one type is nonsense, use the other. */
359 if (t1 == error_mark_node)
360 return t2;
361 if (t2 == error_mark_node)
362 return t1;
364 code1 = TREE_CODE (t1);
365 code2 = TREE_CODE (t2);
367 /* Merge the attributes. */
368 attributes = targetm.merge_type_attributes (t1, t2);
370 /* If one is an enumerated type and the other is the compatible
371 integer type, the composite type might be either of the two
372 (DR#013 question 3). For consistency, use the enumerated type as
373 the composite type. */
375 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
376 return t1;
377 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
378 return t2;
380 gcc_assert (code1 == code2);
382 switch (code1)
384 case POINTER_TYPE:
385 /* For two pointers, do this recursively on the target type. */
387 tree pointed_to_1 = TREE_TYPE (t1);
388 tree pointed_to_2 = TREE_TYPE (t2);
389 tree target = composite_type (pointed_to_1, pointed_to_2);
390 t1 = build_pointer_type (target);
391 t1 = build_type_attribute_variant (t1, attributes);
392 return qualify_type (t1, t2);
395 case ARRAY_TYPE:
397 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
398 int quals;
399 tree unqual_elt;
400 tree d1 = TYPE_DOMAIN (t1);
401 tree d2 = TYPE_DOMAIN (t2);
402 bool d1_variable, d2_variable;
403 bool d1_zero, d2_zero;
404 bool t1_complete, t2_complete;
406 /* We should not have any type quals on arrays at all. */
407 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
408 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
410 t1_complete = COMPLETE_TYPE_P (t1);
411 t2_complete = COMPLETE_TYPE_P (t2);
413 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
414 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
416 d1_variable = (!d1_zero
417 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
418 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
419 d2_variable = (!d2_zero
420 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
421 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
422 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
423 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
425 /* Save space: see if the result is identical to one of the args. */
426 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
427 && (d2_variable || d2_zero || !d1_variable))
428 return build_type_attribute_variant (t1, attributes);
429 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
430 && (d1_variable || d1_zero || !d2_variable))
431 return build_type_attribute_variant (t2, attributes);
433 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
434 return build_type_attribute_variant (t1, attributes);
435 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
436 return build_type_attribute_variant (t2, attributes);
438 /* Merge the element types, and have a size if either arg has
439 one. We may have qualifiers on the element types. To set
440 up TYPE_MAIN_VARIANT correctly, we need to form the
441 composite of the unqualified types and add the qualifiers
442 back at the end. */
443 quals = TYPE_QUALS (strip_array_types (elt));
444 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
445 t1 = build_array_type (unqual_elt,
446 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
447 && (d2_variable
448 || d2_zero
449 || !d1_variable))
450 ? t1
451 : t2));
452 /* Ensure a composite type involving a zero-length array type
453 is a zero-length type not an incomplete type. */
454 if (d1_zero && d2_zero
455 && (t1_complete || t2_complete)
456 && !COMPLETE_TYPE_P (t1))
458 TYPE_SIZE (t1) = bitsize_zero_node;
459 TYPE_SIZE_UNIT (t1) = size_zero_node;
461 t1 = c_build_qualified_type (t1, quals);
462 return build_type_attribute_variant (t1, attributes);
465 case ENUMERAL_TYPE:
466 case RECORD_TYPE:
467 case UNION_TYPE:
468 if (attributes != NULL)
470 /* Try harder not to create a new aggregate type. */
471 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
472 return t1;
473 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
474 return t2;
476 return build_type_attribute_variant (t1, attributes);
478 case FUNCTION_TYPE:
479 /* Function types: prefer the one that specified arg types.
480 If both do, merge the arg types. Also merge the return types. */
482 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
483 tree p1 = TYPE_ARG_TYPES (t1);
484 tree p2 = TYPE_ARG_TYPES (t2);
485 int len;
486 tree newargs, n;
487 int i;
489 /* Save space: see if the result is identical to one of the args. */
490 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
491 return build_type_attribute_variant (t1, attributes);
492 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
493 return build_type_attribute_variant (t2, attributes);
495 /* Simple way if one arg fails to specify argument types. */
496 if (TYPE_ARG_TYPES (t1) == 0)
498 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
499 t1 = build_type_attribute_variant (t1, attributes);
500 return qualify_type (t1, t2);
502 if (TYPE_ARG_TYPES (t2) == 0)
504 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
505 t1 = build_type_attribute_variant (t1, attributes);
506 return qualify_type (t1, t2);
509 /* If both args specify argument types, we must merge the two
510 lists, argument by argument. */
511 /* Tell global_bindings_p to return false so that variable_size
512 doesn't die on VLAs in parameter types. */
513 c_override_global_bindings_to_false = true;
515 len = list_length (p1);
516 newargs = 0;
518 for (i = 0; i < len; i++)
519 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
521 n = newargs;
523 for (; p1;
524 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
526 /* A null type means arg type is not specified.
527 Take whatever the other function type has. */
528 if (TREE_VALUE (p1) == 0)
530 TREE_VALUE (n) = TREE_VALUE (p2);
531 goto parm_done;
533 if (TREE_VALUE (p2) == 0)
535 TREE_VALUE (n) = TREE_VALUE (p1);
536 goto parm_done;
539 /* Given wait (union {union wait *u; int *i} *)
540 and wait (union wait *),
541 prefer union wait * as type of parm. */
542 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
543 && TREE_VALUE (p1) != TREE_VALUE (p2))
545 tree memb;
546 tree mv2 = TREE_VALUE (p2);
547 if (mv2 && mv2 != error_mark_node
548 && TREE_CODE (mv2) != ARRAY_TYPE)
549 mv2 = TYPE_MAIN_VARIANT (mv2);
550 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
551 memb; memb = DECL_CHAIN (memb))
553 tree mv3 = TREE_TYPE (memb);
554 if (mv3 && mv3 != error_mark_node
555 && TREE_CODE (mv3) != ARRAY_TYPE)
556 mv3 = TYPE_MAIN_VARIANT (mv3);
557 if (comptypes (mv3, mv2))
559 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
560 TREE_VALUE (p2));
561 pedwarn (input_location, OPT_pedantic,
562 "function types not truly compatible in ISO C");
563 goto parm_done;
567 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
568 && TREE_VALUE (p2) != TREE_VALUE (p1))
570 tree memb;
571 tree mv1 = TREE_VALUE (p1);
572 if (mv1 && mv1 != error_mark_node
573 && TREE_CODE (mv1) != ARRAY_TYPE)
574 mv1 = TYPE_MAIN_VARIANT (mv1);
575 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
576 memb; memb = DECL_CHAIN (memb))
578 tree mv3 = TREE_TYPE (memb);
579 if (mv3 && mv3 != error_mark_node
580 && TREE_CODE (mv3) != ARRAY_TYPE)
581 mv3 = TYPE_MAIN_VARIANT (mv3);
582 if (comptypes (mv3, mv1))
584 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
585 TREE_VALUE (p1));
586 pedwarn (input_location, OPT_pedantic,
587 "function types not truly compatible in ISO C");
588 goto parm_done;
592 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
593 parm_done: ;
596 c_override_global_bindings_to_false = false;
597 t1 = build_function_type (valtype, newargs);
598 t1 = qualify_type (t1, t2);
599 /* ... falls through ... */
602 default:
603 return build_type_attribute_variant (t1, attributes);
608 /* Return the type of a conditional expression between pointers to
609 possibly differently qualified versions of compatible types.
611 We assume that comp_target_types has already been done and returned
612 nonzero; if that isn't so, this may crash. */
614 static tree
615 common_pointer_type (tree t1, tree t2)
617 tree attributes;
618 tree pointed_to_1, mv1;
619 tree pointed_to_2, mv2;
620 tree target;
621 unsigned target_quals;
622 addr_space_t as1, as2, as_common;
623 int quals1, quals2;
625 /* Save time if the two types are the same. */
627 if (t1 == t2) return t1;
629 /* If one type is nonsense, use the other. */
630 if (t1 == error_mark_node)
631 return t2;
632 if (t2 == error_mark_node)
633 return t1;
635 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
636 && TREE_CODE (t2) == POINTER_TYPE);
638 /* Merge the attributes. */
639 attributes = targetm.merge_type_attributes (t1, t2);
641 /* Find the composite type of the target types, and combine the
642 qualifiers of the two types' targets. Do not lose qualifiers on
643 array element types by taking the TYPE_MAIN_VARIANT. */
644 mv1 = pointed_to_1 = TREE_TYPE (t1);
645 mv2 = pointed_to_2 = TREE_TYPE (t2);
646 if (TREE_CODE (mv1) != ARRAY_TYPE)
647 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
648 if (TREE_CODE (mv2) != ARRAY_TYPE)
649 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
650 target = composite_type (mv1, mv2);
652 /* For function types do not merge const qualifiers, but drop them
653 if used inconsistently. The middle-end uses these to mark const
654 and noreturn functions. */
655 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
656 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
658 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
659 target_quals = (quals1 & quals2);
660 else
661 target_quals = (quals1 | quals2);
663 /* If the two named address spaces are different, determine the common
664 superset address space. This is guaranteed to exist due to the
665 assumption that comp_target_type returned non-zero. */
666 as1 = TYPE_ADDR_SPACE (pointed_to_1);
667 as2 = TYPE_ADDR_SPACE (pointed_to_2);
668 if (!addr_space_superset (as1, as2, &as_common))
669 gcc_unreachable ();
671 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
673 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
674 return build_type_attribute_variant (t1, attributes);
677 /* Return the common type for two arithmetic types under the usual
678 arithmetic conversions. The default conversions have already been
679 applied, and enumerated types converted to their compatible integer
680 types. The resulting type is unqualified and has no attributes.
682 This is the type for the result of most arithmetic operations
683 if the operands have the given two types. */
685 static tree
686 c_common_type (tree t1, tree t2)
688 enum tree_code code1;
689 enum tree_code code2;
691 /* If one type is nonsense, use the other. */
692 if (t1 == error_mark_node)
693 return t2;
694 if (t2 == error_mark_node)
695 return t1;
697 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
698 t1 = TYPE_MAIN_VARIANT (t1);
700 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
701 t2 = TYPE_MAIN_VARIANT (t2);
703 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
704 t1 = build_type_attribute_variant (t1, NULL_TREE);
706 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
707 t2 = build_type_attribute_variant (t2, NULL_TREE);
709 /* Save time if the two types are the same. */
711 if (t1 == t2) return t1;
713 code1 = TREE_CODE (t1);
714 code2 = TREE_CODE (t2);
716 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
717 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
718 || code1 == INTEGER_TYPE);
719 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
720 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
721 || code2 == INTEGER_TYPE);
723 /* When one operand is a decimal float type, the other operand cannot be
724 a generic float type or a complex type. We also disallow vector types
725 here. */
726 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
727 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
729 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
731 error ("can%'t mix operands of decimal float and vector types");
732 return error_mark_node;
734 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
736 error ("can%'t mix operands of decimal float and complex types");
737 return error_mark_node;
739 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
741 error ("can%'t mix operands of decimal float and other float types");
742 return error_mark_node;
746 /* If one type is a vector type, return that type. (How the usual
747 arithmetic conversions apply to the vector types extension is not
748 precisely specified.) */
749 if (code1 == VECTOR_TYPE)
750 return t1;
752 if (code2 == VECTOR_TYPE)
753 return t2;
755 /* If one type is complex, form the common type of the non-complex
756 components, then make that complex. Use T1 or T2 if it is the
757 required type. */
758 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
760 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
761 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
762 tree subtype = c_common_type (subtype1, subtype2);
764 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
765 return t1;
766 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
767 return t2;
768 else
769 return build_complex_type (subtype);
772 /* If only one is real, use it as the result. */
774 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
775 return t1;
777 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
778 return t2;
780 /* If both are real and either are decimal floating point types, use
781 the decimal floating point type with the greater precision. */
783 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
785 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
786 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
787 return dfloat128_type_node;
788 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
789 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
790 return dfloat64_type_node;
791 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
792 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
793 return dfloat32_type_node;
796 /* Deal with fixed-point types. */
797 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
799 unsigned int unsignedp = 0, satp = 0;
800 enum machine_mode m1, m2;
801 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
803 m1 = TYPE_MODE (t1);
804 m2 = TYPE_MODE (t2);
806 /* If one input type is saturating, the result type is saturating. */
807 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
808 satp = 1;
810 /* If both fixed-point types are unsigned, the result type is unsigned.
811 When mixing fixed-point and integer types, follow the sign of the
812 fixed-point type.
813 Otherwise, the result type is signed. */
814 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
815 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
816 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
817 && TYPE_UNSIGNED (t1))
818 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
819 && TYPE_UNSIGNED (t2)))
820 unsignedp = 1;
822 /* The result type is signed. */
823 if (unsignedp == 0)
825 /* If the input type is unsigned, we need to convert to the
826 signed type. */
827 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
829 enum mode_class mclass = (enum mode_class) 0;
830 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
831 mclass = MODE_FRACT;
832 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
833 mclass = MODE_ACCUM;
834 else
835 gcc_unreachable ();
836 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
838 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
840 enum mode_class mclass = (enum mode_class) 0;
841 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
842 mclass = MODE_FRACT;
843 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
844 mclass = MODE_ACCUM;
845 else
846 gcc_unreachable ();
847 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
851 if (code1 == FIXED_POINT_TYPE)
853 fbit1 = GET_MODE_FBIT (m1);
854 ibit1 = GET_MODE_IBIT (m1);
856 else
858 fbit1 = 0;
859 /* Signed integers need to subtract one sign bit. */
860 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
863 if (code2 == FIXED_POINT_TYPE)
865 fbit2 = GET_MODE_FBIT (m2);
866 ibit2 = GET_MODE_IBIT (m2);
868 else
870 fbit2 = 0;
871 /* Signed integers need to subtract one sign bit. */
872 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
875 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
876 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
877 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
878 satp);
881 /* Both real or both integers; use the one with greater precision. */
883 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
884 return t1;
885 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
886 return t2;
888 /* Same precision. Prefer long longs to longs to ints when the
889 same precision, following the C99 rules on integer type rank
890 (which are equivalent to the C90 rules for C90 types). */
892 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
893 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
894 return long_long_unsigned_type_node;
896 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
897 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
899 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
900 return long_long_unsigned_type_node;
901 else
902 return long_long_integer_type_node;
905 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
906 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
907 return long_unsigned_type_node;
909 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
910 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
912 /* But preserve unsignedness from the other type,
913 since long cannot hold all the values of an unsigned int. */
914 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
915 return long_unsigned_type_node;
916 else
917 return long_integer_type_node;
920 /* Likewise, prefer long double to double even if same size. */
921 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
923 return long_double_type_node;
925 /* Otherwise prefer the unsigned one. */
927 if (TYPE_UNSIGNED (t1))
928 return t1;
929 else
930 return t2;
933 /* Wrapper around c_common_type that is used by c-common.c and other
934 front end optimizations that remove promotions. ENUMERAL_TYPEs
935 are allowed here and are converted to their compatible integer types.
936 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
937 preferably a non-Boolean type as the common type. */
938 tree
939 common_type (tree t1, tree t2)
941 if (TREE_CODE (t1) == ENUMERAL_TYPE)
942 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
943 if (TREE_CODE (t2) == ENUMERAL_TYPE)
944 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
946 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
947 if (TREE_CODE (t1) == BOOLEAN_TYPE
948 && TREE_CODE (t2) == BOOLEAN_TYPE)
949 return boolean_type_node;
951 /* If either type is BOOLEAN_TYPE, then return the other. */
952 if (TREE_CODE (t1) == BOOLEAN_TYPE)
953 return t2;
954 if (TREE_CODE (t2) == BOOLEAN_TYPE)
955 return t1;
957 return c_common_type (t1, t2);
960 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
961 or various other operations. Return 2 if they are compatible
962 but a warning may be needed if you use them together. */
965 comptypes (tree type1, tree type2)
967 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
968 int val;
970 val = comptypes_internal (type1, type2, NULL, NULL);
971 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
973 return val;
976 /* Like comptypes, but if it returns non-zero because enum and int are
977 compatible, it sets *ENUM_AND_INT_P to true. */
979 static int
980 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
982 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
983 int val;
985 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
986 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
988 return val;
991 /* Like comptypes, but if it returns nonzero for different types, it
992 sets *DIFFERENT_TYPES_P to true. */
995 comptypes_check_different_types (tree type1, tree type2,
996 bool *different_types_p)
998 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
999 int val;
1001 val = comptypes_internal (type1, type2, NULL, different_types_p);
1002 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1004 return val;
1007 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1008 or various other operations. Return 2 if they are compatible
1009 but a warning may be needed if you use them together. If
1010 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1011 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1012 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1013 NULL, and the types are compatible but different enough not to be
1014 permitted in C1X typedef redeclarations, then this sets
1015 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1016 false, but may or may not be set if the types are incompatible.
1017 This differs from comptypes, in that we don't free the seen
1018 types. */
1020 static int
1021 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1022 bool *different_types_p)
1024 const_tree t1 = type1;
1025 const_tree t2 = type2;
1026 int attrval, val;
1028 /* Suppress errors caused by previously reported errors. */
1030 if (t1 == t2 || !t1 || !t2
1031 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1032 return 1;
1034 /* Enumerated types are compatible with integer types, but this is
1035 not transitive: two enumerated types in the same translation unit
1036 are compatible with each other only if they are the same type. */
1038 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1040 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1041 if (TREE_CODE (t2) != VOID_TYPE)
1043 if (enum_and_int_p != NULL)
1044 *enum_and_int_p = true;
1045 if (different_types_p != NULL)
1046 *different_types_p = true;
1049 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1051 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1052 if (TREE_CODE (t1) != VOID_TYPE)
1054 if (enum_and_int_p != NULL)
1055 *enum_and_int_p = true;
1056 if (different_types_p != NULL)
1057 *different_types_p = true;
1061 if (t1 == t2)
1062 return 1;
1064 /* Different classes of types can't be compatible. */
1066 if (TREE_CODE (t1) != TREE_CODE (t2))
1067 return 0;
1069 /* Qualifiers must match. C99 6.7.3p9 */
1071 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1072 return 0;
1074 /* Allow for two different type nodes which have essentially the same
1075 definition. Note that we already checked for equality of the type
1076 qualifiers (just above). */
1078 if (TREE_CODE (t1) != ARRAY_TYPE
1079 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1080 return 1;
1082 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1083 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
1084 return 0;
1086 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1087 val = 0;
1089 switch (TREE_CODE (t1))
1091 case POINTER_TYPE:
1092 /* Do not remove mode or aliasing information. */
1093 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1094 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1095 break;
1096 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1097 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1098 enum_and_int_p, different_types_p));
1099 break;
1101 case FUNCTION_TYPE:
1102 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1103 different_types_p);
1104 break;
1106 case ARRAY_TYPE:
1108 tree d1 = TYPE_DOMAIN (t1);
1109 tree d2 = TYPE_DOMAIN (t2);
1110 bool d1_variable, d2_variable;
1111 bool d1_zero, d2_zero;
1112 val = 1;
1114 /* Target types must match incl. qualifiers. */
1115 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1116 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1117 enum_and_int_p,
1118 different_types_p)))
1119 return 0;
1121 if (different_types_p != NULL
1122 && (d1 == 0) != (d2 == 0))
1123 *different_types_p = true;
1124 /* Sizes must match unless one is missing or variable. */
1125 if (d1 == 0 || d2 == 0 || d1 == d2)
1126 break;
1128 d1_zero = !TYPE_MAX_VALUE (d1);
1129 d2_zero = !TYPE_MAX_VALUE (d2);
1131 d1_variable = (!d1_zero
1132 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1133 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1134 d2_variable = (!d2_zero
1135 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1136 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1137 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1138 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1140 if (different_types_p != NULL
1141 && d1_variable != d2_variable)
1142 *different_types_p = true;
1143 if (d1_variable || d2_variable)
1144 break;
1145 if (d1_zero && d2_zero)
1146 break;
1147 if (d1_zero || d2_zero
1148 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1149 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1150 val = 0;
1152 break;
1155 case ENUMERAL_TYPE:
1156 case RECORD_TYPE:
1157 case UNION_TYPE:
1158 if (val != 1 && !same_translation_unit_p (t1, t2))
1160 tree a1 = TYPE_ATTRIBUTES (t1);
1161 tree a2 = TYPE_ATTRIBUTES (t2);
1163 if (! attribute_list_contained (a1, a2)
1164 && ! attribute_list_contained (a2, a1))
1165 break;
1167 if (attrval != 2)
1168 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1169 different_types_p);
1170 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1171 different_types_p);
1173 break;
1175 case VECTOR_TYPE:
1176 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1177 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1178 enum_and_int_p, different_types_p));
1179 break;
1181 default:
1182 break;
1184 return attrval == 2 && val == 1 ? 2 : val;
1187 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1188 their qualifiers, except for named address spaces. If the pointers point to
1189 different named addresses, then we must determine if one address space is a
1190 subset of the other. */
1192 static int
1193 comp_target_types (location_t location, tree ttl, tree ttr)
1195 int val;
1196 tree mvl = TREE_TYPE (ttl);
1197 tree mvr = TREE_TYPE (ttr);
1198 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1199 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1200 addr_space_t as_common;
1201 bool enum_and_int_p;
1203 /* Fail if pointers point to incompatible address spaces. */
1204 if (!addr_space_superset (asl, asr, &as_common))
1205 return 0;
1207 /* Do not lose qualifiers on element types of array types that are
1208 pointer targets by taking their TYPE_MAIN_VARIANT. */
1209 if (TREE_CODE (mvl) != ARRAY_TYPE)
1210 mvl = TYPE_MAIN_VARIANT (mvl);
1211 if (TREE_CODE (mvr) != ARRAY_TYPE)
1212 mvr = TYPE_MAIN_VARIANT (mvr);
1213 enum_and_int_p = false;
1214 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1216 if (val == 2)
1217 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1219 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1220 warning_at (location, OPT_Wc___compat,
1221 "pointer target types incompatible in C++");
1223 return val;
1226 /* Subroutines of `comptypes'. */
1228 /* Determine whether two trees derive from the same translation unit.
1229 If the CONTEXT chain ends in a null, that tree's context is still
1230 being parsed, so if two trees have context chains ending in null,
1231 they're in the same translation unit. */
1233 same_translation_unit_p (const_tree t1, const_tree t2)
1235 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1236 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1238 case tcc_declaration:
1239 t1 = DECL_CONTEXT (t1); break;
1240 case tcc_type:
1241 t1 = TYPE_CONTEXT (t1); break;
1242 case tcc_exceptional:
1243 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1244 default: gcc_unreachable ();
1247 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1248 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1250 case tcc_declaration:
1251 t2 = DECL_CONTEXT (t2); break;
1252 case tcc_type:
1253 t2 = TYPE_CONTEXT (t2); break;
1254 case tcc_exceptional:
1255 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1256 default: gcc_unreachable ();
1259 return t1 == t2;
1262 /* Allocate the seen two types, assuming that they are compatible. */
1264 static struct tagged_tu_seen_cache *
1265 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1267 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1268 tu->next = tagged_tu_seen_base;
1269 tu->t1 = t1;
1270 tu->t2 = t2;
1272 tagged_tu_seen_base = tu;
1274 /* The C standard says that two structures in different translation
1275 units are compatible with each other only if the types of their
1276 fields are compatible (among other things). We assume that they
1277 are compatible until proven otherwise when building the cache.
1278 An example where this can occur is:
1279 struct a
1281 struct a *next;
1283 If we are comparing this against a similar struct in another TU,
1284 and did not assume they were compatible, we end up with an infinite
1285 loop. */
1286 tu->val = 1;
1287 return tu;
1290 /* Free the seen types until we get to TU_TIL. */
1292 static void
1293 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1295 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1296 while (tu != tu_til)
1298 const struct tagged_tu_seen_cache *const tu1
1299 = (const struct tagged_tu_seen_cache *) tu;
1300 tu = tu1->next;
1301 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1303 tagged_tu_seen_base = tu_til;
1306 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1307 compatible. If the two types are not the same (which has been
1308 checked earlier), this can only happen when multiple translation
1309 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1310 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1311 comptypes_internal. */
1313 static int
1314 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1315 bool *enum_and_int_p, bool *different_types_p)
1317 tree s1, s2;
1318 bool needs_warning = false;
1320 /* We have to verify that the tags of the types are the same. This
1321 is harder than it looks because this may be a typedef, so we have
1322 to go look at the original type. It may even be a typedef of a
1323 typedef...
1324 In the case of compiler-created builtin structs the TYPE_DECL
1325 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1326 while (TYPE_NAME (t1)
1327 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1328 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1329 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1331 while (TYPE_NAME (t2)
1332 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1333 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1334 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1336 /* C90 didn't have the requirement that the two tags be the same. */
1337 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1338 return 0;
1340 /* C90 didn't say what happened if one or both of the types were
1341 incomplete; we choose to follow C99 rules here, which is that they
1342 are compatible. */
1343 if (TYPE_SIZE (t1) == NULL
1344 || TYPE_SIZE (t2) == NULL)
1345 return 1;
1348 const struct tagged_tu_seen_cache * tts_i;
1349 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1350 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1351 return tts_i->val;
1354 switch (TREE_CODE (t1))
1356 case ENUMERAL_TYPE:
1358 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1359 /* Speed up the case where the type values are in the same order. */
1360 tree tv1 = TYPE_VALUES (t1);
1361 tree tv2 = TYPE_VALUES (t2);
1363 if (tv1 == tv2)
1365 return 1;
1368 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1370 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1371 break;
1372 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1374 tu->val = 0;
1375 return 0;
1379 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1381 return 1;
1383 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1385 tu->val = 0;
1386 return 0;
1389 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1391 tu->val = 0;
1392 return 0;
1395 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1397 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1398 if (s2 == NULL
1399 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1401 tu->val = 0;
1402 return 0;
1405 return 1;
1408 case UNION_TYPE:
1410 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1411 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1413 tu->val = 0;
1414 return 0;
1417 /* Speed up the common case where the fields are in the same order. */
1418 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1419 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1421 int result;
1423 if (DECL_NAME (s1) != DECL_NAME (s2))
1424 break;
1425 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1426 enum_and_int_p, different_types_p);
1428 if (result != 1 && !DECL_NAME (s1))
1429 break;
1430 if (result == 0)
1432 tu->val = 0;
1433 return 0;
1435 if (result == 2)
1436 needs_warning = true;
1438 if (TREE_CODE (s1) == FIELD_DECL
1439 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1440 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1442 tu->val = 0;
1443 return 0;
1446 if (!s1 && !s2)
1448 tu->val = needs_warning ? 2 : 1;
1449 return tu->val;
1452 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1454 bool ok = false;
1456 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1457 if (DECL_NAME (s1) == DECL_NAME (s2))
1459 int result;
1461 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1462 enum_and_int_p,
1463 different_types_p);
1465 if (result != 1 && !DECL_NAME (s1))
1466 continue;
1467 if (result == 0)
1469 tu->val = 0;
1470 return 0;
1472 if (result == 2)
1473 needs_warning = true;
1475 if (TREE_CODE (s1) == FIELD_DECL
1476 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1477 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1478 break;
1480 ok = true;
1481 break;
1483 if (!ok)
1485 tu->val = 0;
1486 return 0;
1489 tu->val = needs_warning ? 2 : 10;
1490 return tu->val;
1493 case RECORD_TYPE:
1495 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1497 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1498 s1 && s2;
1499 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1501 int result;
1502 if (TREE_CODE (s1) != TREE_CODE (s2)
1503 || DECL_NAME (s1) != DECL_NAME (s2))
1504 break;
1505 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1506 enum_and_int_p, different_types_p);
1507 if (result == 0)
1508 break;
1509 if (result == 2)
1510 needs_warning = true;
1512 if (TREE_CODE (s1) == FIELD_DECL
1513 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1514 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1515 break;
1517 if (s1 && s2)
1518 tu->val = 0;
1519 else
1520 tu->val = needs_warning ? 2 : 1;
1521 return tu->val;
1524 default:
1525 gcc_unreachable ();
1529 /* Return 1 if two function types F1 and F2 are compatible.
1530 If either type specifies no argument types,
1531 the other must specify a fixed number of self-promoting arg types.
1532 Otherwise, if one type specifies only the number of arguments,
1533 the other must specify that number of self-promoting arg types.
1534 Otherwise, the argument types must match.
1535 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1537 static int
1538 function_types_compatible_p (const_tree f1, const_tree f2,
1539 bool *enum_and_int_p, bool *different_types_p)
1541 tree args1, args2;
1542 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1543 int val = 1;
1544 int val1;
1545 tree ret1, ret2;
1547 ret1 = TREE_TYPE (f1);
1548 ret2 = TREE_TYPE (f2);
1550 /* 'volatile' qualifiers on a function's return type used to mean
1551 the function is noreturn. */
1552 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1553 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1554 if (TYPE_VOLATILE (ret1))
1555 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1556 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1557 if (TYPE_VOLATILE (ret2))
1558 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1559 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1560 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1561 if (val == 0)
1562 return 0;
1564 args1 = TYPE_ARG_TYPES (f1);
1565 args2 = TYPE_ARG_TYPES (f2);
1567 if (different_types_p != NULL
1568 && (args1 == 0) != (args2 == 0))
1569 *different_types_p = true;
1571 /* An unspecified parmlist matches any specified parmlist
1572 whose argument types don't need default promotions. */
1574 if (args1 == 0)
1576 if (!self_promoting_args_p (args2))
1577 return 0;
1578 /* If one of these types comes from a non-prototype fn definition,
1579 compare that with the other type's arglist.
1580 If they don't match, ask for a warning (but no error). */
1581 if (TYPE_ACTUAL_ARG_TYPES (f1)
1582 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1583 enum_and_int_p, different_types_p))
1584 val = 2;
1585 return val;
1587 if (args2 == 0)
1589 if (!self_promoting_args_p (args1))
1590 return 0;
1591 if (TYPE_ACTUAL_ARG_TYPES (f2)
1592 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1593 enum_and_int_p, different_types_p))
1594 val = 2;
1595 return val;
1598 /* Both types have argument lists: compare them and propagate results. */
1599 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1600 different_types_p);
1601 return val1 != 1 ? val1 : val;
1604 /* Check two lists of types for compatibility, returning 0 for
1605 incompatible, 1 for compatible, or 2 for compatible with
1606 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1607 comptypes_internal. */
1609 static int
1610 type_lists_compatible_p (const_tree args1, const_tree args2,
1611 bool *enum_and_int_p, bool *different_types_p)
1613 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1614 int val = 1;
1615 int newval = 0;
1617 while (1)
1619 tree a1, mv1, a2, mv2;
1620 if (args1 == 0 && args2 == 0)
1621 return val;
1622 /* If one list is shorter than the other,
1623 they fail to match. */
1624 if (args1 == 0 || args2 == 0)
1625 return 0;
1626 mv1 = a1 = TREE_VALUE (args1);
1627 mv2 = a2 = TREE_VALUE (args2);
1628 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1629 mv1 = TYPE_MAIN_VARIANT (mv1);
1630 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1631 mv2 = TYPE_MAIN_VARIANT (mv2);
1632 /* A null pointer instead of a type
1633 means there is supposed to be an argument
1634 but nothing is specified about what type it has.
1635 So match anything that self-promotes. */
1636 if (different_types_p != NULL
1637 && (a1 == 0) != (a2 == 0))
1638 *different_types_p = true;
1639 if (a1 == 0)
1641 if (c_type_promotes_to (a2) != a2)
1642 return 0;
1644 else if (a2 == 0)
1646 if (c_type_promotes_to (a1) != a1)
1647 return 0;
1649 /* If one of the lists has an error marker, ignore this arg. */
1650 else if (TREE_CODE (a1) == ERROR_MARK
1651 || TREE_CODE (a2) == ERROR_MARK)
1653 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1654 different_types_p)))
1656 if (different_types_p != NULL)
1657 *different_types_p = true;
1658 /* Allow wait (union {union wait *u; int *i} *)
1659 and wait (union wait *) to be compatible. */
1660 if (TREE_CODE (a1) == UNION_TYPE
1661 && (TYPE_NAME (a1) == 0
1662 || TYPE_TRANSPARENT_AGGR (a1))
1663 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1664 && tree_int_cst_equal (TYPE_SIZE (a1),
1665 TYPE_SIZE (a2)))
1667 tree memb;
1668 for (memb = TYPE_FIELDS (a1);
1669 memb; memb = DECL_CHAIN (memb))
1671 tree mv3 = TREE_TYPE (memb);
1672 if (mv3 && mv3 != error_mark_node
1673 && TREE_CODE (mv3) != ARRAY_TYPE)
1674 mv3 = TYPE_MAIN_VARIANT (mv3);
1675 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1676 different_types_p))
1677 break;
1679 if (memb == 0)
1680 return 0;
1682 else if (TREE_CODE (a2) == UNION_TYPE
1683 && (TYPE_NAME (a2) == 0
1684 || TYPE_TRANSPARENT_AGGR (a2))
1685 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1686 && tree_int_cst_equal (TYPE_SIZE (a2),
1687 TYPE_SIZE (a1)))
1689 tree memb;
1690 for (memb = TYPE_FIELDS (a2);
1691 memb; memb = DECL_CHAIN (memb))
1693 tree mv3 = TREE_TYPE (memb);
1694 if (mv3 && mv3 != error_mark_node
1695 && TREE_CODE (mv3) != ARRAY_TYPE)
1696 mv3 = TYPE_MAIN_VARIANT (mv3);
1697 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1698 different_types_p))
1699 break;
1701 if (memb == 0)
1702 return 0;
1704 else
1705 return 0;
1708 /* comptypes said ok, but record if it said to warn. */
1709 if (newval > val)
1710 val = newval;
1712 args1 = TREE_CHAIN (args1);
1713 args2 = TREE_CHAIN (args2);
1717 /* Compute the size to increment a pointer by. */
1719 static tree
1720 c_size_in_bytes (const_tree type)
1722 enum tree_code code = TREE_CODE (type);
1724 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1725 return size_one_node;
1727 if (!COMPLETE_OR_VOID_TYPE_P (type))
1729 error ("arithmetic on pointer to an incomplete type");
1730 return size_one_node;
1733 /* Convert in case a char is more than one unit. */
1734 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1735 size_int (TYPE_PRECISION (char_type_node)
1736 / BITS_PER_UNIT));
1739 /* Return either DECL or its known constant value (if it has one). */
1741 tree
1742 decl_constant_value (tree decl)
1744 if (/* Don't change a variable array bound or initial value to a constant
1745 in a place where a variable is invalid. Note that DECL_INITIAL
1746 isn't valid for a PARM_DECL. */
1747 current_function_decl != 0
1748 && TREE_CODE (decl) != PARM_DECL
1749 && !TREE_THIS_VOLATILE (decl)
1750 && TREE_READONLY (decl)
1751 && DECL_INITIAL (decl) != 0
1752 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1753 /* This is invalid if initial value is not constant.
1754 If it has either a function call, a memory reference,
1755 or a variable, then re-evaluating it could give different results. */
1756 && TREE_CONSTANT (DECL_INITIAL (decl))
1757 /* Check for cases where this is sub-optimal, even though valid. */
1758 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1759 return DECL_INITIAL (decl);
1760 return decl;
1763 /* Convert the array expression EXP to a pointer. */
1764 static tree
1765 array_to_pointer_conversion (location_t loc, tree exp)
1767 tree orig_exp = exp;
1768 tree type = TREE_TYPE (exp);
1769 tree adr;
1770 tree restype = TREE_TYPE (type);
1771 tree ptrtype;
1773 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1775 STRIP_TYPE_NOPS (exp);
1777 if (TREE_NO_WARNING (orig_exp))
1778 TREE_NO_WARNING (exp) = 1;
1780 ptrtype = build_pointer_type (restype);
1782 if (TREE_CODE (exp) == INDIRECT_REF)
1783 return convert (ptrtype, TREE_OPERAND (exp, 0));
1785 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1786 return convert (ptrtype, adr);
1789 /* Convert the function expression EXP to a pointer. */
1790 static tree
1791 function_to_pointer_conversion (location_t loc, tree exp)
1793 tree orig_exp = exp;
1795 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1797 STRIP_TYPE_NOPS (exp);
1799 if (TREE_NO_WARNING (orig_exp))
1800 TREE_NO_WARNING (exp) = 1;
1802 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1805 /* Mark EXP as read, not just set, for set but not used -Wunused
1806 warning purposes. */
1808 void
1809 mark_exp_read (tree exp)
1811 switch (TREE_CODE (exp))
1813 case VAR_DECL:
1814 case PARM_DECL:
1815 DECL_READ_P (exp) = 1;
1816 break;
1817 case ARRAY_REF:
1818 case COMPONENT_REF:
1819 case MODIFY_EXPR:
1820 case REALPART_EXPR:
1821 case IMAGPART_EXPR:
1822 CASE_CONVERT:
1823 case ADDR_EXPR:
1824 mark_exp_read (TREE_OPERAND (exp, 0));
1825 break;
1826 case COMPOUND_EXPR:
1827 case C_MAYBE_CONST_EXPR:
1828 mark_exp_read (TREE_OPERAND (exp, 1));
1829 break;
1830 default:
1831 break;
1835 /* Perform the default conversion of arrays and functions to pointers.
1836 Return the result of converting EXP. For any other expression, just
1837 return EXP.
1839 LOC is the location of the expression. */
1841 struct c_expr
1842 default_function_array_conversion (location_t loc, struct c_expr exp)
1844 tree orig_exp = exp.value;
1845 tree type = TREE_TYPE (exp.value);
1846 enum tree_code code = TREE_CODE (type);
1848 switch (code)
1850 case ARRAY_TYPE:
1852 bool not_lvalue = false;
1853 bool lvalue_array_p;
1855 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1856 || CONVERT_EXPR_P (exp.value))
1857 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1859 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1860 not_lvalue = true;
1861 exp.value = TREE_OPERAND (exp.value, 0);
1864 if (TREE_NO_WARNING (orig_exp))
1865 TREE_NO_WARNING (exp.value) = 1;
1867 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1868 if (!flag_isoc99 && !lvalue_array_p)
1870 /* Before C99, non-lvalue arrays do not decay to pointers.
1871 Normally, using such an array would be invalid; but it can
1872 be used correctly inside sizeof or as a statement expression.
1873 Thus, do not give an error here; an error will result later. */
1874 return exp;
1877 exp.value = array_to_pointer_conversion (loc, exp.value);
1879 break;
1880 case FUNCTION_TYPE:
1881 exp.value = function_to_pointer_conversion (loc, exp.value);
1882 break;
1883 default:
1884 break;
1887 return exp;
1890 struct c_expr
1891 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1893 mark_exp_read (exp.value);
1894 return default_function_array_conversion (loc, exp);
1897 /* EXP is an expression of integer type. Apply the integer promotions
1898 to it and return the promoted value. */
1900 tree
1901 perform_integral_promotions (tree exp)
1903 tree type = TREE_TYPE (exp);
1904 enum tree_code code = TREE_CODE (type);
1906 gcc_assert (INTEGRAL_TYPE_P (type));
1908 /* Normally convert enums to int,
1909 but convert wide enums to something wider. */
1910 if (code == ENUMERAL_TYPE)
1912 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1913 TYPE_PRECISION (integer_type_node)),
1914 ((TYPE_PRECISION (type)
1915 >= TYPE_PRECISION (integer_type_node))
1916 && TYPE_UNSIGNED (type)));
1918 return convert (type, exp);
1921 /* ??? This should no longer be needed now bit-fields have their
1922 proper types. */
1923 if (TREE_CODE (exp) == COMPONENT_REF
1924 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1925 /* If it's thinner than an int, promote it like a
1926 c_promoting_integer_type_p, otherwise leave it alone. */
1927 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1928 TYPE_PRECISION (integer_type_node)))
1929 return convert (integer_type_node, exp);
1931 if (c_promoting_integer_type_p (type))
1933 /* Preserve unsignedness if not really getting any wider. */
1934 if (TYPE_UNSIGNED (type)
1935 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1936 return convert (unsigned_type_node, exp);
1938 return convert (integer_type_node, exp);
1941 return exp;
1945 /* Perform default promotions for C data used in expressions.
1946 Enumeral types or short or char are converted to int.
1947 In addition, manifest constants symbols are replaced by their values. */
1949 tree
1950 default_conversion (tree exp)
1952 tree orig_exp;
1953 tree type = TREE_TYPE (exp);
1954 enum tree_code code = TREE_CODE (type);
1955 tree promoted_type;
1957 mark_exp_read (exp);
1959 /* Functions and arrays have been converted during parsing. */
1960 gcc_assert (code != FUNCTION_TYPE);
1961 if (code == ARRAY_TYPE)
1962 return exp;
1964 /* Constants can be used directly unless they're not loadable. */
1965 if (TREE_CODE (exp) == CONST_DECL)
1966 exp = DECL_INITIAL (exp);
1968 /* Strip no-op conversions. */
1969 orig_exp = exp;
1970 STRIP_TYPE_NOPS (exp);
1972 if (TREE_NO_WARNING (orig_exp))
1973 TREE_NO_WARNING (exp) = 1;
1975 if (code == VOID_TYPE)
1977 error ("void value not ignored as it ought to be");
1978 return error_mark_node;
1981 exp = require_complete_type (exp);
1982 if (exp == error_mark_node)
1983 return error_mark_node;
1985 promoted_type = targetm.promoted_type (type);
1986 if (promoted_type)
1987 return convert (promoted_type, exp);
1989 if (INTEGRAL_TYPE_P (type))
1990 return perform_integral_promotions (exp);
1992 return exp;
1995 /* Look up COMPONENT in a structure or union TYPE.
1997 If the component name is not found, returns NULL_TREE. Otherwise,
1998 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1999 stepping down the chain to the component, which is in the last
2000 TREE_VALUE of the list. Normally the list is of length one, but if
2001 the component is embedded within (nested) anonymous structures or
2002 unions, the list steps down the chain to the component. */
2004 static tree
2005 lookup_field (tree type, tree component)
2007 tree field;
2009 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2010 to the field elements. Use a binary search on this array to quickly
2011 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2012 will always be set for structures which have many elements. */
2014 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2016 int bot, top, half;
2017 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2019 field = TYPE_FIELDS (type);
2020 bot = 0;
2021 top = TYPE_LANG_SPECIFIC (type)->s->len;
2022 while (top - bot > 1)
2024 half = (top - bot + 1) >> 1;
2025 field = field_array[bot+half];
2027 if (DECL_NAME (field) == NULL_TREE)
2029 /* Step through all anon unions in linear fashion. */
2030 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2032 field = field_array[bot++];
2033 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2034 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2036 tree anon = lookup_field (TREE_TYPE (field), component);
2038 if (anon)
2039 return tree_cons (NULL_TREE, field, anon);
2041 /* The Plan 9 compiler permits referring
2042 directly to an anonymous struct/union field
2043 using a typedef name. */
2044 if (flag_plan9_extensions
2045 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2046 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2047 == TYPE_DECL)
2048 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2049 == component))
2050 break;
2054 /* Entire record is only anon unions. */
2055 if (bot > top)
2056 return NULL_TREE;
2058 /* Restart the binary search, with new lower bound. */
2059 continue;
2062 if (DECL_NAME (field) == component)
2063 break;
2064 if (DECL_NAME (field) < component)
2065 bot += half;
2066 else
2067 top = bot + half;
2070 if (DECL_NAME (field_array[bot]) == component)
2071 field = field_array[bot];
2072 else if (DECL_NAME (field) != component)
2073 return NULL_TREE;
2075 else
2077 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2079 if (DECL_NAME (field) == NULL_TREE
2080 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2081 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2083 tree anon = lookup_field (TREE_TYPE (field), component);
2085 if (anon)
2086 return tree_cons (NULL_TREE, field, anon);
2088 /* The Plan 9 compiler permits referring directly to an
2089 anonymous struct/union field using a typedef
2090 name. */
2091 if (flag_plan9_extensions
2092 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2093 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2094 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2095 == component))
2096 break;
2099 if (DECL_NAME (field) == component)
2100 break;
2103 if (field == NULL_TREE)
2104 return NULL_TREE;
2107 return tree_cons (NULL_TREE, field, NULL_TREE);
2110 /* Make an expression to refer to the COMPONENT field of structure or
2111 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2112 location of the COMPONENT_REF. */
2114 tree
2115 build_component_ref (location_t loc, tree datum, tree component)
2117 tree type = TREE_TYPE (datum);
2118 enum tree_code code = TREE_CODE (type);
2119 tree field = NULL;
2120 tree ref;
2121 bool datum_lvalue = lvalue_p (datum);
2123 if (!objc_is_public (datum, component))
2124 return error_mark_node;
2126 /* Detect Objective-C property syntax object.property. */
2127 if (c_dialect_objc ()
2128 && (ref = objc_maybe_build_component_ref (datum, component)))
2129 return ref;
2131 /* See if there is a field or component with name COMPONENT. */
2133 if (code == RECORD_TYPE || code == UNION_TYPE)
2135 if (!COMPLETE_TYPE_P (type))
2137 c_incomplete_type_error (NULL_TREE, type);
2138 return error_mark_node;
2141 field = lookup_field (type, component);
2143 if (!field)
2145 error_at (loc, "%qT has no member named %qE", type, component);
2146 return error_mark_node;
2149 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2150 This might be better solved in future the way the C++ front
2151 end does it - by giving the anonymous entities each a
2152 separate name and type, and then have build_component_ref
2153 recursively call itself. We can't do that here. */
2156 tree subdatum = TREE_VALUE (field);
2157 int quals;
2158 tree subtype;
2159 bool use_datum_quals;
2161 if (TREE_TYPE (subdatum) == error_mark_node)
2162 return error_mark_node;
2164 /* If this is an rvalue, it does not have qualifiers in C
2165 standard terms and we must avoid propagating such
2166 qualifiers down to a non-lvalue array that is then
2167 converted to a pointer. */
2168 use_datum_quals = (datum_lvalue
2169 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2171 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2172 if (use_datum_quals)
2173 quals |= TYPE_QUALS (TREE_TYPE (datum));
2174 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2176 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2177 NULL_TREE);
2178 SET_EXPR_LOCATION (ref, loc);
2179 if (TREE_READONLY (subdatum)
2180 || (use_datum_quals && TREE_READONLY (datum)))
2181 TREE_READONLY (ref) = 1;
2182 if (TREE_THIS_VOLATILE (subdatum)
2183 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2184 TREE_THIS_VOLATILE (ref) = 1;
2186 if (TREE_DEPRECATED (subdatum))
2187 warn_deprecated_use (subdatum, NULL_TREE);
2189 datum = ref;
2191 field = TREE_CHAIN (field);
2193 while (field);
2195 return ref;
2197 else if (code != ERROR_MARK)
2198 error_at (loc,
2199 "request for member %qE in something not a structure or union",
2200 component);
2202 return error_mark_node;
2205 /* Given an expression PTR for a pointer, return an expression
2206 for the value pointed to.
2207 ERRORSTRING is the name of the operator to appear in error messages.
2209 LOC is the location to use for the generated tree. */
2211 tree
2212 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2214 tree pointer = default_conversion (ptr);
2215 tree type = TREE_TYPE (pointer);
2216 tree ref;
2218 if (TREE_CODE (type) == POINTER_TYPE)
2220 if (CONVERT_EXPR_P (pointer)
2221 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2223 /* If a warning is issued, mark it to avoid duplicates from
2224 the backend. This only needs to be done at
2225 warn_strict_aliasing > 2. */
2226 if (warn_strict_aliasing > 2)
2227 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2228 type, TREE_OPERAND (pointer, 0)))
2229 TREE_NO_WARNING (pointer) = 1;
2232 if (TREE_CODE (pointer) == ADDR_EXPR
2233 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2234 == TREE_TYPE (type)))
2236 ref = TREE_OPERAND (pointer, 0);
2237 protected_set_expr_location (ref, loc);
2238 return ref;
2240 else
2242 tree t = TREE_TYPE (type);
2244 ref = build1 (INDIRECT_REF, t, pointer);
2246 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2248 error_at (loc, "dereferencing pointer to incomplete type");
2249 return error_mark_node;
2251 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2252 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2254 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2255 so that we get the proper error message if the result is used
2256 to assign to. Also, &* is supposed to be a no-op.
2257 And ANSI C seems to specify that the type of the result
2258 should be the const type. */
2259 /* A de-reference of a pointer to const is not a const. It is valid
2260 to change it via some other pointer. */
2261 TREE_READONLY (ref) = TYPE_READONLY (t);
2262 TREE_SIDE_EFFECTS (ref)
2263 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2264 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2265 protected_set_expr_location (ref, loc);
2266 return ref;
2269 else if (TREE_CODE (pointer) != ERROR_MARK)
2270 switch (errstring)
2272 case RO_ARRAY_INDEXING:
2273 error_at (loc,
2274 "invalid type argument of array indexing (have %qT)",
2275 type);
2276 break;
2277 case RO_UNARY_STAR:
2278 error_at (loc,
2279 "invalid type argument of unary %<*%> (have %qT)",
2280 type);
2281 break;
2282 case RO_ARROW:
2283 error_at (loc,
2284 "invalid type argument of %<->%> (have %qT)",
2285 type);
2286 break;
2287 default:
2288 gcc_unreachable ();
2290 return error_mark_node;
2293 /* This handles expressions of the form "a[i]", which denotes
2294 an array reference.
2296 This is logically equivalent in C to *(a+i), but we may do it differently.
2297 If A is a variable or a member, we generate a primitive ARRAY_REF.
2298 This avoids forcing the array out of registers, and can work on
2299 arrays that are not lvalues (for example, members of structures returned
2300 by functions).
2302 For vector types, allow vector[i] but not i[vector], and create
2303 *(((type*)&vectortype) + i) for the expression.
2305 LOC is the location to use for the returned expression. */
2307 tree
2308 build_array_ref (location_t loc, tree array, tree index)
2310 tree ret;
2311 bool swapped = false;
2312 if (TREE_TYPE (array) == error_mark_node
2313 || TREE_TYPE (index) == error_mark_node)
2314 return error_mark_node;
2316 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2317 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2318 /* Allow vector[index] but not index[vector]. */
2319 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2321 tree temp;
2322 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2323 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2325 error_at (loc,
2326 "subscripted value is neither array nor pointer nor vector");
2328 return error_mark_node;
2330 temp = array;
2331 array = index;
2332 index = temp;
2333 swapped = true;
2336 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2338 error_at (loc, "array subscript is not an integer");
2339 return error_mark_node;
2342 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2344 error_at (loc, "subscripted value is pointer to function");
2345 return error_mark_node;
2348 /* ??? Existing practice has been to warn only when the char
2349 index is syntactically the index, not for char[array]. */
2350 if (!swapped)
2351 warn_array_subscript_with_type_char (index);
2353 /* Apply default promotions *after* noticing character types. */
2354 index = default_conversion (index);
2356 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2358 /* For vector[index], convert the vector to a
2359 pointer of the underlying type. */
2360 if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2362 tree type = TREE_TYPE (array);
2363 tree type1;
2365 if (TREE_CODE (index) == INTEGER_CST)
2366 if (!host_integerp (index, 1)
2367 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2368 >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2369 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2371 c_common_mark_addressable_vec (array);
2372 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2373 type = build_pointer_type (type);
2374 type1 = build_pointer_type (TREE_TYPE (array));
2375 array = build1 (ADDR_EXPR, type1, array);
2376 array = convert (type, array);
2379 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2381 tree rval, type;
2383 /* An array that is indexed by a non-constant
2384 cannot be stored in a register; we must be able to do
2385 address arithmetic on its address.
2386 Likewise an array of elements of variable size. */
2387 if (TREE_CODE (index) != INTEGER_CST
2388 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2389 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2391 if (!c_mark_addressable (array))
2392 return error_mark_node;
2394 /* An array that is indexed by a constant value which is not within
2395 the array bounds cannot be stored in a register either; because we
2396 would get a crash in store_bit_field/extract_bit_field when trying
2397 to access a non-existent part of the register. */
2398 if (TREE_CODE (index) == INTEGER_CST
2399 && TYPE_DOMAIN (TREE_TYPE (array))
2400 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2402 if (!c_mark_addressable (array))
2403 return error_mark_node;
2406 if (pedantic)
2408 tree foo = array;
2409 while (TREE_CODE (foo) == COMPONENT_REF)
2410 foo = TREE_OPERAND (foo, 0);
2411 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2412 pedwarn (loc, OPT_pedantic,
2413 "ISO C forbids subscripting %<register%> array");
2414 else if (!flag_isoc99 && !lvalue_p (foo))
2415 pedwarn (loc, OPT_pedantic,
2416 "ISO C90 forbids subscripting non-lvalue array");
2419 type = TREE_TYPE (TREE_TYPE (array));
2420 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2421 /* Array ref is const/volatile if the array elements are
2422 or if the array is. */
2423 TREE_READONLY (rval)
2424 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2425 | TREE_READONLY (array));
2426 TREE_SIDE_EFFECTS (rval)
2427 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2428 | TREE_SIDE_EFFECTS (array));
2429 TREE_THIS_VOLATILE (rval)
2430 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2431 /* This was added by rms on 16 Nov 91.
2432 It fixes vol struct foo *a; a->elts[1]
2433 in an inline function.
2434 Hope it doesn't break something else. */
2435 | TREE_THIS_VOLATILE (array));
2436 ret = require_complete_type (rval);
2437 protected_set_expr_location (ret, loc);
2438 return ret;
2440 else
2442 tree ar = default_conversion (array);
2444 if (ar == error_mark_node)
2445 return ar;
2447 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2448 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2450 return build_indirect_ref
2451 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2452 RO_ARRAY_INDEXING);
2456 /* Build an external reference to identifier ID. FUN indicates
2457 whether this will be used for a function call. LOC is the source
2458 location of the identifier. This sets *TYPE to the type of the
2459 identifier, which is not the same as the type of the returned value
2460 for CONST_DECLs defined as enum constants. If the type of the
2461 identifier is not available, *TYPE is set to NULL. */
2462 tree
2463 build_external_ref (location_t loc, tree id, int fun, tree *type)
2465 tree ref;
2466 tree decl = lookup_name (id);
2468 /* In Objective-C, an instance variable (ivar) may be preferred to
2469 whatever lookup_name() found. */
2470 decl = objc_lookup_ivar (decl, id);
2472 *type = NULL;
2473 if (decl && decl != error_mark_node)
2475 ref = decl;
2476 *type = TREE_TYPE (ref);
2478 else if (fun)
2479 /* Implicit function declaration. */
2480 ref = implicitly_declare (loc, id);
2481 else if (decl == error_mark_node)
2482 /* Don't complain about something that's already been
2483 complained about. */
2484 return error_mark_node;
2485 else
2487 undeclared_variable (loc, id);
2488 return error_mark_node;
2491 if (TREE_TYPE (ref) == error_mark_node)
2492 return error_mark_node;
2494 if (TREE_DEPRECATED (ref))
2495 warn_deprecated_use (ref, NULL_TREE);
2497 /* Recursive call does not count as usage. */
2498 if (ref != current_function_decl)
2500 TREE_USED (ref) = 1;
2503 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2505 if (!in_sizeof && !in_typeof)
2506 C_DECL_USED (ref) = 1;
2507 else if (DECL_INITIAL (ref) == 0
2508 && DECL_EXTERNAL (ref)
2509 && !TREE_PUBLIC (ref))
2510 record_maybe_used_decl (ref);
2513 if (TREE_CODE (ref) == CONST_DECL)
2515 used_types_insert (TREE_TYPE (ref));
2517 if (warn_cxx_compat
2518 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2519 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2521 warning_at (loc, OPT_Wc___compat,
2522 ("enum constant defined in struct or union "
2523 "is not visible in C++"));
2524 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2527 ref = DECL_INITIAL (ref);
2528 TREE_CONSTANT (ref) = 1;
2530 else if (current_function_decl != 0
2531 && !DECL_FILE_SCOPE_P (current_function_decl)
2532 && (TREE_CODE (ref) == VAR_DECL
2533 || TREE_CODE (ref) == PARM_DECL
2534 || TREE_CODE (ref) == FUNCTION_DECL))
2536 tree context = decl_function_context (ref);
2538 if (context != 0 && context != current_function_decl)
2539 DECL_NONLOCAL (ref) = 1;
2541 /* C99 6.7.4p3: An inline definition of a function with external
2542 linkage ... shall not contain a reference to an identifier with
2543 internal linkage. */
2544 else if (current_function_decl != 0
2545 && DECL_DECLARED_INLINE_P (current_function_decl)
2546 && DECL_EXTERNAL (current_function_decl)
2547 && VAR_OR_FUNCTION_DECL_P (ref)
2548 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2549 && ! TREE_PUBLIC (ref)
2550 && DECL_CONTEXT (ref) != current_function_decl)
2551 record_inline_static (loc, current_function_decl, ref,
2552 csi_internal);
2554 return ref;
2557 /* Record details of decls possibly used inside sizeof or typeof. */
2558 struct maybe_used_decl
2560 /* The decl. */
2561 tree decl;
2562 /* The level seen at (in_sizeof + in_typeof). */
2563 int level;
2564 /* The next one at this level or above, or NULL. */
2565 struct maybe_used_decl *next;
2568 static struct maybe_used_decl *maybe_used_decls;
2570 /* Record that DECL, an undefined static function reference seen
2571 inside sizeof or typeof, might be used if the operand of sizeof is
2572 a VLA type or the operand of typeof is a variably modified
2573 type. */
2575 static void
2576 record_maybe_used_decl (tree decl)
2578 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2579 t->decl = decl;
2580 t->level = in_sizeof + in_typeof;
2581 t->next = maybe_used_decls;
2582 maybe_used_decls = t;
2585 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2586 USED is false, just discard them. If it is true, mark them used
2587 (if no longer inside sizeof or typeof) or move them to the next
2588 level up (if still inside sizeof or typeof). */
2590 void
2591 pop_maybe_used (bool used)
2593 struct maybe_used_decl *p = maybe_used_decls;
2594 int cur_level = in_sizeof + in_typeof;
2595 while (p && p->level > cur_level)
2597 if (used)
2599 if (cur_level == 0)
2600 C_DECL_USED (p->decl) = 1;
2601 else
2602 p->level = cur_level;
2604 p = p->next;
2606 if (!used || cur_level == 0)
2607 maybe_used_decls = p;
2610 /* Return the result of sizeof applied to EXPR. */
2612 struct c_expr
2613 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2615 struct c_expr ret;
2616 if (expr.value == error_mark_node)
2618 ret.value = error_mark_node;
2619 ret.original_code = ERROR_MARK;
2620 ret.original_type = NULL;
2621 pop_maybe_used (false);
2623 else
2625 bool expr_const_operands = true;
2626 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2627 &expr_const_operands);
2628 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2629 ret.original_code = ERROR_MARK;
2630 ret.original_type = NULL;
2631 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2633 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2634 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2635 folded_expr, ret.value);
2636 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2637 SET_EXPR_LOCATION (ret.value, loc);
2639 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2641 return ret;
2644 /* Return the result of sizeof applied to T, a structure for the type
2645 name passed to sizeof (rather than the type itself). LOC is the
2646 location of the original expression. */
2648 struct c_expr
2649 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2651 tree type;
2652 struct c_expr ret;
2653 tree type_expr = NULL_TREE;
2654 bool type_expr_const = true;
2655 type = groktypename (t, &type_expr, &type_expr_const);
2656 ret.value = c_sizeof (loc, type);
2657 ret.original_code = ERROR_MARK;
2658 ret.original_type = NULL;
2659 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2660 && c_vla_type_p (type))
2662 /* If the type is a [*] array, it is a VLA but is represented as
2663 having a size of zero. In such a case we must ensure that
2664 the result of sizeof does not get folded to a constant by
2665 c_fully_fold, because if the size is evaluated the result is
2666 not constant and so constraints on zero or negative size
2667 arrays must not be applied when this sizeof call is inside
2668 another array declarator. */
2669 if (!type_expr)
2670 type_expr = integer_zero_node;
2671 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2672 type_expr, ret.value);
2673 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2675 pop_maybe_used (type != error_mark_node
2676 ? C_TYPE_VARIABLE_SIZE (type) : false);
2677 return ret;
2680 /* Build a function call to function FUNCTION with parameters PARAMS.
2681 The function call is at LOC.
2682 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2683 TREE_VALUE of each node is a parameter-expression.
2684 FUNCTION's data type may be a function type or a pointer-to-function. */
2686 tree
2687 build_function_call (location_t loc, tree function, tree params)
2689 VEC(tree,gc) *vec;
2690 tree ret;
2692 vec = VEC_alloc (tree, gc, list_length (params));
2693 for (; params; params = TREE_CHAIN (params))
2694 VEC_quick_push (tree, vec, TREE_VALUE (params));
2695 ret = build_function_call_vec (loc, function, vec, NULL);
2696 VEC_free (tree, gc, vec);
2697 return ret;
2700 /* Build a function call to function FUNCTION with parameters PARAMS.
2701 ORIGTYPES, if not NULL, is a vector of types; each element is
2702 either NULL or the original type of the corresponding element in
2703 PARAMS. The original type may differ from TREE_TYPE of the
2704 parameter for enums. FUNCTION's data type may be a function type
2705 or pointer-to-function. This function changes the elements of
2706 PARAMS. */
2708 tree
2709 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2710 VEC(tree,gc) *origtypes)
2712 tree fntype, fundecl = 0;
2713 tree name = NULL_TREE, result;
2714 tree tem;
2715 int nargs;
2716 tree *argarray;
2719 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2720 STRIP_TYPE_NOPS (function);
2722 /* Convert anything with function type to a pointer-to-function. */
2723 if (TREE_CODE (function) == FUNCTION_DECL)
2725 /* Implement type-directed function overloading for builtins.
2726 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2727 handle all the type checking. The result is a complete expression
2728 that implements this function call. */
2729 tem = resolve_overloaded_builtin (loc, function, params);
2730 if (tem)
2731 return tem;
2733 name = DECL_NAME (function);
2734 fundecl = function;
2736 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2737 function = function_to_pointer_conversion (loc, function);
2739 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2740 expressions, like those used for ObjC messenger dispatches. */
2741 if (!VEC_empty (tree, params))
2742 function = objc_rewrite_function_call (function,
2743 VEC_index (tree, params, 0));
2745 function = c_fully_fold (function, false, NULL);
2747 fntype = TREE_TYPE (function);
2749 if (TREE_CODE (fntype) == ERROR_MARK)
2750 return error_mark_node;
2752 if (!(TREE_CODE (fntype) == POINTER_TYPE
2753 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2755 error_at (loc, "called object %qE is not a function", function);
2756 return error_mark_node;
2759 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2760 current_function_returns_abnormally = 1;
2762 /* fntype now gets the type of function pointed to. */
2763 fntype = TREE_TYPE (fntype);
2765 /* Convert the parameters to the types declared in the
2766 function prototype, or apply default promotions. */
2768 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2769 function, fundecl);
2770 if (nargs < 0)
2771 return error_mark_node;
2773 /* Check that the function is called through a compatible prototype.
2774 If it is not, replace the call by a trap, wrapped up in a compound
2775 expression if necessary. This has the nice side-effect to prevent
2776 the tree-inliner from generating invalid assignment trees which may
2777 blow up in the RTL expander later. */
2778 if (CONVERT_EXPR_P (function)
2779 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2780 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2781 && !comptypes (fntype, TREE_TYPE (tem)))
2783 tree return_type = TREE_TYPE (fntype);
2784 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2785 NULL_TREE);
2786 int i;
2788 /* This situation leads to run-time undefined behavior. We can't,
2789 therefore, simply error unless we can prove that all possible
2790 executions of the program must execute the code. */
2791 if (warning_at (loc, 0, "function called through a non-compatible type"))
2792 /* We can, however, treat "undefined" any way we please.
2793 Call abort to encourage the user to fix the program. */
2794 inform (loc, "if this code is reached, the program will abort");
2795 /* Before the abort, allow the function arguments to exit or
2796 call longjmp. */
2797 for (i = 0; i < nargs; i++)
2798 trap = build2 (COMPOUND_EXPR, void_type_node,
2799 VEC_index (tree, params, i), trap);
2801 if (VOID_TYPE_P (return_type))
2803 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2804 pedwarn (loc, 0,
2805 "function with qualified void return type called");
2806 return trap;
2808 else
2810 tree rhs;
2812 if (AGGREGATE_TYPE_P (return_type))
2813 rhs = build_compound_literal (loc, return_type,
2814 build_constructor (return_type, 0),
2815 false);
2816 else
2817 rhs = build_zero_cst (return_type);
2819 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2820 trap, rhs));
2824 argarray = VEC_address (tree, params);
2826 /* Check that arguments to builtin functions match the expectations. */
2827 if (fundecl
2828 && DECL_BUILT_IN (fundecl)
2829 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2830 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2831 return error_mark_node;
2833 /* Check that the arguments to the function are valid. */
2834 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2835 TYPE_ARG_TYPES (fntype));
2837 if (name != NULL_TREE
2838 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2840 if (require_constant_value)
2841 result =
2842 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2843 function, nargs, argarray);
2844 else
2845 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2846 function, nargs, argarray);
2847 if (TREE_CODE (result) == NOP_EXPR
2848 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2849 STRIP_TYPE_NOPS (result);
2851 else
2852 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2853 function, nargs, argarray);
2855 if (VOID_TYPE_P (TREE_TYPE (result)))
2857 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2858 pedwarn (loc, 0,
2859 "function with qualified void return type called");
2860 return result;
2862 return require_complete_type (result);
2865 /* Convert the argument expressions in the vector VALUES
2866 to the types in the list TYPELIST.
2868 If TYPELIST is exhausted, or when an element has NULL as its type,
2869 perform the default conversions.
2871 ORIGTYPES is the original types of the expressions in VALUES. This
2872 holds the type of enum values which have been converted to integral
2873 types. It may be NULL.
2875 FUNCTION is a tree for the called function. It is used only for
2876 error messages, where it is formatted with %qE.
2878 This is also where warnings about wrong number of args are generated.
2880 Returns the actual number of arguments processed (which may be less
2881 than the length of VALUES in some error situations), or -1 on
2882 failure. */
2884 static int
2885 convert_arguments (tree typelist, VEC(tree,gc) *values,
2886 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2888 tree typetail, val;
2889 unsigned int parmnum;
2890 bool error_args = false;
2891 const bool type_generic = fundecl
2892 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2893 bool type_generic_remove_excess_precision = false;
2894 tree selector;
2896 /* Change pointer to function to the function itself for
2897 diagnostics. */
2898 if (TREE_CODE (function) == ADDR_EXPR
2899 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2900 function = TREE_OPERAND (function, 0);
2902 /* Handle an ObjC selector specially for diagnostics. */
2903 selector = objc_message_selector ();
2905 /* For type-generic built-in functions, determine whether excess
2906 precision should be removed (classification) or not
2907 (comparison). */
2908 if (type_generic
2909 && DECL_BUILT_IN (fundecl)
2910 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2912 switch (DECL_FUNCTION_CODE (fundecl))
2914 case BUILT_IN_ISFINITE:
2915 case BUILT_IN_ISINF:
2916 case BUILT_IN_ISINF_SIGN:
2917 case BUILT_IN_ISNAN:
2918 case BUILT_IN_ISNORMAL:
2919 case BUILT_IN_FPCLASSIFY:
2920 type_generic_remove_excess_precision = true;
2921 break;
2923 default:
2924 type_generic_remove_excess_precision = false;
2925 break;
2929 /* Scan the given expressions and types, producing individual
2930 converted arguments. */
2932 for (typetail = typelist, parmnum = 0;
2933 VEC_iterate (tree, values, parmnum, val);
2934 ++parmnum)
2936 tree type = typetail ? TREE_VALUE (typetail) : 0;
2937 tree valtype = TREE_TYPE (val);
2938 tree rname = function;
2939 int argnum = parmnum + 1;
2940 const char *invalid_func_diag;
2941 bool excess_precision = false;
2942 bool npc;
2943 tree parmval;
2945 if (type == void_type_node)
2947 if (selector)
2948 error_at (input_location,
2949 "too many arguments to method %qE", selector);
2950 else
2951 error_at (input_location,
2952 "too many arguments to function %qE", function);
2954 if (fundecl && !DECL_BUILT_IN (fundecl))
2955 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2956 return parmnum;
2959 if (selector && argnum > 2)
2961 rname = selector;
2962 argnum -= 2;
2965 npc = null_pointer_constant_p (val);
2967 /* If there is excess precision and a prototype, convert once to
2968 the required type rather than converting via the semantic
2969 type. Likewise without a prototype a float value represented
2970 as long double should be converted once to double. But for
2971 type-generic classification functions excess precision must
2972 be removed here. */
2973 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2974 && (type || !type_generic || !type_generic_remove_excess_precision))
2976 val = TREE_OPERAND (val, 0);
2977 excess_precision = true;
2979 val = c_fully_fold (val, false, NULL);
2980 STRIP_TYPE_NOPS (val);
2982 val = require_complete_type (val);
2984 if (type != 0)
2986 /* Formal parm type is specified by a function prototype. */
2988 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2990 error ("type of formal parameter %d is incomplete", parmnum + 1);
2991 parmval = val;
2993 else
2995 tree origtype;
2997 /* Optionally warn about conversions that
2998 differ from the default conversions. */
2999 if (warn_traditional_conversion || warn_traditional)
3001 unsigned int formal_prec = TYPE_PRECISION (type);
3003 if (INTEGRAL_TYPE_P (type)
3004 && TREE_CODE (valtype) == REAL_TYPE)
3005 warning (0, "passing argument %d of %qE as integer "
3006 "rather than floating due to prototype",
3007 argnum, rname);
3008 if (INTEGRAL_TYPE_P (type)
3009 && TREE_CODE (valtype) == COMPLEX_TYPE)
3010 warning (0, "passing argument %d of %qE as integer "
3011 "rather than complex due to prototype",
3012 argnum, rname);
3013 else if (TREE_CODE (type) == COMPLEX_TYPE
3014 && TREE_CODE (valtype) == REAL_TYPE)
3015 warning (0, "passing argument %d of %qE as complex "
3016 "rather than floating due to prototype",
3017 argnum, rname);
3018 else if (TREE_CODE (type) == REAL_TYPE
3019 && INTEGRAL_TYPE_P (valtype))
3020 warning (0, "passing argument %d of %qE as floating "
3021 "rather than integer due to prototype",
3022 argnum, rname);
3023 else if (TREE_CODE (type) == COMPLEX_TYPE
3024 && INTEGRAL_TYPE_P (valtype))
3025 warning (0, "passing argument %d of %qE as complex "
3026 "rather than integer due to prototype",
3027 argnum, rname);
3028 else if (TREE_CODE (type) == REAL_TYPE
3029 && TREE_CODE (valtype) == COMPLEX_TYPE)
3030 warning (0, "passing argument %d of %qE as floating "
3031 "rather than complex due to prototype",
3032 argnum, rname);
3033 /* ??? At some point, messages should be written about
3034 conversions between complex types, but that's too messy
3035 to do now. */
3036 else if (TREE_CODE (type) == REAL_TYPE
3037 && TREE_CODE (valtype) == REAL_TYPE)
3039 /* Warn if any argument is passed as `float',
3040 since without a prototype it would be `double'. */
3041 if (formal_prec == TYPE_PRECISION (float_type_node)
3042 && type != dfloat32_type_node)
3043 warning (0, "passing argument %d of %qE as %<float%> "
3044 "rather than %<double%> due to prototype",
3045 argnum, rname);
3047 /* Warn if mismatch between argument and prototype
3048 for decimal float types. Warn of conversions with
3049 binary float types and of precision narrowing due to
3050 prototype. */
3051 else if (type != valtype
3052 && (type == dfloat32_type_node
3053 || type == dfloat64_type_node
3054 || type == dfloat128_type_node
3055 || valtype == dfloat32_type_node
3056 || valtype == dfloat64_type_node
3057 || valtype == dfloat128_type_node)
3058 && (formal_prec
3059 <= TYPE_PRECISION (valtype)
3060 || (type == dfloat128_type_node
3061 && (valtype
3062 != dfloat64_type_node
3063 && (valtype
3064 != dfloat32_type_node)))
3065 || (type == dfloat64_type_node
3066 && (valtype
3067 != dfloat32_type_node))))
3068 warning (0, "passing argument %d of %qE as %qT "
3069 "rather than %qT due to prototype",
3070 argnum, rname, type, valtype);
3073 /* Detect integer changing in width or signedness.
3074 These warnings are only activated with
3075 -Wtraditional-conversion, not with -Wtraditional. */
3076 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3077 && INTEGRAL_TYPE_P (valtype))
3079 tree would_have_been = default_conversion (val);
3080 tree type1 = TREE_TYPE (would_have_been);
3082 if (TREE_CODE (type) == ENUMERAL_TYPE
3083 && (TYPE_MAIN_VARIANT (type)
3084 == TYPE_MAIN_VARIANT (valtype)))
3085 /* No warning if function asks for enum
3086 and the actual arg is that enum type. */
3088 else if (formal_prec != TYPE_PRECISION (type1))
3089 warning (OPT_Wtraditional_conversion,
3090 "passing argument %d of %qE "
3091 "with different width due to prototype",
3092 argnum, rname);
3093 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3095 /* Don't complain if the formal parameter type
3096 is an enum, because we can't tell now whether
3097 the value was an enum--even the same enum. */
3098 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3100 else if (TREE_CODE (val) == INTEGER_CST
3101 && int_fits_type_p (val, type))
3102 /* Change in signedness doesn't matter
3103 if a constant value is unaffected. */
3105 /* If the value is extended from a narrower
3106 unsigned type, it doesn't matter whether we
3107 pass it as signed or unsigned; the value
3108 certainly is the same either way. */
3109 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3110 && TYPE_UNSIGNED (valtype))
3112 else if (TYPE_UNSIGNED (type))
3113 warning (OPT_Wtraditional_conversion,
3114 "passing argument %d of %qE "
3115 "as unsigned due to prototype",
3116 argnum, rname);
3117 else
3118 warning (OPT_Wtraditional_conversion,
3119 "passing argument %d of %qE "
3120 "as signed due to prototype", argnum, rname);
3124 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3125 sake of better warnings from convert_and_check. */
3126 if (excess_precision)
3127 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3128 origtype = (origtypes == NULL
3129 ? NULL_TREE
3130 : VEC_index (tree, origtypes, parmnum));
3131 parmval = convert_for_assignment (input_location, type, val,
3132 origtype, ic_argpass, npc,
3133 fundecl, function,
3134 parmnum + 1);
3136 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3137 && INTEGRAL_TYPE_P (type)
3138 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3139 parmval = default_conversion (parmval);
3142 else if (TREE_CODE (valtype) == REAL_TYPE
3143 && (TYPE_PRECISION (valtype)
3144 < TYPE_PRECISION (double_type_node))
3145 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3147 if (type_generic)
3148 parmval = val;
3149 else
3151 /* Convert `float' to `double'. */
3152 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3153 warning (OPT_Wdouble_promotion,
3154 "implicit conversion from %qT to %qT when passing "
3155 "argument to function",
3156 valtype, double_type_node);
3157 parmval = convert (double_type_node, val);
3160 else if (excess_precision && !type_generic)
3161 /* A "double" argument with excess precision being passed
3162 without a prototype or in variable arguments. */
3163 parmval = convert (valtype, val);
3164 else if ((invalid_func_diag =
3165 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3167 error (invalid_func_diag);
3168 return -1;
3170 else
3171 /* Convert `short' and `char' to full-size `int'. */
3172 parmval = default_conversion (val);
3174 VEC_replace (tree, values, parmnum, parmval);
3175 if (parmval == error_mark_node)
3176 error_args = true;
3178 if (typetail)
3179 typetail = TREE_CHAIN (typetail);
3182 gcc_assert (parmnum == VEC_length (tree, values));
3184 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3186 error_at (input_location,
3187 "too few arguments to function %qE", function);
3188 if (fundecl && !DECL_BUILT_IN (fundecl))
3189 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3190 return -1;
3193 return error_args ? -1 : (int) parmnum;
3196 /* This is the entry point used by the parser to build unary operators
3197 in the input. CODE, a tree_code, specifies the unary operator, and
3198 ARG is the operand. For unary plus, the C parser currently uses
3199 CONVERT_EXPR for code.
3201 LOC is the location to use for the tree generated.
3204 struct c_expr
3205 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3207 struct c_expr result;
3209 result.value = build_unary_op (loc, code, arg.value, 0);
3210 result.original_code = code;
3211 result.original_type = NULL;
3213 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3214 overflow_warning (loc, result.value);
3216 return result;
3219 /* This is the entry point used by the parser to build binary operators
3220 in the input. CODE, a tree_code, specifies the binary operator, and
3221 ARG1 and ARG2 are the operands. In addition to constructing the
3222 expression, we check for operands that were written with other binary
3223 operators in a way that is likely to confuse the user.
3225 LOCATION is the location of the binary operator. */
3227 struct c_expr
3228 parser_build_binary_op (location_t location, enum tree_code code,
3229 struct c_expr arg1, struct c_expr arg2)
3231 struct c_expr result;
3233 enum tree_code code1 = arg1.original_code;
3234 enum tree_code code2 = arg2.original_code;
3235 tree type1 = (arg1.original_type
3236 ? arg1.original_type
3237 : TREE_TYPE (arg1.value));
3238 tree type2 = (arg2.original_type
3239 ? arg2.original_type
3240 : TREE_TYPE (arg2.value));
3242 result.value = build_binary_op (location, code,
3243 arg1.value, arg2.value, 1);
3244 result.original_code = code;
3245 result.original_type = NULL;
3247 if (TREE_CODE (result.value) == ERROR_MARK)
3248 return result;
3250 if (location != UNKNOWN_LOCATION)
3251 protected_set_expr_location (result.value, location);
3253 /* Check for cases such as x+y<<z which users are likely
3254 to misinterpret. */
3255 if (warn_parentheses)
3256 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3258 if (warn_logical_op)
3259 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3260 code1, arg1.value, code2, arg2.value);
3262 /* Warn about comparisons against string literals, with the exception
3263 of testing for equality or inequality of a string literal with NULL. */
3264 if (code == EQ_EXPR || code == NE_EXPR)
3266 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3267 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3268 warning_at (location, OPT_Waddress,
3269 "comparison with string literal results in unspecified behavior");
3271 else if (TREE_CODE_CLASS (code) == tcc_comparison
3272 && (code1 == STRING_CST || code2 == STRING_CST))
3273 warning_at (location, OPT_Waddress,
3274 "comparison with string literal results in unspecified behavior");
3276 if (TREE_OVERFLOW_P (result.value)
3277 && !TREE_OVERFLOW_P (arg1.value)
3278 && !TREE_OVERFLOW_P (arg2.value))
3279 overflow_warning (location, result.value);
3281 /* Warn about comparisons of different enum types. */
3282 if (warn_enum_compare
3283 && TREE_CODE_CLASS (code) == tcc_comparison
3284 && TREE_CODE (type1) == ENUMERAL_TYPE
3285 && TREE_CODE (type2) == ENUMERAL_TYPE
3286 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3287 warning_at (location, OPT_Wenum_compare,
3288 "comparison between %qT and %qT",
3289 type1, type2);
3291 return result;
3294 /* Return a tree for the difference of pointers OP0 and OP1.
3295 The resulting tree has type int. */
3297 static tree
3298 pointer_diff (location_t loc, tree op0, tree op1)
3300 tree restype = ptrdiff_type_node;
3301 tree result, inttype;
3303 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3304 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3305 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3306 tree con0, con1, lit0, lit1;
3307 tree orig_op1 = op1;
3309 /* If the operands point into different address spaces, we need to
3310 explicitly convert them to pointers into the common address space
3311 before we can subtract the numerical address values. */
3312 if (as0 != as1)
3314 addr_space_t as_common;
3315 tree common_type;
3317 /* Determine the common superset address space. This is guaranteed
3318 to exist because the caller verified that comp_target_types
3319 returned non-zero. */
3320 if (!addr_space_superset (as0, as1, &as_common))
3321 gcc_unreachable ();
3323 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3324 op0 = convert (common_type, op0);
3325 op1 = convert (common_type, op1);
3328 /* Determine integer type to perform computations in. This will usually
3329 be the same as the result type (ptrdiff_t), but may need to be a wider
3330 type if pointers for the address space are wider than ptrdiff_t. */
3331 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3332 inttype = lang_hooks.types.type_for_size
3333 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3334 else
3335 inttype = restype;
3338 if (TREE_CODE (target_type) == VOID_TYPE)
3339 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3340 "pointer of type %<void *%> used in subtraction");
3341 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3342 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3343 "pointer to a function used in subtraction");
3345 /* If the conversion to ptrdiff_type does anything like widening or
3346 converting a partial to an integral mode, we get a convert_expression
3347 that is in the way to do any simplifications.
3348 (fold-const.c doesn't know that the extra bits won't be needed.
3349 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3350 different mode in place.)
3351 So first try to find a common term here 'by hand'; we want to cover
3352 at least the cases that occur in legal static initializers. */
3353 if (CONVERT_EXPR_P (op0)
3354 && (TYPE_PRECISION (TREE_TYPE (op0))
3355 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3356 con0 = TREE_OPERAND (op0, 0);
3357 else
3358 con0 = op0;
3359 if (CONVERT_EXPR_P (op1)
3360 && (TYPE_PRECISION (TREE_TYPE (op1))
3361 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3362 con1 = TREE_OPERAND (op1, 0);
3363 else
3364 con1 = op1;
3366 if (TREE_CODE (con0) == PLUS_EXPR)
3368 lit0 = TREE_OPERAND (con0, 1);
3369 con0 = TREE_OPERAND (con0, 0);
3371 else
3372 lit0 = integer_zero_node;
3374 if (TREE_CODE (con1) == PLUS_EXPR)
3376 lit1 = TREE_OPERAND (con1, 1);
3377 con1 = TREE_OPERAND (con1, 0);
3379 else
3380 lit1 = integer_zero_node;
3382 if (operand_equal_p (con0, con1, 0))
3384 op0 = lit0;
3385 op1 = lit1;
3389 /* First do the subtraction as integers;
3390 then drop through to build the divide operator.
3391 Do not do default conversions on the minus operator
3392 in case restype is a short type. */
3394 op0 = build_binary_op (loc,
3395 MINUS_EXPR, convert (inttype, op0),
3396 convert (inttype, op1), 0);
3397 /* This generates an error if op1 is pointer to incomplete type. */
3398 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3399 error_at (loc, "arithmetic on pointer to an incomplete type");
3401 /* This generates an error if op0 is pointer to incomplete type. */
3402 op1 = c_size_in_bytes (target_type);
3404 /* Divide by the size, in easiest possible way. */
3405 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3406 op0, convert (inttype, op1));
3408 /* Convert to final result type if necessary. */
3409 return convert (restype, result);
3412 /* Construct and perhaps optimize a tree representation
3413 for a unary operation. CODE, a tree_code, specifies the operation
3414 and XARG is the operand.
3415 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3416 the default promotions (such as from short to int).
3417 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3418 allows non-lvalues; this is only used to handle conversion of non-lvalue
3419 arrays to pointers in C99.
3421 LOCATION is the location of the operator. */
3423 tree
3424 build_unary_op (location_t location,
3425 enum tree_code code, tree xarg, int flag)
3427 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3428 tree arg = xarg;
3429 tree argtype = 0;
3430 enum tree_code typecode;
3431 tree val;
3432 tree ret = error_mark_node;
3433 tree eptype = NULL_TREE;
3434 int noconvert = flag;
3435 const char *invalid_op_diag;
3436 bool int_operands;
3438 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3439 if (int_operands)
3440 arg = remove_c_maybe_const_expr (arg);
3442 if (code != ADDR_EXPR)
3443 arg = require_complete_type (arg);
3445 typecode = TREE_CODE (TREE_TYPE (arg));
3446 if (typecode == ERROR_MARK)
3447 return error_mark_node;
3448 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3449 typecode = INTEGER_TYPE;
3451 if ((invalid_op_diag
3452 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3454 error_at (location, invalid_op_diag);
3455 return error_mark_node;
3458 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3460 eptype = TREE_TYPE (arg);
3461 arg = TREE_OPERAND (arg, 0);
3464 switch (code)
3466 case CONVERT_EXPR:
3467 /* This is used for unary plus, because a CONVERT_EXPR
3468 is enough to prevent anybody from looking inside for
3469 associativity, but won't generate any code. */
3470 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3471 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3472 || typecode == VECTOR_TYPE))
3474 error_at (location, "wrong type argument to unary plus");
3475 return error_mark_node;
3477 else if (!noconvert)
3478 arg = default_conversion (arg);
3479 arg = non_lvalue_loc (location, arg);
3480 break;
3482 case NEGATE_EXPR:
3483 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3484 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3485 || typecode == VECTOR_TYPE))
3487 error_at (location, "wrong type argument to unary minus");
3488 return error_mark_node;
3490 else if (!noconvert)
3491 arg = default_conversion (arg);
3492 break;
3494 case BIT_NOT_EXPR:
3495 /* ~ works on integer types and non float vectors. */
3496 if (typecode == INTEGER_TYPE
3497 || (typecode == VECTOR_TYPE
3498 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3500 if (!noconvert)
3501 arg = default_conversion (arg);
3503 else if (typecode == COMPLEX_TYPE)
3505 code = CONJ_EXPR;
3506 pedwarn (location, OPT_pedantic,
3507 "ISO C does not support %<~%> for complex conjugation");
3508 if (!noconvert)
3509 arg = default_conversion (arg);
3511 else
3513 error_at (location, "wrong type argument to bit-complement");
3514 return error_mark_node;
3516 break;
3518 case ABS_EXPR:
3519 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3521 error_at (location, "wrong type argument to abs");
3522 return error_mark_node;
3524 else if (!noconvert)
3525 arg = default_conversion (arg);
3526 break;
3528 case CONJ_EXPR:
3529 /* Conjugating a real value is a no-op, but allow it anyway. */
3530 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3531 || typecode == COMPLEX_TYPE))
3533 error_at (location, "wrong type argument to conjugation");
3534 return error_mark_node;
3536 else if (!noconvert)
3537 arg = default_conversion (arg);
3538 break;
3540 case TRUTH_NOT_EXPR:
3541 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3542 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3543 && typecode != COMPLEX_TYPE)
3545 error_at (location,
3546 "wrong type argument to unary exclamation mark");
3547 return error_mark_node;
3549 arg = c_objc_common_truthvalue_conversion (location, arg);
3550 ret = invert_truthvalue_loc (location, arg);
3551 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3552 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3553 location = EXPR_LOCATION (ret);
3554 goto return_build_unary_op;
3556 case REALPART_EXPR:
3557 case IMAGPART_EXPR:
3558 ret = build_real_imag_expr (location, code, arg);
3559 if (ret == error_mark_node)
3560 return error_mark_node;
3561 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3562 eptype = TREE_TYPE (eptype);
3563 goto return_build_unary_op;
3565 case PREINCREMENT_EXPR:
3566 case POSTINCREMENT_EXPR:
3567 case PREDECREMENT_EXPR:
3568 case POSTDECREMENT_EXPR:
3570 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3572 tree inner = build_unary_op (location, code,
3573 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3574 if (inner == error_mark_node)
3575 return error_mark_node;
3576 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3577 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3578 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3579 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3580 goto return_build_unary_op;
3583 /* Complain about anything that is not a true lvalue. In
3584 Objective-C, skip this check for property_refs. */
3585 if (!objc_is_property_ref (arg)
3586 && !lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3587 || code == POSTINCREMENT_EXPR)
3588 ? lv_increment
3589 : lv_decrement)))
3590 return error_mark_node;
3592 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3594 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3595 warning_at (location, OPT_Wc___compat,
3596 "increment of enumeration value is invalid in C++");
3597 else
3598 warning_at (location, OPT_Wc___compat,
3599 "decrement of enumeration value is invalid in C++");
3602 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3603 arg = c_fully_fold (arg, false, NULL);
3605 /* Increment or decrement the real part of the value,
3606 and don't change the imaginary part. */
3607 if (typecode == COMPLEX_TYPE)
3609 tree real, imag;
3611 pedwarn (location, OPT_pedantic,
3612 "ISO C does not support %<++%> and %<--%> on complex types");
3614 arg = stabilize_reference (arg);
3615 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3616 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3617 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3618 if (real == error_mark_node || imag == error_mark_node)
3619 return error_mark_node;
3620 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3621 real, imag);
3622 goto return_build_unary_op;
3625 /* Report invalid types. */
3627 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3628 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3630 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3631 error_at (location, "wrong type argument to increment");
3632 else
3633 error_at (location, "wrong type argument to decrement");
3635 return error_mark_node;
3639 tree inc;
3641 argtype = TREE_TYPE (arg);
3643 /* Compute the increment. */
3645 if (typecode == POINTER_TYPE)
3647 /* If pointer target is an undefined struct,
3648 we just cannot know how to do the arithmetic. */
3649 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3651 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3652 error_at (location,
3653 "increment of pointer to unknown structure");
3654 else
3655 error_at (location,
3656 "decrement of pointer to unknown structure");
3658 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3659 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3661 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3662 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3663 "wrong type argument to increment");
3664 else
3665 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3666 "wrong type argument to decrement");
3669 inc = c_size_in_bytes (TREE_TYPE (argtype));
3670 inc = fold_convert_loc (location, sizetype, inc);
3672 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3674 /* For signed fract types, we invert ++ to -- or
3675 -- to ++, and change inc from 1 to -1, because
3676 it is not possible to represent 1 in signed fract constants.
3677 For unsigned fract types, the result always overflows and
3678 we get an undefined (original) or the maximum value. */
3679 if (code == PREINCREMENT_EXPR)
3680 code = PREDECREMENT_EXPR;
3681 else if (code == PREDECREMENT_EXPR)
3682 code = PREINCREMENT_EXPR;
3683 else if (code == POSTINCREMENT_EXPR)
3684 code = POSTDECREMENT_EXPR;
3685 else /* code == POSTDECREMENT_EXPR */
3686 code = POSTINCREMENT_EXPR;
3688 inc = integer_minus_one_node;
3689 inc = convert (argtype, inc);
3691 else
3693 inc = integer_one_node;
3694 inc = convert (argtype, inc);
3697 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3698 need to ask Objective-C to build the increment or decrement
3699 expression for it. */
3700 if (objc_is_property_ref (arg))
3701 return objc_build_incr_expr_for_property_ref (location, code,
3702 arg, inc);
3704 /* Report a read-only lvalue. */
3705 if (TYPE_READONLY (argtype))
3707 readonly_error (arg,
3708 ((code == PREINCREMENT_EXPR
3709 || code == POSTINCREMENT_EXPR)
3710 ? lv_increment : lv_decrement));
3711 return error_mark_node;
3713 else if (TREE_READONLY (arg))
3714 readonly_warning (arg,
3715 ((code == PREINCREMENT_EXPR
3716 || code == POSTINCREMENT_EXPR)
3717 ? lv_increment : lv_decrement));
3719 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3720 val = boolean_increment (code, arg);
3721 else
3722 val = build2 (code, TREE_TYPE (arg), arg, inc);
3723 TREE_SIDE_EFFECTS (val) = 1;
3724 if (TREE_CODE (val) != code)
3725 TREE_NO_WARNING (val) = 1;
3726 ret = val;
3727 goto return_build_unary_op;
3730 case ADDR_EXPR:
3731 /* Note that this operation never does default_conversion. */
3733 /* The operand of unary '&' must be an lvalue (which excludes
3734 expressions of type void), or, in C99, the result of a [] or
3735 unary '*' operator. */
3736 if (VOID_TYPE_P (TREE_TYPE (arg))
3737 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3738 && (TREE_CODE (arg) != INDIRECT_REF
3739 || !flag_isoc99))
3740 pedwarn (location, 0, "taking address of expression of type %<void%>");
3742 /* Let &* cancel out to simplify resulting code. */
3743 if (TREE_CODE (arg) == INDIRECT_REF)
3745 /* Don't let this be an lvalue. */
3746 if (lvalue_p (TREE_OPERAND (arg, 0)))
3747 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3748 ret = TREE_OPERAND (arg, 0);
3749 goto return_build_unary_op;
3752 /* For &x[y], return x+y */
3753 if (TREE_CODE (arg) == ARRAY_REF)
3755 tree op0 = TREE_OPERAND (arg, 0);
3756 if (!c_mark_addressable (op0))
3757 return error_mark_node;
3758 return build_binary_op (location, PLUS_EXPR,
3759 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3760 ? array_to_pointer_conversion (location,
3761 op0)
3762 : op0),
3763 TREE_OPERAND (arg, 1), 1);
3766 /* Anything not already handled and not a true memory reference
3767 or a non-lvalue array is an error. */
3768 else if (typecode != FUNCTION_TYPE && !flag
3769 && !lvalue_or_else (arg, lv_addressof))
3770 return error_mark_node;
3772 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3773 folding later. */
3774 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3776 tree inner = build_unary_op (location, code,
3777 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3778 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3779 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3780 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3781 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3782 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3783 goto return_build_unary_op;
3786 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3787 argtype = TREE_TYPE (arg);
3789 /* If the lvalue is const or volatile, merge that into the type
3790 to which the address will point. This should only be needed
3791 for function types. */
3792 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3793 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3795 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3796 int quals = orig_quals;
3798 if (TREE_READONLY (arg))
3799 quals |= TYPE_QUAL_CONST;
3800 if (TREE_THIS_VOLATILE (arg))
3801 quals |= TYPE_QUAL_VOLATILE;
3803 gcc_assert (quals == orig_quals
3804 || TREE_CODE (argtype) == FUNCTION_TYPE);
3806 argtype = c_build_qualified_type (argtype, quals);
3809 if (!c_mark_addressable (arg))
3810 return error_mark_node;
3812 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3813 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3815 argtype = build_pointer_type (argtype);
3817 /* ??? Cope with user tricks that amount to offsetof. Delete this
3818 when we have proper support for integer constant expressions. */
3819 val = get_base_address (arg);
3820 if (val && TREE_CODE (val) == INDIRECT_REF
3821 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3823 tree op0 = fold_convert_loc (location, sizetype,
3824 fold_offsetof (arg, val)), op1;
3826 op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3827 ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0);
3828 goto return_build_unary_op;
3831 val = build1 (ADDR_EXPR, argtype, arg);
3833 ret = val;
3834 goto return_build_unary_op;
3836 default:
3837 gcc_unreachable ();
3840 if (argtype == 0)
3841 argtype = TREE_TYPE (arg);
3842 if (TREE_CODE (arg) == INTEGER_CST)
3843 ret = (require_constant_value
3844 ? fold_build1_initializer_loc (location, code, argtype, arg)
3845 : fold_build1_loc (location, code, argtype, arg));
3846 else
3847 ret = build1 (code, argtype, arg);
3848 return_build_unary_op:
3849 gcc_assert (ret != error_mark_node);
3850 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3851 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3852 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3853 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3854 ret = note_integer_operands (ret);
3855 if (eptype)
3856 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3857 protected_set_expr_location (ret, location);
3858 return ret;
3861 /* Return nonzero if REF is an lvalue valid for this language.
3862 Lvalues can be assigned, unless their type has TYPE_READONLY.
3863 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3865 bool
3866 lvalue_p (const_tree ref)
3868 const enum tree_code code = TREE_CODE (ref);
3870 switch (code)
3872 case REALPART_EXPR:
3873 case IMAGPART_EXPR:
3874 case COMPONENT_REF:
3875 return lvalue_p (TREE_OPERAND (ref, 0));
3877 case C_MAYBE_CONST_EXPR:
3878 return lvalue_p (TREE_OPERAND (ref, 1));
3880 case COMPOUND_LITERAL_EXPR:
3881 case STRING_CST:
3882 return 1;
3884 case INDIRECT_REF:
3885 case ARRAY_REF:
3886 case VAR_DECL:
3887 case PARM_DECL:
3888 case RESULT_DECL:
3889 case ERROR_MARK:
3890 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3891 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3893 case BIND_EXPR:
3894 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3896 default:
3897 return 0;
3901 /* Give an error for storing in something that is 'const'. */
3903 static void
3904 readonly_error (tree arg, enum lvalue_use use)
3906 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3907 || use == lv_asm);
3908 /* Using this macro rather than (for example) arrays of messages
3909 ensures that all the format strings are checked at compile
3910 time. */
3911 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3912 : (use == lv_increment ? (I) \
3913 : (use == lv_decrement ? (D) : (AS))))
3914 if (TREE_CODE (arg) == COMPONENT_REF)
3916 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3917 readonly_error (TREE_OPERAND (arg, 0), use);
3918 else
3919 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3920 G_("increment of read-only member %qD"),
3921 G_("decrement of read-only member %qD"),
3922 G_("read-only member %qD used as %<asm%> output")),
3923 TREE_OPERAND (arg, 1));
3925 else if (TREE_CODE (arg) == VAR_DECL)
3926 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3927 G_("increment of read-only variable %qD"),
3928 G_("decrement of read-only variable %qD"),
3929 G_("read-only variable %qD used as %<asm%> output")),
3930 arg);
3931 else
3932 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3933 G_("increment of read-only location %qE"),
3934 G_("decrement of read-only location %qE"),
3935 G_("read-only location %qE used as %<asm%> output")),
3936 arg);
3939 /* Give a warning for storing in something that is read-only in GCC
3940 terms but not const in ISO C terms. */
3942 static void
3943 readonly_warning (tree arg, enum lvalue_use use)
3945 switch (use)
3947 case lv_assign:
3948 warning (0, "assignment of read-only location %qE", arg);
3949 break;
3950 case lv_increment:
3951 warning (0, "increment of read-only location %qE", arg);
3952 break;
3953 case lv_decrement:
3954 warning (0, "decrement of read-only location %qE", arg);
3955 break;
3956 default:
3957 gcc_unreachable ();
3959 return;
3963 /* Return nonzero if REF is an lvalue valid for this language;
3964 otherwise, print an error message and return zero. USE says
3965 how the lvalue is being used and so selects the error message. */
3967 static int
3968 lvalue_or_else (const_tree ref, enum lvalue_use use)
3970 int win = lvalue_p (ref);
3972 if (!win)
3973 lvalue_error (use);
3975 return win;
3978 /* Mark EXP saying that we need to be able to take the
3979 address of it; it should not be allocated in a register.
3980 Returns true if successful. */
3982 bool
3983 c_mark_addressable (tree exp)
3985 tree x = exp;
3987 while (1)
3988 switch (TREE_CODE (x))
3990 case COMPONENT_REF:
3991 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3993 error
3994 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3995 return false;
3998 /* ... fall through ... */
4000 case ADDR_EXPR:
4001 case ARRAY_REF:
4002 case REALPART_EXPR:
4003 case IMAGPART_EXPR:
4004 x = TREE_OPERAND (x, 0);
4005 break;
4007 case COMPOUND_LITERAL_EXPR:
4008 case CONSTRUCTOR:
4009 TREE_ADDRESSABLE (x) = 1;
4010 return true;
4012 case VAR_DECL:
4013 case CONST_DECL:
4014 case PARM_DECL:
4015 case RESULT_DECL:
4016 if (C_DECL_REGISTER (x)
4017 && DECL_NONLOCAL (x))
4019 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4021 error
4022 ("global register variable %qD used in nested function", x);
4023 return false;
4025 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4027 else if (C_DECL_REGISTER (x))
4029 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4030 error ("address of global register variable %qD requested", x);
4031 else
4032 error ("address of register variable %qD requested", x);
4033 return false;
4036 /* drops in */
4037 case FUNCTION_DECL:
4038 TREE_ADDRESSABLE (x) = 1;
4039 /* drops out */
4040 default:
4041 return true;
4045 /* Convert EXPR to TYPE, warning about conversion problems with
4046 constants. SEMANTIC_TYPE is the type this conversion would use
4047 without excess precision. If SEMANTIC_TYPE is NULL, this function
4048 is equivalent to convert_and_check. This function is a wrapper that
4049 handles conversions that may be different than
4050 the usual ones because of excess precision. */
4052 static tree
4053 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4055 if (TREE_TYPE (expr) == type)
4056 return expr;
4058 if (!semantic_type)
4059 return convert_and_check (type, expr);
4061 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4062 && TREE_TYPE (expr) != semantic_type)
4064 /* For integers, we need to check the real conversion, not
4065 the conversion to the excess precision type. */
4066 expr = convert_and_check (semantic_type, expr);
4068 /* Result type is the excess precision type, which should be
4069 large enough, so do not check. */
4070 return convert (type, expr);
4073 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4074 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4075 if folded to an integer constant then the unselected half may
4076 contain arbitrary operations not normally permitted in constant
4077 expressions. Set the location of the expression to LOC. */
4079 tree
4080 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4081 tree op1, tree op1_original_type, tree op2,
4082 tree op2_original_type)
4084 tree type1;
4085 tree type2;
4086 enum tree_code code1;
4087 enum tree_code code2;
4088 tree result_type = NULL;
4089 tree semantic_result_type = NULL;
4090 tree orig_op1 = op1, orig_op2 = op2;
4091 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4092 bool ifexp_int_operands;
4093 tree ret;
4095 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4096 if (op1_int_operands)
4097 op1 = remove_c_maybe_const_expr (op1);
4098 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4099 if (op2_int_operands)
4100 op2 = remove_c_maybe_const_expr (op2);
4101 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4102 if (ifexp_int_operands)
4103 ifexp = remove_c_maybe_const_expr (ifexp);
4105 /* Promote both alternatives. */
4107 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4108 op1 = default_conversion (op1);
4109 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4110 op2 = default_conversion (op2);
4112 if (TREE_CODE (ifexp) == ERROR_MARK
4113 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4114 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4115 return error_mark_node;
4117 type1 = TREE_TYPE (op1);
4118 code1 = TREE_CODE (type1);
4119 type2 = TREE_TYPE (op2);
4120 code2 = TREE_CODE (type2);
4122 /* C90 does not permit non-lvalue arrays in conditional expressions.
4123 In C99 they will be pointers by now. */
4124 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4126 error_at (colon_loc, "non-lvalue array in conditional expression");
4127 return error_mark_node;
4130 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4131 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4132 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4133 || code1 == COMPLEX_TYPE)
4134 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4135 || code2 == COMPLEX_TYPE))
4137 semantic_result_type = c_common_type (type1, type2);
4138 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4140 op1 = TREE_OPERAND (op1, 0);
4141 type1 = TREE_TYPE (op1);
4142 gcc_assert (TREE_CODE (type1) == code1);
4144 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4146 op2 = TREE_OPERAND (op2, 0);
4147 type2 = TREE_TYPE (op2);
4148 gcc_assert (TREE_CODE (type2) == code2);
4152 if (warn_cxx_compat)
4154 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4155 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4157 if (TREE_CODE (t1) == ENUMERAL_TYPE
4158 && TREE_CODE (t2) == ENUMERAL_TYPE
4159 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4160 warning_at (colon_loc, OPT_Wc___compat,
4161 ("different enum types in conditional is "
4162 "invalid in C++: %qT vs %qT"),
4163 t1, t2);
4166 /* Quickly detect the usual case where op1 and op2 have the same type
4167 after promotion. */
4168 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4170 if (type1 == type2)
4171 result_type = type1;
4172 else
4173 result_type = TYPE_MAIN_VARIANT (type1);
4175 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4176 || code1 == COMPLEX_TYPE)
4177 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4178 || code2 == COMPLEX_TYPE))
4180 result_type = c_common_type (type1, type2);
4181 do_warn_double_promotion (result_type, type1, type2,
4182 "implicit conversion from %qT to %qT to "
4183 "match other result of conditional",
4184 colon_loc);
4186 /* If -Wsign-compare, warn here if type1 and type2 have
4187 different signedness. We'll promote the signed to unsigned
4188 and later code won't know it used to be different.
4189 Do this check on the original types, so that explicit casts
4190 will be considered, but default promotions won't. */
4191 if (c_inhibit_evaluation_warnings == 0)
4193 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4194 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4196 if (unsigned_op1 ^ unsigned_op2)
4198 bool ovf;
4200 /* Do not warn if the result type is signed, since the
4201 signed type will only be chosen if it can represent
4202 all the values of the unsigned type. */
4203 if (!TYPE_UNSIGNED (result_type))
4204 /* OK */;
4205 else
4207 bool op1_maybe_const = true;
4208 bool op2_maybe_const = true;
4210 /* Do not warn if the signed quantity is an
4211 unsuffixed integer literal (or some static
4212 constant expression involving such literals) and
4213 it is non-negative. This warning requires the
4214 operands to be folded for best results, so do
4215 that folding in this case even without
4216 warn_sign_compare to avoid warning options
4217 possibly affecting code generation. */
4218 c_inhibit_evaluation_warnings
4219 += (ifexp == truthvalue_false_node);
4220 op1 = c_fully_fold (op1, require_constant_value,
4221 &op1_maybe_const);
4222 c_inhibit_evaluation_warnings
4223 -= (ifexp == truthvalue_false_node);
4225 c_inhibit_evaluation_warnings
4226 += (ifexp == truthvalue_true_node);
4227 op2 = c_fully_fold (op2, require_constant_value,
4228 &op2_maybe_const);
4229 c_inhibit_evaluation_warnings
4230 -= (ifexp == truthvalue_true_node);
4232 if (warn_sign_compare)
4234 if ((unsigned_op2
4235 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4236 || (unsigned_op1
4237 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4238 /* OK */;
4239 else
4240 warning_at (colon_loc, OPT_Wsign_compare,
4241 ("signed and unsigned type in "
4242 "conditional expression"));
4244 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4245 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4246 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4247 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4252 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4254 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4255 pedwarn (colon_loc, OPT_pedantic,
4256 "ISO C forbids conditional expr with only one void side");
4257 result_type = void_type_node;
4259 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4261 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4262 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4263 addr_space_t as_common;
4265 if (comp_target_types (colon_loc, type1, type2))
4266 result_type = common_pointer_type (type1, type2);
4267 else if (null_pointer_constant_p (orig_op1))
4268 result_type = type2;
4269 else if (null_pointer_constant_p (orig_op2))
4270 result_type = type1;
4271 else if (!addr_space_superset (as1, as2, &as_common))
4273 error_at (colon_loc, "pointers to disjoint address spaces "
4274 "used in conditional expression");
4275 return error_mark_node;
4277 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4279 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4280 pedwarn (colon_loc, OPT_pedantic,
4281 "ISO C forbids conditional expr between "
4282 "%<void *%> and function pointer");
4283 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4284 TREE_TYPE (type2)));
4286 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4288 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4289 pedwarn (colon_loc, OPT_pedantic,
4290 "ISO C forbids conditional expr between "
4291 "%<void *%> and function pointer");
4292 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4293 TREE_TYPE (type1)));
4295 /* Objective-C pointer comparisons are a bit more lenient. */
4296 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4297 result_type = objc_common_type (type1, type2);
4298 else
4300 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4302 pedwarn (colon_loc, 0,
4303 "pointer type mismatch in conditional expression");
4304 result_type = build_pointer_type
4305 (build_qualified_type (void_type_node, qual));
4308 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4310 if (!null_pointer_constant_p (orig_op2))
4311 pedwarn (colon_loc, 0,
4312 "pointer/integer type mismatch in conditional expression");
4313 else
4315 op2 = null_pointer_node;
4317 result_type = type1;
4319 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4321 if (!null_pointer_constant_p (orig_op1))
4322 pedwarn (colon_loc, 0,
4323 "pointer/integer type mismatch in conditional expression");
4324 else
4326 op1 = null_pointer_node;
4328 result_type = type2;
4331 if (!result_type)
4333 if (flag_cond_mismatch)
4334 result_type = void_type_node;
4335 else
4337 error_at (colon_loc, "type mismatch in conditional expression");
4338 return error_mark_node;
4342 /* Merge const and volatile flags of the incoming types. */
4343 result_type
4344 = build_type_variant (result_type,
4345 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4346 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4348 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4349 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4351 if (ifexp_bcp && ifexp == truthvalue_true_node)
4353 op2_int_operands = true;
4354 op1 = c_fully_fold (op1, require_constant_value, NULL);
4356 if (ifexp_bcp && ifexp == truthvalue_false_node)
4358 op1_int_operands = true;
4359 op2 = c_fully_fold (op2, require_constant_value, NULL);
4361 int_const = int_operands = (ifexp_int_operands
4362 && op1_int_operands
4363 && op2_int_operands);
4364 if (int_operands)
4366 int_const = ((ifexp == truthvalue_true_node
4367 && TREE_CODE (orig_op1) == INTEGER_CST
4368 && !TREE_OVERFLOW (orig_op1))
4369 || (ifexp == truthvalue_false_node
4370 && TREE_CODE (orig_op2) == INTEGER_CST
4371 && !TREE_OVERFLOW (orig_op2)));
4373 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4374 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4375 else
4377 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4378 if (int_operands)
4379 ret = note_integer_operands (ret);
4381 if (semantic_result_type)
4382 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4384 protected_set_expr_location (ret, colon_loc);
4385 return ret;
4388 /* Return a compound expression that performs two expressions and
4389 returns the value of the second of them.
4391 LOC is the location of the COMPOUND_EXPR. */
4393 tree
4394 build_compound_expr (location_t loc, tree expr1, tree expr2)
4396 bool expr1_int_operands, expr2_int_operands;
4397 tree eptype = NULL_TREE;
4398 tree ret;
4400 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4401 if (expr1_int_operands)
4402 expr1 = remove_c_maybe_const_expr (expr1);
4403 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4404 if (expr2_int_operands)
4405 expr2 = remove_c_maybe_const_expr (expr2);
4407 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4408 expr1 = TREE_OPERAND (expr1, 0);
4409 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4411 eptype = TREE_TYPE (expr2);
4412 expr2 = TREE_OPERAND (expr2, 0);
4415 if (!TREE_SIDE_EFFECTS (expr1))
4417 /* The left-hand operand of a comma expression is like an expression
4418 statement: with -Wunused, we should warn if it doesn't have
4419 any side-effects, unless it was explicitly cast to (void). */
4420 if (warn_unused_value)
4422 if (VOID_TYPE_P (TREE_TYPE (expr1))
4423 && CONVERT_EXPR_P (expr1))
4424 ; /* (void) a, b */
4425 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4426 && TREE_CODE (expr1) == COMPOUND_EXPR
4427 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4428 ; /* (void) a, (void) b, c */
4429 else
4430 warning_at (loc, OPT_Wunused_value,
4431 "left-hand operand of comma expression has no effect");
4435 /* With -Wunused, we should also warn if the left-hand operand does have
4436 side-effects, but computes a value which is not used. For example, in
4437 `foo() + bar(), baz()' the result of the `+' operator is not used,
4438 so we should issue a warning. */
4439 else if (warn_unused_value)
4440 warn_if_unused_value (expr1, loc);
4442 if (expr2 == error_mark_node)
4443 return error_mark_node;
4445 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4447 if (flag_isoc99
4448 && expr1_int_operands
4449 && expr2_int_operands)
4450 ret = note_integer_operands (ret);
4452 if (eptype)
4453 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4455 protected_set_expr_location (ret, loc);
4456 return ret;
4459 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4460 which we are casting. OTYPE is the type of the expression being
4461 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4462 of the cast. -Wcast-qual appeared on the command line. Named
4463 address space qualifiers are not handled here, because they result
4464 in different warnings. */
4466 static void
4467 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4469 tree in_type = type;
4470 tree in_otype = otype;
4471 int added = 0;
4472 int discarded = 0;
4473 bool is_const;
4475 /* Check that the qualifiers on IN_TYPE are a superset of the
4476 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4477 nodes is uninteresting and we stop as soon as we hit a
4478 non-POINTER_TYPE node on either type. */
4481 in_otype = TREE_TYPE (in_otype);
4482 in_type = TREE_TYPE (in_type);
4484 /* GNU C allows cv-qualified function types. 'const' means the
4485 function is very pure, 'volatile' means it can't return. We
4486 need to warn when such qualifiers are added, not when they're
4487 taken away. */
4488 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4489 && TREE_CODE (in_type) == FUNCTION_TYPE)
4490 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4491 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4492 else
4493 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4494 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4496 while (TREE_CODE (in_type) == POINTER_TYPE
4497 && TREE_CODE (in_otype) == POINTER_TYPE);
4499 if (added)
4500 warning_at (loc, OPT_Wcast_qual,
4501 "cast adds %q#v qualifier to function type", added);
4503 if (discarded)
4504 /* There are qualifiers present in IN_OTYPE that are not present
4505 in IN_TYPE. */
4506 warning_at (loc, OPT_Wcast_qual,
4507 "cast discards %q#v qualifier from pointer target type",
4508 discarded);
4510 if (added || discarded)
4511 return;
4513 /* A cast from **T to const **T is unsafe, because it can cause a
4514 const value to be changed with no additional warning. We only
4515 issue this warning if T is the same on both sides, and we only
4516 issue the warning if there are the same number of pointers on
4517 both sides, as otherwise the cast is clearly unsafe anyhow. A
4518 cast is unsafe when a qualifier is added at one level and const
4519 is not present at all outer levels.
4521 To issue this warning, we check at each level whether the cast
4522 adds new qualifiers not already seen. We don't need to special
4523 case function types, as they won't have the same
4524 TYPE_MAIN_VARIANT. */
4526 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4527 return;
4528 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4529 return;
4531 in_type = type;
4532 in_otype = otype;
4533 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4536 in_type = TREE_TYPE (in_type);
4537 in_otype = TREE_TYPE (in_otype);
4538 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4539 && !is_const)
4541 warning_at (loc, OPT_Wcast_qual,
4542 "to be safe all intermediate pointers in cast from "
4543 "%qT to %qT must be %<const%> qualified",
4544 otype, type);
4545 break;
4547 if (is_const)
4548 is_const = TYPE_READONLY (in_type);
4550 while (TREE_CODE (in_type) == POINTER_TYPE);
4553 /* Build an expression representing a cast to type TYPE of expression EXPR.
4554 LOC is the location of the cast-- typically the open paren of the cast. */
4556 tree
4557 build_c_cast (location_t loc, tree type, tree expr)
4559 tree value;
4561 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4562 expr = TREE_OPERAND (expr, 0);
4564 value = expr;
4566 if (type == error_mark_node || expr == error_mark_node)
4567 return error_mark_node;
4569 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4570 only in <protocol> qualifications. But when constructing cast expressions,
4571 the protocols do matter and must be kept around. */
4572 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4573 return build1 (NOP_EXPR, type, expr);
4575 type = TYPE_MAIN_VARIANT (type);
4577 if (TREE_CODE (type) == ARRAY_TYPE)
4579 error_at (loc, "cast specifies array type");
4580 return error_mark_node;
4583 if (TREE_CODE (type) == FUNCTION_TYPE)
4585 error_at (loc, "cast specifies function type");
4586 return error_mark_node;
4589 if (!VOID_TYPE_P (type))
4591 value = require_complete_type (value);
4592 if (value == error_mark_node)
4593 return error_mark_node;
4596 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4598 if (TREE_CODE (type) == RECORD_TYPE
4599 || TREE_CODE (type) == UNION_TYPE)
4600 pedwarn (loc, OPT_pedantic,
4601 "ISO C forbids casting nonscalar to the same type");
4603 else if (TREE_CODE (type) == UNION_TYPE)
4605 tree field;
4607 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4608 if (TREE_TYPE (field) != error_mark_node
4609 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4610 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4611 break;
4613 if (field)
4615 tree t;
4616 bool maybe_const = true;
4618 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4619 t = c_fully_fold (value, false, &maybe_const);
4620 t = build_constructor_single (type, field, t);
4621 if (!maybe_const)
4622 t = c_wrap_maybe_const (t, true);
4623 t = digest_init (loc, type, t,
4624 NULL_TREE, false, true, 0);
4625 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4626 return t;
4628 error_at (loc, "cast to union type from type not present in union");
4629 return error_mark_node;
4631 else
4633 tree otype, ovalue;
4635 if (type == void_type_node)
4637 tree t = build1 (CONVERT_EXPR, type, value);
4638 SET_EXPR_LOCATION (t, loc);
4639 return t;
4642 otype = TREE_TYPE (value);
4644 /* Optionally warn about potentially worrisome casts. */
4645 if (warn_cast_qual
4646 && TREE_CODE (type) == POINTER_TYPE
4647 && TREE_CODE (otype) == POINTER_TYPE)
4648 handle_warn_cast_qual (loc, type, otype);
4650 /* Warn about conversions between pointers to disjoint
4651 address spaces. */
4652 if (TREE_CODE (type) == POINTER_TYPE
4653 && TREE_CODE (otype) == POINTER_TYPE
4654 && !null_pointer_constant_p (value))
4656 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4657 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4658 addr_space_t as_common;
4660 if (!addr_space_superset (as_to, as_from, &as_common))
4662 if (ADDR_SPACE_GENERIC_P (as_from))
4663 warning_at (loc, 0, "cast to %s address space pointer "
4664 "from disjoint generic address space pointer",
4665 c_addr_space_name (as_to));
4667 else if (ADDR_SPACE_GENERIC_P (as_to))
4668 warning_at (loc, 0, "cast to generic address space pointer "
4669 "from disjoint %s address space pointer",
4670 c_addr_space_name (as_from));
4672 else
4673 warning_at (loc, 0, "cast to %s address space pointer "
4674 "from disjoint %s address space pointer",
4675 c_addr_space_name (as_to),
4676 c_addr_space_name (as_from));
4680 /* Warn about possible alignment problems. */
4681 if (STRICT_ALIGNMENT
4682 && TREE_CODE (type) == POINTER_TYPE
4683 && TREE_CODE (otype) == POINTER_TYPE
4684 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4685 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4686 /* Don't warn about opaque types, where the actual alignment
4687 restriction is unknown. */
4688 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4689 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4690 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4691 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4692 warning_at (loc, OPT_Wcast_align,
4693 "cast increases required alignment of target type");
4695 if (TREE_CODE (type) == INTEGER_TYPE
4696 && TREE_CODE (otype) == POINTER_TYPE
4697 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4698 /* Unlike conversion of integers to pointers, where the
4699 warning is disabled for converting constants because
4700 of cases such as SIG_*, warn about converting constant
4701 pointers to integers. In some cases it may cause unwanted
4702 sign extension, and a warning is appropriate. */
4703 warning_at (loc, OPT_Wpointer_to_int_cast,
4704 "cast from pointer to integer of different size");
4706 if (TREE_CODE (value) == CALL_EXPR
4707 && TREE_CODE (type) != TREE_CODE (otype))
4708 warning_at (loc, OPT_Wbad_function_cast,
4709 "cast from function call of type %qT "
4710 "to non-matching type %qT", otype, type);
4712 if (TREE_CODE (type) == POINTER_TYPE
4713 && TREE_CODE (otype) == INTEGER_TYPE
4714 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4715 /* Don't warn about converting any constant. */
4716 && !TREE_CONSTANT (value))
4717 warning_at (loc,
4718 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4719 "of different size");
4721 if (warn_strict_aliasing <= 2)
4722 strict_aliasing_warning (otype, type, expr);
4724 /* If pedantic, warn for conversions between function and object
4725 pointer types, except for converting a null pointer constant
4726 to function pointer type. */
4727 if (pedantic
4728 && TREE_CODE (type) == POINTER_TYPE
4729 && TREE_CODE (otype) == POINTER_TYPE
4730 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4731 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4732 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4733 "conversion of function pointer to object pointer type");
4735 if (pedantic
4736 && TREE_CODE (type) == POINTER_TYPE
4737 && TREE_CODE (otype) == POINTER_TYPE
4738 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4739 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4740 && !null_pointer_constant_p (value))
4741 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4742 "conversion of object pointer to function pointer type");
4744 ovalue = value;
4745 value = convert (type, value);
4747 /* Ignore any integer overflow caused by the cast. */
4748 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4750 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4752 if (!TREE_OVERFLOW (value))
4754 /* Avoid clobbering a shared constant. */
4755 value = copy_node (value);
4756 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4759 else if (TREE_OVERFLOW (value))
4760 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4761 value = build_int_cst_wide (TREE_TYPE (value),
4762 TREE_INT_CST_LOW (value),
4763 TREE_INT_CST_HIGH (value));
4767 /* Don't let a cast be an lvalue. */
4768 if (value == expr)
4769 value = non_lvalue_loc (loc, value);
4771 /* Don't allow the results of casting to floating-point or complex
4772 types be confused with actual constants, or casts involving
4773 integer and pointer types other than direct integer-to-integer
4774 and integer-to-pointer be confused with integer constant
4775 expressions and null pointer constants. */
4776 if (TREE_CODE (value) == REAL_CST
4777 || TREE_CODE (value) == COMPLEX_CST
4778 || (TREE_CODE (value) == INTEGER_CST
4779 && !((TREE_CODE (expr) == INTEGER_CST
4780 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4781 || TREE_CODE (expr) == REAL_CST
4782 || TREE_CODE (expr) == COMPLEX_CST)))
4783 value = build1 (NOP_EXPR, type, value);
4785 if (CAN_HAVE_LOCATION_P (value))
4786 SET_EXPR_LOCATION (value, loc);
4787 return value;
4790 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4791 location of the open paren of the cast, or the position of the cast
4792 expr. */
4793 tree
4794 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4796 tree type;
4797 tree type_expr = NULL_TREE;
4798 bool type_expr_const = true;
4799 tree ret;
4800 int saved_wsp = warn_strict_prototypes;
4802 /* This avoids warnings about unprototyped casts on
4803 integers. E.g. "#define SIG_DFL (void(*)())0". */
4804 if (TREE_CODE (expr) == INTEGER_CST)
4805 warn_strict_prototypes = 0;
4806 type = groktypename (type_name, &type_expr, &type_expr_const);
4807 warn_strict_prototypes = saved_wsp;
4809 ret = build_c_cast (loc, type, expr);
4810 if (type_expr)
4812 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4813 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4814 SET_EXPR_LOCATION (ret, loc);
4817 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4818 SET_EXPR_LOCATION (ret, loc);
4820 /* C++ does not permits types to be defined in a cast, but it
4821 allows references to incomplete types. */
4822 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4823 warning_at (loc, OPT_Wc___compat,
4824 "defining a type in a cast is invalid in C++");
4826 return ret;
4829 /* Build an assignment expression of lvalue LHS from value RHS.
4830 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4831 may differ from TREE_TYPE (LHS) for an enum bitfield.
4832 MODIFYCODE is the code for a binary operator that we use
4833 to combine the old value of LHS with RHS to get the new value.
4834 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4835 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4836 which may differ from TREE_TYPE (RHS) for an enum value.
4838 LOCATION is the location of the MODIFYCODE operator.
4839 RHS_LOC is the location of the RHS. */
4841 tree
4842 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4843 enum tree_code modifycode,
4844 location_t rhs_loc, tree rhs, tree rhs_origtype)
4846 tree result;
4847 tree newrhs;
4848 tree rhs_semantic_type = NULL_TREE;
4849 tree lhstype = TREE_TYPE (lhs);
4850 tree olhstype = lhstype;
4851 bool npc;
4853 /* Types that aren't fully specified cannot be used in assignments. */
4854 lhs = require_complete_type (lhs);
4856 /* Avoid duplicate error messages from operands that had errors. */
4857 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4858 return error_mark_node;
4860 /* For ObjC properties, defer this check. */
4861 if (!objc_is_property_ref (lhs) && !lvalue_or_else (lhs, lv_assign))
4862 return error_mark_node;
4864 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4866 rhs_semantic_type = TREE_TYPE (rhs);
4867 rhs = TREE_OPERAND (rhs, 0);
4870 newrhs = rhs;
4872 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4874 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4875 lhs_origtype, modifycode, rhs_loc, rhs,
4876 rhs_origtype);
4877 if (inner == error_mark_node)
4878 return error_mark_node;
4879 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4880 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4881 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4882 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4883 protected_set_expr_location (result, location);
4884 return result;
4887 /* If a binary op has been requested, combine the old LHS value with the RHS
4888 producing the value we should actually store into the LHS. */
4890 if (modifycode != NOP_EXPR)
4892 lhs = c_fully_fold (lhs, false, NULL);
4893 lhs = stabilize_reference (lhs);
4894 newrhs = build_binary_op (location,
4895 modifycode, lhs, rhs, 1);
4897 /* The original type of the right hand side is no longer
4898 meaningful. */
4899 rhs_origtype = NULL_TREE;
4902 if (c_dialect_objc ())
4904 /* Check if we are modifying an Objective-C property reference;
4905 if so, we need to generate setter calls. */
4906 result = objc_maybe_build_modify_expr (lhs, newrhs);
4907 if (result)
4908 return result;
4910 /* Else, do the check that we postponed for Objective-C. */
4911 if (!lvalue_or_else (lhs, lv_assign))
4912 return error_mark_node;
4915 /* Give an error for storing in something that is 'const'. */
4917 if (TYPE_READONLY (lhstype)
4918 || ((TREE_CODE (lhstype) == RECORD_TYPE
4919 || TREE_CODE (lhstype) == UNION_TYPE)
4920 && C_TYPE_FIELDS_READONLY (lhstype)))
4922 readonly_error (lhs, lv_assign);
4923 return error_mark_node;
4925 else if (TREE_READONLY (lhs))
4926 readonly_warning (lhs, lv_assign);
4928 /* If storing into a structure or union member,
4929 it has probably been given type `int'.
4930 Compute the type that would go with
4931 the actual amount of storage the member occupies. */
4933 if (TREE_CODE (lhs) == COMPONENT_REF
4934 && (TREE_CODE (lhstype) == INTEGER_TYPE
4935 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4936 || TREE_CODE (lhstype) == REAL_TYPE
4937 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4938 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4940 /* If storing in a field that is in actuality a short or narrower than one,
4941 we must store in the field in its actual type. */
4943 if (lhstype != TREE_TYPE (lhs))
4945 lhs = copy_node (lhs);
4946 TREE_TYPE (lhs) = lhstype;
4949 /* Issue -Wc++-compat warnings about an assignment to an enum type
4950 when LHS does not have its original type. This happens for,
4951 e.g., an enum bitfield in a struct. */
4952 if (warn_cxx_compat
4953 && lhs_origtype != NULL_TREE
4954 && lhs_origtype != lhstype
4955 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4957 tree checktype = (rhs_origtype != NULL_TREE
4958 ? rhs_origtype
4959 : TREE_TYPE (rhs));
4960 if (checktype != error_mark_node
4961 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4962 warning_at (location, OPT_Wc___compat,
4963 "enum conversion in assignment is invalid in C++");
4966 /* Convert new value to destination type. Fold it first, then
4967 restore any excess precision information, for the sake of
4968 conversion warnings. */
4970 npc = null_pointer_constant_p (newrhs);
4971 newrhs = c_fully_fold (newrhs, false, NULL);
4972 if (rhs_semantic_type)
4973 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4974 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4975 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4976 if (TREE_CODE (newrhs) == ERROR_MARK)
4977 return error_mark_node;
4979 /* Emit ObjC write barrier, if necessary. */
4980 if (c_dialect_objc () && flag_objc_gc)
4982 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4983 if (result)
4985 protected_set_expr_location (result, location);
4986 return result;
4990 /* Scan operands. */
4992 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4993 TREE_SIDE_EFFECTS (result) = 1;
4994 protected_set_expr_location (result, location);
4996 /* If we got the LHS in a different type for storing in,
4997 convert the result back to the nominal type of LHS
4998 so that the value we return always has the same type
4999 as the LHS argument. */
5001 if (olhstype == TREE_TYPE (result))
5002 return result;
5004 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5005 ic_assign, false, NULL_TREE, NULL_TREE, 0);
5006 protected_set_expr_location (result, location);
5007 return result;
5010 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5011 This is used to implement -fplan9-extensions. */
5013 static bool
5014 find_anonymous_field_with_type (tree struct_type, tree type)
5016 tree field;
5017 bool found;
5019 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5020 || TREE_CODE (struct_type) == UNION_TYPE);
5021 found = false;
5022 for (field = TYPE_FIELDS (struct_type);
5023 field != NULL_TREE;
5024 field = TREE_CHAIN (field))
5026 if (DECL_NAME (field) == NULL
5027 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5029 if (found)
5030 return false;
5031 found = true;
5033 else if (DECL_NAME (field) == NULL
5034 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5035 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5036 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5038 if (found)
5039 return false;
5040 found = true;
5043 return found;
5046 /* RHS is an expression whose type is pointer to struct. If there is
5047 an anonymous field in RHS with type TYPE, then return a pointer to
5048 that field in RHS. This is used with -fplan9-extensions. This
5049 returns NULL if no conversion could be found. */
5051 static tree
5052 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5054 tree rhs_struct_type, lhs_main_type;
5055 tree field, found_field;
5056 bool found_sub_field;
5057 tree ret;
5059 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5060 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5061 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5062 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5064 gcc_assert (POINTER_TYPE_P (type));
5065 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5067 found_field = NULL_TREE;
5068 found_sub_field = false;
5069 for (field = TYPE_FIELDS (rhs_struct_type);
5070 field != NULL_TREE;
5071 field = TREE_CHAIN (field))
5073 if (DECL_NAME (field) != NULL_TREE
5074 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5075 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5076 continue;
5077 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5079 if (found_field != NULL_TREE)
5080 return NULL_TREE;
5081 found_field = field;
5083 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5084 lhs_main_type))
5086 if (found_field != NULL_TREE)
5087 return NULL_TREE;
5088 found_field = field;
5089 found_sub_field = true;
5093 if (found_field == NULL_TREE)
5094 return NULL_TREE;
5096 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5097 build_fold_indirect_ref (rhs), found_field,
5098 NULL_TREE);
5099 ret = build_fold_addr_expr_loc (location, ret);
5101 if (found_sub_field)
5103 ret = convert_to_anonymous_field (location, type, ret);
5104 gcc_assert (ret != NULL_TREE);
5107 return ret;
5110 /* Convert value RHS to type TYPE as preparation for an assignment to
5111 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5112 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5113 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5114 constant before any folding.
5115 The real work of conversion is done by `convert'.
5116 The purpose of this function is to generate error messages
5117 for assignments that are not allowed in C.
5118 ERRTYPE says whether it is argument passing, assignment,
5119 initialization or return.
5121 LOCATION is the location of the RHS.
5122 FUNCTION is a tree for the function being called.
5123 PARMNUM is the number of the argument, for printing in error messages. */
5125 static tree
5126 convert_for_assignment (location_t location, tree type, tree rhs,
5127 tree origtype, enum impl_conv errtype,
5128 bool null_pointer_constant, tree fundecl,
5129 tree function, int parmnum)
5131 enum tree_code codel = TREE_CODE (type);
5132 tree orig_rhs = rhs;
5133 tree rhstype;
5134 enum tree_code coder;
5135 tree rname = NULL_TREE;
5136 bool objc_ok = false;
5138 if (errtype == ic_argpass)
5140 tree selector;
5141 /* Change pointer to function to the function itself for
5142 diagnostics. */
5143 if (TREE_CODE (function) == ADDR_EXPR
5144 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5145 function = TREE_OPERAND (function, 0);
5147 /* Handle an ObjC selector specially for diagnostics. */
5148 selector = objc_message_selector ();
5149 rname = function;
5150 if (selector && parmnum > 2)
5152 rname = selector;
5153 parmnum -= 2;
5157 /* This macro is used to emit diagnostics to ensure that all format
5158 strings are complete sentences, visible to gettext and checked at
5159 compile time. */
5160 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5161 do { \
5162 switch (errtype) \
5164 case ic_argpass: \
5165 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5166 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5167 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5168 "expected %qT but argument is of type %qT", \
5169 type, rhstype); \
5170 break; \
5171 case ic_assign: \
5172 pedwarn (LOCATION, OPT, AS); \
5173 break; \
5174 case ic_init: \
5175 pedwarn_init (LOCATION, OPT, IN); \
5176 break; \
5177 case ic_return: \
5178 pedwarn (LOCATION, OPT, RE); \
5179 break; \
5180 default: \
5181 gcc_unreachable (); \
5183 } while (0)
5185 /* This macro is used to emit diagnostics to ensure that all format
5186 strings are complete sentences, visible to gettext and checked at
5187 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5188 extra parameter to enumerate qualifiers. */
5190 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5191 do { \
5192 switch (errtype) \
5194 case ic_argpass: \
5195 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5196 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5197 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5198 "expected %qT but argument is of type %qT", \
5199 type, rhstype); \
5200 break; \
5201 case ic_assign: \
5202 pedwarn (LOCATION, OPT, AS, QUALS); \
5203 break; \
5204 case ic_init: \
5205 pedwarn (LOCATION, OPT, IN, QUALS); \
5206 break; \
5207 case ic_return: \
5208 pedwarn (LOCATION, OPT, RE, QUALS); \
5209 break; \
5210 default: \
5211 gcc_unreachable (); \
5213 } while (0)
5215 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5216 rhs = TREE_OPERAND (rhs, 0);
5218 rhstype = TREE_TYPE (rhs);
5219 coder = TREE_CODE (rhstype);
5221 if (coder == ERROR_MARK)
5222 return error_mark_node;
5224 if (c_dialect_objc ())
5226 int parmno;
5228 switch (errtype)
5230 case ic_return:
5231 parmno = 0;
5232 break;
5234 case ic_assign:
5235 parmno = -1;
5236 break;
5238 case ic_init:
5239 parmno = -2;
5240 break;
5242 default:
5243 parmno = parmnum;
5244 break;
5247 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5250 if (warn_cxx_compat)
5252 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5253 if (checktype != error_mark_node
5254 && TREE_CODE (type) == ENUMERAL_TYPE
5255 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5257 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5258 G_("enum conversion when passing argument "
5259 "%d of %qE is invalid in C++"),
5260 G_("enum conversion in assignment is "
5261 "invalid in C++"),
5262 G_("enum conversion in initialization is "
5263 "invalid in C++"),
5264 G_("enum conversion in return is "
5265 "invalid in C++"));
5269 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5270 return rhs;
5272 if (coder == VOID_TYPE)
5274 /* Except for passing an argument to an unprototyped function,
5275 this is a constraint violation. When passing an argument to
5276 an unprototyped function, it is compile-time undefined;
5277 making it a constraint in that case was rejected in
5278 DR#252. */
5279 error_at (location, "void value not ignored as it ought to be");
5280 return error_mark_node;
5282 rhs = require_complete_type (rhs);
5283 if (rhs == error_mark_node)
5284 return error_mark_node;
5285 /* A type converts to a reference to it.
5286 This code doesn't fully support references, it's just for the
5287 special case of va_start and va_copy. */
5288 if (codel == REFERENCE_TYPE
5289 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5291 if (!lvalue_p (rhs))
5293 error_at (location, "cannot pass rvalue to reference parameter");
5294 return error_mark_node;
5296 if (!c_mark_addressable (rhs))
5297 return error_mark_node;
5298 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5299 SET_EXPR_LOCATION (rhs, location);
5301 /* We already know that these two types are compatible, but they
5302 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5303 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5304 likely to be va_list, a typedef to __builtin_va_list, which
5305 is different enough that it will cause problems later. */
5306 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5308 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5309 SET_EXPR_LOCATION (rhs, location);
5312 rhs = build1 (NOP_EXPR, type, rhs);
5313 SET_EXPR_LOCATION (rhs, location);
5314 return rhs;
5316 /* Some types can interconvert without explicit casts. */
5317 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5318 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5319 return convert (type, rhs);
5320 /* Arithmetic types all interconvert, and enum is treated like int. */
5321 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5322 || codel == FIXED_POINT_TYPE
5323 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5324 || codel == BOOLEAN_TYPE)
5325 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5326 || coder == FIXED_POINT_TYPE
5327 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5328 || coder == BOOLEAN_TYPE))
5330 tree ret;
5331 bool save = in_late_binary_op;
5332 if (codel == BOOLEAN_TYPE)
5333 in_late_binary_op = true;
5334 ret = convert_and_check (type, orig_rhs);
5335 if (codel == BOOLEAN_TYPE)
5336 in_late_binary_op = save;
5337 return ret;
5340 /* Aggregates in different TUs might need conversion. */
5341 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5342 && codel == coder
5343 && comptypes (type, rhstype))
5344 return convert_and_check (type, rhs);
5346 /* Conversion to a transparent union or record from its member types.
5347 This applies only to function arguments. */
5348 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5349 && TYPE_TRANSPARENT_AGGR (type))
5350 && errtype == ic_argpass)
5352 tree memb, marginal_memb = NULL_TREE;
5354 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5356 tree memb_type = TREE_TYPE (memb);
5358 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5359 TYPE_MAIN_VARIANT (rhstype)))
5360 break;
5362 if (TREE_CODE (memb_type) != POINTER_TYPE)
5363 continue;
5365 if (coder == POINTER_TYPE)
5367 tree ttl = TREE_TYPE (memb_type);
5368 tree ttr = TREE_TYPE (rhstype);
5370 /* Any non-function converts to a [const][volatile] void *
5371 and vice versa; otherwise, targets must be the same.
5372 Meanwhile, the lhs target must have all the qualifiers of
5373 the rhs. */
5374 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5375 || comp_target_types (location, memb_type, rhstype))
5377 /* If this type won't generate any warnings, use it. */
5378 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5379 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5380 && TREE_CODE (ttl) == FUNCTION_TYPE)
5381 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5382 == TYPE_QUALS (ttr))
5383 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5384 == TYPE_QUALS (ttl))))
5385 break;
5387 /* Keep looking for a better type, but remember this one. */
5388 if (!marginal_memb)
5389 marginal_memb = memb;
5393 /* Can convert integer zero to any pointer type. */
5394 if (null_pointer_constant)
5396 rhs = null_pointer_node;
5397 break;
5401 if (memb || marginal_memb)
5403 if (!memb)
5405 /* We have only a marginally acceptable member type;
5406 it needs a warning. */
5407 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5408 tree ttr = TREE_TYPE (rhstype);
5410 /* Const and volatile mean something different for function
5411 types, so the usual warnings are not appropriate. */
5412 if (TREE_CODE (ttr) == FUNCTION_TYPE
5413 && TREE_CODE (ttl) == FUNCTION_TYPE)
5415 /* Because const and volatile on functions are
5416 restrictions that say the function will not do
5417 certain things, it is okay to use a const or volatile
5418 function where an ordinary one is wanted, but not
5419 vice-versa. */
5420 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5421 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5422 WARN_FOR_QUALIFIERS (location, 0,
5423 G_("passing argument %d of %qE "
5424 "makes %q#v qualified function "
5425 "pointer from unqualified"),
5426 G_("assignment makes %q#v qualified "
5427 "function pointer from "
5428 "unqualified"),
5429 G_("initialization makes %q#v qualified "
5430 "function pointer from "
5431 "unqualified"),
5432 G_("return makes %q#v qualified function "
5433 "pointer from unqualified"),
5434 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5436 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5437 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5438 WARN_FOR_QUALIFIERS (location, 0,
5439 G_("passing argument %d of %qE discards "
5440 "%qv qualifier from pointer target type"),
5441 G_("assignment discards %qv qualifier "
5442 "from pointer target type"),
5443 G_("initialization discards %qv qualifier "
5444 "from pointer target type"),
5445 G_("return discards %qv qualifier from "
5446 "pointer target type"),
5447 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5449 memb = marginal_memb;
5452 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5453 pedwarn (location, OPT_pedantic,
5454 "ISO C prohibits argument conversion to union type");
5456 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5457 return build_constructor_single (type, memb, rhs);
5461 /* Conversions among pointers */
5462 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5463 && (coder == codel))
5465 tree ttl = TREE_TYPE (type);
5466 tree ttr = TREE_TYPE (rhstype);
5467 tree mvl = ttl;
5468 tree mvr = ttr;
5469 bool is_opaque_pointer;
5470 int target_cmp = 0; /* Cache comp_target_types () result. */
5471 addr_space_t asl;
5472 addr_space_t asr;
5474 if (TREE_CODE (mvl) != ARRAY_TYPE)
5475 mvl = TYPE_MAIN_VARIANT (mvl);
5476 if (TREE_CODE (mvr) != ARRAY_TYPE)
5477 mvr = TYPE_MAIN_VARIANT (mvr);
5478 /* Opaque pointers are treated like void pointers. */
5479 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5481 /* The Plan 9 compiler permits a pointer to a struct to be
5482 automatically converted into a pointer to an anonymous field
5483 within the struct. */
5484 if (flag_plan9_extensions
5485 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5486 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5487 && mvl != mvr)
5489 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5490 if (new_rhs != NULL_TREE)
5492 rhs = new_rhs;
5493 rhstype = TREE_TYPE (rhs);
5494 coder = TREE_CODE (rhstype);
5495 ttr = TREE_TYPE (rhstype);
5496 mvr = TYPE_MAIN_VARIANT (ttr);
5500 /* C++ does not allow the implicit conversion void* -> T*. However,
5501 for the purpose of reducing the number of false positives, we
5502 tolerate the special case of
5504 int *p = NULL;
5506 where NULL is typically defined in C to be '(void *) 0'. */
5507 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5508 warning_at (location, OPT_Wc___compat,
5509 "request for implicit conversion "
5510 "from %qT to %qT not permitted in C++", rhstype, type);
5512 /* See if the pointers point to incompatible address spaces. */
5513 asl = TYPE_ADDR_SPACE (ttl);
5514 asr = TYPE_ADDR_SPACE (ttr);
5515 if (!null_pointer_constant_p (rhs)
5516 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5518 switch (errtype)
5520 case ic_argpass:
5521 error_at (location, "passing argument %d of %qE from pointer to "
5522 "non-enclosed address space", parmnum, rname);
5523 break;
5524 case ic_assign:
5525 error_at (location, "assignment from pointer to "
5526 "non-enclosed address space");
5527 break;
5528 case ic_init:
5529 error_at (location, "initialization from pointer to "
5530 "non-enclosed address space");
5531 break;
5532 case ic_return:
5533 error_at (location, "return from pointer to "
5534 "non-enclosed address space");
5535 break;
5536 default:
5537 gcc_unreachable ();
5539 return error_mark_node;
5542 /* Check if the right-hand side has a format attribute but the
5543 left-hand side doesn't. */
5544 if (warn_missing_format_attribute
5545 && check_missing_format_attribute (type, rhstype))
5547 switch (errtype)
5549 case ic_argpass:
5550 warning_at (location, OPT_Wmissing_format_attribute,
5551 "argument %d of %qE might be "
5552 "a candidate for a format attribute",
5553 parmnum, rname);
5554 break;
5555 case ic_assign:
5556 warning_at (location, OPT_Wmissing_format_attribute,
5557 "assignment left-hand side might be "
5558 "a candidate for a format attribute");
5559 break;
5560 case ic_init:
5561 warning_at (location, OPT_Wmissing_format_attribute,
5562 "initialization left-hand side might be "
5563 "a candidate for a format attribute");
5564 break;
5565 case ic_return:
5566 warning_at (location, OPT_Wmissing_format_attribute,
5567 "return type might be "
5568 "a candidate for a format attribute");
5569 break;
5570 default:
5571 gcc_unreachable ();
5575 /* Any non-function converts to a [const][volatile] void *
5576 and vice versa; otherwise, targets must be the same.
5577 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5578 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5579 || (target_cmp = comp_target_types (location, type, rhstype))
5580 || is_opaque_pointer
5581 || (c_common_unsigned_type (mvl)
5582 == c_common_unsigned_type (mvr)))
5584 if (pedantic
5585 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5587 (VOID_TYPE_P (ttr)
5588 && !null_pointer_constant
5589 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5590 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5591 G_("ISO C forbids passing argument %d of "
5592 "%qE between function pointer "
5593 "and %<void *%>"),
5594 G_("ISO C forbids assignment between "
5595 "function pointer and %<void *%>"),
5596 G_("ISO C forbids initialization between "
5597 "function pointer and %<void *%>"),
5598 G_("ISO C forbids return between function "
5599 "pointer and %<void *%>"));
5600 /* Const and volatile mean something different for function types,
5601 so the usual warnings are not appropriate. */
5602 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5603 && TREE_CODE (ttl) != FUNCTION_TYPE)
5605 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5606 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5608 /* Types differing only by the presence of the 'volatile'
5609 qualifier are acceptable if the 'volatile' has been added
5610 in by the Objective-C EH machinery. */
5611 if (!objc_type_quals_match (ttl, ttr))
5612 WARN_FOR_QUALIFIERS (location, 0,
5613 G_("passing argument %d of %qE discards "
5614 "%qv qualifier from pointer target type"),
5615 G_("assignment discards %qv qualifier "
5616 "from pointer target type"),
5617 G_("initialization discards %qv qualifier "
5618 "from pointer target type"),
5619 G_("return discards %qv qualifier from "
5620 "pointer target type"),
5621 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5623 /* If this is not a case of ignoring a mismatch in signedness,
5624 no warning. */
5625 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5626 || target_cmp)
5628 /* If there is a mismatch, do warn. */
5629 else if (warn_pointer_sign)
5630 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5631 G_("pointer targets in passing argument "
5632 "%d of %qE differ in signedness"),
5633 G_("pointer targets in assignment "
5634 "differ in signedness"),
5635 G_("pointer targets in initialization "
5636 "differ in signedness"),
5637 G_("pointer targets in return differ "
5638 "in signedness"));
5640 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5641 && TREE_CODE (ttr) == FUNCTION_TYPE)
5643 /* Because const and volatile on functions are restrictions
5644 that say the function will not do certain things,
5645 it is okay to use a const or volatile function
5646 where an ordinary one is wanted, but not vice-versa. */
5647 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5648 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5649 WARN_FOR_QUALIFIERS (location, 0,
5650 G_("passing argument %d of %qE makes "
5651 "%q#v qualified function pointer "
5652 "from unqualified"),
5653 G_("assignment makes %q#v qualified function "
5654 "pointer from unqualified"),
5655 G_("initialization makes %q#v qualified "
5656 "function pointer from unqualified"),
5657 G_("return makes %q#v qualified function "
5658 "pointer from unqualified"),
5659 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5662 else
5663 /* Avoid warning about the volatile ObjC EH puts on decls. */
5664 if (!objc_ok)
5665 WARN_FOR_ASSIGNMENT (location, 0,
5666 G_("passing argument %d of %qE from "
5667 "incompatible pointer type"),
5668 G_("assignment from incompatible pointer type"),
5669 G_("initialization from incompatible "
5670 "pointer type"),
5671 G_("return from incompatible pointer type"));
5673 return convert (type, rhs);
5675 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5677 /* ??? This should not be an error when inlining calls to
5678 unprototyped functions. */
5679 error_at (location, "invalid use of non-lvalue array");
5680 return error_mark_node;
5682 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5684 /* An explicit constant 0 can convert to a pointer,
5685 or one that results from arithmetic, even including
5686 a cast to integer type. */
5687 if (!null_pointer_constant)
5688 WARN_FOR_ASSIGNMENT (location, 0,
5689 G_("passing argument %d of %qE makes "
5690 "pointer from integer without a cast"),
5691 G_("assignment makes pointer from integer "
5692 "without a cast"),
5693 G_("initialization makes pointer from "
5694 "integer without a cast"),
5695 G_("return makes pointer from integer "
5696 "without a cast"));
5698 return convert (type, rhs);
5700 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5702 WARN_FOR_ASSIGNMENT (location, 0,
5703 G_("passing argument %d of %qE makes integer "
5704 "from pointer without a cast"),
5705 G_("assignment makes integer from pointer "
5706 "without a cast"),
5707 G_("initialization makes integer from pointer "
5708 "without a cast"),
5709 G_("return makes integer from pointer "
5710 "without a cast"));
5711 return convert (type, rhs);
5713 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5715 tree ret;
5716 bool save = in_late_binary_op;
5717 in_late_binary_op = true;
5718 ret = convert (type, rhs);
5719 in_late_binary_op = save;
5720 return ret;
5723 switch (errtype)
5725 case ic_argpass:
5726 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5727 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5728 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5729 "expected %qT but argument is of type %qT", type, rhstype);
5730 break;
5731 case ic_assign:
5732 error_at (location, "incompatible types when assigning to type %qT from "
5733 "type %qT", type, rhstype);
5734 break;
5735 case ic_init:
5736 error_at (location,
5737 "incompatible types when initializing type %qT using type %qT",
5738 type, rhstype);
5739 break;
5740 case ic_return:
5741 error_at (location,
5742 "incompatible types when returning type %qT but %qT was "
5743 "expected", rhstype, type);
5744 break;
5745 default:
5746 gcc_unreachable ();
5749 return error_mark_node;
5752 /* If VALUE is a compound expr all of whose expressions are constant, then
5753 return its value. Otherwise, return error_mark_node.
5755 This is for handling COMPOUND_EXPRs as initializer elements
5756 which is allowed with a warning when -pedantic is specified. */
5758 static tree
5759 valid_compound_expr_initializer (tree value, tree endtype)
5761 if (TREE_CODE (value) == COMPOUND_EXPR)
5763 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5764 == error_mark_node)
5765 return error_mark_node;
5766 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5767 endtype);
5769 else if (!initializer_constant_valid_p (value, endtype))
5770 return error_mark_node;
5771 else
5772 return value;
5775 /* Perform appropriate conversions on the initial value of a variable,
5776 store it in the declaration DECL,
5777 and print any error messages that are appropriate.
5778 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5779 If the init is invalid, store an ERROR_MARK.
5781 INIT_LOC is the location of the initial value. */
5783 void
5784 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5786 tree value, type;
5787 bool npc = false;
5789 /* If variable's type was invalidly declared, just ignore it. */
5791 type = TREE_TYPE (decl);
5792 if (TREE_CODE (type) == ERROR_MARK)
5793 return;
5795 /* Digest the specified initializer into an expression. */
5797 if (init)
5798 npc = null_pointer_constant_p (init);
5799 value = digest_init (init_loc, type, init, origtype, npc,
5800 true, TREE_STATIC (decl));
5802 /* Store the expression if valid; else report error. */
5804 if (!in_system_header
5805 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5806 warning (OPT_Wtraditional, "traditional C rejects automatic "
5807 "aggregate initialization");
5809 DECL_INITIAL (decl) = value;
5811 /* ANSI wants warnings about out-of-range constant initializers. */
5812 STRIP_TYPE_NOPS (value);
5813 if (TREE_STATIC (decl))
5814 constant_expression_warning (value);
5816 /* Check if we need to set array size from compound literal size. */
5817 if (TREE_CODE (type) == ARRAY_TYPE
5818 && TYPE_DOMAIN (type) == 0
5819 && value != error_mark_node)
5821 tree inside_init = init;
5823 STRIP_TYPE_NOPS (inside_init);
5824 inside_init = fold (inside_init);
5826 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5828 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5830 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5832 /* For int foo[] = (int [3]){1}; we need to set array size
5833 now since later on array initializer will be just the
5834 brace enclosed list of the compound literal. */
5835 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5836 TREE_TYPE (decl) = type;
5837 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5838 layout_type (type);
5839 layout_decl (cldecl, 0);
5845 /* Methods for storing and printing names for error messages. */
5847 /* Implement a spelling stack that allows components of a name to be pushed
5848 and popped. Each element on the stack is this structure. */
5850 struct spelling
5852 int kind;
5853 union
5855 unsigned HOST_WIDE_INT i;
5856 const char *s;
5857 } u;
5860 #define SPELLING_STRING 1
5861 #define SPELLING_MEMBER 2
5862 #define SPELLING_BOUNDS 3
5864 static struct spelling *spelling; /* Next stack element (unused). */
5865 static struct spelling *spelling_base; /* Spelling stack base. */
5866 static int spelling_size; /* Size of the spelling stack. */
5868 /* Macros to save and restore the spelling stack around push_... functions.
5869 Alternative to SAVE_SPELLING_STACK. */
5871 #define SPELLING_DEPTH() (spelling - spelling_base)
5872 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5874 /* Push an element on the spelling stack with type KIND and assign VALUE
5875 to MEMBER. */
5877 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5879 int depth = SPELLING_DEPTH (); \
5881 if (depth >= spelling_size) \
5883 spelling_size += 10; \
5884 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5885 spelling_size); \
5886 RESTORE_SPELLING_DEPTH (depth); \
5889 spelling->kind = (KIND); \
5890 spelling->MEMBER = (VALUE); \
5891 spelling++; \
5894 /* Push STRING on the stack. Printed literally. */
5896 static void
5897 push_string (const char *string)
5899 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5902 /* Push a member name on the stack. Printed as '.' STRING. */
5904 static void
5905 push_member_name (tree decl)
5907 const char *const string
5908 = (DECL_NAME (decl)
5909 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5910 : _("<anonymous>"));
5911 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5914 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5916 static void
5917 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5919 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5922 /* Compute the maximum size in bytes of the printed spelling. */
5924 static int
5925 spelling_length (void)
5927 int size = 0;
5928 struct spelling *p;
5930 for (p = spelling_base; p < spelling; p++)
5932 if (p->kind == SPELLING_BOUNDS)
5933 size += 25;
5934 else
5935 size += strlen (p->u.s) + 1;
5938 return size;
5941 /* Print the spelling to BUFFER and return it. */
5943 static char *
5944 print_spelling (char *buffer)
5946 char *d = buffer;
5947 struct spelling *p;
5949 for (p = spelling_base; p < spelling; p++)
5950 if (p->kind == SPELLING_BOUNDS)
5952 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5953 d += strlen (d);
5955 else
5957 const char *s;
5958 if (p->kind == SPELLING_MEMBER)
5959 *d++ = '.';
5960 for (s = p->u.s; (*d = *s++); d++)
5963 *d++ = '\0';
5964 return buffer;
5967 /* Issue an error message for a bad initializer component.
5968 GMSGID identifies the message.
5969 The component name is taken from the spelling stack. */
5971 void
5972 error_init (const char *gmsgid)
5974 char *ofwhat;
5976 /* The gmsgid may be a format string with %< and %>. */
5977 error (gmsgid);
5978 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5979 if (*ofwhat)
5980 error ("(near initialization for %qs)", ofwhat);
5983 /* Issue a pedantic warning for a bad initializer component. OPT is
5984 the option OPT_* (from options.h) controlling this warning or 0 if
5985 it is unconditionally given. GMSGID identifies the message. The
5986 component name is taken from the spelling stack. */
5988 void
5989 pedwarn_init (location_t location, int opt, const char *gmsgid)
5991 char *ofwhat;
5993 /* The gmsgid may be a format string with %< and %>. */
5994 pedwarn (location, opt, gmsgid);
5995 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5996 if (*ofwhat)
5997 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
6000 /* Issue a warning for a bad initializer component.
6002 OPT is the OPT_W* value corresponding to the warning option that
6003 controls this warning. GMSGID identifies the message. The
6004 component name is taken from the spelling stack. */
6006 static void
6007 warning_init (int opt, const char *gmsgid)
6009 char *ofwhat;
6011 /* The gmsgid may be a format string with %< and %>. */
6012 warning (opt, gmsgid);
6013 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6014 if (*ofwhat)
6015 warning (opt, "(near initialization for %qs)", ofwhat);
6018 /* If TYPE is an array type and EXPR is a parenthesized string
6019 constant, warn if pedantic that EXPR is being used to initialize an
6020 object of type TYPE. */
6022 void
6023 maybe_warn_string_init (tree type, struct c_expr expr)
6025 if (pedantic
6026 && TREE_CODE (type) == ARRAY_TYPE
6027 && TREE_CODE (expr.value) == STRING_CST
6028 && expr.original_code != STRING_CST)
6029 pedwarn_init (input_location, OPT_pedantic,
6030 "array initialized from parenthesized string constant");
6033 /* Digest the parser output INIT as an initializer for type TYPE.
6034 Return a C expression of type TYPE to represent the initial value.
6036 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6038 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6040 If INIT is a string constant, STRICT_STRING is true if it is
6041 unparenthesized or we should not warn here for it being parenthesized.
6042 For other types of INIT, STRICT_STRING is not used.
6044 INIT_LOC is the location of the INIT.
6046 REQUIRE_CONSTANT requests an error if non-constant initializers or
6047 elements are seen. */
6049 static tree
6050 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6051 bool null_pointer_constant, bool strict_string,
6052 int require_constant)
6054 enum tree_code code = TREE_CODE (type);
6055 tree inside_init = init;
6056 tree semantic_type = NULL_TREE;
6057 bool maybe_const = true;
6059 if (type == error_mark_node
6060 || !init
6061 || init == error_mark_node
6062 || TREE_TYPE (init) == error_mark_node)
6063 return error_mark_node;
6065 STRIP_TYPE_NOPS (inside_init);
6067 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6069 semantic_type = TREE_TYPE (inside_init);
6070 inside_init = TREE_OPERAND (inside_init, 0);
6072 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6073 inside_init = decl_constant_value_for_optimization (inside_init);
6075 /* Initialization of an array of chars from a string constant
6076 optionally enclosed in braces. */
6078 if (code == ARRAY_TYPE && inside_init
6079 && TREE_CODE (inside_init) == STRING_CST)
6081 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6082 /* Note that an array could be both an array of character type
6083 and an array of wchar_t if wchar_t is signed char or unsigned
6084 char. */
6085 bool char_array = (typ1 == char_type_node
6086 || typ1 == signed_char_type_node
6087 || typ1 == unsigned_char_type_node);
6088 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6089 bool char16_array = !!comptypes (typ1, char16_type_node);
6090 bool char32_array = !!comptypes (typ1, char32_type_node);
6092 if (char_array || wchar_array || char16_array || char32_array)
6094 struct c_expr expr;
6095 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6096 expr.value = inside_init;
6097 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6098 expr.original_type = NULL;
6099 maybe_warn_string_init (type, expr);
6101 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6102 pedwarn_init (init_loc, OPT_pedantic,
6103 "initialization of a flexible array member");
6105 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6106 TYPE_MAIN_VARIANT (type)))
6107 return inside_init;
6109 if (char_array)
6111 if (typ2 != char_type_node)
6113 error_init ("char-array initialized from wide string");
6114 return error_mark_node;
6117 else
6119 if (typ2 == char_type_node)
6121 error_init ("wide character array initialized from non-wide "
6122 "string");
6123 return error_mark_node;
6125 else if (!comptypes(typ1, typ2))
6127 error_init ("wide character array initialized from "
6128 "incompatible wide string");
6129 return error_mark_node;
6133 TREE_TYPE (inside_init) = type;
6134 if (TYPE_DOMAIN (type) != 0
6135 && TYPE_SIZE (type) != 0
6136 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6138 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6140 /* Subtract the size of a single (possibly wide) character
6141 because it's ok to ignore the terminating null char
6142 that is counted in the length of the constant. */
6143 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6144 (len
6145 - (TYPE_PRECISION (typ1)
6146 / BITS_PER_UNIT))))
6147 pedwarn_init (init_loc, 0,
6148 ("initializer-string for array of chars "
6149 "is too long"));
6150 else if (warn_cxx_compat
6151 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6152 warning_at (init_loc, OPT_Wc___compat,
6153 ("initializer-string for array chars "
6154 "is too long for C++"));
6157 return inside_init;
6159 else if (INTEGRAL_TYPE_P (typ1))
6161 error_init ("array of inappropriate type initialized "
6162 "from string constant");
6163 return error_mark_node;
6167 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6168 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6169 below and handle as a constructor. */
6170 if (code == VECTOR_TYPE
6171 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6172 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6173 && TREE_CONSTANT (inside_init))
6175 if (TREE_CODE (inside_init) == VECTOR_CST
6176 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6177 TYPE_MAIN_VARIANT (type)))
6178 return inside_init;
6180 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6182 unsigned HOST_WIDE_INT ix;
6183 tree value;
6184 bool constant_p = true;
6186 /* Iterate through elements and check if all constructor
6187 elements are *_CSTs. */
6188 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6189 if (!CONSTANT_CLASS_P (value))
6191 constant_p = false;
6192 break;
6195 if (constant_p)
6196 return build_vector_from_ctor (type,
6197 CONSTRUCTOR_ELTS (inside_init));
6201 if (warn_sequence_point)
6202 verify_sequence_points (inside_init);
6204 /* Any type can be initialized
6205 from an expression of the same type, optionally with braces. */
6207 if (inside_init && TREE_TYPE (inside_init) != 0
6208 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6209 TYPE_MAIN_VARIANT (type))
6210 || (code == ARRAY_TYPE
6211 && comptypes (TREE_TYPE (inside_init), type))
6212 || (code == VECTOR_TYPE
6213 && comptypes (TREE_TYPE (inside_init), type))
6214 || (code == POINTER_TYPE
6215 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6216 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6217 TREE_TYPE (type)))))
6219 if (code == POINTER_TYPE)
6221 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6223 if (TREE_CODE (inside_init) == STRING_CST
6224 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6225 inside_init = array_to_pointer_conversion
6226 (init_loc, inside_init);
6227 else
6229 error_init ("invalid use of non-lvalue array");
6230 return error_mark_node;
6235 if (code == VECTOR_TYPE)
6236 /* Although the types are compatible, we may require a
6237 conversion. */
6238 inside_init = convert (type, inside_init);
6240 if (require_constant
6241 && (code == VECTOR_TYPE || !flag_isoc99)
6242 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6244 /* As an extension, allow initializing objects with static storage
6245 duration with compound literals (which are then treated just as
6246 the brace enclosed list they contain). Also allow this for
6247 vectors, as we can only assign them with compound literals. */
6248 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6249 inside_init = DECL_INITIAL (decl);
6252 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6253 && TREE_CODE (inside_init) != CONSTRUCTOR)
6255 error_init ("array initialized from non-constant array expression");
6256 return error_mark_node;
6259 /* Compound expressions can only occur here if -pedantic or
6260 -pedantic-errors is specified. In the later case, we always want
6261 an error. In the former case, we simply want a warning. */
6262 if (require_constant && pedantic
6263 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6265 inside_init
6266 = valid_compound_expr_initializer (inside_init,
6267 TREE_TYPE (inside_init));
6268 if (inside_init == error_mark_node)
6269 error_init ("initializer element is not constant");
6270 else
6271 pedwarn_init (init_loc, OPT_pedantic,
6272 "initializer element is not constant");
6273 if (flag_pedantic_errors)
6274 inside_init = error_mark_node;
6276 else if (require_constant
6277 && !initializer_constant_valid_p (inside_init,
6278 TREE_TYPE (inside_init)))
6280 error_init ("initializer element is not constant");
6281 inside_init = error_mark_node;
6283 else if (require_constant && !maybe_const)
6284 pedwarn_init (init_loc, 0,
6285 "initializer element is not a constant expression");
6287 /* Added to enable additional -Wmissing-format-attribute warnings. */
6288 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6289 inside_init = convert_for_assignment (init_loc, type, inside_init,
6290 origtype,
6291 ic_init, null_pointer_constant,
6292 NULL_TREE, NULL_TREE, 0);
6293 return inside_init;
6296 /* Handle scalar types, including conversions. */
6298 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6299 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6300 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6302 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6303 && (TREE_CODE (init) == STRING_CST
6304 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6305 inside_init = init = array_to_pointer_conversion (init_loc, init);
6306 if (semantic_type)
6307 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6308 inside_init);
6309 inside_init
6310 = convert_for_assignment (init_loc, type, inside_init, origtype,
6311 ic_init, null_pointer_constant,
6312 NULL_TREE, NULL_TREE, 0);
6314 /* Check to see if we have already given an error message. */
6315 if (inside_init == error_mark_node)
6317 else if (require_constant && !TREE_CONSTANT (inside_init))
6319 error_init ("initializer element is not constant");
6320 inside_init = error_mark_node;
6322 else if (require_constant
6323 && !initializer_constant_valid_p (inside_init,
6324 TREE_TYPE (inside_init)))
6326 error_init ("initializer element is not computable at load time");
6327 inside_init = error_mark_node;
6329 else if (require_constant && !maybe_const)
6330 pedwarn_init (init_loc, 0,
6331 "initializer element is not a constant expression");
6333 return inside_init;
6336 /* Come here only for records and arrays. */
6338 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6340 error_init ("variable-sized object may not be initialized");
6341 return error_mark_node;
6344 error_init ("invalid initializer");
6345 return error_mark_node;
6348 /* Handle initializers that use braces. */
6350 /* Type of object we are accumulating a constructor for.
6351 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6352 static tree constructor_type;
6354 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6355 left to fill. */
6356 static tree constructor_fields;
6358 /* For an ARRAY_TYPE, this is the specified index
6359 at which to store the next element we get. */
6360 static tree constructor_index;
6362 /* For an ARRAY_TYPE, this is the maximum index. */
6363 static tree constructor_max_index;
6365 /* For a RECORD_TYPE, this is the first field not yet written out. */
6366 static tree constructor_unfilled_fields;
6368 /* For an ARRAY_TYPE, this is the index of the first element
6369 not yet written out. */
6370 static tree constructor_unfilled_index;
6372 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6373 This is so we can generate gaps between fields, when appropriate. */
6374 static tree constructor_bit_index;
6376 /* If we are saving up the elements rather than allocating them,
6377 this is the list of elements so far (in reverse order,
6378 most recent first). */
6379 static VEC(constructor_elt,gc) *constructor_elements;
6381 /* 1 if constructor should be incrementally stored into a constructor chain,
6382 0 if all the elements should be kept in AVL tree. */
6383 static int constructor_incremental;
6385 /* 1 if so far this constructor's elements are all compile-time constants. */
6386 static int constructor_constant;
6388 /* 1 if so far this constructor's elements are all valid address constants. */
6389 static int constructor_simple;
6391 /* 1 if this constructor has an element that cannot be part of a
6392 constant expression. */
6393 static int constructor_nonconst;
6395 /* 1 if this constructor is erroneous so far. */
6396 static int constructor_erroneous;
6398 /* Structure for managing pending initializer elements, organized as an
6399 AVL tree. */
6401 struct init_node
6403 struct init_node *left, *right;
6404 struct init_node *parent;
6405 int balance;
6406 tree purpose;
6407 tree value;
6408 tree origtype;
6411 /* Tree of pending elements at this constructor level.
6412 These are elements encountered out of order
6413 which belong at places we haven't reached yet in actually
6414 writing the output.
6415 Will never hold tree nodes across GC runs. */
6416 static struct init_node *constructor_pending_elts;
6418 /* The SPELLING_DEPTH of this constructor. */
6419 static int constructor_depth;
6421 /* DECL node for which an initializer is being read.
6422 0 means we are reading a constructor expression
6423 such as (struct foo) {...}. */
6424 static tree constructor_decl;
6426 /* Nonzero if this is an initializer for a top-level decl. */
6427 static int constructor_top_level;
6429 /* Nonzero if there were any member designators in this initializer. */
6430 static int constructor_designated;
6432 /* Nesting depth of designator list. */
6433 static int designator_depth;
6435 /* Nonzero if there were diagnosed errors in this designator list. */
6436 static int designator_erroneous;
6439 /* This stack has a level for each implicit or explicit level of
6440 structuring in the initializer, including the outermost one. It
6441 saves the values of most of the variables above. */
6443 struct constructor_range_stack;
6445 struct constructor_stack
6447 struct constructor_stack *next;
6448 tree type;
6449 tree fields;
6450 tree index;
6451 tree max_index;
6452 tree unfilled_index;
6453 tree unfilled_fields;
6454 tree bit_index;
6455 VEC(constructor_elt,gc) *elements;
6456 struct init_node *pending_elts;
6457 int offset;
6458 int depth;
6459 /* If value nonzero, this value should replace the entire
6460 constructor at this level. */
6461 struct c_expr replacement_value;
6462 struct constructor_range_stack *range_stack;
6463 char constant;
6464 char simple;
6465 char nonconst;
6466 char implicit;
6467 char erroneous;
6468 char outer;
6469 char incremental;
6470 char designated;
6473 static struct constructor_stack *constructor_stack;
6475 /* This stack represents designators from some range designator up to
6476 the last designator in the list. */
6478 struct constructor_range_stack
6480 struct constructor_range_stack *next, *prev;
6481 struct constructor_stack *stack;
6482 tree range_start;
6483 tree index;
6484 tree range_end;
6485 tree fields;
6488 static struct constructor_range_stack *constructor_range_stack;
6490 /* This stack records separate initializers that are nested.
6491 Nested initializers can't happen in ANSI C, but GNU C allows them
6492 in cases like { ... (struct foo) { ... } ... }. */
6494 struct initializer_stack
6496 struct initializer_stack *next;
6497 tree decl;
6498 struct constructor_stack *constructor_stack;
6499 struct constructor_range_stack *constructor_range_stack;
6500 VEC(constructor_elt,gc) *elements;
6501 struct spelling *spelling;
6502 struct spelling *spelling_base;
6503 int spelling_size;
6504 char top_level;
6505 char require_constant_value;
6506 char require_constant_elements;
6509 static struct initializer_stack *initializer_stack;
6511 /* Prepare to parse and output the initializer for variable DECL. */
6513 void
6514 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6516 const char *locus;
6517 struct initializer_stack *p = XNEW (struct initializer_stack);
6519 p->decl = constructor_decl;
6520 p->require_constant_value = require_constant_value;
6521 p->require_constant_elements = require_constant_elements;
6522 p->constructor_stack = constructor_stack;
6523 p->constructor_range_stack = constructor_range_stack;
6524 p->elements = constructor_elements;
6525 p->spelling = spelling;
6526 p->spelling_base = spelling_base;
6527 p->spelling_size = spelling_size;
6528 p->top_level = constructor_top_level;
6529 p->next = initializer_stack;
6530 initializer_stack = p;
6532 constructor_decl = decl;
6533 constructor_designated = 0;
6534 constructor_top_level = top_level;
6536 if (decl != 0 && decl != error_mark_node)
6538 require_constant_value = TREE_STATIC (decl);
6539 require_constant_elements
6540 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6541 /* For a scalar, you can always use any value to initialize,
6542 even within braces. */
6543 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6544 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6545 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6546 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6547 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6549 else
6551 require_constant_value = 0;
6552 require_constant_elements = 0;
6553 locus = _("(anonymous)");
6556 constructor_stack = 0;
6557 constructor_range_stack = 0;
6559 missing_braces_mentioned = 0;
6561 spelling_base = 0;
6562 spelling_size = 0;
6563 RESTORE_SPELLING_DEPTH (0);
6565 if (locus)
6566 push_string (locus);
6569 void
6570 finish_init (void)
6572 struct initializer_stack *p = initializer_stack;
6574 /* Free the whole constructor stack of this initializer. */
6575 while (constructor_stack)
6577 struct constructor_stack *q = constructor_stack;
6578 constructor_stack = q->next;
6579 free (q);
6582 gcc_assert (!constructor_range_stack);
6584 /* Pop back to the data of the outer initializer (if any). */
6585 free (spelling_base);
6587 constructor_decl = p->decl;
6588 require_constant_value = p->require_constant_value;
6589 require_constant_elements = p->require_constant_elements;
6590 constructor_stack = p->constructor_stack;
6591 constructor_range_stack = p->constructor_range_stack;
6592 constructor_elements = p->elements;
6593 spelling = p->spelling;
6594 spelling_base = p->spelling_base;
6595 spelling_size = p->spelling_size;
6596 constructor_top_level = p->top_level;
6597 initializer_stack = p->next;
6598 free (p);
6601 /* Call here when we see the initializer is surrounded by braces.
6602 This is instead of a call to push_init_level;
6603 it is matched by a call to pop_init_level.
6605 TYPE is the type to initialize, for a constructor expression.
6606 For an initializer for a decl, TYPE is zero. */
6608 void
6609 really_start_incremental_init (tree type)
6611 struct constructor_stack *p = XNEW (struct constructor_stack);
6613 if (type == 0)
6614 type = TREE_TYPE (constructor_decl);
6616 if (TREE_CODE (type) == VECTOR_TYPE
6617 && TYPE_VECTOR_OPAQUE (type))
6618 error ("opaque vector types cannot be initialized");
6620 p->type = constructor_type;
6621 p->fields = constructor_fields;
6622 p->index = constructor_index;
6623 p->max_index = constructor_max_index;
6624 p->unfilled_index = constructor_unfilled_index;
6625 p->unfilled_fields = constructor_unfilled_fields;
6626 p->bit_index = constructor_bit_index;
6627 p->elements = constructor_elements;
6628 p->constant = constructor_constant;
6629 p->simple = constructor_simple;
6630 p->nonconst = constructor_nonconst;
6631 p->erroneous = constructor_erroneous;
6632 p->pending_elts = constructor_pending_elts;
6633 p->depth = constructor_depth;
6634 p->replacement_value.value = 0;
6635 p->replacement_value.original_code = ERROR_MARK;
6636 p->replacement_value.original_type = NULL;
6637 p->implicit = 0;
6638 p->range_stack = 0;
6639 p->outer = 0;
6640 p->incremental = constructor_incremental;
6641 p->designated = constructor_designated;
6642 p->next = 0;
6643 constructor_stack = p;
6645 constructor_constant = 1;
6646 constructor_simple = 1;
6647 constructor_nonconst = 0;
6648 constructor_depth = SPELLING_DEPTH ();
6649 constructor_elements = 0;
6650 constructor_pending_elts = 0;
6651 constructor_type = type;
6652 constructor_incremental = 1;
6653 constructor_designated = 0;
6654 designator_depth = 0;
6655 designator_erroneous = 0;
6657 if (TREE_CODE (constructor_type) == RECORD_TYPE
6658 || TREE_CODE (constructor_type) == UNION_TYPE)
6660 constructor_fields = TYPE_FIELDS (constructor_type);
6661 /* Skip any nameless bit fields at the beginning. */
6662 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6663 && DECL_NAME (constructor_fields) == 0)
6664 constructor_fields = DECL_CHAIN (constructor_fields);
6666 constructor_unfilled_fields = constructor_fields;
6667 constructor_bit_index = bitsize_zero_node;
6669 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6671 if (TYPE_DOMAIN (constructor_type))
6673 constructor_max_index
6674 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6676 /* Detect non-empty initializations of zero-length arrays. */
6677 if (constructor_max_index == NULL_TREE
6678 && TYPE_SIZE (constructor_type))
6679 constructor_max_index = integer_minus_one_node;
6681 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6682 to initialize VLAs will cause a proper error; avoid tree
6683 checking errors as well by setting a safe value. */
6684 if (constructor_max_index
6685 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6686 constructor_max_index = integer_minus_one_node;
6688 constructor_index
6689 = convert (bitsizetype,
6690 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6692 else
6694 constructor_index = bitsize_zero_node;
6695 constructor_max_index = NULL_TREE;
6698 constructor_unfilled_index = constructor_index;
6700 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6702 /* Vectors are like simple fixed-size arrays. */
6703 constructor_max_index =
6704 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6705 constructor_index = bitsize_zero_node;
6706 constructor_unfilled_index = constructor_index;
6708 else
6710 /* Handle the case of int x = {5}; */
6711 constructor_fields = constructor_type;
6712 constructor_unfilled_fields = constructor_type;
6716 /* Push down into a subobject, for initialization.
6717 If this is for an explicit set of braces, IMPLICIT is 0.
6718 If it is because the next element belongs at a lower level,
6719 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6721 void
6722 push_init_level (int implicit, struct obstack * braced_init_obstack)
6724 struct constructor_stack *p;
6725 tree value = NULL_TREE;
6727 /* If we've exhausted any levels that didn't have braces,
6728 pop them now. If implicit == 1, this will have been done in
6729 process_init_element; do not repeat it here because in the case
6730 of excess initializers for an empty aggregate this leads to an
6731 infinite cycle of popping a level and immediately recreating
6732 it. */
6733 if (implicit != 1)
6735 while (constructor_stack->implicit)
6737 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6738 || TREE_CODE (constructor_type) == UNION_TYPE)
6739 && constructor_fields == 0)
6740 process_init_element (pop_init_level (1, braced_init_obstack),
6741 true, braced_init_obstack);
6742 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6743 && constructor_max_index
6744 && tree_int_cst_lt (constructor_max_index,
6745 constructor_index))
6746 process_init_element (pop_init_level (1, braced_init_obstack),
6747 true, braced_init_obstack);
6748 else
6749 break;
6753 /* Unless this is an explicit brace, we need to preserve previous
6754 content if any. */
6755 if (implicit)
6757 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6758 || TREE_CODE (constructor_type) == UNION_TYPE)
6759 && constructor_fields)
6760 value = find_init_member (constructor_fields, braced_init_obstack);
6761 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6762 value = find_init_member (constructor_index, braced_init_obstack);
6765 p = XNEW (struct constructor_stack);
6766 p->type = constructor_type;
6767 p->fields = constructor_fields;
6768 p->index = constructor_index;
6769 p->max_index = constructor_max_index;
6770 p->unfilled_index = constructor_unfilled_index;
6771 p->unfilled_fields = constructor_unfilled_fields;
6772 p->bit_index = constructor_bit_index;
6773 p->elements = constructor_elements;
6774 p->constant = constructor_constant;
6775 p->simple = constructor_simple;
6776 p->nonconst = constructor_nonconst;
6777 p->erroneous = constructor_erroneous;
6778 p->pending_elts = constructor_pending_elts;
6779 p->depth = constructor_depth;
6780 p->replacement_value.value = 0;
6781 p->replacement_value.original_code = ERROR_MARK;
6782 p->replacement_value.original_type = NULL;
6783 p->implicit = implicit;
6784 p->outer = 0;
6785 p->incremental = constructor_incremental;
6786 p->designated = constructor_designated;
6787 p->next = constructor_stack;
6788 p->range_stack = 0;
6789 constructor_stack = p;
6791 constructor_constant = 1;
6792 constructor_simple = 1;
6793 constructor_nonconst = 0;
6794 constructor_depth = SPELLING_DEPTH ();
6795 constructor_elements = 0;
6796 constructor_incremental = 1;
6797 constructor_designated = 0;
6798 constructor_pending_elts = 0;
6799 if (!implicit)
6801 p->range_stack = constructor_range_stack;
6802 constructor_range_stack = 0;
6803 designator_depth = 0;
6804 designator_erroneous = 0;
6807 /* Don't die if an entire brace-pair level is superfluous
6808 in the containing level. */
6809 if (constructor_type == 0)
6811 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6812 || TREE_CODE (constructor_type) == UNION_TYPE)
6814 /* Don't die if there are extra init elts at the end. */
6815 if (constructor_fields == 0)
6816 constructor_type = 0;
6817 else
6819 constructor_type = TREE_TYPE (constructor_fields);
6820 push_member_name (constructor_fields);
6821 constructor_depth++;
6824 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6826 constructor_type = TREE_TYPE (constructor_type);
6827 push_array_bounds (tree_low_cst (constructor_index, 1));
6828 constructor_depth++;
6831 if (constructor_type == 0)
6833 error_init ("extra brace group at end of initializer");
6834 constructor_fields = 0;
6835 constructor_unfilled_fields = 0;
6836 return;
6839 if (value && TREE_CODE (value) == CONSTRUCTOR)
6841 constructor_constant = TREE_CONSTANT (value);
6842 constructor_simple = TREE_STATIC (value);
6843 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6844 constructor_elements = CONSTRUCTOR_ELTS (value);
6845 if (!VEC_empty (constructor_elt, constructor_elements)
6846 && (TREE_CODE (constructor_type) == RECORD_TYPE
6847 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6848 set_nonincremental_init (braced_init_obstack);
6851 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6853 missing_braces_mentioned = 1;
6854 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6857 if (TREE_CODE (constructor_type) == RECORD_TYPE
6858 || TREE_CODE (constructor_type) == UNION_TYPE)
6860 constructor_fields = TYPE_FIELDS (constructor_type);
6861 /* Skip any nameless bit fields at the beginning. */
6862 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6863 && DECL_NAME (constructor_fields) == 0)
6864 constructor_fields = DECL_CHAIN (constructor_fields);
6866 constructor_unfilled_fields = constructor_fields;
6867 constructor_bit_index = bitsize_zero_node;
6869 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6871 /* Vectors are like simple fixed-size arrays. */
6872 constructor_max_index =
6873 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6874 constructor_index = convert (bitsizetype, integer_zero_node);
6875 constructor_unfilled_index = constructor_index;
6877 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6879 if (TYPE_DOMAIN (constructor_type))
6881 constructor_max_index
6882 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6884 /* Detect non-empty initializations of zero-length arrays. */
6885 if (constructor_max_index == NULL_TREE
6886 && TYPE_SIZE (constructor_type))
6887 constructor_max_index = integer_minus_one_node;
6889 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6890 to initialize VLAs will cause a proper error; avoid tree
6891 checking errors as well by setting a safe value. */
6892 if (constructor_max_index
6893 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6894 constructor_max_index = integer_minus_one_node;
6896 constructor_index
6897 = convert (bitsizetype,
6898 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6900 else
6901 constructor_index = bitsize_zero_node;
6903 constructor_unfilled_index = constructor_index;
6904 if (value && TREE_CODE (value) == STRING_CST)
6906 /* We need to split the char/wchar array into individual
6907 characters, so that we don't have to special case it
6908 everywhere. */
6909 set_nonincremental_init_from_string (value, braced_init_obstack);
6912 else
6914 if (constructor_type != error_mark_node)
6915 warning_init (0, "braces around scalar initializer");
6916 constructor_fields = constructor_type;
6917 constructor_unfilled_fields = constructor_type;
6921 /* At the end of an implicit or explicit brace level,
6922 finish up that level of constructor. If a single expression
6923 with redundant braces initialized that level, return the
6924 c_expr structure for that expression. Otherwise, the original_code
6925 element is set to ERROR_MARK.
6926 If we were outputting the elements as they are read, return 0 as the value
6927 from inner levels (process_init_element ignores that),
6928 but return error_mark_node as the value from the outermost level
6929 (that's what we want to put in DECL_INITIAL).
6930 Otherwise, return a CONSTRUCTOR expression as the value. */
6932 struct c_expr
6933 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6935 struct constructor_stack *p;
6936 struct c_expr ret;
6937 ret.value = 0;
6938 ret.original_code = ERROR_MARK;
6939 ret.original_type = NULL;
6941 if (implicit == 0)
6943 /* When we come to an explicit close brace,
6944 pop any inner levels that didn't have explicit braces. */
6945 while (constructor_stack->implicit)
6947 process_init_element (pop_init_level (1, braced_init_obstack),
6948 true, braced_init_obstack);
6950 gcc_assert (!constructor_range_stack);
6953 /* Now output all pending elements. */
6954 constructor_incremental = 1;
6955 output_pending_init_elements (1, braced_init_obstack);
6957 p = constructor_stack;
6959 /* Error for initializing a flexible array member, or a zero-length
6960 array member in an inappropriate context. */
6961 if (constructor_type && constructor_fields
6962 && TREE_CODE (constructor_type) == ARRAY_TYPE
6963 && TYPE_DOMAIN (constructor_type)
6964 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6966 /* Silently discard empty initializations. The parser will
6967 already have pedwarned for empty brackets. */
6968 if (integer_zerop (constructor_unfilled_index))
6969 constructor_type = NULL_TREE;
6970 else
6972 gcc_assert (!TYPE_SIZE (constructor_type));
6974 if (constructor_depth > 2)
6975 error_init ("initialization of flexible array member in a nested context");
6976 else
6977 pedwarn_init (input_location, OPT_pedantic,
6978 "initialization of a flexible array member");
6980 /* We have already issued an error message for the existence
6981 of a flexible array member not at the end of the structure.
6982 Discard the initializer so that we do not die later. */
6983 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
6984 constructor_type = NULL_TREE;
6988 /* Warn when some struct elements are implicitly initialized to zero. */
6989 if (warn_missing_field_initializers
6990 && constructor_type
6991 && TREE_CODE (constructor_type) == RECORD_TYPE
6992 && constructor_unfilled_fields)
6994 /* Do not warn for flexible array members or zero-length arrays. */
6995 while (constructor_unfilled_fields
6996 && (!DECL_SIZE (constructor_unfilled_fields)
6997 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6998 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7000 /* Do not warn if this level of the initializer uses member
7001 designators; it is likely to be deliberate. */
7002 if (constructor_unfilled_fields && !constructor_designated)
7004 push_member_name (constructor_unfilled_fields);
7005 warning_init (OPT_Wmissing_field_initializers,
7006 "missing initializer");
7007 RESTORE_SPELLING_DEPTH (constructor_depth);
7011 /* Pad out the end of the structure. */
7012 if (p->replacement_value.value)
7013 /* If this closes a superfluous brace pair,
7014 just pass out the element between them. */
7015 ret = p->replacement_value;
7016 else if (constructor_type == 0)
7018 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7019 && TREE_CODE (constructor_type) != UNION_TYPE
7020 && TREE_CODE (constructor_type) != ARRAY_TYPE
7021 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7023 /* A nonincremental scalar initializer--just return
7024 the element, after verifying there is just one. */
7025 if (VEC_empty (constructor_elt,constructor_elements))
7027 if (!constructor_erroneous)
7028 error_init ("empty scalar initializer");
7029 ret.value = error_mark_node;
7031 else if (VEC_length (constructor_elt,constructor_elements) != 1)
7033 error_init ("extra elements in scalar initializer");
7034 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7036 else
7037 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7039 else
7041 if (constructor_erroneous)
7042 ret.value = error_mark_node;
7043 else
7045 ret.value = build_constructor (constructor_type,
7046 constructor_elements);
7047 if (constructor_constant)
7048 TREE_CONSTANT (ret.value) = 1;
7049 if (constructor_constant && constructor_simple)
7050 TREE_STATIC (ret.value) = 1;
7051 if (constructor_nonconst)
7052 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7056 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7058 if (constructor_nonconst)
7059 ret.original_code = C_MAYBE_CONST_EXPR;
7060 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7061 ret.original_code = ERROR_MARK;
7064 constructor_type = p->type;
7065 constructor_fields = p->fields;
7066 constructor_index = p->index;
7067 constructor_max_index = p->max_index;
7068 constructor_unfilled_index = p->unfilled_index;
7069 constructor_unfilled_fields = p->unfilled_fields;
7070 constructor_bit_index = p->bit_index;
7071 constructor_elements = p->elements;
7072 constructor_constant = p->constant;
7073 constructor_simple = p->simple;
7074 constructor_nonconst = p->nonconst;
7075 constructor_erroneous = p->erroneous;
7076 constructor_incremental = p->incremental;
7077 constructor_designated = p->designated;
7078 constructor_pending_elts = p->pending_elts;
7079 constructor_depth = p->depth;
7080 if (!p->implicit)
7081 constructor_range_stack = p->range_stack;
7082 RESTORE_SPELLING_DEPTH (constructor_depth);
7084 constructor_stack = p->next;
7085 free (p);
7087 if (ret.value == 0 && constructor_stack == 0)
7088 ret.value = error_mark_node;
7089 return ret;
7092 /* Common handling for both array range and field name designators.
7093 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7095 static int
7096 set_designator (int array, struct obstack * braced_init_obstack)
7098 tree subtype;
7099 enum tree_code subcode;
7101 /* Don't die if an entire brace-pair level is superfluous
7102 in the containing level. */
7103 if (constructor_type == 0)
7104 return 1;
7106 /* If there were errors in this designator list already, bail out
7107 silently. */
7108 if (designator_erroneous)
7109 return 1;
7111 if (!designator_depth)
7113 gcc_assert (!constructor_range_stack);
7115 /* Designator list starts at the level of closest explicit
7116 braces. */
7117 while (constructor_stack->implicit)
7119 process_init_element (pop_init_level (1, braced_init_obstack),
7120 true, braced_init_obstack);
7122 constructor_designated = 1;
7123 return 0;
7126 switch (TREE_CODE (constructor_type))
7128 case RECORD_TYPE:
7129 case UNION_TYPE:
7130 subtype = TREE_TYPE (constructor_fields);
7131 if (subtype != error_mark_node)
7132 subtype = TYPE_MAIN_VARIANT (subtype);
7133 break;
7134 case ARRAY_TYPE:
7135 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7136 break;
7137 default:
7138 gcc_unreachable ();
7141 subcode = TREE_CODE (subtype);
7142 if (array && subcode != ARRAY_TYPE)
7144 error_init ("array index in non-array initializer");
7145 return 1;
7147 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7149 error_init ("field name not in record or union initializer");
7150 return 1;
7153 constructor_designated = 1;
7154 push_init_level (2, braced_init_obstack);
7155 return 0;
7158 /* If there are range designators in designator list, push a new designator
7159 to constructor_range_stack. RANGE_END is end of such stack range or
7160 NULL_TREE if there is no range designator at this level. */
7162 static void
7163 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7165 struct constructor_range_stack *p;
7167 p = (struct constructor_range_stack *)
7168 obstack_alloc (braced_init_obstack,
7169 sizeof (struct constructor_range_stack));
7170 p->prev = constructor_range_stack;
7171 p->next = 0;
7172 p->fields = constructor_fields;
7173 p->range_start = constructor_index;
7174 p->index = constructor_index;
7175 p->stack = constructor_stack;
7176 p->range_end = range_end;
7177 if (constructor_range_stack)
7178 constructor_range_stack->next = p;
7179 constructor_range_stack = p;
7182 /* Within an array initializer, specify the next index to be initialized.
7183 FIRST is that index. If LAST is nonzero, then initialize a range
7184 of indices, running from FIRST through LAST. */
7186 void
7187 set_init_index (tree first, tree last,
7188 struct obstack * braced_init_obstack)
7190 if (set_designator (1, braced_init_obstack))
7191 return;
7193 designator_erroneous = 1;
7195 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7196 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7198 error_init ("array index in initializer not of integer type");
7199 return;
7202 if (TREE_CODE (first) != INTEGER_CST)
7204 first = c_fully_fold (first, false, NULL);
7205 if (TREE_CODE (first) == INTEGER_CST)
7206 pedwarn_init (input_location, OPT_pedantic,
7207 "array index in initializer is not "
7208 "an integer constant expression");
7211 if (last && TREE_CODE (last) != INTEGER_CST)
7213 last = c_fully_fold (last, false, NULL);
7214 if (TREE_CODE (last) == INTEGER_CST)
7215 pedwarn_init (input_location, OPT_pedantic,
7216 "array index in initializer is not "
7217 "an integer constant expression");
7220 if (TREE_CODE (first) != INTEGER_CST)
7221 error_init ("nonconstant array index in initializer");
7222 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7223 error_init ("nonconstant array index in initializer");
7224 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7225 error_init ("array index in non-array initializer");
7226 else if (tree_int_cst_sgn (first) == -1)
7227 error_init ("array index in initializer exceeds array bounds");
7228 else if (constructor_max_index
7229 && tree_int_cst_lt (constructor_max_index, first))
7230 error_init ("array index in initializer exceeds array bounds");
7231 else
7233 constant_expression_warning (first);
7234 if (last)
7235 constant_expression_warning (last);
7236 constructor_index = convert (bitsizetype, first);
7238 if (last)
7240 if (tree_int_cst_equal (first, last))
7241 last = 0;
7242 else if (tree_int_cst_lt (last, first))
7244 error_init ("empty index range in initializer");
7245 last = 0;
7247 else
7249 last = convert (bitsizetype, last);
7250 if (constructor_max_index != 0
7251 && tree_int_cst_lt (constructor_max_index, last))
7253 error_init ("array index range in initializer exceeds array bounds");
7254 last = 0;
7259 designator_depth++;
7260 designator_erroneous = 0;
7261 if (constructor_range_stack || last)
7262 push_range_stack (last, braced_init_obstack);
7266 /* Within a struct initializer, specify the next field to be initialized. */
7268 void
7269 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7271 tree field;
7273 if (set_designator (0, braced_init_obstack))
7274 return;
7276 designator_erroneous = 1;
7278 if (TREE_CODE (constructor_type) != RECORD_TYPE
7279 && TREE_CODE (constructor_type) != UNION_TYPE)
7281 error_init ("field name not in record or union initializer");
7282 return;
7285 field = lookup_field (constructor_type, fieldname);
7287 if (field == 0)
7288 error ("unknown field %qE specified in initializer", fieldname);
7289 else
7292 constructor_fields = TREE_VALUE (field);
7293 designator_depth++;
7294 designator_erroneous = 0;
7295 if (constructor_range_stack)
7296 push_range_stack (NULL_TREE, braced_init_obstack);
7297 field = TREE_CHAIN (field);
7298 if (field)
7300 if (set_designator (0, braced_init_obstack))
7301 return;
7304 while (field != NULL_TREE);
7307 /* Add a new initializer to the tree of pending initializers. PURPOSE
7308 identifies the initializer, either array index or field in a structure.
7309 VALUE is the value of that index or field. If ORIGTYPE is not
7310 NULL_TREE, it is the original type of VALUE.
7312 IMPLICIT is true if value comes from pop_init_level (1),
7313 the new initializer has been merged with the existing one
7314 and thus no warnings should be emitted about overriding an
7315 existing initializer. */
7317 static void
7318 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7319 struct obstack * braced_init_obstack)
7321 struct init_node *p, **q, *r;
7323 q = &constructor_pending_elts;
7324 p = 0;
7326 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7328 while (*q != 0)
7330 p = *q;
7331 if (tree_int_cst_lt (purpose, p->purpose))
7332 q = &p->left;
7333 else if (tree_int_cst_lt (p->purpose, purpose))
7334 q = &p->right;
7335 else
7337 if (!implicit)
7339 if (TREE_SIDE_EFFECTS (p->value))
7340 warning_init (0, "initialized field with side-effects overwritten");
7341 else if (warn_override_init)
7342 warning_init (OPT_Woverride_init, "initialized field overwritten");
7344 p->value = value;
7345 p->origtype = origtype;
7346 return;
7350 else
7352 tree bitpos;
7354 bitpos = bit_position (purpose);
7355 while (*q != NULL)
7357 p = *q;
7358 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7359 q = &p->left;
7360 else if (p->purpose != purpose)
7361 q = &p->right;
7362 else
7364 if (!implicit)
7366 if (TREE_SIDE_EFFECTS (p->value))
7367 warning_init (0, "initialized field with side-effects overwritten");
7368 else if (warn_override_init)
7369 warning_init (OPT_Woverride_init, "initialized field overwritten");
7371 p->value = value;
7372 p->origtype = origtype;
7373 return;
7378 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7379 sizeof (struct init_node));
7380 r->purpose = purpose;
7381 r->value = value;
7382 r->origtype = origtype;
7384 *q = r;
7385 r->parent = p;
7386 r->left = 0;
7387 r->right = 0;
7388 r->balance = 0;
7390 while (p)
7392 struct init_node *s;
7394 if (r == p->left)
7396 if (p->balance == 0)
7397 p->balance = -1;
7398 else if (p->balance < 0)
7400 if (r->balance < 0)
7402 /* L rotation. */
7403 p->left = r->right;
7404 if (p->left)
7405 p->left->parent = p;
7406 r->right = p;
7408 p->balance = 0;
7409 r->balance = 0;
7411 s = p->parent;
7412 p->parent = r;
7413 r->parent = s;
7414 if (s)
7416 if (s->left == p)
7417 s->left = r;
7418 else
7419 s->right = r;
7421 else
7422 constructor_pending_elts = r;
7424 else
7426 /* LR rotation. */
7427 struct init_node *t = r->right;
7429 r->right = t->left;
7430 if (r->right)
7431 r->right->parent = r;
7432 t->left = r;
7434 p->left = t->right;
7435 if (p->left)
7436 p->left->parent = p;
7437 t->right = p;
7439 p->balance = t->balance < 0;
7440 r->balance = -(t->balance > 0);
7441 t->balance = 0;
7443 s = p->parent;
7444 p->parent = t;
7445 r->parent = t;
7446 t->parent = s;
7447 if (s)
7449 if (s->left == p)
7450 s->left = t;
7451 else
7452 s->right = t;
7454 else
7455 constructor_pending_elts = t;
7457 break;
7459 else
7461 /* p->balance == +1; growth of left side balances the node. */
7462 p->balance = 0;
7463 break;
7466 else /* r == p->right */
7468 if (p->balance == 0)
7469 /* Growth propagation from right side. */
7470 p->balance++;
7471 else if (p->balance > 0)
7473 if (r->balance > 0)
7475 /* R rotation. */
7476 p->right = r->left;
7477 if (p->right)
7478 p->right->parent = p;
7479 r->left = p;
7481 p->balance = 0;
7482 r->balance = 0;
7484 s = p->parent;
7485 p->parent = r;
7486 r->parent = s;
7487 if (s)
7489 if (s->left == p)
7490 s->left = r;
7491 else
7492 s->right = r;
7494 else
7495 constructor_pending_elts = r;
7497 else /* r->balance == -1 */
7499 /* RL rotation */
7500 struct init_node *t = r->left;
7502 r->left = t->right;
7503 if (r->left)
7504 r->left->parent = r;
7505 t->right = r;
7507 p->right = t->left;
7508 if (p->right)
7509 p->right->parent = p;
7510 t->left = p;
7512 r->balance = (t->balance < 0);
7513 p->balance = -(t->balance > 0);
7514 t->balance = 0;
7516 s = p->parent;
7517 p->parent = t;
7518 r->parent = t;
7519 t->parent = s;
7520 if (s)
7522 if (s->left == p)
7523 s->left = t;
7524 else
7525 s->right = t;
7527 else
7528 constructor_pending_elts = t;
7530 break;
7532 else
7534 /* p->balance == -1; growth of right side balances the node. */
7535 p->balance = 0;
7536 break;
7540 r = p;
7541 p = p->parent;
7545 /* Build AVL tree from a sorted chain. */
7547 static void
7548 set_nonincremental_init (struct obstack * braced_init_obstack)
7550 unsigned HOST_WIDE_INT ix;
7551 tree index, value;
7553 if (TREE_CODE (constructor_type) != RECORD_TYPE
7554 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7555 return;
7557 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7559 add_pending_init (index, value, NULL_TREE, false,
7560 braced_init_obstack);
7562 constructor_elements = 0;
7563 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7565 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7566 /* Skip any nameless bit fields at the beginning. */
7567 while (constructor_unfilled_fields != 0
7568 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7569 && DECL_NAME (constructor_unfilled_fields) == 0)
7570 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7573 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7575 if (TYPE_DOMAIN (constructor_type))
7576 constructor_unfilled_index
7577 = convert (bitsizetype,
7578 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7579 else
7580 constructor_unfilled_index = bitsize_zero_node;
7582 constructor_incremental = 0;
7585 /* Build AVL tree from a string constant. */
7587 static void
7588 set_nonincremental_init_from_string (tree str,
7589 struct obstack * braced_init_obstack)
7591 tree value, purpose, type;
7592 HOST_WIDE_INT val[2];
7593 const char *p, *end;
7594 int byte, wchar_bytes, charwidth, bitpos;
7596 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7598 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7599 charwidth = TYPE_PRECISION (char_type_node);
7600 type = TREE_TYPE (constructor_type);
7601 p = TREE_STRING_POINTER (str);
7602 end = p + TREE_STRING_LENGTH (str);
7604 for (purpose = bitsize_zero_node;
7605 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7606 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7608 if (wchar_bytes == 1)
7610 val[1] = (unsigned char) *p++;
7611 val[0] = 0;
7613 else
7615 val[0] = 0;
7616 val[1] = 0;
7617 for (byte = 0; byte < wchar_bytes; byte++)
7619 if (BYTES_BIG_ENDIAN)
7620 bitpos = (wchar_bytes - byte - 1) * charwidth;
7621 else
7622 bitpos = byte * charwidth;
7623 val[bitpos < HOST_BITS_PER_WIDE_INT]
7624 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7625 << (bitpos % HOST_BITS_PER_WIDE_INT);
7629 if (!TYPE_UNSIGNED (type))
7631 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7632 if (bitpos < HOST_BITS_PER_WIDE_INT)
7634 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7636 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7637 val[0] = -1;
7640 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7642 if (val[1] < 0)
7643 val[0] = -1;
7645 else if (val[0] & (((HOST_WIDE_INT) 1)
7646 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7647 val[0] |= ((HOST_WIDE_INT) -1)
7648 << (bitpos - HOST_BITS_PER_WIDE_INT);
7651 value = build_int_cst_wide (type, val[1], val[0]);
7652 add_pending_init (purpose, value, NULL_TREE, false,
7653 braced_init_obstack);
7656 constructor_incremental = 0;
7659 /* Return value of FIELD in pending initializer or zero if the field was
7660 not initialized yet. */
7662 static tree
7663 find_init_member (tree field, struct obstack * braced_init_obstack)
7665 struct init_node *p;
7667 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7669 if (constructor_incremental
7670 && tree_int_cst_lt (field, constructor_unfilled_index))
7671 set_nonincremental_init (braced_init_obstack);
7673 p = constructor_pending_elts;
7674 while (p)
7676 if (tree_int_cst_lt (field, p->purpose))
7677 p = p->left;
7678 else if (tree_int_cst_lt (p->purpose, field))
7679 p = p->right;
7680 else
7681 return p->value;
7684 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7686 tree bitpos = bit_position (field);
7688 if (constructor_incremental
7689 && (!constructor_unfilled_fields
7690 || tree_int_cst_lt (bitpos,
7691 bit_position (constructor_unfilled_fields))))
7692 set_nonincremental_init (braced_init_obstack);
7694 p = constructor_pending_elts;
7695 while (p)
7697 if (field == p->purpose)
7698 return p->value;
7699 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7700 p = p->left;
7701 else
7702 p = p->right;
7705 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7707 if (!VEC_empty (constructor_elt, constructor_elements)
7708 && (VEC_last (constructor_elt, constructor_elements)->index
7709 == field))
7710 return VEC_last (constructor_elt, constructor_elements)->value;
7712 return 0;
7715 /* "Output" the next constructor element.
7716 At top level, really output it to assembler code now.
7717 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7718 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7719 TYPE is the data type that the containing data type wants here.
7720 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7721 If VALUE is a string constant, STRICT_STRING is true if it is
7722 unparenthesized or we should not warn here for it being parenthesized.
7723 For other types of VALUE, STRICT_STRING is not used.
7725 PENDING if non-nil means output pending elements that belong
7726 right after this element. (PENDING is normally 1;
7727 it is 0 while outputting pending elements, to avoid recursion.)
7729 IMPLICIT is true if value comes from pop_init_level (1),
7730 the new initializer has been merged with the existing one
7731 and thus no warnings should be emitted about overriding an
7732 existing initializer. */
7734 static void
7735 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7736 tree field, int pending, bool implicit,
7737 struct obstack * braced_init_obstack)
7739 tree semantic_type = NULL_TREE;
7740 constructor_elt *celt;
7741 bool maybe_const = true;
7742 bool npc;
7744 if (type == error_mark_node || value == error_mark_node)
7746 constructor_erroneous = 1;
7747 return;
7749 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7750 && (TREE_CODE (value) == STRING_CST
7751 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7752 && !(TREE_CODE (value) == STRING_CST
7753 && TREE_CODE (type) == ARRAY_TYPE
7754 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7755 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7756 TYPE_MAIN_VARIANT (type)))
7757 value = array_to_pointer_conversion (input_location, value);
7759 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7760 && require_constant_value && !flag_isoc99 && pending)
7762 /* As an extension, allow initializing objects with static storage
7763 duration with compound literals (which are then treated just as
7764 the brace enclosed list they contain). */
7765 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7766 value = DECL_INITIAL (decl);
7769 npc = null_pointer_constant_p (value);
7770 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7772 semantic_type = TREE_TYPE (value);
7773 value = TREE_OPERAND (value, 0);
7775 value = c_fully_fold (value, require_constant_value, &maybe_const);
7777 if (value == error_mark_node)
7778 constructor_erroneous = 1;
7779 else if (!TREE_CONSTANT (value))
7780 constructor_constant = 0;
7781 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7782 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7783 || TREE_CODE (constructor_type) == UNION_TYPE)
7784 && DECL_C_BIT_FIELD (field)
7785 && TREE_CODE (value) != INTEGER_CST))
7786 constructor_simple = 0;
7787 if (!maybe_const)
7788 constructor_nonconst = 1;
7790 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7792 if (require_constant_value)
7794 error_init ("initializer element is not constant");
7795 value = error_mark_node;
7797 else if (require_constant_elements)
7798 pedwarn (input_location, 0,
7799 "initializer element is not computable at load time");
7801 else if (!maybe_const
7802 && (require_constant_value || require_constant_elements))
7803 pedwarn_init (input_location, 0,
7804 "initializer element is not a constant expression");
7806 /* Issue -Wc++-compat warnings about initializing a bitfield with
7807 enum type. */
7808 if (warn_cxx_compat
7809 && field != NULL_TREE
7810 && TREE_CODE (field) == FIELD_DECL
7811 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7812 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7813 != TYPE_MAIN_VARIANT (type))
7814 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7816 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7817 if (checktype != error_mark_node
7818 && (TYPE_MAIN_VARIANT (checktype)
7819 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7820 warning_init (OPT_Wc___compat,
7821 "enum conversion in initialization is invalid in C++");
7824 /* If this field is empty (and not at the end of structure),
7825 don't do anything other than checking the initializer. */
7826 if (field
7827 && (TREE_TYPE (field) == error_mark_node
7828 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7829 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7830 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7831 || DECL_CHAIN (field)))))
7832 return;
7834 if (semantic_type)
7835 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7836 value = digest_init (input_location, type, value, origtype, npc,
7837 strict_string, require_constant_value);
7838 if (value == error_mark_node)
7840 constructor_erroneous = 1;
7841 return;
7843 if (require_constant_value || require_constant_elements)
7844 constant_expression_warning (value);
7846 /* If this element doesn't come next in sequence,
7847 put it on constructor_pending_elts. */
7848 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7849 && (!constructor_incremental
7850 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7852 if (constructor_incremental
7853 && tree_int_cst_lt (field, constructor_unfilled_index))
7854 set_nonincremental_init (braced_init_obstack);
7856 add_pending_init (field, value, origtype, implicit,
7857 braced_init_obstack);
7858 return;
7860 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7861 && (!constructor_incremental
7862 || field != constructor_unfilled_fields))
7864 /* We do this for records but not for unions. In a union,
7865 no matter which field is specified, it can be initialized
7866 right away since it starts at the beginning of the union. */
7867 if (constructor_incremental)
7869 if (!constructor_unfilled_fields)
7870 set_nonincremental_init (braced_init_obstack);
7871 else
7873 tree bitpos, unfillpos;
7875 bitpos = bit_position (field);
7876 unfillpos = bit_position (constructor_unfilled_fields);
7878 if (tree_int_cst_lt (bitpos, unfillpos))
7879 set_nonincremental_init (braced_init_obstack);
7883 add_pending_init (field, value, origtype, implicit,
7884 braced_init_obstack);
7885 return;
7887 else if (TREE_CODE (constructor_type) == UNION_TYPE
7888 && !VEC_empty (constructor_elt, constructor_elements))
7890 if (!implicit)
7892 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7893 constructor_elements)->value))
7894 warning_init (0,
7895 "initialized field with side-effects overwritten");
7896 else if (warn_override_init)
7897 warning_init (OPT_Woverride_init, "initialized field overwritten");
7900 /* We can have just one union field set. */
7901 constructor_elements = 0;
7904 /* Otherwise, output this element either to
7905 constructor_elements or to the assembler file. */
7907 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7908 celt->index = field;
7909 celt->value = value;
7911 /* Advance the variable that indicates sequential elements output. */
7912 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7913 constructor_unfilled_index
7914 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7915 bitsize_one_node);
7916 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7918 constructor_unfilled_fields
7919 = DECL_CHAIN (constructor_unfilled_fields);
7921 /* Skip any nameless bit fields. */
7922 while (constructor_unfilled_fields != 0
7923 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7924 && DECL_NAME (constructor_unfilled_fields) == 0)
7925 constructor_unfilled_fields =
7926 DECL_CHAIN (constructor_unfilled_fields);
7928 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7929 constructor_unfilled_fields = 0;
7931 /* Now output any pending elements which have become next. */
7932 if (pending)
7933 output_pending_init_elements (0, braced_init_obstack);
7936 /* Output any pending elements which have become next.
7937 As we output elements, constructor_unfilled_{fields,index}
7938 advances, which may cause other elements to become next;
7939 if so, they too are output.
7941 If ALL is 0, we return when there are
7942 no more pending elements to output now.
7944 If ALL is 1, we output space as necessary so that
7945 we can output all the pending elements. */
7946 static void
7947 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7949 struct init_node *elt = constructor_pending_elts;
7950 tree next;
7952 retry:
7954 /* Look through the whole pending tree.
7955 If we find an element that should be output now,
7956 output it. Otherwise, set NEXT to the element
7957 that comes first among those still pending. */
7959 next = 0;
7960 while (elt)
7962 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7964 if (tree_int_cst_equal (elt->purpose,
7965 constructor_unfilled_index))
7966 output_init_element (elt->value, elt->origtype, true,
7967 TREE_TYPE (constructor_type),
7968 constructor_unfilled_index, 0, false,
7969 braced_init_obstack);
7970 else if (tree_int_cst_lt (constructor_unfilled_index,
7971 elt->purpose))
7973 /* Advance to the next smaller node. */
7974 if (elt->left)
7975 elt = elt->left;
7976 else
7978 /* We have reached the smallest node bigger than the
7979 current unfilled index. Fill the space first. */
7980 next = elt->purpose;
7981 break;
7984 else
7986 /* Advance to the next bigger node. */
7987 if (elt->right)
7988 elt = elt->right;
7989 else
7991 /* We have reached the biggest node in a subtree. Find
7992 the parent of it, which is the next bigger node. */
7993 while (elt->parent && elt->parent->right == elt)
7994 elt = elt->parent;
7995 elt = elt->parent;
7996 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7997 elt->purpose))
7999 next = elt->purpose;
8000 break;
8005 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8006 || TREE_CODE (constructor_type) == UNION_TYPE)
8008 tree ctor_unfilled_bitpos, elt_bitpos;
8010 /* If the current record is complete we are done. */
8011 if (constructor_unfilled_fields == 0)
8012 break;
8014 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8015 elt_bitpos = bit_position (elt->purpose);
8016 /* We can't compare fields here because there might be empty
8017 fields in between. */
8018 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8020 constructor_unfilled_fields = elt->purpose;
8021 output_init_element (elt->value, elt->origtype, true,
8022 TREE_TYPE (elt->purpose),
8023 elt->purpose, 0, false,
8024 braced_init_obstack);
8026 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8028 /* Advance to the next smaller node. */
8029 if (elt->left)
8030 elt = elt->left;
8031 else
8033 /* We have reached the smallest node bigger than the
8034 current unfilled field. Fill the space first. */
8035 next = elt->purpose;
8036 break;
8039 else
8041 /* Advance to the next bigger node. */
8042 if (elt->right)
8043 elt = elt->right;
8044 else
8046 /* We have reached the biggest node in a subtree. Find
8047 the parent of it, which is the next bigger node. */
8048 while (elt->parent && elt->parent->right == elt)
8049 elt = elt->parent;
8050 elt = elt->parent;
8051 if (elt
8052 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8053 bit_position (elt->purpose))))
8055 next = elt->purpose;
8056 break;
8063 /* Ordinarily return, but not if we want to output all
8064 and there are elements left. */
8065 if (!(all && next != 0))
8066 return;
8068 /* If it's not incremental, just skip over the gap, so that after
8069 jumping to retry we will output the next successive element. */
8070 if (TREE_CODE (constructor_type) == RECORD_TYPE
8071 || TREE_CODE (constructor_type) == UNION_TYPE)
8072 constructor_unfilled_fields = next;
8073 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8074 constructor_unfilled_index = next;
8076 /* ELT now points to the node in the pending tree with the next
8077 initializer to output. */
8078 goto retry;
8081 /* Add one non-braced element to the current constructor level.
8082 This adjusts the current position within the constructor's type.
8083 This may also start or terminate implicit levels
8084 to handle a partly-braced initializer.
8086 Once this has found the correct level for the new element,
8087 it calls output_init_element.
8089 IMPLICIT is true if value comes from pop_init_level (1),
8090 the new initializer has been merged with the existing one
8091 and thus no warnings should be emitted about overriding an
8092 existing initializer. */
8094 void
8095 process_init_element (struct c_expr value, bool implicit,
8096 struct obstack * braced_init_obstack)
8098 tree orig_value = value.value;
8099 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8100 bool strict_string = value.original_code == STRING_CST;
8102 designator_depth = 0;
8103 designator_erroneous = 0;
8105 /* Handle superfluous braces around string cst as in
8106 char x[] = {"foo"}; */
8107 if (string_flag
8108 && constructor_type
8109 && TREE_CODE (constructor_type) == ARRAY_TYPE
8110 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8111 && integer_zerop (constructor_unfilled_index))
8113 if (constructor_stack->replacement_value.value)
8114 error_init ("excess elements in char array initializer");
8115 constructor_stack->replacement_value = value;
8116 return;
8119 if (constructor_stack->replacement_value.value != 0)
8121 error_init ("excess elements in struct initializer");
8122 return;
8125 /* Ignore elements of a brace group if it is entirely superfluous
8126 and has already been diagnosed. */
8127 if (constructor_type == 0)
8128 return;
8130 /* If we've exhausted any levels that didn't have braces,
8131 pop them now. */
8132 while (constructor_stack->implicit)
8134 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8135 || TREE_CODE (constructor_type) == UNION_TYPE)
8136 && constructor_fields == 0)
8137 process_init_element (pop_init_level (1, braced_init_obstack),
8138 true, braced_init_obstack);
8139 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8140 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8141 && (constructor_max_index == 0
8142 || tree_int_cst_lt (constructor_max_index,
8143 constructor_index)))
8144 process_init_element (pop_init_level (1, braced_init_obstack),
8145 true, braced_init_obstack);
8146 else
8147 break;
8150 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8151 if (constructor_range_stack)
8153 /* If value is a compound literal and we'll be just using its
8154 content, don't put it into a SAVE_EXPR. */
8155 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8156 || !require_constant_value
8157 || flag_isoc99)
8159 tree semantic_type = NULL_TREE;
8160 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8162 semantic_type = TREE_TYPE (value.value);
8163 value.value = TREE_OPERAND (value.value, 0);
8165 value.value = c_save_expr (value.value);
8166 if (semantic_type)
8167 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8168 value.value);
8172 while (1)
8174 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8176 tree fieldtype;
8177 enum tree_code fieldcode;
8179 if (constructor_fields == 0)
8181 pedwarn_init (input_location, 0,
8182 "excess elements in struct initializer");
8183 break;
8186 fieldtype = TREE_TYPE (constructor_fields);
8187 if (fieldtype != error_mark_node)
8188 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8189 fieldcode = TREE_CODE (fieldtype);
8191 /* Error for non-static initialization of a flexible array member. */
8192 if (fieldcode == ARRAY_TYPE
8193 && !require_constant_value
8194 && TYPE_SIZE (fieldtype) == NULL_TREE
8195 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8197 error_init ("non-static initialization of a flexible array member");
8198 break;
8201 /* Accept a string constant to initialize a subarray. */
8202 if (value.value != 0
8203 && fieldcode == ARRAY_TYPE
8204 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8205 && string_flag)
8206 value.value = orig_value;
8207 /* Otherwise, if we have come to a subaggregate,
8208 and we don't have an element of its type, push into it. */
8209 else if (value.value != 0
8210 && value.value != error_mark_node
8211 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8212 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8213 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8215 push_init_level (1, braced_init_obstack);
8216 continue;
8219 if (value.value)
8221 push_member_name (constructor_fields);
8222 output_init_element (value.value, value.original_type,
8223 strict_string, fieldtype,
8224 constructor_fields, 1, implicit,
8225 braced_init_obstack);
8226 RESTORE_SPELLING_DEPTH (constructor_depth);
8228 else
8229 /* Do the bookkeeping for an element that was
8230 directly output as a constructor. */
8232 /* For a record, keep track of end position of last field. */
8233 if (DECL_SIZE (constructor_fields))
8234 constructor_bit_index
8235 = size_binop_loc (input_location, PLUS_EXPR,
8236 bit_position (constructor_fields),
8237 DECL_SIZE (constructor_fields));
8239 /* If the current field was the first one not yet written out,
8240 it isn't now, so update. */
8241 if (constructor_unfilled_fields == constructor_fields)
8243 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8244 /* Skip any nameless bit fields. */
8245 while (constructor_unfilled_fields != 0
8246 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8247 && DECL_NAME (constructor_unfilled_fields) == 0)
8248 constructor_unfilled_fields =
8249 DECL_CHAIN (constructor_unfilled_fields);
8253 constructor_fields = DECL_CHAIN (constructor_fields);
8254 /* Skip any nameless bit fields at the beginning. */
8255 while (constructor_fields != 0
8256 && DECL_C_BIT_FIELD (constructor_fields)
8257 && DECL_NAME (constructor_fields) == 0)
8258 constructor_fields = DECL_CHAIN (constructor_fields);
8260 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8262 tree fieldtype;
8263 enum tree_code fieldcode;
8265 if (constructor_fields == 0)
8267 pedwarn_init (input_location, 0,
8268 "excess elements in union initializer");
8269 break;
8272 fieldtype = TREE_TYPE (constructor_fields);
8273 if (fieldtype != error_mark_node)
8274 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8275 fieldcode = TREE_CODE (fieldtype);
8277 /* Warn that traditional C rejects initialization of unions.
8278 We skip the warning if the value is zero. This is done
8279 under the assumption that the zero initializer in user
8280 code appears conditioned on e.g. __STDC__ to avoid
8281 "missing initializer" warnings and relies on default
8282 initialization to zero in the traditional C case.
8283 We also skip the warning if the initializer is designated,
8284 again on the assumption that this must be conditional on
8285 __STDC__ anyway (and we've already complained about the
8286 member-designator already). */
8287 if (!in_system_header && !constructor_designated
8288 && !(value.value && (integer_zerop (value.value)
8289 || real_zerop (value.value))))
8290 warning (OPT_Wtraditional, "traditional C rejects initialization "
8291 "of unions");
8293 /* Accept a string constant to initialize a subarray. */
8294 if (value.value != 0
8295 && fieldcode == ARRAY_TYPE
8296 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8297 && string_flag)
8298 value.value = orig_value;
8299 /* Otherwise, if we have come to a subaggregate,
8300 and we don't have an element of its type, push into it. */
8301 else if (value.value != 0
8302 && value.value != error_mark_node
8303 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8304 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8305 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8307 push_init_level (1, braced_init_obstack);
8308 continue;
8311 if (value.value)
8313 push_member_name (constructor_fields);
8314 output_init_element (value.value, value.original_type,
8315 strict_string, fieldtype,
8316 constructor_fields, 1, implicit,
8317 braced_init_obstack);
8318 RESTORE_SPELLING_DEPTH (constructor_depth);
8320 else
8321 /* Do the bookkeeping for an element that was
8322 directly output as a constructor. */
8324 constructor_bit_index = DECL_SIZE (constructor_fields);
8325 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8328 constructor_fields = 0;
8330 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8332 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8333 enum tree_code eltcode = TREE_CODE (elttype);
8335 /* Accept a string constant to initialize a subarray. */
8336 if (value.value != 0
8337 && eltcode == ARRAY_TYPE
8338 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8339 && string_flag)
8340 value.value = orig_value;
8341 /* Otherwise, if we have come to a subaggregate,
8342 and we don't have an element of its type, push into it. */
8343 else if (value.value != 0
8344 && value.value != error_mark_node
8345 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8346 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8347 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8349 push_init_level (1, braced_init_obstack);
8350 continue;
8353 if (constructor_max_index != 0
8354 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8355 || integer_all_onesp (constructor_max_index)))
8357 pedwarn_init (input_location, 0,
8358 "excess elements in array initializer");
8359 break;
8362 /* Now output the actual element. */
8363 if (value.value)
8365 push_array_bounds (tree_low_cst (constructor_index, 1));
8366 output_init_element (value.value, value.original_type,
8367 strict_string, elttype,
8368 constructor_index, 1, implicit,
8369 braced_init_obstack);
8370 RESTORE_SPELLING_DEPTH (constructor_depth);
8373 constructor_index
8374 = size_binop_loc (input_location, PLUS_EXPR,
8375 constructor_index, bitsize_one_node);
8377 if (!value.value)
8378 /* If we are doing the bookkeeping for an element that was
8379 directly output as a constructor, we must update
8380 constructor_unfilled_index. */
8381 constructor_unfilled_index = constructor_index;
8383 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8385 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8387 /* Do a basic check of initializer size. Note that vectors
8388 always have a fixed size derived from their type. */
8389 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8391 pedwarn_init (input_location, 0,
8392 "excess elements in vector initializer");
8393 break;
8396 /* Now output the actual element. */
8397 if (value.value)
8399 if (TREE_CODE (value.value) == VECTOR_CST)
8400 elttype = TYPE_MAIN_VARIANT (constructor_type);
8401 output_init_element (value.value, value.original_type,
8402 strict_string, elttype,
8403 constructor_index, 1, implicit,
8404 braced_init_obstack);
8407 constructor_index
8408 = size_binop_loc (input_location,
8409 PLUS_EXPR, constructor_index, bitsize_one_node);
8411 if (!value.value)
8412 /* If we are doing the bookkeeping for an element that was
8413 directly output as a constructor, we must update
8414 constructor_unfilled_index. */
8415 constructor_unfilled_index = constructor_index;
8418 /* Handle the sole element allowed in a braced initializer
8419 for a scalar variable. */
8420 else if (constructor_type != error_mark_node
8421 && constructor_fields == 0)
8423 pedwarn_init (input_location, 0,
8424 "excess elements in scalar initializer");
8425 break;
8427 else
8429 if (value.value)
8430 output_init_element (value.value, value.original_type,
8431 strict_string, constructor_type,
8432 NULL_TREE, 1, implicit,
8433 braced_init_obstack);
8434 constructor_fields = 0;
8437 /* Handle range initializers either at this level or anywhere higher
8438 in the designator stack. */
8439 if (constructor_range_stack)
8441 struct constructor_range_stack *p, *range_stack;
8442 int finish = 0;
8444 range_stack = constructor_range_stack;
8445 constructor_range_stack = 0;
8446 while (constructor_stack != range_stack->stack)
8448 gcc_assert (constructor_stack->implicit);
8449 process_init_element (pop_init_level (1,
8450 braced_init_obstack),
8451 true, braced_init_obstack);
8453 for (p = range_stack;
8454 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8455 p = p->prev)
8457 gcc_assert (constructor_stack->implicit);
8458 process_init_element (pop_init_level (1, braced_init_obstack),
8459 true, braced_init_obstack);
8462 p->index = size_binop_loc (input_location,
8463 PLUS_EXPR, p->index, bitsize_one_node);
8464 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8465 finish = 1;
8467 while (1)
8469 constructor_index = p->index;
8470 constructor_fields = p->fields;
8471 if (finish && p->range_end && p->index == p->range_start)
8473 finish = 0;
8474 p->prev = 0;
8476 p = p->next;
8477 if (!p)
8478 break;
8479 push_init_level (2, braced_init_obstack);
8480 p->stack = constructor_stack;
8481 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8482 p->index = p->range_start;
8485 if (!finish)
8486 constructor_range_stack = range_stack;
8487 continue;
8490 break;
8493 constructor_range_stack = 0;
8496 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8497 (guaranteed to be 'volatile' or null) and ARGS (represented using
8498 an ASM_EXPR node). */
8499 tree
8500 build_asm_stmt (tree cv_qualifier, tree args)
8502 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8503 ASM_VOLATILE_P (args) = 1;
8504 return add_stmt (args);
8507 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8508 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8509 SIMPLE indicates whether there was anything at all after the
8510 string in the asm expression -- asm("blah") and asm("blah" : )
8511 are subtly different. We use a ASM_EXPR node to represent this. */
8512 tree
8513 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8514 tree clobbers, tree labels, bool simple)
8516 tree tail;
8517 tree args;
8518 int i;
8519 const char *constraint;
8520 const char **oconstraints;
8521 bool allows_mem, allows_reg, is_inout;
8522 int ninputs, noutputs;
8524 ninputs = list_length (inputs);
8525 noutputs = list_length (outputs);
8526 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8528 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8530 /* Remove output conversions that change the type but not the mode. */
8531 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8533 tree output = TREE_VALUE (tail);
8535 /* ??? Really, this should not be here. Users should be using a
8536 proper lvalue, dammit. But there's a long history of using casts
8537 in the output operands. In cases like longlong.h, this becomes a
8538 primitive form of typechecking -- if the cast can be removed, then
8539 the output operand had a type of the proper width; otherwise we'll
8540 get an error. Gross, but ... */
8541 STRIP_NOPS (output);
8543 if (!lvalue_or_else (output, lv_asm))
8544 output = error_mark_node;
8546 if (output != error_mark_node
8547 && (TREE_READONLY (output)
8548 || TYPE_READONLY (TREE_TYPE (output))
8549 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8550 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8551 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8552 readonly_error (output, lv_asm);
8554 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8555 oconstraints[i] = constraint;
8557 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8558 &allows_mem, &allows_reg, &is_inout))
8560 /* If the operand is going to end up in memory,
8561 mark it addressable. */
8562 if (!allows_reg && !c_mark_addressable (output))
8563 output = error_mark_node;
8565 else
8566 output = error_mark_node;
8568 TREE_VALUE (tail) = output;
8571 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8573 tree input;
8575 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8576 input = TREE_VALUE (tail);
8578 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8579 oconstraints, &allows_mem, &allows_reg))
8581 /* If the operand is going to end up in memory,
8582 mark it addressable. */
8583 if (!allows_reg && allows_mem)
8585 /* Strip the nops as we allow this case. FIXME, this really
8586 should be rejected or made deprecated. */
8587 STRIP_NOPS (input);
8588 if (!c_mark_addressable (input))
8589 input = error_mark_node;
8592 else
8593 input = error_mark_node;
8595 TREE_VALUE (tail) = input;
8598 /* ASMs with labels cannot have outputs. This should have been
8599 enforced by the parser. */
8600 gcc_assert (outputs == NULL || labels == NULL);
8602 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8604 /* asm statements without outputs, including simple ones, are treated
8605 as volatile. */
8606 ASM_INPUT_P (args) = simple;
8607 ASM_VOLATILE_P (args) = (noutputs == 0);
8609 return args;
8612 /* Generate a goto statement to LABEL. LOC is the location of the
8613 GOTO. */
8615 tree
8616 c_finish_goto_label (location_t loc, tree label)
8618 tree decl = lookup_label_for_goto (loc, label);
8619 if (!decl)
8620 return NULL_TREE;
8621 TREE_USED (decl) = 1;
8623 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8624 SET_EXPR_LOCATION (t, loc);
8625 return add_stmt (t);
8629 /* Generate a computed goto statement to EXPR. LOC is the location of
8630 the GOTO. */
8632 tree
8633 c_finish_goto_ptr (location_t loc, tree expr)
8635 tree t;
8636 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8637 expr = c_fully_fold (expr, false, NULL);
8638 expr = convert (ptr_type_node, expr);
8639 t = build1 (GOTO_EXPR, void_type_node, expr);
8640 SET_EXPR_LOCATION (t, loc);
8641 return add_stmt (t);
8644 /* Generate a C `return' statement. RETVAL is the expression for what
8645 to return, or a null pointer for `return;' with no value. LOC is
8646 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8647 is the original type of RETVAL. */
8649 tree
8650 c_finish_return (location_t loc, tree retval, tree origtype)
8652 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8653 bool no_warning = false;
8654 bool npc = false;
8656 if (TREE_THIS_VOLATILE (current_function_decl))
8657 warning_at (loc, 0,
8658 "function declared %<noreturn%> has a %<return%> statement");
8660 if (retval)
8662 tree semantic_type = NULL_TREE;
8663 npc = null_pointer_constant_p (retval);
8664 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8666 semantic_type = TREE_TYPE (retval);
8667 retval = TREE_OPERAND (retval, 0);
8669 retval = c_fully_fold (retval, false, NULL);
8670 if (semantic_type)
8671 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8674 if (!retval)
8676 current_function_returns_null = 1;
8677 if ((warn_return_type || flag_isoc99)
8678 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8680 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8681 "%<return%> with no value, in "
8682 "function returning non-void");
8683 no_warning = true;
8686 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8688 current_function_returns_null = 1;
8689 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8690 pedwarn (loc, 0,
8691 "%<return%> with a value, in function returning void");
8692 else
8693 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8694 "%<return%> with expression, in function returning void");
8696 else
8698 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8699 ic_return,
8700 npc, NULL_TREE, NULL_TREE, 0);
8701 tree res = DECL_RESULT (current_function_decl);
8702 tree inner;
8704 current_function_returns_value = 1;
8705 if (t == error_mark_node)
8706 return NULL_TREE;
8708 inner = t = convert (TREE_TYPE (res), t);
8710 /* Strip any conversions, additions, and subtractions, and see if
8711 we are returning the address of a local variable. Warn if so. */
8712 while (1)
8714 switch (TREE_CODE (inner))
8716 CASE_CONVERT:
8717 case NON_LVALUE_EXPR:
8718 case PLUS_EXPR:
8719 case POINTER_PLUS_EXPR:
8720 inner = TREE_OPERAND (inner, 0);
8721 continue;
8723 case MINUS_EXPR:
8724 /* If the second operand of the MINUS_EXPR has a pointer
8725 type (or is converted from it), this may be valid, so
8726 don't give a warning. */
8728 tree op1 = TREE_OPERAND (inner, 1);
8730 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8731 && (CONVERT_EXPR_P (op1)
8732 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8733 op1 = TREE_OPERAND (op1, 0);
8735 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8736 break;
8738 inner = TREE_OPERAND (inner, 0);
8739 continue;
8742 case ADDR_EXPR:
8743 inner = TREE_OPERAND (inner, 0);
8745 while (REFERENCE_CLASS_P (inner)
8746 && TREE_CODE (inner) != INDIRECT_REF)
8747 inner = TREE_OPERAND (inner, 0);
8749 if (DECL_P (inner)
8750 && !DECL_EXTERNAL (inner)
8751 && !TREE_STATIC (inner)
8752 && DECL_CONTEXT (inner) == current_function_decl)
8753 warning_at (loc,
8754 0, "function returns address of local variable");
8755 break;
8757 default:
8758 break;
8761 break;
8764 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8765 SET_EXPR_LOCATION (retval, loc);
8767 if (warn_sequence_point)
8768 verify_sequence_points (retval);
8771 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8772 TREE_NO_WARNING (ret_stmt) |= no_warning;
8773 return add_stmt (ret_stmt);
8776 struct c_switch {
8777 /* The SWITCH_EXPR being built. */
8778 tree switch_expr;
8780 /* The original type of the testing expression, i.e. before the
8781 default conversion is applied. */
8782 tree orig_type;
8784 /* A splay-tree mapping the low element of a case range to the high
8785 element, or NULL_TREE if there is no high element. Used to
8786 determine whether or not a new case label duplicates an old case
8787 label. We need a tree, rather than simply a hash table, because
8788 of the GNU case range extension. */
8789 splay_tree cases;
8791 /* The bindings at the point of the switch. This is used for
8792 warnings crossing decls when branching to a case label. */
8793 struct c_spot_bindings *bindings;
8795 /* The next node on the stack. */
8796 struct c_switch *next;
8799 /* A stack of the currently active switch statements. The innermost
8800 switch statement is on the top of the stack. There is no need to
8801 mark the stack for garbage collection because it is only active
8802 during the processing of the body of a function, and we never
8803 collect at that point. */
8805 struct c_switch *c_switch_stack;
8807 /* Start a C switch statement, testing expression EXP. Return the new
8808 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8809 SWITCH_COND_LOC is the location of the switch's condition. */
8811 tree
8812 c_start_case (location_t switch_loc,
8813 location_t switch_cond_loc,
8814 tree exp)
8816 tree orig_type = error_mark_node;
8817 struct c_switch *cs;
8819 if (exp != error_mark_node)
8821 orig_type = TREE_TYPE (exp);
8823 if (!INTEGRAL_TYPE_P (orig_type))
8825 if (orig_type != error_mark_node)
8827 error_at (switch_cond_loc, "switch quantity not an integer");
8828 orig_type = error_mark_node;
8830 exp = integer_zero_node;
8832 else
8834 tree type = TYPE_MAIN_VARIANT (orig_type);
8836 if (!in_system_header
8837 && (type == long_integer_type_node
8838 || type == long_unsigned_type_node))
8839 warning_at (switch_cond_loc,
8840 OPT_Wtraditional, "%<long%> switch expression not "
8841 "converted to %<int%> in ISO C");
8843 exp = c_fully_fold (exp, false, NULL);
8844 exp = default_conversion (exp);
8846 if (warn_sequence_point)
8847 verify_sequence_points (exp);
8851 /* Add this new SWITCH_EXPR to the stack. */
8852 cs = XNEW (struct c_switch);
8853 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8854 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8855 cs->orig_type = orig_type;
8856 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8857 cs->bindings = c_get_switch_bindings ();
8858 cs->next = c_switch_stack;
8859 c_switch_stack = cs;
8861 return add_stmt (cs->switch_expr);
8864 /* Process a case label at location LOC. */
8866 tree
8867 do_case (location_t loc, tree low_value, tree high_value)
8869 tree label = NULL_TREE;
8871 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8873 low_value = c_fully_fold (low_value, false, NULL);
8874 if (TREE_CODE (low_value) == INTEGER_CST)
8875 pedwarn (input_location, OPT_pedantic,
8876 "case label is not an integer constant expression");
8879 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8881 high_value = c_fully_fold (high_value, false, NULL);
8882 if (TREE_CODE (high_value) == INTEGER_CST)
8883 pedwarn (input_location, OPT_pedantic,
8884 "case label is not an integer constant expression");
8887 if (c_switch_stack == NULL)
8889 if (low_value)
8890 error_at (loc, "case label not within a switch statement");
8891 else
8892 error_at (loc, "%<default%> label not within a switch statement");
8893 return NULL_TREE;
8896 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8897 EXPR_LOCATION (c_switch_stack->switch_expr),
8898 loc))
8899 return NULL_TREE;
8901 label = c_add_case_label (loc, c_switch_stack->cases,
8902 SWITCH_COND (c_switch_stack->switch_expr),
8903 c_switch_stack->orig_type,
8904 low_value, high_value);
8905 if (label == error_mark_node)
8906 label = NULL_TREE;
8907 return label;
8910 /* Finish the switch statement. */
8912 void
8913 c_finish_case (tree body)
8915 struct c_switch *cs = c_switch_stack;
8916 location_t switch_location;
8918 SWITCH_BODY (cs->switch_expr) = body;
8920 /* Emit warnings as needed. */
8921 switch_location = EXPR_LOCATION (cs->switch_expr);
8922 c_do_switch_warnings (cs->cases, switch_location,
8923 TREE_TYPE (cs->switch_expr),
8924 SWITCH_COND (cs->switch_expr));
8926 /* Pop the stack. */
8927 c_switch_stack = cs->next;
8928 splay_tree_delete (cs->cases);
8929 c_release_switch_bindings (cs->bindings);
8930 XDELETE (cs);
8933 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8934 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8935 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8936 statement, and was not surrounded with parenthesis. */
8938 void
8939 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8940 tree else_block, bool nested_if)
8942 tree stmt;
8944 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8945 if (warn_parentheses && nested_if && else_block == NULL)
8947 tree inner_if = then_block;
8949 /* We know from the grammar productions that there is an IF nested
8950 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8951 it might not be exactly THEN_BLOCK, but should be the last
8952 non-container statement within. */
8953 while (1)
8954 switch (TREE_CODE (inner_if))
8956 case COND_EXPR:
8957 goto found;
8958 case BIND_EXPR:
8959 inner_if = BIND_EXPR_BODY (inner_if);
8960 break;
8961 case STATEMENT_LIST:
8962 inner_if = expr_last (then_block);
8963 break;
8964 case TRY_FINALLY_EXPR:
8965 case TRY_CATCH_EXPR:
8966 inner_if = TREE_OPERAND (inner_if, 0);
8967 break;
8968 default:
8969 gcc_unreachable ();
8971 found:
8973 if (COND_EXPR_ELSE (inner_if))
8974 warning_at (if_locus, OPT_Wparentheses,
8975 "suggest explicit braces to avoid ambiguous %<else%>");
8978 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8979 SET_EXPR_LOCATION (stmt, if_locus);
8980 add_stmt (stmt);
8983 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8984 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8985 is false for DO loops. INCR is the FOR increment expression. BODY is
8986 the statement controlled by the loop. BLAB is the break label. CLAB is
8987 the continue label. Everything is allowed to be NULL. */
8989 void
8990 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8991 tree blab, tree clab, bool cond_is_first)
8993 tree entry = NULL, exit = NULL, t;
8995 /* If the condition is zero don't generate a loop construct. */
8996 if (cond && integer_zerop (cond))
8998 if (cond_is_first)
9000 t = build_and_jump (&blab);
9001 SET_EXPR_LOCATION (t, start_locus);
9002 add_stmt (t);
9005 else
9007 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9009 /* If we have an exit condition, then we build an IF with gotos either
9010 out of the loop, or to the top of it. If there's no exit condition,
9011 then we just build a jump back to the top. */
9012 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9014 if (cond && !integer_nonzerop (cond))
9016 /* Canonicalize the loop condition to the end. This means
9017 generating a branch to the loop condition. Reuse the
9018 continue label, if possible. */
9019 if (cond_is_first)
9021 if (incr || !clab)
9023 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9024 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9026 else
9027 t = build1 (GOTO_EXPR, void_type_node, clab);
9028 SET_EXPR_LOCATION (t, start_locus);
9029 add_stmt (t);
9032 t = build_and_jump (&blab);
9033 if (cond_is_first)
9034 exit = fold_build3_loc (start_locus,
9035 COND_EXPR, void_type_node, cond, exit, t);
9036 else
9037 exit = fold_build3_loc (input_location,
9038 COND_EXPR, void_type_node, cond, exit, t);
9041 add_stmt (top);
9044 if (body)
9045 add_stmt (body);
9046 if (clab)
9047 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9048 if (incr)
9049 add_stmt (incr);
9050 if (entry)
9051 add_stmt (entry);
9052 if (exit)
9053 add_stmt (exit);
9054 if (blab)
9055 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9058 tree
9059 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9061 bool skip;
9062 tree label = *label_p;
9064 /* In switch statements break is sometimes stylistically used after
9065 a return statement. This can lead to spurious warnings about
9066 control reaching the end of a non-void function when it is
9067 inlined. Note that we are calling block_may_fallthru with
9068 language specific tree nodes; this works because
9069 block_may_fallthru returns true when given something it does not
9070 understand. */
9071 skip = !block_may_fallthru (cur_stmt_list);
9073 if (!label)
9075 if (!skip)
9076 *label_p = label = create_artificial_label (loc);
9078 else if (TREE_CODE (label) == LABEL_DECL)
9080 else switch (TREE_INT_CST_LOW (label))
9082 case 0:
9083 if (is_break)
9084 error_at (loc, "break statement not within loop or switch");
9085 else
9086 error_at (loc, "continue statement not within a loop");
9087 return NULL_TREE;
9089 case 1:
9090 gcc_assert (is_break);
9091 error_at (loc, "break statement used with OpenMP for loop");
9092 return NULL_TREE;
9094 default:
9095 gcc_unreachable ();
9098 if (skip)
9099 return NULL_TREE;
9101 if (!is_break)
9102 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9104 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9107 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9109 static void
9110 emit_side_effect_warnings (location_t loc, tree expr)
9112 if (expr == error_mark_node)
9114 else if (!TREE_SIDE_EFFECTS (expr))
9116 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9117 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9119 else
9120 warn_if_unused_value (expr, loc);
9123 /* Process an expression as if it were a complete statement. Emit
9124 diagnostics, but do not call ADD_STMT. LOC is the location of the
9125 statement. */
9127 tree
9128 c_process_expr_stmt (location_t loc, tree expr)
9130 tree exprv;
9132 if (!expr)
9133 return NULL_TREE;
9135 expr = c_fully_fold (expr, false, NULL);
9137 if (warn_sequence_point)
9138 verify_sequence_points (expr);
9140 if (TREE_TYPE (expr) != error_mark_node
9141 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9142 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9143 error_at (loc, "expression statement has incomplete type");
9145 /* If we're not processing a statement expression, warn about unused values.
9146 Warnings for statement expressions will be emitted later, once we figure
9147 out which is the result. */
9148 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9149 && warn_unused_value)
9150 emit_side_effect_warnings (loc, expr);
9152 exprv = expr;
9153 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9154 exprv = TREE_OPERAND (exprv, 1);
9155 if (DECL_P (exprv) || handled_component_p (exprv))
9156 mark_exp_read (exprv);
9158 /* If the expression is not of a type to which we cannot assign a line
9159 number, wrap the thing in a no-op NOP_EXPR. */
9160 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9162 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9163 SET_EXPR_LOCATION (expr, loc);
9166 return expr;
9169 /* Emit an expression as a statement. LOC is the location of the
9170 expression. */
9172 tree
9173 c_finish_expr_stmt (location_t loc, tree expr)
9175 if (expr)
9176 return add_stmt (c_process_expr_stmt (loc, expr));
9177 else
9178 return NULL;
9181 /* Do the opposite and emit a statement as an expression. To begin,
9182 create a new binding level and return it. */
9184 tree
9185 c_begin_stmt_expr (void)
9187 tree ret;
9189 /* We must force a BLOCK for this level so that, if it is not expanded
9190 later, there is a way to turn off the entire subtree of blocks that
9191 are contained in it. */
9192 keep_next_level ();
9193 ret = c_begin_compound_stmt (true);
9195 c_bindings_start_stmt_expr (c_switch_stack == NULL
9196 ? NULL
9197 : c_switch_stack->bindings);
9199 /* Mark the current statement list as belonging to a statement list. */
9200 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9202 return ret;
9205 /* LOC is the location of the compound statement to which this body
9206 belongs. */
9208 tree
9209 c_finish_stmt_expr (location_t loc, tree body)
9211 tree last, type, tmp, val;
9212 tree *last_p;
9214 body = c_end_compound_stmt (loc, body, true);
9216 c_bindings_end_stmt_expr (c_switch_stack == NULL
9217 ? NULL
9218 : c_switch_stack->bindings);
9220 /* Locate the last statement in BODY. See c_end_compound_stmt
9221 about always returning a BIND_EXPR. */
9222 last_p = &BIND_EXPR_BODY (body);
9223 last = BIND_EXPR_BODY (body);
9225 continue_searching:
9226 if (TREE_CODE (last) == STATEMENT_LIST)
9228 tree_stmt_iterator i;
9230 /* This can happen with degenerate cases like ({ }). No value. */
9231 if (!TREE_SIDE_EFFECTS (last))
9232 return body;
9234 /* If we're supposed to generate side effects warnings, process
9235 all of the statements except the last. */
9236 if (warn_unused_value)
9238 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9240 location_t tloc;
9241 tree t = tsi_stmt (i);
9243 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9244 emit_side_effect_warnings (tloc, t);
9247 else
9248 i = tsi_last (last);
9249 last_p = tsi_stmt_ptr (i);
9250 last = *last_p;
9253 /* If the end of the list is exception related, then the list was split
9254 by a call to push_cleanup. Continue searching. */
9255 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9256 || TREE_CODE (last) == TRY_CATCH_EXPR)
9258 last_p = &TREE_OPERAND (last, 0);
9259 last = *last_p;
9260 goto continue_searching;
9263 if (last == error_mark_node)
9264 return last;
9266 /* In the case that the BIND_EXPR is not necessary, return the
9267 expression out from inside it. */
9268 if (last == BIND_EXPR_BODY (body)
9269 && BIND_EXPR_VARS (body) == NULL)
9271 /* Even if this looks constant, do not allow it in a constant
9272 expression. */
9273 last = c_wrap_maybe_const (last, true);
9274 /* Do not warn if the return value of a statement expression is
9275 unused. */
9276 TREE_NO_WARNING (last) = 1;
9277 return last;
9280 /* Extract the type of said expression. */
9281 type = TREE_TYPE (last);
9283 /* If we're not returning a value at all, then the BIND_EXPR that
9284 we already have is a fine expression to return. */
9285 if (!type || VOID_TYPE_P (type))
9286 return body;
9288 /* Now that we've located the expression containing the value, it seems
9289 silly to make voidify_wrapper_expr repeat the process. Create a
9290 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9291 tmp = create_tmp_var_raw (type, NULL);
9293 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9294 tree_expr_nonnegative_p giving up immediately. */
9295 val = last;
9296 if (TREE_CODE (val) == NOP_EXPR
9297 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9298 val = TREE_OPERAND (val, 0);
9300 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9301 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9304 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9305 SET_EXPR_LOCATION (t, loc);
9306 return t;
9310 /* Begin and end compound statements. This is as simple as pushing
9311 and popping new statement lists from the tree. */
9313 tree
9314 c_begin_compound_stmt (bool do_scope)
9316 tree stmt = push_stmt_list ();
9317 if (do_scope)
9318 push_scope ();
9319 return stmt;
9322 /* End a compound statement. STMT is the statement. LOC is the
9323 location of the compound statement-- this is usually the location
9324 of the opening brace. */
9326 tree
9327 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9329 tree block = NULL;
9331 if (do_scope)
9333 if (c_dialect_objc ())
9334 objc_clear_super_receiver ();
9335 block = pop_scope ();
9338 stmt = pop_stmt_list (stmt);
9339 stmt = c_build_bind_expr (loc, block, stmt);
9341 /* If this compound statement is nested immediately inside a statement
9342 expression, then force a BIND_EXPR to be created. Otherwise we'll
9343 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9344 STATEMENT_LISTs merge, and thus we can lose track of what statement
9345 was really last. */
9346 if (cur_stmt_list
9347 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9348 && TREE_CODE (stmt) != BIND_EXPR)
9350 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9351 TREE_SIDE_EFFECTS (stmt) = 1;
9352 SET_EXPR_LOCATION (stmt, loc);
9355 return stmt;
9358 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9359 when the current scope is exited. EH_ONLY is true when this is not
9360 meant to apply to normal control flow transfer. */
9362 void
9363 push_cleanup (tree decl, tree cleanup, bool eh_only)
9365 enum tree_code code;
9366 tree stmt, list;
9367 bool stmt_expr;
9369 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9370 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9371 add_stmt (stmt);
9372 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9373 list = push_stmt_list ();
9374 TREE_OPERAND (stmt, 0) = list;
9375 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9378 /* Build a binary-operation expression without default conversions.
9379 CODE is the kind of expression to build.
9380 LOCATION is the operator's location.
9381 This function differs from `build' in several ways:
9382 the data type of the result is computed and recorded in it,
9383 warnings are generated if arg data types are invalid,
9384 special handling for addition and subtraction of pointers is known,
9385 and some optimization is done (operations on narrow ints
9386 are done in the narrower type when that gives the same result).
9387 Constant folding is also done before the result is returned.
9389 Note that the operands will never have enumeral types, or function
9390 or array types, because either they will have the default conversions
9391 performed or they have both just been converted to some other type in which
9392 the arithmetic is to be done. */
9394 tree
9395 build_binary_op (location_t location, enum tree_code code,
9396 tree orig_op0, tree orig_op1, int convert_p)
9398 tree type0, type1, orig_type0, orig_type1;
9399 tree eptype;
9400 enum tree_code code0, code1;
9401 tree op0, op1;
9402 tree ret = error_mark_node;
9403 const char *invalid_op_diag;
9404 bool op0_int_operands, op1_int_operands;
9405 bool int_const, int_const_or_overflow, int_operands;
9407 /* Expression code to give to the expression when it is built.
9408 Normally this is CODE, which is what the caller asked for,
9409 but in some special cases we change it. */
9410 enum tree_code resultcode = code;
9412 /* Data type in which the computation is to be performed.
9413 In the simplest cases this is the common type of the arguments. */
9414 tree result_type = NULL;
9416 /* When the computation is in excess precision, the type of the
9417 final EXCESS_PRECISION_EXPR. */
9418 tree semantic_result_type = NULL;
9420 /* Nonzero means operands have already been type-converted
9421 in whatever way is necessary.
9422 Zero means they need to be converted to RESULT_TYPE. */
9423 int converted = 0;
9425 /* Nonzero means create the expression with this type, rather than
9426 RESULT_TYPE. */
9427 tree build_type = 0;
9429 /* Nonzero means after finally constructing the expression
9430 convert it to this type. */
9431 tree final_type = 0;
9433 /* Nonzero if this is an operation like MIN or MAX which can
9434 safely be computed in short if both args are promoted shorts.
9435 Also implies COMMON.
9436 -1 indicates a bitwise operation; this makes a difference
9437 in the exact conditions for when it is safe to do the operation
9438 in a narrower mode. */
9439 int shorten = 0;
9441 /* Nonzero if this is a comparison operation;
9442 if both args are promoted shorts, compare the original shorts.
9443 Also implies COMMON. */
9444 int short_compare = 0;
9446 /* Nonzero if this is a right-shift operation, which can be computed on the
9447 original short and then promoted if the operand is a promoted short. */
9448 int short_shift = 0;
9450 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9451 int common = 0;
9453 /* True means types are compatible as far as ObjC is concerned. */
9454 bool objc_ok;
9456 /* True means this is an arithmetic operation that may need excess
9457 precision. */
9458 bool may_need_excess_precision;
9460 /* True means this is a boolean operation that converts both its
9461 operands to truth-values. */
9462 bool boolean_op = false;
9464 if (location == UNKNOWN_LOCATION)
9465 location = input_location;
9467 op0 = orig_op0;
9468 op1 = orig_op1;
9470 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9471 if (op0_int_operands)
9472 op0 = remove_c_maybe_const_expr (op0);
9473 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9474 if (op1_int_operands)
9475 op1 = remove_c_maybe_const_expr (op1);
9476 int_operands = (op0_int_operands && op1_int_operands);
9477 if (int_operands)
9479 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9480 && TREE_CODE (orig_op1) == INTEGER_CST);
9481 int_const = (int_const_or_overflow
9482 && !TREE_OVERFLOW (orig_op0)
9483 && !TREE_OVERFLOW (orig_op1));
9485 else
9486 int_const = int_const_or_overflow = false;
9488 if (convert_p)
9490 op0 = default_conversion (op0);
9491 op1 = default_conversion (op1);
9494 orig_type0 = type0 = TREE_TYPE (op0);
9495 orig_type1 = type1 = TREE_TYPE (op1);
9497 /* The expression codes of the data types of the arguments tell us
9498 whether the arguments are integers, floating, pointers, etc. */
9499 code0 = TREE_CODE (type0);
9500 code1 = TREE_CODE (type1);
9502 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9503 STRIP_TYPE_NOPS (op0);
9504 STRIP_TYPE_NOPS (op1);
9506 /* If an error was already reported for one of the arguments,
9507 avoid reporting another error. */
9509 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9510 return error_mark_node;
9512 if ((invalid_op_diag
9513 = targetm.invalid_binary_op (code, type0, type1)))
9515 error_at (location, invalid_op_diag);
9516 return error_mark_node;
9519 switch (code)
9521 case PLUS_EXPR:
9522 case MINUS_EXPR:
9523 case MULT_EXPR:
9524 case TRUNC_DIV_EXPR:
9525 case CEIL_DIV_EXPR:
9526 case FLOOR_DIV_EXPR:
9527 case ROUND_DIV_EXPR:
9528 case EXACT_DIV_EXPR:
9529 may_need_excess_precision = true;
9530 break;
9531 default:
9532 may_need_excess_precision = false;
9533 break;
9535 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9537 op0 = TREE_OPERAND (op0, 0);
9538 type0 = TREE_TYPE (op0);
9540 else if (may_need_excess_precision
9541 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9543 type0 = eptype;
9544 op0 = convert (eptype, op0);
9546 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9548 op1 = TREE_OPERAND (op1, 0);
9549 type1 = TREE_TYPE (op1);
9551 else if (may_need_excess_precision
9552 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9554 type1 = eptype;
9555 op1 = convert (eptype, op1);
9558 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9560 switch (code)
9562 case PLUS_EXPR:
9563 /* Handle the pointer + int case. */
9564 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9566 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9567 goto return_build_binary_op;
9569 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9571 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9572 goto return_build_binary_op;
9574 else
9575 common = 1;
9576 break;
9578 case MINUS_EXPR:
9579 /* Subtraction of two similar pointers.
9580 We must subtract them as integers, then divide by object size. */
9581 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9582 && comp_target_types (location, type0, type1))
9584 ret = pointer_diff (location, op0, op1);
9585 goto return_build_binary_op;
9587 /* Handle pointer minus int. Just like pointer plus int. */
9588 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9590 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9591 goto return_build_binary_op;
9593 else
9594 common = 1;
9595 break;
9597 case MULT_EXPR:
9598 common = 1;
9599 break;
9601 case TRUNC_DIV_EXPR:
9602 case CEIL_DIV_EXPR:
9603 case FLOOR_DIV_EXPR:
9604 case ROUND_DIV_EXPR:
9605 case EXACT_DIV_EXPR:
9606 warn_for_div_by_zero (location, op1);
9608 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9609 || code0 == FIXED_POINT_TYPE
9610 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9611 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9612 || code1 == FIXED_POINT_TYPE
9613 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9615 enum tree_code tcode0 = code0, tcode1 = code1;
9617 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9618 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9619 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9620 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9622 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9623 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9624 resultcode = RDIV_EXPR;
9625 else
9626 /* Although it would be tempting to shorten always here, that
9627 loses on some targets, since the modulo instruction is
9628 undefined if the quotient can't be represented in the
9629 computation mode. We shorten only if unsigned or if
9630 dividing by something we know != -1. */
9631 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9632 || (TREE_CODE (op1) == INTEGER_CST
9633 && !integer_all_onesp (op1)));
9634 common = 1;
9636 break;
9638 case BIT_AND_EXPR:
9639 case BIT_IOR_EXPR:
9640 case BIT_XOR_EXPR:
9641 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9642 shorten = -1;
9643 /* Allow vector types which are not floating point types. */
9644 else if (code0 == VECTOR_TYPE
9645 && code1 == VECTOR_TYPE
9646 && !VECTOR_FLOAT_TYPE_P (type0)
9647 && !VECTOR_FLOAT_TYPE_P (type1))
9648 common = 1;
9649 break;
9651 case TRUNC_MOD_EXPR:
9652 case FLOOR_MOD_EXPR:
9653 warn_for_div_by_zero (location, op1);
9655 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9656 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9657 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9658 common = 1;
9659 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9661 /* Although it would be tempting to shorten always here, that loses
9662 on some targets, since the modulo instruction is undefined if the
9663 quotient can't be represented in the computation mode. We shorten
9664 only if unsigned or if dividing by something we know != -1. */
9665 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9666 || (TREE_CODE (op1) == INTEGER_CST
9667 && !integer_all_onesp (op1)));
9668 common = 1;
9670 break;
9672 case TRUTH_ANDIF_EXPR:
9673 case TRUTH_ORIF_EXPR:
9674 case TRUTH_AND_EXPR:
9675 case TRUTH_OR_EXPR:
9676 case TRUTH_XOR_EXPR:
9677 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9678 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9679 || code0 == FIXED_POINT_TYPE)
9680 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9681 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9682 || code1 == FIXED_POINT_TYPE))
9684 /* Result of these operations is always an int,
9685 but that does not mean the operands should be
9686 converted to ints! */
9687 result_type = integer_type_node;
9688 op0 = c_common_truthvalue_conversion (location, op0);
9689 op1 = c_common_truthvalue_conversion (location, op1);
9690 converted = 1;
9691 boolean_op = true;
9693 if (code == TRUTH_ANDIF_EXPR)
9695 int_const_or_overflow = (int_operands
9696 && TREE_CODE (orig_op0) == INTEGER_CST
9697 && (op0 == truthvalue_false_node
9698 || TREE_CODE (orig_op1) == INTEGER_CST));
9699 int_const = (int_const_or_overflow
9700 && !TREE_OVERFLOW (orig_op0)
9701 && (op0 == truthvalue_false_node
9702 || !TREE_OVERFLOW (orig_op1)));
9704 else if (code == TRUTH_ORIF_EXPR)
9706 int_const_or_overflow = (int_operands
9707 && TREE_CODE (orig_op0) == INTEGER_CST
9708 && (op0 == truthvalue_true_node
9709 || TREE_CODE (orig_op1) == INTEGER_CST));
9710 int_const = (int_const_or_overflow
9711 && !TREE_OVERFLOW (orig_op0)
9712 && (op0 == truthvalue_true_node
9713 || !TREE_OVERFLOW (orig_op1)));
9715 break;
9717 /* Shift operations: result has same type as first operand;
9718 always convert second operand to int.
9719 Also set SHORT_SHIFT if shifting rightward. */
9721 case RSHIFT_EXPR:
9722 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9723 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9725 result_type = type0;
9726 converted = 1;
9728 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9729 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9730 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9731 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9733 result_type = type0;
9734 converted = 1;
9736 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9737 && code1 == INTEGER_TYPE)
9739 if (TREE_CODE (op1) == INTEGER_CST)
9741 if (tree_int_cst_sgn (op1) < 0)
9743 int_const = false;
9744 if (c_inhibit_evaluation_warnings == 0)
9745 warning (0, "right shift count is negative");
9747 else
9749 if (!integer_zerop (op1))
9750 short_shift = 1;
9752 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9754 int_const = false;
9755 if (c_inhibit_evaluation_warnings == 0)
9756 warning (0, "right shift count >= width of type");
9761 /* Use the type of the value to be shifted. */
9762 result_type = type0;
9763 /* Convert the non vector shift-count to an integer, regardless
9764 of size of value being shifted. */
9765 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9766 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9767 op1 = convert (integer_type_node, op1);
9768 /* Avoid converting op1 to result_type later. */
9769 converted = 1;
9771 break;
9773 case LSHIFT_EXPR:
9774 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9775 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9777 result_type = type0;
9778 converted = 1;
9780 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9781 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9782 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9783 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9785 result_type = type0;
9786 converted = 1;
9788 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9789 && code1 == INTEGER_TYPE)
9791 if (TREE_CODE (op1) == INTEGER_CST)
9793 if (tree_int_cst_sgn (op1) < 0)
9795 int_const = false;
9796 if (c_inhibit_evaluation_warnings == 0)
9797 warning (0, "left shift count is negative");
9800 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9802 int_const = false;
9803 if (c_inhibit_evaluation_warnings == 0)
9804 warning (0, "left shift count >= width of type");
9808 /* Use the type of the value to be shifted. */
9809 result_type = type0;
9810 /* Convert the non vector shift-count to an integer, regardless
9811 of size of value being shifted. */
9812 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9813 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9814 op1 = convert (integer_type_node, op1);
9815 /* Avoid converting op1 to result_type later. */
9816 converted = 1;
9818 break;
9820 case EQ_EXPR:
9821 case NE_EXPR:
9822 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9823 warning_at (location,
9824 OPT_Wfloat_equal,
9825 "comparing floating point with == or != is unsafe");
9826 /* Result of comparison is always int,
9827 but don't convert the args to int! */
9828 build_type = integer_type_node;
9829 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9830 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9831 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9832 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9833 short_compare = 1;
9834 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9836 if (TREE_CODE (op0) == ADDR_EXPR
9837 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9839 if (code == EQ_EXPR)
9840 warning_at (location,
9841 OPT_Waddress,
9842 "the comparison will always evaluate as %<false%> "
9843 "for the address of %qD will never be NULL",
9844 TREE_OPERAND (op0, 0));
9845 else
9846 warning_at (location,
9847 OPT_Waddress,
9848 "the comparison will always evaluate as %<true%> "
9849 "for the address of %qD will never be NULL",
9850 TREE_OPERAND (op0, 0));
9852 result_type = type0;
9854 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9856 if (TREE_CODE (op1) == ADDR_EXPR
9857 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9859 if (code == EQ_EXPR)
9860 warning_at (location,
9861 OPT_Waddress,
9862 "the comparison will always evaluate as %<false%> "
9863 "for the address of %qD will never be NULL",
9864 TREE_OPERAND (op1, 0));
9865 else
9866 warning_at (location,
9867 OPT_Waddress,
9868 "the comparison will always evaluate as %<true%> "
9869 "for the address of %qD will never be NULL",
9870 TREE_OPERAND (op1, 0));
9872 result_type = type1;
9874 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9876 tree tt0 = TREE_TYPE (type0);
9877 tree tt1 = TREE_TYPE (type1);
9878 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
9879 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
9880 addr_space_t as_common = ADDR_SPACE_GENERIC;
9882 /* Anything compares with void *. void * compares with anything.
9883 Otherwise, the targets must be compatible
9884 and both must be object or both incomplete. */
9885 if (comp_target_types (location, type0, type1))
9886 result_type = common_pointer_type (type0, type1);
9887 else if (!addr_space_superset (as0, as1, &as_common))
9889 error_at (location, "comparison of pointers to "
9890 "disjoint address spaces");
9891 return error_mark_node;
9893 else if (VOID_TYPE_P (tt0))
9895 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
9896 pedwarn (location, OPT_pedantic, "ISO C forbids "
9897 "comparison of %<void *%> with function pointer");
9899 else if (VOID_TYPE_P (tt1))
9901 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
9902 pedwarn (location, OPT_pedantic, "ISO C forbids "
9903 "comparison of %<void *%> with function pointer");
9905 else
9906 /* Avoid warning about the volatile ObjC EH puts on decls. */
9907 if (!objc_ok)
9908 pedwarn (location, 0,
9909 "comparison of distinct pointer types lacks a cast");
9911 if (result_type == NULL_TREE)
9913 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9914 result_type = build_pointer_type
9915 (build_qualified_type (void_type_node, qual));
9918 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9920 result_type = type0;
9921 pedwarn (location, 0, "comparison between pointer and integer");
9923 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9925 result_type = type1;
9926 pedwarn (location, 0, "comparison between pointer and integer");
9928 break;
9930 case LE_EXPR:
9931 case GE_EXPR:
9932 case LT_EXPR:
9933 case GT_EXPR:
9934 build_type = integer_type_node;
9935 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9936 || code0 == FIXED_POINT_TYPE)
9937 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9938 || code1 == FIXED_POINT_TYPE))
9939 short_compare = 1;
9940 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9942 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
9943 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
9944 addr_space_t as_common;
9946 if (comp_target_types (location, type0, type1))
9948 result_type = common_pointer_type (type0, type1);
9949 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9950 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9951 pedwarn (location, 0,
9952 "comparison of complete and incomplete pointers");
9953 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9954 pedwarn (location, OPT_pedantic, "ISO C forbids "
9955 "ordered comparisons of pointers to functions");
9956 else if (null_pointer_constant_p (orig_op0)
9957 || null_pointer_constant_p (orig_op1))
9958 warning_at (location, OPT_Wextra,
9959 "ordered comparison of pointer with null pointer");
9962 else if (!addr_space_superset (as0, as1, &as_common))
9964 error_at (location, "comparison of pointers to "
9965 "disjoint address spaces");
9966 return error_mark_node;
9968 else
9970 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9971 result_type = build_pointer_type
9972 (build_qualified_type (void_type_node, qual));
9973 pedwarn (location, 0,
9974 "comparison of distinct pointer types lacks a cast");
9977 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9979 result_type = type0;
9980 if (pedantic)
9981 pedwarn (location, OPT_pedantic,
9982 "ordered comparison of pointer with integer zero");
9983 else if (extra_warnings)
9984 warning_at (location, OPT_Wextra,
9985 "ordered comparison of pointer with integer zero");
9987 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9989 result_type = type1;
9990 if (pedantic)
9991 pedwarn (location, OPT_pedantic,
9992 "ordered comparison of pointer with integer zero");
9993 else if (extra_warnings)
9994 warning_at (location, OPT_Wextra,
9995 "ordered comparison of pointer with integer zero");
9997 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9999 result_type = type0;
10000 pedwarn (location, 0, "comparison between pointer and integer");
10002 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10004 result_type = type1;
10005 pedwarn (location, 0, "comparison between pointer and integer");
10007 break;
10009 default:
10010 gcc_unreachable ();
10013 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10014 return error_mark_node;
10016 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10017 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10018 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
10019 TREE_TYPE (type1))))
10021 binary_op_error (location, code, type0, type1);
10022 return error_mark_node;
10025 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10026 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10028 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10029 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10031 bool first_complex = (code0 == COMPLEX_TYPE);
10032 bool second_complex = (code1 == COMPLEX_TYPE);
10033 int none_complex = (!first_complex && !second_complex);
10035 if (shorten || common || short_compare)
10037 result_type = c_common_type (type0, type1);
10038 do_warn_double_promotion (result_type, type0, type1,
10039 "implicit conversion from %qT to %qT "
10040 "to match other operand of binary "
10041 "expression",
10042 location);
10043 if (result_type == error_mark_node)
10044 return error_mark_node;
10047 if (first_complex != second_complex
10048 && (code == PLUS_EXPR
10049 || code == MINUS_EXPR
10050 || code == MULT_EXPR
10051 || (code == TRUNC_DIV_EXPR && first_complex))
10052 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10053 && flag_signed_zeros)
10055 /* An operation on mixed real/complex operands must be
10056 handled specially, but the language-independent code can
10057 more easily optimize the plain complex arithmetic if
10058 -fno-signed-zeros. */
10059 tree real_type = TREE_TYPE (result_type);
10060 tree real, imag;
10061 if (type0 != orig_type0 || type1 != orig_type1)
10063 gcc_assert (may_need_excess_precision && common);
10064 semantic_result_type = c_common_type (orig_type0, orig_type1);
10066 if (first_complex)
10068 if (TREE_TYPE (op0) != result_type)
10069 op0 = convert_and_check (result_type, op0);
10070 if (TREE_TYPE (op1) != real_type)
10071 op1 = convert_and_check (real_type, op1);
10073 else
10075 if (TREE_TYPE (op0) != real_type)
10076 op0 = convert_and_check (real_type, op0);
10077 if (TREE_TYPE (op1) != result_type)
10078 op1 = convert_and_check (result_type, op1);
10080 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10081 return error_mark_node;
10082 if (first_complex)
10084 op0 = c_save_expr (op0);
10085 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10086 op0, 1);
10087 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10088 op0, 1);
10089 switch (code)
10091 case MULT_EXPR:
10092 case TRUNC_DIV_EXPR:
10093 imag = build2 (resultcode, real_type, imag, op1);
10094 /* Fall through. */
10095 case PLUS_EXPR:
10096 case MINUS_EXPR:
10097 real = build2 (resultcode, real_type, real, op1);
10098 break;
10099 default:
10100 gcc_unreachable();
10103 else
10105 op1 = c_save_expr (op1);
10106 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10107 op1, 1);
10108 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10109 op1, 1);
10110 switch (code)
10112 case MULT_EXPR:
10113 imag = build2 (resultcode, real_type, op0, imag);
10114 /* Fall through. */
10115 case PLUS_EXPR:
10116 real = build2 (resultcode, real_type, op0, real);
10117 break;
10118 case MINUS_EXPR:
10119 real = build2 (resultcode, real_type, op0, real);
10120 imag = build1 (NEGATE_EXPR, real_type, imag);
10121 break;
10122 default:
10123 gcc_unreachable();
10126 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10127 goto return_build_binary_op;
10130 /* For certain operations (which identify themselves by shorten != 0)
10131 if both args were extended from the same smaller type,
10132 do the arithmetic in that type and then extend.
10134 shorten !=0 and !=1 indicates a bitwise operation.
10135 For them, this optimization is safe only if
10136 both args are zero-extended or both are sign-extended.
10137 Otherwise, we might change the result.
10138 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10139 but calculated in (unsigned short) it would be (unsigned short)-1. */
10141 if (shorten && none_complex)
10143 final_type = result_type;
10144 result_type = shorten_binary_op (result_type, op0, op1,
10145 shorten == -1);
10148 /* Shifts can be shortened if shifting right. */
10150 if (short_shift)
10152 int unsigned_arg;
10153 tree arg0 = get_narrower (op0, &unsigned_arg);
10155 final_type = result_type;
10157 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10158 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10160 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10161 && tree_int_cst_sgn (op1) > 0
10162 /* We can shorten only if the shift count is less than the
10163 number of bits in the smaller type size. */
10164 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10165 /* We cannot drop an unsigned shift after sign-extension. */
10166 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10168 /* Do an unsigned shift if the operand was zero-extended. */
10169 result_type
10170 = c_common_signed_or_unsigned_type (unsigned_arg,
10171 TREE_TYPE (arg0));
10172 /* Convert value-to-be-shifted to that type. */
10173 if (TREE_TYPE (op0) != result_type)
10174 op0 = convert (result_type, op0);
10175 converted = 1;
10179 /* Comparison operations are shortened too but differently.
10180 They identify themselves by setting short_compare = 1. */
10182 if (short_compare)
10184 /* Don't write &op0, etc., because that would prevent op0
10185 from being kept in a register.
10186 Instead, make copies of the our local variables and
10187 pass the copies by reference, then copy them back afterward. */
10188 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10189 enum tree_code xresultcode = resultcode;
10190 tree val
10191 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10193 if (val != 0)
10195 ret = val;
10196 goto return_build_binary_op;
10199 op0 = xop0, op1 = xop1;
10200 converted = 1;
10201 resultcode = xresultcode;
10203 if (c_inhibit_evaluation_warnings == 0)
10205 bool op0_maybe_const = true;
10206 bool op1_maybe_const = true;
10207 tree orig_op0_folded, orig_op1_folded;
10209 if (in_late_binary_op)
10211 orig_op0_folded = orig_op0;
10212 orig_op1_folded = orig_op1;
10214 else
10216 /* Fold for the sake of possible warnings, as in
10217 build_conditional_expr. This requires the
10218 "original" values to be folded, not just op0 and
10219 op1. */
10220 c_inhibit_evaluation_warnings++;
10221 op0 = c_fully_fold (op0, require_constant_value,
10222 &op0_maybe_const);
10223 op1 = c_fully_fold (op1, require_constant_value,
10224 &op1_maybe_const);
10225 c_inhibit_evaluation_warnings--;
10226 orig_op0_folded = c_fully_fold (orig_op0,
10227 require_constant_value,
10228 NULL);
10229 orig_op1_folded = c_fully_fold (orig_op1,
10230 require_constant_value,
10231 NULL);
10234 if (warn_sign_compare)
10235 warn_for_sign_compare (location, orig_op0_folded,
10236 orig_op1_folded, op0, op1,
10237 result_type, resultcode);
10238 if (!in_late_binary_op)
10240 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10241 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10242 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10243 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10249 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10250 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10251 Then the expression will be built.
10252 It will be given type FINAL_TYPE if that is nonzero;
10253 otherwise, it will be given type RESULT_TYPE. */
10255 if (!result_type)
10257 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10258 return error_mark_node;
10261 if (build_type == NULL_TREE)
10263 build_type = result_type;
10264 if ((type0 != orig_type0 || type1 != orig_type1)
10265 && !boolean_op)
10267 gcc_assert (may_need_excess_precision && common);
10268 semantic_result_type = c_common_type (orig_type0, orig_type1);
10272 if (!converted)
10274 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10275 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10277 /* This can happen if one operand has a vector type, and the other
10278 has a different type. */
10279 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10280 return error_mark_node;
10283 /* Treat expressions in initializers specially as they can't trap. */
10284 if (int_const_or_overflow)
10285 ret = (require_constant_value
10286 ? fold_build2_initializer_loc (location, resultcode, build_type,
10287 op0, op1)
10288 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10289 else
10290 ret = build2 (resultcode, build_type, op0, op1);
10291 if (final_type != 0)
10292 ret = convert (final_type, ret);
10294 return_build_binary_op:
10295 gcc_assert (ret != error_mark_node);
10296 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10297 ret = (int_operands
10298 ? note_integer_operands (ret)
10299 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10300 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10301 && !in_late_binary_op)
10302 ret = note_integer_operands (ret);
10303 if (semantic_result_type)
10304 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10305 protected_set_expr_location (ret, location);
10306 return ret;
10310 /* Convert EXPR to be a truth-value, validating its type for this
10311 purpose. LOCATION is the source location for the expression. */
10313 tree
10314 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10316 bool int_const, int_operands;
10318 switch (TREE_CODE (TREE_TYPE (expr)))
10320 case ARRAY_TYPE:
10321 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10322 return error_mark_node;
10324 case RECORD_TYPE:
10325 error_at (location, "used struct type value where scalar is required");
10326 return error_mark_node;
10328 case UNION_TYPE:
10329 error_at (location, "used union type value where scalar is required");
10330 return error_mark_node;
10332 case FUNCTION_TYPE:
10333 gcc_unreachable ();
10335 default:
10336 break;
10339 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10340 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10341 if (int_operands)
10342 expr = remove_c_maybe_const_expr (expr);
10344 /* ??? Should we also give an error for void and vectors rather than
10345 leaving those to give errors later? */
10346 expr = c_common_truthvalue_conversion (location, expr);
10348 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10350 if (TREE_OVERFLOW (expr))
10351 return expr;
10352 else
10353 return note_integer_operands (expr);
10355 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10356 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10357 return expr;
10361 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10362 required. */
10364 tree
10365 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10367 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10369 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10370 /* Executing a compound literal inside a function reinitializes
10371 it. */
10372 if (!TREE_STATIC (decl))
10373 *se = true;
10374 return decl;
10376 else
10377 return expr;
10380 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10382 tree
10383 c_begin_omp_parallel (void)
10385 tree block;
10387 keep_next_level ();
10388 block = c_begin_compound_stmt (true);
10390 return block;
10393 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10394 statement. LOC is the location of the OMP_PARALLEL. */
10396 tree
10397 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10399 tree stmt;
10401 block = c_end_compound_stmt (loc, block, true);
10403 stmt = make_node (OMP_PARALLEL);
10404 TREE_TYPE (stmt) = void_type_node;
10405 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10406 OMP_PARALLEL_BODY (stmt) = block;
10407 SET_EXPR_LOCATION (stmt, loc);
10409 return add_stmt (stmt);
10412 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10414 tree
10415 c_begin_omp_task (void)
10417 tree block;
10419 keep_next_level ();
10420 block = c_begin_compound_stmt (true);
10422 return block;
10425 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10426 statement. LOC is the location of the #pragma. */
10428 tree
10429 c_finish_omp_task (location_t loc, tree clauses, tree block)
10431 tree stmt;
10433 block = c_end_compound_stmt (loc, block, true);
10435 stmt = make_node (OMP_TASK);
10436 TREE_TYPE (stmt) = void_type_node;
10437 OMP_TASK_CLAUSES (stmt) = clauses;
10438 OMP_TASK_BODY (stmt) = block;
10439 SET_EXPR_LOCATION (stmt, loc);
10441 return add_stmt (stmt);
10444 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10445 Remove any elements from the list that are invalid. */
10447 tree
10448 c_finish_omp_clauses (tree clauses)
10450 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10451 tree c, t, *pc = &clauses;
10452 const char *name;
10454 bitmap_obstack_initialize (NULL);
10455 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10456 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10457 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10459 for (pc = &clauses, c = clauses; c ; c = *pc)
10461 bool remove = false;
10462 bool need_complete = false;
10463 bool need_implicitly_determined = false;
10465 switch (OMP_CLAUSE_CODE (c))
10467 case OMP_CLAUSE_SHARED:
10468 name = "shared";
10469 need_implicitly_determined = true;
10470 goto check_dup_generic;
10472 case OMP_CLAUSE_PRIVATE:
10473 name = "private";
10474 need_complete = true;
10475 need_implicitly_determined = true;
10476 goto check_dup_generic;
10478 case OMP_CLAUSE_REDUCTION:
10479 name = "reduction";
10480 need_implicitly_determined = true;
10481 t = OMP_CLAUSE_DECL (c);
10482 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10483 || POINTER_TYPE_P (TREE_TYPE (t)))
10485 error_at (OMP_CLAUSE_LOCATION (c),
10486 "%qE has invalid type for %<reduction%>", t);
10487 remove = true;
10489 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10491 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10492 const char *r_name = NULL;
10494 switch (r_code)
10496 case PLUS_EXPR:
10497 case MULT_EXPR:
10498 case MINUS_EXPR:
10499 break;
10500 case BIT_AND_EXPR:
10501 r_name = "&";
10502 break;
10503 case BIT_XOR_EXPR:
10504 r_name = "^";
10505 break;
10506 case BIT_IOR_EXPR:
10507 r_name = "|";
10508 break;
10509 case TRUTH_ANDIF_EXPR:
10510 r_name = "&&";
10511 break;
10512 case TRUTH_ORIF_EXPR:
10513 r_name = "||";
10514 break;
10515 default:
10516 gcc_unreachable ();
10518 if (r_name)
10520 error_at (OMP_CLAUSE_LOCATION (c),
10521 "%qE has invalid type for %<reduction(%s)%>",
10522 t, r_name);
10523 remove = true;
10526 goto check_dup_generic;
10528 case OMP_CLAUSE_COPYPRIVATE:
10529 name = "copyprivate";
10530 goto check_dup_generic;
10532 case OMP_CLAUSE_COPYIN:
10533 name = "copyin";
10534 t = OMP_CLAUSE_DECL (c);
10535 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10537 error_at (OMP_CLAUSE_LOCATION (c),
10538 "%qE must be %<threadprivate%> for %<copyin%>", t);
10539 remove = true;
10541 goto check_dup_generic;
10543 check_dup_generic:
10544 t = OMP_CLAUSE_DECL (c);
10545 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10547 error_at (OMP_CLAUSE_LOCATION (c),
10548 "%qE is not a variable in clause %qs", t, name);
10549 remove = true;
10551 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10552 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10553 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10555 error_at (OMP_CLAUSE_LOCATION (c),
10556 "%qE appears more than once in data clauses", t);
10557 remove = true;
10559 else
10560 bitmap_set_bit (&generic_head, DECL_UID (t));
10561 break;
10563 case OMP_CLAUSE_FIRSTPRIVATE:
10564 name = "firstprivate";
10565 t = OMP_CLAUSE_DECL (c);
10566 need_complete = true;
10567 need_implicitly_determined = true;
10568 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10570 error_at (OMP_CLAUSE_LOCATION (c),
10571 "%qE is not a variable in clause %<firstprivate%>", t);
10572 remove = true;
10574 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10575 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10577 error_at (OMP_CLAUSE_LOCATION (c),
10578 "%qE appears more than once in data clauses", t);
10579 remove = true;
10581 else
10582 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10583 break;
10585 case OMP_CLAUSE_LASTPRIVATE:
10586 name = "lastprivate";
10587 t = OMP_CLAUSE_DECL (c);
10588 need_complete = true;
10589 need_implicitly_determined = true;
10590 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10592 error_at (OMP_CLAUSE_LOCATION (c),
10593 "%qE is not a variable in clause %<lastprivate%>", t);
10594 remove = true;
10596 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10597 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10599 error_at (OMP_CLAUSE_LOCATION (c),
10600 "%qE appears more than once in data clauses", t);
10601 remove = true;
10603 else
10604 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10605 break;
10607 case OMP_CLAUSE_IF:
10608 case OMP_CLAUSE_NUM_THREADS:
10609 case OMP_CLAUSE_SCHEDULE:
10610 case OMP_CLAUSE_NOWAIT:
10611 case OMP_CLAUSE_ORDERED:
10612 case OMP_CLAUSE_DEFAULT:
10613 case OMP_CLAUSE_UNTIED:
10614 case OMP_CLAUSE_COLLAPSE:
10615 pc = &OMP_CLAUSE_CHAIN (c);
10616 continue;
10618 default:
10619 gcc_unreachable ();
10622 if (!remove)
10624 t = OMP_CLAUSE_DECL (c);
10626 if (need_complete)
10628 t = require_complete_type (t);
10629 if (t == error_mark_node)
10630 remove = true;
10633 if (need_implicitly_determined)
10635 const char *share_name = NULL;
10637 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10638 share_name = "threadprivate";
10639 else switch (c_omp_predetermined_sharing (t))
10641 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10642 break;
10643 case OMP_CLAUSE_DEFAULT_SHARED:
10644 share_name = "shared";
10645 break;
10646 case OMP_CLAUSE_DEFAULT_PRIVATE:
10647 share_name = "private";
10648 break;
10649 default:
10650 gcc_unreachable ();
10652 if (share_name)
10654 error_at (OMP_CLAUSE_LOCATION (c),
10655 "%qE is predetermined %qs for %qs",
10656 t, share_name, name);
10657 remove = true;
10662 if (remove)
10663 *pc = OMP_CLAUSE_CHAIN (c);
10664 else
10665 pc = &OMP_CLAUSE_CHAIN (c);
10668 bitmap_obstack_release (NULL);
10669 return clauses;
10672 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10673 down to the element type of an array. */
10675 tree
10676 c_build_qualified_type (tree type, int type_quals)
10678 if (type == error_mark_node)
10679 return type;
10681 if (TREE_CODE (type) == ARRAY_TYPE)
10683 tree t;
10684 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10685 type_quals);
10687 /* See if we already have an identically qualified type. */
10688 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10690 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10691 && TYPE_NAME (t) == TYPE_NAME (type)
10692 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10693 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10694 TYPE_ATTRIBUTES (type)))
10695 break;
10697 if (!t)
10699 tree domain = TYPE_DOMAIN (type);
10701 t = build_variant_type_copy (type);
10702 TREE_TYPE (t) = element_type;
10704 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10705 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10706 SET_TYPE_STRUCTURAL_EQUALITY (t);
10707 else if (TYPE_CANONICAL (element_type) != element_type
10708 || (domain && TYPE_CANONICAL (domain) != domain))
10710 tree unqualified_canon
10711 = build_array_type (TYPE_CANONICAL (element_type),
10712 domain? TYPE_CANONICAL (domain)
10713 : NULL_TREE);
10714 TYPE_CANONICAL (t)
10715 = c_build_qualified_type (unqualified_canon, type_quals);
10717 else
10718 TYPE_CANONICAL (t) = t;
10720 return t;
10723 /* A restrict-qualified pointer type must be a pointer to object or
10724 incomplete type. Note that the use of POINTER_TYPE_P also allows
10725 REFERENCE_TYPEs, which is appropriate for C++. */
10726 if ((type_quals & TYPE_QUAL_RESTRICT)
10727 && (!POINTER_TYPE_P (type)
10728 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10730 error ("invalid use of %<restrict%>");
10731 type_quals &= ~TYPE_QUAL_RESTRICT;
10734 return build_qualified_type (type, type_quals);
10737 /* Build a VA_ARG_EXPR for the C parser. */
10739 tree
10740 c_build_va_arg (location_t loc, tree expr, tree type)
10742 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10743 warning_at (loc, OPT_Wc___compat,
10744 "C++ requires promoted type, not enum type, in %<va_arg%>");
10745 return build_va_arg (loc, expr, type);