1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993 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 "partly bracketed initializer"
37 message within this initializer. */
38 static int partial_bracket_mentioned = 0;
40 extern char *index ();
41 extern char *rindex ();
43 int mark_addressable ();
44 static tree convert_for_assignment ();
45 static void warn_for_assignment ();
46 static int function_types_compatible_p ();
47 static int type_lists_compatible_p ();
48 int self_promoting_args_p ();
49 static int self_promoting_type_p ();
50 static int comp_target_types ();
51 static tree pointer_int_sum ();
52 static tree pointer_diff ();
53 static tree convert_sequence ();
54 static tree unary_complex_lvalue ();
55 static tree process_init_constructor ();
56 static tree convert_arguments ();
57 static char *get_spelling ();
58 static tree digest_init ();
59 static void pedantic_lvalue_warning ();
60 tree truthvalue_conversion ();
61 void incomplete_type_error ();
62 void readonly_warning ();
63 static tree internal_build_compound_expr ();
65 void process_init_element ();
67 /* Do `exp = require_complete_type (exp);' to make sure exp
68 does not have an incomplete type. (That includes void types.) */
71 require_complete_type (value)
74 tree type = TREE_TYPE (value);
76 /* First, detect a valid value with a complete type. */
77 if (TYPE_SIZE (type) != 0
78 && type != void_type_node)
81 incomplete_type_error (value, type);
82 return error_mark_node;
85 /* Print an error message for invalid use of an incomplete type.
86 VALUE is the expression that was used (or 0 if that isn't known)
87 and TYPE is the type that was invalid. */
90 incomplete_type_error (value, type)
96 /* Avoid duplicate error message. */
97 if (TREE_CODE (type) == ERROR_MARK)
100 if (value != 0 && (TREE_CODE (value) == VAR_DECL
101 || TREE_CODE (value) == PARM_DECL))
102 error ("`%s' has an incomplete type",
103 IDENTIFIER_POINTER (DECL_NAME (value)));
107 /* We must print an error message. Be clever about what it says. */
109 switch (TREE_CODE (type))
112 errmsg = "invalid use of undefined type `struct %s'";
116 errmsg = "invalid use of undefined type `union %s'";
120 errmsg = "invalid use of undefined type `enum %s'";
124 error ("invalid use of void expression");
128 if (TYPE_DOMAIN (type))
130 type = TREE_TYPE (type);
133 error ("invalid use of array with unspecified bounds");
140 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
141 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
143 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
144 error ("invalid use of incomplete typedef `%s'",
145 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
149 /* Return a variant of TYPE which has all the type qualifiers of LIKE
150 as well as those of TYPE. */
153 qualify_type (type, like)
156 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
157 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
158 return c_build_type_variant (type, constflag, volflag);
161 /* Return the common type of two types.
162 We assume that comptypes has already been done and returned 1;
163 if that isn't so, this may crash. In particular, we assume that qualifiers
166 This is the type for the result of most arithmetic operations
167 if the operands have the given two types. */
173 register enum tree_code code1;
174 register enum tree_code code2;
176 /* Save time if the two types are the same. */
178 if (t1 == t2) return t1;
180 /* If one type is nonsense, use the other. */
181 if (t1 == error_mark_node)
183 if (t2 == error_mark_node)
186 /* Treat an enum type as the unsigned integer type of the same width. */
188 if (TREE_CODE (t1) == ENUMERAL_TYPE)
189 t1 = type_for_size (TYPE_PRECISION (t1), 1);
190 if (TREE_CODE (t2) == ENUMERAL_TYPE)
191 t2 = type_for_size (TYPE_PRECISION (t2), 1);
193 code1 = TREE_CODE (t1);
194 code2 = TREE_CODE (t2);
196 /* If one type is complex, form the common type
197 of the non-complex components,
198 then make that complex. */
199 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
201 tree subtype1, subtype2, subtype;
202 if (code1 == COMPLEX_TYPE)
203 subtype1 = TREE_TYPE (t1);
206 if (code2 == COMPLEX_TYPE)
207 subtype2 = TREE_TYPE (t2);
210 subtype = common_type (subtype1, subtype2);
211 return build_complex_type (subtype);
218 /* If only one is real, use it as the result. */
220 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
223 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
226 /* Both real or both integers; use the one with greater precision. */
228 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
230 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
233 /* Same precision. Prefer longs to ints even when same size. */
235 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
236 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
237 return long_unsigned_type_node;
239 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
240 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
242 /* But preserve unsignedness from the other type,
243 since long cannot hold all the values of an unsigned int. */
244 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
245 return long_unsigned_type_node;
246 return long_integer_type_node;
249 /* Otherwise prefer the unsigned one. */
251 if (TREE_UNSIGNED (t1))
256 /* For two pointers, do this recursively on the target type,
257 and combine the qualifiers of the two types' targets. */
258 /* This code was turned off; I don't know why.
259 But ANSI C specifies doing this with the qualifiers.
260 So I turned it on again. */
262 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
263 TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
265 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
267 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
268 return build_pointer_type (c_build_type_variant (target, constp, volatilep));
271 return build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
276 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
277 /* Save space: see if the result is identical to one of the args. */
278 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
280 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
282 /* Merge the element types, and have a size if either arg has one. */
283 return build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
287 /* Function types: prefer the one that specified arg types.
288 If both do, merge the arg types. Also merge the return types. */
290 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
291 tree p1 = TYPE_ARG_TYPES (t1);
292 tree p2 = TYPE_ARG_TYPES (t2);
297 /* Save space: see if the result is identical to one of the args. */
298 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
300 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
303 /* Simple way if one arg fails to specify argument types. */
304 if (TYPE_ARG_TYPES (t1) == 0)
305 return build_function_type (valtype, TYPE_ARG_TYPES (t2));
306 if (TYPE_ARG_TYPES (t2) == 0)
307 return build_function_type (valtype, TYPE_ARG_TYPES (t1));
309 /* If both args specify argument types, we must merge the two
310 lists, argument by argument. */
312 len = list_length (p1);
315 for (i = 0; i < len; i++)
316 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
321 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
323 /* A null type means arg type is not specified.
324 Take whatever the other function type has. */
325 if (TREE_VALUE (p1) == 0)
327 TREE_VALUE (n) = TREE_VALUE (p2);
330 if (TREE_VALUE (p2) == 0)
332 TREE_VALUE (n) = TREE_VALUE (p1);
336 /* Given wait (union {union wait *u; int *i} *)
337 and wait (union wait *),
338 prefer union wait * as type of parm. */
339 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
340 && TREE_VALUE (p1) != TREE_VALUE (p2))
343 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
344 memb; memb = TREE_CHAIN (memb))
345 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
347 TREE_VALUE (n) = TREE_VALUE (p2);
349 pedwarn ("function types not truly compatible in ANSI C");
353 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
354 && TREE_VALUE (p2) != TREE_VALUE (p1))
357 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
358 memb; memb = TREE_CHAIN (memb))
359 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
361 TREE_VALUE (n) = TREE_VALUE (p1);
363 pedwarn ("function types not truly compatible in ANSI C");
367 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
371 return build_function_type (valtype, newargs);
380 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
381 or various other operations. Return 2 if they are compatible
382 but a warning may be needed if you use them together. */
385 comptypes (type1, type2)
388 register tree t1 = type1;
389 register tree t2 = type2;
391 /* Suppress errors caused by previously reported errors. */
393 if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
396 /* Treat an enum type as the unsigned integer type of the same width. */
398 if (TREE_CODE (t1) == ENUMERAL_TYPE)
399 t1 = type_for_size (TYPE_PRECISION (t1), 1);
400 if (TREE_CODE (t2) == ENUMERAL_TYPE)
401 t2 = type_for_size (TYPE_PRECISION (t2), 1);
406 /* Different classes of types can't be compatible. */
408 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
410 /* Qualifiers must match. */
412 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
414 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
417 /* Allow for two different type nodes which have essentially the same
418 definition. Note that we already checked for equality of the type
419 type qualifiers (just above). */
421 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
424 switch (TREE_CODE (t1))
427 return (TREE_TYPE (t1) == TREE_TYPE (t2)
428 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
431 return function_types_compatible_p (t1, t2);
435 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
437 tree d1 = TYPE_DOMAIN (t1);
438 tree d2 = TYPE_DOMAIN (t2);
440 /* Target types must match incl. qualifiers. */
441 if (TREE_TYPE (t1) != TREE_TYPE (t2)
442 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
445 /* Sizes must match unless one is missing or variable. */
446 if (d1 == 0 || d2 == 0 || d1 == d2
447 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
448 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
449 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
450 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
453 return (((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
454 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
455 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
456 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
457 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
458 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
459 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
460 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))))
465 if (maybe_objc_comptypes (t1, t2, 0) == 1)
471 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
472 ignoring their qualifiers. */
475 comp_target_types (ttl, ttr)
480 /* Give maybe_objc_comptypes a crack at letting these types through. */
481 if (val = maybe_objc_comptypes (ttl, ttr, 1) >= 0)
484 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
485 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
487 if (val == 2 && pedantic)
488 pedwarn ("types are not quite compatible");
492 /* Subroutines of `comptypes'. */
494 /* Return 1 if two function types F1 and F2 are compatible.
495 If either type specifies no argument types,
496 the other must specify a fixed number of self-promoting arg types.
497 Otherwise, if one type specifies only the number of arguments,
498 the other must specify that number of self-promoting arg types.
499 Otherwise, the argument types must match. */
502 function_types_compatible_p (f1, f2)
506 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
510 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
511 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
514 args1 = TYPE_ARG_TYPES (f1);
515 args2 = TYPE_ARG_TYPES (f2);
517 /* An unspecified parmlist matches any specified parmlist
518 whose argument types don't need default promotions. */
522 if (!self_promoting_args_p (args2))
524 /* If one of these types comes from a non-prototype fn definition,
525 compare that with the other type's arglist.
526 If they don't match, ask for a warning (but no error). */
527 if (TYPE_ACTUAL_ARG_TYPES (f1)
528 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
534 if (!self_promoting_args_p (args1))
536 if (TYPE_ACTUAL_ARG_TYPES (f2)
537 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
542 /* Both types have argument lists: compare them and propagate results. */
543 val1 = type_lists_compatible_p (args1, args2);
544 return val1 != 1 ? val1 : val;
547 /* Check two lists of types for compatibility,
548 returning 0 for incompatible, 1 for compatible,
549 or 2 for compatible with warning. */
552 type_lists_compatible_p (args1, args2)
555 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
561 if (args1 == 0 && args2 == 0)
563 /* If one list is shorter than the other,
564 they fail to match. */
565 if (args1 == 0 || args2 == 0)
567 /* A null pointer instead of a type
568 means there is supposed to be an argument
569 but nothing is specified about what type it has.
570 So match anything that self-promotes. */
571 if (TREE_VALUE (args1) == 0)
573 if (! self_promoting_type_p (TREE_VALUE (args2)))
576 else if (TREE_VALUE (args2) == 0)
578 if (! self_promoting_type_p (TREE_VALUE (args1)))
581 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
583 /* Allow wait (union {union wait *u; int *i} *)
584 and wait (union wait *) to be compatible. */
585 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
586 && TYPE_NAME (TREE_VALUE (args1)) == 0
587 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
588 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
589 TYPE_SIZE (TREE_VALUE (args2))))
592 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
593 memb; memb = TREE_CHAIN (memb))
594 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
599 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
600 && TYPE_NAME (TREE_VALUE (args2)) == 0
601 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
602 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
603 TYPE_SIZE (TREE_VALUE (args1))))
606 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
607 memb; memb = TREE_CHAIN (memb))
608 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
617 /* comptypes said ok, but record if it said to warn. */
621 args1 = TREE_CHAIN (args1);
622 args2 = TREE_CHAIN (args2);
626 /* Return 1 if PARMS specifies a fixed number of parameters
627 and none of their types is affected by default promotions. */
630 self_promoting_args_p (parms)
634 for (t = parms; t; t = TREE_CHAIN (t))
636 register tree type = TREE_VALUE (t);
638 if (TREE_CHAIN (t) == 0 && type != void_type_node)
644 if (TYPE_MAIN_VARIANT (type) == float_type_node)
647 if (C_PROMOTING_INTEGER_TYPE_P (type))
653 /* Return 1 if TYPE is not affected by default promotions. */
656 self_promoting_type_p (type)
659 if (TYPE_MAIN_VARIANT (type) == float_type_node)
662 if (C_PROMOTING_INTEGER_TYPE_P (type))
668 /* Return an unsigned type the same as TYPE in other respects. */
674 tree type1 = TYPE_MAIN_VARIANT (type);
675 if (type1 == signed_char_type_node || type1 == char_type_node)
676 return unsigned_char_type_node;
677 if (type1 == integer_type_node)
678 return unsigned_type_node;
679 if (type1 == short_integer_type_node)
680 return short_unsigned_type_node;
681 if (type1 == long_integer_type_node)
682 return long_unsigned_type_node;
683 if (type1 == long_long_integer_type_node)
684 return long_long_unsigned_type_node;
688 /* Return a signed type the same as TYPE in other respects. */
694 tree type1 = TYPE_MAIN_VARIANT (type);
695 if (type1 == unsigned_char_type_node || type1 == char_type_node)
696 return signed_char_type_node;
697 if (type1 == unsigned_type_node)
698 return integer_type_node;
699 if (type1 == short_unsigned_type_node)
700 return short_integer_type_node;
701 if (type1 == long_unsigned_type_node)
702 return long_integer_type_node;
703 if (type1 == long_long_unsigned_type_node)
704 return long_long_integer_type_node;
708 /* Return a type the same as TYPE except unsigned or
709 signed according to UNSIGNEDP. */
712 signed_or_unsigned_type (unsignedp, type)
716 if (TREE_CODE (type) != INTEGER_TYPE)
718 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
719 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
720 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
721 return unsignedp ? unsigned_type_node : integer_type_node;
722 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
723 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
724 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
725 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
726 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
727 return (unsignedp ? long_long_unsigned_type_node
728 : long_long_integer_type_node);
732 /* Compute the value of the `sizeof' operator. */
738 enum tree_code code = TREE_CODE (type);
741 if (code == FUNCTION_TYPE)
743 if (pedantic || warn_pointer_arith)
744 pedwarn ("sizeof applied to a function type");
747 if (code == VOID_TYPE)
749 if (pedantic || warn_pointer_arith)
750 pedwarn ("sizeof applied to a void type");
753 if (code == ERROR_MARK)
755 if (TYPE_SIZE (type) == 0)
757 error ("sizeof applied to an incomplete type");
761 /* Convert in case a char is more than one unit. */
762 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
763 size_int (TYPE_PRECISION (char_type_node)));
764 /* size_binop does not put the constant in range, so do it now. */
765 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
766 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
771 c_sizeof_nowarn (type)
774 enum tree_code code = TREE_CODE (type);
777 if (code == FUNCTION_TYPE
779 || code == ERROR_MARK)
781 if (TYPE_SIZE (type) == 0)
784 /* Convert in case a char is more than one unit. */
785 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
786 size_int (TYPE_PRECISION (char_type_node)));
787 force_fit_type (t, 0);
791 /* Compute the size to increment a pointer by. */
794 c_size_in_bytes (type)
797 enum tree_code code = TREE_CODE (type);
800 if (code == FUNCTION_TYPE)
802 if (code == VOID_TYPE)
804 if (code == ERROR_MARK)
806 if (TYPE_SIZE (type) == 0)
808 error ("arithmetic on pointer to an incomplete type");
812 /* Convert in case a char is more than one unit. */
813 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
814 size_int (BITS_PER_UNIT));
815 force_fit_type (t, 0);
819 /* Implement the __alignof keyword: Return the minimum required
820 alignment of TYPE, measured in bytes. */
826 enum tree_code code = TREE_CODE (type);
828 if (code == FUNCTION_TYPE)
829 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
831 if (code == VOID_TYPE || code == ERROR_MARK)
834 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
837 /* Implement the __alignof keyword: Return the minimum required
838 alignment of EXPR, measured in bytes. For VAR_DECL's and
839 FIELD_DECL's return DECL_ALIGN (which can be set from an
840 "aligned" __attribute__ specification). */
843 c_alignof_expr (expr)
846 if (TREE_CODE (expr) == VAR_DECL)
847 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
849 if (TREE_CODE (expr) == COMPONENT_REF
850 && DECL_BIT_FIELD (TREE_OPERAND (expr, 1)))
852 error ("`__alignof' applied to a bit-field");
855 else if (TREE_CODE (expr) == COMPONENT_REF
856 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
857 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
859 if (TREE_CODE (expr) == INDIRECT_REF)
861 tree t = TREE_OPERAND (expr, 0);
863 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
865 while (TREE_CODE (t) == NOP_EXPR
866 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
870 t = TREE_OPERAND (t, 0);
871 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
872 if (thisalign > bestalign)
873 best = t, bestalign = thisalign;
875 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
878 return c_alignof (TREE_TYPE (expr));
880 /* Return either DECL or its known constant value (if it has one). */
883 decl_constant_value (decl)
886 if (! TREE_PUBLIC (decl)
887 /* Don't change a variable array bound or initial value to a constant
888 in a place where a variable is invalid. */
889 && current_function_decl != 0
891 && ! TREE_THIS_VOLATILE (decl)
892 && DECL_INITIAL (decl) != 0
893 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
894 /* This is invalid if initial value is not constant.
895 If it has either a function call, a memory reference,
896 or a variable, then re-evaluating it could give different results. */
897 && TREE_CONSTANT (DECL_INITIAL (decl))
898 /* Check for cases where this is sub-optimal, even though valid. */
899 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
900 && DECL_MODE (decl) != BLKmode)
901 return DECL_INITIAL (decl);
905 /* Perform default promotions for C data used in expressions.
906 Arrays and functions are converted to pointers;
907 enumeral types or short or char, to int.
908 In addition, manifest constants symbols are replaced by their values. */
911 default_conversion (exp)
914 register tree type = TREE_TYPE (exp);
915 register enum tree_code code = TREE_CODE (type);
917 /* Constants can be used directly unless they're not loadable. */
918 if (TREE_CODE (exp) == CONST_DECL)
919 exp = DECL_INITIAL (exp);
920 /* Replace a nonvolatile const static variable with its value. */
922 && TREE_CODE (exp) == VAR_DECL
923 && TREE_READONLY (exp)
924 /* But not for iterators! */
926 && DECL_MODE (exp) != BLKmode)
928 exp = decl_constant_value (exp);
929 type = TREE_TYPE (exp);
932 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
934 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
935 to integer and cause infinite recursion. */
936 while (TREE_CODE (exp) == NON_LVALUE_EXPR
937 || (TREE_CODE (exp) == NOP_EXPR
938 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
939 exp = TREE_OPERAND (exp, 0);
941 /* Normally convert enums to int,
942 but convert wide enums to something wider. */
943 if (code == ENUMERAL_TYPE)
945 type = type_for_size (MAX (TYPE_PRECISION (type),
946 TYPE_PRECISION (integer_type_node)),
948 || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node))
949 && TREE_UNSIGNED (type)));
950 return convert (type, exp);
953 if (C_PROMOTING_INTEGER_TYPE_P (type))
955 /* Traditionally, unsignedness is preserved in default promotions.
956 Also preserve unsignedness if not really getting any wider. */
957 if (TREE_UNSIGNED (type)
959 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
960 return convert (unsigned_type_node, exp);
961 return convert (integer_type_node, exp);
963 if (flag_traditional && !flag_allow_single_precision
964 && TYPE_MAIN_VARIANT (type) == float_type_node)
965 return convert (double_type_node, exp);
966 if (code == VOID_TYPE)
968 error ("void value not ignored as it ought to be");
969 return error_mark_node;
971 if (code == FUNCTION_TYPE)
973 return build_unary_op (ADDR_EXPR, exp, 0);
975 if (code == ARRAY_TYPE)
978 tree restype = TREE_TYPE (type);
983 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
984 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
986 constp = TREE_READONLY (exp);
987 volatilep = TREE_THIS_VOLATILE (exp);
990 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
991 || constp || volatilep)
992 restype = c_build_type_variant (restype,
993 TYPE_READONLY (type) || constp,
994 TYPE_VOLATILE (type) || volatilep);
996 if (TREE_CODE (exp) == INDIRECT_REF)
997 return convert (TYPE_POINTER_TO (restype),
998 TREE_OPERAND (exp, 0));
1000 if (TREE_CODE (exp) == COMPOUND_EXPR)
1002 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1003 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1004 TREE_OPERAND (exp, 0), op1);
1008 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1010 error ("invalid use of non-lvalue array");
1011 return error_mark_node;
1014 ptrtype = build_pointer_type (restype);
1016 if (TREE_CODE (exp) == VAR_DECL)
1018 /* ??? This is not really quite correct
1019 in that the type of the operand of ADDR_EXPR
1020 is not the target type of the type of the ADDR_EXPR itself.
1021 Question is, can this lossage be avoided? */
1022 adr = build1 (ADDR_EXPR, ptrtype, exp);
1023 if (mark_addressable (exp) == 0)
1024 return error_mark_node;
1025 TREE_CONSTANT (adr) = staticp (exp);
1026 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1029 /* This way is better for a COMPONENT_REF since it can
1030 simplify the offset for a component. */
1031 adr = build_unary_op (ADDR_EXPR, exp, 1);
1032 return convert (ptrtype, adr);
1037 /* Look up component name in the structure type definition.
1039 If this component name is found indirectly within an anonymous union,
1040 store in *INDIRECT the component which directly contains
1041 that anonymous union. Otherwise, set *INDIRECT to 0. */
1044 lookup_field (type, component, indirect)
1045 tree type, component;
1050 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1051 to the field elements. Use a binary search on this array to quickly
1052 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1053 will always be set for structures which have many elements. */
1055 if (TYPE_LANG_SPECIFIC (type))
1058 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1060 field = TYPE_FIELDS (type);
1062 top = TYPE_LANG_SPECIFIC (type)->len;
1063 while (top - bot > 1)
1067 half = (top - bot + 1) >> 1;
1068 field = field_array[bot+half];
1070 if (DECL_NAME (field) == NULL_TREE)
1072 /* Step through all anon unions in linear fashion. */
1073 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1077 field = field_array[bot++];
1078 anon = lookup_field (TREE_TYPE (field), component, &junk);
1079 if (anon != NULL_TREE)
1086 /* Entire record is only anon unions. */
1090 /* Restart the binary search, with new lower bound. */
1094 cmp = (HOST_WIDE_INT) DECL_NAME (field) - (HOST_WIDE_INT) component;
1103 if (DECL_NAME (field_array[bot]) == component)
1104 field = field_array[bot];
1105 else if (DECL_NAME (field) != component)
1110 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1112 if (DECL_NAME (field) == NULL_TREE)
1115 tree anon = lookup_field (TREE_TYPE (field), component, &junk);
1116 if (anon != NULL_TREE)
1123 if (DECL_NAME (field) == component)
1128 *indirect = NULL_TREE;
1132 /* Make an expression to refer to the COMPONENT field of
1133 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1136 build_component_ref (datum, component)
1137 tree datum, component;
1139 register tree type = TREE_TYPE (datum);
1140 register enum tree_code code = TREE_CODE (type);
1141 register tree field = NULL;
1144 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1145 unless we are not to support things not strictly ANSI. */
1146 switch (TREE_CODE (datum))
1150 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1151 return build (COMPOUND_EXPR, TREE_TYPE (value),
1152 TREE_OPERAND (datum, 0), value);
1155 return build_conditional_expr
1156 (TREE_OPERAND (datum, 0),
1157 build_component_ref (TREE_OPERAND (datum, 1), component),
1158 build_component_ref (TREE_OPERAND (datum, 2), component));
1161 /* See if there is a field or component with name COMPONENT. */
1163 if (code == RECORD_TYPE || code == UNION_TYPE)
1167 if (TYPE_SIZE (type) == 0)
1169 incomplete_type_error (NULL_TREE, type);
1170 return error_mark_node;
1173 field = lookup_field (type, component, &indirect);
1177 error (code == RECORD_TYPE
1178 ? "structure has no member named `%s'"
1179 : "union has no member named `%s'",
1180 IDENTIFIER_POINTER (component));
1181 return error_mark_node;
1183 if (TREE_TYPE (field) == error_mark_node)
1184 return error_mark_node;
1186 /* If FIELD was found buried within an anonymous union,
1187 make one COMPONENT_REF to get that anonymous union,
1188 then fall thru to make a second COMPONENT_REF to get FIELD. */
1191 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1192 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1193 TREE_READONLY (ref) = 1;
1194 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1195 TREE_THIS_VOLATILE (ref) = 1;
1199 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1201 if (TREE_READONLY (datum) || TREE_READONLY (field))
1202 TREE_READONLY (ref) = 1;
1203 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1204 TREE_THIS_VOLATILE (ref) = 1;
1208 else if (code != ERROR_MARK)
1209 error ("request for member `%s' in something not a structure or union",
1210 IDENTIFIER_POINTER (component));
1212 return error_mark_node;
1215 /* Given an expression PTR for a pointer, return an expression
1216 for the value pointed to.
1217 ERRORSTRING is the name of the operator to appear in error messages. */
1220 build_indirect_ref (ptr, errorstring)
1224 register tree pointer = default_conversion (ptr);
1225 register tree type = TREE_TYPE (pointer);
1227 if (TREE_CODE (type) == POINTER_TYPE)
1229 if (TREE_CODE (pointer) == ADDR_EXPR
1231 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1232 == TREE_TYPE (type)))
1233 return TREE_OPERAND (pointer, 0);
1236 tree t = TREE_TYPE (type);
1237 register tree ref = build1 (INDIRECT_REF,
1238 TYPE_MAIN_VARIANT (t), pointer);
1240 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1242 error ("dereferencing pointer to incomplete type");
1243 return error_mark_node;
1245 if (TREE_CODE (t) == VOID_TYPE)
1246 warning ("dereferencing `void *' pointer");
1248 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1249 so that we get the proper error message if the result is used
1250 to assign to. Also, &* is supposed to be a no-op.
1251 And ANSI C seems to specify that the type of the result
1252 should be the const type. */
1253 /* A de-reference of a pointer to const is not a const. It is valid
1254 to change it via some other pointer. */
1255 TREE_READONLY (ref) = TYPE_READONLY (t);
1256 TREE_SIDE_EFFECTS (ref)
1257 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1258 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1262 else if (TREE_CODE (pointer) != ERROR_MARK)
1263 error ("invalid type argument of `%s'", errorstring);
1264 return error_mark_node;
1267 /* This handles expressions of the form "a[i]", which denotes
1270 This is logically equivalent in C to *(a+i), but we may do it differently.
1271 If A is a variable or a member, we generate a primitive ARRAY_REF.
1272 This avoids forcing the array out of registers, and can work on
1273 arrays that are not lvalues (for example, members of structures returned
1277 build_array_ref (array, index)
1282 error ("subscript missing in array reference");
1283 return error_mark_node;
1286 if (TREE_TYPE (array) == error_mark_node
1287 || TREE_TYPE (index) == error_mark_node)
1288 return error_mark_node;
1290 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1291 && TREE_CODE (array) != INDIRECT_REF)
1295 /* Subscripting with type char is likely to lose
1296 on a machine where chars are signed.
1297 So warn on any machine, but optionally.
1298 Don't warn for unsigned char since that type is safe.
1299 Don't warn for signed char because anyone who uses that
1300 must have done so deliberately. */
1301 if (warn_char_subscripts
1302 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1303 warning ("array subscript has type `char'");
1305 /* Apply default promotions *after* noticing character types. */
1306 index = default_conversion (index);
1308 /* Require integer *after* promotion, for sake of enums. */
1309 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1311 error ("array subscript is not an integer");
1312 return error_mark_node;
1315 /* An array that is indexed by a non-constant
1316 cannot be stored in a register; we must be able to do
1317 address arithmetic on its address.
1318 Likewise an array of elements of variable size. */
1319 if (TREE_CODE (index) != INTEGER_CST
1320 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1321 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1323 if (mark_addressable (array) == 0)
1324 return error_mark_node;
1326 /* An array that is indexed by a constant value which is not within
1327 the array bounds cannot be stored in a register either; because we
1328 would get a crash in store_bit_field/extract_bit_field when trying
1329 to access a non-existent part of the register. */
1330 if (TREE_CODE (index) == INTEGER_CST
1331 && TYPE_VALUES (TREE_TYPE (array))
1332 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1334 if (mark_addressable (array) == 0)
1335 return error_mark_node;
1338 if (pedantic && !lvalue_p (array))
1340 if (DECL_REGISTER (array))
1341 pedwarn ("ANSI C forbids subscripting `register' array");
1343 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1349 while (TREE_CODE (foo) == COMPONENT_REF)
1350 foo = TREE_OPERAND (foo, 0);
1351 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1352 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1355 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1356 rval = build (ARRAY_REF, type, array, index);
1357 /* Array ref is const/volatile if the array elements are
1358 or if the array is. */
1359 TREE_READONLY (rval)
1360 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1361 | TREE_READONLY (array));
1362 TREE_SIDE_EFFECTS (rval)
1363 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1364 | TREE_SIDE_EFFECTS (array));
1365 TREE_THIS_VOLATILE (rval)
1366 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1367 /* This was added by rms on 16 Nov 91.
1368 It fixes vol struct foo *a; a->elts[1]
1369 in an inline function.
1370 Hope it doesn't break something else. */
1371 | TREE_THIS_VOLATILE (array));
1372 return require_complete_type (fold (rval));
1376 tree ar = default_conversion (array);
1377 tree ind = default_conversion (index);
1379 /* Put the integer in IND to simplify error checking. */
1380 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1387 if (ar == error_mark_node)
1390 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
1392 error ("subscripted value is neither array nor pointer");
1393 return error_mark_node;
1395 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1397 error ("array subscript is not an integer");
1398 return error_mark_node;
1401 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1406 /* Build a function call to function FUNCTION with parameters PARAMS.
1407 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1408 TREE_VALUE of each node is a parameter-expression.
1409 FUNCTION's data type may be a function type or a pointer-to-function. */
1412 build_function_call (function, params)
1413 tree function, params;
1415 register tree fntype, fundecl = 0;
1416 register tree coerced_params;
1417 tree name = NULL_TREE, assembler_name = NULL_TREE;
1419 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1420 STRIP_TYPE_NOPS (function);
1422 /* Convert anything with function type to a pointer-to-function. */
1423 if (TREE_CODE (function) == FUNCTION_DECL)
1425 name = DECL_NAME (function);
1426 assembler_name = DECL_ASSEMBLER_NAME (function);
1428 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1429 (because calling an inline function does not mean the function
1430 needs to be separately compiled). */
1431 fntype = build_type_variant (TREE_TYPE (function),
1432 TREE_READONLY (function),
1433 TREE_THIS_VOLATILE (function));
1435 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1438 function = default_conversion (function);
1440 fntype = TREE_TYPE (function);
1442 if (TREE_CODE (fntype) == ERROR_MARK)
1443 return error_mark_node;
1445 if (!(TREE_CODE (fntype) == POINTER_TYPE
1446 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1448 error ("called object is not a function");
1449 return error_mark_node;
1452 /* fntype now gets the type of function pointed to. */
1453 fntype = TREE_TYPE (fntype);
1455 /* Convert the parameters to the types declared in the
1456 function prototype, or apply default promotions. */
1459 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1461 /* Check for errors in format strings. */
1463 if (warn_format && (name || assembler_name))
1464 check_function_format (name, assembler_name, coerced_params);
1466 /* Recognize certain built-in functions so we can make tree-codes
1467 other than CALL_EXPR. We do this when it enables fold-const.c
1468 to do something useful. */
1470 if (TREE_CODE (function) == ADDR_EXPR
1471 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1472 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1473 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1478 if (coerced_params == 0)
1479 return integer_zero_node;
1480 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1484 register tree result
1485 = build (CALL_EXPR, TREE_TYPE (fntype),
1486 function, coerced_params, NULL_TREE);
1488 TREE_SIDE_EFFECTS (result) = 1;
1489 if (TREE_TYPE (result) == void_type_node)
1491 return require_complete_type (result);
1495 /* Convert the argument expressions in the list VALUES
1496 to the types in the list TYPELIST. The result is a list of converted
1497 argument expressions.
1499 If TYPELIST is exhausted, or when an element has NULL as its type,
1500 perform the default conversions.
1502 PARMLIST is the chain of parm decls for the function being called.
1503 It may be 0, if that info is not available.
1504 It is used only for generating error messages.
1506 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1508 This is also where warnings about wrong number of args are generated.
1510 Both VALUES and the returned value are chains of TREE_LIST nodes
1511 with the elements of the list in the TREE_VALUE slots of those nodes. */
1514 convert_arguments (typelist, values, name, fundecl)
1515 tree typelist, values, name, fundecl;
1517 register tree typetail, valtail;
1518 register tree result = NULL;
1521 /* Scan the given expressions and types, producing individual
1522 converted arguments and pushing them on RESULT in reverse order. */
1524 for (valtail = values, typetail = typelist, parmnum = 0;
1526 valtail = TREE_CHAIN (valtail), parmnum++)
1528 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1529 register tree val = TREE_VALUE (valtail);
1531 if (type == void_type_node)
1534 error ("too many arguments to function `%s'",
1535 IDENTIFIER_POINTER (name));
1537 error ("too many arguments to function");
1541 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1542 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1543 to convert automatically to a pointer. */
1544 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1545 val = TREE_OPERAND (val, 0);
1547 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1548 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1549 val = default_conversion (val);
1551 val = require_complete_type (val);
1555 /* Formal parm type is specified by a function prototype. */
1558 if (TYPE_SIZE (type) == 0)
1560 error ("type of formal parameter %d is incomplete", parmnum + 1);
1567 #ifdef PROMOTE_PROTOTYPES
1568 /* Rather than truncating and then reextending,
1569 convert directly to int, if that's the type we will want. */
1570 if (! flag_traditional
1571 && (TREE_CODE (type) == INTEGER_TYPE
1572 || TREE_CODE (type) == ENUMERAL_TYPE)
1573 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1574 type = integer_type_node;
1577 #if 0 /* This turns out not to win--there's no way to write a prototype
1578 for a function whose arg type is a union with no tag. */
1579 /* Nameless union automatically casts the types it contains. */
1580 if (TREE_CODE (type) == UNION_TYPE && TYPE_NAME (type) == 0)
1584 for (field = TYPE_FIELDS (type); field;
1585 field = TREE_CHAIN (field))
1586 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
1587 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1591 val = build1 (CONVERT_EXPR, type, val);
1595 /* Optionally warn about conversions that
1596 differ from the default conversions. */
1597 if (warn_conversion)
1599 int formal_prec = TYPE_PRECISION (type);
1601 if (TREE_CODE (type) != REAL_TYPE
1602 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1603 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1604 else if (TREE_CODE (type) == REAL_TYPE
1605 && TREE_CODE (TREE_TYPE (val)) != REAL_TYPE)
1606 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1607 else if (TREE_CODE (type) == REAL_TYPE
1608 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1610 /* Warn if any argument is passed as `float',
1611 since without a prototype it would be `double'. */
1612 if (formal_prec == TYPE_PRECISION (float_type_node))
1613 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1615 /* Detect integer changing in width or signedness. */
1616 else if ((TREE_CODE (type) == INTEGER_TYPE
1617 || TREE_CODE (type) == ENUMERAL_TYPE)
1618 && (TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
1619 || TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE))
1621 tree would_have_been = default_conversion (val);
1622 tree type1 = TREE_TYPE (would_have_been);
1624 if (TREE_CODE (type) == ENUMERAL_TYPE
1625 && type == TREE_TYPE (val))
1626 /* No warning if function asks for enum
1627 and the actual arg is that enum type. */
1629 else if (formal_prec != TYPE_PRECISION (type1))
1630 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1631 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1633 /* Don't complain if the formal parameter type
1634 is an enum, because we can't tell now whether
1635 the value was an enum--even the same enum. */
1636 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1638 else if (TREE_CODE (val) == INTEGER_CST
1639 && int_fits_type_p (val, type))
1640 /* Change in signedness doesn't matter
1641 if a constant value is unaffected. */
1643 /* Likewise for a constant in a NOP_EXPR. */
1644 else if (TREE_CODE (val) == NOP_EXPR
1645 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1646 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1648 #if 0 /* We never get such tree structure here. */
1649 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1650 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1651 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1652 /* Change in signedness doesn't matter
1653 if an enum value is unaffected. */
1656 /* If the value is extended from a narrower
1657 unsigned type, it doesn't matter whether we
1658 pass it as signed or unsigned; the value
1659 certainly is the same either way. */
1660 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1661 && TREE_UNSIGNED (TREE_TYPE (val)))
1663 else if (TREE_UNSIGNED (type))
1664 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1666 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1670 parmval = convert_for_assignment (type, val,
1671 (char *)0, /* arg passing */
1672 fundecl, name, parmnum + 1);
1674 #ifdef PROMOTE_PROTOTYPES
1675 if ((TREE_CODE (type) == INTEGER_TYPE
1676 || TREE_CODE (type) == ENUMERAL_TYPE)
1677 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1678 parmval = default_conversion (parmval);
1681 result = tree_cons (NULL_TREE, parmval, result);
1683 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1684 && (TYPE_PRECISION (TREE_TYPE (val))
1685 < TYPE_PRECISION (double_type_node)))
1686 /* Convert `float' to `double'. */
1687 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1689 /* Convert `short' and `char' to full-size `int'. */
1690 result = tree_cons (NULL_TREE, default_conversion (val), result);
1693 typetail = TREE_CHAIN (typetail);
1696 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1699 error ("too few arguments to function `%s'",
1700 IDENTIFIER_POINTER (name));
1702 error ("too few arguments to function");
1705 return nreverse (result);
1708 /* This is the entry point used by the parser
1709 for binary operators in the input.
1710 In addition to constructing the expression,
1711 we check for operands that were written with other binary operators
1712 in a way that is likely to confuse the user. */
1715 parser_build_binary_op (code, arg1, arg2)
1716 enum tree_code code;
1719 tree result = build_binary_op (code, arg1, arg2, 1);
1722 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1723 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1724 enum tree_code code1 = ERROR_MARK;
1725 enum tree_code code2 = ERROR_MARK;
1727 if (class1 == 'e' || class1 == '1'
1728 || class1 == '2' || class1 == '<')
1729 code1 = C_EXP_ORIGINAL_CODE (arg1);
1730 if (class2 == 'e' || class2 == '1'
1731 || class2 == '2' || class2 == '<')
1732 code2 = C_EXP_ORIGINAL_CODE (arg2);
1734 /* Check for cases such as x+y<<z which users are likely
1735 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1736 is cleared to prevent these warnings. */
1737 if (warn_parentheses)
1739 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1741 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1742 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1743 warning ("suggest parentheses around + or - inside shift");
1746 if (code == TRUTH_ORIF_EXPR)
1748 if (code1 == TRUTH_ANDIF_EXPR
1749 || code2 == TRUTH_ANDIF_EXPR)
1750 warning ("suggest parentheses around && within ||");
1753 if (code == BIT_IOR_EXPR)
1755 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1756 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1757 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1758 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1759 warning ("suggest parentheses around arithmetic in operand of |");
1762 if (code == BIT_XOR_EXPR)
1764 if (code1 == BIT_AND_EXPR
1765 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1766 || code2 == BIT_AND_EXPR
1767 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1768 warning ("suggest parentheses around arithmetic in operand of ^");
1771 if (code == BIT_AND_EXPR)
1773 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1774 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1775 warning ("suggest parentheses around + or - in operand of &");
1779 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1780 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1781 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1782 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1784 unsigned_conversion_warning (result, arg1);
1785 unsigned_conversion_warning (result, arg2);
1786 overflow_warning (result);
1788 class = TREE_CODE_CLASS (TREE_CODE (result));
1790 /* Record the code that was specified in the source,
1791 for the sake of warnings about confusing nesting. */
1792 if (class == 'e' || class == '1'
1793 || class == '2' || class == '<')
1794 C_SET_EXP_ORIGINAL_CODE (result, code);
1797 int flag = TREE_CONSTANT (result);
1798 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1799 so that convert_for_assignment wouldn't strip it.
1800 That way, we got warnings for things like p = (1 - 1).
1801 But it turns out we should not get those warnings. */
1802 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1803 C_SET_EXP_ORIGINAL_CODE (result, code);
1804 TREE_CONSTANT (result) = flag;
1810 /* Build a binary-operation expression without default conversions.
1811 CODE is the kind of expression to build.
1812 This function differs from `build' in several ways:
1813 the data type of the result is computed and recorded in it,
1814 warnings are generated if arg data types are invalid,
1815 special handling for addition and subtraction of pointers is known,
1816 and some optimization is done (operations on narrow ints
1817 are done in the narrower type when that gives the same result).
1818 Constant folding is also done before the result is returned.
1820 Note that the operands will never have enumeral types, or function
1821 or array types, because either they will have the default conversions
1822 performed or they have both just been converted to some other type in which
1823 the arithmetic is to be done. */
1826 build_binary_op (code, orig_op0, orig_op1, convert_p)
1827 enum tree_code code;
1828 tree orig_op0, orig_op1;
1832 register enum tree_code code0, code1;
1835 /* Expression code to give to the expression when it is built.
1836 Normally this is CODE, which is what the caller asked for,
1837 but in some special cases we change it. */
1838 register enum tree_code resultcode = code;
1840 /* Data type in which the computation is to be performed.
1841 In the simplest cases this is the common type of the arguments. */
1842 register tree result_type = NULL;
1844 /* Nonzero means operands have already been type-converted
1845 in whatever way is necessary.
1846 Zero means they need to be converted to RESULT_TYPE. */
1849 /* Nonzero means after finally constructing the expression
1850 give it this type. Otherwise, give it type RESULT_TYPE. */
1851 tree final_type = 0;
1853 /* Nonzero if this is an operation like MIN or MAX which can
1854 safely be computed in short if both args are promoted shorts.
1855 Also implies COMMON.
1856 -1 indicates a bitwise operation; this makes a difference
1857 in the exact conditions for when it is safe to do the operation
1858 in a narrower mode. */
1861 /* Nonzero if this is a comparison operation;
1862 if both args are promoted shorts, compare the original shorts.
1863 Also implies COMMON. */
1864 int short_compare = 0;
1866 /* Nonzero if this is a right-shift operation, which can be computed on the
1867 original short and then promoted if the operand is a promoted short. */
1868 int short_shift = 0;
1870 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1875 op0 = default_conversion (orig_op0);
1876 op1 = default_conversion (orig_op1);
1884 type0 = TREE_TYPE (op0);
1885 type1 = TREE_TYPE (op1);
1887 /* The expression codes of the data types of the arguments tell us
1888 whether the arguments are integers, floating, pointers, etc. */
1889 code0 = TREE_CODE (type0);
1890 code1 = TREE_CODE (type1);
1892 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1893 STRIP_TYPE_NOPS (op0);
1894 STRIP_TYPE_NOPS (op1);
1896 /* If an error was already reported for one of the arguments,
1897 avoid reporting another error. */
1899 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1900 return error_mark_node;
1905 /* Handle the pointer + int case. */
1906 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1907 return pointer_int_sum (PLUS_EXPR, op0, op1);
1908 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1909 return pointer_int_sum (PLUS_EXPR, op1, op0);
1915 /* Subtraction of two similar pointers.
1916 We must subtract them as integers, then divide by object size. */
1917 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1918 && comp_target_types (type0, type1))
1919 return pointer_diff (op0, op1);
1920 /* Handle pointer minus int. Just like pointer plus int. */
1921 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1922 return pointer_int_sum (MINUS_EXPR, op0, op1);
1931 case TRUNC_DIV_EXPR:
1933 case FLOOR_DIV_EXPR:
1934 case ROUND_DIV_EXPR:
1935 case EXACT_DIV_EXPR:
1936 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1937 || code0 == COMPLEX_TYPE)
1938 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1939 || code1 == COMPLEX_TYPE))
1941 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
1942 resultcode = RDIV_EXPR;
1944 /* When dividing two signed integers, you have to promote to int.
1945 E.g. (short) -32868 / (short) -1 doesn't fit in a short. */
1946 shorten = TREE_UNSIGNED (orig_op0);
1952 case BIT_ANDTC_EXPR:
1955 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1957 /* If one operand is a constant, and the other is a short type
1958 that has been converted to an int,
1959 really do the work in the short type and then convert the
1960 result to int. If we are lucky, the constant will be 0 or 1
1961 in the short type, making the entire operation go away. */
1962 if (TREE_CODE (op0) == INTEGER_CST
1963 && TREE_CODE (op1) == NOP_EXPR
1964 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
1965 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
1967 final_type = result_type;
1968 op1 = TREE_OPERAND (op1, 0);
1969 result_type = TREE_TYPE (op1);
1971 if (TREE_CODE (op1) == INTEGER_CST
1972 && TREE_CODE (op0) == NOP_EXPR
1973 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
1974 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
1976 final_type = result_type;
1977 op0 = TREE_OPERAND (op0, 0);
1978 result_type = TREE_TYPE (op0);
1982 case TRUNC_MOD_EXPR:
1983 case FLOOR_MOD_EXPR:
1984 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1986 /* Although it would be tempting to shorten always here, that loses
1987 on some targets, since the modulo instruction is undefined if the
1988 quotient can't be represented in the computation mode. We shorten
1989 only if unsigned or if dividing by something we know != -1. */
1990 shorten = (TREE_UNSIGNED (orig_op0)
1991 || (TREE_CODE (op1) == INTEGER_CST
1992 && (TREE_INT_CST_LOW (op1) != -1
1993 || TREE_INT_CST_HIGH (op1) != -1)));
1998 case TRUTH_ANDIF_EXPR:
1999 case TRUTH_ORIF_EXPR:
2000 case TRUTH_AND_EXPR:
2002 case TRUTH_XOR_EXPR:
2003 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2004 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2005 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2006 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2008 /* Result of these operations is always an int,
2009 but that does not mean the operands should be
2010 converted to ints! */
2011 result_type = integer_type_node;
2012 op0 = truthvalue_conversion (op0);
2013 op1 = truthvalue_conversion (op1);
2018 /* Shift operations: result has same type as first operand;
2019 always convert second operand to int.
2020 Also set SHORT_SHIFT if shifting rightward. */
2023 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2025 if (TREE_CODE (op1) == INTEGER_CST)
2027 if (tree_int_cst_lt (op1, integer_zero_node))
2028 warning ("right shift count is negative");
2031 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2033 if (TREE_INT_CST_HIGH (op1) != 0
2034 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2035 >= TYPE_PRECISION (type0)))
2036 warning ("right shift count >= width of type");
2039 /* Use the type of the value to be shifted.
2040 This is what most traditional C compilers do. */
2041 result_type = type0;
2042 /* Unless traditional, convert the shift-count to an integer,
2043 regardless of size of value being shifted. */
2044 if (! flag_traditional)
2046 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2047 op1 = convert (integer_type_node, op1);
2048 /* Avoid converting op1 to result_type later. */
2055 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2057 if (TREE_CODE (op1) == INTEGER_CST)
2059 if (tree_int_cst_lt (op1, integer_zero_node))
2060 warning ("left shift count is negative");
2061 else if (TREE_INT_CST_HIGH (op1) != 0
2062 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2063 >= TYPE_PRECISION (type0)))
2064 warning ("left shift count >= width of type");
2066 /* Use the type of the value to be shifted.
2067 This is what most traditional C compilers do. */
2068 result_type = type0;
2069 /* Unless traditional, convert the shift-count to an integer,
2070 regardless of size of value being shifted. */
2071 if (! flag_traditional)
2073 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2074 op1 = convert (integer_type_node, op1);
2075 /* Avoid converting op1 to result_type later. */
2083 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2085 if (TREE_CODE (op1) == INTEGER_CST)
2087 if (tree_int_cst_lt (op1, integer_zero_node))
2088 warning ("shift count is negative");
2089 else if (TREE_INT_CST_HIGH (op1) != 0
2090 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2091 >= TYPE_PRECISION (type0)))
2092 warning ("shift count >= width of type");
2094 /* Use the type of the value to be shifted.
2095 This is what most traditional C compilers do. */
2096 result_type = type0;
2097 /* Unless traditional, convert the shift-count to an integer,
2098 regardless of size of value being shifted. */
2099 if (! flag_traditional)
2101 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2102 op1 = convert (integer_type_node, op1);
2103 /* Avoid converting op1 to result_type later. */
2111 /* Result of comparison is always int,
2112 but don't convert the args to int! */
2113 result_type = integer_type_node;
2115 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2116 || code0 == COMPLEX_TYPE)
2117 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2118 || code1 == COMPLEX_TYPE))
2120 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2122 register tree tt0 = TREE_TYPE (type0);
2123 register tree tt1 = TREE_TYPE (type1);
2124 /* Anything compares with void *. void * compares with anything.
2125 Otherwise, the targets must be compatible
2126 and both must be object or both incomplete. */
2127 if (comp_target_types (type0, type1))
2129 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2131 /* op0 != orig_op0 detects the case of something
2132 whose value is 0 but which isn't a valid null ptr const. */
2133 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2134 && TREE_CODE (tt1) == FUNCTION_TYPE)
2135 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2137 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2139 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2140 && TREE_CODE (tt0) == FUNCTION_TYPE)
2141 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2144 pedwarn ("comparison of distinct pointer types lacks a cast");
2146 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2147 && integer_zerop (op1))
2148 op1 = null_pointer_node;
2149 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2150 && integer_zerop (op0))
2151 op0 = null_pointer_node;
2152 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2154 if (! flag_traditional)
2155 pedwarn ("comparison between pointer and integer");
2156 op1 = convert (TREE_TYPE (op0), op1);
2158 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2160 if (! flag_traditional)
2161 pedwarn ("comparison between pointer and integer");
2162 op0 = convert (TREE_TYPE (op1), op0);
2165 /* If args are not valid, clear out RESULT_TYPE
2166 to cause an error message later. */
2172 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2173 || code0 == COMPLEX_TYPE)
2174 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2175 || code1 == COMPLEX_TYPE))
2177 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2179 if (! comp_target_types (type0, type1))
2180 pedwarn ("comparison of distinct pointer types lacks a cast");
2182 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2183 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2184 result_type = common_type (type0, type1);
2192 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2193 || code0 == COMPLEX_TYPE)
2194 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2195 || code1 == COMPLEX_TYPE))
2197 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2199 if (! comp_target_types (type0, type1))
2200 pedwarn ("comparison of distinct pointer types lacks a cast");
2201 else if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2202 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2203 pedwarn ("comparison of complete and incomplete pointers");
2205 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2206 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2207 result_type = integer_type_node;
2209 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2210 && integer_zerop (op1))
2212 result_type = integer_type_node;
2213 op1 = null_pointer_node;
2215 pedwarn ("ordered comparison of pointer with integer zero");
2217 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2218 && integer_zerop (op0))
2220 result_type = integer_type_node;
2221 op0 = null_pointer_node;
2223 pedwarn ("ordered comparison of pointer with integer zero");
2225 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2227 result_type = integer_type_node;
2228 if (! flag_traditional)
2229 pedwarn ("comparison between pointer and integer");
2230 op1 = convert (TREE_TYPE (op0), op1);
2232 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2234 result_type = integer_type_node;
2235 if (! flag_traditional)
2236 pedwarn ("comparison between pointer and integer");
2237 op0 = convert (TREE_TYPE (op1), op0);
2243 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2245 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2247 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2249 if (shorten || common || short_compare)
2250 result_type = common_type (type0, type1);
2252 /* For certain operations (which identify themselves by shorten != 0)
2253 if both args were extended from the same smaller type,
2254 do the arithmetic in that type and then extend.
2256 shorten !=0 and !=1 indicates a bitwise operation.
2257 For them, this optimization is safe only if
2258 both args are zero-extended or both are sign-extended.
2259 Otherwise, we might change the result.
2260 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2261 but calculated in (unsigned short) it would be (unsigned short)-1. */
2263 if (shorten && none_complex)
2265 int unsigned0, unsigned1;
2266 tree arg0 = get_narrower (op0, &unsigned0);
2267 tree arg1 = get_narrower (op1, &unsigned1);
2268 /* UNS is 1 if the operation to be done is an unsigned one. */
2269 int uns = TREE_UNSIGNED (result_type);
2272 final_type = result_type;
2274 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2275 but it *requires* conversion to FINAL_TYPE. */
2277 if ((TYPE_PRECISION (TREE_TYPE (op0))
2278 == TYPE_PRECISION (TREE_TYPE (arg0)))
2279 && TREE_TYPE (op0) != final_type)
2280 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2281 if ((TYPE_PRECISION (TREE_TYPE (op1))
2282 == TYPE_PRECISION (TREE_TYPE (arg1)))
2283 && TREE_TYPE (op1) != final_type)
2284 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2286 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2288 /* For bitwise operations, signedness of nominal type
2289 does not matter. Consider only how operands were extended. */
2293 /* Note that in all three cases below we refrain from optimizing
2294 an unsigned operation on sign-extended args.
2295 That would not be valid. */
2297 /* Both args variable: if both extended in same way
2298 from same width, do it in that width.
2299 Do it unsigned if args were zero-extended. */
2300 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2301 < TYPE_PRECISION (result_type))
2302 && (TYPE_PRECISION (TREE_TYPE (arg1))
2303 == TYPE_PRECISION (TREE_TYPE (arg0)))
2304 && unsigned0 == unsigned1
2305 && (unsigned0 || !uns))
2307 = signed_or_unsigned_type (unsigned0,
2308 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2309 else if (TREE_CODE (arg0) == INTEGER_CST
2310 && (unsigned1 || !uns)
2311 && (TYPE_PRECISION (TREE_TYPE (arg1))
2312 < TYPE_PRECISION (result_type))
2313 && (type = signed_or_unsigned_type (unsigned1,
2315 int_fits_type_p (arg0, type)))
2317 else if (TREE_CODE (arg1) == INTEGER_CST
2318 && (unsigned0 || !uns)
2319 && (TYPE_PRECISION (TREE_TYPE (arg0))
2320 < TYPE_PRECISION (result_type))
2321 && (type = signed_or_unsigned_type (unsigned0,
2323 int_fits_type_p (arg1, type)))
2327 /* Shifts can be shortened if shifting right. */
2332 tree arg0 = get_narrower (op0, &unsigned_arg);
2334 final_type = result_type;
2336 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2337 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2339 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2340 /* If arg is sign-extended and then unsigned-shifted,
2341 we can simulate this with a signed shift in arg's type
2342 only if the extended result is at least twice as wide
2343 as the arg. Otherwise, the shift could use up all the
2344 ones made by sign-extension and bring in zeros.
2345 We can't optimize that case at all, but in most machines
2346 it never happens because available widths are 2**N. */
2347 && (!TREE_UNSIGNED (final_type)
2349 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2351 /* Do an unsigned shift if the operand was zero-extended. */
2353 = signed_or_unsigned_type (unsigned_arg,
2355 /* Convert value-to-be-shifted to that type. */
2356 if (TREE_TYPE (op0) != result_type)
2357 op0 = convert (result_type, op0);
2362 /* Comparison operations are shortened too but differently.
2363 They identify themselves by setting short_compare = 1. */
2365 if (short_compare && none_complex)
2367 /* Don't write &op0, etc., because that would prevent op0
2368 from being kept in a register.
2369 Instead, make copies of the our local variables and
2370 pass the copies by reference, then copy them back afterward. */
2371 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2372 enum tree_code xresultcode = resultcode;
2374 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2377 op0 = xop0, op1 = xop1, result_type = xresult_type;
2378 resultcode = xresultcode;
2382 tree op0_type = TREE_TYPE (orig_op0);
2383 tree op1_type = TREE_TYPE (orig_op1);
2384 int op0_unsigned = TREE_UNSIGNED (op0_type);
2385 int op1_unsigned = TREE_UNSIGNED (op1_type);
2387 /* Give warnings for comparisons between signed and unsigned
2388 quantities that will fail. Do not warn if the signed quantity
2389 is an unsuffixed integer literal (or some static constant
2390 expression involving such literals) and it is positive.
2391 Do not warn if the width of the unsigned quantity is less
2392 than that of the signed quantity, since in this case all
2393 values of the unsigned quantity fit in the signed quantity.
2394 Do not warn if the signed type is the same size as the
2395 result_type since sign extension does not cause trouble in
2397 /* Do the checking based on the original operand trees, so that
2398 casts will be considered, but default promotions won't be. */
2399 if (op0_unsigned != op1_unsigned
2401 && TYPE_PRECISION (op0_type) >= TYPE_PRECISION (op1_type)
2402 && TYPE_PRECISION (op0_type) < TYPE_PRECISION (result_type)
2403 && (TREE_CODE (op1) != INTEGER_CST
2404 || (TREE_CODE (op1) == INTEGER_CST
2405 && INT_CST_LT (op1, integer_zero_node))))
2408 && TYPE_PRECISION (op1_type) >= TYPE_PRECISION (op0_type)
2409 && TYPE_PRECISION (op1_type) < TYPE_PRECISION (result_type)
2410 && (TREE_CODE (op0) != INTEGER_CST
2411 || (TREE_CODE (op0) == INTEGER_CST
2412 && INT_CST_LT (op0, integer_zero_node))))))
2413 warning ("comparison between signed and unsigned");
2418 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2419 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2420 Then the expression will be built.
2421 It will be given type FINAL_TYPE if that is nonzero;
2422 otherwise, it will be given type RESULT_TYPE. */
2426 binary_op_error (code);
2427 return error_mark_node;
2432 if (TREE_TYPE (op0) != result_type)
2433 op0 = convert (result_type, op0);
2434 if (TREE_TYPE (op1) != result_type)
2435 op1 = convert (result_type, op1);
2439 register tree result = build (resultcode, result_type, op0, op1);
2440 register tree folded;
2442 folded = fold (result);
2443 if (folded == result)
2444 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2445 if (final_type != 0)
2446 return convert (final_type, folded);
2451 /* Return a tree for the sum or difference (RESULTCODE says which)
2452 of pointer PTROP and integer INTOP. */
2455 pointer_int_sum (resultcode, ptrop, intop)
2456 enum tree_code resultcode;
2457 register tree ptrop, intop;
2461 register tree result;
2462 register tree folded;
2464 /* The result is a pointer of the same type that is being added. */
2466 register tree result_type = TREE_TYPE (ptrop);
2468 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2470 if (pedantic || warn_pointer_arith)
2471 pedwarn ("pointer of type `void *' used in arithmetic");
2472 size_exp = integer_one_node;
2474 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2476 if (pedantic || warn_pointer_arith)
2477 pedwarn ("pointer to a function used in arithmetic");
2478 size_exp = integer_one_node;
2481 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2483 /* If what we are about to multiply by the size of the elements
2484 contains a constant term, apply distributive law
2485 and multiply that constant term separately.
2486 This helps produce common subexpressions. */
2488 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2489 && ! TREE_CONSTANT (intop)
2490 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2491 && TREE_CONSTANT (size_exp)
2492 /* If the constant comes from pointer subtraction,
2493 skip this optimization--it would cause an error. */
2494 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE)
2496 enum tree_code subcode = resultcode;
2497 tree int_type = TREE_TYPE (intop);
2498 if (TREE_CODE (intop) == MINUS_EXPR)
2499 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2500 /* Convert both subexpression types to the type of intop,
2501 because weird cases involving pointer arithmetic
2502 can result in a sum or difference with different type args. */
2503 ptrop = build_binary_op (subcode, ptrop,
2504 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2505 intop = convert (int_type, TREE_OPERAND (intop, 0));
2508 /* Convert the integer argument to a type the same size as a pointer
2509 so the multiply won't overflow spuriously. */
2511 if (TYPE_PRECISION (TREE_TYPE (intop)) != POINTER_SIZE)
2512 intop = convert (type_for_size (POINTER_SIZE, 0), intop);
2514 /* Replace the integer argument
2515 with a suitable product by the object size. */
2517 intop = build_binary_op (MULT_EXPR, intop, size_exp, 1);
2519 /* Create the sum or difference. */
2521 result = build (resultcode, result_type, ptrop, intop);
2523 folded = fold (result);
2524 if (folded == result)
2525 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2529 /* Return a tree for the difference of pointers OP0 and OP1.
2530 The resulting tree has type int. */
2533 pointer_diff (op0, op1)
2534 register tree op0, op1;
2536 register tree result, folded;
2537 tree restype = ptrdiff_type_node;
2539 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2541 if (pedantic || warn_pointer_arith)
2543 if (TREE_CODE (target_type) == VOID_TYPE)
2544 pedwarn ("pointer of type `void *' used in subtraction");
2545 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2546 pedwarn ("pointer to a function used in subtraction");
2549 /* First do the subtraction as integers;
2550 then drop through to build the divide operator. */
2552 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2553 convert (restype, op1), 1);
2554 /* This generates an error if op1 is pointer to incomplete type. */
2555 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2556 error ("arithmetic on pointer to an incomplete type");
2557 /* This generates an error if op0 is pointer to incomplete type. */
2558 op1 = c_size_in_bytes (target_type);
2560 /* Divide by the size, in easiest possible way. */
2562 result = build (EXACT_DIV_EXPR, restype, op0, op1);
2564 folded = fold (result);
2565 if (folded == result)
2566 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2570 /* Construct and perhaps optimize a tree representation
2571 for a unary operation. CODE, a tree_code, specifies the operation
2572 and XARG is the operand. NOCONVERT nonzero suppresses
2573 the default promotions (such as from short to int). */
2576 build_unary_op (code, xarg, noconvert)
2577 enum tree_code code;
2581 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2582 register tree arg = xarg;
2583 register tree argtype = 0;
2584 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2585 char *errstring = NULL;
2588 if (typecode == ERROR_MARK)
2589 return error_mark_node;
2590 if (typecode == ENUMERAL_TYPE)
2591 typecode = INTEGER_TYPE;
2596 /* This is used for unary plus, because a CONVERT_EXPR
2597 is enough to prevent anybody from looking inside for
2598 associativity, but won't generate any code. */
2599 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2600 || typecode == COMPLEX_TYPE))
2601 errstring = "wrong type argument to unary plus";
2602 else if (!noconvert)
2603 arg = default_conversion (arg);
2607 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2608 || typecode == COMPLEX_TYPE))
2609 errstring = "wrong type argument to unary minus";
2610 else if (!noconvert)
2611 arg = default_conversion (arg);
2615 if (typecode == COMPLEX_TYPE)
2619 arg = default_conversion (arg);
2621 else if (typecode != INTEGER_TYPE)
2622 errstring = "wrong type argument to bit-complement";
2623 else if (!noconvert)
2624 arg = default_conversion (arg);
2628 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2629 || typecode == COMPLEX_TYPE))
2630 errstring = "wrong type argument to abs";
2631 else if (!noconvert)
2632 arg = default_conversion (arg);
2636 /* Conjugating a real value is a no-op, but allow it anyway. */
2637 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2638 || typecode == COMPLEX_TYPE))
2639 errstring = "wrong type argument to conjugation";
2640 else if (!noconvert)
2641 arg = default_conversion (arg);
2644 case TRUTH_NOT_EXPR:
2645 if (typecode != INTEGER_TYPE
2646 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2647 && typecode != COMPLEX_TYPE
2648 /* These will convert to a pointer. */
2649 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2651 errstring = "wrong type argument to unary exclamation mark";
2654 arg = truthvalue_conversion (arg);
2655 return invert_truthvalue (arg);
2661 if (TREE_CODE (arg) == COMPLEX_CST)
2662 return TREE_REALPART (arg);
2663 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2664 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2669 if (TREE_CODE (arg) == COMPLEX_CST)
2670 return TREE_IMAGPART (arg);
2671 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2672 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2674 return convert (TREE_TYPE (arg), integer_zero_node);
2676 case PREINCREMENT_EXPR:
2677 case POSTINCREMENT_EXPR:
2678 case PREDECREMENT_EXPR:
2679 case POSTDECREMENT_EXPR:
2680 /* Handle complex lvalues (when permitted)
2681 by reduction to simpler cases. */
2683 val = unary_complex_lvalue (code, arg);
2687 /* Increment or decrement the real part of the value,
2688 and don't change the imaginary part. */
2689 if (typecode == COMPLEX_TYPE)
2693 arg = stabilize_reference (arg);
2694 real = build_unary_op (REALPART_EXPR, arg, 1);
2695 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2696 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2697 build_unary_op (code, real, 1), imag);
2700 /* Report invalid types. */
2702 if (typecode != POINTER_TYPE
2703 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2705 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2706 errstring ="wrong type argument to increment";
2708 errstring ="wrong type argument to decrement";
2714 tree result_type = TREE_TYPE (arg);
2716 arg = get_unwidened (arg, 0);
2717 argtype = TREE_TYPE (arg);
2719 /* Compute the increment. */
2721 if (typecode == POINTER_TYPE)
2723 /* If pointer target is an undefined struct,
2724 we just cannot know how to do the arithmetic. */
2725 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2726 error ("%s of pointer to unknown structure",
2727 ((code == PREINCREMENT_EXPR
2728 || code == POSTINCREMENT_EXPR)
2729 ? "increment" : "decrement"));
2730 else if ((pedantic || warn_pointer_arith)
2731 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2732 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2733 pedwarn ("wrong type argument to %s",
2734 ((code == PREINCREMENT_EXPR
2735 || code == POSTINCREMENT_EXPR)
2736 ? "increment" : "decrement"));
2737 inc = c_sizeof_nowarn (TREE_TYPE (result_type));
2740 inc = integer_one_node;
2742 inc = convert (argtype, inc);
2744 /* Handle incrementing a cast-expression. */
2747 switch (TREE_CODE (arg))
2752 case FIX_TRUNC_EXPR:
2753 case FIX_FLOOR_EXPR:
2754 case FIX_ROUND_EXPR:
2756 pedantic_lvalue_warning (CONVERT_EXPR);
2757 /* If the real type has the same machine representation
2758 as the type it is cast to, we can make better output
2759 by adding directly to the inside of the cast. */
2760 if ((TREE_CODE (TREE_TYPE (arg))
2761 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2762 && (TYPE_MODE (TREE_TYPE (arg))
2763 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2764 arg = TREE_OPERAND (arg, 0);
2767 tree incremented, modify, value;
2768 arg = stabilize_reference (arg);
2769 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2772 value = save_expr (arg);
2773 incremented = build (((code == PREINCREMENT_EXPR
2774 || code == POSTINCREMENT_EXPR)
2775 ? PLUS_EXPR : MINUS_EXPR),
2776 argtype, value, inc);
2777 TREE_SIDE_EFFECTS (incremented) = 1;
2778 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2779 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2780 TREE_USED (value) = 1;
2790 /* Complain about anything else that is not a true lvalue. */
2791 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2792 || code == POSTINCREMENT_EXPR)
2793 ? "increment" : "decrement")))
2794 return error_mark_node;
2796 /* Report a read-only lvalue. */
2797 if (TREE_READONLY (arg))
2798 readonly_warning (arg,
2799 ((code == PREINCREMENT_EXPR
2800 || code == POSTINCREMENT_EXPR)
2801 ? "increment" : "decrement"));
2803 val = build (code, TREE_TYPE (arg), arg, inc);
2804 TREE_SIDE_EFFECTS (val) = 1;
2805 val = convert (result_type, val);
2806 if (TREE_CODE (val) != code)
2807 TREE_NO_UNUSED_WARNING (val) = 1;
2812 /* Note that this operation never does default_conversion
2813 regardless of NOCONVERT. */
2815 /* Let &* cancel out to simplify resulting code. */
2816 if (TREE_CODE (arg) == INDIRECT_REF)
2818 /* Don't let this be an lvalue. */
2819 if (lvalue_p (TREE_OPERAND (arg, 0)))
2820 return non_lvalue (TREE_OPERAND (arg, 0));
2821 return TREE_OPERAND (arg, 0);
2824 /* For &x[y], return x+y */
2825 if (TREE_CODE (arg) == ARRAY_REF)
2827 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
2828 return error_mark_node;
2829 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2830 TREE_OPERAND (arg, 1), 1);
2833 /* Handle complex lvalues (when permitted)
2834 by reduction to simpler cases. */
2835 val = unary_complex_lvalue (code, arg);
2839 #if 0 /* Turned off because inconsistent;
2840 float f; *&(int)f = 3.4 stores in int format
2841 whereas (int)f = 3.4 stores in float format. */
2842 /* Address of a cast is just a cast of the address
2843 of the operand of the cast. */
2844 switch (TREE_CODE (arg))
2849 case FIX_TRUNC_EXPR:
2850 case FIX_FLOOR_EXPR:
2851 case FIX_ROUND_EXPR:
2854 pedwarn ("ANSI C forbids the address of a cast expression");
2855 return convert (build_pointer_type (TREE_TYPE (arg)),
2856 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
2861 /* Allow the address of a constructor if all the elements
2863 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
2865 /* Anything not already handled and not a true memory reference
2867 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
2868 return error_mark_node;
2870 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2871 argtype = TREE_TYPE (arg);
2872 /* If the lvalue is const or volatile,
2873 merge that into the type that the address will point to. */
2874 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
2875 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2877 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
2878 argtype = c_build_type_variant (argtype,
2879 TREE_READONLY (arg),
2880 TREE_THIS_VOLATILE (arg));
2883 argtype = build_pointer_type (argtype);
2885 if (mark_addressable (arg) == 0)
2886 return error_mark_node;
2891 if (TREE_CODE (arg) == COMPONENT_REF)
2893 tree field = TREE_OPERAND (arg, 1);
2895 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
2897 if (DECL_BIT_FIELD (field))
2899 error ("attempt to take address of bit-field structure member `%s'",
2900 IDENTIFIER_POINTER (DECL_NAME (field)));
2901 return error_mark_node;
2904 addr = convert (argtype, addr);
2906 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
2909 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
2910 size_int (BITS_PER_UNIT));
2911 int flag = TREE_CONSTANT (addr);
2912 addr = fold (build (PLUS_EXPR, argtype,
2913 addr, convert (argtype, offset)));
2914 TREE_CONSTANT (addr) = flag;
2918 addr = build1 (code, argtype, arg);
2920 /* Address of a static or external variable or
2921 file-scope function counts as a constant. */
2923 && ! (TREE_CODE (arg) == FUNCTION_DECL
2924 && DECL_CONTEXT (arg) != 0))
2925 TREE_CONSTANT (addr) = 1;
2933 argtype = TREE_TYPE (arg);
2934 return fold (build1 (code, argtype, arg));
2938 return error_mark_node;
2942 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
2943 convert ARG with the same conversions in the same order
2944 and return the result. */
2947 convert_sequence (conversions, arg)
2951 switch (TREE_CODE (conversions))
2956 case FIX_TRUNC_EXPR:
2957 case FIX_FLOOR_EXPR:
2958 case FIX_ROUND_EXPR:
2960 return convert (TREE_TYPE (conversions),
2961 convert_sequence (TREE_OPERAND (conversions, 0),
2970 /* Return nonzero if REF is an lvalue valid for this language.
2971 Lvalues can be assigned, unless their type has TYPE_READONLY.
2972 Lvalues can have their address taken, unless they have DECL_REGISTER. */
2978 register enum tree_code code = TREE_CODE (ref);
2985 return lvalue_p (TREE_OPERAND (ref, 0));
2996 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2997 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
3004 /* Return nonzero if REF is an lvalue valid for this language;
3005 otherwise, print an error message and return zero. */
3008 lvalue_or_else (ref, string)
3012 int win = lvalue_p (ref);
3014 error ("invalid lvalue in %s", string);
3018 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3019 for certain kinds of expressions which are not really lvalues
3020 but which we can accept as lvalues.
3022 If ARG is not a kind of expression we can handle, return zero. */
3025 unary_complex_lvalue (code, arg)
3026 enum tree_code code;
3029 /* Handle (a, b) used as an "lvalue". */
3030 if (TREE_CODE (arg) == COMPOUND_EXPR)
3032 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3033 pedantic_lvalue_warning (COMPOUND_EXPR);
3034 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3035 TREE_OPERAND (arg, 0), real_result);
3038 /* Handle (a ? b : c) used as an "lvalue". */
3039 if (TREE_CODE (arg) == COND_EXPR)
3041 pedantic_lvalue_warning (COND_EXPR);
3042 return (build_conditional_expr
3043 (TREE_OPERAND (arg, 0),
3044 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3045 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3051 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3052 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3055 pedantic_lvalue_warning (code)
3056 enum tree_code code;
3059 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3060 code == COND_EXPR ? "conditional"
3061 : code == COMPOUND_EXPR ? "compound" : "cast");
3064 /* Warn about storing in something that is `const'. */
3067 readonly_warning (arg, string)
3072 strcpy (buf, string);
3074 /* Forbid assignments to iterators. */
3075 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3077 strcat (buf, " of iterator `%s'");
3078 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3081 if (TREE_CODE (arg) == COMPONENT_REF)
3083 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3084 readonly_warning (TREE_OPERAND (arg, 0), string);
3087 strcat (buf, " of read-only member `%s'");
3088 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3091 else if (TREE_CODE (arg) == VAR_DECL)
3093 strcat (buf, " of read-only variable `%s'");
3094 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3098 pedwarn ("%s of read-only location", buf);
3102 /* Mark EXP saying that we need to be able to take the
3103 address of it; it should not be allocated in a register.
3104 Value is 1 if successful. */
3107 mark_addressable (exp)
3110 register tree x = exp;
3112 switch (TREE_CODE (x))
3119 x = TREE_OPERAND (x, 0);
3123 TREE_ADDRESSABLE (x) = 1;
3130 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3131 && DECL_NONLOCAL (x))
3133 if (TREE_PUBLIC (x))
3135 error ("global register variable `%s' used in nested function",
3136 IDENTIFIER_POINTER (DECL_NAME (x)));
3139 pedwarn ("register variable `%s' used in nested function",
3140 IDENTIFIER_POINTER (DECL_NAME (x)));
3142 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3144 if (TREE_PUBLIC (x))
3146 error ("address of global register variable `%s' requested",
3147 IDENTIFIER_POINTER (DECL_NAME (x)));
3150 pedwarn ("address of register variable `%s' requested",
3151 IDENTIFIER_POINTER (DECL_NAME (x)));
3153 put_var_into_stack (x);
3157 TREE_ADDRESSABLE (x) = 1;
3158 #if 0 /* poplevel deals with this now. */
3159 if (DECL_CONTEXT (x) == 0)
3160 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3168 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3171 build_conditional_expr (ifexp, op1, op2)
3172 tree ifexp, op1, op2;
3174 register tree type1;
3175 register tree type2;
3176 register enum tree_code code1;
3177 register enum tree_code code2;
3178 register tree result_type = NULL;
3179 tree orig_op1 = op1, orig_op2 = op2;
3181 /* If second operand is omitted, it is the same as the first one;
3182 make sure it is calculated only once. */
3186 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression");
3187 ifexp = op1 = save_expr (ifexp);
3190 ifexp = truthvalue_conversion (default_conversion (ifexp));
3192 #if 0 /* Produces wrong result if within sizeof. */
3193 /* Don't promote the operands separately if they promote
3194 the same way. Return the unpromoted type and let the combined
3195 value get promoted if necessary. */
3197 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3198 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3199 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3200 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3202 if (TREE_CODE (ifexp) == INTEGER_CST)
3203 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3205 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3209 /* Promote both alternatives. */
3211 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3212 op1 = default_conversion (op1);
3213 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3214 op2 = default_conversion (op2);
3216 if (TREE_CODE (ifexp) == ERROR_MARK
3217 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3218 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3219 return error_mark_node;
3221 type1 = TREE_TYPE (op1);
3222 code1 = TREE_CODE (type1);
3223 type2 = TREE_TYPE (op2);
3224 code2 = TREE_CODE (type2);
3226 /* Quickly detect the usual case where op1 and op2 have the same type
3228 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3231 result_type = type1;
3233 result_type = TYPE_MAIN_VARIANT (type1);
3235 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3236 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3238 result_type = common_type (type1, type2);
3240 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3242 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3243 pedwarn ("ANSI C forbids conditional expr with only one void side");
3244 result_type = void_type_node;
3246 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3248 if (comp_target_types (type1, type2))
3249 result_type = common_type (type1, type2);
3250 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3251 && TREE_CODE (orig_op1) != NOP_EXPR)
3252 result_type = qualify_type (type2, type1);
3253 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3254 && TREE_CODE (orig_op2) != NOP_EXPR)
3255 result_type = qualify_type (type1, type2);
3256 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3258 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3259 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3260 result_type = qualify_type (type1, type2);
3262 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3264 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3265 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3266 result_type = qualify_type (type2, type1);
3270 pedwarn ("pointer type mismatch in conditional expression");
3271 result_type = build_pointer_type (void_type_node);
3274 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3276 if (! integer_zerop (op2))
3277 pedwarn ("pointer/integer type mismatch in conditional expression");
3280 op2 = null_pointer_node;
3281 #if 0 /* The spec seems to say this is permitted. */
3282 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3283 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3286 result_type = type1;
3288 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3290 if (!integer_zerop (op1))
3291 pedwarn ("pointer/integer type mismatch in conditional expression");
3294 op1 = null_pointer_node;
3295 #if 0 /* The spec seems to say this is permitted. */
3296 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3297 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3300 result_type = type2;
3305 if (flag_cond_mismatch)
3306 result_type = void_type_node;
3309 error ("type mismatch in conditional expression");
3310 return error_mark_node;
3314 /* Merge const and volatile flags of the incoming types. */
3316 = build_type_variant (result_type,
3317 TREE_READONLY (op1) || TREE_READONLY (op2),
3318 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3320 if (result_type != TREE_TYPE (op1))
3321 op1 = convert_and_check (result_type, op1);
3322 if (result_type != TREE_TYPE (op2))
3323 op2 = convert_and_check (result_type, op2);
3326 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3328 result_type = TREE_TYPE (op1);
3329 if (TREE_CONSTANT (ifexp))
3330 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3332 if (TYPE_MODE (result_type) == BLKmode)
3334 register tree tempvar
3335 = build_decl (VAR_DECL, NULL_TREE, result_type);
3336 register tree xop1 = build_modify_expr (tempvar, op1);
3337 register tree xop2 = build_modify_expr (tempvar, op2);
3338 register tree result = fold (build (COND_EXPR, result_type,
3339 ifexp, xop1, xop2));
3341 layout_decl (tempvar, TYPE_ALIGN (result_type));
3342 /* No way to handle variable-sized objects here.
3343 I fear that the entire handling of BLKmode conditional exprs
3344 needs to be redone. */
3345 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3348 = assign_stack_local (DECL_MODE (tempvar),
3349 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3350 + BITS_PER_UNIT - 1)
3354 TREE_SIDE_EFFECTS (result)
3355 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3356 | TREE_SIDE_EFFECTS (op2);
3357 return build (COMPOUND_EXPR, result_type, result, tempvar);
3362 if (TREE_CODE (ifexp) == INTEGER_CST)
3363 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3365 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3368 /* Given a list of expressions, return a compound expression
3369 that performs them all and returns the value of the last of them. */
3372 build_compound_expr (list)
3375 return internal_build_compound_expr (list, TRUE);
3379 internal_build_compound_expr (list, first_p)
3385 if (TREE_CHAIN (list) == 0)
3387 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3388 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3390 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3391 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3392 list = TREE_OPERAND (list, 0);
3395 /* Don't let (0, 0) be null pointer constant. */
3396 if (!first_p && integer_zerop (TREE_VALUE (list)))
3397 return non_lvalue (TREE_VALUE (list));
3398 return TREE_VALUE (list);
3401 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3403 /* Convert arrays to pointers when there really is a comma operator. */
3404 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3405 TREE_VALUE (TREE_CHAIN (list))
3406 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3409 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3411 /* When pedantic, a compound expression can be neither an lvalue
3412 nor an integer constant expression. */
3413 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
3416 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3419 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3422 build_c_cast (type, expr)
3426 register tree value = expr;
3428 if (type == error_mark_node || expr == error_mark_node)
3429 return error_mark_node;
3430 type = TYPE_MAIN_VARIANT (type);
3433 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3434 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3435 value = TREE_OPERAND (value, 0);
3438 if (TREE_CODE (type) == ARRAY_TYPE)
3440 error ("cast specifies array type");
3441 return error_mark_node;
3444 if (TREE_CODE (type) == FUNCTION_TYPE)
3446 error ("cast specifies function type");
3447 return error_mark_node;
3450 if (type == TREE_TYPE (value))
3454 if (TREE_CODE (type) == RECORD_TYPE
3455 || TREE_CODE (type) == UNION_TYPE)
3456 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3459 else if (TREE_CODE (type) == UNION_TYPE)
3462 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3463 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3464 value = default_conversion (value);
3466 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3467 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3468 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3477 pedwarn ("ANSI C forbids casts to union type");
3478 if (TYPE_NAME (type) != 0)
3480 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3481 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3483 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3487 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3488 build_tree_list (field, value)),
3490 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3493 error ("cast to union type from type not present in union");
3494 return error_mark_node;
3500 /* If casting to void, avoid the error that would come
3501 from default_conversion in the case of a non-lvalue array. */
3502 if (type == void_type_node)
3503 return build1 (CONVERT_EXPR, type, value);
3505 /* Convert functions and arrays to pointers,
3506 but don't convert any other types. */
3507 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3508 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3509 value = default_conversion (value);
3510 otype = TREE_TYPE (value);
3512 /* Optionally warn about potentially worrisome casts. */
3515 && TREE_CODE (type) == POINTER_TYPE
3516 && TREE_CODE (otype) == POINTER_TYPE)
3518 if (TYPE_VOLATILE (TREE_TYPE (otype))
3519 && ! TYPE_VOLATILE (TREE_TYPE (type)))
3520 pedwarn ("cast discards `volatile' from pointer target type");
3521 if (TYPE_READONLY (TREE_TYPE (otype))
3522 && ! TYPE_READONLY (TREE_TYPE (type)))
3523 pedwarn ("cast discards `const' from pointer target type");
3526 /* Warn about possible alignment problems. */
3527 if (STRICT_ALIGNMENT && warn_cast_align
3528 && TREE_CODE (type) == POINTER_TYPE
3529 && TREE_CODE (otype) == POINTER_TYPE
3530 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3531 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3532 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3533 warning ("cast increases required alignment of target type");
3535 if (TREE_CODE (type) == INTEGER_TYPE
3536 && TREE_CODE (otype) == POINTER_TYPE
3537 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3538 && !TREE_CONSTANT (value))
3539 warning ("cast from pointer to integer of different size");
3541 if (TREE_CODE (type) == POINTER_TYPE
3542 && TREE_CODE (otype) == INTEGER_TYPE
3543 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3545 /* Don't warn about converting 0 to pointer,
3546 provided the 0 was explicit--not cast or made by folding. */
3547 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3549 /* Don't warn about converting any constant. */
3550 && !TREE_CONSTANT (value))
3551 warning ("cast to pointer from integer of different size");
3554 value = convert (type, value);
3556 /* Ignore any integer overflow caused by the cast. */
3557 if (TREE_CODE (value) == INTEGER_CST)
3559 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3560 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3564 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3565 if (pedantic && TREE_CODE (value) == INTEGER_CST
3566 && TREE_CODE (expr) == INTEGER_CST
3567 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3568 value = non_lvalue (value);
3570 /* If pedantic, don't let a cast be an lvalue. */
3571 if (value == expr && pedantic)
3572 value = non_lvalue (value);
3577 /* Build an assignment expression of lvalue LHS from value RHS.
3578 MODIFYCODE is the code for a binary operator that we use
3579 to combine the old value of LHS with RHS to get the new value.
3580 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3583 build_modify_expr (lhs, modifycode, rhs)
3585 enum tree_code modifycode;
3587 register tree result;
3589 tree lhstype = TREE_TYPE (lhs);
3590 tree olhstype = lhstype;
3592 /* Types that aren't fully specified cannot be used in assignments. */
3593 lhs = require_complete_type (lhs);
3595 /* Avoid duplicate error messages from operands that had errors. */
3596 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3597 return error_mark_node;
3599 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3600 /* Do not use STRIP_NOPS here. We do not want an enumerator
3601 whose value is 0 to count as a null pointer constant. */
3602 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3603 rhs = TREE_OPERAND (rhs, 0);
3607 /* Handle control structure constructs used as "lvalues". */
3609 switch (TREE_CODE (lhs))
3611 /* Handle (a, b) used as an "lvalue". */
3613 pedantic_lvalue_warning (COMPOUND_EXPR);
3614 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3616 if (TREE_CODE (newrhs) == ERROR_MARK)
3617 return error_mark_node;
3618 return build (COMPOUND_EXPR, lhstype,
3619 TREE_OPERAND (lhs, 0), newrhs);
3621 /* Handle (a ? b : c) used as an "lvalue". */
3623 pedantic_lvalue_warning (COND_EXPR);
3624 rhs = save_expr (rhs);
3626 /* Produce (a ? (b = rhs) : (c = rhs))
3627 except that the RHS goes through a save-expr
3628 so the code to compute it is only emitted once. */
3630 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3631 build_modify_expr (TREE_OPERAND (lhs, 1),
3633 build_modify_expr (TREE_OPERAND (lhs, 2),
3635 if (TREE_CODE (cond) == ERROR_MARK)
3637 /* Make sure the code to compute the rhs comes out
3638 before the split. */
3639 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3640 /* But cast it to void to avoid an "unused" error. */
3641 convert (void_type_node, rhs), cond);
3645 /* If a binary op has been requested, combine the old LHS value with the RHS
3646 producing the value we should actually store into the LHS. */
3648 if (modifycode != NOP_EXPR)
3650 lhs = stabilize_reference (lhs);
3651 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3654 /* Handle a cast used as an "lvalue".
3655 We have already performed any binary operator using the value as cast.
3656 Now convert the result to the cast type of the lhs,
3657 and then true type of the lhs and store it there;
3658 then convert result back to the cast type to be the value
3659 of the assignment. */
3661 switch (TREE_CODE (lhs))
3666 case FIX_TRUNC_EXPR:
3667 case FIX_FLOOR_EXPR:
3668 case FIX_ROUND_EXPR:
3670 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3671 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3672 newrhs = default_conversion (newrhs);
3674 tree inner_lhs = TREE_OPERAND (lhs, 0);
3676 result = build_modify_expr (inner_lhs, NOP_EXPR,
3677 convert (TREE_TYPE (inner_lhs),
3678 convert (lhstype, newrhs)));
3679 if (TREE_CODE (result) == ERROR_MARK)
3681 pedantic_lvalue_warning (CONVERT_EXPR);
3682 return convert (TREE_TYPE (lhs), result);
3686 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3687 Reject anything strange now. */
3689 if (!lvalue_or_else (lhs, "assignment"))
3690 return error_mark_node;
3692 /* Warn about storing in something that is `const'. */
3694 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3695 || ((TREE_CODE (lhstype) == RECORD_TYPE
3696 || TREE_CODE (lhstype) == UNION_TYPE)
3697 && C_TYPE_FIELDS_READONLY (lhstype)))
3698 readonly_warning (lhs, "assignment");
3700 /* If storing into a structure or union member,
3701 it has probably been given type `int'.
3702 Compute the type that would go with
3703 the actual amount of storage the member occupies. */
3705 if (TREE_CODE (lhs) == COMPONENT_REF
3706 && (TREE_CODE (lhstype) == INTEGER_TYPE
3707 || TREE_CODE (lhstype) == REAL_TYPE
3708 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3709 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3711 /* If storing in a field that is in actuality a short or narrower than one,
3712 we must store in the field in its actual type. */
3714 if (lhstype != TREE_TYPE (lhs))
3716 lhs = copy_node (lhs);
3717 TREE_TYPE (lhs) = lhstype;
3720 /* Convert new value to destination type. */
3722 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
3723 NULL_TREE, NULL_TREE, 0);
3724 if (TREE_CODE (newrhs) == ERROR_MARK)
3725 return error_mark_node;
3727 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3728 TREE_SIDE_EFFECTS (result) = 1;
3730 /* If we got the LHS in a different type for storing in,
3731 convert the result back to the nominal type of LHS
3732 so that the value we return always has the same type
3733 as the LHS argument. */
3735 if (olhstype == TREE_TYPE (result))
3737 return convert_for_assignment (olhstype, result, "assignment",
3738 NULL_TREE, NULL_TREE, 0);
3741 /* Convert value RHS to type TYPE as preparation for an assignment
3742 to an lvalue of type TYPE.
3743 The real work of conversion is done by `convert'.
3744 The purpose of this function is to generate error messages
3745 for assignments that are not allowed in C.
3746 ERRTYPE is a string to use in error messages:
3747 "assignment", "return", etc. If it is null, this is parameter passing
3748 for a function call (and different error messages are output). Otherwise,
3749 it may be a name stored in the spelling stack and interpreted by
3752 FUNNAME is the name of the function being called,
3753 as an IDENTIFIER_NODE, or null.
3754 PARMNUM is the number of the argument, for printing in error messages. */
3757 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3760 tree fundecl, funname;
3763 register enum tree_code codel = TREE_CODE (type);
3764 register tree rhstype;
3765 register enum tree_code coder;
3767 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3768 /* Do not use STRIP_NOPS here. We do not want an enumerator
3769 whose value is 0 to count as a null pointer constant. */
3770 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3771 rhs = TREE_OPERAND (rhs, 0);
3773 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3774 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3775 rhs = default_conversion (rhs);
3777 rhstype = TREE_TYPE (rhs);
3778 coder = TREE_CODE (rhstype);
3780 if (coder == ERROR_MARK)
3781 return error_mark_node;
3783 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3785 overflow_warning (rhs);
3786 /* Check for Objective-C protocols. This will issue a warning if
3787 there are protocol violations. No need to use the return value. */
3788 maybe_objc_comptypes (type, rhstype, 0);
3792 if (coder == VOID_TYPE)
3794 error ("void value not ignored as it ought to be");
3795 return error_mark_node;
3797 /* Arithmetic types all interconvert, and enum is treated like int. */
3798 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
3799 || codel == COMPLEX_TYPE)
3801 (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
3802 || coder == COMPLEX_TYPE))
3803 return convert_and_check (type, rhs);
3804 /* Conversion to a union from its member types. */
3805 else if (codel == UNION_TYPE)
3808 for (memb_types = TYPE_FIELDS (type); memb_types;
3809 memb_types = TREE_CHAIN (memb_types))
3811 if (comptypes (TREE_TYPE (memb_types), TREE_TYPE (rhs)))
3814 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
3815 pedwarn ("ANSI C prohibits argument conversion to union type");
3816 return build1 (NOP_EXPR, type, rhs);
3818 else if (coder == POINTER_TYPE
3819 && TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE)
3821 tree memb_type = TREE_TYPE (memb_types);
3822 register tree ttl = TREE_TYPE (memb_type);
3823 register tree ttr = TREE_TYPE (rhstype);
3825 /* Any non-function converts to a [const][volatile] void *
3826 and vice versa; otherwise, targets must be the same.
3827 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3828 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3829 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3830 || comp_target_types (memb_type, rhstype))
3832 /* Const and volatile mean something different for function types,
3833 so the usual warnings are not appropriate. */
3834 if (TREE_CODE (ttr) != FUNCTION_TYPE
3835 || TREE_CODE (ttl) != FUNCTION_TYPE)
3837 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
3838 warn_for_assignment ("%s discards `const' from pointer target type",
3839 get_spelling (errtype), funname, parmnum);
3840 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
3841 warn_for_assignment ("%s discards `volatile' from pointer target type",
3842 get_spelling (errtype), funname, parmnum);
3846 /* Because const and volatile on functions are restrictions
3847 that say the function will not do certain things,
3848 it is okay to use a const or volatile function
3849 where an ordinary one is wanted, but not vice-versa. */
3850 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
3851 warn_for_assignment ("%s makes `const *' function pointer from non-const",
3852 get_spelling (errtype), funname, parmnum);
3853 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
3854 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
3855 get_spelling (errtype), funname, parmnum);
3858 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
3859 pedwarn ("ANSI C prohibits argument conversion to union type");
3860 return build1 (NOP_EXPR, type, rhs);
3865 /* Conversions among pointers */
3866 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
3868 register tree ttl = TREE_TYPE (type);
3869 register tree ttr = TREE_TYPE (rhstype);
3871 /* Any non-function converts to a [const][volatile] void *
3872 and vice versa; otherwise, targets must be the same.
3873 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3874 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3875 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3876 || comp_target_types (type, rhstype)
3877 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
3878 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3881 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
3882 && TREE_CODE (ttr) == FUNCTION_TYPE)
3884 (TYPE_MAIN_VARIANT (ttr) == void_type_node
3885 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3886 which are not ANSI null ptr constants. */
3887 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3888 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3889 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
3890 get_spelling (errtype), funname, parmnum);
3891 /* Const and volatile mean something different for function types,
3892 so the usual warnings are not appropriate. */
3893 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3894 || TREE_CODE (ttl) != FUNCTION_TYPE)
3896 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
3897 warn_for_assignment ("%s discards `const' from pointer target type",
3898 get_spelling (errtype), funname, parmnum);
3899 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
3900 warn_for_assignment ("%s discards `volatile' from pointer target type",
3901 get_spelling (errtype), funname, parmnum);
3902 /* If this is not a case of ignoring a mismatch in signedness,
3904 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3905 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3906 || comp_target_types (type, rhstype))
3908 /* If there is a mismatch, do warn. */
3910 warn_for_assignment ("pointer targets in %s differ in signedness",
3911 get_spelling (errtype), funname, parmnum);
3915 /* Because const and volatile on functions are restrictions
3916 that say the function will not do certain things,
3917 it is okay to use a const or volatile function
3918 where an ordinary one is wanted, but not vice-versa. */
3919 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
3920 warn_for_assignment ("%s makes `const *' function pointer from non-const",
3921 get_spelling (errtype), funname, parmnum);
3922 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
3923 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
3924 get_spelling (errtype), funname, parmnum);
3928 warn_for_assignment ("%s from incompatible pointer type",
3929 get_spelling (errtype), funname, parmnum);
3930 return convert (type, rhs);
3932 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3934 /* An explicit constant 0 can convert to a pointer,
3935 or one that results from arithmetic, even including
3936 a cast to integer type. */
3937 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3939 ! (TREE_CODE (rhs) == NOP_EXPR
3940 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3941 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3942 && integer_zerop (TREE_OPERAND (rhs, 0))))
3944 warn_for_assignment ("%s makes pointer from integer without a cast",
3945 get_spelling (errtype), funname, parmnum);
3946 return convert (type, rhs);
3948 return null_pointer_node;
3950 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3952 warn_for_assignment ("%s makes integer from pointer without a cast",
3953 get_spelling (errtype), funname, parmnum);
3954 return convert (type, rhs);
3961 tree selector = maybe_building_objc_message_expr ();
3963 if (selector && parmnum > 2)
3964 error ("incompatible type for argument %d of `%s'",
3965 parmnum - 2, IDENTIFIER_POINTER (selector));
3967 error ("incompatible type for argument %d of `%s'",
3968 parmnum, IDENTIFIER_POINTER (funname));
3971 error ("incompatible type for argument %d of indirect function call",
3975 error ("incompatible types in %s", get_spelling (errtype));
3977 return error_mark_node;
3980 /* Print a warning using MSG.
3981 It gets OPNAME as its one parameter.
3982 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3983 FUNCTION and ARGNUM are handled specially if we are building an
3984 Objective-C selector. */
3987 warn_for_assignment (msg, opname, function, argnum)
3993 static char argstring[] = "passing arg %d of `%s'";
3994 static char argnofun[] = "passing arg %d";
3998 tree selector = maybe_building_objc_message_expr ();
4000 if (selector && argnum > 2)
4002 function = selector;
4007 /* Function name is known; supply it. */
4008 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4009 + sizeof (argstring) + 25 /*%d*/ + 1);
4010 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4014 /* Function name unknown (call through ptr); just give arg number. */
4015 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4016 sprintf (opname, argnofun, argnum);
4019 pedwarn (msg, opname);
4022 /* Return nonzero if VALUE is a valid constant-valued expression
4023 for use in initializing a static variable; one that can be an
4024 element of a "constant" initializer.
4026 Return null_pointer_node if the value is absolute;
4027 if it is relocatable, return the variable that determines the relocation.
4028 We assume that VALUE has been folded as much as possible;
4029 therefore, we do not need to check for such things as
4030 arithmetic-combinations of integers. */
4033 initializer_constant_valid_p (value, endtype)
4037 switch (TREE_CODE (value))
4040 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4041 && TREE_CONSTANT (value))
4042 return initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)));
4044 return TREE_STATIC (value) ? null_pointer_node : 0;
4050 return null_pointer_node;
4053 return TREE_OPERAND (value, 0);
4055 case NON_LVALUE_EXPR:
4056 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4060 /* Allow conversions between pointer types. */
4061 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4062 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
4063 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4064 /* Allow conversions between real types. */
4065 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4066 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
4067 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4068 /* Allow length-preserving conversions between integer types. */
4069 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4070 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4071 && tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (value)),
4072 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value, 0)))))
4073 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4074 /* Allow conversions between integer types only if explicit value. */
4075 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4076 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4078 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4080 if (inner == null_pointer_node)
4081 return null_pointer_node;
4084 /* Allow (int) &foo provided int is as wide as a pointer. */
4085 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4086 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
4087 && ! tree_int_cst_lt (TYPE_SIZE (TREE_TYPE (value)),
4088 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value, 0)))))
4089 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4091 /* Allow conversions to union types if the value inside is okay. */
4092 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4093 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4098 if (TREE_CODE (endtype) == INTEGER_TYPE
4099 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4102 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4104 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4106 /* If either term is absolute, use the other terms relocation. */
4107 if (valid0 == null_pointer_node)
4109 if (valid1 == null_pointer_node)
4115 if (TREE_CODE (endtype) == INTEGER_TYPE
4116 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4119 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4121 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4123 /* Win if second argument is absolute. */
4124 if (valid1 == null_pointer_node)
4126 /* Win if both arguments have the same relocation.
4127 Then the value is absolute. */
4128 if (valid0 == valid1)
4129 return null_pointer_node;
4137 /* If VALUE is a compound expr all of whose expressions are constant, then
4138 return its value. Otherwise, return error_mark_node.
4140 This is for handling COMPOUND_EXPRs as initializer elements
4141 which is allowed with a warning when -pedantic is specified. */
4144 valid_compound_expr_initializer (value, endtype)
4148 if (TREE_CODE (value) == COMPOUND_EXPR)
4150 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4152 return error_mark_node;
4153 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4156 else if (! TREE_CONSTANT (value)
4157 && ! initializer_constant_valid_p (value, endtype))
4158 return error_mark_node;
4163 /* Perform appropriate conversions on the initial value of a variable,
4164 store it in the declaration DECL,
4165 and print any error messages that are appropriate.
4166 If the init is invalid, store an ERROR_MARK. */
4169 store_init_value (decl, init)
4172 register tree value, type;
4174 /* If variable's type was invalidly declared, just ignore it. */
4176 type = TREE_TYPE (decl);
4177 if (TREE_CODE (type) == ERROR_MARK)
4180 /* Digest the specified initializer into an expression. */
4182 value = digest_init (type, init, TREE_STATIC (decl),
4183 TREE_STATIC (decl) || pedantic);
4185 /* Store the expression if valid; else report error. */
4188 /* Note that this is the only place we can detect the error
4189 in a case such as struct foo bar = (struct foo) { x, y };
4190 where there is one initial value which is a constructor expression. */
4191 if (value == error_mark_node)
4193 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4195 error ("initializer for static variable is not constant");
4196 value = error_mark_node;
4198 else if (TREE_STATIC (decl)
4199 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4201 error ("initializer for static variable uses complicated arithmetic");
4202 value = error_mark_node;
4206 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4208 if (! TREE_CONSTANT (value))
4209 pedwarn ("aggregate initializer is not constant");
4210 else if (! TREE_STATIC (value))
4211 pedwarn ("aggregate initializer uses complicated arithmetic");
4216 DECL_INITIAL (decl) = value;
4218 /* ANSI wants warnings about out-of-range constant initializers. */
4219 STRIP_TYPE_NOPS (value);
4220 constant_expression_warning (value);
4223 /* Methods for storing and printing names for error messages. */
4225 /* Implement a spelling stack that allows components of a name to be pushed
4226 and popped. Each element on the stack is this structure. */
4238 #define SPELLING_STRING 1
4239 #define SPELLING_MEMBER 2
4240 #define SPELLING_BOUNDS 3
4242 static struct spelling *spelling; /* Next stack element (unused). */
4243 static struct spelling *spelling_base; /* Spelling stack base. */
4244 static int spelling_size; /* Size of the spelling stack. */
4246 /* Macros to save and restore the spelling stack around push_... functions.
4247 Alternative to SAVE_SPELLING_STACK. */
4249 #define SPELLING_DEPTH() (spelling - spelling_base)
4250 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4252 /* Save and restore the spelling stack around arbitrary C code. */
4254 #define SAVE_SPELLING_DEPTH(code) \
4256 int __depth = SPELLING_DEPTH (); \
4258 RESTORE_SPELLING_DEPTH (__depth); \
4261 /* Push an element on the spelling stack with type KIND and assign VALUE
4264 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4266 int depth = SPELLING_DEPTH (); \
4268 if (depth >= spelling_size) \
4270 spelling_size += 10; \
4271 if (spelling_base == 0) \
4273 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4276 = (struct spelling *) xrealloc (spelling_base, \
4277 spelling_size * sizeof (struct spelling)); \
4278 RESTORE_SPELLING_DEPTH (depth); \
4281 spelling->kind = (KIND); \
4282 spelling->MEMBER = (VALUE); \
4286 /* Push STRING on the stack. Printed literally. */
4289 push_string (string)
4292 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4295 /* Push a member name on the stack. Printed as '.' STRING. */
4298 push_member_name (decl)
4303 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4304 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4307 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4310 push_array_bounds (bounds)
4313 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4316 /* Compute the maximum size in bytes of the printed spelling. */
4321 register int size = 0;
4322 register struct spelling *p;
4324 for (p = spelling_base; p < spelling; p++)
4326 if (p->kind == SPELLING_BOUNDS)
4329 size += strlen (p->u.s) + 1;
4335 /* Print the spelling to BUFFER and return it. */
4338 print_spelling (buffer)
4339 register char *buffer;
4341 register char *d = buffer;
4343 register struct spelling *p;
4345 for (p = spelling_base; p < spelling; p++)
4346 if (p->kind == SPELLING_BOUNDS)
4348 sprintf (d, "[%d]", p->u.i);
4353 if (p->kind == SPELLING_MEMBER)
4355 for (s = p->u.s; *d = *s++; d++)
4362 /* Provide a means to pass component names derived from the spelling stack. */
4364 char initialization_message;
4366 /* Interpret the spelling of the given ERRTYPE message. */
4369 get_spelling (errtype)
4372 static char *buffer;
4373 static int size = -1;
4375 if (errtype == &initialization_message)
4377 /* Avoid counting chars */
4378 static char message[] = "initialization of `%s'";
4379 register int needed = sizeof (message) + spelling_length () + 1;
4383 buffer = (char *) xmalloc (size = needed);
4385 buffer = (char *) xrealloc (buffer, size = needed);
4387 temp = (char *) alloca (needed);
4388 sprintf (buffer, message, print_spelling (temp));
4395 /* Issue an error message for a bad initializer component.
4396 FORMAT describes the message. OFWHAT is the name for the component.
4397 LOCAL is a format string for formatting the insertion of the name
4400 If OFWHAT is null, the component name is stored on the spelling stack.
4401 If the component name is a null string, then LOCAL is omitted entirely. */
4404 error_init (format, local, ofwhat)
4405 char *format, *local, *ofwhat;
4410 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4411 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4414 sprintf (buffer, local, ofwhat);
4418 error (format, buffer);
4421 /* Issue a pedantic warning for a bad initializer component.
4422 FORMAT describes the message. OFWHAT is the name for the component.
4423 LOCAL is a format string for formatting the insertion of the name
4426 If OFWHAT is null, the component name is stored on the spelling stack.
4427 If the component name is a null string, then LOCAL is omitted entirely. */
4430 pedwarn_init (format, local, ofwhat)
4431 char *format, *local, *ofwhat;
4436 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4437 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4440 sprintf (buffer, local, ofwhat);
4444 pedwarn (format, buffer);
4447 /* Digest the parser output INIT as an initializer for type TYPE.
4448 Return a C expression of type TYPE to represent the initial value.
4450 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4451 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4452 applies only to elements of constructors. */
4455 digest_init (type, init, require_constant, constructor_constant)
4457 int require_constant, constructor_constant;
4459 enum tree_code code = TREE_CODE (type);
4460 tree inside_init = init;
4462 if (init == error_mark_node)
4465 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4466 /* Do not use STRIP_NOPS here. We do not want an enumerator
4467 whose value is 0 to count as a null pointer constant. */
4468 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4469 inside_init = TREE_OPERAND (init, 0);
4471 /* Initialization of an array of chars from a string constant
4472 optionally enclosed in braces. */
4474 if (code == ARRAY_TYPE)
4476 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4477 if ((typ1 == char_type_node
4478 || typ1 == signed_char_type_node
4479 || typ1 == unsigned_char_type_node
4480 || typ1 == unsigned_wchar_type_node
4481 || typ1 == signed_wchar_type_node)
4482 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4484 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4485 TYPE_MAIN_VARIANT (type)))
4488 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4490 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4492 error_init ("char-array%s initialized from wide string",
4494 return error_mark_node;
4496 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4498 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4500 error_init ("int-array%s initialized from non-wide string",
4502 return error_mark_node;
4505 TREE_TYPE (inside_init) = type;
4506 if (TYPE_DOMAIN (type) != 0
4507 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4509 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4510 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4511 /* Subtract 1 (or sizeof (wchar_t))
4512 because it's ok to ignore the terminating null char
4513 that is counted in the length of the constant. */
4514 if (size < TREE_STRING_LENGTH (inside_init)
4515 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4516 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4519 "initializer-string for array of chars%s is too long",
4526 /* Any type can be initialized
4527 from an expression of the same type, optionally with braces. */
4529 if (inside_init && TREE_TYPE (inside_init) != 0
4530 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4531 TYPE_MAIN_VARIANT (type))
4532 || (code == ARRAY_TYPE
4533 && comptypes (TREE_TYPE (inside_init), type))
4534 || (code == POINTER_TYPE
4535 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4536 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4537 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4538 TREE_TYPE (type)))))
4540 if (code == POINTER_TYPE
4541 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4542 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4543 inside_init = default_conversion (inside_init);
4544 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4545 && TREE_CODE (inside_init) != CONSTRUCTOR)
4547 error_init ("array%s initialized from non-constant array expression",
4549 return error_mark_node;
4552 if (optimize && TREE_READONLY (inside_init)
4553 && TREE_CODE (inside_init) == VAR_DECL)
4554 inside_init = decl_constant_value (inside_init);
4556 /* Compound expressions can only occur here if -pedantic or
4557 -pedantic-errors is specified. In the later case, we always want
4558 an error. In the former case, we simply want a warning. */
4559 if (require_constant && pedantic
4560 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4563 = valid_compound_expr_initializer (inside_init,
4564 TREE_TYPE (inside_init));
4565 if (inside_init == error_mark_node)
4566 error_init ("initializer element%s is not constant",
4569 pedwarn_init ("initializer element%s is not constant",
4571 if (flag_pedantic_errors)
4572 inside_init = error_mark_node;
4574 else if (require_constant && ! TREE_CONSTANT (inside_init))
4576 error_init ("initializer element%s is not constant",
4578 inside_init = error_mark_node;
4580 else if (require_constant
4581 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4583 error_init ("initializer element%s is not computable at load time",
4585 inside_init = error_mark_node;
4591 /* Handle scalar types, including conversions. */
4593 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4594 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4596 /* Note that convert_for_assignment calls default_conversion
4597 for arrays and functions. We must not call it in the
4598 case where inside_init is a null pointer constant. */
4600 = convert_for_assignment (type, init, "initialization",
4601 NULL_TREE, NULL_TREE, 0);
4603 if (require_constant && ! TREE_CONSTANT (inside_init))
4605 error_init ("initializer element%s is not constant",
4607 inside_init = error_mark_node;
4609 else if (require_constant
4610 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4612 error_init ("initializer element%s is not computable at load time",
4614 inside_init = error_mark_node;
4620 /* Come here only for records and arrays. */
4622 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4624 error_init ("variable-sized object%s may not be initialized",
4626 return error_mark_node;
4629 /* Traditionally, you can write struct foo x = 0;
4630 and it initializes the first element of x to 0. */
4631 if (flag_traditional)
4633 tree top = 0, prev = 0;
4634 while (TREE_CODE (type) == RECORD_TYPE
4635 || TREE_CODE (type) == ARRAY_TYPE
4636 || TREE_CODE (type) == QUAL_UNION_TYPE
4637 || TREE_CODE (type) == UNION_TYPE)
4639 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4643 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4645 if (TREE_CODE (type) == ARRAY_TYPE)
4646 type = TREE_TYPE (type);
4647 else if (TYPE_FIELDS (type))
4648 type = TREE_TYPE (TYPE_FIELDS (type));
4651 error_init ("invalid initializer%s", " for `%s'", NULL);
4652 return error_mark_node;
4655 TREE_OPERAND (prev, 1)
4656 = build_tree_list (NULL_TREE,
4657 digest_init (type, init, require_constant,
4658 constructor_constant));
4661 error_init ("invalid initializer%s", " for `%s'", NULL);
4662 return error_mark_node;
4665 /* Handle initializers that use braces. */
4667 static void output_init_element ();
4668 static void output_pending_init_elements ();
4669 static void check_init_type_bitfields ();
4671 /* Type of object we are accumulating a constructor for.
4672 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4673 static tree constructor_type;
4675 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4677 static tree constructor_fields;
4679 /* For an ARRAY_TYPE, this is the specified index
4680 at which to store the next element we get.
4681 This is a special INTEGER_CST node that we modify in place. */
4682 static tree constructor_index;
4684 /* For an ARRAY_TYPE, this is the end index of the range
4685 to intitialize with the next element, or NULL in the ordinary case
4686 where the element is used just once. */
4687 static tree constructor_range_end;
4689 /* For an ARRAY_TYPE, this is the maximum index. */
4690 static tree constructor_max_index;
4692 /* For a RECORD_TYPE, this is the first field not yet written out. */
4693 static tree constructor_unfilled_fields;
4695 /* For an ARRAY_TYPE, this is the index of the first element
4696 not yet written out.
4697 This is a special INTEGER_CST node that we modify in place. */
4698 static tree constructor_unfilled_index;
4700 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4701 This is so we can generate gaps between fields, when appropriate.
4702 This is a special INTEGER_CST node that we modify in place. */
4703 static tree constructor_bit_index;
4705 /* If we are saving up the elements rather than allocating them,
4706 this is the list of elements so far (in reverse order,
4707 most recent first). */
4708 static tree constructor_elements;
4710 /* 1 if so far this constructor's elements are all compile-time constants. */
4711 static int constructor_constant;
4713 /* 1 if so far this constructor's elements are all valid address constants. */
4714 static int constructor_simple;
4716 /* 1 if this constructor is erroneous so far. */
4717 static int constructor_erroneous;
4719 /* 1 if have called defer_addressed_constants. */
4720 static int constructor_subconstants_deferred;
4722 /* List of pending elements at this constructor level.
4723 These are elements encountered out of order
4724 which belong at places we haven't reached yet in actually
4725 writing the output. */
4726 static tree constructor_pending_elts;
4728 /* The SPELLING_DEPTH of this constructor. */
4729 static int constructor_depth;
4731 /* 0 if implicitly pushing constructor levels is allowed. */
4732 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4734 /* 1 if this constructor level was entered implicitly. */
4735 static int constructor_implicit;
4737 static int require_constant_value;
4738 static int require_constant_elements;
4740 /* 1 if it is ok to output this constructor as we read it.
4741 0 means must accumulate a CONSTRUCTOR expression. */
4742 static int constructor_incremental;
4744 /* DECL node for which an initializer is being read.
4745 0 means we are reading a constructor expression
4746 such as (struct foo) {...}. */
4747 static tree constructor_decl;
4749 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4750 static char *constructor_asmspec;
4752 /* Nonzero if this is an initializer for a top-level decl. */
4753 static int constructor_top_level;
4755 /* When we finish reading a constructor expression
4756 (constructor_decl is 0), the CONSTRUCTOR goes here. */
4757 static tree constructor_result;
4759 /* This stack has a level for each implicit or explicit level of
4760 structuring in the initializer, including the outermost one. It
4761 saves the values of most of the variables above. */
4763 struct constructor_stack
4765 struct constructor_stack *next;
4771 tree unfilled_index;
4772 tree unfilled_fields;
4778 /* If nonzero, this value should replace the entire
4779 constructor at this level. */
4780 tree replacement_value;
4789 struct constructor_stack *constructor_stack;
4791 /* This stack records separate initializers that are nested.
4792 Nested initializers can't happen in ANSI C, but GNU C allows them
4793 in cases like { ... (struct foo) { ... } ... }. */
4795 struct initializer_stack
4797 struct initializer_stack *next;
4800 struct constructor_stack *constructor_stack;
4801 struct spelling *spelling;
4802 struct spelling *spelling_base;
4806 char require_constant_value;
4807 char require_constant_elements;
4811 struct initializer_stack *initializer_stack;
4813 /* Prepare to parse and output the initializer for variable DECL. */
4816 start_init (decl, asmspec_tree, top_level)
4822 struct initializer_stack *p
4823 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
4827 asmspec = TREE_STRING_POINTER (asmspec_tree);
4829 p->decl = constructor_decl;
4830 p->asmspec = constructor_asmspec;
4831 p->incremental = constructor_incremental;
4832 p->require_constant_value = require_constant_value;
4833 p->require_constant_elements = require_constant_elements;
4834 p->constructor_stack = constructor_stack;
4835 p->spelling = spelling;
4836 p->spelling_base = spelling_base;
4837 p->spelling_size = spelling_size;
4838 p->deferred = constructor_subconstants_deferred;
4839 p->top_level = constructor_top_level;
4840 p->next = initializer_stack;
4841 initializer_stack = p;
4843 constructor_decl = decl;
4844 constructor_incremental = top_level;
4845 constructor_asmspec = asmspec;
4846 constructor_subconstants_deferred = 0;
4847 constructor_top_level = top_level;
4851 require_constant_value = TREE_STATIC (decl);
4852 require_constant_elements
4853 = ((TREE_STATIC (decl) || pedantic)
4854 /* For a scalar, you can always use any value to initialize,
4855 even within braces. */
4856 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4857 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4858 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4859 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4860 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4861 constructor_incremental |= TREE_STATIC (decl);
4865 require_constant_value = 0;
4866 require_constant_elements = 0;
4867 locus = "(anonymous)";
4870 constructor_stack = 0;
4874 RESTORE_SPELLING_DEPTH (0);
4877 push_string (locus);
4883 struct initializer_stack *p = initializer_stack;
4885 /* Output subconstants (string constants, usually)
4886 that were referenced within this initializer and saved up.
4887 Must do this if and only if we called defer_addressed_constants. */
4888 if (constructor_subconstants_deferred)
4889 output_deferred_addressed_constants ();
4891 /* Free the whole constructor stack of this initializer. */
4892 while (constructor_stack)
4894 struct constructor_stack *q = constructor_stack;
4895 constructor_stack = q->next;
4899 /* Pop back to the data of the outer initializer (if any). */
4900 constructor_decl = p->decl;
4901 constructor_asmspec = p->asmspec;
4902 constructor_incremental = p->incremental;
4903 require_constant_value = p->require_constant_value;
4904 require_constant_elements = p->require_constant_elements;
4905 constructor_stack = p->constructor_stack;
4906 spelling = p->spelling;
4907 spelling_base = p->spelling_base;
4908 spelling_size = p->spelling_size;
4909 constructor_subconstants_deferred = p->deferred;
4910 constructor_top_level = p->top_level;
4911 initializer_stack = p->next;
4915 /* Call here when we see the initializer is surrounded by braces.
4916 This is instead of a call to push_init_level;
4917 it is matched by a call to pop_init_level.
4919 TYPE is the type to initialize, for a constructor expression.
4920 For an initializer for a decl, TYPE is zero. */
4923 really_start_incremental_init (type)
4926 struct constructor_stack *p
4927 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
4930 type = TREE_TYPE (constructor_decl);
4932 /* Turn off constructor_incremental if type is a struct with bitfields.
4933 Do this before the first push, so that the corrected value
4934 is available in finish_init. */
4935 check_init_type_bitfields (type);
4937 p->type = constructor_type;
4938 p->fields = constructor_fields;
4939 p->index = constructor_index;
4940 p->range_end = constructor_range_end;
4941 p->max_index = constructor_max_index;
4942 p->unfilled_index = constructor_unfilled_index;
4943 p->unfilled_fields = constructor_unfilled_fields;
4944 p->bit_index = constructor_bit_index;
4946 p->constant = constructor_constant;
4947 p->simple = constructor_simple;
4948 p->erroneous = constructor_erroneous;
4949 p->pending_elts = constructor_pending_elts;
4950 p->depth = constructor_depth;
4951 p->replacement_value = 0;
4953 p->incremental = constructor_incremental;
4956 constructor_stack = p;
4958 constructor_constant = 1;
4959 constructor_simple = 1;
4960 constructor_depth = SPELLING_DEPTH ();
4961 constructor_elements = 0;
4962 constructor_pending_elts = 0;
4963 constructor_type = type;
4965 if (TREE_CODE (constructor_type) == RECORD_TYPE
4966 || TREE_CODE (constructor_type) == UNION_TYPE)
4968 constructor_fields = TYPE_FIELDS (constructor_type);
4969 /* Skip any nameless bit fields atthe beginning. */
4970 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
4971 && DECL_NAME (constructor_fields) == 0)
4972 constructor_fields = TREE_CHAIN (constructor_fields);
4973 constructor_unfilled_fields = constructor_fields;
4974 constructor_bit_index = copy_node (integer_zero_node);
4976 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4978 constructor_range_end = 0;
4979 if (TYPE_DOMAIN (constructor_type))
4981 constructor_max_index
4982 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4984 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4987 constructor_index = copy_node (integer_zero_node);
4988 constructor_unfilled_index = copy_node (constructor_index);
4992 /* Handle the case of int x = {5}; */
4993 constructor_fields = constructor_type;
4994 constructor_unfilled_fields = constructor_type;
4997 if (constructor_incremental)
4999 int momentary = suspend_momentary ();
5000 push_obstacks_nochange ();
5001 if (TREE_PERMANENT (constructor_decl))
5002 end_temporary_allocation ();
5003 make_decl_rtl (constructor_decl, constructor_asmspec,
5004 constructor_top_level);
5005 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5007 resume_momentary (momentary);
5010 if (constructor_incremental)
5012 defer_addressed_constants ();
5013 constructor_subconstants_deferred = 1;
5017 /* Push down into a subobject, for initialization.
5018 If this is for an explicit set of braces, IMPLICIT is 0.
5019 If it is because the next element belongs at a lower level,
5023 push_init_level (implicit)
5026 struct constructor_stack *p;
5028 /* If we've exhausted any levels that didn't have braces,
5030 while (constructor_stack->implicit)
5032 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5033 || TREE_CODE (constructor_type) == UNION_TYPE)
5034 && constructor_fields == 0)
5035 process_init_element (pop_init_level (1));
5036 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5037 && tree_int_cst_lt (constructor_max_index, constructor_index))
5038 process_init_element (pop_init_level (1));
5043 /* Structure elements may require alignment. Do this now
5044 if necessary for the subaggregate. */
5045 if (constructor_incremental && TREE_CODE (constructor_type) == RECORD_TYPE
5046 && constructor_fields)
5048 /* Advance to offset of this element. */
5049 if (! tree_int_cst_equal (constructor_bit_index,
5050 DECL_FIELD_BITPOS (constructor_fields)))
5052 int next = (TREE_INT_CST_LOW
5053 (DECL_FIELD_BITPOS (constructor_fields))
5055 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5058 assemble_zeros (next - here);
5062 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5063 p->type = constructor_type;
5064 p->fields = constructor_fields;
5065 p->index = constructor_index;
5066 p->range_end = constructor_range_end;
5067 p->max_index = constructor_max_index;
5068 p->unfilled_index = constructor_unfilled_index;
5069 p->unfilled_fields = constructor_unfilled_fields;
5070 p->bit_index = constructor_bit_index;
5071 p->elements = constructor_elements;
5072 p->constant = constructor_constant;
5073 p->simple = constructor_simple;
5074 p->erroneous = constructor_erroneous;
5075 p->pending_elts = constructor_pending_elts;
5076 p->depth = constructor_depth;
5077 p->replacement_value = 0;
5078 p->implicit = implicit;
5079 p->incremental = constructor_incremental;
5081 p->next = constructor_stack;
5082 constructor_stack = p;
5084 constructor_constant = 1;
5085 constructor_simple = 1;
5086 constructor_depth = SPELLING_DEPTH ();
5087 constructor_elements = 0;
5088 constructor_pending_elts = 0;
5090 /* Don't die if an entire brace-pair level is superfluous
5091 in the containing level. */
5092 if (constructor_type == 0)
5094 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5095 || TREE_CODE (constructor_type) == UNION_TYPE)
5097 /* Don't die if there are extra init elts at the end. */
5098 if (constructor_fields == 0)
5099 constructor_type = 0;
5102 constructor_type = TREE_TYPE (constructor_fields);
5103 push_member_name (constructor_fields);
5106 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5108 constructor_type = TREE_TYPE (constructor_type);
5109 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5112 /* Turn off constructor_incremental if type is a struct with bitfields. */
5113 if (constructor_type != 0)
5114 check_init_type_bitfields (constructor_type);
5116 if (constructor_type == 0)
5118 error_init ("extra brace group at end of initializer%s",
5120 constructor_fields = 0;
5121 constructor_unfilled_fields = 0;
5123 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5124 || TREE_CODE (constructor_type) == UNION_TYPE)
5126 constructor_fields = TYPE_FIELDS (constructor_type);
5127 /* Skip any nameless bit fields atthe beginning. */
5128 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5129 && DECL_NAME (constructor_fields) == 0)
5130 constructor_fields = TREE_CHAIN (constructor_fields);
5131 constructor_unfilled_fields = constructor_fields;
5132 constructor_bit_index = copy_node (integer_zero_node);
5134 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5136 constructor_range_end = 0;
5137 if (TYPE_DOMAIN (constructor_type))
5139 constructor_max_index
5140 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5142 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5145 constructor_index = copy_node (integer_zero_node);
5146 constructor_unfilled_index = copy_node (constructor_index);
5150 warning ("braces around scalar initializer");
5151 constructor_fields = constructor_type;
5152 constructor_unfilled_fields = constructor_type;
5156 /* Don't read a struct incrementally if it has any bitfields,
5157 because the incremental reading code doesn't know how to
5158 handle bitfields yet. */
5161 check_init_type_bitfields (type)
5164 if (TREE_CODE (type) == RECORD_TYPE)
5167 for (tail = TYPE_FIELDS (type); tail;
5168 tail = TREE_CHAIN (tail))
5169 if (DECL_BIT_FIELD (tail)
5170 /* This catches cases like `int foo : 8;'. */
5171 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail)))
5173 constructor_incremental = 0;
5179 /* At the end of an implicit or explicit brace level,
5180 finish up that level of constructor.
5181 If we were outputting the elements as they are read, return 0
5182 from inner levels (process_init_element ignores that),
5183 but return error_mark_node from the outermost level
5184 (that's what we want to put in DECL_INITIAL).
5185 Otherwise, return a CONSTRUCTOR expression. */
5188 pop_init_level (implicit)
5191 struct constructor_stack *p;
5193 tree constructor = 0;
5197 /* When we come to an explicit close brace,
5198 pop any inner levels that didn't have explicit braces. */
5199 while (constructor_stack->implicit)
5200 process_init_element (pop_init_level (1));
5203 p = constructor_stack;
5205 if (constructor_type != 0)
5206 size = int_size_in_bytes (constructor_type);
5208 /* Now output all pending elements. */
5209 output_pending_init_elements (1);
5211 #if 0 /* c-parse.in warns about {}. */
5212 /* In ANSI, each brace level must have at least one element. */
5213 if (! implicit && pedantic
5214 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5215 ? integer_zerop (constructor_unfilled_index)
5216 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5217 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL);
5220 /* Pad out the end of the structure. */
5222 if (p->replacement_value)
5224 /* If this closes a superfluous brace pair,
5225 just pass out the element between them. */
5226 constructor = p->replacement_value;
5227 /* If this is the top level thing within the initializer,
5228 and it's for a variable, then since we already called
5229 assemble_variable, we must output the value now. */
5230 if (p->next == 0 && constructor_decl != 0
5231 && constructor_incremental)
5233 constructor = digest_init (constructor_type, constructor,
5236 /* If initializing an array of unknown size,
5237 determine the size now. */
5238 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5239 && TYPE_DOMAIN (constructor_type) == 0)
5243 push_obstacks_nochange ();
5244 if (TREE_PERMANENT (constructor_type))
5245 end_temporary_allocation ();
5247 /* We shouldn't have an incomplete array type within
5249 if (constructor_stack->next)
5253 = complete_array_type (constructor_type,
5258 size = int_size_in_bytes (constructor_type);
5262 output_constant (constructor, size);
5265 else if (constructor_type == 0)
5267 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5268 && TREE_CODE (constructor_type) != UNION_TYPE
5269 && TREE_CODE (constructor_type) != ARRAY_TYPE
5270 && ! constructor_incremental)
5272 /* A nonincremental scalar initializer--just return
5273 the element, after verifying there is just one. */
5274 if (constructor_elements == 0)
5276 error_init ("empty scalar initializer%s",
5278 constructor = error_mark_node;
5280 else if (TREE_CHAIN (constructor_elements) != 0)
5282 error_init ("extra elements in scalar initializer%s",
5284 constructor = TREE_VALUE (constructor_elements);
5287 constructor = TREE_VALUE (constructor_elements);
5289 else if (! constructor_incremental)
5291 if (constructor_erroneous)
5292 constructor = error_mark_node;
5295 int momentary = suspend_momentary ();
5297 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5298 nreverse (constructor_elements));
5299 if (constructor_constant)
5300 TREE_CONSTANT (constructor) = 1;
5301 if (constructor_constant && constructor_simple)
5302 TREE_STATIC (constructor) = 1;
5304 resume_momentary (momentary);
5310 int momentary = suspend_momentary ();
5312 if (TREE_CODE (constructor_type) == RECORD_TYPE
5313 || TREE_CODE (constructor_type) == UNION_TYPE)
5315 /* Find the offset of the end of that field. */
5316 filled = size_binop (CEIL_DIV_EXPR,
5317 constructor_bit_index,
5318 size_int (BITS_PER_UNIT));
5320 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5322 /* If initializing an array of unknown size,
5323 determine the size now. */
5324 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5325 && TYPE_DOMAIN (constructor_type) == 0)
5328 = size_binop (MINUS_EXPR,
5329 constructor_unfilled_index,
5332 push_obstacks_nochange ();
5333 if (TREE_PERMANENT (constructor_type))
5334 end_temporary_allocation ();
5335 maxindex = copy_node (maxindex);
5336 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5337 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5339 /* We shouldn't have an incomplete array type within
5341 if (constructor_stack->next)
5345 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)),
5347 error_with_decl (constructor_decl, "zero-size array `%s'");
5348 layout_type (constructor_type);
5349 size = int_size_in_bytes (constructor_type);
5353 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5354 size_in_bytes (TREE_TYPE (constructor_type)));
5360 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5362 resume_momentary (momentary);
5366 constructor_type = p->type;
5367 constructor_fields = p->fields;
5368 constructor_index = p->index;
5369 constructor_range_end = p->range_end;
5370 constructor_max_index = p->max_index;
5371 constructor_unfilled_index = p->unfilled_index;
5372 constructor_unfilled_fields = p->unfilled_fields;
5373 constructor_bit_index = p->bit_index;
5374 constructor_elements = p->elements;
5375 constructor_constant = p->constant;
5376 constructor_simple = p->simple;
5377 constructor_erroneous = p->erroneous;
5378 constructor_pending_elts = p->pending_elts;
5379 constructor_depth = p->depth;
5380 constructor_incremental = p->incremental;
5381 RESTORE_SPELLING_DEPTH (constructor_depth);
5383 constructor_stack = p->next;
5386 if (constructor == 0)
5388 if (constructor_stack == 0)
5389 return error_mark_node;
5395 /* Within an array initializer, specify the next index to be initialized.
5396 FIRST is that index. If LAST is nonzero, then initialize a range
5397 of indices, running from FIRST through LAST. */
5400 set_init_index (first, last)
5403 while ((TREE_CODE (first) == NOP_EXPR
5404 || TREE_CODE (first) == CONVERT_EXPR
5405 || TREE_CODE (first) == NON_LVALUE_EXPR)
5406 && (TYPE_MODE (TREE_TYPE (first))
5407 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5408 (first) = TREE_OPERAND (first, 0);
5410 while ((TREE_CODE (last) == NOP_EXPR
5411 || TREE_CODE (last) == CONVERT_EXPR
5412 || TREE_CODE (last) == NON_LVALUE_EXPR)
5413 && (TYPE_MODE (TREE_TYPE (last))
5414 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5415 (last) = TREE_OPERAND (last, 0);
5417 if (TREE_CODE (first) != INTEGER_CST)
5418 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5419 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5420 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5421 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5422 error_init ("duplicate array index in initializer%s", " for `%s'", NULL);
5425 TREE_INT_CST_LOW (constructor_index)
5426 = TREE_INT_CST_LOW (first);
5427 TREE_INT_CST_HIGH (constructor_index)
5428 = TREE_INT_CST_HIGH (first);
5430 if (last != 0 && tree_int_cst_lt (last, first))
5431 error_init ("empty index range in initializer%s", " for `%s'", NULL);
5435 pedwarn ("ANSI C forbids specifying element to initialize");
5436 constructor_range_end = last;
5441 /* Within a struct initializer, specify the next field to be initialized. */
5444 set_init_label (fieldname)
5450 for (tail = TYPE_FIELDS (constructor_type); tail;
5451 tail = TREE_CHAIN (tail))
5453 if (tail == constructor_unfilled_fields)
5455 if (DECL_NAME (tail) == fieldname)
5460 error ("unknown field `%s' specified in initializer",
5461 IDENTIFIER_POINTER (fieldname));
5463 error ("field `%s' already initialized",
5464 IDENTIFIER_POINTER (fieldname));
5467 constructor_fields = tail;
5469 pedwarn ("ANSI C forbids specifying structure member to initialize");
5473 /* "Output" the next constructor element.
5474 At top level, really output it to assembler code now.
5475 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5476 TYPE is the data type that the containing data type wants here.
5477 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5479 PENDING if non-nil means output pending elements that belong
5480 right after this element. (PENDING is normally 1;
5481 it is 0 while outputting pending elements, to avoid recursion.) */
5484 output_init_element (value, type, field, pending)
5485 tree value, type, field;
5490 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5491 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5492 && !(TREE_CODE (value) == STRING_CST
5493 && TREE_CODE (type) == ARRAY_TYPE
5494 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5495 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5496 TYPE_MAIN_VARIANT (type))))
5497 value = default_conversion (value);
5499 if (value == error_mark_node)
5500 constructor_erroneous = 1;
5501 else if (!TREE_CONSTANT (value))
5502 constructor_constant = 0;
5503 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5504 constructor_simple = 0;
5506 if (require_constant_value && ! TREE_CONSTANT (value))
5508 error_init ("initializer element%s is not constant",
5510 value = error_mark_node;
5512 else if (require_constant_elements
5513 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5515 error_init ("initializer element%s is not computable at load time",
5517 value = error_mark_node;
5520 /* If this element duplicates one on constructor_pending_elts,
5521 print a message and ignore it. Don't do this when we're
5522 processing elements taken off constructor_pending_elts,
5523 because we'd always get spurious errors. */
5526 if (TREE_CODE (constructor_type) == RECORD_TYPE
5527 || TREE_CODE (constructor_type) == UNION_TYPE)
5529 if (purpose_member (field, constructor_pending_elts))
5531 error_init ("duplicate initializer%s", " for `%s'", NULL);
5535 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5538 for (tail = constructor_pending_elts; tail;
5539 tail = TREE_CHAIN (tail))
5540 if (TREE_PURPOSE (tail) != 0
5541 && TREE_CODE (TREE_PURPOSE (tail)) == INTEGER_CST
5542 && tree_int_cst_equal (TREE_PURPOSE (tail), constructor_index))
5547 error_init ("duplicate initializer%s", " for `%s'", NULL);
5553 /* If this element doesn't come next in sequence,
5554 put it on constructor_pending_elts. */
5555 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5556 && !tree_int_cst_equal (field, constructor_unfilled_index))
5559 /* The copy_node is needed in case field is actually
5560 constructor_index, which is modified in place. */
5561 constructor_pending_elts
5562 = tree_cons (copy_node (field),
5563 digest_init (type, value, 0, 0),
5564 constructor_pending_elts);
5566 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5567 && field != constructor_unfilled_fields)
5569 /* We do this for records but not for unions. In a union,
5570 no matter which field is specified, it can be initialized
5571 right away since it starts at the beginning of the union. */
5573 constructor_pending_elts
5575 digest_init (type, value, 0, 0),
5576 constructor_pending_elts);
5580 /* Otherwise, output this element either to
5581 constructor_elements or to the assembler file. */
5585 if (! constructor_incremental)
5587 if (field && TREE_CODE (field) == INTEGER_CST)
5588 field = copy_node (field);
5589 constructor_elements
5590 = tree_cons (field, digest_init (type, value, 0, 0),
5591 constructor_elements);
5595 /* Structure elements may require alignment.
5596 Do this, if necessary. */
5597 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5599 /* Advance to offset of this element. */
5600 if (! tree_int_cst_equal (constructor_bit_index,
5601 DECL_FIELD_BITPOS (constructor_fields)))
5603 int next = (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
5605 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5608 assemble_zeros (next - here);
5611 output_constant (digest_init (type, value, 0, 0),
5612 int_size_in_bytes (type));
5614 /* For a record or union,
5615 keep track of end position of last field. */
5616 if (TREE_CODE (constructor_type) == RECORD_TYPE
5617 || TREE_CODE (constructor_type) == UNION_TYPE)
5619 tree temp = size_binop (PLUS_EXPR,
5620 DECL_FIELD_BITPOS (constructor_fields),
5621 DECL_SIZE (constructor_fields));
5622 TREE_INT_CST_LOW (constructor_bit_index)
5623 = TREE_INT_CST_LOW (temp);
5624 TREE_INT_CST_HIGH (constructor_bit_index)
5625 = TREE_INT_CST_HIGH (temp);
5630 /* Advance the variable that indicates sequential elements output. */
5631 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5633 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
5635 TREE_INT_CST_LOW (constructor_unfilled_index)
5636 = TREE_INT_CST_LOW (tem);
5637 TREE_INT_CST_HIGH (constructor_unfilled_index)
5638 = TREE_INT_CST_HIGH (tem);
5640 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5641 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5642 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5643 constructor_unfilled_fields = 0;
5645 /* Now output any pending elements which have become next. */
5647 output_pending_init_elements (0);
5651 /* Output any pending elements which have become next.
5652 As we output elements, constructor_unfilled_{fields,index}
5653 advances, which may cause other elements to become next;
5654 if so, they too are output.
5656 If ALL is 0, we return when there are
5657 no more pending elements to output now.
5659 If ALL is 1, we output space as necessary so that
5660 we can output all the pending elements. */
5663 output_pending_init_elements (all)
5671 /* Look thru the whole pending list.
5672 If we find an element that should be output now,
5673 output it. Otherwise, set NEXT to the element
5674 that comes first among those still pending. */
5677 for (tail = constructor_pending_elts; tail;
5678 tail = TREE_CHAIN (tail))
5680 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5682 if (tree_int_cst_equal (TREE_PURPOSE (tail),
5683 constructor_unfilled_index))
5685 output_init_element (TREE_VALUE (tail), TREE_TYPE (constructor_type),
5686 constructor_unfilled_index, 0);
5689 else if (tree_int_cst_lt (TREE_PURPOSE (tail),
5690 constructor_unfilled_index))
5693 || tree_int_cst_lt (TREE_PURPOSE (tail),
5695 next = TREE_PURPOSE (tail);
5697 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5698 || TREE_CODE (constructor_type) == UNION_TYPE)
5700 if (TREE_PURPOSE (tail) == constructor_unfilled_fields)
5702 output_init_element (TREE_VALUE (tail),
5703 TREE_TYPE (constructor_unfilled_fields),
5704 constructor_unfilled_fields,
5708 else if (constructor_unfilled_fields == 0
5709 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
5710 DECL_FIELD_BITPOS (constructor_unfilled_fields)))
5713 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
5714 DECL_FIELD_BITPOS (next)))
5715 next = TREE_PURPOSE (tail);
5719 /* Ordinarily return, but not if we want to output all
5720 and there are elements left. */
5721 if (! (all && next != 0))
5724 /* Generate space up to the position of NEXT. */
5725 if (constructor_incremental)
5730 if (TREE_CODE (constructor_type) == RECORD_TYPE
5731 || TREE_CODE (constructor_type) == UNION_TYPE)
5733 /* Find the last field written out. */
5734 for (tail = TYPE_FIELDS (constructor_type); tail;
5735 tail = TREE_CHAIN (tail))
5736 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
5738 /* Find the offset of the end of that field. */
5739 filled = size_binop (CEIL_DIV_EXPR,
5740 size_binop (PLUS_EXPR,
5741 DECL_FIELD_BITPOS (tail),
5743 size_int (BITS_PER_UNIT));
5744 nextpos_tree = size_binop (CEIL_DIV_EXPR,
5745 DECL_FIELD_BITPOS (next),
5746 size_int (BITS_PER_UNIT));
5747 constructor_unfilled_fields = next;
5749 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5751 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5752 size_in_bytes (TREE_TYPE (constructor_type)));
5754 = size_binop (MULT_EXPR, next,
5755 size_in_bytes (TREE_TYPE (constructor_type)));
5756 TREE_INT_CST_LOW (constructor_unfilled_index)
5757 = TREE_INT_CST_LOW (next);
5758 TREE_INT_CST_HIGH (constructor_unfilled_index)
5759 = TREE_INT_CST_HIGH (next);
5766 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
5768 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
5773 /* If it's not incremental, just skip over the gap,
5774 so that after jumping to retry we will output the next
5775 successive element. */
5776 if (TREE_CODE (constructor_type) == RECORD_TYPE
5777 || TREE_CODE (constructor_type) == UNION_TYPE)
5778 constructor_unfilled_fields = next;
5779 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5781 TREE_INT_CST_LOW (constructor_unfilled_index)
5782 = TREE_INT_CST_LOW (next);
5783 TREE_INT_CST_HIGH (constructor_unfilled_index)
5784 = TREE_INT_CST_HIGH (next);
5791 /* Add one non-braced element to the current constructor level.
5792 This adjusts the current position within the constructor's type.
5793 This may also start or terminate implicit levels
5794 to handle a partly-braced initializer.
5796 Once this has found the correct level for the new element,
5797 it calls output_init_element.
5799 Note: if we are incrementally outputting this constructor,
5800 this function may be called with a null argument
5801 representing a sub-constructor that was already incrementally output.
5802 When that happens, we output nothing, but we do the bookkeeping
5803 to skip past that element of the current constructor. */
5806 process_init_element (value)
5809 tree orig_value = value;
5810 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5812 /* Handle superfluous braces around string cst as in
5813 char x[] = {"foo"}; */
5815 && TREE_CODE (constructor_type) == ARRAY_TYPE
5816 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5817 && integer_zerop (constructor_unfilled_index))
5819 constructor_stack->replacement_value = value;
5823 if (constructor_stack->replacement_value != 0)
5825 error_init ("excess elements in struct initializer%s",
5826 " after `%s'", NULL_PTR);
5830 /* Ignore elements of a brace group if it is entirely superfluous
5831 and has already been diagnosed. */
5832 if (constructor_type == 0)
5835 /* If we've exhausted any levels that didn't have braces,
5837 while (constructor_stack->implicit)
5839 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5840 || TREE_CODE (constructor_type) == UNION_TYPE)
5841 && constructor_fields == 0)
5842 process_init_element (pop_init_level (1));
5843 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5844 && tree_int_cst_lt (constructor_max_index, constructor_index))
5845 process_init_element (pop_init_level (1));
5852 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5855 enum tree_code fieldcode;
5857 if (constructor_fields == 0)
5859 pedwarn_init ("excess elements in struct initializer%s",
5860 " after `%s'", NULL_PTR);
5864 fieldtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_fields));
5865 fieldcode = TREE_CODE (fieldtype);
5867 /* Accept a string constant to initialize a subarray. */
5869 && fieldcode == ARRAY_TYPE
5870 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5873 /* Otherwise, if we have come to a subaggregate,
5874 and we don't have an element of its type, push into it. */
5875 else if (value != 0 && !constructor_no_implicit
5876 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5877 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5878 || fieldcode == UNION_TYPE))
5880 push_init_level (1);
5886 push_member_name (constructor_fields);
5887 output_init_element (value, fieldtype, constructor_fields, 1);
5888 RESTORE_SPELLING_DEPTH (constructor_depth);
5891 /* Do the bookkeeping for an element that was
5892 directly output as a constructor. */
5894 /* For a record, keep track of end position of last field. */
5895 tree temp = size_binop (PLUS_EXPR,
5896 DECL_FIELD_BITPOS (constructor_fields),
5897 DECL_SIZE (constructor_fields));
5898 TREE_INT_CST_LOW (constructor_bit_index)
5899 = TREE_INT_CST_LOW (temp);
5900 TREE_INT_CST_HIGH (constructor_bit_index)
5901 = TREE_INT_CST_HIGH (temp);
5903 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5906 constructor_fields = TREE_CHAIN (constructor_fields);
5907 /* Skip any nameless bit fields atthe beginning. */
5908 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5909 && DECL_NAME (constructor_fields) == 0)
5910 constructor_fields = TREE_CHAIN (constructor_fields);
5913 if (TREE_CODE (constructor_type) == UNION_TYPE)
5916 enum tree_code fieldcode;
5918 if (constructor_fields == 0)
5920 pedwarn_init ("excess elements in union initializer%s",
5921 " after `%s'", NULL_PTR);
5925 fieldtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_fields));
5926 fieldcode = TREE_CODE (fieldtype);
5928 /* Accept a string constant to initialize a subarray. */
5930 && fieldcode == ARRAY_TYPE
5931 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5934 /* Otherwise, if we have come to a subaggregate,
5935 and we don't have an element of its type, push into it. */
5936 else if (value != 0 && !constructor_no_implicit
5937 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5938 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5939 || fieldcode == UNION_TYPE))
5941 push_init_level (1);
5947 push_member_name (constructor_fields);
5948 output_init_element (value, fieldtype, constructor_fields, 1);
5949 RESTORE_SPELLING_DEPTH (constructor_depth);
5952 /* Do the bookkeeping for an element that was
5953 directly output as a constructor. */
5955 TREE_INT_CST_LOW (constructor_bit_index)
5956 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
5957 TREE_INT_CST_HIGH (constructor_bit_index)
5958 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
5960 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5963 constructor_fields = 0;
5966 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5968 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5969 enum tree_code eltcode = TREE_CODE (elttype);
5971 /* Accept a string constant to initialize a subarray. */
5973 && eltcode == ARRAY_TYPE
5974 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
5977 /* Otherwise, if we have come to a subaggregate,
5978 and we don't have an element of its type, push into it. */
5979 else if (value != 0 && !constructor_no_implicit
5980 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
5981 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
5982 || eltcode == UNION_TYPE))
5984 push_init_level (1);
5988 if (constructor_max_index != 0
5989 && tree_int_cst_lt (constructor_max_index, constructor_index))
5991 pedwarn_init ("excess elements in array initializer%s",
5992 " after `%s'", NULL_PTR);
5996 /* Now output the actual element.
5997 Ordinarily, output once.
5998 If there is a range, repeat it till we advance past the range. */
6005 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6006 output_init_element (value, elttype, constructor_index, 1);
6007 RESTORE_SPELLING_DEPTH (constructor_depth);
6010 tem = size_binop (PLUS_EXPR, constructor_index,
6012 TREE_INT_CST_LOW (constructor_index)
6013 = TREE_INT_CST_LOW (tem);
6014 TREE_INT_CST_HIGH (constructor_index)
6015 = TREE_INT_CST_HIGH (tem);
6018 /* If we are doing the bookkeeping for an element that was
6019 directly output as a constructor,
6020 we must update constructor_unfilled_index. */
6022 TREE_INT_CST_LOW (constructor_unfilled_index)
6023 = TREE_INT_CST_LOW (constructor_index);
6024 TREE_INT_CST_HIGH (constructor_unfilled_index)
6025 = TREE_INT_CST_HIGH (constructor_index);
6028 while (! (constructor_range_end == 0
6029 || tree_int_cst_lt (constructor_range_end,
6030 constructor_index)));
6035 /* Handle the sole element allowed in a braced initializer
6036 for a scalar variable. */
6037 if (constructor_fields == 0)
6039 pedwarn_init ("excess elements in scalar initializer%s",
6040 " after `%s'", NULL_PTR);
6045 output_init_element (value, constructor_type, NULL_TREE, 1);
6046 constructor_fields = 0;
6050 /* If the (lexically) previous elments are not now saved,
6051 we can discard the storage for them. */
6052 if (constructor_incremental && constructor_pending_elts == 0 && value != 0)
6056 /* Expand an ASM statement with operands, handling output operands
6057 that are not variables or INDIRECT_REFS by transforming such
6058 cases into cases that expand_asm_operands can handle.
6060 Arguments are same as for expand_asm_operands. */
6063 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6064 tree string, outputs, inputs, clobbers;
6069 int noutputs = list_length (outputs);
6071 /* o[I] is the place that output number I should be written. */
6072 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6075 if (TREE_CODE (string) == ADDR_EXPR)
6076 string = TREE_OPERAND (string, 0);
6077 if (TREE_CODE (string) != STRING_CST)
6079 error ("asm template is not a string constant");
6083 /* Record the contents of OUTPUTS before it is modified. */
6084 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6085 o[i] = TREE_VALUE (tail);
6087 /* Perform default conversions on array and function inputs. */
6088 /* Don't do this for other types--
6089 it would screw up operands expected to be in memory. */
6090 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6091 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6092 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6093 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6095 /* Generate the ASM_OPERANDS insn;
6096 store into the TREE_VALUEs of OUTPUTS some trees for
6097 where the values were actually stored. */
6098 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6100 /* Copy all the intermediate outputs into the specified outputs. */
6101 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6103 if (o[i] != TREE_VALUE (tail))
6105 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6109 /* Detect modification of read-only values.
6110 (Otherwise done by build_modify_expr.) */
6113 tree type = TREE_TYPE (o[i]);
6114 if (TYPE_READONLY (type)
6115 || ((TREE_CODE (type) == RECORD_TYPE
6116 || TREE_CODE (type) == UNION_TYPE)
6117 && C_TYPE_FIELDS_READONLY (type)))
6118 readonly_warning (o[i], "modification by `asm'");
6122 /* Those MODIFY_EXPRs could do autoincrements. */
6126 /* Expand a C `return' statement.
6127 RETVAL is the expression for what to return,
6128 or a null pointer for `return;' with no value. */
6131 c_expand_return (retval)
6134 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6136 if (TREE_THIS_VOLATILE (current_function_decl))
6137 warning ("function declared `volatile' has a `return' statement");
6141 current_function_returns_null = 1;
6142 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6143 warning ("`return' with no value, in function returning non-void");
6144 expand_null_return ();
6146 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6148 current_function_returns_null = 1;
6149 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6150 pedwarn ("`return' with a value, in function returning void");
6151 expand_return (retval);
6155 tree t = convert_for_assignment (valtype, retval, "return",
6156 NULL_TREE, NULL_TREE, 0);
6157 tree res = DECL_RESULT (current_function_decl);
6158 t = build (MODIFY_EXPR, TREE_TYPE (res),
6159 res, convert (TREE_TYPE (res), t));
6160 TREE_SIDE_EFFECTS (t) = 1;
6162 current_function_returns_value = 1;
6166 /* Start a C switch statement, testing expression EXP.
6167 Return EXP if it is valid, an error node otherwise. */
6170 c_expand_start_case (exp)
6173 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6174 tree type = TREE_TYPE (exp);
6176 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6178 error ("switch quantity not an integer");
6179 exp = error_mark_node;
6184 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6186 if (warn_traditional
6187 && (type == long_integer_type_node
6188 || type == long_unsigned_type_node))
6189 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6191 exp = default_conversion (exp);
6192 type = TREE_TYPE (exp);
6193 index = get_unwidened (exp, NULL_TREE);
6194 /* We can't strip a conversion from a signed type to an unsigned,
6195 because if we did, int_fits_type_p would do the wrong thing
6196 when checking case values for being in range,
6197 and it's too hard to do the right thing. */
6198 if (TREE_UNSIGNED (TREE_TYPE (exp))
6199 == TREE_UNSIGNED (TREE_TYPE (index)))
6203 expand_start_case (1, exp, type, "switch statement");