gcc2 snapshot 980401 import
[official-gcc.git] / gcc / c-typeck.c
blobd56ab38d64187d6ea298329fabed20c32fae3296
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 <stdio.h>
33 #include "tree.h"
34 #include "c-tree.h"
35 #include "flags.h"
36 #include "output.h"
38 #ifdef HAVE_STDLIB_H
39 #include <stdlib.h>
40 #endif
42 #ifdef HAVE_STRING_H
43 #include <string.h>
44 #else
45 #ifdef HAVE_STRINGS_H
46 #include <strings.h>
47 #endif
48 #endif
50 /* Nonzero if we've already printed a "missing braces around initializer"
51 message within this initializer. */
52 static int missing_braces_mentioned;
54 #ifdef NEED_DECLARATION_INDEX
55 extern char *index ();
56 #endif
58 #ifdef NEED_DECLARATION_RINDEX
59 extern char *rindex ();
60 #endif
62 static tree qualify_type PROTO((tree, tree));
63 static int comp_target_types PROTO((tree, tree));
64 static int function_types_compatible_p PROTO((tree, tree));
65 static int type_lists_compatible_p PROTO((tree, tree));
66 static int self_promoting_type_p PROTO((tree));
67 static tree decl_constant_value PROTO((tree));
68 static tree lookup_field PROTO((tree, tree, tree *));
69 static tree convert_arguments PROTO((tree, tree, tree, tree));
70 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
71 static tree pointer_diff PROTO((tree, tree));
72 static tree unary_complex_lvalue PROTO((enum tree_code, tree));
73 static void pedantic_lvalue_warning PROTO((enum tree_code));
74 static tree internal_build_compound_expr PROTO((tree, int));
75 static tree convert_for_assignment PROTO((tree, tree, char *, tree,
76 tree, int));
77 static void warn_for_assignment PROTO((char *, char *, tree, int));
78 static tree valid_compound_expr_initializer PROTO((tree, tree));
79 static void push_string PROTO((char *));
80 static void push_member_name PROTO((tree));
81 static void push_array_bounds PROTO((int));
82 static int spelling_length PROTO((void));
83 static char *print_spelling PROTO((char *));
84 static char *get_spelling PROTO((char *));
85 static void warning_init PROTO((char *, char *,
86 char *));
87 static tree digest_init PROTO((tree, tree, int, int));
88 static void check_init_type_bitfields PROTO((tree));
89 static void output_init_element PROTO((tree, tree, tree, int));
90 static void output_pending_init_elements PROTO((int));
91 static void add_pending_init PROTO((tree, tree));
92 static int pending_init_member PROTO((tree));
94 /* Do `exp = require_complete_type (exp);' to make sure exp
95 does not have an incomplete type. (That includes void types.) */
97 tree
98 require_complete_type (value)
99 tree value;
101 tree type = TREE_TYPE (value);
103 /* First, detect a valid value with a complete type. */
104 if (TYPE_SIZE (type) != 0
105 && type != void_type_node)
106 return value;
108 incomplete_type_error (value, type);
109 return error_mark_node;
112 /* Print an error message for invalid use of an incomplete type.
113 VALUE is the expression that was used (or 0 if that isn't known)
114 and TYPE is the type that was invalid. */
116 void
117 incomplete_type_error (value, type)
118 tree value;
119 tree type;
121 char *errmsg;
123 /* Avoid duplicate error message. */
124 if (TREE_CODE (type) == ERROR_MARK)
125 return;
127 if (value != 0 && (TREE_CODE (value) == VAR_DECL
128 || TREE_CODE (value) == PARM_DECL))
129 error ("`%s' has an incomplete type",
130 IDENTIFIER_POINTER (DECL_NAME (value)));
131 else
133 retry:
134 /* We must print an error message. Be clever about what it says. */
136 switch (TREE_CODE (type))
138 case RECORD_TYPE:
139 errmsg = "invalid use of undefined type `struct %s'";
140 break;
142 case UNION_TYPE:
143 errmsg = "invalid use of undefined type `union %s'";
144 break;
146 case ENUMERAL_TYPE:
147 errmsg = "invalid use of undefined type `enum %s'";
148 break;
150 case VOID_TYPE:
151 error ("invalid use of void expression");
152 return;
154 case ARRAY_TYPE:
155 if (TYPE_DOMAIN (type))
157 type = TREE_TYPE (type);
158 goto retry;
160 error ("invalid use of array with unspecified bounds");
161 return;
163 default:
164 abort ();
167 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
168 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
169 else
170 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
171 error ("invalid use of incomplete typedef `%s'",
172 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
176 /* Return a variant of TYPE which has all the type qualifiers of LIKE
177 as well as those of TYPE. */
179 static tree
180 qualify_type (type, like)
181 tree type, like;
183 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
184 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
185 return c_build_type_variant (type, constflag, volflag);
188 /* Return the common type of two types.
189 We assume that comptypes has already been done and returned 1;
190 if that isn't so, this may crash. In particular, we assume that qualifiers
191 match.
193 This is the type for the result of most arithmetic operations
194 if the operands have the given two types. */
196 tree
197 common_type (t1, t2)
198 tree t1, t2;
200 register enum tree_code code1;
201 register enum tree_code code2;
202 tree attributes;
204 /* Save time if the two types are the same. */
206 if (t1 == t2) return t1;
208 /* If one type is nonsense, use the other. */
209 if (t1 == error_mark_node)
210 return t2;
211 if (t2 == error_mark_node)
212 return t1;
214 /* Merge the attributes */
215 attributes = merge_attributes (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
217 /* Treat an enum type as the unsigned integer type of the same width. */
219 if (TREE_CODE (t1) == ENUMERAL_TYPE)
220 t1 = type_for_size (TYPE_PRECISION (t1), 1);
221 if (TREE_CODE (t2) == ENUMERAL_TYPE)
222 t2 = type_for_size (TYPE_PRECISION (t2), 1);
224 code1 = TREE_CODE (t1);
225 code2 = TREE_CODE (t2);
227 /* If one type is complex, form the common type of the non-complex
228 components, then make that complex. Use T1 or T2 if it is the
229 required type. */
230 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
232 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
233 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
234 tree subtype = common_type (subtype1, subtype2);
236 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
237 return build_type_attribute_variant (t1, attributes);
238 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
239 return build_type_attribute_variant (t2, attributes);
240 else
241 return build_type_attribute_variant (build_complex_type (subtype),
242 attributes);
245 switch (code1)
247 case INTEGER_TYPE:
248 case REAL_TYPE:
249 /* If only one is real, use it as the result. */
251 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
252 return build_type_attribute_variant (t1, attributes);
254 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
255 return build_type_attribute_variant (t2, attributes);
257 /* Both real or both integers; use the one with greater precision. */
259 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
260 return build_type_attribute_variant (t1, attributes);
261 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
262 return build_type_attribute_variant (t2, attributes);
264 /* Same precision. Prefer longs to ints even when same size. */
266 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
267 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
268 return build_type_attribute_variant (long_unsigned_type_node,
269 attributes);
271 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
272 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
274 /* But preserve unsignedness from the other type,
275 since long cannot hold all the values of an unsigned int. */
276 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
277 t1 = long_unsigned_type_node;
278 else
279 t1 = long_integer_type_node;
280 return build_type_attribute_variant (t1, attributes);
283 /* Likewise, prefer long double to double even if same size. */
284 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
285 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
286 return build_type_attribute_variant (long_double_type_node,
287 attributes);
289 /* Otherwise prefer the unsigned one. */
291 if (TREE_UNSIGNED (t1))
292 return build_type_attribute_variant (t1, attributes);
293 else
294 return build_type_attribute_variant (t2, attributes);
296 case POINTER_TYPE:
297 /* For two pointers, do this recursively on the target type,
298 and combine the qualifiers of the two types' targets. */
299 /* This code was turned off; I don't know why.
300 But ANSI C specifies doing this with the qualifiers.
301 So I turned it on again. */
303 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
304 TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
305 int constp
306 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
307 int volatilep
308 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
309 t1 = build_pointer_type (c_build_type_variant (target, constp,
310 volatilep));
311 return build_type_attribute_variant (t1, attributes);
313 #if 0
314 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
315 return build_type_attribute_variant (t1, attributes);
316 #endif
318 case ARRAY_TYPE:
320 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
321 /* Save space: see if the result is identical to one of the args. */
322 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
323 return build_type_attribute_variant (t1, attributes);
324 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
325 return build_type_attribute_variant (t2, attributes);
326 /* Merge the element types, and have a size if either arg has one. */
327 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
328 return build_type_attribute_variant (t1, attributes);
331 case FUNCTION_TYPE:
332 /* Function types: prefer the one that specified arg types.
333 If both do, merge the arg types. Also merge the return types. */
335 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
336 tree p1 = TYPE_ARG_TYPES (t1);
337 tree p2 = TYPE_ARG_TYPES (t2);
338 int len;
339 tree newargs, n;
340 int i;
342 /* Save space: see if the result is identical to one of the args. */
343 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
344 return build_type_attribute_variant (t1, attributes);
345 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
346 return build_type_attribute_variant (t2, attributes);
348 /* Simple way if one arg fails to specify argument types. */
349 if (TYPE_ARG_TYPES (t1) == 0)
351 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
352 return build_type_attribute_variant (t1, attributes);
354 if (TYPE_ARG_TYPES (t2) == 0)
356 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
357 return build_type_attribute_variant (t1, attributes);
360 /* If both args specify argument types, we must merge the two
361 lists, argument by argument. */
363 len = list_length (p1);
364 newargs = 0;
366 for (i = 0; i < len; i++)
367 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
369 n = newargs;
371 for (; p1;
372 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
374 /* A null type means arg type is not specified.
375 Take whatever the other function type has. */
376 if (TREE_VALUE (p1) == 0)
378 TREE_VALUE (n) = TREE_VALUE (p2);
379 goto parm_done;
381 if (TREE_VALUE (p2) == 0)
383 TREE_VALUE (n) = TREE_VALUE (p1);
384 goto parm_done;
387 /* Given wait (union {union wait *u; int *i} *)
388 and wait (union wait *),
389 prefer union wait * as type of parm. */
390 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
391 && TREE_VALUE (p1) != TREE_VALUE (p2))
393 tree memb;
394 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
395 memb; memb = TREE_CHAIN (memb))
396 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
398 TREE_VALUE (n) = TREE_VALUE (p2);
399 if (pedantic)
400 pedwarn ("function types not truly compatible in ANSI C");
401 goto parm_done;
404 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
405 && TREE_VALUE (p2) != TREE_VALUE (p1))
407 tree memb;
408 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
409 memb; memb = TREE_CHAIN (memb))
410 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
412 TREE_VALUE (n) = TREE_VALUE (p1);
413 if (pedantic)
414 pedwarn ("function types not truly compatible in ANSI C");
415 goto parm_done;
418 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
419 parm_done: ;
422 t1 = build_function_type (valtype, newargs);
423 /* ... falls through ... */
426 default:
427 return build_type_attribute_variant (t1, attributes);
432 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
433 or various other operations. Return 2 if they are compatible
434 but a warning may be needed if you use them together. */
437 comptypes (type1, type2)
438 tree type1, type2;
440 register tree t1 = type1;
441 register tree t2 = type2;
442 int attrval, val;
444 /* Suppress errors caused by previously reported errors. */
446 if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
447 return 1;
449 /* Treat an enum type as the integer type of the same width and
450 signedness. */
452 if (TREE_CODE (t1) == ENUMERAL_TYPE)
453 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
454 if (TREE_CODE (t2) == ENUMERAL_TYPE)
455 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
457 if (t1 == t2)
458 return 1;
460 /* Different classes of types can't be compatible. */
462 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
464 /* Qualifiers must match. */
466 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
467 return 0;
468 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
469 return 0;
471 /* Allow for two different type nodes which have essentially the same
472 definition. Note that we already checked for equality of the type
473 type qualifiers (just above). */
475 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
476 return 1;
478 #ifndef COMP_TYPE_ATTRIBUTES
479 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
480 #endif
482 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
483 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
484 return 0;
486 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
487 val = 0;
489 switch (TREE_CODE (t1))
491 case POINTER_TYPE:
492 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
493 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
494 break;
496 case FUNCTION_TYPE:
497 val = function_types_compatible_p (t1, t2);
498 break;
500 case ARRAY_TYPE:
502 tree d1 = TYPE_DOMAIN (t1);
503 tree d2 = TYPE_DOMAIN (t2);
504 val = 1;
506 /* Target types must match incl. qualifiers. */
507 if (TREE_TYPE (t1) != TREE_TYPE (t2)
508 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
509 return 0;
511 /* Sizes must match unless one is missing or variable. */
512 if (d1 == 0 || d2 == 0 || d1 == d2
513 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
514 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
515 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
516 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
517 break;
519 if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
520 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
521 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
522 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
523 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
524 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
525 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
526 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2)))))
527 val = 0;
528 break;
531 case RECORD_TYPE:
532 if (maybe_objc_comptypes (t1, t2, 0) == 1)
533 val = 1;
534 break;
536 default:
537 break;
539 return attrval == 2 && val == 1 ? 2 : val;
542 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
543 ignoring their qualifiers. */
545 static int
546 comp_target_types (ttl, ttr)
547 tree ttl, ttr;
549 int val;
551 /* Give maybe_objc_comptypes a crack at letting these types through. */
552 if (val = maybe_objc_comptypes (ttl, ttr, 1) >= 0)
553 return val;
555 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
556 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
558 if (val == 2 && pedantic)
559 pedwarn ("types are not quite compatible");
560 return val;
563 /* Subroutines of `comptypes'. */
565 /* Return 1 if two function types F1 and F2 are compatible.
566 If either type specifies no argument types,
567 the other must specify a fixed number of self-promoting arg types.
568 Otherwise, if one type specifies only the number of arguments,
569 the other must specify that number of self-promoting arg types.
570 Otherwise, the argument types must match. */
572 static int
573 function_types_compatible_p (f1, f2)
574 tree f1, f2;
576 tree args1, args2;
577 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
578 int val = 1;
579 int val1;
581 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
582 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
583 return 0;
585 args1 = TYPE_ARG_TYPES (f1);
586 args2 = TYPE_ARG_TYPES (f2);
588 /* An unspecified parmlist matches any specified parmlist
589 whose argument types don't need default promotions. */
591 if (args1 == 0)
593 if (!self_promoting_args_p (args2))
594 return 0;
595 /* If one of these types comes from a non-prototype fn definition,
596 compare that with the other type's arglist.
597 If they don't match, ask for a warning (but no error). */
598 if (TYPE_ACTUAL_ARG_TYPES (f1)
599 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
600 val = 2;
601 return val;
603 if (args2 == 0)
605 if (!self_promoting_args_p (args1))
606 return 0;
607 if (TYPE_ACTUAL_ARG_TYPES (f2)
608 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
609 val = 2;
610 return val;
613 /* Both types have argument lists: compare them and propagate results. */
614 val1 = type_lists_compatible_p (args1, args2);
615 return val1 != 1 ? val1 : val;
618 /* Check two lists of types for compatibility,
619 returning 0 for incompatible, 1 for compatible,
620 or 2 for compatible with warning. */
622 static int
623 type_lists_compatible_p (args1, args2)
624 tree args1, args2;
626 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
627 int val = 1;
628 int newval = 0;
630 while (1)
632 if (args1 == 0 && args2 == 0)
633 return val;
634 /* If one list is shorter than the other,
635 they fail to match. */
636 if (args1 == 0 || args2 == 0)
637 return 0;
638 /* A null pointer instead of a type
639 means there is supposed to be an argument
640 but nothing is specified about what type it has.
641 So match anything that self-promotes. */
642 if (TREE_VALUE (args1) == 0)
644 if (! self_promoting_type_p (TREE_VALUE (args2)))
645 return 0;
647 else if (TREE_VALUE (args2) == 0)
649 if (! self_promoting_type_p (TREE_VALUE (args1)))
650 return 0;
652 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
654 /* Allow wait (union {union wait *u; int *i} *)
655 and wait (union wait *) to be compatible. */
656 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
657 && (TYPE_NAME (TREE_VALUE (args1)) == 0
658 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
659 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
660 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
661 TYPE_SIZE (TREE_VALUE (args2))))
663 tree memb;
664 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
665 memb; memb = TREE_CHAIN (memb))
666 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
667 break;
668 if (memb == 0)
669 return 0;
671 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
672 && (TYPE_NAME (TREE_VALUE (args2)) == 0
673 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
674 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
675 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
676 TYPE_SIZE (TREE_VALUE (args1))))
678 tree memb;
679 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
680 memb; memb = TREE_CHAIN (memb))
681 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
682 break;
683 if (memb == 0)
684 return 0;
686 else
687 return 0;
690 /* comptypes said ok, but record if it said to warn. */
691 if (newval > val)
692 val = newval;
694 args1 = TREE_CHAIN (args1);
695 args2 = TREE_CHAIN (args2);
699 /* Return 1 if PARMS specifies a fixed number of parameters
700 and none of their types is affected by default promotions. */
703 self_promoting_args_p (parms)
704 tree parms;
706 register tree t;
707 for (t = parms; t; t = TREE_CHAIN (t))
709 register tree type = TREE_VALUE (t);
711 if (TREE_CHAIN (t) == 0 && type != void_type_node)
712 return 0;
714 if (type == 0)
715 return 0;
717 if (TYPE_MAIN_VARIANT (type) == float_type_node)
718 return 0;
720 if (C_PROMOTING_INTEGER_TYPE_P (type))
721 return 0;
723 return 1;
726 /* Return 1 if TYPE is not affected by default promotions. */
728 static int
729 self_promoting_type_p (type)
730 tree type;
732 if (TYPE_MAIN_VARIANT (type) == float_type_node)
733 return 0;
735 if (C_PROMOTING_INTEGER_TYPE_P (type))
736 return 0;
738 return 1;
741 /* Return an unsigned type the same as TYPE in other respects. */
743 tree
744 unsigned_type (type)
745 tree type;
747 tree type1 = TYPE_MAIN_VARIANT (type);
748 if (type1 == signed_char_type_node || type1 == char_type_node)
749 return unsigned_char_type_node;
750 if (type1 == integer_type_node)
751 return unsigned_type_node;
752 if (type1 == short_integer_type_node)
753 return short_unsigned_type_node;
754 if (type1 == long_integer_type_node)
755 return long_unsigned_type_node;
756 if (type1 == long_long_integer_type_node)
757 return long_long_unsigned_type_node;
758 if (type1 == intDI_type_node)
759 return unsigned_intDI_type_node;
760 if (type1 == intSI_type_node)
761 return unsigned_intSI_type_node;
762 if (type1 == intHI_type_node)
763 return unsigned_intHI_type_node;
764 if (type1 == intQI_type_node)
765 return unsigned_intQI_type_node;
767 return signed_or_unsigned_type (1, type);
770 /* Return a signed type the same as TYPE in other respects. */
772 tree
773 signed_type (type)
774 tree type;
776 tree type1 = TYPE_MAIN_VARIANT (type);
777 if (type1 == unsigned_char_type_node || type1 == char_type_node)
778 return signed_char_type_node;
779 if (type1 == unsigned_type_node)
780 return integer_type_node;
781 if (type1 == short_unsigned_type_node)
782 return short_integer_type_node;
783 if (type1 == long_unsigned_type_node)
784 return long_integer_type_node;
785 if (type1 == long_long_unsigned_type_node)
786 return long_long_integer_type_node;
787 if (type1 == unsigned_intDI_type_node)
788 return intDI_type_node;
789 if (type1 == unsigned_intSI_type_node)
790 return intSI_type_node;
791 if (type1 == unsigned_intHI_type_node)
792 return intHI_type_node;
793 if (type1 == unsigned_intQI_type_node)
794 return intQI_type_node;
796 return signed_or_unsigned_type (0, type);
799 /* Return a type the same as TYPE except unsigned or
800 signed according to UNSIGNEDP. */
802 tree
803 signed_or_unsigned_type (unsignedp, type)
804 int unsignedp;
805 tree type;
807 if ((! INTEGRAL_TYPE_P (type) && ! POINTER_TYPE_P (type))
808 || TREE_UNSIGNED (type) == unsignedp)
809 return type;
810 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
811 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
812 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
813 return unsignedp ? unsigned_type_node : integer_type_node;
814 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
815 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
816 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
817 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
818 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
819 return (unsignedp ? long_long_unsigned_type_node
820 : long_long_integer_type_node);
821 return type;
824 /* Compute the value of the `sizeof' operator. */
826 tree
827 c_sizeof (type)
828 tree type;
830 enum tree_code code = TREE_CODE (type);
831 tree t;
833 if (code == FUNCTION_TYPE)
835 if (pedantic || warn_pointer_arith)
836 pedwarn ("sizeof applied to a function type");
837 return size_int (1);
839 if (code == VOID_TYPE)
841 if (pedantic || warn_pointer_arith)
842 pedwarn ("sizeof applied to a void type");
843 return size_int (1);
845 if (code == ERROR_MARK)
846 return size_int (1);
847 if (TYPE_SIZE (type) == 0)
849 error ("sizeof applied to an incomplete type");
850 return size_int (0);
853 /* Convert in case a char is more than one unit. */
854 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
855 size_int (TYPE_PRECISION (char_type_node)));
856 /* size_binop does not put the constant in range, so do it now. */
857 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
858 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
859 return t;
862 tree
863 c_sizeof_nowarn (type)
864 tree type;
866 enum tree_code code = TREE_CODE (type);
867 tree t;
869 if (code == FUNCTION_TYPE
870 || code == VOID_TYPE
871 || code == ERROR_MARK)
872 return size_int (1);
873 if (TYPE_SIZE (type) == 0)
874 return size_int (0);
876 /* Convert in case a char is more than one unit. */
877 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
878 size_int (TYPE_PRECISION (char_type_node)));
879 force_fit_type (t, 0);
880 return t;
883 /* Compute the size to increment a pointer by. */
885 tree
886 c_size_in_bytes (type)
887 tree type;
889 enum tree_code code = TREE_CODE (type);
890 tree t;
892 if (code == FUNCTION_TYPE)
893 return size_int (1);
894 if (code == VOID_TYPE)
895 return size_int (1);
896 if (code == ERROR_MARK)
897 return size_int (1);
898 if (TYPE_SIZE (type) == 0)
900 error ("arithmetic on pointer to an incomplete type");
901 return size_int (1);
904 /* Convert in case a char is more than one unit. */
905 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
906 size_int (BITS_PER_UNIT));
907 force_fit_type (t, 0);
908 return t;
911 /* Implement the __alignof keyword: Return the minimum required
912 alignment of TYPE, measured in bytes. */
914 tree
915 c_alignof (type)
916 tree type;
918 enum tree_code code = TREE_CODE (type);
920 if (code == FUNCTION_TYPE)
921 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
923 if (code == VOID_TYPE || code == ERROR_MARK)
924 return size_int (1);
926 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
929 /* Implement the __alignof keyword: Return the minimum required
930 alignment of EXPR, measured in bytes. For VAR_DECL's and
931 FIELD_DECL's return DECL_ALIGN (which can be set from an
932 "aligned" __attribute__ specification). */
934 tree
935 c_alignof_expr (expr)
936 tree expr;
938 if (TREE_CODE (expr) == VAR_DECL)
939 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
941 if (TREE_CODE (expr) == COMPONENT_REF
942 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
944 error ("`__alignof' applied to a bit-field");
945 return size_int (1);
947 else if (TREE_CODE (expr) == COMPONENT_REF
948 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
949 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
951 if (TREE_CODE (expr) == INDIRECT_REF)
953 tree t = TREE_OPERAND (expr, 0);
954 tree best = t;
955 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
957 while (TREE_CODE (t) == NOP_EXPR
958 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
960 int thisalign;
962 t = TREE_OPERAND (t, 0);
963 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
964 if (thisalign > bestalign)
965 best = t, bestalign = thisalign;
967 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
969 else
970 return c_alignof (TREE_TYPE (expr));
973 /* Return either DECL or its known constant value (if it has one). */
975 static tree
976 decl_constant_value (decl)
977 tree decl;
979 if (/* Don't change a variable array bound or initial value to a constant
980 in a place where a variable is invalid. */
981 current_function_decl != 0
982 && ! pedantic
983 && ! TREE_THIS_VOLATILE (decl)
984 && TREE_READONLY (decl) && ! ITERATOR_P (decl)
985 && DECL_INITIAL (decl) != 0
986 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
987 /* This is invalid if initial value is not constant.
988 If it has either a function call, a memory reference,
989 or a variable, then re-evaluating it could give different results. */
990 && TREE_CONSTANT (DECL_INITIAL (decl))
991 /* Check for cases where this is sub-optimal, even though valid. */
992 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
993 && DECL_MODE (decl) != BLKmode)
994 return DECL_INITIAL (decl);
995 return decl;
998 /* Perform default promotions for C data used in expressions.
999 Arrays and functions are converted to pointers;
1000 enumeral types or short or char, to int.
1001 In addition, manifest constants symbols are replaced by their values. */
1003 tree
1004 default_conversion (exp)
1005 tree exp;
1007 register tree type = TREE_TYPE (exp);
1008 register enum tree_code code = TREE_CODE (type);
1010 /* Constants can be used directly unless they're not loadable. */
1011 if (TREE_CODE (exp) == CONST_DECL)
1012 exp = DECL_INITIAL (exp);
1014 /* Replace a nonvolatile const static variable with its value unless
1015 it is an array, in which case we must be sure that taking the
1016 address of the array produces consistent results. */
1017 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1019 exp = decl_constant_value (exp);
1020 type = TREE_TYPE (exp);
1023 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1024 an lvalue. */
1025 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
1026 to integer and cause infinite recursion. */
1027 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1028 || (TREE_CODE (exp) == NOP_EXPR
1029 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1030 exp = TREE_OPERAND (exp, 0);
1032 /* Normally convert enums to int,
1033 but convert wide enums to something wider. */
1034 if (code == ENUMERAL_TYPE)
1036 type = type_for_size (MAX (TYPE_PRECISION (type),
1037 TYPE_PRECISION (integer_type_node)),
1038 ((flag_traditional
1039 || (TYPE_PRECISION (type)
1040 >= TYPE_PRECISION (integer_type_node)))
1041 && TREE_UNSIGNED (type)));
1042 return convert (type, exp);
1045 if (TREE_CODE (exp) == COMPONENT_REF
1046 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
1048 tree width = DECL_SIZE (TREE_OPERAND (exp, 1));
1049 HOST_WIDE_INT low = TREE_INT_CST_LOW (width);
1051 /* If it's thinner than an int, promote it like a
1052 C_PROMOTING_INTEGER_TYPE_P, otherwise leave it alone. */
1054 if (low < TYPE_PRECISION (integer_type_node))
1056 if (flag_traditional && TREE_UNSIGNED (type))
1057 return convert (unsigned_type_node, exp);
1058 else
1059 return convert (integer_type_node, exp);
1063 if (C_PROMOTING_INTEGER_TYPE_P (type))
1065 /* Traditionally, unsignedness is preserved in default promotions.
1066 Also preserve unsignedness if not really getting any wider. */
1067 if (TREE_UNSIGNED (type)
1068 && (flag_traditional
1069 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1070 return convert (unsigned_type_node, exp);
1071 return convert (integer_type_node, exp);
1073 if (flag_traditional && !flag_allow_single_precision
1074 && TYPE_MAIN_VARIANT (type) == float_type_node)
1075 return convert (double_type_node, exp);
1076 if (code == VOID_TYPE)
1078 error ("void value not ignored as it ought to be");
1079 return error_mark_node;
1081 if (code == FUNCTION_TYPE)
1083 return build_unary_op (ADDR_EXPR, exp, 0);
1085 if (code == ARRAY_TYPE)
1087 register tree adr;
1088 tree restype = TREE_TYPE (type);
1089 tree ptrtype;
1090 int constp = 0;
1091 int volatilep = 0;
1093 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1094 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1096 constp = TREE_READONLY (exp);
1097 volatilep = TREE_THIS_VOLATILE (exp);
1100 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1101 || constp || volatilep)
1102 restype = c_build_type_variant (restype,
1103 TYPE_READONLY (type) || constp,
1104 TYPE_VOLATILE (type) || volatilep);
1106 if (TREE_CODE (exp) == INDIRECT_REF)
1107 return convert (TYPE_POINTER_TO (restype),
1108 TREE_OPERAND (exp, 0));
1110 if (TREE_CODE (exp) == COMPOUND_EXPR)
1112 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1113 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1114 TREE_OPERAND (exp, 0), op1);
1117 if (! lvalue_p (exp)
1118 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1120 error ("invalid use of non-lvalue array");
1121 return error_mark_node;
1124 ptrtype = build_pointer_type (restype);
1126 if (TREE_CODE (exp) == VAR_DECL)
1128 /* ??? This is not really quite correct
1129 in that the type of the operand of ADDR_EXPR
1130 is not the target type of the type of the ADDR_EXPR itself.
1131 Question is, can this lossage be avoided? */
1132 adr = build1 (ADDR_EXPR, ptrtype, exp);
1133 if (mark_addressable (exp) == 0)
1134 return error_mark_node;
1135 TREE_CONSTANT (adr) = staticp (exp);
1136 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1137 return adr;
1139 /* This way is better for a COMPONENT_REF since it can
1140 simplify the offset for a component. */
1141 adr = build_unary_op (ADDR_EXPR, exp, 1);
1142 return convert (ptrtype, adr);
1144 return exp;
1147 /* Look up component name in the structure type definition.
1149 If this component name is found indirectly within an anonymous union,
1150 store in *INDIRECT the component which directly contains
1151 that anonymous union. Otherwise, set *INDIRECT to 0. */
1153 static tree
1154 lookup_field (type, component, indirect)
1155 tree type, component;
1156 tree *indirect;
1158 tree field;
1160 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1161 to the field elements. Use a binary search on this array to quickly
1162 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1163 will always be set for structures which have many elements. */
1165 if (TYPE_LANG_SPECIFIC (type))
1167 int bot, top, half;
1168 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1170 field = TYPE_FIELDS (type);
1171 bot = 0;
1172 top = TYPE_LANG_SPECIFIC (type)->len;
1173 while (top - bot > 1)
1175 half = (top - bot + 1) >> 1;
1176 field = field_array[bot+half];
1178 if (DECL_NAME (field) == NULL_TREE)
1180 /* Step through all anon unions in linear fashion. */
1181 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1183 tree anon = 0, junk;
1185 field = field_array[bot++];
1186 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1187 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1188 anon = lookup_field (TREE_TYPE (field), component, &junk);
1190 if (anon != NULL_TREE)
1192 *indirect = field;
1193 return anon;
1197 /* Entire record is only anon unions. */
1198 if (bot > top)
1199 return NULL_TREE;
1201 /* Restart the binary search, with new lower bound. */
1202 continue;
1205 if (DECL_NAME (field) == component)
1206 break;
1207 if (DECL_NAME (field) < component)
1208 bot += half;
1209 else
1210 top = bot + half;
1213 if (DECL_NAME (field_array[bot]) == component)
1214 field = field_array[bot];
1215 else if (DECL_NAME (field) != component)
1216 field = 0;
1218 else
1220 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1222 if (DECL_NAME (field) == NULL_TREE)
1224 tree junk;
1225 tree anon = 0;
1227 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1228 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1229 anon = lookup_field (TREE_TYPE (field), component, &junk);
1231 if (anon != NULL_TREE)
1233 *indirect = field;
1234 return anon;
1238 if (DECL_NAME (field) == component)
1239 break;
1243 *indirect = NULL_TREE;
1244 return field;
1247 /* Make an expression to refer to the COMPONENT field of
1248 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1250 tree
1251 build_component_ref (datum, component)
1252 tree datum, component;
1254 register tree type = TREE_TYPE (datum);
1255 register enum tree_code code = TREE_CODE (type);
1256 register tree field = NULL;
1257 register tree ref;
1259 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1260 unless we are not to support things not strictly ANSI. */
1261 switch (TREE_CODE (datum))
1263 case COMPOUND_EXPR:
1265 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1266 return build (COMPOUND_EXPR, TREE_TYPE (value),
1267 TREE_OPERAND (datum, 0), value);
1269 case COND_EXPR:
1270 return build_conditional_expr
1271 (TREE_OPERAND (datum, 0),
1272 build_component_ref (TREE_OPERAND (datum, 1), component),
1273 build_component_ref (TREE_OPERAND (datum, 2), component));
1275 default:
1276 break;
1279 /* See if there is a field or component with name COMPONENT. */
1281 if (code == RECORD_TYPE || code == UNION_TYPE)
1283 tree indirect = 0;
1285 if (TYPE_SIZE (type) == 0)
1287 incomplete_type_error (NULL_TREE, type);
1288 return error_mark_node;
1291 field = lookup_field (type, component, &indirect);
1293 if (!field)
1295 error (code == RECORD_TYPE
1296 ? "structure has no member named `%s'"
1297 : "union has no member named `%s'",
1298 IDENTIFIER_POINTER (component));
1299 return error_mark_node;
1301 if (TREE_TYPE (field) == error_mark_node)
1302 return error_mark_node;
1304 /* If FIELD was found buried within an anonymous union,
1305 make one COMPONENT_REF to get that anonymous union,
1306 then fall thru to make a second COMPONENT_REF to get FIELD. */
1307 if (indirect != 0)
1309 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1310 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1311 TREE_READONLY (ref) = 1;
1312 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1313 TREE_THIS_VOLATILE (ref) = 1;
1314 datum = ref;
1317 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1319 if (TREE_READONLY (datum) || TREE_READONLY (field))
1320 TREE_READONLY (ref) = 1;
1321 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1322 TREE_THIS_VOLATILE (ref) = 1;
1324 return ref;
1326 else if (code != ERROR_MARK)
1327 error ("request for member `%s' in something not a structure or union",
1328 IDENTIFIER_POINTER (component));
1330 return error_mark_node;
1333 /* Given an expression PTR for a pointer, return an expression
1334 for the value pointed to.
1335 ERRORSTRING is the name of the operator to appear in error messages. */
1337 tree
1338 build_indirect_ref (ptr, errorstring)
1339 tree ptr;
1340 char *errorstring;
1342 register tree pointer = default_conversion (ptr);
1343 register tree type = TREE_TYPE (pointer);
1345 if (TREE_CODE (type) == POINTER_TYPE)
1347 if (TREE_CODE (pointer) == ADDR_EXPR
1348 && !flag_volatile
1349 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1350 == TREE_TYPE (type)))
1351 return TREE_OPERAND (pointer, 0);
1352 else
1354 tree t = TREE_TYPE (type);
1355 register tree ref = build1 (INDIRECT_REF,
1356 TYPE_MAIN_VARIANT (t), pointer);
1358 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1360 error ("dereferencing pointer to incomplete type");
1361 return error_mark_node;
1363 if (TREE_CODE (t) == VOID_TYPE && skip_evaluation == 0)
1364 warning ("dereferencing `void *' pointer");
1366 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1367 so that we get the proper error message if the result is used
1368 to assign to. Also, &* is supposed to be a no-op.
1369 And ANSI C seems to specify that the type of the result
1370 should be the const type. */
1371 /* A de-reference of a pointer to const is not a const. It is valid
1372 to change it via some other pointer. */
1373 TREE_READONLY (ref) = TYPE_READONLY (t);
1374 TREE_SIDE_EFFECTS (ref)
1375 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1376 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1377 return ref;
1380 else if (TREE_CODE (pointer) != ERROR_MARK)
1381 error ("invalid type argument of `%s'", errorstring);
1382 return error_mark_node;
1385 /* This handles expressions of the form "a[i]", which denotes
1386 an array reference.
1388 This is logically equivalent in C to *(a+i), but we may do it differently.
1389 If A is a variable or a member, we generate a primitive ARRAY_REF.
1390 This avoids forcing the array out of registers, and can work on
1391 arrays that are not lvalues (for example, members of structures returned
1392 by functions). */
1394 tree
1395 build_array_ref (array, index)
1396 tree array, index;
1398 if (index == 0)
1400 error ("subscript missing in array reference");
1401 return error_mark_node;
1404 if (TREE_TYPE (array) == error_mark_node
1405 || TREE_TYPE (index) == error_mark_node)
1406 return error_mark_node;
1408 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1409 && TREE_CODE (array) != INDIRECT_REF)
1411 tree rval, type;
1413 /* Subscripting with type char is likely to lose
1414 on a machine where chars are signed.
1415 So warn on any machine, but optionally.
1416 Don't warn for unsigned char since that type is safe.
1417 Don't warn for signed char because anyone who uses that
1418 must have done so deliberately. */
1419 if (warn_char_subscripts
1420 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1421 warning ("array subscript has type `char'");
1423 /* Apply default promotions *after* noticing character types. */
1424 index = default_conversion (index);
1426 /* Require integer *after* promotion, for sake of enums. */
1427 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1429 error ("array subscript is not an integer");
1430 return error_mark_node;
1433 /* An array that is indexed by a non-constant
1434 cannot be stored in a register; we must be able to do
1435 address arithmetic on its address.
1436 Likewise an array of elements of variable size. */
1437 if (TREE_CODE (index) != INTEGER_CST
1438 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1439 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1441 if (mark_addressable (array) == 0)
1442 return error_mark_node;
1444 /* An array that is indexed by a constant value which is not within
1445 the array bounds cannot be stored in a register either; because we
1446 would get a crash in store_bit_field/extract_bit_field when trying
1447 to access a non-existent part of the register. */
1448 if (TREE_CODE (index) == INTEGER_CST
1449 && TYPE_VALUES (TREE_TYPE (array))
1450 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1452 if (mark_addressable (array) == 0)
1453 return error_mark_node;
1456 if (pedantic && !lvalue_p (array))
1458 if (DECL_REGISTER (array))
1459 pedwarn ("ANSI C forbids subscripting `register' array");
1460 else
1461 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1464 if (pedantic)
1466 tree foo = array;
1467 while (TREE_CODE (foo) == COMPONENT_REF)
1468 foo = TREE_OPERAND (foo, 0);
1469 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1470 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1473 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1474 rval = build (ARRAY_REF, type, array, index);
1475 /* Array ref is const/volatile if the array elements are
1476 or if the array is. */
1477 TREE_READONLY (rval)
1478 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1479 | TREE_READONLY (array));
1480 TREE_SIDE_EFFECTS (rval)
1481 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1482 | TREE_SIDE_EFFECTS (array));
1483 TREE_THIS_VOLATILE (rval)
1484 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1485 /* This was added by rms on 16 Nov 91.
1486 It fixes vol struct foo *a; a->elts[1]
1487 in an inline function.
1488 Hope it doesn't break something else. */
1489 | TREE_THIS_VOLATILE (array));
1490 return require_complete_type (fold (rval));
1494 tree ar = default_conversion (array);
1495 tree ind = default_conversion (index);
1497 /* Do the same warning check as above, but only on the part that's
1498 syntactically the index and only if it is also semantically
1499 the index. */
1500 if (warn_char_subscripts
1501 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1502 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1503 warning ("subscript has type `char'");
1505 /* Put the integer in IND to simplify error checking. */
1506 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1508 tree temp = ar;
1509 ar = ind;
1510 ind = temp;
1513 if (ar == error_mark_node)
1514 return ar;
1516 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1517 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1519 error ("subscripted value is neither array nor pointer");
1520 return error_mark_node;
1522 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1524 error ("array subscript is not an integer");
1525 return error_mark_node;
1528 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1529 "array indexing");
1533 /* Build a function call to function FUNCTION with parameters PARAMS.
1534 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1535 TREE_VALUE of each node is a parameter-expression.
1536 FUNCTION's data type may be a function type or a pointer-to-function. */
1538 tree
1539 build_function_call (function, params)
1540 tree function, params;
1542 register tree fntype, fundecl = 0;
1543 register tree coerced_params;
1544 tree name = NULL_TREE, assembler_name = NULL_TREE;
1546 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1547 STRIP_TYPE_NOPS (function);
1549 /* Convert anything with function type to a pointer-to-function. */
1550 if (TREE_CODE (function) == FUNCTION_DECL)
1552 name = DECL_NAME (function);
1553 assembler_name = DECL_ASSEMBLER_NAME (function);
1555 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1556 (because calling an inline function does not mean the function
1557 needs to be separately compiled). */
1558 fntype = build_type_variant (TREE_TYPE (function),
1559 TREE_READONLY (function),
1560 TREE_THIS_VOLATILE (function));
1561 fundecl = function;
1562 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1564 else
1565 function = default_conversion (function);
1567 fntype = TREE_TYPE (function);
1569 if (TREE_CODE (fntype) == ERROR_MARK)
1570 return error_mark_node;
1572 if (!(TREE_CODE (fntype) == POINTER_TYPE
1573 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1575 error ("called object is not a function");
1576 return error_mark_node;
1579 /* fntype now gets the type of function pointed to. */
1580 fntype = TREE_TYPE (fntype);
1582 /* Convert the parameters to the types declared in the
1583 function prototype, or apply default promotions. */
1585 coerced_params
1586 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1588 /* Check for errors in format strings. */
1590 if (warn_format && (name || assembler_name))
1591 check_function_format (name, assembler_name, coerced_params);
1593 /* Recognize certain built-in functions so we can make tree-codes
1594 other than CALL_EXPR. We do this when it enables fold-const.c
1595 to do something useful. */
1597 if (TREE_CODE (function) == ADDR_EXPR
1598 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1599 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1600 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1602 case BUILT_IN_ABS:
1603 case BUILT_IN_LABS:
1604 case BUILT_IN_FABS:
1605 if (coerced_params == 0)
1606 return integer_zero_node;
1607 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1608 default:
1609 break;
1613 register tree result
1614 = build (CALL_EXPR, TREE_TYPE (fntype),
1615 function, coerced_params, NULL_TREE);
1617 TREE_SIDE_EFFECTS (result) = 1;
1618 if (TREE_TYPE (result) == void_type_node)
1619 return result;
1620 return require_complete_type (result);
1624 /* Convert the argument expressions in the list VALUES
1625 to the types in the list TYPELIST. The result is a list of converted
1626 argument expressions.
1628 If TYPELIST is exhausted, or when an element has NULL as its type,
1629 perform the default conversions.
1631 PARMLIST is the chain of parm decls for the function being called.
1632 It may be 0, if that info is not available.
1633 It is used only for generating error messages.
1635 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1637 This is also where warnings about wrong number of args are generated.
1639 Both VALUES and the returned value are chains of TREE_LIST nodes
1640 with the elements of the list in the TREE_VALUE slots of those nodes. */
1642 static tree
1643 convert_arguments (typelist, values, name, fundecl)
1644 tree typelist, values, name, fundecl;
1646 register tree typetail, valtail;
1647 register tree result = NULL;
1648 int parmnum;
1650 /* Scan the given expressions and types, producing individual
1651 converted arguments and pushing them on RESULT in reverse order. */
1653 for (valtail = values, typetail = typelist, parmnum = 0;
1654 valtail;
1655 valtail = TREE_CHAIN (valtail), parmnum++)
1657 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1658 register tree val = TREE_VALUE (valtail);
1660 if (type == void_type_node)
1662 if (name)
1663 error ("too many arguments to function `%s'",
1664 IDENTIFIER_POINTER (name));
1665 else
1666 error ("too many arguments to function");
1667 break;
1670 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1671 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1672 to convert automatically to a pointer. */
1673 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1674 val = TREE_OPERAND (val, 0);
1676 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1677 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1678 val = default_conversion (val);
1680 val = require_complete_type (val);
1682 if (type != 0)
1684 /* Formal parm type is specified by a function prototype. */
1685 tree parmval;
1687 if (TYPE_SIZE (type) == 0)
1689 error ("type of formal parameter %d is incomplete", parmnum + 1);
1690 parmval = val;
1692 else
1694 /* Optionally warn about conversions that
1695 differ from the default conversions. */
1696 if (warn_conversion)
1698 int formal_prec = TYPE_PRECISION (type);
1700 if (INTEGRAL_TYPE_P (type)
1701 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1702 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1703 else if (TREE_CODE (type) == COMPLEX_TYPE
1704 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1705 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1706 else if (TREE_CODE (type) == REAL_TYPE
1707 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1708 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1709 else if (TREE_CODE (type) == REAL_TYPE
1710 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1711 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1712 /* ??? At some point, messages should be written about
1713 conversions between complex types, but that's too messy
1714 to do now. */
1715 else if (TREE_CODE (type) == REAL_TYPE
1716 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1718 /* Warn if any argument is passed as `float',
1719 since without a prototype it would be `double'. */
1720 if (formal_prec == TYPE_PRECISION (float_type_node))
1721 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1723 /* Detect integer changing in width or signedness. */
1724 else if (INTEGRAL_TYPE_P (type)
1725 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1727 tree would_have_been = default_conversion (val);
1728 tree type1 = TREE_TYPE (would_have_been);
1730 if (TREE_CODE (type) == ENUMERAL_TYPE
1731 && type == TREE_TYPE (val))
1732 /* No warning if function asks for enum
1733 and the actual arg is that enum type. */
1735 else if (formal_prec != TYPE_PRECISION (type1))
1736 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1737 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1739 /* Don't complain if the formal parameter type
1740 is an enum, because we can't tell now whether
1741 the value was an enum--even the same enum. */
1742 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1744 else if (TREE_CODE (val) == INTEGER_CST
1745 && int_fits_type_p (val, type))
1746 /* Change in signedness doesn't matter
1747 if a constant value is unaffected. */
1749 /* Likewise for a constant in a NOP_EXPR. */
1750 else if (TREE_CODE (val) == NOP_EXPR
1751 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1752 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1754 #if 0 /* We never get such tree structure here. */
1755 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1756 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1757 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1758 /* Change in signedness doesn't matter
1759 if an enum value is unaffected. */
1761 #endif
1762 /* If the value is extended from a narrower
1763 unsigned type, it doesn't matter whether we
1764 pass it as signed or unsigned; the value
1765 certainly is the same either way. */
1766 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1767 && TREE_UNSIGNED (TREE_TYPE (val)))
1769 else if (TREE_UNSIGNED (type))
1770 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1771 else
1772 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1776 parmval = convert_for_assignment (type, val,
1777 (char *) 0, /* arg passing */
1778 fundecl, name, parmnum + 1);
1780 #ifdef PROMOTE_PROTOTYPES
1781 if ((TREE_CODE (type) == INTEGER_TYPE
1782 || TREE_CODE (type) == ENUMERAL_TYPE)
1783 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1784 parmval = default_conversion (parmval);
1785 #endif
1787 result = tree_cons (NULL_TREE, parmval, result);
1789 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1790 && (TYPE_PRECISION (TREE_TYPE (val))
1791 < TYPE_PRECISION (double_type_node)))
1792 /* Convert `float' to `double'. */
1793 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1794 else
1795 /* Convert `short' and `char' to full-size `int'. */
1796 result = tree_cons (NULL_TREE, default_conversion (val), result);
1798 if (typetail)
1799 typetail = TREE_CHAIN (typetail);
1802 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1804 if (name)
1805 error ("too few arguments to function `%s'",
1806 IDENTIFIER_POINTER (name));
1807 else
1808 error ("too few arguments to function");
1811 return nreverse (result);
1814 /* This is the entry point used by the parser
1815 for binary operators in the input.
1816 In addition to constructing the expression,
1817 we check for operands that were written with other binary operators
1818 in a way that is likely to confuse the user. */
1820 tree
1821 parser_build_binary_op (code, arg1, arg2)
1822 enum tree_code code;
1823 tree arg1, arg2;
1825 tree result = build_binary_op (code, arg1, arg2, 1);
1827 char class;
1828 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1829 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1830 enum tree_code code1 = ERROR_MARK;
1831 enum tree_code code2 = ERROR_MARK;
1833 if (class1 == 'e' || class1 == '1'
1834 || class1 == '2' || class1 == '<')
1835 code1 = C_EXP_ORIGINAL_CODE (arg1);
1836 if (class2 == 'e' || class2 == '1'
1837 || class2 == '2' || class2 == '<')
1838 code2 = C_EXP_ORIGINAL_CODE (arg2);
1840 /* Check for cases such as x+y<<z which users are likely
1841 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1842 is cleared to prevent these warnings. */
1843 if (warn_parentheses)
1845 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1847 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1848 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1849 warning ("suggest parentheses around + or - inside shift");
1852 if (code == TRUTH_ORIF_EXPR)
1854 if (code1 == TRUTH_ANDIF_EXPR
1855 || code2 == TRUTH_ANDIF_EXPR)
1856 warning ("suggest parentheses around && within ||");
1859 if (code == BIT_IOR_EXPR)
1861 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1862 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1863 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1864 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1865 warning ("suggest parentheses around arithmetic in operand of |");
1866 /* Check cases like x|y==z */
1867 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1868 warning ("suggest parentheses around comparison in operand of |");
1871 if (code == BIT_XOR_EXPR)
1873 if (code1 == BIT_AND_EXPR
1874 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1875 || code2 == BIT_AND_EXPR
1876 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1877 warning ("suggest parentheses around arithmetic in operand of ^");
1878 /* Check cases like x^y==z */
1879 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1880 warning ("suggest parentheses around comparison in operand of ^");
1883 if (code == BIT_AND_EXPR)
1885 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1886 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1887 warning ("suggest parentheses around + or - in operand of &");
1888 /* Check cases like x&y==z */
1889 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1890 warning ("suggest parentheses around comparison in operand of &");
1894 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1895 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1896 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1897 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1899 unsigned_conversion_warning (result, arg1);
1900 unsigned_conversion_warning (result, arg2);
1901 overflow_warning (result);
1903 class = TREE_CODE_CLASS (TREE_CODE (result));
1905 /* Record the code that was specified in the source,
1906 for the sake of warnings about confusing nesting. */
1907 if (class == 'e' || class == '1'
1908 || class == '2' || class == '<')
1909 C_SET_EXP_ORIGINAL_CODE (result, code);
1910 else
1912 int flag = TREE_CONSTANT (result);
1913 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1914 so that convert_for_assignment wouldn't strip it.
1915 That way, we got warnings for things like p = (1 - 1).
1916 But it turns out we should not get those warnings. */
1917 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1918 C_SET_EXP_ORIGINAL_CODE (result, code);
1919 TREE_CONSTANT (result) = flag;
1922 return result;
1925 /* Build a binary-operation expression without default conversions.
1926 CODE is the kind of expression to build.
1927 This function differs from `build' in several ways:
1928 the data type of the result is computed and recorded in it,
1929 warnings are generated if arg data types are invalid,
1930 special handling for addition and subtraction of pointers is known,
1931 and some optimization is done (operations on narrow ints
1932 are done in the narrower type when that gives the same result).
1933 Constant folding is also done before the result is returned.
1935 Note that the operands will never have enumeral types, or function
1936 or array types, because either they will have the default conversions
1937 performed or they have both just been converted to some other type in which
1938 the arithmetic is to be done. */
1940 tree
1941 build_binary_op (code, orig_op0, orig_op1, convert_p)
1942 enum tree_code code;
1943 tree orig_op0, orig_op1;
1944 int convert_p;
1946 tree type0, type1;
1947 register enum tree_code code0, code1;
1948 tree op0, op1;
1950 /* Expression code to give to the expression when it is built.
1951 Normally this is CODE, which is what the caller asked for,
1952 but in some special cases we change it. */
1953 register enum tree_code resultcode = code;
1955 /* Data type in which the computation is to be performed.
1956 In the simplest cases this is the common type of the arguments. */
1957 register tree result_type = NULL;
1959 /* Nonzero means operands have already been type-converted
1960 in whatever way is necessary.
1961 Zero means they need to be converted to RESULT_TYPE. */
1962 int converted = 0;
1964 /* Nonzero means create the expression with this type, rather than
1965 RESULT_TYPE. */
1966 tree build_type = 0;
1968 /* Nonzero means after finally constructing the expression
1969 convert it to this type. */
1970 tree final_type = 0;
1972 /* Nonzero if this is an operation like MIN or MAX which can
1973 safely be computed in short if both args are promoted shorts.
1974 Also implies COMMON.
1975 -1 indicates a bitwise operation; this makes a difference
1976 in the exact conditions for when it is safe to do the operation
1977 in a narrower mode. */
1978 int shorten = 0;
1980 /* Nonzero if this is a comparison operation;
1981 if both args are promoted shorts, compare the original shorts.
1982 Also implies COMMON. */
1983 int short_compare = 0;
1985 /* Nonzero if this is a right-shift operation, which can be computed on the
1986 original short and then promoted if the operand is a promoted short. */
1987 int short_shift = 0;
1989 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1990 int common = 0;
1992 if (convert_p)
1994 op0 = default_conversion (orig_op0);
1995 op1 = default_conversion (orig_op1);
1997 else
1999 op0 = orig_op0;
2000 op1 = orig_op1;
2003 type0 = TREE_TYPE (op0);
2004 type1 = TREE_TYPE (op1);
2006 /* The expression codes of the data types of the arguments tell us
2007 whether the arguments are integers, floating, pointers, etc. */
2008 code0 = TREE_CODE (type0);
2009 code1 = TREE_CODE (type1);
2011 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2012 STRIP_TYPE_NOPS (op0);
2013 STRIP_TYPE_NOPS (op1);
2015 /* If an error was already reported for one of the arguments,
2016 avoid reporting another error. */
2018 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2019 return error_mark_node;
2021 switch (code)
2023 case PLUS_EXPR:
2024 /* Handle the pointer + int case. */
2025 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2026 return pointer_int_sum (PLUS_EXPR, op0, op1);
2027 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2028 return pointer_int_sum (PLUS_EXPR, op1, op0);
2029 else
2030 common = 1;
2031 break;
2033 case MINUS_EXPR:
2034 /* Subtraction of two similar pointers.
2035 We must subtract them as integers, then divide by object size. */
2036 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2037 && comp_target_types (type0, type1))
2038 return pointer_diff (op0, op1);
2039 /* Handle pointer minus int. Just like pointer plus int. */
2040 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2041 return pointer_int_sum (MINUS_EXPR, op0, op1);
2042 else
2043 common = 1;
2044 break;
2046 case MULT_EXPR:
2047 common = 1;
2048 break;
2050 case TRUNC_DIV_EXPR:
2051 case CEIL_DIV_EXPR:
2052 case FLOOR_DIV_EXPR:
2053 case ROUND_DIV_EXPR:
2054 case EXACT_DIV_EXPR:
2055 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2056 || code0 == COMPLEX_TYPE)
2057 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2058 || code1 == COMPLEX_TYPE))
2060 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2061 resultcode = RDIV_EXPR;
2062 else
2064 /* Although it would be tempting to shorten always here, that
2065 loses on some targets, since the modulo instruction is
2066 undefined if the quotient can't be represented in the
2067 computation mode. We shorten only if unsigned or if
2068 dividing by something we know != -1. */
2069 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2070 || (TREE_CODE (op1) == INTEGER_CST
2071 && (TREE_INT_CST_LOW (op1) != -1
2072 || TREE_INT_CST_HIGH (op1) != -1)));
2074 common = 1;
2076 break;
2078 case BIT_AND_EXPR:
2079 case BIT_ANDTC_EXPR:
2080 case BIT_IOR_EXPR:
2081 case BIT_XOR_EXPR:
2082 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2083 shorten = -1;
2084 /* If one operand is a constant, and the other is a short type
2085 that has been converted to an int,
2086 really do the work in the short type and then convert the
2087 result to int. If we are lucky, the constant will be 0 or 1
2088 in the short type, making the entire operation go away. */
2089 if (TREE_CODE (op0) == INTEGER_CST
2090 && TREE_CODE (op1) == NOP_EXPR
2091 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2092 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2094 final_type = result_type;
2095 op1 = TREE_OPERAND (op1, 0);
2096 result_type = TREE_TYPE (op1);
2098 if (TREE_CODE (op1) == INTEGER_CST
2099 && TREE_CODE (op0) == NOP_EXPR
2100 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2101 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2103 final_type = result_type;
2104 op0 = TREE_OPERAND (op0, 0);
2105 result_type = TREE_TYPE (op0);
2107 break;
2109 case TRUNC_MOD_EXPR:
2110 case FLOOR_MOD_EXPR:
2111 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2113 /* Although it would be tempting to shorten always here, that loses
2114 on some targets, since the modulo instruction is undefined if the
2115 quotient can't be represented in the computation mode. We shorten
2116 only if unsigned or if dividing by something we know != -1. */
2117 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2118 || (TREE_CODE (op1) == INTEGER_CST
2119 && (TREE_INT_CST_LOW (op1) != -1
2120 || TREE_INT_CST_HIGH (op1) != -1)));
2121 common = 1;
2123 break;
2125 case TRUTH_ANDIF_EXPR:
2126 case TRUTH_ORIF_EXPR:
2127 case TRUTH_AND_EXPR:
2128 case TRUTH_OR_EXPR:
2129 case TRUTH_XOR_EXPR:
2130 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2131 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2132 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2133 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2135 /* Result of these operations is always an int,
2136 but that does not mean the operands should be
2137 converted to ints! */
2138 result_type = integer_type_node;
2139 op0 = truthvalue_conversion (op0);
2140 op1 = truthvalue_conversion (op1);
2141 converted = 1;
2143 break;
2145 /* Shift operations: result has same type as first operand;
2146 always convert second operand to int.
2147 Also set SHORT_SHIFT if shifting rightward. */
2149 case RSHIFT_EXPR:
2150 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2152 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2154 if (tree_int_cst_sgn (op1) < 0)
2155 warning ("right shift count is negative");
2156 else
2158 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2159 short_shift = 1;
2160 if (TREE_INT_CST_HIGH (op1) != 0
2161 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2162 >= TYPE_PRECISION (type0)))
2163 warning ("right shift count >= width of type");
2166 /* Use the type of the value to be shifted.
2167 This is what most traditional C compilers do. */
2168 result_type = type0;
2169 /* Unless traditional, convert the shift-count to an integer,
2170 regardless of size of value being shifted. */
2171 if (! flag_traditional)
2173 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2174 op1 = convert (integer_type_node, op1);
2175 /* Avoid converting op1 to result_type later. */
2176 converted = 1;
2179 break;
2181 case LSHIFT_EXPR:
2182 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2184 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2186 if (tree_int_cst_sgn (op1) < 0)
2187 warning ("left shift count is negative");
2188 else if (TREE_INT_CST_HIGH (op1) != 0
2189 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2190 >= TYPE_PRECISION (type0)))
2191 warning ("left shift count >= width of type");
2193 /* Use the type of the value to be shifted.
2194 This is what most traditional C compilers do. */
2195 result_type = type0;
2196 /* Unless traditional, convert the shift-count to an integer,
2197 regardless of size of value being shifted. */
2198 if (! flag_traditional)
2200 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2201 op1 = convert (integer_type_node, op1);
2202 /* Avoid converting op1 to result_type later. */
2203 converted = 1;
2206 break;
2208 case RROTATE_EXPR:
2209 case LROTATE_EXPR:
2210 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2212 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2214 if (tree_int_cst_sgn (op1) < 0)
2215 warning ("shift count is negative");
2216 else if (TREE_INT_CST_HIGH (op1) != 0
2217 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2218 >= TYPE_PRECISION (type0)))
2219 warning ("shift count >= width of type");
2221 /* Use the type of the value to be shifted.
2222 This is what most traditional C compilers do. */
2223 result_type = type0;
2224 /* Unless traditional, convert the shift-count to an integer,
2225 regardless of size of value being shifted. */
2226 if (! flag_traditional)
2228 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2229 op1 = convert (integer_type_node, op1);
2230 /* Avoid converting op1 to result_type later. */
2231 converted = 1;
2234 break;
2236 case EQ_EXPR:
2237 case NE_EXPR:
2238 /* Result of comparison is always int,
2239 but don't convert the args to int! */
2240 build_type = integer_type_node;
2241 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2242 || code0 == COMPLEX_TYPE)
2243 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2244 || code1 == COMPLEX_TYPE))
2245 short_compare = 1;
2246 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2248 register tree tt0 = TREE_TYPE (type0);
2249 register tree tt1 = TREE_TYPE (type1);
2250 /* Anything compares with void *. void * compares with anything.
2251 Otherwise, the targets must be compatible
2252 and both must be object or both incomplete. */
2253 if (comp_target_types (type0, type1))
2254 result_type = common_type (type0, type1);
2255 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2257 /* op0 != orig_op0 detects the case of something
2258 whose value is 0 but which isn't a valid null ptr const. */
2259 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2260 && TREE_CODE (tt1) == FUNCTION_TYPE)
2261 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2263 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2265 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2266 && TREE_CODE (tt0) == FUNCTION_TYPE)
2267 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2269 else
2270 pedwarn ("comparison of distinct pointer types lacks a cast");
2272 if (result_type == NULL_TREE)
2273 result_type = ptr_type_node;
2275 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2276 && integer_zerop (op1))
2277 result_type = type0;
2278 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2279 && integer_zerop (op0))
2280 result_type = type1;
2281 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2283 result_type = type0;
2284 if (! flag_traditional)
2285 pedwarn ("comparison between pointer and integer");
2287 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2289 result_type = type1;
2290 if (! flag_traditional)
2291 pedwarn ("comparison between pointer and integer");
2293 break;
2295 case MAX_EXPR:
2296 case MIN_EXPR:
2297 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2298 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2299 shorten = 1;
2300 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2302 if (comp_target_types (type0, type1))
2304 result_type = common_type (type0, type1);
2305 if (pedantic
2306 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2307 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2309 else
2311 result_type = ptr_type_node;
2312 pedwarn ("comparison of distinct pointer types lacks a cast");
2315 break;
2317 case LE_EXPR:
2318 case GE_EXPR:
2319 case LT_EXPR:
2320 case GT_EXPR:
2321 build_type = integer_type_node;
2322 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2323 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2324 short_compare = 1;
2325 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2327 if (comp_target_types (type0, type1))
2329 result_type = common_type (type0, type1);
2330 if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2331 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2332 pedwarn ("comparison of complete and incomplete pointers");
2333 else if (pedantic
2334 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2335 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2337 else
2339 result_type = ptr_type_node;
2340 pedwarn ("comparison of distinct pointer types lacks a cast");
2343 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2344 && integer_zerop (op1))
2346 result_type = type0;
2347 if (pedantic || extra_warnings)
2348 pedwarn ("ordered comparison of pointer with integer zero");
2350 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2351 && integer_zerop (op0))
2353 result_type = type1;
2354 if (pedantic)
2355 pedwarn ("ordered comparison of pointer with integer zero");
2357 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2359 result_type = type0;
2360 if (! flag_traditional)
2361 pedwarn ("comparison between pointer and integer");
2363 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2365 result_type = type1;
2366 if (! flag_traditional)
2367 pedwarn ("comparison between pointer and integer");
2369 break;
2371 default:
2372 break;
2375 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2377 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2379 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2381 if (shorten || common || short_compare)
2382 result_type = common_type (type0, type1);
2384 /* For certain operations (which identify themselves by shorten != 0)
2385 if both args were extended from the same smaller type,
2386 do the arithmetic in that type and then extend.
2388 shorten !=0 and !=1 indicates a bitwise operation.
2389 For them, this optimization is safe only if
2390 both args are zero-extended or both are sign-extended.
2391 Otherwise, we might change the result.
2392 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2393 but calculated in (unsigned short) it would be (unsigned short)-1. */
2395 if (shorten && none_complex)
2397 int unsigned0, unsigned1;
2398 tree arg0 = get_narrower (op0, &unsigned0);
2399 tree arg1 = get_narrower (op1, &unsigned1);
2400 /* UNS is 1 if the operation to be done is an unsigned one. */
2401 int uns = TREE_UNSIGNED (result_type);
2402 tree type;
2404 final_type = result_type;
2406 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2407 but it *requires* conversion to FINAL_TYPE. */
2409 if ((TYPE_PRECISION (TREE_TYPE (op0))
2410 == TYPE_PRECISION (TREE_TYPE (arg0)))
2411 && TREE_TYPE (op0) != final_type)
2412 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2413 if ((TYPE_PRECISION (TREE_TYPE (op1))
2414 == TYPE_PRECISION (TREE_TYPE (arg1)))
2415 && TREE_TYPE (op1) != final_type)
2416 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2418 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2420 /* For bitwise operations, signedness of nominal type
2421 does not matter. Consider only how operands were extended. */
2422 if (shorten == -1)
2423 uns = unsigned0;
2425 /* Note that in all three cases below we refrain from optimizing
2426 an unsigned operation on sign-extended args.
2427 That would not be valid. */
2429 /* Both args variable: if both extended in same way
2430 from same width, do it in that width.
2431 Do it unsigned if args were zero-extended. */
2432 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2433 < TYPE_PRECISION (result_type))
2434 && (TYPE_PRECISION (TREE_TYPE (arg1))
2435 == TYPE_PRECISION (TREE_TYPE (arg0)))
2436 && unsigned0 == unsigned1
2437 && (unsigned0 || !uns))
2438 result_type
2439 = signed_or_unsigned_type (unsigned0,
2440 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2441 else if (TREE_CODE (arg0) == INTEGER_CST
2442 && (unsigned1 || !uns)
2443 && (TYPE_PRECISION (TREE_TYPE (arg1))
2444 < TYPE_PRECISION (result_type))
2445 && (type = signed_or_unsigned_type (unsigned1,
2446 TREE_TYPE (arg1)),
2447 int_fits_type_p (arg0, type)))
2448 result_type = type;
2449 else if (TREE_CODE (arg1) == INTEGER_CST
2450 && (unsigned0 || !uns)
2451 && (TYPE_PRECISION (TREE_TYPE (arg0))
2452 < TYPE_PRECISION (result_type))
2453 && (type = signed_or_unsigned_type (unsigned0,
2454 TREE_TYPE (arg0)),
2455 int_fits_type_p (arg1, type)))
2456 result_type = type;
2459 /* Shifts can be shortened if shifting right. */
2461 if (short_shift)
2463 int unsigned_arg;
2464 tree arg0 = get_narrower (op0, &unsigned_arg);
2466 final_type = result_type;
2468 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2469 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2471 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2472 /* We can shorten only if the shift count is less than the
2473 number of bits in the smaller type size. */
2474 && TREE_INT_CST_HIGH (op1) == 0
2475 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
2476 /* If arg is sign-extended and then unsigned-shifted,
2477 we can simulate this with a signed shift in arg's type
2478 only if the extended result is at least twice as wide
2479 as the arg. Otherwise, the shift could use up all the
2480 ones made by sign-extension and bring in zeros.
2481 We can't optimize that case at all, but in most machines
2482 it never happens because available widths are 2**N. */
2483 && (!TREE_UNSIGNED (final_type)
2484 || unsigned_arg
2485 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2487 /* Do an unsigned shift if the operand was zero-extended. */
2488 result_type
2489 = signed_or_unsigned_type (unsigned_arg,
2490 TREE_TYPE (arg0));
2491 /* Convert value-to-be-shifted to that type. */
2492 if (TREE_TYPE (op0) != result_type)
2493 op0 = convert (result_type, op0);
2494 converted = 1;
2498 /* Comparison operations are shortened too but differently.
2499 They identify themselves by setting short_compare = 1. */
2501 if (short_compare)
2503 /* Don't write &op0, etc., because that would prevent op0
2504 from being kept in a register.
2505 Instead, make copies of the our local variables and
2506 pass the copies by reference, then copy them back afterward. */
2507 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2508 enum tree_code xresultcode = resultcode;
2509 tree val
2510 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2511 if (val != 0)
2512 return val;
2513 op0 = xop0, op1 = xop1;
2514 converted = 1;
2515 resultcode = xresultcode;
2517 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2518 && skip_evaluation == 0)
2520 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2521 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2523 int unsignedp0, unsignedp1;
2524 tree primop0 = get_narrower (op0, &unsignedp0);
2525 tree primop1 = get_narrower (op1, &unsignedp1);
2527 /* Avoid spurious warnings for comparison with enumerators. */
2529 xop0 = orig_op0;
2530 xop1 = orig_op1;
2531 STRIP_TYPE_NOPS (xop0);
2532 STRIP_TYPE_NOPS (xop1);
2534 /* Give warnings for comparisons between signed and unsigned
2535 quantities that may fail. */
2536 /* Do the checking based on the original operand trees, so that
2537 casts will be considered, but default promotions won't be. */
2539 /* Do not warn if the comparison is being done in a signed type,
2540 since the signed type will only be chosen if it can represent
2541 all the values of the unsigned type. */
2542 if (! TREE_UNSIGNED (result_type))
2543 /* OK */;
2544 /* Do not warn if both operands are unsigned. */
2545 else if (op0_signed == op1_signed)
2546 /* OK */;
2547 /* Do not warn if the signed quantity is an unsuffixed
2548 integer literal (or some static constant expression
2549 involving such literals) and it is non-negative. */
2550 else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST
2551 && tree_int_cst_sgn (xop0) >= 0)
2552 || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
2553 && tree_int_cst_sgn (xop1) >= 0))
2554 /* OK */;
2555 /* Do not warn if the comparison is an equality operation,
2556 the unsigned quantity is an integral constant and it does
2557 not use the most significant bit of result_type. */
2558 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
2559 && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST
2560 && int_fits_type_p (xop1, signed_type (result_type)))
2561 || (op1_signed && TREE_CODE (xop0) == INTEGER_CST
2562 && int_fits_type_p (xop0, signed_type (result_type)))))
2563 /* OK */;
2564 else
2565 warning ("comparison between signed and unsigned");
2567 /* Warn if two unsigned values are being compared in a size
2568 larger than their original size, and one (and only one) is the
2569 result of a `~' operator. This comparison will always fail.
2571 Also warn if one operand is a constant, and the constant
2572 does not have all bits set that are set in the ~ operand
2573 when it is extended. */
2575 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2576 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2578 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2579 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2580 &unsignedp0);
2581 else
2582 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2583 &unsignedp1);
2585 if (TREE_CODE (primop0) == INTEGER_CST
2586 || TREE_CODE (primop1) == INTEGER_CST)
2588 tree primop;
2589 long constant, mask;
2590 int unsignedp, bits;
2592 if (TREE_CODE (primop0) == INTEGER_CST)
2594 primop = primop1;
2595 unsignedp = unsignedp1;
2596 constant = TREE_INT_CST_LOW (primop0);
2598 else
2600 primop = primop0;
2601 unsignedp = unsignedp0;
2602 constant = TREE_INT_CST_LOW (primop1);
2605 bits = TYPE_PRECISION (TREE_TYPE (primop));
2606 if (bits < TYPE_PRECISION (result_type)
2607 && bits < HOST_BITS_PER_LONG && unsignedp)
2609 mask = (~0L) << bits;
2610 if ((mask & constant) != mask)
2611 warning ("comparison of promoted ~unsigned with constant");
2614 else if (unsignedp0 && unsignedp1
2615 && (TYPE_PRECISION (TREE_TYPE (primop0))
2616 < TYPE_PRECISION (result_type))
2617 && (TYPE_PRECISION (TREE_TYPE (primop1))
2618 < TYPE_PRECISION (result_type)))
2619 warning ("comparison of promoted ~unsigned with unsigned");
2625 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2626 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2627 Then the expression will be built.
2628 It will be given type FINAL_TYPE if that is nonzero;
2629 otherwise, it will be given type RESULT_TYPE. */
2631 if (!result_type)
2633 binary_op_error (code);
2634 return error_mark_node;
2637 if (! converted)
2639 if (TREE_TYPE (op0) != result_type)
2640 op0 = convert (result_type, op0);
2641 if (TREE_TYPE (op1) != result_type)
2642 op1 = convert (result_type, op1);
2645 if (build_type == NULL_TREE)
2646 build_type = result_type;
2649 register tree result = build (resultcode, build_type, op0, op1);
2650 register tree folded;
2652 folded = fold (result);
2653 if (folded == result)
2654 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2655 if (final_type != 0)
2656 return convert (final_type, folded);
2657 return folded;
2661 /* Return a tree for the sum or difference (RESULTCODE says which)
2662 of pointer PTROP and integer INTOP. */
2664 static tree
2665 pointer_int_sum (resultcode, ptrop, intop)
2666 enum tree_code resultcode;
2667 register tree ptrop, intop;
2669 tree size_exp;
2671 register tree result;
2672 register tree folded;
2674 /* The result is a pointer of the same type that is being added. */
2676 register tree result_type = TREE_TYPE (ptrop);
2678 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2680 if (pedantic || warn_pointer_arith)
2681 pedwarn ("pointer of type `void *' used in arithmetic");
2682 size_exp = integer_one_node;
2684 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2686 if (pedantic || warn_pointer_arith)
2687 pedwarn ("pointer to a function used in arithmetic");
2688 size_exp = integer_one_node;
2690 else
2691 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2693 /* If what we are about to multiply by the size of the elements
2694 contains a constant term, apply distributive law
2695 and multiply that constant term separately.
2696 This helps produce common subexpressions. */
2698 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2699 && ! TREE_CONSTANT (intop)
2700 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2701 && TREE_CONSTANT (size_exp)
2702 /* If the constant comes from pointer subtraction,
2703 skip this optimization--it would cause an error. */
2704 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2705 /* If the constant is unsigned, and smaller than the pointer size,
2706 then we must skip this optimization. This is because it could cause
2707 an overflow error if the constant is negative but INTOP is not. */
2708 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2709 || (TYPE_PRECISION (TREE_TYPE (intop))
2710 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2712 enum tree_code subcode = resultcode;
2713 tree int_type = TREE_TYPE (intop);
2714 if (TREE_CODE (intop) == MINUS_EXPR)
2715 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2716 /* Convert both subexpression types to the type of intop,
2717 because weird cases involving pointer arithmetic
2718 can result in a sum or difference with different type args. */
2719 ptrop = build_binary_op (subcode, ptrop,
2720 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2721 intop = convert (int_type, TREE_OPERAND (intop, 0));
2724 /* Convert the integer argument to a type the same size as sizetype
2725 so the multiply won't overflow spuriously. */
2727 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2728 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2729 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2730 TREE_UNSIGNED (sizetype)), intop);
2732 /* Replace the integer argument with a suitable product by the object size.
2733 Do this multiplication as signed, then convert to the appropriate
2734 pointer type (actually unsigned integral). */
2736 intop = convert (result_type,
2737 build_binary_op (MULT_EXPR, intop,
2738 convert (TREE_TYPE (intop), size_exp), 1));
2740 /* Create the sum or difference. */
2742 result = build (resultcode, result_type, ptrop, intop);
2744 folded = fold (result);
2745 if (folded == result)
2746 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2747 return folded;
2750 /* Return a tree for the difference of pointers OP0 and OP1.
2751 The resulting tree has type int. */
2753 static tree
2754 pointer_diff (op0, op1)
2755 register tree op0, op1;
2757 register tree result, folded;
2758 tree restype = ptrdiff_type_node;
2760 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2762 if (pedantic || warn_pointer_arith)
2764 if (TREE_CODE (target_type) == VOID_TYPE)
2765 pedwarn ("pointer of type `void *' used in subtraction");
2766 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2767 pedwarn ("pointer to a function used in subtraction");
2770 /* First do the subtraction as integers;
2771 then drop through to build the divide operator.
2772 Do not do default conversions on the minus operator
2773 in case restype is a short type. */
2775 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2776 convert (restype, op1), 0);
2777 /* This generates an error if op1 is pointer to incomplete type. */
2778 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2779 error ("arithmetic on pointer to an incomplete type");
2781 /* This generates an error if op0 is pointer to incomplete type. */
2782 op1 = c_size_in_bytes (target_type);
2784 /* Divide by the size, in easiest possible way. */
2786 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2788 folded = fold (result);
2789 if (folded == result)
2790 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2791 return folded;
2794 /* Construct and perhaps optimize a tree representation
2795 for a unary operation. CODE, a tree_code, specifies the operation
2796 and XARG is the operand. NOCONVERT nonzero suppresses
2797 the default promotions (such as from short to int). */
2799 tree
2800 build_unary_op (code, xarg, noconvert)
2801 enum tree_code code;
2802 tree xarg;
2803 int noconvert;
2805 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2806 register tree arg = xarg;
2807 register tree argtype = 0;
2808 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2809 char *errstring = NULL;
2810 tree val;
2812 if (typecode == ERROR_MARK)
2813 return error_mark_node;
2814 if (typecode == ENUMERAL_TYPE)
2815 typecode = INTEGER_TYPE;
2817 switch (code)
2819 case CONVERT_EXPR:
2820 /* This is used for unary plus, because a CONVERT_EXPR
2821 is enough to prevent anybody from looking inside for
2822 associativity, but won't generate any code. */
2823 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2824 || typecode == COMPLEX_TYPE))
2825 errstring = "wrong type argument to unary plus";
2826 else if (!noconvert)
2827 arg = default_conversion (arg);
2828 break;
2830 case NEGATE_EXPR:
2831 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2832 || typecode == COMPLEX_TYPE))
2833 errstring = "wrong type argument to unary minus";
2834 else if (!noconvert)
2835 arg = default_conversion (arg);
2836 break;
2838 case BIT_NOT_EXPR:
2839 if (typecode == COMPLEX_TYPE)
2841 code = CONJ_EXPR;
2842 if (!noconvert)
2843 arg = default_conversion (arg);
2845 else if (typecode != INTEGER_TYPE)
2846 errstring = "wrong type argument to bit-complement";
2847 else if (!noconvert)
2848 arg = default_conversion (arg);
2849 break;
2851 case ABS_EXPR:
2852 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2853 || typecode == COMPLEX_TYPE))
2854 errstring = "wrong type argument to abs";
2855 else if (!noconvert)
2856 arg = default_conversion (arg);
2857 break;
2859 case CONJ_EXPR:
2860 /* Conjugating a real value is a no-op, but allow it anyway. */
2861 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2862 || typecode == COMPLEX_TYPE))
2863 errstring = "wrong type argument to conjugation";
2864 else if (!noconvert)
2865 arg = default_conversion (arg);
2866 break;
2868 case TRUTH_NOT_EXPR:
2869 if (typecode != INTEGER_TYPE
2870 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2871 && typecode != COMPLEX_TYPE
2872 /* These will convert to a pointer. */
2873 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2875 errstring = "wrong type argument to unary exclamation mark";
2876 break;
2878 arg = truthvalue_conversion (arg);
2879 return invert_truthvalue (arg);
2881 case NOP_EXPR:
2882 break;
2884 case REALPART_EXPR:
2885 if (TREE_CODE (arg) == COMPLEX_CST)
2886 return TREE_REALPART (arg);
2887 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2888 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2889 else
2890 return arg;
2892 case IMAGPART_EXPR:
2893 if (TREE_CODE (arg) == COMPLEX_CST)
2894 return TREE_IMAGPART (arg);
2895 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2896 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2897 else
2898 return convert (TREE_TYPE (arg), integer_zero_node);
2900 case PREINCREMENT_EXPR:
2901 case POSTINCREMENT_EXPR:
2902 case PREDECREMENT_EXPR:
2903 case POSTDECREMENT_EXPR:
2904 /* Handle complex lvalues (when permitted)
2905 by reduction to simpler cases. */
2907 val = unary_complex_lvalue (code, arg);
2908 if (val != 0)
2909 return val;
2911 /* Increment or decrement the real part of the value,
2912 and don't change the imaginary part. */
2913 if (typecode == COMPLEX_TYPE)
2915 tree real, imag;
2917 arg = stabilize_reference (arg);
2918 real = build_unary_op (REALPART_EXPR, arg, 1);
2919 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2920 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2921 build_unary_op (code, real, 1), imag);
2924 /* Report invalid types. */
2926 if (typecode != POINTER_TYPE
2927 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2929 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2930 errstring ="wrong type argument to increment";
2931 else
2932 errstring ="wrong type argument to decrement";
2933 break;
2937 register tree inc;
2938 tree result_type = TREE_TYPE (arg);
2940 arg = get_unwidened (arg, 0);
2941 argtype = TREE_TYPE (arg);
2943 /* Compute the increment. */
2945 if (typecode == POINTER_TYPE)
2947 /* If pointer target is an undefined struct,
2948 we just cannot know how to do the arithmetic. */
2949 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2950 error ("%s of pointer to unknown structure",
2951 ((code == PREINCREMENT_EXPR
2952 || code == POSTINCREMENT_EXPR)
2953 ? "increment" : "decrement"));
2954 else if ((pedantic || warn_pointer_arith)
2955 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2956 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2957 pedwarn ("wrong type argument to %s",
2958 ((code == PREINCREMENT_EXPR
2959 || code == POSTINCREMENT_EXPR)
2960 ? "increment" : "decrement"));
2961 inc = c_size_in_bytes (TREE_TYPE (result_type));
2963 else
2964 inc = integer_one_node;
2966 inc = convert (argtype, inc);
2968 /* Handle incrementing a cast-expression. */
2970 while (1)
2971 switch (TREE_CODE (arg))
2973 case NOP_EXPR:
2974 case CONVERT_EXPR:
2975 case FLOAT_EXPR:
2976 case FIX_TRUNC_EXPR:
2977 case FIX_FLOOR_EXPR:
2978 case FIX_ROUND_EXPR:
2979 case FIX_CEIL_EXPR:
2980 pedantic_lvalue_warning (CONVERT_EXPR);
2981 /* If the real type has the same machine representation
2982 as the type it is cast to, we can make better output
2983 by adding directly to the inside of the cast. */
2984 if ((TREE_CODE (TREE_TYPE (arg))
2985 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2986 && (TYPE_MODE (TREE_TYPE (arg))
2987 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2988 arg = TREE_OPERAND (arg, 0);
2989 else
2991 tree incremented, modify, value;
2992 arg = stabilize_reference (arg);
2993 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2994 value = arg;
2995 else
2996 value = save_expr (arg);
2997 incremented = build (((code == PREINCREMENT_EXPR
2998 || code == POSTINCREMENT_EXPR)
2999 ? PLUS_EXPR : MINUS_EXPR),
3000 argtype, value, inc);
3001 TREE_SIDE_EFFECTS (incremented) = 1;
3002 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3003 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3004 TREE_USED (value) = 1;
3005 return value;
3007 break;
3009 default:
3010 goto give_up;
3012 give_up:
3014 /* Complain about anything else that is not a true lvalue. */
3015 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3016 || code == POSTINCREMENT_EXPR)
3017 ? "increment" : "decrement")))
3018 return error_mark_node;
3020 /* Report a read-only lvalue. */
3021 if (TREE_READONLY (arg))
3022 readonly_warning (arg,
3023 ((code == PREINCREMENT_EXPR
3024 || code == POSTINCREMENT_EXPR)
3025 ? "increment" : "decrement"));
3027 val = build (code, TREE_TYPE (arg), arg, inc);
3028 TREE_SIDE_EFFECTS (val) = 1;
3029 val = convert (result_type, val);
3030 if (TREE_CODE (val) != code)
3031 TREE_NO_UNUSED_WARNING (val) = 1;
3032 return val;
3035 case ADDR_EXPR:
3036 /* Note that this operation never does default_conversion
3037 regardless of NOCONVERT. */
3039 /* Let &* cancel out to simplify resulting code. */
3040 if (TREE_CODE (arg) == INDIRECT_REF)
3042 /* Don't let this be an lvalue. */
3043 if (lvalue_p (TREE_OPERAND (arg, 0)))
3044 return non_lvalue (TREE_OPERAND (arg, 0));
3045 return TREE_OPERAND (arg, 0);
3048 /* For &x[y], return x+y */
3049 if (TREE_CODE (arg) == ARRAY_REF)
3051 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
3052 return error_mark_node;
3053 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3054 TREE_OPERAND (arg, 1), 1);
3057 /* Handle complex lvalues (when permitted)
3058 by reduction to simpler cases. */
3059 val = unary_complex_lvalue (code, arg);
3060 if (val != 0)
3061 return val;
3063 #if 0 /* Turned off because inconsistent;
3064 float f; *&(int)f = 3.4 stores in int format
3065 whereas (int)f = 3.4 stores in float format. */
3066 /* Address of a cast is just a cast of the address
3067 of the operand of the cast. */
3068 switch (TREE_CODE (arg))
3070 case NOP_EXPR:
3071 case CONVERT_EXPR:
3072 case FLOAT_EXPR:
3073 case FIX_TRUNC_EXPR:
3074 case FIX_FLOOR_EXPR:
3075 case FIX_ROUND_EXPR:
3076 case FIX_CEIL_EXPR:
3077 if (pedantic)
3078 pedwarn ("ANSI C forbids the address of a cast expression");
3079 return convert (build_pointer_type (TREE_TYPE (arg)),
3080 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3081 0));
3083 #endif
3085 /* Allow the address of a constructor if all the elements
3086 are constant. */
3087 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3089 /* Anything not already handled and not a true memory reference
3090 is an error. */
3091 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
3092 return error_mark_node;
3094 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3095 argtype = TREE_TYPE (arg);
3096 /* If the lvalue is const or volatile,
3097 merge that into the type that the address will point to. */
3098 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
3099 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3101 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3102 argtype = c_build_type_variant (argtype,
3103 TREE_READONLY (arg),
3104 TREE_THIS_VOLATILE (arg));
3107 argtype = build_pointer_type (argtype);
3109 if (mark_addressable (arg) == 0)
3110 return error_mark_node;
3113 tree addr;
3115 if (TREE_CODE (arg) == COMPONENT_REF)
3117 tree field = TREE_OPERAND (arg, 1);
3119 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3121 if (DECL_C_BIT_FIELD (field))
3123 error ("attempt to take address of bit-field structure member `%s'",
3124 IDENTIFIER_POINTER (DECL_NAME (field)));
3125 return error_mark_node;
3128 addr = convert (argtype, addr);
3130 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3132 tree offset
3133 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3134 size_int (BITS_PER_UNIT));
3135 int flag = TREE_CONSTANT (addr);
3136 addr = fold (build (PLUS_EXPR, argtype,
3137 addr, convert (argtype, offset)));
3138 TREE_CONSTANT (addr) = flag;
3141 else
3142 addr = build1 (code, argtype, arg);
3144 /* Address of a static or external variable or
3145 file-scope function counts as a constant. */
3146 if (staticp (arg)
3147 && ! (TREE_CODE (arg) == FUNCTION_DECL
3148 && DECL_CONTEXT (arg) != 0))
3149 TREE_CONSTANT (addr) = 1;
3150 return addr;
3153 default:
3154 break;
3157 if (!errstring)
3159 if (argtype == 0)
3160 argtype = TREE_TYPE (arg);
3161 return fold (build1 (code, argtype, arg));
3164 error (errstring);
3165 return error_mark_node;
3168 #if 0
3169 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3170 convert ARG with the same conversions in the same order
3171 and return the result. */
3173 static tree
3174 convert_sequence (conversions, arg)
3175 tree conversions;
3176 tree arg;
3178 switch (TREE_CODE (conversions))
3180 case NOP_EXPR:
3181 case CONVERT_EXPR:
3182 case FLOAT_EXPR:
3183 case FIX_TRUNC_EXPR:
3184 case FIX_FLOOR_EXPR:
3185 case FIX_ROUND_EXPR:
3186 case FIX_CEIL_EXPR:
3187 return convert (TREE_TYPE (conversions),
3188 convert_sequence (TREE_OPERAND (conversions, 0),
3189 arg));
3191 default:
3192 return arg;
3195 #endif /* 0 */
3197 /* Return nonzero if REF is an lvalue valid for this language.
3198 Lvalues can be assigned, unless their type has TYPE_READONLY.
3199 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3202 lvalue_p (ref)
3203 tree ref;
3205 register enum tree_code code = TREE_CODE (ref);
3207 switch (code)
3209 case REALPART_EXPR:
3210 case IMAGPART_EXPR:
3211 case COMPONENT_REF:
3212 return lvalue_p (TREE_OPERAND (ref, 0));
3214 case STRING_CST:
3215 return 1;
3217 case INDIRECT_REF:
3218 case ARRAY_REF:
3219 case VAR_DECL:
3220 case PARM_DECL:
3221 case RESULT_DECL:
3222 case ERROR_MARK:
3223 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3224 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3226 case BIND_EXPR:
3227 case RTL_EXPR:
3228 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3230 default:
3231 return 0;
3235 /* Return nonzero if REF is an lvalue valid for this language;
3236 otherwise, print an error message and return zero. */
3239 lvalue_or_else (ref, string)
3240 tree ref;
3241 char *string;
3243 int win = lvalue_p (ref);
3244 if (! win)
3245 error ("invalid lvalue in %s", string);
3246 return win;
3249 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3250 for certain kinds of expressions which are not really lvalues
3251 but which we can accept as lvalues.
3253 If ARG is not a kind of expression we can handle, return zero. */
3255 static tree
3256 unary_complex_lvalue (code, arg)
3257 enum tree_code code;
3258 tree arg;
3260 /* Handle (a, b) used as an "lvalue". */
3261 if (TREE_CODE (arg) == COMPOUND_EXPR)
3263 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3265 /* If this returns a function type, it isn't really being used as
3266 an lvalue, so don't issue a warning about it. */
3267 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3268 pedantic_lvalue_warning (COMPOUND_EXPR);
3270 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3271 TREE_OPERAND (arg, 0), real_result);
3274 /* Handle (a ? b : c) used as an "lvalue". */
3275 if (TREE_CODE (arg) == COND_EXPR)
3277 pedantic_lvalue_warning (COND_EXPR);
3278 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3279 pedantic_lvalue_warning (COMPOUND_EXPR);
3281 return (build_conditional_expr
3282 (TREE_OPERAND (arg, 0),
3283 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3284 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3287 return 0;
3290 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3291 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3293 static void
3294 pedantic_lvalue_warning (code)
3295 enum tree_code code;
3297 if (pedantic)
3298 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3299 code == COND_EXPR ? "conditional"
3300 : code == COMPOUND_EXPR ? "compound" : "cast");
3303 /* Warn about storing in something that is `const'. */
3305 void
3306 readonly_warning (arg, string)
3307 tree arg;
3308 char *string;
3310 char buf[80];
3311 strcpy (buf, string);
3313 /* Forbid assignments to iterators. */
3314 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3316 strcat (buf, " of iterator `%s'");
3317 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3320 if (TREE_CODE (arg) == COMPONENT_REF)
3322 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3323 readonly_warning (TREE_OPERAND (arg, 0), string);
3324 else
3326 strcat (buf, " of read-only member `%s'");
3327 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3330 else if (TREE_CODE (arg) == VAR_DECL)
3332 strcat (buf, " of read-only variable `%s'");
3333 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3335 else
3337 pedwarn ("%s of read-only location", buf);
3341 /* Mark EXP saying that we need to be able to take the
3342 address of it; it should not be allocated in a register.
3343 Value is 1 if successful. */
3346 mark_addressable (exp)
3347 tree exp;
3349 register tree x = exp;
3350 while (1)
3351 switch (TREE_CODE (x))
3353 case COMPONENT_REF:
3354 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3356 error ("cannot take address of bitfield `%s'",
3357 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3358 return 0;
3361 /* ... fall through ... */
3363 case ADDR_EXPR:
3364 case ARRAY_REF:
3365 case REALPART_EXPR:
3366 case IMAGPART_EXPR:
3367 x = TREE_OPERAND (x, 0);
3368 break;
3370 case CONSTRUCTOR:
3371 TREE_ADDRESSABLE (x) = 1;
3372 return 1;
3374 case VAR_DECL:
3375 case CONST_DECL:
3376 case PARM_DECL:
3377 case RESULT_DECL:
3378 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3379 && DECL_NONLOCAL (x))
3381 if (TREE_PUBLIC (x))
3383 error ("global register variable `%s' used in nested function",
3384 IDENTIFIER_POINTER (DECL_NAME (x)));
3385 return 0;
3387 pedwarn ("register variable `%s' used in nested function",
3388 IDENTIFIER_POINTER (DECL_NAME (x)));
3390 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3392 if (TREE_PUBLIC (x))
3394 error ("address of global register variable `%s' requested",
3395 IDENTIFIER_POINTER (DECL_NAME (x)));
3396 return 0;
3399 /* If we are making this addressable due to its having
3400 volatile components, give a different error message. Also
3401 handle the case of an unnamed parameter by not trying
3402 to give the name. */
3404 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3406 error ("cannot put object with volatile field into register");
3407 return 0;
3410 pedwarn ("address of register variable `%s' requested",
3411 IDENTIFIER_POINTER (DECL_NAME (x)));
3413 put_var_into_stack (x);
3415 /* drops in */
3416 case FUNCTION_DECL:
3417 TREE_ADDRESSABLE (x) = 1;
3418 #if 0 /* poplevel deals with this now. */
3419 if (DECL_CONTEXT (x) == 0)
3420 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3421 #endif
3423 default:
3424 return 1;
3428 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3430 tree
3431 build_conditional_expr (ifexp, op1, op2)
3432 tree ifexp, op1, op2;
3434 register tree type1;
3435 register tree type2;
3436 register enum tree_code code1;
3437 register enum tree_code code2;
3438 register tree result_type = NULL;
3439 tree orig_op1 = op1, orig_op2 = op2;
3441 ifexp = truthvalue_conversion (default_conversion (ifexp));
3443 #if 0 /* Produces wrong result if within sizeof. */
3444 /* Don't promote the operands separately if they promote
3445 the same way. Return the unpromoted type and let the combined
3446 value get promoted if necessary. */
3448 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3449 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3450 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3451 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3453 if (TREE_CODE (ifexp) == INTEGER_CST)
3454 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3456 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3458 #endif
3460 /* Promote both alternatives. */
3462 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3463 op1 = default_conversion (op1);
3464 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3465 op2 = default_conversion (op2);
3467 if (TREE_CODE (ifexp) == ERROR_MARK
3468 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3469 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3470 return error_mark_node;
3472 type1 = TREE_TYPE (op1);
3473 code1 = TREE_CODE (type1);
3474 type2 = TREE_TYPE (op2);
3475 code2 = TREE_CODE (type2);
3477 /* Quickly detect the usual case where op1 and op2 have the same type
3478 after promotion. */
3479 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3481 if (type1 == type2)
3482 result_type = type1;
3483 else
3484 result_type = TYPE_MAIN_VARIANT (type1);
3486 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3487 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3489 result_type = common_type (type1, type2);
3491 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3493 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3494 pedwarn ("ANSI C forbids conditional expr with only one void side");
3495 result_type = void_type_node;
3497 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3499 if (comp_target_types (type1, type2))
3500 result_type = common_type (type1, type2);
3501 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3502 && TREE_CODE (orig_op1) != NOP_EXPR)
3503 result_type = qualify_type (type2, type1);
3504 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3505 && TREE_CODE (orig_op2) != NOP_EXPR)
3506 result_type = qualify_type (type1, type2);
3507 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3509 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3510 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3511 result_type = qualify_type (type1, type2);
3513 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3515 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3516 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3517 result_type = qualify_type (type2, type1);
3519 else
3521 pedwarn ("pointer type mismatch in conditional expression");
3522 result_type = build_pointer_type (void_type_node);
3525 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3527 if (! integer_zerop (op2))
3528 pedwarn ("pointer/integer type mismatch in conditional expression");
3529 else
3531 op2 = null_pointer_node;
3532 #if 0 /* The spec seems to say this is permitted. */
3533 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3534 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3535 #endif
3537 result_type = type1;
3539 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3541 if (!integer_zerop (op1))
3542 pedwarn ("pointer/integer type mismatch in conditional expression");
3543 else
3545 op1 = null_pointer_node;
3546 #if 0 /* The spec seems to say this is permitted. */
3547 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3548 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3549 #endif
3551 result_type = type2;
3554 if (!result_type)
3556 if (flag_cond_mismatch)
3557 result_type = void_type_node;
3558 else
3560 error ("type mismatch in conditional expression");
3561 return error_mark_node;
3565 /* Merge const and volatile flags of the incoming types. */
3566 result_type
3567 = build_type_variant (result_type,
3568 TREE_READONLY (op1) || TREE_READONLY (op2),
3569 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3571 if (result_type != TREE_TYPE (op1))
3572 op1 = convert_and_check (result_type, op1);
3573 if (result_type != TREE_TYPE (op2))
3574 op2 = convert_and_check (result_type, op2);
3576 #if 0
3577 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3579 result_type = TREE_TYPE (op1);
3580 if (TREE_CONSTANT (ifexp))
3581 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3583 if (TYPE_MODE (result_type) == BLKmode)
3585 register tree tempvar
3586 = build_decl (VAR_DECL, NULL_TREE, result_type);
3587 register tree xop1 = build_modify_expr (tempvar, op1);
3588 register tree xop2 = build_modify_expr (tempvar, op2);
3589 register tree result = fold (build (COND_EXPR, result_type,
3590 ifexp, xop1, xop2));
3592 layout_decl (tempvar, TYPE_ALIGN (result_type));
3593 /* No way to handle variable-sized objects here.
3594 I fear that the entire handling of BLKmode conditional exprs
3595 needs to be redone. */
3596 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3597 abort ();
3598 DECL_RTL (tempvar)
3599 = assign_stack_local (DECL_MODE (tempvar),
3600 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3601 + BITS_PER_UNIT - 1)
3602 / BITS_PER_UNIT,
3605 TREE_SIDE_EFFECTS (result)
3606 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3607 | TREE_SIDE_EFFECTS (op2);
3608 return build (COMPOUND_EXPR, result_type, result, tempvar);
3611 #endif /* 0 */
3613 if (TREE_CODE (ifexp) == INTEGER_CST)
3614 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3616 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3619 /* Given a list of expressions, return a compound expression
3620 that performs them all and returns the value of the last of them. */
3622 tree
3623 build_compound_expr (list)
3624 tree list;
3626 return internal_build_compound_expr (list, TRUE);
3629 static tree
3630 internal_build_compound_expr (list, first_p)
3631 tree list;
3632 int first_p;
3634 register tree rest;
3636 if (TREE_CHAIN (list) == 0)
3638 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3639 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3641 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3642 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3643 list = TREE_OPERAND (list, 0);
3644 #endif
3646 /* Don't let (0, 0) be null pointer constant. */
3647 if (!first_p && integer_zerop (TREE_VALUE (list)))
3648 return non_lvalue (TREE_VALUE (list));
3649 return TREE_VALUE (list);
3652 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3654 /* Convert arrays to pointers when there really is a comma operator. */
3655 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3656 TREE_VALUE (TREE_CHAIN (list))
3657 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3660 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3662 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3664 /* The left-hand operand of a comma expression is like an expression
3665 statement: with -W or -Wunused, we should warn if it doesn't have
3666 any side-effects, unless it was explicitly cast to (void). */
3667 if ((extra_warnings || warn_unused)
3668 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3669 && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
3670 warning ("left-hand operand of comma expression has no effect");
3672 /* When pedantic, a compound expression can be neither an lvalue
3673 nor an integer constant expression. */
3674 if (! pedantic)
3675 return rest;
3678 /* With -Wunused, we should also warn if the left-hand operand does have
3679 side-effects, but computes a value which is not used. For example, in
3680 `foo() + bar(), baz()' the result of the `+' operator is not used,
3681 so we should issue a warning. */
3682 else if (warn_unused)
3683 warn_if_unused_value (TREE_VALUE (list));
3685 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3688 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3690 tree
3691 build_c_cast (type, expr)
3692 register tree type;
3693 tree expr;
3695 register tree value = expr;
3697 if (type == error_mark_node || expr == error_mark_node)
3698 return error_mark_node;
3699 type = TYPE_MAIN_VARIANT (type);
3701 #if 0
3702 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3703 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3704 value = TREE_OPERAND (value, 0);
3705 #endif
3707 if (TREE_CODE (type) == ARRAY_TYPE)
3709 error ("cast specifies array type");
3710 return error_mark_node;
3713 if (TREE_CODE (type) == FUNCTION_TYPE)
3715 error ("cast specifies function type");
3716 return error_mark_node;
3719 if (type == TREE_TYPE (value))
3721 if (pedantic)
3723 if (TREE_CODE (type) == RECORD_TYPE
3724 || TREE_CODE (type) == UNION_TYPE)
3725 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3728 else if (TREE_CODE (type) == UNION_TYPE)
3730 tree field;
3731 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3732 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3733 value = default_conversion (value);
3735 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3736 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3737 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3738 break;
3740 if (field)
3742 char *name;
3743 tree t;
3745 if (pedantic)
3746 pedwarn ("ANSI C forbids casts to union type");
3747 if (TYPE_NAME (type) != 0)
3749 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3750 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3751 else
3752 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3754 else
3755 name = "";
3756 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3757 build_tree_list (field, value)),
3758 0, 0);
3759 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3760 return t;
3762 error ("cast to union type from type not present in union");
3763 return error_mark_node;
3765 else
3767 tree otype, ovalue;
3769 /* If casting to void, avoid the error that would come
3770 from default_conversion in the case of a non-lvalue array. */
3771 if (type == void_type_node)
3772 return build1 (CONVERT_EXPR, type, value);
3774 /* Convert functions and arrays to pointers,
3775 but don't convert any other types. */
3776 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3777 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3778 value = default_conversion (value);
3779 otype = TREE_TYPE (value);
3781 /* Optionally warn about potentially worrisome casts. */
3783 if (warn_cast_qual
3784 && TREE_CODE (type) == POINTER_TYPE
3785 && TREE_CODE (otype) == POINTER_TYPE)
3787 if (TYPE_VOLATILE (TREE_TYPE (otype))
3788 && ! TYPE_VOLATILE (TREE_TYPE (type)))
3789 pedwarn ("cast discards `volatile' from pointer target type");
3790 if (TYPE_READONLY (TREE_TYPE (otype))
3791 && ! TYPE_READONLY (TREE_TYPE (type)))
3792 pedwarn ("cast discards `const' from pointer target type");
3795 /* Warn about possible alignment problems. */
3796 if (STRICT_ALIGNMENT && warn_cast_align
3797 && TREE_CODE (type) == POINTER_TYPE
3798 && TREE_CODE (otype) == POINTER_TYPE
3799 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3800 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3801 /* Don't warn about opaque types, where the actual alignment
3802 restriction is unknown. */
3803 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3804 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3805 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3806 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3807 warning ("cast increases required alignment of target type");
3809 if (TREE_CODE (type) == INTEGER_TYPE
3810 && TREE_CODE (otype) == POINTER_TYPE
3811 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3812 && !TREE_CONSTANT (value))
3813 warning ("cast from pointer to integer of different size");
3815 if (warn_bad_function_cast
3816 && TREE_CODE (value) == CALL_EXPR
3817 && TREE_CODE (type) != TREE_CODE (otype))
3818 warning ("cast does not match function type");
3820 if (TREE_CODE (type) == POINTER_TYPE
3821 && TREE_CODE (otype) == INTEGER_TYPE
3822 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3823 #if 0
3824 /* Don't warn about converting 0 to pointer,
3825 provided the 0 was explicit--not cast or made by folding. */
3826 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3827 #endif
3828 /* Don't warn about converting any constant. */
3829 && !TREE_CONSTANT (value))
3830 warning ("cast to pointer from integer of different size");
3832 ovalue = value;
3833 value = convert (type, value);
3835 /* Ignore any integer overflow caused by the cast. */
3836 if (TREE_CODE (value) == INTEGER_CST)
3838 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3839 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3843 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3844 if (pedantic && TREE_CODE (value) == INTEGER_CST
3845 && TREE_CODE (expr) == INTEGER_CST
3846 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3847 value = non_lvalue (value);
3849 /* If pedantic, don't let a cast be an lvalue. */
3850 if (value == expr && pedantic)
3851 value = non_lvalue (value);
3853 return value;
3856 /* Build an assignment expression of lvalue LHS from value RHS.
3857 MODIFYCODE is the code for a binary operator that we use
3858 to combine the old value of LHS with RHS to get the new value.
3859 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3861 tree
3862 build_modify_expr (lhs, modifycode, rhs)
3863 tree lhs, rhs;
3864 enum tree_code modifycode;
3866 register tree result;
3867 tree newrhs;
3868 tree lhstype = TREE_TYPE (lhs);
3869 tree olhstype = lhstype;
3871 /* Types that aren't fully specified cannot be used in assignments. */
3872 lhs = require_complete_type (lhs);
3874 /* Avoid duplicate error messages from operands that had errors. */
3875 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3876 return error_mark_node;
3878 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3879 /* Do not use STRIP_NOPS here. We do not want an enumerator
3880 whose value is 0 to count as a null pointer constant. */
3881 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3882 rhs = TREE_OPERAND (rhs, 0);
3884 newrhs = rhs;
3886 /* Handle control structure constructs used as "lvalues". */
3888 switch (TREE_CODE (lhs))
3890 /* Handle (a, b) used as an "lvalue". */
3891 case COMPOUND_EXPR:
3892 pedantic_lvalue_warning (COMPOUND_EXPR);
3893 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3894 modifycode, rhs);
3895 if (TREE_CODE (newrhs) == ERROR_MARK)
3896 return error_mark_node;
3897 return build (COMPOUND_EXPR, lhstype,
3898 TREE_OPERAND (lhs, 0), newrhs);
3900 /* Handle (a ? b : c) used as an "lvalue". */
3901 case COND_EXPR:
3902 pedantic_lvalue_warning (COND_EXPR);
3903 rhs = save_expr (rhs);
3905 /* Produce (a ? (b = rhs) : (c = rhs))
3906 except that the RHS goes through a save-expr
3907 so the code to compute it is only emitted once. */
3908 tree cond
3909 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3910 build_modify_expr (TREE_OPERAND (lhs, 1),
3911 modifycode, rhs),
3912 build_modify_expr (TREE_OPERAND (lhs, 2),
3913 modifycode, rhs));
3914 if (TREE_CODE (cond) == ERROR_MARK)
3915 return cond;
3916 /* Make sure the code to compute the rhs comes out
3917 before the split. */
3918 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3919 /* But cast it to void to avoid an "unused" error. */
3920 convert (void_type_node, rhs), cond);
3922 default:
3923 break;
3926 /* If a binary op has been requested, combine the old LHS value with the RHS
3927 producing the value we should actually store into the LHS. */
3929 if (modifycode != NOP_EXPR)
3931 lhs = stabilize_reference (lhs);
3932 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3935 /* Handle a cast used as an "lvalue".
3936 We have already performed any binary operator using the value as cast.
3937 Now convert the result to the cast type of the lhs,
3938 and then true type of the lhs and store it there;
3939 then convert result back to the cast type to be the value
3940 of the assignment. */
3942 switch (TREE_CODE (lhs))
3944 case NOP_EXPR:
3945 case CONVERT_EXPR:
3946 case FLOAT_EXPR:
3947 case FIX_TRUNC_EXPR:
3948 case FIX_FLOOR_EXPR:
3949 case FIX_ROUND_EXPR:
3950 case FIX_CEIL_EXPR:
3951 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3952 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3953 newrhs = default_conversion (newrhs);
3955 tree inner_lhs = TREE_OPERAND (lhs, 0);
3956 tree result;
3957 result = build_modify_expr (inner_lhs, NOP_EXPR,
3958 convert (TREE_TYPE (inner_lhs),
3959 convert (lhstype, newrhs)));
3960 if (TREE_CODE (result) == ERROR_MARK)
3961 return result;
3962 pedantic_lvalue_warning (CONVERT_EXPR);
3963 return convert (TREE_TYPE (lhs), result);
3966 default:
3967 break;
3970 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3971 Reject anything strange now. */
3973 if (!lvalue_or_else (lhs, "assignment"))
3974 return error_mark_node;
3976 /* Warn about storing in something that is `const'. */
3978 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3979 || ((TREE_CODE (lhstype) == RECORD_TYPE
3980 || TREE_CODE (lhstype) == UNION_TYPE)
3981 && C_TYPE_FIELDS_READONLY (lhstype)))
3982 readonly_warning (lhs, "assignment");
3984 /* If storing into a structure or union member,
3985 it has probably been given type `int'.
3986 Compute the type that would go with
3987 the actual amount of storage the member occupies. */
3989 if (TREE_CODE (lhs) == COMPONENT_REF
3990 && (TREE_CODE (lhstype) == INTEGER_TYPE
3991 || TREE_CODE (lhstype) == REAL_TYPE
3992 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3993 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3995 /* If storing in a field that is in actuality a short or narrower than one,
3996 we must store in the field in its actual type. */
3998 if (lhstype != TREE_TYPE (lhs))
4000 lhs = copy_node (lhs);
4001 TREE_TYPE (lhs) = lhstype;
4004 /* Convert new value to destination type. */
4006 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
4007 NULL_TREE, NULL_TREE, 0);
4008 if (TREE_CODE (newrhs) == ERROR_MARK)
4009 return error_mark_node;
4011 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4012 TREE_SIDE_EFFECTS (result) = 1;
4014 /* If we got the LHS in a different type for storing in,
4015 convert the result back to the nominal type of LHS
4016 so that the value we return always has the same type
4017 as the LHS argument. */
4019 if (olhstype == TREE_TYPE (result))
4020 return result;
4021 return convert_for_assignment (olhstype, result, "assignment",
4022 NULL_TREE, NULL_TREE, 0);
4025 /* Convert value RHS to type TYPE as preparation for an assignment
4026 to an lvalue of type TYPE.
4027 The real work of conversion is done by `convert'.
4028 The purpose of this function is to generate error messages
4029 for assignments that are not allowed in C.
4030 ERRTYPE is a string to use in error messages:
4031 "assignment", "return", etc. If it is null, this is parameter passing
4032 for a function call (and different error messages are output). Otherwise,
4033 it may be a name stored in the spelling stack and interpreted by
4034 get_spelling.
4036 FUNNAME is the name of the function being called,
4037 as an IDENTIFIER_NODE, or null.
4038 PARMNUM is the number of the argument, for printing in error messages. */
4040 static tree
4041 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
4042 tree type, rhs;
4043 char *errtype;
4044 tree fundecl, funname;
4045 int parmnum;
4047 register enum tree_code codel = TREE_CODE (type);
4048 register tree rhstype;
4049 register enum tree_code coder;
4051 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4052 /* Do not use STRIP_NOPS here. We do not want an enumerator
4053 whose value is 0 to count as a null pointer constant. */
4054 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4055 rhs = TREE_OPERAND (rhs, 0);
4057 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4058 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4059 rhs = default_conversion (rhs);
4060 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4061 rhs = decl_constant_value (rhs);
4063 rhstype = TREE_TYPE (rhs);
4064 coder = TREE_CODE (rhstype);
4066 if (coder == ERROR_MARK)
4067 return error_mark_node;
4069 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4071 overflow_warning (rhs);
4072 /* Check for Objective-C protocols. This will issue a warning if
4073 there are protocol violations. No need to use the return value. */
4074 maybe_objc_comptypes (type, rhstype, 0);
4075 return rhs;
4078 if (coder == VOID_TYPE)
4080 error ("void value not ignored as it ought to be");
4081 return error_mark_node;
4083 /* Arithmetic types all interconvert, and enum is treated like int. */
4084 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
4085 || codel == COMPLEX_TYPE)
4086 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
4087 || coder == COMPLEX_TYPE))
4088 return convert_and_check (type, rhs);
4090 /* Conversion to a transparent union from its member types.
4091 This applies only to function arguments. */
4092 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4094 tree memb_types;
4095 tree marginal_memb_type = 0;
4097 for (memb_types = TYPE_FIELDS (type); memb_types;
4098 memb_types = TREE_CHAIN (memb_types))
4100 tree memb_type = TREE_TYPE (memb_types);
4102 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4103 TYPE_MAIN_VARIANT (rhstype)))
4104 break;
4106 if (TREE_CODE (memb_type) != POINTER_TYPE)
4107 continue;
4109 if (coder == POINTER_TYPE)
4111 register tree ttl = TREE_TYPE (memb_type);
4112 register tree ttr = TREE_TYPE (rhstype);
4114 /* Any non-function converts to a [const][volatile] void *
4115 and vice versa; otherwise, targets must be the same.
4116 Meanwhile, the lhs target must have all the qualifiers of
4117 the rhs. */
4118 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4119 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4120 || comp_target_types (memb_type, rhstype))
4122 /* If this type won't generate any warnings, use it. */
4123 if ((TREE_CODE (ttr) == FUNCTION_TYPE
4124 && TREE_CODE (ttl) == FUNCTION_TYPE)
4125 ? ((! TYPE_READONLY (ttl) | TYPE_READONLY (ttr))
4126 & (! TYPE_VOLATILE (ttl) | TYPE_VOLATILE (ttr)))
4127 : ((TYPE_READONLY (ttl) | ! TYPE_READONLY (ttr))
4128 & (TYPE_VOLATILE (ttl) | ! TYPE_VOLATILE (ttr))))
4129 break;
4131 /* Keep looking for a better type, but remember this one. */
4132 if (! marginal_memb_type)
4133 marginal_memb_type = memb_type;
4137 /* Can convert integer zero to any pointer type. */
4138 if (integer_zerop (rhs)
4139 || (TREE_CODE (rhs) == NOP_EXPR
4140 && integer_zerop (TREE_OPERAND (rhs, 0))))
4142 rhs = null_pointer_node;
4143 break;
4147 if (memb_types || marginal_memb_type)
4149 if (! memb_types)
4151 /* We have only a marginally acceptable member type;
4152 it needs a warning. */
4153 register tree ttl = TREE_TYPE (marginal_memb_type);
4154 register tree ttr = TREE_TYPE (rhstype);
4156 /* Const and volatile mean something different for function
4157 types, so the usual warnings are not appropriate. */
4158 if (TREE_CODE (ttr) == FUNCTION_TYPE
4159 && TREE_CODE (ttl) == FUNCTION_TYPE)
4161 /* Because const and volatile on functions are
4162 restrictions that say the function will not do
4163 certain things, it is okay to use a const or volatile
4164 function where an ordinary one is wanted, but not
4165 vice-versa. */
4166 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4167 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4168 get_spelling (errtype), funname,
4169 parmnum);
4170 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4171 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4172 get_spelling (errtype), funname,
4173 parmnum);
4175 else
4177 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4178 warn_for_assignment ("%s discards `const' from pointer target type",
4179 get_spelling (errtype), funname,
4180 parmnum);
4181 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4182 warn_for_assignment ("%s discards `volatile' from pointer target type",
4183 get_spelling (errtype), funname,
4184 parmnum);
4188 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4189 pedwarn ("ANSI C prohibits argument conversion to union type");
4191 return build1 (NOP_EXPR, type, rhs);
4195 /* Conversions among pointers */
4196 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
4198 register tree ttl = TREE_TYPE (type);
4199 register tree ttr = TREE_TYPE (rhstype);
4201 /* Any non-function converts to a [const][volatile] void *
4202 and vice versa; otherwise, targets must be the same.
4203 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4204 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4205 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4206 || comp_target_types (type, rhstype)
4207 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4208 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4210 if (pedantic
4211 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4212 && TREE_CODE (ttr) == FUNCTION_TYPE)
4214 (TYPE_MAIN_VARIANT (ttr) == void_type_node
4215 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4216 which are not ANSI null ptr constants. */
4217 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4218 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4219 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4220 get_spelling (errtype), funname, parmnum);
4221 /* Const and volatile mean something different for function types,
4222 so the usual warnings are not appropriate. */
4223 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4224 && TREE_CODE (ttl) != FUNCTION_TYPE)
4226 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4227 warn_for_assignment ("%s discards `const' from pointer target type",
4228 get_spelling (errtype), funname, parmnum);
4229 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4230 warn_for_assignment ("%s discards `volatile' from pointer target type",
4231 get_spelling (errtype), funname, parmnum);
4232 /* If this is not a case of ignoring a mismatch in signedness,
4233 no warning. */
4234 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4235 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4236 || comp_target_types (type, rhstype))
4238 /* If there is a mismatch, do warn. */
4239 else if (pedantic)
4240 warn_for_assignment ("pointer targets in %s differ in signedness",
4241 get_spelling (errtype), funname, parmnum);
4243 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4244 && TREE_CODE (ttr) == FUNCTION_TYPE)
4246 /* Because const and volatile on functions are restrictions
4247 that say the function will not do certain things,
4248 it is okay to use a const or volatile function
4249 where an ordinary one is wanted, but not vice-versa. */
4250 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4251 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4252 get_spelling (errtype), funname, parmnum);
4253 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4254 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4255 get_spelling (errtype), funname, parmnum);
4258 else
4259 warn_for_assignment ("%s from incompatible pointer type",
4260 get_spelling (errtype), funname, parmnum);
4261 return convert (type, rhs);
4263 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4265 /* An explicit constant 0 can convert to a pointer,
4266 or one that results from arithmetic, even including
4267 a cast to integer type. */
4268 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4270 ! (TREE_CODE (rhs) == NOP_EXPR
4271 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4272 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4273 && integer_zerop (TREE_OPERAND (rhs, 0))))
4275 warn_for_assignment ("%s makes pointer from integer without a cast",
4276 get_spelling (errtype), funname, parmnum);
4277 return convert (type, rhs);
4279 return null_pointer_node;
4281 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4283 warn_for_assignment ("%s makes integer from pointer without a cast",
4284 get_spelling (errtype), funname, parmnum);
4285 return convert (type, rhs);
4288 if (!errtype)
4290 if (funname)
4292 tree selector = maybe_building_objc_message_expr ();
4294 if (selector && parmnum > 2)
4295 error ("incompatible type for argument %d of `%s'",
4296 parmnum - 2, IDENTIFIER_POINTER (selector));
4297 else
4298 error ("incompatible type for argument %d of `%s'",
4299 parmnum, IDENTIFIER_POINTER (funname));
4301 else
4302 error ("incompatible type for argument %d of indirect function call",
4303 parmnum);
4305 else
4306 error ("incompatible types in %s", get_spelling (errtype));
4308 return error_mark_node;
4311 /* Print a warning using MSG.
4312 It gets OPNAME as its one parameter.
4313 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4314 FUNCTION and ARGNUM are handled specially if we are building an
4315 Objective-C selector. */
4317 static void
4318 warn_for_assignment (msg, opname, function, argnum)
4319 char *msg;
4320 char *opname;
4321 tree function;
4322 int argnum;
4324 static char argstring[] = "passing arg %d of `%s'";
4325 static char argnofun[] = "passing arg %d";
4327 if (opname == 0)
4329 tree selector = maybe_building_objc_message_expr ();
4331 if (selector && argnum > 2)
4333 function = selector;
4334 argnum -= 2;
4336 if (function)
4338 /* Function name is known; supply it. */
4339 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4340 + sizeof (argstring) + 25 /*%d*/ + 1);
4341 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4343 else
4345 /* Function name unknown (call through ptr); just give arg number. */
4346 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4347 sprintf (opname, argnofun, argnum);
4350 pedwarn (msg, opname);
4353 /* Return nonzero if VALUE is a valid constant-valued expression
4354 for use in initializing a static variable; one that can be an
4355 element of a "constant" initializer.
4357 Return null_pointer_node if the value is absolute;
4358 if it is relocatable, return the variable that determines the relocation.
4359 We assume that VALUE has been folded as much as possible;
4360 therefore, we do not need to check for such things as
4361 arithmetic-combinations of integers. */
4363 tree
4364 initializer_constant_valid_p (value, endtype)
4365 tree value;
4366 tree endtype;
4368 switch (TREE_CODE (value))
4370 case CONSTRUCTOR:
4371 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4372 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
4373 && TREE_CONSTANT (value)
4374 && CONSTRUCTOR_ELTS (value))
4375 return
4376 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
4377 endtype);
4379 return TREE_STATIC (value) ? null_pointer_node : 0;
4381 case INTEGER_CST:
4382 case REAL_CST:
4383 case STRING_CST:
4384 case COMPLEX_CST:
4385 return null_pointer_node;
4387 case ADDR_EXPR:
4388 return TREE_OPERAND (value, 0);
4390 case NON_LVALUE_EXPR:
4391 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4393 case CONVERT_EXPR:
4394 case NOP_EXPR:
4395 /* Allow conversions between pointer types. */
4396 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4397 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
4398 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4400 /* Allow conversions between real types. */
4401 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4402 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
4403 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4405 /* Allow length-preserving conversions between integer types. */
4406 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4407 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4408 && (TYPE_PRECISION (TREE_TYPE (value))
4409 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4410 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4412 /* Allow conversions between other integer types only if
4413 explicit value. */
4414 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4415 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4417 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4418 endtype);
4419 if (inner == null_pointer_node)
4420 return null_pointer_node;
4421 return 0;
4424 /* Allow (int) &foo provided int is as wide as a pointer. */
4425 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4426 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
4427 && (TYPE_PRECISION (TREE_TYPE (value))
4428 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4429 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4430 endtype);
4432 /* Likewise conversions from int to pointers. */
4433 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4434 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4435 && (TYPE_PRECISION (TREE_TYPE (value))
4436 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4437 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4438 endtype);
4440 /* Allow conversions to union types if the value inside is okay. */
4441 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4442 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4443 endtype);
4444 return 0;
4446 case PLUS_EXPR:
4447 if (TREE_CODE (endtype) == INTEGER_TYPE
4448 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4449 return 0;
4451 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4452 endtype);
4453 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4454 endtype);
4455 /* If either term is absolute, use the other terms relocation. */
4456 if (valid0 == null_pointer_node)
4457 return valid1;
4458 if (valid1 == null_pointer_node)
4459 return valid0;
4460 return 0;
4463 case MINUS_EXPR:
4464 if (TREE_CODE (endtype) == INTEGER_TYPE
4465 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4466 return 0;
4468 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4469 endtype);
4470 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4471 endtype);
4472 /* Win if second argument is absolute. */
4473 if (valid1 == null_pointer_node)
4474 return valid0;
4475 /* Win if both arguments have the same relocation.
4476 Then the value is absolute. */
4477 if (valid0 == valid1)
4478 return null_pointer_node;
4479 return 0;
4482 default:
4483 return 0;
4487 /* If VALUE is a compound expr all of whose expressions are constant, then
4488 return its value. Otherwise, return error_mark_node.
4490 This is for handling COMPOUND_EXPRs as initializer elements
4491 which is allowed with a warning when -pedantic is specified. */
4493 static tree
4494 valid_compound_expr_initializer (value, endtype)
4495 tree value;
4496 tree endtype;
4498 if (TREE_CODE (value) == COMPOUND_EXPR)
4500 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4501 == error_mark_node)
4502 return error_mark_node;
4503 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4504 endtype);
4506 else if (! TREE_CONSTANT (value)
4507 && ! initializer_constant_valid_p (value, endtype))
4508 return error_mark_node;
4509 else
4510 return value;
4513 /* Perform appropriate conversions on the initial value of a variable,
4514 store it in the declaration DECL,
4515 and print any error messages that are appropriate.
4516 If the init is invalid, store an ERROR_MARK. */
4518 void
4519 store_init_value (decl, init)
4520 tree decl, init;
4522 register tree value, type;
4524 /* If variable's type was invalidly declared, just ignore it. */
4526 type = TREE_TYPE (decl);
4527 if (TREE_CODE (type) == ERROR_MARK)
4528 return;
4530 /* Digest the specified initializer into an expression. */
4532 value = digest_init (type, init, TREE_STATIC (decl),
4533 TREE_STATIC (decl) || pedantic);
4535 /* Store the expression if valid; else report error. */
4537 #if 0
4538 /* Note that this is the only place we can detect the error
4539 in a case such as struct foo bar = (struct foo) { x, y };
4540 where there is one initial value which is a constructor expression. */
4541 if (value == error_mark_node)
4543 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4545 error ("initializer for static variable is not constant");
4546 value = error_mark_node;
4548 else if (TREE_STATIC (decl)
4549 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4551 error ("initializer for static variable uses complicated arithmetic");
4552 value = error_mark_node;
4554 else
4556 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4558 if (! TREE_CONSTANT (value))
4559 pedwarn ("aggregate initializer is not constant");
4560 else if (! TREE_STATIC (value))
4561 pedwarn ("aggregate initializer uses complicated arithmetic");
4564 #endif
4566 DECL_INITIAL (decl) = value;
4568 /* ANSI wants warnings about out-of-range constant initializers. */
4569 STRIP_TYPE_NOPS (value);
4570 constant_expression_warning (value);
4573 /* Methods for storing and printing names for error messages. */
4575 /* Implement a spelling stack that allows components of a name to be pushed
4576 and popped. Each element on the stack is this structure. */
4578 struct spelling
4580 int kind;
4581 union
4583 int i;
4584 char *s;
4585 } u;
4588 #define SPELLING_STRING 1
4589 #define SPELLING_MEMBER 2
4590 #define SPELLING_BOUNDS 3
4592 static struct spelling *spelling; /* Next stack element (unused). */
4593 static struct spelling *spelling_base; /* Spelling stack base. */
4594 static int spelling_size; /* Size of the spelling stack. */
4596 /* Macros to save and restore the spelling stack around push_... functions.
4597 Alternative to SAVE_SPELLING_STACK. */
4599 #define SPELLING_DEPTH() (spelling - spelling_base)
4600 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4602 /* Save and restore the spelling stack around arbitrary C code. */
4604 #define SAVE_SPELLING_DEPTH(code) \
4606 int __depth = SPELLING_DEPTH (); \
4607 code; \
4608 RESTORE_SPELLING_DEPTH (__depth); \
4611 /* Push an element on the spelling stack with type KIND and assign VALUE
4612 to MEMBER. */
4614 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4616 int depth = SPELLING_DEPTH (); \
4618 if (depth >= spelling_size) \
4620 spelling_size += 10; \
4621 if (spelling_base == 0) \
4622 spelling_base \
4623 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4624 else \
4625 spelling_base \
4626 = (struct spelling *) xrealloc (spelling_base, \
4627 spelling_size * sizeof (struct spelling)); \
4628 RESTORE_SPELLING_DEPTH (depth); \
4631 spelling->kind = (KIND); \
4632 spelling->MEMBER = (VALUE); \
4633 spelling++; \
4636 /* Push STRING on the stack. Printed literally. */
4638 static void
4639 push_string (string)
4640 char *string;
4642 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4645 /* Push a member name on the stack. Printed as '.' STRING. */
4647 static void
4648 push_member_name (decl)
4649 tree decl;
4652 char *string
4653 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4654 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4657 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4659 static void
4660 push_array_bounds (bounds)
4661 int bounds;
4663 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4666 /* Compute the maximum size in bytes of the printed spelling. */
4668 static int
4669 spelling_length ()
4671 register int size = 0;
4672 register struct spelling *p;
4674 for (p = spelling_base; p < spelling; p++)
4676 if (p->kind == SPELLING_BOUNDS)
4677 size += 25;
4678 else
4679 size += strlen (p->u.s) + 1;
4682 return size;
4685 /* Print the spelling to BUFFER and return it. */
4687 static char *
4688 print_spelling (buffer)
4689 register char *buffer;
4691 register char *d = buffer;
4692 register char *s;
4693 register struct spelling *p;
4695 for (p = spelling_base; p < spelling; p++)
4696 if (p->kind == SPELLING_BOUNDS)
4698 sprintf (d, "[%d]", p->u.i);
4699 d += strlen (d);
4701 else
4703 if (p->kind == SPELLING_MEMBER)
4704 *d++ = '.';
4705 for (s = p->u.s; *d = *s++; d++)
4708 *d++ = '\0';
4709 return buffer;
4712 /* Provide a means to pass component names derived from the spelling stack. */
4714 char initialization_message;
4716 /* Interpret the spelling of the given ERRTYPE message. */
4718 static char *
4719 get_spelling (errtype)
4720 char *errtype;
4722 static char *buffer;
4723 static int size = -1;
4725 if (errtype == &initialization_message)
4727 /* Avoid counting chars */
4728 static char message[] = "initialization of `%s'";
4729 register int needed = sizeof (message) + spelling_length () + 1;
4730 char *temp;
4732 if (size < 0)
4733 buffer = (char *) xmalloc (size = needed);
4734 if (needed > size)
4735 buffer = (char *) xrealloc (buffer, size = needed);
4737 temp = (char *) alloca (needed);
4738 sprintf (buffer, message, print_spelling (temp));
4739 return buffer;
4742 return errtype;
4745 /* Issue an error message for a bad initializer component.
4746 FORMAT describes the message. OFWHAT is the name for the component.
4747 LOCAL is a format string for formatting the insertion of the name
4748 into the message.
4750 If OFWHAT is null, the component name is stored on the spelling stack.
4751 If the component name is a null string, then LOCAL is omitted entirely. */
4753 void
4754 error_init (format, local, ofwhat)
4755 char *format, *local, *ofwhat;
4757 char *buffer;
4759 if (ofwhat == 0)
4760 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4761 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4763 if (*ofwhat)
4764 sprintf (buffer, local, ofwhat);
4765 else
4766 buffer[0] = 0;
4768 error (format, buffer);
4771 /* Issue a pedantic warning for a bad initializer component.
4772 FORMAT describes the message. OFWHAT is the name for the component.
4773 LOCAL is a format string for formatting the insertion of the name
4774 into the message.
4776 If OFWHAT is null, the component name is stored on the spelling stack.
4777 If the component name is a null string, then LOCAL is omitted entirely. */
4779 void
4780 pedwarn_init (format, local, ofwhat)
4781 char *format, *local, *ofwhat;
4783 char *buffer;
4785 if (ofwhat == 0)
4786 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4787 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4789 if (*ofwhat)
4790 sprintf (buffer, local, ofwhat);
4791 else
4792 buffer[0] = 0;
4794 pedwarn (format, buffer);
4797 /* Issue a warning for a bad initializer component.
4798 FORMAT describes the message. OFWHAT is the name for the component.
4799 LOCAL is a format string for formatting the insertion of the name
4800 into the message.
4802 If OFWHAT is null, the component name is stored on the spelling stack.
4803 If the component name is a null string, then LOCAL is omitted entirely. */
4805 static void
4806 warning_init (format, local, ofwhat)
4807 char *format, *local, *ofwhat;
4809 char *buffer;
4811 if (ofwhat == 0)
4812 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4813 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4815 if (*ofwhat)
4816 sprintf (buffer, local, ofwhat);
4817 else
4818 buffer[0] = 0;
4820 warning (format, buffer);
4823 /* Digest the parser output INIT as an initializer for type TYPE.
4824 Return a C expression of type TYPE to represent the initial value.
4826 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4827 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4828 applies only to elements of constructors. */
4830 static tree
4831 digest_init (type, init, require_constant, constructor_constant)
4832 tree type, init;
4833 int require_constant, constructor_constant;
4835 enum tree_code code = TREE_CODE (type);
4836 tree inside_init = init;
4838 if (init == error_mark_node)
4839 return init;
4841 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4842 /* Do not use STRIP_NOPS here. We do not want an enumerator
4843 whose value is 0 to count as a null pointer constant. */
4844 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4845 inside_init = TREE_OPERAND (init, 0);
4847 /* Initialization of an array of chars from a string constant
4848 optionally enclosed in braces. */
4850 if (code == ARRAY_TYPE)
4852 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4853 if ((typ1 == char_type_node
4854 || typ1 == signed_char_type_node
4855 || typ1 == unsigned_char_type_node
4856 || typ1 == unsigned_wchar_type_node
4857 || typ1 == signed_wchar_type_node)
4858 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4860 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4861 TYPE_MAIN_VARIANT (type)))
4862 return inside_init;
4864 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4865 != char_type_node)
4866 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4868 error_init ("char-array%s initialized from wide string",
4869 " `%s'", NULL);
4870 return error_mark_node;
4872 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4873 == char_type_node)
4874 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4876 error_init ("int-array%s initialized from non-wide string",
4877 " `%s'", NULL);
4878 return error_mark_node;
4881 TREE_TYPE (inside_init) = type;
4882 if (TYPE_DOMAIN (type) != 0
4883 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4885 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4886 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4887 /* Subtract 1 (or sizeof (wchar_t))
4888 because it's ok to ignore the terminating null char
4889 that is counted in the length of the constant. */
4890 if (size < TREE_STRING_LENGTH (inside_init)
4891 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4892 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4893 : 1))
4894 pedwarn_init (
4895 "initializer-string for array of chars%s is too long",
4896 " `%s'", NULL);
4898 return inside_init;
4902 /* Any type can be initialized
4903 from an expression of the same type, optionally with braces. */
4905 if (inside_init && TREE_TYPE (inside_init) != 0
4906 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4907 TYPE_MAIN_VARIANT (type))
4908 || (code == ARRAY_TYPE
4909 && comptypes (TREE_TYPE (inside_init), type))
4910 || (code == POINTER_TYPE
4911 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4912 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4913 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4914 TREE_TYPE (type)))))
4916 if (code == POINTER_TYPE
4917 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4918 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4919 inside_init = default_conversion (inside_init);
4920 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4921 && TREE_CODE (inside_init) != CONSTRUCTOR)
4923 error_init ("array%s initialized from non-constant array expression",
4924 " `%s'", NULL);
4925 return error_mark_node;
4928 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4929 inside_init = decl_constant_value (inside_init);
4931 /* Compound expressions can only occur here if -pedantic or
4932 -pedantic-errors is specified. In the later case, we always want
4933 an error. In the former case, we simply want a warning. */
4934 if (require_constant && pedantic
4935 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4937 inside_init
4938 = valid_compound_expr_initializer (inside_init,
4939 TREE_TYPE (inside_init));
4940 if (inside_init == error_mark_node)
4941 error_init ("initializer element%s is not constant",
4942 " for `%s'", NULL);
4943 else
4944 pedwarn_init ("initializer element%s is not constant",
4945 " for `%s'", NULL);
4946 if (flag_pedantic_errors)
4947 inside_init = error_mark_node;
4949 else if (require_constant && ! TREE_CONSTANT (inside_init))
4951 error_init ("initializer element%s is not constant",
4952 " for `%s'", NULL);
4953 inside_init = error_mark_node;
4955 else if (require_constant
4956 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4958 error_init ("initializer element%s is not computable at load time",
4959 " for `%s'", NULL);
4960 inside_init = error_mark_node;
4963 return inside_init;
4966 /* Handle scalar types, including conversions. */
4968 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4969 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4971 /* Note that convert_for_assignment calls default_conversion
4972 for arrays and functions. We must not call it in the
4973 case where inside_init is a null pointer constant. */
4974 inside_init
4975 = convert_for_assignment (type, init, "initialization",
4976 NULL_TREE, NULL_TREE, 0);
4978 if (require_constant && ! TREE_CONSTANT (inside_init))
4980 error_init ("initializer element%s is not constant",
4981 " for `%s'", NULL);
4982 inside_init = error_mark_node;
4984 else if (require_constant
4985 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4987 error_init ("initializer element%s is not computable at load time",
4988 " for `%s'", NULL);
4989 inside_init = error_mark_node;
4992 return inside_init;
4995 /* Come here only for records and arrays. */
4997 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4999 error_init ("variable-sized object%s may not be initialized",
5000 " `%s'", NULL);
5001 return error_mark_node;
5004 /* Traditionally, you can write struct foo x = 0;
5005 and it initializes the first element of x to 0. */
5006 if (flag_traditional)
5008 tree top = 0, prev = 0, otype = type;
5009 while (TREE_CODE (type) == RECORD_TYPE
5010 || TREE_CODE (type) == ARRAY_TYPE
5011 || TREE_CODE (type) == QUAL_UNION_TYPE
5012 || TREE_CODE (type) == UNION_TYPE)
5014 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
5015 if (prev == 0)
5016 top = temp;
5017 else
5018 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
5019 prev = temp;
5020 if (TREE_CODE (type) == ARRAY_TYPE)
5021 type = TREE_TYPE (type);
5022 else if (TYPE_FIELDS (type))
5023 type = TREE_TYPE (TYPE_FIELDS (type));
5024 else
5026 error_init ("invalid initializer%s", " for `%s'", NULL);
5027 return error_mark_node;
5031 if (otype != type)
5033 TREE_OPERAND (prev, 1)
5034 = build_tree_list (NULL_TREE,
5035 digest_init (type, init, require_constant,
5036 constructor_constant));
5037 return top;
5039 else
5040 return error_mark_node;
5042 error_init ("invalid initializer%s", " for `%s'", NULL);
5043 return error_mark_node;
5046 /* Handle initializers that use braces. */
5048 /* Type of object we are accumulating a constructor for.
5049 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5050 static tree constructor_type;
5052 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5053 left to fill. */
5054 static tree constructor_fields;
5056 /* For an ARRAY_TYPE, this is the specified index
5057 at which to store the next element we get.
5058 This is a special INTEGER_CST node that we modify in place. */
5059 static tree constructor_index;
5061 /* For an ARRAY_TYPE, this is the end index of the range
5062 to initialize with the next element, or NULL in the ordinary case
5063 where the element is used just once. */
5064 static tree constructor_range_end;
5066 /* For an ARRAY_TYPE, this is the maximum index. */
5067 static tree constructor_max_index;
5069 /* For a RECORD_TYPE, this is the first field not yet written out. */
5070 static tree constructor_unfilled_fields;
5072 /* For an ARRAY_TYPE, this is the index of the first element
5073 not yet written out.
5074 This is a special INTEGER_CST node that we modify in place. */
5075 static tree constructor_unfilled_index;
5077 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5078 This is so we can generate gaps between fields, when appropriate.
5079 This is a special INTEGER_CST node that we modify in place. */
5080 static tree constructor_bit_index;
5082 /* If we are saving up the elements rather than allocating them,
5083 this is the list of elements so far (in reverse order,
5084 most recent first). */
5085 static tree constructor_elements;
5087 /* 1 if so far this constructor's elements are all compile-time constants. */
5088 static int constructor_constant;
5090 /* 1 if so far this constructor's elements are all valid address constants. */
5091 static int constructor_simple;
5093 /* 1 if this constructor is erroneous so far. */
5094 static int constructor_erroneous;
5096 /* 1 if have called defer_addressed_constants. */
5097 static int constructor_subconstants_deferred;
5099 /* Structure for managing pending initializer elements, organized as an
5100 AVL tree. */
5102 struct init_node
5104 struct init_node *left, *right;
5105 struct init_node *parent;
5106 int balance;
5107 tree purpose;
5108 tree value;
5111 /* Tree of pending elements at this constructor level.
5112 These are elements encountered out of order
5113 which belong at places we haven't reached yet in actually
5114 writing the output. */
5115 static struct init_node *constructor_pending_elts;
5117 /* The SPELLING_DEPTH of this constructor. */
5118 static int constructor_depth;
5120 /* 0 if implicitly pushing constructor levels is allowed. */
5121 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
5123 /* 1 if this constructor level was entered implicitly. */
5124 static int constructor_implicit;
5126 static int require_constant_value;
5127 static int require_constant_elements;
5129 /* 1 if it is ok to output this constructor as we read it.
5130 0 means must accumulate a CONSTRUCTOR expression. */
5131 static int constructor_incremental;
5133 /* DECL node for which an initializer is being read.
5134 0 means we are reading a constructor expression
5135 such as (struct foo) {...}. */
5136 static tree constructor_decl;
5138 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
5139 static char *constructor_asmspec;
5141 /* Nonzero if this is an initializer for a top-level decl. */
5142 static int constructor_top_level;
5144 /* When we finish reading a constructor expression
5145 (constructor_decl is 0), the CONSTRUCTOR goes here. */
5146 static tree constructor_result;
5148 /* This stack has a level for each implicit or explicit level of
5149 structuring in the initializer, including the outermost one. It
5150 saves the values of most of the variables above. */
5152 struct constructor_stack
5154 struct constructor_stack *next;
5155 tree type;
5156 tree fields;
5157 tree index;
5158 tree range_end;
5159 tree max_index;
5160 tree unfilled_index;
5161 tree unfilled_fields;
5162 tree bit_index;
5163 tree elements;
5164 int offset;
5165 struct init_node *pending_elts;
5166 int depth;
5167 /* If nonzero, this value should replace the entire
5168 constructor at this level. */
5169 tree replacement_value;
5170 char constant;
5171 char simple;
5172 char implicit;
5173 char incremental;
5174 char erroneous;
5175 char outer;
5178 struct constructor_stack *constructor_stack;
5180 /* This stack records separate initializers that are nested.
5181 Nested initializers can't happen in ANSI C, but GNU C allows them
5182 in cases like { ... (struct foo) { ... } ... }. */
5184 struct initializer_stack
5186 struct initializer_stack *next;
5187 tree decl;
5188 char *asmspec;
5189 struct constructor_stack *constructor_stack;
5190 tree elements;
5191 struct spelling *spelling;
5192 struct spelling *spelling_base;
5193 int spelling_size;
5194 char top_level;
5195 char incremental;
5196 char require_constant_value;
5197 char require_constant_elements;
5198 char deferred;
5201 struct initializer_stack *initializer_stack;
5203 /* Prepare to parse and output the initializer for variable DECL. */
5205 void
5206 start_init (decl, asmspec_tree, top_level)
5207 tree decl;
5208 tree asmspec_tree;
5209 int top_level;
5211 char *locus;
5212 struct initializer_stack *p
5213 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5214 char *asmspec = 0;
5216 if (asmspec_tree)
5217 asmspec = TREE_STRING_POINTER (asmspec_tree);
5219 p->decl = constructor_decl;
5220 p->asmspec = constructor_asmspec;
5221 p->incremental = constructor_incremental;
5222 p->require_constant_value = require_constant_value;
5223 p->require_constant_elements = require_constant_elements;
5224 p->constructor_stack = constructor_stack;
5225 p->elements = constructor_elements;
5226 p->spelling = spelling;
5227 p->spelling_base = spelling_base;
5228 p->spelling_size = spelling_size;
5229 p->deferred = constructor_subconstants_deferred;
5230 p->top_level = constructor_top_level;
5231 p->next = initializer_stack;
5232 initializer_stack = p;
5234 constructor_decl = decl;
5235 constructor_incremental = top_level;
5236 constructor_asmspec = asmspec;
5237 constructor_subconstants_deferred = 0;
5238 constructor_top_level = top_level;
5240 if (decl != 0)
5242 require_constant_value = TREE_STATIC (decl);
5243 require_constant_elements
5244 = ((TREE_STATIC (decl) || pedantic)
5245 /* For a scalar, you can always use any value to initialize,
5246 even within braces. */
5247 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5248 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5249 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5250 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5251 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5252 constructor_incremental |= TREE_STATIC (decl);
5254 else
5256 require_constant_value = 0;
5257 require_constant_elements = 0;
5258 locus = "(anonymous)";
5261 constructor_stack = 0;
5263 missing_braces_mentioned = 0;
5265 spelling_base = 0;
5266 spelling_size = 0;
5267 RESTORE_SPELLING_DEPTH (0);
5269 if (locus)
5270 push_string (locus);
5273 void
5274 finish_init ()
5276 struct initializer_stack *p = initializer_stack;
5278 /* Output subconstants (string constants, usually)
5279 that were referenced within this initializer and saved up.
5280 Must do this if and only if we called defer_addressed_constants. */
5281 if (constructor_subconstants_deferred)
5282 output_deferred_addressed_constants ();
5284 /* Free the whole constructor stack of this initializer. */
5285 while (constructor_stack)
5287 struct constructor_stack *q = constructor_stack;
5288 constructor_stack = q->next;
5289 free (q);
5292 /* Pop back to the data of the outer initializer (if any). */
5293 constructor_decl = p->decl;
5294 constructor_asmspec = p->asmspec;
5295 constructor_incremental = p->incremental;
5296 require_constant_value = p->require_constant_value;
5297 require_constant_elements = p->require_constant_elements;
5298 constructor_stack = p->constructor_stack;
5299 constructor_elements = p->elements;
5300 spelling = p->spelling;
5301 spelling_base = p->spelling_base;
5302 spelling_size = p->spelling_size;
5303 constructor_subconstants_deferred = p->deferred;
5304 constructor_top_level = p->top_level;
5305 initializer_stack = p->next;
5306 free (p);
5309 /* Call here when we see the initializer is surrounded by braces.
5310 This is instead of a call to push_init_level;
5311 it is matched by a call to pop_init_level.
5313 TYPE is the type to initialize, for a constructor expression.
5314 For an initializer for a decl, TYPE is zero. */
5316 void
5317 really_start_incremental_init (type)
5318 tree type;
5320 struct constructor_stack *p
5321 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5323 if (type == 0)
5324 type = TREE_TYPE (constructor_decl);
5326 /* Turn off constructor_incremental if type is a struct with bitfields.
5327 Do this before the first push, so that the corrected value
5328 is available in finish_init. */
5329 check_init_type_bitfields (type);
5331 p->type = constructor_type;
5332 p->fields = constructor_fields;
5333 p->index = constructor_index;
5334 p->range_end = constructor_range_end;
5335 p->max_index = constructor_max_index;
5336 p->unfilled_index = constructor_unfilled_index;
5337 p->unfilled_fields = constructor_unfilled_fields;
5338 p->bit_index = constructor_bit_index;
5339 p->elements = constructor_elements;
5340 p->constant = constructor_constant;
5341 p->simple = constructor_simple;
5342 p->erroneous = constructor_erroneous;
5343 p->pending_elts = constructor_pending_elts;
5344 p->depth = constructor_depth;
5345 p->replacement_value = 0;
5346 p->implicit = 0;
5347 p->incremental = constructor_incremental;
5348 p->outer = 0;
5349 p->next = 0;
5350 constructor_stack = p;
5352 constructor_constant = 1;
5353 constructor_simple = 1;
5354 constructor_depth = SPELLING_DEPTH ();
5355 constructor_elements = 0;
5356 constructor_pending_elts = 0;
5357 constructor_type = type;
5359 if (TREE_CODE (constructor_type) == RECORD_TYPE
5360 || TREE_CODE (constructor_type) == UNION_TYPE)
5362 constructor_fields = TYPE_FIELDS (constructor_type);
5363 /* Skip any nameless bit fields at the beginning. */
5364 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5365 && DECL_NAME (constructor_fields) == 0)
5366 constructor_fields = TREE_CHAIN (constructor_fields);
5367 constructor_unfilled_fields = constructor_fields;
5368 constructor_bit_index = copy_node (integer_zero_node);
5370 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5372 constructor_range_end = 0;
5373 if (TYPE_DOMAIN (constructor_type))
5375 constructor_max_index
5376 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5377 constructor_index
5378 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5380 else
5381 constructor_index = copy_node (integer_zero_node);
5382 constructor_unfilled_index = copy_node (constructor_index);
5384 else
5386 /* Handle the case of int x = {5}; */
5387 constructor_fields = constructor_type;
5388 constructor_unfilled_fields = constructor_type;
5391 if (constructor_incremental)
5393 int momentary = suspend_momentary ();
5394 push_obstacks_nochange ();
5395 if (TREE_PERMANENT (constructor_decl))
5396 end_temporary_allocation ();
5397 make_decl_rtl (constructor_decl, constructor_asmspec,
5398 constructor_top_level);
5399 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5400 pop_obstacks ();
5401 resume_momentary (momentary);
5404 if (constructor_incremental)
5406 defer_addressed_constants ();
5407 constructor_subconstants_deferred = 1;
5411 /* Push down into a subobject, for initialization.
5412 If this is for an explicit set of braces, IMPLICIT is 0.
5413 If it is because the next element belongs at a lower level,
5414 IMPLICIT is 1. */
5416 void
5417 push_init_level (implicit)
5418 int implicit;
5420 struct constructor_stack *p;
5422 /* If we've exhausted any levels that didn't have braces,
5423 pop them now. */
5424 while (constructor_stack->implicit)
5426 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5427 || TREE_CODE (constructor_type) == UNION_TYPE)
5428 && constructor_fields == 0)
5429 process_init_element (pop_init_level (1));
5430 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5431 && tree_int_cst_lt (constructor_max_index, constructor_index))
5432 process_init_element (pop_init_level (1));
5433 else
5434 break;
5437 /* Structure elements may require alignment. Do this now if necessary
5438 for the subaggregate, and if it comes next in sequence. Don't do
5439 this for subaggregates that will go on the pending list. */
5440 if (constructor_incremental && constructor_type != 0
5441 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields
5442 && constructor_fields == constructor_unfilled_fields)
5444 /* Advance to offset of this element. */
5445 if (! tree_int_cst_equal (constructor_bit_index,
5446 DECL_FIELD_BITPOS (constructor_fields)))
5448 int next = (TREE_INT_CST_LOW
5449 (DECL_FIELD_BITPOS (constructor_fields))
5450 / BITS_PER_UNIT);
5451 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5452 / BITS_PER_UNIT);
5454 assemble_zeros (next - here);
5456 /* Indicate that we have now filled the structure up to the current
5457 field. */
5458 constructor_unfilled_fields = constructor_fields;
5461 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5462 p->type = constructor_type;
5463 p->fields = constructor_fields;
5464 p->index = constructor_index;
5465 p->range_end = constructor_range_end;
5466 p->max_index = constructor_max_index;
5467 p->unfilled_index = constructor_unfilled_index;
5468 p->unfilled_fields = constructor_unfilled_fields;
5469 p->bit_index = constructor_bit_index;
5470 p->elements = constructor_elements;
5471 p->constant = constructor_constant;
5472 p->simple = constructor_simple;
5473 p->erroneous = constructor_erroneous;
5474 p->pending_elts = constructor_pending_elts;
5475 p->depth = constructor_depth;
5476 p->replacement_value = 0;
5477 p->implicit = implicit;
5478 p->incremental = constructor_incremental;
5479 p->outer = 0;
5480 p->next = constructor_stack;
5481 constructor_stack = p;
5483 constructor_constant = 1;
5484 constructor_simple = 1;
5485 constructor_depth = SPELLING_DEPTH ();
5486 constructor_elements = 0;
5487 constructor_pending_elts = 0;
5489 /* Don't die if an entire brace-pair level is superfluous
5490 in the containing level. */
5491 if (constructor_type == 0)
5493 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5494 || TREE_CODE (constructor_type) == UNION_TYPE)
5496 /* Don't die if there are extra init elts at the end. */
5497 if (constructor_fields == 0)
5498 constructor_type = 0;
5499 else
5501 constructor_type = TREE_TYPE (constructor_fields);
5502 push_member_name (constructor_fields);
5503 constructor_depth++;
5504 if (constructor_fields != constructor_unfilled_fields)
5505 constructor_incremental = 0;
5508 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5510 constructor_type = TREE_TYPE (constructor_type);
5511 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5512 constructor_depth++;
5513 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5514 || constructor_range_end != 0)
5515 constructor_incremental = 0;
5518 if (constructor_type == 0)
5520 error_init ("extra brace group at end of initializer%s",
5521 " for `%s'", NULL);
5522 constructor_fields = 0;
5523 constructor_unfilled_fields = 0;
5524 return;
5527 /* Turn off constructor_incremental if type is a struct with bitfields. */
5528 check_init_type_bitfields (constructor_type);
5530 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5532 missing_braces_mentioned = 1;
5533 warning_init ("missing braces around initializer%s", " for `%s'", NULL);
5536 if (TREE_CODE (constructor_type) == RECORD_TYPE
5537 || TREE_CODE (constructor_type) == UNION_TYPE)
5539 constructor_fields = TYPE_FIELDS (constructor_type);
5540 /* Skip any nameless bit fields at the beginning. */
5541 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5542 && DECL_NAME (constructor_fields) == 0)
5543 constructor_fields = TREE_CHAIN (constructor_fields);
5544 constructor_unfilled_fields = constructor_fields;
5545 constructor_bit_index = copy_node (integer_zero_node);
5547 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5549 constructor_range_end = 0;
5550 if (TYPE_DOMAIN (constructor_type))
5552 constructor_max_index
5553 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5554 constructor_index
5555 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5557 else
5558 constructor_index = copy_node (integer_zero_node);
5559 constructor_unfilled_index = copy_node (constructor_index);
5561 else
5563 warning_init ("braces around scalar initializer%s", " for `%s'", NULL);
5564 constructor_fields = constructor_type;
5565 constructor_unfilled_fields = constructor_type;
5569 /* Don't read a struct incrementally if it has any bitfields,
5570 because the incremental reading code doesn't know how to
5571 handle bitfields yet. */
5573 static void
5574 check_init_type_bitfields (type)
5575 tree type;
5577 if (TREE_CODE (type) == RECORD_TYPE)
5579 tree tail;
5580 for (tail = TYPE_FIELDS (type); tail;
5581 tail = TREE_CHAIN (tail))
5583 if (DECL_C_BIT_FIELD (tail)
5584 /* This catches cases like `int foo : 8;'. */
5585 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail)))
5587 constructor_incremental = 0;
5588 break;
5591 check_init_type_bitfields (TREE_TYPE (tail));
5595 else if (TREE_CODE (type) == ARRAY_TYPE)
5596 check_init_type_bitfields (TREE_TYPE (type));
5599 /* At the end of an implicit or explicit brace level,
5600 finish up that level of constructor.
5601 If we were outputting the elements as they are read, return 0
5602 from inner levels (process_init_element ignores that),
5603 but return error_mark_node from the outermost level
5604 (that's what we want to put in DECL_INITIAL).
5605 Otherwise, return a CONSTRUCTOR expression. */
5607 tree
5608 pop_init_level (implicit)
5609 int implicit;
5611 struct constructor_stack *p;
5612 int size = 0;
5613 tree constructor = 0;
5615 if (implicit == 0)
5617 /* When we come to an explicit close brace,
5618 pop any inner levels that didn't have explicit braces. */
5619 while (constructor_stack->implicit)
5620 process_init_element (pop_init_level (1));
5623 p = constructor_stack;
5625 if (constructor_type != 0)
5626 size = int_size_in_bytes (constructor_type);
5628 /* Now output all pending elements. */
5629 output_pending_init_elements (1);
5631 #if 0 /* c-parse.in warns about {}. */
5632 /* In ANSI, each brace level must have at least one element. */
5633 if (! implicit && pedantic
5634 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5635 ? integer_zerop (constructor_unfilled_index)
5636 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5637 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL);
5638 #endif
5640 /* Pad out the end of the structure. */
5642 if (p->replacement_value)
5644 /* If this closes a superfluous brace pair,
5645 just pass out the element between them. */
5646 constructor = p->replacement_value;
5647 /* If this is the top level thing within the initializer,
5648 and it's for a variable, then since we already called
5649 assemble_variable, we must output the value now. */
5650 if (p->next == 0 && constructor_decl != 0
5651 && constructor_incremental)
5653 constructor = digest_init (constructor_type, constructor,
5654 require_constant_value,
5655 require_constant_elements);
5657 /* If initializing an array of unknown size,
5658 determine the size now. */
5659 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5660 && TYPE_DOMAIN (constructor_type) == 0)
5662 int failure;
5663 int momentary_p;
5665 push_obstacks_nochange ();
5666 if (TREE_PERMANENT (constructor_type))
5667 end_temporary_allocation ();
5669 momentary_p = suspend_momentary ();
5671 /* We shouldn't have an incomplete array type within
5672 some other type. */
5673 if (constructor_stack->next)
5674 abort ();
5676 failure
5677 = complete_array_type (constructor_type,
5678 constructor, 0);
5679 if (failure)
5680 abort ();
5682 size = int_size_in_bytes (constructor_type);
5683 resume_momentary (momentary_p);
5684 pop_obstacks ();
5687 output_constant (constructor, size);
5690 else if (constructor_type == 0)
5692 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5693 && TREE_CODE (constructor_type) != UNION_TYPE
5694 && TREE_CODE (constructor_type) != ARRAY_TYPE
5695 && ! constructor_incremental)
5697 /* A nonincremental scalar initializer--just return
5698 the element, after verifying there is just one. */
5699 if (constructor_elements == 0)
5701 error_init ("empty scalar initializer%s",
5702 " for `%s'", NULL);
5703 constructor = error_mark_node;
5705 else if (TREE_CHAIN (constructor_elements) != 0)
5707 error_init ("extra elements in scalar initializer%s",
5708 " for `%s'", NULL);
5709 constructor = TREE_VALUE (constructor_elements);
5711 else
5712 constructor = TREE_VALUE (constructor_elements);
5714 else if (! constructor_incremental)
5716 if (constructor_erroneous)
5717 constructor = error_mark_node;
5718 else
5720 int momentary = suspend_momentary ();
5722 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5723 nreverse (constructor_elements));
5724 if (constructor_constant)
5725 TREE_CONSTANT (constructor) = 1;
5726 if (constructor_constant && constructor_simple)
5727 TREE_STATIC (constructor) = 1;
5729 resume_momentary (momentary);
5732 else
5734 tree filled;
5735 int momentary = suspend_momentary ();
5737 if (TREE_CODE (constructor_type) == RECORD_TYPE
5738 || TREE_CODE (constructor_type) == UNION_TYPE)
5740 /* Find the offset of the end of that field. */
5741 filled = size_binop (CEIL_DIV_EXPR,
5742 constructor_bit_index,
5743 size_int (BITS_PER_UNIT));
5745 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5747 /* If initializing an array of unknown size,
5748 determine the size now. */
5749 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5750 && TYPE_DOMAIN (constructor_type) == 0)
5752 tree maxindex
5753 = size_binop (MINUS_EXPR,
5754 constructor_unfilled_index,
5755 integer_one_node);
5757 push_obstacks_nochange ();
5758 if (TREE_PERMANENT (constructor_type))
5759 end_temporary_allocation ();
5760 maxindex = copy_node (maxindex);
5761 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5762 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5764 /* TYPE_MAX_VALUE is always one less than the number of elements
5765 in the array, because we start counting at zero. Therefore,
5766 warn only if the value is less than zero. */
5767 if (pedantic
5768 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5769 < 0))
5770 error_with_decl (constructor_decl,
5771 "zero or negative array size `%s'");
5772 layout_type (constructor_type);
5773 size = int_size_in_bytes (constructor_type);
5774 pop_obstacks ();
5777 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5778 size_in_bytes (TREE_TYPE (constructor_type)));
5780 else
5781 filled = 0;
5783 if (filled != 0)
5784 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5786 resume_momentary (momentary);
5790 constructor_type = p->type;
5791 constructor_fields = p->fields;
5792 constructor_index = p->index;
5793 constructor_range_end = p->range_end;
5794 constructor_max_index = p->max_index;
5795 constructor_unfilled_index = p->unfilled_index;
5796 constructor_unfilled_fields = p->unfilled_fields;
5797 constructor_bit_index = p->bit_index;
5798 constructor_elements = p->elements;
5799 constructor_constant = p->constant;
5800 constructor_simple = p->simple;
5801 constructor_erroneous = p->erroneous;
5802 constructor_pending_elts = p->pending_elts;
5803 constructor_depth = p->depth;
5804 constructor_incremental = p->incremental;
5805 RESTORE_SPELLING_DEPTH (constructor_depth);
5807 constructor_stack = p->next;
5808 free (p);
5810 if (constructor == 0)
5812 if (constructor_stack == 0)
5813 return error_mark_node;
5814 return NULL_TREE;
5816 return constructor;
5819 /* Within an array initializer, specify the next index to be initialized.
5820 FIRST is that index. If LAST is nonzero, then initialize a range
5821 of indices, running from FIRST through LAST. */
5823 void
5824 set_init_index (first, last)
5825 tree first, last;
5827 while ((TREE_CODE (first) == NOP_EXPR
5828 || TREE_CODE (first) == CONVERT_EXPR
5829 || TREE_CODE (first) == NON_LVALUE_EXPR)
5830 && (TYPE_MODE (TREE_TYPE (first))
5831 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5832 (first) = TREE_OPERAND (first, 0);
5833 if (last)
5834 while ((TREE_CODE (last) == NOP_EXPR
5835 || TREE_CODE (last) == CONVERT_EXPR
5836 || TREE_CODE (last) == NON_LVALUE_EXPR)
5837 && (TYPE_MODE (TREE_TYPE (last))
5838 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5839 (last) = TREE_OPERAND (last, 0);
5841 if (TREE_CODE (first) != INTEGER_CST)
5842 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5843 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5844 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5845 else if (! constructor_unfilled_index)
5846 error_init ("array index in non-array initializer%s", " for `%s'", NULL);
5847 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5848 error_init ("duplicate array index in initializer%s", " for `%s'", NULL);
5849 else
5851 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (first);
5852 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (first);
5854 if (last != 0 && tree_int_cst_lt (last, first))
5855 error_init ("empty index range in initializer%s", " for `%s'", NULL);
5856 else
5858 if (pedantic)
5859 pedwarn ("ANSI C forbids specifying element to initialize");
5860 constructor_range_end = last;
5865 /* Within a struct initializer, specify the next field to be initialized. */
5867 void
5868 set_init_label (fieldname)
5869 tree fieldname;
5871 tree tail;
5872 int passed = 0;
5874 /* Don't die if an entire brace-pair level is superfluous
5875 in the containing level. */
5876 if (constructor_type == 0)
5877 return;
5879 for (tail = TYPE_FIELDS (constructor_type); tail;
5880 tail = TREE_CHAIN (tail))
5882 if (tail == constructor_unfilled_fields)
5883 passed = 1;
5884 if (DECL_NAME (tail) == fieldname)
5885 break;
5888 if (tail == 0)
5889 error ("unknown field `%s' specified in initializer",
5890 IDENTIFIER_POINTER (fieldname));
5891 else if (!passed)
5892 error ("field `%s' already initialized",
5893 IDENTIFIER_POINTER (fieldname));
5894 else
5896 constructor_fields = tail;
5897 if (pedantic)
5898 pedwarn ("ANSI C forbids specifying structure member to initialize");
5902 /* Add a new initializer to the tree of pending initializers. PURPOSE
5903 indentifies the initializer, either array index or field in a structure.
5904 VALUE is the value of that index or field. */
5906 static void
5907 add_pending_init (purpose, value)
5908 tree purpose, value;
5910 struct init_node *p, **q, *r;
5912 q = &constructor_pending_elts;
5913 p = 0;
5915 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5917 while (*q != 0)
5919 p = *q;
5920 if (tree_int_cst_lt (purpose, p->purpose))
5921 q = &p->left;
5922 else if (tree_int_cst_lt (p->purpose, purpose))
5923 q = &p->right;
5924 else
5925 abort ();
5928 else
5930 while (*q != NULL)
5932 p = *q;
5933 if (tree_int_cst_lt (DECL_FIELD_BITPOS (purpose),
5934 DECL_FIELD_BITPOS (p->purpose)))
5935 q = &p->left;
5936 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (p->purpose),
5937 DECL_FIELD_BITPOS (purpose)))
5938 q = &p->right;
5939 else
5940 abort ();
5944 r = (struct init_node *) oballoc (sizeof (struct init_node));
5945 r->purpose = purpose;
5946 r->value = value;
5948 *q = r;
5949 r->parent = p;
5950 r->left = 0;
5951 r->right = 0;
5952 r->balance = 0;
5954 while (p)
5956 struct init_node *s;
5958 if (r == p->left)
5960 if (p->balance == 0)
5961 p->balance = -1;
5962 else if (p->balance < 0)
5964 if (r->balance < 0)
5966 /* L rotation. */
5967 p->left = r->right;
5968 if (p->left)
5969 p->left->parent = p;
5970 r->right = p;
5972 p->balance = 0;
5973 r->balance = 0;
5975 s = p->parent;
5976 p->parent = r;
5977 r->parent = s;
5978 if (s)
5980 if (s->left == p)
5981 s->left = r;
5982 else
5983 s->right = r;
5985 else
5986 constructor_pending_elts = r;
5988 else
5990 /* LR rotation. */
5991 struct init_node *t = r->right;
5993 r->right = t->left;
5994 if (r->right)
5995 r->right->parent = r;
5996 t->left = r;
5998 p->left = t->right;
5999 if (p->left)
6000 p->left->parent = p;
6001 t->right = p;
6003 p->balance = t->balance < 0;
6004 r->balance = -(t->balance > 0);
6005 t->balance = 0;
6007 s = p->parent;
6008 p->parent = t;
6009 r->parent = t;
6010 t->parent = s;
6011 if (s)
6013 if (s->left == p)
6014 s->left = t;
6015 else
6016 s->right = t;
6018 else
6019 constructor_pending_elts = t;
6021 break;
6023 else
6025 /* p->balance == +1; growth of left side balances the node. */
6026 p->balance = 0;
6027 break;
6030 else /* r == p->right */
6032 if (p->balance == 0)
6033 /* Growth propagation from right side. */
6034 p->balance++;
6035 else if (p->balance > 0)
6037 if (r->balance > 0)
6039 /* R rotation. */
6040 p->right = r->left;
6041 if (p->right)
6042 p->right->parent = p;
6043 r->left = p;
6045 p->balance = 0;
6046 r->balance = 0;
6048 s = p->parent;
6049 p->parent = r;
6050 r->parent = s;
6051 if (s)
6053 if (s->left == p)
6054 s->left = r;
6055 else
6056 s->right = r;
6058 else
6059 constructor_pending_elts = r;
6061 else /* r->balance == -1 */
6063 /* RL rotation */
6064 struct init_node *t = r->left;
6066 r->left = t->right;
6067 if (r->left)
6068 r->left->parent = r;
6069 t->right = r;
6071 p->right = t->left;
6072 if (p->right)
6073 p->right->parent = p;
6074 t->left = p;
6076 r->balance = (t->balance < 0);
6077 p->balance = -(t->balance > 0);
6078 t->balance = 0;
6080 s = p->parent;
6081 p->parent = t;
6082 r->parent = t;
6083 t->parent = s;
6084 if (s)
6086 if (s->left == p)
6087 s->left = t;
6088 else
6089 s->right = t;
6091 else
6092 constructor_pending_elts = t;
6094 break;
6096 else
6098 /* p->balance == -1; growth of right side balances the node. */
6099 p->balance = 0;
6100 break;
6104 r = p;
6105 p = p->parent;
6109 /* Return nonzero if FIELD is equal to the index of a pending initializer. */
6111 static int
6112 pending_init_member (field)
6113 tree field;
6115 struct init_node *p;
6117 p = constructor_pending_elts;
6118 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6120 while (p)
6122 if (tree_int_cst_equal (field, p->purpose))
6123 return 1;
6124 else if (tree_int_cst_lt (field, p->purpose))
6125 p = p->left;
6126 else
6127 p = p->right;
6130 else
6132 while (p)
6134 if (field == p->purpose)
6135 return 1;
6136 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (field),
6137 DECL_FIELD_BITPOS (p->purpose)))
6138 p = p->left;
6139 else
6140 p = p->right;
6144 return 0;
6147 /* "Output" the next constructor element.
6148 At top level, really output it to assembler code now.
6149 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6150 TYPE is the data type that the containing data type wants here.
6151 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6153 PENDING if non-nil means output pending elements that belong
6154 right after this element. (PENDING is normally 1;
6155 it is 0 while outputting pending elements, to avoid recursion.) */
6157 static void
6158 output_init_element (value, type, field, pending)
6159 tree value, type, field;
6160 int pending;
6162 int duplicate = 0;
6164 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6165 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6166 && !(TREE_CODE (value) == STRING_CST
6167 && TREE_CODE (type) == ARRAY_TYPE
6168 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6169 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6170 TYPE_MAIN_VARIANT (type))))
6171 value = default_conversion (value);
6173 if (value == error_mark_node)
6174 constructor_erroneous = 1;
6175 else if (!TREE_CONSTANT (value))
6176 constructor_constant = 0;
6177 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6178 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6179 || TREE_CODE (constructor_type) == UNION_TYPE)
6180 && DECL_C_BIT_FIELD (field)
6181 && TREE_CODE (value) != INTEGER_CST))
6182 constructor_simple = 0;
6184 if (require_constant_value && ! TREE_CONSTANT (value))
6186 error_init ("initializer element%s is not constant",
6187 " for `%s'", NULL);
6188 value = error_mark_node;
6190 else if (require_constant_elements
6191 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6193 error_init ("initializer element%s is not computable at load time",
6194 " for `%s'", NULL);
6195 value = error_mark_node;
6198 /* If this element duplicates one on constructor_pending_elts,
6199 print a message and ignore it. Don't do this when we're
6200 processing elements taken off constructor_pending_elts,
6201 because we'd always get spurious errors. */
6202 if (pending)
6204 if (TREE_CODE (constructor_type) == RECORD_TYPE
6205 || TREE_CODE (constructor_type) == UNION_TYPE
6206 || TREE_CODE (constructor_type) == ARRAY_TYPE)
6208 if (pending_init_member (field))
6210 error_init ("duplicate initializer%s", " for `%s'", NULL);
6211 duplicate = 1;
6216 /* If this element doesn't come next in sequence,
6217 put it on constructor_pending_elts. */
6218 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6219 && !tree_int_cst_equal (field, constructor_unfilled_index))
6221 if (! duplicate)
6222 /* The copy_node is needed in case field is actually
6223 constructor_index, which is modified in place. */
6224 add_pending_init (copy_node (field),
6225 digest_init (type, value, require_constant_value,
6226 require_constant_elements));
6228 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6229 && field != constructor_unfilled_fields)
6231 /* We do this for records but not for unions. In a union,
6232 no matter which field is specified, it can be initialized
6233 right away since it starts at the beginning of the union. */
6234 if (!duplicate)
6235 add_pending_init (field,
6236 digest_init (type, value, require_constant_value,
6237 require_constant_elements));
6239 else
6241 /* Otherwise, output this element either to
6242 constructor_elements or to the assembler file. */
6244 if (!duplicate)
6246 if (! constructor_incremental)
6248 if (field && TREE_CODE (field) == INTEGER_CST)
6249 field = copy_node (field);
6250 constructor_elements
6251 = tree_cons (field, digest_init (type, value,
6252 require_constant_value,
6253 require_constant_elements),
6254 constructor_elements);
6256 else
6258 /* Structure elements may require alignment.
6259 Do this, if necessary. */
6260 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6262 /* Advance to offset of this element. */
6263 if (! tree_int_cst_equal (constructor_bit_index,
6264 DECL_FIELD_BITPOS (field)))
6266 int next = (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
6267 / BITS_PER_UNIT);
6268 int here = (TREE_INT_CST_LOW (constructor_bit_index)
6269 / BITS_PER_UNIT);
6271 assemble_zeros (next - here);
6274 output_constant (digest_init (type, value,
6275 require_constant_value,
6276 require_constant_elements),
6277 int_size_in_bytes (type));
6279 /* For a record or union,
6280 keep track of end position of last field. */
6281 if (TREE_CODE (constructor_type) == RECORD_TYPE
6282 || TREE_CODE (constructor_type) == UNION_TYPE)
6284 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
6285 DECL_SIZE (field));
6286 TREE_INT_CST_LOW (constructor_bit_index)
6287 = TREE_INT_CST_LOW (temp);
6288 TREE_INT_CST_HIGH (constructor_bit_index)
6289 = TREE_INT_CST_HIGH (temp);
6294 /* Advance the variable that indicates sequential elements output. */
6295 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6297 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
6298 integer_one_node);
6299 TREE_INT_CST_LOW (constructor_unfilled_index)
6300 = TREE_INT_CST_LOW (tem);
6301 TREE_INT_CST_HIGH (constructor_unfilled_index)
6302 = TREE_INT_CST_HIGH (tem);
6304 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6305 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6306 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6307 constructor_unfilled_fields = 0;
6309 /* Now output any pending elements which have become next. */
6310 if (pending)
6311 output_pending_init_elements (0);
6315 /* Output any pending elements which have become next.
6316 As we output elements, constructor_unfilled_{fields,index}
6317 advances, which may cause other elements to become next;
6318 if so, they too are output.
6320 If ALL is 0, we return when there are
6321 no more pending elements to output now.
6323 If ALL is 1, we output space as necessary so that
6324 we can output all the pending elements. */
6326 static void
6327 output_pending_init_elements (all)
6328 int all;
6330 struct init_node *elt = constructor_pending_elts;
6331 tree next;
6333 retry:
6335 /* Look thru the whole pending tree.
6336 If we find an element that should be output now,
6337 output it. Otherwise, set NEXT to the element
6338 that comes first among those still pending. */
6340 next = 0;
6341 while (elt)
6343 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6345 if (tree_int_cst_equal (elt->purpose,
6346 constructor_unfilled_index))
6347 output_init_element (elt->value,
6348 TREE_TYPE (constructor_type),
6349 constructor_unfilled_index, 0);
6350 else if (tree_int_cst_lt (constructor_unfilled_index,
6351 elt->purpose))
6353 /* Advance to the next smaller node. */
6354 if (elt->left)
6355 elt = elt->left;
6356 else
6358 /* We have reached the smallest node bigger than the
6359 current unfilled index. Fill the space first. */
6360 next = elt->purpose;
6361 break;
6364 else
6366 /* Advance to the next bigger node. */
6367 if (elt->right)
6368 elt = elt->right;
6369 else
6371 /* We have reached the biggest node in a subtree. Find
6372 the parent of it, which is the next bigger node. */
6373 while (elt->parent && elt->parent->right == elt)
6374 elt = elt->parent;
6375 elt = elt->parent;
6376 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6377 elt->purpose))
6379 next = elt->purpose;
6380 break;
6385 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6386 || TREE_CODE (constructor_type) == UNION_TYPE)
6388 /* If the current record is complete we are done. */
6389 if (constructor_unfilled_fields == 0)
6390 break;
6391 if (elt->purpose == constructor_unfilled_fields)
6393 output_init_element (elt->value,
6394 TREE_TYPE (constructor_unfilled_fields),
6395 constructor_unfilled_fields,
6398 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6399 DECL_FIELD_BITPOS (elt->purpose)))
6401 /* Advance to the next smaller node. */
6402 if (elt->left)
6403 elt = elt->left;
6404 else
6406 /* We have reached the smallest node bigger than the
6407 current unfilled field. Fill the space first. */
6408 next = elt->purpose;
6409 break;
6412 else
6414 /* Advance to the next bigger node. */
6415 if (elt->right)
6416 elt = elt->right;
6417 else
6419 /* We have reached the biggest node in a subtree. Find
6420 the parent of it, which is the next bigger node. */
6421 while (elt->parent && elt->parent->right == elt)
6422 elt = elt->parent;
6423 elt = elt->parent;
6424 if (elt
6425 && tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6426 DECL_FIELD_BITPOS (elt->purpose)))
6428 next = elt->purpose;
6429 break;
6436 /* Ordinarily return, but not if we want to output all
6437 and there are elements left. */
6438 if (! (all && next != 0))
6439 return;
6441 /* Generate space up to the position of NEXT. */
6442 if (constructor_incremental)
6444 tree filled;
6445 tree nextpos_tree = size_int (0);
6447 if (TREE_CODE (constructor_type) == RECORD_TYPE
6448 || TREE_CODE (constructor_type) == UNION_TYPE)
6450 tree tail;
6451 /* Find the last field written out, if any. */
6452 for (tail = TYPE_FIELDS (constructor_type); tail;
6453 tail = TREE_CHAIN (tail))
6454 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
6455 break;
6457 if (tail)
6458 /* Find the offset of the end of that field. */
6459 filled = size_binop (CEIL_DIV_EXPR,
6460 size_binop (PLUS_EXPR,
6461 DECL_FIELD_BITPOS (tail),
6462 DECL_SIZE (tail)),
6463 size_int (BITS_PER_UNIT));
6464 else
6465 filled = size_int (0);
6467 nextpos_tree = size_binop (CEIL_DIV_EXPR,
6468 DECL_FIELD_BITPOS (next),
6469 size_int (BITS_PER_UNIT));
6471 TREE_INT_CST_HIGH (constructor_bit_index)
6472 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
6473 TREE_INT_CST_LOW (constructor_bit_index)
6474 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
6475 constructor_unfilled_fields = next;
6477 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6479 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
6480 size_in_bytes (TREE_TYPE (constructor_type)));
6481 nextpos_tree
6482 = size_binop (MULT_EXPR, next,
6483 size_in_bytes (TREE_TYPE (constructor_type)));
6484 TREE_INT_CST_LOW (constructor_unfilled_index)
6485 = TREE_INT_CST_LOW (next);
6486 TREE_INT_CST_HIGH (constructor_unfilled_index)
6487 = TREE_INT_CST_HIGH (next);
6489 else
6490 filled = 0;
6492 if (filled)
6494 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
6496 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
6499 else
6501 /* If it's not incremental, just skip over the gap,
6502 so that after jumping to retry we will output the next
6503 successive element. */
6504 if (TREE_CODE (constructor_type) == RECORD_TYPE
6505 || TREE_CODE (constructor_type) == UNION_TYPE)
6506 constructor_unfilled_fields = next;
6507 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6509 TREE_INT_CST_LOW (constructor_unfilled_index)
6510 = TREE_INT_CST_LOW (next);
6511 TREE_INT_CST_HIGH (constructor_unfilled_index)
6512 = TREE_INT_CST_HIGH (next);
6516 /* ELT now points to the node in the pending tree with the next
6517 initializer to output. */
6518 goto retry;
6521 /* Add one non-braced element to the current constructor level.
6522 This adjusts the current position within the constructor's type.
6523 This may also start or terminate implicit levels
6524 to handle a partly-braced initializer.
6526 Once this has found the correct level for the new element,
6527 it calls output_init_element.
6529 Note: if we are incrementally outputting this constructor,
6530 this function may be called with a null argument
6531 representing a sub-constructor that was already incrementally output.
6532 When that happens, we output nothing, but we do the bookkeeping
6533 to skip past that element of the current constructor. */
6535 void
6536 process_init_element (value)
6537 tree value;
6539 tree orig_value = value;
6540 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6542 /* Handle superfluous braces around string cst as in
6543 char x[] = {"foo"}; */
6544 if (string_flag
6545 && constructor_type
6546 && TREE_CODE (constructor_type) == ARRAY_TYPE
6547 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6548 && integer_zerop (constructor_unfilled_index))
6550 constructor_stack->replacement_value = value;
6551 return;
6554 if (constructor_stack->replacement_value != 0)
6556 error_init ("excess elements in struct initializer%s",
6557 " after `%s'", NULL_PTR);
6558 return;
6561 /* Ignore elements of a brace group if it is entirely superfluous
6562 and has already been diagnosed. */
6563 if (constructor_type == 0)
6564 return;
6566 /* If we've exhausted any levels that didn't have braces,
6567 pop them now. */
6568 while (constructor_stack->implicit)
6570 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6571 || TREE_CODE (constructor_type) == UNION_TYPE)
6572 && constructor_fields == 0)
6573 process_init_element (pop_init_level (1));
6574 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6575 && (constructor_max_index == 0
6576 || tree_int_cst_lt (constructor_max_index,
6577 constructor_index)))
6578 process_init_element (pop_init_level (1));
6579 else
6580 break;
6583 while (1)
6585 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6587 tree fieldtype;
6588 enum tree_code fieldcode;
6590 if (constructor_fields == 0)
6592 pedwarn_init ("excess elements in struct initializer%s",
6593 " after `%s'", NULL_PTR);
6594 break;
6597 fieldtype = TREE_TYPE (constructor_fields);
6598 if (fieldtype != error_mark_node)
6599 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6600 fieldcode = TREE_CODE (fieldtype);
6602 /* Accept a string constant to initialize a subarray. */
6603 if (value != 0
6604 && fieldcode == ARRAY_TYPE
6605 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6606 && string_flag)
6607 value = orig_value;
6608 /* Otherwise, if we have come to a subaggregate,
6609 and we don't have an element of its type, push into it. */
6610 else if (value != 0 && !constructor_no_implicit
6611 && value != error_mark_node
6612 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6613 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6614 || fieldcode == UNION_TYPE))
6616 push_init_level (1);
6617 continue;
6620 if (value)
6622 push_member_name (constructor_fields);
6623 output_init_element (value, fieldtype, constructor_fields, 1);
6624 RESTORE_SPELLING_DEPTH (constructor_depth);
6626 else
6627 /* Do the bookkeeping for an element that was
6628 directly output as a constructor. */
6630 /* For a record, keep track of end position of last field. */
6631 tree temp = size_binop (PLUS_EXPR,
6632 DECL_FIELD_BITPOS (constructor_fields),
6633 DECL_SIZE (constructor_fields));
6634 TREE_INT_CST_LOW (constructor_bit_index)
6635 = TREE_INT_CST_LOW (temp);
6636 TREE_INT_CST_HIGH (constructor_bit_index)
6637 = TREE_INT_CST_HIGH (temp);
6639 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6642 constructor_fields = TREE_CHAIN (constructor_fields);
6643 /* Skip any nameless bit fields at the beginning. */
6644 while (constructor_fields != 0
6645 && DECL_C_BIT_FIELD (constructor_fields)
6646 && DECL_NAME (constructor_fields) == 0)
6647 constructor_fields = TREE_CHAIN (constructor_fields);
6648 break;
6650 if (TREE_CODE (constructor_type) == UNION_TYPE)
6652 tree fieldtype;
6653 enum tree_code fieldcode;
6655 if (constructor_fields == 0)
6657 pedwarn_init ("excess elements in union initializer%s",
6658 " after `%s'", NULL_PTR);
6659 break;
6662 fieldtype = TREE_TYPE (constructor_fields);
6663 if (fieldtype != error_mark_node)
6664 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6665 fieldcode = TREE_CODE (fieldtype);
6667 /* Accept a string constant to initialize a subarray. */
6668 if (value != 0
6669 && fieldcode == ARRAY_TYPE
6670 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6671 && string_flag)
6672 value = orig_value;
6673 /* Otherwise, if we have come to a subaggregate,
6674 and we don't have an element of its type, push into it. */
6675 else if (value != 0 && !constructor_no_implicit
6676 && value != error_mark_node
6677 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6678 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6679 || fieldcode == UNION_TYPE))
6681 push_init_level (1);
6682 continue;
6685 if (value)
6687 push_member_name (constructor_fields);
6688 output_init_element (value, fieldtype, constructor_fields, 1);
6689 RESTORE_SPELLING_DEPTH (constructor_depth);
6691 else
6692 /* Do the bookkeeping for an element that was
6693 directly output as a constructor. */
6695 TREE_INT_CST_LOW (constructor_bit_index)
6696 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6697 TREE_INT_CST_HIGH (constructor_bit_index)
6698 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6700 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6703 constructor_fields = 0;
6704 break;
6706 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6708 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6709 enum tree_code eltcode = TREE_CODE (elttype);
6711 /* Accept a string constant to initialize a subarray. */
6712 if (value != 0
6713 && eltcode == ARRAY_TYPE
6714 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6715 && string_flag)
6716 value = orig_value;
6717 /* Otherwise, if we have come to a subaggregate,
6718 and we don't have an element of its type, push into it. */
6719 else if (value != 0 && !constructor_no_implicit
6720 && value != error_mark_node
6721 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6722 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6723 || eltcode == UNION_TYPE))
6725 push_init_level (1);
6726 continue;
6729 if (constructor_max_index != 0
6730 && tree_int_cst_lt (constructor_max_index, constructor_index))
6732 pedwarn_init ("excess elements in array initializer%s",
6733 " after `%s'", NULL_PTR);
6734 break;
6737 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */
6738 if (constructor_range_end)
6740 if (constructor_max_index != 0
6741 && tree_int_cst_lt (constructor_max_index,
6742 constructor_range_end))
6744 pedwarn_init ("excess elements in array initializer%s",
6745 " after `%s'", NULL_PTR);
6746 TREE_INT_CST_HIGH (constructor_range_end)
6747 = TREE_INT_CST_HIGH (constructor_max_index);
6748 TREE_INT_CST_LOW (constructor_range_end)
6749 = TREE_INT_CST_LOW (constructor_max_index);
6752 value = save_expr (value);
6755 /* Now output the actual element.
6756 Ordinarily, output once.
6757 If there is a range, repeat it till we advance past the range. */
6760 tree tem;
6762 if (value)
6764 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6765 output_init_element (value, elttype, constructor_index, 1);
6766 RESTORE_SPELLING_DEPTH (constructor_depth);
6769 tem = size_binop (PLUS_EXPR, constructor_index,
6770 integer_one_node);
6771 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (tem);
6772 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (tem);
6774 if (!value)
6775 /* If we are doing the bookkeeping for an element that was
6776 directly output as a constructor,
6777 we must update constructor_unfilled_index. */
6779 TREE_INT_CST_LOW (constructor_unfilled_index)
6780 = TREE_INT_CST_LOW (constructor_index);
6781 TREE_INT_CST_HIGH (constructor_unfilled_index)
6782 = TREE_INT_CST_HIGH (constructor_index);
6785 while (! (constructor_range_end == 0
6786 || tree_int_cst_lt (constructor_range_end,
6787 constructor_index)));
6789 break;
6792 /* Handle the sole element allowed in a braced initializer
6793 for a scalar variable. */
6794 if (constructor_fields == 0)
6796 pedwarn_init ("excess elements in scalar initializer%s",
6797 " after `%s'", NULL_PTR);
6798 break;
6801 if (value)
6802 output_init_element (value, constructor_type, NULL_TREE, 1);
6803 constructor_fields = 0;
6804 break;
6807 /* If the (lexically) previous elments are not now saved,
6808 we can discard the storage for them. */
6809 if (constructor_incremental && constructor_pending_elts == 0 && value != 0
6810 && constructor_stack == 0)
6811 clear_momentary ();
6814 /* Expand an ASM statement with operands, handling output operands
6815 that are not variables or INDIRECT_REFS by transforming such
6816 cases into cases that expand_asm_operands can handle.
6818 Arguments are same as for expand_asm_operands. */
6820 void
6821 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6822 tree string, outputs, inputs, clobbers;
6823 int vol;
6824 char *filename;
6825 int line;
6827 int noutputs = list_length (outputs);
6828 register int i;
6829 /* o[I] is the place that output number I should be written. */
6830 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6831 register tree tail;
6833 if (TREE_CODE (string) == ADDR_EXPR)
6834 string = TREE_OPERAND (string, 0);
6835 if (TREE_CODE (string) != STRING_CST)
6837 error ("asm template is not a string constant");
6838 return;
6841 /* Record the contents of OUTPUTS before it is modified. */
6842 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6843 o[i] = TREE_VALUE (tail);
6845 /* Perform default conversions on array and function inputs. */
6846 /* Don't do this for other types--
6847 it would screw up operands expected to be in memory. */
6848 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6849 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6850 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6851 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6853 /* Generate the ASM_OPERANDS insn;
6854 store into the TREE_VALUEs of OUTPUTS some trees for
6855 where the values were actually stored. */
6856 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6858 /* Copy all the intermediate outputs into the specified outputs. */
6859 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6861 if (o[i] != TREE_VALUE (tail))
6863 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6864 0, VOIDmode, 0);
6865 free_temp_slots ();
6867 /* Detect modification of read-only values.
6868 (Otherwise done by build_modify_expr.) */
6869 else
6871 tree type = TREE_TYPE (o[i]);
6872 if (TREE_READONLY (o[i])
6873 || TYPE_READONLY (type)
6874 || ((TREE_CODE (type) == RECORD_TYPE
6875 || TREE_CODE (type) == UNION_TYPE)
6876 && C_TYPE_FIELDS_READONLY (type)))
6877 readonly_warning (o[i], "modification by `asm'");
6881 /* Those MODIFY_EXPRs could do autoincrements. */
6882 emit_queue ();
6885 /* Expand a C `return' statement.
6886 RETVAL is the expression for what to return,
6887 or a null pointer for `return;' with no value. */
6889 void
6890 c_expand_return (retval)
6891 tree retval;
6893 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6895 if (TREE_THIS_VOLATILE (current_function_decl))
6896 warning ("function declared `noreturn' has a `return' statement");
6898 if (!retval)
6900 current_function_returns_null = 1;
6901 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6902 warning ("`return' with no value, in function returning non-void");
6903 expand_null_return ();
6905 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6907 current_function_returns_null = 1;
6908 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6909 pedwarn ("`return' with a value, in function returning void");
6910 expand_return (retval);
6912 else
6914 tree t = convert_for_assignment (valtype, retval, "return",
6915 NULL_TREE, NULL_TREE, 0);
6916 tree res = DECL_RESULT (current_function_decl);
6917 tree inner;
6919 if (t == error_mark_node)
6920 return;
6922 inner = t = convert (TREE_TYPE (res), t);
6924 /* Strip any conversions, additions, and subtractions, and see if
6925 we are returning the address of a local variable. Warn if so. */
6926 while (1)
6928 switch (TREE_CODE (inner))
6930 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6931 case PLUS_EXPR:
6932 inner = TREE_OPERAND (inner, 0);
6933 continue;
6935 case MINUS_EXPR:
6936 /* If the second operand of the MINUS_EXPR has a pointer
6937 type (or is converted from it), this may be valid, so
6938 don't give a warning. */
6940 tree op1 = TREE_OPERAND (inner, 1);
6942 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6943 && (TREE_CODE (op1) == NOP_EXPR
6944 || TREE_CODE (op1) == NON_LVALUE_EXPR
6945 || TREE_CODE (op1) == CONVERT_EXPR))
6946 op1 = TREE_OPERAND (op1, 0);
6948 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6949 break;
6951 inner = TREE_OPERAND (inner, 0);
6952 continue;
6955 case ADDR_EXPR:
6956 inner = TREE_OPERAND (inner, 0);
6958 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6959 inner = TREE_OPERAND (inner, 0);
6961 if (TREE_CODE (inner) == VAR_DECL
6962 && ! DECL_EXTERNAL (inner)
6963 && ! TREE_STATIC (inner)
6964 && DECL_CONTEXT (inner) == current_function_decl)
6965 warning ("function returns address of local variable");
6966 break;
6968 default:
6969 break;
6972 break;
6975 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6976 TREE_SIDE_EFFECTS (t) = 1;
6977 expand_return (t);
6978 current_function_returns_value = 1;
6982 /* Start a C switch statement, testing expression EXP.
6983 Return EXP if it is valid, an error node otherwise. */
6985 tree
6986 c_expand_start_case (exp)
6987 tree exp;
6989 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6990 tree type = TREE_TYPE (exp);
6992 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6994 error ("switch quantity not an integer");
6995 exp = error_mark_node;
6997 else
6999 tree index;
7000 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7002 if (warn_traditional
7003 && (type == long_integer_type_node
7004 || type == long_unsigned_type_node))
7005 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
7007 exp = default_conversion (exp);
7008 type = TREE_TYPE (exp);
7009 index = get_unwidened (exp, NULL_TREE);
7010 /* We can't strip a conversion from a signed type to an unsigned,
7011 because if we did, int_fits_type_p would do the wrong thing
7012 when checking case values for being in range,
7013 and it's too hard to do the right thing. */
7014 if (TREE_UNSIGNED (TREE_TYPE (exp))
7015 == TREE_UNSIGNED (TREE_TYPE (index)))
7016 exp = index;
7019 expand_start_case (1, exp, type, "switch statement");
7021 return exp;