1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 88, 91, 92, 93, 1994 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization.
26 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
27 and to process initializations in declarations (since they work
28 like a strange sort of assignment). */
36 /* Nonzero if we've already printed a "missing braces around initializer"
37 message within this initializer. */
38 static int missing_braces_mentioned;
40 extern char *index ();
41 extern char *rindex ();
43 static tree quality_type PROTO((tree, tree));
44 static int comp_target_types PROTO((tree, tree));
45 static int function_types_compatible_p PROTO((tree, tree));
46 static int type_lists_compatible_p PROTO((tree, tree));
47 static int self_promoting_type_p PROTO((tree));
48 static tree decl_constant_value PROTO((tree));
49 static tree lookup_field PROTO((tree, tree, tree *));
50 static tree convert_arguments PROTO((tree, tree, tree, tree));
51 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
52 static tree pointer_diff PROTO((tree, tree));
53 static tree unary_complex_lvalue PROTO((enum tree_code, tree));
54 static void pedantic_lvalue_warning PROTO((enum tree_code));
55 static tree internal_build_compound_expr PROTO((tree, int));
56 static tree convert_for_assignment PROTO((tree, tree, char *, tree,
58 static void warn_for_assignment PROTO((char *, char *, tree, int));
59 static tree valid_compound_expr_initializer PROTO((tree, tree));
60 static void push_string PROTO((char *));
61 static void push_member_name PROTO((tree));
62 static void push_array_bounds PROTO((int));
63 static int spelling_length PROTO((void));
64 static char *print_spelling PROTO((char *));
65 static char *get_spelling PROTO((char *));
66 static void warning_init PROTO((char *, char *,
68 static tree digest_init PROTO((tree, tree, int, int));
69 static void check_init_type_bitfields PROTO((tree));
70 static void output_init_element PROTO((tree, tree, tree, int));
71 static void output_pending_init_elements PROTO((int));
73 /* Do `exp = require_complete_type (exp);' to make sure exp
74 does not have an incomplete type. (That includes void types.) */
77 require_complete_type (value)
80 tree type = TREE_TYPE (value);
82 /* First, detect a valid value with a complete type. */
83 if (TYPE_SIZE (type) != 0
84 && type != void_type_node)
87 incomplete_type_error (value, type);
88 return error_mark_node;
91 /* Print an error message for invalid use of an incomplete type.
92 VALUE is the expression that was used (or 0 if that isn't known)
93 and TYPE is the type that was invalid. */
96 incomplete_type_error (value, type)
102 /* Avoid duplicate error message. */
103 if (TREE_CODE (type) == ERROR_MARK)
106 if (value != 0 && (TREE_CODE (value) == VAR_DECL
107 || TREE_CODE (value) == PARM_DECL))
108 error ("`%s' has an incomplete type",
109 IDENTIFIER_POINTER (DECL_NAME (value)));
113 /* We must print an error message. Be clever about what it says. */
115 switch (TREE_CODE (type))
118 errmsg = "invalid use of undefined type `struct %s'";
122 errmsg = "invalid use of undefined type `union %s'";
126 errmsg = "invalid use of undefined type `enum %s'";
130 error ("invalid use of void expression");
134 if (TYPE_DOMAIN (type))
136 type = TREE_TYPE (type);
139 error ("invalid use of array with unspecified bounds");
146 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
147 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
149 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
150 error ("invalid use of incomplete typedef `%s'",
151 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
155 /* Return a variant of TYPE which has all the type qualifiers of LIKE
156 as well as those of TYPE. */
159 qualify_type (type, like)
162 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
163 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
164 return c_build_type_variant (type, constflag, volflag);
167 /* Return the common type of two types.
168 We assume that comptypes has already been done and returned 1;
169 if that isn't so, this may crash. In particular, we assume that qualifiers
172 This is the type for the result of most arithmetic operations
173 if the operands have the given two types. */
179 register enum tree_code code1;
180 register enum tree_code code2;
183 /* Save time if the two types are the same. */
185 if (t1 == t2) return t1;
187 /* If one type is nonsense, use the other. */
188 if (t1 == error_mark_node)
190 if (t2 == error_mark_node)
193 /* Merge the attributes */
195 { register tree a1, a2;
196 a1 = TYPE_ATTRIBUTES (t1);
197 a2 = TYPE_ATTRIBUTES (t2);
199 /* Either one unset? Take the set one. */
201 if (!(attributes = a1))
204 /* One that completely contains the other? Take it. */
206 else if (a2 && !attribute_list_contained (a1, a2))
207 if (attribute_list_contained (a2, a1))
211 /* Pick the longest list, and hang on the other
214 if (list_length (a1) < list_length (a2))
215 attributes = a2, a2 = a1;
217 for (; a2; a2 = TREE_CHAIN (a2))
218 if (!value_member (attributes, a2))
221 TREE_CHAIN (a1) = attributes;
227 /* Treat an enum type as the unsigned integer type of the same width. */
229 if (TREE_CODE (t1) == ENUMERAL_TYPE)
230 t1 = type_for_size (TYPE_PRECISION (t1), 1);
231 if (TREE_CODE (t2) == ENUMERAL_TYPE)
232 t2 = type_for_size (TYPE_PRECISION (t2), 1);
234 code1 = TREE_CODE (t1);
235 code2 = TREE_CODE (t2);
237 /* If one type is complex, form the common type of the non-complex
238 components, then make that complex. Use T1 or T2 if it is the
240 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
242 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
243 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
244 tree subtype = common_type (subtype1, subtype2);
246 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
247 return build_type_attribute_variant (t1, attributes);
248 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
249 return build_type_attribute_variant (t2, attributes);
251 return build_type_attribute_variant (build_complex_type (subtype),
259 /* If only one is real, use it as the result. */
261 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
262 return build_type_attribute_variant (t1, attributes);
264 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
265 return build_type_attribute_variant (t2, attributes);
267 /* Both real or both integers; use the one with greater precision. */
269 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
270 return build_type_attribute_variant (t1, attributes);
271 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
272 return build_type_attribute_variant (t2, attributes);
274 /* Same precision. Prefer longs to ints even when same size. */
276 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
277 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
278 return build_type_attribute_variant (long_unsigned_type_node,
281 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
282 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
284 /* But preserve unsignedness from the other type,
285 since long cannot hold all the values of an unsigned int. */
286 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
287 t1 = long_unsigned_type_node;
289 t1 = long_integer_type_node;
290 return build_type_attribute_variant (t1, attributes);
293 /* Otherwise prefer the unsigned one. */
295 if (TREE_UNSIGNED (t1))
296 return build_type_attribute_variant (t1, attributes);
298 return build_type_attribute_variant (t2, attributes);
301 /* For two pointers, do this recursively on the target type,
302 and combine the qualifiers of the two types' targets. */
303 /* This code was turned off; I don't know why.
304 But ANSI C specifies doing this with the qualifiers.
305 So I turned it on again. */
307 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
308 TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
310 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
312 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
313 t1 = build_pointer_type (c_build_type_variant (target, constp,
315 return build_type_attribute_variant (t1, attributes);
318 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
319 return build_type_attribute_variant (t1, attributes);
324 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
325 /* Save space: see if the result is identical to one of the args. */
326 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
327 return build_type_attribute_variant (t1, attributes);
328 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
329 return build_type_attribute_variant (t2, attributes);
330 /* Merge the element types, and have a size if either arg has one. */
331 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
332 return build_type_attribute_variant (t1, attributes);
336 /* Function types: prefer the one that specified arg types.
337 If both do, merge the arg types. Also merge the return types. */
339 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
340 tree p1 = TYPE_ARG_TYPES (t1);
341 tree p2 = TYPE_ARG_TYPES (t2);
346 /* Save space: see if the result is identical to one of the args. */
347 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
348 return build_type_attribute_variant (t1, attributes);
349 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
350 return build_type_attribute_variant (t2, attributes);
352 /* Simple way if one arg fails to specify argument types. */
353 if (TYPE_ARG_TYPES (t1) == 0)
355 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
356 return build_type_attribute_variant (t1, attributes);
358 if (TYPE_ARG_TYPES (t2) == 0)
360 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
361 return build_type_attribute_variant (t1, attributes);
364 /* If both args specify argument types, we must merge the two
365 lists, argument by argument. */
367 len = list_length (p1);
370 for (i = 0; i < len; i++)
371 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
376 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
378 /* A null type means arg type is not specified.
379 Take whatever the other function type has. */
380 if (TREE_VALUE (p1) == 0)
382 TREE_VALUE (n) = TREE_VALUE (p2);
385 if (TREE_VALUE (p2) == 0)
387 TREE_VALUE (n) = TREE_VALUE (p1);
391 /* Given wait (union {union wait *u; int *i} *)
392 and wait (union wait *),
393 prefer union wait * as type of parm. */
394 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
395 && TREE_VALUE (p1) != TREE_VALUE (p2))
398 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
399 memb; memb = TREE_CHAIN (memb))
400 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
402 TREE_VALUE (n) = TREE_VALUE (p2);
404 pedwarn ("function types not truly compatible in ANSI C");
408 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
409 && TREE_VALUE (p2) != TREE_VALUE (p1))
412 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
413 memb; memb = TREE_CHAIN (memb))
414 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
416 TREE_VALUE (n) = TREE_VALUE (p1);
418 pedwarn ("function types not truly compatible in ANSI C");
422 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
426 t1 = build_function_type (valtype, newargs);
427 /* ... falls through ... */
431 return build_type_attribute_variant (t1, attributes);
436 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
437 or various other operations. Return 2 if they are compatible
438 but a warning may be needed if you use them together. */
441 comptypes (type1, type2)
444 register tree t1 = type1;
445 register tree t2 = type2;
448 /* Suppress errors caused by previously reported errors. */
450 if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
453 /* Treat an enum type as the integer type of the same width and
456 if (TREE_CODE (t1) == ENUMERAL_TYPE)
457 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
458 if (TREE_CODE (t2) == ENUMERAL_TYPE)
459 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
464 /* Different classes of types can't be compatible. */
466 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
468 /* Qualifiers must match. */
470 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
472 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
475 /* Allow for two different type nodes which have essentially the same
476 definition. Note that we already checked for equality of the type
477 type qualifiers (just above). */
479 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
482 #ifndef COMP_TYPE_ATTRIBUTES
483 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
486 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
487 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
490 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
493 switch (TREE_CODE (t1))
496 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
497 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
501 val = function_types_compatible_p (t1, t2);
506 tree d1 = TYPE_DOMAIN (t1);
507 tree d2 = TYPE_DOMAIN (t2);
510 /* Target types must match incl. qualifiers. */
511 if (TREE_TYPE (t1) != TREE_TYPE (t2)
512 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
515 /* Sizes must match unless one is missing or variable. */
516 if (d1 == 0 || d2 == 0 || d1 == d2
517 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
518 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
519 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
520 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
523 if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
524 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
525 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
526 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
527 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
528 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
529 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
530 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2)))))
536 if (maybe_objc_comptypes (t1, t2, 0) == 1)
540 return attrval == 2 && val == 1 ? 2 : val;
543 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
544 ignoring their qualifiers. */
547 comp_target_types (ttl, ttr)
552 /* Give maybe_objc_comptypes a crack at letting these types through. */
553 if (val = maybe_objc_comptypes (ttl, ttr, 1) >= 0)
556 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
557 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
559 if (val == 2 && pedantic)
560 pedwarn ("types are not quite compatible");
564 /* Subroutines of `comptypes'. */
566 /* Return 1 if two function types F1 and F2 are compatible.
567 If either type specifies no argument types,
568 the other must specify a fixed number of self-promoting arg types.
569 Otherwise, if one type specifies only the number of arguments,
570 the other must specify that number of self-promoting arg types.
571 Otherwise, the argument types must match. */
574 function_types_compatible_p (f1, f2)
578 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
582 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
583 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
586 args1 = TYPE_ARG_TYPES (f1);
587 args2 = TYPE_ARG_TYPES (f2);
589 /* An unspecified parmlist matches any specified parmlist
590 whose argument types don't need default promotions. */
594 if (!self_promoting_args_p (args2))
596 /* If one of these types comes from a non-prototype fn definition,
597 compare that with the other type's arglist.
598 If they don't match, ask for a warning (but no error). */
599 if (TYPE_ACTUAL_ARG_TYPES (f1)
600 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
606 if (!self_promoting_args_p (args1))
608 if (TYPE_ACTUAL_ARG_TYPES (f2)
609 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
614 /* Both types have argument lists: compare them and propagate results. */
615 val1 = type_lists_compatible_p (args1, args2);
616 return val1 != 1 ? val1 : val;
619 /* Check two lists of types for compatibility,
620 returning 0 for incompatible, 1 for compatible,
621 or 2 for compatible with warning. */
624 type_lists_compatible_p (args1, args2)
627 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
633 if (args1 == 0 && args2 == 0)
635 /* If one list is shorter than the other,
636 they fail to match. */
637 if (args1 == 0 || args2 == 0)
639 /* A null pointer instead of a type
640 means there is supposed to be an argument
641 but nothing is specified about what type it has.
642 So match anything that self-promotes. */
643 if (TREE_VALUE (args1) == 0)
645 if (! self_promoting_type_p (TREE_VALUE (args2)))
648 else if (TREE_VALUE (args2) == 0)
650 if (! self_promoting_type_p (TREE_VALUE (args1)))
653 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
655 /* Allow wait (union {union wait *u; int *i} *)
656 and wait (union wait *) to be compatible. */
657 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
658 && (TYPE_NAME (TREE_VALUE (args1)) == 0
659 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
660 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
661 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
662 TYPE_SIZE (TREE_VALUE (args2))))
665 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
666 memb; memb = TREE_CHAIN (memb))
667 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
672 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
673 && (TYPE_NAME (TREE_VALUE (args2)) == 0
674 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
675 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
676 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
677 TYPE_SIZE (TREE_VALUE (args1))))
680 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
681 memb; memb = TREE_CHAIN (memb))
682 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
691 /* comptypes said ok, but record if it said to warn. */
695 args1 = TREE_CHAIN (args1);
696 args2 = TREE_CHAIN (args2);
700 /* Return 1 if PARMS specifies a fixed number of parameters
701 and none of their types is affected by default promotions. */
704 self_promoting_args_p (parms)
708 for (t = parms; t; t = TREE_CHAIN (t))
710 register tree type = TREE_VALUE (t);
712 if (TREE_CHAIN (t) == 0 && type != void_type_node)
718 if (TYPE_MAIN_VARIANT (type) == float_type_node)
721 if (C_PROMOTING_INTEGER_TYPE_P (type))
727 /* Return 1 if TYPE is not affected by default promotions. */
730 self_promoting_type_p (type)
733 if (TYPE_MAIN_VARIANT (type) == float_type_node)
736 if (C_PROMOTING_INTEGER_TYPE_P (type))
742 /* Return an unsigned type the same as TYPE in other respects. */
748 tree type1 = TYPE_MAIN_VARIANT (type);
749 if (type1 == signed_char_type_node || type1 == char_type_node)
750 return unsigned_char_type_node;
751 if (type1 == integer_type_node)
752 return unsigned_type_node;
753 if (type1 == short_integer_type_node)
754 return short_unsigned_type_node;
755 if (type1 == long_integer_type_node)
756 return long_unsigned_type_node;
757 if (type1 == long_long_integer_type_node)
758 return long_long_unsigned_type_node;
762 /* Return a signed type the same as TYPE in other respects. */
768 tree type1 = TYPE_MAIN_VARIANT (type);
769 if (type1 == unsigned_char_type_node || type1 == char_type_node)
770 return signed_char_type_node;
771 if (type1 == unsigned_type_node)
772 return integer_type_node;
773 if (type1 == short_unsigned_type_node)
774 return short_integer_type_node;
775 if (type1 == long_unsigned_type_node)
776 return long_integer_type_node;
777 if (type1 == long_long_unsigned_type_node)
778 return long_long_integer_type_node;
782 /* Return a type the same as TYPE except unsigned or
783 signed according to UNSIGNEDP. */
786 signed_or_unsigned_type (unsignedp, type)
790 if (! INTEGRAL_TYPE_P (type))
792 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
793 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
794 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
795 return unsignedp ? unsigned_type_node : integer_type_node;
796 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
797 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
798 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
799 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
800 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
801 return (unsignedp ? long_long_unsigned_type_node
802 : long_long_integer_type_node);
806 /* Compute the value of the `sizeof' operator. */
812 enum tree_code code = TREE_CODE (type);
815 if (code == FUNCTION_TYPE)
817 if (pedantic || warn_pointer_arith)
818 pedwarn ("sizeof applied to a function type");
821 if (code == VOID_TYPE)
823 if (pedantic || warn_pointer_arith)
824 pedwarn ("sizeof applied to a void type");
827 if (code == ERROR_MARK)
829 if (TYPE_SIZE (type) == 0)
831 error ("sizeof applied to an incomplete type");
835 /* Convert in case a char is more than one unit. */
836 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
837 size_int (TYPE_PRECISION (char_type_node)));
838 /* size_binop does not put the constant in range, so do it now. */
839 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
840 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
845 c_sizeof_nowarn (type)
848 enum tree_code code = TREE_CODE (type);
851 if (code == FUNCTION_TYPE
853 || code == ERROR_MARK)
855 if (TYPE_SIZE (type) == 0)
858 /* Convert in case a char is more than one unit. */
859 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
860 size_int (TYPE_PRECISION (char_type_node)));
861 force_fit_type (t, 0);
865 /* Compute the size to increment a pointer by. */
868 c_size_in_bytes (type)
871 enum tree_code code = TREE_CODE (type);
874 if (code == FUNCTION_TYPE)
876 if (code == VOID_TYPE)
878 if (code == ERROR_MARK)
880 if (TYPE_SIZE (type) == 0)
882 error ("arithmetic on pointer to an incomplete type");
886 /* Convert in case a char is more than one unit. */
887 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
888 size_int (BITS_PER_UNIT));
889 force_fit_type (t, 0);
893 /* Implement the __alignof keyword: Return the minimum required
894 alignment of TYPE, measured in bytes. */
900 enum tree_code code = TREE_CODE (type);
902 if (code == FUNCTION_TYPE)
903 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
905 if (code == VOID_TYPE || code == ERROR_MARK)
908 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
911 /* Implement the __alignof keyword: Return the minimum required
912 alignment of EXPR, measured in bytes. For VAR_DECL's and
913 FIELD_DECL's return DECL_ALIGN (which can be set from an
914 "aligned" __attribute__ specification). */
917 c_alignof_expr (expr)
920 if (TREE_CODE (expr) == VAR_DECL)
921 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
923 if (TREE_CODE (expr) == COMPONENT_REF
924 && DECL_BIT_FIELD (TREE_OPERAND (expr, 1)))
926 error ("`__alignof' applied to a bit-field");
929 else if (TREE_CODE (expr) == COMPONENT_REF
930 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
931 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
933 if (TREE_CODE (expr) == INDIRECT_REF)
935 tree t = TREE_OPERAND (expr, 0);
937 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
939 while (TREE_CODE (t) == NOP_EXPR
940 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
944 t = TREE_OPERAND (t, 0);
945 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
946 if (thisalign > bestalign)
947 best = t, bestalign = thisalign;
949 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
952 return c_alignof (TREE_TYPE (expr));
954 /* Return either DECL or its known constant value (if it has one). */
957 decl_constant_value (decl)
960 if (! TREE_PUBLIC (decl)
961 /* Don't change a variable array bound or initial value to a constant
962 in a place where a variable is invalid. */
963 && current_function_decl != 0
965 && ! TREE_THIS_VOLATILE (decl)
966 && TREE_READONLY (decl) && ! ITERATOR_P (decl)
967 && DECL_INITIAL (decl) != 0
968 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
969 /* This is invalid if initial value is not constant.
970 If it has either a function call, a memory reference,
971 or a variable, then re-evaluating it could give different results. */
972 && TREE_CONSTANT (DECL_INITIAL (decl))
973 /* Check for cases where this is sub-optimal, even though valid. */
974 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
975 && DECL_MODE (decl) != BLKmode)
976 return DECL_INITIAL (decl);
980 /* Perform default promotions for C data used in expressions.
981 Arrays and functions are converted to pointers;
982 enumeral types or short or char, to int.
983 In addition, manifest constants symbols are replaced by their values. */
986 default_conversion (exp)
989 register tree type = TREE_TYPE (exp);
990 register enum tree_code code = TREE_CODE (type);
992 /* Constants can be used directly unless they're not loadable. */
993 if (TREE_CODE (exp) == CONST_DECL)
994 exp = DECL_INITIAL (exp);
996 /* Replace a nonvolatile const static variable with its value unless
997 it is an array, in which case we must be sure that taking the
998 address of the array produces consistent results. */
999 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1001 exp = decl_constant_value (exp);
1002 type = TREE_TYPE (exp);
1005 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1007 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
1008 to integer and cause infinite recursion. */
1009 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1010 || (TREE_CODE (exp) == NOP_EXPR
1011 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1012 exp = TREE_OPERAND (exp, 0);
1014 /* Normally convert enums to int,
1015 but convert wide enums to something wider. */
1016 if (code == ENUMERAL_TYPE)
1018 type = type_for_size (MAX (TYPE_PRECISION (type),
1019 TYPE_PRECISION (integer_type_node)),
1021 || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node))
1022 && TREE_UNSIGNED (type)));
1023 return convert (type, exp);
1026 if (C_PROMOTING_INTEGER_TYPE_P (type))
1028 /* Traditionally, unsignedness is preserved in default promotions.
1029 Also preserve unsignedness if not really getting any wider. */
1030 if (TREE_UNSIGNED (type)
1031 && (flag_traditional
1032 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1033 return convert (unsigned_type_node, exp);
1034 return convert (integer_type_node, exp);
1036 if (flag_traditional && !flag_allow_single_precision
1037 && TYPE_MAIN_VARIANT (type) == float_type_node)
1038 return convert (double_type_node, exp);
1039 if (code == VOID_TYPE)
1041 error ("void value not ignored as it ought to be");
1042 return error_mark_node;
1044 if (code == FUNCTION_TYPE)
1046 return build_unary_op (ADDR_EXPR, exp, 0);
1048 if (code == ARRAY_TYPE)
1051 tree restype = TREE_TYPE (type);
1056 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1057 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1059 constp = TREE_READONLY (exp);
1060 volatilep = TREE_THIS_VOLATILE (exp);
1063 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1064 || constp || volatilep)
1065 restype = c_build_type_variant (restype,
1066 TYPE_READONLY (type) || constp,
1067 TYPE_VOLATILE (type) || volatilep);
1069 if (TREE_CODE (exp) == INDIRECT_REF)
1070 return convert (TYPE_POINTER_TO (restype),
1071 TREE_OPERAND (exp, 0));
1073 if (TREE_CODE (exp) == COMPOUND_EXPR)
1075 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1076 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1077 TREE_OPERAND (exp, 0), op1);
1081 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1083 error ("invalid use of non-lvalue array");
1084 return error_mark_node;
1087 ptrtype = build_pointer_type (restype);
1089 if (TREE_CODE (exp) == VAR_DECL)
1091 /* ??? This is not really quite correct
1092 in that the type of the operand of ADDR_EXPR
1093 is not the target type of the type of the ADDR_EXPR itself.
1094 Question is, can this lossage be avoided? */
1095 adr = build1 (ADDR_EXPR, ptrtype, exp);
1096 if (mark_addressable (exp) == 0)
1097 return error_mark_node;
1098 TREE_CONSTANT (adr) = staticp (exp);
1099 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1102 /* This way is better for a COMPONENT_REF since it can
1103 simplify the offset for a component. */
1104 adr = build_unary_op (ADDR_EXPR, exp, 1);
1105 return convert (ptrtype, adr);
1110 /* Look up component name in the structure type definition.
1112 If this component name is found indirectly within an anonymous union,
1113 store in *INDIRECT the component which directly contains
1114 that anonymous union. Otherwise, set *INDIRECT to 0. */
1117 lookup_field (type, component, indirect)
1118 tree type, component;
1123 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1124 to the field elements. Use a binary search on this array to quickly
1125 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1126 will always be set for structures which have many elements. */
1128 if (TYPE_LANG_SPECIFIC (type))
1131 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1133 field = TYPE_FIELDS (type);
1135 top = TYPE_LANG_SPECIFIC (type)->len;
1136 while (top - bot > 1)
1140 half = (top - bot + 1) >> 1;
1141 field = field_array[bot+half];
1143 if (DECL_NAME (field) == NULL_TREE)
1145 /* Step through all anon unions in linear fashion. */
1146 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1150 field = field_array[bot++];
1151 anon = lookup_field (TREE_TYPE (field), component, &junk);
1152 if (anon != NULL_TREE)
1159 /* Entire record is only anon unions. */
1163 /* Restart the binary search, with new lower bound. */
1167 cmp = (HOST_WIDE_INT) DECL_NAME (field) - (HOST_WIDE_INT) component;
1176 if (DECL_NAME (field_array[bot]) == component)
1177 field = field_array[bot];
1178 else if (DECL_NAME (field) != component)
1183 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1185 if (DECL_NAME (field) == NULL_TREE)
1188 tree anon = lookup_field (TREE_TYPE (field), component, &junk);
1189 if (anon != NULL_TREE)
1196 if (DECL_NAME (field) == component)
1201 *indirect = NULL_TREE;
1205 /* Make an expression to refer to the COMPONENT field of
1206 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1209 build_component_ref (datum, component)
1210 tree datum, component;
1212 register tree type = TREE_TYPE (datum);
1213 register enum tree_code code = TREE_CODE (type);
1214 register tree field = NULL;
1217 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1218 unless we are not to support things not strictly ANSI. */
1219 switch (TREE_CODE (datum))
1223 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1224 return build (COMPOUND_EXPR, TREE_TYPE (value),
1225 TREE_OPERAND (datum, 0), value);
1228 return build_conditional_expr
1229 (TREE_OPERAND (datum, 0),
1230 build_component_ref (TREE_OPERAND (datum, 1), component),
1231 build_component_ref (TREE_OPERAND (datum, 2), component));
1234 /* See if there is a field or component with name COMPONENT. */
1236 if (code == RECORD_TYPE || code == UNION_TYPE)
1240 if (TYPE_SIZE (type) == 0)
1242 incomplete_type_error (NULL_TREE, type);
1243 return error_mark_node;
1246 field = lookup_field (type, component, &indirect);
1250 error (code == RECORD_TYPE
1251 ? "structure has no member named `%s'"
1252 : "union has no member named `%s'",
1253 IDENTIFIER_POINTER (component));
1254 return error_mark_node;
1256 if (TREE_TYPE (field) == error_mark_node)
1257 return error_mark_node;
1259 /* If FIELD was found buried within an anonymous union,
1260 make one COMPONENT_REF to get that anonymous union,
1261 then fall thru to make a second COMPONENT_REF to get FIELD. */
1264 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1265 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1266 TREE_READONLY (ref) = 1;
1267 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1268 TREE_THIS_VOLATILE (ref) = 1;
1272 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1274 if (TREE_READONLY (datum) || TREE_READONLY (field))
1275 TREE_READONLY (ref) = 1;
1276 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1277 TREE_THIS_VOLATILE (ref) = 1;
1281 else if (code != ERROR_MARK)
1282 error ("request for member `%s' in something not a structure or union",
1283 IDENTIFIER_POINTER (component));
1285 return error_mark_node;
1288 /* Given an expression PTR for a pointer, return an expression
1289 for the value pointed to.
1290 ERRORSTRING is the name of the operator to appear in error messages. */
1293 build_indirect_ref (ptr, errorstring)
1297 register tree pointer = default_conversion (ptr);
1298 register tree type = TREE_TYPE (pointer);
1300 if (TREE_CODE (type) == POINTER_TYPE)
1302 if (TREE_CODE (pointer) == ADDR_EXPR
1304 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1305 == TREE_TYPE (type)))
1306 return TREE_OPERAND (pointer, 0);
1309 tree t = TREE_TYPE (type);
1310 register tree ref = build1 (INDIRECT_REF,
1311 TYPE_MAIN_VARIANT (t), pointer);
1313 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1315 error ("dereferencing pointer to incomplete type");
1316 return error_mark_node;
1318 if (TREE_CODE (t) == VOID_TYPE)
1319 warning ("dereferencing `void *' pointer");
1321 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1322 so that we get the proper error message if the result is used
1323 to assign to. Also, &* is supposed to be a no-op.
1324 And ANSI C seems to specify that the type of the result
1325 should be the const type. */
1326 /* A de-reference of a pointer to const is not a const. It is valid
1327 to change it via some other pointer. */
1328 TREE_READONLY (ref) = TYPE_READONLY (t);
1329 TREE_SIDE_EFFECTS (ref)
1330 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1331 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1335 else if (TREE_CODE (pointer) != ERROR_MARK)
1336 error ("invalid type argument of `%s'", errorstring);
1337 return error_mark_node;
1340 /* This handles expressions of the form "a[i]", which denotes
1343 This is logically equivalent in C to *(a+i), but we may do it differently.
1344 If A is a variable or a member, we generate a primitive ARRAY_REF.
1345 This avoids forcing the array out of registers, and can work on
1346 arrays that are not lvalues (for example, members of structures returned
1350 build_array_ref (array, index)
1355 error ("subscript missing in array reference");
1356 return error_mark_node;
1359 if (TREE_TYPE (array) == error_mark_node
1360 || TREE_TYPE (index) == error_mark_node)
1361 return error_mark_node;
1363 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1364 && TREE_CODE (array) != INDIRECT_REF)
1368 /* Subscripting with type char is likely to lose
1369 on a machine where chars are signed.
1370 So warn on any machine, but optionally.
1371 Don't warn for unsigned char since that type is safe.
1372 Don't warn for signed char because anyone who uses that
1373 must have done so deliberately. */
1374 if (warn_char_subscripts
1375 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1376 warning ("array subscript has type `char'");
1378 /* Apply default promotions *after* noticing character types. */
1379 index = default_conversion (index);
1381 /* Require integer *after* promotion, for sake of enums. */
1382 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1384 error ("array subscript is not an integer");
1385 return error_mark_node;
1388 /* An array that is indexed by a non-constant
1389 cannot be stored in a register; we must be able to do
1390 address arithmetic on its address.
1391 Likewise an array of elements of variable size. */
1392 if (TREE_CODE (index) != INTEGER_CST
1393 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1394 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1396 if (mark_addressable (array) == 0)
1397 return error_mark_node;
1399 /* An array that is indexed by a constant value which is not within
1400 the array bounds cannot be stored in a register either; because we
1401 would get a crash in store_bit_field/extract_bit_field when trying
1402 to access a non-existent part of the register. */
1403 if (TREE_CODE (index) == INTEGER_CST
1404 && TYPE_VALUES (TREE_TYPE (array))
1405 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1407 if (mark_addressable (array) == 0)
1408 return error_mark_node;
1411 if (pedantic && !lvalue_p (array))
1413 if (DECL_REGISTER (array))
1414 pedwarn ("ANSI C forbids subscripting `register' array");
1416 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1422 while (TREE_CODE (foo) == COMPONENT_REF)
1423 foo = TREE_OPERAND (foo, 0);
1424 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1425 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1428 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1429 rval = build (ARRAY_REF, type, array, index);
1430 /* Array ref is const/volatile if the array elements are
1431 or if the array is. */
1432 TREE_READONLY (rval)
1433 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1434 | TREE_READONLY (array));
1435 TREE_SIDE_EFFECTS (rval)
1436 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1437 | TREE_SIDE_EFFECTS (array));
1438 TREE_THIS_VOLATILE (rval)
1439 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1440 /* This was added by rms on 16 Nov 91.
1441 It fixes vol struct foo *a; a->elts[1]
1442 in an inline function.
1443 Hope it doesn't break something else. */
1444 | TREE_THIS_VOLATILE (array));
1445 return require_complete_type (fold (rval));
1449 tree ar = default_conversion (array);
1450 tree ind = default_conversion (index);
1452 /* Put the integer in IND to simplify error checking. */
1453 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1460 if (ar == error_mark_node)
1463 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
1465 error ("subscripted value is neither array nor pointer");
1466 return error_mark_node;
1468 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1470 error ("array subscript is not an integer");
1471 return error_mark_node;
1474 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1479 /* Build a function call to function FUNCTION with parameters PARAMS.
1480 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1481 TREE_VALUE of each node is a parameter-expression.
1482 FUNCTION's data type may be a function type or a pointer-to-function. */
1485 build_function_call (function, params)
1486 tree function, params;
1488 register tree fntype, fundecl = 0;
1489 register tree coerced_params;
1490 tree name = NULL_TREE, assembler_name = NULL_TREE;
1492 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1493 STRIP_TYPE_NOPS (function);
1495 /* Convert anything with function type to a pointer-to-function. */
1496 if (TREE_CODE (function) == FUNCTION_DECL)
1498 name = DECL_NAME (function);
1499 assembler_name = DECL_ASSEMBLER_NAME (function);
1501 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1502 (because calling an inline function does not mean the function
1503 needs to be separately compiled). */
1504 fntype = build_type_variant (TREE_TYPE (function),
1505 TREE_READONLY (function),
1506 TREE_THIS_VOLATILE (function));
1508 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1511 function = default_conversion (function);
1513 fntype = TREE_TYPE (function);
1515 if (TREE_CODE (fntype) == ERROR_MARK)
1516 return error_mark_node;
1518 if (!(TREE_CODE (fntype) == POINTER_TYPE
1519 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1521 error ("called object is not a function");
1522 return error_mark_node;
1525 /* fntype now gets the type of function pointed to. */
1526 fntype = TREE_TYPE (fntype);
1528 /* Convert the parameters to the types declared in the
1529 function prototype, or apply default promotions. */
1532 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1534 /* Check for errors in format strings. */
1536 if (warn_format && (name || assembler_name))
1537 check_function_format (name, assembler_name, coerced_params);
1539 /* Recognize certain built-in functions so we can make tree-codes
1540 other than CALL_EXPR. We do this when it enables fold-const.c
1541 to do something useful. */
1543 if (TREE_CODE (function) == ADDR_EXPR
1544 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1545 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1546 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1551 if (coerced_params == 0)
1552 return integer_zero_node;
1553 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1557 register tree result
1558 = build (CALL_EXPR, TREE_TYPE (fntype),
1559 function, coerced_params, NULL_TREE);
1561 TREE_SIDE_EFFECTS (result) = 1;
1562 if (TREE_TYPE (result) == void_type_node)
1564 return require_complete_type (result);
1568 /* Convert the argument expressions in the list VALUES
1569 to the types in the list TYPELIST. The result is a list of converted
1570 argument expressions.
1572 If TYPELIST is exhausted, or when an element has NULL as its type,
1573 perform the default conversions.
1575 PARMLIST is the chain of parm decls for the function being called.
1576 It may be 0, if that info is not available.
1577 It is used only for generating error messages.
1579 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1581 This is also where warnings about wrong number of args are generated.
1583 Both VALUES and the returned value are chains of TREE_LIST nodes
1584 with the elements of the list in the TREE_VALUE slots of those nodes. */
1587 convert_arguments (typelist, values, name, fundecl)
1588 tree typelist, values, name, fundecl;
1590 register tree typetail, valtail;
1591 register tree result = NULL;
1594 /* Scan the given expressions and types, producing individual
1595 converted arguments and pushing them on RESULT in reverse order. */
1597 for (valtail = values, typetail = typelist, parmnum = 0;
1599 valtail = TREE_CHAIN (valtail), parmnum++)
1601 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1602 register tree val = TREE_VALUE (valtail);
1604 if (type == void_type_node)
1607 error ("too many arguments to function `%s'",
1608 IDENTIFIER_POINTER (name));
1610 error ("too many arguments to function");
1614 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1615 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1616 to convert automatically to a pointer. */
1617 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1618 val = TREE_OPERAND (val, 0);
1620 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1621 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1622 val = default_conversion (val);
1624 val = require_complete_type (val);
1628 /* Formal parm type is specified by a function prototype. */
1631 if (TYPE_SIZE (type) == 0)
1633 error ("type of formal parameter %d is incomplete", parmnum + 1);
1638 /* Optionally warn about conversions that
1639 differ from the default conversions. */
1640 if (warn_conversion)
1642 int formal_prec = TYPE_PRECISION (type);
1644 if (INTEGRAL_TYPE_P (type)
1645 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1646 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1647 else if (TREE_CODE (type) == COMPLEX_TYPE
1648 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1649 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1650 else if (TREE_CODE (type) == REAL_TYPE
1651 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1652 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1653 else if (TREE_CODE (type) == REAL_TYPE
1654 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1655 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1656 /* ??? At some point, messages should be written about
1657 conversions between complex types, but that's too messy
1659 else if (TREE_CODE (type) == REAL_TYPE
1660 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1662 /* Warn if any argument is passed as `float',
1663 since without a prototype it would be `double'. */
1664 if (formal_prec == TYPE_PRECISION (float_type_node))
1665 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1667 /* Detect integer changing in width or signedness. */
1668 else if (INTEGRAL_TYPE_P (type)
1669 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1671 tree would_have_been = default_conversion (val);
1672 tree type1 = TREE_TYPE (would_have_been);
1674 if (TREE_CODE (type) == ENUMERAL_TYPE
1675 && type == TREE_TYPE (val))
1676 /* No warning if function asks for enum
1677 and the actual arg is that enum type. */
1679 else if (formal_prec != TYPE_PRECISION (type1))
1680 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1681 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1683 /* Don't complain if the formal parameter type
1684 is an enum, because we can't tell now whether
1685 the value was an enum--even the same enum. */
1686 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1688 else if (TREE_CODE (val) == INTEGER_CST
1689 && int_fits_type_p (val, type))
1690 /* Change in signedness doesn't matter
1691 if a constant value is unaffected. */
1693 /* Likewise for a constant in a NOP_EXPR. */
1694 else if (TREE_CODE (val) == NOP_EXPR
1695 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1696 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1698 #if 0 /* We never get such tree structure here. */
1699 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1700 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1701 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1702 /* Change in signedness doesn't matter
1703 if an enum value is unaffected. */
1706 /* If the value is extended from a narrower
1707 unsigned type, it doesn't matter whether we
1708 pass it as signed or unsigned; the value
1709 certainly is the same either way. */
1710 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1711 && TREE_UNSIGNED (TREE_TYPE (val)))
1713 else if (TREE_UNSIGNED (type))
1714 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1716 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1720 parmval = convert_for_assignment (type, val,
1721 (char *)0, /* arg passing */
1722 fundecl, name, parmnum + 1);
1724 #ifdef PROMOTE_PROTOTYPES
1725 if ((TREE_CODE (type) == INTEGER_TYPE
1726 || TREE_CODE (type) == ENUMERAL_TYPE)
1727 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1728 parmval = default_conversion (parmval);
1731 result = tree_cons (NULL_TREE, parmval, result);
1733 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1734 && (TYPE_PRECISION (TREE_TYPE (val))
1735 < TYPE_PRECISION (double_type_node)))
1736 /* Convert `float' to `double'. */
1737 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1739 /* Convert `short' and `char' to full-size `int'. */
1740 result = tree_cons (NULL_TREE, default_conversion (val), result);
1743 typetail = TREE_CHAIN (typetail);
1746 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1749 error ("too few arguments to function `%s'",
1750 IDENTIFIER_POINTER (name));
1752 error ("too few arguments to function");
1755 return nreverse (result);
1758 /* This is the entry point used by the parser
1759 for binary operators in the input.
1760 In addition to constructing the expression,
1761 we check for operands that were written with other binary operators
1762 in a way that is likely to confuse the user. */
1765 parser_build_binary_op (code, arg1, arg2)
1766 enum tree_code code;
1769 tree result = build_binary_op (code, arg1, arg2, 1);
1772 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1773 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1774 enum tree_code code1 = ERROR_MARK;
1775 enum tree_code code2 = ERROR_MARK;
1777 if (class1 == 'e' || class1 == '1'
1778 || class1 == '2' || class1 == '<')
1779 code1 = C_EXP_ORIGINAL_CODE (arg1);
1780 if (class2 == 'e' || class2 == '1'
1781 || class2 == '2' || class2 == '<')
1782 code2 = C_EXP_ORIGINAL_CODE (arg2);
1784 /* Check for cases such as x+y<<z which users are likely
1785 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1786 is cleared to prevent these warnings. */
1787 if (warn_parentheses)
1789 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1791 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1792 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1793 warning ("suggest parentheses around + or - inside shift");
1796 if (code == TRUTH_ORIF_EXPR)
1798 if (code1 == TRUTH_ANDIF_EXPR
1799 || code2 == TRUTH_ANDIF_EXPR)
1800 warning ("suggest parentheses around && within ||");
1803 if (code == BIT_IOR_EXPR)
1805 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1806 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1807 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1808 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1809 warning ("suggest parentheses around arithmetic in operand of |");
1812 if (code == BIT_XOR_EXPR)
1814 if (code1 == BIT_AND_EXPR
1815 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1816 || code2 == BIT_AND_EXPR
1817 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1818 warning ("suggest parentheses around arithmetic in operand of ^");
1821 if (code == BIT_AND_EXPR)
1823 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1824 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1825 warning ("suggest parentheses around + or - in operand of &");
1829 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1830 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1831 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1832 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1834 unsigned_conversion_warning (result, arg1);
1835 unsigned_conversion_warning (result, arg2);
1836 overflow_warning (result);
1838 class = TREE_CODE_CLASS (TREE_CODE (result));
1840 /* Record the code that was specified in the source,
1841 for the sake of warnings about confusing nesting. */
1842 if (class == 'e' || class == '1'
1843 || class == '2' || class == '<')
1844 C_SET_EXP_ORIGINAL_CODE (result, code);
1847 int flag = TREE_CONSTANT (result);
1848 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1849 so that convert_for_assignment wouldn't strip it.
1850 That way, we got warnings for things like p = (1 - 1).
1851 But it turns out we should not get those warnings. */
1852 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1853 C_SET_EXP_ORIGINAL_CODE (result, code);
1854 TREE_CONSTANT (result) = flag;
1860 /* Build a binary-operation expression without default conversions.
1861 CODE is the kind of expression to build.
1862 This function differs from `build' in several ways:
1863 the data type of the result is computed and recorded in it,
1864 warnings are generated if arg data types are invalid,
1865 special handling for addition and subtraction of pointers is known,
1866 and some optimization is done (operations on narrow ints
1867 are done in the narrower type when that gives the same result).
1868 Constant folding is also done before the result is returned.
1870 Note that the operands will never have enumeral types, or function
1871 or array types, because either they will have the default conversions
1872 performed or they have both just been converted to some other type in which
1873 the arithmetic is to be done. */
1876 build_binary_op (code, orig_op0, orig_op1, convert_p)
1877 enum tree_code code;
1878 tree orig_op0, orig_op1;
1882 register enum tree_code code0, code1;
1885 /* Expression code to give to the expression when it is built.
1886 Normally this is CODE, which is what the caller asked for,
1887 but in some special cases we change it. */
1888 register enum tree_code resultcode = code;
1890 /* Data type in which the computation is to be performed.
1891 In the simplest cases this is the common type of the arguments. */
1892 register tree result_type = NULL;
1894 /* Nonzero means operands have already been type-converted
1895 in whatever way is necessary.
1896 Zero means they need to be converted to RESULT_TYPE. */
1899 /* Nonzero means after finally constructing the expression
1900 give it this type. Otherwise, give it type RESULT_TYPE. */
1901 tree final_type = 0;
1903 /* Nonzero if this is an operation like MIN or MAX which can
1904 safely be computed in short if both args are promoted shorts.
1905 Also implies COMMON.
1906 -1 indicates a bitwise operation; this makes a difference
1907 in the exact conditions for when it is safe to do the operation
1908 in a narrower mode. */
1911 /* Nonzero if this is a comparison operation;
1912 if both args are promoted shorts, compare the original shorts.
1913 Also implies COMMON. */
1914 int short_compare = 0;
1916 /* Nonzero if this is a right-shift operation, which can be computed on the
1917 original short and then promoted if the operand is a promoted short. */
1918 int short_shift = 0;
1920 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1925 op0 = default_conversion (orig_op0);
1926 op1 = default_conversion (orig_op1);
1934 type0 = TREE_TYPE (op0);
1935 type1 = TREE_TYPE (op1);
1937 /* The expression codes of the data types of the arguments tell us
1938 whether the arguments are integers, floating, pointers, etc. */
1939 code0 = TREE_CODE (type0);
1940 code1 = TREE_CODE (type1);
1942 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1943 STRIP_TYPE_NOPS (op0);
1944 STRIP_TYPE_NOPS (op1);
1946 /* If an error was already reported for one of the arguments,
1947 avoid reporting another error. */
1949 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1950 return error_mark_node;
1955 /* Handle the pointer + int case. */
1956 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1957 return pointer_int_sum (PLUS_EXPR, op0, op1);
1958 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1959 return pointer_int_sum (PLUS_EXPR, op1, op0);
1965 /* Subtraction of two similar pointers.
1966 We must subtract them as integers, then divide by object size. */
1967 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1968 && comp_target_types (type0, type1))
1969 return pointer_diff (op0, op1);
1970 /* Handle pointer minus int. Just like pointer plus int. */
1971 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1972 return pointer_int_sum (MINUS_EXPR, op0, op1);
1981 case TRUNC_DIV_EXPR:
1983 case FLOOR_DIV_EXPR:
1984 case ROUND_DIV_EXPR:
1985 case EXACT_DIV_EXPR:
1986 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1987 || code0 == COMPLEX_TYPE)
1988 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1989 || code1 == COMPLEX_TYPE))
1991 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
1992 resultcode = RDIV_EXPR;
1995 /* Although it would be tempting to shorten always here, that
1996 loses on some targets, since the modulo instruction is
1997 undefined if the quotient can't be represented in the
1998 computation mode. We shorten only if unsigned or if
1999 dividing by something we know != -1. */
2000 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2001 || (TREE_CODE (op1) == INTEGER_CST
2002 && (TREE_INT_CST_LOW (op1) != -1
2003 || TREE_INT_CST_HIGH (op1) != -1)));
2010 case BIT_ANDTC_EXPR:
2013 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2015 /* If one operand is a constant, and the other is a short type
2016 that has been converted to an int,
2017 really do the work in the short type and then convert the
2018 result to int. If we are lucky, the constant will be 0 or 1
2019 in the short type, making the entire operation go away. */
2020 if (TREE_CODE (op0) == INTEGER_CST
2021 && TREE_CODE (op1) == NOP_EXPR
2022 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2023 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2025 final_type = result_type;
2026 op1 = TREE_OPERAND (op1, 0);
2027 result_type = TREE_TYPE (op1);
2029 if (TREE_CODE (op1) == INTEGER_CST
2030 && TREE_CODE (op0) == NOP_EXPR
2031 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2032 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2034 final_type = result_type;
2035 op0 = TREE_OPERAND (op0, 0);
2036 result_type = TREE_TYPE (op0);
2040 case TRUNC_MOD_EXPR:
2041 case FLOOR_MOD_EXPR:
2042 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2044 /* Although it would be tempting to shorten always here, that loses
2045 on some targets, since the modulo instruction is undefined if the
2046 quotient can't be represented in the computation mode. We shorten
2047 only if unsigned or if dividing by something we know != -1. */
2048 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2049 || (TREE_CODE (op1) == INTEGER_CST
2050 && (TREE_INT_CST_LOW (op1) != -1
2051 || TREE_INT_CST_HIGH (op1) != -1)));
2056 case TRUTH_ANDIF_EXPR:
2057 case TRUTH_ORIF_EXPR:
2058 case TRUTH_AND_EXPR:
2060 case TRUTH_XOR_EXPR:
2061 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2062 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2063 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2064 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2066 /* Result of these operations is always an int,
2067 but that does not mean the operands should be
2068 converted to ints! */
2069 result_type = integer_type_node;
2070 op0 = truthvalue_conversion (op0);
2071 op1 = truthvalue_conversion (op1);
2076 /* Shift operations: result has same type as first operand;
2077 always convert second operand to int.
2078 Also set SHORT_SHIFT if shifting rightward. */
2081 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2083 if (TREE_CODE (op1) == INTEGER_CST)
2085 if (tree_int_cst_sgn (op1) < 0)
2086 warning ("right shift count is negative");
2089 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2091 if (TREE_INT_CST_HIGH (op1) != 0
2092 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2093 >= TYPE_PRECISION (type0)))
2094 warning ("right shift count >= width of type");
2097 /* Use the type of the value to be shifted.
2098 This is what most traditional C compilers do. */
2099 result_type = type0;
2100 /* Unless traditional, convert the shift-count to an integer,
2101 regardless of size of value being shifted. */
2102 if (! flag_traditional)
2104 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2105 op1 = convert (integer_type_node, op1);
2106 /* Avoid converting op1 to result_type later. */
2113 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2115 if (TREE_CODE (op1) == INTEGER_CST)
2117 if (tree_int_cst_sgn (op1) < 0)
2118 warning ("left shift count is negative");
2119 else if (TREE_INT_CST_HIGH (op1) != 0
2120 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2121 >= TYPE_PRECISION (type0)))
2122 warning ("left shift count >= width of type");
2124 /* Use the type of the value to be shifted.
2125 This is what most traditional C compilers do. */
2126 result_type = type0;
2127 /* Unless traditional, convert the shift-count to an integer,
2128 regardless of size of value being shifted. */
2129 if (! flag_traditional)
2131 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2132 op1 = convert (integer_type_node, op1);
2133 /* Avoid converting op1 to result_type later. */
2141 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2143 if (TREE_CODE (op1) == INTEGER_CST)
2145 if (tree_int_cst_sgn (op1) < 0)
2146 warning ("shift count is negative");
2147 else if (TREE_INT_CST_HIGH (op1) != 0
2148 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2149 >= TYPE_PRECISION (type0)))
2150 warning ("shift count >= width of type");
2152 /* Use the type of the value to be shifted.
2153 This is what most traditional C compilers do. */
2154 result_type = type0;
2155 /* Unless traditional, convert the shift-count to an integer,
2156 regardless of size of value being shifted. */
2157 if (! flag_traditional)
2159 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2160 op1 = convert (integer_type_node, op1);
2161 /* Avoid converting op1 to result_type later. */
2169 /* Result of comparison is always int,
2170 but don't convert the args to int! */
2171 result_type = integer_type_node;
2173 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2174 || code0 == COMPLEX_TYPE)
2175 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2176 || code1 == COMPLEX_TYPE))
2178 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2180 register tree tt0 = TREE_TYPE (type0);
2181 register tree tt1 = TREE_TYPE (type1);
2182 /* Anything compares with void *. void * compares with anything.
2183 Otherwise, the targets must be compatible
2184 and both must be object or both incomplete. */
2185 if (comp_target_types (type0, type1))
2187 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2189 /* op0 != orig_op0 detects the case of something
2190 whose value is 0 but which isn't a valid null ptr const. */
2191 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2192 && TREE_CODE (tt1) == FUNCTION_TYPE)
2193 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2195 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2197 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2198 && TREE_CODE (tt0) == FUNCTION_TYPE)
2199 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2202 pedwarn ("comparison of distinct pointer types lacks a cast");
2204 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2205 && integer_zerop (op1))
2206 op1 = null_pointer_node;
2207 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2208 && integer_zerop (op0))
2209 op0 = null_pointer_node;
2210 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2212 if (! flag_traditional)
2213 pedwarn ("comparison between pointer and integer");
2214 op1 = convert (TREE_TYPE (op0), op1);
2216 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2218 if (! flag_traditional)
2219 pedwarn ("comparison between pointer and integer");
2220 op0 = convert (TREE_TYPE (op1), op0);
2223 /* If args are not valid, clear out RESULT_TYPE
2224 to cause an error message later. */
2230 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2231 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2233 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2235 if (! comp_target_types (type0, type1))
2236 pedwarn ("comparison of distinct pointer types lacks a cast");
2238 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2239 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2240 result_type = common_type (type0, type1);
2248 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2249 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2251 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2253 if (! comp_target_types (type0, type1))
2254 pedwarn ("comparison of distinct pointer types lacks a cast");
2255 else if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2256 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2257 pedwarn ("comparison of complete and incomplete pointers");
2259 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2260 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2261 result_type = integer_type_node;
2263 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2264 && integer_zerop (op1))
2266 result_type = integer_type_node;
2267 op1 = null_pointer_node;
2269 pedwarn ("ordered comparison of pointer with integer zero");
2271 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2272 && integer_zerop (op0))
2274 result_type = integer_type_node;
2275 op0 = null_pointer_node;
2277 pedwarn ("ordered comparison of pointer with integer zero");
2279 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2281 result_type = integer_type_node;
2282 if (! flag_traditional)
2283 pedwarn ("comparison between pointer and integer");
2284 op1 = convert (TREE_TYPE (op0), op1);
2286 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2288 result_type = integer_type_node;
2289 if (! flag_traditional)
2290 pedwarn ("comparison between pointer and integer");
2291 op0 = convert (TREE_TYPE (op1), op0);
2297 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2299 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2301 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2303 if (shorten || common || short_compare)
2304 result_type = common_type (type0, type1);
2306 /* For certain operations (which identify themselves by shorten != 0)
2307 if both args were extended from the same smaller type,
2308 do the arithmetic in that type and then extend.
2310 shorten !=0 and !=1 indicates a bitwise operation.
2311 For them, this optimization is safe only if
2312 both args are zero-extended or both are sign-extended.
2313 Otherwise, we might change the result.
2314 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2315 but calculated in (unsigned short) it would be (unsigned short)-1. */
2317 if (shorten && none_complex)
2319 int unsigned0, unsigned1;
2320 tree arg0 = get_narrower (op0, &unsigned0);
2321 tree arg1 = get_narrower (op1, &unsigned1);
2322 /* UNS is 1 if the operation to be done is an unsigned one. */
2323 int uns = TREE_UNSIGNED (result_type);
2326 final_type = result_type;
2328 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2329 but it *requires* conversion to FINAL_TYPE. */
2331 if ((TYPE_PRECISION (TREE_TYPE (op0))
2332 == TYPE_PRECISION (TREE_TYPE (arg0)))
2333 && TREE_TYPE (op0) != final_type)
2334 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2335 if ((TYPE_PRECISION (TREE_TYPE (op1))
2336 == TYPE_PRECISION (TREE_TYPE (arg1)))
2337 && TREE_TYPE (op1) != final_type)
2338 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2340 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2342 /* For bitwise operations, signedness of nominal type
2343 does not matter. Consider only how operands were extended. */
2347 /* Note that in all three cases below we refrain from optimizing
2348 an unsigned operation on sign-extended args.
2349 That would not be valid. */
2351 /* Both args variable: if both extended in same way
2352 from same width, do it in that width.
2353 Do it unsigned if args were zero-extended. */
2354 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2355 < TYPE_PRECISION (result_type))
2356 && (TYPE_PRECISION (TREE_TYPE (arg1))
2357 == TYPE_PRECISION (TREE_TYPE (arg0)))
2358 && unsigned0 == unsigned1
2359 && (unsigned0 || !uns))
2361 = signed_or_unsigned_type (unsigned0,
2362 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2363 else if (TREE_CODE (arg0) == INTEGER_CST
2364 && (unsigned1 || !uns)
2365 && (TYPE_PRECISION (TREE_TYPE (arg1))
2366 < TYPE_PRECISION (result_type))
2367 && (type = signed_or_unsigned_type (unsigned1,
2369 int_fits_type_p (arg0, type)))
2371 else if (TREE_CODE (arg1) == INTEGER_CST
2372 && (unsigned0 || !uns)
2373 && (TYPE_PRECISION (TREE_TYPE (arg0))
2374 < TYPE_PRECISION (result_type))
2375 && (type = signed_or_unsigned_type (unsigned0,
2377 int_fits_type_p (arg1, type)))
2381 /* Shifts can be shortened if shifting right. */
2386 tree arg0 = get_narrower (op0, &unsigned_arg);
2388 final_type = result_type;
2390 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2391 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2393 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2394 /* If arg is sign-extended and then unsigned-shifted,
2395 we can simulate this with a signed shift in arg's type
2396 only if the extended result is at least twice as wide
2397 as the arg. Otherwise, the shift could use up all the
2398 ones made by sign-extension and bring in zeros.
2399 We can't optimize that case at all, but in most machines
2400 it never happens because available widths are 2**N. */
2401 && (!TREE_UNSIGNED (final_type)
2403 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2405 /* Do an unsigned shift if the operand was zero-extended. */
2407 = signed_or_unsigned_type (unsigned_arg,
2409 /* Convert value-to-be-shifted to that type. */
2410 if (TREE_TYPE (op0) != result_type)
2411 op0 = convert (result_type, op0);
2416 /* Comparison operations are shortened too but differently.
2417 They identify themselves by setting short_compare = 1. */
2421 /* Don't write &op0, etc., because that would prevent op0
2422 from being kept in a register.
2423 Instead, make copies of the our local variables and
2424 pass the copies by reference, then copy them back afterward. */
2425 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2426 enum tree_code xresultcode = resultcode;
2428 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2431 op0 = xop0, op1 = xop1, result_type = xresult_type;
2432 resultcode = xresultcode;
2436 tree op0_type = TREE_TYPE (orig_op0);
2437 tree op1_type = TREE_TYPE (orig_op1);
2438 int op0_unsigned = TREE_UNSIGNED (op0_type);
2439 int op1_unsigned = TREE_UNSIGNED (op1_type);
2441 /* Give warnings for comparisons between signed and unsigned
2442 quantities that will fail. Do not warn if the signed quantity
2443 is an unsuffixed integer literal (or some static constant
2444 expression involving such literals) and it is positive.
2445 Do not warn if the width of the unsigned quantity is less
2446 than that of the signed quantity, since in this case all
2447 values of the unsigned quantity fit in the signed quantity.
2448 Do not warn if the signed type is the same size as the
2449 result_type since sign extension does not cause trouble in
2451 /* Do the checking based on the original operand trees, so that
2452 casts will be considered, but default promotions won't be. */
2453 if (op0_unsigned != op1_unsigned
2455 && TYPE_PRECISION (op0_type) >= TYPE_PRECISION (op1_type)
2456 && TYPE_PRECISION (op0_type) < TYPE_PRECISION (result_type)
2457 && (TREE_CODE (op1) != INTEGER_CST
2458 || (TREE_CODE (op1) == INTEGER_CST
2459 && INT_CST_LT (op1, integer_zero_node))))
2462 && TYPE_PRECISION (op1_type) >= TYPE_PRECISION (op0_type)
2463 && TYPE_PRECISION (op1_type) < TYPE_PRECISION (result_type)
2464 && (TREE_CODE (op0) != INTEGER_CST
2465 || (TREE_CODE (op0) == INTEGER_CST
2466 && INT_CST_LT (op0, integer_zero_node))))))
2467 warning ("comparison between signed and unsigned");
2472 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2473 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2474 Then the expression will be built.
2475 It will be given type FINAL_TYPE if that is nonzero;
2476 otherwise, it will be given type RESULT_TYPE. */
2480 binary_op_error (code);
2481 return error_mark_node;
2486 if (TREE_TYPE (op0) != result_type)
2487 op0 = convert (result_type, op0);
2488 if (TREE_TYPE (op1) != result_type)
2489 op1 = convert (result_type, op1);
2493 register tree result = build (resultcode, result_type, op0, op1);
2494 register tree folded;
2496 folded = fold (result);
2497 if (folded == result)
2498 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2499 if (final_type != 0)
2500 return convert (final_type, folded);
2505 /* Return a tree for the sum or difference (RESULTCODE says which)
2506 of pointer PTROP and integer INTOP. */
2509 pointer_int_sum (resultcode, ptrop, intop)
2510 enum tree_code resultcode;
2511 register tree ptrop, intop;
2515 register tree result;
2516 register tree folded;
2518 /* The result is a pointer of the same type that is being added. */
2520 register tree result_type = TREE_TYPE (ptrop);
2522 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2524 if (pedantic || warn_pointer_arith)
2525 pedwarn ("pointer of type `void *' used in arithmetic");
2526 size_exp = integer_one_node;
2528 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2530 if (pedantic || warn_pointer_arith)
2531 pedwarn ("pointer to a function used in arithmetic");
2532 size_exp = integer_one_node;
2535 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2537 /* If what we are about to multiply by the size of the elements
2538 contains a constant term, apply distributive law
2539 and multiply that constant term separately.
2540 This helps produce common subexpressions. */
2542 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2543 && ! TREE_CONSTANT (intop)
2544 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2545 && TREE_CONSTANT (size_exp)
2546 /* If the constant comes from pointer subtraction,
2547 skip this optimization--it would cause an error. */
2548 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE)
2550 enum tree_code subcode = resultcode;
2551 tree int_type = TREE_TYPE (intop);
2552 if (TREE_CODE (intop) == MINUS_EXPR)
2553 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2554 /* Convert both subexpression types to the type of intop,
2555 because weird cases involving pointer arithmetic
2556 can result in a sum or difference with different type args. */
2557 ptrop = build_binary_op (subcode, ptrop,
2558 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2559 intop = convert (int_type, TREE_OPERAND (intop, 0));
2562 /* Convert the integer argument to a type the same size as a pointer
2563 so the multiply won't overflow spuriously. */
2565 if (TYPE_PRECISION (TREE_TYPE (intop)) != POINTER_SIZE)
2566 intop = convert (type_for_size (POINTER_SIZE, 0), intop);
2568 /* Replace the integer argument with a suitable product by the object size.
2569 Do this multiplication as signed, then convert to the appropriate
2570 pointer type (actually unsigned integral). */
2572 intop = convert (result_type,
2573 build_binary_op (MULT_EXPR, intop,
2574 convert (TREE_TYPE (intop), size_exp), 1));
2576 /* Create the sum or difference. */
2578 result = build (resultcode, result_type, ptrop, intop);
2580 folded = fold (result);
2581 if (folded == result)
2582 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2586 /* Return a tree for the difference of pointers OP0 and OP1.
2587 The resulting tree has type int. */
2590 pointer_diff (op0, op1)
2591 register tree op0, op1;
2593 register tree result, folded;
2594 tree restype = ptrdiff_type_node;
2596 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2598 if (pedantic || warn_pointer_arith)
2600 if (TREE_CODE (target_type) == VOID_TYPE)
2601 pedwarn ("pointer of type `void *' used in subtraction");
2602 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2603 pedwarn ("pointer to a function used in subtraction");
2606 /* First do the subtraction as integers;
2607 then drop through to build the divide operator. */
2609 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2610 convert (restype, op1), 1);
2611 /* This generates an error if op1 is pointer to incomplete type. */
2612 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2613 error ("arithmetic on pointer to an incomplete type");
2615 /* This generates an error if op0 is pointer to incomplete type. */
2616 op1 = c_size_in_bytes (target_type);
2618 /* Divide by the size, in easiest possible way. */
2620 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2622 folded = fold (result);
2623 if (folded == result)
2624 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2628 /* Construct and perhaps optimize a tree representation
2629 for a unary operation. CODE, a tree_code, specifies the operation
2630 and XARG is the operand. NOCONVERT nonzero suppresses
2631 the default promotions (such as from short to int). */
2634 build_unary_op (code, xarg, noconvert)
2635 enum tree_code code;
2639 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2640 register tree arg = xarg;
2641 register tree argtype = 0;
2642 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2643 char *errstring = NULL;
2646 if (typecode == ERROR_MARK)
2647 return error_mark_node;
2648 if (typecode == ENUMERAL_TYPE)
2649 typecode = INTEGER_TYPE;
2654 /* This is used for unary plus, because a CONVERT_EXPR
2655 is enough to prevent anybody from looking inside for
2656 associativity, but won't generate any code. */
2657 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2658 || typecode == COMPLEX_TYPE))
2659 errstring = "wrong type argument to unary plus";
2660 else if (!noconvert)
2661 arg = default_conversion (arg);
2665 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2666 || typecode == COMPLEX_TYPE))
2667 errstring = "wrong type argument to unary minus";
2668 else if (!noconvert)
2669 arg = default_conversion (arg);
2673 if (typecode == COMPLEX_TYPE)
2677 arg = default_conversion (arg);
2679 else if (typecode != INTEGER_TYPE)
2680 errstring = "wrong type argument to bit-complement";
2681 else if (!noconvert)
2682 arg = default_conversion (arg);
2686 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2687 || typecode == COMPLEX_TYPE))
2688 errstring = "wrong type argument to abs";
2689 else if (!noconvert)
2690 arg = default_conversion (arg);
2694 /* Conjugating a real value is a no-op, but allow it anyway. */
2695 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2696 || typecode == COMPLEX_TYPE))
2697 errstring = "wrong type argument to conjugation";
2698 else if (!noconvert)
2699 arg = default_conversion (arg);
2702 case TRUTH_NOT_EXPR:
2703 if (typecode != INTEGER_TYPE
2704 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2705 && typecode != COMPLEX_TYPE
2706 /* These will convert to a pointer. */
2707 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2709 errstring = "wrong type argument to unary exclamation mark";
2712 arg = truthvalue_conversion (arg);
2713 return invert_truthvalue (arg);
2719 if (TREE_CODE (arg) == COMPLEX_CST)
2720 return TREE_REALPART (arg);
2721 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2722 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2727 if (TREE_CODE (arg) == COMPLEX_CST)
2728 return TREE_IMAGPART (arg);
2729 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2730 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2732 return convert (TREE_TYPE (arg), integer_zero_node);
2734 case PREINCREMENT_EXPR:
2735 case POSTINCREMENT_EXPR:
2736 case PREDECREMENT_EXPR:
2737 case POSTDECREMENT_EXPR:
2738 /* Handle complex lvalues (when permitted)
2739 by reduction to simpler cases. */
2741 val = unary_complex_lvalue (code, arg);
2745 /* Increment or decrement the real part of the value,
2746 and don't change the imaginary part. */
2747 if (typecode == COMPLEX_TYPE)
2751 arg = stabilize_reference (arg);
2752 real = build_unary_op (REALPART_EXPR, arg, 1);
2753 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2754 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2755 build_unary_op (code, real, 1), imag);
2758 /* Report invalid types. */
2760 if (typecode != POINTER_TYPE
2761 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2763 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2764 errstring ="wrong type argument to increment";
2766 errstring ="wrong type argument to decrement";
2772 tree result_type = TREE_TYPE (arg);
2774 arg = get_unwidened (arg, 0);
2775 argtype = TREE_TYPE (arg);
2777 /* Compute the increment. */
2779 if (typecode == POINTER_TYPE)
2781 /* If pointer target is an undefined struct,
2782 we just cannot know how to do the arithmetic. */
2783 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2784 error ("%s of pointer to unknown structure",
2785 ((code == PREINCREMENT_EXPR
2786 || code == POSTINCREMENT_EXPR)
2787 ? "increment" : "decrement"));
2788 else if ((pedantic || warn_pointer_arith)
2789 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2790 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2791 pedwarn ("wrong type argument to %s",
2792 ((code == PREINCREMENT_EXPR
2793 || code == POSTINCREMENT_EXPR)
2794 ? "increment" : "decrement"));
2795 inc = c_size_in_bytes (TREE_TYPE (result_type));
2798 inc = integer_one_node;
2800 inc = convert (argtype, inc);
2802 /* Handle incrementing a cast-expression. */
2805 switch (TREE_CODE (arg))
2810 case FIX_TRUNC_EXPR:
2811 case FIX_FLOOR_EXPR:
2812 case FIX_ROUND_EXPR:
2814 pedantic_lvalue_warning (CONVERT_EXPR);
2815 /* If the real type has the same machine representation
2816 as the type it is cast to, we can make better output
2817 by adding directly to the inside of the cast. */
2818 if ((TREE_CODE (TREE_TYPE (arg))
2819 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2820 && (TYPE_MODE (TREE_TYPE (arg))
2821 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2822 arg = TREE_OPERAND (arg, 0);
2825 tree incremented, modify, value;
2826 arg = stabilize_reference (arg);
2827 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2830 value = save_expr (arg);
2831 incremented = build (((code == PREINCREMENT_EXPR
2832 || code == POSTINCREMENT_EXPR)
2833 ? PLUS_EXPR : MINUS_EXPR),
2834 argtype, value, inc);
2835 TREE_SIDE_EFFECTS (incremented) = 1;
2836 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2837 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2838 TREE_USED (value) = 1;
2848 /* Complain about anything else that is not a true lvalue. */
2849 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2850 || code == POSTINCREMENT_EXPR)
2851 ? "increment" : "decrement")))
2852 return error_mark_node;
2854 /* Report a read-only lvalue. */
2855 if (TREE_READONLY (arg))
2856 readonly_warning (arg,
2857 ((code == PREINCREMENT_EXPR
2858 || code == POSTINCREMENT_EXPR)
2859 ? "increment" : "decrement"));
2861 val = build (code, TREE_TYPE (arg), arg, inc);
2862 TREE_SIDE_EFFECTS (val) = 1;
2863 val = convert (result_type, val);
2864 if (TREE_CODE (val) != code)
2865 TREE_NO_UNUSED_WARNING (val) = 1;
2870 /* Note that this operation never does default_conversion
2871 regardless of NOCONVERT. */
2873 /* Let &* cancel out to simplify resulting code. */
2874 if (TREE_CODE (arg) == INDIRECT_REF)
2876 /* Don't let this be an lvalue. */
2877 if (lvalue_p (TREE_OPERAND (arg, 0)))
2878 return non_lvalue (TREE_OPERAND (arg, 0));
2879 return TREE_OPERAND (arg, 0);
2882 /* For &x[y], return x+y */
2883 if (TREE_CODE (arg) == ARRAY_REF)
2885 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
2886 return error_mark_node;
2887 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2888 TREE_OPERAND (arg, 1), 1);
2891 /* Handle complex lvalues (when permitted)
2892 by reduction to simpler cases. */
2893 val = unary_complex_lvalue (code, arg);
2897 #if 0 /* Turned off because inconsistent;
2898 float f; *&(int)f = 3.4 stores in int format
2899 whereas (int)f = 3.4 stores in float format. */
2900 /* Address of a cast is just a cast of the address
2901 of the operand of the cast. */
2902 switch (TREE_CODE (arg))
2907 case FIX_TRUNC_EXPR:
2908 case FIX_FLOOR_EXPR:
2909 case FIX_ROUND_EXPR:
2912 pedwarn ("ANSI C forbids the address of a cast expression");
2913 return convert (build_pointer_type (TREE_TYPE (arg)),
2914 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
2919 /* Allow the address of a constructor if all the elements
2921 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
2923 /* Anything not already handled and not a true memory reference
2925 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
2926 return error_mark_node;
2928 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2929 argtype = TREE_TYPE (arg);
2930 /* If the lvalue is const or volatile,
2931 merge that into the type that the address will point to. */
2932 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
2933 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2935 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
2936 argtype = c_build_type_variant (argtype,
2937 TREE_READONLY (arg),
2938 TREE_THIS_VOLATILE (arg));
2941 argtype = build_pointer_type (argtype);
2943 if (mark_addressable (arg) == 0)
2944 return error_mark_node;
2949 if (TREE_CODE (arg) == COMPONENT_REF)
2951 tree field = TREE_OPERAND (arg, 1);
2953 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
2955 if (DECL_BIT_FIELD (field))
2957 error ("attempt to take address of bit-field structure member `%s'",
2958 IDENTIFIER_POINTER (DECL_NAME (field)));
2959 return error_mark_node;
2962 addr = convert (argtype, addr);
2964 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
2967 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
2968 size_int (BITS_PER_UNIT));
2969 int flag = TREE_CONSTANT (addr);
2970 addr = fold (build (PLUS_EXPR, argtype,
2971 addr, convert (argtype, offset)));
2972 TREE_CONSTANT (addr) = flag;
2976 addr = build1 (code, argtype, arg);
2978 /* Address of a static or external variable or
2979 file-scope function counts as a constant. */
2981 && ! (TREE_CODE (arg) == FUNCTION_DECL
2982 && DECL_CONTEXT (arg) != 0))
2983 TREE_CONSTANT (addr) = 1;
2991 argtype = TREE_TYPE (arg);
2992 return fold (build1 (code, argtype, arg));
2996 return error_mark_node;
3000 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3001 convert ARG with the same conversions in the same order
3002 and return the result. */
3005 convert_sequence (conversions, arg)
3009 switch (TREE_CODE (conversions))
3014 case FIX_TRUNC_EXPR:
3015 case FIX_FLOOR_EXPR:
3016 case FIX_ROUND_EXPR:
3018 return convert (TREE_TYPE (conversions),
3019 convert_sequence (TREE_OPERAND (conversions, 0),
3028 /* Return nonzero if REF is an lvalue valid for this language.
3029 Lvalues can be assigned, unless their type has TYPE_READONLY.
3030 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3036 register enum tree_code code = TREE_CODE (ref);
3043 return lvalue_p (TREE_OPERAND (ref, 0));
3054 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3055 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
3062 /* Return nonzero if REF is an lvalue valid for this language;
3063 otherwise, print an error message and return zero. */
3066 lvalue_or_else (ref, string)
3070 int win = lvalue_p (ref);
3072 error ("invalid lvalue in %s", string);
3076 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3077 for certain kinds of expressions which are not really lvalues
3078 but which we can accept as lvalues.
3080 If ARG is not a kind of expression we can handle, return zero. */
3083 unary_complex_lvalue (code, arg)
3084 enum tree_code code;
3087 /* Handle (a, b) used as an "lvalue". */
3088 if (TREE_CODE (arg) == COMPOUND_EXPR)
3090 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3091 pedantic_lvalue_warning (COMPOUND_EXPR);
3092 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3093 TREE_OPERAND (arg, 0), real_result);
3096 /* Handle (a ? b : c) used as an "lvalue". */
3097 if (TREE_CODE (arg) == COND_EXPR)
3099 pedantic_lvalue_warning (COND_EXPR);
3100 return (build_conditional_expr
3101 (TREE_OPERAND (arg, 0),
3102 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3103 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3109 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3110 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3113 pedantic_lvalue_warning (code)
3114 enum tree_code code;
3117 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3118 code == COND_EXPR ? "conditional"
3119 : code == COMPOUND_EXPR ? "compound" : "cast");
3122 /* Warn about storing in something that is `const'. */
3125 readonly_warning (arg, string)
3130 strcpy (buf, string);
3132 /* Forbid assignments to iterators. */
3133 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3135 strcat (buf, " of iterator `%s'");
3136 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3139 if (TREE_CODE (arg) == COMPONENT_REF)
3141 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3142 readonly_warning (TREE_OPERAND (arg, 0), string);
3145 strcat (buf, " of read-only member `%s'");
3146 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3149 else if (TREE_CODE (arg) == VAR_DECL)
3151 strcat (buf, " of read-only variable `%s'");
3152 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3156 pedwarn ("%s of read-only location", buf);
3160 /* Mark EXP saying that we need to be able to take the
3161 address of it; it should not be allocated in a register.
3162 Value is 1 if successful. */
3165 mark_addressable (exp)
3168 register tree x = exp;
3170 switch (TREE_CODE (x))
3177 x = TREE_OPERAND (x, 0);
3181 TREE_ADDRESSABLE (x) = 1;
3188 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3189 && DECL_NONLOCAL (x))
3191 if (TREE_PUBLIC (x))
3193 error ("global register variable `%s' used in nested function",
3194 IDENTIFIER_POINTER (DECL_NAME (x)));
3197 pedwarn ("register variable `%s' used in nested function",
3198 IDENTIFIER_POINTER (DECL_NAME (x)));
3200 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3202 if (TREE_PUBLIC (x))
3204 error ("address of global register variable `%s' requested",
3205 IDENTIFIER_POINTER (DECL_NAME (x)));
3209 /* If we are making this addressable due to its having
3210 volatile components, give a different error message. Also
3211 handle the case of an unnamed parameter by not trying
3212 to give the name. */
3214 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3216 error ("cannot put object with volatile field into register");
3220 pedwarn ("address of register variable `%s' requested",
3221 IDENTIFIER_POINTER (DECL_NAME (x)));
3223 put_var_into_stack (x);
3227 TREE_ADDRESSABLE (x) = 1;
3228 #if 0 /* poplevel deals with this now. */
3229 if (DECL_CONTEXT (x) == 0)
3230 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3238 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3241 build_conditional_expr (ifexp, op1, op2)
3242 tree ifexp, op1, op2;
3244 register tree type1;
3245 register tree type2;
3246 register enum tree_code code1;
3247 register enum tree_code code2;
3248 register tree result_type = NULL;
3249 tree orig_op1 = op1, orig_op2 = op2;
3251 /* If second operand is omitted, it is the same as the first one;
3252 make sure it is calculated only once. */
3256 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression");
3257 ifexp = op1 = save_expr (ifexp);
3260 ifexp = truthvalue_conversion (default_conversion (ifexp));
3262 #if 0 /* Produces wrong result if within sizeof. */
3263 /* Don't promote the operands separately if they promote
3264 the same way. Return the unpromoted type and let the combined
3265 value get promoted if necessary. */
3267 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3268 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3269 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3270 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3272 if (TREE_CODE (ifexp) == INTEGER_CST)
3273 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3275 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3279 /* Promote both alternatives. */
3281 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3282 op1 = default_conversion (op1);
3283 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3284 op2 = default_conversion (op2);
3286 if (TREE_CODE (ifexp) == ERROR_MARK
3287 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3288 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3289 return error_mark_node;
3291 type1 = TREE_TYPE (op1);
3292 code1 = TREE_CODE (type1);
3293 type2 = TREE_TYPE (op2);
3294 code2 = TREE_CODE (type2);
3296 /* Quickly detect the usual case where op1 and op2 have the same type
3298 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3301 result_type = type1;
3303 result_type = TYPE_MAIN_VARIANT (type1);
3305 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3306 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3308 result_type = common_type (type1, type2);
3310 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3312 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3313 pedwarn ("ANSI C forbids conditional expr with only one void side");
3314 result_type = void_type_node;
3316 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3318 if (comp_target_types (type1, type2))
3319 result_type = common_type (type1, type2);
3320 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3321 && TREE_CODE (orig_op1) != NOP_EXPR)
3322 result_type = qualify_type (type2, type1);
3323 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3324 && TREE_CODE (orig_op2) != NOP_EXPR)
3325 result_type = qualify_type (type1, type2);
3326 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3328 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3329 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3330 result_type = qualify_type (type1, type2);
3332 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3334 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3335 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3336 result_type = qualify_type (type2, type1);
3340 pedwarn ("pointer type mismatch in conditional expression");
3341 result_type = build_pointer_type (void_type_node);
3344 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3346 if (! integer_zerop (op2))
3347 pedwarn ("pointer/integer type mismatch in conditional expression");
3350 op2 = null_pointer_node;
3351 #if 0 /* The spec seems to say this is permitted. */
3352 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3353 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3356 result_type = type1;
3358 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3360 if (!integer_zerop (op1))
3361 pedwarn ("pointer/integer type mismatch in conditional expression");
3364 op1 = null_pointer_node;
3365 #if 0 /* The spec seems to say this is permitted. */
3366 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3367 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3370 result_type = type2;
3375 if (flag_cond_mismatch)
3376 result_type = void_type_node;
3379 error ("type mismatch in conditional expression");
3380 return error_mark_node;
3384 /* Merge const and volatile flags of the incoming types. */
3386 = build_type_variant (result_type,
3387 TREE_READONLY (op1) || TREE_READONLY (op2),
3388 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3390 if (result_type != TREE_TYPE (op1))
3391 op1 = convert_and_check (result_type, op1);
3392 if (result_type != TREE_TYPE (op2))
3393 op2 = convert_and_check (result_type, op2);
3396 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3398 result_type = TREE_TYPE (op1);
3399 if (TREE_CONSTANT (ifexp))
3400 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3402 if (TYPE_MODE (result_type) == BLKmode)
3404 register tree tempvar
3405 = build_decl (VAR_DECL, NULL_TREE, result_type);
3406 register tree xop1 = build_modify_expr (tempvar, op1);
3407 register tree xop2 = build_modify_expr (tempvar, op2);
3408 register tree result = fold (build (COND_EXPR, result_type,
3409 ifexp, xop1, xop2));
3411 layout_decl (tempvar, TYPE_ALIGN (result_type));
3412 /* No way to handle variable-sized objects here.
3413 I fear that the entire handling of BLKmode conditional exprs
3414 needs to be redone. */
3415 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3418 = assign_stack_local (DECL_MODE (tempvar),
3419 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3420 + BITS_PER_UNIT - 1)
3424 TREE_SIDE_EFFECTS (result)
3425 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3426 | TREE_SIDE_EFFECTS (op2);
3427 return build (COMPOUND_EXPR, result_type, result, tempvar);
3432 if (TREE_CODE (ifexp) == INTEGER_CST)
3433 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3435 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3438 /* Given a list of expressions, return a compound expression
3439 that performs them all and returns the value of the last of them. */
3442 build_compound_expr (list)
3445 return internal_build_compound_expr (list, TRUE);
3449 internal_build_compound_expr (list, first_p)
3455 if (TREE_CHAIN (list) == 0)
3457 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3458 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3460 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3461 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3462 list = TREE_OPERAND (list, 0);
3465 /* Don't let (0, 0) be null pointer constant. */
3466 if (!first_p && integer_zerop (TREE_VALUE (list)))
3467 return non_lvalue (TREE_VALUE (list));
3468 return TREE_VALUE (list);
3471 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3473 /* Convert arrays to pointers when there really is a comma operator. */
3474 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3475 TREE_VALUE (TREE_CHAIN (list))
3476 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3479 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3481 /* When pedantic, a compound expression can be neither an lvalue
3482 nor an integer constant expression. */
3483 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
3486 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3489 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3492 build_c_cast (type, expr)
3496 register tree value = expr;
3498 if (type == error_mark_node || expr == error_mark_node)
3499 return error_mark_node;
3500 type = TYPE_MAIN_VARIANT (type);
3503 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3504 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3505 value = TREE_OPERAND (value, 0);
3508 if (TREE_CODE (type) == ARRAY_TYPE)
3510 error ("cast specifies array type");
3511 return error_mark_node;
3514 if (TREE_CODE (type) == FUNCTION_TYPE)
3516 error ("cast specifies function type");
3517 return error_mark_node;
3520 if (type == TREE_TYPE (value))
3524 if (TREE_CODE (type) == RECORD_TYPE
3525 || TREE_CODE (type) == UNION_TYPE)
3526 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3529 else if (TREE_CODE (type) == UNION_TYPE)
3532 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3533 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3534 value = default_conversion (value);
3536 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3537 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3538 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3547 pedwarn ("ANSI C forbids casts to union type");
3548 if (TYPE_NAME (type) != 0)
3550 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3551 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3553 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3557 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3558 build_tree_list (field, value)),
3560 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3563 error ("cast to union type from type not present in union");
3564 return error_mark_node;
3570 /* If casting to void, avoid the error that would come
3571 from default_conversion in the case of a non-lvalue array. */
3572 if (type == void_type_node)
3573 return build1 (CONVERT_EXPR, type, value);
3575 /* Convert functions and arrays to pointers,
3576 but don't convert any other types. */
3577 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3578 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3579 value = default_conversion (value);
3580 otype = TREE_TYPE (value);
3582 /* Optionally warn about potentially worrisome casts. */
3585 && TREE_CODE (type) == POINTER_TYPE
3586 && TREE_CODE (otype) == POINTER_TYPE)
3588 if (TYPE_VOLATILE (TREE_TYPE (otype))
3589 && ! TYPE_VOLATILE (TREE_TYPE (type)))
3590 pedwarn ("cast discards `volatile' from pointer target type");
3591 if (TYPE_READONLY (TREE_TYPE (otype))
3592 && ! TYPE_READONLY (TREE_TYPE (type)))
3593 pedwarn ("cast discards `const' from pointer target type");
3596 /* Warn about possible alignment problems. */
3597 if (STRICT_ALIGNMENT && warn_cast_align
3598 && TREE_CODE (type) == POINTER_TYPE
3599 && TREE_CODE (otype) == POINTER_TYPE
3600 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3601 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3602 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3603 warning ("cast increases required alignment of target type");
3605 if (TREE_CODE (type) == INTEGER_TYPE
3606 && TREE_CODE (otype) == POINTER_TYPE
3607 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3608 && !TREE_CONSTANT (value))
3609 warning ("cast from pointer to integer of different size");
3611 if (warn_bad_function_cast
3612 && TREE_CODE (value) == CALL_EXPR
3613 && TREE_CODE (type) != TREE_CODE (otype))
3614 warning ("cast does not match function type");
3616 if (TREE_CODE (type) == POINTER_TYPE
3617 && TREE_CODE (otype) == INTEGER_TYPE
3618 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3620 /* Don't warn about converting 0 to pointer,
3621 provided the 0 was explicit--not cast or made by folding. */
3622 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3624 /* Don't warn about converting any constant. */
3625 && !TREE_CONSTANT (value))
3626 warning ("cast to pointer from integer of different size");
3629 value = convert (type, value);
3631 /* Ignore any integer overflow caused by the cast. */
3632 if (TREE_CODE (value) == INTEGER_CST)
3634 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3635 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3639 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3640 if (pedantic && TREE_CODE (value) == INTEGER_CST
3641 && TREE_CODE (expr) == INTEGER_CST
3642 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3643 value = non_lvalue (value);
3645 /* If pedantic, don't let a cast be an lvalue. */
3646 if (value == expr && pedantic)
3647 value = non_lvalue (value);
3652 /* Build an assignment expression of lvalue LHS from value RHS.
3653 MODIFYCODE is the code for a binary operator that we use
3654 to combine the old value of LHS with RHS to get the new value.
3655 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3658 build_modify_expr (lhs, modifycode, rhs)
3660 enum tree_code modifycode;
3662 register tree result;
3664 tree lhstype = TREE_TYPE (lhs);
3665 tree olhstype = lhstype;
3667 /* Types that aren't fully specified cannot be used in assignments. */
3668 lhs = require_complete_type (lhs);
3670 /* Avoid duplicate error messages from operands that had errors. */
3671 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3672 return error_mark_node;
3674 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3675 /* Do not use STRIP_NOPS here. We do not want an enumerator
3676 whose value is 0 to count as a null pointer constant. */
3677 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3678 rhs = TREE_OPERAND (rhs, 0);
3682 /* Handle control structure constructs used as "lvalues". */
3684 switch (TREE_CODE (lhs))
3686 /* Handle (a, b) used as an "lvalue". */
3688 pedantic_lvalue_warning (COMPOUND_EXPR);
3689 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3691 if (TREE_CODE (newrhs) == ERROR_MARK)
3692 return error_mark_node;
3693 return build (COMPOUND_EXPR, lhstype,
3694 TREE_OPERAND (lhs, 0), newrhs);
3696 /* Handle (a ? b : c) used as an "lvalue". */
3698 pedantic_lvalue_warning (COND_EXPR);
3699 rhs = save_expr (rhs);
3701 /* Produce (a ? (b = rhs) : (c = rhs))
3702 except that the RHS goes through a save-expr
3703 so the code to compute it is only emitted once. */
3705 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3706 build_modify_expr (TREE_OPERAND (lhs, 1),
3708 build_modify_expr (TREE_OPERAND (lhs, 2),
3710 if (TREE_CODE (cond) == ERROR_MARK)
3712 /* Make sure the code to compute the rhs comes out
3713 before the split. */
3714 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3715 /* But cast it to void to avoid an "unused" error. */
3716 convert (void_type_node, rhs), cond);
3720 /* If a binary op has been requested, combine the old LHS value with the RHS
3721 producing the value we should actually store into the LHS. */
3723 if (modifycode != NOP_EXPR)
3725 lhs = stabilize_reference (lhs);
3726 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3729 /* Handle a cast used as an "lvalue".
3730 We have already performed any binary operator using the value as cast.
3731 Now convert the result to the cast type of the lhs,
3732 and then true type of the lhs and store it there;
3733 then convert result back to the cast type to be the value
3734 of the assignment. */
3736 switch (TREE_CODE (lhs))
3741 case FIX_TRUNC_EXPR:
3742 case FIX_FLOOR_EXPR:
3743 case FIX_ROUND_EXPR:
3745 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3746 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3747 newrhs = default_conversion (newrhs);
3749 tree inner_lhs = TREE_OPERAND (lhs, 0);
3751 result = build_modify_expr (inner_lhs, NOP_EXPR,
3752 convert (TREE_TYPE (inner_lhs),
3753 convert (lhstype, newrhs)));
3754 if (TREE_CODE (result) == ERROR_MARK)
3756 pedantic_lvalue_warning (CONVERT_EXPR);
3757 return convert (TREE_TYPE (lhs), result);
3761 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3762 Reject anything strange now. */
3764 if (!lvalue_or_else (lhs, "assignment"))
3765 return error_mark_node;
3767 /* Warn about storing in something that is `const'. */
3769 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3770 || ((TREE_CODE (lhstype) == RECORD_TYPE
3771 || TREE_CODE (lhstype) == UNION_TYPE)
3772 && C_TYPE_FIELDS_READONLY (lhstype)))
3773 readonly_warning (lhs, "assignment");
3775 /* If storing into a structure or union member,
3776 it has probably been given type `int'.
3777 Compute the type that would go with
3778 the actual amount of storage the member occupies. */
3780 if (TREE_CODE (lhs) == COMPONENT_REF
3781 && (TREE_CODE (lhstype) == INTEGER_TYPE
3782 || TREE_CODE (lhstype) == REAL_TYPE
3783 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3784 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3786 /* If storing in a field that is in actuality a short or narrower than one,
3787 we must store in the field in its actual type. */
3789 if (lhstype != TREE_TYPE (lhs))
3791 lhs = copy_node (lhs);
3792 TREE_TYPE (lhs) = lhstype;
3795 /* Convert new value to destination type. */
3797 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
3798 NULL_TREE, NULL_TREE, 0);
3799 if (TREE_CODE (newrhs) == ERROR_MARK)
3800 return error_mark_node;
3802 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3803 TREE_SIDE_EFFECTS (result) = 1;
3805 /* If we got the LHS in a different type for storing in,
3806 convert the result back to the nominal type of LHS
3807 so that the value we return always has the same type
3808 as the LHS argument. */
3810 if (olhstype == TREE_TYPE (result))
3812 return convert_for_assignment (olhstype, result, "assignment",
3813 NULL_TREE, NULL_TREE, 0);
3816 /* Convert value RHS to type TYPE as preparation for an assignment
3817 to an lvalue of type TYPE.
3818 The real work of conversion is done by `convert'.
3819 The purpose of this function is to generate error messages
3820 for assignments that are not allowed in C.
3821 ERRTYPE is a string to use in error messages:
3822 "assignment", "return", etc. If it is null, this is parameter passing
3823 for a function call (and different error messages are output). Otherwise,
3824 it may be a name stored in the spelling stack and interpreted by
3827 FUNNAME is the name of the function being called,
3828 as an IDENTIFIER_NODE, or null.
3829 PARMNUM is the number of the argument, for printing in error messages. */
3832 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3835 tree fundecl, funname;
3838 register enum tree_code codel = TREE_CODE (type);
3839 register tree rhstype;
3840 register enum tree_code coder;
3842 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3843 /* Do not use STRIP_NOPS here. We do not want an enumerator
3844 whose value is 0 to count as a null pointer constant. */
3845 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3846 rhs = TREE_OPERAND (rhs, 0);
3848 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3849 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3850 rhs = default_conversion (rhs);
3851 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3852 rhs = decl_constant_value (rhs);
3854 rhstype = TREE_TYPE (rhs);
3855 coder = TREE_CODE (rhstype);
3857 if (coder == ERROR_MARK)
3858 return error_mark_node;
3860 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3862 overflow_warning (rhs);
3863 /* Check for Objective-C protocols. This will issue a warning if
3864 there are protocol violations. No need to use the return value. */
3865 maybe_objc_comptypes (type, rhstype, 0);
3869 if (coder == VOID_TYPE)
3871 error ("void value not ignored as it ought to be");
3872 return error_mark_node;
3874 /* Arithmetic types all interconvert, and enum is treated like int. */
3875 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
3876 || codel == COMPLEX_TYPE)
3877 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
3878 || coder == COMPLEX_TYPE))
3879 return convert_and_check (type, rhs);
3881 /* Conversion to a union from its member types. */
3882 else if (codel == UNION_TYPE)
3886 for (memb_types = TYPE_FIELDS (type); memb_types;
3887 memb_types = TREE_CHAIN (memb_types))
3889 if (comptypes (TREE_TYPE (memb_types), TREE_TYPE (rhs)))
3892 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
3893 pedwarn ("ANSI C prohibits argument conversion to union type");
3894 return build1 (NOP_EXPR, type, rhs);
3897 else if (coder == POINTER_TYPE
3898 && TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE)
3900 tree memb_type = TREE_TYPE (memb_types);
3901 register tree ttl = TREE_TYPE (memb_type);
3902 register tree ttr = TREE_TYPE (rhstype);
3904 /* Any non-function converts to a [const][volatile] void *
3905 and vice versa; otherwise, targets must be the same.
3906 Meanwhile, the lhs target must have all the qualifiers of
3908 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3909 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3910 || comp_target_types (memb_type, rhstype))
3912 /* Const and volatile mean something different for function
3913 types, so the usual warnings are not appropriate. */
3914 if (TREE_CODE (ttr) != FUNCTION_TYPE
3915 || TREE_CODE (ttl) != FUNCTION_TYPE)
3917 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
3918 warn_for_assignment ("%s discards `const' from pointer target type",
3919 get_spelling (errtype), funname,
3921 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
3922 warn_for_assignment ("%s discards `volatile' from pointer target type",
3923 get_spelling (errtype), funname,
3928 /* Because const and volatile on functions are
3929 restrictions that say the function will not do
3930 certain things, it is okay to use a const or volatile
3931 function where an ordinary one is wanted, but not
3933 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
3934 warn_for_assignment ("%s makes `const *' function pointer from non-const",
3935 get_spelling (errtype), funname,
3937 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
3938 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
3939 get_spelling (errtype), funname,
3944 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
3945 pedwarn ("ANSI C prohibits argument conversion to union type");
3946 return build1 (NOP_EXPR, type, rhs);
3950 /* Can convert integer zero to any pointer type. */
3951 else if (TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE
3952 && (integer_zerop (rhs)
3953 || (TREE_CODE (rhs) == NOP_EXPR
3954 && integer_zerop (TREE_OPERAND (rhs, 0)))))
3955 return build1 (NOP_EXPR, type, null_pointer_node);
3959 /* Conversions among pointers */
3960 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
3962 register tree ttl = TREE_TYPE (type);
3963 register tree ttr = TREE_TYPE (rhstype);
3965 /* Any non-function converts to a [const][volatile] void *
3966 and vice versa; otherwise, targets must be the same.
3967 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3968 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3969 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3970 || comp_target_types (type, rhstype)
3971 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
3972 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3975 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
3976 && TREE_CODE (ttr) == FUNCTION_TYPE)
3978 (TYPE_MAIN_VARIANT (ttr) == void_type_node
3979 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3980 which are not ANSI null ptr constants. */
3981 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3982 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3983 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
3984 get_spelling (errtype), funname, parmnum);
3985 /* Const and volatile mean something different for function types,
3986 so the usual warnings are not appropriate. */
3987 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3988 || TREE_CODE (ttl) != FUNCTION_TYPE)
3990 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
3991 warn_for_assignment ("%s discards `const' from pointer target type",
3992 get_spelling (errtype), funname, parmnum);
3993 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
3994 warn_for_assignment ("%s discards `volatile' from pointer target type",
3995 get_spelling (errtype), funname, parmnum);
3996 /* If this is not a case of ignoring a mismatch in signedness,
3998 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3999 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4000 || comp_target_types (type, rhstype))
4002 /* If there is a mismatch, do warn. */
4004 warn_for_assignment ("pointer targets in %s differ in signedness",
4005 get_spelling (errtype), funname, parmnum);
4009 /* Because const and volatile on functions are restrictions
4010 that say the function will not do certain things,
4011 it is okay to use a const or volatile function
4012 where an ordinary one is wanted, but not vice-versa. */
4013 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4014 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4015 get_spelling (errtype), funname, parmnum);
4016 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4017 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4018 get_spelling (errtype), funname, parmnum);
4022 warn_for_assignment ("%s from incompatible pointer type",
4023 get_spelling (errtype), funname, parmnum);
4024 return convert (type, rhs);
4026 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4028 /* An explicit constant 0 can convert to a pointer,
4029 or one that results from arithmetic, even including
4030 a cast to integer type. */
4031 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4033 ! (TREE_CODE (rhs) == NOP_EXPR
4034 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4035 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4036 && integer_zerop (TREE_OPERAND (rhs, 0))))
4038 warn_for_assignment ("%s makes pointer from integer without a cast",
4039 get_spelling (errtype), funname, parmnum);
4040 return convert (type, rhs);
4042 return null_pointer_node;
4044 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4046 warn_for_assignment ("%s makes integer from pointer without a cast",
4047 get_spelling (errtype), funname, parmnum);
4048 return convert (type, rhs);
4055 tree selector = maybe_building_objc_message_expr ();
4057 if (selector && parmnum > 2)
4058 error ("incompatible type for argument %d of `%s'",
4059 parmnum - 2, IDENTIFIER_POINTER (selector));
4061 error ("incompatible type for argument %d of `%s'",
4062 parmnum, IDENTIFIER_POINTER (funname));
4065 error ("incompatible type for argument %d of indirect function call",
4069 error ("incompatible types in %s", get_spelling (errtype));
4071 return error_mark_node;
4074 /* Print a warning using MSG.
4075 It gets OPNAME as its one parameter.
4076 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4077 FUNCTION and ARGNUM are handled specially if we are building an
4078 Objective-C selector. */
4081 warn_for_assignment (msg, opname, function, argnum)
4087 static char argstring[] = "passing arg %d of `%s'";
4088 static char argnofun[] = "passing arg %d";
4092 tree selector = maybe_building_objc_message_expr ();
4094 if (selector && argnum > 2)
4096 function = selector;
4101 /* Function name is known; supply it. */
4102 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4103 + sizeof (argstring) + 25 /*%d*/ + 1);
4104 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4108 /* Function name unknown (call through ptr); just give arg number. */
4109 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4110 sprintf (opname, argnofun, argnum);
4113 pedwarn (msg, opname);
4116 /* Return nonzero if VALUE is a valid constant-valued expression
4117 for use in initializing a static variable; one that can be an
4118 element of a "constant" initializer.
4120 Return null_pointer_node if the value is absolute;
4121 if it is relocatable, return the variable that determines the relocation.
4122 We assume that VALUE has been folded as much as possible;
4123 therefore, we do not need to check for such things as
4124 arithmetic-combinations of integers. */
4127 initializer_constant_valid_p (value, endtype)
4131 switch (TREE_CODE (value))
4134 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4135 && TREE_CONSTANT (value))
4137 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
4140 return TREE_STATIC (value) ? null_pointer_node : 0;
4146 return null_pointer_node;
4149 return TREE_OPERAND (value, 0);
4151 case NON_LVALUE_EXPR:
4152 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4156 /* Allow conversions between pointer types. */
4157 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4158 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
4159 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4161 /* Allow conversions between real types. */
4162 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4163 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
4164 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4166 /* Allow length-preserving conversions between integer types. */
4167 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4168 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4169 && (TYPE_PRECISION (TREE_TYPE (value))
4170 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4171 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4173 /* Allow conversions between other integer types only if
4175 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4176 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4178 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4180 if (inner == null_pointer_node)
4181 return null_pointer_node;
4185 /* Allow (int) &foo provided int is as wide as a pointer. */
4186 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4187 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
4188 && (TYPE_PRECISION (TREE_TYPE (value))
4189 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4190 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4193 /* Likewise conversions from int to pointers. */
4194 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4195 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4196 && (TYPE_PRECISION (TREE_TYPE (value))
4197 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4198 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4201 /* Allow conversions to union types if the value inside is okay. */
4202 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4203 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4208 if (TREE_CODE (endtype) == INTEGER_TYPE
4209 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4212 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4214 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4216 /* If either term is absolute, use the other terms relocation. */
4217 if (valid0 == null_pointer_node)
4219 if (valid1 == null_pointer_node)
4225 if (TREE_CODE (endtype) == INTEGER_TYPE
4226 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4229 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4231 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4233 /* Win if second argument is absolute. */
4234 if (valid1 == null_pointer_node)
4236 /* Win if both arguments have the same relocation.
4237 Then the value is absolute. */
4238 if (valid0 == valid1)
4239 return null_pointer_node;
4247 /* If VALUE is a compound expr all of whose expressions are constant, then
4248 return its value. Otherwise, return error_mark_node.
4250 This is for handling COMPOUND_EXPRs as initializer elements
4251 which is allowed with a warning when -pedantic is specified. */
4254 valid_compound_expr_initializer (value, endtype)
4258 if (TREE_CODE (value) == COMPOUND_EXPR)
4260 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4262 return error_mark_node;
4263 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4266 else if (! TREE_CONSTANT (value)
4267 && ! initializer_constant_valid_p (value, endtype))
4268 return error_mark_node;
4273 /* Perform appropriate conversions on the initial value of a variable,
4274 store it in the declaration DECL,
4275 and print any error messages that are appropriate.
4276 If the init is invalid, store an ERROR_MARK. */
4279 store_init_value (decl, init)
4282 register tree value, type;
4284 /* If variable's type was invalidly declared, just ignore it. */
4286 type = TREE_TYPE (decl);
4287 if (TREE_CODE (type) == ERROR_MARK)
4290 /* Digest the specified initializer into an expression. */
4292 value = digest_init (type, init, TREE_STATIC (decl),
4293 TREE_STATIC (decl) || pedantic);
4295 /* Store the expression if valid; else report error. */
4298 /* Note that this is the only place we can detect the error
4299 in a case such as struct foo bar = (struct foo) { x, y };
4300 where there is one initial value which is a constructor expression. */
4301 if (value == error_mark_node)
4303 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4305 error ("initializer for static variable is not constant");
4306 value = error_mark_node;
4308 else if (TREE_STATIC (decl)
4309 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4311 error ("initializer for static variable uses complicated arithmetic");
4312 value = error_mark_node;
4316 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4318 if (! TREE_CONSTANT (value))
4319 pedwarn ("aggregate initializer is not constant");
4320 else if (! TREE_STATIC (value))
4321 pedwarn ("aggregate initializer uses complicated arithmetic");
4326 DECL_INITIAL (decl) = value;
4328 /* ANSI wants warnings about out-of-range constant initializers. */
4329 STRIP_TYPE_NOPS (value);
4330 constant_expression_warning (value);
4333 /* Methods for storing and printing names for error messages. */
4335 /* Implement a spelling stack that allows components of a name to be pushed
4336 and popped. Each element on the stack is this structure. */
4348 #define SPELLING_STRING 1
4349 #define SPELLING_MEMBER 2
4350 #define SPELLING_BOUNDS 3
4352 static struct spelling *spelling; /* Next stack element (unused). */
4353 static struct spelling *spelling_base; /* Spelling stack base. */
4354 static int spelling_size; /* Size of the spelling stack. */
4356 /* Macros to save and restore the spelling stack around push_... functions.
4357 Alternative to SAVE_SPELLING_STACK. */
4359 #define SPELLING_DEPTH() (spelling - spelling_base)
4360 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4362 /* Save and restore the spelling stack around arbitrary C code. */
4364 #define SAVE_SPELLING_DEPTH(code) \
4366 int __depth = SPELLING_DEPTH (); \
4368 RESTORE_SPELLING_DEPTH (__depth); \
4371 /* Push an element on the spelling stack with type KIND and assign VALUE
4374 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4376 int depth = SPELLING_DEPTH (); \
4378 if (depth >= spelling_size) \
4380 spelling_size += 10; \
4381 if (spelling_base == 0) \
4383 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4386 = (struct spelling *) xrealloc (spelling_base, \
4387 spelling_size * sizeof (struct spelling)); \
4388 RESTORE_SPELLING_DEPTH (depth); \
4391 spelling->kind = (KIND); \
4392 spelling->MEMBER = (VALUE); \
4396 /* Push STRING on the stack. Printed literally. */
4399 push_string (string)
4402 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4405 /* Push a member name on the stack. Printed as '.' STRING. */
4408 push_member_name (decl)
4413 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4414 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4417 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4420 push_array_bounds (bounds)
4423 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4426 /* Compute the maximum size in bytes of the printed spelling. */
4431 register int size = 0;
4432 register struct spelling *p;
4434 for (p = spelling_base; p < spelling; p++)
4436 if (p->kind == SPELLING_BOUNDS)
4439 size += strlen (p->u.s) + 1;
4445 /* Print the spelling to BUFFER and return it. */
4448 print_spelling (buffer)
4449 register char *buffer;
4451 register char *d = buffer;
4453 register struct spelling *p;
4455 for (p = spelling_base; p < spelling; p++)
4456 if (p->kind == SPELLING_BOUNDS)
4458 sprintf (d, "[%d]", p->u.i);
4463 if (p->kind == SPELLING_MEMBER)
4465 for (s = p->u.s; *d = *s++; d++)
4472 /* Provide a means to pass component names derived from the spelling stack. */
4474 char initialization_message;
4476 /* Interpret the spelling of the given ERRTYPE message. */
4479 get_spelling (errtype)
4482 static char *buffer;
4483 static int size = -1;
4485 if (errtype == &initialization_message)
4487 /* Avoid counting chars */
4488 static char message[] = "initialization of `%s'";
4489 register int needed = sizeof (message) + spelling_length () + 1;
4493 buffer = (char *) xmalloc (size = needed);
4495 buffer = (char *) xrealloc (buffer, size = needed);
4497 temp = (char *) alloca (needed);
4498 sprintf (buffer, message, print_spelling (temp));
4505 /* Issue an error message for a bad initializer component.
4506 FORMAT describes the message. OFWHAT is the name for the component.
4507 LOCAL is a format string for formatting the insertion of the name
4510 If OFWHAT is null, the component name is stored on the spelling stack.
4511 If the component name is a null string, then LOCAL is omitted entirely. */
4514 error_init (format, local, ofwhat)
4515 char *format, *local, *ofwhat;
4520 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4521 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4524 sprintf (buffer, local, ofwhat);
4528 error (format, buffer);
4531 /* Issue a pedantic warning for a bad initializer component.
4532 FORMAT describes the message. OFWHAT is the name for the component.
4533 LOCAL is a format string for formatting the insertion of the name
4536 If OFWHAT is null, the component name is stored on the spelling stack.
4537 If the component name is a null string, then LOCAL is omitted entirely. */
4540 pedwarn_init (format, local, ofwhat)
4541 char *format, *local, *ofwhat;
4546 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4547 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4550 sprintf (buffer, local, ofwhat);
4554 pedwarn (format, buffer);
4557 /* Issue a warning for a bad initializer component.
4558 FORMAT describes the message. OFWHAT is the name for the component.
4559 LOCAL is a format string for formatting the insertion of the name
4562 If OFWHAT is null, the component name is stored on the spelling stack.
4563 If the component name is a null string, then LOCAL is omitted entirely. */
4566 warning_init (format, local, ofwhat)
4567 char *format, *local, *ofwhat;
4572 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4573 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4576 sprintf (buffer, local, ofwhat);
4580 warning (format, buffer);
4583 /* Digest the parser output INIT as an initializer for type TYPE.
4584 Return a C expression of type TYPE to represent the initial value.
4586 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4587 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4588 applies only to elements of constructors. */
4591 digest_init (type, init, require_constant, constructor_constant)
4593 int require_constant, constructor_constant;
4595 enum tree_code code = TREE_CODE (type);
4596 tree inside_init = init;
4598 if (init == error_mark_node)
4601 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4602 /* Do not use STRIP_NOPS here. We do not want an enumerator
4603 whose value is 0 to count as a null pointer constant. */
4604 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4605 inside_init = TREE_OPERAND (init, 0);
4607 /* Initialization of an array of chars from a string constant
4608 optionally enclosed in braces. */
4610 if (code == ARRAY_TYPE)
4612 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4613 if ((typ1 == char_type_node
4614 || typ1 == signed_char_type_node
4615 || typ1 == unsigned_char_type_node
4616 || typ1 == unsigned_wchar_type_node
4617 || typ1 == signed_wchar_type_node)
4618 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4620 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4621 TYPE_MAIN_VARIANT (type)))
4624 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4626 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4628 error_init ("char-array%s initialized from wide string",
4630 return error_mark_node;
4632 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4634 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4636 error_init ("int-array%s initialized from non-wide string",
4638 return error_mark_node;
4641 TREE_TYPE (inside_init) = type;
4642 if (TYPE_DOMAIN (type) != 0
4643 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4645 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4646 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4647 /* Subtract 1 (or sizeof (wchar_t))
4648 because it's ok to ignore the terminating null char
4649 that is counted in the length of the constant. */
4650 if (size < TREE_STRING_LENGTH (inside_init)
4651 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4652 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4655 "initializer-string for array of chars%s is too long",
4662 /* Any type can be initialized
4663 from an expression of the same type, optionally with braces. */
4665 if (inside_init && TREE_TYPE (inside_init) != 0
4666 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4667 TYPE_MAIN_VARIANT (type))
4668 || (code == ARRAY_TYPE
4669 && comptypes (TREE_TYPE (inside_init), type))
4670 || (code == POINTER_TYPE
4671 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4672 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4673 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4674 TREE_TYPE (type)))))
4676 if (code == POINTER_TYPE
4677 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4678 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4679 inside_init = default_conversion (inside_init);
4680 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4681 && TREE_CODE (inside_init) != CONSTRUCTOR)
4683 error_init ("array%s initialized from non-constant array expression",
4685 return error_mark_node;
4688 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4689 inside_init = decl_constant_value (inside_init);
4691 /* Compound expressions can only occur here if -pedantic or
4692 -pedantic-errors is specified. In the later case, we always want
4693 an error. In the former case, we simply want a warning. */
4694 if (require_constant && pedantic
4695 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4698 = valid_compound_expr_initializer (inside_init,
4699 TREE_TYPE (inside_init));
4700 if (inside_init == error_mark_node)
4701 error_init ("initializer element%s is not constant",
4704 pedwarn_init ("initializer element%s is not constant",
4706 if (flag_pedantic_errors)
4707 inside_init = error_mark_node;
4709 else if (require_constant && ! TREE_CONSTANT (inside_init))
4711 error_init ("initializer element%s is not constant",
4713 inside_init = error_mark_node;
4715 else if (require_constant
4716 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4718 error_init ("initializer element%s is not computable at load time",
4720 inside_init = error_mark_node;
4726 /* Handle scalar types, including conversions. */
4728 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4729 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4731 /* Note that convert_for_assignment calls default_conversion
4732 for arrays and functions. We must not call it in the
4733 case where inside_init is a null pointer constant. */
4735 = convert_for_assignment (type, init, "initialization",
4736 NULL_TREE, NULL_TREE, 0);
4738 if (require_constant && ! TREE_CONSTANT (inside_init))
4740 error_init ("initializer element%s is not constant",
4742 inside_init = error_mark_node;
4744 else if (require_constant
4745 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4747 error_init ("initializer element%s is not computable at load time",
4749 inside_init = error_mark_node;
4755 /* Come here only for records and arrays. */
4757 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4759 error_init ("variable-sized object%s may not be initialized",
4761 return error_mark_node;
4764 /* Traditionally, you can write struct foo x = 0;
4765 and it initializes the first element of x to 0. */
4766 if (flag_traditional)
4768 tree top = 0, prev = 0;
4769 while (TREE_CODE (type) == RECORD_TYPE
4770 || TREE_CODE (type) == ARRAY_TYPE
4771 || TREE_CODE (type) == QUAL_UNION_TYPE
4772 || TREE_CODE (type) == UNION_TYPE)
4774 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4778 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4780 if (TREE_CODE (type) == ARRAY_TYPE)
4781 type = TREE_TYPE (type);
4782 else if (TYPE_FIELDS (type))
4783 type = TREE_TYPE (TYPE_FIELDS (type));
4786 error_init ("invalid initializer%s", " for `%s'", NULL);
4787 return error_mark_node;
4790 TREE_OPERAND (prev, 1)
4791 = build_tree_list (NULL_TREE,
4792 digest_init (type, init, require_constant,
4793 constructor_constant));
4796 error_init ("invalid initializer%s", " for `%s'", NULL);
4797 return error_mark_node;
4800 /* Handle initializers that use braces. */
4802 /* Type of object we are accumulating a constructor for.
4803 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4804 static tree constructor_type;
4806 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4808 static tree constructor_fields;
4810 /* For an ARRAY_TYPE, this is the specified index
4811 at which to store the next element we get.
4812 This is a special INTEGER_CST node that we modify in place. */
4813 static tree constructor_index;
4815 /* For an ARRAY_TYPE, this is the end index of the range
4816 to intitialize with the next element, or NULL in the ordinary case
4817 where the element is used just once. */
4818 static tree constructor_range_end;
4820 /* For an ARRAY_TYPE, this is the maximum index. */
4821 static tree constructor_max_index;
4823 /* For a RECORD_TYPE, this is the first field not yet written out. */
4824 static tree constructor_unfilled_fields;
4826 /* For an ARRAY_TYPE, this is the index of the first element
4827 not yet written out.
4828 This is a special INTEGER_CST node that we modify in place. */
4829 static tree constructor_unfilled_index;
4831 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4832 This is so we can generate gaps between fields, when appropriate.
4833 This is a special INTEGER_CST node that we modify in place. */
4834 static tree constructor_bit_index;
4836 /* If we are saving up the elements rather than allocating them,
4837 this is the list of elements so far (in reverse order,
4838 most recent first). */
4839 static tree constructor_elements;
4841 /* 1 if so far this constructor's elements are all compile-time constants. */
4842 static int constructor_constant;
4844 /* 1 if so far this constructor's elements are all valid address constants. */
4845 static int constructor_simple;
4847 /* 1 if this constructor is erroneous so far. */
4848 static int constructor_erroneous;
4850 /* 1 if have called defer_addressed_constants. */
4851 static int constructor_subconstants_deferred;
4853 /* List of pending elements at this constructor level.
4854 These are elements encountered out of order
4855 which belong at places we haven't reached yet in actually
4856 writing the output. */
4857 static tree constructor_pending_elts;
4859 /* The SPELLING_DEPTH of this constructor. */
4860 static int constructor_depth;
4862 /* 0 if implicitly pushing constructor levels is allowed. */
4863 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4865 /* 1 if this constructor level was entered implicitly. */
4866 static int constructor_implicit;
4868 static int require_constant_value;
4869 static int require_constant_elements;
4871 /* 1 if it is ok to output this constructor as we read it.
4872 0 means must accumulate a CONSTRUCTOR expression. */
4873 static int constructor_incremental;
4875 /* DECL node for which an initializer is being read.
4876 0 means we are reading a constructor expression
4877 such as (struct foo) {...}. */
4878 static tree constructor_decl;
4880 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4881 static char *constructor_asmspec;
4883 /* Nonzero if this is an initializer for a top-level decl. */
4884 static int constructor_top_level;
4886 /* When we finish reading a constructor expression
4887 (constructor_decl is 0), the CONSTRUCTOR goes here. */
4888 static tree constructor_result;
4890 /* This stack has a level for each implicit or explicit level of
4891 structuring in the initializer, including the outermost one. It
4892 saves the values of most of the variables above. */
4894 struct constructor_stack
4896 struct constructor_stack *next;
4902 tree unfilled_index;
4903 tree unfilled_fields;
4909 /* If nonzero, this value should replace the entire
4910 constructor at this level. */
4911 tree replacement_value;
4920 struct constructor_stack *constructor_stack;
4922 /* This stack records separate initializers that are nested.
4923 Nested initializers can't happen in ANSI C, but GNU C allows them
4924 in cases like { ... (struct foo) { ... } ... }. */
4926 struct initializer_stack
4928 struct initializer_stack *next;
4931 struct constructor_stack *constructor_stack;
4933 struct spelling *spelling;
4934 struct spelling *spelling_base;
4938 char require_constant_value;
4939 char require_constant_elements;
4943 struct initializer_stack *initializer_stack;
4945 /* Prepare to parse and output the initializer for variable DECL. */
4948 start_init (decl, asmspec_tree, top_level)
4954 struct initializer_stack *p
4955 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
4959 asmspec = TREE_STRING_POINTER (asmspec_tree);
4961 p->decl = constructor_decl;
4962 p->asmspec = constructor_asmspec;
4963 p->incremental = constructor_incremental;
4964 p->require_constant_value = require_constant_value;
4965 p->require_constant_elements = require_constant_elements;
4966 p->constructor_stack = constructor_stack;
4967 p->elements = constructor_elements;
4968 p->spelling = spelling;
4969 p->spelling_base = spelling_base;
4970 p->spelling_size = spelling_size;
4971 p->deferred = constructor_subconstants_deferred;
4972 p->top_level = constructor_top_level;
4973 p->next = initializer_stack;
4974 initializer_stack = p;
4976 constructor_decl = decl;
4977 constructor_incremental = top_level;
4978 constructor_asmspec = asmspec;
4979 constructor_subconstants_deferred = 0;
4980 constructor_top_level = top_level;
4984 require_constant_value = TREE_STATIC (decl);
4985 require_constant_elements
4986 = ((TREE_STATIC (decl) || pedantic)
4987 /* For a scalar, you can always use any value to initialize,
4988 even within braces. */
4989 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4990 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4991 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4992 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4993 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4994 constructor_incremental |= TREE_STATIC (decl);
4998 require_constant_value = 0;
4999 require_constant_elements = 0;
5000 locus = "(anonymous)";
5003 constructor_stack = 0;
5005 missing_braces_mentioned = 0;
5009 RESTORE_SPELLING_DEPTH (0);
5012 push_string (locus);
5018 struct initializer_stack *p = initializer_stack;
5020 /* Output subconstants (string constants, usually)
5021 that were referenced within this initializer and saved up.
5022 Must do this if and only if we called defer_addressed_constants. */
5023 if (constructor_subconstants_deferred)
5024 output_deferred_addressed_constants ();
5026 /* Free the whole constructor stack of this initializer. */
5027 while (constructor_stack)
5029 struct constructor_stack *q = constructor_stack;
5030 constructor_stack = q->next;
5034 /* Pop back to the data of the outer initializer (if any). */
5035 constructor_decl = p->decl;
5036 constructor_asmspec = p->asmspec;
5037 constructor_incremental = p->incremental;
5038 require_constant_value = p->require_constant_value;
5039 require_constant_elements = p->require_constant_elements;
5040 constructor_stack = p->constructor_stack;
5041 constructor_elements = p->elements;
5042 spelling = p->spelling;
5043 spelling_base = p->spelling_base;
5044 spelling_size = p->spelling_size;
5045 constructor_subconstants_deferred = p->deferred;
5046 constructor_top_level = p->top_level;
5047 initializer_stack = p->next;
5051 /* Call here when we see the initializer is surrounded by braces.
5052 This is instead of a call to push_init_level;
5053 it is matched by a call to pop_init_level.
5055 TYPE is the type to initialize, for a constructor expression.
5056 For an initializer for a decl, TYPE is zero. */
5059 really_start_incremental_init (type)
5062 struct constructor_stack *p
5063 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5066 type = TREE_TYPE (constructor_decl);
5068 /* Turn off constructor_incremental if type is a struct with bitfields.
5069 Do this before the first push, so that the corrected value
5070 is available in finish_init. */
5071 check_init_type_bitfields (type);
5073 p->type = constructor_type;
5074 p->fields = constructor_fields;
5075 p->index = constructor_index;
5076 p->range_end = constructor_range_end;
5077 p->max_index = constructor_max_index;
5078 p->unfilled_index = constructor_unfilled_index;
5079 p->unfilled_fields = constructor_unfilled_fields;
5080 p->bit_index = constructor_bit_index;
5081 p->elements = constructor_elements;
5082 p->constant = constructor_constant;
5083 p->simple = constructor_simple;
5084 p->erroneous = constructor_erroneous;
5085 p->pending_elts = constructor_pending_elts;
5086 p->depth = constructor_depth;
5087 p->replacement_value = 0;
5089 p->incremental = constructor_incremental;
5092 constructor_stack = p;
5094 constructor_constant = 1;
5095 constructor_simple = 1;
5096 constructor_depth = SPELLING_DEPTH ();
5097 constructor_elements = 0;
5098 constructor_pending_elts = 0;
5099 constructor_type = type;
5101 if (TREE_CODE (constructor_type) == RECORD_TYPE
5102 || TREE_CODE (constructor_type) == UNION_TYPE)
5104 constructor_fields = TYPE_FIELDS (constructor_type);
5105 /* Skip any nameless bit fields atthe beginning. */
5106 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5107 && DECL_NAME (constructor_fields) == 0)
5108 constructor_fields = TREE_CHAIN (constructor_fields);
5109 constructor_unfilled_fields = constructor_fields;
5110 constructor_bit_index = copy_node (integer_zero_node);
5112 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5114 constructor_range_end = 0;
5115 if (TYPE_DOMAIN (constructor_type))
5117 constructor_max_index
5118 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5120 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5123 constructor_index = copy_node (integer_zero_node);
5124 constructor_unfilled_index = copy_node (constructor_index);
5128 /* Handle the case of int x = {5}; */
5129 constructor_fields = constructor_type;
5130 constructor_unfilled_fields = constructor_type;
5133 if (constructor_incremental)
5135 int momentary = suspend_momentary ();
5136 push_obstacks_nochange ();
5137 if (TREE_PERMANENT (constructor_decl))
5138 end_temporary_allocation ();
5139 make_decl_rtl (constructor_decl, constructor_asmspec,
5140 constructor_top_level);
5141 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5143 resume_momentary (momentary);
5146 if (constructor_incremental)
5148 defer_addressed_constants ();
5149 constructor_subconstants_deferred = 1;
5153 /* Push down into a subobject, for initialization.
5154 If this is for an explicit set of braces, IMPLICIT is 0.
5155 If it is because the next element belongs at a lower level,
5159 push_init_level (implicit)
5162 struct constructor_stack *p;
5164 /* If we've exhausted any levels that didn't have braces,
5166 while (constructor_stack->implicit)
5168 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5169 || TREE_CODE (constructor_type) == UNION_TYPE)
5170 && constructor_fields == 0)
5171 process_init_element (pop_init_level (1));
5172 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5173 && tree_int_cst_lt (constructor_max_index, constructor_index))
5174 process_init_element (pop_init_level (1));
5179 /* Structure elements may require alignment. Do this now
5180 if necessary for the subaggregate. */
5181 if (constructor_incremental && constructor_type != 0
5182 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields)
5184 /* Advance to offset of this element. */
5185 if (! tree_int_cst_equal (constructor_bit_index,
5186 DECL_FIELD_BITPOS (constructor_fields)))
5188 int next = (TREE_INT_CST_LOW
5189 (DECL_FIELD_BITPOS (constructor_fields))
5191 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5194 assemble_zeros (next - here);
5198 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5199 p->type = constructor_type;
5200 p->fields = constructor_fields;
5201 p->index = constructor_index;
5202 p->range_end = constructor_range_end;
5203 p->max_index = constructor_max_index;
5204 p->unfilled_index = constructor_unfilled_index;
5205 p->unfilled_fields = constructor_unfilled_fields;
5206 p->bit_index = constructor_bit_index;
5207 p->elements = constructor_elements;
5208 p->constant = constructor_constant;
5209 p->simple = constructor_simple;
5210 p->erroneous = constructor_erroneous;
5211 p->pending_elts = constructor_pending_elts;
5212 p->depth = constructor_depth;
5213 p->replacement_value = 0;
5214 p->implicit = implicit;
5215 p->incremental = constructor_incremental;
5217 p->next = constructor_stack;
5218 constructor_stack = p;
5220 constructor_constant = 1;
5221 constructor_simple = 1;
5222 constructor_depth = SPELLING_DEPTH ();
5223 constructor_elements = 0;
5224 constructor_pending_elts = 0;
5226 /* Don't die if an entire brace-pair level is superfluous
5227 in the containing level. */
5228 if (constructor_type == 0)
5230 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5231 || TREE_CODE (constructor_type) == UNION_TYPE)
5233 /* Don't die if there are extra init elts at the end. */
5234 if (constructor_fields == 0)
5235 constructor_type = 0;
5238 constructor_type = TREE_TYPE (constructor_fields);
5239 push_member_name (constructor_fields);
5240 if (constructor_fields != constructor_unfilled_fields)
5241 constructor_incremental = 0;
5244 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5246 constructor_type = TREE_TYPE (constructor_type);
5247 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5248 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5249 || constructor_range_end != 0)
5250 constructor_incremental = 0;
5253 if (constructor_type == 0)
5255 error_init ("extra brace group at end of initializer%s",
5257 constructor_fields = 0;
5258 constructor_unfilled_fields = 0;
5262 /* Turn off constructor_incremental if type is a struct with bitfields. */
5263 check_init_type_bitfields (constructor_type);
5265 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5267 missing_braces_mentioned = 1;
5268 warning_init ("missing braces around initializer%s", " for `%s'", NULL);
5271 if (TREE_CODE (constructor_type) == RECORD_TYPE
5272 || TREE_CODE (constructor_type) == UNION_TYPE)
5274 constructor_fields = TYPE_FIELDS (constructor_type);
5275 /* Skip any nameless bit fields atthe beginning. */
5276 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5277 && DECL_NAME (constructor_fields) == 0)
5278 constructor_fields = TREE_CHAIN (constructor_fields);
5279 constructor_unfilled_fields = constructor_fields;
5280 constructor_bit_index = copy_node (integer_zero_node);
5282 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5284 constructor_range_end = 0;
5285 if (TYPE_DOMAIN (constructor_type))
5287 constructor_max_index
5288 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5290 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5293 constructor_index = copy_node (integer_zero_node);
5294 constructor_unfilled_index = copy_node (constructor_index);
5298 warning_init ("braces around scalar initializer%s", " for `%s'", NULL);
5299 constructor_fields = constructor_type;
5300 constructor_unfilled_fields = constructor_type;
5304 /* Don't read a struct incrementally if it has any bitfields,
5305 because the incremental reading code doesn't know how to
5306 handle bitfields yet. */
5309 check_init_type_bitfields (type)
5312 if (TREE_CODE (type) == RECORD_TYPE)
5315 for (tail = TYPE_FIELDS (type); tail;
5316 tail = TREE_CHAIN (tail))
5318 if (DECL_BIT_FIELD (tail)
5319 /* This catches cases like `int foo : 8;'. */
5320 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail)))
5322 constructor_incremental = 0;
5326 check_init_type_bitfields (TREE_TYPE (tail));
5330 else if (TREE_CODE (type) == ARRAY_TYPE)
5331 check_init_type_bitfields (TREE_TYPE (type));
5334 /* At the end of an implicit or explicit brace level,
5335 finish up that level of constructor.
5336 If we were outputting the elements as they are read, return 0
5337 from inner levels (process_init_element ignores that),
5338 but return error_mark_node from the outermost level
5339 (that's what we want to put in DECL_INITIAL).
5340 Otherwise, return a CONSTRUCTOR expression. */
5343 pop_init_level (implicit)
5346 struct constructor_stack *p;
5348 tree constructor = 0;
5352 /* When we come to an explicit close brace,
5353 pop any inner levels that didn't have explicit braces. */
5354 while (constructor_stack->implicit)
5355 process_init_element (pop_init_level (1));
5358 p = constructor_stack;
5360 if (constructor_type != 0)
5361 size = int_size_in_bytes (constructor_type);
5363 /* Now output all pending elements. */
5364 output_pending_init_elements (1);
5366 #if 0 /* c-parse.in warns about {}. */
5367 /* In ANSI, each brace level must have at least one element. */
5368 if (! implicit && pedantic
5369 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5370 ? integer_zerop (constructor_unfilled_index)
5371 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5372 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL);
5375 /* Pad out the end of the structure. */
5377 if (p->replacement_value)
5379 /* If this closes a superfluous brace pair,
5380 just pass out the element between them. */
5381 constructor = p->replacement_value;
5382 /* If this is the top level thing within the initializer,
5383 and it's for a variable, then since we already called
5384 assemble_variable, we must output the value now. */
5385 if (p->next == 0 && constructor_decl != 0
5386 && constructor_incremental)
5388 constructor = digest_init (constructor_type, constructor,
5391 /* If initializing an array of unknown size,
5392 determine the size now. */
5393 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5394 && TYPE_DOMAIN (constructor_type) == 0)
5399 push_obstacks_nochange ();
5400 if (TREE_PERMANENT (constructor_type))
5401 end_temporary_allocation ();
5403 momentary_p = suspend_momentary ();
5405 /* We shouldn't have an incomplete array type within
5407 if (constructor_stack->next)
5411 = complete_array_type (constructor_type,
5416 size = int_size_in_bytes (constructor_type);
5417 resume_momentary (momentary_p);
5421 output_constant (constructor, size);
5424 else if (constructor_type == 0)
5426 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5427 && TREE_CODE (constructor_type) != UNION_TYPE
5428 && TREE_CODE (constructor_type) != ARRAY_TYPE
5429 && ! constructor_incremental)
5431 /* A nonincremental scalar initializer--just return
5432 the element, after verifying there is just one. */
5433 if (constructor_elements == 0)
5435 error_init ("empty scalar initializer%s",
5437 constructor = error_mark_node;
5439 else if (TREE_CHAIN (constructor_elements) != 0)
5441 error_init ("extra elements in scalar initializer%s",
5443 constructor = TREE_VALUE (constructor_elements);
5446 constructor = TREE_VALUE (constructor_elements);
5448 else if (! constructor_incremental)
5450 if (constructor_erroneous)
5451 constructor = error_mark_node;
5454 int momentary = suspend_momentary ();
5456 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5457 nreverse (constructor_elements));
5458 if (constructor_constant)
5459 TREE_CONSTANT (constructor) = 1;
5460 if (constructor_constant && constructor_simple)
5461 TREE_STATIC (constructor) = 1;
5463 resume_momentary (momentary);
5469 int momentary = suspend_momentary ();
5471 if (TREE_CODE (constructor_type) == RECORD_TYPE
5472 || TREE_CODE (constructor_type) == UNION_TYPE)
5474 /* Find the offset of the end of that field. */
5475 filled = size_binop (CEIL_DIV_EXPR,
5476 constructor_bit_index,
5477 size_int (BITS_PER_UNIT));
5479 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5481 /* If initializing an array of unknown size,
5482 determine the size now. */
5483 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5484 && TYPE_DOMAIN (constructor_type) == 0)
5487 = size_binop (MINUS_EXPR,
5488 constructor_unfilled_index,
5491 push_obstacks_nochange ();
5492 if (TREE_PERMANENT (constructor_type))
5493 end_temporary_allocation ();
5494 maxindex = copy_node (maxindex);
5495 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5496 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5498 /* TYPE_MAX_VALUE is always one less than the number of elements
5499 in the array, because we start counting at zero. Therefore,
5500 warn only if the value is less than zero. */
5502 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5504 error_with_decl (constructor_decl,
5505 "zero or negative array size `%s'");
5506 layout_type (constructor_type);
5507 size = int_size_in_bytes (constructor_type);
5511 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5512 size_in_bytes (TREE_TYPE (constructor_type)));
5518 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5520 resume_momentary (momentary);
5524 constructor_type = p->type;
5525 constructor_fields = p->fields;
5526 constructor_index = p->index;
5527 constructor_range_end = p->range_end;
5528 constructor_max_index = p->max_index;
5529 constructor_unfilled_index = p->unfilled_index;
5530 constructor_unfilled_fields = p->unfilled_fields;
5531 constructor_bit_index = p->bit_index;
5532 constructor_elements = p->elements;
5533 constructor_constant = p->constant;
5534 constructor_simple = p->simple;
5535 constructor_erroneous = p->erroneous;
5536 constructor_pending_elts = p->pending_elts;
5537 constructor_depth = p->depth;
5538 constructor_incremental = p->incremental;
5539 RESTORE_SPELLING_DEPTH (constructor_depth);
5541 constructor_stack = p->next;
5544 if (constructor == 0)
5546 if (constructor_stack == 0)
5547 return error_mark_node;
5553 /* Within an array initializer, specify the next index to be initialized.
5554 FIRST is that index. If LAST is nonzero, then initialize a range
5555 of indices, running from FIRST through LAST. */
5558 set_init_index (first, last)
5561 while ((TREE_CODE (first) == NOP_EXPR
5562 || TREE_CODE (first) == CONVERT_EXPR
5563 || TREE_CODE (first) == NON_LVALUE_EXPR)
5564 && (TYPE_MODE (TREE_TYPE (first))
5565 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5566 (first) = TREE_OPERAND (first, 0);
5568 while ((TREE_CODE (last) == NOP_EXPR
5569 || TREE_CODE (last) == CONVERT_EXPR
5570 || TREE_CODE (last) == NON_LVALUE_EXPR)
5571 && (TYPE_MODE (TREE_TYPE (last))
5572 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5573 (last) = TREE_OPERAND (last, 0);
5575 if (TREE_CODE (first) != INTEGER_CST)
5576 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5577 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5578 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5579 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5580 error_init ("duplicate array index in initializer%s", " for `%s'", NULL);
5583 TREE_INT_CST_LOW (constructor_index)
5584 = TREE_INT_CST_LOW (first);
5585 TREE_INT_CST_HIGH (constructor_index)
5586 = TREE_INT_CST_HIGH (first);
5588 if (last != 0 && tree_int_cst_lt (last, first))
5589 error_init ("empty index range in initializer%s", " for `%s'", NULL);
5593 pedwarn ("ANSI C forbids specifying element to initialize");
5594 constructor_range_end = last;
5599 /* Within a struct initializer, specify the next field to be initialized. */
5602 set_init_label (fieldname)
5608 for (tail = TYPE_FIELDS (constructor_type); tail;
5609 tail = TREE_CHAIN (tail))
5611 if (tail == constructor_unfilled_fields)
5613 if (DECL_NAME (tail) == fieldname)
5618 error ("unknown field `%s' specified in initializer",
5619 IDENTIFIER_POINTER (fieldname));
5621 error ("field `%s' already initialized",
5622 IDENTIFIER_POINTER (fieldname));
5625 constructor_fields = tail;
5627 pedwarn ("ANSI C forbids specifying structure member to initialize");
5631 /* "Output" the next constructor element.
5632 At top level, really output it to assembler code now.
5633 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5634 TYPE is the data type that the containing data type wants here.
5635 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5637 PENDING if non-nil means output pending elements that belong
5638 right after this element. (PENDING is normally 1;
5639 it is 0 while outputting pending elements, to avoid recursion.) */
5642 output_init_element (value, type, field, pending)
5643 tree value, type, field;
5648 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5649 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5650 && !(TREE_CODE (value) == STRING_CST
5651 && TREE_CODE (type) == ARRAY_TYPE
5652 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5653 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5654 TYPE_MAIN_VARIANT (type))))
5655 value = default_conversion (value);
5657 if (value == error_mark_node)
5658 constructor_erroneous = 1;
5659 else if (!TREE_CONSTANT (value))
5660 constructor_constant = 0;
5661 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5662 constructor_simple = 0;
5664 if (require_constant_value && ! TREE_CONSTANT (value))
5666 error_init ("initializer element%s is not constant",
5668 value = error_mark_node;
5670 else if (require_constant_elements
5671 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5673 error_init ("initializer element%s is not computable at load time",
5675 value = error_mark_node;
5678 /* If this element duplicates one on constructor_pending_elts,
5679 print a message and ignore it. Don't do this when we're
5680 processing elements taken off constructor_pending_elts,
5681 because we'd always get spurious errors. */
5684 if (TREE_CODE (constructor_type) == RECORD_TYPE
5685 || TREE_CODE (constructor_type) == UNION_TYPE)
5687 if (purpose_member (field, constructor_pending_elts))
5689 error_init ("duplicate initializer%s", " for `%s'", NULL);
5693 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5696 for (tail = constructor_pending_elts; tail;
5697 tail = TREE_CHAIN (tail))
5698 if (TREE_PURPOSE (tail) != 0
5699 && TREE_CODE (TREE_PURPOSE (tail)) == INTEGER_CST
5700 && tree_int_cst_equal (TREE_PURPOSE (tail), constructor_index))
5705 error_init ("duplicate initializer%s", " for `%s'", NULL);
5711 /* If this element doesn't come next in sequence,
5712 put it on constructor_pending_elts. */
5713 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5714 && !tree_int_cst_equal (field, constructor_unfilled_index))
5717 /* The copy_node is needed in case field is actually
5718 constructor_index, which is modified in place. */
5719 constructor_pending_elts
5720 = tree_cons (copy_node (field),
5721 digest_init (type, value, 0, 0),
5722 constructor_pending_elts);
5724 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5725 && field != constructor_unfilled_fields)
5727 /* We do this for records but not for unions. In a union,
5728 no matter which field is specified, it can be initialized
5729 right away since it starts at the beginning of the union. */
5731 constructor_pending_elts
5733 digest_init (type, value, 0, 0),
5734 constructor_pending_elts);
5738 /* Otherwise, output this element either to
5739 constructor_elements or to the assembler file. */
5743 if (! constructor_incremental)
5745 if (field && TREE_CODE (field) == INTEGER_CST)
5746 field = copy_node (field);
5747 constructor_elements
5748 = tree_cons (field, digest_init (type, value, 0, 0),
5749 constructor_elements);
5753 /* Structure elements may require alignment.
5754 Do this, if necessary. */
5755 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5757 /* Advance to offset of this element. */
5758 if (! tree_int_cst_equal (constructor_bit_index,
5759 DECL_FIELD_BITPOS (field)))
5761 int next = (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
5763 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5766 assemble_zeros (next - here);
5769 output_constant (digest_init (type, value, 0, 0),
5770 int_size_in_bytes (type));
5772 /* For a record or union,
5773 keep track of end position of last field. */
5774 if (TREE_CODE (constructor_type) == RECORD_TYPE
5775 || TREE_CODE (constructor_type) == UNION_TYPE)
5777 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
5779 TREE_INT_CST_LOW (constructor_bit_index)
5780 = TREE_INT_CST_LOW (temp);
5781 TREE_INT_CST_HIGH (constructor_bit_index)
5782 = TREE_INT_CST_HIGH (temp);
5787 /* Advance the variable that indicates sequential elements output. */
5788 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5790 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
5792 TREE_INT_CST_LOW (constructor_unfilled_index)
5793 = TREE_INT_CST_LOW (tem);
5794 TREE_INT_CST_HIGH (constructor_unfilled_index)
5795 = TREE_INT_CST_HIGH (tem);
5797 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5798 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5799 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5800 constructor_unfilled_fields = 0;
5802 /* Now output any pending elements which have become next. */
5804 output_pending_init_elements (0);
5808 /* Output any pending elements which have become next.
5809 As we output elements, constructor_unfilled_{fields,index}
5810 advances, which may cause other elements to become next;
5811 if so, they too are output.
5813 If ALL is 0, we return when there are
5814 no more pending elements to output now.
5816 If ALL is 1, we output space as necessary so that
5817 we can output all the pending elements. */
5820 output_pending_init_elements (all)
5828 /* Look thru the whole pending list.
5829 If we find an element that should be output now,
5830 output it. Otherwise, set NEXT to the element
5831 that comes first among those still pending. */
5834 for (tail = constructor_pending_elts; tail;
5835 tail = TREE_CHAIN (tail))
5837 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5839 if (tree_int_cst_equal (TREE_PURPOSE (tail),
5840 constructor_unfilled_index))
5842 output_init_element (TREE_VALUE (tail),
5843 TREE_TYPE (constructor_type),
5844 constructor_unfilled_index, 0);
5847 else if (tree_int_cst_lt (TREE_PURPOSE (tail),
5848 constructor_unfilled_index))
5851 || tree_int_cst_lt (TREE_PURPOSE (tail), next))
5852 next = TREE_PURPOSE (tail);
5854 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5855 || TREE_CODE (constructor_type) == UNION_TYPE)
5857 if (TREE_PURPOSE (tail) == constructor_unfilled_fields)
5859 output_init_element (TREE_VALUE (tail),
5860 TREE_TYPE (constructor_unfilled_fields),
5861 constructor_unfilled_fields,
5865 else if (constructor_unfilled_fields == 0
5866 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
5867 DECL_FIELD_BITPOS (constructor_unfilled_fields)))
5870 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
5871 DECL_FIELD_BITPOS (next)))
5872 next = TREE_PURPOSE (tail);
5876 /* Ordinarily return, but not if we want to output all
5877 and there are elements left. */
5878 if (! (all && next != 0))
5881 /* Generate space up to the position of NEXT. */
5882 if (constructor_incremental)
5885 tree nextpos_tree = size_int (0);
5887 if (TREE_CODE (constructor_type) == RECORD_TYPE
5888 || TREE_CODE (constructor_type) == UNION_TYPE)
5890 /* Find the last field written out, if any. */
5891 for (tail = TYPE_FIELDS (constructor_type); tail;
5892 tail = TREE_CHAIN (tail))
5893 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
5897 /* Find the offset of the end of that field. */
5898 filled = size_binop (CEIL_DIV_EXPR,
5899 size_binop (PLUS_EXPR,
5900 DECL_FIELD_BITPOS (tail),
5902 size_int (BITS_PER_UNIT));
5904 filled = size_int (0);
5906 nextpos_tree = size_binop (CEIL_DIV_EXPR,
5907 DECL_FIELD_BITPOS (next),
5908 size_int (BITS_PER_UNIT));
5910 TREE_INT_CST_HIGH (constructor_bit_index)
5911 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
5912 TREE_INT_CST_LOW (constructor_bit_index)
5913 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
5914 constructor_unfilled_fields = next;
5916 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5918 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5919 size_in_bytes (TREE_TYPE (constructor_type)));
5921 = size_binop (MULT_EXPR, next,
5922 size_in_bytes (TREE_TYPE (constructor_type)));
5923 TREE_INT_CST_LOW (constructor_unfilled_index)
5924 = TREE_INT_CST_LOW (next);
5925 TREE_INT_CST_HIGH (constructor_unfilled_index)
5926 = TREE_INT_CST_HIGH (next);
5933 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
5935 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
5940 /* If it's not incremental, just skip over the gap,
5941 so that after jumping to retry we will output the next
5942 successive element. */
5943 if (TREE_CODE (constructor_type) == RECORD_TYPE
5944 || TREE_CODE (constructor_type) == UNION_TYPE)
5945 constructor_unfilled_fields = next;
5946 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5948 TREE_INT_CST_LOW (constructor_unfilled_index)
5949 = TREE_INT_CST_LOW (next);
5950 TREE_INT_CST_HIGH (constructor_unfilled_index)
5951 = TREE_INT_CST_HIGH (next);
5958 /* Add one non-braced element to the current constructor level.
5959 This adjusts the current position within the constructor's type.
5960 This may also start or terminate implicit levels
5961 to handle a partly-braced initializer.
5963 Once this has found the correct level for the new element,
5964 it calls output_init_element.
5966 Note: if we are incrementally outputting this constructor,
5967 this function may be called with a null argument
5968 representing a sub-constructor that was already incrementally output.
5969 When that happens, we output nothing, but we do the bookkeeping
5970 to skip past that element of the current constructor. */
5973 process_init_element (value)
5976 tree orig_value = value;
5977 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5979 /* Handle superfluous braces around string cst as in
5980 char x[] = {"foo"}; */
5983 && TREE_CODE (constructor_type) == ARRAY_TYPE
5984 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5985 && integer_zerop (constructor_unfilled_index))
5987 constructor_stack->replacement_value = value;
5991 if (constructor_stack->replacement_value != 0)
5993 error_init ("excess elements in struct initializer%s",
5994 " after `%s'", NULL_PTR);
5998 /* Ignore elements of a brace group if it is entirely superfluous
5999 and has already been diagnosed. */
6000 if (constructor_type == 0)
6003 /* If we've exhausted any levels that didn't have braces,
6005 while (constructor_stack->implicit)
6007 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6008 || TREE_CODE (constructor_type) == UNION_TYPE)
6009 && constructor_fields == 0)
6010 process_init_element (pop_init_level (1));
6011 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6012 && tree_int_cst_lt (constructor_max_index, constructor_index))
6013 process_init_element (pop_init_level (1));
6020 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6023 enum tree_code fieldcode;
6025 if (constructor_fields == 0)
6027 pedwarn_init ("excess elements in struct initializer%s",
6028 " after `%s'", NULL_PTR);
6032 fieldtype = TREE_TYPE (constructor_fields);
6033 if (fieldtype != error_mark_node)
6034 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6035 fieldcode = TREE_CODE (fieldtype);
6037 /* Accept a string constant to initialize a subarray. */
6039 && fieldcode == ARRAY_TYPE
6040 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6043 /* Otherwise, if we have come to a subaggregate,
6044 and we don't have an element of its type, push into it. */
6045 else if (value != 0 && !constructor_no_implicit
6046 && value != error_mark_node
6047 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6048 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6049 || fieldcode == UNION_TYPE))
6051 push_init_level (1);
6057 push_member_name (constructor_fields);
6058 output_init_element (value, fieldtype, constructor_fields, 1);
6059 RESTORE_SPELLING_DEPTH (constructor_depth);
6062 /* Do the bookkeeping for an element that was
6063 directly output as a constructor. */
6065 /* For a record, keep track of end position of last field. */
6066 tree temp = size_binop (PLUS_EXPR,
6067 DECL_FIELD_BITPOS (constructor_fields),
6068 DECL_SIZE (constructor_fields));
6069 TREE_INT_CST_LOW (constructor_bit_index)
6070 = TREE_INT_CST_LOW (temp);
6071 TREE_INT_CST_HIGH (constructor_bit_index)
6072 = TREE_INT_CST_HIGH (temp);
6074 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6077 constructor_fields = TREE_CHAIN (constructor_fields);
6078 /* Skip any nameless bit fields atthe beginning. */
6079 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
6080 && DECL_NAME (constructor_fields) == 0)
6081 constructor_fields = TREE_CHAIN (constructor_fields);
6084 if (TREE_CODE (constructor_type) == UNION_TYPE)
6087 enum tree_code fieldcode;
6089 if (constructor_fields == 0)
6091 pedwarn_init ("excess elements in union initializer%s",
6092 " after `%s'", NULL_PTR);
6096 fieldtype = TREE_TYPE (constructor_fields);
6097 if (fieldtype != error_mark_node)
6098 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6099 fieldcode = TREE_CODE (fieldtype);
6101 /* Accept a string constant to initialize a subarray. */
6103 && fieldcode == ARRAY_TYPE
6104 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6107 /* Otherwise, if we have come to a subaggregate,
6108 and we don't have an element of its type, push into it. */
6109 else if (value != 0 && !constructor_no_implicit
6110 && value != error_mark_node
6111 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6112 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6113 || fieldcode == UNION_TYPE))
6115 push_init_level (1);
6121 push_member_name (constructor_fields);
6122 output_init_element (value, fieldtype, constructor_fields, 1);
6123 RESTORE_SPELLING_DEPTH (constructor_depth);
6126 /* Do the bookkeeping for an element that was
6127 directly output as a constructor. */
6129 TREE_INT_CST_LOW (constructor_bit_index)
6130 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6131 TREE_INT_CST_HIGH (constructor_bit_index)
6132 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6134 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6137 constructor_fields = 0;
6140 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6142 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6143 enum tree_code eltcode = TREE_CODE (elttype);
6145 /* Accept a string constant to initialize a subarray. */
6147 && eltcode == ARRAY_TYPE
6148 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6151 /* Otherwise, if we have come to a subaggregate,
6152 and we don't have an element of its type, push into it. */
6153 else if (value != 0 && !constructor_no_implicit
6154 && value != error_mark_node
6155 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6156 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6157 || eltcode == UNION_TYPE))
6159 push_init_level (1);
6163 if (constructor_max_index != 0
6164 && tree_int_cst_lt (constructor_max_index, constructor_index))
6166 pedwarn_init ("excess elements in array initializer%s",
6167 " after `%s'", NULL_PTR);
6171 /* Now output the actual element.
6172 Ordinarily, output once.
6173 If there is a range, repeat it till we advance past the range. */
6180 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6181 output_init_element (value, elttype, constructor_index, 1);
6182 RESTORE_SPELLING_DEPTH (constructor_depth);
6185 tem = size_binop (PLUS_EXPR, constructor_index,
6187 TREE_INT_CST_LOW (constructor_index)
6188 = TREE_INT_CST_LOW (tem);
6189 TREE_INT_CST_HIGH (constructor_index)
6190 = TREE_INT_CST_HIGH (tem);
6193 /* If we are doing the bookkeeping for an element that was
6194 directly output as a constructor,
6195 we must update constructor_unfilled_index. */
6197 TREE_INT_CST_LOW (constructor_unfilled_index)
6198 = TREE_INT_CST_LOW (constructor_index);
6199 TREE_INT_CST_HIGH (constructor_unfilled_index)
6200 = TREE_INT_CST_HIGH (constructor_index);
6203 while (! (constructor_range_end == 0
6204 || tree_int_cst_lt (constructor_range_end,
6205 constructor_index)));
6210 /* Handle the sole element allowed in a braced initializer
6211 for a scalar variable. */
6212 if (constructor_fields == 0)
6214 pedwarn_init ("excess elements in scalar initializer%s",
6215 " after `%s'", NULL_PTR);
6220 output_init_element (value, constructor_type, NULL_TREE, 1);
6221 constructor_fields = 0;
6225 /* If the (lexically) previous elments are not now saved,
6226 we can discard the storage for them. */
6227 if (constructor_incremental && constructor_pending_elts == 0 && value != 0)
6231 /* Expand an ASM statement with operands, handling output operands
6232 that are not variables or INDIRECT_REFS by transforming such
6233 cases into cases that expand_asm_operands can handle.
6235 Arguments are same as for expand_asm_operands. */
6238 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6239 tree string, outputs, inputs, clobbers;
6244 int noutputs = list_length (outputs);
6246 /* o[I] is the place that output number I should be written. */
6247 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6250 if (TREE_CODE (string) == ADDR_EXPR)
6251 string = TREE_OPERAND (string, 0);
6252 if (TREE_CODE (string) != STRING_CST)
6254 error ("asm template is not a string constant");
6258 /* Record the contents of OUTPUTS before it is modified. */
6259 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6260 o[i] = TREE_VALUE (tail);
6262 /* Perform default conversions on array and function inputs. */
6263 /* Don't do this for other types--
6264 it would screw up operands expected to be in memory. */
6265 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6266 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6267 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6268 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6270 /* Generate the ASM_OPERANDS insn;
6271 store into the TREE_VALUEs of OUTPUTS some trees for
6272 where the values were actually stored. */
6273 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6275 /* Copy all the intermediate outputs into the specified outputs. */
6276 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6278 if (o[i] != TREE_VALUE (tail))
6280 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6284 /* Detect modification of read-only values.
6285 (Otherwise done by build_modify_expr.) */
6288 tree type = TREE_TYPE (o[i]);
6289 if (TYPE_READONLY (type)
6290 || ((TREE_CODE (type) == RECORD_TYPE
6291 || TREE_CODE (type) == UNION_TYPE)
6292 && C_TYPE_FIELDS_READONLY (type)))
6293 readonly_warning (o[i], "modification by `asm'");
6297 /* Those MODIFY_EXPRs could do autoincrements. */
6301 /* Expand a C `return' statement.
6302 RETVAL is the expression for what to return,
6303 or a null pointer for `return;' with no value. */
6306 c_expand_return (retval)
6309 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6311 if (TREE_THIS_VOLATILE (current_function_decl))
6312 warning ("function declared `noreturn' has a `return' statement");
6316 current_function_returns_null = 1;
6317 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6318 warning ("`return' with no value, in function returning non-void");
6319 expand_null_return ();
6321 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6323 current_function_returns_null = 1;
6324 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6325 pedwarn ("`return' with a value, in function returning void");
6326 expand_return (retval);
6330 tree t = convert_for_assignment (valtype, retval, "return",
6331 NULL_TREE, NULL_TREE, 0);
6332 tree res = DECL_RESULT (current_function_decl);
6335 if (t == error_mark_node)
6338 inner = t = convert (TREE_TYPE (res), t);
6340 /* Strip any conversions, additions, and subtractions, and see if
6341 we are returning the address of a local variable. Warn if so. */
6342 while (TREE_CODE (inner) == NOP_EXPR
6343 || TREE_CODE (inner) == NON_LVALUE_EXPR
6344 || TREE_CODE (inner) == CONVERT_EXPR
6345 || TREE_CODE (inner) == PLUS_EXPR
6346 || TREE_CODE (inner) == MINUS_EXPR)
6347 inner = TREE_OPERAND (inner, 0);
6349 if (TREE_CODE (inner) == ADDR_EXPR)
6351 inner = TREE_OPERAND (inner, 0);
6353 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6354 inner = TREE_OPERAND (inner, 0);
6356 if (TREE_CODE (inner) == VAR_DECL
6357 && ! DECL_EXTERNAL (inner)
6358 && ! TREE_STATIC (inner)
6359 && DECL_CONTEXT (inner) == current_function_decl)
6360 warning ("function returns address of local variable");
6363 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6364 TREE_SIDE_EFFECTS (t) = 1;
6366 current_function_returns_value = 1;
6370 /* Start a C switch statement, testing expression EXP.
6371 Return EXP if it is valid, an error node otherwise. */
6374 c_expand_start_case (exp)
6377 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6378 tree type = TREE_TYPE (exp);
6380 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6382 error ("switch quantity not an integer");
6383 exp = error_mark_node;
6388 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6390 if (warn_traditional
6391 && (type == long_integer_type_node
6392 || type == long_unsigned_type_node))
6393 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6395 exp = default_conversion (exp);
6396 type = TREE_TYPE (exp);
6397 index = get_unwidened (exp, NULL_TREE);
6398 /* We can't strip a conversion from a signed type to an unsigned,
6399 because if we did, int_fits_type_p would do the wrong thing
6400 when checking case values for being in range,
6401 and it's too hard to do the right thing. */
6402 if (TREE_UNSIGNED (TREE_TYPE (exp))
6403 == TREE_UNSIGNED (TREE_TYPE (index)))
6407 expand_start_case (1, exp, type, "switch statement");