1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 88, 91, 92, 93, 1994 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). */
36 /* Nonzero if we've already printed a "missing braces around initializer"
37 message within this initializer. */
38 static int missing_braces_mentioned;
40 extern char *index ();
41 extern char *rindex ();
43 int mark_addressable ();
44 static tree convert_for_assignment ();
45 static void warn_for_assignment ();
46 static int function_types_compatible_p ();
47 static int type_lists_compatible_p ();
48 int self_promoting_args_p ();
49 static int self_promoting_type_p ();
50 static int comp_target_types ();
51 static tree pointer_int_sum ();
52 static tree pointer_diff ();
53 static tree convert_sequence ();
54 static tree unary_complex_lvalue ();
55 static tree process_init_constructor ();
56 static tree convert_arguments ();
57 static char *get_spelling ();
58 static tree digest_init ();
59 static void pedantic_lvalue_warning ();
60 tree truthvalue_conversion ();
61 void incomplete_type_error ();
62 void readonly_warning ();
63 static tree internal_build_compound_expr ();
65 void process_init_element ();
67 /* Do `exp = require_complete_type (exp);' to make sure exp
68 does not have an incomplete type. (That includes void types.) */
71 require_complete_type (value)
74 tree type = TREE_TYPE (value);
76 /* First, detect a valid value with a complete type. */
77 if (TYPE_SIZE (type) != 0
78 && type != void_type_node)
81 incomplete_type_error (value, type);
82 return error_mark_node;
85 /* Print an error message for invalid use of an incomplete type.
86 VALUE is the expression that was used (or 0 if that isn't known)
87 and TYPE is the type that was invalid. */
90 incomplete_type_error (value, type)
96 /* Avoid duplicate error message. */
97 if (TREE_CODE (type) == ERROR_MARK)
100 if (value != 0 && (TREE_CODE (value) == VAR_DECL
101 || TREE_CODE (value) == PARM_DECL))
102 error ("`%s' has an incomplete type",
103 IDENTIFIER_POINTER (DECL_NAME (value)));
107 /* We must print an error message. Be clever about what it says. */
109 switch (TREE_CODE (type))
112 errmsg = "invalid use of undefined type `struct %s'";
116 errmsg = "invalid use of undefined type `union %s'";
120 errmsg = "invalid use of undefined type `enum %s'";
124 error ("invalid use of void expression");
128 if (TYPE_DOMAIN (type))
130 type = TREE_TYPE (type);
133 error ("invalid use of array with unspecified bounds");
140 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
141 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
143 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
144 error ("invalid use of incomplete typedef `%s'",
145 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
149 /* Return a variant of TYPE which has all the type qualifiers of LIKE
150 as well as those of TYPE. */
153 qualify_type (type, like)
156 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
157 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
158 return c_build_type_variant (type, constflag, volflag);
161 /* Return the common type of two types.
162 We assume that comptypes has already been done and returned 1;
163 if that isn't so, this may crash. In particular, we assume that qualifiers
166 This is the type for the result of most arithmetic operations
167 if the operands have the given two types. */
173 register enum tree_code code1;
174 register enum tree_code code2;
176 /* Save time if the two types are the same. */
178 if (t1 == t2) return t1;
180 /* If one type is nonsense, use the other. */
181 if (t1 == error_mark_node)
183 if (t2 == error_mark_node)
186 /* Treat an enum type as the unsigned integer type of the same width. */
188 if (TREE_CODE (t1) == ENUMERAL_TYPE)
189 t1 = type_for_size (TYPE_PRECISION (t1), 1);
190 if (TREE_CODE (t2) == ENUMERAL_TYPE)
191 t2 = type_for_size (TYPE_PRECISION (t2), 1);
193 code1 = TREE_CODE (t1);
194 code2 = TREE_CODE (t2);
196 /* If one type is complex, form the common type of the non-complex
197 components, then make that complex. Use T1 or T2 if it is the
199 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
201 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
202 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
203 tree subtype = common_type (subtype1, subtype2);
205 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
207 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
210 return build_complex_type (subtype);
217 /* If only one is real, use it as the result. */
219 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
222 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
225 /* Both real or both integers; use the one with greater precision. */
227 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
229 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
232 /* Same precision. Prefer longs to ints even when same size. */
234 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
235 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
236 return long_unsigned_type_node;
238 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
239 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
241 /* But preserve unsignedness from the other type,
242 since long cannot hold all the values of an unsigned int. */
243 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
244 return long_unsigned_type_node;
245 return long_integer_type_node;
248 /* Otherwise prefer the unsigned one. */
250 if (TREE_UNSIGNED (t1))
255 /* For two pointers, do this recursively on the target type,
256 and combine the qualifiers of the two types' targets. */
257 /* This code was turned off; I don't know why.
258 But ANSI C specifies doing this with the qualifiers.
259 So I turned it on again. */
261 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
262 TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
264 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
266 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
267 return build_pointer_type (c_build_type_variant (target, constp, volatilep));
270 return build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
275 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
276 /* Save space: see if the result is identical to one of the args. */
277 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
279 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
281 /* Merge the element types, and have a size if either arg has one. */
282 return build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
286 /* Function types: prefer the one that specified arg types.
287 If both do, merge the arg types. Also merge the return types. */
289 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
290 tree p1 = TYPE_ARG_TYPES (t1);
291 tree p2 = TYPE_ARG_TYPES (t2);
296 /* Save space: see if the result is identical to one of the args. */
297 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
299 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
302 /* Simple way if one arg fails to specify argument types. */
303 if (TYPE_ARG_TYPES (t1) == 0)
304 return build_function_type (valtype, TYPE_ARG_TYPES (t2));
305 if (TYPE_ARG_TYPES (t2) == 0)
306 return build_function_type (valtype, TYPE_ARG_TYPES (t1));
308 /* If both args specify argument types, we must merge the two
309 lists, argument by argument. */
311 len = list_length (p1);
314 for (i = 0; i < len; i++)
315 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
320 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
322 /* A null type means arg type is not specified.
323 Take whatever the other function type has. */
324 if (TREE_VALUE (p1) == 0)
326 TREE_VALUE (n) = TREE_VALUE (p2);
329 if (TREE_VALUE (p2) == 0)
331 TREE_VALUE (n) = TREE_VALUE (p1);
335 /* Given wait (union {union wait *u; int *i} *)
336 and wait (union wait *),
337 prefer union wait * as type of parm. */
338 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
339 && TREE_VALUE (p1) != TREE_VALUE (p2))
342 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
343 memb; memb = TREE_CHAIN (memb))
344 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
346 TREE_VALUE (n) = TREE_VALUE (p2);
348 pedwarn ("function types not truly compatible in ANSI C");
352 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
353 && TREE_VALUE (p2) != TREE_VALUE (p1))
356 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
357 memb; memb = TREE_CHAIN (memb))
358 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
360 TREE_VALUE (n) = TREE_VALUE (p1);
362 pedwarn ("function types not truly compatible in ANSI C");
366 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
370 return build_function_type (valtype, newargs);
379 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
380 or various other operations. Return 2 if they are compatible
381 but a warning may be needed if you use them together. */
384 comptypes (type1, type2)
387 register tree t1 = type1;
388 register tree t2 = type2;
390 /* Suppress errors caused by previously reported errors. */
392 if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
395 /* Treat an enum type as the unsigned integer type of the same width. */
397 if (TREE_CODE (t1) == ENUMERAL_TYPE)
398 t1 = type_for_size (TYPE_PRECISION (t1), 1);
399 if (TREE_CODE (t2) == ENUMERAL_TYPE)
400 t2 = type_for_size (TYPE_PRECISION (t2), 1);
405 /* Different classes of types can't be compatible. */
407 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
409 /* Qualifiers must match. */
411 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
413 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
416 /* Allow for two different type nodes which have essentially the same
417 definition. Note that we already checked for equality of the type
418 type qualifiers (just above). */
420 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
423 switch (TREE_CODE (t1))
426 return (TREE_TYPE (t1) == TREE_TYPE (t2)
427 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
430 return function_types_compatible_p (t1, t2);
434 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
436 tree d1 = TYPE_DOMAIN (t1);
437 tree d2 = TYPE_DOMAIN (t2);
439 /* Target types must match incl. qualifiers. */
440 if (TREE_TYPE (t1) != TREE_TYPE (t2)
441 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
444 /* Sizes must match unless one is missing or variable. */
445 if (d1 == 0 || d2 == 0 || d1 == d2
446 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
447 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
448 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
449 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
452 return (((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
453 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
454 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
455 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
456 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
457 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
458 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
459 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))))
464 if (maybe_objc_comptypes (t1, t2, 0) == 1)
470 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
471 ignoring their qualifiers. */
474 comp_target_types (ttl, ttr)
479 /* Give maybe_objc_comptypes a crack at letting these types through. */
480 if (val = maybe_objc_comptypes (ttl, ttr, 1) >= 0)
483 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
484 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
486 if (val == 2 && pedantic)
487 pedwarn ("types are not quite compatible");
491 /* Subroutines of `comptypes'. */
493 /* Return 1 if two function types F1 and F2 are compatible.
494 If either type specifies no argument types,
495 the other must specify a fixed number of self-promoting arg types.
496 Otherwise, if one type specifies only the number of arguments,
497 the other must specify that number of self-promoting arg types.
498 Otherwise, the argument types must match. */
501 function_types_compatible_p (f1, f2)
505 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
509 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
510 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
513 args1 = TYPE_ARG_TYPES (f1);
514 args2 = TYPE_ARG_TYPES (f2);
516 /* An unspecified parmlist matches any specified parmlist
517 whose argument types don't need default promotions. */
521 if (!self_promoting_args_p (args2))
523 /* If one of these types comes from a non-prototype fn definition,
524 compare that with the other type's arglist.
525 If they don't match, ask for a warning (but no error). */
526 if (TYPE_ACTUAL_ARG_TYPES (f1)
527 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
533 if (!self_promoting_args_p (args1))
535 if (TYPE_ACTUAL_ARG_TYPES (f2)
536 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
541 /* Both types have argument lists: compare them and propagate results. */
542 val1 = type_lists_compatible_p (args1, args2);
543 return val1 != 1 ? val1 : val;
546 /* Check two lists of types for compatibility,
547 returning 0 for incompatible, 1 for compatible,
548 or 2 for compatible with warning. */
551 type_lists_compatible_p (args1, args2)
554 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
560 if (args1 == 0 && args2 == 0)
562 /* If one list is shorter than the other,
563 they fail to match. */
564 if (args1 == 0 || args2 == 0)
566 /* A null pointer instead of a type
567 means there is supposed to be an argument
568 but nothing is specified about what type it has.
569 So match anything that self-promotes. */
570 if (TREE_VALUE (args1) == 0)
572 if (! self_promoting_type_p (TREE_VALUE (args2)))
575 else if (TREE_VALUE (args2) == 0)
577 if (! self_promoting_type_p (TREE_VALUE (args1)))
580 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
582 /* Allow wait (union {union wait *u; int *i} *)
583 and wait (union wait *) to be compatible. */
584 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
585 && TYPE_NAME (TREE_VALUE (args1)) == 0
586 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
587 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
588 TYPE_SIZE (TREE_VALUE (args2))))
591 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
592 memb; memb = TREE_CHAIN (memb))
593 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
598 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
599 && TYPE_NAME (TREE_VALUE (args2)) == 0
600 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
601 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
602 TYPE_SIZE (TREE_VALUE (args1))))
605 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
606 memb; memb = TREE_CHAIN (memb))
607 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
616 /* comptypes said ok, but record if it said to warn. */
620 args1 = TREE_CHAIN (args1);
621 args2 = TREE_CHAIN (args2);
625 /* Return 1 if PARMS specifies a fixed number of parameters
626 and none of their types is affected by default promotions. */
629 self_promoting_args_p (parms)
633 for (t = parms; t; t = TREE_CHAIN (t))
635 register tree type = TREE_VALUE (t);
637 if (TREE_CHAIN (t) == 0 && type != void_type_node)
643 if (TYPE_MAIN_VARIANT (type) == float_type_node)
646 if (C_PROMOTING_INTEGER_TYPE_P (type))
652 /* Return 1 if TYPE is not affected by default promotions. */
655 self_promoting_type_p (type)
658 if (TYPE_MAIN_VARIANT (type) == float_type_node)
661 if (C_PROMOTING_INTEGER_TYPE_P (type))
667 /* Return an unsigned type the same as TYPE in other respects. */
673 tree type1 = TYPE_MAIN_VARIANT (type);
674 if (type1 == signed_char_type_node || type1 == char_type_node)
675 return unsigned_char_type_node;
676 if (type1 == integer_type_node)
677 return unsigned_type_node;
678 if (type1 == short_integer_type_node)
679 return short_unsigned_type_node;
680 if (type1 == long_integer_type_node)
681 return long_unsigned_type_node;
682 if (type1 == long_long_integer_type_node)
683 return long_long_unsigned_type_node;
687 /* Return a signed type the same as TYPE in other respects. */
693 tree type1 = TYPE_MAIN_VARIANT (type);
694 if (type1 == unsigned_char_type_node || type1 == char_type_node)
695 return signed_char_type_node;
696 if (type1 == unsigned_type_node)
697 return integer_type_node;
698 if (type1 == short_unsigned_type_node)
699 return short_integer_type_node;
700 if (type1 == long_unsigned_type_node)
701 return long_integer_type_node;
702 if (type1 == long_long_unsigned_type_node)
703 return long_long_integer_type_node;
707 /* Return a type the same as TYPE except unsigned or
708 signed according to UNSIGNEDP. */
711 signed_or_unsigned_type (unsignedp, type)
715 if (TREE_CODE (type) != INTEGER_TYPE)
717 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
718 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
719 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
720 return unsignedp ? unsigned_type_node : integer_type_node;
721 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
722 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
723 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
724 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
725 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
726 return (unsignedp ? long_long_unsigned_type_node
727 : long_long_integer_type_node);
731 /* Compute the value of the `sizeof' operator. */
737 enum tree_code code = TREE_CODE (type);
740 if (code == FUNCTION_TYPE)
742 if (pedantic || warn_pointer_arith)
743 pedwarn ("sizeof applied to a function type");
746 if (code == VOID_TYPE)
748 if (pedantic || warn_pointer_arith)
749 pedwarn ("sizeof applied to a void type");
752 if (code == ERROR_MARK)
754 if (TYPE_SIZE (type) == 0)
756 error ("sizeof applied to an incomplete type");
760 /* Convert in case a char is more than one unit. */
761 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
762 size_int (TYPE_PRECISION (char_type_node)));
763 /* size_binop does not put the constant in range, so do it now. */
764 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
765 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
770 c_sizeof_nowarn (type)
773 enum tree_code code = TREE_CODE (type);
776 if (code == FUNCTION_TYPE
778 || code == ERROR_MARK)
780 if (TYPE_SIZE (type) == 0)
783 /* Convert in case a char is more than one unit. */
784 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
785 size_int (TYPE_PRECISION (char_type_node)));
786 force_fit_type (t, 0);
790 /* Compute the size to increment a pointer by. */
793 c_size_in_bytes (type)
796 enum tree_code code = TREE_CODE (type);
799 if (code == FUNCTION_TYPE)
801 if (code == VOID_TYPE)
803 if (code == ERROR_MARK)
805 if (TYPE_SIZE (type) == 0)
807 error ("arithmetic on pointer to an incomplete type");
811 /* Convert in case a char is more than one unit. */
812 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
813 size_int (BITS_PER_UNIT));
814 force_fit_type (t, 0);
818 /* Implement the __alignof keyword: Return the minimum required
819 alignment of TYPE, measured in bytes. */
825 enum tree_code code = TREE_CODE (type);
827 if (code == FUNCTION_TYPE)
828 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
830 if (code == VOID_TYPE || code == ERROR_MARK)
833 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
836 /* Implement the __alignof keyword: Return the minimum required
837 alignment of EXPR, measured in bytes. For VAR_DECL's and
838 FIELD_DECL's return DECL_ALIGN (which can be set from an
839 "aligned" __attribute__ specification). */
842 c_alignof_expr (expr)
845 if (TREE_CODE (expr) == VAR_DECL)
846 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
848 if (TREE_CODE (expr) == COMPONENT_REF
849 && DECL_BIT_FIELD (TREE_OPERAND (expr, 1)))
851 error ("`__alignof' applied to a bit-field");
854 else if (TREE_CODE (expr) == COMPONENT_REF
855 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
856 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
858 if (TREE_CODE (expr) == INDIRECT_REF)
860 tree t = TREE_OPERAND (expr, 0);
862 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
864 while (TREE_CODE (t) == NOP_EXPR
865 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
869 t = TREE_OPERAND (t, 0);
870 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
871 if (thisalign > bestalign)
872 best = t, bestalign = thisalign;
874 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
877 return c_alignof (TREE_TYPE (expr));
879 /* Return either DECL or its known constant value (if it has one). */
882 decl_constant_value (decl)
885 if (! TREE_PUBLIC (decl)
886 /* Don't change a variable array bound or initial value to a constant
887 in a place where a variable is invalid. */
888 && current_function_decl != 0
890 && ! TREE_THIS_VOLATILE (decl)
891 && DECL_INITIAL (decl) != 0
892 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
893 /* This is invalid if initial value is not constant.
894 If it has either a function call, a memory reference,
895 or a variable, then re-evaluating it could give different results. */
896 && TREE_CONSTANT (DECL_INITIAL (decl))
897 /* Check for cases where this is sub-optimal, even though valid. */
898 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
899 && DECL_MODE (decl) != BLKmode)
900 return DECL_INITIAL (decl);
904 /* Perform default promotions for C data used in expressions.
905 Arrays and functions are converted to pointers;
906 enumeral types or short or char, to int.
907 In addition, manifest constants symbols are replaced by their values. */
910 default_conversion (exp)
913 register tree type = TREE_TYPE (exp);
914 register enum tree_code code = TREE_CODE (type);
916 /* Constants can be used directly unless they're not loadable. */
917 if (TREE_CODE (exp) == CONST_DECL)
918 exp = DECL_INITIAL (exp);
919 /* Replace a nonvolatile const static variable with its value. */
921 && TREE_CODE (exp) == VAR_DECL
922 && TREE_READONLY (exp)
923 /* But not for iterators! */
925 && DECL_MODE (exp) != BLKmode)
927 exp = decl_constant_value (exp);
928 type = TREE_TYPE (exp);
931 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
933 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
934 to integer and cause infinite recursion. */
935 while (TREE_CODE (exp) == NON_LVALUE_EXPR
936 || (TREE_CODE (exp) == NOP_EXPR
937 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
938 exp = TREE_OPERAND (exp, 0);
940 /* Normally convert enums to int,
941 but convert wide enums to something wider. */
942 if (code == ENUMERAL_TYPE)
944 type = type_for_size (MAX (TYPE_PRECISION (type),
945 TYPE_PRECISION (integer_type_node)),
947 || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node))
948 && TREE_UNSIGNED (type)));
949 return convert (type, exp);
952 if (C_PROMOTING_INTEGER_TYPE_P (type))
954 /* Traditionally, unsignedness is preserved in default promotions.
955 Also preserve unsignedness if not really getting any wider. */
956 if (TREE_UNSIGNED (type)
958 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
959 return convert (unsigned_type_node, exp);
960 return convert (integer_type_node, exp);
962 if (flag_traditional && !flag_allow_single_precision
963 && TYPE_MAIN_VARIANT (type) == float_type_node)
964 return convert (double_type_node, exp);
965 if (code == VOID_TYPE)
967 error ("void value not ignored as it ought to be");
968 return error_mark_node;
970 if (code == FUNCTION_TYPE)
972 return build_unary_op (ADDR_EXPR, exp, 0);
974 if (code == ARRAY_TYPE)
977 tree restype = TREE_TYPE (type);
982 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
983 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
985 constp = TREE_READONLY (exp);
986 volatilep = TREE_THIS_VOLATILE (exp);
989 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
990 || constp || volatilep)
991 restype = c_build_type_variant (restype,
992 TYPE_READONLY (type) || constp,
993 TYPE_VOLATILE (type) || volatilep);
995 if (TREE_CODE (exp) == INDIRECT_REF)
996 return convert (TYPE_POINTER_TO (restype),
997 TREE_OPERAND (exp, 0));
999 if (TREE_CODE (exp) == COMPOUND_EXPR)
1001 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1002 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1003 TREE_OPERAND (exp, 0), op1);
1007 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1009 error ("invalid use of non-lvalue array");
1010 return error_mark_node;
1013 ptrtype = build_pointer_type (restype);
1015 if (TREE_CODE (exp) == VAR_DECL)
1017 /* ??? This is not really quite correct
1018 in that the type of the operand of ADDR_EXPR
1019 is not the target type of the type of the ADDR_EXPR itself.
1020 Question is, can this lossage be avoided? */
1021 adr = build1 (ADDR_EXPR, ptrtype, exp);
1022 if (mark_addressable (exp) == 0)
1023 return error_mark_node;
1024 TREE_CONSTANT (adr) = staticp (exp);
1025 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1028 /* This way is better for a COMPONENT_REF since it can
1029 simplify the offset for a component. */
1030 adr = build_unary_op (ADDR_EXPR, exp, 1);
1031 return convert (ptrtype, adr);
1036 /* Look up component name in the structure type definition.
1038 If this component name is found indirectly within an anonymous union,
1039 store in *INDIRECT the component which directly contains
1040 that anonymous union. Otherwise, set *INDIRECT to 0. */
1043 lookup_field (type, component, indirect)
1044 tree type, component;
1049 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1050 to the field elements. Use a binary search on this array to quickly
1051 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1052 will always be set for structures which have many elements. */
1054 if (TYPE_LANG_SPECIFIC (type))
1057 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1059 field = TYPE_FIELDS (type);
1061 top = TYPE_LANG_SPECIFIC (type)->len;
1062 while (top - bot > 1)
1066 half = (top - bot + 1) >> 1;
1067 field = field_array[bot+half];
1069 if (DECL_NAME (field) == NULL_TREE)
1071 /* Step through all anon unions in linear fashion. */
1072 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1076 field = field_array[bot++];
1077 anon = lookup_field (TREE_TYPE (field), component, &junk);
1078 if (anon != NULL_TREE)
1085 /* Entire record is only anon unions. */
1089 /* Restart the binary search, with new lower bound. */
1093 cmp = (HOST_WIDE_INT) DECL_NAME (field) - (HOST_WIDE_INT) component;
1102 if (DECL_NAME (field_array[bot]) == component)
1103 field = field_array[bot];
1104 else if (DECL_NAME (field) != component)
1109 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1111 if (DECL_NAME (field) == NULL_TREE)
1114 tree anon = lookup_field (TREE_TYPE (field), component, &junk);
1115 if (anon != NULL_TREE)
1122 if (DECL_NAME (field) == component)
1127 *indirect = NULL_TREE;
1131 /* Make an expression to refer to the COMPONENT field of
1132 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1135 build_component_ref (datum, component)
1136 tree datum, component;
1138 register tree type = TREE_TYPE (datum);
1139 register enum tree_code code = TREE_CODE (type);
1140 register tree field = NULL;
1143 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1144 unless we are not to support things not strictly ANSI. */
1145 switch (TREE_CODE (datum))
1149 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1150 return build (COMPOUND_EXPR, TREE_TYPE (value),
1151 TREE_OPERAND (datum, 0), value);
1154 return build_conditional_expr
1155 (TREE_OPERAND (datum, 0),
1156 build_component_ref (TREE_OPERAND (datum, 1), component),
1157 build_component_ref (TREE_OPERAND (datum, 2), component));
1160 /* See if there is a field or component with name COMPONENT. */
1162 if (code == RECORD_TYPE || code == UNION_TYPE)
1166 if (TYPE_SIZE (type) == 0)
1168 incomplete_type_error (NULL_TREE, type);
1169 return error_mark_node;
1172 field = lookup_field (type, component, &indirect);
1176 error (code == RECORD_TYPE
1177 ? "structure has no member named `%s'"
1178 : "union has no member named `%s'",
1179 IDENTIFIER_POINTER (component));
1180 return error_mark_node;
1182 if (TREE_TYPE (field) == error_mark_node)
1183 return error_mark_node;
1185 /* If FIELD was found buried within an anonymous union,
1186 make one COMPONENT_REF to get that anonymous union,
1187 then fall thru to make a second COMPONENT_REF to get FIELD. */
1190 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1191 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1192 TREE_READONLY (ref) = 1;
1193 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1194 TREE_THIS_VOLATILE (ref) = 1;
1198 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1200 if (TREE_READONLY (datum) || TREE_READONLY (field))
1201 TREE_READONLY (ref) = 1;
1202 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1203 TREE_THIS_VOLATILE (ref) = 1;
1207 else if (code != ERROR_MARK)
1208 error ("request for member `%s' in something not a structure or union",
1209 IDENTIFIER_POINTER (component));
1211 return error_mark_node;
1214 /* Given an expression PTR for a pointer, return an expression
1215 for the value pointed to.
1216 ERRORSTRING is the name of the operator to appear in error messages. */
1219 build_indirect_ref (ptr, errorstring)
1223 register tree pointer = default_conversion (ptr);
1224 register tree type = TREE_TYPE (pointer);
1226 if (TREE_CODE (type) == POINTER_TYPE)
1228 if (TREE_CODE (pointer) == ADDR_EXPR
1230 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1231 == TREE_TYPE (type)))
1232 return TREE_OPERAND (pointer, 0);
1235 tree t = TREE_TYPE (type);
1236 register tree ref = build1 (INDIRECT_REF,
1237 TYPE_MAIN_VARIANT (t), pointer);
1239 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1241 error ("dereferencing pointer to incomplete type");
1242 return error_mark_node;
1244 if (TREE_CODE (t) == VOID_TYPE)
1245 warning ("dereferencing `void *' pointer");
1247 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1248 so that we get the proper error message if the result is used
1249 to assign to. Also, &* is supposed to be a no-op.
1250 And ANSI C seems to specify that the type of the result
1251 should be the const type. */
1252 /* A de-reference of a pointer to const is not a const. It is valid
1253 to change it via some other pointer. */
1254 TREE_READONLY (ref) = TYPE_READONLY (t);
1255 TREE_SIDE_EFFECTS (ref)
1256 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1257 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1261 else if (TREE_CODE (pointer) != ERROR_MARK)
1262 error ("invalid type argument of `%s'", errorstring);
1263 return error_mark_node;
1266 /* This handles expressions of the form "a[i]", which denotes
1269 This is logically equivalent in C to *(a+i), but we may do it differently.
1270 If A is a variable or a member, we generate a primitive ARRAY_REF.
1271 This avoids forcing the array out of registers, and can work on
1272 arrays that are not lvalues (for example, members of structures returned
1276 build_array_ref (array, index)
1281 error ("subscript missing in array reference");
1282 return error_mark_node;
1285 if (TREE_TYPE (array) == error_mark_node
1286 || TREE_TYPE (index) == error_mark_node)
1287 return error_mark_node;
1289 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1290 && TREE_CODE (array) != INDIRECT_REF)
1294 /* Subscripting with type char is likely to lose
1295 on a machine where chars are signed.
1296 So warn on any machine, but optionally.
1297 Don't warn for unsigned char since that type is safe.
1298 Don't warn for signed char because anyone who uses that
1299 must have done so deliberately. */
1300 if (warn_char_subscripts
1301 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1302 warning ("array subscript has type `char'");
1304 /* Apply default promotions *after* noticing character types. */
1305 index = default_conversion (index);
1307 /* Require integer *after* promotion, for sake of enums. */
1308 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1310 error ("array subscript is not an integer");
1311 return error_mark_node;
1314 /* An array that is indexed by a non-constant
1315 cannot be stored in a register; we must be able to do
1316 address arithmetic on its address.
1317 Likewise an array of elements of variable size. */
1318 if (TREE_CODE (index) != INTEGER_CST
1319 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1320 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1322 if (mark_addressable (array) == 0)
1323 return error_mark_node;
1325 /* An array that is indexed by a constant value which is not within
1326 the array bounds cannot be stored in a register either; because we
1327 would get a crash in store_bit_field/extract_bit_field when trying
1328 to access a non-existent part of the register. */
1329 if (TREE_CODE (index) == INTEGER_CST
1330 && TYPE_VALUES (TREE_TYPE (array))
1331 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1333 if (mark_addressable (array) == 0)
1334 return error_mark_node;
1337 if (pedantic && !lvalue_p (array))
1339 if (DECL_REGISTER (array))
1340 pedwarn ("ANSI C forbids subscripting `register' array");
1342 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1348 while (TREE_CODE (foo) == COMPONENT_REF)
1349 foo = TREE_OPERAND (foo, 0);
1350 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1351 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1354 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1355 rval = build (ARRAY_REF, type, array, index);
1356 /* Array ref is const/volatile if the array elements are
1357 or if the array is. */
1358 TREE_READONLY (rval)
1359 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1360 | TREE_READONLY (array));
1361 TREE_SIDE_EFFECTS (rval)
1362 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1363 | TREE_SIDE_EFFECTS (array));
1364 TREE_THIS_VOLATILE (rval)
1365 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1366 /* This was added by rms on 16 Nov 91.
1367 It fixes vol struct foo *a; a->elts[1]
1368 in an inline function.
1369 Hope it doesn't break something else. */
1370 | TREE_THIS_VOLATILE (array));
1371 return require_complete_type (fold (rval));
1375 tree ar = default_conversion (array);
1376 tree ind = default_conversion (index);
1378 /* Put the integer in IND to simplify error checking. */
1379 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1386 if (ar == error_mark_node)
1389 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
1391 error ("subscripted value is neither array nor pointer");
1392 return error_mark_node;
1394 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1396 error ("array subscript is not an integer");
1397 return error_mark_node;
1400 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1405 /* Build a function call to function FUNCTION with parameters PARAMS.
1406 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1407 TREE_VALUE of each node is a parameter-expression.
1408 FUNCTION's data type may be a function type or a pointer-to-function. */
1411 build_function_call (function, params)
1412 tree function, params;
1414 register tree fntype, fundecl = 0;
1415 register tree coerced_params;
1416 tree name = NULL_TREE, assembler_name = NULL_TREE;
1418 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1419 STRIP_TYPE_NOPS (function);
1421 /* Convert anything with function type to a pointer-to-function. */
1422 if (TREE_CODE (function) == FUNCTION_DECL)
1424 name = DECL_NAME (function);
1425 assembler_name = DECL_ASSEMBLER_NAME (function);
1427 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1428 (because calling an inline function does not mean the function
1429 needs to be separately compiled). */
1430 fntype = build_type_variant (TREE_TYPE (function),
1431 TREE_READONLY (function),
1432 TREE_THIS_VOLATILE (function));
1434 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1437 function = default_conversion (function);
1439 fntype = TREE_TYPE (function);
1441 if (TREE_CODE (fntype) == ERROR_MARK)
1442 return error_mark_node;
1444 if (!(TREE_CODE (fntype) == POINTER_TYPE
1445 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1447 error ("called object is not a function");
1448 return error_mark_node;
1451 /* fntype now gets the type of function pointed to. */
1452 fntype = TREE_TYPE (fntype);
1454 /* Convert the parameters to the types declared in the
1455 function prototype, or apply default promotions. */
1458 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1460 /* Check for errors in format strings. */
1462 if (warn_format && (name || assembler_name))
1463 check_function_format (name, assembler_name, coerced_params);
1465 /* Recognize certain built-in functions so we can make tree-codes
1466 other than CALL_EXPR. We do this when it enables fold-const.c
1467 to do something useful. */
1469 if (TREE_CODE (function) == ADDR_EXPR
1470 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1471 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1472 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1477 if (coerced_params == 0)
1478 return integer_zero_node;
1479 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1483 register tree result
1484 = build (CALL_EXPR, TREE_TYPE (fntype),
1485 function, coerced_params, NULL_TREE);
1487 TREE_SIDE_EFFECTS (result) = 1;
1488 if (TREE_TYPE (result) == void_type_node)
1490 return require_complete_type (result);
1494 /* Convert the argument expressions in the list VALUES
1495 to the types in the list TYPELIST. The result is a list of converted
1496 argument expressions.
1498 If TYPELIST is exhausted, or when an element has NULL as its type,
1499 perform the default conversions.
1501 PARMLIST is the chain of parm decls for the function being called.
1502 It may be 0, if that info is not available.
1503 It is used only for generating error messages.
1505 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1507 This is also where warnings about wrong number of args are generated.
1509 Both VALUES and the returned value are chains of TREE_LIST nodes
1510 with the elements of the list in the TREE_VALUE slots of those nodes. */
1513 convert_arguments (typelist, values, name, fundecl)
1514 tree typelist, values, name, fundecl;
1516 register tree typetail, valtail;
1517 register tree result = NULL;
1520 /* Scan the given expressions and types, producing individual
1521 converted arguments and pushing them on RESULT in reverse order. */
1523 for (valtail = values, typetail = typelist, parmnum = 0;
1525 valtail = TREE_CHAIN (valtail), parmnum++)
1527 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1528 register tree val = TREE_VALUE (valtail);
1530 if (type == void_type_node)
1533 error ("too many arguments to function `%s'",
1534 IDENTIFIER_POINTER (name));
1536 error ("too many arguments to function");
1540 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1541 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1542 to convert automatically to a pointer. */
1543 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1544 val = TREE_OPERAND (val, 0);
1546 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1547 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1548 val = default_conversion (val);
1550 val = require_complete_type (val);
1554 /* Formal parm type is specified by a function prototype. */
1557 if (TYPE_SIZE (type) == 0)
1559 error ("type of formal parameter %d is incomplete", parmnum + 1);
1566 #ifdef PROMOTE_PROTOTYPES
1567 /* Rather than truncating and then reextending,
1568 convert directly to int, if that's the type we will want. */
1569 if (! flag_traditional
1570 && (TREE_CODE (type) == INTEGER_TYPE
1571 || TREE_CODE (type) == ENUMERAL_TYPE)
1572 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1573 type = integer_type_node;
1576 #if 0 /* This turns out not to win--there's no way to write a prototype
1577 for a function whose arg type is a union with no tag. */
1578 /* Nameless union automatically casts the types it contains. */
1579 if (TREE_CODE (type) == UNION_TYPE && TYPE_NAME (type) == 0)
1583 for (field = TYPE_FIELDS (type); field;
1584 field = TREE_CHAIN (field))
1585 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
1586 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1590 val = build1 (CONVERT_EXPR, type, val);
1594 /* Optionally warn about conversions that
1595 differ from the default conversions. */
1596 if (warn_conversion)
1598 int formal_prec = TYPE_PRECISION (type);
1600 if (TREE_CODE (type) != REAL_TYPE
1601 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1602 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1603 else if (TREE_CODE (type) == REAL_TYPE
1604 && TREE_CODE (TREE_TYPE (val)) != REAL_TYPE)
1605 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1606 else if (TREE_CODE (type) == REAL_TYPE
1607 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1609 /* Warn if any argument is passed as `float',
1610 since without a prototype it would be `double'. */
1611 if (formal_prec == TYPE_PRECISION (float_type_node))
1612 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1614 /* Detect integer changing in width or signedness. */
1615 else if ((TREE_CODE (type) == INTEGER_TYPE
1616 || TREE_CODE (type) == ENUMERAL_TYPE)
1617 && (TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
1618 || TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE))
1620 tree would_have_been = default_conversion (val);
1621 tree type1 = TREE_TYPE (would_have_been);
1623 if (TREE_CODE (type) == ENUMERAL_TYPE
1624 && type == TREE_TYPE (val))
1625 /* No warning if function asks for enum
1626 and the actual arg is that enum type. */
1628 else if (formal_prec != TYPE_PRECISION (type1))
1629 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1630 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1632 /* Don't complain if the formal parameter type
1633 is an enum, because we can't tell now whether
1634 the value was an enum--even the same enum. */
1635 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1637 else if (TREE_CODE (val) == INTEGER_CST
1638 && int_fits_type_p (val, type))
1639 /* Change in signedness doesn't matter
1640 if a constant value is unaffected. */
1642 /* Likewise for a constant in a NOP_EXPR. */
1643 else if (TREE_CODE (val) == NOP_EXPR
1644 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1645 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1647 #if 0 /* We never get such tree structure here. */
1648 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1649 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1650 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1651 /* Change in signedness doesn't matter
1652 if an enum value is unaffected. */
1655 /* If the value is extended from a narrower
1656 unsigned type, it doesn't matter whether we
1657 pass it as signed or unsigned; the value
1658 certainly is the same either way. */
1659 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1660 && TREE_UNSIGNED (TREE_TYPE (val)))
1662 else if (TREE_UNSIGNED (type))
1663 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1665 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1669 parmval = convert_for_assignment (type, val,
1670 (char *)0, /* arg passing */
1671 fundecl, name, parmnum + 1);
1673 #ifdef PROMOTE_PROTOTYPES
1674 if ((TREE_CODE (type) == INTEGER_TYPE
1675 || TREE_CODE (type) == ENUMERAL_TYPE)
1676 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1677 parmval = default_conversion (parmval);
1680 result = tree_cons (NULL_TREE, parmval, result);
1682 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1683 && (TYPE_PRECISION (TREE_TYPE (val))
1684 < TYPE_PRECISION (double_type_node)))
1685 /* Convert `float' to `double'. */
1686 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1688 /* Convert `short' and `char' to full-size `int'. */
1689 result = tree_cons (NULL_TREE, default_conversion (val), result);
1692 typetail = TREE_CHAIN (typetail);
1695 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1698 error ("too few arguments to function `%s'",
1699 IDENTIFIER_POINTER (name));
1701 error ("too few arguments to function");
1704 return nreverse (result);
1707 /* This is the entry point used by the parser
1708 for binary operators in the input.
1709 In addition to constructing the expression,
1710 we check for operands that were written with other binary operators
1711 in a way that is likely to confuse the user. */
1714 parser_build_binary_op (code, arg1, arg2)
1715 enum tree_code code;
1718 tree result = build_binary_op (code, arg1, arg2, 1);
1721 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1722 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1723 enum tree_code code1 = ERROR_MARK;
1724 enum tree_code code2 = ERROR_MARK;
1726 if (class1 == 'e' || class1 == '1'
1727 || class1 == '2' || class1 == '<')
1728 code1 = C_EXP_ORIGINAL_CODE (arg1);
1729 if (class2 == 'e' || class2 == '1'
1730 || class2 == '2' || class2 == '<')
1731 code2 = C_EXP_ORIGINAL_CODE (arg2);
1733 /* Check for cases such as x+y<<z which users are likely
1734 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1735 is cleared to prevent these warnings. */
1736 if (warn_parentheses)
1738 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1740 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1741 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1742 warning ("suggest parentheses around + or - inside shift");
1745 if (code == TRUTH_ORIF_EXPR)
1747 if (code1 == TRUTH_ANDIF_EXPR
1748 || code2 == TRUTH_ANDIF_EXPR)
1749 warning ("suggest parentheses around && within ||");
1752 if (code == BIT_IOR_EXPR)
1754 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1755 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1756 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1757 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1758 warning ("suggest parentheses around arithmetic in operand of |");
1761 if (code == BIT_XOR_EXPR)
1763 if (code1 == BIT_AND_EXPR
1764 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1765 || code2 == BIT_AND_EXPR
1766 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1767 warning ("suggest parentheses around arithmetic in operand of ^");
1770 if (code == BIT_AND_EXPR)
1772 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1773 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1774 warning ("suggest parentheses around + or - in operand of &");
1778 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1779 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1780 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1781 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1783 unsigned_conversion_warning (result, arg1);
1784 unsigned_conversion_warning (result, arg2);
1785 overflow_warning (result);
1787 class = TREE_CODE_CLASS (TREE_CODE (result));
1789 /* Record the code that was specified in the source,
1790 for the sake of warnings about confusing nesting. */
1791 if (class == 'e' || class == '1'
1792 || class == '2' || class == '<')
1793 C_SET_EXP_ORIGINAL_CODE (result, code);
1796 int flag = TREE_CONSTANT (result);
1797 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1798 so that convert_for_assignment wouldn't strip it.
1799 That way, we got warnings for things like p = (1 - 1).
1800 But it turns out we should not get those warnings. */
1801 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1802 C_SET_EXP_ORIGINAL_CODE (result, code);
1803 TREE_CONSTANT (result) = flag;
1809 /* Build a binary-operation expression without default conversions.
1810 CODE is the kind of expression to build.
1811 This function differs from `build' in several ways:
1812 the data type of the result is computed and recorded in it,
1813 warnings are generated if arg data types are invalid,
1814 special handling for addition and subtraction of pointers is known,
1815 and some optimization is done (operations on narrow ints
1816 are done in the narrower type when that gives the same result).
1817 Constant folding is also done before the result is returned.
1819 Note that the operands will never have enumeral types, or function
1820 or array types, because either they will have the default conversions
1821 performed or they have both just been converted to some other type in which
1822 the arithmetic is to be done. */
1825 build_binary_op (code, orig_op0, orig_op1, convert_p)
1826 enum tree_code code;
1827 tree orig_op0, orig_op1;
1831 register enum tree_code code0, code1;
1834 /* Expression code to give to the expression when it is built.
1835 Normally this is CODE, which is what the caller asked for,
1836 but in some special cases we change it. */
1837 register enum tree_code resultcode = code;
1839 /* Data type in which the computation is to be performed.
1840 In the simplest cases this is the common type of the arguments. */
1841 register tree result_type = NULL;
1843 /* Nonzero means operands have already been type-converted
1844 in whatever way is necessary.
1845 Zero means they need to be converted to RESULT_TYPE. */
1848 /* Nonzero means after finally constructing the expression
1849 give it this type. Otherwise, give it type RESULT_TYPE. */
1850 tree final_type = 0;
1852 /* Nonzero if this is an operation like MIN or MAX which can
1853 safely be computed in short if both args are promoted shorts.
1854 Also implies COMMON.
1855 -1 indicates a bitwise operation; this makes a difference
1856 in the exact conditions for when it is safe to do the operation
1857 in a narrower mode. */
1860 /* Nonzero if this is a comparison operation;
1861 if both args are promoted shorts, compare the original shorts.
1862 Also implies COMMON. */
1863 int short_compare = 0;
1865 /* Nonzero if this is a right-shift operation, which can be computed on the
1866 original short and then promoted if the operand is a promoted short. */
1867 int short_shift = 0;
1869 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1874 op0 = default_conversion (orig_op0);
1875 op1 = default_conversion (orig_op1);
1883 type0 = TREE_TYPE (op0);
1884 type1 = TREE_TYPE (op1);
1886 /* The expression codes of the data types of the arguments tell us
1887 whether the arguments are integers, floating, pointers, etc. */
1888 code0 = TREE_CODE (type0);
1889 code1 = TREE_CODE (type1);
1891 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1892 STRIP_TYPE_NOPS (op0);
1893 STRIP_TYPE_NOPS (op1);
1895 /* If an error was already reported for one of the arguments,
1896 avoid reporting another error. */
1898 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1899 return error_mark_node;
1904 /* Handle the pointer + int case. */
1905 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1906 return pointer_int_sum (PLUS_EXPR, op0, op1);
1907 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1908 return pointer_int_sum (PLUS_EXPR, op1, op0);
1914 /* Subtraction of two similar pointers.
1915 We must subtract them as integers, then divide by object size. */
1916 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1917 && comp_target_types (type0, type1))
1918 return pointer_diff (op0, op1);
1919 /* Handle pointer minus int. Just like pointer plus int. */
1920 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1921 return pointer_int_sum (MINUS_EXPR, op0, op1);
1930 case TRUNC_DIV_EXPR:
1932 case FLOOR_DIV_EXPR:
1933 case ROUND_DIV_EXPR:
1934 case EXACT_DIV_EXPR:
1935 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1936 || code0 == COMPLEX_TYPE)
1937 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1938 || code1 == COMPLEX_TYPE))
1940 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
1941 resultcode = RDIV_EXPR;
1944 /* Although it would be tempting to shorten always here, that
1945 loses on some targets, since the modulo instruction is
1946 undefined if the quotient can't be represented in the
1947 computation mode. We shorten only if unsigned or if
1948 dividing by something we know != -1. */
1949 shorten = (TREE_UNSIGNED (orig_op0)
1950 || (TREE_CODE (op1) == INTEGER_CST
1951 && (TREE_INT_CST_LOW (op1) != -1
1952 || TREE_INT_CST_HIGH (op1) != -1)));
1959 case BIT_ANDTC_EXPR:
1962 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1964 /* If one operand is a constant, and the other is a short type
1965 that has been converted to an int,
1966 really do the work in the short type and then convert the
1967 result to int. If we are lucky, the constant will be 0 or 1
1968 in the short type, making the entire operation go away. */
1969 if (TREE_CODE (op0) == INTEGER_CST
1970 && TREE_CODE (op1) == NOP_EXPR
1971 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
1972 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
1974 final_type = result_type;
1975 op1 = TREE_OPERAND (op1, 0);
1976 result_type = TREE_TYPE (op1);
1978 if (TREE_CODE (op1) == INTEGER_CST
1979 && TREE_CODE (op0) == NOP_EXPR
1980 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
1981 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
1983 final_type = result_type;
1984 op0 = TREE_OPERAND (op0, 0);
1985 result_type = TREE_TYPE (op0);
1989 case TRUNC_MOD_EXPR:
1990 case FLOOR_MOD_EXPR:
1991 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1993 /* Although it would be tempting to shorten always here, that loses
1994 on some targets, since the modulo instruction is undefined if the
1995 quotient can't be represented in the computation mode. We shorten
1996 only if unsigned or if dividing by something we know != -1. */
1997 shorten = (TREE_UNSIGNED (orig_op0)
1998 || (TREE_CODE (op1) == INTEGER_CST
1999 && (TREE_INT_CST_LOW (op1) != -1
2000 || TREE_INT_CST_HIGH (op1) != -1)));
2005 case TRUTH_ANDIF_EXPR:
2006 case TRUTH_ORIF_EXPR:
2007 case TRUTH_AND_EXPR:
2009 case TRUTH_XOR_EXPR:
2010 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2011 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2012 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2013 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2015 /* Result of these operations is always an int,
2016 but that does not mean the operands should be
2017 converted to ints! */
2018 result_type = integer_type_node;
2019 op0 = truthvalue_conversion (op0);
2020 op1 = truthvalue_conversion (op1);
2025 /* Shift operations: result has same type as first operand;
2026 always convert second operand to int.
2027 Also set SHORT_SHIFT if shifting rightward. */
2030 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2032 if (TREE_CODE (op1) == INTEGER_CST)
2034 if (tree_int_cst_lt (op1, integer_zero_node))
2035 warning ("right shift count is negative");
2038 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2040 if (TREE_INT_CST_HIGH (op1) != 0
2041 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2042 >= TYPE_PRECISION (type0)))
2043 warning ("right shift count >= width of type");
2046 /* Use the type of the value to be shifted.
2047 This is what most traditional C compilers do. */
2048 result_type = type0;
2049 /* Unless traditional, convert the shift-count to an integer,
2050 regardless of size of value being shifted. */
2051 if (! flag_traditional)
2053 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2054 op1 = convert (integer_type_node, op1);
2055 /* Avoid converting op1 to result_type later. */
2062 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2064 if (TREE_CODE (op1) == INTEGER_CST)
2066 if (tree_int_cst_lt (op1, integer_zero_node))
2067 warning ("left shift count is negative");
2068 else if (TREE_INT_CST_HIGH (op1) != 0
2069 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2070 >= TYPE_PRECISION (type0)))
2071 warning ("left shift count >= width of type");
2073 /* Use the type of the value to be shifted.
2074 This is what most traditional C compilers do. */
2075 result_type = type0;
2076 /* Unless traditional, convert the shift-count to an integer,
2077 regardless of size of value being shifted. */
2078 if (! flag_traditional)
2080 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2081 op1 = convert (integer_type_node, op1);
2082 /* Avoid converting op1 to result_type later. */
2090 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2092 if (TREE_CODE (op1) == INTEGER_CST)
2094 if (tree_int_cst_lt (op1, integer_zero_node))
2095 warning ("shift count is negative");
2096 else if (TREE_INT_CST_HIGH (op1) != 0
2097 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2098 >= TYPE_PRECISION (type0)))
2099 warning ("shift count >= width of type");
2101 /* Use the type of the value to be shifted.
2102 This is what most traditional C compilers do. */
2103 result_type = type0;
2104 /* Unless traditional, convert the shift-count to an integer,
2105 regardless of size of value being shifted. */
2106 if (! flag_traditional)
2108 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2109 op1 = convert (integer_type_node, op1);
2110 /* Avoid converting op1 to result_type later. */
2118 /* Result of comparison is always int,
2119 but don't convert the args to int! */
2120 result_type = integer_type_node;
2122 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2123 || code0 == COMPLEX_TYPE)
2124 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2125 || code1 == COMPLEX_TYPE))
2127 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2129 register tree tt0 = TREE_TYPE (type0);
2130 register tree tt1 = TREE_TYPE (type1);
2131 /* Anything compares with void *. void * compares with anything.
2132 Otherwise, the targets must be compatible
2133 and both must be object or both incomplete. */
2134 if (comp_target_types (type0, type1))
2136 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2138 /* op0 != orig_op0 detects the case of something
2139 whose value is 0 but which isn't a valid null ptr const. */
2140 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2141 && TREE_CODE (tt1) == FUNCTION_TYPE)
2142 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2144 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2146 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2147 && TREE_CODE (tt0) == FUNCTION_TYPE)
2148 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2151 pedwarn ("comparison of distinct pointer types lacks a cast");
2153 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2154 && integer_zerop (op1))
2155 op1 = null_pointer_node;
2156 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2157 && integer_zerop (op0))
2158 op0 = null_pointer_node;
2159 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2161 if (! flag_traditional)
2162 pedwarn ("comparison between pointer and integer");
2163 op1 = convert (TREE_TYPE (op0), op1);
2165 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2167 if (! flag_traditional)
2168 pedwarn ("comparison between pointer and integer");
2169 op0 = convert (TREE_TYPE (op1), op0);
2172 /* If args are not valid, clear out RESULT_TYPE
2173 to cause an error message later. */
2179 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2180 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2182 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2184 if (! comp_target_types (type0, type1))
2185 pedwarn ("comparison of distinct pointer types lacks a cast");
2187 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2188 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2189 result_type = common_type (type0, type1);
2197 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2198 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2200 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2202 if (! comp_target_types (type0, type1))
2203 pedwarn ("comparison of distinct pointer types lacks a cast");
2204 else if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2205 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2206 pedwarn ("comparison of complete and incomplete pointers");
2208 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2209 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2210 result_type = integer_type_node;
2212 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2213 && integer_zerop (op1))
2215 result_type = integer_type_node;
2216 op1 = null_pointer_node;
2218 pedwarn ("ordered comparison of pointer with integer zero");
2220 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2221 && integer_zerop (op0))
2223 result_type = integer_type_node;
2224 op0 = null_pointer_node;
2226 pedwarn ("ordered comparison of pointer with integer zero");
2228 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2230 result_type = integer_type_node;
2231 if (! flag_traditional)
2232 pedwarn ("comparison between pointer and integer");
2233 op1 = convert (TREE_TYPE (op0), op1);
2235 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2237 result_type = integer_type_node;
2238 if (! flag_traditional)
2239 pedwarn ("comparison between pointer and integer");
2240 op0 = convert (TREE_TYPE (op1), op0);
2246 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2248 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2250 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2252 if (shorten || common || short_compare)
2253 result_type = common_type (type0, type1);
2255 /* For certain operations (which identify themselves by shorten != 0)
2256 if both args were extended from the same smaller type,
2257 do the arithmetic in that type and then extend.
2259 shorten !=0 and !=1 indicates a bitwise operation.
2260 For them, this optimization is safe only if
2261 both args are zero-extended or both are sign-extended.
2262 Otherwise, we might change the result.
2263 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2264 but calculated in (unsigned short) it would be (unsigned short)-1. */
2266 if (shorten && none_complex)
2268 int unsigned0, unsigned1;
2269 tree arg0 = get_narrower (op0, &unsigned0);
2270 tree arg1 = get_narrower (op1, &unsigned1);
2271 /* UNS is 1 if the operation to be done is an unsigned one. */
2272 int uns = TREE_UNSIGNED (result_type);
2275 final_type = result_type;
2277 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2278 but it *requires* conversion to FINAL_TYPE. */
2280 if ((TYPE_PRECISION (TREE_TYPE (op0))
2281 == TYPE_PRECISION (TREE_TYPE (arg0)))
2282 && TREE_TYPE (op0) != final_type)
2283 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2284 if ((TYPE_PRECISION (TREE_TYPE (op1))
2285 == TYPE_PRECISION (TREE_TYPE (arg1)))
2286 && TREE_TYPE (op1) != final_type)
2287 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2289 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2291 /* For bitwise operations, signedness of nominal type
2292 does not matter. Consider only how operands were extended. */
2296 /* Note that in all three cases below we refrain from optimizing
2297 an unsigned operation on sign-extended args.
2298 That would not be valid. */
2300 /* Both args variable: if both extended in same way
2301 from same width, do it in that width.
2302 Do it unsigned if args were zero-extended. */
2303 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2304 < TYPE_PRECISION (result_type))
2305 && (TYPE_PRECISION (TREE_TYPE (arg1))
2306 == TYPE_PRECISION (TREE_TYPE (arg0)))
2307 && unsigned0 == unsigned1
2308 && (unsigned0 || !uns))
2310 = signed_or_unsigned_type (unsigned0,
2311 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2312 else if (TREE_CODE (arg0) == INTEGER_CST
2313 && (unsigned1 || !uns)
2314 && (TYPE_PRECISION (TREE_TYPE (arg1))
2315 < TYPE_PRECISION (result_type))
2316 && (type = signed_or_unsigned_type (unsigned1,
2318 int_fits_type_p (arg0, type)))
2320 else if (TREE_CODE (arg1) == INTEGER_CST
2321 && (unsigned0 || !uns)
2322 && (TYPE_PRECISION (TREE_TYPE (arg0))
2323 < TYPE_PRECISION (result_type))
2324 && (type = signed_or_unsigned_type (unsigned0,
2326 int_fits_type_p (arg1, type)))
2330 /* Shifts can be shortened if shifting right. */
2335 tree arg0 = get_narrower (op0, &unsigned_arg);
2337 final_type = result_type;
2339 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2340 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2342 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2343 /* If arg is sign-extended and then unsigned-shifted,
2344 we can simulate this with a signed shift in arg's type
2345 only if the extended result is at least twice as wide
2346 as the arg. Otherwise, the shift could use up all the
2347 ones made by sign-extension and bring in zeros.
2348 We can't optimize that case at all, but in most machines
2349 it never happens because available widths are 2**N. */
2350 && (!TREE_UNSIGNED (final_type)
2352 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2354 /* Do an unsigned shift if the operand was zero-extended. */
2356 = signed_or_unsigned_type (unsigned_arg,
2358 /* Convert value-to-be-shifted to that type. */
2359 if (TREE_TYPE (op0) != result_type)
2360 op0 = convert (result_type, op0);
2365 /* Comparison operations are shortened too but differently.
2366 They identify themselves by setting short_compare = 1. */
2370 /* Don't write &op0, etc., because that would prevent op0
2371 from being kept in a register.
2372 Instead, make copies of the our local variables and
2373 pass the copies by reference, then copy them back afterward. */
2374 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2375 enum tree_code xresultcode = resultcode;
2377 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2380 op0 = xop0, op1 = xop1, result_type = xresult_type;
2381 resultcode = xresultcode;
2385 tree op0_type = TREE_TYPE (orig_op0);
2386 tree op1_type = TREE_TYPE (orig_op1);
2387 int op0_unsigned = TREE_UNSIGNED (op0_type);
2388 int op1_unsigned = TREE_UNSIGNED (op1_type);
2390 /* Give warnings for comparisons between signed and unsigned
2391 quantities that will fail. Do not warn if the signed quantity
2392 is an unsuffixed integer literal (or some static constant
2393 expression involving such literals) and it is positive.
2394 Do not warn if the width of the unsigned quantity is less
2395 than that of the signed quantity, since in this case all
2396 values of the unsigned quantity fit in the signed quantity.
2397 Do not warn if the signed type is the same size as the
2398 result_type since sign extension does not cause trouble in
2400 /* Do the checking based on the original operand trees, so that
2401 casts will be considered, but default promotions won't be. */
2402 if (op0_unsigned != op1_unsigned
2404 && TYPE_PRECISION (op0_type) >= TYPE_PRECISION (op1_type)
2405 && TYPE_PRECISION (op0_type) < TYPE_PRECISION (result_type)
2406 && (TREE_CODE (op1) != INTEGER_CST
2407 || (TREE_CODE (op1) == INTEGER_CST
2408 && INT_CST_LT (op1, integer_zero_node))))
2411 && TYPE_PRECISION (op1_type) >= TYPE_PRECISION (op0_type)
2412 && TYPE_PRECISION (op1_type) < TYPE_PRECISION (result_type)
2413 && (TREE_CODE (op0) != INTEGER_CST
2414 || (TREE_CODE (op0) == INTEGER_CST
2415 && INT_CST_LT (op0, integer_zero_node))))))
2416 warning ("comparison between signed and unsigned");
2421 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2422 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2423 Then the expression will be built.
2424 It will be given type FINAL_TYPE if that is nonzero;
2425 otherwise, it will be given type RESULT_TYPE. */
2429 binary_op_error (code);
2430 return error_mark_node;
2435 if (TREE_TYPE (op0) != result_type)
2436 op0 = convert (result_type, op0);
2437 if (TREE_TYPE (op1) != result_type)
2438 op1 = convert (result_type, op1);
2442 register tree result = build (resultcode, result_type, op0, op1);
2443 register tree folded;
2445 folded = fold (result);
2446 if (folded == result)
2447 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2448 if (final_type != 0)
2449 return convert (final_type, folded);
2454 /* Return a tree for the sum or difference (RESULTCODE says which)
2455 of pointer PTROP and integer INTOP. */
2458 pointer_int_sum (resultcode, ptrop, intop)
2459 enum tree_code resultcode;
2460 register tree ptrop, intop;
2464 register tree result;
2465 register tree folded;
2467 /* The result is a pointer of the same type that is being added. */
2469 register tree result_type = TREE_TYPE (ptrop);
2471 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2473 if (pedantic || warn_pointer_arith)
2474 pedwarn ("pointer of type `void *' used in arithmetic");
2475 size_exp = integer_one_node;
2477 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2479 if (pedantic || warn_pointer_arith)
2480 pedwarn ("pointer to a function used in arithmetic");
2481 size_exp = integer_one_node;
2484 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2486 /* If what we are about to multiply by the size of the elements
2487 contains a constant term, apply distributive law
2488 and multiply that constant term separately.
2489 This helps produce common subexpressions. */
2491 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2492 && ! TREE_CONSTANT (intop)
2493 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2494 && TREE_CONSTANT (size_exp)
2495 /* If the constant comes from pointer subtraction,
2496 skip this optimization--it would cause an error. */
2497 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE)
2499 enum tree_code subcode = resultcode;
2500 tree int_type = TREE_TYPE (intop);
2501 if (TREE_CODE (intop) == MINUS_EXPR)
2502 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2503 /* Convert both subexpression types to the type of intop,
2504 because weird cases involving pointer arithmetic
2505 can result in a sum or difference with different type args. */
2506 ptrop = build_binary_op (subcode, ptrop,
2507 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2508 intop = convert (int_type, TREE_OPERAND (intop, 0));
2511 /* Convert the integer argument to a type the same size as a pointer
2512 so the multiply won't overflow spuriously. */
2514 if (TYPE_PRECISION (TREE_TYPE (intop)) != POINTER_SIZE)
2515 intop = convert (type_for_size (POINTER_SIZE, 0), intop);
2517 /* Replace the integer argument
2518 with a suitable product by the object size. */
2520 intop = build_binary_op (MULT_EXPR, intop, size_exp, 1);
2522 /* Create the sum or difference. */
2524 result = build (resultcode, result_type, ptrop, intop);
2526 folded = fold (result);
2527 if (folded == result)
2528 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2532 /* Return a tree for the difference of pointers OP0 and OP1.
2533 The resulting tree has type int. */
2536 pointer_diff (op0, op1)
2537 register tree op0, op1;
2539 register tree result, folded;
2540 tree restype = ptrdiff_type_node;
2542 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2544 if (pedantic || warn_pointer_arith)
2546 if (TREE_CODE (target_type) == VOID_TYPE)
2547 pedwarn ("pointer of type `void *' used in subtraction");
2548 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2549 pedwarn ("pointer to a function used in subtraction");
2552 /* First do the subtraction as integers;
2553 then drop through to build the divide operator. */
2555 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2556 convert (restype, op1), 1);
2557 /* This generates an error if op1 is pointer to incomplete type. */
2558 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2559 error ("arithmetic on pointer to an incomplete type");
2560 /* This generates an error if op0 is pointer to incomplete type. */
2561 op1 = c_size_in_bytes (target_type);
2563 /* Divide by the size, in easiest possible way. */
2565 result = build (EXACT_DIV_EXPR, restype, op0, op1);
2567 folded = fold (result);
2568 if (folded == result)
2569 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2573 /* Construct and perhaps optimize a tree representation
2574 for a unary operation. CODE, a tree_code, specifies the operation
2575 and XARG is the operand. NOCONVERT nonzero suppresses
2576 the default promotions (such as from short to int). */
2579 build_unary_op (code, xarg, noconvert)
2580 enum tree_code code;
2584 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2585 register tree arg = xarg;
2586 register tree argtype = 0;
2587 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2588 char *errstring = NULL;
2591 if (typecode == ERROR_MARK)
2592 return error_mark_node;
2593 if (typecode == ENUMERAL_TYPE)
2594 typecode = INTEGER_TYPE;
2599 /* This is used for unary plus, because a CONVERT_EXPR
2600 is enough to prevent anybody from looking inside for
2601 associativity, but won't generate any code. */
2602 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2603 || typecode == COMPLEX_TYPE))
2604 errstring = "wrong type argument to unary plus";
2605 else if (!noconvert)
2606 arg = default_conversion (arg);
2610 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2611 || typecode == COMPLEX_TYPE))
2612 errstring = "wrong type argument to unary minus";
2613 else if (!noconvert)
2614 arg = default_conversion (arg);
2618 if (typecode == COMPLEX_TYPE)
2622 arg = default_conversion (arg);
2624 else if (typecode != INTEGER_TYPE)
2625 errstring = "wrong type argument to bit-complement";
2626 else if (!noconvert)
2627 arg = default_conversion (arg);
2631 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2632 || typecode == COMPLEX_TYPE))
2633 errstring = "wrong type argument to abs";
2634 else if (!noconvert)
2635 arg = default_conversion (arg);
2639 /* Conjugating a real value is a no-op, but allow it anyway. */
2640 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2641 || typecode == COMPLEX_TYPE))
2642 errstring = "wrong type argument to conjugation";
2643 else if (!noconvert)
2644 arg = default_conversion (arg);
2647 case TRUTH_NOT_EXPR:
2648 if (typecode != INTEGER_TYPE
2649 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2650 && typecode != COMPLEX_TYPE
2651 /* These will convert to a pointer. */
2652 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2654 errstring = "wrong type argument to unary exclamation mark";
2657 arg = truthvalue_conversion (arg);
2658 return invert_truthvalue (arg);
2664 if (TREE_CODE (arg) == COMPLEX_CST)
2665 return TREE_REALPART (arg);
2666 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2667 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2672 if (TREE_CODE (arg) == COMPLEX_CST)
2673 return TREE_IMAGPART (arg);
2674 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2675 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2677 return convert (TREE_TYPE (arg), integer_zero_node);
2679 case PREINCREMENT_EXPR:
2680 case POSTINCREMENT_EXPR:
2681 case PREDECREMENT_EXPR:
2682 case POSTDECREMENT_EXPR:
2683 /* Handle complex lvalues (when permitted)
2684 by reduction to simpler cases. */
2686 val = unary_complex_lvalue (code, arg);
2690 /* Increment or decrement the real part of the value,
2691 and don't change the imaginary part. */
2692 if (typecode == COMPLEX_TYPE)
2696 arg = stabilize_reference (arg);
2697 real = build_unary_op (REALPART_EXPR, arg, 1);
2698 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2699 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2700 build_unary_op (code, real, 1), imag);
2703 /* Report invalid types. */
2705 if (typecode != POINTER_TYPE
2706 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2708 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2709 errstring ="wrong type argument to increment";
2711 errstring ="wrong type argument to decrement";
2717 tree result_type = TREE_TYPE (arg);
2719 arg = get_unwidened (arg, 0);
2720 argtype = TREE_TYPE (arg);
2722 /* Compute the increment. */
2724 if (typecode == POINTER_TYPE)
2726 /* If pointer target is an undefined struct,
2727 we just cannot know how to do the arithmetic. */
2728 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2729 error ("%s of pointer to unknown structure",
2730 ((code == PREINCREMENT_EXPR
2731 || code == POSTINCREMENT_EXPR)
2732 ? "increment" : "decrement"));
2733 else if ((pedantic || warn_pointer_arith)
2734 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2735 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2736 pedwarn ("wrong type argument to %s",
2737 ((code == PREINCREMENT_EXPR
2738 || code == POSTINCREMENT_EXPR)
2739 ? "increment" : "decrement"));
2740 inc = c_sizeof_nowarn (TREE_TYPE (result_type));
2743 inc = integer_one_node;
2745 inc = convert (argtype, inc);
2747 /* Handle incrementing a cast-expression. */
2750 switch (TREE_CODE (arg))
2755 case FIX_TRUNC_EXPR:
2756 case FIX_FLOOR_EXPR:
2757 case FIX_ROUND_EXPR:
2759 pedantic_lvalue_warning (CONVERT_EXPR);
2760 /* If the real type has the same machine representation
2761 as the type it is cast to, we can make better output
2762 by adding directly to the inside of the cast. */
2763 if ((TREE_CODE (TREE_TYPE (arg))
2764 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2765 && (TYPE_MODE (TREE_TYPE (arg))
2766 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2767 arg = TREE_OPERAND (arg, 0);
2770 tree incremented, modify, value;
2771 arg = stabilize_reference (arg);
2772 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2775 value = save_expr (arg);
2776 incremented = build (((code == PREINCREMENT_EXPR
2777 || code == POSTINCREMENT_EXPR)
2778 ? PLUS_EXPR : MINUS_EXPR),
2779 argtype, value, inc);
2780 TREE_SIDE_EFFECTS (incremented) = 1;
2781 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2782 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2783 TREE_USED (value) = 1;
2793 /* Complain about anything else that is not a true lvalue. */
2794 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2795 || code == POSTINCREMENT_EXPR)
2796 ? "increment" : "decrement")))
2797 return error_mark_node;
2799 /* Report a read-only lvalue. */
2800 if (TREE_READONLY (arg))
2801 readonly_warning (arg,
2802 ((code == PREINCREMENT_EXPR
2803 || code == POSTINCREMENT_EXPR)
2804 ? "increment" : "decrement"));
2806 val = build (code, TREE_TYPE (arg), arg, inc);
2807 TREE_SIDE_EFFECTS (val) = 1;
2808 val = convert (result_type, val);
2809 if (TREE_CODE (val) != code)
2810 TREE_NO_UNUSED_WARNING (val) = 1;
2815 /* Note that this operation never does default_conversion
2816 regardless of NOCONVERT. */
2818 /* Let &* cancel out to simplify resulting code. */
2819 if (TREE_CODE (arg) == INDIRECT_REF)
2821 /* Don't let this be an lvalue. */
2822 if (lvalue_p (TREE_OPERAND (arg, 0)))
2823 return non_lvalue (TREE_OPERAND (arg, 0));
2824 return TREE_OPERAND (arg, 0);
2827 /* For &x[y], return x+y */
2828 if (TREE_CODE (arg) == ARRAY_REF)
2830 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
2831 return error_mark_node;
2832 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2833 TREE_OPERAND (arg, 1), 1);
2836 /* Handle complex lvalues (when permitted)
2837 by reduction to simpler cases. */
2838 val = unary_complex_lvalue (code, arg);
2842 #if 0 /* Turned off because inconsistent;
2843 float f; *&(int)f = 3.4 stores in int format
2844 whereas (int)f = 3.4 stores in float format. */
2845 /* Address of a cast is just a cast of the address
2846 of the operand of the cast. */
2847 switch (TREE_CODE (arg))
2852 case FIX_TRUNC_EXPR:
2853 case FIX_FLOOR_EXPR:
2854 case FIX_ROUND_EXPR:
2857 pedwarn ("ANSI C forbids the address of a cast expression");
2858 return convert (build_pointer_type (TREE_TYPE (arg)),
2859 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
2864 /* Allow the address of a constructor if all the elements
2866 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
2868 /* Anything not already handled and not a true memory reference
2870 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
2871 return error_mark_node;
2873 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2874 argtype = TREE_TYPE (arg);
2875 /* If the lvalue is const or volatile,
2876 merge that into the type that the address will point to. */
2877 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
2878 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2880 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
2881 argtype = c_build_type_variant (argtype,
2882 TREE_READONLY (arg),
2883 TREE_THIS_VOLATILE (arg));
2886 argtype = build_pointer_type (argtype);
2888 if (mark_addressable (arg) == 0)
2889 return error_mark_node;
2894 if (TREE_CODE (arg) == COMPONENT_REF)
2896 tree field = TREE_OPERAND (arg, 1);
2898 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
2900 if (DECL_BIT_FIELD (field))
2902 error ("attempt to take address of bit-field structure member `%s'",
2903 IDENTIFIER_POINTER (DECL_NAME (field)));
2904 return error_mark_node;
2907 addr = convert (argtype, addr);
2909 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
2912 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
2913 size_int (BITS_PER_UNIT));
2914 int flag = TREE_CONSTANT (addr);
2915 addr = fold (build (PLUS_EXPR, argtype,
2916 addr, convert (argtype, offset)));
2917 TREE_CONSTANT (addr) = flag;
2921 addr = build1 (code, argtype, arg);
2923 /* Address of a static or external variable or
2924 file-scope function counts as a constant. */
2926 && ! (TREE_CODE (arg) == FUNCTION_DECL
2927 && DECL_CONTEXT (arg) != 0))
2928 TREE_CONSTANT (addr) = 1;
2936 argtype = TREE_TYPE (arg);
2937 return fold (build1 (code, argtype, arg));
2941 return error_mark_node;
2945 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
2946 convert ARG with the same conversions in the same order
2947 and return the result. */
2950 convert_sequence (conversions, arg)
2954 switch (TREE_CODE (conversions))
2959 case FIX_TRUNC_EXPR:
2960 case FIX_FLOOR_EXPR:
2961 case FIX_ROUND_EXPR:
2963 return convert (TREE_TYPE (conversions),
2964 convert_sequence (TREE_OPERAND (conversions, 0),
2973 /* Return nonzero if REF is an lvalue valid for this language.
2974 Lvalues can be assigned, unless their type has TYPE_READONLY.
2975 Lvalues can have their address taken, unless they have DECL_REGISTER. */
2981 register enum tree_code code = TREE_CODE (ref);
2988 return lvalue_p (TREE_OPERAND (ref, 0));
2999 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3000 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
3007 /* Return nonzero if REF is an lvalue valid for this language;
3008 otherwise, print an error message and return zero. */
3011 lvalue_or_else (ref, string)
3015 int win = lvalue_p (ref);
3017 error ("invalid lvalue in %s", string);
3021 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3022 for certain kinds of expressions which are not really lvalues
3023 but which we can accept as lvalues.
3025 If ARG is not a kind of expression we can handle, return zero. */
3028 unary_complex_lvalue (code, arg)
3029 enum tree_code code;
3032 /* Handle (a, b) used as an "lvalue". */
3033 if (TREE_CODE (arg) == COMPOUND_EXPR)
3035 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3036 pedantic_lvalue_warning (COMPOUND_EXPR);
3037 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3038 TREE_OPERAND (arg, 0), real_result);
3041 /* Handle (a ? b : c) used as an "lvalue". */
3042 if (TREE_CODE (arg) == COND_EXPR)
3044 pedantic_lvalue_warning (COND_EXPR);
3045 return (build_conditional_expr
3046 (TREE_OPERAND (arg, 0),
3047 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3048 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3054 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3055 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3058 pedantic_lvalue_warning (code)
3059 enum tree_code code;
3062 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3063 code == COND_EXPR ? "conditional"
3064 : code == COMPOUND_EXPR ? "compound" : "cast");
3067 /* Warn about storing in something that is `const'. */
3070 readonly_warning (arg, string)
3075 strcpy (buf, string);
3077 /* Forbid assignments to iterators. */
3078 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3080 strcat (buf, " of iterator `%s'");
3081 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3084 if (TREE_CODE (arg) == COMPONENT_REF)
3086 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3087 readonly_warning (TREE_OPERAND (arg, 0), string);
3090 strcat (buf, " of read-only member `%s'");
3091 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3094 else if (TREE_CODE (arg) == VAR_DECL)
3096 strcat (buf, " of read-only variable `%s'");
3097 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3101 pedwarn ("%s of read-only location", buf);
3105 /* Mark EXP saying that we need to be able to take the
3106 address of it; it should not be allocated in a register.
3107 Value is 1 if successful. */
3110 mark_addressable (exp)
3113 register tree x = exp;
3115 switch (TREE_CODE (x))
3122 x = TREE_OPERAND (x, 0);
3126 TREE_ADDRESSABLE (x) = 1;
3133 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3134 && DECL_NONLOCAL (x))
3136 if (TREE_PUBLIC (x))
3138 error ("global register variable `%s' used in nested function",
3139 IDENTIFIER_POINTER (DECL_NAME (x)));
3142 pedwarn ("register variable `%s' used in nested function",
3143 IDENTIFIER_POINTER (DECL_NAME (x)));
3145 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3147 if (TREE_PUBLIC (x))
3149 error ("address of global register variable `%s' requested",
3150 IDENTIFIER_POINTER (DECL_NAME (x)));
3153 pedwarn ("address of register variable `%s' requested",
3154 IDENTIFIER_POINTER (DECL_NAME (x)));
3156 put_var_into_stack (x);
3160 TREE_ADDRESSABLE (x) = 1;
3161 #if 0 /* poplevel deals with this now. */
3162 if (DECL_CONTEXT (x) == 0)
3163 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3171 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3174 build_conditional_expr (ifexp, op1, op2)
3175 tree ifexp, op1, op2;
3177 register tree type1;
3178 register tree type2;
3179 register enum tree_code code1;
3180 register enum tree_code code2;
3181 register tree result_type = NULL;
3182 tree orig_op1 = op1, orig_op2 = op2;
3184 /* If second operand is omitted, it is the same as the first one;
3185 make sure it is calculated only once. */
3189 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression");
3190 ifexp = op1 = save_expr (ifexp);
3193 ifexp = truthvalue_conversion (default_conversion (ifexp));
3195 #if 0 /* Produces wrong result if within sizeof. */
3196 /* Don't promote the operands separately if they promote
3197 the same way. Return the unpromoted type and let the combined
3198 value get promoted if necessary. */
3200 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3201 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3202 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3203 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3205 if (TREE_CODE (ifexp) == INTEGER_CST)
3206 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3208 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3212 /* Promote both alternatives. */
3214 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3215 op1 = default_conversion (op1);
3216 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3217 op2 = default_conversion (op2);
3219 if (TREE_CODE (ifexp) == ERROR_MARK
3220 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3221 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3222 return error_mark_node;
3224 type1 = TREE_TYPE (op1);
3225 code1 = TREE_CODE (type1);
3226 type2 = TREE_TYPE (op2);
3227 code2 = TREE_CODE (type2);
3229 /* Quickly detect the usual case where op1 and op2 have the same type
3231 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3234 result_type = type1;
3236 result_type = TYPE_MAIN_VARIANT (type1);
3238 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3239 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3241 result_type = common_type (type1, type2);
3243 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3245 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3246 pedwarn ("ANSI C forbids conditional expr with only one void side");
3247 result_type = void_type_node;
3249 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3251 if (comp_target_types (type1, type2))
3252 result_type = common_type (type1, type2);
3253 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3254 && TREE_CODE (orig_op1) != NOP_EXPR)
3255 result_type = qualify_type (type2, type1);
3256 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3257 && TREE_CODE (orig_op2) != NOP_EXPR)
3258 result_type = qualify_type (type1, type2);
3259 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3261 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3262 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3263 result_type = qualify_type (type1, type2);
3265 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3267 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3268 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3269 result_type = qualify_type (type2, type1);
3273 pedwarn ("pointer type mismatch in conditional expression");
3274 result_type = build_pointer_type (void_type_node);
3277 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3279 if (! integer_zerop (op2))
3280 pedwarn ("pointer/integer type mismatch in conditional expression");
3283 op2 = null_pointer_node;
3284 #if 0 /* The spec seems to say this is permitted. */
3285 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3286 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3289 result_type = type1;
3291 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3293 if (!integer_zerop (op1))
3294 pedwarn ("pointer/integer type mismatch in conditional expression");
3297 op1 = null_pointer_node;
3298 #if 0 /* The spec seems to say this is permitted. */
3299 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3300 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3303 result_type = type2;
3308 if (flag_cond_mismatch)
3309 result_type = void_type_node;
3312 error ("type mismatch in conditional expression");
3313 return error_mark_node;
3317 /* Merge const and volatile flags of the incoming types. */
3319 = build_type_variant (result_type,
3320 TREE_READONLY (op1) || TREE_READONLY (op2),
3321 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3323 if (result_type != TREE_TYPE (op1))
3324 op1 = convert_and_check (result_type, op1);
3325 if (result_type != TREE_TYPE (op2))
3326 op2 = convert_and_check (result_type, op2);
3329 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3331 result_type = TREE_TYPE (op1);
3332 if (TREE_CONSTANT (ifexp))
3333 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3335 if (TYPE_MODE (result_type) == BLKmode)
3337 register tree tempvar
3338 = build_decl (VAR_DECL, NULL_TREE, result_type);
3339 register tree xop1 = build_modify_expr (tempvar, op1);
3340 register tree xop2 = build_modify_expr (tempvar, op2);
3341 register tree result = fold (build (COND_EXPR, result_type,
3342 ifexp, xop1, xop2));
3344 layout_decl (tempvar, TYPE_ALIGN (result_type));
3345 /* No way to handle variable-sized objects here.
3346 I fear that the entire handling of BLKmode conditional exprs
3347 needs to be redone. */
3348 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3351 = assign_stack_local (DECL_MODE (tempvar),
3352 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3353 + BITS_PER_UNIT - 1)
3357 TREE_SIDE_EFFECTS (result)
3358 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3359 | TREE_SIDE_EFFECTS (op2);
3360 return build (COMPOUND_EXPR, result_type, result, tempvar);
3365 if (TREE_CODE (ifexp) == INTEGER_CST)
3366 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3368 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3371 /* Given a list of expressions, return a compound expression
3372 that performs them all and returns the value of the last of them. */
3375 build_compound_expr (list)
3378 return internal_build_compound_expr (list, TRUE);
3382 internal_build_compound_expr (list, first_p)
3388 if (TREE_CHAIN (list) == 0)
3390 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3391 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3393 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3394 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3395 list = TREE_OPERAND (list, 0);
3398 /* Don't let (0, 0) be null pointer constant. */
3399 if (!first_p && integer_zerop (TREE_VALUE (list)))
3400 return non_lvalue (TREE_VALUE (list));
3401 return TREE_VALUE (list);
3404 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3406 /* Convert arrays to pointers when there really is a comma operator. */
3407 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3408 TREE_VALUE (TREE_CHAIN (list))
3409 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3412 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3414 /* When pedantic, a compound expression can be neither an lvalue
3415 nor an integer constant expression. */
3416 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
3419 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3422 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3425 build_c_cast (type, expr)
3429 register tree value = expr;
3431 if (type == error_mark_node || expr == error_mark_node)
3432 return error_mark_node;
3433 type = TYPE_MAIN_VARIANT (type);
3436 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3437 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3438 value = TREE_OPERAND (value, 0);
3441 if (TREE_CODE (type) == ARRAY_TYPE)
3443 error ("cast specifies array type");
3444 return error_mark_node;
3447 if (TREE_CODE (type) == FUNCTION_TYPE)
3449 error ("cast specifies function type");
3450 return error_mark_node;
3453 if (type == TREE_TYPE (value))
3457 if (TREE_CODE (type) == RECORD_TYPE
3458 || TREE_CODE (type) == UNION_TYPE)
3459 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3462 else if (TREE_CODE (type) == UNION_TYPE)
3465 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3466 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3467 value = default_conversion (value);
3469 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3470 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3471 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3480 pedwarn ("ANSI C forbids casts to union type");
3481 if (TYPE_NAME (type) != 0)
3483 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3484 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3486 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3490 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3491 build_tree_list (field, value)),
3493 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3496 error ("cast to union type from type not present in union");
3497 return error_mark_node;
3503 /* If casting to void, avoid the error that would come
3504 from default_conversion in the case of a non-lvalue array. */
3505 if (type == void_type_node)
3506 return build1 (CONVERT_EXPR, type, value);
3508 /* Convert functions and arrays to pointers,
3509 but don't convert any other types. */
3510 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3511 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3512 value = default_conversion (value);
3513 otype = TREE_TYPE (value);
3515 /* Optionally warn about potentially worrisome casts. */
3518 && TREE_CODE (type) == POINTER_TYPE
3519 && TREE_CODE (otype) == POINTER_TYPE)
3521 if (TYPE_VOLATILE (TREE_TYPE (otype))
3522 && ! TYPE_VOLATILE (TREE_TYPE (type)))
3523 pedwarn ("cast discards `volatile' from pointer target type");
3524 if (TYPE_READONLY (TREE_TYPE (otype))
3525 && ! TYPE_READONLY (TREE_TYPE (type)))
3526 pedwarn ("cast discards `const' from pointer target type");
3529 /* Warn about possible alignment problems. */
3530 if (STRICT_ALIGNMENT && warn_cast_align
3531 && TREE_CODE (type) == POINTER_TYPE
3532 && TREE_CODE (otype) == POINTER_TYPE
3533 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3534 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3535 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3536 warning ("cast increases required alignment of target type");
3538 if (TREE_CODE (type) == INTEGER_TYPE
3539 && TREE_CODE (otype) == POINTER_TYPE
3540 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3541 && !TREE_CONSTANT (value))
3542 warning ("cast from pointer to integer of different size");
3544 if (TREE_CODE (type) == POINTER_TYPE
3545 && TREE_CODE (otype) == INTEGER_TYPE
3546 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3548 /* Don't warn about converting 0 to pointer,
3549 provided the 0 was explicit--not cast or made by folding. */
3550 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3552 /* Don't warn about converting any constant. */
3553 && !TREE_CONSTANT (value))
3554 warning ("cast to pointer from integer of different size");
3557 value = convert (type, value);
3559 /* Ignore any integer overflow caused by the cast. */
3560 if (TREE_CODE (value) == INTEGER_CST)
3562 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3563 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3567 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3568 if (pedantic && TREE_CODE (value) == INTEGER_CST
3569 && TREE_CODE (expr) == INTEGER_CST
3570 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3571 value = non_lvalue (value);
3573 /* If pedantic, don't let a cast be an lvalue. */
3574 if (value == expr && pedantic)
3575 value = non_lvalue (value);
3580 /* Build an assignment expression of lvalue LHS from value RHS.
3581 MODIFYCODE is the code for a binary operator that we use
3582 to combine the old value of LHS with RHS to get the new value.
3583 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3586 build_modify_expr (lhs, modifycode, rhs)
3588 enum tree_code modifycode;
3590 register tree result;
3592 tree lhstype = TREE_TYPE (lhs);
3593 tree olhstype = lhstype;
3595 /* Types that aren't fully specified cannot be used in assignments. */
3596 lhs = require_complete_type (lhs);
3598 /* Avoid duplicate error messages from operands that had errors. */
3599 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3600 return error_mark_node;
3602 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3603 /* Do not use STRIP_NOPS here. We do not want an enumerator
3604 whose value is 0 to count as a null pointer constant. */
3605 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3606 rhs = TREE_OPERAND (rhs, 0);
3610 /* Handle control structure constructs used as "lvalues". */
3612 switch (TREE_CODE (lhs))
3614 /* Handle (a, b) used as an "lvalue". */
3616 pedantic_lvalue_warning (COMPOUND_EXPR);
3617 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3619 if (TREE_CODE (newrhs) == ERROR_MARK)
3620 return error_mark_node;
3621 return build (COMPOUND_EXPR, lhstype,
3622 TREE_OPERAND (lhs, 0), newrhs);
3624 /* Handle (a ? b : c) used as an "lvalue". */
3626 pedantic_lvalue_warning (COND_EXPR);
3627 rhs = save_expr (rhs);
3629 /* Produce (a ? (b = rhs) : (c = rhs))
3630 except that the RHS goes through a save-expr
3631 so the code to compute it is only emitted once. */
3633 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3634 build_modify_expr (TREE_OPERAND (lhs, 1),
3636 build_modify_expr (TREE_OPERAND (lhs, 2),
3638 if (TREE_CODE (cond) == ERROR_MARK)
3640 /* Make sure the code to compute the rhs comes out
3641 before the split. */
3642 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3643 /* But cast it to void to avoid an "unused" error. */
3644 convert (void_type_node, rhs), cond);
3648 /* If a binary op has been requested, combine the old LHS value with the RHS
3649 producing the value we should actually store into the LHS. */
3651 if (modifycode != NOP_EXPR)
3653 lhs = stabilize_reference (lhs);
3654 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3657 /* Handle a cast used as an "lvalue".
3658 We have already performed any binary operator using the value as cast.
3659 Now convert the result to the cast type of the lhs,
3660 and then true type of the lhs and store it there;
3661 then convert result back to the cast type to be the value
3662 of the assignment. */
3664 switch (TREE_CODE (lhs))
3669 case FIX_TRUNC_EXPR:
3670 case FIX_FLOOR_EXPR:
3671 case FIX_ROUND_EXPR:
3673 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3674 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3675 newrhs = default_conversion (newrhs);
3677 tree inner_lhs = TREE_OPERAND (lhs, 0);
3679 result = build_modify_expr (inner_lhs, NOP_EXPR,
3680 convert (TREE_TYPE (inner_lhs),
3681 convert (lhstype, newrhs)));
3682 if (TREE_CODE (result) == ERROR_MARK)
3684 pedantic_lvalue_warning (CONVERT_EXPR);
3685 return convert (TREE_TYPE (lhs), result);
3689 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3690 Reject anything strange now. */
3692 if (!lvalue_or_else (lhs, "assignment"))
3693 return error_mark_node;
3695 /* Warn about storing in something that is `const'. */
3697 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3698 || ((TREE_CODE (lhstype) == RECORD_TYPE
3699 || TREE_CODE (lhstype) == UNION_TYPE)
3700 && C_TYPE_FIELDS_READONLY (lhstype)))
3701 readonly_warning (lhs, "assignment");
3703 /* If storing into a structure or union member,
3704 it has probably been given type `int'.
3705 Compute the type that would go with
3706 the actual amount of storage the member occupies. */
3708 if (TREE_CODE (lhs) == COMPONENT_REF
3709 && (TREE_CODE (lhstype) == INTEGER_TYPE
3710 || TREE_CODE (lhstype) == REAL_TYPE
3711 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3712 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3714 /* If storing in a field that is in actuality a short or narrower than one,
3715 we must store in the field in its actual type. */
3717 if (lhstype != TREE_TYPE (lhs))
3719 lhs = copy_node (lhs);
3720 TREE_TYPE (lhs) = lhstype;
3723 /* Convert new value to destination type. */
3725 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
3726 NULL_TREE, NULL_TREE, 0);
3727 if (TREE_CODE (newrhs) == ERROR_MARK)
3728 return error_mark_node;
3730 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3731 TREE_SIDE_EFFECTS (result) = 1;
3733 /* If we got the LHS in a different type for storing in,
3734 convert the result back to the nominal type of LHS
3735 so that the value we return always has the same type
3736 as the LHS argument. */
3738 if (olhstype == TREE_TYPE (result))
3740 return convert_for_assignment (olhstype, result, "assignment",
3741 NULL_TREE, NULL_TREE, 0);
3744 /* Convert value RHS to type TYPE as preparation for an assignment
3745 to an lvalue of type TYPE.
3746 The real work of conversion is done by `convert'.
3747 The purpose of this function is to generate error messages
3748 for assignments that are not allowed in C.
3749 ERRTYPE is a string to use in error messages:
3750 "assignment", "return", etc. If it is null, this is parameter passing
3751 for a function call (and different error messages are output). Otherwise,
3752 it may be a name stored in the spelling stack and interpreted by
3755 FUNNAME is the name of the function being called,
3756 as an IDENTIFIER_NODE, or null.
3757 PARMNUM is the number of the argument, for printing in error messages. */
3760 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3763 tree fundecl, funname;
3766 register enum tree_code codel = TREE_CODE (type);
3767 register tree rhstype;
3768 register enum tree_code coder;
3770 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3771 /* Do not use STRIP_NOPS here. We do not want an enumerator
3772 whose value is 0 to count as a null pointer constant. */
3773 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3774 rhs = TREE_OPERAND (rhs, 0);
3776 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3777 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3778 rhs = default_conversion (rhs);
3780 rhstype = TREE_TYPE (rhs);
3781 coder = TREE_CODE (rhstype);
3783 if (coder == ERROR_MARK)
3784 return error_mark_node;
3786 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3788 overflow_warning (rhs);
3789 /* Check for Objective-C protocols. This will issue a warning if
3790 there are protocol violations. No need to use the return value. */
3791 maybe_objc_comptypes (type, rhstype, 0);
3795 if (coder == VOID_TYPE)
3797 error ("void value not ignored as it ought to be");
3798 return error_mark_node;
3800 /* Arithmetic types all interconvert, and enum is treated like int. */
3801 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
3802 || codel == COMPLEX_TYPE)
3804 (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
3805 || coder == COMPLEX_TYPE))
3806 return convert_and_check (type, rhs);
3807 /* Conversion to a union from its member types. */
3808 else if (codel == UNION_TYPE)
3811 for (memb_types = TYPE_FIELDS (type); memb_types;
3812 memb_types = TREE_CHAIN (memb_types))
3814 if (comptypes (TREE_TYPE (memb_types), TREE_TYPE (rhs)))
3817 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
3818 pedwarn ("ANSI C prohibits argument conversion to union type");
3819 return build1 (NOP_EXPR, type, rhs);
3821 else if (coder == POINTER_TYPE
3822 && TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE)
3824 tree memb_type = TREE_TYPE (memb_types);
3825 register tree ttl = TREE_TYPE (memb_type);
3826 register tree ttr = TREE_TYPE (rhstype);
3828 /* Any non-function converts to a [const][volatile] void *
3829 and vice versa; otherwise, targets must be the same.
3830 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3831 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3832 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3833 || comp_target_types (memb_type, rhstype))
3835 /* Const and volatile mean something different for function types,
3836 so the usual warnings are not appropriate. */
3837 if (TREE_CODE (ttr) != FUNCTION_TYPE
3838 || TREE_CODE (ttl) != FUNCTION_TYPE)
3840 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
3841 warn_for_assignment ("%s discards `const' from pointer target type",
3842 get_spelling (errtype), funname, parmnum);
3843 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
3844 warn_for_assignment ("%s discards `volatile' from pointer target type",
3845 get_spelling (errtype), funname, parmnum);
3849 /* Because const and volatile on functions are restrictions
3850 that say the function will not do certain things,
3851 it is okay to use a const or volatile function
3852 where an ordinary one is wanted, but not vice-versa. */
3853 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
3854 warn_for_assignment ("%s makes `const *' function pointer from non-const",
3855 get_spelling (errtype), funname, parmnum);
3856 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
3857 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
3858 get_spelling (errtype), funname, parmnum);
3861 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
3862 pedwarn ("ANSI C prohibits argument conversion to union type");
3863 return build1 (NOP_EXPR, type, rhs);
3868 /* Conversions among pointers */
3869 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
3871 register tree ttl = TREE_TYPE (type);
3872 register tree ttr = TREE_TYPE (rhstype);
3874 /* Any non-function converts to a [const][volatile] void *
3875 and vice versa; otherwise, targets must be the same.
3876 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3877 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3878 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3879 || comp_target_types (type, rhstype)
3880 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
3881 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3884 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
3885 && TREE_CODE (ttr) == FUNCTION_TYPE)
3887 (TYPE_MAIN_VARIANT (ttr) == void_type_node
3888 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3889 which are not ANSI null ptr constants. */
3890 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3891 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3892 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
3893 get_spelling (errtype), funname, parmnum);
3894 /* Const and volatile mean something different for function types,
3895 so the usual warnings are not appropriate. */
3896 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3897 || TREE_CODE (ttl) != FUNCTION_TYPE)
3899 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
3900 warn_for_assignment ("%s discards `const' from pointer target type",
3901 get_spelling (errtype), funname, parmnum);
3902 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
3903 warn_for_assignment ("%s discards `volatile' from pointer target type",
3904 get_spelling (errtype), funname, parmnum);
3905 /* If this is not a case of ignoring a mismatch in signedness,
3907 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3908 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3909 || comp_target_types (type, rhstype))
3911 /* If there is a mismatch, do warn. */
3913 warn_for_assignment ("pointer targets in %s differ in signedness",
3914 get_spelling (errtype), funname, parmnum);
3918 /* Because const and volatile on functions are restrictions
3919 that say the function will not do certain things,
3920 it is okay to use a const or volatile function
3921 where an ordinary one is wanted, but not vice-versa. */
3922 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
3923 warn_for_assignment ("%s makes `const *' function pointer from non-const",
3924 get_spelling (errtype), funname, parmnum);
3925 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
3926 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
3927 get_spelling (errtype), funname, parmnum);
3931 warn_for_assignment ("%s from incompatible pointer type",
3932 get_spelling (errtype), funname, parmnum);
3933 return convert (type, rhs);
3935 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3937 /* An explicit constant 0 can convert to a pointer,
3938 or one that results from arithmetic, even including
3939 a cast to integer type. */
3940 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3942 ! (TREE_CODE (rhs) == NOP_EXPR
3943 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3944 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3945 && integer_zerop (TREE_OPERAND (rhs, 0))))
3947 warn_for_assignment ("%s makes pointer from integer without a cast",
3948 get_spelling (errtype), funname, parmnum);
3949 return convert (type, rhs);
3951 return null_pointer_node;
3953 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3955 warn_for_assignment ("%s makes integer from pointer without a cast",
3956 get_spelling (errtype), funname, parmnum);
3957 return convert (type, rhs);
3964 tree selector = maybe_building_objc_message_expr ();
3966 if (selector && parmnum > 2)
3967 error ("incompatible type for argument %d of `%s'",
3968 parmnum - 2, IDENTIFIER_POINTER (selector));
3970 error ("incompatible type for argument %d of `%s'",
3971 parmnum, IDENTIFIER_POINTER (funname));
3974 error ("incompatible type for argument %d of indirect function call",
3978 error ("incompatible types in %s", get_spelling (errtype));
3980 return error_mark_node;
3983 /* Print a warning using MSG.
3984 It gets OPNAME as its one parameter.
3985 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3986 FUNCTION and ARGNUM are handled specially if we are building an
3987 Objective-C selector. */
3990 warn_for_assignment (msg, opname, function, argnum)
3996 static char argstring[] = "passing arg %d of `%s'";
3997 static char argnofun[] = "passing arg %d";
4001 tree selector = maybe_building_objc_message_expr ();
4003 if (selector && argnum > 2)
4005 function = selector;
4010 /* Function name is known; supply it. */
4011 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4012 + sizeof (argstring) + 25 /*%d*/ + 1);
4013 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4017 /* Function name unknown (call through ptr); just give arg number. */
4018 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4019 sprintf (opname, argnofun, argnum);
4022 pedwarn (msg, opname);
4025 /* Return nonzero if VALUE is a valid constant-valued expression
4026 for use in initializing a static variable; one that can be an
4027 element of a "constant" initializer.
4029 Return null_pointer_node if the value is absolute;
4030 if it is relocatable, return the variable that determines the relocation.
4031 We assume that VALUE has been folded as much as possible;
4032 therefore, we do not need to check for such things as
4033 arithmetic-combinations of integers. */
4036 initializer_constant_valid_p (value, endtype)
4040 switch (TREE_CODE (value))
4043 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4044 && TREE_CONSTANT (value))
4045 return initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)));
4047 return TREE_STATIC (value) ? null_pointer_node : 0;
4053 return null_pointer_node;
4056 return TREE_OPERAND (value, 0);
4058 case NON_LVALUE_EXPR:
4059 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4063 /* Allow conversions between pointer types. */
4064 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4065 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
4066 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4067 /* Allow conversions between real types. */
4068 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4069 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
4070 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4071 /* Allow length-preserving conversions between integer types. */
4072 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4073 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4074 && tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (value)),
4075 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value, 0)))))
4076 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4077 /* Allow conversions between integer types only if explicit value. */
4078 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4079 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4081 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4083 if (inner == null_pointer_node)
4084 return null_pointer_node;
4087 /* Allow (int) &foo provided int is as wide as a pointer. */
4088 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4089 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
4090 && ! tree_int_cst_lt (TYPE_SIZE (TREE_TYPE (value)),
4091 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value, 0)))))
4092 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4094 /* Allow conversions to union types if the value inside is okay. */
4095 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4096 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4101 if (TREE_CODE (endtype) == INTEGER_TYPE
4102 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4105 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4107 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4109 /* If either term is absolute, use the other terms relocation. */
4110 if (valid0 == null_pointer_node)
4112 if (valid1 == null_pointer_node)
4118 if (TREE_CODE (endtype) == INTEGER_TYPE
4119 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4122 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4124 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4126 /* Win if second argument is absolute. */
4127 if (valid1 == null_pointer_node)
4129 /* Win if both arguments have the same relocation.
4130 Then the value is absolute. */
4131 if (valid0 == valid1)
4132 return null_pointer_node;
4140 /* If VALUE is a compound expr all of whose expressions are constant, then
4141 return its value. Otherwise, return error_mark_node.
4143 This is for handling COMPOUND_EXPRs as initializer elements
4144 which is allowed with a warning when -pedantic is specified. */
4147 valid_compound_expr_initializer (value, endtype)
4151 if (TREE_CODE (value) == COMPOUND_EXPR)
4153 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4155 return error_mark_node;
4156 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4159 else if (! TREE_CONSTANT (value)
4160 && ! initializer_constant_valid_p (value, endtype))
4161 return error_mark_node;
4166 /* Perform appropriate conversions on the initial value of a variable,
4167 store it in the declaration DECL,
4168 and print any error messages that are appropriate.
4169 If the init is invalid, store an ERROR_MARK. */
4172 store_init_value (decl, init)
4175 register tree value, type;
4177 /* If variable's type was invalidly declared, just ignore it. */
4179 type = TREE_TYPE (decl);
4180 if (TREE_CODE (type) == ERROR_MARK)
4183 /* Digest the specified initializer into an expression. */
4185 value = digest_init (type, init, TREE_STATIC (decl),
4186 TREE_STATIC (decl) || pedantic);
4188 /* Store the expression if valid; else report error. */
4191 /* Note that this is the only place we can detect the error
4192 in a case such as struct foo bar = (struct foo) { x, y };
4193 where there is one initial value which is a constructor expression. */
4194 if (value == error_mark_node)
4196 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4198 error ("initializer for static variable is not constant");
4199 value = error_mark_node;
4201 else if (TREE_STATIC (decl)
4202 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4204 error ("initializer for static variable uses complicated arithmetic");
4205 value = error_mark_node;
4209 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4211 if (! TREE_CONSTANT (value))
4212 pedwarn ("aggregate initializer is not constant");
4213 else if (! TREE_STATIC (value))
4214 pedwarn ("aggregate initializer uses complicated arithmetic");
4219 DECL_INITIAL (decl) = value;
4221 /* ANSI wants warnings about out-of-range constant initializers. */
4222 STRIP_TYPE_NOPS (value);
4223 constant_expression_warning (value);
4226 /* Methods for storing and printing names for error messages. */
4228 /* Implement a spelling stack that allows components of a name to be pushed
4229 and popped. Each element on the stack is this structure. */
4241 #define SPELLING_STRING 1
4242 #define SPELLING_MEMBER 2
4243 #define SPELLING_BOUNDS 3
4245 static struct spelling *spelling; /* Next stack element (unused). */
4246 static struct spelling *spelling_base; /* Spelling stack base. */
4247 static int spelling_size; /* Size of the spelling stack. */
4249 /* Macros to save and restore the spelling stack around push_... functions.
4250 Alternative to SAVE_SPELLING_STACK. */
4252 #define SPELLING_DEPTH() (spelling - spelling_base)
4253 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4255 /* Save and restore the spelling stack around arbitrary C code. */
4257 #define SAVE_SPELLING_DEPTH(code) \
4259 int __depth = SPELLING_DEPTH (); \
4261 RESTORE_SPELLING_DEPTH (__depth); \
4264 /* Push an element on the spelling stack with type KIND and assign VALUE
4267 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4269 int depth = SPELLING_DEPTH (); \
4271 if (depth >= spelling_size) \
4273 spelling_size += 10; \
4274 if (spelling_base == 0) \
4276 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4279 = (struct spelling *) xrealloc (spelling_base, \
4280 spelling_size * sizeof (struct spelling)); \
4281 RESTORE_SPELLING_DEPTH (depth); \
4284 spelling->kind = (KIND); \
4285 spelling->MEMBER = (VALUE); \
4289 /* Push STRING on the stack. Printed literally. */
4292 push_string (string)
4295 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4298 /* Push a member name on the stack. Printed as '.' STRING. */
4301 push_member_name (decl)
4306 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4307 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4310 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4313 push_array_bounds (bounds)
4316 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4319 /* Compute the maximum size in bytes of the printed spelling. */
4324 register int size = 0;
4325 register struct spelling *p;
4327 for (p = spelling_base; p < spelling; p++)
4329 if (p->kind == SPELLING_BOUNDS)
4332 size += strlen (p->u.s) + 1;
4338 /* Print the spelling to BUFFER and return it. */
4341 print_spelling (buffer)
4342 register char *buffer;
4344 register char *d = buffer;
4346 register struct spelling *p;
4348 for (p = spelling_base; p < spelling; p++)
4349 if (p->kind == SPELLING_BOUNDS)
4351 sprintf (d, "[%d]", p->u.i);
4356 if (p->kind == SPELLING_MEMBER)
4358 for (s = p->u.s; *d = *s++; d++)
4365 /* Provide a means to pass component names derived from the spelling stack. */
4367 char initialization_message;
4369 /* Interpret the spelling of the given ERRTYPE message. */
4372 get_spelling (errtype)
4375 static char *buffer;
4376 static int size = -1;
4378 if (errtype == &initialization_message)
4380 /* Avoid counting chars */
4381 static char message[] = "initialization of `%s'";
4382 register int needed = sizeof (message) + spelling_length () + 1;
4386 buffer = (char *) xmalloc (size = needed);
4388 buffer = (char *) xrealloc (buffer, size = needed);
4390 temp = (char *) alloca (needed);
4391 sprintf (buffer, message, print_spelling (temp));
4398 /* Issue an error message for a bad initializer component.
4399 FORMAT describes the message. OFWHAT is the name for the component.
4400 LOCAL is a format string for formatting the insertion of the name
4403 If OFWHAT is null, the component name is stored on the spelling stack.
4404 If the component name is a null string, then LOCAL is omitted entirely. */
4407 error_init (format, local, ofwhat)
4408 char *format, *local, *ofwhat;
4413 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4414 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4417 sprintf (buffer, local, ofwhat);
4421 error (format, buffer);
4424 /* Issue a pedantic warning for a bad initializer component.
4425 FORMAT describes the message. OFWHAT is the name for the component.
4426 LOCAL is a format string for formatting the insertion of the name
4429 If OFWHAT is null, the component name is stored on the spelling stack.
4430 If the component name is a null string, then LOCAL is omitted entirely. */
4433 pedwarn_init (format, local, ofwhat)
4434 char *format, *local, *ofwhat;
4439 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4440 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4443 sprintf (buffer, local, ofwhat);
4447 pedwarn (format, buffer);
4450 /* Issue a warning for a bad initializer component.
4451 FORMAT describes the message. OFWHAT is the name for the component.
4452 LOCAL is a format string for formatting the insertion of the name
4455 If OFWHAT is null, the component name is stored on the spelling stack.
4456 If the component name is a null string, then LOCAL is omitted entirely. */
4459 warning_init (format, local, ofwhat)
4460 char *format, *local, *ofwhat;
4465 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4466 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4469 sprintf (buffer, local, ofwhat);
4473 warning (format, buffer);
4476 /* Digest the parser output INIT as an initializer for type TYPE.
4477 Return a C expression of type TYPE to represent the initial value.
4479 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4480 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4481 applies only to elements of constructors. */
4484 digest_init (type, init, require_constant, constructor_constant)
4486 int require_constant, constructor_constant;
4488 enum tree_code code = TREE_CODE (type);
4489 tree inside_init = init;
4491 if (init == error_mark_node)
4494 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4495 /* Do not use STRIP_NOPS here. We do not want an enumerator
4496 whose value is 0 to count as a null pointer constant. */
4497 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4498 inside_init = TREE_OPERAND (init, 0);
4500 /* Initialization of an array of chars from a string constant
4501 optionally enclosed in braces. */
4503 if (code == ARRAY_TYPE)
4505 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4506 if ((typ1 == char_type_node
4507 || typ1 == signed_char_type_node
4508 || typ1 == unsigned_char_type_node
4509 || typ1 == unsigned_wchar_type_node
4510 || typ1 == signed_wchar_type_node)
4511 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4513 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4514 TYPE_MAIN_VARIANT (type)))
4517 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4519 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4521 error_init ("char-array%s initialized from wide string",
4523 return error_mark_node;
4525 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4527 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4529 error_init ("int-array%s initialized from non-wide string",
4531 return error_mark_node;
4534 TREE_TYPE (inside_init) = type;
4535 if (TYPE_DOMAIN (type) != 0
4536 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4538 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4539 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4540 /* Subtract 1 (or sizeof (wchar_t))
4541 because it's ok to ignore the terminating null char
4542 that is counted in the length of the constant. */
4543 if (size < TREE_STRING_LENGTH (inside_init)
4544 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4545 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4548 "initializer-string for array of chars%s is too long",
4555 /* Any type can be initialized
4556 from an expression of the same type, optionally with braces. */
4558 if (inside_init && TREE_TYPE (inside_init) != 0
4559 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4560 TYPE_MAIN_VARIANT (type))
4561 || (code == ARRAY_TYPE
4562 && comptypes (TREE_TYPE (inside_init), type))
4563 || (code == POINTER_TYPE
4564 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4565 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4566 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4567 TREE_TYPE (type)))))
4569 if (code == POINTER_TYPE
4570 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4571 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4572 inside_init = default_conversion (inside_init);
4573 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4574 && TREE_CODE (inside_init) != CONSTRUCTOR)
4576 error_init ("array%s initialized from non-constant array expression",
4578 return error_mark_node;
4581 if (optimize && TREE_READONLY (inside_init)
4582 && TREE_CODE (inside_init) == VAR_DECL)
4583 inside_init = decl_constant_value (inside_init);
4585 /* Compound expressions can only occur here if -pedantic or
4586 -pedantic-errors is specified. In the later case, we always want
4587 an error. In the former case, we simply want a warning. */
4588 if (require_constant && pedantic
4589 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4592 = valid_compound_expr_initializer (inside_init,
4593 TREE_TYPE (inside_init));
4594 if (inside_init == error_mark_node)
4595 error_init ("initializer element%s is not constant",
4598 pedwarn_init ("initializer element%s is not constant",
4600 if (flag_pedantic_errors)
4601 inside_init = error_mark_node;
4603 else if (require_constant && ! TREE_CONSTANT (inside_init))
4605 error_init ("initializer element%s is not constant",
4607 inside_init = error_mark_node;
4609 else if (require_constant
4610 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4612 error_init ("initializer element%s is not computable at load time",
4614 inside_init = error_mark_node;
4620 /* Handle scalar types, including conversions. */
4622 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4623 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4625 /* Note that convert_for_assignment calls default_conversion
4626 for arrays and functions. We must not call it in the
4627 case where inside_init is a null pointer constant. */
4629 = convert_for_assignment (type, init, "initialization",
4630 NULL_TREE, NULL_TREE, 0);
4632 if (require_constant && ! TREE_CONSTANT (inside_init))
4634 error_init ("initializer element%s is not constant",
4636 inside_init = error_mark_node;
4638 else if (require_constant
4639 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4641 error_init ("initializer element%s is not computable at load time",
4643 inside_init = error_mark_node;
4649 /* Come here only for records and arrays. */
4651 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4653 error_init ("variable-sized object%s may not be initialized",
4655 return error_mark_node;
4658 /* Traditionally, you can write struct foo x = 0;
4659 and it initializes the first element of x to 0. */
4660 if (flag_traditional)
4662 tree top = 0, prev = 0;
4663 while (TREE_CODE (type) == RECORD_TYPE
4664 || TREE_CODE (type) == ARRAY_TYPE
4665 || TREE_CODE (type) == QUAL_UNION_TYPE
4666 || TREE_CODE (type) == UNION_TYPE)
4668 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4672 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4674 if (TREE_CODE (type) == ARRAY_TYPE)
4675 type = TREE_TYPE (type);
4676 else if (TYPE_FIELDS (type))
4677 type = TREE_TYPE (TYPE_FIELDS (type));
4680 error_init ("invalid initializer%s", " for `%s'", NULL);
4681 return error_mark_node;
4684 TREE_OPERAND (prev, 1)
4685 = build_tree_list (NULL_TREE,
4686 digest_init (type, init, require_constant,
4687 constructor_constant));
4690 error_init ("invalid initializer%s", " for `%s'", NULL);
4691 return error_mark_node;
4694 /* Handle initializers that use braces. */
4696 static void output_init_element ();
4697 static void output_pending_init_elements ();
4698 static void check_init_type_bitfields ();
4700 /* Type of object we are accumulating a constructor for.
4701 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4702 static tree constructor_type;
4704 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4706 static tree constructor_fields;
4708 /* For an ARRAY_TYPE, this is the specified index
4709 at which to store the next element we get.
4710 This is a special INTEGER_CST node that we modify in place. */
4711 static tree constructor_index;
4713 /* For an ARRAY_TYPE, this is the end index of the range
4714 to intitialize with the next element, or NULL in the ordinary case
4715 where the element is used just once. */
4716 static tree constructor_range_end;
4718 /* For an ARRAY_TYPE, this is the maximum index. */
4719 static tree constructor_max_index;
4721 /* For a RECORD_TYPE, this is the first field not yet written out. */
4722 static tree constructor_unfilled_fields;
4724 /* For an ARRAY_TYPE, this is the index of the first element
4725 not yet written out.
4726 This is a special INTEGER_CST node that we modify in place. */
4727 static tree constructor_unfilled_index;
4729 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4730 This is so we can generate gaps between fields, when appropriate.
4731 This is a special INTEGER_CST node that we modify in place. */
4732 static tree constructor_bit_index;
4734 /* If we are saving up the elements rather than allocating them,
4735 this is the list of elements so far (in reverse order,
4736 most recent first). */
4737 static tree constructor_elements;
4739 /* 1 if so far this constructor's elements are all compile-time constants. */
4740 static int constructor_constant;
4742 /* 1 if so far this constructor's elements are all valid address constants. */
4743 static int constructor_simple;
4745 /* 1 if this constructor is erroneous so far. */
4746 static int constructor_erroneous;
4748 /* 1 if have called defer_addressed_constants. */
4749 static int constructor_subconstants_deferred;
4751 /* List of pending elements at this constructor level.
4752 These are elements encountered out of order
4753 which belong at places we haven't reached yet in actually
4754 writing the output. */
4755 static tree constructor_pending_elts;
4757 /* The SPELLING_DEPTH of this constructor. */
4758 static int constructor_depth;
4760 /* 0 if implicitly pushing constructor levels is allowed. */
4761 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4763 /* 1 if this constructor level was entered implicitly. */
4764 static int constructor_implicit;
4766 static int require_constant_value;
4767 static int require_constant_elements;
4769 /* 1 if it is ok to output this constructor as we read it.
4770 0 means must accumulate a CONSTRUCTOR expression. */
4771 static int constructor_incremental;
4773 /* DECL node for which an initializer is being read.
4774 0 means we are reading a constructor expression
4775 such as (struct foo) {...}. */
4776 static tree constructor_decl;
4778 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4779 static char *constructor_asmspec;
4781 /* Nonzero if this is an initializer for a top-level decl. */
4782 static int constructor_top_level;
4784 /* When we finish reading a constructor expression
4785 (constructor_decl is 0), the CONSTRUCTOR goes here. */
4786 static tree constructor_result;
4788 /* This stack has a level for each implicit or explicit level of
4789 structuring in the initializer, including the outermost one. It
4790 saves the values of most of the variables above. */
4792 struct constructor_stack
4794 struct constructor_stack *next;
4800 tree unfilled_index;
4801 tree unfilled_fields;
4807 /* If nonzero, this value should replace the entire
4808 constructor at this level. */
4809 tree replacement_value;
4818 struct constructor_stack *constructor_stack;
4820 /* This stack records separate initializers that are nested.
4821 Nested initializers can't happen in ANSI C, but GNU C allows them
4822 in cases like { ... (struct foo) { ... } ... }. */
4824 struct initializer_stack
4826 struct initializer_stack *next;
4829 struct constructor_stack *constructor_stack;
4831 struct spelling *spelling;
4832 struct spelling *spelling_base;
4836 char require_constant_value;
4837 char require_constant_elements;
4841 struct initializer_stack *initializer_stack;
4843 /* Prepare to parse and output the initializer for variable DECL. */
4846 start_init (decl, asmspec_tree, top_level)
4852 struct initializer_stack *p
4853 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
4857 asmspec = TREE_STRING_POINTER (asmspec_tree);
4859 p->decl = constructor_decl;
4860 p->asmspec = constructor_asmspec;
4861 p->incremental = constructor_incremental;
4862 p->require_constant_value = require_constant_value;
4863 p->require_constant_elements = require_constant_elements;
4864 p->constructor_stack = constructor_stack;
4865 p->elements = constructor_elements;
4866 p->spelling = spelling;
4867 p->spelling_base = spelling_base;
4868 p->spelling_size = spelling_size;
4869 p->deferred = constructor_subconstants_deferred;
4870 p->top_level = constructor_top_level;
4871 p->next = initializer_stack;
4872 initializer_stack = p;
4874 constructor_decl = decl;
4875 constructor_incremental = top_level;
4876 constructor_asmspec = asmspec;
4877 constructor_subconstants_deferred = 0;
4878 constructor_top_level = top_level;
4882 require_constant_value = TREE_STATIC (decl);
4883 require_constant_elements
4884 = ((TREE_STATIC (decl) || pedantic)
4885 /* For a scalar, you can always use any value to initialize,
4886 even within braces. */
4887 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4888 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4889 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4890 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4891 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4892 constructor_incremental |= TREE_STATIC (decl);
4896 require_constant_value = 0;
4897 require_constant_elements = 0;
4898 locus = "(anonymous)";
4901 constructor_stack = 0;
4903 missing_braces_mentioned = 0;
4907 RESTORE_SPELLING_DEPTH (0);
4910 push_string (locus);
4916 struct initializer_stack *p = initializer_stack;
4918 /* Output subconstants (string constants, usually)
4919 that were referenced within this initializer and saved up.
4920 Must do this if and only if we called defer_addressed_constants. */
4921 if (constructor_subconstants_deferred)
4922 output_deferred_addressed_constants ();
4924 /* Free the whole constructor stack of this initializer. */
4925 while (constructor_stack)
4927 struct constructor_stack *q = constructor_stack;
4928 constructor_stack = q->next;
4932 /* Pop back to the data of the outer initializer (if any). */
4933 constructor_decl = p->decl;
4934 constructor_asmspec = p->asmspec;
4935 constructor_incremental = p->incremental;
4936 require_constant_value = p->require_constant_value;
4937 require_constant_elements = p->require_constant_elements;
4938 constructor_stack = p->constructor_stack;
4939 constructor_elements = p->elements;
4940 spelling = p->spelling;
4941 spelling_base = p->spelling_base;
4942 spelling_size = p->spelling_size;
4943 constructor_subconstants_deferred = p->deferred;
4944 constructor_top_level = p->top_level;
4945 initializer_stack = p->next;
4949 /* Call here when we see the initializer is surrounded by braces.
4950 This is instead of a call to push_init_level;
4951 it is matched by a call to pop_init_level.
4953 TYPE is the type to initialize, for a constructor expression.
4954 For an initializer for a decl, TYPE is zero. */
4957 really_start_incremental_init (type)
4960 struct constructor_stack *p
4961 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
4964 type = TREE_TYPE (constructor_decl);
4966 /* Turn off constructor_incremental if type is a struct with bitfields.
4967 Do this before the first push, so that the corrected value
4968 is available in finish_init. */
4969 check_init_type_bitfields (type);
4971 p->type = constructor_type;
4972 p->fields = constructor_fields;
4973 p->index = constructor_index;
4974 p->range_end = constructor_range_end;
4975 p->max_index = constructor_max_index;
4976 p->unfilled_index = constructor_unfilled_index;
4977 p->unfilled_fields = constructor_unfilled_fields;
4978 p->bit_index = constructor_bit_index;
4979 p->elements = constructor_elements;
4980 p->constant = constructor_constant;
4981 p->simple = constructor_simple;
4982 p->erroneous = constructor_erroneous;
4983 p->pending_elts = constructor_pending_elts;
4984 p->depth = constructor_depth;
4985 p->replacement_value = 0;
4987 p->incremental = constructor_incremental;
4990 constructor_stack = p;
4992 constructor_constant = 1;
4993 constructor_simple = 1;
4994 constructor_depth = SPELLING_DEPTH ();
4995 constructor_elements = 0;
4996 constructor_pending_elts = 0;
4997 constructor_type = type;
4999 if (TREE_CODE (constructor_type) == RECORD_TYPE
5000 || TREE_CODE (constructor_type) == UNION_TYPE)
5002 constructor_fields = TYPE_FIELDS (constructor_type);
5003 /* Skip any nameless bit fields atthe beginning. */
5004 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5005 && DECL_NAME (constructor_fields) == 0)
5006 constructor_fields = TREE_CHAIN (constructor_fields);
5007 constructor_unfilled_fields = constructor_fields;
5008 constructor_bit_index = copy_node (integer_zero_node);
5010 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5012 constructor_range_end = 0;
5013 if (TYPE_DOMAIN (constructor_type))
5015 constructor_max_index
5016 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5018 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5021 constructor_index = copy_node (integer_zero_node);
5022 constructor_unfilled_index = copy_node (constructor_index);
5026 /* Handle the case of int x = {5}; */
5027 constructor_fields = constructor_type;
5028 constructor_unfilled_fields = constructor_type;
5031 if (constructor_incremental)
5033 int momentary = suspend_momentary ();
5034 push_obstacks_nochange ();
5035 if (TREE_PERMANENT (constructor_decl))
5036 end_temporary_allocation ();
5037 make_decl_rtl (constructor_decl, constructor_asmspec,
5038 constructor_top_level);
5039 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5041 resume_momentary (momentary);
5044 if (constructor_incremental)
5046 defer_addressed_constants ();
5047 constructor_subconstants_deferred = 1;
5051 /* Push down into a subobject, for initialization.
5052 If this is for an explicit set of braces, IMPLICIT is 0.
5053 If it is because the next element belongs at a lower level,
5057 push_init_level (implicit)
5060 struct constructor_stack *p;
5062 /* If we've exhausted any levels that didn't have braces,
5064 while (constructor_stack->implicit)
5066 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5067 || TREE_CODE (constructor_type) == UNION_TYPE)
5068 && constructor_fields == 0)
5069 process_init_element (pop_init_level (1));
5070 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5071 && tree_int_cst_lt (constructor_max_index, constructor_index))
5072 process_init_element (pop_init_level (1));
5077 /* Structure elements may require alignment. Do this now
5078 if necessary for the subaggregate. */
5079 if (constructor_incremental && TREE_CODE (constructor_type) == RECORD_TYPE
5080 && constructor_fields)
5082 /* Advance to offset of this element. */
5083 if (! tree_int_cst_equal (constructor_bit_index,
5084 DECL_FIELD_BITPOS (constructor_fields)))
5086 int next = (TREE_INT_CST_LOW
5087 (DECL_FIELD_BITPOS (constructor_fields))
5089 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5092 assemble_zeros (next - here);
5096 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5097 p->type = constructor_type;
5098 p->fields = constructor_fields;
5099 p->index = constructor_index;
5100 p->range_end = constructor_range_end;
5101 p->max_index = constructor_max_index;
5102 p->unfilled_index = constructor_unfilled_index;
5103 p->unfilled_fields = constructor_unfilled_fields;
5104 p->bit_index = constructor_bit_index;
5105 p->elements = constructor_elements;
5106 p->constant = constructor_constant;
5107 p->simple = constructor_simple;
5108 p->erroneous = constructor_erroneous;
5109 p->pending_elts = constructor_pending_elts;
5110 p->depth = constructor_depth;
5111 p->replacement_value = 0;
5112 p->implicit = implicit;
5113 p->incremental = constructor_incremental;
5115 p->next = constructor_stack;
5116 constructor_stack = p;
5118 constructor_constant = 1;
5119 constructor_simple = 1;
5120 constructor_depth = SPELLING_DEPTH ();
5121 constructor_elements = 0;
5122 constructor_pending_elts = 0;
5124 /* Don't die if an entire brace-pair level is superfluous
5125 in the containing level. */
5126 if (constructor_type == 0)
5128 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5129 || TREE_CODE (constructor_type) == UNION_TYPE)
5131 /* Don't die if there are extra init elts at the end. */
5132 if (constructor_fields == 0)
5133 constructor_type = 0;
5136 constructor_type = TREE_TYPE (constructor_fields);
5137 push_member_name (constructor_fields);
5140 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5142 constructor_type = TREE_TYPE (constructor_type);
5143 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5146 if (constructor_type == 0)
5148 error_init ("extra brace group at end of initializer%s",
5150 constructor_fields = 0;
5151 constructor_unfilled_fields = 0;
5155 /* Turn off constructor_incremental if type is a struct with bitfields. */
5156 check_init_type_bitfields (constructor_type);
5158 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5160 missing_braces_mentioned = 1;
5161 warning_init ("missing braces around initializer%s", " for `%s'", NULL);
5164 if (TREE_CODE (constructor_type) == RECORD_TYPE
5165 || TREE_CODE (constructor_type) == UNION_TYPE)
5167 constructor_fields = TYPE_FIELDS (constructor_type);
5168 /* Skip any nameless bit fields atthe beginning. */
5169 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5170 && DECL_NAME (constructor_fields) == 0)
5171 constructor_fields = TREE_CHAIN (constructor_fields);
5172 constructor_unfilled_fields = constructor_fields;
5173 constructor_bit_index = copy_node (integer_zero_node);
5175 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5177 constructor_range_end = 0;
5178 if (TYPE_DOMAIN (constructor_type))
5180 constructor_max_index
5181 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5183 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5186 constructor_index = copy_node (integer_zero_node);
5187 constructor_unfilled_index = copy_node (constructor_index);
5191 warning_init ("braces around scalar initializer%s", " for `%s'", NULL);
5192 constructor_fields = constructor_type;
5193 constructor_unfilled_fields = constructor_type;
5197 /* Don't read a struct incrementally if it has any bitfields,
5198 because the incremental reading code doesn't know how to
5199 handle bitfields yet. */
5202 check_init_type_bitfields (type)
5205 if (TREE_CODE (type) == RECORD_TYPE)
5208 for (tail = TYPE_FIELDS (type); tail;
5209 tail = TREE_CHAIN (tail))
5211 if (DECL_BIT_FIELD (tail)
5212 /* This catches cases like `int foo : 8;'. */
5213 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail)))
5215 constructor_incremental = 0;
5219 check_init_type_bitfields (TREE_TYPE (tail));
5223 else if (TREE_CODE (type) == ARRAY_TYPE)
5224 check_init_type_bitfields (TREE_TYPE (type));
5227 /* At the end of an implicit or explicit brace level,
5228 finish up that level of constructor.
5229 If we were outputting the elements as they are read, return 0
5230 from inner levels (process_init_element ignores that),
5231 but return error_mark_node from the outermost level
5232 (that's what we want to put in DECL_INITIAL).
5233 Otherwise, return a CONSTRUCTOR expression. */
5236 pop_init_level (implicit)
5239 struct constructor_stack *p;
5241 tree constructor = 0;
5245 /* When we come to an explicit close brace,
5246 pop any inner levels that didn't have explicit braces. */
5247 while (constructor_stack->implicit)
5248 process_init_element (pop_init_level (1));
5251 p = constructor_stack;
5253 if (constructor_type != 0)
5254 size = int_size_in_bytes (constructor_type);
5256 /* Now output all pending elements. */
5257 output_pending_init_elements (1);
5259 #if 0 /* c-parse.in warns about {}. */
5260 /* In ANSI, each brace level must have at least one element. */
5261 if (! implicit && pedantic
5262 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5263 ? integer_zerop (constructor_unfilled_index)
5264 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5265 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL);
5268 /* Pad out the end of the structure. */
5270 if (p->replacement_value)
5272 /* If this closes a superfluous brace pair,
5273 just pass out the element between them. */
5274 constructor = p->replacement_value;
5275 /* If this is the top level thing within the initializer,
5276 and it's for a variable, then since we already called
5277 assemble_variable, we must output the value now. */
5278 if (p->next == 0 && constructor_decl != 0
5279 && constructor_incremental)
5281 constructor = digest_init (constructor_type, constructor,
5284 /* If initializing an array of unknown size,
5285 determine the size now. */
5286 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5287 && TYPE_DOMAIN (constructor_type) == 0)
5292 push_obstacks_nochange ();
5293 if (TREE_PERMANENT (constructor_type))
5294 end_temporary_allocation ();
5296 momentary_p = suspend_momentary ();
5298 /* We shouldn't have an incomplete array type within
5300 if (constructor_stack->next)
5304 = complete_array_type (constructor_type,
5309 size = int_size_in_bytes (constructor_type);
5310 resume_momentary (momentary_p);
5314 output_constant (constructor, size);
5317 else if (constructor_type == 0)
5319 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5320 && TREE_CODE (constructor_type) != UNION_TYPE
5321 && TREE_CODE (constructor_type) != ARRAY_TYPE
5322 && ! constructor_incremental)
5324 /* A nonincremental scalar initializer--just return
5325 the element, after verifying there is just one. */
5326 if (constructor_elements == 0)
5328 error_init ("empty scalar initializer%s",
5330 constructor = error_mark_node;
5332 else if (TREE_CHAIN (constructor_elements) != 0)
5334 error_init ("extra elements in scalar initializer%s",
5336 constructor = TREE_VALUE (constructor_elements);
5339 constructor = TREE_VALUE (constructor_elements);
5341 else if (! constructor_incremental)
5343 if (constructor_erroneous)
5344 constructor = error_mark_node;
5347 int momentary = suspend_momentary ();
5349 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5350 nreverse (constructor_elements));
5351 if (constructor_constant)
5352 TREE_CONSTANT (constructor) = 1;
5353 if (constructor_constant && constructor_simple)
5354 TREE_STATIC (constructor) = 1;
5356 resume_momentary (momentary);
5362 int momentary = suspend_momentary ();
5364 if (TREE_CODE (constructor_type) == RECORD_TYPE
5365 || TREE_CODE (constructor_type) == UNION_TYPE)
5367 /* Find the offset of the end of that field. */
5368 filled = size_binop (CEIL_DIV_EXPR,
5369 constructor_bit_index,
5370 size_int (BITS_PER_UNIT));
5372 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5374 /* If initializing an array of unknown size,
5375 determine the size now. */
5376 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5377 && TYPE_DOMAIN (constructor_type) == 0)
5380 = size_binop (MINUS_EXPR,
5381 constructor_unfilled_index,
5384 push_obstacks_nochange ();
5385 if (TREE_PERMANENT (constructor_type))
5386 end_temporary_allocation ();
5387 maxindex = copy_node (maxindex);
5388 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5389 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5392 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)),
5394 error_with_decl (constructor_decl, "zero-size array `%s'");
5395 layout_type (constructor_type);
5396 size = int_size_in_bytes (constructor_type);
5400 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5401 size_in_bytes (TREE_TYPE (constructor_type)));
5407 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5409 resume_momentary (momentary);
5413 constructor_type = p->type;
5414 constructor_fields = p->fields;
5415 constructor_index = p->index;
5416 constructor_range_end = p->range_end;
5417 constructor_max_index = p->max_index;
5418 constructor_unfilled_index = p->unfilled_index;
5419 constructor_unfilled_fields = p->unfilled_fields;
5420 constructor_bit_index = p->bit_index;
5421 constructor_elements = p->elements;
5422 constructor_constant = p->constant;
5423 constructor_simple = p->simple;
5424 constructor_erroneous = p->erroneous;
5425 constructor_pending_elts = p->pending_elts;
5426 constructor_depth = p->depth;
5427 constructor_incremental = p->incremental;
5428 RESTORE_SPELLING_DEPTH (constructor_depth);
5430 constructor_stack = p->next;
5433 if (constructor == 0)
5435 if (constructor_stack == 0)
5436 return error_mark_node;
5442 /* Within an array initializer, specify the next index to be initialized.
5443 FIRST is that index. If LAST is nonzero, then initialize a range
5444 of indices, running from FIRST through LAST. */
5447 set_init_index (first, last)
5450 while ((TREE_CODE (first) == NOP_EXPR
5451 || TREE_CODE (first) == CONVERT_EXPR
5452 || TREE_CODE (first) == NON_LVALUE_EXPR)
5453 && (TYPE_MODE (TREE_TYPE (first))
5454 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5455 (first) = TREE_OPERAND (first, 0);
5457 while ((TREE_CODE (last) == NOP_EXPR
5458 || TREE_CODE (last) == CONVERT_EXPR
5459 || TREE_CODE (last) == NON_LVALUE_EXPR)
5460 && (TYPE_MODE (TREE_TYPE (last))
5461 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5462 (last) = TREE_OPERAND (last, 0);
5464 if (TREE_CODE (first) != INTEGER_CST)
5465 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5466 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5467 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5468 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5469 error_init ("duplicate array index in initializer%s", " for `%s'", NULL);
5472 TREE_INT_CST_LOW (constructor_index)
5473 = TREE_INT_CST_LOW (first);
5474 TREE_INT_CST_HIGH (constructor_index)
5475 = TREE_INT_CST_HIGH (first);
5477 if (last != 0 && tree_int_cst_lt (last, first))
5478 error_init ("empty index range in initializer%s", " for `%s'", NULL);
5482 pedwarn ("ANSI C forbids specifying element to initialize");
5483 constructor_range_end = last;
5488 /* Within a struct initializer, specify the next field to be initialized. */
5491 set_init_label (fieldname)
5497 for (tail = TYPE_FIELDS (constructor_type); tail;
5498 tail = TREE_CHAIN (tail))
5500 if (tail == constructor_unfilled_fields)
5502 if (DECL_NAME (tail) == fieldname)
5507 error ("unknown field `%s' specified in initializer",
5508 IDENTIFIER_POINTER (fieldname));
5510 error ("field `%s' already initialized",
5511 IDENTIFIER_POINTER (fieldname));
5514 constructor_fields = tail;
5516 pedwarn ("ANSI C forbids specifying structure member to initialize");
5520 /* "Output" the next constructor element.
5521 At top level, really output it to assembler code now.
5522 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5523 TYPE is the data type that the containing data type wants here.
5524 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5526 PENDING if non-nil means output pending elements that belong
5527 right after this element. (PENDING is normally 1;
5528 it is 0 while outputting pending elements, to avoid recursion.) */
5531 output_init_element (value, type, field, pending)
5532 tree value, type, field;
5537 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5538 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5539 && !(TREE_CODE (value) == STRING_CST
5540 && TREE_CODE (type) == ARRAY_TYPE
5541 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5542 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5543 TYPE_MAIN_VARIANT (type))))
5544 value = default_conversion (value);
5546 if (value == error_mark_node)
5547 constructor_erroneous = 1;
5548 else if (!TREE_CONSTANT (value))
5549 constructor_constant = 0;
5550 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5551 constructor_simple = 0;
5553 if (require_constant_value && ! TREE_CONSTANT (value))
5555 error_init ("initializer element%s is not constant",
5557 value = error_mark_node;
5559 else if (require_constant_elements
5560 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5562 error_init ("initializer element%s is not computable at load time",
5564 value = error_mark_node;
5567 /* If this element duplicates one on constructor_pending_elts,
5568 print a message and ignore it. Don't do this when we're
5569 processing elements taken off constructor_pending_elts,
5570 because we'd always get spurious errors. */
5573 if (TREE_CODE (constructor_type) == RECORD_TYPE
5574 || TREE_CODE (constructor_type) == UNION_TYPE)
5576 if (purpose_member (field, constructor_pending_elts))
5578 error_init ("duplicate initializer%s", " for `%s'", NULL);
5582 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5585 for (tail = constructor_pending_elts; tail;
5586 tail = TREE_CHAIN (tail))
5587 if (TREE_PURPOSE (tail) != 0
5588 && TREE_CODE (TREE_PURPOSE (tail)) == INTEGER_CST
5589 && tree_int_cst_equal (TREE_PURPOSE (tail), constructor_index))
5594 error_init ("duplicate initializer%s", " for `%s'", NULL);
5600 /* If this element doesn't come next in sequence,
5601 put it on constructor_pending_elts. */
5602 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5603 && !tree_int_cst_equal (field, constructor_unfilled_index))
5606 /* The copy_node is needed in case field is actually
5607 constructor_index, which is modified in place. */
5608 constructor_pending_elts
5609 = tree_cons (copy_node (field),
5610 digest_init (type, value, 0, 0),
5611 constructor_pending_elts);
5613 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5614 && field != constructor_unfilled_fields)
5616 /* We do this for records but not for unions. In a union,
5617 no matter which field is specified, it can be initialized
5618 right away since it starts at the beginning of the union. */
5620 constructor_pending_elts
5622 digest_init (type, value, 0, 0),
5623 constructor_pending_elts);
5627 /* Otherwise, output this element either to
5628 constructor_elements or to the assembler file. */
5632 if (! constructor_incremental)
5634 if (field && TREE_CODE (field) == INTEGER_CST)
5635 field = copy_node (field);
5636 constructor_elements
5637 = tree_cons (field, digest_init (type, value, 0, 0),
5638 constructor_elements);
5642 /* Structure elements may require alignment.
5643 Do this, if necessary. */
5644 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5646 /* Advance to offset of this element. */
5647 if (! tree_int_cst_equal (constructor_bit_index,
5648 DECL_FIELD_BITPOS (field)))
5650 int next = (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
5652 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5655 assemble_zeros (next - here);
5658 output_constant (digest_init (type, value, 0, 0),
5659 int_size_in_bytes (type));
5661 /* For a record or union,
5662 keep track of end position of last field. */
5663 if (TREE_CODE (constructor_type) == RECORD_TYPE
5664 || TREE_CODE (constructor_type) == UNION_TYPE)
5666 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
5668 TREE_INT_CST_LOW (constructor_bit_index)
5669 = TREE_INT_CST_LOW (temp);
5670 TREE_INT_CST_HIGH (constructor_bit_index)
5671 = TREE_INT_CST_HIGH (temp);
5676 /* Advance the variable that indicates sequential elements output. */
5677 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5679 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
5681 TREE_INT_CST_LOW (constructor_unfilled_index)
5682 = TREE_INT_CST_LOW (tem);
5683 TREE_INT_CST_HIGH (constructor_unfilled_index)
5684 = TREE_INT_CST_HIGH (tem);
5686 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5687 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5688 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5689 constructor_unfilled_fields = 0;
5691 /* Now output any pending elements which have become next. */
5693 output_pending_init_elements (0);
5697 /* Output any pending elements which have become next.
5698 As we output elements, constructor_unfilled_{fields,index}
5699 advances, which may cause other elements to become next;
5700 if so, they too are output.
5702 If ALL is 0, we return when there are
5703 no more pending elements to output now.
5705 If ALL is 1, we output space as necessary so that
5706 we can output all the pending elements. */
5709 output_pending_init_elements (all)
5717 /* Look thru the whole pending list.
5718 If we find an element that should be output now,
5719 output it. Otherwise, set NEXT to the element
5720 that comes first among those still pending. */
5723 for (tail = constructor_pending_elts; tail;
5724 tail = TREE_CHAIN (tail))
5726 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5728 if (tree_int_cst_equal (TREE_PURPOSE (tail),
5729 constructor_unfilled_index))
5731 output_init_element (TREE_VALUE (tail), TREE_TYPE (constructor_type),
5732 constructor_unfilled_index, 0);
5735 else if (tree_int_cst_lt (TREE_PURPOSE (tail),
5736 constructor_unfilled_index))
5739 || tree_int_cst_lt (TREE_PURPOSE (tail),
5741 next = TREE_PURPOSE (tail);
5743 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5744 || TREE_CODE (constructor_type) == UNION_TYPE)
5746 if (TREE_PURPOSE (tail) == constructor_unfilled_fields)
5748 output_init_element (TREE_VALUE (tail),
5749 TREE_TYPE (constructor_unfilled_fields),
5750 constructor_unfilled_fields,
5754 else if (constructor_unfilled_fields == 0
5755 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
5756 DECL_FIELD_BITPOS (constructor_unfilled_fields)))
5759 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
5760 DECL_FIELD_BITPOS (next)))
5761 next = TREE_PURPOSE (tail);
5765 /* Ordinarily return, but not if we want to output all
5766 and there are elements left. */
5767 if (! (all && next != 0))
5770 /* Generate space up to the position of NEXT. */
5771 if (constructor_incremental)
5776 if (TREE_CODE (constructor_type) == RECORD_TYPE
5777 || TREE_CODE (constructor_type) == UNION_TYPE)
5779 /* Find the last field written out, if any. */
5780 for (tail = TYPE_FIELDS (constructor_type); tail;
5781 tail = TREE_CHAIN (tail))
5782 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
5786 /* Find the offset of the end of that field. */
5787 filled = size_binop (CEIL_DIV_EXPR,
5788 size_binop (PLUS_EXPR,
5789 DECL_FIELD_BITPOS (tail),
5791 size_int (BITS_PER_UNIT));
5793 filled = size_int (0);
5795 nextpos_tree = size_binop (CEIL_DIV_EXPR,
5796 DECL_FIELD_BITPOS (next),
5797 size_int (BITS_PER_UNIT));
5799 TREE_INT_CST_HIGH (constructor_bit_index)
5800 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
5801 TREE_INT_CST_LOW (constructor_bit_index)
5802 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
5803 constructor_unfilled_fields = next;
5805 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5807 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5808 size_in_bytes (TREE_TYPE (constructor_type)));
5810 = size_binop (MULT_EXPR, next,
5811 size_in_bytes (TREE_TYPE (constructor_type)));
5812 TREE_INT_CST_LOW (constructor_unfilled_index)
5813 = TREE_INT_CST_LOW (next);
5814 TREE_INT_CST_HIGH (constructor_unfilled_index)
5815 = TREE_INT_CST_HIGH (next);
5822 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
5824 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
5829 /* If it's not incremental, just skip over the gap,
5830 so that after jumping to retry we will output the next
5831 successive element. */
5832 if (TREE_CODE (constructor_type) == RECORD_TYPE
5833 || TREE_CODE (constructor_type) == UNION_TYPE)
5834 constructor_unfilled_fields = next;
5835 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5837 TREE_INT_CST_LOW (constructor_unfilled_index)
5838 = TREE_INT_CST_LOW (next);
5839 TREE_INT_CST_HIGH (constructor_unfilled_index)
5840 = TREE_INT_CST_HIGH (next);
5847 /* Add one non-braced element to the current constructor level.
5848 This adjusts the current position within the constructor's type.
5849 This may also start or terminate implicit levels
5850 to handle a partly-braced initializer.
5852 Once this has found the correct level for the new element,
5853 it calls output_init_element.
5855 Note: if we are incrementally outputting this constructor,
5856 this function may be called with a null argument
5857 representing a sub-constructor that was already incrementally output.
5858 When that happens, we output nothing, but we do the bookkeeping
5859 to skip past that element of the current constructor. */
5862 process_init_element (value)
5865 tree orig_value = value;
5866 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5868 /* Handle superfluous braces around string cst as in
5869 char x[] = {"foo"}; */
5871 && TREE_CODE (constructor_type) == ARRAY_TYPE
5872 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5873 && integer_zerop (constructor_unfilled_index))
5875 constructor_stack->replacement_value = value;
5879 if (constructor_stack->replacement_value != 0)
5881 error_init ("excess elements in struct initializer%s",
5882 " after `%s'", NULL_PTR);
5886 /* Ignore elements of a brace group if it is entirely superfluous
5887 and has already been diagnosed. */
5888 if (constructor_type == 0)
5891 /* If we've exhausted any levels that didn't have braces,
5893 while (constructor_stack->implicit)
5895 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5896 || TREE_CODE (constructor_type) == UNION_TYPE)
5897 && constructor_fields == 0)
5898 process_init_element (pop_init_level (1));
5899 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5900 && tree_int_cst_lt (constructor_max_index, constructor_index))
5901 process_init_element (pop_init_level (1));
5908 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5911 enum tree_code fieldcode;
5913 if (constructor_fields == 0)
5915 pedwarn_init ("excess elements in struct initializer%s",
5916 " after `%s'", NULL_PTR);
5920 fieldtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_fields));
5921 fieldcode = TREE_CODE (fieldtype);
5923 /* Accept a string constant to initialize a subarray. */
5925 && fieldcode == ARRAY_TYPE
5926 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5929 /* Otherwise, if we have come to a subaggregate,
5930 and we don't have an element of its type, push into it. */
5931 else if (value != 0 && !constructor_no_implicit
5932 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5933 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5934 || fieldcode == UNION_TYPE))
5936 push_init_level (1);
5942 push_member_name (constructor_fields);
5943 output_init_element (value, fieldtype, constructor_fields, 1);
5944 RESTORE_SPELLING_DEPTH (constructor_depth);
5947 /* Do the bookkeeping for an element that was
5948 directly output as a constructor. */
5950 /* For a record, keep track of end position of last field. */
5951 tree temp = size_binop (PLUS_EXPR,
5952 DECL_FIELD_BITPOS (constructor_fields),
5953 DECL_SIZE (constructor_fields));
5954 TREE_INT_CST_LOW (constructor_bit_index)
5955 = TREE_INT_CST_LOW (temp);
5956 TREE_INT_CST_HIGH (constructor_bit_index)
5957 = TREE_INT_CST_HIGH (temp);
5959 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5962 constructor_fields = TREE_CHAIN (constructor_fields);
5963 /* Skip any nameless bit fields atthe beginning. */
5964 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5965 && DECL_NAME (constructor_fields) == 0)
5966 constructor_fields = TREE_CHAIN (constructor_fields);
5969 if (TREE_CODE (constructor_type) == UNION_TYPE)
5972 enum tree_code fieldcode;
5974 if (constructor_fields == 0)
5976 pedwarn_init ("excess elements in union initializer%s",
5977 " after `%s'", NULL_PTR);
5981 fieldtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_fields));
5982 fieldcode = TREE_CODE (fieldtype);
5984 /* Accept a string constant to initialize a subarray. */
5986 && fieldcode == ARRAY_TYPE
5987 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5990 /* Otherwise, if we have come to a subaggregate,
5991 and we don't have an element of its type, push into it. */
5992 else if (value != 0 && !constructor_no_implicit
5993 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5994 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5995 || fieldcode == UNION_TYPE))
5997 push_init_level (1);
6003 push_member_name (constructor_fields);
6004 output_init_element (value, fieldtype, constructor_fields, 1);
6005 RESTORE_SPELLING_DEPTH (constructor_depth);
6008 /* Do the bookkeeping for an element that was
6009 directly output as a constructor. */
6011 TREE_INT_CST_LOW (constructor_bit_index)
6012 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6013 TREE_INT_CST_HIGH (constructor_bit_index)
6014 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6016 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6019 constructor_fields = 0;
6022 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6024 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6025 enum tree_code eltcode = TREE_CODE (elttype);
6027 /* Accept a string constant to initialize a subarray. */
6029 && eltcode == ARRAY_TYPE
6030 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6033 /* Otherwise, if we have come to a subaggregate,
6034 and we don't have an element of its type, push into it. */
6035 else if (value != 0 && !constructor_no_implicit
6036 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6037 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6038 || eltcode == UNION_TYPE))
6040 push_init_level (1);
6044 if (constructor_max_index != 0
6045 && tree_int_cst_lt (constructor_max_index, constructor_index))
6047 pedwarn_init ("excess elements in array initializer%s",
6048 " after `%s'", NULL_PTR);
6052 /* Now output the actual element.
6053 Ordinarily, output once.
6054 If there is a range, repeat it till we advance past the range. */
6061 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6062 output_init_element (value, elttype, constructor_index, 1);
6063 RESTORE_SPELLING_DEPTH (constructor_depth);
6066 tem = size_binop (PLUS_EXPR, constructor_index,
6068 TREE_INT_CST_LOW (constructor_index)
6069 = TREE_INT_CST_LOW (tem);
6070 TREE_INT_CST_HIGH (constructor_index)
6071 = TREE_INT_CST_HIGH (tem);
6074 /* If we are doing the bookkeeping for an element that was
6075 directly output as a constructor,
6076 we must update constructor_unfilled_index. */
6078 TREE_INT_CST_LOW (constructor_unfilled_index)
6079 = TREE_INT_CST_LOW (constructor_index);
6080 TREE_INT_CST_HIGH (constructor_unfilled_index)
6081 = TREE_INT_CST_HIGH (constructor_index);
6084 while (! (constructor_range_end == 0
6085 || tree_int_cst_lt (constructor_range_end,
6086 constructor_index)));
6091 /* Handle the sole element allowed in a braced initializer
6092 for a scalar variable. */
6093 if (constructor_fields == 0)
6095 pedwarn_init ("excess elements in scalar initializer%s",
6096 " after `%s'", NULL_PTR);
6101 output_init_element (value, constructor_type, NULL_TREE, 1);
6102 constructor_fields = 0;
6106 /* If the (lexically) previous elments are not now saved,
6107 we can discard the storage for them. */
6108 if (constructor_incremental && constructor_pending_elts == 0 && value != 0)
6112 /* Expand an ASM statement with operands, handling output operands
6113 that are not variables or INDIRECT_REFS by transforming such
6114 cases into cases that expand_asm_operands can handle.
6116 Arguments are same as for expand_asm_operands. */
6119 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6120 tree string, outputs, inputs, clobbers;
6125 int noutputs = list_length (outputs);
6127 /* o[I] is the place that output number I should be written. */
6128 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6131 if (TREE_CODE (string) == ADDR_EXPR)
6132 string = TREE_OPERAND (string, 0);
6133 if (TREE_CODE (string) != STRING_CST)
6135 error ("asm template is not a string constant");
6139 /* Record the contents of OUTPUTS before it is modified. */
6140 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6141 o[i] = TREE_VALUE (tail);
6143 /* Perform default conversions on array and function inputs. */
6144 /* Don't do this for other types--
6145 it would screw up operands expected to be in memory. */
6146 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6147 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6148 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6149 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6151 /* Generate the ASM_OPERANDS insn;
6152 store into the TREE_VALUEs of OUTPUTS some trees for
6153 where the values were actually stored. */
6154 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6156 /* Copy all the intermediate outputs into the specified outputs. */
6157 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6159 if (o[i] != TREE_VALUE (tail))
6161 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6165 /* Detect modification of read-only values.
6166 (Otherwise done by build_modify_expr.) */
6169 tree type = TREE_TYPE (o[i]);
6170 if (TYPE_READONLY (type)
6171 || ((TREE_CODE (type) == RECORD_TYPE
6172 || TREE_CODE (type) == UNION_TYPE)
6173 && C_TYPE_FIELDS_READONLY (type)))
6174 readonly_warning (o[i], "modification by `asm'");
6178 /* Those MODIFY_EXPRs could do autoincrements. */
6182 /* Expand a C `return' statement.
6183 RETVAL is the expression for what to return,
6184 or a null pointer for `return;' with no value. */
6187 c_expand_return (retval)
6190 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6192 if (TREE_THIS_VOLATILE (current_function_decl))
6193 warning ("function declared `noreturn' has a `return' statement");
6197 current_function_returns_null = 1;
6198 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6199 warning ("`return' with no value, in function returning non-void");
6200 expand_null_return ();
6202 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6204 current_function_returns_null = 1;
6205 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6206 pedwarn ("`return' with a value, in function returning void");
6207 expand_return (retval);
6211 tree t = convert_for_assignment (valtype, retval, "return",
6212 NULL_TREE, NULL_TREE, 0);
6213 tree res = DECL_RESULT (current_function_decl);
6216 if (t == error_mark_node)
6219 inner = t = convert (TREE_TYPE (res), t);
6221 /* Strip any conversions, additions, and subtractions, and see if
6222 we are returning the address of a local variable. Warn if so. */
6223 while (TREE_CODE (inner) == NOP_EXPR
6224 || TREE_CODE (inner) == NON_LVALUE_EXPR
6225 || TREE_CODE (inner) == CONVERT_EXPR
6226 || TREE_CODE (inner) == PLUS_EXPR
6227 || TREE_CODE (inner) == MINUS_EXPR)
6228 inner = TREE_OPERAND (inner, 0);
6230 if (TREE_CODE (inner) == ADDR_EXPR)
6232 inner = TREE_OPERAND (inner, 0);
6234 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6235 inner = TREE_OPERAND (inner, 0);
6237 if (TREE_CODE (inner) == VAR_DECL
6238 && ! TREE_STATIC (inner)
6239 && DECL_CONTEXT (inner) == current_function_decl)
6240 warning ("function returns address of local variable");
6243 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6244 TREE_SIDE_EFFECTS (t) = 1;
6246 current_function_returns_value = 1;
6250 /* Start a C switch statement, testing expression EXP.
6251 Return EXP if it is valid, an error node otherwise. */
6254 c_expand_start_case (exp)
6257 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6258 tree type = TREE_TYPE (exp);
6260 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6262 error ("switch quantity not an integer");
6263 exp = error_mark_node;
6268 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6270 if (warn_traditional
6271 && (type == long_integer_type_node
6272 || type == long_unsigned_type_node))
6273 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6275 exp = default_conversion (exp);
6276 type = TREE_TYPE (exp);
6277 index = get_unwidened (exp, NULL_TREE);
6278 /* We can't strip a conversion from a signed type to an unsigned,
6279 because if we did, int_fits_type_p would do the wrong thing
6280 when checking case values for being in range,
6281 and it's too hard to do the right thing. */
6282 if (TREE_UNSIGNED (TREE_TYPE (exp))
6283 == TREE_UNSIGNED (TREE_TYPE (index)))
6287 expand_start_case (1, exp, type, "switch statement");