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, int);
76 static void output_init_element (tree, 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)
249 /* For two pointers, do this recursively on the target type. */
251 tree pointed_to_1 = TREE_TYPE (t1);
252 tree pointed_to_2 = TREE_TYPE (t2);
253 tree target = composite_type (pointed_to_1, pointed_to_2);
254 t1 = build_pointer_type (target);
255 t1 = build_type_attribute_variant (t1, attributes);
256 return qualify_type (t1, t2);
261 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
263 /* We should not have any type quals on arrays at all. */
264 if (TYPE_QUALS (t1) || TYPE_QUALS (t2))
267 /* Save space: see if the result is identical to one of the args. */
268 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
269 return build_type_attribute_variant (t1, attributes);
270 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
271 return build_type_attribute_variant (t2, attributes);
273 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
274 return build_type_attribute_variant (t1, attributes);
275 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
276 return build_type_attribute_variant (t2, attributes);
278 /* Merge the element types, and have a size if either arg has one. */
279 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
280 return build_type_attribute_variant (t1, attributes);
284 /* Function types: prefer the one that specified arg types.
285 If both do, merge the arg types. Also merge the return types. */
287 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
288 tree p1 = TYPE_ARG_TYPES (t1);
289 tree p2 = TYPE_ARG_TYPES (t2);
294 /* Save space: see if the result is identical to one of the args. */
295 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
296 return build_type_attribute_variant (t1, attributes);
297 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
298 return build_type_attribute_variant (t2, attributes);
300 /* Simple way if one arg fails to specify argument types. */
301 if (TYPE_ARG_TYPES (t1) == 0)
303 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
304 t1 = build_type_attribute_variant (t1, attributes);
305 return qualify_type (t1, t2);
307 if (TYPE_ARG_TYPES (t2) == 0)
309 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
310 t1 = build_type_attribute_variant (t1, attributes);
311 return qualify_type (t1, t2);
314 /* If both args specify argument types, we must merge the two
315 lists, argument by argument. */
316 /* Tell global_bindings_p to return false so that variable_size
317 doesn't abort on VLAs in parameter types. */
318 c_override_global_bindings_to_false = true;
320 len = list_length (p1);
323 for (i = 0; i < len; i++)
324 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
329 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
331 /* A null type means arg type is not specified.
332 Take whatever the other function type has. */
333 if (TREE_VALUE (p1) == 0)
335 TREE_VALUE (n) = TREE_VALUE (p2);
338 if (TREE_VALUE (p2) == 0)
340 TREE_VALUE (n) = TREE_VALUE (p1);
344 /* Given wait (union {union wait *u; int *i} *)
345 and wait (union wait *),
346 prefer union wait * as type of parm. */
347 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
348 && TREE_VALUE (p1) != TREE_VALUE (p2))
351 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
352 memb; memb = TREE_CHAIN (memb))
353 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
355 TREE_VALUE (n) = TREE_VALUE (p2);
357 pedwarn ("function types not truly compatible in ISO C");
361 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
362 && TREE_VALUE (p2) != TREE_VALUE (p1))
365 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
366 memb; memb = TREE_CHAIN (memb))
367 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
369 TREE_VALUE (n) = TREE_VALUE (p1);
371 pedwarn ("function types not truly compatible in ISO C");
375 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
379 c_override_global_bindings_to_false = false;
380 t1 = build_function_type (valtype, newargs);
381 t1 = qualify_type (t1, t2);
382 /* ... falls through ... */
386 return build_type_attribute_variant (t1, attributes);
391 /* Return the type of a conditional expression between pointers to
392 possibly differently qualified versions of compatible types.
394 We assume that comp_target_types has already been done and returned
395 nonzero; if that isn't so, this may crash. */
398 common_pointer_type (tree t1, tree t2)
405 /* Save time if the two types are the same. */
407 if (t1 == t2) return t1;
409 /* If one type is nonsense, use the other. */
410 if (t1 == error_mark_node)
412 if (t2 == error_mark_node)
415 if (TREE_CODE (t1) != POINTER_TYPE || TREE_CODE (t2) != POINTER_TYPE)
418 /* Merge the attributes. */
419 attributes = targetm.merge_type_attributes (t1, t2);
421 /* Find the composite type of the target types, and combine the
422 qualifiers of the two types' targets. */
423 pointed_to_1 = TREE_TYPE (t1);
424 pointed_to_2 = TREE_TYPE (t2);
425 target = composite_type (TYPE_MAIN_VARIANT (pointed_to_1),
426 TYPE_MAIN_VARIANT (pointed_to_2));
427 t1 = build_pointer_type (c_build_qualified_type
429 TYPE_QUALS (pointed_to_1) |
430 TYPE_QUALS (pointed_to_2)));
431 return build_type_attribute_variant (t1, attributes);
434 /* Return the common type for two arithmetic types under the usual
435 arithmetic conversions. The default conversions have already been
436 applied, and enumerated types converted to their compatible integer
437 types. The resulting type is unqualified and has no attributes.
439 This is the type for the result of most arithmetic operations
440 if the operands have the given two types. */
443 common_type (tree t1, tree t2)
445 enum tree_code code1;
446 enum tree_code code2;
448 /* If one type is nonsense, use the other. */
449 if (t1 == error_mark_node)
451 if (t2 == error_mark_node)
454 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
455 t1 = TYPE_MAIN_VARIANT (t1);
457 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
458 t2 = TYPE_MAIN_VARIANT (t2);
460 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
461 t1 = build_type_attribute_variant (t1, NULL_TREE);
463 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
464 t2 = build_type_attribute_variant (t2, NULL_TREE);
466 /* Save time if the two types are the same. */
468 if (t1 == t2) return t1;
470 code1 = TREE_CODE (t1);
471 code2 = TREE_CODE (t2);
473 if (code1 != VECTOR_TYPE && code1 != COMPLEX_TYPE
474 && code1 != REAL_TYPE && code1 != INTEGER_TYPE)
477 if (code2 != VECTOR_TYPE && code2 != COMPLEX_TYPE
478 && code2 != REAL_TYPE && code2 != INTEGER_TYPE)
481 /* If one type is a vector type, return that type. (How the usual
482 arithmetic conversions apply to the vector types extension is not
483 precisely specified.) */
484 if (code1 == VECTOR_TYPE)
487 if (code2 == VECTOR_TYPE)
490 /* If one type is complex, form the common type of the non-complex
491 components, then make that complex. Use T1 or T2 if it is the
493 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
495 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
496 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
497 tree subtype = common_type (subtype1, subtype2);
499 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
501 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
504 return build_complex_type (subtype);
507 /* If only one is real, use it as the result. */
509 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
512 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
515 /* Both real or both integers; use the one with greater precision. */
517 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
519 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
522 /* Same precision. Prefer long longs to longs to ints when the
523 same precision, following the C99 rules on integer type rank
524 (which are equivalent to the C90 rules for C90 types). */
526 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
527 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
528 return long_long_unsigned_type_node;
530 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
531 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
533 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
534 return long_long_unsigned_type_node;
536 return long_long_integer_type_node;
539 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
540 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
541 return long_unsigned_type_node;
543 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
544 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
546 /* But preserve unsignedness from the other type,
547 since long cannot hold all the values of an unsigned int. */
548 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
549 return long_unsigned_type_node;
551 return long_integer_type_node;
554 /* Likewise, prefer long double to double even if same size. */
555 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
556 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
557 return long_double_type_node;
559 /* Otherwise prefer the unsigned one. */
561 if (TYPE_UNSIGNED (t1))
567 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
568 or various other operations. Return 2 if they are compatible
569 but a warning may be needed if you use them together. */
572 comptypes (tree type1, tree type2)
578 /* Suppress errors caused by previously reported errors. */
580 if (t1 == t2 || !t1 || !t2
581 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
584 /* If either type is the internal version of sizetype, return the
586 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
587 && TYPE_ORIG_SIZE_TYPE (t1))
588 t1 = TYPE_ORIG_SIZE_TYPE (t1);
590 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
591 && TYPE_ORIG_SIZE_TYPE (t2))
592 t2 = TYPE_ORIG_SIZE_TYPE (t2);
595 /* Enumerated types are compatible with integer types, but this is
596 not transitive: two enumerated types in the same translation unit
597 are compatible with each other only if they are the same type. */
599 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
600 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
601 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
602 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
607 /* Different classes of types can't be compatible. */
609 if (TREE_CODE (t1) != TREE_CODE (t2))
612 /* Qualifiers must match. C99 6.7.3p9 */
614 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
617 /* Allow for two different type nodes which have essentially the same
618 definition. Note that we already checked for equality of the type
619 qualifiers (just above). */
621 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
624 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
625 if (! (attrval = targetm.comp_type_attributes (t1, t2)))
628 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
631 switch (TREE_CODE (t1))
634 /* We must give ObjC the first crack at comparing pointers, since
635 protocol qualifiers may be involved. */
636 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
638 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
639 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
643 val = function_types_compatible_p (t1, t2);
648 tree d1 = TYPE_DOMAIN (t1);
649 tree d2 = TYPE_DOMAIN (t2);
650 bool d1_variable, d2_variable;
651 bool d1_zero, d2_zero;
654 /* Target types must match incl. qualifiers. */
655 if (TREE_TYPE (t1) != TREE_TYPE (t2)
656 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
659 /* Sizes must match unless one is missing or variable. */
660 if (d1 == 0 || d2 == 0 || d1 == d2)
663 d1_zero = ! TYPE_MAX_VALUE (d1);
664 d2_zero = ! TYPE_MAX_VALUE (d2);
666 d1_variable = (! d1_zero
667 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
668 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
669 d2_variable = (! d2_zero
670 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
671 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
673 if (d1_variable || d2_variable)
675 if (d1_zero && d2_zero)
677 if (d1_zero || d2_zero
678 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
679 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
686 /* We are dealing with two distinct structs. In assorted Objective-C
687 corner cases, however, these can still be deemed equivalent. */
688 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
693 if (val != 1 && !same_translation_unit_p (t1, t2))
694 val = tagged_types_tu_compatible_p (t1, t2);
698 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
699 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
705 return attrval == 2 && val == 1 ? 2 : val;
708 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
709 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
710 to 1 or 0 depending if the check of the pointer types is meant to
711 be reflexive or not (typically, assignments are not reflexive,
712 while comparisons are reflexive).
716 comp_target_types (tree ttl, tree ttr, int reflexive)
720 /* Give objc_comptypes a crack at letting these types through. */
721 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
724 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
725 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
727 if (val == 2 && pedantic)
728 pedwarn ("types are not quite compatible");
732 /* Subroutines of `comptypes'. */
734 /* Determine whether two trees derive from the same translation unit.
735 If the CONTEXT chain ends in a null, that tree's context is still
736 being parsed, so if two trees have context chains ending in null,
737 they're in the same translation unit. */
739 same_translation_unit_p (tree t1, tree t2)
741 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
742 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
744 case 'd': t1 = DECL_CONTEXT (t1); break;
745 case 't': t1 = TYPE_CONTEXT (t1); break;
746 case 'x': t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
750 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
751 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
753 case 'd': t2 = DECL_CONTEXT (t2); break;
754 case 't': t2 = TYPE_CONTEXT (t2); break;
755 case 'x': t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
762 /* The C standard says that two structures in different translation
763 units are compatible with each other only if the types of their
764 fields are compatible (among other things). So, consider two copies
765 of this structure: */
767 struct tagged_tu_seen {
768 const struct tagged_tu_seen * next;
773 /* Can they be compatible with each other? We choose to break the
774 recursion by allowing those types to be compatible. */
776 static const struct tagged_tu_seen * tagged_tu_seen_base;
778 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
779 compatible. If the two types are not the same (which has been
780 checked earlier), this can only happen when multiple translation
781 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
785 tagged_types_tu_compatible_p (tree t1, tree t2)
788 bool needs_warning = false;
790 /* We have to verify that the tags of the types are the same. This
791 is harder than it looks because this may be a typedef, so we have
792 to go look at the original type. It may even be a typedef of a
794 In the case of compiler-created builtin structs the TYPE_DECL
795 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
796 while (TYPE_NAME (t1)
797 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
798 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
799 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
801 while (TYPE_NAME (t2)
802 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
803 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
804 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
806 /* C90 didn't have the requirement that the two tags be the same. */
807 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
810 /* C90 didn't say what happened if one or both of the types were
811 incomplete; we choose to follow C99 rules here, which is that they
813 if (TYPE_SIZE (t1) == NULL
814 || TYPE_SIZE (t2) == NULL)
818 const struct tagged_tu_seen * tts_i;
819 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
820 if (tts_i->t1 == t1 && tts_i->t2 == t2)
824 switch (TREE_CODE (t1))
829 /* Speed up the case where the type values are in the same order. */
830 tree tv1 = TYPE_VALUES (t1);
831 tree tv2 = TYPE_VALUES (t2);
836 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
838 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
840 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
844 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
846 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
849 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
852 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
854 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
856 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
864 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
867 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
870 struct tagged_tu_seen tts;
872 tts.next = tagged_tu_seen_base;
875 tagged_tu_seen_base = &tts;
877 if (DECL_NAME (s1) != NULL)
878 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
879 if (DECL_NAME (s1) == DECL_NAME (s2))
882 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
886 needs_warning = true;
888 if (TREE_CODE (s1) == FIELD_DECL
889 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
890 DECL_FIELD_BIT_OFFSET (s2)) != 1)
896 tagged_tu_seen_base = tts.next;
900 return needs_warning ? 2 : 1;
905 struct tagged_tu_seen tts;
907 tts.next = tagged_tu_seen_base;
910 tagged_tu_seen_base = &tts;
912 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
914 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
917 if (TREE_CODE (s1) != TREE_CODE (s2)
918 || DECL_NAME (s1) != DECL_NAME (s2))
920 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
924 needs_warning = true;
926 if (TREE_CODE (s1) == FIELD_DECL
927 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
928 DECL_FIELD_BIT_OFFSET (s2)) != 1)
931 tagged_tu_seen_base = tts.next;
934 return needs_warning ? 2 : 1;
942 /* Return 1 if two function types F1 and F2 are compatible.
943 If either type specifies no argument types,
944 the other must specify a fixed number of self-promoting arg types.
945 Otherwise, if one type specifies only the number of arguments,
946 the other must specify that number of self-promoting arg types.
947 Otherwise, the argument types must match. */
950 function_types_compatible_p (tree f1, tree f2)
953 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
958 ret1 = TREE_TYPE (f1);
959 ret2 = TREE_TYPE (f2);
961 /* 'volatile' qualifiers on a function's return type mean the function
963 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
964 pedwarn ("function return types not compatible due to `volatile'");
965 if (TYPE_VOLATILE (ret1))
966 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
967 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
968 if (TYPE_VOLATILE (ret2))
969 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
970 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
971 val = comptypes (ret1, ret2);
975 args1 = TYPE_ARG_TYPES (f1);
976 args2 = TYPE_ARG_TYPES (f2);
978 /* An unspecified parmlist matches any specified parmlist
979 whose argument types don't need default promotions. */
983 if (!self_promoting_args_p (args2))
985 /* If one of these types comes from a non-prototype fn definition,
986 compare that with the other type's arglist.
987 If they don't match, ask for a warning (but no error). */
988 if (TYPE_ACTUAL_ARG_TYPES (f1)
989 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
995 if (!self_promoting_args_p (args1))
997 if (TYPE_ACTUAL_ARG_TYPES (f2)
998 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1003 /* Both types have argument lists: compare them and propagate results. */
1004 val1 = type_lists_compatible_p (args1, args2);
1005 return val1 != 1 ? val1 : val;
1008 /* Check two lists of types for compatibility,
1009 returning 0 for incompatible, 1 for compatible,
1010 or 2 for compatible with warning. */
1013 type_lists_compatible_p (tree args1, tree args2)
1015 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1021 if (args1 == 0 && args2 == 0)
1023 /* If one list is shorter than the other,
1024 they fail to match. */
1025 if (args1 == 0 || args2 == 0)
1027 /* A null pointer instead of a type
1028 means there is supposed to be an argument
1029 but nothing is specified about what type it has.
1030 So match anything that self-promotes. */
1031 if (TREE_VALUE (args1) == 0)
1033 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
1036 else if (TREE_VALUE (args2) == 0)
1038 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
1041 /* If one of the lists has an error marker, ignore this arg. */
1042 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
1043 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
1045 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
1046 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
1048 /* Allow wait (union {union wait *u; int *i} *)
1049 and wait (union wait *) to be compatible. */
1050 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
1051 && (TYPE_NAME (TREE_VALUE (args1)) == 0
1052 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
1053 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
1054 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
1055 TYPE_SIZE (TREE_VALUE (args2))))
1058 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
1059 memb; memb = TREE_CHAIN (memb))
1060 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
1065 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
1066 && (TYPE_NAME (TREE_VALUE (args2)) == 0
1067 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
1068 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
1069 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
1070 TYPE_SIZE (TREE_VALUE (args1))))
1073 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
1074 memb; memb = TREE_CHAIN (memb))
1075 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
1084 /* comptypes said ok, but record if it said to warn. */
1088 args1 = TREE_CHAIN (args1);
1089 args2 = TREE_CHAIN (args2);
1093 /* Compute the size to increment a pointer by. */
1096 c_size_in_bytes (tree type)
1098 enum tree_code code = TREE_CODE (type);
1100 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1101 return size_one_node;
1103 if (!COMPLETE_OR_VOID_TYPE_P (type))
1105 error ("arithmetic on pointer to an incomplete type");
1106 return size_one_node;
1109 /* Convert in case a char is more than one unit. */
1110 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1111 size_int (TYPE_PRECISION (char_type_node)
1115 /* Return either DECL or its known constant value (if it has one). */
1118 decl_constant_value (tree decl)
1120 if (/* Don't change a variable array bound or initial value to a constant
1121 in a place where a variable is invalid. Note that DECL_INITIAL
1122 isn't valid for a PARM_DECL. */
1123 current_function_decl != 0
1124 && TREE_CODE (decl) != PARM_DECL
1125 && ! TREE_THIS_VOLATILE (decl)
1126 && TREE_READONLY (decl)
1127 && DECL_INITIAL (decl) != 0
1128 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1129 /* This is invalid if initial value is not constant.
1130 If it has either a function call, a memory reference,
1131 or a variable, then re-evaluating it could give different results. */
1132 && TREE_CONSTANT (DECL_INITIAL (decl))
1133 /* Check for cases where this is sub-optimal, even though valid. */
1134 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1135 return DECL_INITIAL (decl);
1139 /* Return either DECL or its known constant value (if it has one), but
1140 return DECL if pedantic or DECL has mode BLKmode. This is for
1141 bug-compatibility with the old behavior of decl_constant_value
1142 (before GCC 3.0); every use of this function is a bug and it should
1143 be removed before GCC 3.1. It is not appropriate to use pedantic
1144 in a way that affects optimization, and BLKmode is probably not the
1145 right test for avoiding misoptimizations either. */
1148 decl_constant_value_for_broken_optimization (tree decl)
1150 if (pedantic || DECL_MODE (decl) == BLKmode)
1153 return decl_constant_value (decl);
1157 /* Perform the default conversion of arrays and functions to pointers.
1158 Return the result of converting EXP. For any other expression, just
1162 default_function_array_conversion (tree exp)
1165 tree type = TREE_TYPE (exp);
1166 enum tree_code code = TREE_CODE (type);
1169 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1172 Do not use STRIP_NOPS here! It will remove conversions from pointer
1173 to integer and cause infinite recursion. */
1175 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1176 || (TREE_CODE (exp) == NOP_EXPR
1177 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1179 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1181 exp = TREE_OPERAND (exp, 0);
1184 if (TREE_NO_WARNING (orig_exp))
1185 TREE_NO_WARNING (exp) = 1;
1187 if (code == FUNCTION_TYPE)
1189 return build_unary_op (ADDR_EXPR, exp, 0);
1191 if (code == ARRAY_TYPE)
1194 tree restype = TREE_TYPE (type);
1200 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1202 constp = TREE_READONLY (exp);
1203 volatilep = TREE_THIS_VOLATILE (exp);
1206 if (TYPE_QUALS (type) || constp || volatilep)
1208 = c_build_qualified_type (restype,
1210 | (constp * TYPE_QUAL_CONST)
1211 | (volatilep * TYPE_QUAL_VOLATILE));
1213 if (TREE_CODE (exp) == INDIRECT_REF)
1214 return convert (build_pointer_type (restype),
1215 TREE_OPERAND (exp, 0));
1217 if (TREE_CODE (exp) == COMPOUND_EXPR)
1219 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1220 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1221 TREE_OPERAND (exp, 0), op1);
1224 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1225 if (!flag_isoc99 && !lvalue_array_p)
1227 /* Before C99, non-lvalue arrays do not decay to pointers.
1228 Normally, using such an array would be invalid; but it can
1229 be used correctly inside sizeof or as a statement expression.
1230 Thus, do not give an error here; an error will result later. */
1234 ptrtype = build_pointer_type (restype);
1236 if (TREE_CODE (exp) == VAR_DECL)
1238 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1239 ADDR_EXPR because it's the best way of representing what
1240 happens in C when we take the address of an array and place
1241 it in a pointer to the element type. */
1242 adr = build1 (ADDR_EXPR, ptrtype, exp);
1243 if (!c_mark_addressable (exp))
1244 return error_mark_node;
1245 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1248 /* This way is better for a COMPONENT_REF since it can
1249 simplify the offset for a component. */
1250 adr = build_unary_op (ADDR_EXPR, exp, 1);
1251 return convert (ptrtype, adr);
1256 /* Perform default promotions for C data used in expressions.
1257 Arrays and functions are converted to pointers;
1258 enumeral types or short or char, to int.
1259 In addition, manifest constants symbols are replaced by their values. */
1262 default_conversion (tree exp)
1265 tree type = TREE_TYPE (exp);
1266 enum tree_code code = TREE_CODE (type);
1268 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1269 return default_function_array_conversion (exp);
1271 /* Constants can be used directly unless they're not loadable. */
1272 if (TREE_CODE (exp) == CONST_DECL)
1273 exp = DECL_INITIAL (exp);
1275 /* Replace a nonvolatile const static variable with its value unless
1276 it is an array, in which case we must be sure that taking the
1277 address of the array produces consistent results. */
1278 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1280 exp = decl_constant_value_for_broken_optimization (exp);
1281 type = TREE_TYPE (exp);
1284 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1287 Do not use STRIP_NOPS here! It will remove conversions from pointer
1288 to integer and cause infinite recursion. */
1290 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1291 || (TREE_CODE (exp) == NOP_EXPR
1292 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1293 exp = TREE_OPERAND (exp, 0);
1295 if (TREE_NO_WARNING (orig_exp))
1296 TREE_NO_WARNING (exp) = 1;
1298 /* Normally convert enums to int,
1299 but convert wide enums to something wider. */
1300 if (code == ENUMERAL_TYPE)
1302 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1303 TYPE_PRECISION (integer_type_node)),
1304 ((TYPE_PRECISION (type)
1305 >= TYPE_PRECISION (integer_type_node))
1306 && TYPE_UNSIGNED (type)));
1308 return convert (type, exp);
1311 if (TREE_CODE (exp) == COMPONENT_REF
1312 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1313 /* If it's thinner than an int, promote it like a
1314 c_promoting_integer_type_p, otherwise leave it alone. */
1315 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1316 TYPE_PRECISION (integer_type_node)))
1317 return convert (integer_type_node, exp);
1319 if (c_promoting_integer_type_p (type))
1321 /* Preserve unsignedness if not really getting any wider. */
1322 if (TYPE_UNSIGNED (type)
1323 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1324 return convert (unsigned_type_node, exp);
1326 return convert (integer_type_node, exp);
1329 if (code == VOID_TYPE)
1331 error ("void value not ignored as it ought to be");
1332 return error_mark_node;
1337 /* Look up COMPONENT in a structure or union DECL.
1339 If the component name is not found, returns NULL_TREE. Otherwise,
1340 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1341 stepping down the chain to the component, which is in the last
1342 TREE_VALUE of the list. Normally the list is of length one, but if
1343 the component is embedded within (nested) anonymous structures or
1344 unions, the list steps down the chain to the component. */
1347 lookup_field (tree decl, tree component)
1349 tree type = TREE_TYPE (decl);
1352 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1353 to the field elements. Use a binary search on this array to quickly
1354 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1355 will always be set for structures which have many elements. */
1357 if (TYPE_LANG_SPECIFIC (type))
1360 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1362 field = TYPE_FIELDS (type);
1364 top = TYPE_LANG_SPECIFIC (type)->s->len;
1365 while (top - bot > 1)
1367 half = (top - bot + 1) >> 1;
1368 field = field_array[bot+half];
1370 if (DECL_NAME (field) == NULL_TREE)
1372 /* Step through all anon unions in linear fashion. */
1373 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1375 field = field_array[bot++];
1376 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1377 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1379 tree anon = lookup_field (field, component);
1382 return tree_cons (NULL_TREE, field, anon);
1386 /* Entire record is only anon unions. */
1390 /* Restart the binary search, with new lower bound. */
1394 if (DECL_NAME (field) == component)
1396 if (DECL_NAME (field) < component)
1402 if (DECL_NAME (field_array[bot]) == component)
1403 field = field_array[bot];
1404 else if (DECL_NAME (field) != component)
1409 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1411 if (DECL_NAME (field) == NULL_TREE
1412 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1413 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1415 tree anon = lookup_field (field, component);
1418 return tree_cons (NULL_TREE, field, anon);
1421 if (DECL_NAME (field) == component)
1425 if (field == NULL_TREE)
1429 return tree_cons (NULL_TREE, field, NULL_TREE);
1432 /* Make an expression to refer to the COMPONENT field of
1433 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1436 build_component_ref (tree datum, tree component)
1438 tree type = TREE_TYPE (datum);
1439 enum tree_code code = TREE_CODE (type);
1443 if (!objc_is_public (datum, component))
1444 return error_mark_node;
1446 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1447 Ensure that the arguments are not lvalues; otherwise,
1448 if the component is an array, it would wrongly decay to a pointer in
1450 We cannot do this with a COND_EXPR, because in a conditional expression
1451 the default promotions are applied to both sides, and this would yield
1452 the wrong type of the result; for example, if the components have
1454 switch (TREE_CODE (datum))
1458 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1459 return build (COMPOUND_EXPR, TREE_TYPE (value),
1460 TREE_OPERAND (datum, 0), non_lvalue (value));
1466 /* See if there is a field or component with name COMPONENT. */
1468 if (code == RECORD_TYPE || code == UNION_TYPE)
1470 if (!COMPLETE_TYPE_P (type))
1472 c_incomplete_type_error (NULL_TREE, type);
1473 return error_mark_node;
1476 field = lookup_field (datum, component);
1480 error ("%s has no member named `%s'",
1481 code == RECORD_TYPE ? "structure" : "union",
1482 IDENTIFIER_POINTER (component));
1483 return error_mark_node;
1486 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1487 This might be better solved in future the way the C++ front
1488 end does it - by giving the anonymous entities each a
1489 separate name and type, and then have build_component_ref
1490 recursively call itself. We can't do that here. */
1493 tree subdatum = TREE_VALUE (field);
1495 if (TREE_TYPE (subdatum) == error_mark_node)
1496 return error_mark_node;
1498 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1500 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1501 TREE_READONLY (ref) = 1;
1502 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1503 TREE_THIS_VOLATILE (ref) = 1;
1505 if (TREE_DEPRECATED (subdatum))
1506 warn_deprecated_use (subdatum);
1510 field = TREE_CHAIN (field);
1516 else if (code != ERROR_MARK)
1517 error ("request for member `%s' in something not a structure or union",
1518 IDENTIFIER_POINTER (component));
1520 return error_mark_node;
1523 /* Given an expression PTR for a pointer, return an expression
1524 for the value pointed to.
1525 ERRORSTRING is the name of the operator to appear in error messages. */
1528 build_indirect_ref (tree ptr, const char *errorstring)
1530 tree pointer = default_conversion (ptr);
1531 tree type = TREE_TYPE (pointer);
1533 if (TREE_CODE (type) == POINTER_TYPE)
1535 if (TREE_CODE (pointer) == ADDR_EXPR
1536 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1537 == TREE_TYPE (type)))
1538 return TREE_OPERAND (pointer, 0);
1541 tree t = TREE_TYPE (type);
1542 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1544 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1546 error ("dereferencing pointer to incomplete type");
1547 return error_mark_node;
1549 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1550 warning ("dereferencing `void *' pointer");
1552 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1553 so that we get the proper error message if the result is used
1554 to assign to. Also, &* is supposed to be a no-op.
1555 And ANSI C seems to specify that the type of the result
1556 should be the const type. */
1557 /* A de-reference of a pointer to const is not a const. It is valid
1558 to change it via some other pointer. */
1559 TREE_READONLY (ref) = TYPE_READONLY (t);
1560 TREE_SIDE_EFFECTS (ref)
1561 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1562 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1566 else if (TREE_CODE (pointer) != ERROR_MARK)
1567 error ("invalid type argument of `%s'", errorstring);
1568 return error_mark_node;
1571 /* This handles expressions of the form "a[i]", which denotes
1574 This is logically equivalent in C to *(a+i), but we may do it differently.
1575 If A is a variable or a member, we generate a primitive ARRAY_REF.
1576 This avoids forcing the array out of registers, and can work on
1577 arrays that are not lvalues (for example, members of structures returned
1581 build_array_ref (tree array, tree index)
1585 error ("subscript missing in array reference");
1586 return error_mark_node;
1589 if (TREE_TYPE (array) == error_mark_node
1590 || TREE_TYPE (index) == error_mark_node)
1591 return error_mark_node;
1593 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1597 /* Subscripting with type char is likely to lose
1598 on a machine where chars are signed.
1599 So warn on any machine, but optionally.
1600 Don't warn for unsigned char since that type is safe.
1601 Don't warn for signed char because anyone who uses that
1602 must have done so deliberately. */
1603 if (warn_char_subscripts
1604 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1605 warning ("array subscript has type `char'");
1607 /* Apply default promotions *after* noticing character types. */
1608 index = default_conversion (index);
1610 /* Require integer *after* promotion, for sake of enums. */
1611 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1613 error ("array subscript is not an integer");
1614 return error_mark_node;
1617 /* An array that is indexed by a non-constant
1618 cannot be stored in a register; we must be able to do
1619 address arithmetic on its address.
1620 Likewise an array of elements of variable size. */
1621 if (TREE_CODE (index) != INTEGER_CST
1622 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1623 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1625 if (!c_mark_addressable (array))
1626 return error_mark_node;
1628 /* An array that is indexed by a constant value which is not within
1629 the array bounds cannot be stored in a register either; because we
1630 would get a crash in store_bit_field/extract_bit_field when trying
1631 to access a non-existent part of the register. */
1632 if (TREE_CODE (index) == INTEGER_CST
1633 && TYPE_DOMAIN (TREE_TYPE (array))
1634 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1636 if (!c_mark_addressable (array))
1637 return error_mark_node;
1643 while (TREE_CODE (foo) == COMPONENT_REF)
1644 foo = TREE_OPERAND (foo, 0);
1645 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1646 pedwarn ("ISO C forbids subscripting `register' array");
1647 else if (! flag_isoc99 && ! lvalue_p (foo))
1648 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1651 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1652 rval = build (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1653 /* Array ref is const/volatile if the array elements are
1654 or if the array is. */
1655 TREE_READONLY (rval)
1656 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1657 | TREE_READONLY (array));
1658 TREE_SIDE_EFFECTS (rval)
1659 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1660 | TREE_SIDE_EFFECTS (array));
1661 TREE_THIS_VOLATILE (rval)
1662 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1663 /* This was added by rms on 16 Nov 91.
1664 It fixes vol struct foo *a; a->elts[1]
1665 in an inline function.
1666 Hope it doesn't break something else. */
1667 | TREE_THIS_VOLATILE (array));
1668 return require_complete_type (fold (rval));
1672 tree ar = default_conversion (array);
1673 tree ind = default_conversion (index);
1675 /* Do the same warning check as above, but only on the part that's
1676 syntactically the index and only if it is also semantically
1678 if (warn_char_subscripts
1679 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1680 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1681 warning ("subscript has type `char'");
1683 /* Put the integer in IND to simplify error checking. */
1684 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1691 if (ar == error_mark_node)
1694 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1695 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1697 error ("subscripted value is neither array nor pointer");
1698 return error_mark_node;
1700 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1702 error ("array subscript is not an integer");
1703 return error_mark_node;
1706 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1711 /* Build an external reference to identifier ID. FUN indicates
1712 whether this will be used for a function call. */
1714 build_external_ref (tree id, int fun)
1717 tree decl = lookup_name (id);
1718 tree objc_ivar = lookup_objc_ivar (id);
1720 if (decl && decl != error_mark_node)
1722 /* Properly declared variable or function reference. */
1725 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1727 warning ("local declaration of `%s' hides instance variable",
1728 IDENTIFIER_POINTER (id));
1737 /* Implicit function declaration. */
1738 ref = implicitly_declare (id);
1739 else if (decl == error_mark_node)
1740 /* Don't complain about something that's already been
1741 complained about. */
1742 return error_mark_node;
1745 undeclared_variable (id);
1746 return error_mark_node;
1749 if (TREE_TYPE (ref) == error_mark_node)
1750 return error_mark_node;
1752 if (TREE_DEPRECATED (ref))
1753 warn_deprecated_use (ref);
1755 if (!skip_evaluation)
1756 assemble_external (ref);
1757 TREE_USED (ref) = 1;
1759 if (TREE_CODE (ref) == CONST_DECL)
1761 ref = DECL_INITIAL (ref);
1762 TREE_CONSTANT (ref) = 1;
1763 TREE_INVARIANT (ref) = 1;
1765 else if (current_function_decl != 0
1766 && !DECL_FILE_SCOPE_P (current_function_decl)
1767 && (TREE_CODE (ref) == VAR_DECL
1768 || TREE_CODE (ref) == PARM_DECL
1769 || TREE_CODE (ref) == FUNCTION_DECL))
1771 tree context = decl_function_context (ref);
1773 if (context != 0 && context != current_function_decl)
1774 DECL_NONLOCAL (ref) = 1;
1780 /* Build a function call to function FUNCTION with parameters PARAMS.
1781 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1782 TREE_VALUE of each node is a parameter-expression.
1783 FUNCTION's data type may be a function type or a pointer-to-function. */
1786 build_function_call (tree function, tree params)
1788 tree fntype, fundecl = 0;
1789 tree coerced_params;
1790 tree name = NULL_TREE, result;
1793 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1794 STRIP_TYPE_NOPS (function);
1796 /* Convert anything with function type to a pointer-to-function. */
1797 if (TREE_CODE (function) == FUNCTION_DECL)
1799 name = DECL_NAME (function);
1801 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1802 (because calling an inline function does not mean the function
1803 needs to be separately compiled). */
1804 fntype = build_type_variant (TREE_TYPE (function),
1805 TREE_READONLY (function),
1806 TREE_THIS_VOLATILE (function));
1808 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1811 function = default_conversion (function);
1813 fntype = TREE_TYPE (function);
1815 if (TREE_CODE (fntype) == ERROR_MARK)
1816 return error_mark_node;
1818 if (!(TREE_CODE (fntype) == POINTER_TYPE
1819 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1821 error ("called object is not a function");
1822 return error_mark_node;
1825 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1826 current_function_returns_abnormally = 1;
1828 /* fntype now gets the type of function pointed to. */
1829 fntype = TREE_TYPE (fntype);
1831 /* Check that the function is called through a compatible prototype.
1832 If it is not, replace the call by a trap, wrapped up in a compound
1833 expression if necessary. This has the nice side-effect to prevent
1834 the tree-inliner from generating invalid assignment trees which may
1835 blow up in the RTL expander later.
1837 ??? This doesn't work for Objective-C because objc_comptypes
1838 refuses to compare function prototypes, yet the compiler appears
1839 to build calls that are flagged as invalid by C's comptypes. */
1840 if (! c_dialect_objc ()
1841 && TREE_CODE (function) == NOP_EXPR
1842 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1843 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1844 && ! comptypes (fntype, TREE_TYPE (tem)))
1846 tree return_type = TREE_TYPE (fntype);
1847 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1850 /* This situation leads to run-time undefined behavior. We can't,
1851 therefore, simply error unless we can prove that all possible
1852 executions of the program must execute the code. */
1853 warning ("function called through a non-compatible type");
1855 /* We can, however, treat "undefined" any way we please.
1856 Call abort to encourage the user to fix the program. */
1857 inform ("if this code is reached, the program will abort");
1859 if (VOID_TYPE_P (return_type))
1865 if (AGGREGATE_TYPE_P (return_type))
1866 rhs = build_compound_literal (return_type,
1867 build_constructor (return_type,
1870 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1872 return build (COMPOUND_EXPR, return_type, trap, rhs);
1876 /* Convert the parameters to the types declared in the
1877 function prototype, or apply default promotions. */
1880 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1882 /* Check that the arguments to the function are valid. */
1884 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1886 result = build (CALL_EXPR, TREE_TYPE (fntype),
1887 function, coerced_params, NULL_TREE);
1888 TREE_SIDE_EFFECTS (result) = 1;
1890 if (require_constant_value)
1892 result = fold_initializer (result);
1894 if (TREE_CONSTANT (result)
1895 && (name == NULL_TREE
1896 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
1897 pedwarn_init ("initializer element is not constant");
1900 result = fold (result);
1902 if (VOID_TYPE_P (TREE_TYPE (result)))
1904 return require_complete_type (result);
1907 /* Convert the argument expressions in the list VALUES
1908 to the types in the list TYPELIST. The result is a list of converted
1909 argument expressions.
1911 If TYPELIST is exhausted, or when an element has NULL as its type,
1912 perform the default conversions.
1914 PARMLIST is the chain of parm decls for the function being called.
1915 It may be 0, if that info is not available.
1916 It is used only for generating error messages.
1918 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1920 This is also where warnings about wrong number of args are generated.
1922 Both VALUES and the returned value are chains of TREE_LIST nodes
1923 with the elements of the list in the TREE_VALUE slots of those nodes. */
1926 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1928 tree typetail, valtail;
1932 /* Scan the given expressions and types, producing individual
1933 converted arguments and pushing them on RESULT in reverse order. */
1935 for (valtail = values, typetail = typelist, parmnum = 0;
1937 valtail = TREE_CHAIN (valtail), parmnum++)
1939 tree type = typetail ? TREE_VALUE (typetail) : 0;
1940 tree val = TREE_VALUE (valtail);
1942 if (type == void_type_node)
1945 error ("too many arguments to function `%s'",
1946 IDENTIFIER_POINTER (name));
1948 error ("too many arguments to function");
1952 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1953 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1954 to convert automatically to a pointer. */
1955 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1956 val = TREE_OPERAND (val, 0);
1958 val = default_function_array_conversion (val);
1960 val = require_complete_type (val);
1964 /* Formal parm type is specified by a function prototype. */
1967 if (!COMPLETE_TYPE_P (type))
1969 error ("type of formal parameter %d is incomplete", parmnum + 1);
1974 /* Optionally warn about conversions that
1975 differ from the default conversions. */
1976 if (warn_conversion || warn_traditional)
1978 int formal_prec = TYPE_PRECISION (type);
1980 if (INTEGRAL_TYPE_P (type)
1981 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1982 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1983 if (INTEGRAL_TYPE_P (type)
1984 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1985 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1986 else if (TREE_CODE (type) == COMPLEX_TYPE
1987 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1988 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1989 else if (TREE_CODE (type) == REAL_TYPE
1990 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1991 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1992 else if (TREE_CODE (type) == COMPLEX_TYPE
1993 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1994 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1995 else if (TREE_CODE (type) == REAL_TYPE
1996 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1997 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1998 /* ??? At some point, messages should be written about
1999 conversions between complex types, but that's too messy
2001 else if (TREE_CODE (type) == REAL_TYPE
2002 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2004 /* Warn if any argument is passed as `float',
2005 since without a prototype it would be `double'. */
2006 if (formal_prec == TYPE_PRECISION (float_type_node))
2007 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
2009 /* Detect integer changing in width or signedness.
2010 These warnings are only activated with
2011 -Wconversion, not with -Wtraditional. */
2012 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2013 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2015 tree would_have_been = default_conversion (val);
2016 tree type1 = TREE_TYPE (would_have_been);
2018 if (TREE_CODE (type) == ENUMERAL_TYPE
2019 && (TYPE_MAIN_VARIANT (type)
2020 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2021 /* No warning if function asks for enum
2022 and the actual arg is that enum type. */
2024 else if (formal_prec != TYPE_PRECISION (type1))
2025 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
2026 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2028 /* Don't complain if the formal parameter type
2029 is an enum, because we can't tell now whether
2030 the value was an enum--even the same enum. */
2031 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2033 else if (TREE_CODE (val) == INTEGER_CST
2034 && int_fits_type_p (val, type))
2035 /* Change in signedness doesn't matter
2036 if a constant value is unaffected. */
2038 /* Likewise for a constant in a NOP_EXPR. */
2039 else if (TREE_CODE (val) == NOP_EXPR
2040 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2041 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2043 /* If the value is extended from a narrower
2044 unsigned type, it doesn't matter whether we
2045 pass it as signed or unsigned; the value
2046 certainly is the same either way. */
2047 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2048 && TYPE_UNSIGNED (TREE_TYPE (val)))
2050 else if (TYPE_UNSIGNED (type))
2051 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
2053 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
2057 parmval = convert_for_assignment (type, val,
2058 (char *) 0, /* arg passing */
2059 fundecl, name, parmnum + 1);
2061 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2062 && INTEGRAL_TYPE_P (type)
2063 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2064 parmval = default_conversion (parmval);
2066 result = tree_cons (NULL_TREE, parmval, result);
2068 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2069 && (TYPE_PRECISION (TREE_TYPE (val))
2070 < TYPE_PRECISION (double_type_node)))
2071 /* Convert `float' to `double'. */
2072 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2074 /* Convert `short' and `char' to full-size `int'. */
2075 result = tree_cons (NULL_TREE, default_conversion (val), result);
2078 typetail = TREE_CHAIN (typetail);
2081 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2084 error ("too few arguments to function `%s'",
2085 IDENTIFIER_POINTER (name));
2087 error ("too few arguments to function");
2090 return nreverse (result);
2093 /* This is the entry point used by the parser
2094 for binary operators in the input.
2095 In addition to constructing the expression,
2096 we check for operands that were written with other binary operators
2097 in a way that is likely to confuse the user. */
2100 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2103 struct c_expr result;
2105 enum tree_code code1 = arg1.original_code;
2106 enum tree_code code2 = arg2.original_code;
2108 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2109 result.original_code = code;
2111 if (TREE_CODE (result.value) == ERROR_MARK)
2114 /* Check for cases such as x+y<<z which users are likely
2116 if (warn_parentheses)
2118 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2120 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2121 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2122 warning ("suggest parentheses around + or - inside shift");
2125 if (code == TRUTH_ORIF_EXPR)
2127 if (code1 == TRUTH_ANDIF_EXPR
2128 || code2 == TRUTH_ANDIF_EXPR)
2129 warning ("suggest parentheses around && within ||");
2132 if (code == BIT_IOR_EXPR)
2134 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2135 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2136 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2137 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2138 warning ("suggest parentheses around arithmetic in operand of |");
2139 /* Check cases like x|y==z */
2140 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2141 warning ("suggest parentheses around comparison in operand of |");
2144 if (code == BIT_XOR_EXPR)
2146 if (code1 == BIT_AND_EXPR
2147 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2148 || code2 == BIT_AND_EXPR
2149 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2150 warning ("suggest parentheses around arithmetic in operand of ^");
2151 /* Check cases like x^y==z */
2152 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2153 warning ("suggest parentheses around comparison in operand of ^");
2156 if (code == BIT_AND_EXPR)
2158 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2159 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2160 warning ("suggest parentheses around + or - in operand of &");
2161 /* Check cases like x&y==z */
2162 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2163 warning ("suggest parentheses around comparison in operand of &");
2165 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2166 if (TREE_CODE_CLASS (code) == '<'
2167 && (TREE_CODE_CLASS (code1) == '<'
2168 || TREE_CODE_CLASS (code2) == '<'))
2169 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2173 unsigned_conversion_warning (result.value, arg1.value);
2174 unsigned_conversion_warning (result.value, arg2.value);
2175 overflow_warning (result.value);
2180 /* Return a tree for the difference of pointers OP0 and OP1.
2181 The resulting tree has type int. */
2184 pointer_diff (tree op0, tree op1)
2186 tree restype = ptrdiff_type_node;
2188 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2189 tree con0, con1, lit0, lit1;
2190 tree orig_op1 = op1;
2192 if (pedantic || warn_pointer_arith)
2194 if (TREE_CODE (target_type) == VOID_TYPE)
2195 pedwarn ("pointer of type `void *' used in subtraction");
2196 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2197 pedwarn ("pointer to a function used in subtraction");
2200 /* If the conversion to ptrdiff_type does anything like widening or
2201 converting a partial to an integral mode, we get a convert_expression
2202 that is in the way to do any simplifications.
2203 (fold-const.c doesn't know that the extra bits won't be needed.
2204 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2205 different mode in place.)
2206 So first try to find a common term here 'by hand'; we want to cover
2207 at least the cases that occur in legal static initializers. */
2208 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2209 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2211 if (TREE_CODE (con0) == PLUS_EXPR)
2213 lit0 = TREE_OPERAND (con0, 1);
2214 con0 = TREE_OPERAND (con0, 0);
2217 lit0 = integer_zero_node;
2219 if (TREE_CODE (con1) == PLUS_EXPR)
2221 lit1 = TREE_OPERAND (con1, 1);
2222 con1 = TREE_OPERAND (con1, 0);
2225 lit1 = integer_zero_node;
2227 if (operand_equal_p (con0, con1, 0))
2234 /* First do the subtraction as integers;
2235 then drop through to build the divide operator.
2236 Do not do default conversions on the minus operator
2237 in case restype is a short type. */
2239 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2240 convert (restype, op1), 0);
2241 /* This generates an error if op1 is pointer to incomplete type. */
2242 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2243 error ("arithmetic on pointer to an incomplete type");
2245 /* This generates an error if op0 is pointer to incomplete type. */
2246 op1 = c_size_in_bytes (target_type);
2248 /* Divide by the size, in easiest possible way. */
2249 return fold (build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2252 /* Construct and perhaps optimize a tree representation
2253 for a unary operation. CODE, a tree_code, specifies the operation
2254 and XARG is the operand.
2255 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2256 the default promotions (such as from short to int).
2257 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2258 allows non-lvalues; this is only used to handle conversion of non-lvalue
2259 arrays to pointers in C99. */
2262 build_unary_op (enum tree_code code, tree xarg, int flag)
2264 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2267 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2269 int noconvert = flag;
2271 if (typecode == ERROR_MARK)
2272 return error_mark_node;
2273 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2274 typecode = INTEGER_TYPE;
2279 /* This is used for unary plus, because a CONVERT_EXPR
2280 is enough to prevent anybody from looking inside for
2281 associativity, but won't generate any code. */
2282 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2283 || typecode == COMPLEX_TYPE
2284 || typecode == VECTOR_TYPE))
2286 error ("wrong type argument to unary plus");
2287 return error_mark_node;
2289 else if (!noconvert)
2290 arg = default_conversion (arg);
2291 arg = non_lvalue (arg);
2295 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2296 || typecode == COMPLEX_TYPE
2297 || typecode == VECTOR_TYPE))
2299 error ("wrong type argument to unary minus");
2300 return error_mark_node;
2302 else if (!noconvert)
2303 arg = default_conversion (arg);
2307 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2310 arg = default_conversion (arg);
2312 else if (typecode == COMPLEX_TYPE)
2316 pedwarn ("ISO C does not support `~' for complex conjugation");
2318 arg = default_conversion (arg);
2322 error ("wrong type argument to bit-complement");
2323 return error_mark_node;
2328 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2330 error ("wrong type argument to abs");
2331 return error_mark_node;
2333 else if (!noconvert)
2334 arg = default_conversion (arg);
2338 /* Conjugating a real value is a no-op, but allow it anyway. */
2339 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2340 || typecode == COMPLEX_TYPE))
2342 error ("wrong type argument to conjugation");
2343 return error_mark_node;
2345 else if (!noconvert)
2346 arg = default_conversion (arg);
2349 case TRUTH_NOT_EXPR:
2350 if (typecode != INTEGER_TYPE
2351 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2352 && typecode != COMPLEX_TYPE
2353 /* These will convert to a pointer. */
2354 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2356 error ("wrong type argument to unary exclamation mark");
2357 return error_mark_node;
2359 arg = lang_hooks.truthvalue_conversion (arg);
2360 return invert_truthvalue (arg);
2366 if (TREE_CODE (arg) == COMPLEX_CST)
2367 return TREE_REALPART (arg);
2368 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2369 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2374 if (TREE_CODE (arg) == COMPLEX_CST)
2375 return TREE_IMAGPART (arg);
2376 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2377 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2379 return convert (TREE_TYPE (arg), integer_zero_node);
2381 case PREINCREMENT_EXPR:
2382 case POSTINCREMENT_EXPR:
2383 case PREDECREMENT_EXPR:
2384 case POSTDECREMENT_EXPR:
2386 /* Increment or decrement the real part of the value,
2387 and don't change the imaginary part. */
2388 if (typecode == COMPLEX_TYPE)
2393 pedwarn ("ISO C does not support `++' and `--' on complex types");
2395 arg = stabilize_reference (arg);
2396 real = build_unary_op (REALPART_EXPR, arg, 1);
2397 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2398 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2399 build_unary_op (code, real, 1), imag);
2402 /* Report invalid types. */
2404 if (typecode != POINTER_TYPE
2405 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2407 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2408 error ("wrong type argument to increment");
2410 error ("wrong type argument to decrement");
2412 return error_mark_node;
2417 tree result_type = TREE_TYPE (arg);
2419 arg = get_unwidened (arg, 0);
2420 argtype = TREE_TYPE (arg);
2422 /* Compute the increment. */
2424 if (typecode == POINTER_TYPE)
2426 /* If pointer target is an undefined struct,
2427 we just cannot know how to do the arithmetic. */
2428 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2430 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2431 error ("increment of pointer to unknown structure");
2433 error ("decrement of pointer to unknown structure");
2435 else if ((pedantic || warn_pointer_arith)
2436 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2437 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2439 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2440 pedwarn ("wrong type argument to increment");
2442 pedwarn ("wrong type argument to decrement");
2445 inc = c_size_in_bytes (TREE_TYPE (result_type));
2448 inc = integer_one_node;
2450 inc = convert (argtype, inc);
2452 /* Complain about anything else that is not a true lvalue. */
2453 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2454 || code == POSTINCREMENT_EXPR)
2455 ? "invalid lvalue in increment"
2456 : "invalid lvalue in decrement")))
2457 return error_mark_node;
2459 /* Report a read-only lvalue. */
2460 if (TREE_READONLY (arg))
2461 readonly_error (arg,
2462 ((code == PREINCREMENT_EXPR
2463 || code == POSTINCREMENT_EXPR)
2464 ? "increment" : "decrement"));
2466 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2467 val = boolean_increment (code, arg);
2469 val = build (code, TREE_TYPE (arg), arg, inc);
2470 TREE_SIDE_EFFECTS (val) = 1;
2471 val = convert (result_type, val);
2472 if (TREE_CODE (val) != code)
2473 TREE_NO_WARNING (val) = 1;
2478 /* Note that this operation never does default_conversion. */
2480 /* Let &* cancel out to simplify resulting code. */
2481 if (TREE_CODE (arg) == INDIRECT_REF)
2483 /* Don't let this be an lvalue. */
2484 if (lvalue_p (TREE_OPERAND (arg, 0)))
2485 return non_lvalue (TREE_OPERAND (arg, 0));
2486 return TREE_OPERAND (arg, 0);
2489 /* For &x[y], return x+y */
2490 if (TREE_CODE (arg) == ARRAY_REF)
2492 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2493 return error_mark_node;
2494 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2495 TREE_OPERAND (arg, 1), 1);
2498 /* Anything not already handled and not a true memory reference
2499 or a non-lvalue array is an error. */
2500 else if (typecode != FUNCTION_TYPE && !flag
2501 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2502 return error_mark_node;
2504 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2505 argtype = TREE_TYPE (arg);
2507 /* If the lvalue is const or volatile, merge that into the type
2508 to which the address will point. Note that you can't get a
2509 restricted pointer by taking the address of something, so we
2510 only have to deal with `const' and `volatile' here. */
2511 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2512 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2513 argtype = c_build_type_variant (argtype,
2514 TREE_READONLY (arg),
2515 TREE_THIS_VOLATILE (arg));
2517 argtype = build_pointer_type (argtype);
2519 if (!c_mark_addressable (arg))
2520 return error_mark_node;
2525 if (TREE_CODE (arg) == COMPONENT_REF)
2527 tree field = TREE_OPERAND (arg, 1);
2529 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2531 if (DECL_C_BIT_FIELD (field))
2533 error ("attempt to take address of bit-field structure member `%s'",
2534 IDENTIFIER_POINTER (DECL_NAME (field)));
2535 return error_mark_node;
2538 addr = fold (build (PLUS_EXPR, argtype,
2539 convert (argtype, addr),
2540 convert (argtype, byte_position (field))));
2543 addr = build1 (code, argtype, arg);
2545 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2546 TREE_INVARIANT (addr) = TREE_CONSTANT (addr) = 1;
2556 argtype = TREE_TYPE (arg);
2557 val = build1 (code, argtype, arg);
2558 return require_constant_value ? fold_initializer (val) : fold (val);
2561 /* Return nonzero if REF is an lvalue valid for this language.
2562 Lvalues can be assigned, unless their type has TYPE_READONLY.
2563 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2568 enum tree_code code = TREE_CODE (ref);
2575 return lvalue_p (TREE_OPERAND (ref, 0));
2577 case COMPOUND_LITERAL_EXPR:
2587 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2588 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2591 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2598 /* Return nonzero if REF is an lvalue valid for this language;
2599 otherwise, print an error message and return zero. */
2602 lvalue_or_else (tree ref, const char *msgid)
2604 int win = lvalue_p (ref);
2607 error ("%s", msgid);
2613 /* Warn about storing in something that is `const'. */
2616 readonly_error (tree arg, const char *msgid)
2618 if (TREE_CODE (arg) == COMPONENT_REF)
2620 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2621 readonly_error (TREE_OPERAND (arg, 0), msgid);
2623 error ("%s of read-only member `%s'", _(msgid),
2624 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2626 else if (TREE_CODE (arg) == VAR_DECL)
2627 error ("%s of read-only variable `%s'", _(msgid),
2628 IDENTIFIER_POINTER (DECL_NAME (arg)));
2630 error ("%s of read-only location", _(msgid));
2633 /* Mark EXP saying that we need to be able to take the
2634 address of it; it should not be allocated in a register.
2635 Returns true if successful. */
2638 c_mark_addressable (tree exp)
2643 switch (TREE_CODE (x))
2646 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2648 error ("cannot take address of bit-field `%s'",
2649 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2653 /* ... fall through ... */
2659 x = TREE_OPERAND (x, 0);
2662 case COMPOUND_LITERAL_EXPR:
2664 TREE_ADDRESSABLE (x) = 1;
2671 if (C_DECL_REGISTER (x)
2672 && DECL_NONLOCAL (x))
2674 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2676 error ("global register variable `%s' used in nested function",
2677 IDENTIFIER_POINTER (DECL_NAME (x)));
2680 pedwarn ("register variable `%s' used in nested function",
2681 IDENTIFIER_POINTER (DECL_NAME (x)));
2683 else if (C_DECL_REGISTER (x))
2685 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2687 error ("address of global register variable `%s' requested",
2688 IDENTIFIER_POINTER (DECL_NAME (x)));
2692 pedwarn ("address of register variable `%s' requested",
2693 IDENTIFIER_POINTER (DECL_NAME (x)));
2698 TREE_ADDRESSABLE (x) = 1;
2705 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2708 build_conditional_expr (tree ifexp, tree op1, tree op2)
2712 enum tree_code code1;
2713 enum tree_code code2;
2714 tree result_type = NULL;
2715 tree orig_op1 = op1, orig_op2 = op2;
2717 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2719 /* Promote both alternatives. */
2721 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2722 op1 = default_conversion (op1);
2723 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2724 op2 = default_conversion (op2);
2726 if (TREE_CODE (ifexp) == ERROR_MARK
2727 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2728 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2729 return error_mark_node;
2731 type1 = TREE_TYPE (op1);
2732 code1 = TREE_CODE (type1);
2733 type2 = TREE_TYPE (op2);
2734 code2 = TREE_CODE (type2);
2736 /* C90 does not permit non-lvalue arrays in conditional expressions.
2737 In C99 they will be pointers by now. */
2738 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2740 error ("non-lvalue array in conditional expression");
2741 return error_mark_node;
2744 /* Quickly detect the usual case where op1 and op2 have the same type
2746 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2749 result_type = type1;
2751 result_type = TYPE_MAIN_VARIANT (type1);
2753 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2754 || code1 == COMPLEX_TYPE)
2755 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2756 || code2 == COMPLEX_TYPE))
2758 result_type = common_type (type1, type2);
2760 /* If -Wsign-compare, warn here if type1 and type2 have
2761 different signedness. We'll promote the signed to unsigned
2762 and later code won't know it used to be different.
2763 Do this check on the original types, so that explicit casts
2764 will be considered, but default promotions won't. */
2765 if (warn_sign_compare && !skip_evaluation)
2767 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2768 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2770 if (unsigned_op1 ^ unsigned_op2)
2772 /* Do not warn if the result type is signed, since the
2773 signed type will only be chosen if it can represent
2774 all the values of the unsigned type. */
2775 if (! TYPE_UNSIGNED (result_type))
2777 /* Do not warn if the signed quantity is an unsuffixed
2778 integer literal (or some static constant expression
2779 involving such literals) and it is non-negative. */
2780 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2781 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
2784 warning ("signed and unsigned type in conditional expression");
2788 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2790 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2791 pedwarn ("ISO C forbids conditional expr with only one void side");
2792 result_type = void_type_node;
2794 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2796 if (comp_target_types (type1, type2, 1))
2797 result_type = common_pointer_type (type1, type2);
2798 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2799 && TREE_CODE (orig_op1) != NOP_EXPR)
2800 result_type = qualify_type (type2, type1);
2801 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2802 && TREE_CODE (orig_op2) != NOP_EXPR)
2803 result_type = qualify_type (type1, type2);
2804 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2806 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2807 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2808 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2809 TREE_TYPE (type2)));
2811 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2813 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2814 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2815 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2816 TREE_TYPE (type1)));
2820 pedwarn ("pointer type mismatch in conditional expression");
2821 result_type = build_pointer_type (void_type_node);
2824 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2826 if (! integer_zerop (op2))
2827 pedwarn ("pointer/integer type mismatch in conditional expression");
2830 op2 = null_pointer_node;
2832 result_type = type1;
2834 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2836 if (!integer_zerop (op1))
2837 pedwarn ("pointer/integer type mismatch in conditional expression");
2840 op1 = null_pointer_node;
2842 result_type = type2;
2847 if (flag_cond_mismatch)
2848 result_type = void_type_node;
2851 error ("type mismatch in conditional expression");
2852 return error_mark_node;
2856 /* Merge const and volatile flags of the incoming types. */
2858 = build_type_variant (result_type,
2859 TREE_READONLY (op1) || TREE_READONLY (op2),
2860 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2862 if (result_type != TREE_TYPE (op1))
2863 op1 = convert_and_check (result_type, op1);
2864 if (result_type != TREE_TYPE (op2))
2865 op2 = convert_and_check (result_type, op2);
2867 if (TREE_CODE (ifexp) == INTEGER_CST)
2868 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2870 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2873 /* Return a compound expression that performs two expressions and
2874 returns the value of the second of them. */
2877 build_compound_expr (tree expr1, tree expr2)
2879 /* Convert arrays and functions to pointers. */
2880 expr2 = default_function_array_conversion (expr2);
2882 /* Don't let (0, 0) be null pointer constant. */
2883 if (integer_zerop (expr2))
2884 expr2 = non_lvalue (expr2);
2886 if (! TREE_SIDE_EFFECTS (expr1))
2888 /* The left-hand operand of a comma expression is like an expression
2889 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2890 any side-effects, unless it was explicitly cast to (void). */
2891 if (warn_unused_value
2892 && ! (TREE_CODE (expr1) == CONVERT_EXPR
2893 && VOID_TYPE_P (TREE_TYPE (expr1))))
2894 warning ("left-hand operand of comma expression has no effect");
2897 /* With -Wunused, we should also warn if the left-hand operand does have
2898 side-effects, but computes a value which is not used. For example, in
2899 `foo() + bar(), baz()' the result of the `+' operator is not used,
2900 so we should issue a warning. */
2901 else if (warn_unused_value)
2902 warn_if_unused_value (expr1, input_location);
2904 return build (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
2907 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2910 build_c_cast (tree type, tree expr)
2914 if (type == error_mark_node || expr == error_mark_node)
2915 return error_mark_node;
2917 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2918 only in <protocol> qualifications. But when constructing cast expressions,
2919 the protocols do matter and must be kept around. */
2920 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2921 type = TYPE_MAIN_VARIANT (type);
2923 if (TREE_CODE (type) == ARRAY_TYPE)
2925 error ("cast specifies array type");
2926 return error_mark_node;
2929 if (TREE_CODE (type) == FUNCTION_TYPE)
2931 error ("cast specifies function type");
2932 return error_mark_node;
2935 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2939 if (TREE_CODE (type) == RECORD_TYPE
2940 || TREE_CODE (type) == UNION_TYPE)
2941 pedwarn ("ISO C forbids casting nonscalar to the same type");
2944 else if (TREE_CODE (type) == UNION_TYPE)
2947 value = default_function_array_conversion (value);
2949 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2950 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2951 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
2959 pedwarn ("ISO C forbids casts to union type");
2960 t = digest_init (type,
2961 build_constructor (type,
2962 build_tree_list (field, value)),
2964 TREE_CONSTANT (t) = TREE_CONSTANT (value);
2965 TREE_INVARIANT (t) = TREE_INVARIANT (value);
2968 error ("cast to union type from type not present in union");
2969 return error_mark_node;
2975 /* If casting to void, avoid the error that would come
2976 from default_conversion in the case of a non-lvalue array. */
2977 if (type == void_type_node)
2978 return build1 (CONVERT_EXPR, type, value);
2980 /* Convert functions and arrays to pointers,
2981 but don't convert any other types. */
2982 value = default_function_array_conversion (value);
2983 otype = TREE_TYPE (value);
2985 /* Optionally warn about potentially worrisome casts. */
2988 && TREE_CODE (type) == POINTER_TYPE
2989 && TREE_CODE (otype) == POINTER_TYPE)
2991 tree in_type = type;
2992 tree in_otype = otype;
2996 /* Check that the qualifiers on IN_TYPE are a superset of
2997 the qualifiers of IN_OTYPE. The outermost level of
2998 POINTER_TYPE nodes is uninteresting and we stop as soon
2999 as we hit a non-POINTER_TYPE node on either type. */
3002 in_otype = TREE_TYPE (in_otype);
3003 in_type = TREE_TYPE (in_type);
3005 /* GNU C allows cv-qualified function types. 'const'
3006 means the function is very pure, 'volatile' means it
3007 can't return. We need to warn when such qualifiers
3008 are added, not when they're taken away. */
3009 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3010 && TREE_CODE (in_type) == FUNCTION_TYPE)
3011 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3013 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3015 while (TREE_CODE (in_type) == POINTER_TYPE
3016 && TREE_CODE (in_otype) == POINTER_TYPE);
3019 warning ("cast adds new qualifiers to function type");
3022 /* There are qualifiers present in IN_OTYPE that are not
3023 present in IN_TYPE. */
3024 warning ("cast discards qualifiers from pointer target type");
3027 /* Warn about possible alignment problems. */
3028 if (STRICT_ALIGNMENT && warn_cast_align
3029 && TREE_CODE (type) == POINTER_TYPE
3030 && TREE_CODE (otype) == POINTER_TYPE
3031 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3032 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3033 /* Don't warn about opaque types, where the actual alignment
3034 restriction is unknown. */
3035 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3036 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3037 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3038 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3039 warning ("cast increases required alignment of target type");
3041 if (TREE_CODE (type) == INTEGER_TYPE
3042 && TREE_CODE (otype) == POINTER_TYPE
3043 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3044 && !TREE_CONSTANT (value))
3045 warning ("cast from pointer to integer of different size");
3047 if (warn_bad_function_cast
3048 && TREE_CODE (value) == CALL_EXPR
3049 && TREE_CODE (type) != TREE_CODE (otype))
3050 warning ("cast does not match function type");
3052 if (TREE_CODE (type) == POINTER_TYPE
3053 && TREE_CODE (otype) == INTEGER_TYPE
3054 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3055 /* Don't warn about converting any constant. */
3056 && !TREE_CONSTANT (value))
3057 warning ("cast to pointer from integer of different size");
3059 if (TREE_CODE (type) == POINTER_TYPE
3060 && TREE_CODE (otype) == POINTER_TYPE
3061 && TREE_CODE (expr) == ADDR_EXPR
3062 && DECL_P (TREE_OPERAND (expr, 0))
3063 && flag_strict_aliasing && warn_strict_aliasing
3064 && !VOID_TYPE_P (TREE_TYPE (type)))
3066 /* Casting the address of a decl to non void pointer. Warn
3067 if the cast breaks type based aliasing. */
3068 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3069 warning ("type-punning to incomplete type might break strict-aliasing rules");
3072 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3073 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3075 if (!alias_sets_conflict_p (set1, set2))
3076 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3077 else if (warn_strict_aliasing > 1
3078 && !alias_sets_might_conflict_p (set1, set2))
3079 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3083 /* If pedantic, warn for conversions between function and object
3084 pointer types, except for converting a null pointer constant
3085 to function pointer type. */
3087 && TREE_CODE (type) == POINTER_TYPE
3088 && TREE_CODE (otype) == POINTER_TYPE
3089 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3090 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3091 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3094 && TREE_CODE (type) == POINTER_TYPE
3095 && TREE_CODE (otype) == POINTER_TYPE
3096 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3097 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3098 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3099 && TREE_CODE (expr) != NOP_EXPR))
3100 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3103 /* Replace a nonvolatile const static variable with its value. */
3104 if (optimize && TREE_CODE (value) == VAR_DECL)
3105 value = decl_constant_value (value);
3106 value = convert (type, value);
3108 /* Ignore any integer overflow caused by the cast. */
3109 if (TREE_CODE (value) == INTEGER_CST)
3111 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3113 if (TREE_CODE_CLASS (TREE_CODE (ovalue)) == 'c')
3114 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3118 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3119 if (TREE_CODE (value) == INTEGER_CST
3120 && TREE_CODE (expr) == INTEGER_CST
3121 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3122 value = non_lvalue (value);
3124 /* Don't let a cast be an lvalue. */
3126 value = non_lvalue (value);
3131 /* Interpret a cast of expression EXPR to type TYPE. */
3133 c_cast_expr (tree type, tree expr)
3135 int saved_wsp = warn_strict_prototypes;
3137 /* This avoids warnings about unprototyped casts on
3138 integers. E.g. "#define SIG_DFL (void(*)())0". */
3139 if (TREE_CODE (expr) == INTEGER_CST)
3140 warn_strict_prototypes = 0;
3141 type = groktypename (type);
3142 warn_strict_prototypes = saved_wsp;
3144 return build_c_cast (type, expr);
3148 /* Build an assignment expression of lvalue LHS from value RHS.
3149 MODIFYCODE is the code for a binary operator that we use
3150 to combine the old value of LHS with RHS to get the new value.
3151 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3154 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3158 tree lhstype = TREE_TYPE (lhs);
3159 tree olhstype = lhstype;
3161 /* Types that aren't fully specified cannot be used in assignments. */
3162 lhs = require_complete_type (lhs);
3164 /* Avoid duplicate error messages from operands that had errors. */
3165 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3166 return error_mark_node;
3168 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3169 /* Do not use STRIP_NOPS here. We do not want an enumerator
3170 whose value is 0 to count as a null pointer constant. */
3171 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3172 rhs = TREE_OPERAND (rhs, 0);
3176 /* If a binary op has been requested, combine the old LHS value with the RHS
3177 producing the value we should actually store into the LHS. */
3179 if (modifycode != NOP_EXPR)
3181 lhs = stabilize_reference (lhs);
3182 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3185 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3186 return error_mark_node;
3188 /* Warn about storing in something that is `const'. */
3190 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3191 || ((TREE_CODE (lhstype) == RECORD_TYPE
3192 || TREE_CODE (lhstype) == UNION_TYPE)
3193 && C_TYPE_FIELDS_READONLY (lhstype)))
3194 readonly_error (lhs, "assignment");
3196 /* If storing into a structure or union member,
3197 it has probably been given type `int'.
3198 Compute the type that would go with
3199 the actual amount of storage the member occupies. */
3201 if (TREE_CODE (lhs) == COMPONENT_REF
3202 && (TREE_CODE (lhstype) == INTEGER_TYPE
3203 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3204 || TREE_CODE (lhstype) == REAL_TYPE
3205 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3206 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3208 /* If storing in a field that is in actuality a short or narrower than one,
3209 we must store in the field in its actual type. */
3211 if (lhstype != TREE_TYPE (lhs))
3213 lhs = copy_node (lhs);
3214 TREE_TYPE (lhs) = lhstype;
3217 /* Convert new value to destination type. */
3219 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3220 NULL_TREE, NULL_TREE, 0);
3221 if (TREE_CODE (newrhs) == ERROR_MARK)
3222 return error_mark_node;
3226 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3227 TREE_SIDE_EFFECTS (result) = 1;
3229 /* If we got the LHS in a different type for storing in,
3230 convert the result back to the nominal type of LHS
3231 so that the value we return always has the same type
3232 as the LHS argument. */
3234 if (olhstype == TREE_TYPE (result))
3236 return convert_for_assignment (olhstype, result, _("assignment"),
3237 NULL_TREE, NULL_TREE, 0);
3240 /* Convert value RHS to type TYPE as preparation for an assignment
3241 to an lvalue of type TYPE.
3242 The real work of conversion is done by `convert'.
3243 The purpose of this function is to generate error messages
3244 for assignments that are not allowed in C.
3245 ERRTYPE is a string to use in error messages:
3246 "assignment", "return", etc. If it is null, this is parameter passing
3247 for a function call (and different error messages are output).
3249 FUNNAME is the name of the function being called,
3250 as an IDENTIFIER_NODE, or null.
3251 PARMNUM is the number of the argument, for printing in error messages. */
3254 convert_for_assignment (tree type, tree rhs, const char *errtype,
3255 tree fundecl, tree funname, int parmnum)
3257 enum tree_code codel = TREE_CODE (type);
3259 enum tree_code coder;
3261 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3262 /* Do not use STRIP_NOPS here. We do not want an enumerator
3263 whose value is 0 to count as a null pointer constant. */
3264 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3265 rhs = TREE_OPERAND (rhs, 0);
3267 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3268 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3269 rhs = default_conversion (rhs);
3270 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3271 rhs = decl_constant_value_for_broken_optimization (rhs);
3273 rhstype = TREE_TYPE (rhs);
3274 coder = TREE_CODE (rhstype);
3276 if (coder == ERROR_MARK)
3277 return error_mark_node;
3279 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3281 overflow_warning (rhs);
3282 /* Check for Objective-C protocols. This will automatically
3283 issue a warning if there are protocol violations. No need to
3284 use the return value. */
3285 if (c_dialect_objc ())
3286 objc_comptypes (type, rhstype, 0);
3290 if (coder == VOID_TYPE)
3292 error ("void value not ignored as it ought to be");
3293 return error_mark_node;
3295 /* A type converts to a reference to it.
3296 This code doesn't fully support references, it's just for the
3297 special case of va_start and va_copy. */
3298 if (codel == REFERENCE_TYPE
3299 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3301 if (!lvalue_p (rhs))
3303 error ("cannot pass rvalue to reference parameter");
3304 return error_mark_node;
3306 if (!c_mark_addressable (rhs))
3307 return error_mark_node;
3308 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3310 /* We already know that these two types are compatible, but they
3311 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3312 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3313 likely to be va_list, a typedef to __builtin_va_list, which
3314 is different enough that it will cause problems later. */
3315 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3316 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3318 rhs = build1 (NOP_EXPR, type, rhs);
3321 /* Some types can interconvert without explicit casts. */
3322 else if (codel == VECTOR_TYPE
3323 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3324 return convert (type, rhs);
3325 /* Arithmetic types all interconvert, and enum is treated like int. */
3326 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3327 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3328 || codel == BOOLEAN_TYPE)
3329 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3330 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3331 || coder == BOOLEAN_TYPE))
3332 return convert_and_check (type, rhs);
3334 /* Conversion to a transparent union from its member types.
3335 This applies only to function arguments. */
3336 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3339 tree marginal_memb_type = 0;
3341 for (memb_types = TYPE_FIELDS (type); memb_types;
3342 memb_types = TREE_CHAIN (memb_types))
3344 tree memb_type = TREE_TYPE (memb_types);
3346 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3347 TYPE_MAIN_VARIANT (rhstype)))
3350 if (TREE_CODE (memb_type) != POINTER_TYPE)
3353 if (coder == POINTER_TYPE)
3355 tree ttl = TREE_TYPE (memb_type);
3356 tree ttr = TREE_TYPE (rhstype);
3358 /* Any non-function converts to a [const][volatile] void *
3359 and vice versa; otherwise, targets must be the same.
3360 Meanwhile, the lhs target must have all the qualifiers of
3362 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3363 || comp_target_types (memb_type, rhstype, 0))
3365 /* If this type won't generate any warnings, use it. */
3366 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3367 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3368 && TREE_CODE (ttl) == FUNCTION_TYPE)
3369 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3370 == TYPE_QUALS (ttr))
3371 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3372 == TYPE_QUALS (ttl))))
3375 /* Keep looking for a better type, but remember this one. */
3376 if (! marginal_memb_type)
3377 marginal_memb_type = memb_type;
3381 /* Can convert integer zero to any pointer type. */
3382 if (integer_zerop (rhs)
3383 || (TREE_CODE (rhs) == NOP_EXPR
3384 && integer_zerop (TREE_OPERAND (rhs, 0))))
3386 rhs = null_pointer_node;
3391 if (memb_types || marginal_memb_type)
3395 /* We have only a marginally acceptable member type;
3396 it needs a warning. */
3397 tree ttl = TREE_TYPE (marginal_memb_type);
3398 tree ttr = TREE_TYPE (rhstype);
3400 /* Const and volatile mean something different for function
3401 types, so the usual warnings are not appropriate. */
3402 if (TREE_CODE (ttr) == FUNCTION_TYPE
3403 && TREE_CODE (ttl) == FUNCTION_TYPE)
3405 /* Because const and volatile on functions are
3406 restrictions that say the function will not do
3407 certain things, it is okay to use a const or volatile
3408 function where an ordinary one is wanted, but not
3410 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3411 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3412 errtype, funname, parmnum);
3414 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3415 warn_for_assignment ("%s discards qualifiers from pointer target type",
3420 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3421 pedwarn ("ISO C prohibits argument conversion to union type");
3423 return build1 (NOP_EXPR, type, rhs);
3427 /* Conversions among pointers */
3428 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3429 && (coder == codel))
3431 tree ttl = TREE_TYPE (type);
3432 tree ttr = TREE_TYPE (rhstype);
3433 bool is_opaque_pointer;
3434 int target_cmp = 0; /* Cache comp_target_types () result. */
3436 /* Opaque pointers are treated like void pointers. */
3437 is_opaque_pointer = (targetm.vector_opaque_p (type)
3438 || targetm.vector_opaque_p (rhstype))
3439 && TREE_CODE (ttl) == VECTOR_TYPE
3440 && TREE_CODE (ttr) == VECTOR_TYPE;
3442 /* Any non-function converts to a [const][volatile] void *
3443 and vice versa; otherwise, targets must be the same.
3444 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3445 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3446 || (target_cmp = comp_target_types (type, rhstype, 0))
3447 || is_opaque_pointer
3448 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3449 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3452 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3455 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3456 which are not ANSI null ptr constants. */
3457 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3458 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3459 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3460 errtype, funname, parmnum);
3461 /* Const and volatile mean something different for function types,
3462 so the usual warnings are not appropriate. */
3463 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3464 && TREE_CODE (ttl) != FUNCTION_TYPE)
3466 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3467 warn_for_assignment ("%s discards qualifiers from pointer target type",
3468 errtype, funname, parmnum);
3469 /* If this is not a case of ignoring a mismatch in signedness,
3471 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3474 /* If there is a mismatch, do warn. */
3476 warn_for_assignment ("pointer targets in %s differ in signedness",
3477 errtype, funname, parmnum);
3479 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3480 && TREE_CODE (ttr) == FUNCTION_TYPE)
3482 /* Because const and volatile on functions are restrictions
3483 that say the function will not do certain things,
3484 it is okay to use a const or volatile function
3485 where an ordinary one is wanted, but not vice-versa. */
3486 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3487 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3488 errtype, funname, parmnum);
3492 warn_for_assignment ("%s from incompatible pointer type",
3493 errtype, funname, parmnum);
3494 return convert (type, rhs);
3496 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3498 error ("invalid use of non-lvalue array");
3499 return error_mark_node;
3501 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3503 /* An explicit constant 0 can convert to a pointer,
3504 or one that results from arithmetic, even including
3505 a cast to integer type. */
3506 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3508 ! (TREE_CODE (rhs) == NOP_EXPR
3509 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3510 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3511 && integer_zerop (TREE_OPERAND (rhs, 0))))
3512 warn_for_assignment ("%s makes pointer from integer without a cast",
3513 errtype, funname, parmnum);
3515 return convert (type, rhs);
3517 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3519 warn_for_assignment ("%s makes integer from pointer without a cast",
3520 errtype, funname, parmnum);
3521 return convert (type, rhs);
3523 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3524 return convert (type, rhs);
3530 tree selector = objc_message_selector ();
3532 if (selector && parmnum > 2)
3533 error ("incompatible type for argument %d of `%s'",
3534 parmnum - 2, IDENTIFIER_POINTER (selector));
3536 error ("incompatible type for argument %d of `%s'",
3537 parmnum, IDENTIFIER_POINTER (funname));
3540 error ("incompatible type for argument %d of indirect function call",
3544 error ("incompatible types in %s", errtype);
3546 return error_mark_node;
3549 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3550 is used for error and waring reporting and indicates which argument
3551 is being processed. */
3554 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3558 /* If FN was prototyped, the value has been converted already
3559 in convert_arguments. */
3560 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3563 type = TREE_TYPE (parm);
3564 ret = convert_for_assignment (type, value,
3565 (char *) 0 /* arg passing */, fn,
3566 DECL_NAME (fn), argnum);
3567 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3568 && INTEGRAL_TYPE_P (type)
3569 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3570 ret = default_conversion (ret);
3574 /* Print a warning using MSGID.
3575 It gets OPNAME as its one parameter.
3576 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3577 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3578 FUNCTION and ARGNUM are handled specially if we are building an
3579 Objective-C selector. */
3582 warn_for_assignment (const char *msgid, const char *opname, tree function,
3587 tree selector = objc_message_selector ();
3590 if (selector && argnum > 2)
3592 function = selector;
3599 /* Function name is known; supply it. */
3600 const char *const argstring = _("passing arg of `%s'");
3601 new_opname = alloca (IDENTIFIER_LENGTH (function)
3602 + strlen (argstring) + 1 + 1);
3603 sprintf (new_opname, argstring,
3604 IDENTIFIER_POINTER (function));
3608 /* Function name unknown (call through ptr). */
3609 const char *const argnofun = _("passing arg of pointer to function");
3610 new_opname = alloca (strlen (argnofun) + 1 + 1);
3611 sprintf (new_opname, argnofun);
3616 /* Function name is known; supply it. */
3617 const char *const argstring = _("passing arg %d of `%s'");
3618 new_opname = alloca (IDENTIFIER_LENGTH (function)
3619 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3620 sprintf (new_opname, argstring, argnum,
3621 IDENTIFIER_POINTER (function));
3625 /* Function name unknown (call through ptr); just give arg number. */
3626 const char *const argnofun = _("passing arg %d of pointer to function");
3627 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3628 sprintf (new_opname, argnofun, argnum);
3630 opname = new_opname;
3632 pedwarn (msgid, opname);
3635 /* If VALUE is a compound expr all of whose expressions are constant, then
3636 return its value. Otherwise, return error_mark_node.
3638 This is for handling COMPOUND_EXPRs as initializer elements
3639 which is allowed with a warning when -pedantic is specified. */
3642 valid_compound_expr_initializer (tree value, tree endtype)
3644 if (TREE_CODE (value) == COMPOUND_EXPR)
3646 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3648 return error_mark_node;
3649 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3652 else if (! TREE_CONSTANT (value)
3653 && ! initializer_constant_valid_p (value, endtype))
3654 return error_mark_node;
3659 /* Perform appropriate conversions on the initial value of a variable,
3660 store it in the declaration DECL,
3661 and print any error messages that are appropriate.
3662 If the init is invalid, store an ERROR_MARK. */
3665 store_init_value (tree decl, tree init)
3669 /* If variable's type was invalidly declared, just ignore it. */
3671 type = TREE_TYPE (decl);
3672 if (TREE_CODE (type) == ERROR_MARK)
3675 /* Digest the specified initializer into an expression. */
3677 value = digest_init (type, init, TREE_STATIC (decl));
3679 /* Store the expression if valid; else report error. */
3681 if (warn_traditional && !in_system_header
3682 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3683 warning ("traditional C rejects automatic aggregate initialization");
3685 DECL_INITIAL (decl) = value;
3687 /* ANSI wants warnings about out-of-range constant initializers. */
3688 STRIP_TYPE_NOPS (value);
3689 constant_expression_warning (value);
3691 /* Check if we need to set array size from compound literal size. */
3692 if (TREE_CODE (type) == ARRAY_TYPE
3693 && TYPE_DOMAIN (type) == 0
3694 && value != error_mark_node)
3696 tree inside_init = init;
3698 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3699 inside_init = TREE_OPERAND (init, 0);
3700 inside_init = fold (inside_init);
3702 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3704 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3706 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3708 /* For int foo[] = (int [3]){1}; we need to set array size
3709 now since later on array initializer will be just the
3710 brace enclosed list of the compound literal. */
3711 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3713 layout_decl (decl, 0);
3719 /* Methods for storing and printing names for error messages. */
3721 /* Implement a spelling stack that allows components of a name to be pushed
3722 and popped. Each element on the stack is this structure. */
3734 #define SPELLING_STRING 1
3735 #define SPELLING_MEMBER 2
3736 #define SPELLING_BOUNDS 3
3738 static struct spelling *spelling; /* Next stack element (unused). */
3739 static struct spelling *spelling_base; /* Spelling stack base. */
3740 static int spelling_size; /* Size of the spelling stack. */
3742 /* Macros to save and restore the spelling stack around push_... functions.
3743 Alternative to SAVE_SPELLING_STACK. */
3745 #define SPELLING_DEPTH() (spelling - spelling_base)
3746 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3748 /* Push an element on the spelling stack with type KIND and assign VALUE
3751 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3753 int depth = SPELLING_DEPTH (); \
3755 if (depth >= spelling_size) \
3757 spelling_size += 10; \
3758 if (spelling_base == 0) \
3759 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3761 spelling_base = xrealloc (spelling_base, \
3762 spelling_size * sizeof (struct spelling)); \
3763 RESTORE_SPELLING_DEPTH (depth); \
3766 spelling->kind = (KIND); \
3767 spelling->MEMBER = (VALUE); \
3771 /* Push STRING on the stack. Printed literally. */
3774 push_string (const char *string)
3776 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3779 /* Push a member name on the stack. Printed as '.' STRING. */
3782 push_member_name (tree decl)
3784 const char *const string
3785 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3786 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3789 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3792 push_array_bounds (int bounds)
3794 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3797 /* Compute the maximum size in bytes of the printed spelling. */
3800 spelling_length (void)
3805 for (p = spelling_base; p < spelling; p++)
3807 if (p->kind == SPELLING_BOUNDS)
3810 size += strlen (p->u.s) + 1;
3816 /* Print the spelling to BUFFER and return it. */
3819 print_spelling (char *buffer)
3824 for (p = spelling_base; p < spelling; p++)
3825 if (p->kind == SPELLING_BOUNDS)
3827 sprintf (d, "[%d]", p->u.i);
3833 if (p->kind == SPELLING_MEMBER)
3835 for (s = p->u.s; (*d = *s++); d++)
3842 /* Issue an error message for a bad initializer component.
3843 MSGID identifies the message.
3844 The component name is taken from the spelling stack. */
3847 error_init (const char *msgid)
3851 error ("%s", _(msgid));
3852 ofwhat = print_spelling (alloca (spelling_length () + 1));
3854 error ("(near initialization for `%s')", ofwhat);
3857 /* Issue a pedantic warning for a bad initializer component.
3858 MSGID identifies the message.
3859 The component name is taken from the spelling stack. */
3862 pedwarn_init (const char *msgid)
3866 pedwarn ("%s", _(msgid));
3867 ofwhat = print_spelling (alloca (spelling_length () + 1));
3869 pedwarn ("(near initialization for `%s')", ofwhat);
3872 /* Issue a warning for a bad initializer component.
3873 MSGID identifies the message.
3874 The component name is taken from the spelling stack. */
3877 warning_init (const char *msgid)
3881 warning ("%s", _(msgid));
3882 ofwhat = print_spelling (alloca (spelling_length () + 1));
3884 warning ("(near initialization for `%s')", ofwhat);
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 REQUIRE_CONSTANT requests an error if non-constant initializers or
3891 elements are seen. */
3894 digest_init (tree type, tree init, int require_constant)
3896 enum tree_code code = TREE_CODE (type);
3897 tree inside_init = init;
3899 if (type == error_mark_node
3900 || init == error_mark_node
3901 || TREE_TYPE (init) == error_mark_node)
3902 return error_mark_node;
3904 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3905 /* Do not use STRIP_NOPS here. We do not want an enumerator
3906 whose value is 0 to count as a null pointer constant. */
3907 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3908 inside_init = TREE_OPERAND (init, 0);
3910 inside_init = fold (inside_init);
3912 /* Initialization of an array of chars from a string constant
3913 optionally enclosed in braces. */
3915 if (code == ARRAY_TYPE)
3917 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3918 if ((typ1 == char_type_node
3919 || typ1 == signed_char_type_node
3920 || typ1 == unsigned_char_type_node
3921 || typ1 == unsigned_wchar_type_node
3922 || typ1 == signed_wchar_type_node)
3923 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3925 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3926 TYPE_MAIN_VARIANT (type)))
3929 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3931 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3933 error_init ("char-array initialized from wide string");
3934 return error_mark_node;
3936 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3938 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3940 error_init ("int-array initialized from non-wide string");
3941 return error_mark_node;
3944 TREE_TYPE (inside_init) = type;
3945 if (TYPE_DOMAIN (type) != 0
3946 && TYPE_SIZE (type) != 0
3947 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3948 /* Subtract 1 (or sizeof (wchar_t))
3949 because it's ok to ignore the terminating null char
3950 that is counted in the length of the constant. */
3951 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
3952 TREE_STRING_LENGTH (inside_init)
3953 - ((TYPE_PRECISION (typ1)
3954 != TYPE_PRECISION (char_type_node))
3955 ? (TYPE_PRECISION (wchar_type_node)
3958 pedwarn_init ("initializer-string for array of chars is too long");
3964 /* Build a VECTOR_CST from a *constant* vector constructor. If the
3965 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
3966 below and handle as a constructor. */
3967 if (code == VECTOR_TYPE
3968 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
3969 && TREE_CONSTANT (inside_init))
3971 if (TREE_CODE (inside_init) == VECTOR_CST
3972 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3973 TYPE_MAIN_VARIANT (type)))
3976 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
3979 /* Any type can be initialized
3980 from an expression of the same type, optionally with braces. */
3982 if (inside_init && TREE_TYPE (inside_init) != 0
3983 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3984 TYPE_MAIN_VARIANT (type))
3985 || (code == ARRAY_TYPE
3986 && comptypes (TREE_TYPE (inside_init), type))
3987 || (code == VECTOR_TYPE
3988 && comptypes (TREE_TYPE (inside_init), type))
3989 || (code == POINTER_TYPE
3990 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3991 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
3993 || (code == POINTER_TYPE
3994 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
3995 && comptypes (TREE_TYPE (inside_init),
3996 TREE_TYPE (type)))))
3998 if (code == POINTER_TYPE)
4000 inside_init = default_function_array_conversion (inside_init);
4002 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4004 error_init ("invalid use of non-lvalue array");
4005 return error_mark_node;
4009 if (code == VECTOR_TYPE)
4010 /* Although the types are compatible, we may require a
4012 inside_init = convert (type, inside_init);
4014 if (require_constant && !flag_isoc99
4015 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4017 /* As an extension, allow initializing objects with static storage
4018 duration with compound literals (which are then treated just as
4019 the brace enclosed list they contain). */
4020 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4021 inside_init = DECL_INITIAL (decl);
4024 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4025 && TREE_CODE (inside_init) != CONSTRUCTOR)
4027 error_init ("array initialized from non-constant array expression");
4028 return error_mark_node;
4031 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4032 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4034 /* Compound expressions can only occur here if -pedantic or
4035 -pedantic-errors is specified. In the later case, we always want
4036 an error. In the former case, we simply want a warning. */
4037 if (require_constant && pedantic
4038 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4041 = valid_compound_expr_initializer (inside_init,
4042 TREE_TYPE (inside_init));
4043 if (inside_init == error_mark_node)
4044 error_init ("initializer element is not constant");
4046 pedwarn_init ("initializer element is not constant");
4047 if (flag_pedantic_errors)
4048 inside_init = error_mark_node;
4050 else if (require_constant
4051 && (!TREE_CONSTANT (inside_init)
4052 /* This test catches things like `7 / 0' which
4053 result in an expression for which TREE_CONSTANT
4054 is true, but which is not actually something
4055 that is a legal constant. We really should not
4056 be using this function, because it is a part of
4057 the back-end. Instead, the expression should
4058 already have been turned into ERROR_MARK_NODE. */
4059 || !initializer_constant_valid_p (inside_init,
4060 TREE_TYPE (inside_init))))
4062 error_init ("initializer element is not constant");
4063 inside_init = error_mark_node;
4069 /* Handle scalar types, including conversions. */
4071 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4072 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4073 || code == VECTOR_TYPE)
4075 /* Note that convert_for_assignment calls default_conversion
4076 for arrays and functions. We must not call it in the
4077 case where inside_init is a null pointer constant. */
4079 = convert_for_assignment (type, init, _("initialization"),
4080 NULL_TREE, NULL_TREE, 0);
4082 if (require_constant && ! TREE_CONSTANT (inside_init))
4084 error_init ("initializer element is not constant");
4085 inside_init = error_mark_node;
4087 else if (require_constant
4088 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4090 error_init ("initializer element is not computable at load time");
4091 inside_init = error_mark_node;
4097 /* Come here only for records and arrays. */
4099 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4101 error_init ("variable-sized object may not be initialized");
4102 return error_mark_node;
4105 error_init ("invalid initializer");
4106 return error_mark_node;
4109 /* Handle initializers that use braces. */
4111 /* Type of object we are accumulating a constructor for.
4112 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4113 static tree constructor_type;
4115 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4117 static tree constructor_fields;
4119 /* For an ARRAY_TYPE, this is the specified index
4120 at which to store the next element we get. */
4121 static tree constructor_index;
4123 /* For an ARRAY_TYPE, this is the maximum index. */
4124 static tree constructor_max_index;
4126 /* For a RECORD_TYPE, this is the first field not yet written out. */
4127 static tree constructor_unfilled_fields;
4129 /* For an ARRAY_TYPE, this is the index of the first element
4130 not yet written out. */
4131 static tree constructor_unfilled_index;
4133 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4134 This is so we can generate gaps between fields, when appropriate. */
4135 static tree constructor_bit_index;
4137 /* If we are saving up the elements rather than allocating them,
4138 this is the list of elements so far (in reverse order,
4139 most recent first). */
4140 static tree constructor_elements;
4142 /* 1 if constructor should be incrementally stored into a constructor chain,
4143 0 if all the elements should be kept in AVL tree. */
4144 static int constructor_incremental;
4146 /* 1 if so far this constructor's elements are all compile-time constants. */
4147 static int constructor_constant;
4149 /* 1 if so far this constructor's elements are all valid address constants. */
4150 static int constructor_simple;
4152 /* 1 if this constructor is erroneous so far. */
4153 static int constructor_erroneous;
4155 /* Structure for managing pending initializer elements, organized as an
4160 struct init_node *left, *right;
4161 struct init_node *parent;
4167 /* Tree of pending elements at this constructor level.
4168 These are elements encountered out of order
4169 which belong at places we haven't reached yet in actually
4171 Will never hold tree nodes across GC runs. */
4172 static struct init_node *constructor_pending_elts;
4174 /* The SPELLING_DEPTH of this constructor. */
4175 static int constructor_depth;
4177 /* 0 if implicitly pushing constructor levels is allowed. */
4178 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4180 /* DECL node for which an initializer is being read.
4181 0 means we are reading a constructor expression
4182 such as (struct foo) {...}. */
4183 static tree constructor_decl;
4185 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4186 static const char *constructor_asmspec;
4188 /* Nonzero if this is an initializer for a top-level decl. */
4189 static int constructor_top_level;
4191 /* Nonzero if there were any member designators in this initializer. */
4192 static int constructor_designated;
4194 /* Nesting depth of designator list. */
4195 static int designator_depth;
4197 /* Nonzero if there were diagnosed errors in this designator list. */
4198 static int designator_errorneous;
4201 /* This stack has a level for each implicit or explicit level of
4202 structuring in the initializer, including the outermost one. It
4203 saves the values of most of the variables above. */
4205 struct constructor_range_stack;
4207 struct constructor_stack
4209 struct constructor_stack *next;
4214 tree unfilled_index;
4215 tree unfilled_fields;
4218 struct init_node *pending_elts;
4221 /* If nonzero, this value should replace the entire
4222 constructor at this level. */
4223 tree replacement_value;
4224 struct constructor_range_stack *range_stack;
4234 struct constructor_stack *constructor_stack;
4236 /* This stack represents designators from some range designator up to
4237 the last designator in the list. */
4239 struct constructor_range_stack
4241 struct constructor_range_stack *next, *prev;
4242 struct constructor_stack *stack;
4249 struct constructor_range_stack *constructor_range_stack;
4251 /* This stack records separate initializers that are nested.
4252 Nested initializers can't happen in ANSI C, but GNU C allows them
4253 in cases like { ... (struct foo) { ... } ... }. */
4255 struct initializer_stack
4257 struct initializer_stack *next;
4259 const char *asmspec;
4260 struct constructor_stack *constructor_stack;
4261 struct constructor_range_stack *constructor_range_stack;
4263 struct spelling *spelling;
4264 struct spelling *spelling_base;
4267 char require_constant_value;
4268 char require_constant_elements;
4271 struct initializer_stack *initializer_stack;
4273 /* Prepare to parse and output the initializer for variable DECL. */
4276 start_init (tree decl, tree asmspec_tree, int top_level)
4279 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4280 const char *asmspec = 0;
4283 asmspec = TREE_STRING_POINTER (asmspec_tree);
4285 p->decl = constructor_decl;
4286 p->asmspec = constructor_asmspec;
4287 p->require_constant_value = require_constant_value;
4288 p->require_constant_elements = require_constant_elements;
4289 p->constructor_stack = constructor_stack;
4290 p->constructor_range_stack = constructor_range_stack;
4291 p->elements = constructor_elements;
4292 p->spelling = spelling;
4293 p->spelling_base = spelling_base;
4294 p->spelling_size = spelling_size;
4295 p->top_level = constructor_top_level;
4296 p->next = initializer_stack;
4297 initializer_stack = p;
4299 constructor_decl = decl;
4300 constructor_asmspec = asmspec;
4301 constructor_designated = 0;
4302 constructor_top_level = top_level;
4306 require_constant_value = TREE_STATIC (decl);
4307 require_constant_elements
4308 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4309 /* For a scalar, you can always use any value to initialize,
4310 even within braces. */
4311 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4312 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4313 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4314 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4315 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4319 require_constant_value = 0;
4320 require_constant_elements = 0;
4321 locus = "(anonymous)";
4324 constructor_stack = 0;
4325 constructor_range_stack = 0;
4327 missing_braces_mentioned = 0;
4331 RESTORE_SPELLING_DEPTH (0);
4334 push_string (locus);
4340 struct initializer_stack *p = initializer_stack;
4342 /* Free the whole constructor stack of this initializer. */
4343 while (constructor_stack)
4345 struct constructor_stack *q = constructor_stack;
4346 constructor_stack = q->next;
4350 if (constructor_range_stack)
4353 /* Pop back to the data of the outer initializer (if any). */
4354 free (spelling_base);
4356 constructor_decl = p->decl;
4357 constructor_asmspec = p->asmspec;
4358 require_constant_value = p->require_constant_value;
4359 require_constant_elements = p->require_constant_elements;
4360 constructor_stack = p->constructor_stack;
4361 constructor_range_stack = p->constructor_range_stack;
4362 constructor_elements = p->elements;
4363 spelling = p->spelling;
4364 spelling_base = p->spelling_base;
4365 spelling_size = p->spelling_size;
4366 constructor_top_level = p->top_level;
4367 initializer_stack = p->next;
4371 /* Call here when we see the initializer is surrounded by braces.
4372 This is instead of a call to push_init_level;
4373 it is matched by a call to pop_init_level.
4375 TYPE is the type to initialize, for a constructor expression.
4376 For an initializer for a decl, TYPE is zero. */
4379 really_start_incremental_init (tree type)
4381 struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
4384 type = TREE_TYPE (constructor_decl);
4386 if (targetm.vector_opaque_p (type))
4387 error ("opaque vector types cannot be initialized");
4389 p->type = constructor_type;
4390 p->fields = constructor_fields;
4391 p->index = constructor_index;
4392 p->max_index = constructor_max_index;
4393 p->unfilled_index = constructor_unfilled_index;
4394 p->unfilled_fields = constructor_unfilled_fields;
4395 p->bit_index = constructor_bit_index;
4396 p->elements = constructor_elements;
4397 p->constant = constructor_constant;
4398 p->simple = constructor_simple;
4399 p->erroneous = constructor_erroneous;
4400 p->pending_elts = constructor_pending_elts;
4401 p->depth = constructor_depth;
4402 p->replacement_value = 0;
4406 p->incremental = constructor_incremental;
4407 p->designated = constructor_designated;
4409 constructor_stack = p;
4411 constructor_constant = 1;
4412 constructor_simple = 1;
4413 constructor_depth = SPELLING_DEPTH ();
4414 constructor_elements = 0;
4415 constructor_pending_elts = 0;
4416 constructor_type = type;
4417 constructor_incremental = 1;
4418 constructor_designated = 0;
4419 designator_depth = 0;
4420 designator_errorneous = 0;
4422 if (TREE_CODE (constructor_type) == RECORD_TYPE
4423 || TREE_CODE (constructor_type) == UNION_TYPE)
4425 constructor_fields = TYPE_FIELDS (constructor_type);
4426 /* Skip any nameless bit fields at the beginning. */
4427 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4428 && DECL_NAME (constructor_fields) == 0)
4429 constructor_fields = TREE_CHAIN (constructor_fields);
4431 constructor_unfilled_fields = constructor_fields;
4432 constructor_bit_index = bitsize_zero_node;
4434 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4436 if (TYPE_DOMAIN (constructor_type))
4438 constructor_max_index
4439 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4441 /* Detect non-empty initializations of zero-length arrays. */
4442 if (constructor_max_index == NULL_TREE
4443 && TYPE_SIZE (constructor_type))
4444 constructor_max_index = build_int_2 (-1, -1);
4446 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4447 to initialize VLAs will cause a proper error; avoid tree
4448 checking errors as well by setting a safe value. */
4449 if (constructor_max_index
4450 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4451 constructor_max_index = build_int_2 (-1, -1);
4454 = convert (bitsizetype,
4455 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4458 constructor_index = bitsize_zero_node;
4460 constructor_unfilled_index = constructor_index;
4462 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4464 /* Vectors are like simple fixed-size arrays. */
4465 constructor_max_index =
4466 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4467 constructor_index = convert (bitsizetype, bitsize_zero_node);
4468 constructor_unfilled_index = constructor_index;
4472 /* Handle the case of int x = {5}; */
4473 constructor_fields = constructor_type;
4474 constructor_unfilled_fields = constructor_type;
4478 /* Push down into a subobject, for initialization.
4479 If this is for an explicit set of braces, IMPLICIT is 0.
4480 If it is because the next element belongs at a lower level,
4481 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4484 push_init_level (int implicit)
4486 struct constructor_stack *p;
4487 tree value = NULL_TREE;
4489 /* If we've exhausted any levels that didn't have braces,
4491 while (constructor_stack->implicit)
4493 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4494 || TREE_CODE (constructor_type) == UNION_TYPE)
4495 && constructor_fields == 0)
4496 process_init_element (pop_init_level (1));
4497 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4498 && constructor_max_index
4499 && tree_int_cst_lt (constructor_max_index, constructor_index))
4500 process_init_element (pop_init_level (1));
4505 /* Unless this is an explicit brace, we need to preserve previous
4509 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4510 || TREE_CODE (constructor_type) == UNION_TYPE)
4511 && constructor_fields)
4512 value = find_init_member (constructor_fields);
4513 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4514 value = find_init_member (constructor_index);
4517 p = xmalloc (sizeof (struct constructor_stack));
4518 p->type = constructor_type;
4519 p->fields = constructor_fields;
4520 p->index = constructor_index;
4521 p->max_index = constructor_max_index;
4522 p->unfilled_index = constructor_unfilled_index;
4523 p->unfilled_fields = constructor_unfilled_fields;
4524 p->bit_index = constructor_bit_index;
4525 p->elements = constructor_elements;
4526 p->constant = constructor_constant;
4527 p->simple = constructor_simple;
4528 p->erroneous = constructor_erroneous;
4529 p->pending_elts = constructor_pending_elts;
4530 p->depth = constructor_depth;
4531 p->replacement_value = 0;
4532 p->implicit = implicit;
4534 p->incremental = constructor_incremental;
4535 p->designated = constructor_designated;
4536 p->next = constructor_stack;
4538 constructor_stack = p;
4540 constructor_constant = 1;
4541 constructor_simple = 1;
4542 constructor_depth = SPELLING_DEPTH ();
4543 constructor_elements = 0;
4544 constructor_incremental = 1;
4545 constructor_designated = 0;
4546 constructor_pending_elts = 0;
4549 p->range_stack = constructor_range_stack;
4550 constructor_range_stack = 0;
4551 designator_depth = 0;
4552 designator_errorneous = 0;
4555 /* Don't die if an entire brace-pair level is superfluous
4556 in the containing level. */
4557 if (constructor_type == 0)
4559 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4560 || TREE_CODE (constructor_type) == UNION_TYPE)
4562 /* Don't die if there are extra init elts at the end. */
4563 if (constructor_fields == 0)
4564 constructor_type = 0;
4567 constructor_type = TREE_TYPE (constructor_fields);
4568 push_member_name (constructor_fields);
4569 constructor_depth++;
4572 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4574 constructor_type = TREE_TYPE (constructor_type);
4575 push_array_bounds (tree_low_cst (constructor_index, 0));
4576 constructor_depth++;
4579 if (constructor_type == 0)
4581 error_init ("extra brace group at end of initializer");
4582 constructor_fields = 0;
4583 constructor_unfilled_fields = 0;
4587 if (value && TREE_CODE (value) == CONSTRUCTOR)
4589 constructor_constant = TREE_CONSTANT (value);
4590 constructor_simple = TREE_STATIC (value);
4591 constructor_elements = CONSTRUCTOR_ELTS (value);
4592 if (constructor_elements
4593 && (TREE_CODE (constructor_type) == RECORD_TYPE
4594 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4595 set_nonincremental_init ();
4598 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4600 missing_braces_mentioned = 1;
4601 warning_init ("missing braces around initializer");
4604 if (TREE_CODE (constructor_type) == RECORD_TYPE
4605 || TREE_CODE (constructor_type) == UNION_TYPE)
4607 constructor_fields = TYPE_FIELDS (constructor_type);
4608 /* Skip any nameless bit fields at the beginning. */
4609 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4610 && DECL_NAME (constructor_fields) == 0)
4611 constructor_fields = TREE_CHAIN (constructor_fields);
4613 constructor_unfilled_fields = constructor_fields;
4614 constructor_bit_index = bitsize_zero_node;
4616 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4618 /* Vectors are like simple fixed-size arrays. */
4619 constructor_max_index =
4620 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4621 constructor_index = convert (bitsizetype, integer_zero_node);
4622 constructor_unfilled_index = constructor_index;
4624 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4626 if (TYPE_DOMAIN (constructor_type))
4628 constructor_max_index
4629 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4631 /* Detect non-empty initializations of zero-length arrays. */
4632 if (constructor_max_index == NULL_TREE
4633 && TYPE_SIZE (constructor_type))
4634 constructor_max_index = build_int_2 (-1, -1);
4636 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4637 to initialize VLAs will cause a proper error; avoid tree
4638 checking errors as well by setting a safe value. */
4639 if (constructor_max_index
4640 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4641 constructor_max_index = build_int_2 (-1, -1);
4644 = convert (bitsizetype,
4645 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4648 constructor_index = bitsize_zero_node;
4650 constructor_unfilled_index = constructor_index;
4651 if (value && TREE_CODE (value) == STRING_CST)
4653 /* We need to split the char/wchar array into individual
4654 characters, so that we don't have to special case it
4656 set_nonincremental_init_from_string (value);
4661 warning_init ("braces around scalar initializer");
4662 constructor_fields = constructor_type;
4663 constructor_unfilled_fields = constructor_type;
4667 /* At the end of an implicit or explicit brace level,
4668 finish up that level of constructor.
4669 If we were outputting the elements as they are read, return 0
4670 from inner levels (process_init_element ignores that),
4671 but return error_mark_node from the outermost level
4672 (that's what we want to put in DECL_INITIAL).
4673 Otherwise, return a CONSTRUCTOR expression. */
4676 pop_init_level (int implicit)
4678 struct constructor_stack *p;
4679 tree constructor = 0;
4683 /* When we come to an explicit close brace,
4684 pop any inner levels that didn't have explicit braces. */
4685 while (constructor_stack->implicit)
4686 process_init_element (pop_init_level (1));
4688 if (constructor_range_stack)
4692 /* Now output all pending elements. */
4693 constructor_incremental = 1;
4694 output_pending_init_elements (1);
4696 p = constructor_stack;
4698 /* Error for initializing a flexible array member, or a zero-length
4699 array member in an inappropriate context. */
4700 if (constructor_type && constructor_fields
4701 && TREE_CODE (constructor_type) == ARRAY_TYPE
4702 && TYPE_DOMAIN (constructor_type)
4703 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4705 /* Silently discard empty initializations. The parser will
4706 already have pedwarned for empty brackets. */
4707 if (integer_zerop (constructor_unfilled_index))
4708 constructor_type = NULL_TREE;
4709 else if (! TYPE_SIZE (constructor_type))
4711 if (constructor_depth > 2)
4712 error_init ("initialization of flexible array member in a nested context");
4714 pedwarn_init ("initialization of a flexible array member");
4716 /* We have already issued an error message for the existence
4717 of a flexible array member not at the end of the structure.
4718 Discard the initializer so that we do not abort later. */
4719 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4720 constructor_type = NULL_TREE;
4723 /* Zero-length arrays are no longer special, so we should no longer
4728 /* Warn when some struct elements are implicitly initialized to zero. */
4731 && TREE_CODE (constructor_type) == RECORD_TYPE
4732 && constructor_unfilled_fields)
4734 /* Do not warn for flexible array members or zero-length arrays. */
4735 while (constructor_unfilled_fields
4736 && (! DECL_SIZE (constructor_unfilled_fields)
4737 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4738 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4740 /* Do not warn if this level of the initializer uses member
4741 designators; it is likely to be deliberate. */
4742 if (constructor_unfilled_fields && !constructor_designated)
4744 push_member_name (constructor_unfilled_fields);
4745 warning_init ("missing initializer");
4746 RESTORE_SPELLING_DEPTH (constructor_depth);
4750 /* Pad out the end of the structure. */
4751 if (p->replacement_value)
4752 /* If this closes a superfluous brace pair,
4753 just pass out the element between them. */
4754 constructor = p->replacement_value;
4755 else if (constructor_type == 0)
4757 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4758 && TREE_CODE (constructor_type) != UNION_TYPE
4759 && TREE_CODE (constructor_type) != ARRAY_TYPE
4760 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4762 /* A nonincremental scalar initializer--just return
4763 the element, after verifying there is just one. */
4764 if (constructor_elements == 0)
4766 if (!constructor_erroneous)
4767 error_init ("empty scalar initializer");
4768 constructor = error_mark_node;
4770 else if (TREE_CHAIN (constructor_elements) != 0)
4772 error_init ("extra elements in scalar initializer");
4773 constructor = TREE_VALUE (constructor_elements);
4776 constructor = TREE_VALUE (constructor_elements);
4780 if (constructor_erroneous)
4781 constructor = error_mark_node;
4784 constructor = build_constructor (constructor_type,
4785 nreverse (constructor_elements));
4786 if (constructor_constant)
4787 TREE_CONSTANT (constructor) = TREE_INVARIANT (constructor) = 1;
4788 if (constructor_constant && constructor_simple)
4789 TREE_STATIC (constructor) = 1;
4793 constructor_type = p->type;
4794 constructor_fields = p->fields;
4795 constructor_index = p->index;
4796 constructor_max_index = p->max_index;
4797 constructor_unfilled_index = p->unfilled_index;
4798 constructor_unfilled_fields = p->unfilled_fields;
4799 constructor_bit_index = p->bit_index;
4800 constructor_elements = p->elements;
4801 constructor_constant = p->constant;
4802 constructor_simple = p->simple;
4803 constructor_erroneous = p->erroneous;
4804 constructor_incremental = p->incremental;
4805 constructor_designated = p->designated;
4806 constructor_pending_elts = p->pending_elts;
4807 constructor_depth = p->depth;
4809 constructor_range_stack = p->range_stack;
4810 RESTORE_SPELLING_DEPTH (constructor_depth);
4812 constructor_stack = p->next;
4815 if (constructor == 0)
4817 if (constructor_stack == 0)
4818 return error_mark_node;
4824 /* Common handling for both array range and field name designators.
4825 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4828 set_designator (int array)
4831 enum tree_code subcode;
4833 /* Don't die if an entire brace-pair level is superfluous
4834 in the containing level. */
4835 if (constructor_type == 0)
4838 /* If there were errors in this designator list already, bail out silently. */
4839 if (designator_errorneous)
4842 if (!designator_depth)
4844 if (constructor_range_stack)
4847 /* Designator list starts at the level of closest explicit
4849 while (constructor_stack->implicit)
4850 process_init_element (pop_init_level (1));
4851 constructor_designated = 1;
4855 if (constructor_no_implicit)
4857 error_init ("initialization designators may not nest");
4861 if (TREE_CODE (constructor_type) == RECORD_TYPE
4862 || TREE_CODE (constructor_type) == UNION_TYPE)
4864 subtype = TREE_TYPE (constructor_fields);
4865 if (subtype != error_mark_node)
4866 subtype = TYPE_MAIN_VARIANT (subtype);
4868 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4870 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4875 subcode = TREE_CODE (subtype);
4876 if (array && subcode != ARRAY_TYPE)
4878 error_init ("array index in non-array initializer");
4881 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4883 error_init ("field name not in record or union initializer");
4887 constructor_designated = 1;
4888 push_init_level (2);
4892 /* If there are range designators in designator list, push a new designator
4893 to constructor_range_stack. RANGE_END is end of such stack range or
4894 NULL_TREE if there is no range designator at this level. */
4897 push_range_stack (tree range_end)
4899 struct constructor_range_stack *p;
4901 p = ggc_alloc (sizeof (struct constructor_range_stack));
4902 p->prev = constructor_range_stack;
4904 p->fields = constructor_fields;
4905 p->range_start = constructor_index;
4906 p->index = constructor_index;
4907 p->stack = constructor_stack;
4908 p->range_end = range_end;
4909 if (constructor_range_stack)
4910 constructor_range_stack->next = p;
4911 constructor_range_stack = p;
4914 /* Within an array initializer, specify the next index to be initialized.
4915 FIRST is that index. If LAST is nonzero, then initialize a range
4916 of indices, running from FIRST through LAST. */
4919 set_init_index (tree first, tree last)
4921 if (set_designator (1))
4924 designator_errorneous = 1;
4926 while ((TREE_CODE (first) == NOP_EXPR
4927 || TREE_CODE (first) == CONVERT_EXPR
4928 || TREE_CODE (first) == NON_LVALUE_EXPR)
4929 && (TYPE_MODE (TREE_TYPE (first))
4930 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
4931 first = TREE_OPERAND (first, 0);
4934 while ((TREE_CODE (last) == NOP_EXPR
4935 || TREE_CODE (last) == CONVERT_EXPR
4936 || TREE_CODE (last) == NON_LVALUE_EXPR)
4937 && (TYPE_MODE (TREE_TYPE (last))
4938 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
4939 last = TREE_OPERAND (last, 0);
4941 if (TREE_CODE (first) != INTEGER_CST)
4942 error_init ("nonconstant array index in initializer");
4943 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
4944 error_init ("nonconstant array index in initializer");
4945 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
4946 error_init ("array index in non-array initializer");
4947 else if (tree_int_cst_sgn (first) == -1)
4948 error_init ("array index in initializer exceeds array bounds");
4949 else if (constructor_max_index
4950 && tree_int_cst_lt (constructor_max_index, first))
4951 error_init ("array index in initializer exceeds array bounds");
4954 constructor_index = convert (bitsizetype, first);
4958 if (tree_int_cst_equal (first, last))
4960 else if (tree_int_cst_lt (last, first))
4962 error_init ("empty index range in initializer");
4967 last = convert (bitsizetype, last);
4968 if (constructor_max_index != 0
4969 && tree_int_cst_lt (constructor_max_index, last))
4971 error_init ("array index range in initializer exceeds array bounds");
4978 designator_errorneous = 0;
4979 if (constructor_range_stack || last)
4980 push_range_stack (last);
4984 /* Within a struct initializer, specify the next field to be initialized. */
4987 set_init_label (tree fieldname)
4991 if (set_designator (0))
4994 designator_errorneous = 1;
4996 if (TREE_CODE (constructor_type) != RECORD_TYPE
4997 && TREE_CODE (constructor_type) != UNION_TYPE)
4999 error_init ("field name not in record or union initializer");
5003 for (tail = TYPE_FIELDS (constructor_type); tail;
5004 tail = TREE_CHAIN (tail))
5006 if (DECL_NAME (tail) == fieldname)
5011 error ("unknown field `%s' specified in initializer",
5012 IDENTIFIER_POINTER (fieldname));
5015 constructor_fields = tail;
5017 designator_errorneous = 0;
5018 if (constructor_range_stack)
5019 push_range_stack (NULL_TREE);
5023 /* Add a new initializer to the tree of pending initializers. PURPOSE
5024 identifies the initializer, either array index or field in a structure.
5025 VALUE is the value of that index or field. */
5028 add_pending_init (tree purpose, tree value)
5030 struct init_node *p, **q, *r;
5032 q = &constructor_pending_elts;
5035 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5040 if (tree_int_cst_lt (purpose, p->purpose))
5042 else if (tree_int_cst_lt (p->purpose, purpose))
5046 if (TREE_SIDE_EFFECTS (p->value))
5047 warning_init ("initialized field with side-effects overwritten");
5057 bitpos = bit_position (purpose);
5061 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5063 else if (p->purpose != purpose)
5067 if (TREE_SIDE_EFFECTS (p->value))
5068 warning_init ("initialized field with side-effects overwritten");
5075 r = ggc_alloc (sizeof (struct init_node));
5076 r->purpose = purpose;
5087 struct init_node *s;
5091 if (p->balance == 0)
5093 else if (p->balance < 0)
5100 p->left->parent = p;
5117 constructor_pending_elts = r;
5122 struct init_node *t = r->right;
5126 r->right->parent = r;
5131 p->left->parent = p;
5134 p->balance = t->balance < 0;
5135 r->balance = -(t->balance > 0);
5150 constructor_pending_elts = t;
5156 /* p->balance == +1; growth of left side balances the node. */
5161 else /* r == p->right */
5163 if (p->balance == 0)
5164 /* Growth propagation from right side. */
5166 else if (p->balance > 0)
5173 p->right->parent = p;
5190 constructor_pending_elts = r;
5192 else /* r->balance == -1 */
5195 struct init_node *t = r->left;
5199 r->left->parent = r;
5204 p->right->parent = p;
5207 r->balance = (t->balance < 0);
5208 p->balance = -(t->balance > 0);
5223 constructor_pending_elts = t;
5229 /* p->balance == -1; growth of right side balances the node. */
5240 /* Build AVL tree from a sorted chain. */
5243 set_nonincremental_init (void)
5247 if (TREE_CODE (constructor_type) != RECORD_TYPE
5248 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5251 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5252 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5253 constructor_elements = 0;
5254 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5256 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5257 /* Skip any nameless bit fields at the beginning. */
5258 while (constructor_unfilled_fields != 0
5259 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5260 && DECL_NAME (constructor_unfilled_fields) == 0)
5261 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5264 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5266 if (TYPE_DOMAIN (constructor_type))
5267 constructor_unfilled_index
5268 = convert (bitsizetype,
5269 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5271 constructor_unfilled_index = bitsize_zero_node;
5273 constructor_incremental = 0;
5276 /* Build AVL tree from a string constant. */
5279 set_nonincremental_init_from_string (tree str)
5281 tree value, purpose, type;
5282 HOST_WIDE_INT val[2];
5283 const char *p, *end;
5284 int byte, wchar_bytes, charwidth, bitpos;
5286 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5289 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5290 == TYPE_PRECISION (char_type_node))
5292 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5293 == TYPE_PRECISION (wchar_type_node))
5294 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5298 charwidth = TYPE_PRECISION (char_type_node);
5299 type = TREE_TYPE (constructor_type);
5300 p = TREE_STRING_POINTER (str);
5301 end = p + TREE_STRING_LENGTH (str);
5303 for (purpose = bitsize_zero_node;
5304 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5305 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5307 if (wchar_bytes == 1)
5309 val[1] = (unsigned char) *p++;
5316 for (byte = 0; byte < wchar_bytes; byte++)
5318 if (BYTES_BIG_ENDIAN)
5319 bitpos = (wchar_bytes - byte - 1) * charwidth;
5321 bitpos = byte * charwidth;
5322 val[bitpos < HOST_BITS_PER_WIDE_INT]
5323 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5324 << (bitpos % HOST_BITS_PER_WIDE_INT);
5328 if (!TYPE_UNSIGNED (type))
5330 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5331 if (bitpos < HOST_BITS_PER_WIDE_INT)
5333 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5335 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5339 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5344 else if (val[0] & (((HOST_WIDE_INT) 1)
5345 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5346 val[0] |= ((HOST_WIDE_INT) -1)
5347 << (bitpos - HOST_BITS_PER_WIDE_INT);
5350 value = build_int_2 (val[1], val[0]);
5351 TREE_TYPE (value) = type;
5352 add_pending_init (purpose, value);
5355 constructor_incremental = 0;
5358 /* Return value of FIELD in pending initializer or zero if the field was
5359 not initialized yet. */
5362 find_init_member (tree field)
5364 struct init_node *p;
5366 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5368 if (constructor_incremental
5369 && tree_int_cst_lt (field, constructor_unfilled_index))
5370 set_nonincremental_init ();
5372 p = constructor_pending_elts;
5375 if (tree_int_cst_lt (field, p->purpose))
5377 else if (tree_int_cst_lt (p->purpose, field))
5383 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5385 tree bitpos = bit_position (field);
5387 if (constructor_incremental
5388 && (!constructor_unfilled_fields
5389 || tree_int_cst_lt (bitpos,
5390 bit_position (constructor_unfilled_fields))))
5391 set_nonincremental_init ();
5393 p = constructor_pending_elts;
5396 if (field == p->purpose)
5398 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5404 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5406 if (constructor_elements
5407 && TREE_PURPOSE (constructor_elements) == field)
5408 return TREE_VALUE (constructor_elements);
5413 /* "Output" the next constructor element.
5414 At top level, really output it to assembler code now.
5415 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5416 TYPE is the data type that the containing data type wants here.
5417 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5419 PENDING if non-nil means output pending elements that belong
5420 right after this element. (PENDING is normally 1;
5421 it is 0 while outputting pending elements, to avoid recursion.) */
5424 output_init_element (tree value, tree type, tree field, int pending)
5426 if (type == error_mark_node)
5428 constructor_erroneous = 1;
5431 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5432 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5433 && !(TREE_CODE (value) == STRING_CST
5434 && TREE_CODE (type) == ARRAY_TYPE
5435 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5436 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5437 TYPE_MAIN_VARIANT (type))))
5438 value = default_conversion (value);
5440 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5441 && require_constant_value && !flag_isoc99 && pending)
5443 /* As an extension, allow initializing objects with static storage
5444 duration with compound literals (which are then treated just as
5445 the brace enclosed list they contain). */
5446 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5447 value = DECL_INITIAL (decl);
5450 if (value == error_mark_node)
5451 constructor_erroneous = 1;
5452 else if (!TREE_CONSTANT (value))
5453 constructor_constant = 0;
5454 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5455 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5456 || TREE_CODE (constructor_type) == UNION_TYPE)
5457 && DECL_C_BIT_FIELD (field)
5458 && TREE_CODE (value) != INTEGER_CST))
5459 constructor_simple = 0;
5461 if (require_constant_value && ! TREE_CONSTANT (value))
5463 error_init ("initializer element is not constant");
5464 value = error_mark_node;
5466 else if (require_constant_elements
5467 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5468 pedwarn ("initializer element is not computable at load time");
5470 /* If this field is empty (and not at the end of structure),
5471 don't do anything other than checking the initializer. */
5473 && (TREE_TYPE (field) == error_mark_node
5474 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5475 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5476 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5477 || TREE_CHAIN (field)))))
5480 value = digest_init (type, value, require_constant_value);
5481 if (value == error_mark_node)
5483 constructor_erroneous = 1;
5487 /* If this element doesn't come next in sequence,
5488 put it on constructor_pending_elts. */
5489 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5490 && (!constructor_incremental
5491 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5493 if (constructor_incremental
5494 && tree_int_cst_lt (field, constructor_unfilled_index))
5495 set_nonincremental_init ();
5497 add_pending_init (field, value);
5500 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5501 && (!constructor_incremental
5502 || field != constructor_unfilled_fields))
5504 /* We do this for records but not for unions. In a union,
5505 no matter which field is specified, it can be initialized
5506 right away since it starts at the beginning of the union. */
5507 if (constructor_incremental)
5509 if (!constructor_unfilled_fields)
5510 set_nonincremental_init ();
5513 tree bitpos, unfillpos;
5515 bitpos = bit_position (field);
5516 unfillpos = bit_position (constructor_unfilled_fields);
5518 if (tree_int_cst_lt (bitpos, unfillpos))
5519 set_nonincremental_init ();
5523 add_pending_init (field, value);
5526 else if (TREE_CODE (constructor_type) == UNION_TYPE
5527 && constructor_elements)
5529 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5530 warning_init ("initialized field with side-effects overwritten");
5532 /* We can have just one union field set. */
5533 constructor_elements = 0;
5536 /* Otherwise, output this element either to
5537 constructor_elements or to the assembler file. */
5539 if (field && TREE_CODE (field) == INTEGER_CST)
5540 field = copy_node (field);
5541 constructor_elements
5542 = tree_cons (field, value, constructor_elements);
5544 /* Advance the variable that indicates sequential elements output. */
5545 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5546 constructor_unfilled_index
5547 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5549 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5551 constructor_unfilled_fields
5552 = TREE_CHAIN (constructor_unfilled_fields);
5554 /* Skip any nameless bit fields. */
5555 while (constructor_unfilled_fields != 0
5556 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5557 && DECL_NAME (constructor_unfilled_fields) == 0)
5558 constructor_unfilled_fields =
5559 TREE_CHAIN (constructor_unfilled_fields);
5561 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5562 constructor_unfilled_fields = 0;
5564 /* Now output any pending elements which have become next. */
5566 output_pending_init_elements (0);
5569 /* Output any pending elements which have become next.
5570 As we output elements, constructor_unfilled_{fields,index}
5571 advances, which may cause other elements to become next;
5572 if so, they too are output.
5574 If ALL is 0, we return when there are
5575 no more pending elements to output now.
5577 If ALL is 1, we output space as necessary so that
5578 we can output all the pending elements. */
5581 output_pending_init_elements (int all)
5583 struct init_node *elt = constructor_pending_elts;
5588 /* Look through the whole pending tree.
5589 If we find an element that should be output now,
5590 output it. Otherwise, set NEXT to the element
5591 that comes first among those still pending. */
5596 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5598 if (tree_int_cst_equal (elt->purpose,
5599 constructor_unfilled_index))
5600 output_init_element (elt->value,
5601 TREE_TYPE (constructor_type),
5602 constructor_unfilled_index, 0);
5603 else if (tree_int_cst_lt (constructor_unfilled_index,
5606 /* Advance to the next smaller node. */
5611 /* We have reached the smallest node bigger than the
5612 current unfilled index. Fill the space first. */
5613 next = elt->purpose;
5619 /* Advance to the next bigger node. */
5624 /* We have reached the biggest node in a subtree. Find
5625 the parent of it, which is the next bigger node. */
5626 while (elt->parent && elt->parent->right == elt)
5629 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5632 next = elt->purpose;
5638 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5639 || TREE_CODE (constructor_type) == UNION_TYPE)
5641 tree ctor_unfilled_bitpos, elt_bitpos;
5643 /* If the current record is complete we are done. */
5644 if (constructor_unfilled_fields == 0)
5647 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5648 elt_bitpos = bit_position (elt->purpose);
5649 /* We can't compare fields here because there might be empty
5650 fields in between. */
5651 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5653 constructor_unfilled_fields = elt->purpose;
5654 output_init_element (elt->value, TREE_TYPE (elt->purpose),
5657 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5659 /* Advance to the next smaller node. */
5664 /* We have reached the smallest node bigger than the
5665 current unfilled field. Fill the space first. */
5666 next = elt->purpose;
5672 /* Advance to the next bigger node. */
5677 /* We have reached the biggest node in a subtree. Find
5678 the parent of it, which is the next bigger node. */
5679 while (elt->parent && elt->parent->right == elt)
5683 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5684 bit_position (elt->purpose))))
5686 next = elt->purpose;
5694 /* Ordinarily return, but not if we want to output all
5695 and there are elements left. */
5696 if (! (all && next != 0))
5699 /* If it's not incremental, just skip over the gap, so that after
5700 jumping to retry we will output the next successive element. */
5701 if (TREE_CODE (constructor_type) == RECORD_TYPE
5702 || TREE_CODE (constructor_type) == UNION_TYPE)
5703 constructor_unfilled_fields = next;
5704 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5705 constructor_unfilled_index = next;
5707 /* ELT now points to the node in the pending tree with the next
5708 initializer to output. */
5712 /* Add one non-braced element to the current constructor level.
5713 This adjusts the current position within the constructor's type.
5714 This may also start or terminate implicit levels
5715 to handle a partly-braced initializer.
5717 Once this has found the correct level for the new element,
5718 it calls output_init_element. */
5721 process_init_element (tree value)
5723 tree orig_value = value;
5724 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5726 designator_depth = 0;
5727 designator_errorneous = 0;
5729 /* Handle superfluous braces around string cst as in
5730 char x[] = {"foo"}; */
5733 && TREE_CODE (constructor_type) == ARRAY_TYPE
5734 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5735 && integer_zerop (constructor_unfilled_index))
5737 if (constructor_stack->replacement_value)
5738 error_init ("excess elements in char array initializer");
5739 constructor_stack->replacement_value = value;
5743 if (constructor_stack->replacement_value != 0)
5745 error_init ("excess elements in struct initializer");
5749 /* Ignore elements of a brace group if it is entirely superfluous
5750 and has already been diagnosed. */
5751 if (constructor_type == 0)
5754 /* If we've exhausted any levels that didn't have braces,
5756 while (constructor_stack->implicit)
5758 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5759 || TREE_CODE (constructor_type) == UNION_TYPE)
5760 && constructor_fields == 0)
5761 process_init_element (pop_init_level (1));
5762 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5763 && (constructor_max_index == 0
5764 || tree_int_cst_lt (constructor_max_index,
5765 constructor_index)))
5766 process_init_element (pop_init_level (1));
5771 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5772 if (constructor_range_stack)
5774 /* If value is a compound literal and we'll be just using its
5775 content, don't put it into a SAVE_EXPR. */
5776 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5777 || !require_constant_value
5779 value = save_expr (value);
5784 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5787 enum tree_code fieldcode;
5789 if (constructor_fields == 0)
5791 pedwarn_init ("excess elements in struct initializer");
5795 fieldtype = TREE_TYPE (constructor_fields);
5796 if (fieldtype != error_mark_node)
5797 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5798 fieldcode = TREE_CODE (fieldtype);
5800 /* Error for non-static initialization of a flexible array member. */
5801 if (fieldcode == ARRAY_TYPE
5802 && !require_constant_value
5803 && TYPE_SIZE (fieldtype) == NULL_TREE
5804 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5806 error_init ("non-static initialization of a flexible array member");
5810 /* Accept a string constant to initialize a subarray. */
5812 && fieldcode == ARRAY_TYPE
5813 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5816 /* Otherwise, if we have come to a subaggregate,
5817 and we don't have an element of its type, push into it. */
5818 else if (value != 0 && !constructor_no_implicit
5819 && value != error_mark_node
5820 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5821 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5822 || fieldcode == UNION_TYPE))
5824 push_init_level (1);
5830 push_member_name (constructor_fields);
5831 output_init_element (value, fieldtype, constructor_fields, 1);
5832 RESTORE_SPELLING_DEPTH (constructor_depth);
5835 /* Do the bookkeeping for an element that was
5836 directly output as a constructor. */
5838 /* For a record, keep track of end position of last field. */
5839 if (DECL_SIZE (constructor_fields))
5840 constructor_bit_index
5841 = size_binop (PLUS_EXPR,
5842 bit_position (constructor_fields),
5843 DECL_SIZE (constructor_fields));
5845 /* If the current field was the first one not yet written out,
5846 it isn't now, so update. */
5847 if (constructor_unfilled_fields == constructor_fields)
5849 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5850 /* Skip any nameless bit fields. */
5851 while (constructor_unfilled_fields != 0
5852 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5853 && DECL_NAME (constructor_unfilled_fields) == 0)
5854 constructor_unfilled_fields =
5855 TREE_CHAIN (constructor_unfilled_fields);
5859 constructor_fields = TREE_CHAIN (constructor_fields);
5860 /* Skip any nameless bit fields at the beginning. */
5861 while (constructor_fields != 0
5862 && DECL_C_BIT_FIELD (constructor_fields)
5863 && DECL_NAME (constructor_fields) == 0)
5864 constructor_fields = TREE_CHAIN (constructor_fields);
5866 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5869 enum tree_code fieldcode;
5871 if (constructor_fields == 0)
5873 pedwarn_init ("excess elements in union initializer");
5877 fieldtype = TREE_TYPE (constructor_fields);
5878 if (fieldtype != error_mark_node)
5879 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5880 fieldcode = TREE_CODE (fieldtype);
5882 /* Warn that traditional C rejects initialization of unions.
5883 We skip the warning if the value is zero. This is done
5884 under the assumption that the zero initializer in user
5885 code appears conditioned on e.g. __STDC__ to avoid
5886 "missing initializer" warnings and relies on default
5887 initialization to zero in the traditional C case.
5888 We also skip the warning if the initializer is designated,
5889 again on the assumption that this must be conditional on
5890 __STDC__ anyway (and we've already complained about the
5891 member-designator already). */
5892 if (warn_traditional && !in_system_header && !constructor_designated
5893 && !(value && (integer_zerop (value) || real_zerop (value))))
5894 warning ("traditional C rejects initialization of unions");
5896 /* Accept a string constant to initialize a subarray. */
5898 && fieldcode == ARRAY_TYPE
5899 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5902 /* Otherwise, if we have come to a subaggregate,
5903 and we don't have an element of its type, push into it. */
5904 else if (value != 0 && !constructor_no_implicit
5905 && value != error_mark_node
5906 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5907 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5908 || fieldcode == UNION_TYPE))
5910 push_init_level (1);
5916 push_member_name (constructor_fields);
5917 output_init_element (value, fieldtype, constructor_fields, 1);
5918 RESTORE_SPELLING_DEPTH (constructor_depth);
5921 /* Do the bookkeeping for an element that was
5922 directly output as a constructor. */
5924 constructor_bit_index = DECL_SIZE (constructor_fields);
5925 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5928 constructor_fields = 0;
5930 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5932 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5933 enum tree_code eltcode = TREE_CODE (elttype);
5935 /* Accept a string constant to initialize a subarray. */
5937 && eltcode == ARRAY_TYPE
5938 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
5941 /* Otherwise, if we have come to a subaggregate,
5942 and we don't have an element of its type, push into it. */
5943 else if (value != 0 && !constructor_no_implicit
5944 && value != error_mark_node
5945 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
5946 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
5947 || eltcode == UNION_TYPE))
5949 push_init_level (1);
5953 if (constructor_max_index != 0
5954 && (tree_int_cst_lt (constructor_max_index, constructor_index)
5955 || integer_all_onesp (constructor_max_index)))
5957 pedwarn_init ("excess elements in array initializer");
5961 /* Now output the actual element. */
5964 push_array_bounds (tree_low_cst (constructor_index, 0));
5965 output_init_element (value, elttype, constructor_index, 1);
5966 RESTORE_SPELLING_DEPTH (constructor_depth);
5970 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5973 /* If we are doing the bookkeeping for an element that was
5974 directly output as a constructor, we must update
5975 constructor_unfilled_index. */
5976 constructor_unfilled_index = constructor_index;
5978 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5980 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5982 /* Do a basic check of initializer size. Note that vectors
5983 always have a fixed size derived from their type. */
5984 if (tree_int_cst_lt (constructor_max_index, constructor_index))
5986 pedwarn_init ("excess elements in vector initializer");
5990 /* Now output the actual element. */
5992 output_init_element (value, elttype, constructor_index, 1);
5995 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5998 /* If we are doing the bookkeeping for an element that was
5999 directly output as a constructor, we must update
6000 constructor_unfilled_index. */
6001 constructor_unfilled_index = constructor_index;
6004 /* Handle the sole element allowed in a braced initializer
6005 for a scalar variable. */
6006 else if (constructor_fields == 0)
6008 pedwarn_init ("excess elements in scalar initializer");
6014 output_init_element (value, constructor_type, NULL_TREE, 1);
6015 constructor_fields = 0;
6018 /* Handle range initializers either at this level or anywhere higher
6019 in the designator stack. */
6020 if (constructor_range_stack)
6022 struct constructor_range_stack *p, *range_stack;
6025 range_stack = constructor_range_stack;
6026 constructor_range_stack = 0;
6027 while (constructor_stack != range_stack->stack)
6029 if (!constructor_stack->implicit)
6031 process_init_element (pop_init_level (1));
6033 for (p = range_stack;
6034 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6037 if (!constructor_stack->implicit)
6039 process_init_element (pop_init_level (1));
6042 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6043 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6048 constructor_index = p->index;
6049 constructor_fields = p->fields;
6050 if (finish && p->range_end && p->index == p->range_start)
6058 push_init_level (2);
6059 p->stack = constructor_stack;
6060 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6061 p->index = p->range_start;
6065 constructor_range_stack = range_stack;
6072 constructor_range_stack = 0;
6075 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6076 (guaranteed to be 'volatile' or null) and ARGS (represented using
6077 an ASM_EXPR node). */
6079 build_asm_stmt (tree cv_qualifier, tree args)
6081 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6082 ASM_VOLATILE_P (args) = 1;
6083 return add_stmt (args);
6086 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6087 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6088 SIMPLE indicates whether there was anything at all after the
6089 string in the asm expression -- asm("blah") and asm("blah" : )
6090 are subtly different. We use a ASM_EXPR node to represent this. */
6092 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6098 const char *constraint;
6099 bool allows_mem, allows_reg, is_inout;
6103 ninputs = list_length (inputs);
6104 noutputs = list_length (outputs);
6106 /* Remove output conversions that change the type but not the mode. */
6107 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6109 tree output = TREE_VALUE (tail);
6110 STRIP_NOPS (output);
6111 TREE_VALUE (tail) = output;
6112 lvalue_or_else (output, "invalid lvalue in asm statement");
6114 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6116 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
6117 &allows_mem, &allows_reg, &is_inout))
6119 /* By marking this operand as erroneous, we will not try
6120 to process this operand again in expand_asm_operands. */
6121 TREE_VALUE (tail) = error_mark_node;
6125 /* If the operand is a DECL that is going to end up in
6126 memory, assume it is addressable. This is a bit more
6127 conservative than it would ideally be; the exact test is
6128 buried deep in expand_asm_operands and depends on the
6129 DECL_RTL for the OPERAND -- which we don't have at this
6131 if (!allows_reg && DECL_P (output))
6132 c_mark_addressable (output);
6135 /* Perform default conversions on array and function inputs.
6136 Don't do this for other types as it would screw up operands
6137 expected to be in memory. */
6138 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6139 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6141 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6143 /* Simple asm statements are treated as volatile. */
6146 ASM_VOLATILE_P (args) = 1;
6147 ASM_INPUT_P (args) = 1;
6152 /* Generate a goto statement to LABEL. */
6155 c_finish_goto_label (tree label)
6157 tree decl = lookup_label (label);
6161 TREE_USED (decl) = 1;
6162 return add_stmt (build (GOTO_EXPR, void_type_node, decl));
6165 /* Generate a computed goto statement to EXPR. */
6168 c_finish_goto_ptr (tree expr)
6171 pedwarn ("ISO C forbids `goto *expr;'");
6172 expr = convert (ptr_type_node, expr);
6173 return add_stmt (build (GOTO_EXPR, void_type_node, expr));
6176 /* Generate a C `return' statement. RETVAL is the expression for what
6177 to return, or a null pointer for `return;' with no value. */
6180 c_finish_return (tree retval)
6182 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6184 if (TREE_THIS_VOLATILE (current_function_decl))
6185 warning ("function declared `noreturn' has a `return' statement");
6189 current_function_returns_null = 1;
6190 if ((warn_return_type || flag_isoc99)
6191 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6192 pedwarn_c99 ("`return' with no value, in function returning non-void");
6194 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6196 current_function_returns_null = 1;
6197 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6198 pedwarn ("`return' with a value, in function returning void");
6202 tree t = convert_for_assignment (valtype, retval, _("return"),
6203 NULL_TREE, NULL_TREE, 0);
6204 tree res = DECL_RESULT (current_function_decl);
6207 current_function_returns_value = 1;
6208 if (t == error_mark_node)
6211 inner = t = convert (TREE_TYPE (res), t);
6213 /* Strip any conversions, additions, and subtractions, and see if
6214 we are returning the address of a local variable. Warn if so. */
6217 switch (TREE_CODE (inner))
6219 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6221 inner = TREE_OPERAND (inner, 0);
6225 /* If the second operand of the MINUS_EXPR has a pointer
6226 type (or is converted from it), this may be valid, so
6227 don't give a warning. */
6229 tree op1 = TREE_OPERAND (inner, 1);
6231 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6232 && (TREE_CODE (op1) == NOP_EXPR
6233 || TREE_CODE (op1) == NON_LVALUE_EXPR
6234 || TREE_CODE (op1) == CONVERT_EXPR))
6235 op1 = TREE_OPERAND (op1, 0);
6237 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6240 inner = TREE_OPERAND (inner, 0);
6245 inner = TREE_OPERAND (inner, 0);
6247 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6248 inner = TREE_OPERAND (inner, 0);
6251 && ! DECL_EXTERNAL (inner)
6252 && ! TREE_STATIC (inner)
6253 && DECL_CONTEXT (inner) == current_function_decl)
6254 warning ("function returns address of local variable");
6264 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6267 return add_stmt (build_stmt (RETURN_EXPR, retval));
6271 /* The SWITCH_STMT being built. */
6274 /* The original type of the testing expression, ie. before the
6275 default conversion is applied. */
6278 /* A splay-tree mapping the low element of a case range to the high
6279 element, or NULL_TREE if there is no high element. Used to
6280 determine whether or not a new case label duplicates an old case
6281 label. We need a tree, rather than simply a hash table, because
6282 of the GNU case range extension. */
6285 /* The next node on the stack. */
6286 struct c_switch *next;
6289 /* A stack of the currently active switch statements. The innermost
6290 switch statement is on the top of the stack. There is no need to
6291 mark the stack for garbage collection because it is only active
6292 during the processing of the body of a function, and we never
6293 collect at that point. */
6295 struct c_switch *c_switch_stack;
6297 /* Start a C switch statement, testing expression EXP. Return the new
6301 c_start_case (tree exp)
6303 enum tree_code code;
6304 tree type, orig_type = error_mark_node;
6305 struct c_switch *cs;
6307 if (exp != error_mark_node)
6309 code = TREE_CODE (TREE_TYPE (exp));
6310 orig_type = TREE_TYPE (exp);
6312 if (! INTEGRAL_TYPE_P (orig_type)
6313 && code != ERROR_MARK)
6315 error ("switch quantity not an integer");
6316 exp = integer_zero_node;
6320 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6322 if (warn_traditional && !in_system_header
6323 && (type == long_integer_type_node
6324 || type == long_unsigned_type_node))
6325 warning ("`long' switch expression not converted to `int' in ISO C");
6327 exp = default_conversion (exp);
6328 type = TREE_TYPE (exp);
6332 /* Add this new SWITCH_STMT to the stack. */
6333 cs = xmalloc (sizeof (*cs));
6334 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6335 cs->orig_type = orig_type;
6336 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6337 cs->next = c_switch_stack;
6338 c_switch_stack = cs;
6340 return add_stmt (cs->switch_stmt);
6343 /* Process a case label. */
6346 do_case (tree low_value, tree high_value)
6348 tree label = NULL_TREE;
6352 label = c_add_case_label (c_switch_stack->cases,
6353 SWITCH_COND (c_switch_stack->switch_stmt),
6354 c_switch_stack->orig_type,
6355 low_value, high_value);
6356 if (label == error_mark_node)
6360 error ("case label not within a switch statement");
6362 error ("`default' label not within a switch statement");
6367 /* Finish the switch statement. */
6370 c_finish_case (tree body)
6372 struct c_switch *cs = c_switch_stack;
6374 SWITCH_BODY (cs->switch_stmt) = body;
6376 /* Emit warnings as needed. */
6377 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6379 /* Pop the stack. */
6380 c_switch_stack = cs->next;
6381 splay_tree_delete (cs->cases);
6385 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6386 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6387 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6388 statement, and was not surrounded with parenthesis. */
6391 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6392 tree else_block, bool nested_if)
6396 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6397 if (warn_parentheses && nested_if && else_block == NULL)
6399 tree inner_if = then_block;
6401 /* We know from the grammar productions that there is an IF nested
6402 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6403 it might not be exactly THEN_BLOCK, but should be the last
6404 non-container statement within. */
6406 switch (TREE_CODE (inner_if))
6411 inner_if = BIND_EXPR_BODY (inner_if);
6413 case STATEMENT_LIST:
6414 inner_if = expr_last (then_block);
6416 case TRY_FINALLY_EXPR:
6417 case TRY_CATCH_EXPR:
6418 inner_if = TREE_OPERAND (inner_if, 0);
6425 if (COND_EXPR_ELSE (inner_if))
6426 warning ("%Hsuggest explicit braces to avoid ambiguous `else'",
6430 /* Diagnose ";" via the special empty statement node that we create. */
6433 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6436 warning ("%Hempty body in an if-statement",
6437 EXPR_LOCUS (then_block));
6438 then_block = alloc_stmt_list ();
6441 && TREE_CODE (else_block) == NOP_EXPR
6442 && !TREE_TYPE (else_block))
6444 warning ("%Hempty body in an else-statement",
6445 EXPR_LOCUS (else_block));
6446 else_block = alloc_stmt_list ();
6450 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6451 SET_EXPR_LOCATION (stmt, if_locus);
6455 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6456 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6457 is false for DO loops. INCR is the FOR increment expression. BODY is
6458 the statement controlled by the loop. BLAB is the break label. CLAB is
6459 the continue label. Everything is allowed to be NULL. */
6462 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6463 tree blab, tree clab, bool cond_is_first)
6465 tree entry = NULL, exit = NULL, t;
6467 /* Detect do { ... } while (0) and don't generate loop construct. */
6468 if (cond && !cond_is_first && integer_zerop (cond))
6470 if (cond_is_first || cond)
6472 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6474 /* If we have an exit condition, then we build an IF with gotos either
6475 out of the loop, or to the top of it. If there's no exit condition,
6476 then we just build a jump back to the top. */
6477 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6481 /* Canonicalize the loop condition to the end. This means
6482 generating a branch to the loop condition. Reuse the
6483 continue label, if possible. */
6488 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6489 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6492 t = build1 (GOTO_EXPR, void_type_node, clab);
6493 SET_EXPR_LOCATION (t, start_locus);
6497 t = build_and_jump (&blab);
6498 exit = build (COND_EXPR, void_type_node, cond, exit, t);
6501 SET_EXPR_LOCATION (exit, start_locus);
6503 SET_EXPR_LOCATION (exit, input_location);
6512 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6520 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
6524 c_finish_bc_stmt (tree *label_p, bool is_break)
6526 tree label = *label_p;
6529 *label_p = label = create_artificial_label ();
6530 else if (TREE_CODE (label) != LABEL_DECL)
6533 error ("break statement not within loop or switch");
6535 error ("continue statement not within a loop");
6539 return add_stmt (build (GOTO_EXPR, void_type_node, label));
6542 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6545 emit_side_effect_warnings (tree expr)
6547 if (expr == error_mark_node)
6549 else if (!TREE_SIDE_EFFECTS (expr))
6551 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6552 warning ("%Hstatement with no effect",
6553 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
6555 else if (warn_unused_value)
6556 warn_if_unused_value (expr, input_location);
6559 /* Process an expression as if it were a complete statement. Emit
6560 diagnostics, but do not call ADD_STMT. */
6563 c_process_expr_stmt (tree expr)
6568 /* Do default conversion if safe and possibly important,
6569 in case within ({...}). */
6570 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
6571 && (flag_isoc99 || lvalue_p (expr)))
6572 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
6573 expr = default_conversion (expr);
6575 if (warn_sequence_point)
6576 verify_sequence_points (expr);
6578 if (TREE_TYPE (expr) != error_mark_node
6579 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
6580 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
6581 error ("expression statement has incomplete type");
6583 /* If we're not processing a statement expression, warn about unused values.
6584 Warnings for statement expressions will be emitted later, once we figure
6585 out which is the result. */
6586 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6587 && (extra_warnings || warn_unused_value))
6588 emit_side_effect_warnings (expr);
6590 /* If the expression is not of a type to which we cannot assign a line
6591 number, wrap the thing in a no-op NOP_EXPR. */
6592 if (DECL_P (expr) || TREE_CODE_CLASS (TREE_CODE (expr)) == 'c')
6593 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
6596 SET_EXPR_LOCATION (expr, input_location);
6601 /* Emit an expression as a statement. */
6604 c_finish_expr_stmt (tree expr)
6607 return add_stmt (c_process_expr_stmt (expr));
6612 /* Do the opposite and emit a statement as an expression. To begin,
6613 create a new binding level and return it. */
6616 c_begin_stmt_expr (void)
6620 /* We must force a BLOCK for this level so that, if it is not expanded
6621 later, there is a way to turn off the entire subtree of blocks that
6622 are contained in it. */
6624 ret = c_begin_compound_stmt (true);
6626 /* Mark the current statement list as belonging to a statement list. */
6627 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6633 c_finish_stmt_expr (tree body)
6635 tree last, type, tmp, val;
6638 body = c_end_compound_stmt (body, true);
6640 /* Locate the last statement in BODY. See c_end_compound_stmt
6641 about always returning a BIND_EXPR. */
6642 last_p = &BIND_EXPR_BODY (body);
6643 last = BIND_EXPR_BODY (body);
6646 if (TREE_CODE (last) == STATEMENT_LIST)
6648 tree_stmt_iterator i;
6650 /* This can happen with degenerate cases like ({ }). No value. */
6651 if (!TREE_SIDE_EFFECTS (last))
6654 /* If we're supposed to generate side effects warnings, process
6655 all of the statements except the last. */
6656 if (extra_warnings || warn_unused_value)
6658 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
6659 emit_side_effect_warnings (tsi_stmt (i));
6662 i = tsi_last (last);
6663 last_p = tsi_stmt_ptr (i);
6667 /* If the end of the list is exception related, then the list was split
6668 by a call to push_cleanup. Continue searching. */
6669 if (TREE_CODE (last) == TRY_FINALLY_EXPR
6670 || TREE_CODE (last) == TRY_CATCH_EXPR)
6672 last_p = &TREE_OPERAND (last, 0);
6674 goto continue_searching;
6677 /* In the case that the BIND_EXPR is not necessary, return the
6678 expression out from inside it. */
6679 if (last == error_mark_node
6680 || (last == BIND_EXPR_BODY (body)
6681 && BIND_EXPR_VARS (body) == NULL))
6684 /* Extract the type of said expression. */
6685 type = TREE_TYPE (last);
6687 /* If we're not returning a value at all, then the BIND_EXPR that
6688 we already have is a fine expression to return. */
6689 if (!type || VOID_TYPE_P (type))
6692 /* Now that we've located the expression containing the value, it seems
6693 silly to make voidify_wrapper_expr repeat the process. Create a
6694 temporary of the appropriate type and stick it in a TARGET_EXPR. */
6695 tmp = create_tmp_var_raw (type, NULL);
6697 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
6698 tree_expr_nonnegative_p giving up immediately. */
6700 if (TREE_CODE (val) == NOP_EXPR
6701 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
6702 val = TREE_OPERAND (val, 0);
6704 *last_p = build (MODIFY_EXPR, void_type_node, tmp, val);
6705 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
6707 return build (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
6710 /* Begin and end compound statements. This is as simple as pushing
6711 and popping new statement lists from the tree. */
6714 c_begin_compound_stmt (bool do_scope)
6716 tree stmt = push_stmt_list ();
6723 c_end_compound_stmt (tree stmt, bool do_scope)
6729 if (c_dialect_objc ())
6730 objc_clear_super_receiver ();
6731 block = pop_scope ();
6734 stmt = pop_stmt_list (stmt);
6735 stmt = c_build_bind_expr (block, stmt);
6737 /* If this compound statement is nested immediately inside a statement
6738 expression, then force a BIND_EXPR to be created. Otherwise we'll
6739 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
6740 STATEMENT_LISTs merge, and thus we can lose track of what statement
6743 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6744 && TREE_CODE (stmt) != BIND_EXPR)
6746 stmt = build (BIND_EXPR, void_type_node, NULL, stmt, NULL);
6747 TREE_SIDE_EFFECTS (stmt) = 1;
6753 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
6754 when the current scope is exited. EH_ONLY is true when this is not
6755 meant to apply to normal control flow transfer. */
6758 push_cleanup (tree decl ATTRIBUTE_UNUSED, tree cleanup, bool eh_only)
6760 enum tree_code code;
6764 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
6765 stmt = build_stmt (code, NULL, cleanup);
6767 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
6768 list = push_stmt_list ();
6769 TREE_OPERAND (stmt, 0) = list;
6770 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
6773 /* Build a binary-operation expression without default conversions.
6774 CODE is the kind of expression to build.
6775 This function differs from `build' in several ways:
6776 the data type of the result is computed and recorded in it,
6777 warnings are generated if arg data types are invalid,
6778 special handling for addition and subtraction of pointers is known,
6779 and some optimization is done (operations on narrow ints
6780 are done in the narrower type when that gives the same result).
6781 Constant folding is also done before the result is returned.
6783 Note that the operands will never have enumeral types, or function
6784 or array types, because either they will have the default conversions
6785 performed or they have both just been converted to some other type in which
6786 the arithmetic is to be done. */
6789 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6793 enum tree_code code0, code1;
6796 /* Expression code to give to the expression when it is built.
6797 Normally this is CODE, which is what the caller asked for,
6798 but in some special cases we change it. */
6799 enum tree_code resultcode = code;
6801 /* Data type in which the computation is to be performed.
6802 In the simplest cases this is the common type of the arguments. */
6803 tree result_type = NULL;
6805 /* Nonzero means operands have already been type-converted
6806 in whatever way is necessary.
6807 Zero means they need to be converted to RESULT_TYPE. */
6810 /* Nonzero means create the expression with this type, rather than
6812 tree build_type = 0;
6814 /* Nonzero means after finally constructing the expression
6815 convert it to this type. */
6816 tree final_type = 0;
6818 /* Nonzero if this is an operation like MIN or MAX which can
6819 safely be computed in short if both args are promoted shorts.
6820 Also implies COMMON.
6821 -1 indicates a bitwise operation; this makes a difference
6822 in the exact conditions for when it is safe to do the operation
6823 in a narrower mode. */
6826 /* Nonzero if this is a comparison operation;
6827 if both args are promoted shorts, compare the original shorts.
6828 Also implies COMMON. */
6829 int short_compare = 0;
6831 /* Nonzero if this is a right-shift operation, which can be computed on the
6832 original short and then promoted if the operand is a promoted short. */
6833 int short_shift = 0;
6835 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6840 op0 = default_conversion (orig_op0);
6841 op1 = default_conversion (orig_op1);
6849 type0 = TREE_TYPE (op0);
6850 type1 = TREE_TYPE (op1);
6852 /* The expression codes of the data types of the arguments tell us
6853 whether the arguments are integers, floating, pointers, etc. */
6854 code0 = TREE_CODE (type0);
6855 code1 = TREE_CODE (type1);
6857 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6858 STRIP_TYPE_NOPS (op0);
6859 STRIP_TYPE_NOPS (op1);
6861 /* If an error was already reported for one of the arguments,
6862 avoid reporting another error. */
6864 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6865 return error_mark_node;
6870 /* Handle the pointer + int case. */
6871 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6872 return pointer_int_sum (PLUS_EXPR, op0, op1);
6873 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6874 return pointer_int_sum (PLUS_EXPR, op1, op0);
6880 /* Subtraction of two similar pointers.
6881 We must subtract them as integers, then divide by object size. */
6882 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6883 && comp_target_types (type0, type1, 1))
6884 return pointer_diff (op0, op1);
6885 /* Handle pointer minus int. Just like pointer plus int. */
6886 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6887 return pointer_int_sum (MINUS_EXPR, op0, op1);
6896 case TRUNC_DIV_EXPR:
6898 case FLOOR_DIV_EXPR:
6899 case ROUND_DIV_EXPR:
6900 case EXACT_DIV_EXPR:
6901 /* Floating point division by zero is a legitimate way to obtain
6902 infinities and NaNs. */
6903 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6904 warning ("division by zero");
6906 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6907 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6908 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6909 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6911 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6912 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
6913 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
6914 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
6916 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6917 resultcode = RDIV_EXPR;
6919 /* Although it would be tempting to shorten always here, that
6920 loses on some targets, since the modulo instruction is
6921 undefined if the quotient can't be represented in the
6922 computation mode. We shorten only if unsigned or if
6923 dividing by something we know != -1. */
6924 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
6925 || (TREE_CODE (op1) == INTEGER_CST
6926 && ! integer_all_onesp (op1)));
6934 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6936 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6940 case TRUNC_MOD_EXPR:
6941 case FLOOR_MOD_EXPR:
6942 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6943 warning ("division by zero");
6945 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6947 /* Although it would be tempting to shorten always here, that loses
6948 on some targets, since the modulo instruction is undefined if the
6949 quotient can't be represented in the computation mode. We shorten
6950 only if unsigned or if dividing by something we know != -1. */
6951 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
6952 || (TREE_CODE (op1) == INTEGER_CST
6953 && ! integer_all_onesp (op1)));
6958 case TRUTH_ANDIF_EXPR:
6959 case TRUTH_ORIF_EXPR:
6960 case TRUTH_AND_EXPR:
6962 case TRUTH_XOR_EXPR:
6963 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6964 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6965 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6966 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6968 /* Result of these operations is always an int,
6969 but that does not mean the operands should be
6970 converted to ints! */
6971 result_type = integer_type_node;
6972 op0 = lang_hooks.truthvalue_conversion (op0);
6973 op1 = lang_hooks.truthvalue_conversion (op1);
6978 /* Shift operations: result has same type as first operand;
6979 always convert second operand to int.
6980 Also set SHORT_SHIFT if shifting rightward. */
6983 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6985 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6987 if (tree_int_cst_sgn (op1) < 0)
6988 warning ("right shift count is negative");
6991 if (! integer_zerop (op1))
6994 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6995 warning ("right shift count >= width of type");
6999 /* Use the type of the value to be shifted. */
7000 result_type = type0;
7001 /* Convert the shift-count to an integer, regardless of size
7002 of value being shifted. */
7003 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7004 op1 = convert (integer_type_node, op1);
7005 /* Avoid converting op1 to result_type later. */
7011 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7013 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7015 if (tree_int_cst_sgn (op1) < 0)
7016 warning ("left shift count is negative");
7018 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7019 warning ("left shift count >= width of type");
7022 /* Use the type of the value to be shifted. */
7023 result_type = type0;
7024 /* Convert the shift-count to an integer, regardless of size
7025 of value being shifted. */
7026 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7027 op1 = convert (integer_type_node, op1);
7028 /* Avoid converting op1 to result_type later. */
7035 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7037 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7039 if (tree_int_cst_sgn (op1) < 0)
7040 warning ("shift count is negative");
7041 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7042 warning ("shift count >= width of type");
7045 /* Use the type of the value to be shifted. */
7046 result_type = type0;
7047 /* Convert the shift-count to an integer, regardless of size
7048 of value being shifted. */
7049 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7050 op1 = convert (integer_type_node, op1);
7051 /* Avoid converting op1 to result_type later. */
7058 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7059 warning ("comparing floating point with == or != is unsafe");
7060 /* Result of comparison is always int,
7061 but don't convert the args to int! */
7062 build_type = integer_type_node;
7063 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7064 || code0 == COMPLEX_TYPE)
7065 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7066 || code1 == COMPLEX_TYPE))
7068 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7070 tree tt0 = TREE_TYPE (type0);
7071 tree tt1 = TREE_TYPE (type1);
7072 /* Anything compares with void *. void * compares with anything.
7073 Otherwise, the targets must be compatible
7074 and both must be object or both incomplete. */
7075 if (comp_target_types (type0, type1, 1))
7076 result_type = common_pointer_type (type0, type1);
7077 else if (VOID_TYPE_P (tt0))
7079 /* op0 != orig_op0 detects the case of something
7080 whose value is 0 but which isn't a valid null ptr const. */
7081 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7082 && TREE_CODE (tt1) == FUNCTION_TYPE)
7083 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
7085 else if (VOID_TYPE_P (tt1))
7087 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7088 && TREE_CODE (tt0) == FUNCTION_TYPE)
7089 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
7092 pedwarn ("comparison of distinct pointer types lacks a cast");
7094 if (result_type == NULL_TREE)
7095 result_type = ptr_type_node;
7097 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7098 && integer_zerop (op1))
7099 result_type = type0;
7100 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7101 && integer_zerop (op0))
7102 result_type = type1;
7103 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7105 result_type = type0;
7106 pedwarn ("comparison between pointer and integer");
7108 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7110 result_type = type1;
7111 pedwarn ("comparison between pointer and integer");
7117 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7118 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7120 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7122 if (comp_target_types (type0, type1, 1))
7124 result_type = common_pointer_type (type0, type1);
7126 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7127 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7131 result_type = ptr_type_node;
7132 pedwarn ("comparison of distinct pointer types lacks a cast");
7141 build_type = integer_type_node;
7142 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7143 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7145 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7147 if (comp_target_types (type0, type1, 1))
7149 result_type = common_pointer_type (type0, type1);
7150 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7151 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7152 pedwarn ("comparison of complete and incomplete pointers");
7154 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7155 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7159 result_type = ptr_type_node;
7160 pedwarn ("comparison of distinct pointer types lacks a cast");
7163 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7164 && integer_zerop (op1))
7166 result_type = type0;
7167 if (pedantic || extra_warnings)
7168 pedwarn ("ordered comparison of pointer with integer zero");
7170 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7171 && integer_zerop (op0))
7173 result_type = type1;
7175 pedwarn ("ordered comparison of pointer with integer zero");
7177 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7179 result_type = type0;
7180 pedwarn ("comparison between pointer and integer");
7182 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7184 result_type = type1;
7185 pedwarn ("comparison between pointer and integer");
7189 case UNORDERED_EXPR:
7197 build_type = integer_type_node;
7198 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
7200 error ("unordered comparison on non-floating point argument");
7201 return error_mark_node;
7210 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7211 return error_mark_node;
7213 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7214 || code0 == VECTOR_TYPE)
7216 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7217 || code1 == VECTOR_TYPE))
7219 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7221 if (shorten || common || short_compare)
7222 result_type = common_type (type0, type1);
7224 /* For certain operations (which identify themselves by shorten != 0)
7225 if both args were extended from the same smaller type,
7226 do the arithmetic in that type and then extend.
7228 shorten !=0 and !=1 indicates a bitwise operation.
7229 For them, this optimization is safe only if
7230 both args are zero-extended or both are sign-extended.
7231 Otherwise, we might change the result.
7232 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7233 but calculated in (unsigned short) it would be (unsigned short)-1. */
7235 if (shorten && none_complex)
7237 int unsigned0, unsigned1;
7238 tree arg0 = get_narrower (op0, &unsigned0);
7239 tree arg1 = get_narrower (op1, &unsigned1);
7240 /* UNS is 1 if the operation to be done is an unsigned one. */
7241 int uns = TYPE_UNSIGNED (result_type);
7244 final_type = result_type;
7246 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7247 but it *requires* conversion to FINAL_TYPE. */
7249 if ((TYPE_PRECISION (TREE_TYPE (op0))
7250 == TYPE_PRECISION (TREE_TYPE (arg0)))
7251 && TREE_TYPE (op0) != final_type)
7252 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7253 if ((TYPE_PRECISION (TREE_TYPE (op1))
7254 == TYPE_PRECISION (TREE_TYPE (arg1)))
7255 && TREE_TYPE (op1) != final_type)
7256 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7258 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7260 /* For bitwise operations, signedness of nominal type
7261 does not matter. Consider only how operands were extended. */
7265 /* Note that in all three cases below we refrain from optimizing
7266 an unsigned operation on sign-extended args.
7267 That would not be valid. */
7269 /* Both args variable: if both extended in same way
7270 from same width, do it in that width.
7271 Do it unsigned if args were zero-extended. */
7272 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7273 < TYPE_PRECISION (result_type))
7274 && (TYPE_PRECISION (TREE_TYPE (arg1))
7275 == TYPE_PRECISION (TREE_TYPE (arg0)))
7276 && unsigned0 == unsigned1
7277 && (unsigned0 || !uns))
7279 = c_common_signed_or_unsigned_type
7280 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7281 else if (TREE_CODE (arg0) == INTEGER_CST
7282 && (unsigned1 || !uns)
7283 && (TYPE_PRECISION (TREE_TYPE (arg1))
7284 < TYPE_PRECISION (result_type))
7286 = c_common_signed_or_unsigned_type (unsigned1,
7288 int_fits_type_p (arg0, type)))
7290 else if (TREE_CODE (arg1) == INTEGER_CST
7291 && (unsigned0 || !uns)
7292 && (TYPE_PRECISION (TREE_TYPE (arg0))
7293 < TYPE_PRECISION (result_type))
7295 = c_common_signed_or_unsigned_type (unsigned0,
7297 int_fits_type_p (arg1, type)))
7301 /* Shifts can be shortened if shifting right. */
7306 tree arg0 = get_narrower (op0, &unsigned_arg);
7308 final_type = result_type;
7310 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7311 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7313 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7314 /* We can shorten only if the shift count is less than the
7315 number of bits in the smaller type size. */
7316 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7317 /* We cannot drop an unsigned shift after sign-extension. */
7318 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7320 /* Do an unsigned shift if the operand was zero-extended. */
7322 = c_common_signed_or_unsigned_type (unsigned_arg,
7324 /* Convert value-to-be-shifted to that type. */
7325 if (TREE_TYPE (op0) != result_type)
7326 op0 = convert (result_type, op0);
7331 /* Comparison operations are shortened too but differently.
7332 They identify themselves by setting short_compare = 1. */
7336 /* Don't write &op0, etc., because that would prevent op0
7337 from being kept in a register.
7338 Instead, make copies of the our local variables and
7339 pass the copies by reference, then copy them back afterward. */
7340 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7341 enum tree_code xresultcode = resultcode;
7343 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7348 op0 = xop0, op1 = xop1;
7350 resultcode = xresultcode;
7352 if (warn_sign_compare && skip_evaluation == 0)
7354 int op0_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7355 int op1_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7356 int unsignedp0, unsignedp1;
7357 tree primop0 = get_narrower (op0, &unsignedp0);
7358 tree primop1 = get_narrower (op1, &unsignedp1);
7362 STRIP_TYPE_NOPS (xop0);
7363 STRIP_TYPE_NOPS (xop1);
7365 /* Give warnings for comparisons between signed and unsigned
7366 quantities that may fail.
7368 Do the checking based on the original operand trees, so that
7369 casts will be considered, but default promotions won't be.
7371 Do not warn if the comparison is being done in a signed type,
7372 since the signed type will only be chosen if it can represent
7373 all the values of the unsigned type. */
7374 if (! TYPE_UNSIGNED (result_type))
7376 /* Do not warn if both operands are the same signedness. */
7377 else if (op0_signed == op1_signed)
7384 sop = xop0, uop = xop1;
7386 sop = xop1, uop = xop0;
7388 /* Do not warn if the signed quantity is an
7389 unsuffixed integer literal (or some static
7390 constant expression involving such literals or a
7391 conditional expression involving such literals)
7392 and it is non-negative. */
7393 if (tree_expr_nonnegative_p (sop))
7395 /* Do not warn if the comparison is an equality operation,
7396 the unsigned quantity is an integral constant, and it
7397 would fit in the result if the result were signed. */
7398 else if (TREE_CODE (uop) == INTEGER_CST
7399 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7401 (uop, c_common_signed_type (result_type)))
7403 /* Do not warn if the unsigned quantity is an enumeration
7404 constant and its maximum value would fit in the result
7405 if the result were signed. */
7406 else if (TREE_CODE (uop) == INTEGER_CST
7407 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7409 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7410 c_common_signed_type (result_type)))
7413 warning ("comparison between signed and unsigned");
7416 /* Warn if two unsigned values are being compared in a size
7417 larger than their original size, and one (and only one) is the
7418 result of a `~' operator. This comparison will always fail.
7420 Also warn if one operand is a constant, and the constant
7421 does not have all bits set that are set in the ~ operand
7422 when it is extended. */
7424 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7425 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7427 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7428 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7431 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7434 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7437 HOST_WIDE_INT constant, mask;
7438 int unsignedp, bits;
7440 if (host_integerp (primop0, 0))
7443 unsignedp = unsignedp1;
7444 constant = tree_low_cst (primop0, 0);
7449 unsignedp = unsignedp0;
7450 constant = tree_low_cst (primop1, 0);
7453 bits = TYPE_PRECISION (TREE_TYPE (primop));
7454 if (bits < TYPE_PRECISION (result_type)
7455 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7457 mask = (~ (HOST_WIDE_INT) 0) << bits;
7458 if ((mask & constant) != mask)
7459 warning ("comparison of promoted ~unsigned with constant");
7462 else if (unsignedp0 && unsignedp1
7463 && (TYPE_PRECISION (TREE_TYPE (primop0))
7464 < TYPE_PRECISION (result_type))
7465 && (TYPE_PRECISION (TREE_TYPE (primop1))
7466 < TYPE_PRECISION (result_type)))
7467 warning ("comparison of promoted ~unsigned with unsigned");
7473 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7474 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7475 Then the expression will be built.
7476 It will be given type FINAL_TYPE if that is nonzero;
7477 otherwise, it will be given type RESULT_TYPE. */
7481 binary_op_error (code);
7482 return error_mark_node;
7487 if (TREE_TYPE (op0) != result_type)
7488 op0 = convert (result_type, op0);
7489 if (TREE_TYPE (op1) != result_type)
7490 op1 = convert (result_type, op1);
7493 if (build_type == NULL_TREE)
7494 build_type = result_type;
7497 tree result = build (resultcode, build_type, op0, op1);
7499 /* Treat expressions in initializers specially as they can't trap. */
7500 result = require_constant_value ? fold_initializer (result)
7503 if (final_type != 0)
7504 result = convert (final_type, result);
7509 /* Build the result of __builtin_offsetof. TYPE is the first argument to
7510 offsetof, i.e. a type. LIST is a tree_list that encodes component and
7511 array references; PURPOSE is set for the former and VALUE is set for
7515 build_offsetof (tree type, tree list)
7519 /* Build "*(type *)0". */
7520 t = convert (build_pointer_type (type), null_pointer_node);
7521 t = build_indirect_ref (t, "");
7523 /* Build COMPONENT and ARRAY_REF expressions as needed. */
7524 for (list = nreverse (list); list ; list = TREE_CHAIN (list))
7525 if (TREE_PURPOSE (list))
7526 t = build_component_ref (t, TREE_PURPOSE (list));
7528 t = build_array_ref (t, TREE_VALUE (list));
7530 /* Finalize the offsetof expression. For now all we need to do is take
7531 the address of the expression we created, and cast that to an integer
7532 type; this mirrors the traditional macro implementation of offsetof. */
7533 t = build_unary_op (ADDR_EXPR, t, 0);
7534 return convert (size_type_node, t);