1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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. */
30 #include "coretypes.h"
34 #include "langhooks.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
48 /* Nonzero if we've already printed a "missing braces around initializer"
49 message within this initializer. */
50 static int missing_braces_mentioned;
52 static int require_constant_value;
53 static int require_constant_elements;
55 static tree qualify_type (tree, tree);
56 static int tagged_types_tu_compatible_p (tree, tree);
57 static int comp_target_types (tree, tree, int);
58 static int function_types_compatible_p (tree, tree);
59 static int type_lists_compatible_p (tree, tree);
60 static tree decl_constant_value_for_broken_optimization (tree);
61 static tree default_function_array_conversion (tree);
62 static tree lookup_field (tree, tree);
63 static tree convert_arguments (tree, tree, tree, tree);
64 static tree pointer_diff (tree, tree);
65 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
67 static void warn_for_assignment (const char *, const char *, tree, int);
68 static tree valid_compound_expr_initializer (tree, tree);
69 static void push_string (const char *);
70 static void push_member_name (tree);
71 static void push_array_bounds (int);
72 static int spelling_length (void);
73 static char *print_spelling (char *);
74 static void warning_init (const char *);
75 static tree digest_init (tree, tree, bool, int);
76 static void output_init_element (tree, bool, tree, tree, int);
77 static void output_pending_init_elements (int);
78 static int set_designator (int);
79 static void push_range_stack (tree);
80 static void add_pending_init (tree, tree);
81 static void set_nonincremental_init (void);
82 static void set_nonincremental_init_from_string (tree);
83 static tree find_init_member (tree);
84 static int lvalue_or_else (tree, const char *);
86 /* Do `exp = require_complete_type (exp);' to make sure exp
87 does not have an incomplete type. (That includes void types.) */
90 require_complete_type (tree value)
92 tree type = TREE_TYPE (value);
94 if (value == error_mark_node || type == error_mark_node)
95 return error_mark_node;
97 /* First, detect a valid value with a complete type. */
98 if (COMPLETE_TYPE_P (type))
101 c_incomplete_type_error (value, type);
102 return error_mark_node;
105 /* Print an error message for invalid use of an incomplete type.
106 VALUE is the expression that was used (or 0 if that isn't known)
107 and TYPE is the type that was invalid. */
110 c_incomplete_type_error (tree value, tree type)
112 const char *type_code_string;
114 /* Avoid duplicate error message. */
115 if (TREE_CODE (type) == ERROR_MARK)
118 if (value != 0 && (TREE_CODE (value) == VAR_DECL
119 || TREE_CODE (value) == PARM_DECL))
120 error ("`%s' has an incomplete type",
121 IDENTIFIER_POINTER (DECL_NAME (value)));
125 /* We must print an error message. Be clever about what it says. */
127 switch (TREE_CODE (type))
130 type_code_string = "struct";
134 type_code_string = "union";
138 type_code_string = "enum";
142 error ("invalid use of void expression");
146 if (TYPE_DOMAIN (type))
148 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
150 error ("invalid use of flexible array member");
153 type = TREE_TYPE (type);
156 error ("invalid use of array with unspecified bounds");
163 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
164 error ("invalid use of undefined type `%s %s'",
165 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
167 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
168 error ("invalid use of incomplete typedef `%s'",
169 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
173 /* Given a type, apply default promotions wrt unnamed function
174 arguments and return the new type. */
177 c_type_promotes_to (tree type)
179 if (TYPE_MAIN_VARIANT (type) == float_type_node)
180 return double_type_node;
182 if (c_promoting_integer_type_p (type))
184 /* Preserve unsignedness if not really getting any wider. */
185 if (TYPE_UNSIGNED (type)
186 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
187 return unsigned_type_node;
188 return integer_type_node;
194 /* Return a variant of TYPE which has all the type qualifiers of LIKE
195 as well as those of TYPE. */
198 qualify_type (tree type, tree like)
200 return c_build_qualified_type (type,
201 TYPE_QUALS (type) | TYPE_QUALS (like));
204 /* Return the composite type of two compatible types.
206 We assume that comptypes has already been done and returned
207 nonzero; if that isn't so, this may crash. In particular, we
208 assume that qualifiers match. */
211 composite_type (tree t1, tree t2)
213 enum tree_code code1;
214 enum tree_code code2;
217 /* Save time if the two types are the same. */
219 if (t1 == t2) return t1;
221 /* If one type is nonsense, use the other. */
222 if (t1 == error_mark_node)
224 if (t2 == error_mark_node)
227 code1 = TREE_CODE (t1);
228 code2 = TREE_CODE (t2);
230 /* Merge the attributes. */
231 attributes = targetm.merge_type_attributes (t1, t2);
233 /* If one is an enumerated type and the other is the compatible
234 integer type, the composite type might be either of the two
235 (DR#013 question 3). For consistency, use the enumerated type as
236 the composite type. */
238 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
240 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
243 gcc_assert (code1 == code2);
248 /* For two pointers, do this recursively on the target type. */
250 tree pointed_to_1 = TREE_TYPE (t1);
251 tree pointed_to_2 = TREE_TYPE (t2);
252 tree target = composite_type (pointed_to_1, pointed_to_2);
253 t1 = build_pointer_type (target);
254 t1 = build_type_attribute_variant (t1, attributes);
255 return qualify_type (t1, t2);
260 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
262 /* We should not have any type quals on arrays at all. */
263 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
265 /* Save space: see if the result is identical to one of the args. */
266 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
267 return build_type_attribute_variant (t1, attributes);
268 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
269 return build_type_attribute_variant (t2, attributes);
271 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
272 return build_type_attribute_variant (t1, attributes);
273 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
274 return build_type_attribute_variant (t2, attributes);
276 /* Merge the element types, and have a size if either arg has one. */
277 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
278 return build_type_attribute_variant (t1, attributes);
282 /* Function types: prefer the one that specified arg types.
283 If both do, merge the arg types. Also merge the return types. */
285 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
286 tree p1 = TYPE_ARG_TYPES (t1);
287 tree p2 = TYPE_ARG_TYPES (t2);
292 /* Save space: see if the result is identical to one of the args. */
293 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
294 return build_type_attribute_variant (t1, attributes);
295 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
296 return build_type_attribute_variant (t2, attributes);
298 /* Simple way if one arg fails to specify argument types. */
299 if (TYPE_ARG_TYPES (t1) == 0)
301 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
302 t1 = build_type_attribute_variant (t1, attributes);
303 return qualify_type (t1, t2);
305 if (TYPE_ARG_TYPES (t2) == 0)
307 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
308 t1 = build_type_attribute_variant (t1, attributes);
309 return qualify_type (t1, t2);
312 /* If both args specify argument types, we must merge the two
313 lists, argument by argument. */
314 /* Tell global_bindings_p to return false so that variable_size
315 doesn't abort on VLAs in parameter types. */
316 c_override_global_bindings_to_false = true;
318 len = list_length (p1);
321 for (i = 0; i < len; i++)
322 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
327 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
329 /* A null type means arg type is not specified.
330 Take whatever the other function type has. */
331 if (TREE_VALUE (p1) == 0)
333 TREE_VALUE (n) = TREE_VALUE (p2);
336 if (TREE_VALUE (p2) == 0)
338 TREE_VALUE (n) = TREE_VALUE (p1);
342 /* Given wait (union {union wait *u; int *i} *)
343 and wait (union wait *),
344 prefer union wait * as type of parm. */
345 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
346 && TREE_VALUE (p1) != TREE_VALUE (p2))
349 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
350 memb; memb = TREE_CHAIN (memb))
351 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
353 TREE_VALUE (n) = TREE_VALUE (p2);
355 pedwarn ("function types not truly compatible in ISO C");
359 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
360 && TREE_VALUE (p2) != TREE_VALUE (p1))
363 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
364 memb; memb = TREE_CHAIN (memb))
365 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
367 TREE_VALUE (n) = TREE_VALUE (p1);
369 pedwarn ("function types not truly compatible in ISO C");
373 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
377 c_override_global_bindings_to_false = false;
378 t1 = build_function_type (valtype, newargs);
379 t1 = qualify_type (t1, t2);
380 /* ... falls through ... */
384 return build_type_attribute_variant (t1, attributes);
389 /* Return the type of a conditional expression between pointers to
390 possibly differently qualified versions of compatible types.
392 We assume that comp_target_types has already been done and returned
393 nonzero; if that isn't so, this may crash. */
396 common_pointer_type (tree t1, tree t2)
403 /* Save time if the two types are the same. */
405 if (t1 == t2) return t1;
407 /* If one type is nonsense, use the other. */
408 if (t1 == error_mark_node)
410 if (t2 == error_mark_node)
413 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
414 && TREE_CODE (t2) == POINTER_TYPE);
416 /* Merge the attributes. */
417 attributes = targetm.merge_type_attributes (t1, t2);
419 /* Find the composite type of the target types, and combine the
420 qualifiers of the two types' targets. */
421 pointed_to_1 = TREE_TYPE (t1);
422 pointed_to_2 = TREE_TYPE (t2);
423 target = composite_type (TYPE_MAIN_VARIANT (pointed_to_1),
424 TYPE_MAIN_VARIANT (pointed_to_2));
425 t1 = build_pointer_type (c_build_qualified_type
427 TYPE_QUALS (pointed_to_1) |
428 TYPE_QUALS (pointed_to_2)));
429 return build_type_attribute_variant (t1, attributes);
432 /* Return the common type for two arithmetic types under the usual
433 arithmetic conversions. The default conversions have already been
434 applied, and enumerated types converted to their compatible integer
435 types. The resulting type is unqualified and has no attributes.
437 This is the type for the result of most arithmetic operations
438 if the operands have the given two types. */
441 common_type (tree t1, tree t2)
443 enum tree_code code1;
444 enum tree_code code2;
446 /* If one type is nonsense, use the other. */
447 if (t1 == error_mark_node)
449 if (t2 == error_mark_node)
452 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
453 t1 = TYPE_MAIN_VARIANT (t1);
455 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
456 t2 = TYPE_MAIN_VARIANT (t2);
458 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
459 t1 = build_type_attribute_variant (t1, NULL_TREE);
461 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
462 t2 = build_type_attribute_variant (t2, NULL_TREE);
464 /* Save time if the two types are the same. */
466 if (t1 == t2) return t1;
468 code1 = TREE_CODE (t1);
469 code2 = TREE_CODE (t2);
471 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
472 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
473 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
474 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
476 /* If one type is a vector type, return that type. (How the usual
477 arithmetic conversions apply to the vector types extension is not
478 precisely specified.) */
479 if (code1 == VECTOR_TYPE)
482 if (code2 == VECTOR_TYPE)
485 /* If one type is complex, form the common type of the non-complex
486 components, then make that complex. Use T1 or T2 if it is the
488 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
490 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
491 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
492 tree subtype = common_type (subtype1, subtype2);
494 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
496 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
499 return build_complex_type (subtype);
502 /* If only one is real, use it as the result. */
504 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
507 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
510 /* Both real or both integers; use the one with greater precision. */
512 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
514 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
517 /* Same precision. Prefer long longs to longs to ints when the
518 same precision, following the C99 rules on integer type rank
519 (which are equivalent to the C90 rules for C90 types). */
521 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
522 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
523 return long_long_unsigned_type_node;
525 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
526 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
528 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
529 return long_long_unsigned_type_node;
531 return long_long_integer_type_node;
534 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
535 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
536 return long_unsigned_type_node;
538 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
539 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
541 /* But preserve unsignedness from the other type,
542 since long cannot hold all the values of an unsigned int. */
543 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
544 return long_unsigned_type_node;
546 return long_integer_type_node;
549 /* Likewise, prefer long double to double even if same size. */
550 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
551 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
552 return long_double_type_node;
554 /* Otherwise prefer the unsigned one. */
556 if (TYPE_UNSIGNED (t1))
562 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
563 or various other operations. Return 2 if they are compatible
564 but a warning may be needed if you use them together. */
567 comptypes (tree type1, tree type2)
573 /* Suppress errors caused by previously reported errors. */
575 if (t1 == t2 || !t1 || !t2
576 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
579 /* If either type is the internal version of sizetype, return the
581 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
582 && TYPE_ORIG_SIZE_TYPE (t1))
583 t1 = TYPE_ORIG_SIZE_TYPE (t1);
585 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
586 && TYPE_ORIG_SIZE_TYPE (t2))
587 t2 = TYPE_ORIG_SIZE_TYPE (t2);
590 /* Enumerated types are compatible with integer types, but this is
591 not transitive: two enumerated types in the same translation unit
592 are compatible with each other only if they are the same type. */
594 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
595 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
596 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
597 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
602 /* Different classes of types can't be compatible. */
604 if (TREE_CODE (t1) != TREE_CODE (t2))
607 /* Qualifiers must match. C99 6.7.3p9 */
609 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
612 /* Allow for two different type nodes which have essentially the same
613 definition. Note that we already checked for equality of the type
614 qualifiers (just above). */
616 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
619 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
620 if (! (attrval = targetm.comp_type_attributes (t1, t2)))
623 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
626 switch (TREE_CODE (t1))
629 /* We must give ObjC the first crack at comparing pointers, since
630 protocol qualifiers may be involved. */
631 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
633 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
634 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
638 val = function_types_compatible_p (t1, t2);
643 tree d1 = TYPE_DOMAIN (t1);
644 tree d2 = TYPE_DOMAIN (t2);
645 bool d1_variable, d2_variable;
646 bool d1_zero, d2_zero;
649 /* Target types must match incl. qualifiers. */
650 if (TREE_TYPE (t1) != TREE_TYPE (t2)
651 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
654 /* Sizes must match unless one is missing or variable. */
655 if (d1 == 0 || d2 == 0 || d1 == d2)
658 d1_zero = ! TYPE_MAX_VALUE (d1);
659 d2_zero = ! TYPE_MAX_VALUE (d2);
661 d1_variable = (! d1_zero
662 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
663 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
664 d2_variable = (! d2_zero
665 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
666 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
668 if (d1_variable || d2_variable)
670 if (d1_zero && d2_zero)
672 if (d1_zero || d2_zero
673 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
674 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
681 /* We are dealing with two distinct structs. In assorted Objective-C
682 corner cases, however, these can still be deemed equivalent. */
683 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
688 if (val != 1 && !same_translation_unit_p (t1, t2))
689 val = tagged_types_tu_compatible_p (t1, t2);
693 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
694 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
700 return attrval == 2 && val == 1 ? 2 : val;
703 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
704 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
705 to 1 or 0 depending if the check of the pointer types is meant to
706 be reflexive or not (typically, assignments are not reflexive,
707 while comparisons are reflexive).
711 comp_target_types (tree ttl, tree ttr, int reflexive)
715 /* Give objc_comptypes a crack at letting these types through. */
716 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
719 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
720 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
722 if (val == 2 && pedantic)
723 pedwarn ("types are not quite compatible");
727 /* Subroutines of `comptypes'. */
729 /* Determine whether two trees derive from the same translation unit.
730 If the CONTEXT chain ends in a null, that tree's context is still
731 being parsed, so if two trees have context chains ending in null,
732 they're in the same translation unit. */
734 same_translation_unit_p (tree t1, tree t2)
736 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
737 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
739 case 'd': t1 = DECL_CONTEXT (t1); break;
740 case 't': t1 = TYPE_CONTEXT (t1); break;
741 case 'x': t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
742 default: gcc_unreachable ();
745 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
746 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
748 case 'd': t2 = DECL_CONTEXT (t2); break;
749 case 't': t2 = TYPE_CONTEXT (t2); break;
750 case 'x': t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
751 default: gcc_unreachable ();
757 /* The C standard says that two structures in different translation
758 units are compatible with each other only if the types of their
759 fields are compatible (among other things). So, consider two copies
760 of this structure: */
762 struct tagged_tu_seen {
763 const struct tagged_tu_seen * next;
768 /* Can they be compatible with each other? We choose to break the
769 recursion by allowing those types to be compatible. */
771 static const struct tagged_tu_seen * tagged_tu_seen_base;
773 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
774 compatible. If the two types are not the same (which has been
775 checked earlier), this can only happen when multiple translation
776 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
780 tagged_types_tu_compatible_p (tree t1, tree t2)
783 bool needs_warning = false;
785 /* We have to verify that the tags of the types are the same. This
786 is harder than it looks because this may be a typedef, so we have
787 to go look at the original type. It may even be a typedef of a
789 In the case of compiler-created builtin structs the TYPE_DECL
790 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
791 while (TYPE_NAME (t1)
792 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
793 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
794 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
796 while (TYPE_NAME (t2)
797 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
798 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
799 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
801 /* C90 didn't have the requirement that the two tags be the same. */
802 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
805 /* C90 didn't say what happened if one or both of the types were
806 incomplete; we choose to follow C99 rules here, which is that they
808 if (TYPE_SIZE (t1) == NULL
809 || TYPE_SIZE (t2) == NULL)
813 const struct tagged_tu_seen * tts_i;
814 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
815 if (tts_i->t1 == t1 && tts_i->t2 == t2)
819 switch (TREE_CODE (t1))
824 /* Speed up the case where the type values are in the same order. */
825 tree tv1 = TYPE_VALUES (t1);
826 tree tv2 = TYPE_VALUES (t2);
831 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
833 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
835 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
839 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
841 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
844 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
847 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
849 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
851 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
859 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
862 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
865 struct tagged_tu_seen tts;
867 tts.next = tagged_tu_seen_base;
870 tagged_tu_seen_base = &tts;
872 if (DECL_NAME (s1) != NULL)
873 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
874 if (DECL_NAME (s1) == DECL_NAME (s2))
877 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
881 needs_warning = true;
883 if (TREE_CODE (s1) == FIELD_DECL
884 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
885 DECL_FIELD_BIT_OFFSET (s2)) != 1)
891 tagged_tu_seen_base = tts.next;
895 return needs_warning ? 2 : 1;
900 struct tagged_tu_seen tts;
902 tts.next = tagged_tu_seen_base;
905 tagged_tu_seen_base = &tts;
907 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
909 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
912 if (TREE_CODE (s1) != TREE_CODE (s2)
913 || DECL_NAME (s1) != DECL_NAME (s2))
915 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
919 needs_warning = true;
921 if (TREE_CODE (s1) == FIELD_DECL
922 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
923 DECL_FIELD_BIT_OFFSET (s2)) != 1)
926 tagged_tu_seen_base = tts.next;
929 return needs_warning ? 2 : 1;
937 /* Return 1 if two function types F1 and F2 are compatible.
938 If either type specifies no argument types,
939 the other must specify a fixed number of self-promoting arg types.
940 Otherwise, if one type specifies only the number of arguments,
941 the other must specify that number of self-promoting arg types.
942 Otherwise, the argument types must match. */
945 function_types_compatible_p (tree f1, tree f2)
948 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
953 ret1 = TREE_TYPE (f1);
954 ret2 = TREE_TYPE (f2);
956 /* 'volatile' qualifiers on a function's return type used to mean
957 the function is noreturn. */
958 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
959 pedwarn ("function return types not compatible due to `volatile'");
960 if (TYPE_VOLATILE (ret1))
961 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
962 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
963 if (TYPE_VOLATILE (ret2))
964 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
965 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
966 val = comptypes (ret1, ret2);
970 args1 = TYPE_ARG_TYPES (f1);
971 args2 = TYPE_ARG_TYPES (f2);
973 /* An unspecified parmlist matches any specified parmlist
974 whose argument types don't need default promotions. */
978 if (!self_promoting_args_p (args2))
980 /* If one of these types comes from a non-prototype fn definition,
981 compare that with the other type's arglist.
982 If they don't match, ask for a warning (but no error). */
983 if (TYPE_ACTUAL_ARG_TYPES (f1)
984 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
990 if (!self_promoting_args_p (args1))
992 if (TYPE_ACTUAL_ARG_TYPES (f2)
993 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
998 /* Both types have argument lists: compare them and propagate results. */
999 val1 = type_lists_compatible_p (args1, args2);
1000 return val1 != 1 ? val1 : val;
1003 /* Check two lists of types for compatibility,
1004 returning 0 for incompatible, 1 for compatible,
1005 or 2 for compatible with warning. */
1008 type_lists_compatible_p (tree args1, tree args2)
1010 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1016 if (args1 == 0 && args2 == 0)
1018 /* If one list is shorter than the other,
1019 they fail to match. */
1020 if (args1 == 0 || args2 == 0)
1022 /* A null pointer instead of a type
1023 means there is supposed to be an argument
1024 but nothing is specified about what type it has.
1025 So match anything that self-promotes. */
1026 if (TREE_VALUE (args1) == 0)
1028 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
1031 else if (TREE_VALUE (args2) == 0)
1033 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
1036 /* If one of the lists has an error marker, ignore this arg. */
1037 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
1038 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
1040 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
1041 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
1043 /* Allow wait (union {union wait *u; int *i} *)
1044 and wait (union wait *) to be compatible. */
1045 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
1046 && (TYPE_NAME (TREE_VALUE (args1)) == 0
1047 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
1048 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
1049 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
1050 TYPE_SIZE (TREE_VALUE (args2))))
1053 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
1054 memb; memb = TREE_CHAIN (memb))
1055 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
1060 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
1061 && (TYPE_NAME (TREE_VALUE (args2)) == 0
1062 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
1063 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
1064 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
1065 TYPE_SIZE (TREE_VALUE (args1))))
1068 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
1069 memb; memb = TREE_CHAIN (memb))
1070 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
1079 /* comptypes said ok, but record if it said to warn. */
1083 args1 = TREE_CHAIN (args1);
1084 args2 = TREE_CHAIN (args2);
1088 /* Compute the size to increment a pointer by. */
1091 c_size_in_bytes (tree type)
1093 enum tree_code code = TREE_CODE (type);
1095 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1096 return size_one_node;
1098 if (!COMPLETE_OR_VOID_TYPE_P (type))
1100 error ("arithmetic on pointer to an incomplete type");
1101 return size_one_node;
1104 /* Convert in case a char is more than one unit. */
1105 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1106 size_int (TYPE_PRECISION (char_type_node)
1110 /* Return either DECL or its known constant value (if it has one). */
1113 decl_constant_value (tree decl)
1115 if (/* Don't change a variable array bound or initial value to a constant
1116 in a place where a variable is invalid. Note that DECL_INITIAL
1117 isn't valid for a PARM_DECL. */
1118 current_function_decl != 0
1119 && TREE_CODE (decl) != PARM_DECL
1120 && ! TREE_THIS_VOLATILE (decl)
1121 && TREE_READONLY (decl)
1122 && DECL_INITIAL (decl) != 0
1123 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1124 /* This is invalid if initial value is not constant.
1125 If it has either a function call, a memory reference,
1126 or a variable, then re-evaluating it could give different results. */
1127 && TREE_CONSTANT (DECL_INITIAL (decl))
1128 /* Check for cases where this is sub-optimal, even though valid. */
1129 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1130 return DECL_INITIAL (decl);
1134 /* Return either DECL or its known constant value (if it has one), but
1135 return DECL if pedantic or DECL has mode BLKmode. This is for
1136 bug-compatibility with the old behavior of decl_constant_value
1137 (before GCC 3.0); every use of this function is a bug and it should
1138 be removed before GCC 3.1. It is not appropriate to use pedantic
1139 in a way that affects optimization, and BLKmode is probably not the
1140 right test for avoiding misoptimizations either. */
1143 decl_constant_value_for_broken_optimization (tree decl)
1145 if (pedantic || DECL_MODE (decl) == BLKmode)
1148 return decl_constant_value (decl);
1152 /* Perform the default conversion of arrays and functions to pointers.
1153 Return the result of converting EXP. For any other expression, just
1157 default_function_array_conversion (tree exp)
1160 tree type = TREE_TYPE (exp);
1161 enum tree_code code = TREE_CODE (type);
1164 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1167 Do not use STRIP_NOPS here! It will remove conversions from pointer
1168 to integer and cause infinite recursion. */
1170 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1171 || (TREE_CODE (exp) == NOP_EXPR
1172 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1174 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1176 exp = TREE_OPERAND (exp, 0);
1179 if (TREE_NO_WARNING (orig_exp))
1180 TREE_NO_WARNING (exp) = 1;
1182 if (code == FUNCTION_TYPE)
1184 return build_unary_op (ADDR_EXPR, exp, 0);
1186 if (code == ARRAY_TYPE)
1189 tree restype = TREE_TYPE (type);
1195 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1197 constp = TREE_READONLY (exp);
1198 volatilep = TREE_THIS_VOLATILE (exp);
1201 if (TYPE_QUALS (type) || constp || volatilep)
1203 = c_build_qualified_type (restype,
1205 | (constp * TYPE_QUAL_CONST)
1206 | (volatilep * TYPE_QUAL_VOLATILE));
1208 if (TREE_CODE (exp) == INDIRECT_REF)
1209 return convert (build_pointer_type (restype),
1210 TREE_OPERAND (exp, 0));
1212 if (TREE_CODE (exp) == COMPOUND_EXPR)
1214 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1215 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1216 TREE_OPERAND (exp, 0), op1);
1219 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1220 if (!flag_isoc99 && !lvalue_array_p)
1222 /* Before C99, non-lvalue arrays do not decay to pointers.
1223 Normally, using such an array would be invalid; but it can
1224 be used correctly inside sizeof or as a statement expression.
1225 Thus, do not give an error here; an error will result later. */
1229 ptrtype = build_pointer_type (restype);
1231 if (TREE_CODE (exp) == VAR_DECL)
1233 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1234 ADDR_EXPR because it's the best way of representing what
1235 happens in C when we take the address of an array and place
1236 it in a pointer to the element type. */
1237 adr = build1 (ADDR_EXPR, ptrtype, exp);
1238 if (!c_mark_addressable (exp))
1239 return error_mark_node;
1240 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1243 /* This way is better for a COMPONENT_REF since it can
1244 simplify the offset for a component. */
1245 adr = build_unary_op (ADDR_EXPR, exp, 1);
1246 return convert (ptrtype, adr);
1251 /* Perform default promotions for C data used in expressions.
1252 Arrays and functions are converted to pointers;
1253 enumeral types or short or char, to int.
1254 In addition, manifest constants symbols are replaced by their values. */
1257 default_conversion (tree exp)
1260 tree type = TREE_TYPE (exp);
1261 enum tree_code code = TREE_CODE (type);
1263 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1264 return default_function_array_conversion (exp);
1266 /* Constants can be used directly unless they're not loadable. */
1267 if (TREE_CODE (exp) == CONST_DECL)
1268 exp = DECL_INITIAL (exp);
1270 /* Replace a nonvolatile const static variable with its value unless
1271 it is an array, in which case we must be sure that taking the
1272 address of the array produces consistent results. */
1273 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1275 exp = decl_constant_value_for_broken_optimization (exp);
1276 type = TREE_TYPE (exp);
1279 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1282 Do not use STRIP_NOPS here! It will remove conversions from pointer
1283 to integer and cause infinite recursion. */
1285 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1286 || (TREE_CODE (exp) == NOP_EXPR
1287 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1288 exp = TREE_OPERAND (exp, 0);
1290 if (TREE_NO_WARNING (orig_exp))
1291 TREE_NO_WARNING (exp) = 1;
1293 /* Normally convert enums to int,
1294 but convert wide enums to something wider. */
1295 if (code == ENUMERAL_TYPE)
1297 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1298 TYPE_PRECISION (integer_type_node)),
1299 ((TYPE_PRECISION (type)
1300 >= TYPE_PRECISION (integer_type_node))
1301 && TYPE_UNSIGNED (type)));
1303 return convert (type, exp);
1306 if (TREE_CODE (exp) == COMPONENT_REF
1307 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1308 /* If it's thinner than an int, promote it like a
1309 c_promoting_integer_type_p, otherwise leave it alone. */
1310 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1311 TYPE_PRECISION (integer_type_node)))
1312 return convert (integer_type_node, exp);
1314 if (c_promoting_integer_type_p (type))
1316 /* Preserve unsignedness if not really getting any wider. */
1317 if (TYPE_UNSIGNED (type)
1318 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1319 return convert (unsigned_type_node, exp);
1321 return convert (integer_type_node, exp);
1324 if (code == VOID_TYPE)
1326 error ("void value not ignored as it ought to be");
1327 return error_mark_node;
1332 /* Look up COMPONENT in a structure or union DECL.
1334 If the component name is not found, returns NULL_TREE. Otherwise,
1335 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1336 stepping down the chain to the component, which is in the last
1337 TREE_VALUE of the list. Normally the list is of length one, but if
1338 the component is embedded within (nested) anonymous structures or
1339 unions, the list steps down the chain to the component. */
1342 lookup_field (tree decl, tree component)
1344 tree type = TREE_TYPE (decl);
1347 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1348 to the field elements. Use a binary search on this array to quickly
1349 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1350 will always be set for structures which have many elements. */
1352 if (TYPE_LANG_SPECIFIC (type))
1355 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1357 field = TYPE_FIELDS (type);
1359 top = TYPE_LANG_SPECIFIC (type)->s->len;
1360 while (top - bot > 1)
1362 half = (top - bot + 1) >> 1;
1363 field = field_array[bot+half];
1365 if (DECL_NAME (field) == NULL_TREE)
1367 /* Step through all anon unions in linear fashion. */
1368 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1370 field = field_array[bot++];
1371 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1372 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1374 tree anon = lookup_field (field, component);
1377 return tree_cons (NULL_TREE, field, anon);
1381 /* Entire record is only anon unions. */
1385 /* Restart the binary search, with new lower bound. */
1389 if (DECL_NAME (field) == component)
1391 if (DECL_NAME (field) < component)
1397 if (DECL_NAME (field_array[bot]) == component)
1398 field = field_array[bot];
1399 else if (DECL_NAME (field) != component)
1404 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1406 if (DECL_NAME (field) == NULL_TREE
1407 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1408 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1410 tree anon = lookup_field (field, component);
1413 return tree_cons (NULL_TREE, field, anon);
1416 if (DECL_NAME (field) == component)
1420 if (field == NULL_TREE)
1424 return tree_cons (NULL_TREE, field, NULL_TREE);
1427 /* Make an expression to refer to the COMPONENT field of
1428 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1431 build_component_ref (tree datum, tree component)
1433 tree type = TREE_TYPE (datum);
1434 enum tree_code code = TREE_CODE (type);
1438 if (!objc_is_public (datum, component))
1439 return error_mark_node;
1441 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1442 Ensure that the arguments are not lvalues; otherwise,
1443 if the component is an array, it would wrongly decay to a pointer in
1445 We cannot do this with a COND_EXPR, because in a conditional expression
1446 the default promotions are applied to both sides, and this would yield
1447 the wrong type of the result; for example, if the components have
1449 switch (TREE_CODE (datum))
1453 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1454 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
1455 TREE_OPERAND (datum, 0), non_lvalue (value));
1461 /* See if there is a field or component with name COMPONENT. */
1463 if (code == RECORD_TYPE || code == UNION_TYPE)
1465 if (!COMPLETE_TYPE_P (type))
1467 c_incomplete_type_error (NULL_TREE, type);
1468 return error_mark_node;
1471 field = lookup_field (datum, component);
1475 error ("%s has no member named `%s'",
1476 code == RECORD_TYPE ? "structure" : "union",
1477 IDENTIFIER_POINTER (component));
1478 return error_mark_node;
1481 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1482 This might be better solved in future the way the C++ front
1483 end does it - by giving the anonymous entities each a
1484 separate name and type, and then have build_component_ref
1485 recursively call itself. We can't do that here. */
1488 tree subdatum = TREE_VALUE (field);
1490 if (TREE_TYPE (subdatum) == error_mark_node)
1491 return error_mark_node;
1493 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1495 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1496 TREE_READONLY (ref) = 1;
1497 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1498 TREE_THIS_VOLATILE (ref) = 1;
1500 if (TREE_DEPRECATED (subdatum))
1501 warn_deprecated_use (subdatum);
1505 field = TREE_CHAIN (field);
1511 else if (code != ERROR_MARK)
1512 error ("request for member `%s' in something not a structure or union",
1513 IDENTIFIER_POINTER (component));
1515 return error_mark_node;
1518 /* Given an expression PTR for a pointer, return an expression
1519 for the value pointed to.
1520 ERRORSTRING is the name of the operator to appear in error messages. */
1523 build_indirect_ref (tree ptr, const char *errorstring)
1525 tree pointer = default_conversion (ptr);
1526 tree type = TREE_TYPE (pointer);
1528 if (TREE_CODE (type) == POINTER_TYPE)
1530 if (TREE_CODE (pointer) == ADDR_EXPR
1531 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1532 == TREE_TYPE (type)))
1533 return TREE_OPERAND (pointer, 0);
1536 tree t = TREE_TYPE (type);
1537 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1539 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1541 error ("dereferencing pointer to incomplete type");
1542 return error_mark_node;
1544 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1545 warning ("dereferencing `void *' pointer");
1547 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1548 so that we get the proper error message if the result is used
1549 to assign to. Also, &* is supposed to be a no-op.
1550 And ANSI C seems to specify that the type of the result
1551 should be the const type. */
1552 /* A de-reference of a pointer to const is not a const. It is valid
1553 to change it via some other pointer. */
1554 TREE_READONLY (ref) = TYPE_READONLY (t);
1555 TREE_SIDE_EFFECTS (ref)
1556 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1557 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1561 else if (TREE_CODE (pointer) != ERROR_MARK)
1562 error ("invalid type argument of `%s'", errorstring);
1563 return error_mark_node;
1566 /* This handles expressions of the form "a[i]", which denotes
1569 This is logically equivalent in C to *(a+i), but we may do it differently.
1570 If A is a variable or a member, we generate a primitive ARRAY_REF.
1571 This avoids forcing the array out of registers, and can work on
1572 arrays that are not lvalues (for example, members of structures returned
1576 build_array_ref (tree array, tree index)
1580 error ("subscript missing in array reference");
1581 return error_mark_node;
1584 if (TREE_TYPE (array) == error_mark_node
1585 || TREE_TYPE (index) == error_mark_node)
1586 return error_mark_node;
1588 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1592 /* Subscripting with type char is likely to lose
1593 on a machine where chars are signed.
1594 So warn on any machine, but optionally.
1595 Don't warn for unsigned char since that type is safe.
1596 Don't warn for signed char because anyone who uses that
1597 must have done so deliberately. */
1598 if (warn_char_subscripts
1599 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1600 warning ("array subscript has type `char'");
1602 /* Apply default promotions *after* noticing character types. */
1603 index = default_conversion (index);
1605 /* Require integer *after* promotion, for sake of enums. */
1606 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1608 error ("array subscript is not an integer");
1609 return error_mark_node;
1612 /* An array that is indexed by a non-constant
1613 cannot be stored in a register; we must be able to do
1614 address arithmetic on its address.
1615 Likewise an array of elements of variable size. */
1616 if (TREE_CODE (index) != INTEGER_CST
1617 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1618 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1620 if (!c_mark_addressable (array))
1621 return error_mark_node;
1623 /* An array that is indexed by a constant value which is not within
1624 the array bounds cannot be stored in a register either; because we
1625 would get a crash in store_bit_field/extract_bit_field when trying
1626 to access a non-existent part of the register. */
1627 if (TREE_CODE (index) == INTEGER_CST
1628 && TYPE_DOMAIN (TREE_TYPE (array))
1629 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1631 if (!c_mark_addressable (array))
1632 return error_mark_node;
1638 while (TREE_CODE (foo) == COMPONENT_REF)
1639 foo = TREE_OPERAND (foo, 0);
1640 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1641 pedwarn ("ISO C forbids subscripting `register' array");
1642 else if (! flag_isoc99 && ! lvalue_p (foo))
1643 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1646 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1647 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1648 /* Array ref is const/volatile if the array elements are
1649 or if the array is. */
1650 TREE_READONLY (rval)
1651 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1652 | TREE_READONLY (array));
1653 TREE_SIDE_EFFECTS (rval)
1654 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1655 | TREE_SIDE_EFFECTS (array));
1656 TREE_THIS_VOLATILE (rval)
1657 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1658 /* This was added by rms on 16 Nov 91.
1659 It fixes vol struct foo *a; a->elts[1]
1660 in an inline function.
1661 Hope it doesn't break something else. */
1662 | TREE_THIS_VOLATILE (array));
1663 return require_complete_type (fold (rval));
1667 tree ar = default_conversion (array);
1668 tree ind = default_conversion (index);
1670 /* Do the same warning check as above, but only on the part that's
1671 syntactically the index and only if it is also semantically
1673 if (warn_char_subscripts
1674 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1675 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1676 warning ("subscript has type `char'");
1678 /* Put the integer in IND to simplify error checking. */
1679 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1686 if (ar == error_mark_node)
1689 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1690 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1692 error ("subscripted value is neither array nor pointer");
1693 return error_mark_node;
1695 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1697 error ("array subscript is not an integer");
1698 return error_mark_node;
1701 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1706 /* Build an external reference to identifier ID. FUN indicates
1707 whether this will be used for a function call. */
1709 build_external_ref (tree id, int fun)
1712 tree decl = lookup_name (id);
1713 tree objc_ivar = objc_lookup_ivar (id);
1715 if (decl && decl != error_mark_node)
1717 /* Properly declared variable or function reference. */
1720 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1722 warning ("local declaration of `%s' hides instance variable",
1723 IDENTIFIER_POINTER (id));
1732 /* Implicit function declaration. */
1733 ref = implicitly_declare (id);
1734 else if (decl == error_mark_node)
1735 /* Don't complain about something that's already been
1736 complained about. */
1737 return error_mark_node;
1740 undeclared_variable (id);
1741 return error_mark_node;
1744 if (TREE_TYPE (ref) == error_mark_node)
1745 return error_mark_node;
1747 if (TREE_DEPRECATED (ref))
1748 warn_deprecated_use (ref);
1750 if (!skip_evaluation)
1751 assemble_external (ref);
1752 TREE_USED (ref) = 1;
1754 if (TREE_CODE (ref) == CONST_DECL)
1756 ref = DECL_INITIAL (ref);
1757 TREE_CONSTANT (ref) = 1;
1758 TREE_INVARIANT (ref) = 1;
1760 else if (current_function_decl != 0
1761 && !DECL_FILE_SCOPE_P (current_function_decl)
1762 && (TREE_CODE (ref) == VAR_DECL
1763 || TREE_CODE (ref) == PARM_DECL
1764 || TREE_CODE (ref) == FUNCTION_DECL))
1766 tree context = decl_function_context (ref);
1768 if (context != 0 && context != current_function_decl)
1769 DECL_NONLOCAL (ref) = 1;
1775 /* Build a function call to function FUNCTION with parameters PARAMS.
1776 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1777 TREE_VALUE of each node is a parameter-expression.
1778 FUNCTION's data type may be a function type or a pointer-to-function. */
1781 build_function_call (tree function, tree params)
1783 tree fntype, fundecl = 0;
1784 tree coerced_params;
1785 tree name = NULL_TREE, result;
1788 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1789 STRIP_TYPE_NOPS (function);
1791 /* Convert anything with function type to a pointer-to-function. */
1792 if (TREE_CODE (function) == FUNCTION_DECL)
1794 name = DECL_NAME (function);
1796 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1797 (because calling an inline function does not mean the function
1798 needs to be separately compiled). */
1799 fntype = build_type_variant (TREE_TYPE (function),
1800 TREE_READONLY (function),
1801 TREE_THIS_VOLATILE (function));
1803 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1806 function = default_conversion (function);
1808 fntype = TREE_TYPE (function);
1810 if (TREE_CODE (fntype) == ERROR_MARK)
1811 return error_mark_node;
1813 if (!(TREE_CODE (fntype) == POINTER_TYPE
1814 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1816 error ("called object is not a function");
1817 return error_mark_node;
1820 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1821 current_function_returns_abnormally = 1;
1823 /* fntype now gets the type of function pointed to. */
1824 fntype = TREE_TYPE (fntype);
1826 /* Check that the function is called through a compatible prototype.
1827 If it is not, replace the call by a trap, wrapped up in a compound
1828 expression if necessary. This has the nice side-effect to prevent
1829 the tree-inliner from generating invalid assignment trees which may
1830 blow up in the RTL expander later.
1832 ??? This doesn't work for Objective-C because objc_comptypes
1833 refuses to compare function prototypes, yet the compiler appears
1834 to build calls that are flagged as invalid by C's comptypes. */
1835 if (! c_dialect_objc ()
1836 && TREE_CODE (function) == NOP_EXPR
1837 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1838 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1839 && ! comptypes (fntype, TREE_TYPE (tem)))
1841 tree return_type = TREE_TYPE (fntype);
1842 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1845 /* This situation leads to run-time undefined behavior. We can't,
1846 therefore, simply error unless we can prove that all possible
1847 executions of the program must execute the code. */
1848 warning ("function called through a non-compatible type");
1850 /* We can, however, treat "undefined" any way we please.
1851 Call abort to encourage the user to fix the program. */
1852 inform ("if this code is reached, the program will abort");
1854 if (VOID_TYPE_P (return_type))
1860 if (AGGREGATE_TYPE_P (return_type))
1861 rhs = build_compound_literal (return_type,
1862 build_constructor (return_type,
1865 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1867 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
1871 /* Convert the parameters to the types declared in the
1872 function prototype, or apply default promotions. */
1875 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1877 /* Check that the arguments to the function are valid. */
1879 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1881 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
1882 function, coerced_params, NULL_TREE);
1883 TREE_SIDE_EFFECTS (result) = 1;
1885 if (require_constant_value)
1887 result = fold_initializer (result);
1889 if (TREE_CONSTANT (result)
1890 && (name == NULL_TREE
1891 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
1892 pedwarn_init ("initializer element is not constant");
1895 result = fold (result);
1897 if (VOID_TYPE_P (TREE_TYPE (result)))
1899 return require_complete_type (result);
1902 /* Convert the argument expressions in the list VALUES
1903 to the types in the list TYPELIST. The result is a list of converted
1904 argument expressions.
1906 If TYPELIST is exhausted, or when an element has NULL as its type,
1907 perform the default conversions.
1909 PARMLIST is the chain of parm decls for the function being called.
1910 It may be 0, if that info is not available.
1911 It is used only for generating error messages.
1913 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1915 This is also where warnings about wrong number of args are generated.
1917 Both VALUES and the returned value are chains of TREE_LIST nodes
1918 with the elements of the list in the TREE_VALUE slots of those nodes. */
1921 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1923 tree typetail, valtail;
1927 /* Scan the given expressions and types, producing individual
1928 converted arguments and pushing them on RESULT in reverse order. */
1930 for (valtail = values, typetail = typelist, parmnum = 0;
1932 valtail = TREE_CHAIN (valtail), parmnum++)
1934 tree type = typetail ? TREE_VALUE (typetail) : 0;
1935 tree val = TREE_VALUE (valtail);
1937 if (type == void_type_node)
1940 error ("too many arguments to function `%s'",
1941 IDENTIFIER_POINTER (name));
1943 error ("too many arguments to function");
1947 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1948 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1949 to convert automatically to a pointer. */
1950 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1951 val = TREE_OPERAND (val, 0);
1953 val = default_function_array_conversion (val);
1955 val = require_complete_type (val);
1959 /* Formal parm type is specified by a function prototype. */
1962 if (!COMPLETE_TYPE_P (type))
1964 error ("type of formal parameter %d is incomplete", parmnum + 1);
1969 /* Optionally warn about conversions that
1970 differ from the default conversions. */
1971 if (warn_conversion || warn_traditional)
1973 unsigned int formal_prec = TYPE_PRECISION (type);
1975 if (INTEGRAL_TYPE_P (type)
1976 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1977 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1978 if (INTEGRAL_TYPE_P (type)
1979 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1980 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1981 else if (TREE_CODE (type) == COMPLEX_TYPE
1982 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1983 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1984 else if (TREE_CODE (type) == REAL_TYPE
1985 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1986 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1987 else if (TREE_CODE (type) == COMPLEX_TYPE
1988 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1989 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1990 else if (TREE_CODE (type) == REAL_TYPE
1991 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1992 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1993 /* ??? At some point, messages should be written about
1994 conversions between complex types, but that's too messy
1996 else if (TREE_CODE (type) == REAL_TYPE
1997 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1999 /* Warn if any argument is passed as `float',
2000 since without a prototype it would be `double'. */
2001 if (formal_prec == TYPE_PRECISION (float_type_node))
2002 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
2004 /* Detect integer changing in width or signedness.
2005 These warnings are only activated with
2006 -Wconversion, not with -Wtraditional. */
2007 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2008 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2010 tree would_have_been = default_conversion (val);
2011 tree type1 = TREE_TYPE (would_have_been);
2013 if (TREE_CODE (type) == ENUMERAL_TYPE
2014 && (TYPE_MAIN_VARIANT (type)
2015 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2016 /* No warning if function asks for enum
2017 and the actual arg is that enum type. */
2019 else if (formal_prec != TYPE_PRECISION (type1))
2020 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
2021 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2023 /* Don't complain if the formal parameter type
2024 is an enum, because we can't tell now whether
2025 the value was an enum--even the same enum. */
2026 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2028 else if (TREE_CODE (val) == INTEGER_CST
2029 && int_fits_type_p (val, type))
2030 /* Change in signedness doesn't matter
2031 if a constant value is unaffected. */
2033 /* Likewise for a constant in a NOP_EXPR. */
2034 else if (TREE_CODE (val) == NOP_EXPR
2035 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2036 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2038 /* If the value is extended from a narrower
2039 unsigned type, it doesn't matter whether we
2040 pass it as signed or unsigned; the value
2041 certainly is the same either way. */
2042 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2043 && TYPE_UNSIGNED (TREE_TYPE (val)))
2045 else if (TYPE_UNSIGNED (type))
2046 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
2048 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
2052 parmval = convert_for_assignment (type, val,
2053 (char *) 0, /* arg passing */
2054 fundecl, name, parmnum + 1);
2056 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2057 && INTEGRAL_TYPE_P (type)
2058 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2059 parmval = default_conversion (parmval);
2061 result = tree_cons (NULL_TREE, parmval, result);
2063 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2064 && (TYPE_PRECISION (TREE_TYPE (val))
2065 < TYPE_PRECISION (double_type_node)))
2066 /* Convert `float' to `double'. */
2067 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2069 /* Convert `short' and `char' to full-size `int'. */
2070 result = tree_cons (NULL_TREE, default_conversion (val), result);
2073 typetail = TREE_CHAIN (typetail);
2076 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2079 error ("too few arguments to function `%s'",
2080 IDENTIFIER_POINTER (name));
2082 error ("too few arguments to function");
2085 return nreverse (result);
2088 /* This is the entry point used by the parser
2089 for binary operators in the input.
2090 In addition to constructing the expression,
2091 we check for operands that were written with other binary operators
2092 in a way that is likely to confuse the user. */
2095 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2098 struct c_expr result;
2100 enum tree_code code1 = arg1.original_code;
2101 enum tree_code code2 = arg2.original_code;
2103 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2104 result.original_code = code;
2106 if (TREE_CODE (result.value) == ERROR_MARK)
2109 /* Check for cases such as x+y<<z which users are likely
2111 if (warn_parentheses)
2113 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2115 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2116 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2117 warning ("suggest parentheses around + or - inside shift");
2120 if (code == TRUTH_ORIF_EXPR)
2122 if (code1 == TRUTH_ANDIF_EXPR
2123 || code2 == TRUTH_ANDIF_EXPR)
2124 warning ("suggest parentheses around && within ||");
2127 if (code == BIT_IOR_EXPR)
2129 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2130 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2131 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2132 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2133 warning ("suggest parentheses around arithmetic in operand of |");
2134 /* Check cases like x|y==z */
2135 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2136 warning ("suggest parentheses around comparison in operand of |");
2139 if (code == BIT_XOR_EXPR)
2141 if (code1 == BIT_AND_EXPR
2142 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2143 || code2 == BIT_AND_EXPR
2144 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2145 warning ("suggest parentheses around arithmetic in operand of ^");
2146 /* Check cases like x^y==z */
2147 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2148 warning ("suggest parentheses around comparison in operand of ^");
2151 if (code == BIT_AND_EXPR)
2153 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2154 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2155 warning ("suggest parentheses around + or - in operand of &");
2156 /* Check cases like x&y==z */
2157 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2158 warning ("suggest parentheses around comparison in operand of &");
2160 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2161 if (TREE_CODE_CLASS (code) == '<'
2162 && (TREE_CODE_CLASS (code1) == '<'
2163 || TREE_CODE_CLASS (code2) == '<'))
2164 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2168 unsigned_conversion_warning (result.value, arg1.value);
2169 unsigned_conversion_warning (result.value, arg2.value);
2170 overflow_warning (result.value);
2175 /* Return a tree for the difference of pointers OP0 and OP1.
2176 The resulting tree has type int. */
2179 pointer_diff (tree op0, tree op1)
2181 tree restype = ptrdiff_type_node;
2183 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2184 tree con0, con1, lit0, lit1;
2185 tree orig_op1 = op1;
2187 if (pedantic || warn_pointer_arith)
2189 if (TREE_CODE (target_type) == VOID_TYPE)
2190 pedwarn ("pointer of type `void *' used in subtraction");
2191 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2192 pedwarn ("pointer to a function used in subtraction");
2195 /* If the conversion to ptrdiff_type does anything like widening or
2196 converting a partial to an integral mode, we get a convert_expression
2197 that is in the way to do any simplifications.
2198 (fold-const.c doesn't know that the extra bits won't be needed.
2199 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2200 different mode in place.)
2201 So first try to find a common term here 'by hand'; we want to cover
2202 at least the cases that occur in legal static initializers. */
2203 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2204 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2206 if (TREE_CODE (con0) == PLUS_EXPR)
2208 lit0 = TREE_OPERAND (con0, 1);
2209 con0 = TREE_OPERAND (con0, 0);
2212 lit0 = integer_zero_node;
2214 if (TREE_CODE (con1) == PLUS_EXPR)
2216 lit1 = TREE_OPERAND (con1, 1);
2217 con1 = TREE_OPERAND (con1, 0);
2220 lit1 = integer_zero_node;
2222 if (operand_equal_p (con0, con1, 0))
2229 /* First do the subtraction as integers;
2230 then drop through to build the divide operator.
2231 Do not do default conversions on the minus operator
2232 in case restype is a short type. */
2234 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2235 convert (restype, op1), 0);
2236 /* This generates an error if op1 is pointer to incomplete type. */
2237 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2238 error ("arithmetic on pointer to an incomplete type");
2240 /* This generates an error if op0 is pointer to incomplete type. */
2241 op1 = c_size_in_bytes (target_type);
2243 /* Divide by the size, in easiest possible way. */
2244 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2247 /* Construct and perhaps optimize a tree representation
2248 for a unary operation. CODE, a tree_code, specifies the operation
2249 and XARG is the operand.
2250 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2251 the default promotions (such as from short to int).
2252 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2253 allows non-lvalues; this is only used to handle conversion of non-lvalue
2254 arrays to pointers in C99. */
2257 build_unary_op (enum tree_code code, tree xarg, int flag)
2259 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2262 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2264 int noconvert = flag;
2266 if (typecode == ERROR_MARK)
2267 return error_mark_node;
2268 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2269 typecode = INTEGER_TYPE;
2274 /* This is used for unary plus, because a CONVERT_EXPR
2275 is enough to prevent anybody from looking inside for
2276 associativity, but won't generate any code. */
2277 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2278 || typecode == COMPLEX_TYPE
2279 || typecode == VECTOR_TYPE))
2281 error ("wrong type argument to unary plus");
2282 return error_mark_node;
2284 else if (!noconvert)
2285 arg = default_conversion (arg);
2286 arg = non_lvalue (arg);
2290 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2291 || typecode == COMPLEX_TYPE
2292 || typecode == VECTOR_TYPE))
2294 error ("wrong type argument to unary minus");
2295 return error_mark_node;
2297 else if (!noconvert)
2298 arg = default_conversion (arg);
2302 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2305 arg = default_conversion (arg);
2307 else if (typecode == COMPLEX_TYPE)
2311 pedwarn ("ISO C does not support `~' for complex conjugation");
2313 arg = default_conversion (arg);
2317 error ("wrong type argument to bit-complement");
2318 return error_mark_node;
2323 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2325 error ("wrong type argument to abs");
2326 return error_mark_node;
2328 else if (!noconvert)
2329 arg = default_conversion (arg);
2333 /* Conjugating a real value is a no-op, but allow it anyway. */
2334 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2335 || typecode == COMPLEX_TYPE))
2337 error ("wrong type argument to conjugation");
2338 return error_mark_node;
2340 else if (!noconvert)
2341 arg = default_conversion (arg);
2344 case TRUTH_NOT_EXPR:
2345 if (typecode != INTEGER_TYPE
2346 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2347 && typecode != COMPLEX_TYPE
2348 /* These will convert to a pointer. */
2349 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2351 error ("wrong type argument to unary exclamation mark");
2352 return error_mark_node;
2354 arg = lang_hooks.truthvalue_conversion (arg);
2355 return invert_truthvalue (arg);
2361 if (TREE_CODE (arg) == COMPLEX_CST)
2362 return TREE_REALPART (arg);
2363 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2364 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2369 if (TREE_CODE (arg) == COMPLEX_CST)
2370 return TREE_IMAGPART (arg);
2371 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2372 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2374 return convert (TREE_TYPE (arg), integer_zero_node);
2376 case PREINCREMENT_EXPR:
2377 case POSTINCREMENT_EXPR:
2378 case PREDECREMENT_EXPR:
2379 case POSTDECREMENT_EXPR:
2381 /* Increment or decrement the real part of the value,
2382 and don't change the imaginary part. */
2383 if (typecode == COMPLEX_TYPE)
2388 pedwarn ("ISO C does not support `++' and `--' on complex types");
2390 arg = stabilize_reference (arg);
2391 real = build_unary_op (REALPART_EXPR, arg, 1);
2392 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2393 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2394 build_unary_op (code, real, 1), imag);
2397 /* Report invalid types. */
2399 if (typecode != POINTER_TYPE
2400 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2402 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2403 error ("wrong type argument to increment");
2405 error ("wrong type argument to decrement");
2407 return error_mark_node;
2412 tree result_type = TREE_TYPE (arg);
2414 arg = get_unwidened (arg, 0);
2415 argtype = TREE_TYPE (arg);
2417 /* Compute the increment. */
2419 if (typecode == POINTER_TYPE)
2421 /* If pointer target is an undefined struct,
2422 we just cannot know how to do the arithmetic. */
2423 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2425 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2426 error ("increment of pointer to unknown structure");
2428 error ("decrement of pointer to unknown structure");
2430 else if ((pedantic || warn_pointer_arith)
2431 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2432 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2434 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2435 pedwarn ("wrong type argument to increment");
2437 pedwarn ("wrong type argument to decrement");
2440 inc = c_size_in_bytes (TREE_TYPE (result_type));
2443 inc = integer_one_node;
2445 inc = convert (argtype, inc);
2447 /* Complain about anything else that is not a true lvalue. */
2448 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2449 || code == POSTINCREMENT_EXPR)
2450 ? "invalid lvalue in increment"
2451 : "invalid lvalue in decrement")))
2452 return error_mark_node;
2454 /* Report a read-only lvalue. */
2455 if (TREE_READONLY (arg))
2456 readonly_error (arg,
2457 ((code == PREINCREMENT_EXPR
2458 || code == POSTINCREMENT_EXPR)
2459 ? "increment" : "decrement"));
2461 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2462 val = boolean_increment (code, arg);
2464 val = build2 (code, TREE_TYPE (arg), arg, inc);
2465 TREE_SIDE_EFFECTS (val) = 1;
2466 val = convert (result_type, val);
2467 if (TREE_CODE (val) != code)
2468 TREE_NO_WARNING (val) = 1;
2473 /* Note that this operation never does default_conversion. */
2475 /* Let &* cancel out to simplify resulting code. */
2476 if (TREE_CODE (arg) == INDIRECT_REF)
2478 /* Don't let this be an lvalue. */
2479 if (lvalue_p (TREE_OPERAND (arg, 0)))
2480 return non_lvalue (TREE_OPERAND (arg, 0));
2481 return TREE_OPERAND (arg, 0);
2484 /* For &x[y], return x+y */
2485 if (TREE_CODE (arg) == ARRAY_REF)
2487 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2488 return error_mark_node;
2489 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2490 TREE_OPERAND (arg, 1), 1);
2493 /* Anything not already handled and not a true memory reference
2494 or a non-lvalue array is an error. */
2495 else if (typecode != FUNCTION_TYPE && !flag
2496 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2497 return error_mark_node;
2499 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2500 argtype = TREE_TYPE (arg);
2502 /* If the lvalue is const or volatile, merge that into the type
2503 to which the address will point. Note that you can't get a
2504 restricted pointer by taking the address of something, so we
2505 only have to deal with `const' and `volatile' here. */
2506 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2507 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2508 argtype = c_build_type_variant (argtype,
2509 TREE_READONLY (arg),
2510 TREE_THIS_VOLATILE (arg));
2512 if (!c_mark_addressable (arg))
2513 return error_mark_node;
2515 if (TREE_CODE (arg) == COMPONENT_REF
2516 && DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
2518 error ("attempt to take address of bit-field structure member `%D'",
2519 TREE_OPERAND (arg, 1));
2520 return error_mark_node;
2523 argtype = build_pointer_type (argtype);
2524 val = build1 (ADDR_EXPR, argtype, arg);
2526 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2527 TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
2536 argtype = TREE_TYPE (arg);
2537 val = build1 (code, argtype, arg);
2538 return require_constant_value ? fold_initializer (val) : fold (val);
2541 /* Return nonzero if REF is an lvalue valid for this language.
2542 Lvalues can be assigned, unless their type has TYPE_READONLY.
2543 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2548 enum tree_code code = TREE_CODE (ref);
2555 return lvalue_p (TREE_OPERAND (ref, 0));
2557 case COMPOUND_LITERAL_EXPR:
2567 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2568 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2571 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2578 /* Return nonzero if REF is an lvalue valid for this language;
2579 otherwise, print an error message and return zero. */
2582 lvalue_or_else (tree ref, const char *msgid)
2584 int win = lvalue_p (ref);
2587 error ("%s", msgid);
2593 /* Warn about storing in something that is `const'. */
2596 readonly_error (tree arg, const char *msgid)
2598 if (TREE_CODE (arg) == COMPONENT_REF)
2600 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2601 readonly_error (TREE_OPERAND (arg, 0), msgid);
2603 error ("%s of read-only member `%s'", _(msgid),
2604 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2606 else if (TREE_CODE (arg) == VAR_DECL)
2607 error ("%s of read-only variable `%s'", _(msgid),
2608 IDENTIFIER_POINTER (DECL_NAME (arg)));
2610 error ("%s of read-only location", _(msgid));
2613 /* Mark EXP saying that we need to be able to take the
2614 address of it; it should not be allocated in a register.
2615 Returns true if successful. */
2618 c_mark_addressable (tree exp)
2623 switch (TREE_CODE (x))
2626 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2628 error ("cannot take address of bit-field `%s'",
2629 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2633 /* ... fall through ... */
2639 x = TREE_OPERAND (x, 0);
2642 case COMPOUND_LITERAL_EXPR:
2644 TREE_ADDRESSABLE (x) = 1;
2651 if (C_DECL_REGISTER (x)
2652 && DECL_NONLOCAL (x))
2654 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2656 error ("global register variable `%s' used in nested function",
2657 IDENTIFIER_POINTER (DECL_NAME (x)));
2660 pedwarn ("register variable `%s' used in nested function",
2661 IDENTIFIER_POINTER (DECL_NAME (x)));
2663 else if (C_DECL_REGISTER (x))
2665 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2667 error ("address of global register variable `%s' requested",
2668 IDENTIFIER_POINTER (DECL_NAME (x)));
2672 pedwarn ("address of register variable `%s' requested",
2673 IDENTIFIER_POINTER (DECL_NAME (x)));
2678 TREE_ADDRESSABLE (x) = 1;
2685 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2688 build_conditional_expr (tree ifexp, tree op1, tree op2)
2692 enum tree_code code1;
2693 enum tree_code code2;
2694 tree result_type = NULL;
2695 tree orig_op1 = op1, orig_op2 = op2;
2697 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2699 /* Promote both alternatives. */
2701 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2702 op1 = default_conversion (op1);
2703 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2704 op2 = default_conversion (op2);
2706 if (TREE_CODE (ifexp) == ERROR_MARK
2707 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2708 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2709 return error_mark_node;
2711 type1 = TREE_TYPE (op1);
2712 code1 = TREE_CODE (type1);
2713 type2 = TREE_TYPE (op2);
2714 code2 = TREE_CODE (type2);
2716 /* C90 does not permit non-lvalue arrays in conditional expressions.
2717 In C99 they will be pointers by now. */
2718 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2720 error ("non-lvalue array in conditional expression");
2721 return error_mark_node;
2724 /* Quickly detect the usual case where op1 and op2 have the same type
2726 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2729 result_type = type1;
2731 result_type = TYPE_MAIN_VARIANT (type1);
2733 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2734 || code1 == COMPLEX_TYPE)
2735 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2736 || code2 == COMPLEX_TYPE))
2738 result_type = common_type (type1, type2);
2740 /* If -Wsign-compare, warn here if type1 and type2 have
2741 different signedness. We'll promote the signed to unsigned
2742 and later code won't know it used to be different.
2743 Do this check on the original types, so that explicit casts
2744 will be considered, but default promotions won't. */
2745 if (warn_sign_compare && !skip_evaluation)
2747 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2748 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2750 if (unsigned_op1 ^ unsigned_op2)
2752 /* Do not warn if the result type is signed, since the
2753 signed type will only be chosen if it can represent
2754 all the values of the unsigned type. */
2755 if (! TYPE_UNSIGNED (result_type))
2757 /* Do not warn if the signed quantity is an unsuffixed
2758 integer literal (or some static constant expression
2759 involving such literals) and it is non-negative. */
2760 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2761 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
2764 warning ("signed and unsigned type in conditional expression");
2768 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2770 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2771 pedwarn ("ISO C forbids conditional expr with only one void side");
2772 result_type = void_type_node;
2774 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2776 if (comp_target_types (type1, type2, 1))
2777 result_type = common_pointer_type (type1, type2);
2778 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2779 && TREE_CODE (orig_op1) != NOP_EXPR)
2780 result_type = qualify_type (type2, type1);
2781 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2782 && TREE_CODE (orig_op2) != NOP_EXPR)
2783 result_type = qualify_type (type1, type2);
2784 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2786 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2787 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2788 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2789 TREE_TYPE (type2)));
2791 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2793 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2794 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2795 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2796 TREE_TYPE (type1)));
2800 pedwarn ("pointer type mismatch in conditional expression");
2801 result_type = build_pointer_type (void_type_node);
2804 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2806 if (! integer_zerop (op2))
2807 pedwarn ("pointer/integer type mismatch in conditional expression");
2810 op2 = null_pointer_node;
2812 result_type = type1;
2814 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2816 if (!integer_zerop (op1))
2817 pedwarn ("pointer/integer type mismatch in conditional expression");
2820 op1 = null_pointer_node;
2822 result_type = type2;
2827 if (flag_cond_mismatch)
2828 result_type = void_type_node;
2831 error ("type mismatch in conditional expression");
2832 return error_mark_node;
2836 /* Merge const and volatile flags of the incoming types. */
2838 = build_type_variant (result_type,
2839 TREE_READONLY (op1) || TREE_READONLY (op2),
2840 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2842 if (result_type != TREE_TYPE (op1))
2843 op1 = convert_and_check (result_type, op1);
2844 if (result_type != TREE_TYPE (op2))
2845 op2 = convert_and_check (result_type, op2);
2847 if (TREE_CODE (ifexp) == INTEGER_CST)
2848 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2850 return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2));
2853 /* Return a compound expression that performs two expressions and
2854 returns the value of the second of them. */
2857 build_compound_expr (tree expr1, tree expr2)
2859 /* Convert arrays and functions to pointers. */
2860 expr2 = default_function_array_conversion (expr2);
2862 /* Don't let (0, 0) be null pointer constant. */
2863 if (integer_zerop (expr2))
2864 expr2 = non_lvalue (expr2);
2866 if (! TREE_SIDE_EFFECTS (expr1))
2868 /* The left-hand operand of a comma expression is like an expression
2869 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2870 any side-effects, unless it was explicitly cast to (void). */
2871 if (warn_unused_value
2872 && ! (TREE_CODE (expr1) == CONVERT_EXPR
2873 && VOID_TYPE_P (TREE_TYPE (expr1))))
2874 warning ("left-hand operand of comma expression has no effect");
2877 /* With -Wunused, we should also warn if the left-hand operand does have
2878 side-effects, but computes a value which is not used. For example, in
2879 `foo() + bar(), baz()' the result of the `+' operator is not used,
2880 so we should issue a warning. */
2881 else if (warn_unused_value)
2882 warn_if_unused_value (expr1, input_location);
2884 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
2887 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2890 build_c_cast (tree type, tree expr)
2894 if (type == error_mark_node || expr == error_mark_node)
2895 return error_mark_node;
2897 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2898 only in <protocol> qualifications. But when constructing cast expressions,
2899 the protocols do matter and must be kept around. */
2900 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
2901 return build1 (NOP_EXPR, type, expr);
2903 type = TYPE_MAIN_VARIANT (type);
2905 if (TREE_CODE (type) == ARRAY_TYPE)
2907 error ("cast specifies array type");
2908 return error_mark_node;
2911 if (TREE_CODE (type) == FUNCTION_TYPE)
2913 error ("cast specifies function type");
2914 return error_mark_node;
2917 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2921 if (TREE_CODE (type) == RECORD_TYPE
2922 || TREE_CODE (type) == UNION_TYPE)
2923 pedwarn ("ISO C forbids casting nonscalar to the same type");
2926 else if (TREE_CODE (type) == UNION_TYPE)
2929 value = default_function_array_conversion (value);
2931 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2932 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2933 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
2941 pedwarn ("ISO C forbids casts to union type");
2942 t = digest_init (type,
2943 build_constructor (type,
2944 build_tree_list (field, value)),
2946 TREE_CONSTANT (t) = TREE_CONSTANT (value);
2947 TREE_INVARIANT (t) = TREE_INVARIANT (value);
2950 error ("cast to union type from type not present in union");
2951 return error_mark_node;
2957 /* If casting to void, avoid the error that would come
2958 from default_conversion in the case of a non-lvalue array. */
2959 if (type == void_type_node)
2960 return build1 (CONVERT_EXPR, type, value);
2962 /* Convert functions and arrays to pointers,
2963 but don't convert any other types. */
2964 value = default_function_array_conversion (value);
2965 otype = TREE_TYPE (value);
2967 /* Optionally warn about potentially worrisome casts. */
2970 && TREE_CODE (type) == POINTER_TYPE
2971 && TREE_CODE (otype) == POINTER_TYPE)
2973 tree in_type = type;
2974 tree in_otype = otype;
2978 /* Check that the qualifiers on IN_TYPE are a superset of
2979 the qualifiers of IN_OTYPE. The outermost level of
2980 POINTER_TYPE nodes is uninteresting and we stop as soon
2981 as we hit a non-POINTER_TYPE node on either type. */
2984 in_otype = TREE_TYPE (in_otype);
2985 in_type = TREE_TYPE (in_type);
2987 /* GNU C allows cv-qualified function types. 'const'
2988 means the function is very pure, 'volatile' means it
2989 can't return. We need to warn when such qualifiers
2990 are added, not when they're taken away. */
2991 if (TREE_CODE (in_otype) == FUNCTION_TYPE
2992 && TREE_CODE (in_type) == FUNCTION_TYPE)
2993 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
2995 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
2997 while (TREE_CODE (in_type) == POINTER_TYPE
2998 && TREE_CODE (in_otype) == POINTER_TYPE);
3001 warning ("cast adds new qualifiers to function type");
3004 /* There are qualifiers present in IN_OTYPE that are not
3005 present in IN_TYPE. */
3006 warning ("cast discards qualifiers from pointer target type");
3009 /* Warn about possible alignment problems. */
3010 if (STRICT_ALIGNMENT && warn_cast_align
3011 && TREE_CODE (type) == POINTER_TYPE
3012 && TREE_CODE (otype) == POINTER_TYPE
3013 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3014 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3015 /* Don't warn about opaque types, where the actual alignment
3016 restriction is unknown. */
3017 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3018 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3019 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3020 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3021 warning ("cast increases required alignment of target type");
3023 if (TREE_CODE (type) == INTEGER_TYPE
3024 && TREE_CODE (otype) == POINTER_TYPE
3025 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3026 && !TREE_CONSTANT (value))
3027 warning ("cast from pointer to integer of different size");
3029 if (warn_bad_function_cast
3030 && TREE_CODE (value) == CALL_EXPR
3031 && TREE_CODE (type) != TREE_CODE (otype))
3032 warning ("cast does not match function type");
3034 if (TREE_CODE (type) == POINTER_TYPE
3035 && TREE_CODE (otype) == INTEGER_TYPE
3036 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3037 /* Don't warn about converting any constant. */
3038 && !TREE_CONSTANT (value))
3039 warning ("cast to pointer from integer of different size");
3041 if (TREE_CODE (type) == POINTER_TYPE
3042 && TREE_CODE (otype) == POINTER_TYPE
3043 && TREE_CODE (expr) == ADDR_EXPR
3044 && DECL_P (TREE_OPERAND (expr, 0))
3045 && flag_strict_aliasing && warn_strict_aliasing
3046 && !VOID_TYPE_P (TREE_TYPE (type)))
3048 /* Casting the address of a decl to non void pointer. Warn
3049 if the cast breaks type based aliasing. */
3050 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3051 warning ("type-punning to incomplete type might break strict-aliasing rules");
3054 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3055 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3057 if (!alias_sets_conflict_p (set1, set2))
3058 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3059 else if (warn_strict_aliasing > 1
3060 && !alias_sets_might_conflict_p (set1, set2))
3061 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3065 /* If pedantic, warn for conversions between function and object
3066 pointer types, except for converting a null pointer constant
3067 to function pointer type. */
3069 && TREE_CODE (type) == POINTER_TYPE
3070 && TREE_CODE (otype) == POINTER_TYPE
3071 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3072 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3073 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3076 && TREE_CODE (type) == POINTER_TYPE
3077 && TREE_CODE (otype) == POINTER_TYPE
3078 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3079 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3080 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3081 && TREE_CODE (expr) != NOP_EXPR))
3082 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3085 /* Replace a nonvolatile const static variable with its value. */
3086 if (optimize && TREE_CODE (value) == VAR_DECL)
3087 value = decl_constant_value (value);
3088 value = convert (type, value);
3090 /* Ignore any integer overflow caused by the cast. */
3091 if (TREE_CODE (value) == INTEGER_CST)
3093 if (EXPR_P (ovalue))
3094 /* If OVALUE had overflow set, then so will VALUE, so it
3095 is safe to overwrite. */
3096 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3098 TREE_OVERFLOW (value) = 0;
3100 if (TREE_CODE_CLASS (TREE_CODE (ovalue)) == 'c')
3101 /* Similarly, constant_overflow cannot have become
3103 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3107 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3108 if (TREE_CODE (value) == INTEGER_CST
3109 && TREE_CODE (expr) == INTEGER_CST
3110 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3111 value = non_lvalue (value);
3113 /* Don't let a cast be an lvalue. */
3115 value = non_lvalue (value);
3120 /* Interpret a cast of expression EXPR to type TYPE. */
3122 c_cast_expr (tree type, tree expr)
3124 int saved_wsp = warn_strict_prototypes;
3126 /* This avoids warnings about unprototyped casts on
3127 integers. E.g. "#define SIG_DFL (void(*)())0". */
3128 if (TREE_CODE (expr) == INTEGER_CST)
3129 warn_strict_prototypes = 0;
3130 type = groktypename (type);
3131 warn_strict_prototypes = saved_wsp;
3133 return build_c_cast (type, expr);
3137 /* Build an assignment expression of lvalue LHS from value RHS.
3138 MODIFYCODE is the code for a binary operator that we use
3139 to combine the old value of LHS with RHS to get the new value.
3140 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3143 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3147 tree lhstype = TREE_TYPE (lhs);
3148 tree olhstype = lhstype;
3150 /* Types that aren't fully specified cannot be used in assignments. */
3151 lhs = require_complete_type (lhs);
3153 /* Avoid duplicate error messages from operands that had errors. */
3154 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3155 return error_mark_node;
3157 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3158 /* Do not use STRIP_NOPS here. We do not want an enumerator
3159 whose value is 0 to count as a null pointer constant. */
3160 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3161 rhs = TREE_OPERAND (rhs, 0);
3165 /* If a binary op has been requested, combine the old LHS value with the RHS
3166 producing the value we should actually store into the LHS. */
3168 if (modifycode != NOP_EXPR)
3170 lhs = stabilize_reference (lhs);
3171 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3174 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3175 return error_mark_node;
3177 /* Warn about storing in something that is `const'. */
3179 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3180 || ((TREE_CODE (lhstype) == RECORD_TYPE
3181 || TREE_CODE (lhstype) == UNION_TYPE)
3182 && C_TYPE_FIELDS_READONLY (lhstype)))
3183 readonly_error (lhs, "assignment");
3185 /* If storing into a structure or union member,
3186 it has probably been given type `int'.
3187 Compute the type that would go with
3188 the actual amount of storage the member occupies. */
3190 if (TREE_CODE (lhs) == COMPONENT_REF
3191 && (TREE_CODE (lhstype) == INTEGER_TYPE
3192 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3193 || TREE_CODE (lhstype) == REAL_TYPE
3194 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3195 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3197 /* If storing in a field that is in actuality a short or narrower than one,
3198 we must store in the field in its actual type. */
3200 if (lhstype != TREE_TYPE (lhs))
3202 lhs = copy_node (lhs);
3203 TREE_TYPE (lhs) = lhstype;
3206 /* Convert new value to destination type. */
3208 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3209 NULL_TREE, NULL_TREE, 0);
3210 if (TREE_CODE (newrhs) == ERROR_MARK)
3211 return error_mark_node;
3215 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3216 TREE_SIDE_EFFECTS (result) = 1;
3218 /* If we got the LHS in a different type for storing in,
3219 convert the result back to the nominal type of LHS
3220 so that the value we return always has the same type
3221 as the LHS argument. */
3223 if (olhstype == TREE_TYPE (result))
3225 return convert_for_assignment (olhstype, result, _("assignment"),
3226 NULL_TREE, NULL_TREE, 0);
3229 /* Convert value RHS to type TYPE as preparation for an assignment
3230 to an lvalue of type TYPE.
3231 The real work of conversion is done by `convert'.
3232 The purpose of this function is to generate error messages
3233 for assignments that are not allowed in C.
3234 ERRTYPE is a string to use in error messages:
3235 "assignment", "return", etc. If it is null, this is parameter passing
3236 for a function call (and different error messages are output).
3238 FUNNAME is the name of the function being called,
3239 as an IDENTIFIER_NODE, or null.
3240 PARMNUM is the number of the argument, for printing in error messages. */
3243 convert_for_assignment (tree type, tree rhs, const char *errtype,
3244 tree fundecl, tree funname, int parmnum)
3246 enum tree_code codel = TREE_CODE (type);
3248 enum tree_code coder;
3250 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3251 /* Do not use STRIP_NOPS here. We do not want an enumerator
3252 whose value is 0 to count as a null pointer constant. */
3253 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3254 rhs = TREE_OPERAND (rhs, 0);
3256 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3257 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3258 rhs = default_conversion (rhs);
3259 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3260 rhs = decl_constant_value_for_broken_optimization (rhs);
3262 rhstype = TREE_TYPE (rhs);
3263 coder = TREE_CODE (rhstype);
3265 if (coder == ERROR_MARK)
3266 return error_mark_node;
3268 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3270 overflow_warning (rhs);
3271 /* Check for Objective-C protocols. This will automatically
3272 issue a warning if there are protocol violations. No need to
3273 use the return value. */
3274 if (c_dialect_objc ())
3275 objc_comptypes (type, rhstype, 0);
3279 if (coder == VOID_TYPE)
3281 error ("void value not ignored as it ought to be");
3282 return error_mark_node;
3284 /* A type converts to a reference to it.
3285 This code doesn't fully support references, it's just for the
3286 special case of va_start and va_copy. */
3287 if (codel == REFERENCE_TYPE
3288 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3290 if (!lvalue_p (rhs))
3292 error ("cannot pass rvalue to reference parameter");
3293 return error_mark_node;
3295 if (!c_mark_addressable (rhs))
3296 return error_mark_node;
3297 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3299 /* We already know that these two types are compatible, but they
3300 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3301 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3302 likely to be va_list, a typedef to __builtin_va_list, which
3303 is different enough that it will cause problems later. */
3304 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3305 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3307 rhs = build1 (NOP_EXPR, type, rhs);
3310 /* Some types can interconvert without explicit casts. */
3311 else if (codel == VECTOR_TYPE
3312 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3313 return convert (type, rhs);
3314 /* Arithmetic types all interconvert, and enum is treated like int. */
3315 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3316 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3317 || codel == BOOLEAN_TYPE)
3318 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3319 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3320 || coder == BOOLEAN_TYPE))
3321 return convert_and_check (type, rhs);
3323 /* Conversion to a transparent union from its member types.
3324 This applies only to function arguments. */
3325 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3328 tree marginal_memb_type = 0;
3330 for (memb_types = TYPE_FIELDS (type); memb_types;
3331 memb_types = TREE_CHAIN (memb_types))
3333 tree memb_type = TREE_TYPE (memb_types);
3335 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3336 TYPE_MAIN_VARIANT (rhstype)))
3339 if (TREE_CODE (memb_type) != POINTER_TYPE)
3342 if (coder == POINTER_TYPE)
3344 tree ttl = TREE_TYPE (memb_type);
3345 tree ttr = TREE_TYPE (rhstype);
3347 /* Any non-function converts to a [const][volatile] void *
3348 and vice versa; otherwise, targets must be the same.
3349 Meanwhile, the lhs target must have all the qualifiers of
3351 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3352 || comp_target_types (memb_type, rhstype, 0))
3354 /* If this type won't generate any warnings, use it. */
3355 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3356 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3357 && TREE_CODE (ttl) == FUNCTION_TYPE)
3358 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3359 == TYPE_QUALS (ttr))
3360 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3361 == TYPE_QUALS (ttl))))
3364 /* Keep looking for a better type, but remember this one. */
3365 if (! marginal_memb_type)
3366 marginal_memb_type = memb_type;
3370 /* Can convert integer zero to any pointer type. */
3371 if (integer_zerop (rhs)
3372 || (TREE_CODE (rhs) == NOP_EXPR
3373 && integer_zerop (TREE_OPERAND (rhs, 0))))
3375 rhs = null_pointer_node;
3380 if (memb_types || marginal_memb_type)
3384 /* We have only a marginally acceptable member type;
3385 it needs a warning. */
3386 tree ttl = TREE_TYPE (marginal_memb_type);
3387 tree ttr = TREE_TYPE (rhstype);
3389 /* Const and volatile mean something different for function
3390 types, so the usual warnings are not appropriate. */
3391 if (TREE_CODE (ttr) == FUNCTION_TYPE
3392 && TREE_CODE (ttl) == FUNCTION_TYPE)
3394 /* Because const and volatile on functions are
3395 restrictions that say the function will not do
3396 certain things, it is okay to use a const or volatile
3397 function where an ordinary one is wanted, but not
3399 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3400 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3401 errtype, funname, parmnum);
3403 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3404 warn_for_assignment ("%s discards qualifiers from pointer target type",
3409 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3410 pedwarn ("ISO C prohibits argument conversion to union type");
3412 return build1 (NOP_EXPR, type, rhs);
3416 /* Conversions among pointers */
3417 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3418 && (coder == codel))
3420 tree ttl = TREE_TYPE (type);
3421 tree ttr = TREE_TYPE (rhstype);
3422 bool is_opaque_pointer;
3423 int target_cmp = 0; /* Cache comp_target_types () result. */
3425 /* Opaque pointers are treated like void pointers. */
3426 is_opaque_pointer = (targetm.vector_opaque_p (type)
3427 || targetm.vector_opaque_p (rhstype))
3428 && TREE_CODE (ttl) == VECTOR_TYPE
3429 && TREE_CODE (ttr) == VECTOR_TYPE;
3431 /* Any non-function converts to a [const][volatile] void *
3432 and vice versa; otherwise, targets must be the same.
3433 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3434 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3435 || (target_cmp = comp_target_types (type, rhstype, 0))
3436 || is_opaque_pointer
3437 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3438 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3441 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3444 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3445 which are not ANSI null ptr constants. */
3446 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3447 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3448 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3449 errtype, funname, parmnum);
3450 /* Const and volatile mean something different for function types,
3451 so the usual warnings are not appropriate. */
3452 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3453 && TREE_CODE (ttl) != FUNCTION_TYPE)
3455 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3456 warn_for_assignment ("%s discards qualifiers from pointer target type",
3457 errtype, funname, parmnum);
3458 /* If this is not a case of ignoring a mismatch in signedness,
3460 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3463 /* If there is a mismatch, do warn. */
3465 warn_for_assignment ("pointer targets in %s differ in signedness",
3466 errtype, funname, parmnum);
3468 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3469 && TREE_CODE (ttr) == FUNCTION_TYPE)
3471 /* Because const and volatile on functions are restrictions
3472 that say the function will not do certain things,
3473 it is okay to use a const or volatile function
3474 where an ordinary one is wanted, but not vice-versa. */
3475 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3476 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3477 errtype, funname, parmnum);
3481 warn_for_assignment ("%s from incompatible pointer type",
3482 errtype, funname, parmnum);
3483 return convert (type, rhs);
3485 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3487 error ("invalid use of non-lvalue array");
3488 return error_mark_node;
3490 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3492 /* An explicit constant 0 can convert to a pointer,
3493 or one that results from arithmetic, even including
3494 a cast to integer type. */
3495 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3497 ! (TREE_CODE (rhs) == NOP_EXPR
3498 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3499 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3500 && integer_zerop (TREE_OPERAND (rhs, 0))))
3501 warn_for_assignment ("%s makes pointer from integer without a cast",
3502 errtype, funname, parmnum);
3504 return convert (type, rhs);
3506 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3508 warn_for_assignment ("%s makes integer from pointer without a cast",
3509 errtype, funname, parmnum);
3510 return convert (type, rhs);
3512 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3513 return convert (type, rhs);
3519 tree selector = objc_message_selector ();
3521 if (selector && parmnum > 2)
3522 error ("incompatible type for argument %d of `%s'",
3523 parmnum - 2, IDENTIFIER_POINTER (selector));
3525 error ("incompatible type for argument %d of `%s'",
3526 parmnum, IDENTIFIER_POINTER (funname));
3529 error ("incompatible type for argument %d of indirect function call",
3533 error ("incompatible types in %s", errtype);
3535 return error_mark_node;
3538 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3539 is used for error and waring reporting and indicates which argument
3540 is being processed. */
3543 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3547 /* If FN was prototyped, the value has been converted already
3548 in convert_arguments. */
3549 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3552 type = TREE_TYPE (parm);
3553 ret = convert_for_assignment (type, value,
3554 (char *) 0 /* arg passing */, fn,
3555 DECL_NAME (fn), argnum);
3556 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3557 && INTEGRAL_TYPE_P (type)
3558 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3559 ret = default_conversion (ret);
3563 /* Print a warning using MSGID.
3564 It gets OPNAME as its one parameter.
3565 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3566 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3567 FUNCTION and ARGNUM are handled specially if we are building an
3568 Objective-C selector. */
3571 warn_for_assignment (const char *msgid, const char *opname, tree function,
3576 tree selector = objc_message_selector ();
3579 if (selector && argnum > 2)
3581 function = selector;
3588 /* Function name is known; supply it. */
3589 const char *const argstring = _("passing arg of `%s'");
3590 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
3591 + strlen (argstring) + 1 + 1);
3592 sprintf (new_opname, argstring,
3593 IDENTIFIER_POINTER (function));
3597 /* Function name unknown (call through ptr). */
3598 const char *const argnofun = _("passing arg of pointer to function");
3599 new_opname = (char *) alloca (strlen (argnofun) + 1 + 1);
3600 sprintf (new_opname, argnofun);
3605 /* Function name is known; supply it. */
3606 const char *const argstring = _("passing arg %d of `%s'");
3607 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
3608 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3609 sprintf (new_opname, argstring, argnum,
3610 IDENTIFIER_POINTER (function));
3614 /* Function name unknown (call through ptr); just give arg number. */
3615 const char *const argnofun = _("passing arg %d of pointer to function");
3616 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3617 sprintf (new_opname, argnofun, argnum);
3619 opname = new_opname;
3621 pedwarn (msgid, opname);
3624 /* If VALUE is a compound expr all of whose expressions are constant, then
3625 return its value. Otherwise, return error_mark_node.
3627 This is for handling COMPOUND_EXPRs as initializer elements
3628 which is allowed with a warning when -pedantic is specified. */
3631 valid_compound_expr_initializer (tree value, tree endtype)
3633 if (TREE_CODE (value) == COMPOUND_EXPR)
3635 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3637 return error_mark_node;
3638 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3641 else if (! TREE_CONSTANT (value)
3642 && ! initializer_constant_valid_p (value, endtype))
3643 return error_mark_node;
3648 /* Perform appropriate conversions on the initial value of a variable,
3649 store it in the declaration DECL,
3650 and print any error messages that are appropriate.
3651 If the init is invalid, store an ERROR_MARK. */
3654 store_init_value (tree decl, tree init)
3658 /* If variable's type was invalidly declared, just ignore it. */
3660 type = TREE_TYPE (decl);
3661 if (TREE_CODE (type) == ERROR_MARK)
3664 /* Digest the specified initializer into an expression. */
3666 value = digest_init (type, init, true, TREE_STATIC (decl));
3668 /* Store the expression if valid; else report error. */
3670 if (warn_traditional && !in_system_header
3671 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3672 warning ("traditional C rejects automatic aggregate initialization");
3674 DECL_INITIAL (decl) = value;
3676 /* ANSI wants warnings about out-of-range constant initializers. */
3677 STRIP_TYPE_NOPS (value);
3678 constant_expression_warning (value);
3680 /* Check if we need to set array size from compound literal size. */
3681 if (TREE_CODE (type) == ARRAY_TYPE
3682 && TYPE_DOMAIN (type) == 0
3683 && value != error_mark_node)
3685 tree inside_init = init;
3687 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3688 inside_init = TREE_OPERAND (init, 0);
3689 inside_init = fold (inside_init);
3691 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3693 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3695 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3697 /* For int foo[] = (int [3]){1}; we need to set array size
3698 now since later on array initializer will be just the
3699 brace enclosed list of the compound literal. */
3700 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3702 layout_decl (decl, 0);
3708 /* Methods for storing and printing names for error messages. */
3710 /* Implement a spelling stack that allows components of a name to be pushed
3711 and popped. Each element on the stack is this structure. */
3723 #define SPELLING_STRING 1
3724 #define SPELLING_MEMBER 2
3725 #define SPELLING_BOUNDS 3
3727 static struct spelling *spelling; /* Next stack element (unused). */
3728 static struct spelling *spelling_base; /* Spelling stack base. */
3729 static int spelling_size; /* Size of the spelling stack. */
3731 /* Macros to save and restore the spelling stack around push_... functions.
3732 Alternative to SAVE_SPELLING_STACK. */
3734 #define SPELLING_DEPTH() (spelling - spelling_base)
3735 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3737 /* Push an element on the spelling stack with type KIND and assign VALUE
3740 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3742 int depth = SPELLING_DEPTH (); \
3744 if (depth >= spelling_size) \
3746 spelling_size += 10; \
3747 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
3749 RESTORE_SPELLING_DEPTH (depth); \
3752 spelling->kind = (KIND); \
3753 spelling->MEMBER = (VALUE); \
3757 /* Push STRING on the stack. Printed literally. */
3760 push_string (const char *string)
3762 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3765 /* Push a member name on the stack. Printed as '.' STRING. */
3768 push_member_name (tree decl)
3770 const char *const string
3771 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3772 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3775 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3778 push_array_bounds (int bounds)
3780 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3783 /* Compute the maximum size in bytes of the printed spelling. */
3786 spelling_length (void)
3791 for (p = spelling_base; p < spelling; p++)
3793 if (p->kind == SPELLING_BOUNDS)
3796 size += strlen (p->u.s) + 1;
3802 /* Print the spelling to BUFFER and return it. */
3805 print_spelling (char *buffer)
3810 for (p = spelling_base; p < spelling; p++)
3811 if (p->kind == SPELLING_BOUNDS)
3813 sprintf (d, "[%d]", p->u.i);
3819 if (p->kind == SPELLING_MEMBER)
3821 for (s = p->u.s; (*d = *s++); d++)
3828 /* Issue an error message for a bad initializer component.
3829 MSGID identifies the message.
3830 The component name is taken from the spelling stack. */
3833 error_init (const char *msgid)
3837 error ("%s", _(msgid));
3838 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3840 error ("(near initialization for `%s')", ofwhat);
3843 /* Issue a pedantic warning for a bad initializer component.
3844 MSGID identifies the message.
3845 The component name is taken from the spelling stack. */
3848 pedwarn_init (const char *msgid)
3852 pedwarn ("%s", _(msgid));
3853 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3855 pedwarn ("(near initialization for `%s')", ofwhat);
3858 /* Issue a warning for a bad initializer component.
3859 MSGID identifies the message.
3860 The component name is taken from the spelling stack. */
3863 warning_init (const char *msgid)
3867 warning ("%s", _(msgid));
3868 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3870 warning ("(near initialization for `%s')", ofwhat);
3873 /* If TYPE is an array type and EXPR is a parenthesized string
3874 constant, warn if pedantic that EXPR is being used to initialize an
3875 object of type TYPE. */
3878 maybe_warn_string_init (tree type, struct c_expr expr)
3881 && TREE_CODE (type) == ARRAY_TYPE
3882 && TREE_CODE (expr.value) == STRING_CST
3883 && expr.original_code != STRING_CST)
3884 pedwarn_init ("array initialized from parenthesized string constant");
3887 /* Digest the parser output INIT as an initializer for type TYPE.
3888 Return a C expression of type TYPE to represent the initial value.
3890 If INIT is a string constant, STRICT_STRING is true if it is
3891 unparenthesized or we should not warn here for it being parenthesized.
3892 For other types of INIT, STRICT_STRING is not used.
3894 REQUIRE_CONSTANT requests an error if non-constant initializers or
3895 elements are seen. */
3898 digest_init (tree type, tree init, bool strict_string, int require_constant)
3900 enum tree_code code = TREE_CODE (type);
3901 tree inside_init = init;
3903 if (type == error_mark_node
3904 || init == error_mark_node
3905 || TREE_TYPE (init) == error_mark_node)
3906 return error_mark_node;
3908 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3909 /* Do not use STRIP_NOPS here. We do not want an enumerator
3910 whose value is 0 to count as a null pointer constant. */
3911 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3912 inside_init = TREE_OPERAND (init, 0);
3914 inside_init = fold (inside_init);
3916 /* Initialization of an array of chars from a string constant
3917 optionally enclosed in braces. */
3919 if (code == ARRAY_TYPE && inside_init
3920 && TREE_CODE (inside_init) == STRING_CST)
3922 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3923 /* Note that an array could be both an array of character type
3924 and an array of wchar_t if wchar_t is signed char or unsigned
3926 bool char_array = (typ1 == char_type_node
3927 || typ1 == signed_char_type_node
3928 || typ1 == unsigned_char_type_node);
3929 bool wchar_array = !!comptypes (typ1, wchar_type_node);
3930 if (char_array || wchar_array)
3934 expr.value = inside_init;
3935 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
3936 maybe_warn_string_init (type, expr);
3939 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3942 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3943 TYPE_MAIN_VARIANT (type)))
3946 if (!wchar_array && !char_string)
3948 error_init ("char-array initialized from wide string");
3949 return error_mark_node;
3951 if (char_string && !char_array)
3953 error_init ("wchar_t-array initialized from non-wide string");
3954 return error_mark_node;
3957 TREE_TYPE (inside_init) = type;
3958 if (TYPE_DOMAIN (type) != 0
3959 && TYPE_SIZE (type) != 0
3960 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3961 /* Subtract 1 (or sizeof (wchar_t))
3962 because it's ok to ignore the terminating null char
3963 that is counted in the length of the constant. */
3964 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
3965 TREE_STRING_LENGTH (inside_init)
3966 - ((TYPE_PRECISION (typ1)
3967 != TYPE_PRECISION (char_type_node))
3968 ? (TYPE_PRECISION (wchar_type_node)
3971 pedwarn_init ("initializer-string for array of chars is too long");
3975 else if (INTEGRAL_TYPE_P (typ1))
3977 error_init ("array of inappropriate type initialized "
3978 "from string constant");
3979 return error_mark_node;
3983 /* Build a VECTOR_CST from a *constant* vector constructor. If the
3984 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
3985 below and handle as a constructor. */
3986 if (code == VECTOR_TYPE
3987 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
3988 && TREE_CONSTANT (inside_init))
3990 if (TREE_CODE (inside_init) == VECTOR_CST
3991 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3992 TYPE_MAIN_VARIANT (type)))
3995 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
3998 /* Any type can be initialized
3999 from an expression of the same type, optionally with braces. */
4001 if (inside_init && TREE_TYPE (inside_init) != 0
4002 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4003 TYPE_MAIN_VARIANT (type))
4004 || (code == ARRAY_TYPE
4005 && comptypes (TREE_TYPE (inside_init), type))
4006 || (code == VECTOR_TYPE
4007 && comptypes (TREE_TYPE (inside_init), type))
4008 || (code == POINTER_TYPE
4009 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4010 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4012 || (code == POINTER_TYPE
4013 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4014 && comptypes (TREE_TYPE (inside_init),
4015 TREE_TYPE (type)))))
4017 if (code == POINTER_TYPE)
4019 inside_init = default_function_array_conversion (inside_init);
4021 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4023 error_init ("invalid use of non-lvalue array");
4024 return error_mark_node;
4028 if (code == VECTOR_TYPE)
4029 /* Although the types are compatible, we may require a
4031 inside_init = convert (type, inside_init);
4033 if (require_constant && !flag_isoc99
4034 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4036 /* As an extension, allow initializing objects with static storage
4037 duration with compound literals (which are then treated just as
4038 the brace enclosed list they contain). */
4039 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4040 inside_init = DECL_INITIAL (decl);
4043 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4044 && TREE_CODE (inside_init) != CONSTRUCTOR)
4046 error_init ("array initialized from non-constant array expression");
4047 return error_mark_node;
4050 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4051 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4053 /* Compound expressions can only occur here if -pedantic or
4054 -pedantic-errors is specified. In the later case, we always want
4055 an error. In the former case, we simply want a warning. */
4056 if (require_constant && pedantic
4057 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4060 = valid_compound_expr_initializer (inside_init,
4061 TREE_TYPE (inside_init));
4062 if (inside_init == error_mark_node)
4063 error_init ("initializer element is not constant");
4065 pedwarn_init ("initializer element is not constant");
4066 if (flag_pedantic_errors)
4067 inside_init = error_mark_node;
4069 else if (require_constant
4070 && (!TREE_CONSTANT (inside_init)
4071 /* This test catches things like `7 / 0' which
4072 result in an expression for which TREE_CONSTANT
4073 is true, but which is not actually something
4074 that is a legal constant. We really should not
4075 be using this function, because it is a part of
4076 the back-end. Instead, the expression should
4077 already have been turned into ERROR_MARK_NODE. */
4078 || !initializer_constant_valid_p (inside_init,
4079 TREE_TYPE (inside_init))))
4081 error_init ("initializer element is not constant");
4082 inside_init = error_mark_node;
4088 /* Handle scalar types, including conversions. */
4090 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4091 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4092 || code == VECTOR_TYPE)
4094 /* Note that convert_for_assignment calls default_conversion
4095 for arrays and functions. We must not call it in the
4096 case where inside_init is a null pointer constant. */
4098 = convert_for_assignment (type, init, _("initialization"),
4099 NULL_TREE, NULL_TREE, 0);
4101 if (require_constant && ! TREE_CONSTANT (inside_init))
4103 error_init ("initializer element is not constant");
4104 inside_init = error_mark_node;
4106 else if (require_constant
4107 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4109 error_init ("initializer element is not computable at load time");
4110 inside_init = error_mark_node;
4116 /* Come here only for records and arrays. */
4118 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4120 error_init ("variable-sized object may not be initialized");
4121 return error_mark_node;
4124 error_init ("invalid initializer");
4125 return error_mark_node;
4128 /* Handle initializers that use braces. */
4130 /* Type of object we are accumulating a constructor for.
4131 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4132 static tree constructor_type;
4134 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4136 static tree constructor_fields;
4138 /* For an ARRAY_TYPE, this is the specified index
4139 at which to store the next element we get. */
4140 static tree constructor_index;
4142 /* For an ARRAY_TYPE, this is the maximum index. */
4143 static tree constructor_max_index;
4145 /* For a RECORD_TYPE, this is the first field not yet written out. */
4146 static tree constructor_unfilled_fields;
4148 /* For an ARRAY_TYPE, this is the index of the first element
4149 not yet written out. */
4150 static tree constructor_unfilled_index;
4152 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4153 This is so we can generate gaps between fields, when appropriate. */
4154 static tree constructor_bit_index;
4156 /* If we are saving up the elements rather than allocating them,
4157 this is the list of elements so far (in reverse order,
4158 most recent first). */
4159 static tree constructor_elements;
4161 /* 1 if constructor should be incrementally stored into a constructor chain,
4162 0 if all the elements should be kept in AVL tree. */
4163 static int constructor_incremental;
4165 /* 1 if so far this constructor's elements are all compile-time constants. */
4166 static int constructor_constant;
4168 /* 1 if so far this constructor's elements are all valid address constants. */
4169 static int constructor_simple;
4171 /* 1 if this constructor is erroneous so far. */
4172 static int constructor_erroneous;
4174 /* Structure for managing pending initializer elements, organized as an
4179 struct init_node *left, *right;
4180 struct init_node *parent;
4186 /* Tree of pending elements at this constructor level.
4187 These are elements encountered out of order
4188 which belong at places we haven't reached yet in actually
4190 Will never hold tree nodes across GC runs. */
4191 static struct init_node *constructor_pending_elts;
4193 /* The SPELLING_DEPTH of this constructor. */
4194 static int constructor_depth;
4196 /* 0 if implicitly pushing constructor levels is allowed. */
4197 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4199 /* DECL node for which an initializer is being read.
4200 0 means we are reading a constructor expression
4201 such as (struct foo) {...}. */
4202 static tree constructor_decl;
4204 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4205 static const char *constructor_asmspec;
4207 /* Nonzero if this is an initializer for a top-level decl. */
4208 static int constructor_top_level;
4210 /* Nonzero if there were any member designators in this initializer. */
4211 static int constructor_designated;
4213 /* Nesting depth of designator list. */
4214 static int designator_depth;
4216 /* Nonzero if there were diagnosed errors in this designator list. */
4217 static int designator_errorneous;
4220 /* This stack has a level for each implicit or explicit level of
4221 structuring in the initializer, including the outermost one. It
4222 saves the values of most of the variables above. */
4224 struct constructor_range_stack;
4226 struct constructor_stack
4228 struct constructor_stack *next;
4233 tree unfilled_index;
4234 tree unfilled_fields;
4237 struct init_node *pending_elts;
4240 /* If value nonzero, this value should replace the entire
4241 constructor at this level. */
4242 struct c_expr replacement_value;
4243 struct constructor_range_stack *range_stack;
4253 struct constructor_stack *constructor_stack;
4255 /* This stack represents designators from some range designator up to
4256 the last designator in the list. */
4258 struct constructor_range_stack
4260 struct constructor_range_stack *next, *prev;
4261 struct constructor_stack *stack;
4268 struct constructor_range_stack *constructor_range_stack;
4270 /* This stack records separate initializers that are nested.
4271 Nested initializers can't happen in ANSI C, but GNU C allows them
4272 in cases like { ... (struct foo) { ... } ... }. */
4274 struct initializer_stack
4276 struct initializer_stack *next;
4278 const char *asmspec;
4279 struct constructor_stack *constructor_stack;
4280 struct constructor_range_stack *constructor_range_stack;
4282 struct spelling *spelling;
4283 struct spelling *spelling_base;
4286 char require_constant_value;
4287 char require_constant_elements;
4290 struct initializer_stack *initializer_stack;
4292 /* Prepare to parse and output the initializer for variable DECL. */
4295 start_init (tree decl, tree asmspec_tree, int top_level)
4298 struct initializer_stack *p = XNEW (struct initializer_stack);
4299 const char *asmspec = 0;
4302 asmspec = TREE_STRING_POINTER (asmspec_tree);
4304 p->decl = constructor_decl;
4305 p->asmspec = constructor_asmspec;
4306 p->require_constant_value = require_constant_value;
4307 p->require_constant_elements = require_constant_elements;
4308 p->constructor_stack = constructor_stack;
4309 p->constructor_range_stack = constructor_range_stack;
4310 p->elements = constructor_elements;
4311 p->spelling = spelling;
4312 p->spelling_base = spelling_base;
4313 p->spelling_size = spelling_size;
4314 p->top_level = constructor_top_level;
4315 p->next = initializer_stack;
4316 initializer_stack = p;
4318 constructor_decl = decl;
4319 constructor_asmspec = asmspec;
4320 constructor_designated = 0;
4321 constructor_top_level = top_level;
4325 require_constant_value = TREE_STATIC (decl);
4326 require_constant_elements
4327 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4328 /* For a scalar, you can always use any value to initialize,
4329 even within braces. */
4330 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4331 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4332 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4333 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4334 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4338 require_constant_value = 0;
4339 require_constant_elements = 0;
4340 locus = "(anonymous)";
4343 constructor_stack = 0;
4344 constructor_range_stack = 0;
4346 missing_braces_mentioned = 0;
4350 RESTORE_SPELLING_DEPTH (0);
4353 push_string (locus);
4359 struct initializer_stack *p = initializer_stack;
4361 /* Free the whole constructor stack of this initializer. */
4362 while (constructor_stack)
4364 struct constructor_stack *q = constructor_stack;
4365 constructor_stack = q->next;
4369 gcc_assert (!constructor_range_stack);
4371 /* Pop back to the data of the outer initializer (if any). */
4372 free (spelling_base);
4374 constructor_decl = p->decl;
4375 constructor_asmspec = p->asmspec;
4376 require_constant_value = p->require_constant_value;
4377 require_constant_elements = p->require_constant_elements;
4378 constructor_stack = p->constructor_stack;
4379 constructor_range_stack = p->constructor_range_stack;
4380 constructor_elements = p->elements;
4381 spelling = p->spelling;
4382 spelling_base = p->spelling_base;
4383 spelling_size = p->spelling_size;
4384 constructor_top_level = p->top_level;
4385 initializer_stack = p->next;
4389 /* Call here when we see the initializer is surrounded by braces.
4390 This is instead of a call to push_init_level;
4391 it is matched by a call to pop_init_level.
4393 TYPE is the type to initialize, for a constructor expression.
4394 For an initializer for a decl, TYPE is zero. */
4397 really_start_incremental_init (tree type)
4399 struct constructor_stack *p = XNEW (struct constructor_stack);
4402 type = TREE_TYPE (constructor_decl);
4404 if (targetm.vector_opaque_p (type))
4405 error ("opaque vector types cannot be initialized");
4407 p->type = constructor_type;
4408 p->fields = constructor_fields;
4409 p->index = constructor_index;
4410 p->max_index = constructor_max_index;
4411 p->unfilled_index = constructor_unfilled_index;
4412 p->unfilled_fields = constructor_unfilled_fields;
4413 p->bit_index = constructor_bit_index;
4414 p->elements = constructor_elements;
4415 p->constant = constructor_constant;
4416 p->simple = constructor_simple;
4417 p->erroneous = constructor_erroneous;
4418 p->pending_elts = constructor_pending_elts;
4419 p->depth = constructor_depth;
4420 p->replacement_value.value = 0;
4421 p->replacement_value.original_code = ERROR_MARK;
4425 p->incremental = constructor_incremental;
4426 p->designated = constructor_designated;
4428 constructor_stack = p;
4430 constructor_constant = 1;
4431 constructor_simple = 1;
4432 constructor_depth = SPELLING_DEPTH ();
4433 constructor_elements = 0;
4434 constructor_pending_elts = 0;
4435 constructor_type = type;
4436 constructor_incremental = 1;
4437 constructor_designated = 0;
4438 designator_depth = 0;
4439 designator_errorneous = 0;
4441 if (TREE_CODE (constructor_type) == RECORD_TYPE
4442 || TREE_CODE (constructor_type) == UNION_TYPE)
4444 constructor_fields = TYPE_FIELDS (constructor_type);
4445 /* Skip any nameless bit fields at the beginning. */
4446 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4447 && DECL_NAME (constructor_fields) == 0)
4448 constructor_fields = TREE_CHAIN (constructor_fields);
4450 constructor_unfilled_fields = constructor_fields;
4451 constructor_bit_index = bitsize_zero_node;
4453 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4455 if (TYPE_DOMAIN (constructor_type))
4457 constructor_max_index
4458 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4460 /* Detect non-empty initializations of zero-length arrays. */
4461 if (constructor_max_index == NULL_TREE
4462 && TYPE_SIZE (constructor_type))
4463 constructor_max_index = build_int_cst (NULL_TREE, -1);
4465 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4466 to initialize VLAs will cause a proper error; avoid tree
4467 checking errors as well by setting a safe value. */
4468 if (constructor_max_index
4469 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4470 constructor_max_index = build_int_cst (NULL_TREE, -1);
4473 = convert (bitsizetype,
4474 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4477 constructor_index = bitsize_zero_node;
4479 constructor_unfilled_index = constructor_index;
4481 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4483 /* Vectors are like simple fixed-size arrays. */
4484 constructor_max_index =
4485 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4486 constructor_index = convert (bitsizetype, bitsize_zero_node);
4487 constructor_unfilled_index = constructor_index;
4491 /* Handle the case of int x = {5}; */
4492 constructor_fields = constructor_type;
4493 constructor_unfilled_fields = constructor_type;
4497 /* Push down into a subobject, for initialization.
4498 If this is for an explicit set of braces, IMPLICIT is 0.
4499 If it is because the next element belongs at a lower level,
4500 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4503 push_init_level (int implicit)
4505 struct constructor_stack *p;
4506 tree value = NULL_TREE;
4508 /* If we've exhausted any levels that didn't have braces,
4510 while (constructor_stack->implicit)
4512 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4513 || TREE_CODE (constructor_type) == UNION_TYPE)
4514 && constructor_fields == 0)
4515 process_init_element (pop_init_level (1));
4516 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4517 && constructor_max_index
4518 && tree_int_cst_lt (constructor_max_index, constructor_index))
4519 process_init_element (pop_init_level (1));
4524 /* Unless this is an explicit brace, we need to preserve previous
4528 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4529 || TREE_CODE (constructor_type) == UNION_TYPE)
4530 && constructor_fields)
4531 value = find_init_member (constructor_fields);
4532 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4533 value = find_init_member (constructor_index);
4536 p = XNEW (struct constructor_stack);
4537 p->type = constructor_type;
4538 p->fields = constructor_fields;
4539 p->index = constructor_index;
4540 p->max_index = constructor_max_index;
4541 p->unfilled_index = constructor_unfilled_index;
4542 p->unfilled_fields = constructor_unfilled_fields;
4543 p->bit_index = constructor_bit_index;
4544 p->elements = constructor_elements;
4545 p->constant = constructor_constant;
4546 p->simple = constructor_simple;
4547 p->erroneous = constructor_erroneous;
4548 p->pending_elts = constructor_pending_elts;
4549 p->depth = constructor_depth;
4550 p->replacement_value.value = 0;
4551 p->replacement_value.original_code = ERROR_MARK;
4552 p->implicit = implicit;
4554 p->incremental = constructor_incremental;
4555 p->designated = constructor_designated;
4556 p->next = constructor_stack;
4558 constructor_stack = p;
4560 constructor_constant = 1;
4561 constructor_simple = 1;
4562 constructor_depth = SPELLING_DEPTH ();
4563 constructor_elements = 0;
4564 constructor_incremental = 1;
4565 constructor_designated = 0;
4566 constructor_pending_elts = 0;
4569 p->range_stack = constructor_range_stack;
4570 constructor_range_stack = 0;
4571 designator_depth = 0;
4572 designator_errorneous = 0;
4575 /* Don't die if an entire brace-pair level is superfluous
4576 in the containing level. */
4577 if (constructor_type == 0)
4579 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4580 || TREE_CODE (constructor_type) == UNION_TYPE)
4582 /* Don't die if there are extra init elts at the end. */
4583 if (constructor_fields == 0)
4584 constructor_type = 0;
4587 constructor_type = TREE_TYPE (constructor_fields);
4588 push_member_name (constructor_fields);
4589 constructor_depth++;
4592 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4594 constructor_type = TREE_TYPE (constructor_type);
4595 push_array_bounds (tree_low_cst (constructor_index, 0));
4596 constructor_depth++;
4599 if (constructor_type == 0)
4601 error_init ("extra brace group at end of initializer");
4602 constructor_fields = 0;
4603 constructor_unfilled_fields = 0;
4607 if (value && TREE_CODE (value) == CONSTRUCTOR)
4609 constructor_constant = TREE_CONSTANT (value);
4610 constructor_simple = TREE_STATIC (value);
4611 constructor_elements = CONSTRUCTOR_ELTS (value);
4612 if (constructor_elements
4613 && (TREE_CODE (constructor_type) == RECORD_TYPE
4614 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4615 set_nonincremental_init ();
4618 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4620 missing_braces_mentioned = 1;
4621 warning_init ("missing braces around initializer");
4624 if (TREE_CODE (constructor_type) == RECORD_TYPE
4625 || TREE_CODE (constructor_type) == UNION_TYPE)
4627 constructor_fields = TYPE_FIELDS (constructor_type);
4628 /* Skip any nameless bit fields at the beginning. */
4629 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4630 && DECL_NAME (constructor_fields) == 0)
4631 constructor_fields = TREE_CHAIN (constructor_fields);
4633 constructor_unfilled_fields = constructor_fields;
4634 constructor_bit_index = bitsize_zero_node;
4636 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4638 /* Vectors are like simple fixed-size arrays. */
4639 constructor_max_index =
4640 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4641 constructor_index = convert (bitsizetype, integer_zero_node);
4642 constructor_unfilled_index = constructor_index;
4644 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4646 if (TYPE_DOMAIN (constructor_type))
4648 constructor_max_index
4649 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4651 /* Detect non-empty initializations of zero-length arrays. */
4652 if (constructor_max_index == NULL_TREE
4653 && TYPE_SIZE (constructor_type))
4654 constructor_max_index = build_int_cst (NULL_TREE, -1);
4656 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4657 to initialize VLAs will cause a proper error; avoid tree
4658 checking errors as well by setting a safe value. */
4659 if (constructor_max_index
4660 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4661 constructor_max_index = build_int_cst (NULL_TREE, -1);
4664 = convert (bitsizetype,
4665 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4668 constructor_index = bitsize_zero_node;
4670 constructor_unfilled_index = constructor_index;
4671 if (value && TREE_CODE (value) == STRING_CST)
4673 /* We need to split the char/wchar array into individual
4674 characters, so that we don't have to special case it
4676 set_nonincremental_init_from_string (value);
4681 warning_init ("braces around scalar initializer");
4682 constructor_fields = constructor_type;
4683 constructor_unfilled_fields = constructor_type;
4687 /* At the end of an implicit or explicit brace level,
4688 finish up that level of constructor. If a single expression
4689 with redundant braces initialized that level, return the
4690 c_expr structure for that expression. Otherwise, the original_code
4691 element is set to ERROR_MARK.
4692 If we were outputting the elements as they are read, return 0 as the value
4693 from inner levels (process_init_element ignores that),
4694 but return error_mark_node as the value from the outermost level
4695 (that's what we want to put in DECL_INITIAL).
4696 Otherwise, return a CONSTRUCTOR expression as the value. */
4699 pop_init_level (int implicit)
4701 struct constructor_stack *p;
4704 ret.original_code = ERROR_MARK;
4708 /* When we come to an explicit close brace,
4709 pop any inner levels that didn't have explicit braces. */
4710 while (constructor_stack->implicit)
4711 process_init_element (pop_init_level (1));
4713 gcc_assert (!constructor_range_stack);
4716 /* Now output all pending elements. */
4717 constructor_incremental = 1;
4718 output_pending_init_elements (1);
4720 p = constructor_stack;
4722 /* Error for initializing a flexible array member, or a zero-length
4723 array member in an inappropriate context. */
4724 if (constructor_type && constructor_fields
4725 && TREE_CODE (constructor_type) == ARRAY_TYPE
4726 && TYPE_DOMAIN (constructor_type)
4727 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4729 /* Silently discard empty initializations. The parser will
4730 already have pedwarned for empty brackets. */
4731 if (integer_zerop (constructor_unfilled_index))
4732 constructor_type = NULL_TREE;
4735 gcc_assert (!TYPE_SIZE (constructor_type));
4737 if (constructor_depth > 2)
4738 error_init ("initialization of flexible array member in a nested context");
4740 pedwarn_init ("initialization of a flexible array member");
4742 /* We have already issued an error message for the existence
4743 of a flexible array member not at the end of the structure.
4744 Discard the initializer so that we do not abort later. */
4745 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4746 constructor_type = NULL_TREE;
4750 /* Warn when some struct elements are implicitly initialized to zero. */
4751 if (warn_missing_field_initializers
4753 && TREE_CODE (constructor_type) == RECORD_TYPE
4754 && constructor_unfilled_fields)
4756 /* Do not warn for flexible array members or zero-length arrays. */
4757 while (constructor_unfilled_fields
4758 && (! DECL_SIZE (constructor_unfilled_fields)
4759 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4760 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4762 /* Do not warn if this level of the initializer uses member
4763 designators; it is likely to be deliberate. */
4764 if (constructor_unfilled_fields && !constructor_designated)
4766 push_member_name (constructor_unfilled_fields);
4767 warning_init ("missing initializer");
4768 RESTORE_SPELLING_DEPTH (constructor_depth);
4772 /* Pad out the end of the structure. */
4773 if (p->replacement_value.value)
4774 /* If this closes a superfluous brace pair,
4775 just pass out the element between them. */
4776 ret = p->replacement_value;
4777 else if (constructor_type == 0)
4779 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4780 && TREE_CODE (constructor_type) != UNION_TYPE
4781 && TREE_CODE (constructor_type) != ARRAY_TYPE
4782 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4784 /* A nonincremental scalar initializer--just return
4785 the element, after verifying there is just one. */
4786 if (constructor_elements == 0)
4788 if (!constructor_erroneous)
4789 error_init ("empty scalar initializer");
4790 ret.value = error_mark_node;
4792 else if (TREE_CHAIN (constructor_elements) != 0)
4794 error_init ("extra elements in scalar initializer");
4795 ret.value = TREE_VALUE (constructor_elements);
4798 ret.value = TREE_VALUE (constructor_elements);
4802 if (constructor_erroneous)
4803 ret.value = error_mark_node;
4806 ret.value = build_constructor (constructor_type,
4807 nreverse (constructor_elements));
4808 if (constructor_constant)
4809 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
4810 if (constructor_constant && constructor_simple)
4811 TREE_STATIC (ret.value) = 1;
4815 constructor_type = p->type;
4816 constructor_fields = p->fields;
4817 constructor_index = p->index;
4818 constructor_max_index = p->max_index;
4819 constructor_unfilled_index = p->unfilled_index;
4820 constructor_unfilled_fields = p->unfilled_fields;
4821 constructor_bit_index = p->bit_index;
4822 constructor_elements = p->elements;
4823 constructor_constant = p->constant;
4824 constructor_simple = p->simple;
4825 constructor_erroneous = p->erroneous;
4826 constructor_incremental = p->incremental;
4827 constructor_designated = p->designated;
4828 constructor_pending_elts = p->pending_elts;
4829 constructor_depth = p->depth;
4831 constructor_range_stack = p->range_stack;
4832 RESTORE_SPELLING_DEPTH (constructor_depth);
4834 constructor_stack = p->next;
4839 if (constructor_stack == 0)
4841 ret.value = error_mark_node;
4849 /* Common handling for both array range and field name designators.
4850 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4853 set_designator (int array)
4856 enum tree_code subcode;
4858 /* Don't die if an entire brace-pair level is superfluous
4859 in the containing level. */
4860 if (constructor_type == 0)
4863 /* If there were errors in this designator list already, bail out
4865 if (designator_errorneous)
4868 if (!designator_depth)
4870 gcc_assert (!constructor_range_stack);
4872 /* Designator list starts at the level of closest explicit
4874 while (constructor_stack->implicit)
4875 process_init_element (pop_init_level (1));
4876 constructor_designated = 1;
4880 if (constructor_no_implicit)
4882 error_init ("initialization designators may not nest");
4886 switch (TREE_CODE (constructor_type))
4890 subtype = TREE_TYPE (constructor_fields);
4891 if (subtype != error_mark_node)
4892 subtype = TYPE_MAIN_VARIANT (subtype);
4895 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4901 subcode = TREE_CODE (subtype);
4902 if (array && subcode != ARRAY_TYPE)
4904 error_init ("array index in non-array initializer");
4907 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4909 error_init ("field name not in record or union initializer");
4913 constructor_designated = 1;
4914 push_init_level (2);
4918 /* If there are range designators in designator list, push a new designator
4919 to constructor_range_stack. RANGE_END is end of such stack range or
4920 NULL_TREE if there is no range designator at this level. */
4923 push_range_stack (tree range_end)
4925 struct constructor_range_stack *p;
4927 p = GGC_NEW (struct constructor_range_stack);
4928 p->prev = constructor_range_stack;
4930 p->fields = constructor_fields;
4931 p->range_start = constructor_index;
4932 p->index = constructor_index;
4933 p->stack = constructor_stack;
4934 p->range_end = range_end;
4935 if (constructor_range_stack)
4936 constructor_range_stack->next = p;
4937 constructor_range_stack = p;
4940 /* Within an array initializer, specify the next index to be initialized.
4941 FIRST is that index. If LAST is nonzero, then initialize a range
4942 of indices, running from FIRST through LAST. */
4945 set_init_index (tree first, tree last)
4947 if (set_designator (1))
4950 designator_errorneous = 1;
4952 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
4953 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
4955 error_init ("array index in initializer not of integer type");
4959 while ((TREE_CODE (first) == NOP_EXPR
4960 || TREE_CODE (first) == CONVERT_EXPR
4961 || TREE_CODE (first) == NON_LVALUE_EXPR)
4962 && (TYPE_MODE (TREE_TYPE (first))
4963 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
4964 first = TREE_OPERAND (first, 0);
4967 while ((TREE_CODE (last) == NOP_EXPR
4968 || TREE_CODE (last) == CONVERT_EXPR
4969 || TREE_CODE (last) == NON_LVALUE_EXPR)
4970 && (TYPE_MODE (TREE_TYPE (last))
4971 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
4972 last = TREE_OPERAND (last, 0);
4974 if (TREE_CODE (first) != INTEGER_CST)
4975 error_init ("nonconstant array index in initializer");
4976 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
4977 error_init ("nonconstant array index in initializer");
4978 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
4979 error_init ("array index in non-array initializer");
4980 else if (tree_int_cst_sgn (first) == -1)
4981 error_init ("array index in initializer exceeds array bounds");
4982 else if (constructor_max_index
4983 && tree_int_cst_lt (constructor_max_index, first))
4984 error_init ("array index in initializer exceeds array bounds");
4987 constructor_index = convert (bitsizetype, first);
4991 if (tree_int_cst_equal (first, last))
4993 else if (tree_int_cst_lt (last, first))
4995 error_init ("empty index range in initializer");
5000 last = convert (bitsizetype, last);
5001 if (constructor_max_index != 0
5002 && tree_int_cst_lt (constructor_max_index, last))
5004 error_init ("array index range in initializer exceeds array bounds");
5011 designator_errorneous = 0;
5012 if (constructor_range_stack || last)
5013 push_range_stack (last);
5017 /* Within a struct initializer, specify the next field to be initialized. */
5020 set_init_label (tree fieldname)
5024 if (set_designator (0))
5027 designator_errorneous = 1;
5029 if (TREE_CODE (constructor_type) != RECORD_TYPE
5030 && TREE_CODE (constructor_type) != UNION_TYPE)
5032 error_init ("field name not in record or union initializer");
5036 for (tail = TYPE_FIELDS (constructor_type); tail;
5037 tail = TREE_CHAIN (tail))
5039 if (DECL_NAME (tail) == fieldname)
5044 error ("unknown field `%s' specified in initializer",
5045 IDENTIFIER_POINTER (fieldname));
5048 constructor_fields = tail;
5050 designator_errorneous = 0;
5051 if (constructor_range_stack)
5052 push_range_stack (NULL_TREE);
5056 /* Add a new initializer to the tree of pending initializers. PURPOSE
5057 identifies the initializer, either array index or field in a structure.
5058 VALUE is the value of that index or field. */
5061 add_pending_init (tree purpose, tree value)
5063 struct init_node *p, **q, *r;
5065 q = &constructor_pending_elts;
5068 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5073 if (tree_int_cst_lt (purpose, p->purpose))
5075 else if (tree_int_cst_lt (p->purpose, purpose))
5079 if (TREE_SIDE_EFFECTS (p->value))
5080 warning_init ("initialized field with side-effects overwritten");
5090 bitpos = bit_position (purpose);
5094 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5096 else if (p->purpose != purpose)
5100 if (TREE_SIDE_EFFECTS (p->value))
5101 warning_init ("initialized field with side-effects overwritten");
5108 r = GGC_NEW (struct init_node);
5109 r->purpose = purpose;
5120 struct init_node *s;
5124 if (p->balance == 0)
5126 else if (p->balance < 0)
5133 p->left->parent = p;
5150 constructor_pending_elts = r;
5155 struct init_node *t = r->right;
5159 r->right->parent = r;
5164 p->left->parent = p;
5167 p->balance = t->balance < 0;
5168 r->balance = -(t->balance > 0);
5183 constructor_pending_elts = t;
5189 /* p->balance == +1; growth of left side balances the node. */
5194 else /* r == p->right */
5196 if (p->balance == 0)
5197 /* Growth propagation from right side. */
5199 else if (p->balance > 0)
5206 p->right->parent = p;
5223 constructor_pending_elts = r;
5225 else /* r->balance == -1 */
5228 struct init_node *t = r->left;
5232 r->left->parent = r;
5237 p->right->parent = p;
5240 r->balance = (t->balance < 0);
5241 p->balance = -(t->balance > 0);
5256 constructor_pending_elts = t;
5262 /* p->balance == -1; growth of right side balances the node. */
5273 /* Build AVL tree from a sorted chain. */
5276 set_nonincremental_init (void)
5280 if (TREE_CODE (constructor_type) != RECORD_TYPE
5281 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5284 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5285 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5286 constructor_elements = 0;
5287 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5289 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5290 /* Skip any nameless bit fields at the beginning. */
5291 while (constructor_unfilled_fields != 0
5292 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5293 && DECL_NAME (constructor_unfilled_fields) == 0)
5294 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5297 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5299 if (TYPE_DOMAIN (constructor_type))
5300 constructor_unfilled_index
5301 = convert (bitsizetype,
5302 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5304 constructor_unfilled_index = bitsize_zero_node;
5306 constructor_incremental = 0;
5309 /* Build AVL tree from a string constant. */
5312 set_nonincremental_init_from_string (tree str)
5314 tree value, purpose, type;
5315 HOST_WIDE_INT val[2];
5316 const char *p, *end;
5317 int byte, wchar_bytes, charwidth, bitpos;
5319 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5321 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5322 == TYPE_PRECISION (char_type_node))
5326 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5327 == TYPE_PRECISION (wchar_type_node));
5328 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5330 charwidth = TYPE_PRECISION (char_type_node);
5331 type = TREE_TYPE (constructor_type);
5332 p = TREE_STRING_POINTER (str);
5333 end = p + TREE_STRING_LENGTH (str);
5335 for (purpose = bitsize_zero_node;
5336 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5337 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5339 if (wchar_bytes == 1)
5341 val[1] = (unsigned char) *p++;
5348 for (byte = 0; byte < wchar_bytes; byte++)
5350 if (BYTES_BIG_ENDIAN)
5351 bitpos = (wchar_bytes - byte - 1) * charwidth;
5353 bitpos = byte * charwidth;
5354 val[bitpos < HOST_BITS_PER_WIDE_INT]
5355 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5356 << (bitpos % HOST_BITS_PER_WIDE_INT);
5360 if (!TYPE_UNSIGNED (type))
5362 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5363 if (bitpos < HOST_BITS_PER_WIDE_INT)
5365 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5367 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5371 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5376 else if (val[0] & (((HOST_WIDE_INT) 1)
5377 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5378 val[0] |= ((HOST_WIDE_INT) -1)
5379 << (bitpos - HOST_BITS_PER_WIDE_INT);
5382 value = build_int_cst_wide (type, val[1], val[0]);
5383 add_pending_init (purpose, value);
5386 constructor_incremental = 0;
5389 /* Return value of FIELD in pending initializer or zero if the field was
5390 not initialized yet. */
5393 find_init_member (tree field)
5395 struct init_node *p;
5397 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5399 if (constructor_incremental
5400 && tree_int_cst_lt (field, constructor_unfilled_index))
5401 set_nonincremental_init ();
5403 p = constructor_pending_elts;
5406 if (tree_int_cst_lt (field, p->purpose))
5408 else if (tree_int_cst_lt (p->purpose, field))
5414 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5416 tree bitpos = bit_position (field);
5418 if (constructor_incremental
5419 && (!constructor_unfilled_fields
5420 || tree_int_cst_lt (bitpos,
5421 bit_position (constructor_unfilled_fields))))
5422 set_nonincremental_init ();
5424 p = constructor_pending_elts;
5427 if (field == p->purpose)
5429 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5435 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5437 if (constructor_elements
5438 && TREE_PURPOSE (constructor_elements) == field)
5439 return TREE_VALUE (constructor_elements);
5444 /* "Output" the next constructor element.
5445 At top level, really output it to assembler code now.
5446 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5447 TYPE is the data type that the containing data type wants here.
5448 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5449 If VALUE is a string constant, STRICT_STRING is true if it is
5450 unparenthesized or we should not warn here for it being parenthesized.
5451 For other types of VALUE, STRICT_STRING is not used.
5453 PENDING if non-nil means output pending elements that belong
5454 right after this element. (PENDING is normally 1;
5455 it is 0 while outputting pending elements, to avoid recursion.) */
5458 output_init_element (tree value, bool strict_string, tree type, tree field,
5461 if (type == error_mark_node)
5463 constructor_erroneous = 1;
5466 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5467 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5468 && !(TREE_CODE (value) == STRING_CST
5469 && TREE_CODE (type) == ARRAY_TYPE
5470 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5471 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5472 TYPE_MAIN_VARIANT (type))))
5473 value = default_conversion (value);
5475 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5476 && require_constant_value && !flag_isoc99 && pending)
5478 /* As an extension, allow initializing objects with static storage
5479 duration with compound literals (which are then treated just as
5480 the brace enclosed list they contain). */
5481 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5482 value = DECL_INITIAL (decl);
5485 if (value == error_mark_node)
5486 constructor_erroneous = 1;
5487 else if (!TREE_CONSTANT (value))
5488 constructor_constant = 0;
5489 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5490 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5491 || TREE_CODE (constructor_type) == UNION_TYPE)
5492 && DECL_C_BIT_FIELD (field)
5493 && TREE_CODE (value) != INTEGER_CST))
5494 constructor_simple = 0;
5496 if (require_constant_value && ! TREE_CONSTANT (value))
5498 error_init ("initializer element is not constant");
5499 value = error_mark_node;
5501 else if (require_constant_elements
5502 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5503 pedwarn ("initializer element is not computable at load time");
5505 /* If this field is empty (and not at the end of structure),
5506 don't do anything other than checking the initializer. */
5508 && (TREE_TYPE (field) == error_mark_node
5509 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5510 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5511 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5512 || TREE_CHAIN (field)))))
5515 value = digest_init (type, value, strict_string, require_constant_value);
5516 if (value == error_mark_node)
5518 constructor_erroneous = 1;
5522 /* If this element doesn't come next in sequence,
5523 put it on constructor_pending_elts. */
5524 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5525 && (!constructor_incremental
5526 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5528 if (constructor_incremental
5529 && tree_int_cst_lt (field, constructor_unfilled_index))
5530 set_nonincremental_init ();
5532 add_pending_init (field, value);
5535 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5536 && (!constructor_incremental
5537 || field != constructor_unfilled_fields))
5539 /* We do this for records but not for unions. In a union,
5540 no matter which field is specified, it can be initialized
5541 right away since it starts at the beginning of the union. */
5542 if (constructor_incremental)
5544 if (!constructor_unfilled_fields)
5545 set_nonincremental_init ();
5548 tree bitpos, unfillpos;
5550 bitpos = bit_position (field);
5551 unfillpos = bit_position (constructor_unfilled_fields);
5553 if (tree_int_cst_lt (bitpos, unfillpos))
5554 set_nonincremental_init ();
5558 add_pending_init (field, value);
5561 else if (TREE_CODE (constructor_type) == UNION_TYPE
5562 && constructor_elements)
5564 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5565 warning_init ("initialized field with side-effects overwritten");
5567 /* We can have just one union field set. */
5568 constructor_elements = 0;
5571 /* Otherwise, output this element either to
5572 constructor_elements or to the assembler file. */
5574 if (field && TREE_CODE (field) == INTEGER_CST)
5575 field = copy_node (field);
5576 constructor_elements
5577 = tree_cons (field, value, constructor_elements);
5579 /* Advance the variable that indicates sequential elements output. */
5580 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5581 constructor_unfilled_index
5582 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5584 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5586 constructor_unfilled_fields
5587 = TREE_CHAIN (constructor_unfilled_fields);
5589 /* Skip any nameless bit fields. */
5590 while (constructor_unfilled_fields != 0
5591 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5592 && DECL_NAME (constructor_unfilled_fields) == 0)
5593 constructor_unfilled_fields =
5594 TREE_CHAIN (constructor_unfilled_fields);
5596 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5597 constructor_unfilled_fields = 0;
5599 /* Now output any pending elements which have become next. */
5601 output_pending_init_elements (0);
5604 /* Output any pending elements which have become next.
5605 As we output elements, constructor_unfilled_{fields,index}
5606 advances, which may cause other elements to become next;
5607 if so, they too are output.
5609 If ALL is 0, we return when there are
5610 no more pending elements to output now.
5612 If ALL is 1, we output space as necessary so that
5613 we can output all the pending elements. */
5616 output_pending_init_elements (int all)
5618 struct init_node *elt = constructor_pending_elts;
5623 /* Look through the whole pending tree.
5624 If we find an element that should be output now,
5625 output it. Otherwise, set NEXT to the element
5626 that comes first among those still pending. */
5631 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5633 if (tree_int_cst_equal (elt->purpose,
5634 constructor_unfilled_index))
5635 output_init_element (elt->value, true,
5636 TREE_TYPE (constructor_type),
5637 constructor_unfilled_index, 0);
5638 else if (tree_int_cst_lt (constructor_unfilled_index,
5641 /* Advance to the next smaller node. */
5646 /* We have reached the smallest node bigger than the
5647 current unfilled index. Fill the space first. */
5648 next = elt->purpose;
5654 /* Advance to the next bigger node. */
5659 /* We have reached the biggest node in a subtree. Find
5660 the parent of it, which is the next bigger node. */
5661 while (elt->parent && elt->parent->right == elt)
5664 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5667 next = elt->purpose;
5673 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5674 || TREE_CODE (constructor_type) == UNION_TYPE)
5676 tree ctor_unfilled_bitpos, elt_bitpos;
5678 /* If the current record is complete we are done. */
5679 if (constructor_unfilled_fields == 0)
5682 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5683 elt_bitpos = bit_position (elt->purpose);
5684 /* We can't compare fields here because there might be empty
5685 fields in between. */
5686 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5688 constructor_unfilled_fields = elt->purpose;
5689 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
5692 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5694 /* Advance to the next smaller node. */
5699 /* We have reached the smallest node bigger than the
5700 current unfilled field. Fill the space first. */
5701 next = elt->purpose;
5707 /* Advance to the next bigger node. */
5712 /* We have reached the biggest node in a subtree. Find
5713 the parent of it, which is the next bigger node. */
5714 while (elt->parent && elt->parent->right == elt)
5718 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5719 bit_position (elt->purpose))))
5721 next = elt->purpose;
5729 /* Ordinarily return, but not if we want to output all
5730 and there are elements left. */
5731 if (! (all && next != 0))
5734 /* If it's not incremental, just skip over the gap, so that after
5735 jumping to retry we will output the next successive element. */
5736 if (TREE_CODE (constructor_type) == RECORD_TYPE
5737 || TREE_CODE (constructor_type) == UNION_TYPE)
5738 constructor_unfilled_fields = next;
5739 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5740 constructor_unfilled_index = next;
5742 /* ELT now points to the node in the pending tree with the next
5743 initializer to output. */
5747 /* Add one non-braced element to the current constructor level.
5748 This adjusts the current position within the constructor's type.
5749 This may also start or terminate implicit levels
5750 to handle a partly-braced initializer.
5752 Once this has found the correct level for the new element,
5753 it calls output_init_element. */
5756 process_init_element (struct c_expr value)
5758 tree orig_value = value.value;
5759 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
5760 bool strict_string = value.original_code == STRING_CST;
5762 designator_depth = 0;
5763 designator_errorneous = 0;
5765 /* Handle superfluous braces around string cst as in
5766 char x[] = {"foo"}; */
5769 && TREE_CODE (constructor_type) == ARRAY_TYPE
5770 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
5771 && integer_zerop (constructor_unfilled_index))
5773 if (constructor_stack->replacement_value.value)
5774 error_init ("excess elements in char array initializer");
5775 constructor_stack->replacement_value = value;
5779 if (constructor_stack->replacement_value.value != 0)
5781 error_init ("excess elements in struct initializer");
5785 /* Ignore elements of a brace group if it is entirely superfluous
5786 and has already been diagnosed. */
5787 if (constructor_type == 0)
5790 /* If we've exhausted any levels that didn't have braces,
5792 while (constructor_stack->implicit)
5794 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5795 || TREE_CODE (constructor_type) == UNION_TYPE)
5796 && constructor_fields == 0)
5797 process_init_element (pop_init_level (1));
5798 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5799 && (constructor_max_index == 0
5800 || tree_int_cst_lt (constructor_max_index,
5801 constructor_index)))
5802 process_init_element (pop_init_level (1));
5807 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5808 if (constructor_range_stack)
5810 /* If value is a compound literal and we'll be just using its
5811 content, don't put it into a SAVE_EXPR. */
5812 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
5813 || !require_constant_value
5815 value.value = save_expr (value.value);
5820 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5823 enum tree_code fieldcode;
5825 if (constructor_fields == 0)
5827 pedwarn_init ("excess elements in struct initializer");
5831 fieldtype = TREE_TYPE (constructor_fields);
5832 if (fieldtype != error_mark_node)
5833 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5834 fieldcode = TREE_CODE (fieldtype);
5836 /* Error for non-static initialization of a flexible array member. */
5837 if (fieldcode == ARRAY_TYPE
5838 && !require_constant_value
5839 && TYPE_SIZE (fieldtype) == NULL_TREE
5840 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5842 error_init ("non-static initialization of a flexible array member");
5846 /* Accept a string constant to initialize a subarray. */
5847 if (value.value != 0
5848 && fieldcode == ARRAY_TYPE
5849 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
5851 value.value = orig_value;
5852 /* Otherwise, if we have come to a subaggregate,
5853 and we don't have an element of its type, push into it. */
5854 else if (value.value != 0 && !constructor_no_implicit
5855 && value.value != error_mark_node
5856 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
5857 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5858 || fieldcode == UNION_TYPE))
5860 push_init_level (1);
5866 push_member_name (constructor_fields);
5867 output_init_element (value.value, strict_string,
5868 fieldtype, constructor_fields, 1);
5869 RESTORE_SPELLING_DEPTH (constructor_depth);
5872 /* Do the bookkeeping for an element that was
5873 directly output as a constructor. */
5875 /* For a record, keep track of end position of last field. */
5876 if (DECL_SIZE (constructor_fields))
5877 constructor_bit_index
5878 = size_binop (PLUS_EXPR,
5879 bit_position (constructor_fields),
5880 DECL_SIZE (constructor_fields));
5882 /* If the current field was the first one not yet written out,
5883 it isn't now, so update. */
5884 if (constructor_unfilled_fields == constructor_fields)
5886 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5887 /* Skip any nameless bit fields. */
5888 while (constructor_unfilled_fields != 0
5889 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5890 && DECL_NAME (constructor_unfilled_fields) == 0)
5891 constructor_unfilled_fields =
5892 TREE_CHAIN (constructor_unfilled_fields);
5896 constructor_fields = TREE_CHAIN (constructor_fields);
5897 /* Skip any nameless bit fields at the beginning. */
5898 while (constructor_fields != 0
5899 && DECL_C_BIT_FIELD (constructor_fields)
5900 && DECL_NAME (constructor_fields) == 0)
5901 constructor_fields = TREE_CHAIN (constructor_fields);
5903 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5906 enum tree_code fieldcode;
5908 if (constructor_fields == 0)
5910 pedwarn_init ("excess elements in union initializer");
5914 fieldtype = TREE_TYPE (constructor_fields);
5915 if (fieldtype != error_mark_node)
5916 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5917 fieldcode = TREE_CODE (fieldtype);
5919 /* Warn that traditional C rejects initialization of unions.
5920 We skip the warning if the value is zero. This is done
5921 under the assumption that the zero initializer in user
5922 code appears conditioned on e.g. __STDC__ to avoid
5923 "missing initializer" warnings and relies on default
5924 initialization to zero in the traditional C case.
5925 We also skip the warning if the initializer is designated,
5926 again on the assumption that this must be conditional on
5927 __STDC__ anyway (and we've already complained about the
5928 member-designator already). */
5929 if (warn_traditional && !in_system_header && !constructor_designated
5930 && !(value.value && (integer_zerop (value.value)
5931 || real_zerop (value.value))))
5932 warning ("traditional C rejects initialization of unions");
5934 /* Accept a string constant to initialize a subarray. */
5935 if (value.value != 0
5936 && fieldcode == ARRAY_TYPE
5937 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
5939 value.value = orig_value;
5940 /* Otherwise, if we have come to a subaggregate,
5941 and we don't have an element of its type, push into it. */
5942 else if (value.value != 0 && !constructor_no_implicit
5943 && value.value != error_mark_node
5944 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
5945 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5946 || fieldcode == UNION_TYPE))
5948 push_init_level (1);
5954 push_member_name (constructor_fields);
5955 output_init_element (value.value, strict_string,
5956 fieldtype, constructor_fields, 1);
5957 RESTORE_SPELLING_DEPTH (constructor_depth);
5960 /* Do the bookkeeping for an element that was
5961 directly output as a constructor. */
5963 constructor_bit_index = DECL_SIZE (constructor_fields);
5964 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5967 constructor_fields = 0;
5969 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5971 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5972 enum tree_code eltcode = TREE_CODE (elttype);
5974 /* Accept a string constant to initialize a subarray. */
5975 if (value.value != 0
5976 && eltcode == ARRAY_TYPE
5977 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
5979 value.value = orig_value;
5980 /* Otherwise, if we have come to a subaggregate,
5981 and we don't have an element of its type, push into it. */
5982 else if (value.value != 0 && !constructor_no_implicit
5983 && value.value != error_mark_node
5984 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
5985 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
5986 || eltcode == UNION_TYPE))
5988 push_init_level (1);
5992 if (constructor_max_index != 0
5993 && (tree_int_cst_lt (constructor_max_index, constructor_index)
5994 || integer_all_onesp (constructor_max_index)))
5996 pedwarn_init ("excess elements in array initializer");
6000 /* Now output the actual element. */
6003 push_array_bounds (tree_low_cst (constructor_index, 0));
6004 output_init_element (value.value, strict_string,
6005 elttype, constructor_index, 1);
6006 RESTORE_SPELLING_DEPTH (constructor_depth);
6010 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6013 /* If we are doing the bookkeeping for an element that was
6014 directly output as a constructor, we must update
6015 constructor_unfilled_index. */
6016 constructor_unfilled_index = constructor_index;
6018 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6020 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6022 /* Do a basic check of initializer size. Note that vectors
6023 always have a fixed size derived from their type. */
6024 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6026 pedwarn_init ("excess elements in vector initializer");
6030 /* Now output the actual element. */
6032 output_init_element (value.value, strict_string,
6033 elttype, constructor_index, 1);
6036 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6039 /* If we are doing the bookkeeping for an element that was
6040 directly output as a constructor, we must update
6041 constructor_unfilled_index. */
6042 constructor_unfilled_index = constructor_index;
6045 /* Handle the sole element allowed in a braced initializer
6046 for a scalar variable. */
6047 else if (constructor_fields == 0)
6049 pedwarn_init ("excess elements in scalar initializer");
6055 output_init_element (value.value, strict_string,
6056 constructor_type, NULL_TREE, 1);
6057 constructor_fields = 0;
6060 /* Handle range initializers either at this level or anywhere higher
6061 in the designator stack. */
6062 if (constructor_range_stack)
6064 struct constructor_range_stack *p, *range_stack;
6067 range_stack = constructor_range_stack;
6068 constructor_range_stack = 0;
6069 while (constructor_stack != range_stack->stack)
6071 gcc_assert (constructor_stack->implicit);
6072 process_init_element (pop_init_level (1));
6074 for (p = range_stack;
6075 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6078 gcc_assert (constructor_stack->implicit);
6079 process_init_element (pop_init_level (1));
6082 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6083 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6088 constructor_index = p->index;
6089 constructor_fields = p->fields;
6090 if (finish && p->range_end && p->index == p->range_start)
6098 push_init_level (2);
6099 p->stack = constructor_stack;
6100 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6101 p->index = p->range_start;
6105 constructor_range_stack = range_stack;
6112 constructor_range_stack = 0;
6115 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6116 (guaranteed to be 'volatile' or null) and ARGS (represented using
6117 an ASM_EXPR node). */
6119 build_asm_stmt (tree cv_qualifier, tree args)
6121 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6122 ASM_VOLATILE_P (args) = 1;
6123 return add_stmt (args);
6126 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6127 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6128 SIMPLE indicates whether there was anything at all after the
6129 string in the asm expression -- asm("blah") and asm("blah" : )
6130 are subtly different. We use a ASM_EXPR node to represent this. */
6132 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6138 const char *constraint;
6139 bool allows_mem, allows_reg, is_inout;
6143 ninputs = list_length (inputs);
6144 noutputs = list_length (outputs);
6146 /* Remove output conversions that change the type but not the mode. */
6147 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6149 tree output = TREE_VALUE (tail);
6150 STRIP_NOPS (output);
6151 TREE_VALUE (tail) = output;
6152 lvalue_or_else (output, "invalid lvalue in asm statement");
6154 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6156 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
6157 &allows_mem, &allows_reg, &is_inout))
6159 /* By marking this operand as erroneous, we will not try
6160 to process this operand again in expand_asm_operands. */
6161 TREE_VALUE (tail) = error_mark_node;
6165 /* If the operand is a DECL that is going to end up in
6166 memory, assume it is addressable. This is a bit more
6167 conservative than it would ideally be; the exact test is
6168 buried deep in expand_asm_operands and depends on the
6169 DECL_RTL for the OPERAND -- which we don't have at this
6171 if (!allows_reg && DECL_P (output))
6172 c_mark_addressable (output);
6175 /* Perform default conversions on array and function inputs.
6176 Don't do this for other types as it would screw up operands
6177 expected to be in memory. */
6178 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6179 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6181 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6183 /* Simple asm statements are treated as volatile. */
6186 ASM_VOLATILE_P (args) = 1;
6187 ASM_INPUT_P (args) = 1;
6192 /* Generate a goto statement to LABEL. */
6195 c_finish_goto_label (tree label)
6197 tree decl = lookup_label (label);
6201 TREE_USED (decl) = 1;
6202 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6205 /* Generate a computed goto statement to EXPR. */
6208 c_finish_goto_ptr (tree expr)
6211 pedwarn ("ISO C forbids `goto *expr;'");
6212 expr = convert (ptr_type_node, expr);
6213 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6216 /* Generate a C `return' statement. RETVAL is the expression for what
6217 to return, or a null pointer for `return;' with no value. */
6220 c_finish_return (tree retval)
6222 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6224 if (TREE_THIS_VOLATILE (current_function_decl))
6225 warning ("function declared `noreturn' has a `return' statement");
6229 current_function_returns_null = 1;
6230 if ((warn_return_type || flag_isoc99)
6231 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6232 pedwarn_c99 ("`return' with no value, in function returning non-void");
6234 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6236 current_function_returns_null = 1;
6237 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6238 pedwarn ("`return' with a value, in function returning void");
6242 tree t = convert_for_assignment (valtype, retval, _("return"),
6243 NULL_TREE, NULL_TREE, 0);
6244 tree res = DECL_RESULT (current_function_decl);
6247 current_function_returns_value = 1;
6248 if (t == error_mark_node)
6251 inner = t = convert (TREE_TYPE (res), t);
6253 /* Strip any conversions, additions, and subtractions, and see if
6254 we are returning the address of a local variable. Warn if so. */
6257 switch (TREE_CODE (inner))
6259 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6261 inner = TREE_OPERAND (inner, 0);
6265 /* If the second operand of the MINUS_EXPR has a pointer
6266 type (or is converted from it), this may be valid, so
6267 don't give a warning. */
6269 tree op1 = TREE_OPERAND (inner, 1);
6271 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6272 && (TREE_CODE (op1) == NOP_EXPR
6273 || TREE_CODE (op1) == NON_LVALUE_EXPR
6274 || TREE_CODE (op1) == CONVERT_EXPR))
6275 op1 = TREE_OPERAND (op1, 0);
6277 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6280 inner = TREE_OPERAND (inner, 0);
6285 inner = TREE_OPERAND (inner, 0);
6287 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r'
6288 && TREE_CODE (inner) != INDIRECT_REF)
6289 inner = TREE_OPERAND (inner, 0);
6292 && ! DECL_EXTERNAL (inner)
6293 && ! TREE_STATIC (inner)
6294 && DECL_CONTEXT (inner) == current_function_decl)
6295 warning ("function returns address of local variable");
6305 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6308 return add_stmt (build_stmt (RETURN_EXPR, retval));
6312 /* The SWITCH_STMT being built. */
6315 /* The original type of the testing expression, ie. before the
6316 default conversion is applied. */
6319 /* A splay-tree mapping the low element of a case range to the high
6320 element, or NULL_TREE if there is no high element. Used to
6321 determine whether or not a new case label duplicates an old case
6322 label. We need a tree, rather than simply a hash table, because
6323 of the GNU case range extension. */
6326 /* The next node on the stack. */
6327 struct c_switch *next;
6330 /* A stack of the currently active switch statements. The innermost
6331 switch statement is on the top of the stack. There is no need to
6332 mark the stack for garbage collection because it is only active
6333 during the processing of the body of a function, and we never
6334 collect at that point. */
6336 struct c_switch *c_switch_stack;
6338 /* Start a C switch statement, testing expression EXP. Return the new
6342 c_start_case (tree exp)
6344 enum tree_code code;
6345 tree type, orig_type = error_mark_node;
6346 struct c_switch *cs;
6348 if (exp != error_mark_node)
6350 code = TREE_CODE (TREE_TYPE (exp));
6351 orig_type = TREE_TYPE (exp);
6353 if (! INTEGRAL_TYPE_P (orig_type)
6354 && code != ERROR_MARK)
6356 error ("switch quantity not an integer");
6357 exp = integer_zero_node;
6361 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6363 if (warn_traditional && !in_system_header
6364 && (type == long_integer_type_node
6365 || type == long_unsigned_type_node))
6366 warning ("`long' switch expression not converted to `int' in ISO C");
6368 exp = default_conversion (exp);
6369 type = TREE_TYPE (exp);
6373 /* Add this new SWITCH_STMT to the stack. */
6374 cs = XNEW (struct c_switch);
6375 cs->switch_stmt = build_stmt ((enum tree_code) SWITCH_STMT, exp, NULL_TREE,
6377 cs->orig_type = orig_type;
6378 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6379 cs->next = c_switch_stack;
6380 c_switch_stack = cs;
6382 return add_stmt (cs->switch_stmt);
6385 /* Process a case label. */
6388 do_case (tree low_value, tree high_value)
6390 tree label = NULL_TREE;
6394 label = c_add_case_label (c_switch_stack->cases,
6395 SWITCH_COND (c_switch_stack->switch_stmt),
6396 c_switch_stack->orig_type,
6397 low_value, high_value);
6398 if (label == error_mark_node)
6402 error ("case label not within a switch statement");
6404 error ("`default' label not within a switch statement");
6409 /* Finish the switch statement. */
6412 c_finish_case (tree body)
6414 struct c_switch *cs = c_switch_stack;
6416 SWITCH_BODY (cs->switch_stmt) = body;
6418 /* Emit warnings as needed. */
6419 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6421 /* Pop the stack. */
6422 c_switch_stack = cs->next;
6423 splay_tree_delete (cs->cases);
6427 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6428 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6429 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6430 statement, and was not surrounded with parenthesis. */
6433 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6434 tree else_block, bool nested_if)
6438 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6439 if (warn_parentheses && nested_if && else_block == NULL)
6441 tree inner_if = then_block;
6443 /* We know from the grammar productions that there is an IF nested
6444 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6445 it might not be exactly THEN_BLOCK, but should be the last
6446 non-container statement within. */
6448 switch (TREE_CODE (inner_if))
6453 inner_if = BIND_EXPR_BODY (inner_if);
6455 case STATEMENT_LIST:
6456 inner_if = expr_last (then_block);
6458 case TRY_FINALLY_EXPR:
6459 case TRY_CATCH_EXPR:
6460 inner_if = TREE_OPERAND (inner_if, 0);
6467 if (COND_EXPR_ELSE (inner_if))
6468 warning ("%Hsuggest explicit braces to avoid ambiguous `else'",
6472 /* Diagnose ";" via the special empty statement node that we create. */
6475 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6478 warning ("%Hempty body in an if-statement",
6479 EXPR_LOCUS (then_block));
6480 then_block = alloc_stmt_list ();
6483 && TREE_CODE (else_block) == NOP_EXPR
6484 && !TREE_TYPE (else_block))
6486 warning ("%Hempty body in an else-statement",
6487 EXPR_LOCUS (else_block));
6488 else_block = alloc_stmt_list ();
6492 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6493 SET_EXPR_LOCATION (stmt, if_locus);
6497 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6498 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6499 is false for DO loops. INCR is the FOR increment expression. BODY is
6500 the statement controlled by the loop. BLAB is the break label. CLAB is
6501 the continue label. Everything is allowed to be NULL. */
6504 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6505 tree blab, tree clab, bool cond_is_first)
6507 tree entry = NULL, exit = NULL, t;
6509 /* Detect do { ... } while (0) and don't generate loop construct. */
6510 if (cond && !cond_is_first && integer_zerop (cond))
6512 if (cond_is_first || cond)
6514 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6516 /* If we have an exit condition, then we build an IF with gotos either
6517 out of the loop, or to the top of it. If there's no exit condition,
6518 then we just build a jump back to the top. */
6519 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6523 /* Canonicalize the loop condition to the end. This means
6524 generating a branch to the loop condition. Reuse the
6525 continue label, if possible. */
6530 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6531 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6534 t = build1 (GOTO_EXPR, void_type_node, clab);
6535 SET_EXPR_LOCATION (t, start_locus);
6539 t = build_and_jump (&blab);
6540 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
6543 SET_EXPR_LOCATION (exit, start_locus);
6545 SET_EXPR_LOCATION (exit, input_location);
6554 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6562 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
6566 c_finish_bc_stmt (tree *label_p, bool is_break)
6568 tree label = *label_p;
6571 *label_p = label = create_artificial_label ();
6572 else if (TREE_CODE (label) != LABEL_DECL)
6575 error ("break statement not within loop or switch");
6577 error ("continue statement not within a loop");
6581 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
6584 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6587 emit_side_effect_warnings (tree expr)
6589 if (expr == error_mark_node)
6591 else if (!TREE_SIDE_EFFECTS (expr))
6593 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6594 warning ("%Hstatement with no effect",
6595 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
6597 else if (warn_unused_value)
6598 warn_if_unused_value (expr, input_location);
6601 /* Process an expression as if it were a complete statement. Emit
6602 diagnostics, but do not call ADD_STMT. */
6605 c_process_expr_stmt (tree expr)
6610 /* Do default conversion if safe and possibly important,
6611 in case within ({...}). */
6612 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
6613 && (flag_isoc99 || lvalue_p (expr)))
6614 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
6615 expr = default_conversion (expr);
6617 if (warn_sequence_point)
6618 verify_sequence_points (expr);
6620 if (TREE_TYPE (expr) != error_mark_node
6621 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
6622 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
6623 error ("expression statement has incomplete type");
6625 /* If we're not processing a statement expression, warn about unused values.
6626 Warnings for statement expressions will be emitted later, once we figure
6627 out which is the result. */
6628 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6629 && (extra_warnings || warn_unused_value))
6630 emit_side_effect_warnings (expr);
6632 /* If the expression is not of a type to which we cannot assign a line
6633 number, wrap the thing in a no-op NOP_EXPR. */
6634 if (DECL_P (expr) || TREE_CODE_CLASS (TREE_CODE (expr)) == 'c')
6635 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
6638 SET_EXPR_LOCATION (expr, input_location);
6643 /* Emit an expression as a statement. */
6646 c_finish_expr_stmt (tree expr)
6649 return add_stmt (c_process_expr_stmt (expr));
6654 /* Do the opposite and emit a statement as an expression. To begin,
6655 create a new binding level and return it. */
6658 c_begin_stmt_expr (void)
6662 /* We must force a BLOCK for this level so that, if it is not expanded
6663 later, there is a way to turn off the entire subtree of blocks that
6664 are contained in it. */
6666 ret = c_begin_compound_stmt (true);
6668 /* Mark the current statement list as belonging to a statement list. */
6669 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6675 c_finish_stmt_expr (tree body)
6677 tree last, type, tmp, val;
6680 body = c_end_compound_stmt (body, true);
6682 /* Locate the last statement in BODY. See c_end_compound_stmt
6683 about always returning a BIND_EXPR. */
6684 last_p = &BIND_EXPR_BODY (body);
6685 last = BIND_EXPR_BODY (body);
6688 if (TREE_CODE (last) == STATEMENT_LIST)
6690 tree_stmt_iterator i;
6692 /* This can happen with degenerate cases like ({ }). No value. */
6693 if (!TREE_SIDE_EFFECTS (last))
6696 /* If we're supposed to generate side effects warnings, process
6697 all of the statements except the last. */
6698 if (extra_warnings || warn_unused_value)
6700 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
6701 emit_side_effect_warnings (tsi_stmt (i));
6704 i = tsi_last (last);
6705 last_p = tsi_stmt_ptr (i);
6709 /* If the end of the list is exception related, then the list was split
6710 by a call to push_cleanup. Continue searching. */
6711 if (TREE_CODE (last) == TRY_FINALLY_EXPR
6712 || TREE_CODE (last) == TRY_CATCH_EXPR)
6714 last_p = &TREE_OPERAND (last, 0);
6716 goto continue_searching;
6719 /* In the case that the BIND_EXPR is not necessary, return the
6720 expression out from inside it. */
6721 if (last == error_mark_node
6722 || (last == BIND_EXPR_BODY (body)
6723 && BIND_EXPR_VARS (body) == NULL))
6726 /* Extract the type of said expression. */
6727 type = TREE_TYPE (last);
6729 /* If we're not returning a value at all, then the BIND_EXPR that
6730 we already have is a fine expression to return. */
6731 if (!type || VOID_TYPE_P (type))
6734 /* Now that we've located the expression containing the value, it seems
6735 silly to make voidify_wrapper_expr repeat the process. Create a
6736 temporary of the appropriate type and stick it in a TARGET_EXPR. */
6737 tmp = create_tmp_var_raw (type, NULL);
6739 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
6740 tree_expr_nonnegative_p giving up immediately. */
6742 if (TREE_CODE (val) == NOP_EXPR
6743 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
6744 val = TREE_OPERAND (val, 0);
6746 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
6747 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
6749 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
6752 /* Begin and end compound statements. This is as simple as pushing
6753 and popping new statement lists from the tree. */
6756 c_begin_compound_stmt (bool do_scope)
6758 tree stmt = push_stmt_list ();
6765 c_end_compound_stmt (tree stmt, bool do_scope)
6771 if (c_dialect_objc ())
6772 objc_clear_super_receiver ();
6773 block = pop_scope ();
6776 stmt = pop_stmt_list (stmt);
6777 stmt = c_build_bind_expr (block, stmt);
6779 /* If this compound statement is nested immediately inside a statement
6780 expression, then force a BIND_EXPR to be created. Otherwise we'll
6781 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
6782 STATEMENT_LISTs merge, and thus we can lose track of what statement
6785 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6786 && TREE_CODE (stmt) != BIND_EXPR)
6788 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
6789 TREE_SIDE_EFFECTS (stmt) = 1;
6795 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
6796 when the current scope is exited. EH_ONLY is true when this is not
6797 meant to apply to normal control flow transfer. */
6800 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
6802 enum tree_code code;
6806 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
6807 stmt = build_stmt (code, NULL, cleanup);
6809 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
6810 list = push_stmt_list ();
6811 TREE_OPERAND (stmt, 0) = list;
6812 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
6815 /* Build a binary-operation expression without default conversions.
6816 CODE is the kind of expression to build.
6817 This function differs from `build' in several ways:
6818 the data type of the result is computed and recorded in it,
6819 warnings are generated if arg data types are invalid,
6820 special handling for addition and subtraction of pointers is known,
6821 and some optimization is done (operations on narrow ints
6822 are done in the narrower type when that gives the same result).
6823 Constant folding is also done before the result is returned.
6825 Note that the operands will never have enumeral types, or function
6826 or array types, because either they will have the default conversions
6827 performed or they have both just been converted to some other type in which
6828 the arithmetic is to be done. */
6831 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6835 enum tree_code code0, code1;
6838 /* Expression code to give to the expression when it is built.
6839 Normally this is CODE, which is what the caller asked for,
6840 but in some special cases we change it. */
6841 enum tree_code resultcode = code;
6843 /* Data type in which the computation is to be performed.
6844 In the simplest cases this is the common type of the arguments. */
6845 tree result_type = NULL;
6847 /* Nonzero means operands have already been type-converted
6848 in whatever way is necessary.
6849 Zero means they need to be converted to RESULT_TYPE. */
6852 /* Nonzero means create the expression with this type, rather than
6854 tree build_type = 0;
6856 /* Nonzero means after finally constructing the expression
6857 convert it to this type. */
6858 tree final_type = 0;
6860 /* Nonzero if this is an operation like MIN or MAX which can
6861 safely be computed in short if both args are promoted shorts.
6862 Also implies COMMON.
6863 -1 indicates a bitwise operation; this makes a difference
6864 in the exact conditions for when it is safe to do the operation
6865 in a narrower mode. */
6868 /* Nonzero if this is a comparison operation;
6869 if both args are promoted shorts, compare the original shorts.
6870 Also implies COMMON. */
6871 int short_compare = 0;
6873 /* Nonzero if this is a right-shift operation, which can be computed on the
6874 original short and then promoted if the operand is a promoted short. */
6875 int short_shift = 0;
6877 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6882 op0 = default_conversion (orig_op0);
6883 op1 = default_conversion (orig_op1);
6891 type0 = TREE_TYPE (op0);
6892 type1 = TREE_TYPE (op1);
6894 /* The expression codes of the data types of the arguments tell us
6895 whether the arguments are integers, floating, pointers, etc. */
6896 code0 = TREE_CODE (type0);
6897 code1 = TREE_CODE (type1);
6899 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6900 STRIP_TYPE_NOPS (op0);
6901 STRIP_TYPE_NOPS (op1);
6903 /* If an error was already reported for one of the arguments,
6904 avoid reporting another error. */
6906 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6907 return error_mark_node;
6912 /* Handle the pointer + int case. */
6913 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6914 return pointer_int_sum (PLUS_EXPR, op0, op1);
6915 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6916 return pointer_int_sum (PLUS_EXPR, op1, op0);
6922 /* Subtraction of two similar pointers.
6923 We must subtract them as integers, then divide by object size. */
6924 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6925 && comp_target_types (type0, type1, 1))
6926 return pointer_diff (op0, op1);
6927 /* Handle pointer minus int. Just like pointer plus int. */
6928 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6929 return pointer_int_sum (MINUS_EXPR, op0, op1);
6938 case TRUNC_DIV_EXPR:
6940 case FLOOR_DIV_EXPR:
6941 case ROUND_DIV_EXPR:
6942 case EXACT_DIV_EXPR:
6943 /* Floating point division by zero is a legitimate way to obtain
6944 infinities and NaNs. */
6945 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6946 warning ("division by zero");
6948 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6949 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6950 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6951 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6953 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6954 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
6955 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
6956 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
6958 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6959 resultcode = RDIV_EXPR;
6961 /* Although it would be tempting to shorten always here, that
6962 loses on some targets, since the modulo instruction is
6963 undefined if the quotient can't be represented in the
6964 computation mode. We shorten only if unsigned or if
6965 dividing by something we know != -1. */
6966 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
6967 || (TREE_CODE (op1) == INTEGER_CST
6968 && ! integer_all_onesp (op1)));
6976 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6978 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6982 case TRUNC_MOD_EXPR:
6983 case FLOOR_MOD_EXPR:
6984 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6985 warning ("division by zero");
6987 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6989 /* Although it would be tempting to shorten always here, that loses
6990 on some targets, since the modulo instruction is undefined if the
6991 quotient can't be represented in the computation mode. We shorten
6992 only if unsigned or if dividing by something we know != -1. */
6993 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
6994 || (TREE_CODE (op1) == INTEGER_CST
6995 && ! integer_all_onesp (op1)));
7000 case TRUTH_ANDIF_EXPR:
7001 case TRUTH_ORIF_EXPR:
7002 case TRUTH_AND_EXPR:
7004 case TRUTH_XOR_EXPR:
7005 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7006 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7007 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7008 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7010 /* Result of these operations is always an int,
7011 but that does not mean the operands should be
7012 converted to ints! */
7013 result_type = integer_type_node;
7014 op0 = lang_hooks.truthvalue_conversion (op0);
7015 op1 = lang_hooks.truthvalue_conversion (op1);
7020 /* Shift operations: result has same type as first operand;
7021 always convert second operand to int.
7022 Also set SHORT_SHIFT if shifting rightward. */
7025 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7027 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7029 if (tree_int_cst_sgn (op1) < 0)
7030 warning ("right shift count is negative");
7033 if (! integer_zerop (op1))
7036 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7037 warning ("right shift count >= width of type");
7041 /* Use the type of the value to be shifted. */
7042 result_type = type0;
7043 /* Convert the shift-count to an integer, regardless of size
7044 of value being shifted. */
7045 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7046 op1 = convert (integer_type_node, op1);
7047 /* Avoid converting op1 to result_type later. */
7053 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7055 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7057 if (tree_int_cst_sgn (op1) < 0)
7058 warning ("left shift count is negative");
7060 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7061 warning ("left shift count >= width of type");
7064 /* Use the type of the value to be shifted. */
7065 result_type = type0;
7066 /* Convert the shift-count to an integer, regardless of size
7067 of value being shifted. */
7068 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7069 op1 = convert (integer_type_node, op1);
7070 /* Avoid converting op1 to result_type later. */
7077 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7079 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7081 if (tree_int_cst_sgn (op1) < 0)
7082 warning ("shift count is negative");
7083 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7084 warning ("shift count >= width of type");
7087 /* Use the type of the value to be shifted. */
7088 result_type = type0;
7089 /* Convert the shift-count to an integer, regardless of size
7090 of value being shifted. */
7091 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7092 op1 = convert (integer_type_node, op1);
7093 /* Avoid converting op1 to result_type later. */
7100 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7101 warning ("comparing floating point with == or != is unsafe");
7102 /* Result of comparison is always int,
7103 but don't convert the args to int! */
7104 build_type = integer_type_node;
7105 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7106 || code0 == COMPLEX_TYPE)
7107 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7108 || code1 == COMPLEX_TYPE))
7110 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7112 tree tt0 = TREE_TYPE (type0);
7113 tree tt1 = TREE_TYPE (type1);
7114 /* Anything compares with void *. void * compares with anything.
7115 Otherwise, the targets must be compatible
7116 and both must be object or both incomplete. */
7117 if (comp_target_types (type0, type1, 1))
7118 result_type = common_pointer_type (type0, type1);
7119 else if (VOID_TYPE_P (tt0))
7121 /* op0 != orig_op0 detects the case of something
7122 whose value is 0 but which isn't a valid null ptr const. */
7123 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7124 && TREE_CODE (tt1) == FUNCTION_TYPE)
7125 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
7127 else if (VOID_TYPE_P (tt1))
7129 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7130 && TREE_CODE (tt0) == FUNCTION_TYPE)
7131 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
7134 pedwarn ("comparison of distinct pointer types lacks a cast");
7136 if (result_type == NULL_TREE)
7137 result_type = ptr_type_node;
7139 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7140 && integer_zerop (op1))
7141 result_type = type0;
7142 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7143 && integer_zerop (op0))
7144 result_type = type1;
7145 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7147 result_type = type0;
7148 pedwarn ("comparison between pointer and integer");
7150 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7152 result_type = type1;
7153 pedwarn ("comparison between pointer and integer");
7159 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7160 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7162 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7164 if (comp_target_types (type0, type1, 1))
7166 result_type = common_pointer_type (type0, type1);
7168 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7169 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7173 result_type = ptr_type_node;
7174 pedwarn ("comparison of distinct pointer types lacks a cast");
7183 build_type = integer_type_node;
7184 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7185 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7187 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7189 if (comp_target_types (type0, type1, 1))
7191 result_type = common_pointer_type (type0, type1);
7192 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7193 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7194 pedwarn ("comparison of complete and incomplete pointers");
7196 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7197 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7201 result_type = ptr_type_node;
7202 pedwarn ("comparison of distinct pointer types lacks a cast");
7205 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7206 && integer_zerop (op1))
7208 result_type = type0;
7209 if (pedantic || extra_warnings)
7210 pedwarn ("ordered comparison of pointer with integer zero");
7212 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7213 && integer_zerop (op0))
7215 result_type = type1;
7217 pedwarn ("ordered comparison of pointer with integer zero");
7219 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7221 result_type = type0;
7222 pedwarn ("comparison between pointer and integer");
7224 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7226 result_type = type1;
7227 pedwarn ("comparison between pointer and integer");
7231 case UNORDERED_EXPR:
7239 build_type = integer_type_node;
7240 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
7242 error ("unordered comparison on non-floating point argument");
7243 return error_mark_node;
7252 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7253 return error_mark_node;
7255 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7256 || code0 == VECTOR_TYPE)
7258 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7259 || code1 == VECTOR_TYPE))
7261 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7263 if (shorten || common || short_compare)
7264 result_type = common_type (type0, type1);
7266 /* For certain operations (which identify themselves by shorten != 0)
7267 if both args were extended from the same smaller type,
7268 do the arithmetic in that type and then extend.
7270 shorten !=0 and !=1 indicates a bitwise operation.
7271 For them, this optimization is safe only if
7272 both args are zero-extended or both are sign-extended.
7273 Otherwise, we might change the result.
7274 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7275 but calculated in (unsigned short) it would be (unsigned short)-1. */
7277 if (shorten && none_complex)
7279 int unsigned0, unsigned1;
7280 tree arg0 = get_narrower (op0, &unsigned0);
7281 tree arg1 = get_narrower (op1, &unsigned1);
7282 /* UNS is 1 if the operation to be done is an unsigned one. */
7283 int uns = TYPE_UNSIGNED (result_type);
7286 final_type = result_type;
7288 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7289 but it *requires* conversion to FINAL_TYPE. */
7291 if ((TYPE_PRECISION (TREE_TYPE (op0))
7292 == TYPE_PRECISION (TREE_TYPE (arg0)))
7293 && TREE_TYPE (op0) != final_type)
7294 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7295 if ((TYPE_PRECISION (TREE_TYPE (op1))
7296 == TYPE_PRECISION (TREE_TYPE (arg1)))
7297 && TREE_TYPE (op1) != final_type)
7298 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7300 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7302 /* For bitwise operations, signedness of nominal type
7303 does not matter. Consider only how operands were extended. */
7307 /* Note that in all three cases below we refrain from optimizing
7308 an unsigned operation on sign-extended args.
7309 That would not be valid. */
7311 /* Both args variable: if both extended in same way
7312 from same width, do it in that width.
7313 Do it unsigned if args were zero-extended. */
7314 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7315 < TYPE_PRECISION (result_type))
7316 && (TYPE_PRECISION (TREE_TYPE (arg1))
7317 == TYPE_PRECISION (TREE_TYPE (arg0)))
7318 && unsigned0 == unsigned1
7319 && (unsigned0 || !uns))
7321 = c_common_signed_or_unsigned_type
7322 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7323 else if (TREE_CODE (arg0) == INTEGER_CST
7324 && (unsigned1 || !uns)
7325 && (TYPE_PRECISION (TREE_TYPE (arg1))
7326 < TYPE_PRECISION (result_type))
7328 = c_common_signed_or_unsigned_type (unsigned1,
7330 int_fits_type_p (arg0, type)))
7332 else if (TREE_CODE (arg1) == INTEGER_CST
7333 && (unsigned0 || !uns)
7334 && (TYPE_PRECISION (TREE_TYPE (arg0))
7335 < TYPE_PRECISION (result_type))
7337 = c_common_signed_or_unsigned_type (unsigned0,
7339 int_fits_type_p (arg1, type)))
7343 /* Shifts can be shortened if shifting right. */
7348 tree arg0 = get_narrower (op0, &unsigned_arg);
7350 final_type = result_type;
7352 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7353 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7355 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7356 /* We can shorten only if the shift count is less than the
7357 number of bits in the smaller type size. */
7358 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7359 /* We cannot drop an unsigned shift after sign-extension. */
7360 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7362 /* Do an unsigned shift if the operand was zero-extended. */
7364 = c_common_signed_or_unsigned_type (unsigned_arg,
7366 /* Convert value-to-be-shifted to that type. */
7367 if (TREE_TYPE (op0) != result_type)
7368 op0 = convert (result_type, op0);
7373 /* Comparison operations are shortened too but differently.
7374 They identify themselves by setting short_compare = 1. */
7378 /* Don't write &op0, etc., because that would prevent op0
7379 from being kept in a register.
7380 Instead, make copies of the our local variables and
7381 pass the copies by reference, then copy them back afterward. */
7382 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7383 enum tree_code xresultcode = resultcode;
7385 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7390 op0 = xop0, op1 = xop1;
7392 resultcode = xresultcode;
7394 if (warn_sign_compare && skip_evaluation == 0)
7396 int op0_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7397 int op1_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7398 int unsignedp0, unsignedp1;
7399 tree primop0 = get_narrower (op0, &unsignedp0);
7400 tree primop1 = get_narrower (op1, &unsignedp1);
7404 STRIP_TYPE_NOPS (xop0);
7405 STRIP_TYPE_NOPS (xop1);
7407 /* Give warnings for comparisons between signed and unsigned
7408 quantities that may fail.
7410 Do the checking based on the original operand trees, so that
7411 casts will be considered, but default promotions won't be.
7413 Do not warn if the comparison is being done in a signed type,
7414 since the signed type will only be chosen if it can represent
7415 all the values of the unsigned type. */
7416 if (! TYPE_UNSIGNED (result_type))
7418 /* Do not warn if both operands are the same signedness. */
7419 else if (op0_signed == op1_signed)
7426 sop = xop0, uop = xop1;
7428 sop = xop1, uop = xop0;
7430 /* Do not warn if the signed quantity is an
7431 unsuffixed integer literal (or some static
7432 constant expression involving such literals or a
7433 conditional expression involving such literals)
7434 and it is non-negative. */
7435 if (tree_expr_nonnegative_p (sop))
7437 /* Do not warn if the comparison is an equality operation,
7438 the unsigned quantity is an integral constant, and it
7439 would fit in the result if the result were signed. */
7440 else if (TREE_CODE (uop) == INTEGER_CST
7441 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7443 (uop, c_common_signed_type (result_type)))
7445 /* Do not warn if the unsigned quantity is an enumeration
7446 constant and its maximum value would fit in the result
7447 if the result were signed. */
7448 else if (TREE_CODE (uop) == INTEGER_CST
7449 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7451 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7452 c_common_signed_type (result_type)))
7455 warning ("comparison between signed and unsigned");
7458 /* Warn if two unsigned values are being compared in a size
7459 larger than their original size, and one (and only one) is the
7460 result of a `~' operator. This comparison will always fail.
7462 Also warn if one operand is a constant, and the constant
7463 does not have all bits set that are set in the ~ operand
7464 when it is extended. */
7466 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7467 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7469 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7470 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7473 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7476 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7479 HOST_WIDE_INT constant, mask;
7480 int unsignedp, bits;
7482 if (host_integerp (primop0, 0))
7485 unsignedp = unsignedp1;
7486 constant = tree_low_cst (primop0, 0);
7491 unsignedp = unsignedp0;
7492 constant = tree_low_cst (primop1, 0);
7495 bits = TYPE_PRECISION (TREE_TYPE (primop));
7496 if (bits < TYPE_PRECISION (result_type)
7497 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7499 mask = (~ (HOST_WIDE_INT) 0) << bits;
7500 if ((mask & constant) != mask)
7501 warning ("comparison of promoted ~unsigned with constant");
7504 else if (unsignedp0 && unsignedp1
7505 && (TYPE_PRECISION (TREE_TYPE (primop0))
7506 < TYPE_PRECISION (result_type))
7507 && (TYPE_PRECISION (TREE_TYPE (primop1))
7508 < TYPE_PRECISION (result_type)))
7509 warning ("comparison of promoted ~unsigned with unsigned");
7515 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7516 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7517 Then the expression will be built.
7518 It will be given type FINAL_TYPE if that is nonzero;
7519 otherwise, it will be given type RESULT_TYPE. */
7523 binary_op_error (code);
7524 return error_mark_node;
7529 if (TREE_TYPE (op0) != result_type)
7530 op0 = convert (result_type, op0);
7531 if (TREE_TYPE (op1) != result_type)
7532 op1 = convert (result_type, op1);
7534 /* This can happen if one operand has a vector type, and the other
7535 has a different type. */
7536 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
7537 return error_mark_node;
7540 if (build_type == NULL_TREE)
7541 build_type = result_type;
7544 tree result = build2 (resultcode, build_type, op0, op1);
7546 /* Treat expressions in initializers specially as they can't trap. */
7547 result = require_constant_value ? fold_initializer (result)
7550 if (final_type != 0)
7551 result = convert (final_type, result);