1998-09-21 Ben Elliston <bje@cygnus.com>
[official-gcc.git] / gcc / c-typeck.c
blob0a1f490577cdd9403c77ee459b3bf3c0fb1509d3
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 88, 91-97, 1998 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)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This file is part of the C front end.
23 It contains routines to build C expressions given their operands,
24 including computing the types of the result, C-specific error checks,
25 and some optimization.
27 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
28 and to process initializations in declarations (since they work
29 like a strange sort of assignment). */
31 #include "config.h"
32 #include "system.h"
33 #include "tree.h"
34 #include "c-tree.h"
35 #include "flags.h"
36 #include "output.h"
37 #include "rtl.h"
38 #include "expr.h"
39 #include "toplev.h"
41 /* Nonzero if we've already printed a "missing braces around initializer"
42 message within this initializer. */
43 static int missing_braces_mentioned;
45 static tree qualify_type PROTO((tree, tree));
46 static int comp_target_types PROTO((tree, tree));
47 static int function_types_compatible_p PROTO((tree, tree));
48 static int type_lists_compatible_p PROTO((tree, tree));
49 static int self_promoting_type_p PROTO((tree));
50 static tree decl_constant_value PROTO((tree));
51 static tree lookup_field PROTO((tree, tree, tree *));
52 static tree convert_arguments PROTO((tree, tree, tree, tree));
53 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
54 static tree pointer_diff PROTO((tree, tree));
55 static tree unary_complex_lvalue PROTO((enum tree_code, tree));
56 static void pedantic_lvalue_warning PROTO((enum tree_code));
57 static tree internal_build_compound_expr PROTO((tree, int));
58 static tree convert_for_assignment PROTO((tree, tree, char *, tree,
59 tree, int));
60 static void warn_for_assignment PROTO((char *, char *, tree, int));
61 static tree valid_compound_expr_initializer PROTO((tree, tree));
62 static void push_string PROTO((char *));
63 static void push_member_name PROTO((tree));
64 static void push_array_bounds PROTO((int));
65 static int spelling_length PROTO((void));
66 static char *print_spelling PROTO((char *));
67 static char *get_spelling PROTO((char *));
68 static void warning_init PROTO((char *, char *,
69 char *));
70 static tree digest_init PROTO((tree, tree, int, int));
71 static void check_init_type_bitfields PROTO((tree));
72 static void output_init_element PROTO((tree, tree, tree, int));
73 static void output_pending_init_elements PROTO((int));
74 static void add_pending_init PROTO((tree, tree));
75 static int pending_init_member PROTO((tree));
77 /* Do `exp = require_complete_type (exp);' to make sure exp
78 does not have an incomplete type. (That includes void types.) */
80 tree
81 require_complete_type (value)
82 tree value;
84 tree type = TREE_TYPE (value);
86 /* First, detect a valid value with a complete type. */
87 if (TYPE_SIZE (type) != 0
88 && type != void_type_node)
89 return value;
91 incomplete_type_error (value, type);
92 return error_mark_node;
95 /* Print an error message for invalid use of an incomplete type.
96 VALUE is the expression that was used (or 0 if that isn't known)
97 and TYPE is the type that was invalid. */
99 void
100 incomplete_type_error (value, type)
101 tree value;
102 tree type;
104 char *errmsg;
106 /* Avoid duplicate error message. */
107 if (TREE_CODE (type) == ERROR_MARK)
108 return;
110 if (value != 0 && (TREE_CODE (value) == VAR_DECL
111 || TREE_CODE (value) == PARM_DECL))
112 error ("`%s' has an incomplete type",
113 IDENTIFIER_POINTER (DECL_NAME (value)));
114 else
116 retry:
117 /* We must print an error message. Be clever about what it says. */
119 switch (TREE_CODE (type))
121 case RECORD_TYPE:
122 errmsg = "invalid use of undefined type `struct %s'";
123 break;
125 case UNION_TYPE:
126 errmsg = "invalid use of undefined type `union %s'";
127 break;
129 case ENUMERAL_TYPE:
130 errmsg = "invalid use of undefined type `enum %s'";
131 break;
133 case VOID_TYPE:
134 error ("invalid use of void expression");
135 return;
137 case ARRAY_TYPE:
138 if (TYPE_DOMAIN (type))
140 type = TREE_TYPE (type);
141 goto retry;
143 error ("invalid use of array with unspecified bounds");
144 return;
146 default:
147 abort ();
150 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
151 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
152 else
153 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
154 error ("invalid use of incomplete typedef `%s'",
155 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
159 /* Return a variant of TYPE which has all the type qualifiers of LIKE
160 as well as those of TYPE. */
162 static tree
163 qualify_type (type, like)
164 tree type, like;
166 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
167 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
168 return c_build_type_variant (type, constflag, volflag);
171 /* Return the common type of two types.
172 We assume that comptypes has already been done and returned 1;
173 if that isn't so, this may crash. In particular, we assume that qualifiers
174 match.
176 This is the type for the result of most arithmetic operations
177 if the operands have the given two types. */
179 tree
180 common_type (t1, t2)
181 tree t1, t2;
183 register enum tree_code code1;
184 register enum tree_code code2;
185 tree attributes;
187 /* Save time if the two types are the same. */
189 if (t1 == t2) return t1;
191 /* If one type is nonsense, use the other. */
192 if (t1 == error_mark_node)
193 return t2;
194 if (t2 == error_mark_node)
195 return t1;
197 /* Merge the attributes. */
198 attributes = merge_machine_type_attributes (t1, t2);
200 /* Treat an enum type as the unsigned integer type of the same width. */
202 if (TREE_CODE (t1) == ENUMERAL_TYPE)
203 t1 = type_for_size (TYPE_PRECISION (t1), 1);
204 if (TREE_CODE (t2) == ENUMERAL_TYPE)
205 t2 = type_for_size (TYPE_PRECISION (t2), 1);
207 code1 = TREE_CODE (t1);
208 code2 = TREE_CODE (t2);
210 /* If one type is complex, form the common type of the non-complex
211 components, then make that complex. Use T1 or T2 if it is the
212 required type. */
213 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
215 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
216 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
217 tree subtype = common_type (subtype1, subtype2);
219 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
220 return build_type_attribute_variant (t1, attributes);
221 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
222 return build_type_attribute_variant (t2, attributes);
223 else
224 return build_type_attribute_variant (build_complex_type (subtype),
225 attributes);
228 switch (code1)
230 case INTEGER_TYPE:
231 case REAL_TYPE:
232 /* If only one is real, use it as the result. */
234 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
235 return build_type_attribute_variant (t1, attributes);
237 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
238 return build_type_attribute_variant (t2, attributes);
240 /* Both real or both integers; use the one with greater precision. */
242 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
243 return build_type_attribute_variant (t1, attributes);
244 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
245 return build_type_attribute_variant (t2, attributes);
247 /* Same precision. Prefer longs to ints even when same size. */
249 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
250 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
251 return build_type_attribute_variant (long_unsigned_type_node,
252 attributes);
254 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
255 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
257 /* But preserve unsignedness from the other type,
258 since long cannot hold all the values of an unsigned int. */
259 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
260 t1 = long_unsigned_type_node;
261 else
262 t1 = long_integer_type_node;
263 return build_type_attribute_variant (t1, attributes);
266 /* Likewise, prefer long double to double even if same size. */
267 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
268 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
269 return build_type_attribute_variant (long_double_type_node,
270 attributes);
272 /* Otherwise prefer the unsigned one. */
274 if (TREE_UNSIGNED (t1))
275 return build_type_attribute_variant (t1, attributes);
276 else
277 return build_type_attribute_variant (t2, attributes);
279 case POINTER_TYPE:
280 /* For two pointers, do this recursively on the target type,
281 and combine the qualifiers of the two types' targets. */
282 /* This code was turned off; I don't know why.
283 But ANSI C specifies doing this with the qualifiers.
284 So I turned it on again. */
286 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
287 TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
288 int constp
289 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
290 int volatilep
291 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
292 t1 = build_pointer_type (c_build_type_variant (target, constp,
293 volatilep));
294 return build_type_attribute_variant (t1, attributes);
296 #if 0
297 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
298 return build_type_attribute_variant (t1, attributes);
299 #endif
301 case ARRAY_TYPE:
303 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
304 /* Save space: see if the result is identical to one of the args. */
305 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
306 return build_type_attribute_variant (t1, attributes);
307 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
308 return build_type_attribute_variant (t2, attributes);
309 /* Merge the element types, and have a size if either arg has one. */
310 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
311 return build_type_attribute_variant (t1, attributes);
314 case FUNCTION_TYPE:
315 /* Function types: prefer the one that specified arg types.
316 If both do, merge the arg types. Also merge the return types. */
318 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
319 tree p1 = TYPE_ARG_TYPES (t1);
320 tree p2 = TYPE_ARG_TYPES (t2);
321 int len;
322 tree newargs, n;
323 int i;
325 /* Save space: see if the result is identical to one of the args. */
326 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
327 return build_type_attribute_variant (t1, attributes);
328 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
329 return build_type_attribute_variant (t2, attributes);
331 /* Simple way if one arg fails to specify argument types. */
332 if (TYPE_ARG_TYPES (t1) == 0)
334 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
335 return build_type_attribute_variant (t1, attributes);
337 if (TYPE_ARG_TYPES (t2) == 0)
339 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
340 return build_type_attribute_variant (t1, attributes);
343 /* If both args specify argument types, we must merge the two
344 lists, argument by argument. */
346 len = list_length (p1);
347 newargs = 0;
349 for (i = 0; i < len; i++)
350 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
352 n = newargs;
354 for (; p1;
355 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
357 /* A null type means arg type is not specified.
358 Take whatever the other function type has. */
359 if (TREE_VALUE (p1) == 0)
361 TREE_VALUE (n) = TREE_VALUE (p2);
362 goto parm_done;
364 if (TREE_VALUE (p2) == 0)
366 TREE_VALUE (n) = TREE_VALUE (p1);
367 goto parm_done;
370 /* Given wait (union {union wait *u; int *i} *)
371 and wait (union wait *),
372 prefer union wait * as type of parm. */
373 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
374 && TREE_VALUE (p1) != TREE_VALUE (p2))
376 tree memb;
377 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
378 memb; memb = TREE_CHAIN (memb))
379 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
381 TREE_VALUE (n) = TREE_VALUE (p2);
382 if (pedantic)
383 pedwarn ("function types not truly compatible in ANSI C");
384 goto parm_done;
387 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
388 && TREE_VALUE (p2) != TREE_VALUE (p1))
390 tree memb;
391 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
392 memb; memb = TREE_CHAIN (memb))
393 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
395 TREE_VALUE (n) = TREE_VALUE (p1);
396 if (pedantic)
397 pedwarn ("function types not truly compatible in ANSI C");
398 goto parm_done;
401 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
402 parm_done: ;
405 t1 = build_function_type (valtype, newargs);
406 /* ... falls through ... */
409 default:
410 return build_type_attribute_variant (t1, attributes);
415 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
416 or various other operations. Return 2 if they are compatible
417 but a warning may be needed if you use them together. */
420 comptypes (type1, type2)
421 tree type1, type2;
423 register tree t1 = type1;
424 register tree t2 = type2;
425 int attrval, val;
427 /* Suppress errors caused by previously reported errors. */
429 if (t1 == t2 || !t1 || !t2
430 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
431 return 1;
433 /* Treat an enum type as the integer type of the same width and
434 signedness. */
436 if (TREE_CODE (t1) == ENUMERAL_TYPE)
437 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
438 if (TREE_CODE (t2) == ENUMERAL_TYPE)
439 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
441 if (t1 == t2)
442 return 1;
444 /* Different classes of types can't be compatible. */
446 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
448 /* Qualifiers must match. */
450 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
451 return 0;
452 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
453 return 0;
455 /* Allow for two different type nodes which have essentially the same
456 definition. Note that we already checked for equality of the type
457 qualifiers (just above). */
459 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
460 return 1;
462 #ifndef COMP_TYPE_ATTRIBUTES
463 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
464 #endif
466 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
467 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
468 return 0;
470 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
471 val = 0;
473 switch (TREE_CODE (t1))
475 case POINTER_TYPE:
476 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
477 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
478 break;
480 case FUNCTION_TYPE:
481 val = function_types_compatible_p (t1, t2);
482 break;
484 case ARRAY_TYPE:
486 tree d1 = TYPE_DOMAIN (t1);
487 tree d2 = TYPE_DOMAIN (t2);
488 val = 1;
490 /* Target types must match incl. qualifiers. */
491 if (TREE_TYPE (t1) != TREE_TYPE (t2)
492 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
493 return 0;
495 /* Sizes must match unless one is missing or variable. */
496 if (d1 == 0 || d2 == 0 || d1 == d2
497 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
498 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
499 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
500 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
501 break;
503 if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
504 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
505 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
506 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
507 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
508 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
509 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
510 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2)))))
511 val = 0;
512 break;
515 case RECORD_TYPE:
516 if (maybe_objc_comptypes (t1, t2, 0) == 1)
517 val = 1;
518 break;
520 default:
521 break;
523 return attrval == 2 && val == 1 ? 2 : val;
526 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
527 ignoring their qualifiers. */
529 static int
530 comp_target_types (ttl, ttr)
531 tree ttl, ttr;
533 int val;
535 /* Give maybe_objc_comptypes a crack at letting these types through. */
536 if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0)
537 return val;
539 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
540 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
542 if (val == 2 && pedantic)
543 pedwarn ("types are not quite compatible");
544 return val;
547 /* Subroutines of `comptypes'. */
549 /* Return 1 if two function types F1 and F2 are compatible.
550 If either type specifies no argument types,
551 the other must specify a fixed number of self-promoting arg types.
552 Otherwise, if one type specifies only the number of arguments,
553 the other must specify that number of self-promoting arg types.
554 Otherwise, the argument types must match. */
556 static int
557 function_types_compatible_p (f1, f2)
558 tree f1, f2;
560 tree args1, args2;
561 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
562 int val = 1;
563 int val1;
565 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
566 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
567 return 0;
569 args1 = TYPE_ARG_TYPES (f1);
570 args2 = TYPE_ARG_TYPES (f2);
572 /* An unspecified parmlist matches any specified parmlist
573 whose argument types don't need default promotions. */
575 if (args1 == 0)
577 if (!self_promoting_args_p (args2))
578 return 0;
579 /* If one of these types comes from a non-prototype fn definition,
580 compare that with the other type's arglist.
581 If they don't match, ask for a warning (but no error). */
582 if (TYPE_ACTUAL_ARG_TYPES (f1)
583 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
584 val = 2;
585 return val;
587 if (args2 == 0)
589 if (!self_promoting_args_p (args1))
590 return 0;
591 if (TYPE_ACTUAL_ARG_TYPES (f2)
592 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
593 val = 2;
594 return val;
597 /* Both types have argument lists: compare them and propagate results. */
598 val1 = type_lists_compatible_p (args1, args2);
599 return val1 != 1 ? val1 : val;
602 /* Check two lists of types for compatibility,
603 returning 0 for incompatible, 1 for compatible,
604 or 2 for compatible with warning. */
606 static int
607 type_lists_compatible_p (args1, args2)
608 tree args1, args2;
610 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
611 int val = 1;
612 int newval = 0;
614 while (1)
616 if (args1 == 0 && args2 == 0)
617 return val;
618 /* If one list is shorter than the other,
619 they fail to match. */
620 if (args1 == 0 || args2 == 0)
621 return 0;
622 /* A null pointer instead of a type
623 means there is supposed to be an argument
624 but nothing is specified about what type it has.
625 So match anything that self-promotes. */
626 if (TREE_VALUE (args1) == 0)
628 if (! self_promoting_type_p (TREE_VALUE (args2)))
629 return 0;
631 else if (TREE_VALUE (args2) == 0)
633 if (! self_promoting_type_p (TREE_VALUE (args1)))
634 return 0;
636 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
638 /* Allow wait (union {union wait *u; int *i} *)
639 and wait (union wait *) to be compatible. */
640 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
641 && (TYPE_NAME (TREE_VALUE (args1)) == 0
642 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
643 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
644 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
645 TYPE_SIZE (TREE_VALUE (args2))))
647 tree memb;
648 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
649 memb; memb = TREE_CHAIN (memb))
650 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
651 break;
652 if (memb == 0)
653 return 0;
655 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
656 && (TYPE_NAME (TREE_VALUE (args2)) == 0
657 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
658 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
659 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
660 TYPE_SIZE (TREE_VALUE (args1))))
662 tree memb;
663 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
664 memb; memb = TREE_CHAIN (memb))
665 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
666 break;
667 if (memb == 0)
668 return 0;
670 else
671 return 0;
674 /* comptypes said ok, but record if it said to warn. */
675 if (newval > val)
676 val = newval;
678 args1 = TREE_CHAIN (args1);
679 args2 = TREE_CHAIN (args2);
683 /* Return 1 if PARMS specifies a fixed number of parameters
684 and none of their types is affected by default promotions. */
687 self_promoting_args_p (parms)
688 tree parms;
690 register tree t;
691 for (t = parms; t; t = TREE_CHAIN (t))
693 register tree type = TREE_VALUE (t);
695 if (TREE_CHAIN (t) == 0 && type != void_type_node)
696 return 0;
698 if (type == 0)
699 return 0;
701 if (TYPE_MAIN_VARIANT (type) == float_type_node)
702 return 0;
704 if (C_PROMOTING_INTEGER_TYPE_P (type))
705 return 0;
707 return 1;
710 /* Return 1 if TYPE is not affected by default promotions. */
712 static int
713 self_promoting_type_p (type)
714 tree type;
716 if (TYPE_MAIN_VARIANT (type) == float_type_node)
717 return 0;
719 if (C_PROMOTING_INTEGER_TYPE_P (type))
720 return 0;
722 return 1;
725 /* Return an unsigned type the same as TYPE in other respects. */
727 tree
728 unsigned_type (type)
729 tree type;
731 tree type1 = TYPE_MAIN_VARIANT (type);
732 if (type1 == signed_char_type_node || type1 == char_type_node)
733 return unsigned_char_type_node;
734 if (type1 == integer_type_node)
735 return unsigned_type_node;
736 if (type1 == short_integer_type_node)
737 return short_unsigned_type_node;
738 if (type1 == long_integer_type_node)
739 return long_unsigned_type_node;
740 if (type1 == long_long_integer_type_node)
741 return long_long_unsigned_type_node;
742 if (type1 == intDI_type_node)
743 return unsigned_intDI_type_node;
744 if (type1 == intSI_type_node)
745 return unsigned_intSI_type_node;
746 if (type1 == intHI_type_node)
747 return unsigned_intHI_type_node;
748 if (type1 == intQI_type_node)
749 return unsigned_intQI_type_node;
751 return signed_or_unsigned_type (1, type);
754 /* Return a signed type the same as TYPE in other respects. */
756 tree
757 signed_type (type)
758 tree type;
760 tree type1 = TYPE_MAIN_VARIANT (type);
761 if (type1 == unsigned_char_type_node || type1 == char_type_node)
762 return signed_char_type_node;
763 if (type1 == unsigned_type_node)
764 return integer_type_node;
765 if (type1 == short_unsigned_type_node)
766 return short_integer_type_node;
767 if (type1 == long_unsigned_type_node)
768 return long_integer_type_node;
769 if (type1 == long_long_unsigned_type_node)
770 return long_long_integer_type_node;
771 if (type1 == unsigned_intDI_type_node)
772 return intDI_type_node;
773 if (type1 == unsigned_intSI_type_node)
774 return intSI_type_node;
775 if (type1 == unsigned_intHI_type_node)
776 return intHI_type_node;
777 if (type1 == unsigned_intQI_type_node)
778 return intQI_type_node;
780 return signed_or_unsigned_type (0, type);
783 /* Return a type the same as TYPE except unsigned or
784 signed according to UNSIGNEDP. */
786 tree
787 signed_or_unsigned_type (unsignedp, type)
788 int unsignedp;
789 tree type;
791 if ((! INTEGRAL_TYPE_P (type) && ! POINTER_TYPE_P (type))
792 || TREE_UNSIGNED (type) == unsignedp)
793 return type;
794 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
795 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
796 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
797 return unsignedp ? unsigned_type_node : integer_type_node;
798 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
799 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
800 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
801 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
802 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
803 return (unsignedp ? long_long_unsigned_type_node
804 : long_long_integer_type_node);
805 return type;
808 /* Compute the value of the `sizeof' operator. */
810 tree
811 c_sizeof (type)
812 tree type;
814 enum tree_code code = TREE_CODE (type);
815 tree t;
817 if (code == FUNCTION_TYPE)
819 if (pedantic || warn_pointer_arith)
820 pedwarn ("sizeof applied to a function type");
821 return size_int (1);
823 if (code == VOID_TYPE)
825 if (pedantic || warn_pointer_arith)
826 pedwarn ("sizeof applied to a void type");
827 return size_int (1);
829 if (code == ERROR_MARK)
830 return size_int (1);
831 if (TYPE_SIZE (type) == 0)
833 error ("sizeof applied to an incomplete type");
834 return size_int (0);
837 /* Convert in case a char is more than one unit. */
838 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
839 size_int (TYPE_PRECISION (char_type_node)));
840 t = convert (sizetype, t);
841 /* size_binop does not put the constant in range, so do it now. */
842 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
843 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
844 return t;
847 tree
848 c_sizeof_nowarn (type)
849 tree type;
851 enum tree_code code = TREE_CODE (type);
852 tree t;
854 if (code == FUNCTION_TYPE
855 || code == VOID_TYPE
856 || code == ERROR_MARK)
857 return size_int (1);
858 if (TYPE_SIZE (type) == 0)
859 return size_int (0);
861 /* Convert in case a char is more than one unit. */
862 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
863 size_int (TYPE_PRECISION (char_type_node)));
864 t = convert (sizetype, t);
865 force_fit_type (t, 0);
866 return t;
869 /* Compute the size to increment a pointer by. */
871 tree
872 c_size_in_bytes (type)
873 tree type;
875 enum tree_code code = TREE_CODE (type);
876 tree t;
878 if (code == FUNCTION_TYPE)
879 return size_int (1);
880 if (code == VOID_TYPE)
881 return size_int (1);
882 if (code == ERROR_MARK)
883 return size_int (1);
884 if (TYPE_SIZE (type) == 0)
886 error ("arithmetic on pointer to an incomplete type");
887 return size_int (1);
890 /* Convert in case a char is more than one unit. */
891 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
892 size_int (BITS_PER_UNIT));
893 t = convert (sizetype, t);
894 force_fit_type (t, 0);
895 return t;
898 /* Implement the __alignof keyword: Return the minimum required
899 alignment of TYPE, measured in bytes. */
901 tree
902 c_alignof (type)
903 tree type;
905 enum tree_code code = TREE_CODE (type);
907 if (code == FUNCTION_TYPE)
908 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
910 if (code == VOID_TYPE || code == ERROR_MARK)
911 return size_int (1);
913 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
916 /* Implement the __alignof keyword: Return the minimum required
917 alignment of EXPR, measured in bytes. For VAR_DECL's and
918 FIELD_DECL's return DECL_ALIGN (which can be set from an
919 "aligned" __attribute__ specification). */
921 tree
922 c_alignof_expr (expr)
923 tree expr;
925 if (TREE_CODE (expr) == VAR_DECL)
926 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
928 if (TREE_CODE (expr) == COMPONENT_REF
929 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
931 error ("`__alignof' applied to a bit-field");
932 return size_int (1);
934 else if (TREE_CODE (expr) == COMPONENT_REF
935 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
936 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
938 if (TREE_CODE (expr) == INDIRECT_REF)
940 tree t = TREE_OPERAND (expr, 0);
941 tree best = t;
942 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
944 while (TREE_CODE (t) == NOP_EXPR
945 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
947 int thisalign;
949 t = TREE_OPERAND (t, 0);
950 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
951 if (thisalign > bestalign)
952 best = t, bestalign = thisalign;
954 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
956 else
957 return c_alignof (TREE_TYPE (expr));
960 /* Return either DECL or its known constant value (if it has one). */
962 static tree
963 decl_constant_value (decl)
964 tree decl;
966 if (/* Don't change a variable array bound or initial value to a constant
967 in a place where a variable is invalid. */
968 current_function_decl != 0
969 && ! pedantic
970 && ! TREE_THIS_VOLATILE (decl)
971 && TREE_READONLY (decl) && ! ITERATOR_P (decl)
972 && DECL_INITIAL (decl) != 0
973 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
974 /* This is invalid if initial value is not constant.
975 If it has either a function call, a memory reference,
976 or a variable, then re-evaluating it could give different results. */
977 && TREE_CONSTANT (DECL_INITIAL (decl))
978 /* Check for cases where this is sub-optimal, even though valid. */
979 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
980 && DECL_MODE (decl) != BLKmode)
981 return DECL_INITIAL (decl);
982 return decl;
985 /* Perform default promotions for C data used in expressions.
986 Arrays and functions are converted to pointers;
987 enumeral types or short or char, to int.
988 In addition, manifest constants symbols are replaced by their values. */
990 tree
991 default_conversion (exp)
992 tree exp;
994 register tree type = TREE_TYPE (exp);
995 register enum tree_code code = TREE_CODE (type);
997 /* Constants can be used directly unless they're not loadable. */
998 if (TREE_CODE (exp) == CONST_DECL)
999 exp = DECL_INITIAL (exp);
1001 /* Replace a nonvolatile const static variable with its value unless
1002 it is an array, in which case we must be sure that taking the
1003 address of the array produces consistent results. */
1004 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1006 exp = decl_constant_value (exp);
1007 type = TREE_TYPE (exp);
1010 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1011 an lvalue. */
1012 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
1013 to integer and cause infinite recursion. */
1014 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1015 || (TREE_CODE (exp) == NOP_EXPR
1016 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1017 exp = TREE_OPERAND (exp, 0);
1019 /* Normally convert enums to int,
1020 but convert wide enums to something wider. */
1021 if (code == ENUMERAL_TYPE)
1023 type = type_for_size (MAX (TYPE_PRECISION (type),
1024 TYPE_PRECISION (integer_type_node)),
1025 ((flag_traditional
1026 || (TYPE_PRECISION (type)
1027 >= TYPE_PRECISION (integer_type_node)))
1028 && TREE_UNSIGNED (type)));
1029 return convert (type, exp);
1032 if (TREE_CODE (exp) == COMPONENT_REF
1033 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
1035 tree width = DECL_SIZE (TREE_OPERAND (exp, 1));
1036 HOST_WIDE_INT low = TREE_INT_CST_LOW (width);
1038 /* If it's thinner than an int, promote it like a
1039 C_PROMOTING_INTEGER_TYPE_P, otherwise leave it alone. */
1041 if (low < TYPE_PRECISION (integer_type_node))
1043 if (flag_traditional && TREE_UNSIGNED (type))
1044 return convert (unsigned_type_node, exp);
1045 else
1046 return convert (integer_type_node, exp);
1050 if (C_PROMOTING_INTEGER_TYPE_P (type))
1052 /* Traditionally, unsignedness is preserved in default promotions.
1053 Also preserve unsignedness if not really getting any wider. */
1054 if (TREE_UNSIGNED (type)
1055 && (flag_traditional
1056 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1057 return convert (unsigned_type_node, exp);
1058 return convert (integer_type_node, exp);
1060 if (flag_traditional && !flag_allow_single_precision
1061 && TYPE_MAIN_VARIANT (type) == float_type_node)
1062 return convert (double_type_node, exp);
1063 if (code == VOID_TYPE)
1065 error ("void value not ignored as it ought to be");
1066 return error_mark_node;
1068 if (code == FUNCTION_TYPE)
1070 return build_unary_op (ADDR_EXPR, exp, 0);
1072 if (code == ARRAY_TYPE)
1074 register tree adr;
1075 tree restype = TREE_TYPE (type);
1076 tree ptrtype;
1077 int constp = 0;
1078 int volatilep = 0;
1080 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1081 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1083 constp = TREE_READONLY (exp);
1084 volatilep = TREE_THIS_VOLATILE (exp);
1087 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1088 || constp || volatilep)
1089 restype = c_build_type_variant (restype,
1090 TYPE_READONLY (type) || constp,
1091 TYPE_VOLATILE (type) || volatilep);
1093 if (TREE_CODE (exp) == INDIRECT_REF)
1094 return convert (TYPE_POINTER_TO (restype),
1095 TREE_OPERAND (exp, 0));
1097 if (TREE_CODE (exp) == COMPOUND_EXPR)
1099 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1100 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1101 TREE_OPERAND (exp, 0), op1);
1104 if (! lvalue_p (exp)
1105 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1107 error ("invalid use of non-lvalue array");
1108 return error_mark_node;
1111 ptrtype = build_pointer_type (restype);
1113 if (TREE_CODE (exp) == VAR_DECL)
1115 /* ??? This is not really quite correct
1116 in that the type of the operand of ADDR_EXPR
1117 is not the target type of the type of the ADDR_EXPR itself.
1118 Question is, can this lossage be avoided? */
1119 adr = build1 (ADDR_EXPR, ptrtype, exp);
1120 if (mark_addressable (exp) == 0)
1121 return error_mark_node;
1122 TREE_CONSTANT (adr) = staticp (exp);
1123 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1124 return adr;
1126 /* This way is better for a COMPONENT_REF since it can
1127 simplify the offset for a component. */
1128 adr = build_unary_op (ADDR_EXPR, exp, 1);
1129 return convert (ptrtype, adr);
1131 return exp;
1134 /* Look up component name in the structure type definition.
1136 If this component name is found indirectly within an anonymous union,
1137 store in *INDIRECT the component which directly contains
1138 that anonymous union. Otherwise, set *INDIRECT to 0. */
1140 static tree
1141 lookup_field (type, component, indirect)
1142 tree type, component;
1143 tree *indirect;
1145 tree field;
1147 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1148 to the field elements. Use a binary search on this array to quickly
1149 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1150 will always be set for structures which have many elements. */
1152 if (TYPE_LANG_SPECIFIC (type))
1154 int bot, top, half;
1155 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1157 field = TYPE_FIELDS (type);
1158 bot = 0;
1159 top = TYPE_LANG_SPECIFIC (type)->len;
1160 while (top - bot > 1)
1162 half = (top - bot + 1) >> 1;
1163 field = field_array[bot+half];
1165 if (DECL_NAME (field) == NULL_TREE)
1167 /* Step through all anon unions in linear fashion. */
1168 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1170 tree anon = 0, junk;
1172 field = field_array[bot++];
1173 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1174 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1175 anon = lookup_field (TREE_TYPE (field), component, &junk);
1177 if (anon != NULL_TREE)
1179 *indirect = field;
1180 return anon;
1184 /* Entire record is only anon unions. */
1185 if (bot > top)
1186 return NULL_TREE;
1188 /* Restart the binary search, with new lower bound. */
1189 continue;
1192 if (DECL_NAME (field) == component)
1193 break;
1194 if (DECL_NAME (field) < component)
1195 bot += half;
1196 else
1197 top = bot + half;
1200 if (DECL_NAME (field_array[bot]) == component)
1201 field = field_array[bot];
1202 else if (DECL_NAME (field) != component)
1203 field = 0;
1205 else
1207 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1209 if (DECL_NAME (field) == NULL_TREE)
1211 tree junk;
1212 tree anon = 0;
1214 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1215 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1216 anon = lookup_field (TREE_TYPE (field), component, &junk);
1218 if (anon != NULL_TREE)
1220 *indirect = field;
1221 return anon;
1225 if (DECL_NAME (field) == component)
1226 break;
1230 *indirect = NULL_TREE;
1231 return field;
1234 /* Make an expression to refer to the COMPONENT field of
1235 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1237 tree
1238 build_component_ref (datum, component)
1239 tree datum, component;
1241 register tree type = TREE_TYPE (datum);
1242 register enum tree_code code = TREE_CODE (type);
1243 register tree field = NULL;
1244 register tree ref;
1246 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1247 unless we are not to support things not strictly ANSI. */
1248 switch (TREE_CODE (datum))
1250 case COMPOUND_EXPR:
1252 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1253 return build (COMPOUND_EXPR, TREE_TYPE (value),
1254 TREE_OPERAND (datum, 0), value);
1256 case COND_EXPR:
1257 return build_conditional_expr
1258 (TREE_OPERAND (datum, 0),
1259 build_component_ref (TREE_OPERAND (datum, 1), component),
1260 build_component_ref (TREE_OPERAND (datum, 2), component));
1262 default:
1263 break;
1266 /* See if there is a field or component with name COMPONENT. */
1268 if (code == RECORD_TYPE || code == UNION_TYPE)
1270 tree indirect = 0;
1272 if (TYPE_SIZE (type) == 0)
1274 incomplete_type_error (NULL_TREE, type);
1275 return error_mark_node;
1278 field = lookup_field (type, component, &indirect);
1280 if (!field)
1282 error (code == RECORD_TYPE
1283 ? "structure has no member named `%s'"
1284 : "union has no member named `%s'",
1285 IDENTIFIER_POINTER (component));
1286 return error_mark_node;
1288 if (TREE_TYPE (field) == error_mark_node)
1289 return error_mark_node;
1291 /* If FIELD was found buried within an anonymous union,
1292 make one COMPONENT_REF to get that anonymous union,
1293 then fall thru to make a second COMPONENT_REF to get FIELD. */
1294 if (indirect != 0)
1296 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1297 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1298 TREE_READONLY (ref) = 1;
1299 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1300 TREE_THIS_VOLATILE (ref) = 1;
1301 datum = ref;
1304 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1306 if (TREE_READONLY (datum) || TREE_READONLY (field))
1307 TREE_READONLY (ref) = 1;
1308 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1309 TREE_THIS_VOLATILE (ref) = 1;
1311 return ref;
1313 else if (code != ERROR_MARK)
1314 error ("request for member `%s' in something not a structure or union",
1315 IDENTIFIER_POINTER (component));
1317 return error_mark_node;
1320 /* Given an expression PTR for a pointer, return an expression
1321 for the value pointed to.
1322 ERRORSTRING is the name of the operator to appear in error messages. */
1324 tree
1325 build_indirect_ref (ptr, errorstring)
1326 tree ptr;
1327 char *errorstring;
1329 register tree pointer = default_conversion (ptr);
1330 register tree type = TREE_TYPE (pointer);
1332 if (TREE_CODE (type) == POINTER_TYPE)
1334 if (TREE_CODE (pointer) == ADDR_EXPR
1335 && !flag_volatile
1336 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1337 == TREE_TYPE (type)))
1338 return TREE_OPERAND (pointer, 0);
1339 else
1341 tree t = TREE_TYPE (type);
1342 register tree ref = build1 (INDIRECT_REF,
1343 TYPE_MAIN_VARIANT (t), pointer);
1345 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1347 error ("dereferencing pointer to incomplete type");
1348 return error_mark_node;
1350 if (TREE_CODE (t) == VOID_TYPE && skip_evaluation == 0)
1351 warning ("dereferencing `void *' pointer");
1353 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1354 so that we get the proper error message if the result is used
1355 to assign to. Also, &* is supposed to be a no-op.
1356 And ANSI C seems to specify that the type of the result
1357 should be the const type. */
1358 /* A de-reference of a pointer to const is not a const. It is valid
1359 to change it via some other pointer. */
1360 TREE_READONLY (ref) = TYPE_READONLY (t);
1361 TREE_SIDE_EFFECTS (ref)
1362 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1363 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1364 return ref;
1367 else if (TREE_CODE (pointer) != ERROR_MARK)
1368 error ("invalid type argument of `%s'", errorstring);
1369 return error_mark_node;
1372 /* This handles expressions of the form "a[i]", which denotes
1373 an array reference.
1375 This is logically equivalent in C to *(a+i), but we may do it differently.
1376 If A is a variable or a member, we generate a primitive ARRAY_REF.
1377 This avoids forcing the array out of registers, and can work on
1378 arrays that are not lvalues (for example, members of structures returned
1379 by functions). */
1381 tree
1382 build_array_ref (array, index)
1383 tree array, index;
1385 if (index == 0)
1387 error ("subscript missing in array reference");
1388 return error_mark_node;
1391 if (TREE_TYPE (array) == error_mark_node
1392 || TREE_TYPE (index) == error_mark_node)
1393 return error_mark_node;
1395 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1396 && TREE_CODE (array) != INDIRECT_REF)
1398 tree rval, type;
1400 /* Subscripting with type char is likely to lose
1401 on a machine where chars are signed.
1402 So warn on any machine, but optionally.
1403 Don't warn for unsigned char since that type is safe.
1404 Don't warn for signed char because anyone who uses that
1405 must have done so deliberately. */
1406 if (warn_char_subscripts
1407 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1408 warning ("array subscript has type `char'");
1410 /* Apply default promotions *after* noticing character types. */
1411 index = default_conversion (index);
1413 /* Require integer *after* promotion, for sake of enums. */
1414 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1416 error ("array subscript is not an integer");
1417 return error_mark_node;
1420 /* An array that is indexed by a non-constant
1421 cannot be stored in a register; we must be able to do
1422 address arithmetic on its address.
1423 Likewise an array of elements of variable size. */
1424 if (TREE_CODE (index) != INTEGER_CST
1425 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1426 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1428 if (mark_addressable (array) == 0)
1429 return error_mark_node;
1431 /* An array that is indexed by a constant value which is not within
1432 the array bounds cannot be stored in a register either; because we
1433 would get a crash in store_bit_field/extract_bit_field when trying
1434 to access a non-existent part of the register. */
1435 if (TREE_CODE (index) == INTEGER_CST
1436 && TYPE_VALUES (TREE_TYPE (array))
1437 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1439 if (mark_addressable (array) == 0)
1440 return error_mark_node;
1443 if (pedantic && !lvalue_p (array))
1445 if (DECL_REGISTER (array))
1446 pedwarn ("ANSI C forbids subscripting `register' array");
1447 else
1448 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1451 if (pedantic)
1453 tree foo = array;
1454 while (TREE_CODE (foo) == COMPONENT_REF)
1455 foo = TREE_OPERAND (foo, 0);
1456 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1457 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1460 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1461 rval = build (ARRAY_REF, type, array, index);
1462 /* Array ref is const/volatile if the array elements are
1463 or if the array is. */
1464 TREE_READONLY (rval)
1465 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1466 | TREE_READONLY (array));
1467 TREE_SIDE_EFFECTS (rval)
1468 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1469 | TREE_SIDE_EFFECTS (array));
1470 TREE_THIS_VOLATILE (rval)
1471 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1472 /* This was added by rms on 16 Nov 91.
1473 It fixes vol struct foo *a; a->elts[1]
1474 in an inline function.
1475 Hope it doesn't break something else. */
1476 | TREE_THIS_VOLATILE (array));
1477 return require_complete_type (fold (rval));
1481 tree ar = default_conversion (array);
1482 tree ind = default_conversion (index);
1484 /* Do the same warning check as above, but only on the part that's
1485 syntactically the index and only if it is also semantically
1486 the index. */
1487 if (warn_char_subscripts
1488 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1489 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1490 warning ("subscript has type `char'");
1492 /* Put the integer in IND to simplify error checking. */
1493 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1495 tree temp = ar;
1496 ar = ind;
1497 ind = temp;
1500 if (ar == error_mark_node)
1501 return ar;
1503 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1504 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1506 error ("subscripted value is neither array nor pointer");
1507 return error_mark_node;
1509 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1511 error ("array subscript is not an integer");
1512 return error_mark_node;
1515 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1516 "array indexing");
1520 /* Build a function call to function FUNCTION with parameters PARAMS.
1521 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1522 TREE_VALUE of each node is a parameter-expression.
1523 FUNCTION's data type may be a function type or a pointer-to-function. */
1525 tree
1526 build_function_call (function, params)
1527 tree function, params;
1529 register tree fntype, fundecl = 0;
1530 register tree coerced_params;
1531 tree name = NULL_TREE, assembler_name = NULL_TREE;
1533 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1534 STRIP_TYPE_NOPS (function);
1536 /* Convert anything with function type to a pointer-to-function. */
1537 if (TREE_CODE (function) == FUNCTION_DECL)
1539 name = DECL_NAME (function);
1540 assembler_name = DECL_ASSEMBLER_NAME (function);
1542 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1543 (because calling an inline function does not mean the function
1544 needs to be separately compiled). */
1545 fntype = build_type_variant (TREE_TYPE (function),
1546 TREE_READONLY (function),
1547 TREE_THIS_VOLATILE (function));
1548 fundecl = function;
1549 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1551 else
1552 function = default_conversion (function);
1554 fntype = TREE_TYPE (function);
1556 if (TREE_CODE (fntype) == ERROR_MARK)
1557 return error_mark_node;
1559 if (!(TREE_CODE (fntype) == POINTER_TYPE
1560 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1562 error ("called object is not a function");
1563 return error_mark_node;
1566 /* fntype now gets the type of function pointed to. */
1567 fntype = TREE_TYPE (fntype);
1569 /* Convert the parameters to the types declared in the
1570 function prototype, or apply default promotions. */
1572 coerced_params
1573 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1575 /* Check for errors in format strings. */
1577 if (warn_format && (name || assembler_name))
1578 check_function_format (name, assembler_name, coerced_params);
1580 /* Recognize certain built-in functions so we can make tree-codes
1581 other than CALL_EXPR. We do this when it enables fold-const.c
1582 to do something useful. */
1584 if (TREE_CODE (function) == ADDR_EXPR
1585 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1586 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1587 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1589 case BUILT_IN_ABS:
1590 case BUILT_IN_LABS:
1591 case BUILT_IN_FABS:
1592 if (coerced_params == 0)
1593 return integer_zero_node;
1594 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1595 default:
1596 break;
1600 register tree result
1601 = build (CALL_EXPR, TREE_TYPE (fntype),
1602 function, coerced_params, NULL_TREE);
1604 TREE_SIDE_EFFECTS (result) = 1;
1605 if (TREE_TYPE (result) == void_type_node)
1606 return result;
1607 return require_complete_type (result);
1611 /* Convert the argument expressions in the list VALUES
1612 to the types in the list TYPELIST. The result is a list of converted
1613 argument expressions.
1615 If TYPELIST is exhausted, or when an element has NULL as its type,
1616 perform the default conversions.
1618 PARMLIST is the chain of parm decls for the function being called.
1619 It may be 0, if that info is not available.
1620 It is used only for generating error messages.
1622 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1624 This is also where warnings about wrong number of args are generated.
1626 Both VALUES and the returned value are chains of TREE_LIST nodes
1627 with the elements of the list in the TREE_VALUE slots of those nodes. */
1629 static tree
1630 convert_arguments (typelist, values, name, fundecl)
1631 tree typelist, values, name, fundecl;
1633 register tree typetail, valtail;
1634 register tree result = NULL;
1635 int parmnum;
1637 /* Scan the given expressions and types, producing individual
1638 converted arguments and pushing them on RESULT in reverse order. */
1640 for (valtail = values, typetail = typelist, parmnum = 0;
1641 valtail;
1642 valtail = TREE_CHAIN (valtail), parmnum++)
1644 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1645 register tree val = TREE_VALUE (valtail);
1647 if (type == void_type_node)
1649 if (name)
1650 error ("too many arguments to function `%s'",
1651 IDENTIFIER_POINTER (name));
1652 else
1653 error ("too many arguments to function");
1654 break;
1657 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1658 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1659 to convert automatically to a pointer. */
1660 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1661 val = TREE_OPERAND (val, 0);
1663 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1664 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1665 val = default_conversion (val);
1667 val = require_complete_type (val);
1669 if (type != 0)
1671 /* Formal parm type is specified by a function prototype. */
1672 tree parmval;
1674 if (TYPE_SIZE (type) == 0)
1676 error ("type of formal parameter %d is incomplete", parmnum + 1);
1677 parmval = val;
1679 else
1681 /* Optionally warn about conversions that
1682 differ from the default conversions. */
1683 if (warn_conversion)
1685 int formal_prec = TYPE_PRECISION (type);
1687 if (INTEGRAL_TYPE_P (type)
1688 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1689 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1690 else if (TREE_CODE (type) == COMPLEX_TYPE
1691 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1692 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1693 else if (TREE_CODE (type) == REAL_TYPE
1694 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1695 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1696 else if (TREE_CODE (type) == REAL_TYPE
1697 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1698 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1699 /* ??? At some point, messages should be written about
1700 conversions between complex types, but that's too messy
1701 to do now. */
1702 else if (TREE_CODE (type) == REAL_TYPE
1703 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1705 /* Warn if any argument is passed as `float',
1706 since without a prototype it would be `double'. */
1707 if (formal_prec == TYPE_PRECISION (float_type_node))
1708 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1710 /* Detect integer changing in width or signedness. */
1711 else if (INTEGRAL_TYPE_P (type)
1712 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1714 tree would_have_been = default_conversion (val);
1715 tree type1 = TREE_TYPE (would_have_been);
1717 if (TREE_CODE (type) == ENUMERAL_TYPE
1718 && type == TREE_TYPE (val))
1719 /* No warning if function asks for enum
1720 and the actual arg is that enum type. */
1722 else if (formal_prec != TYPE_PRECISION (type1))
1723 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1724 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1726 /* Don't complain if the formal parameter type
1727 is an enum, because we can't tell now whether
1728 the value was an enum--even the same enum. */
1729 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1731 else if (TREE_CODE (val) == INTEGER_CST
1732 && int_fits_type_p (val, type))
1733 /* Change in signedness doesn't matter
1734 if a constant value is unaffected. */
1736 /* Likewise for a constant in a NOP_EXPR. */
1737 else if (TREE_CODE (val) == NOP_EXPR
1738 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1739 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1741 #if 0 /* We never get such tree structure here. */
1742 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1743 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1744 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1745 /* Change in signedness doesn't matter
1746 if an enum value is unaffected. */
1748 #endif
1749 /* If the value is extended from a narrower
1750 unsigned type, it doesn't matter whether we
1751 pass it as signed or unsigned; the value
1752 certainly is the same either way. */
1753 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1754 && TREE_UNSIGNED (TREE_TYPE (val)))
1756 else if (TREE_UNSIGNED (type))
1757 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1758 else
1759 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1763 parmval = convert_for_assignment (type, val,
1764 (char *) 0, /* arg passing */
1765 fundecl, name, parmnum + 1);
1767 #ifdef PROMOTE_PROTOTYPES
1768 if ((TREE_CODE (type) == INTEGER_TYPE
1769 || TREE_CODE (type) == ENUMERAL_TYPE)
1770 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1771 parmval = default_conversion (parmval);
1772 #endif
1774 result = tree_cons (NULL_TREE, parmval, result);
1776 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1777 && (TYPE_PRECISION (TREE_TYPE (val))
1778 < TYPE_PRECISION (double_type_node)))
1779 /* Convert `float' to `double'. */
1780 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1781 else
1782 /* Convert `short' and `char' to full-size `int'. */
1783 result = tree_cons (NULL_TREE, default_conversion (val), result);
1785 if (typetail)
1786 typetail = TREE_CHAIN (typetail);
1789 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1791 if (name)
1792 error ("too few arguments to function `%s'",
1793 IDENTIFIER_POINTER (name));
1794 else
1795 error ("too few arguments to function");
1798 return nreverse (result);
1801 /* This is the entry point used by the parser
1802 for binary operators in the input.
1803 In addition to constructing the expression,
1804 we check for operands that were written with other binary operators
1805 in a way that is likely to confuse the user. */
1807 tree
1808 parser_build_binary_op (code, arg1, arg2)
1809 enum tree_code code;
1810 tree arg1, arg2;
1812 tree result = build_binary_op (code, arg1, arg2, 1);
1814 char class;
1815 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1816 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1817 enum tree_code code1 = ERROR_MARK;
1818 enum tree_code code2 = ERROR_MARK;
1820 if (class1 == 'e' || class1 == '1'
1821 || class1 == '2' || class1 == '<')
1822 code1 = C_EXP_ORIGINAL_CODE (arg1);
1823 if (class2 == 'e' || class2 == '1'
1824 || class2 == '2' || class2 == '<')
1825 code2 = C_EXP_ORIGINAL_CODE (arg2);
1827 /* Check for cases such as x+y<<z which users are likely
1828 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1829 is cleared to prevent these warnings. */
1830 if (warn_parentheses)
1832 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1834 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1835 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1836 warning ("suggest parentheses around + or - inside shift");
1839 if (code == TRUTH_ORIF_EXPR)
1841 if (code1 == TRUTH_ANDIF_EXPR
1842 || code2 == TRUTH_ANDIF_EXPR)
1843 warning ("suggest parentheses around && within ||");
1846 if (code == BIT_IOR_EXPR)
1848 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1849 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1850 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1851 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1852 warning ("suggest parentheses around arithmetic in operand of |");
1853 /* Check cases like x|y==z */
1854 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1855 warning ("suggest parentheses around comparison in operand of |");
1858 if (code == BIT_XOR_EXPR)
1860 if (code1 == BIT_AND_EXPR
1861 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1862 || code2 == BIT_AND_EXPR
1863 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1864 warning ("suggest parentheses around arithmetic in operand of ^");
1865 /* Check cases like x^y==z */
1866 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1867 warning ("suggest parentheses around comparison in operand of ^");
1870 if (code == BIT_AND_EXPR)
1872 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1873 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1874 warning ("suggest parentheses around + or - in operand of &");
1875 /* Check cases like x&y==z */
1876 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1877 warning ("suggest parentheses around comparison in operand of &");
1881 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1882 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1883 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1884 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1886 unsigned_conversion_warning (result, arg1);
1887 unsigned_conversion_warning (result, arg2);
1888 overflow_warning (result);
1890 class = TREE_CODE_CLASS (TREE_CODE (result));
1892 /* Record the code that was specified in the source,
1893 for the sake of warnings about confusing nesting. */
1894 if (class == 'e' || class == '1'
1895 || class == '2' || class == '<')
1896 C_SET_EXP_ORIGINAL_CODE (result, code);
1897 else
1899 int flag = TREE_CONSTANT (result);
1900 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1901 so that convert_for_assignment wouldn't strip it.
1902 That way, we got warnings for things like p = (1 - 1).
1903 But it turns out we should not get those warnings. */
1904 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1905 C_SET_EXP_ORIGINAL_CODE (result, code);
1906 TREE_CONSTANT (result) = flag;
1909 return result;
1912 /* Build a binary-operation expression without default conversions.
1913 CODE is the kind of expression to build.
1914 This function differs from `build' in several ways:
1915 the data type of the result is computed and recorded in it,
1916 warnings are generated if arg data types are invalid,
1917 special handling for addition and subtraction of pointers is known,
1918 and some optimization is done (operations on narrow ints
1919 are done in the narrower type when that gives the same result).
1920 Constant folding is also done before the result is returned.
1922 Note that the operands will never have enumeral types, or function
1923 or array types, because either they will have the default conversions
1924 performed or they have both just been converted to some other type in which
1925 the arithmetic is to be done. */
1927 tree
1928 build_binary_op (code, orig_op0, orig_op1, convert_p)
1929 enum tree_code code;
1930 tree orig_op0, orig_op1;
1931 int convert_p;
1933 tree type0, type1;
1934 register enum tree_code code0, code1;
1935 tree op0, op1;
1937 /* Expression code to give to the expression when it is built.
1938 Normally this is CODE, which is what the caller asked for,
1939 but in some special cases we change it. */
1940 register enum tree_code resultcode = code;
1942 /* Data type in which the computation is to be performed.
1943 In the simplest cases this is the common type of the arguments. */
1944 register tree result_type = NULL;
1946 /* Nonzero means operands have already been type-converted
1947 in whatever way is necessary.
1948 Zero means they need to be converted to RESULT_TYPE. */
1949 int converted = 0;
1951 /* Nonzero means create the expression with this type, rather than
1952 RESULT_TYPE. */
1953 tree build_type = 0;
1955 /* Nonzero means after finally constructing the expression
1956 convert it to this type. */
1957 tree final_type = 0;
1959 /* Nonzero if this is an operation like MIN or MAX which can
1960 safely be computed in short if both args are promoted shorts.
1961 Also implies COMMON.
1962 -1 indicates a bitwise operation; this makes a difference
1963 in the exact conditions for when it is safe to do the operation
1964 in a narrower mode. */
1965 int shorten = 0;
1967 /* Nonzero if this is a comparison operation;
1968 if both args are promoted shorts, compare the original shorts.
1969 Also implies COMMON. */
1970 int short_compare = 0;
1972 /* Nonzero if this is a right-shift operation, which can be computed on the
1973 original short and then promoted if the operand is a promoted short. */
1974 int short_shift = 0;
1976 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1977 int common = 0;
1979 if (convert_p)
1981 op0 = default_conversion (orig_op0);
1982 op1 = default_conversion (orig_op1);
1984 else
1986 op0 = orig_op0;
1987 op1 = orig_op1;
1990 type0 = TREE_TYPE (op0);
1991 type1 = TREE_TYPE (op1);
1993 /* The expression codes of the data types of the arguments tell us
1994 whether the arguments are integers, floating, pointers, etc. */
1995 code0 = TREE_CODE (type0);
1996 code1 = TREE_CODE (type1);
1998 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1999 STRIP_TYPE_NOPS (op0);
2000 STRIP_TYPE_NOPS (op1);
2002 /* If an error was already reported for one of the arguments,
2003 avoid reporting another error. */
2005 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2006 return error_mark_node;
2008 switch (code)
2010 case PLUS_EXPR:
2011 /* Handle the pointer + int case. */
2012 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2013 return pointer_int_sum (PLUS_EXPR, op0, op1);
2014 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2015 return pointer_int_sum (PLUS_EXPR, op1, op0);
2016 else
2017 common = 1;
2018 break;
2020 case MINUS_EXPR:
2021 /* Subtraction of two similar pointers.
2022 We must subtract them as integers, then divide by object size. */
2023 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2024 && comp_target_types (type0, type1))
2025 return pointer_diff (op0, op1);
2026 /* Handle pointer minus int. Just like pointer plus int. */
2027 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2028 return pointer_int_sum (MINUS_EXPR, op0, op1);
2029 else
2030 common = 1;
2031 break;
2033 case MULT_EXPR:
2034 common = 1;
2035 break;
2037 case TRUNC_DIV_EXPR:
2038 case CEIL_DIV_EXPR:
2039 case FLOOR_DIV_EXPR:
2040 case ROUND_DIV_EXPR:
2041 case EXACT_DIV_EXPR:
2042 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2043 || code0 == COMPLEX_TYPE)
2044 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2045 || code1 == COMPLEX_TYPE))
2047 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2048 resultcode = RDIV_EXPR;
2049 else
2051 /* Although it would be tempting to shorten always here, that
2052 loses on some targets, since the modulo instruction is
2053 undefined if the quotient can't be represented in the
2054 computation mode. We shorten only if unsigned or if
2055 dividing by something we know != -1. */
2056 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2057 || (TREE_CODE (op1) == INTEGER_CST
2058 && (TREE_INT_CST_LOW (op1) != -1
2059 || TREE_INT_CST_HIGH (op1) != -1)));
2061 common = 1;
2063 break;
2065 case BIT_AND_EXPR:
2066 case BIT_ANDTC_EXPR:
2067 case BIT_IOR_EXPR:
2068 case BIT_XOR_EXPR:
2069 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2070 shorten = -1;
2071 /* If one operand is a constant, and the other is a short type
2072 that has been converted to an int,
2073 really do the work in the short type and then convert the
2074 result to int. If we are lucky, the constant will be 0 or 1
2075 in the short type, making the entire operation go away. */
2076 if (TREE_CODE (op0) == INTEGER_CST
2077 && TREE_CODE (op1) == NOP_EXPR
2078 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2079 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2081 final_type = result_type;
2082 op1 = TREE_OPERAND (op1, 0);
2083 result_type = TREE_TYPE (op1);
2085 if (TREE_CODE (op1) == INTEGER_CST
2086 && TREE_CODE (op0) == NOP_EXPR
2087 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2088 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2090 final_type = result_type;
2091 op0 = TREE_OPERAND (op0, 0);
2092 result_type = TREE_TYPE (op0);
2094 break;
2096 case TRUNC_MOD_EXPR:
2097 case FLOOR_MOD_EXPR:
2098 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2100 /* Although it would be tempting to shorten always here, that loses
2101 on some targets, since the modulo instruction is undefined if the
2102 quotient can't be represented in the computation mode. We shorten
2103 only if unsigned or if dividing by something we know != -1. */
2104 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2105 || (TREE_CODE (op1) == INTEGER_CST
2106 && (TREE_INT_CST_LOW (op1) != -1
2107 || TREE_INT_CST_HIGH (op1) != -1)));
2108 common = 1;
2110 break;
2112 case TRUTH_ANDIF_EXPR:
2113 case TRUTH_ORIF_EXPR:
2114 case TRUTH_AND_EXPR:
2115 case TRUTH_OR_EXPR:
2116 case TRUTH_XOR_EXPR:
2117 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2118 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2119 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2120 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2122 /* Result of these operations is always an int,
2123 but that does not mean the operands should be
2124 converted to ints! */
2125 result_type = integer_type_node;
2126 op0 = truthvalue_conversion (op0);
2127 op1 = truthvalue_conversion (op1);
2128 converted = 1;
2130 break;
2132 /* Shift operations: result has same type as first operand;
2133 always convert second operand to int.
2134 Also set SHORT_SHIFT if shifting rightward. */
2136 case RSHIFT_EXPR:
2137 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2139 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2141 if (tree_int_cst_sgn (op1) < 0)
2142 warning ("right shift count is negative");
2143 else
2145 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2146 short_shift = 1;
2147 if (TREE_INT_CST_HIGH (op1) != 0
2148 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2149 >= TYPE_PRECISION (type0)))
2150 warning ("right shift count >= width of type");
2153 /* Use the type of the value to be shifted.
2154 This is what most traditional C compilers do. */
2155 result_type = type0;
2156 /* Unless traditional, convert the shift-count to an integer,
2157 regardless of size of value being shifted. */
2158 if (! flag_traditional)
2160 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2161 op1 = convert (integer_type_node, op1);
2162 /* Avoid converting op1 to result_type later. */
2163 converted = 1;
2166 break;
2168 case LSHIFT_EXPR:
2169 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2171 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2173 if (tree_int_cst_sgn (op1) < 0)
2174 warning ("left shift count is negative");
2175 else if (TREE_INT_CST_HIGH (op1) != 0
2176 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2177 >= TYPE_PRECISION (type0)))
2178 warning ("left shift count >= width of type");
2180 /* Use the type of the value to be shifted.
2181 This is what most traditional C compilers do. */
2182 result_type = type0;
2183 /* Unless traditional, convert the shift-count to an integer,
2184 regardless of size of value being shifted. */
2185 if (! flag_traditional)
2187 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2188 op1 = convert (integer_type_node, op1);
2189 /* Avoid converting op1 to result_type later. */
2190 converted = 1;
2193 break;
2195 case RROTATE_EXPR:
2196 case LROTATE_EXPR:
2197 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2199 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2201 if (tree_int_cst_sgn (op1) < 0)
2202 warning ("shift count is negative");
2203 else if (TREE_INT_CST_HIGH (op1) != 0
2204 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2205 >= TYPE_PRECISION (type0)))
2206 warning ("shift count >= width of type");
2208 /* Use the type of the value to be shifted.
2209 This is what most traditional C compilers do. */
2210 result_type = type0;
2211 /* Unless traditional, convert the shift-count to an integer,
2212 regardless of size of value being shifted. */
2213 if (! flag_traditional)
2215 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2216 op1 = convert (integer_type_node, op1);
2217 /* Avoid converting op1 to result_type later. */
2218 converted = 1;
2221 break;
2223 case EQ_EXPR:
2224 case NE_EXPR:
2225 /* Result of comparison is always int,
2226 but don't convert the args to int! */
2227 build_type = integer_type_node;
2228 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2229 || code0 == COMPLEX_TYPE)
2230 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2231 || code1 == COMPLEX_TYPE))
2232 short_compare = 1;
2233 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2235 register tree tt0 = TREE_TYPE (type0);
2236 register tree tt1 = TREE_TYPE (type1);
2237 /* Anything compares with void *. void * compares with anything.
2238 Otherwise, the targets must be compatible
2239 and both must be object or both incomplete. */
2240 if (comp_target_types (type0, type1))
2241 result_type = common_type (type0, type1);
2242 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2244 /* op0 != orig_op0 detects the case of something
2245 whose value is 0 but which isn't a valid null ptr const. */
2246 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2247 && TREE_CODE (tt1) == FUNCTION_TYPE)
2248 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2250 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2252 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2253 && TREE_CODE (tt0) == FUNCTION_TYPE)
2254 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2256 else
2257 pedwarn ("comparison of distinct pointer types lacks a cast");
2259 if (result_type == NULL_TREE)
2260 result_type = ptr_type_node;
2262 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2263 && integer_zerop (op1))
2264 result_type = type0;
2265 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2266 && integer_zerop (op0))
2267 result_type = type1;
2268 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2270 result_type = type0;
2271 if (! flag_traditional)
2272 pedwarn ("comparison between pointer and integer");
2274 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2276 result_type = type1;
2277 if (! flag_traditional)
2278 pedwarn ("comparison between pointer and integer");
2280 break;
2282 case MAX_EXPR:
2283 case MIN_EXPR:
2284 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2285 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2286 shorten = 1;
2287 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2289 if (comp_target_types (type0, type1))
2291 result_type = common_type (type0, type1);
2292 if (pedantic
2293 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2294 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2296 else
2298 result_type = ptr_type_node;
2299 pedwarn ("comparison of distinct pointer types lacks a cast");
2302 break;
2304 case LE_EXPR:
2305 case GE_EXPR:
2306 case LT_EXPR:
2307 case GT_EXPR:
2308 build_type = integer_type_node;
2309 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2310 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2311 short_compare = 1;
2312 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2314 if (comp_target_types (type0, type1))
2316 result_type = common_type (type0, type1);
2317 if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2318 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2319 pedwarn ("comparison of complete and incomplete pointers");
2320 else if (pedantic
2321 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2322 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2324 else
2326 result_type = ptr_type_node;
2327 pedwarn ("comparison of distinct pointer types lacks a cast");
2330 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2331 && integer_zerop (op1))
2333 result_type = type0;
2334 if (pedantic || extra_warnings)
2335 pedwarn ("ordered comparison of pointer with integer zero");
2337 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2338 && integer_zerop (op0))
2340 result_type = type1;
2341 if (pedantic)
2342 pedwarn ("ordered comparison of pointer with integer zero");
2344 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2346 result_type = type0;
2347 if (! flag_traditional)
2348 pedwarn ("comparison between pointer and integer");
2350 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2352 result_type = type1;
2353 if (! flag_traditional)
2354 pedwarn ("comparison between pointer and integer");
2356 break;
2358 default:
2359 break;
2362 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2364 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2366 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2368 if (shorten || common || short_compare)
2369 result_type = common_type (type0, type1);
2371 /* For certain operations (which identify themselves by shorten != 0)
2372 if both args were extended from the same smaller type,
2373 do the arithmetic in that type and then extend.
2375 shorten !=0 and !=1 indicates a bitwise operation.
2376 For them, this optimization is safe only if
2377 both args are zero-extended or both are sign-extended.
2378 Otherwise, we might change the result.
2379 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2380 but calculated in (unsigned short) it would be (unsigned short)-1. */
2382 if (shorten && none_complex)
2384 int unsigned0, unsigned1;
2385 tree arg0 = get_narrower (op0, &unsigned0);
2386 tree arg1 = get_narrower (op1, &unsigned1);
2387 /* UNS is 1 if the operation to be done is an unsigned one. */
2388 int uns = TREE_UNSIGNED (result_type);
2389 tree type;
2391 final_type = result_type;
2393 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2394 but it *requires* conversion to FINAL_TYPE. */
2396 if ((TYPE_PRECISION (TREE_TYPE (op0))
2397 == TYPE_PRECISION (TREE_TYPE (arg0)))
2398 && TREE_TYPE (op0) != final_type)
2399 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2400 if ((TYPE_PRECISION (TREE_TYPE (op1))
2401 == TYPE_PRECISION (TREE_TYPE (arg1)))
2402 && TREE_TYPE (op1) != final_type)
2403 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2405 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2407 /* For bitwise operations, signedness of nominal type
2408 does not matter. Consider only how operands were extended. */
2409 if (shorten == -1)
2410 uns = unsigned0;
2412 /* Note that in all three cases below we refrain from optimizing
2413 an unsigned operation on sign-extended args.
2414 That would not be valid. */
2416 /* Both args variable: if both extended in same way
2417 from same width, do it in that width.
2418 Do it unsigned if args were zero-extended. */
2419 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2420 < TYPE_PRECISION (result_type))
2421 && (TYPE_PRECISION (TREE_TYPE (arg1))
2422 == TYPE_PRECISION (TREE_TYPE (arg0)))
2423 && unsigned0 == unsigned1
2424 && (unsigned0 || !uns))
2425 result_type
2426 = signed_or_unsigned_type (unsigned0,
2427 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2428 else if (TREE_CODE (arg0) == INTEGER_CST
2429 && (unsigned1 || !uns)
2430 && (TYPE_PRECISION (TREE_TYPE (arg1))
2431 < TYPE_PRECISION (result_type))
2432 && (type = signed_or_unsigned_type (unsigned1,
2433 TREE_TYPE (arg1)),
2434 int_fits_type_p (arg0, type)))
2435 result_type = type;
2436 else if (TREE_CODE (arg1) == INTEGER_CST
2437 && (unsigned0 || !uns)
2438 && (TYPE_PRECISION (TREE_TYPE (arg0))
2439 < TYPE_PRECISION (result_type))
2440 && (type = signed_or_unsigned_type (unsigned0,
2441 TREE_TYPE (arg0)),
2442 int_fits_type_p (arg1, type)))
2443 result_type = type;
2446 /* Shifts can be shortened if shifting right. */
2448 if (short_shift)
2450 int unsigned_arg;
2451 tree arg0 = get_narrower (op0, &unsigned_arg);
2453 final_type = result_type;
2455 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2456 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2458 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2459 /* We can shorten only if the shift count is less than the
2460 number of bits in the smaller type size. */
2461 && TREE_INT_CST_HIGH (op1) == 0
2462 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
2463 /* If arg is sign-extended and then unsigned-shifted,
2464 we can simulate this with a signed shift in arg's type
2465 only if the extended result is at least twice as wide
2466 as the arg. Otherwise, the shift could use up all the
2467 ones made by sign-extension and bring in zeros.
2468 We can't optimize that case at all, but in most machines
2469 it never happens because available widths are 2**N. */
2470 && (!TREE_UNSIGNED (final_type)
2471 || unsigned_arg
2472 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2474 /* Do an unsigned shift if the operand was zero-extended. */
2475 result_type
2476 = signed_or_unsigned_type (unsigned_arg,
2477 TREE_TYPE (arg0));
2478 /* Convert value-to-be-shifted to that type. */
2479 if (TREE_TYPE (op0) != result_type)
2480 op0 = convert (result_type, op0);
2481 converted = 1;
2485 /* Comparison operations are shortened too but differently.
2486 They identify themselves by setting short_compare = 1. */
2488 if (short_compare)
2490 /* Don't write &op0, etc., because that would prevent op0
2491 from being kept in a register.
2492 Instead, make copies of the our local variables and
2493 pass the copies by reference, then copy them back afterward. */
2494 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2495 enum tree_code xresultcode = resultcode;
2496 tree val
2497 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2498 if (val != 0)
2499 return val;
2500 op0 = xop0, op1 = xop1;
2501 converted = 1;
2502 resultcode = xresultcode;
2504 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2505 && skip_evaluation == 0)
2507 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2508 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2510 int unsignedp0, unsignedp1;
2511 tree primop0 = get_narrower (op0, &unsignedp0);
2512 tree primop1 = get_narrower (op1, &unsignedp1);
2514 /* Avoid spurious warnings for comparison with enumerators. */
2516 xop0 = orig_op0;
2517 xop1 = orig_op1;
2518 STRIP_TYPE_NOPS (xop0);
2519 STRIP_TYPE_NOPS (xop1);
2521 /* Give warnings for comparisons between signed and unsigned
2522 quantities that may fail. */
2523 /* Do the checking based on the original operand trees, so that
2524 casts will be considered, but default promotions won't be. */
2526 /* Do not warn if the comparison is being done in a signed type,
2527 since the signed type will only be chosen if it can represent
2528 all the values of the unsigned type. */
2529 if (! TREE_UNSIGNED (result_type))
2530 /* OK */;
2531 /* Do not warn if both operands are unsigned. */
2532 else if (op0_signed == op1_signed)
2533 /* OK */;
2534 /* Do not warn if the signed quantity is an unsuffixed
2535 integer literal (or some static constant expression
2536 involving such literals) and it is non-negative. */
2537 else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST
2538 && tree_int_cst_sgn (xop0) >= 0)
2539 || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
2540 && tree_int_cst_sgn (xop1) >= 0))
2541 /* OK */;
2542 /* Do not warn if the comparison is an equality operation,
2543 the unsigned quantity is an integral constant and it does
2544 not use the most significant bit of result_type. */
2545 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
2546 && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST
2547 && int_fits_type_p (xop1, signed_type (result_type)))
2548 || (op1_signed && TREE_CODE (xop0) == INTEGER_CST
2549 && int_fits_type_p (xop0, signed_type (result_type)))))
2550 /* OK */;
2551 else
2552 warning ("comparison between signed and unsigned");
2554 /* Warn if two unsigned values are being compared in a size
2555 larger than their original size, and one (and only one) is the
2556 result of a `~' operator. This comparison will always fail.
2558 Also warn if one operand is a constant, and the constant
2559 does not have all bits set that are set in the ~ operand
2560 when it is extended. */
2562 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2563 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2565 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2566 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2567 &unsignedp0);
2568 else
2569 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2570 &unsignedp1);
2572 if (TREE_CODE (primop0) == INTEGER_CST
2573 || TREE_CODE (primop1) == INTEGER_CST)
2575 tree primop;
2576 long constant, mask;
2577 int unsignedp, bits;
2579 if (TREE_CODE (primop0) == INTEGER_CST)
2581 primop = primop1;
2582 unsignedp = unsignedp1;
2583 constant = TREE_INT_CST_LOW (primop0);
2585 else
2587 primop = primop0;
2588 unsignedp = unsignedp0;
2589 constant = TREE_INT_CST_LOW (primop1);
2592 bits = TYPE_PRECISION (TREE_TYPE (primop));
2593 if (bits < TYPE_PRECISION (result_type)
2594 && bits < HOST_BITS_PER_LONG && unsignedp)
2596 mask = (~0L) << bits;
2597 if ((mask & constant) != mask)
2598 warning ("comparison of promoted ~unsigned with constant");
2601 else if (unsignedp0 && unsignedp1
2602 && (TYPE_PRECISION (TREE_TYPE (primop0))
2603 < TYPE_PRECISION (result_type))
2604 && (TYPE_PRECISION (TREE_TYPE (primop1))
2605 < TYPE_PRECISION (result_type)))
2606 warning ("comparison of promoted ~unsigned with unsigned");
2612 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2613 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2614 Then the expression will be built.
2615 It will be given type FINAL_TYPE if that is nonzero;
2616 otherwise, it will be given type RESULT_TYPE. */
2618 if (!result_type)
2620 binary_op_error (code);
2621 return error_mark_node;
2624 if (! converted)
2626 if (TREE_TYPE (op0) != result_type)
2627 op0 = convert (result_type, op0);
2628 if (TREE_TYPE (op1) != result_type)
2629 op1 = convert (result_type, op1);
2632 if (build_type == NULL_TREE)
2633 build_type = result_type;
2636 register tree result = build (resultcode, build_type, op0, op1);
2637 register tree folded;
2639 folded = fold (result);
2640 if (folded == result)
2641 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2642 if (final_type != 0)
2643 return convert (final_type, folded);
2644 return folded;
2648 /* Return a tree for the sum or difference (RESULTCODE says which)
2649 of pointer PTROP and integer INTOP. */
2651 static tree
2652 pointer_int_sum (resultcode, ptrop, intop)
2653 enum tree_code resultcode;
2654 register tree ptrop, intop;
2656 tree size_exp;
2658 register tree result;
2659 register tree folded;
2661 /* The result is a pointer of the same type that is being added. */
2663 register tree result_type = TREE_TYPE (ptrop);
2665 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2667 if (pedantic || warn_pointer_arith)
2668 pedwarn ("pointer of type `void *' used in arithmetic");
2669 size_exp = integer_one_node;
2671 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2673 if (pedantic || warn_pointer_arith)
2674 pedwarn ("pointer to a function used in arithmetic");
2675 size_exp = integer_one_node;
2677 else
2678 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2680 /* If what we are about to multiply by the size of the elements
2681 contains a constant term, apply distributive law
2682 and multiply that constant term separately.
2683 This helps produce common subexpressions. */
2685 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2686 && ! TREE_CONSTANT (intop)
2687 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2688 && TREE_CONSTANT (size_exp)
2689 /* If the constant comes from pointer subtraction,
2690 skip this optimization--it would cause an error. */
2691 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2692 /* If the constant is unsigned, and smaller than the pointer size,
2693 then we must skip this optimization. This is because it could cause
2694 an overflow error if the constant is negative but INTOP is not. */
2695 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2696 || (TYPE_PRECISION (TREE_TYPE (intop))
2697 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2699 enum tree_code subcode = resultcode;
2700 tree int_type = TREE_TYPE (intop);
2701 if (TREE_CODE (intop) == MINUS_EXPR)
2702 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2703 /* Convert both subexpression types to the type of intop,
2704 because weird cases involving pointer arithmetic
2705 can result in a sum or difference with different type args. */
2706 ptrop = build_binary_op (subcode, ptrop,
2707 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2708 intop = convert (int_type, TREE_OPERAND (intop, 0));
2711 /* Convert the integer argument to a type the same size as sizetype
2712 so the multiply won't overflow spuriously. */
2714 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2715 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2716 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2717 TREE_UNSIGNED (sizetype)), intop);
2719 /* Replace the integer argument with a suitable product by the object size.
2720 Do this multiplication as signed, then convert to the appropriate
2721 pointer type (actually unsigned integral). */
2723 intop = convert (result_type,
2724 build_binary_op (MULT_EXPR, intop,
2725 convert (TREE_TYPE (intop), size_exp), 1));
2727 /* Create the sum or difference. */
2729 result = build (resultcode, result_type, ptrop, intop);
2731 folded = fold (result);
2732 if (folded == result)
2733 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2734 return folded;
2737 /* Return a tree for the difference of pointers OP0 and OP1.
2738 The resulting tree has type int. */
2740 static tree
2741 pointer_diff (op0, op1)
2742 register tree op0, op1;
2744 register tree result, folded;
2745 tree restype = ptrdiff_type_node;
2747 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2749 if (pedantic || warn_pointer_arith)
2751 if (TREE_CODE (target_type) == VOID_TYPE)
2752 pedwarn ("pointer of type `void *' used in subtraction");
2753 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2754 pedwarn ("pointer to a function used in subtraction");
2757 /* First do the subtraction as integers;
2758 then drop through to build the divide operator.
2759 Do not do default conversions on the minus operator
2760 in case restype is a short type. */
2762 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2763 convert (restype, op1), 0);
2764 /* This generates an error if op1 is pointer to incomplete type. */
2765 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2766 error ("arithmetic on pointer to an incomplete type");
2768 /* This generates an error if op0 is pointer to incomplete type. */
2769 op1 = c_size_in_bytes (target_type);
2771 /* Divide by the size, in easiest possible way. */
2773 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2775 folded = fold (result);
2776 if (folded == result)
2777 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2778 return folded;
2781 /* Construct and perhaps optimize a tree representation
2782 for a unary operation. CODE, a tree_code, specifies the operation
2783 and XARG is the operand. NOCONVERT nonzero suppresses
2784 the default promotions (such as from short to int). */
2786 tree
2787 build_unary_op (code, xarg, noconvert)
2788 enum tree_code code;
2789 tree xarg;
2790 int noconvert;
2792 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2793 register tree arg = xarg;
2794 register tree argtype = 0;
2795 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2796 char *errstring = NULL;
2797 tree val;
2799 if (typecode == ERROR_MARK)
2800 return error_mark_node;
2801 if (typecode == ENUMERAL_TYPE)
2802 typecode = INTEGER_TYPE;
2804 switch (code)
2806 case CONVERT_EXPR:
2807 /* This is used for unary plus, because a CONVERT_EXPR
2808 is enough to prevent anybody from looking inside for
2809 associativity, but won't generate any code. */
2810 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2811 || typecode == COMPLEX_TYPE))
2812 errstring = "wrong type argument to unary plus";
2813 else if (!noconvert)
2814 arg = default_conversion (arg);
2815 break;
2817 case NEGATE_EXPR:
2818 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2819 || typecode == COMPLEX_TYPE))
2820 errstring = "wrong type argument to unary minus";
2821 else if (!noconvert)
2822 arg = default_conversion (arg);
2823 break;
2825 case BIT_NOT_EXPR:
2826 if (typecode == COMPLEX_TYPE)
2828 code = CONJ_EXPR;
2829 if (!noconvert)
2830 arg = default_conversion (arg);
2832 else if (typecode != INTEGER_TYPE)
2833 errstring = "wrong type argument to bit-complement";
2834 else if (!noconvert)
2835 arg = default_conversion (arg);
2836 break;
2838 case ABS_EXPR:
2839 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2840 || typecode == COMPLEX_TYPE))
2841 errstring = "wrong type argument to abs";
2842 else if (!noconvert)
2843 arg = default_conversion (arg);
2844 break;
2846 case CONJ_EXPR:
2847 /* Conjugating a real value is a no-op, but allow it anyway. */
2848 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2849 || typecode == COMPLEX_TYPE))
2850 errstring = "wrong type argument to conjugation";
2851 else if (!noconvert)
2852 arg = default_conversion (arg);
2853 break;
2855 case TRUTH_NOT_EXPR:
2856 if (typecode != INTEGER_TYPE
2857 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2858 && typecode != COMPLEX_TYPE
2859 /* These will convert to a pointer. */
2860 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2862 errstring = "wrong type argument to unary exclamation mark";
2863 break;
2865 arg = truthvalue_conversion (arg);
2866 return invert_truthvalue (arg);
2868 case NOP_EXPR:
2869 break;
2871 case REALPART_EXPR:
2872 if (TREE_CODE (arg) == COMPLEX_CST)
2873 return TREE_REALPART (arg);
2874 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2875 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2876 else
2877 return arg;
2879 case IMAGPART_EXPR:
2880 if (TREE_CODE (arg) == COMPLEX_CST)
2881 return TREE_IMAGPART (arg);
2882 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2883 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2884 else
2885 return convert (TREE_TYPE (arg), integer_zero_node);
2887 case PREINCREMENT_EXPR:
2888 case POSTINCREMENT_EXPR:
2889 case PREDECREMENT_EXPR:
2890 case POSTDECREMENT_EXPR:
2891 /* Handle complex lvalues (when permitted)
2892 by reduction to simpler cases. */
2894 val = unary_complex_lvalue (code, arg);
2895 if (val != 0)
2896 return val;
2898 /* Increment or decrement the real part of the value,
2899 and don't change the imaginary part. */
2900 if (typecode == COMPLEX_TYPE)
2902 tree real, imag;
2904 arg = stabilize_reference (arg);
2905 real = build_unary_op (REALPART_EXPR, arg, 1);
2906 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2907 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2908 build_unary_op (code, real, 1), imag);
2911 /* Report invalid types. */
2913 if (typecode != POINTER_TYPE
2914 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2916 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2917 errstring ="wrong type argument to increment";
2918 else
2919 errstring ="wrong type argument to decrement";
2920 break;
2924 register tree inc;
2925 tree result_type = TREE_TYPE (arg);
2927 arg = get_unwidened (arg, 0);
2928 argtype = TREE_TYPE (arg);
2930 /* Compute the increment. */
2932 if (typecode == POINTER_TYPE)
2934 /* If pointer target is an undefined struct,
2935 we just cannot know how to do the arithmetic. */
2936 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2937 error ("%s of pointer to unknown structure",
2938 ((code == PREINCREMENT_EXPR
2939 || code == POSTINCREMENT_EXPR)
2940 ? "increment" : "decrement"));
2941 else if ((pedantic || warn_pointer_arith)
2942 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2943 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2944 pedwarn ("wrong type argument to %s",
2945 ((code == PREINCREMENT_EXPR
2946 || code == POSTINCREMENT_EXPR)
2947 ? "increment" : "decrement"));
2948 inc = c_size_in_bytes (TREE_TYPE (result_type));
2950 else
2951 inc = integer_one_node;
2953 inc = convert (argtype, inc);
2955 /* Handle incrementing a cast-expression. */
2957 while (1)
2958 switch (TREE_CODE (arg))
2960 case NOP_EXPR:
2961 case CONVERT_EXPR:
2962 case FLOAT_EXPR:
2963 case FIX_TRUNC_EXPR:
2964 case FIX_FLOOR_EXPR:
2965 case FIX_ROUND_EXPR:
2966 case FIX_CEIL_EXPR:
2967 pedantic_lvalue_warning (CONVERT_EXPR);
2968 /* If the real type has the same machine representation
2969 as the type it is cast to, we can make better output
2970 by adding directly to the inside of the cast. */
2971 if ((TREE_CODE (TREE_TYPE (arg))
2972 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2973 && (TYPE_MODE (TREE_TYPE (arg))
2974 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2975 arg = TREE_OPERAND (arg, 0);
2976 else
2978 tree incremented, modify, value;
2979 arg = stabilize_reference (arg);
2980 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2981 value = arg;
2982 else
2983 value = save_expr (arg);
2984 incremented = build (((code == PREINCREMENT_EXPR
2985 || code == POSTINCREMENT_EXPR)
2986 ? PLUS_EXPR : MINUS_EXPR),
2987 argtype, value, inc);
2988 TREE_SIDE_EFFECTS (incremented) = 1;
2989 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2990 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2991 TREE_USED (value) = 1;
2992 return value;
2994 break;
2996 default:
2997 goto give_up;
2999 give_up:
3001 /* Complain about anything else that is not a true lvalue. */
3002 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3003 || code == POSTINCREMENT_EXPR)
3004 ? "increment" : "decrement")))
3005 return error_mark_node;
3007 /* Report a read-only lvalue. */
3008 if (TREE_READONLY (arg))
3009 readonly_warning (arg,
3010 ((code == PREINCREMENT_EXPR
3011 || code == POSTINCREMENT_EXPR)
3012 ? "increment" : "decrement"));
3014 val = build (code, TREE_TYPE (arg), arg, inc);
3015 TREE_SIDE_EFFECTS (val) = 1;
3016 val = convert (result_type, val);
3017 if (TREE_CODE (val) != code)
3018 TREE_NO_UNUSED_WARNING (val) = 1;
3019 return val;
3022 case ADDR_EXPR:
3023 /* Note that this operation never does default_conversion
3024 regardless of NOCONVERT. */
3026 /* Let &* cancel out to simplify resulting code. */
3027 if (TREE_CODE (arg) == INDIRECT_REF)
3029 /* Don't let this be an lvalue. */
3030 if (lvalue_p (TREE_OPERAND (arg, 0)))
3031 return non_lvalue (TREE_OPERAND (arg, 0));
3032 return TREE_OPERAND (arg, 0);
3035 /* For &x[y], return x+y */
3036 if (TREE_CODE (arg) == ARRAY_REF)
3038 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
3039 return error_mark_node;
3040 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3041 TREE_OPERAND (arg, 1), 1);
3044 /* Handle complex lvalues (when permitted)
3045 by reduction to simpler cases. */
3046 val = unary_complex_lvalue (code, arg);
3047 if (val != 0)
3048 return val;
3050 #if 0 /* Turned off because inconsistent;
3051 float f; *&(int)f = 3.4 stores in int format
3052 whereas (int)f = 3.4 stores in float format. */
3053 /* Address of a cast is just a cast of the address
3054 of the operand of the cast. */
3055 switch (TREE_CODE (arg))
3057 case NOP_EXPR:
3058 case CONVERT_EXPR:
3059 case FLOAT_EXPR:
3060 case FIX_TRUNC_EXPR:
3061 case FIX_FLOOR_EXPR:
3062 case FIX_ROUND_EXPR:
3063 case FIX_CEIL_EXPR:
3064 if (pedantic)
3065 pedwarn ("ANSI C forbids the address of a cast expression");
3066 return convert (build_pointer_type (TREE_TYPE (arg)),
3067 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3068 0));
3070 #endif
3072 /* Allow the address of a constructor if all the elements
3073 are constant. */
3074 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3076 /* Anything not already handled and not a true memory reference
3077 is an error. */
3078 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
3079 return error_mark_node;
3081 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3082 argtype = TREE_TYPE (arg);
3083 /* If the lvalue is const or volatile,
3084 merge that into the type that the address will point to. */
3085 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
3086 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3088 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3089 argtype = c_build_type_variant (argtype,
3090 TREE_READONLY (arg),
3091 TREE_THIS_VOLATILE (arg));
3094 argtype = build_pointer_type (argtype);
3096 if (mark_addressable (arg) == 0)
3097 return error_mark_node;
3100 tree addr;
3102 if (TREE_CODE (arg) == COMPONENT_REF)
3104 tree field = TREE_OPERAND (arg, 1);
3106 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3108 if (DECL_C_BIT_FIELD (field))
3110 error ("attempt to take address of bit-field structure member `%s'",
3111 IDENTIFIER_POINTER (DECL_NAME (field)));
3112 return error_mark_node;
3115 addr = convert (argtype, addr);
3117 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3119 tree offset
3120 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3121 size_int (BITS_PER_UNIT));
3122 int flag = TREE_CONSTANT (addr);
3123 addr = fold (build (PLUS_EXPR, argtype,
3124 addr, convert (argtype, offset)));
3125 TREE_CONSTANT (addr) = flag;
3128 else
3129 addr = build1 (code, argtype, arg);
3131 /* Address of a static or external variable or
3132 file-scope function counts as a constant. */
3133 if (staticp (arg)
3134 && ! (TREE_CODE (arg) == FUNCTION_DECL
3135 && DECL_CONTEXT (arg) != 0))
3136 TREE_CONSTANT (addr) = 1;
3137 return addr;
3140 default:
3141 break;
3144 if (!errstring)
3146 if (argtype == 0)
3147 argtype = TREE_TYPE (arg);
3148 return fold (build1 (code, argtype, arg));
3151 error (errstring);
3152 return error_mark_node;
3155 #if 0
3156 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3157 convert ARG with the same conversions in the same order
3158 and return the result. */
3160 static tree
3161 convert_sequence (conversions, arg)
3162 tree conversions;
3163 tree arg;
3165 switch (TREE_CODE (conversions))
3167 case NOP_EXPR:
3168 case CONVERT_EXPR:
3169 case FLOAT_EXPR:
3170 case FIX_TRUNC_EXPR:
3171 case FIX_FLOOR_EXPR:
3172 case FIX_ROUND_EXPR:
3173 case FIX_CEIL_EXPR:
3174 return convert (TREE_TYPE (conversions),
3175 convert_sequence (TREE_OPERAND (conversions, 0),
3176 arg));
3178 default:
3179 return arg;
3182 #endif /* 0 */
3184 /* Return nonzero if REF is an lvalue valid for this language.
3185 Lvalues can be assigned, unless their type has TYPE_READONLY.
3186 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3189 lvalue_p (ref)
3190 tree ref;
3192 register enum tree_code code = TREE_CODE (ref);
3194 switch (code)
3196 case REALPART_EXPR:
3197 case IMAGPART_EXPR:
3198 case COMPONENT_REF:
3199 return lvalue_p (TREE_OPERAND (ref, 0));
3201 case STRING_CST:
3202 return 1;
3204 case INDIRECT_REF:
3205 case ARRAY_REF:
3206 case VAR_DECL:
3207 case PARM_DECL:
3208 case RESULT_DECL:
3209 case ERROR_MARK:
3210 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3211 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3213 case BIND_EXPR:
3214 case RTL_EXPR:
3215 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3217 default:
3218 return 0;
3222 /* Return nonzero if REF is an lvalue valid for this language;
3223 otherwise, print an error message and return zero. */
3226 lvalue_or_else (ref, string)
3227 tree ref;
3228 char *string;
3230 int win = lvalue_p (ref);
3231 if (! win)
3232 error ("invalid lvalue in %s", string);
3233 return win;
3236 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3237 for certain kinds of expressions which are not really lvalues
3238 but which we can accept as lvalues.
3240 If ARG is not a kind of expression we can handle, return zero. */
3242 static tree
3243 unary_complex_lvalue (code, arg)
3244 enum tree_code code;
3245 tree arg;
3247 /* Handle (a, b) used as an "lvalue". */
3248 if (TREE_CODE (arg) == COMPOUND_EXPR)
3250 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3252 /* If this returns a function type, it isn't really being used as
3253 an lvalue, so don't issue a warning about it. */
3254 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3255 pedantic_lvalue_warning (COMPOUND_EXPR);
3257 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3258 TREE_OPERAND (arg, 0), real_result);
3261 /* Handle (a ? b : c) used as an "lvalue". */
3262 if (TREE_CODE (arg) == COND_EXPR)
3264 pedantic_lvalue_warning (COND_EXPR);
3265 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3266 pedantic_lvalue_warning (COMPOUND_EXPR);
3268 return (build_conditional_expr
3269 (TREE_OPERAND (arg, 0),
3270 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3271 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3274 return 0;
3277 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3278 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3280 static void
3281 pedantic_lvalue_warning (code)
3282 enum tree_code code;
3284 if (pedantic)
3285 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3286 code == COND_EXPR ? "conditional"
3287 : code == COMPOUND_EXPR ? "compound" : "cast");
3290 /* Warn about storing in something that is `const'. */
3292 void
3293 readonly_warning (arg, string)
3294 tree arg;
3295 char *string;
3297 char buf[80];
3298 strcpy (buf, string);
3300 /* Forbid assignments to iterators. */
3301 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3303 strcat (buf, " of iterator `%s'");
3304 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3307 if (TREE_CODE (arg) == COMPONENT_REF)
3309 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3310 readonly_warning (TREE_OPERAND (arg, 0), string);
3311 else
3313 strcat (buf, " of read-only member `%s'");
3314 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3317 else if (TREE_CODE (arg) == VAR_DECL)
3319 strcat (buf, " of read-only variable `%s'");
3320 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3322 else
3324 pedwarn ("%s of read-only location", buf);
3328 /* Mark EXP saying that we need to be able to take the
3329 address of it; it should not be allocated in a register.
3330 Value is 1 if successful. */
3333 mark_addressable (exp)
3334 tree exp;
3336 register tree x = exp;
3337 while (1)
3338 switch (TREE_CODE (x))
3340 case COMPONENT_REF:
3341 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3343 error ("cannot take address of bitfield `%s'",
3344 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3345 return 0;
3348 /* ... fall through ... */
3350 case ADDR_EXPR:
3351 case ARRAY_REF:
3352 case REALPART_EXPR:
3353 case IMAGPART_EXPR:
3354 x = TREE_OPERAND (x, 0);
3355 break;
3357 case CONSTRUCTOR:
3358 TREE_ADDRESSABLE (x) = 1;
3359 return 1;
3361 case VAR_DECL:
3362 case CONST_DECL:
3363 case PARM_DECL:
3364 case RESULT_DECL:
3365 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3366 && DECL_NONLOCAL (x))
3368 if (TREE_PUBLIC (x))
3370 error ("global register variable `%s' used in nested function",
3371 IDENTIFIER_POINTER (DECL_NAME (x)));
3372 return 0;
3374 pedwarn ("register variable `%s' used in nested function",
3375 IDENTIFIER_POINTER (DECL_NAME (x)));
3377 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3379 if (TREE_PUBLIC (x))
3381 error ("address of global register variable `%s' requested",
3382 IDENTIFIER_POINTER (DECL_NAME (x)));
3383 return 0;
3386 /* If we are making this addressable due to its having
3387 volatile components, give a different error message. Also
3388 handle the case of an unnamed parameter by not trying
3389 to give the name. */
3391 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3393 error ("cannot put object with volatile field into register");
3394 return 0;
3397 pedwarn ("address of register variable `%s' requested",
3398 IDENTIFIER_POINTER (DECL_NAME (x)));
3400 put_var_into_stack (x);
3402 /* drops in */
3403 case FUNCTION_DECL:
3404 TREE_ADDRESSABLE (x) = 1;
3405 #if 0 /* poplevel deals with this now. */
3406 if (DECL_CONTEXT (x) == 0)
3407 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3408 #endif
3410 default:
3411 return 1;
3415 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3417 tree
3418 build_conditional_expr (ifexp, op1, op2)
3419 tree ifexp, op1, op2;
3421 register tree type1;
3422 register tree type2;
3423 register enum tree_code code1;
3424 register enum tree_code code2;
3425 register tree result_type = NULL;
3426 tree orig_op1 = op1, orig_op2 = op2;
3428 ifexp = truthvalue_conversion (default_conversion (ifexp));
3430 #if 0 /* Produces wrong result if within sizeof. */
3431 /* Don't promote the operands separately if they promote
3432 the same way. Return the unpromoted type and let the combined
3433 value get promoted if necessary. */
3435 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3436 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3437 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3438 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3440 if (TREE_CODE (ifexp) == INTEGER_CST)
3441 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3443 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3445 #endif
3447 /* Promote both alternatives. */
3449 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3450 op1 = default_conversion (op1);
3451 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3452 op2 = default_conversion (op2);
3454 if (TREE_CODE (ifexp) == ERROR_MARK
3455 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3456 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3457 return error_mark_node;
3459 type1 = TREE_TYPE (op1);
3460 code1 = TREE_CODE (type1);
3461 type2 = TREE_TYPE (op2);
3462 code2 = TREE_CODE (type2);
3464 /* Quickly detect the usual case where op1 and op2 have the same type
3465 after promotion. */
3466 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3468 if (type1 == type2)
3469 result_type = type1;
3470 else
3471 result_type = TYPE_MAIN_VARIANT (type1);
3473 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3474 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3476 result_type = common_type (type1, type2);
3478 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3480 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3481 pedwarn ("ANSI C forbids conditional expr with only one void side");
3482 result_type = void_type_node;
3484 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3486 if (comp_target_types (type1, type2))
3487 result_type = common_type (type1, type2);
3488 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3489 && TREE_CODE (orig_op1) != NOP_EXPR)
3490 result_type = qualify_type (type2, type1);
3491 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3492 && TREE_CODE (orig_op2) != NOP_EXPR)
3493 result_type = qualify_type (type1, type2);
3494 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3496 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3497 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3498 result_type = qualify_type (type1, type2);
3500 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3502 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3503 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3504 result_type = qualify_type (type2, type1);
3506 else
3508 pedwarn ("pointer type mismatch in conditional expression");
3509 result_type = build_pointer_type (void_type_node);
3512 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3514 if (! integer_zerop (op2))
3515 pedwarn ("pointer/integer type mismatch in conditional expression");
3516 else
3518 op2 = null_pointer_node;
3519 #if 0 /* The spec seems to say this is permitted. */
3520 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3521 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3522 #endif
3524 result_type = type1;
3526 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3528 if (!integer_zerop (op1))
3529 pedwarn ("pointer/integer type mismatch in conditional expression");
3530 else
3532 op1 = null_pointer_node;
3533 #if 0 /* The spec seems to say this is permitted. */
3534 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3535 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3536 #endif
3538 result_type = type2;
3541 if (!result_type)
3543 if (flag_cond_mismatch)
3544 result_type = void_type_node;
3545 else
3547 error ("type mismatch in conditional expression");
3548 return error_mark_node;
3552 /* Merge const and volatile flags of the incoming types. */
3553 result_type
3554 = build_type_variant (result_type,
3555 TREE_READONLY (op1) || TREE_READONLY (op2),
3556 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3558 if (result_type != TREE_TYPE (op1))
3559 op1 = convert_and_check (result_type, op1);
3560 if (result_type != TREE_TYPE (op2))
3561 op2 = convert_and_check (result_type, op2);
3563 #if 0
3564 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3566 result_type = TREE_TYPE (op1);
3567 if (TREE_CONSTANT (ifexp))
3568 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3570 if (TYPE_MODE (result_type) == BLKmode)
3572 register tree tempvar
3573 = build_decl (VAR_DECL, NULL_TREE, result_type);
3574 register tree xop1 = build_modify_expr (tempvar, op1);
3575 register tree xop2 = build_modify_expr (tempvar, op2);
3576 register tree result = fold (build (COND_EXPR, result_type,
3577 ifexp, xop1, xop2));
3579 layout_decl (tempvar, TYPE_ALIGN (result_type));
3580 /* No way to handle variable-sized objects here.
3581 I fear that the entire handling of BLKmode conditional exprs
3582 needs to be redone. */
3583 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3584 abort ();
3585 DECL_RTL (tempvar)
3586 = assign_stack_local (DECL_MODE (tempvar),
3587 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3588 + BITS_PER_UNIT - 1)
3589 / BITS_PER_UNIT,
3592 TREE_SIDE_EFFECTS (result)
3593 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3594 | TREE_SIDE_EFFECTS (op2);
3595 return build (COMPOUND_EXPR, result_type, result, tempvar);
3598 #endif /* 0 */
3600 if (TREE_CODE (ifexp) == INTEGER_CST)
3601 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3603 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3606 /* Given a list of expressions, return a compound expression
3607 that performs them all and returns the value of the last of them. */
3609 tree
3610 build_compound_expr (list)
3611 tree list;
3613 return internal_build_compound_expr (list, TRUE);
3616 static tree
3617 internal_build_compound_expr (list, first_p)
3618 tree list;
3619 int first_p;
3621 register tree rest;
3623 if (TREE_CHAIN (list) == 0)
3625 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3626 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3628 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3629 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3630 list = TREE_OPERAND (list, 0);
3631 #endif
3633 /* Don't let (0, 0) be null pointer constant. */
3634 if (!first_p && integer_zerop (TREE_VALUE (list)))
3635 return non_lvalue (TREE_VALUE (list));
3636 return TREE_VALUE (list);
3639 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3641 /* Convert arrays to pointers when there really is a comma operator. */
3642 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3643 TREE_VALUE (TREE_CHAIN (list))
3644 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3647 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3649 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3651 /* The left-hand operand of a comma expression is like an expression
3652 statement: with -W or -Wunused, we should warn if it doesn't have
3653 any side-effects, unless it was explicitly cast to (void). */
3654 if ((extra_warnings || warn_unused)
3655 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3656 && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
3657 warning ("left-hand operand of comma expression has no effect");
3659 /* When pedantic, a compound expression can be neither an lvalue
3660 nor an integer constant expression. */
3661 if (! pedantic)
3662 return rest;
3665 /* With -Wunused, we should also warn if the left-hand operand does have
3666 side-effects, but computes a value which is not used. For example, in
3667 `foo() + bar(), baz()' the result of the `+' operator is not used,
3668 so we should issue a warning. */
3669 else if (warn_unused)
3670 warn_if_unused_value (TREE_VALUE (list));
3672 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3675 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3677 tree
3678 build_c_cast (type, expr)
3679 register tree type;
3680 tree expr;
3682 register tree value = expr;
3684 if (type == error_mark_node || expr == error_mark_node)
3685 return error_mark_node;
3686 type = TYPE_MAIN_VARIANT (type);
3688 #if 0
3689 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3690 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3691 value = TREE_OPERAND (value, 0);
3692 #endif
3694 if (TREE_CODE (type) == ARRAY_TYPE)
3696 error ("cast specifies array type");
3697 return error_mark_node;
3700 if (TREE_CODE (type) == FUNCTION_TYPE)
3702 error ("cast specifies function type");
3703 return error_mark_node;
3706 if (type == TREE_TYPE (value))
3708 if (pedantic)
3710 if (TREE_CODE (type) == RECORD_TYPE
3711 || TREE_CODE (type) == UNION_TYPE)
3712 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3715 else if (TREE_CODE (type) == UNION_TYPE)
3717 tree field;
3718 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3719 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3720 value = default_conversion (value);
3722 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3723 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3724 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3725 break;
3727 if (field)
3729 char *name;
3730 tree t;
3732 if (pedantic)
3733 pedwarn ("ANSI C forbids casts to union type");
3734 if (TYPE_NAME (type) != 0)
3736 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3737 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3738 else
3739 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3741 else
3742 name = "";
3743 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3744 build_tree_list (field, value)),
3745 0, 0);
3746 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3747 return t;
3749 error ("cast to union type from type not present in union");
3750 return error_mark_node;
3752 else
3754 tree otype, ovalue;
3756 /* If casting to void, avoid the error that would come
3757 from default_conversion in the case of a non-lvalue array. */
3758 if (type == void_type_node)
3759 return build1 (CONVERT_EXPR, type, value);
3761 /* Convert functions and arrays to pointers,
3762 but don't convert any other types. */
3763 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3764 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3765 value = default_conversion (value);
3766 otype = TREE_TYPE (value);
3768 /* Optionally warn about potentially worrisome casts. */
3770 if (warn_cast_qual
3771 && TREE_CODE (type) == POINTER_TYPE
3772 && TREE_CODE (otype) == POINTER_TYPE)
3774 /* Go to the innermost object being pointed to. */
3775 tree in_type = type;
3776 tree in_otype = otype;
3778 while (TREE_CODE (in_type) == POINTER_TYPE)
3779 in_type = TREE_TYPE (in_type);
3780 while (TREE_CODE (in_otype) == POINTER_TYPE)
3781 in_otype = TREE_TYPE (in_otype);
3783 if (TYPE_VOLATILE (in_otype) && ! TYPE_VOLATILE (in_type))
3784 pedwarn ("cast discards `volatile' from pointer target type");
3785 if (TYPE_READONLY (in_otype) && ! TYPE_READONLY (in_type))
3786 pedwarn ("cast discards `const' from pointer target type");
3789 /* Warn about possible alignment problems. */
3790 if (STRICT_ALIGNMENT && warn_cast_align
3791 && TREE_CODE (type) == POINTER_TYPE
3792 && TREE_CODE (otype) == POINTER_TYPE
3793 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3794 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3795 /* Don't warn about opaque types, where the actual alignment
3796 restriction is unknown. */
3797 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3798 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3799 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3800 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3801 warning ("cast increases required alignment of target type");
3803 if (TREE_CODE (type) == INTEGER_TYPE
3804 && TREE_CODE (otype) == POINTER_TYPE
3805 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3806 && !TREE_CONSTANT (value))
3807 warning ("cast from pointer to integer of different size");
3809 if (warn_bad_function_cast
3810 && TREE_CODE (value) == CALL_EXPR
3811 && TREE_CODE (type) != TREE_CODE (otype))
3812 warning ("cast does not match function type");
3814 if (TREE_CODE (type) == POINTER_TYPE
3815 && TREE_CODE (otype) == INTEGER_TYPE
3816 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3817 #if 0
3818 /* Don't warn about converting 0 to pointer,
3819 provided the 0 was explicit--not cast or made by folding. */
3820 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3821 #endif
3822 /* Don't warn about converting any constant. */
3823 && !TREE_CONSTANT (value))
3824 warning ("cast to pointer from integer of different size");
3826 ovalue = value;
3827 value = convert (type, value);
3829 /* Ignore any integer overflow caused by the cast. */
3830 if (TREE_CODE (value) == INTEGER_CST)
3832 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3833 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3837 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3838 if (pedantic && TREE_CODE (value) == INTEGER_CST
3839 && TREE_CODE (expr) == INTEGER_CST
3840 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3841 value = non_lvalue (value);
3843 /* If pedantic, don't let a cast be an lvalue. */
3844 if (value == expr && pedantic)
3845 value = non_lvalue (value);
3847 return value;
3850 /* Build an assignment expression of lvalue LHS from value RHS.
3851 MODIFYCODE is the code for a binary operator that we use
3852 to combine the old value of LHS with RHS to get the new value.
3853 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3855 tree
3856 build_modify_expr (lhs, modifycode, rhs)
3857 tree lhs, rhs;
3858 enum tree_code modifycode;
3860 register tree result;
3861 tree newrhs;
3862 tree lhstype = TREE_TYPE (lhs);
3863 tree olhstype = lhstype;
3865 /* Types that aren't fully specified cannot be used in assignments. */
3866 lhs = require_complete_type (lhs);
3868 /* Avoid duplicate error messages from operands that had errors. */
3869 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3870 return error_mark_node;
3872 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3873 /* Do not use STRIP_NOPS here. We do not want an enumerator
3874 whose value is 0 to count as a null pointer constant. */
3875 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3876 rhs = TREE_OPERAND (rhs, 0);
3878 newrhs = rhs;
3880 /* Handle control structure constructs used as "lvalues". */
3882 switch (TREE_CODE (lhs))
3884 /* Handle (a, b) used as an "lvalue". */
3885 case COMPOUND_EXPR:
3886 pedantic_lvalue_warning (COMPOUND_EXPR);
3887 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3888 modifycode, rhs);
3889 if (TREE_CODE (newrhs) == ERROR_MARK)
3890 return error_mark_node;
3891 return build (COMPOUND_EXPR, lhstype,
3892 TREE_OPERAND (lhs, 0), newrhs);
3894 /* Handle (a ? b : c) used as an "lvalue". */
3895 case COND_EXPR:
3896 pedantic_lvalue_warning (COND_EXPR);
3897 rhs = save_expr (rhs);
3899 /* Produce (a ? (b = rhs) : (c = rhs))
3900 except that the RHS goes through a save-expr
3901 so the code to compute it is only emitted once. */
3902 tree cond
3903 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3904 build_modify_expr (TREE_OPERAND (lhs, 1),
3905 modifycode, rhs),
3906 build_modify_expr (TREE_OPERAND (lhs, 2),
3907 modifycode, rhs));
3908 if (TREE_CODE (cond) == ERROR_MARK)
3909 return cond;
3910 /* Make sure the code to compute the rhs comes out
3911 before the split. */
3912 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3913 /* But cast it to void to avoid an "unused" error. */
3914 convert (void_type_node, rhs), cond);
3916 default:
3917 break;
3920 /* If a binary op has been requested, combine the old LHS value with the RHS
3921 producing the value we should actually store into the LHS. */
3923 if (modifycode != NOP_EXPR)
3925 lhs = stabilize_reference (lhs);
3926 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3929 /* Handle a cast used as an "lvalue".
3930 We have already performed any binary operator using the value as cast.
3931 Now convert the result to the cast type of the lhs,
3932 and then true type of the lhs and store it there;
3933 then convert result back to the cast type to be the value
3934 of the assignment. */
3936 switch (TREE_CODE (lhs))
3938 case NOP_EXPR:
3939 case CONVERT_EXPR:
3940 case FLOAT_EXPR:
3941 case FIX_TRUNC_EXPR:
3942 case FIX_FLOOR_EXPR:
3943 case FIX_ROUND_EXPR:
3944 case FIX_CEIL_EXPR:
3945 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3946 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3947 newrhs = default_conversion (newrhs);
3949 tree inner_lhs = TREE_OPERAND (lhs, 0);
3950 tree result;
3951 result = build_modify_expr (inner_lhs, NOP_EXPR,
3952 convert (TREE_TYPE (inner_lhs),
3953 convert (lhstype, newrhs)));
3954 if (TREE_CODE (result) == ERROR_MARK)
3955 return result;
3956 pedantic_lvalue_warning (CONVERT_EXPR);
3957 return convert (TREE_TYPE (lhs), result);
3960 default:
3961 break;
3964 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3965 Reject anything strange now. */
3967 if (!lvalue_or_else (lhs, "assignment"))
3968 return error_mark_node;
3970 /* Warn about storing in something that is `const'. */
3972 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3973 || ((TREE_CODE (lhstype) == RECORD_TYPE
3974 || TREE_CODE (lhstype) == UNION_TYPE)
3975 && C_TYPE_FIELDS_READONLY (lhstype)))
3976 readonly_warning (lhs, "assignment");
3978 /* If storing into a structure or union member,
3979 it has probably been given type `int'.
3980 Compute the type that would go with
3981 the actual amount of storage the member occupies. */
3983 if (TREE_CODE (lhs) == COMPONENT_REF
3984 && (TREE_CODE (lhstype) == INTEGER_TYPE
3985 || TREE_CODE (lhstype) == REAL_TYPE
3986 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3987 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3989 /* If storing in a field that is in actuality a short or narrower than one,
3990 we must store in the field in its actual type. */
3992 if (lhstype != TREE_TYPE (lhs))
3994 lhs = copy_node (lhs);
3995 TREE_TYPE (lhs) = lhstype;
3998 /* Convert new value to destination type. */
4000 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
4001 NULL_TREE, NULL_TREE, 0);
4002 if (TREE_CODE (newrhs) == ERROR_MARK)
4003 return error_mark_node;
4005 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4006 TREE_SIDE_EFFECTS (result) = 1;
4008 /* If we got the LHS in a different type for storing in,
4009 convert the result back to the nominal type of LHS
4010 so that the value we return always has the same type
4011 as the LHS argument. */
4013 if (olhstype == TREE_TYPE (result))
4014 return result;
4015 return convert_for_assignment (olhstype, result, "assignment",
4016 NULL_TREE, NULL_TREE, 0);
4019 /* Convert value RHS to type TYPE as preparation for an assignment
4020 to an lvalue of type TYPE.
4021 The real work of conversion is done by `convert'.
4022 The purpose of this function is to generate error messages
4023 for assignments that are not allowed in C.
4024 ERRTYPE is a string to use in error messages:
4025 "assignment", "return", etc. If it is null, this is parameter passing
4026 for a function call (and different error messages are output). Otherwise,
4027 it may be a name stored in the spelling stack and interpreted by
4028 get_spelling.
4030 FUNNAME is the name of the function being called,
4031 as an IDENTIFIER_NODE, or null.
4032 PARMNUM is the number of the argument, for printing in error messages. */
4034 static tree
4035 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
4036 tree type, rhs;
4037 char *errtype;
4038 tree fundecl, funname;
4039 int parmnum;
4041 register enum tree_code codel = TREE_CODE (type);
4042 register tree rhstype;
4043 register enum tree_code coder;
4045 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4046 /* Do not use STRIP_NOPS here. We do not want an enumerator
4047 whose value is 0 to count as a null pointer constant. */
4048 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4049 rhs = TREE_OPERAND (rhs, 0);
4051 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4052 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4053 rhs = default_conversion (rhs);
4054 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4055 rhs = decl_constant_value (rhs);
4057 rhstype = TREE_TYPE (rhs);
4058 coder = TREE_CODE (rhstype);
4060 if (coder == ERROR_MARK)
4061 return error_mark_node;
4063 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4065 overflow_warning (rhs);
4066 /* Check for Objective-C protocols. This will issue a warning if
4067 there are protocol violations. No need to use the return value. */
4068 maybe_objc_comptypes (type, rhstype, 0);
4069 return rhs;
4072 if (coder == VOID_TYPE)
4074 error ("void value not ignored as it ought to be");
4075 return error_mark_node;
4077 /* Arithmetic types all interconvert, and enum is treated like int. */
4078 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
4079 || codel == COMPLEX_TYPE)
4080 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
4081 || coder == COMPLEX_TYPE))
4082 return convert_and_check (type, rhs);
4084 /* Conversion to a transparent union from its member types.
4085 This applies only to function arguments. */
4086 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4088 tree memb_types;
4089 tree marginal_memb_type = 0;
4091 for (memb_types = TYPE_FIELDS (type); memb_types;
4092 memb_types = TREE_CHAIN (memb_types))
4094 tree memb_type = TREE_TYPE (memb_types);
4096 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4097 TYPE_MAIN_VARIANT (rhstype)))
4098 break;
4100 if (TREE_CODE (memb_type) != POINTER_TYPE)
4101 continue;
4103 if (coder == POINTER_TYPE)
4105 register tree ttl = TREE_TYPE (memb_type);
4106 register tree ttr = TREE_TYPE (rhstype);
4108 /* Any non-function converts to a [const][volatile] void *
4109 and vice versa; otherwise, targets must be the same.
4110 Meanwhile, the lhs target must have all the qualifiers of
4111 the rhs. */
4112 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4113 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4114 || comp_target_types (memb_type, rhstype))
4116 /* If this type won't generate any warnings, use it. */
4117 if ((TREE_CODE (ttr) == FUNCTION_TYPE
4118 && TREE_CODE (ttl) == FUNCTION_TYPE)
4119 ? ((! TYPE_READONLY (ttl) | TYPE_READONLY (ttr))
4120 & (! TYPE_VOLATILE (ttl) | TYPE_VOLATILE (ttr)))
4121 : ((TYPE_READONLY (ttl) | ! TYPE_READONLY (ttr))
4122 & (TYPE_VOLATILE (ttl) | ! TYPE_VOLATILE (ttr))))
4123 break;
4125 /* Keep looking for a better type, but remember this one. */
4126 if (! marginal_memb_type)
4127 marginal_memb_type = memb_type;
4131 /* Can convert integer zero to any pointer type. */
4132 if (integer_zerop (rhs)
4133 || (TREE_CODE (rhs) == NOP_EXPR
4134 && integer_zerop (TREE_OPERAND (rhs, 0))))
4136 rhs = null_pointer_node;
4137 break;
4141 if (memb_types || marginal_memb_type)
4143 if (! memb_types)
4145 /* We have only a marginally acceptable member type;
4146 it needs a warning. */
4147 register tree ttl = TREE_TYPE (marginal_memb_type);
4148 register tree ttr = TREE_TYPE (rhstype);
4150 /* Const and volatile mean something different for function
4151 types, so the usual warnings are not appropriate. */
4152 if (TREE_CODE (ttr) == FUNCTION_TYPE
4153 && TREE_CODE (ttl) == FUNCTION_TYPE)
4155 /* Because const and volatile on functions are
4156 restrictions that say the function will not do
4157 certain things, it is okay to use a const or volatile
4158 function where an ordinary one is wanted, but not
4159 vice-versa. */
4160 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4161 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4162 get_spelling (errtype), funname,
4163 parmnum);
4164 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4165 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4166 get_spelling (errtype), funname,
4167 parmnum);
4169 else
4171 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4172 warn_for_assignment ("%s discards `const' from pointer target type",
4173 get_spelling (errtype), funname,
4174 parmnum);
4175 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4176 warn_for_assignment ("%s discards `volatile' from pointer target type",
4177 get_spelling (errtype), funname,
4178 parmnum);
4182 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4183 pedwarn ("ANSI C prohibits argument conversion to union type");
4185 return build1 (NOP_EXPR, type, rhs);
4189 /* Conversions among pointers */
4190 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
4192 register tree ttl = TREE_TYPE (type);
4193 register tree ttr = TREE_TYPE (rhstype);
4195 /* Any non-function converts to a [const][volatile] void *
4196 and vice versa; otherwise, targets must be the same.
4197 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4198 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4199 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4200 || comp_target_types (type, rhstype)
4201 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4202 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4204 if (pedantic
4205 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4206 && TREE_CODE (ttr) == FUNCTION_TYPE)
4208 (TYPE_MAIN_VARIANT (ttr) == void_type_node
4209 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4210 which are not ANSI null ptr constants. */
4211 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4212 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4213 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4214 get_spelling (errtype), funname, parmnum);
4215 /* Const and volatile mean something different for function types,
4216 so the usual warnings are not appropriate. */
4217 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4218 && TREE_CODE (ttl) != FUNCTION_TYPE)
4220 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4221 warn_for_assignment ("%s discards `const' from pointer target type",
4222 get_spelling (errtype), funname, parmnum);
4223 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4224 warn_for_assignment ("%s discards `volatile' from pointer target type",
4225 get_spelling (errtype), funname, parmnum);
4226 /* If this is not a case of ignoring a mismatch in signedness,
4227 no warning. */
4228 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4229 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4230 || comp_target_types (type, rhstype))
4232 /* If there is a mismatch, do warn. */
4233 else if (pedantic)
4234 warn_for_assignment ("pointer targets in %s differ in signedness",
4235 get_spelling (errtype), funname, parmnum);
4237 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4238 && TREE_CODE (ttr) == FUNCTION_TYPE)
4240 /* Because const and volatile on functions are restrictions
4241 that say the function will not do certain things,
4242 it is okay to use a const or volatile function
4243 where an ordinary one is wanted, but not vice-versa. */
4244 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4245 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4246 get_spelling (errtype), funname, parmnum);
4247 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4248 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4249 get_spelling (errtype), funname, parmnum);
4252 else
4253 warn_for_assignment ("%s from incompatible pointer type",
4254 get_spelling (errtype), funname, parmnum);
4255 return convert (type, rhs);
4257 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4259 /* An explicit constant 0 can convert to a pointer,
4260 or one that results from arithmetic, even including
4261 a cast to integer type. */
4262 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4264 ! (TREE_CODE (rhs) == NOP_EXPR
4265 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4266 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4267 && integer_zerop (TREE_OPERAND (rhs, 0))))
4269 warn_for_assignment ("%s makes pointer from integer without a cast",
4270 get_spelling (errtype), funname, parmnum);
4271 return convert (type, rhs);
4273 return null_pointer_node;
4275 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4277 warn_for_assignment ("%s makes integer from pointer without a cast",
4278 get_spelling (errtype), funname, parmnum);
4279 return convert (type, rhs);
4282 if (!errtype)
4284 if (funname)
4286 tree selector = maybe_building_objc_message_expr ();
4288 if (selector && parmnum > 2)
4289 error ("incompatible type for argument %d of `%s'",
4290 parmnum - 2, IDENTIFIER_POINTER (selector));
4291 else
4292 error ("incompatible type for argument %d of `%s'",
4293 parmnum, IDENTIFIER_POINTER (funname));
4295 else
4296 error ("incompatible type for argument %d of indirect function call",
4297 parmnum);
4299 else
4300 error ("incompatible types in %s", get_spelling (errtype));
4302 return error_mark_node;
4305 /* Print a warning using MSG.
4306 It gets OPNAME as its one parameter.
4307 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4308 FUNCTION and ARGNUM are handled specially if we are building an
4309 Objective-C selector. */
4311 static void
4312 warn_for_assignment (msg, opname, function, argnum)
4313 char *msg;
4314 char *opname;
4315 tree function;
4316 int argnum;
4318 static char argstring[] = "passing arg %d of `%s'";
4319 static char argnofun[] = "passing arg %d";
4321 if (opname == 0)
4323 tree selector = maybe_building_objc_message_expr ();
4325 if (selector && argnum > 2)
4327 function = selector;
4328 argnum -= 2;
4330 if (function)
4332 /* Function name is known; supply it. */
4333 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4334 + sizeof (argstring) + 25 /*%d*/ + 1);
4335 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4337 else
4339 /* Function name unknown (call through ptr); just give arg number. */
4340 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4341 sprintf (opname, argnofun, argnum);
4344 pedwarn (msg, opname);
4347 /* Return nonzero if VALUE is a valid constant-valued expression
4348 for use in initializing a static variable; one that can be an
4349 element of a "constant" initializer.
4351 Return null_pointer_node if the value is absolute;
4352 if it is relocatable, return the variable that determines the relocation.
4353 We assume that VALUE has been folded as much as possible;
4354 therefore, we do not need to check for such things as
4355 arithmetic-combinations of integers. */
4357 tree
4358 initializer_constant_valid_p (value, endtype)
4359 tree value;
4360 tree endtype;
4362 switch (TREE_CODE (value))
4364 case CONSTRUCTOR:
4365 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4366 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
4367 && TREE_CONSTANT (value)
4368 && CONSTRUCTOR_ELTS (value))
4369 return
4370 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
4371 endtype);
4373 return TREE_STATIC (value) ? null_pointer_node : 0;
4375 case INTEGER_CST:
4376 case REAL_CST:
4377 case STRING_CST:
4378 case COMPLEX_CST:
4379 return null_pointer_node;
4381 case ADDR_EXPR:
4382 return TREE_OPERAND (value, 0);
4384 case NON_LVALUE_EXPR:
4385 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4387 case CONVERT_EXPR:
4388 case NOP_EXPR:
4389 /* Allow conversions between pointer types. */
4390 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4391 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
4392 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4394 /* Allow conversions between real types. */
4395 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4396 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
4397 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4399 /* Allow length-preserving conversions between integer types. */
4400 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4401 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4402 && (TYPE_PRECISION (TREE_TYPE (value))
4403 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4404 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4406 /* Allow conversions between other integer types only if
4407 explicit value. */
4408 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4409 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4411 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4412 endtype);
4413 if (inner == null_pointer_node)
4414 return null_pointer_node;
4415 return 0;
4418 /* Allow (int) &foo provided int is as wide as a pointer. */
4419 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4420 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
4421 && (TYPE_PRECISION (TREE_TYPE (value))
4422 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4423 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4424 endtype);
4426 /* Likewise conversions from int to pointers, but also allow
4427 conversions from 0. */
4428 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4429 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4431 if (integer_zerop (TREE_OPERAND (value, 0)))
4432 return null_pointer_node;
4433 else if (TYPE_PRECISION (TREE_TYPE (value))
4434 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))
4435 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4436 endtype);
4439 /* Allow conversions to union types if the value inside is okay. */
4440 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4441 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4442 endtype);
4443 return 0;
4445 case PLUS_EXPR:
4446 if (TREE_CODE (endtype) == INTEGER_TYPE
4447 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4448 return 0;
4450 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4451 endtype);
4452 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4453 endtype);
4454 /* If either term is absolute, use the other terms relocation. */
4455 if (valid0 == null_pointer_node)
4456 return valid1;
4457 if (valid1 == null_pointer_node)
4458 return valid0;
4459 return 0;
4462 case MINUS_EXPR:
4463 if (TREE_CODE (endtype) == INTEGER_TYPE
4464 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4465 return 0;
4467 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4468 endtype);
4469 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4470 endtype);
4471 /* Win if second argument is absolute. */
4472 if (valid1 == null_pointer_node)
4473 return valid0;
4474 /* Win if both arguments have the same relocation.
4475 Then the value is absolute. */
4476 if (valid0 == valid1)
4477 return null_pointer_node;
4478 return 0;
4481 default:
4482 return 0;
4486 /* If VALUE is a compound expr all of whose expressions are constant, then
4487 return its value. Otherwise, return error_mark_node.
4489 This is for handling COMPOUND_EXPRs as initializer elements
4490 which is allowed with a warning when -pedantic is specified. */
4492 static tree
4493 valid_compound_expr_initializer (value, endtype)
4494 tree value;
4495 tree endtype;
4497 if (TREE_CODE (value) == COMPOUND_EXPR)
4499 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4500 == error_mark_node)
4501 return error_mark_node;
4502 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4503 endtype);
4505 else if (! TREE_CONSTANT (value)
4506 && ! initializer_constant_valid_p (value, endtype))
4507 return error_mark_node;
4508 else
4509 return value;
4512 /* Perform appropriate conversions on the initial value of a variable,
4513 store it in the declaration DECL,
4514 and print any error messages that are appropriate.
4515 If the init is invalid, store an ERROR_MARK. */
4517 void
4518 store_init_value (decl, init)
4519 tree decl, init;
4521 register tree value, type;
4523 /* If variable's type was invalidly declared, just ignore it. */
4525 type = TREE_TYPE (decl);
4526 if (TREE_CODE (type) == ERROR_MARK)
4527 return;
4529 /* Digest the specified initializer into an expression. */
4531 value = digest_init (type, init, TREE_STATIC (decl),
4532 TREE_STATIC (decl) || pedantic);
4534 /* Store the expression if valid; else report error. */
4536 #if 0
4537 /* Note that this is the only place we can detect the error
4538 in a case such as struct foo bar = (struct foo) { x, y };
4539 where there is one initial value which is a constructor expression. */
4540 if (value == error_mark_node)
4542 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4544 error ("initializer for static variable is not constant");
4545 value = error_mark_node;
4547 else if (TREE_STATIC (decl)
4548 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4550 error ("initializer for static variable uses complicated arithmetic");
4551 value = error_mark_node;
4553 else
4555 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4557 if (! TREE_CONSTANT (value))
4558 pedwarn ("aggregate initializer is not constant");
4559 else if (! TREE_STATIC (value))
4560 pedwarn ("aggregate initializer uses complicated arithmetic");
4563 #endif
4565 DECL_INITIAL (decl) = value;
4567 /* ANSI wants warnings about out-of-range constant initializers. */
4568 STRIP_TYPE_NOPS (value);
4569 constant_expression_warning (value);
4572 /* Methods for storing and printing names for error messages. */
4574 /* Implement a spelling stack that allows components of a name to be pushed
4575 and popped. Each element on the stack is this structure. */
4577 struct spelling
4579 int kind;
4580 union
4582 int i;
4583 char *s;
4584 } u;
4587 #define SPELLING_STRING 1
4588 #define SPELLING_MEMBER 2
4589 #define SPELLING_BOUNDS 3
4591 static struct spelling *spelling; /* Next stack element (unused). */
4592 static struct spelling *spelling_base; /* Spelling stack base. */
4593 static int spelling_size; /* Size of the spelling stack. */
4595 /* Macros to save and restore the spelling stack around push_... functions.
4596 Alternative to SAVE_SPELLING_STACK. */
4598 #define SPELLING_DEPTH() (spelling - spelling_base)
4599 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4601 /* Save and restore the spelling stack around arbitrary C code. */
4603 #define SAVE_SPELLING_DEPTH(code) \
4605 int __depth = SPELLING_DEPTH (); \
4606 code; \
4607 RESTORE_SPELLING_DEPTH (__depth); \
4610 /* Push an element on the spelling stack with type KIND and assign VALUE
4611 to MEMBER. */
4613 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4615 int depth = SPELLING_DEPTH (); \
4617 if (depth >= spelling_size) \
4619 spelling_size += 10; \
4620 if (spelling_base == 0) \
4621 spelling_base \
4622 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4623 else \
4624 spelling_base \
4625 = (struct spelling *) xrealloc (spelling_base, \
4626 spelling_size * sizeof (struct spelling)); \
4627 RESTORE_SPELLING_DEPTH (depth); \
4630 spelling->kind = (KIND); \
4631 spelling->MEMBER = (VALUE); \
4632 spelling++; \
4635 /* Push STRING on the stack. Printed literally. */
4637 static void
4638 push_string (string)
4639 char *string;
4641 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4644 /* Push a member name on the stack. Printed as '.' STRING. */
4646 static void
4647 push_member_name (decl)
4648 tree decl;
4651 char *string
4652 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4653 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4656 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4658 static void
4659 push_array_bounds (bounds)
4660 int bounds;
4662 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4665 /* Compute the maximum size in bytes of the printed spelling. */
4667 static int
4668 spelling_length ()
4670 register int size = 0;
4671 register struct spelling *p;
4673 for (p = spelling_base; p < spelling; p++)
4675 if (p->kind == SPELLING_BOUNDS)
4676 size += 25;
4677 else
4678 size += strlen (p->u.s) + 1;
4681 return size;
4684 /* Print the spelling to BUFFER and return it. */
4686 static char *
4687 print_spelling (buffer)
4688 register char *buffer;
4690 register char *d = buffer;
4691 register char *s;
4692 register struct spelling *p;
4694 for (p = spelling_base; p < spelling; p++)
4695 if (p->kind == SPELLING_BOUNDS)
4697 sprintf (d, "[%d]", p->u.i);
4698 d += strlen (d);
4700 else
4702 if (p->kind == SPELLING_MEMBER)
4703 *d++ = '.';
4704 for (s = p->u.s; (*d = *s++); d++)
4707 *d++ = '\0';
4708 return buffer;
4711 /* Provide a means to pass component names derived from the spelling stack. */
4713 char initialization_message;
4715 /* Interpret the spelling of the given ERRTYPE message. */
4717 static char *
4718 get_spelling (errtype)
4719 char *errtype;
4721 static char *buffer;
4722 static int size = -1;
4724 if (errtype == &initialization_message)
4726 /* Avoid counting chars */
4727 static char message[] = "initialization of `%s'";
4728 register int needed = sizeof (message) + spelling_length () + 1;
4729 char *temp;
4731 if (size < 0)
4732 buffer = (char *) xmalloc (size = needed);
4733 if (needed > size)
4734 buffer = (char *) xrealloc (buffer, size = needed);
4736 temp = (char *) alloca (needed);
4737 sprintf (buffer, message, print_spelling (temp));
4738 return buffer;
4741 return errtype;
4744 /* Issue an error message for a bad initializer component.
4745 FORMAT describes the message. OFWHAT is the name for the component.
4746 LOCAL is a format string for formatting the insertion of the name
4747 into the message.
4749 If OFWHAT is null, the component name is stored on the spelling stack.
4750 If the component name is a null string, then LOCAL is omitted entirely. */
4752 void
4753 error_init (format, local, ofwhat)
4754 char *format, *local, *ofwhat;
4756 char *buffer;
4758 if (ofwhat == 0)
4759 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4760 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4762 if (*ofwhat)
4763 sprintf (buffer, local, ofwhat);
4764 else
4765 buffer[0] = 0;
4767 error (format, buffer);
4770 /* Issue a pedantic warning for a bad initializer component.
4771 FORMAT describes the message. OFWHAT is the name for the component.
4772 LOCAL is a format string for formatting the insertion of the name
4773 into the message.
4775 If OFWHAT is null, the component name is stored on the spelling stack.
4776 If the component name is a null string, then LOCAL is omitted entirely. */
4778 void
4779 pedwarn_init (format, local, ofwhat)
4780 char *format, *local, *ofwhat;
4782 char *buffer;
4784 if (ofwhat == 0)
4785 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4786 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4788 if (*ofwhat)
4789 sprintf (buffer, local, ofwhat);
4790 else
4791 buffer[0] = 0;
4793 pedwarn (format, buffer);
4796 /* Issue a warning for a bad initializer component.
4797 FORMAT describes the message. OFWHAT is the name for the component.
4798 LOCAL is a format string for formatting the insertion of the name
4799 into the message.
4801 If OFWHAT is null, the component name is stored on the spelling stack.
4802 If the component name is a null string, then LOCAL is omitted entirely. */
4804 static void
4805 warning_init (format, local, ofwhat)
4806 char *format, *local, *ofwhat;
4808 char *buffer;
4810 if (ofwhat == 0)
4811 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4812 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4814 if (*ofwhat)
4815 sprintf (buffer, local, ofwhat);
4816 else
4817 buffer[0] = 0;
4819 warning (format, buffer);
4822 /* Digest the parser output INIT as an initializer for type TYPE.
4823 Return a C expression of type TYPE to represent the initial value.
4825 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4826 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4827 applies only to elements of constructors. */
4829 static tree
4830 digest_init (type, init, require_constant, constructor_constant)
4831 tree type, init;
4832 int require_constant, constructor_constant;
4834 enum tree_code code = TREE_CODE (type);
4835 tree inside_init = init;
4837 if (init == error_mark_node)
4838 return init;
4840 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4841 /* Do not use STRIP_NOPS here. We do not want an enumerator
4842 whose value is 0 to count as a null pointer constant. */
4843 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4844 inside_init = TREE_OPERAND (init, 0);
4846 /* Initialization of an array of chars from a string constant
4847 optionally enclosed in braces. */
4849 if (code == ARRAY_TYPE)
4851 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4852 if ((typ1 == char_type_node
4853 || typ1 == signed_char_type_node
4854 || typ1 == unsigned_char_type_node
4855 || typ1 == unsigned_wchar_type_node
4856 || typ1 == signed_wchar_type_node)
4857 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4859 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4860 TYPE_MAIN_VARIANT (type)))
4861 return inside_init;
4863 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4864 != char_type_node)
4865 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4867 error_init ("char-array%s initialized from wide string",
4868 " `%s'", NULL);
4869 return error_mark_node;
4871 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4872 == char_type_node)
4873 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4875 error_init ("int-array%s initialized from non-wide string",
4876 " `%s'", NULL);
4877 return error_mark_node;
4880 TREE_TYPE (inside_init) = type;
4881 if (TYPE_DOMAIN (type) != 0
4882 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4884 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4885 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4886 /* Subtract 1 (or sizeof (wchar_t))
4887 because it's ok to ignore the terminating null char
4888 that is counted in the length of the constant. */
4889 if (size < TREE_STRING_LENGTH (inside_init)
4890 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4891 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4892 : 1))
4893 pedwarn_init (
4894 "initializer-string for array of chars%s is too long",
4895 " `%s'", NULL);
4897 return inside_init;
4901 /* Any type can be initialized
4902 from an expression of the same type, optionally with braces. */
4904 if (inside_init && TREE_TYPE (inside_init) != 0
4905 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4906 TYPE_MAIN_VARIANT (type))
4907 || (code == ARRAY_TYPE
4908 && comptypes (TREE_TYPE (inside_init), type))
4909 || (code == POINTER_TYPE
4910 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4911 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4912 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4913 TREE_TYPE (type)))))
4915 if (code == POINTER_TYPE
4916 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4917 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4918 inside_init = default_conversion (inside_init);
4919 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4920 && TREE_CODE (inside_init) != CONSTRUCTOR)
4922 error_init ("array%s initialized from non-constant array expression",
4923 " `%s'", NULL);
4924 return error_mark_node;
4927 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4928 inside_init = decl_constant_value (inside_init);
4930 /* Compound expressions can only occur here if -pedantic or
4931 -pedantic-errors is specified. In the later case, we always want
4932 an error. In the former case, we simply want a warning. */
4933 if (require_constant && pedantic
4934 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4936 inside_init
4937 = valid_compound_expr_initializer (inside_init,
4938 TREE_TYPE (inside_init));
4939 if (inside_init == error_mark_node)
4940 error_init ("initializer element%s is not constant",
4941 " for `%s'", NULL);
4942 else
4943 pedwarn_init ("initializer element%s is not constant",
4944 " for `%s'", NULL);
4945 if (flag_pedantic_errors)
4946 inside_init = error_mark_node;
4948 else if (require_constant && ! TREE_CONSTANT (inside_init))
4950 error_init ("initializer element%s is not constant",
4951 " for `%s'", NULL);
4952 inside_init = error_mark_node;
4954 else if (require_constant
4955 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4957 error_init ("initializer element%s is not computable at load time",
4958 " for `%s'", NULL);
4959 inside_init = error_mark_node;
4962 return inside_init;
4965 /* Handle scalar types, including conversions. */
4967 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4968 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4970 /* Note that convert_for_assignment calls default_conversion
4971 for arrays and functions. We must not call it in the
4972 case where inside_init is a null pointer constant. */
4973 inside_init
4974 = convert_for_assignment (type, init, "initialization",
4975 NULL_TREE, NULL_TREE, 0);
4977 if (require_constant && ! TREE_CONSTANT (inside_init))
4979 error_init ("initializer element%s is not constant",
4980 " for `%s'", NULL);
4981 inside_init = error_mark_node;
4983 else if (require_constant
4984 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4986 error_init ("initializer element%s is not computable at load time",
4987 " for `%s'", NULL);
4988 inside_init = error_mark_node;
4991 return inside_init;
4994 /* Come here only for records and arrays. */
4996 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4998 error_init ("variable-sized object%s may not be initialized",
4999 " `%s'", NULL);
5000 return error_mark_node;
5003 /* Traditionally, you can write struct foo x = 0;
5004 and it initializes the first element of x to 0. */
5005 if (flag_traditional)
5007 tree top = 0, prev = 0, otype = type;
5008 while (TREE_CODE (type) == RECORD_TYPE
5009 || TREE_CODE (type) == ARRAY_TYPE
5010 || TREE_CODE (type) == QUAL_UNION_TYPE
5011 || TREE_CODE (type) == UNION_TYPE)
5013 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
5014 if (prev == 0)
5015 top = temp;
5016 else
5017 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
5018 prev = temp;
5019 if (TREE_CODE (type) == ARRAY_TYPE)
5020 type = TREE_TYPE (type);
5021 else if (TYPE_FIELDS (type))
5022 type = TREE_TYPE (TYPE_FIELDS (type));
5023 else
5025 error_init ("invalid initializer%s", " for `%s'", NULL);
5026 return error_mark_node;
5030 if (otype != type)
5032 TREE_OPERAND (prev, 1)
5033 = build_tree_list (NULL_TREE,
5034 digest_init (type, init, require_constant,
5035 constructor_constant));
5036 return top;
5038 else
5039 return error_mark_node;
5041 error_init ("invalid initializer%s", " for `%s'", NULL);
5042 return error_mark_node;
5045 /* Handle initializers that use braces. */
5047 /* Type of object we are accumulating a constructor for.
5048 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5049 static tree constructor_type;
5051 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5052 left to fill. */
5053 static tree constructor_fields;
5055 /* For an ARRAY_TYPE, this is the specified index
5056 at which to store the next element we get.
5057 This is a special INTEGER_CST node that we modify in place. */
5058 static tree constructor_index;
5060 /* For an ARRAY_TYPE, this is the end index of the range
5061 to initialize with the next element, or NULL in the ordinary case
5062 where the element is used just once. */
5063 static tree constructor_range_end;
5065 /* For an ARRAY_TYPE, this is the maximum index. */
5066 static tree constructor_max_index;
5068 /* For a RECORD_TYPE, this is the first field not yet written out. */
5069 static tree constructor_unfilled_fields;
5071 /* For an ARRAY_TYPE, this is the index of the first element
5072 not yet written out.
5073 This is a special INTEGER_CST node that we modify in place. */
5074 static tree constructor_unfilled_index;
5076 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5077 This is so we can generate gaps between fields, when appropriate.
5078 This is a special INTEGER_CST node that we modify in place. */
5079 static tree constructor_bit_index;
5081 /* If we are saving up the elements rather than allocating them,
5082 this is the list of elements so far (in reverse order,
5083 most recent first). */
5084 static tree constructor_elements;
5086 /* 1 if so far this constructor's elements are all compile-time constants. */
5087 static int constructor_constant;
5089 /* 1 if so far this constructor's elements are all valid address constants. */
5090 static int constructor_simple;
5092 /* 1 if this constructor is erroneous so far. */
5093 static int constructor_erroneous;
5095 /* 1 if have called defer_addressed_constants. */
5096 static int constructor_subconstants_deferred;
5098 /* Structure for managing pending initializer elements, organized as an
5099 AVL tree. */
5101 struct init_node
5103 struct init_node *left, *right;
5104 struct init_node *parent;
5105 int balance;
5106 tree purpose;
5107 tree value;
5110 /* Tree of pending elements at this constructor level.
5111 These are elements encountered out of order
5112 which belong at places we haven't reached yet in actually
5113 writing the output. */
5114 static struct init_node *constructor_pending_elts;
5116 /* The SPELLING_DEPTH of this constructor. */
5117 static int constructor_depth;
5119 /* 0 if implicitly pushing constructor levels is allowed. */
5120 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
5122 static int require_constant_value;
5123 static int require_constant_elements;
5125 /* 1 if it is ok to output this constructor as we read it.
5126 0 means must accumulate a CONSTRUCTOR expression. */
5127 static int constructor_incremental;
5129 /* DECL node for which an initializer is being read.
5130 0 means we are reading a constructor expression
5131 such as (struct foo) {...}. */
5132 static tree constructor_decl;
5134 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
5135 static char *constructor_asmspec;
5137 /* Nonzero if this is an initializer for a top-level decl. */
5138 static int constructor_top_level;
5141 /* This stack has a level for each implicit or explicit level of
5142 structuring in the initializer, including the outermost one. It
5143 saves the values of most of the variables above. */
5145 struct constructor_stack
5147 struct constructor_stack *next;
5148 tree type;
5149 tree fields;
5150 tree index;
5151 tree range_end;
5152 tree max_index;
5153 tree unfilled_index;
5154 tree unfilled_fields;
5155 tree bit_index;
5156 tree elements;
5157 int offset;
5158 struct init_node *pending_elts;
5159 int depth;
5160 /* If nonzero, this value should replace the entire
5161 constructor at this level. */
5162 tree replacement_value;
5163 char constant;
5164 char simple;
5165 char implicit;
5166 char incremental;
5167 char erroneous;
5168 char outer;
5171 struct constructor_stack *constructor_stack;
5173 /* This stack records separate initializers that are nested.
5174 Nested initializers can't happen in ANSI C, but GNU C allows them
5175 in cases like { ... (struct foo) { ... } ... }. */
5177 struct initializer_stack
5179 struct initializer_stack *next;
5180 tree decl;
5181 char *asmspec;
5182 struct constructor_stack *constructor_stack;
5183 tree elements;
5184 struct spelling *spelling;
5185 struct spelling *spelling_base;
5186 int spelling_size;
5187 char top_level;
5188 char incremental;
5189 char require_constant_value;
5190 char require_constant_elements;
5191 char deferred;
5194 struct initializer_stack *initializer_stack;
5196 /* Prepare to parse and output the initializer for variable DECL. */
5198 void
5199 start_init (decl, asmspec_tree, top_level)
5200 tree decl;
5201 tree asmspec_tree;
5202 int top_level;
5204 char *locus;
5205 struct initializer_stack *p
5206 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5207 char *asmspec = 0;
5209 if (asmspec_tree)
5210 asmspec = TREE_STRING_POINTER (asmspec_tree);
5212 p->decl = constructor_decl;
5213 p->asmspec = constructor_asmspec;
5214 p->incremental = constructor_incremental;
5215 p->require_constant_value = require_constant_value;
5216 p->require_constant_elements = require_constant_elements;
5217 p->constructor_stack = constructor_stack;
5218 p->elements = constructor_elements;
5219 p->spelling = spelling;
5220 p->spelling_base = spelling_base;
5221 p->spelling_size = spelling_size;
5222 p->deferred = constructor_subconstants_deferred;
5223 p->top_level = constructor_top_level;
5224 p->next = initializer_stack;
5225 initializer_stack = p;
5227 constructor_decl = decl;
5228 constructor_incremental = top_level;
5229 constructor_asmspec = asmspec;
5230 constructor_subconstants_deferred = 0;
5231 constructor_top_level = top_level;
5233 if (decl != 0)
5235 require_constant_value = TREE_STATIC (decl);
5236 require_constant_elements
5237 = ((TREE_STATIC (decl) || pedantic)
5238 /* For a scalar, you can always use any value to initialize,
5239 even within braces. */
5240 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5241 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5242 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5243 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5244 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5245 constructor_incremental |= TREE_STATIC (decl);
5247 else
5249 require_constant_value = 0;
5250 require_constant_elements = 0;
5251 locus = "(anonymous)";
5254 constructor_stack = 0;
5256 missing_braces_mentioned = 0;
5258 spelling_base = 0;
5259 spelling_size = 0;
5260 RESTORE_SPELLING_DEPTH (0);
5262 if (locus)
5263 push_string (locus);
5266 void
5267 finish_init ()
5269 struct initializer_stack *p = initializer_stack;
5271 /* Output subconstants (string constants, usually)
5272 that were referenced within this initializer and saved up.
5273 Must do this if and only if we called defer_addressed_constants. */
5274 if (constructor_subconstants_deferred)
5275 output_deferred_addressed_constants ();
5277 /* Free the whole constructor stack of this initializer. */
5278 while (constructor_stack)
5280 struct constructor_stack *q = constructor_stack;
5281 constructor_stack = q->next;
5282 free (q);
5285 /* Pop back to the data of the outer initializer (if any). */
5286 constructor_decl = p->decl;
5287 constructor_asmspec = p->asmspec;
5288 constructor_incremental = p->incremental;
5289 require_constant_value = p->require_constant_value;
5290 require_constant_elements = p->require_constant_elements;
5291 constructor_stack = p->constructor_stack;
5292 constructor_elements = p->elements;
5293 spelling = p->spelling;
5294 spelling_base = p->spelling_base;
5295 spelling_size = p->spelling_size;
5296 constructor_subconstants_deferred = p->deferred;
5297 constructor_top_level = p->top_level;
5298 initializer_stack = p->next;
5299 free (p);
5302 /* Call here when we see the initializer is surrounded by braces.
5303 This is instead of a call to push_init_level;
5304 it is matched by a call to pop_init_level.
5306 TYPE is the type to initialize, for a constructor expression.
5307 For an initializer for a decl, TYPE is zero. */
5309 void
5310 really_start_incremental_init (type)
5311 tree type;
5313 struct constructor_stack *p
5314 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5316 if (type == 0)
5317 type = TREE_TYPE (constructor_decl);
5319 /* Turn off constructor_incremental if type is a struct with bitfields.
5320 Do this before the first push, so that the corrected value
5321 is available in finish_init. */
5322 check_init_type_bitfields (type);
5324 p->type = constructor_type;
5325 p->fields = constructor_fields;
5326 p->index = constructor_index;
5327 p->range_end = constructor_range_end;
5328 p->max_index = constructor_max_index;
5329 p->unfilled_index = constructor_unfilled_index;
5330 p->unfilled_fields = constructor_unfilled_fields;
5331 p->bit_index = constructor_bit_index;
5332 p->elements = constructor_elements;
5333 p->constant = constructor_constant;
5334 p->simple = constructor_simple;
5335 p->erroneous = constructor_erroneous;
5336 p->pending_elts = constructor_pending_elts;
5337 p->depth = constructor_depth;
5338 p->replacement_value = 0;
5339 p->implicit = 0;
5340 p->incremental = constructor_incremental;
5341 p->outer = 0;
5342 p->next = 0;
5343 constructor_stack = p;
5345 constructor_constant = 1;
5346 constructor_simple = 1;
5347 constructor_depth = SPELLING_DEPTH ();
5348 constructor_elements = 0;
5349 constructor_pending_elts = 0;
5350 constructor_type = type;
5352 if (TREE_CODE (constructor_type) == RECORD_TYPE
5353 || TREE_CODE (constructor_type) == UNION_TYPE)
5355 constructor_fields = TYPE_FIELDS (constructor_type);
5356 /* Skip any nameless bit fields at the beginning. */
5357 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5358 && DECL_NAME (constructor_fields) == 0)
5359 constructor_fields = TREE_CHAIN (constructor_fields);
5360 constructor_unfilled_fields = constructor_fields;
5361 constructor_bit_index = copy_node (integer_zero_node);
5362 TREE_TYPE (constructor_bit_index) = sbitsizetype;
5364 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5366 constructor_range_end = 0;
5367 if (TYPE_DOMAIN (constructor_type))
5369 constructor_max_index
5370 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5371 constructor_index
5372 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5374 else
5375 constructor_index = copy_node (integer_zero_node);
5376 constructor_unfilled_index = copy_node (constructor_index);
5378 else
5380 /* Handle the case of int x = {5}; */
5381 constructor_fields = constructor_type;
5382 constructor_unfilled_fields = constructor_type;
5385 if (constructor_incremental)
5387 int momentary = suspend_momentary ();
5388 push_obstacks_nochange ();
5389 if (TREE_PERMANENT (constructor_decl))
5390 end_temporary_allocation ();
5391 make_decl_rtl (constructor_decl, constructor_asmspec,
5392 constructor_top_level);
5393 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5394 pop_obstacks ();
5395 resume_momentary (momentary);
5398 if (constructor_incremental)
5400 defer_addressed_constants ();
5401 constructor_subconstants_deferred = 1;
5405 /* Push down into a subobject, for initialization.
5406 If this is for an explicit set of braces, IMPLICIT is 0.
5407 If it is because the next element belongs at a lower level,
5408 IMPLICIT is 1. */
5410 void
5411 push_init_level (implicit)
5412 int implicit;
5414 struct constructor_stack *p;
5416 /* If we've exhausted any levels that didn't have braces,
5417 pop them now. */
5418 while (constructor_stack->implicit)
5420 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5421 || TREE_CODE (constructor_type) == UNION_TYPE)
5422 && constructor_fields == 0)
5423 process_init_element (pop_init_level (1));
5424 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5425 && tree_int_cst_lt (constructor_max_index, constructor_index))
5426 process_init_element (pop_init_level (1));
5427 else
5428 break;
5431 /* Structure elements may require alignment. Do this now if necessary
5432 for the subaggregate, and if it comes next in sequence. Don't do
5433 this for subaggregates that will go on the pending list. */
5434 if (constructor_incremental && constructor_type != 0
5435 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields
5436 && constructor_fields == constructor_unfilled_fields)
5438 /* Advance to offset of this element. */
5439 if (! tree_int_cst_equal (constructor_bit_index,
5440 DECL_FIELD_BITPOS (constructor_fields)))
5442 /* By using unsigned arithmetic, the result will be correct even
5443 in case of overflows, if BITS_PER_UNIT is a power of two. */
5444 unsigned next = (TREE_INT_CST_LOW
5445 (DECL_FIELD_BITPOS (constructor_fields))
5446 / (unsigned)BITS_PER_UNIT);
5447 unsigned here = (TREE_INT_CST_LOW (constructor_bit_index)
5448 / (unsigned)BITS_PER_UNIT);
5450 assemble_zeros ((next - here)
5451 * (unsigned)BITS_PER_UNIT
5452 / (unsigned)BITS_PER_UNIT);
5454 /* Indicate that we have now filled the structure up to the current
5455 field. */
5456 constructor_unfilled_fields = constructor_fields;
5459 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5460 p->type = constructor_type;
5461 p->fields = constructor_fields;
5462 p->index = constructor_index;
5463 p->range_end = constructor_range_end;
5464 p->max_index = constructor_max_index;
5465 p->unfilled_index = constructor_unfilled_index;
5466 p->unfilled_fields = constructor_unfilled_fields;
5467 p->bit_index = constructor_bit_index;
5468 p->elements = constructor_elements;
5469 p->constant = constructor_constant;
5470 p->simple = constructor_simple;
5471 p->erroneous = constructor_erroneous;
5472 p->pending_elts = constructor_pending_elts;
5473 p->depth = constructor_depth;
5474 p->replacement_value = 0;
5475 p->implicit = implicit;
5476 p->incremental = constructor_incremental;
5477 p->outer = 0;
5478 p->next = constructor_stack;
5479 constructor_stack = p;
5481 constructor_constant = 1;
5482 constructor_simple = 1;
5483 constructor_depth = SPELLING_DEPTH ();
5484 constructor_elements = 0;
5485 constructor_pending_elts = 0;
5487 /* Don't die if an entire brace-pair level is superfluous
5488 in the containing level. */
5489 if (constructor_type == 0)
5491 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5492 || TREE_CODE (constructor_type) == UNION_TYPE)
5494 /* Don't die if there are extra init elts at the end. */
5495 if (constructor_fields == 0)
5496 constructor_type = 0;
5497 else
5499 constructor_type = TREE_TYPE (constructor_fields);
5500 push_member_name (constructor_fields);
5501 constructor_depth++;
5502 if (constructor_fields != constructor_unfilled_fields)
5503 constructor_incremental = 0;
5506 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5508 constructor_type = TREE_TYPE (constructor_type);
5509 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5510 constructor_depth++;
5511 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5512 || constructor_range_end != 0)
5513 constructor_incremental = 0;
5516 if (constructor_type == 0)
5518 error_init ("extra brace group at end of initializer%s",
5519 " for `%s'", NULL);
5520 constructor_fields = 0;
5521 constructor_unfilled_fields = 0;
5522 return;
5525 /* Turn off constructor_incremental if type is a struct with bitfields. */
5526 check_init_type_bitfields (constructor_type);
5528 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5530 missing_braces_mentioned = 1;
5531 warning_init ("missing braces around initializer%s", " for `%s'", NULL);
5534 if (TREE_CODE (constructor_type) == RECORD_TYPE
5535 || TREE_CODE (constructor_type) == UNION_TYPE)
5537 constructor_fields = TYPE_FIELDS (constructor_type);
5538 /* Skip any nameless bit fields at the beginning. */
5539 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5540 && DECL_NAME (constructor_fields) == 0)
5541 constructor_fields = TREE_CHAIN (constructor_fields);
5542 constructor_unfilled_fields = constructor_fields;
5543 constructor_bit_index = copy_node (integer_zero_node);
5544 TREE_TYPE (constructor_bit_index) = sbitsizetype;
5546 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5548 constructor_range_end = 0;
5549 if (TYPE_DOMAIN (constructor_type))
5551 constructor_max_index
5552 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5553 constructor_index
5554 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5556 else
5557 constructor_index = copy_node (integer_zero_node);
5558 constructor_unfilled_index = copy_node (constructor_index);
5560 else
5562 warning_init ("braces around scalar initializer%s", " for `%s'", NULL);
5563 constructor_fields = constructor_type;
5564 constructor_unfilled_fields = constructor_type;
5568 /* Don't read a struct incrementally if it has any bitfields,
5569 because the incremental reading code doesn't know how to
5570 handle bitfields yet. */
5572 static void
5573 check_init_type_bitfields (type)
5574 tree type;
5576 if (TREE_CODE (type) == RECORD_TYPE)
5578 tree tail;
5579 for (tail = TYPE_FIELDS (type); tail;
5580 tail = TREE_CHAIN (tail))
5582 if (DECL_C_BIT_FIELD (tail)
5583 /* This catches cases like `int foo : 8;'. */
5584 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail)))
5586 constructor_incremental = 0;
5587 break;
5590 check_init_type_bitfields (TREE_TYPE (tail));
5594 else if (TREE_CODE (type) == ARRAY_TYPE)
5595 check_init_type_bitfields (TREE_TYPE (type));
5598 /* At the end of an implicit or explicit brace level,
5599 finish up that level of constructor.
5600 If we were outputting the elements as they are read, return 0
5601 from inner levels (process_init_element ignores that),
5602 but return error_mark_node from the outermost level
5603 (that's what we want to put in DECL_INITIAL).
5604 Otherwise, return a CONSTRUCTOR expression. */
5606 tree
5607 pop_init_level (implicit)
5608 int implicit;
5610 struct constructor_stack *p;
5611 int size = 0;
5612 tree constructor = 0;
5614 if (implicit == 0)
5616 /* When we come to an explicit close brace,
5617 pop any inner levels that didn't have explicit braces. */
5618 while (constructor_stack->implicit)
5619 process_init_element (pop_init_level (1));
5622 p = constructor_stack;
5624 if (constructor_type != 0)
5625 size = int_size_in_bytes (constructor_type);
5627 /* Warn when some struct elements are implicitly initialized to zero. */
5628 if (extra_warnings
5629 && constructor_type
5630 && TREE_CODE (constructor_type) == RECORD_TYPE
5631 && constructor_unfilled_fields)
5633 push_member_name (constructor_unfilled_fields);
5634 warning_init ("missing initializer%s", " for `%s'", NULL);
5635 RESTORE_SPELLING_DEPTH (constructor_depth);
5638 /* Now output all pending elements. */
5639 output_pending_init_elements (1);
5641 #if 0 /* c-parse.in warns about {}. */
5642 /* In ANSI, each brace level must have at least one element. */
5643 if (! implicit && pedantic
5644 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5645 ? integer_zerop (constructor_unfilled_index)
5646 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5647 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL);
5648 #endif
5650 /* Pad out the end of the structure. */
5652 if (p->replacement_value)
5654 /* If this closes a superfluous brace pair,
5655 just pass out the element between them. */
5656 constructor = p->replacement_value;
5657 /* If this is the top level thing within the initializer,
5658 and it's for a variable, then since we already called
5659 assemble_variable, we must output the value now. */
5660 if (p->next == 0 && constructor_decl != 0
5661 && constructor_incremental)
5663 constructor = digest_init (constructor_type, constructor,
5664 require_constant_value,
5665 require_constant_elements);
5667 /* If initializing an array of unknown size,
5668 determine the size now. */
5669 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5670 && TYPE_DOMAIN (constructor_type) == 0)
5672 int failure;
5673 int momentary_p;
5675 push_obstacks_nochange ();
5676 if (TREE_PERMANENT (constructor_type))
5677 end_temporary_allocation ();
5679 momentary_p = suspend_momentary ();
5681 /* We shouldn't have an incomplete array type within
5682 some other type. */
5683 if (constructor_stack->next)
5684 abort ();
5686 failure
5687 = complete_array_type (constructor_type,
5688 constructor, 0);
5689 if (failure)
5690 abort ();
5692 size = int_size_in_bytes (constructor_type);
5693 resume_momentary (momentary_p);
5694 pop_obstacks ();
5697 output_constant (constructor, size);
5700 else if (constructor_type == 0)
5702 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5703 && TREE_CODE (constructor_type) != UNION_TYPE
5704 && TREE_CODE (constructor_type) != ARRAY_TYPE
5705 && ! constructor_incremental)
5707 /* A nonincremental scalar initializer--just return
5708 the element, after verifying there is just one. */
5709 if (constructor_elements == 0)
5711 error_init ("empty scalar initializer%s",
5712 " for `%s'", NULL);
5713 constructor = error_mark_node;
5715 else if (TREE_CHAIN (constructor_elements) != 0)
5717 error_init ("extra elements in scalar initializer%s",
5718 " for `%s'", NULL);
5719 constructor = TREE_VALUE (constructor_elements);
5721 else
5722 constructor = TREE_VALUE (constructor_elements);
5724 else if (! constructor_incremental)
5726 if (constructor_erroneous)
5727 constructor = error_mark_node;
5728 else
5730 int momentary = suspend_momentary ();
5732 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5733 nreverse (constructor_elements));
5734 if (constructor_constant)
5735 TREE_CONSTANT (constructor) = 1;
5736 if (constructor_constant && constructor_simple)
5737 TREE_STATIC (constructor) = 1;
5739 resume_momentary (momentary);
5742 else
5744 tree filled;
5745 int momentary = suspend_momentary ();
5747 if (TREE_CODE (constructor_type) == RECORD_TYPE
5748 || TREE_CODE (constructor_type) == UNION_TYPE)
5750 /* Find the offset of the end of that field. */
5751 filled = size_binop (CEIL_DIV_EXPR,
5752 constructor_bit_index,
5753 size_int (BITS_PER_UNIT));
5755 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5757 /* If initializing an array of unknown size,
5758 determine the size now. */
5759 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5760 && TYPE_DOMAIN (constructor_type) == 0)
5762 tree maxindex
5763 = size_binop (MINUS_EXPR,
5764 constructor_unfilled_index,
5765 integer_one_node);
5767 push_obstacks_nochange ();
5768 if (TREE_PERMANENT (constructor_type))
5769 end_temporary_allocation ();
5770 maxindex = copy_node (maxindex);
5771 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5772 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5774 /* TYPE_MAX_VALUE is always one less than the number of elements
5775 in the array, because we start counting at zero. Therefore,
5776 warn only if the value is less than zero. */
5777 if (pedantic
5778 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5779 < 0))
5780 error_with_decl (constructor_decl,
5781 "zero or negative array size `%s'");
5782 layout_type (constructor_type);
5783 size = int_size_in_bytes (constructor_type);
5784 pop_obstacks ();
5787 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5788 size_in_bytes (TREE_TYPE (constructor_type)));
5790 else
5791 filled = 0;
5793 if (filled != 0)
5794 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5796 resume_momentary (momentary);
5800 constructor_type = p->type;
5801 constructor_fields = p->fields;
5802 constructor_index = p->index;
5803 constructor_range_end = p->range_end;
5804 constructor_max_index = p->max_index;
5805 constructor_unfilled_index = p->unfilled_index;
5806 constructor_unfilled_fields = p->unfilled_fields;
5807 constructor_bit_index = p->bit_index;
5808 constructor_elements = p->elements;
5809 constructor_constant = p->constant;
5810 constructor_simple = p->simple;
5811 constructor_erroneous = p->erroneous;
5812 constructor_pending_elts = p->pending_elts;
5813 constructor_depth = p->depth;
5814 constructor_incremental = p->incremental;
5815 RESTORE_SPELLING_DEPTH (constructor_depth);
5817 constructor_stack = p->next;
5818 free (p);
5820 if (constructor == 0)
5822 if (constructor_stack == 0)
5823 return error_mark_node;
5824 return NULL_TREE;
5826 return constructor;
5829 /* Within an array initializer, specify the next index to be initialized.
5830 FIRST is that index. If LAST is nonzero, then initialize a range
5831 of indices, running from FIRST through LAST. */
5833 void
5834 set_init_index (first, last)
5835 tree first, last;
5837 while ((TREE_CODE (first) == NOP_EXPR
5838 || TREE_CODE (first) == CONVERT_EXPR
5839 || TREE_CODE (first) == NON_LVALUE_EXPR)
5840 && (TYPE_MODE (TREE_TYPE (first))
5841 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5842 (first) = TREE_OPERAND (first, 0);
5843 if (last)
5844 while ((TREE_CODE (last) == NOP_EXPR
5845 || TREE_CODE (last) == CONVERT_EXPR
5846 || TREE_CODE (last) == NON_LVALUE_EXPR)
5847 && (TYPE_MODE (TREE_TYPE (last))
5848 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5849 (last) = TREE_OPERAND (last, 0);
5851 if (TREE_CODE (first) != INTEGER_CST)
5852 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5853 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5854 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5855 else if (! constructor_unfilled_index)
5856 error_init ("array index in non-array initializer%s", " for `%s'", NULL);
5857 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5858 error_init ("duplicate array index in initializer%s", " for `%s'", NULL);
5859 else
5861 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (first);
5862 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (first);
5864 if (last != 0 && tree_int_cst_lt (last, first))
5865 error_init ("empty index range in initializer%s", " for `%s'", NULL);
5866 else
5868 if (pedantic)
5869 pedwarn ("ANSI C forbids specifying element to initialize");
5870 constructor_range_end = last;
5875 /* Within a struct initializer, specify the next field to be initialized. */
5877 void
5878 set_init_label (fieldname)
5879 tree fieldname;
5881 tree tail;
5882 int passed = 0;
5884 /* Don't die if an entire brace-pair level is superfluous
5885 in the containing level. */
5886 if (constructor_type == 0)
5887 return;
5889 for (tail = TYPE_FIELDS (constructor_type); tail;
5890 tail = TREE_CHAIN (tail))
5892 if (tail == constructor_unfilled_fields)
5893 passed = 1;
5894 if (DECL_NAME (tail) == fieldname)
5895 break;
5898 if (tail == 0)
5899 error ("unknown field `%s' specified in initializer",
5900 IDENTIFIER_POINTER (fieldname));
5901 else if (!passed)
5902 error ("field `%s' already initialized",
5903 IDENTIFIER_POINTER (fieldname));
5904 else
5906 constructor_fields = tail;
5907 if (pedantic)
5908 pedwarn ("ANSI C forbids specifying structure member to initialize");
5912 /* Add a new initializer to the tree of pending initializers. PURPOSE
5913 indentifies the initializer, either array index or field in a structure.
5914 VALUE is the value of that index or field. */
5916 static void
5917 add_pending_init (purpose, value)
5918 tree purpose, value;
5920 struct init_node *p, **q, *r;
5922 q = &constructor_pending_elts;
5923 p = 0;
5925 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5927 while (*q != 0)
5929 p = *q;
5930 if (tree_int_cst_lt (purpose, p->purpose))
5931 q = &p->left;
5932 else if (tree_int_cst_lt (p->purpose, purpose))
5933 q = &p->right;
5934 else
5935 abort ();
5938 else
5940 while (*q != NULL)
5942 p = *q;
5943 if (tree_int_cst_lt (DECL_FIELD_BITPOS (purpose),
5944 DECL_FIELD_BITPOS (p->purpose)))
5945 q = &p->left;
5946 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (p->purpose),
5947 DECL_FIELD_BITPOS (purpose)))
5948 q = &p->right;
5949 else
5950 abort ();
5954 r = (struct init_node *) oballoc (sizeof (struct init_node));
5955 r->purpose = purpose;
5956 r->value = value;
5958 *q = r;
5959 r->parent = p;
5960 r->left = 0;
5961 r->right = 0;
5962 r->balance = 0;
5964 while (p)
5966 struct init_node *s;
5968 if (r == p->left)
5970 if (p->balance == 0)
5971 p->balance = -1;
5972 else if (p->balance < 0)
5974 if (r->balance < 0)
5976 /* L rotation. */
5977 p->left = r->right;
5978 if (p->left)
5979 p->left->parent = p;
5980 r->right = p;
5982 p->balance = 0;
5983 r->balance = 0;
5985 s = p->parent;
5986 p->parent = r;
5987 r->parent = s;
5988 if (s)
5990 if (s->left == p)
5991 s->left = r;
5992 else
5993 s->right = r;
5995 else
5996 constructor_pending_elts = r;
5998 else
6000 /* LR rotation. */
6001 struct init_node *t = r->right;
6003 r->right = t->left;
6004 if (r->right)
6005 r->right->parent = r;
6006 t->left = r;
6008 p->left = t->right;
6009 if (p->left)
6010 p->left->parent = p;
6011 t->right = p;
6013 p->balance = t->balance < 0;
6014 r->balance = -(t->balance > 0);
6015 t->balance = 0;
6017 s = p->parent;
6018 p->parent = t;
6019 r->parent = t;
6020 t->parent = s;
6021 if (s)
6023 if (s->left == p)
6024 s->left = t;
6025 else
6026 s->right = t;
6028 else
6029 constructor_pending_elts = t;
6031 break;
6033 else
6035 /* p->balance == +1; growth of left side balances the node. */
6036 p->balance = 0;
6037 break;
6040 else /* r == p->right */
6042 if (p->balance == 0)
6043 /* Growth propagation from right side. */
6044 p->balance++;
6045 else if (p->balance > 0)
6047 if (r->balance > 0)
6049 /* R rotation. */
6050 p->right = r->left;
6051 if (p->right)
6052 p->right->parent = p;
6053 r->left = p;
6055 p->balance = 0;
6056 r->balance = 0;
6058 s = p->parent;
6059 p->parent = r;
6060 r->parent = s;
6061 if (s)
6063 if (s->left == p)
6064 s->left = r;
6065 else
6066 s->right = r;
6068 else
6069 constructor_pending_elts = r;
6071 else /* r->balance == -1 */
6073 /* RL rotation */
6074 struct init_node *t = r->left;
6076 r->left = t->right;
6077 if (r->left)
6078 r->left->parent = r;
6079 t->right = r;
6081 p->right = t->left;
6082 if (p->right)
6083 p->right->parent = p;
6084 t->left = p;
6086 r->balance = (t->balance < 0);
6087 p->balance = -(t->balance > 0);
6088 t->balance = 0;
6090 s = p->parent;
6091 p->parent = t;
6092 r->parent = t;
6093 t->parent = s;
6094 if (s)
6096 if (s->left == p)
6097 s->left = t;
6098 else
6099 s->right = t;
6101 else
6102 constructor_pending_elts = t;
6104 break;
6106 else
6108 /* p->balance == -1; growth of right side balances the node. */
6109 p->balance = 0;
6110 break;
6114 r = p;
6115 p = p->parent;
6119 /* Return nonzero if FIELD is equal to the index of a pending initializer. */
6121 static int
6122 pending_init_member (field)
6123 tree field;
6125 struct init_node *p;
6127 p = constructor_pending_elts;
6128 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6130 while (p)
6132 if (tree_int_cst_equal (field, p->purpose))
6133 return 1;
6134 else if (tree_int_cst_lt (field, p->purpose))
6135 p = p->left;
6136 else
6137 p = p->right;
6140 else
6142 while (p)
6144 if (field == p->purpose)
6145 return 1;
6146 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (field),
6147 DECL_FIELD_BITPOS (p->purpose)))
6148 p = p->left;
6149 else
6150 p = p->right;
6154 return 0;
6157 /* "Output" the next constructor element.
6158 At top level, really output it to assembler code now.
6159 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6160 TYPE is the data type that the containing data type wants here.
6161 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6163 PENDING if non-nil means output pending elements that belong
6164 right after this element. (PENDING is normally 1;
6165 it is 0 while outputting pending elements, to avoid recursion.) */
6167 static void
6168 output_init_element (value, type, field, pending)
6169 tree value, type, field;
6170 int pending;
6172 int duplicate = 0;
6174 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6175 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6176 && !(TREE_CODE (value) == STRING_CST
6177 && TREE_CODE (type) == ARRAY_TYPE
6178 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6179 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6180 TYPE_MAIN_VARIANT (type))))
6181 value = default_conversion (value);
6183 if (value == error_mark_node)
6184 constructor_erroneous = 1;
6185 else if (!TREE_CONSTANT (value))
6186 constructor_constant = 0;
6187 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6188 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6189 || TREE_CODE (constructor_type) == UNION_TYPE)
6190 && DECL_C_BIT_FIELD (field)
6191 && TREE_CODE (value) != INTEGER_CST))
6192 constructor_simple = 0;
6194 if (require_constant_value && ! TREE_CONSTANT (value))
6196 error_init ("initializer element%s is not constant",
6197 " for `%s'", NULL);
6198 value = error_mark_node;
6200 else if (require_constant_elements
6201 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6203 error_init ("initializer element%s is not computable at load time",
6204 " for `%s'", NULL);
6205 value = error_mark_node;
6208 /* If this element duplicates one on constructor_pending_elts,
6209 print a message and ignore it. Don't do this when we're
6210 processing elements taken off constructor_pending_elts,
6211 because we'd always get spurious errors. */
6212 if (pending)
6214 if (TREE_CODE (constructor_type) == RECORD_TYPE
6215 || TREE_CODE (constructor_type) == UNION_TYPE
6216 || TREE_CODE (constructor_type) == ARRAY_TYPE)
6218 if (pending_init_member (field))
6220 error_init ("duplicate initializer%s", " for `%s'", NULL);
6221 duplicate = 1;
6226 /* If this element doesn't come next in sequence,
6227 put it on constructor_pending_elts. */
6228 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6229 && !tree_int_cst_equal (field, constructor_unfilled_index))
6231 if (! duplicate)
6232 /* The copy_node is needed in case field is actually
6233 constructor_index, which is modified in place. */
6234 add_pending_init (copy_node (field),
6235 digest_init (type, value, require_constant_value,
6236 require_constant_elements));
6238 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6239 && field != constructor_unfilled_fields)
6241 /* We do this for records but not for unions. In a union,
6242 no matter which field is specified, it can be initialized
6243 right away since it starts at the beginning of the union. */
6244 if (!duplicate)
6245 add_pending_init (field,
6246 digest_init (type, value, require_constant_value,
6247 require_constant_elements));
6249 else
6251 /* Otherwise, output this element either to
6252 constructor_elements or to the assembler file. */
6254 if (!duplicate)
6256 if (! constructor_incremental)
6258 if (field && TREE_CODE (field) == INTEGER_CST)
6259 field = copy_node (field);
6260 constructor_elements
6261 = tree_cons (field, digest_init (type, value,
6262 require_constant_value,
6263 require_constant_elements),
6264 constructor_elements);
6266 else
6268 /* Structure elements may require alignment.
6269 Do this, if necessary. */
6270 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6272 /* Advance to offset of this element. */
6273 if (! tree_int_cst_equal (constructor_bit_index,
6274 DECL_FIELD_BITPOS (field)))
6276 /* By using unsigned arithmetic, the result will be
6277 correct even in case of overflows, if BITS_PER_UNIT
6278 is a power of two. */
6279 unsigned next = (TREE_INT_CST_LOW
6280 (DECL_FIELD_BITPOS (field))
6281 / (unsigned)BITS_PER_UNIT);
6282 unsigned here = (TREE_INT_CST_LOW
6283 (constructor_bit_index)
6284 / (unsigned)BITS_PER_UNIT);
6286 assemble_zeros ((next - here)
6287 * (unsigned)BITS_PER_UNIT
6288 / (unsigned)BITS_PER_UNIT);
6291 output_constant (digest_init (type, value,
6292 require_constant_value,
6293 require_constant_elements),
6294 int_size_in_bytes (type));
6296 /* For a record or union,
6297 keep track of end position of last field. */
6298 if (TREE_CODE (constructor_type) == RECORD_TYPE
6299 || TREE_CODE (constructor_type) == UNION_TYPE)
6301 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
6302 DECL_SIZE (field));
6303 TREE_INT_CST_LOW (constructor_bit_index)
6304 = TREE_INT_CST_LOW (temp);
6305 TREE_INT_CST_HIGH (constructor_bit_index)
6306 = TREE_INT_CST_HIGH (temp);
6311 /* Advance the variable that indicates sequential elements output. */
6312 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6314 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
6315 integer_one_node);
6316 TREE_INT_CST_LOW (constructor_unfilled_index)
6317 = TREE_INT_CST_LOW (tem);
6318 TREE_INT_CST_HIGH (constructor_unfilled_index)
6319 = TREE_INT_CST_HIGH (tem);
6321 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6322 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6323 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6324 constructor_unfilled_fields = 0;
6326 /* Now output any pending elements which have become next. */
6327 if (pending)
6328 output_pending_init_elements (0);
6332 /* Output any pending elements which have become next.
6333 As we output elements, constructor_unfilled_{fields,index}
6334 advances, which may cause other elements to become next;
6335 if so, they too are output.
6337 If ALL is 0, we return when there are
6338 no more pending elements to output now.
6340 If ALL is 1, we output space as necessary so that
6341 we can output all the pending elements. */
6343 static void
6344 output_pending_init_elements (all)
6345 int all;
6347 struct init_node *elt = constructor_pending_elts;
6348 tree next;
6350 retry:
6352 /* Look thru the whole pending tree.
6353 If we find an element that should be output now,
6354 output it. Otherwise, set NEXT to the element
6355 that comes first among those still pending. */
6357 next = 0;
6358 while (elt)
6360 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6362 if (tree_int_cst_equal (elt->purpose,
6363 constructor_unfilled_index))
6364 output_init_element (elt->value,
6365 TREE_TYPE (constructor_type),
6366 constructor_unfilled_index, 0);
6367 else if (tree_int_cst_lt (constructor_unfilled_index,
6368 elt->purpose))
6370 /* Advance to the next smaller node. */
6371 if (elt->left)
6372 elt = elt->left;
6373 else
6375 /* We have reached the smallest node bigger than the
6376 current unfilled index. Fill the space first. */
6377 next = elt->purpose;
6378 break;
6381 else
6383 /* Advance to the next bigger node. */
6384 if (elt->right)
6385 elt = elt->right;
6386 else
6388 /* We have reached the biggest node in a subtree. Find
6389 the parent of it, which is the next bigger node. */
6390 while (elt->parent && elt->parent->right == elt)
6391 elt = elt->parent;
6392 elt = elt->parent;
6393 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6394 elt->purpose))
6396 next = elt->purpose;
6397 break;
6402 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6403 || TREE_CODE (constructor_type) == UNION_TYPE)
6405 /* If the current record is complete we are done. */
6406 if (constructor_unfilled_fields == 0)
6407 break;
6408 if (elt->purpose == constructor_unfilled_fields)
6410 output_init_element (elt->value,
6411 TREE_TYPE (constructor_unfilled_fields),
6412 constructor_unfilled_fields,
6415 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6416 DECL_FIELD_BITPOS (elt->purpose)))
6418 /* Advance to the next smaller node. */
6419 if (elt->left)
6420 elt = elt->left;
6421 else
6423 /* We have reached the smallest node bigger than the
6424 current unfilled field. Fill the space first. */
6425 next = elt->purpose;
6426 break;
6429 else
6431 /* Advance to the next bigger node. */
6432 if (elt->right)
6433 elt = elt->right;
6434 else
6436 /* We have reached the biggest node in a subtree. Find
6437 the parent of it, which is the next bigger node. */
6438 while (elt->parent && elt->parent->right == elt)
6439 elt = elt->parent;
6440 elt = elt->parent;
6441 if (elt
6442 && tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6443 DECL_FIELD_BITPOS (elt->purpose)))
6445 next = elt->purpose;
6446 break;
6453 /* Ordinarily return, but not if we want to output all
6454 and there are elements left. */
6455 if (! (all && next != 0))
6456 return;
6458 /* Generate space up to the position of NEXT. */
6459 if (constructor_incremental)
6461 tree filled;
6462 tree nextpos_tree = size_int (0);
6464 if (TREE_CODE (constructor_type) == RECORD_TYPE
6465 || TREE_CODE (constructor_type) == UNION_TYPE)
6467 tree tail;
6468 /* Find the last field written out, if any. */
6469 for (tail = TYPE_FIELDS (constructor_type); tail;
6470 tail = TREE_CHAIN (tail))
6471 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
6472 break;
6474 if (tail)
6475 /* Find the offset of the end of that field. */
6476 filled = size_binop (CEIL_DIV_EXPR,
6477 size_binop (PLUS_EXPR,
6478 DECL_FIELD_BITPOS (tail),
6479 DECL_SIZE (tail)),
6480 size_int (BITS_PER_UNIT));
6481 else
6482 filled = size_int (0);
6484 nextpos_tree = size_binop (CEIL_DIV_EXPR,
6485 DECL_FIELD_BITPOS (next),
6486 size_int (BITS_PER_UNIT));
6488 TREE_INT_CST_HIGH (constructor_bit_index)
6489 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
6490 TREE_INT_CST_LOW (constructor_bit_index)
6491 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
6492 constructor_unfilled_fields = next;
6494 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6496 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
6497 size_in_bytes (TREE_TYPE (constructor_type)));
6498 nextpos_tree
6499 = size_binop (MULT_EXPR, next,
6500 size_in_bytes (TREE_TYPE (constructor_type)));
6501 TREE_INT_CST_LOW (constructor_unfilled_index)
6502 = TREE_INT_CST_LOW (next);
6503 TREE_INT_CST_HIGH (constructor_unfilled_index)
6504 = TREE_INT_CST_HIGH (next);
6506 else
6507 filled = 0;
6509 if (filled)
6511 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
6513 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
6516 else
6518 /* If it's not incremental, just skip over the gap,
6519 so that after jumping to retry we will output the next
6520 successive element. */
6521 if (TREE_CODE (constructor_type) == RECORD_TYPE
6522 || TREE_CODE (constructor_type) == UNION_TYPE)
6523 constructor_unfilled_fields = next;
6524 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6526 TREE_INT_CST_LOW (constructor_unfilled_index)
6527 = TREE_INT_CST_LOW (next);
6528 TREE_INT_CST_HIGH (constructor_unfilled_index)
6529 = TREE_INT_CST_HIGH (next);
6533 /* ELT now points to the node in the pending tree with the next
6534 initializer to output. */
6535 goto retry;
6538 /* Add one non-braced element to the current constructor level.
6539 This adjusts the current position within the constructor's type.
6540 This may also start or terminate implicit levels
6541 to handle a partly-braced initializer.
6543 Once this has found the correct level for the new element,
6544 it calls output_init_element.
6546 Note: if we are incrementally outputting this constructor,
6547 this function may be called with a null argument
6548 representing a sub-constructor that was already incrementally output.
6549 When that happens, we output nothing, but we do the bookkeeping
6550 to skip past that element of the current constructor. */
6552 void
6553 process_init_element (value)
6554 tree value;
6556 tree orig_value = value;
6557 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6559 /* Handle superfluous braces around string cst as in
6560 char x[] = {"foo"}; */
6561 if (string_flag
6562 && constructor_type
6563 && TREE_CODE (constructor_type) == ARRAY_TYPE
6564 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6565 && integer_zerop (constructor_unfilled_index))
6567 constructor_stack->replacement_value = value;
6568 return;
6571 if (constructor_stack->replacement_value != 0)
6573 error_init ("excess elements in struct initializer%s",
6574 " after `%s'", NULL_PTR);
6575 return;
6578 /* Ignore elements of a brace group if it is entirely superfluous
6579 and has already been diagnosed. */
6580 if (constructor_type == 0)
6581 return;
6583 /* If we've exhausted any levels that didn't have braces,
6584 pop them now. */
6585 while (constructor_stack->implicit)
6587 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6588 || TREE_CODE (constructor_type) == UNION_TYPE)
6589 && constructor_fields == 0)
6590 process_init_element (pop_init_level (1));
6591 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6592 && (constructor_max_index == 0
6593 || tree_int_cst_lt (constructor_max_index,
6594 constructor_index)))
6595 process_init_element (pop_init_level (1));
6596 else
6597 break;
6600 while (1)
6602 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6604 tree fieldtype;
6605 enum tree_code fieldcode;
6607 if (constructor_fields == 0)
6609 pedwarn_init ("excess elements in struct initializer%s",
6610 " after `%s'", NULL_PTR);
6611 break;
6614 fieldtype = TREE_TYPE (constructor_fields);
6615 if (fieldtype != error_mark_node)
6616 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6617 fieldcode = TREE_CODE (fieldtype);
6619 /* Accept a string constant to initialize a subarray. */
6620 if (value != 0
6621 && fieldcode == ARRAY_TYPE
6622 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6623 && string_flag)
6624 value = orig_value;
6625 /* Otherwise, if we have come to a subaggregate,
6626 and we don't have an element of its type, push into it. */
6627 else if (value != 0 && !constructor_no_implicit
6628 && value != error_mark_node
6629 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6630 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6631 || fieldcode == UNION_TYPE))
6633 push_init_level (1);
6634 continue;
6637 if (value)
6639 push_member_name (constructor_fields);
6640 output_init_element (value, fieldtype, constructor_fields, 1);
6641 RESTORE_SPELLING_DEPTH (constructor_depth);
6643 else
6644 /* Do the bookkeeping for an element that was
6645 directly output as a constructor. */
6647 /* For a record, keep track of end position of last field. */
6648 tree temp = size_binop (PLUS_EXPR,
6649 DECL_FIELD_BITPOS (constructor_fields),
6650 DECL_SIZE (constructor_fields));
6651 TREE_INT_CST_LOW (constructor_bit_index)
6652 = TREE_INT_CST_LOW (temp);
6653 TREE_INT_CST_HIGH (constructor_bit_index)
6654 = TREE_INT_CST_HIGH (temp);
6656 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6659 constructor_fields = TREE_CHAIN (constructor_fields);
6660 /* Skip any nameless bit fields at the beginning. */
6661 while (constructor_fields != 0
6662 && DECL_C_BIT_FIELD (constructor_fields)
6663 && DECL_NAME (constructor_fields) == 0)
6664 constructor_fields = TREE_CHAIN (constructor_fields);
6665 break;
6667 if (TREE_CODE (constructor_type) == UNION_TYPE)
6669 tree fieldtype;
6670 enum tree_code fieldcode;
6672 if (constructor_fields == 0)
6674 pedwarn_init ("excess elements in union initializer%s",
6675 " after `%s'", NULL_PTR);
6676 break;
6679 fieldtype = TREE_TYPE (constructor_fields);
6680 if (fieldtype != error_mark_node)
6681 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6682 fieldcode = TREE_CODE (fieldtype);
6684 /* Accept a string constant to initialize a subarray. */
6685 if (value != 0
6686 && fieldcode == ARRAY_TYPE
6687 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6688 && string_flag)
6689 value = orig_value;
6690 /* Otherwise, if we have come to a subaggregate,
6691 and we don't have an element of its type, push into it. */
6692 else if (value != 0 && !constructor_no_implicit
6693 && value != error_mark_node
6694 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6695 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6696 || fieldcode == UNION_TYPE))
6698 push_init_level (1);
6699 continue;
6702 if (value)
6704 push_member_name (constructor_fields);
6705 output_init_element (value, fieldtype, constructor_fields, 1);
6706 RESTORE_SPELLING_DEPTH (constructor_depth);
6708 else
6709 /* Do the bookkeeping for an element that was
6710 directly output as a constructor. */
6712 TREE_INT_CST_LOW (constructor_bit_index)
6713 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6714 TREE_INT_CST_HIGH (constructor_bit_index)
6715 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6717 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6720 constructor_fields = 0;
6721 break;
6723 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6725 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6726 enum tree_code eltcode = TREE_CODE (elttype);
6728 /* Accept a string constant to initialize a subarray. */
6729 if (value != 0
6730 && eltcode == ARRAY_TYPE
6731 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6732 && string_flag)
6733 value = orig_value;
6734 /* Otherwise, if we have come to a subaggregate,
6735 and we don't have an element of its type, push into it. */
6736 else if (value != 0 && !constructor_no_implicit
6737 && value != error_mark_node
6738 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6739 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6740 || eltcode == UNION_TYPE))
6742 push_init_level (1);
6743 continue;
6746 if (constructor_max_index != 0
6747 && tree_int_cst_lt (constructor_max_index, constructor_index))
6749 pedwarn_init ("excess elements in array initializer%s",
6750 " after `%s'", NULL_PTR);
6751 break;
6754 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */
6755 if (constructor_range_end)
6757 if (constructor_max_index != 0
6758 && tree_int_cst_lt (constructor_max_index,
6759 constructor_range_end))
6761 pedwarn_init ("excess elements in array initializer%s",
6762 " after `%s'", NULL_PTR);
6763 TREE_INT_CST_HIGH (constructor_range_end)
6764 = TREE_INT_CST_HIGH (constructor_max_index);
6765 TREE_INT_CST_LOW (constructor_range_end)
6766 = TREE_INT_CST_LOW (constructor_max_index);
6769 value = save_expr (value);
6772 /* Now output the actual element.
6773 Ordinarily, output once.
6774 If there is a range, repeat it till we advance past the range. */
6777 tree tem;
6779 if (value)
6781 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6782 output_init_element (value, elttype, constructor_index, 1);
6783 RESTORE_SPELLING_DEPTH (constructor_depth);
6786 tem = size_binop (PLUS_EXPR, constructor_index,
6787 integer_one_node);
6788 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (tem);
6789 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (tem);
6791 if (!value)
6792 /* If we are doing the bookkeeping for an element that was
6793 directly output as a constructor,
6794 we must update constructor_unfilled_index. */
6796 TREE_INT_CST_LOW (constructor_unfilled_index)
6797 = TREE_INT_CST_LOW (constructor_index);
6798 TREE_INT_CST_HIGH (constructor_unfilled_index)
6799 = TREE_INT_CST_HIGH (constructor_index);
6802 while (! (constructor_range_end == 0
6803 || tree_int_cst_lt (constructor_range_end,
6804 constructor_index)));
6806 break;
6809 /* Handle the sole element allowed in a braced initializer
6810 for a scalar variable. */
6811 if (constructor_fields == 0)
6813 pedwarn_init ("excess elements in scalar initializer%s",
6814 " after `%s'", NULL_PTR);
6815 break;
6818 if (value)
6819 output_init_element (value, constructor_type, NULL_TREE, 1);
6820 constructor_fields = 0;
6821 break;
6824 /* If the (lexically) previous elments are not now saved,
6825 we can discard the storage for them. */
6826 if (constructor_incremental && constructor_pending_elts == 0 && value != 0
6827 && constructor_stack == 0)
6828 clear_momentary ();
6831 /* Expand an ASM statement with operands, handling output operands
6832 that are not variables or INDIRECT_REFS by transforming such
6833 cases into cases that expand_asm_operands can handle.
6835 Arguments are same as for expand_asm_operands. */
6837 void
6838 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6839 tree string, outputs, inputs, clobbers;
6840 int vol;
6841 char *filename;
6842 int line;
6844 int noutputs = list_length (outputs);
6845 register int i;
6846 /* o[I] is the place that output number I should be written. */
6847 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6848 register tree tail;
6850 if (TREE_CODE (string) == ADDR_EXPR)
6851 string = TREE_OPERAND (string, 0);
6852 if (TREE_CODE (string) != STRING_CST)
6854 error ("asm template is not a string constant");
6855 return;
6858 /* Record the contents of OUTPUTS before it is modified. */
6859 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6860 o[i] = TREE_VALUE (tail);
6862 /* Perform default conversions on array and function inputs. */
6863 /* Don't do this for other types--
6864 it would screw up operands expected to be in memory. */
6865 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6866 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6867 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6868 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6870 /* Generate the ASM_OPERANDS insn;
6871 store into the TREE_VALUEs of OUTPUTS some trees for
6872 where the values were actually stored. */
6873 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6875 /* Copy all the intermediate outputs into the specified outputs. */
6876 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6878 if (o[i] != TREE_VALUE (tail))
6880 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6881 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6882 free_temp_slots ();
6884 /* Detect modification of read-only values.
6885 (Otherwise done by build_modify_expr.) */
6886 else
6888 tree type = TREE_TYPE (o[i]);
6889 if (TREE_READONLY (o[i])
6890 || TYPE_READONLY (type)
6891 || ((TREE_CODE (type) == RECORD_TYPE
6892 || TREE_CODE (type) == UNION_TYPE)
6893 && C_TYPE_FIELDS_READONLY (type)))
6894 readonly_warning (o[i], "modification by `asm'");
6898 /* Those MODIFY_EXPRs could do autoincrements. */
6899 emit_queue ();
6902 /* Expand a C `return' statement.
6903 RETVAL is the expression for what to return,
6904 or a null pointer for `return;' with no value. */
6906 void
6907 c_expand_return (retval)
6908 tree retval;
6910 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6912 if (TREE_THIS_VOLATILE (current_function_decl))
6913 warning ("function declared `noreturn' has a `return' statement");
6915 if (!retval)
6917 current_function_returns_null = 1;
6918 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6919 warning ("`return' with no value, in function returning non-void");
6920 expand_null_return ();
6922 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6924 current_function_returns_null = 1;
6925 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6926 pedwarn ("`return' with a value, in function returning void");
6927 expand_return (retval);
6929 else
6931 tree t = convert_for_assignment (valtype, retval, "return",
6932 NULL_TREE, NULL_TREE, 0);
6933 tree res = DECL_RESULT (current_function_decl);
6934 tree inner;
6936 if (t == error_mark_node)
6937 return;
6939 inner = t = convert (TREE_TYPE (res), t);
6941 /* Strip any conversions, additions, and subtractions, and see if
6942 we are returning the address of a local variable. Warn if so. */
6943 while (1)
6945 switch (TREE_CODE (inner))
6947 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6948 case PLUS_EXPR:
6949 inner = TREE_OPERAND (inner, 0);
6950 continue;
6952 case MINUS_EXPR:
6953 /* If the second operand of the MINUS_EXPR has a pointer
6954 type (or is converted from it), this may be valid, so
6955 don't give a warning. */
6957 tree op1 = TREE_OPERAND (inner, 1);
6959 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6960 && (TREE_CODE (op1) == NOP_EXPR
6961 || TREE_CODE (op1) == NON_LVALUE_EXPR
6962 || TREE_CODE (op1) == CONVERT_EXPR))
6963 op1 = TREE_OPERAND (op1, 0);
6965 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6966 break;
6968 inner = TREE_OPERAND (inner, 0);
6969 continue;
6972 case ADDR_EXPR:
6973 inner = TREE_OPERAND (inner, 0);
6975 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6976 inner = TREE_OPERAND (inner, 0);
6978 if (TREE_CODE (inner) == VAR_DECL
6979 && ! DECL_EXTERNAL (inner)
6980 && ! TREE_STATIC (inner)
6981 && DECL_CONTEXT (inner) == current_function_decl)
6982 warning ("function returns address of local variable");
6983 break;
6985 default:
6986 break;
6989 break;
6992 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6993 TREE_SIDE_EFFECTS (t) = 1;
6994 expand_return (t);
6995 current_function_returns_value = 1;
6999 /* Start a C switch statement, testing expression EXP.
7000 Return EXP if it is valid, an error node otherwise. */
7002 tree
7003 c_expand_start_case (exp)
7004 tree exp;
7006 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
7007 tree type = TREE_TYPE (exp);
7009 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
7011 error ("switch quantity not an integer");
7012 exp = error_mark_node;
7014 else
7016 tree index;
7017 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7019 if (warn_traditional
7020 && (type == long_integer_type_node
7021 || type == long_unsigned_type_node))
7022 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
7024 exp = default_conversion (exp);
7025 type = TREE_TYPE (exp);
7026 index = get_unwidened (exp, NULL_TREE);
7027 /* We can't strip a conversion from a signed type to an unsigned,
7028 because if we did, int_fits_type_p would do the wrong thing
7029 when checking case values for being in range,
7030 and it's too hard to do the right thing. */
7031 if (TREE_UNSIGNED (TREE_TYPE (exp))
7032 == TREE_UNSIGNED (TREE_TYPE (index)))
7033 exp = index;
7036 expand_start_case (1, exp, type, "switch statement");
7038 return exp;