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 static tree quality_type PROTO((tree, tree));
44 static int comp_target_types PROTO((tree, tree));
45 static int function_types_compatible_p PROTO((tree, tree));
46 static int type_lists_compatible_p PROTO((tree, tree));
47 static int self_promoting_type_p PROTO((tree));
48 static tree decl_constant_value PROTO((tree));
49 static tree lookup_field PROTO((tree, tree, tree *));
50 static tree convert_arguments PROTO((tree, tree, tree, tree));
51 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
52 static tree pointer_diff PROTO((tree, tree));
53 static tree unary_complex_lvalue PROTO((enum tree_code, tree));
54 static void pedantic_lvalue_warning PROTO((enum tree_code));
55 static tree internal_build_compound_expr PROTO((tree, int));
56 static tree convert_for_assignment PROTO((tree, tree, char *, tree,
58 static void warn_for_assignment PROTO((char *, char *, tree, int));
59 static tree valid_compound_expr_initializer PROTO((tree, tree));
60 static void push_string PROTO((char *));
61 static void push_member_name PROTO((tree));
62 static void push_array_bounds PROTO((int));
63 static int spelling_length PROTO((void));
64 static char *print_spelling PROTO((char *));
65 static char *get_spelling PROTO((char *));
66 static void warning_init PROTO((char *, char *,
68 static tree digest_init PROTO((tree, tree, int, int));
69 static void check_init_type_bitfields PROTO((tree));
70 static void output_init_element PROTO((tree, tree, tree, int));
71 static void output_pending_init_elements PROTO((int));
73 /* Do `exp = require_complete_type (exp);' to make sure exp
74 does not have an incomplete type. (That includes void types.) */
77 require_complete_type (value)
80 tree type = TREE_TYPE (value);
82 /* First, detect a valid value with a complete type. */
83 if (TYPE_SIZE (type) != 0
84 && type != void_type_node)
87 incomplete_type_error (value, type);
88 return error_mark_node;
91 /* Print an error message for invalid use of an incomplete type.
92 VALUE is the expression that was used (or 0 if that isn't known)
93 and TYPE is the type that was invalid. */
96 incomplete_type_error (value, type)
102 /* Avoid duplicate error message. */
103 if (TREE_CODE (type) == ERROR_MARK)
106 if (value != 0 && (TREE_CODE (value) == VAR_DECL
107 || TREE_CODE (value) == PARM_DECL))
108 error ("`%s' has an incomplete type",
109 IDENTIFIER_POINTER (DECL_NAME (value)));
113 /* We must print an error message. Be clever about what it says. */
115 switch (TREE_CODE (type))
118 errmsg = "invalid use of undefined type `struct %s'";
122 errmsg = "invalid use of undefined type `union %s'";
126 errmsg = "invalid use of undefined type `enum %s'";
130 error ("invalid use of void expression");
134 if (TYPE_DOMAIN (type))
136 type = TREE_TYPE (type);
139 error ("invalid use of array with unspecified bounds");
146 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
147 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
149 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
150 error ("invalid use of incomplete typedef `%s'",
151 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
155 /* Return a variant of TYPE which has all the type qualifiers of LIKE
156 as well as those of TYPE. */
159 qualify_type (type, like)
162 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
163 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
164 return c_build_type_variant (type, constflag, volflag);
167 /* Return the common type of two types.
168 We assume that comptypes has already been done and returned 1;
169 if that isn't so, this may crash. In particular, we assume that qualifiers
172 This is the type for the result of most arithmetic operations
173 if the operands have the given two types. */
179 register enum tree_code code1;
180 register enum tree_code code2;
182 /* Save time if the two types are the same. */
184 if (t1 == t2) return t1;
186 /* If one type is nonsense, use the other. */
187 if (t1 == error_mark_node)
189 if (t2 == error_mark_node)
192 /* Treat an enum type as the unsigned integer type of the same width. */
194 if (TREE_CODE (t1) == ENUMERAL_TYPE)
195 t1 = type_for_size (TYPE_PRECISION (t1), 1);
196 if (TREE_CODE (t2) == ENUMERAL_TYPE)
197 t2 = type_for_size (TYPE_PRECISION (t2), 1);
199 code1 = TREE_CODE (t1);
200 code2 = TREE_CODE (t2);
202 /* If one type is complex, form the common type of the non-complex
203 components, then make that complex. Use T1 or T2 if it is the
205 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
207 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
208 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
209 tree subtype = common_type (subtype1, subtype2);
211 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
213 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
216 return build_complex_type (subtype);
223 /* If only one is real, use it as the result. */
225 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
228 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
231 /* Both real or both integers; use the one with greater precision. */
233 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
235 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
238 /* Same precision. Prefer longs to ints even when same size. */
240 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
241 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
242 return long_unsigned_type_node;
244 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
245 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
247 /* But preserve unsignedness from the other type,
248 since long cannot hold all the values of an unsigned int. */
249 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
250 return long_unsigned_type_node;
251 return long_integer_type_node;
254 /* Otherwise prefer the unsigned one. */
256 if (TREE_UNSIGNED (t1))
261 /* For two pointers, do this recursively on the target type,
262 and combine the qualifiers of the two types' targets. */
263 /* This code was turned off; I don't know why.
264 But ANSI C specifies doing this with the qualifiers.
265 So I turned it on again. */
267 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
268 TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
270 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
272 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
273 return build_pointer_type (c_build_type_variant (target, constp, volatilep));
276 return build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
281 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
282 /* Save space: see if the result is identical to one of the args. */
283 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
285 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
287 /* Merge the element types, and have a size if either arg has one. */
288 return build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
292 /* Function types: prefer the one that specified arg types.
293 If both do, merge the arg types. Also merge the return types. */
295 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
296 tree p1 = TYPE_ARG_TYPES (t1);
297 tree p2 = TYPE_ARG_TYPES (t2);
302 /* Save space: see if the result is identical to one of the args. */
303 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
305 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
308 /* Simple way if one arg fails to specify argument types. */
309 if (TYPE_ARG_TYPES (t1) == 0)
310 return build_function_type (valtype, TYPE_ARG_TYPES (t2));
311 if (TYPE_ARG_TYPES (t2) == 0)
312 return build_function_type (valtype, TYPE_ARG_TYPES (t1));
314 /* If both args specify argument types, we must merge the two
315 lists, argument by argument. */
317 len = list_length (p1);
320 for (i = 0; i < len; i++)
321 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
326 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
328 /* A null type means arg type is not specified.
329 Take whatever the other function type has. */
330 if (TREE_VALUE (p1) == 0)
332 TREE_VALUE (n) = TREE_VALUE (p2);
335 if (TREE_VALUE (p2) == 0)
337 TREE_VALUE (n) = TREE_VALUE (p1);
341 /* Given wait (union {union wait *u; int *i} *)
342 and wait (union wait *),
343 prefer union wait * as type of parm. */
344 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
345 && TREE_VALUE (p1) != TREE_VALUE (p2))
348 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
349 memb; memb = TREE_CHAIN (memb))
350 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
352 TREE_VALUE (n) = TREE_VALUE (p2);
354 pedwarn ("function types not truly compatible in ANSI C");
358 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
359 && TREE_VALUE (p2) != TREE_VALUE (p1))
362 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
363 memb; memb = TREE_CHAIN (memb))
364 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
366 TREE_VALUE (n) = TREE_VALUE (p1);
368 pedwarn ("function types not truly compatible in ANSI C");
372 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
376 return build_function_type (valtype, newargs);
385 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
386 or various other operations. Return 2 if they are compatible
387 but a warning may be needed if you use them together. */
390 comptypes (type1, type2)
393 register tree t1 = type1;
394 register tree t2 = type2;
396 /* Suppress errors caused by previously reported errors. */
398 if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
401 /* Treat an enum type as the unsigned integer type of the same width. */
403 if (TREE_CODE (t1) == ENUMERAL_TYPE)
404 t1 = type_for_size (TYPE_PRECISION (t1), 1);
405 if (TREE_CODE (t2) == ENUMERAL_TYPE)
406 t2 = type_for_size (TYPE_PRECISION (t2), 1);
411 /* Different classes of types can't be compatible. */
413 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
415 /* Qualifiers must match. */
417 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
419 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
422 /* Allow for two different type nodes which have essentially the same
423 definition. Note that we already checked for equality of the type
424 type qualifiers (just above). */
426 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
429 switch (TREE_CODE (t1))
432 return (TREE_TYPE (t1) == TREE_TYPE (t2)
433 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
436 return function_types_compatible_p (t1, t2);
440 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
442 tree d1 = TYPE_DOMAIN (t1);
443 tree d2 = TYPE_DOMAIN (t2);
445 /* Target types must match incl. qualifiers. */
446 if (TREE_TYPE (t1) != TREE_TYPE (t2)
447 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
450 /* Sizes must match unless one is missing or variable. */
451 if (d1 == 0 || d2 == 0 || d1 == d2
452 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
453 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
454 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
455 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
458 return (((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
459 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
460 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
461 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
462 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
463 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
464 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
465 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))))
470 if (maybe_objc_comptypes (t1, t2, 0) == 1)
476 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
477 ignoring their qualifiers. */
480 comp_target_types (ttl, ttr)
485 /* Give maybe_objc_comptypes a crack at letting these types through. */
486 if (val = maybe_objc_comptypes (ttl, ttr, 1) >= 0)
489 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
490 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
492 if (val == 2 && pedantic)
493 pedwarn ("types are not quite compatible");
497 /* Subroutines of `comptypes'. */
499 /* Return 1 if two function types F1 and F2 are compatible.
500 If either type specifies no argument types,
501 the other must specify a fixed number of self-promoting arg types.
502 Otherwise, if one type specifies only the number of arguments,
503 the other must specify that number of self-promoting arg types.
504 Otherwise, the argument types must match. */
507 function_types_compatible_p (f1, f2)
511 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
515 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
516 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
519 args1 = TYPE_ARG_TYPES (f1);
520 args2 = TYPE_ARG_TYPES (f2);
522 /* An unspecified parmlist matches any specified parmlist
523 whose argument types don't need default promotions. */
527 if (!self_promoting_args_p (args2))
529 /* If one of these types comes from a non-prototype fn definition,
530 compare that with the other type's arglist.
531 If they don't match, ask for a warning (but no error). */
532 if (TYPE_ACTUAL_ARG_TYPES (f1)
533 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
539 if (!self_promoting_args_p (args1))
541 if (TYPE_ACTUAL_ARG_TYPES (f2)
542 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
547 /* Both types have argument lists: compare them and propagate results. */
548 val1 = type_lists_compatible_p (args1, args2);
549 return val1 != 1 ? val1 : val;
552 /* Check two lists of types for compatibility,
553 returning 0 for incompatible, 1 for compatible,
554 or 2 for compatible with warning. */
557 type_lists_compatible_p (args1, args2)
560 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
566 if (args1 == 0 && args2 == 0)
568 /* If one list is shorter than the other,
569 they fail to match. */
570 if (args1 == 0 || args2 == 0)
572 /* A null pointer instead of a type
573 means there is supposed to be an argument
574 but nothing is specified about what type it has.
575 So match anything that self-promotes. */
576 if (TREE_VALUE (args1) == 0)
578 if (! self_promoting_type_p (TREE_VALUE (args2)))
581 else if (TREE_VALUE (args2) == 0)
583 if (! self_promoting_type_p (TREE_VALUE (args1)))
586 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
588 /* Allow wait (union {union wait *u; int *i} *)
589 and wait (union wait *) to be compatible. */
590 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
591 && TYPE_NAME (TREE_VALUE (args1)) == 0
592 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
593 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
594 TYPE_SIZE (TREE_VALUE (args2))))
597 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
598 memb; memb = TREE_CHAIN (memb))
599 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
604 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
605 && TYPE_NAME (TREE_VALUE (args2)) == 0
606 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
607 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
608 TYPE_SIZE (TREE_VALUE (args1))))
611 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
612 memb; memb = TREE_CHAIN (memb))
613 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
622 /* comptypes said ok, but record if it said to warn. */
626 args1 = TREE_CHAIN (args1);
627 args2 = TREE_CHAIN (args2);
631 /* Return 1 if PARMS specifies a fixed number of parameters
632 and none of their types is affected by default promotions. */
635 self_promoting_args_p (parms)
639 for (t = parms; t; t = TREE_CHAIN (t))
641 register tree type = TREE_VALUE (t);
643 if (TREE_CHAIN (t) == 0 && type != void_type_node)
649 if (TYPE_MAIN_VARIANT (type) == float_type_node)
652 if (C_PROMOTING_INTEGER_TYPE_P (type))
658 /* Return 1 if TYPE is not affected by default promotions. */
661 self_promoting_type_p (type)
664 if (TYPE_MAIN_VARIANT (type) == float_type_node)
667 if (C_PROMOTING_INTEGER_TYPE_P (type))
673 /* Return an unsigned type the same as TYPE in other respects. */
679 tree type1 = TYPE_MAIN_VARIANT (type);
680 if (type1 == signed_char_type_node || type1 == char_type_node)
681 return unsigned_char_type_node;
682 if (type1 == integer_type_node)
683 return unsigned_type_node;
684 if (type1 == short_integer_type_node)
685 return short_unsigned_type_node;
686 if (type1 == long_integer_type_node)
687 return long_unsigned_type_node;
688 if (type1 == long_long_integer_type_node)
689 return long_long_unsigned_type_node;
693 /* Return a signed type the same as TYPE in other respects. */
699 tree type1 = TYPE_MAIN_VARIANT (type);
700 if (type1 == unsigned_char_type_node || type1 == char_type_node)
701 return signed_char_type_node;
702 if (type1 == unsigned_type_node)
703 return integer_type_node;
704 if (type1 == short_unsigned_type_node)
705 return short_integer_type_node;
706 if (type1 == long_unsigned_type_node)
707 return long_integer_type_node;
708 if (type1 == long_long_unsigned_type_node)
709 return long_long_integer_type_node;
713 /* Return a type the same as TYPE except unsigned or
714 signed according to UNSIGNEDP. */
717 signed_or_unsigned_type (unsignedp, type)
721 if (! INTEGRAL_TYPE_P (type))
723 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
724 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
725 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
726 return unsignedp ? unsigned_type_node : integer_type_node;
727 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
728 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
729 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
730 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
731 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
732 return (unsignedp ? long_long_unsigned_type_node
733 : long_long_integer_type_node);
737 /* Compute the value of the `sizeof' operator. */
743 enum tree_code code = TREE_CODE (type);
746 if (code == FUNCTION_TYPE)
748 if (pedantic || warn_pointer_arith)
749 pedwarn ("sizeof applied to a function type");
752 if (code == VOID_TYPE)
754 if (pedantic || warn_pointer_arith)
755 pedwarn ("sizeof applied to a void type");
758 if (code == ERROR_MARK)
760 if (TYPE_SIZE (type) == 0)
762 error ("sizeof applied to an incomplete type");
766 /* Convert in case a char is more than one unit. */
767 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
768 size_int (TYPE_PRECISION (char_type_node)));
769 /* size_binop does not put the constant in range, so do it now. */
770 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
771 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
776 c_sizeof_nowarn (type)
779 enum tree_code code = TREE_CODE (type);
782 if (code == FUNCTION_TYPE
784 || code == ERROR_MARK)
786 if (TYPE_SIZE (type) == 0)
789 /* Convert in case a char is more than one unit. */
790 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
791 size_int (TYPE_PRECISION (char_type_node)));
792 force_fit_type (t, 0);
796 /* Compute the size to increment a pointer by. */
799 c_size_in_bytes (type)
802 enum tree_code code = TREE_CODE (type);
805 if (code == FUNCTION_TYPE)
807 if (code == VOID_TYPE)
809 if (code == ERROR_MARK)
811 if (TYPE_SIZE (type) == 0)
813 error ("arithmetic on pointer to an incomplete type");
817 /* Convert in case a char is more than one unit. */
818 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
819 size_int (BITS_PER_UNIT));
820 force_fit_type (t, 0);
824 /* Implement the __alignof keyword: Return the minimum required
825 alignment of TYPE, measured in bytes. */
831 enum tree_code code = TREE_CODE (type);
833 if (code == FUNCTION_TYPE)
834 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
836 if (code == VOID_TYPE || code == ERROR_MARK)
839 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
842 /* Implement the __alignof keyword: Return the minimum required
843 alignment of EXPR, measured in bytes. For VAR_DECL's and
844 FIELD_DECL's return DECL_ALIGN (which can be set from an
845 "aligned" __attribute__ specification). */
848 c_alignof_expr (expr)
851 if (TREE_CODE (expr) == VAR_DECL)
852 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
854 if (TREE_CODE (expr) == COMPONENT_REF
855 && DECL_BIT_FIELD (TREE_OPERAND (expr, 1)))
857 error ("`__alignof' applied to a bit-field");
860 else if (TREE_CODE (expr) == COMPONENT_REF
861 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
862 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
864 if (TREE_CODE (expr) == INDIRECT_REF)
866 tree t = TREE_OPERAND (expr, 0);
868 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
870 while (TREE_CODE (t) == NOP_EXPR
871 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
875 t = TREE_OPERAND (t, 0);
876 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
877 if (thisalign > bestalign)
878 best = t, bestalign = thisalign;
880 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
883 return c_alignof (TREE_TYPE (expr));
885 /* Return either DECL or its known constant value (if it has one). */
888 decl_constant_value (decl)
891 if (! TREE_PUBLIC (decl)
892 /* Don't change a variable array bound or initial value to a constant
893 in a place where a variable is invalid. */
894 && current_function_decl != 0
896 && ! TREE_THIS_VOLATILE (decl)
897 && DECL_INITIAL (decl) != 0
898 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
899 /* This is invalid if initial value is not constant.
900 If it has either a function call, a memory reference,
901 or a variable, then re-evaluating it could give different results. */
902 && TREE_CONSTANT (DECL_INITIAL (decl))
903 /* Check for cases where this is sub-optimal, even though valid. */
904 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
905 && DECL_MODE (decl) != BLKmode)
906 return DECL_INITIAL (decl);
910 /* Perform default promotions for C data used in expressions.
911 Arrays and functions are converted to pointers;
912 enumeral types or short or char, to int.
913 In addition, manifest constants symbols are replaced by their values. */
916 default_conversion (exp)
919 register tree type = TREE_TYPE (exp);
920 register enum tree_code code = TREE_CODE (type);
922 /* Constants can be used directly unless they're not loadable. */
923 if (TREE_CODE (exp) == CONST_DECL)
924 exp = DECL_INITIAL (exp);
925 /* Replace a nonvolatile const static variable with its value. */
927 && TREE_CODE (exp) == VAR_DECL
928 && TREE_READONLY (exp)
929 /* But not for iterators! */
931 && DECL_MODE (exp) != BLKmode)
933 exp = decl_constant_value (exp);
934 type = TREE_TYPE (exp);
937 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
939 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
940 to integer and cause infinite recursion. */
941 while (TREE_CODE (exp) == NON_LVALUE_EXPR
942 || (TREE_CODE (exp) == NOP_EXPR
943 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
944 exp = TREE_OPERAND (exp, 0);
946 /* Normally convert enums to int,
947 but convert wide enums to something wider. */
948 if (code == ENUMERAL_TYPE)
950 type = type_for_size (MAX (TYPE_PRECISION (type),
951 TYPE_PRECISION (integer_type_node)),
953 || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node))
954 && TREE_UNSIGNED (type)));
955 return convert (type, exp);
958 if (C_PROMOTING_INTEGER_TYPE_P (type))
960 /* Traditionally, unsignedness is preserved in default promotions.
961 Also preserve unsignedness if not really getting any wider. */
962 if (TREE_UNSIGNED (type)
964 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
965 return convert (unsigned_type_node, exp);
966 return convert (integer_type_node, exp);
968 if (flag_traditional && !flag_allow_single_precision
969 && TYPE_MAIN_VARIANT (type) == float_type_node)
970 return convert (double_type_node, exp);
971 if (code == VOID_TYPE)
973 error ("void value not ignored as it ought to be");
974 return error_mark_node;
976 if (code == FUNCTION_TYPE)
978 return build_unary_op (ADDR_EXPR, exp, 0);
980 if (code == ARRAY_TYPE)
983 tree restype = TREE_TYPE (type);
988 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
989 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
991 constp = TREE_READONLY (exp);
992 volatilep = TREE_THIS_VOLATILE (exp);
995 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
996 || constp || volatilep)
997 restype = c_build_type_variant (restype,
998 TYPE_READONLY (type) || constp,
999 TYPE_VOLATILE (type) || volatilep);
1001 if (TREE_CODE (exp) == INDIRECT_REF)
1002 return convert (TYPE_POINTER_TO (restype),
1003 TREE_OPERAND (exp, 0));
1005 if (TREE_CODE (exp) == COMPOUND_EXPR)
1007 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1008 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1009 TREE_OPERAND (exp, 0), op1);
1013 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1015 error ("invalid use of non-lvalue array");
1016 return error_mark_node;
1019 ptrtype = build_pointer_type (restype);
1021 if (TREE_CODE (exp) == VAR_DECL)
1023 /* ??? This is not really quite correct
1024 in that the type of the operand of ADDR_EXPR
1025 is not the target type of the type of the ADDR_EXPR itself.
1026 Question is, can this lossage be avoided? */
1027 adr = build1 (ADDR_EXPR, ptrtype, exp);
1028 if (mark_addressable (exp) == 0)
1029 return error_mark_node;
1030 TREE_CONSTANT (adr) = staticp (exp);
1031 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1034 /* This way is better for a COMPONENT_REF since it can
1035 simplify the offset for a component. */
1036 adr = build_unary_op (ADDR_EXPR, exp, 1);
1037 return convert (ptrtype, adr);
1042 /* Look up component name in the structure type definition.
1044 If this component name is found indirectly within an anonymous union,
1045 store in *INDIRECT the component which directly contains
1046 that anonymous union. Otherwise, set *INDIRECT to 0. */
1049 lookup_field (type, component, indirect)
1050 tree type, component;
1055 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1056 to the field elements. Use a binary search on this array to quickly
1057 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1058 will always be set for structures which have many elements. */
1060 if (TYPE_LANG_SPECIFIC (type))
1063 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1065 field = TYPE_FIELDS (type);
1067 top = TYPE_LANG_SPECIFIC (type)->len;
1068 while (top - bot > 1)
1072 half = (top - bot + 1) >> 1;
1073 field = field_array[bot+half];
1075 if (DECL_NAME (field) == NULL_TREE)
1077 /* Step through all anon unions in linear fashion. */
1078 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1082 field = field_array[bot++];
1083 anon = lookup_field (TREE_TYPE (field), component, &junk);
1084 if (anon != NULL_TREE)
1091 /* Entire record is only anon unions. */
1095 /* Restart the binary search, with new lower bound. */
1099 cmp = (HOST_WIDE_INT) DECL_NAME (field) - (HOST_WIDE_INT) component;
1108 if (DECL_NAME (field_array[bot]) == component)
1109 field = field_array[bot];
1110 else if (DECL_NAME (field) != component)
1115 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1117 if (DECL_NAME (field) == NULL_TREE)
1120 tree anon = lookup_field (TREE_TYPE (field), component, &junk);
1121 if (anon != NULL_TREE)
1128 if (DECL_NAME (field) == component)
1133 *indirect = NULL_TREE;
1137 /* Make an expression to refer to the COMPONENT field of
1138 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1141 build_component_ref (datum, component)
1142 tree datum, component;
1144 register tree type = TREE_TYPE (datum);
1145 register enum tree_code code = TREE_CODE (type);
1146 register tree field = NULL;
1149 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1150 unless we are not to support things not strictly ANSI. */
1151 switch (TREE_CODE (datum))
1155 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1156 return build (COMPOUND_EXPR, TREE_TYPE (value),
1157 TREE_OPERAND (datum, 0), value);
1160 return build_conditional_expr
1161 (TREE_OPERAND (datum, 0),
1162 build_component_ref (TREE_OPERAND (datum, 1), component),
1163 build_component_ref (TREE_OPERAND (datum, 2), component));
1166 /* See if there is a field or component with name COMPONENT. */
1168 if (code == RECORD_TYPE || code == UNION_TYPE)
1172 if (TYPE_SIZE (type) == 0)
1174 incomplete_type_error (NULL_TREE, type);
1175 return error_mark_node;
1178 field = lookup_field (type, component, &indirect);
1182 error (code == RECORD_TYPE
1183 ? "structure has no member named `%s'"
1184 : "union has no member named `%s'",
1185 IDENTIFIER_POINTER (component));
1186 return error_mark_node;
1188 if (TREE_TYPE (field) == error_mark_node)
1189 return error_mark_node;
1191 /* If FIELD was found buried within an anonymous union,
1192 make one COMPONENT_REF to get that anonymous union,
1193 then fall thru to make a second COMPONENT_REF to get FIELD. */
1196 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1197 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1198 TREE_READONLY (ref) = 1;
1199 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1200 TREE_THIS_VOLATILE (ref) = 1;
1204 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1206 if (TREE_READONLY (datum) || TREE_READONLY (field))
1207 TREE_READONLY (ref) = 1;
1208 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1209 TREE_THIS_VOLATILE (ref) = 1;
1213 else if (code != ERROR_MARK)
1214 error ("request for member `%s' in something not a structure or union",
1215 IDENTIFIER_POINTER (component));
1217 return error_mark_node;
1220 /* Given an expression PTR for a pointer, return an expression
1221 for the value pointed to.
1222 ERRORSTRING is the name of the operator to appear in error messages. */
1225 build_indirect_ref (ptr, errorstring)
1229 register tree pointer = default_conversion (ptr);
1230 register tree type = TREE_TYPE (pointer);
1232 if (TREE_CODE (type) == POINTER_TYPE)
1234 if (TREE_CODE (pointer) == ADDR_EXPR
1236 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1237 == TREE_TYPE (type)))
1238 return TREE_OPERAND (pointer, 0);
1241 tree t = TREE_TYPE (type);
1242 register tree ref = build1 (INDIRECT_REF,
1243 TYPE_MAIN_VARIANT (t), pointer);
1245 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1247 error ("dereferencing pointer to incomplete type");
1248 return error_mark_node;
1250 if (TREE_CODE (t) == VOID_TYPE)
1251 warning ("dereferencing `void *' pointer");
1253 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1254 so that we get the proper error message if the result is used
1255 to assign to. Also, &* is supposed to be a no-op.
1256 And ANSI C seems to specify that the type of the result
1257 should be the const type. */
1258 /* A de-reference of a pointer to const is not a const. It is valid
1259 to change it via some other pointer. */
1260 TREE_READONLY (ref) = TYPE_READONLY (t);
1261 TREE_SIDE_EFFECTS (ref)
1262 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1263 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1267 else if (TREE_CODE (pointer) != ERROR_MARK)
1268 error ("invalid type argument of `%s'", errorstring);
1269 return error_mark_node;
1272 /* This handles expressions of the form "a[i]", which denotes
1275 This is logically equivalent in C to *(a+i), but we may do it differently.
1276 If A is a variable or a member, we generate a primitive ARRAY_REF.
1277 This avoids forcing the array out of registers, and can work on
1278 arrays that are not lvalues (for example, members of structures returned
1282 build_array_ref (array, index)
1287 error ("subscript missing in array reference");
1288 return error_mark_node;
1291 if (TREE_TYPE (array) == error_mark_node
1292 || TREE_TYPE (index) == error_mark_node)
1293 return error_mark_node;
1295 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1296 && TREE_CODE (array) != INDIRECT_REF)
1300 /* Subscripting with type char is likely to lose
1301 on a machine where chars are signed.
1302 So warn on any machine, but optionally.
1303 Don't warn for unsigned char since that type is safe.
1304 Don't warn for signed char because anyone who uses that
1305 must have done so deliberately. */
1306 if (warn_char_subscripts
1307 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1308 warning ("array subscript has type `char'");
1310 /* Apply default promotions *after* noticing character types. */
1311 index = default_conversion (index);
1313 /* Require integer *after* promotion, for sake of enums. */
1314 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1316 error ("array subscript is not an integer");
1317 return error_mark_node;
1320 /* An array that is indexed by a non-constant
1321 cannot be stored in a register; we must be able to do
1322 address arithmetic on its address.
1323 Likewise an array of elements of variable size. */
1324 if (TREE_CODE (index) != INTEGER_CST
1325 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1326 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1328 if (mark_addressable (array) == 0)
1329 return error_mark_node;
1331 /* An array that is indexed by a constant value which is not within
1332 the array bounds cannot be stored in a register either; because we
1333 would get a crash in store_bit_field/extract_bit_field when trying
1334 to access a non-existent part of the register. */
1335 if (TREE_CODE (index) == INTEGER_CST
1336 && TYPE_VALUES (TREE_TYPE (array))
1337 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1339 if (mark_addressable (array) == 0)
1340 return error_mark_node;
1343 if (pedantic && !lvalue_p (array))
1345 if (DECL_REGISTER (array))
1346 pedwarn ("ANSI C forbids subscripting `register' array");
1348 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1354 while (TREE_CODE (foo) == COMPONENT_REF)
1355 foo = TREE_OPERAND (foo, 0);
1356 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1357 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1360 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1361 rval = build (ARRAY_REF, type, array, index);
1362 /* Array ref is const/volatile if the array elements are
1363 or if the array is. */
1364 TREE_READONLY (rval)
1365 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1366 | TREE_READONLY (array));
1367 TREE_SIDE_EFFECTS (rval)
1368 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1369 | TREE_SIDE_EFFECTS (array));
1370 TREE_THIS_VOLATILE (rval)
1371 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1372 /* This was added by rms on 16 Nov 91.
1373 It fixes vol struct foo *a; a->elts[1]
1374 in an inline function.
1375 Hope it doesn't break something else. */
1376 | TREE_THIS_VOLATILE (array));
1377 return require_complete_type (fold (rval));
1381 tree ar = default_conversion (array);
1382 tree ind = default_conversion (index);
1384 /* Put the integer in IND to simplify error checking. */
1385 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1392 if (ar == error_mark_node)
1395 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
1397 error ("subscripted value is neither array nor pointer");
1398 return error_mark_node;
1400 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1402 error ("array subscript is not an integer");
1403 return error_mark_node;
1406 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1411 /* Build a function call to function FUNCTION with parameters PARAMS.
1412 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1413 TREE_VALUE of each node is a parameter-expression.
1414 FUNCTION's data type may be a function type or a pointer-to-function. */
1417 build_function_call (function, params)
1418 tree function, params;
1420 register tree fntype, fundecl = 0;
1421 register tree coerced_params;
1422 tree name = NULL_TREE, assembler_name = NULL_TREE;
1424 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1425 STRIP_TYPE_NOPS (function);
1427 /* Convert anything with function type to a pointer-to-function. */
1428 if (TREE_CODE (function) == FUNCTION_DECL)
1430 name = DECL_NAME (function);
1431 assembler_name = DECL_ASSEMBLER_NAME (function);
1433 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1434 (because calling an inline function does not mean the function
1435 needs to be separately compiled). */
1436 fntype = build_type_variant (TREE_TYPE (function),
1437 TREE_READONLY (function),
1438 TREE_THIS_VOLATILE (function));
1440 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1443 function = default_conversion (function);
1445 fntype = TREE_TYPE (function);
1447 if (TREE_CODE (fntype) == ERROR_MARK)
1448 return error_mark_node;
1450 if (!(TREE_CODE (fntype) == POINTER_TYPE
1451 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1453 error ("called object is not a function");
1454 return error_mark_node;
1457 /* fntype now gets the type of function pointed to. */
1458 fntype = TREE_TYPE (fntype);
1460 /* Convert the parameters to the types declared in the
1461 function prototype, or apply default promotions. */
1464 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1466 /* Check for errors in format strings. */
1468 if (warn_format && (name || assembler_name))
1469 check_function_format (name, assembler_name, coerced_params);
1471 /* Recognize certain built-in functions so we can make tree-codes
1472 other than CALL_EXPR. We do this when it enables fold-const.c
1473 to do something useful. */
1475 if (TREE_CODE (function) == ADDR_EXPR
1476 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1477 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1478 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1483 if (coerced_params == 0)
1484 return integer_zero_node;
1485 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1489 register tree result
1490 = build (CALL_EXPR, TREE_TYPE (fntype),
1491 function, coerced_params, NULL_TREE);
1493 TREE_SIDE_EFFECTS (result) = 1;
1494 if (TREE_TYPE (result) == void_type_node)
1496 return require_complete_type (result);
1500 /* Convert the argument expressions in the list VALUES
1501 to the types in the list TYPELIST. The result is a list of converted
1502 argument expressions.
1504 If TYPELIST is exhausted, or when an element has NULL as its type,
1505 perform the default conversions.
1507 PARMLIST is the chain of parm decls for the function being called.
1508 It may be 0, if that info is not available.
1509 It is used only for generating error messages.
1511 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1513 This is also where warnings about wrong number of args are generated.
1515 Both VALUES and the returned value are chains of TREE_LIST nodes
1516 with the elements of the list in the TREE_VALUE slots of those nodes. */
1519 convert_arguments (typelist, values, name, fundecl)
1520 tree typelist, values, name, fundecl;
1522 register tree typetail, valtail;
1523 register tree result = NULL;
1526 /* Scan the given expressions and types, producing individual
1527 converted arguments and pushing them on RESULT in reverse order. */
1529 for (valtail = values, typetail = typelist, parmnum = 0;
1531 valtail = TREE_CHAIN (valtail), parmnum++)
1533 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1534 register tree val = TREE_VALUE (valtail);
1536 if (type == void_type_node)
1539 error ("too many arguments to function `%s'",
1540 IDENTIFIER_POINTER (name));
1542 error ("too many arguments to function");
1546 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1547 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1548 to convert automatically to a pointer. */
1549 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1550 val = TREE_OPERAND (val, 0);
1552 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1553 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1554 val = default_conversion (val);
1556 val = require_complete_type (val);
1560 /* Formal parm type is specified by a function prototype. */
1563 if (TYPE_SIZE (type) == 0)
1565 error ("type of formal parameter %d is incomplete", parmnum + 1);
1570 #ifdef PROMOTE_PROTOTYPES
1571 /* Rather than truncating and then reextending,
1572 convert directly to int, if that's the type we will want. */
1573 if (! flag_traditional
1574 && (TREE_CODE (type) == INTEGER_TYPE
1575 || TREE_CODE (type) == ENUMERAL_TYPE)
1576 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1577 type = integer_type_node;
1580 #if 0 /* This turns out not to win--there's no way to write a prototype
1581 for a function whose arg type is a union with no tag. */
1582 /* Nameless union automatically casts the types it contains. */
1583 if (TREE_CODE (type) == UNION_TYPE && TYPE_NAME (type) == 0)
1587 for (field = TYPE_FIELDS (type); field;
1588 field = TREE_CHAIN (field))
1589 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
1590 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1594 val = build1 (CONVERT_EXPR, type, val);
1598 /* Optionally warn about conversions that
1599 differ from the default conversions. */
1600 if (warn_conversion)
1602 int formal_prec = TYPE_PRECISION (type);
1604 if (TREE_CODE (type) != REAL_TYPE
1605 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1606 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1607 else if (TREE_CODE (type) == REAL_TYPE
1608 && TREE_CODE (TREE_TYPE (val)) != REAL_TYPE)
1609 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1610 else if (TREE_CODE (type) == REAL_TYPE
1611 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1613 /* Warn if any argument is passed as `float',
1614 since without a prototype it would be `double'. */
1615 if (formal_prec == TYPE_PRECISION (float_type_node))
1616 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1618 /* Detect integer changing in width or signedness. */
1619 else if ((TREE_CODE (type) == INTEGER_TYPE
1620 || TREE_CODE (type) == ENUMERAL_TYPE)
1621 && (TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
1622 || TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE))
1624 tree would_have_been = default_conversion (val);
1625 tree type1 = TREE_TYPE (would_have_been);
1627 if (TREE_CODE (type) == ENUMERAL_TYPE
1628 && type == TREE_TYPE (val))
1629 /* No warning if function asks for enum
1630 and the actual arg is that enum type. */
1632 else if (formal_prec != TYPE_PRECISION (type1))
1633 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1634 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1636 /* Don't complain if the formal parameter type
1637 is an enum, because we can't tell now whether
1638 the value was an enum--even the same enum. */
1639 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1641 else if (TREE_CODE (val) == INTEGER_CST
1642 && int_fits_type_p (val, type))
1643 /* Change in signedness doesn't matter
1644 if a constant value is unaffected. */
1646 /* Likewise for a constant in a NOP_EXPR. */
1647 else if (TREE_CODE (val) == NOP_EXPR
1648 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1649 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1651 #if 0 /* We never get such tree structure here. */
1652 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1653 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1654 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1655 /* Change in signedness doesn't matter
1656 if an enum value is unaffected. */
1659 /* If the value is extended from a narrower
1660 unsigned type, it doesn't matter whether we
1661 pass it as signed or unsigned; the value
1662 certainly is the same either way. */
1663 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1664 && TREE_UNSIGNED (TREE_TYPE (val)))
1666 else if (TREE_UNSIGNED (type))
1667 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1669 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1673 parmval = convert_for_assignment (type, val,
1674 (char *)0, /* arg passing */
1675 fundecl, name, parmnum + 1);
1677 #ifdef PROMOTE_PROTOTYPES
1678 if ((TREE_CODE (type) == INTEGER_TYPE
1679 || TREE_CODE (type) == ENUMERAL_TYPE)
1680 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1681 parmval = default_conversion (parmval);
1684 result = tree_cons (NULL_TREE, parmval, result);
1686 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1687 && (TYPE_PRECISION (TREE_TYPE (val))
1688 < TYPE_PRECISION (double_type_node)))
1689 /* Convert `float' to `double'. */
1690 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1692 /* Convert `short' and `char' to full-size `int'. */
1693 result = tree_cons (NULL_TREE, default_conversion (val), result);
1696 typetail = TREE_CHAIN (typetail);
1699 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1702 error ("too few arguments to function `%s'",
1703 IDENTIFIER_POINTER (name));
1705 error ("too few arguments to function");
1708 return nreverse (result);
1711 /* This is the entry point used by the parser
1712 for binary operators in the input.
1713 In addition to constructing the expression,
1714 we check for operands that were written with other binary operators
1715 in a way that is likely to confuse the user. */
1718 parser_build_binary_op (code, arg1, arg2)
1719 enum tree_code code;
1722 tree result = build_binary_op (code, arg1, arg2, 1);
1725 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1726 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1727 enum tree_code code1 = ERROR_MARK;
1728 enum tree_code code2 = ERROR_MARK;
1730 if (class1 == 'e' || class1 == '1'
1731 || class1 == '2' || class1 == '<')
1732 code1 = C_EXP_ORIGINAL_CODE (arg1);
1733 if (class2 == 'e' || class2 == '1'
1734 || class2 == '2' || class2 == '<')
1735 code2 = C_EXP_ORIGINAL_CODE (arg2);
1737 /* Check for cases such as x+y<<z which users are likely
1738 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1739 is cleared to prevent these warnings. */
1740 if (warn_parentheses)
1742 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1744 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1745 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1746 warning ("suggest parentheses around + or - inside shift");
1749 if (code == TRUTH_ORIF_EXPR)
1751 if (code1 == TRUTH_ANDIF_EXPR
1752 || code2 == TRUTH_ANDIF_EXPR)
1753 warning ("suggest parentheses around && within ||");
1756 if (code == BIT_IOR_EXPR)
1758 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1759 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1760 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1761 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1762 warning ("suggest parentheses around arithmetic in operand of |");
1765 if (code == BIT_XOR_EXPR)
1767 if (code1 == BIT_AND_EXPR
1768 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1769 || code2 == BIT_AND_EXPR
1770 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1771 warning ("suggest parentheses around arithmetic in operand of ^");
1774 if (code == BIT_AND_EXPR)
1776 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1777 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1778 warning ("suggest parentheses around + or - in operand of &");
1782 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1783 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1784 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1785 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1787 unsigned_conversion_warning (result, arg1);
1788 unsigned_conversion_warning (result, arg2);
1789 overflow_warning (result);
1791 class = TREE_CODE_CLASS (TREE_CODE (result));
1793 /* Record the code that was specified in the source,
1794 for the sake of warnings about confusing nesting. */
1795 if (class == 'e' || class == '1'
1796 || class == '2' || class == '<')
1797 C_SET_EXP_ORIGINAL_CODE (result, code);
1800 int flag = TREE_CONSTANT (result);
1801 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1802 so that convert_for_assignment wouldn't strip it.
1803 That way, we got warnings for things like p = (1 - 1).
1804 But it turns out we should not get those warnings. */
1805 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1806 C_SET_EXP_ORIGINAL_CODE (result, code);
1807 TREE_CONSTANT (result) = flag;
1813 /* Build a binary-operation expression without default conversions.
1814 CODE is the kind of expression to build.
1815 This function differs from `build' in several ways:
1816 the data type of the result is computed and recorded in it,
1817 warnings are generated if arg data types are invalid,
1818 special handling for addition and subtraction of pointers is known,
1819 and some optimization is done (operations on narrow ints
1820 are done in the narrower type when that gives the same result).
1821 Constant folding is also done before the result is returned.
1823 Note that the operands will never have enumeral types, or function
1824 or array types, because either they will have the default conversions
1825 performed or they have both just been converted to some other type in which
1826 the arithmetic is to be done. */
1829 build_binary_op (code, orig_op0, orig_op1, convert_p)
1830 enum tree_code code;
1831 tree orig_op0, orig_op1;
1835 register enum tree_code code0, code1;
1838 /* Expression code to give to the expression when it is built.
1839 Normally this is CODE, which is what the caller asked for,
1840 but in some special cases we change it. */
1841 register enum tree_code resultcode = code;
1843 /* Data type in which the computation is to be performed.
1844 In the simplest cases this is the common type of the arguments. */
1845 register tree result_type = NULL;
1847 /* Nonzero means operands have already been type-converted
1848 in whatever way is necessary.
1849 Zero means they need to be converted to RESULT_TYPE. */
1852 /* Nonzero means after finally constructing the expression
1853 give it this type. Otherwise, give it type RESULT_TYPE. */
1854 tree final_type = 0;
1856 /* Nonzero if this is an operation like MIN or MAX which can
1857 safely be computed in short if both args are promoted shorts.
1858 Also implies COMMON.
1859 -1 indicates a bitwise operation; this makes a difference
1860 in the exact conditions for when it is safe to do the operation
1861 in a narrower mode. */
1864 /* Nonzero if this is a comparison operation;
1865 if both args are promoted shorts, compare the original shorts.
1866 Also implies COMMON. */
1867 int short_compare = 0;
1869 /* Nonzero if this is a right-shift operation, which can be computed on the
1870 original short and then promoted if the operand is a promoted short. */
1871 int short_shift = 0;
1873 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1878 op0 = default_conversion (orig_op0);
1879 op1 = default_conversion (orig_op1);
1887 type0 = TREE_TYPE (op0);
1888 type1 = TREE_TYPE (op1);
1890 /* The expression codes of the data types of the arguments tell us
1891 whether the arguments are integers, floating, pointers, etc. */
1892 code0 = TREE_CODE (type0);
1893 code1 = TREE_CODE (type1);
1895 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1896 STRIP_TYPE_NOPS (op0);
1897 STRIP_TYPE_NOPS (op1);
1899 /* If an error was already reported for one of the arguments,
1900 avoid reporting another error. */
1902 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1903 return error_mark_node;
1908 /* Handle the pointer + int case. */
1909 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1910 return pointer_int_sum (PLUS_EXPR, op0, op1);
1911 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1912 return pointer_int_sum (PLUS_EXPR, op1, op0);
1918 /* Subtraction of two similar pointers.
1919 We must subtract them as integers, then divide by object size. */
1920 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1921 && comp_target_types (type0, type1))
1922 return pointer_diff (op0, op1);
1923 /* Handle pointer minus int. Just like pointer plus int. */
1924 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1925 return pointer_int_sum (MINUS_EXPR, op0, op1);
1934 case TRUNC_DIV_EXPR:
1936 case FLOOR_DIV_EXPR:
1937 case ROUND_DIV_EXPR:
1938 case EXACT_DIV_EXPR:
1939 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1940 || code0 == COMPLEX_TYPE)
1941 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1942 || code1 == COMPLEX_TYPE))
1944 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
1945 resultcode = RDIV_EXPR;
1948 /* Although it would be tempting to shorten always here, that
1949 loses on some targets, since the modulo instruction is
1950 undefined if the quotient can't be represented in the
1951 computation mode. We shorten only if unsigned or if
1952 dividing by something we know != -1. */
1953 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
1954 || (TREE_CODE (op1) == INTEGER_CST
1955 && (TREE_INT_CST_LOW (op1) != -1
1956 || TREE_INT_CST_HIGH (op1) != -1)));
1963 case BIT_ANDTC_EXPR:
1966 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1968 /* If one operand is a constant, and the other is a short type
1969 that has been converted to an int,
1970 really do the work in the short type and then convert the
1971 result to int. If we are lucky, the constant will be 0 or 1
1972 in the short type, making the entire operation go away. */
1973 if (TREE_CODE (op0) == INTEGER_CST
1974 && TREE_CODE (op1) == NOP_EXPR
1975 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
1976 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
1978 final_type = result_type;
1979 op1 = TREE_OPERAND (op1, 0);
1980 result_type = TREE_TYPE (op1);
1982 if (TREE_CODE (op1) == INTEGER_CST
1983 && TREE_CODE (op0) == NOP_EXPR
1984 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
1985 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
1987 final_type = result_type;
1988 op0 = TREE_OPERAND (op0, 0);
1989 result_type = TREE_TYPE (op0);
1993 case TRUNC_MOD_EXPR:
1994 case FLOOR_MOD_EXPR:
1995 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1997 /* Although it would be tempting to shorten always here, that loses
1998 on some targets, since the modulo instruction is undefined if the
1999 quotient can't be represented in the computation mode. We shorten
2000 only if unsigned or if dividing by something we know != -1. */
2001 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2002 || (TREE_CODE (op1) == INTEGER_CST
2003 && (TREE_INT_CST_LOW (op1) != -1
2004 || TREE_INT_CST_HIGH (op1) != -1)));
2009 case TRUTH_ANDIF_EXPR:
2010 case TRUTH_ORIF_EXPR:
2011 case TRUTH_AND_EXPR:
2013 case TRUTH_XOR_EXPR:
2014 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2015 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2016 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2017 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2019 /* Result of these operations is always an int,
2020 but that does not mean the operands should be
2021 converted to ints! */
2022 result_type = integer_type_node;
2023 op0 = truthvalue_conversion (op0);
2024 op1 = truthvalue_conversion (op1);
2029 /* Shift operations: result has same type as first operand;
2030 always convert second operand to int.
2031 Also set SHORT_SHIFT if shifting rightward. */
2034 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2036 if (TREE_CODE (op1) == INTEGER_CST)
2038 if (tree_int_cst_sgn (op1) < 0)
2039 warning ("right shift count is negative");
2042 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2044 if (TREE_INT_CST_HIGH (op1) != 0
2045 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2046 >= TYPE_PRECISION (type0)))
2047 warning ("right shift count >= width of type");
2050 /* Use the type of the value to be shifted.
2051 This is what most traditional C compilers do. */
2052 result_type = type0;
2053 /* Unless traditional, convert the shift-count to an integer,
2054 regardless of size of value being shifted. */
2055 if (! flag_traditional)
2057 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2058 op1 = convert (integer_type_node, op1);
2059 /* Avoid converting op1 to result_type later. */
2066 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2068 if (TREE_CODE (op1) == INTEGER_CST)
2070 if (tree_int_cst_sgn (op1) < 0)
2071 warning ("left shift count is negative");
2072 else if (TREE_INT_CST_HIGH (op1) != 0
2073 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2074 >= TYPE_PRECISION (type0)))
2075 warning ("left shift count >= width of type");
2077 /* Use the type of the value to be shifted.
2078 This is what most traditional C compilers do. */
2079 result_type = type0;
2080 /* Unless traditional, convert the shift-count to an integer,
2081 regardless of size of value being shifted. */
2082 if (! flag_traditional)
2084 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2085 op1 = convert (integer_type_node, op1);
2086 /* Avoid converting op1 to result_type later. */
2094 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2096 if (TREE_CODE (op1) == INTEGER_CST)
2098 if (tree_int_cst_sgn (op1) < 0)
2099 warning ("shift count is negative");
2100 else if (TREE_INT_CST_HIGH (op1) != 0
2101 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2102 >= TYPE_PRECISION (type0)))
2103 warning ("shift count >= width of type");
2105 /* Use the type of the value to be shifted.
2106 This is what most traditional C compilers do. */
2107 result_type = type0;
2108 /* Unless traditional, convert the shift-count to an integer,
2109 regardless of size of value being shifted. */
2110 if (! flag_traditional)
2112 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2113 op1 = convert (integer_type_node, op1);
2114 /* Avoid converting op1 to result_type later. */
2122 /* Result of comparison is always int,
2123 but don't convert the args to int! */
2124 result_type = integer_type_node;
2126 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2127 || code0 == COMPLEX_TYPE)
2128 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2129 || code1 == COMPLEX_TYPE))
2131 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2133 register tree tt0 = TREE_TYPE (type0);
2134 register tree tt1 = TREE_TYPE (type1);
2135 /* Anything compares with void *. void * compares with anything.
2136 Otherwise, the targets must be compatible
2137 and both must be object or both incomplete. */
2138 if (comp_target_types (type0, type1))
2140 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2142 /* op0 != orig_op0 detects the case of something
2143 whose value is 0 but which isn't a valid null ptr const. */
2144 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2145 && TREE_CODE (tt1) == FUNCTION_TYPE)
2146 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2148 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2150 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2151 && TREE_CODE (tt0) == FUNCTION_TYPE)
2152 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2155 pedwarn ("comparison of distinct pointer types lacks a cast");
2157 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2158 && integer_zerop (op1))
2159 op1 = null_pointer_node;
2160 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2161 && integer_zerop (op0))
2162 op0 = null_pointer_node;
2163 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2165 if (! flag_traditional)
2166 pedwarn ("comparison between pointer and integer");
2167 op1 = convert (TREE_TYPE (op0), op1);
2169 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2171 if (! flag_traditional)
2172 pedwarn ("comparison between pointer and integer");
2173 op0 = convert (TREE_TYPE (op1), op0);
2176 /* If args are not valid, clear out RESULT_TYPE
2177 to cause an error message later. */
2183 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2184 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2186 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2188 if (! comp_target_types (type0, type1))
2189 pedwarn ("comparison of distinct pointer types lacks a cast");
2191 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2192 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2193 result_type = common_type (type0, type1);
2201 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2202 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2204 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2206 if (! comp_target_types (type0, type1))
2207 pedwarn ("comparison of distinct pointer types lacks a cast");
2208 else if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2209 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2210 pedwarn ("comparison of complete and incomplete pointers");
2212 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2213 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2214 result_type = integer_type_node;
2216 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2217 && integer_zerop (op1))
2219 result_type = integer_type_node;
2220 op1 = null_pointer_node;
2222 pedwarn ("ordered comparison of pointer with integer zero");
2224 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2225 && integer_zerop (op0))
2227 result_type = integer_type_node;
2228 op0 = null_pointer_node;
2230 pedwarn ("ordered comparison of pointer with integer zero");
2232 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2234 result_type = integer_type_node;
2235 if (! flag_traditional)
2236 pedwarn ("comparison between pointer and integer");
2237 op1 = convert (TREE_TYPE (op0), op1);
2239 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2241 result_type = integer_type_node;
2242 if (! flag_traditional)
2243 pedwarn ("comparison between pointer and integer");
2244 op0 = convert (TREE_TYPE (op1), op0);
2250 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2252 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2254 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2256 if (shorten || common || short_compare)
2257 result_type = common_type (type0, type1);
2259 /* For certain operations (which identify themselves by shorten != 0)
2260 if both args were extended from the same smaller type,
2261 do the arithmetic in that type and then extend.
2263 shorten !=0 and !=1 indicates a bitwise operation.
2264 For them, this optimization is safe only if
2265 both args are zero-extended or both are sign-extended.
2266 Otherwise, we might change the result.
2267 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2268 but calculated in (unsigned short) it would be (unsigned short)-1. */
2270 if (shorten && none_complex)
2272 int unsigned0, unsigned1;
2273 tree arg0 = get_narrower (op0, &unsigned0);
2274 tree arg1 = get_narrower (op1, &unsigned1);
2275 /* UNS is 1 if the operation to be done is an unsigned one. */
2276 int uns = TREE_UNSIGNED (result_type);
2279 final_type = result_type;
2281 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2282 but it *requires* conversion to FINAL_TYPE. */
2284 if ((TYPE_PRECISION (TREE_TYPE (op0))
2285 == TYPE_PRECISION (TREE_TYPE (arg0)))
2286 && TREE_TYPE (op0) != final_type)
2287 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2288 if ((TYPE_PRECISION (TREE_TYPE (op1))
2289 == TYPE_PRECISION (TREE_TYPE (arg1)))
2290 && TREE_TYPE (op1) != final_type)
2291 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2293 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2295 /* For bitwise operations, signedness of nominal type
2296 does not matter. Consider only how operands were extended. */
2300 /* Note that in all three cases below we refrain from optimizing
2301 an unsigned operation on sign-extended args.
2302 That would not be valid. */
2304 /* Both args variable: if both extended in same way
2305 from same width, do it in that width.
2306 Do it unsigned if args were zero-extended. */
2307 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2308 < TYPE_PRECISION (result_type))
2309 && (TYPE_PRECISION (TREE_TYPE (arg1))
2310 == TYPE_PRECISION (TREE_TYPE (arg0)))
2311 && unsigned0 == unsigned1
2312 && (unsigned0 || !uns))
2314 = signed_or_unsigned_type (unsigned0,
2315 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2316 else if (TREE_CODE (arg0) == INTEGER_CST
2317 && (unsigned1 || !uns)
2318 && (TYPE_PRECISION (TREE_TYPE (arg1))
2319 < TYPE_PRECISION (result_type))
2320 && (type = signed_or_unsigned_type (unsigned1,
2322 int_fits_type_p (arg0, type)))
2324 else if (TREE_CODE (arg1) == INTEGER_CST
2325 && (unsigned0 || !uns)
2326 && (TYPE_PRECISION (TREE_TYPE (arg0))
2327 < TYPE_PRECISION (result_type))
2328 && (type = signed_or_unsigned_type (unsigned0,
2330 int_fits_type_p (arg1, type)))
2334 /* Shifts can be shortened if shifting right. */
2339 tree arg0 = get_narrower (op0, &unsigned_arg);
2341 final_type = result_type;
2343 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2344 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2346 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2347 /* If arg is sign-extended and then unsigned-shifted,
2348 we can simulate this with a signed shift in arg's type
2349 only if the extended result is at least twice as wide
2350 as the arg. Otherwise, the shift could use up all the
2351 ones made by sign-extension and bring in zeros.
2352 We can't optimize that case at all, but in most machines
2353 it never happens because available widths are 2**N. */
2354 && (!TREE_UNSIGNED (final_type)
2356 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2358 /* Do an unsigned shift if the operand was zero-extended. */
2360 = signed_or_unsigned_type (unsigned_arg,
2362 /* Convert value-to-be-shifted to that type. */
2363 if (TREE_TYPE (op0) != result_type)
2364 op0 = convert (result_type, op0);
2369 /* Comparison operations are shortened too but differently.
2370 They identify themselves by setting short_compare = 1. */
2374 /* Don't write &op0, etc., because that would prevent op0
2375 from being kept in a register.
2376 Instead, make copies of the our local variables and
2377 pass the copies by reference, then copy them back afterward. */
2378 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2379 enum tree_code xresultcode = resultcode;
2381 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2384 op0 = xop0, op1 = xop1, result_type = xresult_type;
2385 resultcode = xresultcode;
2389 tree op0_type = TREE_TYPE (orig_op0);
2390 tree op1_type = TREE_TYPE (orig_op1);
2391 int op0_unsigned = TREE_UNSIGNED (op0_type);
2392 int op1_unsigned = TREE_UNSIGNED (op1_type);
2394 /* Give warnings for comparisons between signed and unsigned
2395 quantities that will fail. Do not warn if the signed quantity
2396 is an unsuffixed integer literal (or some static constant
2397 expression involving such literals) and it is positive.
2398 Do not warn if the width of the unsigned quantity is less
2399 than that of the signed quantity, since in this case all
2400 values of the unsigned quantity fit in the signed quantity.
2401 Do not warn if the signed type is the same size as the
2402 result_type since sign extension does not cause trouble in
2404 /* Do the checking based on the original operand trees, so that
2405 casts will be considered, but default promotions won't be. */
2406 if (op0_unsigned != op1_unsigned
2408 && TYPE_PRECISION (op0_type) >= TYPE_PRECISION (op1_type)
2409 && TYPE_PRECISION (op0_type) < TYPE_PRECISION (result_type)
2410 && (TREE_CODE (op1) != INTEGER_CST
2411 || (TREE_CODE (op1) == INTEGER_CST
2412 && INT_CST_LT (op1, integer_zero_node))))
2415 && TYPE_PRECISION (op1_type) >= TYPE_PRECISION (op0_type)
2416 && TYPE_PRECISION (op1_type) < TYPE_PRECISION (result_type)
2417 && (TREE_CODE (op0) != INTEGER_CST
2418 || (TREE_CODE (op0) == INTEGER_CST
2419 && INT_CST_LT (op0, integer_zero_node))))))
2420 warning ("comparison between signed and unsigned");
2425 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2426 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2427 Then the expression will be built.
2428 It will be given type FINAL_TYPE if that is nonzero;
2429 otherwise, it will be given type RESULT_TYPE. */
2433 binary_op_error (code);
2434 return error_mark_node;
2439 if (TREE_TYPE (op0) != result_type)
2440 op0 = convert (result_type, op0);
2441 if (TREE_TYPE (op1) != result_type)
2442 op1 = convert (result_type, op1);
2446 register tree result = build (resultcode, result_type, op0, op1);
2447 register tree folded;
2449 folded = fold (result);
2450 if (folded == result)
2451 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2452 if (final_type != 0)
2453 return convert (final_type, folded);
2458 /* Return a tree for the sum or difference (RESULTCODE says which)
2459 of pointer PTROP and integer INTOP. */
2462 pointer_int_sum (resultcode, ptrop, intop)
2463 enum tree_code resultcode;
2464 register tree ptrop, intop;
2468 register tree result;
2469 register tree folded;
2471 /* The result is a pointer of the same type that is being added. */
2473 register tree result_type = TREE_TYPE (ptrop);
2475 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2477 if (pedantic || warn_pointer_arith)
2478 pedwarn ("pointer of type `void *' used in arithmetic");
2479 size_exp = integer_one_node;
2481 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2483 if (pedantic || warn_pointer_arith)
2484 pedwarn ("pointer to a function used in arithmetic");
2485 size_exp = integer_one_node;
2488 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2490 /* If what we are about to multiply by the size of the elements
2491 contains a constant term, apply distributive law
2492 and multiply that constant term separately.
2493 This helps produce common subexpressions. */
2495 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2496 && ! TREE_CONSTANT (intop)
2497 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2498 && TREE_CONSTANT (size_exp)
2499 /* If the constant comes from pointer subtraction,
2500 skip this optimization--it would cause an error. */
2501 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE)
2503 enum tree_code subcode = resultcode;
2504 tree int_type = TREE_TYPE (intop);
2505 if (TREE_CODE (intop) == MINUS_EXPR)
2506 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2507 /* Convert both subexpression types to the type of intop,
2508 because weird cases involving pointer arithmetic
2509 can result in a sum or difference with different type args. */
2510 ptrop = build_binary_op (subcode, ptrop,
2511 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2512 intop = convert (int_type, TREE_OPERAND (intop, 0));
2515 /* Convert the integer argument to a type the same size as a pointer
2516 so the multiply won't overflow spuriously. */
2518 if (TYPE_PRECISION (TREE_TYPE (intop)) != POINTER_SIZE)
2519 intop = convert (type_for_size (POINTER_SIZE, 0), intop);
2521 /* Replace the integer argument with a suitable product by the object size.
2522 Do this multiplication as signed, then convert to the appropriate
2523 pointer type (actually unsigned integral). */
2525 intop = convert (result_type,
2526 build_binary_op (MULT_EXPR, intop,
2527 convert (TREE_TYPE (intop), size_exp), 1));
2529 /* Create the sum or difference. */
2531 result = build (resultcode, result_type, ptrop, intop);
2533 folded = fold (result);
2534 if (folded == result)
2535 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2539 /* Return a tree for the difference of pointers OP0 and OP1.
2540 The resulting tree has type int. */
2543 pointer_diff (op0, op1)
2544 register tree op0, op1;
2546 register tree result, folded;
2547 tree restype = ptrdiff_type_node;
2549 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2551 if (pedantic || warn_pointer_arith)
2553 if (TREE_CODE (target_type) == VOID_TYPE)
2554 pedwarn ("pointer of type `void *' used in subtraction");
2555 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2556 pedwarn ("pointer to a function used in subtraction");
2559 /* First do the subtraction as integers;
2560 then drop through to build the divide operator. */
2562 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2563 convert (restype, op1), 1);
2564 /* This generates an error if op1 is pointer to incomplete type. */
2565 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2566 error ("arithmetic on pointer to an incomplete type");
2568 /* This generates an error if op0 is pointer to incomplete type. */
2569 op1 = c_size_in_bytes (target_type);
2571 /* Divide by the size, in easiest possible way. */
2573 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2575 folded = fold (result);
2576 if (folded == result)
2577 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2581 /* Construct and perhaps optimize a tree representation
2582 for a unary operation. CODE, a tree_code, specifies the operation
2583 and XARG is the operand. NOCONVERT nonzero suppresses
2584 the default promotions (such as from short to int). */
2587 build_unary_op (code, xarg, noconvert)
2588 enum tree_code code;
2592 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2593 register tree arg = xarg;
2594 register tree argtype = 0;
2595 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2596 char *errstring = NULL;
2599 if (typecode == ERROR_MARK)
2600 return error_mark_node;
2601 if (typecode == ENUMERAL_TYPE)
2602 typecode = INTEGER_TYPE;
2607 /* This is used for unary plus, because a CONVERT_EXPR
2608 is enough to prevent anybody from looking inside for
2609 associativity, but won't generate any code. */
2610 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2611 || typecode == COMPLEX_TYPE))
2612 errstring = "wrong type argument to unary plus";
2613 else if (!noconvert)
2614 arg = default_conversion (arg);
2618 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2619 || typecode == COMPLEX_TYPE))
2620 errstring = "wrong type argument to unary minus";
2621 else if (!noconvert)
2622 arg = default_conversion (arg);
2626 if (typecode == COMPLEX_TYPE)
2630 arg = default_conversion (arg);
2632 else if (typecode != INTEGER_TYPE)
2633 errstring = "wrong type argument to bit-complement";
2634 else if (!noconvert)
2635 arg = default_conversion (arg);
2639 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2640 || typecode == COMPLEX_TYPE))
2641 errstring = "wrong type argument to abs";
2642 else if (!noconvert)
2643 arg = default_conversion (arg);
2647 /* Conjugating a real value is a no-op, but allow it anyway. */
2648 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2649 || typecode == COMPLEX_TYPE))
2650 errstring = "wrong type argument to conjugation";
2651 else if (!noconvert)
2652 arg = default_conversion (arg);
2655 case TRUTH_NOT_EXPR:
2656 if (typecode != INTEGER_TYPE
2657 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2658 && typecode != COMPLEX_TYPE
2659 /* These will convert to a pointer. */
2660 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2662 errstring = "wrong type argument to unary exclamation mark";
2665 arg = truthvalue_conversion (arg);
2666 return invert_truthvalue (arg);
2672 if (TREE_CODE (arg) == COMPLEX_CST)
2673 return TREE_REALPART (arg);
2674 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2675 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2680 if (TREE_CODE (arg) == COMPLEX_CST)
2681 return TREE_IMAGPART (arg);
2682 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2683 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2685 return convert (TREE_TYPE (arg), integer_zero_node);
2687 case PREINCREMENT_EXPR:
2688 case POSTINCREMENT_EXPR:
2689 case PREDECREMENT_EXPR:
2690 case POSTDECREMENT_EXPR:
2691 /* Handle complex lvalues (when permitted)
2692 by reduction to simpler cases. */
2694 val = unary_complex_lvalue (code, arg);
2698 /* Increment or decrement the real part of the value,
2699 and don't change the imaginary part. */
2700 if (typecode == COMPLEX_TYPE)
2704 arg = stabilize_reference (arg);
2705 real = build_unary_op (REALPART_EXPR, arg, 1);
2706 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2707 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2708 build_unary_op (code, real, 1), imag);
2711 /* Report invalid types. */
2713 if (typecode != POINTER_TYPE
2714 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2716 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2717 errstring ="wrong type argument to increment";
2719 errstring ="wrong type argument to decrement";
2725 tree result_type = TREE_TYPE (arg);
2727 arg = get_unwidened (arg, 0);
2728 argtype = TREE_TYPE (arg);
2730 /* Compute the increment. */
2732 if (typecode == POINTER_TYPE)
2734 /* If pointer target is an undefined struct,
2735 we just cannot know how to do the arithmetic. */
2736 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2737 error ("%s of pointer to unknown structure",
2738 ((code == PREINCREMENT_EXPR
2739 || code == POSTINCREMENT_EXPR)
2740 ? "increment" : "decrement"));
2741 else if ((pedantic || warn_pointer_arith)
2742 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2743 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2744 pedwarn ("wrong type argument to %s",
2745 ((code == PREINCREMENT_EXPR
2746 || code == POSTINCREMENT_EXPR)
2747 ? "increment" : "decrement"));
2748 inc = c_sizeof_nowarn (TREE_TYPE (result_type));
2751 inc = integer_one_node;
2753 inc = convert (argtype, inc);
2755 /* Handle incrementing a cast-expression. */
2758 switch (TREE_CODE (arg))
2763 case FIX_TRUNC_EXPR:
2764 case FIX_FLOOR_EXPR:
2765 case FIX_ROUND_EXPR:
2767 pedantic_lvalue_warning (CONVERT_EXPR);
2768 /* If the real type has the same machine representation
2769 as the type it is cast to, we can make better output
2770 by adding directly to the inside of the cast. */
2771 if ((TREE_CODE (TREE_TYPE (arg))
2772 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2773 && (TYPE_MODE (TREE_TYPE (arg))
2774 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2775 arg = TREE_OPERAND (arg, 0);
2778 tree incremented, modify, value;
2779 arg = stabilize_reference (arg);
2780 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2783 value = save_expr (arg);
2784 incremented = build (((code == PREINCREMENT_EXPR
2785 || code == POSTINCREMENT_EXPR)
2786 ? PLUS_EXPR : MINUS_EXPR),
2787 argtype, value, inc);
2788 TREE_SIDE_EFFECTS (incremented) = 1;
2789 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2790 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2791 TREE_USED (value) = 1;
2801 /* Complain about anything else that is not a true lvalue. */
2802 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2803 || code == POSTINCREMENT_EXPR)
2804 ? "increment" : "decrement")))
2805 return error_mark_node;
2807 /* Report a read-only lvalue. */
2808 if (TREE_READONLY (arg))
2809 readonly_warning (arg,
2810 ((code == PREINCREMENT_EXPR
2811 || code == POSTINCREMENT_EXPR)
2812 ? "increment" : "decrement"));
2814 val = build (code, TREE_TYPE (arg), arg, inc);
2815 TREE_SIDE_EFFECTS (val) = 1;
2816 val = convert (result_type, val);
2817 if (TREE_CODE (val) != code)
2818 TREE_NO_UNUSED_WARNING (val) = 1;
2823 /* Note that this operation never does default_conversion
2824 regardless of NOCONVERT. */
2826 /* Let &* cancel out to simplify resulting code. */
2827 if (TREE_CODE (arg) == INDIRECT_REF)
2829 /* Don't let this be an lvalue. */
2830 if (lvalue_p (TREE_OPERAND (arg, 0)))
2831 return non_lvalue (TREE_OPERAND (arg, 0));
2832 return TREE_OPERAND (arg, 0);
2835 /* For &x[y], return x+y */
2836 if (TREE_CODE (arg) == ARRAY_REF)
2838 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
2839 return error_mark_node;
2840 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2841 TREE_OPERAND (arg, 1), 1);
2844 /* Handle complex lvalues (when permitted)
2845 by reduction to simpler cases. */
2846 val = unary_complex_lvalue (code, arg);
2850 #if 0 /* Turned off because inconsistent;
2851 float f; *&(int)f = 3.4 stores in int format
2852 whereas (int)f = 3.4 stores in float format. */
2853 /* Address of a cast is just a cast of the address
2854 of the operand of the cast. */
2855 switch (TREE_CODE (arg))
2860 case FIX_TRUNC_EXPR:
2861 case FIX_FLOOR_EXPR:
2862 case FIX_ROUND_EXPR:
2865 pedwarn ("ANSI C forbids the address of a cast expression");
2866 return convert (build_pointer_type (TREE_TYPE (arg)),
2867 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
2872 /* Allow the address of a constructor if all the elements
2874 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
2876 /* Anything not already handled and not a true memory reference
2878 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
2879 return error_mark_node;
2881 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2882 argtype = TREE_TYPE (arg);
2883 /* If the lvalue is const or volatile,
2884 merge that into the type that the address will point to. */
2885 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
2886 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2888 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
2889 argtype = c_build_type_variant (argtype,
2890 TREE_READONLY (arg),
2891 TREE_THIS_VOLATILE (arg));
2894 argtype = build_pointer_type (argtype);
2896 if (mark_addressable (arg) == 0)
2897 return error_mark_node;
2902 if (TREE_CODE (arg) == COMPONENT_REF)
2904 tree field = TREE_OPERAND (arg, 1);
2906 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
2908 if (DECL_BIT_FIELD (field))
2910 error ("attempt to take address of bit-field structure member `%s'",
2911 IDENTIFIER_POINTER (DECL_NAME (field)));
2912 return error_mark_node;
2915 addr = convert (argtype, addr);
2917 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
2920 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
2921 size_int (BITS_PER_UNIT));
2922 int flag = TREE_CONSTANT (addr);
2923 addr = fold (build (PLUS_EXPR, argtype,
2924 addr, convert (argtype, offset)));
2925 TREE_CONSTANT (addr) = flag;
2929 addr = build1 (code, argtype, arg);
2931 /* Address of a static or external variable or
2932 file-scope function counts as a constant. */
2934 && ! (TREE_CODE (arg) == FUNCTION_DECL
2935 && DECL_CONTEXT (arg) != 0))
2936 TREE_CONSTANT (addr) = 1;
2944 argtype = TREE_TYPE (arg);
2945 return fold (build1 (code, argtype, arg));
2949 return error_mark_node;
2953 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
2954 convert ARG with the same conversions in the same order
2955 and return the result. */
2958 convert_sequence (conversions, arg)
2962 switch (TREE_CODE (conversions))
2967 case FIX_TRUNC_EXPR:
2968 case FIX_FLOOR_EXPR:
2969 case FIX_ROUND_EXPR:
2971 return convert (TREE_TYPE (conversions),
2972 convert_sequence (TREE_OPERAND (conversions, 0),
2981 /* Return nonzero if REF is an lvalue valid for this language.
2982 Lvalues can be assigned, unless their type has TYPE_READONLY.
2983 Lvalues can have their address taken, unless they have DECL_REGISTER. */
2989 register enum tree_code code = TREE_CODE (ref);
2996 return lvalue_p (TREE_OPERAND (ref, 0));
3007 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3008 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
3015 /* Return nonzero if REF is an lvalue valid for this language;
3016 otherwise, print an error message and return zero. */
3019 lvalue_or_else (ref, string)
3023 int win = lvalue_p (ref);
3025 error ("invalid lvalue in %s", string);
3029 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3030 for certain kinds of expressions which are not really lvalues
3031 but which we can accept as lvalues.
3033 If ARG is not a kind of expression we can handle, return zero. */
3036 unary_complex_lvalue (code, arg)
3037 enum tree_code code;
3040 /* Handle (a, b) used as an "lvalue". */
3041 if (TREE_CODE (arg) == COMPOUND_EXPR)
3043 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3044 pedantic_lvalue_warning (COMPOUND_EXPR);
3045 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3046 TREE_OPERAND (arg, 0), real_result);
3049 /* Handle (a ? b : c) used as an "lvalue". */
3050 if (TREE_CODE (arg) == COND_EXPR)
3052 pedantic_lvalue_warning (COND_EXPR);
3053 return (build_conditional_expr
3054 (TREE_OPERAND (arg, 0),
3055 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3056 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3062 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3063 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3066 pedantic_lvalue_warning (code)
3067 enum tree_code code;
3070 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3071 code == COND_EXPR ? "conditional"
3072 : code == COMPOUND_EXPR ? "compound" : "cast");
3075 /* Warn about storing in something that is `const'. */
3078 readonly_warning (arg, string)
3083 strcpy (buf, string);
3085 /* Forbid assignments to iterators. */
3086 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3088 strcat (buf, " of iterator `%s'");
3089 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3092 if (TREE_CODE (arg) == COMPONENT_REF)
3094 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3095 readonly_warning (TREE_OPERAND (arg, 0), string);
3098 strcat (buf, " of read-only member `%s'");
3099 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3102 else if (TREE_CODE (arg) == VAR_DECL)
3104 strcat (buf, " of read-only variable `%s'");
3105 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3109 pedwarn ("%s of read-only location", buf);
3113 /* Mark EXP saying that we need to be able to take the
3114 address of it; it should not be allocated in a register.
3115 Value is 1 if successful. */
3118 mark_addressable (exp)
3121 register tree x = exp;
3123 switch (TREE_CODE (x))
3130 x = TREE_OPERAND (x, 0);
3134 TREE_ADDRESSABLE (x) = 1;
3141 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3142 && DECL_NONLOCAL (x))
3144 if (TREE_PUBLIC (x))
3146 error ("global register variable `%s' used in nested function",
3147 IDENTIFIER_POINTER (DECL_NAME (x)));
3150 pedwarn ("register variable `%s' used in nested function",
3151 IDENTIFIER_POINTER (DECL_NAME (x)));
3153 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3155 if (TREE_PUBLIC (x))
3157 error ("address of global register variable `%s' requested",
3158 IDENTIFIER_POINTER (DECL_NAME (x)));
3161 pedwarn ("address of register variable `%s' requested",
3162 IDENTIFIER_POINTER (DECL_NAME (x)));
3164 put_var_into_stack (x);
3168 TREE_ADDRESSABLE (x) = 1;
3169 #if 0 /* poplevel deals with this now. */
3170 if (DECL_CONTEXT (x) == 0)
3171 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3179 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3182 build_conditional_expr (ifexp, op1, op2)
3183 tree ifexp, op1, op2;
3185 register tree type1;
3186 register tree type2;
3187 register enum tree_code code1;
3188 register enum tree_code code2;
3189 register tree result_type = NULL;
3190 tree orig_op1 = op1, orig_op2 = op2;
3192 /* If second operand is omitted, it is the same as the first one;
3193 make sure it is calculated only once. */
3197 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression");
3198 ifexp = op1 = save_expr (ifexp);
3201 ifexp = truthvalue_conversion (default_conversion (ifexp));
3203 #if 0 /* Produces wrong result if within sizeof. */
3204 /* Don't promote the operands separately if they promote
3205 the same way. Return the unpromoted type and let the combined
3206 value get promoted if necessary. */
3208 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3209 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3210 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3211 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3213 if (TREE_CODE (ifexp) == INTEGER_CST)
3214 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3216 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3220 /* Promote both alternatives. */
3222 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3223 op1 = default_conversion (op1);
3224 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3225 op2 = default_conversion (op2);
3227 if (TREE_CODE (ifexp) == ERROR_MARK
3228 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3229 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3230 return error_mark_node;
3232 type1 = TREE_TYPE (op1);
3233 code1 = TREE_CODE (type1);
3234 type2 = TREE_TYPE (op2);
3235 code2 = TREE_CODE (type2);
3237 /* Quickly detect the usual case where op1 and op2 have the same type
3239 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3242 result_type = type1;
3244 result_type = TYPE_MAIN_VARIANT (type1);
3246 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3247 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3249 result_type = common_type (type1, type2);
3251 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3253 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3254 pedwarn ("ANSI C forbids conditional expr with only one void side");
3255 result_type = void_type_node;
3257 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3259 if (comp_target_types (type1, type2))
3260 result_type = common_type (type1, type2);
3261 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3262 && TREE_CODE (orig_op1) != NOP_EXPR)
3263 result_type = qualify_type (type2, type1);
3264 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3265 && TREE_CODE (orig_op2) != NOP_EXPR)
3266 result_type = qualify_type (type1, type2);
3267 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3269 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3270 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3271 result_type = qualify_type (type1, type2);
3273 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3275 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3276 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3277 result_type = qualify_type (type2, type1);
3281 pedwarn ("pointer type mismatch in conditional expression");
3282 result_type = build_pointer_type (void_type_node);
3285 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3287 if (! integer_zerop (op2))
3288 pedwarn ("pointer/integer type mismatch in conditional expression");
3291 op2 = null_pointer_node;
3292 #if 0 /* The spec seems to say this is permitted. */
3293 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3294 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3297 result_type = type1;
3299 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3301 if (!integer_zerop (op1))
3302 pedwarn ("pointer/integer type mismatch in conditional expression");
3305 op1 = null_pointer_node;
3306 #if 0 /* The spec seems to say this is permitted. */
3307 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3308 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3311 result_type = type2;
3316 if (flag_cond_mismatch)
3317 result_type = void_type_node;
3320 error ("type mismatch in conditional expression");
3321 return error_mark_node;
3325 /* Merge const and volatile flags of the incoming types. */
3327 = build_type_variant (result_type,
3328 TREE_READONLY (op1) || TREE_READONLY (op2),
3329 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3331 if (result_type != TREE_TYPE (op1))
3332 op1 = convert_and_check (result_type, op1);
3333 if (result_type != TREE_TYPE (op2))
3334 op2 = convert_and_check (result_type, op2);
3337 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3339 result_type = TREE_TYPE (op1);
3340 if (TREE_CONSTANT (ifexp))
3341 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3343 if (TYPE_MODE (result_type) == BLKmode)
3345 register tree tempvar
3346 = build_decl (VAR_DECL, NULL_TREE, result_type);
3347 register tree xop1 = build_modify_expr (tempvar, op1);
3348 register tree xop2 = build_modify_expr (tempvar, op2);
3349 register tree result = fold (build (COND_EXPR, result_type,
3350 ifexp, xop1, xop2));
3352 layout_decl (tempvar, TYPE_ALIGN (result_type));
3353 /* No way to handle variable-sized objects here.
3354 I fear that the entire handling of BLKmode conditional exprs
3355 needs to be redone. */
3356 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3359 = assign_stack_local (DECL_MODE (tempvar),
3360 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3361 + BITS_PER_UNIT - 1)
3365 TREE_SIDE_EFFECTS (result)
3366 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3367 | TREE_SIDE_EFFECTS (op2);
3368 return build (COMPOUND_EXPR, result_type, result, tempvar);
3373 if (TREE_CODE (ifexp) == INTEGER_CST)
3374 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3376 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3379 /* Given a list of expressions, return a compound expression
3380 that performs them all and returns the value of the last of them. */
3383 build_compound_expr (list)
3386 return internal_build_compound_expr (list, TRUE);
3390 internal_build_compound_expr (list, first_p)
3396 if (TREE_CHAIN (list) == 0)
3398 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3399 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3401 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3402 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3403 list = TREE_OPERAND (list, 0);
3406 /* Don't let (0, 0) be null pointer constant. */
3407 if (!first_p && integer_zerop (TREE_VALUE (list)))
3408 return non_lvalue (TREE_VALUE (list));
3409 return TREE_VALUE (list);
3412 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3414 /* Convert arrays to pointers when there really is a comma operator. */
3415 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3416 TREE_VALUE (TREE_CHAIN (list))
3417 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3420 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3422 /* When pedantic, a compound expression can be neither an lvalue
3423 nor an integer constant expression. */
3424 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
3427 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3430 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3433 build_c_cast (type, expr)
3437 register tree value = expr;
3439 if (type == error_mark_node || expr == error_mark_node)
3440 return error_mark_node;
3441 type = TYPE_MAIN_VARIANT (type);
3444 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3445 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3446 value = TREE_OPERAND (value, 0);
3449 if (TREE_CODE (type) == ARRAY_TYPE)
3451 error ("cast specifies array type");
3452 return error_mark_node;
3455 if (TREE_CODE (type) == FUNCTION_TYPE)
3457 error ("cast specifies function type");
3458 return error_mark_node;
3461 if (type == TREE_TYPE (value))
3465 if (TREE_CODE (type) == RECORD_TYPE
3466 || TREE_CODE (type) == UNION_TYPE)
3467 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3470 else if (TREE_CODE (type) == UNION_TYPE)
3473 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3474 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3475 value = default_conversion (value);
3477 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3478 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3479 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3488 pedwarn ("ANSI C forbids casts to union type");
3489 if (TYPE_NAME (type) != 0)
3491 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3492 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3494 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3498 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3499 build_tree_list (field, value)),
3501 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3504 error ("cast to union type from type not present in union");
3505 return error_mark_node;
3511 /* If casting to void, avoid the error that would come
3512 from default_conversion in the case of a non-lvalue array. */
3513 if (type == void_type_node)
3514 return build1 (CONVERT_EXPR, type, value);
3516 /* Convert functions and arrays to pointers,
3517 but don't convert any other types. */
3518 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3519 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3520 value = default_conversion (value);
3521 otype = TREE_TYPE (value);
3523 /* Optionally warn about potentially worrisome casts. */
3526 && TREE_CODE (type) == POINTER_TYPE
3527 && TREE_CODE (otype) == POINTER_TYPE)
3529 if (TYPE_VOLATILE (TREE_TYPE (otype))
3530 && ! TYPE_VOLATILE (TREE_TYPE (type)))
3531 pedwarn ("cast discards `volatile' from pointer target type");
3532 if (TYPE_READONLY (TREE_TYPE (otype))
3533 && ! TYPE_READONLY (TREE_TYPE (type)))
3534 pedwarn ("cast discards `const' from pointer target type");
3537 /* Warn about possible alignment problems. */
3538 if (STRICT_ALIGNMENT && warn_cast_align
3539 && TREE_CODE (type) == POINTER_TYPE
3540 && TREE_CODE (otype) == POINTER_TYPE
3541 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3542 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3543 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3544 warning ("cast increases required alignment of target type");
3546 if (TREE_CODE (type) == INTEGER_TYPE
3547 && TREE_CODE (otype) == POINTER_TYPE
3548 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3549 && !TREE_CONSTANT (value))
3550 warning ("cast from pointer to integer of different size");
3552 if (TREE_CODE (type) == POINTER_TYPE
3553 && TREE_CODE (otype) == INTEGER_TYPE
3554 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3556 /* Don't warn about converting 0 to pointer,
3557 provided the 0 was explicit--not cast or made by folding. */
3558 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3560 /* Don't warn about converting any constant. */
3561 && !TREE_CONSTANT (value))
3562 warning ("cast to pointer from integer of different size");
3565 value = convert (type, value);
3567 /* Ignore any integer overflow caused by the cast. */
3568 if (TREE_CODE (value) == INTEGER_CST)
3570 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3571 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3575 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3576 if (pedantic && TREE_CODE (value) == INTEGER_CST
3577 && TREE_CODE (expr) == INTEGER_CST
3578 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3579 value = non_lvalue (value);
3581 /* If pedantic, don't let a cast be an lvalue. */
3582 if (value == expr && pedantic)
3583 value = non_lvalue (value);
3588 /* Build an assignment expression of lvalue LHS from value RHS.
3589 MODIFYCODE is the code for a binary operator that we use
3590 to combine the old value of LHS with RHS to get the new value.
3591 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3594 build_modify_expr (lhs, modifycode, rhs)
3596 enum tree_code modifycode;
3598 register tree result;
3600 tree lhstype = TREE_TYPE (lhs);
3601 tree olhstype = lhstype;
3603 /* Types that aren't fully specified cannot be used in assignments. */
3604 lhs = require_complete_type (lhs);
3606 /* Avoid duplicate error messages from operands that had errors. */
3607 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3608 return error_mark_node;
3610 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3611 /* Do not use STRIP_NOPS here. We do not want an enumerator
3612 whose value is 0 to count as a null pointer constant. */
3613 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3614 rhs = TREE_OPERAND (rhs, 0);
3618 /* Handle control structure constructs used as "lvalues". */
3620 switch (TREE_CODE (lhs))
3622 /* Handle (a, b) used as an "lvalue". */
3624 pedantic_lvalue_warning (COMPOUND_EXPR);
3625 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3627 if (TREE_CODE (newrhs) == ERROR_MARK)
3628 return error_mark_node;
3629 return build (COMPOUND_EXPR, lhstype,
3630 TREE_OPERAND (lhs, 0), newrhs);
3632 /* Handle (a ? b : c) used as an "lvalue". */
3634 pedantic_lvalue_warning (COND_EXPR);
3635 rhs = save_expr (rhs);
3637 /* Produce (a ? (b = rhs) : (c = rhs))
3638 except that the RHS goes through a save-expr
3639 so the code to compute it is only emitted once. */
3641 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3642 build_modify_expr (TREE_OPERAND (lhs, 1),
3644 build_modify_expr (TREE_OPERAND (lhs, 2),
3646 if (TREE_CODE (cond) == ERROR_MARK)
3648 /* Make sure the code to compute the rhs comes out
3649 before the split. */
3650 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3651 /* But cast it to void to avoid an "unused" error. */
3652 convert (void_type_node, rhs), cond);
3656 /* If a binary op has been requested, combine the old LHS value with the RHS
3657 producing the value we should actually store into the LHS. */
3659 if (modifycode != NOP_EXPR)
3661 lhs = stabilize_reference (lhs);
3662 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3665 /* Handle a cast used as an "lvalue".
3666 We have already performed any binary operator using the value as cast.
3667 Now convert the result to the cast type of the lhs,
3668 and then true type of the lhs and store it there;
3669 then convert result back to the cast type to be the value
3670 of the assignment. */
3672 switch (TREE_CODE (lhs))
3677 case FIX_TRUNC_EXPR:
3678 case FIX_FLOOR_EXPR:
3679 case FIX_ROUND_EXPR:
3681 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3682 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3683 newrhs = default_conversion (newrhs);
3685 tree inner_lhs = TREE_OPERAND (lhs, 0);
3687 result = build_modify_expr (inner_lhs, NOP_EXPR,
3688 convert (TREE_TYPE (inner_lhs),
3689 convert (lhstype, newrhs)));
3690 if (TREE_CODE (result) == ERROR_MARK)
3692 pedantic_lvalue_warning (CONVERT_EXPR);
3693 return convert (TREE_TYPE (lhs), result);
3697 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3698 Reject anything strange now. */
3700 if (!lvalue_or_else (lhs, "assignment"))
3701 return error_mark_node;
3703 /* Warn about storing in something that is `const'. */
3705 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3706 || ((TREE_CODE (lhstype) == RECORD_TYPE
3707 || TREE_CODE (lhstype) == UNION_TYPE)
3708 && C_TYPE_FIELDS_READONLY (lhstype)))
3709 readonly_warning (lhs, "assignment");
3711 /* If storing into a structure or union member,
3712 it has probably been given type `int'.
3713 Compute the type that would go with
3714 the actual amount of storage the member occupies. */
3716 if (TREE_CODE (lhs) == COMPONENT_REF
3717 && (TREE_CODE (lhstype) == INTEGER_TYPE
3718 || TREE_CODE (lhstype) == REAL_TYPE
3719 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3720 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3722 /* If storing in a field that is in actuality a short or narrower than one,
3723 we must store in the field in its actual type. */
3725 if (lhstype != TREE_TYPE (lhs))
3727 lhs = copy_node (lhs);
3728 TREE_TYPE (lhs) = lhstype;
3731 /* Convert new value to destination type. */
3733 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
3734 NULL_TREE, NULL_TREE, 0);
3735 if (TREE_CODE (newrhs) == ERROR_MARK)
3736 return error_mark_node;
3738 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3739 TREE_SIDE_EFFECTS (result) = 1;
3741 /* If we got the LHS in a different type for storing in,
3742 convert the result back to the nominal type of LHS
3743 so that the value we return always has the same type
3744 as the LHS argument. */
3746 if (olhstype == TREE_TYPE (result))
3748 return convert_for_assignment (olhstype, result, "assignment",
3749 NULL_TREE, NULL_TREE, 0);
3752 /* Convert value RHS to type TYPE as preparation for an assignment
3753 to an lvalue of type TYPE.
3754 The real work of conversion is done by `convert'.
3755 The purpose of this function is to generate error messages
3756 for assignments that are not allowed in C.
3757 ERRTYPE is a string to use in error messages:
3758 "assignment", "return", etc. If it is null, this is parameter passing
3759 for a function call (and different error messages are output). Otherwise,
3760 it may be a name stored in the spelling stack and interpreted by
3763 FUNNAME is the name of the function being called,
3764 as an IDENTIFIER_NODE, or null.
3765 PARMNUM is the number of the argument, for printing in error messages. */
3768 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3771 tree fundecl, funname;
3774 register enum tree_code codel = TREE_CODE (type);
3775 register tree rhstype;
3776 register enum tree_code coder;
3778 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3779 /* Do not use STRIP_NOPS here. We do not want an enumerator
3780 whose value is 0 to count as a null pointer constant. */
3781 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3782 rhs = TREE_OPERAND (rhs, 0);
3784 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3785 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3786 rhs = default_conversion (rhs);
3788 rhstype = TREE_TYPE (rhs);
3789 coder = TREE_CODE (rhstype);
3791 if (coder == ERROR_MARK)
3792 return error_mark_node;
3794 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3796 overflow_warning (rhs);
3797 /* Check for Objective-C protocols. This will issue a warning if
3798 there are protocol violations. No need to use the return value. */
3799 maybe_objc_comptypes (type, rhstype, 0);
3803 if (coder == VOID_TYPE)
3805 error ("void value not ignored as it ought to be");
3806 return error_mark_node;
3808 /* Arithmetic types all interconvert, and enum is treated like int. */
3809 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
3810 || codel == COMPLEX_TYPE)
3812 (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
3813 || coder == COMPLEX_TYPE))
3814 return convert_and_check (type, rhs);
3815 /* Conversion to a union from its member types. */
3816 else if (codel == UNION_TYPE)
3819 for (memb_types = TYPE_FIELDS (type); memb_types;
3820 memb_types = TREE_CHAIN (memb_types))
3822 if (comptypes (TREE_TYPE (memb_types), TREE_TYPE (rhs)))
3825 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
3826 pedwarn ("ANSI C prohibits argument conversion to union type");
3827 return build1 (NOP_EXPR, type, rhs);
3829 else if (coder == POINTER_TYPE
3830 && TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE)
3832 tree memb_type = TREE_TYPE (memb_types);
3833 register tree ttl = TREE_TYPE (memb_type);
3834 register tree ttr = TREE_TYPE (rhstype);
3836 /* Any non-function converts to a [const][volatile] void *
3837 and vice versa; otherwise, targets must be the same.
3838 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3839 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3840 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3841 || comp_target_types (memb_type, rhstype))
3843 /* Const and volatile mean something different for function types,
3844 so the usual warnings are not appropriate. */
3845 if (TREE_CODE (ttr) != FUNCTION_TYPE
3846 || TREE_CODE (ttl) != FUNCTION_TYPE)
3848 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
3849 warn_for_assignment ("%s discards `const' from pointer target type",
3850 get_spelling (errtype), funname, parmnum);
3851 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
3852 warn_for_assignment ("%s discards `volatile' from pointer target type",
3853 get_spelling (errtype), funname, parmnum);
3857 /* Because const and volatile on functions are restrictions
3858 that say the function will not do certain things,
3859 it is okay to use a const or volatile function
3860 where an ordinary one is wanted, but not vice-versa. */
3861 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
3862 warn_for_assignment ("%s makes `const *' function pointer from non-const",
3863 get_spelling (errtype), funname, parmnum);
3864 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
3865 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
3866 get_spelling (errtype), funname, parmnum);
3869 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
3870 pedwarn ("ANSI C prohibits argument conversion to union type");
3871 return build1 (NOP_EXPR, type, rhs);
3876 /* Conversions among pointers */
3877 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
3879 register tree ttl = TREE_TYPE (type);
3880 register tree ttr = TREE_TYPE (rhstype);
3882 /* Any non-function converts to a [const][volatile] void *
3883 and vice versa; otherwise, targets must be the same.
3884 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3885 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3886 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3887 || comp_target_types (type, rhstype)
3888 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
3889 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3892 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
3893 && TREE_CODE (ttr) == FUNCTION_TYPE)
3895 (TYPE_MAIN_VARIANT (ttr) == void_type_node
3896 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3897 which are not ANSI null ptr constants. */
3898 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3899 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3900 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
3901 get_spelling (errtype), funname, parmnum);
3902 /* Const and volatile mean something different for function types,
3903 so the usual warnings are not appropriate. */
3904 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3905 || TREE_CODE (ttl) != FUNCTION_TYPE)
3907 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
3908 warn_for_assignment ("%s discards `const' from pointer target type",
3909 get_spelling (errtype), funname, parmnum);
3910 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
3911 warn_for_assignment ("%s discards `volatile' from pointer target type",
3912 get_spelling (errtype), funname, parmnum);
3913 /* If this is not a case of ignoring a mismatch in signedness,
3915 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3916 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3917 || comp_target_types (type, rhstype))
3919 /* If there is a mismatch, do warn. */
3921 warn_for_assignment ("pointer targets in %s differ in signedness",
3922 get_spelling (errtype), funname, parmnum);
3926 /* Because const and volatile on functions are restrictions
3927 that say the function will not do certain things,
3928 it is okay to use a const or volatile function
3929 where an ordinary one is wanted, but not vice-versa. */
3930 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
3931 warn_for_assignment ("%s makes `const *' function pointer from non-const",
3932 get_spelling (errtype), funname, parmnum);
3933 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
3934 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
3935 get_spelling (errtype), funname, parmnum);
3939 warn_for_assignment ("%s from incompatible pointer type",
3940 get_spelling (errtype), funname, parmnum);
3941 return convert (type, rhs);
3943 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3945 /* An explicit constant 0 can convert to a pointer,
3946 or one that results from arithmetic, even including
3947 a cast to integer type. */
3948 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3950 ! (TREE_CODE (rhs) == NOP_EXPR
3951 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3952 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3953 && integer_zerop (TREE_OPERAND (rhs, 0))))
3955 warn_for_assignment ("%s makes pointer from integer without a cast",
3956 get_spelling (errtype), funname, parmnum);
3957 return convert (type, rhs);
3959 return null_pointer_node;
3961 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3963 warn_for_assignment ("%s makes integer from pointer without a cast",
3964 get_spelling (errtype), funname, parmnum);
3965 return convert (type, rhs);
3972 tree selector = maybe_building_objc_message_expr ();
3974 if (selector && parmnum > 2)
3975 error ("incompatible type for argument %d of `%s'",
3976 parmnum - 2, IDENTIFIER_POINTER (selector));
3978 error ("incompatible type for argument %d of `%s'",
3979 parmnum, IDENTIFIER_POINTER (funname));
3982 error ("incompatible type for argument %d of indirect function call",
3986 error ("incompatible types in %s", get_spelling (errtype));
3988 return error_mark_node;
3991 /* Print a warning using MSG.
3992 It gets OPNAME as its one parameter.
3993 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3994 FUNCTION and ARGNUM are handled specially if we are building an
3995 Objective-C selector. */
3998 warn_for_assignment (msg, opname, function, argnum)
4004 static char argstring[] = "passing arg %d of `%s'";
4005 static char argnofun[] = "passing arg %d";
4009 tree selector = maybe_building_objc_message_expr ();
4011 if (selector && argnum > 2)
4013 function = selector;
4018 /* Function name is known; supply it. */
4019 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4020 + sizeof (argstring) + 25 /*%d*/ + 1);
4021 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4025 /* Function name unknown (call through ptr); just give arg number. */
4026 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4027 sprintf (opname, argnofun, argnum);
4030 pedwarn (msg, opname);
4033 /* Return nonzero if VALUE is a valid constant-valued expression
4034 for use in initializing a static variable; one that can be an
4035 element of a "constant" initializer.
4037 Return null_pointer_node if the value is absolute;
4038 if it is relocatable, return the variable that determines the relocation.
4039 We assume that VALUE has been folded as much as possible;
4040 therefore, we do not need to check for such things as
4041 arithmetic-combinations of integers. */
4044 initializer_constant_valid_p (value, endtype)
4048 switch (TREE_CODE (value))
4051 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4052 && TREE_CONSTANT (value))
4054 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
4057 return TREE_STATIC (value) ? null_pointer_node : 0;
4063 return null_pointer_node;
4066 return TREE_OPERAND (value, 0);
4068 case NON_LVALUE_EXPR:
4069 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4073 /* Allow conversions between pointer types. */
4074 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4075 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
4076 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4077 /* Allow conversions between real types. */
4078 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4079 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
4080 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4081 /* Allow length-preserving conversions between integer types. */
4082 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4083 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4084 && tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (value)),
4085 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value, 0)))))
4086 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4087 /* Allow conversions between integer types only if explicit value. */
4088 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4089 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4091 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4093 if (inner == null_pointer_node)
4094 return null_pointer_node;
4097 /* Allow (int) &foo provided int is as wide as a pointer. */
4098 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4099 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
4100 && ! tree_int_cst_lt (TYPE_SIZE (TREE_TYPE (value)),
4101 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value, 0)))))
4102 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4104 /* Allow conversions to union types if the value inside is okay. */
4105 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4106 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4111 if (TREE_CODE (endtype) == INTEGER_TYPE
4112 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4115 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4117 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4119 /* If either term is absolute, use the other terms relocation. */
4120 if (valid0 == null_pointer_node)
4122 if (valid1 == null_pointer_node)
4128 if (TREE_CODE (endtype) == INTEGER_TYPE
4129 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4132 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4134 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4136 /* Win if second argument is absolute. */
4137 if (valid1 == null_pointer_node)
4139 /* Win if both arguments have the same relocation.
4140 Then the value is absolute. */
4141 if (valid0 == valid1)
4142 return null_pointer_node;
4150 /* If VALUE is a compound expr all of whose expressions are constant, then
4151 return its value. Otherwise, return error_mark_node.
4153 This is for handling COMPOUND_EXPRs as initializer elements
4154 which is allowed with a warning when -pedantic is specified. */
4157 valid_compound_expr_initializer (value, endtype)
4161 if (TREE_CODE (value) == COMPOUND_EXPR)
4163 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4165 return error_mark_node;
4166 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4169 else if (! TREE_CONSTANT (value)
4170 && ! initializer_constant_valid_p (value, endtype))
4171 return error_mark_node;
4176 /* Perform appropriate conversions on the initial value of a variable,
4177 store it in the declaration DECL,
4178 and print any error messages that are appropriate.
4179 If the init is invalid, store an ERROR_MARK. */
4182 store_init_value (decl, init)
4185 register tree value, type;
4187 /* If variable's type was invalidly declared, just ignore it. */
4189 type = TREE_TYPE (decl);
4190 if (TREE_CODE (type) == ERROR_MARK)
4193 /* Digest the specified initializer into an expression. */
4195 value = digest_init (type, init, TREE_STATIC (decl),
4196 TREE_STATIC (decl) || pedantic);
4198 /* Store the expression if valid; else report error. */
4201 /* Note that this is the only place we can detect the error
4202 in a case such as struct foo bar = (struct foo) { x, y };
4203 where there is one initial value which is a constructor expression. */
4204 if (value == error_mark_node)
4206 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4208 error ("initializer for static variable is not constant");
4209 value = error_mark_node;
4211 else if (TREE_STATIC (decl)
4212 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4214 error ("initializer for static variable uses complicated arithmetic");
4215 value = error_mark_node;
4219 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4221 if (! TREE_CONSTANT (value))
4222 pedwarn ("aggregate initializer is not constant");
4223 else if (! TREE_STATIC (value))
4224 pedwarn ("aggregate initializer uses complicated arithmetic");
4229 DECL_INITIAL (decl) = value;
4231 /* ANSI wants warnings about out-of-range constant initializers. */
4232 STRIP_TYPE_NOPS (value);
4233 constant_expression_warning (value);
4236 /* Methods for storing and printing names for error messages. */
4238 /* Implement a spelling stack that allows components of a name to be pushed
4239 and popped. Each element on the stack is this structure. */
4251 #define SPELLING_STRING 1
4252 #define SPELLING_MEMBER 2
4253 #define SPELLING_BOUNDS 3
4255 static struct spelling *spelling; /* Next stack element (unused). */
4256 static struct spelling *spelling_base; /* Spelling stack base. */
4257 static int spelling_size; /* Size of the spelling stack. */
4259 /* Macros to save and restore the spelling stack around push_... functions.
4260 Alternative to SAVE_SPELLING_STACK. */
4262 #define SPELLING_DEPTH() (spelling - spelling_base)
4263 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4265 /* Save and restore the spelling stack around arbitrary C code. */
4267 #define SAVE_SPELLING_DEPTH(code) \
4269 int __depth = SPELLING_DEPTH (); \
4271 RESTORE_SPELLING_DEPTH (__depth); \
4274 /* Push an element on the spelling stack with type KIND and assign VALUE
4277 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4279 int depth = SPELLING_DEPTH (); \
4281 if (depth >= spelling_size) \
4283 spelling_size += 10; \
4284 if (spelling_base == 0) \
4286 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4289 = (struct spelling *) xrealloc (spelling_base, \
4290 spelling_size * sizeof (struct spelling)); \
4291 RESTORE_SPELLING_DEPTH (depth); \
4294 spelling->kind = (KIND); \
4295 spelling->MEMBER = (VALUE); \
4299 /* Push STRING on the stack. Printed literally. */
4302 push_string (string)
4305 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4308 /* Push a member name on the stack. Printed as '.' STRING. */
4311 push_member_name (decl)
4316 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4317 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4320 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4323 push_array_bounds (bounds)
4326 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4329 /* Compute the maximum size in bytes of the printed spelling. */
4334 register int size = 0;
4335 register struct spelling *p;
4337 for (p = spelling_base; p < spelling; p++)
4339 if (p->kind == SPELLING_BOUNDS)
4342 size += strlen (p->u.s) + 1;
4348 /* Print the spelling to BUFFER and return it. */
4351 print_spelling (buffer)
4352 register char *buffer;
4354 register char *d = buffer;
4356 register struct spelling *p;
4358 for (p = spelling_base; p < spelling; p++)
4359 if (p->kind == SPELLING_BOUNDS)
4361 sprintf (d, "[%d]", p->u.i);
4366 if (p->kind == SPELLING_MEMBER)
4368 for (s = p->u.s; *d = *s++; d++)
4375 /* Provide a means to pass component names derived from the spelling stack. */
4377 char initialization_message;
4379 /* Interpret the spelling of the given ERRTYPE message. */
4382 get_spelling (errtype)
4385 static char *buffer;
4386 static int size = -1;
4388 if (errtype == &initialization_message)
4390 /* Avoid counting chars */
4391 static char message[] = "initialization of `%s'";
4392 register int needed = sizeof (message) + spelling_length () + 1;
4396 buffer = (char *) xmalloc (size = needed);
4398 buffer = (char *) xrealloc (buffer, size = needed);
4400 temp = (char *) alloca (needed);
4401 sprintf (buffer, message, print_spelling (temp));
4408 /* Issue an error message for a bad initializer component.
4409 FORMAT describes the message. OFWHAT is the name for the component.
4410 LOCAL is a format string for formatting the insertion of the name
4413 If OFWHAT is null, the component name is stored on the spelling stack.
4414 If the component name is a null string, then LOCAL is omitted entirely. */
4417 error_init (format, local, ofwhat)
4418 char *format, *local, *ofwhat;
4423 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4424 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4427 sprintf (buffer, local, ofwhat);
4431 error (format, buffer);
4434 /* Issue a pedantic warning for a bad initializer component.
4435 FORMAT describes the message. OFWHAT is the name for the component.
4436 LOCAL is a format string for formatting the insertion of the name
4439 If OFWHAT is null, the component name is stored on the spelling stack.
4440 If the component name is a null string, then LOCAL is omitted entirely. */
4443 pedwarn_init (format, local, ofwhat)
4444 char *format, *local, *ofwhat;
4449 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4450 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4453 sprintf (buffer, local, ofwhat);
4457 pedwarn (format, buffer);
4460 /* Issue a warning for a bad initializer component.
4461 FORMAT describes the message. OFWHAT is the name for the component.
4462 LOCAL is a format string for formatting the insertion of the name
4465 If OFWHAT is null, the component name is stored on the spelling stack.
4466 If the component name is a null string, then LOCAL is omitted entirely. */
4469 warning_init (format, local, ofwhat)
4470 char *format, *local, *ofwhat;
4475 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4476 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4479 sprintf (buffer, local, ofwhat);
4483 warning (format, buffer);
4486 /* Digest the parser output INIT as an initializer for type TYPE.
4487 Return a C expression of type TYPE to represent the initial value.
4489 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4490 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4491 applies only to elements of constructors. */
4494 digest_init (type, init, require_constant, constructor_constant)
4496 int require_constant, constructor_constant;
4498 enum tree_code code = TREE_CODE (type);
4499 tree inside_init = init;
4501 if (init == error_mark_node)
4504 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4505 /* Do not use STRIP_NOPS here. We do not want an enumerator
4506 whose value is 0 to count as a null pointer constant. */
4507 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4508 inside_init = TREE_OPERAND (init, 0);
4510 /* Initialization of an array of chars from a string constant
4511 optionally enclosed in braces. */
4513 if (code == ARRAY_TYPE)
4515 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4516 if ((typ1 == char_type_node
4517 || typ1 == signed_char_type_node
4518 || typ1 == unsigned_char_type_node
4519 || typ1 == unsigned_wchar_type_node
4520 || typ1 == signed_wchar_type_node)
4521 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4523 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4524 TYPE_MAIN_VARIANT (type)))
4527 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4529 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4531 error_init ("char-array%s initialized from wide string",
4533 return error_mark_node;
4535 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4537 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4539 error_init ("int-array%s initialized from non-wide string",
4541 return error_mark_node;
4544 TREE_TYPE (inside_init) = type;
4545 if (TYPE_DOMAIN (type) != 0
4546 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4548 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4549 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4550 /* Subtract 1 (or sizeof (wchar_t))
4551 because it's ok to ignore the terminating null char
4552 that is counted in the length of the constant. */
4553 if (size < TREE_STRING_LENGTH (inside_init)
4554 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4555 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4558 "initializer-string for array of chars%s is too long",
4565 /* Any type can be initialized
4566 from an expression of the same type, optionally with braces. */
4568 if (inside_init && TREE_TYPE (inside_init) != 0
4569 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4570 TYPE_MAIN_VARIANT (type))
4571 || (code == ARRAY_TYPE
4572 && comptypes (TREE_TYPE (inside_init), type))
4573 || (code == POINTER_TYPE
4574 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4575 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4576 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4577 TREE_TYPE (type)))))
4579 if (code == POINTER_TYPE
4580 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4581 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4582 inside_init = default_conversion (inside_init);
4583 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4584 && TREE_CODE (inside_init) != CONSTRUCTOR)
4586 error_init ("array%s initialized from non-constant array expression",
4588 return error_mark_node;
4591 if (optimize && TREE_READONLY (inside_init)
4592 && TREE_CODE (inside_init) == VAR_DECL)
4593 inside_init = decl_constant_value (inside_init);
4595 /* Compound expressions can only occur here if -pedantic or
4596 -pedantic-errors is specified. In the later case, we always want
4597 an error. In the former case, we simply want a warning. */
4598 if (require_constant && pedantic
4599 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4602 = valid_compound_expr_initializer (inside_init,
4603 TREE_TYPE (inside_init));
4604 if (inside_init == error_mark_node)
4605 error_init ("initializer element%s is not constant",
4608 pedwarn_init ("initializer element%s is not constant",
4610 if (flag_pedantic_errors)
4611 inside_init = error_mark_node;
4613 else if (require_constant && ! TREE_CONSTANT (inside_init))
4615 error_init ("initializer element%s is not constant",
4617 inside_init = error_mark_node;
4619 else if (require_constant
4620 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4622 error_init ("initializer element%s is not computable at load time",
4624 inside_init = error_mark_node;
4630 /* Handle scalar types, including conversions. */
4632 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4633 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4635 /* Note that convert_for_assignment calls default_conversion
4636 for arrays and functions. We must not call it in the
4637 case where inside_init is a null pointer constant. */
4639 = convert_for_assignment (type, init, "initialization",
4640 NULL_TREE, NULL_TREE, 0);
4642 if (require_constant && ! TREE_CONSTANT (inside_init))
4644 error_init ("initializer element%s is not constant",
4646 inside_init = error_mark_node;
4648 else if (require_constant
4649 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4651 error_init ("initializer element%s is not computable at load time",
4653 inside_init = error_mark_node;
4659 /* Come here only for records and arrays. */
4661 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4663 error_init ("variable-sized object%s may not be initialized",
4665 return error_mark_node;
4668 /* Traditionally, you can write struct foo x = 0;
4669 and it initializes the first element of x to 0. */
4670 if (flag_traditional)
4672 tree top = 0, prev = 0;
4673 while (TREE_CODE (type) == RECORD_TYPE
4674 || TREE_CODE (type) == ARRAY_TYPE
4675 || TREE_CODE (type) == QUAL_UNION_TYPE
4676 || TREE_CODE (type) == UNION_TYPE)
4678 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4682 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4684 if (TREE_CODE (type) == ARRAY_TYPE)
4685 type = TREE_TYPE (type);
4686 else if (TYPE_FIELDS (type))
4687 type = TREE_TYPE (TYPE_FIELDS (type));
4690 error_init ("invalid initializer%s", " for `%s'", NULL);
4691 return error_mark_node;
4694 TREE_OPERAND (prev, 1)
4695 = build_tree_list (NULL_TREE,
4696 digest_init (type, init, require_constant,
4697 constructor_constant));
4700 error_init ("invalid initializer%s", " for `%s'", NULL);
4701 return error_mark_node;
4704 /* Handle initializers that use braces. */
4706 /* Type of object we are accumulating a constructor for.
4707 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4708 static tree constructor_type;
4710 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4712 static tree constructor_fields;
4714 /* For an ARRAY_TYPE, this is the specified index
4715 at which to store the next element we get.
4716 This is a special INTEGER_CST node that we modify in place. */
4717 static tree constructor_index;
4719 /* For an ARRAY_TYPE, this is the end index of the range
4720 to intitialize with the next element, or NULL in the ordinary case
4721 where the element is used just once. */
4722 static tree constructor_range_end;
4724 /* For an ARRAY_TYPE, this is the maximum index. */
4725 static tree constructor_max_index;
4727 /* For a RECORD_TYPE, this is the first field not yet written out. */
4728 static tree constructor_unfilled_fields;
4730 /* For an ARRAY_TYPE, this is the index of the first element
4731 not yet written out.
4732 This is a special INTEGER_CST node that we modify in place. */
4733 static tree constructor_unfilled_index;
4735 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4736 This is so we can generate gaps between fields, when appropriate.
4737 This is a special INTEGER_CST node that we modify in place. */
4738 static tree constructor_bit_index;
4740 /* If we are saving up the elements rather than allocating them,
4741 this is the list of elements so far (in reverse order,
4742 most recent first). */
4743 static tree constructor_elements;
4745 /* 1 if so far this constructor's elements are all compile-time constants. */
4746 static int constructor_constant;
4748 /* 1 if so far this constructor's elements are all valid address constants. */
4749 static int constructor_simple;
4751 /* 1 if this constructor is erroneous so far. */
4752 static int constructor_erroneous;
4754 /* 1 if have called defer_addressed_constants. */
4755 static int constructor_subconstants_deferred;
4757 /* List of pending elements at this constructor level.
4758 These are elements encountered out of order
4759 which belong at places we haven't reached yet in actually
4760 writing the output. */
4761 static tree constructor_pending_elts;
4763 /* The SPELLING_DEPTH of this constructor. */
4764 static int constructor_depth;
4766 /* 0 if implicitly pushing constructor levels is allowed. */
4767 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4769 /* 1 if this constructor level was entered implicitly. */
4770 static int constructor_implicit;
4772 static int require_constant_value;
4773 static int require_constant_elements;
4775 /* 1 if it is ok to output this constructor as we read it.
4776 0 means must accumulate a CONSTRUCTOR expression. */
4777 static int constructor_incremental;
4779 /* DECL node for which an initializer is being read.
4780 0 means we are reading a constructor expression
4781 such as (struct foo) {...}. */
4782 static tree constructor_decl;
4784 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4785 static char *constructor_asmspec;
4787 /* Nonzero if this is an initializer for a top-level decl. */
4788 static int constructor_top_level;
4790 /* When we finish reading a constructor expression
4791 (constructor_decl is 0), the CONSTRUCTOR goes here. */
4792 static tree constructor_result;
4794 /* This stack has a level for each implicit or explicit level of
4795 structuring in the initializer, including the outermost one. It
4796 saves the values of most of the variables above. */
4798 struct constructor_stack
4800 struct constructor_stack *next;
4806 tree unfilled_index;
4807 tree unfilled_fields;
4813 /* If nonzero, this value should replace the entire
4814 constructor at this level. */
4815 tree replacement_value;
4824 struct constructor_stack *constructor_stack;
4826 /* This stack records separate initializers that are nested.
4827 Nested initializers can't happen in ANSI C, but GNU C allows them
4828 in cases like { ... (struct foo) { ... } ... }. */
4830 struct initializer_stack
4832 struct initializer_stack *next;
4835 struct constructor_stack *constructor_stack;
4837 struct spelling *spelling;
4838 struct spelling *spelling_base;
4842 char require_constant_value;
4843 char require_constant_elements;
4847 struct initializer_stack *initializer_stack;
4849 /* Prepare to parse and output the initializer for variable DECL. */
4852 start_init (decl, asmspec_tree, top_level)
4858 struct initializer_stack *p
4859 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
4863 asmspec = TREE_STRING_POINTER (asmspec_tree);
4865 p->decl = constructor_decl;
4866 p->asmspec = constructor_asmspec;
4867 p->incremental = constructor_incremental;
4868 p->require_constant_value = require_constant_value;
4869 p->require_constant_elements = require_constant_elements;
4870 p->constructor_stack = constructor_stack;
4871 p->elements = constructor_elements;
4872 p->spelling = spelling;
4873 p->spelling_base = spelling_base;
4874 p->spelling_size = spelling_size;
4875 p->deferred = constructor_subconstants_deferred;
4876 p->top_level = constructor_top_level;
4877 p->next = initializer_stack;
4878 initializer_stack = p;
4880 constructor_decl = decl;
4881 constructor_incremental = top_level;
4882 constructor_asmspec = asmspec;
4883 constructor_subconstants_deferred = 0;
4884 constructor_top_level = top_level;
4888 require_constant_value = TREE_STATIC (decl);
4889 require_constant_elements
4890 = ((TREE_STATIC (decl) || pedantic)
4891 /* For a scalar, you can always use any value to initialize,
4892 even within braces. */
4893 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4894 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4895 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4896 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4897 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4898 constructor_incremental |= TREE_STATIC (decl);
4902 require_constant_value = 0;
4903 require_constant_elements = 0;
4904 locus = "(anonymous)";
4907 constructor_stack = 0;
4909 missing_braces_mentioned = 0;
4913 RESTORE_SPELLING_DEPTH (0);
4916 push_string (locus);
4922 struct initializer_stack *p = initializer_stack;
4924 /* Output subconstants (string constants, usually)
4925 that were referenced within this initializer and saved up.
4926 Must do this if and only if we called defer_addressed_constants. */
4927 if (constructor_subconstants_deferred)
4928 output_deferred_addressed_constants ();
4930 /* Free the whole constructor stack of this initializer. */
4931 while (constructor_stack)
4933 struct constructor_stack *q = constructor_stack;
4934 constructor_stack = q->next;
4938 /* Pop back to the data of the outer initializer (if any). */
4939 constructor_decl = p->decl;
4940 constructor_asmspec = p->asmspec;
4941 constructor_incremental = p->incremental;
4942 require_constant_value = p->require_constant_value;
4943 require_constant_elements = p->require_constant_elements;
4944 constructor_stack = p->constructor_stack;
4945 constructor_elements = p->elements;
4946 spelling = p->spelling;
4947 spelling_base = p->spelling_base;
4948 spelling_size = p->spelling_size;
4949 constructor_subconstants_deferred = p->deferred;
4950 constructor_top_level = p->top_level;
4951 initializer_stack = p->next;
4955 /* Call here when we see the initializer is surrounded by braces.
4956 This is instead of a call to push_init_level;
4957 it is matched by a call to pop_init_level.
4959 TYPE is the type to initialize, for a constructor expression.
4960 For an initializer for a decl, TYPE is zero. */
4963 really_start_incremental_init (type)
4966 struct constructor_stack *p
4967 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
4970 type = TREE_TYPE (constructor_decl);
4972 /* Turn off constructor_incremental if type is a struct with bitfields.
4973 Do this before the first push, so that the corrected value
4974 is available in finish_init. */
4975 check_init_type_bitfields (type);
4977 p->type = constructor_type;
4978 p->fields = constructor_fields;
4979 p->index = constructor_index;
4980 p->range_end = constructor_range_end;
4981 p->max_index = constructor_max_index;
4982 p->unfilled_index = constructor_unfilled_index;
4983 p->unfilled_fields = constructor_unfilled_fields;
4984 p->bit_index = constructor_bit_index;
4985 p->elements = constructor_elements;
4986 p->constant = constructor_constant;
4987 p->simple = constructor_simple;
4988 p->erroneous = constructor_erroneous;
4989 p->pending_elts = constructor_pending_elts;
4990 p->depth = constructor_depth;
4991 p->replacement_value = 0;
4993 p->incremental = constructor_incremental;
4996 constructor_stack = p;
4998 constructor_constant = 1;
4999 constructor_simple = 1;
5000 constructor_depth = SPELLING_DEPTH ();
5001 constructor_elements = 0;
5002 constructor_pending_elts = 0;
5003 constructor_type = type;
5005 if (TREE_CODE (constructor_type) == RECORD_TYPE
5006 || TREE_CODE (constructor_type) == UNION_TYPE)
5008 constructor_fields = TYPE_FIELDS (constructor_type);
5009 /* Skip any nameless bit fields atthe beginning. */
5010 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5011 && DECL_NAME (constructor_fields) == 0)
5012 constructor_fields = TREE_CHAIN (constructor_fields);
5013 constructor_unfilled_fields = constructor_fields;
5014 constructor_bit_index = copy_node (integer_zero_node);
5016 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5018 constructor_range_end = 0;
5019 if (TYPE_DOMAIN (constructor_type))
5021 constructor_max_index
5022 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5024 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5027 constructor_index = copy_node (integer_zero_node);
5028 constructor_unfilled_index = copy_node (constructor_index);
5032 /* Handle the case of int x = {5}; */
5033 constructor_fields = constructor_type;
5034 constructor_unfilled_fields = constructor_type;
5037 if (constructor_incremental)
5039 int momentary = suspend_momentary ();
5040 push_obstacks_nochange ();
5041 if (TREE_PERMANENT (constructor_decl))
5042 end_temporary_allocation ();
5043 make_decl_rtl (constructor_decl, constructor_asmspec,
5044 constructor_top_level);
5045 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5047 resume_momentary (momentary);
5050 if (constructor_incremental)
5052 defer_addressed_constants ();
5053 constructor_subconstants_deferred = 1;
5057 /* Push down into a subobject, for initialization.
5058 If this is for an explicit set of braces, IMPLICIT is 0.
5059 If it is because the next element belongs at a lower level,
5063 push_init_level (implicit)
5066 struct constructor_stack *p;
5068 /* If we've exhausted any levels that didn't have braces,
5070 while (constructor_stack->implicit)
5072 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5073 || TREE_CODE (constructor_type) == UNION_TYPE)
5074 && constructor_fields == 0)
5075 process_init_element (pop_init_level (1));
5076 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5077 && tree_int_cst_lt (constructor_max_index, constructor_index))
5078 process_init_element (pop_init_level (1));
5083 /* Structure elements may require alignment. Do this now
5084 if necessary for the subaggregate. */
5085 if (constructor_incremental && TREE_CODE (constructor_type) == RECORD_TYPE
5086 && constructor_fields)
5088 /* Advance to offset of this element. */
5089 if (! tree_int_cst_equal (constructor_bit_index,
5090 DECL_FIELD_BITPOS (constructor_fields)))
5092 int next = (TREE_INT_CST_LOW
5093 (DECL_FIELD_BITPOS (constructor_fields))
5095 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5098 assemble_zeros (next - here);
5102 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5103 p->type = constructor_type;
5104 p->fields = constructor_fields;
5105 p->index = constructor_index;
5106 p->range_end = constructor_range_end;
5107 p->max_index = constructor_max_index;
5108 p->unfilled_index = constructor_unfilled_index;
5109 p->unfilled_fields = constructor_unfilled_fields;
5110 p->bit_index = constructor_bit_index;
5111 p->elements = constructor_elements;
5112 p->constant = constructor_constant;
5113 p->simple = constructor_simple;
5114 p->erroneous = constructor_erroneous;
5115 p->pending_elts = constructor_pending_elts;
5116 p->depth = constructor_depth;
5117 p->replacement_value = 0;
5118 p->implicit = implicit;
5119 p->incremental = constructor_incremental;
5121 p->next = constructor_stack;
5122 constructor_stack = p;
5124 constructor_constant = 1;
5125 constructor_simple = 1;
5126 constructor_depth = SPELLING_DEPTH ();
5127 constructor_elements = 0;
5128 constructor_pending_elts = 0;
5130 /* Don't die if an entire brace-pair level is superfluous
5131 in the containing level. */
5132 if (constructor_type == 0)
5134 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5135 || TREE_CODE (constructor_type) == UNION_TYPE)
5137 /* Don't die if there are extra init elts at the end. */
5138 if (constructor_fields == 0)
5139 constructor_type = 0;
5142 constructor_type = TREE_TYPE (constructor_fields);
5143 push_member_name (constructor_fields);
5146 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5148 constructor_type = TREE_TYPE (constructor_type);
5149 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5152 if (constructor_type == 0)
5154 error_init ("extra brace group at end of initializer%s",
5156 constructor_fields = 0;
5157 constructor_unfilled_fields = 0;
5161 /* Turn off constructor_incremental if type is a struct with bitfields. */
5162 check_init_type_bitfields (constructor_type);
5164 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5166 missing_braces_mentioned = 1;
5167 warning_init ("missing braces around initializer%s", " for `%s'", NULL);
5170 if (TREE_CODE (constructor_type) == RECORD_TYPE
5171 || TREE_CODE (constructor_type) == UNION_TYPE)
5173 constructor_fields = TYPE_FIELDS (constructor_type);
5174 /* Skip any nameless bit fields atthe beginning. */
5175 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5176 && DECL_NAME (constructor_fields) == 0)
5177 constructor_fields = TREE_CHAIN (constructor_fields);
5178 constructor_unfilled_fields = constructor_fields;
5179 constructor_bit_index = copy_node (integer_zero_node);
5181 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5183 constructor_range_end = 0;
5184 if (TYPE_DOMAIN (constructor_type))
5186 constructor_max_index
5187 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5189 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5192 constructor_index = copy_node (integer_zero_node);
5193 constructor_unfilled_index = copy_node (constructor_index);
5197 warning_init ("braces around scalar initializer%s", " for `%s'", NULL);
5198 constructor_fields = constructor_type;
5199 constructor_unfilled_fields = constructor_type;
5203 /* Don't read a struct incrementally if it has any bitfields,
5204 because the incremental reading code doesn't know how to
5205 handle bitfields yet. */
5208 check_init_type_bitfields (type)
5211 if (TREE_CODE (type) == RECORD_TYPE)
5214 for (tail = TYPE_FIELDS (type); tail;
5215 tail = TREE_CHAIN (tail))
5217 if (DECL_BIT_FIELD (tail)
5218 /* This catches cases like `int foo : 8;'. */
5219 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail)))
5221 constructor_incremental = 0;
5225 check_init_type_bitfields (TREE_TYPE (tail));
5229 else if (TREE_CODE (type) == ARRAY_TYPE)
5230 check_init_type_bitfields (TREE_TYPE (type));
5233 /* At the end of an implicit or explicit brace level,
5234 finish up that level of constructor.
5235 If we were outputting the elements as they are read, return 0
5236 from inner levels (process_init_element ignores that),
5237 but return error_mark_node from the outermost level
5238 (that's what we want to put in DECL_INITIAL).
5239 Otherwise, return a CONSTRUCTOR expression. */
5242 pop_init_level (implicit)
5245 struct constructor_stack *p;
5247 tree constructor = 0;
5251 /* When we come to an explicit close brace,
5252 pop any inner levels that didn't have explicit braces. */
5253 while (constructor_stack->implicit)
5254 process_init_element (pop_init_level (1));
5257 p = constructor_stack;
5259 if (constructor_type != 0)
5260 size = int_size_in_bytes (constructor_type);
5262 /* Now output all pending elements. */
5263 output_pending_init_elements (1);
5265 #if 0 /* c-parse.in warns about {}. */
5266 /* In ANSI, each brace level must have at least one element. */
5267 if (! implicit && pedantic
5268 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5269 ? integer_zerop (constructor_unfilled_index)
5270 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5271 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL);
5274 /* Pad out the end of the structure. */
5276 if (p->replacement_value)
5278 /* If this closes a superfluous brace pair,
5279 just pass out the element between them. */
5280 constructor = p->replacement_value;
5281 /* If this is the top level thing within the initializer,
5282 and it's for a variable, then since we already called
5283 assemble_variable, we must output the value now. */
5284 if (p->next == 0 && constructor_decl != 0
5285 && constructor_incremental)
5287 constructor = digest_init (constructor_type, constructor,
5290 /* If initializing an array of unknown size,
5291 determine the size now. */
5292 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5293 && TYPE_DOMAIN (constructor_type) == 0)
5298 push_obstacks_nochange ();
5299 if (TREE_PERMANENT (constructor_type))
5300 end_temporary_allocation ();
5302 momentary_p = suspend_momentary ();
5304 /* We shouldn't have an incomplete array type within
5306 if (constructor_stack->next)
5310 = complete_array_type (constructor_type,
5315 size = int_size_in_bytes (constructor_type);
5316 resume_momentary (momentary_p);
5320 output_constant (constructor, size);
5323 else if (constructor_type == 0)
5325 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5326 && TREE_CODE (constructor_type) != UNION_TYPE
5327 && TREE_CODE (constructor_type) != ARRAY_TYPE
5328 && ! constructor_incremental)
5330 /* A nonincremental scalar initializer--just return
5331 the element, after verifying there is just one. */
5332 if (constructor_elements == 0)
5334 error_init ("empty scalar initializer%s",
5336 constructor = error_mark_node;
5338 else if (TREE_CHAIN (constructor_elements) != 0)
5340 error_init ("extra elements in scalar initializer%s",
5342 constructor = TREE_VALUE (constructor_elements);
5345 constructor = TREE_VALUE (constructor_elements);
5347 else if (! constructor_incremental)
5349 if (constructor_erroneous)
5350 constructor = error_mark_node;
5353 int momentary = suspend_momentary ();
5355 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5356 nreverse (constructor_elements));
5357 if (constructor_constant)
5358 TREE_CONSTANT (constructor) = 1;
5359 if (constructor_constant && constructor_simple)
5360 TREE_STATIC (constructor) = 1;
5362 resume_momentary (momentary);
5368 int momentary = suspend_momentary ();
5370 if (TREE_CODE (constructor_type) == RECORD_TYPE
5371 || TREE_CODE (constructor_type) == UNION_TYPE)
5373 /* Find the offset of the end of that field. */
5374 filled = size_binop (CEIL_DIV_EXPR,
5375 constructor_bit_index,
5376 size_int (BITS_PER_UNIT));
5378 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5380 /* If initializing an array of unknown size,
5381 determine the size now. */
5382 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5383 && TYPE_DOMAIN (constructor_type) == 0)
5386 = size_binop (MINUS_EXPR,
5387 constructor_unfilled_index,
5390 push_obstacks_nochange ();
5391 if (TREE_PERMANENT (constructor_type))
5392 end_temporary_allocation ();
5393 maxindex = copy_node (maxindex);
5394 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5395 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5397 /* TYPE_MAX_VALUE is always one less than the number of elements
5398 in the array, because we start counting at zero. Therefore,
5399 warn only if the value is less than zero. */
5401 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5403 error_with_decl (constructor_decl,
5404 "zero or negative array size `%s'");
5405 layout_type (constructor_type);
5406 size = int_size_in_bytes (constructor_type);
5410 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5411 size_in_bytes (TREE_TYPE (constructor_type)));
5417 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5419 resume_momentary (momentary);
5423 constructor_type = p->type;
5424 constructor_fields = p->fields;
5425 constructor_index = p->index;
5426 constructor_range_end = p->range_end;
5427 constructor_max_index = p->max_index;
5428 constructor_unfilled_index = p->unfilled_index;
5429 constructor_unfilled_fields = p->unfilled_fields;
5430 constructor_bit_index = p->bit_index;
5431 constructor_elements = p->elements;
5432 constructor_constant = p->constant;
5433 constructor_simple = p->simple;
5434 constructor_erroneous = p->erroneous;
5435 constructor_pending_elts = p->pending_elts;
5436 constructor_depth = p->depth;
5437 constructor_incremental = p->incremental;
5438 RESTORE_SPELLING_DEPTH (constructor_depth);
5440 constructor_stack = p->next;
5443 if (constructor == 0)
5445 if (constructor_stack == 0)
5446 return error_mark_node;
5452 /* Within an array initializer, specify the next index to be initialized.
5453 FIRST is that index. If LAST is nonzero, then initialize a range
5454 of indices, running from FIRST through LAST. */
5457 set_init_index (first, last)
5460 while ((TREE_CODE (first) == NOP_EXPR
5461 || TREE_CODE (first) == CONVERT_EXPR
5462 || TREE_CODE (first) == NON_LVALUE_EXPR)
5463 && (TYPE_MODE (TREE_TYPE (first))
5464 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5465 (first) = TREE_OPERAND (first, 0);
5467 while ((TREE_CODE (last) == NOP_EXPR
5468 || TREE_CODE (last) == CONVERT_EXPR
5469 || TREE_CODE (last) == NON_LVALUE_EXPR)
5470 && (TYPE_MODE (TREE_TYPE (last))
5471 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5472 (last) = TREE_OPERAND (last, 0);
5474 if (TREE_CODE (first) != INTEGER_CST)
5475 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5476 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5477 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5478 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5479 error_init ("duplicate array index in initializer%s", " for `%s'", NULL);
5482 TREE_INT_CST_LOW (constructor_index)
5483 = TREE_INT_CST_LOW (first);
5484 TREE_INT_CST_HIGH (constructor_index)
5485 = TREE_INT_CST_HIGH (first);
5487 if (last != 0 && tree_int_cst_lt (last, first))
5488 error_init ("empty index range in initializer%s", " for `%s'", NULL);
5492 pedwarn ("ANSI C forbids specifying element to initialize");
5493 constructor_range_end = last;
5498 /* Within a struct initializer, specify the next field to be initialized. */
5501 set_init_label (fieldname)
5507 for (tail = TYPE_FIELDS (constructor_type); tail;
5508 tail = TREE_CHAIN (tail))
5510 if (tail == constructor_unfilled_fields)
5512 if (DECL_NAME (tail) == fieldname)
5517 error ("unknown field `%s' specified in initializer",
5518 IDENTIFIER_POINTER (fieldname));
5520 error ("field `%s' already initialized",
5521 IDENTIFIER_POINTER (fieldname));
5524 constructor_fields = tail;
5526 pedwarn ("ANSI C forbids specifying structure member to initialize");
5530 /* "Output" the next constructor element.
5531 At top level, really output it to assembler code now.
5532 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5533 TYPE is the data type that the containing data type wants here.
5534 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5536 PENDING if non-nil means output pending elements that belong
5537 right after this element. (PENDING is normally 1;
5538 it is 0 while outputting pending elements, to avoid recursion.) */
5541 output_init_element (value, type, field, pending)
5542 tree value, type, field;
5547 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5548 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5549 && !(TREE_CODE (value) == STRING_CST
5550 && TREE_CODE (type) == ARRAY_TYPE
5551 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5552 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5553 TYPE_MAIN_VARIANT (type))))
5554 value = default_conversion (value);
5556 if (value == error_mark_node)
5557 constructor_erroneous = 1;
5558 else if (!TREE_CONSTANT (value))
5559 constructor_constant = 0;
5560 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5561 constructor_simple = 0;
5563 if (require_constant_value && ! TREE_CONSTANT (value))
5565 error_init ("initializer element%s is not constant",
5567 value = error_mark_node;
5569 else if (require_constant_elements
5570 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5572 error_init ("initializer element%s is not computable at load time",
5574 value = error_mark_node;
5577 /* If this element duplicates one on constructor_pending_elts,
5578 print a message and ignore it. Don't do this when we're
5579 processing elements taken off constructor_pending_elts,
5580 because we'd always get spurious errors. */
5583 if (TREE_CODE (constructor_type) == RECORD_TYPE
5584 || TREE_CODE (constructor_type) == UNION_TYPE)
5586 if (purpose_member (field, constructor_pending_elts))
5588 error_init ("duplicate initializer%s", " for `%s'", NULL);
5592 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5595 for (tail = constructor_pending_elts; tail;
5596 tail = TREE_CHAIN (tail))
5597 if (TREE_PURPOSE (tail) != 0
5598 && TREE_CODE (TREE_PURPOSE (tail)) == INTEGER_CST
5599 && tree_int_cst_equal (TREE_PURPOSE (tail), constructor_index))
5604 error_init ("duplicate initializer%s", " for `%s'", NULL);
5610 /* If this element doesn't come next in sequence,
5611 put it on constructor_pending_elts. */
5612 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5613 && !tree_int_cst_equal (field, constructor_unfilled_index))
5616 /* The copy_node is needed in case field is actually
5617 constructor_index, which is modified in place. */
5618 constructor_pending_elts
5619 = tree_cons (copy_node (field),
5620 digest_init (type, value, 0, 0),
5621 constructor_pending_elts);
5623 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5624 && field != constructor_unfilled_fields)
5626 /* We do this for records but not for unions. In a union,
5627 no matter which field is specified, it can be initialized
5628 right away since it starts at the beginning of the union. */
5630 constructor_pending_elts
5632 digest_init (type, value, 0, 0),
5633 constructor_pending_elts);
5637 /* Otherwise, output this element either to
5638 constructor_elements or to the assembler file. */
5642 if (! constructor_incremental)
5644 if (field && TREE_CODE (field) == INTEGER_CST)
5645 field = copy_node (field);
5646 constructor_elements
5647 = tree_cons (field, digest_init (type, value, 0, 0),
5648 constructor_elements);
5652 /* Structure elements may require alignment.
5653 Do this, if necessary. */
5654 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5656 /* Advance to offset of this element. */
5657 if (! tree_int_cst_equal (constructor_bit_index,
5658 DECL_FIELD_BITPOS (field)))
5660 int next = (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
5662 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5665 assemble_zeros (next - here);
5668 output_constant (digest_init (type, value, 0, 0),
5669 int_size_in_bytes (type));
5671 /* For a record or union,
5672 keep track of end position of last field. */
5673 if (TREE_CODE (constructor_type) == RECORD_TYPE
5674 || TREE_CODE (constructor_type) == UNION_TYPE)
5676 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
5678 TREE_INT_CST_LOW (constructor_bit_index)
5679 = TREE_INT_CST_LOW (temp);
5680 TREE_INT_CST_HIGH (constructor_bit_index)
5681 = TREE_INT_CST_HIGH (temp);
5686 /* Advance the variable that indicates sequential elements output. */
5687 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5689 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
5691 TREE_INT_CST_LOW (constructor_unfilled_index)
5692 = TREE_INT_CST_LOW (tem);
5693 TREE_INT_CST_HIGH (constructor_unfilled_index)
5694 = TREE_INT_CST_HIGH (tem);
5696 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5697 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5698 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5699 constructor_unfilled_fields = 0;
5701 /* Now output any pending elements which have become next. */
5703 output_pending_init_elements (0);
5707 /* Output any pending elements which have become next.
5708 As we output elements, constructor_unfilled_{fields,index}
5709 advances, which may cause other elements to become next;
5710 if so, they too are output.
5712 If ALL is 0, we return when there are
5713 no more pending elements to output now.
5715 If ALL is 1, we output space as necessary so that
5716 we can output all the pending elements. */
5719 output_pending_init_elements (all)
5727 /* Look thru the whole pending list.
5728 If we find an element that should be output now,
5729 output it. Otherwise, set NEXT to the element
5730 that comes first among those still pending. */
5733 for (tail = constructor_pending_elts; tail;
5734 tail = TREE_CHAIN (tail))
5736 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5738 if (tree_int_cst_equal (TREE_PURPOSE (tail),
5739 constructor_unfilled_index))
5741 output_init_element (TREE_VALUE (tail),
5742 TREE_TYPE (constructor_type),
5743 constructor_unfilled_index, 0);
5746 else if (tree_int_cst_lt (TREE_PURPOSE (tail),
5747 constructor_unfilled_index))
5750 || tree_int_cst_lt (TREE_PURPOSE (tail), next))
5751 next = TREE_PURPOSE (tail);
5753 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5754 || TREE_CODE (constructor_type) == UNION_TYPE)
5756 if (TREE_PURPOSE (tail) == constructor_unfilled_fields)
5758 output_init_element (TREE_VALUE (tail),
5759 TREE_TYPE (constructor_unfilled_fields),
5760 constructor_unfilled_fields,
5764 else if (constructor_unfilled_fields == 0
5765 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
5766 DECL_FIELD_BITPOS (constructor_unfilled_fields)))
5769 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
5770 DECL_FIELD_BITPOS (next)))
5771 next = TREE_PURPOSE (tail);
5775 /* Ordinarily return, but not if we want to output all
5776 and there are elements left. */
5777 if (! (all && next != 0))
5780 /* Generate space up to the position of NEXT. */
5781 if (constructor_incremental)
5784 tree nextpos_tree = size_int (0);
5786 if (TREE_CODE (constructor_type) == RECORD_TYPE
5787 || TREE_CODE (constructor_type) == UNION_TYPE)
5789 /* Find the last field written out, if any. */
5790 for (tail = TYPE_FIELDS (constructor_type); tail;
5791 tail = TREE_CHAIN (tail))
5792 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
5796 /* Find the offset of the end of that field. */
5797 filled = size_binop (CEIL_DIV_EXPR,
5798 size_binop (PLUS_EXPR,
5799 DECL_FIELD_BITPOS (tail),
5801 size_int (BITS_PER_UNIT));
5803 filled = size_int (0);
5805 nextpos_tree = size_binop (CEIL_DIV_EXPR,
5806 DECL_FIELD_BITPOS (next),
5807 size_int (BITS_PER_UNIT));
5809 TREE_INT_CST_HIGH (constructor_bit_index)
5810 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
5811 TREE_INT_CST_LOW (constructor_bit_index)
5812 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
5813 constructor_unfilled_fields = next;
5815 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5817 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5818 size_in_bytes (TREE_TYPE (constructor_type)));
5820 = size_binop (MULT_EXPR, next,
5821 size_in_bytes (TREE_TYPE (constructor_type)));
5822 TREE_INT_CST_LOW (constructor_unfilled_index)
5823 = TREE_INT_CST_LOW (next);
5824 TREE_INT_CST_HIGH (constructor_unfilled_index)
5825 = TREE_INT_CST_HIGH (next);
5832 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
5834 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
5839 /* If it's not incremental, just skip over the gap,
5840 so that after jumping to retry we will output the next
5841 successive element. */
5842 if (TREE_CODE (constructor_type) == RECORD_TYPE
5843 || TREE_CODE (constructor_type) == UNION_TYPE)
5844 constructor_unfilled_fields = next;
5845 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5847 TREE_INT_CST_LOW (constructor_unfilled_index)
5848 = TREE_INT_CST_LOW (next);
5849 TREE_INT_CST_HIGH (constructor_unfilled_index)
5850 = TREE_INT_CST_HIGH (next);
5857 /* Add one non-braced element to the current constructor level.
5858 This adjusts the current position within the constructor's type.
5859 This may also start or terminate implicit levels
5860 to handle a partly-braced initializer.
5862 Once this has found the correct level for the new element,
5863 it calls output_init_element.
5865 Note: if we are incrementally outputting this constructor,
5866 this function may be called with a null argument
5867 representing a sub-constructor that was already incrementally output.
5868 When that happens, we output nothing, but we do the bookkeeping
5869 to skip past that element of the current constructor. */
5872 process_init_element (value)
5875 tree orig_value = value;
5876 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5878 /* Handle superfluous braces around string cst as in
5879 char x[] = {"foo"}; */
5882 && TREE_CODE (constructor_type) == ARRAY_TYPE
5883 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5884 && integer_zerop (constructor_unfilled_index))
5886 constructor_stack->replacement_value = value;
5890 if (constructor_stack->replacement_value != 0)
5892 error_init ("excess elements in struct initializer%s",
5893 " after `%s'", NULL_PTR);
5897 /* Ignore elements of a brace group if it is entirely superfluous
5898 and has already been diagnosed. */
5899 if (constructor_type == 0)
5902 /* If we've exhausted any levels that didn't have braces,
5904 while (constructor_stack->implicit)
5906 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5907 || TREE_CODE (constructor_type) == UNION_TYPE)
5908 && constructor_fields == 0)
5909 process_init_element (pop_init_level (1));
5910 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5911 && tree_int_cst_lt (constructor_max_index, constructor_index))
5912 process_init_element (pop_init_level (1));
5919 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5922 enum tree_code fieldcode;
5924 if (constructor_fields == 0)
5926 pedwarn_init ("excess elements in struct initializer%s",
5927 " after `%s'", NULL_PTR);
5931 fieldtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_fields));
5932 fieldcode = TREE_CODE (fieldtype);
5934 /* Accept a string constant to initialize a subarray. */
5936 && fieldcode == ARRAY_TYPE
5937 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5940 /* Otherwise, if we have come to a subaggregate,
5941 and we don't have an element of its type, push into it. */
5942 else if (value != 0 && !constructor_no_implicit
5943 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5944 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5945 || fieldcode == UNION_TYPE))
5947 push_init_level (1);
5953 push_member_name (constructor_fields);
5954 output_init_element (value, fieldtype, constructor_fields, 1);
5955 RESTORE_SPELLING_DEPTH (constructor_depth);
5958 /* Do the bookkeeping for an element that was
5959 directly output as a constructor. */
5961 /* For a record, keep track of end position of last field. */
5962 tree temp = size_binop (PLUS_EXPR,
5963 DECL_FIELD_BITPOS (constructor_fields),
5964 DECL_SIZE (constructor_fields));
5965 TREE_INT_CST_LOW (constructor_bit_index)
5966 = TREE_INT_CST_LOW (temp);
5967 TREE_INT_CST_HIGH (constructor_bit_index)
5968 = TREE_INT_CST_HIGH (temp);
5970 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5973 constructor_fields = TREE_CHAIN (constructor_fields);
5974 /* Skip any nameless bit fields atthe beginning. */
5975 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5976 && DECL_NAME (constructor_fields) == 0)
5977 constructor_fields = TREE_CHAIN (constructor_fields);
5980 if (TREE_CODE (constructor_type) == UNION_TYPE)
5983 enum tree_code fieldcode;
5985 if (constructor_fields == 0)
5987 pedwarn_init ("excess elements in union initializer%s",
5988 " after `%s'", NULL_PTR);
5992 fieldtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_fields));
5993 fieldcode = TREE_CODE (fieldtype);
5995 /* Accept a string constant to initialize a subarray. */
5997 && fieldcode == ARRAY_TYPE
5998 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6001 /* Otherwise, if we have come to a subaggregate,
6002 and we don't have an element of its type, push into it. */
6003 else if (value != 0 && !constructor_no_implicit
6004 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6005 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6006 || fieldcode == UNION_TYPE))
6008 push_init_level (1);
6014 push_member_name (constructor_fields);
6015 output_init_element (value, fieldtype, constructor_fields, 1);
6016 RESTORE_SPELLING_DEPTH (constructor_depth);
6019 /* Do the bookkeeping for an element that was
6020 directly output as a constructor. */
6022 TREE_INT_CST_LOW (constructor_bit_index)
6023 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6024 TREE_INT_CST_HIGH (constructor_bit_index)
6025 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6027 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6030 constructor_fields = 0;
6033 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6035 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6036 enum tree_code eltcode = TREE_CODE (elttype);
6038 /* Accept a string constant to initialize a subarray. */
6040 && eltcode == ARRAY_TYPE
6041 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6044 /* Otherwise, if we have come to a subaggregate,
6045 and we don't have an element of its type, push into it. */
6046 else if (value != 0 && !constructor_no_implicit
6047 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6048 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6049 || eltcode == UNION_TYPE))
6051 push_init_level (1);
6055 if (constructor_max_index != 0
6056 && tree_int_cst_lt (constructor_max_index, constructor_index))
6058 pedwarn_init ("excess elements in array initializer%s",
6059 " after `%s'", NULL_PTR);
6063 /* Now output the actual element.
6064 Ordinarily, output once.
6065 If there is a range, repeat it till we advance past the range. */
6072 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6073 output_init_element (value, elttype, constructor_index, 1);
6074 RESTORE_SPELLING_DEPTH (constructor_depth);
6077 tem = size_binop (PLUS_EXPR, constructor_index,
6079 TREE_INT_CST_LOW (constructor_index)
6080 = TREE_INT_CST_LOW (tem);
6081 TREE_INT_CST_HIGH (constructor_index)
6082 = TREE_INT_CST_HIGH (tem);
6085 /* If we are doing the bookkeeping for an element that was
6086 directly output as a constructor,
6087 we must update constructor_unfilled_index. */
6089 TREE_INT_CST_LOW (constructor_unfilled_index)
6090 = TREE_INT_CST_LOW (constructor_index);
6091 TREE_INT_CST_HIGH (constructor_unfilled_index)
6092 = TREE_INT_CST_HIGH (constructor_index);
6095 while (! (constructor_range_end == 0
6096 || tree_int_cst_lt (constructor_range_end,
6097 constructor_index)));
6102 /* Handle the sole element allowed in a braced initializer
6103 for a scalar variable. */
6104 if (constructor_fields == 0)
6106 pedwarn_init ("excess elements in scalar initializer%s",
6107 " after `%s'", NULL_PTR);
6112 output_init_element (value, constructor_type, NULL_TREE, 1);
6113 constructor_fields = 0;
6117 /* If the (lexically) previous elments are not now saved,
6118 we can discard the storage for them. */
6119 if (constructor_incremental && constructor_pending_elts == 0 && value != 0)
6123 /* Expand an ASM statement with operands, handling output operands
6124 that are not variables or INDIRECT_REFS by transforming such
6125 cases into cases that expand_asm_operands can handle.
6127 Arguments are same as for expand_asm_operands. */
6130 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6131 tree string, outputs, inputs, clobbers;
6136 int noutputs = list_length (outputs);
6138 /* o[I] is the place that output number I should be written. */
6139 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6142 if (TREE_CODE (string) == ADDR_EXPR)
6143 string = TREE_OPERAND (string, 0);
6144 if (TREE_CODE (string) != STRING_CST)
6146 error ("asm template is not a string constant");
6150 /* Record the contents of OUTPUTS before it is modified. */
6151 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6152 o[i] = TREE_VALUE (tail);
6154 /* Perform default conversions on array and function inputs. */
6155 /* Don't do this for other types--
6156 it would screw up operands expected to be in memory. */
6157 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6158 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6159 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6160 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6162 /* Generate the ASM_OPERANDS insn;
6163 store into the TREE_VALUEs of OUTPUTS some trees for
6164 where the values were actually stored. */
6165 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6167 /* Copy all the intermediate outputs into the specified outputs. */
6168 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6170 if (o[i] != TREE_VALUE (tail))
6172 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6176 /* Detect modification of read-only values.
6177 (Otherwise done by build_modify_expr.) */
6180 tree type = TREE_TYPE (o[i]);
6181 if (TYPE_READONLY (type)
6182 || ((TREE_CODE (type) == RECORD_TYPE
6183 || TREE_CODE (type) == UNION_TYPE)
6184 && C_TYPE_FIELDS_READONLY (type)))
6185 readonly_warning (o[i], "modification by `asm'");
6189 /* Those MODIFY_EXPRs could do autoincrements. */
6193 /* Expand a C `return' statement.
6194 RETVAL is the expression for what to return,
6195 or a null pointer for `return;' with no value. */
6198 c_expand_return (retval)
6201 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6203 if (TREE_THIS_VOLATILE (current_function_decl))
6204 warning ("function declared `noreturn' has a `return' statement");
6208 current_function_returns_null = 1;
6209 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6210 warning ("`return' with no value, in function returning non-void");
6211 expand_null_return ();
6213 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6215 current_function_returns_null = 1;
6216 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6217 pedwarn ("`return' with a value, in function returning void");
6218 expand_return (retval);
6222 tree t = convert_for_assignment (valtype, retval, "return",
6223 NULL_TREE, NULL_TREE, 0);
6224 tree res = DECL_RESULT (current_function_decl);
6227 if (t == error_mark_node)
6230 inner = t = convert (TREE_TYPE (res), t);
6232 /* Strip any conversions, additions, and subtractions, and see if
6233 we are returning the address of a local variable. Warn if so. */
6234 while (TREE_CODE (inner) == NOP_EXPR
6235 || TREE_CODE (inner) == NON_LVALUE_EXPR
6236 || TREE_CODE (inner) == CONVERT_EXPR
6237 || TREE_CODE (inner) == PLUS_EXPR
6238 || TREE_CODE (inner) == MINUS_EXPR)
6239 inner = TREE_OPERAND (inner, 0);
6241 if (TREE_CODE (inner) == ADDR_EXPR)
6243 inner = TREE_OPERAND (inner, 0);
6245 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6246 inner = TREE_OPERAND (inner, 0);
6248 if (TREE_CODE (inner) == VAR_DECL
6249 && ! DECL_EXTERNAL (inner)
6250 && ! TREE_STATIC (inner)
6251 && DECL_CONTEXT (inner) == current_function_decl)
6252 warning ("function returns address of local variable");
6255 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6256 TREE_SIDE_EFFECTS (t) = 1;
6258 current_function_returns_value = 1;
6262 /* Start a C switch statement, testing expression EXP.
6263 Return EXP if it is valid, an error node otherwise. */
6266 c_expand_start_case (exp)
6269 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6270 tree type = TREE_TYPE (exp);
6272 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6274 error ("switch quantity not an integer");
6275 exp = error_mark_node;
6280 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6282 if (warn_traditional
6283 && (type == long_integer_type_node
6284 || type == long_unsigned_type_node))
6285 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6287 exp = default_conversion (exp);
6288 type = TREE_TYPE (exp);
6289 index = get_unwidened (exp, NULL_TREE);
6290 /* We can't strip a conversion from a signed type to an unsigned,
6291 because if we did, int_fits_type_p would do the wrong thing
6292 when checking case values for being in range,
6293 and it's too hard to do the right thing. */
6294 if (TREE_UNSIGNED (TREE_TYPE (exp))
6295 == TREE_UNSIGNED (TREE_TYPE (index)))
6299 expand_start_case (1, exp, type, "switch statement");