1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 88, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This file is part of the C front end.
23 It contains routines to build C expressions given their operands,
24 including computing the types of the result, C-specific error checks,
25 and some optimization.
27 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
28 and to process initializations in declarations (since they work
29 like a strange sort of assignment). */
38 /* Nonzero if we've already printed a "missing braces around initializer"
39 message within this initializer. */
40 static int missing_braces_mentioned;
42 extern char *index ();
43 extern char *rindex ();
45 static tree quality_type PROTO((tree, tree));
46 static int comp_target_types PROTO((tree, tree));
47 static int function_types_compatible_p PROTO((tree, tree));
48 static int type_lists_compatible_p PROTO((tree, tree));
49 static int self_promoting_type_p PROTO((tree));
50 static tree decl_constant_value PROTO((tree));
51 static tree lookup_field PROTO((tree, tree, tree *));
52 static tree convert_arguments PROTO((tree, tree, tree, tree));
53 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
54 static tree pointer_diff PROTO((tree, tree));
55 static tree unary_complex_lvalue PROTO((enum tree_code, tree));
56 static void pedantic_lvalue_warning PROTO((enum tree_code));
57 static tree internal_build_compound_expr PROTO((tree, int));
58 static tree convert_for_assignment PROTO((tree, tree, char *, tree,
60 static void warn_for_assignment PROTO((char *, char *, tree, int));
61 static tree valid_compound_expr_initializer PROTO((tree, tree));
62 static void push_string PROTO((char *));
63 static void push_member_name PROTO((tree));
64 static void push_array_bounds PROTO((int));
65 static int spelling_length PROTO((void));
66 static char *print_spelling PROTO((char *));
67 static char *get_spelling PROTO((char *));
68 static void warning_init PROTO((char *, char *,
70 static tree digest_init PROTO((tree, tree, int, int));
71 static void check_init_type_bitfields PROTO((tree));
72 static void output_init_element PROTO((tree, tree, tree, int));
73 static void output_pending_init_elements PROTO((int));
75 /* Do `exp = require_complete_type (exp);' to make sure exp
76 does not have an incomplete type. (That includes void types.) */
79 require_complete_type (value)
82 tree type = TREE_TYPE (value);
84 /* First, detect a valid value with a complete type. */
85 if (TYPE_SIZE (type) != 0
86 && type != void_type_node)
89 incomplete_type_error (value, type);
90 return error_mark_node;
93 /* Print an error message for invalid use of an incomplete type.
94 VALUE is the expression that was used (or 0 if that isn't known)
95 and TYPE is the type that was invalid. */
98 incomplete_type_error (value, type)
104 /* Avoid duplicate error message. */
105 if (TREE_CODE (type) == ERROR_MARK)
108 if (value != 0 && (TREE_CODE (value) == VAR_DECL
109 || TREE_CODE (value) == PARM_DECL))
110 error ("`%s' has an incomplete type",
111 IDENTIFIER_POINTER (DECL_NAME (value)));
115 /* We must print an error message. Be clever about what it says. */
117 switch (TREE_CODE (type))
120 errmsg = "invalid use of undefined type `struct %s'";
124 errmsg = "invalid use of undefined type `union %s'";
128 errmsg = "invalid use of undefined type `enum %s'";
132 error ("invalid use of void expression");
136 if (TYPE_DOMAIN (type))
138 type = TREE_TYPE (type);
141 error ("invalid use of array with unspecified bounds");
148 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
149 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
151 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
152 error ("invalid use of incomplete typedef `%s'",
153 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
157 /* Return a variant of TYPE which has all the type qualifiers of LIKE
158 as well as those of TYPE. */
161 qualify_type (type, like)
164 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
165 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
166 return c_build_type_variant (type, constflag, volflag);
169 /* Return the common type of two types.
170 We assume that comptypes has already been done and returned 1;
171 if that isn't so, this may crash. In particular, we assume that qualifiers
174 This is the type for the result of most arithmetic operations
175 if the operands have the given two types. */
181 register enum tree_code code1;
182 register enum tree_code code2;
185 /* Save time if the two types are the same. */
187 if (t1 == t2) return t1;
189 /* If one type is nonsense, use the other. */
190 if (t1 == error_mark_node)
192 if (t2 == error_mark_node)
195 /* Merge the attributes */
197 { register tree a1, a2;
198 a1 = TYPE_ATTRIBUTES (t1);
199 a2 = TYPE_ATTRIBUTES (t2);
201 /* Either one unset? Take the set one. */
203 if (!(attributes = a1))
206 /* One that completely contains the other? Take it. */
208 else if (a2 && !attribute_list_contained (a1, a2))
209 if (attribute_list_contained (a2, a1))
213 /* Pick the longest list, and hang on the other list. */
214 /* ??? For the moment we punt on the issue of attrs with args. */
216 if (list_length (a1) < list_length (a2))
217 attributes = a2, a2 = a1;
219 for (; a2; a2 = TREE_CHAIN (a2))
220 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
221 attributes) == NULL_TREE)
224 TREE_CHAIN (a1) = attributes;
230 /* Treat an enum type as the unsigned integer type of the same width. */
232 if (TREE_CODE (t1) == ENUMERAL_TYPE)
233 t1 = type_for_size (TYPE_PRECISION (t1), 1);
234 if (TREE_CODE (t2) == ENUMERAL_TYPE)
235 t2 = type_for_size (TYPE_PRECISION (t2), 1);
237 code1 = TREE_CODE (t1);
238 code2 = TREE_CODE (t2);
240 /* If one type is complex, form the common type of the non-complex
241 components, then make that complex. Use T1 or T2 if it is the
243 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
245 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
246 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
247 tree subtype = common_type (subtype1, subtype2);
249 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
250 return build_type_attribute_variant (t1, attributes);
251 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
252 return build_type_attribute_variant (t2, attributes);
254 return build_type_attribute_variant (build_complex_type (subtype),
262 /* If only one is real, use it as the result. */
264 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
265 return build_type_attribute_variant (t1, attributes);
267 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
268 return build_type_attribute_variant (t2, attributes);
270 /* Both real or both integers; use the one with greater precision. */
272 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
273 return build_type_attribute_variant (t1, attributes);
274 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
275 return build_type_attribute_variant (t2, attributes);
277 /* Same precision. Prefer longs to ints even when same size. */
279 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
280 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
281 return build_type_attribute_variant (long_unsigned_type_node,
284 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
285 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
287 /* But preserve unsignedness from the other type,
288 since long cannot hold all the values of an unsigned int. */
289 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
290 t1 = long_unsigned_type_node;
292 t1 = long_integer_type_node;
293 return build_type_attribute_variant (t1, attributes);
296 /* Otherwise prefer the unsigned one. */
298 if (TREE_UNSIGNED (t1))
299 return build_type_attribute_variant (t1, attributes);
301 return build_type_attribute_variant (t2, attributes);
304 /* For two pointers, do this recursively on the target type,
305 and combine the qualifiers of the two types' targets. */
306 /* This code was turned off; I don't know why.
307 But ANSI C specifies doing this with the qualifiers.
308 So I turned it on again. */
310 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
311 TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
313 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
315 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
316 t1 = build_pointer_type (c_build_type_variant (target, constp,
318 return build_type_attribute_variant (t1, attributes);
321 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
322 return build_type_attribute_variant (t1, attributes);
327 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
328 /* Save space: see if the result is identical to one of the args. */
329 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
330 return build_type_attribute_variant (t1, attributes);
331 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
332 return build_type_attribute_variant (t2, attributes);
333 /* Merge the element types, and have a size if either arg has one. */
334 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
335 return build_type_attribute_variant (t1, attributes);
339 /* Function types: prefer the one that specified arg types.
340 If both do, merge the arg types. Also merge the return types. */
342 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
343 tree p1 = TYPE_ARG_TYPES (t1);
344 tree p2 = TYPE_ARG_TYPES (t2);
349 /* Save space: see if the result is identical to one of the args. */
350 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
351 return build_type_attribute_variant (t1, attributes);
352 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
353 return build_type_attribute_variant (t2, attributes);
355 /* Simple way if one arg fails to specify argument types. */
356 if (TYPE_ARG_TYPES (t1) == 0)
358 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
359 return build_type_attribute_variant (t1, attributes);
361 if (TYPE_ARG_TYPES (t2) == 0)
363 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
364 return build_type_attribute_variant (t1, attributes);
367 /* If both args specify argument types, we must merge the two
368 lists, argument by argument. */
370 len = list_length (p1);
373 for (i = 0; i < len; i++)
374 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
379 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
381 /* A null type means arg type is not specified.
382 Take whatever the other function type has. */
383 if (TREE_VALUE (p1) == 0)
385 TREE_VALUE (n) = TREE_VALUE (p2);
388 if (TREE_VALUE (p2) == 0)
390 TREE_VALUE (n) = TREE_VALUE (p1);
394 /* Given wait (union {union wait *u; int *i} *)
395 and wait (union wait *),
396 prefer union wait * as type of parm. */
397 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
398 && TREE_VALUE (p1) != TREE_VALUE (p2))
401 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
402 memb; memb = TREE_CHAIN (memb))
403 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
405 TREE_VALUE (n) = TREE_VALUE (p2);
407 pedwarn ("function types not truly compatible in ANSI C");
411 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
412 && TREE_VALUE (p2) != TREE_VALUE (p1))
415 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
416 memb; memb = TREE_CHAIN (memb))
417 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
419 TREE_VALUE (n) = TREE_VALUE (p1);
421 pedwarn ("function types not truly compatible in ANSI C");
425 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
429 t1 = build_function_type (valtype, newargs);
430 /* ... falls through ... */
434 return build_type_attribute_variant (t1, attributes);
439 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
440 or various other operations. Return 2 if they are compatible
441 but a warning may be needed if you use them together. */
444 comptypes (type1, type2)
447 register tree t1 = type1;
448 register tree t2 = type2;
451 /* Suppress errors caused by previously reported errors. */
453 if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
456 /* Treat an enum type as the integer type of the same width and
459 if (TREE_CODE (t1) == ENUMERAL_TYPE)
460 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
461 if (TREE_CODE (t2) == ENUMERAL_TYPE)
462 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
467 /* Different classes of types can't be compatible. */
469 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
471 /* Qualifiers must match. */
473 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
475 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
478 /* Allow for two different type nodes which have essentially the same
479 definition. Note that we already checked for equality of the type
480 type qualifiers (just above). */
482 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
485 #ifndef COMP_TYPE_ATTRIBUTES
486 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
489 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
490 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
493 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
496 switch (TREE_CODE (t1))
499 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
500 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
504 val = function_types_compatible_p (t1, t2);
509 tree d1 = TYPE_DOMAIN (t1);
510 tree d2 = TYPE_DOMAIN (t2);
513 /* Target types must match incl. qualifiers. */
514 if (TREE_TYPE (t1) != TREE_TYPE (t2)
515 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
518 /* Sizes must match unless one is missing or variable. */
519 if (d1 == 0 || d2 == 0 || d1 == d2
520 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
521 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
522 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
523 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
526 if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
527 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
528 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
529 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
530 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
531 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
532 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
533 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2)))))
539 if (maybe_objc_comptypes (t1, t2, 0) == 1)
543 return attrval == 2 && val == 1 ? 2 : val;
546 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
547 ignoring their qualifiers. */
550 comp_target_types (ttl, ttr)
555 /* Give maybe_objc_comptypes a crack at letting these types through. */
556 if (val = maybe_objc_comptypes (ttl, ttr, 1) >= 0)
559 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
560 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
562 if (val == 2 && pedantic)
563 pedwarn ("types are not quite compatible");
567 /* Subroutines of `comptypes'. */
569 /* Return 1 if two function types F1 and F2 are compatible.
570 If either type specifies no argument types,
571 the other must specify a fixed number of self-promoting arg types.
572 Otherwise, if one type specifies only the number of arguments,
573 the other must specify that number of self-promoting arg types.
574 Otherwise, the argument types must match. */
577 function_types_compatible_p (f1, f2)
581 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
585 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
586 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
589 args1 = TYPE_ARG_TYPES (f1);
590 args2 = TYPE_ARG_TYPES (f2);
592 /* An unspecified parmlist matches any specified parmlist
593 whose argument types don't need default promotions. */
597 if (!self_promoting_args_p (args2))
599 /* If one of these types comes from a non-prototype fn definition,
600 compare that with the other type's arglist.
601 If they don't match, ask for a warning (but no error). */
602 if (TYPE_ACTUAL_ARG_TYPES (f1)
603 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
609 if (!self_promoting_args_p (args1))
611 if (TYPE_ACTUAL_ARG_TYPES (f2)
612 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
617 /* Both types have argument lists: compare them and propagate results. */
618 val1 = type_lists_compatible_p (args1, args2);
619 return val1 != 1 ? val1 : val;
622 /* Check two lists of types for compatibility,
623 returning 0 for incompatible, 1 for compatible,
624 or 2 for compatible with warning. */
627 type_lists_compatible_p (args1, args2)
630 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
636 if (args1 == 0 && args2 == 0)
638 /* If one list is shorter than the other,
639 they fail to match. */
640 if (args1 == 0 || args2 == 0)
642 /* A null pointer instead of a type
643 means there is supposed to be an argument
644 but nothing is specified about what type it has.
645 So match anything that self-promotes. */
646 if (TREE_VALUE (args1) == 0)
648 if (! self_promoting_type_p (TREE_VALUE (args2)))
651 else if (TREE_VALUE (args2) == 0)
653 if (! self_promoting_type_p (TREE_VALUE (args1)))
656 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
658 /* Allow wait (union {union wait *u; int *i} *)
659 and wait (union wait *) to be compatible. */
660 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
661 && (TYPE_NAME (TREE_VALUE (args1)) == 0
662 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
663 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
664 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
665 TYPE_SIZE (TREE_VALUE (args2))))
668 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
669 memb; memb = TREE_CHAIN (memb))
670 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
675 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
676 && (TYPE_NAME (TREE_VALUE (args2)) == 0
677 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
678 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
679 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
680 TYPE_SIZE (TREE_VALUE (args1))))
683 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
684 memb; memb = TREE_CHAIN (memb))
685 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
694 /* comptypes said ok, but record if it said to warn. */
698 args1 = TREE_CHAIN (args1);
699 args2 = TREE_CHAIN (args2);
703 /* Return 1 if PARMS specifies a fixed number of parameters
704 and none of their types is affected by default promotions. */
707 self_promoting_args_p (parms)
711 for (t = parms; t; t = TREE_CHAIN (t))
713 register tree type = TREE_VALUE (t);
715 if (TREE_CHAIN (t) == 0 && type != void_type_node)
721 if (TYPE_MAIN_VARIANT (type) == float_type_node)
724 if (C_PROMOTING_INTEGER_TYPE_P (type))
730 /* Return 1 if TYPE is not affected by default promotions. */
733 self_promoting_type_p (type)
736 if (TYPE_MAIN_VARIANT (type) == float_type_node)
739 if (C_PROMOTING_INTEGER_TYPE_P (type))
745 /* Return an unsigned type the same as TYPE in other respects. */
751 tree type1 = TYPE_MAIN_VARIANT (type);
752 if (type1 == signed_char_type_node || type1 == char_type_node)
753 return unsigned_char_type_node;
754 if (type1 == integer_type_node)
755 return unsigned_type_node;
756 if (type1 == short_integer_type_node)
757 return short_unsigned_type_node;
758 if (type1 == long_integer_type_node)
759 return long_unsigned_type_node;
760 if (type1 == long_long_integer_type_node)
761 return long_long_unsigned_type_node;
762 if (type1 == intDI_type_node)
763 return unsigned_intDI_type_node;
764 if (type1 == intSI_type_node)
765 return unsigned_intSI_type_node;
766 if (type1 == intHI_type_node)
767 return unsigned_intHI_type_node;
768 if (type1 == intQI_type_node)
769 return unsigned_intQI_type_node;
773 /* Return a signed type the same as TYPE in other respects. */
779 tree type1 = TYPE_MAIN_VARIANT (type);
780 if (type1 == unsigned_char_type_node || type1 == char_type_node)
781 return signed_char_type_node;
782 if (type1 == unsigned_type_node)
783 return integer_type_node;
784 if (type1 == short_unsigned_type_node)
785 return short_integer_type_node;
786 if (type1 == long_unsigned_type_node)
787 return long_integer_type_node;
788 if (type1 == long_long_unsigned_type_node)
789 return long_long_integer_type_node;
790 if (type1 == unsigned_intDI_type_node)
791 return intDI_type_node;
792 if (type1 == unsigned_intSI_type_node)
793 return intSI_type_node;
794 if (type1 == unsigned_intHI_type_node)
795 return intHI_type_node;
796 if (type1 == unsigned_intQI_type_node)
797 return intQI_type_node;
801 /* Return a type the same as TYPE except unsigned or
802 signed according to UNSIGNEDP. */
805 signed_or_unsigned_type (unsignedp, type)
809 if (! INTEGRAL_TYPE_P (type))
811 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
812 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
813 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
814 return unsignedp ? unsigned_type_node : integer_type_node;
815 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
816 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
817 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
818 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
819 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
820 return (unsignedp ? long_long_unsigned_type_node
821 : long_long_integer_type_node);
825 /* Compute the value of the `sizeof' operator. */
831 enum tree_code code = TREE_CODE (type);
834 if (code == FUNCTION_TYPE)
836 if (pedantic || warn_pointer_arith)
837 pedwarn ("sizeof applied to a function type");
840 if (code == VOID_TYPE)
842 if (pedantic || warn_pointer_arith)
843 pedwarn ("sizeof applied to a void type");
846 if (code == ERROR_MARK)
848 if (TYPE_SIZE (type) == 0)
850 error ("sizeof applied to an incomplete type");
854 /* Convert in case a char is more than one unit. */
855 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
856 size_int (TYPE_PRECISION (char_type_node)));
857 /* size_binop does not put the constant in range, so do it now. */
858 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
859 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
864 c_sizeof_nowarn (type)
867 enum tree_code code = TREE_CODE (type);
870 if (code == FUNCTION_TYPE
872 || code == ERROR_MARK)
874 if (TYPE_SIZE (type) == 0)
877 /* Convert in case a char is more than one unit. */
878 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
879 size_int (TYPE_PRECISION (char_type_node)));
880 force_fit_type (t, 0);
884 /* Compute the size to increment a pointer by. */
887 c_size_in_bytes (type)
890 enum tree_code code = TREE_CODE (type);
893 if (code == FUNCTION_TYPE)
895 if (code == VOID_TYPE)
897 if (code == ERROR_MARK)
899 if (TYPE_SIZE (type) == 0)
901 error ("arithmetic on pointer to an incomplete type");
905 /* Convert in case a char is more than one unit. */
906 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
907 size_int (BITS_PER_UNIT));
908 force_fit_type (t, 0);
912 /* Implement the __alignof keyword: Return the minimum required
913 alignment of TYPE, measured in bytes. */
919 enum tree_code code = TREE_CODE (type);
921 if (code == FUNCTION_TYPE)
922 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
924 if (code == VOID_TYPE || code == ERROR_MARK)
927 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
930 /* Implement the __alignof keyword: Return the minimum required
931 alignment of EXPR, measured in bytes. For VAR_DECL's and
932 FIELD_DECL's return DECL_ALIGN (which can be set from an
933 "aligned" __attribute__ specification). */
936 c_alignof_expr (expr)
939 if (TREE_CODE (expr) == VAR_DECL)
940 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
942 if (TREE_CODE (expr) == COMPONENT_REF
943 && DECL_BIT_FIELD (TREE_OPERAND (expr, 1)))
945 error ("`__alignof' applied to a bit-field");
948 else if (TREE_CODE (expr) == COMPONENT_REF
949 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
950 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
952 if (TREE_CODE (expr) == INDIRECT_REF)
954 tree t = TREE_OPERAND (expr, 0);
956 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
958 while (TREE_CODE (t) == NOP_EXPR
959 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
963 t = TREE_OPERAND (t, 0);
964 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
965 if (thisalign > bestalign)
966 best = t, bestalign = thisalign;
968 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
971 return c_alignof (TREE_TYPE (expr));
973 /* Return either DECL or its known constant value (if it has one). */
976 decl_constant_value (decl)
979 if (! TREE_PUBLIC (decl)
980 /* Don't change a variable array bound or initial value to a constant
981 in a place where a variable is invalid. */
982 && current_function_decl != 0
984 && ! TREE_THIS_VOLATILE (decl)
985 && TREE_READONLY (decl) && ! ITERATOR_P (decl)
986 && DECL_INITIAL (decl) != 0
987 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
988 /* This is invalid if initial value is not constant.
989 If it has either a function call, a memory reference,
990 or a variable, then re-evaluating it could give different results. */
991 && TREE_CONSTANT (DECL_INITIAL (decl))
992 /* Check for cases where this is sub-optimal, even though valid. */
993 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
994 && DECL_MODE (decl) != BLKmode)
995 return DECL_INITIAL (decl);
999 /* Perform default promotions for C data used in expressions.
1000 Arrays and functions are converted to pointers;
1001 enumeral types or short or char, to int.
1002 In addition, manifest constants symbols are replaced by their values. */
1005 default_conversion (exp)
1008 register tree type = TREE_TYPE (exp);
1009 register enum tree_code code = TREE_CODE (type);
1011 /* Constants can be used directly unless they're not loadable. */
1012 if (TREE_CODE (exp) == CONST_DECL)
1013 exp = DECL_INITIAL (exp);
1015 /* Replace a nonvolatile const static variable with its value unless
1016 it is an array, in which case we must be sure that taking the
1017 address of the array produces consistent results. */
1018 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1020 exp = decl_constant_value (exp);
1021 type = TREE_TYPE (exp);
1024 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1026 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
1027 to integer and cause infinite recursion. */
1028 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1029 || (TREE_CODE (exp) == NOP_EXPR
1030 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1031 exp = TREE_OPERAND (exp, 0);
1033 /* Normally convert enums to int,
1034 but convert wide enums to something wider. */
1035 if (code == ENUMERAL_TYPE)
1037 type = type_for_size (MAX (TYPE_PRECISION (type),
1038 TYPE_PRECISION (integer_type_node)),
1040 || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node))
1041 && TREE_UNSIGNED (type)));
1042 return convert (type, exp);
1045 if (C_PROMOTING_INTEGER_TYPE_P (type))
1047 /* Traditionally, unsignedness is preserved in default promotions.
1048 Also preserve unsignedness if not really getting any wider. */
1049 if (TREE_UNSIGNED (type)
1050 && (flag_traditional
1051 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1052 return convert (unsigned_type_node, exp);
1053 return convert (integer_type_node, exp);
1055 if (flag_traditional && !flag_allow_single_precision
1056 && TYPE_MAIN_VARIANT (type) == float_type_node)
1057 return convert (double_type_node, exp);
1058 if (code == VOID_TYPE)
1060 error ("void value not ignored as it ought to be");
1061 return error_mark_node;
1063 if (code == FUNCTION_TYPE)
1065 return build_unary_op (ADDR_EXPR, exp, 0);
1067 if (code == ARRAY_TYPE)
1070 tree restype = TREE_TYPE (type);
1075 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1076 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1078 constp = TREE_READONLY (exp);
1079 volatilep = TREE_THIS_VOLATILE (exp);
1082 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1083 || constp || volatilep)
1084 restype = c_build_type_variant (restype,
1085 TYPE_READONLY (type) || constp,
1086 TYPE_VOLATILE (type) || volatilep);
1088 if (TREE_CODE (exp) == INDIRECT_REF)
1089 return convert (TYPE_POINTER_TO (restype),
1090 TREE_OPERAND (exp, 0));
1092 if (TREE_CODE (exp) == COMPOUND_EXPR)
1094 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1095 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1096 TREE_OPERAND (exp, 0), op1);
1100 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1102 error ("invalid use of non-lvalue array");
1103 return error_mark_node;
1106 ptrtype = build_pointer_type (restype);
1108 if (TREE_CODE (exp) == VAR_DECL)
1110 /* ??? This is not really quite correct
1111 in that the type of the operand of ADDR_EXPR
1112 is not the target type of the type of the ADDR_EXPR itself.
1113 Question is, can this lossage be avoided? */
1114 adr = build1 (ADDR_EXPR, ptrtype, exp);
1115 if (mark_addressable (exp) == 0)
1116 return error_mark_node;
1117 TREE_CONSTANT (adr) = staticp (exp);
1118 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1121 /* This way is better for a COMPONENT_REF since it can
1122 simplify the offset for a component. */
1123 adr = build_unary_op (ADDR_EXPR, exp, 1);
1124 return convert (ptrtype, adr);
1129 /* Look up component name in the structure type definition.
1131 If this component name is found indirectly within an anonymous union,
1132 store in *INDIRECT the component which directly contains
1133 that anonymous union. Otherwise, set *INDIRECT to 0. */
1136 lookup_field (type, component, indirect)
1137 tree type, component;
1142 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1143 to the field elements. Use a binary search on this array to quickly
1144 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1145 will always be set for structures which have many elements. */
1147 if (TYPE_LANG_SPECIFIC (type))
1150 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1152 field = TYPE_FIELDS (type);
1154 top = TYPE_LANG_SPECIFIC (type)->len;
1155 while (top - bot > 1)
1157 half = (top - bot + 1) >> 1;
1158 field = field_array[bot+half];
1160 if (DECL_NAME (field) == NULL_TREE)
1162 /* Step through all anon unions in linear fashion. */
1163 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1167 field = field_array[bot++];
1168 anon = lookup_field (TREE_TYPE (field), component, &junk);
1169 if (anon != NULL_TREE)
1176 /* Entire record is only anon unions. */
1180 /* Restart the binary search, with new lower bound. */
1184 if (DECL_NAME (field) == component)
1186 if (DECL_NAME (field) < component)
1192 if (DECL_NAME (field_array[bot]) == component)
1193 field = field_array[bot];
1194 else if (DECL_NAME (field) != component)
1199 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1201 if (DECL_NAME (field) == NULL_TREE)
1204 tree anon = lookup_field (TREE_TYPE (field), component, &junk);
1205 if (anon != NULL_TREE)
1212 if (DECL_NAME (field) == component)
1217 *indirect = NULL_TREE;
1221 /* Make an expression to refer to the COMPONENT field of
1222 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1225 build_component_ref (datum, component)
1226 tree datum, component;
1228 register tree type = TREE_TYPE (datum);
1229 register enum tree_code code = TREE_CODE (type);
1230 register tree field = NULL;
1233 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1234 unless we are not to support things not strictly ANSI. */
1235 switch (TREE_CODE (datum))
1239 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1240 return build (COMPOUND_EXPR, TREE_TYPE (value),
1241 TREE_OPERAND (datum, 0), value);
1244 return build_conditional_expr
1245 (TREE_OPERAND (datum, 0),
1246 build_component_ref (TREE_OPERAND (datum, 1), component),
1247 build_component_ref (TREE_OPERAND (datum, 2), component));
1250 /* See if there is a field or component with name COMPONENT. */
1252 if (code == RECORD_TYPE || code == UNION_TYPE)
1256 if (TYPE_SIZE (type) == 0)
1258 incomplete_type_error (NULL_TREE, type);
1259 return error_mark_node;
1262 field = lookup_field (type, component, &indirect);
1266 error (code == RECORD_TYPE
1267 ? "structure has no member named `%s'"
1268 : "union has no member named `%s'",
1269 IDENTIFIER_POINTER (component));
1270 return error_mark_node;
1272 if (TREE_TYPE (field) == error_mark_node)
1273 return error_mark_node;
1275 /* If FIELD was found buried within an anonymous union,
1276 make one COMPONENT_REF to get that anonymous union,
1277 then fall thru to make a second COMPONENT_REF to get FIELD. */
1280 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1281 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1282 TREE_READONLY (ref) = 1;
1283 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1284 TREE_THIS_VOLATILE (ref) = 1;
1288 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1290 if (TREE_READONLY (datum) || TREE_READONLY (field))
1291 TREE_READONLY (ref) = 1;
1292 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1293 TREE_THIS_VOLATILE (ref) = 1;
1297 else if (code != ERROR_MARK)
1298 error ("request for member `%s' in something not a structure or union",
1299 IDENTIFIER_POINTER (component));
1301 return error_mark_node;
1304 /* Given an expression PTR for a pointer, return an expression
1305 for the value pointed to.
1306 ERRORSTRING is the name of the operator to appear in error messages. */
1309 build_indirect_ref (ptr, errorstring)
1313 register tree pointer = default_conversion (ptr);
1314 register tree type = TREE_TYPE (pointer);
1316 if (TREE_CODE (type) == POINTER_TYPE)
1318 if (TREE_CODE (pointer) == ADDR_EXPR
1320 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1321 == TREE_TYPE (type)))
1322 return TREE_OPERAND (pointer, 0);
1325 tree t = TREE_TYPE (type);
1326 register tree ref = build1 (INDIRECT_REF,
1327 TYPE_MAIN_VARIANT (t), pointer);
1329 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1331 error ("dereferencing pointer to incomplete type");
1332 return error_mark_node;
1334 if (TREE_CODE (t) == VOID_TYPE)
1335 warning ("dereferencing `void *' pointer");
1337 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1338 so that we get the proper error message if the result is used
1339 to assign to. Also, &* is supposed to be a no-op.
1340 And ANSI C seems to specify that the type of the result
1341 should be the const type. */
1342 /* A de-reference of a pointer to const is not a const. It is valid
1343 to change it via some other pointer. */
1344 TREE_READONLY (ref) = TYPE_READONLY (t);
1345 TREE_SIDE_EFFECTS (ref)
1346 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1347 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1351 else if (TREE_CODE (pointer) != ERROR_MARK)
1352 error ("invalid type argument of `%s'", errorstring);
1353 return error_mark_node;
1356 /* This handles expressions of the form "a[i]", which denotes
1359 This is logically equivalent in C to *(a+i), but we may do it differently.
1360 If A is a variable or a member, we generate a primitive ARRAY_REF.
1361 This avoids forcing the array out of registers, and can work on
1362 arrays that are not lvalues (for example, members of structures returned
1366 build_array_ref (array, index)
1371 error ("subscript missing in array reference");
1372 return error_mark_node;
1375 if (TREE_TYPE (array) == error_mark_node
1376 || TREE_TYPE (index) == error_mark_node)
1377 return error_mark_node;
1379 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1380 && TREE_CODE (array) != INDIRECT_REF)
1384 /* Subscripting with type char is likely to lose
1385 on a machine where chars are signed.
1386 So warn on any machine, but optionally.
1387 Don't warn for unsigned char since that type is safe.
1388 Don't warn for signed char because anyone who uses that
1389 must have done so deliberately. */
1390 if (warn_char_subscripts
1391 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1392 warning ("array subscript has type `char'");
1394 /* Apply default promotions *after* noticing character types. */
1395 index = default_conversion (index);
1397 /* Require integer *after* promotion, for sake of enums. */
1398 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1400 error ("array subscript is not an integer");
1401 return error_mark_node;
1404 /* An array that is indexed by a non-constant
1405 cannot be stored in a register; we must be able to do
1406 address arithmetic on its address.
1407 Likewise an array of elements of variable size. */
1408 if (TREE_CODE (index) != INTEGER_CST
1409 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1410 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1412 if (mark_addressable (array) == 0)
1413 return error_mark_node;
1415 /* An array that is indexed by a constant value which is not within
1416 the array bounds cannot be stored in a register either; because we
1417 would get a crash in store_bit_field/extract_bit_field when trying
1418 to access a non-existent part of the register. */
1419 if (TREE_CODE (index) == INTEGER_CST
1420 && TYPE_VALUES (TREE_TYPE (array))
1421 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1423 if (mark_addressable (array) == 0)
1424 return error_mark_node;
1427 if (pedantic && !lvalue_p (array))
1429 if (DECL_REGISTER (array))
1430 pedwarn ("ANSI C forbids subscripting `register' array");
1432 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1438 while (TREE_CODE (foo) == COMPONENT_REF)
1439 foo = TREE_OPERAND (foo, 0);
1440 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1441 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1444 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1445 rval = build (ARRAY_REF, type, array, index);
1446 /* Array ref is const/volatile if the array elements are
1447 or if the array is. */
1448 TREE_READONLY (rval)
1449 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1450 | TREE_READONLY (array));
1451 TREE_SIDE_EFFECTS (rval)
1452 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1453 | TREE_SIDE_EFFECTS (array));
1454 TREE_THIS_VOLATILE (rval)
1455 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1456 /* This was added by rms on 16 Nov 91.
1457 It fixes vol struct foo *a; a->elts[1]
1458 in an inline function.
1459 Hope it doesn't break something else. */
1460 | TREE_THIS_VOLATILE (array));
1461 return require_complete_type (fold (rval));
1465 tree ar = default_conversion (array);
1466 tree ind = default_conversion (index);
1468 /* Put the integer in IND to simplify error checking. */
1469 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1476 if (ar == error_mark_node)
1479 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
1481 error ("subscripted value is neither array nor pointer");
1482 return error_mark_node;
1484 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1486 error ("array subscript is not an integer");
1487 return error_mark_node;
1490 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1495 /* Build a function call to function FUNCTION with parameters PARAMS.
1496 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1497 TREE_VALUE of each node is a parameter-expression.
1498 FUNCTION's data type may be a function type or a pointer-to-function. */
1501 build_function_call (function, params)
1502 tree function, params;
1504 register tree fntype, fundecl = 0;
1505 register tree coerced_params;
1506 tree name = NULL_TREE, assembler_name = NULL_TREE;
1508 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1509 STRIP_TYPE_NOPS (function);
1511 /* Convert anything with function type to a pointer-to-function. */
1512 if (TREE_CODE (function) == FUNCTION_DECL)
1514 name = DECL_NAME (function);
1515 assembler_name = DECL_ASSEMBLER_NAME (function);
1517 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1518 (because calling an inline function does not mean the function
1519 needs to be separately compiled). */
1520 fntype = build_type_variant (TREE_TYPE (function),
1521 TREE_READONLY (function),
1522 TREE_THIS_VOLATILE (function));
1524 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1527 function = default_conversion (function);
1529 fntype = TREE_TYPE (function);
1531 if (TREE_CODE (fntype) == ERROR_MARK)
1532 return error_mark_node;
1534 if (!(TREE_CODE (fntype) == POINTER_TYPE
1535 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1537 error ("called object is not a function");
1538 return error_mark_node;
1541 /* fntype now gets the type of function pointed to. */
1542 fntype = TREE_TYPE (fntype);
1544 /* Convert the parameters to the types declared in the
1545 function prototype, or apply default promotions. */
1548 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1550 /* Check for errors in format strings. */
1552 if (warn_format && (name || assembler_name))
1553 check_function_format (name, assembler_name, coerced_params);
1555 /* Recognize certain built-in functions so we can make tree-codes
1556 other than CALL_EXPR. We do this when it enables fold-const.c
1557 to do something useful. */
1559 if (TREE_CODE (function) == ADDR_EXPR
1560 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1561 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1562 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1567 if (coerced_params == 0)
1568 return integer_zero_node;
1569 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1573 register tree result
1574 = build (CALL_EXPR, TREE_TYPE (fntype),
1575 function, coerced_params, NULL_TREE);
1577 TREE_SIDE_EFFECTS (result) = 1;
1578 if (TREE_TYPE (result) == void_type_node)
1580 return require_complete_type (result);
1584 /* Convert the argument expressions in the list VALUES
1585 to the types in the list TYPELIST. The result is a list of converted
1586 argument expressions.
1588 If TYPELIST is exhausted, or when an element has NULL as its type,
1589 perform the default conversions.
1591 PARMLIST is the chain of parm decls for the function being called.
1592 It may be 0, if that info is not available.
1593 It is used only for generating error messages.
1595 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1597 This is also where warnings about wrong number of args are generated.
1599 Both VALUES and the returned value are chains of TREE_LIST nodes
1600 with the elements of the list in the TREE_VALUE slots of those nodes. */
1603 convert_arguments (typelist, values, name, fundecl)
1604 tree typelist, values, name, fundecl;
1606 register tree typetail, valtail;
1607 register tree result = NULL;
1610 /* Scan the given expressions and types, producing individual
1611 converted arguments and pushing them on RESULT in reverse order. */
1613 for (valtail = values, typetail = typelist, parmnum = 0;
1615 valtail = TREE_CHAIN (valtail), parmnum++)
1617 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1618 register tree val = TREE_VALUE (valtail);
1620 if (type == void_type_node)
1623 error ("too many arguments to function `%s'",
1624 IDENTIFIER_POINTER (name));
1626 error ("too many arguments to function");
1630 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1631 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1632 to convert automatically to a pointer. */
1633 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1634 val = TREE_OPERAND (val, 0);
1636 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1637 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1638 val = default_conversion (val);
1640 val = require_complete_type (val);
1644 /* Formal parm type is specified by a function prototype. */
1647 if (TYPE_SIZE (type) == 0)
1649 error ("type of formal parameter %d is incomplete", parmnum + 1);
1654 /* Optionally warn about conversions that
1655 differ from the default conversions. */
1656 if (warn_conversion)
1658 int formal_prec = TYPE_PRECISION (type);
1660 if (INTEGRAL_TYPE_P (type)
1661 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1662 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1663 else if (TREE_CODE (type) == COMPLEX_TYPE
1664 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1665 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1666 else if (TREE_CODE (type) == REAL_TYPE
1667 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1668 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1669 else if (TREE_CODE (type) == REAL_TYPE
1670 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1671 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1672 /* ??? At some point, messages should be written about
1673 conversions between complex types, but that's too messy
1675 else if (TREE_CODE (type) == REAL_TYPE
1676 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1678 /* Warn if any argument is passed as `float',
1679 since without a prototype it would be `double'. */
1680 if (formal_prec == TYPE_PRECISION (float_type_node))
1681 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1683 /* Detect integer changing in width or signedness. */
1684 else if (INTEGRAL_TYPE_P (type)
1685 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1687 tree would_have_been = default_conversion (val);
1688 tree type1 = TREE_TYPE (would_have_been);
1690 if (TREE_CODE (type) == ENUMERAL_TYPE
1691 && type == TREE_TYPE (val))
1692 /* No warning if function asks for enum
1693 and the actual arg is that enum type. */
1695 else if (formal_prec != TYPE_PRECISION (type1))
1696 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1697 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1699 /* Don't complain if the formal parameter type
1700 is an enum, because we can't tell now whether
1701 the value was an enum--even the same enum. */
1702 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1704 else if (TREE_CODE (val) == INTEGER_CST
1705 && int_fits_type_p (val, type))
1706 /* Change in signedness doesn't matter
1707 if a constant value is unaffected. */
1709 /* Likewise for a constant in a NOP_EXPR. */
1710 else if (TREE_CODE (val) == NOP_EXPR
1711 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1712 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1714 #if 0 /* We never get such tree structure here. */
1715 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1716 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1717 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1718 /* Change in signedness doesn't matter
1719 if an enum value is unaffected. */
1722 /* If the value is extended from a narrower
1723 unsigned type, it doesn't matter whether we
1724 pass it as signed or unsigned; the value
1725 certainly is the same either way. */
1726 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1727 && TREE_UNSIGNED (TREE_TYPE (val)))
1729 else if (TREE_UNSIGNED (type))
1730 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1732 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1736 parmval = convert_for_assignment (type, val,
1737 (char *)0, /* arg passing */
1738 fundecl, name, parmnum + 1);
1740 #ifdef PROMOTE_PROTOTYPES
1741 if ((TREE_CODE (type) == INTEGER_TYPE
1742 || TREE_CODE (type) == ENUMERAL_TYPE)
1743 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1744 parmval = default_conversion (parmval);
1747 result = tree_cons (NULL_TREE, parmval, result);
1749 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1750 && (TYPE_PRECISION (TREE_TYPE (val))
1751 < TYPE_PRECISION (double_type_node)))
1752 /* Convert `float' to `double'. */
1753 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1755 /* Convert `short' and `char' to full-size `int'. */
1756 result = tree_cons (NULL_TREE, default_conversion (val), result);
1759 typetail = TREE_CHAIN (typetail);
1762 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1765 error ("too few arguments to function `%s'",
1766 IDENTIFIER_POINTER (name));
1768 error ("too few arguments to function");
1771 return nreverse (result);
1774 /* This is the entry point used by the parser
1775 for binary operators in the input.
1776 In addition to constructing the expression,
1777 we check for operands that were written with other binary operators
1778 in a way that is likely to confuse the user. */
1781 parser_build_binary_op (code, arg1, arg2)
1782 enum tree_code code;
1785 tree result = build_binary_op (code, arg1, arg2, 1);
1788 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1789 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1790 enum tree_code code1 = ERROR_MARK;
1791 enum tree_code code2 = ERROR_MARK;
1793 if (class1 == 'e' || class1 == '1'
1794 || class1 == '2' || class1 == '<')
1795 code1 = C_EXP_ORIGINAL_CODE (arg1);
1796 if (class2 == 'e' || class2 == '1'
1797 || class2 == '2' || class2 == '<')
1798 code2 = C_EXP_ORIGINAL_CODE (arg2);
1800 /* Check for cases such as x+y<<z which users are likely
1801 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1802 is cleared to prevent these warnings. */
1803 if (warn_parentheses)
1805 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1807 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1808 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1809 warning ("suggest parentheses around + or - inside shift");
1812 if (code == TRUTH_ORIF_EXPR)
1814 if (code1 == TRUTH_ANDIF_EXPR
1815 || code2 == TRUTH_ANDIF_EXPR)
1816 warning ("suggest parentheses around && within ||");
1819 if (code == BIT_IOR_EXPR)
1821 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1822 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1823 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1824 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1825 warning ("suggest parentheses around arithmetic in operand of |");
1828 if (code == BIT_XOR_EXPR)
1830 if (code1 == BIT_AND_EXPR
1831 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1832 || code2 == BIT_AND_EXPR
1833 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1834 warning ("suggest parentheses around arithmetic in operand of ^");
1837 if (code == BIT_AND_EXPR)
1839 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1840 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1841 warning ("suggest parentheses around + or - in operand of &");
1845 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1846 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1847 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1848 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1850 unsigned_conversion_warning (result, arg1);
1851 unsigned_conversion_warning (result, arg2);
1852 overflow_warning (result);
1854 class = TREE_CODE_CLASS (TREE_CODE (result));
1856 /* Record the code that was specified in the source,
1857 for the sake of warnings about confusing nesting. */
1858 if (class == 'e' || class == '1'
1859 || class == '2' || class == '<')
1860 C_SET_EXP_ORIGINAL_CODE (result, code);
1863 int flag = TREE_CONSTANT (result);
1864 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1865 so that convert_for_assignment wouldn't strip it.
1866 That way, we got warnings for things like p = (1 - 1).
1867 But it turns out we should not get those warnings. */
1868 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1869 C_SET_EXP_ORIGINAL_CODE (result, code);
1870 TREE_CONSTANT (result) = flag;
1876 /* Build a binary-operation expression without default conversions.
1877 CODE is the kind of expression to build.
1878 This function differs from `build' in several ways:
1879 the data type of the result is computed and recorded in it,
1880 warnings are generated if arg data types are invalid,
1881 special handling for addition and subtraction of pointers is known,
1882 and some optimization is done (operations on narrow ints
1883 are done in the narrower type when that gives the same result).
1884 Constant folding is also done before the result is returned.
1886 Note that the operands will never have enumeral types, or function
1887 or array types, because either they will have the default conversions
1888 performed or they have both just been converted to some other type in which
1889 the arithmetic is to be done. */
1892 build_binary_op (code, orig_op0, orig_op1, convert_p)
1893 enum tree_code code;
1894 tree orig_op0, orig_op1;
1898 register enum tree_code code0, code1;
1901 /* Expression code to give to the expression when it is built.
1902 Normally this is CODE, which is what the caller asked for,
1903 but in some special cases we change it. */
1904 register enum tree_code resultcode = code;
1906 /* Data type in which the computation is to be performed.
1907 In the simplest cases this is the common type of the arguments. */
1908 register tree result_type = NULL;
1910 /* Nonzero means operands have already been type-converted
1911 in whatever way is necessary.
1912 Zero means they need to be converted to RESULT_TYPE. */
1915 /* Nonzero means create the expression with this type, rather than
1917 tree build_type = 0;
1919 /* Nonzero means after finally constructing the expression
1920 convert it to this type. */
1921 tree final_type = 0;
1923 /* Nonzero if this is an operation like MIN or MAX which can
1924 safely be computed in short if both args are promoted shorts.
1925 Also implies COMMON.
1926 -1 indicates a bitwise operation; this makes a difference
1927 in the exact conditions for when it is safe to do the operation
1928 in a narrower mode. */
1931 /* Nonzero if this is a comparison operation;
1932 if both args are promoted shorts, compare the original shorts.
1933 Also implies COMMON. */
1934 int short_compare = 0;
1936 /* Nonzero if this is a right-shift operation, which can be computed on the
1937 original short and then promoted if the operand is a promoted short. */
1938 int short_shift = 0;
1940 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1945 op0 = default_conversion (orig_op0);
1946 op1 = default_conversion (orig_op1);
1954 type0 = TREE_TYPE (op0);
1955 type1 = TREE_TYPE (op1);
1957 /* The expression codes of the data types of the arguments tell us
1958 whether the arguments are integers, floating, pointers, etc. */
1959 code0 = TREE_CODE (type0);
1960 code1 = TREE_CODE (type1);
1962 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1963 STRIP_TYPE_NOPS (op0);
1964 STRIP_TYPE_NOPS (op1);
1966 /* If an error was already reported for one of the arguments,
1967 avoid reporting another error. */
1969 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1970 return error_mark_node;
1975 /* Handle the pointer + int case. */
1976 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1977 return pointer_int_sum (PLUS_EXPR, op0, op1);
1978 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1979 return pointer_int_sum (PLUS_EXPR, op1, op0);
1985 /* Subtraction of two similar pointers.
1986 We must subtract them as integers, then divide by object size. */
1987 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1988 && comp_target_types (type0, type1))
1989 return pointer_diff (op0, op1);
1990 /* Handle pointer minus int. Just like pointer plus int. */
1991 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1992 return pointer_int_sum (MINUS_EXPR, op0, op1);
2001 case TRUNC_DIV_EXPR:
2003 case FLOOR_DIV_EXPR:
2004 case ROUND_DIV_EXPR:
2005 case EXACT_DIV_EXPR:
2006 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2007 || code0 == COMPLEX_TYPE)
2008 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2009 || code1 == COMPLEX_TYPE))
2011 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2012 resultcode = RDIV_EXPR;
2015 /* Although it would be tempting to shorten always here, that
2016 loses on some targets, since the modulo instruction is
2017 undefined if the quotient can't be represented in the
2018 computation mode. We shorten only if unsigned or if
2019 dividing by something we know != -1. */
2020 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2021 || (TREE_CODE (op1) == INTEGER_CST
2022 && (TREE_INT_CST_LOW (op1) != -1
2023 || TREE_INT_CST_HIGH (op1) != -1)));
2030 case BIT_ANDTC_EXPR:
2033 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2035 /* If one operand is a constant, and the other is a short type
2036 that has been converted to an int,
2037 really do the work in the short type and then convert the
2038 result to int. If we are lucky, the constant will be 0 or 1
2039 in the short type, making the entire operation go away. */
2040 if (TREE_CODE (op0) == INTEGER_CST
2041 && TREE_CODE (op1) == NOP_EXPR
2042 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2043 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2045 final_type = result_type;
2046 op1 = TREE_OPERAND (op1, 0);
2047 result_type = TREE_TYPE (op1);
2049 if (TREE_CODE (op1) == INTEGER_CST
2050 && TREE_CODE (op0) == NOP_EXPR
2051 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2052 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2054 final_type = result_type;
2055 op0 = TREE_OPERAND (op0, 0);
2056 result_type = TREE_TYPE (op0);
2060 case TRUNC_MOD_EXPR:
2061 case FLOOR_MOD_EXPR:
2062 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2064 /* Although it would be tempting to shorten always here, that loses
2065 on some targets, since the modulo instruction is undefined if the
2066 quotient can't be represented in the computation mode. We shorten
2067 only if unsigned or if dividing by something we know != -1. */
2068 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2069 || (TREE_CODE (op1) == INTEGER_CST
2070 && (TREE_INT_CST_LOW (op1) != -1
2071 || TREE_INT_CST_HIGH (op1) != -1)));
2076 case TRUTH_ANDIF_EXPR:
2077 case TRUTH_ORIF_EXPR:
2078 case TRUTH_AND_EXPR:
2080 case TRUTH_XOR_EXPR:
2081 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2082 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2083 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2084 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2086 /* Result of these operations is always an int,
2087 but that does not mean the operands should be
2088 converted to ints! */
2089 result_type = integer_type_node;
2090 op0 = truthvalue_conversion (op0);
2091 op1 = truthvalue_conversion (op1);
2096 /* Shift operations: result has same type as first operand;
2097 always convert second operand to int.
2098 Also set SHORT_SHIFT if shifting rightward. */
2101 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2103 if (TREE_CODE (op1) == INTEGER_CST)
2105 if (tree_int_cst_sgn (op1) < 0)
2106 warning ("right shift count is negative");
2109 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2111 if (TREE_INT_CST_HIGH (op1) != 0
2112 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2113 >= TYPE_PRECISION (type0)))
2114 warning ("right shift count >= width of type");
2117 /* Use the type of the value to be shifted.
2118 This is what most traditional C compilers do. */
2119 result_type = type0;
2120 /* Unless traditional, convert the shift-count to an integer,
2121 regardless of size of value being shifted. */
2122 if (! flag_traditional)
2124 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2125 op1 = convert (integer_type_node, op1);
2126 /* Avoid converting op1 to result_type later. */
2133 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2135 if (TREE_CODE (op1) == INTEGER_CST)
2137 if (tree_int_cst_sgn (op1) < 0)
2138 warning ("left shift count is negative");
2139 else if (TREE_INT_CST_HIGH (op1) != 0
2140 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2141 >= TYPE_PRECISION (type0)))
2142 warning ("left shift count >= width of type");
2144 /* Use the type of the value to be shifted.
2145 This is what most traditional C compilers do. */
2146 result_type = type0;
2147 /* Unless traditional, convert the shift-count to an integer,
2148 regardless of size of value being shifted. */
2149 if (! flag_traditional)
2151 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2152 op1 = convert (integer_type_node, op1);
2153 /* Avoid converting op1 to result_type later. */
2161 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2163 if (TREE_CODE (op1) == INTEGER_CST)
2165 if (tree_int_cst_sgn (op1) < 0)
2166 warning ("shift count is negative");
2167 else if (TREE_INT_CST_HIGH (op1) != 0
2168 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2169 >= TYPE_PRECISION (type0)))
2170 warning ("shift count >= width of type");
2172 /* Use the type of the value to be shifted.
2173 This is what most traditional C compilers do. */
2174 result_type = type0;
2175 /* Unless traditional, convert the shift-count to an integer,
2176 regardless of size of value being shifted. */
2177 if (! flag_traditional)
2179 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2180 op1 = convert (integer_type_node, op1);
2181 /* Avoid converting op1 to result_type later. */
2189 /* Result of comparison is always int,
2190 but don't convert the args to int! */
2191 build_type = integer_type_node;
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 register tree tt0 = TREE_TYPE (type0);
2200 register tree tt1 = TREE_TYPE (type1);
2201 /* Anything compares with void *. void * compares with anything.
2202 Otherwise, the targets must be compatible
2203 and both must be object or both incomplete. */
2204 if (comp_target_types (type0, type1))
2205 result_type = common_type (type0, type1);
2206 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2208 /* op0 != orig_op0 detects the case of something
2209 whose value is 0 but which isn't a valid null ptr const. */
2210 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2211 && TREE_CODE (tt1) == FUNCTION_TYPE)
2212 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2214 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2216 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2217 && TREE_CODE (tt0) == FUNCTION_TYPE)
2218 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2221 pedwarn ("comparison of distinct pointer types lacks a cast");
2223 if (result_type == NULL_TREE)
2224 result_type = ptr_type_node;
2226 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2227 && integer_zerop (op1))
2228 result_type = type0;
2229 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2230 && integer_zerop (op0))
2231 result_type = type1;
2232 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2234 result_type = type0;
2235 if (! flag_traditional)
2236 pedwarn ("comparison between pointer and integer");
2238 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2240 result_type = type1;
2241 if (! flag_traditional)
2242 pedwarn ("comparison between pointer and integer");
2248 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2249 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2251 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2253 if (comp_target_types (type0, type1))
2255 result_type = common_type (type0, type1);
2257 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2258 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2262 result_type = ptr_type_node;
2263 pedwarn ("comparison of distinct pointer types lacks a cast");
2272 build_type = integer_type_node;
2273 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2274 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2276 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2278 if (comp_target_types (type0, type1))
2280 result_type = common_type (type0, type1);
2281 if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2282 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2283 pedwarn ("comparison of complete and incomplete pointers");
2285 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2286 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2290 result_type = ptr_type_node;
2291 pedwarn ("comparison of distinct pointer types lacks a cast");
2294 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2295 && integer_zerop (op1))
2297 result_type = type0;
2298 if (pedantic || extra_warnings)
2299 pedwarn ("ordered comparison of pointer with integer zero");
2301 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2302 && integer_zerop (op0))
2304 result_type = type1;
2306 pedwarn ("ordered comparison of pointer with integer zero");
2308 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2310 result_type = type0;
2311 if (! flag_traditional)
2312 pedwarn ("comparison between pointer and integer");
2314 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2316 result_type = type1;
2317 if (! flag_traditional)
2318 pedwarn ("comparison between pointer and integer");
2323 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2325 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2327 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2329 if (shorten || common || short_compare)
2330 result_type = common_type (type0, type1);
2332 /* For certain operations (which identify themselves by shorten != 0)
2333 if both args were extended from the same smaller type,
2334 do the arithmetic in that type and then extend.
2336 shorten !=0 and !=1 indicates a bitwise operation.
2337 For them, this optimization is safe only if
2338 both args are zero-extended or both are sign-extended.
2339 Otherwise, we might change the result.
2340 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2341 but calculated in (unsigned short) it would be (unsigned short)-1. */
2343 if (shorten && none_complex)
2345 int unsigned0, unsigned1;
2346 tree arg0 = get_narrower (op0, &unsigned0);
2347 tree arg1 = get_narrower (op1, &unsigned1);
2348 /* UNS is 1 if the operation to be done is an unsigned one. */
2349 int uns = TREE_UNSIGNED (result_type);
2352 final_type = result_type;
2354 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2355 but it *requires* conversion to FINAL_TYPE. */
2357 if ((TYPE_PRECISION (TREE_TYPE (op0))
2358 == TYPE_PRECISION (TREE_TYPE (arg0)))
2359 && TREE_TYPE (op0) != final_type)
2360 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2361 if ((TYPE_PRECISION (TREE_TYPE (op1))
2362 == TYPE_PRECISION (TREE_TYPE (arg1)))
2363 && TREE_TYPE (op1) != final_type)
2364 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2366 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2368 /* For bitwise operations, signedness of nominal type
2369 does not matter. Consider only how operands were extended. */
2373 /* Note that in all three cases below we refrain from optimizing
2374 an unsigned operation on sign-extended args.
2375 That would not be valid. */
2377 /* Both args variable: if both extended in same way
2378 from same width, do it in that width.
2379 Do it unsigned if args were zero-extended. */
2380 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2381 < TYPE_PRECISION (result_type))
2382 && (TYPE_PRECISION (TREE_TYPE (arg1))
2383 == TYPE_PRECISION (TREE_TYPE (arg0)))
2384 && unsigned0 == unsigned1
2385 && (unsigned0 || !uns))
2387 = signed_or_unsigned_type (unsigned0,
2388 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2389 else if (TREE_CODE (arg0) == INTEGER_CST
2390 && (unsigned1 || !uns)
2391 && (TYPE_PRECISION (TREE_TYPE (arg1))
2392 < TYPE_PRECISION (result_type))
2393 && (type = signed_or_unsigned_type (unsigned1,
2395 int_fits_type_p (arg0, type)))
2397 else if (TREE_CODE (arg1) == INTEGER_CST
2398 && (unsigned0 || !uns)
2399 && (TYPE_PRECISION (TREE_TYPE (arg0))
2400 < TYPE_PRECISION (result_type))
2401 && (type = signed_or_unsigned_type (unsigned0,
2403 int_fits_type_p (arg1, type)))
2407 /* Shifts can be shortened if shifting right. */
2412 tree arg0 = get_narrower (op0, &unsigned_arg);
2414 final_type = result_type;
2416 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2417 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2419 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2420 /* We can shorten only if the shift count is less than the
2421 number of bits in the smaller type size. */
2422 && TREE_INT_CST_HIGH (op1) == 0
2423 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
2424 /* If arg is sign-extended and then unsigned-shifted,
2425 we can simulate this with a signed shift in arg's type
2426 only if the extended result is at least twice as wide
2427 as the arg. Otherwise, the shift could use up all the
2428 ones made by sign-extension and bring in zeros.
2429 We can't optimize that case at all, but in most machines
2430 it never happens because available widths are 2**N. */
2431 && (!TREE_UNSIGNED (final_type)
2433 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2435 /* Do an unsigned shift if the operand was zero-extended. */
2437 = signed_or_unsigned_type (unsigned_arg,
2439 /* Convert value-to-be-shifted to that type. */
2440 if (TREE_TYPE (op0) != result_type)
2441 op0 = convert (result_type, op0);
2446 /* Comparison operations are shortened too but differently.
2447 They identify themselves by setting short_compare = 1. */
2451 /* Don't write &op0, etc., because that would prevent op0
2452 from being kept in a register.
2453 Instead, make copies of the our local variables and
2454 pass the copies by reference, then copy them back afterward. */
2455 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2456 enum tree_code xresultcode = resultcode;
2458 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2461 op0 = xop0, op1 = xop1;
2463 resultcode = xresultcode;
2467 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2468 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2470 int unsignedp0, unsignedp1;
2471 tree primop0 = get_narrower (op0, &unsignedp0);
2472 tree primop1 = get_narrower (op1, &unsignedp1);
2474 /* Avoid spurious warnings for comparison with enumerators. */
2478 STRIP_TYPE_NOPS (xop0);
2479 STRIP_TYPE_NOPS (xop1);
2481 /* Give warnings for comparisons between signed and unsigned
2482 quantities that may fail. */
2483 /* Do the checking based on the original operand trees, so that
2484 casts will be considered, but default promotions won't be. */
2486 /* Do not warn if the comparison is being done in a signed type,
2487 since the signed type will only be chosen if it can represent
2488 all the values of the unsigned type. */
2489 if (! TREE_UNSIGNED (result_type))
2491 /* Do not warn if both operands are unsigned. */
2492 else if (op0_signed == op1_signed)
2494 /* Do not warn if the signed quantity is an unsuffixed
2495 integer literal (or some static constant expression
2496 involving such literals) and it is non-negative. */
2497 else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST
2498 && tree_int_cst_sgn (xop0) >= 0)
2499 || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
2500 && tree_int_cst_sgn (xop1) >= 0))
2502 /* Do not warn if the comparison is an equality operation,
2503 the unsigned quantity is an integral constant and it does
2504 not use the most significant bit of result_type. */
2505 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
2506 && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST
2507 && int_fits_type_p (xop1, signed_type (result_type))
2508 || (op1_signed && TREE_CODE (xop0) == INTEGER_CST
2509 && int_fits_type_p (xop0, signed_type (result_type))))))
2512 warning ("comparison between signed and unsigned");
2514 /* Warn if two unsigned values are being compared in a size
2515 larger than their original size, and one (and only one) is the
2516 result of a `~' operator. This comparison will always fail.
2518 Also warn if one operand is a constant, and the constant
2519 does not have all bits set that are set in the ~ operand
2520 when it is extended. */
2522 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2523 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2525 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2526 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2529 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2532 if (TREE_CODE (primop0) == INTEGER_CST
2533 || TREE_CODE (primop1) == INTEGER_CST)
2536 long constant, mask;
2537 int unsignedp, bits;
2539 if (TREE_CODE (primop0) == INTEGER_CST)
2542 unsignedp = unsignedp1;
2543 constant = TREE_INT_CST_LOW (primop0);
2548 unsignedp = unsignedp0;
2549 constant = TREE_INT_CST_LOW (primop1);
2552 bits = TYPE_PRECISION (TREE_TYPE (primop));
2553 if (bits < TYPE_PRECISION (result_type)
2554 && bits < HOST_BITS_PER_LONG && unsignedp)
2556 mask = (~0L) << bits;
2557 if ((mask & constant) != mask)
2558 warning ("comparison of promoted ~unsigned with constant");
2561 else if (unsignedp0 && unsignedp1
2562 && (TYPE_PRECISION (TREE_TYPE (primop0))
2563 < TYPE_PRECISION (result_type))
2564 && (TYPE_PRECISION (TREE_TYPE (primop1))
2565 < TYPE_PRECISION (result_type)))
2566 warning ("comparison of promoted ~unsigned with unsigned");
2572 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2573 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2574 Then the expression will be built.
2575 It will be given type FINAL_TYPE if that is nonzero;
2576 otherwise, it will be given type RESULT_TYPE. */
2580 binary_op_error (code);
2581 return error_mark_node;
2586 if (TREE_TYPE (op0) != result_type)
2587 op0 = convert (result_type, op0);
2588 if (TREE_TYPE (op1) != result_type)
2589 op1 = convert (result_type, op1);
2592 if (build_type == NULL_TREE)
2593 build_type = result_type;
2596 register tree result = build (resultcode, build_type, op0, op1);
2597 register tree folded;
2599 folded = fold (result);
2600 if (folded == result)
2601 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2602 if (final_type != 0)
2603 return convert (final_type, folded);
2608 /* Return a tree for the sum or difference (RESULTCODE says which)
2609 of pointer PTROP and integer INTOP. */
2612 pointer_int_sum (resultcode, ptrop, intop)
2613 enum tree_code resultcode;
2614 register tree ptrop, intop;
2618 register tree result;
2619 register tree folded;
2621 /* The result is a pointer of the same type that is being added. */
2623 register tree result_type = TREE_TYPE (ptrop);
2625 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2627 if (pedantic || warn_pointer_arith)
2628 pedwarn ("pointer of type `void *' used in arithmetic");
2629 size_exp = integer_one_node;
2631 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2633 if (pedantic || warn_pointer_arith)
2634 pedwarn ("pointer to a function used in arithmetic");
2635 size_exp = integer_one_node;
2638 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2640 /* If what we are about to multiply by the size of the elements
2641 contains a constant term, apply distributive law
2642 and multiply that constant term separately.
2643 This helps produce common subexpressions. */
2645 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2646 && ! TREE_CONSTANT (intop)
2647 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2648 && TREE_CONSTANT (size_exp)
2649 /* If the constant comes from pointer subtraction,
2650 skip this optimization--it would cause an error. */
2651 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2652 /* If the constant is unsigned, and smaller than the pointer size,
2653 then we must skip this optimization. This is because it could cause
2654 an overflow error if the constant is negative but INTOP is not. */
2655 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2656 || (TYPE_PRECISION (TREE_TYPE (intop))
2657 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2659 enum tree_code subcode = resultcode;
2660 tree int_type = TREE_TYPE (intop);
2661 if (TREE_CODE (intop) == MINUS_EXPR)
2662 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2663 /* Convert both subexpression types to the type of intop,
2664 because weird cases involving pointer arithmetic
2665 can result in a sum or difference with different type args. */
2666 ptrop = build_binary_op (subcode, ptrop,
2667 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2668 intop = convert (int_type, TREE_OPERAND (intop, 0));
2671 /* Convert the integer argument to a type the same size as a pointer
2672 so the multiply won't overflow spuriously. */
2674 if (TYPE_PRECISION (TREE_TYPE (intop)) != POINTER_SIZE)
2675 intop = convert (type_for_size (POINTER_SIZE, 0), intop);
2677 /* Replace the integer argument with a suitable product by the object size.
2678 Do this multiplication as signed, then convert to the appropriate
2679 pointer type (actually unsigned integral). */
2681 intop = convert (result_type,
2682 build_binary_op (MULT_EXPR, intop,
2683 convert (TREE_TYPE (intop), size_exp), 1));
2685 /* Create the sum or difference. */
2687 result = build (resultcode, result_type, ptrop, intop);
2689 folded = fold (result);
2690 if (folded == result)
2691 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2695 /* Return a tree for the difference of pointers OP0 and OP1.
2696 The resulting tree has type int. */
2699 pointer_diff (op0, op1)
2700 register tree op0, op1;
2702 register tree result, folded;
2703 tree restype = ptrdiff_type_node;
2705 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2707 if (pedantic || warn_pointer_arith)
2709 if (TREE_CODE (target_type) == VOID_TYPE)
2710 pedwarn ("pointer of type `void *' used in subtraction");
2711 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2712 pedwarn ("pointer to a function used in subtraction");
2715 /* First do the subtraction as integers;
2716 then drop through to build the divide operator. */
2718 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2719 convert (restype, op1), 1);
2720 /* This generates an error if op1 is pointer to incomplete type. */
2721 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2722 error ("arithmetic on pointer to an incomplete type");
2724 /* This generates an error if op0 is pointer to incomplete type. */
2725 op1 = c_size_in_bytes (target_type);
2727 /* Divide by the size, in easiest possible way. */
2729 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2731 folded = fold (result);
2732 if (folded == result)
2733 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2737 /* Construct and perhaps optimize a tree representation
2738 for a unary operation. CODE, a tree_code, specifies the operation
2739 and XARG is the operand. NOCONVERT nonzero suppresses
2740 the default promotions (such as from short to int). */
2743 build_unary_op (code, xarg, noconvert)
2744 enum tree_code code;
2748 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2749 register tree arg = xarg;
2750 register tree argtype = 0;
2751 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2752 char *errstring = NULL;
2755 if (typecode == ERROR_MARK)
2756 return error_mark_node;
2757 if (typecode == ENUMERAL_TYPE)
2758 typecode = INTEGER_TYPE;
2763 /* This is used for unary plus, because a CONVERT_EXPR
2764 is enough to prevent anybody from looking inside for
2765 associativity, but won't generate any code. */
2766 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2767 || typecode == COMPLEX_TYPE))
2768 errstring = "wrong type argument to unary plus";
2769 else if (!noconvert)
2770 arg = default_conversion (arg);
2774 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2775 || typecode == COMPLEX_TYPE))
2776 errstring = "wrong type argument to unary minus";
2777 else if (!noconvert)
2778 arg = default_conversion (arg);
2782 if (typecode == COMPLEX_TYPE)
2786 arg = default_conversion (arg);
2788 else if (typecode != INTEGER_TYPE)
2789 errstring = "wrong type argument to bit-complement";
2790 else if (!noconvert)
2791 arg = default_conversion (arg);
2795 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2796 || typecode == COMPLEX_TYPE))
2797 errstring = "wrong type argument to abs";
2798 else if (!noconvert)
2799 arg = default_conversion (arg);
2803 /* Conjugating a real value is a no-op, but allow it anyway. */
2804 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2805 || typecode == COMPLEX_TYPE))
2806 errstring = "wrong type argument to conjugation";
2807 else if (!noconvert)
2808 arg = default_conversion (arg);
2811 case TRUTH_NOT_EXPR:
2812 if (typecode != INTEGER_TYPE
2813 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2814 && typecode != COMPLEX_TYPE
2815 /* These will convert to a pointer. */
2816 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2818 errstring = "wrong type argument to unary exclamation mark";
2821 arg = truthvalue_conversion (arg);
2822 return invert_truthvalue (arg);
2828 if (TREE_CODE (arg) == COMPLEX_CST)
2829 return TREE_REALPART (arg);
2830 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2831 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2836 if (TREE_CODE (arg) == COMPLEX_CST)
2837 return TREE_IMAGPART (arg);
2838 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2839 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2841 return convert (TREE_TYPE (arg), integer_zero_node);
2843 case PREINCREMENT_EXPR:
2844 case POSTINCREMENT_EXPR:
2845 case PREDECREMENT_EXPR:
2846 case POSTDECREMENT_EXPR:
2847 /* Handle complex lvalues (when permitted)
2848 by reduction to simpler cases. */
2850 val = unary_complex_lvalue (code, arg);
2854 /* Increment or decrement the real part of the value,
2855 and don't change the imaginary part. */
2856 if (typecode == COMPLEX_TYPE)
2860 arg = stabilize_reference (arg);
2861 real = build_unary_op (REALPART_EXPR, arg, 1);
2862 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2863 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2864 build_unary_op (code, real, 1), imag);
2867 /* Report invalid types. */
2869 if (typecode != POINTER_TYPE
2870 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2872 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2873 errstring ="wrong type argument to increment";
2875 errstring ="wrong type argument to decrement";
2881 tree result_type = TREE_TYPE (arg);
2883 arg = get_unwidened (arg, 0);
2884 argtype = TREE_TYPE (arg);
2886 /* Compute the increment. */
2888 if (typecode == POINTER_TYPE)
2890 /* If pointer target is an undefined struct,
2891 we just cannot know how to do the arithmetic. */
2892 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2893 error ("%s of pointer to unknown structure",
2894 ((code == PREINCREMENT_EXPR
2895 || code == POSTINCREMENT_EXPR)
2896 ? "increment" : "decrement"));
2897 else if ((pedantic || warn_pointer_arith)
2898 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2899 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2900 pedwarn ("wrong type argument to %s",
2901 ((code == PREINCREMENT_EXPR
2902 || code == POSTINCREMENT_EXPR)
2903 ? "increment" : "decrement"));
2904 inc = c_size_in_bytes (TREE_TYPE (result_type));
2907 inc = integer_one_node;
2909 inc = convert (argtype, inc);
2911 /* Handle incrementing a cast-expression. */
2914 switch (TREE_CODE (arg))
2919 case FIX_TRUNC_EXPR:
2920 case FIX_FLOOR_EXPR:
2921 case FIX_ROUND_EXPR:
2923 pedantic_lvalue_warning (CONVERT_EXPR);
2924 /* If the real type has the same machine representation
2925 as the type it is cast to, we can make better output
2926 by adding directly to the inside of the cast. */
2927 if ((TREE_CODE (TREE_TYPE (arg))
2928 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2929 && (TYPE_MODE (TREE_TYPE (arg))
2930 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2931 arg = TREE_OPERAND (arg, 0);
2934 tree incremented, modify, value;
2935 arg = stabilize_reference (arg);
2936 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2939 value = save_expr (arg);
2940 incremented = build (((code == PREINCREMENT_EXPR
2941 || code == POSTINCREMENT_EXPR)
2942 ? PLUS_EXPR : MINUS_EXPR),
2943 argtype, value, inc);
2944 TREE_SIDE_EFFECTS (incremented) = 1;
2945 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2946 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2947 TREE_USED (value) = 1;
2957 /* Complain about anything else that is not a true lvalue. */
2958 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2959 || code == POSTINCREMENT_EXPR)
2960 ? "increment" : "decrement")))
2961 return error_mark_node;
2963 /* Report a read-only lvalue. */
2964 if (TREE_READONLY (arg))
2965 readonly_warning (arg,
2966 ((code == PREINCREMENT_EXPR
2967 || code == POSTINCREMENT_EXPR)
2968 ? "increment" : "decrement"));
2970 val = build (code, TREE_TYPE (arg), arg, inc);
2971 TREE_SIDE_EFFECTS (val) = 1;
2972 val = convert (result_type, val);
2973 if (TREE_CODE (val) != code)
2974 TREE_NO_UNUSED_WARNING (val) = 1;
2979 /* Note that this operation never does default_conversion
2980 regardless of NOCONVERT. */
2982 /* Let &* cancel out to simplify resulting code. */
2983 if (TREE_CODE (arg) == INDIRECT_REF)
2985 /* Don't let this be an lvalue. */
2986 if (lvalue_p (TREE_OPERAND (arg, 0)))
2987 return non_lvalue (TREE_OPERAND (arg, 0));
2988 return TREE_OPERAND (arg, 0);
2991 /* For &x[y], return x+y */
2992 if (TREE_CODE (arg) == ARRAY_REF)
2994 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
2995 return error_mark_node;
2996 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2997 TREE_OPERAND (arg, 1), 1);
3000 /* Handle complex lvalues (when permitted)
3001 by reduction to simpler cases. */
3002 val = unary_complex_lvalue (code, arg);
3006 #if 0 /* Turned off because inconsistent;
3007 float f; *&(int)f = 3.4 stores in int format
3008 whereas (int)f = 3.4 stores in float format. */
3009 /* Address of a cast is just a cast of the address
3010 of the operand of the cast. */
3011 switch (TREE_CODE (arg))
3016 case FIX_TRUNC_EXPR:
3017 case FIX_FLOOR_EXPR:
3018 case FIX_ROUND_EXPR:
3021 pedwarn ("ANSI C forbids the address of a cast expression");
3022 return convert (build_pointer_type (TREE_TYPE (arg)),
3023 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3028 /* Allow the address of a constructor if all the elements
3030 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3032 /* Anything not already handled and not a true memory reference
3034 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
3035 return error_mark_node;
3037 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3038 argtype = TREE_TYPE (arg);
3039 /* If the lvalue is const or volatile,
3040 merge that into the type that the address will point to. */
3041 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
3042 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3044 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3045 argtype = c_build_type_variant (argtype,
3046 TREE_READONLY (arg),
3047 TREE_THIS_VOLATILE (arg));
3050 argtype = build_pointer_type (argtype);
3052 if (mark_addressable (arg) == 0)
3053 return error_mark_node;
3058 if (TREE_CODE (arg) == COMPONENT_REF)
3060 tree field = TREE_OPERAND (arg, 1);
3062 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3064 if (DECL_BIT_FIELD (field))
3066 error ("attempt to take address of bit-field structure member `%s'",
3067 IDENTIFIER_POINTER (DECL_NAME (field)));
3068 return error_mark_node;
3071 addr = convert (argtype, addr);
3073 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3076 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3077 size_int (BITS_PER_UNIT));
3078 int flag = TREE_CONSTANT (addr);
3079 addr = fold (build (PLUS_EXPR, argtype,
3080 addr, convert (argtype, offset)));
3081 TREE_CONSTANT (addr) = flag;
3085 addr = build1 (code, argtype, arg);
3087 /* Address of a static or external variable or
3088 file-scope function counts as a constant. */
3090 && ! (TREE_CODE (arg) == FUNCTION_DECL
3091 && DECL_CONTEXT (arg) != 0))
3092 TREE_CONSTANT (addr) = 1;
3100 argtype = TREE_TYPE (arg);
3101 return fold (build1 (code, argtype, arg));
3105 return error_mark_node;
3109 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3110 convert ARG with the same conversions in the same order
3111 and return the result. */
3114 convert_sequence (conversions, arg)
3118 switch (TREE_CODE (conversions))
3123 case FIX_TRUNC_EXPR:
3124 case FIX_FLOOR_EXPR:
3125 case FIX_ROUND_EXPR:
3127 return convert (TREE_TYPE (conversions),
3128 convert_sequence (TREE_OPERAND (conversions, 0),
3137 /* Return nonzero if REF is an lvalue valid for this language.
3138 Lvalues can be assigned, unless their type has TYPE_READONLY.
3139 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3145 register enum tree_code code = TREE_CODE (ref);
3152 return lvalue_p (TREE_OPERAND (ref, 0));
3163 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3164 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
3171 /* Return nonzero if REF is an lvalue valid for this language;
3172 otherwise, print an error message and return zero. */
3175 lvalue_or_else (ref, string)
3179 int win = lvalue_p (ref);
3181 error ("invalid lvalue in %s", string);
3185 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3186 for certain kinds of expressions which are not really lvalues
3187 but which we can accept as lvalues.
3189 If ARG is not a kind of expression we can handle, return zero. */
3192 unary_complex_lvalue (code, arg)
3193 enum tree_code code;
3196 /* Handle (a, b) used as an "lvalue". */
3197 if (TREE_CODE (arg) == COMPOUND_EXPR)
3199 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3200 pedantic_lvalue_warning (COMPOUND_EXPR);
3201 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3202 TREE_OPERAND (arg, 0), real_result);
3205 /* Handle (a ? b : c) used as an "lvalue". */
3206 if (TREE_CODE (arg) == COND_EXPR)
3208 pedantic_lvalue_warning (COND_EXPR);
3209 return (build_conditional_expr
3210 (TREE_OPERAND (arg, 0),
3211 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3212 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3218 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3219 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3222 pedantic_lvalue_warning (code)
3223 enum tree_code code;
3226 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3227 code == COND_EXPR ? "conditional"
3228 : code == COMPOUND_EXPR ? "compound" : "cast");
3231 /* Warn about storing in something that is `const'. */
3234 readonly_warning (arg, string)
3239 strcpy (buf, string);
3241 /* Forbid assignments to iterators. */
3242 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3244 strcat (buf, " of iterator `%s'");
3245 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3248 if (TREE_CODE (arg) == COMPONENT_REF)
3250 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3251 readonly_warning (TREE_OPERAND (arg, 0), string);
3254 strcat (buf, " of read-only member `%s'");
3255 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3258 else if (TREE_CODE (arg) == VAR_DECL)
3260 strcat (buf, " of read-only variable `%s'");
3261 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3265 pedwarn ("%s of read-only location", buf);
3269 /* Mark EXP saying that we need to be able to take the
3270 address of it; it should not be allocated in a register.
3271 Value is 1 if successful. */
3274 mark_addressable (exp)
3277 register tree x = exp;
3279 switch (TREE_CODE (x))
3286 x = TREE_OPERAND (x, 0);
3290 TREE_ADDRESSABLE (x) = 1;
3297 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3298 && DECL_NONLOCAL (x))
3300 if (TREE_PUBLIC (x))
3302 error ("global register variable `%s' used in nested function",
3303 IDENTIFIER_POINTER (DECL_NAME (x)));
3306 pedwarn ("register variable `%s' used in nested function",
3307 IDENTIFIER_POINTER (DECL_NAME (x)));
3309 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3311 if (TREE_PUBLIC (x))
3313 error ("address of global register variable `%s' requested",
3314 IDENTIFIER_POINTER (DECL_NAME (x)));
3318 /* If we are making this addressable due to its having
3319 volatile components, give a different error message. Also
3320 handle the case of an unnamed parameter by not trying
3321 to give the name. */
3323 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3325 error ("cannot put object with volatile field into register");
3329 pedwarn ("address of register variable `%s' requested",
3330 IDENTIFIER_POINTER (DECL_NAME (x)));
3332 put_var_into_stack (x);
3336 TREE_ADDRESSABLE (x) = 1;
3337 #if 0 /* poplevel deals with this now. */
3338 if (DECL_CONTEXT (x) == 0)
3339 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3347 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3350 build_conditional_expr (ifexp, op1, op2)
3351 tree ifexp, op1, op2;
3353 register tree type1;
3354 register tree type2;
3355 register enum tree_code code1;
3356 register enum tree_code code2;
3357 register tree result_type = NULL;
3358 tree orig_op1 = op1, orig_op2 = op2;
3360 /* If second operand is omitted, it is the same as the first one;
3361 make sure it is calculated only once. */
3365 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression");
3366 ifexp = op1 = save_expr (ifexp);
3369 ifexp = truthvalue_conversion (default_conversion (ifexp));
3371 #if 0 /* Produces wrong result if within sizeof. */
3372 /* Don't promote the operands separately if they promote
3373 the same way. Return the unpromoted type and let the combined
3374 value get promoted if necessary. */
3376 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3377 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3378 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3379 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3381 if (TREE_CODE (ifexp) == INTEGER_CST)
3382 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3384 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3388 /* Promote both alternatives. */
3390 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3391 op1 = default_conversion (op1);
3392 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3393 op2 = default_conversion (op2);
3395 if (TREE_CODE (ifexp) == ERROR_MARK
3396 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3397 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3398 return error_mark_node;
3400 type1 = TREE_TYPE (op1);
3401 code1 = TREE_CODE (type1);
3402 type2 = TREE_TYPE (op2);
3403 code2 = TREE_CODE (type2);
3405 /* Quickly detect the usual case where op1 and op2 have the same type
3407 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3410 result_type = type1;
3412 result_type = TYPE_MAIN_VARIANT (type1);
3414 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3415 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3417 result_type = common_type (type1, type2);
3419 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3421 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3422 pedwarn ("ANSI C forbids conditional expr with only one void side");
3423 result_type = void_type_node;
3425 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3427 if (comp_target_types (type1, type2))
3428 result_type = common_type (type1, type2);
3429 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3430 && TREE_CODE (orig_op1) != NOP_EXPR)
3431 result_type = qualify_type (type2, type1);
3432 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3433 && TREE_CODE (orig_op2) != NOP_EXPR)
3434 result_type = qualify_type (type1, type2);
3435 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3437 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3438 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3439 result_type = qualify_type (type1, type2);
3441 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3443 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3444 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3445 result_type = qualify_type (type2, type1);
3449 pedwarn ("pointer type mismatch in conditional expression");
3450 result_type = build_pointer_type (void_type_node);
3453 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3455 if (! integer_zerop (op2))
3456 pedwarn ("pointer/integer type mismatch in conditional expression");
3459 op2 = null_pointer_node;
3460 #if 0 /* The spec seems to say this is permitted. */
3461 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3462 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3465 result_type = type1;
3467 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3469 if (!integer_zerop (op1))
3470 pedwarn ("pointer/integer type mismatch in conditional expression");
3473 op1 = null_pointer_node;
3474 #if 0 /* The spec seems to say this is permitted. */
3475 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3476 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3479 result_type = type2;
3484 if (flag_cond_mismatch)
3485 result_type = void_type_node;
3488 error ("type mismatch in conditional expression");
3489 return error_mark_node;
3493 /* Merge const and volatile flags of the incoming types. */
3495 = build_type_variant (result_type,
3496 TREE_READONLY (op1) || TREE_READONLY (op2),
3497 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3499 if (result_type != TREE_TYPE (op1))
3500 op1 = convert_and_check (result_type, op1);
3501 if (result_type != TREE_TYPE (op2))
3502 op2 = convert_and_check (result_type, op2);
3505 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3507 result_type = TREE_TYPE (op1);
3508 if (TREE_CONSTANT (ifexp))
3509 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3511 if (TYPE_MODE (result_type) == BLKmode)
3513 register tree tempvar
3514 = build_decl (VAR_DECL, NULL_TREE, result_type);
3515 register tree xop1 = build_modify_expr (tempvar, op1);
3516 register tree xop2 = build_modify_expr (tempvar, op2);
3517 register tree result = fold (build (COND_EXPR, result_type,
3518 ifexp, xop1, xop2));
3520 layout_decl (tempvar, TYPE_ALIGN (result_type));
3521 /* No way to handle variable-sized objects here.
3522 I fear that the entire handling of BLKmode conditional exprs
3523 needs to be redone. */
3524 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3527 = assign_stack_local (DECL_MODE (tempvar),
3528 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3529 + BITS_PER_UNIT - 1)
3533 TREE_SIDE_EFFECTS (result)
3534 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3535 | TREE_SIDE_EFFECTS (op2);
3536 return build (COMPOUND_EXPR, result_type, result, tempvar);
3541 if (TREE_CODE (ifexp) == INTEGER_CST)
3542 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3544 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3547 /* Given a list of expressions, return a compound expression
3548 that performs them all and returns the value of the last of them. */
3551 build_compound_expr (list)
3554 return internal_build_compound_expr (list, TRUE);
3558 internal_build_compound_expr (list, first_p)
3564 if (TREE_CHAIN (list) == 0)
3566 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3567 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3569 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3570 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3571 list = TREE_OPERAND (list, 0);
3574 /* Don't let (0, 0) be null pointer constant. */
3575 if (!first_p && integer_zerop (TREE_VALUE (list)))
3576 return non_lvalue (TREE_VALUE (list));
3577 return TREE_VALUE (list);
3580 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3582 /* Convert arrays to pointers when there really is a comma operator. */
3583 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3584 TREE_VALUE (TREE_CHAIN (list))
3585 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3588 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3590 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3592 /* The left-hand operand of a comma expression is like an expression
3593 statement: with -W or -Wunused, we should warn if it doesn't have
3594 any side-effects, unless it was explicitly cast to (void). */
3595 if ((extra_warnings || warn_unused)
3596 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3597 && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
3598 warning ("left-hand operand of comma expression has no effect");
3600 /* When pedantic, a compound expression can be neither an lvalue
3601 nor an integer constant expression. */
3606 /* With -Wunused, we should also warn if the left-hand operand does have
3607 side-effects, but computes a value which is not used. For example, in
3608 `foo() + bar(), baz()' the result of the `+' operator is not used,
3609 so we should issue a warning. */
3610 else if (warn_unused)
3611 warn_if_unused_value (TREE_VALUE (list));
3613 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3616 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3619 build_c_cast (type, expr)
3623 register tree value = expr;
3625 if (type == error_mark_node || expr == error_mark_node)
3626 return error_mark_node;
3627 type = TYPE_MAIN_VARIANT (type);
3630 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3631 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3632 value = TREE_OPERAND (value, 0);
3635 if (TREE_CODE (type) == ARRAY_TYPE)
3637 error ("cast specifies array type");
3638 return error_mark_node;
3641 if (TREE_CODE (type) == FUNCTION_TYPE)
3643 error ("cast specifies function type");
3644 return error_mark_node;
3647 if (type == TREE_TYPE (value))
3651 if (TREE_CODE (type) == RECORD_TYPE
3652 || TREE_CODE (type) == UNION_TYPE)
3653 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3656 else if (TREE_CODE (type) == UNION_TYPE)
3659 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3660 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3661 value = default_conversion (value);
3663 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3664 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3665 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3674 pedwarn ("ANSI C forbids casts to union type");
3675 if (TYPE_NAME (type) != 0)
3677 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3678 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3680 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3684 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3685 build_tree_list (field, value)),
3687 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3690 error ("cast to union type from type not present in union");
3691 return error_mark_node;
3697 /* If casting to void, avoid the error that would come
3698 from default_conversion in the case of a non-lvalue array. */
3699 if (type == void_type_node)
3700 return build1 (CONVERT_EXPR, type, value);
3702 /* Convert functions and arrays to pointers,
3703 but don't convert any other types. */
3704 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3705 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3706 value = default_conversion (value);
3707 otype = TREE_TYPE (value);
3709 /* Optionally warn about potentially worrisome casts. */
3712 && TREE_CODE (type) == POINTER_TYPE
3713 && TREE_CODE (otype) == POINTER_TYPE)
3715 if (TYPE_VOLATILE (TREE_TYPE (otype))
3716 && ! TYPE_VOLATILE (TREE_TYPE (type)))
3717 pedwarn ("cast discards `volatile' from pointer target type");
3718 if (TYPE_READONLY (TREE_TYPE (otype))
3719 && ! TYPE_READONLY (TREE_TYPE (type)))
3720 pedwarn ("cast discards `const' from pointer target type");
3723 /* Warn about possible alignment problems. */
3724 if (STRICT_ALIGNMENT && warn_cast_align
3725 && TREE_CODE (type) == POINTER_TYPE
3726 && TREE_CODE (otype) == POINTER_TYPE
3727 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3728 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3729 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3730 warning ("cast increases required alignment of target type");
3732 if (TREE_CODE (type) == INTEGER_TYPE
3733 && TREE_CODE (otype) == POINTER_TYPE
3734 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3735 && !TREE_CONSTANT (value))
3736 warning ("cast from pointer to integer of different size");
3738 if (warn_bad_function_cast
3739 && TREE_CODE (value) == CALL_EXPR
3740 && TREE_CODE (type) != TREE_CODE (otype))
3741 warning ("cast does not match function type");
3743 if (TREE_CODE (type) == POINTER_TYPE
3744 && TREE_CODE (otype) == INTEGER_TYPE
3745 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3747 /* Don't warn about converting 0 to pointer,
3748 provided the 0 was explicit--not cast or made by folding. */
3749 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3751 /* Don't warn about converting any constant. */
3752 && !TREE_CONSTANT (value))
3753 warning ("cast to pointer from integer of different size");
3756 value = convert (type, value);
3758 /* Ignore any integer overflow caused by the cast. */
3759 if (TREE_CODE (value) == INTEGER_CST)
3761 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3762 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3766 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3767 if (pedantic && TREE_CODE (value) == INTEGER_CST
3768 && TREE_CODE (expr) == INTEGER_CST
3769 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3770 value = non_lvalue (value);
3772 /* If pedantic, don't let a cast be an lvalue. */
3773 if (value == expr && pedantic)
3774 value = non_lvalue (value);
3779 /* Build an assignment expression of lvalue LHS from value RHS.
3780 MODIFYCODE is the code for a binary operator that we use
3781 to combine the old value of LHS with RHS to get the new value.
3782 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3785 build_modify_expr (lhs, modifycode, rhs)
3787 enum tree_code modifycode;
3789 register tree result;
3791 tree lhstype = TREE_TYPE (lhs);
3792 tree olhstype = lhstype;
3794 /* Types that aren't fully specified cannot be used in assignments. */
3795 lhs = require_complete_type (lhs);
3797 /* Avoid duplicate error messages from operands that had errors. */
3798 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3799 return error_mark_node;
3801 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3802 /* Do not use STRIP_NOPS here. We do not want an enumerator
3803 whose value is 0 to count as a null pointer constant. */
3804 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3805 rhs = TREE_OPERAND (rhs, 0);
3809 /* Handle control structure constructs used as "lvalues". */
3811 switch (TREE_CODE (lhs))
3813 /* Handle (a, b) used as an "lvalue". */
3815 pedantic_lvalue_warning (COMPOUND_EXPR);
3816 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3818 if (TREE_CODE (newrhs) == ERROR_MARK)
3819 return error_mark_node;
3820 return build (COMPOUND_EXPR, lhstype,
3821 TREE_OPERAND (lhs, 0), newrhs);
3823 /* Handle (a ? b : c) used as an "lvalue". */
3825 pedantic_lvalue_warning (COND_EXPR);
3826 rhs = save_expr (rhs);
3828 /* Produce (a ? (b = rhs) : (c = rhs))
3829 except that the RHS goes through a save-expr
3830 so the code to compute it is only emitted once. */
3832 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3833 build_modify_expr (TREE_OPERAND (lhs, 1),
3835 build_modify_expr (TREE_OPERAND (lhs, 2),
3837 if (TREE_CODE (cond) == ERROR_MARK)
3839 /* Make sure the code to compute the rhs comes out
3840 before the split. */
3841 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3842 /* But cast it to void to avoid an "unused" error. */
3843 convert (void_type_node, rhs), cond);
3847 /* If a binary op has been requested, combine the old LHS value with the RHS
3848 producing the value we should actually store into the LHS. */
3850 if (modifycode != NOP_EXPR)
3852 lhs = stabilize_reference (lhs);
3853 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3856 /* Handle a cast used as an "lvalue".
3857 We have already performed any binary operator using the value as cast.
3858 Now convert the result to the cast type of the lhs,
3859 and then true type of the lhs and store it there;
3860 then convert result back to the cast type to be the value
3861 of the assignment. */
3863 switch (TREE_CODE (lhs))
3868 case FIX_TRUNC_EXPR:
3869 case FIX_FLOOR_EXPR:
3870 case FIX_ROUND_EXPR:
3872 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3873 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3874 newrhs = default_conversion (newrhs);
3876 tree inner_lhs = TREE_OPERAND (lhs, 0);
3878 result = build_modify_expr (inner_lhs, NOP_EXPR,
3879 convert (TREE_TYPE (inner_lhs),
3880 convert (lhstype, newrhs)));
3881 if (TREE_CODE (result) == ERROR_MARK)
3883 pedantic_lvalue_warning (CONVERT_EXPR);
3884 return convert (TREE_TYPE (lhs), result);
3888 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3889 Reject anything strange now. */
3891 if (!lvalue_or_else (lhs, "assignment"))
3892 return error_mark_node;
3894 /* Warn about storing in something that is `const'. */
3896 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3897 || ((TREE_CODE (lhstype) == RECORD_TYPE
3898 || TREE_CODE (lhstype) == UNION_TYPE)
3899 && C_TYPE_FIELDS_READONLY (lhstype)))
3900 readonly_warning (lhs, "assignment");
3902 /* If storing into a structure or union member,
3903 it has probably been given type `int'.
3904 Compute the type that would go with
3905 the actual amount of storage the member occupies. */
3907 if (TREE_CODE (lhs) == COMPONENT_REF
3908 && (TREE_CODE (lhstype) == INTEGER_TYPE
3909 || TREE_CODE (lhstype) == REAL_TYPE
3910 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3911 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3913 /* If storing in a field that is in actuality a short or narrower than one,
3914 we must store in the field in its actual type. */
3916 if (lhstype != TREE_TYPE (lhs))
3918 lhs = copy_node (lhs);
3919 TREE_TYPE (lhs) = lhstype;
3922 /* Convert new value to destination type. */
3924 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
3925 NULL_TREE, NULL_TREE, 0);
3926 if (TREE_CODE (newrhs) == ERROR_MARK)
3927 return error_mark_node;
3929 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3930 TREE_SIDE_EFFECTS (result) = 1;
3932 /* If we got the LHS in a different type for storing in,
3933 convert the result back to the nominal type of LHS
3934 so that the value we return always has the same type
3935 as the LHS argument. */
3937 if (olhstype == TREE_TYPE (result))
3939 return convert_for_assignment (olhstype, result, "assignment",
3940 NULL_TREE, NULL_TREE, 0);
3943 /* Convert value RHS to type TYPE as preparation for an assignment
3944 to an lvalue of type TYPE.
3945 The real work of conversion is done by `convert'.
3946 The purpose of this function is to generate error messages
3947 for assignments that are not allowed in C.
3948 ERRTYPE is a string to use in error messages:
3949 "assignment", "return", etc. If it is null, this is parameter passing
3950 for a function call (and different error messages are output). Otherwise,
3951 it may be a name stored in the spelling stack and interpreted by
3954 FUNNAME is the name of the function being called,
3955 as an IDENTIFIER_NODE, or null.
3956 PARMNUM is the number of the argument, for printing in error messages. */
3959 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3962 tree fundecl, funname;
3965 register enum tree_code codel = TREE_CODE (type);
3966 register tree rhstype;
3967 register enum tree_code coder;
3969 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3970 /* Do not use STRIP_NOPS here. We do not want an enumerator
3971 whose value is 0 to count as a null pointer constant. */
3972 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3973 rhs = TREE_OPERAND (rhs, 0);
3975 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3976 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3977 rhs = default_conversion (rhs);
3978 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3979 rhs = decl_constant_value (rhs);
3981 rhstype = TREE_TYPE (rhs);
3982 coder = TREE_CODE (rhstype);
3984 if (coder == ERROR_MARK)
3985 return error_mark_node;
3987 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3989 overflow_warning (rhs);
3990 /* Check for Objective-C protocols. This will issue a warning if
3991 there are protocol violations. No need to use the return value. */
3992 maybe_objc_comptypes (type, rhstype, 0);
3996 if (coder == VOID_TYPE)
3998 error ("void value not ignored as it ought to be");
3999 return error_mark_node;
4001 /* Arithmetic types all interconvert, and enum is treated like int. */
4002 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
4003 || codel == COMPLEX_TYPE)
4004 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
4005 || coder == COMPLEX_TYPE))
4006 return convert_and_check (type, rhs);
4008 /* Conversion to a union from its member types. */
4009 else if (codel == UNION_TYPE)
4013 for (memb_types = TYPE_FIELDS (type); memb_types;
4014 memb_types = TREE_CHAIN (memb_types))
4016 if (comptypes (TREE_TYPE (memb_types), TREE_TYPE (rhs)))
4019 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
4020 pedwarn ("ANSI C prohibits argument conversion to union type");
4021 return build1 (NOP_EXPR, type, rhs);
4024 else if (coder == POINTER_TYPE
4025 && TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE)
4027 tree memb_type = TREE_TYPE (memb_types);
4028 register tree ttl = TREE_TYPE (memb_type);
4029 register tree ttr = TREE_TYPE (rhstype);
4031 /* Any non-function converts to a [const][volatile] void *
4032 and vice versa; otherwise, targets must be the same.
4033 Meanwhile, the lhs target must have all the qualifiers of
4035 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4036 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4037 || comp_target_types (memb_type, rhstype))
4039 /* Const and volatile mean something different for function
4040 types, so the usual warnings are not appropriate. */
4041 if (TREE_CODE (ttr) != FUNCTION_TYPE
4042 || TREE_CODE (ttl) != FUNCTION_TYPE)
4044 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4045 warn_for_assignment ("%s discards `const' from pointer target type",
4046 get_spelling (errtype), funname,
4048 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4049 warn_for_assignment ("%s discards `volatile' from pointer target type",
4050 get_spelling (errtype), funname,
4055 /* Because const and volatile on functions are
4056 restrictions that say the function will not do
4057 certain things, it is okay to use a const or volatile
4058 function where an ordinary one is wanted, but not
4060 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4061 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4062 get_spelling (errtype), funname,
4064 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4065 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4066 get_spelling (errtype), funname,
4071 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
4072 pedwarn ("ANSI C prohibits argument conversion to union type");
4073 return build1 (NOP_EXPR, type, rhs);
4077 /* Can convert integer zero to any pointer type. */
4078 else if (TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE
4079 && (integer_zerop (rhs)
4080 || (TREE_CODE (rhs) == NOP_EXPR
4081 && integer_zerop (TREE_OPERAND (rhs, 0)))))
4082 return build1 (NOP_EXPR, type, null_pointer_node);
4086 /* Conversions among pointers */
4087 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
4089 register tree ttl = TREE_TYPE (type);
4090 register tree ttr = TREE_TYPE (rhstype);
4092 /* Any non-function converts to a [const][volatile] void *
4093 and vice versa; otherwise, targets must be the same.
4094 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4095 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4096 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4097 || comp_target_types (type, rhstype)
4098 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4099 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4102 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4103 && TREE_CODE (ttr) == FUNCTION_TYPE)
4105 (TYPE_MAIN_VARIANT (ttr) == void_type_node
4106 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4107 which are not ANSI null ptr constants. */
4108 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4109 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4110 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4111 get_spelling (errtype), funname, parmnum);
4112 /* Const and volatile mean something different for function types,
4113 so the usual warnings are not appropriate. */
4114 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4115 && TREE_CODE (ttl) != FUNCTION_TYPE)
4117 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4118 warn_for_assignment ("%s discards `const' from pointer target type",
4119 get_spelling (errtype), funname, parmnum);
4120 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4121 warn_for_assignment ("%s discards `volatile' from pointer target type",
4122 get_spelling (errtype), funname, parmnum);
4123 /* If this is not a case of ignoring a mismatch in signedness,
4125 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4126 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4127 || comp_target_types (type, rhstype))
4129 /* If there is a mismatch, do warn. */
4131 warn_for_assignment ("pointer targets in %s differ in signedness",
4132 get_spelling (errtype), funname, parmnum);
4134 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4135 && TREE_CODE (ttr) == FUNCTION_TYPE)
4137 /* Because const and volatile on functions are restrictions
4138 that say the function will not do certain things,
4139 it is okay to use a const or volatile function
4140 where an ordinary one is wanted, but not vice-versa. */
4141 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4142 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4143 get_spelling (errtype), funname, parmnum);
4144 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4145 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4146 get_spelling (errtype), funname, parmnum);
4150 warn_for_assignment ("%s from incompatible pointer type",
4151 get_spelling (errtype), funname, parmnum);
4152 return convert (type, rhs);
4154 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4156 /* An explicit constant 0 can convert to a pointer,
4157 or one that results from arithmetic, even including
4158 a cast to integer type. */
4159 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4161 ! (TREE_CODE (rhs) == NOP_EXPR
4162 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4163 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4164 && integer_zerop (TREE_OPERAND (rhs, 0))))
4166 warn_for_assignment ("%s makes pointer from integer without a cast",
4167 get_spelling (errtype), funname, parmnum);
4168 return convert (type, rhs);
4170 return null_pointer_node;
4172 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4174 warn_for_assignment ("%s makes integer from pointer without a cast",
4175 get_spelling (errtype), funname, parmnum);
4176 return convert (type, rhs);
4183 tree selector = maybe_building_objc_message_expr ();
4185 if (selector && parmnum > 2)
4186 error ("incompatible type for argument %d of `%s'",
4187 parmnum - 2, IDENTIFIER_POINTER (selector));
4189 error ("incompatible type for argument %d of `%s'",
4190 parmnum, IDENTIFIER_POINTER (funname));
4193 error ("incompatible type for argument %d of indirect function call",
4197 error ("incompatible types in %s", get_spelling (errtype));
4199 return error_mark_node;
4202 /* Print a warning using MSG.
4203 It gets OPNAME as its one parameter.
4204 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4205 FUNCTION and ARGNUM are handled specially if we are building an
4206 Objective-C selector. */
4209 warn_for_assignment (msg, opname, function, argnum)
4215 static char argstring[] = "passing arg %d of `%s'";
4216 static char argnofun[] = "passing arg %d";
4220 tree selector = maybe_building_objc_message_expr ();
4222 if (selector && argnum > 2)
4224 function = selector;
4229 /* Function name is known; supply it. */
4230 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4231 + sizeof (argstring) + 25 /*%d*/ + 1);
4232 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4236 /* Function name unknown (call through ptr); just give arg number. */
4237 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4238 sprintf (opname, argnofun, argnum);
4241 pedwarn (msg, opname);
4244 /* Return nonzero if VALUE is a valid constant-valued expression
4245 for use in initializing a static variable; one that can be an
4246 element of a "constant" initializer.
4248 Return null_pointer_node if the value is absolute;
4249 if it is relocatable, return the variable that determines the relocation.
4250 We assume that VALUE has been folded as much as possible;
4251 therefore, we do not need to check for such things as
4252 arithmetic-combinations of integers. */
4255 initializer_constant_valid_p (value, endtype)
4259 switch (TREE_CODE (value))
4262 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4263 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
4264 && TREE_CONSTANT (value))
4266 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
4269 return TREE_STATIC (value) ? null_pointer_node : 0;
4275 return null_pointer_node;
4278 return TREE_OPERAND (value, 0);
4280 case NON_LVALUE_EXPR:
4281 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4285 /* Allow conversions between pointer types. */
4286 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4287 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
4288 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4290 /* Allow conversions between real types. */
4291 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4292 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
4293 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4295 /* Allow length-preserving conversions between integer types. */
4296 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4297 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4298 && (TYPE_PRECISION (TREE_TYPE (value))
4299 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4300 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4302 /* Allow conversions between other integer types only if
4304 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4305 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4307 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4309 if (inner == null_pointer_node)
4310 return null_pointer_node;
4314 /* Allow (int) &foo provided int is as wide as a pointer. */
4315 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4316 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
4317 && (TYPE_PRECISION (TREE_TYPE (value))
4318 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4319 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4322 /* Likewise conversions from int to pointers. */
4323 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4324 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4325 && (TYPE_PRECISION (TREE_TYPE (value))
4326 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4327 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4330 /* Allow conversions to union types if the value inside is okay. */
4331 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4332 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4337 if (TREE_CODE (endtype) == INTEGER_TYPE
4338 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4341 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4343 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4345 /* If either term is absolute, use the other terms relocation. */
4346 if (valid0 == null_pointer_node)
4348 if (valid1 == null_pointer_node)
4354 if (TREE_CODE (endtype) == INTEGER_TYPE
4355 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4358 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4360 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4362 /* Win if second argument is absolute. */
4363 if (valid1 == null_pointer_node)
4365 /* Win if both arguments have the same relocation.
4366 Then the value is absolute. */
4367 if (valid0 == valid1)
4368 return null_pointer_node;
4376 /* If VALUE is a compound expr all of whose expressions are constant, then
4377 return its value. Otherwise, return error_mark_node.
4379 This is for handling COMPOUND_EXPRs as initializer elements
4380 which is allowed with a warning when -pedantic is specified. */
4383 valid_compound_expr_initializer (value, endtype)
4387 if (TREE_CODE (value) == COMPOUND_EXPR)
4389 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4391 return error_mark_node;
4392 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4395 else if (! TREE_CONSTANT (value)
4396 && ! initializer_constant_valid_p (value, endtype))
4397 return error_mark_node;
4402 /* Perform appropriate conversions on the initial value of a variable,
4403 store it in the declaration DECL,
4404 and print any error messages that are appropriate.
4405 If the init is invalid, store an ERROR_MARK. */
4408 store_init_value (decl, init)
4411 register tree value, type;
4413 /* If variable's type was invalidly declared, just ignore it. */
4415 type = TREE_TYPE (decl);
4416 if (TREE_CODE (type) == ERROR_MARK)
4419 /* Digest the specified initializer into an expression. */
4421 value = digest_init (type, init, TREE_STATIC (decl),
4422 TREE_STATIC (decl) || pedantic);
4424 /* Store the expression if valid; else report error. */
4427 /* Note that this is the only place we can detect the error
4428 in a case such as struct foo bar = (struct foo) { x, y };
4429 where there is one initial value which is a constructor expression. */
4430 if (value == error_mark_node)
4432 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4434 error ("initializer for static variable is not constant");
4435 value = error_mark_node;
4437 else if (TREE_STATIC (decl)
4438 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4440 error ("initializer for static variable uses complicated arithmetic");
4441 value = error_mark_node;
4445 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4447 if (! TREE_CONSTANT (value))
4448 pedwarn ("aggregate initializer is not constant");
4449 else if (! TREE_STATIC (value))
4450 pedwarn ("aggregate initializer uses complicated arithmetic");
4455 DECL_INITIAL (decl) = value;
4457 /* ANSI wants warnings about out-of-range constant initializers. */
4458 STRIP_TYPE_NOPS (value);
4459 constant_expression_warning (value);
4462 /* Methods for storing and printing names for error messages. */
4464 /* Implement a spelling stack that allows components of a name to be pushed
4465 and popped. Each element on the stack is this structure. */
4477 #define SPELLING_STRING 1
4478 #define SPELLING_MEMBER 2
4479 #define SPELLING_BOUNDS 3
4481 static struct spelling *spelling; /* Next stack element (unused). */
4482 static struct spelling *spelling_base; /* Spelling stack base. */
4483 static int spelling_size; /* Size of the spelling stack. */
4485 /* Macros to save and restore the spelling stack around push_... functions.
4486 Alternative to SAVE_SPELLING_STACK. */
4488 #define SPELLING_DEPTH() (spelling - spelling_base)
4489 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4491 /* Save and restore the spelling stack around arbitrary C code. */
4493 #define SAVE_SPELLING_DEPTH(code) \
4495 int __depth = SPELLING_DEPTH (); \
4497 RESTORE_SPELLING_DEPTH (__depth); \
4500 /* Push an element on the spelling stack with type KIND and assign VALUE
4503 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4505 int depth = SPELLING_DEPTH (); \
4507 if (depth >= spelling_size) \
4509 spelling_size += 10; \
4510 if (spelling_base == 0) \
4512 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4515 = (struct spelling *) xrealloc (spelling_base, \
4516 spelling_size * sizeof (struct spelling)); \
4517 RESTORE_SPELLING_DEPTH (depth); \
4520 spelling->kind = (KIND); \
4521 spelling->MEMBER = (VALUE); \
4525 /* Push STRING on the stack. Printed literally. */
4528 push_string (string)
4531 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4534 /* Push a member name on the stack. Printed as '.' STRING. */
4537 push_member_name (decl)
4542 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4543 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4546 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4549 push_array_bounds (bounds)
4552 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4555 /* Compute the maximum size in bytes of the printed spelling. */
4560 register int size = 0;
4561 register struct spelling *p;
4563 for (p = spelling_base; p < spelling; p++)
4565 if (p->kind == SPELLING_BOUNDS)
4568 size += strlen (p->u.s) + 1;
4574 /* Print the spelling to BUFFER and return it. */
4577 print_spelling (buffer)
4578 register char *buffer;
4580 register char *d = buffer;
4582 register struct spelling *p;
4584 for (p = spelling_base; p < spelling; p++)
4585 if (p->kind == SPELLING_BOUNDS)
4587 sprintf (d, "[%d]", p->u.i);
4592 if (p->kind == SPELLING_MEMBER)
4594 for (s = p->u.s; *d = *s++; d++)
4601 /* Provide a means to pass component names derived from the spelling stack. */
4603 char initialization_message;
4605 /* Interpret the spelling of the given ERRTYPE message. */
4608 get_spelling (errtype)
4611 static char *buffer;
4612 static int size = -1;
4614 if (errtype == &initialization_message)
4616 /* Avoid counting chars */
4617 static char message[] = "initialization of `%s'";
4618 register int needed = sizeof (message) + spelling_length () + 1;
4622 buffer = (char *) xmalloc (size = needed);
4624 buffer = (char *) xrealloc (buffer, size = needed);
4626 temp = (char *) alloca (needed);
4627 sprintf (buffer, message, print_spelling (temp));
4634 /* Issue an error message for a bad initializer component.
4635 FORMAT describes the message. OFWHAT is the name for the component.
4636 LOCAL is a format string for formatting the insertion of the name
4639 If OFWHAT is null, the component name is stored on the spelling stack.
4640 If the component name is a null string, then LOCAL is omitted entirely. */
4643 error_init (format, local, ofwhat)
4644 char *format, *local, *ofwhat;
4649 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4650 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4653 sprintf (buffer, local, ofwhat);
4657 error (format, buffer);
4660 /* Issue a pedantic warning for a bad initializer component.
4661 FORMAT describes the message. OFWHAT is the name for the component.
4662 LOCAL is a format string for formatting the insertion of the name
4665 If OFWHAT is null, the component name is stored on the spelling stack.
4666 If the component name is a null string, then LOCAL is omitted entirely. */
4669 pedwarn_init (format, local, ofwhat)
4670 char *format, *local, *ofwhat;
4675 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4676 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4679 sprintf (buffer, local, ofwhat);
4683 pedwarn (format, buffer);
4686 /* Issue a warning for a bad initializer component.
4687 FORMAT describes the message. OFWHAT is the name for the component.
4688 LOCAL is a format string for formatting the insertion of the name
4691 If OFWHAT is null, the component name is stored on the spelling stack.
4692 If the component name is a null string, then LOCAL is omitted entirely. */
4695 warning_init (format, local, ofwhat)
4696 char *format, *local, *ofwhat;
4701 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4702 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4705 sprintf (buffer, local, ofwhat);
4709 warning (format, buffer);
4712 /* Digest the parser output INIT as an initializer for type TYPE.
4713 Return a C expression of type TYPE to represent the initial value.
4715 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4716 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4717 applies only to elements of constructors. */
4720 digest_init (type, init, require_constant, constructor_constant)
4722 int require_constant, constructor_constant;
4724 enum tree_code code = TREE_CODE (type);
4725 tree inside_init = init;
4727 if (init == error_mark_node)
4730 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4731 /* Do not use STRIP_NOPS here. We do not want an enumerator
4732 whose value is 0 to count as a null pointer constant. */
4733 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4734 inside_init = TREE_OPERAND (init, 0);
4736 /* Initialization of an array of chars from a string constant
4737 optionally enclosed in braces. */
4739 if (code == ARRAY_TYPE)
4741 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4742 if ((typ1 == char_type_node
4743 || typ1 == signed_char_type_node
4744 || typ1 == unsigned_char_type_node
4745 || typ1 == unsigned_wchar_type_node
4746 || typ1 == signed_wchar_type_node)
4747 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4749 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4750 TYPE_MAIN_VARIANT (type)))
4753 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4755 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4757 error_init ("char-array%s initialized from wide string",
4759 return error_mark_node;
4761 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4763 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4765 error_init ("int-array%s initialized from non-wide string",
4767 return error_mark_node;
4770 TREE_TYPE (inside_init) = type;
4771 if (TYPE_DOMAIN (type) != 0
4772 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4774 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4775 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4776 /* Subtract 1 (or sizeof (wchar_t))
4777 because it's ok to ignore the terminating null char
4778 that is counted in the length of the constant. */
4779 if (size < TREE_STRING_LENGTH (inside_init)
4780 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4781 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4784 "initializer-string for array of chars%s is too long",
4791 /* Any type can be initialized
4792 from an expression of the same type, optionally with braces. */
4794 if (inside_init && TREE_TYPE (inside_init) != 0
4795 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4796 TYPE_MAIN_VARIANT (type))
4797 || (code == ARRAY_TYPE
4798 && comptypes (TREE_TYPE (inside_init), type))
4799 || (code == POINTER_TYPE
4800 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4801 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4802 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4803 TREE_TYPE (type)))))
4805 if (code == POINTER_TYPE
4806 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4807 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4808 inside_init = default_conversion (inside_init);
4809 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4810 && TREE_CODE (inside_init) != CONSTRUCTOR)
4812 error_init ("array%s initialized from non-constant array expression",
4814 return error_mark_node;
4817 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4818 inside_init = decl_constant_value (inside_init);
4820 /* Compound expressions can only occur here if -pedantic or
4821 -pedantic-errors is specified. In the later case, we always want
4822 an error. In the former case, we simply want a warning. */
4823 if (require_constant && pedantic
4824 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4827 = valid_compound_expr_initializer (inside_init,
4828 TREE_TYPE (inside_init));
4829 if (inside_init == error_mark_node)
4830 error_init ("initializer element%s is not constant",
4833 pedwarn_init ("initializer element%s is not constant",
4835 if (flag_pedantic_errors)
4836 inside_init = error_mark_node;
4838 else if (require_constant && ! TREE_CONSTANT (inside_init))
4840 error_init ("initializer element%s is not constant",
4842 inside_init = error_mark_node;
4844 else if (require_constant
4845 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4847 error_init ("initializer element%s is not computable at load time",
4849 inside_init = error_mark_node;
4855 /* Handle scalar types, including conversions. */
4857 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4858 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4860 /* Note that convert_for_assignment calls default_conversion
4861 for arrays and functions. We must not call it in the
4862 case where inside_init is a null pointer constant. */
4864 = convert_for_assignment (type, init, "initialization",
4865 NULL_TREE, NULL_TREE, 0);
4867 if (require_constant && ! TREE_CONSTANT (inside_init))
4869 error_init ("initializer element%s is not constant",
4871 inside_init = error_mark_node;
4873 else if (require_constant
4874 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4876 error_init ("initializer element%s is not computable at load time",
4878 inside_init = error_mark_node;
4884 /* Come here only for records and arrays. */
4886 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4888 error_init ("variable-sized object%s may not be initialized",
4890 return error_mark_node;
4893 /* Traditionally, you can write struct foo x = 0;
4894 and it initializes the first element of x to 0. */
4895 if (flag_traditional)
4897 tree top = 0, prev = 0;
4898 while (TREE_CODE (type) == RECORD_TYPE
4899 || TREE_CODE (type) == ARRAY_TYPE
4900 || TREE_CODE (type) == QUAL_UNION_TYPE
4901 || TREE_CODE (type) == UNION_TYPE)
4903 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4907 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4909 if (TREE_CODE (type) == ARRAY_TYPE)
4910 type = TREE_TYPE (type);
4911 else if (TYPE_FIELDS (type))
4912 type = TREE_TYPE (TYPE_FIELDS (type));
4915 error_init ("invalid initializer%s", " for `%s'", NULL);
4916 return error_mark_node;
4919 TREE_OPERAND (prev, 1)
4920 = build_tree_list (NULL_TREE,
4921 digest_init (type, init, require_constant,
4922 constructor_constant));
4925 error_init ("invalid initializer%s", " for `%s'", NULL);
4926 return error_mark_node;
4929 /* Handle initializers that use braces. */
4931 /* Type of object we are accumulating a constructor for.
4932 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4933 static tree constructor_type;
4935 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4937 static tree constructor_fields;
4939 /* For an ARRAY_TYPE, this is the specified index
4940 at which to store the next element we get.
4941 This is a special INTEGER_CST node that we modify in place. */
4942 static tree constructor_index;
4944 /* For an ARRAY_TYPE, this is the end index of the range
4945 to initialize with the next element, or NULL in the ordinary case
4946 where the element is used just once. */
4947 static tree constructor_range_end;
4949 /* For an ARRAY_TYPE, this is the maximum index. */
4950 static tree constructor_max_index;
4952 /* For a RECORD_TYPE, this is the first field not yet written out. */
4953 static tree constructor_unfilled_fields;
4955 /* For an ARRAY_TYPE, this is the index of the first element
4956 not yet written out.
4957 This is a special INTEGER_CST node that we modify in place. */
4958 static tree constructor_unfilled_index;
4960 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4961 This is so we can generate gaps between fields, when appropriate.
4962 This is a special INTEGER_CST node that we modify in place. */
4963 static tree constructor_bit_index;
4965 /* If we are saving up the elements rather than allocating them,
4966 this is the list of elements so far (in reverse order,
4967 most recent first). */
4968 static tree constructor_elements;
4970 /* 1 if so far this constructor's elements are all compile-time constants. */
4971 static int constructor_constant;
4973 /* 1 if so far this constructor's elements are all valid address constants. */
4974 static int constructor_simple;
4976 /* 1 if this constructor is erroneous so far. */
4977 static int constructor_erroneous;
4979 /* 1 if have called defer_addressed_constants. */
4980 static int constructor_subconstants_deferred;
4982 /* List of pending elements at this constructor level.
4983 These are elements encountered out of order
4984 which belong at places we haven't reached yet in actually
4985 writing the output. */
4986 static tree constructor_pending_elts;
4988 /* The SPELLING_DEPTH of this constructor. */
4989 static int constructor_depth;
4991 /* 0 if implicitly pushing constructor levels is allowed. */
4992 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4994 /* 1 if this constructor level was entered implicitly. */
4995 static int constructor_implicit;
4997 static int require_constant_value;
4998 static int require_constant_elements;
5000 /* 1 if it is ok to output this constructor as we read it.
5001 0 means must accumulate a CONSTRUCTOR expression. */
5002 static int constructor_incremental;
5004 /* DECL node for which an initializer is being read.
5005 0 means we are reading a constructor expression
5006 such as (struct foo) {...}. */
5007 static tree constructor_decl;
5009 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
5010 static char *constructor_asmspec;
5012 /* Nonzero if this is an initializer for a top-level decl. */
5013 static int constructor_top_level;
5015 /* When we finish reading a constructor expression
5016 (constructor_decl is 0), the CONSTRUCTOR goes here. */
5017 static tree constructor_result;
5019 /* This stack has a level for each implicit or explicit level of
5020 structuring in the initializer, including the outermost one. It
5021 saves the values of most of the variables above. */
5023 struct constructor_stack
5025 struct constructor_stack *next;
5031 tree unfilled_index;
5032 tree unfilled_fields;
5038 /* If nonzero, this value should replace the entire
5039 constructor at this level. */
5040 tree replacement_value;
5049 struct constructor_stack *constructor_stack;
5051 /* This stack records separate initializers that are nested.
5052 Nested initializers can't happen in ANSI C, but GNU C allows them
5053 in cases like { ... (struct foo) { ... } ... }. */
5055 struct initializer_stack
5057 struct initializer_stack *next;
5060 struct constructor_stack *constructor_stack;
5062 struct spelling *spelling;
5063 struct spelling *spelling_base;
5067 char require_constant_value;
5068 char require_constant_elements;
5072 struct initializer_stack *initializer_stack;
5074 /* Prepare to parse and output the initializer for variable DECL. */
5077 start_init (decl, asmspec_tree, top_level)
5083 struct initializer_stack *p
5084 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5088 asmspec = TREE_STRING_POINTER (asmspec_tree);
5090 p->decl = constructor_decl;
5091 p->asmspec = constructor_asmspec;
5092 p->incremental = constructor_incremental;
5093 p->require_constant_value = require_constant_value;
5094 p->require_constant_elements = require_constant_elements;
5095 p->constructor_stack = constructor_stack;
5096 p->elements = constructor_elements;
5097 p->spelling = spelling;
5098 p->spelling_base = spelling_base;
5099 p->spelling_size = spelling_size;
5100 p->deferred = constructor_subconstants_deferred;
5101 p->top_level = constructor_top_level;
5102 p->next = initializer_stack;
5103 initializer_stack = p;
5105 constructor_decl = decl;
5106 constructor_incremental = top_level;
5107 constructor_asmspec = asmspec;
5108 constructor_subconstants_deferred = 0;
5109 constructor_top_level = top_level;
5113 require_constant_value = TREE_STATIC (decl);
5114 require_constant_elements
5115 = ((TREE_STATIC (decl) || pedantic)
5116 /* For a scalar, you can always use any value to initialize,
5117 even within braces. */
5118 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5119 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5120 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5121 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5122 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5123 constructor_incremental |= TREE_STATIC (decl);
5127 require_constant_value = 0;
5128 require_constant_elements = 0;
5129 locus = "(anonymous)";
5132 constructor_stack = 0;
5134 missing_braces_mentioned = 0;
5138 RESTORE_SPELLING_DEPTH (0);
5141 push_string (locus);
5147 struct initializer_stack *p = initializer_stack;
5149 /* Output subconstants (string constants, usually)
5150 that were referenced within this initializer and saved up.
5151 Must do this if and only if we called defer_addressed_constants. */
5152 if (constructor_subconstants_deferred)
5153 output_deferred_addressed_constants ();
5155 /* Free the whole constructor stack of this initializer. */
5156 while (constructor_stack)
5158 struct constructor_stack *q = constructor_stack;
5159 constructor_stack = q->next;
5163 /* Pop back to the data of the outer initializer (if any). */
5164 constructor_decl = p->decl;
5165 constructor_asmspec = p->asmspec;
5166 constructor_incremental = p->incremental;
5167 require_constant_value = p->require_constant_value;
5168 require_constant_elements = p->require_constant_elements;
5169 constructor_stack = p->constructor_stack;
5170 constructor_elements = p->elements;
5171 spelling = p->spelling;
5172 spelling_base = p->spelling_base;
5173 spelling_size = p->spelling_size;
5174 constructor_subconstants_deferred = p->deferred;
5175 constructor_top_level = p->top_level;
5176 initializer_stack = p->next;
5180 /* Call here when we see the initializer is surrounded by braces.
5181 This is instead of a call to push_init_level;
5182 it is matched by a call to pop_init_level.
5184 TYPE is the type to initialize, for a constructor expression.
5185 For an initializer for a decl, TYPE is zero. */
5188 really_start_incremental_init (type)
5191 struct constructor_stack *p
5192 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5195 type = TREE_TYPE (constructor_decl);
5197 /* Turn off constructor_incremental if type is a struct with bitfields.
5198 Do this before the first push, so that the corrected value
5199 is available in finish_init. */
5200 check_init_type_bitfields (type);
5202 p->type = constructor_type;
5203 p->fields = constructor_fields;
5204 p->index = constructor_index;
5205 p->range_end = constructor_range_end;
5206 p->max_index = constructor_max_index;
5207 p->unfilled_index = constructor_unfilled_index;
5208 p->unfilled_fields = constructor_unfilled_fields;
5209 p->bit_index = constructor_bit_index;
5210 p->elements = constructor_elements;
5211 p->constant = constructor_constant;
5212 p->simple = constructor_simple;
5213 p->erroneous = constructor_erroneous;
5214 p->pending_elts = constructor_pending_elts;
5215 p->depth = constructor_depth;
5216 p->replacement_value = 0;
5218 p->incremental = constructor_incremental;
5221 constructor_stack = p;
5223 constructor_constant = 1;
5224 constructor_simple = 1;
5225 constructor_depth = SPELLING_DEPTH ();
5226 constructor_elements = 0;
5227 constructor_pending_elts = 0;
5228 constructor_type = type;
5230 if (TREE_CODE (constructor_type) == RECORD_TYPE
5231 || TREE_CODE (constructor_type) == UNION_TYPE)
5233 constructor_fields = TYPE_FIELDS (constructor_type);
5234 /* Skip any nameless bit fields atthe beginning. */
5235 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5236 && DECL_NAME (constructor_fields) == 0)
5237 constructor_fields = TREE_CHAIN (constructor_fields);
5238 constructor_unfilled_fields = constructor_fields;
5239 constructor_bit_index = copy_node (integer_zero_node);
5241 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5243 constructor_range_end = 0;
5244 if (TYPE_DOMAIN (constructor_type))
5246 constructor_max_index
5247 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5249 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5252 constructor_index = copy_node (integer_zero_node);
5253 constructor_unfilled_index = copy_node (constructor_index);
5257 /* Handle the case of int x = {5}; */
5258 constructor_fields = constructor_type;
5259 constructor_unfilled_fields = constructor_type;
5262 if (constructor_incremental)
5264 int momentary = suspend_momentary ();
5265 push_obstacks_nochange ();
5266 if (TREE_PERMANENT (constructor_decl))
5267 end_temporary_allocation ();
5268 make_decl_rtl (constructor_decl, constructor_asmspec,
5269 constructor_top_level);
5270 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5272 resume_momentary (momentary);
5275 if (constructor_incremental)
5277 defer_addressed_constants ();
5278 constructor_subconstants_deferred = 1;
5282 /* Push down into a subobject, for initialization.
5283 If this is for an explicit set of braces, IMPLICIT is 0.
5284 If it is because the next element belongs at a lower level,
5288 push_init_level (implicit)
5291 struct constructor_stack *p;
5293 /* If we've exhausted any levels that didn't have braces,
5295 while (constructor_stack->implicit)
5297 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5298 || TREE_CODE (constructor_type) == UNION_TYPE)
5299 && constructor_fields == 0)
5300 process_init_element (pop_init_level (1));
5301 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5302 && tree_int_cst_lt (constructor_max_index, constructor_index))
5303 process_init_element (pop_init_level (1));
5308 /* Structure elements may require alignment. Do this now
5309 if necessary for the subaggregate. */
5310 if (constructor_incremental && constructor_type != 0
5311 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields)
5313 /* Advance to offset of this element. */
5314 if (! tree_int_cst_equal (constructor_bit_index,
5315 DECL_FIELD_BITPOS (constructor_fields)))
5317 int next = (TREE_INT_CST_LOW
5318 (DECL_FIELD_BITPOS (constructor_fields))
5320 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5323 assemble_zeros (next - here);
5327 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5328 p->type = constructor_type;
5329 p->fields = constructor_fields;
5330 p->index = constructor_index;
5331 p->range_end = constructor_range_end;
5332 p->max_index = constructor_max_index;
5333 p->unfilled_index = constructor_unfilled_index;
5334 p->unfilled_fields = constructor_unfilled_fields;
5335 p->bit_index = constructor_bit_index;
5336 p->elements = constructor_elements;
5337 p->constant = constructor_constant;
5338 p->simple = constructor_simple;
5339 p->erroneous = constructor_erroneous;
5340 p->pending_elts = constructor_pending_elts;
5341 p->depth = constructor_depth;
5342 p->replacement_value = 0;
5343 p->implicit = implicit;
5344 p->incremental = constructor_incremental;
5346 p->next = constructor_stack;
5347 constructor_stack = p;
5349 constructor_constant = 1;
5350 constructor_simple = 1;
5351 constructor_depth = SPELLING_DEPTH ();
5352 constructor_elements = 0;
5353 constructor_pending_elts = 0;
5355 /* Don't die if an entire brace-pair level is superfluous
5356 in the containing level. */
5357 if (constructor_type == 0)
5359 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5360 || TREE_CODE (constructor_type) == UNION_TYPE)
5362 /* Don't die if there are extra init elts at the end. */
5363 if (constructor_fields == 0)
5364 constructor_type = 0;
5367 constructor_type = TREE_TYPE (constructor_fields);
5368 push_member_name (constructor_fields);
5369 constructor_depth++;
5370 if (constructor_fields != constructor_unfilled_fields)
5371 constructor_incremental = 0;
5374 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5376 constructor_type = TREE_TYPE (constructor_type);
5377 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5378 constructor_depth++;
5379 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5380 || constructor_range_end != 0)
5381 constructor_incremental = 0;
5384 if (constructor_type == 0)
5386 error_init ("extra brace group at end of initializer%s",
5388 constructor_fields = 0;
5389 constructor_unfilled_fields = 0;
5393 /* Turn off constructor_incremental if type is a struct with bitfields. */
5394 check_init_type_bitfields (constructor_type);
5396 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5398 missing_braces_mentioned = 1;
5399 warning_init ("missing braces around initializer%s", " for `%s'", NULL);
5402 if (TREE_CODE (constructor_type) == RECORD_TYPE
5403 || TREE_CODE (constructor_type) == UNION_TYPE)
5405 constructor_fields = TYPE_FIELDS (constructor_type);
5406 /* Skip any nameless bit fields atthe beginning. */
5407 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5408 && DECL_NAME (constructor_fields) == 0)
5409 constructor_fields = TREE_CHAIN (constructor_fields);
5410 constructor_unfilled_fields = constructor_fields;
5411 constructor_bit_index = copy_node (integer_zero_node);
5413 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5415 constructor_range_end = 0;
5416 if (TYPE_DOMAIN (constructor_type))
5418 constructor_max_index
5419 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5421 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5424 constructor_index = copy_node (integer_zero_node);
5425 constructor_unfilled_index = copy_node (constructor_index);
5429 warning_init ("braces around scalar initializer%s", " for `%s'", NULL);
5430 constructor_fields = constructor_type;
5431 constructor_unfilled_fields = constructor_type;
5435 /* Don't read a struct incrementally if it has any bitfields,
5436 because the incremental reading code doesn't know how to
5437 handle bitfields yet. */
5440 check_init_type_bitfields (type)
5443 if (TREE_CODE (type) == RECORD_TYPE)
5446 for (tail = TYPE_FIELDS (type); tail;
5447 tail = TREE_CHAIN (tail))
5449 if (DECL_BIT_FIELD (tail)
5450 /* This catches cases like `int foo : 8;'. */
5451 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail)))
5453 constructor_incremental = 0;
5457 check_init_type_bitfields (TREE_TYPE (tail));
5461 else if (TREE_CODE (type) == ARRAY_TYPE)
5462 check_init_type_bitfields (TREE_TYPE (type));
5465 /* At the end of an implicit or explicit brace level,
5466 finish up that level of constructor.
5467 If we were outputting the elements as they are read, return 0
5468 from inner levels (process_init_element ignores that),
5469 but return error_mark_node from the outermost level
5470 (that's what we want to put in DECL_INITIAL).
5471 Otherwise, return a CONSTRUCTOR expression. */
5474 pop_init_level (implicit)
5477 struct constructor_stack *p;
5479 tree constructor = 0;
5483 /* When we come to an explicit close brace,
5484 pop any inner levels that didn't have explicit braces. */
5485 while (constructor_stack->implicit)
5486 process_init_element (pop_init_level (1));
5489 p = constructor_stack;
5491 if (constructor_type != 0)
5492 size = int_size_in_bytes (constructor_type);
5494 /* Now output all pending elements. */
5495 output_pending_init_elements (1);
5497 #if 0 /* c-parse.in warns about {}. */
5498 /* In ANSI, each brace level must have at least one element. */
5499 if (! implicit && pedantic
5500 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5501 ? integer_zerop (constructor_unfilled_index)
5502 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5503 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL);
5506 /* Pad out the end of the structure. */
5508 if (p->replacement_value)
5510 /* If this closes a superfluous brace pair,
5511 just pass out the element between them. */
5512 constructor = p->replacement_value;
5513 /* If this is the top level thing within the initializer,
5514 and it's for a variable, then since we already called
5515 assemble_variable, we must output the value now. */
5516 if (p->next == 0 && constructor_decl != 0
5517 && constructor_incremental)
5519 constructor = digest_init (constructor_type, constructor,
5520 require_constant_value,
5521 require_constant_elements);
5523 /* If initializing an array of unknown size,
5524 determine the size now. */
5525 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5526 && TYPE_DOMAIN (constructor_type) == 0)
5531 push_obstacks_nochange ();
5532 if (TREE_PERMANENT (constructor_type))
5533 end_temporary_allocation ();
5535 momentary_p = suspend_momentary ();
5537 /* We shouldn't have an incomplete array type within
5539 if (constructor_stack->next)
5543 = complete_array_type (constructor_type,
5548 size = int_size_in_bytes (constructor_type);
5549 resume_momentary (momentary_p);
5553 output_constant (constructor, size);
5556 else if (constructor_type == 0)
5558 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5559 && TREE_CODE (constructor_type) != UNION_TYPE
5560 && TREE_CODE (constructor_type) != ARRAY_TYPE
5561 && ! constructor_incremental)
5563 /* A nonincremental scalar initializer--just return
5564 the element, after verifying there is just one. */
5565 if (constructor_elements == 0)
5567 error_init ("empty scalar initializer%s",
5569 constructor = error_mark_node;
5571 else if (TREE_CHAIN (constructor_elements) != 0)
5573 error_init ("extra elements in scalar initializer%s",
5575 constructor = TREE_VALUE (constructor_elements);
5578 constructor = TREE_VALUE (constructor_elements);
5580 else if (! constructor_incremental)
5582 if (constructor_erroneous)
5583 constructor = error_mark_node;
5586 int momentary = suspend_momentary ();
5588 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5589 nreverse (constructor_elements));
5590 if (constructor_constant)
5591 TREE_CONSTANT (constructor) = 1;
5592 if (constructor_constant && constructor_simple)
5593 TREE_STATIC (constructor) = 1;
5595 resume_momentary (momentary);
5601 int momentary = suspend_momentary ();
5603 if (TREE_CODE (constructor_type) == RECORD_TYPE
5604 || TREE_CODE (constructor_type) == UNION_TYPE)
5606 /* Find the offset of the end of that field. */
5607 filled = size_binop (CEIL_DIV_EXPR,
5608 constructor_bit_index,
5609 size_int (BITS_PER_UNIT));
5611 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5613 /* If initializing an array of unknown size,
5614 determine the size now. */
5615 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5616 && TYPE_DOMAIN (constructor_type) == 0)
5619 = size_binop (MINUS_EXPR,
5620 constructor_unfilled_index,
5623 push_obstacks_nochange ();
5624 if (TREE_PERMANENT (constructor_type))
5625 end_temporary_allocation ();
5626 maxindex = copy_node (maxindex);
5627 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5628 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5630 /* TYPE_MAX_VALUE is always one less than the number of elements
5631 in the array, because we start counting at zero. Therefore,
5632 warn only if the value is less than zero. */
5634 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5636 error_with_decl (constructor_decl,
5637 "zero or negative array size `%s'");
5638 layout_type (constructor_type);
5639 size = int_size_in_bytes (constructor_type);
5643 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5644 size_in_bytes (TREE_TYPE (constructor_type)));
5650 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5652 resume_momentary (momentary);
5656 constructor_type = p->type;
5657 constructor_fields = p->fields;
5658 constructor_index = p->index;
5659 constructor_range_end = p->range_end;
5660 constructor_max_index = p->max_index;
5661 constructor_unfilled_index = p->unfilled_index;
5662 constructor_unfilled_fields = p->unfilled_fields;
5663 constructor_bit_index = p->bit_index;
5664 constructor_elements = p->elements;
5665 constructor_constant = p->constant;
5666 constructor_simple = p->simple;
5667 constructor_erroneous = p->erroneous;
5668 constructor_pending_elts = p->pending_elts;
5669 constructor_depth = p->depth;
5670 constructor_incremental = p->incremental;
5671 RESTORE_SPELLING_DEPTH (constructor_depth);
5673 constructor_stack = p->next;
5676 if (constructor == 0)
5678 if (constructor_stack == 0)
5679 return error_mark_node;
5685 /* Within an array initializer, specify the next index to be initialized.
5686 FIRST is that index. If LAST is nonzero, then initialize a range
5687 of indices, running from FIRST through LAST. */
5690 set_init_index (first, last)
5693 while ((TREE_CODE (first) == NOP_EXPR
5694 || TREE_CODE (first) == CONVERT_EXPR
5695 || TREE_CODE (first) == NON_LVALUE_EXPR)
5696 && (TYPE_MODE (TREE_TYPE (first))
5697 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5698 (first) = TREE_OPERAND (first, 0);
5700 while ((TREE_CODE (last) == NOP_EXPR
5701 || TREE_CODE (last) == CONVERT_EXPR
5702 || TREE_CODE (last) == NON_LVALUE_EXPR)
5703 && (TYPE_MODE (TREE_TYPE (last))
5704 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5705 (last) = TREE_OPERAND (last, 0);
5707 if (TREE_CODE (first) != INTEGER_CST)
5708 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5709 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5710 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5711 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5712 error_init ("duplicate array index in initializer%s", " for `%s'", NULL);
5715 TREE_INT_CST_LOW (constructor_index)
5716 = TREE_INT_CST_LOW (first);
5717 TREE_INT_CST_HIGH (constructor_index)
5718 = TREE_INT_CST_HIGH (first);
5720 if (last != 0 && tree_int_cst_lt (last, first))
5721 error_init ("empty index range in initializer%s", " for `%s'", NULL);
5725 pedwarn ("ANSI C forbids specifying element to initialize");
5726 constructor_range_end = last;
5731 /* Within a struct initializer, specify the next field to be initialized. */
5734 set_init_label (fieldname)
5740 for (tail = TYPE_FIELDS (constructor_type); tail;
5741 tail = TREE_CHAIN (tail))
5743 if (tail == constructor_unfilled_fields)
5745 if (DECL_NAME (tail) == fieldname)
5750 error ("unknown field `%s' specified in initializer",
5751 IDENTIFIER_POINTER (fieldname));
5753 error ("field `%s' already initialized",
5754 IDENTIFIER_POINTER (fieldname));
5757 constructor_fields = tail;
5759 pedwarn ("ANSI C forbids specifying structure member to initialize");
5763 /* "Output" the next constructor element.
5764 At top level, really output it to assembler code now.
5765 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5766 TYPE is the data type that the containing data type wants here.
5767 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5769 PENDING if non-nil means output pending elements that belong
5770 right after this element. (PENDING is normally 1;
5771 it is 0 while outputting pending elements, to avoid recursion.) */
5774 output_init_element (value, type, field, pending)
5775 tree value, type, field;
5780 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5781 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5782 && !(TREE_CODE (value) == STRING_CST
5783 && TREE_CODE (type) == ARRAY_TYPE
5784 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5785 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5786 TYPE_MAIN_VARIANT (type))))
5787 value = default_conversion (value);
5789 if (value == error_mark_node)
5790 constructor_erroneous = 1;
5791 else if (!TREE_CONSTANT (value))
5792 constructor_constant = 0;
5793 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5794 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5795 || TREE_CODE (constructor_type) == UNION_TYPE)
5796 && DECL_BIT_FIELD (field) && TREE_CODE (value) != INTEGER_CST))
5797 constructor_simple = 0;
5799 if (require_constant_value && ! TREE_CONSTANT (value))
5801 error_init ("initializer element%s is not constant",
5803 value = error_mark_node;
5805 else if (require_constant_elements
5806 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5808 error_init ("initializer element%s is not computable at load time",
5810 value = error_mark_node;
5813 /* If this element duplicates one on constructor_pending_elts,
5814 print a message and ignore it. Don't do this when we're
5815 processing elements taken off constructor_pending_elts,
5816 because we'd always get spurious errors. */
5819 if (TREE_CODE (constructor_type) == RECORD_TYPE
5820 || TREE_CODE (constructor_type) == UNION_TYPE)
5822 if (purpose_member (field, constructor_pending_elts))
5824 error_init ("duplicate initializer%s", " for `%s'", NULL);
5828 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5831 for (tail = constructor_pending_elts; tail;
5832 tail = TREE_CHAIN (tail))
5833 if (TREE_PURPOSE (tail) != 0
5834 && TREE_CODE (TREE_PURPOSE (tail)) == INTEGER_CST
5835 && tree_int_cst_equal (TREE_PURPOSE (tail), constructor_index))
5840 error_init ("duplicate initializer%s", " for `%s'", NULL);
5846 /* If this element doesn't come next in sequence,
5847 put it on constructor_pending_elts. */
5848 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5849 && !tree_int_cst_equal (field, constructor_unfilled_index))
5852 /* The copy_node is needed in case field is actually
5853 constructor_index, which is modified in place. */
5854 constructor_pending_elts
5855 = tree_cons (copy_node (field),
5856 digest_init (type, value, require_constant_value,
5857 require_constant_elements),
5858 constructor_pending_elts);
5860 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5861 && field != constructor_unfilled_fields)
5863 /* We do this for records but not for unions. In a union,
5864 no matter which field is specified, it can be initialized
5865 right away since it starts at the beginning of the union. */
5867 constructor_pending_elts
5869 digest_init (type, value, require_constant_value,
5870 require_constant_elements),
5871 constructor_pending_elts);
5875 /* Otherwise, output this element either to
5876 constructor_elements or to the assembler file. */
5880 if (! constructor_incremental)
5882 if (field && TREE_CODE (field) == INTEGER_CST)
5883 field = copy_node (field);
5884 constructor_elements
5885 = tree_cons (field, digest_init (type, value,
5886 require_constant_value,
5887 require_constant_elements),
5888 constructor_elements);
5892 /* Structure elements may require alignment.
5893 Do this, if necessary. */
5894 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5896 /* Advance to offset of this element. */
5897 if (! tree_int_cst_equal (constructor_bit_index,
5898 DECL_FIELD_BITPOS (field)))
5900 int next = (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
5902 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5905 assemble_zeros (next - here);
5908 output_constant (digest_init (type, value,
5909 require_constant_value,
5910 require_constant_elements),
5911 int_size_in_bytes (type));
5913 /* For a record or union,
5914 keep track of end position of last field. */
5915 if (TREE_CODE (constructor_type) == RECORD_TYPE
5916 || TREE_CODE (constructor_type) == UNION_TYPE)
5918 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
5920 TREE_INT_CST_LOW (constructor_bit_index)
5921 = TREE_INT_CST_LOW (temp);
5922 TREE_INT_CST_HIGH (constructor_bit_index)
5923 = TREE_INT_CST_HIGH (temp);
5928 /* Advance the variable that indicates sequential elements output. */
5929 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5931 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
5933 TREE_INT_CST_LOW (constructor_unfilled_index)
5934 = TREE_INT_CST_LOW (tem);
5935 TREE_INT_CST_HIGH (constructor_unfilled_index)
5936 = TREE_INT_CST_HIGH (tem);
5938 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5939 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5940 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5941 constructor_unfilled_fields = 0;
5943 /* Now output any pending elements which have become next. */
5945 output_pending_init_elements (0);
5949 /* Output any pending elements which have become next.
5950 As we output elements, constructor_unfilled_{fields,index}
5951 advances, which may cause other elements to become next;
5952 if so, they too are output.
5954 If ALL is 0, we return when there are
5955 no more pending elements to output now.
5957 If ALL is 1, we output space as necessary so that
5958 we can output all the pending elements. */
5961 output_pending_init_elements (all)
5969 /* Look thru the whole pending list.
5970 If we find an element that should be output now,
5971 output it. Otherwise, set NEXT to the element
5972 that comes first among those still pending. */
5975 for (tail = constructor_pending_elts; tail;
5976 tail = TREE_CHAIN (tail))
5978 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5980 if (tree_int_cst_equal (TREE_PURPOSE (tail),
5981 constructor_unfilled_index))
5983 output_init_element (TREE_VALUE (tail),
5984 TREE_TYPE (constructor_type),
5985 constructor_unfilled_index, 0);
5988 else if (tree_int_cst_lt (TREE_PURPOSE (tail),
5989 constructor_unfilled_index))
5992 || tree_int_cst_lt (TREE_PURPOSE (tail), next))
5993 next = TREE_PURPOSE (tail);
5995 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5996 || TREE_CODE (constructor_type) == UNION_TYPE)
5998 if (TREE_PURPOSE (tail) == constructor_unfilled_fields)
6000 output_init_element (TREE_VALUE (tail),
6001 TREE_TYPE (constructor_unfilled_fields),
6002 constructor_unfilled_fields,
6006 else if (constructor_unfilled_fields == 0
6007 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
6008 DECL_FIELD_BITPOS (constructor_unfilled_fields)))
6011 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
6012 DECL_FIELD_BITPOS (next)))
6013 next = TREE_PURPOSE (tail);
6017 /* Ordinarily return, but not if we want to output all
6018 and there are elements left. */
6019 if (! (all && next != 0))
6022 /* Generate space up to the position of NEXT. */
6023 if (constructor_incremental)
6026 tree nextpos_tree = size_int (0);
6028 if (TREE_CODE (constructor_type) == RECORD_TYPE
6029 || TREE_CODE (constructor_type) == UNION_TYPE)
6031 /* Find the last field written out, if any. */
6032 for (tail = TYPE_FIELDS (constructor_type); tail;
6033 tail = TREE_CHAIN (tail))
6034 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
6038 /* Find the offset of the end of that field. */
6039 filled = size_binop (CEIL_DIV_EXPR,
6040 size_binop (PLUS_EXPR,
6041 DECL_FIELD_BITPOS (tail),
6043 size_int (BITS_PER_UNIT));
6045 filled = size_int (0);
6047 nextpos_tree = size_binop (CEIL_DIV_EXPR,
6048 DECL_FIELD_BITPOS (next),
6049 size_int (BITS_PER_UNIT));
6051 TREE_INT_CST_HIGH (constructor_bit_index)
6052 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
6053 TREE_INT_CST_LOW (constructor_bit_index)
6054 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
6055 constructor_unfilled_fields = next;
6057 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6059 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
6060 size_in_bytes (TREE_TYPE (constructor_type)));
6062 = size_binop (MULT_EXPR, next,
6063 size_in_bytes (TREE_TYPE (constructor_type)));
6064 TREE_INT_CST_LOW (constructor_unfilled_index)
6065 = TREE_INT_CST_LOW (next);
6066 TREE_INT_CST_HIGH (constructor_unfilled_index)
6067 = TREE_INT_CST_HIGH (next);
6074 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
6076 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
6081 /* If it's not incremental, just skip over the gap,
6082 so that after jumping to retry we will output the next
6083 successive element. */
6084 if (TREE_CODE (constructor_type) == RECORD_TYPE
6085 || TREE_CODE (constructor_type) == UNION_TYPE)
6086 constructor_unfilled_fields = next;
6087 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6089 TREE_INT_CST_LOW (constructor_unfilled_index)
6090 = TREE_INT_CST_LOW (next);
6091 TREE_INT_CST_HIGH (constructor_unfilled_index)
6092 = TREE_INT_CST_HIGH (next);
6099 /* Add one non-braced element to the current constructor level.
6100 This adjusts the current position within the constructor's type.
6101 This may also start or terminate implicit levels
6102 to handle a partly-braced initializer.
6104 Once this has found the correct level for the new element,
6105 it calls output_init_element.
6107 Note: if we are incrementally outputting this constructor,
6108 this function may be called with a null argument
6109 representing a sub-constructor that was already incrementally output.
6110 When that happens, we output nothing, but we do the bookkeeping
6111 to skip past that element of the current constructor. */
6114 process_init_element (value)
6117 tree orig_value = value;
6118 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6120 /* Handle superfluous braces around string cst as in
6121 char x[] = {"foo"}; */
6124 && TREE_CODE (constructor_type) == ARRAY_TYPE
6125 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6126 && integer_zerop (constructor_unfilled_index))
6128 constructor_stack->replacement_value = value;
6132 if (constructor_stack->replacement_value != 0)
6134 error_init ("excess elements in struct initializer%s",
6135 " after `%s'", NULL_PTR);
6139 /* Ignore elements of a brace group if it is entirely superfluous
6140 and has already been diagnosed. */
6141 if (constructor_type == 0)
6144 /* If we've exhausted any levels that didn't have braces,
6146 while (constructor_stack->implicit)
6148 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6149 || TREE_CODE (constructor_type) == UNION_TYPE)
6150 && constructor_fields == 0)
6151 process_init_element (pop_init_level (1));
6152 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6153 && tree_int_cst_lt (constructor_max_index, constructor_index))
6154 process_init_element (pop_init_level (1));
6161 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6164 enum tree_code fieldcode;
6166 if (constructor_fields == 0)
6168 pedwarn_init ("excess elements in struct initializer%s",
6169 " after `%s'", NULL_PTR);
6173 fieldtype = TREE_TYPE (constructor_fields);
6174 if (fieldtype != error_mark_node)
6175 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6176 fieldcode = TREE_CODE (fieldtype);
6178 /* Accept a string constant to initialize a subarray. */
6180 && fieldcode == ARRAY_TYPE
6181 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6184 /* Otherwise, if we have come to a subaggregate,
6185 and we don't have an element of its type, push into it. */
6186 else if (value != 0 && !constructor_no_implicit
6187 && value != error_mark_node
6188 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6189 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6190 || fieldcode == UNION_TYPE))
6192 push_init_level (1);
6198 push_member_name (constructor_fields);
6199 output_init_element (value, fieldtype, constructor_fields, 1);
6200 RESTORE_SPELLING_DEPTH (constructor_depth);
6203 /* Do the bookkeeping for an element that was
6204 directly output as a constructor. */
6206 /* For a record, keep track of end position of last field. */
6207 tree temp = size_binop (PLUS_EXPR,
6208 DECL_FIELD_BITPOS (constructor_fields),
6209 DECL_SIZE (constructor_fields));
6210 TREE_INT_CST_LOW (constructor_bit_index)
6211 = TREE_INT_CST_LOW (temp);
6212 TREE_INT_CST_HIGH (constructor_bit_index)
6213 = TREE_INT_CST_HIGH (temp);
6215 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6218 constructor_fields = TREE_CHAIN (constructor_fields);
6219 /* Skip any nameless bit fields atthe beginning. */
6220 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
6221 && DECL_NAME (constructor_fields) == 0)
6222 constructor_fields = TREE_CHAIN (constructor_fields);
6225 if (TREE_CODE (constructor_type) == UNION_TYPE)
6228 enum tree_code fieldcode;
6230 if (constructor_fields == 0)
6232 pedwarn_init ("excess elements in union initializer%s",
6233 " after `%s'", NULL_PTR);
6237 fieldtype = TREE_TYPE (constructor_fields);
6238 if (fieldtype != error_mark_node)
6239 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6240 fieldcode = TREE_CODE (fieldtype);
6242 /* Accept a string constant to initialize a subarray. */
6244 && fieldcode == ARRAY_TYPE
6245 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6248 /* Otherwise, if we have come to a subaggregate,
6249 and we don't have an element of its type, push into it. */
6250 else if (value != 0 && !constructor_no_implicit
6251 && value != error_mark_node
6252 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6253 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6254 || fieldcode == UNION_TYPE))
6256 push_init_level (1);
6262 push_member_name (constructor_fields);
6263 output_init_element (value, fieldtype, constructor_fields, 1);
6264 RESTORE_SPELLING_DEPTH (constructor_depth);
6267 /* Do the bookkeeping for an element that was
6268 directly output as a constructor. */
6270 TREE_INT_CST_LOW (constructor_bit_index)
6271 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6272 TREE_INT_CST_HIGH (constructor_bit_index)
6273 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6275 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6278 constructor_fields = 0;
6281 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6283 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6284 enum tree_code eltcode = TREE_CODE (elttype);
6286 /* Accept a string constant to initialize a subarray. */
6288 && eltcode == ARRAY_TYPE
6289 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6292 /* Otherwise, if we have come to a subaggregate,
6293 and we don't have an element of its type, push into it. */
6294 else if (value != 0 && !constructor_no_implicit
6295 && value != error_mark_node
6296 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6297 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6298 || eltcode == UNION_TYPE))
6300 push_init_level (1);
6304 if (constructor_max_index != 0
6305 && tree_int_cst_lt (constructor_max_index, constructor_index))
6307 pedwarn_init ("excess elements in array initializer%s",
6308 " after `%s'", NULL_PTR);
6312 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */
6313 if (constructor_range_end)
6314 value = save_expr (value);
6316 /* Now output the actual element.
6317 Ordinarily, output once.
6318 If there is a range, repeat it till we advance past the range. */
6325 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6326 output_init_element (value, elttype, constructor_index, 1);
6327 RESTORE_SPELLING_DEPTH (constructor_depth);
6330 tem = size_binop (PLUS_EXPR, constructor_index,
6332 TREE_INT_CST_LOW (constructor_index)
6333 = TREE_INT_CST_LOW (tem);
6334 TREE_INT_CST_HIGH (constructor_index)
6335 = TREE_INT_CST_HIGH (tem);
6338 /* If we are doing the bookkeeping for an element that was
6339 directly output as a constructor,
6340 we must update constructor_unfilled_index. */
6342 TREE_INT_CST_LOW (constructor_unfilled_index)
6343 = TREE_INT_CST_LOW (constructor_index);
6344 TREE_INT_CST_HIGH (constructor_unfilled_index)
6345 = TREE_INT_CST_HIGH (constructor_index);
6348 while (! (constructor_range_end == 0
6349 || tree_int_cst_lt (constructor_range_end,
6350 constructor_index)));
6355 /* Handle the sole element allowed in a braced initializer
6356 for a scalar variable. */
6357 if (constructor_fields == 0)
6359 pedwarn_init ("excess elements in scalar initializer%s",
6360 " after `%s'", NULL_PTR);
6365 output_init_element (value, constructor_type, NULL_TREE, 1);
6366 constructor_fields = 0;
6370 /* If the (lexically) previous elments are not now saved,
6371 we can discard the storage for them. */
6372 if (constructor_incremental && constructor_pending_elts == 0 && value != 0
6373 && constructor_stack == 0)
6377 /* Expand an ASM statement with operands, handling output operands
6378 that are not variables or INDIRECT_REFS by transforming such
6379 cases into cases that expand_asm_operands can handle.
6381 Arguments are same as for expand_asm_operands. */
6384 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6385 tree string, outputs, inputs, clobbers;
6390 int noutputs = list_length (outputs);
6392 /* o[I] is the place that output number I should be written. */
6393 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6396 if (TREE_CODE (string) == ADDR_EXPR)
6397 string = TREE_OPERAND (string, 0);
6398 if (TREE_CODE (string) != STRING_CST)
6400 error ("asm template is not a string constant");
6404 /* Record the contents of OUTPUTS before it is modified. */
6405 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6406 o[i] = TREE_VALUE (tail);
6408 /* Perform default conversions on array and function inputs. */
6409 /* Don't do this for other types--
6410 it would screw up operands expected to be in memory. */
6411 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6412 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6413 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6414 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6416 /* Generate the ASM_OPERANDS insn;
6417 store into the TREE_VALUEs of OUTPUTS some trees for
6418 where the values were actually stored. */
6419 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6421 /* Copy all the intermediate outputs into the specified outputs. */
6422 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6424 if (o[i] != TREE_VALUE (tail))
6426 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6430 /* Detect modification of read-only values.
6431 (Otherwise done by build_modify_expr.) */
6434 tree type = TREE_TYPE (o[i]);
6435 if (TREE_READONLY (o[i])
6436 || TYPE_READONLY (type)
6437 || ((TREE_CODE (type) == RECORD_TYPE
6438 || TREE_CODE (type) == UNION_TYPE)
6439 && C_TYPE_FIELDS_READONLY (type)))
6440 readonly_warning (o[i], "modification by `asm'");
6444 /* Those MODIFY_EXPRs could do autoincrements. */
6448 /* Expand a C `return' statement.
6449 RETVAL is the expression for what to return,
6450 or a null pointer for `return;' with no value. */
6453 c_expand_return (retval)
6456 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6458 if (TREE_THIS_VOLATILE (current_function_decl))
6459 warning ("function declared `noreturn' has a `return' statement");
6463 current_function_returns_null = 1;
6464 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6465 warning ("`return' with no value, in function returning non-void");
6466 expand_null_return ();
6468 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6470 current_function_returns_null = 1;
6471 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6472 pedwarn ("`return' with a value, in function returning void");
6473 expand_return (retval);
6477 tree t = convert_for_assignment (valtype, retval, "return",
6478 NULL_TREE, NULL_TREE, 0);
6479 tree res = DECL_RESULT (current_function_decl);
6482 if (t == error_mark_node)
6485 inner = t = convert (TREE_TYPE (res), t);
6487 /* Strip any conversions, additions, and subtractions, and see if
6488 we are returning the address of a local variable. Warn if so. */
6491 switch (TREE_CODE (inner))
6493 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6495 inner = TREE_OPERAND (inner, 0);
6499 /* If the second operand of the MINUS_EXPR has a pointer
6500 type (or is converted from it), this may be valid, so
6501 don't give a warning. */
6503 tree op1 = TREE_OPERAND (inner, 1);
6505 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6506 && (TREE_CODE (op1) == NOP_EXPR
6507 || TREE_CODE (op1) == NON_LVALUE_EXPR
6508 || TREE_CODE (op1) == CONVERT_EXPR))
6509 op1 = TREE_OPERAND (op1, 0);
6511 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6514 inner = TREE_OPERAND (inner, 0);
6519 inner = TREE_OPERAND (inner, 0);
6521 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6522 inner = TREE_OPERAND (inner, 0);
6524 if (TREE_CODE (inner) == VAR_DECL
6525 && ! DECL_EXTERNAL (inner)
6526 && ! TREE_STATIC (inner)
6527 && DECL_CONTEXT (inner) == current_function_decl)
6528 warning ("function returns address of local variable");
6535 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6536 TREE_SIDE_EFFECTS (t) = 1;
6538 current_function_returns_value = 1;
6542 /* Start a C switch statement, testing expression EXP.
6543 Return EXP if it is valid, an error node otherwise. */
6546 c_expand_start_case (exp)
6549 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6550 tree type = TREE_TYPE (exp);
6552 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6554 error ("switch quantity not an integer");
6555 exp = error_mark_node;
6560 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6562 if (warn_traditional
6563 && (type == long_integer_type_node
6564 || type == long_unsigned_type_node))
6565 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6567 exp = default_conversion (exp);
6568 type = TREE_TYPE (exp);
6569 index = get_unwidened (exp, NULL_TREE);
6570 /* We can't strip a conversion from a signed type to an unsigned,
6571 because if we did, int_fits_type_p would do the wrong thing
6572 when checking case values for being in range,
6573 and it's too hard to do the right thing. */
6574 if (TREE_UNSIGNED (TREE_TYPE (exp))
6575 == TREE_UNSIGNED (TREE_TYPE (index)))
6579 expand_start_case (1, exp, type, "switch statement");