(ASM_SPEC): Change {% to %{.
[official-gcc.git] / gcc / c-typeck.c
blob9e97bffe006633cb41201cc69a3c2695aa55fe78
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 88, 91, 92-5, 1996 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 /* Nonzero if we've already printed a "missing braces around initializer"
39 message within this initializer. */
40 static int missing_braces_mentioned;
42 extern char *index ();
43 extern char *rindex ();
45 static tree qualify_type PROTO((tree, tree));
46 static int comp_target_types PROTO((tree, tree));
47 static int function_types_compatible_p PROTO((tree, tree));
48 static int type_lists_compatible_p PROTO((tree, tree));
49 static int self_promoting_type_p PROTO((tree));
50 static tree decl_constant_value PROTO((tree));
51 static tree lookup_field PROTO((tree, tree, tree *));
52 static tree convert_arguments PROTO((tree, tree, tree, tree));
53 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
54 static tree pointer_diff PROTO((tree, tree));
55 static tree unary_complex_lvalue PROTO((enum tree_code, tree));
56 static void pedantic_lvalue_warning PROTO((enum tree_code));
57 static tree internal_build_compound_expr PROTO((tree, int));
58 static tree convert_for_assignment PROTO((tree, tree, char *, tree,
59 tree, int));
60 static void warn_for_assignment PROTO((char *, char *, tree, int));
61 static tree valid_compound_expr_initializer PROTO((tree, tree));
62 static void push_string PROTO((char *));
63 static void push_member_name PROTO((tree));
64 static void push_array_bounds PROTO((int));
65 static int spelling_length PROTO((void));
66 static char *print_spelling PROTO((char *));
67 static char *get_spelling PROTO((char *));
68 static void warning_init PROTO((char *, char *,
69 char *));
70 static tree digest_init PROTO((tree, tree, int, int));
71 static void check_init_type_bitfields PROTO((tree));
72 static void output_init_element PROTO((tree, tree, tree, int));
73 static void output_pending_init_elements PROTO((int));
75 /* Do `exp = require_complete_type (exp);' to make sure exp
76 does not have an incomplete type. (That includes void types.) */
78 tree
79 require_complete_type (value)
80 tree value;
82 tree type = TREE_TYPE (value);
84 /* First, detect a valid value with a complete type. */
85 if (TYPE_SIZE (type) != 0
86 && type != void_type_node)
87 return value;
89 incomplete_type_error (value, type);
90 return error_mark_node;
93 /* Print an error message for invalid use of an incomplete type.
94 VALUE is the expression that was used (or 0 if that isn't known)
95 and TYPE is the type that was invalid. */
97 void
98 incomplete_type_error (value, type)
99 tree value;
100 tree type;
102 char *errmsg;
104 /* Avoid duplicate error message. */
105 if (TREE_CODE (type) == ERROR_MARK)
106 return;
108 if (value != 0 && (TREE_CODE (value) == VAR_DECL
109 || TREE_CODE (value) == PARM_DECL))
110 error ("`%s' has an incomplete type",
111 IDENTIFIER_POINTER (DECL_NAME (value)));
112 else
114 retry:
115 /* We must print an error message. Be clever about what it says. */
117 switch (TREE_CODE (type))
119 case RECORD_TYPE:
120 errmsg = "invalid use of undefined type `struct %s'";
121 break;
123 case UNION_TYPE:
124 errmsg = "invalid use of undefined type `union %s'";
125 break;
127 case ENUMERAL_TYPE:
128 errmsg = "invalid use of undefined type `enum %s'";
129 break;
131 case VOID_TYPE:
132 error ("invalid use of void expression");
133 return;
135 case ARRAY_TYPE:
136 if (TYPE_DOMAIN (type))
138 type = TREE_TYPE (type);
139 goto retry;
141 error ("invalid use of array with unspecified bounds");
142 return;
144 default:
145 abort ();
148 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
149 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
150 else
151 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
152 error ("invalid use of incomplete typedef `%s'",
153 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
157 /* Return a variant of TYPE which has all the type qualifiers of LIKE
158 as well as those of TYPE. */
160 static tree
161 qualify_type (type, like)
162 tree type, like;
164 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
165 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
166 return c_build_type_variant (type, constflag, volflag);
169 /* Return the common type of two types.
170 We assume that comptypes has already been done and returned 1;
171 if that isn't so, this may crash. In particular, we assume that qualifiers
172 match.
174 This is the type for the result of most arithmetic operations
175 if the operands have the given two types. */
177 tree
178 common_type (t1, t2)
179 tree t1, t2;
181 register enum tree_code code1;
182 register enum tree_code code2;
183 tree attributes;
185 /* Save time if the two types are the same. */
187 if (t1 == t2) return t1;
189 /* If one type is nonsense, use the other. */
190 if (t1 == error_mark_node)
191 return t2;
192 if (t2 == error_mark_node)
193 return t1;
195 /* Merge the attributes */
196 attributes = merge_attributes (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
198 /* Treat an enum type as the unsigned integer type of the same width. */
200 if (TREE_CODE (t1) == ENUMERAL_TYPE)
201 t1 = type_for_size (TYPE_PRECISION (t1), 1);
202 if (TREE_CODE (t2) == ENUMERAL_TYPE)
203 t2 = type_for_size (TYPE_PRECISION (t2), 1);
205 code1 = TREE_CODE (t1);
206 code2 = TREE_CODE (t2);
208 /* If one type is complex, form the common type of the non-complex
209 components, then make that complex. Use T1 or T2 if it is the
210 required type. */
211 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
213 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
214 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
215 tree subtype = common_type (subtype1, subtype2);
217 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
218 return build_type_attribute_variant (t1, attributes);
219 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
220 return build_type_attribute_variant (t2, attributes);
221 else
222 return build_type_attribute_variant (build_complex_type (subtype),
223 attributes);
226 switch (code1)
228 case INTEGER_TYPE:
229 case REAL_TYPE:
230 /* If only one is real, use it as the result. */
232 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
233 return build_type_attribute_variant (t1, attributes);
235 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
236 return build_type_attribute_variant (t2, attributes);
238 /* Both real or both integers; use the one with greater precision. */
240 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
241 return build_type_attribute_variant (t1, attributes);
242 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
243 return build_type_attribute_variant (t2, attributes);
245 /* Same precision. Prefer longs to ints even when same size. */
247 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
248 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
249 return build_type_attribute_variant (long_unsigned_type_node,
250 attributes);
252 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
253 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
255 /* But preserve unsignedness from the other type,
256 since long cannot hold all the values of an unsigned int. */
257 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
258 t1 = long_unsigned_type_node;
259 else
260 t1 = long_integer_type_node;
261 return build_type_attribute_variant (t1, attributes);
264 /* Otherwise prefer the unsigned one. */
266 if (TREE_UNSIGNED (t1))
267 return build_type_attribute_variant (t1, attributes);
268 else
269 return build_type_attribute_variant (t2, attributes);
271 case POINTER_TYPE:
272 /* For two pointers, do this recursively on the target type,
273 and combine the qualifiers of the two types' targets. */
274 /* This code was turned off; I don't know why.
275 But ANSI C specifies doing this with the qualifiers.
276 So I turned it on again. */
278 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
279 TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
280 int constp
281 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
282 int volatilep
283 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
284 t1 = build_pointer_type (c_build_type_variant (target, constp,
285 volatilep));
286 return build_type_attribute_variant (t1, attributes);
288 #if 0
289 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
290 return build_type_attribute_variant (t1, attributes);
291 #endif
293 case ARRAY_TYPE:
295 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
296 /* Save space: see if the result is identical to one of the args. */
297 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
298 return build_type_attribute_variant (t1, attributes);
299 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
300 return build_type_attribute_variant (t2, attributes);
301 /* Merge the element types, and have a size if either arg has one. */
302 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
303 return build_type_attribute_variant (t1, attributes);
306 case FUNCTION_TYPE:
307 /* Function types: prefer the one that specified arg types.
308 If both do, merge the arg types. Also merge the return types. */
310 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
311 tree p1 = TYPE_ARG_TYPES (t1);
312 tree p2 = TYPE_ARG_TYPES (t2);
313 int len;
314 tree newargs, n;
315 int i;
317 /* Save space: see if the result is identical to one of the args. */
318 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
319 return build_type_attribute_variant (t1, attributes);
320 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
321 return build_type_attribute_variant (t2, attributes);
323 /* Simple way if one arg fails to specify argument types. */
324 if (TYPE_ARG_TYPES (t1) == 0)
326 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
327 return build_type_attribute_variant (t1, attributes);
329 if (TYPE_ARG_TYPES (t2) == 0)
331 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
332 return build_type_attribute_variant (t1, attributes);
335 /* If both args specify argument types, we must merge the two
336 lists, argument by argument. */
338 len = list_length (p1);
339 newargs = 0;
341 for (i = 0; i < len; i++)
342 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
344 n = newargs;
346 for (; p1;
347 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
349 /* A null type means arg type is not specified.
350 Take whatever the other function type has. */
351 if (TREE_VALUE (p1) == 0)
353 TREE_VALUE (n) = TREE_VALUE (p2);
354 goto parm_done;
356 if (TREE_VALUE (p2) == 0)
358 TREE_VALUE (n) = TREE_VALUE (p1);
359 goto parm_done;
362 /* Given wait (union {union wait *u; int *i} *)
363 and wait (union wait *),
364 prefer union wait * as type of parm. */
365 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
366 && TREE_VALUE (p1) != TREE_VALUE (p2))
368 tree memb;
369 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
370 memb; memb = TREE_CHAIN (memb))
371 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
373 TREE_VALUE (n) = TREE_VALUE (p2);
374 if (pedantic)
375 pedwarn ("function types not truly compatible in ANSI C");
376 goto parm_done;
379 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
380 && TREE_VALUE (p2) != TREE_VALUE (p1))
382 tree memb;
383 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
384 memb; memb = TREE_CHAIN (memb))
385 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
387 TREE_VALUE (n) = TREE_VALUE (p1);
388 if (pedantic)
389 pedwarn ("function types not truly compatible in ANSI C");
390 goto parm_done;
393 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
394 parm_done: ;
397 t1 = build_function_type (valtype, newargs);
398 /* ... falls through ... */
401 default:
402 return build_type_attribute_variant (t1, attributes);
407 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
408 or various other operations. Return 2 if they are compatible
409 but a warning may be needed if you use them together. */
412 comptypes (type1, type2)
413 tree type1, type2;
415 register tree t1 = type1;
416 register tree t2 = type2;
417 int attrval, val;
419 /* Suppress errors caused by previously reported errors. */
421 if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
422 return 1;
424 /* Treat an enum type as the integer type of the same width and
425 signedness. */
427 if (TREE_CODE (t1) == ENUMERAL_TYPE)
428 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
429 if (TREE_CODE (t2) == ENUMERAL_TYPE)
430 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
432 if (t1 == t2)
433 return 1;
435 /* Different classes of types can't be compatible. */
437 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
439 /* Qualifiers must match. */
441 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
442 return 0;
443 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
444 return 0;
446 /* Allow for two different type nodes which have essentially the same
447 definition. Note that we already checked for equality of the type
448 type qualifiers (just above). */
450 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
451 return 1;
453 #ifndef COMP_TYPE_ATTRIBUTES
454 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
455 #endif
457 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
458 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
459 return 0;
461 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
462 val = 0;
464 switch (TREE_CODE (t1))
466 case POINTER_TYPE:
467 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
468 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
469 break;
471 case FUNCTION_TYPE:
472 val = function_types_compatible_p (t1, t2);
473 break;
475 case ARRAY_TYPE:
477 tree d1 = TYPE_DOMAIN (t1);
478 tree d2 = TYPE_DOMAIN (t2);
479 val = 1;
481 /* Target types must match incl. qualifiers. */
482 if (TREE_TYPE (t1) != TREE_TYPE (t2)
483 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
484 return 0;
486 /* Sizes must match unless one is missing or variable. */
487 if (d1 == 0 || d2 == 0 || d1 == d2
488 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
489 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
490 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
491 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
492 break;
494 if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
495 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
496 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
497 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
498 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
499 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
500 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
501 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2)))))
502 val = 0;
503 break;
506 case RECORD_TYPE:
507 if (maybe_objc_comptypes (t1, t2, 0) == 1)
508 val = 1;
509 break;
511 return attrval == 2 && val == 1 ? 2 : val;
514 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
515 ignoring their qualifiers. */
517 static int
518 comp_target_types (ttl, ttr)
519 tree ttl, ttr;
521 int val;
523 /* Give maybe_objc_comptypes a crack at letting these types through. */
524 if (val = maybe_objc_comptypes (ttl, ttr, 1) >= 0)
525 return val;
527 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
528 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
530 if (val == 2 && pedantic)
531 pedwarn ("types are not quite compatible");
532 return val;
535 /* Subroutines of `comptypes'. */
537 /* Return 1 if two function types F1 and F2 are compatible.
538 If either type specifies no argument types,
539 the other must specify a fixed number of self-promoting arg types.
540 Otherwise, if one type specifies only the number of arguments,
541 the other must specify that number of self-promoting arg types.
542 Otherwise, the argument types must match. */
544 static int
545 function_types_compatible_p (f1, f2)
546 tree f1, f2;
548 tree args1, args2;
549 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
550 int val = 1;
551 int val1;
553 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
554 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
555 return 0;
557 args1 = TYPE_ARG_TYPES (f1);
558 args2 = TYPE_ARG_TYPES (f2);
560 /* An unspecified parmlist matches any specified parmlist
561 whose argument types don't need default promotions. */
563 if (args1 == 0)
565 if (!self_promoting_args_p (args2))
566 return 0;
567 /* If one of these types comes from a non-prototype fn definition,
568 compare that with the other type's arglist.
569 If they don't match, ask for a warning (but no error). */
570 if (TYPE_ACTUAL_ARG_TYPES (f1)
571 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
572 val = 2;
573 return val;
575 if (args2 == 0)
577 if (!self_promoting_args_p (args1))
578 return 0;
579 if (TYPE_ACTUAL_ARG_TYPES (f2)
580 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
581 val = 2;
582 return val;
585 /* Both types have argument lists: compare them and propagate results. */
586 val1 = type_lists_compatible_p (args1, args2);
587 return val1 != 1 ? val1 : val;
590 /* Check two lists of types for compatibility,
591 returning 0 for incompatible, 1 for compatible,
592 or 2 for compatible with warning. */
594 static int
595 type_lists_compatible_p (args1, args2)
596 tree args1, args2;
598 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
599 int val = 1;
600 int newval = 0;
602 while (1)
604 if (args1 == 0 && args2 == 0)
605 return val;
606 /* If one list is shorter than the other,
607 they fail to match. */
608 if (args1 == 0 || args2 == 0)
609 return 0;
610 /* A null pointer instead of a type
611 means there is supposed to be an argument
612 but nothing is specified about what type it has.
613 So match anything that self-promotes. */
614 if (TREE_VALUE (args1) == 0)
616 if (! self_promoting_type_p (TREE_VALUE (args2)))
617 return 0;
619 else if (TREE_VALUE (args2) == 0)
621 if (! self_promoting_type_p (TREE_VALUE (args1)))
622 return 0;
624 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
626 /* Allow wait (union {union wait *u; int *i} *)
627 and wait (union wait *) to be compatible. */
628 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
629 && (TYPE_NAME (TREE_VALUE (args1)) == 0
630 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
631 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
632 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
633 TYPE_SIZE (TREE_VALUE (args2))))
635 tree memb;
636 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
637 memb; memb = TREE_CHAIN (memb))
638 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
639 break;
640 if (memb == 0)
641 return 0;
643 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
644 && (TYPE_NAME (TREE_VALUE (args2)) == 0
645 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
646 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
647 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
648 TYPE_SIZE (TREE_VALUE (args1))))
650 tree memb;
651 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
652 memb; memb = TREE_CHAIN (memb))
653 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
654 break;
655 if (memb == 0)
656 return 0;
658 else
659 return 0;
662 /* comptypes said ok, but record if it said to warn. */
663 if (newval > val)
664 val = newval;
666 args1 = TREE_CHAIN (args1);
667 args2 = TREE_CHAIN (args2);
671 /* Return 1 if PARMS specifies a fixed number of parameters
672 and none of their types is affected by default promotions. */
675 self_promoting_args_p (parms)
676 tree parms;
678 register tree t;
679 for (t = parms; t; t = TREE_CHAIN (t))
681 register tree type = TREE_VALUE (t);
683 if (TREE_CHAIN (t) == 0 && type != void_type_node)
684 return 0;
686 if (type == 0)
687 return 0;
689 if (TYPE_MAIN_VARIANT (type) == float_type_node)
690 return 0;
692 if (C_PROMOTING_INTEGER_TYPE_P (type))
693 return 0;
695 return 1;
698 /* Return 1 if TYPE is not affected by default promotions. */
700 static int
701 self_promoting_type_p (type)
702 tree type;
704 if (TYPE_MAIN_VARIANT (type) == float_type_node)
705 return 0;
707 if (C_PROMOTING_INTEGER_TYPE_P (type))
708 return 0;
710 return 1;
713 /* Return an unsigned type the same as TYPE in other respects. */
715 tree
716 unsigned_type (type)
717 tree type;
719 tree type1 = TYPE_MAIN_VARIANT (type);
720 if (type1 == signed_char_type_node || type1 == char_type_node)
721 return unsigned_char_type_node;
722 if (type1 == integer_type_node)
723 return unsigned_type_node;
724 if (type1 == short_integer_type_node)
725 return short_unsigned_type_node;
726 if (type1 == long_integer_type_node)
727 return long_unsigned_type_node;
728 if (type1 == long_long_integer_type_node)
729 return long_long_unsigned_type_node;
730 if (type1 == intDI_type_node)
731 return unsigned_intDI_type_node;
732 if (type1 == intSI_type_node)
733 return unsigned_intSI_type_node;
734 if (type1 == intHI_type_node)
735 return unsigned_intHI_type_node;
736 if (type1 == intQI_type_node)
737 return unsigned_intQI_type_node;
738 return type;
741 /* Return a signed type the same as TYPE in other respects. */
743 tree
744 signed_type (type)
745 tree type;
747 tree type1 = TYPE_MAIN_VARIANT (type);
748 if (type1 == unsigned_char_type_node || type1 == char_type_node)
749 return signed_char_type_node;
750 if (type1 == unsigned_type_node)
751 return integer_type_node;
752 if (type1 == short_unsigned_type_node)
753 return short_integer_type_node;
754 if (type1 == long_unsigned_type_node)
755 return long_integer_type_node;
756 if (type1 == long_long_unsigned_type_node)
757 return long_long_integer_type_node;
758 if (type1 == unsigned_intDI_type_node)
759 return intDI_type_node;
760 if (type1 == unsigned_intSI_type_node)
761 return intSI_type_node;
762 if (type1 == unsigned_intHI_type_node)
763 return intHI_type_node;
764 if (type1 == unsigned_intQI_type_node)
765 return intQI_type_node;
766 return type;
769 /* Return a type the same as TYPE except unsigned or
770 signed according to UNSIGNEDP. */
772 tree
773 signed_or_unsigned_type (unsignedp, type)
774 int unsignedp;
775 tree type;
777 if (! INTEGRAL_TYPE_P (type))
778 return type;
779 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
780 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
781 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
782 return unsignedp ? unsigned_type_node : integer_type_node;
783 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
784 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
785 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
786 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
787 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
788 return (unsignedp ? long_long_unsigned_type_node
789 : long_long_integer_type_node);
790 return type;
793 /* Compute the value of the `sizeof' operator. */
795 tree
796 c_sizeof (type)
797 tree type;
799 enum tree_code code = TREE_CODE (type);
800 tree t;
802 if (code == FUNCTION_TYPE)
804 if (pedantic || warn_pointer_arith)
805 pedwarn ("sizeof applied to a function type");
806 return size_int (1);
808 if (code == VOID_TYPE)
810 if (pedantic || warn_pointer_arith)
811 pedwarn ("sizeof applied to a void type");
812 return size_int (1);
814 if (code == ERROR_MARK)
815 return size_int (1);
816 if (TYPE_SIZE (type) == 0)
818 error ("sizeof applied to an incomplete type");
819 return size_int (0);
822 /* Convert in case a char is more than one unit. */
823 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
824 size_int (TYPE_PRECISION (char_type_node)));
825 /* size_binop does not put the constant in range, so do it now. */
826 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
827 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
828 return t;
831 tree
832 c_sizeof_nowarn (type)
833 tree type;
835 enum tree_code code = TREE_CODE (type);
836 tree t;
838 if (code == FUNCTION_TYPE
839 || code == VOID_TYPE
840 || code == ERROR_MARK)
841 return size_int (1);
842 if (TYPE_SIZE (type) == 0)
843 return size_int (0);
845 /* Convert in case a char is more than one unit. */
846 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
847 size_int (TYPE_PRECISION (char_type_node)));
848 force_fit_type (t, 0);
849 return t;
852 /* Compute the size to increment a pointer by. */
854 tree
855 c_size_in_bytes (type)
856 tree type;
858 enum tree_code code = TREE_CODE (type);
859 tree t;
861 if (code == FUNCTION_TYPE)
862 return size_int (1);
863 if (code == VOID_TYPE)
864 return size_int (1);
865 if (code == ERROR_MARK)
866 return size_int (1);
867 if (TYPE_SIZE (type) == 0)
869 error ("arithmetic on pointer to an incomplete type");
870 return size_int (1);
873 /* Convert in case a char is more than one unit. */
874 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
875 size_int (BITS_PER_UNIT));
876 force_fit_type (t, 0);
877 return t;
880 /* Implement the __alignof keyword: Return the minimum required
881 alignment of TYPE, measured in bytes. */
883 tree
884 c_alignof (type)
885 tree type;
887 enum tree_code code = TREE_CODE (type);
889 if (code == FUNCTION_TYPE)
890 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
892 if (code == VOID_TYPE || code == ERROR_MARK)
893 return size_int (1);
895 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
898 /* Implement the __alignof keyword: Return the minimum required
899 alignment of EXPR, measured in bytes. For VAR_DECL's and
900 FIELD_DECL's return DECL_ALIGN (which can be set from an
901 "aligned" __attribute__ specification). */
903 tree
904 c_alignof_expr (expr)
905 tree expr;
907 if (TREE_CODE (expr) == VAR_DECL)
908 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
910 if (TREE_CODE (expr) == COMPONENT_REF
911 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
913 error ("`__alignof' applied to a bit-field");
914 return size_int (1);
916 else if (TREE_CODE (expr) == COMPONENT_REF
917 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
918 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
920 if (TREE_CODE (expr) == INDIRECT_REF)
922 tree t = TREE_OPERAND (expr, 0);
923 tree best = t;
924 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
926 while (TREE_CODE (t) == NOP_EXPR
927 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
929 int thisalign;
931 t = TREE_OPERAND (t, 0);
932 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
933 if (thisalign > bestalign)
934 best = t, bestalign = thisalign;
936 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
938 else
939 return c_alignof (TREE_TYPE (expr));
941 /* Return either DECL or its known constant value (if it has one). */
943 static tree
944 decl_constant_value (decl)
945 tree decl;
947 if (! TREE_PUBLIC (decl)
948 /* Don't change a variable array bound or initial value to a constant
949 in a place where a variable is invalid. */
950 && current_function_decl != 0
951 && ! pedantic
952 && ! TREE_THIS_VOLATILE (decl)
953 && TREE_READONLY (decl) && ! ITERATOR_P (decl)
954 && DECL_INITIAL (decl) != 0
955 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
956 /* This is invalid if initial value is not constant.
957 If it has either a function call, a memory reference,
958 or a variable, then re-evaluating it could give different results. */
959 && TREE_CONSTANT (DECL_INITIAL (decl))
960 /* Check for cases where this is sub-optimal, even though valid. */
961 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
962 && DECL_MODE (decl) != BLKmode)
963 return DECL_INITIAL (decl);
964 return decl;
967 /* Perform default promotions for C data used in expressions.
968 Arrays and functions are converted to pointers;
969 enumeral types or short or char, to int.
970 In addition, manifest constants symbols are replaced by their values. */
972 tree
973 default_conversion (exp)
974 tree exp;
976 register tree type = TREE_TYPE (exp);
977 register enum tree_code code = TREE_CODE (type);
979 /* Constants can be used directly unless they're not loadable. */
980 if (TREE_CODE (exp) == CONST_DECL)
981 exp = DECL_INITIAL (exp);
983 /* Replace a nonvolatile const static variable with its value unless
984 it is an array, in which case we must be sure that taking the
985 address of the array produces consistent results. */
986 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
988 exp = decl_constant_value (exp);
989 type = TREE_TYPE (exp);
992 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
993 an lvalue. */
994 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
995 to integer and cause infinite recursion. */
996 while (TREE_CODE (exp) == NON_LVALUE_EXPR
997 || (TREE_CODE (exp) == NOP_EXPR
998 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
999 exp = TREE_OPERAND (exp, 0);
1001 /* Normally convert enums to int,
1002 but convert wide enums to something wider. */
1003 if (code == ENUMERAL_TYPE)
1005 type = type_for_size (MAX (TYPE_PRECISION (type),
1006 TYPE_PRECISION (integer_type_node)),
1007 ((flag_traditional
1008 || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node))
1009 && TREE_UNSIGNED (type)));
1010 return convert (type, exp);
1013 if (TREE_CODE (exp) == COMPONENT_REF
1014 && DECL_BIT_FIELD (TREE_OPERAND (exp, 1)))
1016 tree width = DECL_SIZE (TREE_OPERAND (exp, 1));
1017 HOST_WIDE_INT low = TREE_INT_CST_LOW (width);
1019 /* If it's thinner than an int, promote it like a
1020 C_PROMOTING_INTEGER_TYPE_P, otherwise leave it alone. */
1022 if (low < TYPE_PRECISION (integer_type_node))
1024 if ( flag_traditional && TREE_UNSIGNED (type))
1025 return convert (unsigned_type_node, exp);
1026 else
1027 return convert (integer_type_node, exp);
1031 if (C_PROMOTING_INTEGER_TYPE_P (type))
1033 /* Traditionally, unsignedness is preserved in default promotions.
1034 Also preserve unsignedness if not really getting any wider. */
1035 if (TREE_UNSIGNED (type)
1036 && (flag_traditional
1037 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1038 return convert (unsigned_type_node, exp);
1039 return convert (integer_type_node, exp);
1041 if (flag_traditional && !flag_allow_single_precision
1042 && TYPE_MAIN_VARIANT (type) == float_type_node)
1043 return convert (double_type_node, exp);
1044 if (code == VOID_TYPE)
1046 error ("void value not ignored as it ought to be");
1047 return error_mark_node;
1049 if (code == FUNCTION_TYPE)
1051 return build_unary_op (ADDR_EXPR, exp, 0);
1053 if (code == ARRAY_TYPE)
1055 register tree adr;
1056 tree restype = TREE_TYPE (type);
1057 tree ptrtype;
1058 int constp = 0;
1059 int volatilep = 0;
1061 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1062 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1064 constp = TREE_READONLY (exp);
1065 volatilep = TREE_THIS_VOLATILE (exp);
1068 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1069 || constp || volatilep)
1070 restype = c_build_type_variant (restype,
1071 TYPE_READONLY (type) || constp,
1072 TYPE_VOLATILE (type) || volatilep);
1074 if (TREE_CODE (exp) == INDIRECT_REF)
1075 return convert (TYPE_POINTER_TO (restype),
1076 TREE_OPERAND (exp, 0));
1078 if (TREE_CODE (exp) == COMPOUND_EXPR)
1080 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1081 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1082 TREE_OPERAND (exp, 0), op1);
1085 if (!lvalue_p (exp)
1086 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1088 error ("invalid use of non-lvalue array");
1089 return error_mark_node;
1092 ptrtype = build_pointer_type (restype);
1094 if (TREE_CODE (exp) == VAR_DECL)
1096 /* ??? This is not really quite correct
1097 in that the type of the operand of ADDR_EXPR
1098 is not the target type of the type of the ADDR_EXPR itself.
1099 Question is, can this lossage be avoided? */
1100 adr = build1 (ADDR_EXPR, ptrtype, exp);
1101 if (mark_addressable (exp) == 0)
1102 return error_mark_node;
1103 TREE_CONSTANT (adr) = staticp (exp);
1104 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1105 return adr;
1107 /* This way is better for a COMPONENT_REF since it can
1108 simplify the offset for a component. */
1109 adr = build_unary_op (ADDR_EXPR, exp, 1);
1110 return convert (ptrtype, adr);
1112 return exp;
1115 /* Look up component name in the structure type definition.
1117 If this component name is found indirectly within an anonymous union,
1118 store in *INDIRECT the component which directly contains
1119 that anonymous union. Otherwise, set *INDIRECT to 0. */
1121 static tree
1122 lookup_field (type, component, indirect)
1123 tree type, component;
1124 tree *indirect;
1126 tree field;
1128 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1129 to the field elements. Use a binary search on this array to quickly
1130 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1131 will always be set for structures which have many elements. */
1133 if (TYPE_LANG_SPECIFIC (type))
1135 int bot, top, half;
1136 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1138 field = TYPE_FIELDS (type);
1139 bot = 0;
1140 top = TYPE_LANG_SPECIFIC (type)->len;
1141 while (top - bot > 1)
1143 half = (top - bot + 1) >> 1;
1144 field = field_array[bot+half];
1146 if (DECL_NAME (field) == NULL_TREE)
1148 /* Step through all anon unions in linear fashion. */
1149 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1151 tree anon, junk;
1153 field = field_array[bot++];
1154 anon = lookup_field (TREE_TYPE (field), component, &junk);
1155 if (anon != NULL_TREE)
1157 *indirect = field;
1158 return anon;
1162 /* Entire record is only anon unions. */
1163 if (bot > top)
1164 return NULL_TREE;
1166 /* Restart the binary search, with new lower bound. */
1167 continue;
1170 if (DECL_NAME (field) == component)
1171 break;
1172 if (DECL_NAME (field) < component)
1173 bot += half;
1174 else
1175 top = bot + half;
1178 if (DECL_NAME (field_array[bot]) == component)
1179 field = field_array[bot];
1180 else if (DECL_NAME (field) != component)
1181 field = 0;
1183 else
1185 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1187 if (DECL_NAME (field) == NULL_TREE)
1189 tree junk;
1190 tree anon = lookup_field (TREE_TYPE (field), component, &junk);
1191 if (anon != NULL_TREE)
1193 *indirect = field;
1194 return anon;
1198 if (DECL_NAME (field) == component)
1199 break;
1203 *indirect = NULL_TREE;
1204 return field;
1207 /* Make an expression to refer to the COMPONENT field of
1208 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1210 tree
1211 build_component_ref (datum, component)
1212 tree datum, component;
1214 register tree type = TREE_TYPE (datum);
1215 register enum tree_code code = TREE_CODE (type);
1216 register tree field = NULL;
1217 register tree ref;
1219 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1220 unless we are not to support things not strictly ANSI. */
1221 switch (TREE_CODE (datum))
1223 case COMPOUND_EXPR:
1225 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1226 return build (COMPOUND_EXPR, TREE_TYPE (value),
1227 TREE_OPERAND (datum, 0), value);
1229 case COND_EXPR:
1230 return build_conditional_expr
1231 (TREE_OPERAND (datum, 0),
1232 build_component_ref (TREE_OPERAND (datum, 1), component),
1233 build_component_ref (TREE_OPERAND (datum, 2), component));
1236 /* See if there is a field or component with name COMPONENT. */
1238 if (code == RECORD_TYPE || code == UNION_TYPE)
1240 tree indirect = 0;
1242 if (TYPE_SIZE (type) == 0)
1244 incomplete_type_error (NULL_TREE, type);
1245 return error_mark_node;
1248 field = lookup_field (type, component, &indirect);
1250 if (!field)
1252 error (code == RECORD_TYPE
1253 ? "structure has no member named `%s'"
1254 : "union has no member named `%s'",
1255 IDENTIFIER_POINTER (component));
1256 return error_mark_node;
1258 if (TREE_TYPE (field) == error_mark_node)
1259 return error_mark_node;
1261 /* If FIELD was found buried within an anonymous union,
1262 make one COMPONENT_REF to get that anonymous union,
1263 then fall thru to make a second COMPONENT_REF to get FIELD. */
1264 if (indirect != 0)
1266 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1267 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1268 TREE_READONLY (ref) = 1;
1269 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1270 TREE_THIS_VOLATILE (ref) = 1;
1271 datum = ref;
1274 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1276 if (TREE_READONLY (datum) || TREE_READONLY (field))
1277 TREE_READONLY (ref) = 1;
1278 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1279 TREE_THIS_VOLATILE (ref) = 1;
1281 return ref;
1283 else if (code != ERROR_MARK)
1284 error ("request for member `%s' in something not a structure or union",
1285 IDENTIFIER_POINTER (component));
1287 return error_mark_node;
1290 /* Given an expression PTR for a pointer, return an expression
1291 for the value pointed to.
1292 ERRORSTRING is the name of the operator to appear in error messages. */
1294 tree
1295 build_indirect_ref (ptr, errorstring)
1296 tree ptr;
1297 char *errorstring;
1299 register tree pointer = default_conversion (ptr);
1300 register tree type = TREE_TYPE (pointer);
1302 if (TREE_CODE (type) == POINTER_TYPE)
1304 if (TREE_CODE (pointer) == ADDR_EXPR
1305 && !flag_volatile
1306 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1307 == TREE_TYPE (type)))
1308 return TREE_OPERAND (pointer, 0);
1309 else
1311 tree t = TREE_TYPE (type);
1312 register tree ref = build1 (INDIRECT_REF,
1313 TYPE_MAIN_VARIANT (t), pointer);
1315 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1317 error ("dereferencing pointer to incomplete type");
1318 return error_mark_node;
1320 if (TREE_CODE (t) == VOID_TYPE)
1321 warning ("dereferencing `void *' pointer");
1323 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1324 so that we get the proper error message if the result is used
1325 to assign to. Also, &* is supposed to be a no-op.
1326 And ANSI C seems to specify that the type of the result
1327 should be the const type. */
1328 /* A de-reference of a pointer to const is not a const. It is valid
1329 to change it via some other pointer. */
1330 TREE_READONLY (ref) = TYPE_READONLY (t);
1331 TREE_SIDE_EFFECTS (ref)
1332 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1333 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1334 return ref;
1337 else if (TREE_CODE (pointer) != ERROR_MARK)
1338 error ("invalid type argument of `%s'", errorstring);
1339 return error_mark_node;
1342 /* This handles expressions of the form "a[i]", which denotes
1343 an array reference.
1345 This is logically equivalent in C to *(a+i), but we may do it differently.
1346 If A is a variable or a member, we generate a primitive ARRAY_REF.
1347 This avoids forcing the array out of registers, and can work on
1348 arrays that are not lvalues (for example, members of structures returned
1349 by functions). */
1351 tree
1352 build_array_ref (array, index)
1353 tree array, index;
1355 if (index == 0)
1357 error ("subscript missing in array reference");
1358 return error_mark_node;
1361 if (TREE_TYPE (array) == error_mark_node
1362 || TREE_TYPE (index) == error_mark_node)
1363 return error_mark_node;
1365 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1366 && TREE_CODE (array) != INDIRECT_REF)
1368 tree rval, type;
1370 /* Subscripting with type char is likely to lose
1371 on a machine where chars are signed.
1372 So warn on any machine, but optionally.
1373 Don't warn for unsigned char since that type is safe.
1374 Don't warn for signed char because anyone who uses that
1375 must have done so deliberately. */
1376 if (warn_char_subscripts
1377 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1378 warning ("array subscript has type `char'");
1380 /* Apply default promotions *after* noticing character types. */
1381 index = default_conversion (index);
1383 /* Require integer *after* promotion, for sake of enums. */
1384 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1386 error ("array subscript is not an integer");
1387 return error_mark_node;
1390 /* An array that is indexed by a non-constant
1391 cannot be stored in a register; we must be able to do
1392 address arithmetic on its address.
1393 Likewise an array of elements of variable size. */
1394 if (TREE_CODE (index) != INTEGER_CST
1395 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1396 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1398 if (mark_addressable (array) == 0)
1399 return error_mark_node;
1401 /* An array that is indexed by a constant value which is not within
1402 the array bounds cannot be stored in a register either; because we
1403 would get a crash in store_bit_field/extract_bit_field when trying
1404 to access a non-existent part of the register. */
1405 if (TREE_CODE (index) == INTEGER_CST
1406 && TYPE_VALUES (TREE_TYPE (array))
1407 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1409 if (mark_addressable (array) == 0)
1410 return error_mark_node;
1413 if (pedantic && !lvalue_p (array))
1415 if (DECL_REGISTER (array))
1416 pedwarn ("ANSI C forbids subscripting `register' array");
1417 else
1418 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1421 if (pedantic)
1423 tree foo = array;
1424 while (TREE_CODE (foo) == COMPONENT_REF)
1425 foo = TREE_OPERAND (foo, 0);
1426 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1427 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1430 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1431 rval = build (ARRAY_REF, type, array, index);
1432 /* Array ref is const/volatile if the array elements are
1433 or if the array is. */
1434 TREE_READONLY (rval)
1435 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1436 | TREE_READONLY (array));
1437 TREE_SIDE_EFFECTS (rval)
1438 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1439 | TREE_SIDE_EFFECTS (array));
1440 TREE_THIS_VOLATILE (rval)
1441 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1442 /* This was added by rms on 16 Nov 91.
1443 It fixes vol struct foo *a; a->elts[1]
1444 in an inline function.
1445 Hope it doesn't break something else. */
1446 | TREE_THIS_VOLATILE (array));
1447 return require_complete_type (fold (rval));
1451 tree ar = default_conversion (array);
1452 tree ind = default_conversion (index);
1454 /* Put the integer in IND to simplify error checking. */
1455 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1457 tree temp = ar;
1458 ar = ind;
1459 ind = temp;
1462 if (ar == error_mark_node)
1463 return ar;
1465 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1466 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1468 error ("subscripted value is neither array nor pointer");
1469 return error_mark_node;
1471 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1473 error ("array subscript is not an integer");
1474 return error_mark_node;
1477 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1478 "array indexing");
1482 /* Build a function call to function FUNCTION with parameters PARAMS.
1483 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1484 TREE_VALUE of each node is a parameter-expression.
1485 FUNCTION's data type may be a function type or a pointer-to-function. */
1487 tree
1488 build_function_call (function, params)
1489 tree function, params;
1491 register tree fntype, fundecl = 0;
1492 register tree coerced_params;
1493 tree name = NULL_TREE, assembler_name = NULL_TREE;
1495 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1496 STRIP_TYPE_NOPS (function);
1498 /* Convert anything with function type to a pointer-to-function. */
1499 if (TREE_CODE (function) == FUNCTION_DECL)
1501 name = DECL_NAME (function);
1502 assembler_name = DECL_ASSEMBLER_NAME (function);
1504 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1505 (because calling an inline function does not mean the function
1506 needs to be separately compiled). */
1507 fntype = build_type_variant (TREE_TYPE (function),
1508 TREE_READONLY (function),
1509 TREE_THIS_VOLATILE (function));
1510 fundecl = function;
1511 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1513 else
1514 function = default_conversion (function);
1516 fntype = TREE_TYPE (function);
1518 if (TREE_CODE (fntype) == ERROR_MARK)
1519 return error_mark_node;
1521 if (!(TREE_CODE (fntype) == POINTER_TYPE
1522 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1524 error ("called object is not a function");
1525 return error_mark_node;
1528 /* fntype now gets the type of function pointed to. */
1529 fntype = TREE_TYPE (fntype);
1531 /* Convert the parameters to the types declared in the
1532 function prototype, or apply default promotions. */
1534 coerced_params
1535 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1537 /* Check for errors in format strings. */
1539 if (warn_format && (name || assembler_name))
1540 check_function_format (name, assembler_name, coerced_params);
1542 /* Recognize certain built-in functions so we can make tree-codes
1543 other than CALL_EXPR. We do this when it enables fold-const.c
1544 to do something useful. */
1546 if (TREE_CODE (function) == ADDR_EXPR
1547 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1548 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1549 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1551 case BUILT_IN_ABS:
1552 case BUILT_IN_LABS:
1553 case BUILT_IN_FABS:
1554 if (coerced_params == 0)
1555 return integer_zero_node;
1556 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1560 register tree result
1561 = build (CALL_EXPR, TREE_TYPE (fntype),
1562 function, coerced_params, NULL_TREE);
1564 TREE_SIDE_EFFECTS (result) = 1;
1565 if (TREE_TYPE (result) == void_type_node)
1566 return result;
1567 return require_complete_type (result);
1571 /* Convert the argument expressions in the list VALUES
1572 to the types in the list TYPELIST. The result is a list of converted
1573 argument expressions.
1575 If TYPELIST is exhausted, or when an element has NULL as its type,
1576 perform the default conversions.
1578 PARMLIST is the chain of parm decls for the function being called.
1579 It may be 0, if that info is not available.
1580 It is used only for generating error messages.
1582 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1584 This is also where warnings about wrong number of args are generated.
1586 Both VALUES and the returned value are chains of TREE_LIST nodes
1587 with the elements of the list in the TREE_VALUE slots of those nodes. */
1589 static tree
1590 convert_arguments (typelist, values, name, fundecl)
1591 tree typelist, values, name, fundecl;
1593 register tree typetail, valtail;
1594 register tree result = NULL;
1595 int parmnum;
1597 /* Scan the given expressions and types, producing individual
1598 converted arguments and pushing them on RESULT in reverse order. */
1600 for (valtail = values, typetail = typelist, parmnum = 0;
1601 valtail;
1602 valtail = TREE_CHAIN (valtail), parmnum++)
1604 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1605 register tree val = TREE_VALUE (valtail);
1607 if (type == void_type_node)
1609 if (name)
1610 error ("too many arguments to function `%s'",
1611 IDENTIFIER_POINTER (name));
1612 else
1613 error ("too many arguments to function");
1614 break;
1617 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1618 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1619 to convert automatically to a pointer. */
1620 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1621 val = TREE_OPERAND (val, 0);
1623 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1624 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1625 val = default_conversion (val);
1627 val = require_complete_type (val);
1629 if (type != 0)
1631 /* Formal parm type is specified by a function prototype. */
1632 tree parmval;
1634 if (TYPE_SIZE (type) == 0)
1636 error ("type of formal parameter %d is incomplete", parmnum + 1);
1637 parmval = val;
1639 else
1641 /* Optionally warn about conversions that
1642 differ from the default conversions. */
1643 if (warn_conversion)
1645 int formal_prec = TYPE_PRECISION (type);
1647 if (INTEGRAL_TYPE_P (type)
1648 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1649 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1650 else if (TREE_CODE (type) == COMPLEX_TYPE
1651 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1652 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1653 else if (TREE_CODE (type) == REAL_TYPE
1654 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1655 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1656 else if (TREE_CODE (type) == REAL_TYPE
1657 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1658 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1659 /* ??? At some point, messages should be written about
1660 conversions between complex types, but that's too messy
1661 to do now. */
1662 else if (TREE_CODE (type) == REAL_TYPE
1663 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1665 /* Warn if any argument is passed as `float',
1666 since without a prototype it would be `double'. */
1667 if (formal_prec == TYPE_PRECISION (float_type_node))
1668 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1670 /* Detect integer changing in width or signedness. */
1671 else if (INTEGRAL_TYPE_P (type)
1672 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1674 tree would_have_been = default_conversion (val);
1675 tree type1 = TREE_TYPE (would_have_been);
1677 if (TREE_CODE (type) == ENUMERAL_TYPE
1678 && type == TREE_TYPE (val))
1679 /* No warning if function asks for enum
1680 and the actual arg is that enum type. */
1682 else if (formal_prec != TYPE_PRECISION (type1))
1683 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1684 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1686 /* Don't complain if the formal parameter type
1687 is an enum, because we can't tell now whether
1688 the value was an enum--even the same enum. */
1689 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1691 else if (TREE_CODE (val) == INTEGER_CST
1692 && int_fits_type_p (val, type))
1693 /* Change in signedness doesn't matter
1694 if a constant value is unaffected. */
1696 /* Likewise for a constant in a NOP_EXPR. */
1697 else if (TREE_CODE (val) == NOP_EXPR
1698 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1699 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1701 #if 0 /* We never get such tree structure here. */
1702 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1703 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1704 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1705 /* Change in signedness doesn't matter
1706 if an enum value is unaffected. */
1708 #endif
1709 /* If the value is extended from a narrower
1710 unsigned type, it doesn't matter whether we
1711 pass it as signed or unsigned; the value
1712 certainly is the same either way. */
1713 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1714 && TREE_UNSIGNED (TREE_TYPE (val)))
1716 else if (TREE_UNSIGNED (type))
1717 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1718 else
1719 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1723 parmval = convert_for_assignment (type, val,
1724 (char *) 0, /* arg passing */
1725 fundecl, name, parmnum + 1);
1727 #ifdef PROMOTE_PROTOTYPES
1728 if ((TREE_CODE (type) == INTEGER_TYPE
1729 || TREE_CODE (type) == ENUMERAL_TYPE)
1730 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1731 parmval = default_conversion (parmval);
1732 #endif
1734 result = tree_cons (NULL_TREE, parmval, result);
1736 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1737 && (TYPE_PRECISION (TREE_TYPE (val))
1738 < TYPE_PRECISION (double_type_node)))
1739 /* Convert `float' to `double'. */
1740 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1741 else
1742 /* Convert `short' and `char' to full-size `int'. */
1743 result = tree_cons (NULL_TREE, default_conversion (val), result);
1745 if (typetail)
1746 typetail = TREE_CHAIN (typetail);
1749 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1751 if (name)
1752 error ("too few arguments to function `%s'",
1753 IDENTIFIER_POINTER (name));
1754 else
1755 error ("too few arguments to function");
1758 return nreverse (result);
1761 /* This is the entry point used by the parser
1762 for binary operators in the input.
1763 In addition to constructing the expression,
1764 we check for operands that were written with other binary operators
1765 in a way that is likely to confuse the user. */
1767 tree
1768 parser_build_binary_op (code, arg1, arg2)
1769 enum tree_code code;
1770 tree arg1, arg2;
1772 tree result = build_binary_op (code, arg1, arg2, 1);
1774 char class;
1775 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1776 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1777 enum tree_code code1 = ERROR_MARK;
1778 enum tree_code code2 = ERROR_MARK;
1780 if (class1 == 'e' || class1 == '1'
1781 || class1 == '2' || class1 == '<')
1782 code1 = C_EXP_ORIGINAL_CODE (arg1);
1783 if (class2 == 'e' || class2 == '1'
1784 || class2 == '2' || class2 == '<')
1785 code2 = C_EXP_ORIGINAL_CODE (arg2);
1787 /* Check for cases such as x+y<<z which users are likely
1788 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1789 is cleared to prevent these warnings. */
1790 if (warn_parentheses)
1792 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1794 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1795 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1796 warning ("suggest parentheses around + or - inside shift");
1799 if (code == TRUTH_ORIF_EXPR)
1801 if (code1 == TRUTH_ANDIF_EXPR
1802 || code2 == TRUTH_ANDIF_EXPR)
1803 warning ("suggest parentheses around && within ||");
1806 if (code == BIT_IOR_EXPR)
1808 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1809 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1810 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1811 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1812 warning ("suggest parentheses around arithmetic in operand of |");
1813 /* Check cases like x|y==z */
1814 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1815 warning ("suggest parentheses around comparison in operand of |");
1818 if (code == BIT_XOR_EXPR)
1820 if (code1 == BIT_AND_EXPR
1821 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1822 || code2 == BIT_AND_EXPR
1823 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1824 warning ("suggest parentheses around arithmetic in operand of ^");
1825 /* Check cases like x^y==z */
1826 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1827 warning ("suggest parentheses around comparison in operand of ^");
1830 if (code == BIT_AND_EXPR)
1832 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1833 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1834 warning ("suggest parentheses around + or - in operand of &");
1835 /* Check cases like x&y==z */
1836 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1837 warning ("suggest parentheses around comparison in operand of &");
1841 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1842 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1843 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1844 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1846 unsigned_conversion_warning (result, arg1);
1847 unsigned_conversion_warning (result, arg2);
1848 overflow_warning (result);
1850 class = TREE_CODE_CLASS (TREE_CODE (result));
1852 /* Record the code that was specified in the source,
1853 for the sake of warnings about confusing nesting. */
1854 if (class == 'e' || class == '1'
1855 || class == '2' || class == '<')
1856 C_SET_EXP_ORIGINAL_CODE (result, code);
1857 else
1859 int flag = TREE_CONSTANT (result);
1860 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1861 so that convert_for_assignment wouldn't strip it.
1862 That way, we got warnings for things like p = (1 - 1).
1863 But it turns out we should not get those warnings. */
1864 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1865 C_SET_EXP_ORIGINAL_CODE (result, code);
1866 TREE_CONSTANT (result) = flag;
1869 return result;
1872 /* Build a binary-operation expression without default conversions.
1873 CODE is the kind of expression to build.
1874 This function differs from `build' in several ways:
1875 the data type of the result is computed and recorded in it,
1876 warnings are generated if arg data types are invalid,
1877 special handling for addition and subtraction of pointers is known,
1878 and some optimization is done (operations on narrow ints
1879 are done in the narrower type when that gives the same result).
1880 Constant folding is also done before the result is returned.
1882 Note that the operands will never have enumeral types, or function
1883 or array types, because either they will have the default conversions
1884 performed or they have both just been converted to some other type in which
1885 the arithmetic is to be done. */
1887 tree
1888 build_binary_op (code, orig_op0, orig_op1, convert_p)
1889 enum tree_code code;
1890 tree orig_op0, orig_op1;
1891 int convert_p;
1893 tree type0, type1;
1894 register enum tree_code code0, code1;
1895 tree op0, op1;
1897 /* Expression code to give to the expression when it is built.
1898 Normally this is CODE, which is what the caller asked for,
1899 but in some special cases we change it. */
1900 register enum tree_code resultcode = code;
1902 /* Data type in which the computation is to be performed.
1903 In the simplest cases this is the common type of the arguments. */
1904 register tree result_type = NULL;
1906 /* Nonzero means operands have already been type-converted
1907 in whatever way is necessary.
1908 Zero means they need to be converted to RESULT_TYPE. */
1909 int converted = 0;
1911 /* Nonzero means create the expression with this type, rather than
1912 RESULT_TYPE. */
1913 tree build_type = 0;
1915 /* Nonzero means after finally constructing the expression
1916 convert it to this type. */
1917 tree final_type = 0;
1919 /* Nonzero if this is an operation like MIN or MAX which can
1920 safely be computed in short if both args are promoted shorts.
1921 Also implies COMMON.
1922 -1 indicates a bitwise operation; this makes a difference
1923 in the exact conditions for when it is safe to do the operation
1924 in a narrower mode. */
1925 int shorten = 0;
1927 /* Nonzero if this is a comparison operation;
1928 if both args are promoted shorts, compare the original shorts.
1929 Also implies COMMON. */
1930 int short_compare = 0;
1932 /* Nonzero if this is a right-shift operation, which can be computed on the
1933 original short and then promoted if the operand is a promoted short. */
1934 int short_shift = 0;
1936 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1937 int common = 0;
1939 if (convert_p)
1941 op0 = default_conversion (orig_op0);
1942 op1 = default_conversion (orig_op1);
1944 else
1946 op0 = orig_op0;
1947 op1 = orig_op1;
1950 type0 = TREE_TYPE (op0);
1951 type1 = TREE_TYPE (op1);
1953 /* The expression codes of the data types of the arguments tell us
1954 whether the arguments are integers, floating, pointers, etc. */
1955 code0 = TREE_CODE (type0);
1956 code1 = TREE_CODE (type1);
1958 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1959 STRIP_TYPE_NOPS (op0);
1960 STRIP_TYPE_NOPS (op1);
1962 /* If an error was already reported for one of the arguments,
1963 avoid reporting another error. */
1965 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1966 return error_mark_node;
1968 switch (code)
1970 case PLUS_EXPR:
1971 /* Handle the pointer + int case. */
1972 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1973 return pointer_int_sum (PLUS_EXPR, op0, op1);
1974 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1975 return pointer_int_sum (PLUS_EXPR, op1, op0);
1976 else
1977 common = 1;
1978 break;
1980 case MINUS_EXPR:
1981 /* Subtraction of two similar pointers.
1982 We must subtract them as integers, then divide by object size. */
1983 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1984 && comp_target_types (type0, type1))
1985 return pointer_diff (op0, op1);
1986 /* Handle pointer minus int. Just like pointer plus int. */
1987 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1988 return pointer_int_sum (MINUS_EXPR, op0, op1);
1989 else
1990 common = 1;
1991 break;
1993 case MULT_EXPR:
1994 common = 1;
1995 break;
1997 case TRUNC_DIV_EXPR:
1998 case CEIL_DIV_EXPR:
1999 case FLOOR_DIV_EXPR:
2000 case ROUND_DIV_EXPR:
2001 case EXACT_DIV_EXPR:
2002 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2003 || code0 == COMPLEX_TYPE)
2004 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2005 || code1 == COMPLEX_TYPE))
2007 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2008 resultcode = RDIV_EXPR;
2009 else
2011 /* Although it would be tempting to shorten always here, that
2012 loses on some targets, since the modulo instruction is
2013 undefined if the quotient can't be represented in the
2014 computation mode. We shorten only if unsigned or if
2015 dividing by something we know != -1. */
2016 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2017 || (TREE_CODE (op1) == INTEGER_CST
2018 && (TREE_INT_CST_LOW (op1) != -1
2019 || TREE_INT_CST_HIGH (op1) != -1)));
2021 common = 1;
2023 break;
2025 case BIT_AND_EXPR:
2026 case BIT_ANDTC_EXPR:
2027 case BIT_IOR_EXPR:
2028 case BIT_XOR_EXPR:
2029 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2030 shorten = -1;
2031 /* If one operand is a constant, and the other is a short type
2032 that has been converted to an int,
2033 really do the work in the short type and then convert the
2034 result to int. If we are lucky, the constant will be 0 or 1
2035 in the short type, making the entire operation go away. */
2036 if (TREE_CODE (op0) == INTEGER_CST
2037 && TREE_CODE (op1) == NOP_EXPR
2038 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2039 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2041 final_type = result_type;
2042 op1 = TREE_OPERAND (op1, 0);
2043 result_type = TREE_TYPE (op1);
2045 if (TREE_CODE (op1) == INTEGER_CST
2046 && TREE_CODE (op0) == NOP_EXPR
2047 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2048 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2050 final_type = result_type;
2051 op0 = TREE_OPERAND (op0, 0);
2052 result_type = TREE_TYPE (op0);
2054 break;
2056 case TRUNC_MOD_EXPR:
2057 case FLOOR_MOD_EXPR:
2058 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2060 /* Although it would be tempting to shorten always here, that loses
2061 on some targets, since the modulo instruction is undefined if the
2062 quotient can't be represented in the computation mode. We shorten
2063 only if unsigned or if dividing by something we know != -1. */
2064 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2065 || (TREE_CODE (op1) == INTEGER_CST
2066 && (TREE_INT_CST_LOW (op1) != -1
2067 || TREE_INT_CST_HIGH (op1) != -1)));
2068 common = 1;
2070 break;
2072 case TRUTH_ANDIF_EXPR:
2073 case TRUTH_ORIF_EXPR:
2074 case TRUTH_AND_EXPR:
2075 case TRUTH_OR_EXPR:
2076 case TRUTH_XOR_EXPR:
2077 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2078 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2079 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2080 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2082 /* Result of these operations is always an int,
2083 but that does not mean the operands should be
2084 converted to ints! */
2085 result_type = integer_type_node;
2086 op0 = truthvalue_conversion (op0);
2087 op1 = truthvalue_conversion (op1);
2088 converted = 1;
2090 break;
2092 /* Shift operations: result has same type as first operand;
2093 always convert second operand to int.
2094 Also set SHORT_SHIFT if shifting rightward. */
2096 case RSHIFT_EXPR:
2097 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2099 if (TREE_CODE (op1) == INTEGER_CST)
2101 if (tree_int_cst_sgn (op1) < 0)
2102 warning ("right shift count is negative");
2103 else
2105 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2106 short_shift = 1;
2107 if (TREE_INT_CST_HIGH (op1) != 0
2108 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2109 >= TYPE_PRECISION (type0)))
2110 warning ("right shift count >= width of type");
2113 /* Use the type of the value to be shifted.
2114 This is what most traditional C compilers do. */
2115 result_type = type0;
2116 /* Unless traditional, convert the shift-count to an integer,
2117 regardless of size of value being shifted. */
2118 if (! flag_traditional)
2120 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2121 op1 = convert (integer_type_node, op1);
2122 /* Avoid converting op1 to result_type later. */
2123 converted = 1;
2126 break;
2128 case LSHIFT_EXPR:
2129 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2131 if (TREE_CODE (op1) == INTEGER_CST)
2133 if (tree_int_cst_sgn (op1) < 0)
2134 warning ("left shift count is negative");
2135 else if (TREE_INT_CST_HIGH (op1) != 0
2136 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2137 >= TYPE_PRECISION (type0)))
2138 warning ("left shift count >= width of type");
2140 /* Use the type of the value to be shifted.
2141 This is what most traditional C compilers do. */
2142 result_type = type0;
2143 /* Unless traditional, convert the shift-count to an integer,
2144 regardless of size of value being shifted. */
2145 if (! flag_traditional)
2147 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2148 op1 = convert (integer_type_node, op1);
2149 /* Avoid converting op1 to result_type later. */
2150 converted = 1;
2153 break;
2155 case RROTATE_EXPR:
2156 case LROTATE_EXPR:
2157 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2159 if (TREE_CODE (op1) == INTEGER_CST)
2161 if (tree_int_cst_sgn (op1) < 0)
2162 warning ("shift count is negative");
2163 else if (TREE_INT_CST_HIGH (op1) != 0
2164 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2165 >= TYPE_PRECISION (type0)))
2166 warning ("shift count >= width of type");
2168 /* Use the type of the value to be shifted.
2169 This is what most traditional C compilers do. */
2170 result_type = type0;
2171 /* Unless traditional, convert the shift-count to an integer,
2172 regardless of size of value being shifted. */
2173 if (! flag_traditional)
2175 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2176 op1 = convert (integer_type_node, op1);
2177 /* Avoid converting op1 to result_type later. */
2178 converted = 1;
2181 break;
2183 case EQ_EXPR:
2184 case NE_EXPR:
2185 /* Result of comparison is always int,
2186 but don't convert the args to int! */
2187 build_type = integer_type_node;
2188 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2189 || code0 == COMPLEX_TYPE)
2190 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2191 || code1 == COMPLEX_TYPE))
2192 short_compare = 1;
2193 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2195 register tree tt0 = TREE_TYPE (type0);
2196 register tree tt1 = TREE_TYPE (type1);
2197 /* Anything compares with void *. void * compares with anything.
2198 Otherwise, the targets must be compatible
2199 and both must be object or both incomplete. */
2200 if (comp_target_types (type0, type1))
2201 result_type = common_type (type0, type1);
2202 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2204 /* op0 != orig_op0 detects the case of something
2205 whose value is 0 but which isn't a valid null ptr const. */
2206 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2207 && TREE_CODE (tt1) == FUNCTION_TYPE)
2208 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2210 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2212 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2213 && TREE_CODE (tt0) == FUNCTION_TYPE)
2214 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2216 else
2217 pedwarn ("comparison of distinct pointer types lacks a cast");
2219 if (result_type == NULL_TREE)
2220 result_type = ptr_type_node;
2222 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2223 && integer_zerop (op1))
2224 result_type = type0;
2225 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2226 && integer_zerop (op0))
2227 result_type = type1;
2228 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2230 result_type = type0;
2231 if (! flag_traditional)
2232 pedwarn ("comparison between pointer and integer");
2234 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2236 result_type = type1;
2237 if (! flag_traditional)
2238 pedwarn ("comparison between pointer and integer");
2240 break;
2242 case MAX_EXPR:
2243 case MIN_EXPR:
2244 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2245 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2246 shorten = 1;
2247 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2249 if (comp_target_types (type0, type1))
2251 result_type = common_type (type0, type1);
2252 if (pedantic
2253 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2254 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2256 else
2258 result_type = ptr_type_node;
2259 pedwarn ("comparison of distinct pointer types lacks a cast");
2262 break;
2264 case LE_EXPR:
2265 case GE_EXPR:
2266 case LT_EXPR:
2267 case GT_EXPR:
2268 build_type = integer_type_node;
2269 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2270 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2271 short_compare = 1;
2272 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2274 if (comp_target_types (type0, type1))
2276 result_type = common_type (type0, type1);
2277 if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2278 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2279 pedwarn ("comparison of complete and incomplete pointers");
2280 else if (pedantic
2281 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2282 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2284 else
2286 result_type = ptr_type_node;
2287 pedwarn ("comparison of distinct pointer types lacks a cast");
2290 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2291 && integer_zerop (op1))
2293 result_type = type0;
2294 if (pedantic || extra_warnings)
2295 pedwarn ("ordered comparison of pointer with integer zero");
2297 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2298 && integer_zerop (op0))
2300 result_type = type1;
2301 if (pedantic)
2302 pedwarn ("ordered comparison of pointer with integer zero");
2304 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2306 result_type = type0;
2307 if (! flag_traditional)
2308 pedwarn ("comparison between pointer and integer");
2310 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2312 result_type = type1;
2313 if (! flag_traditional)
2314 pedwarn ("comparison between pointer and integer");
2316 break;
2319 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2321 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2323 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2325 if (shorten || common || short_compare)
2326 result_type = common_type (type0, type1);
2328 /* For certain operations (which identify themselves by shorten != 0)
2329 if both args were extended from the same smaller type,
2330 do the arithmetic in that type and then extend.
2332 shorten !=0 and !=1 indicates a bitwise operation.
2333 For them, this optimization is safe only if
2334 both args are zero-extended or both are sign-extended.
2335 Otherwise, we might change the result.
2336 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2337 but calculated in (unsigned short) it would be (unsigned short)-1. */
2339 if (shorten && none_complex)
2341 int unsigned0, unsigned1;
2342 tree arg0 = get_narrower (op0, &unsigned0);
2343 tree arg1 = get_narrower (op1, &unsigned1);
2344 /* UNS is 1 if the operation to be done is an unsigned one. */
2345 int uns = TREE_UNSIGNED (result_type);
2346 tree type;
2348 final_type = result_type;
2350 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2351 but it *requires* conversion to FINAL_TYPE. */
2353 if ((TYPE_PRECISION (TREE_TYPE (op0))
2354 == TYPE_PRECISION (TREE_TYPE (arg0)))
2355 && TREE_TYPE (op0) != final_type)
2356 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2357 if ((TYPE_PRECISION (TREE_TYPE (op1))
2358 == TYPE_PRECISION (TREE_TYPE (arg1)))
2359 && TREE_TYPE (op1) != final_type)
2360 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2362 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2364 /* For bitwise operations, signedness of nominal type
2365 does not matter. Consider only how operands were extended. */
2366 if (shorten == -1)
2367 uns = unsigned0;
2369 /* Note that in all three cases below we refrain from optimizing
2370 an unsigned operation on sign-extended args.
2371 That would not be valid. */
2373 /* Both args variable: if both extended in same way
2374 from same width, do it in that width.
2375 Do it unsigned if args were zero-extended. */
2376 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2377 < TYPE_PRECISION (result_type))
2378 && (TYPE_PRECISION (TREE_TYPE (arg1))
2379 == TYPE_PRECISION (TREE_TYPE (arg0)))
2380 && unsigned0 == unsigned1
2381 && (unsigned0 || !uns))
2382 result_type
2383 = signed_or_unsigned_type (unsigned0,
2384 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2385 else if (TREE_CODE (arg0) == INTEGER_CST
2386 && (unsigned1 || !uns)
2387 && (TYPE_PRECISION (TREE_TYPE (arg1))
2388 < TYPE_PRECISION (result_type))
2389 && (type = signed_or_unsigned_type (unsigned1,
2390 TREE_TYPE (arg1)),
2391 int_fits_type_p (arg0, type)))
2392 result_type = type;
2393 else if (TREE_CODE (arg1) == INTEGER_CST
2394 && (unsigned0 || !uns)
2395 && (TYPE_PRECISION (TREE_TYPE (arg0))
2396 < TYPE_PRECISION (result_type))
2397 && (type = signed_or_unsigned_type (unsigned0,
2398 TREE_TYPE (arg0)),
2399 int_fits_type_p (arg1, type)))
2400 result_type = type;
2403 /* Shifts can be shortened if shifting right. */
2405 if (short_shift)
2407 int unsigned_arg;
2408 tree arg0 = get_narrower (op0, &unsigned_arg);
2410 final_type = result_type;
2412 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2413 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2415 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2416 /* We can shorten only if the shift count is less than the
2417 number of bits in the smaller type size. */
2418 && TREE_INT_CST_HIGH (op1) == 0
2419 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
2420 /* If arg is sign-extended and then unsigned-shifted,
2421 we can simulate this with a signed shift in arg's type
2422 only if the extended result is at least twice as wide
2423 as the arg. Otherwise, the shift could use up all the
2424 ones made by sign-extension and bring in zeros.
2425 We can't optimize that case at all, but in most machines
2426 it never happens because available widths are 2**N. */
2427 && (!TREE_UNSIGNED (final_type)
2428 || unsigned_arg
2429 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2431 /* Do an unsigned shift if the operand was zero-extended. */
2432 result_type
2433 = signed_or_unsigned_type (unsigned_arg,
2434 TREE_TYPE (arg0));
2435 /* Convert value-to-be-shifted to that type. */
2436 if (TREE_TYPE (op0) != result_type)
2437 op0 = convert (result_type, op0);
2438 converted = 1;
2442 /* Comparison operations are shortened too but differently.
2443 They identify themselves by setting short_compare = 1. */
2445 if (short_compare)
2447 /* Don't write &op0, etc., because that would prevent op0
2448 from being kept in a register.
2449 Instead, make copies of the our local variables and
2450 pass the copies by reference, then copy them back afterward. */
2451 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2452 enum tree_code xresultcode = resultcode;
2453 tree val
2454 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2455 if (val != 0)
2456 return val;
2457 op0 = xop0, op1 = xop1;
2458 converted = 1;
2459 resultcode = xresultcode;
2461 if (warn_sign_compare)
2463 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2464 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2466 int unsignedp0, unsignedp1;
2467 tree primop0 = get_narrower (op0, &unsignedp0);
2468 tree primop1 = get_narrower (op1, &unsignedp1);
2470 /* Avoid spurious warnings for comparison with enumerators. */
2472 xop0 = orig_op0;
2473 xop1 = orig_op1;
2474 STRIP_TYPE_NOPS (xop0);
2475 STRIP_TYPE_NOPS (xop1);
2477 /* Give warnings for comparisons between signed and unsigned
2478 quantities that may fail. */
2479 /* Do the checking based on the original operand trees, so that
2480 casts will be considered, but default promotions won't be. */
2482 /* Do not warn if the comparison is being done in a signed type,
2483 since the signed type will only be chosen if it can represent
2484 all the values of the unsigned type. */
2485 if (! TREE_UNSIGNED (result_type))
2486 /* OK */;
2487 /* Do not warn if both operands are unsigned. */
2488 else if (op0_signed == op1_signed)
2489 /* OK */;
2490 /* Do not warn if the signed quantity is an unsuffixed
2491 integer literal (or some static constant expression
2492 involving such literals) and it is non-negative. */
2493 else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST
2494 && tree_int_cst_sgn (xop0) >= 0)
2495 || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
2496 && tree_int_cst_sgn (xop1) >= 0))
2497 /* OK */;
2498 /* Do not warn if the comparison is an equality operation,
2499 the unsigned quantity is an integral constant and it does
2500 not use the most significant bit of result_type. */
2501 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
2502 && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST
2503 && int_fits_type_p (xop1, signed_type (result_type)))
2504 || (op1_signed && TREE_CODE (xop0) == INTEGER_CST
2505 && int_fits_type_p (xop0, signed_type (result_type)))))
2506 /* OK */;
2507 else
2508 warning ("comparison between signed and unsigned");
2510 /* Warn if two unsigned values are being compared in a size
2511 larger than their original size, and one (and only one) is the
2512 result of a `~' operator. This comparison will always fail.
2514 Also warn if one operand is a constant, and the constant
2515 does not have all bits set that are set in the ~ operand
2516 when it is extended. */
2518 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2519 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2521 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2522 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2523 &unsignedp0);
2524 else
2525 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2526 &unsignedp1);
2528 if (TREE_CODE (primop0) == INTEGER_CST
2529 || TREE_CODE (primop1) == INTEGER_CST)
2531 tree primop;
2532 long constant, mask;
2533 int unsignedp, bits;
2535 if (TREE_CODE (primop0) == INTEGER_CST)
2537 primop = primop1;
2538 unsignedp = unsignedp1;
2539 constant = TREE_INT_CST_LOW (primop0);
2541 else
2543 primop = primop0;
2544 unsignedp = unsignedp0;
2545 constant = TREE_INT_CST_LOW (primop1);
2548 bits = TYPE_PRECISION (TREE_TYPE (primop));
2549 if (bits < TYPE_PRECISION (result_type)
2550 && bits < HOST_BITS_PER_LONG && unsignedp)
2552 mask = (~0L) << bits;
2553 if ((mask & constant) != mask)
2554 warning ("comparison of promoted ~unsigned with constant");
2557 else if (unsignedp0 && unsignedp1
2558 && (TYPE_PRECISION (TREE_TYPE (primop0))
2559 < TYPE_PRECISION (result_type))
2560 && (TYPE_PRECISION (TREE_TYPE (primop1))
2561 < TYPE_PRECISION (result_type)))
2562 warning ("comparison of promoted ~unsigned with unsigned");
2568 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2569 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2570 Then the expression will be built.
2571 It will be given type FINAL_TYPE if that is nonzero;
2572 otherwise, it will be given type RESULT_TYPE. */
2574 if (!result_type)
2576 binary_op_error (code);
2577 return error_mark_node;
2580 if (! converted)
2582 if (TREE_TYPE (op0) != result_type)
2583 op0 = convert (result_type, op0);
2584 if (TREE_TYPE (op1) != result_type)
2585 op1 = convert (result_type, op1);
2588 if (build_type == NULL_TREE)
2589 build_type = result_type;
2592 register tree result = build (resultcode, build_type, op0, op1);
2593 register tree folded;
2595 folded = fold (result);
2596 if (folded == result)
2597 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2598 if (final_type != 0)
2599 return convert (final_type, folded);
2600 return folded;
2604 /* Return a tree for the sum or difference (RESULTCODE says which)
2605 of pointer PTROP and integer INTOP. */
2607 static tree
2608 pointer_int_sum (resultcode, ptrop, intop)
2609 enum tree_code resultcode;
2610 register tree ptrop, intop;
2612 tree size_exp;
2614 register tree result;
2615 register tree folded;
2617 /* The result is a pointer of the same type that is being added. */
2619 register tree result_type = TREE_TYPE (ptrop);
2621 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2623 if (pedantic || warn_pointer_arith)
2624 pedwarn ("pointer of type `void *' used in arithmetic");
2625 size_exp = integer_one_node;
2627 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2629 if (pedantic || warn_pointer_arith)
2630 pedwarn ("pointer to a function used in arithmetic");
2631 size_exp = integer_one_node;
2633 else
2634 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2636 /* If what we are about to multiply by the size of the elements
2637 contains a constant term, apply distributive law
2638 and multiply that constant term separately.
2639 This helps produce common subexpressions. */
2641 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2642 && ! TREE_CONSTANT (intop)
2643 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2644 && TREE_CONSTANT (size_exp)
2645 /* If the constant comes from pointer subtraction,
2646 skip this optimization--it would cause an error. */
2647 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2648 /* If the constant is unsigned, and smaller than the pointer size,
2649 then we must skip this optimization. This is because it could cause
2650 an overflow error if the constant is negative but INTOP is not. */
2651 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2652 || (TYPE_PRECISION (TREE_TYPE (intop))
2653 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2655 enum tree_code subcode = resultcode;
2656 tree int_type = TREE_TYPE (intop);
2657 if (TREE_CODE (intop) == MINUS_EXPR)
2658 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2659 /* Convert both subexpression types to the type of intop,
2660 because weird cases involving pointer arithmetic
2661 can result in a sum or difference with different type args. */
2662 ptrop = build_binary_op (subcode, ptrop,
2663 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2664 intop = convert (int_type, TREE_OPERAND (intop, 0));
2667 /* Convert the integer argument to a type the same size as sizetype
2668 so the multiply won't overflow spuriously. */
2670 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2671 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2672 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2673 TREE_UNSIGNED (sizetype)), intop);
2675 /* Replace the integer argument with a suitable product by the object size.
2676 Do this multiplication as signed, then convert to the appropriate
2677 pointer type (actually unsigned integral). */
2679 intop = convert (result_type,
2680 build_binary_op (MULT_EXPR, intop,
2681 convert (TREE_TYPE (intop), size_exp), 1));
2683 /* Create the sum or difference. */
2685 result = build (resultcode, result_type, ptrop, intop);
2687 folded = fold (result);
2688 if (folded == result)
2689 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2690 return folded;
2693 /* Return a tree for the difference of pointers OP0 and OP1.
2694 The resulting tree has type int. */
2696 static tree
2697 pointer_diff (op0, op1)
2698 register tree op0, op1;
2700 register tree result, folded;
2701 tree restype = ptrdiff_type_node;
2703 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2705 if (pedantic || warn_pointer_arith)
2707 if (TREE_CODE (target_type) == VOID_TYPE)
2708 pedwarn ("pointer of type `void *' used in subtraction");
2709 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2710 pedwarn ("pointer to a function used in subtraction");
2713 /* First do the subtraction as integers;
2714 then drop through to build the divide operator. */
2716 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2717 convert (restype, op1), 1);
2718 /* This generates an error if op1 is pointer to incomplete type. */
2719 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2720 error ("arithmetic on pointer to an incomplete type");
2722 /* This generates an error if op0 is pointer to incomplete type. */
2723 op1 = c_size_in_bytes (target_type);
2725 /* Divide by the size, in easiest possible way. */
2727 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2729 folded = fold (result);
2730 if (folded == result)
2731 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2732 return folded;
2735 /* Construct and perhaps optimize a tree representation
2736 for a unary operation. CODE, a tree_code, specifies the operation
2737 and XARG is the operand. NOCONVERT nonzero suppresses
2738 the default promotions (such as from short to int). */
2740 tree
2741 build_unary_op (code, xarg, noconvert)
2742 enum tree_code code;
2743 tree xarg;
2744 int noconvert;
2746 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2747 register tree arg = xarg;
2748 register tree argtype = 0;
2749 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2750 char *errstring = NULL;
2751 tree val;
2753 if (typecode == ERROR_MARK)
2754 return error_mark_node;
2755 if (typecode == ENUMERAL_TYPE)
2756 typecode = INTEGER_TYPE;
2758 switch (code)
2760 case CONVERT_EXPR:
2761 /* This is used for unary plus, because a CONVERT_EXPR
2762 is enough to prevent anybody from looking inside for
2763 associativity, but won't generate any code. */
2764 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2765 || typecode == COMPLEX_TYPE))
2766 errstring = "wrong type argument to unary plus";
2767 else if (!noconvert)
2768 arg = default_conversion (arg);
2769 break;
2771 case NEGATE_EXPR:
2772 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2773 || typecode == COMPLEX_TYPE))
2774 errstring = "wrong type argument to unary minus";
2775 else if (!noconvert)
2776 arg = default_conversion (arg);
2777 break;
2779 case BIT_NOT_EXPR:
2780 if (typecode == COMPLEX_TYPE)
2782 code = CONJ_EXPR;
2783 if (!noconvert)
2784 arg = default_conversion (arg);
2786 else if (typecode != INTEGER_TYPE)
2787 errstring = "wrong type argument to bit-complement";
2788 else if (!noconvert)
2789 arg = default_conversion (arg);
2790 break;
2792 case ABS_EXPR:
2793 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2794 || typecode == COMPLEX_TYPE))
2795 errstring = "wrong type argument to abs";
2796 else if (!noconvert)
2797 arg = default_conversion (arg);
2798 break;
2800 case CONJ_EXPR:
2801 /* Conjugating a real value is a no-op, but allow it anyway. */
2802 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2803 || typecode == COMPLEX_TYPE))
2804 errstring = "wrong type argument to conjugation";
2805 else if (!noconvert)
2806 arg = default_conversion (arg);
2807 break;
2809 case TRUTH_NOT_EXPR:
2810 if (typecode != INTEGER_TYPE
2811 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2812 && typecode != COMPLEX_TYPE
2813 /* These will convert to a pointer. */
2814 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2816 errstring = "wrong type argument to unary exclamation mark";
2817 break;
2819 arg = truthvalue_conversion (arg);
2820 return invert_truthvalue (arg);
2822 case NOP_EXPR:
2823 break;
2825 case REALPART_EXPR:
2826 if (TREE_CODE (arg) == COMPLEX_CST)
2827 return TREE_REALPART (arg);
2828 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2829 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2830 else
2831 return arg;
2833 case IMAGPART_EXPR:
2834 if (TREE_CODE (arg) == COMPLEX_CST)
2835 return TREE_IMAGPART (arg);
2836 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2837 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2838 else
2839 return convert (TREE_TYPE (arg), integer_zero_node);
2841 case PREINCREMENT_EXPR:
2842 case POSTINCREMENT_EXPR:
2843 case PREDECREMENT_EXPR:
2844 case POSTDECREMENT_EXPR:
2845 /* Handle complex lvalues (when permitted)
2846 by reduction to simpler cases. */
2848 val = unary_complex_lvalue (code, arg);
2849 if (val != 0)
2850 return val;
2852 /* Increment or decrement the real part of the value,
2853 and don't change the imaginary part. */
2854 if (typecode == COMPLEX_TYPE)
2856 tree real, imag;
2858 arg = stabilize_reference (arg);
2859 real = build_unary_op (REALPART_EXPR, arg, 1);
2860 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2861 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2862 build_unary_op (code, real, 1), imag);
2865 /* Report invalid types. */
2867 if (typecode != POINTER_TYPE
2868 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2870 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2871 errstring ="wrong type argument to increment";
2872 else
2873 errstring ="wrong type argument to decrement";
2874 break;
2878 register tree inc;
2879 tree result_type = TREE_TYPE (arg);
2881 arg = get_unwidened (arg, 0);
2882 argtype = TREE_TYPE (arg);
2884 /* Compute the increment. */
2886 if (typecode == POINTER_TYPE)
2888 /* If pointer target is an undefined struct,
2889 we just cannot know how to do the arithmetic. */
2890 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2891 error ("%s of pointer to unknown structure",
2892 ((code == PREINCREMENT_EXPR
2893 || code == POSTINCREMENT_EXPR)
2894 ? "increment" : "decrement"));
2895 else if ((pedantic || warn_pointer_arith)
2896 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2897 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2898 pedwarn ("wrong type argument to %s",
2899 ((code == PREINCREMENT_EXPR
2900 || code == POSTINCREMENT_EXPR)
2901 ? "increment" : "decrement"));
2902 inc = c_size_in_bytes (TREE_TYPE (result_type));
2904 else
2905 inc = integer_one_node;
2907 inc = convert (argtype, inc);
2909 /* Handle incrementing a cast-expression. */
2911 while (1)
2912 switch (TREE_CODE (arg))
2914 case NOP_EXPR:
2915 case CONVERT_EXPR:
2916 case FLOAT_EXPR:
2917 case FIX_TRUNC_EXPR:
2918 case FIX_FLOOR_EXPR:
2919 case FIX_ROUND_EXPR:
2920 case FIX_CEIL_EXPR:
2921 pedantic_lvalue_warning (CONVERT_EXPR);
2922 /* If the real type has the same machine representation
2923 as the type it is cast to, we can make better output
2924 by adding directly to the inside of the cast. */
2925 if ((TREE_CODE (TREE_TYPE (arg))
2926 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2927 && (TYPE_MODE (TREE_TYPE (arg))
2928 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2929 arg = TREE_OPERAND (arg, 0);
2930 else
2932 tree incremented, modify, value;
2933 arg = stabilize_reference (arg);
2934 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2935 value = arg;
2936 else
2937 value = save_expr (arg);
2938 incremented = build (((code == PREINCREMENT_EXPR
2939 || code == POSTINCREMENT_EXPR)
2940 ? PLUS_EXPR : MINUS_EXPR),
2941 argtype, value, inc);
2942 TREE_SIDE_EFFECTS (incremented) = 1;
2943 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2944 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2945 TREE_USED (value) = 1;
2946 return value;
2948 break;
2950 default:
2951 goto give_up;
2953 give_up:
2955 /* Complain about anything else that is not a true lvalue. */
2956 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2957 || code == POSTINCREMENT_EXPR)
2958 ? "increment" : "decrement")))
2959 return error_mark_node;
2961 /* Report a read-only lvalue. */
2962 if (TREE_READONLY (arg))
2963 readonly_warning (arg,
2964 ((code == PREINCREMENT_EXPR
2965 || code == POSTINCREMENT_EXPR)
2966 ? "increment" : "decrement"));
2968 val = build (code, TREE_TYPE (arg), arg, inc);
2969 TREE_SIDE_EFFECTS (val) = 1;
2970 val = convert (result_type, val);
2971 if (TREE_CODE (val) != code)
2972 TREE_NO_UNUSED_WARNING (val) = 1;
2973 return val;
2976 case ADDR_EXPR:
2977 /* Note that this operation never does default_conversion
2978 regardless of NOCONVERT. */
2980 /* Let &* cancel out to simplify resulting code. */
2981 if (TREE_CODE (arg) == INDIRECT_REF)
2983 /* Don't let this be an lvalue. */
2984 if (lvalue_p (TREE_OPERAND (arg, 0)))
2985 return non_lvalue (TREE_OPERAND (arg, 0));
2986 return TREE_OPERAND (arg, 0);
2989 /* For &x[y], return x+y */
2990 if (TREE_CODE (arg) == ARRAY_REF)
2992 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
2993 return error_mark_node;
2994 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2995 TREE_OPERAND (arg, 1), 1);
2998 /* Handle complex lvalues (when permitted)
2999 by reduction to simpler cases. */
3000 val = unary_complex_lvalue (code, arg);
3001 if (val != 0)
3002 return val;
3004 #if 0 /* Turned off because inconsistent;
3005 float f; *&(int)f = 3.4 stores in int format
3006 whereas (int)f = 3.4 stores in float format. */
3007 /* Address of a cast is just a cast of the address
3008 of the operand of the cast. */
3009 switch (TREE_CODE (arg))
3011 case NOP_EXPR:
3012 case CONVERT_EXPR:
3013 case FLOAT_EXPR:
3014 case FIX_TRUNC_EXPR:
3015 case FIX_FLOOR_EXPR:
3016 case FIX_ROUND_EXPR:
3017 case FIX_CEIL_EXPR:
3018 if (pedantic)
3019 pedwarn ("ANSI C forbids the address of a cast expression");
3020 return convert (build_pointer_type (TREE_TYPE (arg)),
3021 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3022 0));
3024 #endif
3026 /* Allow the address of a constructor if all the elements
3027 are constant. */
3028 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3030 /* Anything not already handled and not a true memory reference
3031 is an error. */
3032 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
3033 return error_mark_node;
3035 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3036 argtype = TREE_TYPE (arg);
3037 /* If the lvalue is const or volatile,
3038 merge that into the type that the address will point to. */
3039 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
3040 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3042 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3043 argtype = c_build_type_variant (argtype,
3044 TREE_READONLY (arg),
3045 TREE_THIS_VOLATILE (arg));
3048 argtype = build_pointer_type (argtype);
3050 if (mark_addressable (arg) == 0)
3051 return error_mark_node;
3054 tree addr;
3056 if (TREE_CODE (arg) == COMPONENT_REF)
3058 tree field = TREE_OPERAND (arg, 1);
3060 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3062 if (DECL_C_BIT_FIELD (field))
3064 error ("attempt to take address of bit-field structure member `%s'",
3065 IDENTIFIER_POINTER (DECL_NAME (field)));
3066 return error_mark_node;
3069 addr = convert (argtype, addr);
3071 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3073 tree offset
3074 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3075 size_int (BITS_PER_UNIT));
3076 int flag = TREE_CONSTANT (addr);
3077 addr = fold (build (PLUS_EXPR, argtype,
3078 addr, convert (argtype, offset)));
3079 TREE_CONSTANT (addr) = flag;
3082 else
3083 addr = build1 (code, argtype, arg);
3085 /* Address of a static or external variable or
3086 file-scope function counts as a constant. */
3087 if (staticp (arg)
3088 && ! (TREE_CODE (arg) == FUNCTION_DECL
3089 && DECL_CONTEXT (arg) != 0))
3090 TREE_CONSTANT (addr) = 1;
3091 return addr;
3095 if (!errstring)
3097 if (argtype == 0)
3098 argtype = TREE_TYPE (arg);
3099 return fold (build1 (code, argtype, arg));
3102 error (errstring);
3103 return error_mark_node;
3106 #if 0
3107 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3108 convert ARG with the same conversions in the same order
3109 and return the result. */
3111 static tree
3112 convert_sequence (conversions, arg)
3113 tree conversions;
3114 tree arg;
3116 switch (TREE_CODE (conversions))
3118 case NOP_EXPR:
3119 case CONVERT_EXPR:
3120 case FLOAT_EXPR:
3121 case FIX_TRUNC_EXPR:
3122 case FIX_FLOOR_EXPR:
3123 case FIX_ROUND_EXPR:
3124 case FIX_CEIL_EXPR:
3125 return convert (TREE_TYPE (conversions),
3126 convert_sequence (TREE_OPERAND (conversions, 0),
3127 arg));
3129 default:
3130 return arg;
3133 #endif /* 0 */
3135 /* Return nonzero if REF is an lvalue valid for this language.
3136 Lvalues can be assigned, unless their type has TYPE_READONLY.
3137 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3140 lvalue_p (ref)
3141 tree ref;
3143 register enum tree_code code = TREE_CODE (ref);
3145 switch (code)
3147 case REALPART_EXPR:
3148 case IMAGPART_EXPR:
3149 case COMPONENT_REF:
3150 return lvalue_p (TREE_OPERAND (ref, 0));
3152 case STRING_CST:
3153 return 1;
3155 case INDIRECT_REF:
3156 case ARRAY_REF:
3157 case VAR_DECL:
3158 case PARM_DECL:
3159 case RESULT_DECL:
3160 case ERROR_MARK:
3161 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3162 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
3163 return 1;
3164 break;
3166 return 0;
3169 /* Return nonzero if REF is an lvalue valid for this language;
3170 otherwise, print an error message and return zero. */
3173 lvalue_or_else (ref, string)
3174 tree ref;
3175 char *string;
3177 int win = lvalue_p (ref);
3178 if (! win)
3179 error ("invalid lvalue in %s", string);
3180 return win;
3183 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3184 for certain kinds of expressions which are not really lvalues
3185 but which we can accept as lvalues.
3187 If ARG is not a kind of expression we can handle, return zero. */
3189 static tree
3190 unary_complex_lvalue (code, arg)
3191 enum tree_code code;
3192 tree arg;
3194 /* Handle (a, b) used as an "lvalue". */
3195 if (TREE_CODE (arg) == COMPOUND_EXPR)
3197 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3198 pedantic_lvalue_warning (COMPOUND_EXPR);
3199 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3200 TREE_OPERAND (arg, 0), real_result);
3203 /* Handle (a ? b : c) used as an "lvalue". */
3204 if (TREE_CODE (arg) == COND_EXPR)
3206 pedantic_lvalue_warning (COND_EXPR);
3207 return (build_conditional_expr
3208 (TREE_OPERAND (arg, 0),
3209 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3210 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3213 return 0;
3216 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3217 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3219 static void
3220 pedantic_lvalue_warning (code)
3221 enum tree_code code;
3223 if (pedantic)
3224 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3225 code == COND_EXPR ? "conditional"
3226 : code == COMPOUND_EXPR ? "compound" : "cast");
3229 /* Warn about storing in something that is `const'. */
3231 void
3232 readonly_warning (arg, string)
3233 tree arg;
3234 char *string;
3236 char buf[80];
3237 strcpy (buf, string);
3239 /* Forbid assignments to iterators. */
3240 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3242 strcat (buf, " of iterator `%s'");
3243 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3246 if (TREE_CODE (arg) == COMPONENT_REF)
3248 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3249 readonly_warning (TREE_OPERAND (arg, 0), string);
3250 else
3252 strcat (buf, " of read-only member `%s'");
3253 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3256 else if (TREE_CODE (arg) == VAR_DECL)
3258 strcat (buf, " of read-only variable `%s'");
3259 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3261 else
3263 pedwarn ("%s of read-only location", buf);
3267 /* Mark EXP saying that we need to be able to take the
3268 address of it; it should not be allocated in a register.
3269 Value is 1 if successful. */
3272 mark_addressable (exp)
3273 tree exp;
3275 register tree x = exp;
3276 while (1)
3277 switch (TREE_CODE (x))
3279 case COMPONENT_REF:
3280 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3282 error ("cannot take address of bitfield `%s'",
3283 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3284 return 0;
3287 /* ... fall through ... */
3289 case ADDR_EXPR:
3290 case ARRAY_REF:
3291 case REALPART_EXPR:
3292 case IMAGPART_EXPR:
3293 x = TREE_OPERAND (x, 0);
3294 break;
3296 case CONSTRUCTOR:
3297 TREE_ADDRESSABLE (x) = 1;
3298 return 1;
3300 case VAR_DECL:
3301 case CONST_DECL:
3302 case PARM_DECL:
3303 case RESULT_DECL:
3304 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3305 && DECL_NONLOCAL (x))
3307 if (TREE_PUBLIC (x))
3309 error ("global register variable `%s' used in nested function",
3310 IDENTIFIER_POINTER (DECL_NAME (x)));
3311 return 0;
3313 pedwarn ("register variable `%s' used in nested function",
3314 IDENTIFIER_POINTER (DECL_NAME (x)));
3316 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3318 if (TREE_PUBLIC (x))
3320 error ("address of global register variable `%s' requested",
3321 IDENTIFIER_POINTER (DECL_NAME (x)));
3322 return 0;
3325 /* If we are making this addressable due to its having
3326 volatile components, give a different error message. Also
3327 handle the case of an unnamed parameter by not trying
3328 to give the name. */
3330 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3332 error ("cannot put object with volatile field into register");
3333 return 0;
3336 pedwarn ("address of register variable `%s' requested",
3337 IDENTIFIER_POINTER (DECL_NAME (x)));
3339 put_var_into_stack (x);
3341 /* drops in */
3342 case FUNCTION_DECL:
3343 TREE_ADDRESSABLE (x) = 1;
3344 #if 0 /* poplevel deals with this now. */
3345 if (DECL_CONTEXT (x) == 0)
3346 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3347 #endif
3349 default:
3350 return 1;
3354 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3356 tree
3357 build_conditional_expr (ifexp, op1, op2)
3358 tree ifexp, op1, op2;
3360 register tree type1;
3361 register tree type2;
3362 register enum tree_code code1;
3363 register enum tree_code code2;
3364 register tree result_type = NULL;
3365 tree orig_op1 = op1, orig_op2 = op2;
3367 /* If second operand is omitted, it is the same as the first one;
3368 make sure it is calculated only once. */
3369 if (op1 == 0)
3371 if (pedantic)
3372 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression");
3373 ifexp = orig_op1 = op1 = save_expr (ifexp);
3376 ifexp = truthvalue_conversion (default_conversion (ifexp));
3378 #if 0 /* Produces wrong result if within sizeof. */
3379 /* Don't promote the operands separately if they promote
3380 the same way. Return the unpromoted type and let the combined
3381 value get promoted if necessary. */
3383 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3384 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3385 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3386 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3388 if (TREE_CODE (ifexp) == INTEGER_CST)
3389 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3391 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3393 #endif
3395 /* Promote both alternatives. */
3397 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3398 op1 = default_conversion (op1);
3399 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3400 op2 = default_conversion (op2);
3402 if (TREE_CODE (ifexp) == ERROR_MARK
3403 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3404 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3405 return error_mark_node;
3407 type1 = TREE_TYPE (op1);
3408 code1 = TREE_CODE (type1);
3409 type2 = TREE_TYPE (op2);
3410 code2 = TREE_CODE (type2);
3412 /* Quickly detect the usual case where op1 and op2 have the same type
3413 after promotion. */
3414 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3416 if (type1 == type2)
3417 result_type = type1;
3418 else
3419 result_type = TYPE_MAIN_VARIANT (type1);
3421 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3422 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3424 result_type = common_type (type1, type2);
3426 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3428 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3429 pedwarn ("ANSI C forbids conditional expr with only one void side");
3430 result_type = void_type_node;
3432 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3434 if (comp_target_types (type1, type2))
3435 result_type = common_type (type1, type2);
3436 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3437 && TREE_CODE (orig_op1) != NOP_EXPR)
3438 result_type = qualify_type (type2, type1);
3439 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3440 && TREE_CODE (orig_op2) != NOP_EXPR)
3441 result_type = qualify_type (type1, type2);
3442 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3444 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3445 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3446 result_type = qualify_type (type1, type2);
3448 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3450 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3451 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3452 result_type = qualify_type (type2, type1);
3454 else
3456 pedwarn ("pointer type mismatch in conditional expression");
3457 result_type = build_pointer_type (void_type_node);
3460 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3462 if (! integer_zerop (op2))
3463 pedwarn ("pointer/integer type mismatch in conditional expression");
3464 else
3466 op2 = null_pointer_node;
3467 #if 0 /* The spec seems to say this is permitted. */
3468 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3469 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3470 #endif
3472 result_type = type1;
3474 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3476 if (!integer_zerop (op1))
3477 pedwarn ("pointer/integer type mismatch in conditional expression");
3478 else
3480 op1 = null_pointer_node;
3481 #if 0 /* The spec seems to say this is permitted. */
3482 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3483 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3484 #endif
3486 result_type = type2;
3489 if (!result_type)
3491 if (flag_cond_mismatch)
3492 result_type = void_type_node;
3493 else
3495 error ("type mismatch in conditional expression");
3496 return error_mark_node;
3500 /* Merge const and volatile flags of the incoming types. */
3501 result_type
3502 = build_type_variant (result_type,
3503 TREE_READONLY (op1) || TREE_READONLY (op2),
3504 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3506 if (result_type != TREE_TYPE (op1))
3507 op1 = convert_and_check (result_type, op1);
3508 if (result_type != TREE_TYPE (op2))
3509 op2 = convert_and_check (result_type, op2);
3511 #if 0
3512 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3514 result_type = TREE_TYPE (op1);
3515 if (TREE_CONSTANT (ifexp))
3516 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3518 if (TYPE_MODE (result_type) == BLKmode)
3520 register tree tempvar
3521 = build_decl (VAR_DECL, NULL_TREE, result_type);
3522 register tree xop1 = build_modify_expr (tempvar, op1);
3523 register tree xop2 = build_modify_expr (tempvar, op2);
3524 register tree result = fold (build (COND_EXPR, result_type,
3525 ifexp, xop1, xop2));
3527 layout_decl (tempvar, TYPE_ALIGN (result_type));
3528 /* No way to handle variable-sized objects here.
3529 I fear that the entire handling of BLKmode conditional exprs
3530 needs to be redone. */
3531 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3532 abort ();
3533 DECL_RTL (tempvar)
3534 = assign_stack_local (DECL_MODE (tempvar),
3535 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3536 + BITS_PER_UNIT - 1)
3537 / BITS_PER_UNIT,
3540 TREE_SIDE_EFFECTS (result)
3541 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3542 | TREE_SIDE_EFFECTS (op2);
3543 return build (COMPOUND_EXPR, result_type, result, tempvar);
3546 #endif /* 0 */
3548 if (TREE_CODE (ifexp) == INTEGER_CST)
3549 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3551 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3554 /* Given a list of expressions, return a compound expression
3555 that performs them all and returns the value of the last of them. */
3557 tree
3558 build_compound_expr (list)
3559 tree list;
3561 return internal_build_compound_expr (list, TRUE);
3564 static tree
3565 internal_build_compound_expr (list, first_p)
3566 tree list;
3567 int first_p;
3569 register tree rest;
3571 if (TREE_CHAIN (list) == 0)
3573 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3574 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3576 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3577 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3578 list = TREE_OPERAND (list, 0);
3579 #endif
3581 /* Don't let (0, 0) be null pointer constant. */
3582 if (!first_p && integer_zerop (TREE_VALUE (list)))
3583 return non_lvalue (TREE_VALUE (list));
3584 return TREE_VALUE (list);
3587 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3589 /* Convert arrays to pointers when there really is a comma operator. */
3590 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3591 TREE_VALUE (TREE_CHAIN (list))
3592 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3595 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3597 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3599 /* The left-hand operand of a comma expression is like an expression
3600 statement: with -W or -Wunused, we should warn if it doesn't have
3601 any side-effects, unless it was explicitly cast to (void). */
3602 if ((extra_warnings || warn_unused)
3603 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3604 && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
3605 warning ("left-hand operand of comma expression has no effect");
3607 /* When pedantic, a compound expression can be neither an lvalue
3608 nor an integer constant expression. */
3609 if (! pedantic)
3610 return rest;
3613 /* With -Wunused, we should also warn if the left-hand operand does have
3614 side-effects, but computes a value which is not used. For example, in
3615 `foo() + bar(), baz()' the result of the `+' operator is not used,
3616 so we should issue a warning. */
3617 else if (warn_unused)
3618 warn_if_unused_value (TREE_VALUE (list));
3620 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3623 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3625 tree
3626 build_c_cast (type, expr)
3627 register tree type;
3628 tree expr;
3630 register tree value = expr;
3632 if (type == error_mark_node || expr == error_mark_node)
3633 return error_mark_node;
3634 type = TYPE_MAIN_VARIANT (type);
3636 #if 0
3637 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3638 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3639 value = TREE_OPERAND (value, 0);
3640 #endif
3642 if (TREE_CODE (type) == ARRAY_TYPE)
3644 error ("cast specifies array type");
3645 return error_mark_node;
3648 if (TREE_CODE (type) == FUNCTION_TYPE)
3650 error ("cast specifies function type");
3651 return error_mark_node;
3654 if (type == TREE_TYPE (value))
3656 if (pedantic)
3658 if (TREE_CODE (type) == RECORD_TYPE
3659 || TREE_CODE (type) == UNION_TYPE)
3660 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3663 else if (TREE_CODE (type) == UNION_TYPE)
3665 tree field;
3666 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3667 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3668 value = default_conversion (value);
3670 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3671 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3672 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3673 break;
3675 if (field)
3677 char *name;
3678 tree t;
3680 if (pedantic)
3681 pedwarn ("ANSI C forbids casts to union type");
3682 if (TYPE_NAME (type) != 0)
3684 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3685 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3686 else
3687 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3689 else
3690 name = "";
3691 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3692 build_tree_list (field, value)),
3693 0, 0);
3694 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3695 return t;
3697 error ("cast to union type from type not present in union");
3698 return error_mark_node;
3700 else
3702 tree otype, ovalue;
3704 /* If casting to void, avoid the error that would come
3705 from default_conversion in the case of a non-lvalue array. */
3706 if (type == void_type_node)
3707 return build1 (CONVERT_EXPR, type, value);
3709 /* Convert functions and arrays to pointers,
3710 but don't convert any other types. */
3711 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3712 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3713 value = default_conversion (value);
3714 otype = TREE_TYPE (value);
3716 /* Optionally warn about potentially worrisome casts. */
3718 if (warn_cast_qual
3719 && TREE_CODE (type) == POINTER_TYPE
3720 && TREE_CODE (otype) == POINTER_TYPE)
3722 if (TYPE_VOLATILE (TREE_TYPE (otype))
3723 && ! TYPE_VOLATILE (TREE_TYPE (type)))
3724 pedwarn ("cast discards `volatile' from pointer target type");
3725 if (TYPE_READONLY (TREE_TYPE (otype))
3726 && ! TYPE_READONLY (TREE_TYPE (type)))
3727 pedwarn ("cast discards `const' from pointer target type");
3730 /* Warn about possible alignment problems. */
3731 if (STRICT_ALIGNMENT && warn_cast_align
3732 && TREE_CODE (type) == POINTER_TYPE
3733 && TREE_CODE (otype) == POINTER_TYPE
3734 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3735 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3736 /* Don't warn about opaque types, where the actual alignment
3737 restriction is unknown. */
3738 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3739 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3740 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3741 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3742 warning ("cast increases required alignment of target type");
3744 if (TREE_CODE (type) == INTEGER_TYPE
3745 && TREE_CODE (otype) == POINTER_TYPE
3746 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3747 && !TREE_CONSTANT (value))
3748 warning ("cast from pointer to integer of different size");
3750 if (warn_bad_function_cast
3751 && TREE_CODE (value) == CALL_EXPR
3752 && TREE_CODE (type) != TREE_CODE (otype))
3753 warning ("cast does not match function type");
3755 if (TREE_CODE (type) == POINTER_TYPE
3756 && TREE_CODE (otype) == INTEGER_TYPE
3757 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3758 #if 0
3759 /* Don't warn about converting 0 to pointer,
3760 provided the 0 was explicit--not cast or made by folding. */
3761 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3762 #endif
3763 /* Don't warn about converting any constant. */
3764 && !TREE_CONSTANT (value))
3765 warning ("cast to pointer from integer of different size");
3767 ovalue = value;
3768 value = convert (type, value);
3770 /* Ignore any integer overflow caused by the cast. */
3771 if (TREE_CODE (value) == INTEGER_CST)
3773 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3774 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3778 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3779 if (pedantic && TREE_CODE (value) == INTEGER_CST
3780 && TREE_CODE (expr) == INTEGER_CST
3781 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3782 value = non_lvalue (value);
3784 /* If pedantic, don't let a cast be an lvalue. */
3785 if (value == expr && pedantic)
3786 value = non_lvalue (value);
3788 return value;
3791 /* Build an assignment expression of lvalue LHS from value RHS.
3792 MODIFYCODE is the code for a binary operator that we use
3793 to combine the old value of LHS with RHS to get the new value.
3794 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3796 tree
3797 build_modify_expr (lhs, modifycode, rhs)
3798 tree lhs, rhs;
3799 enum tree_code modifycode;
3801 register tree result;
3802 tree newrhs;
3803 tree lhstype = TREE_TYPE (lhs);
3804 tree olhstype = lhstype;
3806 /* Types that aren't fully specified cannot be used in assignments. */
3807 lhs = require_complete_type (lhs);
3809 /* Avoid duplicate error messages from operands that had errors. */
3810 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3811 return error_mark_node;
3813 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3814 /* Do not use STRIP_NOPS here. We do not want an enumerator
3815 whose value is 0 to count as a null pointer constant. */
3816 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3817 rhs = TREE_OPERAND (rhs, 0);
3819 newrhs = rhs;
3821 /* Handle control structure constructs used as "lvalues". */
3823 switch (TREE_CODE (lhs))
3825 /* Handle (a, b) used as an "lvalue". */
3826 case COMPOUND_EXPR:
3827 pedantic_lvalue_warning (COMPOUND_EXPR);
3828 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3829 modifycode, rhs);
3830 if (TREE_CODE (newrhs) == ERROR_MARK)
3831 return error_mark_node;
3832 return build (COMPOUND_EXPR, lhstype,
3833 TREE_OPERAND (lhs, 0), newrhs);
3835 /* Handle (a ? b : c) used as an "lvalue". */
3836 case COND_EXPR:
3837 pedantic_lvalue_warning (COND_EXPR);
3838 rhs = save_expr (rhs);
3840 /* Produce (a ? (b = rhs) : (c = rhs))
3841 except that the RHS goes through a save-expr
3842 so the code to compute it is only emitted once. */
3843 tree cond
3844 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3845 build_modify_expr (TREE_OPERAND (lhs, 1),
3846 modifycode, rhs),
3847 build_modify_expr (TREE_OPERAND (lhs, 2),
3848 modifycode, rhs));
3849 if (TREE_CODE (cond) == ERROR_MARK)
3850 return cond;
3851 /* Make sure the code to compute the rhs comes out
3852 before the split. */
3853 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3854 /* But cast it to void to avoid an "unused" error. */
3855 convert (void_type_node, rhs), cond);
3859 /* If a binary op has been requested, combine the old LHS value with the RHS
3860 producing the value we should actually store into the LHS. */
3862 if (modifycode != NOP_EXPR)
3864 lhs = stabilize_reference (lhs);
3865 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3868 /* Handle a cast used as an "lvalue".
3869 We have already performed any binary operator using the value as cast.
3870 Now convert the result to the cast type of the lhs,
3871 and then true type of the lhs and store it there;
3872 then convert result back to the cast type to be the value
3873 of the assignment. */
3875 switch (TREE_CODE (lhs))
3877 case NOP_EXPR:
3878 case CONVERT_EXPR:
3879 case FLOAT_EXPR:
3880 case FIX_TRUNC_EXPR:
3881 case FIX_FLOOR_EXPR:
3882 case FIX_ROUND_EXPR:
3883 case FIX_CEIL_EXPR:
3884 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3885 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3886 newrhs = default_conversion (newrhs);
3888 tree inner_lhs = TREE_OPERAND (lhs, 0);
3889 tree result;
3890 result = build_modify_expr (inner_lhs, NOP_EXPR,
3891 convert (TREE_TYPE (inner_lhs),
3892 convert (lhstype, newrhs)));
3893 if (TREE_CODE (result) == ERROR_MARK)
3894 return result;
3895 pedantic_lvalue_warning (CONVERT_EXPR);
3896 return convert (TREE_TYPE (lhs), result);
3900 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3901 Reject anything strange now. */
3903 if (!lvalue_or_else (lhs, "assignment"))
3904 return error_mark_node;
3906 /* Warn about storing in something that is `const'. */
3908 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3909 || ((TREE_CODE (lhstype) == RECORD_TYPE
3910 || TREE_CODE (lhstype) == UNION_TYPE)
3911 && C_TYPE_FIELDS_READONLY (lhstype)))
3912 readonly_warning (lhs, "assignment");
3914 /* If storing into a structure or union member,
3915 it has probably been given type `int'.
3916 Compute the type that would go with
3917 the actual amount of storage the member occupies. */
3919 if (TREE_CODE (lhs) == COMPONENT_REF
3920 && (TREE_CODE (lhstype) == INTEGER_TYPE
3921 || TREE_CODE (lhstype) == REAL_TYPE
3922 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3923 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3925 /* If storing in a field that is in actuality a short or narrower than one,
3926 we must store in the field in its actual type. */
3928 if (lhstype != TREE_TYPE (lhs))
3930 lhs = copy_node (lhs);
3931 TREE_TYPE (lhs) = lhstype;
3934 /* Convert new value to destination type. */
3936 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
3937 NULL_TREE, NULL_TREE, 0);
3938 if (TREE_CODE (newrhs) == ERROR_MARK)
3939 return error_mark_node;
3941 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3942 TREE_SIDE_EFFECTS (result) = 1;
3944 /* If we got the LHS in a different type for storing in,
3945 convert the result back to the nominal type of LHS
3946 so that the value we return always has the same type
3947 as the LHS argument. */
3949 if (olhstype == TREE_TYPE (result))
3950 return result;
3951 return convert_for_assignment (olhstype, result, "assignment",
3952 NULL_TREE, NULL_TREE, 0);
3955 /* Convert value RHS to type TYPE as preparation for an assignment
3956 to an lvalue of type TYPE.
3957 The real work of conversion is done by `convert'.
3958 The purpose of this function is to generate error messages
3959 for assignments that are not allowed in C.
3960 ERRTYPE is a string to use in error messages:
3961 "assignment", "return", etc. If it is null, this is parameter passing
3962 for a function call (and different error messages are output). Otherwise,
3963 it may be a name stored in the spelling stack and interpreted by
3964 get_spelling.
3966 FUNNAME is the name of the function being called,
3967 as an IDENTIFIER_NODE, or null.
3968 PARMNUM is the number of the argument, for printing in error messages. */
3970 static tree
3971 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3972 tree type, rhs;
3973 char *errtype;
3974 tree fundecl, funname;
3975 int parmnum;
3977 register enum tree_code codel = TREE_CODE (type);
3978 register tree rhstype;
3979 register enum tree_code coder;
3981 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3982 /* Do not use STRIP_NOPS here. We do not want an enumerator
3983 whose value is 0 to count as a null pointer constant. */
3984 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3985 rhs = TREE_OPERAND (rhs, 0);
3987 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3988 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3989 rhs = default_conversion (rhs);
3990 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3991 rhs = decl_constant_value (rhs);
3993 rhstype = TREE_TYPE (rhs);
3994 coder = TREE_CODE (rhstype);
3996 if (coder == ERROR_MARK)
3997 return error_mark_node;
3999 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4001 overflow_warning (rhs);
4002 /* Check for Objective-C protocols. This will issue a warning if
4003 there are protocol violations. No need to use the return value. */
4004 maybe_objc_comptypes (type, rhstype, 0);
4005 return rhs;
4008 if (coder == VOID_TYPE)
4010 error ("void value not ignored as it ought to be");
4011 return error_mark_node;
4013 /* Arithmetic types all interconvert, and enum is treated like int. */
4014 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
4015 || codel == COMPLEX_TYPE)
4016 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
4017 || coder == COMPLEX_TYPE))
4018 return convert_and_check (type, rhs);
4020 /* Conversion to a transparent union from its member types.
4021 This applies only to function arguments. */
4022 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4024 tree memb_types;
4025 tree marginal_memb_type = 0;
4027 for (memb_types = TYPE_FIELDS (type); memb_types;
4028 memb_types = TREE_CHAIN (memb_types))
4030 tree memb_type = TREE_TYPE (memb_types);
4032 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4033 TYPE_MAIN_VARIANT (rhstype)))
4034 break;
4036 if (TREE_CODE (memb_type) != POINTER_TYPE)
4037 continue;
4039 if (coder == POINTER_TYPE)
4041 register tree ttl = TREE_TYPE (memb_type);
4042 register tree ttr = TREE_TYPE (rhstype);
4044 /* Any non-function converts to a [const][volatile] void *
4045 and vice versa; otherwise, targets must be the same.
4046 Meanwhile, the lhs target must have all the qualifiers of
4047 the rhs. */
4048 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4049 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4050 || comp_target_types (memb_type, rhstype))
4052 /* If this type won't generate any warnings, use it. */
4053 if ((TREE_CODE (ttr) == FUNCTION_TYPE
4054 && TREE_CODE (ttl) == FUNCTION_TYPE)
4055 ? ((! TYPE_READONLY (ttl) | TYPE_READONLY (ttr))
4056 & (! TYPE_VOLATILE (ttl) | TYPE_VOLATILE (ttr)))
4057 : ((TYPE_READONLY (ttl) | ! TYPE_READONLY (ttr))
4058 & (TYPE_VOLATILE (ttl) | ! TYPE_VOLATILE (ttr))))
4059 break;
4061 /* Keep looking for a better type, but remember this one. */
4062 if (! marginal_memb_type)
4063 marginal_memb_type = memb_type;
4067 /* Can convert integer zero to any pointer type. */
4068 if (integer_zerop (rhs)
4069 || (TREE_CODE (rhs) == NOP_EXPR
4070 && integer_zerop (TREE_OPERAND (rhs, 0))))
4072 rhs = null_pointer_node;
4073 break;
4077 if (memb_types || marginal_memb_type)
4079 if (! memb_types)
4081 /* We have only a marginally acceptable member type;
4082 it needs a warning. */
4083 register tree ttl = TREE_TYPE (marginal_memb_type);
4084 register tree ttr = TREE_TYPE (rhstype);
4086 /* Const and volatile mean something different for function
4087 types, so the usual warnings are not appropriate. */
4088 if (TREE_CODE (ttr) == FUNCTION_TYPE
4089 && TREE_CODE (ttl) == FUNCTION_TYPE)
4091 /* Because const and volatile on functions are
4092 restrictions that say the function will not do
4093 certain things, it is okay to use a const or volatile
4094 function where an ordinary one is wanted, but not
4095 vice-versa. */
4096 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4097 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4098 get_spelling (errtype), funname,
4099 parmnum);
4100 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4101 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4102 get_spelling (errtype), funname,
4103 parmnum);
4105 else
4107 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4108 warn_for_assignment ("%s discards `const' from pointer target type",
4109 get_spelling (errtype), funname,
4110 parmnum);
4111 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4112 warn_for_assignment ("%s discards `volatile' from pointer target type",
4113 get_spelling (errtype), funname,
4114 parmnum);
4118 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4119 pedwarn ("ANSI C prohibits argument conversion to union type");
4121 return build1 (NOP_EXPR, type, rhs);
4125 /* Conversions among pointers */
4126 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
4128 register tree ttl = TREE_TYPE (type);
4129 register tree ttr = TREE_TYPE (rhstype);
4131 /* Any non-function converts to a [const][volatile] void *
4132 and vice versa; otherwise, targets must be the same.
4133 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4134 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4135 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4136 || comp_target_types (type, rhstype)
4137 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4138 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4140 if (pedantic
4141 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4142 && TREE_CODE (ttr) == FUNCTION_TYPE)
4144 (TYPE_MAIN_VARIANT (ttr) == void_type_node
4145 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4146 which are not ANSI null ptr constants. */
4147 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4148 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4149 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4150 get_spelling (errtype), funname, parmnum);
4151 /* Const and volatile mean something different for function types,
4152 so the usual warnings are not appropriate. */
4153 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4154 && TREE_CODE (ttl) != FUNCTION_TYPE)
4156 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4157 warn_for_assignment ("%s discards `const' from pointer target type",
4158 get_spelling (errtype), funname, parmnum);
4159 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4160 warn_for_assignment ("%s discards `volatile' from pointer target type",
4161 get_spelling (errtype), funname, parmnum);
4162 /* If this is not a case of ignoring a mismatch in signedness,
4163 no warning. */
4164 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4165 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4166 || comp_target_types (type, rhstype))
4168 /* If there is a mismatch, do warn. */
4169 else if (pedantic)
4170 warn_for_assignment ("pointer targets in %s differ in signedness",
4171 get_spelling (errtype), funname, parmnum);
4173 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4174 && TREE_CODE (ttr) == FUNCTION_TYPE)
4176 /* Because const and volatile on functions are restrictions
4177 that say the function will not do certain things,
4178 it is okay to use a const or volatile function
4179 where an ordinary one is wanted, but not vice-versa. */
4180 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4181 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4182 get_spelling (errtype), funname, parmnum);
4183 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4184 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4185 get_spelling (errtype), funname, parmnum);
4188 else
4189 warn_for_assignment ("%s from incompatible pointer type",
4190 get_spelling (errtype), funname, parmnum);
4191 return convert (type, rhs);
4193 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4195 /* An explicit constant 0 can convert to a pointer,
4196 or one that results from arithmetic, even including
4197 a cast to integer type. */
4198 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4200 ! (TREE_CODE (rhs) == NOP_EXPR
4201 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4202 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4203 && integer_zerop (TREE_OPERAND (rhs, 0))))
4205 warn_for_assignment ("%s makes pointer from integer without a cast",
4206 get_spelling (errtype), funname, parmnum);
4207 return convert (type, rhs);
4209 return null_pointer_node;
4211 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4213 warn_for_assignment ("%s makes integer from pointer without a cast",
4214 get_spelling (errtype), funname, parmnum);
4215 return convert (type, rhs);
4218 if (!errtype)
4220 if (funname)
4222 tree selector = maybe_building_objc_message_expr ();
4224 if (selector && parmnum > 2)
4225 error ("incompatible type for argument %d of `%s'",
4226 parmnum - 2, IDENTIFIER_POINTER (selector));
4227 else
4228 error ("incompatible type for argument %d of `%s'",
4229 parmnum, IDENTIFIER_POINTER (funname));
4231 else
4232 error ("incompatible type for argument %d of indirect function call",
4233 parmnum);
4235 else
4236 error ("incompatible types in %s", get_spelling (errtype));
4238 return error_mark_node;
4241 /* Print a warning using MSG.
4242 It gets OPNAME as its one parameter.
4243 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4244 FUNCTION and ARGNUM are handled specially if we are building an
4245 Objective-C selector. */
4247 static void
4248 warn_for_assignment (msg, opname, function, argnum)
4249 char *msg;
4250 char *opname;
4251 tree function;
4252 int argnum;
4254 static char argstring[] = "passing arg %d of `%s'";
4255 static char argnofun[] = "passing arg %d";
4257 if (opname == 0)
4259 tree selector = maybe_building_objc_message_expr ();
4261 if (selector && argnum > 2)
4263 function = selector;
4264 argnum -= 2;
4266 if (function)
4268 /* Function name is known; supply it. */
4269 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4270 + sizeof (argstring) + 25 /*%d*/ + 1);
4271 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4273 else
4275 /* Function name unknown (call through ptr); just give arg number. */
4276 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4277 sprintf (opname, argnofun, argnum);
4280 pedwarn (msg, opname);
4283 /* Return nonzero if VALUE is a valid constant-valued expression
4284 for use in initializing a static variable; one that can be an
4285 element of a "constant" initializer.
4287 Return null_pointer_node if the value is absolute;
4288 if it is relocatable, return the variable that determines the relocation.
4289 We assume that VALUE has been folded as much as possible;
4290 therefore, we do not need to check for such things as
4291 arithmetic-combinations of integers. */
4293 tree
4294 initializer_constant_valid_p (value, endtype)
4295 tree value;
4296 tree endtype;
4298 switch (TREE_CODE (value))
4300 case CONSTRUCTOR:
4301 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4302 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
4303 && TREE_CONSTANT (value)
4304 && CONSTRUCTOR_ELTS (value))
4305 return
4306 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
4307 endtype);
4309 return TREE_STATIC (value) ? null_pointer_node : 0;
4311 case INTEGER_CST:
4312 case REAL_CST:
4313 case STRING_CST:
4314 case COMPLEX_CST:
4315 return null_pointer_node;
4317 case ADDR_EXPR:
4318 return TREE_OPERAND (value, 0);
4320 case NON_LVALUE_EXPR:
4321 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4323 case CONVERT_EXPR:
4324 case NOP_EXPR:
4325 /* Allow conversions between pointer types. */
4326 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4327 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
4328 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4330 /* Allow conversions between real types. */
4331 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4332 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
4333 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4335 /* Allow length-preserving conversions between integer types. */
4336 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4337 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4338 && (TYPE_PRECISION (TREE_TYPE (value))
4339 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4340 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4342 /* Allow conversions between other integer types only if
4343 explicit value. */
4344 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4345 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4347 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4348 endtype);
4349 if (inner == null_pointer_node)
4350 return null_pointer_node;
4351 return 0;
4354 /* Allow (int) &foo provided int is as wide as a pointer. */
4355 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4356 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
4357 && (TYPE_PRECISION (TREE_TYPE (value))
4358 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4359 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4360 endtype);
4362 /* Likewise conversions from int to pointers. */
4363 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4364 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4365 && (TYPE_PRECISION (TREE_TYPE (value))
4366 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4367 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4368 endtype);
4370 /* Allow conversions to union types if the value inside is okay. */
4371 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4372 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4373 endtype);
4374 return 0;
4376 case PLUS_EXPR:
4377 if (TREE_CODE (endtype) == INTEGER_TYPE
4378 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4379 return 0;
4381 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4382 endtype);
4383 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4384 endtype);
4385 /* If either term is absolute, use the other terms relocation. */
4386 if (valid0 == null_pointer_node)
4387 return valid1;
4388 if (valid1 == null_pointer_node)
4389 return valid0;
4390 return 0;
4393 case MINUS_EXPR:
4394 if (TREE_CODE (endtype) == INTEGER_TYPE
4395 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4396 return 0;
4398 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4399 endtype);
4400 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4401 endtype);
4402 /* Win if second argument is absolute. */
4403 if (valid1 == null_pointer_node)
4404 return valid0;
4405 /* Win if both arguments have the same relocation.
4406 Then the value is absolute. */
4407 if (valid0 == valid1)
4408 return null_pointer_node;
4409 return 0;
4413 return 0;
4416 /* If VALUE is a compound expr all of whose expressions are constant, then
4417 return its value. Otherwise, return error_mark_node.
4419 This is for handling COMPOUND_EXPRs as initializer elements
4420 which is allowed with a warning when -pedantic is specified. */
4422 static tree
4423 valid_compound_expr_initializer (value, endtype)
4424 tree value;
4425 tree endtype;
4427 if (TREE_CODE (value) == COMPOUND_EXPR)
4429 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4430 == error_mark_node)
4431 return error_mark_node;
4432 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4433 endtype);
4435 else if (! TREE_CONSTANT (value)
4436 && ! initializer_constant_valid_p (value, endtype))
4437 return error_mark_node;
4438 else
4439 return value;
4442 /* Perform appropriate conversions on the initial value of a variable,
4443 store it in the declaration DECL,
4444 and print any error messages that are appropriate.
4445 If the init is invalid, store an ERROR_MARK. */
4447 void
4448 store_init_value (decl, init)
4449 tree decl, init;
4451 register tree value, type;
4453 /* If variable's type was invalidly declared, just ignore it. */
4455 type = TREE_TYPE (decl);
4456 if (TREE_CODE (type) == ERROR_MARK)
4457 return;
4459 /* Digest the specified initializer into an expression. */
4461 value = digest_init (type, init, TREE_STATIC (decl),
4462 TREE_STATIC (decl) || pedantic);
4464 /* Store the expression if valid; else report error. */
4466 #if 0
4467 /* Note that this is the only place we can detect the error
4468 in a case such as struct foo bar = (struct foo) { x, y };
4469 where there is one initial value which is a constructor expression. */
4470 if (value == error_mark_node)
4472 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4474 error ("initializer for static variable is not constant");
4475 value = error_mark_node;
4477 else if (TREE_STATIC (decl)
4478 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4480 error ("initializer for static variable uses complicated arithmetic");
4481 value = error_mark_node;
4483 else
4485 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4487 if (! TREE_CONSTANT (value))
4488 pedwarn ("aggregate initializer is not constant");
4489 else if (! TREE_STATIC (value))
4490 pedwarn ("aggregate initializer uses complicated arithmetic");
4493 #endif
4495 DECL_INITIAL (decl) = value;
4497 /* ANSI wants warnings about out-of-range constant initializers. */
4498 STRIP_TYPE_NOPS (value);
4499 constant_expression_warning (value);
4502 /* Methods for storing and printing names for error messages. */
4504 /* Implement a spelling stack that allows components of a name to be pushed
4505 and popped. Each element on the stack is this structure. */
4507 struct spelling
4509 int kind;
4510 union
4512 int i;
4513 char *s;
4514 } u;
4517 #define SPELLING_STRING 1
4518 #define SPELLING_MEMBER 2
4519 #define SPELLING_BOUNDS 3
4521 static struct spelling *spelling; /* Next stack element (unused). */
4522 static struct spelling *spelling_base; /* Spelling stack base. */
4523 static int spelling_size; /* Size of the spelling stack. */
4525 /* Macros to save and restore the spelling stack around push_... functions.
4526 Alternative to SAVE_SPELLING_STACK. */
4528 #define SPELLING_DEPTH() (spelling - spelling_base)
4529 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4531 /* Save and restore the spelling stack around arbitrary C code. */
4533 #define SAVE_SPELLING_DEPTH(code) \
4535 int __depth = SPELLING_DEPTH (); \
4536 code; \
4537 RESTORE_SPELLING_DEPTH (__depth); \
4540 /* Push an element on the spelling stack with type KIND and assign VALUE
4541 to MEMBER. */
4543 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4545 int depth = SPELLING_DEPTH (); \
4547 if (depth >= spelling_size) \
4549 spelling_size += 10; \
4550 if (spelling_base == 0) \
4551 spelling_base \
4552 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4553 else \
4554 spelling_base \
4555 = (struct spelling *) xrealloc (spelling_base, \
4556 spelling_size * sizeof (struct spelling)); \
4557 RESTORE_SPELLING_DEPTH (depth); \
4560 spelling->kind = (KIND); \
4561 spelling->MEMBER = (VALUE); \
4562 spelling++; \
4565 /* Push STRING on the stack. Printed literally. */
4567 static void
4568 push_string (string)
4569 char *string;
4571 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4574 /* Push a member name on the stack. Printed as '.' STRING. */
4576 static void
4577 push_member_name (decl)
4578 tree decl;
4581 char *string
4582 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4583 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4586 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4588 static void
4589 push_array_bounds (bounds)
4590 int bounds;
4592 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4595 /* Compute the maximum size in bytes of the printed spelling. */
4597 static int
4598 spelling_length ()
4600 register int size = 0;
4601 register struct spelling *p;
4603 for (p = spelling_base; p < spelling; p++)
4605 if (p->kind == SPELLING_BOUNDS)
4606 size += 25;
4607 else
4608 size += strlen (p->u.s) + 1;
4611 return size;
4614 /* Print the spelling to BUFFER and return it. */
4616 static char *
4617 print_spelling (buffer)
4618 register char *buffer;
4620 register char *d = buffer;
4621 register char *s;
4622 register struct spelling *p;
4624 for (p = spelling_base; p < spelling; p++)
4625 if (p->kind == SPELLING_BOUNDS)
4627 sprintf (d, "[%d]", p->u.i);
4628 d += strlen (d);
4630 else
4632 if (p->kind == SPELLING_MEMBER)
4633 *d++ = '.';
4634 for (s = p->u.s; *d = *s++; d++)
4637 *d++ = '\0';
4638 return buffer;
4641 /* Provide a means to pass component names derived from the spelling stack. */
4643 char initialization_message;
4645 /* Interpret the spelling of the given ERRTYPE message. */
4647 static char *
4648 get_spelling (errtype)
4649 char *errtype;
4651 static char *buffer;
4652 static int size = -1;
4654 if (errtype == &initialization_message)
4656 /* Avoid counting chars */
4657 static char message[] = "initialization of `%s'";
4658 register int needed = sizeof (message) + spelling_length () + 1;
4659 char *temp;
4661 if (size < 0)
4662 buffer = (char *) xmalloc (size = needed);
4663 if (needed > size)
4664 buffer = (char *) xrealloc (buffer, size = needed);
4666 temp = (char *) alloca (needed);
4667 sprintf (buffer, message, print_spelling (temp));
4668 return buffer;
4671 return errtype;
4674 /* Issue an error message for a bad initializer component.
4675 FORMAT describes the message. OFWHAT is the name for the component.
4676 LOCAL is a format string for formatting the insertion of the name
4677 into the message.
4679 If OFWHAT is null, the component name is stored on the spelling stack.
4680 If the component name is a null string, then LOCAL is omitted entirely. */
4682 void
4683 error_init (format, local, ofwhat)
4684 char *format, *local, *ofwhat;
4686 char *buffer;
4688 if (ofwhat == 0)
4689 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4690 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4692 if (*ofwhat)
4693 sprintf (buffer, local, ofwhat);
4694 else
4695 buffer[0] = 0;
4697 error (format, buffer);
4700 /* Issue a pedantic warning for a bad initializer component.
4701 FORMAT describes the message. OFWHAT is the name for the component.
4702 LOCAL is a format string for formatting the insertion of the name
4703 into the message.
4705 If OFWHAT is null, the component name is stored on the spelling stack.
4706 If the component name is a null string, then LOCAL is omitted entirely. */
4708 void
4709 pedwarn_init (format, local, ofwhat)
4710 char *format, *local, *ofwhat;
4712 char *buffer;
4714 if (ofwhat == 0)
4715 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4716 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4718 if (*ofwhat)
4719 sprintf (buffer, local, ofwhat);
4720 else
4721 buffer[0] = 0;
4723 pedwarn (format, buffer);
4726 /* Issue a warning for a bad initializer component.
4727 FORMAT describes the message. OFWHAT is the name for the component.
4728 LOCAL is a format string for formatting the insertion of the name
4729 into the message.
4731 If OFWHAT is null, the component name is stored on the spelling stack.
4732 If the component name is a null string, then LOCAL is omitted entirely. */
4734 static void
4735 warning_init (format, local, ofwhat)
4736 char *format, *local, *ofwhat;
4738 char *buffer;
4740 if (ofwhat == 0)
4741 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4742 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4744 if (*ofwhat)
4745 sprintf (buffer, local, ofwhat);
4746 else
4747 buffer[0] = 0;
4749 warning (format, buffer);
4752 /* Digest the parser output INIT as an initializer for type TYPE.
4753 Return a C expression of type TYPE to represent the initial value.
4755 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4756 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4757 applies only to elements of constructors. */
4759 static tree
4760 digest_init (type, init, require_constant, constructor_constant)
4761 tree type, init;
4762 int require_constant, constructor_constant;
4764 enum tree_code code = TREE_CODE (type);
4765 tree inside_init = init;
4767 if (init == error_mark_node)
4768 return init;
4770 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4771 /* Do not use STRIP_NOPS here. We do not want an enumerator
4772 whose value is 0 to count as a null pointer constant. */
4773 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4774 inside_init = TREE_OPERAND (init, 0);
4776 /* Initialization of an array of chars from a string constant
4777 optionally enclosed in braces. */
4779 if (code == ARRAY_TYPE)
4781 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4782 if ((typ1 == char_type_node
4783 || typ1 == signed_char_type_node
4784 || typ1 == unsigned_char_type_node
4785 || typ1 == unsigned_wchar_type_node
4786 || typ1 == signed_wchar_type_node)
4787 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4789 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4790 TYPE_MAIN_VARIANT (type)))
4791 return inside_init;
4793 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4794 != char_type_node)
4795 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4797 error_init ("char-array%s initialized from wide string",
4798 " `%s'", NULL);
4799 return error_mark_node;
4801 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4802 == char_type_node)
4803 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4805 error_init ("int-array%s initialized from non-wide string",
4806 " `%s'", NULL);
4807 return error_mark_node;
4810 TREE_TYPE (inside_init) = type;
4811 if (TYPE_DOMAIN (type) != 0
4812 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4814 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4815 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4816 /* Subtract 1 (or sizeof (wchar_t))
4817 because it's ok to ignore the terminating null char
4818 that is counted in the length of the constant. */
4819 if (size < TREE_STRING_LENGTH (inside_init)
4820 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4821 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4822 : 1))
4823 pedwarn_init (
4824 "initializer-string for array of chars%s is too long",
4825 " `%s'", NULL);
4827 return inside_init;
4831 /* Any type can be initialized
4832 from an expression of the same type, optionally with braces. */
4834 if (inside_init && TREE_TYPE (inside_init) != 0
4835 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4836 TYPE_MAIN_VARIANT (type))
4837 || (code == ARRAY_TYPE
4838 && comptypes (TREE_TYPE (inside_init), type))
4839 || (code == POINTER_TYPE
4840 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4841 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4842 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4843 TREE_TYPE (type)))))
4845 if (code == POINTER_TYPE
4846 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4847 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4848 inside_init = default_conversion (inside_init);
4849 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4850 && TREE_CODE (inside_init) != CONSTRUCTOR)
4852 error_init ("array%s initialized from non-constant array expression",
4853 " `%s'", NULL);
4854 return error_mark_node;
4857 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4858 inside_init = decl_constant_value (inside_init);
4860 /* Compound expressions can only occur here if -pedantic or
4861 -pedantic-errors is specified. In the later case, we always want
4862 an error. In the former case, we simply want a warning. */
4863 if (require_constant && pedantic
4864 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4866 inside_init
4867 = valid_compound_expr_initializer (inside_init,
4868 TREE_TYPE (inside_init));
4869 if (inside_init == error_mark_node)
4870 error_init ("initializer element%s is not constant",
4871 " for `%s'", NULL);
4872 else
4873 pedwarn_init ("initializer element%s is not constant",
4874 " for `%s'", NULL);
4875 if (flag_pedantic_errors)
4876 inside_init = error_mark_node;
4878 else if (require_constant && ! TREE_CONSTANT (inside_init))
4880 error_init ("initializer element%s is not constant",
4881 " for `%s'", NULL);
4882 inside_init = error_mark_node;
4884 else if (require_constant
4885 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4887 error_init ("initializer element%s is not computable at load time",
4888 " for `%s'", NULL);
4889 inside_init = error_mark_node;
4892 return inside_init;
4895 /* Handle scalar types, including conversions. */
4897 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4898 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4900 /* Note that convert_for_assignment calls default_conversion
4901 for arrays and functions. We must not call it in the
4902 case where inside_init is a null pointer constant. */
4903 inside_init
4904 = convert_for_assignment (type, init, "initialization",
4905 NULL_TREE, NULL_TREE, 0);
4907 if (require_constant && ! TREE_CONSTANT (inside_init))
4909 error_init ("initializer element%s is not constant",
4910 " for `%s'", NULL);
4911 inside_init = error_mark_node;
4913 else if (require_constant
4914 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4916 error_init ("initializer element%s is not computable at load time",
4917 " for `%s'", NULL);
4918 inside_init = error_mark_node;
4921 return inside_init;
4924 /* Come here only for records and arrays. */
4926 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4928 error_init ("variable-sized object%s may not be initialized",
4929 " `%s'", NULL);
4930 return error_mark_node;
4933 /* Traditionally, you can write struct foo x = 0;
4934 and it initializes the first element of x to 0. */
4935 if (flag_traditional)
4937 tree top = 0, prev = 0, otype = type;
4938 while (TREE_CODE (type) == RECORD_TYPE
4939 || TREE_CODE (type) == ARRAY_TYPE
4940 || TREE_CODE (type) == QUAL_UNION_TYPE
4941 || TREE_CODE (type) == UNION_TYPE)
4943 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4944 if (prev == 0)
4945 top = temp;
4946 else
4947 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4948 prev = temp;
4949 if (TREE_CODE (type) == ARRAY_TYPE)
4950 type = TREE_TYPE (type);
4951 else if (TYPE_FIELDS (type))
4952 type = TREE_TYPE (TYPE_FIELDS (type));
4953 else
4955 error_init ("invalid initializer%s", " for `%s'", NULL);
4956 return error_mark_node;
4960 if (otype != type)
4962 TREE_OPERAND (prev, 1)
4963 = build_tree_list (NULL_TREE,
4964 digest_init (type, init, require_constant,
4965 constructor_constant));
4966 return top;
4968 else
4969 return error_mark_node;
4971 error_init ("invalid initializer%s", " for `%s'", NULL);
4972 return error_mark_node;
4975 /* Handle initializers that use braces. */
4977 /* Type of object we are accumulating a constructor for.
4978 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4979 static tree constructor_type;
4981 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4982 left to fill. */
4983 static tree constructor_fields;
4985 /* For an ARRAY_TYPE, this is the specified index
4986 at which to store the next element we get.
4987 This is a special INTEGER_CST node that we modify in place. */
4988 static tree constructor_index;
4990 /* For an ARRAY_TYPE, this is the end index of the range
4991 to initialize with the next element, or NULL in the ordinary case
4992 where the element is used just once. */
4993 static tree constructor_range_end;
4995 /* For an ARRAY_TYPE, this is the maximum index. */
4996 static tree constructor_max_index;
4998 /* For a RECORD_TYPE, this is the first field not yet written out. */
4999 static tree constructor_unfilled_fields;
5001 /* For an ARRAY_TYPE, this is the index of the first element
5002 not yet written out.
5003 This is a special INTEGER_CST node that we modify in place. */
5004 static tree constructor_unfilled_index;
5006 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5007 This is so we can generate gaps between fields, when appropriate.
5008 This is a special INTEGER_CST node that we modify in place. */
5009 static tree constructor_bit_index;
5011 /* If we are saving up the elements rather than allocating them,
5012 this is the list of elements so far (in reverse order,
5013 most recent first). */
5014 static tree constructor_elements;
5016 /* 1 if so far this constructor's elements are all compile-time constants. */
5017 static int constructor_constant;
5019 /* 1 if so far this constructor's elements are all valid address constants. */
5020 static int constructor_simple;
5022 /* 1 if this constructor is erroneous so far. */
5023 static int constructor_erroneous;
5025 /* 1 if have called defer_addressed_constants. */
5026 static int constructor_subconstants_deferred;
5028 /* List of pending elements at this constructor level.
5029 These are elements encountered out of order
5030 which belong at places we haven't reached yet in actually
5031 writing the output. */
5032 static tree constructor_pending_elts;
5034 /* The SPELLING_DEPTH of this constructor. */
5035 static int constructor_depth;
5037 /* 0 if implicitly pushing constructor levels is allowed. */
5038 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
5040 /* 1 if this constructor level was entered implicitly. */
5041 static int constructor_implicit;
5043 static int require_constant_value;
5044 static int require_constant_elements;
5046 /* 1 if it is ok to output this constructor as we read it.
5047 0 means must accumulate a CONSTRUCTOR expression. */
5048 static int constructor_incremental;
5050 /* DECL node for which an initializer is being read.
5051 0 means we are reading a constructor expression
5052 such as (struct foo) {...}. */
5053 static tree constructor_decl;
5055 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
5056 static char *constructor_asmspec;
5058 /* Nonzero if this is an initializer for a top-level decl. */
5059 static int constructor_top_level;
5061 /* When we finish reading a constructor expression
5062 (constructor_decl is 0), the CONSTRUCTOR goes here. */
5063 static tree constructor_result;
5065 /* This stack has a level for each implicit or explicit level of
5066 structuring in the initializer, including the outermost one. It
5067 saves the values of most of the variables above. */
5069 struct constructor_stack
5071 struct constructor_stack *next;
5072 tree type;
5073 tree fields;
5074 tree index;
5075 tree range_end;
5076 tree max_index;
5077 tree unfilled_index;
5078 tree unfilled_fields;
5079 tree bit_index;
5080 tree elements;
5081 int offset;
5082 tree pending_elts;
5083 int depth;
5084 /* If nonzero, this value should replace the entire
5085 constructor at this level. */
5086 tree replacement_value;
5087 char constant;
5088 char simple;
5089 char implicit;
5090 char incremental;
5091 char erroneous;
5092 char outer;
5095 struct constructor_stack *constructor_stack;
5097 /* This stack records separate initializers that are nested.
5098 Nested initializers can't happen in ANSI C, but GNU C allows them
5099 in cases like { ... (struct foo) { ... } ... }. */
5101 struct initializer_stack
5103 struct initializer_stack *next;
5104 tree decl;
5105 char *asmspec;
5106 struct constructor_stack *constructor_stack;
5107 tree elements;
5108 struct spelling *spelling;
5109 struct spelling *spelling_base;
5110 int spelling_size;
5111 char top_level;
5112 char incremental;
5113 char require_constant_value;
5114 char require_constant_elements;
5115 char deferred;
5118 struct initializer_stack *initializer_stack;
5120 /* Prepare to parse and output the initializer for variable DECL. */
5122 void
5123 start_init (decl, asmspec_tree, top_level)
5124 tree decl;
5125 tree asmspec_tree;
5126 int top_level;
5128 char *locus;
5129 struct initializer_stack *p
5130 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5131 char *asmspec = 0;
5133 if (asmspec_tree)
5134 asmspec = TREE_STRING_POINTER (asmspec_tree);
5136 p->decl = constructor_decl;
5137 p->asmspec = constructor_asmspec;
5138 p->incremental = constructor_incremental;
5139 p->require_constant_value = require_constant_value;
5140 p->require_constant_elements = require_constant_elements;
5141 p->constructor_stack = constructor_stack;
5142 p->elements = constructor_elements;
5143 p->spelling = spelling;
5144 p->spelling_base = spelling_base;
5145 p->spelling_size = spelling_size;
5146 p->deferred = constructor_subconstants_deferred;
5147 p->top_level = constructor_top_level;
5148 p->next = initializer_stack;
5149 initializer_stack = p;
5151 constructor_decl = decl;
5152 constructor_incremental = top_level;
5153 constructor_asmspec = asmspec;
5154 constructor_subconstants_deferred = 0;
5155 constructor_top_level = top_level;
5157 if (decl != 0)
5159 require_constant_value = TREE_STATIC (decl);
5160 require_constant_elements
5161 = ((TREE_STATIC (decl) || pedantic)
5162 /* For a scalar, you can always use any value to initialize,
5163 even within braces. */
5164 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5165 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5166 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5167 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5168 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5169 constructor_incremental |= TREE_STATIC (decl);
5171 else
5173 require_constant_value = 0;
5174 require_constant_elements = 0;
5175 locus = "(anonymous)";
5178 constructor_stack = 0;
5180 missing_braces_mentioned = 0;
5182 spelling_base = 0;
5183 spelling_size = 0;
5184 RESTORE_SPELLING_DEPTH (0);
5186 if (locus)
5187 push_string (locus);
5190 void
5191 finish_init ()
5193 struct initializer_stack *p = initializer_stack;
5195 /* Output subconstants (string constants, usually)
5196 that were referenced within this initializer and saved up.
5197 Must do this if and only if we called defer_addressed_constants. */
5198 if (constructor_subconstants_deferred)
5199 output_deferred_addressed_constants ();
5201 /* Free the whole constructor stack of this initializer. */
5202 while (constructor_stack)
5204 struct constructor_stack *q = constructor_stack;
5205 constructor_stack = q->next;
5206 free (q);
5209 /* Pop back to the data of the outer initializer (if any). */
5210 constructor_decl = p->decl;
5211 constructor_asmspec = p->asmspec;
5212 constructor_incremental = p->incremental;
5213 require_constant_value = p->require_constant_value;
5214 require_constant_elements = p->require_constant_elements;
5215 constructor_stack = p->constructor_stack;
5216 constructor_elements = p->elements;
5217 spelling = p->spelling;
5218 spelling_base = p->spelling_base;
5219 spelling_size = p->spelling_size;
5220 constructor_subconstants_deferred = p->deferred;
5221 constructor_top_level = p->top_level;
5222 initializer_stack = p->next;
5223 free (p);
5226 /* Call here when we see the initializer is surrounded by braces.
5227 This is instead of a call to push_init_level;
5228 it is matched by a call to pop_init_level.
5230 TYPE is the type to initialize, for a constructor expression.
5231 For an initializer for a decl, TYPE is zero. */
5233 void
5234 really_start_incremental_init (type)
5235 tree type;
5237 struct constructor_stack *p
5238 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5240 if (type == 0)
5241 type = TREE_TYPE (constructor_decl);
5243 /* Turn off constructor_incremental if type is a struct with bitfields.
5244 Do this before the first push, so that the corrected value
5245 is available in finish_init. */
5246 check_init_type_bitfields (type);
5248 p->type = constructor_type;
5249 p->fields = constructor_fields;
5250 p->index = constructor_index;
5251 p->range_end = constructor_range_end;
5252 p->max_index = constructor_max_index;
5253 p->unfilled_index = constructor_unfilled_index;
5254 p->unfilled_fields = constructor_unfilled_fields;
5255 p->bit_index = constructor_bit_index;
5256 p->elements = constructor_elements;
5257 p->constant = constructor_constant;
5258 p->simple = constructor_simple;
5259 p->erroneous = constructor_erroneous;
5260 p->pending_elts = constructor_pending_elts;
5261 p->depth = constructor_depth;
5262 p->replacement_value = 0;
5263 p->implicit = 0;
5264 p->incremental = constructor_incremental;
5265 p->outer = 0;
5266 p->next = 0;
5267 constructor_stack = p;
5269 constructor_constant = 1;
5270 constructor_simple = 1;
5271 constructor_depth = SPELLING_DEPTH ();
5272 constructor_elements = 0;
5273 constructor_pending_elts = 0;
5274 constructor_type = type;
5276 if (TREE_CODE (constructor_type) == RECORD_TYPE
5277 || TREE_CODE (constructor_type) == UNION_TYPE)
5279 constructor_fields = TYPE_FIELDS (constructor_type);
5280 /* Skip any nameless bit fields at the beginning. */
5281 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5282 && DECL_NAME (constructor_fields) == 0)
5283 constructor_fields = TREE_CHAIN (constructor_fields);
5284 constructor_unfilled_fields = constructor_fields;
5285 constructor_bit_index = copy_node (integer_zero_node);
5287 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5289 constructor_range_end = 0;
5290 if (TYPE_DOMAIN (constructor_type))
5292 constructor_max_index
5293 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5294 constructor_index
5295 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5297 else
5298 constructor_index = copy_node (integer_zero_node);
5299 constructor_unfilled_index = copy_node (constructor_index);
5301 else
5303 /* Handle the case of int x = {5}; */
5304 constructor_fields = constructor_type;
5305 constructor_unfilled_fields = constructor_type;
5308 if (constructor_incremental)
5310 int momentary = suspend_momentary ();
5311 push_obstacks_nochange ();
5312 if (TREE_PERMANENT (constructor_decl))
5313 end_temporary_allocation ();
5314 make_decl_rtl (constructor_decl, constructor_asmspec,
5315 constructor_top_level);
5316 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5317 pop_obstacks ();
5318 resume_momentary (momentary);
5321 if (constructor_incremental)
5323 defer_addressed_constants ();
5324 constructor_subconstants_deferred = 1;
5328 /* Push down into a subobject, for initialization.
5329 If this is for an explicit set of braces, IMPLICIT is 0.
5330 If it is because the next element belongs at a lower level,
5331 IMPLICIT is 1. */
5333 void
5334 push_init_level (implicit)
5335 int implicit;
5337 struct constructor_stack *p;
5339 /* If we've exhausted any levels that didn't have braces,
5340 pop them now. */
5341 while (constructor_stack->implicit)
5343 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5344 || TREE_CODE (constructor_type) == UNION_TYPE)
5345 && constructor_fields == 0)
5346 process_init_element (pop_init_level (1));
5347 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5348 && tree_int_cst_lt (constructor_max_index, constructor_index))
5349 process_init_element (pop_init_level (1));
5350 else
5351 break;
5354 /* Structure elements may require alignment. Do this now if necessary
5355 for the subaggregate, and if it comes next in sequence. Don't do
5356 this for subaggregates that will go on the pending list. */
5357 if (constructor_incremental && constructor_type != 0
5358 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields
5359 && constructor_fields == constructor_unfilled_fields)
5361 /* Advance to offset of this element. */
5362 if (! tree_int_cst_equal (constructor_bit_index,
5363 DECL_FIELD_BITPOS (constructor_fields)))
5365 int next = (TREE_INT_CST_LOW
5366 (DECL_FIELD_BITPOS (constructor_fields))
5367 / BITS_PER_UNIT);
5368 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5369 / BITS_PER_UNIT);
5371 assemble_zeros (next - here);
5373 /* Indicate that we have now filled the structure up to the current
5374 field. */
5375 constructor_unfilled_fields = constructor_fields;
5378 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5379 p->type = constructor_type;
5380 p->fields = constructor_fields;
5381 p->index = constructor_index;
5382 p->range_end = constructor_range_end;
5383 p->max_index = constructor_max_index;
5384 p->unfilled_index = constructor_unfilled_index;
5385 p->unfilled_fields = constructor_unfilled_fields;
5386 p->bit_index = constructor_bit_index;
5387 p->elements = constructor_elements;
5388 p->constant = constructor_constant;
5389 p->simple = constructor_simple;
5390 p->erroneous = constructor_erroneous;
5391 p->pending_elts = constructor_pending_elts;
5392 p->depth = constructor_depth;
5393 p->replacement_value = 0;
5394 p->implicit = implicit;
5395 p->incremental = constructor_incremental;
5396 p->outer = 0;
5397 p->next = constructor_stack;
5398 constructor_stack = p;
5400 constructor_constant = 1;
5401 constructor_simple = 1;
5402 constructor_depth = SPELLING_DEPTH ();
5403 constructor_elements = 0;
5404 constructor_pending_elts = 0;
5406 /* Don't die if an entire brace-pair level is superfluous
5407 in the containing level. */
5408 if (constructor_type == 0)
5410 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5411 || TREE_CODE (constructor_type) == UNION_TYPE)
5413 /* Don't die if there are extra init elts at the end. */
5414 if (constructor_fields == 0)
5415 constructor_type = 0;
5416 else
5418 constructor_type = TREE_TYPE (constructor_fields);
5419 push_member_name (constructor_fields);
5420 constructor_depth++;
5421 if (constructor_fields != constructor_unfilled_fields)
5422 constructor_incremental = 0;
5425 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5427 constructor_type = TREE_TYPE (constructor_type);
5428 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5429 constructor_depth++;
5430 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5431 || constructor_range_end != 0)
5432 constructor_incremental = 0;
5435 if (constructor_type == 0)
5437 error_init ("extra brace group at end of initializer%s",
5438 " for `%s'", NULL);
5439 constructor_fields = 0;
5440 constructor_unfilled_fields = 0;
5441 return;
5444 /* Turn off constructor_incremental if type is a struct with bitfields. */
5445 check_init_type_bitfields (constructor_type);
5447 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5449 missing_braces_mentioned = 1;
5450 warning_init ("missing braces around initializer%s", " for `%s'", NULL);
5453 if (TREE_CODE (constructor_type) == RECORD_TYPE
5454 || TREE_CODE (constructor_type) == UNION_TYPE)
5456 constructor_fields = TYPE_FIELDS (constructor_type);
5457 /* Skip any nameless bit fields at the beginning. */
5458 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5459 && DECL_NAME (constructor_fields) == 0)
5460 constructor_fields = TREE_CHAIN (constructor_fields);
5461 constructor_unfilled_fields = constructor_fields;
5462 constructor_bit_index = copy_node (integer_zero_node);
5464 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5466 constructor_range_end = 0;
5467 if (TYPE_DOMAIN (constructor_type))
5469 constructor_max_index
5470 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5471 constructor_index
5472 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5474 else
5475 constructor_index = copy_node (integer_zero_node);
5476 constructor_unfilled_index = copy_node (constructor_index);
5478 else
5480 warning_init ("braces around scalar initializer%s", " for `%s'", NULL);
5481 constructor_fields = constructor_type;
5482 constructor_unfilled_fields = constructor_type;
5486 /* Don't read a struct incrementally if it has any bitfields,
5487 because the incremental reading code doesn't know how to
5488 handle bitfields yet. */
5490 static void
5491 check_init_type_bitfields (type)
5492 tree type;
5494 if (TREE_CODE (type) == RECORD_TYPE)
5496 tree tail;
5497 for (tail = TYPE_FIELDS (type); tail;
5498 tail = TREE_CHAIN (tail))
5500 if (DECL_C_BIT_FIELD (tail)
5501 /* This catches cases like `int foo : 8;'. */
5502 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail)))
5504 constructor_incremental = 0;
5505 break;
5508 check_init_type_bitfields (TREE_TYPE (tail));
5512 else if (TREE_CODE (type) == ARRAY_TYPE)
5513 check_init_type_bitfields (TREE_TYPE (type));
5516 /* At the end of an implicit or explicit brace level,
5517 finish up that level of constructor.
5518 If we were outputting the elements as they are read, return 0
5519 from inner levels (process_init_element ignores that),
5520 but return error_mark_node from the outermost level
5521 (that's what we want to put in DECL_INITIAL).
5522 Otherwise, return a CONSTRUCTOR expression. */
5524 tree
5525 pop_init_level (implicit)
5526 int implicit;
5528 struct constructor_stack *p;
5529 int size = 0;
5530 tree constructor = 0;
5532 if (implicit == 0)
5534 /* When we come to an explicit close brace,
5535 pop any inner levels that didn't have explicit braces. */
5536 while (constructor_stack->implicit)
5537 process_init_element (pop_init_level (1));
5540 p = constructor_stack;
5542 if (constructor_type != 0)
5543 size = int_size_in_bytes (constructor_type);
5545 /* Now output all pending elements. */
5546 output_pending_init_elements (1);
5548 #if 0 /* c-parse.in warns about {}. */
5549 /* In ANSI, each brace level must have at least one element. */
5550 if (! implicit && pedantic
5551 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5552 ? integer_zerop (constructor_unfilled_index)
5553 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5554 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL);
5555 #endif
5557 /* Pad out the end of the structure. */
5559 if (p->replacement_value)
5561 /* If this closes a superfluous brace pair,
5562 just pass out the element between them. */
5563 constructor = p->replacement_value;
5564 /* If this is the top level thing within the initializer,
5565 and it's for a variable, then since we already called
5566 assemble_variable, we must output the value now. */
5567 if (p->next == 0 && constructor_decl != 0
5568 && constructor_incremental)
5570 constructor = digest_init (constructor_type, constructor,
5571 require_constant_value,
5572 require_constant_elements);
5574 /* If initializing an array of unknown size,
5575 determine the size now. */
5576 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5577 && TYPE_DOMAIN (constructor_type) == 0)
5579 int failure;
5580 int momentary_p;
5582 push_obstacks_nochange ();
5583 if (TREE_PERMANENT (constructor_type))
5584 end_temporary_allocation ();
5586 momentary_p = suspend_momentary ();
5588 /* We shouldn't have an incomplete array type within
5589 some other type. */
5590 if (constructor_stack->next)
5591 abort ();
5593 failure
5594 = complete_array_type (constructor_type,
5595 constructor, 0);
5596 if (failure)
5597 abort ();
5599 size = int_size_in_bytes (constructor_type);
5600 resume_momentary (momentary_p);
5601 pop_obstacks ();
5604 output_constant (constructor, size);
5607 else if (constructor_type == 0)
5609 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5610 && TREE_CODE (constructor_type) != UNION_TYPE
5611 && TREE_CODE (constructor_type) != ARRAY_TYPE
5612 && ! constructor_incremental)
5614 /* A nonincremental scalar initializer--just return
5615 the element, after verifying there is just one. */
5616 if (constructor_elements == 0)
5618 error_init ("empty scalar initializer%s",
5619 " for `%s'", NULL);
5620 constructor = error_mark_node;
5622 else if (TREE_CHAIN (constructor_elements) != 0)
5624 error_init ("extra elements in scalar initializer%s",
5625 " for `%s'", NULL);
5626 constructor = TREE_VALUE (constructor_elements);
5628 else
5629 constructor = TREE_VALUE (constructor_elements);
5631 else if (! constructor_incremental)
5633 if (constructor_erroneous)
5634 constructor = error_mark_node;
5635 else
5637 int momentary = suspend_momentary ();
5639 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5640 nreverse (constructor_elements));
5641 if (constructor_constant)
5642 TREE_CONSTANT (constructor) = 1;
5643 if (constructor_constant && constructor_simple)
5644 TREE_STATIC (constructor) = 1;
5646 resume_momentary (momentary);
5649 else
5651 tree filled;
5652 int momentary = suspend_momentary ();
5654 if (TREE_CODE (constructor_type) == RECORD_TYPE
5655 || TREE_CODE (constructor_type) == UNION_TYPE)
5657 /* Find the offset of the end of that field. */
5658 filled = size_binop (CEIL_DIV_EXPR,
5659 constructor_bit_index,
5660 size_int (BITS_PER_UNIT));
5662 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5664 /* If initializing an array of unknown size,
5665 determine the size now. */
5666 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5667 && TYPE_DOMAIN (constructor_type) == 0)
5669 tree maxindex
5670 = size_binop (MINUS_EXPR,
5671 constructor_unfilled_index,
5672 integer_one_node);
5674 push_obstacks_nochange ();
5675 if (TREE_PERMANENT (constructor_type))
5676 end_temporary_allocation ();
5677 maxindex = copy_node (maxindex);
5678 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5679 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5681 /* TYPE_MAX_VALUE is always one less than the number of elements
5682 in the array, because we start counting at zero. Therefore,
5683 warn only if the value is less than zero. */
5684 if (pedantic
5685 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5686 < 0))
5687 error_with_decl (constructor_decl,
5688 "zero or negative array size `%s'");
5689 layout_type (constructor_type);
5690 size = int_size_in_bytes (constructor_type);
5691 pop_obstacks ();
5694 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5695 size_in_bytes (TREE_TYPE (constructor_type)));
5697 else
5698 filled = 0;
5700 if (filled != 0)
5701 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5703 resume_momentary (momentary);
5707 constructor_type = p->type;
5708 constructor_fields = p->fields;
5709 constructor_index = p->index;
5710 constructor_range_end = p->range_end;
5711 constructor_max_index = p->max_index;
5712 constructor_unfilled_index = p->unfilled_index;
5713 constructor_unfilled_fields = p->unfilled_fields;
5714 constructor_bit_index = p->bit_index;
5715 constructor_elements = p->elements;
5716 constructor_constant = p->constant;
5717 constructor_simple = p->simple;
5718 constructor_erroneous = p->erroneous;
5719 constructor_pending_elts = p->pending_elts;
5720 constructor_depth = p->depth;
5721 constructor_incremental = p->incremental;
5722 RESTORE_SPELLING_DEPTH (constructor_depth);
5724 constructor_stack = p->next;
5725 free (p);
5727 if (constructor == 0)
5729 if (constructor_stack == 0)
5730 return error_mark_node;
5731 return NULL_TREE;
5733 return constructor;
5736 /* Within an array initializer, specify the next index to be initialized.
5737 FIRST is that index. If LAST is nonzero, then initialize a range
5738 of indices, running from FIRST through LAST. */
5740 void
5741 set_init_index (first, last)
5742 tree first, last;
5744 while ((TREE_CODE (first) == NOP_EXPR
5745 || TREE_CODE (first) == CONVERT_EXPR
5746 || TREE_CODE (first) == NON_LVALUE_EXPR)
5747 && (TYPE_MODE (TREE_TYPE (first))
5748 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5749 (first) = TREE_OPERAND (first, 0);
5750 if (last)
5751 while ((TREE_CODE (last) == NOP_EXPR
5752 || TREE_CODE (last) == CONVERT_EXPR
5753 || TREE_CODE (last) == NON_LVALUE_EXPR)
5754 && (TYPE_MODE (TREE_TYPE (last))
5755 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5756 (last) = TREE_OPERAND (last, 0);
5758 if (TREE_CODE (first) != INTEGER_CST)
5759 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5760 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5761 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5762 else if (! constructor_unfilled_index)
5763 error_init ("array index in non-array initializer%s", " for `%s'", NULL);
5764 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5765 error_init ("duplicate array index in initializer%s", " for `%s'", NULL);
5766 else
5768 TREE_INT_CST_LOW (constructor_index)
5769 = TREE_INT_CST_LOW (first);
5770 TREE_INT_CST_HIGH (constructor_index)
5771 = TREE_INT_CST_HIGH (first);
5773 if (last != 0 && tree_int_cst_lt (last, first))
5774 error_init ("empty index range in initializer%s", " for `%s'", NULL);
5775 else
5777 if (pedantic)
5778 pedwarn ("ANSI C forbids specifying element to initialize");
5779 constructor_range_end = last;
5784 /* Within a struct initializer, specify the next field to be initialized. */
5786 void
5787 set_init_label (fieldname)
5788 tree fieldname;
5790 tree tail;
5791 int passed = 0;
5793 /* Don't die if an entire brace-pair level is superfluous
5794 in the containing level. */
5795 if (constructor_type == 0)
5796 return;
5798 for (tail = TYPE_FIELDS (constructor_type); tail;
5799 tail = TREE_CHAIN (tail))
5801 if (tail == constructor_unfilled_fields)
5802 passed = 1;
5803 if (DECL_NAME (tail) == fieldname)
5804 break;
5807 if (tail == 0)
5808 error ("unknown field `%s' specified in initializer",
5809 IDENTIFIER_POINTER (fieldname));
5810 else if (!passed)
5811 error ("field `%s' already initialized",
5812 IDENTIFIER_POINTER (fieldname));
5813 else
5815 constructor_fields = tail;
5816 if (pedantic)
5817 pedwarn ("ANSI C forbids specifying structure member to initialize");
5821 /* "Output" the next constructor element.
5822 At top level, really output it to assembler code now.
5823 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5824 TYPE is the data type that the containing data type wants here.
5825 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5827 PENDING if non-nil means output pending elements that belong
5828 right after this element. (PENDING is normally 1;
5829 it is 0 while outputting pending elements, to avoid recursion.) */
5831 static void
5832 output_init_element (value, type, field, pending)
5833 tree value, type, field;
5834 int pending;
5836 int duplicate = 0;
5838 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5839 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5840 && !(TREE_CODE (value) == STRING_CST
5841 && TREE_CODE (type) == ARRAY_TYPE
5842 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5843 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5844 TYPE_MAIN_VARIANT (type))))
5845 value = default_conversion (value);
5847 if (value == error_mark_node)
5848 constructor_erroneous = 1;
5849 else if (!TREE_CONSTANT (value))
5850 constructor_constant = 0;
5851 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5852 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5853 || TREE_CODE (constructor_type) == UNION_TYPE)
5854 && DECL_C_BIT_FIELD (field)
5855 && TREE_CODE (value) != INTEGER_CST))
5856 constructor_simple = 0;
5858 if (require_constant_value && ! TREE_CONSTANT (value))
5860 error_init ("initializer element%s is not constant",
5861 " for `%s'", NULL);
5862 value = error_mark_node;
5864 else if (require_constant_elements
5865 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5867 error_init ("initializer element%s is not computable at load time",
5868 " for `%s'", NULL);
5869 value = error_mark_node;
5872 /* If this element duplicates one on constructor_pending_elts,
5873 print a message and ignore it. Don't do this when we're
5874 processing elements taken off constructor_pending_elts,
5875 because we'd always get spurious errors. */
5876 if (pending)
5878 if (TREE_CODE (constructor_type) == RECORD_TYPE
5879 || TREE_CODE (constructor_type) == UNION_TYPE)
5881 if (purpose_member (field, constructor_pending_elts))
5883 error_init ("duplicate initializer%s", " for `%s'", NULL);
5884 duplicate = 1;
5887 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5889 tree tail;
5890 for (tail = constructor_pending_elts; tail;
5891 tail = TREE_CHAIN (tail))
5892 if (TREE_PURPOSE (tail) != 0
5893 && TREE_CODE (TREE_PURPOSE (tail)) == INTEGER_CST
5894 && tree_int_cst_equal (TREE_PURPOSE (tail), constructor_index))
5895 break;
5897 if (tail != 0)
5899 error_init ("duplicate initializer%s", " for `%s'", NULL);
5900 duplicate = 1;
5905 /* If this element doesn't come next in sequence,
5906 put it on constructor_pending_elts. */
5907 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5908 && !tree_int_cst_equal (field, constructor_unfilled_index))
5910 if (! duplicate)
5911 /* The copy_node is needed in case field is actually
5912 constructor_index, which is modified in place. */
5913 constructor_pending_elts
5914 = tree_cons (copy_node (field),
5915 digest_init (type, value, require_constant_value,
5916 require_constant_elements),
5917 constructor_pending_elts);
5919 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5920 && field != constructor_unfilled_fields)
5922 /* We do this for records but not for unions. In a union,
5923 no matter which field is specified, it can be initialized
5924 right away since it starts at the beginning of the union. */
5925 if (!duplicate)
5926 constructor_pending_elts
5927 = tree_cons (field,
5928 digest_init (type, value, require_constant_value,
5929 require_constant_elements),
5930 constructor_pending_elts);
5932 else
5934 /* Otherwise, output this element either to
5935 constructor_elements or to the assembler file. */
5937 if (!duplicate)
5939 if (! constructor_incremental)
5941 if (field && TREE_CODE (field) == INTEGER_CST)
5942 field = copy_node (field);
5943 constructor_elements
5944 = tree_cons (field, digest_init (type, value,
5945 require_constant_value,
5946 require_constant_elements),
5947 constructor_elements);
5949 else
5951 /* Structure elements may require alignment.
5952 Do this, if necessary. */
5953 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5955 /* Advance to offset of this element. */
5956 if (! tree_int_cst_equal (constructor_bit_index,
5957 DECL_FIELD_BITPOS (field)))
5959 int next = (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
5960 / BITS_PER_UNIT);
5961 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5962 / BITS_PER_UNIT);
5964 assemble_zeros (next - here);
5967 output_constant (digest_init (type, value,
5968 require_constant_value,
5969 require_constant_elements),
5970 int_size_in_bytes (type));
5972 /* For a record or union,
5973 keep track of end position of last field. */
5974 if (TREE_CODE (constructor_type) == RECORD_TYPE
5975 || TREE_CODE (constructor_type) == UNION_TYPE)
5977 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
5978 DECL_SIZE (field));
5979 TREE_INT_CST_LOW (constructor_bit_index)
5980 = TREE_INT_CST_LOW (temp);
5981 TREE_INT_CST_HIGH (constructor_bit_index)
5982 = TREE_INT_CST_HIGH (temp);
5987 /* Advance the variable that indicates sequential elements output. */
5988 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5990 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
5991 integer_one_node);
5992 TREE_INT_CST_LOW (constructor_unfilled_index)
5993 = TREE_INT_CST_LOW (tem);
5994 TREE_INT_CST_HIGH (constructor_unfilled_index)
5995 = TREE_INT_CST_HIGH (tem);
5997 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5998 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5999 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6000 constructor_unfilled_fields = 0;
6002 /* Now output any pending elements which have become next. */
6003 if (pending)
6004 output_pending_init_elements (0);
6008 /* Output any pending elements which have become next.
6009 As we output elements, constructor_unfilled_{fields,index}
6010 advances, which may cause other elements to become next;
6011 if so, they too are output.
6013 If ALL is 0, we return when there are
6014 no more pending elements to output now.
6016 If ALL is 1, we output space as necessary so that
6017 we can output all the pending elements. */
6019 static void
6020 output_pending_init_elements (all)
6021 int all;
6023 tree tail;
6024 tree next;
6026 retry:
6028 /* Look thru the whole pending list.
6029 If we find an element that should be output now,
6030 output it. Otherwise, set NEXT to the element
6031 that comes first among those still pending. */
6033 next = 0;
6034 for (tail = constructor_pending_elts; tail;
6035 tail = TREE_CHAIN (tail))
6037 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6039 if (tree_int_cst_equal (TREE_PURPOSE (tail),
6040 constructor_unfilled_index))
6042 output_init_element (TREE_VALUE (tail),
6043 TREE_TYPE (constructor_type),
6044 constructor_unfilled_index, 0);
6045 goto retry;
6047 else if (tree_int_cst_lt (TREE_PURPOSE (tail),
6048 constructor_unfilled_index))
6050 else if (next == 0
6051 || tree_int_cst_lt (TREE_PURPOSE (tail), next))
6052 next = TREE_PURPOSE (tail);
6054 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6055 || TREE_CODE (constructor_type) == UNION_TYPE)
6057 if (TREE_PURPOSE (tail) == constructor_unfilled_fields)
6059 output_init_element (TREE_VALUE (tail),
6060 TREE_TYPE (constructor_unfilled_fields),
6061 constructor_unfilled_fields,
6063 goto retry;
6065 else if (constructor_unfilled_fields == 0
6066 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
6067 DECL_FIELD_BITPOS (constructor_unfilled_fields)))
6069 else if (next == 0
6070 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
6071 DECL_FIELD_BITPOS (next)))
6072 next = TREE_PURPOSE (tail);
6076 /* Ordinarily return, but not if we want to output all
6077 and there are elements left. */
6078 if (! (all && next != 0))
6079 return;
6081 /* Generate space up to the position of NEXT. */
6082 if (constructor_incremental)
6084 tree filled;
6085 tree nextpos_tree = size_int (0);
6087 if (TREE_CODE (constructor_type) == RECORD_TYPE
6088 || TREE_CODE (constructor_type) == UNION_TYPE)
6090 /* Find the last field written out, if any. */
6091 for (tail = TYPE_FIELDS (constructor_type); tail;
6092 tail = TREE_CHAIN (tail))
6093 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
6094 break;
6096 if (tail)
6097 /* Find the offset of the end of that field. */
6098 filled = size_binop (CEIL_DIV_EXPR,
6099 size_binop (PLUS_EXPR,
6100 DECL_FIELD_BITPOS (tail),
6101 DECL_SIZE (tail)),
6102 size_int (BITS_PER_UNIT));
6103 else
6104 filled = size_int (0);
6106 nextpos_tree = size_binop (CEIL_DIV_EXPR,
6107 DECL_FIELD_BITPOS (next),
6108 size_int (BITS_PER_UNIT));
6110 TREE_INT_CST_HIGH (constructor_bit_index)
6111 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
6112 TREE_INT_CST_LOW (constructor_bit_index)
6113 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
6114 constructor_unfilled_fields = next;
6116 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6118 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
6119 size_in_bytes (TREE_TYPE (constructor_type)));
6120 nextpos_tree
6121 = size_binop (MULT_EXPR, next,
6122 size_in_bytes (TREE_TYPE (constructor_type)));
6123 TREE_INT_CST_LOW (constructor_unfilled_index)
6124 = TREE_INT_CST_LOW (next);
6125 TREE_INT_CST_HIGH (constructor_unfilled_index)
6126 = TREE_INT_CST_HIGH (next);
6128 else
6129 filled = 0;
6131 if (filled)
6133 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
6135 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
6138 else
6140 /* If it's not incremental, just skip over the gap,
6141 so that after jumping to retry we will output the next
6142 successive element. */
6143 if (TREE_CODE (constructor_type) == RECORD_TYPE
6144 || TREE_CODE (constructor_type) == UNION_TYPE)
6145 constructor_unfilled_fields = next;
6146 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6148 TREE_INT_CST_LOW (constructor_unfilled_index)
6149 = TREE_INT_CST_LOW (next);
6150 TREE_INT_CST_HIGH (constructor_unfilled_index)
6151 = TREE_INT_CST_HIGH (next);
6155 goto retry;
6158 /* Add one non-braced element to the current constructor level.
6159 This adjusts the current position within the constructor's type.
6160 This may also start or terminate implicit levels
6161 to handle a partly-braced initializer.
6163 Once this has found the correct level for the new element,
6164 it calls output_init_element.
6166 Note: if we are incrementally outputting this constructor,
6167 this function may be called with a null argument
6168 representing a sub-constructor that was already incrementally output.
6169 When that happens, we output nothing, but we do the bookkeeping
6170 to skip past that element of the current constructor. */
6172 void
6173 process_init_element (value)
6174 tree value;
6176 tree orig_value = value;
6177 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6179 /* Handle superfluous braces around string cst as in
6180 char x[] = {"foo"}; */
6181 if (string_flag
6182 && constructor_type
6183 && TREE_CODE (constructor_type) == ARRAY_TYPE
6184 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6185 && integer_zerop (constructor_unfilled_index))
6187 constructor_stack->replacement_value = value;
6188 return;
6191 if (constructor_stack->replacement_value != 0)
6193 error_init ("excess elements in struct initializer%s",
6194 " after `%s'", NULL_PTR);
6195 return;
6198 /* Ignore elements of a brace group if it is entirely superfluous
6199 and has already been diagnosed. */
6200 if (constructor_type == 0)
6201 return;
6203 /* If we've exhausted any levels that didn't have braces,
6204 pop them now. */
6205 while (constructor_stack->implicit)
6207 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6208 || TREE_CODE (constructor_type) == UNION_TYPE)
6209 && constructor_fields == 0)
6210 process_init_element (pop_init_level (1));
6211 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6212 && tree_int_cst_lt (constructor_max_index, constructor_index))
6213 process_init_element (pop_init_level (1));
6214 else
6215 break;
6218 while (1)
6220 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6222 tree fieldtype;
6223 enum tree_code fieldcode;
6225 if (constructor_fields == 0)
6227 pedwarn_init ("excess elements in struct initializer%s",
6228 " after `%s'", NULL_PTR);
6229 break;
6232 fieldtype = TREE_TYPE (constructor_fields);
6233 if (fieldtype != error_mark_node)
6234 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6235 fieldcode = TREE_CODE (fieldtype);
6237 /* Accept a string constant to initialize a subarray. */
6238 if (value != 0
6239 && fieldcode == ARRAY_TYPE
6240 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6241 && string_flag)
6242 value = orig_value;
6243 /* Otherwise, if we have come to a subaggregate,
6244 and we don't have an element of its type, push into it. */
6245 else if (value != 0 && !constructor_no_implicit
6246 && value != error_mark_node
6247 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6248 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6249 || fieldcode == UNION_TYPE))
6251 push_init_level (1);
6252 continue;
6255 if (value)
6257 push_member_name (constructor_fields);
6258 output_init_element (value, fieldtype, constructor_fields, 1);
6259 RESTORE_SPELLING_DEPTH (constructor_depth);
6261 else
6262 /* Do the bookkeeping for an element that was
6263 directly output as a constructor. */
6265 /* For a record, keep track of end position of last field. */
6266 tree temp = size_binop (PLUS_EXPR,
6267 DECL_FIELD_BITPOS (constructor_fields),
6268 DECL_SIZE (constructor_fields));
6269 TREE_INT_CST_LOW (constructor_bit_index)
6270 = TREE_INT_CST_LOW (temp);
6271 TREE_INT_CST_HIGH (constructor_bit_index)
6272 = TREE_INT_CST_HIGH (temp);
6274 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6277 constructor_fields = TREE_CHAIN (constructor_fields);
6278 /* Skip any nameless bit fields at the beginning. */
6279 while (constructor_fields != 0
6280 && DECL_C_BIT_FIELD (constructor_fields)
6281 && DECL_NAME (constructor_fields) == 0)
6282 constructor_fields = TREE_CHAIN (constructor_fields);
6283 break;
6285 if (TREE_CODE (constructor_type) == UNION_TYPE)
6287 tree fieldtype;
6288 enum tree_code fieldcode;
6290 if (constructor_fields == 0)
6292 pedwarn_init ("excess elements in union initializer%s",
6293 " after `%s'", NULL_PTR);
6294 break;
6297 fieldtype = TREE_TYPE (constructor_fields);
6298 if (fieldtype != error_mark_node)
6299 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6300 fieldcode = TREE_CODE (fieldtype);
6302 /* Accept a string constant to initialize a subarray. */
6303 if (value != 0
6304 && fieldcode == ARRAY_TYPE
6305 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6306 && string_flag)
6307 value = orig_value;
6308 /* Otherwise, if we have come to a subaggregate,
6309 and we don't have an element of its type, push into it. */
6310 else if (value != 0 && !constructor_no_implicit
6311 && value != error_mark_node
6312 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6313 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6314 || fieldcode == UNION_TYPE))
6316 push_init_level (1);
6317 continue;
6320 if (value)
6322 push_member_name (constructor_fields);
6323 output_init_element (value, fieldtype, constructor_fields, 1);
6324 RESTORE_SPELLING_DEPTH (constructor_depth);
6326 else
6327 /* Do the bookkeeping for an element that was
6328 directly output as a constructor. */
6330 TREE_INT_CST_LOW (constructor_bit_index)
6331 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6332 TREE_INT_CST_HIGH (constructor_bit_index)
6333 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6335 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6338 constructor_fields = 0;
6339 break;
6341 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6343 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6344 enum tree_code eltcode = TREE_CODE (elttype);
6346 /* Accept a string constant to initialize a subarray. */
6347 if (value != 0
6348 && eltcode == ARRAY_TYPE
6349 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6350 && string_flag)
6351 value = orig_value;
6352 /* Otherwise, if we have come to a subaggregate,
6353 and we don't have an element of its type, push into it. */
6354 else if (value != 0 && !constructor_no_implicit
6355 && value != error_mark_node
6356 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6357 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6358 || eltcode == UNION_TYPE))
6360 push_init_level (1);
6361 continue;
6364 if (constructor_max_index != 0
6365 && tree_int_cst_lt (constructor_max_index, constructor_index))
6367 pedwarn_init ("excess elements in array initializer%s",
6368 " after `%s'", NULL_PTR);
6369 break;
6372 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */
6373 if (constructor_range_end)
6374 value = save_expr (value);
6376 /* Now output the actual element.
6377 Ordinarily, output once.
6378 If there is a range, repeat it till we advance past the range. */
6381 tree tem;
6383 if (value)
6385 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6386 output_init_element (value, elttype, constructor_index, 1);
6387 RESTORE_SPELLING_DEPTH (constructor_depth);
6390 tem = size_binop (PLUS_EXPR, constructor_index,
6391 integer_one_node);
6392 TREE_INT_CST_LOW (constructor_index)
6393 = TREE_INT_CST_LOW (tem);
6394 TREE_INT_CST_HIGH (constructor_index)
6395 = TREE_INT_CST_HIGH (tem);
6397 if (!value)
6398 /* If we are doing the bookkeeping for an element that was
6399 directly output as a constructor,
6400 we must update constructor_unfilled_index. */
6402 TREE_INT_CST_LOW (constructor_unfilled_index)
6403 = TREE_INT_CST_LOW (constructor_index);
6404 TREE_INT_CST_HIGH (constructor_unfilled_index)
6405 = TREE_INT_CST_HIGH (constructor_index);
6408 while (! (constructor_range_end == 0
6409 || tree_int_cst_lt (constructor_range_end,
6410 constructor_index)));
6412 break;
6415 /* Handle the sole element allowed in a braced initializer
6416 for a scalar variable. */
6417 if (constructor_fields == 0)
6419 pedwarn_init ("excess elements in scalar initializer%s",
6420 " after `%s'", NULL_PTR);
6421 break;
6424 if (value)
6425 output_init_element (value, constructor_type, NULL_TREE, 1);
6426 constructor_fields = 0;
6427 break;
6430 /* If the (lexically) previous elments are not now saved,
6431 we can discard the storage for them. */
6432 if (constructor_incremental && constructor_pending_elts == 0 && value != 0
6433 && constructor_stack == 0)
6434 clear_momentary ();
6437 /* Expand an ASM statement with operands, handling output operands
6438 that are not variables or INDIRECT_REFS by transforming such
6439 cases into cases that expand_asm_operands can handle.
6441 Arguments are same as for expand_asm_operands. */
6443 void
6444 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6445 tree string, outputs, inputs, clobbers;
6446 int vol;
6447 char *filename;
6448 int line;
6450 int noutputs = list_length (outputs);
6451 register int i;
6452 /* o[I] is the place that output number I should be written. */
6453 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6454 register tree tail;
6456 if (TREE_CODE (string) == ADDR_EXPR)
6457 string = TREE_OPERAND (string, 0);
6458 if (TREE_CODE (string) != STRING_CST)
6460 error ("asm template is not a string constant");
6461 return;
6464 /* Record the contents of OUTPUTS before it is modified. */
6465 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6466 o[i] = TREE_VALUE (tail);
6468 /* Perform default conversions on array and function inputs. */
6469 /* Don't do this for other types--
6470 it would screw up operands expected to be in memory. */
6471 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6472 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6473 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6474 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6476 /* Generate the ASM_OPERANDS insn;
6477 store into the TREE_VALUEs of OUTPUTS some trees for
6478 where the values were actually stored. */
6479 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6481 /* Copy all the intermediate outputs into the specified outputs. */
6482 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6484 if (o[i] != TREE_VALUE (tail))
6486 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6487 0, VOIDmode, 0);
6488 free_temp_slots ();
6490 /* Detect modification of read-only values.
6491 (Otherwise done by build_modify_expr.) */
6492 else
6494 tree type = TREE_TYPE (o[i]);
6495 if (TREE_READONLY (o[i])
6496 || TYPE_READONLY (type)
6497 || ((TREE_CODE (type) == RECORD_TYPE
6498 || TREE_CODE (type) == UNION_TYPE)
6499 && C_TYPE_FIELDS_READONLY (type)))
6500 readonly_warning (o[i], "modification by `asm'");
6504 /* Those MODIFY_EXPRs could do autoincrements. */
6505 emit_queue ();
6508 /* Expand a C `return' statement.
6509 RETVAL is the expression for what to return,
6510 or a null pointer for `return;' with no value. */
6512 void
6513 c_expand_return (retval)
6514 tree retval;
6516 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6518 if (TREE_THIS_VOLATILE (current_function_decl))
6519 warning ("function declared `noreturn' has a `return' statement");
6521 if (!retval)
6523 current_function_returns_null = 1;
6524 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6525 warning ("`return' with no value, in function returning non-void");
6526 expand_null_return ();
6528 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6530 current_function_returns_null = 1;
6531 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6532 pedwarn ("`return' with a value, in function returning void");
6533 expand_return (retval);
6535 else
6537 tree t = convert_for_assignment (valtype, retval, "return",
6538 NULL_TREE, NULL_TREE, 0);
6539 tree res = DECL_RESULT (current_function_decl);
6540 tree inner;
6542 if (t == error_mark_node)
6543 return;
6545 inner = t = convert (TREE_TYPE (res), t);
6547 /* Strip any conversions, additions, and subtractions, and see if
6548 we are returning the address of a local variable. Warn if so. */
6549 while (1)
6551 switch (TREE_CODE (inner))
6553 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6554 case PLUS_EXPR:
6555 inner = TREE_OPERAND (inner, 0);
6556 continue;
6558 case MINUS_EXPR:
6559 /* If the second operand of the MINUS_EXPR has a pointer
6560 type (or is converted from it), this may be valid, so
6561 don't give a warning. */
6563 tree op1 = TREE_OPERAND (inner, 1);
6565 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6566 && (TREE_CODE (op1) == NOP_EXPR
6567 || TREE_CODE (op1) == NON_LVALUE_EXPR
6568 || TREE_CODE (op1) == CONVERT_EXPR))
6569 op1 = TREE_OPERAND (op1, 0);
6571 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6572 break;
6574 inner = TREE_OPERAND (inner, 0);
6575 continue;
6578 case ADDR_EXPR:
6579 inner = TREE_OPERAND (inner, 0);
6581 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6582 inner = TREE_OPERAND (inner, 0);
6584 if (TREE_CODE (inner) == VAR_DECL
6585 && ! DECL_EXTERNAL (inner)
6586 && ! TREE_STATIC (inner)
6587 && DECL_CONTEXT (inner) == current_function_decl)
6588 warning ("function returns address of local variable");
6589 break;
6592 break;
6595 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6596 TREE_SIDE_EFFECTS (t) = 1;
6597 expand_return (t);
6598 current_function_returns_value = 1;
6602 /* Start a C switch statement, testing expression EXP.
6603 Return EXP if it is valid, an error node otherwise. */
6605 tree
6606 c_expand_start_case (exp)
6607 tree exp;
6609 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6610 tree type = TREE_TYPE (exp);
6612 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6614 error ("switch quantity not an integer");
6615 exp = error_mark_node;
6617 else
6619 tree index;
6620 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6622 if (warn_traditional
6623 && (type == long_integer_type_node
6624 || type == long_unsigned_type_node))
6625 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6627 exp = default_conversion (exp);
6628 type = TREE_TYPE (exp);
6629 index = get_unwidened (exp, NULL_TREE);
6630 /* We can't strip a conversion from a signed type to an unsigned,
6631 because if we did, int_fits_type_p would do the wrong thing
6632 when checking case values for being in range,
6633 and it's too hard to do the right thing. */
6634 if (TREE_UNSIGNED (TREE_TYPE (exp))
6635 == TREE_UNSIGNED (TREE_TYPE (index)))
6636 exp = index;
6639 expand_start_case (1, exp, type, "switch statement");
6641 return exp;