* rtl.h (rtunion_def): Constify member `rtstr'.
[official-gcc.git] / gcc / c-typeck.c
blob2cac8875ca5573c978239ae2ee994afccb0ed8a3
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
32 #include "config.h"
33 #include "system.h"
34 #include "tree.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "rtl.h"
40 #include "expr.h"
41 #include "toplev.h"
42 #include "intl.h"
43 #include "defaults.h"
44 #include "ggc.h"
46 /* Nonzero if we've already printed a "missing braces around initializer"
47 message within this initializer. */
48 static int missing_braces_mentioned;
50 static tree qualify_type PARAMS ((tree, tree));
51 static int comp_target_types PARAMS ((tree, tree));
52 static int function_types_compatible_p PARAMS ((tree, tree));
53 static int type_lists_compatible_p PARAMS ((tree, tree));
54 static tree decl_constant_value PARAMS ((tree));
55 static tree lookup_field PARAMS ((tree, tree, tree *));
56 static tree convert_arguments PARAMS ((tree, tree, tree, tree));
57 static tree pointer_int_sum PARAMS ((enum tree_code, tree, tree));
58 static tree pointer_diff PARAMS ((tree, tree));
59 static tree unary_complex_lvalue PARAMS ((enum tree_code, tree));
60 static void pedantic_lvalue_warning PARAMS ((enum tree_code));
61 static tree internal_build_compound_expr PARAMS ((tree, int));
62 static tree convert_for_assignment PARAMS ((tree, tree, const char *,
63 tree, tree, int));
64 static void warn_for_assignment PARAMS ((const char *, const char *,
65 tree, int));
66 static tree valid_compound_expr_initializer PARAMS ((tree, tree));
67 static void push_string PARAMS ((const char *));
68 static void push_member_name PARAMS ((tree));
69 static void push_array_bounds PARAMS ((int));
70 static int spelling_length PARAMS ((void));
71 static char *print_spelling PARAMS ((char *));
72 static void warning_init PARAMS ((const char *));
73 static tree digest_init PARAMS ((tree, tree, int, int));
74 static void check_init_type_bitfields PARAMS ((tree));
75 static void output_init_element PARAMS ((tree, tree, tree, int));
76 static void output_pending_init_elements PARAMS ((int));
77 static void add_pending_init PARAMS ((tree, tree));
78 static int pending_init_member PARAMS ((tree));
80 /* Do `exp = require_complete_type (exp);' to make sure exp
81 does not have an incomplete type. (That includes void types.) */
83 tree
84 require_complete_type (value)
85 tree value;
87 tree type = TREE_TYPE (value);
89 if (TREE_CODE (value) == ERROR_MARK)
90 return error_mark_node;
92 /* First, detect a valid value with a complete type. */
93 if (TYPE_SIZE (type) != 0
94 && type != void_type_node)
95 return value;
97 incomplete_type_error (value, type);
98 return error_mark_node;
101 /* Print an error message for invalid use of an incomplete type.
102 VALUE is the expression that was used (or 0 if that isn't known)
103 and TYPE is the type that was invalid. */
105 void
106 incomplete_type_error (value, type)
107 tree value;
108 tree type;
110 const char *type_code_string;
112 /* Avoid duplicate error message. */
113 if (TREE_CODE (type) == ERROR_MARK)
114 return;
116 if (value != 0 && (TREE_CODE (value) == VAR_DECL
117 || TREE_CODE (value) == PARM_DECL))
118 error ("`%s' has an incomplete type",
119 IDENTIFIER_POINTER (DECL_NAME (value)));
120 else
122 retry:
123 /* We must print an error message. Be clever about what it says. */
125 switch (TREE_CODE (type))
127 case RECORD_TYPE:
128 type_code_string = "struct";
129 break;
131 case UNION_TYPE:
132 type_code_string = "union";
133 break;
135 case ENUMERAL_TYPE:
136 type_code_string = "enum";
137 break;
139 case VOID_TYPE:
140 error ("invalid use of void expression");
141 return;
143 case ARRAY_TYPE:
144 if (TYPE_DOMAIN (type))
146 type = TREE_TYPE (type);
147 goto retry;
149 error ("invalid use of array with unspecified bounds");
150 return;
152 default:
153 abort ();
156 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
157 error ("invalid use of undefined type `%s %s'",
158 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
159 else
160 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
161 error ("invalid use of incomplete typedef `%s'",
162 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
166 /* Return a variant of TYPE which has all the type qualifiers of LIKE
167 as well as those of TYPE. */
169 static tree
170 qualify_type (type, like)
171 tree type, like;
173 return c_build_qualified_type (type,
174 TYPE_QUALS (type) | TYPE_QUALS (like));
177 /* Return the common type of two types.
178 We assume that comptypes has already been done and returned 1;
179 if that isn't so, this may crash. In particular, we assume that qualifiers
180 match.
182 This is the type for the result of most arithmetic operations
183 if the operands have the given two types. */
185 tree
186 common_type (t1, t2)
187 tree t1, t2;
189 register enum tree_code code1;
190 register enum tree_code code2;
191 tree attributes;
193 /* Save time if the two types are the same. */
195 if (t1 == t2) return t1;
197 /* If one type is nonsense, use the other. */
198 if (t1 == error_mark_node)
199 return t2;
200 if (t2 == error_mark_node)
201 return t1;
203 /* Merge the attributes. */
204 attributes = merge_machine_type_attributes (t1, t2);
206 /* Treat an enum type as the unsigned integer type of the same width. */
208 if (TREE_CODE (t1) == ENUMERAL_TYPE)
209 t1 = type_for_size (TYPE_PRECISION (t1), 1);
210 if (TREE_CODE (t2) == ENUMERAL_TYPE)
211 t2 = type_for_size (TYPE_PRECISION (t2), 1);
213 code1 = TREE_CODE (t1);
214 code2 = TREE_CODE (t2);
216 /* If one type is complex, form the common type of the non-complex
217 components, then make that complex. Use T1 or T2 if it is the
218 required type. */
219 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
221 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
222 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
223 tree subtype = common_type (subtype1, subtype2);
225 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
226 return build_type_attribute_variant (t1, attributes);
227 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
228 return build_type_attribute_variant (t2, attributes);
229 else
230 return build_type_attribute_variant (build_complex_type (subtype),
231 attributes);
234 switch (code1)
236 case INTEGER_TYPE:
237 case REAL_TYPE:
238 /* If only one is real, use it as the result. */
240 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
241 return build_type_attribute_variant (t1, attributes);
243 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
244 return build_type_attribute_variant (t2, attributes);
246 /* Both real or both integers; use the one with greater precision. */
248 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
249 return build_type_attribute_variant (t1, attributes);
250 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
251 return build_type_attribute_variant (t2, attributes);
253 /* Same precision. Prefer longs to ints even when same size. */
255 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
256 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
257 return build_type_attribute_variant (long_unsigned_type_node,
258 attributes);
260 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
261 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
263 /* But preserve unsignedness from the other type,
264 since long cannot hold all the values of an unsigned int. */
265 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
266 t1 = long_unsigned_type_node;
267 else
268 t1 = long_integer_type_node;
269 return build_type_attribute_variant (t1, attributes);
272 /* Likewise, prefer long double to double even if same size. */
273 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
274 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
275 return build_type_attribute_variant (long_double_type_node,
276 attributes);
278 /* Otherwise prefer the unsigned one. */
280 if (TREE_UNSIGNED (t1))
281 return build_type_attribute_variant (t1, attributes);
282 else
283 return build_type_attribute_variant (t2, attributes);
285 case POINTER_TYPE:
286 /* For two pointers, do this recursively on the target type,
287 and combine the qualifiers of the two types' targets. */
288 /* This code was turned off; I don't know why.
289 But ANSI C specifies doing this with the qualifiers.
290 So I turned it on again. */
292 tree pointed_to_1 = TREE_TYPE (t1);
293 tree pointed_to_2 = TREE_TYPE (t2);
294 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
295 TYPE_MAIN_VARIANT (pointed_to_2));
296 t1 = build_pointer_type (c_build_qualified_type
297 (target,
298 TYPE_QUALS (pointed_to_1) |
299 TYPE_QUALS (pointed_to_2)));
300 return build_type_attribute_variant (t1, attributes);
302 #if 0
303 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
304 return build_type_attribute_variant (t1, attributes);
305 #endif
307 case ARRAY_TYPE:
309 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
310 /* Save space: see if the result is identical to one of the args. */
311 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
312 return build_type_attribute_variant (t1, attributes);
313 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
314 return build_type_attribute_variant (t2, attributes);
315 /* Merge the element types, and have a size if either arg has one. */
316 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
317 return build_type_attribute_variant (t1, attributes);
320 case FUNCTION_TYPE:
321 /* Function types: prefer the one that specified arg types.
322 If both do, merge the arg types. Also merge the return types. */
324 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
325 tree p1 = TYPE_ARG_TYPES (t1);
326 tree p2 = TYPE_ARG_TYPES (t2);
327 int len;
328 tree newargs, n;
329 int i;
331 /* Save space: see if the result is identical to one of the args. */
332 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
333 return build_type_attribute_variant (t1, attributes);
334 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
335 return build_type_attribute_variant (t2, attributes);
337 /* Simple way if one arg fails to specify argument types. */
338 if (TYPE_ARG_TYPES (t1) == 0)
340 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
341 return build_type_attribute_variant (t1, attributes);
343 if (TYPE_ARG_TYPES (t2) == 0)
345 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
346 return build_type_attribute_variant (t1, attributes);
349 /* If both args specify argument types, we must merge the two
350 lists, argument by argument. */
352 len = list_length (p1);
353 newargs = 0;
355 for (i = 0; i < len; i++)
356 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
358 n = newargs;
360 for (; p1;
361 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
363 /* A null type means arg type is not specified.
364 Take whatever the other function type has. */
365 if (TREE_VALUE (p1) == 0)
367 TREE_VALUE (n) = TREE_VALUE (p2);
368 goto parm_done;
370 if (TREE_VALUE (p2) == 0)
372 TREE_VALUE (n) = TREE_VALUE (p1);
373 goto parm_done;
376 /* Given wait (union {union wait *u; int *i} *)
377 and wait (union wait *),
378 prefer union wait * as type of parm. */
379 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
380 && TREE_VALUE (p1) != TREE_VALUE (p2))
382 tree memb;
383 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
384 memb; memb = TREE_CHAIN (memb))
385 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
387 TREE_VALUE (n) = TREE_VALUE (p2);
388 if (pedantic)
389 pedwarn ("function types not truly compatible in ANSI C");
390 goto parm_done;
393 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
394 && TREE_VALUE (p2) != TREE_VALUE (p1))
396 tree memb;
397 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
398 memb; memb = TREE_CHAIN (memb))
399 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
401 TREE_VALUE (n) = TREE_VALUE (p1);
402 if (pedantic)
403 pedwarn ("function types not truly compatible in ANSI C");
404 goto parm_done;
407 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
408 parm_done: ;
411 t1 = build_function_type (valtype, newargs);
412 /* ... falls through ... */
415 default:
416 return build_type_attribute_variant (t1, attributes);
421 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
422 or various other operations. Return 2 if they are compatible
423 but a warning may be needed if you use them together. */
426 comptypes (type1, type2)
427 tree type1, type2;
429 register tree t1 = type1;
430 register tree t2 = type2;
431 int attrval, val;
433 /* Suppress errors caused by previously reported errors. */
435 if (t1 == t2 || !t1 || !t2
436 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
437 return 1;
439 /* If either type is the internal version of sizetype, return the
440 language version. */
441 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
442 && TYPE_DOMAIN (t1) != 0)
443 t1 = TYPE_DOMAIN (t1);
445 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
446 && TYPE_DOMAIN (t2) != 0)
447 t2 = TYPE_DOMAIN (t2);
449 /* Treat an enum type as the integer type of the same width and
450 signedness. */
452 if (TREE_CODE (t1) == ENUMERAL_TYPE)
453 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
454 if (TREE_CODE (t2) == ENUMERAL_TYPE)
455 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
457 if (t1 == t2)
458 return 1;
460 /* Different classes of types can't be compatible. */
462 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
464 /* Qualifiers must match. */
466 if (TYPE_QUALS (t1) != TYPE_QUALS (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 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_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
518 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
519 val = 0;
521 break;
524 case RECORD_TYPE:
525 if (maybe_objc_comptypes (t1, t2, 0) == 1)
526 val = 1;
527 break;
529 default:
530 break;
532 return attrval == 2 && val == 1 ? 2 : val;
535 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
536 ignoring their qualifiers. */
538 static int
539 comp_target_types (ttl, ttr)
540 tree ttl, ttr;
542 int val;
544 /* Give maybe_objc_comptypes a crack at letting these types through. */
545 if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0)
546 return val;
548 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
549 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
551 if (val == 2 && pedantic)
552 pedwarn ("types are not quite compatible");
553 return val;
556 /* Subroutines of `comptypes'. */
558 /* Return 1 if two function types F1 and F2 are compatible.
559 If either type specifies no argument types,
560 the other must specify a fixed number of self-promoting arg types.
561 Otherwise, if one type specifies only the number of arguments,
562 the other must specify that number of self-promoting arg types.
563 Otherwise, the argument types must match. */
565 static int
566 function_types_compatible_p (f1, f2)
567 tree f1, f2;
569 tree args1, args2;
570 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
571 int val = 1;
572 int val1;
574 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
575 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
576 return 0;
578 args1 = TYPE_ARG_TYPES (f1);
579 args2 = TYPE_ARG_TYPES (f2);
581 /* An unspecified parmlist matches any specified parmlist
582 whose argument types don't need default promotions. */
584 if (args1 == 0)
586 if (!self_promoting_args_p (args2))
587 return 0;
588 /* If one of these types comes from a non-prototype fn definition,
589 compare that with the other type's arglist.
590 If they don't match, ask for a warning (but no error). */
591 if (TYPE_ACTUAL_ARG_TYPES (f1)
592 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
593 val = 2;
594 return val;
596 if (args2 == 0)
598 if (!self_promoting_args_p (args1))
599 return 0;
600 if (TYPE_ACTUAL_ARG_TYPES (f2)
601 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
602 val = 2;
603 return val;
606 /* Both types have argument lists: compare them and propagate results. */
607 val1 = type_lists_compatible_p (args1, args2);
608 return val1 != 1 ? val1 : val;
611 /* Check two lists of types for compatibility,
612 returning 0 for incompatible, 1 for compatible,
613 or 2 for compatible with warning. */
615 static int
616 type_lists_compatible_p (args1, args2)
617 tree args1, args2;
619 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
620 int val = 1;
621 int newval = 0;
623 while (1)
625 if (args1 == 0 && args2 == 0)
626 return val;
627 /* If one list is shorter than the other,
628 they fail to match. */
629 if (args1 == 0 || args2 == 0)
630 return 0;
631 /* A null pointer instead of a type
632 means there is supposed to be an argument
633 but nothing is specified about what type it has.
634 So match anything that self-promotes. */
635 if (TREE_VALUE (args1) == 0)
637 if (simple_type_promotes_to (TREE_VALUE (args2)) != NULL_TREE)
638 return 0;
640 else if (TREE_VALUE (args2) == 0)
642 if (simple_type_promotes_to (TREE_VALUE (args1)) != NULL_TREE)
643 return 0;
645 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
647 /* Allow wait (union {union wait *u; int *i} *)
648 and wait (union wait *) to be compatible. */
649 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
650 && (TYPE_NAME (TREE_VALUE (args1)) == 0
651 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
652 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
653 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
654 TYPE_SIZE (TREE_VALUE (args2))))
656 tree memb;
657 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
658 memb; memb = TREE_CHAIN (memb))
659 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
660 break;
661 if (memb == 0)
662 return 0;
664 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
665 && (TYPE_NAME (TREE_VALUE (args2)) == 0
666 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
667 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
668 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
669 TYPE_SIZE (TREE_VALUE (args1))))
671 tree memb;
672 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
673 memb; memb = TREE_CHAIN (memb))
674 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
675 break;
676 if (memb == 0)
677 return 0;
679 else
680 return 0;
683 /* comptypes said ok, but record if it said to warn. */
684 if (newval > val)
685 val = newval;
687 args1 = TREE_CHAIN (args1);
688 args2 = TREE_CHAIN (args2);
692 /* Compute the value of the `sizeof' operator. */
694 tree
695 c_sizeof (type)
696 tree type;
698 enum tree_code code = TREE_CODE (type);
700 if (code == FUNCTION_TYPE)
702 if (pedantic || warn_pointer_arith)
703 pedwarn ("sizeof applied to a function type");
704 return size_one_node;
706 if (code == VOID_TYPE)
708 if (pedantic || warn_pointer_arith)
709 pedwarn ("sizeof applied to a void type");
710 return size_one_node;
713 if (code == ERROR_MARK)
714 return size_one_node;
716 if (TYPE_SIZE (type) == 0)
718 error ("sizeof applied to an incomplete type");
719 return size_zero_node;
722 /* Convert in case a char is more than one unit. */
723 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
724 size_int (TYPE_PRECISION (char_type_node)
725 / BITS_PER_UNIT));
728 tree
729 c_sizeof_nowarn (type)
730 tree type;
732 enum tree_code code = TREE_CODE (type);
734 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
735 return size_one_node;
737 if (TYPE_SIZE (type) == 0)
738 return size_zero_node;
740 /* Convert in case a char is more than one unit. */
741 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
742 size_int (TYPE_PRECISION (char_type_node)
743 / BITS_PER_UNIT));
746 /* Compute the size to increment a pointer by. */
748 tree
749 c_size_in_bytes (type)
750 tree type;
752 enum tree_code code = TREE_CODE (type);
754 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
755 return size_one_node;
757 if (TYPE_SIZE (type) == 0)
759 error ("arithmetic on pointer to an incomplete type");
760 return size_one_node;
763 /* Convert in case a char is more than one unit. */
764 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
765 size_int (TYPE_PRECISION (char_type_node)
766 / BITS_PER_UNIT));
769 /* Implement the __alignof keyword: Return the minimum required
770 alignment of TYPE, measured in bytes. */
772 tree
773 c_alignof (type)
774 tree type;
776 enum tree_code code = TREE_CODE (type);
778 if (code == FUNCTION_TYPE)
779 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
781 if (code == VOID_TYPE || code == ERROR_MARK)
782 return size_one_node;
784 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
787 /* Implement the __alignof keyword: Return the minimum required
788 alignment of EXPR, measured in bytes. For VAR_DECL's and
789 FIELD_DECL's return DECL_ALIGN (which can be set from an
790 "aligned" __attribute__ specification). */
792 tree
793 c_alignof_expr (expr)
794 tree expr;
796 if (TREE_CODE (expr) == VAR_DECL)
797 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
799 if (TREE_CODE (expr) == COMPONENT_REF
800 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
802 error ("`__alignof' applied to a bit-field");
803 return size_one_node;
805 else if (TREE_CODE (expr) == COMPONENT_REF
806 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
807 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
809 if (TREE_CODE (expr) == INDIRECT_REF)
811 tree t = TREE_OPERAND (expr, 0);
812 tree best = t;
813 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
815 while (TREE_CODE (t) == NOP_EXPR
816 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
818 int thisalign;
820 t = TREE_OPERAND (t, 0);
821 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
822 if (thisalign > bestalign)
823 best = t, bestalign = thisalign;
825 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
827 else
828 return c_alignof (TREE_TYPE (expr));
831 /* Return either DECL or its known constant value (if it has one). */
833 static tree
834 decl_constant_value (decl)
835 tree decl;
837 if (/* Don't change a variable array bound or initial value to a constant
838 in a place where a variable is invalid. */
839 current_function_decl != 0
840 && ! pedantic
841 && ! TREE_THIS_VOLATILE (decl)
842 && TREE_READONLY (decl) && ! ITERATOR_P (decl)
843 && DECL_INITIAL (decl) != 0
844 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
845 /* This is invalid if initial value is not constant.
846 If it has either a function call, a memory reference,
847 or a variable, then re-evaluating it could give different results. */
848 && TREE_CONSTANT (DECL_INITIAL (decl))
849 /* Check for cases where this is sub-optimal, even though valid. */
850 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
851 && DECL_MODE (decl) != BLKmode)
852 return DECL_INITIAL (decl);
853 return decl;
856 /* Perform default promotions for C data used in expressions.
857 Arrays and functions are converted to pointers;
858 enumeral types or short or char, to int.
859 In addition, manifest constants symbols are replaced by their values. */
861 tree
862 default_conversion (exp)
863 tree exp;
865 register tree type = TREE_TYPE (exp);
866 register enum tree_code code = TREE_CODE (type);
868 /* Constants can be used directly unless they're not loadable. */
869 if (TREE_CODE (exp) == CONST_DECL)
870 exp = DECL_INITIAL (exp);
872 /* Replace a nonvolatile const static variable with its value unless
873 it is an array, in which case we must be sure that taking the
874 address of the array produces consistent results. */
875 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
877 exp = decl_constant_value (exp);
878 type = TREE_TYPE (exp);
881 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
882 an lvalue.
884 Do not use STRIP_NOPS here! It will remove conversions from pointer
885 to integer and cause infinite recursion. */
886 while (TREE_CODE (exp) == NON_LVALUE_EXPR
887 || (TREE_CODE (exp) == NOP_EXPR
888 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
889 exp = TREE_OPERAND (exp, 0);
891 /* Normally convert enums to int,
892 but convert wide enums to something wider. */
893 if (code == ENUMERAL_TYPE)
895 type = type_for_size (MAX (TYPE_PRECISION (type),
896 TYPE_PRECISION (integer_type_node)),
897 ((flag_traditional
898 || (TYPE_PRECISION (type)
899 >= TYPE_PRECISION (integer_type_node)))
900 && TREE_UNSIGNED (type)));
902 return convert (type, exp);
905 if (TREE_CODE (exp) == COMPONENT_REF
906 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
907 /* If it's thinner than an int, promote it like a
908 C_PROMOTING_INTEGER_TYPE_P, otherwise leave it alone. */
909 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
910 TYPE_PRECISION (integer_type_node)))
911 return convert (flag_traditional && TREE_UNSIGNED (type)
912 ? unsigned_type_node : integer_type_node,
913 exp);
915 if (C_PROMOTING_INTEGER_TYPE_P (type))
917 /* Traditionally, unsignedness is preserved in default promotions.
918 Also preserve unsignedness if not really getting any wider. */
919 if (TREE_UNSIGNED (type)
920 && (flag_traditional
921 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
922 return convert (unsigned_type_node, exp);
924 return convert (integer_type_node, exp);
927 if (flag_traditional && !flag_allow_single_precision
928 && TYPE_MAIN_VARIANT (type) == float_type_node)
929 return convert (double_type_node, exp);
931 if (code == VOID_TYPE)
933 error ("void value not ignored as it ought to be");
934 return error_mark_node;
936 if (code == FUNCTION_TYPE)
938 return build_unary_op (ADDR_EXPR, exp, 0);
940 if (code == ARRAY_TYPE)
942 register tree adr;
943 tree restype = TREE_TYPE (type);
944 tree ptrtype;
945 int constp = 0;
946 int volatilep = 0;
948 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
949 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
951 constp = TREE_READONLY (exp);
952 volatilep = TREE_THIS_VOLATILE (exp);
955 if (TYPE_QUALS (type) || constp || volatilep)
956 restype
957 = c_build_qualified_type (restype,
958 TYPE_QUALS (type)
959 | (constp * TYPE_QUAL_CONST)
960 | (volatilep * TYPE_QUAL_VOLATILE));
962 if (TREE_CODE (exp) == INDIRECT_REF)
963 return convert (TYPE_POINTER_TO (restype),
964 TREE_OPERAND (exp, 0));
966 if (TREE_CODE (exp) == COMPOUND_EXPR)
968 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
969 return build (COMPOUND_EXPR, TREE_TYPE (op1),
970 TREE_OPERAND (exp, 0), op1);
973 if (! lvalue_p (exp)
974 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
976 error ("invalid use of non-lvalue array");
977 return error_mark_node;
980 ptrtype = build_pointer_type (restype);
982 if (TREE_CODE (exp) == VAR_DECL)
984 /* ??? This is not really quite correct
985 in that the type of the operand of ADDR_EXPR
986 is not the target type of the type of the ADDR_EXPR itself.
987 Question is, can this lossage be avoided? */
988 adr = build1 (ADDR_EXPR, ptrtype, exp);
989 if (mark_addressable (exp) == 0)
990 return error_mark_node;
991 TREE_CONSTANT (adr) = staticp (exp);
992 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
993 return adr;
995 /* This way is better for a COMPONENT_REF since it can
996 simplify the offset for a component. */
997 adr = build_unary_op (ADDR_EXPR, exp, 1);
998 return convert (ptrtype, adr);
1000 return exp;
1003 /* Look up component name in the structure type definition.
1005 If this component name is found indirectly within an anonymous union,
1006 store in *INDIRECT the component which directly contains
1007 that anonymous union. Otherwise, set *INDIRECT to 0. */
1009 static tree
1010 lookup_field (type, component, indirect)
1011 tree type, component;
1012 tree *indirect;
1014 tree field;
1016 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1017 to the field elements. Use a binary search on this array to quickly
1018 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1019 will always be set for structures which have many elements. */
1021 if (TYPE_LANG_SPECIFIC (type))
1023 int bot, top, half;
1024 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1026 field = TYPE_FIELDS (type);
1027 bot = 0;
1028 top = TYPE_LANG_SPECIFIC (type)->len;
1029 while (top - bot > 1)
1031 half = (top - bot + 1) >> 1;
1032 field = field_array[bot+half];
1034 if (DECL_NAME (field) == NULL_TREE)
1036 /* Step through all anon unions in linear fashion. */
1037 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1039 tree anon = 0, junk;
1041 field = field_array[bot++];
1042 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1043 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1044 anon = lookup_field (TREE_TYPE (field), component, &junk);
1046 if (anon != NULL_TREE)
1048 *indirect = field;
1049 return anon;
1053 /* Entire record is only anon unions. */
1054 if (bot > top)
1055 return NULL_TREE;
1057 /* Restart the binary search, with new lower bound. */
1058 continue;
1061 if (DECL_NAME (field) == component)
1062 break;
1063 if (DECL_NAME (field) < component)
1064 bot += half;
1065 else
1066 top = bot + half;
1069 if (DECL_NAME (field_array[bot]) == component)
1070 field = field_array[bot];
1071 else if (DECL_NAME (field) != component)
1072 field = 0;
1074 else
1076 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1078 if (DECL_NAME (field) == NULL_TREE)
1080 tree junk;
1081 tree anon = 0;
1083 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1084 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1085 anon = lookup_field (TREE_TYPE (field), component, &junk);
1087 if (anon != NULL_TREE)
1089 *indirect = field;
1090 return anon;
1094 if (DECL_NAME (field) == component)
1095 break;
1099 *indirect = NULL_TREE;
1100 return field;
1103 /* Make an expression to refer to the COMPONENT field of
1104 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1106 tree
1107 build_component_ref (datum, component)
1108 tree datum, component;
1110 register tree type = TREE_TYPE (datum);
1111 register enum tree_code code = TREE_CODE (type);
1112 register tree field = NULL;
1113 register tree ref;
1115 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1116 unless we are not to support things not strictly ANSI. */
1117 switch (TREE_CODE (datum))
1119 case COMPOUND_EXPR:
1121 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1122 return build (COMPOUND_EXPR, TREE_TYPE (value),
1123 TREE_OPERAND (datum, 0), value);
1125 case COND_EXPR:
1126 return build_conditional_expr
1127 (TREE_OPERAND (datum, 0),
1128 build_component_ref (TREE_OPERAND (datum, 1), component),
1129 build_component_ref (TREE_OPERAND (datum, 2), component));
1131 default:
1132 break;
1135 /* See if there is a field or component with name COMPONENT. */
1137 if (code == RECORD_TYPE || code == UNION_TYPE)
1139 tree indirect = 0;
1141 if (TYPE_SIZE (type) == 0)
1143 incomplete_type_error (NULL_TREE, type);
1144 return error_mark_node;
1147 field = lookup_field (type, component, &indirect);
1149 if (!field)
1151 error ("%s has no member named `%s'",
1152 code == RECORD_TYPE ? "structure" : "union",
1153 IDENTIFIER_POINTER (component));
1154 return error_mark_node;
1156 if (TREE_TYPE (field) == error_mark_node)
1157 return error_mark_node;
1159 /* If FIELD was found buried within an anonymous union,
1160 make one COMPONENT_REF to get that anonymous union,
1161 then fall thru to make a second COMPONENT_REF to get FIELD. */
1162 if (indirect != 0)
1164 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1165 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1166 TREE_READONLY (ref) = 1;
1167 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1168 TREE_THIS_VOLATILE (ref) = 1;
1169 datum = ref;
1172 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1174 if (TREE_READONLY (datum) || TREE_READONLY (field))
1175 TREE_READONLY (ref) = 1;
1176 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1177 TREE_THIS_VOLATILE (ref) = 1;
1179 return ref;
1181 else if (code != ERROR_MARK)
1182 error ("request for member `%s' in something not a structure or union",
1183 IDENTIFIER_POINTER (component));
1185 return error_mark_node;
1188 /* Given an expression PTR for a pointer, return an expression
1189 for the value pointed to.
1190 ERRORSTRING is the name of the operator to appear in error messages. */
1192 tree
1193 build_indirect_ref (ptr, errorstring)
1194 tree ptr;
1195 const char *errorstring;
1197 register tree pointer = default_conversion (ptr);
1198 register tree type = TREE_TYPE (pointer);
1200 if (TREE_CODE (type) == POINTER_TYPE)
1202 if (TREE_CODE (pointer) == ADDR_EXPR
1203 && !flag_volatile
1204 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1205 == TREE_TYPE (type)))
1206 return TREE_OPERAND (pointer, 0);
1207 else
1209 tree t = TREE_TYPE (type);
1210 register tree ref = build1 (INDIRECT_REF,
1211 TYPE_MAIN_VARIANT (t), pointer);
1213 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1215 error ("dereferencing pointer to incomplete type");
1216 return error_mark_node;
1218 if (TREE_CODE (t) == VOID_TYPE && skip_evaluation == 0)
1219 warning ("dereferencing `void *' pointer");
1221 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1222 so that we get the proper error message if the result is used
1223 to assign to. Also, &* is supposed to be a no-op.
1224 And ANSI C seems to specify that the type of the result
1225 should be the const type. */
1226 /* A de-reference of a pointer to const is not a const. It is valid
1227 to change it via some other pointer. */
1228 TREE_READONLY (ref) = TYPE_READONLY (t);
1229 TREE_SIDE_EFFECTS (ref)
1230 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1231 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1232 return ref;
1235 else if (TREE_CODE (pointer) != ERROR_MARK)
1236 error ("invalid type argument of `%s'", errorstring);
1237 return error_mark_node;
1240 /* This handles expressions of the form "a[i]", which denotes
1241 an array reference.
1243 This is logically equivalent in C to *(a+i), but we may do it differently.
1244 If A is a variable or a member, we generate a primitive ARRAY_REF.
1245 This avoids forcing the array out of registers, and can work on
1246 arrays that are not lvalues (for example, members of structures returned
1247 by functions). */
1249 tree
1250 build_array_ref (array, index)
1251 tree array, index;
1253 if (index == 0)
1255 error ("subscript missing in array reference");
1256 return error_mark_node;
1259 if (TREE_TYPE (array) == error_mark_node
1260 || TREE_TYPE (index) == error_mark_node)
1261 return error_mark_node;
1263 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1264 && TREE_CODE (array) != INDIRECT_REF)
1266 tree rval, type;
1268 /* Subscripting with type char is likely to lose
1269 on a machine where chars are signed.
1270 So warn on any machine, but optionally.
1271 Don't warn for unsigned char since that type is safe.
1272 Don't warn for signed char because anyone who uses that
1273 must have done so deliberately. */
1274 if (warn_char_subscripts
1275 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1276 warning ("array subscript has type `char'");
1278 /* Apply default promotions *after* noticing character types. */
1279 index = default_conversion (index);
1281 /* Require integer *after* promotion, for sake of enums. */
1282 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1284 error ("array subscript is not an integer");
1285 return error_mark_node;
1288 /* An array that is indexed by a non-constant
1289 cannot be stored in a register; we must be able to do
1290 address arithmetic on its address.
1291 Likewise an array of elements of variable size. */
1292 if (TREE_CODE (index) != INTEGER_CST
1293 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1294 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1296 if (mark_addressable (array) == 0)
1297 return error_mark_node;
1299 /* An array that is indexed by a constant value which is not within
1300 the array bounds cannot be stored in a register either; because we
1301 would get a crash in store_bit_field/extract_bit_field when trying
1302 to access a non-existent part of the register. */
1303 if (TREE_CODE (index) == INTEGER_CST
1304 && TYPE_VALUES (TREE_TYPE (array))
1305 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1307 if (mark_addressable (array) == 0)
1308 return error_mark_node;
1311 if (pedantic && !lvalue_p (array))
1313 if (DECL_REGISTER (array))
1314 pedwarn ("ANSI C forbids subscripting `register' array");
1315 else
1316 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1319 if (pedantic)
1321 tree foo = array;
1322 while (TREE_CODE (foo) == COMPONENT_REF)
1323 foo = TREE_OPERAND (foo, 0);
1324 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1325 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1328 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1329 rval = build (ARRAY_REF, type, array, index);
1330 /* Array ref is const/volatile if the array elements are
1331 or if the array is. */
1332 TREE_READONLY (rval)
1333 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1334 | TREE_READONLY (array));
1335 TREE_SIDE_EFFECTS (rval)
1336 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1337 | TREE_SIDE_EFFECTS (array));
1338 TREE_THIS_VOLATILE (rval)
1339 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1340 /* This was added by rms on 16 Nov 91.
1341 It fixes vol struct foo *a; a->elts[1]
1342 in an inline function.
1343 Hope it doesn't break something else. */
1344 | TREE_THIS_VOLATILE (array));
1345 return require_complete_type (fold (rval));
1349 tree ar = default_conversion (array);
1350 tree ind = default_conversion (index);
1352 /* Do the same warning check as above, but only on the part that's
1353 syntactically the index and only if it is also semantically
1354 the index. */
1355 if (warn_char_subscripts
1356 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1357 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1358 warning ("subscript has type `char'");
1360 /* Put the integer in IND to simplify error checking. */
1361 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1363 tree temp = ar;
1364 ar = ind;
1365 ind = temp;
1368 if (ar == error_mark_node)
1369 return ar;
1371 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1372 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1374 error ("subscripted value is neither array nor pointer");
1375 return error_mark_node;
1377 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1379 error ("array subscript is not an integer");
1380 return error_mark_node;
1383 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1384 "array indexing");
1388 /* Build a function call to function FUNCTION with parameters PARAMS.
1389 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1390 TREE_VALUE of each node is a parameter-expression.
1391 FUNCTION's data type may be a function type or a pointer-to-function. */
1393 tree
1394 build_function_call (function, params)
1395 tree function, params;
1397 register tree fntype, fundecl = 0;
1398 register tree coerced_params;
1399 tree name = NULL_TREE, assembler_name = NULL_TREE, result;
1401 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1402 STRIP_TYPE_NOPS (function);
1404 /* Convert anything with function type to a pointer-to-function. */
1405 if (TREE_CODE (function) == FUNCTION_DECL)
1407 name = DECL_NAME (function);
1408 assembler_name = DECL_ASSEMBLER_NAME (function);
1410 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1411 (because calling an inline function does not mean the function
1412 needs to be separately compiled). */
1413 fntype = build_type_variant (TREE_TYPE (function),
1414 TREE_READONLY (function),
1415 TREE_THIS_VOLATILE (function));
1416 fundecl = function;
1417 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1419 else
1420 function = default_conversion (function);
1422 fntype = TREE_TYPE (function);
1424 if (TREE_CODE (fntype) == ERROR_MARK)
1425 return error_mark_node;
1427 if (!(TREE_CODE (fntype) == POINTER_TYPE
1428 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1430 error ("called object is not a function");
1431 return error_mark_node;
1434 /* fntype now gets the type of function pointed to. */
1435 fntype = TREE_TYPE (fntype);
1437 /* Convert the parameters to the types declared in the
1438 function prototype, or apply default promotions. */
1440 coerced_params
1441 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1443 /* Check for errors in format strings. */
1445 if (warn_format && (name || assembler_name))
1446 check_function_format (name, assembler_name, coerced_params);
1448 /* Recognize certain built-in functions so we can make tree-codes
1449 other than CALL_EXPR. We do this when it enables fold-const.c
1450 to do something useful. */
1452 if (TREE_CODE (function) == ADDR_EXPR
1453 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1454 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1456 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1457 params, coerced_params);
1458 if (result)
1459 return result;
1462 result = build (CALL_EXPR, TREE_TYPE (fntype),
1463 function, coerced_params, NULL_TREE);
1465 TREE_SIDE_EFFECTS (result) = 1;
1466 if (TREE_TYPE (result) == void_type_node)
1467 return result;
1468 return require_complete_type (result);
1471 /* Convert the argument expressions in the list VALUES
1472 to the types in the list TYPELIST. The result is a list of converted
1473 argument expressions.
1475 If TYPELIST is exhausted, or when an element has NULL as its type,
1476 perform the default conversions.
1478 PARMLIST is the chain of parm decls for the function being called.
1479 It may be 0, if that info is not available.
1480 It is used only for generating error messages.
1482 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1484 This is also where warnings about wrong number of args are generated.
1486 Both VALUES and the returned value are chains of TREE_LIST nodes
1487 with the elements of the list in the TREE_VALUE slots of those nodes. */
1489 static tree
1490 convert_arguments (typelist, values, name, fundecl)
1491 tree typelist, values, name, fundecl;
1493 register tree typetail, valtail;
1494 register tree result = NULL;
1495 int parmnum;
1497 /* Scan the given expressions and types, producing individual
1498 converted arguments and pushing them on RESULT in reverse order. */
1500 for (valtail = values, typetail = typelist, parmnum = 0;
1501 valtail;
1502 valtail = TREE_CHAIN (valtail), parmnum++)
1504 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1505 register tree val = TREE_VALUE (valtail);
1507 if (type == void_type_node)
1509 if (name)
1510 error ("too many arguments to function `%s'",
1511 IDENTIFIER_POINTER (name));
1512 else
1513 error ("too many arguments to function");
1514 break;
1517 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1518 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1519 to convert automatically to a pointer. */
1520 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1521 val = TREE_OPERAND (val, 0);
1523 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1524 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1525 val = default_conversion (val);
1527 val = require_complete_type (val);
1529 if (type != 0)
1531 /* Formal parm type is specified by a function prototype. */
1532 tree parmval;
1534 if (TYPE_SIZE (type) == 0)
1536 error ("type of formal parameter %d is incomplete", parmnum + 1);
1537 parmval = val;
1539 else
1541 /* Optionally warn about conversions that
1542 differ from the default conversions. */
1543 if (warn_conversion)
1545 int formal_prec = TYPE_PRECISION (type);
1547 if (INTEGRAL_TYPE_P (type)
1548 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1549 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1550 else if (TREE_CODE (type) == COMPLEX_TYPE
1551 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1552 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1553 else if (TREE_CODE (type) == REAL_TYPE
1554 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1555 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1556 else if (TREE_CODE (type) == REAL_TYPE
1557 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1558 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1559 /* ??? At some point, messages should be written about
1560 conversions between complex types, but that's too messy
1561 to do now. */
1562 else if (TREE_CODE (type) == REAL_TYPE
1563 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1565 /* Warn if any argument is passed as `float',
1566 since without a prototype it would be `double'. */
1567 if (formal_prec == TYPE_PRECISION (float_type_node))
1568 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1570 /* Detect integer changing in width or signedness. */
1571 else if (INTEGRAL_TYPE_P (type)
1572 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1574 tree would_have_been = default_conversion (val);
1575 tree type1 = TREE_TYPE (would_have_been);
1577 if (TREE_CODE (type) == ENUMERAL_TYPE
1578 && type == TREE_TYPE (val))
1579 /* No warning if function asks for enum
1580 and the actual arg is that enum type. */
1582 else if (formal_prec != TYPE_PRECISION (type1))
1583 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1584 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1586 /* Don't complain if the formal parameter type
1587 is an enum, because we can't tell now whether
1588 the value was an enum--even the same enum. */
1589 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1591 else if (TREE_CODE (val) == INTEGER_CST
1592 && int_fits_type_p (val, type))
1593 /* Change in signedness doesn't matter
1594 if a constant value is unaffected. */
1596 /* Likewise for a constant in a NOP_EXPR. */
1597 else if (TREE_CODE (val) == NOP_EXPR
1598 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1599 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1601 #if 0 /* We never get such tree structure here. */
1602 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1603 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1604 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1605 /* Change in signedness doesn't matter
1606 if an enum value is unaffected. */
1608 #endif
1609 /* If the value is extended from a narrower
1610 unsigned type, it doesn't matter whether we
1611 pass it as signed or unsigned; the value
1612 certainly is the same either way. */
1613 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1614 && TREE_UNSIGNED (TREE_TYPE (val)))
1616 else if (TREE_UNSIGNED (type))
1617 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1618 else
1619 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1623 parmval = convert_for_assignment (type, val,
1624 (char *) 0, /* arg passing */
1625 fundecl, name, parmnum + 1);
1627 if (PROMOTE_PROTOTYPES
1628 && (TREE_CODE (type) == INTEGER_TYPE
1629 || TREE_CODE (type) == ENUMERAL_TYPE)
1630 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1631 parmval = default_conversion (parmval);
1633 result = tree_cons (NULL_TREE, parmval, result);
1635 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1636 && (TYPE_PRECISION (TREE_TYPE (val))
1637 < TYPE_PRECISION (double_type_node)))
1638 /* Convert `float' to `double'. */
1639 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1640 else
1641 /* Convert `short' and `char' to full-size `int'. */
1642 result = tree_cons (NULL_TREE, default_conversion (val), result);
1644 if (typetail)
1645 typetail = TREE_CHAIN (typetail);
1648 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1650 if (name)
1651 error ("too few arguments to function `%s'",
1652 IDENTIFIER_POINTER (name));
1653 else
1654 error ("too few arguments to function");
1657 return nreverse (result);
1660 /* This is the entry point used by the parser
1661 for binary operators in the input.
1662 In addition to constructing the expression,
1663 we check for operands that were written with other binary operators
1664 in a way that is likely to confuse the user. */
1666 tree
1667 parser_build_binary_op (code, arg1, arg2)
1668 enum tree_code code;
1669 tree arg1, arg2;
1671 tree result = build_binary_op (code, arg1, arg2, 1);
1673 char class;
1674 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1675 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1676 enum tree_code code1 = ERROR_MARK;
1677 enum tree_code code2 = ERROR_MARK;
1679 if (class1 == 'e' || class1 == '1'
1680 || class1 == '2' || class1 == '<')
1681 code1 = C_EXP_ORIGINAL_CODE (arg1);
1682 if (class2 == 'e' || class2 == '1'
1683 || class2 == '2' || class2 == '<')
1684 code2 = C_EXP_ORIGINAL_CODE (arg2);
1686 /* Check for cases such as x+y<<z which users are likely
1687 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1688 is cleared to prevent these warnings. */
1689 if (warn_parentheses)
1691 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1693 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1694 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1695 warning ("suggest parentheses around + or - inside shift");
1698 if (code == TRUTH_ORIF_EXPR)
1700 if (code1 == TRUTH_ANDIF_EXPR
1701 || code2 == TRUTH_ANDIF_EXPR)
1702 warning ("suggest parentheses around && within ||");
1705 if (code == BIT_IOR_EXPR)
1707 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1708 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1709 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1710 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1711 warning ("suggest parentheses around arithmetic in operand of |");
1712 /* Check cases like x|y==z */
1713 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1714 warning ("suggest parentheses around comparison in operand of |");
1717 if (code == BIT_XOR_EXPR)
1719 if (code1 == BIT_AND_EXPR
1720 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1721 || code2 == BIT_AND_EXPR
1722 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1723 warning ("suggest parentheses around arithmetic in operand of ^");
1724 /* Check cases like x^y==z */
1725 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1726 warning ("suggest parentheses around comparison in operand of ^");
1729 if (code == BIT_AND_EXPR)
1731 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1732 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1733 warning ("suggest parentheses around + or - in operand of &");
1734 /* Check cases like x&y==z */
1735 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1736 warning ("suggest parentheses around comparison in operand of &");
1740 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1741 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1742 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1743 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1745 unsigned_conversion_warning (result, arg1);
1746 unsigned_conversion_warning (result, arg2);
1747 overflow_warning (result);
1749 class = TREE_CODE_CLASS (TREE_CODE (result));
1751 /* Record the code that was specified in the source,
1752 for the sake of warnings about confusing nesting. */
1753 if (class == 'e' || class == '1'
1754 || class == '2' || class == '<')
1755 C_SET_EXP_ORIGINAL_CODE (result, code);
1756 else
1758 int flag = TREE_CONSTANT (result);
1759 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1760 so that convert_for_assignment wouldn't strip it.
1761 That way, we got warnings for things like p = (1 - 1).
1762 But it turns out we should not get those warnings. */
1763 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1764 C_SET_EXP_ORIGINAL_CODE (result, code);
1765 TREE_CONSTANT (result) = flag;
1768 return result;
1771 /* Build a binary-operation expression without default conversions.
1772 CODE is the kind of expression to build.
1773 This function differs from `build' in several ways:
1774 the data type of the result is computed and recorded in it,
1775 warnings are generated if arg data types are invalid,
1776 special handling for addition and subtraction of pointers is known,
1777 and some optimization is done (operations on narrow ints
1778 are done in the narrower type when that gives the same result).
1779 Constant folding is also done before the result is returned.
1781 Note that the operands will never have enumeral types, or function
1782 or array types, because either they will have the default conversions
1783 performed or they have both just been converted to some other type in which
1784 the arithmetic is to be done. */
1786 tree
1787 build_binary_op (code, orig_op0, orig_op1, convert_p)
1788 enum tree_code code;
1789 tree orig_op0, orig_op1;
1790 int convert_p;
1792 tree type0, type1;
1793 register enum tree_code code0, code1;
1794 tree op0, op1;
1796 /* Expression code to give to the expression when it is built.
1797 Normally this is CODE, which is what the caller asked for,
1798 but in some special cases we change it. */
1799 register enum tree_code resultcode = code;
1801 /* Data type in which the computation is to be performed.
1802 In the simplest cases this is the common type of the arguments. */
1803 register tree result_type = NULL;
1805 /* Nonzero means operands have already been type-converted
1806 in whatever way is necessary.
1807 Zero means they need to be converted to RESULT_TYPE. */
1808 int converted = 0;
1810 /* Nonzero means create the expression with this type, rather than
1811 RESULT_TYPE. */
1812 tree build_type = 0;
1814 /* Nonzero means after finally constructing the expression
1815 convert it to this type. */
1816 tree final_type = 0;
1818 /* Nonzero if this is an operation like MIN or MAX which can
1819 safely be computed in short if both args are promoted shorts.
1820 Also implies COMMON.
1821 -1 indicates a bitwise operation; this makes a difference
1822 in the exact conditions for when it is safe to do the operation
1823 in a narrower mode. */
1824 int shorten = 0;
1826 /* Nonzero if this is a comparison operation;
1827 if both args are promoted shorts, compare the original shorts.
1828 Also implies COMMON. */
1829 int short_compare = 0;
1831 /* Nonzero if this is a right-shift operation, which can be computed on the
1832 original short and then promoted if the operand is a promoted short. */
1833 int short_shift = 0;
1835 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1836 int common = 0;
1838 if (convert_p)
1840 op0 = default_conversion (orig_op0);
1841 op1 = default_conversion (orig_op1);
1843 else
1845 op0 = orig_op0;
1846 op1 = orig_op1;
1849 type0 = TREE_TYPE (op0);
1850 type1 = TREE_TYPE (op1);
1852 /* The expression codes of the data types of the arguments tell us
1853 whether the arguments are integers, floating, pointers, etc. */
1854 code0 = TREE_CODE (type0);
1855 code1 = TREE_CODE (type1);
1857 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1858 STRIP_TYPE_NOPS (op0);
1859 STRIP_TYPE_NOPS (op1);
1861 /* If an error was already reported for one of the arguments,
1862 avoid reporting another error. */
1864 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1865 return error_mark_node;
1867 switch (code)
1869 case PLUS_EXPR:
1870 /* Handle the pointer + int case. */
1871 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1872 return pointer_int_sum (PLUS_EXPR, op0, op1);
1873 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1874 return pointer_int_sum (PLUS_EXPR, op1, op0);
1875 else
1876 common = 1;
1877 break;
1879 case MINUS_EXPR:
1880 /* Subtraction of two similar pointers.
1881 We must subtract them as integers, then divide by object size. */
1882 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1883 && comp_target_types (type0, type1))
1884 return pointer_diff (op0, op1);
1885 /* Handle pointer minus int. Just like pointer plus int. */
1886 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1887 return pointer_int_sum (MINUS_EXPR, op0, op1);
1888 else
1889 common = 1;
1890 break;
1892 case MULT_EXPR:
1893 common = 1;
1894 break;
1896 case TRUNC_DIV_EXPR:
1897 case CEIL_DIV_EXPR:
1898 case FLOOR_DIV_EXPR:
1899 case ROUND_DIV_EXPR:
1900 case EXACT_DIV_EXPR:
1901 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1902 || code0 == COMPLEX_TYPE)
1903 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1904 || code1 == COMPLEX_TYPE))
1906 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
1907 resultcode = RDIV_EXPR;
1908 else
1909 /* Although it would be tempting to shorten always here, that
1910 loses on some targets, since the modulo instruction is
1911 undefined if the quotient can't be represented in the
1912 computation mode. We shorten only if unsigned or if
1913 dividing by something we know != -1. */
1914 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
1915 || (TREE_CODE (op1) == INTEGER_CST
1916 && ! integer_all_onesp (op1)));
1917 common = 1;
1919 break;
1921 case BIT_AND_EXPR:
1922 case BIT_ANDTC_EXPR:
1923 case BIT_IOR_EXPR:
1924 case BIT_XOR_EXPR:
1925 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1926 shorten = -1;
1927 /* If one operand is a constant, and the other is a short type
1928 that has been converted to an int,
1929 really do the work in the short type and then convert the
1930 result to int. If we are lucky, the constant will be 0 or 1
1931 in the short type, making the entire operation go away. */
1932 if (TREE_CODE (op0) == INTEGER_CST
1933 && TREE_CODE (op1) == NOP_EXPR
1934 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
1935 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
1937 final_type = result_type;
1938 op1 = TREE_OPERAND (op1, 0);
1939 result_type = TREE_TYPE (op1);
1941 if (TREE_CODE (op1) == INTEGER_CST
1942 && TREE_CODE (op0) == NOP_EXPR
1943 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
1944 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
1946 final_type = result_type;
1947 op0 = TREE_OPERAND (op0, 0);
1948 result_type = TREE_TYPE (op0);
1950 break;
1952 case TRUNC_MOD_EXPR:
1953 case FLOOR_MOD_EXPR:
1954 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1956 /* Although it would be tempting to shorten always here, that loses
1957 on some targets, since the modulo instruction is undefined if the
1958 quotient can't be represented in the computation mode. We shorten
1959 only if unsigned or if dividing by something we know != -1. */
1960 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
1961 || (TREE_CODE (op1) == INTEGER_CST
1962 && ! integer_all_onesp (op1)));
1963 common = 1;
1965 break;
1967 case TRUTH_ANDIF_EXPR:
1968 case TRUTH_ORIF_EXPR:
1969 case TRUTH_AND_EXPR:
1970 case TRUTH_OR_EXPR:
1971 case TRUTH_XOR_EXPR:
1972 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
1973 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
1974 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
1975 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
1977 /* Result of these operations is always an int,
1978 but that does not mean the operands should be
1979 converted to ints! */
1980 result_type = integer_type_node;
1981 op0 = truthvalue_conversion (op0);
1982 op1 = truthvalue_conversion (op1);
1983 converted = 1;
1985 break;
1987 /* Shift operations: result has same type as first operand;
1988 always convert second operand to int.
1989 Also set SHORT_SHIFT if shifting rightward. */
1991 case RSHIFT_EXPR:
1992 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1994 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
1996 if (tree_int_cst_sgn (op1) < 0)
1997 warning ("right shift count is negative");
1998 else
2000 if (! integer_zerop (op1))
2001 short_shift = 1;
2003 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2004 warning ("right shift count >= width of type");
2008 /* Use the type of the value to be shifted.
2009 This is what most traditional C compilers do. */
2010 result_type = type0;
2011 /* Unless traditional, convert the shift-count to an integer,
2012 regardless of size of value being shifted. */
2013 if (! flag_traditional)
2015 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2016 op1 = convert (integer_type_node, op1);
2017 /* Avoid converting op1 to result_type later. */
2018 converted = 1;
2021 break;
2023 case LSHIFT_EXPR:
2024 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2026 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2028 if (tree_int_cst_sgn (op1) < 0)
2029 warning ("left shift count is negative");
2031 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2032 warning ("left shift count >= width of type");
2035 /* Use the type of the value to be shifted.
2036 This is what most traditional C compilers do. */
2037 result_type = type0;
2038 /* Unless traditional, convert the shift-count to an integer,
2039 regardless of size of value being shifted. */
2040 if (! flag_traditional)
2042 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2043 op1 = convert (integer_type_node, op1);
2044 /* Avoid converting op1 to result_type later. */
2045 converted = 1;
2048 break;
2050 case RROTATE_EXPR:
2051 case LROTATE_EXPR:
2052 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2054 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2056 if (tree_int_cst_sgn (op1) < 0)
2057 warning ("shift count is negative");
2058 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2059 warning ("shift count >= width of type");
2062 /* Use the type of the value to be shifted.
2063 This is what most traditional C compilers do. */
2064 result_type = type0;
2065 /* Unless traditional, convert the shift-count to an integer,
2066 regardless of size of value being shifted. */
2067 if (! flag_traditional)
2069 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2070 op1 = convert (integer_type_node, op1);
2071 /* Avoid converting op1 to result_type later. */
2072 converted = 1;
2075 break;
2077 case EQ_EXPR:
2078 case NE_EXPR:
2079 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2080 warning ("comparing floating point with == or != is unsafe");
2081 /* Result of comparison is always int,
2082 but don't convert the args to int! */
2083 build_type = integer_type_node;
2084 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2085 || code0 == COMPLEX_TYPE)
2086 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2087 || code1 == COMPLEX_TYPE))
2088 short_compare = 1;
2089 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2091 register tree tt0 = TREE_TYPE (type0);
2092 register tree tt1 = TREE_TYPE (type1);
2093 /* Anything compares with void *. void * compares with anything.
2094 Otherwise, the targets must be compatible
2095 and both must be object or both incomplete. */
2096 if (comp_target_types (type0, type1))
2097 result_type = common_type (type0, type1);
2098 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2100 /* op0 != orig_op0 detects the case of something
2101 whose value is 0 but which isn't a valid null ptr const. */
2102 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2103 && TREE_CODE (tt1) == FUNCTION_TYPE)
2104 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2106 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2108 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2109 && TREE_CODE (tt0) == FUNCTION_TYPE)
2110 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2112 else
2113 pedwarn ("comparison of distinct pointer types lacks a cast");
2115 if (result_type == NULL_TREE)
2116 result_type = ptr_type_node;
2118 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2119 && integer_zerop (op1))
2120 result_type = type0;
2121 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2122 && integer_zerop (op0))
2123 result_type = type1;
2124 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2126 result_type = type0;
2127 if (! flag_traditional)
2128 pedwarn ("comparison between pointer and integer");
2130 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2132 result_type = type1;
2133 if (! flag_traditional)
2134 pedwarn ("comparison between pointer and integer");
2136 break;
2138 case MAX_EXPR:
2139 case MIN_EXPR:
2140 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2141 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2142 shorten = 1;
2143 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2145 if (comp_target_types (type0, type1))
2147 result_type = common_type (type0, type1);
2148 if (pedantic
2149 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2150 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2152 else
2154 result_type = ptr_type_node;
2155 pedwarn ("comparison of distinct pointer types lacks a cast");
2158 break;
2160 case LE_EXPR:
2161 case GE_EXPR:
2162 case LT_EXPR:
2163 case GT_EXPR:
2164 build_type = integer_type_node;
2165 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2166 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2167 short_compare = 1;
2168 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2170 if (comp_target_types (type0, type1))
2172 result_type = common_type (type0, type1);
2173 if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2174 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2175 pedwarn ("comparison of complete and incomplete pointers");
2176 else if (pedantic
2177 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2178 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2180 else
2182 result_type = ptr_type_node;
2183 pedwarn ("comparison of distinct pointer types lacks a cast");
2186 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2187 && integer_zerop (op1))
2189 result_type = type0;
2190 if (pedantic || extra_warnings)
2191 pedwarn ("ordered comparison of pointer with integer zero");
2193 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2194 && integer_zerop (op0))
2196 result_type = type1;
2197 if (pedantic)
2198 pedwarn ("ordered comparison of pointer with integer zero");
2200 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2202 result_type = type0;
2203 if (! flag_traditional)
2204 pedwarn ("comparison between pointer and integer");
2206 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2208 result_type = type1;
2209 if (! flag_traditional)
2210 pedwarn ("comparison between pointer and integer");
2212 break;
2214 case UNORDERED_EXPR:
2215 case ORDERED_EXPR:
2216 case UNLT_EXPR:
2217 case UNLE_EXPR:
2218 case UNGT_EXPR:
2219 case UNGE_EXPR:
2220 case UNEQ_EXPR:
2221 build_type = integer_type_node;
2222 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2224 error ("unordered comparison on non-floating point argument");
2225 return error_mark_node;
2227 common = 1;
2228 break;
2230 default:
2231 break;
2234 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2236 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2238 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2240 if (shorten || common || short_compare)
2241 result_type = common_type (type0, type1);
2243 /* For certain operations (which identify themselves by shorten != 0)
2244 if both args were extended from the same smaller type,
2245 do the arithmetic in that type and then extend.
2247 shorten !=0 and !=1 indicates a bitwise operation.
2248 For them, this optimization is safe only if
2249 both args are zero-extended or both are sign-extended.
2250 Otherwise, we might change the result.
2251 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2252 but calculated in (unsigned short) it would be (unsigned short)-1. */
2254 if (shorten && none_complex)
2256 int unsigned0, unsigned1;
2257 tree arg0 = get_narrower (op0, &unsigned0);
2258 tree arg1 = get_narrower (op1, &unsigned1);
2259 /* UNS is 1 if the operation to be done is an unsigned one. */
2260 int uns = TREE_UNSIGNED (result_type);
2261 tree type;
2263 final_type = result_type;
2265 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2266 but it *requires* conversion to FINAL_TYPE. */
2268 if ((TYPE_PRECISION (TREE_TYPE (op0))
2269 == TYPE_PRECISION (TREE_TYPE (arg0)))
2270 && TREE_TYPE (op0) != final_type)
2271 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2272 if ((TYPE_PRECISION (TREE_TYPE (op1))
2273 == TYPE_PRECISION (TREE_TYPE (arg1)))
2274 && TREE_TYPE (op1) != final_type)
2275 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2277 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2279 /* For bitwise operations, signedness of nominal type
2280 does not matter. Consider only how operands were extended. */
2281 if (shorten == -1)
2282 uns = unsigned0;
2284 /* Note that in all three cases below we refrain from optimizing
2285 an unsigned operation on sign-extended args.
2286 That would not be valid. */
2288 /* Both args variable: if both extended in same way
2289 from same width, do it in that width.
2290 Do it unsigned if args were zero-extended. */
2291 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2292 < TYPE_PRECISION (result_type))
2293 && (TYPE_PRECISION (TREE_TYPE (arg1))
2294 == TYPE_PRECISION (TREE_TYPE (arg0)))
2295 && unsigned0 == unsigned1
2296 && (unsigned0 || !uns))
2297 result_type
2298 = signed_or_unsigned_type (unsigned0,
2299 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2300 else if (TREE_CODE (arg0) == INTEGER_CST
2301 && (unsigned1 || !uns)
2302 && (TYPE_PRECISION (TREE_TYPE (arg1))
2303 < TYPE_PRECISION (result_type))
2304 && (type = signed_or_unsigned_type (unsigned1,
2305 TREE_TYPE (arg1)),
2306 int_fits_type_p (arg0, type)))
2307 result_type = type;
2308 else if (TREE_CODE (arg1) == INTEGER_CST
2309 && (unsigned0 || !uns)
2310 && (TYPE_PRECISION (TREE_TYPE (arg0))
2311 < TYPE_PRECISION (result_type))
2312 && (type = signed_or_unsigned_type (unsigned0,
2313 TREE_TYPE (arg0)),
2314 int_fits_type_p (arg1, type)))
2315 result_type = type;
2318 /* Shifts can be shortened if shifting right. */
2320 if (short_shift)
2322 int unsigned_arg;
2323 tree arg0 = get_narrower (op0, &unsigned_arg);
2325 final_type = result_type;
2327 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2328 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2330 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2331 /* We can shorten only if the shift count is less than the
2332 number of bits in the smaller type size. */
2333 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
2334 /* If arg is sign-extended and then unsigned-shifted,
2335 we can simulate this with a signed shift in arg's type
2336 only if the extended result is at least twice as wide
2337 as the arg. Otherwise, the shift could use up all the
2338 ones made by sign-extension and bring in zeros.
2339 We can't optimize that case at all, but in most machines
2340 it never happens because available widths are 2**N. */
2341 && (!TREE_UNSIGNED (final_type)
2342 || unsigned_arg
2343 || (2 * TYPE_PRECISION (TREE_TYPE (arg0))
2344 <= TYPE_PRECISION (result_type))))
2346 /* Do an unsigned shift if the operand was zero-extended. */
2347 result_type
2348 = signed_or_unsigned_type (unsigned_arg,
2349 TREE_TYPE (arg0));
2350 /* Convert value-to-be-shifted to that type. */
2351 if (TREE_TYPE (op0) != result_type)
2352 op0 = convert (result_type, op0);
2353 converted = 1;
2357 /* Comparison operations are shortened too but differently.
2358 They identify themselves by setting short_compare = 1. */
2360 if (short_compare)
2362 /* Don't write &op0, etc., because that would prevent op0
2363 from being kept in a register.
2364 Instead, make copies of the our local variables and
2365 pass the copies by reference, then copy them back afterward. */
2366 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2367 enum tree_code xresultcode = resultcode;
2368 tree val
2369 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2370 if (val != 0)
2371 return val;
2372 op0 = xop0, op1 = xop1;
2373 converted = 1;
2374 resultcode = xresultcode;
2376 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2377 && skip_evaluation == 0)
2379 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2380 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2382 int unsignedp0, unsignedp1;
2383 tree primop0 = get_narrower (op0, &unsignedp0);
2384 tree primop1 = get_narrower (op1, &unsignedp1);
2386 xop0 = orig_op0;
2387 xop1 = orig_op1;
2388 STRIP_TYPE_NOPS (xop0);
2389 STRIP_TYPE_NOPS (xop1);
2391 /* Give warnings for comparisons between signed and unsigned
2392 quantities that may fail. */
2393 /* Do the checking based on the original operand trees, so that
2394 casts will be considered, but default promotions won't be. */
2396 /* Do not warn if the comparison is being done in a signed type,
2397 since the signed type will only be chosen if it can represent
2398 all the values of the unsigned type. */
2399 if (! TREE_UNSIGNED (result_type))
2400 /* OK */;
2401 /* Do not warn if both operands are the same signedness. */
2402 else if (op0_signed == op1_signed)
2403 /* OK */;
2404 else
2406 tree sop, uop;
2407 if (op0_signed)
2408 sop = xop0, uop = xop1;
2409 else
2410 sop = xop1, uop = xop0;
2412 /* Do not warn if the signed quantity is an unsuffixed
2413 integer literal (or some static constant expression
2414 involving such literals) and it is non-negative. */
2415 if (TREE_CODE (sop) == INTEGER_CST
2416 && tree_int_cst_sgn (sop) >= 0)
2417 /* OK */;
2418 /* Do not warn if the comparison is an equality operation,
2419 the unsigned quantity is an integral constant, and it
2420 would fit in the result if the result were signed. */
2421 else if (TREE_CODE (uop) == INTEGER_CST
2422 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2423 && int_fits_type_p (uop, signed_type (result_type)))
2424 /* OK */;
2425 /* Do not warn if the unsigned quantity is an enumeration
2426 constant and its maximum value would fit in the result
2427 if the result were signed. */
2428 else if (TREE_CODE (uop) == INTEGER_CST
2429 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2430 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2431 signed_type (result_type)))
2432 /* OK */;
2433 else
2434 warning ("comparison between signed and unsigned");
2437 /* Warn if two unsigned values are being compared in a size
2438 larger than their original size, and one (and only one) is the
2439 result of a `~' operator. This comparison will always fail.
2441 Also warn if one operand is a constant, and the constant
2442 does not have all bits set that are set in the ~ operand
2443 when it is extended. */
2445 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2446 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2448 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2449 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2450 &unsignedp0);
2451 else
2452 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2453 &unsignedp1);
2455 if (TREE_CODE (primop0) == INTEGER_CST
2456 || TREE_CODE (primop1) == INTEGER_CST)
2458 tree primop;
2459 HOST_WIDE_INT constant, mask;
2460 int unsignedp, bits;
2462 if (TREE_CODE (primop0) == INTEGER_CST)
2464 primop = primop1;
2465 unsignedp = unsignedp1;
2466 constant = TREE_INT_CST_LOW (primop0);
2468 else
2470 primop = primop0;
2471 unsignedp = unsignedp0;
2472 constant = TREE_INT_CST_LOW (primop1);
2475 bits = TYPE_PRECISION (TREE_TYPE (primop));
2476 if (bits < TYPE_PRECISION (result_type)
2477 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
2479 mask = (~ (HOST_WIDE_INT) 0) << bits;
2480 if ((mask & constant) != mask)
2481 warning ("comparison of promoted ~unsigned with constant");
2484 else if (unsignedp0 && unsignedp1
2485 && (TYPE_PRECISION (TREE_TYPE (primop0))
2486 < TYPE_PRECISION (result_type))
2487 && (TYPE_PRECISION (TREE_TYPE (primop1))
2488 < TYPE_PRECISION (result_type)))
2489 warning ("comparison of promoted ~unsigned with unsigned");
2495 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2496 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2497 Then the expression will be built.
2498 It will be given type FINAL_TYPE if that is nonzero;
2499 otherwise, it will be given type RESULT_TYPE. */
2501 if (!result_type)
2503 binary_op_error (code);
2504 return error_mark_node;
2507 if (! converted)
2509 if (TREE_TYPE (op0) != result_type)
2510 op0 = convert (result_type, op0);
2511 if (TREE_TYPE (op1) != result_type)
2512 op1 = convert (result_type, op1);
2515 if (build_type == NULL_TREE)
2516 build_type = result_type;
2519 register tree result = build (resultcode, build_type, op0, op1);
2520 register tree folded;
2522 folded = fold (result);
2523 if (folded == result)
2524 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2525 if (final_type != 0)
2526 return convert (final_type, folded);
2527 return folded;
2531 /* Return a tree for the sum or difference (RESULTCODE says which)
2532 of pointer PTROP and integer INTOP. */
2534 static tree
2535 pointer_int_sum (resultcode, ptrop, intop)
2536 enum tree_code resultcode;
2537 register tree ptrop, intop;
2539 tree size_exp;
2541 register tree result;
2542 register tree folded;
2544 /* The result is a pointer of the same type that is being added. */
2546 register tree result_type = TREE_TYPE (ptrop);
2548 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2550 if (pedantic || warn_pointer_arith)
2551 pedwarn ("pointer of type `void *' used in arithmetic");
2552 size_exp = integer_one_node;
2554 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2556 if (pedantic || warn_pointer_arith)
2557 pedwarn ("pointer to a function used in arithmetic");
2558 size_exp = integer_one_node;
2560 else
2561 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2563 /* If what we are about to multiply by the size of the elements
2564 contains a constant term, apply distributive law
2565 and multiply that constant term separately.
2566 This helps produce common subexpressions. */
2568 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2569 && ! TREE_CONSTANT (intop)
2570 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2571 && TREE_CONSTANT (size_exp)
2572 /* If the constant comes from pointer subtraction,
2573 skip this optimization--it would cause an error. */
2574 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2575 /* If the constant is unsigned, and smaller than the pointer size,
2576 then we must skip this optimization. This is because it could cause
2577 an overflow error if the constant is negative but INTOP is not. */
2578 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2579 || (TYPE_PRECISION (TREE_TYPE (intop))
2580 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2582 enum tree_code subcode = resultcode;
2583 tree int_type = TREE_TYPE (intop);
2584 if (TREE_CODE (intop) == MINUS_EXPR)
2585 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2586 /* Convert both subexpression types to the type of intop,
2587 because weird cases involving pointer arithmetic
2588 can result in a sum or difference with different type args. */
2589 ptrop = build_binary_op (subcode, ptrop,
2590 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2591 intop = convert (int_type, TREE_OPERAND (intop, 0));
2594 /* Convert the integer argument to a type the same size as sizetype
2595 so the multiply won't overflow spuriously. */
2597 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2598 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2599 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2600 TREE_UNSIGNED (sizetype)), intop);
2602 /* Replace the integer argument with a suitable product by the object size.
2603 Do this multiplication as signed, then convert to the appropriate
2604 pointer type (actually unsigned integral). */
2606 intop = convert (result_type,
2607 build_binary_op (MULT_EXPR, intop,
2608 convert (TREE_TYPE (intop), size_exp), 1));
2610 /* Create the sum or difference. */
2612 result = build (resultcode, result_type, ptrop, intop);
2614 folded = fold (result);
2615 if (folded == result)
2616 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2617 return folded;
2620 /* Return a tree for the difference of pointers OP0 and OP1.
2621 The resulting tree has type int. */
2623 static tree
2624 pointer_diff (op0, op1)
2625 register tree op0, op1;
2627 register tree result, folded;
2628 tree restype = ptrdiff_type_node;
2630 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2632 if (pedantic || warn_pointer_arith)
2634 if (TREE_CODE (target_type) == VOID_TYPE)
2635 pedwarn ("pointer of type `void *' used in subtraction");
2636 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2637 pedwarn ("pointer to a function used in subtraction");
2640 /* First do the subtraction as integers;
2641 then drop through to build the divide operator.
2642 Do not do default conversions on the minus operator
2643 in case restype is a short type. */
2645 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2646 convert (restype, op1), 0);
2647 /* This generates an error if op1 is pointer to incomplete type. */
2648 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2649 error ("arithmetic on pointer to an incomplete type");
2651 /* This generates an error if op0 is pointer to incomplete type. */
2652 op1 = c_size_in_bytes (target_type);
2654 /* Divide by the size, in easiest possible way. */
2656 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2658 folded = fold (result);
2659 if (folded == result)
2660 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2661 return folded;
2664 /* Construct and perhaps optimize a tree representation
2665 for a unary operation. CODE, a tree_code, specifies the operation
2666 and XARG is the operand. NOCONVERT nonzero suppresses
2667 the default promotions (such as from short to int). */
2669 tree
2670 build_unary_op (code, xarg, noconvert)
2671 enum tree_code code;
2672 tree xarg;
2673 int noconvert;
2675 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2676 register tree arg = xarg;
2677 register tree argtype = 0;
2678 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2679 tree val;
2681 if (typecode == ERROR_MARK)
2682 return error_mark_node;
2683 if (typecode == ENUMERAL_TYPE)
2684 typecode = INTEGER_TYPE;
2686 switch (code)
2688 case CONVERT_EXPR:
2689 /* This is used for unary plus, because a CONVERT_EXPR
2690 is enough to prevent anybody from looking inside for
2691 associativity, but won't generate any code. */
2692 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2693 || typecode == COMPLEX_TYPE))
2695 error ("wrong type argument to unary plus");
2696 return error_mark_node;
2698 else if (!noconvert)
2699 arg = default_conversion (arg);
2700 break;
2702 case NEGATE_EXPR:
2703 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2704 || typecode == COMPLEX_TYPE))
2706 error ("wrong type argument to unary minus");
2707 return error_mark_node;
2709 else if (!noconvert)
2710 arg = default_conversion (arg);
2711 break;
2713 case BIT_NOT_EXPR:
2714 if (typecode == COMPLEX_TYPE)
2716 code = CONJ_EXPR;
2717 if (!noconvert)
2718 arg = default_conversion (arg);
2720 else if (typecode != INTEGER_TYPE)
2722 error ("wrong type argument to bit-complement");
2723 return error_mark_node;
2725 else if (!noconvert)
2726 arg = default_conversion (arg);
2727 break;
2729 case ABS_EXPR:
2730 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2731 || typecode == COMPLEX_TYPE))
2733 error ("wrong type argument to abs");
2734 return error_mark_node;
2736 else if (!noconvert)
2737 arg = default_conversion (arg);
2738 break;
2740 case CONJ_EXPR:
2741 /* Conjugating a real value is a no-op, but allow it anyway. */
2742 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2743 || typecode == COMPLEX_TYPE))
2745 error ("wrong type argument to conjugation");
2746 return error_mark_node;
2748 else if (!noconvert)
2749 arg = default_conversion (arg);
2750 break;
2752 case TRUTH_NOT_EXPR:
2753 if (typecode != INTEGER_TYPE
2754 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2755 && typecode != COMPLEX_TYPE
2756 /* These will convert to a pointer. */
2757 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2759 error ("wrong type argument to unary exclamation mark");
2760 return error_mark_node;
2762 arg = truthvalue_conversion (arg);
2763 return invert_truthvalue (arg);
2765 case NOP_EXPR:
2766 break;
2768 case REALPART_EXPR:
2769 if (TREE_CODE (arg) == COMPLEX_CST)
2770 return TREE_REALPART (arg);
2771 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2772 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2773 else
2774 return arg;
2776 case IMAGPART_EXPR:
2777 if (TREE_CODE (arg) == COMPLEX_CST)
2778 return TREE_IMAGPART (arg);
2779 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2780 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2781 else
2782 return convert (TREE_TYPE (arg), integer_zero_node);
2784 case PREINCREMENT_EXPR:
2785 case POSTINCREMENT_EXPR:
2786 case PREDECREMENT_EXPR:
2787 case POSTDECREMENT_EXPR:
2788 /* Handle complex lvalues (when permitted)
2789 by reduction to simpler cases. */
2791 val = unary_complex_lvalue (code, arg);
2792 if (val != 0)
2793 return val;
2795 /* Increment or decrement the real part of the value,
2796 and don't change the imaginary part. */
2797 if (typecode == COMPLEX_TYPE)
2799 tree real, imag;
2801 arg = stabilize_reference (arg);
2802 real = build_unary_op (REALPART_EXPR, arg, 1);
2803 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2804 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2805 build_unary_op (code, real, 1), imag);
2808 /* Report invalid types. */
2810 if (typecode != POINTER_TYPE
2811 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2813 error ("wrong type argument to %s",
2814 code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2815 ? "increment" : "decrement");
2816 return error_mark_node;
2820 register tree inc;
2821 tree result_type = TREE_TYPE (arg);
2823 arg = get_unwidened (arg, 0);
2824 argtype = TREE_TYPE (arg);
2826 /* Compute the increment. */
2828 if (typecode == POINTER_TYPE)
2830 /* If pointer target is an undefined struct,
2831 we just cannot know how to do the arithmetic. */
2832 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2833 error ("%s of pointer to unknown structure",
2834 code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2835 ? "increment" : "decrement");
2836 else if ((pedantic || warn_pointer_arith)
2837 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2838 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2839 pedwarn ("wrong type argument to %s",
2840 code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2841 ? "increment" : "decrement");
2842 inc = c_size_in_bytes (TREE_TYPE (result_type));
2844 else
2845 inc = integer_one_node;
2847 inc = convert (argtype, inc);
2849 /* Handle incrementing a cast-expression. */
2851 while (1)
2852 switch (TREE_CODE (arg))
2854 case NOP_EXPR:
2855 case CONVERT_EXPR:
2856 case FLOAT_EXPR:
2857 case FIX_TRUNC_EXPR:
2858 case FIX_FLOOR_EXPR:
2859 case FIX_ROUND_EXPR:
2860 case FIX_CEIL_EXPR:
2861 pedantic_lvalue_warning (CONVERT_EXPR);
2862 /* If the real type has the same machine representation
2863 as the type it is cast to, we can make better output
2864 by adding directly to the inside of the cast. */
2865 if ((TREE_CODE (TREE_TYPE (arg))
2866 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2867 && (TYPE_MODE (TREE_TYPE (arg))
2868 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2869 arg = TREE_OPERAND (arg, 0);
2870 else
2872 tree incremented, modify, value;
2873 arg = stabilize_reference (arg);
2874 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2875 value = arg;
2876 else
2877 value = save_expr (arg);
2878 incremented = build (((code == PREINCREMENT_EXPR
2879 || code == POSTINCREMENT_EXPR)
2880 ? PLUS_EXPR : MINUS_EXPR),
2881 argtype, value, inc);
2882 TREE_SIDE_EFFECTS (incremented) = 1;
2883 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2884 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2885 TREE_USED (value) = 1;
2886 return value;
2888 break;
2890 default:
2891 goto give_up;
2893 give_up:
2895 /* Complain about anything else that is not a true lvalue. */
2896 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2897 || code == POSTINCREMENT_EXPR)
2898 ? "invalid lvalue in increment"
2899 : "invalid lvalue in decrement")))
2900 return error_mark_node;
2902 /* Report a read-only lvalue. */
2903 if (TREE_READONLY (arg))
2904 readonly_warning (arg,
2905 ((code == PREINCREMENT_EXPR
2906 || code == POSTINCREMENT_EXPR)
2907 ? "increment" : "decrement"));
2909 val = build (code, TREE_TYPE (arg), arg, inc);
2910 TREE_SIDE_EFFECTS (val) = 1;
2911 val = convert (result_type, val);
2912 if (TREE_CODE (val) != code)
2913 TREE_NO_UNUSED_WARNING (val) = 1;
2914 return val;
2917 case ADDR_EXPR:
2918 /* Note that this operation never does default_conversion
2919 regardless of NOCONVERT. */
2921 /* Let &* cancel out to simplify resulting code. */
2922 if (TREE_CODE (arg) == INDIRECT_REF)
2924 /* Don't let this be an lvalue. */
2925 if (lvalue_p (TREE_OPERAND (arg, 0)))
2926 return non_lvalue (TREE_OPERAND (arg, 0));
2927 return TREE_OPERAND (arg, 0);
2930 /* For &x[y], return x+y */
2931 if (TREE_CODE (arg) == ARRAY_REF)
2933 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
2934 return error_mark_node;
2935 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2936 TREE_OPERAND (arg, 1), 1);
2939 /* Handle complex lvalues (when permitted)
2940 by reduction to simpler cases. */
2941 val = unary_complex_lvalue (code, arg);
2942 if (val != 0)
2943 return val;
2945 #if 0 /* Turned off because inconsistent;
2946 float f; *&(int)f = 3.4 stores in int format
2947 whereas (int)f = 3.4 stores in float format. */
2948 /* Address of a cast is just a cast of the address
2949 of the operand of the cast. */
2950 switch (TREE_CODE (arg))
2952 case NOP_EXPR:
2953 case CONVERT_EXPR:
2954 case FLOAT_EXPR:
2955 case FIX_TRUNC_EXPR:
2956 case FIX_FLOOR_EXPR:
2957 case FIX_ROUND_EXPR:
2958 case FIX_CEIL_EXPR:
2959 if (pedantic)
2960 pedwarn ("ANSI C forbids the address of a cast expression");
2961 return convert (build_pointer_type (TREE_TYPE (arg)),
2962 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
2963 0));
2965 #endif
2967 /* Allow the address of a constructor if all the elements
2968 are constant. */
2969 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
2971 /* Anything not already handled and not a true memory reference
2972 is an error. */
2973 else if (typecode != FUNCTION_TYPE
2974 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2975 return error_mark_node;
2977 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2978 argtype = TREE_TYPE (arg);
2979 /* If the lvalue is const or volatile, merge that into the type
2980 to which the address will point. Note that you can't get a
2981 restricted pointer by taking the address of something, so we
2982 only have to deal with `const' and `volatile' here. */
2983 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
2984 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2986 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
2987 argtype = c_build_type_variant (argtype,
2988 TREE_READONLY (arg),
2989 TREE_THIS_VOLATILE (arg));
2992 argtype = build_pointer_type (argtype);
2994 if (mark_addressable (arg) == 0)
2995 return error_mark_node;
2998 tree addr;
3000 if (TREE_CODE (arg) == COMPONENT_REF)
3002 tree field = TREE_OPERAND (arg, 1);
3004 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3006 if (DECL_C_BIT_FIELD (field))
3008 error ("attempt to take address of bit-field structure member `%s'",
3009 IDENTIFIER_POINTER (DECL_NAME (field)));
3010 return error_mark_node;
3013 addr = convert (argtype, addr);
3015 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3017 tree offset
3018 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3019 bitsize_int (BITS_PER_UNIT));
3020 int flag = TREE_CONSTANT (addr);
3022 addr = fold (build (PLUS_EXPR, argtype,
3023 addr, convert (argtype, offset)));
3024 TREE_CONSTANT (addr) = flag;
3027 else
3028 addr = build1 (code, argtype, arg);
3030 /* Address of a static or external variable or
3031 file-scope function counts as a constant. */
3032 if (staticp (arg)
3033 && ! (TREE_CODE (arg) == FUNCTION_DECL
3034 && DECL_CONTEXT (arg) != 0))
3035 TREE_CONSTANT (addr) = 1;
3036 return addr;
3039 default:
3040 break;
3043 if (argtype == 0)
3044 argtype = TREE_TYPE (arg);
3045 return fold (build1 (code, argtype, arg));
3048 #if 0
3049 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3050 convert ARG with the same conversions in the same order
3051 and return the result. */
3053 static tree
3054 convert_sequence (conversions, arg)
3055 tree conversions;
3056 tree arg;
3058 switch (TREE_CODE (conversions))
3060 case NOP_EXPR:
3061 case CONVERT_EXPR:
3062 case FLOAT_EXPR:
3063 case FIX_TRUNC_EXPR:
3064 case FIX_FLOOR_EXPR:
3065 case FIX_ROUND_EXPR:
3066 case FIX_CEIL_EXPR:
3067 return convert (TREE_TYPE (conversions),
3068 convert_sequence (TREE_OPERAND (conversions, 0),
3069 arg));
3071 default:
3072 return arg;
3075 #endif /* 0 */
3077 /* Return nonzero if REF is an lvalue valid for this language.
3078 Lvalues can be assigned, unless their type has TYPE_READONLY.
3079 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3082 lvalue_p (ref)
3083 tree ref;
3085 register enum tree_code code = TREE_CODE (ref);
3087 switch (code)
3089 case REALPART_EXPR:
3090 case IMAGPART_EXPR:
3091 case COMPONENT_REF:
3092 return lvalue_p (TREE_OPERAND (ref, 0));
3094 case STRING_CST:
3095 return 1;
3097 case INDIRECT_REF:
3098 case ARRAY_REF:
3099 case VAR_DECL:
3100 case PARM_DECL:
3101 case RESULT_DECL:
3102 case ERROR_MARK:
3103 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3104 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3106 case BIND_EXPR:
3107 case RTL_EXPR:
3108 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3110 default:
3111 return 0;
3115 /* Return nonzero if REF is an lvalue valid for this language;
3116 otherwise, print an error message and return zero. */
3119 lvalue_or_else (ref, msgid)
3120 tree ref;
3121 const char *msgid;
3123 int win = lvalue_p (ref);
3125 if (! win)
3126 error ("%s", msgid);
3128 return win;
3131 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3132 for certain kinds of expressions which are not really lvalues
3133 but which we can accept as lvalues.
3135 If ARG is not a kind of expression we can handle, return zero. */
3137 static tree
3138 unary_complex_lvalue (code, arg)
3139 enum tree_code code;
3140 tree arg;
3142 /* Handle (a, b) used as an "lvalue". */
3143 if (TREE_CODE (arg) == COMPOUND_EXPR)
3145 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3147 /* If this returns a function type, it isn't really being used as
3148 an lvalue, so don't issue a warning about it. */
3149 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3150 pedantic_lvalue_warning (COMPOUND_EXPR);
3152 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3153 TREE_OPERAND (arg, 0), real_result);
3156 /* Handle (a ? b : c) used as an "lvalue". */
3157 if (TREE_CODE (arg) == COND_EXPR)
3159 pedantic_lvalue_warning (COND_EXPR);
3160 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3161 pedantic_lvalue_warning (COMPOUND_EXPR);
3163 return (build_conditional_expr
3164 (TREE_OPERAND (arg, 0),
3165 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3166 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3169 return 0;
3172 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3173 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3175 static void
3176 pedantic_lvalue_warning (code)
3177 enum tree_code code;
3179 if (pedantic)
3180 switch (code)
3182 case COND_EXPR:
3183 pedwarn ("ANSI C forbids use of conditional expressions as lvalues");
3184 break;
3185 case COMPOUND_EXPR:
3186 pedwarn ("ANSI C forbids use of compound expressions as lvalues");
3187 break;
3188 default:
3189 pedwarn ("ANSI C forbids use of cast expressions as lvalues");
3190 break;
3194 /* Warn about storing in something that is `const'. */
3196 void
3197 readonly_warning (arg, msgid)
3198 tree arg;
3199 const char *msgid;
3201 /* Forbid assignments to iterators. */
3202 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3203 pedwarn ("%s of iterator `%s'", _(msgid),
3204 IDENTIFIER_POINTER (DECL_NAME (arg)));
3206 if (TREE_CODE (arg) == COMPONENT_REF)
3208 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3209 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3210 else
3211 pedwarn ("%s of read-only member `%s'", _(msgid),
3212 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3214 else if (TREE_CODE (arg) == VAR_DECL)
3215 pedwarn ("%s of read-only variable `%s'", _(msgid),
3216 IDENTIFIER_POINTER (DECL_NAME (arg)));
3217 else
3218 pedwarn ("%s of read-only location", _(msgid));
3221 /* Mark EXP saying that we need to be able to take the
3222 address of it; it should not be allocated in a register.
3223 Value is 1 if successful. */
3226 mark_addressable (exp)
3227 tree exp;
3229 register tree x = exp;
3230 while (1)
3231 switch (TREE_CODE (x))
3233 case COMPONENT_REF:
3234 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3236 error ("cannot take address of bitfield `%s'",
3237 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3238 return 0;
3241 /* ... fall through ... */
3243 case ADDR_EXPR:
3244 case ARRAY_REF:
3245 case REALPART_EXPR:
3246 case IMAGPART_EXPR:
3247 x = TREE_OPERAND (x, 0);
3248 break;
3250 case CONSTRUCTOR:
3251 TREE_ADDRESSABLE (x) = 1;
3252 return 1;
3254 case VAR_DECL:
3255 case CONST_DECL:
3256 case PARM_DECL:
3257 case RESULT_DECL:
3258 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3259 && DECL_NONLOCAL (x))
3261 if (TREE_PUBLIC (x))
3263 error ("global register variable `%s' used in nested function",
3264 IDENTIFIER_POINTER (DECL_NAME (x)));
3265 return 0;
3267 pedwarn ("register variable `%s' used in nested function",
3268 IDENTIFIER_POINTER (DECL_NAME (x)));
3270 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3272 if (TREE_PUBLIC (x))
3274 error ("address of global register variable `%s' requested",
3275 IDENTIFIER_POINTER (DECL_NAME (x)));
3276 return 0;
3279 /* If we are making this addressable due to its having
3280 volatile components, give a different error message. Also
3281 handle the case of an unnamed parameter by not trying
3282 to give the name. */
3284 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3286 error ("cannot put object with volatile field into register");
3287 return 0;
3290 pedwarn ("address of register variable `%s' requested",
3291 IDENTIFIER_POINTER (DECL_NAME (x)));
3293 put_var_into_stack (x);
3295 /* drops in */
3296 case FUNCTION_DECL:
3297 TREE_ADDRESSABLE (x) = 1;
3298 #if 0 /* poplevel deals with this now. */
3299 if (DECL_CONTEXT (x) == 0)
3300 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3301 #endif
3303 default:
3304 return 1;
3308 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3310 tree
3311 build_conditional_expr (ifexp, op1, op2)
3312 tree ifexp, op1, op2;
3314 register tree type1;
3315 register tree type2;
3316 register enum tree_code code1;
3317 register enum tree_code code2;
3318 register tree result_type = NULL;
3319 tree orig_op1 = op1, orig_op2 = op2;
3321 ifexp = truthvalue_conversion (default_conversion (ifexp));
3323 #if 0 /* Produces wrong result if within sizeof. */
3324 /* Don't promote the operands separately if they promote
3325 the same way. Return the unpromoted type and let the combined
3326 value get promoted if necessary. */
3328 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3329 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3330 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3331 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3333 if (TREE_CODE (ifexp) == INTEGER_CST)
3334 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3336 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3338 #endif
3340 /* Promote both alternatives. */
3342 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3343 op1 = default_conversion (op1);
3344 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3345 op2 = default_conversion (op2);
3347 if (TREE_CODE (ifexp) == ERROR_MARK
3348 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3349 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3350 return error_mark_node;
3352 type1 = TREE_TYPE (op1);
3353 code1 = TREE_CODE (type1);
3354 type2 = TREE_TYPE (op2);
3355 code2 = TREE_CODE (type2);
3357 /* Quickly detect the usual case where op1 and op2 have the same type
3358 after promotion. */
3359 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3361 if (type1 == type2)
3362 result_type = type1;
3363 else
3364 result_type = TYPE_MAIN_VARIANT (type1);
3366 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3367 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3369 result_type = common_type (type1, type2);
3371 /* If -Wsign-compare, warn here if type1 and type2 have
3372 different signedness. We'll promote the signed to unsigned
3373 and later code won't know it used to be different.
3374 Do this check on the original types, so that explicit casts
3375 will be considered, but default promotions won't. */
3376 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare)
3377 && !skip_evaluation)
3379 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3380 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3382 if (unsigned_op1 ^ unsigned_op2)
3384 /* Do not warn if the result type is signed, since the
3385 signed type will only be chosen if it can represent
3386 all the values of the unsigned type. */
3387 if (! TREE_UNSIGNED (result_type))
3388 /* OK */;
3389 /* Do not warn if the signed quantity is an unsuffixed
3390 integer literal (or some static constant expression
3391 involving such literals) and it is non-negative. */
3392 else if ((unsigned_op2 && TREE_CODE (op1) == INTEGER_CST
3393 && tree_int_cst_sgn (op1) >= 0)
3394 || (unsigned_op1 && TREE_CODE (op2) == INTEGER_CST
3395 && tree_int_cst_sgn (op2) >= 0))
3396 /* OK */;
3397 else
3398 warning ("signed and unsigned type in conditional expression");
3402 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3404 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3405 pedwarn ("ANSI C forbids conditional expr with only one void side");
3406 result_type = void_type_node;
3408 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3410 if (comp_target_types (type1, type2))
3411 result_type = common_type (type1, type2);
3412 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3413 && TREE_CODE (orig_op1) != NOP_EXPR)
3414 result_type = qualify_type (type2, type1);
3415 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3416 && TREE_CODE (orig_op2) != NOP_EXPR)
3417 result_type = qualify_type (type1, type2);
3418 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3420 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3421 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3422 result_type = qualify_type (type1, type2);
3424 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3426 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3427 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3428 result_type = qualify_type (type2, type1);
3430 else
3432 pedwarn ("pointer type mismatch in conditional expression");
3433 result_type = build_pointer_type (void_type_node);
3436 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3438 if (! integer_zerop (op2))
3439 pedwarn ("pointer/integer type mismatch in conditional expression");
3440 else
3442 op2 = null_pointer_node;
3443 #if 0 /* The spec seems to say this is permitted. */
3444 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3445 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3446 #endif
3448 result_type = type1;
3450 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3452 if (!integer_zerop (op1))
3453 pedwarn ("pointer/integer type mismatch in conditional expression");
3454 else
3456 op1 = null_pointer_node;
3457 #if 0 /* The spec seems to say this is permitted. */
3458 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3459 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3460 #endif
3462 result_type = type2;
3465 if (!result_type)
3467 if (flag_cond_mismatch)
3468 result_type = void_type_node;
3469 else
3471 error ("type mismatch in conditional expression");
3472 return error_mark_node;
3476 /* Merge const and volatile flags of the incoming types. */
3477 result_type
3478 = build_type_variant (result_type,
3479 TREE_READONLY (op1) || TREE_READONLY (op2),
3480 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3482 if (result_type != TREE_TYPE (op1))
3483 op1 = convert_and_check (result_type, op1);
3484 if (result_type != TREE_TYPE (op2))
3485 op2 = convert_and_check (result_type, op2);
3487 if (TREE_CODE (ifexp) == INTEGER_CST)
3488 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3490 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3493 /* Given a list of expressions, return a compound expression
3494 that performs them all and returns the value of the last of them. */
3496 tree
3497 build_compound_expr (list)
3498 tree list;
3500 return internal_build_compound_expr (list, TRUE);
3503 static tree
3504 internal_build_compound_expr (list, first_p)
3505 tree list;
3506 int first_p;
3508 register tree rest;
3510 if (TREE_CHAIN (list) == 0)
3512 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3513 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3515 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3516 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3517 list = TREE_OPERAND (list, 0);
3518 #endif
3520 /* Don't let (0, 0) be null pointer constant. */
3521 if (!first_p && integer_zerop (TREE_VALUE (list)))
3522 return non_lvalue (TREE_VALUE (list));
3523 return TREE_VALUE (list);
3526 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3528 /* Convert arrays to pointers when there really is a comma operator. */
3529 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3530 TREE_VALUE (TREE_CHAIN (list))
3531 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3534 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3536 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3538 /* The left-hand operand of a comma expression is like an expression
3539 statement: with -W or -Wunused, we should warn if it doesn't have
3540 any side-effects, unless it was explicitly cast to (void). */
3541 if ((extra_warnings || warn_unused)
3542 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3543 && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
3544 warning ("left-hand operand of comma expression has no effect");
3546 /* When pedantic, a compound expression can be neither an lvalue
3547 nor an integer constant expression. */
3548 if (! pedantic)
3549 return rest;
3552 /* With -Wunused, we should also warn if the left-hand operand does have
3553 side-effects, but computes a value which is not used. For example, in
3554 `foo() + bar(), baz()' the result of the `+' operator is not used,
3555 so we should issue a warning. */
3556 else if (warn_unused)
3557 warn_if_unused_value (TREE_VALUE (list));
3559 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3562 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3564 tree
3565 build_c_cast (type, expr)
3566 register tree type;
3567 tree expr;
3569 register tree value = expr;
3571 if (type == error_mark_node || expr == error_mark_node)
3572 return error_mark_node;
3573 type = TYPE_MAIN_VARIANT (type);
3575 #if 0
3576 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3577 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3578 value = TREE_OPERAND (value, 0);
3579 #endif
3581 if (TREE_CODE (type) == ARRAY_TYPE)
3583 error ("cast specifies array type");
3584 return error_mark_node;
3587 if (TREE_CODE (type) == FUNCTION_TYPE)
3589 error ("cast specifies function type");
3590 return error_mark_node;
3593 if (type == TREE_TYPE (value))
3595 if (pedantic)
3597 if (TREE_CODE (type) == RECORD_TYPE
3598 || TREE_CODE (type) == UNION_TYPE)
3599 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3602 else if (TREE_CODE (type) == UNION_TYPE)
3604 tree field;
3605 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3606 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3607 value = default_conversion (value);
3609 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3610 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3611 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3612 break;
3614 if (field)
3616 const char *name;
3617 tree t;
3619 if (pedantic)
3620 pedwarn ("ANSI C forbids casts to union type");
3621 if (TYPE_NAME (type) != 0)
3623 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3624 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3625 else
3626 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3628 else
3629 name = "";
3630 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3631 build_tree_list (field, value)),
3632 0, 0);
3633 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3634 return t;
3636 error ("cast to union type from type not present in union");
3637 return error_mark_node;
3639 else
3641 tree otype, ovalue;
3643 /* If casting to void, avoid the error that would come
3644 from default_conversion in the case of a non-lvalue array. */
3645 if (type == void_type_node)
3646 return build1 (CONVERT_EXPR, type, value);
3648 /* Convert functions and arrays to pointers,
3649 but don't convert any other types. */
3650 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3651 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3652 value = default_conversion (value);
3653 otype = TREE_TYPE (value);
3655 /* Optionally warn about potentially worrisome casts. */
3657 if (warn_cast_qual
3658 && TREE_CODE (type) == POINTER_TYPE
3659 && TREE_CODE (otype) == POINTER_TYPE)
3661 tree in_type = type;
3662 tree in_otype = otype;
3663 int warn = 0;
3665 /* Check that the qualifiers on IN_TYPE are a superset of
3666 the qualifiers of IN_OTYPE. The outermost level of
3667 POINTER_TYPE nodes is uninteresting and we stop as soon
3668 as we hit a non-POINTER_TYPE node on either type. */
3671 in_otype = TREE_TYPE (in_otype);
3672 in_type = TREE_TYPE (in_type);
3673 warn |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3675 while (TREE_CODE (in_type) == POINTER_TYPE
3676 && TREE_CODE (in_otype) == POINTER_TYPE);
3678 if (warn)
3679 /* There are qualifiers present in IN_OTYPE that are not
3680 present in IN_TYPE. */
3681 pedwarn ("cast discards qualifiers from pointer target type");
3684 /* Warn about possible alignment problems. */
3685 if (STRICT_ALIGNMENT && warn_cast_align
3686 && TREE_CODE (type) == POINTER_TYPE
3687 && TREE_CODE (otype) == POINTER_TYPE
3688 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3689 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3690 /* Don't warn about opaque types, where the actual alignment
3691 restriction is unknown. */
3692 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3693 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3694 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3695 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3696 warning ("cast increases required alignment of target type");
3698 if (TREE_CODE (type) == INTEGER_TYPE
3699 && TREE_CODE (otype) == POINTER_TYPE
3700 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3701 && !TREE_CONSTANT (value))
3702 warning ("cast from pointer to integer of different size");
3704 if (warn_bad_function_cast
3705 && TREE_CODE (value) == CALL_EXPR
3706 && TREE_CODE (type) != TREE_CODE (otype))
3707 warning ("cast does not match function type");
3709 if (TREE_CODE (type) == POINTER_TYPE
3710 && TREE_CODE (otype) == INTEGER_TYPE
3711 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3712 #if 0
3713 /* Don't warn about converting 0 to pointer,
3714 provided the 0 was explicit--not cast or made by folding. */
3715 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3716 #endif
3717 /* Don't warn about converting any constant. */
3718 && !TREE_CONSTANT (value))
3719 warning ("cast to pointer from integer of different size");
3721 ovalue = value;
3722 value = convert (type, value);
3724 /* Ignore any integer overflow caused by the cast. */
3725 if (TREE_CODE (value) == INTEGER_CST)
3727 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3728 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3732 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3733 if (pedantic && TREE_CODE (value) == INTEGER_CST
3734 && TREE_CODE (expr) == INTEGER_CST
3735 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3736 value = non_lvalue (value);
3738 /* If pedantic, don't let a cast be an lvalue. */
3739 if (value == expr && pedantic)
3740 value = non_lvalue (value);
3742 return value;
3745 /* Build an assignment expression of lvalue LHS from value RHS.
3746 MODIFYCODE is the code for a binary operator that we use
3747 to combine the old value of LHS with RHS to get the new value.
3748 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3750 tree
3751 build_modify_expr (lhs, modifycode, rhs)
3752 tree lhs, rhs;
3753 enum tree_code modifycode;
3755 register tree result;
3756 tree newrhs;
3757 tree lhstype = TREE_TYPE (lhs);
3758 tree olhstype = lhstype;
3760 /* Types that aren't fully specified cannot be used in assignments. */
3761 lhs = require_complete_type (lhs);
3763 /* Avoid duplicate error messages from operands that had errors. */
3764 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3765 return error_mark_node;
3767 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3768 /* Do not use STRIP_NOPS here. We do not want an enumerator
3769 whose value is 0 to count as a null pointer constant. */
3770 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3771 rhs = TREE_OPERAND (rhs, 0);
3773 newrhs = rhs;
3775 /* Handle control structure constructs used as "lvalues". */
3777 switch (TREE_CODE (lhs))
3779 /* Handle (a, b) used as an "lvalue". */
3780 case COMPOUND_EXPR:
3781 pedantic_lvalue_warning (COMPOUND_EXPR);
3782 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3783 if (TREE_CODE (newrhs) == ERROR_MARK)
3784 return error_mark_node;
3785 return build (COMPOUND_EXPR, lhstype,
3786 TREE_OPERAND (lhs, 0), newrhs);
3788 /* Handle (a ? b : c) used as an "lvalue". */
3789 case COND_EXPR:
3790 pedantic_lvalue_warning (COND_EXPR);
3791 rhs = save_expr (rhs);
3793 /* Produce (a ? (b = rhs) : (c = rhs))
3794 except that the RHS goes through a save-expr
3795 so the code to compute it is only emitted once. */
3796 tree cond
3797 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3798 build_modify_expr (TREE_OPERAND (lhs, 1),
3799 modifycode, rhs),
3800 build_modify_expr (TREE_OPERAND (lhs, 2),
3801 modifycode, rhs));
3802 if (TREE_CODE (cond) == ERROR_MARK)
3803 return cond;
3804 /* Make sure the code to compute the rhs comes out
3805 before the split. */
3806 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3807 /* But cast it to void to avoid an "unused" error. */
3808 convert (void_type_node, rhs), cond);
3810 default:
3811 break;
3814 /* If a binary op has been requested, combine the old LHS value with the RHS
3815 producing the value we should actually store into the LHS. */
3817 if (modifycode != NOP_EXPR)
3819 lhs = stabilize_reference (lhs);
3820 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3823 /* Handle a cast used as an "lvalue".
3824 We have already performed any binary operator using the value as cast.
3825 Now convert the result to the cast type of the lhs,
3826 and then true type of the lhs and store it there;
3827 then convert result back to the cast type to be the value
3828 of the assignment. */
3830 switch (TREE_CODE (lhs))
3832 case NOP_EXPR:
3833 case CONVERT_EXPR:
3834 case FLOAT_EXPR:
3835 case FIX_TRUNC_EXPR:
3836 case FIX_FLOOR_EXPR:
3837 case FIX_ROUND_EXPR:
3838 case FIX_CEIL_EXPR:
3839 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3840 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3841 newrhs = default_conversion (newrhs);
3843 tree inner_lhs = TREE_OPERAND (lhs, 0);
3844 tree result;
3845 result = build_modify_expr (inner_lhs, NOP_EXPR,
3846 convert (TREE_TYPE (inner_lhs),
3847 convert (lhstype, newrhs)));
3848 if (TREE_CODE (result) == ERROR_MARK)
3849 return result;
3850 pedantic_lvalue_warning (CONVERT_EXPR);
3851 return convert (TREE_TYPE (lhs), result);
3854 default:
3855 break;
3858 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3859 Reject anything strange now. */
3861 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3862 return error_mark_node;
3864 /* Warn about storing in something that is `const'. */
3866 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3867 || ((TREE_CODE (lhstype) == RECORD_TYPE
3868 || TREE_CODE (lhstype) == UNION_TYPE)
3869 && C_TYPE_FIELDS_READONLY (lhstype)))
3870 readonly_warning (lhs, "assignment");
3872 /* If storing into a structure or union member,
3873 it has probably been given type `int'.
3874 Compute the type that would go with
3875 the actual amount of storage the member occupies. */
3877 if (TREE_CODE (lhs) == COMPONENT_REF
3878 && (TREE_CODE (lhstype) == INTEGER_TYPE
3879 || TREE_CODE (lhstype) == REAL_TYPE
3880 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3881 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3883 /* If storing in a field that is in actuality a short or narrower than one,
3884 we must store in the field in its actual type. */
3886 if (lhstype != TREE_TYPE (lhs))
3888 lhs = copy_node (lhs);
3889 TREE_TYPE (lhs) = lhstype;
3892 /* Convert new value to destination type. */
3894 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3895 NULL_TREE, NULL_TREE, 0);
3896 if (TREE_CODE (newrhs) == ERROR_MARK)
3897 return error_mark_node;
3899 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3900 TREE_SIDE_EFFECTS (result) = 1;
3902 /* If we got the LHS in a different type for storing in,
3903 convert the result back to the nominal type of LHS
3904 so that the value we return always has the same type
3905 as the LHS argument. */
3907 if (olhstype == TREE_TYPE (result))
3908 return result;
3909 return convert_for_assignment (olhstype, result, _("assignment"),
3910 NULL_TREE, NULL_TREE, 0);
3913 /* Convert value RHS to type TYPE as preparation for an assignment
3914 to an lvalue of type TYPE.
3915 The real work of conversion is done by `convert'.
3916 The purpose of this function is to generate error messages
3917 for assignments that are not allowed in C.
3918 ERRTYPE is a string to use in error messages:
3919 "assignment", "return", etc. If it is null, this is parameter passing
3920 for a function call (and different error messages are output).
3922 FUNNAME is the name of the function being called,
3923 as an IDENTIFIER_NODE, or null.
3924 PARMNUM is the number of the argument, for printing in error messages. */
3926 static tree
3927 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3928 tree type, rhs;
3929 const char *errtype;
3930 tree fundecl, funname;
3931 int parmnum;
3933 register enum tree_code codel = TREE_CODE (type);
3934 register tree rhstype;
3935 register enum tree_code coder;
3937 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3938 /* Do not use STRIP_NOPS here. We do not want an enumerator
3939 whose value is 0 to count as a null pointer constant. */
3940 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3941 rhs = TREE_OPERAND (rhs, 0);
3943 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3944 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3945 rhs = default_conversion (rhs);
3946 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3947 rhs = decl_constant_value (rhs);
3949 rhstype = TREE_TYPE (rhs);
3950 coder = TREE_CODE (rhstype);
3952 if (coder == ERROR_MARK)
3953 return error_mark_node;
3955 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3957 overflow_warning (rhs);
3958 /* Check for Objective-C protocols. This will issue a warning if
3959 there are protocol violations. No need to use the return value. */
3960 maybe_objc_comptypes (type, rhstype, 0);
3961 return rhs;
3964 if (coder == VOID_TYPE)
3966 error ("void value not ignored as it ought to be");
3967 return error_mark_node;
3969 /* Arithmetic types all interconvert, and enum is treated like int. */
3970 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
3971 || codel == COMPLEX_TYPE)
3972 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
3973 || coder == COMPLEX_TYPE))
3974 return convert_and_check (type, rhs);
3976 /* Conversion to a transparent union from its member types.
3977 This applies only to function arguments. */
3978 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3980 tree memb_types;
3981 tree marginal_memb_type = 0;
3983 for (memb_types = TYPE_FIELDS (type); memb_types;
3984 memb_types = TREE_CHAIN (memb_types))
3986 tree memb_type = TREE_TYPE (memb_types);
3988 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3989 TYPE_MAIN_VARIANT (rhstype)))
3990 break;
3992 if (TREE_CODE (memb_type) != POINTER_TYPE)
3993 continue;
3995 if (coder == POINTER_TYPE)
3997 register tree ttl = TREE_TYPE (memb_type);
3998 register tree ttr = TREE_TYPE (rhstype);
4000 /* Any non-function converts to a [const][volatile] void *
4001 and vice versa; otherwise, targets must be the same.
4002 Meanwhile, the lhs target must have all the qualifiers of
4003 the rhs. */
4004 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4005 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4006 || comp_target_types (memb_type, rhstype))
4008 /* If this type won't generate any warnings, use it. */
4009 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4010 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4011 && TREE_CODE (ttl) == FUNCTION_TYPE)
4012 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4013 == TYPE_QUALS (ttr))
4014 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4015 == TYPE_QUALS (ttl))))
4016 break;
4018 /* Keep looking for a better type, but remember this one. */
4019 if (! marginal_memb_type)
4020 marginal_memb_type = memb_type;
4024 /* Can convert integer zero to any pointer type. */
4025 if (integer_zerop (rhs)
4026 || (TREE_CODE (rhs) == NOP_EXPR
4027 && integer_zerop (TREE_OPERAND (rhs, 0))))
4029 rhs = null_pointer_node;
4030 break;
4034 if (memb_types || marginal_memb_type)
4036 if (! memb_types)
4038 /* We have only a marginally acceptable member type;
4039 it needs a warning. */
4040 register tree ttl = TREE_TYPE (marginal_memb_type);
4041 register tree ttr = TREE_TYPE (rhstype);
4043 /* Const and volatile mean something different for function
4044 types, so the usual warnings are not appropriate. */
4045 if (TREE_CODE (ttr) == FUNCTION_TYPE
4046 && TREE_CODE (ttl) == FUNCTION_TYPE)
4048 /* Because const and volatile on functions are
4049 restrictions that say the function will not do
4050 certain things, it is okay to use a const or volatile
4051 function where an ordinary one is wanted, but not
4052 vice-versa. */
4053 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4054 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4055 errtype, funname, parmnum);
4057 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4058 warn_for_assignment ("%s discards qualifiers from pointer target type",
4059 errtype, funname,
4060 parmnum);
4063 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4064 pedwarn ("ANSI C prohibits argument conversion to union type");
4066 return build1 (NOP_EXPR, type, rhs);
4070 /* Conversions among pointers */
4071 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
4073 register tree ttl = TREE_TYPE (type);
4074 register tree ttr = TREE_TYPE (rhstype);
4076 /* Any non-function converts to a [const][volatile] void *
4077 and vice versa; otherwise, targets must be the same.
4078 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4079 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4080 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4081 || comp_target_types (type, rhstype)
4082 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4083 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4085 if (pedantic
4086 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4087 && TREE_CODE (ttr) == FUNCTION_TYPE)
4089 (TYPE_MAIN_VARIANT (ttr) == void_type_node
4090 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4091 which are not ANSI null ptr constants. */
4092 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4093 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4094 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4095 errtype, funname, parmnum);
4096 /* Const and volatile mean something different for function types,
4097 so the usual warnings are not appropriate. */
4098 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4099 && TREE_CODE (ttl) != FUNCTION_TYPE)
4101 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4102 warn_for_assignment ("%s discards qualifiers from pointer target type",
4103 errtype, funname, parmnum);
4104 /* If this is not a case of ignoring a mismatch in signedness,
4105 no warning. */
4106 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4107 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4108 || comp_target_types (type, rhstype))
4110 /* If there is a mismatch, do warn. */
4111 else if (pedantic)
4112 warn_for_assignment ("pointer targets in %s differ in signedness",
4113 errtype, funname, parmnum);
4115 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4116 && TREE_CODE (ttr) == FUNCTION_TYPE)
4118 /* Because const and volatile on functions are restrictions
4119 that say the function will not do certain things,
4120 it is okay to use a const or volatile function
4121 where an ordinary one is wanted, but not vice-versa. */
4122 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4123 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4124 errtype, funname, parmnum);
4127 else
4128 warn_for_assignment ("%s from incompatible pointer type",
4129 errtype, funname, parmnum);
4130 return convert (type, rhs);
4132 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4134 /* An explicit constant 0 can convert to a pointer,
4135 or one that results from arithmetic, even including
4136 a cast to integer type. */
4137 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4139 ! (TREE_CODE (rhs) == NOP_EXPR
4140 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4141 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4142 && integer_zerop (TREE_OPERAND (rhs, 0))))
4144 warn_for_assignment ("%s makes pointer from integer without a cast",
4145 errtype, funname, parmnum);
4146 return convert (type, rhs);
4148 return null_pointer_node;
4150 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4152 warn_for_assignment ("%s makes integer from pointer without a cast",
4153 errtype, funname, parmnum);
4154 return convert (type, rhs);
4157 if (!errtype)
4159 if (funname)
4161 tree selector = maybe_building_objc_message_expr ();
4163 if (selector && parmnum > 2)
4164 error ("incompatible type for argument %d of `%s'",
4165 parmnum - 2, IDENTIFIER_POINTER (selector));
4166 else
4167 error ("incompatible type for argument %d of `%s'",
4168 parmnum, IDENTIFIER_POINTER (funname));
4170 else
4171 error ("incompatible type for argument %d of indirect function call",
4172 parmnum);
4174 else
4175 error ("incompatible types in %s", errtype);
4177 return error_mark_node;
4180 /* Print a warning using MSGID.
4181 It gets OPNAME as its one parameter.
4182 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4183 FUNCTION and ARGNUM are handled specially if we are building an
4184 Objective-C selector. */
4186 static void
4187 warn_for_assignment (msgid, opname, function, argnum)
4188 const char *msgid;
4189 const char *opname;
4190 tree function;
4191 int argnum;
4193 if (opname == 0)
4195 tree selector = maybe_building_objc_message_expr ();
4196 char * new_opname;
4198 if (selector && argnum > 2)
4200 function = selector;
4201 argnum -= 2;
4203 if (function)
4205 /* Function name is known; supply it. */
4206 const char *argstring = _("passing arg %d of `%s'");
4207 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4208 + strlen (argstring) + 1 + 25
4209 /*%d*/ + 1);
4210 sprintf (new_opname, argstring, argnum,
4211 IDENTIFIER_POINTER (function));
4213 else
4215 /* Function name unknown (call through ptr); just give arg number.*/
4216 const char *argnofun = _("passing arg %d of pointer to function");
4217 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4218 sprintf (new_opname, argnofun, argnum);
4220 opname = new_opname;
4222 pedwarn (msgid, opname);
4225 /* If VALUE is a compound expr all of whose expressions are constant, then
4226 return its value. Otherwise, return error_mark_node.
4228 This is for handling COMPOUND_EXPRs as initializer elements
4229 which is allowed with a warning when -pedantic is specified. */
4231 static tree
4232 valid_compound_expr_initializer (value, endtype)
4233 tree value;
4234 tree endtype;
4236 if (TREE_CODE (value) == COMPOUND_EXPR)
4238 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4239 == error_mark_node)
4240 return error_mark_node;
4241 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4242 endtype);
4244 else if (! TREE_CONSTANT (value)
4245 && ! initializer_constant_valid_p (value, endtype))
4246 return error_mark_node;
4247 else
4248 return value;
4251 /* Perform appropriate conversions on the initial value of a variable,
4252 store it in the declaration DECL,
4253 and print any error messages that are appropriate.
4254 If the init is invalid, store an ERROR_MARK. */
4256 void
4257 store_init_value (decl, init)
4258 tree decl, init;
4260 register tree value, type;
4262 /* If variable's type was invalidly declared, just ignore it. */
4264 type = TREE_TYPE (decl);
4265 if (TREE_CODE (type) == ERROR_MARK)
4266 return;
4268 /* Digest the specified initializer into an expression. */
4270 value = digest_init (type, init, TREE_STATIC (decl),
4271 TREE_STATIC (decl) || pedantic);
4273 /* Store the expression if valid; else report error. */
4275 #if 0
4276 /* Note that this is the only place we can detect the error
4277 in a case such as struct foo bar = (struct foo) { x, y };
4278 where there is one initial value which is a constructor expression. */
4279 if (value == error_mark_node)
4281 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4283 error ("initializer for static variable is not constant");
4284 value = error_mark_node;
4286 else if (TREE_STATIC (decl)
4287 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4289 error ("initializer for static variable uses complicated arithmetic");
4290 value = error_mark_node;
4292 else
4294 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4296 if (! TREE_CONSTANT (value))
4297 pedwarn ("aggregate initializer is not constant");
4298 else if (! TREE_STATIC (value))
4299 pedwarn ("aggregate initializer uses complicated arithmetic");
4302 #endif
4304 DECL_INITIAL (decl) = value;
4306 /* ANSI wants warnings about out-of-range constant initializers. */
4307 STRIP_TYPE_NOPS (value);
4308 constant_expression_warning (value);
4311 /* Methods for storing and printing names for error messages. */
4313 /* Implement a spelling stack that allows components of a name to be pushed
4314 and popped. Each element on the stack is this structure. */
4316 struct spelling
4318 int kind;
4319 union
4321 int i;
4322 const char *s;
4323 } u;
4326 #define SPELLING_STRING 1
4327 #define SPELLING_MEMBER 2
4328 #define SPELLING_BOUNDS 3
4330 static struct spelling *spelling; /* Next stack element (unused). */
4331 static struct spelling *spelling_base; /* Spelling stack base. */
4332 static int spelling_size; /* Size of the spelling stack. */
4334 /* Macros to save and restore the spelling stack around push_... functions.
4335 Alternative to SAVE_SPELLING_STACK. */
4337 #define SPELLING_DEPTH() (spelling - spelling_base)
4338 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4340 /* Save and restore the spelling stack around arbitrary C code. */
4342 #define SAVE_SPELLING_DEPTH(code) \
4344 int __depth = SPELLING_DEPTH (); \
4345 code; \
4346 RESTORE_SPELLING_DEPTH (__depth); \
4349 /* Push an element on the spelling stack with type KIND and assign VALUE
4350 to MEMBER. */
4352 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4354 int depth = SPELLING_DEPTH (); \
4356 if (depth >= spelling_size) \
4358 spelling_size += 10; \
4359 if (spelling_base == 0) \
4360 spelling_base \
4361 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4362 else \
4363 spelling_base \
4364 = (struct spelling *) xrealloc (spelling_base, \
4365 spelling_size * sizeof (struct spelling)); \
4366 RESTORE_SPELLING_DEPTH (depth); \
4369 spelling->kind = (KIND); \
4370 spelling->MEMBER = (VALUE); \
4371 spelling++; \
4374 /* Push STRING on the stack. Printed literally. */
4376 static void
4377 push_string (string)
4378 const char *string;
4380 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4383 /* Push a member name on the stack. Printed as '.' STRING. */
4385 static void
4386 push_member_name (decl)
4387 tree decl;
4390 const char *string
4391 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4392 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4395 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4397 static void
4398 push_array_bounds (bounds)
4399 int bounds;
4401 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4404 /* Compute the maximum size in bytes of the printed spelling. */
4406 static int
4407 spelling_length ()
4409 register int size = 0;
4410 register struct spelling *p;
4412 for (p = spelling_base; p < spelling; p++)
4414 if (p->kind == SPELLING_BOUNDS)
4415 size += 25;
4416 else
4417 size += strlen (p->u.s) + 1;
4420 return size;
4423 /* Print the spelling to BUFFER and return it. */
4425 static char *
4426 print_spelling (buffer)
4427 register char *buffer;
4429 register char *d = buffer;
4430 register struct spelling *p;
4432 for (p = spelling_base; p < spelling; p++)
4433 if (p->kind == SPELLING_BOUNDS)
4435 sprintf (d, "[%d]", p->u.i);
4436 d += strlen (d);
4438 else
4440 register const char *s;
4441 if (p->kind == SPELLING_MEMBER)
4442 *d++ = '.';
4443 for (s = p->u.s; (*d = *s++); d++)
4446 *d++ = '\0';
4447 return buffer;
4450 /* Issue an error message for a bad initializer component.
4451 MSGID identifies the message.
4452 The component name is taken from the spelling stack. */
4454 void
4455 error_init (msgid)
4456 const char *msgid;
4458 char *ofwhat;
4460 error ("%s", msgid);
4461 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4462 if (*ofwhat)
4463 error ("(near initialization for `%s')", ofwhat);
4466 /* Issue a pedantic warning for a bad initializer component.
4467 MSGID identifies the message.
4468 The component name is taken from the spelling stack. */
4470 void
4471 pedwarn_init (msgid)
4472 const char *msgid;
4474 char *ofwhat;
4476 pedwarn ("%s", msgid);
4477 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4478 if (*ofwhat)
4479 pedwarn ("(near initialization for `%s')", ofwhat);
4482 /* Issue a warning for a bad initializer component.
4483 MSGID identifies the message.
4484 The component name is taken from the spelling stack. */
4486 static void
4487 warning_init (msgid)
4488 const char *msgid;
4490 char *ofwhat;
4492 warning ("%s", msgid);
4493 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4494 if (*ofwhat)
4495 warning ("(near initialization for `%s')", ofwhat);
4498 /* Digest the parser output INIT as an initializer for type TYPE.
4499 Return a C expression of type TYPE to represent the initial value.
4501 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4502 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4503 applies only to elements of constructors. */
4505 static tree
4506 digest_init (type, init, require_constant, constructor_constant)
4507 tree type, init;
4508 int require_constant, constructor_constant;
4510 enum tree_code code = TREE_CODE (type);
4511 tree inside_init = init;
4513 if (type == error_mark_node || init == error_mark_node)
4514 return error_mark_node;
4516 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4517 /* Do not use STRIP_NOPS here. We do not want an enumerator
4518 whose value is 0 to count as a null pointer constant. */
4519 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4520 inside_init = TREE_OPERAND (init, 0);
4522 /* Initialization of an array of chars from a string constant
4523 optionally enclosed in braces. */
4525 if (code == ARRAY_TYPE)
4527 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4528 if ((typ1 == char_type_node
4529 || typ1 == signed_char_type_node
4530 || typ1 == unsigned_char_type_node
4531 || typ1 == unsigned_wchar_type_node
4532 || typ1 == signed_wchar_type_node)
4533 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4535 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4536 TYPE_MAIN_VARIANT (type)))
4537 return inside_init;
4539 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4540 != char_type_node)
4541 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4543 error_init ("char-array initialized from wide string");
4544 return error_mark_node;
4546 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4547 == char_type_node)
4548 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4550 error_init ("int-array initialized from non-wide string");
4551 return error_mark_node;
4554 TREE_TYPE (inside_init) = type;
4555 if (TYPE_DOMAIN (type) != 0
4556 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4557 /* Subtract 1 (or sizeof (wchar_t))
4558 because it's ok to ignore the terminating null char
4559 that is counted in the length of the constant. */
4560 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4561 TREE_STRING_LENGTH (inside_init)
4562 - ((TYPE_PRECISION (typ1)
4563 != TYPE_PRECISION (char_type_node))
4564 ? (TYPE_PRECISION (wchar_type_node)
4565 / BITS_PER_UNIT)
4566 : 1)))
4567 pedwarn_init ("initializer-string for array of chars is too long");
4569 return inside_init;
4573 /* Any type can be initialized
4574 from an expression of the same type, optionally with braces. */
4576 if (inside_init && TREE_TYPE (inside_init) != 0
4577 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4578 TYPE_MAIN_VARIANT (type))
4579 || (code == ARRAY_TYPE
4580 && comptypes (TREE_TYPE (inside_init), type))
4581 || (code == POINTER_TYPE
4582 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4583 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4584 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4585 TREE_TYPE (type)))))
4587 if (code == POINTER_TYPE
4588 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4589 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4590 inside_init = default_conversion (inside_init);
4591 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4592 && TREE_CODE (inside_init) != CONSTRUCTOR)
4594 error_init ("array initialized from non-constant array expression");
4595 return error_mark_node;
4598 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4599 inside_init = decl_constant_value (inside_init);
4601 /* Compound expressions can only occur here if -pedantic or
4602 -pedantic-errors is specified. In the later case, we always want
4603 an error. In the former case, we simply want a warning. */
4604 if (require_constant && pedantic
4605 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4607 inside_init
4608 = valid_compound_expr_initializer (inside_init,
4609 TREE_TYPE (inside_init));
4610 if (inside_init == error_mark_node)
4611 error_init ("initializer element is not constant");
4612 else
4613 pedwarn_init ("initializer element is not constant");
4614 if (flag_pedantic_errors)
4615 inside_init = error_mark_node;
4617 else if (require_constant && ! TREE_CONSTANT (inside_init))
4619 error_init ("initializer element is not constant");
4620 inside_init = error_mark_node;
4622 else if (require_constant
4623 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4625 error_init ("initializer element is not computable at load time");
4626 inside_init = error_mark_node;
4629 return inside_init;
4632 /* Handle scalar types, including conversions. */
4634 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4635 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4637 /* Note that convert_for_assignment calls default_conversion
4638 for arrays and functions. We must not call it in the
4639 case where inside_init is a null pointer constant. */
4640 inside_init
4641 = convert_for_assignment (type, init, _("initialization"),
4642 NULL_TREE, NULL_TREE, 0);
4644 if (require_constant && ! TREE_CONSTANT (inside_init))
4646 error_init ("initializer element is not constant");
4647 inside_init = error_mark_node;
4649 else if (require_constant
4650 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4652 error_init ("initializer element is not computable at load time");
4653 inside_init = error_mark_node;
4656 return inside_init;
4659 /* Come here only for records and arrays. */
4661 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4663 error_init ("variable-sized object may not be initialized");
4664 return error_mark_node;
4667 /* Traditionally, you can write struct foo x = 0;
4668 and it initializes the first element of x to 0. */
4669 if (flag_traditional)
4671 tree top = 0, prev = 0, otype = type;
4672 while (TREE_CODE (type) == RECORD_TYPE
4673 || TREE_CODE (type) == ARRAY_TYPE
4674 || TREE_CODE (type) == QUAL_UNION_TYPE
4675 || TREE_CODE (type) == UNION_TYPE)
4677 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4678 if (prev == 0)
4679 top = temp;
4680 else
4681 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4682 prev = temp;
4683 if (TREE_CODE (type) == ARRAY_TYPE)
4684 type = TREE_TYPE (type);
4685 else if (TYPE_FIELDS (type))
4686 type = TREE_TYPE (TYPE_FIELDS (type));
4687 else
4689 error_init ("invalid initializer");
4690 return error_mark_node;
4694 if (otype != type)
4696 TREE_OPERAND (prev, 1)
4697 = build_tree_list (NULL_TREE,
4698 digest_init (type, init, require_constant,
4699 constructor_constant));
4700 return top;
4702 else
4703 return error_mark_node;
4705 error_init ("invalid initializer");
4706 return error_mark_node;
4709 /* Handle initializers that use braces. */
4711 /* Type of object we are accumulating a constructor for.
4712 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4713 static tree constructor_type;
4715 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4716 left to fill. */
4717 static tree constructor_fields;
4719 /* For an ARRAY_TYPE, this is the specified index
4720 at which to store the next element we get.
4721 This is a special INTEGER_CST node that we modify in place. */
4722 static tree constructor_index;
4724 /* For an ARRAY_TYPE, this is the end index of the range
4725 to initialize with the next element, or NULL in the ordinary case
4726 where the element is used just once. */
4727 static tree constructor_range_end;
4729 /* For an ARRAY_TYPE, this is the maximum index. */
4730 static tree constructor_max_index;
4732 /* For a RECORD_TYPE, this is the first field not yet written out. */
4733 static tree constructor_unfilled_fields;
4735 /* For an ARRAY_TYPE, this is the index of the first element
4736 not yet written out.
4737 This is a special INTEGER_CST node that we modify in place. */
4738 static tree constructor_unfilled_index;
4740 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4741 This is so we can generate gaps between fields, when appropriate.
4742 This is a special INTEGER_CST node that we modify in place. */
4743 static tree constructor_bit_index;
4745 /* If we are saving up the elements rather than allocating them,
4746 this is the list of elements so far (in reverse order,
4747 most recent first). */
4748 static tree constructor_elements;
4750 /* 1 if so far this constructor's elements are all compile-time constants. */
4751 static int constructor_constant;
4753 /* 1 if so far this constructor's elements are all valid address constants. */
4754 static int constructor_simple;
4756 /* 1 if this constructor is erroneous so far. */
4757 static int constructor_erroneous;
4759 /* 1 if have called defer_addressed_constants. */
4760 static int constructor_subconstants_deferred;
4762 /* Structure for managing pending initializer elements, organized as an
4763 AVL tree. */
4765 struct init_node
4767 struct init_node *left, *right;
4768 struct init_node *parent;
4769 int balance;
4770 tree purpose;
4771 tree value;
4774 /* Tree of pending elements at this constructor level.
4775 These are elements encountered out of order
4776 which belong at places we haven't reached yet in actually
4777 writing the output.
4778 Will never hold tree nodes across GC runs. */
4779 static struct init_node *constructor_pending_elts;
4781 /* The SPELLING_DEPTH of this constructor. */
4782 static int constructor_depth;
4784 /* 0 if implicitly pushing constructor levels is allowed. */
4785 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4787 static int require_constant_value;
4788 static int require_constant_elements;
4790 /* 1 if it is ok to output this constructor as we read it.
4791 0 means must accumulate a CONSTRUCTOR expression. */
4792 static int constructor_incremental;
4794 /* DECL node for which an initializer is being read.
4795 0 means we are reading a constructor expression
4796 such as (struct foo) {...}. */
4797 static tree constructor_decl;
4799 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4800 static char *constructor_asmspec;
4802 /* Nonzero if this is an initializer for a top-level decl. */
4803 static int constructor_top_level;
4806 /* This stack has a level for each implicit or explicit level of
4807 structuring in the initializer, including the outermost one. It
4808 saves the values of most of the variables above. */
4810 struct constructor_stack
4812 struct constructor_stack *next;
4813 tree type;
4814 tree fields;
4815 tree index;
4816 tree range_end;
4817 tree max_index;
4818 tree unfilled_index;
4819 tree unfilled_fields;
4820 tree bit_index;
4821 tree elements;
4822 int offset;
4823 struct init_node *pending_elts;
4824 int depth;
4825 /* If nonzero, this value should replace the entire
4826 constructor at this level. */
4827 tree replacement_value;
4828 char constant;
4829 char simple;
4830 char implicit;
4831 char incremental;
4832 char erroneous;
4833 char outer;
4836 struct constructor_stack *constructor_stack;
4838 /* This stack records separate initializers that are nested.
4839 Nested initializers can't happen in ANSI C, but GNU C allows them
4840 in cases like { ... (struct foo) { ... } ... }. */
4842 struct initializer_stack
4844 struct initializer_stack *next;
4845 tree decl;
4846 char *asmspec;
4847 struct constructor_stack *constructor_stack;
4848 tree elements;
4849 struct spelling *spelling;
4850 struct spelling *spelling_base;
4851 int spelling_size;
4852 char top_level;
4853 char incremental;
4854 char require_constant_value;
4855 char require_constant_elements;
4856 char deferred;
4859 struct initializer_stack *initializer_stack;
4861 /* Prepare to parse and output the initializer for variable DECL. */
4863 void
4864 start_init (decl, asmspec_tree, top_level)
4865 tree decl;
4866 tree asmspec_tree;
4867 int top_level;
4869 const char *locus;
4870 struct initializer_stack *p
4871 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
4872 char *asmspec = 0;
4874 if (asmspec_tree)
4875 asmspec = TREE_STRING_POINTER (asmspec_tree);
4877 p->decl = constructor_decl;
4878 p->asmspec = constructor_asmspec;
4879 p->incremental = constructor_incremental;
4880 p->require_constant_value = require_constant_value;
4881 p->require_constant_elements = require_constant_elements;
4882 p->constructor_stack = constructor_stack;
4883 p->elements = constructor_elements;
4884 p->spelling = spelling;
4885 p->spelling_base = spelling_base;
4886 p->spelling_size = spelling_size;
4887 p->deferred = constructor_subconstants_deferred;
4888 p->top_level = constructor_top_level;
4889 p->next = initializer_stack;
4890 initializer_stack = p;
4892 constructor_decl = decl;
4893 constructor_incremental = top_level;
4894 constructor_asmspec = asmspec;
4895 constructor_subconstants_deferred = 0;
4896 constructor_top_level = top_level;
4898 if (decl != 0)
4900 require_constant_value = TREE_STATIC (decl);
4901 require_constant_elements
4902 = ((TREE_STATIC (decl) || pedantic)
4903 /* For a scalar, you can always use any value to initialize,
4904 even within braces. */
4905 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4906 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4907 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4908 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4909 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4910 constructor_incremental |= TREE_STATIC (decl);
4912 else
4914 require_constant_value = 0;
4915 require_constant_elements = 0;
4916 locus = "(anonymous)";
4919 constructor_stack = 0;
4921 missing_braces_mentioned = 0;
4923 spelling_base = 0;
4924 spelling_size = 0;
4925 RESTORE_SPELLING_DEPTH (0);
4927 if (locus)
4928 push_string (locus);
4931 void
4932 finish_init ()
4934 struct initializer_stack *p = initializer_stack;
4936 /* Output subconstants (string constants, usually)
4937 that were referenced within this initializer and saved up.
4938 Must do this if and only if we called defer_addressed_constants. */
4939 if (constructor_subconstants_deferred)
4940 output_deferred_addressed_constants ();
4942 /* Free the whole constructor stack of this initializer. */
4943 while (constructor_stack)
4945 struct constructor_stack *q = constructor_stack;
4946 constructor_stack = q->next;
4947 free (q);
4950 /* Pop back to the data of the outer initializer (if any). */
4951 constructor_decl = p->decl;
4952 constructor_asmspec = p->asmspec;
4953 constructor_incremental = p->incremental;
4954 require_constant_value = p->require_constant_value;
4955 require_constant_elements = p->require_constant_elements;
4956 constructor_stack = p->constructor_stack;
4957 constructor_elements = p->elements;
4958 spelling = p->spelling;
4959 spelling_base = p->spelling_base;
4960 spelling_size = p->spelling_size;
4961 constructor_subconstants_deferred = p->deferred;
4962 constructor_top_level = p->top_level;
4963 initializer_stack = p->next;
4964 free (p);
4967 /* Call here when we see the initializer is surrounded by braces.
4968 This is instead of a call to push_init_level;
4969 it is matched by a call to pop_init_level.
4971 TYPE is the type to initialize, for a constructor expression.
4972 For an initializer for a decl, TYPE is zero. */
4974 void
4975 really_start_incremental_init (type)
4976 tree type;
4978 struct constructor_stack *p
4979 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
4981 if (type == 0)
4982 type = TREE_TYPE (constructor_decl);
4984 /* Turn off constructor_incremental if type is a struct with bitfields.
4985 Do this before the first push, so that the corrected value
4986 is available in finish_init. */
4987 check_init_type_bitfields (type);
4989 p->type = constructor_type;
4990 p->fields = constructor_fields;
4991 p->index = constructor_index;
4992 p->range_end = constructor_range_end;
4993 p->max_index = constructor_max_index;
4994 p->unfilled_index = constructor_unfilled_index;
4995 p->unfilled_fields = constructor_unfilled_fields;
4996 p->bit_index = constructor_bit_index;
4997 p->elements = constructor_elements;
4998 p->constant = constructor_constant;
4999 p->simple = constructor_simple;
5000 p->erroneous = constructor_erroneous;
5001 p->pending_elts = constructor_pending_elts;
5002 p->depth = constructor_depth;
5003 p->replacement_value = 0;
5004 p->implicit = 0;
5005 p->incremental = constructor_incremental;
5006 p->outer = 0;
5007 p->next = 0;
5008 constructor_stack = p;
5010 constructor_constant = 1;
5011 constructor_simple = 1;
5012 constructor_depth = SPELLING_DEPTH ();
5013 constructor_elements = 0;
5014 constructor_pending_elts = 0;
5015 constructor_type = type;
5017 if (TREE_CODE (constructor_type) == RECORD_TYPE
5018 || TREE_CODE (constructor_type) == UNION_TYPE)
5020 constructor_fields = TYPE_FIELDS (constructor_type);
5021 /* Skip any nameless bit fields at the beginning. */
5022 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5023 && DECL_NAME (constructor_fields) == 0)
5024 constructor_fields = TREE_CHAIN (constructor_fields);
5025 constructor_unfilled_fields = constructor_fields;
5026 constructor_bit_index = copy_node (size_zero_node);
5028 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5030 constructor_range_end = 0;
5031 if (TYPE_DOMAIN (constructor_type))
5033 constructor_max_index
5034 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5035 constructor_index
5036 = copy_node (convert (bitsizetype,
5037 TYPE_MIN_VALUE
5038 (TYPE_DOMAIN (constructor_type))));
5040 else
5041 constructor_index = copy_node (bitsize_int (0));
5043 constructor_unfilled_index = copy_node (constructor_index);
5045 else
5047 /* Handle the case of int x = {5}; */
5048 constructor_fields = constructor_type;
5049 constructor_unfilled_fields = constructor_type;
5052 if (constructor_incremental)
5054 make_decl_rtl (constructor_decl, constructor_asmspec,
5055 constructor_top_level);
5056 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5058 defer_addressed_constants ();
5059 constructor_subconstants_deferred = 1;
5063 /* Push down into a subobject, for initialization.
5064 If this is for an explicit set of braces, IMPLICIT is 0.
5065 If it is because the next element belongs at a lower level,
5066 IMPLICIT is 1. */
5068 void
5069 push_init_level (implicit)
5070 int implicit;
5072 struct constructor_stack *p;
5074 /* If we've exhausted any levels that didn't have braces,
5075 pop them now. */
5076 while (constructor_stack->implicit)
5078 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5079 || TREE_CODE (constructor_type) == UNION_TYPE)
5080 && constructor_fields == 0)
5081 process_init_element (pop_init_level (1));
5082 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5083 && tree_int_cst_lt (constructor_max_index, constructor_index))
5084 process_init_element (pop_init_level (1));
5085 else
5086 break;
5089 /* Structure elements may require alignment. Do this now if necessary
5090 for the subaggregate, and if it comes next in sequence. Don't do
5091 this for subaggregates that will go on the pending list. */
5092 if (constructor_incremental && constructor_type != 0
5093 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields
5094 && constructor_fields == constructor_unfilled_fields)
5096 /* Advance to offset of this element. */
5097 if (! tree_int_cst_equal (constructor_bit_index,
5098 DECL_FIELD_BITPOS (constructor_fields)))
5100 /* By using unsigned arithmetic, the result will be correct even
5101 in case of overflows, if BITS_PER_UNIT is a power of two. */
5102 unsigned next = (TREE_INT_CST_LOW
5103 (DECL_FIELD_BITPOS (constructor_fields))
5104 / (unsigned)BITS_PER_UNIT);
5105 unsigned here = (TREE_INT_CST_LOW (constructor_bit_index)
5106 / (unsigned)BITS_PER_UNIT);
5108 assemble_zeros ((next - here)
5109 * (unsigned)BITS_PER_UNIT
5110 / (unsigned)BITS_PER_UNIT);
5112 /* Indicate that we have now filled the structure up to the current
5113 field. */
5114 constructor_unfilled_fields = constructor_fields;
5117 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5118 p->type = constructor_type;
5119 p->fields = constructor_fields;
5120 p->index = constructor_index;
5121 p->range_end = constructor_range_end;
5122 p->max_index = constructor_max_index;
5123 p->unfilled_index = constructor_unfilled_index;
5124 p->unfilled_fields = constructor_unfilled_fields;
5125 p->bit_index = constructor_bit_index;
5126 p->elements = constructor_elements;
5127 p->constant = constructor_constant;
5128 p->simple = constructor_simple;
5129 p->erroneous = constructor_erroneous;
5130 p->pending_elts = constructor_pending_elts;
5131 p->depth = constructor_depth;
5132 p->replacement_value = 0;
5133 p->implicit = implicit;
5134 p->incremental = constructor_incremental;
5135 p->outer = 0;
5136 p->next = constructor_stack;
5137 constructor_stack = p;
5139 constructor_constant = 1;
5140 constructor_simple = 1;
5141 constructor_depth = SPELLING_DEPTH ();
5142 constructor_elements = 0;
5143 constructor_pending_elts = 0;
5145 /* Don't die if an entire brace-pair level is superfluous
5146 in the containing level. */
5147 if (constructor_type == 0)
5149 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5150 || TREE_CODE (constructor_type) == UNION_TYPE)
5152 /* Don't die if there are extra init elts at the end. */
5153 if (constructor_fields == 0)
5154 constructor_type = 0;
5155 else
5157 constructor_type = TREE_TYPE (constructor_fields);
5158 push_member_name (constructor_fields);
5159 constructor_depth++;
5160 if (constructor_fields != constructor_unfilled_fields)
5161 constructor_incremental = 0;
5164 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5166 constructor_type = TREE_TYPE (constructor_type);
5167 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5168 constructor_depth++;
5169 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5170 || constructor_range_end != 0)
5171 constructor_incremental = 0;
5174 if (constructor_type == 0)
5176 error_init ("extra brace group at end of initializer");
5177 constructor_fields = 0;
5178 constructor_unfilled_fields = 0;
5179 return;
5182 /* Turn off constructor_incremental if type is a struct with bitfields. */
5183 check_init_type_bitfields (constructor_type);
5185 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5187 missing_braces_mentioned = 1;
5188 warning_init ("missing braces around initializer");
5191 if (TREE_CODE (constructor_type) == RECORD_TYPE
5192 || TREE_CODE (constructor_type) == UNION_TYPE)
5194 constructor_fields = TYPE_FIELDS (constructor_type);
5195 /* Skip any nameless bit fields at the beginning. */
5196 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5197 && DECL_NAME (constructor_fields) == 0)
5198 constructor_fields = TREE_CHAIN (constructor_fields);
5199 constructor_unfilled_fields = constructor_fields;
5200 constructor_bit_index = copy_node (size_zero_node);
5202 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5204 constructor_range_end = 0;
5205 if (TYPE_DOMAIN (constructor_type))
5207 constructor_max_index
5208 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5209 constructor_index
5210 = copy_node (convert (bitsizetype,
5211 TYPE_MIN_VALUE
5212 (TYPE_DOMAIN (constructor_type))));
5214 else
5215 constructor_index = bitsize_int (0);
5217 constructor_unfilled_index = copy_node (constructor_index);
5219 else
5221 warning_init ("braces around scalar initializer");
5222 constructor_fields = constructor_type;
5223 constructor_unfilled_fields = constructor_type;
5227 /* Don't read a struct incrementally if it has any bitfields,
5228 because the incremental reading code doesn't know how to
5229 handle bitfields yet. */
5231 static void
5232 check_init_type_bitfields (type)
5233 tree type;
5235 if (TREE_CODE (type) == RECORD_TYPE)
5237 tree tail;
5238 for (tail = TYPE_FIELDS (type); tail;
5239 tail = TREE_CHAIN (tail))
5241 if (DECL_C_BIT_FIELD (tail))
5243 constructor_incremental = 0;
5244 break;
5247 check_init_type_bitfields (TREE_TYPE (tail));
5251 else if (TREE_CODE (type) == UNION_TYPE)
5253 tree tail = TYPE_FIELDS (type);
5254 if (tail && DECL_C_BIT_FIELD (tail))
5255 /* We also use the nonincremental algorithm for initiliazation
5256 of unions whose first member is a bitfield, becuase the
5257 incremental algorithm has no code for dealing with
5258 bitfields. */
5259 constructor_incremental = 0;
5262 else if (TREE_CODE (type) == ARRAY_TYPE)
5263 check_init_type_bitfields (TREE_TYPE (type));
5266 /* At the end of an implicit or explicit brace level,
5267 finish up that level of constructor.
5268 If we were outputting the elements as they are read, return 0
5269 from inner levels (process_init_element ignores that),
5270 but return error_mark_node from the outermost level
5271 (that's what we want to put in DECL_INITIAL).
5272 Otherwise, return a CONSTRUCTOR expression. */
5274 tree
5275 pop_init_level (implicit)
5276 int implicit;
5278 struct constructor_stack *p;
5279 int size = 0;
5280 tree constructor = 0;
5282 if (implicit == 0)
5284 /* When we come to an explicit close brace,
5285 pop any inner levels that didn't have explicit braces. */
5286 while (constructor_stack->implicit)
5287 process_init_element (pop_init_level (1));
5290 p = constructor_stack;
5292 if (constructor_type != 0)
5293 size = int_size_in_bytes (constructor_type);
5295 /* Warn when some struct elements are implicitly initialized to zero. */
5296 if (extra_warnings
5297 && constructor_type
5298 && TREE_CODE (constructor_type) == RECORD_TYPE
5299 && constructor_unfilled_fields)
5301 push_member_name (constructor_unfilled_fields);
5302 warning_init ("missing initializer");
5303 RESTORE_SPELLING_DEPTH (constructor_depth);
5306 /* Now output all pending elements. */
5307 output_pending_init_elements (1);
5309 #if 0 /* c-parse.in warns about {}. */
5310 /* In ANSI, each brace level must have at least one element. */
5311 if (! implicit && pedantic
5312 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5313 ? integer_zerop (constructor_unfilled_index)
5314 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5315 pedwarn_init ("empty braces in initializer");
5316 #endif
5318 /* Pad out the end of the structure. */
5320 if (p->replacement_value)
5322 /* If this closes a superfluous brace pair,
5323 just pass out the element between them. */
5324 constructor = p->replacement_value;
5325 /* If this is the top level thing within the initializer,
5326 and it's for a variable, then since we already called
5327 assemble_variable, we must output the value now. */
5328 if (p->next == 0 && constructor_decl != 0
5329 && constructor_incremental)
5331 constructor = digest_init (constructor_type, constructor,
5332 require_constant_value,
5333 require_constant_elements);
5335 /* If initializing an array of unknown size,
5336 determine the size now. */
5337 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5338 && TYPE_DOMAIN (constructor_type) == 0)
5340 /* We shouldn't have an incomplete array type within
5341 some other type. */
5342 if (constructor_stack->next)
5343 abort ();
5345 if (complete_array_type (constructor_type, constructor, 0))
5346 abort ();
5348 size = int_size_in_bytes (constructor_type);
5351 output_constant (constructor, size);
5354 else if (constructor_type == 0)
5356 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5357 && TREE_CODE (constructor_type) != UNION_TYPE
5358 && TREE_CODE (constructor_type) != ARRAY_TYPE
5359 && ! constructor_incremental)
5361 /* A nonincremental scalar initializer--just return
5362 the element, after verifying there is just one. */
5363 if (constructor_elements == 0)
5365 error_init ("empty scalar initializer");
5366 constructor = error_mark_node;
5368 else if (TREE_CHAIN (constructor_elements) != 0)
5370 error_init ("extra elements in scalar initializer");
5371 constructor = TREE_VALUE (constructor_elements);
5373 else
5374 constructor = TREE_VALUE (constructor_elements);
5376 else if (! constructor_incremental)
5378 if (constructor_erroneous)
5379 constructor = error_mark_node;
5380 else
5382 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5383 nreverse (constructor_elements));
5384 if (constructor_constant)
5385 TREE_CONSTANT (constructor) = 1;
5386 if (constructor_constant && constructor_simple)
5387 TREE_STATIC (constructor) = 1;
5390 else
5392 tree filled;
5394 if (TREE_CODE (constructor_type) == RECORD_TYPE
5395 || TREE_CODE (constructor_type) == UNION_TYPE)
5396 /* Find the offset of the end of that field. */
5397 filled = size_binop (CEIL_DIV_EXPR,
5398 constructor_bit_index,
5399 size_int (BITS_PER_UNIT));
5401 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5403 /* If initializing an array of unknown size,
5404 determine the size now. */
5405 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5406 && TYPE_DOMAIN (constructor_type) == 0)
5408 tree maxindex
5409 = copy_node (size_diffop (constructor_unfilled_index,
5410 bitsize_int (1)));
5412 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5413 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5415 /* TYPE_MAX_VALUE is always one less than the number of elements
5416 in the array, because we start counting at zero. Therefore,
5417 warn only if the value is less than zero. */
5418 if (pedantic
5419 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5420 < 0))
5421 error_with_decl (constructor_decl,
5422 "zero or negative array size `%s'");
5423 layout_type (constructor_type);
5424 size = int_size_in_bytes (constructor_type);
5427 filled
5428 = size_binop (MULT_EXPR, constructor_unfilled_index,
5429 convert (bitsizetype,
5430 TYPE_SIZE_UNIT
5431 (TREE_TYPE (constructor_type))));
5433 else
5434 filled = 0;
5436 if (filled != 0)
5437 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5441 constructor_type = p->type;
5442 constructor_fields = p->fields;
5443 constructor_index = p->index;
5444 constructor_range_end = p->range_end;
5445 constructor_max_index = p->max_index;
5446 constructor_unfilled_index = p->unfilled_index;
5447 constructor_unfilled_fields = p->unfilled_fields;
5448 constructor_bit_index = p->bit_index;
5449 constructor_elements = p->elements;
5450 constructor_constant = p->constant;
5451 constructor_simple = p->simple;
5452 constructor_erroneous = p->erroneous;
5453 constructor_pending_elts = p->pending_elts;
5454 constructor_depth = p->depth;
5455 constructor_incremental = p->incremental;
5456 RESTORE_SPELLING_DEPTH (constructor_depth);
5458 constructor_stack = p->next;
5459 free (p);
5461 if (constructor == 0)
5463 if (constructor_stack == 0)
5464 return error_mark_node;
5465 return NULL_TREE;
5467 return constructor;
5470 /* Within an array initializer, specify the next index to be initialized.
5471 FIRST is that index. If LAST is nonzero, then initialize a range
5472 of indices, running from FIRST through LAST. */
5474 void
5475 set_init_index (first, last)
5476 tree first, last;
5478 while ((TREE_CODE (first) == NOP_EXPR
5479 || TREE_CODE (first) == CONVERT_EXPR
5480 || TREE_CODE (first) == NON_LVALUE_EXPR)
5481 && (TYPE_MODE (TREE_TYPE (first))
5482 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5483 (first) = TREE_OPERAND (first, 0);
5484 if (last)
5485 while ((TREE_CODE (last) == NOP_EXPR
5486 || TREE_CODE (last) == CONVERT_EXPR
5487 || TREE_CODE (last) == NON_LVALUE_EXPR)
5488 && (TYPE_MODE (TREE_TYPE (last))
5489 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5490 (last) = TREE_OPERAND (last, 0);
5492 if (TREE_CODE (first) != INTEGER_CST)
5493 error_init ("nonconstant array index in initializer");
5494 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5495 error_init ("nonconstant array index in initializer");
5496 else if (! constructor_unfilled_index)
5497 error_init ("array index in non-array initializer");
5498 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5499 error_init ("duplicate array index in initializer");
5500 else
5502 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (first);
5503 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (first);
5505 if (last != 0 && tree_int_cst_lt (last, first))
5506 error_init ("empty index range in initializer");
5507 else
5509 if (pedantic)
5510 pedwarn ("ANSI C forbids specifying element to initialize");
5511 constructor_range_end = last;
5516 /* Within a struct initializer, specify the next field to be initialized. */
5518 void
5519 set_init_label (fieldname)
5520 tree fieldname;
5522 tree tail;
5523 int passed = 0;
5525 /* Don't die if an entire brace-pair level is superfluous
5526 in the containing level. */
5527 if (constructor_type == 0)
5528 return;
5530 for (tail = TYPE_FIELDS (constructor_type); tail;
5531 tail = TREE_CHAIN (tail))
5533 if (tail == constructor_unfilled_fields)
5534 passed = 1;
5535 if (DECL_NAME (tail) == fieldname)
5536 break;
5539 if (tail == 0)
5540 error ("unknown field `%s' specified in initializer",
5541 IDENTIFIER_POINTER (fieldname));
5542 else if (!passed)
5543 error ("field `%s' already initialized",
5544 IDENTIFIER_POINTER (fieldname));
5545 else
5547 constructor_fields = tail;
5548 if (pedantic)
5549 pedwarn ("ANSI C forbids specifying structure member to initialize");
5553 /* Add a new initializer to the tree of pending initializers. PURPOSE
5554 indentifies the initializer, either array index or field in a structure.
5555 VALUE is the value of that index or field. */
5557 static void
5558 add_pending_init (purpose, value)
5559 tree purpose, value;
5561 struct init_node *p, **q, *r;
5563 q = &constructor_pending_elts;
5564 p = 0;
5566 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5568 while (*q != 0)
5570 p = *q;
5571 if (tree_int_cst_lt (purpose, p->purpose))
5572 q = &p->left;
5573 else if (p->purpose != purpose)
5574 q = &p->right;
5575 else
5576 abort ();
5579 else
5581 while (*q != NULL)
5583 p = *q;
5584 if (tree_int_cst_lt (DECL_FIELD_BITPOS (purpose),
5585 DECL_FIELD_BITPOS (p->purpose)))
5586 q = &p->left;
5587 else if (p->purpose != purpose)
5588 q = &p->right;
5589 else
5590 abort ();
5594 r = (struct init_node *) ggc_alloc_obj (sizeof (struct init_node), 0);
5595 r->purpose = purpose;
5596 r->value = value;
5598 *q = r;
5599 r->parent = p;
5600 r->left = 0;
5601 r->right = 0;
5602 r->balance = 0;
5604 while (p)
5606 struct init_node *s;
5608 if (r == p->left)
5610 if (p->balance == 0)
5611 p->balance = -1;
5612 else if (p->balance < 0)
5614 if (r->balance < 0)
5616 /* L rotation. */
5617 p->left = r->right;
5618 if (p->left)
5619 p->left->parent = p;
5620 r->right = p;
5622 p->balance = 0;
5623 r->balance = 0;
5625 s = p->parent;
5626 p->parent = r;
5627 r->parent = s;
5628 if (s)
5630 if (s->left == p)
5631 s->left = r;
5632 else
5633 s->right = r;
5635 else
5636 constructor_pending_elts = r;
5638 else
5640 /* LR rotation. */
5641 struct init_node *t = r->right;
5643 r->right = t->left;
5644 if (r->right)
5645 r->right->parent = r;
5646 t->left = r;
5648 p->left = t->right;
5649 if (p->left)
5650 p->left->parent = p;
5651 t->right = p;
5653 p->balance = t->balance < 0;
5654 r->balance = -(t->balance > 0);
5655 t->balance = 0;
5657 s = p->parent;
5658 p->parent = t;
5659 r->parent = t;
5660 t->parent = s;
5661 if (s)
5663 if (s->left == p)
5664 s->left = t;
5665 else
5666 s->right = t;
5668 else
5669 constructor_pending_elts = t;
5671 break;
5673 else
5675 /* p->balance == +1; growth of left side balances the node. */
5676 p->balance = 0;
5677 break;
5680 else /* r == p->right */
5682 if (p->balance == 0)
5683 /* Growth propagation from right side. */
5684 p->balance++;
5685 else if (p->balance > 0)
5687 if (r->balance > 0)
5689 /* R rotation. */
5690 p->right = r->left;
5691 if (p->right)
5692 p->right->parent = p;
5693 r->left = p;
5695 p->balance = 0;
5696 r->balance = 0;
5698 s = p->parent;
5699 p->parent = r;
5700 r->parent = s;
5701 if (s)
5703 if (s->left == p)
5704 s->left = r;
5705 else
5706 s->right = r;
5708 else
5709 constructor_pending_elts = r;
5711 else /* r->balance == -1 */
5713 /* RL rotation */
5714 struct init_node *t = r->left;
5716 r->left = t->right;
5717 if (r->left)
5718 r->left->parent = r;
5719 t->right = r;
5721 p->right = t->left;
5722 if (p->right)
5723 p->right->parent = p;
5724 t->left = p;
5726 r->balance = (t->balance < 0);
5727 p->balance = -(t->balance > 0);
5728 t->balance = 0;
5730 s = p->parent;
5731 p->parent = t;
5732 r->parent = t;
5733 t->parent = s;
5734 if (s)
5736 if (s->left == p)
5737 s->left = t;
5738 else
5739 s->right = t;
5741 else
5742 constructor_pending_elts = t;
5744 break;
5746 else
5748 /* p->balance == -1; growth of right side balances the node. */
5749 p->balance = 0;
5750 break;
5754 r = p;
5755 p = p->parent;
5759 /* Return nonzero if FIELD is equal to the index of a pending initializer. */
5761 static int
5762 pending_init_member (field)
5763 tree field;
5765 struct init_node *p;
5767 p = constructor_pending_elts;
5768 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5770 while (p)
5772 if (field == p->purpose)
5773 return 1;
5774 else if (tree_int_cst_lt (field, p->purpose))
5775 p = p->left;
5776 else
5777 p = p->right;
5780 else
5782 while (p)
5784 if (field == p->purpose)
5785 return 1;
5786 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (field),
5787 DECL_FIELD_BITPOS (p->purpose)))
5788 p = p->left;
5789 else
5790 p = p->right;
5794 return 0;
5797 /* "Output" the next constructor element.
5798 At top level, really output it to assembler code now.
5799 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5800 TYPE is the data type that the containing data type wants here.
5801 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5803 PENDING if non-nil means output pending elements that belong
5804 right after this element. (PENDING is normally 1;
5805 it is 0 while outputting pending elements, to avoid recursion.) */
5807 static void
5808 output_init_element (value, type, field, pending)
5809 tree value, type, field;
5810 int pending;
5812 int duplicate = 0;
5814 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5815 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5816 && !(TREE_CODE (value) == STRING_CST
5817 && TREE_CODE (type) == ARRAY_TYPE
5818 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5819 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5820 TYPE_MAIN_VARIANT (type))))
5821 value = default_conversion (value);
5823 if (value == error_mark_node)
5824 constructor_erroneous = 1;
5825 else if (!TREE_CONSTANT (value))
5826 constructor_constant = 0;
5827 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5828 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5829 || TREE_CODE (constructor_type) == UNION_TYPE)
5830 && DECL_C_BIT_FIELD (field)
5831 && TREE_CODE (value) != INTEGER_CST))
5832 constructor_simple = 0;
5834 if (require_constant_value && ! TREE_CONSTANT (value))
5836 error_init ("initializer element is not constant");
5837 value = error_mark_node;
5839 else if (require_constant_elements
5840 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5842 error_init ("initializer element is not computable at load time");
5843 value = error_mark_node;
5846 /* If this element duplicates one on constructor_pending_elts,
5847 print a message and ignore it. Don't do this when we're
5848 processing elements taken off constructor_pending_elts,
5849 because we'd always get spurious errors. */
5850 if (pending)
5852 if (TREE_CODE (constructor_type) == RECORD_TYPE
5853 || TREE_CODE (constructor_type) == UNION_TYPE
5854 || TREE_CODE (constructor_type) == ARRAY_TYPE)
5856 if (pending_init_member (field))
5858 error_init ("duplicate initializer");
5859 duplicate = 1;
5864 /* If this element doesn't come next in sequence,
5865 put it on constructor_pending_elts. */
5866 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5867 && !tree_int_cst_equal (field, constructor_unfilled_index))
5869 if (! duplicate)
5870 /* The copy_node is needed in case field is actually
5871 constructor_index, which is modified in place. */
5872 add_pending_init (copy_node (field),
5873 digest_init (type, value, require_constant_value,
5874 require_constant_elements));
5876 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5877 && field != constructor_unfilled_fields)
5879 /* We do this for records but not for unions. In a union,
5880 no matter which field is specified, it can be initialized
5881 right away since it starts at the beginning of the union. */
5882 if (!duplicate)
5883 add_pending_init (field,
5884 digest_init (type, value, require_constant_value,
5885 require_constant_elements));
5887 else
5889 /* Otherwise, output this element either to
5890 constructor_elements or to the assembler file. */
5892 if (!duplicate)
5894 if (! constructor_incremental)
5896 if (field && TREE_CODE (field) == INTEGER_CST)
5897 field = copy_node (field);
5898 constructor_elements
5899 = tree_cons (field, digest_init (type, value,
5900 require_constant_value,
5901 require_constant_elements),
5902 constructor_elements);
5904 else
5906 /* Structure elements may require alignment.
5907 Do this, if necessary. */
5908 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5910 /* Advance to offset of this element. */
5911 if (! tree_int_cst_equal (constructor_bit_index,
5912 DECL_FIELD_BITPOS (field)))
5914 /* By using unsigned arithmetic, the result will be
5915 correct even in case of overflows, if BITS_PER_UNIT
5916 is a power of two. */
5917 unsigned next = (TREE_INT_CST_LOW
5918 (DECL_FIELD_BITPOS (field))
5919 / (unsigned)BITS_PER_UNIT);
5920 unsigned here = (TREE_INT_CST_LOW
5921 (constructor_bit_index)
5922 / (unsigned)BITS_PER_UNIT);
5924 assemble_zeros ((next - here)
5925 * (unsigned)BITS_PER_UNIT
5926 / (unsigned)BITS_PER_UNIT);
5929 output_constant (digest_init (type, value,
5930 require_constant_value,
5931 require_constant_elements),
5932 int_size_in_bytes (type));
5934 /* For a record or union,
5935 keep track of end position of last field. */
5936 if (TREE_CODE (constructor_type) == RECORD_TYPE
5937 || TREE_CODE (constructor_type) == UNION_TYPE)
5939 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
5940 DECL_SIZE (field));
5942 TREE_INT_CST_LOW (constructor_bit_index)
5943 = TREE_INT_CST_LOW (temp);
5944 TREE_INT_CST_HIGH (constructor_bit_index)
5945 = TREE_INT_CST_HIGH (temp);
5950 /* Advance the variable that indicates sequential elements output. */
5951 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5953 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
5954 bitsize_int (1));
5956 TREE_INT_CST_LOW (constructor_unfilled_index)
5957 = TREE_INT_CST_LOW (tem);
5958 TREE_INT_CST_HIGH (constructor_unfilled_index)
5959 = TREE_INT_CST_HIGH (tem);
5961 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5963 constructor_unfilled_fields =
5964 TREE_CHAIN (constructor_unfilled_fields);
5965 /* Skip any nameless bit fields. */
5966 while (constructor_unfilled_fields != 0
5967 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5968 && DECL_NAME (constructor_unfilled_fields) == 0)
5969 constructor_unfilled_fields =
5970 TREE_CHAIN (constructor_unfilled_fields);
5972 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5973 constructor_unfilled_fields = 0;
5975 /* Now output any pending elements which have become next. */
5976 if (pending)
5977 output_pending_init_elements (0);
5981 /* Output any pending elements which have become next.
5982 As we output elements, constructor_unfilled_{fields,index}
5983 advances, which may cause other elements to become next;
5984 if so, they too are output.
5986 If ALL is 0, we return when there are
5987 no more pending elements to output now.
5989 If ALL is 1, we output space as necessary so that
5990 we can output all the pending elements. */
5992 static void
5993 output_pending_init_elements (all)
5994 int all;
5996 struct init_node *elt = constructor_pending_elts;
5997 tree next;
5999 retry:
6001 /* Look thru the whole pending tree.
6002 If we find an element that should be output now,
6003 output it. Otherwise, set NEXT to the element
6004 that comes first among those still pending. */
6006 next = 0;
6007 while (elt)
6009 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6011 if (tree_int_cst_equal (elt->purpose,
6012 constructor_unfilled_index))
6013 output_init_element (elt->value,
6014 TREE_TYPE (constructor_type),
6015 constructor_unfilled_index, 0);
6016 else if (tree_int_cst_lt (constructor_unfilled_index,
6017 elt->purpose))
6019 /* Advance to the next smaller node. */
6020 if (elt->left)
6021 elt = elt->left;
6022 else
6024 /* We have reached the smallest node bigger than the
6025 current unfilled index. Fill the space first. */
6026 next = elt->purpose;
6027 break;
6030 else
6032 /* Advance to the next bigger node. */
6033 if (elt->right)
6034 elt = elt->right;
6035 else
6037 /* We have reached the biggest node in a subtree. Find
6038 the parent of it, which is the next bigger node. */
6039 while (elt->parent && elt->parent->right == elt)
6040 elt = elt->parent;
6041 elt = elt->parent;
6042 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6043 elt->purpose))
6045 next = elt->purpose;
6046 break;
6051 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6052 || TREE_CODE (constructor_type) == UNION_TYPE)
6054 /* If the current record is complete we are done. */
6055 if (constructor_unfilled_fields == 0)
6056 break;
6057 if (elt->purpose == constructor_unfilled_fields)
6059 output_init_element (elt->value,
6060 TREE_TYPE (constructor_unfilled_fields),
6061 constructor_unfilled_fields,
6064 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6065 DECL_FIELD_BITPOS (elt->purpose)))
6067 /* Advance to the next smaller node. */
6068 if (elt->left)
6069 elt = elt->left;
6070 else
6072 /* We have reached the smallest node bigger than the
6073 current unfilled field. Fill the space first. */
6074 next = elt->purpose;
6075 break;
6078 else
6080 /* Advance to the next bigger node. */
6081 if (elt->right)
6082 elt = elt->right;
6083 else
6085 /* We have reached the biggest node in a subtree. Find
6086 the parent of it, which is the next bigger node. */
6087 while (elt->parent && elt->parent->right == elt)
6088 elt = elt->parent;
6089 elt = elt->parent;
6090 if (elt
6091 && tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6092 DECL_FIELD_BITPOS (elt->purpose)))
6094 next = elt->purpose;
6095 break;
6102 /* Ordinarily return, but not if we want to output all
6103 and there are elements left. */
6104 if (! (all && next != 0))
6105 return;
6107 /* Generate space up to the position of NEXT. */
6108 if (constructor_incremental)
6110 tree filled;
6111 tree nextpos_tree = bitsize_int (0);
6113 if (TREE_CODE (constructor_type) == RECORD_TYPE
6114 || TREE_CODE (constructor_type) == UNION_TYPE)
6116 tree tail;
6118 /* Find the last field written out, if any. */
6119 for (tail = TYPE_FIELDS (constructor_type); tail;
6120 tail = TREE_CHAIN (tail))
6121 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
6122 break;
6124 if (tail)
6125 /* Find the offset of the end of that field. */
6126 filled = size_binop (CEIL_DIV_EXPR,
6127 size_binop (PLUS_EXPR,
6128 DECL_FIELD_BITPOS (tail),
6129 DECL_SIZE (tail)),
6130 bitsize_int (BITS_PER_UNIT));
6131 else
6132 filled = bitsize_int (0);
6134 nextpos_tree = size_binop (CEIL_DIV_EXPR,
6135 DECL_FIELD_BITPOS (next),
6136 bitsize_int (BITS_PER_UNIT));
6138 TREE_INT_CST_HIGH (constructor_bit_index)
6139 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
6140 TREE_INT_CST_LOW (constructor_bit_index)
6141 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
6142 constructor_unfilled_fields = next;
6144 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6146 filled
6147 = size_binop (MULT_EXPR, constructor_unfilled_index,
6148 convert (bitsizetype,
6149 TYPE_SIZE_UNIT
6150 (TREE_TYPE (constructor_type))));
6151 nextpos_tree
6152 = size_binop (MULT_EXPR, next,
6153 convert (bitsizetype, TYPE_SIZE_UNIT
6154 (TREE_TYPE (constructor_type))));
6155 TREE_INT_CST_LOW (constructor_unfilled_index)
6156 = TREE_INT_CST_LOW (next);
6157 TREE_INT_CST_HIGH (constructor_unfilled_index)
6158 = TREE_INT_CST_HIGH (next);
6160 else
6161 filled = 0;
6163 if (filled)
6165 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
6167 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
6170 else
6172 /* If it's not incremental, just skip over the gap,
6173 so that after jumping to retry we will output the next
6174 successive element. */
6175 if (TREE_CODE (constructor_type) == RECORD_TYPE
6176 || TREE_CODE (constructor_type) == UNION_TYPE)
6177 constructor_unfilled_fields = next;
6178 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6180 TREE_INT_CST_LOW (constructor_unfilled_index)
6181 = TREE_INT_CST_LOW (next);
6182 TREE_INT_CST_HIGH (constructor_unfilled_index)
6183 = TREE_INT_CST_HIGH (next);
6187 /* ELT now points to the node in the pending tree with the next
6188 initializer to output. */
6189 goto retry;
6192 /* Add one non-braced element to the current constructor level.
6193 This adjusts the current position within the constructor's type.
6194 This may also start or terminate implicit levels
6195 to handle a partly-braced initializer.
6197 Once this has found the correct level for the new element,
6198 it calls output_init_element.
6200 Note: if we are incrementally outputting this constructor,
6201 this function may be called with a null argument
6202 representing a sub-constructor that was already incrementally output.
6203 When that happens, we output nothing, but we do the bookkeeping
6204 to skip past that element of the current constructor. */
6206 void
6207 process_init_element (value)
6208 tree value;
6210 tree orig_value = value;
6211 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6213 /* Handle superfluous braces around string cst as in
6214 char x[] = {"foo"}; */
6215 if (string_flag
6216 && constructor_type
6217 && TREE_CODE (constructor_type) == ARRAY_TYPE
6218 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6219 && integer_zerop (constructor_unfilled_index))
6221 if (constructor_stack->replacement_value)
6222 error_init ("excess elements in char array initializer");
6223 constructor_stack->replacement_value = value;
6224 return;
6227 if (constructor_stack->replacement_value != 0)
6229 error_init ("excess elements in struct initializer");
6230 return;
6233 /* Ignore elements of a brace group if it is entirely superfluous
6234 and has already been diagnosed. */
6235 if (constructor_type == 0)
6236 return;
6238 /* If we've exhausted any levels that didn't have braces,
6239 pop them now. */
6240 while (constructor_stack->implicit)
6242 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6243 || TREE_CODE (constructor_type) == UNION_TYPE)
6244 && constructor_fields == 0)
6245 process_init_element (pop_init_level (1));
6246 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6247 && (constructor_max_index == 0
6248 || tree_int_cst_lt (constructor_max_index,
6249 constructor_index)))
6250 process_init_element (pop_init_level (1));
6251 else
6252 break;
6255 while (1)
6257 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6259 tree fieldtype;
6260 enum tree_code fieldcode;
6262 if (constructor_fields == 0)
6264 pedwarn_init ("excess elements in struct initializer");
6265 break;
6268 fieldtype = TREE_TYPE (constructor_fields);
6269 if (fieldtype != error_mark_node)
6270 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6271 fieldcode = TREE_CODE (fieldtype);
6273 /* Accept a string constant to initialize a subarray. */
6274 if (value != 0
6275 && fieldcode == ARRAY_TYPE
6276 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6277 && string_flag)
6278 value = orig_value;
6279 /* Otherwise, if we have come to a subaggregate,
6280 and we don't have an element of its type, push into it. */
6281 else if (value != 0 && !constructor_no_implicit
6282 && value != error_mark_node
6283 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6284 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6285 || fieldcode == UNION_TYPE))
6287 push_init_level (1);
6288 continue;
6291 if (value)
6293 push_member_name (constructor_fields);
6294 output_init_element (value, fieldtype, constructor_fields, 1);
6295 RESTORE_SPELLING_DEPTH (constructor_depth);
6297 else
6298 /* Do the bookkeeping for an element that was
6299 directly output as a constructor. */
6301 /* For a record, keep track of end position of last field. */
6302 tree temp = size_binop (PLUS_EXPR,
6303 DECL_FIELD_BITPOS (constructor_fields),
6304 DECL_SIZE (constructor_fields));
6306 TREE_INT_CST_LOW (constructor_bit_index)
6307 = TREE_INT_CST_LOW (temp);
6308 TREE_INT_CST_HIGH (constructor_bit_index)
6309 = TREE_INT_CST_HIGH (temp);
6311 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6312 /* Skip any nameless bit fields. */
6313 while (constructor_unfilled_fields != 0
6314 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6315 && DECL_NAME (constructor_unfilled_fields) == 0)
6316 constructor_unfilled_fields =
6317 TREE_CHAIN (constructor_unfilled_fields);
6320 constructor_fields = TREE_CHAIN (constructor_fields);
6321 /* Skip any nameless bit fields at the beginning. */
6322 while (constructor_fields != 0
6323 && DECL_C_BIT_FIELD (constructor_fields)
6324 && DECL_NAME (constructor_fields) == 0)
6325 constructor_fields = TREE_CHAIN (constructor_fields);
6326 break;
6328 if (TREE_CODE (constructor_type) == UNION_TYPE)
6330 tree fieldtype;
6331 enum tree_code fieldcode;
6333 if (constructor_fields == 0)
6335 pedwarn_init ("excess elements in union initializer");
6336 break;
6339 fieldtype = TREE_TYPE (constructor_fields);
6340 if (fieldtype != error_mark_node)
6341 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6342 fieldcode = TREE_CODE (fieldtype);
6344 /* Accept a string constant to initialize a subarray. */
6345 if (value != 0
6346 && fieldcode == ARRAY_TYPE
6347 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6348 && string_flag)
6349 value = orig_value;
6350 /* Otherwise, if we have come to a subaggregate,
6351 and we don't have an element of its type, push into it. */
6352 else if (value != 0 && !constructor_no_implicit
6353 && value != error_mark_node
6354 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6355 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6356 || fieldcode == UNION_TYPE))
6358 push_init_level (1);
6359 continue;
6362 if (value)
6364 push_member_name (constructor_fields);
6365 output_init_element (value, fieldtype, constructor_fields, 1);
6366 RESTORE_SPELLING_DEPTH (constructor_depth);
6368 else
6369 /* Do the bookkeeping for an element that was
6370 directly output as a constructor. */
6372 TREE_INT_CST_LOW (constructor_bit_index)
6373 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6374 TREE_INT_CST_HIGH (constructor_bit_index)
6375 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6377 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6380 constructor_fields = 0;
6381 break;
6383 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6385 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6386 enum tree_code eltcode = TREE_CODE (elttype);
6388 /* Accept a string constant to initialize a subarray. */
6389 if (value != 0
6390 && eltcode == ARRAY_TYPE
6391 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6392 && string_flag)
6393 value = orig_value;
6394 /* Otherwise, if we have come to a subaggregate,
6395 and we don't have an element of its type, push into it. */
6396 else if (value != 0 && !constructor_no_implicit
6397 && value != error_mark_node
6398 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6399 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6400 || eltcode == UNION_TYPE))
6402 push_init_level (1);
6403 continue;
6406 if (constructor_max_index != 0
6407 && tree_int_cst_lt (constructor_max_index, constructor_index))
6409 pedwarn_init ("excess elements in array initializer");
6410 break;
6413 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */
6414 if (constructor_range_end)
6416 if (constructor_max_index != 0
6417 && tree_int_cst_lt (constructor_max_index,
6418 constructor_range_end))
6420 pedwarn_init ("excess elements in array initializer");
6421 TREE_INT_CST_HIGH (constructor_range_end)
6422 = TREE_INT_CST_HIGH (constructor_max_index);
6423 TREE_INT_CST_LOW (constructor_range_end)
6424 = TREE_INT_CST_LOW (constructor_max_index);
6427 value = save_expr (value);
6430 /* Now output the actual element.
6431 Ordinarily, output once.
6432 If there is a range, repeat it till we advance past the range. */
6435 tree tem;
6437 if (value)
6439 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6440 output_init_element (value, elttype, constructor_index, 1);
6441 RESTORE_SPELLING_DEPTH (constructor_depth);
6444 tem = size_binop (PLUS_EXPR, constructor_index, bitsize_int (1));
6445 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (tem);
6446 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (tem);
6448 if (!value)
6449 /* If we are doing the bookkeeping for an element that was
6450 directly output as a constructor,
6451 we must update constructor_unfilled_index. */
6453 TREE_INT_CST_LOW (constructor_unfilled_index)
6454 = TREE_INT_CST_LOW (constructor_index);
6455 TREE_INT_CST_HIGH (constructor_unfilled_index)
6456 = TREE_INT_CST_HIGH (constructor_index);
6459 while (! (constructor_range_end == 0
6460 || tree_int_cst_lt (constructor_range_end,
6461 constructor_index)));
6463 break;
6466 /* Handle the sole element allowed in a braced initializer
6467 for a scalar variable. */
6468 if (constructor_fields == 0)
6470 pedwarn_init ("excess elements in scalar initializer");
6471 break;
6474 if (value)
6475 output_init_element (value, constructor_type, NULL_TREE, 1);
6476 constructor_fields = 0;
6477 break;
6481 /* Expand an ASM statement with operands, handling output operands
6482 that are not variables or INDIRECT_REFS by transforming such
6483 cases into cases that expand_asm_operands can handle.
6485 Arguments are same as for expand_asm_operands. */
6487 void
6488 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6489 tree string, outputs, inputs, clobbers;
6490 int vol;
6491 char *filename;
6492 int line;
6494 int noutputs = list_length (outputs);
6495 register int i;
6496 /* o[I] is the place that output number I should be written. */
6497 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6498 register tree tail;
6500 if (TREE_CODE (string) == ADDR_EXPR)
6501 string = TREE_OPERAND (string, 0);
6502 if (TREE_CODE (string) != STRING_CST)
6504 error ("asm template is not a string constant");
6505 return;
6508 /* Record the contents of OUTPUTS before it is modified. */
6509 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6511 tree output = TREE_VALUE (tail);
6513 /* We can remove conversions that just change the type, not the mode. */
6514 STRIP_NOPS (output);
6515 o[i] = output;
6517 /* Allow conversions as LHS here. build_modify_expr as called below
6518 will do the right thing with them. */
6519 while (TREE_CODE (output) == NOP_EXPR
6520 || TREE_CODE (output) == CONVERT_EXPR
6521 || TREE_CODE (output) == FLOAT_EXPR
6522 || TREE_CODE (output) == FIX_TRUNC_EXPR
6523 || TREE_CODE (output) == FIX_FLOOR_EXPR
6524 || TREE_CODE (output) == FIX_ROUND_EXPR
6525 || TREE_CODE (output) == FIX_CEIL_EXPR)
6526 output = TREE_OPERAND (output, 0);
6528 lvalue_or_else (o[i], "invalid lvalue in asm statement");
6531 /* Perform default conversions on array and function inputs. */
6532 /* Don't do this for other types--
6533 it would screw up operands expected to be in memory. */
6534 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6535 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6536 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6537 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6539 /* Generate the ASM_OPERANDS insn;
6540 store into the TREE_VALUEs of OUTPUTS some trees for
6541 where the values were actually stored. */
6542 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6544 /* Copy all the intermediate outputs into the specified outputs. */
6545 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6547 if (o[i] != TREE_VALUE (tail))
6549 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6550 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6551 free_temp_slots ();
6553 /* Detect modification of read-only values.
6554 (Otherwise done by build_modify_expr.) */
6555 else
6557 tree type = TREE_TYPE (o[i]);
6558 if (TREE_READONLY (o[i])
6559 || TYPE_READONLY (type)
6560 || ((TREE_CODE (type) == RECORD_TYPE
6561 || TREE_CODE (type) == UNION_TYPE)
6562 && C_TYPE_FIELDS_READONLY (type)))
6563 readonly_warning (o[i], "modification by `asm'");
6567 /* Those MODIFY_EXPRs could do autoincrements. */
6568 emit_queue ();
6571 /* Expand a C `return' statement.
6572 RETVAL is the expression for what to return,
6573 or a null pointer for `return;' with no value. */
6575 void
6576 c_expand_return (retval)
6577 tree retval;
6579 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6581 if (TREE_THIS_VOLATILE (current_function_decl))
6582 warning ("function declared `noreturn' has a `return' statement");
6584 if (!retval)
6586 current_function_returns_null = 1;
6587 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6588 warning ("`return' with no value, in function returning non-void");
6589 expand_null_return ();
6591 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6593 current_function_returns_null = 1;
6594 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6595 pedwarn ("`return' with a value, in function returning void");
6596 expand_return (retval);
6598 else
6600 tree t = convert_for_assignment (valtype, retval, _("return"),
6601 NULL_TREE, NULL_TREE, 0);
6602 tree res = DECL_RESULT (current_function_decl);
6603 tree inner;
6605 if (t == error_mark_node)
6606 return;
6608 inner = t = convert (TREE_TYPE (res), t);
6610 /* Strip any conversions, additions, and subtractions, and see if
6611 we are returning the address of a local variable. Warn if so. */
6612 while (1)
6614 switch (TREE_CODE (inner))
6616 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6617 case PLUS_EXPR:
6618 inner = TREE_OPERAND (inner, 0);
6619 continue;
6621 case MINUS_EXPR:
6622 /* If the second operand of the MINUS_EXPR has a pointer
6623 type (or is converted from it), this may be valid, so
6624 don't give a warning. */
6626 tree op1 = TREE_OPERAND (inner, 1);
6628 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6629 && (TREE_CODE (op1) == NOP_EXPR
6630 || TREE_CODE (op1) == NON_LVALUE_EXPR
6631 || TREE_CODE (op1) == CONVERT_EXPR))
6632 op1 = TREE_OPERAND (op1, 0);
6634 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6635 break;
6637 inner = TREE_OPERAND (inner, 0);
6638 continue;
6641 case ADDR_EXPR:
6642 inner = TREE_OPERAND (inner, 0);
6644 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6645 inner = TREE_OPERAND (inner, 0);
6647 if (TREE_CODE (inner) == VAR_DECL
6648 && ! DECL_EXTERNAL (inner)
6649 && ! TREE_STATIC (inner)
6650 && DECL_CONTEXT (inner) == current_function_decl)
6651 warning ("function returns address of local variable");
6652 break;
6654 default:
6655 break;
6658 break;
6661 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6662 TREE_SIDE_EFFECTS (t) = 1;
6663 expand_return (t);
6664 current_function_returns_value = 1;
6668 /* Start a C switch statement, testing expression EXP.
6669 Return EXP if it is valid, an error node otherwise. */
6671 tree
6672 c_expand_start_case (exp)
6673 tree exp;
6675 register enum tree_code code;
6676 tree type;
6678 if (TREE_CODE (exp) == ERROR_MARK)
6679 return exp;
6681 code = TREE_CODE (TREE_TYPE (exp));
6682 type = TREE_TYPE (exp);
6684 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6686 error ("switch quantity not an integer");
6687 exp = error_mark_node;
6689 else
6691 tree index;
6692 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6694 if (warn_traditional
6695 && ! in_system_header
6696 && (type == long_integer_type_node
6697 || type == long_unsigned_type_node))
6698 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6700 exp = default_conversion (exp);
6701 type = TREE_TYPE (exp);
6702 index = get_unwidened (exp, NULL_TREE);
6703 /* We can't strip a conversion from a signed type to an unsigned,
6704 because if we did, int_fits_type_p would do the wrong thing
6705 when checking case values for being in range,
6706 and it's too hard to do the right thing. */
6707 if (TREE_UNSIGNED (TREE_TYPE (exp))
6708 == TREE_UNSIGNED (TREE_TYPE (index)))
6709 exp = index;
6712 expand_start_case (1, exp, type, "switch statement");
6714 return exp;