Import gcc-2.8.1.tar.bz2
[official-gcc.git] / gcc / c-typeck.c
blobe0989582a989f5ce8b8fe3330c9a4340d7586281
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 88, 91-7, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This file is part of the C front end.
23 It contains routines to build C expressions given their operands,
24 including computing the types of the result, C-specific error checks,
25 and some optimization.
27 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
28 and to process initializations in declarations (since they work
29 like a strange sort of assignment). */
31 #include "config.h"
32 #include <stdio.h>
33 #include "tree.h"
34 #include "c-tree.h"
35 #include "flags.h"
36 #include "output.h"
38 #ifdef HAVE_STDLIB_H
39 #include <stdlib.h>
40 #endif
42 #ifdef HAVE_STRING_H
43 #include <string.h>
44 #else
45 #ifdef HAVE_STRINGS_H
46 #include <strings.h>
47 #endif
48 #endif
50 /* Nonzero if we've already printed a "missing braces around initializer"
51 message within this initializer. */
52 static int missing_braces_mentioned;
54 #ifdef NEED_DECLARATION_INDEX
55 extern char *index ();
56 #endif
58 #ifdef NEED_DECLARATION_RINDEX
59 extern char *rindex ();
60 #endif
62 static tree qualify_type PROTO((tree, tree));
63 static int comp_target_types PROTO((tree, tree));
64 static int function_types_compatible_p PROTO((tree, tree));
65 static int type_lists_compatible_p PROTO((tree, tree));
66 static int self_promoting_type_p PROTO((tree));
67 static tree decl_constant_value PROTO((tree));
68 static tree lookup_field PROTO((tree, tree, tree *));
69 static tree convert_arguments PROTO((tree, tree, tree, tree));
70 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
71 static tree pointer_diff PROTO((tree, tree));
72 static tree unary_complex_lvalue PROTO((enum tree_code, tree));
73 static void pedantic_lvalue_warning PROTO((enum tree_code));
74 static tree internal_build_compound_expr PROTO((tree, int));
75 static tree convert_for_assignment PROTO((tree, tree, char *, tree,
76 tree, int));
77 static void warn_for_assignment PROTO((char *, char *, tree, int));
78 static tree valid_compound_expr_initializer PROTO((tree, tree));
79 static void push_string PROTO((char *));
80 static void push_member_name PROTO((tree));
81 static void push_array_bounds PROTO((int));
82 static int spelling_length PROTO((void));
83 static char *print_spelling PROTO((char *));
84 static char *get_spelling PROTO((char *));
85 static void warning_init PROTO((char *, char *,
86 char *));
87 static tree digest_init PROTO((tree, tree, int, int));
88 static void check_init_type_bitfields PROTO((tree));
89 static void output_init_element PROTO((tree, tree, tree, int));
90 static void output_pending_init_elements PROTO((int));
92 /* Do `exp = require_complete_type (exp);' to make sure exp
93 does not have an incomplete type. (That includes void types.) */
95 tree
96 require_complete_type (value)
97 tree value;
99 tree type = TREE_TYPE (value);
101 /* First, detect a valid value with a complete type. */
102 if (TYPE_SIZE (type) != 0
103 && type != void_type_node)
104 return value;
106 incomplete_type_error (value, type);
107 return error_mark_node;
110 /* Print an error message for invalid use of an incomplete type.
111 VALUE is the expression that was used (or 0 if that isn't known)
112 and TYPE is the type that was invalid. */
114 void
115 incomplete_type_error (value, type)
116 tree value;
117 tree type;
119 char *errmsg;
121 /* Avoid duplicate error message. */
122 if (TREE_CODE (type) == ERROR_MARK)
123 return;
125 if (value != 0 && (TREE_CODE (value) == VAR_DECL
126 || TREE_CODE (value) == PARM_DECL))
127 error ("`%s' has an incomplete type",
128 IDENTIFIER_POINTER (DECL_NAME (value)));
129 else
131 retry:
132 /* We must print an error message. Be clever about what it says. */
134 switch (TREE_CODE (type))
136 case RECORD_TYPE:
137 errmsg = "invalid use of undefined type `struct %s'";
138 break;
140 case UNION_TYPE:
141 errmsg = "invalid use of undefined type `union %s'";
142 break;
144 case ENUMERAL_TYPE:
145 errmsg = "invalid use of undefined type `enum %s'";
146 break;
148 case VOID_TYPE:
149 error ("invalid use of void expression");
150 return;
152 case ARRAY_TYPE:
153 if (TYPE_DOMAIN (type))
155 type = TREE_TYPE (type);
156 goto retry;
158 error ("invalid use of array with unspecified bounds");
159 return;
161 default:
162 abort ();
165 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
166 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
167 else
168 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
169 error ("invalid use of incomplete typedef `%s'",
170 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
174 /* Return a variant of TYPE which has all the type qualifiers of LIKE
175 as well as those of TYPE. */
177 static tree
178 qualify_type (type, like)
179 tree type, like;
181 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
182 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
183 return c_build_type_variant (type, constflag, volflag);
186 /* Return the common type of two types.
187 We assume that comptypes has already been done and returned 1;
188 if that isn't so, this may crash. In particular, we assume that qualifiers
189 match.
191 This is the type for the result of most arithmetic operations
192 if the operands have the given two types. */
194 tree
195 common_type (t1, t2)
196 tree t1, t2;
198 register enum tree_code code1;
199 register enum tree_code code2;
200 tree attributes;
202 /* Save time if the two types are the same. */
204 if (t1 == t2) return t1;
206 /* If one type is nonsense, use the other. */
207 if (t1 == error_mark_node)
208 return t2;
209 if (t2 == error_mark_node)
210 return t1;
212 /* Merge the attributes */
213 attributes = merge_attributes (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
215 /* Treat an enum type as the unsigned integer type of the same width. */
217 if (TREE_CODE (t1) == ENUMERAL_TYPE)
218 t1 = type_for_size (TYPE_PRECISION (t1), 1);
219 if (TREE_CODE (t2) == ENUMERAL_TYPE)
220 t2 = type_for_size (TYPE_PRECISION (t2), 1);
222 code1 = TREE_CODE (t1);
223 code2 = TREE_CODE (t2);
225 /* If one type is complex, form the common type of the non-complex
226 components, then make that complex. Use T1 or T2 if it is the
227 required type. */
228 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
230 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
231 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
232 tree subtype = common_type (subtype1, subtype2);
234 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
235 return build_type_attribute_variant (t1, attributes);
236 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
237 return build_type_attribute_variant (t2, attributes);
238 else
239 return build_type_attribute_variant (build_complex_type (subtype),
240 attributes);
243 switch (code1)
245 case INTEGER_TYPE:
246 case REAL_TYPE:
247 /* If only one is real, use it as the result. */
249 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
250 return build_type_attribute_variant (t1, attributes);
252 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
253 return build_type_attribute_variant (t2, attributes);
255 /* Both real or both integers; use the one with greater precision. */
257 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
258 return build_type_attribute_variant (t1, attributes);
259 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
260 return build_type_attribute_variant (t2, attributes);
262 /* Same precision. Prefer longs to ints even when same size. */
264 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
265 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
266 return build_type_attribute_variant (long_unsigned_type_node,
267 attributes);
269 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
270 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
272 /* But preserve unsignedness from the other type,
273 since long cannot hold all the values of an unsigned int. */
274 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
275 t1 = long_unsigned_type_node;
276 else
277 t1 = long_integer_type_node;
278 return build_type_attribute_variant (t1, attributes);
281 /* Likewise, prefer long double to double even if same size. */
282 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
283 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
284 return build_type_attribute_variant (long_double_type_node,
285 attributes);
287 /* Otherwise prefer the unsigned one. */
289 if (TREE_UNSIGNED (t1))
290 return build_type_attribute_variant (t1, attributes);
291 else
292 return build_type_attribute_variant (t2, attributes);
294 case POINTER_TYPE:
295 /* For two pointers, do this recursively on the target type,
296 and combine the qualifiers of the two types' targets. */
297 /* This code was turned off; I don't know why.
298 But ANSI C specifies doing this with the qualifiers.
299 So I turned it on again. */
301 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
302 TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
303 int constp
304 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
305 int volatilep
306 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
307 t1 = build_pointer_type (c_build_type_variant (target, constp,
308 volatilep));
309 return build_type_attribute_variant (t1, attributes);
311 #if 0
312 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
313 return build_type_attribute_variant (t1, attributes);
314 #endif
316 case ARRAY_TYPE:
318 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
319 /* Save space: see if the result is identical to one of the args. */
320 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
321 return build_type_attribute_variant (t1, attributes);
322 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
323 return build_type_attribute_variant (t2, attributes);
324 /* Merge the element types, and have a size if either arg has one. */
325 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
326 return build_type_attribute_variant (t1, attributes);
329 case FUNCTION_TYPE:
330 /* Function types: prefer the one that specified arg types.
331 If both do, merge the arg types. Also merge the return types. */
333 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
334 tree p1 = TYPE_ARG_TYPES (t1);
335 tree p2 = TYPE_ARG_TYPES (t2);
336 int len;
337 tree newargs, n;
338 int i;
340 /* Save space: see if the result is identical to one of the args. */
341 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
342 return build_type_attribute_variant (t1, attributes);
343 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
344 return build_type_attribute_variant (t2, attributes);
346 /* Simple way if one arg fails to specify argument types. */
347 if (TYPE_ARG_TYPES (t1) == 0)
349 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
350 return build_type_attribute_variant (t1, attributes);
352 if (TYPE_ARG_TYPES (t2) == 0)
354 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
355 return build_type_attribute_variant (t1, attributes);
358 /* If both args specify argument types, we must merge the two
359 lists, argument by argument. */
361 len = list_length (p1);
362 newargs = 0;
364 for (i = 0; i < len; i++)
365 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
367 n = newargs;
369 for (; p1;
370 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
372 /* A null type means arg type is not specified.
373 Take whatever the other function type has. */
374 if (TREE_VALUE (p1) == 0)
376 TREE_VALUE (n) = TREE_VALUE (p2);
377 goto parm_done;
379 if (TREE_VALUE (p2) == 0)
381 TREE_VALUE (n) = TREE_VALUE (p1);
382 goto parm_done;
385 /* Given wait (union {union wait *u; int *i} *)
386 and wait (union wait *),
387 prefer union wait * as type of parm. */
388 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
389 && TREE_VALUE (p1) != TREE_VALUE (p2))
391 tree memb;
392 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
393 memb; memb = TREE_CHAIN (memb))
394 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
396 TREE_VALUE (n) = TREE_VALUE (p2);
397 if (pedantic)
398 pedwarn ("function types not truly compatible in ANSI C");
399 goto parm_done;
402 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
403 && TREE_VALUE (p2) != TREE_VALUE (p1))
405 tree memb;
406 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
407 memb; memb = TREE_CHAIN (memb))
408 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
410 TREE_VALUE (n) = TREE_VALUE (p1);
411 if (pedantic)
412 pedwarn ("function types not truly compatible in ANSI C");
413 goto parm_done;
416 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
417 parm_done: ;
420 t1 = build_function_type (valtype, newargs);
421 /* ... falls through ... */
424 default:
425 return build_type_attribute_variant (t1, attributes);
430 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
431 or various other operations. Return 2 if they are compatible
432 but a warning may be needed if you use them together. */
435 comptypes (type1, type2)
436 tree type1, type2;
438 register tree t1 = type1;
439 register tree t2 = type2;
440 int attrval, val;
442 /* Suppress errors caused by previously reported errors. */
444 if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
445 return 1;
447 /* Treat an enum type as the integer type of the same width and
448 signedness. */
450 if (TREE_CODE (t1) == ENUMERAL_TYPE)
451 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
452 if (TREE_CODE (t2) == ENUMERAL_TYPE)
453 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
455 if (t1 == t2)
456 return 1;
458 /* Different classes of types can't be compatible. */
460 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
462 /* Qualifiers must match. */
464 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
465 return 0;
466 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
467 return 0;
469 /* Allow for two different type nodes which have essentially the same
470 definition. Note that we already checked for equality of the type
471 type qualifiers (just above). */
473 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
474 return 1;
476 #ifndef COMP_TYPE_ATTRIBUTES
477 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
478 #endif
480 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
481 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
482 return 0;
484 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
485 val = 0;
487 switch (TREE_CODE (t1))
489 case POINTER_TYPE:
490 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
491 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
492 break;
494 case FUNCTION_TYPE:
495 val = function_types_compatible_p (t1, t2);
496 break;
498 case ARRAY_TYPE:
500 tree d1 = TYPE_DOMAIN (t1);
501 tree d2 = TYPE_DOMAIN (t2);
502 val = 1;
504 /* Target types must match incl. qualifiers. */
505 if (TREE_TYPE (t1) != TREE_TYPE (t2)
506 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
507 return 0;
509 /* Sizes must match unless one is missing or variable. */
510 if (d1 == 0 || d2 == 0 || d1 == d2
511 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
512 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
513 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
514 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
515 break;
517 if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
518 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
519 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
520 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
521 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
522 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
523 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
524 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2)))))
525 val = 0;
526 break;
529 case RECORD_TYPE:
530 if (maybe_objc_comptypes (t1, t2, 0) == 1)
531 val = 1;
532 break;
534 default:
535 break;
537 return attrval == 2 && val == 1 ? 2 : val;
540 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
541 ignoring their qualifiers. */
543 static int
544 comp_target_types (ttl, ttr)
545 tree ttl, ttr;
547 int val;
549 /* Give maybe_objc_comptypes a crack at letting these types through. */
550 if (val = maybe_objc_comptypes (ttl, ttr, 1) >= 0)
551 return val;
553 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
554 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
556 if (val == 2 && pedantic)
557 pedwarn ("types are not quite compatible");
558 return val;
561 /* Subroutines of `comptypes'. */
563 /* Return 1 if two function types F1 and F2 are compatible.
564 If either type specifies no argument types,
565 the other must specify a fixed number of self-promoting arg types.
566 Otherwise, if one type specifies only the number of arguments,
567 the other must specify that number of self-promoting arg types.
568 Otherwise, the argument types must match. */
570 static int
571 function_types_compatible_p (f1, f2)
572 tree f1, f2;
574 tree args1, args2;
575 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
576 int val = 1;
577 int val1;
579 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
580 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
581 return 0;
583 args1 = TYPE_ARG_TYPES (f1);
584 args2 = TYPE_ARG_TYPES (f2);
586 /* An unspecified parmlist matches any specified parmlist
587 whose argument types don't need default promotions. */
589 if (args1 == 0)
591 if (!self_promoting_args_p (args2))
592 return 0;
593 /* If one of these types comes from a non-prototype fn definition,
594 compare that with the other type's arglist.
595 If they don't match, ask for a warning (but no error). */
596 if (TYPE_ACTUAL_ARG_TYPES (f1)
597 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
598 val = 2;
599 return val;
601 if (args2 == 0)
603 if (!self_promoting_args_p (args1))
604 return 0;
605 if (TYPE_ACTUAL_ARG_TYPES (f2)
606 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
607 val = 2;
608 return val;
611 /* Both types have argument lists: compare them and propagate results. */
612 val1 = type_lists_compatible_p (args1, args2);
613 return val1 != 1 ? val1 : val;
616 /* Check two lists of types for compatibility,
617 returning 0 for incompatible, 1 for compatible,
618 or 2 for compatible with warning. */
620 static int
621 type_lists_compatible_p (args1, args2)
622 tree args1, args2;
624 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
625 int val = 1;
626 int newval = 0;
628 while (1)
630 if (args1 == 0 && args2 == 0)
631 return val;
632 /* If one list is shorter than the other,
633 they fail to match. */
634 if (args1 == 0 || args2 == 0)
635 return 0;
636 /* A null pointer instead of a type
637 means there is supposed to be an argument
638 but nothing is specified about what type it has.
639 So match anything that self-promotes. */
640 if (TREE_VALUE (args1) == 0)
642 if (! self_promoting_type_p (TREE_VALUE (args2)))
643 return 0;
645 else if (TREE_VALUE (args2) == 0)
647 if (! self_promoting_type_p (TREE_VALUE (args1)))
648 return 0;
650 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
652 /* Allow wait (union {union wait *u; int *i} *)
653 and wait (union wait *) to be compatible. */
654 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
655 && (TYPE_NAME (TREE_VALUE (args1)) == 0
656 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
657 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
658 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
659 TYPE_SIZE (TREE_VALUE (args2))))
661 tree memb;
662 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
663 memb; memb = TREE_CHAIN (memb))
664 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
665 break;
666 if (memb == 0)
667 return 0;
669 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
670 && (TYPE_NAME (TREE_VALUE (args2)) == 0
671 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
672 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
673 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
674 TYPE_SIZE (TREE_VALUE (args1))))
676 tree memb;
677 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
678 memb; memb = TREE_CHAIN (memb))
679 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
680 break;
681 if (memb == 0)
682 return 0;
684 else
685 return 0;
688 /* comptypes said ok, but record if it said to warn. */
689 if (newval > val)
690 val = newval;
692 args1 = TREE_CHAIN (args1);
693 args2 = TREE_CHAIN (args2);
697 /* Return 1 if PARMS specifies a fixed number of parameters
698 and none of their types is affected by default promotions. */
701 self_promoting_args_p (parms)
702 tree parms;
704 register tree t;
705 for (t = parms; t; t = TREE_CHAIN (t))
707 register tree type = TREE_VALUE (t);
709 if (TREE_CHAIN (t) == 0 && type != void_type_node)
710 return 0;
712 if (type == 0)
713 return 0;
715 if (TYPE_MAIN_VARIANT (type) == float_type_node)
716 return 0;
718 if (C_PROMOTING_INTEGER_TYPE_P (type))
719 return 0;
721 return 1;
724 /* Return 1 if TYPE is not affected by default promotions. */
726 static int
727 self_promoting_type_p (type)
728 tree type;
730 if (TYPE_MAIN_VARIANT (type) == float_type_node)
731 return 0;
733 if (C_PROMOTING_INTEGER_TYPE_P (type))
734 return 0;
736 return 1;
739 /* Return an unsigned type the same as TYPE in other respects. */
741 tree
742 unsigned_type (type)
743 tree type;
745 tree type1 = TYPE_MAIN_VARIANT (type);
746 if (type1 == signed_char_type_node || type1 == char_type_node)
747 return unsigned_char_type_node;
748 if (type1 == integer_type_node)
749 return unsigned_type_node;
750 if (type1 == short_integer_type_node)
751 return short_unsigned_type_node;
752 if (type1 == long_integer_type_node)
753 return long_unsigned_type_node;
754 if (type1 == long_long_integer_type_node)
755 return long_long_unsigned_type_node;
756 if (type1 == intDI_type_node)
757 return unsigned_intDI_type_node;
758 if (type1 == intSI_type_node)
759 return unsigned_intSI_type_node;
760 if (type1 == intHI_type_node)
761 return unsigned_intHI_type_node;
762 if (type1 == intQI_type_node)
763 return unsigned_intQI_type_node;
765 return signed_or_unsigned_type (1, type);
768 /* Return a signed type the same as TYPE in other respects. */
770 tree
771 signed_type (type)
772 tree type;
774 tree type1 = TYPE_MAIN_VARIANT (type);
775 if (type1 == unsigned_char_type_node || type1 == char_type_node)
776 return signed_char_type_node;
777 if (type1 == unsigned_type_node)
778 return integer_type_node;
779 if (type1 == short_unsigned_type_node)
780 return short_integer_type_node;
781 if (type1 == long_unsigned_type_node)
782 return long_integer_type_node;
783 if (type1 == long_long_unsigned_type_node)
784 return long_long_integer_type_node;
785 if (type1 == unsigned_intDI_type_node)
786 return intDI_type_node;
787 if (type1 == unsigned_intSI_type_node)
788 return intSI_type_node;
789 if (type1 == unsigned_intHI_type_node)
790 return intHI_type_node;
791 if (type1 == unsigned_intQI_type_node)
792 return intQI_type_node;
794 return signed_or_unsigned_type (0, type);
797 /* Return a type the same as TYPE except unsigned or
798 signed according to UNSIGNEDP. */
800 tree
801 signed_or_unsigned_type (unsignedp, type)
802 int unsignedp;
803 tree type;
805 if ((! INTEGRAL_TYPE_P (type) && ! POINTER_TYPE_P (type))
806 || TREE_UNSIGNED (type) == unsignedp)
807 return type;
808 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
809 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
810 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
811 return unsignedp ? unsigned_type_node : integer_type_node;
812 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
813 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
814 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
815 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
816 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
817 return (unsignedp ? long_long_unsigned_type_node
818 : long_long_integer_type_node);
819 return type;
822 /* Compute the value of the `sizeof' operator. */
824 tree
825 c_sizeof (type)
826 tree type;
828 enum tree_code code = TREE_CODE (type);
829 tree t;
831 if (code == FUNCTION_TYPE)
833 if (pedantic || warn_pointer_arith)
834 pedwarn ("sizeof applied to a function type");
835 return size_int (1);
837 if (code == VOID_TYPE)
839 if (pedantic || warn_pointer_arith)
840 pedwarn ("sizeof applied to a void type");
841 return size_int (1);
843 if (code == ERROR_MARK)
844 return size_int (1);
845 if (TYPE_SIZE (type) == 0)
847 error ("sizeof applied to an incomplete type");
848 return size_int (0);
851 /* Convert in case a char is more than one unit. */
852 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
853 size_int (TYPE_PRECISION (char_type_node)));
854 /* size_binop does not put the constant in range, so do it now. */
855 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
856 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
857 return t;
860 tree
861 c_sizeof_nowarn (type)
862 tree type;
864 enum tree_code code = TREE_CODE (type);
865 tree t;
867 if (code == FUNCTION_TYPE
868 || code == VOID_TYPE
869 || code == ERROR_MARK)
870 return size_int (1);
871 if (TYPE_SIZE (type) == 0)
872 return size_int (0);
874 /* Convert in case a char is more than one unit. */
875 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
876 size_int (TYPE_PRECISION (char_type_node)));
877 force_fit_type (t, 0);
878 return t;
881 /* Compute the size to increment a pointer by. */
883 tree
884 c_size_in_bytes (type)
885 tree type;
887 enum tree_code code = TREE_CODE (type);
888 tree t;
890 if (code == FUNCTION_TYPE)
891 return size_int (1);
892 if (code == VOID_TYPE)
893 return size_int (1);
894 if (code == ERROR_MARK)
895 return size_int (1);
896 if (TYPE_SIZE (type) == 0)
898 error ("arithmetic on pointer to an incomplete type");
899 return size_int (1);
902 /* Convert in case a char is more than one unit. */
903 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
904 size_int (BITS_PER_UNIT));
905 force_fit_type (t, 0);
906 return t;
909 /* Implement the __alignof keyword: Return the minimum required
910 alignment of TYPE, measured in bytes. */
912 tree
913 c_alignof (type)
914 tree type;
916 enum tree_code code = TREE_CODE (type);
918 if (code == FUNCTION_TYPE)
919 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
921 if (code == VOID_TYPE || code == ERROR_MARK)
922 return size_int (1);
924 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
927 /* Implement the __alignof keyword: Return the minimum required
928 alignment of EXPR, measured in bytes. For VAR_DECL's and
929 FIELD_DECL's return DECL_ALIGN (which can be set from an
930 "aligned" __attribute__ specification). */
932 tree
933 c_alignof_expr (expr)
934 tree expr;
936 if (TREE_CODE (expr) == VAR_DECL)
937 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
939 if (TREE_CODE (expr) == COMPONENT_REF
940 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
942 error ("`__alignof' applied to a bit-field");
943 return size_int (1);
945 else if (TREE_CODE (expr) == COMPONENT_REF
946 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
947 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
949 if (TREE_CODE (expr) == INDIRECT_REF)
951 tree t = TREE_OPERAND (expr, 0);
952 tree best = t;
953 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
955 while (TREE_CODE (t) == NOP_EXPR
956 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
958 int thisalign;
960 t = TREE_OPERAND (t, 0);
961 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
962 if (thisalign > bestalign)
963 best = t, bestalign = thisalign;
965 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
967 else
968 return c_alignof (TREE_TYPE (expr));
971 /* Return either DECL or its known constant value (if it has one). */
973 static tree
974 decl_constant_value (decl)
975 tree decl;
977 if (/* Don't change a variable array bound or initial value to a constant
978 in a place where a variable is invalid. */
979 current_function_decl != 0
980 && ! pedantic
981 && ! TREE_THIS_VOLATILE (decl)
982 && TREE_READONLY (decl) && ! ITERATOR_P (decl)
983 && DECL_INITIAL (decl) != 0
984 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
985 /* This is invalid if initial value is not constant.
986 If it has either a function call, a memory reference,
987 or a variable, then re-evaluating it could give different results. */
988 && TREE_CONSTANT (DECL_INITIAL (decl))
989 /* Check for cases where this is sub-optimal, even though valid. */
990 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
991 && DECL_MODE (decl) != BLKmode)
992 return DECL_INITIAL (decl);
993 return decl;
996 /* Perform default promotions for C data used in expressions.
997 Arrays and functions are converted to pointers;
998 enumeral types or short or char, to int.
999 In addition, manifest constants symbols are replaced by their values. */
1001 tree
1002 default_conversion (exp)
1003 tree exp;
1005 register tree type = TREE_TYPE (exp);
1006 register enum tree_code code = TREE_CODE (type);
1008 /* Constants can be used directly unless they're not loadable. */
1009 if (TREE_CODE (exp) == CONST_DECL)
1010 exp = DECL_INITIAL (exp);
1012 /* Replace a nonvolatile const static variable with its value unless
1013 it is an array, in which case we must be sure that taking the
1014 address of the array produces consistent results. */
1015 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1017 exp = decl_constant_value (exp);
1018 type = TREE_TYPE (exp);
1021 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1022 an lvalue. */
1023 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
1024 to integer and cause infinite recursion. */
1025 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1026 || (TREE_CODE (exp) == NOP_EXPR
1027 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1028 exp = TREE_OPERAND (exp, 0);
1030 /* Normally convert enums to int,
1031 but convert wide enums to something wider. */
1032 if (code == ENUMERAL_TYPE)
1034 type = type_for_size (MAX (TYPE_PRECISION (type),
1035 TYPE_PRECISION (integer_type_node)),
1036 ((flag_traditional
1037 || (TYPE_PRECISION (type)
1038 >= TYPE_PRECISION (integer_type_node)))
1039 && TREE_UNSIGNED (type)));
1040 return convert (type, exp);
1043 if (TREE_CODE (exp) == COMPONENT_REF
1044 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
1046 tree width = DECL_SIZE (TREE_OPERAND (exp, 1));
1047 HOST_WIDE_INT low = TREE_INT_CST_LOW (width);
1049 /* If it's thinner than an int, promote it like a
1050 C_PROMOTING_INTEGER_TYPE_P, otherwise leave it alone. */
1052 if (low < TYPE_PRECISION (integer_type_node))
1054 if (flag_traditional && TREE_UNSIGNED (type))
1055 return convert (unsigned_type_node, exp);
1056 else
1057 return convert (integer_type_node, exp);
1061 if (C_PROMOTING_INTEGER_TYPE_P (type))
1063 /* Traditionally, unsignedness is preserved in default promotions.
1064 Also preserve unsignedness if not really getting any wider. */
1065 if (TREE_UNSIGNED (type)
1066 && (flag_traditional
1067 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1068 return convert (unsigned_type_node, exp);
1069 return convert (integer_type_node, exp);
1071 if (flag_traditional && !flag_allow_single_precision
1072 && TYPE_MAIN_VARIANT (type) == float_type_node)
1073 return convert (double_type_node, exp);
1074 if (code == VOID_TYPE)
1076 error ("void value not ignored as it ought to be");
1077 return error_mark_node;
1079 if (code == FUNCTION_TYPE)
1081 return build_unary_op (ADDR_EXPR, exp, 0);
1083 if (code == ARRAY_TYPE)
1085 register tree adr;
1086 tree restype = TREE_TYPE (type);
1087 tree ptrtype;
1088 int constp = 0;
1089 int volatilep = 0;
1091 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1092 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1094 constp = TREE_READONLY (exp);
1095 volatilep = TREE_THIS_VOLATILE (exp);
1098 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1099 || constp || volatilep)
1100 restype = c_build_type_variant (restype,
1101 TYPE_READONLY (type) || constp,
1102 TYPE_VOLATILE (type) || volatilep);
1104 if (TREE_CODE (exp) == INDIRECT_REF)
1105 return convert (TYPE_POINTER_TO (restype),
1106 TREE_OPERAND (exp, 0));
1108 if (TREE_CODE (exp) == COMPOUND_EXPR)
1110 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1111 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1112 TREE_OPERAND (exp, 0), op1);
1115 if (! lvalue_p (exp)
1116 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1118 error ("invalid use of non-lvalue array");
1119 return error_mark_node;
1122 ptrtype = build_pointer_type (restype);
1124 if (TREE_CODE (exp) == VAR_DECL)
1126 /* ??? This is not really quite correct
1127 in that the type of the operand of ADDR_EXPR
1128 is not the target type of the type of the ADDR_EXPR itself.
1129 Question is, can this lossage be avoided? */
1130 adr = build1 (ADDR_EXPR, ptrtype, exp);
1131 if (mark_addressable (exp) == 0)
1132 return error_mark_node;
1133 TREE_CONSTANT (adr) = staticp (exp);
1134 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1135 return adr;
1137 /* This way is better for a COMPONENT_REF since it can
1138 simplify the offset for a component. */
1139 adr = build_unary_op (ADDR_EXPR, exp, 1);
1140 return convert (ptrtype, adr);
1142 return exp;
1145 /* Look up component name in the structure type definition.
1147 If this component name is found indirectly within an anonymous union,
1148 store in *INDIRECT the component which directly contains
1149 that anonymous union. Otherwise, set *INDIRECT to 0. */
1151 static tree
1152 lookup_field (type, component, indirect)
1153 tree type, component;
1154 tree *indirect;
1156 tree field;
1158 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1159 to the field elements. Use a binary search on this array to quickly
1160 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1161 will always be set for structures which have many elements. */
1163 if (TYPE_LANG_SPECIFIC (type))
1165 int bot, top, half;
1166 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1168 field = TYPE_FIELDS (type);
1169 bot = 0;
1170 top = TYPE_LANG_SPECIFIC (type)->len;
1171 while (top - bot > 1)
1173 half = (top - bot + 1) >> 1;
1174 field = field_array[bot+half];
1176 if (DECL_NAME (field) == NULL_TREE)
1178 /* Step through all anon unions in linear fashion. */
1179 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1181 tree anon = 0, junk;
1183 field = field_array[bot++];
1184 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1185 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1186 anon = lookup_field (TREE_TYPE (field), component, &junk);
1188 if (anon != NULL_TREE)
1190 *indirect = field;
1191 return anon;
1195 /* Entire record is only anon unions. */
1196 if (bot > top)
1197 return NULL_TREE;
1199 /* Restart the binary search, with new lower bound. */
1200 continue;
1203 if (DECL_NAME (field) == component)
1204 break;
1205 if (DECL_NAME (field) < component)
1206 bot += half;
1207 else
1208 top = bot + half;
1211 if (DECL_NAME (field_array[bot]) == component)
1212 field = field_array[bot];
1213 else if (DECL_NAME (field) != component)
1214 field = 0;
1216 else
1218 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1220 if (DECL_NAME (field) == NULL_TREE)
1222 tree junk;
1223 tree anon = 0;
1225 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1226 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1227 anon = lookup_field (TREE_TYPE (field), component, &junk);
1229 if (anon != NULL_TREE)
1231 *indirect = field;
1232 return anon;
1236 if (DECL_NAME (field) == component)
1237 break;
1241 *indirect = NULL_TREE;
1242 return field;
1245 /* Make an expression to refer to the COMPONENT field of
1246 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1248 tree
1249 build_component_ref (datum, component)
1250 tree datum, component;
1252 register tree type = TREE_TYPE (datum);
1253 register enum tree_code code = TREE_CODE (type);
1254 register tree field = NULL;
1255 register tree ref;
1257 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1258 unless we are not to support things not strictly ANSI. */
1259 switch (TREE_CODE (datum))
1261 case COMPOUND_EXPR:
1263 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1264 return build (COMPOUND_EXPR, TREE_TYPE (value),
1265 TREE_OPERAND (datum, 0), value);
1267 case COND_EXPR:
1268 return build_conditional_expr
1269 (TREE_OPERAND (datum, 0),
1270 build_component_ref (TREE_OPERAND (datum, 1), component),
1271 build_component_ref (TREE_OPERAND (datum, 2), component));
1273 default:
1274 break;
1277 /* See if there is a field or component with name COMPONENT. */
1279 if (code == RECORD_TYPE || code == UNION_TYPE)
1281 tree indirect = 0;
1283 if (TYPE_SIZE (type) == 0)
1285 incomplete_type_error (NULL_TREE, type);
1286 return error_mark_node;
1289 field = lookup_field (type, component, &indirect);
1291 if (!field)
1293 error (code == RECORD_TYPE
1294 ? "structure has no member named `%s'"
1295 : "union has no member named `%s'",
1296 IDENTIFIER_POINTER (component));
1297 return error_mark_node;
1299 if (TREE_TYPE (field) == error_mark_node)
1300 return error_mark_node;
1302 /* If FIELD was found buried within an anonymous union,
1303 make one COMPONENT_REF to get that anonymous union,
1304 then fall thru to make a second COMPONENT_REF to get FIELD. */
1305 if (indirect != 0)
1307 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1308 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1309 TREE_READONLY (ref) = 1;
1310 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1311 TREE_THIS_VOLATILE (ref) = 1;
1312 datum = ref;
1315 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1317 if (TREE_READONLY (datum) || TREE_READONLY (field))
1318 TREE_READONLY (ref) = 1;
1319 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1320 TREE_THIS_VOLATILE (ref) = 1;
1322 return ref;
1324 else if (code != ERROR_MARK)
1325 error ("request for member `%s' in something not a structure or union",
1326 IDENTIFIER_POINTER (component));
1328 return error_mark_node;
1331 /* Given an expression PTR for a pointer, return an expression
1332 for the value pointed to.
1333 ERRORSTRING is the name of the operator to appear in error messages. */
1335 tree
1336 build_indirect_ref (ptr, errorstring)
1337 tree ptr;
1338 char *errorstring;
1340 register tree pointer = default_conversion (ptr);
1341 register tree type = TREE_TYPE (pointer);
1343 if (TREE_CODE (type) == POINTER_TYPE)
1345 if (TREE_CODE (pointer) == ADDR_EXPR
1346 && !flag_volatile
1347 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1348 == TREE_TYPE (type)))
1349 return TREE_OPERAND (pointer, 0);
1350 else
1352 tree t = TREE_TYPE (type);
1353 register tree ref = build1 (INDIRECT_REF,
1354 TYPE_MAIN_VARIANT (t), pointer);
1356 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1358 error ("dereferencing pointer to incomplete type");
1359 return error_mark_node;
1361 if (TREE_CODE (t) == VOID_TYPE && skip_evaluation == 0)
1362 warning ("dereferencing `void *' pointer");
1364 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1365 so that we get the proper error message if the result is used
1366 to assign to. Also, &* is supposed to be a no-op.
1367 And ANSI C seems to specify that the type of the result
1368 should be the const type. */
1369 /* A de-reference of a pointer to const is not a const. It is valid
1370 to change it via some other pointer. */
1371 TREE_READONLY (ref) = TYPE_READONLY (t);
1372 TREE_SIDE_EFFECTS (ref)
1373 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1374 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1375 return ref;
1378 else if (TREE_CODE (pointer) != ERROR_MARK)
1379 error ("invalid type argument of `%s'", errorstring);
1380 return error_mark_node;
1383 /* This handles expressions of the form "a[i]", which denotes
1384 an array reference.
1386 This is logically equivalent in C to *(a+i), but we may do it differently.
1387 If A is a variable or a member, we generate a primitive ARRAY_REF.
1388 This avoids forcing the array out of registers, and can work on
1389 arrays that are not lvalues (for example, members of structures returned
1390 by functions). */
1392 tree
1393 build_array_ref (array, index)
1394 tree array, index;
1396 if (index == 0)
1398 error ("subscript missing in array reference");
1399 return error_mark_node;
1402 if (TREE_TYPE (array) == error_mark_node
1403 || TREE_TYPE (index) == error_mark_node)
1404 return error_mark_node;
1406 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1407 && TREE_CODE (array) != INDIRECT_REF)
1409 tree rval, type;
1411 /* Subscripting with type char is likely to lose
1412 on a machine where chars are signed.
1413 So warn on any machine, but optionally.
1414 Don't warn for unsigned char since that type is safe.
1415 Don't warn for signed char because anyone who uses that
1416 must have done so deliberately. */
1417 if (warn_char_subscripts
1418 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1419 warning ("array subscript has type `char'");
1421 /* Apply default promotions *after* noticing character types. */
1422 index = default_conversion (index);
1424 /* Require integer *after* promotion, for sake of enums. */
1425 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1427 error ("array subscript is not an integer");
1428 return error_mark_node;
1431 /* An array that is indexed by a non-constant
1432 cannot be stored in a register; we must be able to do
1433 address arithmetic on its address.
1434 Likewise an array of elements of variable size. */
1435 if (TREE_CODE (index) != INTEGER_CST
1436 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1437 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1439 if (mark_addressable (array) == 0)
1440 return error_mark_node;
1442 /* An array that is indexed by a constant value which is not within
1443 the array bounds cannot be stored in a register either; because we
1444 would get a crash in store_bit_field/extract_bit_field when trying
1445 to access a non-existent part of the register. */
1446 if (TREE_CODE (index) == INTEGER_CST
1447 && TYPE_VALUES (TREE_TYPE (array))
1448 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1450 if (mark_addressable (array) == 0)
1451 return error_mark_node;
1454 if (pedantic && !lvalue_p (array))
1456 if (DECL_REGISTER (array))
1457 pedwarn ("ANSI C forbids subscripting `register' array");
1458 else
1459 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1462 if (pedantic)
1464 tree foo = array;
1465 while (TREE_CODE (foo) == COMPONENT_REF)
1466 foo = TREE_OPERAND (foo, 0);
1467 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1468 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1471 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1472 rval = build (ARRAY_REF, type, array, index);
1473 /* Array ref is const/volatile if the array elements are
1474 or if the array is. */
1475 TREE_READONLY (rval)
1476 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1477 | TREE_READONLY (array));
1478 TREE_SIDE_EFFECTS (rval)
1479 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1480 | TREE_SIDE_EFFECTS (array));
1481 TREE_THIS_VOLATILE (rval)
1482 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1483 /* This was added by rms on 16 Nov 91.
1484 It fixes vol struct foo *a; a->elts[1]
1485 in an inline function.
1486 Hope it doesn't break something else. */
1487 | TREE_THIS_VOLATILE (array));
1488 return require_complete_type (fold (rval));
1492 tree ar = default_conversion (array);
1493 tree ind = default_conversion (index);
1495 /* Do the same warning check as above, but only on the part that's
1496 syntactically the index and only if it is also semantically
1497 the index. */
1498 if (warn_char_subscripts
1499 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1500 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1501 warning ("subscript has type `char'");
1503 /* Put the integer in IND to simplify error checking. */
1504 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1506 tree temp = ar;
1507 ar = ind;
1508 ind = temp;
1511 if (ar == error_mark_node)
1512 return ar;
1514 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1515 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1517 error ("subscripted value is neither array nor pointer");
1518 return error_mark_node;
1520 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1522 error ("array subscript is not an integer");
1523 return error_mark_node;
1526 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1527 "array indexing");
1531 /* Build a function call to function FUNCTION with parameters PARAMS.
1532 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1533 TREE_VALUE of each node is a parameter-expression.
1534 FUNCTION's data type may be a function type or a pointer-to-function. */
1536 tree
1537 build_function_call (function, params)
1538 tree function, params;
1540 register tree fntype, fundecl = 0;
1541 register tree coerced_params;
1542 tree name = NULL_TREE, assembler_name = NULL_TREE;
1544 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1545 STRIP_TYPE_NOPS (function);
1547 /* Convert anything with function type to a pointer-to-function. */
1548 if (TREE_CODE (function) == FUNCTION_DECL)
1550 name = DECL_NAME (function);
1551 assembler_name = DECL_ASSEMBLER_NAME (function);
1553 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1554 (because calling an inline function does not mean the function
1555 needs to be separately compiled). */
1556 fntype = build_type_variant (TREE_TYPE (function),
1557 TREE_READONLY (function),
1558 TREE_THIS_VOLATILE (function));
1559 fundecl = function;
1560 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1562 else
1563 function = default_conversion (function);
1565 fntype = TREE_TYPE (function);
1567 if (TREE_CODE (fntype) == ERROR_MARK)
1568 return error_mark_node;
1570 if (!(TREE_CODE (fntype) == POINTER_TYPE
1571 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1573 error ("called object is not a function");
1574 return error_mark_node;
1577 /* fntype now gets the type of function pointed to. */
1578 fntype = TREE_TYPE (fntype);
1580 /* Convert the parameters to the types declared in the
1581 function prototype, or apply default promotions. */
1583 coerced_params
1584 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1586 /* Check for errors in format strings. */
1588 if (warn_format && (name || assembler_name))
1589 check_function_format (name, assembler_name, coerced_params);
1591 /* Recognize certain built-in functions so we can make tree-codes
1592 other than CALL_EXPR. We do this when it enables fold-const.c
1593 to do something useful. */
1595 if (TREE_CODE (function) == ADDR_EXPR
1596 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1597 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1598 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1600 case BUILT_IN_ABS:
1601 case BUILT_IN_LABS:
1602 case BUILT_IN_FABS:
1603 if (coerced_params == 0)
1604 return integer_zero_node;
1605 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1606 default:
1607 break;
1611 register tree result
1612 = build (CALL_EXPR, TREE_TYPE (fntype),
1613 function, coerced_params, NULL_TREE);
1615 TREE_SIDE_EFFECTS (result) = 1;
1616 if (TREE_TYPE (result) == void_type_node)
1617 return result;
1618 return require_complete_type (result);
1622 /* Convert the argument expressions in the list VALUES
1623 to the types in the list TYPELIST. The result is a list of converted
1624 argument expressions.
1626 If TYPELIST is exhausted, or when an element has NULL as its type,
1627 perform the default conversions.
1629 PARMLIST is the chain of parm decls for the function being called.
1630 It may be 0, if that info is not available.
1631 It is used only for generating error messages.
1633 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1635 This is also where warnings about wrong number of args are generated.
1637 Both VALUES and the returned value are chains of TREE_LIST nodes
1638 with the elements of the list in the TREE_VALUE slots of those nodes. */
1640 static tree
1641 convert_arguments (typelist, values, name, fundecl)
1642 tree typelist, values, name, fundecl;
1644 register tree typetail, valtail;
1645 register tree result = NULL;
1646 int parmnum;
1648 /* Scan the given expressions and types, producing individual
1649 converted arguments and pushing them on RESULT in reverse order. */
1651 for (valtail = values, typetail = typelist, parmnum = 0;
1652 valtail;
1653 valtail = TREE_CHAIN (valtail), parmnum++)
1655 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1656 register tree val = TREE_VALUE (valtail);
1658 if (type == void_type_node)
1660 if (name)
1661 error ("too many arguments to function `%s'",
1662 IDENTIFIER_POINTER (name));
1663 else
1664 error ("too many arguments to function");
1665 break;
1668 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1669 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1670 to convert automatically to a pointer. */
1671 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1672 val = TREE_OPERAND (val, 0);
1674 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1675 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1676 val = default_conversion (val);
1678 val = require_complete_type (val);
1680 if (type != 0)
1682 /* Formal parm type is specified by a function prototype. */
1683 tree parmval;
1685 if (TYPE_SIZE (type) == 0)
1687 error ("type of formal parameter %d is incomplete", parmnum + 1);
1688 parmval = val;
1690 else
1692 /* Optionally warn about conversions that
1693 differ from the default conversions. */
1694 if (warn_conversion)
1696 int formal_prec = TYPE_PRECISION (type);
1698 if (INTEGRAL_TYPE_P (type)
1699 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1700 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1701 else if (TREE_CODE (type) == COMPLEX_TYPE
1702 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1703 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1704 else if (TREE_CODE (type) == REAL_TYPE
1705 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1706 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1707 else if (TREE_CODE (type) == REAL_TYPE
1708 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1709 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1710 /* ??? At some point, messages should be written about
1711 conversions between complex types, but that's too messy
1712 to do now. */
1713 else if (TREE_CODE (type) == REAL_TYPE
1714 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1716 /* Warn if any argument is passed as `float',
1717 since without a prototype it would be `double'. */
1718 if (formal_prec == TYPE_PRECISION (float_type_node))
1719 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1721 /* Detect integer changing in width or signedness. */
1722 else if (INTEGRAL_TYPE_P (type)
1723 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1725 tree would_have_been = default_conversion (val);
1726 tree type1 = TREE_TYPE (would_have_been);
1728 if (TREE_CODE (type) == ENUMERAL_TYPE
1729 && type == TREE_TYPE (val))
1730 /* No warning if function asks for enum
1731 and the actual arg is that enum type. */
1733 else if (formal_prec != TYPE_PRECISION (type1))
1734 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1735 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1737 /* Don't complain if the formal parameter type
1738 is an enum, because we can't tell now whether
1739 the value was an enum--even the same enum. */
1740 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1742 else if (TREE_CODE (val) == INTEGER_CST
1743 && int_fits_type_p (val, type))
1744 /* Change in signedness doesn't matter
1745 if a constant value is unaffected. */
1747 /* Likewise for a constant in a NOP_EXPR. */
1748 else if (TREE_CODE (val) == NOP_EXPR
1749 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1750 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1752 #if 0 /* We never get such tree structure here. */
1753 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1754 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1755 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1756 /* Change in signedness doesn't matter
1757 if an enum value is unaffected. */
1759 #endif
1760 /* If the value is extended from a narrower
1761 unsigned type, it doesn't matter whether we
1762 pass it as signed or unsigned; the value
1763 certainly is the same either way. */
1764 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1765 && TREE_UNSIGNED (TREE_TYPE (val)))
1767 else if (TREE_UNSIGNED (type))
1768 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1769 else
1770 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1774 parmval = convert_for_assignment (type, val,
1775 (char *) 0, /* arg passing */
1776 fundecl, name, parmnum + 1);
1778 #ifdef PROMOTE_PROTOTYPES
1779 if ((TREE_CODE (type) == INTEGER_TYPE
1780 || TREE_CODE (type) == ENUMERAL_TYPE)
1781 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1782 parmval = default_conversion (parmval);
1783 #endif
1785 result = tree_cons (NULL_TREE, parmval, result);
1787 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1788 && (TYPE_PRECISION (TREE_TYPE (val))
1789 < TYPE_PRECISION (double_type_node)))
1790 /* Convert `float' to `double'. */
1791 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1792 else
1793 /* Convert `short' and `char' to full-size `int'. */
1794 result = tree_cons (NULL_TREE, default_conversion (val), result);
1796 if (typetail)
1797 typetail = TREE_CHAIN (typetail);
1800 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1802 if (name)
1803 error ("too few arguments to function `%s'",
1804 IDENTIFIER_POINTER (name));
1805 else
1806 error ("too few arguments to function");
1809 return nreverse (result);
1812 /* This is the entry point used by the parser
1813 for binary operators in the input.
1814 In addition to constructing the expression,
1815 we check for operands that were written with other binary operators
1816 in a way that is likely to confuse the user. */
1818 tree
1819 parser_build_binary_op (code, arg1, arg2)
1820 enum tree_code code;
1821 tree arg1, arg2;
1823 tree result = build_binary_op (code, arg1, arg2, 1);
1825 char class;
1826 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1827 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1828 enum tree_code code1 = ERROR_MARK;
1829 enum tree_code code2 = ERROR_MARK;
1831 if (class1 == 'e' || class1 == '1'
1832 || class1 == '2' || class1 == '<')
1833 code1 = C_EXP_ORIGINAL_CODE (arg1);
1834 if (class2 == 'e' || class2 == '1'
1835 || class2 == '2' || class2 == '<')
1836 code2 = C_EXP_ORIGINAL_CODE (arg2);
1838 /* Check for cases such as x+y<<z which users are likely
1839 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1840 is cleared to prevent these warnings. */
1841 if (warn_parentheses)
1843 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1845 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1846 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1847 warning ("suggest parentheses around + or - inside shift");
1850 if (code == TRUTH_ORIF_EXPR)
1852 if (code1 == TRUTH_ANDIF_EXPR
1853 || code2 == TRUTH_ANDIF_EXPR)
1854 warning ("suggest parentheses around && within ||");
1857 if (code == BIT_IOR_EXPR)
1859 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1860 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1861 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1862 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1863 warning ("suggest parentheses around arithmetic in operand of |");
1864 /* Check cases like x|y==z */
1865 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1866 warning ("suggest parentheses around comparison in operand of |");
1869 if (code == BIT_XOR_EXPR)
1871 if (code1 == BIT_AND_EXPR
1872 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1873 || code2 == BIT_AND_EXPR
1874 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1875 warning ("suggest parentheses around arithmetic in operand of ^");
1876 /* Check cases like x^y==z */
1877 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1878 warning ("suggest parentheses around comparison in operand of ^");
1881 if (code == BIT_AND_EXPR)
1883 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1884 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1885 warning ("suggest parentheses around + or - in operand of &");
1886 /* Check cases like x&y==z */
1887 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1888 warning ("suggest parentheses around comparison in operand of &");
1892 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1893 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1894 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1895 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1897 unsigned_conversion_warning (result, arg1);
1898 unsigned_conversion_warning (result, arg2);
1899 overflow_warning (result);
1901 class = TREE_CODE_CLASS (TREE_CODE (result));
1903 /* Record the code that was specified in the source,
1904 for the sake of warnings about confusing nesting. */
1905 if (class == 'e' || class == '1'
1906 || class == '2' || class == '<')
1907 C_SET_EXP_ORIGINAL_CODE (result, code);
1908 else
1910 int flag = TREE_CONSTANT (result);
1911 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1912 so that convert_for_assignment wouldn't strip it.
1913 That way, we got warnings for things like p = (1 - 1).
1914 But it turns out we should not get those warnings. */
1915 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1916 C_SET_EXP_ORIGINAL_CODE (result, code);
1917 TREE_CONSTANT (result) = flag;
1920 return result;
1923 /* Build a binary-operation expression without default conversions.
1924 CODE is the kind of expression to build.
1925 This function differs from `build' in several ways:
1926 the data type of the result is computed and recorded in it,
1927 warnings are generated if arg data types are invalid,
1928 special handling for addition and subtraction of pointers is known,
1929 and some optimization is done (operations on narrow ints
1930 are done in the narrower type when that gives the same result).
1931 Constant folding is also done before the result is returned.
1933 Note that the operands will never have enumeral types, or function
1934 or array types, because either they will have the default conversions
1935 performed or they have both just been converted to some other type in which
1936 the arithmetic is to be done. */
1938 tree
1939 build_binary_op (code, orig_op0, orig_op1, convert_p)
1940 enum tree_code code;
1941 tree orig_op0, orig_op1;
1942 int convert_p;
1944 tree type0, type1;
1945 register enum tree_code code0, code1;
1946 tree op0, op1;
1948 /* Expression code to give to the expression when it is built.
1949 Normally this is CODE, which is what the caller asked for,
1950 but in some special cases we change it. */
1951 register enum tree_code resultcode = code;
1953 /* Data type in which the computation is to be performed.
1954 In the simplest cases this is the common type of the arguments. */
1955 register tree result_type = NULL;
1957 /* Nonzero means operands have already been type-converted
1958 in whatever way is necessary.
1959 Zero means they need to be converted to RESULT_TYPE. */
1960 int converted = 0;
1962 /* Nonzero means create the expression with this type, rather than
1963 RESULT_TYPE. */
1964 tree build_type = 0;
1966 /* Nonzero means after finally constructing the expression
1967 convert it to this type. */
1968 tree final_type = 0;
1970 /* Nonzero if this is an operation like MIN or MAX which can
1971 safely be computed in short if both args are promoted shorts.
1972 Also implies COMMON.
1973 -1 indicates a bitwise operation; this makes a difference
1974 in the exact conditions for when it is safe to do the operation
1975 in a narrower mode. */
1976 int shorten = 0;
1978 /* Nonzero if this is a comparison operation;
1979 if both args are promoted shorts, compare the original shorts.
1980 Also implies COMMON. */
1981 int short_compare = 0;
1983 /* Nonzero if this is a right-shift operation, which can be computed on the
1984 original short and then promoted if the operand is a promoted short. */
1985 int short_shift = 0;
1987 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1988 int common = 0;
1990 if (convert_p)
1992 op0 = default_conversion (orig_op0);
1993 op1 = default_conversion (orig_op1);
1995 else
1997 op0 = orig_op0;
1998 op1 = orig_op1;
2001 type0 = TREE_TYPE (op0);
2002 type1 = TREE_TYPE (op1);
2004 /* The expression codes of the data types of the arguments tell us
2005 whether the arguments are integers, floating, pointers, etc. */
2006 code0 = TREE_CODE (type0);
2007 code1 = TREE_CODE (type1);
2009 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2010 STRIP_TYPE_NOPS (op0);
2011 STRIP_TYPE_NOPS (op1);
2013 /* If an error was already reported for one of the arguments,
2014 avoid reporting another error. */
2016 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2017 return error_mark_node;
2019 switch (code)
2021 case PLUS_EXPR:
2022 /* Handle the pointer + int case. */
2023 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2024 return pointer_int_sum (PLUS_EXPR, op0, op1);
2025 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2026 return pointer_int_sum (PLUS_EXPR, op1, op0);
2027 else
2028 common = 1;
2029 break;
2031 case MINUS_EXPR:
2032 /* Subtraction of two similar pointers.
2033 We must subtract them as integers, then divide by object size. */
2034 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2035 && comp_target_types (type0, type1))
2036 return pointer_diff (op0, op1);
2037 /* Handle pointer minus int. Just like pointer plus int. */
2038 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2039 return pointer_int_sum (MINUS_EXPR, op0, op1);
2040 else
2041 common = 1;
2042 break;
2044 case MULT_EXPR:
2045 common = 1;
2046 break;
2048 case TRUNC_DIV_EXPR:
2049 case CEIL_DIV_EXPR:
2050 case FLOOR_DIV_EXPR:
2051 case ROUND_DIV_EXPR:
2052 case EXACT_DIV_EXPR:
2053 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2054 || code0 == COMPLEX_TYPE)
2055 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2056 || code1 == COMPLEX_TYPE))
2058 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2059 resultcode = RDIV_EXPR;
2060 else
2062 /* Although it would be tempting to shorten always here, that
2063 loses on some targets, since the modulo instruction is
2064 undefined if the quotient can't be represented in the
2065 computation mode. We shorten only if unsigned or if
2066 dividing by something we know != -1. */
2067 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2068 || (TREE_CODE (op1) == INTEGER_CST
2069 && (TREE_INT_CST_LOW (op1) != -1
2070 || TREE_INT_CST_HIGH (op1) != -1)));
2072 common = 1;
2074 break;
2076 case BIT_AND_EXPR:
2077 case BIT_ANDTC_EXPR:
2078 case BIT_IOR_EXPR:
2079 case BIT_XOR_EXPR:
2080 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2081 shorten = -1;
2082 /* If one operand is a constant, and the other is a short type
2083 that has been converted to an int,
2084 really do the work in the short type and then convert the
2085 result to int. If we are lucky, the constant will be 0 or 1
2086 in the short type, making the entire operation go away. */
2087 if (TREE_CODE (op0) == INTEGER_CST
2088 && TREE_CODE (op1) == NOP_EXPR
2089 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2090 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2092 final_type = result_type;
2093 op1 = TREE_OPERAND (op1, 0);
2094 result_type = TREE_TYPE (op1);
2096 if (TREE_CODE (op1) == INTEGER_CST
2097 && TREE_CODE (op0) == NOP_EXPR
2098 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2099 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2101 final_type = result_type;
2102 op0 = TREE_OPERAND (op0, 0);
2103 result_type = TREE_TYPE (op0);
2105 break;
2107 case TRUNC_MOD_EXPR:
2108 case FLOOR_MOD_EXPR:
2109 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2111 /* Although it would be tempting to shorten always here, that loses
2112 on some targets, since the modulo instruction is undefined if the
2113 quotient can't be represented in the computation mode. We shorten
2114 only if unsigned or if dividing by something we know != -1. */
2115 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2116 || (TREE_CODE (op1) == INTEGER_CST
2117 && (TREE_INT_CST_LOW (op1) != -1
2118 || TREE_INT_CST_HIGH (op1) != -1)));
2119 common = 1;
2121 break;
2123 case TRUTH_ANDIF_EXPR:
2124 case TRUTH_ORIF_EXPR:
2125 case TRUTH_AND_EXPR:
2126 case TRUTH_OR_EXPR:
2127 case TRUTH_XOR_EXPR:
2128 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2129 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2130 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2131 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2133 /* Result of these operations is always an int,
2134 but that does not mean the operands should be
2135 converted to ints! */
2136 result_type = integer_type_node;
2137 op0 = truthvalue_conversion (op0);
2138 op1 = truthvalue_conversion (op1);
2139 converted = 1;
2141 break;
2143 /* Shift operations: result has same type as first operand;
2144 always convert second operand to int.
2145 Also set SHORT_SHIFT if shifting rightward. */
2147 case RSHIFT_EXPR:
2148 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2150 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2152 if (tree_int_cst_sgn (op1) < 0)
2153 warning ("right shift count is negative");
2154 else
2156 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2157 short_shift = 1;
2158 if (TREE_INT_CST_HIGH (op1) != 0
2159 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2160 >= TYPE_PRECISION (type0)))
2161 warning ("right shift count >= width of type");
2164 /* Use the type of the value to be shifted.
2165 This is what most traditional C compilers do. */
2166 result_type = type0;
2167 /* Unless traditional, convert the shift-count to an integer,
2168 regardless of size of value being shifted. */
2169 if (! flag_traditional)
2171 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2172 op1 = convert (integer_type_node, op1);
2173 /* Avoid converting op1 to result_type later. */
2174 converted = 1;
2177 break;
2179 case LSHIFT_EXPR:
2180 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2182 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2184 if (tree_int_cst_sgn (op1) < 0)
2185 warning ("left shift count is negative");
2186 else if (TREE_INT_CST_HIGH (op1) != 0
2187 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2188 >= TYPE_PRECISION (type0)))
2189 warning ("left shift count >= width of type");
2191 /* Use the type of the value to be shifted.
2192 This is what most traditional C compilers do. */
2193 result_type = type0;
2194 /* Unless traditional, convert the shift-count to an integer,
2195 regardless of size of value being shifted. */
2196 if (! flag_traditional)
2198 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2199 op1 = convert (integer_type_node, op1);
2200 /* Avoid converting op1 to result_type later. */
2201 converted = 1;
2204 break;
2206 case RROTATE_EXPR:
2207 case LROTATE_EXPR:
2208 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2210 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2212 if (tree_int_cst_sgn (op1) < 0)
2213 warning ("shift count is negative");
2214 else if (TREE_INT_CST_HIGH (op1) != 0
2215 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2216 >= TYPE_PRECISION (type0)))
2217 warning ("shift count >= width of type");
2219 /* Use the type of the value to be shifted.
2220 This is what most traditional C compilers do. */
2221 result_type = type0;
2222 /* Unless traditional, convert the shift-count to an integer,
2223 regardless of size of value being shifted. */
2224 if (! flag_traditional)
2226 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2227 op1 = convert (integer_type_node, op1);
2228 /* Avoid converting op1 to result_type later. */
2229 converted = 1;
2232 break;
2234 case EQ_EXPR:
2235 case NE_EXPR:
2236 /* Result of comparison is always int,
2237 but don't convert the args to int! */
2238 build_type = integer_type_node;
2239 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2240 || code0 == COMPLEX_TYPE)
2241 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2242 || code1 == COMPLEX_TYPE))
2243 short_compare = 1;
2244 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2246 register tree tt0 = TREE_TYPE (type0);
2247 register tree tt1 = TREE_TYPE (type1);
2248 /* Anything compares with void *. void * compares with anything.
2249 Otherwise, the targets must be compatible
2250 and both must be object or both incomplete. */
2251 if (comp_target_types (type0, type1))
2252 result_type = common_type (type0, type1);
2253 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2255 /* op0 != orig_op0 detects the case of something
2256 whose value is 0 but which isn't a valid null ptr const. */
2257 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2258 && TREE_CODE (tt1) == FUNCTION_TYPE)
2259 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2261 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2263 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2264 && TREE_CODE (tt0) == FUNCTION_TYPE)
2265 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2267 else
2268 pedwarn ("comparison of distinct pointer types lacks a cast");
2270 if (result_type == NULL_TREE)
2271 result_type = ptr_type_node;
2273 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2274 && integer_zerop (op1))
2275 result_type = type0;
2276 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2277 && integer_zerop (op0))
2278 result_type = type1;
2279 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2281 result_type = type0;
2282 if (! flag_traditional)
2283 pedwarn ("comparison between pointer and integer");
2285 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2287 result_type = type1;
2288 if (! flag_traditional)
2289 pedwarn ("comparison between pointer and integer");
2291 break;
2293 case MAX_EXPR:
2294 case MIN_EXPR:
2295 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2296 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2297 shorten = 1;
2298 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2300 if (comp_target_types (type0, type1))
2302 result_type = common_type (type0, type1);
2303 if (pedantic
2304 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2305 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2307 else
2309 result_type = ptr_type_node;
2310 pedwarn ("comparison of distinct pointer types lacks a cast");
2313 break;
2315 case LE_EXPR:
2316 case GE_EXPR:
2317 case LT_EXPR:
2318 case GT_EXPR:
2319 build_type = integer_type_node;
2320 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2321 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2322 short_compare = 1;
2323 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2325 if (comp_target_types (type0, type1))
2327 result_type = common_type (type0, type1);
2328 if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2329 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2330 pedwarn ("comparison of complete and incomplete pointers");
2331 else if (pedantic
2332 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2333 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2335 else
2337 result_type = ptr_type_node;
2338 pedwarn ("comparison of distinct pointer types lacks a cast");
2341 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2342 && integer_zerop (op1))
2344 result_type = type0;
2345 if (pedantic || extra_warnings)
2346 pedwarn ("ordered comparison of pointer with integer zero");
2348 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2349 && integer_zerop (op0))
2351 result_type = type1;
2352 if (pedantic)
2353 pedwarn ("ordered comparison of pointer with integer zero");
2355 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2357 result_type = type0;
2358 if (! flag_traditional)
2359 pedwarn ("comparison between pointer and integer");
2361 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2363 result_type = type1;
2364 if (! flag_traditional)
2365 pedwarn ("comparison between pointer and integer");
2367 break;
2369 default:
2370 break;
2373 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2375 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2377 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2379 if (shorten || common || short_compare)
2380 result_type = common_type (type0, type1);
2382 /* For certain operations (which identify themselves by shorten != 0)
2383 if both args were extended from the same smaller type,
2384 do the arithmetic in that type and then extend.
2386 shorten !=0 and !=1 indicates a bitwise operation.
2387 For them, this optimization is safe only if
2388 both args are zero-extended or both are sign-extended.
2389 Otherwise, we might change the result.
2390 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2391 but calculated in (unsigned short) it would be (unsigned short)-1. */
2393 if (shorten && none_complex)
2395 int unsigned0, unsigned1;
2396 tree arg0 = get_narrower (op0, &unsigned0);
2397 tree arg1 = get_narrower (op1, &unsigned1);
2398 /* UNS is 1 if the operation to be done is an unsigned one. */
2399 int uns = TREE_UNSIGNED (result_type);
2400 tree type;
2402 final_type = result_type;
2404 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2405 but it *requires* conversion to FINAL_TYPE. */
2407 if ((TYPE_PRECISION (TREE_TYPE (op0))
2408 == TYPE_PRECISION (TREE_TYPE (arg0)))
2409 && TREE_TYPE (op0) != final_type)
2410 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2411 if ((TYPE_PRECISION (TREE_TYPE (op1))
2412 == TYPE_PRECISION (TREE_TYPE (arg1)))
2413 && TREE_TYPE (op1) != final_type)
2414 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2416 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2418 /* For bitwise operations, signedness of nominal type
2419 does not matter. Consider only how operands were extended. */
2420 if (shorten == -1)
2421 uns = unsigned0;
2423 /* Note that in all three cases below we refrain from optimizing
2424 an unsigned operation on sign-extended args.
2425 That would not be valid. */
2427 /* Both args variable: if both extended in same way
2428 from same width, do it in that width.
2429 Do it unsigned if args were zero-extended. */
2430 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2431 < TYPE_PRECISION (result_type))
2432 && (TYPE_PRECISION (TREE_TYPE (arg1))
2433 == TYPE_PRECISION (TREE_TYPE (arg0)))
2434 && unsigned0 == unsigned1
2435 && (unsigned0 || !uns))
2436 result_type
2437 = signed_or_unsigned_type (unsigned0,
2438 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2439 else if (TREE_CODE (arg0) == INTEGER_CST
2440 && (unsigned1 || !uns)
2441 && (TYPE_PRECISION (TREE_TYPE (arg1))
2442 < TYPE_PRECISION (result_type))
2443 && (type = signed_or_unsigned_type (unsigned1,
2444 TREE_TYPE (arg1)),
2445 int_fits_type_p (arg0, type)))
2446 result_type = type;
2447 else if (TREE_CODE (arg1) == INTEGER_CST
2448 && (unsigned0 || !uns)
2449 && (TYPE_PRECISION (TREE_TYPE (arg0))
2450 < TYPE_PRECISION (result_type))
2451 && (type = signed_or_unsigned_type (unsigned0,
2452 TREE_TYPE (arg0)),
2453 int_fits_type_p (arg1, type)))
2454 result_type = type;
2457 /* Shifts can be shortened if shifting right. */
2459 if (short_shift)
2461 int unsigned_arg;
2462 tree arg0 = get_narrower (op0, &unsigned_arg);
2464 final_type = result_type;
2466 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2467 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2469 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2470 /* We can shorten only if the shift count is less than the
2471 number of bits in the smaller type size. */
2472 && TREE_INT_CST_HIGH (op1) == 0
2473 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
2474 /* If arg is sign-extended and then unsigned-shifted,
2475 we can simulate this with a signed shift in arg's type
2476 only if the extended result is at least twice as wide
2477 as the arg. Otherwise, the shift could use up all the
2478 ones made by sign-extension and bring in zeros.
2479 We can't optimize that case at all, but in most machines
2480 it never happens because available widths are 2**N. */
2481 && (!TREE_UNSIGNED (final_type)
2482 || unsigned_arg
2483 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2485 /* Do an unsigned shift if the operand was zero-extended. */
2486 result_type
2487 = signed_or_unsigned_type (unsigned_arg,
2488 TREE_TYPE (arg0));
2489 /* Convert value-to-be-shifted to that type. */
2490 if (TREE_TYPE (op0) != result_type)
2491 op0 = convert (result_type, op0);
2492 converted = 1;
2496 /* Comparison operations are shortened too but differently.
2497 They identify themselves by setting short_compare = 1. */
2499 if (short_compare)
2501 /* Don't write &op0, etc., because that would prevent op0
2502 from being kept in a register.
2503 Instead, make copies of the our local variables and
2504 pass the copies by reference, then copy them back afterward. */
2505 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2506 enum tree_code xresultcode = resultcode;
2507 tree val
2508 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2509 if (val != 0)
2510 return val;
2511 op0 = xop0, op1 = xop1;
2512 converted = 1;
2513 resultcode = xresultcode;
2515 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2516 && skip_evaluation == 0)
2518 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2519 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2521 int unsignedp0, unsignedp1;
2522 tree primop0 = get_narrower (op0, &unsignedp0);
2523 tree primop1 = get_narrower (op1, &unsignedp1);
2525 /* Avoid spurious warnings for comparison with enumerators. */
2527 xop0 = orig_op0;
2528 xop1 = orig_op1;
2529 STRIP_TYPE_NOPS (xop0);
2530 STRIP_TYPE_NOPS (xop1);
2532 /* Give warnings for comparisons between signed and unsigned
2533 quantities that may fail. */
2534 /* Do the checking based on the original operand trees, so that
2535 casts will be considered, but default promotions won't be. */
2537 /* Do not warn if the comparison is being done in a signed type,
2538 since the signed type will only be chosen if it can represent
2539 all the values of the unsigned type. */
2540 if (! TREE_UNSIGNED (result_type))
2541 /* OK */;
2542 /* Do not warn if both operands are unsigned. */
2543 else if (op0_signed == op1_signed)
2544 /* OK */;
2545 /* Do not warn if the signed quantity is an unsuffixed
2546 integer literal (or some static constant expression
2547 involving such literals) and it is non-negative. */
2548 else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST
2549 && tree_int_cst_sgn (xop0) >= 0)
2550 || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
2551 && tree_int_cst_sgn (xop1) >= 0))
2552 /* OK */;
2553 /* Do not warn if the comparison is an equality operation,
2554 the unsigned quantity is an integral constant and it does
2555 not use the most significant bit of result_type. */
2556 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
2557 && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST
2558 && int_fits_type_p (xop1, signed_type (result_type)))
2559 || (op1_signed && TREE_CODE (xop0) == INTEGER_CST
2560 && int_fits_type_p (xop0, signed_type (result_type)))))
2561 /* OK */;
2562 else
2563 warning ("comparison between signed and unsigned");
2565 /* Warn if two unsigned values are being compared in a size
2566 larger than their original size, and one (and only one) is the
2567 result of a `~' operator. This comparison will always fail.
2569 Also warn if one operand is a constant, and the constant
2570 does not have all bits set that are set in the ~ operand
2571 when it is extended. */
2573 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2574 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2576 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2577 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2578 &unsignedp0);
2579 else
2580 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2581 &unsignedp1);
2583 if (TREE_CODE (primop0) == INTEGER_CST
2584 || TREE_CODE (primop1) == INTEGER_CST)
2586 tree primop;
2587 long constant, mask;
2588 int unsignedp, bits;
2590 if (TREE_CODE (primop0) == INTEGER_CST)
2592 primop = primop1;
2593 unsignedp = unsignedp1;
2594 constant = TREE_INT_CST_LOW (primop0);
2596 else
2598 primop = primop0;
2599 unsignedp = unsignedp0;
2600 constant = TREE_INT_CST_LOW (primop1);
2603 bits = TYPE_PRECISION (TREE_TYPE (primop));
2604 if (bits < TYPE_PRECISION (result_type)
2605 && bits < HOST_BITS_PER_LONG && unsignedp)
2607 mask = (~0L) << bits;
2608 if ((mask & constant) != mask)
2609 warning ("comparison of promoted ~unsigned with constant");
2612 else if (unsignedp0 && unsignedp1
2613 && (TYPE_PRECISION (TREE_TYPE (primop0))
2614 < TYPE_PRECISION (result_type))
2615 && (TYPE_PRECISION (TREE_TYPE (primop1))
2616 < TYPE_PRECISION (result_type)))
2617 warning ("comparison of promoted ~unsigned with unsigned");
2623 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2624 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2625 Then the expression will be built.
2626 It will be given type FINAL_TYPE if that is nonzero;
2627 otherwise, it will be given type RESULT_TYPE. */
2629 if (!result_type)
2631 binary_op_error (code);
2632 return error_mark_node;
2635 if (! converted)
2637 if (TREE_TYPE (op0) != result_type)
2638 op0 = convert (result_type, op0);
2639 if (TREE_TYPE (op1) != result_type)
2640 op1 = convert (result_type, op1);
2643 if (build_type == NULL_TREE)
2644 build_type = result_type;
2647 register tree result = build (resultcode, build_type, op0, op1);
2648 register tree folded;
2650 folded = fold (result);
2651 if (folded == result)
2652 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2653 if (final_type != 0)
2654 return convert (final_type, folded);
2655 return folded;
2659 /* Return a tree for the sum or difference (RESULTCODE says which)
2660 of pointer PTROP and integer INTOP. */
2662 static tree
2663 pointer_int_sum (resultcode, ptrop, intop)
2664 enum tree_code resultcode;
2665 register tree ptrop, intop;
2667 tree size_exp;
2669 register tree result;
2670 register tree folded;
2672 /* The result is a pointer of the same type that is being added. */
2674 register tree result_type = TREE_TYPE (ptrop);
2676 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2678 if (pedantic || warn_pointer_arith)
2679 pedwarn ("pointer of type `void *' used in arithmetic");
2680 size_exp = integer_one_node;
2682 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2684 if (pedantic || warn_pointer_arith)
2685 pedwarn ("pointer to a function used in arithmetic");
2686 size_exp = integer_one_node;
2688 else
2689 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2691 /* If what we are about to multiply by the size of the elements
2692 contains a constant term, apply distributive law
2693 and multiply that constant term separately.
2694 This helps produce common subexpressions. */
2696 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2697 && ! TREE_CONSTANT (intop)
2698 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2699 && TREE_CONSTANT (size_exp)
2700 /* If the constant comes from pointer subtraction,
2701 skip this optimization--it would cause an error. */
2702 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2703 /* If the constant is unsigned, and smaller than the pointer size,
2704 then we must skip this optimization. This is because it could cause
2705 an overflow error if the constant is negative but INTOP is not. */
2706 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2707 || (TYPE_PRECISION (TREE_TYPE (intop))
2708 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2710 enum tree_code subcode = resultcode;
2711 tree int_type = TREE_TYPE (intop);
2712 if (TREE_CODE (intop) == MINUS_EXPR)
2713 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2714 /* Convert both subexpression types to the type of intop,
2715 because weird cases involving pointer arithmetic
2716 can result in a sum or difference with different type args. */
2717 ptrop = build_binary_op (subcode, ptrop,
2718 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2719 intop = convert (int_type, TREE_OPERAND (intop, 0));
2722 /* Convert the integer argument to a type the same size as sizetype
2723 so the multiply won't overflow spuriously. */
2725 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2726 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2727 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2728 TREE_UNSIGNED (sizetype)), intop);
2730 /* Replace the integer argument with a suitable product by the object size.
2731 Do this multiplication as signed, then convert to the appropriate
2732 pointer type (actually unsigned integral). */
2734 intop = convert (result_type,
2735 build_binary_op (MULT_EXPR, intop,
2736 convert (TREE_TYPE (intop), size_exp), 1));
2738 /* Create the sum or difference. */
2740 result = build (resultcode, result_type, ptrop, intop);
2742 folded = fold (result);
2743 if (folded == result)
2744 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2745 return folded;
2748 /* Return a tree for the difference of pointers OP0 and OP1.
2749 The resulting tree has type int. */
2751 static tree
2752 pointer_diff (op0, op1)
2753 register tree op0, op1;
2755 register tree result, folded;
2756 tree restype = ptrdiff_type_node;
2758 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2760 if (pedantic || warn_pointer_arith)
2762 if (TREE_CODE (target_type) == VOID_TYPE)
2763 pedwarn ("pointer of type `void *' used in subtraction");
2764 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2765 pedwarn ("pointer to a function used in subtraction");
2768 /* First do the subtraction as integers;
2769 then drop through to build the divide operator.
2770 Do not do default conversions on the minus operator
2771 in case restype is a short type. */
2773 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2774 convert (restype, op1), 0);
2775 /* This generates an error if op1 is pointer to incomplete type. */
2776 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2777 error ("arithmetic on pointer to an incomplete type");
2779 /* This generates an error if op0 is pointer to incomplete type. */
2780 op1 = c_size_in_bytes (target_type);
2782 /* Divide by the size, in easiest possible way. */
2784 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2786 folded = fold (result);
2787 if (folded == result)
2788 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2789 return folded;
2792 /* Construct and perhaps optimize a tree representation
2793 for a unary operation. CODE, a tree_code, specifies the operation
2794 and XARG is the operand. NOCONVERT nonzero suppresses
2795 the default promotions (such as from short to int). */
2797 tree
2798 build_unary_op (code, xarg, noconvert)
2799 enum tree_code code;
2800 tree xarg;
2801 int noconvert;
2803 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2804 register tree arg = xarg;
2805 register tree argtype = 0;
2806 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2807 char *errstring = NULL;
2808 tree val;
2810 if (typecode == ERROR_MARK)
2811 return error_mark_node;
2812 if (typecode == ENUMERAL_TYPE)
2813 typecode = INTEGER_TYPE;
2815 switch (code)
2817 case CONVERT_EXPR:
2818 /* This is used for unary plus, because a CONVERT_EXPR
2819 is enough to prevent anybody from looking inside for
2820 associativity, but won't generate any code. */
2821 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2822 || typecode == COMPLEX_TYPE))
2823 errstring = "wrong type argument to unary plus";
2824 else if (!noconvert)
2825 arg = default_conversion (arg);
2826 break;
2828 case NEGATE_EXPR:
2829 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2830 || typecode == COMPLEX_TYPE))
2831 errstring = "wrong type argument to unary minus";
2832 else if (!noconvert)
2833 arg = default_conversion (arg);
2834 break;
2836 case BIT_NOT_EXPR:
2837 if (typecode == COMPLEX_TYPE)
2839 code = CONJ_EXPR;
2840 if (!noconvert)
2841 arg = default_conversion (arg);
2843 else if (typecode != INTEGER_TYPE)
2844 errstring = "wrong type argument to bit-complement";
2845 else if (!noconvert)
2846 arg = default_conversion (arg);
2847 break;
2849 case ABS_EXPR:
2850 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2851 || typecode == COMPLEX_TYPE))
2852 errstring = "wrong type argument to abs";
2853 else if (!noconvert)
2854 arg = default_conversion (arg);
2855 break;
2857 case CONJ_EXPR:
2858 /* Conjugating a real value is a no-op, but allow it anyway. */
2859 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2860 || typecode == COMPLEX_TYPE))
2861 errstring = "wrong type argument to conjugation";
2862 else if (!noconvert)
2863 arg = default_conversion (arg);
2864 break;
2866 case TRUTH_NOT_EXPR:
2867 if (typecode != INTEGER_TYPE
2868 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2869 && typecode != COMPLEX_TYPE
2870 /* These will convert to a pointer. */
2871 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2873 errstring = "wrong type argument to unary exclamation mark";
2874 break;
2876 arg = truthvalue_conversion (arg);
2877 return invert_truthvalue (arg);
2879 case NOP_EXPR:
2880 break;
2882 case REALPART_EXPR:
2883 if (TREE_CODE (arg) == COMPLEX_CST)
2884 return TREE_REALPART (arg);
2885 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2886 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2887 else
2888 return arg;
2890 case IMAGPART_EXPR:
2891 if (TREE_CODE (arg) == COMPLEX_CST)
2892 return TREE_IMAGPART (arg);
2893 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2894 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2895 else
2896 return convert (TREE_TYPE (arg), integer_zero_node);
2898 case PREINCREMENT_EXPR:
2899 case POSTINCREMENT_EXPR:
2900 case PREDECREMENT_EXPR:
2901 case POSTDECREMENT_EXPR:
2902 /* Handle complex lvalues (when permitted)
2903 by reduction to simpler cases. */
2905 val = unary_complex_lvalue (code, arg);
2906 if (val != 0)
2907 return val;
2909 /* Increment or decrement the real part of the value,
2910 and don't change the imaginary part. */
2911 if (typecode == COMPLEX_TYPE)
2913 tree real, imag;
2915 arg = stabilize_reference (arg);
2916 real = build_unary_op (REALPART_EXPR, arg, 1);
2917 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2918 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2919 build_unary_op (code, real, 1), imag);
2922 /* Report invalid types. */
2924 if (typecode != POINTER_TYPE
2925 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2927 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2928 errstring ="wrong type argument to increment";
2929 else
2930 errstring ="wrong type argument to decrement";
2931 break;
2935 register tree inc;
2936 tree result_type = TREE_TYPE (arg);
2938 arg = get_unwidened (arg, 0);
2939 argtype = TREE_TYPE (arg);
2941 /* Compute the increment. */
2943 if (typecode == POINTER_TYPE)
2945 /* If pointer target is an undefined struct,
2946 we just cannot know how to do the arithmetic. */
2947 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2948 error ("%s of pointer to unknown structure",
2949 ((code == PREINCREMENT_EXPR
2950 || code == POSTINCREMENT_EXPR)
2951 ? "increment" : "decrement"));
2952 else if ((pedantic || warn_pointer_arith)
2953 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2954 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2955 pedwarn ("wrong type argument to %s",
2956 ((code == PREINCREMENT_EXPR
2957 || code == POSTINCREMENT_EXPR)
2958 ? "increment" : "decrement"));
2959 inc = c_size_in_bytes (TREE_TYPE (result_type));
2961 else
2962 inc = integer_one_node;
2964 inc = convert (argtype, inc);
2966 /* Handle incrementing a cast-expression. */
2968 while (1)
2969 switch (TREE_CODE (arg))
2971 case NOP_EXPR:
2972 case CONVERT_EXPR:
2973 case FLOAT_EXPR:
2974 case FIX_TRUNC_EXPR:
2975 case FIX_FLOOR_EXPR:
2976 case FIX_ROUND_EXPR:
2977 case FIX_CEIL_EXPR:
2978 pedantic_lvalue_warning (CONVERT_EXPR);
2979 /* If the real type has the same machine representation
2980 as the type it is cast to, we can make better output
2981 by adding directly to the inside of the cast. */
2982 if ((TREE_CODE (TREE_TYPE (arg))
2983 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2984 && (TYPE_MODE (TREE_TYPE (arg))
2985 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2986 arg = TREE_OPERAND (arg, 0);
2987 else
2989 tree incremented, modify, value;
2990 arg = stabilize_reference (arg);
2991 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2992 value = arg;
2993 else
2994 value = save_expr (arg);
2995 incremented = build (((code == PREINCREMENT_EXPR
2996 || code == POSTINCREMENT_EXPR)
2997 ? PLUS_EXPR : MINUS_EXPR),
2998 argtype, value, inc);
2999 TREE_SIDE_EFFECTS (incremented) = 1;
3000 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3001 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3002 TREE_USED (value) = 1;
3003 return value;
3005 break;
3007 default:
3008 goto give_up;
3010 give_up:
3012 /* Complain about anything else that is not a true lvalue. */
3013 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3014 || code == POSTINCREMENT_EXPR)
3015 ? "increment" : "decrement")))
3016 return error_mark_node;
3018 /* Report a read-only lvalue. */
3019 if (TREE_READONLY (arg))
3020 readonly_warning (arg,
3021 ((code == PREINCREMENT_EXPR
3022 || code == POSTINCREMENT_EXPR)
3023 ? "increment" : "decrement"));
3025 val = build (code, TREE_TYPE (arg), arg, inc);
3026 TREE_SIDE_EFFECTS (val) = 1;
3027 val = convert (result_type, val);
3028 if (TREE_CODE (val) != code)
3029 TREE_NO_UNUSED_WARNING (val) = 1;
3030 return val;
3033 case ADDR_EXPR:
3034 /* Note that this operation never does default_conversion
3035 regardless of NOCONVERT. */
3037 /* Let &* cancel out to simplify resulting code. */
3038 if (TREE_CODE (arg) == INDIRECT_REF)
3040 /* Don't let this be an lvalue. */
3041 if (lvalue_p (TREE_OPERAND (arg, 0)))
3042 return non_lvalue (TREE_OPERAND (arg, 0));
3043 return TREE_OPERAND (arg, 0);
3046 /* For &x[y], return x+y */
3047 if (TREE_CODE (arg) == ARRAY_REF)
3049 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
3050 return error_mark_node;
3051 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3052 TREE_OPERAND (arg, 1), 1);
3055 /* Handle complex lvalues (when permitted)
3056 by reduction to simpler cases. */
3057 val = unary_complex_lvalue (code, arg);
3058 if (val != 0)
3059 return val;
3061 #if 0 /* Turned off because inconsistent;
3062 float f; *&(int)f = 3.4 stores in int format
3063 whereas (int)f = 3.4 stores in float format. */
3064 /* Address of a cast is just a cast of the address
3065 of the operand of the cast. */
3066 switch (TREE_CODE (arg))
3068 case NOP_EXPR:
3069 case CONVERT_EXPR:
3070 case FLOAT_EXPR:
3071 case FIX_TRUNC_EXPR:
3072 case FIX_FLOOR_EXPR:
3073 case FIX_ROUND_EXPR:
3074 case FIX_CEIL_EXPR:
3075 if (pedantic)
3076 pedwarn ("ANSI C forbids the address of a cast expression");
3077 return convert (build_pointer_type (TREE_TYPE (arg)),
3078 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3079 0));
3081 #endif
3083 /* Allow the address of a constructor if all the elements
3084 are constant. */
3085 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3087 /* Anything not already handled and not a true memory reference
3088 is an error. */
3089 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
3090 return error_mark_node;
3092 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3093 argtype = TREE_TYPE (arg);
3094 /* If the lvalue is const or volatile,
3095 merge that into the type that the address will point to. */
3096 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
3097 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3099 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3100 argtype = c_build_type_variant (argtype,
3101 TREE_READONLY (arg),
3102 TREE_THIS_VOLATILE (arg));
3105 argtype = build_pointer_type (argtype);
3107 if (mark_addressable (arg) == 0)
3108 return error_mark_node;
3111 tree addr;
3113 if (TREE_CODE (arg) == COMPONENT_REF)
3115 tree field = TREE_OPERAND (arg, 1);
3117 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3119 if (DECL_C_BIT_FIELD (field))
3121 error ("attempt to take address of bit-field structure member `%s'",
3122 IDENTIFIER_POINTER (DECL_NAME (field)));
3123 return error_mark_node;
3126 addr = convert (argtype, addr);
3128 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3130 tree offset
3131 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3132 size_int (BITS_PER_UNIT));
3133 int flag = TREE_CONSTANT (addr);
3134 addr = fold (build (PLUS_EXPR, argtype,
3135 addr, convert (argtype, offset)));
3136 TREE_CONSTANT (addr) = flag;
3139 else
3140 addr = build1 (code, argtype, arg);
3142 /* Address of a static or external variable or
3143 file-scope function counts as a constant. */
3144 if (staticp (arg)
3145 && ! (TREE_CODE (arg) == FUNCTION_DECL
3146 && DECL_CONTEXT (arg) != 0))
3147 TREE_CONSTANT (addr) = 1;
3148 return addr;
3151 default:
3152 break;
3155 if (!errstring)
3157 if (argtype == 0)
3158 argtype = TREE_TYPE (arg);
3159 return fold (build1 (code, argtype, arg));
3162 error (errstring);
3163 return error_mark_node;
3166 #if 0
3167 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3168 convert ARG with the same conversions in the same order
3169 and return the result. */
3171 static tree
3172 convert_sequence (conversions, arg)
3173 tree conversions;
3174 tree arg;
3176 switch (TREE_CODE (conversions))
3178 case NOP_EXPR:
3179 case CONVERT_EXPR:
3180 case FLOAT_EXPR:
3181 case FIX_TRUNC_EXPR:
3182 case FIX_FLOOR_EXPR:
3183 case FIX_ROUND_EXPR:
3184 case FIX_CEIL_EXPR:
3185 return convert (TREE_TYPE (conversions),
3186 convert_sequence (TREE_OPERAND (conversions, 0),
3187 arg));
3189 default:
3190 return arg;
3193 #endif /* 0 */
3195 /* Return nonzero if REF is an lvalue valid for this language.
3196 Lvalues can be assigned, unless their type has TYPE_READONLY.
3197 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3200 lvalue_p (ref)
3201 tree ref;
3203 register enum tree_code code = TREE_CODE (ref);
3205 switch (code)
3207 case REALPART_EXPR:
3208 case IMAGPART_EXPR:
3209 case COMPONENT_REF:
3210 return lvalue_p (TREE_OPERAND (ref, 0));
3212 case STRING_CST:
3213 return 1;
3215 case INDIRECT_REF:
3216 case ARRAY_REF:
3217 case VAR_DECL:
3218 case PARM_DECL:
3219 case RESULT_DECL:
3220 case ERROR_MARK:
3221 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3222 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3224 case BIND_EXPR:
3225 case RTL_EXPR:
3226 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3228 default:
3229 return 0;
3233 /* Return nonzero if REF is an lvalue valid for this language;
3234 otherwise, print an error message and return zero. */
3237 lvalue_or_else (ref, string)
3238 tree ref;
3239 char *string;
3241 int win = lvalue_p (ref);
3242 if (! win)
3243 error ("invalid lvalue in %s", string);
3244 return win;
3247 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3248 for certain kinds of expressions which are not really lvalues
3249 but which we can accept as lvalues.
3251 If ARG is not a kind of expression we can handle, return zero. */
3253 static tree
3254 unary_complex_lvalue (code, arg)
3255 enum tree_code code;
3256 tree arg;
3258 /* Handle (a, b) used as an "lvalue". */
3259 if (TREE_CODE (arg) == COMPOUND_EXPR)
3261 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3263 /* If this returns a function type, it isn't really being used as
3264 an lvalue, so don't issue a warning about it. */
3265 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3266 pedantic_lvalue_warning (COMPOUND_EXPR);
3268 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3269 TREE_OPERAND (arg, 0), real_result);
3272 /* Handle (a ? b : c) used as an "lvalue". */
3273 if (TREE_CODE (arg) == COND_EXPR)
3275 pedantic_lvalue_warning (COND_EXPR);
3276 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3277 pedantic_lvalue_warning (COMPOUND_EXPR);
3279 return (build_conditional_expr
3280 (TREE_OPERAND (arg, 0),
3281 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3282 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3285 return 0;
3288 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3289 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3291 static void
3292 pedantic_lvalue_warning (code)
3293 enum tree_code code;
3295 if (pedantic)
3296 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3297 code == COND_EXPR ? "conditional"
3298 : code == COMPOUND_EXPR ? "compound" : "cast");
3301 /* Warn about storing in something that is `const'. */
3303 void
3304 readonly_warning (arg, string)
3305 tree arg;
3306 char *string;
3308 char buf[80];
3309 strcpy (buf, string);
3311 /* Forbid assignments to iterators. */
3312 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3314 strcat (buf, " of iterator `%s'");
3315 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3318 if (TREE_CODE (arg) == COMPONENT_REF)
3320 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3321 readonly_warning (TREE_OPERAND (arg, 0), string);
3322 else
3324 strcat (buf, " of read-only member `%s'");
3325 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3328 else if (TREE_CODE (arg) == VAR_DECL)
3330 strcat (buf, " of read-only variable `%s'");
3331 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3333 else
3335 pedwarn ("%s of read-only location", buf);
3339 /* Mark EXP saying that we need to be able to take the
3340 address of it; it should not be allocated in a register.
3341 Value is 1 if successful. */
3344 mark_addressable (exp)
3345 tree exp;
3347 register tree x = exp;
3348 while (1)
3349 switch (TREE_CODE (x))
3351 case COMPONENT_REF:
3352 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3354 error ("cannot take address of bitfield `%s'",
3355 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3356 return 0;
3359 /* ... fall through ... */
3361 case ADDR_EXPR:
3362 case ARRAY_REF:
3363 case REALPART_EXPR:
3364 case IMAGPART_EXPR:
3365 x = TREE_OPERAND (x, 0);
3366 break;
3368 case CONSTRUCTOR:
3369 TREE_ADDRESSABLE (x) = 1;
3370 return 1;
3372 case VAR_DECL:
3373 case CONST_DECL:
3374 case PARM_DECL:
3375 case RESULT_DECL:
3376 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3377 && DECL_NONLOCAL (x))
3379 if (TREE_PUBLIC (x))
3381 error ("global register variable `%s' used in nested function",
3382 IDENTIFIER_POINTER (DECL_NAME (x)));
3383 return 0;
3385 pedwarn ("register variable `%s' used in nested function",
3386 IDENTIFIER_POINTER (DECL_NAME (x)));
3388 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3390 if (TREE_PUBLIC (x))
3392 error ("address of global register variable `%s' requested",
3393 IDENTIFIER_POINTER (DECL_NAME (x)));
3394 return 0;
3397 /* If we are making this addressable due to its having
3398 volatile components, give a different error message. Also
3399 handle the case of an unnamed parameter by not trying
3400 to give the name. */
3402 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3404 error ("cannot put object with volatile field into register");
3405 return 0;
3408 pedwarn ("address of register variable `%s' requested",
3409 IDENTIFIER_POINTER (DECL_NAME (x)));
3411 put_var_into_stack (x);
3413 /* drops in */
3414 case FUNCTION_DECL:
3415 TREE_ADDRESSABLE (x) = 1;
3416 #if 0 /* poplevel deals with this now. */
3417 if (DECL_CONTEXT (x) == 0)
3418 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3419 #endif
3421 default:
3422 return 1;
3426 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3428 tree
3429 build_conditional_expr (ifexp, op1, op2)
3430 tree ifexp, op1, op2;
3432 register tree type1;
3433 register tree type2;
3434 register enum tree_code code1;
3435 register enum tree_code code2;
3436 register tree result_type = NULL;
3437 tree orig_op1 = op1, orig_op2 = op2;
3439 ifexp = truthvalue_conversion (default_conversion (ifexp));
3441 #if 0 /* Produces wrong result if within sizeof. */
3442 /* Don't promote the operands separately if they promote
3443 the same way. Return the unpromoted type and let the combined
3444 value get promoted if necessary. */
3446 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3447 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3448 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3449 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3451 if (TREE_CODE (ifexp) == INTEGER_CST)
3452 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3454 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3456 #endif
3458 /* Promote both alternatives. */
3460 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3461 op1 = default_conversion (op1);
3462 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3463 op2 = default_conversion (op2);
3465 if (TREE_CODE (ifexp) == ERROR_MARK
3466 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3467 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3468 return error_mark_node;
3470 type1 = TREE_TYPE (op1);
3471 code1 = TREE_CODE (type1);
3472 type2 = TREE_TYPE (op2);
3473 code2 = TREE_CODE (type2);
3475 /* Quickly detect the usual case where op1 and op2 have the same type
3476 after promotion. */
3477 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3479 if (type1 == type2)
3480 result_type = type1;
3481 else
3482 result_type = TYPE_MAIN_VARIANT (type1);
3484 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3485 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3487 result_type = common_type (type1, type2);
3489 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3491 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3492 pedwarn ("ANSI C forbids conditional expr with only one void side");
3493 result_type = void_type_node;
3495 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3497 if (comp_target_types (type1, type2))
3498 result_type = common_type (type1, type2);
3499 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3500 && TREE_CODE (orig_op1) != NOP_EXPR)
3501 result_type = qualify_type (type2, type1);
3502 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3503 && TREE_CODE (orig_op2) != NOP_EXPR)
3504 result_type = qualify_type (type1, type2);
3505 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3507 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3508 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3509 result_type = qualify_type (type1, type2);
3511 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3513 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3514 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3515 result_type = qualify_type (type2, type1);
3517 else
3519 pedwarn ("pointer type mismatch in conditional expression");
3520 result_type = build_pointer_type (void_type_node);
3523 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3525 if (! integer_zerop (op2))
3526 pedwarn ("pointer/integer type mismatch in conditional expression");
3527 else
3529 op2 = null_pointer_node;
3530 #if 0 /* The spec seems to say this is permitted. */
3531 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3532 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3533 #endif
3535 result_type = type1;
3537 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3539 if (!integer_zerop (op1))
3540 pedwarn ("pointer/integer type mismatch in conditional expression");
3541 else
3543 op1 = null_pointer_node;
3544 #if 0 /* The spec seems to say this is permitted. */
3545 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3546 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3547 #endif
3549 result_type = type2;
3552 if (!result_type)
3554 if (flag_cond_mismatch)
3555 result_type = void_type_node;
3556 else
3558 error ("type mismatch in conditional expression");
3559 return error_mark_node;
3563 /* Merge const and volatile flags of the incoming types. */
3564 result_type
3565 = build_type_variant (result_type,
3566 TREE_READONLY (op1) || TREE_READONLY (op2),
3567 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3569 if (result_type != TREE_TYPE (op1))
3570 op1 = convert_and_check (result_type, op1);
3571 if (result_type != TREE_TYPE (op2))
3572 op2 = convert_and_check (result_type, op2);
3574 #if 0
3575 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3577 result_type = TREE_TYPE (op1);
3578 if (TREE_CONSTANT (ifexp))
3579 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3581 if (TYPE_MODE (result_type) == BLKmode)
3583 register tree tempvar
3584 = build_decl (VAR_DECL, NULL_TREE, result_type);
3585 register tree xop1 = build_modify_expr (tempvar, op1);
3586 register tree xop2 = build_modify_expr (tempvar, op2);
3587 register tree result = fold (build (COND_EXPR, result_type,
3588 ifexp, xop1, xop2));
3590 layout_decl (tempvar, TYPE_ALIGN (result_type));
3591 /* No way to handle variable-sized objects here.
3592 I fear that the entire handling of BLKmode conditional exprs
3593 needs to be redone. */
3594 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3595 abort ();
3596 DECL_RTL (tempvar)
3597 = assign_stack_local (DECL_MODE (tempvar),
3598 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3599 + BITS_PER_UNIT - 1)
3600 / BITS_PER_UNIT,
3603 TREE_SIDE_EFFECTS (result)
3604 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3605 | TREE_SIDE_EFFECTS (op2);
3606 return build (COMPOUND_EXPR, result_type, result, tempvar);
3609 #endif /* 0 */
3611 if (TREE_CODE (ifexp) == INTEGER_CST)
3612 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3614 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3617 /* Given a list of expressions, return a compound expression
3618 that performs them all and returns the value of the last of them. */
3620 tree
3621 build_compound_expr (list)
3622 tree list;
3624 return internal_build_compound_expr (list, TRUE);
3627 static tree
3628 internal_build_compound_expr (list, first_p)
3629 tree list;
3630 int first_p;
3632 register tree rest;
3634 if (TREE_CHAIN (list) == 0)
3636 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3637 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3639 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3640 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3641 list = TREE_OPERAND (list, 0);
3642 #endif
3644 /* Don't let (0, 0) be null pointer constant. */
3645 if (!first_p && integer_zerop (TREE_VALUE (list)))
3646 return non_lvalue (TREE_VALUE (list));
3647 return TREE_VALUE (list);
3650 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3652 /* Convert arrays to pointers when there really is a comma operator. */
3653 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3654 TREE_VALUE (TREE_CHAIN (list))
3655 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3658 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3660 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3662 /* The left-hand operand of a comma expression is like an expression
3663 statement: with -W or -Wunused, we should warn if it doesn't have
3664 any side-effects, unless it was explicitly cast to (void). */
3665 if ((extra_warnings || warn_unused)
3666 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3667 && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
3668 warning ("left-hand operand of comma expression has no effect");
3670 /* When pedantic, a compound expression can be neither an lvalue
3671 nor an integer constant expression. */
3672 if (! pedantic)
3673 return rest;
3676 /* With -Wunused, we should also warn if the left-hand operand does have
3677 side-effects, but computes a value which is not used. For example, in
3678 `foo() + bar(), baz()' the result of the `+' operator is not used,
3679 so we should issue a warning. */
3680 else if (warn_unused)
3681 warn_if_unused_value (TREE_VALUE (list));
3683 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3686 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3688 tree
3689 build_c_cast (type, expr)
3690 register tree type;
3691 tree expr;
3693 register tree value = expr;
3695 if (type == error_mark_node || expr == error_mark_node)
3696 return error_mark_node;
3697 type = TYPE_MAIN_VARIANT (type);
3699 #if 0
3700 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3701 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3702 value = TREE_OPERAND (value, 0);
3703 #endif
3705 if (TREE_CODE (type) == ARRAY_TYPE)
3707 error ("cast specifies array type");
3708 return error_mark_node;
3711 if (TREE_CODE (type) == FUNCTION_TYPE)
3713 error ("cast specifies function type");
3714 return error_mark_node;
3717 if (type == TREE_TYPE (value))
3719 if (pedantic)
3721 if (TREE_CODE (type) == RECORD_TYPE
3722 || TREE_CODE (type) == UNION_TYPE)
3723 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3726 else if (TREE_CODE (type) == UNION_TYPE)
3728 tree field;
3729 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3730 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3731 value = default_conversion (value);
3733 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3734 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3735 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3736 break;
3738 if (field)
3740 char *name;
3741 tree t;
3743 if (pedantic)
3744 pedwarn ("ANSI C forbids casts to union type");
3745 if (TYPE_NAME (type) != 0)
3747 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3748 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3749 else
3750 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3752 else
3753 name = "";
3754 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3755 build_tree_list (field, value)),
3756 0, 0);
3757 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3758 return t;
3760 error ("cast to union type from type not present in union");
3761 return error_mark_node;
3763 else
3765 tree otype, ovalue;
3767 /* If casting to void, avoid the error that would come
3768 from default_conversion in the case of a non-lvalue array. */
3769 if (type == void_type_node)
3770 return build1 (CONVERT_EXPR, type, value);
3772 /* Convert functions and arrays to pointers,
3773 but don't convert any other types. */
3774 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3775 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3776 value = default_conversion (value);
3777 otype = TREE_TYPE (value);
3779 /* Optionally warn about potentially worrisome casts. */
3781 if (warn_cast_qual
3782 && TREE_CODE (type) == POINTER_TYPE
3783 && TREE_CODE (otype) == POINTER_TYPE)
3785 if (TYPE_VOLATILE (TREE_TYPE (otype))
3786 && ! TYPE_VOLATILE (TREE_TYPE (type)))
3787 pedwarn ("cast discards `volatile' from pointer target type");
3788 if (TYPE_READONLY (TREE_TYPE (otype))
3789 && ! TYPE_READONLY (TREE_TYPE (type)))
3790 pedwarn ("cast discards `const' from pointer target type");
3793 /* Warn about possible alignment problems. */
3794 if (STRICT_ALIGNMENT && warn_cast_align
3795 && TREE_CODE (type) == POINTER_TYPE
3796 && TREE_CODE (otype) == POINTER_TYPE
3797 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3798 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3799 /* Don't warn about opaque types, where the actual alignment
3800 restriction is unknown. */
3801 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3802 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3803 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3804 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3805 warning ("cast increases required alignment of target type");
3807 if (TREE_CODE (type) == INTEGER_TYPE
3808 && TREE_CODE (otype) == POINTER_TYPE
3809 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3810 && !TREE_CONSTANT (value))
3811 warning ("cast from pointer to integer of different size");
3813 if (warn_bad_function_cast
3814 && TREE_CODE (value) == CALL_EXPR
3815 && TREE_CODE (type) != TREE_CODE (otype))
3816 warning ("cast does not match function type");
3818 if (TREE_CODE (type) == POINTER_TYPE
3819 && TREE_CODE (otype) == INTEGER_TYPE
3820 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3821 #if 0
3822 /* Don't warn about converting 0 to pointer,
3823 provided the 0 was explicit--not cast or made by folding. */
3824 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3825 #endif
3826 /* Don't warn about converting any constant. */
3827 && !TREE_CONSTANT (value))
3828 warning ("cast to pointer from integer of different size");
3830 ovalue = value;
3831 value = convert (type, value);
3833 /* Ignore any integer overflow caused by the cast. */
3834 if (TREE_CODE (value) == INTEGER_CST)
3836 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3837 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3841 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3842 if (pedantic && TREE_CODE (value) == INTEGER_CST
3843 && TREE_CODE (expr) == INTEGER_CST
3844 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3845 value = non_lvalue (value);
3847 /* If pedantic, don't let a cast be an lvalue. */
3848 if (value == expr && pedantic)
3849 value = non_lvalue (value);
3851 return value;
3854 /* Build an assignment expression of lvalue LHS from value RHS.
3855 MODIFYCODE is the code for a binary operator that we use
3856 to combine the old value of LHS with RHS to get the new value.
3857 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3859 tree
3860 build_modify_expr (lhs, modifycode, rhs)
3861 tree lhs, rhs;
3862 enum tree_code modifycode;
3864 register tree result;
3865 tree newrhs;
3866 tree lhstype = TREE_TYPE (lhs);
3867 tree olhstype = lhstype;
3869 /* Types that aren't fully specified cannot be used in assignments. */
3870 lhs = require_complete_type (lhs);
3872 /* Avoid duplicate error messages from operands that had errors. */
3873 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3874 return error_mark_node;
3876 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3877 /* Do not use STRIP_NOPS here. We do not want an enumerator
3878 whose value is 0 to count as a null pointer constant. */
3879 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3880 rhs = TREE_OPERAND (rhs, 0);
3882 newrhs = rhs;
3884 /* Handle control structure constructs used as "lvalues". */
3886 switch (TREE_CODE (lhs))
3888 /* Handle (a, b) used as an "lvalue". */
3889 case COMPOUND_EXPR:
3890 pedantic_lvalue_warning (COMPOUND_EXPR);
3891 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3892 modifycode, rhs);
3893 if (TREE_CODE (newrhs) == ERROR_MARK)
3894 return error_mark_node;
3895 return build (COMPOUND_EXPR, lhstype,
3896 TREE_OPERAND (lhs, 0), newrhs);
3898 /* Handle (a ? b : c) used as an "lvalue". */
3899 case COND_EXPR:
3900 pedantic_lvalue_warning (COND_EXPR);
3901 rhs = save_expr (rhs);
3903 /* Produce (a ? (b = rhs) : (c = rhs))
3904 except that the RHS goes through a save-expr
3905 so the code to compute it is only emitted once. */
3906 tree cond
3907 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3908 build_modify_expr (TREE_OPERAND (lhs, 1),
3909 modifycode, rhs),
3910 build_modify_expr (TREE_OPERAND (lhs, 2),
3911 modifycode, rhs));
3912 if (TREE_CODE (cond) == ERROR_MARK)
3913 return cond;
3914 /* Make sure the code to compute the rhs comes out
3915 before the split. */
3916 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3917 /* But cast it to void to avoid an "unused" error. */
3918 convert (void_type_node, rhs), cond);
3920 default:
3921 break;
3924 /* If a binary op has been requested, combine the old LHS value with the RHS
3925 producing the value we should actually store into the LHS. */
3927 if (modifycode != NOP_EXPR)
3929 lhs = stabilize_reference (lhs);
3930 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3933 /* Handle a cast used as an "lvalue".
3934 We have already performed any binary operator using the value as cast.
3935 Now convert the result to the cast type of the lhs,
3936 and then true type of the lhs and store it there;
3937 then convert result back to the cast type to be the value
3938 of the assignment. */
3940 switch (TREE_CODE (lhs))
3942 case NOP_EXPR:
3943 case CONVERT_EXPR:
3944 case FLOAT_EXPR:
3945 case FIX_TRUNC_EXPR:
3946 case FIX_FLOOR_EXPR:
3947 case FIX_ROUND_EXPR:
3948 case FIX_CEIL_EXPR:
3949 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3950 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3951 newrhs = default_conversion (newrhs);
3953 tree inner_lhs = TREE_OPERAND (lhs, 0);
3954 tree result;
3955 result = build_modify_expr (inner_lhs, NOP_EXPR,
3956 convert (TREE_TYPE (inner_lhs),
3957 convert (lhstype, newrhs)));
3958 if (TREE_CODE (result) == ERROR_MARK)
3959 return result;
3960 pedantic_lvalue_warning (CONVERT_EXPR);
3961 return convert (TREE_TYPE (lhs), result);
3964 default:
3965 break;
3968 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3969 Reject anything strange now. */
3971 if (!lvalue_or_else (lhs, "assignment"))
3972 return error_mark_node;
3974 /* Warn about storing in something that is `const'. */
3976 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3977 || ((TREE_CODE (lhstype) == RECORD_TYPE
3978 || TREE_CODE (lhstype) == UNION_TYPE)
3979 && C_TYPE_FIELDS_READONLY (lhstype)))
3980 readonly_warning (lhs, "assignment");
3982 /* If storing into a structure or union member,
3983 it has probably been given type `int'.
3984 Compute the type that would go with
3985 the actual amount of storage the member occupies. */
3987 if (TREE_CODE (lhs) == COMPONENT_REF
3988 && (TREE_CODE (lhstype) == INTEGER_TYPE
3989 || TREE_CODE (lhstype) == REAL_TYPE
3990 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3991 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3993 /* If storing in a field that is in actuality a short or narrower than one,
3994 we must store in the field in its actual type. */
3996 if (lhstype != TREE_TYPE (lhs))
3998 lhs = copy_node (lhs);
3999 TREE_TYPE (lhs) = lhstype;
4002 /* Convert new value to destination type. */
4004 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
4005 NULL_TREE, NULL_TREE, 0);
4006 if (TREE_CODE (newrhs) == ERROR_MARK)
4007 return error_mark_node;
4009 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4010 TREE_SIDE_EFFECTS (result) = 1;
4012 /* If we got the LHS in a different type for storing in,
4013 convert the result back to the nominal type of LHS
4014 so that the value we return always has the same type
4015 as the LHS argument. */
4017 if (olhstype == TREE_TYPE (result))
4018 return result;
4019 return convert_for_assignment (olhstype, result, "assignment",
4020 NULL_TREE, NULL_TREE, 0);
4023 /* Convert value RHS to type TYPE as preparation for an assignment
4024 to an lvalue of type TYPE.
4025 The real work of conversion is done by `convert'.
4026 The purpose of this function is to generate error messages
4027 for assignments that are not allowed in C.
4028 ERRTYPE is a string to use in error messages:
4029 "assignment", "return", etc. If it is null, this is parameter passing
4030 for a function call (and different error messages are output). Otherwise,
4031 it may be a name stored in the spelling stack and interpreted by
4032 get_spelling.
4034 FUNNAME is the name of the function being called,
4035 as an IDENTIFIER_NODE, or null.
4036 PARMNUM is the number of the argument, for printing in error messages. */
4038 static tree
4039 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
4040 tree type, rhs;
4041 char *errtype;
4042 tree fundecl, funname;
4043 int parmnum;
4045 register enum tree_code codel = TREE_CODE (type);
4046 register tree rhstype;
4047 register enum tree_code coder;
4049 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4050 /* Do not use STRIP_NOPS here. We do not want an enumerator
4051 whose value is 0 to count as a null pointer constant. */
4052 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4053 rhs = TREE_OPERAND (rhs, 0);
4055 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4056 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4057 rhs = default_conversion (rhs);
4058 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4059 rhs = decl_constant_value (rhs);
4061 rhstype = TREE_TYPE (rhs);
4062 coder = TREE_CODE (rhstype);
4064 if (coder == ERROR_MARK)
4065 return error_mark_node;
4067 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4069 overflow_warning (rhs);
4070 /* Check for Objective-C protocols. This will issue a warning if
4071 there are protocol violations. No need to use the return value. */
4072 maybe_objc_comptypes (type, rhstype, 0);
4073 return rhs;
4076 if (coder == VOID_TYPE)
4078 error ("void value not ignored as it ought to be");
4079 return error_mark_node;
4081 /* Arithmetic types all interconvert, and enum is treated like int. */
4082 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
4083 || codel == COMPLEX_TYPE)
4084 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
4085 || coder == COMPLEX_TYPE))
4086 return convert_and_check (type, rhs);
4088 /* Conversion to a transparent union from its member types.
4089 This applies only to function arguments. */
4090 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4092 tree memb_types;
4093 tree marginal_memb_type = 0;
4095 for (memb_types = TYPE_FIELDS (type); memb_types;
4096 memb_types = TREE_CHAIN (memb_types))
4098 tree memb_type = TREE_TYPE (memb_types);
4100 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4101 TYPE_MAIN_VARIANT (rhstype)))
4102 break;
4104 if (TREE_CODE (memb_type) != POINTER_TYPE)
4105 continue;
4107 if (coder == POINTER_TYPE)
4109 register tree ttl = TREE_TYPE (memb_type);
4110 register tree ttr = TREE_TYPE (rhstype);
4112 /* Any non-function converts to a [const][volatile] void *
4113 and vice versa; otherwise, targets must be the same.
4114 Meanwhile, the lhs target must have all the qualifiers of
4115 the rhs. */
4116 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4117 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4118 || comp_target_types (memb_type, rhstype))
4120 /* If this type won't generate any warnings, use it. */
4121 if ((TREE_CODE (ttr) == FUNCTION_TYPE
4122 && TREE_CODE (ttl) == FUNCTION_TYPE)
4123 ? ((! TYPE_READONLY (ttl) | TYPE_READONLY (ttr))
4124 & (! TYPE_VOLATILE (ttl) | TYPE_VOLATILE (ttr)))
4125 : ((TYPE_READONLY (ttl) | ! TYPE_READONLY (ttr))
4126 & (TYPE_VOLATILE (ttl) | ! TYPE_VOLATILE (ttr))))
4127 break;
4129 /* Keep looking for a better type, but remember this one. */
4130 if (! marginal_memb_type)
4131 marginal_memb_type = memb_type;
4135 /* Can convert integer zero to any pointer type. */
4136 if (integer_zerop (rhs)
4137 || (TREE_CODE (rhs) == NOP_EXPR
4138 && integer_zerop (TREE_OPERAND (rhs, 0))))
4140 rhs = null_pointer_node;
4141 break;
4145 if (memb_types || marginal_memb_type)
4147 if (! memb_types)
4149 /* We have only a marginally acceptable member type;
4150 it needs a warning. */
4151 register tree ttl = TREE_TYPE (marginal_memb_type);
4152 register tree ttr = TREE_TYPE (rhstype);
4154 /* Const and volatile mean something different for function
4155 types, so the usual warnings are not appropriate. */
4156 if (TREE_CODE (ttr) == FUNCTION_TYPE
4157 && TREE_CODE (ttl) == FUNCTION_TYPE)
4159 /* Because const and volatile on functions are
4160 restrictions that say the function will not do
4161 certain things, it is okay to use a const or volatile
4162 function where an ordinary one is wanted, but not
4163 vice-versa. */
4164 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4165 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4166 get_spelling (errtype), funname,
4167 parmnum);
4168 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4169 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4170 get_spelling (errtype), funname,
4171 parmnum);
4173 else
4175 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4176 warn_for_assignment ("%s discards `const' from pointer target type",
4177 get_spelling (errtype), funname,
4178 parmnum);
4179 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4180 warn_for_assignment ("%s discards `volatile' from pointer target type",
4181 get_spelling (errtype), funname,
4182 parmnum);
4186 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4187 pedwarn ("ANSI C prohibits argument conversion to union type");
4189 return build1 (NOP_EXPR, type, rhs);
4193 /* Conversions among pointers */
4194 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
4196 register tree ttl = TREE_TYPE (type);
4197 register tree ttr = TREE_TYPE (rhstype);
4199 /* Any non-function converts to a [const][volatile] void *
4200 and vice versa; otherwise, targets must be the same.
4201 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4202 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4203 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4204 || comp_target_types (type, rhstype)
4205 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4206 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4208 if (pedantic
4209 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4210 && TREE_CODE (ttr) == FUNCTION_TYPE)
4212 (TYPE_MAIN_VARIANT (ttr) == void_type_node
4213 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4214 which are not ANSI null ptr constants. */
4215 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4216 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4217 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4218 get_spelling (errtype), funname, parmnum);
4219 /* Const and volatile mean something different for function types,
4220 so the usual warnings are not appropriate. */
4221 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4222 && TREE_CODE (ttl) != FUNCTION_TYPE)
4224 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4225 warn_for_assignment ("%s discards `const' from pointer target type",
4226 get_spelling (errtype), funname, parmnum);
4227 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4228 warn_for_assignment ("%s discards `volatile' from pointer target type",
4229 get_spelling (errtype), funname, parmnum);
4230 /* If this is not a case of ignoring a mismatch in signedness,
4231 no warning. */
4232 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4233 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4234 || comp_target_types (type, rhstype))
4236 /* If there is a mismatch, do warn. */
4237 else if (pedantic)
4238 warn_for_assignment ("pointer targets in %s differ in signedness",
4239 get_spelling (errtype), funname, parmnum);
4241 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4242 && TREE_CODE (ttr) == FUNCTION_TYPE)
4244 /* Because const and volatile on functions are restrictions
4245 that say the function will not do certain things,
4246 it is okay to use a const or volatile function
4247 where an ordinary one is wanted, but not vice-versa. */
4248 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4249 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4250 get_spelling (errtype), funname, parmnum);
4251 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4252 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4253 get_spelling (errtype), funname, parmnum);
4256 else
4257 warn_for_assignment ("%s from incompatible pointer type",
4258 get_spelling (errtype), funname, parmnum);
4259 return convert (type, rhs);
4261 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4263 /* An explicit constant 0 can convert to a pointer,
4264 or one that results from arithmetic, even including
4265 a cast to integer type. */
4266 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4268 ! (TREE_CODE (rhs) == NOP_EXPR
4269 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4270 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4271 && integer_zerop (TREE_OPERAND (rhs, 0))))
4273 warn_for_assignment ("%s makes pointer from integer without a cast",
4274 get_spelling (errtype), funname, parmnum);
4275 return convert (type, rhs);
4277 return null_pointer_node;
4279 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4281 warn_for_assignment ("%s makes integer from pointer without a cast",
4282 get_spelling (errtype), funname, parmnum);
4283 return convert (type, rhs);
4286 if (!errtype)
4288 if (funname)
4290 tree selector = maybe_building_objc_message_expr ();
4292 if (selector && parmnum > 2)
4293 error ("incompatible type for argument %d of `%s'",
4294 parmnum - 2, IDENTIFIER_POINTER (selector));
4295 else
4296 error ("incompatible type for argument %d of `%s'",
4297 parmnum, IDENTIFIER_POINTER (funname));
4299 else
4300 error ("incompatible type for argument %d of indirect function call",
4301 parmnum);
4303 else
4304 error ("incompatible types in %s", get_spelling (errtype));
4306 return error_mark_node;
4309 /* Print a warning using MSG.
4310 It gets OPNAME as its one parameter.
4311 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4312 FUNCTION and ARGNUM are handled specially if we are building an
4313 Objective-C selector. */
4315 static void
4316 warn_for_assignment (msg, opname, function, argnum)
4317 char *msg;
4318 char *opname;
4319 tree function;
4320 int argnum;
4322 static char argstring[] = "passing arg %d of `%s'";
4323 static char argnofun[] = "passing arg %d";
4325 if (opname == 0)
4327 tree selector = maybe_building_objc_message_expr ();
4329 if (selector && argnum > 2)
4331 function = selector;
4332 argnum -= 2;
4334 if (function)
4336 /* Function name is known; supply it. */
4337 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4338 + sizeof (argstring) + 25 /*%d*/ + 1);
4339 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4341 else
4343 /* Function name unknown (call through ptr); just give arg number. */
4344 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4345 sprintf (opname, argnofun, argnum);
4348 pedwarn (msg, opname);
4351 /* Return nonzero if VALUE is a valid constant-valued expression
4352 for use in initializing a static variable; one that can be an
4353 element of a "constant" initializer.
4355 Return null_pointer_node if the value is absolute;
4356 if it is relocatable, return the variable that determines the relocation.
4357 We assume that VALUE has been folded as much as possible;
4358 therefore, we do not need to check for such things as
4359 arithmetic-combinations of integers. */
4361 tree
4362 initializer_constant_valid_p (value, endtype)
4363 tree value;
4364 tree endtype;
4366 switch (TREE_CODE (value))
4368 case CONSTRUCTOR:
4369 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4370 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
4371 && TREE_CONSTANT (value)
4372 && CONSTRUCTOR_ELTS (value))
4373 return
4374 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
4375 endtype);
4377 return TREE_STATIC (value) ? null_pointer_node : 0;
4379 case INTEGER_CST:
4380 case REAL_CST:
4381 case STRING_CST:
4382 case COMPLEX_CST:
4383 return null_pointer_node;
4385 case ADDR_EXPR:
4386 return TREE_OPERAND (value, 0);
4388 case NON_LVALUE_EXPR:
4389 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4391 case CONVERT_EXPR:
4392 case NOP_EXPR:
4393 /* Allow conversions between pointer types. */
4394 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4395 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
4396 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4398 /* Allow conversions between real types. */
4399 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4400 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
4401 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4403 /* Allow length-preserving conversions between integer types. */
4404 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4405 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4406 && (TYPE_PRECISION (TREE_TYPE (value))
4407 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4408 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4410 /* Allow conversions between other integer types only if
4411 explicit value. */
4412 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4413 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4415 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4416 endtype);
4417 if (inner == null_pointer_node)
4418 return null_pointer_node;
4419 return 0;
4422 /* Allow (int) &foo provided int is as wide as a pointer. */
4423 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4424 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
4425 && (TYPE_PRECISION (TREE_TYPE (value))
4426 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4427 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4428 endtype);
4430 /* Likewise conversions from int to pointers. */
4431 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4432 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4433 && (TYPE_PRECISION (TREE_TYPE (value))
4434 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4435 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4436 endtype);
4438 /* Allow conversions to union types if the value inside is okay. */
4439 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4440 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4441 endtype);
4442 return 0;
4444 case PLUS_EXPR:
4445 if (TREE_CODE (endtype) == INTEGER_TYPE
4446 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4447 return 0;
4449 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4450 endtype);
4451 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4452 endtype);
4453 /* If either term is absolute, use the other terms relocation. */
4454 if (valid0 == null_pointer_node)
4455 return valid1;
4456 if (valid1 == null_pointer_node)
4457 return valid0;
4458 return 0;
4461 case MINUS_EXPR:
4462 if (TREE_CODE (endtype) == INTEGER_TYPE
4463 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4464 return 0;
4466 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4467 endtype);
4468 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4469 endtype);
4470 /* Win if second argument is absolute. */
4471 if (valid1 == null_pointer_node)
4472 return valid0;
4473 /* Win if both arguments have the same relocation.
4474 Then the value is absolute. */
4475 if (valid0 == valid1)
4476 return null_pointer_node;
4477 return 0;
4480 default:
4481 return 0;
4485 /* If VALUE is a compound expr all of whose expressions are constant, then
4486 return its value. Otherwise, return error_mark_node.
4488 This is for handling COMPOUND_EXPRs as initializer elements
4489 which is allowed with a warning when -pedantic is specified. */
4491 static tree
4492 valid_compound_expr_initializer (value, endtype)
4493 tree value;
4494 tree endtype;
4496 if (TREE_CODE (value) == COMPOUND_EXPR)
4498 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4499 == error_mark_node)
4500 return error_mark_node;
4501 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4502 endtype);
4504 else if (! TREE_CONSTANT (value)
4505 && ! initializer_constant_valid_p (value, endtype))
4506 return error_mark_node;
4507 else
4508 return value;
4511 /* Perform appropriate conversions on the initial value of a variable,
4512 store it in the declaration DECL,
4513 and print any error messages that are appropriate.
4514 If the init is invalid, store an ERROR_MARK. */
4516 void
4517 store_init_value (decl, init)
4518 tree decl, init;
4520 register tree value, type;
4522 /* If variable's type was invalidly declared, just ignore it. */
4524 type = TREE_TYPE (decl);
4525 if (TREE_CODE (type) == ERROR_MARK)
4526 return;
4528 /* Digest the specified initializer into an expression. */
4530 value = digest_init (type, init, TREE_STATIC (decl),
4531 TREE_STATIC (decl) || pedantic);
4533 /* Store the expression if valid; else report error. */
4535 #if 0
4536 /* Note that this is the only place we can detect the error
4537 in a case such as struct foo bar = (struct foo) { x, y };
4538 where there is one initial value which is a constructor expression. */
4539 if (value == error_mark_node)
4541 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4543 error ("initializer for static variable is not constant");
4544 value = error_mark_node;
4546 else if (TREE_STATIC (decl)
4547 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4549 error ("initializer for static variable uses complicated arithmetic");
4550 value = error_mark_node;
4552 else
4554 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4556 if (! TREE_CONSTANT (value))
4557 pedwarn ("aggregate initializer is not constant");
4558 else if (! TREE_STATIC (value))
4559 pedwarn ("aggregate initializer uses complicated arithmetic");
4562 #endif
4564 DECL_INITIAL (decl) = value;
4566 /* ANSI wants warnings about out-of-range constant initializers. */
4567 STRIP_TYPE_NOPS (value);
4568 constant_expression_warning (value);
4571 /* Methods for storing and printing names for error messages. */
4573 /* Implement a spelling stack that allows components of a name to be pushed
4574 and popped. Each element on the stack is this structure. */
4576 struct spelling
4578 int kind;
4579 union
4581 int i;
4582 char *s;
4583 } u;
4586 #define SPELLING_STRING 1
4587 #define SPELLING_MEMBER 2
4588 #define SPELLING_BOUNDS 3
4590 static struct spelling *spelling; /* Next stack element (unused). */
4591 static struct spelling *spelling_base; /* Spelling stack base. */
4592 static int spelling_size; /* Size of the spelling stack. */
4594 /* Macros to save and restore the spelling stack around push_... functions.
4595 Alternative to SAVE_SPELLING_STACK. */
4597 #define SPELLING_DEPTH() (spelling - spelling_base)
4598 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4600 /* Save and restore the spelling stack around arbitrary C code. */
4602 #define SAVE_SPELLING_DEPTH(code) \
4604 int __depth = SPELLING_DEPTH (); \
4605 code; \
4606 RESTORE_SPELLING_DEPTH (__depth); \
4609 /* Push an element on the spelling stack with type KIND and assign VALUE
4610 to MEMBER. */
4612 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4614 int depth = SPELLING_DEPTH (); \
4616 if (depth >= spelling_size) \
4618 spelling_size += 10; \
4619 if (spelling_base == 0) \
4620 spelling_base \
4621 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4622 else \
4623 spelling_base \
4624 = (struct spelling *) xrealloc (spelling_base, \
4625 spelling_size * sizeof (struct spelling)); \
4626 RESTORE_SPELLING_DEPTH (depth); \
4629 spelling->kind = (KIND); \
4630 spelling->MEMBER = (VALUE); \
4631 spelling++; \
4634 /* Push STRING on the stack. Printed literally. */
4636 static void
4637 push_string (string)
4638 char *string;
4640 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4643 /* Push a member name on the stack. Printed as '.' STRING. */
4645 static void
4646 push_member_name (decl)
4647 tree decl;
4650 char *string
4651 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4652 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4655 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4657 static void
4658 push_array_bounds (bounds)
4659 int bounds;
4661 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4664 /* Compute the maximum size in bytes of the printed spelling. */
4666 static int
4667 spelling_length ()
4669 register int size = 0;
4670 register struct spelling *p;
4672 for (p = spelling_base; p < spelling; p++)
4674 if (p->kind == SPELLING_BOUNDS)
4675 size += 25;
4676 else
4677 size += strlen (p->u.s) + 1;
4680 return size;
4683 /* Print the spelling to BUFFER and return it. */
4685 static char *
4686 print_spelling (buffer)
4687 register char *buffer;
4689 register char *d = buffer;
4690 register char *s;
4691 register struct spelling *p;
4693 for (p = spelling_base; p < spelling; p++)
4694 if (p->kind == SPELLING_BOUNDS)
4696 sprintf (d, "[%d]", p->u.i);
4697 d += strlen (d);
4699 else
4701 if (p->kind == SPELLING_MEMBER)
4702 *d++ = '.';
4703 for (s = p->u.s; *d = *s++; d++)
4706 *d++ = '\0';
4707 return buffer;
4710 /* Provide a means to pass component names derived from the spelling stack. */
4712 char initialization_message;
4714 /* Interpret the spelling of the given ERRTYPE message. */
4716 static char *
4717 get_spelling (errtype)
4718 char *errtype;
4720 static char *buffer;
4721 static int size = -1;
4723 if (errtype == &initialization_message)
4725 /* Avoid counting chars */
4726 static char message[] = "initialization of `%s'";
4727 register int needed = sizeof (message) + spelling_length () + 1;
4728 char *temp;
4730 if (size < 0)
4731 buffer = (char *) xmalloc (size = needed);
4732 if (needed > size)
4733 buffer = (char *) xrealloc (buffer, size = needed);
4735 temp = (char *) alloca (needed);
4736 sprintf (buffer, message, print_spelling (temp));
4737 return buffer;
4740 return errtype;
4743 /* Issue an error message for a bad initializer component.
4744 FORMAT describes the message. OFWHAT is the name for the component.
4745 LOCAL is a format string for formatting the insertion of the name
4746 into the message.
4748 If OFWHAT is null, the component name is stored on the spelling stack.
4749 If the component name is a null string, then LOCAL is omitted entirely. */
4751 void
4752 error_init (format, local, ofwhat)
4753 char *format, *local, *ofwhat;
4755 char *buffer;
4757 if (ofwhat == 0)
4758 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4759 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4761 if (*ofwhat)
4762 sprintf (buffer, local, ofwhat);
4763 else
4764 buffer[0] = 0;
4766 error (format, buffer);
4769 /* Issue a pedantic warning for a bad initializer component.
4770 FORMAT describes the message. OFWHAT is the name for the component.
4771 LOCAL is a format string for formatting the insertion of the name
4772 into the message.
4774 If OFWHAT is null, the component name is stored on the spelling stack.
4775 If the component name is a null string, then LOCAL is omitted entirely. */
4777 void
4778 pedwarn_init (format, local, ofwhat)
4779 char *format, *local, *ofwhat;
4781 char *buffer;
4783 if (ofwhat == 0)
4784 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4785 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4787 if (*ofwhat)
4788 sprintf (buffer, local, ofwhat);
4789 else
4790 buffer[0] = 0;
4792 pedwarn (format, buffer);
4795 /* Issue a warning for a bad initializer component.
4796 FORMAT describes the message. OFWHAT is the name for the component.
4797 LOCAL is a format string for formatting the insertion of the name
4798 into the message.
4800 If OFWHAT is null, the component name is stored on the spelling stack.
4801 If the component name is a null string, then LOCAL is omitted entirely. */
4803 static void
4804 warning_init (format, local, ofwhat)
4805 char *format, *local, *ofwhat;
4807 char *buffer;
4809 if (ofwhat == 0)
4810 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4811 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4813 if (*ofwhat)
4814 sprintf (buffer, local, ofwhat);
4815 else
4816 buffer[0] = 0;
4818 warning (format, buffer);
4821 /* Digest the parser output INIT as an initializer for type TYPE.
4822 Return a C expression of type TYPE to represent the initial value.
4824 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4825 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4826 applies only to elements of constructors. */
4828 static tree
4829 digest_init (type, init, require_constant, constructor_constant)
4830 tree type, init;
4831 int require_constant, constructor_constant;
4833 enum tree_code code = TREE_CODE (type);
4834 tree inside_init = init;
4836 if (init == error_mark_node)
4837 return init;
4839 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4840 /* Do not use STRIP_NOPS here. We do not want an enumerator
4841 whose value is 0 to count as a null pointer constant. */
4842 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4843 inside_init = TREE_OPERAND (init, 0);
4845 /* Initialization of an array of chars from a string constant
4846 optionally enclosed in braces. */
4848 if (code == ARRAY_TYPE)
4850 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4851 if ((typ1 == char_type_node
4852 || typ1 == signed_char_type_node
4853 || typ1 == unsigned_char_type_node
4854 || typ1 == unsigned_wchar_type_node
4855 || typ1 == signed_wchar_type_node)
4856 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4858 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4859 TYPE_MAIN_VARIANT (type)))
4860 return inside_init;
4862 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4863 != char_type_node)
4864 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4866 error_init ("char-array%s initialized from wide string",
4867 " `%s'", NULL);
4868 return error_mark_node;
4870 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4871 == char_type_node)
4872 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4874 error_init ("int-array%s initialized from non-wide string",
4875 " `%s'", NULL);
4876 return error_mark_node;
4879 TREE_TYPE (inside_init) = type;
4880 if (TYPE_DOMAIN (type) != 0
4881 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4883 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4884 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4885 /* Subtract 1 (or sizeof (wchar_t))
4886 because it's ok to ignore the terminating null char
4887 that is counted in the length of the constant. */
4888 if (size < TREE_STRING_LENGTH (inside_init)
4889 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4890 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4891 : 1))
4892 pedwarn_init (
4893 "initializer-string for array of chars%s is too long",
4894 " `%s'", NULL);
4896 return inside_init;
4900 /* Any type can be initialized
4901 from an expression of the same type, optionally with braces. */
4903 if (inside_init && TREE_TYPE (inside_init) != 0
4904 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4905 TYPE_MAIN_VARIANT (type))
4906 || (code == ARRAY_TYPE
4907 && comptypes (TREE_TYPE (inside_init), type))
4908 || (code == POINTER_TYPE
4909 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4910 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4911 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4912 TREE_TYPE (type)))))
4914 if (code == POINTER_TYPE
4915 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4916 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4917 inside_init = default_conversion (inside_init);
4918 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4919 && TREE_CODE (inside_init) != CONSTRUCTOR)
4921 error_init ("array%s initialized from non-constant array expression",
4922 " `%s'", NULL);
4923 return error_mark_node;
4926 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4927 inside_init = decl_constant_value (inside_init);
4929 /* Compound expressions can only occur here if -pedantic or
4930 -pedantic-errors is specified. In the later case, we always want
4931 an error. In the former case, we simply want a warning. */
4932 if (require_constant && pedantic
4933 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4935 inside_init
4936 = valid_compound_expr_initializer (inside_init,
4937 TREE_TYPE (inside_init));
4938 if (inside_init == error_mark_node)
4939 error_init ("initializer element%s is not constant",
4940 " for `%s'", NULL);
4941 else
4942 pedwarn_init ("initializer element%s is not constant",
4943 " for `%s'", NULL);
4944 if (flag_pedantic_errors)
4945 inside_init = error_mark_node;
4947 else if (require_constant && ! TREE_CONSTANT (inside_init))
4949 error_init ("initializer element%s is not constant",
4950 " for `%s'", NULL);
4951 inside_init = error_mark_node;
4953 else if (require_constant
4954 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4956 error_init ("initializer element%s is not computable at load time",
4957 " for `%s'", NULL);
4958 inside_init = error_mark_node;
4961 return inside_init;
4964 /* Handle scalar types, including conversions. */
4966 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4967 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4969 /* Note that convert_for_assignment calls default_conversion
4970 for arrays and functions. We must not call it in the
4971 case where inside_init is a null pointer constant. */
4972 inside_init
4973 = convert_for_assignment (type, init, "initialization",
4974 NULL_TREE, NULL_TREE, 0);
4976 if (require_constant && ! TREE_CONSTANT (inside_init))
4978 error_init ("initializer element%s is not constant",
4979 " for `%s'", NULL);
4980 inside_init = error_mark_node;
4982 else if (require_constant
4983 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4985 error_init ("initializer element%s is not computable at load time",
4986 " for `%s'", NULL);
4987 inside_init = error_mark_node;
4990 return inside_init;
4993 /* Come here only for records and arrays. */
4995 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4997 error_init ("variable-sized object%s may not be initialized",
4998 " `%s'", NULL);
4999 return error_mark_node;
5002 /* Traditionally, you can write struct foo x = 0;
5003 and it initializes the first element of x to 0. */
5004 if (flag_traditional)
5006 tree top = 0, prev = 0, otype = type;
5007 while (TREE_CODE (type) == RECORD_TYPE
5008 || TREE_CODE (type) == ARRAY_TYPE
5009 || TREE_CODE (type) == QUAL_UNION_TYPE
5010 || TREE_CODE (type) == UNION_TYPE)
5012 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
5013 if (prev == 0)
5014 top = temp;
5015 else
5016 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
5017 prev = temp;
5018 if (TREE_CODE (type) == ARRAY_TYPE)
5019 type = TREE_TYPE (type);
5020 else if (TYPE_FIELDS (type))
5021 type = TREE_TYPE (TYPE_FIELDS (type));
5022 else
5024 error_init ("invalid initializer%s", " for `%s'", NULL);
5025 return error_mark_node;
5029 if (otype != type)
5031 TREE_OPERAND (prev, 1)
5032 = build_tree_list (NULL_TREE,
5033 digest_init (type, init, require_constant,
5034 constructor_constant));
5035 return top;
5037 else
5038 return error_mark_node;
5040 error_init ("invalid initializer%s", " for `%s'", NULL);
5041 return error_mark_node;
5044 /* Handle initializers that use braces. */
5046 /* Type of object we are accumulating a constructor for.
5047 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5048 static tree constructor_type;
5050 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5051 left to fill. */
5052 static tree constructor_fields;
5054 /* For an ARRAY_TYPE, this is the specified index
5055 at which to store the next element we get.
5056 This is a special INTEGER_CST node that we modify in place. */
5057 static tree constructor_index;
5059 /* For an ARRAY_TYPE, this is the end index of the range
5060 to initialize with the next element, or NULL in the ordinary case
5061 where the element is used just once. */
5062 static tree constructor_range_end;
5064 /* For an ARRAY_TYPE, this is the maximum index. */
5065 static tree constructor_max_index;
5067 /* For a RECORD_TYPE, this is the first field not yet written out. */
5068 static tree constructor_unfilled_fields;
5070 /* For an ARRAY_TYPE, this is the index of the first element
5071 not yet written out.
5072 This is a special INTEGER_CST node that we modify in place. */
5073 static tree constructor_unfilled_index;
5075 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5076 This is so we can generate gaps between fields, when appropriate.
5077 This is a special INTEGER_CST node that we modify in place. */
5078 static tree constructor_bit_index;
5080 /* If we are saving up the elements rather than allocating them,
5081 this is the list of elements so far (in reverse order,
5082 most recent first). */
5083 static tree constructor_elements;
5085 /* 1 if so far this constructor's elements are all compile-time constants. */
5086 static int constructor_constant;
5088 /* 1 if so far this constructor's elements are all valid address constants. */
5089 static int constructor_simple;
5091 /* 1 if this constructor is erroneous so far. */
5092 static int constructor_erroneous;
5094 /* 1 if have called defer_addressed_constants. */
5095 static int constructor_subconstants_deferred;
5097 /* List of pending elements at this constructor level.
5098 These are elements encountered out of order
5099 which belong at places we haven't reached yet in actually
5100 writing the output. */
5101 static tree constructor_pending_elts;
5103 /* The SPELLING_DEPTH of this constructor. */
5104 static int constructor_depth;
5106 /* 0 if implicitly pushing constructor levels is allowed. */
5107 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
5109 /* 1 if this constructor level was entered implicitly. */
5110 static int constructor_implicit;
5112 static int require_constant_value;
5113 static int require_constant_elements;
5115 /* 1 if it is ok to output this constructor as we read it.
5116 0 means must accumulate a CONSTRUCTOR expression. */
5117 static int constructor_incremental;
5119 /* DECL node for which an initializer is being read.
5120 0 means we are reading a constructor expression
5121 such as (struct foo) {...}. */
5122 static tree constructor_decl;
5124 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
5125 static char *constructor_asmspec;
5127 /* Nonzero if this is an initializer for a top-level decl. */
5128 static int constructor_top_level;
5130 /* When we finish reading a constructor expression
5131 (constructor_decl is 0), the CONSTRUCTOR goes here. */
5132 static tree constructor_result;
5134 /* This stack has a level for each implicit or explicit level of
5135 structuring in the initializer, including the outermost one. It
5136 saves the values of most of the variables above. */
5138 struct constructor_stack
5140 struct constructor_stack *next;
5141 tree type;
5142 tree fields;
5143 tree index;
5144 tree range_end;
5145 tree max_index;
5146 tree unfilled_index;
5147 tree unfilled_fields;
5148 tree bit_index;
5149 tree elements;
5150 int offset;
5151 tree pending_elts;
5152 int depth;
5153 /* If nonzero, this value should replace the entire
5154 constructor at this level. */
5155 tree replacement_value;
5156 char constant;
5157 char simple;
5158 char implicit;
5159 char incremental;
5160 char erroneous;
5161 char outer;
5164 struct constructor_stack *constructor_stack;
5166 /* This stack records separate initializers that are nested.
5167 Nested initializers can't happen in ANSI C, but GNU C allows them
5168 in cases like { ... (struct foo) { ... } ... }. */
5170 struct initializer_stack
5172 struct initializer_stack *next;
5173 tree decl;
5174 char *asmspec;
5175 struct constructor_stack *constructor_stack;
5176 tree elements;
5177 struct spelling *spelling;
5178 struct spelling *spelling_base;
5179 int spelling_size;
5180 char top_level;
5181 char incremental;
5182 char require_constant_value;
5183 char require_constant_elements;
5184 char deferred;
5187 struct initializer_stack *initializer_stack;
5189 /* Prepare to parse and output the initializer for variable DECL. */
5191 void
5192 start_init (decl, asmspec_tree, top_level)
5193 tree decl;
5194 tree asmspec_tree;
5195 int top_level;
5197 char *locus;
5198 struct initializer_stack *p
5199 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5200 char *asmspec = 0;
5202 if (asmspec_tree)
5203 asmspec = TREE_STRING_POINTER (asmspec_tree);
5205 p->decl = constructor_decl;
5206 p->asmspec = constructor_asmspec;
5207 p->incremental = constructor_incremental;
5208 p->require_constant_value = require_constant_value;
5209 p->require_constant_elements = require_constant_elements;
5210 p->constructor_stack = constructor_stack;
5211 p->elements = constructor_elements;
5212 p->spelling = spelling;
5213 p->spelling_base = spelling_base;
5214 p->spelling_size = spelling_size;
5215 p->deferred = constructor_subconstants_deferred;
5216 p->top_level = constructor_top_level;
5217 p->next = initializer_stack;
5218 initializer_stack = p;
5220 constructor_decl = decl;
5221 constructor_incremental = top_level;
5222 constructor_asmspec = asmspec;
5223 constructor_subconstants_deferred = 0;
5224 constructor_top_level = top_level;
5226 if (decl != 0)
5228 require_constant_value = TREE_STATIC (decl);
5229 require_constant_elements
5230 = ((TREE_STATIC (decl) || pedantic)
5231 /* For a scalar, you can always use any value to initialize,
5232 even within braces. */
5233 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5234 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5235 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5236 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5237 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5238 constructor_incremental |= TREE_STATIC (decl);
5240 else
5242 require_constant_value = 0;
5243 require_constant_elements = 0;
5244 locus = "(anonymous)";
5247 constructor_stack = 0;
5249 missing_braces_mentioned = 0;
5251 spelling_base = 0;
5252 spelling_size = 0;
5253 RESTORE_SPELLING_DEPTH (0);
5255 if (locus)
5256 push_string (locus);
5259 void
5260 finish_init ()
5262 struct initializer_stack *p = initializer_stack;
5264 /* Output subconstants (string constants, usually)
5265 that were referenced within this initializer and saved up.
5266 Must do this if and only if we called defer_addressed_constants. */
5267 if (constructor_subconstants_deferred)
5268 output_deferred_addressed_constants ();
5270 /* Free the whole constructor stack of this initializer. */
5271 while (constructor_stack)
5273 struct constructor_stack *q = constructor_stack;
5274 constructor_stack = q->next;
5275 free (q);
5278 /* Pop back to the data of the outer initializer (if any). */
5279 constructor_decl = p->decl;
5280 constructor_asmspec = p->asmspec;
5281 constructor_incremental = p->incremental;
5282 require_constant_value = p->require_constant_value;
5283 require_constant_elements = p->require_constant_elements;
5284 constructor_stack = p->constructor_stack;
5285 constructor_elements = p->elements;
5286 spelling = p->spelling;
5287 spelling_base = p->spelling_base;
5288 spelling_size = p->spelling_size;
5289 constructor_subconstants_deferred = p->deferred;
5290 constructor_top_level = p->top_level;
5291 initializer_stack = p->next;
5292 free (p);
5295 /* Call here when we see the initializer is surrounded by braces.
5296 This is instead of a call to push_init_level;
5297 it is matched by a call to pop_init_level.
5299 TYPE is the type to initialize, for a constructor expression.
5300 For an initializer for a decl, TYPE is zero. */
5302 void
5303 really_start_incremental_init (type)
5304 tree type;
5306 struct constructor_stack *p
5307 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5309 if (type == 0)
5310 type = TREE_TYPE (constructor_decl);
5312 /* Turn off constructor_incremental if type is a struct with bitfields.
5313 Do this before the first push, so that the corrected value
5314 is available in finish_init. */
5315 check_init_type_bitfields (type);
5317 p->type = constructor_type;
5318 p->fields = constructor_fields;
5319 p->index = constructor_index;
5320 p->range_end = constructor_range_end;
5321 p->max_index = constructor_max_index;
5322 p->unfilled_index = constructor_unfilled_index;
5323 p->unfilled_fields = constructor_unfilled_fields;
5324 p->bit_index = constructor_bit_index;
5325 p->elements = constructor_elements;
5326 p->constant = constructor_constant;
5327 p->simple = constructor_simple;
5328 p->erroneous = constructor_erroneous;
5329 p->pending_elts = constructor_pending_elts;
5330 p->depth = constructor_depth;
5331 p->replacement_value = 0;
5332 p->implicit = 0;
5333 p->incremental = constructor_incremental;
5334 p->outer = 0;
5335 p->next = 0;
5336 constructor_stack = p;
5338 constructor_constant = 1;
5339 constructor_simple = 1;
5340 constructor_depth = SPELLING_DEPTH ();
5341 constructor_elements = 0;
5342 constructor_pending_elts = 0;
5343 constructor_type = type;
5345 if (TREE_CODE (constructor_type) == RECORD_TYPE
5346 || TREE_CODE (constructor_type) == UNION_TYPE)
5348 constructor_fields = TYPE_FIELDS (constructor_type);
5349 /* Skip any nameless bit fields at the beginning. */
5350 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5351 && DECL_NAME (constructor_fields) == 0)
5352 constructor_fields = TREE_CHAIN (constructor_fields);
5353 constructor_unfilled_fields = constructor_fields;
5354 constructor_bit_index = copy_node (integer_zero_node);
5356 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5358 constructor_range_end = 0;
5359 if (TYPE_DOMAIN (constructor_type))
5361 constructor_max_index
5362 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5363 constructor_index
5364 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5366 else
5367 constructor_index = copy_node (integer_zero_node);
5368 constructor_unfilled_index = copy_node (constructor_index);
5370 else
5372 /* Handle the case of int x = {5}; */
5373 constructor_fields = constructor_type;
5374 constructor_unfilled_fields = constructor_type;
5377 if (constructor_incremental)
5379 int momentary = suspend_momentary ();
5380 push_obstacks_nochange ();
5381 if (TREE_PERMANENT (constructor_decl))
5382 end_temporary_allocation ();
5383 make_decl_rtl (constructor_decl, constructor_asmspec,
5384 constructor_top_level);
5385 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5386 pop_obstacks ();
5387 resume_momentary (momentary);
5390 if (constructor_incremental)
5392 defer_addressed_constants ();
5393 constructor_subconstants_deferred = 1;
5397 /* Push down into a subobject, for initialization.
5398 If this is for an explicit set of braces, IMPLICIT is 0.
5399 If it is because the next element belongs at a lower level,
5400 IMPLICIT is 1. */
5402 void
5403 push_init_level (implicit)
5404 int implicit;
5406 struct constructor_stack *p;
5408 /* If we've exhausted any levels that didn't have braces,
5409 pop them now. */
5410 while (constructor_stack->implicit)
5412 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5413 || TREE_CODE (constructor_type) == UNION_TYPE)
5414 && constructor_fields == 0)
5415 process_init_element (pop_init_level (1));
5416 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5417 && tree_int_cst_lt (constructor_max_index, constructor_index))
5418 process_init_element (pop_init_level (1));
5419 else
5420 break;
5423 /* Structure elements may require alignment. Do this now if necessary
5424 for the subaggregate, and if it comes next in sequence. Don't do
5425 this for subaggregates that will go on the pending list. */
5426 if (constructor_incremental && constructor_type != 0
5427 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields
5428 && constructor_fields == constructor_unfilled_fields)
5430 /* Advance to offset of this element. */
5431 if (! tree_int_cst_equal (constructor_bit_index,
5432 DECL_FIELD_BITPOS (constructor_fields)))
5434 int next = (TREE_INT_CST_LOW
5435 (DECL_FIELD_BITPOS (constructor_fields))
5436 / BITS_PER_UNIT);
5437 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5438 / BITS_PER_UNIT);
5440 assemble_zeros (next - here);
5442 /* Indicate that we have now filled the structure up to the current
5443 field. */
5444 constructor_unfilled_fields = constructor_fields;
5447 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5448 p->type = constructor_type;
5449 p->fields = constructor_fields;
5450 p->index = constructor_index;
5451 p->range_end = constructor_range_end;
5452 p->max_index = constructor_max_index;
5453 p->unfilled_index = constructor_unfilled_index;
5454 p->unfilled_fields = constructor_unfilled_fields;
5455 p->bit_index = constructor_bit_index;
5456 p->elements = constructor_elements;
5457 p->constant = constructor_constant;
5458 p->simple = constructor_simple;
5459 p->erroneous = constructor_erroneous;
5460 p->pending_elts = constructor_pending_elts;
5461 p->depth = constructor_depth;
5462 p->replacement_value = 0;
5463 p->implicit = implicit;
5464 p->incremental = constructor_incremental;
5465 p->outer = 0;
5466 p->next = constructor_stack;
5467 constructor_stack = p;
5469 constructor_constant = 1;
5470 constructor_simple = 1;
5471 constructor_depth = SPELLING_DEPTH ();
5472 constructor_elements = 0;
5473 constructor_pending_elts = 0;
5475 /* Don't die if an entire brace-pair level is superfluous
5476 in the containing level. */
5477 if (constructor_type == 0)
5479 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5480 || TREE_CODE (constructor_type) == UNION_TYPE)
5482 /* Don't die if there are extra init elts at the end. */
5483 if (constructor_fields == 0)
5484 constructor_type = 0;
5485 else
5487 constructor_type = TREE_TYPE (constructor_fields);
5488 push_member_name (constructor_fields);
5489 constructor_depth++;
5490 if (constructor_fields != constructor_unfilled_fields)
5491 constructor_incremental = 0;
5494 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5496 constructor_type = TREE_TYPE (constructor_type);
5497 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5498 constructor_depth++;
5499 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5500 || constructor_range_end != 0)
5501 constructor_incremental = 0;
5504 if (constructor_type == 0)
5506 error_init ("extra brace group at end of initializer%s",
5507 " for `%s'", NULL);
5508 constructor_fields = 0;
5509 constructor_unfilled_fields = 0;
5510 return;
5513 /* Turn off constructor_incremental if type is a struct with bitfields. */
5514 check_init_type_bitfields (constructor_type);
5516 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5518 missing_braces_mentioned = 1;
5519 warning_init ("missing braces around initializer%s", " for `%s'", NULL);
5522 if (TREE_CODE (constructor_type) == RECORD_TYPE
5523 || TREE_CODE (constructor_type) == UNION_TYPE)
5525 constructor_fields = TYPE_FIELDS (constructor_type);
5526 /* Skip any nameless bit fields at the beginning. */
5527 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5528 && DECL_NAME (constructor_fields) == 0)
5529 constructor_fields = TREE_CHAIN (constructor_fields);
5530 constructor_unfilled_fields = constructor_fields;
5531 constructor_bit_index = copy_node (integer_zero_node);
5533 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5535 constructor_range_end = 0;
5536 if (TYPE_DOMAIN (constructor_type))
5538 constructor_max_index
5539 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5540 constructor_index
5541 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5543 else
5544 constructor_index = copy_node (integer_zero_node);
5545 constructor_unfilled_index = copy_node (constructor_index);
5547 else
5549 warning_init ("braces around scalar initializer%s", " for `%s'", NULL);
5550 constructor_fields = constructor_type;
5551 constructor_unfilled_fields = constructor_type;
5555 /* Don't read a struct incrementally if it has any bitfields,
5556 because the incremental reading code doesn't know how to
5557 handle bitfields yet. */
5559 static void
5560 check_init_type_bitfields (type)
5561 tree type;
5563 if (TREE_CODE (type) == RECORD_TYPE)
5565 tree tail;
5566 for (tail = TYPE_FIELDS (type); tail;
5567 tail = TREE_CHAIN (tail))
5569 if (DECL_C_BIT_FIELD (tail)
5570 /* This catches cases like `int foo : 8;'. */
5571 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail)))
5573 constructor_incremental = 0;
5574 break;
5577 check_init_type_bitfields (TREE_TYPE (tail));
5581 else if (TREE_CODE (type) == ARRAY_TYPE)
5582 check_init_type_bitfields (TREE_TYPE (type));
5585 /* At the end of an implicit or explicit brace level,
5586 finish up that level of constructor.
5587 If we were outputting the elements as they are read, return 0
5588 from inner levels (process_init_element ignores that),
5589 but return error_mark_node from the outermost level
5590 (that's what we want to put in DECL_INITIAL).
5591 Otherwise, return a CONSTRUCTOR expression. */
5593 tree
5594 pop_init_level (implicit)
5595 int implicit;
5597 struct constructor_stack *p;
5598 int size = 0;
5599 tree constructor = 0;
5601 if (implicit == 0)
5603 /* When we come to an explicit close brace,
5604 pop any inner levels that didn't have explicit braces. */
5605 while (constructor_stack->implicit)
5606 process_init_element (pop_init_level (1));
5609 p = constructor_stack;
5611 if (constructor_type != 0)
5612 size = int_size_in_bytes (constructor_type);
5614 /* Now output all pending elements. */
5615 output_pending_init_elements (1);
5617 #if 0 /* c-parse.in warns about {}. */
5618 /* In ANSI, each brace level must have at least one element. */
5619 if (! implicit && pedantic
5620 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5621 ? integer_zerop (constructor_unfilled_index)
5622 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5623 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL);
5624 #endif
5626 /* Pad out the end of the structure. */
5628 if (p->replacement_value)
5630 /* If this closes a superfluous brace pair,
5631 just pass out the element between them. */
5632 constructor = p->replacement_value;
5633 /* If this is the top level thing within the initializer,
5634 and it's for a variable, then since we already called
5635 assemble_variable, we must output the value now. */
5636 if (p->next == 0 && constructor_decl != 0
5637 && constructor_incremental)
5639 constructor = digest_init (constructor_type, constructor,
5640 require_constant_value,
5641 require_constant_elements);
5643 /* If initializing an array of unknown size,
5644 determine the size now. */
5645 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5646 && TYPE_DOMAIN (constructor_type) == 0)
5648 int failure;
5649 int momentary_p;
5651 push_obstacks_nochange ();
5652 if (TREE_PERMANENT (constructor_type))
5653 end_temporary_allocation ();
5655 momentary_p = suspend_momentary ();
5657 /* We shouldn't have an incomplete array type within
5658 some other type. */
5659 if (constructor_stack->next)
5660 abort ();
5662 failure
5663 = complete_array_type (constructor_type,
5664 constructor, 0);
5665 if (failure)
5666 abort ();
5668 size = int_size_in_bytes (constructor_type);
5669 resume_momentary (momentary_p);
5670 pop_obstacks ();
5673 output_constant (constructor, size);
5676 else if (constructor_type == 0)
5678 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5679 && TREE_CODE (constructor_type) != UNION_TYPE
5680 && TREE_CODE (constructor_type) != ARRAY_TYPE
5681 && ! constructor_incremental)
5683 /* A nonincremental scalar initializer--just return
5684 the element, after verifying there is just one. */
5685 if (constructor_elements == 0)
5687 error_init ("empty scalar initializer%s",
5688 " for `%s'", NULL);
5689 constructor = error_mark_node;
5691 else if (TREE_CHAIN (constructor_elements) != 0)
5693 error_init ("extra elements in scalar initializer%s",
5694 " for `%s'", NULL);
5695 constructor = TREE_VALUE (constructor_elements);
5697 else
5698 constructor = TREE_VALUE (constructor_elements);
5700 else if (! constructor_incremental)
5702 if (constructor_erroneous)
5703 constructor = error_mark_node;
5704 else
5706 int momentary = suspend_momentary ();
5708 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5709 nreverse (constructor_elements));
5710 if (constructor_constant)
5711 TREE_CONSTANT (constructor) = 1;
5712 if (constructor_constant && constructor_simple)
5713 TREE_STATIC (constructor) = 1;
5715 resume_momentary (momentary);
5718 else
5720 tree filled;
5721 int momentary = suspend_momentary ();
5723 if (TREE_CODE (constructor_type) == RECORD_TYPE
5724 || TREE_CODE (constructor_type) == UNION_TYPE)
5726 /* Find the offset of the end of that field. */
5727 filled = size_binop (CEIL_DIV_EXPR,
5728 constructor_bit_index,
5729 size_int (BITS_PER_UNIT));
5731 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5733 /* If initializing an array of unknown size,
5734 determine the size now. */
5735 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5736 && TYPE_DOMAIN (constructor_type) == 0)
5738 tree maxindex
5739 = size_binop (MINUS_EXPR,
5740 constructor_unfilled_index,
5741 integer_one_node);
5743 push_obstacks_nochange ();
5744 if (TREE_PERMANENT (constructor_type))
5745 end_temporary_allocation ();
5746 maxindex = copy_node (maxindex);
5747 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5748 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5750 /* TYPE_MAX_VALUE is always one less than the number of elements
5751 in the array, because we start counting at zero. Therefore,
5752 warn only if the value is less than zero. */
5753 if (pedantic
5754 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5755 < 0))
5756 error_with_decl (constructor_decl,
5757 "zero or negative array size `%s'");
5758 layout_type (constructor_type);
5759 size = int_size_in_bytes (constructor_type);
5760 pop_obstacks ();
5763 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5764 size_in_bytes (TREE_TYPE (constructor_type)));
5766 else
5767 filled = 0;
5769 if (filled != 0)
5770 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5772 resume_momentary (momentary);
5776 constructor_type = p->type;
5777 constructor_fields = p->fields;
5778 constructor_index = p->index;
5779 constructor_range_end = p->range_end;
5780 constructor_max_index = p->max_index;
5781 constructor_unfilled_index = p->unfilled_index;
5782 constructor_unfilled_fields = p->unfilled_fields;
5783 constructor_bit_index = p->bit_index;
5784 constructor_elements = p->elements;
5785 constructor_constant = p->constant;
5786 constructor_simple = p->simple;
5787 constructor_erroneous = p->erroneous;
5788 constructor_pending_elts = p->pending_elts;
5789 constructor_depth = p->depth;
5790 constructor_incremental = p->incremental;
5791 RESTORE_SPELLING_DEPTH (constructor_depth);
5793 constructor_stack = p->next;
5794 free (p);
5796 if (constructor == 0)
5798 if (constructor_stack == 0)
5799 return error_mark_node;
5800 return NULL_TREE;
5802 return constructor;
5805 /* Within an array initializer, specify the next index to be initialized.
5806 FIRST is that index. If LAST is nonzero, then initialize a range
5807 of indices, running from FIRST through LAST. */
5809 void
5810 set_init_index (first, last)
5811 tree first, last;
5813 while ((TREE_CODE (first) == NOP_EXPR
5814 || TREE_CODE (first) == CONVERT_EXPR
5815 || TREE_CODE (first) == NON_LVALUE_EXPR)
5816 && (TYPE_MODE (TREE_TYPE (first))
5817 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5818 (first) = TREE_OPERAND (first, 0);
5819 if (last)
5820 while ((TREE_CODE (last) == NOP_EXPR
5821 || TREE_CODE (last) == CONVERT_EXPR
5822 || TREE_CODE (last) == NON_LVALUE_EXPR)
5823 && (TYPE_MODE (TREE_TYPE (last))
5824 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5825 (last) = TREE_OPERAND (last, 0);
5827 if (TREE_CODE (first) != INTEGER_CST)
5828 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5829 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5830 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5831 else if (! constructor_unfilled_index)
5832 error_init ("array index in non-array initializer%s", " for `%s'", NULL);
5833 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5834 error_init ("duplicate array index in initializer%s", " for `%s'", NULL);
5835 else
5837 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (first);
5838 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (first);
5840 if (last != 0 && tree_int_cst_lt (last, first))
5841 error_init ("empty index range in initializer%s", " for `%s'", NULL);
5842 else
5844 if (pedantic)
5845 pedwarn ("ANSI C forbids specifying element to initialize");
5846 constructor_range_end = last;
5851 /* Within a struct initializer, specify the next field to be initialized. */
5853 void
5854 set_init_label (fieldname)
5855 tree fieldname;
5857 tree tail;
5858 int passed = 0;
5860 /* Don't die if an entire brace-pair level is superfluous
5861 in the containing level. */
5862 if (constructor_type == 0)
5863 return;
5865 for (tail = TYPE_FIELDS (constructor_type); tail;
5866 tail = TREE_CHAIN (tail))
5868 if (tail == constructor_unfilled_fields)
5869 passed = 1;
5870 if (DECL_NAME (tail) == fieldname)
5871 break;
5874 if (tail == 0)
5875 error ("unknown field `%s' specified in initializer",
5876 IDENTIFIER_POINTER (fieldname));
5877 else if (!passed)
5878 error ("field `%s' already initialized",
5879 IDENTIFIER_POINTER (fieldname));
5880 else
5882 constructor_fields = tail;
5883 if (pedantic)
5884 pedwarn ("ANSI C forbids specifying structure member to initialize");
5888 /* "Output" the next constructor element.
5889 At top level, really output it to assembler code now.
5890 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5891 TYPE is the data type that the containing data type wants here.
5892 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5894 PENDING if non-nil means output pending elements that belong
5895 right after this element. (PENDING is normally 1;
5896 it is 0 while outputting pending elements, to avoid recursion.) */
5898 static void
5899 output_init_element (value, type, field, pending)
5900 tree value, type, field;
5901 int pending;
5903 int duplicate = 0;
5905 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5906 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5907 && !(TREE_CODE (value) == STRING_CST
5908 && TREE_CODE (type) == ARRAY_TYPE
5909 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5910 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5911 TYPE_MAIN_VARIANT (type))))
5912 value = default_conversion (value);
5914 if (value == error_mark_node)
5915 constructor_erroneous = 1;
5916 else if (!TREE_CONSTANT (value))
5917 constructor_constant = 0;
5918 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5919 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5920 || TREE_CODE (constructor_type) == UNION_TYPE)
5921 && DECL_C_BIT_FIELD (field)
5922 && TREE_CODE (value) != INTEGER_CST))
5923 constructor_simple = 0;
5925 if (require_constant_value && ! TREE_CONSTANT (value))
5927 error_init ("initializer element%s is not constant",
5928 " for `%s'", NULL);
5929 value = error_mark_node;
5931 else if (require_constant_elements
5932 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5934 error_init ("initializer element%s is not computable at load time",
5935 " for `%s'", NULL);
5936 value = error_mark_node;
5939 /* If this element duplicates one on constructor_pending_elts,
5940 print a message and ignore it. Don't do this when we're
5941 processing elements taken off constructor_pending_elts,
5942 because we'd always get spurious errors. */
5943 if (pending)
5945 if (TREE_CODE (constructor_type) == RECORD_TYPE
5946 || TREE_CODE (constructor_type) == UNION_TYPE)
5948 if (purpose_member (field, constructor_pending_elts))
5950 error_init ("duplicate initializer%s", " for `%s'", NULL);
5951 duplicate = 1;
5954 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5956 tree tail;
5957 for (tail = constructor_pending_elts; tail;
5958 tail = TREE_CHAIN (tail))
5959 if (TREE_PURPOSE (tail) != 0
5960 && TREE_CODE (TREE_PURPOSE (tail)) == INTEGER_CST
5961 && tree_int_cst_equal (TREE_PURPOSE (tail), constructor_index))
5962 break;
5964 if (tail != 0)
5966 error_init ("duplicate initializer%s", " for `%s'", NULL);
5967 duplicate = 1;
5972 /* If this element doesn't come next in sequence,
5973 put it on constructor_pending_elts. */
5974 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5975 && !tree_int_cst_equal (field, constructor_unfilled_index))
5977 if (! duplicate)
5978 /* The copy_node is needed in case field is actually
5979 constructor_index, which is modified in place. */
5980 constructor_pending_elts
5981 = tree_cons (copy_node (field),
5982 digest_init (type, value, require_constant_value,
5983 require_constant_elements),
5984 constructor_pending_elts);
5986 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5987 && field != constructor_unfilled_fields)
5989 /* We do this for records but not for unions. In a union,
5990 no matter which field is specified, it can be initialized
5991 right away since it starts at the beginning of the union. */
5992 if (!duplicate)
5993 constructor_pending_elts
5994 = tree_cons (field,
5995 digest_init (type, value, require_constant_value,
5996 require_constant_elements),
5997 constructor_pending_elts);
5999 else
6001 /* Otherwise, output this element either to
6002 constructor_elements or to the assembler file. */
6004 if (!duplicate)
6006 if (! constructor_incremental)
6008 if (field && TREE_CODE (field) == INTEGER_CST)
6009 field = copy_node (field);
6010 constructor_elements
6011 = tree_cons (field, digest_init (type, value,
6012 require_constant_value,
6013 require_constant_elements),
6014 constructor_elements);
6016 else
6018 /* Structure elements may require alignment.
6019 Do this, if necessary. */
6020 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6022 /* Advance to offset of this element. */
6023 if (! tree_int_cst_equal (constructor_bit_index,
6024 DECL_FIELD_BITPOS (field)))
6026 int next = (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
6027 / BITS_PER_UNIT);
6028 int here = (TREE_INT_CST_LOW (constructor_bit_index)
6029 / BITS_PER_UNIT);
6031 assemble_zeros (next - here);
6034 output_constant (digest_init (type, value,
6035 require_constant_value,
6036 require_constant_elements),
6037 int_size_in_bytes (type));
6039 /* For a record or union,
6040 keep track of end position of last field. */
6041 if (TREE_CODE (constructor_type) == RECORD_TYPE
6042 || TREE_CODE (constructor_type) == UNION_TYPE)
6044 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
6045 DECL_SIZE (field));
6046 TREE_INT_CST_LOW (constructor_bit_index)
6047 = TREE_INT_CST_LOW (temp);
6048 TREE_INT_CST_HIGH (constructor_bit_index)
6049 = TREE_INT_CST_HIGH (temp);
6054 /* Advance the variable that indicates sequential elements output. */
6055 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6057 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
6058 integer_one_node);
6059 TREE_INT_CST_LOW (constructor_unfilled_index)
6060 = TREE_INT_CST_LOW (tem);
6061 TREE_INT_CST_HIGH (constructor_unfilled_index)
6062 = TREE_INT_CST_HIGH (tem);
6064 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6065 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6066 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6067 constructor_unfilled_fields = 0;
6069 /* Now output any pending elements which have become next. */
6070 if (pending)
6071 output_pending_init_elements (0);
6075 /* Output any pending elements which have become next.
6076 As we output elements, constructor_unfilled_{fields,index}
6077 advances, which may cause other elements to become next;
6078 if so, they too are output.
6080 If ALL is 0, we return when there are
6081 no more pending elements to output now.
6083 If ALL is 1, we output space as necessary so that
6084 we can output all the pending elements. */
6086 static void
6087 output_pending_init_elements (all)
6088 int all;
6090 tree tail;
6091 tree next;
6093 retry:
6095 /* Look thru the whole pending list.
6096 If we find an element that should be output now,
6097 output it. Otherwise, set NEXT to the element
6098 that comes first among those still pending. */
6100 next = 0;
6101 for (tail = constructor_pending_elts; tail;
6102 tail = TREE_CHAIN (tail))
6104 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6106 if (tree_int_cst_equal (TREE_PURPOSE (tail),
6107 constructor_unfilled_index))
6109 output_init_element (TREE_VALUE (tail),
6110 TREE_TYPE (constructor_type),
6111 constructor_unfilled_index, 0);
6112 goto retry;
6114 else if (tree_int_cst_lt (TREE_PURPOSE (tail),
6115 constructor_unfilled_index))
6117 else if (next == 0
6118 || tree_int_cst_lt (TREE_PURPOSE (tail), next))
6119 next = TREE_PURPOSE (tail);
6121 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6122 || TREE_CODE (constructor_type) == UNION_TYPE)
6124 if (TREE_PURPOSE (tail) == constructor_unfilled_fields)
6126 output_init_element (TREE_VALUE (tail),
6127 TREE_TYPE (constructor_unfilled_fields),
6128 constructor_unfilled_fields,
6130 goto retry;
6132 else if (constructor_unfilled_fields == 0
6133 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
6134 DECL_FIELD_BITPOS (constructor_unfilled_fields)))
6136 else if (next == 0
6137 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
6138 DECL_FIELD_BITPOS (next)))
6139 next = TREE_PURPOSE (tail);
6143 /* Ordinarily return, but not if we want to output all
6144 and there are elements left. */
6145 if (! (all && next != 0))
6146 return;
6148 /* Generate space up to the position of NEXT. */
6149 if (constructor_incremental)
6151 tree filled;
6152 tree nextpos_tree = size_int (0);
6154 if (TREE_CODE (constructor_type) == RECORD_TYPE
6155 || TREE_CODE (constructor_type) == UNION_TYPE)
6157 /* Find the last field written out, if any. */
6158 for (tail = TYPE_FIELDS (constructor_type); tail;
6159 tail = TREE_CHAIN (tail))
6160 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
6161 break;
6163 if (tail)
6164 /* Find the offset of the end of that field. */
6165 filled = size_binop (CEIL_DIV_EXPR,
6166 size_binop (PLUS_EXPR,
6167 DECL_FIELD_BITPOS (tail),
6168 DECL_SIZE (tail)),
6169 size_int (BITS_PER_UNIT));
6170 else
6171 filled = size_int (0);
6173 nextpos_tree = size_binop (CEIL_DIV_EXPR,
6174 DECL_FIELD_BITPOS (next),
6175 size_int (BITS_PER_UNIT));
6177 TREE_INT_CST_HIGH (constructor_bit_index)
6178 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
6179 TREE_INT_CST_LOW (constructor_bit_index)
6180 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
6181 constructor_unfilled_fields = next;
6183 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6185 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
6186 size_in_bytes (TREE_TYPE (constructor_type)));
6187 nextpos_tree
6188 = size_binop (MULT_EXPR, next,
6189 size_in_bytes (TREE_TYPE (constructor_type)));
6190 TREE_INT_CST_LOW (constructor_unfilled_index)
6191 = TREE_INT_CST_LOW (next);
6192 TREE_INT_CST_HIGH (constructor_unfilled_index)
6193 = TREE_INT_CST_HIGH (next);
6195 else
6196 filled = 0;
6198 if (filled)
6200 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
6202 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
6205 else
6207 /* If it's not incremental, just skip over the gap,
6208 so that after jumping to retry we will output the next
6209 successive element. */
6210 if (TREE_CODE (constructor_type) == RECORD_TYPE
6211 || TREE_CODE (constructor_type) == UNION_TYPE)
6212 constructor_unfilled_fields = next;
6213 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6215 TREE_INT_CST_LOW (constructor_unfilled_index)
6216 = TREE_INT_CST_LOW (next);
6217 TREE_INT_CST_HIGH (constructor_unfilled_index)
6218 = TREE_INT_CST_HIGH (next);
6222 goto retry;
6225 /* Add one non-braced element to the current constructor level.
6226 This adjusts the current position within the constructor's type.
6227 This may also start or terminate implicit levels
6228 to handle a partly-braced initializer.
6230 Once this has found the correct level for the new element,
6231 it calls output_init_element.
6233 Note: if we are incrementally outputting this constructor,
6234 this function may be called with a null argument
6235 representing a sub-constructor that was already incrementally output.
6236 When that happens, we output nothing, but we do the bookkeeping
6237 to skip past that element of the current constructor. */
6239 void
6240 process_init_element (value)
6241 tree value;
6243 tree orig_value = value;
6244 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6246 /* Handle superfluous braces around string cst as in
6247 char x[] = {"foo"}; */
6248 if (string_flag
6249 && constructor_type
6250 && TREE_CODE (constructor_type) == ARRAY_TYPE
6251 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6252 && integer_zerop (constructor_unfilled_index))
6254 constructor_stack->replacement_value = value;
6255 return;
6258 if (constructor_stack->replacement_value != 0)
6260 error_init ("excess elements in struct initializer%s",
6261 " after `%s'", NULL_PTR);
6262 return;
6265 /* Ignore elements of a brace group if it is entirely superfluous
6266 and has already been diagnosed. */
6267 if (constructor_type == 0)
6268 return;
6270 /* If we've exhausted any levels that didn't have braces,
6271 pop them now. */
6272 while (constructor_stack->implicit)
6274 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6275 || TREE_CODE (constructor_type) == UNION_TYPE)
6276 && constructor_fields == 0)
6277 process_init_element (pop_init_level (1));
6278 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6279 && (constructor_max_index == 0
6280 || tree_int_cst_lt (constructor_max_index,
6281 constructor_index)))
6282 process_init_element (pop_init_level (1));
6283 else
6284 break;
6287 while (1)
6289 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6291 tree fieldtype;
6292 enum tree_code fieldcode;
6294 if (constructor_fields == 0)
6296 pedwarn_init ("excess elements in struct initializer%s",
6297 " after `%s'", NULL_PTR);
6298 break;
6301 fieldtype = TREE_TYPE (constructor_fields);
6302 if (fieldtype != error_mark_node)
6303 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6304 fieldcode = TREE_CODE (fieldtype);
6306 /* Accept a string constant to initialize a subarray. */
6307 if (value != 0
6308 && fieldcode == ARRAY_TYPE
6309 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6310 && string_flag)
6311 value = orig_value;
6312 /* Otherwise, if we have come to a subaggregate,
6313 and we don't have an element of its type, push into it. */
6314 else if (value != 0 && !constructor_no_implicit
6315 && value != error_mark_node
6316 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6317 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6318 || fieldcode == UNION_TYPE))
6320 push_init_level (1);
6321 continue;
6324 if (value)
6326 push_member_name (constructor_fields);
6327 output_init_element (value, fieldtype, constructor_fields, 1);
6328 RESTORE_SPELLING_DEPTH (constructor_depth);
6330 else
6331 /* Do the bookkeeping for an element that was
6332 directly output as a constructor. */
6334 /* For a record, keep track of end position of last field. */
6335 tree temp = size_binop (PLUS_EXPR,
6336 DECL_FIELD_BITPOS (constructor_fields),
6337 DECL_SIZE (constructor_fields));
6338 TREE_INT_CST_LOW (constructor_bit_index)
6339 = TREE_INT_CST_LOW (temp);
6340 TREE_INT_CST_HIGH (constructor_bit_index)
6341 = TREE_INT_CST_HIGH (temp);
6343 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6346 constructor_fields = TREE_CHAIN (constructor_fields);
6347 /* Skip any nameless bit fields at the beginning. */
6348 while (constructor_fields != 0
6349 && DECL_C_BIT_FIELD (constructor_fields)
6350 && DECL_NAME (constructor_fields) == 0)
6351 constructor_fields = TREE_CHAIN (constructor_fields);
6352 break;
6354 if (TREE_CODE (constructor_type) == UNION_TYPE)
6356 tree fieldtype;
6357 enum tree_code fieldcode;
6359 if (constructor_fields == 0)
6361 pedwarn_init ("excess elements in union initializer%s",
6362 " after `%s'", NULL_PTR);
6363 break;
6366 fieldtype = TREE_TYPE (constructor_fields);
6367 if (fieldtype != error_mark_node)
6368 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6369 fieldcode = TREE_CODE (fieldtype);
6371 /* Accept a string constant to initialize a subarray. */
6372 if (value != 0
6373 && fieldcode == ARRAY_TYPE
6374 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6375 && string_flag)
6376 value = orig_value;
6377 /* Otherwise, if we have come to a subaggregate,
6378 and we don't have an element of its type, push into it. */
6379 else if (value != 0 && !constructor_no_implicit
6380 && value != error_mark_node
6381 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6382 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6383 || fieldcode == UNION_TYPE))
6385 push_init_level (1);
6386 continue;
6389 if (value)
6391 push_member_name (constructor_fields);
6392 output_init_element (value, fieldtype, constructor_fields, 1);
6393 RESTORE_SPELLING_DEPTH (constructor_depth);
6395 else
6396 /* Do the bookkeeping for an element that was
6397 directly output as a constructor. */
6399 TREE_INT_CST_LOW (constructor_bit_index)
6400 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6401 TREE_INT_CST_HIGH (constructor_bit_index)
6402 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6404 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6407 constructor_fields = 0;
6408 break;
6410 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6412 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6413 enum tree_code eltcode = TREE_CODE (elttype);
6415 /* Accept a string constant to initialize a subarray. */
6416 if (value != 0
6417 && eltcode == ARRAY_TYPE
6418 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6419 && string_flag)
6420 value = orig_value;
6421 /* Otherwise, if we have come to a subaggregate,
6422 and we don't have an element of its type, push into it. */
6423 else if (value != 0 && !constructor_no_implicit
6424 && value != error_mark_node
6425 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6426 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6427 || eltcode == UNION_TYPE))
6429 push_init_level (1);
6430 continue;
6433 if (constructor_max_index != 0
6434 && tree_int_cst_lt (constructor_max_index, constructor_index))
6436 pedwarn_init ("excess elements in array initializer%s",
6437 " after `%s'", NULL_PTR);
6438 break;
6441 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */
6442 if (constructor_range_end)
6444 if (constructor_max_index != 0
6445 && tree_int_cst_lt (constructor_max_index,
6446 constructor_range_end))
6448 pedwarn_init ("excess elements in array initializer%s",
6449 " after `%s'", NULL_PTR);
6450 TREE_INT_CST_HIGH (constructor_range_end)
6451 = TREE_INT_CST_HIGH (constructor_max_index);
6452 TREE_INT_CST_LOW (constructor_range_end)
6453 = TREE_INT_CST_LOW (constructor_max_index);
6456 value = save_expr (value);
6459 /* Now output the actual element.
6460 Ordinarily, output once.
6461 If there is a range, repeat it till we advance past the range. */
6464 tree tem;
6466 if (value)
6468 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6469 output_init_element (value, elttype, constructor_index, 1);
6470 RESTORE_SPELLING_DEPTH (constructor_depth);
6473 tem = size_binop (PLUS_EXPR, constructor_index,
6474 integer_one_node);
6475 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (tem);
6476 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (tem);
6478 if (!value)
6479 /* If we are doing the bookkeeping for an element that was
6480 directly output as a constructor,
6481 we must update constructor_unfilled_index. */
6483 TREE_INT_CST_LOW (constructor_unfilled_index)
6484 = TREE_INT_CST_LOW (constructor_index);
6485 TREE_INT_CST_HIGH (constructor_unfilled_index)
6486 = TREE_INT_CST_HIGH (constructor_index);
6489 while (! (constructor_range_end == 0
6490 || tree_int_cst_lt (constructor_range_end,
6491 constructor_index)));
6493 break;
6496 /* Handle the sole element allowed in a braced initializer
6497 for a scalar variable. */
6498 if (constructor_fields == 0)
6500 pedwarn_init ("excess elements in scalar initializer%s",
6501 " after `%s'", NULL_PTR);
6502 break;
6505 if (value)
6506 output_init_element (value, constructor_type, NULL_TREE, 1);
6507 constructor_fields = 0;
6508 break;
6511 /* If the (lexically) previous elments are not now saved,
6512 we can discard the storage for them. */
6513 if (constructor_incremental && constructor_pending_elts == 0 && value != 0
6514 && constructor_stack == 0)
6515 clear_momentary ();
6518 /* Expand an ASM statement with operands, handling output operands
6519 that are not variables or INDIRECT_REFS by transforming such
6520 cases into cases that expand_asm_operands can handle.
6522 Arguments are same as for expand_asm_operands. */
6524 void
6525 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6526 tree string, outputs, inputs, clobbers;
6527 int vol;
6528 char *filename;
6529 int line;
6531 int noutputs = list_length (outputs);
6532 register int i;
6533 /* o[I] is the place that output number I should be written. */
6534 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6535 register tree tail;
6537 if (TREE_CODE (string) == ADDR_EXPR)
6538 string = TREE_OPERAND (string, 0);
6539 if (TREE_CODE (string) != STRING_CST)
6541 error ("asm template is not a string constant");
6542 return;
6545 /* Record the contents of OUTPUTS before it is modified. */
6546 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6547 o[i] = TREE_VALUE (tail);
6549 /* Perform default conversions on array and function inputs. */
6550 /* Don't do this for other types--
6551 it would screw up operands expected to be in memory. */
6552 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6553 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6554 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6555 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6557 /* Generate the ASM_OPERANDS insn;
6558 store into the TREE_VALUEs of OUTPUTS some trees for
6559 where the values were actually stored. */
6560 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6562 /* Copy all the intermediate outputs into the specified outputs. */
6563 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6565 if (o[i] != TREE_VALUE (tail))
6567 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6568 0, VOIDmode, 0);
6569 free_temp_slots ();
6571 /* Detect modification of read-only values.
6572 (Otherwise done by build_modify_expr.) */
6573 else
6575 tree type = TREE_TYPE (o[i]);
6576 if (TREE_READONLY (o[i])
6577 || TYPE_READONLY (type)
6578 || ((TREE_CODE (type) == RECORD_TYPE
6579 || TREE_CODE (type) == UNION_TYPE)
6580 && C_TYPE_FIELDS_READONLY (type)))
6581 readonly_warning (o[i], "modification by `asm'");
6585 /* Those MODIFY_EXPRs could do autoincrements. */
6586 emit_queue ();
6589 /* Expand a C `return' statement.
6590 RETVAL is the expression for what to return,
6591 or a null pointer for `return;' with no value. */
6593 void
6594 c_expand_return (retval)
6595 tree retval;
6597 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6599 if (TREE_THIS_VOLATILE (current_function_decl))
6600 warning ("function declared `noreturn' has a `return' statement");
6602 if (!retval)
6604 current_function_returns_null = 1;
6605 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6606 warning ("`return' with no value, in function returning non-void");
6607 expand_null_return ();
6609 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6611 current_function_returns_null = 1;
6612 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6613 pedwarn ("`return' with a value, in function returning void");
6614 expand_return (retval);
6616 else
6618 tree t = convert_for_assignment (valtype, retval, "return",
6619 NULL_TREE, NULL_TREE, 0);
6620 tree res = DECL_RESULT (current_function_decl);
6621 tree inner;
6623 if (t == error_mark_node)
6624 return;
6626 inner = t = convert (TREE_TYPE (res), t);
6628 /* Strip any conversions, additions, and subtractions, and see if
6629 we are returning the address of a local variable. Warn if so. */
6630 while (1)
6632 switch (TREE_CODE (inner))
6634 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6635 case PLUS_EXPR:
6636 inner = TREE_OPERAND (inner, 0);
6637 continue;
6639 case MINUS_EXPR:
6640 /* If the second operand of the MINUS_EXPR has a pointer
6641 type (or is converted from it), this may be valid, so
6642 don't give a warning. */
6644 tree op1 = TREE_OPERAND (inner, 1);
6646 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6647 && (TREE_CODE (op1) == NOP_EXPR
6648 || TREE_CODE (op1) == NON_LVALUE_EXPR
6649 || TREE_CODE (op1) == CONVERT_EXPR))
6650 op1 = TREE_OPERAND (op1, 0);
6652 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6653 break;
6655 inner = TREE_OPERAND (inner, 0);
6656 continue;
6659 case ADDR_EXPR:
6660 inner = TREE_OPERAND (inner, 0);
6662 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6663 inner = TREE_OPERAND (inner, 0);
6665 if (TREE_CODE (inner) == VAR_DECL
6666 && ! DECL_EXTERNAL (inner)
6667 && ! TREE_STATIC (inner)
6668 && DECL_CONTEXT (inner) == current_function_decl)
6669 warning ("function returns address of local variable");
6670 break;
6672 default:
6673 break;
6676 break;
6679 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6680 TREE_SIDE_EFFECTS (t) = 1;
6681 expand_return (t);
6682 current_function_returns_value = 1;
6686 /* Start a C switch statement, testing expression EXP.
6687 Return EXP if it is valid, an error node otherwise. */
6689 tree
6690 c_expand_start_case (exp)
6691 tree exp;
6693 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6694 tree type = TREE_TYPE (exp);
6696 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6698 error ("switch quantity not an integer");
6699 exp = error_mark_node;
6701 else
6703 tree index;
6704 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6706 if (warn_traditional
6707 && (type == long_integer_type_node
6708 || type == long_unsigned_type_node))
6709 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6711 exp = default_conversion (exp);
6712 type = TREE_TYPE (exp);
6713 index = get_unwidened (exp, NULL_TREE);
6714 /* We can't strip a conversion from a signed type to an unsigned,
6715 because if we did, int_fits_type_p would do the wrong thing
6716 when checking case values for being in range,
6717 and it's too hard to do the right thing. */
6718 if (TREE_UNSIGNED (TREE_TYPE (exp))
6719 == TREE_UNSIGNED (TREE_TYPE (index)))
6720 exp = index;
6723 expand_start_case (1, exp, type, "switch statement");
6725 return exp;