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 |");
1826 /* Check cases like x|y==z */
1827 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1828 warning ("suggest parentheses around comparison in operand of |");
1831 if (code == BIT_XOR_EXPR)
1833 if (code1 == BIT_AND_EXPR
1834 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1835 || code2 == BIT_AND_EXPR
1836 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1837 warning ("suggest parentheses around arithmetic in operand of ^");
1838 /* Check cases like x^y==z */
1839 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1840 warning ("suggest parentheses around comparison in operand of ^");
1843 if (code == BIT_AND_EXPR)
1845 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1846 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1847 warning ("suggest parentheses around + or - in operand of &");
1848 /* Check cases like x&y==z */
1849 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1850 warning ("suggest parentheses around comparison in operand of &");
1854 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1855 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1856 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1857 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1859 unsigned_conversion_warning (result, arg1);
1860 unsigned_conversion_warning (result, arg2);
1861 overflow_warning (result);
1863 class = TREE_CODE_CLASS (TREE_CODE (result));
1865 /* Record the code that was specified in the source,
1866 for the sake of warnings about confusing nesting. */
1867 if (class == 'e' || class == '1'
1868 || class == '2' || class == '<')
1869 C_SET_EXP_ORIGINAL_CODE (result, code);
1872 int flag = TREE_CONSTANT (result);
1873 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1874 so that convert_for_assignment wouldn't strip it.
1875 That way, we got warnings for things like p = (1 - 1).
1876 But it turns out we should not get those warnings. */
1877 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1878 C_SET_EXP_ORIGINAL_CODE (result, code);
1879 TREE_CONSTANT (result) = flag;
1885 /* Build a binary-operation expression without default conversions.
1886 CODE is the kind of expression to build.
1887 This function differs from `build' in several ways:
1888 the data type of the result is computed and recorded in it,
1889 warnings are generated if arg data types are invalid,
1890 special handling for addition and subtraction of pointers is known,
1891 and some optimization is done (operations on narrow ints
1892 are done in the narrower type when that gives the same result).
1893 Constant folding is also done before the result is returned.
1895 Note that the operands will never have enumeral types, or function
1896 or array types, because either they will have the default conversions
1897 performed or they have both just been converted to some other type in which
1898 the arithmetic is to be done. */
1901 build_binary_op (code, orig_op0, orig_op1, convert_p)
1902 enum tree_code code;
1903 tree orig_op0, orig_op1;
1907 register enum tree_code code0, code1;
1910 /* Expression code to give to the expression when it is built.
1911 Normally this is CODE, which is what the caller asked for,
1912 but in some special cases we change it. */
1913 register enum tree_code resultcode = code;
1915 /* Data type in which the computation is to be performed.
1916 In the simplest cases this is the common type of the arguments. */
1917 register tree result_type = NULL;
1919 /* Nonzero means operands have already been type-converted
1920 in whatever way is necessary.
1921 Zero means they need to be converted to RESULT_TYPE. */
1924 /* Nonzero means create the expression with this type, rather than
1926 tree build_type = 0;
1928 /* Nonzero means after finally constructing the expression
1929 convert it to this type. */
1930 tree final_type = 0;
1932 /* Nonzero if this is an operation like MIN or MAX which can
1933 safely be computed in short if both args are promoted shorts.
1934 Also implies COMMON.
1935 -1 indicates a bitwise operation; this makes a difference
1936 in the exact conditions for when it is safe to do the operation
1937 in a narrower mode. */
1940 /* Nonzero if this is a comparison operation;
1941 if both args are promoted shorts, compare the original shorts.
1942 Also implies COMMON. */
1943 int short_compare = 0;
1945 /* Nonzero if this is a right-shift operation, which can be computed on the
1946 original short and then promoted if the operand is a promoted short. */
1947 int short_shift = 0;
1949 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1954 op0 = default_conversion (orig_op0);
1955 op1 = default_conversion (orig_op1);
1963 type0 = TREE_TYPE (op0);
1964 type1 = TREE_TYPE (op1);
1966 /* The expression codes of the data types of the arguments tell us
1967 whether the arguments are integers, floating, pointers, etc. */
1968 code0 = TREE_CODE (type0);
1969 code1 = TREE_CODE (type1);
1971 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1972 STRIP_TYPE_NOPS (op0);
1973 STRIP_TYPE_NOPS (op1);
1975 /* If an error was already reported for one of the arguments,
1976 avoid reporting another error. */
1978 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1979 return error_mark_node;
1984 /* Handle the pointer + int case. */
1985 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1986 return pointer_int_sum (PLUS_EXPR, op0, op1);
1987 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1988 return pointer_int_sum (PLUS_EXPR, op1, op0);
1994 /* Subtraction of two similar pointers.
1995 We must subtract them as integers, then divide by object size. */
1996 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1997 && comp_target_types (type0, type1))
1998 return pointer_diff (op0, op1);
1999 /* Handle pointer minus int. Just like pointer plus int. */
2000 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2001 return pointer_int_sum (MINUS_EXPR, op0, op1);
2010 case TRUNC_DIV_EXPR:
2012 case FLOOR_DIV_EXPR:
2013 case ROUND_DIV_EXPR:
2014 case EXACT_DIV_EXPR:
2015 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2016 || code0 == COMPLEX_TYPE)
2017 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2018 || code1 == COMPLEX_TYPE))
2020 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2021 resultcode = RDIV_EXPR;
2024 /* Although it would be tempting to shorten always here, that
2025 loses on some targets, since the modulo instruction is
2026 undefined if the quotient can't be represented in the
2027 computation mode. We shorten only if unsigned or if
2028 dividing by something we know != -1. */
2029 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2030 || (TREE_CODE (op1) == INTEGER_CST
2031 && (TREE_INT_CST_LOW (op1) != -1
2032 || TREE_INT_CST_HIGH (op1) != -1)));
2039 case BIT_ANDTC_EXPR:
2042 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2044 /* If one operand is a constant, and the other is a short type
2045 that has been converted to an int,
2046 really do the work in the short type and then convert the
2047 result to int. If we are lucky, the constant will be 0 or 1
2048 in the short type, making the entire operation go away. */
2049 if (TREE_CODE (op0) == INTEGER_CST
2050 && TREE_CODE (op1) == NOP_EXPR
2051 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2052 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2054 final_type = result_type;
2055 op1 = TREE_OPERAND (op1, 0);
2056 result_type = TREE_TYPE (op1);
2058 if (TREE_CODE (op1) == INTEGER_CST
2059 && TREE_CODE (op0) == NOP_EXPR
2060 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2061 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2063 final_type = result_type;
2064 op0 = TREE_OPERAND (op0, 0);
2065 result_type = TREE_TYPE (op0);
2069 case TRUNC_MOD_EXPR:
2070 case FLOOR_MOD_EXPR:
2071 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2073 /* Although it would be tempting to shorten always here, that loses
2074 on some targets, since the modulo instruction is undefined if the
2075 quotient can't be represented in the computation mode. We shorten
2076 only if unsigned or if dividing by something we know != -1. */
2077 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2078 || (TREE_CODE (op1) == INTEGER_CST
2079 && (TREE_INT_CST_LOW (op1) != -1
2080 || TREE_INT_CST_HIGH (op1) != -1)));
2085 case TRUTH_ANDIF_EXPR:
2086 case TRUTH_ORIF_EXPR:
2087 case TRUTH_AND_EXPR:
2089 case TRUTH_XOR_EXPR:
2090 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2091 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2092 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2093 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2095 /* Result of these operations is always an int,
2096 but that does not mean the operands should be
2097 converted to ints! */
2098 result_type = integer_type_node;
2099 op0 = truthvalue_conversion (op0);
2100 op1 = truthvalue_conversion (op1);
2105 /* Shift operations: result has same type as first operand;
2106 always convert second operand to int.
2107 Also set SHORT_SHIFT if shifting rightward. */
2110 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2112 if (TREE_CODE (op1) == INTEGER_CST)
2114 if (tree_int_cst_sgn (op1) < 0)
2115 warning ("right shift count is negative");
2118 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2120 if (TREE_INT_CST_HIGH (op1) != 0
2121 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2122 >= TYPE_PRECISION (type0)))
2123 warning ("right shift count >= width of type");
2126 /* Use the type of the value to be shifted.
2127 This is what most traditional C compilers do. */
2128 result_type = type0;
2129 /* Unless traditional, convert the shift-count to an integer,
2130 regardless of size of value being shifted. */
2131 if (! flag_traditional)
2133 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2134 op1 = convert (integer_type_node, op1);
2135 /* Avoid converting op1 to result_type later. */
2142 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2144 if (TREE_CODE (op1) == INTEGER_CST)
2146 if (tree_int_cst_sgn (op1) < 0)
2147 warning ("left shift count is negative");
2148 else if (TREE_INT_CST_HIGH (op1) != 0
2149 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2150 >= TYPE_PRECISION (type0)))
2151 warning ("left shift count >= width of type");
2153 /* Use the type of the value to be shifted.
2154 This is what most traditional C compilers do. */
2155 result_type = type0;
2156 /* Unless traditional, convert the shift-count to an integer,
2157 regardless of size of value being shifted. */
2158 if (! flag_traditional)
2160 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2161 op1 = convert (integer_type_node, op1);
2162 /* Avoid converting op1 to result_type later. */
2170 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2172 if (TREE_CODE (op1) == INTEGER_CST)
2174 if (tree_int_cst_sgn (op1) < 0)
2175 warning ("shift count is negative");
2176 else if (TREE_INT_CST_HIGH (op1) != 0
2177 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2178 >= TYPE_PRECISION (type0)))
2179 warning ("shift count >= width of type");
2181 /* Use the type of the value to be shifted.
2182 This is what most traditional C compilers do. */
2183 result_type = type0;
2184 /* Unless traditional, convert the shift-count to an integer,
2185 regardless of size of value being shifted. */
2186 if (! flag_traditional)
2188 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2189 op1 = convert (integer_type_node, op1);
2190 /* Avoid converting op1 to result_type later. */
2198 /* Result of comparison is always int,
2199 but don't convert the args to int! */
2200 build_type = integer_type_node;
2201 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2202 || code0 == COMPLEX_TYPE)
2203 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2204 || code1 == COMPLEX_TYPE))
2206 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2208 register tree tt0 = TREE_TYPE (type0);
2209 register tree tt1 = TREE_TYPE (type1);
2210 /* Anything compares with void *. void * compares with anything.
2211 Otherwise, the targets must be compatible
2212 and both must be object or both incomplete. */
2213 if (comp_target_types (type0, type1))
2214 result_type = common_type (type0, type1);
2215 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2217 /* op0 != orig_op0 detects the case of something
2218 whose value is 0 but which isn't a valid null ptr const. */
2219 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2220 && TREE_CODE (tt1) == FUNCTION_TYPE)
2221 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2223 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2225 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2226 && TREE_CODE (tt0) == FUNCTION_TYPE)
2227 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2230 pedwarn ("comparison of distinct pointer types lacks a cast");
2232 if (result_type == NULL_TREE)
2233 result_type = ptr_type_node;
2235 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2236 && integer_zerop (op1))
2237 result_type = type0;
2238 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2239 && integer_zerop (op0))
2240 result_type = type1;
2241 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2243 result_type = type0;
2244 if (! flag_traditional)
2245 pedwarn ("comparison between pointer and integer");
2247 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2249 result_type = type1;
2250 if (! flag_traditional)
2251 pedwarn ("comparison between pointer and integer");
2257 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2258 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2260 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2262 if (comp_target_types (type0, type1))
2264 result_type = common_type (type0, type1);
2266 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2267 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2271 result_type = ptr_type_node;
2272 pedwarn ("comparison of distinct pointer types lacks a cast");
2281 build_type = integer_type_node;
2282 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2283 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2285 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2287 if (comp_target_types (type0, type1))
2289 result_type = common_type (type0, type1);
2290 if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2291 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2292 pedwarn ("comparison of complete and incomplete pointers");
2294 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2295 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2299 result_type = ptr_type_node;
2300 pedwarn ("comparison of distinct pointer types lacks a cast");
2303 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2304 && integer_zerop (op1))
2306 result_type = type0;
2307 if (pedantic || extra_warnings)
2308 pedwarn ("ordered comparison of pointer with integer zero");
2310 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2311 && integer_zerop (op0))
2313 result_type = type1;
2315 pedwarn ("ordered comparison of pointer with integer zero");
2317 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2319 result_type = type0;
2320 if (! flag_traditional)
2321 pedwarn ("comparison between pointer and integer");
2323 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2325 result_type = type1;
2326 if (! flag_traditional)
2327 pedwarn ("comparison between pointer and integer");
2332 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2334 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2336 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2338 if (shorten || common || short_compare)
2339 result_type = common_type (type0, type1);
2341 /* For certain operations (which identify themselves by shorten != 0)
2342 if both args were extended from the same smaller type,
2343 do the arithmetic in that type and then extend.
2345 shorten !=0 and !=1 indicates a bitwise operation.
2346 For them, this optimization is safe only if
2347 both args are zero-extended or both are sign-extended.
2348 Otherwise, we might change the result.
2349 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2350 but calculated in (unsigned short) it would be (unsigned short)-1. */
2352 if (shorten && none_complex)
2354 int unsigned0, unsigned1;
2355 tree arg0 = get_narrower (op0, &unsigned0);
2356 tree arg1 = get_narrower (op1, &unsigned1);
2357 /* UNS is 1 if the operation to be done is an unsigned one. */
2358 int uns = TREE_UNSIGNED (result_type);
2361 final_type = result_type;
2363 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2364 but it *requires* conversion to FINAL_TYPE. */
2366 if ((TYPE_PRECISION (TREE_TYPE (op0))
2367 == TYPE_PRECISION (TREE_TYPE (arg0)))
2368 && TREE_TYPE (op0) != final_type)
2369 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2370 if ((TYPE_PRECISION (TREE_TYPE (op1))
2371 == TYPE_PRECISION (TREE_TYPE (arg1)))
2372 && TREE_TYPE (op1) != final_type)
2373 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2375 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2377 /* For bitwise operations, signedness of nominal type
2378 does not matter. Consider only how operands were extended. */
2382 /* Note that in all three cases below we refrain from optimizing
2383 an unsigned operation on sign-extended args.
2384 That would not be valid. */
2386 /* Both args variable: if both extended in same way
2387 from same width, do it in that width.
2388 Do it unsigned if args were zero-extended. */
2389 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2390 < TYPE_PRECISION (result_type))
2391 && (TYPE_PRECISION (TREE_TYPE (arg1))
2392 == TYPE_PRECISION (TREE_TYPE (arg0)))
2393 && unsigned0 == unsigned1
2394 && (unsigned0 || !uns))
2396 = signed_or_unsigned_type (unsigned0,
2397 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2398 else if (TREE_CODE (arg0) == INTEGER_CST
2399 && (unsigned1 || !uns)
2400 && (TYPE_PRECISION (TREE_TYPE (arg1))
2401 < TYPE_PRECISION (result_type))
2402 && (type = signed_or_unsigned_type (unsigned1,
2404 int_fits_type_p (arg0, type)))
2406 else if (TREE_CODE (arg1) == INTEGER_CST
2407 && (unsigned0 || !uns)
2408 && (TYPE_PRECISION (TREE_TYPE (arg0))
2409 < TYPE_PRECISION (result_type))
2410 && (type = signed_or_unsigned_type (unsigned0,
2412 int_fits_type_p (arg1, type)))
2416 /* Shifts can be shortened if shifting right. */
2421 tree arg0 = get_narrower (op0, &unsigned_arg);
2423 final_type = result_type;
2425 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2426 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2428 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2429 /* We can shorten only if the shift count is less than the
2430 number of bits in the smaller type size. */
2431 && TREE_INT_CST_HIGH (op1) == 0
2432 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
2433 /* If arg is sign-extended and then unsigned-shifted,
2434 we can simulate this with a signed shift in arg's type
2435 only if the extended result is at least twice as wide
2436 as the arg. Otherwise, the shift could use up all the
2437 ones made by sign-extension and bring in zeros.
2438 We can't optimize that case at all, but in most machines
2439 it never happens because available widths are 2**N. */
2440 && (!TREE_UNSIGNED (final_type)
2442 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2444 /* Do an unsigned shift if the operand was zero-extended. */
2446 = signed_or_unsigned_type (unsigned_arg,
2448 /* Convert value-to-be-shifted to that type. */
2449 if (TREE_TYPE (op0) != result_type)
2450 op0 = convert (result_type, op0);
2455 /* Comparison operations are shortened too but differently.
2456 They identify themselves by setting short_compare = 1. */
2460 /* Don't write &op0, etc., because that would prevent op0
2461 from being kept in a register.
2462 Instead, make copies of the our local variables and
2463 pass the copies by reference, then copy them back afterward. */
2464 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2465 enum tree_code xresultcode = resultcode;
2467 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2470 op0 = xop0, op1 = xop1;
2472 resultcode = xresultcode;
2476 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2477 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2479 int unsignedp0, unsignedp1;
2480 tree primop0 = get_narrower (op0, &unsignedp0);
2481 tree primop1 = get_narrower (op1, &unsignedp1);
2483 /* Avoid spurious warnings for comparison with enumerators. */
2487 STRIP_TYPE_NOPS (xop0);
2488 STRIP_TYPE_NOPS (xop1);
2490 /* Give warnings for comparisons between signed and unsigned
2491 quantities that may fail. */
2492 /* Do the checking based on the original operand trees, so that
2493 casts will be considered, but default promotions won't be. */
2495 /* Do not warn if the comparison is being done in a signed type,
2496 since the signed type will only be chosen if it can represent
2497 all the values of the unsigned type. */
2498 if (! TREE_UNSIGNED (result_type))
2500 /* Do not warn if both operands are unsigned. */
2501 else if (op0_signed == op1_signed)
2503 /* Do not warn if the signed quantity is an unsuffixed
2504 integer literal (or some static constant expression
2505 involving such literals) and it is non-negative. */
2506 else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST
2507 && tree_int_cst_sgn (xop0) >= 0)
2508 || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
2509 && tree_int_cst_sgn (xop1) >= 0))
2511 /* Do not warn if the comparison is an equality operation,
2512 the unsigned quantity is an integral constant and it does
2513 not use the most significant bit of result_type. */
2514 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
2515 && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST
2516 && int_fits_type_p (xop1, signed_type (result_type))
2517 || (op1_signed && TREE_CODE (xop0) == INTEGER_CST
2518 && int_fits_type_p (xop0, signed_type (result_type))))))
2521 warning ("comparison between signed and unsigned");
2523 /* Warn if two unsigned values are being compared in a size
2524 larger than their original size, and one (and only one) is the
2525 result of a `~' operator. This comparison will always fail.
2527 Also warn if one operand is a constant, and the constant
2528 does not have all bits set that are set in the ~ operand
2529 when it is extended. */
2531 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2532 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2534 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2535 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2538 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2541 if (TREE_CODE (primop0) == INTEGER_CST
2542 || TREE_CODE (primop1) == INTEGER_CST)
2545 long constant, mask;
2546 int unsignedp, bits;
2548 if (TREE_CODE (primop0) == INTEGER_CST)
2551 unsignedp = unsignedp1;
2552 constant = TREE_INT_CST_LOW (primop0);
2557 unsignedp = unsignedp0;
2558 constant = TREE_INT_CST_LOW (primop1);
2561 bits = TYPE_PRECISION (TREE_TYPE (primop));
2562 if (bits < TYPE_PRECISION (result_type)
2563 && bits < HOST_BITS_PER_LONG && unsignedp)
2565 mask = (~0L) << bits;
2566 if ((mask & constant) != mask)
2567 warning ("comparison of promoted ~unsigned with constant");
2570 else if (unsignedp0 && unsignedp1
2571 && (TYPE_PRECISION (TREE_TYPE (primop0))
2572 < TYPE_PRECISION (result_type))
2573 && (TYPE_PRECISION (TREE_TYPE (primop1))
2574 < TYPE_PRECISION (result_type)))
2575 warning ("comparison of promoted ~unsigned with unsigned");
2581 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2582 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2583 Then the expression will be built.
2584 It will be given type FINAL_TYPE if that is nonzero;
2585 otherwise, it will be given type RESULT_TYPE. */
2589 binary_op_error (code);
2590 return error_mark_node;
2595 if (TREE_TYPE (op0) != result_type)
2596 op0 = convert (result_type, op0);
2597 if (TREE_TYPE (op1) != result_type)
2598 op1 = convert (result_type, op1);
2601 if (build_type == NULL_TREE)
2602 build_type = result_type;
2605 register tree result = build (resultcode, build_type, op0, op1);
2606 register tree folded;
2608 folded = fold (result);
2609 if (folded == result)
2610 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2611 if (final_type != 0)
2612 return convert (final_type, folded);
2617 /* Return a tree for the sum or difference (RESULTCODE says which)
2618 of pointer PTROP and integer INTOP. */
2621 pointer_int_sum (resultcode, ptrop, intop)
2622 enum tree_code resultcode;
2623 register tree ptrop, intop;
2627 register tree result;
2628 register tree folded;
2630 /* The result is a pointer of the same type that is being added. */
2632 register tree result_type = TREE_TYPE (ptrop);
2634 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2636 if (pedantic || warn_pointer_arith)
2637 pedwarn ("pointer of type `void *' used in arithmetic");
2638 size_exp = integer_one_node;
2640 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2642 if (pedantic || warn_pointer_arith)
2643 pedwarn ("pointer to a function used in arithmetic");
2644 size_exp = integer_one_node;
2647 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2649 /* If what we are about to multiply by the size of the elements
2650 contains a constant term, apply distributive law
2651 and multiply that constant term separately.
2652 This helps produce common subexpressions. */
2654 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2655 && ! TREE_CONSTANT (intop)
2656 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2657 && TREE_CONSTANT (size_exp)
2658 /* If the constant comes from pointer subtraction,
2659 skip this optimization--it would cause an error. */
2660 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2661 /* If the constant is unsigned, and smaller than the pointer size,
2662 then we must skip this optimization. This is because it could cause
2663 an overflow error if the constant is negative but INTOP is not. */
2664 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2665 || (TYPE_PRECISION (TREE_TYPE (intop))
2666 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2668 enum tree_code subcode = resultcode;
2669 tree int_type = TREE_TYPE (intop);
2670 if (TREE_CODE (intop) == MINUS_EXPR)
2671 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2672 /* Convert both subexpression types to the type of intop,
2673 because weird cases involving pointer arithmetic
2674 can result in a sum or difference with different type args. */
2675 ptrop = build_binary_op (subcode, ptrop,
2676 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2677 intop = convert (int_type, TREE_OPERAND (intop, 0));
2680 /* Convert the integer argument to a type the same size as a pointer
2681 so the multiply won't overflow spuriously. */
2683 if (TYPE_PRECISION (TREE_TYPE (intop)) != POINTER_SIZE)
2684 intop = convert (type_for_size (POINTER_SIZE, 0), intop);
2686 /* Replace the integer argument with a suitable product by the object size.
2687 Do this multiplication as signed, then convert to the appropriate
2688 pointer type (actually unsigned integral). */
2690 intop = convert (result_type,
2691 build_binary_op (MULT_EXPR, intop,
2692 convert (TREE_TYPE (intop), size_exp), 1));
2694 /* Create the sum or difference. */
2696 result = build (resultcode, result_type, ptrop, intop);
2698 folded = fold (result);
2699 if (folded == result)
2700 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2704 /* Return a tree for the difference of pointers OP0 and OP1.
2705 The resulting tree has type int. */
2708 pointer_diff (op0, op1)
2709 register tree op0, op1;
2711 register tree result, folded;
2712 tree restype = ptrdiff_type_node;
2714 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2716 if (pedantic || warn_pointer_arith)
2718 if (TREE_CODE (target_type) == VOID_TYPE)
2719 pedwarn ("pointer of type `void *' used in subtraction");
2720 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2721 pedwarn ("pointer to a function used in subtraction");
2724 /* First do the subtraction as integers;
2725 then drop through to build the divide operator. */
2727 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2728 convert (restype, op1), 1);
2729 /* This generates an error if op1 is pointer to incomplete type. */
2730 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2731 error ("arithmetic on pointer to an incomplete type");
2733 /* This generates an error if op0 is pointer to incomplete type. */
2734 op1 = c_size_in_bytes (target_type);
2736 /* Divide by the size, in easiest possible way. */
2738 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2740 folded = fold (result);
2741 if (folded == result)
2742 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2746 /* Construct and perhaps optimize a tree representation
2747 for a unary operation. CODE, a tree_code, specifies the operation
2748 and XARG is the operand. NOCONVERT nonzero suppresses
2749 the default promotions (such as from short to int). */
2752 build_unary_op (code, xarg, noconvert)
2753 enum tree_code code;
2757 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2758 register tree arg = xarg;
2759 register tree argtype = 0;
2760 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2761 char *errstring = NULL;
2764 if (typecode == ERROR_MARK)
2765 return error_mark_node;
2766 if (typecode == ENUMERAL_TYPE)
2767 typecode = INTEGER_TYPE;
2772 /* This is used for unary plus, because a CONVERT_EXPR
2773 is enough to prevent anybody from looking inside for
2774 associativity, but won't generate any code. */
2775 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2776 || typecode == COMPLEX_TYPE))
2777 errstring = "wrong type argument to unary plus";
2778 else if (!noconvert)
2779 arg = default_conversion (arg);
2783 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2784 || typecode == COMPLEX_TYPE))
2785 errstring = "wrong type argument to unary minus";
2786 else if (!noconvert)
2787 arg = default_conversion (arg);
2791 if (typecode == COMPLEX_TYPE)
2795 arg = default_conversion (arg);
2797 else if (typecode != INTEGER_TYPE)
2798 errstring = "wrong type argument to bit-complement";
2799 else if (!noconvert)
2800 arg = default_conversion (arg);
2804 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2805 || typecode == COMPLEX_TYPE))
2806 errstring = "wrong type argument to abs";
2807 else if (!noconvert)
2808 arg = default_conversion (arg);
2812 /* Conjugating a real value is a no-op, but allow it anyway. */
2813 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2814 || typecode == COMPLEX_TYPE))
2815 errstring = "wrong type argument to conjugation";
2816 else if (!noconvert)
2817 arg = default_conversion (arg);
2820 case TRUTH_NOT_EXPR:
2821 if (typecode != INTEGER_TYPE
2822 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2823 && typecode != COMPLEX_TYPE
2824 /* These will convert to a pointer. */
2825 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2827 errstring = "wrong type argument to unary exclamation mark";
2830 arg = truthvalue_conversion (arg);
2831 return invert_truthvalue (arg);
2837 if (TREE_CODE (arg) == COMPLEX_CST)
2838 return TREE_REALPART (arg);
2839 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2840 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2845 if (TREE_CODE (arg) == COMPLEX_CST)
2846 return TREE_IMAGPART (arg);
2847 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2848 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2850 return convert (TREE_TYPE (arg), integer_zero_node);
2852 case PREINCREMENT_EXPR:
2853 case POSTINCREMENT_EXPR:
2854 case PREDECREMENT_EXPR:
2855 case POSTDECREMENT_EXPR:
2856 /* Handle complex lvalues (when permitted)
2857 by reduction to simpler cases. */
2859 val = unary_complex_lvalue (code, arg);
2863 /* Increment or decrement the real part of the value,
2864 and don't change the imaginary part. */
2865 if (typecode == COMPLEX_TYPE)
2869 arg = stabilize_reference (arg);
2870 real = build_unary_op (REALPART_EXPR, arg, 1);
2871 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2872 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2873 build_unary_op (code, real, 1), imag);
2876 /* Report invalid types. */
2878 if (typecode != POINTER_TYPE
2879 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2881 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2882 errstring ="wrong type argument to increment";
2884 errstring ="wrong type argument to decrement";
2890 tree result_type = TREE_TYPE (arg);
2892 arg = get_unwidened (arg, 0);
2893 argtype = TREE_TYPE (arg);
2895 /* Compute the increment. */
2897 if (typecode == POINTER_TYPE)
2899 /* If pointer target is an undefined struct,
2900 we just cannot know how to do the arithmetic. */
2901 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2902 error ("%s of pointer to unknown structure",
2903 ((code == PREINCREMENT_EXPR
2904 || code == POSTINCREMENT_EXPR)
2905 ? "increment" : "decrement"));
2906 else if ((pedantic || warn_pointer_arith)
2907 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2908 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2909 pedwarn ("wrong type argument to %s",
2910 ((code == PREINCREMENT_EXPR
2911 || code == POSTINCREMENT_EXPR)
2912 ? "increment" : "decrement"));
2913 inc = c_size_in_bytes (TREE_TYPE (result_type));
2916 inc = integer_one_node;
2918 inc = convert (argtype, inc);
2920 /* Handle incrementing a cast-expression. */
2923 switch (TREE_CODE (arg))
2928 case FIX_TRUNC_EXPR:
2929 case FIX_FLOOR_EXPR:
2930 case FIX_ROUND_EXPR:
2932 pedantic_lvalue_warning (CONVERT_EXPR);
2933 /* If the real type has the same machine representation
2934 as the type it is cast to, we can make better output
2935 by adding directly to the inside of the cast. */
2936 if ((TREE_CODE (TREE_TYPE (arg))
2937 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2938 && (TYPE_MODE (TREE_TYPE (arg))
2939 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2940 arg = TREE_OPERAND (arg, 0);
2943 tree incremented, modify, value;
2944 arg = stabilize_reference (arg);
2945 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2948 value = save_expr (arg);
2949 incremented = build (((code == PREINCREMENT_EXPR
2950 || code == POSTINCREMENT_EXPR)
2951 ? PLUS_EXPR : MINUS_EXPR),
2952 argtype, value, inc);
2953 TREE_SIDE_EFFECTS (incremented) = 1;
2954 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2955 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2956 TREE_USED (value) = 1;
2966 /* Complain about anything else that is not a true lvalue. */
2967 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2968 || code == POSTINCREMENT_EXPR)
2969 ? "increment" : "decrement")))
2970 return error_mark_node;
2972 /* Report a read-only lvalue. */
2973 if (TREE_READONLY (arg))
2974 readonly_warning (arg,
2975 ((code == PREINCREMENT_EXPR
2976 || code == POSTINCREMENT_EXPR)
2977 ? "increment" : "decrement"));
2979 val = build (code, TREE_TYPE (arg), arg, inc);
2980 TREE_SIDE_EFFECTS (val) = 1;
2981 val = convert (result_type, val);
2982 if (TREE_CODE (val) != code)
2983 TREE_NO_UNUSED_WARNING (val) = 1;
2988 /* Note that this operation never does default_conversion
2989 regardless of NOCONVERT. */
2991 /* Let &* cancel out to simplify resulting code. */
2992 if (TREE_CODE (arg) == INDIRECT_REF)
2994 /* Don't let this be an lvalue. */
2995 if (lvalue_p (TREE_OPERAND (arg, 0)))
2996 return non_lvalue (TREE_OPERAND (arg, 0));
2997 return TREE_OPERAND (arg, 0);
3000 /* For &x[y], return x+y */
3001 if (TREE_CODE (arg) == ARRAY_REF)
3003 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
3004 return error_mark_node;
3005 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3006 TREE_OPERAND (arg, 1), 1);
3009 /* Handle complex lvalues (when permitted)
3010 by reduction to simpler cases. */
3011 val = unary_complex_lvalue (code, arg);
3015 #if 0 /* Turned off because inconsistent;
3016 float f; *&(int)f = 3.4 stores in int format
3017 whereas (int)f = 3.4 stores in float format. */
3018 /* Address of a cast is just a cast of the address
3019 of the operand of the cast. */
3020 switch (TREE_CODE (arg))
3025 case FIX_TRUNC_EXPR:
3026 case FIX_FLOOR_EXPR:
3027 case FIX_ROUND_EXPR:
3030 pedwarn ("ANSI C forbids the address of a cast expression");
3031 return convert (build_pointer_type (TREE_TYPE (arg)),
3032 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3037 /* Allow the address of a constructor if all the elements
3039 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3041 /* Anything not already handled and not a true memory reference
3043 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
3044 return error_mark_node;
3046 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3047 argtype = TREE_TYPE (arg);
3048 /* If the lvalue is const or volatile,
3049 merge that into the type that the address will point to. */
3050 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
3051 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3053 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3054 argtype = c_build_type_variant (argtype,
3055 TREE_READONLY (arg),
3056 TREE_THIS_VOLATILE (arg));
3059 argtype = build_pointer_type (argtype);
3061 if (mark_addressable (arg) == 0)
3062 return error_mark_node;
3067 if (TREE_CODE (arg) == COMPONENT_REF)
3069 tree field = TREE_OPERAND (arg, 1);
3071 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3073 if (DECL_BIT_FIELD (field))
3075 error ("attempt to take address of bit-field structure member `%s'",
3076 IDENTIFIER_POINTER (DECL_NAME (field)));
3077 return error_mark_node;
3080 addr = convert (argtype, addr);
3082 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3085 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3086 size_int (BITS_PER_UNIT));
3087 int flag = TREE_CONSTANT (addr);
3088 addr = fold (build (PLUS_EXPR, argtype,
3089 addr, convert (argtype, offset)));
3090 TREE_CONSTANT (addr) = flag;
3094 addr = build1 (code, argtype, arg);
3096 /* Address of a static or external variable or
3097 file-scope function counts as a constant. */
3099 && ! (TREE_CODE (arg) == FUNCTION_DECL
3100 && DECL_CONTEXT (arg) != 0))
3101 TREE_CONSTANT (addr) = 1;
3109 argtype = TREE_TYPE (arg);
3110 return fold (build1 (code, argtype, arg));
3114 return error_mark_node;
3118 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3119 convert ARG with the same conversions in the same order
3120 and return the result. */
3123 convert_sequence (conversions, arg)
3127 switch (TREE_CODE (conversions))
3132 case FIX_TRUNC_EXPR:
3133 case FIX_FLOOR_EXPR:
3134 case FIX_ROUND_EXPR:
3136 return convert (TREE_TYPE (conversions),
3137 convert_sequence (TREE_OPERAND (conversions, 0),
3146 /* Return nonzero if REF is an lvalue valid for this language.
3147 Lvalues can be assigned, unless their type has TYPE_READONLY.
3148 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3154 register enum tree_code code = TREE_CODE (ref);
3161 return lvalue_p (TREE_OPERAND (ref, 0));
3172 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3173 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
3180 /* Return nonzero if REF is an lvalue valid for this language;
3181 otherwise, print an error message and return zero. */
3184 lvalue_or_else (ref, string)
3188 int win = lvalue_p (ref);
3190 error ("invalid lvalue in %s", string);
3194 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3195 for certain kinds of expressions which are not really lvalues
3196 but which we can accept as lvalues.
3198 If ARG is not a kind of expression we can handle, return zero. */
3201 unary_complex_lvalue (code, arg)
3202 enum tree_code code;
3205 /* Handle (a, b) used as an "lvalue". */
3206 if (TREE_CODE (arg) == COMPOUND_EXPR)
3208 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3209 pedantic_lvalue_warning (COMPOUND_EXPR);
3210 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3211 TREE_OPERAND (arg, 0), real_result);
3214 /* Handle (a ? b : c) used as an "lvalue". */
3215 if (TREE_CODE (arg) == COND_EXPR)
3217 pedantic_lvalue_warning (COND_EXPR);
3218 return (build_conditional_expr
3219 (TREE_OPERAND (arg, 0),
3220 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3221 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3227 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3228 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3231 pedantic_lvalue_warning (code)
3232 enum tree_code code;
3235 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3236 code == COND_EXPR ? "conditional"
3237 : code == COMPOUND_EXPR ? "compound" : "cast");
3240 /* Warn about storing in something that is `const'. */
3243 readonly_warning (arg, string)
3248 strcpy (buf, string);
3250 /* Forbid assignments to iterators. */
3251 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3253 strcat (buf, " of iterator `%s'");
3254 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3257 if (TREE_CODE (arg) == COMPONENT_REF)
3259 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3260 readonly_warning (TREE_OPERAND (arg, 0), string);
3263 strcat (buf, " of read-only member `%s'");
3264 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3267 else if (TREE_CODE (arg) == VAR_DECL)
3269 strcat (buf, " of read-only variable `%s'");
3270 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3274 pedwarn ("%s of read-only location", buf);
3278 /* Mark EXP saying that we need to be able to take the
3279 address of it; it should not be allocated in a register.
3280 Value is 1 if successful. */
3283 mark_addressable (exp)
3286 register tree x = exp;
3288 switch (TREE_CODE (x))
3295 x = TREE_OPERAND (x, 0);
3299 TREE_ADDRESSABLE (x) = 1;
3306 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3307 && DECL_NONLOCAL (x))
3309 if (TREE_PUBLIC (x))
3311 error ("global register variable `%s' used in nested function",
3312 IDENTIFIER_POINTER (DECL_NAME (x)));
3315 pedwarn ("register variable `%s' used in nested function",
3316 IDENTIFIER_POINTER (DECL_NAME (x)));
3318 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3320 if (TREE_PUBLIC (x))
3322 error ("address of global register variable `%s' requested",
3323 IDENTIFIER_POINTER (DECL_NAME (x)));
3327 /* If we are making this addressable due to its having
3328 volatile components, give a different error message. Also
3329 handle the case of an unnamed parameter by not trying
3330 to give the name. */
3332 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3334 error ("cannot put object with volatile field into register");
3338 pedwarn ("address of register variable `%s' requested",
3339 IDENTIFIER_POINTER (DECL_NAME (x)));
3341 put_var_into_stack (x);
3345 TREE_ADDRESSABLE (x) = 1;
3346 #if 0 /* poplevel deals with this now. */
3347 if (DECL_CONTEXT (x) == 0)
3348 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3356 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3359 build_conditional_expr (ifexp, op1, op2)
3360 tree ifexp, op1, op2;
3362 register tree type1;
3363 register tree type2;
3364 register enum tree_code code1;
3365 register enum tree_code code2;
3366 register tree result_type = NULL;
3367 tree orig_op1 = op1, orig_op2 = op2;
3369 /* If second operand is omitted, it is the same as the first one;
3370 make sure it is calculated only once. */
3374 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression");
3375 ifexp = op1 = save_expr (ifexp);
3378 ifexp = truthvalue_conversion (default_conversion (ifexp));
3380 #if 0 /* Produces wrong result if within sizeof. */
3381 /* Don't promote the operands separately if they promote
3382 the same way. Return the unpromoted type and let the combined
3383 value get promoted if necessary. */
3385 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3386 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3387 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3388 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3390 if (TREE_CODE (ifexp) == INTEGER_CST)
3391 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3393 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3397 /* Promote both alternatives. */
3399 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3400 op1 = default_conversion (op1);
3401 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3402 op2 = default_conversion (op2);
3404 if (TREE_CODE (ifexp) == ERROR_MARK
3405 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3406 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3407 return error_mark_node;
3409 type1 = TREE_TYPE (op1);
3410 code1 = TREE_CODE (type1);
3411 type2 = TREE_TYPE (op2);
3412 code2 = TREE_CODE (type2);
3414 /* Quickly detect the usual case where op1 and op2 have the same type
3416 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3419 result_type = type1;
3421 result_type = TYPE_MAIN_VARIANT (type1);
3423 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3424 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3426 result_type = common_type (type1, type2);
3428 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3430 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3431 pedwarn ("ANSI C forbids conditional expr with only one void side");
3432 result_type = void_type_node;
3434 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3436 if (comp_target_types (type1, type2))
3437 result_type = common_type (type1, type2);
3438 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3439 && TREE_CODE (orig_op1) != NOP_EXPR)
3440 result_type = qualify_type (type2, type1);
3441 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3442 && TREE_CODE (orig_op2) != NOP_EXPR)
3443 result_type = qualify_type (type1, type2);
3444 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3446 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3447 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3448 result_type = qualify_type (type1, type2);
3450 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3452 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3453 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3454 result_type = qualify_type (type2, type1);
3458 pedwarn ("pointer type mismatch in conditional expression");
3459 result_type = build_pointer_type (void_type_node);
3462 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3464 if (! integer_zerop (op2))
3465 pedwarn ("pointer/integer type mismatch in conditional expression");
3468 op2 = null_pointer_node;
3469 #if 0 /* The spec seems to say this is permitted. */
3470 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3471 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3474 result_type = type1;
3476 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3478 if (!integer_zerop (op1))
3479 pedwarn ("pointer/integer type mismatch in conditional expression");
3482 op1 = null_pointer_node;
3483 #if 0 /* The spec seems to say this is permitted. */
3484 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3485 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3488 result_type = type2;
3493 if (flag_cond_mismatch)
3494 result_type = void_type_node;
3497 error ("type mismatch in conditional expression");
3498 return error_mark_node;
3502 /* Merge const and volatile flags of the incoming types. */
3504 = build_type_variant (result_type,
3505 TREE_READONLY (op1) || TREE_READONLY (op2),
3506 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3508 if (result_type != TREE_TYPE (op1))
3509 op1 = convert_and_check (result_type, op1);
3510 if (result_type != TREE_TYPE (op2))
3511 op2 = convert_and_check (result_type, op2);
3514 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3516 result_type = TREE_TYPE (op1);
3517 if (TREE_CONSTANT (ifexp))
3518 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3520 if (TYPE_MODE (result_type) == BLKmode)
3522 register tree tempvar
3523 = build_decl (VAR_DECL, NULL_TREE, result_type);
3524 register tree xop1 = build_modify_expr (tempvar, op1);
3525 register tree xop2 = build_modify_expr (tempvar, op2);
3526 register tree result = fold (build (COND_EXPR, result_type,
3527 ifexp, xop1, xop2));
3529 layout_decl (tempvar, TYPE_ALIGN (result_type));
3530 /* No way to handle variable-sized objects here.
3531 I fear that the entire handling of BLKmode conditional exprs
3532 needs to be redone. */
3533 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3536 = assign_stack_local (DECL_MODE (tempvar),
3537 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3538 + BITS_PER_UNIT - 1)
3542 TREE_SIDE_EFFECTS (result)
3543 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3544 | TREE_SIDE_EFFECTS (op2);
3545 return build (COMPOUND_EXPR, result_type, result, tempvar);
3550 if (TREE_CODE (ifexp) == INTEGER_CST)
3551 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3553 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3556 /* Given a list of expressions, return a compound expression
3557 that performs them all and returns the value of the last of them. */
3560 build_compound_expr (list)
3563 return internal_build_compound_expr (list, TRUE);
3567 internal_build_compound_expr (list, first_p)
3573 if (TREE_CHAIN (list) == 0)
3575 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3576 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3578 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3579 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3580 list = TREE_OPERAND (list, 0);
3583 /* Don't let (0, 0) be null pointer constant. */
3584 if (!first_p && integer_zerop (TREE_VALUE (list)))
3585 return non_lvalue (TREE_VALUE (list));
3586 return TREE_VALUE (list);
3589 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3591 /* Convert arrays to pointers when there really is a comma operator. */
3592 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3593 TREE_VALUE (TREE_CHAIN (list))
3594 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3597 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3599 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3601 /* The left-hand operand of a comma expression is like an expression
3602 statement: with -W or -Wunused, we should warn if it doesn't have
3603 any side-effects, unless it was explicitly cast to (void). */
3604 if ((extra_warnings || warn_unused)
3605 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3606 && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
3607 warning ("left-hand operand of comma expression has no effect");
3609 /* When pedantic, a compound expression can be neither an lvalue
3610 nor an integer constant expression. */
3615 /* With -Wunused, we should also warn if the left-hand operand does have
3616 side-effects, but computes a value which is not used. For example, in
3617 `foo() + bar(), baz()' the result of the `+' operator is not used,
3618 so we should issue a warning. */
3619 else if (warn_unused)
3620 warn_if_unused_value (TREE_VALUE (list));
3622 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3625 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3628 build_c_cast (type, expr)
3632 register tree value = expr;
3634 if (type == error_mark_node || expr == error_mark_node)
3635 return error_mark_node;
3636 type = TYPE_MAIN_VARIANT (type);
3639 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3640 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3641 value = TREE_OPERAND (value, 0);
3644 if (TREE_CODE (type) == ARRAY_TYPE)
3646 error ("cast specifies array type");
3647 return error_mark_node;
3650 if (TREE_CODE (type) == FUNCTION_TYPE)
3652 error ("cast specifies function type");
3653 return error_mark_node;
3656 if (type == TREE_TYPE (value))
3660 if (TREE_CODE (type) == RECORD_TYPE
3661 || TREE_CODE (type) == UNION_TYPE)
3662 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3665 else if (TREE_CODE (type) == UNION_TYPE)
3668 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3669 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3670 value = default_conversion (value);
3672 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3673 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3674 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3683 pedwarn ("ANSI C forbids casts to union type");
3684 if (TYPE_NAME (type) != 0)
3686 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3687 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3689 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3693 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3694 build_tree_list (field, value)),
3696 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3699 error ("cast to union type from type not present in union");
3700 return error_mark_node;
3706 /* If casting to void, avoid the error that would come
3707 from default_conversion in the case of a non-lvalue array. */
3708 if (type == void_type_node)
3709 return build1 (CONVERT_EXPR, type, value);
3711 /* Convert functions and arrays to pointers,
3712 but don't convert any other types. */
3713 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3714 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3715 value = default_conversion (value);
3716 otype = TREE_TYPE (value);
3718 /* Optionally warn about potentially worrisome casts. */
3721 && TREE_CODE (type) == POINTER_TYPE
3722 && TREE_CODE (otype) == POINTER_TYPE)
3724 if (TYPE_VOLATILE (TREE_TYPE (otype))
3725 && ! TYPE_VOLATILE (TREE_TYPE (type)))
3726 pedwarn ("cast discards `volatile' from pointer target type");
3727 if (TYPE_READONLY (TREE_TYPE (otype))
3728 && ! TYPE_READONLY (TREE_TYPE (type)))
3729 pedwarn ("cast discards `const' from pointer target type");
3732 /* Warn about possible alignment problems. */
3733 if (STRICT_ALIGNMENT && warn_cast_align
3734 && TREE_CODE (type) == POINTER_TYPE
3735 && TREE_CODE (otype) == POINTER_TYPE
3736 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3737 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3738 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3739 warning ("cast increases required alignment of target type");
3741 if (TREE_CODE (type) == INTEGER_TYPE
3742 && TREE_CODE (otype) == POINTER_TYPE
3743 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3744 && !TREE_CONSTANT (value))
3745 warning ("cast from pointer to integer of different size");
3747 if (warn_bad_function_cast
3748 && TREE_CODE (value) == CALL_EXPR
3749 && TREE_CODE (type) != TREE_CODE (otype))
3750 warning ("cast does not match function type");
3752 if (TREE_CODE (type) == POINTER_TYPE
3753 && TREE_CODE (otype) == INTEGER_TYPE
3754 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3756 /* Don't warn about converting 0 to pointer,
3757 provided the 0 was explicit--not cast or made by folding. */
3758 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3760 /* Don't warn about converting any constant. */
3761 && !TREE_CONSTANT (value))
3762 warning ("cast to pointer from integer of different size");
3765 value = convert (type, value);
3767 /* Ignore any integer overflow caused by the cast. */
3768 if (TREE_CODE (value) == INTEGER_CST)
3770 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3771 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3775 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3776 if (pedantic && TREE_CODE (value) == INTEGER_CST
3777 && TREE_CODE (expr) == INTEGER_CST
3778 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3779 value = non_lvalue (value);
3781 /* If pedantic, don't let a cast be an lvalue. */
3782 if (value == expr && pedantic)
3783 value = non_lvalue (value);
3788 /* Build an assignment expression of lvalue LHS from value RHS.
3789 MODIFYCODE is the code for a binary operator that we use
3790 to combine the old value of LHS with RHS to get the new value.
3791 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3794 build_modify_expr (lhs, modifycode, rhs)
3796 enum tree_code modifycode;
3798 register tree result;
3800 tree lhstype = TREE_TYPE (lhs);
3801 tree olhstype = lhstype;
3803 /* Types that aren't fully specified cannot be used in assignments. */
3804 lhs = require_complete_type (lhs);
3806 /* Avoid duplicate error messages from operands that had errors. */
3807 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3808 return error_mark_node;
3810 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3811 /* Do not use STRIP_NOPS here. We do not want an enumerator
3812 whose value is 0 to count as a null pointer constant. */
3813 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3814 rhs = TREE_OPERAND (rhs, 0);
3818 /* Handle control structure constructs used as "lvalues". */
3820 switch (TREE_CODE (lhs))
3822 /* Handle (a, b) used as an "lvalue". */
3824 pedantic_lvalue_warning (COMPOUND_EXPR);
3825 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3827 if (TREE_CODE (newrhs) == ERROR_MARK)
3828 return error_mark_node;
3829 return build (COMPOUND_EXPR, lhstype,
3830 TREE_OPERAND (lhs, 0), newrhs);
3832 /* Handle (a ? b : c) used as an "lvalue". */
3834 pedantic_lvalue_warning (COND_EXPR);
3835 rhs = save_expr (rhs);
3837 /* Produce (a ? (b = rhs) : (c = rhs))
3838 except that the RHS goes through a save-expr
3839 so the code to compute it is only emitted once. */
3841 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3842 build_modify_expr (TREE_OPERAND (lhs, 1),
3844 build_modify_expr (TREE_OPERAND (lhs, 2),
3846 if (TREE_CODE (cond) == ERROR_MARK)
3848 /* Make sure the code to compute the rhs comes out
3849 before the split. */
3850 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3851 /* But cast it to void to avoid an "unused" error. */
3852 convert (void_type_node, rhs), cond);
3856 /* If a binary op has been requested, combine the old LHS value with the RHS
3857 producing the value we should actually store into the LHS. */
3859 if (modifycode != NOP_EXPR)
3861 lhs = stabilize_reference (lhs);
3862 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3865 /* Handle a cast used as an "lvalue".
3866 We have already performed any binary operator using the value as cast.
3867 Now convert the result to the cast type of the lhs,
3868 and then true type of the lhs and store it there;
3869 then convert result back to the cast type to be the value
3870 of the assignment. */
3872 switch (TREE_CODE (lhs))
3877 case FIX_TRUNC_EXPR:
3878 case FIX_FLOOR_EXPR:
3879 case FIX_ROUND_EXPR:
3881 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3882 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3883 newrhs = default_conversion (newrhs);
3885 tree inner_lhs = TREE_OPERAND (lhs, 0);
3887 result = build_modify_expr (inner_lhs, NOP_EXPR,
3888 convert (TREE_TYPE (inner_lhs),
3889 convert (lhstype, newrhs)));
3890 if (TREE_CODE (result) == ERROR_MARK)
3892 pedantic_lvalue_warning (CONVERT_EXPR);
3893 return convert (TREE_TYPE (lhs), result);
3897 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3898 Reject anything strange now. */
3900 if (!lvalue_or_else (lhs, "assignment"))
3901 return error_mark_node;
3903 /* Warn about storing in something that is `const'. */
3905 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3906 || ((TREE_CODE (lhstype) == RECORD_TYPE
3907 || TREE_CODE (lhstype) == UNION_TYPE)
3908 && C_TYPE_FIELDS_READONLY (lhstype)))
3909 readonly_warning (lhs, "assignment");
3911 /* If storing into a structure or union member,
3912 it has probably been given type `int'.
3913 Compute the type that would go with
3914 the actual amount of storage the member occupies. */
3916 if (TREE_CODE (lhs) == COMPONENT_REF
3917 && (TREE_CODE (lhstype) == INTEGER_TYPE
3918 || TREE_CODE (lhstype) == REAL_TYPE
3919 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3920 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3922 /* If storing in a field that is in actuality a short or narrower than one,
3923 we must store in the field in its actual type. */
3925 if (lhstype != TREE_TYPE (lhs))
3927 lhs = copy_node (lhs);
3928 TREE_TYPE (lhs) = lhstype;
3931 /* Convert new value to destination type. */
3933 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
3934 NULL_TREE, NULL_TREE, 0);
3935 if (TREE_CODE (newrhs) == ERROR_MARK)
3936 return error_mark_node;
3938 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3939 TREE_SIDE_EFFECTS (result) = 1;
3941 /* If we got the LHS in a different type for storing in,
3942 convert the result back to the nominal type of LHS
3943 so that the value we return always has the same type
3944 as the LHS argument. */
3946 if (olhstype == TREE_TYPE (result))
3948 return convert_for_assignment (olhstype, result, "assignment",
3949 NULL_TREE, NULL_TREE, 0);
3952 /* Convert value RHS to type TYPE as preparation for an assignment
3953 to an lvalue of type TYPE.
3954 The real work of conversion is done by `convert'.
3955 The purpose of this function is to generate error messages
3956 for assignments that are not allowed in C.
3957 ERRTYPE is a string to use in error messages:
3958 "assignment", "return", etc. If it is null, this is parameter passing
3959 for a function call (and different error messages are output). Otherwise,
3960 it may be a name stored in the spelling stack and interpreted by
3963 FUNNAME is the name of the function being called,
3964 as an IDENTIFIER_NODE, or null.
3965 PARMNUM is the number of the argument, for printing in error messages. */
3968 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3971 tree fundecl, funname;
3974 register enum tree_code codel = TREE_CODE (type);
3975 register tree rhstype;
3976 register enum tree_code coder;
3978 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3979 /* Do not use STRIP_NOPS here. We do not want an enumerator
3980 whose value is 0 to count as a null pointer constant. */
3981 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3982 rhs = TREE_OPERAND (rhs, 0);
3984 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3985 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3986 rhs = default_conversion (rhs);
3987 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3988 rhs = decl_constant_value (rhs);
3990 rhstype = TREE_TYPE (rhs);
3991 coder = TREE_CODE (rhstype);
3993 if (coder == ERROR_MARK)
3994 return error_mark_node;
3996 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3998 overflow_warning (rhs);
3999 /* Check for Objective-C protocols. This will issue a warning if
4000 there are protocol violations. No need to use the return value. */
4001 maybe_objc_comptypes (type, rhstype, 0);
4005 if (coder == VOID_TYPE)
4007 error ("void value not ignored as it ought to be");
4008 return error_mark_node;
4010 /* Arithmetic types all interconvert, and enum is treated like int. */
4011 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
4012 || codel == COMPLEX_TYPE)
4013 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
4014 || coder == COMPLEX_TYPE))
4015 return convert_and_check (type, rhs);
4017 /* Conversion to a union from its member types. */
4018 else if (codel == UNION_TYPE)
4022 for (memb_types = TYPE_FIELDS (type); memb_types;
4023 memb_types = TREE_CHAIN (memb_types))
4025 if (comptypes (TREE_TYPE (memb_types), TREE_TYPE (rhs)))
4028 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
4029 pedwarn ("ANSI C prohibits argument conversion to union type");
4030 return build1 (NOP_EXPR, type, rhs);
4033 else if (coder == POINTER_TYPE
4034 && TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE)
4036 tree memb_type = TREE_TYPE (memb_types);
4037 register tree ttl = TREE_TYPE (memb_type);
4038 register tree ttr = TREE_TYPE (rhstype);
4040 /* Any non-function converts to a [const][volatile] void *
4041 and vice versa; otherwise, targets must be the same.
4042 Meanwhile, the lhs target must have all the qualifiers of
4044 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4045 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4046 || comp_target_types (memb_type, rhstype))
4048 /* Const and volatile mean something different for function
4049 types, so the usual warnings are not appropriate. */
4050 if (TREE_CODE (ttr) != FUNCTION_TYPE
4051 || TREE_CODE (ttl) != FUNCTION_TYPE)
4053 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4054 warn_for_assignment ("%s discards `const' from pointer target type",
4055 get_spelling (errtype), funname,
4057 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4058 warn_for_assignment ("%s discards `volatile' from pointer target type",
4059 get_spelling (errtype), funname,
4064 /* Because const and volatile on functions are
4065 restrictions that say the function will not do
4066 certain things, it is okay to use a const or volatile
4067 function where an ordinary one is wanted, but not
4069 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4070 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4071 get_spelling (errtype), funname,
4073 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4074 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4075 get_spelling (errtype), funname,
4080 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
4081 pedwarn ("ANSI C prohibits argument conversion to union type");
4082 return build1 (NOP_EXPR, type, rhs);
4086 /* Can convert integer zero to any pointer type. */
4087 else if (TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE
4088 && (integer_zerop (rhs)
4089 || (TREE_CODE (rhs) == NOP_EXPR
4090 && integer_zerop (TREE_OPERAND (rhs, 0)))))
4091 return build1 (NOP_EXPR, type, null_pointer_node);
4095 /* Conversions among pointers */
4096 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
4098 register tree ttl = TREE_TYPE (type);
4099 register tree ttr = TREE_TYPE (rhstype);
4101 /* Any non-function converts to a [const][volatile] void *
4102 and vice versa; otherwise, targets must be the same.
4103 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4104 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4105 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4106 || comp_target_types (type, rhstype)
4107 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4108 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4111 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4112 && TREE_CODE (ttr) == FUNCTION_TYPE)
4114 (TYPE_MAIN_VARIANT (ttr) == void_type_node
4115 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4116 which are not ANSI null ptr constants. */
4117 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4118 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4119 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4120 get_spelling (errtype), funname, parmnum);
4121 /* Const and volatile mean something different for function types,
4122 so the usual warnings are not appropriate. */
4123 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4124 && TREE_CODE (ttl) != FUNCTION_TYPE)
4126 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4127 warn_for_assignment ("%s discards `const' from pointer target type",
4128 get_spelling (errtype), funname, parmnum);
4129 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4130 warn_for_assignment ("%s discards `volatile' from pointer target type",
4131 get_spelling (errtype), funname, parmnum);
4132 /* If this is not a case of ignoring a mismatch in signedness,
4134 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4135 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4136 || comp_target_types (type, rhstype))
4138 /* If there is a mismatch, do warn. */
4140 warn_for_assignment ("pointer targets in %s differ in signedness",
4141 get_spelling (errtype), funname, parmnum);
4143 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4144 && TREE_CODE (ttr) == FUNCTION_TYPE)
4146 /* Because const and volatile on functions are restrictions
4147 that say the function will not do certain things,
4148 it is okay to use a const or volatile function
4149 where an ordinary one is wanted, but not vice-versa. */
4150 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4151 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4152 get_spelling (errtype), funname, parmnum);
4153 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4154 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4155 get_spelling (errtype), funname, parmnum);
4159 warn_for_assignment ("%s from incompatible pointer type",
4160 get_spelling (errtype), funname, parmnum);
4161 return convert (type, rhs);
4163 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4165 /* An explicit constant 0 can convert to a pointer,
4166 or one that results from arithmetic, even including
4167 a cast to integer type. */
4168 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4170 ! (TREE_CODE (rhs) == NOP_EXPR
4171 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4172 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4173 && integer_zerop (TREE_OPERAND (rhs, 0))))
4175 warn_for_assignment ("%s makes pointer from integer without a cast",
4176 get_spelling (errtype), funname, parmnum);
4177 return convert (type, rhs);
4179 return null_pointer_node;
4181 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4183 warn_for_assignment ("%s makes integer from pointer without a cast",
4184 get_spelling (errtype), funname, parmnum);
4185 return convert (type, rhs);
4192 tree selector = maybe_building_objc_message_expr ();
4194 if (selector && parmnum > 2)
4195 error ("incompatible type for argument %d of `%s'",
4196 parmnum - 2, IDENTIFIER_POINTER (selector));
4198 error ("incompatible type for argument %d of `%s'",
4199 parmnum, IDENTIFIER_POINTER (funname));
4202 error ("incompatible type for argument %d of indirect function call",
4206 error ("incompatible types in %s", get_spelling (errtype));
4208 return error_mark_node;
4211 /* Print a warning using MSG.
4212 It gets OPNAME as its one parameter.
4213 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4214 FUNCTION and ARGNUM are handled specially if we are building an
4215 Objective-C selector. */
4218 warn_for_assignment (msg, opname, function, argnum)
4224 static char argstring[] = "passing arg %d of `%s'";
4225 static char argnofun[] = "passing arg %d";
4229 tree selector = maybe_building_objc_message_expr ();
4231 if (selector && argnum > 2)
4233 function = selector;
4238 /* Function name is known; supply it. */
4239 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4240 + sizeof (argstring) + 25 /*%d*/ + 1);
4241 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4245 /* Function name unknown (call through ptr); just give arg number. */
4246 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4247 sprintf (opname, argnofun, argnum);
4250 pedwarn (msg, opname);
4253 /* Return nonzero if VALUE is a valid constant-valued expression
4254 for use in initializing a static variable; one that can be an
4255 element of a "constant" initializer.
4257 Return null_pointer_node if the value is absolute;
4258 if it is relocatable, return the variable that determines the relocation.
4259 We assume that VALUE has been folded as much as possible;
4260 therefore, we do not need to check for such things as
4261 arithmetic-combinations of integers. */
4264 initializer_constant_valid_p (value, endtype)
4268 switch (TREE_CODE (value))
4271 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4272 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
4273 && TREE_CONSTANT (value))
4275 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
4278 return TREE_STATIC (value) ? null_pointer_node : 0;
4284 return null_pointer_node;
4287 return TREE_OPERAND (value, 0);
4289 case NON_LVALUE_EXPR:
4290 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4294 /* Allow conversions between pointer types. */
4295 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4296 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
4297 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4299 /* Allow conversions between real types. */
4300 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4301 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
4302 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4304 /* Allow length-preserving conversions between integer types. */
4305 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4306 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4307 && (TYPE_PRECISION (TREE_TYPE (value))
4308 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4309 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4311 /* Allow conversions between other integer types only if
4313 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4314 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4316 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4318 if (inner == null_pointer_node)
4319 return null_pointer_node;
4323 /* Allow (int) &foo provided int is as wide as a pointer. */
4324 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4325 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
4326 && (TYPE_PRECISION (TREE_TYPE (value))
4327 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4328 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4331 /* Likewise conversions from int to pointers. */
4332 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4333 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4334 && (TYPE_PRECISION (TREE_TYPE (value))
4335 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4336 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4339 /* Allow conversions to union types if the value inside is okay. */
4340 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4341 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4346 if (TREE_CODE (endtype) == INTEGER_TYPE
4347 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4350 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4352 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4354 /* If either term is absolute, use the other terms relocation. */
4355 if (valid0 == null_pointer_node)
4357 if (valid1 == null_pointer_node)
4363 if (TREE_CODE (endtype) == INTEGER_TYPE
4364 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4367 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4369 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4371 /* Win if second argument is absolute. */
4372 if (valid1 == null_pointer_node)
4374 /* Win if both arguments have the same relocation.
4375 Then the value is absolute. */
4376 if (valid0 == valid1)
4377 return null_pointer_node;
4385 /* If VALUE is a compound expr all of whose expressions are constant, then
4386 return its value. Otherwise, return error_mark_node.
4388 This is for handling COMPOUND_EXPRs as initializer elements
4389 which is allowed with a warning when -pedantic is specified. */
4392 valid_compound_expr_initializer (value, endtype)
4396 if (TREE_CODE (value) == COMPOUND_EXPR)
4398 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4400 return error_mark_node;
4401 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4404 else if (! TREE_CONSTANT (value)
4405 && ! initializer_constant_valid_p (value, endtype))
4406 return error_mark_node;
4411 /* Perform appropriate conversions on the initial value of a variable,
4412 store it in the declaration DECL,
4413 and print any error messages that are appropriate.
4414 If the init is invalid, store an ERROR_MARK. */
4417 store_init_value (decl, init)
4420 register tree value, type;
4422 /* If variable's type was invalidly declared, just ignore it. */
4424 type = TREE_TYPE (decl);
4425 if (TREE_CODE (type) == ERROR_MARK)
4428 /* Digest the specified initializer into an expression. */
4430 value = digest_init (type, init, TREE_STATIC (decl),
4431 TREE_STATIC (decl) || pedantic);
4433 /* Store the expression if valid; else report error. */
4436 /* Note that this is the only place we can detect the error
4437 in a case such as struct foo bar = (struct foo) { x, y };
4438 where there is one initial value which is a constructor expression. */
4439 if (value == error_mark_node)
4441 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4443 error ("initializer for static variable is not constant");
4444 value = error_mark_node;
4446 else if (TREE_STATIC (decl)
4447 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4449 error ("initializer for static variable uses complicated arithmetic");
4450 value = error_mark_node;
4454 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4456 if (! TREE_CONSTANT (value))
4457 pedwarn ("aggregate initializer is not constant");
4458 else if (! TREE_STATIC (value))
4459 pedwarn ("aggregate initializer uses complicated arithmetic");
4464 DECL_INITIAL (decl) = value;
4466 /* ANSI wants warnings about out-of-range constant initializers. */
4467 STRIP_TYPE_NOPS (value);
4468 constant_expression_warning (value);
4471 /* Methods for storing and printing names for error messages. */
4473 /* Implement a spelling stack that allows components of a name to be pushed
4474 and popped. Each element on the stack is this structure. */
4486 #define SPELLING_STRING 1
4487 #define SPELLING_MEMBER 2
4488 #define SPELLING_BOUNDS 3
4490 static struct spelling *spelling; /* Next stack element (unused). */
4491 static struct spelling *spelling_base; /* Spelling stack base. */
4492 static int spelling_size; /* Size of the spelling stack. */
4494 /* Macros to save and restore the spelling stack around push_... functions.
4495 Alternative to SAVE_SPELLING_STACK. */
4497 #define SPELLING_DEPTH() (spelling - spelling_base)
4498 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4500 /* Save and restore the spelling stack around arbitrary C code. */
4502 #define SAVE_SPELLING_DEPTH(code) \
4504 int __depth = SPELLING_DEPTH (); \
4506 RESTORE_SPELLING_DEPTH (__depth); \
4509 /* Push an element on the spelling stack with type KIND and assign VALUE
4512 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4514 int depth = SPELLING_DEPTH (); \
4516 if (depth >= spelling_size) \
4518 spelling_size += 10; \
4519 if (spelling_base == 0) \
4521 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4524 = (struct spelling *) xrealloc (spelling_base, \
4525 spelling_size * sizeof (struct spelling)); \
4526 RESTORE_SPELLING_DEPTH (depth); \
4529 spelling->kind = (KIND); \
4530 spelling->MEMBER = (VALUE); \
4534 /* Push STRING on the stack. Printed literally. */
4537 push_string (string)
4540 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4543 /* Push a member name on the stack. Printed as '.' STRING. */
4546 push_member_name (decl)
4551 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4552 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4555 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4558 push_array_bounds (bounds)
4561 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4564 /* Compute the maximum size in bytes of the printed spelling. */
4569 register int size = 0;
4570 register struct spelling *p;
4572 for (p = spelling_base; p < spelling; p++)
4574 if (p->kind == SPELLING_BOUNDS)
4577 size += strlen (p->u.s) + 1;
4583 /* Print the spelling to BUFFER and return it. */
4586 print_spelling (buffer)
4587 register char *buffer;
4589 register char *d = buffer;
4591 register struct spelling *p;
4593 for (p = spelling_base; p < spelling; p++)
4594 if (p->kind == SPELLING_BOUNDS)
4596 sprintf (d, "[%d]", p->u.i);
4601 if (p->kind == SPELLING_MEMBER)
4603 for (s = p->u.s; *d = *s++; d++)
4610 /* Provide a means to pass component names derived from the spelling stack. */
4612 char initialization_message;
4614 /* Interpret the spelling of the given ERRTYPE message. */
4617 get_spelling (errtype)
4620 static char *buffer;
4621 static int size = -1;
4623 if (errtype == &initialization_message)
4625 /* Avoid counting chars */
4626 static char message[] = "initialization of `%s'";
4627 register int needed = sizeof (message) + spelling_length () + 1;
4631 buffer = (char *) xmalloc (size = needed);
4633 buffer = (char *) xrealloc (buffer, size = needed);
4635 temp = (char *) alloca (needed);
4636 sprintf (buffer, message, print_spelling (temp));
4643 /* Issue an error message for a bad initializer component.
4644 FORMAT describes the message. OFWHAT is the name for the component.
4645 LOCAL is a format string for formatting the insertion of the name
4648 If OFWHAT is null, the component name is stored on the spelling stack.
4649 If the component name is a null string, then LOCAL is omitted entirely. */
4652 error_init (format, local, ofwhat)
4653 char *format, *local, *ofwhat;
4658 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4659 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4662 sprintf (buffer, local, ofwhat);
4666 error (format, buffer);
4669 /* Issue a pedantic warning for a bad initializer component.
4670 FORMAT describes the message. OFWHAT is the name for the component.
4671 LOCAL is a format string for formatting the insertion of the name
4674 If OFWHAT is null, the component name is stored on the spelling stack.
4675 If the component name is a null string, then LOCAL is omitted entirely. */
4678 pedwarn_init (format, local, ofwhat)
4679 char *format, *local, *ofwhat;
4684 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4685 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4688 sprintf (buffer, local, ofwhat);
4692 pedwarn (format, buffer);
4695 /* Issue a warning for a bad initializer component.
4696 FORMAT describes the message. OFWHAT is the name for the component.
4697 LOCAL is a format string for formatting the insertion of the name
4700 If OFWHAT is null, the component name is stored on the spelling stack.
4701 If the component name is a null string, then LOCAL is omitted entirely. */
4704 warning_init (format, local, ofwhat)
4705 char *format, *local, *ofwhat;
4710 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4711 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4714 sprintf (buffer, local, ofwhat);
4718 warning (format, buffer);
4721 /* Digest the parser output INIT as an initializer for type TYPE.
4722 Return a C expression of type TYPE to represent the initial value.
4724 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4725 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4726 applies only to elements of constructors. */
4729 digest_init (type, init, require_constant, constructor_constant)
4731 int require_constant, constructor_constant;
4733 enum tree_code code = TREE_CODE (type);
4734 tree inside_init = init;
4736 if (init == error_mark_node)
4739 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4740 /* Do not use STRIP_NOPS here. We do not want an enumerator
4741 whose value is 0 to count as a null pointer constant. */
4742 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4743 inside_init = TREE_OPERAND (init, 0);
4745 /* Initialization of an array of chars from a string constant
4746 optionally enclosed in braces. */
4748 if (code == ARRAY_TYPE)
4750 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4751 if ((typ1 == char_type_node
4752 || typ1 == signed_char_type_node
4753 || typ1 == unsigned_char_type_node
4754 || typ1 == unsigned_wchar_type_node
4755 || typ1 == signed_wchar_type_node)
4756 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4758 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4759 TYPE_MAIN_VARIANT (type)))
4762 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4764 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4766 error_init ("char-array%s initialized from wide string",
4768 return error_mark_node;
4770 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4772 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4774 error_init ("int-array%s initialized from non-wide string",
4776 return error_mark_node;
4779 TREE_TYPE (inside_init) = type;
4780 if (TYPE_DOMAIN (type) != 0
4781 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4783 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4784 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4785 /* Subtract 1 (or sizeof (wchar_t))
4786 because it's ok to ignore the terminating null char
4787 that is counted in the length of the constant. */
4788 if (size < TREE_STRING_LENGTH (inside_init)
4789 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4790 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4793 "initializer-string for array of chars%s is too long",
4800 /* Any type can be initialized
4801 from an expression of the same type, optionally with braces. */
4803 if (inside_init && TREE_TYPE (inside_init) != 0
4804 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4805 TYPE_MAIN_VARIANT (type))
4806 || (code == ARRAY_TYPE
4807 && comptypes (TREE_TYPE (inside_init), type))
4808 || (code == POINTER_TYPE
4809 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4810 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4811 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4812 TREE_TYPE (type)))))
4814 if (code == POINTER_TYPE
4815 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4816 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4817 inside_init = default_conversion (inside_init);
4818 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4819 && TREE_CODE (inside_init) != CONSTRUCTOR)
4821 error_init ("array%s initialized from non-constant array expression",
4823 return error_mark_node;
4826 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4827 inside_init = decl_constant_value (inside_init);
4829 /* Compound expressions can only occur here if -pedantic or
4830 -pedantic-errors is specified. In the later case, we always want
4831 an error. In the former case, we simply want a warning. */
4832 if (require_constant && pedantic
4833 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4836 = valid_compound_expr_initializer (inside_init,
4837 TREE_TYPE (inside_init));
4838 if (inside_init == error_mark_node)
4839 error_init ("initializer element%s is not constant",
4842 pedwarn_init ("initializer element%s is not constant",
4844 if (flag_pedantic_errors)
4845 inside_init = error_mark_node;
4847 else if (require_constant && ! TREE_CONSTANT (inside_init))
4849 error_init ("initializer element%s is not constant",
4851 inside_init = error_mark_node;
4853 else if (require_constant
4854 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4856 error_init ("initializer element%s is not computable at load time",
4858 inside_init = error_mark_node;
4864 /* Handle scalar types, including conversions. */
4866 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4867 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4869 /* Note that convert_for_assignment calls default_conversion
4870 for arrays and functions. We must not call it in the
4871 case where inside_init is a null pointer constant. */
4873 = convert_for_assignment (type, init, "initialization",
4874 NULL_TREE, NULL_TREE, 0);
4876 if (require_constant && ! TREE_CONSTANT (inside_init))
4878 error_init ("initializer element%s is not constant",
4880 inside_init = error_mark_node;
4882 else if (require_constant
4883 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4885 error_init ("initializer element%s is not computable at load time",
4887 inside_init = error_mark_node;
4893 /* Come here only for records and arrays. */
4895 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4897 error_init ("variable-sized object%s may not be initialized",
4899 return error_mark_node;
4902 /* Traditionally, you can write struct foo x = 0;
4903 and it initializes the first element of x to 0. */
4904 if (flag_traditional)
4906 tree top = 0, prev = 0, otype = type;
4907 while (TREE_CODE (type) == RECORD_TYPE
4908 || TREE_CODE (type) == ARRAY_TYPE
4909 || TREE_CODE (type) == QUAL_UNION_TYPE
4910 || TREE_CODE (type) == UNION_TYPE)
4912 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4916 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4918 if (TREE_CODE (type) == ARRAY_TYPE)
4919 type = TREE_TYPE (type);
4920 else if (TYPE_FIELDS (type))
4921 type = TREE_TYPE (TYPE_FIELDS (type));
4924 error_init ("invalid initializer%s", " for `%s'", NULL);
4925 return error_mark_node;
4931 TREE_OPERAND (prev, 1)
4932 = build_tree_list (NULL_TREE,
4933 digest_init (type, init, require_constant,
4934 constructor_constant));
4938 return error_mark_node;
4940 error_init ("invalid initializer%s", " for `%s'", NULL);
4941 return error_mark_node;
4944 /* Handle initializers that use braces. */
4946 /* Type of object we are accumulating a constructor for.
4947 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4948 static tree constructor_type;
4950 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4952 static tree constructor_fields;
4954 /* For an ARRAY_TYPE, this is the specified index
4955 at which to store the next element we get.
4956 This is a special INTEGER_CST node that we modify in place. */
4957 static tree constructor_index;
4959 /* For an ARRAY_TYPE, this is the end index of the range
4960 to initialize with the next element, or NULL in the ordinary case
4961 where the element is used just once. */
4962 static tree constructor_range_end;
4964 /* For an ARRAY_TYPE, this is the maximum index. */
4965 static tree constructor_max_index;
4967 /* For a RECORD_TYPE, this is the first field not yet written out. */
4968 static tree constructor_unfilled_fields;
4970 /* For an ARRAY_TYPE, this is the index of the first element
4971 not yet written out.
4972 This is a special INTEGER_CST node that we modify in place. */
4973 static tree constructor_unfilled_index;
4975 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4976 This is so we can generate gaps between fields, when appropriate.
4977 This is a special INTEGER_CST node that we modify in place. */
4978 static tree constructor_bit_index;
4980 /* If we are saving up the elements rather than allocating them,
4981 this is the list of elements so far (in reverse order,
4982 most recent first). */
4983 static tree constructor_elements;
4985 /* 1 if so far this constructor's elements are all compile-time constants. */
4986 static int constructor_constant;
4988 /* 1 if so far this constructor's elements are all valid address constants. */
4989 static int constructor_simple;
4991 /* 1 if this constructor is erroneous so far. */
4992 static int constructor_erroneous;
4994 /* 1 if have called defer_addressed_constants. */
4995 static int constructor_subconstants_deferred;
4997 /* List of pending elements at this constructor level.
4998 These are elements encountered out of order
4999 which belong at places we haven't reached yet in actually
5000 writing the output. */
5001 static tree constructor_pending_elts;
5003 /* The SPELLING_DEPTH of this constructor. */
5004 static int constructor_depth;
5006 /* 0 if implicitly pushing constructor levels is allowed. */
5007 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
5009 /* 1 if this constructor level was entered implicitly. */
5010 static int constructor_implicit;
5012 static int require_constant_value;
5013 static int require_constant_elements;
5015 /* 1 if it is ok to output this constructor as we read it.
5016 0 means must accumulate a CONSTRUCTOR expression. */
5017 static int constructor_incremental;
5019 /* DECL node for which an initializer is being read.
5020 0 means we are reading a constructor expression
5021 such as (struct foo) {...}. */
5022 static tree constructor_decl;
5024 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
5025 static char *constructor_asmspec;
5027 /* Nonzero if this is an initializer for a top-level decl. */
5028 static int constructor_top_level;
5030 /* When we finish reading a constructor expression
5031 (constructor_decl is 0), the CONSTRUCTOR goes here. */
5032 static tree constructor_result;
5034 /* This stack has a level for each implicit or explicit level of
5035 structuring in the initializer, including the outermost one. It
5036 saves the values of most of the variables above. */
5038 struct constructor_stack
5040 struct constructor_stack *next;
5046 tree unfilled_index;
5047 tree unfilled_fields;
5053 /* If nonzero, this value should replace the entire
5054 constructor at this level. */
5055 tree replacement_value;
5064 struct constructor_stack *constructor_stack;
5066 /* This stack records separate initializers that are nested.
5067 Nested initializers can't happen in ANSI C, but GNU C allows them
5068 in cases like { ... (struct foo) { ... } ... }. */
5070 struct initializer_stack
5072 struct initializer_stack *next;
5075 struct constructor_stack *constructor_stack;
5077 struct spelling *spelling;
5078 struct spelling *spelling_base;
5082 char require_constant_value;
5083 char require_constant_elements;
5087 struct initializer_stack *initializer_stack;
5089 /* Prepare to parse and output the initializer for variable DECL. */
5092 start_init (decl, asmspec_tree, top_level)
5098 struct initializer_stack *p
5099 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5103 asmspec = TREE_STRING_POINTER (asmspec_tree);
5105 p->decl = constructor_decl;
5106 p->asmspec = constructor_asmspec;
5107 p->incremental = constructor_incremental;
5108 p->require_constant_value = require_constant_value;
5109 p->require_constant_elements = require_constant_elements;
5110 p->constructor_stack = constructor_stack;
5111 p->elements = constructor_elements;
5112 p->spelling = spelling;
5113 p->spelling_base = spelling_base;
5114 p->spelling_size = spelling_size;
5115 p->deferred = constructor_subconstants_deferred;
5116 p->top_level = constructor_top_level;
5117 p->next = initializer_stack;
5118 initializer_stack = p;
5120 constructor_decl = decl;
5121 constructor_incremental = top_level;
5122 constructor_asmspec = asmspec;
5123 constructor_subconstants_deferred = 0;
5124 constructor_top_level = top_level;
5128 require_constant_value = TREE_STATIC (decl);
5129 require_constant_elements
5130 = ((TREE_STATIC (decl) || pedantic)
5131 /* For a scalar, you can always use any value to initialize,
5132 even within braces. */
5133 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5134 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5135 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5136 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5137 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5138 constructor_incremental |= TREE_STATIC (decl);
5142 require_constant_value = 0;
5143 require_constant_elements = 0;
5144 locus = "(anonymous)";
5147 constructor_stack = 0;
5149 missing_braces_mentioned = 0;
5153 RESTORE_SPELLING_DEPTH (0);
5156 push_string (locus);
5162 struct initializer_stack *p = initializer_stack;
5164 /* Output subconstants (string constants, usually)
5165 that were referenced within this initializer and saved up.
5166 Must do this if and only if we called defer_addressed_constants. */
5167 if (constructor_subconstants_deferred)
5168 output_deferred_addressed_constants ();
5170 /* Free the whole constructor stack of this initializer. */
5171 while (constructor_stack)
5173 struct constructor_stack *q = constructor_stack;
5174 constructor_stack = q->next;
5178 /* Pop back to the data of the outer initializer (if any). */
5179 constructor_decl = p->decl;
5180 constructor_asmspec = p->asmspec;
5181 constructor_incremental = p->incremental;
5182 require_constant_value = p->require_constant_value;
5183 require_constant_elements = p->require_constant_elements;
5184 constructor_stack = p->constructor_stack;
5185 constructor_elements = p->elements;
5186 spelling = p->spelling;
5187 spelling_base = p->spelling_base;
5188 spelling_size = p->spelling_size;
5189 constructor_subconstants_deferred = p->deferred;
5190 constructor_top_level = p->top_level;
5191 initializer_stack = p->next;
5195 /* Call here when we see the initializer is surrounded by braces.
5196 This is instead of a call to push_init_level;
5197 it is matched by a call to pop_init_level.
5199 TYPE is the type to initialize, for a constructor expression.
5200 For an initializer for a decl, TYPE is zero. */
5203 really_start_incremental_init (type)
5206 struct constructor_stack *p
5207 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5210 type = TREE_TYPE (constructor_decl);
5212 /* Turn off constructor_incremental if type is a struct with bitfields.
5213 Do this before the first push, so that the corrected value
5214 is available in finish_init. */
5215 check_init_type_bitfields (type);
5217 p->type = constructor_type;
5218 p->fields = constructor_fields;
5219 p->index = constructor_index;
5220 p->range_end = constructor_range_end;
5221 p->max_index = constructor_max_index;
5222 p->unfilled_index = constructor_unfilled_index;
5223 p->unfilled_fields = constructor_unfilled_fields;
5224 p->bit_index = constructor_bit_index;
5225 p->elements = constructor_elements;
5226 p->constant = constructor_constant;
5227 p->simple = constructor_simple;
5228 p->erroneous = constructor_erroneous;
5229 p->pending_elts = constructor_pending_elts;
5230 p->depth = constructor_depth;
5231 p->replacement_value = 0;
5233 p->incremental = constructor_incremental;
5236 constructor_stack = p;
5238 constructor_constant = 1;
5239 constructor_simple = 1;
5240 constructor_depth = SPELLING_DEPTH ();
5241 constructor_elements = 0;
5242 constructor_pending_elts = 0;
5243 constructor_type = type;
5245 if (TREE_CODE (constructor_type) == RECORD_TYPE
5246 || TREE_CODE (constructor_type) == UNION_TYPE)
5248 constructor_fields = TYPE_FIELDS (constructor_type);
5249 /* Skip any nameless bit fields at the beginning. */
5250 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5251 && DECL_NAME (constructor_fields) == 0)
5252 constructor_fields = TREE_CHAIN (constructor_fields);
5253 constructor_unfilled_fields = constructor_fields;
5254 constructor_bit_index = copy_node (integer_zero_node);
5256 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5258 constructor_range_end = 0;
5259 if (TYPE_DOMAIN (constructor_type))
5261 constructor_max_index
5262 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5264 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5267 constructor_index = copy_node (integer_zero_node);
5268 constructor_unfilled_index = copy_node (constructor_index);
5272 /* Handle the case of int x = {5}; */
5273 constructor_fields = constructor_type;
5274 constructor_unfilled_fields = constructor_type;
5277 if (constructor_incremental)
5279 int momentary = suspend_momentary ();
5280 push_obstacks_nochange ();
5281 if (TREE_PERMANENT (constructor_decl))
5282 end_temporary_allocation ();
5283 make_decl_rtl (constructor_decl, constructor_asmspec,
5284 constructor_top_level);
5285 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5287 resume_momentary (momentary);
5290 if (constructor_incremental)
5292 defer_addressed_constants ();
5293 constructor_subconstants_deferred = 1;
5297 /* Push down into a subobject, for initialization.
5298 If this is for an explicit set of braces, IMPLICIT is 0.
5299 If it is because the next element belongs at a lower level,
5303 push_init_level (implicit)
5306 struct constructor_stack *p;
5308 /* If we've exhausted any levels that didn't have braces,
5310 while (constructor_stack->implicit)
5312 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5313 || TREE_CODE (constructor_type) == UNION_TYPE)
5314 && constructor_fields == 0)
5315 process_init_element (pop_init_level (1));
5316 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5317 && tree_int_cst_lt (constructor_max_index, constructor_index))
5318 process_init_element (pop_init_level (1));
5323 /* Structure elements may require alignment. Do this now
5324 if necessary for the subaggregate. */
5325 if (constructor_incremental && constructor_type != 0
5326 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields)
5328 /* Advance to offset of this element. */
5329 if (! tree_int_cst_equal (constructor_bit_index,
5330 DECL_FIELD_BITPOS (constructor_fields)))
5332 int next = (TREE_INT_CST_LOW
5333 (DECL_FIELD_BITPOS (constructor_fields))
5335 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5338 assemble_zeros (next - here);
5342 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5343 p->type = constructor_type;
5344 p->fields = constructor_fields;
5345 p->index = constructor_index;
5346 p->range_end = constructor_range_end;
5347 p->max_index = constructor_max_index;
5348 p->unfilled_index = constructor_unfilled_index;
5349 p->unfilled_fields = constructor_unfilled_fields;
5350 p->bit_index = constructor_bit_index;
5351 p->elements = constructor_elements;
5352 p->constant = constructor_constant;
5353 p->simple = constructor_simple;
5354 p->erroneous = constructor_erroneous;
5355 p->pending_elts = constructor_pending_elts;
5356 p->depth = constructor_depth;
5357 p->replacement_value = 0;
5358 p->implicit = implicit;
5359 p->incremental = constructor_incremental;
5361 p->next = constructor_stack;
5362 constructor_stack = p;
5364 constructor_constant = 1;
5365 constructor_simple = 1;
5366 constructor_depth = SPELLING_DEPTH ();
5367 constructor_elements = 0;
5368 constructor_pending_elts = 0;
5370 /* Don't die if an entire brace-pair level is superfluous
5371 in the containing level. */
5372 if (constructor_type == 0)
5374 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5375 || TREE_CODE (constructor_type) == UNION_TYPE)
5377 /* Don't die if there are extra init elts at the end. */
5378 if (constructor_fields == 0)
5379 constructor_type = 0;
5382 constructor_type = TREE_TYPE (constructor_fields);
5383 push_member_name (constructor_fields);
5384 constructor_depth++;
5385 if (constructor_fields != constructor_unfilled_fields)
5386 constructor_incremental = 0;
5389 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5391 constructor_type = TREE_TYPE (constructor_type);
5392 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5393 constructor_depth++;
5394 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5395 || constructor_range_end != 0)
5396 constructor_incremental = 0;
5399 if (constructor_type == 0)
5401 error_init ("extra brace group at end of initializer%s",
5403 constructor_fields = 0;
5404 constructor_unfilled_fields = 0;
5408 /* Turn off constructor_incremental if type is a struct with bitfields. */
5409 check_init_type_bitfields (constructor_type);
5411 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5413 missing_braces_mentioned = 1;
5414 warning_init ("missing braces around initializer%s", " for `%s'", NULL);
5417 if (TREE_CODE (constructor_type) == RECORD_TYPE
5418 || TREE_CODE (constructor_type) == UNION_TYPE)
5420 constructor_fields = TYPE_FIELDS (constructor_type);
5421 /* Skip any nameless bit fields at the beginning. */
5422 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5423 && DECL_NAME (constructor_fields) == 0)
5424 constructor_fields = TREE_CHAIN (constructor_fields);
5425 constructor_unfilled_fields = constructor_fields;
5426 constructor_bit_index = copy_node (integer_zero_node);
5428 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5430 constructor_range_end = 0;
5431 if (TYPE_DOMAIN (constructor_type))
5433 constructor_max_index
5434 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5436 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5439 constructor_index = copy_node (integer_zero_node);
5440 constructor_unfilled_index = copy_node (constructor_index);
5444 warning_init ("braces around scalar initializer%s", " for `%s'", NULL);
5445 constructor_fields = constructor_type;
5446 constructor_unfilled_fields = constructor_type;
5450 /* Don't read a struct incrementally if it has any bitfields,
5451 because the incremental reading code doesn't know how to
5452 handle bitfields yet. */
5455 check_init_type_bitfields (type)
5458 if (TREE_CODE (type) == RECORD_TYPE)
5461 for (tail = TYPE_FIELDS (type); tail;
5462 tail = TREE_CHAIN (tail))
5464 if (DECL_BIT_FIELD (tail)
5465 /* This catches cases like `int foo : 8;'. */
5466 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail)))
5468 constructor_incremental = 0;
5472 check_init_type_bitfields (TREE_TYPE (tail));
5476 else if (TREE_CODE (type) == ARRAY_TYPE)
5477 check_init_type_bitfields (TREE_TYPE (type));
5480 /* At the end of an implicit or explicit brace level,
5481 finish up that level of constructor.
5482 If we were outputting the elements as they are read, return 0
5483 from inner levels (process_init_element ignores that),
5484 but return error_mark_node from the outermost level
5485 (that's what we want to put in DECL_INITIAL).
5486 Otherwise, return a CONSTRUCTOR expression. */
5489 pop_init_level (implicit)
5492 struct constructor_stack *p;
5494 tree constructor = 0;
5498 /* When we come to an explicit close brace,
5499 pop any inner levels that didn't have explicit braces. */
5500 while (constructor_stack->implicit)
5501 process_init_element (pop_init_level (1));
5504 p = constructor_stack;
5506 if (constructor_type != 0)
5507 size = int_size_in_bytes (constructor_type);
5509 /* Now output all pending elements. */
5510 output_pending_init_elements (1);
5512 #if 0 /* c-parse.in warns about {}. */
5513 /* In ANSI, each brace level must have at least one element. */
5514 if (! implicit && pedantic
5515 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5516 ? integer_zerop (constructor_unfilled_index)
5517 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5518 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL);
5521 /* Pad out the end of the structure. */
5523 if (p->replacement_value)
5525 /* If this closes a superfluous brace pair,
5526 just pass out the element between them. */
5527 constructor = p->replacement_value;
5528 /* If this is the top level thing within the initializer,
5529 and it's for a variable, then since we already called
5530 assemble_variable, we must output the value now. */
5531 if (p->next == 0 && constructor_decl != 0
5532 && constructor_incremental)
5534 constructor = digest_init (constructor_type, constructor,
5535 require_constant_value,
5536 require_constant_elements);
5538 /* If initializing an array of unknown size,
5539 determine the size now. */
5540 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5541 && TYPE_DOMAIN (constructor_type) == 0)
5546 push_obstacks_nochange ();
5547 if (TREE_PERMANENT (constructor_type))
5548 end_temporary_allocation ();
5550 momentary_p = suspend_momentary ();
5552 /* We shouldn't have an incomplete array type within
5554 if (constructor_stack->next)
5558 = complete_array_type (constructor_type,
5563 size = int_size_in_bytes (constructor_type);
5564 resume_momentary (momentary_p);
5568 output_constant (constructor, size);
5571 else if (constructor_type == 0)
5573 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5574 && TREE_CODE (constructor_type) != UNION_TYPE
5575 && TREE_CODE (constructor_type) != ARRAY_TYPE
5576 && ! constructor_incremental)
5578 /* A nonincremental scalar initializer--just return
5579 the element, after verifying there is just one. */
5580 if (constructor_elements == 0)
5582 error_init ("empty scalar initializer%s",
5584 constructor = error_mark_node;
5586 else if (TREE_CHAIN (constructor_elements) != 0)
5588 error_init ("extra elements in scalar initializer%s",
5590 constructor = TREE_VALUE (constructor_elements);
5593 constructor = TREE_VALUE (constructor_elements);
5595 else if (! constructor_incremental)
5597 if (constructor_erroneous)
5598 constructor = error_mark_node;
5601 int momentary = suspend_momentary ();
5603 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5604 nreverse (constructor_elements));
5605 if (constructor_constant)
5606 TREE_CONSTANT (constructor) = 1;
5607 if (constructor_constant && constructor_simple)
5608 TREE_STATIC (constructor) = 1;
5610 resume_momentary (momentary);
5616 int momentary = suspend_momentary ();
5618 if (TREE_CODE (constructor_type) == RECORD_TYPE
5619 || TREE_CODE (constructor_type) == UNION_TYPE)
5621 /* Find the offset of the end of that field. */
5622 filled = size_binop (CEIL_DIV_EXPR,
5623 constructor_bit_index,
5624 size_int (BITS_PER_UNIT));
5626 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5628 /* If initializing an array of unknown size,
5629 determine the size now. */
5630 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5631 && TYPE_DOMAIN (constructor_type) == 0)
5634 = size_binop (MINUS_EXPR,
5635 constructor_unfilled_index,
5638 push_obstacks_nochange ();
5639 if (TREE_PERMANENT (constructor_type))
5640 end_temporary_allocation ();
5641 maxindex = copy_node (maxindex);
5642 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5643 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5645 /* TYPE_MAX_VALUE is always one less than the number of elements
5646 in the array, because we start counting at zero. Therefore,
5647 warn only if the value is less than zero. */
5649 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5651 error_with_decl (constructor_decl,
5652 "zero or negative array size `%s'");
5653 layout_type (constructor_type);
5654 size = int_size_in_bytes (constructor_type);
5658 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5659 size_in_bytes (TREE_TYPE (constructor_type)));
5665 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5667 resume_momentary (momentary);
5671 constructor_type = p->type;
5672 constructor_fields = p->fields;
5673 constructor_index = p->index;
5674 constructor_range_end = p->range_end;
5675 constructor_max_index = p->max_index;
5676 constructor_unfilled_index = p->unfilled_index;
5677 constructor_unfilled_fields = p->unfilled_fields;
5678 constructor_bit_index = p->bit_index;
5679 constructor_elements = p->elements;
5680 constructor_constant = p->constant;
5681 constructor_simple = p->simple;
5682 constructor_erroneous = p->erroneous;
5683 constructor_pending_elts = p->pending_elts;
5684 constructor_depth = p->depth;
5685 constructor_incremental = p->incremental;
5686 RESTORE_SPELLING_DEPTH (constructor_depth);
5688 constructor_stack = p->next;
5691 if (constructor == 0)
5693 if (constructor_stack == 0)
5694 return error_mark_node;
5700 /* Within an array initializer, specify the next index to be initialized.
5701 FIRST is that index. If LAST is nonzero, then initialize a range
5702 of indices, running from FIRST through LAST. */
5705 set_init_index (first, last)
5708 while ((TREE_CODE (first) == NOP_EXPR
5709 || TREE_CODE (first) == CONVERT_EXPR
5710 || TREE_CODE (first) == NON_LVALUE_EXPR)
5711 && (TYPE_MODE (TREE_TYPE (first))
5712 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5713 (first) = TREE_OPERAND (first, 0);
5715 while ((TREE_CODE (last) == NOP_EXPR
5716 || TREE_CODE (last) == CONVERT_EXPR
5717 || TREE_CODE (last) == NON_LVALUE_EXPR)
5718 && (TYPE_MODE (TREE_TYPE (last))
5719 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5720 (last) = TREE_OPERAND (last, 0);
5722 if (TREE_CODE (first) != INTEGER_CST)
5723 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5724 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5725 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5726 else if (! constructor_unfilled_index)
5727 error_init ("array index in non-array initializer%s", " for `%s'", NULL);
5728 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5729 error_init ("duplicate array index in initializer%s", " for `%s'", NULL);
5732 TREE_INT_CST_LOW (constructor_index)
5733 = TREE_INT_CST_LOW (first);
5734 TREE_INT_CST_HIGH (constructor_index)
5735 = TREE_INT_CST_HIGH (first);
5737 if (last != 0 && tree_int_cst_lt (last, first))
5738 error_init ("empty index range in initializer%s", " for `%s'", NULL);
5742 pedwarn ("ANSI C forbids specifying element to initialize");
5743 constructor_range_end = last;
5748 /* Within a struct initializer, specify the next field to be initialized. */
5751 set_init_label (fieldname)
5757 /* Don't die if an entire brace-pair level is superfluous
5758 in the containing level. */
5759 if (constructor_type == 0)
5762 for (tail = TYPE_FIELDS (constructor_type); tail;
5763 tail = TREE_CHAIN (tail))
5765 if (tail == constructor_unfilled_fields)
5767 if (DECL_NAME (tail) == fieldname)
5772 error ("unknown field `%s' specified in initializer",
5773 IDENTIFIER_POINTER (fieldname));
5775 error ("field `%s' already initialized",
5776 IDENTIFIER_POINTER (fieldname));
5779 constructor_fields = tail;
5781 pedwarn ("ANSI C forbids specifying structure member to initialize");
5785 /* "Output" the next constructor element.
5786 At top level, really output it to assembler code now.
5787 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5788 TYPE is the data type that the containing data type wants here.
5789 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5791 PENDING if non-nil means output pending elements that belong
5792 right after this element. (PENDING is normally 1;
5793 it is 0 while outputting pending elements, to avoid recursion.) */
5796 output_init_element (value, type, field, pending)
5797 tree value, type, field;
5802 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5803 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5804 && !(TREE_CODE (value) == STRING_CST
5805 && TREE_CODE (type) == ARRAY_TYPE
5806 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5807 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5808 TYPE_MAIN_VARIANT (type))))
5809 value = default_conversion (value);
5811 if (value == error_mark_node)
5812 constructor_erroneous = 1;
5813 else if (!TREE_CONSTANT (value))
5814 constructor_constant = 0;
5815 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5816 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5817 || TREE_CODE (constructor_type) == UNION_TYPE)
5818 && DECL_BIT_FIELD (field) && TREE_CODE (value) != INTEGER_CST))
5819 constructor_simple = 0;
5821 if (require_constant_value && ! TREE_CONSTANT (value))
5823 error_init ("initializer element%s is not constant",
5825 value = error_mark_node;
5827 else if (require_constant_elements
5828 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5830 error_init ("initializer element%s is not computable at load time",
5832 value = error_mark_node;
5835 /* If this element duplicates one on constructor_pending_elts,
5836 print a message and ignore it. Don't do this when we're
5837 processing elements taken off constructor_pending_elts,
5838 because we'd always get spurious errors. */
5841 if (TREE_CODE (constructor_type) == RECORD_TYPE
5842 || TREE_CODE (constructor_type) == UNION_TYPE)
5844 if (purpose_member (field, constructor_pending_elts))
5846 error_init ("duplicate initializer%s", " for `%s'", NULL);
5850 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5853 for (tail = constructor_pending_elts; tail;
5854 tail = TREE_CHAIN (tail))
5855 if (TREE_PURPOSE (tail) != 0
5856 && TREE_CODE (TREE_PURPOSE (tail)) == INTEGER_CST
5857 && tree_int_cst_equal (TREE_PURPOSE (tail), constructor_index))
5862 error_init ("duplicate initializer%s", " for `%s'", NULL);
5868 /* If this element doesn't come next in sequence,
5869 put it on constructor_pending_elts. */
5870 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5871 && !tree_int_cst_equal (field, constructor_unfilled_index))
5874 /* The copy_node is needed in case field is actually
5875 constructor_index, which is modified in place. */
5876 constructor_pending_elts
5877 = tree_cons (copy_node (field),
5878 digest_init (type, value, require_constant_value,
5879 require_constant_elements),
5880 constructor_pending_elts);
5882 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5883 && field != constructor_unfilled_fields)
5885 /* We do this for records but not for unions. In a union,
5886 no matter which field is specified, it can be initialized
5887 right away since it starts at the beginning of the union. */
5889 constructor_pending_elts
5891 digest_init (type, value, require_constant_value,
5892 require_constant_elements),
5893 constructor_pending_elts);
5897 /* Otherwise, output this element either to
5898 constructor_elements or to the assembler file. */
5902 if (! constructor_incremental)
5904 if (field && TREE_CODE (field) == INTEGER_CST)
5905 field = copy_node (field);
5906 constructor_elements
5907 = tree_cons (field, digest_init (type, value,
5908 require_constant_value,
5909 require_constant_elements),
5910 constructor_elements);
5914 /* Structure elements may require alignment.
5915 Do this, if necessary. */
5916 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5918 /* Advance to offset of this element. */
5919 if (! tree_int_cst_equal (constructor_bit_index,
5920 DECL_FIELD_BITPOS (field)))
5922 int next = (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
5924 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5927 assemble_zeros (next - here);
5930 output_constant (digest_init (type, value,
5931 require_constant_value,
5932 require_constant_elements),
5933 int_size_in_bytes (type));
5935 /* For a record or union,
5936 keep track of end position of last field. */
5937 if (TREE_CODE (constructor_type) == RECORD_TYPE
5938 || TREE_CODE (constructor_type) == UNION_TYPE)
5940 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
5942 TREE_INT_CST_LOW (constructor_bit_index)
5943 = TREE_INT_CST_LOW (temp);
5944 TREE_INT_CST_HIGH (constructor_bit_index)
5945 = TREE_INT_CST_HIGH (temp);
5950 /* Advance the variable that indicates sequential elements output. */
5951 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5953 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
5955 TREE_INT_CST_LOW (constructor_unfilled_index)
5956 = TREE_INT_CST_LOW (tem);
5957 TREE_INT_CST_HIGH (constructor_unfilled_index)
5958 = TREE_INT_CST_HIGH (tem);
5960 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5961 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5962 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5963 constructor_unfilled_fields = 0;
5965 /* Now output any pending elements which have become next. */
5967 output_pending_init_elements (0);
5971 /* Output any pending elements which have become next.
5972 As we output elements, constructor_unfilled_{fields,index}
5973 advances, which may cause other elements to become next;
5974 if so, they too are output.
5976 If ALL is 0, we return when there are
5977 no more pending elements to output now.
5979 If ALL is 1, we output space as necessary so that
5980 we can output all the pending elements. */
5983 output_pending_init_elements (all)
5991 /* Look thru the whole pending list.
5992 If we find an element that should be output now,
5993 output it. Otherwise, set NEXT to the element
5994 that comes first among those still pending. */
5997 for (tail = constructor_pending_elts; tail;
5998 tail = TREE_CHAIN (tail))
6000 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6002 if (tree_int_cst_equal (TREE_PURPOSE (tail),
6003 constructor_unfilled_index))
6005 output_init_element (TREE_VALUE (tail),
6006 TREE_TYPE (constructor_type),
6007 constructor_unfilled_index, 0);
6010 else if (tree_int_cst_lt (TREE_PURPOSE (tail),
6011 constructor_unfilled_index))
6014 || tree_int_cst_lt (TREE_PURPOSE (tail), next))
6015 next = TREE_PURPOSE (tail);
6017 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6018 || TREE_CODE (constructor_type) == UNION_TYPE)
6020 if (TREE_PURPOSE (tail) == constructor_unfilled_fields)
6022 output_init_element (TREE_VALUE (tail),
6023 TREE_TYPE (constructor_unfilled_fields),
6024 constructor_unfilled_fields,
6028 else if (constructor_unfilled_fields == 0
6029 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
6030 DECL_FIELD_BITPOS (constructor_unfilled_fields)))
6033 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
6034 DECL_FIELD_BITPOS (next)))
6035 next = TREE_PURPOSE (tail);
6039 /* Ordinarily return, but not if we want to output all
6040 and there are elements left. */
6041 if (! (all && next != 0))
6044 /* Generate space up to the position of NEXT. */
6045 if (constructor_incremental)
6048 tree nextpos_tree = size_int (0);
6050 if (TREE_CODE (constructor_type) == RECORD_TYPE
6051 || TREE_CODE (constructor_type) == UNION_TYPE)
6053 /* Find the last field written out, if any. */
6054 for (tail = TYPE_FIELDS (constructor_type); tail;
6055 tail = TREE_CHAIN (tail))
6056 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
6060 /* Find the offset of the end of that field. */
6061 filled = size_binop (CEIL_DIV_EXPR,
6062 size_binop (PLUS_EXPR,
6063 DECL_FIELD_BITPOS (tail),
6065 size_int (BITS_PER_UNIT));
6067 filled = size_int (0);
6069 nextpos_tree = size_binop (CEIL_DIV_EXPR,
6070 DECL_FIELD_BITPOS (next),
6071 size_int (BITS_PER_UNIT));
6073 TREE_INT_CST_HIGH (constructor_bit_index)
6074 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
6075 TREE_INT_CST_LOW (constructor_bit_index)
6076 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
6077 constructor_unfilled_fields = next;
6079 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6081 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
6082 size_in_bytes (TREE_TYPE (constructor_type)));
6084 = size_binop (MULT_EXPR, next,
6085 size_in_bytes (TREE_TYPE (constructor_type)));
6086 TREE_INT_CST_LOW (constructor_unfilled_index)
6087 = TREE_INT_CST_LOW (next);
6088 TREE_INT_CST_HIGH (constructor_unfilled_index)
6089 = TREE_INT_CST_HIGH (next);
6096 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
6098 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
6103 /* If it's not incremental, just skip over the gap,
6104 so that after jumping to retry we will output the next
6105 successive element. */
6106 if (TREE_CODE (constructor_type) == RECORD_TYPE
6107 || TREE_CODE (constructor_type) == UNION_TYPE)
6108 constructor_unfilled_fields = next;
6109 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6111 TREE_INT_CST_LOW (constructor_unfilled_index)
6112 = TREE_INT_CST_LOW (next);
6113 TREE_INT_CST_HIGH (constructor_unfilled_index)
6114 = TREE_INT_CST_HIGH (next);
6121 /* Add one non-braced element to the current constructor level.
6122 This adjusts the current position within the constructor's type.
6123 This may also start or terminate implicit levels
6124 to handle a partly-braced initializer.
6126 Once this has found the correct level for the new element,
6127 it calls output_init_element.
6129 Note: if we are incrementally outputting this constructor,
6130 this function may be called with a null argument
6131 representing a sub-constructor that was already incrementally output.
6132 When that happens, we output nothing, but we do the bookkeeping
6133 to skip past that element of the current constructor. */
6136 process_init_element (value)
6139 tree orig_value = value;
6140 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6142 /* Handle superfluous braces around string cst as in
6143 char x[] = {"foo"}; */
6146 && TREE_CODE (constructor_type) == ARRAY_TYPE
6147 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6148 && integer_zerop (constructor_unfilled_index))
6150 constructor_stack->replacement_value = value;
6154 if (constructor_stack->replacement_value != 0)
6156 error_init ("excess elements in struct initializer%s",
6157 " after `%s'", NULL_PTR);
6161 /* Ignore elements of a brace group if it is entirely superfluous
6162 and has already been diagnosed. */
6163 if (constructor_type == 0)
6166 /* If we've exhausted any levels that didn't have braces,
6168 while (constructor_stack->implicit)
6170 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6171 || TREE_CODE (constructor_type) == UNION_TYPE)
6172 && constructor_fields == 0)
6173 process_init_element (pop_init_level (1));
6174 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6175 && tree_int_cst_lt (constructor_max_index, constructor_index))
6176 process_init_element (pop_init_level (1));
6183 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6186 enum tree_code fieldcode;
6188 if (constructor_fields == 0)
6190 pedwarn_init ("excess elements in struct initializer%s",
6191 " after `%s'", NULL_PTR);
6195 fieldtype = TREE_TYPE (constructor_fields);
6196 if (fieldtype != error_mark_node)
6197 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6198 fieldcode = TREE_CODE (fieldtype);
6200 /* Accept a string constant to initialize a subarray. */
6202 && fieldcode == ARRAY_TYPE
6203 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6206 /* Otherwise, if we have come to a subaggregate,
6207 and we don't have an element of its type, push into it. */
6208 else if (value != 0 && !constructor_no_implicit
6209 && value != error_mark_node
6210 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6211 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6212 || fieldcode == UNION_TYPE))
6214 push_init_level (1);
6220 push_member_name (constructor_fields);
6221 output_init_element (value, fieldtype, constructor_fields, 1);
6222 RESTORE_SPELLING_DEPTH (constructor_depth);
6225 /* Do the bookkeeping for an element that was
6226 directly output as a constructor. */
6228 /* For a record, keep track of end position of last field. */
6229 tree temp = size_binop (PLUS_EXPR,
6230 DECL_FIELD_BITPOS (constructor_fields),
6231 DECL_SIZE (constructor_fields));
6232 TREE_INT_CST_LOW (constructor_bit_index)
6233 = TREE_INT_CST_LOW (temp);
6234 TREE_INT_CST_HIGH (constructor_bit_index)
6235 = TREE_INT_CST_HIGH (temp);
6237 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6240 constructor_fields = TREE_CHAIN (constructor_fields);
6241 /* Skip any nameless bit fields at the beginning. */
6242 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
6243 && DECL_NAME (constructor_fields) == 0)
6244 constructor_fields = TREE_CHAIN (constructor_fields);
6247 if (TREE_CODE (constructor_type) == UNION_TYPE)
6250 enum tree_code fieldcode;
6252 if (constructor_fields == 0)
6254 pedwarn_init ("excess elements in union initializer%s",
6255 " after `%s'", NULL_PTR);
6259 fieldtype = TREE_TYPE (constructor_fields);
6260 if (fieldtype != error_mark_node)
6261 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6262 fieldcode = TREE_CODE (fieldtype);
6264 /* Accept a string constant to initialize a subarray. */
6266 && fieldcode == ARRAY_TYPE
6267 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6270 /* Otherwise, if we have come to a subaggregate,
6271 and we don't have an element of its type, push into it. */
6272 else if (value != 0 && !constructor_no_implicit
6273 && value != error_mark_node
6274 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6275 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6276 || fieldcode == UNION_TYPE))
6278 push_init_level (1);
6284 push_member_name (constructor_fields);
6285 output_init_element (value, fieldtype, constructor_fields, 1);
6286 RESTORE_SPELLING_DEPTH (constructor_depth);
6289 /* Do the bookkeeping for an element that was
6290 directly output as a constructor. */
6292 TREE_INT_CST_LOW (constructor_bit_index)
6293 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6294 TREE_INT_CST_HIGH (constructor_bit_index)
6295 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6297 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6300 constructor_fields = 0;
6303 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6305 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6306 enum tree_code eltcode = TREE_CODE (elttype);
6308 /* Accept a string constant to initialize a subarray. */
6310 && eltcode == ARRAY_TYPE
6311 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6314 /* Otherwise, if we have come to a subaggregate,
6315 and we don't have an element of its type, push into it. */
6316 else if (value != 0 && !constructor_no_implicit
6317 && value != error_mark_node
6318 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6319 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6320 || eltcode == UNION_TYPE))
6322 push_init_level (1);
6326 if (constructor_max_index != 0
6327 && tree_int_cst_lt (constructor_max_index, constructor_index))
6329 pedwarn_init ("excess elements in array initializer%s",
6330 " after `%s'", NULL_PTR);
6334 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */
6335 if (constructor_range_end)
6336 value = save_expr (value);
6338 /* Now output the actual element.
6339 Ordinarily, output once.
6340 If there is a range, repeat it till we advance past the range. */
6347 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6348 output_init_element (value, elttype, constructor_index, 1);
6349 RESTORE_SPELLING_DEPTH (constructor_depth);
6352 tem = size_binop (PLUS_EXPR, constructor_index,
6354 TREE_INT_CST_LOW (constructor_index)
6355 = TREE_INT_CST_LOW (tem);
6356 TREE_INT_CST_HIGH (constructor_index)
6357 = TREE_INT_CST_HIGH (tem);
6360 /* If we are doing the bookkeeping for an element that was
6361 directly output as a constructor,
6362 we must update constructor_unfilled_index. */
6364 TREE_INT_CST_LOW (constructor_unfilled_index)
6365 = TREE_INT_CST_LOW (constructor_index);
6366 TREE_INT_CST_HIGH (constructor_unfilled_index)
6367 = TREE_INT_CST_HIGH (constructor_index);
6370 while (! (constructor_range_end == 0
6371 || tree_int_cst_lt (constructor_range_end,
6372 constructor_index)));
6377 /* Handle the sole element allowed in a braced initializer
6378 for a scalar variable. */
6379 if (constructor_fields == 0)
6381 pedwarn_init ("excess elements in scalar initializer%s",
6382 " after `%s'", NULL_PTR);
6387 output_init_element (value, constructor_type, NULL_TREE, 1);
6388 constructor_fields = 0;
6392 /* If the (lexically) previous elments are not now saved,
6393 we can discard the storage for them. */
6394 if (constructor_incremental && constructor_pending_elts == 0 && value != 0
6395 && constructor_stack == 0)
6399 /* Expand an ASM statement with operands, handling output operands
6400 that are not variables or INDIRECT_REFS by transforming such
6401 cases into cases that expand_asm_operands can handle.
6403 Arguments are same as for expand_asm_operands. */
6406 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6407 tree string, outputs, inputs, clobbers;
6412 int noutputs = list_length (outputs);
6414 /* o[I] is the place that output number I should be written. */
6415 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6418 if (TREE_CODE (string) == ADDR_EXPR)
6419 string = TREE_OPERAND (string, 0);
6420 if (TREE_CODE (string) != STRING_CST)
6422 error ("asm template is not a string constant");
6426 /* Record the contents of OUTPUTS before it is modified. */
6427 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6428 o[i] = TREE_VALUE (tail);
6430 /* Perform default conversions on array and function inputs. */
6431 /* Don't do this for other types--
6432 it would screw up operands expected to be in memory. */
6433 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6434 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6435 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6436 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6438 /* Generate the ASM_OPERANDS insn;
6439 store into the TREE_VALUEs of OUTPUTS some trees for
6440 where the values were actually stored. */
6441 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6443 /* Copy all the intermediate outputs into the specified outputs. */
6444 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6446 if (o[i] != TREE_VALUE (tail))
6448 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6452 /* Detect modification of read-only values.
6453 (Otherwise done by build_modify_expr.) */
6456 tree type = TREE_TYPE (o[i]);
6457 if (TREE_READONLY (o[i])
6458 || TYPE_READONLY (type)
6459 || ((TREE_CODE (type) == RECORD_TYPE
6460 || TREE_CODE (type) == UNION_TYPE)
6461 && C_TYPE_FIELDS_READONLY (type)))
6462 readonly_warning (o[i], "modification by `asm'");
6466 /* Those MODIFY_EXPRs could do autoincrements. */
6470 /* Expand a C `return' statement.
6471 RETVAL is the expression for what to return,
6472 or a null pointer for `return;' with no value. */
6475 c_expand_return (retval)
6478 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6480 if (TREE_THIS_VOLATILE (current_function_decl))
6481 warning ("function declared `noreturn' has a `return' statement");
6485 current_function_returns_null = 1;
6486 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6487 warning ("`return' with no value, in function returning non-void");
6488 expand_null_return ();
6490 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6492 current_function_returns_null = 1;
6493 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6494 pedwarn ("`return' with a value, in function returning void");
6495 expand_return (retval);
6499 tree t = convert_for_assignment (valtype, retval, "return",
6500 NULL_TREE, NULL_TREE, 0);
6501 tree res = DECL_RESULT (current_function_decl);
6504 if (t == error_mark_node)
6507 inner = t = convert (TREE_TYPE (res), t);
6509 /* Strip any conversions, additions, and subtractions, and see if
6510 we are returning the address of a local variable. Warn if so. */
6513 switch (TREE_CODE (inner))
6515 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6517 inner = TREE_OPERAND (inner, 0);
6521 /* If the second operand of the MINUS_EXPR has a pointer
6522 type (or is converted from it), this may be valid, so
6523 don't give a warning. */
6525 tree op1 = TREE_OPERAND (inner, 1);
6527 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6528 && (TREE_CODE (op1) == NOP_EXPR
6529 || TREE_CODE (op1) == NON_LVALUE_EXPR
6530 || TREE_CODE (op1) == CONVERT_EXPR))
6531 op1 = TREE_OPERAND (op1, 0);
6533 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6536 inner = TREE_OPERAND (inner, 0);
6541 inner = TREE_OPERAND (inner, 0);
6543 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6544 inner = TREE_OPERAND (inner, 0);
6546 if (TREE_CODE (inner) == VAR_DECL
6547 && ! DECL_EXTERNAL (inner)
6548 && ! TREE_STATIC (inner)
6549 && DECL_CONTEXT (inner) == current_function_decl)
6550 warning ("function returns address of local variable");
6557 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6558 TREE_SIDE_EFFECTS (t) = 1;
6560 current_function_returns_value = 1;
6564 /* Start a C switch statement, testing expression EXP.
6565 Return EXP if it is valid, an error node otherwise. */
6568 c_expand_start_case (exp)
6571 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6572 tree type = TREE_TYPE (exp);
6574 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6576 error ("switch quantity not an integer");
6577 exp = error_mark_node;
6582 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6584 if (warn_traditional
6585 && (type == long_integer_type_node
6586 || type == long_unsigned_type_node))
6587 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6589 exp = default_conversion (exp);
6590 type = TREE_TYPE (exp);
6591 index = get_unwidened (exp, NULL_TREE);
6592 /* We can't strip a conversion from a signed type to an unsigned,
6593 because if we did, int_fits_type_p would do the wrong thing
6594 when checking case values for being in range,
6595 and it's too hard to do the right thing. */
6596 if (TREE_UNSIGNED (TREE_TYPE (exp))
6597 == TREE_UNSIGNED (TREE_TYPE (index)))
6601 expand_start_case (1, exp, type, "switch statement");