1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 88, 91, 92-5, 1996 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This file is part of the C front end.
23 It contains routines to build C expressions given their operands,
24 including computing the types of the result, C-specific error checks,
25 and some optimization.
27 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
28 and to process initializations in declarations (since they work
29 like a strange sort of assignment). */
38 /* Nonzero if we've already printed a "missing braces around initializer"
39 message within this initializer. */
40 static int missing_braces_mentioned;
42 extern char *index ();
43 extern char *rindex ();
45 static tree qualify_type PROTO((tree, tree));
46 static int comp_target_types PROTO((tree, tree));
47 static int function_types_compatible_p PROTO((tree, tree));
48 static int type_lists_compatible_p PROTO((tree, tree));
49 static int self_promoting_type_p PROTO((tree));
50 static tree decl_constant_value PROTO((tree));
51 static tree lookup_field PROTO((tree, tree, tree *));
52 static tree convert_arguments PROTO((tree, tree, tree, tree));
53 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
54 static tree pointer_diff PROTO((tree, tree));
55 static tree unary_complex_lvalue PROTO((enum tree_code, tree));
56 static void pedantic_lvalue_warning PROTO((enum tree_code));
57 static tree internal_build_compound_expr PROTO((tree, int));
58 static tree convert_for_assignment PROTO((tree, tree, char *, tree,
60 static void warn_for_assignment PROTO((char *, char *, tree, int));
61 static tree valid_compound_expr_initializer PROTO((tree, tree));
62 static void push_string PROTO((char *));
63 static void push_member_name PROTO((tree));
64 static void push_array_bounds PROTO((int));
65 static int spelling_length PROTO((void));
66 static char *print_spelling PROTO((char *));
67 static char *get_spelling PROTO((char *));
68 static void warning_init PROTO((char *, char *,
70 static tree digest_init PROTO((tree, tree, int, int));
71 static void check_init_type_bitfields PROTO((tree));
72 static void output_init_element PROTO((tree, tree, tree, int));
73 static void output_pending_init_elements PROTO((int));
75 /* Do `exp = require_complete_type (exp);' to make sure exp
76 does not have an incomplete type. (That includes void types.) */
79 require_complete_type (value)
82 tree type = TREE_TYPE (value);
84 /* First, detect a valid value with a complete type. */
85 if (TYPE_SIZE (type) != 0
86 && type != void_type_node)
89 incomplete_type_error (value, type);
90 return error_mark_node;
93 /* Print an error message for invalid use of an incomplete type.
94 VALUE is the expression that was used (or 0 if that isn't known)
95 and TYPE is the type that was invalid. */
98 incomplete_type_error (value, type)
104 /* Avoid duplicate error message. */
105 if (TREE_CODE (type) == ERROR_MARK)
108 if (value != 0 && (TREE_CODE (value) == VAR_DECL
109 || TREE_CODE (value) == PARM_DECL))
110 error ("`%s' has an incomplete type",
111 IDENTIFIER_POINTER (DECL_NAME (value)));
115 /* We must print an error message. Be clever about what it says. */
117 switch (TREE_CODE (type))
120 errmsg = "invalid use of undefined type `struct %s'";
124 errmsg = "invalid use of undefined type `union %s'";
128 errmsg = "invalid use of undefined type `enum %s'";
132 error ("invalid use of void expression");
136 if (TYPE_DOMAIN (type))
138 type = TREE_TYPE (type);
141 error ("invalid use of array with unspecified bounds");
148 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
149 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
151 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
152 error ("invalid use of incomplete typedef `%s'",
153 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
157 /* Return a variant of TYPE which has all the type qualifiers of LIKE
158 as well as those of TYPE. */
161 qualify_type (type, like)
164 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
165 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
166 return c_build_type_variant (type, constflag, volflag);
169 /* Return the common type of two types.
170 We assume that comptypes has already been done and returned 1;
171 if that isn't so, this may crash. In particular, we assume that qualifiers
174 This is the type for the result of most arithmetic operations
175 if the operands have the given two types. */
181 register enum tree_code code1;
182 register enum tree_code code2;
185 /* Save time if the two types are the same. */
187 if (t1 == t2) return t1;
189 /* If one type is nonsense, use the other. */
190 if (t1 == error_mark_node)
192 if (t2 == error_mark_node)
195 /* Merge the attributes */
196 attributes = merge_attributes (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
198 /* Treat an enum type as the unsigned integer type of the same width. */
200 if (TREE_CODE (t1) == ENUMERAL_TYPE)
201 t1 = type_for_size (TYPE_PRECISION (t1), 1);
202 if (TREE_CODE (t2) == ENUMERAL_TYPE)
203 t2 = type_for_size (TYPE_PRECISION (t2), 1);
205 code1 = TREE_CODE (t1);
206 code2 = TREE_CODE (t2);
208 /* If one type is complex, form the common type of the non-complex
209 components, then make that complex. Use T1 or T2 if it is the
211 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
213 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
214 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
215 tree subtype = common_type (subtype1, subtype2);
217 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
218 return build_type_attribute_variant (t1, attributes);
219 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
220 return build_type_attribute_variant (t2, attributes);
222 return build_type_attribute_variant (build_complex_type (subtype),
230 /* If only one is real, use it as the result. */
232 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
233 return build_type_attribute_variant (t1, attributes);
235 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
236 return build_type_attribute_variant (t2, attributes);
238 /* Both real or both integers; use the one with greater precision. */
240 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
241 return build_type_attribute_variant (t1, attributes);
242 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
243 return build_type_attribute_variant (t2, attributes);
245 /* Same precision. Prefer longs to ints even when same size. */
247 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
248 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
249 return build_type_attribute_variant (long_unsigned_type_node,
252 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
253 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
255 /* But preserve unsignedness from the other type,
256 since long cannot hold all the values of an unsigned int. */
257 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
258 t1 = long_unsigned_type_node;
260 t1 = long_integer_type_node;
261 return build_type_attribute_variant (t1, attributes);
264 /* Otherwise prefer the unsigned one. */
266 if (TREE_UNSIGNED (t1))
267 return build_type_attribute_variant (t1, attributes);
269 return build_type_attribute_variant (t2, attributes);
272 /* For two pointers, do this recursively on the target type,
273 and combine the qualifiers of the two types' targets. */
274 /* This code was turned off; I don't know why.
275 But ANSI C specifies doing this with the qualifiers.
276 So I turned it on again. */
278 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
279 TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
281 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
283 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
284 t1 = build_pointer_type (c_build_type_variant (target, constp,
286 return build_type_attribute_variant (t1, attributes);
289 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
290 return build_type_attribute_variant (t1, attributes);
295 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
296 /* Save space: see if the result is identical to one of the args. */
297 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
298 return build_type_attribute_variant (t1, attributes);
299 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
300 return build_type_attribute_variant (t2, attributes);
301 /* Merge the element types, and have a size if either arg has one. */
302 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
303 return build_type_attribute_variant (t1, attributes);
307 /* Function types: prefer the one that specified arg types.
308 If both do, merge the arg types. Also merge the return types. */
310 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
311 tree p1 = TYPE_ARG_TYPES (t1);
312 tree p2 = TYPE_ARG_TYPES (t2);
317 /* Save space: see if the result is identical to one of the args. */
318 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
319 return build_type_attribute_variant (t1, attributes);
320 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
321 return build_type_attribute_variant (t2, attributes);
323 /* Simple way if one arg fails to specify argument types. */
324 if (TYPE_ARG_TYPES (t1) == 0)
326 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
327 return build_type_attribute_variant (t1, attributes);
329 if (TYPE_ARG_TYPES (t2) == 0)
331 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
332 return build_type_attribute_variant (t1, attributes);
335 /* If both args specify argument types, we must merge the two
336 lists, argument by argument. */
338 len = list_length (p1);
341 for (i = 0; i < len; i++)
342 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
347 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
349 /* A null type means arg type is not specified.
350 Take whatever the other function type has. */
351 if (TREE_VALUE (p1) == 0)
353 TREE_VALUE (n) = TREE_VALUE (p2);
356 if (TREE_VALUE (p2) == 0)
358 TREE_VALUE (n) = TREE_VALUE (p1);
362 /* Given wait (union {union wait *u; int *i} *)
363 and wait (union wait *),
364 prefer union wait * as type of parm. */
365 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
366 && TREE_VALUE (p1) != TREE_VALUE (p2))
369 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
370 memb; memb = TREE_CHAIN (memb))
371 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
373 TREE_VALUE (n) = TREE_VALUE (p2);
375 pedwarn ("function types not truly compatible in ANSI C");
379 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
380 && TREE_VALUE (p2) != TREE_VALUE (p1))
383 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
384 memb; memb = TREE_CHAIN (memb))
385 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
387 TREE_VALUE (n) = TREE_VALUE (p1);
389 pedwarn ("function types not truly compatible in ANSI C");
393 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
397 t1 = build_function_type (valtype, newargs);
398 /* ... falls through ... */
402 return build_type_attribute_variant (t1, attributes);
407 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
408 or various other operations. Return 2 if they are compatible
409 but a warning may be needed if you use them together. */
412 comptypes (type1, type2)
415 register tree t1 = type1;
416 register tree t2 = type2;
419 /* Suppress errors caused by previously reported errors. */
421 if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
424 /* Treat an enum type as the integer type of the same width and
427 if (TREE_CODE (t1) == ENUMERAL_TYPE)
428 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
429 if (TREE_CODE (t2) == ENUMERAL_TYPE)
430 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
435 /* Different classes of types can't be compatible. */
437 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
439 /* Qualifiers must match. */
441 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
443 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
446 /* Allow for two different type nodes which have essentially the same
447 definition. Note that we already checked for equality of the type
448 type qualifiers (just above). */
450 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
453 #ifndef COMP_TYPE_ATTRIBUTES
454 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
457 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
458 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
461 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
464 switch (TREE_CODE (t1))
467 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
468 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
472 val = function_types_compatible_p (t1, t2);
477 tree d1 = TYPE_DOMAIN (t1);
478 tree d2 = TYPE_DOMAIN (t2);
481 /* Target types must match incl. qualifiers. */
482 if (TREE_TYPE (t1) != TREE_TYPE (t2)
483 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
486 /* Sizes must match unless one is missing or variable. */
487 if (d1 == 0 || d2 == 0 || d1 == d2
488 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
489 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
490 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
491 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
494 if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
495 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
496 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
497 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
498 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
499 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
500 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
501 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2)))))
507 if (maybe_objc_comptypes (t1, t2, 0) == 1)
511 return attrval == 2 && val == 1 ? 2 : val;
514 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
515 ignoring their qualifiers. */
518 comp_target_types (ttl, ttr)
523 /* Give maybe_objc_comptypes a crack at letting these types through. */
524 if (val = maybe_objc_comptypes (ttl, ttr, 1) >= 0)
527 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
528 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
530 if (val == 2 && pedantic)
531 pedwarn ("types are not quite compatible");
535 /* Subroutines of `comptypes'. */
537 /* Return 1 if two function types F1 and F2 are compatible.
538 If either type specifies no argument types,
539 the other must specify a fixed number of self-promoting arg types.
540 Otherwise, if one type specifies only the number of arguments,
541 the other must specify that number of self-promoting arg types.
542 Otherwise, the argument types must match. */
545 function_types_compatible_p (f1, f2)
549 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
553 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
554 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
557 args1 = TYPE_ARG_TYPES (f1);
558 args2 = TYPE_ARG_TYPES (f2);
560 /* An unspecified parmlist matches any specified parmlist
561 whose argument types don't need default promotions. */
565 if (!self_promoting_args_p (args2))
567 /* If one of these types comes from a non-prototype fn definition,
568 compare that with the other type's arglist.
569 If they don't match, ask for a warning (but no error). */
570 if (TYPE_ACTUAL_ARG_TYPES (f1)
571 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
577 if (!self_promoting_args_p (args1))
579 if (TYPE_ACTUAL_ARG_TYPES (f2)
580 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
585 /* Both types have argument lists: compare them and propagate results. */
586 val1 = type_lists_compatible_p (args1, args2);
587 return val1 != 1 ? val1 : val;
590 /* Check two lists of types for compatibility,
591 returning 0 for incompatible, 1 for compatible,
592 or 2 for compatible with warning. */
595 type_lists_compatible_p (args1, args2)
598 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
604 if (args1 == 0 && args2 == 0)
606 /* If one list is shorter than the other,
607 they fail to match. */
608 if (args1 == 0 || args2 == 0)
610 /* A null pointer instead of a type
611 means there is supposed to be an argument
612 but nothing is specified about what type it has.
613 So match anything that self-promotes. */
614 if (TREE_VALUE (args1) == 0)
616 if (! self_promoting_type_p (TREE_VALUE (args2)))
619 else if (TREE_VALUE (args2) == 0)
621 if (! self_promoting_type_p (TREE_VALUE (args1)))
624 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
626 /* Allow wait (union {union wait *u; int *i} *)
627 and wait (union wait *) to be compatible. */
628 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
629 && (TYPE_NAME (TREE_VALUE (args1)) == 0
630 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
631 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
632 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
633 TYPE_SIZE (TREE_VALUE (args2))))
636 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
637 memb; memb = TREE_CHAIN (memb))
638 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
643 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
644 && (TYPE_NAME (TREE_VALUE (args2)) == 0
645 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
646 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
647 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
648 TYPE_SIZE (TREE_VALUE (args1))))
651 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
652 memb; memb = TREE_CHAIN (memb))
653 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
662 /* comptypes said ok, but record if it said to warn. */
666 args1 = TREE_CHAIN (args1);
667 args2 = TREE_CHAIN (args2);
671 /* Return 1 if PARMS specifies a fixed number of parameters
672 and none of their types is affected by default promotions. */
675 self_promoting_args_p (parms)
679 for (t = parms; t; t = TREE_CHAIN (t))
681 register tree type = TREE_VALUE (t);
683 if (TREE_CHAIN (t) == 0 && type != void_type_node)
689 if (TYPE_MAIN_VARIANT (type) == float_type_node)
692 if (C_PROMOTING_INTEGER_TYPE_P (type))
698 /* Return 1 if TYPE is not affected by default promotions. */
701 self_promoting_type_p (type)
704 if (TYPE_MAIN_VARIANT (type) == float_type_node)
707 if (C_PROMOTING_INTEGER_TYPE_P (type))
713 /* Return an unsigned type the same as TYPE in other respects. */
719 tree type1 = TYPE_MAIN_VARIANT (type);
720 if (type1 == signed_char_type_node || type1 == char_type_node)
721 return unsigned_char_type_node;
722 if (type1 == integer_type_node)
723 return unsigned_type_node;
724 if (type1 == short_integer_type_node)
725 return short_unsigned_type_node;
726 if (type1 == long_integer_type_node)
727 return long_unsigned_type_node;
728 if (type1 == long_long_integer_type_node)
729 return long_long_unsigned_type_node;
730 if (type1 == intDI_type_node)
731 return unsigned_intDI_type_node;
732 if (type1 == intSI_type_node)
733 return unsigned_intSI_type_node;
734 if (type1 == intHI_type_node)
735 return unsigned_intHI_type_node;
736 if (type1 == intQI_type_node)
737 return unsigned_intQI_type_node;
741 /* Return a signed type the same as TYPE in other respects. */
747 tree type1 = TYPE_MAIN_VARIANT (type);
748 if (type1 == unsigned_char_type_node || type1 == char_type_node)
749 return signed_char_type_node;
750 if (type1 == unsigned_type_node)
751 return integer_type_node;
752 if (type1 == short_unsigned_type_node)
753 return short_integer_type_node;
754 if (type1 == long_unsigned_type_node)
755 return long_integer_type_node;
756 if (type1 == long_long_unsigned_type_node)
757 return long_long_integer_type_node;
758 if (type1 == unsigned_intDI_type_node)
759 return intDI_type_node;
760 if (type1 == unsigned_intSI_type_node)
761 return intSI_type_node;
762 if (type1 == unsigned_intHI_type_node)
763 return intHI_type_node;
764 if (type1 == unsigned_intQI_type_node)
765 return intQI_type_node;
769 /* Return a type the same as TYPE except unsigned or
770 signed according to UNSIGNEDP. */
773 signed_or_unsigned_type (unsignedp, type)
777 if (! INTEGRAL_TYPE_P (type))
779 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
780 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
781 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
782 return unsignedp ? unsigned_type_node : integer_type_node;
783 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
784 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
785 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
786 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
787 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
788 return (unsignedp ? long_long_unsigned_type_node
789 : long_long_integer_type_node);
793 /* Compute the value of the `sizeof' operator. */
799 enum tree_code code = TREE_CODE (type);
802 if (code == FUNCTION_TYPE)
804 if (pedantic || warn_pointer_arith)
805 pedwarn ("sizeof applied to a function type");
808 if (code == VOID_TYPE)
810 if (pedantic || warn_pointer_arith)
811 pedwarn ("sizeof applied to a void type");
814 if (code == ERROR_MARK)
816 if (TYPE_SIZE (type) == 0)
818 error ("sizeof applied to an incomplete type");
822 /* Convert in case a char is more than one unit. */
823 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
824 size_int (TYPE_PRECISION (char_type_node)));
825 /* size_binop does not put the constant in range, so do it now. */
826 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
827 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
832 c_sizeof_nowarn (type)
835 enum tree_code code = TREE_CODE (type);
838 if (code == FUNCTION_TYPE
840 || code == ERROR_MARK)
842 if (TYPE_SIZE (type) == 0)
845 /* Convert in case a char is more than one unit. */
846 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
847 size_int (TYPE_PRECISION (char_type_node)));
848 force_fit_type (t, 0);
852 /* Compute the size to increment a pointer by. */
855 c_size_in_bytes (type)
858 enum tree_code code = TREE_CODE (type);
861 if (code == FUNCTION_TYPE)
863 if (code == VOID_TYPE)
865 if (code == ERROR_MARK)
867 if (TYPE_SIZE (type) == 0)
869 error ("arithmetic on pointer to an incomplete type");
873 /* Convert in case a char is more than one unit. */
874 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
875 size_int (BITS_PER_UNIT));
876 force_fit_type (t, 0);
880 /* Implement the __alignof keyword: Return the minimum required
881 alignment of TYPE, measured in bytes. */
887 enum tree_code code = TREE_CODE (type);
889 if (code == FUNCTION_TYPE)
890 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
892 if (code == VOID_TYPE || code == ERROR_MARK)
895 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
898 /* Implement the __alignof keyword: Return the minimum required
899 alignment of EXPR, measured in bytes. For VAR_DECL's and
900 FIELD_DECL's return DECL_ALIGN (which can be set from an
901 "aligned" __attribute__ specification). */
904 c_alignof_expr (expr)
907 if (TREE_CODE (expr) == VAR_DECL)
908 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
910 if (TREE_CODE (expr) == COMPONENT_REF
911 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
913 error ("`__alignof' applied to a bit-field");
916 else if (TREE_CODE (expr) == COMPONENT_REF
917 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
918 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
920 if (TREE_CODE (expr) == INDIRECT_REF)
922 tree t = TREE_OPERAND (expr, 0);
924 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
926 while (TREE_CODE (t) == NOP_EXPR
927 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
931 t = TREE_OPERAND (t, 0);
932 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
933 if (thisalign > bestalign)
934 best = t, bestalign = thisalign;
936 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
939 return c_alignof (TREE_TYPE (expr));
941 /* Return either DECL or its known constant value (if it has one). */
944 decl_constant_value (decl)
947 if (! TREE_PUBLIC (decl)
948 /* Don't change a variable array bound or initial value to a constant
949 in a place where a variable is invalid. */
950 && current_function_decl != 0
952 && ! TREE_THIS_VOLATILE (decl)
953 && TREE_READONLY (decl) && ! ITERATOR_P (decl)
954 && DECL_INITIAL (decl) != 0
955 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
956 /* This is invalid if initial value is not constant.
957 If it has either a function call, a memory reference,
958 or a variable, then re-evaluating it could give different results. */
959 && TREE_CONSTANT (DECL_INITIAL (decl))
960 /* Check for cases where this is sub-optimal, even though valid. */
961 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
962 && DECL_MODE (decl) != BLKmode)
963 return DECL_INITIAL (decl);
967 /* Perform default promotions for C data used in expressions.
968 Arrays and functions are converted to pointers;
969 enumeral types or short or char, to int.
970 In addition, manifest constants symbols are replaced by their values. */
973 default_conversion (exp)
976 register tree type = TREE_TYPE (exp);
977 register enum tree_code code = TREE_CODE (type);
979 /* Constants can be used directly unless they're not loadable. */
980 if (TREE_CODE (exp) == CONST_DECL)
981 exp = DECL_INITIAL (exp);
983 /* Replace a nonvolatile const static variable with its value unless
984 it is an array, in which case we must be sure that taking the
985 address of the array produces consistent results. */
986 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
988 exp = decl_constant_value (exp);
989 type = TREE_TYPE (exp);
992 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
994 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
995 to integer and cause infinite recursion. */
996 while (TREE_CODE (exp) == NON_LVALUE_EXPR
997 || (TREE_CODE (exp) == NOP_EXPR
998 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
999 exp = TREE_OPERAND (exp, 0);
1001 /* Normally convert enums to int,
1002 but convert wide enums to something wider. */
1003 if (code == ENUMERAL_TYPE)
1005 type = type_for_size (MAX (TYPE_PRECISION (type),
1006 TYPE_PRECISION (integer_type_node)),
1008 || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node))
1009 && TREE_UNSIGNED (type)));
1010 return convert (type, exp);
1013 if (C_PROMOTING_INTEGER_TYPE_P (type))
1015 /* Traditionally, unsignedness is preserved in default promotions.
1016 Also preserve unsignedness if not really getting any wider. */
1017 if (TREE_UNSIGNED (type)
1018 && (flag_traditional
1019 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1020 return convert (unsigned_type_node, exp);
1021 return convert (integer_type_node, exp);
1023 if (flag_traditional && !flag_allow_single_precision
1024 && TYPE_MAIN_VARIANT (type) == float_type_node)
1025 return convert (double_type_node, exp);
1026 if (code == VOID_TYPE)
1028 error ("void value not ignored as it ought to be");
1029 return error_mark_node;
1031 if (code == FUNCTION_TYPE)
1033 return build_unary_op (ADDR_EXPR, exp, 0);
1035 if (code == ARRAY_TYPE)
1038 tree restype = TREE_TYPE (type);
1043 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1044 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1046 constp = TREE_READONLY (exp);
1047 volatilep = TREE_THIS_VOLATILE (exp);
1050 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1051 || constp || volatilep)
1052 restype = c_build_type_variant (restype,
1053 TYPE_READONLY (type) || constp,
1054 TYPE_VOLATILE (type) || volatilep);
1056 if (TREE_CODE (exp) == INDIRECT_REF)
1057 return convert (TYPE_POINTER_TO (restype),
1058 TREE_OPERAND (exp, 0));
1060 if (TREE_CODE (exp) == COMPOUND_EXPR)
1062 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1063 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1064 TREE_OPERAND (exp, 0), op1);
1068 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1070 error ("invalid use of non-lvalue array");
1071 return error_mark_node;
1074 ptrtype = build_pointer_type (restype);
1076 if (TREE_CODE (exp) == VAR_DECL)
1078 /* ??? This is not really quite correct
1079 in that the type of the operand of ADDR_EXPR
1080 is not the target type of the type of the ADDR_EXPR itself.
1081 Question is, can this lossage be avoided? */
1082 adr = build1 (ADDR_EXPR, ptrtype, exp);
1083 if (mark_addressable (exp) == 0)
1084 return error_mark_node;
1085 TREE_CONSTANT (adr) = staticp (exp);
1086 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1089 /* This way is better for a COMPONENT_REF since it can
1090 simplify the offset for a component. */
1091 adr = build_unary_op (ADDR_EXPR, exp, 1);
1092 return convert (ptrtype, adr);
1097 /* Look up component name in the structure type definition.
1099 If this component name is found indirectly within an anonymous union,
1100 store in *INDIRECT the component which directly contains
1101 that anonymous union. Otherwise, set *INDIRECT to 0. */
1104 lookup_field (type, component, indirect)
1105 tree type, component;
1110 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1111 to the field elements. Use a binary search on this array to quickly
1112 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1113 will always be set for structures which have many elements. */
1115 if (TYPE_LANG_SPECIFIC (type))
1118 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1120 field = TYPE_FIELDS (type);
1122 top = TYPE_LANG_SPECIFIC (type)->len;
1123 while (top - bot > 1)
1125 half = (top - bot + 1) >> 1;
1126 field = field_array[bot+half];
1128 if (DECL_NAME (field) == NULL_TREE)
1130 /* Step through all anon unions in linear fashion. */
1131 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1135 field = field_array[bot++];
1136 anon = lookup_field (TREE_TYPE (field), component, &junk);
1137 if (anon != NULL_TREE)
1144 /* Entire record is only anon unions. */
1148 /* Restart the binary search, with new lower bound. */
1152 if (DECL_NAME (field) == component)
1154 if (DECL_NAME (field) < component)
1160 if (DECL_NAME (field_array[bot]) == component)
1161 field = field_array[bot];
1162 else if (DECL_NAME (field) != component)
1167 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1169 if (DECL_NAME (field) == NULL_TREE)
1172 tree anon = lookup_field (TREE_TYPE (field), component, &junk);
1173 if (anon != NULL_TREE)
1180 if (DECL_NAME (field) == component)
1185 *indirect = NULL_TREE;
1189 /* Make an expression to refer to the COMPONENT field of
1190 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1193 build_component_ref (datum, component)
1194 tree datum, component;
1196 register tree type = TREE_TYPE (datum);
1197 register enum tree_code code = TREE_CODE (type);
1198 register tree field = NULL;
1201 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1202 unless we are not to support things not strictly ANSI. */
1203 switch (TREE_CODE (datum))
1207 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1208 return build (COMPOUND_EXPR, TREE_TYPE (value),
1209 TREE_OPERAND (datum, 0), value);
1212 return build_conditional_expr
1213 (TREE_OPERAND (datum, 0),
1214 build_component_ref (TREE_OPERAND (datum, 1), component),
1215 build_component_ref (TREE_OPERAND (datum, 2), component));
1218 /* See if there is a field or component with name COMPONENT. */
1220 if (code == RECORD_TYPE || code == UNION_TYPE)
1224 if (TYPE_SIZE (type) == 0)
1226 incomplete_type_error (NULL_TREE, type);
1227 return error_mark_node;
1230 field = lookup_field (type, component, &indirect);
1234 error (code == RECORD_TYPE
1235 ? "structure has no member named `%s'"
1236 : "union has no member named `%s'",
1237 IDENTIFIER_POINTER (component));
1238 return error_mark_node;
1240 if (TREE_TYPE (field) == error_mark_node)
1241 return error_mark_node;
1243 /* If FIELD was found buried within an anonymous union,
1244 make one COMPONENT_REF to get that anonymous union,
1245 then fall thru to make a second COMPONENT_REF to get FIELD. */
1248 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1249 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1250 TREE_READONLY (ref) = 1;
1251 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1252 TREE_THIS_VOLATILE (ref) = 1;
1256 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1258 if (TREE_READONLY (datum) || TREE_READONLY (field))
1259 TREE_READONLY (ref) = 1;
1260 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1261 TREE_THIS_VOLATILE (ref) = 1;
1265 else if (code != ERROR_MARK)
1266 error ("request for member `%s' in something not a structure or union",
1267 IDENTIFIER_POINTER (component));
1269 return error_mark_node;
1272 /* Given an expression PTR for a pointer, return an expression
1273 for the value pointed to.
1274 ERRORSTRING is the name of the operator to appear in error messages. */
1277 build_indirect_ref (ptr, errorstring)
1281 register tree pointer = default_conversion (ptr);
1282 register tree type = TREE_TYPE (pointer);
1284 if (TREE_CODE (type) == POINTER_TYPE)
1286 if (TREE_CODE (pointer) == ADDR_EXPR
1288 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1289 == TREE_TYPE (type)))
1290 return TREE_OPERAND (pointer, 0);
1293 tree t = TREE_TYPE (type);
1294 register tree ref = build1 (INDIRECT_REF,
1295 TYPE_MAIN_VARIANT (t), pointer);
1297 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1299 error ("dereferencing pointer to incomplete type");
1300 return error_mark_node;
1302 if (TREE_CODE (t) == VOID_TYPE)
1303 warning ("dereferencing `void *' pointer");
1305 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1306 so that we get the proper error message if the result is used
1307 to assign to. Also, &* is supposed to be a no-op.
1308 And ANSI C seems to specify that the type of the result
1309 should be the const type. */
1310 /* A de-reference of a pointer to const is not a const. It is valid
1311 to change it via some other pointer. */
1312 TREE_READONLY (ref) = TYPE_READONLY (t);
1313 TREE_SIDE_EFFECTS (ref)
1314 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1315 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1319 else if (TREE_CODE (pointer) != ERROR_MARK)
1320 error ("invalid type argument of `%s'", errorstring);
1321 return error_mark_node;
1324 /* This handles expressions of the form "a[i]", which denotes
1327 This is logically equivalent in C to *(a+i), but we may do it differently.
1328 If A is a variable or a member, we generate a primitive ARRAY_REF.
1329 This avoids forcing the array out of registers, and can work on
1330 arrays that are not lvalues (for example, members of structures returned
1334 build_array_ref (array, index)
1339 error ("subscript missing in array reference");
1340 return error_mark_node;
1343 if (TREE_TYPE (array) == error_mark_node
1344 || TREE_TYPE (index) == error_mark_node)
1345 return error_mark_node;
1347 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1348 && TREE_CODE (array) != INDIRECT_REF)
1352 /* Subscripting with type char is likely to lose
1353 on a machine where chars are signed.
1354 So warn on any machine, but optionally.
1355 Don't warn for unsigned char since that type is safe.
1356 Don't warn for signed char because anyone who uses that
1357 must have done so deliberately. */
1358 if (warn_char_subscripts
1359 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1360 warning ("array subscript has type `char'");
1362 /* Apply default promotions *after* noticing character types. */
1363 index = default_conversion (index);
1365 /* Require integer *after* promotion, for sake of enums. */
1366 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1368 error ("array subscript is not an integer");
1369 return error_mark_node;
1372 /* An array that is indexed by a non-constant
1373 cannot be stored in a register; we must be able to do
1374 address arithmetic on its address.
1375 Likewise an array of elements of variable size. */
1376 if (TREE_CODE (index) != INTEGER_CST
1377 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1378 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1380 if (mark_addressable (array) == 0)
1381 return error_mark_node;
1383 /* An array that is indexed by a constant value which is not within
1384 the array bounds cannot be stored in a register either; because we
1385 would get a crash in store_bit_field/extract_bit_field when trying
1386 to access a non-existent part of the register. */
1387 if (TREE_CODE (index) == INTEGER_CST
1388 && TYPE_VALUES (TREE_TYPE (array))
1389 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1391 if (mark_addressable (array) == 0)
1392 return error_mark_node;
1395 if (pedantic && !lvalue_p (array))
1397 if (DECL_REGISTER (array))
1398 pedwarn ("ANSI C forbids subscripting `register' array");
1400 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1406 while (TREE_CODE (foo) == COMPONENT_REF)
1407 foo = TREE_OPERAND (foo, 0);
1408 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1409 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1412 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1413 rval = build (ARRAY_REF, type, array, index);
1414 /* Array ref is const/volatile if the array elements are
1415 or if the array is. */
1416 TREE_READONLY (rval)
1417 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1418 | TREE_READONLY (array));
1419 TREE_SIDE_EFFECTS (rval)
1420 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1421 | TREE_SIDE_EFFECTS (array));
1422 TREE_THIS_VOLATILE (rval)
1423 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1424 /* This was added by rms on 16 Nov 91.
1425 It fixes vol struct foo *a; a->elts[1]
1426 in an inline function.
1427 Hope it doesn't break something else. */
1428 | TREE_THIS_VOLATILE (array));
1429 return require_complete_type (fold (rval));
1433 tree ar = default_conversion (array);
1434 tree ind = default_conversion (index);
1436 /* Put the integer in IND to simplify error checking. */
1437 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1444 if (ar == error_mark_node)
1447 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1448 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1450 error ("subscripted value is neither array nor pointer");
1451 return error_mark_node;
1453 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1455 error ("array subscript is not an integer");
1456 return error_mark_node;
1459 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1464 /* Build a function call to function FUNCTION with parameters PARAMS.
1465 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1466 TREE_VALUE of each node is a parameter-expression.
1467 FUNCTION's data type may be a function type or a pointer-to-function. */
1470 build_function_call (function, params)
1471 tree function, params;
1473 register tree fntype, fundecl = 0;
1474 register tree coerced_params;
1475 tree name = NULL_TREE, assembler_name = NULL_TREE;
1477 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1478 STRIP_TYPE_NOPS (function);
1480 /* Convert anything with function type to a pointer-to-function. */
1481 if (TREE_CODE (function) == FUNCTION_DECL)
1483 name = DECL_NAME (function);
1484 assembler_name = DECL_ASSEMBLER_NAME (function);
1486 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1487 (because calling an inline function does not mean the function
1488 needs to be separately compiled). */
1489 fntype = build_type_variant (TREE_TYPE (function),
1490 TREE_READONLY (function),
1491 TREE_THIS_VOLATILE (function));
1493 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1496 function = default_conversion (function);
1498 fntype = TREE_TYPE (function);
1500 if (TREE_CODE (fntype) == ERROR_MARK)
1501 return error_mark_node;
1503 if (!(TREE_CODE (fntype) == POINTER_TYPE
1504 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1506 error ("called object is not a function");
1507 return error_mark_node;
1510 /* fntype now gets the type of function pointed to. */
1511 fntype = TREE_TYPE (fntype);
1513 /* Convert the parameters to the types declared in the
1514 function prototype, or apply default promotions. */
1517 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1519 /* Check for errors in format strings. */
1521 if (warn_format && (name || assembler_name))
1522 check_function_format (name, assembler_name, coerced_params);
1524 /* Recognize certain built-in functions so we can make tree-codes
1525 other than CALL_EXPR. We do this when it enables fold-const.c
1526 to do something useful. */
1528 if (TREE_CODE (function) == ADDR_EXPR
1529 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1530 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1531 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1536 if (coerced_params == 0)
1537 return integer_zero_node;
1538 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1542 register tree result
1543 = build (CALL_EXPR, TREE_TYPE (fntype),
1544 function, coerced_params, NULL_TREE);
1546 TREE_SIDE_EFFECTS (result) = 1;
1547 if (TREE_TYPE (result) == void_type_node)
1549 return require_complete_type (result);
1553 /* Convert the argument expressions in the list VALUES
1554 to the types in the list TYPELIST. The result is a list of converted
1555 argument expressions.
1557 If TYPELIST is exhausted, or when an element has NULL as its type,
1558 perform the default conversions.
1560 PARMLIST is the chain of parm decls for the function being called.
1561 It may be 0, if that info is not available.
1562 It is used only for generating error messages.
1564 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1566 This is also where warnings about wrong number of args are generated.
1568 Both VALUES and the returned value are chains of TREE_LIST nodes
1569 with the elements of the list in the TREE_VALUE slots of those nodes. */
1572 convert_arguments (typelist, values, name, fundecl)
1573 tree typelist, values, name, fundecl;
1575 register tree typetail, valtail;
1576 register tree result = NULL;
1579 /* Scan the given expressions and types, producing individual
1580 converted arguments and pushing them on RESULT in reverse order. */
1582 for (valtail = values, typetail = typelist, parmnum = 0;
1584 valtail = TREE_CHAIN (valtail), parmnum++)
1586 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1587 register tree val = TREE_VALUE (valtail);
1589 if (type == void_type_node)
1592 error ("too many arguments to function `%s'",
1593 IDENTIFIER_POINTER (name));
1595 error ("too many arguments to function");
1599 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1600 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1601 to convert automatically to a pointer. */
1602 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1603 val = TREE_OPERAND (val, 0);
1605 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1606 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1607 val = default_conversion (val);
1609 val = require_complete_type (val);
1613 /* Formal parm type is specified by a function prototype. */
1616 if (TYPE_SIZE (type) == 0)
1618 error ("type of formal parameter %d is incomplete", parmnum + 1);
1623 /* Optionally warn about conversions that
1624 differ from the default conversions. */
1625 if (warn_conversion)
1627 int formal_prec = TYPE_PRECISION (type);
1629 if (INTEGRAL_TYPE_P (type)
1630 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1631 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1632 else if (TREE_CODE (type) == COMPLEX_TYPE
1633 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1634 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1635 else if (TREE_CODE (type) == REAL_TYPE
1636 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1637 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1638 else if (TREE_CODE (type) == REAL_TYPE
1639 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1640 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1641 /* ??? At some point, messages should be written about
1642 conversions between complex types, but that's too messy
1644 else if (TREE_CODE (type) == REAL_TYPE
1645 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1647 /* Warn if any argument is passed as `float',
1648 since without a prototype it would be `double'. */
1649 if (formal_prec == TYPE_PRECISION (float_type_node))
1650 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1652 /* Detect integer changing in width or signedness. */
1653 else if (INTEGRAL_TYPE_P (type)
1654 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1656 tree would_have_been = default_conversion (val);
1657 tree type1 = TREE_TYPE (would_have_been);
1659 if (TREE_CODE (type) == ENUMERAL_TYPE
1660 && type == TREE_TYPE (val))
1661 /* No warning if function asks for enum
1662 and the actual arg is that enum type. */
1664 else if (formal_prec != TYPE_PRECISION (type1))
1665 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1666 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1668 /* Don't complain if the formal parameter type
1669 is an enum, because we can't tell now whether
1670 the value was an enum--even the same enum. */
1671 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1673 else if (TREE_CODE (val) == INTEGER_CST
1674 && int_fits_type_p (val, type))
1675 /* Change in signedness doesn't matter
1676 if a constant value is unaffected. */
1678 /* Likewise for a constant in a NOP_EXPR. */
1679 else if (TREE_CODE (val) == NOP_EXPR
1680 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1681 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1683 #if 0 /* We never get such tree structure here. */
1684 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1685 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1686 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1687 /* Change in signedness doesn't matter
1688 if an enum value is unaffected. */
1691 /* If the value is extended from a narrower
1692 unsigned type, it doesn't matter whether we
1693 pass it as signed or unsigned; the value
1694 certainly is the same either way. */
1695 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1696 && TREE_UNSIGNED (TREE_TYPE (val)))
1698 else if (TREE_UNSIGNED (type))
1699 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1701 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1705 parmval = convert_for_assignment (type, val,
1706 (char *)0, /* arg passing */
1707 fundecl, name, parmnum + 1);
1709 #ifdef PROMOTE_PROTOTYPES
1710 if ((TREE_CODE (type) == INTEGER_TYPE
1711 || TREE_CODE (type) == ENUMERAL_TYPE)
1712 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1713 parmval = default_conversion (parmval);
1716 result = tree_cons (NULL_TREE, parmval, result);
1718 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1719 && (TYPE_PRECISION (TREE_TYPE (val))
1720 < TYPE_PRECISION (double_type_node)))
1721 /* Convert `float' to `double'. */
1722 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1724 /* Convert `short' and `char' to full-size `int'. */
1725 result = tree_cons (NULL_TREE, default_conversion (val), result);
1728 typetail = TREE_CHAIN (typetail);
1731 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1734 error ("too few arguments to function `%s'",
1735 IDENTIFIER_POINTER (name));
1737 error ("too few arguments to function");
1740 return nreverse (result);
1743 /* This is the entry point used by the parser
1744 for binary operators in the input.
1745 In addition to constructing the expression,
1746 we check for operands that were written with other binary operators
1747 in a way that is likely to confuse the user. */
1750 parser_build_binary_op (code, arg1, arg2)
1751 enum tree_code code;
1754 tree result = build_binary_op (code, arg1, arg2, 1);
1757 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1758 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1759 enum tree_code code1 = ERROR_MARK;
1760 enum tree_code code2 = ERROR_MARK;
1762 if (class1 == 'e' || class1 == '1'
1763 || class1 == '2' || class1 == '<')
1764 code1 = C_EXP_ORIGINAL_CODE (arg1);
1765 if (class2 == 'e' || class2 == '1'
1766 || class2 == '2' || class2 == '<')
1767 code2 = C_EXP_ORIGINAL_CODE (arg2);
1769 /* Check for cases such as x+y<<z which users are likely
1770 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1771 is cleared to prevent these warnings. */
1772 if (warn_parentheses)
1774 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1776 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1777 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1778 warning ("suggest parentheses around + or - inside shift");
1781 if (code == TRUTH_ORIF_EXPR)
1783 if (code1 == TRUTH_ANDIF_EXPR
1784 || code2 == TRUTH_ANDIF_EXPR)
1785 warning ("suggest parentheses around && within ||");
1788 if (code == BIT_IOR_EXPR)
1790 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1791 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1792 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1793 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1794 warning ("suggest parentheses around arithmetic in operand of |");
1795 /* Check cases like x|y==z */
1796 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1797 warning ("suggest parentheses around comparison in operand of |");
1800 if (code == BIT_XOR_EXPR)
1802 if (code1 == BIT_AND_EXPR
1803 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1804 || code2 == BIT_AND_EXPR
1805 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1806 warning ("suggest parentheses around arithmetic in operand of ^");
1807 /* Check cases like x^y==z */
1808 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1809 warning ("suggest parentheses around comparison in operand of ^");
1812 if (code == BIT_AND_EXPR)
1814 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1815 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1816 warning ("suggest parentheses around + or - in operand of &");
1817 /* Check cases like x&y==z */
1818 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1819 warning ("suggest parentheses around comparison in operand of &");
1823 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1824 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1825 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1826 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1828 unsigned_conversion_warning (result, arg1);
1829 unsigned_conversion_warning (result, arg2);
1830 overflow_warning (result);
1832 class = TREE_CODE_CLASS (TREE_CODE (result));
1834 /* Record the code that was specified in the source,
1835 for the sake of warnings about confusing nesting. */
1836 if (class == 'e' || class == '1'
1837 || class == '2' || class == '<')
1838 C_SET_EXP_ORIGINAL_CODE (result, code);
1841 int flag = TREE_CONSTANT (result);
1842 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1843 so that convert_for_assignment wouldn't strip it.
1844 That way, we got warnings for things like p = (1 - 1).
1845 But it turns out we should not get those warnings. */
1846 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1847 C_SET_EXP_ORIGINAL_CODE (result, code);
1848 TREE_CONSTANT (result) = flag;
1854 /* Build a binary-operation expression without default conversions.
1855 CODE is the kind of expression to build.
1856 This function differs from `build' in several ways:
1857 the data type of the result is computed and recorded in it,
1858 warnings are generated if arg data types are invalid,
1859 special handling for addition and subtraction of pointers is known,
1860 and some optimization is done (operations on narrow ints
1861 are done in the narrower type when that gives the same result).
1862 Constant folding is also done before the result is returned.
1864 Note that the operands will never have enumeral types, or function
1865 or array types, because either they will have the default conversions
1866 performed or they have both just been converted to some other type in which
1867 the arithmetic is to be done. */
1870 build_binary_op (code, orig_op0, orig_op1, convert_p)
1871 enum tree_code code;
1872 tree orig_op0, orig_op1;
1876 register enum tree_code code0, code1;
1879 /* Expression code to give to the expression when it is built.
1880 Normally this is CODE, which is what the caller asked for,
1881 but in some special cases we change it. */
1882 register enum tree_code resultcode = code;
1884 /* Data type in which the computation is to be performed.
1885 In the simplest cases this is the common type of the arguments. */
1886 register tree result_type = NULL;
1888 /* Nonzero means operands have already been type-converted
1889 in whatever way is necessary.
1890 Zero means they need to be converted to RESULT_TYPE. */
1893 /* Nonzero means create the expression with this type, rather than
1895 tree build_type = 0;
1897 /* Nonzero means after finally constructing the expression
1898 convert it to this type. */
1899 tree final_type = 0;
1901 /* Nonzero if this is an operation like MIN or MAX which can
1902 safely be computed in short if both args are promoted shorts.
1903 Also implies COMMON.
1904 -1 indicates a bitwise operation; this makes a difference
1905 in the exact conditions for when it is safe to do the operation
1906 in a narrower mode. */
1909 /* Nonzero if this is a comparison operation;
1910 if both args are promoted shorts, compare the original shorts.
1911 Also implies COMMON. */
1912 int short_compare = 0;
1914 /* Nonzero if this is a right-shift operation, which can be computed on the
1915 original short and then promoted if the operand is a promoted short. */
1916 int short_shift = 0;
1918 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1923 op0 = default_conversion (orig_op0);
1924 op1 = default_conversion (orig_op1);
1932 type0 = TREE_TYPE (op0);
1933 type1 = TREE_TYPE (op1);
1935 /* The expression codes of the data types of the arguments tell us
1936 whether the arguments are integers, floating, pointers, etc. */
1937 code0 = TREE_CODE (type0);
1938 code1 = TREE_CODE (type1);
1940 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1941 STRIP_TYPE_NOPS (op0);
1942 STRIP_TYPE_NOPS (op1);
1944 /* If an error was already reported for one of the arguments,
1945 avoid reporting another error. */
1947 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1948 return error_mark_node;
1953 /* Handle the pointer + int case. */
1954 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1955 return pointer_int_sum (PLUS_EXPR, op0, op1);
1956 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1957 return pointer_int_sum (PLUS_EXPR, op1, op0);
1963 /* Subtraction of two similar pointers.
1964 We must subtract them as integers, then divide by object size. */
1965 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1966 && comp_target_types (type0, type1))
1967 return pointer_diff (op0, op1);
1968 /* Handle pointer minus int. Just like pointer plus int. */
1969 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1970 return pointer_int_sum (MINUS_EXPR, op0, op1);
1979 case TRUNC_DIV_EXPR:
1981 case FLOOR_DIV_EXPR:
1982 case ROUND_DIV_EXPR:
1983 case EXACT_DIV_EXPR:
1984 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1985 || code0 == COMPLEX_TYPE)
1986 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1987 || code1 == COMPLEX_TYPE))
1989 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
1990 resultcode = RDIV_EXPR;
1993 /* Although it would be tempting to shorten always here, that
1994 loses on some targets, since the modulo instruction is
1995 undefined if the quotient can't be represented in the
1996 computation mode. We shorten only if unsigned or if
1997 dividing by something we know != -1. */
1998 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
1999 || (TREE_CODE (op1) == INTEGER_CST
2000 && (TREE_INT_CST_LOW (op1) != -1
2001 || TREE_INT_CST_HIGH (op1) != -1)));
2008 case BIT_ANDTC_EXPR:
2011 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2013 /* If one operand is a constant, and the other is a short type
2014 that has been converted to an int,
2015 really do the work in the short type and then convert the
2016 result to int. If we are lucky, the constant will be 0 or 1
2017 in the short type, making the entire operation go away. */
2018 if (TREE_CODE (op0) == INTEGER_CST
2019 && TREE_CODE (op1) == NOP_EXPR
2020 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2021 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2023 final_type = result_type;
2024 op1 = TREE_OPERAND (op1, 0);
2025 result_type = TREE_TYPE (op1);
2027 if (TREE_CODE (op1) == INTEGER_CST
2028 && TREE_CODE (op0) == NOP_EXPR
2029 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2030 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2032 final_type = result_type;
2033 op0 = TREE_OPERAND (op0, 0);
2034 result_type = TREE_TYPE (op0);
2038 case TRUNC_MOD_EXPR:
2039 case FLOOR_MOD_EXPR:
2040 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2042 /* Although it would be tempting to shorten always here, that loses
2043 on some targets, since the modulo instruction is undefined if the
2044 quotient can't be represented in the computation mode. We shorten
2045 only if unsigned or if dividing by something we know != -1. */
2046 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2047 || (TREE_CODE (op1) == INTEGER_CST
2048 && (TREE_INT_CST_LOW (op1) != -1
2049 || TREE_INT_CST_HIGH (op1) != -1)));
2054 case TRUTH_ANDIF_EXPR:
2055 case TRUTH_ORIF_EXPR:
2056 case TRUTH_AND_EXPR:
2058 case TRUTH_XOR_EXPR:
2059 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2060 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2061 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2062 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2064 /* Result of these operations is always an int,
2065 but that does not mean the operands should be
2066 converted to ints! */
2067 result_type = integer_type_node;
2068 op0 = truthvalue_conversion (op0);
2069 op1 = truthvalue_conversion (op1);
2074 /* Shift operations: result has same type as first operand;
2075 always convert second operand to int.
2076 Also set SHORT_SHIFT if shifting rightward. */
2079 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2081 if (TREE_CODE (op1) == INTEGER_CST)
2083 if (tree_int_cst_sgn (op1) < 0)
2084 warning ("right shift count is negative");
2087 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2089 if (TREE_INT_CST_HIGH (op1) != 0
2090 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2091 >= TYPE_PRECISION (type0)))
2092 warning ("right shift count >= width of type");
2095 /* Use the type of the value to be shifted.
2096 This is what most traditional C compilers do. */
2097 result_type = type0;
2098 /* Unless traditional, convert the shift-count to an integer,
2099 regardless of size of value being shifted. */
2100 if (! flag_traditional)
2102 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2103 op1 = convert (integer_type_node, op1);
2104 /* Avoid converting op1 to result_type later. */
2111 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2113 if (TREE_CODE (op1) == INTEGER_CST)
2115 if (tree_int_cst_sgn (op1) < 0)
2116 warning ("left shift count is negative");
2117 else if (TREE_INT_CST_HIGH (op1) != 0
2118 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2119 >= TYPE_PRECISION (type0)))
2120 warning ("left shift count >= width of type");
2122 /* Use the type of the value to be shifted.
2123 This is what most traditional C compilers do. */
2124 result_type = type0;
2125 /* Unless traditional, convert the shift-count to an integer,
2126 regardless of size of value being shifted. */
2127 if (! flag_traditional)
2129 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2130 op1 = convert (integer_type_node, op1);
2131 /* Avoid converting op1 to result_type later. */
2139 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2141 if (TREE_CODE (op1) == INTEGER_CST)
2143 if (tree_int_cst_sgn (op1) < 0)
2144 warning ("shift count is negative");
2145 else if (TREE_INT_CST_HIGH (op1) != 0
2146 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2147 >= TYPE_PRECISION (type0)))
2148 warning ("shift count >= width of type");
2150 /* Use the type of the value to be shifted.
2151 This is what most traditional C compilers do. */
2152 result_type = type0;
2153 /* Unless traditional, convert the shift-count to an integer,
2154 regardless of size of value being shifted. */
2155 if (! flag_traditional)
2157 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2158 op1 = convert (integer_type_node, op1);
2159 /* Avoid converting op1 to result_type later. */
2167 /* Result of comparison is always int,
2168 but don't convert the args to int! */
2169 build_type = integer_type_node;
2170 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2171 || code0 == COMPLEX_TYPE)
2172 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2173 || code1 == COMPLEX_TYPE))
2175 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2177 register tree tt0 = TREE_TYPE (type0);
2178 register tree tt1 = TREE_TYPE (type1);
2179 /* Anything compares with void *. void * compares with anything.
2180 Otherwise, the targets must be compatible
2181 and both must be object or both incomplete. */
2182 if (comp_target_types (type0, type1))
2183 result_type = common_type (type0, type1);
2184 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2186 /* op0 != orig_op0 detects the case of something
2187 whose value is 0 but which isn't a valid null ptr const. */
2188 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2189 && TREE_CODE (tt1) == FUNCTION_TYPE)
2190 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2192 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2194 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2195 && TREE_CODE (tt0) == FUNCTION_TYPE)
2196 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2199 pedwarn ("comparison of distinct pointer types lacks a cast");
2201 if (result_type == NULL_TREE)
2202 result_type = ptr_type_node;
2204 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2205 && integer_zerop (op1))
2206 result_type = type0;
2207 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2208 && integer_zerop (op0))
2209 result_type = type1;
2210 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2212 result_type = type0;
2213 if (! flag_traditional)
2214 pedwarn ("comparison between pointer and integer");
2216 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2218 result_type = type1;
2219 if (! flag_traditional)
2220 pedwarn ("comparison between pointer and integer");
2226 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2227 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2229 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2231 if (comp_target_types (type0, type1))
2233 result_type = common_type (type0, type1);
2235 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2236 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2240 result_type = ptr_type_node;
2241 pedwarn ("comparison of distinct pointer types lacks a cast");
2250 build_type = integer_type_node;
2251 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2252 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2254 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2256 if (comp_target_types (type0, type1))
2258 result_type = common_type (type0, type1);
2259 if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2260 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2261 pedwarn ("comparison of complete and incomplete pointers");
2263 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2264 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2268 result_type = ptr_type_node;
2269 pedwarn ("comparison of distinct pointer types lacks a cast");
2272 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2273 && integer_zerop (op1))
2275 result_type = type0;
2276 if (pedantic || extra_warnings)
2277 pedwarn ("ordered comparison of pointer with integer zero");
2279 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2280 && integer_zerop (op0))
2282 result_type = type1;
2284 pedwarn ("ordered comparison of pointer with integer zero");
2286 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2288 result_type = type0;
2289 if (! flag_traditional)
2290 pedwarn ("comparison between pointer and integer");
2292 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2294 result_type = type1;
2295 if (! flag_traditional)
2296 pedwarn ("comparison between pointer and integer");
2301 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2303 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2305 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2307 if (shorten || common || short_compare)
2308 result_type = common_type (type0, type1);
2310 /* For certain operations (which identify themselves by shorten != 0)
2311 if both args were extended from the same smaller type,
2312 do the arithmetic in that type and then extend.
2314 shorten !=0 and !=1 indicates a bitwise operation.
2315 For them, this optimization is safe only if
2316 both args are zero-extended or both are sign-extended.
2317 Otherwise, we might change the result.
2318 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2319 but calculated in (unsigned short) it would be (unsigned short)-1. */
2321 if (shorten && none_complex)
2323 int unsigned0, unsigned1;
2324 tree arg0 = get_narrower (op0, &unsigned0);
2325 tree arg1 = get_narrower (op1, &unsigned1);
2326 /* UNS is 1 if the operation to be done is an unsigned one. */
2327 int uns = TREE_UNSIGNED (result_type);
2330 final_type = result_type;
2332 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2333 but it *requires* conversion to FINAL_TYPE. */
2335 if ((TYPE_PRECISION (TREE_TYPE (op0))
2336 == TYPE_PRECISION (TREE_TYPE (arg0)))
2337 && TREE_TYPE (op0) != final_type)
2338 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2339 if ((TYPE_PRECISION (TREE_TYPE (op1))
2340 == TYPE_PRECISION (TREE_TYPE (arg1)))
2341 && TREE_TYPE (op1) != final_type)
2342 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2344 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2346 /* For bitwise operations, signedness of nominal type
2347 does not matter. Consider only how operands were extended. */
2351 /* Note that in all three cases below we refrain from optimizing
2352 an unsigned operation on sign-extended args.
2353 That would not be valid. */
2355 /* Both args variable: if both extended in same way
2356 from same width, do it in that width.
2357 Do it unsigned if args were zero-extended. */
2358 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2359 < TYPE_PRECISION (result_type))
2360 && (TYPE_PRECISION (TREE_TYPE (arg1))
2361 == TYPE_PRECISION (TREE_TYPE (arg0)))
2362 && unsigned0 == unsigned1
2363 && (unsigned0 || !uns))
2365 = signed_or_unsigned_type (unsigned0,
2366 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2367 else if (TREE_CODE (arg0) == INTEGER_CST
2368 && (unsigned1 || !uns)
2369 && (TYPE_PRECISION (TREE_TYPE (arg1))
2370 < TYPE_PRECISION (result_type))
2371 && (type = signed_or_unsigned_type (unsigned1,
2373 int_fits_type_p (arg0, type)))
2375 else if (TREE_CODE (arg1) == INTEGER_CST
2376 && (unsigned0 || !uns)
2377 && (TYPE_PRECISION (TREE_TYPE (arg0))
2378 < TYPE_PRECISION (result_type))
2379 && (type = signed_or_unsigned_type (unsigned0,
2381 int_fits_type_p (arg1, type)))
2385 /* Shifts can be shortened if shifting right. */
2390 tree arg0 = get_narrower (op0, &unsigned_arg);
2392 final_type = result_type;
2394 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2395 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2397 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2398 /* We can shorten only if the shift count is less than the
2399 number of bits in the smaller type size. */
2400 && TREE_INT_CST_HIGH (op1) == 0
2401 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
2402 /* If arg is sign-extended and then unsigned-shifted,
2403 we can simulate this with a signed shift in arg's type
2404 only if the extended result is at least twice as wide
2405 as the arg. Otherwise, the shift could use up all the
2406 ones made by sign-extension and bring in zeros.
2407 We can't optimize that case at all, but in most machines
2408 it never happens because available widths are 2**N. */
2409 && (!TREE_UNSIGNED (final_type)
2411 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2413 /* Do an unsigned shift if the operand was zero-extended. */
2415 = signed_or_unsigned_type (unsigned_arg,
2417 /* Convert value-to-be-shifted to that type. */
2418 if (TREE_TYPE (op0) != result_type)
2419 op0 = convert (result_type, op0);
2424 /* Comparison operations are shortened too but differently.
2425 They identify themselves by setting short_compare = 1. */
2429 /* Don't write &op0, etc., because that would prevent op0
2430 from being kept in a register.
2431 Instead, make copies of the our local variables and
2432 pass the copies by reference, then copy them back afterward. */
2433 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2434 enum tree_code xresultcode = resultcode;
2436 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2439 op0 = xop0, op1 = xop1;
2441 resultcode = xresultcode;
2443 if (warn_sign_compare)
2445 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2446 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2448 int unsignedp0, unsignedp1;
2449 tree primop0 = get_narrower (op0, &unsignedp0);
2450 tree primop1 = get_narrower (op1, &unsignedp1);
2452 /* Avoid spurious warnings for comparison with enumerators. */
2456 STRIP_TYPE_NOPS (xop0);
2457 STRIP_TYPE_NOPS (xop1);
2459 /* Give warnings for comparisons between signed and unsigned
2460 quantities that may fail. */
2461 /* Do the checking based on the original operand trees, so that
2462 casts will be considered, but default promotions won't be. */
2464 /* Do not warn if the comparison is being done in a signed type,
2465 since the signed type will only be chosen if it can represent
2466 all the values of the unsigned type. */
2467 if (! TREE_UNSIGNED (result_type))
2469 /* Do not warn if both operands are unsigned. */
2470 else if (op0_signed == op1_signed)
2472 /* Do not warn if the signed quantity is an unsuffixed
2473 integer literal (or some static constant expression
2474 involving such literals) and it is non-negative. */
2475 else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST
2476 && tree_int_cst_sgn (xop0) >= 0)
2477 || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
2478 && tree_int_cst_sgn (xop1) >= 0))
2480 /* Do not warn if the comparison is an equality operation,
2481 the unsigned quantity is an integral constant and it does
2482 not use the most significant bit of result_type. */
2483 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
2484 && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST
2485 && int_fits_type_p (xop1, signed_type (result_type)))
2486 || (op1_signed && TREE_CODE (xop0) == INTEGER_CST
2487 && int_fits_type_p (xop0, signed_type (result_type)))))
2490 warning ("comparison between signed and unsigned");
2492 /* Warn if two unsigned values are being compared in a size
2493 larger than their original size, and one (and only one) is the
2494 result of a `~' operator. This comparison will always fail.
2496 Also warn if one operand is a constant, and the constant
2497 does not have all bits set that are set in the ~ operand
2498 when it is extended. */
2500 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2501 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2503 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2504 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2507 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2510 if (TREE_CODE (primop0) == INTEGER_CST
2511 || TREE_CODE (primop1) == INTEGER_CST)
2514 long constant, mask;
2515 int unsignedp, bits;
2517 if (TREE_CODE (primop0) == INTEGER_CST)
2520 unsignedp = unsignedp1;
2521 constant = TREE_INT_CST_LOW (primop0);
2526 unsignedp = unsignedp0;
2527 constant = TREE_INT_CST_LOW (primop1);
2530 bits = TYPE_PRECISION (TREE_TYPE (primop));
2531 if (bits < TYPE_PRECISION (result_type)
2532 && bits < HOST_BITS_PER_LONG && unsignedp)
2534 mask = (~0L) << bits;
2535 if ((mask & constant) != mask)
2536 warning ("comparison of promoted ~unsigned with constant");
2539 else if (unsignedp0 && unsignedp1
2540 && (TYPE_PRECISION (TREE_TYPE (primop0))
2541 < TYPE_PRECISION (result_type))
2542 && (TYPE_PRECISION (TREE_TYPE (primop1))
2543 < TYPE_PRECISION (result_type)))
2544 warning ("comparison of promoted ~unsigned with unsigned");
2550 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2551 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2552 Then the expression will be built.
2553 It will be given type FINAL_TYPE if that is nonzero;
2554 otherwise, it will be given type RESULT_TYPE. */
2558 binary_op_error (code);
2559 return error_mark_node;
2564 if (TREE_TYPE (op0) != result_type)
2565 op0 = convert (result_type, op0);
2566 if (TREE_TYPE (op1) != result_type)
2567 op1 = convert (result_type, op1);
2570 if (build_type == NULL_TREE)
2571 build_type = result_type;
2574 register tree result = build (resultcode, build_type, op0, op1);
2575 register tree folded;
2577 folded = fold (result);
2578 if (folded == result)
2579 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2580 if (final_type != 0)
2581 return convert (final_type, folded);
2586 /* Return a tree for the sum or difference (RESULTCODE says which)
2587 of pointer PTROP and integer INTOP. */
2590 pointer_int_sum (resultcode, ptrop, intop)
2591 enum tree_code resultcode;
2592 register tree ptrop, intop;
2596 register tree result;
2597 register tree folded;
2599 /* The result is a pointer of the same type that is being added. */
2601 register tree result_type = TREE_TYPE (ptrop);
2603 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2605 if (pedantic || warn_pointer_arith)
2606 pedwarn ("pointer of type `void *' used in arithmetic");
2607 size_exp = integer_one_node;
2609 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2611 if (pedantic || warn_pointer_arith)
2612 pedwarn ("pointer to a function used in arithmetic");
2613 size_exp = integer_one_node;
2616 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2618 /* If what we are about to multiply by the size of the elements
2619 contains a constant term, apply distributive law
2620 and multiply that constant term separately.
2621 This helps produce common subexpressions. */
2623 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2624 && ! TREE_CONSTANT (intop)
2625 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2626 && TREE_CONSTANT (size_exp)
2627 /* If the constant comes from pointer subtraction,
2628 skip this optimization--it would cause an error. */
2629 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2630 /* If the constant is unsigned, and smaller than the pointer size,
2631 then we must skip this optimization. This is because it could cause
2632 an overflow error if the constant is negative but INTOP is not. */
2633 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2634 || (TYPE_PRECISION (TREE_TYPE (intop))
2635 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2637 enum tree_code subcode = resultcode;
2638 tree int_type = TREE_TYPE (intop);
2639 if (TREE_CODE (intop) == MINUS_EXPR)
2640 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2641 /* Convert both subexpression types to the type of intop,
2642 because weird cases involving pointer arithmetic
2643 can result in a sum or difference with different type args. */
2644 ptrop = build_binary_op (subcode, ptrop,
2645 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2646 intop = convert (int_type, TREE_OPERAND (intop, 0));
2649 /* Convert the integer argument to a type the same size as sizetype
2650 so the multiply won't overflow spuriously. */
2652 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
2653 intop = convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
2655 /* Replace the integer argument with a suitable product by the object size.
2656 Do this multiplication as signed, then convert to the appropriate
2657 pointer type (actually unsigned integral). */
2659 intop = convert (result_type,
2660 build_binary_op (MULT_EXPR, intop,
2661 convert (TREE_TYPE (intop), size_exp), 1));
2663 /* Create the sum or difference. */
2665 result = build (resultcode, result_type, ptrop, intop);
2667 folded = fold (result);
2668 if (folded == result)
2669 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2673 /* Return a tree for the difference of pointers OP0 and OP1.
2674 The resulting tree has type int. */
2677 pointer_diff (op0, op1)
2678 register tree op0, op1;
2680 register tree result, folded;
2681 tree restype = ptrdiff_type_node;
2683 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2685 if (pedantic || warn_pointer_arith)
2687 if (TREE_CODE (target_type) == VOID_TYPE)
2688 pedwarn ("pointer of type `void *' used in subtraction");
2689 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2690 pedwarn ("pointer to a function used in subtraction");
2693 /* First do the subtraction as integers;
2694 then drop through to build the divide operator. */
2696 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2697 convert (restype, op1), 1);
2698 /* This generates an error if op1 is pointer to incomplete type. */
2699 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2700 error ("arithmetic on pointer to an incomplete type");
2702 /* This generates an error if op0 is pointer to incomplete type. */
2703 op1 = c_size_in_bytes (target_type);
2705 /* Divide by the size, in easiest possible way. */
2707 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2709 folded = fold (result);
2710 if (folded == result)
2711 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2715 /* Construct and perhaps optimize a tree representation
2716 for a unary operation. CODE, a tree_code, specifies the operation
2717 and XARG is the operand. NOCONVERT nonzero suppresses
2718 the default promotions (such as from short to int). */
2721 build_unary_op (code, xarg, noconvert)
2722 enum tree_code code;
2726 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2727 register tree arg = xarg;
2728 register tree argtype = 0;
2729 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2730 char *errstring = NULL;
2733 if (typecode == ERROR_MARK)
2734 return error_mark_node;
2735 if (typecode == ENUMERAL_TYPE)
2736 typecode = INTEGER_TYPE;
2741 /* This is used for unary plus, because a CONVERT_EXPR
2742 is enough to prevent anybody from looking inside for
2743 associativity, but won't generate any code. */
2744 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2745 || typecode == COMPLEX_TYPE))
2746 errstring = "wrong type argument to unary plus";
2747 else if (!noconvert)
2748 arg = default_conversion (arg);
2752 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2753 || typecode == COMPLEX_TYPE))
2754 errstring = "wrong type argument to unary minus";
2755 else if (!noconvert)
2756 arg = default_conversion (arg);
2760 if (typecode == COMPLEX_TYPE)
2764 arg = default_conversion (arg);
2766 else if (typecode != INTEGER_TYPE)
2767 errstring = "wrong type argument to bit-complement";
2768 else if (!noconvert)
2769 arg = default_conversion (arg);
2773 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2774 || typecode == COMPLEX_TYPE))
2775 errstring = "wrong type argument to abs";
2776 else if (!noconvert)
2777 arg = default_conversion (arg);
2781 /* Conjugating a real value is a no-op, but allow it anyway. */
2782 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2783 || typecode == COMPLEX_TYPE))
2784 errstring = "wrong type argument to conjugation";
2785 else if (!noconvert)
2786 arg = default_conversion (arg);
2789 case TRUTH_NOT_EXPR:
2790 if (typecode != INTEGER_TYPE
2791 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2792 && typecode != COMPLEX_TYPE
2793 /* These will convert to a pointer. */
2794 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2796 errstring = "wrong type argument to unary exclamation mark";
2799 arg = truthvalue_conversion (arg);
2800 return invert_truthvalue (arg);
2806 if (TREE_CODE (arg) == COMPLEX_CST)
2807 return TREE_REALPART (arg);
2808 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2809 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2814 if (TREE_CODE (arg) == COMPLEX_CST)
2815 return TREE_IMAGPART (arg);
2816 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2817 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2819 return convert (TREE_TYPE (arg), integer_zero_node);
2821 case PREINCREMENT_EXPR:
2822 case POSTINCREMENT_EXPR:
2823 case PREDECREMENT_EXPR:
2824 case POSTDECREMENT_EXPR:
2825 /* Handle complex lvalues (when permitted)
2826 by reduction to simpler cases. */
2828 val = unary_complex_lvalue (code, arg);
2832 /* Increment or decrement the real part of the value,
2833 and don't change the imaginary part. */
2834 if (typecode == COMPLEX_TYPE)
2838 arg = stabilize_reference (arg);
2839 real = build_unary_op (REALPART_EXPR, arg, 1);
2840 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2841 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2842 build_unary_op (code, real, 1), imag);
2845 /* Report invalid types. */
2847 if (typecode != POINTER_TYPE
2848 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2850 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2851 errstring ="wrong type argument to increment";
2853 errstring ="wrong type argument to decrement";
2859 tree result_type = TREE_TYPE (arg);
2861 arg = get_unwidened (arg, 0);
2862 argtype = TREE_TYPE (arg);
2864 /* Compute the increment. */
2866 if (typecode == POINTER_TYPE)
2868 /* If pointer target is an undefined struct,
2869 we just cannot know how to do the arithmetic. */
2870 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2871 error ("%s of pointer to unknown structure",
2872 ((code == PREINCREMENT_EXPR
2873 || code == POSTINCREMENT_EXPR)
2874 ? "increment" : "decrement"));
2875 else if ((pedantic || warn_pointer_arith)
2876 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2877 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2878 pedwarn ("wrong type argument to %s",
2879 ((code == PREINCREMENT_EXPR
2880 || code == POSTINCREMENT_EXPR)
2881 ? "increment" : "decrement"));
2882 inc = c_size_in_bytes (TREE_TYPE (result_type));
2885 inc = integer_one_node;
2887 inc = convert (argtype, inc);
2889 /* Handle incrementing a cast-expression. */
2892 switch (TREE_CODE (arg))
2897 case FIX_TRUNC_EXPR:
2898 case FIX_FLOOR_EXPR:
2899 case FIX_ROUND_EXPR:
2901 pedantic_lvalue_warning (CONVERT_EXPR);
2902 /* If the real type has the same machine representation
2903 as the type it is cast to, we can make better output
2904 by adding directly to the inside of the cast. */
2905 if ((TREE_CODE (TREE_TYPE (arg))
2906 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2907 && (TYPE_MODE (TREE_TYPE (arg))
2908 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2909 arg = TREE_OPERAND (arg, 0);
2912 tree incremented, modify, value;
2913 arg = stabilize_reference (arg);
2914 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2917 value = save_expr (arg);
2918 incremented = build (((code == PREINCREMENT_EXPR
2919 || code == POSTINCREMENT_EXPR)
2920 ? PLUS_EXPR : MINUS_EXPR),
2921 argtype, value, inc);
2922 TREE_SIDE_EFFECTS (incremented) = 1;
2923 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2924 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2925 TREE_USED (value) = 1;
2935 /* Complain about anything else that is not a true lvalue. */
2936 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2937 || code == POSTINCREMENT_EXPR)
2938 ? "increment" : "decrement")))
2939 return error_mark_node;
2941 /* Report a read-only lvalue. */
2942 if (TREE_READONLY (arg))
2943 readonly_warning (arg,
2944 ((code == PREINCREMENT_EXPR
2945 || code == POSTINCREMENT_EXPR)
2946 ? "increment" : "decrement"));
2948 val = build (code, TREE_TYPE (arg), arg, inc);
2949 TREE_SIDE_EFFECTS (val) = 1;
2950 val = convert (result_type, val);
2951 if (TREE_CODE (val) != code)
2952 TREE_NO_UNUSED_WARNING (val) = 1;
2957 /* Note that this operation never does default_conversion
2958 regardless of NOCONVERT. */
2960 /* Let &* cancel out to simplify resulting code. */
2961 if (TREE_CODE (arg) == INDIRECT_REF)
2963 /* Don't let this be an lvalue. */
2964 if (lvalue_p (TREE_OPERAND (arg, 0)))
2965 return non_lvalue (TREE_OPERAND (arg, 0));
2966 return TREE_OPERAND (arg, 0);
2969 /* For &x[y], return x+y */
2970 if (TREE_CODE (arg) == ARRAY_REF)
2972 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
2973 return error_mark_node;
2974 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2975 TREE_OPERAND (arg, 1), 1);
2978 /* Handle complex lvalues (when permitted)
2979 by reduction to simpler cases. */
2980 val = unary_complex_lvalue (code, arg);
2984 #if 0 /* Turned off because inconsistent;
2985 float f; *&(int)f = 3.4 stores in int format
2986 whereas (int)f = 3.4 stores in float format. */
2987 /* Address of a cast is just a cast of the address
2988 of the operand of the cast. */
2989 switch (TREE_CODE (arg))
2994 case FIX_TRUNC_EXPR:
2995 case FIX_FLOOR_EXPR:
2996 case FIX_ROUND_EXPR:
2999 pedwarn ("ANSI C forbids the address of a cast expression");
3000 return convert (build_pointer_type (TREE_TYPE (arg)),
3001 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3006 /* Allow the address of a constructor if all the elements
3008 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3010 /* Anything not already handled and not a true memory reference
3012 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
3013 return error_mark_node;
3015 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3016 argtype = TREE_TYPE (arg);
3017 /* If the lvalue is const or volatile,
3018 merge that into the type that the address will point to. */
3019 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
3020 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3022 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3023 argtype = c_build_type_variant (argtype,
3024 TREE_READONLY (arg),
3025 TREE_THIS_VOLATILE (arg));
3028 argtype = build_pointer_type (argtype);
3030 if (mark_addressable (arg) == 0)
3031 return error_mark_node;
3036 if (TREE_CODE (arg) == COMPONENT_REF)
3038 tree field = TREE_OPERAND (arg, 1);
3040 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3042 if (DECL_C_BIT_FIELD (field))
3044 error ("attempt to take address of bit-field structure member `%s'",
3045 IDENTIFIER_POINTER (DECL_NAME (field)));
3046 return error_mark_node;
3049 addr = convert (argtype, addr);
3051 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3054 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3055 size_int (BITS_PER_UNIT));
3056 int flag = TREE_CONSTANT (addr);
3057 addr = fold (build (PLUS_EXPR, argtype,
3058 addr, convert (argtype, offset)));
3059 TREE_CONSTANT (addr) = flag;
3063 addr = build1 (code, argtype, arg);
3065 /* Address of a static or external variable or
3066 file-scope function counts as a constant. */
3068 && ! (TREE_CODE (arg) == FUNCTION_DECL
3069 && DECL_CONTEXT (arg) != 0))
3070 TREE_CONSTANT (addr) = 1;
3078 argtype = TREE_TYPE (arg);
3079 return fold (build1 (code, argtype, arg));
3083 return error_mark_node;
3087 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3088 convert ARG with the same conversions in the same order
3089 and return the result. */
3092 convert_sequence (conversions, arg)
3096 switch (TREE_CODE (conversions))
3101 case FIX_TRUNC_EXPR:
3102 case FIX_FLOOR_EXPR:
3103 case FIX_ROUND_EXPR:
3105 return convert (TREE_TYPE (conversions),
3106 convert_sequence (TREE_OPERAND (conversions, 0),
3115 /* Return nonzero if REF is an lvalue valid for this language.
3116 Lvalues can be assigned, unless their type has TYPE_READONLY.
3117 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3123 register enum tree_code code = TREE_CODE (ref);
3130 return lvalue_p (TREE_OPERAND (ref, 0));
3141 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3142 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
3149 /* Return nonzero if REF is an lvalue valid for this language;
3150 otherwise, print an error message and return zero. */
3153 lvalue_or_else (ref, string)
3157 int win = lvalue_p (ref);
3159 error ("invalid lvalue in %s", string);
3163 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3164 for certain kinds of expressions which are not really lvalues
3165 but which we can accept as lvalues.
3167 If ARG is not a kind of expression we can handle, return zero. */
3170 unary_complex_lvalue (code, arg)
3171 enum tree_code code;
3174 /* Handle (a, b) used as an "lvalue". */
3175 if (TREE_CODE (arg) == COMPOUND_EXPR)
3177 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3178 pedantic_lvalue_warning (COMPOUND_EXPR);
3179 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3180 TREE_OPERAND (arg, 0), real_result);
3183 /* Handle (a ? b : c) used as an "lvalue". */
3184 if (TREE_CODE (arg) == COND_EXPR)
3186 pedantic_lvalue_warning (COND_EXPR);
3187 return (build_conditional_expr
3188 (TREE_OPERAND (arg, 0),
3189 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3190 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3196 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3197 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3200 pedantic_lvalue_warning (code)
3201 enum tree_code code;
3204 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3205 code == COND_EXPR ? "conditional"
3206 : code == COMPOUND_EXPR ? "compound" : "cast");
3209 /* Warn about storing in something that is `const'. */
3212 readonly_warning (arg, string)
3217 strcpy (buf, string);
3219 /* Forbid assignments to iterators. */
3220 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3222 strcat (buf, " of iterator `%s'");
3223 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3226 if (TREE_CODE (arg) == COMPONENT_REF)
3228 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3229 readonly_warning (TREE_OPERAND (arg, 0), string);
3232 strcat (buf, " of read-only member `%s'");
3233 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3236 else if (TREE_CODE (arg) == VAR_DECL)
3238 strcat (buf, " of read-only variable `%s'");
3239 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3243 pedwarn ("%s of read-only location", buf);
3247 /* Mark EXP saying that we need to be able to take the
3248 address of it; it should not be allocated in a register.
3249 Value is 1 if successful. */
3252 mark_addressable (exp)
3255 register tree x = exp;
3257 switch (TREE_CODE (x))
3260 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3262 error ("cannot take address of bitfield `%s'",
3263 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3267 /* ... fall through ... */
3273 x = TREE_OPERAND (x, 0);
3277 TREE_ADDRESSABLE (x) = 1;
3284 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3285 && DECL_NONLOCAL (x))
3287 if (TREE_PUBLIC (x))
3289 error ("global register variable `%s' used in nested function",
3290 IDENTIFIER_POINTER (DECL_NAME (x)));
3293 pedwarn ("register variable `%s' used in nested function",
3294 IDENTIFIER_POINTER (DECL_NAME (x)));
3296 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3298 if (TREE_PUBLIC (x))
3300 error ("address of global register variable `%s' requested",
3301 IDENTIFIER_POINTER (DECL_NAME (x)));
3305 /* If we are making this addressable due to its having
3306 volatile components, give a different error message. Also
3307 handle the case of an unnamed parameter by not trying
3308 to give the name. */
3310 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3312 error ("cannot put object with volatile field into register");
3316 pedwarn ("address of register variable `%s' requested",
3317 IDENTIFIER_POINTER (DECL_NAME (x)));
3319 put_var_into_stack (x);
3323 TREE_ADDRESSABLE (x) = 1;
3324 #if 0 /* poplevel deals with this now. */
3325 if (DECL_CONTEXT (x) == 0)
3326 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3334 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3337 build_conditional_expr (ifexp, op1, op2)
3338 tree ifexp, op1, op2;
3340 register tree type1;
3341 register tree type2;
3342 register enum tree_code code1;
3343 register enum tree_code code2;
3344 register tree result_type = NULL;
3345 tree orig_op1 = op1, orig_op2 = op2;
3347 /* If second operand is omitted, it is the same as the first one;
3348 make sure it is calculated only once. */
3352 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression");
3353 ifexp = orig_op1 = op1 = save_expr (ifexp);
3356 ifexp = truthvalue_conversion (default_conversion (ifexp));
3358 #if 0 /* Produces wrong result if within sizeof. */
3359 /* Don't promote the operands separately if they promote
3360 the same way. Return the unpromoted type and let the combined
3361 value get promoted if necessary. */
3363 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3364 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3365 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3366 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3368 if (TREE_CODE (ifexp) == INTEGER_CST)
3369 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3371 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3375 /* Promote both alternatives. */
3377 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3378 op1 = default_conversion (op1);
3379 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3380 op2 = default_conversion (op2);
3382 if (TREE_CODE (ifexp) == ERROR_MARK
3383 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3384 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3385 return error_mark_node;
3387 type1 = TREE_TYPE (op1);
3388 code1 = TREE_CODE (type1);
3389 type2 = TREE_TYPE (op2);
3390 code2 = TREE_CODE (type2);
3392 /* Quickly detect the usual case where op1 and op2 have the same type
3394 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3397 result_type = type1;
3399 result_type = TYPE_MAIN_VARIANT (type1);
3401 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3402 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3404 result_type = common_type (type1, type2);
3406 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3408 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3409 pedwarn ("ANSI C forbids conditional expr with only one void side");
3410 result_type = void_type_node;
3412 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3414 if (comp_target_types (type1, type2))
3415 result_type = common_type (type1, type2);
3416 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3417 && TREE_CODE (orig_op1) != NOP_EXPR)
3418 result_type = qualify_type (type2, type1);
3419 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3420 && TREE_CODE (orig_op2) != NOP_EXPR)
3421 result_type = qualify_type (type1, type2);
3422 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3424 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3425 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3426 result_type = qualify_type (type1, type2);
3428 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3430 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3431 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3432 result_type = qualify_type (type2, type1);
3436 pedwarn ("pointer type mismatch in conditional expression");
3437 result_type = build_pointer_type (void_type_node);
3440 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3442 if (! integer_zerop (op2))
3443 pedwarn ("pointer/integer type mismatch in conditional expression");
3446 op2 = null_pointer_node;
3447 #if 0 /* The spec seems to say this is permitted. */
3448 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3449 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3452 result_type = type1;
3454 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3456 if (!integer_zerop (op1))
3457 pedwarn ("pointer/integer type mismatch in conditional expression");
3460 op1 = null_pointer_node;
3461 #if 0 /* The spec seems to say this is permitted. */
3462 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3463 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3466 result_type = type2;
3471 if (flag_cond_mismatch)
3472 result_type = void_type_node;
3475 error ("type mismatch in conditional expression");
3476 return error_mark_node;
3480 /* Merge const and volatile flags of the incoming types. */
3482 = build_type_variant (result_type,
3483 TREE_READONLY (op1) || TREE_READONLY (op2),
3484 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3486 if (result_type != TREE_TYPE (op1))
3487 op1 = convert_and_check (result_type, op1);
3488 if (result_type != TREE_TYPE (op2))
3489 op2 = convert_and_check (result_type, op2);
3492 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3494 result_type = TREE_TYPE (op1);
3495 if (TREE_CONSTANT (ifexp))
3496 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3498 if (TYPE_MODE (result_type) == BLKmode)
3500 register tree tempvar
3501 = build_decl (VAR_DECL, NULL_TREE, result_type);
3502 register tree xop1 = build_modify_expr (tempvar, op1);
3503 register tree xop2 = build_modify_expr (tempvar, op2);
3504 register tree result = fold (build (COND_EXPR, result_type,
3505 ifexp, xop1, xop2));
3507 layout_decl (tempvar, TYPE_ALIGN (result_type));
3508 /* No way to handle variable-sized objects here.
3509 I fear that the entire handling of BLKmode conditional exprs
3510 needs to be redone. */
3511 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3514 = assign_stack_local (DECL_MODE (tempvar),
3515 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3516 + BITS_PER_UNIT - 1)
3520 TREE_SIDE_EFFECTS (result)
3521 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3522 | TREE_SIDE_EFFECTS (op2);
3523 return build (COMPOUND_EXPR, result_type, result, tempvar);
3528 if (TREE_CODE (ifexp) == INTEGER_CST)
3529 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3531 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3534 /* Given a list of expressions, return a compound expression
3535 that performs them all and returns the value of the last of them. */
3538 build_compound_expr (list)
3541 return internal_build_compound_expr (list, TRUE);
3545 internal_build_compound_expr (list, first_p)
3551 if (TREE_CHAIN (list) == 0)
3553 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3554 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3556 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3557 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3558 list = TREE_OPERAND (list, 0);
3561 /* Don't let (0, 0) be null pointer constant. */
3562 if (!first_p && integer_zerop (TREE_VALUE (list)))
3563 return non_lvalue (TREE_VALUE (list));
3564 return TREE_VALUE (list);
3567 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3569 /* Convert arrays to pointers when there really is a comma operator. */
3570 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3571 TREE_VALUE (TREE_CHAIN (list))
3572 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3575 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3577 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3579 /* The left-hand operand of a comma expression is like an expression
3580 statement: with -W or -Wunused, we should warn if it doesn't have
3581 any side-effects, unless it was explicitly cast to (void). */
3582 if ((extra_warnings || warn_unused)
3583 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3584 && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
3585 warning ("left-hand operand of comma expression has no effect");
3587 /* When pedantic, a compound expression can be neither an lvalue
3588 nor an integer constant expression. */
3593 /* With -Wunused, we should also warn if the left-hand operand does have
3594 side-effects, but computes a value which is not used. For example, in
3595 `foo() + bar(), baz()' the result of the `+' operator is not used,
3596 so we should issue a warning. */
3597 else if (warn_unused)
3598 warn_if_unused_value (TREE_VALUE (list));
3600 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3603 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3606 build_c_cast (type, expr)
3610 register tree value = expr;
3612 if (type == error_mark_node || expr == error_mark_node)
3613 return error_mark_node;
3614 type = TYPE_MAIN_VARIANT (type);
3617 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3618 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3619 value = TREE_OPERAND (value, 0);
3622 if (TREE_CODE (type) == ARRAY_TYPE)
3624 error ("cast specifies array type");
3625 return error_mark_node;
3628 if (TREE_CODE (type) == FUNCTION_TYPE)
3630 error ("cast specifies function type");
3631 return error_mark_node;
3634 if (type == TREE_TYPE (value))
3638 if (TREE_CODE (type) == RECORD_TYPE
3639 || TREE_CODE (type) == UNION_TYPE)
3640 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3643 else if (TREE_CODE (type) == UNION_TYPE)
3646 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3647 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3648 value = default_conversion (value);
3650 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3651 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3652 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3661 pedwarn ("ANSI C forbids casts to union type");
3662 if (TYPE_NAME (type) != 0)
3664 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3665 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3667 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3671 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3672 build_tree_list (field, value)),
3674 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3677 error ("cast to union type from type not present in union");
3678 return error_mark_node;
3684 /* If casting to void, avoid the error that would come
3685 from default_conversion in the case of a non-lvalue array. */
3686 if (type == void_type_node)
3687 return build1 (CONVERT_EXPR, type, value);
3689 /* Convert functions and arrays to pointers,
3690 but don't convert any other types. */
3691 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3692 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3693 value = default_conversion (value);
3694 otype = TREE_TYPE (value);
3696 /* Optionally warn about potentially worrisome casts. */
3699 && TREE_CODE (type) == POINTER_TYPE
3700 && TREE_CODE (otype) == POINTER_TYPE)
3702 if (TYPE_VOLATILE (TREE_TYPE (otype))
3703 && ! TYPE_VOLATILE (TREE_TYPE (type)))
3704 pedwarn ("cast discards `volatile' from pointer target type");
3705 if (TYPE_READONLY (TREE_TYPE (otype))
3706 && ! TYPE_READONLY (TREE_TYPE (type)))
3707 pedwarn ("cast discards `const' from pointer target type");
3710 /* Warn about possible alignment problems. */
3711 if (STRICT_ALIGNMENT && warn_cast_align
3712 && TREE_CODE (type) == POINTER_TYPE
3713 && TREE_CODE (otype) == POINTER_TYPE
3714 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3715 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3716 /* Don't warn about opaque types, where the actual alignment
3717 restriction is unknown. */
3718 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3719 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3720 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3721 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3722 warning ("cast increases required alignment of target type");
3724 if (TREE_CODE (type) == INTEGER_TYPE
3725 && TREE_CODE (otype) == POINTER_TYPE
3726 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3727 && !TREE_CONSTANT (value))
3728 warning ("cast from pointer to integer of different size");
3730 if (warn_bad_function_cast
3731 && TREE_CODE (value) == CALL_EXPR
3732 && TREE_CODE (type) != TREE_CODE (otype))
3733 warning ("cast does not match function type");
3735 if (TREE_CODE (type) == POINTER_TYPE
3736 && TREE_CODE (otype) == INTEGER_TYPE
3737 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3739 /* Don't warn about converting 0 to pointer,
3740 provided the 0 was explicit--not cast or made by folding. */
3741 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3743 /* Don't warn about converting any constant. */
3744 && !TREE_CONSTANT (value))
3745 warning ("cast to pointer from integer of different size");
3748 value = convert (type, value);
3750 /* Ignore any integer overflow caused by the cast. */
3751 if (TREE_CODE (value) == INTEGER_CST)
3753 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3754 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3758 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3759 if (pedantic && TREE_CODE (value) == INTEGER_CST
3760 && TREE_CODE (expr) == INTEGER_CST
3761 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3762 value = non_lvalue (value);
3764 /* If pedantic, don't let a cast be an lvalue. */
3765 if (value == expr && pedantic)
3766 value = non_lvalue (value);
3771 /* Build an assignment expression of lvalue LHS from value RHS.
3772 MODIFYCODE is the code for a binary operator that we use
3773 to combine the old value of LHS with RHS to get the new value.
3774 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3777 build_modify_expr (lhs, modifycode, rhs)
3779 enum tree_code modifycode;
3781 register tree result;
3783 tree lhstype = TREE_TYPE (lhs);
3784 tree olhstype = lhstype;
3786 /* Types that aren't fully specified cannot be used in assignments. */
3787 lhs = require_complete_type (lhs);
3789 /* Avoid duplicate error messages from operands that had errors. */
3790 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3791 return error_mark_node;
3793 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3794 /* Do not use STRIP_NOPS here. We do not want an enumerator
3795 whose value is 0 to count as a null pointer constant. */
3796 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3797 rhs = TREE_OPERAND (rhs, 0);
3801 /* Handle control structure constructs used as "lvalues". */
3803 switch (TREE_CODE (lhs))
3805 /* Handle (a, b) used as an "lvalue". */
3807 pedantic_lvalue_warning (COMPOUND_EXPR);
3808 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3810 if (TREE_CODE (newrhs) == ERROR_MARK)
3811 return error_mark_node;
3812 return build (COMPOUND_EXPR, lhstype,
3813 TREE_OPERAND (lhs, 0), newrhs);
3815 /* Handle (a ? b : c) used as an "lvalue". */
3817 pedantic_lvalue_warning (COND_EXPR);
3818 rhs = save_expr (rhs);
3820 /* Produce (a ? (b = rhs) : (c = rhs))
3821 except that the RHS goes through a save-expr
3822 so the code to compute it is only emitted once. */
3824 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3825 build_modify_expr (TREE_OPERAND (lhs, 1),
3827 build_modify_expr (TREE_OPERAND (lhs, 2),
3829 if (TREE_CODE (cond) == ERROR_MARK)
3831 /* Make sure the code to compute the rhs comes out
3832 before the split. */
3833 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3834 /* But cast it to void to avoid an "unused" error. */
3835 convert (void_type_node, rhs), cond);
3839 /* If a binary op has been requested, combine the old LHS value with the RHS
3840 producing the value we should actually store into the LHS. */
3842 if (modifycode != NOP_EXPR)
3844 lhs = stabilize_reference (lhs);
3845 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3848 /* Handle a cast used as an "lvalue".
3849 We have already performed any binary operator using the value as cast.
3850 Now convert the result to the cast type of the lhs,
3851 and then true type of the lhs and store it there;
3852 then convert result back to the cast type to be the value
3853 of the assignment. */
3855 switch (TREE_CODE (lhs))
3860 case FIX_TRUNC_EXPR:
3861 case FIX_FLOOR_EXPR:
3862 case FIX_ROUND_EXPR:
3864 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3865 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3866 newrhs = default_conversion (newrhs);
3868 tree inner_lhs = TREE_OPERAND (lhs, 0);
3870 result = build_modify_expr (inner_lhs, NOP_EXPR,
3871 convert (TREE_TYPE (inner_lhs),
3872 convert (lhstype, newrhs)));
3873 if (TREE_CODE (result) == ERROR_MARK)
3875 pedantic_lvalue_warning (CONVERT_EXPR);
3876 return convert (TREE_TYPE (lhs), result);
3880 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3881 Reject anything strange now. */
3883 if (!lvalue_or_else (lhs, "assignment"))
3884 return error_mark_node;
3886 /* Warn about storing in something that is `const'. */
3888 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3889 || ((TREE_CODE (lhstype) == RECORD_TYPE
3890 || TREE_CODE (lhstype) == UNION_TYPE)
3891 && C_TYPE_FIELDS_READONLY (lhstype)))
3892 readonly_warning (lhs, "assignment");
3894 /* If storing into a structure or union member,
3895 it has probably been given type `int'.
3896 Compute the type that would go with
3897 the actual amount of storage the member occupies. */
3899 if (TREE_CODE (lhs) == COMPONENT_REF
3900 && (TREE_CODE (lhstype) == INTEGER_TYPE
3901 || TREE_CODE (lhstype) == REAL_TYPE
3902 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3903 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3905 /* If storing in a field that is in actuality a short or narrower than one,
3906 we must store in the field in its actual type. */
3908 if (lhstype != TREE_TYPE (lhs))
3910 lhs = copy_node (lhs);
3911 TREE_TYPE (lhs) = lhstype;
3914 /* Convert new value to destination type. */
3916 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
3917 NULL_TREE, NULL_TREE, 0);
3918 if (TREE_CODE (newrhs) == ERROR_MARK)
3919 return error_mark_node;
3921 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3922 TREE_SIDE_EFFECTS (result) = 1;
3924 /* If we got the LHS in a different type for storing in,
3925 convert the result back to the nominal type of LHS
3926 so that the value we return always has the same type
3927 as the LHS argument. */
3929 if (olhstype == TREE_TYPE (result))
3931 return convert_for_assignment (olhstype, result, "assignment",
3932 NULL_TREE, NULL_TREE, 0);
3935 /* Convert value RHS to type TYPE as preparation for an assignment
3936 to an lvalue of type TYPE.
3937 The real work of conversion is done by `convert'.
3938 The purpose of this function is to generate error messages
3939 for assignments that are not allowed in C.
3940 ERRTYPE is a string to use in error messages:
3941 "assignment", "return", etc. If it is null, this is parameter passing
3942 for a function call (and different error messages are output). Otherwise,
3943 it may be a name stored in the spelling stack and interpreted by
3946 FUNNAME is the name of the function being called,
3947 as an IDENTIFIER_NODE, or null.
3948 PARMNUM is the number of the argument, for printing in error messages. */
3951 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3954 tree fundecl, funname;
3957 register enum tree_code codel = TREE_CODE (type);
3958 register tree rhstype;
3959 register enum tree_code coder;
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 (rhs) == NON_LVALUE_EXPR)
3965 rhs = TREE_OPERAND (rhs, 0);
3967 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3968 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3969 rhs = default_conversion (rhs);
3970 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3971 rhs = decl_constant_value (rhs);
3973 rhstype = TREE_TYPE (rhs);
3974 coder = TREE_CODE (rhstype);
3976 if (coder == ERROR_MARK)
3977 return error_mark_node;
3979 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3981 overflow_warning (rhs);
3982 /* Check for Objective-C protocols. This will issue a warning if
3983 there are protocol violations. No need to use the return value. */
3984 maybe_objc_comptypes (type, rhstype, 0);
3988 if (coder == VOID_TYPE)
3990 error ("void value not ignored as it ought to be");
3991 return error_mark_node;
3993 /* Arithmetic types all interconvert, and enum is treated like int. */
3994 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
3995 || codel == COMPLEX_TYPE)
3996 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
3997 || coder == COMPLEX_TYPE))
3998 return convert_and_check (type, rhs);
4000 /* Conversion to a transparent union from its member types.
4001 This applies only to function arguments. */
4002 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4005 tree marginal_memb_type = 0;
4007 for (memb_types = TYPE_FIELDS (type); memb_types;
4008 memb_types = TREE_CHAIN (memb_types))
4010 tree memb_type = TREE_TYPE (memb_types);
4012 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4013 TYPE_MAIN_VARIANT (rhstype)))
4016 if (TREE_CODE (memb_type) != POINTER_TYPE)
4019 if (coder == POINTER_TYPE)
4021 register tree ttl = TREE_TYPE (memb_type);
4022 register tree ttr = TREE_TYPE (rhstype);
4024 /* Any non-function converts to a [const][volatile] void *
4025 and vice versa; otherwise, targets must be the same.
4026 Meanwhile, the lhs target must have all the qualifiers of
4028 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4029 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4030 || comp_target_types (memb_type, rhstype))
4032 /* If this type won't generate any warnings, use it. */
4033 if ((TREE_CODE (ttr) == FUNCTION_TYPE
4034 && TREE_CODE (ttl) == FUNCTION_TYPE)
4035 ? ((! TYPE_READONLY (ttl) | TYPE_READONLY (ttr))
4036 & (! TYPE_VOLATILE (ttl) | TYPE_VOLATILE (ttr)))
4037 : ((TYPE_READONLY (ttl) | ! TYPE_READONLY (ttr))
4038 & (TYPE_VOLATILE (ttl) | ! TYPE_VOLATILE (ttr))))
4041 /* Keep looking for a better type, but remember this one. */
4042 if (! marginal_memb_type)
4043 marginal_memb_type = memb_type;
4047 /* Can convert integer zero to any pointer type. */
4048 if (integer_zerop (rhs)
4049 || (TREE_CODE (rhs) == NOP_EXPR
4050 && integer_zerop (TREE_OPERAND (rhs, 0))))
4052 rhs = null_pointer_node;
4057 if (memb_types || marginal_memb_type)
4061 /* We have only a marginally acceptable member type;
4062 it needs a warning. */
4063 register tree ttl = TREE_TYPE (marginal_memb_type);
4064 register tree ttr = TREE_TYPE (rhstype);
4066 /* Const and volatile mean something different for function
4067 types, so the usual warnings are not appropriate. */
4068 if (TREE_CODE (ttr) == FUNCTION_TYPE
4069 && TREE_CODE (ttl) == FUNCTION_TYPE)
4071 /* Because const and volatile on functions are
4072 restrictions that say the function will not do
4073 certain things, it is okay to use a const or volatile
4074 function where an ordinary one is wanted, but not
4076 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4077 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4078 get_spelling (errtype), funname,
4080 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4081 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4082 get_spelling (errtype), funname,
4087 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4088 warn_for_assignment ("%s discards `const' from pointer target type",
4089 get_spelling (errtype), funname,
4091 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4092 warn_for_assignment ("%s discards `volatile' from pointer target type",
4093 get_spelling (errtype), funname,
4098 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4099 pedwarn ("ANSI C prohibits argument conversion to union type");
4101 return build1 (NOP_EXPR, type, rhs);
4105 /* Conversions among pointers */
4106 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
4108 register tree ttl = TREE_TYPE (type);
4109 register tree ttr = TREE_TYPE (rhstype);
4111 /* Any non-function converts to a [const][volatile] void *
4112 and vice versa; otherwise, targets must be the same.
4113 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4114 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4115 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4116 || comp_target_types (type, rhstype)
4117 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4118 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4121 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4122 && TREE_CODE (ttr) == FUNCTION_TYPE)
4124 (TYPE_MAIN_VARIANT (ttr) == void_type_node
4125 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4126 which are not ANSI null ptr constants. */
4127 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4128 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4129 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4130 get_spelling (errtype), funname, parmnum);
4131 /* Const and volatile mean something different for function types,
4132 so the usual warnings are not appropriate. */
4133 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4134 && TREE_CODE (ttl) != FUNCTION_TYPE)
4136 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4137 warn_for_assignment ("%s discards `const' from pointer target type",
4138 get_spelling (errtype), funname, parmnum);
4139 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4140 warn_for_assignment ("%s discards `volatile' from pointer target type",
4141 get_spelling (errtype), funname, parmnum);
4142 /* If this is not a case of ignoring a mismatch in signedness,
4144 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4145 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4146 || comp_target_types (type, rhstype))
4148 /* If there is a mismatch, do warn. */
4150 warn_for_assignment ("pointer targets in %s differ in signedness",
4151 get_spelling (errtype), funname, parmnum);
4153 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4154 && TREE_CODE (ttr) == FUNCTION_TYPE)
4156 /* Because const and volatile on functions are restrictions
4157 that say the function will not do certain things,
4158 it is okay to use a const or volatile function
4159 where an ordinary one is wanted, but not vice-versa. */
4160 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4161 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4162 get_spelling (errtype), funname, parmnum);
4163 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4164 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4165 get_spelling (errtype), funname, parmnum);
4169 warn_for_assignment ("%s from incompatible pointer type",
4170 get_spelling (errtype), funname, parmnum);
4171 return convert (type, rhs);
4173 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4175 /* An explicit constant 0 can convert to a pointer,
4176 or one that results from arithmetic, even including
4177 a cast to integer type. */
4178 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4180 ! (TREE_CODE (rhs) == NOP_EXPR
4181 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4182 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4183 && integer_zerop (TREE_OPERAND (rhs, 0))))
4185 warn_for_assignment ("%s makes pointer from integer without a cast",
4186 get_spelling (errtype), funname, parmnum);
4187 return convert (type, rhs);
4189 return null_pointer_node;
4191 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4193 warn_for_assignment ("%s makes integer from pointer without a cast",
4194 get_spelling (errtype), funname, parmnum);
4195 return convert (type, rhs);
4202 tree selector = maybe_building_objc_message_expr ();
4204 if (selector && parmnum > 2)
4205 error ("incompatible type for argument %d of `%s'",
4206 parmnum - 2, IDENTIFIER_POINTER (selector));
4208 error ("incompatible type for argument %d of `%s'",
4209 parmnum, IDENTIFIER_POINTER (funname));
4212 error ("incompatible type for argument %d of indirect function call",
4216 error ("incompatible types in %s", get_spelling (errtype));
4218 return error_mark_node;
4221 /* Print a warning using MSG.
4222 It gets OPNAME as its one parameter.
4223 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4224 FUNCTION and ARGNUM are handled specially if we are building an
4225 Objective-C selector. */
4228 warn_for_assignment (msg, opname, function, argnum)
4234 static char argstring[] = "passing arg %d of `%s'";
4235 static char argnofun[] = "passing arg %d";
4239 tree selector = maybe_building_objc_message_expr ();
4241 if (selector && argnum > 2)
4243 function = selector;
4248 /* Function name is known; supply it. */
4249 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4250 + sizeof (argstring) + 25 /*%d*/ + 1);
4251 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4255 /* Function name unknown (call through ptr); just give arg number. */
4256 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4257 sprintf (opname, argnofun, argnum);
4260 pedwarn (msg, opname);
4263 /* Return nonzero if VALUE is a valid constant-valued expression
4264 for use in initializing a static variable; one that can be an
4265 element of a "constant" initializer.
4267 Return null_pointer_node if the value is absolute;
4268 if it is relocatable, return the variable that determines the relocation.
4269 We assume that VALUE has been folded as much as possible;
4270 therefore, we do not need to check for such things as
4271 arithmetic-combinations of integers. */
4274 initializer_constant_valid_p (value, endtype)
4278 switch (TREE_CODE (value))
4281 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4282 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
4283 && TREE_CONSTANT (value)
4284 && CONSTRUCTOR_ELTS (value))
4286 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
4289 return TREE_STATIC (value) ? null_pointer_node : 0;
4295 return null_pointer_node;
4298 return TREE_OPERAND (value, 0);
4300 case NON_LVALUE_EXPR:
4301 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4305 /* Allow conversions between pointer types. */
4306 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4307 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
4308 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4310 /* Allow conversions between real types. */
4311 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4312 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
4313 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4315 /* Allow length-preserving conversions between integer types. */
4316 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4317 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4318 && (TYPE_PRECISION (TREE_TYPE (value))
4319 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4320 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4322 /* Allow conversions between other integer types only if
4324 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4325 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4327 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4329 if (inner == null_pointer_node)
4330 return null_pointer_node;
4334 /* Allow (int) &foo provided int is as wide as a pointer. */
4335 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4336 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
4337 && (TYPE_PRECISION (TREE_TYPE (value))
4338 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4339 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4342 /* Likewise conversions from int to pointers. */
4343 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4344 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4345 && (TYPE_PRECISION (TREE_TYPE (value))
4346 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4347 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4350 /* Allow conversions to union types if the value inside is okay. */
4351 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4352 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4357 if (TREE_CODE (endtype) == INTEGER_TYPE
4358 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4361 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4363 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4365 /* If either term is absolute, use the other terms relocation. */
4366 if (valid0 == null_pointer_node)
4368 if (valid1 == null_pointer_node)
4374 if (TREE_CODE (endtype) == INTEGER_TYPE
4375 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4378 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4380 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4382 /* Win if second argument is absolute. */
4383 if (valid1 == null_pointer_node)
4385 /* Win if both arguments have the same relocation.
4386 Then the value is absolute. */
4387 if (valid0 == valid1)
4388 return null_pointer_node;
4396 /* If VALUE is a compound expr all of whose expressions are constant, then
4397 return its value. Otherwise, return error_mark_node.
4399 This is for handling COMPOUND_EXPRs as initializer elements
4400 which is allowed with a warning when -pedantic is specified. */
4403 valid_compound_expr_initializer (value, endtype)
4407 if (TREE_CODE (value) == COMPOUND_EXPR)
4409 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4411 return error_mark_node;
4412 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4415 else if (! TREE_CONSTANT (value)
4416 && ! initializer_constant_valid_p (value, endtype))
4417 return error_mark_node;
4422 /* Perform appropriate conversions on the initial value of a variable,
4423 store it in the declaration DECL,
4424 and print any error messages that are appropriate.
4425 If the init is invalid, store an ERROR_MARK. */
4428 store_init_value (decl, init)
4431 register tree value, type;
4433 /* If variable's type was invalidly declared, just ignore it. */
4435 type = TREE_TYPE (decl);
4436 if (TREE_CODE (type) == ERROR_MARK)
4439 /* Digest the specified initializer into an expression. */
4441 value = digest_init (type, init, TREE_STATIC (decl),
4442 TREE_STATIC (decl) || pedantic);
4444 /* Store the expression if valid; else report error. */
4447 /* Note that this is the only place we can detect the error
4448 in a case such as struct foo bar = (struct foo) { x, y };
4449 where there is one initial value which is a constructor expression. */
4450 if (value == error_mark_node)
4452 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4454 error ("initializer for static variable is not constant");
4455 value = error_mark_node;
4457 else if (TREE_STATIC (decl)
4458 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4460 error ("initializer for static variable uses complicated arithmetic");
4461 value = error_mark_node;
4465 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4467 if (! TREE_CONSTANT (value))
4468 pedwarn ("aggregate initializer is not constant");
4469 else if (! TREE_STATIC (value))
4470 pedwarn ("aggregate initializer uses complicated arithmetic");
4475 DECL_INITIAL (decl) = value;
4477 /* ANSI wants warnings about out-of-range constant initializers. */
4478 STRIP_TYPE_NOPS (value);
4479 constant_expression_warning (value);
4482 /* Methods for storing and printing names for error messages. */
4484 /* Implement a spelling stack that allows components of a name to be pushed
4485 and popped. Each element on the stack is this structure. */
4497 #define SPELLING_STRING 1
4498 #define SPELLING_MEMBER 2
4499 #define SPELLING_BOUNDS 3
4501 static struct spelling *spelling; /* Next stack element (unused). */
4502 static struct spelling *spelling_base; /* Spelling stack base. */
4503 static int spelling_size; /* Size of the spelling stack. */
4505 /* Macros to save and restore the spelling stack around push_... functions.
4506 Alternative to SAVE_SPELLING_STACK. */
4508 #define SPELLING_DEPTH() (spelling - spelling_base)
4509 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4511 /* Save and restore the spelling stack around arbitrary C code. */
4513 #define SAVE_SPELLING_DEPTH(code) \
4515 int __depth = SPELLING_DEPTH (); \
4517 RESTORE_SPELLING_DEPTH (__depth); \
4520 /* Push an element on the spelling stack with type KIND and assign VALUE
4523 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4525 int depth = SPELLING_DEPTH (); \
4527 if (depth >= spelling_size) \
4529 spelling_size += 10; \
4530 if (spelling_base == 0) \
4532 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4535 = (struct spelling *) xrealloc (spelling_base, \
4536 spelling_size * sizeof (struct spelling)); \
4537 RESTORE_SPELLING_DEPTH (depth); \
4540 spelling->kind = (KIND); \
4541 spelling->MEMBER = (VALUE); \
4545 /* Push STRING on the stack. Printed literally. */
4548 push_string (string)
4551 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4554 /* Push a member name on the stack. Printed as '.' STRING. */
4557 push_member_name (decl)
4562 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4563 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4566 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4569 push_array_bounds (bounds)
4572 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4575 /* Compute the maximum size in bytes of the printed spelling. */
4580 register int size = 0;
4581 register struct spelling *p;
4583 for (p = spelling_base; p < spelling; p++)
4585 if (p->kind == SPELLING_BOUNDS)
4588 size += strlen (p->u.s) + 1;
4594 /* Print the spelling to BUFFER and return it. */
4597 print_spelling (buffer)
4598 register char *buffer;
4600 register char *d = buffer;
4602 register struct spelling *p;
4604 for (p = spelling_base; p < spelling; p++)
4605 if (p->kind == SPELLING_BOUNDS)
4607 sprintf (d, "[%d]", p->u.i);
4612 if (p->kind == SPELLING_MEMBER)
4614 for (s = p->u.s; *d = *s++; d++)
4621 /* Provide a means to pass component names derived from the spelling stack. */
4623 char initialization_message;
4625 /* Interpret the spelling of the given ERRTYPE message. */
4628 get_spelling (errtype)
4631 static char *buffer;
4632 static int size = -1;
4634 if (errtype == &initialization_message)
4636 /* Avoid counting chars */
4637 static char message[] = "initialization of `%s'";
4638 register int needed = sizeof (message) + spelling_length () + 1;
4642 buffer = (char *) xmalloc (size = needed);
4644 buffer = (char *) xrealloc (buffer, size = needed);
4646 temp = (char *) alloca (needed);
4647 sprintf (buffer, message, print_spelling (temp));
4654 /* Issue an error message for a bad initializer component.
4655 FORMAT describes the message. OFWHAT is the name for the component.
4656 LOCAL is a format string for formatting the insertion of the name
4659 If OFWHAT is null, the component name is stored on the spelling stack.
4660 If the component name is a null string, then LOCAL is omitted entirely. */
4663 error_init (format, local, ofwhat)
4664 char *format, *local, *ofwhat;
4669 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4670 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4673 sprintf (buffer, local, ofwhat);
4677 error (format, buffer);
4680 /* Issue a pedantic warning for a bad initializer component.
4681 FORMAT describes the message. OFWHAT is the name for the component.
4682 LOCAL is a format string for formatting the insertion of the name
4685 If OFWHAT is null, the component name is stored on the spelling stack.
4686 If the component name is a null string, then LOCAL is omitted entirely. */
4689 pedwarn_init (format, local, ofwhat)
4690 char *format, *local, *ofwhat;
4695 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4696 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4699 sprintf (buffer, local, ofwhat);
4703 pedwarn (format, buffer);
4706 /* Issue a warning for a bad initializer component.
4707 FORMAT describes the message. OFWHAT is the name for the component.
4708 LOCAL is a format string for formatting the insertion of the name
4711 If OFWHAT is null, the component name is stored on the spelling stack.
4712 If the component name is a null string, then LOCAL is omitted entirely. */
4715 warning_init (format, local, ofwhat)
4716 char *format, *local, *ofwhat;
4721 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4722 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4725 sprintf (buffer, local, ofwhat);
4729 warning (format, buffer);
4732 /* Digest the parser output INIT as an initializer for type TYPE.
4733 Return a C expression of type TYPE to represent the initial value.
4735 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4736 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4737 applies only to elements of constructors. */
4740 digest_init (type, init, require_constant, constructor_constant)
4742 int require_constant, constructor_constant;
4744 enum tree_code code = TREE_CODE (type);
4745 tree inside_init = init;
4747 if (init == error_mark_node)
4750 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4751 /* Do not use STRIP_NOPS here. We do not want an enumerator
4752 whose value is 0 to count as a null pointer constant. */
4753 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4754 inside_init = TREE_OPERAND (init, 0);
4756 /* Initialization of an array of chars from a string constant
4757 optionally enclosed in braces. */
4759 if (code == ARRAY_TYPE)
4761 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4762 if ((typ1 == char_type_node
4763 || typ1 == signed_char_type_node
4764 || typ1 == unsigned_char_type_node
4765 || typ1 == unsigned_wchar_type_node
4766 || typ1 == signed_wchar_type_node)
4767 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4769 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4770 TYPE_MAIN_VARIANT (type)))
4773 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4775 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4777 error_init ("char-array%s initialized from wide string",
4779 return error_mark_node;
4781 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4783 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4785 error_init ("int-array%s initialized from non-wide string",
4787 return error_mark_node;
4790 TREE_TYPE (inside_init) = type;
4791 if (TYPE_DOMAIN (type) != 0
4792 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4794 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4795 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4796 /* Subtract 1 (or sizeof (wchar_t))
4797 because it's ok to ignore the terminating null char
4798 that is counted in the length of the constant. */
4799 if (size < TREE_STRING_LENGTH (inside_init)
4800 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4801 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4804 "initializer-string for array of chars%s is too long",
4811 /* Any type can be initialized
4812 from an expression of the same type, optionally with braces. */
4814 if (inside_init && TREE_TYPE (inside_init) != 0
4815 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4816 TYPE_MAIN_VARIANT (type))
4817 || (code == ARRAY_TYPE
4818 && comptypes (TREE_TYPE (inside_init), type))
4819 || (code == POINTER_TYPE
4820 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4821 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4822 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4823 TREE_TYPE (type)))))
4825 if (code == POINTER_TYPE
4826 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4827 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4828 inside_init = default_conversion (inside_init);
4829 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4830 && TREE_CODE (inside_init) != CONSTRUCTOR)
4832 error_init ("array%s initialized from non-constant array expression",
4834 return error_mark_node;
4837 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4838 inside_init = decl_constant_value (inside_init);
4840 /* Compound expressions can only occur here if -pedantic or
4841 -pedantic-errors is specified. In the later case, we always want
4842 an error. In the former case, we simply want a warning. */
4843 if (require_constant && pedantic
4844 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4847 = valid_compound_expr_initializer (inside_init,
4848 TREE_TYPE (inside_init));
4849 if (inside_init == error_mark_node)
4850 error_init ("initializer element%s is not constant",
4853 pedwarn_init ("initializer element%s is not constant",
4855 if (flag_pedantic_errors)
4856 inside_init = error_mark_node;
4858 else if (require_constant && ! TREE_CONSTANT (inside_init))
4860 error_init ("initializer element%s is not constant",
4862 inside_init = error_mark_node;
4864 else if (require_constant
4865 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4867 error_init ("initializer element%s is not computable at load time",
4869 inside_init = error_mark_node;
4875 /* Handle scalar types, including conversions. */
4877 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4878 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4880 /* Note that convert_for_assignment calls default_conversion
4881 for arrays and functions. We must not call it in the
4882 case where inside_init is a null pointer constant. */
4884 = convert_for_assignment (type, init, "initialization",
4885 NULL_TREE, NULL_TREE, 0);
4887 if (require_constant && ! TREE_CONSTANT (inside_init))
4889 error_init ("initializer element%s is not constant",
4891 inside_init = error_mark_node;
4893 else if (require_constant
4894 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4896 error_init ("initializer element%s is not computable at load time",
4898 inside_init = error_mark_node;
4904 /* Come here only for records and arrays. */
4906 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4908 error_init ("variable-sized object%s may not be initialized",
4910 return error_mark_node;
4913 /* Traditionally, you can write struct foo x = 0;
4914 and it initializes the first element of x to 0. */
4915 if (flag_traditional)
4917 tree top = 0, prev = 0, otype = type;
4918 while (TREE_CODE (type) == RECORD_TYPE
4919 || TREE_CODE (type) == ARRAY_TYPE
4920 || TREE_CODE (type) == QUAL_UNION_TYPE
4921 || TREE_CODE (type) == UNION_TYPE)
4923 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4927 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4929 if (TREE_CODE (type) == ARRAY_TYPE)
4930 type = TREE_TYPE (type);
4931 else if (TYPE_FIELDS (type))
4932 type = TREE_TYPE (TYPE_FIELDS (type));
4935 error_init ("invalid initializer%s", " for `%s'", NULL);
4936 return error_mark_node;
4942 TREE_OPERAND (prev, 1)
4943 = build_tree_list (NULL_TREE,
4944 digest_init (type, init, require_constant,
4945 constructor_constant));
4949 return error_mark_node;
4951 error_init ("invalid initializer%s", " for `%s'", NULL);
4952 return error_mark_node;
4955 /* Handle initializers that use braces. */
4957 /* Type of object we are accumulating a constructor for.
4958 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4959 static tree constructor_type;
4961 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4963 static tree constructor_fields;
4965 /* For an ARRAY_TYPE, this is the specified index
4966 at which to store the next element we get.
4967 This is a special INTEGER_CST node that we modify in place. */
4968 static tree constructor_index;
4970 /* For an ARRAY_TYPE, this is the end index of the range
4971 to initialize with the next element, or NULL in the ordinary case
4972 where the element is used just once. */
4973 static tree constructor_range_end;
4975 /* For an ARRAY_TYPE, this is the maximum index. */
4976 static tree constructor_max_index;
4978 /* For a RECORD_TYPE, this is the first field not yet written out. */
4979 static tree constructor_unfilled_fields;
4981 /* For an ARRAY_TYPE, this is the index of the first element
4982 not yet written out.
4983 This is a special INTEGER_CST node that we modify in place. */
4984 static tree constructor_unfilled_index;
4986 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4987 This is so we can generate gaps between fields, when appropriate.
4988 This is a special INTEGER_CST node that we modify in place. */
4989 static tree constructor_bit_index;
4991 /* If we are saving up the elements rather than allocating them,
4992 this is the list of elements so far (in reverse order,
4993 most recent first). */
4994 static tree constructor_elements;
4996 /* 1 if so far this constructor's elements are all compile-time constants. */
4997 static int constructor_constant;
4999 /* 1 if so far this constructor's elements are all valid address constants. */
5000 static int constructor_simple;
5002 /* 1 if this constructor is erroneous so far. */
5003 static int constructor_erroneous;
5005 /* 1 if have called defer_addressed_constants. */
5006 static int constructor_subconstants_deferred;
5008 /* List of pending elements at this constructor level.
5009 These are elements encountered out of order
5010 which belong at places we haven't reached yet in actually
5011 writing the output. */
5012 static tree constructor_pending_elts;
5014 /* The SPELLING_DEPTH of this constructor. */
5015 static int constructor_depth;
5017 /* 0 if implicitly pushing constructor levels is allowed. */
5018 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
5020 /* 1 if this constructor level was entered implicitly. */
5021 static int constructor_implicit;
5023 static int require_constant_value;
5024 static int require_constant_elements;
5026 /* 1 if it is ok to output this constructor as we read it.
5027 0 means must accumulate a CONSTRUCTOR expression. */
5028 static int constructor_incremental;
5030 /* DECL node for which an initializer is being read.
5031 0 means we are reading a constructor expression
5032 such as (struct foo) {...}. */
5033 static tree constructor_decl;
5035 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
5036 static char *constructor_asmspec;
5038 /* Nonzero if this is an initializer for a top-level decl. */
5039 static int constructor_top_level;
5041 /* When we finish reading a constructor expression
5042 (constructor_decl is 0), the CONSTRUCTOR goes here. */
5043 static tree constructor_result;
5045 /* This stack has a level for each implicit or explicit level of
5046 structuring in the initializer, including the outermost one. It
5047 saves the values of most of the variables above. */
5049 struct constructor_stack
5051 struct constructor_stack *next;
5057 tree unfilled_index;
5058 tree unfilled_fields;
5064 /* If nonzero, this value should replace the entire
5065 constructor at this level. */
5066 tree replacement_value;
5075 struct constructor_stack *constructor_stack;
5077 /* This stack records separate initializers that are nested.
5078 Nested initializers can't happen in ANSI C, but GNU C allows them
5079 in cases like { ... (struct foo) { ... } ... }. */
5081 struct initializer_stack
5083 struct initializer_stack *next;
5086 struct constructor_stack *constructor_stack;
5088 struct spelling *spelling;
5089 struct spelling *spelling_base;
5093 char require_constant_value;
5094 char require_constant_elements;
5098 struct initializer_stack *initializer_stack;
5100 /* Prepare to parse and output the initializer for variable DECL. */
5103 start_init (decl, asmspec_tree, top_level)
5109 struct initializer_stack *p
5110 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5114 asmspec = TREE_STRING_POINTER (asmspec_tree);
5116 p->decl = constructor_decl;
5117 p->asmspec = constructor_asmspec;
5118 p->incremental = constructor_incremental;
5119 p->require_constant_value = require_constant_value;
5120 p->require_constant_elements = require_constant_elements;
5121 p->constructor_stack = constructor_stack;
5122 p->elements = constructor_elements;
5123 p->spelling = spelling;
5124 p->spelling_base = spelling_base;
5125 p->spelling_size = spelling_size;
5126 p->deferred = constructor_subconstants_deferred;
5127 p->top_level = constructor_top_level;
5128 p->next = initializer_stack;
5129 initializer_stack = p;
5131 constructor_decl = decl;
5132 constructor_incremental = top_level;
5133 constructor_asmspec = asmspec;
5134 constructor_subconstants_deferred = 0;
5135 constructor_top_level = top_level;
5139 require_constant_value = TREE_STATIC (decl);
5140 require_constant_elements
5141 = ((TREE_STATIC (decl) || pedantic)
5142 /* For a scalar, you can always use any value to initialize,
5143 even within braces. */
5144 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5145 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5146 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5147 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5148 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5149 constructor_incremental |= TREE_STATIC (decl);
5153 require_constant_value = 0;
5154 require_constant_elements = 0;
5155 locus = "(anonymous)";
5158 constructor_stack = 0;
5160 missing_braces_mentioned = 0;
5164 RESTORE_SPELLING_DEPTH (0);
5167 push_string (locus);
5173 struct initializer_stack *p = initializer_stack;
5175 /* Output subconstants (string constants, usually)
5176 that were referenced within this initializer and saved up.
5177 Must do this if and only if we called defer_addressed_constants. */
5178 if (constructor_subconstants_deferred)
5179 output_deferred_addressed_constants ();
5181 /* Free the whole constructor stack of this initializer. */
5182 while (constructor_stack)
5184 struct constructor_stack *q = constructor_stack;
5185 constructor_stack = q->next;
5189 /* Pop back to the data of the outer initializer (if any). */
5190 constructor_decl = p->decl;
5191 constructor_asmspec = p->asmspec;
5192 constructor_incremental = p->incremental;
5193 require_constant_value = p->require_constant_value;
5194 require_constant_elements = p->require_constant_elements;
5195 constructor_stack = p->constructor_stack;
5196 constructor_elements = p->elements;
5197 spelling = p->spelling;
5198 spelling_base = p->spelling_base;
5199 spelling_size = p->spelling_size;
5200 constructor_subconstants_deferred = p->deferred;
5201 constructor_top_level = p->top_level;
5202 initializer_stack = p->next;
5206 /* Call here when we see the initializer is surrounded by braces.
5207 This is instead of a call to push_init_level;
5208 it is matched by a call to pop_init_level.
5210 TYPE is the type to initialize, for a constructor expression.
5211 For an initializer for a decl, TYPE is zero. */
5214 really_start_incremental_init (type)
5217 struct constructor_stack *p
5218 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5221 type = TREE_TYPE (constructor_decl);
5223 /* Turn off constructor_incremental if type is a struct with bitfields.
5224 Do this before the first push, so that the corrected value
5225 is available in finish_init. */
5226 check_init_type_bitfields (type);
5228 p->type = constructor_type;
5229 p->fields = constructor_fields;
5230 p->index = constructor_index;
5231 p->range_end = constructor_range_end;
5232 p->max_index = constructor_max_index;
5233 p->unfilled_index = constructor_unfilled_index;
5234 p->unfilled_fields = constructor_unfilled_fields;
5235 p->bit_index = constructor_bit_index;
5236 p->elements = constructor_elements;
5237 p->constant = constructor_constant;
5238 p->simple = constructor_simple;
5239 p->erroneous = constructor_erroneous;
5240 p->pending_elts = constructor_pending_elts;
5241 p->depth = constructor_depth;
5242 p->replacement_value = 0;
5244 p->incremental = constructor_incremental;
5247 constructor_stack = p;
5249 constructor_constant = 1;
5250 constructor_simple = 1;
5251 constructor_depth = SPELLING_DEPTH ();
5252 constructor_elements = 0;
5253 constructor_pending_elts = 0;
5254 constructor_type = type;
5256 if (TREE_CODE (constructor_type) == RECORD_TYPE
5257 || TREE_CODE (constructor_type) == UNION_TYPE)
5259 constructor_fields = TYPE_FIELDS (constructor_type);
5260 /* Skip any nameless bit fields at the beginning. */
5261 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5262 && DECL_NAME (constructor_fields) == 0)
5263 constructor_fields = TREE_CHAIN (constructor_fields);
5264 constructor_unfilled_fields = constructor_fields;
5265 constructor_bit_index = copy_node (integer_zero_node);
5267 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5269 constructor_range_end = 0;
5270 if (TYPE_DOMAIN (constructor_type))
5272 constructor_max_index
5273 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5275 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5278 constructor_index = copy_node (integer_zero_node);
5279 constructor_unfilled_index = copy_node (constructor_index);
5283 /* Handle the case of int x = {5}; */
5284 constructor_fields = constructor_type;
5285 constructor_unfilled_fields = constructor_type;
5288 if (constructor_incremental)
5290 int momentary = suspend_momentary ();
5291 push_obstacks_nochange ();
5292 if (TREE_PERMANENT (constructor_decl))
5293 end_temporary_allocation ();
5294 make_decl_rtl (constructor_decl, constructor_asmspec,
5295 constructor_top_level);
5296 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5298 resume_momentary (momentary);
5301 if (constructor_incremental)
5303 defer_addressed_constants ();
5304 constructor_subconstants_deferred = 1;
5308 /* Push down into a subobject, for initialization.
5309 If this is for an explicit set of braces, IMPLICIT is 0.
5310 If it is because the next element belongs at a lower level,
5314 push_init_level (implicit)
5317 struct constructor_stack *p;
5319 /* If we've exhausted any levels that didn't have braces,
5321 while (constructor_stack->implicit)
5323 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5324 || TREE_CODE (constructor_type) == UNION_TYPE)
5325 && constructor_fields == 0)
5326 process_init_element (pop_init_level (1));
5327 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5328 && tree_int_cst_lt (constructor_max_index, constructor_index))
5329 process_init_element (pop_init_level (1));
5334 /* Structure elements may require alignment. Do this now if necessary
5335 for the subaggregate, and if it comes next in sequence. Don't do
5336 this for subaggregates that will go on the pending list. */
5337 if (constructor_incremental && constructor_type != 0
5338 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields
5339 && constructor_fields == constructor_unfilled_fields)
5341 /* Advance to offset of this element. */
5342 if (! tree_int_cst_equal (constructor_bit_index,
5343 DECL_FIELD_BITPOS (constructor_fields)))
5345 int next = (TREE_INT_CST_LOW
5346 (DECL_FIELD_BITPOS (constructor_fields))
5348 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5351 assemble_zeros (next - here);
5353 /* Indicate that we have now filled the structure up to the current
5355 constructor_unfilled_fields = constructor_fields;
5358 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5359 p->type = constructor_type;
5360 p->fields = constructor_fields;
5361 p->index = constructor_index;
5362 p->range_end = constructor_range_end;
5363 p->max_index = constructor_max_index;
5364 p->unfilled_index = constructor_unfilled_index;
5365 p->unfilled_fields = constructor_unfilled_fields;
5366 p->bit_index = constructor_bit_index;
5367 p->elements = constructor_elements;
5368 p->constant = constructor_constant;
5369 p->simple = constructor_simple;
5370 p->erroneous = constructor_erroneous;
5371 p->pending_elts = constructor_pending_elts;
5372 p->depth = constructor_depth;
5373 p->replacement_value = 0;
5374 p->implicit = implicit;
5375 p->incremental = constructor_incremental;
5377 p->next = constructor_stack;
5378 constructor_stack = p;
5380 constructor_constant = 1;
5381 constructor_simple = 1;
5382 constructor_depth = SPELLING_DEPTH ();
5383 constructor_elements = 0;
5384 constructor_pending_elts = 0;
5386 /* Don't die if an entire brace-pair level is superfluous
5387 in the containing level. */
5388 if (constructor_type == 0)
5390 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5391 || TREE_CODE (constructor_type) == UNION_TYPE)
5393 /* Don't die if there are extra init elts at the end. */
5394 if (constructor_fields == 0)
5395 constructor_type = 0;
5398 constructor_type = TREE_TYPE (constructor_fields);
5399 push_member_name (constructor_fields);
5400 constructor_depth++;
5401 if (constructor_fields != constructor_unfilled_fields)
5402 constructor_incremental = 0;
5405 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5407 constructor_type = TREE_TYPE (constructor_type);
5408 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5409 constructor_depth++;
5410 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5411 || constructor_range_end != 0)
5412 constructor_incremental = 0;
5415 if (constructor_type == 0)
5417 error_init ("extra brace group at end of initializer%s",
5419 constructor_fields = 0;
5420 constructor_unfilled_fields = 0;
5424 /* Turn off constructor_incremental if type is a struct with bitfields. */
5425 check_init_type_bitfields (constructor_type);
5427 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5429 missing_braces_mentioned = 1;
5430 warning_init ("missing braces around initializer%s", " for `%s'", NULL);
5433 if (TREE_CODE (constructor_type) == RECORD_TYPE
5434 || TREE_CODE (constructor_type) == UNION_TYPE)
5436 constructor_fields = TYPE_FIELDS (constructor_type);
5437 /* Skip any nameless bit fields at the beginning. */
5438 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5439 && DECL_NAME (constructor_fields) == 0)
5440 constructor_fields = TREE_CHAIN (constructor_fields);
5441 constructor_unfilled_fields = constructor_fields;
5442 constructor_bit_index = copy_node (integer_zero_node);
5444 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5446 constructor_range_end = 0;
5447 if (TYPE_DOMAIN (constructor_type))
5449 constructor_max_index
5450 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5452 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5455 constructor_index = copy_node (integer_zero_node);
5456 constructor_unfilled_index = copy_node (constructor_index);
5460 warning_init ("braces around scalar initializer%s", " for `%s'", NULL);
5461 constructor_fields = constructor_type;
5462 constructor_unfilled_fields = constructor_type;
5466 /* Don't read a struct incrementally if it has any bitfields,
5467 because the incremental reading code doesn't know how to
5468 handle bitfields yet. */
5471 check_init_type_bitfields (type)
5474 if (TREE_CODE (type) == RECORD_TYPE)
5477 for (tail = TYPE_FIELDS (type); tail;
5478 tail = TREE_CHAIN (tail))
5480 if (DECL_C_BIT_FIELD (tail)
5481 /* This catches cases like `int foo : 8;'. */
5482 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail)))
5484 constructor_incremental = 0;
5488 check_init_type_bitfields (TREE_TYPE (tail));
5492 else if (TREE_CODE (type) == ARRAY_TYPE)
5493 check_init_type_bitfields (TREE_TYPE (type));
5496 /* At the end of an implicit or explicit brace level,
5497 finish up that level of constructor.
5498 If we were outputting the elements as they are read, return 0
5499 from inner levels (process_init_element ignores that),
5500 but return error_mark_node from the outermost level
5501 (that's what we want to put in DECL_INITIAL).
5502 Otherwise, return a CONSTRUCTOR expression. */
5505 pop_init_level (implicit)
5508 struct constructor_stack *p;
5510 tree constructor = 0;
5514 /* When we come to an explicit close brace,
5515 pop any inner levels that didn't have explicit braces. */
5516 while (constructor_stack->implicit)
5517 process_init_element (pop_init_level (1));
5520 p = constructor_stack;
5522 if (constructor_type != 0)
5523 size = int_size_in_bytes (constructor_type);
5525 /* Now output all pending elements. */
5526 output_pending_init_elements (1);
5528 #if 0 /* c-parse.in warns about {}. */
5529 /* In ANSI, each brace level must have at least one element. */
5530 if (! implicit && pedantic
5531 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5532 ? integer_zerop (constructor_unfilled_index)
5533 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5534 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL);
5537 /* Pad out the end of the structure. */
5539 if (p->replacement_value)
5541 /* If this closes a superfluous brace pair,
5542 just pass out the element between them. */
5543 constructor = p->replacement_value;
5544 /* If this is the top level thing within the initializer,
5545 and it's for a variable, then since we already called
5546 assemble_variable, we must output the value now. */
5547 if (p->next == 0 && constructor_decl != 0
5548 && constructor_incremental)
5550 constructor = digest_init (constructor_type, constructor,
5551 require_constant_value,
5552 require_constant_elements);
5554 /* If initializing an array of unknown size,
5555 determine the size now. */
5556 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5557 && TYPE_DOMAIN (constructor_type) == 0)
5562 push_obstacks_nochange ();
5563 if (TREE_PERMANENT (constructor_type))
5564 end_temporary_allocation ();
5566 momentary_p = suspend_momentary ();
5568 /* We shouldn't have an incomplete array type within
5570 if (constructor_stack->next)
5574 = complete_array_type (constructor_type,
5579 size = int_size_in_bytes (constructor_type);
5580 resume_momentary (momentary_p);
5584 output_constant (constructor, size);
5587 else if (constructor_type == 0)
5589 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5590 && TREE_CODE (constructor_type) != UNION_TYPE
5591 && TREE_CODE (constructor_type) != ARRAY_TYPE
5592 && ! constructor_incremental)
5594 /* A nonincremental scalar initializer--just return
5595 the element, after verifying there is just one. */
5596 if (constructor_elements == 0)
5598 error_init ("empty scalar initializer%s",
5600 constructor = error_mark_node;
5602 else if (TREE_CHAIN (constructor_elements) != 0)
5604 error_init ("extra elements in scalar initializer%s",
5606 constructor = TREE_VALUE (constructor_elements);
5609 constructor = TREE_VALUE (constructor_elements);
5611 else if (! constructor_incremental)
5613 if (constructor_erroneous)
5614 constructor = error_mark_node;
5617 int momentary = suspend_momentary ();
5619 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5620 nreverse (constructor_elements));
5621 if (constructor_constant)
5622 TREE_CONSTANT (constructor) = 1;
5623 if (constructor_constant && constructor_simple)
5624 TREE_STATIC (constructor) = 1;
5626 resume_momentary (momentary);
5632 int momentary = suspend_momentary ();
5634 if (TREE_CODE (constructor_type) == RECORD_TYPE
5635 || TREE_CODE (constructor_type) == UNION_TYPE)
5637 /* Find the offset of the end of that field. */
5638 filled = size_binop (CEIL_DIV_EXPR,
5639 constructor_bit_index,
5640 size_int (BITS_PER_UNIT));
5642 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5644 /* If initializing an array of unknown size,
5645 determine the size now. */
5646 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5647 && TYPE_DOMAIN (constructor_type) == 0)
5650 = size_binop (MINUS_EXPR,
5651 constructor_unfilled_index,
5654 push_obstacks_nochange ();
5655 if (TREE_PERMANENT (constructor_type))
5656 end_temporary_allocation ();
5657 maxindex = copy_node (maxindex);
5658 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5659 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5661 /* TYPE_MAX_VALUE is always one less than the number of elements
5662 in the array, because we start counting at zero. Therefore,
5663 warn only if the value is less than zero. */
5665 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5667 error_with_decl (constructor_decl,
5668 "zero or negative array size `%s'");
5669 layout_type (constructor_type);
5670 size = int_size_in_bytes (constructor_type);
5674 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5675 size_in_bytes (TREE_TYPE (constructor_type)));
5681 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5683 resume_momentary (momentary);
5687 constructor_type = p->type;
5688 constructor_fields = p->fields;
5689 constructor_index = p->index;
5690 constructor_range_end = p->range_end;
5691 constructor_max_index = p->max_index;
5692 constructor_unfilled_index = p->unfilled_index;
5693 constructor_unfilled_fields = p->unfilled_fields;
5694 constructor_bit_index = p->bit_index;
5695 constructor_elements = p->elements;
5696 constructor_constant = p->constant;
5697 constructor_simple = p->simple;
5698 constructor_erroneous = p->erroneous;
5699 constructor_pending_elts = p->pending_elts;
5700 constructor_depth = p->depth;
5701 constructor_incremental = p->incremental;
5702 RESTORE_SPELLING_DEPTH (constructor_depth);
5704 constructor_stack = p->next;
5707 if (constructor == 0)
5709 if (constructor_stack == 0)
5710 return error_mark_node;
5716 /* Within an array initializer, specify the next index to be initialized.
5717 FIRST is that index. If LAST is nonzero, then initialize a range
5718 of indices, running from FIRST through LAST. */
5721 set_init_index (first, last)
5724 while ((TREE_CODE (first) == NOP_EXPR
5725 || TREE_CODE (first) == CONVERT_EXPR
5726 || TREE_CODE (first) == NON_LVALUE_EXPR)
5727 && (TYPE_MODE (TREE_TYPE (first))
5728 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5729 (first) = TREE_OPERAND (first, 0);
5731 while ((TREE_CODE (last) == NOP_EXPR
5732 || TREE_CODE (last) == CONVERT_EXPR
5733 || TREE_CODE (last) == NON_LVALUE_EXPR)
5734 && (TYPE_MODE (TREE_TYPE (last))
5735 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5736 (last) = TREE_OPERAND (last, 0);
5738 if (TREE_CODE (first) != INTEGER_CST)
5739 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5740 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5741 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5742 else if (! constructor_unfilled_index)
5743 error_init ("array index in non-array initializer%s", " for `%s'", NULL);
5744 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5745 error_init ("duplicate array index in initializer%s", " for `%s'", NULL);
5748 TREE_INT_CST_LOW (constructor_index)
5749 = TREE_INT_CST_LOW (first);
5750 TREE_INT_CST_HIGH (constructor_index)
5751 = TREE_INT_CST_HIGH (first);
5753 if (last != 0 && tree_int_cst_lt (last, first))
5754 error_init ("empty index range in initializer%s", " for `%s'", NULL);
5758 pedwarn ("ANSI C forbids specifying element to initialize");
5759 constructor_range_end = last;
5764 /* Within a struct initializer, specify the next field to be initialized. */
5767 set_init_label (fieldname)
5773 /* Don't die if an entire brace-pair level is superfluous
5774 in the containing level. */
5775 if (constructor_type == 0)
5778 for (tail = TYPE_FIELDS (constructor_type); tail;
5779 tail = TREE_CHAIN (tail))
5781 if (tail == constructor_unfilled_fields)
5783 if (DECL_NAME (tail) == fieldname)
5788 error ("unknown field `%s' specified in initializer",
5789 IDENTIFIER_POINTER (fieldname));
5791 error ("field `%s' already initialized",
5792 IDENTIFIER_POINTER (fieldname));
5795 constructor_fields = tail;
5797 pedwarn ("ANSI C forbids specifying structure member to initialize");
5801 /* "Output" the next constructor element.
5802 At top level, really output it to assembler code now.
5803 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5804 TYPE is the data type that the containing data type wants here.
5805 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5807 PENDING if non-nil means output pending elements that belong
5808 right after this element. (PENDING is normally 1;
5809 it is 0 while outputting pending elements, to avoid recursion.) */
5812 output_init_element (value, type, field, pending)
5813 tree value, type, field;
5818 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5819 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5820 && !(TREE_CODE (value) == STRING_CST
5821 && TREE_CODE (type) == ARRAY_TYPE
5822 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5823 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5824 TYPE_MAIN_VARIANT (type))))
5825 value = default_conversion (value);
5827 if (value == error_mark_node)
5828 constructor_erroneous = 1;
5829 else if (!TREE_CONSTANT (value))
5830 constructor_constant = 0;
5831 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5832 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5833 || TREE_CODE (constructor_type) == UNION_TYPE)
5834 && DECL_C_BIT_FIELD (field)
5835 && TREE_CODE (value) != INTEGER_CST))
5836 constructor_simple = 0;
5838 if (require_constant_value && ! TREE_CONSTANT (value))
5840 error_init ("initializer element%s is not constant",
5842 value = error_mark_node;
5844 else if (require_constant_elements
5845 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5847 error_init ("initializer element%s is not computable at load time",
5849 value = error_mark_node;
5852 /* If this element duplicates one on constructor_pending_elts,
5853 print a message and ignore it. Don't do this when we're
5854 processing elements taken off constructor_pending_elts,
5855 because we'd always get spurious errors. */
5858 if (TREE_CODE (constructor_type) == RECORD_TYPE
5859 || TREE_CODE (constructor_type) == UNION_TYPE)
5861 if (purpose_member (field, constructor_pending_elts))
5863 error_init ("duplicate initializer%s", " for `%s'", NULL);
5867 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5870 for (tail = constructor_pending_elts; tail;
5871 tail = TREE_CHAIN (tail))
5872 if (TREE_PURPOSE (tail) != 0
5873 && TREE_CODE (TREE_PURPOSE (tail)) == INTEGER_CST
5874 && tree_int_cst_equal (TREE_PURPOSE (tail), constructor_index))
5879 error_init ("duplicate initializer%s", " for `%s'", NULL);
5885 /* If this element doesn't come next in sequence,
5886 put it on constructor_pending_elts. */
5887 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5888 && !tree_int_cst_equal (field, constructor_unfilled_index))
5891 /* The copy_node is needed in case field is actually
5892 constructor_index, which is modified in place. */
5893 constructor_pending_elts
5894 = tree_cons (copy_node (field),
5895 digest_init (type, value, require_constant_value,
5896 require_constant_elements),
5897 constructor_pending_elts);
5899 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5900 && field != constructor_unfilled_fields)
5902 /* We do this for records but not for unions. In a union,
5903 no matter which field is specified, it can be initialized
5904 right away since it starts at the beginning of the union. */
5906 constructor_pending_elts
5908 digest_init (type, value, require_constant_value,
5909 require_constant_elements),
5910 constructor_pending_elts);
5914 /* Otherwise, output this element either to
5915 constructor_elements or to the assembler file. */
5919 if (! constructor_incremental)
5921 if (field && TREE_CODE (field) == INTEGER_CST)
5922 field = copy_node (field);
5923 constructor_elements
5924 = tree_cons (field, digest_init (type, value,
5925 require_constant_value,
5926 require_constant_elements),
5927 constructor_elements);
5931 /* Structure elements may require alignment.
5932 Do this, if necessary. */
5933 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5935 /* Advance to offset of this element. */
5936 if (! tree_int_cst_equal (constructor_bit_index,
5937 DECL_FIELD_BITPOS (field)))
5939 int next = (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
5941 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5944 assemble_zeros (next - here);
5947 output_constant (digest_init (type, value,
5948 require_constant_value,
5949 require_constant_elements),
5950 int_size_in_bytes (type));
5952 /* For a record or union,
5953 keep track of end position of last field. */
5954 if (TREE_CODE (constructor_type) == RECORD_TYPE
5955 || TREE_CODE (constructor_type) == UNION_TYPE)
5957 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
5959 TREE_INT_CST_LOW (constructor_bit_index)
5960 = TREE_INT_CST_LOW (temp);
5961 TREE_INT_CST_HIGH (constructor_bit_index)
5962 = TREE_INT_CST_HIGH (temp);
5967 /* Advance the variable that indicates sequential elements output. */
5968 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5970 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
5972 TREE_INT_CST_LOW (constructor_unfilled_index)
5973 = TREE_INT_CST_LOW (tem);
5974 TREE_INT_CST_HIGH (constructor_unfilled_index)
5975 = TREE_INT_CST_HIGH (tem);
5977 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5978 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5979 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5980 constructor_unfilled_fields = 0;
5982 /* Now output any pending elements which have become next. */
5984 output_pending_init_elements (0);
5988 /* Output any pending elements which have become next.
5989 As we output elements, constructor_unfilled_{fields,index}
5990 advances, which may cause other elements to become next;
5991 if so, they too are output.
5993 If ALL is 0, we return when there are
5994 no more pending elements to output now.
5996 If ALL is 1, we output space as necessary so that
5997 we can output all the pending elements. */
6000 output_pending_init_elements (all)
6008 /* Look thru the whole pending list.
6009 If we find an element that should be output now,
6010 output it. Otherwise, set NEXT to the element
6011 that comes first among those still pending. */
6014 for (tail = constructor_pending_elts; tail;
6015 tail = TREE_CHAIN (tail))
6017 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6019 if (tree_int_cst_equal (TREE_PURPOSE (tail),
6020 constructor_unfilled_index))
6022 output_init_element (TREE_VALUE (tail),
6023 TREE_TYPE (constructor_type),
6024 constructor_unfilled_index, 0);
6027 else if (tree_int_cst_lt (TREE_PURPOSE (tail),
6028 constructor_unfilled_index))
6031 || tree_int_cst_lt (TREE_PURPOSE (tail), next))
6032 next = TREE_PURPOSE (tail);
6034 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6035 || TREE_CODE (constructor_type) == UNION_TYPE)
6037 if (TREE_PURPOSE (tail) == constructor_unfilled_fields)
6039 output_init_element (TREE_VALUE (tail),
6040 TREE_TYPE (constructor_unfilled_fields),
6041 constructor_unfilled_fields,
6045 else if (constructor_unfilled_fields == 0
6046 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
6047 DECL_FIELD_BITPOS (constructor_unfilled_fields)))
6050 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
6051 DECL_FIELD_BITPOS (next)))
6052 next = TREE_PURPOSE (tail);
6056 /* Ordinarily return, but not if we want to output all
6057 and there are elements left. */
6058 if (! (all && next != 0))
6061 /* Generate space up to the position of NEXT. */
6062 if (constructor_incremental)
6065 tree nextpos_tree = size_int (0);
6067 if (TREE_CODE (constructor_type) == RECORD_TYPE
6068 || TREE_CODE (constructor_type) == UNION_TYPE)
6070 /* Find the last field written out, if any. */
6071 for (tail = TYPE_FIELDS (constructor_type); tail;
6072 tail = TREE_CHAIN (tail))
6073 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
6077 /* Find the offset of the end of that field. */
6078 filled = size_binop (CEIL_DIV_EXPR,
6079 size_binop (PLUS_EXPR,
6080 DECL_FIELD_BITPOS (tail),
6082 size_int (BITS_PER_UNIT));
6084 filled = size_int (0);
6086 nextpos_tree = size_binop (CEIL_DIV_EXPR,
6087 DECL_FIELD_BITPOS (next),
6088 size_int (BITS_PER_UNIT));
6090 TREE_INT_CST_HIGH (constructor_bit_index)
6091 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
6092 TREE_INT_CST_LOW (constructor_bit_index)
6093 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
6094 constructor_unfilled_fields = next;
6096 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6098 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
6099 size_in_bytes (TREE_TYPE (constructor_type)));
6101 = size_binop (MULT_EXPR, next,
6102 size_in_bytes (TREE_TYPE (constructor_type)));
6103 TREE_INT_CST_LOW (constructor_unfilled_index)
6104 = TREE_INT_CST_LOW (next);
6105 TREE_INT_CST_HIGH (constructor_unfilled_index)
6106 = TREE_INT_CST_HIGH (next);
6113 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
6115 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
6120 /* If it's not incremental, just skip over the gap,
6121 so that after jumping to retry we will output the next
6122 successive element. */
6123 if (TREE_CODE (constructor_type) == RECORD_TYPE
6124 || TREE_CODE (constructor_type) == UNION_TYPE)
6125 constructor_unfilled_fields = next;
6126 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6128 TREE_INT_CST_LOW (constructor_unfilled_index)
6129 = TREE_INT_CST_LOW (next);
6130 TREE_INT_CST_HIGH (constructor_unfilled_index)
6131 = TREE_INT_CST_HIGH (next);
6138 /* Add one non-braced element to the current constructor level.
6139 This adjusts the current position within the constructor's type.
6140 This may also start or terminate implicit levels
6141 to handle a partly-braced initializer.
6143 Once this has found the correct level for the new element,
6144 it calls output_init_element.
6146 Note: if we are incrementally outputting this constructor,
6147 this function may be called with a null argument
6148 representing a sub-constructor that was already incrementally output.
6149 When that happens, we output nothing, but we do the bookkeeping
6150 to skip past that element of the current constructor. */
6153 process_init_element (value)
6156 tree orig_value = value;
6157 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6159 /* Handle superfluous braces around string cst as in
6160 char x[] = {"foo"}; */
6163 && TREE_CODE (constructor_type) == ARRAY_TYPE
6164 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6165 && integer_zerop (constructor_unfilled_index))
6167 constructor_stack->replacement_value = value;
6171 if (constructor_stack->replacement_value != 0)
6173 error_init ("excess elements in struct initializer%s",
6174 " after `%s'", NULL_PTR);
6178 /* Ignore elements of a brace group if it is entirely superfluous
6179 and has already been diagnosed. */
6180 if (constructor_type == 0)
6183 /* If we've exhausted any levels that didn't have braces,
6185 while (constructor_stack->implicit)
6187 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6188 || TREE_CODE (constructor_type) == UNION_TYPE)
6189 && constructor_fields == 0)
6190 process_init_element (pop_init_level (1));
6191 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6192 && tree_int_cst_lt (constructor_max_index, constructor_index))
6193 process_init_element (pop_init_level (1));
6200 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6203 enum tree_code fieldcode;
6205 if (constructor_fields == 0)
6207 pedwarn_init ("excess elements in struct initializer%s",
6208 " after `%s'", NULL_PTR);
6212 fieldtype = TREE_TYPE (constructor_fields);
6213 if (fieldtype != error_mark_node)
6214 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6215 fieldcode = TREE_CODE (fieldtype);
6217 /* Accept a string constant to initialize a subarray. */
6219 && fieldcode == ARRAY_TYPE
6220 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6223 /* Otherwise, if we have come to a subaggregate,
6224 and we don't have an element of its type, push into it. */
6225 else if (value != 0 && !constructor_no_implicit
6226 && value != error_mark_node
6227 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6228 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6229 || fieldcode == UNION_TYPE))
6231 push_init_level (1);
6237 push_member_name (constructor_fields);
6238 output_init_element (value, fieldtype, constructor_fields, 1);
6239 RESTORE_SPELLING_DEPTH (constructor_depth);
6242 /* Do the bookkeeping for an element that was
6243 directly output as a constructor. */
6245 /* For a record, keep track of end position of last field. */
6246 tree temp = size_binop (PLUS_EXPR,
6247 DECL_FIELD_BITPOS (constructor_fields),
6248 DECL_SIZE (constructor_fields));
6249 TREE_INT_CST_LOW (constructor_bit_index)
6250 = TREE_INT_CST_LOW (temp);
6251 TREE_INT_CST_HIGH (constructor_bit_index)
6252 = TREE_INT_CST_HIGH (temp);
6254 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6257 constructor_fields = TREE_CHAIN (constructor_fields);
6258 /* Skip any nameless bit fields at the beginning. */
6259 while (constructor_fields != 0
6260 && DECL_C_BIT_FIELD (constructor_fields)
6261 && DECL_NAME (constructor_fields) == 0)
6262 constructor_fields = TREE_CHAIN (constructor_fields);
6265 if (TREE_CODE (constructor_type) == UNION_TYPE)
6268 enum tree_code fieldcode;
6270 if (constructor_fields == 0)
6272 pedwarn_init ("excess elements in union initializer%s",
6273 " after `%s'", NULL_PTR);
6277 fieldtype = TREE_TYPE (constructor_fields);
6278 if (fieldtype != error_mark_node)
6279 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6280 fieldcode = TREE_CODE (fieldtype);
6282 /* Accept a string constant to initialize a subarray. */
6284 && fieldcode == ARRAY_TYPE
6285 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6288 /* Otherwise, if we have come to a subaggregate,
6289 and we don't have an element of its type, push into it. */
6290 else if (value != 0 && !constructor_no_implicit
6291 && value != error_mark_node
6292 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6293 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6294 || fieldcode == UNION_TYPE))
6296 push_init_level (1);
6302 push_member_name (constructor_fields);
6303 output_init_element (value, fieldtype, constructor_fields, 1);
6304 RESTORE_SPELLING_DEPTH (constructor_depth);
6307 /* Do the bookkeeping for an element that was
6308 directly output as a constructor. */
6310 TREE_INT_CST_LOW (constructor_bit_index)
6311 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6312 TREE_INT_CST_HIGH (constructor_bit_index)
6313 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6315 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6318 constructor_fields = 0;
6321 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6323 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6324 enum tree_code eltcode = TREE_CODE (elttype);
6326 /* Accept a string constant to initialize a subarray. */
6328 && eltcode == ARRAY_TYPE
6329 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6332 /* Otherwise, if we have come to a subaggregate,
6333 and we don't have an element of its type, push into it. */
6334 else if (value != 0 && !constructor_no_implicit
6335 && value != error_mark_node
6336 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6337 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6338 || eltcode == UNION_TYPE))
6340 push_init_level (1);
6344 if (constructor_max_index != 0
6345 && tree_int_cst_lt (constructor_max_index, constructor_index))
6347 pedwarn_init ("excess elements in array initializer%s",
6348 " after `%s'", NULL_PTR);
6352 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */
6353 if (constructor_range_end)
6354 value = save_expr (value);
6356 /* Now output the actual element.
6357 Ordinarily, output once.
6358 If there is a range, repeat it till we advance past the range. */
6365 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6366 output_init_element (value, elttype, constructor_index, 1);
6367 RESTORE_SPELLING_DEPTH (constructor_depth);
6370 tem = size_binop (PLUS_EXPR, constructor_index,
6372 TREE_INT_CST_LOW (constructor_index)
6373 = TREE_INT_CST_LOW (tem);
6374 TREE_INT_CST_HIGH (constructor_index)
6375 = TREE_INT_CST_HIGH (tem);
6378 /* If we are doing the bookkeeping for an element that was
6379 directly output as a constructor,
6380 we must update constructor_unfilled_index. */
6382 TREE_INT_CST_LOW (constructor_unfilled_index)
6383 = TREE_INT_CST_LOW (constructor_index);
6384 TREE_INT_CST_HIGH (constructor_unfilled_index)
6385 = TREE_INT_CST_HIGH (constructor_index);
6388 while (! (constructor_range_end == 0
6389 || tree_int_cst_lt (constructor_range_end,
6390 constructor_index)));
6395 /* Handle the sole element allowed in a braced initializer
6396 for a scalar variable. */
6397 if (constructor_fields == 0)
6399 pedwarn_init ("excess elements in scalar initializer%s",
6400 " after `%s'", NULL_PTR);
6405 output_init_element (value, constructor_type, NULL_TREE, 1);
6406 constructor_fields = 0;
6410 /* If the (lexically) previous elments are not now saved,
6411 we can discard the storage for them. */
6412 if (constructor_incremental && constructor_pending_elts == 0 && value != 0
6413 && constructor_stack == 0)
6417 /* Expand an ASM statement with operands, handling output operands
6418 that are not variables or INDIRECT_REFS by transforming such
6419 cases into cases that expand_asm_operands can handle.
6421 Arguments are same as for expand_asm_operands. */
6424 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6425 tree string, outputs, inputs, clobbers;
6430 int noutputs = list_length (outputs);
6432 /* o[I] is the place that output number I should be written. */
6433 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6436 if (TREE_CODE (string) == ADDR_EXPR)
6437 string = TREE_OPERAND (string, 0);
6438 if (TREE_CODE (string) != STRING_CST)
6440 error ("asm template is not a string constant");
6444 /* Record the contents of OUTPUTS before it is modified. */
6445 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6446 o[i] = TREE_VALUE (tail);
6448 /* Perform default conversions on array and function inputs. */
6449 /* Don't do this for other types--
6450 it would screw up operands expected to be in memory. */
6451 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6452 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6453 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6454 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6456 /* Generate the ASM_OPERANDS insn;
6457 store into the TREE_VALUEs of OUTPUTS some trees for
6458 where the values were actually stored. */
6459 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6461 /* Copy all the intermediate outputs into the specified outputs. */
6462 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6464 if (o[i] != TREE_VALUE (tail))
6466 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6470 /* Detect modification of read-only values.
6471 (Otherwise done by build_modify_expr.) */
6474 tree type = TREE_TYPE (o[i]);
6475 if (TREE_READONLY (o[i])
6476 || TYPE_READONLY (type)
6477 || ((TREE_CODE (type) == RECORD_TYPE
6478 || TREE_CODE (type) == UNION_TYPE)
6479 && C_TYPE_FIELDS_READONLY (type)))
6480 readonly_warning (o[i], "modification by `asm'");
6484 /* Those MODIFY_EXPRs could do autoincrements. */
6488 /* Expand a C `return' statement.
6489 RETVAL is the expression for what to return,
6490 or a null pointer for `return;' with no value. */
6493 c_expand_return (retval)
6496 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6498 if (TREE_THIS_VOLATILE (current_function_decl))
6499 warning ("function declared `noreturn' has a `return' statement");
6503 current_function_returns_null = 1;
6504 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6505 warning ("`return' with no value, in function returning non-void");
6506 expand_null_return ();
6508 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6510 current_function_returns_null = 1;
6511 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6512 pedwarn ("`return' with a value, in function returning void");
6513 expand_return (retval);
6517 tree t = convert_for_assignment (valtype, retval, "return",
6518 NULL_TREE, NULL_TREE, 0);
6519 tree res = DECL_RESULT (current_function_decl);
6522 if (t == error_mark_node)
6525 inner = t = convert (TREE_TYPE (res), t);
6527 /* Strip any conversions, additions, and subtractions, and see if
6528 we are returning the address of a local variable. Warn if so. */
6531 switch (TREE_CODE (inner))
6533 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6535 inner = TREE_OPERAND (inner, 0);
6539 /* If the second operand of the MINUS_EXPR has a pointer
6540 type (or is converted from it), this may be valid, so
6541 don't give a warning. */
6543 tree op1 = TREE_OPERAND (inner, 1);
6545 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6546 && (TREE_CODE (op1) == NOP_EXPR
6547 || TREE_CODE (op1) == NON_LVALUE_EXPR
6548 || TREE_CODE (op1) == CONVERT_EXPR))
6549 op1 = TREE_OPERAND (op1, 0);
6551 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6554 inner = TREE_OPERAND (inner, 0);
6559 inner = TREE_OPERAND (inner, 0);
6561 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6562 inner = TREE_OPERAND (inner, 0);
6564 if (TREE_CODE (inner) == VAR_DECL
6565 && ! DECL_EXTERNAL (inner)
6566 && ! TREE_STATIC (inner)
6567 && DECL_CONTEXT (inner) == current_function_decl)
6568 warning ("function returns address of local variable");
6575 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6576 TREE_SIDE_EFFECTS (t) = 1;
6578 current_function_returns_value = 1;
6582 /* Start a C switch statement, testing expression EXP.
6583 Return EXP if it is valid, an error node otherwise. */
6586 c_expand_start_case (exp)
6589 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6590 tree type = TREE_TYPE (exp);
6592 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6594 error ("switch quantity not an integer");
6595 exp = error_mark_node;
6600 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6602 if (warn_traditional
6603 && (type == long_integer_type_node
6604 || type == long_unsigned_type_node))
6605 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6607 exp = default_conversion (exp);
6608 type = TREE_TYPE (exp);
6609 index = get_unwidened (exp, NULL_TREE);
6610 /* We can't strip a conversion from a signed type to an unsigned,
6611 because if we did, int_fits_type_p would do the wrong thing
6612 when checking case values for being in range,
6613 and it's too hard to do the right thing. */
6614 if (TREE_UNSIGNED (TREE_TYPE (exp))
6615 == TREE_UNSIGNED (TREE_TYPE (index)))
6619 expand_start_case (1, exp, type, "switch statement");