1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 88, 91, 92, 93, 94, 1995 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization.
26 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
27 and to process initializations in declarations (since they work
28 like a strange sort of assignment). */
37 /* Nonzero if we've already printed a "missing braces around initializer"
38 message within this initializer. */
39 static int missing_braces_mentioned;
41 extern char *index ();
42 extern char *rindex ();
44 static tree quality_type PROTO((tree, tree));
45 static int comp_target_types PROTO((tree, tree));
46 static int function_types_compatible_p PROTO((tree, tree));
47 static int type_lists_compatible_p PROTO((tree, tree));
48 static int self_promoting_type_p PROTO((tree));
49 static tree decl_constant_value PROTO((tree));
50 static tree lookup_field PROTO((tree, tree, tree *));
51 static tree convert_arguments PROTO((tree, tree, tree, tree));
52 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
53 static tree pointer_diff PROTO((tree, tree));
54 static tree unary_complex_lvalue PROTO((enum tree_code, tree));
55 static void pedantic_lvalue_warning PROTO((enum tree_code));
56 static tree internal_build_compound_expr PROTO((tree, int));
57 static tree convert_for_assignment PROTO((tree, tree, char *, tree,
59 static void warn_for_assignment PROTO((char *, char *, tree, int));
60 static tree valid_compound_expr_initializer PROTO((tree, tree));
61 static void push_string PROTO((char *));
62 static void push_member_name PROTO((tree));
63 static void push_array_bounds PROTO((int));
64 static int spelling_length PROTO((void));
65 static char *print_spelling PROTO((char *));
66 static char *get_spelling PROTO((char *));
67 static void warning_init PROTO((char *, char *,
69 static tree digest_init PROTO((tree, tree, int, int));
70 static void check_init_type_bitfields PROTO((tree));
71 static void output_init_element PROTO((tree, tree, tree, int));
72 static void output_pending_init_elements PROTO((int));
74 /* Do `exp = require_complete_type (exp);' to make sure exp
75 does not have an incomplete type. (That includes void types.) */
78 require_complete_type (value)
81 tree type = TREE_TYPE (value);
83 /* First, detect a valid value with a complete type. */
84 if (TYPE_SIZE (type) != 0
85 && type != void_type_node)
88 incomplete_type_error (value, type);
89 return error_mark_node;
92 /* Print an error message for invalid use of an incomplete type.
93 VALUE is the expression that was used (or 0 if that isn't known)
94 and TYPE is the type that was invalid. */
97 incomplete_type_error (value, type)
103 /* Avoid duplicate error message. */
104 if (TREE_CODE (type) == ERROR_MARK)
107 if (value != 0 && (TREE_CODE (value) == VAR_DECL
108 || TREE_CODE (value) == PARM_DECL))
109 error ("`%s' has an incomplete type",
110 IDENTIFIER_POINTER (DECL_NAME (value)));
114 /* We must print an error message. Be clever about what it says. */
116 switch (TREE_CODE (type))
119 errmsg = "invalid use of undefined type `struct %s'";
123 errmsg = "invalid use of undefined type `union %s'";
127 errmsg = "invalid use of undefined type `enum %s'";
131 error ("invalid use of void expression");
135 if (TYPE_DOMAIN (type))
137 type = TREE_TYPE (type);
140 error ("invalid use of array with unspecified bounds");
147 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
148 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
150 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
151 error ("invalid use of incomplete typedef `%s'",
152 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
156 /* Return a variant of TYPE which has all the type qualifiers of LIKE
157 as well as those of TYPE. */
160 qualify_type (type, like)
163 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
164 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
165 return c_build_type_variant (type, constflag, volflag);
168 /* Return the common type of two types.
169 We assume that comptypes has already been done and returned 1;
170 if that isn't so, this may crash. In particular, we assume that qualifiers
173 This is the type for the result of most arithmetic operations
174 if the operands have the given two types. */
180 register enum tree_code code1;
181 register enum tree_code code2;
184 /* Save time if the two types are the same. */
186 if (t1 == t2) return t1;
188 /* If one type is nonsense, use the other. */
189 if (t1 == error_mark_node)
191 if (t2 == error_mark_node)
194 /* Merge the attributes */
196 { register tree a1, a2;
197 a1 = TYPE_ATTRIBUTES (t1);
198 a2 = TYPE_ATTRIBUTES (t2);
200 /* Either one unset? Take the set one. */
202 if (!(attributes = a1))
205 /* One that completely contains the other? Take it. */
207 else if (a2 && !attribute_list_contained (a1, a2))
208 if (attribute_list_contained (a2, a1))
212 /* Pick the longest list, and hang on the other
215 if (list_length (a1) < list_length (a2))
216 attributes = a2, a2 = a1;
218 for (; a2; a2 = TREE_CHAIN (a2))
219 if (!value_member (attributes, a2))
222 TREE_CHAIN (a1) = attributes;
228 /* Treat an enum type as the unsigned integer type of the same width. */
230 if (TREE_CODE (t1) == ENUMERAL_TYPE)
231 t1 = type_for_size (TYPE_PRECISION (t1), 1);
232 if (TREE_CODE (t2) == ENUMERAL_TYPE)
233 t2 = type_for_size (TYPE_PRECISION (t2), 1);
235 code1 = TREE_CODE (t1);
236 code2 = TREE_CODE (t2);
238 /* If one type is complex, form the common type of the non-complex
239 components, then make that complex. Use T1 or T2 if it is the
241 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
243 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
244 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
245 tree subtype = common_type (subtype1, subtype2);
247 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
248 return build_type_attribute_variant (t1, attributes);
249 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
250 return build_type_attribute_variant (t2, attributes);
252 return build_type_attribute_variant (build_complex_type (subtype),
260 /* If only one is real, use it as the result. */
262 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
263 return build_type_attribute_variant (t1, attributes);
265 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
266 return build_type_attribute_variant (t2, attributes);
268 /* Both real or both integers; use the one with greater precision. */
270 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
271 return build_type_attribute_variant (t1, attributes);
272 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
273 return build_type_attribute_variant (t2, attributes);
275 /* Same precision. Prefer longs to ints even when same size. */
277 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
278 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
279 return build_type_attribute_variant (long_unsigned_type_node,
282 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
283 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
285 /* But preserve unsignedness from the other type,
286 since long cannot hold all the values of an unsigned int. */
287 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
288 t1 = long_unsigned_type_node;
290 t1 = long_integer_type_node;
291 return build_type_attribute_variant (t1, attributes);
294 /* Otherwise prefer the unsigned one. */
296 if (TREE_UNSIGNED (t1))
297 return build_type_attribute_variant (t1, attributes);
299 return build_type_attribute_variant (t2, attributes);
302 /* For two pointers, do this recursively on the target type,
303 and combine the qualifiers of the two types' targets. */
304 /* This code was turned off; I don't know why.
305 But ANSI C specifies doing this with the qualifiers.
306 So I turned it on again. */
308 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
309 TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
311 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
313 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
314 t1 = build_pointer_type (c_build_type_variant (target, constp,
316 return build_type_attribute_variant (t1, attributes);
319 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
320 return build_type_attribute_variant (t1, attributes);
325 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
326 /* Save space: see if the result is identical to one of the args. */
327 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
328 return build_type_attribute_variant (t1, attributes);
329 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
330 return build_type_attribute_variant (t2, attributes);
331 /* Merge the element types, and have a size if either arg has one. */
332 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
333 return build_type_attribute_variant (t1, attributes);
337 /* Function types: prefer the one that specified arg types.
338 If both do, merge the arg types. Also merge the return types. */
340 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
341 tree p1 = TYPE_ARG_TYPES (t1);
342 tree p2 = TYPE_ARG_TYPES (t2);
347 /* Save space: see if the result is identical to one of the args. */
348 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
349 return build_type_attribute_variant (t1, attributes);
350 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
351 return build_type_attribute_variant (t2, attributes);
353 /* Simple way if one arg fails to specify argument types. */
354 if (TYPE_ARG_TYPES (t1) == 0)
356 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
357 return build_type_attribute_variant (t1, attributes);
359 if (TYPE_ARG_TYPES (t2) == 0)
361 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
362 return build_type_attribute_variant (t1, attributes);
365 /* If both args specify argument types, we must merge the two
366 lists, argument by argument. */
368 len = list_length (p1);
371 for (i = 0; i < len; i++)
372 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
377 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
379 /* A null type means arg type is not specified.
380 Take whatever the other function type has. */
381 if (TREE_VALUE (p1) == 0)
383 TREE_VALUE (n) = TREE_VALUE (p2);
386 if (TREE_VALUE (p2) == 0)
388 TREE_VALUE (n) = TREE_VALUE (p1);
392 /* Given wait (union {union wait *u; int *i} *)
393 and wait (union wait *),
394 prefer union wait * as type of parm. */
395 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
396 && TREE_VALUE (p1) != TREE_VALUE (p2))
399 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
400 memb; memb = TREE_CHAIN (memb))
401 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
403 TREE_VALUE (n) = TREE_VALUE (p2);
405 pedwarn ("function types not truly compatible in ANSI C");
409 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
410 && TREE_VALUE (p2) != TREE_VALUE (p1))
413 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
414 memb; memb = TREE_CHAIN (memb))
415 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
417 TREE_VALUE (n) = TREE_VALUE (p1);
419 pedwarn ("function types not truly compatible in ANSI C");
423 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
427 t1 = build_function_type (valtype, newargs);
428 /* ... falls through ... */
432 return build_type_attribute_variant (t1, attributes);
437 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
438 or various other operations. Return 2 if they are compatible
439 but a warning may be needed if you use them together. */
442 comptypes (type1, type2)
445 register tree t1 = type1;
446 register tree t2 = type2;
449 /* Suppress errors caused by previously reported errors. */
451 if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
454 /* Treat an enum type as the integer type of the same width and
457 if (TREE_CODE (t1) == ENUMERAL_TYPE)
458 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
459 if (TREE_CODE (t2) == ENUMERAL_TYPE)
460 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
465 /* Different classes of types can't be compatible. */
467 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
469 /* Qualifiers must match. */
471 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
473 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
476 /* Allow for two different type nodes which have essentially the same
477 definition. Note that we already checked for equality of the type
478 type qualifiers (just above). */
480 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
483 #ifndef COMP_TYPE_ATTRIBUTES
484 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
487 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
488 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
491 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
494 switch (TREE_CODE (t1))
497 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
498 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
502 val = function_types_compatible_p (t1, t2);
507 tree d1 = TYPE_DOMAIN (t1);
508 tree d2 = TYPE_DOMAIN (t2);
511 /* Target types must match incl. qualifiers. */
512 if (TREE_TYPE (t1) != TREE_TYPE (t2)
513 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
516 /* Sizes must match unless one is missing or variable. */
517 if (d1 == 0 || d2 == 0 || d1 == d2
518 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
519 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
520 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
521 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
524 if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
525 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
526 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
527 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
528 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
529 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
530 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
531 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2)))))
537 if (maybe_objc_comptypes (t1, t2, 0) == 1)
541 return attrval == 2 && val == 1 ? 2 : val;
544 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
545 ignoring their qualifiers. */
548 comp_target_types (ttl, ttr)
553 /* Give maybe_objc_comptypes a crack at letting these types through. */
554 if (val = maybe_objc_comptypes (ttl, ttr, 1) >= 0)
557 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
558 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
560 if (val == 2 && pedantic)
561 pedwarn ("types are not quite compatible");
565 /* Subroutines of `comptypes'. */
567 /* Return 1 if two function types F1 and F2 are compatible.
568 If either type specifies no argument types,
569 the other must specify a fixed number of self-promoting arg types.
570 Otherwise, if one type specifies only the number of arguments,
571 the other must specify that number of self-promoting arg types.
572 Otherwise, the argument types must match. */
575 function_types_compatible_p (f1, f2)
579 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
583 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
584 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
587 args1 = TYPE_ARG_TYPES (f1);
588 args2 = TYPE_ARG_TYPES (f2);
590 /* An unspecified parmlist matches any specified parmlist
591 whose argument types don't need default promotions. */
595 if (!self_promoting_args_p (args2))
597 /* If one of these types comes from a non-prototype fn definition,
598 compare that with the other type's arglist.
599 If they don't match, ask for a warning (but no error). */
600 if (TYPE_ACTUAL_ARG_TYPES (f1)
601 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
607 if (!self_promoting_args_p (args1))
609 if (TYPE_ACTUAL_ARG_TYPES (f2)
610 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
615 /* Both types have argument lists: compare them and propagate results. */
616 val1 = type_lists_compatible_p (args1, args2);
617 return val1 != 1 ? val1 : val;
620 /* Check two lists of types for compatibility,
621 returning 0 for incompatible, 1 for compatible,
622 or 2 for compatible with warning. */
625 type_lists_compatible_p (args1, args2)
628 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
634 if (args1 == 0 && args2 == 0)
636 /* If one list is shorter than the other,
637 they fail to match. */
638 if (args1 == 0 || args2 == 0)
640 /* A null pointer instead of a type
641 means there is supposed to be an argument
642 but nothing is specified about what type it has.
643 So match anything that self-promotes. */
644 if (TREE_VALUE (args1) == 0)
646 if (! self_promoting_type_p (TREE_VALUE (args2)))
649 else if (TREE_VALUE (args2) == 0)
651 if (! self_promoting_type_p (TREE_VALUE (args1)))
654 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
656 /* Allow wait (union {union wait *u; int *i} *)
657 and wait (union wait *) to be compatible. */
658 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
659 && (TYPE_NAME (TREE_VALUE (args1)) == 0
660 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
661 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
662 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
663 TYPE_SIZE (TREE_VALUE (args2))))
666 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
667 memb; memb = TREE_CHAIN (memb))
668 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
673 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
674 && (TYPE_NAME (TREE_VALUE (args2)) == 0
675 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
676 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
677 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
678 TYPE_SIZE (TREE_VALUE (args1))))
681 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
682 memb; memb = TREE_CHAIN (memb))
683 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
692 /* comptypes said ok, but record if it said to warn. */
696 args1 = TREE_CHAIN (args1);
697 args2 = TREE_CHAIN (args2);
701 /* Return 1 if PARMS specifies a fixed number of parameters
702 and none of their types is affected by default promotions. */
705 self_promoting_args_p (parms)
709 for (t = parms; t; t = TREE_CHAIN (t))
711 register tree type = TREE_VALUE (t);
713 if (TREE_CHAIN (t) == 0 && type != void_type_node)
719 if (TYPE_MAIN_VARIANT (type) == float_type_node)
722 if (C_PROMOTING_INTEGER_TYPE_P (type))
728 /* Return 1 if TYPE is not affected by default promotions. */
731 self_promoting_type_p (type)
734 if (TYPE_MAIN_VARIANT (type) == float_type_node)
737 if (C_PROMOTING_INTEGER_TYPE_P (type))
743 /* Return an unsigned type the same as TYPE in other respects. */
749 tree type1 = TYPE_MAIN_VARIANT (type);
750 if (type1 == signed_char_type_node || type1 == char_type_node)
751 return unsigned_char_type_node;
752 if (type1 == integer_type_node)
753 return unsigned_type_node;
754 if (type1 == short_integer_type_node)
755 return short_unsigned_type_node;
756 if (type1 == long_integer_type_node)
757 return long_unsigned_type_node;
758 if (type1 == long_long_integer_type_node)
759 return long_long_unsigned_type_node;
763 /* Return a signed type the same as TYPE in other respects. */
769 tree type1 = TYPE_MAIN_VARIANT (type);
770 if (type1 == unsigned_char_type_node || type1 == char_type_node)
771 return signed_char_type_node;
772 if (type1 == unsigned_type_node)
773 return integer_type_node;
774 if (type1 == short_unsigned_type_node)
775 return short_integer_type_node;
776 if (type1 == long_unsigned_type_node)
777 return long_integer_type_node;
778 if (type1 == long_long_unsigned_type_node)
779 return long_long_integer_type_node;
783 /* Return a type the same as TYPE except unsigned or
784 signed according to UNSIGNEDP. */
787 signed_or_unsigned_type (unsignedp, type)
791 if (! INTEGRAL_TYPE_P (type))
793 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
794 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
795 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
796 return unsignedp ? unsigned_type_node : integer_type_node;
797 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
798 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
799 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
800 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
801 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
802 return (unsignedp ? long_long_unsigned_type_node
803 : long_long_integer_type_node);
807 /* Compute the value of the `sizeof' operator. */
813 enum tree_code code = TREE_CODE (type);
816 if (code == FUNCTION_TYPE)
818 if (pedantic || warn_pointer_arith)
819 pedwarn ("sizeof applied to a function type");
822 if (code == VOID_TYPE)
824 if (pedantic || warn_pointer_arith)
825 pedwarn ("sizeof applied to a void type");
828 if (code == ERROR_MARK)
830 if (TYPE_SIZE (type) == 0)
832 error ("sizeof applied to an incomplete type");
836 /* Convert in case a char is more than one unit. */
837 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
838 size_int (TYPE_PRECISION (char_type_node)));
839 /* size_binop does not put the constant in range, so do it now. */
840 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
841 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
846 c_sizeof_nowarn (type)
849 enum tree_code code = TREE_CODE (type);
852 if (code == FUNCTION_TYPE
854 || code == ERROR_MARK)
856 if (TYPE_SIZE (type) == 0)
859 /* Convert in case a char is more than one unit. */
860 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
861 size_int (TYPE_PRECISION (char_type_node)));
862 force_fit_type (t, 0);
866 /* Compute the size to increment a pointer by. */
869 c_size_in_bytes (type)
872 enum tree_code code = TREE_CODE (type);
875 if (code == FUNCTION_TYPE)
877 if (code == VOID_TYPE)
879 if (code == ERROR_MARK)
881 if (TYPE_SIZE (type) == 0)
883 error ("arithmetic on pointer to an incomplete type");
887 /* Convert in case a char is more than one unit. */
888 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
889 size_int (BITS_PER_UNIT));
890 force_fit_type (t, 0);
894 /* Implement the __alignof keyword: Return the minimum required
895 alignment of TYPE, measured in bytes. */
901 enum tree_code code = TREE_CODE (type);
903 if (code == FUNCTION_TYPE)
904 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
906 if (code == VOID_TYPE || code == ERROR_MARK)
909 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
912 /* Implement the __alignof keyword: Return the minimum required
913 alignment of EXPR, measured in bytes. For VAR_DECL's and
914 FIELD_DECL's return DECL_ALIGN (which can be set from an
915 "aligned" __attribute__ specification). */
918 c_alignof_expr (expr)
921 if (TREE_CODE (expr) == VAR_DECL)
922 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
924 if (TREE_CODE (expr) == COMPONENT_REF
925 && DECL_BIT_FIELD (TREE_OPERAND (expr, 1)))
927 error ("`__alignof' applied to a bit-field");
930 else if (TREE_CODE (expr) == COMPONENT_REF
931 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
932 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
934 if (TREE_CODE (expr) == INDIRECT_REF)
936 tree t = TREE_OPERAND (expr, 0);
938 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
940 while (TREE_CODE (t) == NOP_EXPR
941 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
945 t = TREE_OPERAND (t, 0);
946 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
947 if (thisalign > bestalign)
948 best = t, bestalign = thisalign;
950 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
953 return c_alignof (TREE_TYPE (expr));
955 /* Return either DECL or its known constant value (if it has one). */
958 decl_constant_value (decl)
961 if (! TREE_PUBLIC (decl)
962 /* Don't change a variable array bound or initial value to a constant
963 in a place where a variable is invalid. */
964 && current_function_decl != 0
966 && ! TREE_THIS_VOLATILE (decl)
967 && TREE_READONLY (decl) && ! ITERATOR_P (decl)
968 && DECL_INITIAL (decl) != 0
969 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
970 /* This is invalid if initial value is not constant.
971 If it has either a function call, a memory reference,
972 or a variable, then re-evaluating it could give different results. */
973 && TREE_CONSTANT (DECL_INITIAL (decl))
974 /* Check for cases where this is sub-optimal, even though valid. */
975 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
976 && DECL_MODE (decl) != BLKmode)
977 return DECL_INITIAL (decl);
981 /* Perform default promotions for C data used in expressions.
982 Arrays and functions are converted to pointers;
983 enumeral types or short or char, to int.
984 In addition, manifest constants symbols are replaced by their values. */
987 default_conversion (exp)
990 register tree type = TREE_TYPE (exp);
991 register enum tree_code code = TREE_CODE (type);
993 /* Constants can be used directly unless they're not loadable. */
994 if (TREE_CODE (exp) == CONST_DECL)
995 exp = DECL_INITIAL (exp);
997 /* Replace a nonvolatile const static variable with its value unless
998 it is an array, in which case we must be sure that taking the
999 address of the array produces consistent results. */
1000 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1002 exp = decl_constant_value (exp);
1003 type = TREE_TYPE (exp);
1006 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1008 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
1009 to integer and cause infinite recursion. */
1010 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1011 || (TREE_CODE (exp) == NOP_EXPR
1012 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1013 exp = TREE_OPERAND (exp, 0);
1015 /* Normally convert enums to int,
1016 but convert wide enums to something wider. */
1017 if (code == ENUMERAL_TYPE)
1019 type = type_for_size (MAX (TYPE_PRECISION (type),
1020 TYPE_PRECISION (integer_type_node)),
1022 || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node))
1023 && TREE_UNSIGNED (type)));
1024 return convert (type, exp);
1027 if (C_PROMOTING_INTEGER_TYPE_P (type))
1029 /* Traditionally, unsignedness is preserved in default promotions.
1030 Also preserve unsignedness if not really getting any wider. */
1031 if (TREE_UNSIGNED (type)
1032 && (flag_traditional
1033 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1034 return convert (unsigned_type_node, exp);
1035 return convert (integer_type_node, exp);
1037 if (flag_traditional && !flag_allow_single_precision
1038 && TYPE_MAIN_VARIANT (type) == float_type_node)
1039 return convert (double_type_node, exp);
1040 if (code == VOID_TYPE)
1042 error ("void value not ignored as it ought to be");
1043 return error_mark_node;
1045 if (code == FUNCTION_TYPE)
1047 return build_unary_op (ADDR_EXPR, exp, 0);
1049 if (code == ARRAY_TYPE)
1052 tree restype = TREE_TYPE (type);
1057 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1058 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1060 constp = TREE_READONLY (exp);
1061 volatilep = TREE_THIS_VOLATILE (exp);
1064 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1065 || constp || volatilep)
1066 restype = c_build_type_variant (restype,
1067 TYPE_READONLY (type) || constp,
1068 TYPE_VOLATILE (type) || volatilep);
1070 if (TREE_CODE (exp) == INDIRECT_REF)
1071 return convert (TYPE_POINTER_TO (restype),
1072 TREE_OPERAND (exp, 0));
1074 if (TREE_CODE (exp) == COMPOUND_EXPR)
1076 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1077 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1078 TREE_OPERAND (exp, 0), op1);
1082 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1084 error ("invalid use of non-lvalue array");
1085 return error_mark_node;
1088 ptrtype = build_pointer_type (restype);
1090 if (TREE_CODE (exp) == VAR_DECL)
1092 /* ??? This is not really quite correct
1093 in that the type of the operand of ADDR_EXPR
1094 is not the target type of the type of the ADDR_EXPR itself.
1095 Question is, can this lossage be avoided? */
1096 adr = build1 (ADDR_EXPR, ptrtype, exp);
1097 if (mark_addressable (exp) == 0)
1098 return error_mark_node;
1099 TREE_CONSTANT (adr) = staticp (exp);
1100 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1103 /* This way is better for a COMPONENT_REF since it can
1104 simplify the offset for a component. */
1105 adr = build_unary_op (ADDR_EXPR, exp, 1);
1106 return convert (ptrtype, adr);
1111 /* Look up component name in the structure type definition.
1113 If this component name is found indirectly within an anonymous union,
1114 store in *INDIRECT the component which directly contains
1115 that anonymous union. Otherwise, set *INDIRECT to 0. */
1118 lookup_field (type, component, indirect)
1119 tree type, component;
1124 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1125 to the field elements. Use a binary search on this array to quickly
1126 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1127 will always be set for structures which have many elements. */
1129 if (TYPE_LANG_SPECIFIC (type))
1132 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1134 field = TYPE_FIELDS (type);
1136 top = TYPE_LANG_SPECIFIC (type)->len;
1137 while (top - bot > 1)
1141 half = (top - bot + 1) >> 1;
1142 field = field_array[bot+half];
1144 if (DECL_NAME (field) == NULL_TREE)
1146 /* Step through all anon unions in linear fashion. */
1147 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1151 field = field_array[bot++];
1152 anon = lookup_field (TREE_TYPE (field), component, &junk);
1153 if (anon != NULL_TREE)
1160 /* Entire record is only anon unions. */
1164 /* Restart the binary search, with new lower bound. */
1168 cmp = (HOST_WIDE_INT) DECL_NAME (field) - (HOST_WIDE_INT) component;
1177 if (DECL_NAME (field_array[bot]) == component)
1178 field = field_array[bot];
1179 else if (DECL_NAME (field) != component)
1184 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1186 if (DECL_NAME (field) == NULL_TREE)
1189 tree anon = lookup_field (TREE_TYPE (field), component, &junk);
1190 if (anon != NULL_TREE)
1197 if (DECL_NAME (field) == component)
1202 *indirect = NULL_TREE;
1206 /* Make an expression to refer to the COMPONENT field of
1207 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1210 build_component_ref (datum, component)
1211 tree datum, component;
1213 register tree type = TREE_TYPE (datum);
1214 register enum tree_code code = TREE_CODE (type);
1215 register tree field = NULL;
1218 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1219 unless we are not to support things not strictly ANSI. */
1220 switch (TREE_CODE (datum))
1224 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1225 return build (COMPOUND_EXPR, TREE_TYPE (value),
1226 TREE_OPERAND (datum, 0), value);
1229 return build_conditional_expr
1230 (TREE_OPERAND (datum, 0),
1231 build_component_ref (TREE_OPERAND (datum, 1), component),
1232 build_component_ref (TREE_OPERAND (datum, 2), component));
1235 /* See if there is a field or component with name COMPONENT. */
1237 if (code == RECORD_TYPE || code == UNION_TYPE)
1241 if (TYPE_SIZE (type) == 0)
1243 incomplete_type_error (NULL_TREE, type);
1244 return error_mark_node;
1247 field = lookup_field (type, component, &indirect);
1251 error (code == RECORD_TYPE
1252 ? "structure has no member named `%s'"
1253 : "union has no member named `%s'",
1254 IDENTIFIER_POINTER (component));
1255 return error_mark_node;
1257 if (TREE_TYPE (field) == error_mark_node)
1258 return error_mark_node;
1260 /* If FIELD was found buried within an anonymous union,
1261 make one COMPONENT_REF to get that anonymous union,
1262 then fall thru to make a second COMPONENT_REF to get FIELD. */
1265 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1266 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1267 TREE_READONLY (ref) = 1;
1268 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1269 TREE_THIS_VOLATILE (ref) = 1;
1273 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1275 if (TREE_READONLY (datum) || TREE_READONLY (field))
1276 TREE_READONLY (ref) = 1;
1277 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1278 TREE_THIS_VOLATILE (ref) = 1;
1282 else if (code != ERROR_MARK)
1283 error ("request for member `%s' in something not a structure or union",
1284 IDENTIFIER_POINTER (component));
1286 return error_mark_node;
1289 /* Given an expression PTR for a pointer, return an expression
1290 for the value pointed to.
1291 ERRORSTRING is the name of the operator to appear in error messages. */
1294 build_indirect_ref (ptr, errorstring)
1298 register tree pointer = default_conversion (ptr);
1299 register tree type = TREE_TYPE (pointer);
1301 if (TREE_CODE (type) == POINTER_TYPE)
1303 if (TREE_CODE (pointer) == ADDR_EXPR
1305 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1306 == TREE_TYPE (type)))
1307 return TREE_OPERAND (pointer, 0);
1310 tree t = TREE_TYPE (type);
1311 register tree ref = build1 (INDIRECT_REF,
1312 TYPE_MAIN_VARIANT (t), pointer);
1314 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1316 error ("dereferencing pointer to incomplete type");
1317 return error_mark_node;
1319 if (TREE_CODE (t) == VOID_TYPE)
1320 warning ("dereferencing `void *' pointer");
1322 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1323 so that we get the proper error message if the result is used
1324 to assign to. Also, &* is supposed to be a no-op.
1325 And ANSI C seems to specify that the type of the result
1326 should be the const type. */
1327 /* A de-reference of a pointer to const is not a const. It is valid
1328 to change it via some other pointer. */
1329 TREE_READONLY (ref) = TYPE_READONLY (t);
1330 TREE_SIDE_EFFECTS (ref)
1331 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1332 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1336 else if (TREE_CODE (pointer) != ERROR_MARK)
1337 error ("invalid type argument of `%s'", errorstring);
1338 return error_mark_node;
1341 /* This handles expressions of the form "a[i]", which denotes
1344 This is logically equivalent in C to *(a+i), but we may do it differently.
1345 If A is a variable or a member, we generate a primitive ARRAY_REF.
1346 This avoids forcing the array out of registers, and can work on
1347 arrays that are not lvalues (for example, members of structures returned
1351 build_array_ref (array, index)
1356 error ("subscript missing in array reference");
1357 return error_mark_node;
1360 if (TREE_TYPE (array) == error_mark_node
1361 || TREE_TYPE (index) == error_mark_node)
1362 return error_mark_node;
1364 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1365 && TREE_CODE (array) != INDIRECT_REF)
1369 /* Subscripting with type char is likely to lose
1370 on a machine where chars are signed.
1371 So warn on any machine, but optionally.
1372 Don't warn for unsigned char since that type is safe.
1373 Don't warn for signed char because anyone who uses that
1374 must have done so deliberately. */
1375 if (warn_char_subscripts
1376 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1377 warning ("array subscript has type `char'");
1379 /* Apply default promotions *after* noticing character types. */
1380 index = default_conversion (index);
1382 /* Require integer *after* promotion, for sake of enums. */
1383 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1385 error ("array subscript is not an integer");
1386 return error_mark_node;
1389 /* An array that is indexed by a non-constant
1390 cannot be stored in a register; we must be able to do
1391 address arithmetic on its address.
1392 Likewise an array of elements of variable size. */
1393 if (TREE_CODE (index) != INTEGER_CST
1394 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1395 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1397 if (mark_addressable (array) == 0)
1398 return error_mark_node;
1400 /* An array that is indexed by a constant value which is not within
1401 the array bounds cannot be stored in a register either; because we
1402 would get a crash in store_bit_field/extract_bit_field when trying
1403 to access a non-existent part of the register. */
1404 if (TREE_CODE (index) == INTEGER_CST
1405 && TYPE_VALUES (TREE_TYPE (array))
1406 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1408 if (mark_addressable (array) == 0)
1409 return error_mark_node;
1412 if (pedantic && !lvalue_p (array))
1414 if (DECL_REGISTER (array))
1415 pedwarn ("ANSI C forbids subscripting `register' array");
1417 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1423 while (TREE_CODE (foo) == COMPONENT_REF)
1424 foo = TREE_OPERAND (foo, 0);
1425 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1426 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1429 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1430 rval = build (ARRAY_REF, type, array, index);
1431 /* Array ref is const/volatile if the array elements are
1432 or if the array is. */
1433 TREE_READONLY (rval)
1434 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1435 | TREE_READONLY (array));
1436 TREE_SIDE_EFFECTS (rval)
1437 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1438 | TREE_SIDE_EFFECTS (array));
1439 TREE_THIS_VOLATILE (rval)
1440 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1441 /* This was added by rms on 16 Nov 91.
1442 It fixes vol struct foo *a; a->elts[1]
1443 in an inline function.
1444 Hope it doesn't break something else. */
1445 | TREE_THIS_VOLATILE (array));
1446 return require_complete_type (fold (rval));
1450 tree ar = default_conversion (array);
1451 tree ind = default_conversion (index);
1453 /* Put the integer in IND to simplify error checking. */
1454 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1461 if (ar == error_mark_node)
1464 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
1466 error ("subscripted value is neither array nor pointer");
1467 return error_mark_node;
1469 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1471 error ("array subscript is not an integer");
1472 return error_mark_node;
1475 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1480 /* Build a function call to function FUNCTION with parameters PARAMS.
1481 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1482 TREE_VALUE of each node is a parameter-expression.
1483 FUNCTION's data type may be a function type or a pointer-to-function. */
1486 build_function_call (function, params)
1487 tree function, params;
1489 register tree fntype, fundecl = 0;
1490 register tree coerced_params;
1491 tree name = NULL_TREE, assembler_name = NULL_TREE;
1493 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1494 STRIP_TYPE_NOPS (function);
1496 /* Convert anything with function type to a pointer-to-function. */
1497 if (TREE_CODE (function) == FUNCTION_DECL)
1499 name = DECL_NAME (function);
1500 assembler_name = DECL_ASSEMBLER_NAME (function);
1502 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1503 (because calling an inline function does not mean the function
1504 needs to be separately compiled). */
1505 fntype = build_type_variant (TREE_TYPE (function),
1506 TREE_READONLY (function),
1507 TREE_THIS_VOLATILE (function));
1509 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1512 function = default_conversion (function);
1514 fntype = TREE_TYPE (function);
1516 if (TREE_CODE (fntype) == ERROR_MARK)
1517 return error_mark_node;
1519 if (!(TREE_CODE (fntype) == POINTER_TYPE
1520 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1522 error ("called object is not a function");
1523 return error_mark_node;
1526 /* fntype now gets the type of function pointed to. */
1527 fntype = TREE_TYPE (fntype);
1529 /* Convert the parameters to the types declared in the
1530 function prototype, or apply default promotions. */
1533 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1535 /* Check for errors in format strings. */
1537 if (warn_format && (name || assembler_name))
1538 check_function_format (name, assembler_name, coerced_params);
1540 /* Recognize certain built-in functions so we can make tree-codes
1541 other than CALL_EXPR. We do this when it enables fold-const.c
1542 to do something useful. */
1544 if (TREE_CODE (function) == ADDR_EXPR
1545 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1546 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1547 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1552 if (coerced_params == 0)
1553 return integer_zero_node;
1554 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1558 register tree result
1559 = build (CALL_EXPR, TREE_TYPE (fntype),
1560 function, coerced_params, NULL_TREE);
1562 TREE_SIDE_EFFECTS (result) = 1;
1563 if (TREE_TYPE (result) == void_type_node)
1565 return require_complete_type (result);
1569 /* Convert the argument expressions in the list VALUES
1570 to the types in the list TYPELIST. The result is a list of converted
1571 argument expressions.
1573 If TYPELIST is exhausted, or when an element has NULL as its type,
1574 perform the default conversions.
1576 PARMLIST is the chain of parm decls for the function being called.
1577 It may be 0, if that info is not available.
1578 It is used only for generating error messages.
1580 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1582 This is also where warnings about wrong number of args are generated.
1584 Both VALUES and the returned value are chains of TREE_LIST nodes
1585 with the elements of the list in the TREE_VALUE slots of those nodes. */
1588 convert_arguments (typelist, values, name, fundecl)
1589 tree typelist, values, name, fundecl;
1591 register tree typetail, valtail;
1592 register tree result = NULL;
1595 /* Scan the given expressions and types, producing individual
1596 converted arguments and pushing them on RESULT in reverse order. */
1598 for (valtail = values, typetail = typelist, parmnum = 0;
1600 valtail = TREE_CHAIN (valtail), parmnum++)
1602 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1603 register tree val = TREE_VALUE (valtail);
1605 if (type == void_type_node)
1608 error ("too many arguments to function `%s'",
1609 IDENTIFIER_POINTER (name));
1611 error ("too many arguments to function");
1615 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1616 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1617 to convert automatically to a pointer. */
1618 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1619 val = TREE_OPERAND (val, 0);
1621 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1622 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1623 val = default_conversion (val);
1625 val = require_complete_type (val);
1629 /* Formal parm type is specified by a function prototype. */
1632 if (TYPE_SIZE (type) == 0)
1634 error ("type of formal parameter %d is incomplete", parmnum + 1);
1639 /* Optionally warn about conversions that
1640 differ from the default conversions. */
1641 if (warn_conversion)
1643 int formal_prec = TYPE_PRECISION (type);
1645 if (INTEGRAL_TYPE_P (type)
1646 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1647 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1648 else if (TREE_CODE (type) == COMPLEX_TYPE
1649 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1650 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1651 else if (TREE_CODE (type) == REAL_TYPE
1652 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1653 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1654 else if (TREE_CODE (type) == REAL_TYPE
1655 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1656 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1657 /* ??? At some point, messages should be written about
1658 conversions between complex types, but that's too messy
1660 else if (TREE_CODE (type) == REAL_TYPE
1661 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1663 /* Warn if any argument is passed as `float',
1664 since without a prototype it would be `double'. */
1665 if (formal_prec == TYPE_PRECISION (float_type_node))
1666 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1668 /* Detect integer changing in width or signedness. */
1669 else if (INTEGRAL_TYPE_P (type)
1670 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1672 tree would_have_been = default_conversion (val);
1673 tree type1 = TREE_TYPE (would_have_been);
1675 if (TREE_CODE (type) == ENUMERAL_TYPE
1676 && type == TREE_TYPE (val))
1677 /* No warning if function asks for enum
1678 and the actual arg is that enum type. */
1680 else if (formal_prec != TYPE_PRECISION (type1))
1681 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1682 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1684 /* Don't complain if the formal parameter type
1685 is an enum, because we can't tell now whether
1686 the value was an enum--even the same enum. */
1687 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1689 else if (TREE_CODE (val) == INTEGER_CST
1690 && int_fits_type_p (val, type))
1691 /* Change in signedness doesn't matter
1692 if a constant value is unaffected. */
1694 /* Likewise for a constant in a NOP_EXPR. */
1695 else if (TREE_CODE (val) == NOP_EXPR
1696 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1697 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1699 #if 0 /* We never get such tree structure here. */
1700 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1701 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1702 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1703 /* Change in signedness doesn't matter
1704 if an enum value is unaffected. */
1707 /* If the value is extended from a narrower
1708 unsigned type, it doesn't matter whether we
1709 pass it as signed or unsigned; the value
1710 certainly is the same either way. */
1711 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1712 && TREE_UNSIGNED (TREE_TYPE (val)))
1714 else if (TREE_UNSIGNED (type))
1715 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1717 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1721 parmval = convert_for_assignment (type, val,
1722 (char *)0, /* arg passing */
1723 fundecl, name, parmnum + 1);
1725 #ifdef PROMOTE_PROTOTYPES
1726 if ((TREE_CODE (type) == INTEGER_TYPE
1727 || TREE_CODE (type) == ENUMERAL_TYPE)
1728 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1729 parmval = default_conversion (parmval);
1732 result = tree_cons (NULL_TREE, parmval, result);
1734 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1735 && (TYPE_PRECISION (TREE_TYPE (val))
1736 < TYPE_PRECISION (double_type_node)))
1737 /* Convert `float' to `double'. */
1738 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1740 /* Convert `short' and `char' to full-size `int'. */
1741 result = tree_cons (NULL_TREE, default_conversion (val), result);
1744 typetail = TREE_CHAIN (typetail);
1747 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1750 error ("too few arguments to function `%s'",
1751 IDENTIFIER_POINTER (name));
1753 error ("too few arguments to function");
1756 return nreverse (result);
1759 /* This is the entry point used by the parser
1760 for binary operators in the input.
1761 In addition to constructing the expression,
1762 we check for operands that were written with other binary operators
1763 in a way that is likely to confuse the user. */
1766 parser_build_binary_op (code, arg1, arg2)
1767 enum tree_code code;
1770 tree result = build_binary_op (code, arg1, arg2, 1);
1773 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1774 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1775 enum tree_code code1 = ERROR_MARK;
1776 enum tree_code code2 = ERROR_MARK;
1778 if (class1 == 'e' || class1 == '1'
1779 || class1 == '2' || class1 == '<')
1780 code1 = C_EXP_ORIGINAL_CODE (arg1);
1781 if (class2 == 'e' || class2 == '1'
1782 || class2 == '2' || class2 == '<')
1783 code2 = C_EXP_ORIGINAL_CODE (arg2);
1785 /* Check for cases such as x+y<<z which users are likely
1786 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1787 is cleared to prevent these warnings. */
1788 if (warn_parentheses)
1790 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1792 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1793 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1794 warning ("suggest parentheses around + or - inside shift");
1797 if (code == TRUTH_ORIF_EXPR)
1799 if (code1 == TRUTH_ANDIF_EXPR
1800 || code2 == TRUTH_ANDIF_EXPR)
1801 warning ("suggest parentheses around && within ||");
1804 if (code == BIT_IOR_EXPR)
1806 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1807 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1808 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1809 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1810 warning ("suggest parentheses around arithmetic in operand of |");
1813 if (code == BIT_XOR_EXPR)
1815 if (code1 == BIT_AND_EXPR
1816 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1817 || code2 == BIT_AND_EXPR
1818 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1819 warning ("suggest parentheses around arithmetic in operand of ^");
1822 if (code == BIT_AND_EXPR)
1824 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1825 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1826 warning ("suggest parentheses around + or - in operand of &");
1830 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1831 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1832 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1833 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1835 unsigned_conversion_warning (result, arg1);
1836 unsigned_conversion_warning (result, arg2);
1837 overflow_warning (result);
1839 class = TREE_CODE_CLASS (TREE_CODE (result));
1841 /* Record the code that was specified in the source,
1842 for the sake of warnings about confusing nesting. */
1843 if (class == 'e' || class == '1'
1844 || class == '2' || class == '<')
1845 C_SET_EXP_ORIGINAL_CODE (result, code);
1848 int flag = TREE_CONSTANT (result);
1849 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1850 so that convert_for_assignment wouldn't strip it.
1851 That way, we got warnings for things like p = (1 - 1).
1852 But it turns out we should not get those warnings. */
1853 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1854 C_SET_EXP_ORIGINAL_CODE (result, code);
1855 TREE_CONSTANT (result) = flag;
1861 /* Build a binary-operation expression without default conversions.
1862 CODE is the kind of expression to build.
1863 This function differs from `build' in several ways:
1864 the data type of the result is computed and recorded in it,
1865 warnings are generated if arg data types are invalid,
1866 special handling for addition and subtraction of pointers is known,
1867 and some optimization is done (operations on narrow ints
1868 are done in the narrower type when that gives the same result).
1869 Constant folding is also done before the result is returned.
1871 Note that the operands will never have enumeral types, or function
1872 or array types, because either they will have the default conversions
1873 performed or they have both just been converted to some other type in which
1874 the arithmetic is to be done. */
1877 build_binary_op (code, orig_op0, orig_op1, convert_p)
1878 enum tree_code code;
1879 tree orig_op0, orig_op1;
1883 register enum tree_code code0, code1;
1886 /* Expression code to give to the expression when it is built.
1887 Normally this is CODE, which is what the caller asked for,
1888 but in some special cases we change it. */
1889 register enum tree_code resultcode = code;
1891 /* Data type in which the computation is to be performed.
1892 In the simplest cases this is the common type of the arguments. */
1893 register tree result_type = NULL;
1895 /* Nonzero means operands have already been type-converted
1896 in whatever way is necessary.
1897 Zero means they need to be converted to RESULT_TYPE. */
1900 /* Nonzero means create the expression with this type, rather than
1902 tree build_type = 0;
1904 /* Nonzero means after finally constructing the expression
1905 convert it to this type. */
1906 tree final_type = 0;
1908 /* Nonzero if this is an operation like MIN or MAX which can
1909 safely be computed in short if both args are promoted shorts.
1910 Also implies COMMON.
1911 -1 indicates a bitwise operation; this makes a difference
1912 in the exact conditions for when it is safe to do the operation
1913 in a narrower mode. */
1916 /* Nonzero if this is a comparison operation;
1917 if both args are promoted shorts, compare the original shorts.
1918 Also implies COMMON. */
1919 int short_compare = 0;
1921 /* Nonzero if this is a right-shift operation, which can be computed on the
1922 original short and then promoted if the operand is a promoted short. */
1923 int short_shift = 0;
1925 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1930 op0 = default_conversion (orig_op0);
1931 op1 = default_conversion (orig_op1);
1939 type0 = TREE_TYPE (op0);
1940 type1 = TREE_TYPE (op1);
1942 /* The expression codes of the data types of the arguments tell us
1943 whether the arguments are integers, floating, pointers, etc. */
1944 code0 = TREE_CODE (type0);
1945 code1 = TREE_CODE (type1);
1947 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1948 STRIP_TYPE_NOPS (op0);
1949 STRIP_TYPE_NOPS (op1);
1951 /* If an error was already reported for one of the arguments,
1952 avoid reporting another error. */
1954 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1955 return error_mark_node;
1960 /* Handle the pointer + int case. */
1961 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1962 return pointer_int_sum (PLUS_EXPR, op0, op1);
1963 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1964 return pointer_int_sum (PLUS_EXPR, op1, op0);
1970 /* Subtraction of two similar pointers.
1971 We must subtract them as integers, then divide by object size. */
1972 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1973 && comp_target_types (type0, type1))
1974 return pointer_diff (op0, op1);
1975 /* Handle pointer minus int. Just like pointer plus int. */
1976 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1977 return pointer_int_sum (MINUS_EXPR, op0, op1);
1986 case TRUNC_DIV_EXPR:
1988 case FLOOR_DIV_EXPR:
1989 case ROUND_DIV_EXPR:
1990 case EXACT_DIV_EXPR:
1991 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1992 || code0 == COMPLEX_TYPE)
1993 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1994 || code1 == COMPLEX_TYPE))
1996 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
1997 resultcode = RDIV_EXPR;
2000 /* Although it would be tempting to shorten always here, that
2001 loses on some targets, since the modulo instruction is
2002 undefined if the quotient can't be represented in the
2003 computation mode. We shorten only if unsigned or if
2004 dividing by something we know != -1. */
2005 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2006 || (TREE_CODE (op1) == INTEGER_CST
2007 && (TREE_INT_CST_LOW (op1) != -1
2008 || TREE_INT_CST_HIGH (op1) != -1)));
2015 case BIT_ANDTC_EXPR:
2018 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2020 /* If one operand is a constant, and the other is a short type
2021 that has been converted to an int,
2022 really do the work in the short type and then convert the
2023 result to int. If we are lucky, the constant will be 0 or 1
2024 in the short type, making the entire operation go away. */
2025 if (TREE_CODE (op0) == INTEGER_CST
2026 && TREE_CODE (op1) == NOP_EXPR
2027 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2028 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2030 final_type = result_type;
2031 op1 = TREE_OPERAND (op1, 0);
2032 result_type = TREE_TYPE (op1);
2034 if (TREE_CODE (op1) == INTEGER_CST
2035 && TREE_CODE (op0) == NOP_EXPR
2036 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2037 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2039 final_type = result_type;
2040 op0 = TREE_OPERAND (op0, 0);
2041 result_type = TREE_TYPE (op0);
2045 case TRUNC_MOD_EXPR:
2046 case FLOOR_MOD_EXPR:
2047 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2049 /* Although it would be tempting to shorten always here, that loses
2050 on some targets, since the modulo instruction is undefined if the
2051 quotient can't be represented in the computation mode. We shorten
2052 only if unsigned or if dividing by something we know != -1. */
2053 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2054 || (TREE_CODE (op1) == INTEGER_CST
2055 && (TREE_INT_CST_LOW (op1) != -1
2056 || TREE_INT_CST_HIGH (op1) != -1)));
2061 case TRUTH_ANDIF_EXPR:
2062 case TRUTH_ORIF_EXPR:
2063 case TRUTH_AND_EXPR:
2065 case TRUTH_XOR_EXPR:
2066 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2067 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2068 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2069 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2071 /* Result of these operations is always an int,
2072 but that does not mean the operands should be
2073 converted to ints! */
2074 result_type = integer_type_node;
2075 op0 = truthvalue_conversion (op0);
2076 op1 = truthvalue_conversion (op1);
2081 /* Shift operations: result has same type as first operand;
2082 always convert second operand to int.
2083 Also set SHORT_SHIFT if shifting rightward. */
2086 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2088 if (TREE_CODE (op1) == INTEGER_CST)
2090 if (tree_int_cst_sgn (op1) < 0)
2091 warning ("right shift count is negative");
2094 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2096 if (TREE_INT_CST_HIGH (op1) != 0
2097 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2098 >= TYPE_PRECISION (type0)))
2099 warning ("right shift count >= width of type");
2102 /* Use the type of the value to be shifted.
2103 This is what most traditional C compilers do. */
2104 result_type = type0;
2105 /* Unless traditional, convert the shift-count to an integer,
2106 regardless of size of value being shifted. */
2107 if (! flag_traditional)
2109 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2110 op1 = convert (integer_type_node, op1);
2111 /* Avoid converting op1 to result_type later. */
2118 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2120 if (TREE_CODE (op1) == INTEGER_CST)
2122 if (tree_int_cst_sgn (op1) < 0)
2123 warning ("left shift count is negative");
2124 else if (TREE_INT_CST_HIGH (op1) != 0
2125 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2126 >= TYPE_PRECISION (type0)))
2127 warning ("left shift count >= width of type");
2129 /* Use the type of the value to be shifted.
2130 This is what most traditional C compilers do. */
2131 result_type = type0;
2132 /* Unless traditional, convert the shift-count to an integer,
2133 regardless of size of value being shifted. */
2134 if (! flag_traditional)
2136 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2137 op1 = convert (integer_type_node, op1);
2138 /* Avoid converting op1 to result_type later. */
2146 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2148 if (TREE_CODE (op1) == INTEGER_CST)
2150 if (tree_int_cst_sgn (op1) < 0)
2151 warning ("shift count is negative");
2152 else if (TREE_INT_CST_HIGH (op1) != 0
2153 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2154 >= TYPE_PRECISION (type0)))
2155 warning ("shift count >= width of type");
2157 /* Use the type of the value to be shifted.
2158 This is what most traditional C compilers do. */
2159 result_type = type0;
2160 /* Unless traditional, convert the shift-count to an integer,
2161 regardless of size of value being shifted. */
2162 if (! flag_traditional)
2164 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2165 op1 = convert (integer_type_node, op1);
2166 /* Avoid converting op1 to result_type later. */
2174 /* Result of comparison is always int,
2175 but don't convert the args to int! */
2176 build_type = integer_type_node;
2177 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2178 || code0 == COMPLEX_TYPE)
2179 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2180 || code1 == COMPLEX_TYPE))
2182 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2184 register tree tt0 = TREE_TYPE (type0);
2185 register tree tt1 = TREE_TYPE (type1);
2186 /* Anything compares with void *. void * compares with anything.
2187 Otherwise, the targets must be compatible
2188 and both must be object or both incomplete. */
2189 if (comp_target_types (type0, type1))
2191 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2193 /* op0 != orig_op0 detects the case of something
2194 whose value is 0 but which isn't a valid null ptr const. */
2195 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2196 && TREE_CODE (tt1) == FUNCTION_TYPE)
2197 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2199 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2201 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2202 && TREE_CODE (tt0) == FUNCTION_TYPE)
2203 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2206 pedwarn ("comparison of distinct pointer types lacks a cast");
2207 result_type = common_type (type0, type1);
2209 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2210 && integer_zerop (op1))
2211 result_type = type0;
2212 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2213 && integer_zerop (op0))
2214 result_type = type1;
2215 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2217 result_type = type0;
2218 if (! flag_traditional)
2219 pedwarn ("comparison between pointer and integer");
2221 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2223 result_type = type1;
2224 if (! flag_traditional)
2225 pedwarn ("comparison between pointer and integer");
2231 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2232 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2234 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2236 if (! comp_target_types (type0, type1))
2237 pedwarn ("comparison of distinct pointer types lacks a cast");
2239 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2240 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2241 result_type = common_type (type0, type1);
2249 build_type = integer_type_node;
2250 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2251 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2253 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2255 if (! comp_target_types (type0, type1))
2256 pedwarn ("comparison of distinct pointer types lacks a cast");
2257 else if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2258 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2259 pedwarn ("comparison of complete and incomplete pointers");
2261 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2262 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2263 result_type = common_type (type0, type1);
2265 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2266 && integer_zerop (op1))
2268 result_type = type0;
2270 pedwarn ("ordered comparison of pointer with integer zero");
2272 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2273 && integer_zerop (op0))
2275 result_type = type1;
2277 pedwarn ("ordered comparison of pointer with integer zero");
2279 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2281 result_type = type0;
2282 if (! flag_traditional)
2283 pedwarn ("comparison between pointer and integer");
2285 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2287 result_type = type1;
2288 if (! flag_traditional)
2289 pedwarn ("comparison between pointer and integer");
2294 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2296 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2298 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2300 if (shorten || common || short_compare)
2301 result_type = common_type (type0, type1);
2303 /* For certain operations (which identify themselves by shorten != 0)
2304 if both args were extended from the same smaller type,
2305 do the arithmetic in that type and then extend.
2307 shorten !=0 and !=1 indicates a bitwise operation.
2308 For them, this optimization is safe only if
2309 both args are zero-extended or both are sign-extended.
2310 Otherwise, we might change the result.
2311 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2312 but calculated in (unsigned short) it would be (unsigned short)-1. */
2314 if (shorten && none_complex)
2316 int unsigned0, unsigned1;
2317 tree arg0 = get_narrower (op0, &unsigned0);
2318 tree arg1 = get_narrower (op1, &unsigned1);
2319 /* UNS is 1 if the operation to be done is an unsigned one. */
2320 int uns = TREE_UNSIGNED (result_type);
2323 final_type = result_type;
2325 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2326 but it *requires* conversion to FINAL_TYPE. */
2328 if ((TYPE_PRECISION (TREE_TYPE (op0))
2329 == TYPE_PRECISION (TREE_TYPE (arg0)))
2330 && TREE_TYPE (op0) != final_type)
2331 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2332 if ((TYPE_PRECISION (TREE_TYPE (op1))
2333 == TYPE_PRECISION (TREE_TYPE (arg1)))
2334 && TREE_TYPE (op1) != final_type)
2335 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2337 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2339 /* For bitwise operations, signedness of nominal type
2340 does not matter. Consider only how operands were extended. */
2344 /* Note that in all three cases below we refrain from optimizing
2345 an unsigned operation on sign-extended args.
2346 That would not be valid. */
2348 /* Both args variable: if both extended in same way
2349 from same width, do it in that width.
2350 Do it unsigned if args were zero-extended. */
2351 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2352 < TYPE_PRECISION (result_type))
2353 && (TYPE_PRECISION (TREE_TYPE (arg1))
2354 == TYPE_PRECISION (TREE_TYPE (arg0)))
2355 && unsigned0 == unsigned1
2356 && (unsigned0 || !uns))
2358 = signed_or_unsigned_type (unsigned0,
2359 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2360 else if (TREE_CODE (arg0) == INTEGER_CST
2361 && (unsigned1 || !uns)
2362 && (TYPE_PRECISION (TREE_TYPE (arg1))
2363 < TYPE_PRECISION (result_type))
2364 && (type = signed_or_unsigned_type (unsigned1,
2366 int_fits_type_p (arg0, type)))
2368 else if (TREE_CODE (arg1) == INTEGER_CST
2369 && (unsigned0 || !uns)
2370 && (TYPE_PRECISION (TREE_TYPE (arg0))
2371 < TYPE_PRECISION (result_type))
2372 && (type = signed_or_unsigned_type (unsigned0,
2374 int_fits_type_p (arg1, type)))
2378 /* Shifts can be shortened if shifting right. */
2383 tree arg0 = get_narrower (op0, &unsigned_arg);
2385 final_type = result_type;
2387 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2388 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2390 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2391 /* We can shorten only if the shift count is less than the
2392 number of bits in the smaller type size. */
2393 && TREE_INT_CST_HIGH (op1) == 0
2394 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
2395 /* If arg is sign-extended and then unsigned-shifted,
2396 we can simulate this with a signed shift in arg's type
2397 only if the extended result is at least twice as wide
2398 as the arg. Otherwise, the shift could use up all the
2399 ones made by sign-extension and bring in zeros.
2400 We can't optimize that case at all, but in most machines
2401 it never happens because available widths are 2**N. */
2402 && (!TREE_UNSIGNED (final_type)
2404 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2406 /* Do an unsigned shift if the operand was zero-extended. */
2408 = signed_or_unsigned_type (unsigned_arg,
2410 /* Convert value-to-be-shifted to that type. */
2411 if (TREE_TYPE (op0) != result_type)
2412 op0 = convert (result_type, op0);
2417 /* Comparison operations are shortened too but differently.
2418 They identify themselves by setting short_compare = 1. */
2422 /* Don't write &op0, etc., because that would prevent op0
2423 from being kept in a register.
2424 Instead, make copies of the our local variables and
2425 pass the copies by reference, then copy them back afterward. */
2426 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2427 enum tree_code xresultcode = resultcode;
2429 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2432 op0 = xop0, op1 = xop1;
2434 resultcode = xresultcode;
2438 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2439 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2441 /* Avoid spurious warnings for comparison with enumerators. */
2445 STRIP_TYPE_NOPS (xop0);
2446 STRIP_TYPE_NOPS (xop1);
2448 /* Give warnings for comparisons between signed and unsigned
2449 quantities that may fail. */
2450 /* Do the checking based on the original operand trees, so that
2451 casts will be considered, but default promotions won't be. */
2453 /* Do not warn if the comparison is being done in a signed type,
2454 since the signed type will only be chosen if it can represent
2455 all the values of the unsigned type. */
2456 if (! TREE_UNSIGNED (result_type))
2458 /* Do not warn if both operands are unsigned. */
2459 else if (op0_signed == op1_signed)
2461 /* Do not warn if the signed quantity is an unsuffixed
2462 integer literal (or some static constant expression
2463 involving such literals) and it is non-negative. */
2464 else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST
2465 && tree_int_cst_sgn (xop0) >= 0)
2466 || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
2467 && tree_int_cst_sgn (xop1) >= 0))
2469 /* Do not warn if the comparison is an equality operation,
2470 the unsigned quantity is an integral constant and it does
2471 not use the most significant bit of result_type. */
2472 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
2473 && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST
2474 && int_fits_type_p (xop1, signed_type (result_type))
2475 || (op1_signed && TREE_CODE (xop0) == INTEGER_CST
2476 && int_fits_type_p (xop0, signed_type (result_type))))))
2479 warning ("comparison between signed and unsigned");
2484 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2485 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2486 Then the expression will be built.
2487 It will be given type FINAL_TYPE if that is nonzero;
2488 otherwise, it will be given type RESULT_TYPE. */
2492 binary_op_error (code);
2493 return error_mark_node;
2498 if (TREE_TYPE (op0) != result_type)
2499 op0 = convert (result_type, op0);
2500 if (TREE_TYPE (op1) != result_type)
2501 op1 = convert (result_type, op1);
2504 if (build_type == NULL_TREE)
2505 build_type = result_type;
2508 register tree result = build (resultcode, build_type, op0, op1);
2509 register tree folded;
2511 folded = fold (result);
2512 if (folded == result)
2513 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2514 if (final_type != 0)
2515 return convert (final_type, folded);
2520 /* Return a tree for the sum or difference (RESULTCODE says which)
2521 of pointer PTROP and integer INTOP. */
2524 pointer_int_sum (resultcode, ptrop, intop)
2525 enum tree_code resultcode;
2526 register tree ptrop, intop;
2530 register tree result;
2531 register tree folded;
2533 /* The result is a pointer of the same type that is being added. */
2535 register tree result_type = TREE_TYPE (ptrop);
2537 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2539 if (pedantic || warn_pointer_arith)
2540 pedwarn ("pointer of type `void *' used in arithmetic");
2541 size_exp = integer_one_node;
2543 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2545 if (pedantic || warn_pointer_arith)
2546 pedwarn ("pointer to a function used in arithmetic");
2547 size_exp = integer_one_node;
2550 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2552 /* If what we are about to multiply by the size of the elements
2553 contains a constant term, apply distributive law
2554 and multiply that constant term separately.
2555 This helps produce common subexpressions. */
2557 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2558 && ! TREE_CONSTANT (intop)
2559 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2560 && TREE_CONSTANT (size_exp)
2561 /* If the constant comes from pointer subtraction,
2562 skip this optimization--it would cause an error. */
2563 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE)
2565 enum tree_code subcode = resultcode;
2566 tree int_type = TREE_TYPE (intop);
2567 if (TREE_CODE (intop) == MINUS_EXPR)
2568 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2569 /* Convert both subexpression types to the type of intop,
2570 because weird cases involving pointer arithmetic
2571 can result in a sum or difference with different type args. */
2572 ptrop = build_binary_op (subcode, ptrop,
2573 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2574 intop = convert (int_type, TREE_OPERAND (intop, 0));
2577 /* Convert the integer argument to a type the same size as a pointer
2578 so the multiply won't overflow spuriously. */
2580 if (TYPE_PRECISION (TREE_TYPE (intop)) != POINTER_SIZE)
2581 intop = convert (type_for_size (POINTER_SIZE, 0), intop);
2583 /* Replace the integer argument with a suitable product by the object size.
2584 Do this multiplication as signed, then convert to the appropriate
2585 pointer type (actually unsigned integral). */
2587 intop = convert (result_type,
2588 build_binary_op (MULT_EXPR, intop,
2589 convert (TREE_TYPE (intop), size_exp), 1));
2591 /* Create the sum or difference. */
2593 result = build (resultcode, result_type, ptrop, intop);
2595 folded = fold (result);
2596 if (folded == result)
2597 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2601 /* Return a tree for the difference of pointers OP0 and OP1.
2602 The resulting tree has type int. */
2605 pointer_diff (op0, op1)
2606 register tree op0, op1;
2608 register tree result, folded;
2609 tree restype = ptrdiff_type_node;
2611 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2613 if (pedantic || warn_pointer_arith)
2615 if (TREE_CODE (target_type) == VOID_TYPE)
2616 pedwarn ("pointer of type `void *' used in subtraction");
2617 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2618 pedwarn ("pointer to a function used in subtraction");
2621 /* First do the subtraction as integers;
2622 then drop through to build the divide operator. */
2624 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2625 convert (restype, op1), 1);
2626 /* This generates an error if op1 is pointer to incomplete type. */
2627 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2628 error ("arithmetic on pointer to an incomplete type");
2630 /* This generates an error if op0 is pointer to incomplete type. */
2631 op1 = c_size_in_bytes (target_type);
2633 /* Divide by the size, in easiest possible way. */
2635 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2637 folded = fold (result);
2638 if (folded == result)
2639 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2643 /* Construct and perhaps optimize a tree representation
2644 for a unary operation. CODE, a tree_code, specifies the operation
2645 and XARG is the operand. NOCONVERT nonzero suppresses
2646 the default promotions (such as from short to int). */
2649 build_unary_op (code, xarg, noconvert)
2650 enum tree_code code;
2654 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2655 register tree arg = xarg;
2656 register tree argtype = 0;
2657 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2658 char *errstring = NULL;
2661 if (typecode == ERROR_MARK)
2662 return error_mark_node;
2663 if (typecode == ENUMERAL_TYPE)
2664 typecode = INTEGER_TYPE;
2669 /* This is used for unary plus, because a CONVERT_EXPR
2670 is enough to prevent anybody from looking inside for
2671 associativity, but won't generate any code. */
2672 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2673 || typecode == COMPLEX_TYPE))
2674 errstring = "wrong type argument to unary plus";
2675 else if (!noconvert)
2676 arg = default_conversion (arg);
2680 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2681 || typecode == COMPLEX_TYPE))
2682 errstring = "wrong type argument to unary minus";
2683 else if (!noconvert)
2684 arg = default_conversion (arg);
2688 if (typecode == COMPLEX_TYPE)
2692 arg = default_conversion (arg);
2694 else if (typecode != INTEGER_TYPE)
2695 errstring = "wrong type argument to bit-complement";
2696 else if (!noconvert)
2697 arg = default_conversion (arg);
2701 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2702 || typecode == COMPLEX_TYPE))
2703 errstring = "wrong type argument to abs";
2704 else if (!noconvert)
2705 arg = default_conversion (arg);
2709 /* Conjugating a real value is a no-op, but allow it anyway. */
2710 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2711 || typecode == COMPLEX_TYPE))
2712 errstring = "wrong type argument to conjugation";
2713 else if (!noconvert)
2714 arg = default_conversion (arg);
2717 case TRUTH_NOT_EXPR:
2718 if (typecode != INTEGER_TYPE
2719 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2720 && typecode != COMPLEX_TYPE
2721 /* These will convert to a pointer. */
2722 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2724 errstring = "wrong type argument to unary exclamation mark";
2727 arg = truthvalue_conversion (arg);
2728 return invert_truthvalue (arg);
2734 if (TREE_CODE (arg) == COMPLEX_CST)
2735 return TREE_REALPART (arg);
2736 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2737 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2742 if (TREE_CODE (arg) == COMPLEX_CST)
2743 return TREE_IMAGPART (arg);
2744 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2745 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2747 return convert (TREE_TYPE (arg), integer_zero_node);
2749 case PREINCREMENT_EXPR:
2750 case POSTINCREMENT_EXPR:
2751 case PREDECREMENT_EXPR:
2752 case POSTDECREMENT_EXPR:
2753 /* Handle complex lvalues (when permitted)
2754 by reduction to simpler cases. */
2756 val = unary_complex_lvalue (code, arg);
2760 /* Increment or decrement the real part of the value,
2761 and don't change the imaginary part. */
2762 if (typecode == COMPLEX_TYPE)
2766 arg = stabilize_reference (arg);
2767 real = build_unary_op (REALPART_EXPR, arg, 1);
2768 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2769 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2770 build_unary_op (code, real, 1), imag);
2773 /* Report invalid types. */
2775 if (typecode != POINTER_TYPE
2776 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2778 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2779 errstring ="wrong type argument to increment";
2781 errstring ="wrong type argument to decrement";
2787 tree result_type = TREE_TYPE (arg);
2789 arg = get_unwidened (arg, 0);
2790 argtype = TREE_TYPE (arg);
2792 /* Compute the increment. */
2794 if (typecode == POINTER_TYPE)
2796 /* If pointer target is an undefined struct,
2797 we just cannot know how to do the arithmetic. */
2798 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2799 error ("%s of pointer to unknown structure",
2800 ((code == PREINCREMENT_EXPR
2801 || code == POSTINCREMENT_EXPR)
2802 ? "increment" : "decrement"));
2803 else if ((pedantic || warn_pointer_arith)
2804 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2805 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2806 pedwarn ("wrong type argument to %s",
2807 ((code == PREINCREMENT_EXPR
2808 || code == POSTINCREMENT_EXPR)
2809 ? "increment" : "decrement"));
2810 inc = c_size_in_bytes (TREE_TYPE (result_type));
2813 inc = integer_one_node;
2815 inc = convert (argtype, inc);
2817 /* Handle incrementing a cast-expression. */
2820 switch (TREE_CODE (arg))
2825 case FIX_TRUNC_EXPR:
2826 case FIX_FLOOR_EXPR:
2827 case FIX_ROUND_EXPR:
2829 pedantic_lvalue_warning (CONVERT_EXPR);
2830 /* If the real type has the same machine representation
2831 as the type it is cast to, we can make better output
2832 by adding directly to the inside of the cast. */
2833 if ((TREE_CODE (TREE_TYPE (arg))
2834 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2835 && (TYPE_MODE (TREE_TYPE (arg))
2836 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2837 arg = TREE_OPERAND (arg, 0);
2840 tree incremented, modify, value;
2841 arg = stabilize_reference (arg);
2842 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2845 value = save_expr (arg);
2846 incremented = build (((code == PREINCREMENT_EXPR
2847 || code == POSTINCREMENT_EXPR)
2848 ? PLUS_EXPR : MINUS_EXPR),
2849 argtype, value, inc);
2850 TREE_SIDE_EFFECTS (incremented) = 1;
2851 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2852 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2853 TREE_USED (value) = 1;
2863 /* Complain about anything else that is not a true lvalue. */
2864 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2865 || code == POSTINCREMENT_EXPR)
2866 ? "increment" : "decrement")))
2867 return error_mark_node;
2869 /* Report a read-only lvalue. */
2870 if (TREE_READONLY (arg))
2871 readonly_warning (arg,
2872 ((code == PREINCREMENT_EXPR
2873 || code == POSTINCREMENT_EXPR)
2874 ? "increment" : "decrement"));
2876 val = build (code, TREE_TYPE (arg), arg, inc);
2877 TREE_SIDE_EFFECTS (val) = 1;
2878 val = convert (result_type, val);
2879 if (TREE_CODE (val) != code)
2880 TREE_NO_UNUSED_WARNING (val) = 1;
2885 /* Note that this operation never does default_conversion
2886 regardless of NOCONVERT. */
2888 /* Let &* cancel out to simplify resulting code. */
2889 if (TREE_CODE (arg) == INDIRECT_REF)
2891 /* Don't let this be an lvalue. */
2892 if (lvalue_p (TREE_OPERAND (arg, 0)))
2893 return non_lvalue (TREE_OPERAND (arg, 0));
2894 return TREE_OPERAND (arg, 0);
2897 /* For &x[y], return x+y */
2898 if (TREE_CODE (arg) == ARRAY_REF)
2900 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
2901 return error_mark_node;
2902 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2903 TREE_OPERAND (arg, 1), 1);
2906 /* Handle complex lvalues (when permitted)
2907 by reduction to simpler cases. */
2908 val = unary_complex_lvalue (code, arg);
2912 #if 0 /* Turned off because inconsistent;
2913 float f; *&(int)f = 3.4 stores in int format
2914 whereas (int)f = 3.4 stores in float format. */
2915 /* Address of a cast is just a cast of the address
2916 of the operand of the cast. */
2917 switch (TREE_CODE (arg))
2922 case FIX_TRUNC_EXPR:
2923 case FIX_FLOOR_EXPR:
2924 case FIX_ROUND_EXPR:
2927 pedwarn ("ANSI C forbids the address of a cast expression");
2928 return convert (build_pointer_type (TREE_TYPE (arg)),
2929 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
2934 /* Allow the address of a constructor if all the elements
2936 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
2938 /* Anything not already handled and not a true memory reference
2940 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
2941 return error_mark_node;
2943 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2944 argtype = TREE_TYPE (arg);
2945 /* If the lvalue is const or volatile,
2946 merge that into the type that the address will point to. */
2947 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
2948 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2950 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
2951 argtype = c_build_type_variant (argtype,
2952 TREE_READONLY (arg),
2953 TREE_THIS_VOLATILE (arg));
2956 argtype = build_pointer_type (argtype);
2958 if (mark_addressable (arg) == 0)
2959 return error_mark_node;
2964 if (TREE_CODE (arg) == COMPONENT_REF)
2966 tree field = TREE_OPERAND (arg, 1);
2968 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
2970 if (DECL_BIT_FIELD (field))
2972 error ("attempt to take address of bit-field structure member `%s'",
2973 IDENTIFIER_POINTER (DECL_NAME (field)));
2974 return error_mark_node;
2977 addr = convert (argtype, addr);
2979 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
2982 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
2983 size_int (BITS_PER_UNIT));
2984 int flag = TREE_CONSTANT (addr);
2985 addr = fold (build (PLUS_EXPR, argtype,
2986 addr, convert (argtype, offset)));
2987 TREE_CONSTANT (addr) = flag;
2991 addr = build1 (code, argtype, arg);
2993 /* Address of a static or external variable or
2994 file-scope function counts as a constant. */
2996 && ! (TREE_CODE (arg) == FUNCTION_DECL
2997 && DECL_CONTEXT (arg) != 0))
2998 TREE_CONSTANT (addr) = 1;
3006 argtype = TREE_TYPE (arg);
3007 return fold (build1 (code, argtype, arg));
3011 return error_mark_node;
3015 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3016 convert ARG with the same conversions in the same order
3017 and return the result. */
3020 convert_sequence (conversions, arg)
3024 switch (TREE_CODE (conversions))
3029 case FIX_TRUNC_EXPR:
3030 case FIX_FLOOR_EXPR:
3031 case FIX_ROUND_EXPR:
3033 return convert (TREE_TYPE (conversions),
3034 convert_sequence (TREE_OPERAND (conversions, 0),
3043 /* Return nonzero if REF is an lvalue valid for this language.
3044 Lvalues can be assigned, unless their type has TYPE_READONLY.
3045 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3051 register enum tree_code code = TREE_CODE (ref);
3058 return lvalue_p (TREE_OPERAND (ref, 0));
3069 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3070 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
3077 /* Return nonzero if REF is an lvalue valid for this language;
3078 otherwise, print an error message and return zero. */
3081 lvalue_or_else (ref, string)
3085 int win = lvalue_p (ref);
3087 error ("invalid lvalue in %s", string);
3091 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3092 for certain kinds of expressions which are not really lvalues
3093 but which we can accept as lvalues.
3095 If ARG is not a kind of expression we can handle, return zero. */
3098 unary_complex_lvalue (code, arg)
3099 enum tree_code code;
3102 /* Handle (a, b) used as an "lvalue". */
3103 if (TREE_CODE (arg) == COMPOUND_EXPR)
3105 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3106 pedantic_lvalue_warning (COMPOUND_EXPR);
3107 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3108 TREE_OPERAND (arg, 0), real_result);
3111 /* Handle (a ? b : c) used as an "lvalue". */
3112 if (TREE_CODE (arg) == COND_EXPR)
3114 pedantic_lvalue_warning (COND_EXPR);
3115 return (build_conditional_expr
3116 (TREE_OPERAND (arg, 0),
3117 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3118 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3124 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3125 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3128 pedantic_lvalue_warning (code)
3129 enum tree_code code;
3132 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3133 code == COND_EXPR ? "conditional"
3134 : code == COMPOUND_EXPR ? "compound" : "cast");
3137 /* Warn about storing in something that is `const'. */
3140 readonly_warning (arg, string)
3145 strcpy (buf, string);
3147 /* Forbid assignments to iterators. */
3148 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3150 strcat (buf, " of iterator `%s'");
3151 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3154 if (TREE_CODE (arg) == COMPONENT_REF)
3156 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3157 readonly_warning (TREE_OPERAND (arg, 0), string);
3160 strcat (buf, " of read-only member `%s'");
3161 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3164 else if (TREE_CODE (arg) == VAR_DECL)
3166 strcat (buf, " of read-only variable `%s'");
3167 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3171 pedwarn ("%s of read-only location", buf);
3175 /* Mark EXP saying that we need to be able to take the
3176 address of it; it should not be allocated in a register.
3177 Value is 1 if successful. */
3180 mark_addressable (exp)
3183 register tree x = exp;
3185 switch (TREE_CODE (x))
3192 x = TREE_OPERAND (x, 0);
3196 TREE_ADDRESSABLE (x) = 1;
3203 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3204 && DECL_NONLOCAL (x))
3206 if (TREE_PUBLIC (x))
3208 error ("global register variable `%s' used in nested function",
3209 IDENTIFIER_POINTER (DECL_NAME (x)));
3212 pedwarn ("register variable `%s' used in nested function",
3213 IDENTIFIER_POINTER (DECL_NAME (x)));
3215 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3217 if (TREE_PUBLIC (x))
3219 error ("address of global register variable `%s' requested",
3220 IDENTIFIER_POINTER (DECL_NAME (x)));
3224 /* If we are making this addressable due to its having
3225 volatile components, give a different error message. Also
3226 handle the case of an unnamed parameter by not trying
3227 to give the name. */
3229 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3231 error ("cannot put object with volatile field into register");
3235 pedwarn ("address of register variable `%s' requested",
3236 IDENTIFIER_POINTER (DECL_NAME (x)));
3238 put_var_into_stack (x);
3242 TREE_ADDRESSABLE (x) = 1;
3243 #if 0 /* poplevel deals with this now. */
3244 if (DECL_CONTEXT (x) == 0)
3245 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3253 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3256 build_conditional_expr (ifexp, op1, op2)
3257 tree ifexp, op1, op2;
3259 register tree type1;
3260 register tree type2;
3261 register enum tree_code code1;
3262 register enum tree_code code2;
3263 register tree result_type = NULL;
3264 tree orig_op1 = op1, orig_op2 = op2;
3266 /* If second operand is omitted, it is the same as the first one;
3267 make sure it is calculated only once. */
3271 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression");
3272 ifexp = op1 = save_expr (ifexp);
3275 ifexp = truthvalue_conversion (default_conversion (ifexp));
3277 #if 0 /* Produces wrong result if within sizeof. */
3278 /* Don't promote the operands separately if they promote
3279 the same way. Return the unpromoted type and let the combined
3280 value get promoted if necessary. */
3282 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3283 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3284 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3285 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3287 if (TREE_CODE (ifexp) == INTEGER_CST)
3288 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3290 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3294 /* Promote both alternatives. */
3296 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3297 op1 = default_conversion (op1);
3298 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3299 op2 = default_conversion (op2);
3301 if (TREE_CODE (ifexp) == ERROR_MARK
3302 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3303 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3304 return error_mark_node;
3306 type1 = TREE_TYPE (op1);
3307 code1 = TREE_CODE (type1);
3308 type2 = TREE_TYPE (op2);
3309 code2 = TREE_CODE (type2);
3311 /* Quickly detect the usual case where op1 and op2 have the same type
3313 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3316 result_type = type1;
3318 result_type = TYPE_MAIN_VARIANT (type1);
3320 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3321 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3323 result_type = common_type (type1, type2);
3325 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3327 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3328 pedwarn ("ANSI C forbids conditional expr with only one void side");
3329 result_type = void_type_node;
3331 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3333 if (comp_target_types (type1, type2))
3334 result_type = common_type (type1, type2);
3335 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3336 && TREE_CODE (orig_op1) != NOP_EXPR)
3337 result_type = qualify_type (type2, type1);
3338 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3339 && TREE_CODE (orig_op2) != NOP_EXPR)
3340 result_type = qualify_type (type1, type2);
3341 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3343 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3344 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3345 result_type = qualify_type (type1, type2);
3347 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3349 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3350 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3351 result_type = qualify_type (type2, type1);
3355 pedwarn ("pointer type mismatch in conditional expression");
3356 result_type = build_pointer_type (void_type_node);
3359 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3361 if (! integer_zerop (op2))
3362 pedwarn ("pointer/integer type mismatch in conditional expression");
3365 op2 = null_pointer_node;
3366 #if 0 /* The spec seems to say this is permitted. */
3367 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3368 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3371 result_type = type1;
3373 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3375 if (!integer_zerop (op1))
3376 pedwarn ("pointer/integer type mismatch in conditional expression");
3379 op1 = null_pointer_node;
3380 #if 0 /* The spec seems to say this is permitted. */
3381 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3382 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3385 result_type = type2;
3390 if (flag_cond_mismatch)
3391 result_type = void_type_node;
3394 error ("type mismatch in conditional expression");
3395 return error_mark_node;
3399 /* Merge const and volatile flags of the incoming types. */
3401 = build_type_variant (result_type,
3402 TREE_READONLY (op1) || TREE_READONLY (op2),
3403 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3405 if (result_type != TREE_TYPE (op1))
3406 op1 = convert_and_check (result_type, op1);
3407 if (result_type != TREE_TYPE (op2))
3408 op2 = convert_and_check (result_type, op2);
3411 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3413 result_type = TREE_TYPE (op1);
3414 if (TREE_CONSTANT (ifexp))
3415 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3417 if (TYPE_MODE (result_type) == BLKmode)
3419 register tree tempvar
3420 = build_decl (VAR_DECL, NULL_TREE, result_type);
3421 register tree xop1 = build_modify_expr (tempvar, op1);
3422 register tree xop2 = build_modify_expr (tempvar, op2);
3423 register tree result = fold (build (COND_EXPR, result_type,
3424 ifexp, xop1, xop2));
3426 layout_decl (tempvar, TYPE_ALIGN (result_type));
3427 /* No way to handle variable-sized objects here.
3428 I fear that the entire handling of BLKmode conditional exprs
3429 needs to be redone. */
3430 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3433 = assign_stack_local (DECL_MODE (tempvar),
3434 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3435 + BITS_PER_UNIT - 1)
3439 TREE_SIDE_EFFECTS (result)
3440 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3441 | TREE_SIDE_EFFECTS (op2);
3442 return build (COMPOUND_EXPR, result_type, result, tempvar);
3447 if (TREE_CODE (ifexp) == INTEGER_CST)
3448 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3450 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3453 /* Given a list of expressions, return a compound expression
3454 that performs them all and returns the value of the last of them. */
3457 build_compound_expr (list)
3460 return internal_build_compound_expr (list, TRUE);
3464 internal_build_compound_expr (list, first_p)
3470 if (TREE_CHAIN (list) == 0)
3472 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3473 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3475 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3476 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3477 list = TREE_OPERAND (list, 0);
3480 /* Don't let (0, 0) be null pointer constant. */
3481 if (!first_p && integer_zerop (TREE_VALUE (list)))
3482 return non_lvalue (TREE_VALUE (list));
3483 return TREE_VALUE (list);
3486 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3488 /* Convert arrays to pointers when there really is a comma operator. */
3489 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3490 TREE_VALUE (TREE_CHAIN (list))
3491 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3494 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3496 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3498 /* The left-hand operand of a comma expression is like an expression
3499 statement: with -W or -Wunused, we should warn if it doesn't have
3500 any side-effects, unless it was explicitly cast to (void). */
3501 if ((extra_warnings || warn_unused)
3502 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3503 && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
3504 warning ("left-hand operand of comma expression has no effect");
3506 /* When pedantic, a compound expression can be neither an lvalue
3507 nor an integer constant expression. */
3512 /* With -Wunused, we should also warn if the left-hand operand does have
3513 side-effects, but computes a value which is not used. For example, in
3514 `foo() + bar(), baz()' the result of the `+' operator is not used,
3515 so we should issue a warning. */
3516 else if (warn_unused)
3517 warn_if_unused_value (TREE_VALUE (list));
3519 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3522 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3525 build_c_cast (type, expr)
3529 register tree value = expr;
3531 if (type == error_mark_node || expr == error_mark_node)
3532 return error_mark_node;
3533 type = TYPE_MAIN_VARIANT (type);
3536 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3537 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3538 value = TREE_OPERAND (value, 0);
3541 if (TREE_CODE (type) == ARRAY_TYPE)
3543 error ("cast specifies array type");
3544 return error_mark_node;
3547 if (TREE_CODE (type) == FUNCTION_TYPE)
3549 error ("cast specifies function type");
3550 return error_mark_node;
3553 if (type == TREE_TYPE (value))
3557 if (TREE_CODE (type) == RECORD_TYPE
3558 || TREE_CODE (type) == UNION_TYPE)
3559 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3562 else if (TREE_CODE (type) == UNION_TYPE)
3565 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3566 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3567 value = default_conversion (value);
3569 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3570 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3571 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3580 pedwarn ("ANSI C forbids casts to union type");
3581 if (TYPE_NAME (type) != 0)
3583 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3584 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3586 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3590 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3591 build_tree_list (field, value)),
3593 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3596 error ("cast to union type from type not present in union");
3597 return error_mark_node;
3603 /* If casting to void, avoid the error that would come
3604 from default_conversion in the case of a non-lvalue array. */
3605 if (type == void_type_node)
3606 return build1 (CONVERT_EXPR, type, value);
3608 /* Convert functions and arrays to pointers,
3609 but don't convert any other types. */
3610 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3611 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3612 value = default_conversion (value);
3613 otype = TREE_TYPE (value);
3615 /* Optionally warn about potentially worrisome casts. */
3618 && TREE_CODE (type) == POINTER_TYPE
3619 && TREE_CODE (otype) == POINTER_TYPE)
3621 if (TYPE_VOLATILE (TREE_TYPE (otype))
3622 && ! TYPE_VOLATILE (TREE_TYPE (type)))
3623 pedwarn ("cast discards `volatile' from pointer target type");
3624 if (TYPE_READONLY (TREE_TYPE (otype))
3625 && ! TYPE_READONLY (TREE_TYPE (type)))
3626 pedwarn ("cast discards `const' from pointer target type");
3629 /* Warn about possible alignment problems. */
3630 if (STRICT_ALIGNMENT && warn_cast_align
3631 && TREE_CODE (type) == POINTER_TYPE
3632 && TREE_CODE (otype) == POINTER_TYPE
3633 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3634 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3635 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3636 warning ("cast increases required alignment of target type");
3638 if (TREE_CODE (type) == INTEGER_TYPE
3639 && TREE_CODE (otype) == POINTER_TYPE
3640 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3641 && !TREE_CONSTANT (value))
3642 warning ("cast from pointer to integer of different size");
3644 if (warn_bad_function_cast
3645 && TREE_CODE (value) == CALL_EXPR
3646 && TREE_CODE (type) != TREE_CODE (otype))
3647 warning ("cast does not match function type");
3649 if (TREE_CODE (type) == POINTER_TYPE
3650 && TREE_CODE (otype) == INTEGER_TYPE
3651 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3653 /* Don't warn about converting 0 to pointer,
3654 provided the 0 was explicit--not cast or made by folding. */
3655 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3657 /* Don't warn about converting any constant. */
3658 && !TREE_CONSTANT (value))
3659 warning ("cast to pointer from integer of different size");
3662 value = convert (type, value);
3664 /* Ignore any integer overflow caused by the cast. */
3665 if (TREE_CODE (value) == INTEGER_CST)
3667 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3668 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3672 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3673 if (pedantic && TREE_CODE (value) == INTEGER_CST
3674 && TREE_CODE (expr) == INTEGER_CST
3675 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3676 value = non_lvalue (value);
3678 /* If pedantic, don't let a cast be an lvalue. */
3679 if (value == expr && pedantic)
3680 value = non_lvalue (value);
3685 /* Build an assignment expression of lvalue LHS from value RHS.
3686 MODIFYCODE is the code for a binary operator that we use
3687 to combine the old value of LHS with RHS to get the new value.
3688 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3691 build_modify_expr (lhs, modifycode, rhs)
3693 enum tree_code modifycode;
3695 register tree result;
3697 tree lhstype = TREE_TYPE (lhs);
3698 tree olhstype = lhstype;
3700 /* Types that aren't fully specified cannot be used in assignments. */
3701 lhs = require_complete_type (lhs);
3703 /* Avoid duplicate error messages from operands that had errors. */
3704 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3705 return error_mark_node;
3707 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3708 /* Do not use STRIP_NOPS here. We do not want an enumerator
3709 whose value is 0 to count as a null pointer constant. */
3710 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3711 rhs = TREE_OPERAND (rhs, 0);
3715 /* Handle control structure constructs used as "lvalues". */
3717 switch (TREE_CODE (lhs))
3719 /* Handle (a, b) used as an "lvalue". */
3721 pedantic_lvalue_warning (COMPOUND_EXPR);
3722 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3724 if (TREE_CODE (newrhs) == ERROR_MARK)
3725 return error_mark_node;
3726 return build (COMPOUND_EXPR, lhstype,
3727 TREE_OPERAND (lhs, 0), newrhs);
3729 /* Handle (a ? b : c) used as an "lvalue". */
3731 pedantic_lvalue_warning (COND_EXPR);
3732 rhs = save_expr (rhs);
3734 /* Produce (a ? (b = rhs) : (c = rhs))
3735 except that the RHS goes through a save-expr
3736 so the code to compute it is only emitted once. */
3738 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3739 build_modify_expr (TREE_OPERAND (lhs, 1),
3741 build_modify_expr (TREE_OPERAND (lhs, 2),
3743 if (TREE_CODE (cond) == ERROR_MARK)
3745 /* Make sure the code to compute the rhs comes out
3746 before the split. */
3747 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3748 /* But cast it to void to avoid an "unused" error. */
3749 convert (void_type_node, rhs), cond);
3753 /* If a binary op has been requested, combine the old LHS value with the RHS
3754 producing the value we should actually store into the LHS. */
3756 if (modifycode != NOP_EXPR)
3758 lhs = stabilize_reference (lhs);
3759 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3762 /* Handle a cast used as an "lvalue".
3763 We have already performed any binary operator using the value as cast.
3764 Now convert the result to the cast type of the lhs,
3765 and then true type of the lhs and store it there;
3766 then convert result back to the cast type to be the value
3767 of the assignment. */
3769 switch (TREE_CODE (lhs))
3774 case FIX_TRUNC_EXPR:
3775 case FIX_FLOOR_EXPR:
3776 case FIX_ROUND_EXPR:
3778 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3779 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3780 newrhs = default_conversion (newrhs);
3782 tree inner_lhs = TREE_OPERAND (lhs, 0);
3784 result = build_modify_expr (inner_lhs, NOP_EXPR,
3785 convert (TREE_TYPE (inner_lhs),
3786 convert (lhstype, newrhs)));
3787 if (TREE_CODE (result) == ERROR_MARK)
3789 pedantic_lvalue_warning (CONVERT_EXPR);
3790 return convert (TREE_TYPE (lhs), result);
3794 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3795 Reject anything strange now. */
3797 if (!lvalue_or_else (lhs, "assignment"))
3798 return error_mark_node;
3800 /* Warn about storing in something that is `const'. */
3802 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3803 || ((TREE_CODE (lhstype) == RECORD_TYPE
3804 || TREE_CODE (lhstype) == UNION_TYPE)
3805 && C_TYPE_FIELDS_READONLY (lhstype)))
3806 readonly_warning (lhs, "assignment");
3808 /* If storing into a structure or union member,
3809 it has probably been given type `int'.
3810 Compute the type that would go with
3811 the actual amount of storage the member occupies. */
3813 if (TREE_CODE (lhs) == COMPONENT_REF
3814 && (TREE_CODE (lhstype) == INTEGER_TYPE
3815 || TREE_CODE (lhstype) == REAL_TYPE
3816 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3817 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3819 /* If storing in a field that is in actuality a short or narrower than one,
3820 we must store in the field in its actual type. */
3822 if (lhstype != TREE_TYPE (lhs))
3824 lhs = copy_node (lhs);
3825 TREE_TYPE (lhs) = lhstype;
3828 /* Convert new value to destination type. */
3830 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
3831 NULL_TREE, NULL_TREE, 0);
3832 if (TREE_CODE (newrhs) == ERROR_MARK)
3833 return error_mark_node;
3835 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3836 TREE_SIDE_EFFECTS (result) = 1;
3838 /* If we got the LHS in a different type for storing in,
3839 convert the result back to the nominal type of LHS
3840 so that the value we return always has the same type
3841 as the LHS argument. */
3843 if (olhstype == TREE_TYPE (result))
3845 return convert_for_assignment (olhstype, result, "assignment",
3846 NULL_TREE, NULL_TREE, 0);
3849 /* Convert value RHS to type TYPE as preparation for an assignment
3850 to an lvalue of type TYPE.
3851 The real work of conversion is done by `convert'.
3852 The purpose of this function is to generate error messages
3853 for assignments that are not allowed in C.
3854 ERRTYPE is a string to use in error messages:
3855 "assignment", "return", etc. If it is null, this is parameter passing
3856 for a function call (and different error messages are output). Otherwise,
3857 it may be a name stored in the spelling stack and interpreted by
3860 FUNNAME is the name of the function being called,
3861 as an IDENTIFIER_NODE, or null.
3862 PARMNUM is the number of the argument, for printing in error messages. */
3865 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3868 tree fundecl, funname;
3871 register enum tree_code codel = TREE_CODE (type);
3872 register tree rhstype;
3873 register enum tree_code coder;
3875 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3876 /* Do not use STRIP_NOPS here. We do not want an enumerator
3877 whose value is 0 to count as a null pointer constant. */
3878 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3879 rhs = TREE_OPERAND (rhs, 0);
3881 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3882 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3883 rhs = default_conversion (rhs);
3884 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3885 rhs = decl_constant_value (rhs);
3887 rhstype = TREE_TYPE (rhs);
3888 coder = TREE_CODE (rhstype);
3890 if (coder == ERROR_MARK)
3891 return error_mark_node;
3893 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3895 overflow_warning (rhs);
3896 /* Check for Objective-C protocols. This will issue a warning if
3897 there are protocol violations. No need to use the return value. */
3898 maybe_objc_comptypes (type, rhstype, 0);
3902 if (coder == VOID_TYPE)
3904 error ("void value not ignored as it ought to be");
3905 return error_mark_node;
3907 /* Arithmetic types all interconvert, and enum is treated like int. */
3908 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
3909 || codel == COMPLEX_TYPE)
3910 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
3911 || coder == COMPLEX_TYPE))
3912 return convert_and_check (type, rhs);
3914 /* Conversion to a union from its member types. */
3915 else if (codel == UNION_TYPE)
3919 for (memb_types = TYPE_FIELDS (type); memb_types;
3920 memb_types = TREE_CHAIN (memb_types))
3922 if (comptypes (TREE_TYPE (memb_types), TREE_TYPE (rhs)))
3925 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
3926 pedwarn ("ANSI C prohibits argument conversion to union type");
3927 return build1 (NOP_EXPR, type, rhs);
3930 else if (coder == POINTER_TYPE
3931 && TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE)
3933 tree memb_type = TREE_TYPE (memb_types);
3934 register tree ttl = TREE_TYPE (memb_type);
3935 register tree ttr = TREE_TYPE (rhstype);
3937 /* Any non-function converts to a [const][volatile] void *
3938 and vice versa; otherwise, targets must be the same.
3939 Meanwhile, the lhs target must have all the qualifiers of
3941 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3942 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3943 || comp_target_types (memb_type, rhstype))
3945 /* Const and volatile mean something different for function
3946 types, so the usual warnings are not appropriate. */
3947 if (TREE_CODE (ttr) != FUNCTION_TYPE
3948 || TREE_CODE (ttl) != FUNCTION_TYPE)
3950 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
3951 warn_for_assignment ("%s discards `const' from pointer target type",
3952 get_spelling (errtype), funname,
3954 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
3955 warn_for_assignment ("%s discards `volatile' from pointer target type",
3956 get_spelling (errtype), funname,
3961 /* Because const and volatile on functions are
3962 restrictions that say the function will not do
3963 certain things, it is okay to use a const or volatile
3964 function where an ordinary one is wanted, but not
3966 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
3967 warn_for_assignment ("%s makes `const *' function pointer from non-const",
3968 get_spelling (errtype), funname,
3970 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
3971 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
3972 get_spelling (errtype), funname,
3977 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
3978 pedwarn ("ANSI C prohibits argument conversion to union type");
3979 return build1 (NOP_EXPR, type, rhs);
3983 /* Can convert integer zero to any pointer type. */
3984 else if (TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE
3985 && (integer_zerop (rhs)
3986 || (TREE_CODE (rhs) == NOP_EXPR
3987 && integer_zerop (TREE_OPERAND (rhs, 0)))))
3988 return build1 (NOP_EXPR, type, null_pointer_node);
3992 /* Conversions among pointers */
3993 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
3995 register tree ttl = TREE_TYPE (type);
3996 register tree ttr = TREE_TYPE (rhstype);
3998 /* Any non-function converts to a [const][volatile] void *
3999 and vice versa; otherwise, targets must be the same.
4000 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4001 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4002 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4003 || comp_target_types (type, rhstype)
4004 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4005 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4008 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4009 && TREE_CODE (ttr) == FUNCTION_TYPE)
4011 (TYPE_MAIN_VARIANT (ttr) == void_type_node
4012 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4013 which are not ANSI null ptr constants. */
4014 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4015 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4016 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4017 get_spelling (errtype), funname, parmnum);
4018 /* Const and volatile mean something different for function types,
4019 so the usual warnings are not appropriate. */
4020 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4021 && TREE_CODE (ttl) != FUNCTION_TYPE)
4023 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4024 warn_for_assignment ("%s discards `const' from pointer target type",
4025 get_spelling (errtype), funname, parmnum);
4026 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4027 warn_for_assignment ("%s discards `volatile' from pointer target type",
4028 get_spelling (errtype), funname, parmnum);
4029 /* If this is not a case of ignoring a mismatch in signedness,
4031 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4032 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4033 || comp_target_types (type, rhstype))
4035 /* If there is a mismatch, do warn. */
4037 warn_for_assignment ("pointer targets in %s differ in signedness",
4038 get_spelling (errtype), funname, parmnum);
4042 /* Because const and volatile on functions are restrictions
4043 that say the function will not do certain things,
4044 it is okay to use a const or volatile function
4045 where an ordinary one is wanted, but not vice-versa. */
4046 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4047 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4048 get_spelling (errtype), funname, parmnum);
4049 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4050 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4051 get_spelling (errtype), funname, parmnum);
4055 warn_for_assignment ("%s from incompatible pointer type",
4056 get_spelling (errtype), funname, parmnum);
4057 return convert (type, rhs);
4059 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4061 /* An explicit constant 0 can convert to a pointer,
4062 or one that results from arithmetic, even including
4063 a cast to integer type. */
4064 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4066 ! (TREE_CODE (rhs) == NOP_EXPR
4067 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4068 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4069 && integer_zerop (TREE_OPERAND (rhs, 0))))
4071 warn_for_assignment ("%s makes pointer from integer without a cast",
4072 get_spelling (errtype), funname, parmnum);
4073 return convert (type, rhs);
4075 return null_pointer_node;
4077 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4079 warn_for_assignment ("%s makes integer from pointer without a cast",
4080 get_spelling (errtype), funname, parmnum);
4081 return convert (type, rhs);
4088 tree selector = maybe_building_objc_message_expr ();
4090 if (selector && parmnum > 2)
4091 error ("incompatible type for argument %d of `%s'",
4092 parmnum - 2, IDENTIFIER_POINTER (selector));
4094 error ("incompatible type for argument %d of `%s'",
4095 parmnum, IDENTIFIER_POINTER (funname));
4098 error ("incompatible type for argument %d of indirect function call",
4102 error ("incompatible types in %s", get_spelling (errtype));
4104 return error_mark_node;
4107 /* Print a warning using MSG.
4108 It gets OPNAME as its one parameter.
4109 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4110 FUNCTION and ARGNUM are handled specially if we are building an
4111 Objective-C selector. */
4114 warn_for_assignment (msg, opname, function, argnum)
4120 static char argstring[] = "passing arg %d of `%s'";
4121 static char argnofun[] = "passing arg %d";
4125 tree selector = maybe_building_objc_message_expr ();
4127 if (selector && argnum > 2)
4129 function = selector;
4134 /* Function name is known; supply it. */
4135 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4136 + sizeof (argstring) + 25 /*%d*/ + 1);
4137 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4141 /* Function name unknown (call through ptr); just give arg number. */
4142 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4143 sprintf (opname, argnofun, argnum);
4146 pedwarn (msg, opname);
4149 /* Return nonzero if VALUE is a valid constant-valued expression
4150 for use in initializing a static variable; one that can be an
4151 element of a "constant" initializer.
4153 Return null_pointer_node if the value is absolute;
4154 if it is relocatable, return the variable that determines the relocation.
4155 We assume that VALUE has been folded as much as possible;
4156 therefore, we do not need to check for such things as
4157 arithmetic-combinations of integers. */
4160 initializer_constant_valid_p (value, endtype)
4164 switch (TREE_CODE (value))
4167 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4168 && TREE_CONSTANT (value))
4170 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
4173 return TREE_STATIC (value) ? null_pointer_node : 0;
4179 return null_pointer_node;
4182 return TREE_OPERAND (value, 0);
4184 case NON_LVALUE_EXPR:
4185 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4189 /* Allow conversions between pointer types. */
4190 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4191 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
4192 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4194 /* Allow conversions between real types. */
4195 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4196 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
4197 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4199 /* Allow length-preserving conversions between integer types. */
4200 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4201 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4202 && (TYPE_PRECISION (TREE_TYPE (value))
4203 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4204 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4206 /* Allow conversions between other integer types only if
4208 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4209 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4211 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4213 if (inner == null_pointer_node)
4214 return null_pointer_node;
4218 /* Allow (int) &foo provided int is as wide as a pointer. */
4219 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4220 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
4221 && (TYPE_PRECISION (TREE_TYPE (value))
4222 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4223 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4226 /* Likewise conversions from int to pointers. */
4227 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4228 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4229 && (TYPE_PRECISION (TREE_TYPE (value))
4230 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4231 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4234 /* Allow conversions to union types if the value inside is okay. */
4235 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4236 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4241 if (TREE_CODE (endtype) == INTEGER_TYPE
4242 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4245 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4247 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4249 /* If either term is absolute, use the other terms relocation. */
4250 if (valid0 == null_pointer_node)
4252 if (valid1 == null_pointer_node)
4258 if (TREE_CODE (endtype) == INTEGER_TYPE
4259 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4262 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4264 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4266 /* Win if second argument is absolute. */
4267 if (valid1 == null_pointer_node)
4269 /* Win if both arguments have the same relocation.
4270 Then the value is absolute. */
4271 if (valid0 == valid1)
4272 return null_pointer_node;
4280 /* If VALUE is a compound expr all of whose expressions are constant, then
4281 return its value. Otherwise, return error_mark_node.
4283 This is for handling COMPOUND_EXPRs as initializer elements
4284 which is allowed with a warning when -pedantic is specified. */
4287 valid_compound_expr_initializer (value, endtype)
4291 if (TREE_CODE (value) == COMPOUND_EXPR)
4293 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4295 return error_mark_node;
4296 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4299 else if (! TREE_CONSTANT (value)
4300 && ! initializer_constant_valid_p (value, endtype))
4301 return error_mark_node;
4306 /* Perform appropriate conversions on the initial value of a variable,
4307 store it in the declaration DECL,
4308 and print any error messages that are appropriate.
4309 If the init is invalid, store an ERROR_MARK. */
4312 store_init_value (decl, init)
4315 register tree value, type;
4317 /* If variable's type was invalidly declared, just ignore it. */
4319 type = TREE_TYPE (decl);
4320 if (TREE_CODE (type) == ERROR_MARK)
4323 /* Digest the specified initializer into an expression. */
4325 value = digest_init (type, init, TREE_STATIC (decl),
4326 TREE_STATIC (decl) || pedantic);
4328 /* Store the expression if valid; else report error. */
4331 /* Note that this is the only place we can detect the error
4332 in a case such as struct foo bar = (struct foo) { x, y };
4333 where there is one initial value which is a constructor expression. */
4334 if (value == error_mark_node)
4336 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4338 error ("initializer for static variable is not constant");
4339 value = error_mark_node;
4341 else if (TREE_STATIC (decl)
4342 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4344 error ("initializer for static variable uses complicated arithmetic");
4345 value = error_mark_node;
4349 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4351 if (! TREE_CONSTANT (value))
4352 pedwarn ("aggregate initializer is not constant");
4353 else if (! TREE_STATIC (value))
4354 pedwarn ("aggregate initializer uses complicated arithmetic");
4359 DECL_INITIAL (decl) = value;
4361 /* ANSI wants warnings about out-of-range constant initializers. */
4362 STRIP_TYPE_NOPS (value);
4363 constant_expression_warning (value);
4366 /* Methods for storing and printing names for error messages. */
4368 /* Implement a spelling stack that allows components of a name to be pushed
4369 and popped. Each element on the stack is this structure. */
4381 #define SPELLING_STRING 1
4382 #define SPELLING_MEMBER 2
4383 #define SPELLING_BOUNDS 3
4385 static struct spelling *spelling; /* Next stack element (unused). */
4386 static struct spelling *spelling_base; /* Spelling stack base. */
4387 static int spelling_size; /* Size of the spelling stack. */
4389 /* Macros to save and restore the spelling stack around push_... functions.
4390 Alternative to SAVE_SPELLING_STACK. */
4392 #define SPELLING_DEPTH() (spelling - spelling_base)
4393 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4395 /* Save and restore the spelling stack around arbitrary C code. */
4397 #define SAVE_SPELLING_DEPTH(code) \
4399 int __depth = SPELLING_DEPTH (); \
4401 RESTORE_SPELLING_DEPTH (__depth); \
4404 /* Push an element on the spelling stack with type KIND and assign VALUE
4407 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4409 int depth = SPELLING_DEPTH (); \
4411 if (depth >= spelling_size) \
4413 spelling_size += 10; \
4414 if (spelling_base == 0) \
4416 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4419 = (struct spelling *) xrealloc (spelling_base, \
4420 spelling_size * sizeof (struct spelling)); \
4421 RESTORE_SPELLING_DEPTH (depth); \
4424 spelling->kind = (KIND); \
4425 spelling->MEMBER = (VALUE); \
4429 /* Push STRING on the stack. Printed literally. */
4432 push_string (string)
4435 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4438 /* Push a member name on the stack. Printed as '.' STRING. */
4441 push_member_name (decl)
4446 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4447 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4450 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4453 push_array_bounds (bounds)
4456 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4459 /* Compute the maximum size in bytes of the printed spelling. */
4464 register int size = 0;
4465 register struct spelling *p;
4467 for (p = spelling_base; p < spelling; p++)
4469 if (p->kind == SPELLING_BOUNDS)
4472 size += strlen (p->u.s) + 1;
4478 /* Print the spelling to BUFFER and return it. */
4481 print_spelling (buffer)
4482 register char *buffer;
4484 register char *d = buffer;
4486 register struct spelling *p;
4488 for (p = spelling_base; p < spelling; p++)
4489 if (p->kind == SPELLING_BOUNDS)
4491 sprintf (d, "[%d]", p->u.i);
4496 if (p->kind == SPELLING_MEMBER)
4498 for (s = p->u.s; *d = *s++; d++)
4505 /* Provide a means to pass component names derived from the spelling stack. */
4507 char initialization_message;
4509 /* Interpret the spelling of the given ERRTYPE message. */
4512 get_spelling (errtype)
4515 static char *buffer;
4516 static int size = -1;
4518 if (errtype == &initialization_message)
4520 /* Avoid counting chars */
4521 static char message[] = "initialization of `%s'";
4522 register int needed = sizeof (message) + spelling_length () + 1;
4526 buffer = (char *) xmalloc (size = needed);
4528 buffer = (char *) xrealloc (buffer, size = needed);
4530 temp = (char *) alloca (needed);
4531 sprintf (buffer, message, print_spelling (temp));
4538 /* Issue an error message for a bad initializer component.
4539 FORMAT describes the message. OFWHAT is the name for the component.
4540 LOCAL is a format string for formatting the insertion of the name
4543 If OFWHAT is null, the component name is stored on the spelling stack.
4544 If the component name is a null string, then LOCAL is omitted entirely. */
4547 error_init (format, local, ofwhat)
4548 char *format, *local, *ofwhat;
4553 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4554 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4557 sprintf (buffer, local, ofwhat);
4561 error (format, buffer);
4564 /* Issue a pedantic warning for a bad initializer component.
4565 FORMAT describes the message. OFWHAT is the name for the component.
4566 LOCAL is a format string for formatting the insertion of the name
4569 If OFWHAT is null, the component name is stored on the spelling stack.
4570 If the component name is a null string, then LOCAL is omitted entirely. */
4573 pedwarn_init (format, local, ofwhat)
4574 char *format, *local, *ofwhat;
4579 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4580 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4583 sprintf (buffer, local, ofwhat);
4587 pedwarn (format, buffer);
4590 /* Issue a warning for a bad initializer component.
4591 FORMAT describes the message. OFWHAT is the name for the component.
4592 LOCAL is a format string for formatting the insertion of the name
4595 If OFWHAT is null, the component name is stored on the spelling stack.
4596 If the component name is a null string, then LOCAL is omitted entirely. */
4599 warning_init (format, local, ofwhat)
4600 char *format, *local, *ofwhat;
4605 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4606 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4609 sprintf (buffer, local, ofwhat);
4613 warning (format, buffer);
4616 /* Digest the parser output INIT as an initializer for type TYPE.
4617 Return a C expression of type TYPE to represent the initial value.
4619 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4620 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4621 applies only to elements of constructors. */
4624 digest_init (type, init, require_constant, constructor_constant)
4626 int require_constant, constructor_constant;
4628 enum tree_code code = TREE_CODE (type);
4629 tree inside_init = init;
4631 if (init == error_mark_node)
4634 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4635 /* Do not use STRIP_NOPS here. We do not want an enumerator
4636 whose value is 0 to count as a null pointer constant. */
4637 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4638 inside_init = TREE_OPERAND (init, 0);
4640 /* Initialization of an array of chars from a string constant
4641 optionally enclosed in braces. */
4643 if (code == ARRAY_TYPE)
4645 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4646 if ((typ1 == char_type_node
4647 || typ1 == signed_char_type_node
4648 || typ1 == unsigned_char_type_node
4649 || typ1 == unsigned_wchar_type_node
4650 || typ1 == signed_wchar_type_node)
4651 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4653 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4654 TYPE_MAIN_VARIANT (type)))
4657 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4659 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4661 error_init ("char-array%s initialized from wide string",
4663 return error_mark_node;
4665 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4667 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4669 error_init ("int-array%s initialized from non-wide string",
4671 return error_mark_node;
4674 TREE_TYPE (inside_init) = type;
4675 if (TYPE_DOMAIN (type) != 0
4676 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4678 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4679 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4680 /* Subtract 1 (or sizeof (wchar_t))
4681 because it's ok to ignore the terminating null char
4682 that is counted in the length of the constant. */
4683 if (size < TREE_STRING_LENGTH (inside_init)
4684 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4685 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4688 "initializer-string for array of chars%s is too long",
4695 /* Any type can be initialized
4696 from an expression of the same type, optionally with braces. */
4698 if (inside_init && TREE_TYPE (inside_init) != 0
4699 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4700 TYPE_MAIN_VARIANT (type))
4701 || (code == ARRAY_TYPE
4702 && comptypes (TREE_TYPE (inside_init), type))
4703 || (code == POINTER_TYPE
4704 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4705 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4706 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4707 TREE_TYPE (type)))))
4709 if (code == POINTER_TYPE
4710 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4711 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4712 inside_init = default_conversion (inside_init);
4713 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4714 && TREE_CODE (inside_init) != CONSTRUCTOR)
4716 error_init ("array%s initialized from non-constant array expression",
4718 return error_mark_node;
4721 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4722 inside_init = decl_constant_value (inside_init);
4724 /* Compound expressions can only occur here if -pedantic or
4725 -pedantic-errors is specified. In the later case, we always want
4726 an error. In the former case, we simply want a warning. */
4727 if (require_constant && pedantic
4728 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4731 = valid_compound_expr_initializer (inside_init,
4732 TREE_TYPE (inside_init));
4733 if (inside_init == error_mark_node)
4734 error_init ("initializer element%s is not constant",
4737 pedwarn_init ("initializer element%s is not constant",
4739 if (flag_pedantic_errors)
4740 inside_init = error_mark_node;
4742 else if (require_constant && ! TREE_CONSTANT (inside_init))
4744 error_init ("initializer element%s is not constant",
4746 inside_init = error_mark_node;
4748 else if (require_constant
4749 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4751 error_init ("initializer element%s is not computable at load time",
4753 inside_init = error_mark_node;
4759 /* Handle scalar types, including conversions. */
4761 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4762 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4764 /* Note that convert_for_assignment calls default_conversion
4765 for arrays and functions. We must not call it in the
4766 case where inside_init is a null pointer constant. */
4768 = convert_for_assignment (type, init, "initialization",
4769 NULL_TREE, NULL_TREE, 0);
4771 if (require_constant && ! TREE_CONSTANT (inside_init))
4773 error_init ("initializer element%s is not constant",
4775 inside_init = error_mark_node;
4777 else if (require_constant
4778 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4780 error_init ("initializer element%s is not computable at load time",
4782 inside_init = error_mark_node;
4788 /* Come here only for records and arrays. */
4790 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4792 error_init ("variable-sized object%s may not be initialized",
4794 return error_mark_node;
4797 /* Traditionally, you can write struct foo x = 0;
4798 and it initializes the first element of x to 0. */
4799 if (flag_traditional)
4801 tree top = 0, prev = 0;
4802 while (TREE_CODE (type) == RECORD_TYPE
4803 || TREE_CODE (type) == ARRAY_TYPE
4804 || TREE_CODE (type) == QUAL_UNION_TYPE
4805 || TREE_CODE (type) == UNION_TYPE)
4807 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4811 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4813 if (TREE_CODE (type) == ARRAY_TYPE)
4814 type = TREE_TYPE (type);
4815 else if (TYPE_FIELDS (type))
4816 type = TREE_TYPE (TYPE_FIELDS (type));
4819 error_init ("invalid initializer%s", " for `%s'", NULL);
4820 return error_mark_node;
4823 TREE_OPERAND (prev, 1)
4824 = build_tree_list (NULL_TREE,
4825 digest_init (type, init, require_constant,
4826 constructor_constant));
4829 error_init ("invalid initializer%s", " for `%s'", NULL);
4830 return error_mark_node;
4833 /* Handle initializers that use braces. */
4835 /* Type of object we are accumulating a constructor for.
4836 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4837 static tree constructor_type;
4839 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4841 static tree constructor_fields;
4843 /* For an ARRAY_TYPE, this is the specified index
4844 at which to store the next element we get.
4845 This is a special INTEGER_CST node that we modify in place. */
4846 static tree constructor_index;
4848 /* For an ARRAY_TYPE, this is the end index of the range
4849 to intitialize with the next element, or NULL in the ordinary case
4850 where the element is used just once. */
4851 static tree constructor_range_end;
4853 /* For an ARRAY_TYPE, this is the maximum index. */
4854 static tree constructor_max_index;
4856 /* For a RECORD_TYPE, this is the first field not yet written out. */
4857 static tree constructor_unfilled_fields;
4859 /* For an ARRAY_TYPE, this is the index of the first element
4860 not yet written out.
4861 This is a special INTEGER_CST node that we modify in place. */
4862 static tree constructor_unfilled_index;
4864 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4865 This is so we can generate gaps between fields, when appropriate.
4866 This is a special INTEGER_CST node that we modify in place. */
4867 static tree constructor_bit_index;
4869 /* If we are saving up the elements rather than allocating them,
4870 this is the list of elements so far (in reverse order,
4871 most recent first). */
4872 static tree constructor_elements;
4874 /* 1 if so far this constructor's elements are all compile-time constants. */
4875 static int constructor_constant;
4877 /* 1 if so far this constructor's elements are all valid address constants. */
4878 static int constructor_simple;
4880 /* 1 if this constructor is erroneous so far. */
4881 static int constructor_erroneous;
4883 /* 1 if have called defer_addressed_constants. */
4884 static int constructor_subconstants_deferred;
4886 /* List of pending elements at this constructor level.
4887 These are elements encountered out of order
4888 which belong at places we haven't reached yet in actually
4889 writing the output. */
4890 static tree constructor_pending_elts;
4892 /* The SPELLING_DEPTH of this constructor. */
4893 static int constructor_depth;
4895 /* 0 if implicitly pushing constructor levels is allowed. */
4896 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4898 /* 1 if this constructor level was entered implicitly. */
4899 static int constructor_implicit;
4901 static int require_constant_value;
4902 static int require_constant_elements;
4904 /* 1 if it is ok to output this constructor as we read it.
4905 0 means must accumulate a CONSTRUCTOR expression. */
4906 static int constructor_incremental;
4908 /* DECL node for which an initializer is being read.
4909 0 means we are reading a constructor expression
4910 such as (struct foo) {...}. */
4911 static tree constructor_decl;
4913 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4914 static char *constructor_asmspec;
4916 /* Nonzero if this is an initializer for a top-level decl. */
4917 static int constructor_top_level;
4919 /* When we finish reading a constructor expression
4920 (constructor_decl is 0), the CONSTRUCTOR goes here. */
4921 static tree constructor_result;
4923 /* This stack has a level for each implicit or explicit level of
4924 structuring in the initializer, including the outermost one. It
4925 saves the values of most of the variables above. */
4927 struct constructor_stack
4929 struct constructor_stack *next;
4935 tree unfilled_index;
4936 tree unfilled_fields;
4942 /* If nonzero, this value should replace the entire
4943 constructor at this level. */
4944 tree replacement_value;
4953 struct constructor_stack *constructor_stack;
4955 /* This stack records separate initializers that are nested.
4956 Nested initializers can't happen in ANSI C, but GNU C allows them
4957 in cases like { ... (struct foo) { ... } ... }. */
4959 struct initializer_stack
4961 struct initializer_stack *next;
4964 struct constructor_stack *constructor_stack;
4966 struct spelling *spelling;
4967 struct spelling *spelling_base;
4971 char require_constant_value;
4972 char require_constant_elements;
4976 struct initializer_stack *initializer_stack;
4978 /* Prepare to parse and output the initializer for variable DECL. */
4981 start_init (decl, asmspec_tree, top_level)
4987 struct initializer_stack *p
4988 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
4992 asmspec = TREE_STRING_POINTER (asmspec_tree);
4994 p->decl = constructor_decl;
4995 p->asmspec = constructor_asmspec;
4996 p->incremental = constructor_incremental;
4997 p->require_constant_value = require_constant_value;
4998 p->require_constant_elements = require_constant_elements;
4999 p->constructor_stack = constructor_stack;
5000 p->elements = constructor_elements;
5001 p->spelling = spelling;
5002 p->spelling_base = spelling_base;
5003 p->spelling_size = spelling_size;
5004 p->deferred = constructor_subconstants_deferred;
5005 p->top_level = constructor_top_level;
5006 p->next = initializer_stack;
5007 initializer_stack = p;
5009 constructor_decl = decl;
5010 constructor_incremental = top_level;
5011 constructor_asmspec = asmspec;
5012 constructor_subconstants_deferred = 0;
5013 constructor_top_level = top_level;
5017 require_constant_value = TREE_STATIC (decl);
5018 require_constant_elements
5019 = ((TREE_STATIC (decl) || pedantic)
5020 /* For a scalar, you can always use any value to initialize,
5021 even within braces. */
5022 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5023 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5024 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5025 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5026 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5027 constructor_incremental |= TREE_STATIC (decl);
5031 require_constant_value = 0;
5032 require_constant_elements = 0;
5033 locus = "(anonymous)";
5036 constructor_stack = 0;
5038 missing_braces_mentioned = 0;
5042 RESTORE_SPELLING_DEPTH (0);
5045 push_string (locus);
5051 struct initializer_stack *p = initializer_stack;
5053 /* Output subconstants (string constants, usually)
5054 that were referenced within this initializer and saved up.
5055 Must do this if and only if we called defer_addressed_constants. */
5056 if (constructor_subconstants_deferred)
5057 output_deferred_addressed_constants ();
5059 /* Free the whole constructor stack of this initializer. */
5060 while (constructor_stack)
5062 struct constructor_stack *q = constructor_stack;
5063 constructor_stack = q->next;
5067 /* Pop back to the data of the outer initializer (if any). */
5068 constructor_decl = p->decl;
5069 constructor_asmspec = p->asmspec;
5070 constructor_incremental = p->incremental;
5071 require_constant_value = p->require_constant_value;
5072 require_constant_elements = p->require_constant_elements;
5073 constructor_stack = p->constructor_stack;
5074 constructor_elements = p->elements;
5075 spelling = p->spelling;
5076 spelling_base = p->spelling_base;
5077 spelling_size = p->spelling_size;
5078 constructor_subconstants_deferred = p->deferred;
5079 constructor_top_level = p->top_level;
5080 initializer_stack = p->next;
5084 /* Call here when we see the initializer is surrounded by braces.
5085 This is instead of a call to push_init_level;
5086 it is matched by a call to pop_init_level.
5088 TYPE is the type to initialize, for a constructor expression.
5089 For an initializer for a decl, TYPE is zero. */
5092 really_start_incremental_init (type)
5095 struct constructor_stack *p
5096 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5099 type = TREE_TYPE (constructor_decl);
5101 /* Turn off constructor_incremental if type is a struct with bitfields.
5102 Do this before the first push, so that the corrected value
5103 is available in finish_init. */
5104 check_init_type_bitfields (type);
5106 p->type = constructor_type;
5107 p->fields = constructor_fields;
5108 p->index = constructor_index;
5109 p->range_end = constructor_range_end;
5110 p->max_index = constructor_max_index;
5111 p->unfilled_index = constructor_unfilled_index;
5112 p->unfilled_fields = constructor_unfilled_fields;
5113 p->bit_index = constructor_bit_index;
5114 p->elements = constructor_elements;
5115 p->constant = constructor_constant;
5116 p->simple = constructor_simple;
5117 p->erroneous = constructor_erroneous;
5118 p->pending_elts = constructor_pending_elts;
5119 p->depth = constructor_depth;
5120 p->replacement_value = 0;
5122 p->incremental = constructor_incremental;
5125 constructor_stack = p;
5127 constructor_constant = 1;
5128 constructor_simple = 1;
5129 constructor_depth = SPELLING_DEPTH ();
5130 constructor_elements = 0;
5131 constructor_pending_elts = 0;
5132 constructor_type = type;
5134 if (TREE_CODE (constructor_type) == RECORD_TYPE
5135 || TREE_CODE (constructor_type) == UNION_TYPE)
5137 constructor_fields = TYPE_FIELDS (constructor_type);
5138 /* Skip any nameless bit fields atthe beginning. */
5139 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5140 && DECL_NAME (constructor_fields) == 0)
5141 constructor_fields = TREE_CHAIN (constructor_fields);
5142 constructor_unfilled_fields = constructor_fields;
5143 constructor_bit_index = copy_node (integer_zero_node);
5145 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5147 constructor_range_end = 0;
5148 if (TYPE_DOMAIN (constructor_type))
5150 constructor_max_index
5151 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5153 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5156 constructor_index = copy_node (integer_zero_node);
5157 constructor_unfilled_index = copy_node (constructor_index);
5161 /* Handle the case of int x = {5}; */
5162 constructor_fields = constructor_type;
5163 constructor_unfilled_fields = constructor_type;
5166 if (constructor_incremental)
5168 int momentary = suspend_momentary ();
5169 push_obstacks_nochange ();
5170 if (TREE_PERMANENT (constructor_decl))
5171 end_temporary_allocation ();
5172 make_decl_rtl (constructor_decl, constructor_asmspec,
5173 constructor_top_level);
5174 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5176 resume_momentary (momentary);
5179 if (constructor_incremental)
5181 defer_addressed_constants ();
5182 constructor_subconstants_deferred = 1;
5186 /* Push down into a subobject, for initialization.
5187 If this is for an explicit set of braces, IMPLICIT is 0.
5188 If it is because the next element belongs at a lower level,
5192 push_init_level (implicit)
5195 struct constructor_stack *p;
5197 /* If we've exhausted any levels that didn't have braces,
5199 while (constructor_stack->implicit)
5201 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5202 || TREE_CODE (constructor_type) == UNION_TYPE)
5203 && constructor_fields == 0)
5204 process_init_element (pop_init_level (1));
5205 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5206 && tree_int_cst_lt (constructor_max_index, constructor_index))
5207 process_init_element (pop_init_level (1));
5212 /* Structure elements may require alignment. Do this now
5213 if necessary for the subaggregate. */
5214 if (constructor_incremental && constructor_type != 0
5215 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields)
5217 /* Advance to offset of this element. */
5218 if (! tree_int_cst_equal (constructor_bit_index,
5219 DECL_FIELD_BITPOS (constructor_fields)))
5221 int next = (TREE_INT_CST_LOW
5222 (DECL_FIELD_BITPOS (constructor_fields))
5224 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5227 assemble_zeros (next - here);
5231 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5232 p->type = constructor_type;
5233 p->fields = constructor_fields;
5234 p->index = constructor_index;
5235 p->range_end = constructor_range_end;
5236 p->max_index = constructor_max_index;
5237 p->unfilled_index = constructor_unfilled_index;
5238 p->unfilled_fields = constructor_unfilled_fields;
5239 p->bit_index = constructor_bit_index;
5240 p->elements = constructor_elements;
5241 p->constant = constructor_constant;
5242 p->simple = constructor_simple;
5243 p->erroneous = constructor_erroneous;
5244 p->pending_elts = constructor_pending_elts;
5245 p->depth = constructor_depth;
5246 p->replacement_value = 0;
5247 p->implicit = implicit;
5248 p->incremental = constructor_incremental;
5250 p->next = constructor_stack;
5251 constructor_stack = p;
5253 constructor_constant = 1;
5254 constructor_simple = 1;
5255 constructor_depth = SPELLING_DEPTH ();
5256 constructor_elements = 0;
5257 constructor_pending_elts = 0;
5259 /* Don't die if an entire brace-pair level is superfluous
5260 in the containing level. */
5261 if (constructor_type == 0)
5263 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5264 || TREE_CODE (constructor_type) == UNION_TYPE)
5266 /* Don't die if there are extra init elts at the end. */
5267 if (constructor_fields == 0)
5268 constructor_type = 0;
5271 constructor_type = TREE_TYPE (constructor_fields);
5272 push_member_name (constructor_fields);
5273 if (constructor_fields != constructor_unfilled_fields)
5274 constructor_incremental = 0;
5277 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5279 constructor_type = TREE_TYPE (constructor_type);
5280 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5281 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5282 || constructor_range_end != 0)
5283 constructor_incremental = 0;
5286 if (constructor_type == 0)
5288 error_init ("extra brace group at end of initializer%s",
5290 constructor_fields = 0;
5291 constructor_unfilled_fields = 0;
5295 /* Turn off constructor_incremental if type is a struct with bitfields. */
5296 check_init_type_bitfields (constructor_type);
5298 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5300 missing_braces_mentioned = 1;
5301 warning_init ("missing braces around initializer%s", " for `%s'", NULL);
5304 if (TREE_CODE (constructor_type) == RECORD_TYPE
5305 || TREE_CODE (constructor_type) == UNION_TYPE)
5307 constructor_fields = TYPE_FIELDS (constructor_type);
5308 /* Skip any nameless bit fields atthe beginning. */
5309 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5310 && DECL_NAME (constructor_fields) == 0)
5311 constructor_fields = TREE_CHAIN (constructor_fields);
5312 constructor_unfilled_fields = constructor_fields;
5313 constructor_bit_index = copy_node (integer_zero_node);
5315 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5317 constructor_range_end = 0;
5318 if (TYPE_DOMAIN (constructor_type))
5320 constructor_max_index
5321 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5323 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5326 constructor_index = copy_node (integer_zero_node);
5327 constructor_unfilled_index = copy_node (constructor_index);
5331 warning_init ("braces around scalar initializer%s", " for `%s'", NULL);
5332 constructor_fields = constructor_type;
5333 constructor_unfilled_fields = constructor_type;
5337 /* Don't read a struct incrementally if it has any bitfields,
5338 because the incremental reading code doesn't know how to
5339 handle bitfields yet. */
5342 check_init_type_bitfields (type)
5345 if (TREE_CODE (type) == RECORD_TYPE)
5348 for (tail = TYPE_FIELDS (type); tail;
5349 tail = TREE_CHAIN (tail))
5351 if (DECL_BIT_FIELD (tail)
5352 /* This catches cases like `int foo : 8;'. */
5353 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail)))
5355 constructor_incremental = 0;
5359 check_init_type_bitfields (TREE_TYPE (tail));
5363 else if (TREE_CODE (type) == ARRAY_TYPE)
5364 check_init_type_bitfields (TREE_TYPE (type));
5367 /* At the end of an implicit or explicit brace level,
5368 finish up that level of constructor.
5369 If we were outputting the elements as they are read, return 0
5370 from inner levels (process_init_element ignores that),
5371 but return error_mark_node from the outermost level
5372 (that's what we want to put in DECL_INITIAL).
5373 Otherwise, return a CONSTRUCTOR expression. */
5376 pop_init_level (implicit)
5379 struct constructor_stack *p;
5381 tree constructor = 0;
5385 /* When we come to an explicit close brace,
5386 pop any inner levels that didn't have explicit braces. */
5387 while (constructor_stack->implicit)
5388 process_init_element (pop_init_level (1));
5391 p = constructor_stack;
5393 if (constructor_type != 0)
5394 size = int_size_in_bytes (constructor_type);
5396 /* Now output all pending elements. */
5397 output_pending_init_elements (1);
5399 #if 0 /* c-parse.in warns about {}. */
5400 /* In ANSI, each brace level must have at least one element. */
5401 if (! implicit && pedantic
5402 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5403 ? integer_zerop (constructor_unfilled_index)
5404 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5405 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL);
5408 /* Pad out the end of the structure. */
5410 if (p->replacement_value)
5412 /* If this closes a superfluous brace pair,
5413 just pass out the element between them. */
5414 constructor = p->replacement_value;
5415 /* If this is the top level thing within the initializer,
5416 and it's for a variable, then since we already called
5417 assemble_variable, we must output the value now. */
5418 if (p->next == 0 && constructor_decl != 0
5419 && constructor_incremental)
5421 constructor = digest_init (constructor_type, constructor,
5424 /* If initializing an array of unknown size,
5425 determine the size now. */
5426 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5427 && TYPE_DOMAIN (constructor_type) == 0)
5432 push_obstacks_nochange ();
5433 if (TREE_PERMANENT (constructor_type))
5434 end_temporary_allocation ();
5436 momentary_p = suspend_momentary ();
5438 /* We shouldn't have an incomplete array type within
5440 if (constructor_stack->next)
5444 = complete_array_type (constructor_type,
5449 size = int_size_in_bytes (constructor_type);
5450 resume_momentary (momentary_p);
5454 output_constant (constructor, size);
5457 else if (constructor_type == 0)
5459 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5460 && TREE_CODE (constructor_type) != UNION_TYPE
5461 && TREE_CODE (constructor_type) != ARRAY_TYPE
5462 && ! constructor_incremental)
5464 /* A nonincremental scalar initializer--just return
5465 the element, after verifying there is just one. */
5466 if (constructor_elements == 0)
5468 error_init ("empty scalar initializer%s",
5470 constructor = error_mark_node;
5472 else if (TREE_CHAIN (constructor_elements) != 0)
5474 error_init ("extra elements in scalar initializer%s",
5476 constructor = TREE_VALUE (constructor_elements);
5479 constructor = TREE_VALUE (constructor_elements);
5481 else if (! constructor_incremental)
5483 if (constructor_erroneous)
5484 constructor = error_mark_node;
5487 int momentary = suspend_momentary ();
5489 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5490 nreverse (constructor_elements));
5491 if (constructor_constant)
5492 TREE_CONSTANT (constructor) = 1;
5493 if (constructor_constant && constructor_simple)
5494 TREE_STATIC (constructor) = 1;
5496 resume_momentary (momentary);
5502 int momentary = suspend_momentary ();
5504 if (TREE_CODE (constructor_type) == RECORD_TYPE
5505 || TREE_CODE (constructor_type) == UNION_TYPE)
5507 /* Find the offset of the end of that field. */
5508 filled = size_binop (CEIL_DIV_EXPR,
5509 constructor_bit_index,
5510 size_int (BITS_PER_UNIT));
5512 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5514 /* If initializing an array of unknown size,
5515 determine the size now. */
5516 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5517 && TYPE_DOMAIN (constructor_type) == 0)
5520 = size_binop (MINUS_EXPR,
5521 constructor_unfilled_index,
5524 push_obstacks_nochange ();
5525 if (TREE_PERMANENT (constructor_type))
5526 end_temporary_allocation ();
5527 maxindex = copy_node (maxindex);
5528 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5529 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5531 /* TYPE_MAX_VALUE is always one less than the number of elements
5532 in the array, because we start counting at zero. Therefore,
5533 warn only if the value is less than zero. */
5535 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5537 error_with_decl (constructor_decl,
5538 "zero or negative array size `%s'");
5539 layout_type (constructor_type);
5540 size = int_size_in_bytes (constructor_type);
5544 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5545 size_in_bytes (TREE_TYPE (constructor_type)));
5551 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5553 resume_momentary (momentary);
5557 constructor_type = p->type;
5558 constructor_fields = p->fields;
5559 constructor_index = p->index;
5560 constructor_range_end = p->range_end;
5561 constructor_max_index = p->max_index;
5562 constructor_unfilled_index = p->unfilled_index;
5563 constructor_unfilled_fields = p->unfilled_fields;
5564 constructor_bit_index = p->bit_index;
5565 constructor_elements = p->elements;
5566 constructor_constant = p->constant;
5567 constructor_simple = p->simple;
5568 constructor_erroneous = p->erroneous;
5569 constructor_pending_elts = p->pending_elts;
5570 constructor_depth = p->depth;
5571 constructor_incremental = p->incremental;
5572 RESTORE_SPELLING_DEPTH (constructor_depth);
5574 constructor_stack = p->next;
5577 if (constructor == 0)
5579 if (constructor_stack == 0)
5580 return error_mark_node;
5586 /* Within an array initializer, specify the next index to be initialized.
5587 FIRST is that index. If LAST is nonzero, then initialize a range
5588 of indices, running from FIRST through LAST. */
5591 set_init_index (first, last)
5594 while ((TREE_CODE (first) == NOP_EXPR
5595 || TREE_CODE (first) == CONVERT_EXPR
5596 || TREE_CODE (first) == NON_LVALUE_EXPR)
5597 && (TYPE_MODE (TREE_TYPE (first))
5598 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5599 (first) = TREE_OPERAND (first, 0);
5601 while ((TREE_CODE (last) == NOP_EXPR
5602 || TREE_CODE (last) == CONVERT_EXPR
5603 || TREE_CODE (last) == NON_LVALUE_EXPR)
5604 && (TYPE_MODE (TREE_TYPE (last))
5605 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5606 (last) = TREE_OPERAND (last, 0);
5608 if (TREE_CODE (first) != INTEGER_CST)
5609 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5610 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5611 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5612 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5613 error_init ("duplicate array index in initializer%s", " for `%s'", NULL);
5616 TREE_INT_CST_LOW (constructor_index)
5617 = TREE_INT_CST_LOW (first);
5618 TREE_INT_CST_HIGH (constructor_index)
5619 = TREE_INT_CST_HIGH (first);
5621 if (last != 0 && tree_int_cst_lt (last, first))
5622 error_init ("empty index range in initializer%s", " for `%s'", NULL);
5626 pedwarn ("ANSI C forbids specifying element to initialize");
5627 constructor_range_end = last;
5632 /* Within a struct initializer, specify the next field to be initialized. */
5635 set_init_label (fieldname)
5641 for (tail = TYPE_FIELDS (constructor_type); tail;
5642 tail = TREE_CHAIN (tail))
5644 if (tail == constructor_unfilled_fields)
5646 if (DECL_NAME (tail) == fieldname)
5651 error ("unknown field `%s' specified in initializer",
5652 IDENTIFIER_POINTER (fieldname));
5654 error ("field `%s' already initialized",
5655 IDENTIFIER_POINTER (fieldname));
5658 constructor_fields = tail;
5660 pedwarn ("ANSI C forbids specifying structure member to initialize");
5664 /* "Output" the next constructor element.
5665 At top level, really output it to assembler code now.
5666 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5667 TYPE is the data type that the containing data type wants here.
5668 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5670 PENDING if non-nil means output pending elements that belong
5671 right after this element. (PENDING is normally 1;
5672 it is 0 while outputting pending elements, to avoid recursion.) */
5675 output_init_element (value, type, field, pending)
5676 tree value, type, field;
5681 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5682 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5683 && !(TREE_CODE (value) == STRING_CST
5684 && TREE_CODE (type) == ARRAY_TYPE
5685 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5686 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5687 TYPE_MAIN_VARIANT (type))))
5688 value = default_conversion (value);
5690 if (value == error_mark_node)
5691 constructor_erroneous = 1;
5692 else if (!TREE_CONSTANT (value))
5693 constructor_constant = 0;
5694 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5695 constructor_simple = 0;
5697 if (require_constant_value && ! TREE_CONSTANT (value))
5699 error_init ("initializer element%s is not constant",
5701 value = error_mark_node;
5703 else if (require_constant_elements
5704 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5706 error_init ("initializer element%s is not computable at load time",
5708 value = error_mark_node;
5711 /* If this element duplicates one on constructor_pending_elts,
5712 print a message and ignore it. Don't do this when we're
5713 processing elements taken off constructor_pending_elts,
5714 because we'd always get spurious errors. */
5717 if (TREE_CODE (constructor_type) == RECORD_TYPE
5718 || TREE_CODE (constructor_type) == UNION_TYPE)
5720 if (purpose_member (field, constructor_pending_elts))
5722 error_init ("duplicate initializer%s", " for `%s'", NULL);
5726 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5729 for (tail = constructor_pending_elts; tail;
5730 tail = TREE_CHAIN (tail))
5731 if (TREE_PURPOSE (tail) != 0
5732 && TREE_CODE (TREE_PURPOSE (tail)) == INTEGER_CST
5733 && tree_int_cst_equal (TREE_PURPOSE (tail), constructor_index))
5738 error_init ("duplicate initializer%s", " for `%s'", NULL);
5744 /* If this element doesn't come next in sequence,
5745 put it on constructor_pending_elts. */
5746 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5747 && !tree_int_cst_equal (field, constructor_unfilled_index))
5750 /* The copy_node is needed in case field is actually
5751 constructor_index, which is modified in place. */
5752 constructor_pending_elts
5753 = tree_cons (copy_node (field),
5754 digest_init (type, value, 0, 0),
5755 constructor_pending_elts);
5757 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5758 && field != constructor_unfilled_fields)
5760 /* We do this for records but not for unions. In a union,
5761 no matter which field is specified, it can be initialized
5762 right away since it starts at the beginning of the union. */
5764 constructor_pending_elts
5766 digest_init (type, value, 0, 0),
5767 constructor_pending_elts);
5771 /* Otherwise, output this element either to
5772 constructor_elements or to the assembler file. */
5776 if (! constructor_incremental)
5778 if (field && TREE_CODE (field) == INTEGER_CST)
5779 field = copy_node (field);
5780 constructor_elements
5781 = tree_cons (field, digest_init (type, value, 0, 0),
5782 constructor_elements);
5786 /* Structure elements may require alignment.
5787 Do this, if necessary. */
5788 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5790 /* Advance to offset of this element. */
5791 if (! tree_int_cst_equal (constructor_bit_index,
5792 DECL_FIELD_BITPOS (field)))
5794 int next = (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
5796 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5799 assemble_zeros (next - here);
5802 output_constant (digest_init (type, value, 0, 0),
5803 int_size_in_bytes (type));
5805 /* For a record or union,
5806 keep track of end position of last field. */
5807 if (TREE_CODE (constructor_type) == RECORD_TYPE
5808 || TREE_CODE (constructor_type) == UNION_TYPE)
5810 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
5812 TREE_INT_CST_LOW (constructor_bit_index)
5813 = TREE_INT_CST_LOW (temp);
5814 TREE_INT_CST_HIGH (constructor_bit_index)
5815 = TREE_INT_CST_HIGH (temp);
5820 /* Advance the variable that indicates sequential elements output. */
5821 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5823 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
5825 TREE_INT_CST_LOW (constructor_unfilled_index)
5826 = TREE_INT_CST_LOW (tem);
5827 TREE_INT_CST_HIGH (constructor_unfilled_index)
5828 = TREE_INT_CST_HIGH (tem);
5830 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5831 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5832 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5833 constructor_unfilled_fields = 0;
5835 /* Now output any pending elements which have become next. */
5837 output_pending_init_elements (0);
5841 /* Output any pending elements which have become next.
5842 As we output elements, constructor_unfilled_{fields,index}
5843 advances, which may cause other elements to become next;
5844 if so, they too are output.
5846 If ALL is 0, we return when there are
5847 no more pending elements to output now.
5849 If ALL is 1, we output space as necessary so that
5850 we can output all the pending elements. */
5853 output_pending_init_elements (all)
5861 /* Look thru the whole pending list.
5862 If we find an element that should be output now,
5863 output it. Otherwise, set NEXT to the element
5864 that comes first among those still pending. */
5867 for (tail = constructor_pending_elts; tail;
5868 tail = TREE_CHAIN (tail))
5870 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5872 if (tree_int_cst_equal (TREE_PURPOSE (tail),
5873 constructor_unfilled_index))
5875 output_init_element (TREE_VALUE (tail),
5876 TREE_TYPE (constructor_type),
5877 constructor_unfilled_index, 0);
5880 else if (tree_int_cst_lt (TREE_PURPOSE (tail),
5881 constructor_unfilled_index))
5884 || tree_int_cst_lt (TREE_PURPOSE (tail), next))
5885 next = TREE_PURPOSE (tail);
5887 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5888 || TREE_CODE (constructor_type) == UNION_TYPE)
5890 if (TREE_PURPOSE (tail) == constructor_unfilled_fields)
5892 output_init_element (TREE_VALUE (tail),
5893 TREE_TYPE (constructor_unfilled_fields),
5894 constructor_unfilled_fields,
5898 else if (constructor_unfilled_fields == 0
5899 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
5900 DECL_FIELD_BITPOS (constructor_unfilled_fields)))
5903 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
5904 DECL_FIELD_BITPOS (next)))
5905 next = TREE_PURPOSE (tail);
5909 /* Ordinarily return, but not if we want to output all
5910 and there are elements left. */
5911 if (! (all && next != 0))
5914 /* Generate space up to the position of NEXT. */
5915 if (constructor_incremental)
5918 tree nextpos_tree = size_int (0);
5920 if (TREE_CODE (constructor_type) == RECORD_TYPE
5921 || TREE_CODE (constructor_type) == UNION_TYPE)
5923 /* Find the last field written out, if any. */
5924 for (tail = TYPE_FIELDS (constructor_type); tail;
5925 tail = TREE_CHAIN (tail))
5926 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
5930 /* Find the offset of the end of that field. */
5931 filled = size_binop (CEIL_DIV_EXPR,
5932 size_binop (PLUS_EXPR,
5933 DECL_FIELD_BITPOS (tail),
5935 size_int (BITS_PER_UNIT));
5937 filled = size_int (0);
5939 nextpos_tree = size_binop (CEIL_DIV_EXPR,
5940 DECL_FIELD_BITPOS (next),
5941 size_int (BITS_PER_UNIT));
5943 TREE_INT_CST_HIGH (constructor_bit_index)
5944 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
5945 TREE_INT_CST_LOW (constructor_bit_index)
5946 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
5947 constructor_unfilled_fields = next;
5949 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5951 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5952 size_in_bytes (TREE_TYPE (constructor_type)));
5954 = size_binop (MULT_EXPR, next,
5955 size_in_bytes (TREE_TYPE (constructor_type)));
5956 TREE_INT_CST_LOW (constructor_unfilled_index)
5957 = TREE_INT_CST_LOW (next);
5958 TREE_INT_CST_HIGH (constructor_unfilled_index)
5959 = TREE_INT_CST_HIGH (next);
5966 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
5968 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
5973 /* If it's not incremental, just skip over the gap,
5974 so that after jumping to retry we will output the next
5975 successive element. */
5976 if (TREE_CODE (constructor_type) == RECORD_TYPE
5977 || TREE_CODE (constructor_type) == UNION_TYPE)
5978 constructor_unfilled_fields = next;
5979 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5981 TREE_INT_CST_LOW (constructor_unfilled_index)
5982 = TREE_INT_CST_LOW (next);
5983 TREE_INT_CST_HIGH (constructor_unfilled_index)
5984 = TREE_INT_CST_HIGH (next);
5991 /* Add one non-braced element to the current constructor level.
5992 This adjusts the current position within the constructor's type.
5993 This may also start or terminate implicit levels
5994 to handle a partly-braced initializer.
5996 Once this has found the correct level for the new element,
5997 it calls output_init_element.
5999 Note: if we are incrementally outputting this constructor,
6000 this function may be called with a null argument
6001 representing a sub-constructor that was already incrementally output.
6002 When that happens, we output nothing, but we do the bookkeeping
6003 to skip past that element of the current constructor. */
6006 process_init_element (value)
6009 tree orig_value = value;
6010 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6012 /* Handle superfluous braces around string cst as in
6013 char x[] = {"foo"}; */
6016 && TREE_CODE (constructor_type) == ARRAY_TYPE
6017 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6018 && integer_zerop (constructor_unfilled_index))
6020 constructor_stack->replacement_value = value;
6024 if (constructor_stack->replacement_value != 0)
6026 error_init ("excess elements in struct initializer%s",
6027 " after `%s'", NULL_PTR);
6031 /* Ignore elements of a brace group if it is entirely superfluous
6032 and has already been diagnosed. */
6033 if (constructor_type == 0)
6036 /* If we've exhausted any levels that didn't have braces,
6038 while (constructor_stack->implicit)
6040 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6041 || TREE_CODE (constructor_type) == UNION_TYPE)
6042 && constructor_fields == 0)
6043 process_init_element (pop_init_level (1));
6044 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6045 && tree_int_cst_lt (constructor_max_index, constructor_index))
6046 process_init_element (pop_init_level (1));
6053 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6056 enum tree_code fieldcode;
6058 if (constructor_fields == 0)
6060 pedwarn_init ("excess elements in struct initializer%s",
6061 " after `%s'", NULL_PTR);
6065 fieldtype = TREE_TYPE (constructor_fields);
6066 if (fieldtype != error_mark_node)
6067 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6068 fieldcode = TREE_CODE (fieldtype);
6070 /* Accept a string constant to initialize a subarray. */
6072 && fieldcode == ARRAY_TYPE
6073 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6076 /* Otherwise, if we have come to a subaggregate,
6077 and we don't have an element of its type, push into it. */
6078 else if (value != 0 && !constructor_no_implicit
6079 && value != error_mark_node
6080 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6081 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6082 || fieldcode == UNION_TYPE))
6084 push_init_level (1);
6090 push_member_name (constructor_fields);
6091 output_init_element (value, fieldtype, constructor_fields, 1);
6092 RESTORE_SPELLING_DEPTH (constructor_depth);
6095 /* Do the bookkeeping for an element that was
6096 directly output as a constructor. */
6098 /* For a record, keep track of end position of last field. */
6099 tree temp = size_binop (PLUS_EXPR,
6100 DECL_FIELD_BITPOS (constructor_fields),
6101 DECL_SIZE (constructor_fields));
6102 TREE_INT_CST_LOW (constructor_bit_index)
6103 = TREE_INT_CST_LOW (temp);
6104 TREE_INT_CST_HIGH (constructor_bit_index)
6105 = TREE_INT_CST_HIGH (temp);
6107 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6110 constructor_fields = TREE_CHAIN (constructor_fields);
6111 /* Skip any nameless bit fields atthe beginning. */
6112 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
6113 && DECL_NAME (constructor_fields) == 0)
6114 constructor_fields = TREE_CHAIN (constructor_fields);
6117 if (TREE_CODE (constructor_type) == UNION_TYPE)
6120 enum tree_code fieldcode;
6122 if (constructor_fields == 0)
6124 pedwarn_init ("excess elements in union initializer%s",
6125 " after `%s'", NULL_PTR);
6129 fieldtype = TREE_TYPE (constructor_fields);
6130 if (fieldtype != error_mark_node)
6131 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6132 fieldcode = TREE_CODE (fieldtype);
6134 /* Accept a string constant to initialize a subarray. */
6136 && fieldcode == ARRAY_TYPE
6137 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6140 /* Otherwise, if we have come to a subaggregate,
6141 and we don't have an element of its type, push into it. */
6142 else if (value != 0 && !constructor_no_implicit
6143 && value != error_mark_node
6144 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6145 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6146 || fieldcode == UNION_TYPE))
6148 push_init_level (1);
6154 push_member_name (constructor_fields);
6155 output_init_element (value, fieldtype, constructor_fields, 1);
6156 RESTORE_SPELLING_DEPTH (constructor_depth);
6159 /* Do the bookkeeping for an element that was
6160 directly output as a constructor. */
6162 TREE_INT_CST_LOW (constructor_bit_index)
6163 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6164 TREE_INT_CST_HIGH (constructor_bit_index)
6165 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6167 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6170 constructor_fields = 0;
6173 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6175 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6176 enum tree_code eltcode = TREE_CODE (elttype);
6178 /* Accept a string constant to initialize a subarray. */
6180 && eltcode == ARRAY_TYPE
6181 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6184 /* Otherwise, if we have come to a subaggregate,
6185 and we don't have an element of its type, push into it. */
6186 else if (value != 0 && !constructor_no_implicit
6187 && value != error_mark_node
6188 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6189 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6190 || eltcode == UNION_TYPE))
6192 push_init_level (1);
6196 if (constructor_max_index != 0
6197 && tree_int_cst_lt (constructor_max_index, constructor_index))
6199 pedwarn_init ("excess elements in array initializer%s",
6200 " after `%s'", NULL_PTR);
6204 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */
6205 if (constructor_range_end)
6206 value = save_expr (value);
6208 /* Now output the actual element.
6209 Ordinarily, output once.
6210 If there is a range, repeat it till we advance past the range. */
6217 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6218 output_init_element (value, elttype, constructor_index, 1);
6219 RESTORE_SPELLING_DEPTH (constructor_depth);
6222 tem = size_binop (PLUS_EXPR, constructor_index,
6224 TREE_INT_CST_LOW (constructor_index)
6225 = TREE_INT_CST_LOW (tem);
6226 TREE_INT_CST_HIGH (constructor_index)
6227 = TREE_INT_CST_HIGH (tem);
6230 /* If we are doing the bookkeeping for an element that was
6231 directly output as a constructor,
6232 we must update constructor_unfilled_index. */
6234 TREE_INT_CST_LOW (constructor_unfilled_index)
6235 = TREE_INT_CST_LOW (constructor_index);
6236 TREE_INT_CST_HIGH (constructor_unfilled_index)
6237 = TREE_INT_CST_HIGH (constructor_index);
6240 while (! (constructor_range_end == 0
6241 || tree_int_cst_lt (constructor_range_end,
6242 constructor_index)));
6247 /* Handle the sole element allowed in a braced initializer
6248 for a scalar variable. */
6249 if (constructor_fields == 0)
6251 pedwarn_init ("excess elements in scalar initializer%s",
6252 " after `%s'", NULL_PTR);
6257 output_init_element (value, constructor_type, NULL_TREE, 1);
6258 constructor_fields = 0;
6262 /* If the (lexically) previous elments are not now saved,
6263 we can discard the storage for them. */
6264 if (constructor_incremental && constructor_pending_elts == 0 && value != 0)
6268 /* Expand an ASM statement with operands, handling output operands
6269 that are not variables or INDIRECT_REFS by transforming such
6270 cases into cases that expand_asm_operands can handle.
6272 Arguments are same as for expand_asm_operands. */
6275 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6276 tree string, outputs, inputs, clobbers;
6281 int noutputs = list_length (outputs);
6283 /* o[I] is the place that output number I should be written. */
6284 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6287 if (TREE_CODE (string) == ADDR_EXPR)
6288 string = TREE_OPERAND (string, 0);
6289 if (TREE_CODE (string) != STRING_CST)
6291 error ("asm template is not a string constant");
6295 /* Record the contents of OUTPUTS before it is modified. */
6296 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6297 o[i] = TREE_VALUE (tail);
6299 /* Perform default conversions on array and function inputs. */
6300 /* Don't do this for other types--
6301 it would screw up operands expected to be in memory. */
6302 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6303 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6304 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6305 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6307 /* Generate the ASM_OPERANDS insn;
6308 store into the TREE_VALUEs of OUTPUTS some trees for
6309 where the values were actually stored. */
6310 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6312 /* Copy all the intermediate outputs into the specified outputs. */
6313 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6315 if (o[i] != TREE_VALUE (tail))
6317 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6321 /* Detect modification of read-only values.
6322 (Otherwise done by build_modify_expr.) */
6325 tree type = TREE_TYPE (o[i]);
6326 if (TYPE_READONLY (type)
6327 || ((TREE_CODE (type) == RECORD_TYPE
6328 || TREE_CODE (type) == UNION_TYPE)
6329 && C_TYPE_FIELDS_READONLY (type)))
6330 readonly_warning (o[i], "modification by `asm'");
6334 /* Those MODIFY_EXPRs could do autoincrements. */
6338 /* Expand a C `return' statement.
6339 RETVAL is the expression for what to return,
6340 or a null pointer for `return;' with no value. */
6343 c_expand_return (retval)
6346 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6348 if (TREE_THIS_VOLATILE (current_function_decl))
6349 warning ("function declared `noreturn' has a `return' statement");
6353 current_function_returns_null = 1;
6354 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6355 warning ("`return' with no value, in function returning non-void");
6356 expand_null_return ();
6358 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6360 current_function_returns_null = 1;
6361 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6362 pedwarn ("`return' with a value, in function returning void");
6363 expand_return (retval);
6367 tree t = convert_for_assignment (valtype, retval, "return",
6368 NULL_TREE, NULL_TREE, 0);
6369 tree res = DECL_RESULT (current_function_decl);
6372 if (t == error_mark_node)
6375 inner = t = convert (TREE_TYPE (res), t);
6377 /* Strip any conversions, additions, and subtractions, and see if
6378 we are returning the address of a local variable. Warn if so. */
6381 switch (TREE_CODE (inner))
6383 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6385 inner = TREE_OPERAND (inner, 0);
6389 /* If the second operand of the MINUS_EXPR has a pointer
6390 type (or is converted from it), this may be valid, so
6391 don't give a warning. */
6393 tree op1 = TREE_OPERAND (inner, 1);
6395 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6396 && (TREE_CODE (op1) == NOP_EXPR
6397 || TREE_CODE (op1) == NON_LVALUE_EXPR
6398 || TREE_CODE (op1) == CONVERT_EXPR))
6399 op1 = TREE_OPERAND (op1, 0);
6401 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6404 inner = TREE_OPERAND (inner, 0);
6409 inner = TREE_OPERAND (inner, 0);
6411 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6412 inner = TREE_OPERAND (inner, 0);
6414 if (TREE_CODE (inner) == VAR_DECL
6415 && ! DECL_EXTERNAL (inner)
6416 && ! TREE_STATIC (inner)
6417 && DECL_CONTEXT (inner) == current_function_decl)
6418 warning ("function returns address of local variable");
6425 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6426 TREE_SIDE_EFFECTS (t) = 1;
6428 current_function_returns_value = 1;
6432 /* Start a C switch statement, testing expression EXP.
6433 Return EXP if it is valid, an error node otherwise. */
6436 c_expand_start_case (exp)
6439 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6440 tree type = TREE_TYPE (exp);
6442 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6444 error ("switch quantity not an integer");
6445 exp = error_mark_node;
6450 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6452 if (warn_traditional
6453 && (type == long_integer_type_node
6454 || type == long_unsigned_type_node))
6455 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6457 exp = default_conversion (exp);
6458 type = TREE_TYPE (exp);
6459 index = get_unwidened (exp, NULL_TREE);
6460 /* We can't strip a conversion from a signed type to an unsigned,
6461 because if we did, int_fits_type_p would do the wrong thing
6462 when checking case values for being in range,
6463 and it's too hard to do the right thing. */
6464 if (TREE_UNSIGNED (TREE_TYPE (exp))
6465 == TREE_UNSIGNED (TREE_TYPE (index)))
6469 expand_start_case (1, exp, type, "switch statement");