* cp-tree.h (DECL_LOCAL_FUCNTION_P): New macro.
[official-gcc.git] / gcc / c-typeck.c
blob4e6cadc1ff5bd704dd5627a5b0125fb5b61a3e52
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 88, 91-97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This file is part of the C front end.
23 It contains routines to build C expressions given their operands,
24 including computing the types of the result, C-specific error checks,
25 and some optimization.
27 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
28 and to process initializations in declarations (since they work
29 like a strange sort of assignment). */
31 #include "config.h"
32 #include "system.h"
33 #include "tree.h"
34 #include "c-tree.h"
35 #include "tm_p.h"
36 #include "flags.h"
37 #include "output.h"
38 #include "rtl.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "defaults.h"
44 /* Nonzero if we've already printed a "missing braces around initializer"
45 message within this initializer. */
46 static int missing_braces_mentioned;
48 static tree qualify_type PROTO((tree, tree));
49 static int comp_target_types PROTO((tree, tree));
50 static int function_types_compatible_p PROTO((tree, tree));
51 static int type_lists_compatible_p PROTO((tree, tree));
52 static tree decl_constant_value PROTO((tree));
53 static tree lookup_field PROTO((tree, tree, tree *));
54 static tree convert_arguments PROTO((tree, tree, tree, tree));
55 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
56 static tree pointer_diff PROTO((tree, tree));
57 static tree unary_complex_lvalue PROTO((enum tree_code, tree));
58 static void pedantic_lvalue_warning PROTO((enum tree_code));
59 static tree internal_build_compound_expr PROTO((tree, int));
60 static tree convert_for_assignment PROTO((tree, tree, const char *, tree,
61 tree, int));
62 static void warn_for_assignment PROTO((const char *, const char *,
63 tree, int));
64 static tree valid_compound_expr_initializer PROTO((tree, tree));
65 static void push_string PROTO((const char *));
66 static void push_member_name PROTO((tree));
67 static void push_array_bounds PROTO((int));
68 static int spelling_length PROTO((void));
69 static char *print_spelling PROTO((char *));
70 static void warning_init PROTO((const char *));
71 static tree digest_init PROTO((tree, tree, int, int));
72 static void check_init_type_bitfields PROTO((tree));
73 static void output_init_element PROTO((tree, tree, tree, int));
74 static void output_pending_init_elements PROTO((int));
75 static void add_pending_init PROTO((tree, tree));
76 static int pending_init_member PROTO((tree));
78 /* Do `exp = require_complete_type (exp);' to make sure exp
79 does not have an incomplete type. (That includes void types.) */
81 tree
82 require_complete_type (value)
83 tree value;
85 tree type = TREE_TYPE (value);
87 if (TREE_CODE (value) == ERROR_MARK)
88 return error_mark_node;
90 /* First, detect a valid value with a complete type. */
91 if (TYPE_SIZE (type) != 0
92 && type != void_type_node)
93 return value;
95 incomplete_type_error (value, type);
96 return error_mark_node;
99 /* Print an error message for invalid use of an incomplete type.
100 VALUE is the expression that was used (or 0 if that isn't known)
101 and TYPE is the type that was invalid. */
103 void
104 incomplete_type_error (value, type)
105 tree value;
106 tree type;
108 const char *type_code_string;
110 /* Avoid duplicate error message. */
111 if (TREE_CODE (type) == ERROR_MARK)
112 return;
114 if (value != 0 && (TREE_CODE (value) == VAR_DECL
115 || TREE_CODE (value) == PARM_DECL))
116 error ("`%s' has an incomplete type",
117 IDENTIFIER_POINTER (DECL_NAME (value)));
118 else
120 retry:
121 /* We must print an error message. Be clever about what it says. */
123 switch (TREE_CODE (type))
125 case RECORD_TYPE:
126 type_code_string = "struct";
127 break;
129 case UNION_TYPE:
130 type_code_string = "union";
131 break;
133 case ENUMERAL_TYPE:
134 type_code_string = "enum";
135 break;
137 case VOID_TYPE:
138 error ("invalid use of void expression");
139 return;
141 case ARRAY_TYPE:
142 if (TYPE_DOMAIN (type))
144 type = TREE_TYPE (type);
145 goto retry;
147 error ("invalid use of array with unspecified bounds");
148 return;
150 default:
151 abort ();
154 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
155 error ("invalid use of undefined type `%s %s'",
156 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
157 else
158 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
159 error ("invalid use of incomplete typedef `%s'",
160 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
164 /* Return a variant of TYPE which has all the type qualifiers of LIKE
165 as well as those of TYPE. */
167 static tree
168 qualify_type (type, like)
169 tree type, like;
171 return c_build_qualified_type (type,
172 TYPE_QUALS (type) | TYPE_QUALS (like));
175 /* Return the common type of two types.
176 We assume that comptypes has already been done and returned 1;
177 if that isn't so, this may crash. In particular, we assume that qualifiers
178 match.
180 This is the type for the result of most arithmetic operations
181 if the operands have the given two types. */
183 tree
184 common_type (t1, t2)
185 tree t1, t2;
187 register enum tree_code code1;
188 register enum tree_code code2;
189 tree attributes;
191 /* Save time if the two types are the same. */
193 if (t1 == t2) return t1;
195 /* If one type is nonsense, use the other. */
196 if (t1 == error_mark_node)
197 return t2;
198 if (t2 == error_mark_node)
199 return t1;
201 /* Merge the attributes. */
202 attributes = merge_machine_type_attributes (t1, t2);
204 /* Treat an enum type as the unsigned integer type of the same width. */
206 if (TREE_CODE (t1) == ENUMERAL_TYPE)
207 t1 = type_for_size (TYPE_PRECISION (t1), 1);
208 if (TREE_CODE (t2) == ENUMERAL_TYPE)
209 t2 = type_for_size (TYPE_PRECISION (t2), 1);
211 code1 = TREE_CODE (t1);
212 code2 = TREE_CODE (t2);
214 /* If one type is complex, form the common type of the non-complex
215 components, then make that complex. Use T1 or T2 if it is the
216 required type. */
217 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
219 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
220 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
221 tree subtype = common_type (subtype1, subtype2);
223 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
224 return build_type_attribute_variant (t1, attributes);
225 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
226 return build_type_attribute_variant (t2, attributes);
227 else
228 return build_type_attribute_variant (build_complex_type (subtype),
229 attributes);
232 switch (code1)
234 case INTEGER_TYPE:
235 case REAL_TYPE:
236 /* If only one is real, use it as the result. */
238 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
239 return build_type_attribute_variant (t1, attributes);
241 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
242 return build_type_attribute_variant (t2, attributes);
244 /* Both real or both integers; use the one with greater precision. */
246 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
247 return build_type_attribute_variant (t1, attributes);
248 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
249 return build_type_attribute_variant (t2, attributes);
251 /* Same precision. Prefer longs to ints even when same size. */
253 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
254 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
255 return build_type_attribute_variant (long_unsigned_type_node,
256 attributes);
258 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
259 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
261 /* But preserve unsignedness from the other type,
262 since long cannot hold all the values of an unsigned int. */
263 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
264 t1 = long_unsigned_type_node;
265 else
266 t1 = long_integer_type_node;
267 return build_type_attribute_variant (t1, attributes);
270 /* Likewise, prefer long double to double even if same size. */
271 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
272 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
273 return build_type_attribute_variant (long_double_type_node,
274 attributes);
276 /* Otherwise prefer the unsigned one. */
278 if (TREE_UNSIGNED (t1))
279 return build_type_attribute_variant (t1, attributes);
280 else
281 return build_type_attribute_variant (t2, attributes);
283 case POINTER_TYPE:
284 /* For two pointers, do this recursively on the target type,
285 and combine the qualifiers of the two types' targets. */
286 /* This code was turned off; I don't know why.
287 But ANSI C specifies doing this with the qualifiers.
288 So I turned it on again. */
290 tree pointed_to_1 = TREE_TYPE (t1);
291 tree pointed_to_2 = TREE_TYPE (t2);
292 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
293 TYPE_MAIN_VARIANT (pointed_to_2));
294 t1 = build_pointer_type (c_build_qualified_type
295 (target,
296 TYPE_QUALS (pointed_to_1) |
297 TYPE_QUALS (pointed_to_2)));
298 return build_type_attribute_variant (t1, attributes);
300 #if 0
301 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
302 return build_type_attribute_variant (t1, attributes);
303 #endif
305 case ARRAY_TYPE:
307 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
308 /* Save space: see if the result is identical to one of the args. */
309 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
310 return build_type_attribute_variant (t1, attributes);
311 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
312 return build_type_attribute_variant (t2, attributes);
313 /* Merge the element types, and have a size if either arg has one. */
314 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
315 return build_type_attribute_variant (t1, attributes);
318 case FUNCTION_TYPE:
319 /* Function types: prefer the one that specified arg types.
320 If both do, merge the arg types. Also merge the return types. */
322 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
323 tree p1 = TYPE_ARG_TYPES (t1);
324 tree p2 = TYPE_ARG_TYPES (t2);
325 int len;
326 tree newargs, n;
327 int i;
329 /* Save space: see if the result is identical to one of the args. */
330 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
331 return build_type_attribute_variant (t1, attributes);
332 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
333 return build_type_attribute_variant (t2, attributes);
335 /* Simple way if one arg fails to specify argument types. */
336 if (TYPE_ARG_TYPES (t1) == 0)
338 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
339 return build_type_attribute_variant (t1, attributes);
341 if (TYPE_ARG_TYPES (t2) == 0)
343 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
344 return build_type_attribute_variant (t1, attributes);
347 /* If both args specify argument types, we must merge the two
348 lists, argument by argument. */
350 len = list_length (p1);
351 newargs = 0;
353 for (i = 0; i < len; i++)
354 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
356 n = newargs;
358 for (; p1;
359 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
361 /* A null type means arg type is not specified.
362 Take whatever the other function type has. */
363 if (TREE_VALUE (p1) == 0)
365 TREE_VALUE (n) = TREE_VALUE (p2);
366 goto parm_done;
368 if (TREE_VALUE (p2) == 0)
370 TREE_VALUE (n) = TREE_VALUE (p1);
371 goto parm_done;
374 /* Given wait (union {union wait *u; int *i} *)
375 and wait (union wait *),
376 prefer union wait * as type of parm. */
377 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
378 && TREE_VALUE (p1) != TREE_VALUE (p2))
380 tree memb;
381 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
382 memb; memb = TREE_CHAIN (memb))
383 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
385 TREE_VALUE (n) = TREE_VALUE (p2);
386 if (pedantic)
387 pedwarn ("function types not truly compatible in ANSI C");
388 goto parm_done;
391 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
392 && TREE_VALUE (p2) != TREE_VALUE (p1))
394 tree memb;
395 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
396 memb; memb = TREE_CHAIN (memb))
397 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
399 TREE_VALUE (n) = TREE_VALUE (p1);
400 if (pedantic)
401 pedwarn ("function types not truly compatible in ANSI C");
402 goto parm_done;
405 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
406 parm_done: ;
409 t1 = build_function_type (valtype, newargs);
410 /* ... falls through ... */
413 default:
414 return build_type_attribute_variant (t1, attributes);
419 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
420 or various other operations. Return 2 if they are compatible
421 but a warning may be needed if you use them together. */
424 comptypes (type1, type2)
425 tree type1, type2;
427 register tree t1 = type1;
428 register tree t2 = type2;
429 int attrval, val;
431 /* Suppress errors caused by previously reported errors. */
433 if (t1 == t2 || !t1 || !t2
434 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
435 return 1;
437 /* Treat an enum type as the integer type of the same width and
438 signedness. */
440 if (TREE_CODE (t1) == ENUMERAL_TYPE)
441 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
442 if (TREE_CODE (t2) == ENUMERAL_TYPE)
443 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
445 if (t1 == t2)
446 return 1;
448 /* Different classes of types can't be compatible. */
450 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
452 /* Qualifiers must match. */
454 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
455 return 0;
457 /* Allow for two different type nodes which have essentially the same
458 definition. Note that we already checked for equality of the type
459 qualifiers (just above). */
461 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
462 return 1;
464 #ifndef COMP_TYPE_ATTRIBUTES
465 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
466 #endif
468 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
469 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
470 return 0;
472 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
473 val = 0;
475 switch (TREE_CODE (t1))
477 case POINTER_TYPE:
478 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
479 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
480 break;
482 case FUNCTION_TYPE:
483 val = function_types_compatible_p (t1, t2);
484 break;
486 case ARRAY_TYPE:
488 tree d1 = TYPE_DOMAIN (t1);
489 tree d2 = TYPE_DOMAIN (t2);
490 val = 1;
492 /* Target types must match incl. qualifiers. */
493 if (TREE_TYPE (t1) != TREE_TYPE (t2)
494 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
495 return 0;
497 /* Sizes must match unless one is missing or variable. */
498 if (d1 == 0 || d2 == 0 || d1 == d2
499 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
500 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
501 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
502 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
503 break;
505 if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
506 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
507 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
508 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
509 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
510 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
511 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
512 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2)))))
513 val = 0;
514 break;
517 case RECORD_TYPE:
518 if (maybe_objc_comptypes (t1, t2, 0) == 1)
519 val = 1;
520 break;
522 default:
523 break;
525 return attrval == 2 && val == 1 ? 2 : val;
528 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
529 ignoring their qualifiers. */
531 static int
532 comp_target_types (ttl, ttr)
533 tree ttl, ttr;
535 int val;
537 /* Give maybe_objc_comptypes a crack at letting these types through. */
538 if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0)
539 return val;
541 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
542 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
544 if (val == 2 && pedantic)
545 pedwarn ("types are not quite compatible");
546 return val;
549 /* Subroutines of `comptypes'. */
551 /* Return 1 if two function types F1 and F2 are compatible.
552 If either type specifies no argument types,
553 the other must specify a fixed number of self-promoting arg types.
554 Otherwise, if one type specifies only the number of arguments,
555 the other must specify that number of self-promoting arg types.
556 Otherwise, the argument types must match. */
558 static int
559 function_types_compatible_p (f1, f2)
560 tree f1, f2;
562 tree args1, args2;
563 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
564 int val = 1;
565 int val1;
567 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
568 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
569 return 0;
571 args1 = TYPE_ARG_TYPES (f1);
572 args2 = TYPE_ARG_TYPES (f2);
574 /* An unspecified parmlist matches any specified parmlist
575 whose argument types don't need default promotions. */
577 if (args1 == 0)
579 if (!self_promoting_args_p (args2))
580 return 0;
581 /* If one of these types comes from a non-prototype fn definition,
582 compare that with the other type's arglist.
583 If they don't match, ask for a warning (but no error). */
584 if (TYPE_ACTUAL_ARG_TYPES (f1)
585 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
586 val = 2;
587 return val;
589 if (args2 == 0)
591 if (!self_promoting_args_p (args1))
592 return 0;
593 if (TYPE_ACTUAL_ARG_TYPES (f2)
594 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
595 val = 2;
596 return val;
599 /* Both types have argument lists: compare them and propagate results. */
600 val1 = type_lists_compatible_p (args1, args2);
601 return val1 != 1 ? val1 : val;
604 /* Check two lists of types for compatibility,
605 returning 0 for incompatible, 1 for compatible,
606 or 2 for compatible with warning. */
608 static int
609 type_lists_compatible_p (args1, args2)
610 tree args1, args2;
612 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
613 int val = 1;
614 int newval = 0;
616 while (1)
618 if (args1 == 0 && args2 == 0)
619 return val;
620 /* If one list is shorter than the other,
621 they fail to match. */
622 if (args1 == 0 || args2 == 0)
623 return 0;
624 /* A null pointer instead of a type
625 means there is supposed to be an argument
626 but nothing is specified about what type it has.
627 So match anything that self-promotes. */
628 if (TREE_VALUE (args1) == 0)
630 if (simple_type_promotes_to (TREE_VALUE (args2)) != NULL_TREE)
631 return 0;
633 else if (TREE_VALUE (args2) == 0)
635 if (simple_type_promotes_to (TREE_VALUE (args1)) != NULL_TREE)
636 return 0;
638 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
640 /* Allow wait (union {union wait *u; int *i} *)
641 and wait (union wait *) to be compatible. */
642 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
643 && (TYPE_NAME (TREE_VALUE (args1)) == 0
644 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
645 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
646 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
647 TYPE_SIZE (TREE_VALUE (args2))))
649 tree memb;
650 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
651 memb; memb = TREE_CHAIN (memb))
652 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
653 break;
654 if (memb == 0)
655 return 0;
657 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
658 && (TYPE_NAME (TREE_VALUE (args2)) == 0
659 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
660 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
661 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
662 TYPE_SIZE (TREE_VALUE (args1))))
664 tree memb;
665 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
666 memb; memb = TREE_CHAIN (memb))
667 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
668 break;
669 if (memb == 0)
670 return 0;
672 else
673 return 0;
676 /* comptypes said ok, but record if it said to warn. */
677 if (newval > val)
678 val = newval;
680 args1 = TREE_CHAIN (args1);
681 args2 = TREE_CHAIN (args2);
685 /* Compute the value of the `sizeof' operator. */
687 tree
688 c_sizeof (type)
689 tree type;
691 enum tree_code code = TREE_CODE (type);
692 tree t;
694 if (code == FUNCTION_TYPE)
696 if (pedantic || warn_pointer_arith)
697 pedwarn ("sizeof applied to a function type");
698 return size_int (1);
700 if (code == VOID_TYPE)
702 if (pedantic || warn_pointer_arith)
703 pedwarn ("sizeof applied to a void type");
704 return size_int (1);
706 if (code == ERROR_MARK)
707 return size_int (1);
708 if (TYPE_SIZE (type) == 0)
710 error ("sizeof applied to an incomplete type");
711 return size_int (0);
714 /* Convert in case a char is more than one unit. */
715 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
716 size_int (TYPE_PRECISION (char_type_node)));
717 t = convert (sizetype, t);
718 /* size_binop does not put the constant in range, so do it now. */
719 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
720 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
721 return t;
724 tree
725 c_sizeof_nowarn (type)
726 tree type;
728 enum tree_code code = TREE_CODE (type);
729 tree t;
731 if (code == FUNCTION_TYPE
732 || code == VOID_TYPE
733 || code == ERROR_MARK)
734 return size_int (1);
735 if (TYPE_SIZE (type) == 0)
736 return size_int (0);
738 /* Convert in case a char is more than one unit. */
739 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
740 size_int (TYPE_PRECISION (char_type_node)));
741 t = convert (sizetype, t);
742 force_fit_type (t, 0);
743 return t;
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);
753 tree t;
755 if (code == FUNCTION_TYPE)
756 return size_int (1);
757 if (code == VOID_TYPE)
758 return size_int (1);
759 if (code == ERROR_MARK)
760 return size_int (1);
761 if (TYPE_SIZE (type) == 0)
763 error ("arithmetic on pointer to an incomplete type");
764 return size_int (1);
767 /* Convert in case a char is more than one unit. */
768 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
769 size_int (BITS_PER_UNIT));
770 t = convert (sizetype, t);
771 force_fit_type (t, 0);
772 return t;
775 /* Implement the __alignof keyword: Return the minimum required
776 alignment of TYPE, measured in bytes. */
778 tree
779 c_alignof (type)
780 tree type;
782 enum tree_code code = TREE_CODE (type);
784 if (code == FUNCTION_TYPE)
785 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
787 if (code == VOID_TYPE || code == ERROR_MARK)
788 return size_int (1);
790 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
793 /* Implement the __alignof keyword: Return the minimum required
794 alignment of EXPR, measured in bytes. For VAR_DECL's and
795 FIELD_DECL's return DECL_ALIGN (which can be set from an
796 "aligned" __attribute__ specification). */
798 tree
799 c_alignof_expr (expr)
800 tree expr;
802 if (TREE_CODE (expr) == VAR_DECL)
803 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
805 if (TREE_CODE (expr) == COMPONENT_REF
806 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
808 error ("`__alignof' applied to a bit-field");
809 return size_int (1);
811 else if (TREE_CODE (expr) == COMPONENT_REF
812 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
813 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
815 if (TREE_CODE (expr) == INDIRECT_REF)
817 tree t = TREE_OPERAND (expr, 0);
818 tree best = t;
819 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
821 while (TREE_CODE (t) == NOP_EXPR
822 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
824 int thisalign;
826 t = TREE_OPERAND (t, 0);
827 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
828 if (thisalign > bestalign)
829 best = t, bestalign = thisalign;
831 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
833 else
834 return c_alignof (TREE_TYPE (expr));
837 /* Return either DECL or its known constant value (if it has one). */
839 static tree
840 decl_constant_value (decl)
841 tree decl;
843 if (/* Don't change a variable array bound or initial value to a constant
844 in a place where a variable is invalid. */
845 current_function_decl != 0
846 && ! pedantic
847 && ! TREE_THIS_VOLATILE (decl)
848 && TREE_READONLY (decl) && ! ITERATOR_P (decl)
849 && DECL_INITIAL (decl) != 0
850 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
851 /* This is invalid if initial value is not constant.
852 If it has either a function call, a memory reference,
853 or a variable, then re-evaluating it could give different results. */
854 && TREE_CONSTANT (DECL_INITIAL (decl))
855 /* Check for cases where this is sub-optimal, even though valid. */
856 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
857 && DECL_MODE (decl) != BLKmode)
858 return DECL_INITIAL (decl);
859 return decl;
862 /* Perform default promotions for C data used in expressions.
863 Arrays and functions are converted to pointers;
864 enumeral types or short or char, to int.
865 In addition, manifest constants symbols are replaced by their values. */
867 tree
868 default_conversion (exp)
869 tree exp;
871 register tree type = TREE_TYPE (exp);
872 register enum tree_code code = TREE_CODE (type);
874 /* Constants can be used directly unless they're not loadable. */
875 if (TREE_CODE (exp) == CONST_DECL)
876 exp = DECL_INITIAL (exp);
878 /* Replace a nonvolatile const static variable with its value unless
879 it is an array, in which case we must be sure that taking the
880 address of the array produces consistent results. */
881 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
883 exp = decl_constant_value (exp);
884 type = TREE_TYPE (exp);
887 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
888 an lvalue. */
889 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
890 to integer and cause infinite recursion. */
891 while (TREE_CODE (exp) == NON_LVALUE_EXPR
892 || (TREE_CODE (exp) == NOP_EXPR
893 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
894 exp = TREE_OPERAND (exp, 0);
896 /* Normally convert enums to int,
897 but convert wide enums to something wider. */
898 if (code == ENUMERAL_TYPE)
900 type = type_for_size (MAX (TYPE_PRECISION (type),
901 TYPE_PRECISION (integer_type_node)),
902 ((flag_traditional
903 || (TYPE_PRECISION (type)
904 >= TYPE_PRECISION (integer_type_node)))
905 && TREE_UNSIGNED (type)));
906 return convert (type, exp);
909 if (TREE_CODE (exp) == COMPONENT_REF
910 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
912 tree width = DECL_SIZE (TREE_OPERAND (exp, 1));
913 HOST_WIDE_INT low = TREE_INT_CST_LOW (width);
915 /* If it's thinner than an int, promote it like a
916 C_PROMOTING_INTEGER_TYPE_P, otherwise leave it alone. */
918 if (low < TYPE_PRECISION (integer_type_node))
920 if (flag_traditional && TREE_UNSIGNED (type))
921 return convert (unsigned_type_node, exp);
922 else
923 return convert (integer_type_node, exp);
927 if (C_PROMOTING_INTEGER_TYPE_P (type))
929 /* Traditionally, unsignedness is preserved in default promotions.
930 Also preserve unsignedness if not really getting any wider. */
931 if (TREE_UNSIGNED (type)
932 && (flag_traditional
933 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
934 return convert (unsigned_type_node, exp);
935 return convert (integer_type_node, exp);
937 if (flag_traditional && !flag_allow_single_precision
938 && TYPE_MAIN_VARIANT (type) == float_type_node)
939 return convert (double_type_node, exp);
940 if (code == VOID_TYPE)
942 error ("void value not ignored as it ought to be");
943 return error_mark_node;
945 if (code == FUNCTION_TYPE)
947 return build_unary_op (ADDR_EXPR, exp, 0);
949 if (code == ARRAY_TYPE)
951 register tree adr;
952 tree restype = TREE_TYPE (type);
953 tree ptrtype;
954 int constp = 0;
955 int volatilep = 0;
957 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
958 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
960 constp = TREE_READONLY (exp);
961 volatilep = TREE_THIS_VOLATILE (exp);
964 if (TYPE_QUALS (type) || constp || volatilep)
965 restype
966 = c_build_qualified_type (restype,
967 TYPE_QUALS (type)
968 | (constp * TYPE_QUAL_CONST)
969 | (volatilep * TYPE_QUAL_VOLATILE));
971 if (TREE_CODE (exp) == INDIRECT_REF)
972 return convert (TYPE_POINTER_TO (restype),
973 TREE_OPERAND (exp, 0));
975 if (TREE_CODE (exp) == COMPOUND_EXPR)
977 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
978 return build (COMPOUND_EXPR, TREE_TYPE (op1),
979 TREE_OPERAND (exp, 0), op1);
982 if (! lvalue_p (exp)
983 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
985 error ("invalid use of non-lvalue array");
986 return error_mark_node;
989 ptrtype = build_pointer_type (restype);
991 if (TREE_CODE (exp) == VAR_DECL)
993 /* ??? This is not really quite correct
994 in that the type of the operand of ADDR_EXPR
995 is not the target type of the type of the ADDR_EXPR itself.
996 Question is, can this lossage be avoided? */
997 adr = build1 (ADDR_EXPR, ptrtype, exp);
998 if (mark_addressable (exp) == 0)
999 return error_mark_node;
1000 TREE_CONSTANT (adr) = staticp (exp);
1001 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1002 return adr;
1004 /* This way is better for a COMPONENT_REF since it can
1005 simplify the offset for a component. */
1006 adr = build_unary_op (ADDR_EXPR, exp, 1);
1007 return convert (ptrtype, adr);
1009 return exp;
1012 /* Look up component name in the structure type definition.
1014 If this component name is found indirectly within an anonymous union,
1015 store in *INDIRECT the component which directly contains
1016 that anonymous union. Otherwise, set *INDIRECT to 0. */
1018 static tree
1019 lookup_field (type, component, indirect)
1020 tree type, component;
1021 tree *indirect;
1023 tree field;
1025 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1026 to the field elements. Use a binary search on this array to quickly
1027 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1028 will always be set for structures which have many elements. */
1030 if (TYPE_LANG_SPECIFIC (type))
1032 int bot, top, half;
1033 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1035 field = TYPE_FIELDS (type);
1036 bot = 0;
1037 top = TYPE_LANG_SPECIFIC (type)->len;
1038 while (top - bot > 1)
1040 half = (top - bot + 1) >> 1;
1041 field = field_array[bot+half];
1043 if (DECL_NAME (field) == NULL_TREE)
1045 /* Step through all anon unions in linear fashion. */
1046 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1048 tree anon = 0, junk;
1050 field = field_array[bot++];
1051 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1052 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1053 anon = lookup_field (TREE_TYPE (field), component, &junk);
1055 if (anon != NULL_TREE)
1057 *indirect = field;
1058 return anon;
1062 /* Entire record is only anon unions. */
1063 if (bot > top)
1064 return NULL_TREE;
1066 /* Restart the binary search, with new lower bound. */
1067 continue;
1070 if (DECL_NAME (field) == component)
1071 break;
1072 if (DECL_NAME (field) < component)
1073 bot += half;
1074 else
1075 top = bot + half;
1078 if (DECL_NAME (field_array[bot]) == component)
1079 field = field_array[bot];
1080 else if (DECL_NAME (field) != component)
1081 field = 0;
1083 else
1085 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1087 if (DECL_NAME (field) == NULL_TREE)
1089 tree junk;
1090 tree anon = 0;
1092 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1093 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1094 anon = lookup_field (TREE_TYPE (field), component, &junk);
1096 if (anon != NULL_TREE)
1098 *indirect = field;
1099 return anon;
1103 if (DECL_NAME (field) == component)
1104 break;
1108 *indirect = NULL_TREE;
1109 return field;
1112 /* Make an expression to refer to the COMPONENT field of
1113 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1115 tree
1116 build_component_ref (datum, component)
1117 tree datum, component;
1119 register tree type = TREE_TYPE (datum);
1120 register enum tree_code code = TREE_CODE (type);
1121 register tree field = NULL;
1122 register tree ref;
1124 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1125 unless we are not to support things not strictly ANSI. */
1126 switch (TREE_CODE (datum))
1128 case COMPOUND_EXPR:
1130 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1131 return build (COMPOUND_EXPR, TREE_TYPE (value),
1132 TREE_OPERAND (datum, 0), value);
1134 case COND_EXPR:
1135 return build_conditional_expr
1136 (TREE_OPERAND (datum, 0),
1137 build_component_ref (TREE_OPERAND (datum, 1), component),
1138 build_component_ref (TREE_OPERAND (datum, 2), component));
1140 default:
1141 break;
1144 /* See if there is a field or component with name COMPONENT. */
1146 if (code == RECORD_TYPE || code == UNION_TYPE)
1148 tree indirect = 0;
1150 if (TYPE_SIZE (type) == 0)
1152 incomplete_type_error (NULL_TREE, type);
1153 return error_mark_node;
1156 field = lookup_field (type, component, &indirect);
1158 if (!field)
1160 error (code == RECORD_TYPE
1161 ? "structure has no member named `%s'"
1162 : "union has no member named `%s'",
1163 IDENTIFIER_POINTER (component));
1164 return error_mark_node;
1166 if (TREE_TYPE (field) == error_mark_node)
1167 return error_mark_node;
1169 /* If FIELD was found buried within an anonymous union,
1170 make one COMPONENT_REF to get that anonymous union,
1171 then fall thru to make a second COMPONENT_REF to get FIELD. */
1172 if (indirect != 0)
1174 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1175 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1176 TREE_READONLY (ref) = 1;
1177 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1178 TREE_THIS_VOLATILE (ref) = 1;
1179 datum = ref;
1182 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1184 if (TREE_READONLY (datum) || TREE_READONLY (field))
1185 TREE_READONLY (ref) = 1;
1186 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1187 TREE_THIS_VOLATILE (ref) = 1;
1189 return ref;
1191 else if (code != ERROR_MARK)
1192 error ("request for member `%s' in something not a structure or union",
1193 IDENTIFIER_POINTER (component));
1195 return error_mark_node;
1198 /* Given an expression PTR for a pointer, return an expression
1199 for the value pointed to.
1200 ERRORSTRING is the name of the operator to appear in error messages. */
1202 tree
1203 build_indirect_ref (ptr, errorstring)
1204 tree ptr;
1205 const char *errorstring;
1207 register tree pointer = default_conversion (ptr);
1208 register tree type = TREE_TYPE (pointer);
1210 if (TREE_CODE (type) == POINTER_TYPE)
1212 if (TREE_CODE (pointer) == ADDR_EXPR
1213 && !flag_volatile
1214 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1215 == TREE_TYPE (type)))
1216 return TREE_OPERAND (pointer, 0);
1217 else
1219 tree t = TREE_TYPE (type);
1220 register tree ref = build1 (INDIRECT_REF,
1221 TYPE_MAIN_VARIANT (t), pointer);
1223 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1225 error ("dereferencing pointer to incomplete type");
1226 return error_mark_node;
1228 if (TREE_CODE (t) == VOID_TYPE && skip_evaluation == 0)
1229 warning ("dereferencing `void *' pointer");
1231 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1232 so that we get the proper error message if the result is used
1233 to assign to. Also, &* is supposed to be a no-op.
1234 And ANSI C seems to specify that the type of the result
1235 should be the const type. */
1236 /* A de-reference of a pointer to const is not a const. It is valid
1237 to change it via some other pointer. */
1238 TREE_READONLY (ref) = TYPE_READONLY (t);
1239 TREE_SIDE_EFFECTS (ref)
1240 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1241 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1242 return ref;
1245 else if (TREE_CODE (pointer) != ERROR_MARK)
1246 error ("invalid type argument of `%s'", errorstring);
1247 return error_mark_node;
1250 /* This handles expressions of the form "a[i]", which denotes
1251 an array reference.
1253 This is logically equivalent in C to *(a+i), but we may do it differently.
1254 If A is a variable or a member, we generate a primitive ARRAY_REF.
1255 This avoids forcing the array out of registers, and can work on
1256 arrays that are not lvalues (for example, members of structures returned
1257 by functions). */
1259 tree
1260 build_array_ref (array, index)
1261 tree array, index;
1263 if (index == 0)
1265 error ("subscript missing in array reference");
1266 return error_mark_node;
1269 if (TREE_TYPE (array) == error_mark_node
1270 || TREE_TYPE (index) == error_mark_node)
1271 return error_mark_node;
1273 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1274 && TREE_CODE (array) != INDIRECT_REF)
1276 tree rval, type;
1278 /* Subscripting with type char is likely to lose
1279 on a machine where chars are signed.
1280 So warn on any machine, but optionally.
1281 Don't warn for unsigned char since that type is safe.
1282 Don't warn for signed char because anyone who uses that
1283 must have done so deliberately. */
1284 if (warn_char_subscripts
1285 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1286 warning ("array subscript has type `char'");
1288 /* Apply default promotions *after* noticing character types. */
1289 index = default_conversion (index);
1291 /* Require integer *after* promotion, for sake of enums. */
1292 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1294 error ("array subscript is not an integer");
1295 return error_mark_node;
1298 /* An array that is indexed by a non-constant
1299 cannot be stored in a register; we must be able to do
1300 address arithmetic on its address.
1301 Likewise an array of elements of variable size. */
1302 if (TREE_CODE (index) != INTEGER_CST
1303 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1304 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1306 if (mark_addressable (array) == 0)
1307 return error_mark_node;
1309 /* An array that is indexed by a constant value which is not within
1310 the array bounds cannot be stored in a register either; because we
1311 would get a crash in store_bit_field/extract_bit_field when trying
1312 to access a non-existent part of the register. */
1313 if (TREE_CODE (index) == INTEGER_CST
1314 && TYPE_VALUES (TREE_TYPE (array))
1315 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1317 if (mark_addressable (array) == 0)
1318 return error_mark_node;
1321 if (pedantic && !lvalue_p (array))
1323 if (DECL_REGISTER (array))
1324 pedwarn ("ANSI C forbids subscripting `register' array");
1325 else
1326 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1329 if (pedantic)
1331 tree foo = array;
1332 while (TREE_CODE (foo) == COMPONENT_REF)
1333 foo = TREE_OPERAND (foo, 0);
1334 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1335 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1338 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1339 rval = build (ARRAY_REF, type, array, index);
1340 /* Array ref is const/volatile if the array elements are
1341 or if the array is. */
1342 TREE_READONLY (rval)
1343 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1344 | TREE_READONLY (array));
1345 TREE_SIDE_EFFECTS (rval)
1346 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1347 | TREE_SIDE_EFFECTS (array));
1348 TREE_THIS_VOLATILE (rval)
1349 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1350 /* This was added by rms on 16 Nov 91.
1351 It fixes vol struct foo *a; a->elts[1]
1352 in an inline function.
1353 Hope it doesn't break something else. */
1354 | TREE_THIS_VOLATILE (array));
1355 return require_complete_type (fold (rval));
1359 tree ar = default_conversion (array);
1360 tree ind = default_conversion (index);
1362 /* Do the same warning check as above, but only on the part that's
1363 syntactically the index and only if it is also semantically
1364 the index. */
1365 if (warn_char_subscripts
1366 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1367 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1368 warning ("subscript has type `char'");
1370 /* Put the integer in IND to simplify error checking. */
1371 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1373 tree temp = ar;
1374 ar = ind;
1375 ind = temp;
1378 if (ar == error_mark_node)
1379 return ar;
1381 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1382 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1384 error ("subscripted value is neither array nor pointer");
1385 return error_mark_node;
1387 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1389 error ("array subscript is not an integer");
1390 return error_mark_node;
1393 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1394 "array indexing");
1398 /* Build a function call to function FUNCTION with parameters PARAMS.
1399 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1400 TREE_VALUE of each node is a parameter-expression.
1401 FUNCTION's data type may be a function type or a pointer-to-function. */
1403 tree
1404 build_function_call (function, params)
1405 tree function, params;
1407 register tree fntype, fundecl = 0;
1408 register tree coerced_params;
1409 tree name = NULL_TREE, assembler_name = NULL_TREE;
1411 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1412 STRIP_TYPE_NOPS (function);
1414 /* Convert anything with function type to a pointer-to-function. */
1415 if (TREE_CODE (function) == FUNCTION_DECL)
1417 name = DECL_NAME (function);
1418 assembler_name = DECL_ASSEMBLER_NAME (function);
1420 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1421 (because calling an inline function does not mean the function
1422 needs to be separately compiled). */
1423 fntype = build_type_variant (TREE_TYPE (function),
1424 TREE_READONLY (function),
1425 TREE_THIS_VOLATILE (function));
1426 fundecl = function;
1427 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1429 else
1430 function = default_conversion (function);
1432 fntype = TREE_TYPE (function);
1434 if (TREE_CODE (fntype) == ERROR_MARK)
1435 return error_mark_node;
1437 if (!(TREE_CODE (fntype) == POINTER_TYPE
1438 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1440 error ("called object is not a function");
1441 return error_mark_node;
1444 /* fntype now gets the type of function pointed to. */
1445 fntype = TREE_TYPE (fntype);
1447 /* Convert the parameters to the types declared in the
1448 function prototype, or apply default promotions. */
1450 coerced_params
1451 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1453 /* Check for errors in format strings. */
1455 if (warn_format && (name || assembler_name))
1456 check_function_format (name, assembler_name, coerced_params);
1458 /* Recognize certain built-in functions so we can make tree-codes
1459 other than CALL_EXPR. We do this when it enables fold-const.c
1460 to do something useful. */
1462 if (TREE_CODE (function) == ADDR_EXPR
1463 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1464 && DECL_BUILT_IN (TREE_OPERAND (function, 0))
1465 && DECL_BUILT_IN_CLASS (TREE_OPERAND (function, 0)) == BUILT_IN_NORMAL)
1466 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1468 case BUILT_IN_ABS:
1469 case BUILT_IN_LABS:
1470 case BUILT_IN_FABS:
1471 if (coerced_params == 0)
1472 return integer_zero_node;
1473 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1474 default:
1475 break;
1479 register tree result
1480 = build (CALL_EXPR, TREE_TYPE (fntype),
1481 function, coerced_params, NULL_TREE);
1483 TREE_SIDE_EFFECTS (result) = 1;
1484 if (TREE_TYPE (result) == void_type_node)
1485 return result;
1486 return require_complete_type (result);
1490 /* Convert the argument expressions in the list VALUES
1491 to the types in the list TYPELIST. The result is a list of converted
1492 argument expressions.
1494 If TYPELIST is exhausted, or when an element has NULL as its type,
1495 perform the default conversions.
1497 PARMLIST is the chain of parm decls for the function being called.
1498 It may be 0, if that info is not available.
1499 It is used only for generating error messages.
1501 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1503 This is also where warnings about wrong number of args are generated.
1505 Both VALUES and the returned value are chains of TREE_LIST nodes
1506 with the elements of the list in the TREE_VALUE slots of those nodes. */
1508 static tree
1509 convert_arguments (typelist, values, name, fundecl)
1510 tree typelist, values, name, fundecl;
1512 register tree typetail, valtail;
1513 register tree result = NULL;
1514 int parmnum;
1516 /* Scan the given expressions and types, producing individual
1517 converted arguments and pushing them on RESULT in reverse order. */
1519 for (valtail = values, typetail = typelist, parmnum = 0;
1520 valtail;
1521 valtail = TREE_CHAIN (valtail), parmnum++)
1523 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1524 register tree val = TREE_VALUE (valtail);
1526 if (type == void_type_node)
1528 if (name)
1529 error ("too many arguments to function `%s'",
1530 IDENTIFIER_POINTER (name));
1531 else
1532 error ("too many arguments to function");
1533 break;
1536 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1537 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1538 to convert automatically to a pointer. */
1539 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1540 val = TREE_OPERAND (val, 0);
1542 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1543 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1544 val = default_conversion (val);
1546 val = require_complete_type (val);
1548 if (type != 0)
1550 /* Formal parm type is specified by a function prototype. */
1551 tree parmval;
1553 if (TYPE_SIZE (type) == 0)
1555 error ("type of formal parameter %d is incomplete", parmnum + 1);
1556 parmval = val;
1558 else
1560 /* Optionally warn about conversions that
1561 differ from the default conversions. */
1562 if (warn_conversion)
1564 int formal_prec = TYPE_PRECISION (type);
1566 if (INTEGRAL_TYPE_P (type)
1567 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1568 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1569 else if (TREE_CODE (type) == COMPLEX_TYPE
1570 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1571 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1572 else if (TREE_CODE (type) == REAL_TYPE
1573 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1574 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1575 else if (TREE_CODE (type) == REAL_TYPE
1576 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1577 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1578 /* ??? At some point, messages should be written about
1579 conversions between complex types, but that's too messy
1580 to do now. */
1581 else if (TREE_CODE (type) == REAL_TYPE
1582 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1584 /* Warn if any argument is passed as `float',
1585 since without a prototype it would be `double'. */
1586 if (formal_prec == TYPE_PRECISION (float_type_node))
1587 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1589 /* Detect integer changing in width or signedness. */
1590 else if (INTEGRAL_TYPE_P (type)
1591 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1593 tree would_have_been = default_conversion (val);
1594 tree type1 = TREE_TYPE (would_have_been);
1596 if (TREE_CODE (type) == ENUMERAL_TYPE
1597 && type == TREE_TYPE (val))
1598 /* No warning if function asks for enum
1599 and the actual arg is that enum type. */
1601 else if (formal_prec != TYPE_PRECISION (type1))
1602 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1603 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1605 /* Don't complain if the formal parameter type
1606 is an enum, because we can't tell now whether
1607 the value was an enum--even the same enum. */
1608 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1610 else if (TREE_CODE (val) == INTEGER_CST
1611 && int_fits_type_p (val, type))
1612 /* Change in signedness doesn't matter
1613 if a constant value is unaffected. */
1615 /* Likewise for a constant in a NOP_EXPR. */
1616 else if (TREE_CODE (val) == NOP_EXPR
1617 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1618 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1620 #if 0 /* We never get such tree structure here. */
1621 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1622 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1623 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1624 /* Change in signedness doesn't matter
1625 if an enum value is unaffected. */
1627 #endif
1628 /* If the value is extended from a narrower
1629 unsigned type, it doesn't matter whether we
1630 pass it as signed or unsigned; the value
1631 certainly is the same either way. */
1632 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1633 && TREE_UNSIGNED (TREE_TYPE (val)))
1635 else if (TREE_UNSIGNED (type))
1636 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1637 else
1638 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1642 parmval = convert_for_assignment (type, val,
1643 (char *) 0, /* arg passing */
1644 fundecl, name, parmnum + 1);
1646 if (PROMOTE_PROTOTYPES
1647 && (TREE_CODE (type) == INTEGER_TYPE
1648 || TREE_CODE (type) == ENUMERAL_TYPE)
1649 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1650 parmval = default_conversion (parmval);
1652 result = tree_cons (NULL_TREE, parmval, result);
1654 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1655 && (TYPE_PRECISION (TREE_TYPE (val))
1656 < TYPE_PRECISION (double_type_node)))
1657 /* Convert `float' to `double'. */
1658 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1659 else
1660 /* Convert `short' and `char' to full-size `int'. */
1661 result = tree_cons (NULL_TREE, default_conversion (val), result);
1663 if (typetail)
1664 typetail = TREE_CHAIN (typetail);
1667 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1669 if (name)
1670 error ("too few arguments to function `%s'",
1671 IDENTIFIER_POINTER (name));
1672 else
1673 error ("too few arguments to function");
1676 return nreverse (result);
1679 /* This is the entry point used by the parser
1680 for binary operators in the input.
1681 In addition to constructing the expression,
1682 we check for operands that were written with other binary operators
1683 in a way that is likely to confuse the user. */
1685 tree
1686 parser_build_binary_op (code, arg1, arg2)
1687 enum tree_code code;
1688 tree arg1, arg2;
1690 tree result = build_binary_op (code, arg1, arg2, 1);
1692 char class;
1693 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1694 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1695 enum tree_code code1 = ERROR_MARK;
1696 enum tree_code code2 = ERROR_MARK;
1698 if (class1 == 'e' || class1 == '1'
1699 || class1 == '2' || class1 == '<')
1700 code1 = C_EXP_ORIGINAL_CODE (arg1);
1701 if (class2 == 'e' || class2 == '1'
1702 || class2 == '2' || class2 == '<')
1703 code2 = C_EXP_ORIGINAL_CODE (arg2);
1705 /* Check for cases such as x+y<<z which users are likely
1706 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1707 is cleared to prevent these warnings. */
1708 if (warn_parentheses)
1710 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1712 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1713 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1714 warning ("suggest parentheses around + or - inside shift");
1717 if (code == TRUTH_ORIF_EXPR)
1719 if (code1 == TRUTH_ANDIF_EXPR
1720 || code2 == TRUTH_ANDIF_EXPR)
1721 warning ("suggest parentheses around && within ||");
1724 if (code == BIT_IOR_EXPR)
1726 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1727 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1728 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1729 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1730 warning ("suggest parentheses around arithmetic in operand of |");
1731 /* Check cases like x|y==z */
1732 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1733 warning ("suggest parentheses around comparison in operand of |");
1736 if (code == BIT_XOR_EXPR)
1738 if (code1 == BIT_AND_EXPR
1739 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1740 || code2 == BIT_AND_EXPR
1741 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1742 warning ("suggest parentheses around arithmetic in operand of ^");
1743 /* Check cases like x^y==z */
1744 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1745 warning ("suggest parentheses around comparison in operand of ^");
1748 if (code == BIT_AND_EXPR)
1750 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1751 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1752 warning ("suggest parentheses around + or - in operand of &");
1753 /* Check cases like x&y==z */
1754 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1755 warning ("suggest parentheses around comparison in operand of &");
1759 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1760 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1761 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1762 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1764 unsigned_conversion_warning (result, arg1);
1765 unsigned_conversion_warning (result, arg2);
1766 overflow_warning (result);
1768 class = TREE_CODE_CLASS (TREE_CODE (result));
1770 /* Record the code that was specified in the source,
1771 for the sake of warnings about confusing nesting. */
1772 if (class == 'e' || class == '1'
1773 || class == '2' || class == '<')
1774 C_SET_EXP_ORIGINAL_CODE (result, code);
1775 else
1777 int flag = TREE_CONSTANT (result);
1778 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1779 so that convert_for_assignment wouldn't strip it.
1780 That way, we got warnings for things like p = (1 - 1).
1781 But it turns out we should not get those warnings. */
1782 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1783 C_SET_EXP_ORIGINAL_CODE (result, code);
1784 TREE_CONSTANT (result) = flag;
1787 return result;
1790 /* Build a binary-operation expression without default conversions.
1791 CODE is the kind of expression to build.
1792 This function differs from `build' in several ways:
1793 the data type of the result is computed and recorded in it,
1794 warnings are generated if arg data types are invalid,
1795 special handling for addition and subtraction of pointers is known,
1796 and some optimization is done (operations on narrow ints
1797 are done in the narrower type when that gives the same result).
1798 Constant folding is also done before the result is returned.
1800 Note that the operands will never have enumeral types, or function
1801 or array types, because either they will have the default conversions
1802 performed or they have both just been converted to some other type in which
1803 the arithmetic is to be done. */
1805 tree
1806 build_binary_op (code, orig_op0, orig_op1, convert_p)
1807 enum tree_code code;
1808 tree orig_op0, orig_op1;
1809 int convert_p;
1811 tree type0, type1;
1812 register enum tree_code code0, code1;
1813 tree op0, op1;
1815 /* Expression code to give to the expression when it is built.
1816 Normally this is CODE, which is what the caller asked for,
1817 but in some special cases we change it. */
1818 register enum tree_code resultcode = code;
1820 /* Data type in which the computation is to be performed.
1821 In the simplest cases this is the common type of the arguments. */
1822 register tree result_type = NULL;
1824 /* Nonzero means operands have already been type-converted
1825 in whatever way is necessary.
1826 Zero means they need to be converted to RESULT_TYPE. */
1827 int converted = 0;
1829 /* Nonzero means create the expression with this type, rather than
1830 RESULT_TYPE. */
1831 tree build_type = 0;
1833 /* Nonzero means after finally constructing the expression
1834 convert it to this type. */
1835 tree final_type = 0;
1837 /* Nonzero if this is an operation like MIN or MAX which can
1838 safely be computed in short if both args are promoted shorts.
1839 Also implies COMMON.
1840 -1 indicates a bitwise operation; this makes a difference
1841 in the exact conditions for when it is safe to do the operation
1842 in a narrower mode. */
1843 int shorten = 0;
1845 /* Nonzero if this is a comparison operation;
1846 if both args are promoted shorts, compare the original shorts.
1847 Also implies COMMON. */
1848 int short_compare = 0;
1850 /* Nonzero if this is a right-shift operation, which can be computed on the
1851 original short and then promoted if the operand is a promoted short. */
1852 int short_shift = 0;
1854 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1855 int common = 0;
1857 if (convert_p)
1859 op0 = default_conversion (orig_op0);
1860 op1 = default_conversion (orig_op1);
1862 else
1864 op0 = orig_op0;
1865 op1 = orig_op1;
1868 type0 = TREE_TYPE (op0);
1869 type1 = TREE_TYPE (op1);
1871 /* The expression codes of the data types of the arguments tell us
1872 whether the arguments are integers, floating, pointers, etc. */
1873 code0 = TREE_CODE (type0);
1874 code1 = TREE_CODE (type1);
1876 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1877 STRIP_TYPE_NOPS (op0);
1878 STRIP_TYPE_NOPS (op1);
1880 /* If an error was already reported for one of the arguments,
1881 avoid reporting another error. */
1883 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1884 return error_mark_node;
1886 switch (code)
1888 case PLUS_EXPR:
1889 /* Handle the pointer + int case. */
1890 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1891 return pointer_int_sum (PLUS_EXPR, op0, op1);
1892 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1893 return pointer_int_sum (PLUS_EXPR, op1, op0);
1894 else
1895 common = 1;
1896 break;
1898 case MINUS_EXPR:
1899 /* Subtraction of two similar pointers.
1900 We must subtract them as integers, then divide by object size. */
1901 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1902 && comp_target_types (type0, type1))
1903 return pointer_diff (op0, op1);
1904 /* Handle pointer minus int. Just like pointer plus int. */
1905 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1906 return pointer_int_sum (MINUS_EXPR, op0, op1);
1907 else
1908 common = 1;
1909 break;
1911 case MULT_EXPR:
1912 common = 1;
1913 break;
1915 case TRUNC_DIV_EXPR:
1916 case CEIL_DIV_EXPR:
1917 case FLOOR_DIV_EXPR:
1918 case ROUND_DIV_EXPR:
1919 case EXACT_DIV_EXPR:
1920 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1921 || code0 == COMPLEX_TYPE)
1922 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1923 || code1 == COMPLEX_TYPE))
1925 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
1926 resultcode = RDIV_EXPR;
1927 else
1929 /* Although it would be tempting to shorten always here, that
1930 loses on some targets, since the modulo instruction is
1931 undefined if the quotient can't be represented in the
1932 computation mode. We shorten only if unsigned or if
1933 dividing by something we know != -1. */
1934 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
1935 || (TREE_CODE (op1) == INTEGER_CST
1936 && (TREE_INT_CST_LOW (op1) != -1
1937 || TREE_INT_CST_HIGH (op1) != -1)));
1939 common = 1;
1941 break;
1943 case BIT_AND_EXPR:
1944 case BIT_ANDTC_EXPR:
1945 case BIT_IOR_EXPR:
1946 case BIT_XOR_EXPR:
1947 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1948 shorten = -1;
1949 /* If one operand is a constant, and the other is a short type
1950 that has been converted to an int,
1951 really do the work in the short type and then convert the
1952 result to int. If we are lucky, the constant will be 0 or 1
1953 in the short type, making the entire operation go away. */
1954 if (TREE_CODE (op0) == INTEGER_CST
1955 && TREE_CODE (op1) == NOP_EXPR
1956 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
1957 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
1959 final_type = result_type;
1960 op1 = TREE_OPERAND (op1, 0);
1961 result_type = TREE_TYPE (op1);
1963 if (TREE_CODE (op1) == INTEGER_CST
1964 && TREE_CODE (op0) == NOP_EXPR
1965 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
1966 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
1968 final_type = result_type;
1969 op0 = TREE_OPERAND (op0, 0);
1970 result_type = TREE_TYPE (op0);
1972 break;
1974 case TRUNC_MOD_EXPR:
1975 case FLOOR_MOD_EXPR:
1976 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1978 /* Although it would be tempting to shorten always here, that loses
1979 on some targets, since the modulo instruction is undefined if the
1980 quotient can't be represented in the computation mode. We shorten
1981 only if unsigned or if dividing by something we know != -1. */
1982 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
1983 || (TREE_CODE (op1) == INTEGER_CST
1984 && (TREE_INT_CST_LOW (op1) != -1
1985 || TREE_INT_CST_HIGH (op1) != -1)));
1986 common = 1;
1988 break;
1990 case TRUTH_ANDIF_EXPR:
1991 case TRUTH_ORIF_EXPR:
1992 case TRUTH_AND_EXPR:
1993 case TRUTH_OR_EXPR:
1994 case TRUTH_XOR_EXPR:
1995 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
1996 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
1997 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
1998 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2000 /* Result of these operations is always an int,
2001 but that does not mean the operands should be
2002 converted to ints! */
2003 result_type = integer_type_node;
2004 op0 = truthvalue_conversion (op0);
2005 op1 = truthvalue_conversion (op1);
2006 converted = 1;
2008 break;
2010 /* Shift operations: result has same type as first operand;
2011 always convert second operand to int.
2012 Also set SHORT_SHIFT if shifting rightward. */
2014 case RSHIFT_EXPR:
2015 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2017 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2019 if (tree_int_cst_sgn (op1) < 0)
2020 warning ("right shift count is negative");
2021 else
2023 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2024 short_shift = 1;
2025 if (TREE_INT_CST_HIGH (op1) != 0
2026 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2027 >= TYPE_PRECISION (type0)))
2028 warning ("right shift count >= width of type");
2031 /* Use the type of the value to be shifted.
2032 This is what most traditional C compilers do. */
2033 result_type = type0;
2034 /* Unless traditional, convert the shift-count to an integer,
2035 regardless of size of value being shifted. */
2036 if (! flag_traditional)
2038 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2039 op1 = convert (integer_type_node, op1);
2040 /* Avoid converting op1 to result_type later. */
2041 converted = 1;
2044 break;
2046 case LSHIFT_EXPR:
2047 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2049 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2051 if (tree_int_cst_sgn (op1) < 0)
2052 warning ("left shift count is negative");
2053 else if (TREE_INT_CST_HIGH (op1) != 0
2054 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2055 >= TYPE_PRECISION (type0)))
2056 warning ("left shift count >= width of type");
2058 /* Use the type of the value to be shifted.
2059 This is what most traditional C compilers do. */
2060 result_type = type0;
2061 /* Unless traditional, convert the shift-count to an integer,
2062 regardless of size of value being shifted. */
2063 if (! flag_traditional)
2065 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2066 op1 = convert (integer_type_node, op1);
2067 /* Avoid converting op1 to result_type later. */
2068 converted = 1;
2071 break;
2073 case RROTATE_EXPR:
2074 case LROTATE_EXPR:
2075 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2077 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2079 if (tree_int_cst_sgn (op1) < 0)
2080 warning ("shift count is negative");
2081 else if (TREE_INT_CST_HIGH (op1) != 0
2082 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2083 >= TYPE_PRECISION (type0)))
2084 warning ("shift count >= width of type");
2086 /* Use the type of the value to be shifted.
2087 This is what most traditional C compilers do. */
2088 result_type = type0;
2089 /* Unless traditional, convert the shift-count to an integer,
2090 regardless of size of value being shifted. */
2091 if (! flag_traditional)
2093 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2094 op1 = convert (integer_type_node, op1);
2095 /* Avoid converting op1 to result_type later. */
2096 converted = 1;
2099 break;
2101 case EQ_EXPR:
2102 case NE_EXPR:
2103 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2104 warning ("comparing floating point with == or != is unsafe");
2105 /* Result of comparison is always int,
2106 but don't convert the args to int! */
2107 build_type = integer_type_node;
2108 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2109 || code0 == COMPLEX_TYPE)
2110 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2111 || code1 == COMPLEX_TYPE))
2112 short_compare = 1;
2113 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2115 register tree tt0 = TREE_TYPE (type0);
2116 register tree tt1 = TREE_TYPE (type1);
2117 /* Anything compares with void *. void * compares with anything.
2118 Otherwise, the targets must be compatible
2119 and both must be object or both incomplete. */
2120 if (comp_target_types (type0, type1))
2121 result_type = common_type (type0, type1);
2122 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2124 /* op0 != orig_op0 detects the case of something
2125 whose value is 0 but which isn't a valid null ptr const. */
2126 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2127 && TREE_CODE (tt1) == FUNCTION_TYPE)
2128 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2130 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2132 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2133 && TREE_CODE (tt0) == FUNCTION_TYPE)
2134 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2136 else
2137 pedwarn ("comparison of distinct pointer types lacks a cast");
2139 if (result_type == NULL_TREE)
2140 result_type = ptr_type_node;
2142 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2143 && integer_zerop (op1))
2144 result_type = type0;
2145 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2146 && integer_zerop (op0))
2147 result_type = type1;
2148 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2150 result_type = type0;
2151 if (! flag_traditional)
2152 pedwarn ("comparison between pointer and integer");
2154 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2156 result_type = type1;
2157 if (! flag_traditional)
2158 pedwarn ("comparison between pointer and integer");
2160 break;
2162 case MAX_EXPR:
2163 case MIN_EXPR:
2164 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2165 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2166 shorten = 1;
2167 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2169 if (comp_target_types (type0, type1))
2171 result_type = common_type (type0, type1);
2172 if (pedantic
2173 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2174 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2176 else
2178 result_type = ptr_type_node;
2179 pedwarn ("comparison of distinct pointer types lacks a cast");
2182 break;
2184 case LE_EXPR:
2185 case GE_EXPR:
2186 case LT_EXPR:
2187 case GT_EXPR:
2188 build_type = integer_type_node;
2189 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2190 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2191 short_compare = 1;
2192 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2194 if (comp_target_types (type0, type1))
2196 result_type = common_type (type0, type1);
2197 if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2198 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2199 pedwarn ("comparison of complete and incomplete pointers");
2200 else if (pedantic
2201 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2202 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2204 else
2206 result_type = ptr_type_node;
2207 pedwarn ("comparison of distinct pointer types lacks a cast");
2210 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2211 && integer_zerop (op1))
2213 result_type = type0;
2214 if (pedantic || extra_warnings)
2215 pedwarn ("ordered comparison of pointer with integer zero");
2217 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2218 && integer_zerop (op0))
2220 result_type = type1;
2221 if (pedantic)
2222 pedwarn ("ordered comparison of pointer with integer zero");
2224 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2226 result_type = type0;
2227 if (! flag_traditional)
2228 pedwarn ("comparison between pointer and integer");
2230 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2232 result_type = type1;
2233 if (! flag_traditional)
2234 pedwarn ("comparison between pointer and integer");
2236 break;
2238 default:
2239 break;
2242 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2244 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2246 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2248 if (shorten || common || short_compare)
2249 result_type = common_type (type0, type1);
2251 /* For certain operations (which identify themselves by shorten != 0)
2252 if both args were extended from the same smaller type,
2253 do the arithmetic in that type and then extend.
2255 shorten !=0 and !=1 indicates a bitwise operation.
2256 For them, this optimization is safe only if
2257 both args are zero-extended or both are sign-extended.
2258 Otherwise, we might change the result.
2259 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2260 but calculated in (unsigned short) it would be (unsigned short)-1. */
2262 if (shorten && none_complex)
2264 int unsigned0, unsigned1;
2265 tree arg0 = get_narrower (op0, &unsigned0);
2266 tree arg1 = get_narrower (op1, &unsigned1);
2267 /* UNS is 1 if the operation to be done is an unsigned one. */
2268 int uns = TREE_UNSIGNED (result_type);
2269 tree type;
2271 final_type = result_type;
2273 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2274 but it *requires* conversion to FINAL_TYPE. */
2276 if ((TYPE_PRECISION (TREE_TYPE (op0))
2277 == TYPE_PRECISION (TREE_TYPE (arg0)))
2278 && TREE_TYPE (op0) != final_type)
2279 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2280 if ((TYPE_PRECISION (TREE_TYPE (op1))
2281 == TYPE_PRECISION (TREE_TYPE (arg1)))
2282 && TREE_TYPE (op1) != final_type)
2283 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2285 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2287 /* For bitwise operations, signedness of nominal type
2288 does not matter. Consider only how operands were extended. */
2289 if (shorten == -1)
2290 uns = unsigned0;
2292 /* Note that in all three cases below we refrain from optimizing
2293 an unsigned operation on sign-extended args.
2294 That would not be valid. */
2296 /* Both args variable: if both extended in same way
2297 from same width, do it in that width.
2298 Do it unsigned if args were zero-extended. */
2299 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2300 < TYPE_PRECISION (result_type))
2301 && (TYPE_PRECISION (TREE_TYPE (arg1))
2302 == TYPE_PRECISION (TREE_TYPE (arg0)))
2303 && unsigned0 == unsigned1
2304 && (unsigned0 || !uns))
2305 result_type
2306 = signed_or_unsigned_type (unsigned0,
2307 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2308 else if (TREE_CODE (arg0) == INTEGER_CST
2309 && (unsigned1 || !uns)
2310 && (TYPE_PRECISION (TREE_TYPE (arg1))
2311 < TYPE_PRECISION (result_type))
2312 && (type = signed_or_unsigned_type (unsigned1,
2313 TREE_TYPE (arg1)),
2314 int_fits_type_p (arg0, type)))
2315 result_type = type;
2316 else if (TREE_CODE (arg1) == INTEGER_CST
2317 && (unsigned0 || !uns)
2318 && (TYPE_PRECISION (TREE_TYPE (arg0))
2319 < TYPE_PRECISION (result_type))
2320 && (type = signed_or_unsigned_type (unsigned0,
2321 TREE_TYPE (arg0)),
2322 int_fits_type_p (arg1, type)))
2323 result_type = type;
2326 /* Shifts can be shortened if shifting right. */
2328 if (short_shift)
2330 int unsigned_arg;
2331 tree arg0 = get_narrower (op0, &unsigned_arg);
2333 final_type = result_type;
2335 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2336 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2338 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2339 /* We can shorten only if the shift count is less than the
2340 number of bits in the smaller type size. */
2341 && TREE_INT_CST_HIGH (op1) == 0
2342 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
2343 /* If arg is sign-extended and then unsigned-shifted,
2344 we can simulate this with a signed shift in arg's type
2345 only if the extended result is at least twice as wide
2346 as the arg. Otherwise, the shift could use up all the
2347 ones made by sign-extension and bring in zeros.
2348 We can't optimize that case at all, but in most machines
2349 it never happens because available widths are 2**N. */
2350 && (!TREE_UNSIGNED (final_type)
2351 || unsigned_arg
2352 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2354 /* Do an unsigned shift if the operand was zero-extended. */
2355 result_type
2356 = signed_or_unsigned_type (unsigned_arg,
2357 TREE_TYPE (arg0));
2358 /* Convert value-to-be-shifted to that type. */
2359 if (TREE_TYPE (op0) != result_type)
2360 op0 = convert (result_type, op0);
2361 converted = 1;
2365 /* Comparison operations are shortened too but differently.
2366 They identify themselves by setting short_compare = 1. */
2368 if (short_compare)
2370 /* Don't write &op0, etc., because that would prevent op0
2371 from being kept in a register.
2372 Instead, make copies of the our local variables and
2373 pass the copies by reference, then copy them back afterward. */
2374 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2375 enum tree_code xresultcode = resultcode;
2376 tree val
2377 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2378 if (val != 0)
2379 return val;
2380 op0 = xop0, op1 = xop1;
2381 converted = 1;
2382 resultcode = xresultcode;
2384 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2385 && skip_evaluation == 0)
2387 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2388 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2390 int unsignedp0, unsignedp1;
2391 tree primop0 = get_narrower (op0, &unsignedp0);
2392 tree primop1 = get_narrower (op1, &unsignedp1);
2394 /* Avoid spurious warnings for comparison with enumerators. */
2396 xop0 = orig_op0;
2397 xop1 = orig_op1;
2398 STRIP_TYPE_NOPS (xop0);
2399 STRIP_TYPE_NOPS (xop1);
2401 /* Give warnings for comparisons between signed and unsigned
2402 quantities that may fail. */
2403 /* Do the checking based on the original operand trees, so that
2404 casts will be considered, but default promotions won't be. */
2406 /* Do not warn if the comparison is being done in a signed type,
2407 since the signed type will only be chosen if it can represent
2408 all the values of the unsigned type. */
2409 if (! TREE_UNSIGNED (result_type))
2410 /* OK */;
2411 /* Do not warn if both operands are unsigned. */
2412 else if (op0_signed == op1_signed)
2413 /* OK */;
2414 /* Do not warn if the signed quantity is an unsuffixed
2415 integer literal (or some static constant expression
2416 involving such literals) and it is non-negative. */
2417 else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST
2418 && tree_int_cst_sgn (xop0) >= 0)
2419 || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
2420 && tree_int_cst_sgn (xop1) >= 0))
2421 /* OK */;
2422 /* Do not warn if the comparison is an equality operation,
2423 the unsigned quantity is an integral constant and it does
2424 not use the most significant bit of result_type. */
2425 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
2426 && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST
2427 && int_fits_type_p (xop1, signed_type (result_type)))
2428 || (op1_signed && TREE_CODE (xop0) == INTEGER_CST
2429 && int_fits_type_p (xop0, signed_type (result_type)))))
2430 /* OK */;
2431 else
2432 warning ("comparison between signed and unsigned");
2434 /* Warn if two unsigned values are being compared in a size
2435 larger than their original size, and one (and only one) is the
2436 result of a `~' operator. This comparison will always fail.
2438 Also warn if one operand is a constant, and the constant
2439 does not have all bits set that are set in the ~ operand
2440 when it is extended. */
2442 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2443 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2445 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2446 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2447 &unsignedp0);
2448 else
2449 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2450 &unsignedp1);
2452 if (TREE_CODE (primop0) == INTEGER_CST
2453 || TREE_CODE (primop1) == INTEGER_CST)
2455 tree primop;
2456 long constant, mask;
2457 int unsignedp, bits;
2459 if (TREE_CODE (primop0) == INTEGER_CST)
2461 primop = primop1;
2462 unsignedp = unsignedp1;
2463 constant = TREE_INT_CST_LOW (primop0);
2465 else
2467 primop = primop0;
2468 unsignedp = unsignedp0;
2469 constant = TREE_INT_CST_LOW (primop1);
2472 bits = TYPE_PRECISION (TREE_TYPE (primop));
2473 if (bits < TYPE_PRECISION (result_type)
2474 && bits < HOST_BITS_PER_LONG && unsignedp)
2476 mask = (~0L) << bits;
2477 if ((mask & constant) != mask)
2478 warning ("comparison of promoted ~unsigned with constant");
2481 else if (unsignedp0 && unsignedp1
2482 && (TYPE_PRECISION (TREE_TYPE (primop0))
2483 < TYPE_PRECISION (result_type))
2484 && (TYPE_PRECISION (TREE_TYPE (primop1))
2485 < TYPE_PRECISION (result_type)))
2486 warning ("comparison of promoted ~unsigned with unsigned");
2492 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2493 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2494 Then the expression will be built.
2495 It will be given type FINAL_TYPE if that is nonzero;
2496 otherwise, it will be given type RESULT_TYPE. */
2498 if (!result_type)
2500 binary_op_error (code);
2501 return error_mark_node;
2504 if (! converted)
2506 if (TREE_TYPE (op0) != result_type)
2507 op0 = convert (result_type, op0);
2508 if (TREE_TYPE (op1) != result_type)
2509 op1 = convert (result_type, op1);
2512 if (build_type == NULL_TREE)
2513 build_type = result_type;
2516 register tree result = build (resultcode, build_type, op0, op1);
2517 register tree folded;
2519 folded = fold (result);
2520 if (folded == result)
2521 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2522 if (final_type != 0)
2523 return convert (final_type, folded);
2524 return folded;
2528 /* Return a tree for the sum or difference (RESULTCODE says which)
2529 of pointer PTROP and integer INTOP. */
2531 static tree
2532 pointer_int_sum (resultcode, ptrop, intop)
2533 enum tree_code resultcode;
2534 register tree ptrop, intop;
2536 tree size_exp;
2538 register tree result;
2539 register tree folded;
2541 /* The result is a pointer of the same type that is being added. */
2543 register tree result_type = TREE_TYPE (ptrop);
2545 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2547 if (pedantic || warn_pointer_arith)
2548 pedwarn ("pointer of type `void *' used in arithmetic");
2549 size_exp = integer_one_node;
2551 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2553 if (pedantic || warn_pointer_arith)
2554 pedwarn ("pointer to a function used in arithmetic");
2555 size_exp = integer_one_node;
2557 else
2558 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2560 /* If what we are about to multiply by the size of the elements
2561 contains a constant term, apply distributive law
2562 and multiply that constant term separately.
2563 This helps produce common subexpressions. */
2565 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2566 && ! TREE_CONSTANT (intop)
2567 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2568 && TREE_CONSTANT (size_exp)
2569 /* If the constant comes from pointer subtraction,
2570 skip this optimization--it would cause an error. */
2571 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2572 /* If the constant is unsigned, and smaller than the pointer size,
2573 then we must skip this optimization. This is because it could cause
2574 an overflow error if the constant is negative but INTOP is not. */
2575 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2576 || (TYPE_PRECISION (TREE_TYPE (intop))
2577 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2579 enum tree_code subcode = resultcode;
2580 tree int_type = TREE_TYPE (intop);
2581 if (TREE_CODE (intop) == MINUS_EXPR)
2582 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2583 /* Convert both subexpression types to the type of intop,
2584 because weird cases involving pointer arithmetic
2585 can result in a sum or difference with different type args. */
2586 ptrop = build_binary_op (subcode, ptrop,
2587 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2588 intop = convert (int_type, TREE_OPERAND (intop, 0));
2591 /* Convert the integer argument to a type the same size as sizetype
2592 so the multiply won't overflow spuriously. */
2594 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2595 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2596 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2597 TREE_UNSIGNED (sizetype)), intop);
2599 /* Replace the integer argument with a suitable product by the object size.
2600 Do this multiplication as signed, then convert to the appropriate
2601 pointer type (actually unsigned integral). */
2603 intop = convert (result_type,
2604 build_binary_op (MULT_EXPR, intop,
2605 convert (TREE_TYPE (intop), size_exp), 1));
2607 /* Create the sum or difference. */
2609 result = build (resultcode, result_type, ptrop, intop);
2611 folded = fold (result);
2612 if (folded == result)
2613 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2614 return folded;
2617 /* Return a tree for the difference of pointers OP0 and OP1.
2618 The resulting tree has type int. */
2620 static tree
2621 pointer_diff (op0, op1)
2622 register tree op0, op1;
2624 register tree result, folded;
2625 tree restype = ptrdiff_type_node;
2627 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2629 if (pedantic || warn_pointer_arith)
2631 if (TREE_CODE (target_type) == VOID_TYPE)
2632 pedwarn ("pointer of type `void *' used in subtraction");
2633 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2634 pedwarn ("pointer to a function used in subtraction");
2637 /* First do the subtraction as integers;
2638 then drop through to build the divide operator.
2639 Do not do default conversions on the minus operator
2640 in case restype is a short type. */
2642 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2643 convert (restype, op1), 0);
2644 /* This generates an error if op1 is pointer to incomplete type. */
2645 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2646 error ("arithmetic on pointer to an incomplete type");
2648 /* This generates an error if op0 is pointer to incomplete type. */
2649 op1 = c_size_in_bytes (target_type);
2651 /* Divide by the size, in easiest possible way. */
2653 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2655 folded = fold (result);
2656 if (folded == result)
2657 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2658 return folded;
2661 /* Construct and perhaps optimize a tree representation
2662 for a unary operation. CODE, a tree_code, specifies the operation
2663 and XARG is the operand. NOCONVERT nonzero suppresses
2664 the default promotions (such as from short to int). */
2666 tree
2667 build_unary_op (code, xarg, noconvert)
2668 enum tree_code code;
2669 tree xarg;
2670 int noconvert;
2672 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2673 register tree arg = xarg;
2674 register tree argtype = 0;
2675 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2676 tree val;
2678 if (typecode == ERROR_MARK)
2679 return error_mark_node;
2680 if (typecode == ENUMERAL_TYPE)
2681 typecode = INTEGER_TYPE;
2683 switch (code)
2685 case CONVERT_EXPR:
2686 /* This is used for unary plus, because a CONVERT_EXPR
2687 is enough to prevent anybody from looking inside for
2688 associativity, but won't generate any code. */
2689 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2690 || typecode == COMPLEX_TYPE))
2692 error ("wrong type argument to unary plus");
2693 return error_mark_node;
2695 else if (!noconvert)
2696 arg = default_conversion (arg);
2697 break;
2699 case NEGATE_EXPR:
2700 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2701 || typecode == COMPLEX_TYPE))
2703 error ("wrong type argument to unary minus");
2704 return error_mark_node;
2706 else if (!noconvert)
2707 arg = default_conversion (arg);
2708 break;
2710 case BIT_NOT_EXPR:
2711 if (typecode == COMPLEX_TYPE)
2713 code = CONJ_EXPR;
2714 if (!noconvert)
2715 arg = default_conversion (arg);
2717 else if (typecode != INTEGER_TYPE)
2719 error ("wrong type argument to bit-complement");
2720 return error_mark_node;
2722 else if (!noconvert)
2723 arg = default_conversion (arg);
2724 break;
2726 case ABS_EXPR:
2727 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2728 || typecode == COMPLEX_TYPE))
2730 error ("wrong type argument to abs");
2731 return error_mark_node;
2733 else if (!noconvert)
2734 arg = default_conversion (arg);
2735 break;
2737 case CONJ_EXPR:
2738 /* Conjugating a real value is a no-op, but allow it anyway. */
2739 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2740 || typecode == COMPLEX_TYPE))
2742 error ("wrong type argument to conjugation");
2743 return error_mark_node;
2745 else if (!noconvert)
2746 arg = default_conversion (arg);
2747 break;
2749 case TRUTH_NOT_EXPR:
2750 if (typecode != INTEGER_TYPE
2751 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2752 && typecode != COMPLEX_TYPE
2753 /* These will convert to a pointer. */
2754 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2756 error ("wrong type argument to unary exclamation mark");
2757 return error_mark_node;
2759 arg = truthvalue_conversion (arg);
2760 return invert_truthvalue (arg);
2762 case NOP_EXPR:
2763 break;
2765 case REALPART_EXPR:
2766 if (TREE_CODE (arg) == COMPLEX_CST)
2767 return TREE_REALPART (arg);
2768 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2769 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2770 else
2771 return arg;
2773 case IMAGPART_EXPR:
2774 if (TREE_CODE (arg) == COMPLEX_CST)
2775 return TREE_IMAGPART (arg);
2776 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2777 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2778 else
2779 return convert (TREE_TYPE (arg), integer_zero_node);
2781 case PREINCREMENT_EXPR:
2782 case POSTINCREMENT_EXPR:
2783 case PREDECREMENT_EXPR:
2784 case POSTDECREMENT_EXPR:
2785 /* Handle complex lvalues (when permitted)
2786 by reduction to simpler cases. */
2788 val = unary_complex_lvalue (code, arg);
2789 if (val != 0)
2790 return val;
2792 /* Increment or decrement the real part of the value,
2793 and don't change the imaginary part. */
2794 if (typecode == COMPLEX_TYPE)
2796 tree real, imag;
2798 arg = stabilize_reference (arg);
2799 real = build_unary_op (REALPART_EXPR, arg, 1);
2800 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2801 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2802 build_unary_op (code, real, 1), imag);
2805 /* Report invalid types. */
2807 if (typecode != POINTER_TYPE
2808 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2810 error (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2811 ? "wrong type argument to increment"
2812 : "wrong type argument to decrement");
2813 return error_mark_node;
2817 register tree inc;
2818 tree result_type = TREE_TYPE (arg);
2820 arg = get_unwidened (arg, 0);
2821 argtype = TREE_TYPE (arg);
2823 /* Compute the increment. */
2825 if (typecode == POINTER_TYPE)
2827 /* If pointer target is an undefined struct,
2828 we just cannot know how to do the arithmetic. */
2829 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2830 error (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2831 ? "increment of pointer to unknown structure"
2832 : "decrement of pointer to unknown structure");
2833 else if ((pedantic || warn_pointer_arith)
2834 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2835 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2836 pedwarn (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2837 ? "wrong type argument to increment"
2838 : "wrong type argument to decrement");
2839 inc = c_size_in_bytes (TREE_TYPE (result_type));
2841 else
2842 inc = integer_one_node;
2844 inc = convert (argtype, inc);
2846 /* Handle incrementing a cast-expression. */
2848 while (1)
2849 switch (TREE_CODE (arg))
2851 case NOP_EXPR:
2852 case CONVERT_EXPR:
2853 case FLOAT_EXPR:
2854 case FIX_TRUNC_EXPR:
2855 case FIX_FLOOR_EXPR:
2856 case FIX_ROUND_EXPR:
2857 case FIX_CEIL_EXPR:
2858 pedantic_lvalue_warning (CONVERT_EXPR);
2859 /* If the real type has the same machine representation
2860 as the type it is cast to, we can make better output
2861 by adding directly to the inside of the cast. */
2862 if ((TREE_CODE (TREE_TYPE (arg))
2863 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2864 && (TYPE_MODE (TREE_TYPE (arg))
2865 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2866 arg = TREE_OPERAND (arg, 0);
2867 else
2869 tree incremented, modify, value;
2870 arg = stabilize_reference (arg);
2871 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2872 value = arg;
2873 else
2874 value = save_expr (arg);
2875 incremented = build (((code == PREINCREMENT_EXPR
2876 || code == POSTINCREMENT_EXPR)
2877 ? PLUS_EXPR : MINUS_EXPR),
2878 argtype, value, inc);
2879 TREE_SIDE_EFFECTS (incremented) = 1;
2880 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2881 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2882 TREE_USED (value) = 1;
2883 return value;
2885 break;
2887 default:
2888 goto give_up;
2890 give_up:
2892 /* Complain about anything else that is not a true lvalue. */
2893 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2894 || code == POSTINCREMENT_EXPR)
2895 ? "invalid lvalue in increment"
2896 : "invalid lvalue in decrement")))
2897 return error_mark_node;
2899 /* Report a read-only lvalue. */
2900 if (TREE_READONLY (arg))
2901 readonly_warning (arg,
2902 ((code == PREINCREMENT_EXPR
2903 || code == POSTINCREMENT_EXPR)
2904 ? "increment" : "decrement"));
2906 val = build (code, TREE_TYPE (arg), arg, inc);
2907 TREE_SIDE_EFFECTS (val) = 1;
2908 val = convert (result_type, val);
2909 if (TREE_CODE (val) != code)
2910 TREE_NO_UNUSED_WARNING (val) = 1;
2911 return val;
2914 case ADDR_EXPR:
2915 /* Note that this operation never does default_conversion
2916 regardless of NOCONVERT. */
2918 /* Let &* cancel out to simplify resulting code. */
2919 if (TREE_CODE (arg) == INDIRECT_REF)
2921 /* Don't let this be an lvalue. */
2922 if (lvalue_p (TREE_OPERAND (arg, 0)))
2923 return non_lvalue (TREE_OPERAND (arg, 0));
2924 return TREE_OPERAND (arg, 0);
2927 /* For &x[y], return x+y */
2928 if (TREE_CODE (arg) == ARRAY_REF)
2930 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
2931 return error_mark_node;
2932 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2933 TREE_OPERAND (arg, 1), 1);
2936 /* Handle complex lvalues (when permitted)
2937 by reduction to simpler cases. */
2938 val = unary_complex_lvalue (code, arg);
2939 if (val != 0)
2940 return val;
2942 #if 0 /* Turned off because inconsistent;
2943 float f; *&(int)f = 3.4 stores in int format
2944 whereas (int)f = 3.4 stores in float format. */
2945 /* Address of a cast is just a cast of the address
2946 of the operand of the cast. */
2947 switch (TREE_CODE (arg))
2949 case NOP_EXPR:
2950 case CONVERT_EXPR:
2951 case FLOAT_EXPR:
2952 case FIX_TRUNC_EXPR:
2953 case FIX_FLOOR_EXPR:
2954 case FIX_ROUND_EXPR:
2955 case FIX_CEIL_EXPR:
2956 if (pedantic)
2957 pedwarn ("ANSI C forbids the address of a cast expression");
2958 return convert (build_pointer_type (TREE_TYPE (arg)),
2959 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
2960 0));
2962 #endif
2964 /* Allow the address of a constructor if all the elements
2965 are constant. */
2966 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
2968 /* Anything not already handled and not a true memory reference
2969 is an error. */
2970 else if (typecode != FUNCTION_TYPE
2971 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2972 return error_mark_node;
2974 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2975 argtype = TREE_TYPE (arg);
2976 /* If the lvalue is const or volatile, merge that into the type
2977 to which the address will point. Note that you can't get a
2978 restricted pointer by taking the address of something, so we
2979 only have to deal with `const' and `volatile' here. */
2980 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
2981 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2983 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
2984 argtype = c_build_type_variant (argtype,
2985 TREE_READONLY (arg),
2986 TREE_THIS_VOLATILE (arg));
2989 argtype = build_pointer_type (argtype);
2991 if (mark_addressable (arg) == 0)
2992 return error_mark_node;
2995 tree addr;
2997 if (TREE_CODE (arg) == COMPONENT_REF)
2999 tree field = TREE_OPERAND (arg, 1);
3001 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3003 if (DECL_C_BIT_FIELD (field))
3005 error ("attempt to take address of bit-field structure member `%s'",
3006 IDENTIFIER_POINTER (DECL_NAME (field)));
3007 return error_mark_node;
3010 addr = convert (argtype, addr);
3012 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3014 tree offset
3015 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3016 size_int (BITS_PER_UNIT));
3017 int flag = TREE_CONSTANT (addr);
3018 addr = fold (build (PLUS_EXPR, argtype,
3019 addr, convert (argtype, offset)));
3020 TREE_CONSTANT (addr) = flag;
3023 else
3024 addr = build1 (code, argtype, arg);
3026 /* Address of a static or external variable or
3027 file-scope function counts as a constant. */
3028 if (staticp (arg)
3029 && ! (TREE_CODE (arg) == FUNCTION_DECL
3030 && DECL_CONTEXT (arg) != 0))
3031 TREE_CONSTANT (addr) = 1;
3032 return addr;
3035 default:
3036 break;
3039 if (argtype == 0)
3040 argtype = TREE_TYPE (arg);
3041 return fold (build1 (code, argtype, arg));
3044 #if 0
3045 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3046 convert ARG with the same conversions in the same order
3047 and return the result. */
3049 static tree
3050 convert_sequence (conversions, arg)
3051 tree conversions;
3052 tree arg;
3054 switch (TREE_CODE (conversions))
3056 case NOP_EXPR:
3057 case CONVERT_EXPR:
3058 case FLOAT_EXPR:
3059 case FIX_TRUNC_EXPR:
3060 case FIX_FLOOR_EXPR:
3061 case FIX_ROUND_EXPR:
3062 case FIX_CEIL_EXPR:
3063 return convert (TREE_TYPE (conversions),
3064 convert_sequence (TREE_OPERAND (conversions, 0),
3065 arg));
3067 default:
3068 return arg;
3071 #endif /* 0 */
3073 /* Return nonzero if REF is an lvalue valid for this language.
3074 Lvalues can be assigned, unless their type has TYPE_READONLY.
3075 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3078 lvalue_p (ref)
3079 tree ref;
3081 register enum tree_code code = TREE_CODE (ref);
3083 switch (code)
3085 case REALPART_EXPR:
3086 case IMAGPART_EXPR:
3087 case COMPONENT_REF:
3088 return lvalue_p (TREE_OPERAND (ref, 0));
3090 case STRING_CST:
3091 return 1;
3093 case INDIRECT_REF:
3094 case ARRAY_REF:
3095 case VAR_DECL:
3096 case PARM_DECL:
3097 case RESULT_DECL:
3098 case ERROR_MARK:
3099 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3100 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3102 case BIND_EXPR:
3103 case RTL_EXPR:
3104 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3106 default:
3107 return 0;
3111 /* Return nonzero if REF is an lvalue valid for this language;
3112 otherwise, print an error message and return zero. */
3115 lvalue_or_else (ref, msgid)
3116 tree ref;
3117 const char *msgid;
3119 int win = lvalue_p (ref);
3121 if (! win)
3122 error (msgid);
3124 return win;
3127 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3128 for certain kinds of expressions which are not really lvalues
3129 but which we can accept as lvalues.
3131 If ARG is not a kind of expression we can handle, return zero. */
3133 static tree
3134 unary_complex_lvalue (code, arg)
3135 enum tree_code code;
3136 tree arg;
3138 /* Handle (a, b) used as an "lvalue". */
3139 if (TREE_CODE (arg) == COMPOUND_EXPR)
3141 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3143 /* If this returns a function type, it isn't really being used as
3144 an lvalue, so don't issue a warning about it. */
3145 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3146 pedantic_lvalue_warning (COMPOUND_EXPR);
3148 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3149 TREE_OPERAND (arg, 0), real_result);
3152 /* Handle (a ? b : c) used as an "lvalue". */
3153 if (TREE_CODE (arg) == COND_EXPR)
3155 pedantic_lvalue_warning (COND_EXPR);
3156 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3157 pedantic_lvalue_warning (COMPOUND_EXPR);
3159 return (build_conditional_expr
3160 (TREE_OPERAND (arg, 0),
3161 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3162 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3165 return 0;
3168 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3169 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3171 static void
3172 pedantic_lvalue_warning (code)
3173 enum tree_code code;
3175 if (pedantic)
3176 pedwarn (code == COND_EXPR
3177 ? "ANSI C forbids use of conditional expressions as lvalues"
3178 : code == COMPOUND_EXPR
3179 ? "ANSI C forbids use of compound expressions as lvalues"
3180 : "ANSI C forbids use of cast expressions as lvalues");
3183 /* Warn about storing in something that is `const'. */
3185 void
3186 readonly_warning (arg, msgid)
3187 tree arg;
3188 const char *msgid;
3190 /* Forbid assignments to iterators. */
3191 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3192 pedwarn ("%s of iterator `%s'", _(msgid),
3193 IDENTIFIER_POINTER (DECL_NAME (arg)));
3195 if (TREE_CODE (arg) == COMPONENT_REF)
3197 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3198 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3199 else
3200 pedwarn ("%s of read-only member `%s'", _(msgid),
3201 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3203 else if (TREE_CODE (arg) == VAR_DECL)
3204 pedwarn ("%s of read-only variable `%s'", _(msgid),
3205 IDENTIFIER_POINTER (DECL_NAME (arg)));
3206 else
3207 pedwarn ("%s of read-only location", _(msgid));
3210 /* Mark EXP saying that we need to be able to take the
3211 address of it; it should not be allocated in a register.
3212 Value is 1 if successful. */
3215 mark_addressable (exp)
3216 tree exp;
3218 register tree x = exp;
3219 while (1)
3220 switch (TREE_CODE (x))
3222 case COMPONENT_REF:
3223 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3225 error ("cannot take address of bitfield `%s'",
3226 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3227 return 0;
3230 /* ... fall through ... */
3232 case ADDR_EXPR:
3233 case ARRAY_REF:
3234 case REALPART_EXPR:
3235 case IMAGPART_EXPR:
3236 x = TREE_OPERAND (x, 0);
3237 break;
3239 case CONSTRUCTOR:
3240 TREE_ADDRESSABLE (x) = 1;
3241 return 1;
3243 case VAR_DECL:
3244 case CONST_DECL:
3245 case PARM_DECL:
3246 case RESULT_DECL:
3247 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3248 && DECL_NONLOCAL (x))
3250 if (TREE_PUBLIC (x))
3252 error ("global register variable `%s' used in nested function",
3253 IDENTIFIER_POINTER (DECL_NAME (x)));
3254 return 0;
3256 pedwarn ("register variable `%s' used in nested function",
3257 IDENTIFIER_POINTER (DECL_NAME (x)));
3259 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3261 if (TREE_PUBLIC (x))
3263 error ("address of global register variable `%s' requested",
3264 IDENTIFIER_POINTER (DECL_NAME (x)));
3265 return 0;
3268 /* If we are making this addressable due to its having
3269 volatile components, give a different error message. Also
3270 handle the case of an unnamed parameter by not trying
3271 to give the name. */
3273 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3275 error ("cannot put object with volatile field into register");
3276 return 0;
3279 pedwarn ("address of register variable `%s' requested",
3280 IDENTIFIER_POINTER (DECL_NAME (x)));
3282 put_var_into_stack (x);
3284 /* drops in */
3285 case FUNCTION_DECL:
3286 TREE_ADDRESSABLE (x) = 1;
3287 #if 0 /* poplevel deals with this now. */
3288 if (DECL_CONTEXT (x) == 0)
3289 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3290 #endif
3292 default:
3293 return 1;
3297 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3299 tree
3300 build_conditional_expr (ifexp, op1, op2)
3301 tree ifexp, op1, op2;
3303 register tree type1;
3304 register tree type2;
3305 register enum tree_code code1;
3306 register enum tree_code code2;
3307 register tree result_type = NULL;
3308 tree orig_op1 = op1, orig_op2 = op2;
3310 ifexp = truthvalue_conversion (default_conversion (ifexp));
3312 #if 0 /* Produces wrong result if within sizeof. */
3313 /* Don't promote the operands separately if they promote
3314 the same way. Return the unpromoted type and let the combined
3315 value get promoted if necessary. */
3317 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3318 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3319 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3320 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3322 if (TREE_CODE (ifexp) == INTEGER_CST)
3323 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3325 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3327 #endif
3329 /* Promote both alternatives. */
3331 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3332 op1 = default_conversion (op1);
3333 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3334 op2 = default_conversion (op2);
3336 if (TREE_CODE (ifexp) == ERROR_MARK
3337 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3338 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3339 return error_mark_node;
3341 type1 = TREE_TYPE (op1);
3342 code1 = TREE_CODE (type1);
3343 type2 = TREE_TYPE (op2);
3344 code2 = TREE_CODE (type2);
3346 /* Quickly detect the usual case where op1 and op2 have the same type
3347 after promotion. */
3348 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3350 if (type1 == type2)
3351 result_type = type1;
3352 else
3353 result_type = TYPE_MAIN_VARIANT (type1);
3355 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3356 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3358 result_type = common_type (type1, type2);
3360 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3362 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3363 pedwarn ("ANSI C forbids conditional expr with only one void side");
3364 result_type = void_type_node;
3366 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3368 if (comp_target_types (type1, type2))
3369 result_type = common_type (type1, type2);
3370 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3371 && TREE_CODE (orig_op1) != NOP_EXPR)
3372 result_type = qualify_type (type2, type1);
3373 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3374 && TREE_CODE (orig_op2) != NOP_EXPR)
3375 result_type = qualify_type (type1, type2);
3376 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3378 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3379 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3380 result_type = qualify_type (type1, type2);
3382 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3384 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3385 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3386 result_type = qualify_type (type2, type1);
3388 else
3390 pedwarn ("pointer type mismatch in conditional expression");
3391 result_type = build_pointer_type (void_type_node);
3394 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3396 if (! integer_zerop (op2))
3397 pedwarn ("pointer/integer type mismatch in conditional expression");
3398 else
3400 op2 = null_pointer_node;
3401 #if 0 /* The spec seems to say this is permitted. */
3402 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3403 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3404 #endif
3406 result_type = type1;
3408 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3410 if (!integer_zerop (op1))
3411 pedwarn ("pointer/integer type mismatch in conditional expression");
3412 else
3414 op1 = null_pointer_node;
3415 #if 0 /* The spec seems to say this is permitted. */
3416 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3417 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3418 #endif
3420 result_type = type2;
3423 if (!result_type)
3425 if (flag_cond_mismatch)
3426 result_type = void_type_node;
3427 else
3429 error ("type mismatch in conditional expression");
3430 return error_mark_node;
3434 /* Merge const and volatile flags of the incoming types. */
3435 result_type
3436 = build_type_variant (result_type,
3437 TREE_READONLY (op1) || TREE_READONLY (op2),
3438 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3440 if (result_type != TREE_TYPE (op1))
3441 op1 = convert_and_check (result_type, op1);
3442 if (result_type != TREE_TYPE (op2))
3443 op2 = convert_and_check (result_type, op2);
3445 #if 0
3446 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3448 result_type = TREE_TYPE (op1);
3449 if (TREE_CONSTANT (ifexp))
3450 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3452 if (TYPE_MODE (result_type) == BLKmode)
3454 register tree tempvar
3455 = build_decl (VAR_DECL, NULL_TREE, result_type);
3456 register tree xop1 = build_modify_expr (tempvar, op1);
3457 register tree xop2 = build_modify_expr (tempvar, op2);
3458 register tree result = fold (build (COND_EXPR, result_type,
3459 ifexp, xop1, xop2));
3461 layout_decl (tempvar, TYPE_ALIGN (result_type));
3462 /* No way to handle variable-sized objects here.
3463 I fear that the entire handling of BLKmode conditional exprs
3464 needs to be redone. */
3465 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3466 abort ();
3467 DECL_RTL (tempvar)
3468 = assign_stack_local (DECL_MODE (tempvar),
3469 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3470 + BITS_PER_UNIT - 1)
3471 / BITS_PER_UNIT,
3474 TREE_SIDE_EFFECTS (result)
3475 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3476 | TREE_SIDE_EFFECTS (op2);
3477 return build (COMPOUND_EXPR, result_type, result, tempvar);
3480 #endif /* 0 */
3482 if (TREE_CODE (ifexp) == INTEGER_CST)
3483 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3485 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3488 /* Given a list of expressions, return a compound expression
3489 that performs them all and returns the value of the last of them. */
3491 tree
3492 build_compound_expr (list)
3493 tree list;
3495 return internal_build_compound_expr (list, TRUE);
3498 static tree
3499 internal_build_compound_expr (list, first_p)
3500 tree list;
3501 int first_p;
3503 register tree rest;
3505 if (TREE_CHAIN (list) == 0)
3507 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3508 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3510 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3511 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3512 list = TREE_OPERAND (list, 0);
3513 #endif
3515 /* Don't let (0, 0) be null pointer constant. */
3516 if (!first_p && integer_zerop (TREE_VALUE (list)))
3517 return non_lvalue (TREE_VALUE (list));
3518 return TREE_VALUE (list);
3521 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3523 /* Convert arrays to pointers when there really is a comma operator. */
3524 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3525 TREE_VALUE (TREE_CHAIN (list))
3526 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3529 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3531 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3533 /* The left-hand operand of a comma expression is like an expression
3534 statement: with -W or -Wunused, we should warn if it doesn't have
3535 any side-effects, unless it was explicitly cast to (void). */
3536 if ((extra_warnings || warn_unused)
3537 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3538 && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
3539 warning ("left-hand operand of comma expression has no effect");
3541 /* When pedantic, a compound expression can be neither an lvalue
3542 nor an integer constant expression. */
3543 if (! pedantic)
3544 return rest;
3547 /* With -Wunused, we should also warn if the left-hand operand does have
3548 side-effects, but computes a value which is not used. For example, in
3549 `foo() + bar(), baz()' the result of the `+' operator is not used,
3550 so we should issue a warning. */
3551 else if (warn_unused)
3552 warn_if_unused_value (TREE_VALUE (list));
3554 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3557 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3559 tree
3560 build_c_cast (type, expr)
3561 register tree type;
3562 tree expr;
3564 register tree value = expr;
3566 if (type == error_mark_node || expr == error_mark_node)
3567 return error_mark_node;
3568 type = TYPE_MAIN_VARIANT (type);
3570 #if 0
3571 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3572 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3573 value = TREE_OPERAND (value, 0);
3574 #endif
3576 if (TREE_CODE (type) == ARRAY_TYPE)
3578 error ("cast specifies array type");
3579 return error_mark_node;
3582 if (TREE_CODE (type) == FUNCTION_TYPE)
3584 error ("cast specifies function type");
3585 return error_mark_node;
3588 if (type == TREE_TYPE (value))
3590 if (pedantic)
3592 if (TREE_CODE (type) == RECORD_TYPE
3593 || TREE_CODE (type) == UNION_TYPE)
3594 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3597 else if (TREE_CODE (type) == UNION_TYPE)
3599 tree field;
3600 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3601 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3602 value = default_conversion (value);
3604 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3605 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3606 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3607 break;
3609 if (field)
3611 const char *name;
3612 tree t;
3614 if (pedantic)
3615 pedwarn ("ANSI C forbids casts to union type");
3616 if (TYPE_NAME (type) != 0)
3618 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3619 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3620 else
3621 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3623 else
3624 name = "";
3625 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3626 build_tree_list (field, value)),
3627 0, 0);
3628 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3629 return t;
3631 error ("cast to union type from type not present in union");
3632 return error_mark_node;
3634 else
3636 tree otype, ovalue;
3638 /* If casting to void, avoid the error that would come
3639 from default_conversion in the case of a non-lvalue array. */
3640 if (type == void_type_node)
3641 return build1 (CONVERT_EXPR, type, value);
3643 /* Convert functions and arrays to pointers,
3644 but don't convert any other types. */
3645 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3646 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3647 value = default_conversion (value);
3648 otype = TREE_TYPE (value);
3650 /* Optionally warn about potentially worrisome casts. */
3652 if (warn_cast_qual
3653 && TREE_CODE (type) == POINTER_TYPE
3654 && TREE_CODE (otype) == POINTER_TYPE)
3656 /* Go to the innermost object being pointed to. */
3657 tree in_type = type;
3658 tree in_otype = otype;
3660 while (TREE_CODE (in_type) == POINTER_TYPE)
3661 in_type = TREE_TYPE (in_type);
3662 while (TREE_CODE (in_otype) == POINTER_TYPE)
3663 in_otype = TREE_TYPE (in_otype);
3665 if (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type))
3666 /* There are qualifiers present in IN_OTYPE that are not
3667 present in IN_TYPE. */
3668 pedwarn ("cast discards qualifiers from pointer target type");
3671 /* Warn about possible alignment problems. */
3672 if (STRICT_ALIGNMENT && warn_cast_align
3673 && TREE_CODE (type) == POINTER_TYPE
3674 && TREE_CODE (otype) == POINTER_TYPE
3675 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3676 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3677 /* Don't warn about opaque types, where the actual alignment
3678 restriction is unknown. */
3679 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3680 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3681 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3682 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3683 warning ("cast increases required alignment of target type");
3685 if (TREE_CODE (type) == INTEGER_TYPE
3686 && TREE_CODE (otype) == POINTER_TYPE
3687 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3688 && !TREE_CONSTANT (value))
3689 warning ("cast from pointer to integer of different size");
3691 if (warn_bad_function_cast
3692 && TREE_CODE (value) == CALL_EXPR
3693 && TREE_CODE (type) != TREE_CODE (otype))
3694 warning ("cast does not match function type");
3696 if (TREE_CODE (type) == POINTER_TYPE
3697 && TREE_CODE (otype) == INTEGER_TYPE
3698 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3699 #if 0
3700 /* Don't warn about converting 0 to pointer,
3701 provided the 0 was explicit--not cast or made by folding. */
3702 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3703 #endif
3704 /* Don't warn about converting any constant. */
3705 && !TREE_CONSTANT (value))
3706 warning ("cast to pointer from integer of different size");
3708 ovalue = value;
3709 value = convert (type, value);
3711 /* Ignore any integer overflow caused by the cast. */
3712 if (TREE_CODE (value) == INTEGER_CST)
3714 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3715 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3719 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3720 if (pedantic && TREE_CODE (value) == INTEGER_CST
3721 && TREE_CODE (expr) == INTEGER_CST
3722 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3723 value = non_lvalue (value);
3725 /* If pedantic, don't let a cast be an lvalue. */
3726 if (value == expr && pedantic)
3727 value = non_lvalue (value);
3729 return value;
3732 /* Build an assignment expression of lvalue LHS from value RHS.
3733 MODIFYCODE is the code for a binary operator that we use
3734 to combine the old value of LHS with RHS to get the new value.
3735 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3737 tree
3738 build_modify_expr (lhs, modifycode, rhs)
3739 tree lhs, rhs;
3740 enum tree_code modifycode;
3742 register tree result;
3743 tree newrhs;
3744 tree lhstype = TREE_TYPE (lhs);
3745 tree olhstype = lhstype;
3747 /* Types that aren't fully specified cannot be used in assignments. */
3748 lhs = require_complete_type (lhs);
3750 /* Avoid duplicate error messages from operands that had errors. */
3751 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3752 return error_mark_node;
3754 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3755 /* Do not use STRIP_NOPS here. We do not want an enumerator
3756 whose value is 0 to count as a null pointer constant. */
3757 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3758 rhs = TREE_OPERAND (rhs, 0);
3760 newrhs = rhs;
3762 /* Handle control structure constructs used as "lvalues". */
3764 switch (TREE_CODE (lhs))
3766 /* Handle (a, b) used as an "lvalue". */
3767 case COMPOUND_EXPR:
3768 pedantic_lvalue_warning (COMPOUND_EXPR);
3769 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3770 if (TREE_CODE (newrhs) == ERROR_MARK)
3771 return error_mark_node;
3772 return build (COMPOUND_EXPR, lhstype,
3773 TREE_OPERAND (lhs, 0), newrhs);
3775 /* Handle (a ? b : c) used as an "lvalue". */
3776 case COND_EXPR:
3777 pedantic_lvalue_warning (COND_EXPR);
3778 rhs = save_expr (rhs);
3780 /* Produce (a ? (b = rhs) : (c = rhs))
3781 except that the RHS goes through a save-expr
3782 so the code to compute it is only emitted once. */
3783 tree cond
3784 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3785 build_modify_expr (TREE_OPERAND (lhs, 1),
3786 modifycode, rhs),
3787 build_modify_expr (TREE_OPERAND (lhs, 2),
3788 modifycode, rhs));
3789 if (TREE_CODE (cond) == ERROR_MARK)
3790 return cond;
3791 /* Make sure the code to compute the rhs comes out
3792 before the split. */
3793 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3794 /* But cast it to void to avoid an "unused" error. */
3795 convert (void_type_node, rhs), cond);
3797 default:
3798 break;
3801 /* If a binary op has been requested, combine the old LHS value with the RHS
3802 producing the value we should actually store into the LHS. */
3804 if (modifycode != NOP_EXPR)
3806 lhs = stabilize_reference (lhs);
3807 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3810 /* Handle a cast used as an "lvalue".
3811 We have already performed any binary operator using the value as cast.
3812 Now convert the result to the cast type of the lhs,
3813 and then true type of the lhs and store it there;
3814 then convert result back to the cast type to be the value
3815 of the assignment. */
3817 switch (TREE_CODE (lhs))
3819 case NOP_EXPR:
3820 case CONVERT_EXPR:
3821 case FLOAT_EXPR:
3822 case FIX_TRUNC_EXPR:
3823 case FIX_FLOOR_EXPR:
3824 case FIX_ROUND_EXPR:
3825 case FIX_CEIL_EXPR:
3826 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3827 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3828 newrhs = default_conversion (newrhs);
3830 tree inner_lhs = TREE_OPERAND (lhs, 0);
3831 tree result;
3832 result = build_modify_expr (inner_lhs, NOP_EXPR,
3833 convert (TREE_TYPE (inner_lhs),
3834 convert (lhstype, newrhs)));
3835 if (TREE_CODE (result) == ERROR_MARK)
3836 return result;
3837 pedantic_lvalue_warning (CONVERT_EXPR);
3838 return convert (TREE_TYPE (lhs), result);
3841 default:
3842 break;
3845 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3846 Reject anything strange now. */
3848 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3849 return error_mark_node;
3851 /* Warn about storing in something that is `const'. */
3853 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3854 || ((TREE_CODE (lhstype) == RECORD_TYPE
3855 || TREE_CODE (lhstype) == UNION_TYPE)
3856 && C_TYPE_FIELDS_READONLY (lhstype)))
3857 readonly_warning (lhs, "assignment");
3859 /* If storing into a structure or union member,
3860 it has probably been given type `int'.
3861 Compute the type that would go with
3862 the actual amount of storage the member occupies. */
3864 if (TREE_CODE (lhs) == COMPONENT_REF
3865 && (TREE_CODE (lhstype) == INTEGER_TYPE
3866 || TREE_CODE (lhstype) == REAL_TYPE
3867 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3868 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3870 /* If storing in a field that is in actuality a short or narrower than one,
3871 we must store in the field in its actual type. */
3873 if (lhstype != TREE_TYPE (lhs))
3875 lhs = copy_node (lhs);
3876 TREE_TYPE (lhs) = lhstype;
3879 /* Convert new value to destination type. */
3881 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3882 NULL_TREE, NULL_TREE, 0);
3883 if (TREE_CODE (newrhs) == ERROR_MARK)
3884 return error_mark_node;
3886 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3887 TREE_SIDE_EFFECTS (result) = 1;
3889 /* If we got the LHS in a different type for storing in,
3890 convert the result back to the nominal type of LHS
3891 so that the value we return always has the same type
3892 as the LHS argument. */
3894 if (olhstype == TREE_TYPE (result))
3895 return result;
3896 return convert_for_assignment (olhstype, result, _("assignment"),
3897 NULL_TREE, NULL_TREE, 0);
3900 /* Convert value RHS to type TYPE as preparation for an assignment
3901 to an lvalue of type TYPE.
3902 The real work of conversion is done by `convert'.
3903 The purpose of this function is to generate error messages
3904 for assignments that are not allowed in C.
3905 ERRTYPE is a string to use in error messages:
3906 "assignment", "return", etc. If it is null, this is parameter passing
3907 for a function call (and different error messages are output).
3909 FUNNAME is the name of the function being called,
3910 as an IDENTIFIER_NODE, or null.
3911 PARMNUM is the number of the argument, for printing in error messages. */
3913 static tree
3914 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3915 tree type, rhs;
3916 const char *errtype;
3917 tree fundecl, funname;
3918 int parmnum;
3920 register enum tree_code codel = TREE_CODE (type);
3921 register tree rhstype;
3922 register enum tree_code coder;
3924 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3925 /* Do not use STRIP_NOPS here. We do not want an enumerator
3926 whose value is 0 to count as a null pointer constant. */
3927 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3928 rhs = TREE_OPERAND (rhs, 0);
3930 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3931 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3932 rhs = default_conversion (rhs);
3933 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3934 rhs = decl_constant_value (rhs);
3936 rhstype = TREE_TYPE (rhs);
3937 coder = TREE_CODE (rhstype);
3939 if (coder == ERROR_MARK)
3940 return error_mark_node;
3942 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3944 overflow_warning (rhs);
3945 /* Check for Objective-C protocols. This will issue a warning if
3946 there are protocol violations. No need to use the return value. */
3947 maybe_objc_comptypes (type, rhstype, 0);
3948 return rhs;
3951 if (coder == VOID_TYPE)
3953 error ("void value not ignored as it ought to be");
3954 return error_mark_node;
3956 /* Arithmetic types all interconvert, and enum is treated like int. */
3957 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
3958 || codel == COMPLEX_TYPE)
3959 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
3960 || coder == COMPLEX_TYPE))
3961 return convert_and_check (type, rhs);
3963 /* Conversion to a transparent union from its member types.
3964 This applies only to function arguments. */
3965 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3967 tree memb_types;
3968 tree marginal_memb_type = 0;
3970 for (memb_types = TYPE_FIELDS (type); memb_types;
3971 memb_types = TREE_CHAIN (memb_types))
3973 tree memb_type = TREE_TYPE (memb_types);
3975 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3976 TYPE_MAIN_VARIANT (rhstype)))
3977 break;
3979 if (TREE_CODE (memb_type) != POINTER_TYPE)
3980 continue;
3982 if (coder == POINTER_TYPE)
3984 register tree ttl = TREE_TYPE (memb_type);
3985 register tree ttr = TREE_TYPE (rhstype);
3987 /* Any non-function converts to a [const][volatile] void *
3988 and vice versa; otherwise, targets must be the same.
3989 Meanwhile, the lhs target must have all the qualifiers of
3990 the rhs. */
3991 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3992 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3993 || comp_target_types (memb_type, rhstype))
3995 /* If this type won't generate any warnings, use it. */
3996 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3997 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3998 && TREE_CODE (ttl) == FUNCTION_TYPE)
3999 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4000 == TYPE_QUALS (ttr))
4001 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4002 == TYPE_QUALS (ttl))))
4003 break;
4005 /* Keep looking for a better type, but remember this one. */
4006 if (! marginal_memb_type)
4007 marginal_memb_type = memb_type;
4011 /* Can convert integer zero to any pointer type. */
4012 if (integer_zerop (rhs)
4013 || (TREE_CODE (rhs) == NOP_EXPR
4014 && integer_zerop (TREE_OPERAND (rhs, 0))))
4016 rhs = null_pointer_node;
4017 break;
4021 if (memb_types || marginal_memb_type)
4023 if (! memb_types)
4025 /* We have only a marginally acceptable member type;
4026 it needs a warning. */
4027 register tree ttl = TREE_TYPE (marginal_memb_type);
4028 register tree ttr = TREE_TYPE (rhstype);
4030 /* Const and volatile mean something different for function
4031 types, so the usual warnings are not appropriate. */
4032 if (TREE_CODE (ttr) == FUNCTION_TYPE
4033 && TREE_CODE (ttl) == FUNCTION_TYPE)
4035 /* Because const and volatile on functions are
4036 restrictions that say the function will not do
4037 certain things, it is okay to use a const or volatile
4038 function where an ordinary one is wanted, but not
4039 vice-versa. */
4040 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4041 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4042 errtype, funname, parmnum);
4044 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4045 warn_for_assignment ("%s discards qualifiers from pointer target type",
4046 errtype, funname,
4047 parmnum);
4050 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4051 pedwarn ("ANSI C prohibits argument conversion to union type");
4053 return build1 (NOP_EXPR, type, rhs);
4057 /* Conversions among pointers */
4058 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
4060 register tree ttl = TREE_TYPE (type);
4061 register tree ttr = TREE_TYPE (rhstype);
4063 /* Any non-function converts to a [const][volatile] void *
4064 and vice versa; otherwise, targets must be the same.
4065 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4066 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4067 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4068 || comp_target_types (type, rhstype)
4069 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4070 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4072 if (pedantic
4073 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4074 && TREE_CODE (ttr) == FUNCTION_TYPE)
4076 (TYPE_MAIN_VARIANT (ttr) == void_type_node
4077 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4078 which are not ANSI null ptr constants. */
4079 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4080 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4081 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4082 errtype, funname, parmnum);
4083 /* Const and volatile mean something different for function types,
4084 so the usual warnings are not appropriate. */
4085 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4086 && TREE_CODE (ttl) != FUNCTION_TYPE)
4088 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4089 warn_for_assignment ("%s discards qualifiers from pointer target type",
4090 errtype, funname, parmnum);
4091 /* If this is not a case of ignoring a mismatch in signedness,
4092 no warning. */
4093 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4094 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4095 || comp_target_types (type, rhstype))
4097 /* If there is a mismatch, do warn. */
4098 else if (pedantic)
4099 warn_for_assignment ("pointer targets in %s differ in signedness",
4100 errtype, funname, parmnum);
4102 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4103 && TREE_CODE (ttr) == FUNCTION_TYPE)
4105 /* Because const and volatile on functions are restrictions
4106 that say the function will not do certain things,
4107 it is okay to use a const or volatile function
4108 where an ordinary one is wanted, but not vice-versa. */
4109 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4110 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4111 errtype, funname, parmnum);
4114 else
4115 warn_for_assignment ("%s from incompatible pointer type",
4116 errtype, funname, parmnum);
4117 return convert (type, rhs);
4119 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4121 /* An explicit constant 0 can convert to a pointer,
4122 or one that results from arithmetic, even including
4123 a cast to integer type. */
4124 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4126 ! (TREE_CODE (rhs) == NOP_EXPR
4127 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4128 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4129 && integer_zerop (TREE_OPERAND (rhs, 0))))
4131 warn_for_assignment ("%s makes pointer from integer without a cast",
4132 errtype, funname, parmnum);
4133 return convert (type, rhs);
4135 return null_pointer_node;
4137 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4139 warn_for_assignment ("%s makes integer from pointer without a cast",
4140 errtype, funname, parmnum);
4141 return convert (type, rhs);
4144 if (!errtype)
4146 if (funname)
4148 tree selector = maybe_building_objc_message_expr ();
4150 if (selector && parmnum > 2)
4151 error ("incompatible type for argument %d of `%s'",
4152 parmnum - 2, IDENTIFIER_POINTER (selector));
4153 else
4154 error ("incompatible type for argument %d of `%s'",
4155 parmnum, IDENTIFIER_POINTER (funname));
4157 else
4158 error ("incompatible type for argument %d of indirect function call",
4159 parmnum);
4161 else
4162 error ("incompatible types in %s", errtype);
4164 return error_mark_node;
4167 /* Print a warning using MSGID.
4168 It gets OPNAME as its one parameter.
4169 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4170 FUNCTION and ARGNUM are handled specially if we are building an
4171 Objective-C selector. */
4173 static void
4174 warn_for_assignment (msgid, opname, function, argnum)
4175 const char *msgid;
4176 const char *opname;
4177 tree function;
4178 int argnum;
4180 if (opname == 0)
4182 tree selector = maybe_building_objc_message_expr ();
4183 char * new_opname;
4185 if (selector && argnum > 2)
4187 function = selector;
4188 argnum -= 2;
4190 if (function)
4192 /* Function name is known; supply it. */
4193 const char *argstring = _("passing arg %d of `%s'");
4194 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4195 + strlen (argstring) + 1 + 25
4196 /*%d*/ + 1);
4197 sprintf (new_opname, argstring, argnum,
4198 IDENTIFIER_POINTER (function));
4200 else
4202 /* Function name unknown (call through ptr); just give arg number.*/
4203 const char *argnofun = _("passing arg %d of pointer to function");
4204 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4205 sprintf (new_opname, argnofun, argnum);
4207 opname = new_opname;
4209 pedwarn (msgid, opname);
4212 /* If VALUE is a compound expr all of whose expressions are constant, then
4213 return its value. Otherwise, return error_mark_node.
4215 This is for handling COMPOUND_EXPRs as initializer elements
4216 which is allowed with a warning when -pedantic is specified. */
4218 static tree
4219 valid_compound_expr_initializer (value, endtype)
4220 tree value;
4221 tree endtype;
4223 if (TREE_CODE (value) == COMPOUND_EXPR)
4225 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4226 == error_mark_node)
4227 return error_mark_node;
4228 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4229 endtype);
4231 else if (! TREE_CONSTANT (value)
4232 && ! initializer_constant_valid_p (value, endtype))
4233 return error_mark_node;
4234 else
4235 return value;
4238 /* Perform appropriate conversions on the initial value of a variable,
4239 store it in the declaration DECL,
4240 and print any error messages that are appropriate.
4241 If the init is invalid, store an ERROR_MARK. */
4243 void
4244 store_init_value (decl, init)
4245 tree decl, init;
4247 register tree value, type;
4249 /* If variable's type was invalidly declared, just ignore it. */
4251 type = TREE_TYPE (decl);
4252 if (TREE_CODE (type) == ERROR_MARK)
4253 return;
4255 /* Digest the specified initializer into an expression. */
4257 value = digest_init (type, init, TREE_STATIC (decl),
4258 TREE_STATIC (decl) || pedantic);
4260 /* Store the expression if valid; else report error. */
4262 #if 0
4263 /* Note that this is the only place we can detect the error
4264 in a case such as struct foo bar = (struct foo) { x, y };
4265 where there is one initial value which is a constructor expression. */
4266 if (value == error_mark_node)
4268 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4270 error ("initializer for static variable is not constant");
4271 value = error_mark_node;
4273 else if (TREE_STATIC (decl)
4274 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4276 error ("initializer for static variable uses complicated arithmetic");
4277 value = error_mark_node;
4279 else
4281 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4283 if (! TREE_CONSTANT (value))
4284 pedwarn ("aggregate initializer is not constant");
4285 else if (! TREE_STATIC (value))
4286 pedwarn ("aggregate initializer uses complicated arithmetic");
4289 #endif
4291 DECL_INITIAL (decl) = value;
4293 /* ANSI wants warnings about out-of-range constant initializers. */
4294 STRIP_TYPE_NOPS (value);
4295 constant_expression_warning (value);
4298 /* Methods for storing and printing names for error messages. */
4300 /* Implement a spelling stack that allows components of a name to be pushed
4301 and popped. Each element on the stack is this structure. */
4303 struct spelling
4305 int kind;
4306 union
4308 int i;
4309 const char *s;
4310 } u;
4313 #define SPELLING_STRING 1
4314 #define SPELLING_MEMBER 2
4315 #define SPELLING_BOUNDS 3
4317 static struct spelling *spelling; /* Next stack element (unused). */
4318 static struct spelling *spelling_base; /* Spelling stack base. */
4319 static int spelling_size; /* Size of the spelling stack. */
4321 /* Macros to save and restore the spelling stack around push_... functions.
4322 Alternative to SAVE_SPELLING_STACK. */
4324 #define SPELLING_DEPTH() (spelling - spelling_base)
4325 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4327 /* Save and restore the spelling stack around arbitrary C code. */
4329 #define SAVE_SPELLING_DEPTH(code) \
4331 int __depth = SPELLING_DEPTH (); \
4332 code; \
4333 RESTORE_SPELLING_DEPTH (__depth); \
4336 /* Push an element on the spelling stack with type KIND and assign VALUE
4337 to MEMBER. */
4339 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4341 int depth = SPELLING_DEPTH (); \
4343 if (depth >= spelling_size) \
4345 spelling_size += 10; \
4346 if (spelling_base == 0) \
4347 spelling_base \
4348 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4349 else \
4350 spelling_base \
4351 = (struct spelling *) xrealloc (spelling_base, \
4352 spelling_size * sizeof (struct spelling)); \
4353 RESTORE_SPELLING_DEPTH (depth); \
4356 spelling->kind = (KIND); \
4357 spelling->MEMBER = (VALUE); \
4358 spelling++; \
4361 /* Push STRING on the stack. Printed literally. */
4363 static void
4364 push_string (string)
4365 const char *string;
4367 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4370 /* Push a member name on the stack. Printed as '.' STRING. */
4372 static void
4373 push_member_name (decl)
4374 tree decl;
4377 const char *string
4378 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4379 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4382 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4384 static void
4385 push_array_bounds (bounds)
4386 int bounds;
4388 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4391 /* Compute the maximum size in bytes of the printed spelling. */
4393 static int
4394 spelling_length ()
4396 register int size = 0;
4397 register struct spelling *p;
4399 for (p = spelling_base; p < spelling; p++)
4401 if (p->kind == SPELLING_BOUNDS)
4402 size += 25;
4403 else
4404 size += strlen (p->u.s) + 1;
4407 return size;
4410 /* Print the spelling to BUFFER and return it. */
4412 static char *
4413 print_spelling (buffer)
4414 register char *buffer;
4416 register char *d = buffer;
4417 register struct spelling *p;
4419 for (p = spelling_base; p < spelling; p++)
4420 if (p->kind == SPELLING_BOUNDS)
4422 sprintf (d, "[%d]", p->u.i);
4423 d += strlen (d);
4425 else
4427 register const char *s;
4428 if (p->kind == SPELLING_MEMBER)
4429 *d++ = '.';
4430 for (s = p->u.s; (*d = *s++); d++)
4433 *d++ = '\0';
4434 return buffer;
4437 /* Issue an error message for a bad initializer component.
4438 MSGID identifies the message.
4439 The component name is taken from the spelling stack. */
4441 void
4442 error_init (msgid)
4443 const char *msgid;
4445 char *ofwhat;
4447 error (msgid);
4448 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4449 if (*ofwhat)
4450 error ("(near initialization for `%s')", ofwhat);
4453 /* Issue a pedantic warning for a bad initializer component.
4454 MSGID identifies the message.
4455 The component name is taken from the spelling stack. */
4457 void
4458 pedwarn_init (msgid)
4459 const char *msgid;
4461 char *ofwhat;
4463 pedwarn (msgid);
4464 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4465 if (*ofwhat)
4466 pedwarn ("(near initialization for `%s')", ofwhat);
4469 /* Issue a warning for a bad initializer component.
4470 MSGID identifies the message.
4471 The component name is taken from the spelling stack. */
4473 static void
4474 warning_init (msgid)
4475 const char *msgid;
4477 char *ofwhat;
4479 warning (msgid);
4480 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4481 if (*ofwhat)
4482 warning ("(near initialization for `%s')", ofwhat);
4485 /* Digest the parser output INIT as an initializer for type TYPE.
4486 Return a C expression of type TYPE to represent the initial value.
4488 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4489 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4490 applies only to elements of constructors. */
4492 static tree
4493 digest_init (type, init, require_constant, constructor_constant)
4494 tree type, init;
4495 int require_constant, constructor_constant;
4497 enum tree_code code = TREE_CODE (type);
4498 tree inside_init = init;
4500 if (type == error_mark_node || init == error_mark_node)
4501 return error_mark_node;
4503 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4504 /* Do not use STRIP_NOPS here. We do not want an enumerator
4505 whose value is 0 to count as a null pointer constant. */
4506 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4507 inside_init = TREE_OPERAND (init, 0);
4509 /* Initialization of an array of chars from a string constant
4510 optionally enclosed in braces. */
4512 if (code == ARRAY_TYPE)
4514 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4515 if ((typ1 == char_type_node
4516 || typ1 == signed_char_type_node
4517 || typ1 == unsigned_char_type_node
4518 || typ1 == unsigned_wchar_type_node
4519 || typ1 == signed_wchar_type_node)
4520 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4522 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4523 TYPE_MAIN_VARIANT (type)))
4524 return inside_init;
4526 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4527 != char_type_node)
4528 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4530 error_init ("char-array initialized from wide string");
4531 return error_mark_node;
4533 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4534 == char_type_node)
4535 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4537 error_init ("int-array initialized from non-wide string");
4538 return error_mark_node;
4541 TREE_TYPE (inside_init) = type;
4542 if (TYPE_DOMAIN (type) != 0
4543 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4545 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4546 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4547 /* Subtract 1 (or sizeof (wchar_t))
4548 because it's ok to ignore the terminating null char
4549 that is counted in the length of the constant. */
4550 if (size < TREE_STRING_LENGTH (inside_init)
4551 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4552 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4553 : 1))
4554 pedwarn_init ("initializer-string for array of chars is too long");
4556 return inside_init;
4560 /* Any type can be initialized
4561 from an expression of the same type, optionally with braces. */
4563 if (inside_init && TREE_TYPE (inside_init) != 0
4564 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4565 TYPE_MAIN_VARIANT (type))
4566 || (code == ARRAY_TYPE
4567 && comptypes (TREE_TYPE (inside_init), type))
4568 || (code == POINTER_TYPE
4569 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4570 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4571 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4572 TREE_TYPE (type)))))
4574 if (code == POINTER_TYPE
4575 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4576 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4577 inside_init = default_conversion (inside_init);
4578 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4579 && TREE_CODE (inside_init) != CONSTRUCTOR)
4581 error_init ("array initialized from non-constant array expression");
4582 return error_mark_node;
4585 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4586 inside_init = decl_constant_value (inside_init);
4588 /* Compound expressions can only occur here if -pedantic or
4589 -pedantic-errors is specified. In the later case, we always want
4590 an error. In the former case, we simply want a warning. */
4591 if (require_constant && pedantic
4592 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4594 inside_init
4595 = valid_compound_expr_initializer (inside_init,
4596 TREE_TYPE (inside_init));
4597 if (inside_init == error_mark_node)
4598 error_init ("initializer element is not constant");
4599 else
4600 pedwarn_init ("initializer element is not constant");
4601 if (flag_pedantic_errors)
4602 inside_init = error_mark_node;
4604 else if (require_constant && ! TREE_CONSTANT (inside_init))
4606 error_init ("initializer element is not constant");
4607 inside_init = error_mark_node;
4609 else if (require_constant
4610 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4612 error_init ("initializer element is not computable at load time");
4613 inside_init = error_mark_node;
4616 return inside_init;
4619 /* Handle scalar types, including conversions. */
4621 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4622 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4624 /* Note that convert_for_assignment calls default_conversion
4625 for arrays and functions. We must not call it in the
4626 case where inside_init is a null pointer constant. */
4627 inside_init
4628 = convert_for_assignment (type, init, _("initialization"),
4629 NULL_TREE, NULL_TREE, 0);
4631 if (require_constant && ! TREE_CONSTANT (inside_init))
4633 error_init ("initializer element is not constant");
4634 inside_init = error_mark_node;
4636 else if (require_constant
4637 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4639 error_init ("initializer element is not computable at load time");
4640 inside_init = error_mark_node;
4643 return inside_init;
4646 /* Come here only for records and arrays. */
4648 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4650 error_init ("variable-sized object may not be initialized");
4651 return error_mark_node;
4654 /* Traditionally, you can write struct foo x = 0;
4655 and it initializes the first element of x to 0. */
4656 if (flag_traditional)
4658 tree top = 0, prev = 0, otype = type;
4659 while (TREE_CODE (type) == RECORD_TYPE
4660 || TREE_CODE (type) == ARRAY_TYPE
4661 || TREE_CODE (type) == QUAL_UNION_TYPE
4662 || TREE_CODE (type) == UNION_TYPE)
4664 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4665 if (prev == 0)
4666 top = temp;
4667 else
4668 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4669 prev = temp;
4670 if (TREE_CODE (type) == ARRAY_TYPE)
4671 type = TREE_TYPE (type);
4672 else if (TYPE_FIELDS (type))
4673 type = TREE_TYPE (TYPE_FIELDS (type));
4674 else
4676 error_init ("invalid initializer");
4677 return error_mark_node;
4681 if (otype != type)
4683 TREE_OPERAND (prev, 1)
4684 = build_tree_list (NULL_TREE,
4685 digest_init (type, init, require_constant,
4686 constructor_constant));
4687 return top;
4689 else
4690 return error_mark_node;
4692 error_init ("invalid initializer");
4693 return error_mark_node;
4696 /* Handle initializers that use braces. */
4698 /* Type of object we are accumulating a constructor for.
4699 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4700 static tree constructor_type;
4702 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4703 left to fill. */
4704 static tree constructor_fields;
4706 /* For an ARRAY_TYPE, this is the specified index
4707 at which to store the next element we get.
4708 This is a special INTEGER_CST node that we modify in place. */
4709 static tree constructor_index;
4711 /* For an ARRAY_TYPE, this is the end index of the range
4712 to initialize with the next element, or NULL in the ordinary case
4713 where the element is used just once. */
4714 static tree constructor_range_end;
4716 /* For an ARRAY_TYPE, this is the maximum index. */
4717 static tree constructor_max_index;
4719 /* For a RECORD_TYPE, this is the first field not yet written out. */
4720 static tree constructor_unfilled_fields;
4722 /* For an ARRAY_TYPE, this is the index of the first element
4723 not yet written out.
4724 This is a special INTEGER_CST node that we modify in place. */
4725 static tree constructor_unfilled_index;
4727 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4728 This is so we can generate gaps between fields, when appropriate.
4729 This is a special INTEGER_CST node that we modify in place. */
4730 static tree constructor_bit_index;
4732 /* If we are saving up the elements rather than allocating them,
4733 this is the list of elements so far (in reverse order,
4734 most recent first). */
4735 static tree constructor_elements;
4737 /* 1 if so far this constructor's elements are all compile-time constants. */
4738 static int constructor_constant;
4740 /* 1 if so far this constructor's elements are all valid address constants. */
4741 static int constructor_simple;
4743 /* 1 if this constructor is erroneous so far. */
4744 static int constructor_erroneous;
4746 /* 1 if have called defer_addressed_constants. */
4747 static int constructor_subconstants_deferred;
4749 /* Structure for managing pending initializer elements, organized as an
4750 AVL tree. */
4752 struct init_node
4754 struct init_node *left, *right;
4755 struct init_node *parent;
4756 int balance;
4757 tree purpose;
4758 tree value;
4761 /* Tree of pending elements at this constructor level.
4762 These are elements encountered out of order
4763 which belong at places we haven't reached yet in actually
4764 writing the output. */
4765 static struct init_node *constructor_pending_elts;
4767 /* The SPELLING_DEPTH of this constructor. */
4768 static int constructor_depth;
4770 /* 0 if implicitly pushing constructor levels is allowed. */
4771 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4773 static int require_constant_value;
4774 static int require_constant_elements;
4776 /* 1 if it is ok to output this constructor as we read it.
4777 0 means must accumulate a CONSTRUCTOR expression. */
4778 static int constructor_incremental;
4780 /* DECL node for which an initializer is being read.
4781 0 means we are reading a constructor expression
4782 such as (struct foo) {...}. */
4783 static tree constructor_decl;
4785 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4786 static char *constructor_asmspec;
4788 /* Nonzero if this is an initializer for a top-level decl. */
4789 static int constructor_top_level;
4792 /* This stack has a level for each implicit or explicit level of
4793 structuring in the initializer, including the outermost one. It
4794 saves the values of most of the variables above. */
4796 struct constructor_stack
4798 struct constructor_stack *next;
4799 tree type;
4800 tree fields;
4801 tree index;
4802 tree range_end;
4803 tree max_index;
4804 tree unfilled_index;
4805 tree unfilled_fields;
4806 tree bit_index;
4807 tree elements;
4808 int offset;
4809 struct init_node *pending_elts;
4810 int depth;
4811 /* If nonzero, this value should replace the entire
4812 constructor at this level. */
4813 tree replacement_value;
4814 char constant;
4815 char simple;
4816 char implicit;
4817 char incremental;
4818 char erroneous;
4819 char outer;
4822 struct constructor_stack *constructor_stack;
4824 /* This stack records separate initializers that are nested.
4825 Nested initializers can't happen in ANSI C, but GNU C allows them
4826 in cases like { ... (struct foo) { ... } ... }. */
4828 struct initializer_stack
4830 struct initializer_stack *next;
4831 tree decl;
4832 char *asmspec;
4833 struct constructor_stack *constructor_stack;
4834 tree elements;
4835 struct spelling *spelling;
4836 struct spelling *spelling_base;
4837 int spelling_size;
4838 char top_level;
4839 char incremental;
4840 char require_constant_value;
4841 char require_constant_elements;
4842 char deferred;
4845 struct initializer_stack *initializer_stack;
4847 /* Prepare to parse and output the initializer for variable DECL. */
4849 void
4850 start_init (decl, asmspec_tree, top_level)
4851 tree decl;
4852 tree asmspec_tree;
4853 int top_level;
4855 const char *locus;
4856 struct initializer_stack *p
4857 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
4858 char *asmspec = 0;
4860 if (asmspec_tree)
4861 asmspec = TREE_STRING_POINTER (asmspec_tree);
4863 p->decl = constructor_decl;
4864 p->asmspec = constructor_asmspec;
4865 p->incremental = constructor_incremental;
4866 p->require_constant_value = require_constant_value;
4867 p->require_constant_elements = require_constant_elements;
4868 p->constructor_stack = constructor_stack;
4869 p->elements = constructor_elements;
4870 p->spelling = spelling;
4871 p->spelling_base = spelling_base;
4872 p->spelling_size = spelling_size;
4873 p->deferred = constructor_subconstants_deferred;
4874 p->top_level = constructor_top_level;
4875 p->next = initializer_stack;
4876 initializer_stack = p;
4878 constructor_decl = decl;
4879 constructor_incremental = top_level;
4880 constructor_asmspec = asmspec;
4881 constructor_subconstants_deferred = 0;
4882 constructor_top_level = top_level;
4884 if (decl != 0)
4886 require_constant_value = TREE_STATIC (decl);
4887 require_constant_elements
4888 = ((TREE_STATIC (decl) || pedantic)
4889 /* For a scalar, you can always use any value to initialize,
4890 even within braces. */
4891 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4892 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4893 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4894 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4895 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4896 constructor_incremental |= TREE_STATIC (decl);
4898 else
4900 require_constant_value = 0;
4901 require_constant_elements = 0;
4902 locus = "(anonymous)";
4905 constructor_stack = 0;
4907 missing_braces_mentioned = 0;
4909 spelling_base = 0;
4910 spelling_size = 0;
4911 RESTORE_SPELLING_DEPTH (0);
4913 if (locus)
4914 push_string (locus);
4917 void
4918 finish_init ()
4920 struct initializer_stack *p = initializer_stack;
4922 /* Output subconstants (string constants, usually)
4923 that were referenced within this initializer and saved up.
4924 Must do this if and only if we called defer_addressed_constants. */
4925 if (constructor_subconstants_deferred)
4926 output_deferred_addressed_constants ();
4928 /* Free the whole constructor stack of this initializer. */
4929 while (constructor_stack)
4931 struct constructor_stack *q = constructor_stack;
4932 constructor_stack = q->next;
4933 free (q);
4936 /* Pop back to the data of the outer initializer (if any). */
4937 constructor_decl = p->decl;
4938 constructor_asmspec = p->asmspec;
4939 constructor_incremental = p->incremental;
4940 require_constant_value = p->require_constant_value;
4941 require_constant_elements = p->require_constant_elements;
4942 constructor_stack = p->constructor_stack;
4943 constructor_elements = p->elements;
4944 spelling = p->spelling;
4945 spelling_base = p->spelling_base;
4946 spelling_size = p->spelling_size;
4947 constructor_subconstants_deferred = p->deferred;
4948 constructor_top_level = p->top_level;
4949 initializer_stack = p->next;
4950 free (p);
4953 /* Call here when we see the initializer is surrounded by braces.
4954 This is instead of a call to push_init_level;
4955 it is matched by a call to pop_init_level.
4957 TYPE is the type to initialize, for a constructor expression.
4958 For an initializer for a decl, TYPE is zero. */
4960 void
4961 really_start_incremental_init (type)
4962 tree type;
4964 struct constructor_stack *p
4965 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
4967 if (type == 0)
4968 type = TREE_TYPE (constructor_decl);
4970 /* Turn off constructor_incremental if type is a struct with bitfields.
4971 Do this before the first push, so that the corrected value
4972 is available in finish_init. */
4973 check_init_type_bitfields (type);
4975 p->type = constructor_type;
4976 p->fields = constructor_fields;
4977 p->index = constructor_index;
4978 p->range_end = constructor_range_end;
4979 p->max_index = constructor_max_index;
4980 p->unfilled_index = constructor_unfilled_index;
4981 p->unfilled_fields = constructor_unfilled_fields;
4982 p->bit_index = constructor_bit_index;
4983 p->elements = constructor_elements;
4984 p->constant = constructor_constant;
4985 p->simple = constructor_simple;
4986 p->erroneous = constructor_erroneous;
4987 p->pending_elts = constructor_pending_elts;
4988 p->depth = constructor_depth;
4989 p->replacement_value = 0;
4990 p->implicit = 0;
4991 p->incremental = constructor_incremental;
4992 p->outer = 0;
4993 p->next = 0;
4994 constructor_stack = p;
4996 constructor_constant = 1;
4997 constructor_simple = 1;
4998 constructor_depth = SPELLING_DEPTH ();
4999 constructor_elements = 0;
5000 constructor_pending_elts = 0;
5001 constructor_type = type;
5003 if (TREE_CODE (constructor_type) == RECORD_TYPE
5004 || TREE_CODE (constructor_type) == UNION_TYPE)
5006 constructor_fields = TYPE_FIELDS (constructor_type);
5007 /* Skip any nameless bit fields at the beginning. */
5008 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5009 && DECL_NAME (constructor_fields) == 0)
5010 constructor_fields = TREE_CHAIN (constructor_fields);
5011 constructor_unfilled_fields = constructor_fields;
5012 constructor_bit_index = copy_node (integer_zero_node);
5013 TREE_TYPE (constructor_bit_index) = sbitsizetype;
5015 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5017 constructor_range_end = 0;
5018 if (TYPE_DOMAIN (constructor_type))
5020 constructor_max_index
5021 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5022 constructor_index
5023 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5025 else
5026 constructor_index = copy_node (integer_zero_node);
5027 constructor_unfilled_index = copy_node (constructor_index);
5029 else
5031 /* Handle the case of int x = {5}; */
5032 constructor_fields = constructor_type;
5033 constructor_unfilled_fields = constructor_type;
5036 if (constructor_incremental)
5038 int momentary = suspend_momentary ();
5039 push_obstacks_nochange ();
5040 if (TREE_PERMANENT (constructor_decl))
5041 end_temporary_allocation ();
5042 make_decl_rtl (constructor_decl, constructor_asmspec,
5043 constructor_top_level);
5044 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5045 pop_obstacks ();
5046 resume_momentary (momentary);
5049 if (constructor_incremental)
5051 defer_addressed_constants ();
5052 constructor_subconstants_deferred = 1;
5056 /* Push down into a subobject, for initialization.
5057 If this is for an explicit set of braces, IMPLICIT is 0.
5058 If it is because the next element belongs at a lower level,
5059 IMPLICIT is 1. */
5061 void
5062 push_init_level (implicit)
5063 int implicit;
5065 struct constructor_stack *p;
5067 /* If we've exhausted any levels that didn't have braces,
5068 pop them now. */
5069 while (constructor_stack->implicit)
5071 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5072 || TREE_CODE (constructor_type) == UNION_TYPE)
5073 && constructor_fields == 0)
5074 process_init_element (pop_init_level (1));
5075 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5076 && tree_int_cst_lt (constructor_max_index, constructor_index))
5077 process_init_element (pop_init_level (1));
5078 else
5079 break;
5082 /* Structure elements may require alignment. Do this now if necessary
5083 for the subaggregate, and if it comes next in sequence. Don't do
5084 this for subaggregates that will go on the pending list. */
5085 if (constructor_incremental && constructor_type != 0
5086 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields
5087 && constructor_fields == constructor_unfilled_fields)
5089 /* Advance to offset of this element. */
5090 if (! tree_int_cst_equal (constructor_bit_index,
5091 DECL_FIELD_BITPOS (constructor_fields)))
5093 /* By using unsigned arithmetic, the result will be correct even
5094 in case of overflows, if BITS_PER_UNIT is a power of two. */
5095 unsigned next = (TREE_INT_CST_LOW
5096 (DECL_FIELD_BITPOS (constructor_fields))
5097 / (unsigned)BITS_PER_UNIT);
5098 unsigned here = (TREE_INT_CST_LOW (constructor_bit_index)
5099 / (unsigned)BITS_PER_UNIT);
5101 assemble_zeros ((next - here)
5102 * (unsigned)BITS_PER_UNIT
5103 / (unsigned)BITS_PER_UNIT);
5105 /* Indicate that we have now filled the structure up to the current
5106 field. */
5107 constructor_unfilled_fields = constructor_fields;
5110 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5111 p->type = constructor_type;
5112 p->fields = constructor_fields;
5113 p->index = constructor_index;
5114 p->range_end = constructor_range_end;
5115 p->max_index = constructor_max_index;
5116 p->unfilled_index = constructor_unfilled_index;
5117 p->unfilled_fields = constructor_unfilled_fields;
5118 p->bit_index = constructor_bit_index;
5119 p->elements = constructor_elements;
5120 p->constant = constructor_constant;
5121 p->simple = constructor_simple;
5122 p->erroneous = constructor_erroneous;
5123 p->pending_elts = constructor_pending_elts;
5124 p->depth = constructor_depth;
5125 p->replacement_value = 0;
5126 p->implicit = implicit;
5127 p->incremental = constructor_incremental;
5128 p->outer = 0;
5129 p->next = constructor_stack;
5130 constructor_stack = p;
5132 constructor_constant = 1;
5133 constructor_simple = 1;
5134 constructor_depth = SPELLING_DEPTH ();
5135 constructor_elements = 0;
5136 constructor_pending_elts = 0;
5138 /* Don't die if an entire brace-pair level is superfluous
5139 in the containing level. */
5140 if (constructor_type == 0)
5142 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5143 || TREE_CODE (constructor_type) == UNION_TYPE)
5145 /* Don't die if there are extra init elts at the end. */
5146 if (constructor_fields == 0)
5147 constructor_type = 0;
5148 else
5150 constructor_type = TREE_TYPE (constructor_fields);
5151 push_member_name (constructor_fields);
5152 constructor_depth++;
5153 if (constructor_fields != constructor_unfilled_fields)
5154 constructor_incremental = 0;
5157 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5159 constructor_type = TREE_TYPE (constructor_type);
5160 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5161 constructor_depth++;
5162 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5163 || constructor_range_end != 0)
5164 constructor_incremental = 0;
5167 if (constructor_type == 0)
5169 error_init ("extra brace group at end of initializer");
5170 constructor_fields = 0;
5171 constructor_unfilled_fields = 0;
5172 return;
5175 /* Turn off constructor_incremental if type is a struct with bitfields. */
5176 check_init_type_bitfields (constructor_type);
5178 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5180 missing_braces_mentioned = 1;
5181 warning_init ("missing braces around initializer");
5184 if (TREE_CODE (constructor_type) == RECORD_TYPE
5185 || TREE_CODE (constructor_type) == UNION_TYPE)
5187 constructor_fields = TYPE_FIELDS (constructor_type);
5188 /* Skip any nameless bit fields at the beginning. */
5189 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5190 && DECL_NAME (constructor_fields) == 0)
5191 constructor_fields = TREE_CHAIN (constructor_fields);
5192 constructor_unfilled_fields = constructor_fields;
5193 constructor_bit_index = copy_node (integer_zero_node);
5194 TREE_TYPE (constructor_bit_index) = sbitsizetype;
5196 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5198 constructor_range_end = 0;
5199 if (TYPE_DOMAIN (constructor_type))
5201 constructor_max_index
5202 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5203 constructor_index
5204 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5206 else
5207 constructor_index = copy_node (integer_zero_node);
5208 constructor_unfilled_index = copy_node (constructor_index);
5210 else
5212 warning_init ("braces around scalar initializer");
5213 constructor_fields = constructor_type;
5214 constructor_unfilled_fields = constructor_type;
5218 /* Don't read a struct incrementally if it has any bitfields,
5219 because the incremental reading code doesn't know how to
5220 handle bitfields yet. */
5222 static void
5223 check_init_type_bitfields (type)
5224 tree type;
5226 if (TREE_CODE (type) == RECORD_TYPE)
5228 tree tail;
5229 for (tail = TYPE_FIELDS (type); tail;
5230 tail = TREE_CHAIN (tail))
5232 if (DECL_C_BIT_FIELD (tail))
5234 constructor_incremental = 0;
5235 break;
5238 check_init_type_bitfields (TREE_TYPE (tail));
5242 else if (TREE_CODE (type) == UNION_TYPE)
5244 tree tail = TYPE_FIELDS (type);
5245 if (tail && DECL_C_BIT_FIELD (tail))
5246 /* We also use the nonincremental algorithm for initiliazation
5247 of unions whose first member is a bitfield, becuase the
5248 incremental algorithm has no code for dealing with
5249 bitfields. */
5250 constructor_incremental = 0;
5253 else if (TREE_CODE (type) == ARRAY_TYPE)
5254 check_init_type_bitfields (TREE_TYPE (type));
5257 /* At the end of an implicit or explicit brace level,
5258 finish up that level of constructor.
5259 If we were outputting the elements as they are read, return 0
5260 from inner levels (process_init_element ignores that),
5261 but return error_mark_node from the outermost level
5262 (that's what we want to put in DECL_INITIAL).
5263 Otherwise, return a CONSTRUCTOR expression. */
5265 tree
5266 pop_init_level (implicit)
5267 int implicit;
5269 struct constructor_stack *p;
5270 int size = 0;
5271 tree constructor = 0;
5273 if (implicit == 0)
5275 /* When we come to an explicit close brace,
5276 pop any inner levels that didn't have explicit braces. */
5277 while (constructor_stack->implicit)
5278 process_init_element (pop_init_level (1));
5281 p = constructor_stack;
5283 if (constructor_type != 0)
5284 size = int_size_in_bytes (constructor_type);
5286 /* Warn when some struct elements are implicitly initialized to zero. */
5287 if (extra_warnings
5288 && constructor_type
5289 && TREE_CODE (constructor_type) == RECORD_TYPE
5290 && constructor_unfilled_fields)
5292 push_member_name (constructor_unfilled_fields);
5293 warning_init ("missing initializer");
5294 RESTORE_SPELLING_DEPTH (constructor_depth);
5297 /* Now output all pending elements. */
5298 output_pending_init_elements (1);
5300 #if 0 /* c-parse.in warns about {}. */
5301 /* In ANSI, each brace level must have at least one element. */
5302 if (! implicit && pedantic
5303 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5304 ? integer_zerop (constructor_unfilled_index)
5305 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5306 pedwarn_init ("empty braces in initializer");
5307 #endif
5309 /* Pad out the end of the structure. */
5311 if (p->replacement_value)
5313 /* If this closes a superfluous brace pair,
5314 just pass out the element between them. */
5315 constructor = p->replacement_value;
5316 /* If this is the top level thing within the initializer,
5317 and it's for a variable, then since we already called
5318 assemble_variable, we must output the value now. */
5319 if (p->next == 0 && constructor_decl != 0
5320 && constructor_incremental)
5322 constructor = digest_init (constructor_type, constructor,
5323 require_constant_value,
5324 require_constant_elements);
5326 /* If initializing an array of unknown size,
5327 determine the size now. */
5328 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5329 && TYPE_DOMAIN (constructor_type) == 0)
5331 int failure;
5332 int momentary_p;
5334 push_obstacks_nochange ();
5335 if (TREE_PERMANENT (constructor_type))
5336 end_temporary_allocation ();
5338 momentary_p = suspend_momentary ();
5340 /* We shouldn't have an incomplete array type within
5341 some other type. */
5342 if (constructor_stack->next)
5343 abort ();
5345 failure
5346 = complete_array_type (constructor_type,
5347 constructor, 0);
5348 if (failure)
5349 abort ();
5351 size = int_size_in_bytes (constructor_type);
5352 resume_momentary (momentary_p);
5353 pop_obstacks ();
5356 output_constant (constructor, size);
5359 else if (constructor_type == 0)
5361 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5362 && TREE_CODE (constructor_type) != UNION_TYPE
5363 && TREE_CODE (constructor_type) != ARRAY_TYPE
5364 && ! constructor_incremental)
5366 /* A nonincremental scalar initializer--just return
5367 the element, after verifying there is just one. */
5368 if (constructor_elements == 0)
5370 error_init ("empty scalar initializer");
5371 constructor = error_mark_node;
5373 else if (TREE_CHAIN (constructor_elements) != 0)
5375 error_init ("extra elements in scalar initializer");
5376 constructor = TREE_VALUE (constructor_elements);
5378 else
5379 constructor = TREE_VALUE (constructor_elements);
5381 else if (! constructor_incremental)
5383 if (constructor_erroneous)
5384 constructor = error_mark_node;
5385 else
5387 int momentary = suspend_momentary ();
5389 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5390 nreverse (constructor_elements));
5391 if (constructor_constant)
5392 TREE_CONSTANT (constructor) = 1;
5393 if (constructor_constant && constructor_simple)
5394 TREE_STATIC (constructor) = 1;
5396 resume_momentary (momentary);
5399 else
5401 tree filled;
5402 int momentary = suspend_momentary ();
5404 if (TREE_CODE (constructor_type) == RECORD_TYPE
5405 || TREE_CODE (constructor_type) == UNION_TYPE)
5407 /* Find the offset of the end of that field. */
5408 filled = size_binop (CEIL_DIV_EXPR,
5409 constructor_bit_index,
5410 size_int (BITS_PER_UNIT));
5412 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5414 /* If initializing an array of unknown size,
5415 determine the size now. */
5416 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5417 && TYPE_DOMAIN (constructor_type) == 0)
5419 tree maxindex
5420 = size_binop (MINUS_EXPR,
5421 constructor_unfilled_index,
5422 integer_one_node);
5424 push_obstacks_nochange ();
5425 if (TREE_PERMANENT (constructor_type))
5426 end_temporary_allocation ();
5427 maxindex = copy_node (maxindex);
5428 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5429 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5431 /* TYPE_MAX_VALUE is always one less than the number of elements
5432 in the array, because we start counting at zero. Therefore,
5433 warn only if the value is less than zero. */
5434 if (pedantic
5435 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5436 < 0))
5437 error_with_decl (constructor_decl,
5438 "zero or negative array size `%s'");
5439 layout_type (constructor_type);
5440 size = int_size_in_bytes (constructor_type);
5441 pop_obstacks ();
5444 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5445 size_in_bytes (TREE_TYPE (constructor_type)));
5447 else
5448 filled = 0;
5450 if (filled != 0)
5451 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5453 resume_momentary (momentary);
5457 constructor_type = p->type;
5458 constructor_fields = p->fields;
5459 constructor_index = p->index;
5460 constructor_range_end = p->range_end;
5461 constructor_max_index = p->max_index;
5462 constructor_unfilled_index = p->unfilled_index;
5463 constructor_unfilled_fields = p->unfilled_fields;
5464 constructor_bit_index = p->bit_index;
5465 constructor_elements = p->elements;
5466 constructor_constant = p->constant;
5467 constructor_simple = p->simple;
5468 constructor_erroneous = p->erroneous;
5469 constructor_pending_elts = p->pending_elts;
5470 constructor_depth = p->depth;
5471 constructor_incremental = p->incremental;
5472 RESTORE_SPELLING_DEPTH (constructor_depth);
5474 constructor_stack = p->next;
5475 free (p);
5477 if (constructor == 0)
5479 if (constructor_stack == 0)
5480 return error_mark_node;
5481 return NULL_TREE;
5483 return constructor;
5486 /* Within an array initializer, specify the next index to be initialized.
5487 FIRST is that index. If LAST is nonzero, then initialize a range
5488 of indices, running from FIRST through LAST. */
5490 void
5491 set_init_index (first, last)
5492 tree first, last;
5494 while ((TREE_CODE (first) == NOP_EXPR
5495 || TREE_CODE (first) == CONVERT_EXPR
5496 || TREE_CODE (first) == NON_LVALUE_EXPR)
5497 && (TYPE_MODE (TREE_TYPE (first))
5498 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5499 (first) = TREE_OPERAND (first, 0);
5500 if (last)
5501 while ((TREE_CODE (last) == NOP_EXPR
5502 || TREE_CODE (last) == CONVERT_EXPR
5503 || TREE_CODE (last) == NON_LVALUE_EXPR)
5504 && (TYPE_MODE (TREE_TYPE (last))
5505 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5506 (last) = TREE_OPERAND (last, 0);
5508 if (TREE_CODE (first) != INTEGER_CST)
5509 error_init ("nonconstant array index in initializer");
5510 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5511 error_init ("nonconstant array index in initializer");
5512 else if (! constructor_unfilled_index)
5513 error_init ("array index in non-array initializer");
5514 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5515 error_init ("duplicate array index in initializer");
5516 else
5518 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (first);
5519 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (first);
5521 if (last != 0 && tree_int_cst_lt (last, first))
5522 error_init ("empty index range in initializer");
5523 else
5525 if (pedantic)
5526 pedwarn ("ANSI C forbids specifying element to initialize");
5527 constructor_range_end = last;
5532 /* Within a struct initializer, specify the next field to be initialized. */
5534 void
5535 set_init_label (fieldname)
5536 tree fieldname;
5538 tree tail;
5539 int passed = 0;
5541 /* Don't die if an entire brace-pair level is superfluous
5542 in the containing level. */
5543 if (constructor_type == 0)
5544 return;
5546 for (tail = TYPE_FIELDS (constructor_type); tail;
5547 tail = TREE_CHAIN (tail))
5549 if (tail == constructor_unfilled_fields)
5550 passed = 1;
5551 if (DECL_NAME (tail) == fieldname)
5552 break;
5555 if (tail == 0)
5556 error ("unknown field `%s' specified in initializer",
5557 IDENTIFIER_POINTER (fieldname));
5558 else if (!passed)
5559 error ("field `%s' already initialized",
5560 IDENTIFIER_POINTER (fieldname));
5561 else
5563 constructor_fields = tail;
5564 if (pedantic)
5565 pedwarn ("ANSI C forbids specifying structure member to initialize");
5569 /* Add a new initializer to the tree of pending initializers. PURPOSE
5570 indentifies the initializer, either array index or field in a structure.
5571 VALUE is the value of that index or field. */
5573 static void
5574 add_pending_init (purpose, value)
5575 tree purpose, value;
5577 struct init_node *p, **q, *r;
5579 q = &constructor_pending_elts;
5580 p = 0;
5582 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5584 while (*q != 0)
5586 p = *q;
5587 if (tree_int_cst_lt (purpose, p->purpose))
5588 q = &p->left;
5589 else if (tree_int_cst_lt (p->purpose, purpose))
5590 q = &p->right;
5591 else
5592 abort ();
5595 else
5597 while (*q != NULL)
5599 p = *q;
5600 if (tree_int_cst_lt (DECL_FIELD_BITPOS (purpose),
5601 DECL_FIELD_BITPOS (p->purpose)))
5602 q = &p->left;
5603 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (p->purpose),
5604 DECL_FIELD_BITPOS (purpose)))
5605 q = &p->right;
5606 else
5607 abort ();
5611 r = (struct init_node *) oballoc (sizeof (struct init_node));
5612 r->purpose = purpose;
5613 r->value = value;
5615 *q = r;
5616 r->parent = p;
5617 r->left = 0;
5618 r->right = 0;
5619 r->balance = 0;
5621 while (p)
5623 struct init_node *s;
5625 if (r == p->left)
5627 if (p->balance == 0)
5628 p->balance = -1;
5629 else if (p->balance < 0)
5631 if (r->balance < 0)
5633 /* L rotation. */
5634 p->left = r->right;
5635 if (p->left)
5636 p->left->parent = p;
5637 r->right = p;
5639 p->balance = 0;
5640 r->balance = 0;
5642 s = p->parent;
5643 p->parent = r;
5644 r->parent = s;
5645 if (s)
5647 if (s->left == p)
5648 s->left = r;
5649 else
5650 s->right = r;
5652 else
5653 constructor_pending_elts = r;
5655 else
5657 /* LR rotation. */
5658 struct init_node *t = r->right;
5660 r->right = t->left;
5661 if (r->right)
5662 r->right->parent = r;
5663 t->left = r;
5665 p->left = t->right;
5666 if (p->left)
5667 p->left->parent = p;
5668 t->right = p;
5670 p->balance = t->balance < 0;
5671 r->balance = -(t->balance > 0);
5672 t->balance = 0;
5674 s = p->parent;
5675 p->parent = t;
5676 r->parent = t;
5677 t->parent = s;
5678 if (s)
5680 if (s->left == p)
5681 s->left = t;
5682 else
5683 s->right = t;
5685 else
5686 constructor_pending_elts = t;
5688 break;
5690 else
5692 /* p->balance == +1; growth of left side balances the node. */
5693 p->balance = 0;
5694 break;
5697 else /* r == p->right */
5699 if (p->balance == 0)
5700 /* Growth propagation from right side. */
5701 p->balance++;
5702 else if (p->balance > 0)
5704 if (r->balance > 0)
5706 /* R rotation. */
5707 p->right = r->left;
5708 if (p->right)
5709 p->right->parent = p;
5710 r->left = p;
5712 p->balance = 0;
5713 r->balance = 0;
5715 s = p->parent;
5716 p->parent = r;
5717 r->parent = s;
5718 if (s)
5720 if (s->left == p)
5721 s->left = r;
5722 else
5723 s->right = r;
5725 else
5726 constructor_pending_elts = r;
5728 else /* r->balance == -1 */
5730 /* RL rotation */
5731 struct init_node *t = r->left;
5733 r->left = t->right;
5734 if (r->left)
5735 r->left->parent = r;
5736 t->right = r;
5738 p->right = t->left;
5739 if (p->right)
5740 p->right->parent = p;
5741 t->left = p;
5743 r->balance = (t->balance < 0);
5744 p->balance = -(t->balance > 0);
5745 t->balance = 0;
5747 s = p->parent;
5748 p->parent = t;
5749 r->parent = t;
5750 t->parent = s;
5751 if (s)
5753 if (s->left == p)
5754 s->left = t;
5755 else
5756 s->right = t;
5758 else
5759 constructor_pending_elts = t;
5761 break;
5763 else
5765 /* p->balance == -1; growth of right side balances the node. */
5766 p->balance = 0;
5767 break;
5771 r = p;
5772 p = p->parent;
5776 /* Return nonzero if FIELD is equal to the index of a pending initializer. */
5778 static int
5779 pending_init_member (field)
5780 tree field;
5782 struct init_node *p;
5784 p = constructor_pending_elts;
5785 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5787 while (p)
5789 if (tree_int_cst_equal (field, p->purpose))
5790 return 1;
5791 else if (tree_int_cst_lt (field, p->purpose))
5792 p = p->left;
5793 else
5794 p = p->right;
5797 else
5799 while (p)
5801 if (field == p->purpose)
5802 return 1;
5803 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (field),
5804 DECL_FIELD_BITPOS (p->purpose)))
5805 p = p->left;
5806 else
5807 p = p->right;
5811 return 0;
5814 /* "Output" the next constructor element.
5815 At top level, really output it to assembler code now.
5816 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5817 TYPE is the data type that the containing data type wants here.
5818 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5820 PENDING if non-nil means output pending elements that belong
5821 right after this element. (PENDING is normally 1;
5822 it is 0 while outputting pending elements, to avoid recursion.) */
5824 static void
5825 output_init_element (value, type, field, pending)
5826 tree value, type, field;
5827 int pending;
5829 int duplicate = 0;
5831 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5832 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5833 && !(TREE_CODE (value) == STRING_CST
5834 && TREE_CODE (type) == ARRAY_TYPE
5835 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5836 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5837 TYPE_MAIN_VARIANT (type))))
5838 value = default_conversion (value);
5840 if (value == error_mark_node)
5841 constructor_erroneous = 1;
5842 else if (!TREE_CONSTANT (value))
5843 constructor_constant = 0;
5844 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5845 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5846 || TREE_CODE (constructor_type) == UNION_TYPE)
5847 && DECL_C_BIT_FIELD (field)
5848 && TREE_CODE (value) != INTEGER_CST))
5849 constructor_simple = 0;
5851 if (require_constant_value && ! TREE_CONSTANT (value))
5853 error_init ("initializer element is not constant");
5854 value = error_mark_node;
5856 else if (require_constant_elements
5857 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5859 error_init ("initializer element is not computable at load time");
5860 value = error_mark_node;
5863 /* If this element duplicates one on constructor_pending_elts,
5864 print a message and ignore it. Don't do this when we're
5865 processing elements taken off constructor_pending_elts,
5866 because we'd always get spurious errors. */
5867 if (pending)
5869 if (TREE_CODE (constructor_type) == RECORD_TYPE
5870 || TREE_CODE (constructor_type) == UNION_TYPE
5871 || TREE_CODE (constructor_type) == ARRAY_TYPE)
5873 if (pending_init_member (field))
5875 error_init ("duplicate initializer");
5876 duplicate = 1;
5881 /* If this element doesn't come next in sequence,
5882 put it on constructor_pending_elts. */
5883 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5884 && !tree_int_cst_equal (field, constructor_unfilled_index))
5886 if (! duplicate)
5887 /* The copy_node is needed in case field is actually
5888 constructor_index, which is modified in place. */
5889 add_pending_init (copy_node (field),
5890 digest_init (type, value, require_constant_value,
5891 require_constant_elements));
5893 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5894 && field != constructor_unfilled_fields)
5896 /* We do this for records but not for unions. In a union,
5897 no matter which field is specified, it can be initialized
5898 right away since it starts at the beginning of the union. */
5899 if (!duplicate)
5900 add_pending_init (field,
5901 digest_init (type, value, require_constant_value,
5902 require_constant_elements));
5904 else
5906 /* Otherwise, output this element either to
5907 constructor_elements or to the assembler file. */
5909 if (!duplicate)
5911 if (! constructor_incremental)
5913 if (field && TREE_CODE (field) == INTEGER_CST)
5914 field = copy_node (field);
5915 constructor_elements
5916 = tree_cons (field, digest_init (type, value,
5917 require_constant_value,
5918 require_constant_elements),
5919 constructor_elements);
5921 else
5923 /* Structure elements may require alignment.
5924 Do this, if necessary. */
5925 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5927 /* Advance to offset of this element. */
5928 if (! tree_int_cst_equal (constructor_bit_index,
5929 DECL_FIELD_BITPOS (field)))
5931 /* By using unsigned arithmetic, the result will be
5932 correct even in case of overflows, if BITS_PER_UNIT
5933 is a power of two. */
5934 unsigned next = (TREE_INT_CST_LOW
5935 (DECL_FIELD_BITPOS (field))
5936 / (unsigned)BITS_PER_UNIT);
5937 unsigned here = (TREE_INT_CST_LOW
5938 (constructor_bit_index)
5939 / (unsigned)BITS_PER_UNIT);
5941 assemble_zeros ((next - here)
5942 * (unsigned)BITS_PER_UNIT
5943 / (unsigned)BITS_PER_UNIT);
5946 output_constant (digest_init (type, value,
5947 require_constant_value,
5948 require_constant_elements),
5949 int_size_in_bytes (type));
5951 /* For a record or union,
5952 keep track of end position of last field. */
5953 if (TREE_CODE (constructor_type) == RECORD_TYPE
5954 || TREE_CODE (constructor_type) == UNION_TYPE)
5956 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
5957 DECL_SIZE (field));
5958 TREE_INT_CST_LOW (constructor_bit_index)
5959 = TREE_INT_CST_LOW (temp);
5960 TREE_INT_CST_HIGH (constructor_bit_index)
5961 = TREE_INT_CST_HIGH (temp);
5966 /* Advance the variable that indicates sequential elements output. */
5967 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5969 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
5970 integer_one_node);
5971 TREE_INT_CST_LOW (constructor_unfilled_index)
5972 = TREE_INT_CST_LOW (tem);
5973 TREE_INT_CST_HIGH (constructor_unfilled_index)
5974 = TREE_INT_CST_HIGH (tem);
5976 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5978 constructor_unfilled_fields =
5979 TREE_CHAIN (constructor_unfilled_fields);
5980 /* Skip any nameless bit fields. */
5981 while (constructor_unfilled_fields != 0
5982 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5983 && DECL_NAME (constructor_unfilled_fields) == 0)
5984 constructor_unfilled_fields =
5985 TREE_CHAIN (constructor_unfilled_fields);
5987 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5988 constructor_unfilled_fields = 0;
5990 /* Now output any pending elements which have become next. */
5991 if (pending)
5992 output_pending_init_elements (0);
5996 /* Output any pending elements which have become next.
5997 As we output elements, constructor_unfilled_{fields,index}
5998 advances, which may cause other elements to become next;
5999 if so, they too are output.
6001 If ALL is 0, we return when there are
6002 no more pending elements to output now.
6004 If ALL is 1, we output space as necessary so that
6005 we can output all the pending elements. */
6007 static void
6008 output_pending_init_elements (all)
6009 int all;
6011 struct init_node *elt = constructor_pending_elts;
6012 tree next;
6014 retry:
6016 /* Look thru the whole pending tree.
6017 If we find an element that should be output now,
6018 output it. Otherwise, set NEXT to the element
6019 that comes first among those still pending. */
6021 next = 0;
6022 while (elt)
6024 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6026 if (tree_int_cst_equal (elt->purpose,
6027 constructor_unfilled_index))
6028 output_init_element (elt->value,
6029 TREE_TYPE (constructor_type),
6030 constructor_unfilled_index, 0);
6031 else if (tree_int_cst_lt (constructor_unfilled_index,
6032 elt->purpose))
6034 /* Advance to the next smaller node. */
6035 if (elt->left)
6036 elt = elt->left;
6037 else
6039 /* We have reached the smallest node bigger than the
6040 current unfilled index. Fill the space first. */
6041 next = elt->purpose;
6042 break;
6045 else
6047 /* Advance to the next bigger node. */
6048 if (elt->right)
6049 elt = elt->right;
6050 else
6052 /* We have reached the biggest node in a subtree. Find
6053 the parent of it, which is the next bigger node. */
6054 while (elt->parent && elt->parent->right == elt)
6055 elt = elt->parent;
6056 elt = elt->parent;
6057 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6058 elt->purpose))
6060 next = elt->purpose;
6061 break;
6066 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6067 || TREE_CODE (constructor_type) == UNION_TYPE)
6069 /* If the current record is complete we are done. */
6070 if (constructor_unfilled_fields == 0)
6071 break;
6072 if (elt->purpose == constructor_unfilled_fields)
6074 output_init_element (elt->value,
6075 TREE_TYPE (constructor_unfilled_fields),
6076 constructor_unfilled_fields,
6079 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6080 DECL_FIELD_BITPOS (elt->purpose)))
6082 /* Advance to the next smaller node. */
6083 if (elt->left)
6084 elt = elt->left;
6085 else
6087 /* We have reached the smallest node bigger than the
6088 current unfilled field. Fill the space first. */
6089 next = elt->purpose;
6090 break;
6093 else
6095 /* Advance to the next bigger node. */
6096 if (elt->right)
6097 elt = elt->right;
6098 else
6100 /* We have reached the biggest node in a subtree. Find
6101 the parent of it, which is the next bigger node. */
6102 while (elt->parent && elt->parent->right == elt)
6103 elt = elt->parent;
6104 elt = elt->parent;
6105 if (elt
6106 && tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6107 DECL_FIELD_BITPOS (elt->purpose)))
6109 next = elt->purpose;
6110 break;
6117 /* Ordinarily return, but not if we want to output all
6118 and there are elements left. */
6119 if (! (all && next != 0))
6120 return;
6122 /* Generate space up to the position of NEXT. */
6123 if (constructor_incremental)
6125 tree filled;
6126 tree nextpos_tree = size_int (0);
6128 if (TREE_CODE (constructor_type) == RECORD_TYPE
6129 || TREE_CODE (constructor_type) == UNION_TYPE)
6131 tree tail;
6132 /* Find the last field written out, if any. */
6133 for (tail = TYPE_FIELDS (constructor_type); tail;
6134 tail = TREE_CHAIN (tail))
6135 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
6136 break;
6138 if (tail)
6139 /* Find the offset of the end of that field. */
6140 filled = size_binop (CEIL_DIV_EXPR,
6141 size_binop (PLUS_EXPR,
6142 DECL_FIELD_BITPOS (tail),
6143 DECL_SIZE (tail)),
6144 size_int (BITS_PER_UNIT));
6145 else
6146 filled = size_int (0);
6148 nextpos_tree = size_binop (CEIL_DIV_EXPR,
6149 DECL_FIELD_BITPOS (next),
6150 size_int (BITS_PER_UNIT));
6152 TREE_INT_CST_HIGH (constructor_bit_index)
6153 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
6154 TREE_INT_CST_LOW (constructor_bit_index)
6155 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
6156 constructor_unfilled_fields = next;
6158 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6160 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
6161 size_in_bytes (TREE_TYPE (constructor_type)));
6162 nextpos_tree
6163 = size_binop (MULT_EXPR, next,
6164 size_in_bytes (TREE_TYPE (constructor_type)));
6165 TREE_INT_CST_LOW (constructor_unfilled_index)
6166 = TREE_INT_CST_LOW (next);
6167 TREE_INT_CST_HIGH (constructor_unfilled_index)
6168 = TREE_INT_CST_HIGH (next);
6170 else
6171 filled = 0;
6173 if (filled)
6175 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
6177 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
6180 else
6182 /* If it's not incremental, just skip over the gap,
6183 so that after jumping to retry we will output the next
6184 successive element. */
6185 if (TREE_CODE (constructor_type) == RECORD_TYPE
6186 || TREE_CODE (constructor_type) == UNION_TYPE)
6187 constructor_unfilled_fields = next;
6188 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6190 TREE_INT_CST_LOW (constructor_unfilled_index)
6191 = TREE_INT_CST_LOW (next);
6192 TREE_INT_CST_HIGH (constructor_unfilled_index)
6193 = TREE_INT_CST_HIGH (next);
6197 /* ELT now points to the node in the pending tree with the next
6198 initializer to output. */
6199 goto retry;
6202 /* Add one non-braced element to the current constructor level.
6203 This adjusts the current position within the constructor's type.
6204 This may also start or terminate implicit levels
6205 to handle a partly-braced initializer.
6207 Once this has found the correct level for the new element,
6208 it calls output_init_element.
6210 Note: if we are incrementally outputting this constructor,
6211 this function may be called with a null argument
6212 representing a sub-constructor that was already incrementally output.
6213 When that happens, we output nothing, but we do the bookkeeping
6214 to skip past that element of the current constructor. */
6216 void
6217 process_init_element (value)
6218 tree value;
6220 tree orig_value = value;
6221 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6223 /* Handle superfluous braces around string cst as in
6224 char x[] = {"foo"}; */
6225 if (string_flag
6226 && constructor_type
6227 && TREE_CODE (constructor_type) == ARRAY_TYPE
6228 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6229 && integer_zerop (constructor_unfilled_index))
6231 if (constructor_stack->replacement_value)
6232 error_init ("excess elements in char array initializer");
6233 constructor_stack->replacement_value = value;
6234 return;
6237 if (constructor_stack->replacement_value != 0)
6239 error_init ("excess elements in struct initializer");
6240 return;
6243 /* Ignore elements of a brace group if it is entirely superfluous
6244 and has already been diagnosed. */
6245 if (constructor_type == 0)
6246 return;
6248 /* If we've exhausted any levels that didn't have braces,
6249 pop them now. */
6250 while (constructor_stack->implicit)
6252 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6253 || TREE_CODE (constructor_type) == UNION_TYPE)
6254 && constructor_fields == 0)
6255 process_init_element (pop_init_level (1));
6256 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6257 && (constructor_max_index == 0
6258 || tree_int_cst_lt (constructor_max_index,
6259 constructor_index)))
6260 process_init_element (pop_init_level (1));
6261 else
6262 break;
6265 while (1)
6267 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6269 tree fieldtype;
6270 enum tree_code fieldcode;
6272 if (constructor_fields == 0)
6274 pedwarn_init ("excess elements in struct initializer");
6275 break;
6278 fieldtype = TREE_TYPE (constructor_fields);
6279 if (fieldtype != error_mark_node)
6280 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6281 fieldcode = TREE_CODE (fieldtype);
6283 /* Accept a string constant to initialize a subarray. */
6284 if (value != 0
6285 && fieldcode == ARRAY_TYPE
6286 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6287 && string_flag)
6288 value = orig_value;
6289 /* Otherwise, if we have come to a subaggregate,
6290 and we don't have an element of its type, push into it. */
6291 else if (value != 0 && !constructor_no_implicit
6292 && value != error_mark_node
6293 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6294 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6295 || fieldcode == UNION_TYPE))
6297 push_init_level (1);
6298 continue;
6301 if (value)
6303 push_member_name (constructor_fields);
6304 output_init_element (value, fieldtype, constructor_fields, 1);
6305 RESTORE_SPELLING_DEPTH (constructor_depth);
6307 else
6308 /* Do the bookkeeping for an element that was
6309 directly output as a constructor. */
6311 /* For a record, keep track of end position of last field. */
6312 tree temp = size_binop (PLUS_EXPR,
6313 DECL_FIELD_BITPOS (constructor_fields),
6314 DECL_SIZE (constructor_fields));
6315 TREE_INT_CST_LOW (constructor_bit_index)
6316 = TREE_INT_CST_LOW (temp);
6317 TREE_INT_CST_HIGH (constructor_bit_index)
6318 = TREE_INT_CST_HIGH (temp);
6320 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6321 /* Skip any nameless bit fields. */
6322 while (constructor_unfilled_fields != 0
6323 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6324 && DECL_NAME (constructor_unfilled_fields) == 0)
6325 constructor_unfilled_fields =
6326 TREE_CHAIN (constructor_unfilled_fields);
6329 constructor_fields = TREE_CHAIN (constructor_fields);
6330 /* Skip any nameless bit fields at the beginning. */
6331 while (constructor_fields != 0
6332 && DECL_C_BIT_FIELD (constructor_fields)
6333 && DECL_NAME (constructor_fields) == 0)
6334 constructor_fields = TREE_CHAIN (constructor_fields);
6335 break;
6337 if (TREE_CODE (constructor_type) == UNION_TYPE)
6339 tree fieldtype;
6340 enum tree_code fieldcode;
6342 if (constructor_fields == 0)
6344 pedwarn_init ("excess elements in union initializer");
6345 break;
6348 fieldtype = TREE_TYPE (constructor_fields);
6349 if (fieldtype != error_mark_node)
6350 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6351 fieldcode = TREE_CODE (fieldtype);
6353 /* Accept a string constant to initialize a subarray. */
6354 if (value != 0
6355 && fieldcode == ARRAY_TYPE
6356 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6357 && string_flag)
6358 value = orig_value;
6359 /* Otherwise, if we have come to a subaggregate,
6360 and we don't have an element of its type, push into it. */
6361 else if (value != 0 && !constructor_no_implicit
6362 && value != error_mark_node
6363 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6364 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6365 || fieldcode == UNION_TYPE))
6367 push_init_level (1);
6368 continue;
6371 if (value)
6373 push_member_name (constructor_fields);
6374 output_init_element (value, fieldtype, constructor_fields, 1);
6375 RESTORE_SPELLING_DEPTH (constructor_depth);
6377 else
6378 /* Do the bookkeeping for an element that was
6379 directly output as a constructor. */
6381 TREE_INT_CST_LOW (constructor_bit_index)
6382 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6383 TREE_INT_CST_HIGH (constructor_bit_index)
6384 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6386 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6389 constructor_fields = 0;
6390 break;
6392 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6394 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6395 enum tree_code eltcode = TREE_CODE (elttype);
6397 /* Accept a string constant to initialize a subarray. */
6398 if (value != 0
6399 && eltcode == ARRAY_TYPE
6400 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6401 && string_flag)
6402 value = orig_value;
6403 /* Otherwise, if we have come to a subaggregate,
6404 and we don't have an element of its type, push into it. */
6405 else if (value != 0 && !constructor_no_implicit
6406 && value != error_mark_node
6407 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6408 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6409 || eltcode == UNION_TYPE))
6411 push_init_level (1);
6412 continue;
6415 if (constructor_max_index != 0
6416 && tree_int_cst_lt (constructor_max_index, constructor_index))
6418 pedwarn_init ("excess elements in array initializer");
6419 break;
6422 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */
6423 if (constructor_range_end)
6425 if (constructor_max_index != 0
6426 && tree_int_cst_lt (constructor_max_index,
6427 constructor_range_end))
6429 pedwarn_init ("excess elements in array initializer");
6430 TREE_INT_CST_HIGH (constructor_range_end)
6431 = TREE_INT_CST_HIGH (constructor_max_index);
6432 TREE_INT_CST_LOW (constructor_range_end)
6433 = TREE_INT_CST_LOW (constructor_max_index);
6436 value = save_expr (value);
6439 /* Now output the actual element.
6440 Ordinarily, output once.
6441 If there is a range, repeat it till we advance past the range. */
6444 tree tem;
6446 if (value)
6448 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6449 output_init_element (value, elttype, constructor_index, 1);
6450 RESTORE_SPELLING_DEPTH (constructor_depth);
6453 tem = size_binop (PLUS_EXPR, constructor_index,
6454 integer_one_node);
6455 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (tem);
6456 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (tem);
6458 if (!value)
6459 /* If we are doing the bookkeeping for an element that was
6460 directly output as a constructor,
6461 we must update constructor_unfilled_index. */
6463 TREE_INT_CST_LOW (constructor_unfilled_index)
6464 = TREE_INT_CST_LOW (constructor_index);
6465 TREE_INT_CST_HIGH (constructor_unfilled_index)
6466 = TREE_INT_CST_HIGH (constructor_index);
6469 while (! (constructor_range_end == 0
6470 || tree_int_cst_lt (constructor_range_end,
6471 constructor_index)));
6473 break;
6476 /* Handle the sole element allowed in a braced initializer
6477 for a scalar variable. */
6478 if (constructor_fields == 0)
6480 pedwarn_init ("excess elements in scalar initializer");
6481 break;
6484 if (value)
6485 output_init_element (value, constructor_type, NULL_TREE, 1);
6486 constructor_fields = 0;
6487 break;
6490 /* If the (lexically) previous elments are not now saved,
6491 we can discard the storage for them. */
6492 if (constructor_incremental && constructor_pending_elts == 0 && value != 0
6493 && constructor_stack == 0)
6494 clear_momentary ();
6497 /* Expand an ASM statement with operands, handling output operands
6498 that are not variables or INDIRECT_REFS by transforming such
6499 cases into cases that expand_asm_operands can handle.
6501 Arguments are same as for expand_asm_operands. */
6503 void
6504 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6505 tree string, outputs, inputs, clobbers;
6506 int vol;
6507 char *filename;
6508 int line;
6510 int noutputs = list_length (outputs);
6511 register int i;
6512 /* o[I] is the place that output number I should be written. */
6513 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6514 register tree tail;
6516 if (TREE_CODE (string) == ADDR_EXPR)
6517 string = TREE_OPERAND (string, 0);
6518 if (TREE_CODE (string) != STRING_CST)
6520 error ("asm template is not a string constant");
6521 return;
6524 /* Record the contents of OUTPUTS before it is modified. */
6525 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6527 tree output = TREE_VALUE (tail);
6529 /* We can remove conversions that just change the type, not the mode. */
6530 STRIP_NOPS (output);
6531 o[i] = output;
6533 /* Allow conversions as LHS here. build_modify_expr as called below
6534 will do the right thing with them. */
6535 while (TREE_CODE (output) == NOP_EXPR
6536 || TREE_CODE (output) == CONVERT_EXPR
6537 || TREE_CODE (output) == FLOAT_EXPR
6538 || TREE_CODE (output) == FIX_TRUNC_EXPR
6539 || TREE_CODE (output) == FIX_FLOOR_EXPR
6540 || TREE_CODE (output) == FIX_ROUND_EXPR
6541 || TREE_CODE (output) == FIX_CEIL_EXPR)
6542 output = TREE_OPERAND (output, 0);
6544 lvalue_or_else (o[i], "invalid lvalue in asm statement");
6547 /* Perform default conversions on array and function inputs. */
6548 /* Don't do this for other types--
6549 it would screw up operands expected to be in memory. */
6550 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6551 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6552 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6553 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6555 /* Generate the ASM_OPERANDS insn;
6556 store into the TREE_VALUEs of OUTPUTS some trees for
6557 where the values were actually stored. */
6558 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6560 /* Copy all the intermediate outputs into the specified outputs. */
6561 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6563 if (o[i] != TREE_VALUE (tail))
6565 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6566 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6567 free_temp_slots ();
6569 /* Detect modification of read-only values.
6570 (Otherwise done by build_modify_expr.) */
6571 else
6573 tree type = TREE_TYPE (o[i]);
6574 if (TREE_READONLY (o[i])
6575 || TYPE_READONLY (type)
6576 || ((TREE_CODE (type) == RECORD_TYPE
6577 || TREE_CODE (type) == UNION_TYPE)
6578 && C_TYPE_FIELDS_READONLY (type)))
6579 readonly_warning (o[i], "modification by `asm'");
6583 /* Those MODIFY_EXPRs could do autoincrements. */
6584 emit_queue ();
6587 /* Expand a C `return' statement.
6588 RETVAL is the expression for what to return,
6589 or a null pointer for `return;' with no value. */
6591 void
6592 c_expand_return (retval)
6593 tree retval;
6595 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6597 if (TREE_THIS_VOLATILE (current_function_decl))
6598 warning ("function declared `noreturn' has a `return' statement");
6600 if (!retval)
6602 current_function_returns_null = 1;
6603 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6604 warning ("`return' with no value, in function returning non-void");
6605 expand_null_return ();
6607 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6609 current_function_returns_null = 1;
6610 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6611 pedwarn ("`return' with a value, in function returning void");
6612 expand_return (retval);
6614 else
6616 tree t = convert_for_assignment (valtype, retval, _("return"),
6617 NULL_TREE, NULL_TREE, 0);
6618 tree res = DECL_RESULT (current_function_decl);
6619 tree inner;
6621 if (t == error_mark_node)
6622 return;
6624 inner = t = convert (TREE_TYPE (res), t);
6626 /* Strip any conversions, additions, and subtractions, and see if
6627 we are returning the address of a local variable. Warn if so. */
6628 while (1)
6630 switch (TREE_CODE (inner))
6632 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6633 case PLUS_EXPR:
6634 inner = TREE_OPERAND (inner, 0);
6635 continue;
6637 case MINUS_EXPR:
6638 /* If the second operand of the MINUS_EXPR has a pointer
6639 type (or is converted from it), this may be valid, so
6640 don't give a warning. */
6642 tree op1 = TREE_OPERAND (inner, 1);
6644 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6645 && (TREE_CODE (op1) == NOP_EXPR
6646 || TREE_CODE (op1) == NON_LVALUE_EXPR
6647 || TREE_CODE (op1) == CONVERT_EXPR))
6648 op1 = TREE_OPERAND (op1, 0);
6650 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6651 break;
6653 inner = TREE_OPERAND (inner, 0);
6654 continue;
6657 case ADDR_EXPR:
6658 inner = TREE_OPERAND (inner, 0);
6660 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6661 inner = TREE_OPERAND (inner, 0);
6663 if (TREE_CODE (inner) == VAR_DECL
6664 && ! DECL_EXTERNAL (inner)
6665 && ! TREE_STATIC (inner)
6666 && DECL_CONTEXT (inner) == current_function_decl)
6667 warning ("function returns address of local variable");
6668 break;
6670 default:
6671 break;
6674 break;
6677 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6678 TREE_SIDE_EFFECTS (t) = 1;
6679 expand_return (t);
6680 current_function_returns_value = 1;
6684 /* Start a C switch statement, testing expression EXP.
6685 Return EXP if it is valid, an error node otherwise. */
6687 tree
6688 c_expand_start_case (exp)
6689 tree exp;
6691 register enum tree_code code;
6692 tree type;
6694 if (TREE_CODE (exp) == ERROR_MARK)
6695 return exp;
6697 code = TREE_CODE (TREE_TYPE (exp));
6698 type = TREE_TYPE (exp);
6700 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6702 error ("switch quantity not an integer");
6703 exp = error_mark_node;
6705 else
6707 tree index;
6708 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6710 if (warn_traditional
6711 && (type == long_integer_type_node
6712 || type == long_unsigned_type_node))
6713 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6715 exp = default_conversion (exp);
6716 type = TREE_TYPE (exp);
6717 index = get_unwidened (exp, NULL_TREE);
6718 /* We can't strip a conversion from a signed type to an unsigned,
6719 because if we did, int_fits_type_p would do the wrong thing
6720 when checking case values for being in range,
6721 and it's too hard to do the right thing. */
6722 if (TREE_UNSIGNED (TREE_TYPE (exp))
6723 == TREE_UNSIGNED (TREE_TYPE (index)))
6724 exp = index;
6727 expand_start_case (1, exp, type, "switch statement");
6729 return exp;