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.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
34 #include "coretypes.h"
38 #include "langhooks.h"
49 /* Nonzero if we've already printed a "missing braces around initializer"
50 message within this initializer. */
51 static int missing_braces_mentioned;
53 static int require_constant_value;
54 static int require_constant_elements;
56 static tree qualify_type (tree, tree);
57 static int tagged_types_tu_compatible_p (tree, tree);
58 static int comp_target_types (tree, tree, int);
59 static int function_types_compatible_p (tree, tree);
60 static int type_lists_compatible_p (tree, tree);
61 static tree decl_constant_value_for_broken_optimization (tree);
62 static tree default_function_array_conversion (tree);
63 static tree lookup_field (tree, tree);
64 static tree convert_arguments (tree, tree, tree, tree);
65 static tree pointer_diff (tree, tree);
66 static tree internal_build_compound_expr (tree, int);
67 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
69 static void warn_for_assignment (const char *, const char *, tree, int);
70 static tree valid_compound_expr_initializer (tree, tree);
71 static void push_string (const char *);
72 static void push_member_name (tree);
73 static void push_array_bounds (int);
74 static int spelling_length (void);
75 static char *print_spelling (char *);
76 static void warning_init (const char *);
77 static tree digest_init (tree, tree, int);
78 static void output_init_element (tree, tree, tree, int);
79 static void output_pending_init_elements (int);
80 static int set_designator (int);
81 static void push_range_stack (tree);
82 static void add_pending_init (tree, tree);
83 static void set_nonincremental_init (void);
84 static void set_nonincremental_init_from_string (tree);
85 static tree find_init_member (tree);
86 static int lvalue_or_else (tree, const char *);
88 /* Do `exp = require_complete_type (exp);' to make sure exp
89 does not have an incomplete type. (That includes void types.) */
92 require_complete_type (tree value)
94 tree type = TREE_TYPE (value);
96 if (value == error_mark_node || type == error_mark_node)
97 return error_mark_node;
99 /* First, detect a valid value with a complete type. */
100 if (COMPLETE_TYPE_P (type))
103 c_incomplete_type_error (value, type);
104 return error_mark_node;
107 /* Print an error message for invalid use of an incomplete type.
108 VALUE is the expression that was used (or 0 if that isn't known)
109 and TYPE is the type that was invalid. */
112 c_incomplete_type_error (tree value, tree type)
114 const char *type_code_string;
116 /* Avoid duplicate error message. */
117 if (TREE_CODE (type) == ERROR_MARK)
120 if (value != 0 && (TREE_CODE (value) == VAR_DECL
121 || TREE_CODE (value) == PARM_DECL))
122 error ("`%s' has an incomplete type",
123 IDENTIFIER_POINTER (DECL_NAME (value)));
127 /* We must print an error message. Be clever about what it says. */
129 switch (TREE_CODE (type))
132 type_code_string = "struct";
136 type_code_string = "union";
140 type_code_string = "enum";
144 error ("invalid use of void expression");
148 if (TYPE_DOMAIN (type))
150 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
152 error ("invalid use of flexible array member");
155 type = TREE_TYPE (type);
158 error ("invalid use of array with unspecified bounds");
165 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
166 error ("invalid use of undefined type `%s %s'",
167 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
169 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
170 error ("invalid use of incomplete typedef `%s'",
171 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
175 /* Given a type, apply default promotions wrt unnamed function
176 arguments and return the new type. */
179 c_type_promotes_to (tree type)
181 if (TYPE_MAIN_VARIANT (type) == float_type_node)
182 return double_type_node;
184 if (c_promoting_integer_type_p (type))
186 /* Preserve unsignedness if not really getting any wider. */
187 if (TYPE_UNSIGNED (type)
188 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
189 return unsigned_type_node;
190 return integer_type_node;
196 /* Return a variant of TYPE which has all the type qualifiers of LIKE
197 as well as those of TYPE. */
200 qualify_type (tree type, tree like)
202 return c_build_qualified_type (type,
203 TYPE_QUALS (type) | TYPE_QUALS (like));
206 /* Return the composite type of two compatible types.
208 We assume that comptypes has already been done and returned
209 nonzero; if that isn't so, this may crash. In particular, we
210 assume that qualifiers match. */
213 composite_type (tree t1, tree t2)
215 enum tree_code code1;
216 enum tree_code code2;
219 /* Save time if the two types are the same. */
221 if (t1 == t2) return t1;
223 /* If one type is nonsense, use the other. */
224 if (t1 == error_mark_node)
226 if (t2 == error_mark_node)
229 code1 = TREE_CODE (t1);
230 code2 = TREE_CODE (t2);
232 /* Merge the attributes. */
233 attributes = targetm.merge_type_attributes (t1, t2);
235 /* If one is an enumerated type and the other is the compatible
236 integer type, the composite type might be either of the two
237 (DR#013 question 3). For consistency, use the enumerated type as
238 the composite type. */
240 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
242 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
251 /* For two pointers, do this recursively on the target type. */
253 tree pointed_to_1 = TREE_TYPE (t1);
254 tree pointed_to_2 = TREE_TYPE (t2);
255 tree target = composite_type (pointed_to_1, pointed_to_2);
256 t1 = build_pointer_type (target);
257 return build_type_attribute_variant (t1, attributes);
262 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
263 /* Save space: see if the result is identical to one of the args. */
264 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
265 return build_type_attribute_variant (t1, attributes);
266 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
267 return build_type_attribute_variant (t2, attributes);
268 /* Merge the element types, and have a size if either arg has one. */
269 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
270 return build_type_attribute_variant (t1, attributes);
274 /* Function types: prefer the one that specified arg types.
275 If both do, merge the arg types. Also merge the return types. */
277 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
278 tree p1 = TYPE_ARG_TYPES (t1);
279 tree p2 = TYPE_ARG_TYPES (t2);
284 /* Save space: see if the result is identical to one of the args. */
285 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
286 return build_type_attribute_variant (t1, attributes);
287 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
288 return build_type_attribute_variant (t2, attributes);
290 /* Simple way if one arg fails to specify argument types. */
291 if (TYPE_ARG_TYPES (t1) == 0)
293 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
294 return build_type_attribute_variant (t1, attributes);
296 if (TYPE_ARG_TYPES (t2) == 0)
298 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
299 return build_type_attribute_variant (t1, attributes);
302 /* If both args specify argument types, we must merge the two
303 lists, argument by argument. */
304 /* Tell global_bindings_p to return false so that variable_size
305 doesn't abort on VLAs in parameter types. */
306 c_override_global_bindings_to_false = true;
308 len = list_length (p1);
311 for (i = 0; i < len; i++)
312 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
317 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
319 /* A null type means arg type is not specified.
320 Take whatever the other function type has. */
321 if (TREE_VALUE (p1) == 0)
323 TREE_VALUE (n) = TREE_VALUE (p2);
326 if (TREE_VALUE (p2) == 0)
328 TREE_VALUE (n) = TREE_VALUE (p1);
332 /* Given wait (union {union wait *u; int *i} *)
333 and wait (union wait *),
334 prefer union wait * as type of parm. */
335 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
336 && TREE_VALUE (p1) != TREE_VALUE (p2))
339 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
340 memb; memb = TREE_CHAIN (memb))
341 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
343 TREE_VALUE (n) = TREE_VALUE (p2);
345 pedwarn ("function types not truly compatible in ISO C");
349 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
350 && TREE_VALUE (p2) != TREE_VALUE (p1))
353 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
354 memb; memb = TREE_CHAIN (memb))
355 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
357 TREE_VALUE (n) = TREE_VALUE (p1);
359 pedwarn ("function types not truly compatible in ISO C");
363 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
367 c_override_global_bindings_to_false = false;
368 t1 = build_function_type (valtype, newargs);
369 /* ... falls through ... */
373 return build_type_attribute_variant (t1, attributes);
378 /* Return the type of a conditional expression between pointers to
379 possibly differently qualified versions of compatible types.
381 We assume that comp_target_types has already been done and returned
382 nonzero; if that isn't so, this may crash. */
385 common_pointer_type (tree t1, tree t2)
392 /* Save time if the two types are the same. */
394 if (t1 == t2) return t1;
396 /* If one type is nonsense, use the other. */
397 if (t1 == error_mark_node)
399 if (t2 == error_mark_node)
402 if (TREE_CODE (t1) != POINTER_TYPE || TREE_CODE (t2) != POINTER_TYPE)
405 /* Merge the attributes. */
406 attributes = targetm.merge_type_attributes (t1, t2);
408 /* Find the composite type of the target types, and combine the
409 qualifiers of the two types' targets. */
410 pointed_to_1 = TREE_TYPE (t1);
411 pointed_to_2 = TREE_TYPE (t2);
412 target = composite_type (TYPE_MAIN_VARIANT (pointed_to_1),
413 TYPE_MAIN_VARIANT (pointed_to_2));
414 t1 = build_pointer_type (c_build_qualified_type
416 TYPE_QUALS (pointed_to_1) |
417 TYPE_QUALS (pointed_to_2)));
418 return build_type_attribute_variant (t1, attributes);
421 /* Return the common type for two arithmetic types under the usual
422 arithmetic conversions. The default conversions have already been
423 applied, and enumerated types converted to their compatible integer
424 types. The resulting type is unqualified and has no attributes.
426 This is the type for the result of most arithmetic operations
427 if the operands have the given two types. */
430 common_type (tree t1, tree t2)
432 enum tree_code code1;
433 enum tree_code code2;
435 /* If one type is nonsense, use the other. */
436 if (t1 == error_mark_node)
438 if (t2 == error_mark_node)
441 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
442 t1 = TYPE_MAIN_VARIANT (t1);
444 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
445 t2 = TYPE_MAIN_VARIANT (t2);
447 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
448 t1 = build_type_attribute_variant (t1, NULL_TREE);
450 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
451 t2 = build_type_attribute_variant (t2, NULL_TREE);
453 /* Save time if the two types are the same. */
455 if (t1 == t2) return t1;
457 code1 = TREE_CODE (t1);
458 code2 = TREE_CODE (t2);
460 if (code1 != VECTOR_TYPE && code1 != COMPLEX_TYPE
461 && code1 != REAL_TYPE && code1 != INTEGER_TYPE)
464 if (code2 != VECTOR_TYPE && code2 != COMPLEX_TYPE
465 && code2 != REAL_TYPE && code2 != INTEGER_TYPE)
468 /* If one type is a vector type, return that type. (How the usual
469 arithmetic conversions apply to the vector types extension is not
470 precisely specified.) */
471 if (code1 == VECTOR_TYPE)
474 if (code2 == VECTOR_TYPE)
477 /* If one type is complex, form the common type of the non-complex
478 components, then make that complex. Use T1 or T2 if it is the
480 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
482 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
483 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
484 tree subtype = common_type (subtype1, subtype2);
486 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
488 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
491 return build_complex_type (subtype);
494 /* If only one is real, use it as the result. */
496 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
499 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
502 /* Both real or both integers; use the one with greater precision. */
504 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
506 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
509 /* Same precision. Prefer long longs to longs to ints when the
510 same precision, following the C99 rules on integer type rank
511 (which are equivalent to the C90 rules for C90 types). */
513 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
514 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
515 return long_long_unsigned_type_node;
517 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
518 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
520 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
521 return long_long_unsigned_type_node;
523 return long_long_integer_type_node;
526 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
527 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
528 return long_unsigned_type_node;
530 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
531 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
533 /* But preserve unsignedness from the other type,
534 since long cannot hold all the values of an unsigned int. */
535 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
536 return long_unsigned_type_node;
538 return long_integer_type_node;
541 /* Likewise, prefer long double to double even if same size. */
542 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
543 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
544 return long_double_type_node;
546 /* Otherwise prefer the unsigned one. */
548 if (TYPE_UNSIGNED (t1))
554 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
555 or various other operations. Return 2 if they are compatible
556 but a warning may be needed if you use them together. */
559 comptypes (tree type1, tree type2)
565 /* Suppress errors caused by previously reported errors. */
567 if (t1 == t2 || !t1 || !t2
568 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
571 /* If either type is the internal version of sizetype, return the
573 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
574 && TYPE_ORIG_SIZE_TYPE (t1))
575 t1 = TYPE_ORIG_SIZE_TYPE (t1);
577 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
578 && TYPE_ORIG_SIZE_TYPE (t2))
579 t2 = TYPE_ORIG_SIZE_TYPE (t2);
582 /* Enumerated types are compatible with integer types, but this is
583 not transitive: two enumerated types in the same translation unit
584 are compatible with each other only if they are the same type. */
586 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
587 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
588 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
589 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
594 /* Different classes of types can't be compatible. */
596 if (TREE_CODE (t1) != TREE_CODE (t2))
599 /* Qualifiers must match. C99 6.7.3p9 */
601 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
604 /* Allow for two different type nodes which have essentially the same
605 definition. Note that we already checked for equality of the type
606 qualifiers (just above). */
608 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
611 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
612 if (! (attrval = targetm.comp_type_attributes (t1, t2)))
615 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
618 switch (TREE_CODE (t1))
621 /* We must give ObjC the first crack at comparing pointers, since
622 protocol qualifiers may be involved. */
623 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
625 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
626 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
630 val = function_types_compatible_p (t1, t2);
635 tree d1 = TYPE_DOMAIN (t1);
636 tree d2 = TYPE_DOMAIN (t2);
637 bool d1_variable, d2_variable;
638 bool d1_zero, d2_zero;
641 /* Target types must match incl. qualifiers. */
642 if (TREE_TYPE (t1) != TREE_TYPE (t2)
643 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
646 /* Sizes must match unless one is missing or variable. */
647 if (d1 == 0 || d2 == 0 || d1 == d2)
650 d1_zero = ! TYPE_MAX_VALUE (d1);
651 d2_zero = ! TYPE_MAX_VALUE (d2);
653 d1_variable = (! d1_zero
654 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
655 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
656 d2_variable = (! d2_zero
657 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
658 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
660 if (d1_variable || d2_variable)
662 if (d1_zero && d2_zero)
664 if (d1_zero || d2_zero
665 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
666 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
673 /* We are dealing with two distinct structs. In assorted Objective-C
674 corner cases, however, these can still be deemed equivalent. */
675 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
680 if (val != 1 && !same_translation_unit_p (t1, t2))
681 val = tagged_types_tu_compatible_p (t1, t2);
685 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
686 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
692 return attrval == 2 && val == 1 ? 2 : val;
695 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
696 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
697 to 1 or 0 depending if the check of the pointer types is meant to
698 be reflexive or not (typically, assignments are not reflexive,
699 while comparisons are reflexive).
703 comp_target_types (tree ttl, tree ttr, int reflexive)
707 /* Give objc_comptypes a crack at letting these types through. */
708 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
711 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
712 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
714 if (val == 2 && pedantic)
715 pedwarn ("types are not quite compatible");
719 /* Subroutines of `comptypes'. */
721 /* Determine whether two trees derive from the same translation unit.
722 If the CONTEXT chain ends in a null, that tree's context is still
723 being parsed, so if two trees have context chains ending in null,
724 they're in the same translation unit. */
726 same_translation_unit_p (tree t1, tree t2)
728 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
729 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
731 case 'd': t1 = DECL_CONTEXT (t1); break;
732 case 't': t1 = TYPE_CONTEXT (t1); break;
733 case 'x': t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
737 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
738 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
740 case 'd': t2 = DECL_CONTEXT (t2); break;
741 case 't': t2 = TYPE_CONTEXT (t2); break;
742 case 'x': t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
749 /* The C standard says that two structures in different translation
750 units are compatible with each other only if the types of their
751 fields are compatible (among other things). So, consider two copies
752 of this structure: */
754 struct tagged_tu_seen {
755 const struct tagged_tu_seen * next;
760 /* Can they be compatible with each other? We choose to break the
761 recursion by allowing those types to be compatible. */
763 static const struct tagged_tu_seen * tagged_tu_seen_base;
765 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
766 compatible. If the two types are not the same (which has been
767 checked earlier), this can only happen when multiple translation
768 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
772 tagged_types_tu_compatible_p (tree t1, tree t2)
775 bool needs_warning = false;
777 /* We have to verify that the tags of the types are the same. This
778 is harder than it looks because this may be a typedef, so we have
779 to go look at the original type. It may even be a typedef of a
781 In the case of compiler-created builtin structs the TYPE_DECL
782 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
783 while (TYPE_NAME (t1)
784 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
785 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
786 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
788 while (TYPE_NAME (t2)
789 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
790 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
791 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
793 /* C90 didn't have the requirement that the two tags be the same. */
794 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
797 /* C90 didn't say what happened if one or both of the types were
798 incomplete; we choose to follow C99 rules here, which is that they
800 if (TYPE_SIZE (t1) == NULL
801 || TYPE_SIZE (t2) == NULL)
805 const struct tagged_tu_seen * tts_i;
806 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
807 if (tts_i->t1 == t1 && tts_i->t2 == t2)
811 switch (TREE_CODE (t1))
816 /* Speed up the case where the type values are in the same order. */
817 tree tv1 = TYPE_VALUES (t1);
818 tree tv2 = TYPE_VALUES (t2);
823 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
825 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
827 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
831 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
833 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
836 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
839 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
841 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
843 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
851 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
854 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
857 struct tagged_tu_seen tts;
859 tts.next = tagged_tu_seen_base;
862 tagged_tu_seen_base = &tts;
864 if (DECL_NAME (s1) != NULL)
865 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
866 if (DECL_NAME (s1) == DECL_NAME (s2))
869 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
873 needs_warning = true;
875 if (TREE_CODE (s1) == FIELD_DECL
876 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
877 DECL_FIELD_BIT_OFFSET (s2)) != 1)
883 tagged_tu_seen_base = tts.next;
887 return needs_warning ? 2 : 1;
892 struct tagged_tu_seen tts;
894 tts.next = tagged_tu_seen_base;
897 tagged_tu_seen_base = &tts;
899 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
901 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
904 if (TREE_CODE (s1) != TREE_CODE (s2)
905 || DECL_NAME (s1) != DECL_NAME (s2))
907 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
911 needs_warning = true;
913 if (TREE_CODE (s1) == FIELD_DECL
914 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
915 DECL_FIELD_BIT_OFFSET (s2)) != 1)
918 tagged_tu_seen_base = tts.next;
921 return needs_warning ? 2 : 1;
929 /* Return 1 if two function types F1 and F2 are compatible.
930 If either type specifies no argument types,
931 the other must specify a fixed number of self-promoting arg types.
932 Otherwise, if one type specifies only the number of arguments,
933 the other must specify that number of self-promoting arg types.
934 Otherwise, the argument types must match. */
937 function_types_compatible_p (tree f1, tree f2)
940 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
945 ret1 = TREE_TYPE (f1);
946 ret2 = TREE_TYPE (f2);
948 /* 'volatile' qualifiers on a function's return type mean the function
950 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
951 pedwarn ("function return types not compatible due to `volatile'");
952 if (TYPE_VOLATILE (ret1))
953 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
954 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
955 if (TYPE_VOLATILE (ret2))
956 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
957 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
958 val = comptypes (ret1, ret2);
962 args1 = TYPE_ARG_TYPES (f1);
963 args2 = TYPE_ARG_TYPES (f2);
965 /* An unspecified parmlist matches any specified parmlist
966 whose argument types don't need default promotions. */
970 if (!self_promoting_args_p (args2))
972 /* If one of these types comes from a non-prototype fn definition,
973 compare that with the other type's arglist.
974 If they don't match, ask for a warning (but no error). */
975 if (TYPE_ACTUAL_ARG_TYPES (f1)
976 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
982 if (!self_promoting_args_p (args1))
984 if (TYPE_ACTUAL_ARG_TYPES (f2)
985 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
990 /* Both types have argument lists: compare them and propagate results. */
991 val1 = type_lists_compatible_p (args1, args2);
992 return val1 != 1 ? val1 : val;
995 /* Check two lists of types for compatibility,
996 returning 0 for incompatible, 1 for compatible,
997 or 2 for compatible with warning. */
1000 type_lists_compatible_p (tree args1, tree args2)
1002 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1008 if (args1 == 0 && args2 == 0)
1010 /* If one list is shorter than the other,
1011 they fail to match. */
1012 if (args1 == 0 || args2 == 0)
1014 /* A null pointer instead of a type
1015 means there is supposed to be an argument
1016 but nothing is specified about what type it has.
1017 So match anything that self-promotes. */
1018 if (TREE_VALUE (args1) == 0)
1020 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
1023 else if (TREE_VALUE (args2) == 0)
1025 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
1028 /* If one of the lists has an error marker, ignore this arg. */
1029 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
1030 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
1032 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
1033 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
1035 /* Allow wait (union {union wait *u; int *i} *)
1036 and wait (union wait *) to be compatible. */
1037 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
1038 && (TYPE_NAME (TREE_VALUE (args1)) == 0
1039 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
1040 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
1041 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
1042 TYPE_SIZE (TREE_VALUE (args2))))
1045 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
1046 memb; memb = TREE_CHAIN (memb))
1047 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
1052 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
1053 && (TYPE_NAME (TREE_VALUE (args2)) == 0
1054 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
1055 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
1056 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
1057 TYPE_SIZE (TREE_VALUE (args1))))
1060 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
1061 memb; memb = TREE_CHAIN (memb))
1062 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
1071 /* comptypes said ok, but record if it said to warn. */
1075 args1 = TREE_CHAIN (args1);
1076 args2 = TREE_CHAIN (args2);
1080 /* Compute the size to increment a pointer by. */
1083 c_size_in_bytes (tree type)
1085 enum tree_code code = TREE_CODE (type);
1087 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1088 return size_one_node;
1090 if (!COMPLETE_OR_VOID_TYPE_P (type))
1092 error ("arithmetic on pointer to an incomplete type");
1093 return size_one_node;
1096 /* Convert in case a char is more than one unit. */
1097 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1098 size_int (TYPE_PRECISION (char_type_node)
1102 /* Return either DECL or its known constant value (if it has one). */
1105 decl_constant_value (tree decl)
1107 if (/* Don't change a variable array bound or initial value to a constant
1108 in a place where a variable is invalid. Note that DECL_INITIAL
1109 isn't valid for a PARM_DECL. */
1110 current_function_decl != 0
1111 && TREE_CODE (decl) != PARM_DECL
1112 && ! TREE_THIS_VOLATILE (decl)
1113 && TREE_READONLY (decl)
1114 && DECL_INITIAL (decl) != 0
1115 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1116 /* This is invalid if initial value is not constant.
1117 If it has either a function call, a memory reference,
1118 or a variable, then re-evaluating it could give different results. */
1119 && TREE_CONSTANT (DECL_INITIAL (decl))
1120 /* Check for cases where this is sub-optimal, even though valid. */
1121 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1122 return DECL_INITIAL (decl);
1126 /* Return either DECL or its known constant value (if it has one), but
1127 return DECL if pedantic or DECL has mode BLKmode. This is for
1128 bug-compatibility with the old behavior of decl_constant_value
1129 (before GCC 3.0); every use of this function is a bug and it should
1130 be removed before GCC 3.1. It is not appropriate to use pedantic
1131 in a way that affects optimization, and BLKmode is probably not the
1132 right test for avoiding misoptimizations either. */
1135 decl_constant_value_for_broken_optimization (tree decl)
1137 if (pedantic || DECL_MODE (decl) == BLKmode)
1140 return decl_constant_value (decl);
1144 /* Perform the default conversion of arrays and functions to pointers.
1145 Return the result of converting EXP. For any other expression, just
1149 default_function_array_conversion (tree exp)
1152 tree type = TREE_TYPE (exp);
1153 enum tree_code code = TREE_CODE (type);
1156 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1159 Do not use STRIP_NOPS here! It will remove conversions from pointer
1160 to integer and cause infinite recursion. */
1162 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1163 || (TREE_CODE (exp) == NOP_EXPR
1164 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1166 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1168 exp = TREE_OPERAND (exp, 0);
1171 /* Preserve the original expression code. */
1172 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1173 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1175 if (code == FUNCTION_TYPE)
1177 return build_unary_op (ADDR_EXPR, exp, 0);
1179 if (code == ARRAY_TYPE)
1182 tree restype = TREE_TYPE (type);
1188 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1190 constp = TREE_READONLY (exp);
1191 volatilep = TREE_THIS_VOLATILE (exp);
1194 if (TYPE_QUALS (type) || constp || volatilep)
1196 = c_build_qualified_type (restype,
1198 | (constp * TYPE_QUAL_CONST)
1199 | (volatilep * TYPE_QUAL_VOLATILE));
1201 if (TREE_CODE (exp) == INDIRECT_REF)
1202 return convert (build_pointer_type (restype),
1203 TREE_OPERAND (exp, 0));
1205 if (TREE_CODE (exp) == COMPOUND_EXPR)
1207 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1208 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1209 TREE_OPERAND (exp, 0), op1);
1212 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1213 if (!flag_isoc99 && !lvalue_array_p)
1215 /* Before C99, non-lvalue arrays do not decay to pointers.
1216 Normally, using such an array would be invalid; but it can
1217 be used correctly inside sizeof or as a statement expression.
1218 Thus, do not give an error here; an error will result later. */
1222 ptrtype = build_pointer_type (restype);
1224 if (TREE_CODE (exp) == VAR_DECL)
1226 /* ??? This is not really quite correct
1227 in that the type of the operand of ADDR_EXPR
1228 is not the target type of the type of the ADDR_EXPR itself.
1229 Question is, can this lossage be avoided? */
1230 adr = build1 (ADDR_EXPR, ptrtype, exp);
1231 if (!c_mark_addressable (exp))
1232 return error_mark_node;
1233 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1236 /* This way is better for a COMPONENT_REF since it can
1237 simplify the offset for a component. */
1238 adr = build_unary_op (ADDR_EXPR, exp, 1);
1239 return convert (ptrtype, adr);
1244 /* Perform default promotions for C data used in expressions.
1245 Arrays and functions are converted to pointers;
1246 enumeral types or short or char, to int.
1247 In addition, manifest constants symbols are replaced by their values. */
1250 default_conversion (tree exp)
1253 tree type = TREE_TYPE (exp);
1254 enum tree_code code = TREE_CODE (type);
1256 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1257 return default_function_array_conversion (exp);
1259 /* Constants can be used directly unless they're not loadable. */
1260 if (TREE_CODE (exp) == CONST_DECL)
1261 exp = DECL_INITIAL (exp);
1263 /* Replace a nonvolatile const static variable with its value unless
1264 it is an array, in which case we must be sure that taking the
1265 address of the array produces consistent results. */
1266 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1268 exp = decl_constant_value_for_broken_optimization (exp);
1269 type = TREE_TYPE (exp);
1272 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1275 Do not use STRIP_NOPS here! It will remove conversions from pointer
1276 to integer and cause infinite recursion. */
1278 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1279 || (TREE_CODE (exp) == NOP_EXPR
1280 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1281 exp = TREE_OPERAND (exp, 0);
1283 /* Preserve the original expression code. */
1284 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1285 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1287 /* Normally convert enums to int,
1288 but convert wide enums to something wider. */
1289 if (code == ENUMERAL_TYPE)
1291 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1292 TYPE_PRECISION (integer_type_node)),
1293 ((TYPE_PRECISION (type)
1294 >= TYPE_PRECISION (integer_type_node))
1295 && TYPE_UNSIGNED (type)));
1297 return convert (type, exp);
1300 if (TREE_CODE (exp) == COMPONENT_REF
1301 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1302 /* If it's thinner than an int, promote it like a
1303 c_promoting_integer_type_p, otherwise leave it alone. */
1304 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1305 TYPE_PRECISION (integer_type_node)))
1306 return convert (integer_type_node, exp);
1308 if (c_promoting_integer_type_p (type))
1310 /* Preserve unsignedness if not really getting any wider. */
1311 if (TYPE_UNSIGNED (type)
1312 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1313 return convert (unsigned_type_node, exp);
1315 return convert (integer_type_node, exp);
1318 if (code == VOID_TYPE)
1320 error ("void value not ignored as it ought to be");
1321 return error_mark_node;
1326 /* Look up COMPONENT in a structure or union DECL.
1328 If the component name is not found, returns NULL_TREE. Otherwise,
1329 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1330 stepping down the chain to the component, which is in the last
1331 TREE_VALUE of the list. Normally the list is of length one, but if
1332 the component is embedded within (nested) anonymous structures or
1333 unions, the list steps down the chain to the component. */
1336 lookup_field (tree decl, tree component)
1338 tree type = TREE_TYPE (decl);
1341 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1342 to the field elements. Use a binary search on this array to quickly
1343 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1344 will always be set for structures which have many elements. */
1346 if (TYPE_LANG_SPECIFIC (type))
1349 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1351 field = TYPE_FIELDS (type);
1353 top = TYPE_LANG_SPECIFIC (type)->s->len;
1354 while (top - bot > 1)
1356 half = (top - bot + 1) >> 1;
1357 field = field_array[bot+half];
1359 if (DECL_NAME (field) == NULL_TREE)
1361 /* Step through all anon unions in linear fashion. */
1362 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1364 field = field_array[bot++];
1365 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1366 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1368 tree anon = lookup_field (field, component);
1371 return tree_cons (NULL_TREE, field, anon);
1375 /* Entire record is only anon unions. */
1379 /* Restart the binary search, with new lower bound. */
1383 if (DECL_NAME (field) == component)
1385 if (DECL_NAME (field) < component)
1391 if (DECL_NAME (field_array[bot]) == component)
1392 field = field_array[bot];
1393 else if (DECL_NAME (field) != component)
1398 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1400 if (DECL_NAME (field) == NULL_TREE
1401 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1402 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1404 tree anon = lookup_field (field, component);
1407 return tree_cons (NULL_TREE, field, anon);
1410 if (DECL_NAME (field) == component)
1414 if (field == NULL_TREE)
1418 return tree_cons (NULL_TREE, field, NULL_TREE);
1421 /* Make an expression to refer to the COMPONENT field of
1422 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1425 build_component_ref (tree datum, tree component)
1427 tree type = TREE_TYPE (datum);
1428 enum tree_code code = TREE_CODE (type);
1432 if (!objc_is_public (datum, component))
1433 return error_mark_node;
1435 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1436 Ensure that the arguments are not lvalues; otherwise,
1437 if the component is an array, it would wrongly decay to a pointer in
1439 We cannot do this with a COND_EXPR, because in a conditional expression
1440 the default promotions are applied to both sides, and this would yield
1441 the wrong type of the result; for example, if the components have
1443 switch (TREE_CODE (datum))
1447 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1448 return build (COMPOUND_EXPR, TREE_TYPE (value),
1449 TREE_OPERAND (datum, 0), non_lvalue (value));
1455 /* See if there is a field or component with name COMPONENT. */
1457 if (code == RECORD_TYPE || code == UNION_TYPE)
1459 if (!COMPLETE_TYPE_P (type))
1461 c_incomplete_type_error (NULL_TREE, type);
1462 return error_mark_node;
1465 field = lookup_field (datum, component);
1469 error ("%s has no member named `%s'",
1470 code == RECORD_TYPE ? "structure" : "union",
1471 IDENTIFIER_POINTER (component));
1472 return error_mark_node;
1475 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1476 This might be better solved in future the way the C++ front
1477 end does it - by giving the anonymous entities each a
1478 separate name and type, and then have build_component_ref
1479 recursively call itself. We can't do that here. */
1482 tree subdatum = TREE_VALUE (field);
1484 if (TREE_TYPE (subdatum) == error_mark_node)
1485 return error_mark_node;
1487 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1488 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1489 TREE_READONLY (ref) = 1;
1490 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1491 TREE_THIS_VOLATILE (ref) = 1;
1493 if (TREE_DEPRECATED (subdatum))
1494 warn_deprecated_use (subdatum);
1498 field = TREE_CHAIN (field);
1504 else if (code != ERROR_MARK)
1505 error ("request for member `%s' in something not a structure or union",
1506 IDENTIFIER_POINTER (component));
1508 return error_mark_node;
1511 /* Given an expression PTR for a pointer, return an expression
1512 for the value pointed to.
1513 ERRORSTRING is the name of the operator to appear in error messages. */
1516 build_indirect_ref (tree ptr, const char *errorstring)
1518 tree pointer = default_conversion (ptr);
1519 tree type = TREE_TYPE (pointer);
1521 if (TREE_CODE (type) == POINTER_TYPE)
1523 if (TREE_CODE (pointer) == ADDR_EXPR
1524 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1525 == TREE_TYPE (type)))
1526 return TREE_OPERAND (pointer, 0);
1529 tree t = TREE_TYPE (type);
1530 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1532 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1534 error ("dereferencing pointer to incomplete type");
1535 return error_mark_node;
1537 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1538 warning ("dereferencing `void *' pointer");
1540 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1541 so that we get the proper error message if the result is used
1542 to assign to. Also, &* is supposed to be a no-op.
1543 And ANSI C seems to specify that the type of the result
1544 should be the const type. */
1545 /* A de-reference of a pointer to const is not a const. It is valid
1546 to change it via some other pointer. */
1547 TREE_READONLY (ref) = TYPE_READONLY (t);
1548 TREE_SIDE_EFFECTS (ref)
1549 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1550 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1554 else if (TREE_CODE (pointer) != ERROR_MARK)
1555 error ("invalid type argument of `%s'", errorstring);
1556 return error_mark_node;
1559 /* This handles expressions of the form "a[i]", which denotes
1562 This is logically equivalent in C to *(a+i), but we may do it differently.
1563 If A is a variable or a member, we generate a primitive ARRAY_REF.
1564 This avoids forcing the array out of registers, and can work on
1565 arrays that are not lvalues (for example, members of structures returned
1569 build_array_ref (tree array, tree index)
1573 error ("subscript missing in array reference");
1574 return error_mark_node;
1577 if (TREE_TYPE (array) == error_mark_node
1578 || TREE_TYPE (index) == error_mark_node)
1579 return error_mark_node;
1581 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1585 /* Subscripting with type char is likely to lose
1586 on a machine where chars are signed.
1587 So warn on any machine, but optionally.
1588 Don't warn for unsigned char since that type is safe.
1589 Don't warn for signed char because anyone who uses that
1590 must have done so deliberately. */
1591 if (warn_char_subscripts
1592 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1593 warning ("array subscript has type `char'");
1595 /* Apply default promotions *after* noticing character types. */
1596 index = default_conversion (index);
1598 /* Require integer *after* promotion, for sake of enums. */
1599 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1601 error ("array subscript is not an integer");
1602 return error_mark_node;
1605 /* An array that is indexed by a non-constant
1606 cannot be stored in a register; we must be able to do
1607 address arithmetic on its address.
1608 Likewise an array of elements of variable size. */
1609 if (TREE_CODE (index) != INTEGER_CST
1610 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1611 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1613 if (!c_mark_addressable (array))
1614 return error_mark_node;
1616 /* An array that is indexed by a constant value which is not within
1617 the array bounds cannot be stored in a register either; because we
1618 would get a crash in store_bit_field/extract_bit_field when trying
1619 to access a non-existent part of the register. */
1620 if (TREE_CODE (index) == INTEGER_CST
1621 && TYPE_DOMAIN (TREE_TYPE (array))
1622 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1624 if (!c_mark_addressable (array))
1625 return error_mark_node;
1631 while (TREE_CODE (foo) == COMPONENT_REF)
1632 foo = TREE_OPERAND (foo, 0);
1633 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1634 pedwarn ("ISO C forbids subscripting `register' array");
1635 else if (! flag_isoc99 && ! lvalue_p (foo))
1636 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1639 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1640 rval = build (ARRAY_REF, type, array, index);
1641 /* Array ref is const/volatile if the array elements are
1642 or if the array is. */
1643 TREE_READONLY (rval)
1644 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1645 | TREE_READONLY (array));
1646 TREE_SIDE_EFFECTS (rval)
1647 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1648 | TREE_SIDE_EFFECTS (array));
1649 TREE_THIS_VOLATILE (rval)
1650 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1651 /* This was added by rms on 16 Nov 91.
1652 It fixes vol struct foo *a; a->elts[1]
1653 in an inline function.
1654 Hope it doesn't break something else. */
1655 | TREE_THIS_VOLATILE (array));
1656 return require_complete_type (fold (rval));
1660 tree ar = default_conversion (array);
1661 tree ind = default_conversion (index);
1663 /* Do the same warning check as above, but only on the part that's
1664 syntactically the index and only if it is also semantically
1666 if (warn_char_subscripts
1667 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1668 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1669 warning ("subscript has type `char'");
1671 /* Put the integer in IND to simplify error checking. */
1672 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1679 if (ar == error_mark_node)
1682 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1683 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1685 error ("subscripted value is neither array nor pointer");
1686 return error_mark_node;
1688 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1690 error ("array subscript is not an integer");
1691 return error_mark_node;
1694 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1699 /* Build an external reference to identifier ID. FUN indicates
1700 whether this will be used for a function call. */
1702 build_external_ref (tree id, int fun)
1705 tree decl = lookup_name (id);
1706 tree objc_ivar = lookup_objc_ivar (id);
1708 if (decl && decl != error_mark_node)
1710 /* Properly declared variable or function reference. */
1713 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1715 warning ("local declaration of `%s' hides instance variable",
1716 IDENTIFIER_POINTER (id));
1725 /* Implicit function declaration. */
1726 ref = implicitly_declare (id);
1727 else if (decl == error_mark_node)
1728 /* Don't complain about something that's already been
1729 complained about. */
1730 return error_mark_node;
1733 undeclared_variable (id);
1734 return error_mark_node;
1737 if (TREE_TYPE (ref) == error_mark_node)
1738 return error_mark_node;
1740 if (TREE_DEPRECATED (ref))
1741 warn_deprecated_use (ref);
1743 if (!skip_evaluation)
1744 assemble_external (ref);
1745 TREE_USED (ref) = 1;
1747 if (TREE_CODE (ref) == CONST_DECL)
1749 ref = DECL_INITIAL (ref);
1750 TREE_CONSTANT (ref) = 1;
1751 TREE_INVARIANT (ref) = 1;
1753 else if (current_function_decl != 0
1754 && !DECL_FILE_SCOPE_P (current_function_decl)
1755 && (TREE_CODE (ref) == VAR_DECL
1756 || TREE_CODE (ref) == PARM_DECL
1757 || TREE_CODE (ref) == FUNCTION_DECL))
1759 tree context = decl_function_context (ref);
1761 if (context != 0 && context != current_function_decl)
1762 DECL_NONLOCAL (ref) = 1;
1768 /* Build a function call to function FUNCTION with parameters PARAMS.
1769 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1770 TREE_VALUE of each node is a parameter-expression.
1771 FUNCTION's data type may be a function type or a pointer-to-function. */
1774 build_function_call (tree function, tree params)
1776 tree fntype, fundecl = 0;
1777 tree coerced_params;
1778 tree name = NULL_TREE, result;
1781 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1782 STRIP_TYPE_NOPS (function);
1784 /* Convert anything with function type to a pointer-to-function. */
1785 if (TREE_CODE (function) == FUNCTION_DECL)
1787 name = DECL_NAME (function);
1789 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1790 (because calling an inline function does not mean the function
1791 needs to be separately compiled). */
1792 fntype = build_type_variant (TREE_TYPE (function),
1793 TREE_READONLY (function),
1794 TREE_THIS_VOLATILE (function));
1796 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1799 function = default_conversion (function);
1801 fntype = TREE_TYPE (function);
1803 if (TREE_CODE (fntype) == ERROR_MARK)
1804 return error_mark_node;
1806 if (!(TREE_CODE (fntype) == POINTER_TYPE
1807 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1809 error ("called object is not a function");
1810 return error_mark_node;
1813 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1814 current_function_returns_abnormally = 1;
1816 /* fntype now gets the type of function pointed to. */
1817 fntype = TREE_TYPE (fntype);
1819 /* Check that the function is called through a compatible prototype.
1820 If it is not, replace the call by a trap, wrapped up in a compound
1821 expression if necessary. This has the nice side-effect to prevent
1822 the tree-inliner from generating invalid assignment trees which may
1823 blow up in the RTL expander later.
1825 ??? This doesn't work for Objective-C because objc_comptypes
1826 refuses to compare function prototypes, yet the compiler appears
1827 to build calls that are flagged as invalid by C's comptypes. */
1828 if (! c_dialect_objc ()
1829 && TREE_CODE (function) == NOP_EXPR
1830 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1831 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1832 && ! comptypes (fntype, TREE_TYPE (tem)))
1834 tree return_type = TREE_TYPE (fntype);
1835 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1838 /* This situation leads to run-time undefined behavior. We can't,
1839 therefore, simply error unless we can prove that all possible
1840 executions of the program must execute the code. */
1841 warning ("function called through a non-compatible type");
1843 /* We can, however, treat "undefined" any way we please.
1844 Call abort to encourage the user to fix the program. */
1845 inform ("if this code is reached, the program will abort");
1847 if (VOID_TYPE_P (return_type))
1853 if (AGGREGATE_TYPE_P (return_type))
1854 rhs = build_compound_literal (return_type,
1855 build_constructor (return_type,
1858 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1860 return build (COMPOUND_EXPR, return_type, trap, rhs);
1864 /* Convert the parameters to the types declared in the
1865 function prototype, or apply default promotions. */
1868 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1870 /* Check that the arguments to the function are valid. */
1872 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1874 /* Recognize certain built-in functions so we can make tree-codes
1875 other than CALL_EXPR. We do this when it enables fold-const.c
1876 to do something useful. */
1878 if (TREE_CODE (function) == ADDR_EXPR
1879 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1880 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1882 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1883 params, coerced_params);
1888 result = build (CALL_EXPR, TREE_TYPE (fntype),
1889 function, coerced_params, NULL_TREE);
1890 TREE_SIDE_EFFECTS (result) = 1;
1892 if (require_constant_value)
1894 result = fold_initializer (result);
1896 if (TREE_CONSTANT (result)
1897 && (name == NULL_TREE
1898 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
1899 pedwarn_init ("initializer element is not constant");
1902 result = fold (result);
1904 if (VOID_TYPE_P (TREE_TYPE (result)))
1906 return require_complete_type (result);
1909 /* Convert the argument expressions in the list VALUES
1910 to the types in the list TYPELIST. The result is a list of converted
1911 argument expressions.
1913 If TYPELIST is exhausted, or when an element has NULL as its type,
1914 perform the default conversions.
1916 PARMLIST is the chain of parm decls for the function being called.
1917 It may be 0, if that info is not available.
1918 It is used only for generating error messages.
1920 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1922 This is also where warnings about wrong number of args are generated.
1924 Both VALUES and the returned value are chains of TREE_LIST nodes
1925 with the elements of the list in the TREE_VALUE slots of those nodes. */
1928 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1930 tree typetail, valtail;
1934 /* Scan the given expressions and types, producing individual
1935 converted arguments and pushing them on RESULT in reverse order. */
1937 for (valtail = values, typetail = typelist, parmnum = 0;
1939 valtail = TREE_CHAIN (valtail), parmnum++)
1941 tree type = typetail ? TREE_VALUE (typetail) : 0;
1942 tree val = TREE_VALUE (valtail);
1944 if (type == void_type_node)
1947 error ("too many arguments to function `%s'",
1948 IDENTIFIER_POINTER (name));
1950 error ("too many arguments to function");
1954 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1955 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1956 to convert automatically to a pointer. */
1957 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1958 val = TREE_OPERAND (val, 0);
1960 val = default_function_array_conversion (val);
1962 val = require_complete_type (val);
1966 /* Formal parm type is specified by a function prototype. */
1969 if (!COMPLETE_TYPE_P (type))
1971 error ("type of formal parameter %d is incomplete", parmnum + 1);
1976 /* Optionally warn about conversions that
1977 differ from the default conversions. */
1978 if (warn_conversion || warn_traditional)
1980 int formal_prec = TYPE_PRECISION (type);
1982 if (INTEGRAL_TYPE_P (type)
1983 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1984 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1985 if (INTEGRAL_TYPE_P (type)
1986 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1987 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1988 else if (TREE_CODE (type) == COMPLEX_TYPE
1989 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1990 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1991 else if (TREE_CODE (type) == REAL_TYPE
1992 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1993 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1994 else if (TREE_CODE (type) == COMPLEX_TYPE
1995 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1996 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1997 else if (TREE_CODE (type) == REAL_TYPE
1998 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1999 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
2000 /* ??? At some point, messages should be written about
2001 conversions between complex types, but that's too messy
2003 else if (TREE_CODE (type) == REAL_TYPE
2004 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2006 /* Warn if any argument is passed as `float',
2007 since without a prototype it would be `double'. */
2008 if (formal_prec == TYPE_PRECISION (float_type_node))
2009 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
2011 /* Detect integer changing in width or signedness.
2012 These warnings are only activated with
2013 -Wconversion, not with -Wtraditional. */
2014 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2015 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2017 tree would_have_been = default_conversion (val);
2018 tree type1 = TREE_TYPE (would_have_been);
2020 if (TREE_CODE (type) == ENUMERAL_TYPE
2021 && (TYPE_MAIN_VARIANT (type)
2022 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2023 /* No warning if function asks for enum
2024 and the actual arg is that enum type. */
2026 else if (formal_prec != TYPE_PRECISION (type1))
2027 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
2028 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2030 /* Don't complain if the formal parameter type
2031 is an enum, because we can't tell now whether
2032 the value was an enum--even the same enum. */
2033 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2035 else if (TREE_CODE (val) == INTEGER_CST
2036 && int_fits_type_p (val, type))
2037 /* Change in signedness doesn't matter
2038 if a constant value is unaffected. */
2040 /* Likewise for a constant in a NOP_EXPR. */
2041 else if (TREE_CODE (val) == NOP_EXPR
2042 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2043 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2045 /* If the value is extended from a narrower
2046 unsigned type, it doesn't matter whether we
2047 pass it as signed or unsigned; the value
2048 certainly is the same either way. */
2049 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2050 && TYPE_UNSIGNED (TREE_TYPE (val)))
2052 else if (TYPE_UNSIGNED (type))
2053 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
2055 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
2059 parmval = convert_for_assignment (type, val,
2060 (char *) 0, /* arg passing */
2061 fundecl, name, parmnum + 1);
2063 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2064 && INTEGRAL_TYPE_P (type)
2065 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2066 parmval = default_conversion (parmval);
2068 result = tree_cons (NULL_TREE, parmval, result);
2070 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2071 && (TYPE_PRECISION (TREE_TYPE (val))
2072 < TYPE_PRECISION (double_type_node)))
2073 /* Convert `float' to `double'. */
2074 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2076 /* Convert `short' and `char' to full-size `int'. */
2077 result = tree_cons (NULL_TREE, default_conversion (val), result);
2080 typetail = TREE_CHAIN (typetail);
2083 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2086 error ("too few arguments to function `%s'",
2087 IDENTIFIER_POINTER (name));
2089 error ("too few arguments to function");
2092 return nreverse (result);
2095 /* This is the entry point used by the parser
2096 for binary operators in the input.
2097 In addition to constructing the expression,
2098 we check for operands that were written with other binary operators
2099 in a way that is likely to confuse the user. */
2102 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
2104 tree result = build_binary_op (code, arg1, arg2, 1);
2107 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
2108 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
2109 enum tree_code code1 = ERROR_MARK;
2110 enum tree_code code2 = ERROR_MARK;
2112 if (TREE_CODE (result) == ERROR_MARK)
2113 return error_mark_node;
2115 if (IS_EXPR_CODE_CLASS (class1))
2116 code1 = C_EXP_ORIGINAL_CODE (arg1);
2117 if (IS_EXPR_CODE_CLASS (class2))
2118 code2 = C_EXP_ORIGINAL_CODE (arg2);
2120 /* Check for cases such as x+y<<z which users are likely
2121 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2122 is cleared to prevent these warnings. */
2123 if (warn_parentheses)
2125 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2127 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2128 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2129 warning ("suggest parentheses around + or - inside shift");
2132 if (code == TRUTH_ORIF_EXPR)
2134 if (code1 == TRUTH_ANDIF_EXPR
2135 || code2 == TRUTH_ANDIF_EXPR)
2136 warning ("suggest parentheses around && within ||");
2139 if (code == BIT_IOR_EXPR)
2141 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2142 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2143 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2144 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2145 warning ("suggest parentheses around arithmetic in operand of |");
2146 /* Check cases like x|y==z */
2147 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2148 warning ("suggest parentheses around comparison in operand of |");
2151 if (code == BIT_XOR_EXPR)
2153 if (code1 == BIT_AND_EXPR
2154 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2155 || code2 == BIT_AND_EXPR
2156 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2157 warning ("suggest parentheses around arithmetic in operand of ^");
2158 /* Check cases like x^y==z */
2159 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2160 warning ("suggest parentheses around comparison in operand of ^");
2163 if (code == BIT_AND_EXPR)
2165 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2166 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2167 warning ("suggest parentheses around + or - in operand of &");
2168 /* Check cases like x&y==z */
2169 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2170 warning ("suggest parentheses around comparison in operand of &");
2174 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2175 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2176 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2177 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2179 unsigned_conversion_warning (result, arg1);
2180 unsigned_conversion_warning (result, arg2);
2181 overflow_warning (result);
2183 class = TREE_CODE_CLASS (TREE_CODE (result));
2185 /* Record the code that was specified in the source,
2186 for the sake of warnings about confusing nesting. */
2187 if (IS_EXPR_CODE_CLASS (class))
2188 C_SET_EXP_ORIGINAL_CODE (result, code);
2191 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2192 so that convert_for_assignment wouldn't strip it.
2193 That way, we got warnings for things like p = (1 - 1).
2194 But it turns out we should not get those warnings. */
2195 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2196 C_SET_EXP_ORIGINAL_CODE (result, code);
2203 /* Return true if `t' is known to be non-negative. */
2206 c_tree_expr_nonnegative_p (tree t)
2208 if (TREE_CODE (t) == STMT_EXPR)
2210 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2212 /* Find the last statement in the chain, ignoring the final
2213 * scope statement */
2214 while (TREE_CHAIN (t) != NULL_TREE
2215 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2217 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2219 return tree_expr_nonnegative_p (t);
2222 /* Return a tree for the difference of pointers OP0 and OP1.
2223 The resulting tree has type int. */
2226 pointer_diff (tree op0, tree op1)
2228 tree restype = ptrdiff_type_node;
2230 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2231 tree con0, con1, lit0, lit1;
2232 tree orig_op1 = op1;
2234 if (pedantic || warn_pointer_arith)
2236 if (TREE_CODE (target_type) == VOID_TYPE)
2237 pedwarn ("pointer of type `void *' used in subtraction");
2238 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2239 pedwarn ("pointer to a function used in subtraction");
2242 /* If the conversion to ptrdiff_type does anything like widening or
2243 converting a partial to an integral mode, we get a convert_expression
2244 that is in the way to do any simplifications.
2245 (fold-const.c doesn't know that the extra bits won't be needed.
2246 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2247 different mode in place.)
2248 So first try to find a common term here 'by hand'; we want to cover
2249 at least the cases that occur in legal static initializers. */
2250 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2251 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2253 if (TREE_CODE (con0) == PLUS_EXPR)
2255 lit0 = TREE_OPERAND (con0, 1);
2256 con0 = TREE_OPERAND (con0, 0);
2259 lit0 = integer_zero_node;
2261 if (TREE_CODE (con1) == PLUS_EXPR)
2263 lit1 = TREE_OPERAND (con1, 1);
2264 con1 = TREE_OPERAND (con1, 0);
2267 lit1 = integer_zero_node;
2269 if (operand_equal_p (con0, con1, 0))
2276 /* First do the subtraction as integers;
2277 then drop through to build the divide operator.
2278 Do not do default conversions on the minus operator
2279 in case restype is a short type. */
2281 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2282 convert (restype, op1), 0);
2283 /* This generates an error if op1 is pointer to incomplete type. */
2284 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2285 error ("arithmetic on pointer to an incomplete type");
2287 /* This generates an error if op0 is pointer to incomplete type. */
2288 op1 = c_size_in_bytes (target_type);
2290 /* Divide by the size, in easiest possible way. */
2291 return fold (build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2294 /* Construct and perhaps optimize a tree representation
2295 for a unary operation. CODE, a tree_code, specifies the operation
2296 and XARG is the operand.
2297 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2298 the default promotions (such as from short to int).
2299 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2300 allows non-lvalues; this is only used to handle conversion of non-lvalue
2301 arrays to pointers in C99. */
2304 build_unary_op (enum tree_code code, tree xarg, int flag)
2306 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2309 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2311 int noconvert = flag;
2313 if (typecode == ERROR_MARK)
2314 return error_mark_node;
2315 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2316 typecode = INTEGER_TYPE;
2321 /* This is used for unary plus, because a CONVERT_EXPR
2322 is enough to prevent anybody from looking inside for
2323 associativity, but won't generate any code. */
2324 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2325 || typecode == COMPLEX_TYPE))
2327 error ("wrong type argument to unary plus");
2328 return error_mark_node;
2330 else if (!noconvert)
2331 arg = default_conversion (arg);
2332 arg = non_lvalue (arg);
2336 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2337 || typecode == COMPLEX_TYPE
2338 || typecode == VECTOR_TYPE))
2340 error ("wrong type argument to unary minus");
2341 return error_mark_node;
2343 else if (!noconvert)
2344 arg = default_conversion (arg);
2348 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2351 arg = default_conversion (arg);
2353 else if (typecode == COMPLEX_TYPE)
2357 pedwarn ("ISO C does not support `~' for complex conjugation");
2359 arg = default_conversion (arg);
2363 error ("wrong type argument to bit-complement");
2364 return error_mark_node;
2369 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2371 error ("wrong type argument to abs");
2372 return error_mark_node;
2374 else if (!noconvert)
2375 arg = default_conversion (arg);
2379 /* Conjugating a real value is a no-op, but allow it anyway. */
2380 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2381 || typecode == COMPLEX_TYPE))
2383 error ("wrong type argument to conjugation");
2384 return error_mark_node;
2386 else if (!noconvert)
2387 arg = default_conversion (arg);
2390 case TRUTH_NOT_EXPR:
2391 if (typecode != INTEGER_TYPE
2392 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2393 && typecode != COMPLEX_TYPE
2394 /* These will convert to a pointer. */
2395 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2397 error ("wrong type argument to unary exclamation mark");
2398 return error_mark_node;
2400 arg = lang_hooks.truthvalue_conversion (arg);
2401 return invert_truthvalue (arg);
2407 if (TREE_CODE (arg) == COMPLEX_CST)
2408 return TREE_REALPART (arg);
2409 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2410 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2415 if (TREE_CODE (arg) == COMPLEX_CST)
2416 return TREE_IMAGPART (arg);
2417 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2418 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2420 return convert (TREE_TYPE (arg), integer_zero_node);
2422 case PREINCREMENT_EXPR:
2423 case POSTINCREMENT_EXPR:
2424 case PREDECREMENT_EXPR:
2425 case POSTDECREMENT_EXPR:
2427 /* Increment or decrement the real part of the value,
2428 and don't change the imaginary part. */
2429 if (typecode == COMPLEX_TYPE)
2434 pedwarn ("ISO C does not support `++' and `--' on complex types");
2436 arg = stabilize_reference (arg);
2437 real = build_unary_op (REALPART_EXPR, arg, 1);
2438 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2439 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2440 build_unary_op (code, real, 1), imag);
2443 /* Report invalid types. */
2445 if (typecode != POINTER_TYPE
2446 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2448 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2449 error ("wrong type argument to increment");
2451 error ("wrong type argument to decrement");
2453 return error_mark_node;
2458 tree result_type = TREE_TYPE (arg);
2460 arg = get_unwidened (arg, 0);
2461 argtype = TREE_TYPE (arg);
2463 /* Compute the increment. */
2465 if (typecode == POINTER_TYPE)
2467 /* If pointer target is an undefined struct,
2468 we just cannot know how to do the arithmetic. */
2469 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2471 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2472 error ("increment of pointer to unknown structure");
2474 error ("decrement of pointer to unknown structure");
2476 else if ((pedantic || warn_pointer_arith)
2477 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2478 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2480 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2481 pedwarn ("wrong type argument to increment");
2483 pedwarn ("wrong type argument to decrement");
2486 inc = c_size_in_bytes (TREE_TYPE (result_type));
2489 inc = integer_one_node;
2491 inc = convert (argtype, inc);
2493 /* Complain about anything else that is not a true lvalue. */
2494 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2495 || code == POSTINCREMENT_EXPR)
2496 ? "invalid lvalue in increment"
2497 : "invalid lvalue in decrement")))
2498 return error_mark_node;
2500 /* Report a read-only lvalue. */
2501 if (TREE_READONLY (arg))
2502 readonly_error (arg,
2503 ((code == PREINCREMENT_EXPR
2504 || code == POSTINCREMENT_EXPR)
2505 ? "increment" : "decrement"));
2507 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2508 val = boolean_increment (code, arg);
2510 val = build (code, TREE_TYPE (arg), arg, inc);
2511 TREE_SIDE_EFFECTS (val) = 1;
2512 val = convert (result_type, val);
2513 if (TREE_CODE (val) != code)
2514 TREE_NO_WARNING (val) = 1;
2519 /* Note that this operation never does default_conversion. */
2521 /* Let &* cancel out to simplify resulting code. */
2522 if (TREE_CODE (arg) == INDIRECT_REF)
2524 /* Don't let this be an lvalue. */
2525 if (lvalue_p (TREE_OPERAND (arg, 0)))
2526 return non_lvalue (TREE_OPERAND (arg, 0));
2527 return TREE_OPERAND (arg, 0);
2530 /* For &x[y], return x+y */
2531 if (TREE_CODE (arg) == ARRAY_REF)
2533 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2534 return error_mark_node;
2535 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2536 TREE_OPERAND (arg, 1), 1);
2539 /* Anything not already handled and not a true memory reference
2540 or a non-lvalue array is an error. */
2541 else if (typecode != FUNCTION_TYPE && !flag
2542 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2543 return error_mark_node;
2545 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2546 argtype = TREE_TYPE (arg);
2548 /* If the lvalue is const or volatile, merge that into the type
2549 to which the address will point. Note that you can't get a
2550 restricted pointer by taking the address of something, so we
2551 only have to deal with `const' and `volatile' here. */
2552 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2553 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2554 argtype = c_build_type_variant (argtype,
2555 TREE_READONLY (arg),
2556 TREE_THIS_VOLATILE (arg));
2558 argtype = build_pointer_type (argtype);
2560 if (!c_mark_addressable (arg))
2561 return error_mark_node;
2566 if (TREE_CODE (arg) == COMPONENT_REF)
2568 tree field = TREE_OPERAND (arg, 1);
2570 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2572 if (DECL_C_BIT_FIELD (field))
2574 error ("attempt to take address of bit-field structure member `%s'",
2575 IDENTIFIER_POINTER (DECL_NAME (field)));
2576 return error_mark_node;
2579 addr = fold (build (PLUS_EXPR, argtype,
2580 convert (argtype, addr),
2581 convert (argtype, byte_position (field))));
2584 addr = build1 (code, argtype, arg);
2594 argtype = TREE_TYPE (arg);
2595 val = build1 (code, argtype, arg);
2596 return require_constant_value ? fold_initializer (val) : fold (val);
2599 /* Return nonzero if REF is an lvalue valid for this language.
2600 Lvalues can be assigned, unless their type has TYPE_READONLY.
2601 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2606 enum tree_code code = TREE_CODE (ref);
2613 return lvalue_p (TREE_OPERAND (ref, 0));
2615 case COMPOUND_LITERAL_EXPR:
2625 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2626 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2630 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2637 /* Return nonzero if REF is an lvalue valid for this language;
2638 otherwise, print an error message and return zero. */
2641 lvalue_or_else (tree ref, const char *msgid)
2643 int win = lvalue_p (ref);
2646 error ("%s", msgid);
2652 /* Warn about storing in something that is `const'. */
2655 readonly_error (tree arg, const char *msgid)
2657 if (TREE_CODE (arg) == COMPONENT_REF)
2659 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2660 readonly_error (TREE_OPERAND (arg, 0), msgid);
2662 error ("%s of read-only member `%s'", _(msgid),
2663 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2665 else if (TREE_CODE (arg) == VAR_DECL)
2666 error ("%s of read-only variable `%s'", _(msgid),
2667 IDENTIFIER_POINTER (DECL_NAME (arg)));
2669 error ("%s of read-only location", _(msgid));
2672 /* Mark EXP saying that we need to be able to take the
2673 address of it; it should not be allocated in a register.
2674 Returns true if successful. */
2677 c_mark_addressable (tree exp)
2682 switch (TREE_CODE (x))
2685 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2687 error ("cannot take address of bit-field `%s'",
2688 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2692 /* ... fall through ... */
2698 x = TREE_OPERAND (x, 0);
2701 case COMPOUND_LITERAL_EXPR:
2703 TREE_ADDRESSABLE (x) = 1;
2710 if (C_DECL_REGISTER (x)
2711 && DECL_NONLOCAL (x))
2713 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2715 error ("global register variable `%s' used in nested function",
2716 IDENTIFIER_POINTER (DECL_NAME (x)));
2719 pedwarn ("register variable `%s' used in nested function",
2720 IDENTIFIER_POINTER (DECL_NAME (x)));
2722 else if (C_DECL_REGISTER (x))
2724 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2726 error ("address of global register variable `%s' requested",
2727 IDENTIFIER_POINTER (DECL_NAME (x)));
2731 pedwarn ("address of register variable `%s' requested",
2732 IDENTIFIER_POINTER (DECL_NAME (x)));
2734 put_var_into_stack (x, /*rescan=*/true);
2738 TREE_ADDRESSABLE (x) = 1;
2745 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2748 build_conditional_expr (tree ifexp, tree op1, tree op2)
2752 enum tree_code code1;
2753 enum tree_code code2;
2754 tree result_type = NULL;
2755 tree orig_op1 = op1, orig_op2 = op2;
2757 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2759 /* Promote both alternatives. */
2761 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2762 op1 = default_conversion (op1);
2763 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2764 op2 = default_conversion (op2);
2766 if (TREE_CODE (ifexp) == ERROR_MARK
2767 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2768 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2769 return error_mark_node;
2771 type1 = TREE_TYPE (op1);
2772 code1 = TREE_CODE (type1);
2773 type2 = TREE_TYPE (op2);
2774 code2 = TREE_CODE (type2);
2776 /* C90 does not permit non-lvalue arrays in conditional expressions.
2777 In C99 they will be pointers by now. */
2778 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2780 error ("non-lvalue array in conditional expression");
2781 return error_mark_node;
2784 /* Quickly detect the usual case where op1 and op2 have the same type
2786 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2789 result_type = type1;
2791 result_type = TYPE_MAIN_VARIANT (type1);
2793 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2794 || code1 == COMPLEX_TYPE)
2795 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2796 || code2 == COMPLEX_TYPE))
2798 result_type = common_type (type1, type2);
2800 /* If -Wsign-compare, warn here if type1 and type2 have
2801 different signedness. We'll promote the signed to unsigned
2802 and later code won't know it used to be different.
2803 Do this check on the original types, so that explicit casts
2804 will be considered, but default promotions won't. */
2805 if (warn_sign_compare && !skip_evaluation)
2807 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2808 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2810 if (unsigned_op1 ^ unsigned_op2)
2812 /* Do not warn if the result type is signed, since the
2813 signed type will only be chosen if it can represent
2814 all the values of the unsigned type. */
2815 if (! TYPE_UNSIGNED (result_type))
2817 /* Do not warn if the signed quantity is an unsuffixed
2818 integer literal (or some static constant expression
2819 involving such literals) and it is non-negative. */
2820 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2821 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2824 warning ("signed and unsigned type in conditional expression");
2828 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2830 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2831 pedwarn ("ISO C forbids conditional expr with only one void side");
2832 result_type = void_type_node;
2834 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2836 if (comp_target_types (type1, type2, 1))
2837 result_type = common_pointer_type (type1, type2);
2838 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2839 && TREE_CODE (orig_op1) != NOP_EXPR)
2840 result_type = qualify_type (type2, type1);
2841 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2842 && TREE_CODE (orig_op2) != NOP_EXPR)
2843 result_type = qualify_type (type1, type2);
2844 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2846 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2847 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2848 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2849 TREE_TYPE (type2)));
2851 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2853 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2854 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2855 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2856 TREE_TYPE (type1)));
2860 pedwarn ("pointer type mismatch in conditional expression");
2861 result_type = build_pointer_type (void_type_node);
2864 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2866 if (! integer_zerop (op2))
2867 pedwarn ("pointer/integer type mismatch in conditional expression");
2870 op2 = null_pointer_node;
2872 result_type = type1;
2874 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2876 if (!integer_zerop (op1))
2877 pedwarn ("pointer/integer type mismatch in conditional expression");
2880 op1 = null_pointer_node;
2882 result_type = type2;
2887 if (flag_cond_mismatch)
2888 result_type = void_type_node;
2891 error ("type mismatch in conditional expression");
2892 return error_mark_node;
2896 /* Merge const and volatile flags of the incoming types. */
2898 = build_type_variant (result_type,
2899 TREE_READONLY (op1) || TREE_READONLY (op2),
2900 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2902 if (result_type != TREE_TYPE (op1))
2903 op1 = convert_and_check (result_type, op1);
2904 if (result_type != TREE_TYPE (op2))
2905 op2 = convert_and_check (result_type, op2);
2907 if (TREE_CODE (ifexp) == INTEGER_CST)
2908 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2910 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2913 /* Given a list of expressions, return a compound expression
2914 that performs them all and returns the value of the last of them. */
2917 build_compound_expr (tree list)
2919 return internal_build_compound_expr (list, TRUE);
2923 internal_build_compound_expr (tree list, int first_p)
2927 if (TREE_CHAIN (list) == 0)
2929 /* Convert arrays and functions to pointers when there
2930 really is a comma operator. */
2933 = default_function_array_conversion (TREE_VALUE (list));
2935 /* Don't let (0, 0) be null pointer constant. */
2936 if (!first_p && integer_zerop (TREE_VALUE (list)))
2937 return non_lvalue (TREE_VALUE (list));
2938 return TREE_VALUE (list);
2941 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2943 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2945 /* The left-hand operand of a comma expression is like an expression
2946 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2947 any side-effects, unless it was explicitly cast to (void). */
2948 if (warn_unused_value
2949 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2950 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2951 warning ("left-hand operand of comma expression has no effect");
2954 /* With -Wunused, we should also warn if the left-hand operand does have
2955 side-effects, but computes a value which is not used. For example, in
2956 `foo() + bar(), baz()' the result of the `+' operator is not used,
2957 so we should issue a warning. */
2958 else if (warn_unused_value)
2959 warn_if_unused_value (TREE_VALUE (list));
2961 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2964 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2967 build_c_cast (tree type, tree expr)
2971 if (type == error_mark_node || expr == error_mark_node)
2972 return error_mark_node;
2974 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2975 only in <protocol> qualifications. But when constructing cast expressions,
2976 the protocols do matter and must be kept around. */
2977 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2978 type = TYPE_MAIN_VARIANT (type);
2980 if (TREE_CODE (type) == ARRAY_TYPE)
2982 error ("cast specifies array type");
2983 return error_mark_node;
2986 if (TREE_CODE (type) == FUNCTION_TYPE)
2988 error ("cast specifies function type");
2989 return error_mark_node;
2992 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2996 if (TREE_CODE (type) == RECORD_TYPE
2997 || TREE_CODE (type) == UNION_TYPE)
2998 pedwarn ("ISO C forbids casting nonscalar to the same type");
3001 else if (TREE_CODE (type) == UNION_TYPE)
3004 value = default_function_array_conversion (value);
3006 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3007 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3008 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3016 pedwarn ("ISO C forbids casts to union type");
3017 t = digest_init (type,
3018 build_constructor (type,
3019 build_tree_list (field, value)),
3021 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3022 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3025 error ("cast to union type from type not present in union");
3026 return error_mark_node;
3032 /* If casting to void, avoid the error that would come
3033 from default_conversion in the case of a non-lvalue array. */
3034 if (type == void_type_node)
3035 return build1 (CONVERT_EXPR, type, value);
3037 /* Convert functions and arrays to pointers,
3038 but don't convert any other types. */
3039 value = default_function_array_conversion (value);
3040 otype = TREE_TYPE (value);
3042 /* Optionally warn about potentially worrisome casts. */
3045 && TREE_CODE (type) == POINTER_TYPE
3046 && TREE_CODE (otype) == POINTER_TYPE)
3048 tree in_type = type;
3049 tree in_otype = otype;
3053 /* Check that the qualifiers on IN_TYPE are a superset of
3054 the qualifiers of IN_OTYPE. The outermost level of
3055 POINTER_TYPE nodes is uninteresting and we stop as soon
3056 as we hit a non-POINTER_TYPE node on either type. */
3059 in_otype = TREE_TYPE (in_otype);
3060 in_type = TREE_TYPE (in_type);
3062 /* GNU C allows cv-qualified function types. 'const'
3063 means the function is very pure, 'volatile' means it
3064 can't return. We need to warn when such qualifiers
3065 are added, not when they're taken away. */
3066 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3067 && TREE_CODE (in_type) == FUNCTION_TYPE)
3068 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3070 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3072 while (TREE_CODE (in_type) == POINTER_TYPE
3073 && TREE_CODE (in_otype) == POINTER_TYPE);
3076 warning ("cast adds new qualifiers to function type");
3079 /* There are qualifiers present in IN_OTYPE that are not
3080 present in IN_TYPE. */
3081 warning ("cast discards qualifiers from pointer target type");
3084 /* Warn about possible alignment problems. */
3085 if (STRICT_ALIGNMENT && warn_cast_align
3086 && TREE_CODE (type) == POINTER_TYPE
3087 && TREE_CODE (otype) == POINTER_TYPE
3088 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3089 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3090 /* Don't warn about opaque types, where the actual alignment
3091 restriction is unknown. */
3092 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3093 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3094 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3095 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3096 warning ("cast increases required alignment of target type");
3098 if (TREE_CODE (type) == INTEGER_TYPE
3099 && TREE_CODE (otype) == POINTER_TYPE
3100 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3101 && !TREE_CONSTANT (value))
3102 warning ("cast from pointer to integer of different size");
3104 if (warn_bad_function_cast
3105 && TREE_CODE (value) == CALL_EXPR
3106 && TREE_CODE (type) != TREE_CODE (otype))
3107 warning ("cast does not match function type");
3109 if (TREE_CODE (type) == POINTER_TYPE
3110 && TREE_CODE (otype) == INTEGER_TYPE
3111 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3112 /* Don't warn about converting any constant. */
3113 && !TREE_CONSTANT (value))
3114 warning ("cast to pointer from integer of different size");
3116 if (TREE_CODE (type) == POINTER_TYPE
3117 && TREE_CODE (otype) == POINTER_TYPE
3118 && TREE_CODE (expr) == ADDR_EXPR
3119 && DECL_P (TREE_OPERAND (expr, 0))
3120 && flag_strict_aliasing && warn_strict_aliasing
3121 && !VOID_TYPE_P (TREE_TYPE (type)))
3123 /* Casting the address of a decl to non void pointer. Warn
3124 if the cast breaks type based aliasing. */
3125 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3126 warning ("type-punning to incomplete type might break strict-aliasing rules");
3129 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3130 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3132 if (!alias_sets_conflict_p (set1, set2))
3133 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3134 else if (warn_strict_aliasing > 1
3135 && !alias_sets_might_conflict_p (set1, set2))
3136 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3140 /* If pedantic, warn for conversions between function and object
3141 pointer types, except for converting a null pointer constant
3142 to function pointer type. */
3144 && TREE_CODE (type) == POINTER_TYPE
3145 && TREE_CODE (otype) == POINTER_TYPE
3146 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3147 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3148 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3151 && TREE_CODE (type) == POINTER_TYPE
3152 && TREE_CODE (otype) == POINTER_TYPE
3153 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3154 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3155 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3156 && TREE_CODE (expr) != NOP_EXPR))
3157 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3160 /* Replace a nonvolatile const static variable with its value. */
3161 if (optimize && TREE_CODE (value) == VAR_DECL)
3162 value = decl_constant_value (value);
3163 value = convert (type, value);
3165 /* Ignore any integer overflow caused by the cast. */
3166 if (TREE_CODE (value) == INTEGER_CST)
3168 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3170 if (TREE_CODE_CLASS (TREE_CODE (ovalue)) == 'c')
3171 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3175 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3176 if (TREE_CODE (value) == INTEGER_CST
3177 && TREE_CODE (expr) == INTEGER_CST
3178 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3179 value = non_lvalue (value);
3181 /* Don't let a cast be an lvalue. */
3183 value = non_lvalue (value);
3188 /* Interpret a cast of expression EXPR to type TYPE. */
3190 c_cast_expr (tree type, tree expr)
3192 int saved_wsp = warn_strict_prototypes;
3194 /* This avoids warnings about unprototyped casts on
3195 integers. E.g. "#define SIG_DFL (void(*)())0". */
3196 if (TREE_CODE (expr) == INTEGER_CST)
3197 warn_strict_prototypes = 0;
3198 type = groktypename (type);
3199 warn_strict_prototypes = saved_wsp;
3201 return build_c_cast (type, expr);
3205 /* Build an assignment expression of lvalue LHS from value RHS.
3206 MODIFYCODE is the code for a binary operator that we use
3207 to combine the old value of LHS with RHS to get the new value.
3208 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3211 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3215 tree lhstype = TREE_TYPE (lhs);
3216 tree olhstype = lhstype;
3218 /* Types that aren't fully specified cannot be used in assignments. */
3219 lhs = require_complete_type (lhs);
3221 /* Avoid duplicate error messages from operands that had errors. */
3222 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3223 return error_mark_node;
3225 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3226 /* Do not use STRIP_NOPS here. We do not want an enumerator
3227 whose value is 0 to count as a null pointer constant. */
3228 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3229 rhs = TREE_OPERAND (rhs, 0);
3233 /* If a binary op has been requested, combine the old LHS value with the RHS
3234 producing the value we should actually store into the LHS. */
3236 if (modifycode != NOP_EXPR)
3238 lhs = stabilize_reference (lhs);
3239 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3242 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3243 return error_mark_node;
3245 /* Warn about storing in something that is `const'. */
3247 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3248 || ((TREE_CODE (lhstype) == RECORD_TYPE
3249 || TREE_CODE (lhstype) == UNION_TYPE)
3250 && C_TYPE_FIELDS_READONLY (lhstype)))
3251 readonly_error (lhs, "assignment");
3253 /* If storing into a structure or union member,
3254 it has probably been given type `int'.
3255 Compute the type that would go with
3256 the actual amount of storage the member occupies. */
3258 if (TREE_CODE (lhs) == COMPONENT_REF
3259 && (TREE_CODE (lhstype) == INTEGER_TYPE
3260 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3261 || TREE_CODE (lhstype) == REAL_TYPE
3262 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3263 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3265 /* If storing in a field that is in actuality a short or narrower than one,
3266 we must store in the field in its actual type. */
3268 if (lhstype != TREE_TYPE (lhs))
3270 lhs = copy_node (lhs);
3271 TREE_TYPE (lhs) = lhstype;
3274 /* Convert new value to destination type. */
3276 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3277 NULL_TREE, NULL_TREE, 0);
3278 if (TREE_CODE (newrhs) == ERROR_MARK)
3279 return error_mark_node;
3283 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3284 TREE_SIDE_EFFECTS (result) = 1;
3286 /* If we got the LHS in a different type for storing in,
3287 convert the result back to the nominal type of LHS
3288 so that the value we return always has the same type
3289 as the LHS argument. */
3291 if (olhstype == TREE_TYPE (result))
3293 return convert_for_assignment (olhstype, result, _("assignment"),
3294 NULL_TREE, NULL_TREE, 0);
3297 /* Convert value RHS to type TYPE as preparation for an assignment
3298 to an lvalue of type TYPE.
3299 The real work of conversion is done by `convert'.
3300 The purpose of this function is to generate error messages
3301 for assignments that are not allowed in C.
3302 ERRTYPE is a string to use in error messages:
3303 "assignment", "return", etc. If it is null, this is parameter passing
3304 for a function call (and different error messages are output).
3306 FUNNAME is the name of the function being called,
3307 as an IDENTIFIER_NODE, or null.
3308 PARMNUM is the number of the argument, for printing in error messages. */
3311 convert_for_assignment (tree type, tree rhs, const char *errtype,
3312 tree fundecl, tree funname, int parmnum)
3314 enum tree_code codel = TREE_CODE (type);
3316 enum tree_code coder;
3318 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3319 /* Do not use STRIP_NOPS here. We do not want an enumerator
3320 whose value is 0 to count as a null pointer constant. */
3321 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3322 rhs = TREE_OPERAND (rhs, 0);
3324 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3325 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3326 rhs = default_conversion (rhs);
3327 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3328 rhs = decl_constant_value_for_broken_optimization (rhs);
3330 rhstype = TREE_TYPE (rhs);
3331 coder = TREE_CODE (rhstype);
3333 if (coder == ERROR_MARK)
3334 return error_mark_node;
3336 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3338 overflow_warning (rhs);
3339 /* Check for Objective-C protocols. This will automatically
3340 issue a warning if there are protocol violations. No need to
3341 use the return value. */
3342 if (c_dialect_objc ())
3343 objc_comptypes (type, rhstype, 0);
3347 if (coder == VOID_TYPE)
3349 error ("void value not ignored as it ought to be");
3350 return error_mark_node;
3352 /* A type converts to a reference to it.
3353 This code doesn't fully support references, it's just for the
3354 special case of va_start and va_copy. */
3355 if (codel == REFERENCE_TYPE
3356 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3358 if (!lvalue_p (rhs))
3360 error ("cannot pass rvalue to reference parameter");
3361 return error_mark_node;
3363 if (!c_mark_addressable (rhs))
3364 return error_mark_node;
3365 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3367 /* We already know that these two types are compatible, but they
3368 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3369 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3370 likely to be va_list, a typedef to __builtin_va_list, which
3371 is different enough that it will cause problems later. */
3372 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3373 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3375 rhs = build1 (NOP_EXPR, type, rhs);
3378 /* Some types can interconvert without explicit casts. */
3379 else if (codel == VECTOR_TYPE
3380 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3381 return convert (type, rhs);
3382 /* Arithmetic types all interconvert, and enum is treated like int. */
3383 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3384 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3385 || codel == BOOLEAN_TYPE)
3386 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3387 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3388 || coder == BOOLEAN_TYPE))
3389 return convert_and_check (type, rhs);
3391 /* Conversion to a transparent union from its member types.
3392 This applies only to function arguments. */
3393 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3396 tree marginal_memb_type = 0;
3398 for (memb_types = TYPE_FIELDS (type); memb_types;
3399 memb_types = TREE_CHAIN (memb_types))
3401 tree memb_type = TREE_TYPE (memb_types);
3403 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3404 TYPE_MAIN_VARIANT (rhstype)))
3407 if (TREE_CODE (memb_type) != POINTER_TYPE)
3410 if (coder == POINTER_TYPE)
3412 tree ttl = TREE_TYPE (memb_type);
3413 tree ttr = TREE_TYPE (rhstype);
3415 /* Any non-function converts to a [const][volatile] void *
3416 and vice versa; otherwise, targets must be the same.
3417 Meanwhile, the lhs target must have all the qualifiers of
3419 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3420 || comp_target_types (memb_type, rhstype, 0))
3422 /* If this type won't generate any warnings, use it. */
3423 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3424 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3425 && TREE_CODE (ttl) == FUNCTION_TYPE)
3426 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3427 == TYPE_QUALS (ttr))
3428 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3429 == TYPE_QUALS (ttl))))
3432 /* Keep looking for a better type, but remember this one. */
3433 if (! marginal_memb_type)
3434 marginal_memb_type = memb_type;
3438 /* Can convert integer zero to any pointer type. */
3439 if (integer_zerop (rhs)
3440 || (TREE_CODE (rhs) == NOP_EXPR
3441 && integer_zerop (TREE_OPERAND (rhs, 0))))
3443 rhs = null_pointer_node;
3448 if (memb_types || marginal_memb_type)
3452 /* We have only a marginally acceptable member type;
3453 it needs a warning. */
3454 tree ttl = TREE_TYPE (marginal_memb_type);
3455 tree ttr = TREE_TYPE (rhstype);
3457 /* Const and volatile mean something different for function
3458 types, so the usual warnings are not appropriate. */
3459 if (TREE_CODE (ttr) == FUNCTION_TYPE
3460 && TREE_CODE (ttl) == FUNCTION_TYPE)
3462 /* Because const and volatile on functions are
3463 restrictions that say the function will not do
3464 certain things, it is okay to use a const or volatile
3465 function where an ordinary one is wanted, but not
3467 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3468 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3469 errtype, funname, parmnum);
3471 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3472 warn_for_assignment ("%s discards qualifiers from pointer target type",
3477 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3478 pedwarn ("ISO C prohibits argument conversion to union type");
3480 return build1 (NOP_EXPR, type, rhs);
3484 /* Conversions among pointers */
3485 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3486 && (coder == codel))
3488 tree ttl = TREE_TYPE (type);
3489 tree ttr = TREE_TYPE (rhstype);
3490 bool is_opaque_pointer;
3491 int target_cmp = 0; /* Cache comp_target_types () result. */
3493 /* Opaque pointers are treated like void pointers. */
3494 is_opaque_pointer = (targetm.vector_opaque_p (type)
3495 || targetm.vector_opaque_p (rhstype))
3496 && TREE_CODE (ttl) == VECTOR_TYPE
3497 && TREE_CODE (ttr) == VECTOR_TYPE;
3499 /* Any non-function converts to a [const][volatile] void *
3500 and vice versa; otherwise, targets must be the same.
3501 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3502 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3503 || (target_cmp = comp_target_types (type, rhstype, 0))
3504 || is_opaque_pointer
3505 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3506 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3509 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3512 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3513 which are not ANSI null ptr constants. */
3514 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3515 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3516 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3517 errtype, funname, parmnum);
3518 /* Const and volatile mean something different for function types,
3519 so the usual warnings are not appropriate. */
3520 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3521 && TREE_CODE (ttl) != FUNCTION_TYPE)
3523 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3524 warn_for_assignment ("%s discards qualifiers from pointer target type",
3525 errtype, funname, parmnum);
3526 /* If this is not a case of ignoring a mismatch in signedness,
3528 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3531 /* If there is a mismatch, do warn. */
3533 warn_for_assignment ("pointer targets in %s differ in signedness",
3534 errtype, funname, parmnum);
3536 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3537 && TREE_CODE (ttr) == FUNCTION_TYPE)
3539 /* Because const and volatile on functions are restrictions
3540 that say the function will not do certain things,
3541 it is okay to use a const or volatile function
3542 where an ordinary one is wanted, but not vice-versa. */
3543 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3544 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3545 errtype, funname, parmnum);
3549 warn_for_assignment ("%s from incompatible pointer type",
3550 errtype, funname, parmnum);
3551 return convert (type, rhs);
3553 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3555 error ("invalid use of non-lvalue array");
3556 return error_mark_node;
3558 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3560 /* An explicit constant 0 can convert to a pointer,
3561 or one that results from arithmetic, even including
3562 a cast to integer type. */
3563 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3565 ! (TREE_CODE (rhs) == NOP_EXPR
3566 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3567 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3568 && integer_zerop (TREE_OPERAND (rhs, 0))))
3569 warn_for_assignment ("%s makes pointer from integer without a cast",
3570 errtype, funname, parmnum);
3572 return convert (type, rhs);
3574 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3576 warn_for_assignment ("%s makes integer from pointer without a cast",
3577 errtype, funname, parmnum);
3578 return convert (type, rhs);
3580 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3581 return convert (type, rhs);
3587 tree selector = objc_message_selector ();
3589 if (selector && parmnum > 2)
3590 error ("incompatible type for argument %d of `%s'",
3591 parmnum - 2, IDENTIFIER_POINTER (selector));
3593 error ("incompatible type for argument %d of `%s'",
3594 parmnum, IDENTIFIER_POINTER (funname));
3597 error ("incompatible type for argument %d of indirect function call",
3601 error ("incompatible types in %s", errtype);
3603 return error_mark_node;
3606 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3607 is used for error and waring reporting and indicates which argument
3608 is being processed. */
3611 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3615 /* If FN was prototyped, the value has been converted already
3616 in convert_arguments. */
3617 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3620 type = TREE_TYPE (parm);
3621 ret = convert_for_assignment (type, value,
3622 (char *) 0 /* arg passing */, fn,
3623 DECL_NAME (fn), argnum);
3624 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3625 && INTEGRAL_TYPE_P (type)
3626 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3627 ret = default_conversion (ret);
3631 /* Print a warning using MSGID.
3632 It gets OPNAME as its one parameter.
3633 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3634 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3635 FUNCTION and ARGNUM are handled specially if we are building an
3636 Objective-C selector. */
3639 warn_for_assignment (const char *msgid, const char *opname, tree function,
3644 tree selector = objc_message_selector ();
3647 if (selector && argnum > 2)
3649 function = selector;
3656 /* Function name is known; supply it. */
3657 const char *const argstring = _("passing arg of `%s'");
3658 new_opname = alloca (IDENTIFIER_LENGTH (function)
3659 + strlen (argstring) + 1 + 1);
3660 sprintf (new_opname, argstring,
3661 IDENTIFIER_POINTER (function));
3665 /* Function name unknown (call through ptr). */
3666 const char *const argnofun = _("passing arg of pointer to function");
3667 new_opname = alloca (strlen (argnofun) + 1 + 1);
3668 sprintf (new_opname, argnofun);
3673 /* Function name is known; supply it. */
3674 const char *const argstring = _("passing arg %d of `%s'");
3675 new_opname = alloca (IDENTIFIER_LENGTH (function)
3676 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3677 sprintf (new_opname, argstring, argnum,
3678 IDENTIFIER_POINTER (function));
3682 /* Function name unknown (call through ptr); just give arg number. */
3683 const char *const argnofun = _("passing arg %d of pointer to function");
3684 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3685 sprintf (new_opname, argnofun, argnum);
3687 opname = new_opname;
3689 pedwarn (msgid, opname);
3692 /* If VALUE is a compound expr all of whose expressions are constant, then
3693 return its value. Otherwise, return error_mark_node.
3695 This is for handling COMPOUND_EXPRs as initializer elements
3696 which is allowed with a warning when -pedantic is specified. */
3699 valid_compound_expr_initializer (tree value, tree endtype)
3701 if (TREE_CODE (value) == COMPOUND_EXPR)
3703 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3705 return error_mark_node;
3706 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3709 else if (! TREE_CONSTANT (value)
3710 && ! initializer_constant_valid_p (value, endtype))
3711 return error_mark_node;
3716 /* Perform appropriate conversions on the initial value of a variable,
3717 store it in the declaration DECL,
3718 and print any error messages that are appropriate.
3719 If the init is invalid, store an ERROR_MARK. */
3722 store_init_value (tree decl, tree init)
3726 /* If variable's type was invalidly declared, just ignore it. */
3728 type = TREE_TYPE (decl);
3729 if (TREE_CODE (type) == ERROR_MARK)
3732 /* Digest the specified initializer into an expression. */
3734 value = digest_init (type, init, TREE_STATIC (decl));
3736 /* Store the expression if valid; else report error. */
3738 if (warn_traditional && !in_system_header
3739 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3740 warning ("traditional C rejects automatic aggregate initialization");
3742 DECL_INITIAL (decl) = value;
3744 /* ANSI wants warnings about out-of-range constant initializers. */
3745 STRIP_TYPE_NOPS (value);
3746 constant_expression_warning (value);
3748 /* Check if we need to set array size from compound literal size. */
3749 if (TREE_CODE (type) == ARRAY_TYPE
3750 && TYPE_DOMAIN (type) == 0
3751 && value != error_mark_node)
3753 tree inside_init = init;
3755 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3756 inside_init = TREE_OPERAND (init, 0);
3757 inside_init = fold (inside_init);
3759 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3761 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3763 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3765 /* For int foo[] = (int [3]){1}; we need to set array size
3766 now since later on array initializer will be just the
3767 brace enclosed list of the compound literal. */
3768 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3770 layout_decl (decl, 0);
3776 /* Methods for storing and printing names for error messages. */
3778 /* Implement a spelling stack that allows components of a name to be pushed
3779 and popped. Each element on the stack is this structure. */
3791 #define SPELLING_STRING 1
3792 #define SPELLING_MEMBER 2
3793 #define SPELLING_BOUNDS 3
3795 static struct spelling *spelling; /* Next stack element (unused). */
3796 static struct spelling *spelling_base; /* Spelling stack base. */
3797 static int spelling_size; /* Size of the spelling stack. */
3799 /* Macros to save and restore the spelling stack around push_... functions.
3800 Alternative to SAVE_SPELLING_STACK. */
3802 #define SPELLING_DEPTH() (spelling - spelling_base)
3803 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3805 /* Push an element on the spelling stack with type KIND and assign VALUE
3808 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3810 int depth = SPELLING_DEPTH (); \
3812 if (depth >= spelling_size) \
3814 spelling_size += 10; \
3815 if (spelling_base == 0) \
3816 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3818 spelling_base = xrealloc (spelling_base, \
3819 spelling_size * sizeof (struct spelling)); \
3820 RESTORE_SPELLING_DEPTH (depth); \
3823 spelling->kind = (KIND); \
3824 spelling->MEMBER = (VALUE); \
3828 /* Push STRING on the stack. Printed literally. */
3831 push_string (const char *string)
3833 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3836 /* Push a member name on the stack. Printed as '.' STRING. */
3839 push_member_name (tree decl)
3841 const char *const string
3842 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3843 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3846 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3849 push_array_bounds (int bounds)
3851 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3854 /* Compute the maximum size in bytes of the printed spelling. */
3857 spelling_length (void)
3862 for (p = spelling_base; p < spelling; p++)
3864 if (p->kind == SPELLING_BOUNDS)
3867 size += strlen (p->u.s) + 1;
3873 /* Print the spelling to BUFFER and return it. */
3876 print_spelling (char *buffer)
3881 for (p = spelling_base; p < spelling; p++)
3882 if (p->kind == SPELLING_BOUNDS)
3884 sprintf (d, "[%d]", p->u.i);
3890 if (p->kind == SPELLING_MEMBER)
3892 for (s = p->u.s; (*d = *s++); d++)
3899 /* Issue an error message for a bad initializer component.
3900 MSGID identifies the message.
3901 The component name is taken from the spelling stack. */
3904 error_init (const char *msgid)
3908 error ("%s", _(msgid));
3909 ofwhat = print_spelling (alloca (spelling_length () + 1));
3911 error ("(near initialization for `%s')", ofwhat);
3914 /* Issue a pedantic warning for a bad initializer component.
3915 MSGID identifies the message.
3916 The component name is taken from the spelling stack. */
3919 pedwarn_init (const char *msgid)
3923 pedwarn ("%s", _(msgid));
3924 ofwhat = print_spelling (alloca (spelling_length () + 1));
3926 pedwarn ("(near initialization for `%s')", ofwhat);
3929 /* Issue a warning for a bad initializer component.
3930 MSGID identifies the message.
3931 The component name is taken from the spelling stack. */
3934 warning_init (const char *msgid)
3938 warning ("%s", _(msgid));
3939 ofwhat = print_spelling (alloca (spelling_length () + 1));
3941 warning ("(near initialization for `%s')", ofwhat);
3944 /* Digest the parser output INIT as an initializer for type TYPE.
3945 Return a C expression of type TYPE to represent the initial value.
3947 REQUIRE_CONSTANT requests an error if non-constant initializers or
3948 elements are seen. */
3951 digest_init (tree type, tree init, int require_constant)
3953 enum tree_code code = TREE_CODE (type);
3954 tree inside_init = init;
3956 if (type == error_mark_node
3957 || init == error_mark_node
3958 || TREE_TYPE (init) == error_mark_node)
3959 return error_mark_node;
3961 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3962 /* Do not use STRIP_NOPS here. We do not want an enumerator
3963 whose value is 0 to count as a null pointer constant. */
3964 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3965 inside_init = TREE_OPERAND (init, 0);
3967 inside_init = fold (inside_init);
3969 /* Initialization of an array of chars from a string constant
3970 optionally enclosed in braces. */
3972 if (code == ARRAY_TYPE)
3974 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3975 if ((typ1 == char_type_node
3976 || typ1 == signed_char_type_node
3977 || typ1 == unsigned_char_type_node
3978 || typ1 == unsigned_wchar_type_node
3979 || typ1 == signed_wchar_type_node)
3980 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3982 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3983 TYPE_MAIN_VARIANT (type)))
3986 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3988 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3990 error_init ("char-array initialized from wide string");
3991 return error_mark_node;
3993 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3995 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3997 error_init ("int-array initialized from non-wide string");
3998 return error_mark_node;
4001 TREE_TYPE (inside_init) = type;
4002 if (TYPE_DOMAIN (type) != 0
4003 && TYPE_SIZE (type) != 0
4004 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4005 /* Subtract 1 (or sizeof (wchar_t))
4006 because it's ok to ignore the terminating null char
4007 that is counted in the length of the constant. */
4008 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4009 TREE_STRING_LENGTH (inside_init)
4010 - ((TYPE_PRECISION (typ1)
4011 != TYPE_PRECISION (char_type_node))
4012 ? (TYPE_PRECISION (wchar_type_node)
4015 pedwarn_init ("initializer-string for array of chars is too long");
4021 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4022 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4023 below and handle as a constructor. */
4024 if (code == VECTOR_TYPE
4025 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4026 && TREE_CONSTANT (inside_init))
4028 if (TREE_CODE (inside_init) == VECTOR_CST
4029 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4030 TYPE_MAIN_VARIANT (type)))
4033 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4036 /* Any type can be initialized
4037 from an expression of the same type, optionally with braces. */
4039 if (inside_init && TREE_TYPE (inside_init) != 0
4040 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4041 TYPE_MAIN_VARIANT (type))
4042 || (code == ARRAY_TYPE
4043 && comptypes (TREE_TYPE (inside_init), type))
4044 || (code == VECTOR_TYPE
4045 && comptypes (TREE_TYPE (inside_init), type))
4046 || (code == POINTER_TYPE
4047 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4048 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4050 || (code == POINTER_TYPE
4051 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4052 && comptypes (TREE_TYPE (inside_init),
4053 TREE_TYPE (type)))))
4055 if (code == POINTER_TYPE)
4057 inside_init = default_function_array_conversion (inside_init);
4059 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4061 error_init ("invalid use of non-lvalue array");
4062 return error_mark_node;
4066 if (code == VECTOR_TYPE)
4067 /* Although the types are compatible, we may require a
4069 inside_init = convert (type, inside_init);
4071 if (require_constant && !flag_isoc99
4072 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4074 /* As an extension, allow initializing objects with static storage
4075 duration with compound literals (which are then treated just as
4076 the brace enclosed list they contain). */
4077 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4078 inside_init = DECL_INITIAL (decl);
4081 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4082 && TREE_CODE (inside_init) != CONSTRUCTOR)
4084 error_init ("array initialized from non-constant array expression");
4085 return error_mark_node;
4088 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4089 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4091 /* Compound expressions can only occur here if -pedantic or
4092 -pedantic-errors is specified. In the later case, we always want
4093 an error. In the former case, we simply want a warning. */
4094 if (require_constant && pedantic
4095 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4098 = valid_compound_expr_initializer (inside_init,
4099 TREE_TYPE (inside_init));
4100 if (inside_init == error_mark_node)
4101 error_init ("initializer element is not constant");
4103 pedwarn_init ("initializer element is not constant");
4104 if (flag_pedantic_errors)
4105 inside_init = error_mark_node;
4107 else if (require_constant
4108 && (!TREE_CONSTANT (inside_init)
4109 /* This test catches things like `7 / 0' which
4110 result in an expression for which TREE_CONSTANT
4111 is true, but which is not actually something
4112 that is a legal constant. We really should not
4113 be using this function, because it is a part of
4114 the back-end. Instead, the expression should
4115 already have been turned into ERROR_MARK_NODE. */
4116 || !initializer_constant_valid_p (inside_init,
4117 TREE_TYPE (inside_init))))
4119 error_init ("initializer element is not constant");
4120 inside_init = error_mark_node;
4126 /* Handle scalar types, including conversions. */
4128 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4129 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4130 || code == VECTOR_TYPE)
4132 /* Note that convert_for_assignment calls default_conversion
4133 for arrays and functions. We must not call it in the
4134 case where inside_init is a null pointer constant. */
4136 = convert_for_assignment (type, init, _("initialization"),
4137 NULL_TREE, NULL_TREE, 0);
4139 if (require_constant && ! TREE_CONSTANT (inside_init))
4141 error_init ("initializer element is not constant");
4142 inside_init = error_mark_node;
4144 else if (require_constant
4145 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4147 error_init ("initializer element is not computable at load time");
4148 inside_init = error_mark_node;
4154 /* Come here only for records and arrays. */
4156 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4158 error_init ("variable-sized object may not be initialized");
4159 return error_mark_node;
4162 error_init ("invalid initializer");
4163 return error_mark_node;
4166 /* Handle initializers that use braces. */
4168 /* Type of object we are accumulating a constructor for.
4169 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4170 static tree constructor_type;
4172 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4174 static tree constructor_fields;
4176 /* For an ARRAY_TYPE, this is the specified index
4177 at which to store the next element we get. */
4178 static tree constructor_index;
4180 /* For an ARRAY_TYPE, this is the maximum index. */
4181 static tree constructor_max_index;
4183 /* For a RECORD_TYPE, this is the first field not yet written out. */
4184 static tree constructor_unfilled_fields;
4186 /* For an ARRAY_TYPE, this is the index of the first element
4187 not yet written out. */
4188 static tree constructor_unfilled_index;
4190 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4191 This is so we can generate gaps between fields, when appropriate. */
4192 static tree constructor_bit_index;
4194 /* If we are saving up the elements rather than allocating them,
4195 this is the list of elements so far (in reverse order,
4196 most recent first). */
4197 static tree constructor_elements;
4199 /* 1 if constructor should be incrementally stored into a constructor chain,
4200 0 if all the elements should be kept in AVL tree. */
4201 static int constructor_incremental;
4203 /* 1 if so far this constructor's elements are all compile-time constants. */
4204 static int constructor_constant;
4206 /* 1 if so far this constructor's elements are all valid address constants. */
4207 static int constructor_simple;
4209 /* 1 if this constructor is erroneous so far. */
4210 static int constructor_erroneous;
4212 /* Structure for managing pending initializer elements, organized as an
4217 struct init_node *left, *right;
4218 struct init_node *parent;
4224 /* Tree of pending elements at this constructor level.
4225 These are elements encountered out of order
4226 which belong at places we haven't reached yet in actually
4228 Will never hold tree nodes across GC runs. */
4229 static struct init_node *constructor_pending_elts;
4231 /* The SPELLING_DEPTH of this constructor. */
4232 static int constructor_depth;
4234 /* 0 if implicitly pushing constructor levels is allowed. */
4235 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4237 /* DECL node for which an initializer is being read.
4238 0 means we are reading a constructor expression
4239 such as (struct foo) {...}. */
4240 static tree constructor_decl;
4242 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4243 static const char *constructor_asmspec;
4245 /* Nonzero if this is an initializer for a top-level decl. */
4246 static int constructor_top_level;
4248 /* Nonzero if there were any member designators in this initializer. */
4249 static int constructor_designated;
4251 /* Nesting depth of designator list. */
4252 static int designator_depth;
4254 /* Nonzero if there were diagnosed errors in this designator list. */
4255 static int designator_errorneous;
4258 /* This stack has a level for each implicit or explicit level of
4259 structuring in the initializer, including the outermost one. It
4260 saves the values of most of the variables above. */
4262 struct constructor_range_stack;
4264 struct constructor_stack
4266 struct constructor_stack *next;
4271 tree unfilled_index;
4272 tree unfilled_fields;
4275 struct init_node *pending_elts;
4278 /* If nonzero, this value should replace the entire
4279 constructor at this level. */
4280 tree replacement_value;
4281 struct constructor_range_stack *range_stack;
4291 struct constructor_stack *constructor_stack;
4293 /* This stack represents designators from some range designator up to
4294 the last designator in the list. */
4296 struct constructor_range_stack
4298 struct constructor_range_stack *next, *prev;
4299 struct constructor_stack *stack;
4306 struct constructor_range_stack *constructor_range_stack;
4308 /* This stack records separate initializers that are nested.
4309 Nested initializers can't happen in ANSI C, but GNU C allows them
4310 in cases like { ... (struct foo) { ... } ... }. */
4312 struct initializer_stack
4314 struct initializer_stack *next;
4316 const char *asmspec;
4317 struct constructor_stack *constructor_stack;
4318 struct constructor_range_stack *constructor_range_stack;
4320 struct spelling *spelling;
4321 struct spelling *spelling_base;
4324 char require_constant_value;
4325 char require_constant_elements;
4328 struct initializer_stack *initializer_stack;
4330 /* Prepare to parse and output the initializer for variable DECL. */
4333 start_init (tree decl, tree asmspec_tree, int top_level)
4336 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4337 const char *asmspec = 0;
4340 asmspec = TREE_STRING_POINTER (asmspec_tree);
4342 p->decl = constructor_decl;
4343 p->asmspec = constructor_asmspec;
4344 p->require_constant_value = require_constant_value;
4345 p->require_constant_elements = require_constant_elements;
4346 p->constructor_stack = constructor_stack;
4347 p->constructor_range_stack = constructor_range_stack;
4348 p->elements = constructor_elements;
4349 p->spelling = spelling;
4350 p->spelling_base = spelling_base;
4351 p->spelling_size = spelling_size;
4352 p->top_level = constructor_top_level;
4353 p->next = initializer_stack;
4354 initializer_stack = p;
4356 constructor_decl = decl;
4357 constructor_asmspec = asmspec;
4358 constructor_designated = 0;
4359 constructor_top_level = top_level;
4363 require_constant_value = TREE_STATIC (decl);
4364 require_constant_elements
4365 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4366 /* For a scalar, you can always use any value to initialize,
4367 even within braces. */
4368 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4369 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4370 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4371 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4372 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4376 require_constant_value = 0;
4377 require_constant_elements = 0;
4378 locus = "(anonymous)";
4381 constructor_stack = 0;
4382 constructor_range_stack = 0;
4384 missing_braces_mentioned = 0;
4388 RESTORE_SPELLING_DEPTH (0);
4391 push_string (locus);
4397 struct initializer_stack *p = initializer_stack;
4399 /* Free the whole constructor stack of this initializer. */
4400 while (constructor_stack)
4402 struct constructor_stack *q = constructor_stack;
4403 constructor_stack = q->next;
4407 if (constructor_range_stack)
4410 /* Pop back to the data of the outer initializer (if any). */
4411 free (spelling_base);
4413 constructor_decl = p->decl;
4414 constructor_asmspec = p->asmspec;
4415 require_constant_value = p->require_constant_value;
4416 require_constant_elements = p->require_constant_elements;
4417 constructor_stack = p->constructor_stack;
4418 constructor_range_stack = p->constructor_range_stack;
4419 constructor_elements = p->elements;
4420 spelling = p->spelling;
4421 spelling_base = p->spelling_base;
4422 spelling_size = p->spelling_size;
4423 constructor_top_level = p->top_level;
4424 initializer_stack = p->next;
4428 /* Call here when we see the initializer is surrounded by braces.
4429 This is instead of a call to push_init_level;
4430 it is matched by a call to pop_init_level.
4432 TYPE is the type to initialize, for a constructor expression.
4433 For an initializer for a decl, TYPE is zero. */
4436 really_start_incremental_init (tree type)
4438 struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
4441 type = TREE_TYPE (constructor_decl);
4443 if (targetm.vector_opaque_p (type))
4444 error ("opaque vector types cannot be initialized");
4446 p->type = constructor_type;
4447 p->fields = constructor_fields;
4448 p->index = constructor_index;
4449 p->max_index = constructor_max_index;
4450 p->unfilled_index = constructor_unfilled_index;
4451 p->unfilled_fields = constructor_unfilled_fields;
4452 p->bit_index = constructor_bit_index;
4453 p->elements = constructor_elements;
4454 p->constant = constructor_constant;
4455 p->simple = constructor_simple;
4456 p->erroneous = constructor_erroneous;
4457 p->pending_elts = constructor_pending_elts;
4458 p->depth = constructor_depth;
4459 p->replacement_value = 0;
4463 p->incremental = constructor_incremental;
4464 p->designated = constructor_designated;
4466 constructor_stack = p;
4468 constructor_constant = 1;
4469 constructor_simple = 1;
4470 constructor_depth = SPELLING_DEPTH ();
4471 constructor_elements = 0;
4472 constructor_pending_elts = 0;
4473 constructor_type = type;
4474 constructor_incremental = 1;
4475 constructor_designated = 0;
4476 designator_depth = 0;
4477 designator_errorneous = 0;
4479 if (TREE_CODE (constructor_type) == RECORD_TYPE
4480 || TREE_CODE (constructor_type) == UNION_TYPE)
4482 constructor_fields = TYPE_FIELDS (constructor_type);
4483 /* Skip any nameless bit fields at the beginning. */
4484 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4485 && DECL_NAME (constructor_fields) == 0)
4486 constructor_fields = TREE_CHAIN (constructor_fields);
4488 constructor_unfilled_fields = constructor_fields;
4489 constructor_bit_index = bitsize_zero_node;
4491 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4493 if (TYPE_DOMAIN (constructor_type))
4495 constructor_max_index
4496 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4498 /* Detect non-empty initializations of zero-length arrays. */
4499 if (constructor_max_index == NULL_TREE
4500 && TYPE_SIZE (constructor_type))
4501 constructor_max_index = build_int_2 (-1, -1);
4503 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4504 to initialize VLAs will cause a proper error; avoid tree
4505 checking errors as well by setting a safe value. */
4506 if (constructor_max_index
4507 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4508 constructor_max_index = build_int_2 (-1, -1);
4511 = convert (bitsizetype,
4512 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4515 constructor_index = bitsize_zero_node;
4517 constructor_unfilled_index = constructor_index;
4519 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4521 /* Vectors are like simple fixed-size arrays. */
4522 constructor_max_index =
4523 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4524 constructor_index = convert (bitsizetype, bitsize_zero_node);
4525 constructor_unfilled_index = constructor_index;
4529 /* Handle the case of int x = {5}; */
4530 constructor_fields = constructor_type;
4531 constructor_unfilled_fields = constructor_type;
4535 /* Push down into a subobject, for initialization.
4536 If this is for an explicit set of braces, IMPLICIT is 0.
4537 If it is because the next element belongs at a lower level,
4538 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4541 push_init_level (int implicit)
4543 struct constructor_stack *p;
4544 tree value = NULL_TREE;
4546 /* If we've exhausted any levels that didn't have braces,
4548 while (constructor_stack->implicit)
4550 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4551 || TREE_CODE (constructor_type) == UNION_TYPE)
4552 && constructor_fields == 0)
4553 process_init_element (pop_init_level (1));
4554 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4555 && constructor_max_index
4556 && tree_int_cst_lt (constructor_max_index, constructor_index))
4557 process_init_element (pop_init_level (1));
4562 /* Unless this is an explicit brace, we need to preserve previous
4566 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4567 || TREE_CODE (constructor_type) == UNION_TYPE)
4568 && constructor_fields)
4569 value = find_init_member (constructor_fields);
4570 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4571 value = find_init_member (constructor_index);
4574 p = xmalloc (sizeof (struct constructor_stack));
4575 p->type = constructor_type;
4576 p->fields = constructor_fields;
4577 p->index = constructor_index;
4578 p->max_index = constructor_max_index;
4579 p->unfilled_index = constructor_unfilled_index;
4580 p->unfilled_fields = constructor_unfilled_fields;
4581 p->bit_index = constructor_bit_index;
4582 p->elements = constructor_elements;
4583 p->constant = constructor_constant;
4584 p->simple = constructor_simple;
4585 p->erroneous = constructor_erroneous;
4586 p->pending_elts = constructor_pending_elts;
4587 p->depth = constructor_depth;
4588 p->replacement_value = 0;
4589 p->implicit = implicit;
4591 p->incremental = constructor_incremental;
4592 p->designated = constructor_designated;
4593 p->next = constructor_stack;
4595 constructor_stack = p;
4597 constructor_constant = 1;
4598 constructor_simple = 1;
4599 constructor_depth = SPELLING_DEPTH ();
4600 constructor_elements = 0;
4601 constructor_incremental = 1;
4602 constructor_designated = 0;
4603 constructor_pending_elts = 0;
4606 p->range_stack = constructor_range_stack;
4607 constructor_range_stack = 0;
4608 designator_depth = 0;
4609 designator_errorneous = 0;
4612 /* Don't die if an entire brace-pair level is superfluous
4613 in the containing level. */
4614 if (constructor_type == 0)
4616 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4617 || TREE_CODE (constructor_type) == UNION_TYPE)
4619 /* Don't die if there are extra init elts at the end. */
4620 if (constructor_fields == 0)
4621 constructor_type = 0;
4624 constructor_type = TREE_TYPE (constructor_fields);
4625 push_member_name (constructor_fields);
4626 constructor_depth++;
4629 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4631 constructor_type = TREE_TYPE (constructor_type);
4632 push_array_bounds (tree_low_cst (constructor_index, 0));
4633 constructor_depth++;
4636 if (constructor_type == 0)
4638 error_init ("extra brace group at end of initializer");
4639 constructor_fields = 0;
4640 constructor_unfilled_fields = 0;
4644 if (value && TREE_CODE (value) == CONSTRUCTOR)
4646 constructor_constant = TREE_CONSTANT (value);
4647 constructor_simple = TREE_STATIC (value);
4648 constructor_elements = CONSTRUCTOR_ELTS (value);
4649 if (constructor_elements
4650 && (TREE_CODE (constructor_type) == RECORD_TYPE
4651 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4652 set_nonincremental_init ();
4655 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4657 missing_braces_mentioned = 1;
4658 warning_init ("missing braces around initializer");
4661 if (TREE_CODE (constructor_type) == RECORD_TYPE
4662 || TREE_CODE (constructor_type) == UNION_TYPE)
4664 constructor_fields = TYPE_FIELDS (constructor_type);
4665 /* Skip any nameless bit fields at the beginning. */
4666 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4667 && DECL_NAME (constructor_fields) == 0)
4668 constructor_fields = TREE_CHAIN (constructor_fields);
4670 constructor_unfilled_fields = constructor_fields;
4671 constructor_bit_index = bitsize_zero_node;
4673 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4675 /* Vectors are like simple fixed-size arrays. */
4676 constructor_max_index =
4677 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4678 constructor_index = convert (bitsizetype, integer_zero_node);
4679 constructor_unfilled_index = constructor_index;
4681 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4683 if (TYPE_DOMAIN (constructor_type))
4685 constructor_max_index
4686 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4688 /* Detect non-empty initializations of zero-length arrays. */
4689 if (constructor_max_index == NULL_TREE
4690 && TYPE_SIZE (constructor_type))
4691 constructor_max_index = build_int_2 (-1, -1);
4693 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4694 to initialize VLAs will cause a proper error; avoid tree
4695 checking errors as well by setting a safe value. */
4696 if (constructor_max_index
4697 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4698 constructor_max_index = build_int_2 (-1, -1);
4701 = convert (bitsizetype,
4702 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4705 constructor_index = bitsize_zero_node;
4707 constructor_unfilled_index = constructor_index;
4708 if (value && TREE_CODE (value) == STRING_CST)
4710 /* We need to split the char/wchar array into individual
4711 characters, so that we don't have to special case it
4713 set_nonincremental_init_from_string (value);
4718 warning_init ("braces around scalar initializer");
4719 constructor_fields = constructor_type;
4720 constructor_unfilled_fields = constructor_type;
4724 /* At the end of an implicit or explicit brace level,
4725 finish up that level of constructor.
4726 If we were outputting the elements as they are read, return 0
4727 from inner levels (process_init_element ignores that),
4728 but return error_mark_node from the outermost level
4729 (that's what we want to put in DECL_INITIAL).
4730 Otherwise, return a CONSTRUCTOR expression. */
4733 pop_init_level (int implicit)
4735 struct constructor_stack *p;
4736 tree constructor = 0;
4740 /* When we come to an explicit close brace,
4741 pop any inner levels that didn't have explicit braces. */
4742 while (constructor_stack->implicit)
4743 process_init_element (pop_init_level (1));
4745 if (constructor_range_stack)
4749 /* Now output all pending elements. */
4750 constructor_incremental = 1;
4751 output_pending_init_elements (1);
4753 p = constructor_stack;
4755 /* Error for initializing a flexible array member, or a zero-length
4756 array member in an inappropriate context. */
4757 if (constructor_type && constructor_fields
4758 && TREE_CODE (constructor_type) == ARRAY_TYPE
4759 && TYPE_DOMAIN (constructor_type)
4760 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4762 /* Silently discard empty initializations. The parser will
4763 already have pedwarned for empty brackets. */
4764 if (integer_zerop (constructor_unfilled_index))
4765 constructor_type = NULL_TREE;
4766 else if (! TYPE_SIZE (constructor_type))
4768 if (constructor_depth > 2)
4769 error_init ("initialization of flexible array member in a nested context");
4771 pedwarn_init ("initialization of a flexible array member");
4773 /* We have already issued an error message for the existence
4774 of a flexible array member not at the end of the structure.
4775 Discard the initializer so that we do not abort later. */
4776 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4777 constructor_type = NULL_TREE;
4780 /* Zero-length arrays are no longer special, so we should no longer
4785 /* Warn when some struct elements are implicitly initialized to zero. */
4788 && TREE_CODE (constructor_type) == RECORD_TYPE
4789 && constructor_unfilled_fields)
4791 /* Do not warn for flexible array members or zero-length arrays. */
4792 while (constructor_unfilled_fields
4793 && (! DECL_SIZE (constructor_unfilled_fields)
4794 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4795 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4797 /* Do not warn if this level of the initializer uses member
4798 designators; it is likely to be deliberate. */
4799 if (constructor_unfilled_fields && !constructor_designated)
4801 push_member_name (constructor_unfilled_fields);
4802 warning_init ("missing initializer");
4803 RESTORE_SPELLING_DEPTH (constructor_depth);
4807 /* Pad out the end of the structure. */
4808 if (p->replacement_value)
4809 /* If this closes a superfluous brace pair,
4810 just pass out the element between them. */
4811 constructor = p->replacement_value;
4812 else if (constructor_type == 0)
4814 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4815 && TREE_CODE (constructor_type) != UNION_TYPE
4816 && TREE_CODE (constructor_type) != ARRAY_TYPE
4817 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4819 /* A nonincremental scalar initializer--just return
4820 the element, after verifying there is just one. */
4821 if (constructor_elements == 0)
4823 if (!constructor_erroneous)
4824 error_init ("empty scalar initializer");
4825 constructor = error_mark_node;
4827 else if (TREE_CHAIN (constructor_elements) != 0)
4829 error_init ("extra elements in scalar initializer");
4830 constructor = TREE_VALUE (constructor_elements);
4833 constructor = TREE_VALUE (constructor_elements);
4837 if (constructor_erroneous)
4838 constructor = error_mark_node;
4841 constructor = build_constructor (constructor_type,
4842 nreverse (constructor_elements));
4843 if (constructor_constant)
4844 TREE_CONSTANT (constructor) = TREE_INVARIANT (constructor) = 1;
4845 if (constructor_constant && constructor_simple)
4846 TREE_STATIC (constructor) = 1;
4850 constructor_type = p->type;
4851 constructor_fields = p->fields;
4852 constructor_index = p->index;
4853 constructor_max_index = p->max_index;
4854 constructor_unfilled_index = p->unfilled_index;
4855 constructor_unfilled_fields = p->unfilled_fields;
4856 constructor_bit_index = p->bit_index;
4857 constructor_elements = p->elements;
4858 constructor_constant = p->constant;
4859 constructor_simple = p->simple;
4860 constructor_erroneous = p->erroneous;
4861 constructor_incremental = p->incremental;
4862 constructor_designated = p->designated;
4863 constructor_pending_elts = p->pending_elts;
4864 constructor_depth = p->depth;
4866 constructor_range_stack = p->range_stack;
4867 RESTORE_SPELLING_DEPTH (constructor_depth);
4869 constructor_stack = p->next;
4872 if (constructor == 0)
4874 if (constructor_stack == 0)
4875 return error_mark_node;
4881 /* Common handling for both array range and field name designators.
4882 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4885 set_designator (int array)
4888 enum tree_code subcode;
4890 /* Don't die if an entire brace-pair level is superfluous
4891 in the containing level. */
4892 if (constructor_type == 0)
4895 /* If there were errors in this designator list already, bail out silently. */
4896 if (designator_errorneous)
4899 if (!designator_depth)
4901 if (constructor_range_stack)
4904 /* Designator list starts at the level of closest explicit
4906 while (constructor_stack->implicit)
4907 process_init_element (pop_init_level (1));
4908 constructor_designated = 1;
4912 if (constructor_no_implicit)
4914 error_init ("initialization designators may not nest");
4918 if (TREE_CODE (constructor_type) == RECORD_TYPE
4919 || TREE_CODE (constructor_type) == UNION_TYPE)
4921 subtype = TREE_TYPE (constructor_fields);
4922 if (subtype != error_mark_node)
4923 subtype = TYPE_MAIN_VARIANT (subtype);
4925 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4927 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4932 subcode = TREE_CODE (subtype);
4933 if (array && subcode != ARRAY_TYPE)
4935 error_init ("array index in non-array initializer");
4938 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4940 error_init ("field name not in record or union initializer");
4944 constructor_designated = 1;
4945 push_init_level (2);
4949 /* If there are range designators in designator list, push a new designator
4950 to constructor_range_stack. RANGE_END is end of such stack range or
4951 NULL_TREE if there is no range designator at this level. */
4954 push_range_stack (tree range_end)
4956 struct constructor_range_stack *p;
4958 p = ggc_alloc (sizeof (struct constructor_range_stack));
4959 p->prev = constructor_range_stack;
4961 p->fields = constructor_fields;
4962 p->range_start = constructor_index;
4963 p->index = constructor_index;
4964 p->stack = constructor_stack;
4965 p->range_end = range_end;
4966 if (constructor_range_stack)
4967 constructor_range_stack->next = p;
4968 constructor_range_stack = p;
4971 /* Within an array initializer, specify the next index to be initialized.
4972 FIRST is that index. If LAST is nonzero, then initialize a range
4973 of indices, running from FIRST through LAST. */
4976 set_init_index (tree first, tree last)
4978 if (set_designator (1))
4981 designator_errorneous = 1;
4983 while ((TREE_CODE (first) == NOP_EXPR
4984 || TREE_CODE (first) == CONVERT_EXPR
4985 || TREE_CODE (first) == NON_LVALUE_EXPR)
4986 && (TYPE_MODE (TREE_TYPE (first))
4987 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
4988 first = TREE_OPERAND (first, 0);
4991 while ((TREE_CODE (last) == NOP_EXPR
4992 || TREE_CODE (last) == CONVERT_EXPR
4993 || TREE_CODE (last) == NON_LVALUE_EXPR)
4994 && (TYPE_MODE (TREE_TYPE (last))
4995 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
4996 last = TREE_OPERAND (last, 0);
4998 if (TREE_CODE (first) != INTEGER_CST)
4999 error_init ("nonconstant array index in initializer");
5000 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5001 error_init ("nonconstant array index in initializer");
5002 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5003 error_init ("array index in non-array initializer");
5004 else if (tree_int_cst_sgn (first) == -1)
5005 error_init ("array index in initializer exceeds array bounds");
5006 else if (constructor_max_index
5007 && tree_int_cst_lt (constructor_max_index, first))
5008 error_init ("array index in initializer exceeds array bounds");
5011 constructor_index = convert (bitsizetype, first);
5015 if (tree_int_cst_equal (first, last))
5017 else if (tree_int_cst_lt (last, first))
5019 error_init ("empty index range in initializer");
5024 last = convert (bitsizetype, last);
5025 if (constructor_max_index != 0
5026 && tree_int_cst_lt (constructor_max_index, last))
5028 error_init ("array index range in initializer exceeds array bounds");
5035 designator_errorneous = 0;
5036 if (constructor_range_stack || last)
5037 push_range_stack (last);
5041 /* Within a struct initializer, specify the next field to be initialized. */
5044 set_init_label (tree fieldname)
5048 if (set_designator (0))
5051 designator_errorneous = 1;
5053 if (TREE_CODE (constructor_type) != RECORD_TYPE
5054 && TREE_CODE (constructor_type) != UNION_TYPE)
5056 error_init ("field name not in record or union initializer");
5060 for (tail = TYPE_FIELDS (constructor_type); tail;
5061 tail = TREE_CHAIN (tail))
5063 if (DECL_NAME (tail) == fieldname)
5068 error ("unknown field `%s' specified in initializer",
5069 IDENTIFIER_POINTER (fieldname));
5072 constructor_fields = tail;
5074 designator_errorneous = 0;
5075 if (constructor_range_stack)
5076 push_range_stack (NULL_TREE);
5080 /* Add a new initializer to the tree of pending initializers. PURPOSE
5081 identifies the initializer, either array index or field in a structure.
5082 VALUE is the value of that index or field. */
5085 add_pending_init (tree purpose, tree value)
5087 struct init_node *p, **q, *r;
5089 q = &constructor_pending_elts;
5092 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5097 if (tree_int_cst_lt (purpose, p->purpose))
5099 else if (tree_int_cst_lt (p->purpose, purpose))
5103 if (TREE_SIDE_EFFECTS (p->value))
5104 warning_init ("initialized field with side-effects overwritten");
5114 bitpos = bit_position (purpose);
5118 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5120 else if (p->purpose != purpose)
5124 if (TREE_SIDE_EFFECTS (p->value))
5125 warning_init ("initialized field with side-effects overwritten");
5132 r = ggc_alloc (sizeof (struct init_node));
5133 r->purpose = purpose;
5144 struct init_node *s;
5148 if (p->balance == 0)
5150 else if (p->balance < 0)
5157 p->left->parent = p;
5174 constructor_pending_elts = r;
5179 struct init_node *t = r->right;
5183 r->right->parent = r;
5188 p->left->parent = p;
5191 p->balance = t->balance < 0;
5192 r->balance = -(t->balance > 0);
5207 constructor_pending_elts = t;
5213 /* p->balance == +1; growth of left side balances the node. */
5218 else /* r == p->right */
5220 if (p->balance == 0)
5221 /* Growth propagation from right side. */
5223 else if (p->balance > 0)
5230 p->right->parent = p;
5247 constructor_pending_elts = r;
5249 else /* r->balance == -1 */
5252 struct init_node *t = r->left;
5256 r->left->parent = r;
5261 p->right->parent = p;
5264 r->balance = (t->balance < 0);
5265 p->balance = -(t->balance > 0);
5280 constructor_pending_elts = t;
5286 /* p->balance == -1; growth of right side balances the node. */
5297 /* Build AVL tree from a sorted chain. */
5300 set_nonincremental_init (void)
5304 if (TREE_CODE (constructor_type) != RECORD_TYPE
5305 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5308 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5309 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5310 constructor_elements = 0;
5311 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5313 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5314 /* Skip any nameless bit fields at the beginning. */
5315 while (constructor_unfilled_fields != 0
5316 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5317 && DECL_NAME (constructor_unfilled_fields) == 0)
5318 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5321 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5323 if (TYPE_DOMAIN (constructor_type))
5324 constructor_unfilled_index
5325 = convert (bitsizetype,
5326 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5328 constructor_unfilled_index = bitsize_zero_node;
5330 constructor_incremental = 0;
5333 /* Build AVL tree from a string constant. */
5336 set_nonincremental_init_from_string (tree str)
5338 tree value, purpose, type;
5339 HOST_WIDE_INT val[2];
5340 const char *p, *end;
5341 int byte, wchar_bytes, charwidth, bitpos;
5343 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5346 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5347 == TYPE_PRECISION (char_type_node))
5349 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5350 == TYPE_PRECISION (wchar_type_node))
5351 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5355 charwidth = TYPE_PRECISION (char_type_node);
5356 type = TREE_TYPE (constructor_type);
5357 p = TREE_STRING_POINTER (str);
5358 end = p + TREE_STRING_LENGTH (str);
5360 for (purpose = bitsize_zero_node;
5361 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5362 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5364 if (wchar_bytes == 1)
5366 val[1] = (unsigned char) *p++;
5373 for (byte = 0; byte < wchar_bytes; byte++)
5375 if (BYTES_BIG_ENDIAN)
5376 bitpos = (wchar_bytes - byte - 1) * charwidth;
5378 bitpos = byte * charwidth;
5379 val[bitpos < HOST_BITS_PER_WIDE_INT]
5380 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5381 << (bitpos % HOST_BITS_PER_WIDE_INT);
5385 if (!TYPE_UNSIGNED (type))
5387 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5388 if (bitpos < HOST_BITS_PER_WIDE_INT)
5390 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5392 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5396 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5401 else if (val[0] & (((HOST_WIDE_INT) 1)
5402 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5403 val[0] |= ((HOST_WIDE_INT) -1)
5404 << (bitpos - HOST_BITS_PER_WIDE_INT);
5407 value = build_int_2 (val[1], val[0]);
5408 TREE_TYPE (value) = type;
5409 add_pending_init (purpose, value);
5412 constructor_incremental = 0;
5415 /* Return value of FIELD in pending initializer or zero if the field was
5416 not initialized yet. */
5419 find_init_member (tree field)
5421 struct init_node *p;
5423 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5425 if (constructor_incremental
5426 && tree_int_cst_lt (field, constructor_unfilled_index))
5427 set_nonincremental_init ();
5429 p = constructor_pending_elts;
5432 if (tree_int_cst_lt (field, p->purpose))
5434 else if (tree_int_cst_lt (p->purpose, field))
5440 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5442 tree bitpos = bit_position (field);
5444 if (constructor_incremental
5445 && (!constructor_unfilled_fields
5446 || tree_int_cst_lt (bitpos,
5447 bit_position (constructor_unfilled_fields))))
5448 set_nonincremental_init ();
5450 p = constructor_pending_elts;
5453 if (field == p->purpose)
5455 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5461 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5463 if (constructor_elements
5464 && TREE_PURPOSE (constructor_elements) == field)
5465 return TREE_VALUE (constructor_elements);
5470 /* "Output" the next constructor element.
5471 At top level, really output it to assembler code now.
5472 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5473 TYPE is the data type that the containing data type wants here.
5474 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5476 PENDING if non-nil means output pending elements that belong
5477 right after this element. (PENDING is normally 1;
5478 it is 0 while outputting pending elements, to avoid recursion.) */
5481 output_init_element (tree value, tree type, tree field, int pending)
5483 if (type == error_mark_node)
5485 constructor_erroneous = 1;
5488 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5489 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5490 && !(TREE_CODE (value) == STRING_CST
5491 && TREE_CODE (type) == ARRAY_TYPE
5492 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5493 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5494 TYPE_MAIN_VARIANT (type))))
5495 value = default_conversion (value);
5497 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5498 && require_constant_value && !flag_isoc99 && pending)
5500 /* As an extension, allow initializing objects with static storage
5501 duration with compound literals (which are then treated just as
5502 the brace enclosed list they contain). */
5503 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5504 value = DECL_INITIAL (decl);
5507 if (value == error_mark_node)
5508 constructor_erroneous = 1;
5509 else if (!TREE_CONSTANT (value))
5510 constructor_constant = 0;
5511 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5512 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5513 || TREE_CODE (constructor_type) == UNION_TYPE)
5514 && DECL_C_BIT_FIELD (field)
5515 && TREE_CODE (value) != INTEGER_CST))
5516 constructor_simple = 0;
5518 if (require_constant_value && ! TREE_CONSTANT (value))
5520 error_init ("initializer element is not constant");
5521 value = error_mark_node;
5523 else if (require_constant_elements
5524 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5525 pedwarn ("initializer element is not computable at load time");
5527 /* If this field is empty (and not at the end of structure),
5528 don't do anything other than checking the initializer. */
5530 && (TREE_TYPE (field) == error_mark_node
5531 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5532 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5533 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5534 || TREE_CHAIN (field)))))
5537 value = digest_init (type, value, require_constant_value);
5538 if (value == error_mark_node)
5540 constructor_erroneous = 1;
5544 /* If this element doesn't come next in sequence,
5545 put it on constructor_pending_elts. */
5546 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5547 && (!constructor_incremental
5548 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5550 if (constructor_incremental
5551 && tree_int_cst_lt (field, constructor_unfilled_index))
5552 set_nonincremental_init ();
5554 add_pending_init (field, value);
5557 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5558 && (!constructor_incremental
5559 || field != constructor_unfilled_fields))
5561 /* We do this for records but not for unions. In a union,
5562 no matter which field is specified, it can be initialized
5563 right away since it starts at the beginning of the union. */
5564 if (constructor_incremental)
5566 if (!constructor_unfilled_fields)
5567 set_nonincremental_init ();
5570 tree bitpos, unfillpos;
5572 bitpos = bit_position (field);
5573 unfillpos = bit_position (constructor_unfilled_fields);
5575 if (tree_int_cst_lt (bitpos, unfillpos))
5576 set_nonincremental_init ();
5580 add_pending_init (field, value);
5583 else if (TREE_CODE (constructor_type) == UNION_TYPE
5584 && constructor_elements)
5586 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5587 warning_init ("initialized field with side-effects overwritten");
5589 /* We can have just one union field set. */
5590 constructor_elements = 0;
5593 /* Otherwise, output this element either to
5594 constructor_elements or to the assembler file. */
5596 if (field && TREE_CODE (field) == INTEGER_CST)
5597 field = copy_node (field);
5598 constructor_elements
5599 = tree_cons (field, value, constructor_elements);
5601 /* Advance the variable that indicates sequential elements output. */
5602 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5603 constructor_unfilled_index
5604 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5606 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5608 constructor_unfilled_fields
5609 = TREE_CHAIN (constructor_unfilled_fields);
5611 /* Skip any nameless bit fields. */
5612 while (constructor_unfilled_fields != 0
5613 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5614 && DECL_NAME (constructor_unfilled_fields) == 0)
5615 constructor_unfilled_fields =
5616 TREE_CHAIN (constructor_unfilled_fields);
5618 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5619 constructor_unfilled_fields = 0;
5621 /* Now output any pending elements which have become next. */
5623 output_pending_init_elements (0);
5626 /* Output any pending elements which have become next.
5627 As we output elements, constructor_unfilled_{fields,index}
5628 advances, which may cause other elements to become next;
5629 if so, they too are output.
5631 If ALL is 0, we return when there are
5632 no more pending elements to output now.
5634 If ALL is 1, we output space as necessary so that
5635 we can output all the pending elements. */
5638 output_pending_init_elements (int all)
5640 struct init_node *elt = constructor_pending_elts;
5645 /* Look through the whole pending tree.
5646 If we find an element that should be output now,
5647 output it. Otherwise, set NEXT to the element
5648 that comes first among those still pending. */
5653 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5655 if (tree_int_cst_equal (elt->purpose,
5656 constructor_unfilled_index))
5657 output_init_element (elt->value,
5658 TREE_TYPE (constructor_type),
5659 constructor_unfilled_index, 0);
5660 else if (tree_int_cst_lt (constructor_unfilled_index,
5663 /* Advance to the next smaller node. */
5668 /* We have reached the smallest node bigger than the
5669 current unfilled index. Fill the space first. */
5670 next = elt->purpose;
5676 /* Advance to the next bigger node. */
5681 /* We have reached the biggest node in a subtree. Find
5682 the parent of it, which is the next bigger node. */
5683 while (elt->parent && elt->parent->right == elt)
5686 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5689 next = elt->purpose;
5695 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5696 || TREE_CODE (constructor_type) == UNION_TYPE)
5698 tree ctor_unfilled_bitpos, elt_bitpos;
5700 /* If the current record is complete we are done. */
5701 if (constructor_unfilled_fields == 0)
5704 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5705 elt_bitpos = bit_position (elt->purpose);
5706 /* We can't compare fields here because there might be empty
5707 fields in between. */
5708 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5710 constructor_unfilled_fields = elt->purpose;
5711 output_init_element (elt->value, TREE_TYPE (elt->purpose),
5714 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5716 /* Advance to the next smaller node. */
5721 /* We have reached the smallest node bigger than the
5722 current unfilled field. Fill the space first. */
5723 next = elt->purpose;
5729 /* Advance to the next bigger node. */
5734 /* We have reached the biggest node in a subtree. Find
5735 the parent of it, which is the next bigger node. */
5736 while (elt->parent && elt->parent->right == elt)
5740 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5741 bit_position (elt->purpose))))
5743 next = elt->purpose;
5751 /* Ordinarily return, but not if we want to output all
5752 and there are elements left. */
5753 if (! (all && next != 0))
5756 /* If it's not incremental, just skip over the gap, so that after
5757 jumping to retry we will output the next successive element. */
5758 if (TREE_CODE (constructor_type) == RECORD_TYPE
5759 || TREE_CODE (constructor_type) == UNION_TYPE)
5760 constructor_unfilled_fields = next;
5761 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5762 constructor_unfilled_index = next;
5764 /* ELT now points to the node in the pending tree with the next
5765 initializer to output. */
5769 /* Add one non-braced element to the current constructor level.
5770 This adjusts the current position within the constructor's type.
5771 This may also start or terminate implicit levels
5772 to handle a partly-braced initializer.
5774 Once this has found the correct level for the new element,
5775 it calls output_init_element. */
5778 process_init_element (tree value)
5780 tree orig_value = value;
5781 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5783 designator_depth = 0;
5784 designator_errorneous = 0;
5786 /* Handle superfluous braces around string cst as in
5787 char x[] = {"foo"}; */
5790 && TREE_CODE (constructor_type) == ARRAY_TYPE
5791 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5792 && integer_zerop (constructor_unfilled_index))
5794 if (constructor_stack->replacement_value)
5795 error_init ("excess elements in char array initializer");
5796 constructor_stack->replacement_value = value;
5800 if (constructor_stack->replacement_value != 0)
5802 error_init ("excess elements in struct initializer");
5806 /* Ignore elements of a brace group if it is entirely superfluous
5807 and has already been diagnosed. */
5808 if (constructor_type == 0)
5811 /* If we've exhausted any levels that didn't have braces,
5813 while (constructor_stack->implicit)
5815 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5816 || TREE_CODE (constructor_type) == UNION_TYPE)
5817 && constructor_fields == 0)
5818 process_init_element (pop_init_level (1));
5819 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5820 && (constructor_max_index == 0
5821 || tree_int_cst_lt (constructor_max_index,
5822 constructor_index)))
5823 process_init_element (pop_init_level (1));
5828 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5829 if (constructor_range_stack)
5831 /* If value is a compound literal and we'll be just using its
5832 content, don't put it into a SAVE_EXPR. */
5833 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5834 || !require_constant_value
5836 value = save_expr (value);
5841 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5844 enum tree_code fieldcode;
5846 if (constructor_fields == 0)
5848 pedwarn_init ("excess elements in struct initializer");
5852 fieldtype = TREE_TYPE (constructor_fields);
5853 if (fieldtype != error_mark_node)
5854 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5855 fieldcode = TREE_CODE (fieldtype);
5857 /* Error for non-static initialization of a flexible array member. */
5858 if (fieldcode == ARRAY_TYPE
5859 && !require_constant_value
5860 && TYPE_SIZE (fieldtype) == NULL_TREE
5861 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5863 error_init ("non-static initialization of a flexible array member");
5867 /* Accept a string constant to initialize a subarray. */
5869 && fieldcode == ARRAY_TYPE
5870 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5873 /* Otherwise, if we have come to a subaggregate,
5874 and we don't have an element of its type, push into it. */
5875 else if (value != 0 && !constructor_no_implicit
5876 && value != error_mark_node
5877 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5878 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5879 || fieldcode == UNION_TYPE))
5881 push_init_level (1);
5887 push_member_name (constructor_fields);
5888 output_init_element (value, fieldtype, constructor_fields, 1);
5889 RESTORE_SPELLING_DEPTH (constructor_depth);
5892 /* Do the bookkeeping for an element that was
5893 directly output as a constructor. */
5895 /* For a record, keep track of end position of last field. */
5896 if (DECL_SIZE (constructor_fields))
5897 constructor_bit_index
5898 = size_binop (PLUS_EXPR,
5899 bit_position (constructor_fields),
5900 DECL_SIZE (constructor_fields));
5902 /* If the current field was the first one not yet written out,
5903 it isn't now, so update. */
5904 if (constructor_unfilled_fields == constructor_fields)
5906 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5907 /* Skip any nameless bit fields. */
5908 while (constructor_unfilled_fields != 0
5909 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5910 && DECL_NAME (constructor_unfilled_fields) == 0)
5911 constructor_unfilled_fields =
5912 TREE_CHAIN (constructor_unfilled_fields);
5916 constructor_fields = TREE_CHAIN (constructor_fields);
5917 /* Skip any nameless bit fields at the beginning. */
5918 while (constructor_fields != 0
5919 && DECL_C_BIT_FIELD (constructor_fields)
5920 && DECL_NAME (constructor_fields) == 0)
5921 constructor_fields = TREE_CHAIN (constructor_fields);
5923 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5926 enum tree_code fieldcode;
5928 if (constructor_fields == 0)
5930 pedwarn_init ("excess elements in union initializer");
5934 fieldtype = TREE_TYPE (constructor_fields);
5935 if (fieldtype != error_mark_node)
5936 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5937 fieldcode = TREE_CODE (fieldtype);
5939 /* Warn that traditional C rejects initialization of unions.
5940 We skip the warning if the value is zero. This is done
5941 under the assumption that the zero initializer in user
5942 code appears conditioned on e.g. __STDC__ to avoid
5943 "missing initializer" warnings and relies on default
5944 initialization to zero in the traditional C case.
5945 We also skip the warning if the initializer is designated,
5946 again on the assumption that this must be conditional on
5947 __STDC__ anyway (and we've already complained about the
5948 member-designator already). */
5949 if (warn_traditional && !in_system_header && !constructor_designated
5950 && !(value && (integer_zerop (value) || real_zerop (value))))
5951 warning ("traditional C rejects initialization of unions");
5953 /* Accept a string constant to initialize a subarray. */
5955 && fieldcode == ARRAY_TYPE
5956 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5959 /* Otherwise, if we have come to a subaggregate,
5960 and we don't have an element of its type, push into it. */
5961 else if (value != 0 && !constructor_no_implicit
5962 && value != error_mark_node
5963 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5964 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5965 || fieldcode == UNION_TYPE))
5967 push_init_level (1);
5973 push_member_name (constructor_fields);
5974 output_init_element (value, fieldtype, constructor_fields, 1);
5975 RESTORE_SPELLING_DEPTH (constructor_depth);
5978 /* Do the bookkeeping for an element that was
5979 directly output as a constructor. */
5981 constructor_bit_index = DECL_SIZE (constructor_fields);
5982 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5985 constructor_fields = 0;
5987 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5989 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5990 enum tree_code eltcode = TREE_CODE (elttype);
5992 /* Accept a string constant to initialize a subarray. */
5994 && eltcode == ARRAY_TYPE
5995 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
5998 /* Otherwise, if we have come to a subaggregate,
5999 and we don't have an element of its type, push into it. */
6000 else if (value != 0 && !constructor_no_implicit
6001 && value != error_mark_node
6002 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6003 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6004 || eltcode == UNION_TYPE))
6006 push_init_level (1);
6010 if (constructor_max_index != 0
6011 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6012 || integer_all_onesp (constructor_max_index)))
6014 pedwarn_init ("excess elements in array initializer");
6018 /* Now output the actual element. */
6021 push_array_bounds (tree_low_cst (constructor_index, 0));
6022 output_init_element (value, elttype, constructor_index, 1);
6023 RESTORE_SPELLING_DEPTH (constructor_depth);
6027 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6030 /* If we are doing the bookkeeping for an element that was
6031 directly output as a constructor, we must update
6032 constructor_unfilled_index. */
6033 constructor_unfilled_index = constructor_index;
6035 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6037 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6039 /* Do a basic check of initializer size. Note that vectors
6040 always have a fixed size derived from their type. */
6041 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6043 pedwarn_init ("excess elements in vector initializer");
6047 /* Now output the actual element. */
6049 output_init_element (value, elttype, constructor_index, 1);
6052 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6055 /* If we are doing the bookkeeping for an element that was
6056 directly output as a constructor, we must update
6057 constructor_unfilled_index. */
6058 constructor_unfilled_index = constructor_index;
6061 /* Handle the sole element allowed in a braced initializer
6062 for a scalar variable. */
6063 else if (constructor_fields == 0)
6065 pedwarn_init ("excess elements in scalar initializer");
6071 output_init_element (value, constructor_type, NULL_TREE, 1);
6072 constructor_fields = 0;
6075 /* Handle range initializers either at this level or anywhere higher
6076 in the designator stack. */
6077 if (constructor_range_stack)
6079 struct constructor_range_stack *p, *range_stack;
6082 range_stack = constructor_range_stack;
6083 constructor_range_stack = 0;
6084 while (constructor_stack != range_stack->stack)
6086 if (!constructor_stack->implicit)
6088 process_init_element (pop_init_level (1));
6090 for (p = range_stack;
6091 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6094 if (!constructor_stack->implicit)
6096 process_init_element (pop_init_level (1));
6099 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6100 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6105 constructor_index = p->index;
6106 constructor_fields = p->fields;
6107 if (finish && p->range_end && p->index == p->range_start)
6115 push_init_level (2);
6116 p->stack = constructor_stack;
6117 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6118 p->index = p->range_start;
6122 constructor_range_stack = range_stack;
6129 constructor_range_stack = 0;
6132 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6133 (guaranteed to be 'volatile' or null) and ARGS (represented using
6134 an ASM_STMT node). */
6136 build_asm_stmt (tree cv_qualifier, tree args)
6138 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6139 ASM_VOLATILE_P (args) = 1;
6140 return add_stmt (args);
6143 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6144 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6145 SIMPLE indicates whether there was anything at all after the
6146 string in the asm expression -- asm("blah") and asm("blah" : )
6147 are subtly different. We use a ASM_STMT node to represent this. */
6149 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6155 const char *constraint;
6156 bool allows_mem, allows_reg, is_inout;
6160 ninputs = list_length (inputs);
6161 noutputs = list_length (outputs);
6163 /* Remove output conversions that change the type but not the mode. */
6164 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6166 tree output = TREE_VALUE (tail);
6167 STRIP_NOPS (output);
6168 TREE_VALUE (tail) = output;
6169 lvalue_or_else (output, "invalid lvalue in asm statement");
6171 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6173 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
6174 &allows_mem, &allows_reg, &is_inout))
6176 /* By marking this operand as erroneous, we will not try
6177 to process this operand again in expand_asm_operands. */
6178 TREE_VALUE (tail) = error_mark_node;
6182 /* If the operand is a DECL that is going to end up in
6183 memory, assume it is addressable. This is a bit more
6184 conservative than it would ideally be; the exact test is
6185 buried deep in expand_asm_operands and depends on the
6186 DECL_RTL for the OPERAND -- which we don't have at this
6188 if (!allows_reg && DECL_P (output))
6189 c_mark_addressable (output);
6192 /* Perform default conversions on array and function inputs.
6193 Don't do this for other types as it would screw up operands
6194 expected to be in memory. */
6195 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6196 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6198 args = build_stmt (ASM_STMT, string, outputs, inputs, clobbers);
6200 /* Simple asm statements are treated as volatile. */
6203 ASM_VOLATILE_P (args) = 1;
6204 ASM_INPUT_P (args) = 1;
6209 /* Expand an ASM statement with operands, handling output operands
6210 that are not variables or INDIRECT_REFS by transforming such
6211 cases into cases that expand_asm_operands can handle.
6213 Arguments are same as for expand_asm_operands. */
6216 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6217 tree clobbers, int vol, location_t locus)
6219 int noutputs = list_length (outputs);
6221 /* o[I] is the place that output number I should be written. */
6222 tree *o = alloca (noutputs * sizeof (tree));
6225 /* Record the contents of OUTPUTS before it is modified. */
6226 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6228 o[i] = TREE_VALUE (tail);
6229 if (o[i] == error_mark_node)
6233 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6234 OUTPUTS some trees for where the values were actually stored. */
6235 expand_asm_operands (string, outputs, inputs, clobbers, vol, locus);
6237 /* Copy all the intermediate outputs into the specified outputs. */
6238 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6240 if (o[i] != TREE_VALUE (tail))
6242 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6243 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6246 /* Restore the original value so that it's correct the next
6247 time we expand this function. */
6248 TREE_VALUE (tail) = o[i];
6250 /* Detect modification of read-only values.
6251 (Otherwise done by build_modify_expr.) */
6254 tree type = TREE_TYPE (o[i]);
6255 if (TREE_READONLY (o[i])
6256 || TYPE_READONLY (type)
6257 || ((TREE_CODE (type) == RECORD_TYPE
6258 || TREE_CODE (type) == UNION_TYPE)
6259 && C_TYPE_FIELDS_READONLY (type)))
6260 readonly_error (o[i], "modification by `asm'");
6264 /* Those MODIFY_EXPRs could do autoincrements. */
6268 /* Expand a C `return' statement.
6269 RETVAL is the expression for what to return,
6270 or a null pointer for `return;' with no value. */
6273 c_expand_return (tree retval)
6275 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6277 if (TREE_THIS_VOLATILE (current_function_decl))
6278 warning ("function declared `noreturn' has a `return' statement");
6282 current_function_returns_null = 1;
6283 if ((warn_return_type || flag_isoc99)
6284 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6285 pedwarn_c99 ("`return' with no value, in function returning non-void");
6287 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6289 current_function_returns_null = 1;
6290 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6291 pedwarn ("`return' with a value, in function returning void");
6295 tree t = convert_for_assignment (valtype, retval, _("return"),
6296 NULL_TREE, NULL_TREE, 0);
6297 tree res = DECL_RESULT (current_function_decl);
6300 current_function_returns_value = 1;
6301 if (t == error_mark_node)
6304 inner = t = convert (TREE_TYPE (res), t);
6306 /* Strip any conversions, additions, and subtractions, and see if
6307 we are returning the address of a local variable. Warn if so. */
6310 switch (TREE_CODE (inner))
6312 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6314 inner = TREE_OPERAND (inner, 0);
6318 /* If the second operand of the MINUS_EXPR has a pointer
6319 type (or is converted from it), this may be valid, so
6320 don't give a warning. */
6322 tree op1 = TREE_OPERAND (inner, 1);
6324 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6325 && (TREE_CODE (op1) == NOP_EXPR
6326 || TREE_CODE (op1) == NON_LVALUE_EXPR
6327 || TREE_CODE (op1) == CONVERT_EXPR))
6328 op1 = TREE_OPERAND (op1, 0);
6330 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6333 inner = TREE_OPERAND (inner, 0);
6338 inner = TREE_OPERAND (inner, 0);
6340 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6341 inner = TREE_OPERAND (inner, 0);
6344 && ! DECL_EXTERNAL (inner)
6345 && ! TREE_STATIC (inner)
6346 && DECL_CONTEXT (inner) == current_function_decl)
6347 warning ("function returns address of local variable");
6357 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6360 return add_stmt (build_return_stmt (retval));
6364 /* The SWITCH_STMT being built. */
6366 /* A splay-tree mapping the low element of a case range to the high
6367 element, or NULL_TREE if there is no high element. Used to
6368 determine whether or not a new case label duplicates an old case
6369 label. We need a tree, rather than simply a hash table, because
6370 of the GNU case range extension. */
6372 /* The next node on the stack. */
6373 struct c_switch *next;
6376 /* A stack of the currently active switch statements. The innermost
6377 switch statement is on the top of the stack. There is no need to
6378 mark the stack for garbage collection because it is only active
6379 during the processing of the body of a function, and we never
6380 collect at that point. */
6382 static struct c_switch *switch_stack;
6384 /* Start a C switch statement, testing expression EXP. Return the new
6388 c_start_case (tree exp)
6390 enum tree_code code;
6391 tree type, orig_type = error_mark_node;
6392 struct c_switch *cs;
6394 if (exp != error_mark_node)
6396 code = TREE_CODE (TREE_TYPE (exp));
6397 orig_type = TREE_TYPE (exp);
6399 if (! INTEGRAL_TYPE_P (orig_type)
6400 && code != ERROR_MARK)
6402 error ("switch quantity not an integer");
6403 exp = integer_zero_node;
6407 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6409 if (warn_traditional && !in_system_header
6410 && (type == long_integer_type_node
6411 || type == long_unsigned_type_node))
6412 warning ("`long' switch expression not converted to `int' in ISO C");
6414 exp = default_conversion (exp);
6415 type = TREE_TYPE (exp);
6419 /* Add this new SWITCH_STMT to the stack. */
6420 cs = xmalloc (sizeof (*cs));
6421 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6422 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6423 cs->next = switch_stack;
6426 return add_stmt (switch_stack->switch_stmt);
6429 /* Process a case label. */
6432 do_case (tree low_value, tree high_value)
6434 tree label = NULL_TREE;
6438 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
6440 label = c_add_case_label (switch_stack->cases,
6441 SWITCH_COND (switch_stack->switch_stmt),
6442 low_value, high_value);
6443 if (label == error_mark_node)
6445 else if (switch_was_empty_p)
6447 /* Attach the first case label to the SWITCH_BODY. */
6448 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
6449 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
6453 error ("case label not within a switch statement");
6455 error ("`default' label not within a switch statement");
6460 /* Finish the switch statement. */
6463 c_finish_case (void)
6465 struct c_switch *cs = switch_stack;
6467 /* Emit warnings as needed. */
6468 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6470 /* Rechain the next statements to the SWITCH_STMT. */
6471 last_tree = cs->switch_stmt;
6473 /* Pop the stack. */
6474 switch_stack = switch_stack->next;
6475 splay_tree_delete (cs->cases);
6479 /* Build a binary-operation expression without default conversions.
6480 CODE is the kind of expression to build.
6481 This function differs from `build' in several ways:
6482 the data type of the result is computed and recorded in it,
6483 warnings are generated if arg data types are invalid,
6484 special handling for addition and subtraction of pointers is known,
6485 and some optimization is done (operations on narrow ints
6486 are done in the narrower type when that gives the same result).
6487 Constant folding is also done before the result is returned.
6489 Note that the operands will never have enumeral types, or function
6490 or array types, because either they will have the default conversions
6491 performed or they have both just been converted to some other type in which
6492 the arithmetic is to be done. */
6495 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6499 enum tree_code code0, code1;
6502 /* Expression code to give to the expression when it is built.
6503 Normally this is CODE, which is what the caller asked for,
6504 but in some special cases we change it. */
6505 enum tree_code resultcode = code;
6507 /* Data type in which the computation is to be performed.
6508 In the simplest cases this is the common type of the arguments. */
6509 tree result_type = NULL;
6511 /* Nonzero means operands have already been type-converted
6512 in whatever way is necessary.
6513 Zero means they need to be converted to RESULT_TYPE. */
6516 /* Nonzero means create the expression with this type, rather than
6518 tree build_type = 0;
6520 /* Nonzero means after finally constructing the expression
6521 convert it to this type. */
6522 tree final_type = 0;
6524 /* Nonzero if this is an operation like MIN or MAX which can
6525 safely be computed in short if both args are promoted shorts.
6526 Also implies COMMON.
6527 -1 indicates a bitwise operation; this makes a difference
6528 in the exact conditions for when it is safe to do the operation
6529 in a narrower mode. */
6532 /* Nonzero if this is a comparison operation;
6533 if both args are promoted shorts, compare the original shorts.
6534 Also implies COMMON. */
6535 int short_compare = 0;
6537 /* Nonzero if this is a right-shift operation, which can be computed on the
6538 original short and then promoted if the operand is a promoted short. */
6539 int short_shift = 0;
6541 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6546 op0 = default_conversion (orig_op0);
6547 op1 = default_conversion (orig_op1);
6555 type0 = TREE_TYPE (op0);
6556 type1 = TREE_TYPE (op1);
6558 /* The expression codes of the data types of the arguments tell us
6559 whether the arguments are integers, floating, pointers, etc. */
6560 code0 = TREE_CODE (type0);
6561 code1 = TREE_CODE (type1);
6563 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6564 STRIP_TYPE_NOPS (op0);
6565 STRIP_TYPE_NOPS (op1);
6567 /* If an error was already reported for one of the arguments,
6568 avoid reporting another error. */
6570 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6571 return error_mark_node;
6576 /* Handle the pointer + int case. */
6577 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6578 return pointer_int_sum (PLUS_EXPR, op0, op1);
6579 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6580 return pointer_int_sum (PLUS_EXPR, op1, op0);
6586 /* Subtraction of two similar pointers.
6587 We must subtract them as integers, then divide by object size. */
6588 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6589 && comp_target_types (type0, type1, 1))
6590 return pointer_diff (op0, op1);
6591 /* Handle pointer minus int. Just like pointer plus int. */
6592 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6593 return pointer_int_sum (MINUS_EXPR, op0, op1);
6602 case TRUNC_DIV_EXPR:
6604 case FLOOR_DIV_EXPR:
6605 case ROUND_DIV_EXPR:
6606 case EXACT_DIV_EXPR:
6607 /* Floating point division by zero is a legitimate way to obtain
6608 infinities and NaNs. */
6609 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6610 warning ("division by zero");
6612 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6613 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6614 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6615 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6617 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6618 resultcode = RDIV_EXPR;
6620 /* Although it would be tempting to shorten always here, that
6621 loses on some targets, since the modulo instruction is
6622 undefined if the quotient can't be represented in the
6623 computation mode. We shorten only if unsigned or if
6624 dividing by something we know != -1. */
6625 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
6626 || (TREE_CODE (op1) == INTEGER_CST
6627 && ! integer_all_onesp (op1)));
6635 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6637 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6641 case TRUNC_MOD_EXPR:
6642 case FLOOR_MOD_EXPR:
6643 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6644 warning ("division by zero");
6646 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6648 /* Although it would be tempting to shorten always here, that loses
6649 on some targets, since the modulo instruction is undefined if the
6650 quotient can't be represented in the computation mode. We shorten
6651 only if unsigned or if dividing by something we know != -1. */
6652 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
6653 || (TREE_CODE (op1) == INTEGER_CST
6654 && ! integer_all_onesp (op1)));
6659 case TRUTH_ANDIF_EXPR:
6660 case TRUTH_ORIF_EXPR:
6661 case TRUTH_AND_EXPR:
6663 case TRUTH_XOR_EXPR:
6664 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6665 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6666 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6667 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6669 /* Result of these operations is always an int,
6670 but that does not mean the operands should be
6671 converted to ints! */
6672 result_type = integer_type_node;
6673 op0 = lang_hooks.truthvalue_conversion (op0);
6674 op1 = lang_hooks.truthvalue_conversion (op1);
6679 /* Shift operations: result has same type as first operand;
6680 always convert second operand to int.
6681 Also set SHORT_SHIFT if shifting rightward. */
6684 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6686 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6688 if (tree_int_cst_sgn (op1) < 0)
6689 warning ("right shift count is negative");
6692 if (! integer_zerop (op1))
6695 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6696 warning ("right shift count >= width of type");
6700 /* Use the type of the value to be shifted. */
6701 result_type = type0;
6702 /* Convert the shift-count to an integer, regardless of size
6703 of value being shifted. */
6704 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6705 op1 = convert (integer_type_node, op1);
6706 /* Avoid converting op1 to result_type later. */
6712 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6714 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6716 if (tree_int_cst_sgn (op1) < 0)
6717 warning ("left shift count is negative");
6719 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6720 warning ("left shift count >= width of type");
6723 /* Use the type of the value to be shifted. */
6724 result_type = type0;
6725 /* Convert the shift-count to an integer, regardless of size
6726 of value being shifted. */
6727 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6728 op1 = convert (integer_type_node, op1);
6729 /* Avoid converting op1 to result_type later. */
6736 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6738 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6740 if (tree_int_cst_sgn (op1) < 0)
6741 warning ("shift count is negative");
6742 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6743 warning ("shift count >= width of type");
6746 /* Use the type of the value to be shifted. */
6747 result_type = type0;
6748 /* Convert the shift-count to an integer, regardless of size
6749 of value being shifted. */
6750 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6751 op1 = convert (integer_type_node, op1);
6752 /* Avoid converting op1 to result_type later. */
6759 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
6760 warning ("comparing floating point with == or != is unsafe");
6761 /* Result of comparison is always int,
6762 but don't convert the args to int! */
6763 build_type = integer_type_node;
6764 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6765 || code0 == COMPLEX_TYPE)
6766 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6767 || code1 == COMPLEX_TYPE))
6769 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6771 tree tt0 = TREE_TYPE (type0);
6772 tree tt1 = TREE_TYPE (type1);
6773 /* Anything compares with void *. void * compares with anything.
6774 Otherwise, the targets must be compatible
6775 and both must be object or both incomplete. */
6776 if (comp_target_types (type0, type1, 1))
6777 result_type = common_pointer_type (type0, type1);
6778 else if (VOID_TYPE_P (tt0))
6780 /* op0 != orig_op0 detects the case of something
6781 whose value is 0 but which isn't a valid null ptr const. */
6782 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
6783 && TREE_CODE (tt1) == FUNCTION_TYPE)
6784 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6786 else if (VOID_TYPE_P (tt1))
6788 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
6789 && TREE_CODE (tt0) == FUNCTION_TYPE)
6790 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6793 pedwarn ("comparison of distinct pointer types lacks a cast");
6795 if (result_type == NULL_TREE)
6796 result_type = ptr_type_node;
6798 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6799 && integer_zerop (op1))
6800 result_type = type0;
6801 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6802 && integer_zerop (op0))
6803 result_type = type1;
6804 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6806 result_type = type0;
6807 pedwarn ("comparison between pointer and integer");
6809 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6811 result_type = type1;
6812 pedwarn ("comparison between pointer and integer");
6818 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6819 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6821 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6823 if (comp_target_types (type0, type1, 1))
6825 result_type = common_pointer_type (type0, type1);
6827 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6828 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6832 result_type = ptr_type_node;
6833 pedwarn ("comparison of distinct pointer types lacks a cast");
6842 build_type = integer_type_node;
6843 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6844 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6846 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6848 if (comp_target_types (type0, type1, 1))
6850 result_type = common_pointer_type (type0, type1);
6851 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
6852 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
6853 pedwarn ("comparison of complete and incomplete pointers");
6855 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6856 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6860 result_type = ptr_type_node;
6861 pedwarn ("comparison of distinct pointer types lacks a cast");
6864 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6865 && integer_zerop (op1))
6867 result_type = type0;
6868 if (pedantic || extra_warnings)
6869 pedwarn ("ordered comparison of pointer with integer zero");
6871 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6872 && integer_zerop (op0))
6874 result_type = type1;
6876 pedwarn ("ordered comparison of pointer with integer zero");
6878 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6880 result_type = type0;
6881 pedwarn ("comparison between pointer and integer");
6883 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6885 result_type = type1;
6886 pedwarn ("comparison between pointer and integer");
6890 case UNORDERED_EXPR:
6898 build_type = integer_type_node;
6899 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6901 error ("unordered comparison on non-floating point argument");
6902 return error_mark_node;
6911 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6912 return error_mark_node;
6914 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6915 || code0 == VECTOR_TYPE)
6917 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
6918 || code1 == VECTOR_TYPE))
6920 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
6922 if (shorten || common || short_compare)
6923 result_type = common_type (type0, type1);
6925 /* For certain operations (which identify themselves by shorten != 0)
6926 if both args were extended from the same smaller type,
6927 do the arithmetic in that type and then extend.
6929 shorten !=0 and !=1 indicates a bitwise operation.
6930 For them, this optimization is safe only if
6931 both args are zero-extended or both are sign-extended.
6932 Otherwise, we might change the result.
6933 Eg, (short)-1 | (unsigned short)-1 is (int)-1
6934 but calculated in (unsigned short) it would be (unsigned short)-1. */
6936 if (shorten && none_complex)
6938 int unsigned0, unsigned1;
6939 tree arg0 = get_narrower (op0, &unsigned0);
6940 tree arg1 = get_narrower (op1, &unsigned1);
6941 /* UNS is 1 if the operation to be done is an unsigned one. */
6942 int uns = TYPE_UNSIGNED (result_type);
6945 final_type = result_type;
6947 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
6948 but it *requires* conversion to FINAL_TYPE. */
6950 if ((TYPE_PRECISION (TREE_TYPE (op0))
6951 == TYPE_PRECISION (TREE_TYPE (arg0)))
6952 && TREE_TYPE (op0) != final_type)
6953 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
6954 if ((TYPE_PRECISION (TREE_TYPE (op1))
6955 == TYPE_PRECISION (TREE_TYPE (arg1)))
6956 && TREE_TYPE (op1) != final_type)
6957 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
6959 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
6961 /* For bitwise operations, signedness of nominal type
6962 does not matter. Consider only how operands were extended. */
6966 /* Note that in all three cases below we refrain from optimizing
6967 an unsigned operation on sign-extended args.
6968 That would not be valid. */
6970 /* Both args variable: if both extended in same way
6971 from same width, do it in that width.
6972 Do it unsigned if args were zero-extended. */
6973 if ((TYPE_PRECISION (TREE_TYPE (arg0))
6974 < TYPE_PRECISION (result_type))
6975 && (TYPE_PRECISION (TREE_TYPE (arg1))
6976 == TYPE_PRECISION (TREE_TYPE (arg0)))
6977 && unsigned0 == unsigned1
6978 && (unsigned0 || !uns))
6980 = c_common_signed_or_unsigned_type
6981 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
6982 else if (TREE_CODE (arg0) == INTEGER_CST
6983 && (unsigned1 || !uns)
6984 && (TYPE_PRECISION (TREE_TYPE (arg1))
6985 < TYPE_PRECISION (result_type))
6987 = c_common_signed_or_unsigned_type (unsigned1,
6989 int_fits_type_p (arg0, type)))
6991 else if (TREE_CODE (arg1) == INTEGER_CST
6992 && (unsigned0 || !uns)
6993 && (TYPE_PRECISION (TREE_TYPE (arg0))
6994 < TYPE_PRECISION (result_type))
6996 = c_common_signed_or_unsigned_type (unsigned0,
6998 int_fits_type_p (arg1, type)))
7002 /* Shifts can be shortened if shifting right. */
7007 tree arg0 = get_narrower (op0, &unsigned_arg);
7009 final_type = result_type;
7011 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7012 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7014 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7015 /* We can shorten only if the shift count is less than the
7016 number of bits in the smaller type size. */
7017 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7018 /* We cannot drop an unsigned shift after sign-extension. */
7019 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7021 /* Do an unsigned shift if the operand was zero-extended. */
7023 = c_common_signed_or_unsigned_type (unsigned_arg,
7025 /* Convert value-to-be-shifted to that type. */
7026 if (TREE_TYPE (op0) != result_type)
7027 op0 = convert (result_type, op0);
7032 /* Comparison operations are shortened too but differently.
7033 They identify themselves by setting short_compare = 1. */
7037 /* Don't write &op0, etc., because that would prevent op0
7038 from being kept in a register.
7039 Instead, make copies of the our local variables and
7040 pass the copies by reference, then copy them back afterward. */
7041 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7042 enum tree_code xresultcode = resultcode;
7044 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7049 op0 = xop0, op1 = xop1;
7051 resultcode = xresultcode;
7053 if (warn_sign_compare && skip_evaluation == 0)
7055 int op0_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7056 int op1_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7057 int unsignedp0, unsignedp1;
7058 tree primop0 = get_narrower (op0, &unsignedp0);
7059 tree primop1 = get_narrower (op1, &unsignedp1);
7063 STRIP_TYPE_NOPS (xop0);
7064 STRIP_TYPE_NOPS (xop1);
7066 /* Give warnings for comparisons between signed and unsigned
7067 quantities that may fail.
7069 Do the checking based on the original operand trees, so that
7070 casts will be considered, but default promotions won't be.
7072 Do not warn if the comparison is being done in a signed type,
7073 since the signed type will only be chosen if it can represent
7074 all the values of the unsigned type. */
7075 if (! TYPE_UNSIGNED (result_type))
7077 /* Do not warn if both operands are the same signedness. */
7078 else if (op0_signed == op1_signed)
7085 sop = xop0, uop = xop1;
7087 sop = xop1, uop = xop0;
7089 /* Do not warn if the signed quantity is an
7090 unsuffixed integer literal (or some static
7091 constant expression involving such literals or a
7092 conditional expression involving such literals)
7093 and it is non-negative. */
7094 if (c_tree_expr_nonnegative_p (sop))
7096 /* Do not warn if the comparison is an equality operation,
7097 the unsigned quantity is an integral constant, and it
7098 would fit in the result if the result were signed. */
7099 else if (TREE_CODE (uop) == INTEGER_CST
7100 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7102 (uop, c_common_signed_type (result_type)))
7104 /* Do not warn if the unsigned quantity is an enumeration
7105 constant and its maximum value would fit in the result
7106 if the result were signed. */
7107 else if (TREE_CODE (uop) == INTEGER_CST
7108 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7110 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7111 c_common_signed_type (result_type)))
7114 warning ("comparison between signed and unsigned");
7117 /* Warn if two unsigned values are being compared in a size
7118 larger than their original size, and one (and only one) is the
7119 result of a `~' operator. This comparison will always fail.
7121 Also warn if one operand is a constant, and the constant
7122 does not have all bits set that are set in the ~ operand
7123 when it is extended. */
7125 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7126 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7128 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7129 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7132 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7135 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7138 HOST_WIDE_INT constant, mask;
7139 int unsignedp, bits;
7141 if (host_integerp (primop0, 0))
7144 unsignedp = unsignedp1;
7145 constant = tree_low_cst (primop0, 0);
7150 unsignedp = unsignedp0;
7151 constant = tree_low_cst (primop1, 0);
7154 bits = TYPE_PRECISION (TREE_TYPE (primop));
7155 if (bits < TYPE_PRECISION (result_type)
7156 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7158 mask = (~ (HOST_WIDE_INT) 0) << bits;
7159 if ((mask & constant) != mask)
7160 warning ("comparison of promoted ~unsigned with constant");
7163 else if (unsignedp0 && unsignedp1
7164 && (TYPE_PRECISION (TREE_TYPE (primop0))
7165 < TYPE_PRECISION (result_type))
7166 && (TYPE_PRECISION (TREE_TYPE (primop1))
7167 < TYPE_PRECISION (result_type)))
7168 warning ("comparison of promoted ~unsigned with unsigned");
7174 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7175 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7176 Then the expression will be built.
7177 It will be given type FINAL_TYPE if that is nonzero;
7178 otherwise, it will be given type RESULT_TYPE. */
7182 binary_op_error (code);
7183 return error_mark_node;
7188 if (TREE_TYPE (op0) != result_type)
7189 op0 = convert (result_type, op0);
7190 if (TREE_TYPE (op1) != result_type)
7191 op1 = convert (result_type, op1);
7194 if (build_type == NULL_TREE)
7195 build_type = result_type;
7198 tree result = build (resultcode, build_type, op0, op1);
7200 /* Treat expressions in initializers specially as they can't trap. */
7201 result = require_constant_value ? fold_initializer (result)
7204 if (final_type != 0)
7205 result = convert (final_type, result);
7210 /* Build the result of __builtin_offsetof. TYPE is the first argument to
7211 offsetof, i.e. a type. LIST is a tree_list that encodes component and
7212 array references; PURPOSE is set for the former and VALUE is set for
7216 build_offsetof (tree type, tree list)
7220 /* Build "*(type *)0". */
7221 t = convert (build_pointer_type (type), null_pointer_node);
7222 t = build_indirect_ref (t, "");
7224 /* Build COMPONENT and ARRAY_REF expressions as needed. */
7225 for (list = nreverse (list); list ; list = TREE_CHAIN (list))
7226 if (TREE_PURPOSE (list))
7227 t = build_component_ref (t, TREE_PURPOSE (list));
7229 t = build_array_ref (t, TREE_VALUE (list));
7231 /* Finalize the offsetof expression. For now all we need to do is take
7232 the address of the expression we created, and cast that to an integer
7233 type; this mirrors the traditional macro implementation of offsetof. */
7234 t = build_unary_op (ADDR_EXPR, t, 0);
7235 return convert (size_type_node, t);