Add FR30 to list of targets for which -fpic is inappropriate.
[official-gcc.git] / gcc / c-typeck.c
blobde982588d133231f77470e126969429119e91689
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 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1467 case BUILT_IN_ABS:
1468 case BUILT_IN_LABS:
1469 case BUILT_IN_FABS:
1470 if (coerced_params == 0)
1471 return integer_zero_node;
1472 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1473 default:
1474 break;
1478 register tree result
1479 = build (CALL_EXPR, TREE_TYPE (fntype),
1480 function, coerced_params, NULL_TREE);
1482 TREE_SIDE_EFFECTS (result) = 1;
1483 if (TREE_TYPE (result) == void_type_node)
1484 return result;
1485 return require_complete_type (result);
1489 /* Convert the argument expressions in the list VALUES
1490 to the types in the list TYPELIST. The result is a list of converted
1491 argument expressions.
1493 If TYPELIST is exhausted, or when an element has NULL as its type,
1494 perform the default conversions.
1496 PARMLIST is the chain of parm decls for the function being called.
1497 It may be 0, if that info is not available.
1498 It is used only for generating error messages.
1500 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1502 This is also where warnings about wrong number of args are generated.
1504 Both VALUES and the returned value are chains of TREE_LIST nodes
1505 with the elements of the list in the TREE_VALUE slots of those nodes. */
1507 static tree
1508 convert_arguments (typelist, values, name, fundecl)
1509 tree typelist, values, name, fundecl;
1511 register tree typetail, valtail;
1512 register tree result = NULL;
1513 int parmnum;
1515 /* Scan the given expressions and types, producing individual
1516 converted arguments and pushing them on RESULT in reverse order. */
1518 for (valtail = values, typetail = typelist, parmnum = 0;
1519 valtail;
1520 valtail = TREE_CHAIN (valtail), parmnum++)
1522 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1523 register tree val = TREE_VALUE (valtail);
1525 if (type == void_type_node)
1527 if (name)
1528 error ("too many arguments to function `%s'",
1529 IDENTIFIER_POINTER (name));
1530 else
1531 error ("too many arguments to function");
1532 break;
1535 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1536 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1537 to convert automatically to a pointer. */
1538 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1539 val = TREE_OPERAND (val, 0);
1541 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1542 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1543 val = default_conversion (val);
1545 val = require_complete_type (val);
1547 if (type != 0)
1549 /* Formal parm type is specified by a function prototype. */
1550 tree parmval;
1552 if (TYPE_SIZE (type) == 0)
1554 error ("type of formal parameter %d is incomplete", parmnum + 1);
1555 parmval = val;
1557 else
1559 /* Optionally warn about conversions that
1560 differ from the default conversions. */
1561 if (warn_conversion)
1563 int formal_prec = TYPE_PRECISION (type);
1565 if (INTEGRAL_TYPE_P (type)
1566 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1567 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1568 else if (TREE_CODE (type) == COMPLEX_TYPE
1569 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1570 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1571 else if (TREE_CODE (type) == REAL_TYPE
1572 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1573 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1574 else if (TREE_CODE (type) == REAL_TYPE
1575 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1576 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1577 /* ??? At some point, messages should be written about
1578 conversions between complex types, but that's too messy
1579 to do now. */
1580 else if (TREE_CODE (type) == REAL_TYPE
1581 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1583 /* Warn if any argument is passed as `float',
1584 since without a prototype it would be `double'. */
1585 if (formal_prec == TYPE_PRECISION (float_type_node))
1586 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1588 /* Detect integer changing in width or signedness. */
1589 else if (INTEGRAL_TYPE_P (type)
1590 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1592 tree would_have_been = default_conversion (val);
1593 tree type1 = TREE_TYPE (would_have_been);
1595 if (TREE_CODE (type) == ENUMERAL_TYPE
1596 && type == TREE_TYPE (val))
1597 /* No warning if function asks for enum
1598 and the actual arg is that enum type. */
1600 else if (formal_prec != TYPE_PRECISION (type1))
1601 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1602 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1604 /* Don't complain if the formal parameter type
1605 is an enum, because we can't tell now whether
1606 the value was an enum--even the same enum. */
1607 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1609 else if (TREE_CODE (val) == INTEGER_CST
1610 && int_fits_type_p (val, type))
1611 /* Change in signedness doesn't matter
1612 if a constant value is unaffected. */
1614 /* Likewise for a constant in a NOP_EXPR. */
1615 else if (TREE_CODE (val) == NOP_EXPR
1616 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1617 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1619 #if 0 /* We never get such tree structure here. */
1620 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1621 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1622 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1623 /* Change in signedness doesn't matter
1624 if an enum value is unaffected. */
1626 #endif
1627 /* If the value is extended from a narrower
1628 unsigned type, it doesn't matter whether we
1629 pass it as signed or unsigned; the value
1630 certainly is the same either way. */
1631 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1632 && TREE_UNSIGNED (TREE_TYPE (val)))
1634 else if (TREE_UNSIGNED (type))
1635 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1636 else
1637 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1641 parmval = convert_for_assignment (type, val,
1642 (char *) 0, /* arg passing */
1643 fundecl, name, parmnum + 1);
1645 if (PROMOTE_PROTOTYPES
1646 && (TREE_CODE (type) == INTEGER_TYPE
1647 || TREE_CODE (type) == ENUMERAL_TYPE)
1648 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1649 parmval = default_conversion (parmval);
1651 result = tree_cons (NULL_TREE, parmval, result);
1653 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1654 && (TYPE_PRECISION (TREE_TYPE (val))
1655 < TYPE_PRECISION (double_type_node)))
1656 /* Convert `float' to `double'. */
1657 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1658 else
1659 /* Convert `short' and `char' to full-size `int'. */
1660 result = tree_cons (NULL_TREE, default_conversion (val), result);
1662 if (typetail)
1663 typetail = TREE_CHAIN (typetail);
1666 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1668 if (name)
1669 error ("too few arguments to function `%s'",
1670 IDENTIFIER_POINTER (name));
1671 else
1672 error ("too few arguments to function");
1675 return nreverse (result);
1678 /* This is the entry point used by the parser
1679 for binary operators in the input.
1680 In addition to constructing the expression,
1681 we check for operands that were written with other binary operators
1682 in a way that is likely to confuse the user. */
1684 tree
1685 parser_build_binary_op (code, arg1, arg2)
1686 enum tree_code code;
1687 tree arg1, arg2;
1689 tree result = build_binary_op (code, arg1, arg2, 1);
1691 char class;
1692 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1693 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1694 enum tree_code code1 = ERROR_MARK;
1695 enum tree_code code2 = ERROR_MARK;
1697 if (class1 == 'e' || class1 == '1'
1698 || class1 == '2' || class1 == '<')
1699 code1 = C_EXP_ORIGINAL_CODE (arg1);
1700 if (class2 == 'e' || class2 == '1'
1701 || class2 == '2' || class2 == '<')
1702 code2 = C_EXP_ORIGINAL_CODE (arg2);
1704 /* Check for cases such as x+y<<z which users are likely
1705 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1706 is cleared to prevent these warnings. */
1707 if (warn_parentheses)
1709 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1711 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1712 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1713 warning ("suggest parentheses around + or - inside shift");
1716 if (code == TRUTH_ORIF_EXPR)
1718 if (code1 == TRUTH_ANDIF_EXPR
1719 || code2 == TRUTH_ANDIF_EXPR)
1720 warning ("suggest parentheses around && within ||");
1723 if (code == BIT_IOR_EXPR)
1725 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1726 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1727 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1728 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1729 warning ("suggest parentheses around arithmetic in operand of |");
1730 /* Check cases like x|y==z */
1731 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1732 warning ("suggest parentheses around comparison in operand of |");
1735 if (code == BIT_XOR_EXPR)
1737 if (code1 == BIT_AND_EXPR
1738 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1739 || code2 == BIT_AND_EXPR
1740 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1741 warning ("suggest parentheses around arithmetic in operand of ^");
1742 /* Check cases like x^y==z */
1743 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1744 warning ("suggest parentheses around comparison in operand of ^");
1747 if (code == BIT_AND_EXPR)
1749 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1750 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1751 warning ("suggest parentheses around + or - in operand of &");
1752 /* Check cases like x&y==z */
1753 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1754 warning ("suggest parentheses around comparison in operand of &");
1758 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1759 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1760 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1761 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1763 unsigned_conversion_warning (result, arg1);
1764 unsigned_conversion_warning (result, arg2);
1765 overflow_warning (result);
1767 class = TREE_CODE_CLASS (TREE_CODE (result));
1769 /* Record the code that was specified in the source,
1770 for the sake of warnings about confusing nesting. */
1771 if (class == 'e' || class == '1'
1772 || class == '2' || class == '<')
1773 C_SET_EXP_ORIGINAL_CODE (result, code);
1774 else
1776 int flag = TREE_CONSTANT (result);
1777 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1778 so that convert_for_assignment wouldn't strip it.
1779 That way, we got warnings for things like p = (1 - 1).
1780 But it turns out we should not get those warnings. */
1781 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1782 C_SET_EXP_ORIGINAL_CODE (result, code);
1783 TREE_CONSTANT (result) = flag;
1786 return result;
1789 /* Build a binary-operation expression without default conversions.
1790 CODE is the kind of expression to build.
1791 This function differs from `build' in several ways:
1792 the data type of the result is computed and recorded in it,
1793 warnings are generated if arg data types are invalid,
1794 special handling for addition and subtraction of pointers is known,
1795 and some optimization is done (operations on narrow ints
1796 are done in the narrower type when that gives the same result).
1797 Constant folding is also done before the result is returned.
1799 Note that the operands will never have enumeral types, or function
1800 or array types, because either they will have the default conversions
1801 performed or they have both just been converted to some other type in which
1802 the arithmetic is to be done. */
1804 tree
1805 build_binary_op (code, orig_op0, orig_op1, convert_p)
1806 enum tree_code code;
1807 tree orig_op0, orig_op1;
1808 int convert_p;
1810 tree type0, type1;
1811 register enum tree_code code0, code1;
1812 tree op0, op1;
1814 /* Expression code to give to the expression when it is built.
1815 Normally this is CODE, which is what the caller asked for,
1816 but in some special cases we change it. */
1817 register enum tree_code resultcode = code;
1819 /* Data type in which the computation is to be performed.
1820 In the simplest cases this is the common type of the arguments. */
1821 register tree result_type = NULL;
1823 /* Nonzero means operands have already been type-converted
1824 in whatever way is necessary.
1825 Zero means they need to be converted to RESULT_TYPE. */
1826 int converted = 0;
1828 /* Nonzero means create the expression with this type, rather than
1829 RESULT_TYPE. */
1830 tree build_type = 0;
1832 /* Nonzero means after finally constructing the expression
1833 convert it to this type. */
1834 tree final_type = 0;
1836 /* Nonzero if this is an operation like MIN or MAX which can
1837 safely be computed in short if both args are promoted shorts.
1838 Also implies COMMON.
1839 -1 indicates a bitwise operation; this makes a difference
1840 in the exact conditions for when it is safe to do the operation
1841 in a narrower mode. */
1842 int shorten = 0;
1844 /* Nonzero if this is a comparison operation;
1845 if both args are promoted shorts, compare the original shorts.
1846 Also implies COMMON. */
1847 int short_compare = 0;
1849 /* Nonzero if this is a right-shift operation, which can be computed on the
1850 original short and then promoted if the operand is a promoted short. */
1851 int short_shift = 0;
1853 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1854 int common = 0;
1856 if (convert_p)
1858 op0 = default_conversion (orig_op0);
1859 op1 = default_conversion (orig_op1);
1861 else
1863 op0 = orig_op0;
1864 op1 = orig_op1;
1867 type0 = TREE_TYPE (op0);
1868 type1 = TREE_TYPE (op1);
1870 /* The expression codes of the data types of the arguments tell us
1871 whether the arguments are integers, floating, pointers, etc. */
1872 code0 = TREE_CODE (type0);
1873 code1 = TREE_CODE (type1);
1875 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1876 STRIP_TYPE_NOPS (op0);
1877 STRIP_TYPE_NOPS (op1);
1879 /* If an error was already reported for one of the arguments,
1880 avoid reporting another error. */
1882 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1883 return error_mark_node;
1885 switch (code)
1887 case PLUS_EXPR:
1888 /* Handle the pointer + int case. */
1889 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1890 return pointer_int_sum (PLUS_EXPR, op0, op1);
1891 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1892 return pointer_int_sum (PLUS_EXPR, op1, op0);
1893 else
1894 common = 1;
1895 break;
1897 case MINUS_EXPR:
1898 /* Subtraction of two similar pointers.
1899 We must subtract them as integers, then divide by object size. */
1900 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1901 && comp_target_types (type0, type1))
1902 return pointer_diff (op0, op1);
1903 /* Handle pointer minus int. Just like pointer plus int. */
1904 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1905 return pointer_int_sum (MINUS_EXPR, op0, op1);
1906 else
1907 common = 1;
1908 break;
1910 case MULT_EXPR:
1911 common = 1;
1912 break;
1914 case TRUNC_DIV_EXPR:
1915 case CEIL_DIV_EXPR:
1916 case FLOOR_DIV_EXPR:
1917 case ROUND_DIV_EXPR:
1918 case EXACT_DIV_EXPR:
1919 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1920 || code0 == COMPLEX_TYPE)
1921 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1922 || code1 == COMPLEX_TYPE))
1924 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
1925 resultcode = RDIV_EXPR;
1926 else
1928 /* Although it would be tempting to shorten always here, that
1929 loses on some targets, since the modulo instruction is
1930 undefined if the quotient can't be represented in the
1931 computation mode. We shorten only if unsigned or if
1932 dividing by something we know != -1. */
1933 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
1934 || (TREE_CODE (op1) == INTEGER_CST
1935 && (TREE_INT_CST_LOW (op1) != -1
1936 || TREE_INT_CST_HIGH (op1) != -1)));
1938 common = 1;
1940 break;
1942 case BIT_AND_EXPR:
1943 case BIT_ANDTC_EXPR:
1944 case BIT_IOR_EXPR:
1945 case BIT_XOR_EXPR:
1946 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1947 shorten = -1;
1948 /* If one operand is a constant, and the other is a short type
1949 that has been converted to an int,
1950 really do the work in the short type and then convert the
1951 result to int. If we are lucky, the constant will be 0 or 1
1952 in the short type, making the entire operation go away. */
1953 if (TREE_CODE (op0) == INTEGER_CST
1954 && TREE_CODE (op1) == NOP_EXPR
1955 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
1956 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
1958 final_type = result_type;
1959 op1 = TREE_OPERAND (op1, 0);
1960 result_type = TREE_TYPE (op1);
1962 if (TREE_CODE (op1) == INTEGER_CST
1963 && TREE_CODE (op0) == NOP_EXPR
1964 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
1965 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
1967 final_type = result_type;
1968 op0 = TREE_OPERAND (op0, 0);
1969 result_type = TREE_TYPE (op0);
1971 break;
1973 case TRUNC_MOD_EXPR:
1974 case FLOOR_MOD_EXPR:
1975 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1977 /* Although it would be tempting to shorten always here, that loses
1978 on some targets, since the modulo instruction is undefined if the
1979 quotient can't be represented in the computation mode. We shorten
1980 only if unsigned or if dividing by something we know != -1. */
1981 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
1982 || (TREE_CODE (op1) == INTEGER_CST
1983 && (TREE_INT_CST_LOW (op1) != -1
1984 || TREE_INT_CST_HIGH (op1) != -1)));
1985 common = 1;
1987 break;
1989 case TRUTH_ANDIF_EXPR:
1990 case TRUTH_ORIF_EXPR:
1991 case TRUTH_AND_EXPR:
1992 case TRUTH_OR_EXPR:
1993 case TRUTH_XOR_EXPR:
1994 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
1995 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
1996 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
1997 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
1999 /* Result of these operations is always an int,
2000 but that does not mean the operands should be
2001 converted to ints! */
2002 result_type = integer_type_node;
2003 op0 = truthvalue_conversion (op0);
2004 op1 = truthvalue_conversion (op1);
2005 converted = 1;
2007 break;
2009 /* Shift operations: result has same type as first operand;
2010 always convert second operand to int.
2011 Also set SHORT_SHIFT if shifting rightward. */
2013 case RSHIFT_EXPR:
2014 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2016 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2018 if (tree_int_cst_sgn (op1) < 0)
2019 warning ("right shift count is negative");
2020 else
2022 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2023 short_shift = 1;
2024 if (TREE_INT_CST_HIGH (op1) != 0
2025 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2026 >= TYPE_PRECISION (type0)))
2027 warning ("right shift count >= width of type");
2030 /* Use the type of the value to be shifted.
2031 This is what most traditional C compilers do. */
2032 result_type = type0;
2033 /* Unless traditional, convert the shift-count to an integer,
2034 regardless of size of value being shifted. */
2035 if (! flag_traditional)
2037 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2038 op1 = convert (integer_type_node, op1);
2039 /* Avoid converting op1 to result_type later. */
2040 converted = 1;
2043 break;
2045 case LSHIFT_EXPR:
2046 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2048 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2050 if (tree_int_cst_sgn (op1) < 0)
2051 warning ("left shift count is negative");
2052 else if (TREE_INT_CST_HIGH (op1) != 0
2053 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2054 >= TYPE_PRECISION (type0)))
2055 warning ("left shift count >= width of type");
2057 /* Use the type of the value to be shifted.
2058 This is what most traditional C compilers do. */
2059 result_type = type0;
2060 /* Unless traditional, convert the shift-count to an integer,
2061 regardless of size of value being shifted. */
2062 if (! flag_traditional)
2064 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2065 op1 = convert (integer_type_node, op1);
2066 /* Avoid converting op1 to result_type later. */
2067 converted = 1;
2070 break;
2072 case RROTATE_EXPR:
2073 case LROTATE_EXPR:
2074 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2076 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2078 if (tree_int_cst_sgn (op1) < 0)
2079 warning ("shift count is negative");
2080 else if (TREE_INT_CST_HIGH (op1) != 0
2081 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2082 >= TYPE_PRECISION (type0)))
2083 warning ("shift count >= width of type");
2085 /* Use the type of the value to be shifted.
2086 This is what most traditional C compilers do. */
2087 result_type = type0;
2088 /* Unless traditional, convert the shift-count to an integer,
2089 regardless of size of value being shifted. */
2090 if (! flag_traditional)
2092 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2093 op1 = convert (integer_type_node, op1);
2094 /* Avoid converting op1 to result_type later. */
2095 converted = 1;
2098 break;
2100 case EQ_EXPR:
2101 case NE_EXPR:
2102 /* Result of comparison is always int,
2103 but don't convert the args to int! */
2104 build_type = integer_type_node;
2105 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2106 || code0 == COMPLEX_TYPE)
2107 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2108 || code1 == COMPLEX_TYPE))
2109 short_compare = 1;
2110 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2112 register tree tt0 = TREE_TYPE (type0);
2113 register tree tt1 = TREE_TYPE (type1);
2114 /* Anything compares with void *. void * compares with anything.
2115 Otherwise, the targets must be compatible
2116 and both must be object or both incomplete. */
2117 if (comp_target_types (type0, type1))
2118 result_type = common_type (type0, type1);
2119 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2121 /* op0 != orig_op0 detects the case of something
2122 whose value is 0 but which isn't a valid null ptr const. */
2123 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2124 && TREE_CODE (tt1) == FUNCTION_TYPE)
2125 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2127 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2129 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2130 && TREE_CODE (tt0) == FUNCTION_TYPE)
2131 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2133 else
2134 pedwarn ("comparison of distinct pointer types lacks a cast");
2136 if (result_type == NULL_TREE)
2137 result_type = ptr_type_node;
2139 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2140 && integer_zerop (op1))
2141 result_type = type0;
2142 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2143 && integer_zerop (op0))
2144 result_type = type1;
2145 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2147 result_type = type0;
2148 if (! flag_traditional)
2149 pedwarn ("comparison between pointer and integer");
2151 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2153 result_type = type1;
2154 if (! flag_traditional)
2155 pedwarn ("comparison between pointer and integer");
2157 break;
2159 case MAX_EXPR:
2160 case MIN_EXPR:
2161 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2162 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2163 shorten = 1;
2164 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2166 if (comp_target_types (type0, type1))
2168 result_type = common_type (type0, type1);
2169 if (pedantic
2170 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2171 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2173 else
2175 result_type = ptr_type_node;
2176 pedwarn ("comparison of distinct pointer types lacks a cast");
2179 break;
2181 case LE_EXPR:
2182 case GE_EXPR:
2183 case LT_EXPR:
2184 case GT_EXPR:
2185 build_type = integer_type_node;
2186 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2187 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2188 short_compare = 1;
2189 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2191 if (comp_target_types (type0, type1))
2193 result_type = common_type (type0, type1);
2194 if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2195 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2196 pedwarn ("comparison of complete and incomplete pointers");
2197 else if (pedantic
2198 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2199 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2201 else
2203 result_type = ptr_type_node;
2204 pedwarn ("comparison of distinct pointer types lacks a cast");
2207 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2208 && integer_zerop (op1))
2210 result_type = type0;
2211 if (pedantic || extra_warnings)
2212 pedwarn ("ordered comparison of pointer with integer zero");
2214 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2215 && integer_zerop (op0))
2217 result_type = type1;
2218 if (pedantic)
2219 pedwarn ("ordered comparison of pointer with integer zero");
2221 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2223 result_type = type0;
2224 if (! flag_traditional)
2225 pedwarn ("comparison between pointer and integer");
2227 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2229 result_type = type1;
2230 if (! flag_traditional)
2231 pedwarn ("comparison between pointer and integer");
2233 break;
2235 default:
2236 break;
2239 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2241 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2243 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2245 if (shorten || common || short_compare)
2246 result_type = common_type (type0, type1);
2248 /* For certain operations (which identify themselves by shorten != 0)
2249 if both args were extended from the same smaller type,
2250 do the arithmetic in that type and then extend.
2252 shorten !=0 and !=1 indicates a bitwise operation.
2253 For them, this optimization is safe only if
2254 both args are zero-extended or both are sign-extended.
2255 Otherwise, we might change the result.
2256 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2257 but calculated in (unsigned short) it would be (unsigned short)-1. */
2259 if (shorten && none_complex)
2261 int unsigned0, unsigned1;
2262 tree arg0 = get_narrower (op0, &unsigned0);
2263 tree arg1 = get_narrower (op1, &unsigned1);
2264 /* UNS is 1 if the operation to be done is an unsigned one. */
2265 int uns = TREE_UNSIGNED (result_type);
2266 tree type;
2268 final_type = result_type;
2270 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2271 but it *requires* conversion to FINAL_TYPE. */
2273 if ((TYPE_PRECISION (TREE_TYPE (op0))
2274 == TYPE_PRECISION (TREE_TYPE (arg0)))
2275 && TREE_TYPE (op0) != final_type)
2276 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2277 if ((TYPE_PRECISION (TREE_TYPE (op1))
2278 == TYPE_PRECISION (TREE_TYPE (arg1)))
2279 && TREE_TYPE (op1) != final_type)
2280 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2282 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2284 /* For bitwise operations, signedness of nominal type
2285 does not matter. Consider only how operands were extended. */
2286 if (shorten == -1)
2287 uns = unsigned0;
2289 /* Note that in all three cases below we refrain from optimizing
2290 an unsigned operation on sign-extended args.
2291 That would not be valid. */
2293 /* Both args variable: if both extended in same way
2294 from same width, do it in that width.
2295 Do it unsigned if args were zero-extended. */
2296 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2297 < TYPE_PRECISION (result_type))
2298 && (TYPE_PRECISION (TREE_TYPE (arg1))
2299 == TYPE_PRECISION (TREE_TYPE (arg0)))
2300 && unsigned0 == unsigned1
2301 && (unsigned0 || !uns))
2302 result_type
2303 = signed_or_unsigned_type (unsigned0,
2304 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2305 else if (TREE_CODE (arg0) == INTEGER_CST
2306 && (unsigned1 || !uns)
2307 && (TYPE_PRECISION (TREE_TYPE (arg1))
2308 < TYPE_PRECISION (result_type))
2309 && (type = signed_or_unsigned_type (unsigned1,
2310 TREE_TYPE (arg1)),
2311 int_fits_type_p (arg0, type)))
2312 result_type = type;
2313 else if (TREE_CODE (arg1) == INTEGER_CST
2314 && (unsigned0 || !uns)
2315 && (TYPE_PRECISION (TREE_TYPE (arg0))
2316 < TYPE_PRECISION (result_type))
2317 && (type = signed_or_unsigned_type (unsigned0,
2318 TREE_TYPE (arg0)),
2319 int_fits_type_p (arg1, type)))
2320 result_type = type;
2323 /* Shifts can be shortened if shifting right. */
2325 if (short_shift)
2327 int unsigned_arg;
2328 tree arg0 = get_narrower (op0, &unsigned_arg);
2330 final_type = result_type;
2332 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2333 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2335 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2336 /* We can shorten only if the shift count is less than the
2337 number of bits in the smaller type size. */
2338 && TREE_INT_CST_HIGH (op1) == 0
2339 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
2340 /* If arg is sign-extended and then unsigned-shifted,
2341 we can simulate this with a signed shift in arg's type
2342 only if the extended result is at least twice as wide
2343 as the arg. Otherwise, the shift could use up all the
2344 ones made by sign-extension and bring in zeros.
2345 We can't optimize that case at all, but in most machines
2346 it never happens because available widths are 2**N. */
2347 && (!TREE_UNSIGNED (final_type)
2348 || unsigned_arg
2349 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2351 /* Do an unsigned shift if the operand was zero-extended. */
2352 result_type
2353 = signed_or_unsigned_type (unsigned_arg,
2354 TREE_TYPE (arg0));
2355 /* Convert value-to-be-shifted to that type. */
2356 if (TREE_TYPE (op0) != result_type)
2357 op0 = convert (result_type, op0);
2358 converted = 1;
2362 /* Comparison operations are shortened too but differently.
2363 They identify themselves by setting short_compare = 1. */
2365 if (short_compare)
2367 /* Don't write &op0, etc., because that would prevent op0
2368 from being kept in a register.
2369 Instead, make copies of the our local variables and
2370 pass the copies by reference, then copy them back afterward. */
2371 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2372 enum tree_code xresultcode = resultcode;
2373 tree val
2374 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2375 if (val != 0)
2376 return val;
2377 op0 = xop0, op1 = xop1;
2378 converted = 1;
2379 resultcode = xresultcode;
2381 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2382 && skip_evaluation == 0)
2384 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2385 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2387 int unsignedp0, unsignedp1;
2388 tree primop0 = get_narrower (op0, &unsignedp0);
2389 tree primop1 = get_narrower (op1, &unsignedp1);
2391 /* Avoid spurious warnings for comparison with enumerators. */
2393 xop0 = orig_op0;
2394 xop1 = orig_op1;
2395 STRIP_TYPE_NOPS (xop0);
2396 STRIP_TYPE_NOPS (xop1);
2398 /* Give warnings for comparisons between signed and unsigned
2399 quantities that may fail. */
2400 /* Do the checking based on the original operand trees, so that
2401 casts will be considered, but default promotions won't be. */
2403 /* Do not warn if the comparison is being done in a signed type,
2404 since the signed type will only be chosen if it can represent
2405 all the values of the unsigned type. */
2406 if (! TREE_UNSIGNED (result_type))
2407 /* OK */;
2408 /* Do not warn if both operands are unsigned. */
2409 else if (op0_signed == op1_signed)
2410 /* OK */;
2411 /* Do not warn if the signed quantity is an unsuffixed
2412 integer literal (or some static constant expression
2413 involving such literals) and it is non-negative. */
2414 else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST
2415 && tree_int_cst_sgn (xop0) >= 0)
2416 || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
2417 && tree_int_cst_sgn (xop1) >= 0))
2418 /* OK */;
2419 /* Do not warn if the comparison is an equality operation,
2420 the unsigned quantity is an integral constant and it does
2421 not use the most significant bit of result_type. */
2422 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
2423 && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST
2424 && int_fits_type_p (xop1, signed_type (result_type)))
2425 || (op1_signed && TREE_CODE (xop0) == INTEGER_CST
2426 && int_fits_type_p (xop0, signed_type (result_type)))))
2427 /* OK */;
2428 else
2429 warning ("comparison between signed and unsigned");
2431 /* Warn if two unsigned values are being compared in a size
2432 larger than their original size, and one (and only one) is the
2433 result of a `~' operator. This comparison will always fail.
2435 Also warn if one operand is a constant, and the constant
2436 does not have all bits set that are set in the ~ operand
2437 when it is extended. */
2439 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2440 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2442 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2443 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2444 &unsignedp0);
2445 else
2446 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2447 &unsignedp1);
2449 if (TREE_CODE (primop0) == INTEGER_CST
2450 || TREE_CODE (primop1) == INTEGER_CST)
2452 tree primop;
2453 long constant, mask;
2454 int unsignedp, bits;
2456 if (TREE_CODE (primop0) == INTEGER_CST)
2458 primop = primop1;
2459 unsignedp = unsignedp1;
2460 constant = TREE_INT_CST_LOW (primop0);
2462 else
2464 primop = primop0;
2465 unsignedp = unsignedp0;
2466 constant = TREE_INT_CST_LOW (primop1);
2469 bits = TYPE_PRECISION (TREE_TYPE (primop));
2470 if (bits < TYPE_PRECISION (result_type)
2471 && bits < HOST_BITS_PER_LONG && unsignedp)
2473 mask = (~0L) << bits;
2474 if ((mask & constant) != mask)
2475 warning ("comparison of promoted ~unsigned with constant");
2478 else if (unsignedp0 && unsignedp1
2479 && (TYPE_PRECISION (TREE_TYPE (primop0))
2480 < TYPE_PRECISION (result_type))
2481 && (TYPE_PRECISION (TREE_TYPE (primop1))
2482 < TYPE_PRECISION (result_type)))
2483 warning ("comparison of promoted ~unsigned with unsigned");
2489 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2490 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2491 Then the expression will be built.
2492 It will be given type FINAL_TYPE if that is nonzero;
2493 otherwise, it will be given type RESULT_TYPE. */
2495 if (!result_type)
2497 binary_op_error (code);
2498 return error_mark_node;
2501 if (! converted)
2503 if (TREE_TYPE (op0) != result_type)
2504 op0 = convert (result_type, op0);
2505 if (TREE_TYPE (op1) != result_type)
2506 op1 = convert (result_type, op1);
2509 if (build_type == NULL_TREE)
2510 build_type = result_type;
2513 register tree result = build (resultcode, build_type, op0, op1);
2514 register tree folded;
2516 folded = fold (result);
2517 if (folded == result)
2518 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2519 if (final_type != 0)
2520 return convert (final_type, folded);
2521 return folded;
2525 /* Return a tree for the sum or difference (RESULTCODE says which)
2526 of pointer PTROP and integer INTOP. */
2528 static tree
2529 pointer_int_sum (resultcode, ptrop, intop)
2530 enum tree_code resultcode;
2531 register tree ptrop, intop;
2533 tree size_exp;
2535 register tree result;
2536 register tree folded;
2538 /* The result is a pointer of the same type that is being added. */
2540 register tree result_type = TREE_TYPE (ptrop);
2542 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2544 if (pedantic || warn_pointer_arith)
2545 pedwarn ("pointer of type `void *' used in arithmetic");
2546 size_exp = integer_one_node;
2548 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2550 if (pedantic || warn_pointer_arith)
2551 pedwarn ("pointer to a function used in arithmetic");
2552 size_exp = integer_one_node;
2554 else
2555 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2557 /* If what we are about to multiply by the size of the elements
2558 contains a constant term, apply distributive law
2559 and multiply that constant term separately.
2560 This helps produce common subexpressions. */
2562 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2563 && ! TREE_CONSTANT (intop)
2564 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2565 && TREE_CONSTANT (size_exp)
2566 /* If the constant comes from pointer subtraction,
2567 skip this optimization--it would cause an error. */
2568 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2569 /* If the constant is unsigned, and smaller than the pointer size,
2570 then we must skip this optimization. This is because it could cause
2571 an overflow error if the constant is negative but INTOP is not. */
2572 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2573 || (TYPE_PRECISION (TREE_TYPE (intop))
2574 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2576 enum tree_code subcode = resultcode;
2577 tree int_type = TREE_TYPE (intop);
2578 if (TREE_CODE (intop) == MINUS_EXPR)
2579 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2580 /* Convert both subexpression types to the type of intop,
2581 because weird cases involving pointer arithmetic
2582 can result in a sum or difference with different type args. */
2583 ptrop = build_binary_op (subcode, ptrop,
2584 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2585 intop = convert (int_type, TREE_OPERAND (intop, 0));
2588 /* Convert the integer argument to a type the same size as sizetype
2589 so the multiply won't overflow spuriously. */
2591 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2592 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2593 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2594 TREE_UNSIGNED (sizetype)), intop);
2596 /* Replace the integer argument with a suitable product by the object size.
2597 Do this multiplication as signed, then convert to the appropriate
2598 pointer type (actually unsigned integral). */
2600 intop = convert (result_type,
2601 build_binary_op (MULT_EXPR, intop,
2602 convert (TREE_TYPE (intop), size_exp), 1));
2604 /* Create the sum or difference. */
2606 result = build (resultcode, result_type, ptrop, intop);
2608 folded = fold (result);
2609 if (folded == result)
2610 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2611 return folded;
2614 /* Return a tree for the difference of pointers OP0 and OP1.
2615 The resulting tree has type int. */
2617 static tree
2618 pointer_diff (op0, op1)
2619 register tree op0, op1;
2621 register tree result, folded;
2622 tree restype = ptrdiff_type_node;
2624 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2626 if (pedantic || warn_pointer_arith)
2628 if (TREE_CODE (target_type) == VOID_TYPE)
2629 pedwarn ("pointer of type `void *' used in subtraction");
2630 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2631 pedwarn ("pointer to a function used in subtraction");
2634 /* First do the subtraction as integers;
2635 then drop through to build the divide operator.
2636 Do not do default conversions on the minus operator
2637 in case restype is a short type. */
2639 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2640 convert (restype, op1), 0);
2641 /* This generates an error if op1 is pointer to incomplete type. */
2642 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2643 error ("arithmetic on pointer to an incomplete type");
2645 /* This generates an error if op0 is pointer to incomplete type. */
2646 op1 = c_size_in_bytes (target_type);
2648 /* Divide by the size, in easiest possible way. */
2650 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2652 folded = fold (result);
2653 if (folded == result)
2654 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2655 return folded;
2658 /* Construct and perhaps optimize a tree representation
2659 for a unary operation. CODE, a tree_code, specifies the operation
2660 and XARG is the operand. NOCONVERT nonzero suppresses
2661 the default promotions (such as from short to int). */
2663 tree
2664 build_unary_op (code, xarg, noconvert)
2665 enum tree_code code;
2666 tree xarg;
2667 int noconvert;
2669 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2670 register tree arg = xarg;
2671 register tree argtype = 0;
2672 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2673 tree val;
2675 if (typecode == ERROR_MARK)
2676 return error_mark_node;
2677 if (typecode == ENUMERAL_TYPE)
2678 typecode = INTEGER_TYPE;
2680 switch (code)
2682 case CONVERT_EXPR:
2683 /* This is used for unary plus, because a CONVERT_EXPR
2684 is enough to prevent anybody from looking inside for
2685 associativity, but won't generate any code. */
2686 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2687 || typecode == COMPLEX_TYPE))
2689 error ("wrong type argument to unary plus");
2690 return error_mark_node;
2692 else if (!noconvert)
2693 arg = default_conversion (arg);
2694 break;
2696 case NEGATE_EXPR:
2697 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2698 || typecode == COMPLEX_TYPE))
2700 error ("wrong type argument to unary minus");
2701 return error_mark_node;
2703 else if (!noconvert)
2704 arg = default_conversion (arg);
2705 break;
2707 case BIT_NOT_EXPR:
2708 if (typecode == COMPLEX_TYPE)
2710 code = CONJ_EXPR;
2711 if (!noconvert)
2712 arg = default_conversion (arg);
2714 else if (typecode != INTEGER_TYPE)
2716 error ("wrong type argument to bit-complement");
2717 return error_mark_node;
2719 else if (!noconvert)
2720 arg = default_conversion (arg);
2721 break;
2723 case ABS_EXPR:
2724 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2725 || typecode == COMPLEX_TYPE))
2727 error ("wrong type argument to abs");
2728 return error_mark_node;
2730 else if (!noconvert)
2731 arg = default_conversion (arg);
2732 break;
2734 case CONJ_EXPR:
2735 /* Conjugating a real value is a no-op, but allow it anyway. */
2736 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2737 || typecode == COMPLEX_TYPE))
2739 error ("wrong type argument to conjugation");
2740 return error_mark_node;
2742 else if (!noconvert)
2743 arg = default_conversion (arg);
2744 break;
2746 case TRUTH_NOT_EXPR:
2747 if (typecode != INTEGER_TYPE
2748 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2749 && typecode != COMPLEX_TYPE
2750 /* These will convert to a pointer. */
2751 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2753 error ("wrong type argument to unary exclamation mark");
2754 return error_mark_node;
2756 arg = truthvalue_conversion (arg);
2757 return invert_truthvalue (arg);
2759 case NOP_EXPR:
2760 break;
2762 case REALPART_EXPR:
2763 if (TREE_CODE (arg) == COMPLEX_CST)
2764 return TREE_REALPART (arg);
2765 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2766 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2767 else
2768 return arg;
2770 case IMAGPART_EXPR:
2771 if (TREE_CODE (arg) == COMPLEX_CST)
2772 return TREE_IMAGPART (arg);
2773 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2774 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2775 else
2776 return convert (TREE_TYPE (arg), integer_zero_node);
2778 case PREINCREMENT_EXPR:
2779 case POSTINCREMENT_EXPR:
2780 case PREDECREMENT_EXPR:
2781 case POSTDECREMENT_EXPR:
2782 /* Handle complex lvalues (when permitted)
2783 by reduction to simpler cases. */
2785 val = unary_complex_lvalue (code, arg);
2786 if (val != 0)
2787 return val;
2789 /* Increment or decrement the real part of the value,
2790 and don't change the imaginary part. */
2791 if (typecode == COMPLEX_TYPE)
2793 tree real, imag;
2795 arg = stabilize_reference (arg);
2796 real = build_unary_op (REALPART_EXPR, arg, 1);
2797 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2798 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2799 build_unary_op (code, real, 1), imag);
2802 /* Report invalid types. */
2804 if (typecode != POINTER_TYPE
2805 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2807 error (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2808 ? "wrong type argument to increment"
2809 : "wrong type argument to decrement");
2810 return error_mark_node;
2814 register tree inc;
2815 tree result_type = TREE_TYPE (arg);
2817 arg = get_unwidened (arg, 0);
2818 argtype = TREE_TYPE (arg);
2820 /* Compute the increment. */
2822 if (typecode == POINTER_TYPE)
2824 /* If pointer target is an undefined struct,
2825 we just cannot know how to do the arithmetic. */
2826 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2827 error (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2828 ? "increment of pointer to unknown structure"
2829 : "decrement of pointer to unknown structure");
2830 else if ((pedantic || warn_pointer_arith)
2831 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2832 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2833 pedwarn (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2834 ? "wrong type argument to increment"
2835 : "wrong type argument to decrement");
2836 inc = c_size_in_bytes (TREE_TYPE (result_type));
2838 else
2839 inc = integer_one_node;
2841 inc = convert (argtype, inc);
2843 /* Handle incrementing a cast-expression. */
2845 while (1)
2846 switch (TREE_CODE (arg))
2848 case NOP_EXPR:
2849 case CONVERT_EXPR:
2850 case FLOAT_EXPR:
2851 case FIX_TRUNC_EXPR:
2852 case FIX_FLOOR_EXPR:
2853 case FIX_ROUND_EXPR:
2854 case FIX_CEIL_EXPR:
2855 pedantic_lvalue_warning (CONVERT_EXPR);
2856 /* If the real type has the same machine representation
2857 as the type it is cast to, we can make better output
2858 by adding directly to the inside of the cast. */
2859 if ((TREE_CODE (TREE_TYPE (arg))
2860 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2861 && (TYPE_MODE (TREE_TYPE (arg))
2862 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2863 arg = TREE_OPERAND (arg, 0);
2864 else
2866 tree incremented, modify, value;
2867 arg = stabilize_reference (arg);
2868 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2869 value = arg;
2870 else
2871 value = save_expr (arg);
2872 incremented = build (((code == PREINCREMENT_EXPR
2873 || code == POSTINCREMENT_EXPR)
2874 ? PLUS_EXPR : MINUS_EXPR),
2875 argtype, value, inc);
2876 TREE_SIDE_EFFECTS (incremented) = 1;
2877 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2878 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2879 TREE_USED (value) = 1;
2880 return value;
2882 break;
2884 default:
2885 goto give_up;
2887 give_up:
2889 /* Complain about anything else that is not a true lvalue. */
2890 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2891 || code == POSTINCREMENT_EXPR)
2892 ? "invalid lvalue in increment"
2893 : "invalid lvalue in decrement")))
2894 return error_mark_node;
2896 /* Report a read-only lvalue. */
2897 if (TREE_READONLY (arg))
2898 readonly_warning (arg,
2899 ((code == PREINCREMENT_EXPR
2900 || code == POSTINCREMENT_EXPR)
2901 ? "increment" : "decrement"));
2903 val = build (code, TREE_TYPE (arg), arg, inc);
2904 TREE_SIDE_EFFECTS (val) = 1;
2905 val = convert (result_type, val);
2906 if (TREE_CODE (val) != code)
2907 TREE_NO_UNUSED_WARNING (val) = 1;
2908 return val;
2911 case ADDR_EXPR:
2912 /* Note that this operation never does default_conversion
2913 regardless of NOCONVERT. */
2915 /* Let &* cancel out to simplify resulting code. */
2916 if (TREE_CODE (arg) == INDIRECT_REF)
2918 /* Don't let this be an lvalue. */
2919 if (lvalue_p (TREE_OPERAND (arg, 0)))
2920 return non_lvalue (TREE_OPERAND (arg, 0));
2921 return TREE_OPERAND (arg, 0);
2924 /* For &x[y], return x+y */
2925 if (TREE_CODE (arg) == ARRAY_REF)
2927 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
2928 return error_mark_node;
2929 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2930 TREE_OPERAND (arg, 1), 1);
2933 /* Handle complex lvalues (when permitted)
2934 by reduction to simpler cases. */
2935 val = unary_complex_lvalue (code, arg);
2936 if (val != 0)
2937 return val;
2939 #if 0 /* Turned off because inconsistent;
2940 float f; *&(int)f = 3.4 stores in int format
2941 whereas (int)f = 3.4 stores in float format. */
2942 /* Address of a cast is just a cast of the address
2943 of the operand of the cast. */
2944 switch (TREE_CODE (arg))
2946 case NOP_EXPR:
2947 case CONVERT_EXPR:
2948 case FLOAT_EXPR:
2949 case FIX_TRUNC_EXPR:
2950 case FIX_FLOOR_EXPR:
2951 case FIX_ROUND_EXPR:
2952 case FIX_CEIL_EXPR:
2953 if (pedantic)
2954 pedwarn ("ANSI C forbids the address of a cast expression");
2955 return convert (build_pointer_type (TREE_TYPE (arg)),
2956 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
2957 0));
2959 #endif
2961 /* Allow the address of a constructor if all the elements
2962 are constant. */
2963 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
2965 /* Anything not already handled and not a true memory reference
2966 is an error. */
2967 else if (typecode != FUNCTION_TYPE
2968 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2969 return error_mark_node;
2971 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2972 argtype = TREE_TYPE (arg);
2973 /* If the lvalue is const or volatile, merge that into the type
2974 to which the address will point. Note that you can't get a
2975 restricted pointer by taking the address of something, so we
2976 only have to deal with `const' and `volatile' here. */
2977 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
2978 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2980 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
2981 argtype = c_build_type_variant (argtype,
2982 TREE_READONLY (arg),
2983 TREE_THIS_VOLATILE (arg));
2986 argtype = build_pointer_type (argtype);
2988 if (mark_addressable (arg) == 0)
2989 return error_mark_node;
2992 tree addr;
2994 if (TREE_CODE (arg) == COMPONENT_REF)
2996 tree field = TREE_OPERAND (arg, 1);
2998 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3000 if (DECL_C_BIT_FIELD (field))
3002 error ("attempt to take address of bit-field structure member `%s'",
3003 IDENTIFIER_POINTER (DECL_NAME (field)));
3004 return error_mark_node;
3007 addr = convert (argtype, addr);
3009 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3011 tree offset
3012 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3013 size_int (BITS_PER_UNIT));
3014 int flag = TREE_CONSTANT (addr);
3015 addr = fold (build (PLUS_EXPR, argtype,
3016 addr, convert (argtype, offset)));
3017 TREE_CONSTANT (addr) = flag;
3020 else
3021 addr = build1 (code, argtype, arg);
3023 /* Address of a static or external variable or
3024 file-scope function counts as a constant. */
3025 if (staticp (arg)
3026 && ! (TREE_CODE (arg) == FUNCTION_DECL
3027 && DECL_CONTEXT (arg) != 0))
3028 TREE_CONSTANT (addr) = 1;
3029 return addr;
3032 default:
3033 break;
3036 if (argtype == 0)
3037 argtype = TREE_TYPE (arg);
3038 return fold (build1 (code, argtype, arg));
3041 #if 0
3042 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3043 convert ARG with the same conversions in the same order
3044 and return the result. */
3046 static tree
3047 convert_sequence (conversions, arg)
3048 tree conversions;
3049 tree arg;
3051 switch (TREE_CODE (conversions))
3053 case NOP_EXPR:
3054 case CONVERT_EXPR:
3055 case FLOAT_EXPR:
3056 case FIX_TRUNC_EXPR:
3057 case FIX_FLOOR_EXPR:
3058 case FIX_ROUND_EXPR:
3059 case FIX_CEIL_EXPR:
3060 return convert (TREE_TYPE (conversions),
3061 convert_sequence (TREE_OPERAND (conversions, 0),
3062 arg));
3064 default:
3065 return arg;
3068 #endif /* 0 */
3070 /* Return nonzero if REF is an lvalue valid for this language.
3071 Lvalues can be assigned, unless their type has TYPE_READONLY.
3072 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3075 lvalue_p (ref)
3076 tree ref;
3078 register enum tree_code code = TREE_CODE (ref);
3080 switch (code)
3082 case REALPART_EXPR:
3083 case IMAGPART_EXPR:
3084 case COMPONENT_REF:
3085 return lvalue_p (TREE_OPERAND (ref, 0));
3087 case STRING_CST:
3088 return 1;
3090 case INDIRECT_REF:
3091 case ARRAY_REF:
3092 case VAR_DECL:
3093 case PARM_DECL:
3094 case RESULT_DECL:
3095 case ERROR_MARK:
3096 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3097 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3099 case BIND_EXPR:
3100 case RTL_EXPR:
3101 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3103 default:
3104 return 0;
3108 /* Return nonzero if REF is an lvalue valid for this language;
3109 otherwise, print an error message and return zero. */
3112 lvalue_or_else (ref, msgid)
3113 tree ref;
3114 const char *msgid;
3116 int win = lvalue_p (ref);
3118 if (! win)
3119 error (msgid);
3121 return win;
3124 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3125 for certain kinds of expressions which are not really lvalues
3126 but which we can accept as lvalues.
3128 If ARG is not a kind of expression we can handle, return zero. */
3130 static tree
3131 unary_complex_lvalue (code, arg)
3132 enum tree_code code;
3133 tree arg;
3135 /* Handle (a, b) used as an "lvalue". */
3136 if (TREE_CODE (arg) == COMPOUND_EXPR)
3138 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3140 /* If this returns a function type, it isn't really being used as
3141 an lvalue, so don't issue a warning about it. */
3142 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3143 pedantic_lvalue_warning (COMPOUND_EXPR);
3145 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3146 TREE_OPERAND (arg, 0), real_result);
3149 /* Handle (a ? b : c) used as an "lvalue". */
3150 if (TREE_CODE (arg) == COND_EXPR)
3152 pedantic_lvalue_warning (COND_EXPR);
3153 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3154 pedantic_lvalue_warning (COMPOUND_EXPR);
3156 return (build_conditional_expr
3157 (TREE_OPERAND (arg, 0),
3158 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3159 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3162 return 0;
3165 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3166 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3168 static void
3169 pedantic_lvalue_warning (code)
3170 enum tree_code code;
3172 if (pedantic)
3173 pedwarn (code == COND_EXPR
3174 ? "ANSI C forbids use of conditional expressions as lvalues"
3175 : code == COMPOUND_EXPR
3176 ? "ANSI C forbids use of compound expressions as lvalues"
3177 : "ANSI C forbids use of cast expressions as lvalues");
3180 /* Warn about storing in something that is `const'. */
3182 void
3183 readonly_warning (arg, msgid)
3184 tree arg;
3185 const char *msgid;
3187 /* Forbid assignments to iterators. */
3188 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3189 pedwarn ("%s of iterator `%s'", _(msgid),
3190 IDENTIFIER_POINTER (DECL_NAME (arg)));
3192 if (TREE_CODE (arg) == COMPONENT_REF)
3194 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3195 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3196 else
3197 pedwarn ("%s of read-only member `%s'", _(msgid),
3198 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3200 else if (TREE_CODE (arg) == VAR_DECL)
3201 pedwarn ("%s of read-only variable `%s'", _(msgid),
3202 IDENTIFIER_POINTER (DECL_NAME (arg)));
3203 else
3204 pedwarn ("%s of read-only location", _(msgid));
3207 /* Mark EXP saying that we need to be able to take the
3208 address of it; it should not be allocated in a register.
3209 Value is 1 if successful. */
3212 mark_addressable (exp)
3213 tree exp;
3215 register tree x = exp;
3216 while (1)
3217 switch (TREE_CODE (x))
3219 case COMPONENT_REF:
3220 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3222 error ("cannot take address of bitfield `%s'",
3223 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3224 return 0;
3227 /* ... fall through ... */
3229 case ADDR_EXPR:
3230 case ARRAY_REF:
3231 case REALPART_EXPR:
3232 case IMAGPART_EXPR:
3233 x = TREE_OPERAND (x, 0);
3234 break;
3236 case CONSTRUCTOR:
3237 TREE_ADDRESSABLE (x) = 1;
3238 return 1;
3240 case VAR_DECL:
3241 case CONST_DECL:
3242 case PARM_DECL:
3243 case RESULT_DECL:
3244 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3245 && DECL_NONLOCAL (x))
3247 if (TREE_PUBLIC (x))
3249 error ("global register variable `%s' used in nested function",
3250 IDENTIFIER_POINTER (DECL_NAME (x)));
3251 return 0;
3253 pedwarn ("register variable `%s' used in nested function",
3254 IDENTIFIER_POINTER (DECL_NAME (x)));
3256 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3258 if (TREE_PUBLIC (x))
3260 error ("address of global register variable `%s' requested",
3261 IDENTIFIER_POINTER (DECL_NAME (x)));
3262 return 0;
3265 /* If we are making this addressable due to its having
3266 volatile components, give a different error message. Also
3267 handle the case of an unnamed parameter by not trying
3268 to give the name. */
3270 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3272 error ("cannot put object with volatile field into register");
3273 return 0;
3276 pedwarn ("address of register variable `%s' requested",
3277 IDENTIFIER_POINTER (DECL_NAME (x)));
3279 put_var_into_stack (x);
3281 /* drops in */
3282 case FUNCTION_DECL:
3283 TREE_ADDRESSABLE (x) = 1;
3284 #if 0 /* poplevel deals with this now. */
3285 if (DECL_CONTEXT (x) == 0)
3286 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3287 #endif
3289 default:
3290 return 1;
3294 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3296 tree
3297 build_conditional_expr (ifexp, op1, op2)
3298 tree ifexp, op1, op2;
3300 register tree type1;
3301 register tree type2;
3302 register enum tree_code code1;
3303 register enum tree_code code2;
3304 register tree result_type = NULL;
3305 tree orig_op1 = op1, orig_op2 = op2;
3307 ifexp = truthvalue_conversion (default_conversion (ifexp));
3309 #if 0 /* Produces wrong result if within sizeof. */
3310 /* Don't promote the operands separately if they promote
3311 the same way. Return the unpromoted type and let the combined
3312 value get promoted if necessary. */
3314 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3315 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3316 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3317 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3319 if (TREE_CODE (ifexp) == INTEGER_CST)
3320 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3322 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3324 #endif
3326 /* Promote both alternatives. */
3328 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3329 op1 = default_conversion (op1);
3330 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3331 op2 = default_conversion (op2);
3333 if (TREE_CODE (ifexp) == ERROR_MARK
3334 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3335 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3336 return error_mark_node;
3338 type1 = TREE_TYPE (op1);
3339 code1 = TREE_CODE (type1);
3340 type2 = TREE_TYPE (op2);
3341 code2 = TREE_CODE (type2);
3343 /* Quickly detect the usual case where op1 and op2 have the same type
3344 after promotion. */
3345 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3347 if (type1 == type2)
3348 result_type = type1;
3349 else
3350 result_type = TYPE_MAIN_VARIANT (type1);
3352 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3353 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3355 result_type = common_type (type1, type2);
3357 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3359 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3360 pedwarn ("ANSI C forbids conditional expr with only one void side");
3361 result_type = void_type_node;
3363 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3365 if (comp_target_types (type1, type2))
3366 result_type = common_type (type1, type2);
3367 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3368 && TREE_CODE (orig_op1) != NOP_EXPR)
3369 result_type = qualify_type (type2, type1);
3370 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3371 && TREE_CODE (orig_op2) != NOP_EXPR)
3372 result_type = qualify_type (type1, type2);
3373 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3375 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3376 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3377 result_type = qualify_type (type1, type2);
3379 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3381 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3382 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3383 result_type = qualify_type (type2, type1);
3385 else
3387 pedwarn ("pointer type mismatch in conditional expression");
3388 result_type = build_pointer_type (void_type_node);
3391 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3393 if (! integer_zerop (op2))
3394 pedwarn ("pointer/integer type mismatch in conditional expression");
3395 else
3397 op2 = null_pointer_node;
3398 #if 0 /* The spec seems to say this is permitted. */
3399 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3400 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3401 #endif
3403 result_type = type1;
3405 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3407 if (!integer_zerop (op1))
3408 pedwarn ("pointer/integer type mismatch in conditional expression");
3409 else
3411 op1 = null_pointer_node;
3412 #if 0 /* The spec seems to say this is permitted. */
3413 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3414 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3415 #endif
3417 result_type = type2;
3420 if (!result_type)
3422 if (flag_cond_mismatch)
3423 result_type = void_type_node;
3424 else
3426 error ("type mismatch in conditional expression");
3427 return error_mark_node;
3431 /* Merge const and volatile flags of the incoming types. */
3432 result_type
3433 = build_type_variant (result_type,
3434 TREE_READONLY (op1) || TREE_READONLY (op2),
3435 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3437 if (result_type != TREE_TYPE (op1))
3438 op1 = convert_and_check (result_type, op1);
3439 if (result_type != TREE_TYPE (op2))
3440 op2 = convert_and_check (result_type, op2);
3442 #if 0
3443 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3445 result_type = TREE_TYPE (op1);
3446 if (TREE_CONSTANT (ifexp))
3447 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3449 if (TYPE_MODE (result_type) == BLKmode)
3451 register tree tempvar
3452 = build_decl (VAR_DECL, NULL_TREE, result_type);
3453 register tree xop1 = build_modify_expr (tempvar, op1);
3454 register tree xop2 = build_modify_expr (tempvar, op2);
3455 register tree result = fold (build (COND_EXPR, result_type,
3456 ifexp, xop1, xop2));
3458 layout_decl (tempvar, TYPE_ALIGN (result_type));
3459 /* No way to handle variable-sized objects here.
3460 I fear that the entire handling of BLKmode conditional exprs
3461 needs to be redone. */
3462 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3463 abort ();
3464 DECL_RTL (tempvar)
3465 = assign_stack_local (DECL_MODE (tempvar),
3466 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3467 + BITS_PER_UNIT - 1)
3468 / BITS_PER_UNIT,
3471 TREE_SIDE_EFFECTS (result)
3472 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3473 | TREE_SIDE_EFFECTS (op2);
3474 return build (COMPOUND_EXPR, result_type, result, tempvar);
3477 #endif /* 0 */
3479 if (TREE_CODE (ifexp) == INTEGER_CST)
3480 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3482 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3485 /* Given a list of expressions, return a compound expression
3486 that performs them all and returns the value of the last of them. */
3488 tree
3489 build_compound_expr (list)
3490 tree list;
3492 return internal_build_compound_expr (list, TRUE);
3495 static tree
3496 internal_build_compound_expr (list, first_p)
3497 tree list;
3498 int first_p;
3500 register tree rest;
3502 if (TREE_CHAIN (list) == 0)
3504 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3505 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3507 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3508 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3509 list = TREE_OPERAND (list, 0);
3510 #endif
3512 /* Don't let (0, 0) be null pointer constant. */
3513 if (!first_p && integer_zerop (TREE_VALUE (list)))
3514 return non_lvalue (TREE_VALUE (list));
3515 return TREE_VALUE (list);
3518 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3520 /* Convert arrays to pointers when there really is a comma operator. */
3521 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3522 TREE_VALUE (TREE_CHAIN (list))
3523 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3526 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3528 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3530 /* The left-hand operand of a comma expression is like an expression
3531 statement: with -W or -Wunused, we should warn if it doesn't have
3532 any side-effects, unless it was explicitly cast to (void). */
3533 if ((extra_warnings || warn_unused)
3534 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3535 && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
3536 warning ("left-hand operand of comma expression has no effect");
3538 /* When pedantic, a compound expression can be neither an lvalue
3539 nor an integer constant expression. */
3540 if (! pedantic)
3541 return rest;
3544 /* With -Wunused, we should also warn if the left-hand operand does have
3545 side-effects, but computes a value which is not used. For example, in
3546 `foo() + bar(), baz()' the result of the `+' operator is not used,
3547 so we should issue a warning. */
3548 else if (warn_unused)
3549 warn_if_unused_value (TREE_VALUE (list));
3551 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3554 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3556 tree
3557 build_c_cast (type, expr)
3558 register tree type;
3559 tree expr;
3561 register tree value = expr;
3563 if (type == error_mark_node || expr == error_mark_node)
3564 return error_mark_node;
3565 type = TYPE_MAIN_VARIANT (type);
3567 #if 0
3568 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3569 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3570 value = TREE_OPERAND (value, 0);
3571 #endif
3573 if (TREE_CODE (type) == ARRAY_TYPE)
3575 error ("cast specifies array type");
3576 return error_mark_node;
3579 if (TREE_CODE (type) == FUNCTION_TYPE)
3581 error ("cast specifies function type");
3582 return error_mark_node;
3585 if (type == TREE_TYPE (value))
3587 if (pedantic)
3589 if (TREE_CODE (type) == RECORD_TYPE
3590 || TREE_CODE (type) == UNION_TYPE)
3591 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3594 else if (TREE_CODE (type) == UNION_TYPE)
3596 tree field;
3597 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3598 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3599 value = default_conversion (value);
3601 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3602 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3603 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3604 break;
3606 if (field)
3608 const char *name;
3609 tree t;
3611 if (pedantic)
3612 pedwarn ("ANSI C forbids casts to union type");
3613 if (TYPE_NAME (type) != 0)
3615 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3616 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3617 else
3618 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3620 else
3621 name = "";
3622 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3623 build_tree_list (field, value)),
3624 0, 0);
3625 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3626 return t;
3628 error ("cast to union type from type not present in union");
3629 return error_mark_node;
3631 else
3633 tree otype, ovalue;
3635 /* If casting to void, avoid the error that would come
3636 from default_conversion in the case of a non-lvalue array. */
3637 if (type == void_type_node)
3638 return build1 (CONVERT_EXPR, type, value);
3640 /* Convert functions and arrays to pointers,
3641 but don't convert any other types. */
3642 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3643 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3644 value = default_conversion (value);
3645 otype = TREE_TYPE (value);
3647 /* Optionally warn about potentially worrisome casts. */
3649 if (warn_cast_qual
3650 && TREE_CODE (type) == POINTER_TYPE
3651 && TREE_CODE (otype) == POINTER_TYPE)
3653 /* Go to the innermost object being pointed to. */
3654 tree in_type = type;
3655 tree in_otype = otype;
3657 while (TREE_CODE (in_type) == POINTER_TYPE)
3658 in_type = TREE_TYPE (in_type);
3659 while (TREE_CODE (in_otype) == POINTER_TYPE)
3660 in_otype = TREE_TYPE (in_otype);
3662 if (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type))
3663 /* There are qualifiers present in IN_OTYPE that are not
3664 present in IN_TYPE. */
3665 pedwarn ("cast discards qualifiers from pointer target type");
3668 /* Warn about possible alignment problems. */
3669 if (STRICT_ALIGNMENT && warn_cast_align
3670 && TREE_CODE (type) == POINTER_TYPE
3671 && TREE_CODE (otype) == POINTER_TYPE
3672 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3673 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3674 /* Don't warn about opaque types, where the actual alignment
3675 restriction is unknown. */
3676 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3677 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3678 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3679 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3680 warning ("cast increases required alignment of target type");
3682 if (TREE_CODE (type) == INTEGER_TYPE
3683 && TREE_CODE (otype) == POINTER_TYPE
3684 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3685 && !TREE_CONSTANT (value))
3686 warning ("cast from pointer to integer of different size");
3688 if (warn_bad_function_cast
3689 && TREE_CODE (value) == CALL_EXPR
3690 && TREE_CODE (type) != TREE_CODE (otype))
3691 warning ("cast does not match function type");
3693 if (TREE_CODE (type) == POINTER_TYPE
3694 && TREE_CODE (otype) == INTEGER_TYPE
3695 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3696 #if 0
3697 /* Don't warn about converting 0 to pointer,
3698 provided the 0 was explicit--not cast or made by folding. */
3699 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3700 #endif
3701 /* Don't warn about converting any constant. */
3702 && !TREE_CONSTANT (value))
3703 warning ("cast to pointer from integer of different size");
3705 ovalue = value;
3706 value = convert (type, value);
3708 /* Ignore any integer overflow caused by the cast. */
3709 if (TREE_CODE (value) == INTEGER_CST)
3711 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3712 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3716 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3717 if (pedantic && TREE_CODE (value) == INTEGER_CST
3718 && TREE_CODE (expr) == INTEGER_CST
3719 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3720 value = non_lvalue (value);
3722 /* If pedantic, don't let a cast be an lvalue. */
3723 if (value == expr && pedantic)
3724 value = non_lvalue (value);
3726 return value;
3729 /* Build an assignment expression of lvalue LHS from value RHS.
3730 MODIFYCODE is the code for a binary operator that we use
3731 to combine the old value of LHS with RHS to get the new value.
3732 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3734 tree
3735 build_modify_expr (lhs, modifycode, rhs)
3736 tree lhs, rhs;
3737 enum tree_code modifycode;
3739 register tree result;
3740 tree newrhs;
3741 tree lhstype = TREE_TYPE (lhs);
3742 tree olhstype = lhstype;
3744 /* Types that aren't fully specified cannot be used in assignments. */
3745 lhs = require_complete_type (lhs);
3747 /* Avoid duplicate error messages from operands that had errors. */
3748 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3749 return error_mark_node;
3751 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3752 /* Do not use STRIP_NOPS here. We do not want an enumerator
3753 whose value is 0 to count as a null pointer constant. */
3754 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3755 rhs = TREE_OPERAND (rhs, 0);
3757 newrhs = rhs;
3759 /* Handle control structure constructs used as "lvalues". */
3761 switch (TREE_CODE (lhs))
3763 /* Handle (a, b) used as an "lvalue". */
3764 case COMPOUND_EXPR:
3765 pedantic_lvalue_warning (COMPOUND_EXPR);
3766 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3767 if (TREE_CODE (newrhs) == ERROR_MARK)
3768 return error_mark_node;
3769 return build (COMPOUND_EXPR, lhstype,
3770 TREE_OPERAND (lhs, 0), newrhs);
3772 /* Handle (a ? b : c) used as an "lvalue". */
3773 case COND_EXPR:
3774 pedantic_lvalue_warning (COND_EXPR);
3775 rhs = save_expr (rhs);
3777 /* Produce (a ? (b = rhs) : (c = rhs))
3778 except that the RHS goes through a save-expr
3779 so the code to compute it is only emitted once. */
3780 tree cond
3781 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3782 build_modify_expr (TREE_OPERAND (lhs, 1),
3783 modifycode, rhs),
3784 build_modify_expr (TREE_OPERAND (lhs, 2),
3785 modifycode, rhs));
3786 if (TREE_CODE (cond) == ERROR_MARK)
3787 return cond;
3788 /* Make sure the code to compute the rhs comes out
3789 before the split. */
3790 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3791 /* But cast it to void to avoid an "unused" error. */
3792 convert (void_type_node, rhs), cond);
3794 default:
3795 break;
3798 /* If a binary op has been requested, combine the old LHS value with the RHS
3799 producing the value we should actually store into the LHS. */
3801 if (modifycode != NOP_EXPR)
3803 lhs = stabilize_reference (lhs);
3804 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3807 /* Handle a cast used as an "lvalue".
3808 We have already performed any binary operator using the value as cast.
3809 Now convert the result to the cast type of the lhs,
3810 and then true type of the lhs and store it there;
3811 then convert result back to the cast type to be the value
3812 of the assignment. */
3814 switch (TREE_CODE (lhs))
3816 case NOP_EXPR:
3817 case CONVERT_EXPR:
3818 case FLOAT_EXPR:
3819 case FIX_TRUNC_EXPR:
3820 case FIX_FLOOR_EXPR:
3821 case FIX_ROUND_EXPR:
3822 case FIX_CEIL_EXPR:
3823 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3824 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3825 newrhs = default_conversion (newrhs);
3827 tree inner_lhs = TREE_OPERAND (lhs, 0);
3828 tree result;
3829 result = build_modify_expr (inner_lhs, NOP_EXPR,
3830 convert (TREE_TYPE (inner_lhs),
3831 convert (lhstype, newrhs)));
3832 if (TREE_CODE (result) == ERROR_MARK)
3833 return result;
3834 pedantic_lvalue_warning (CONVERT_EXPR);
3835 return convert (TREE_TYPE (lhs), result);
3838 default:
3839 break;
3842 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3843 Reject anything strange now. */
3845 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3846 return error_mark_node;
3848 /* Warn about storing in something that is `const'. */
3850 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3851 || ((TREE_CODE (lhstype) == RECORD_TYPE
3852 || TREE_CODE (lhstype) == UNION_TYPE)
3853 && C_TYPE_FIELDS_READONLY (lhstype)))
3854 readonly_warning (lhs, "assignment");
3856 /* If storing into a structure or union member,
3857 it has probably been given type `int'.
3858 Compute the type that would go with
3859 the actual amount of storage the member occupies. */
3861 if (TREE_CODE (lhs) == COMPONENT_REF
3862 && (TREE_CODE (lhstype) == INTEGER_TYPE
3863 || TREE_CODE (lhstype) == REAL_TYPE
3864 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3865 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3867 /* If storing in a field that is in actuality a short or narrower than one,
3868 we must store in the field in its actual type. */
3870 if (lhstype != TREE_TYPE (lhs))
3872 lhs = copy_node (lhs);
3873 TREE_TYPE (lhs) = lhstype;
3876 /* Convert new value to destination type. */
3878 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3879 NULL_TREE, NULL_TREE, 0);
3880 if (TREE_CODE (newrhs) == ERROR_MARK)
3881 return error_mark_node;
3883 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3884 TREE_SIDE_EFFECTS (result) = 1;
3886 /* If we got the LHS in a different type for storing in,
3887 convert the result back to the nominal type of LHS
3888 so that the value we return always has the same type
3889 as the LHS argument. */
3891 if (olhstype == TREE_TYPE (result))
3892 return result;
3893 return convert_for_assignment (olhstype, result, _("assignment"),
3894 NULL_TREE, NULL_TREE, 0);
3897 /* Convert value RHS to type TYPE as preparation for an assignment
3898 to an lvalue of type TYPE.
3899 The real work of conversion is done by `convert'.
3900 The purpose of this function is to generate error messages
3901 for assignments that are not allowed in C.
3902 ERRTYPE is a string to use in error messages:
3903 "assignment", "return", etc. If it is null, this is parameter passing
3904 for a function call (and different error messages are output).
3906 FUNNAME is the name of the function being called,
3907 as an IDENTIFIER_NODE, or null.
3908 PARMNUM is the number of the argument, for printing in error messages. */
3910 static tree
3911 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3912 tree type, rhs;
3913 const char *errtype;
3914 tree fundecl, funname;
3915 int parmnum;
3917 register enum tree_code codel = TREE_CODE (type);
3918 register tree rhstype;
3919 register enum tree_code coder;
3921 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3922 /* Do not use STRIP_NOPS here. We do not want an enumerator
3923 whose value is 0 to count as a null pointer constant. */
3924 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3925 rhs = TREE_OPERAND (rhs, 0);
3927 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3928 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3929 rhs = default_conversion (rhs);
3930 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3931 rhs = decl_constant_value (rhs);
3933 rhstype = TREE_TYPE (rhs);
3934 coder = TREE_CODE (rhstype);
3936 if (coder == ERROR_MARK)
3937 return error_mark_node;
3939 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3941 overflow_warning (rhs);
3942 /* Check for Objective-C protocols. This will issue a warning if
3943 there are protocol violations. No need to use the return value. */
3944 maybe_objc_comptypes (type, rhstype, 0);
3945 return rhs;
3948 if (coder == VOID_TYPE)
3950 error ("void value not ignored as it ought to be");
3951 return error_mark_node;
3953 /* Arithmetic types all interconvert, and enum is treated like int. */
3954 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
3955 || codel == COMPLEX_TYPE)
3956 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
3957 || coder == COMPLEX_TYPE))
3958 return convert_and_check (type, rhs);
3960 /* Conversion to a transparent union from its member types.
3961 This applies only to function arguments. */
3962 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3964 tree memb_types;
3965 tree marginal_memb_type = 0;
3967 for (memb_types = TYPE_FIELDS (type); memb_types;
3968 memb_types = TREE_CHAIN (memb_types))
3970 tree memb_type = TREE_TYPE (memb_types);
3972 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3973 TYPE_MAIN_VARIANT (rhstype)))
3974 break;
3976 if (TREE_CODE (memb_type) != POINTER_TYPE)
3977 continue;
3979 if (coder == POINTER_TYPE)
3981 register tree ttl = TREE_TYPE (memb_type);
3982 register tree ttr = TREE_TYPE (rhstype);
3984 /* Any non-function converts to a [const][volatile] void *
3985 and vice versa; otherwise, targets must be the same.
3986 Meanwhile, the lhs target must have all the qualifiers of
3987 the rhs. */
3988 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3989 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3990 || comp_target_types (memb_type, rhstype))
3992 /* If this type won't generate any warnings, use it. */
3993 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3994 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3995 && TREE_CODE (ttl) == FUNCTION_TYPE)
3996 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3997 == TYPE_QUALS (ttr))
3998 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3999 == TYPE_QUALS (ttl))))
4000 break;
4002 /* Keep looking for a better type, but remember this one. */
4003 if (! marginal_memb_type)
4004 marginal_memb_type = memb_type;
4008 /* Can convert integer zero to any pointer type. */
4009 if (integer_zerop (rhs)
4010 || (TREE_CODE (rhs) == NOP_EXPR
4011 && integer_zerop (TREE_OPERAND (rhs, 0))))
4013 rhs = null_pointer_node;
4014 break;
4018 if (memb_types || marginal_memb_type)
4020 if (! memb_types)
4022 /* We have only a marginally acceptable member type;
4023 it needs a warning. */
4024 register tree ttl = TREE_TYPE (marginal_memb_type);
4025 register tree ttr = TREE_TYPE (rhstype);
4027 /* Const and volatile mean something different for function
4028 types, so the usual warnings are not appropriate. */
4029 if (TREE_CODE (ttr) == FUNCTION_TYPE
4030 && TREE_CODE (ttl) == FUNCTION_TYPE)
4032 /* Because const and volatile on functions are
4033 restrictions that say the function will not do
4034 certain things, it is okay to use a const or volatile
4035 function where an ordinary one is wanted, but not
4036 vice-versa. */
4037 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4038 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4039 errtype, funname, parmnum);
4041 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4042 warn_for_assignment ("%s discards qualifiers from pointer target type",
4043 errtype, funname,
4044 parmnum);
4047 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4048 pedwarn ("ANSI C prohibits argument conversion to union type");
4050 return build1 (NOP_EXPR, type, rhs);
4054 /* Conversions among pointers */
4055 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
4057 register tree ttl = TREE_TYPE (type);
4058 register tree ttr = TREE_TYPE (rhstype);
4060 /* Any non-function converts to a [const][volatile] void *
4061 and vice versa; otherwise, targets must be the same.
4062 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4063 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4064 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4065 || comp_target_types (type, rhstype)
4066 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4067 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4069 if (pedantic
4070 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4071 && TREE_CODE (ttr) == FUNCTION_TYPE)
4073 (TYPE_MAIN_VARIANT (ttr) == void_type_node
4074 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4075 which are not ANSI null ptr constants. */
4076 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4077 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4078 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4079 errtype, funname, parmnum);
4080 /* Const and volatile mean something different for function types,
4081 so the usual warnings are not appropriate. */
4082 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4083 && TREE_CODE (ttl) != FUNCTION_TYPE)
4085 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4086 warn_for_assignment ("%s discards qualifiers from pointer target type",
4087 errtype, funname, parmnum);
4088 /* If this is not a case of ignoring a mismatch in signedness,
4089 no warning. */
4090 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4091 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4092 || comp_target_types (type, rhstype))
4094 /* If there is a mismatch, do warn. */
4095 else if (pedantic)
4096 warn_for_assignment ("pointer targets in %s differ in signedness",
4097 errtype, funname, parmnum);
4099 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4100 && TREE_CODE (ttr) == FUNCTION_TYPE)
4102 /* Because const and volatile on functions are restrictions
4103 that say the function will not do certain things,
4104 it is okay to use a const or volatile function
4105 where an ordinary one is wanted, but not vice-versa. */
4106 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4107 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4108 errtype, funname, parmnum);
4111 else
4112 warn_for_assignment ("%s from incompatible pointer type",
4113 errtype, funname, parmnum);
4114 return convert (type, rhs);
4116 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4118 /* An explicit constant 0 can convert to a pointer,
4119 or one that results from arithmetic, even including
4120 a cast to integer type. */
4121 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4123 ! (TREE_CODE (rhs) == NOP_EXPR
4124 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4125 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4126 && integer_zerop (TREE_OPERAND (rhs, 0))))
4128 warn_for_assignment ("%s makes pointer from integer without a cast",
4129 errtype, funname, parmnum);
4130 return convert (type, rhs);
4132 return null_pointer_node;
4134 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4136 warn_for_assignment ("%s makes integer from pointer without a cast",
4137 errtype, funname, parmnum);
4138 return convert (type, rhs);
4141 if (!errtype)
4143 if (funname)
4145 tree selector = maybe_building_objc_message_expr ();
4147 if (selector && parmnum > 2)
4148 error ("incompatible type for argument %d of `%s'",
4149 parmnum - 2, IDENTIFIER_POINTER (selector));
4150 else
4151 error ("incompatible type for argument %d of `%s'",
4152 parmnum, IDENTIFIER_POINTER (funname));
4154 else
4155 error ("incompatible type for argument %d of indirect function call",
4156 parmnum);
4158 else
4159 error ("incompatible types in %s", errtype);
4161 return error_mark_node;
4164 /* Print a warning using MSGID.
4165 It gets OPNAME as its one parameter.
4166 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4167 FUNCTION and ARGNUM are handled specially if we are building an
4168 Objective-C selector. */
4170 static void
4171 warn_for_assignment (msgid, opname, function, argnum)
4172 const char *msgid;
4173 const char *opname;
4174 tree function;
4175 int argnum;
4177 if (opname == 0)
4179 tree selector = maybe_building_objc_message_expr ();
4180 char * new_opname;
4182 if (selector && argnum > 2)
4184 function = selector;
4185 argnum -= 2;
4187 if (function)
4189 /* Function name is known; supply it. */
4190 const char *argstring = _("passing arg %d of `%s'");
4191 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4192 + strlen (argstring) + 1 + 25
4193 /*%d*/ + 1);
4194 sprintf (new_opname, argstring, argnum,
4195 IDENTIFIER_POINTER (function));
4197 else
4199 /* Function name unknown (call through ptr); just give arg number.*/
4200 const char *argnofun = _("passing arg %d of pointer to function");
4201 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4202 sprintf (new_opname, argnofun, argnum);
4204 opname = new_opname;
4206 pedwarn (msgid, opname);
4209 /* If VALUE is a compound expr all of whose expressions are constant, then
4210 return its value. Otherwise, return error_mark_node.
4212 This is for handling COMPOUND_EXPRs as initializer elements
4213 which is allowed with a warning when -pedantic is specified. */
4215 static tree
4216 valid_compound_expr_initializer (value, endtype)
4217 tree value;
4218 tree endtype;
4220 if (TREE_CODE (value) == COMPOUND_EXPR)
4222 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4223 == error_mark_node)
4224 return error_mark_node;
4225 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4226 endtype);
4228 else if (! TREE_CONSTANT (value)
4229 && ! initializer_constant_valid_p (value, endtype))
4230 return error_mark_node;
4231 else
4232 return value;
4235 /* Perform appropriate conversions on the initial value of a variable,
4236 store it in the declaration DECL,
4237 and print any error messages that are appropriate.
4238 If the init is invalid, store an ERROR_MARK. */
4240 void
4241 store_init_value (decl, init)
4242 tree decl, init;
4244 register tree value, type;
4246 /* If variable's type was invalidly declared, just ignore it. */
4248 type = TREE_TYPE (decl);
4249 if (TREE_CODE (type) == ERROR_MARK)
4250 return;
4252 /* Digest the specified initializer into an expression. */
4254 value = digest_init (type, init, TREE_STATIC (decl),
4255 TREE_STATIC (decl) || pedantic);
4257 /* Store the expression if valid; else report error. */
4259 #if 0
4260 /* Note that this is the only place we can detect the error
4261 in a case such as struct foo bar = (struct foo) { x, y };
4262 where there is one initial value which is a constructor expression. */
4263 if (value == error_mark_node)
4265 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4267 error ("initializer for static variable is not constant");
4268 value = error_mark_node;
4270 else if (TREE_STATIC (decl)
4271 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4273 error ("initializer for static variable uses complicated arithmetic");
4274 value = error_mark_node;
4276 else
4278 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4280 if (! TREE_CONSTANT (value))
4281 pedwarn ("aggregate initializer is not constant");
4282 else if (! TREE_STATIC (value))
4283 pedwarn ("aggregate initializer uses complicated arithmetic");
4286 #endif
4288 DECL_INITIAL (decl) = value;
4290 /* ANSI wants warnings about out-of-range constant initializers. */
4291 STRIP_TYPE_NOPS (value);
4292 constant_expression_warning (value);
4295 /* Methods for storing and printing names for error messages. */
4297 /* Implement a spelling stack that allows components of a name to be pushed
4298 and popped. Each element on the stack is this structure. */
4300 struct spelling
4302 int kind;
4303 union
4305 int i;
4306 const char *s;
4307 } u;
4310 #define SPELLING_STRING 1
4311 #define SPELLING_MEMBER 2
4312 #define SPELLING_BOUNDS 3
4314 static struct spelling *spelling; /* Next stack element (unused). */
4315 static struct spelling *spelling_base; /* Spelling stack base. */
4316 static int spelling_size; /* Size of the spelling stack. */
4318 /* Macros to save and restore the spelling stack around push_... functions.
4319 Alternative to SAVE_SPELLING_STACK. */
4321 #define SPELLING_DEPTH() (spelling - spelling_base)
4322 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4324 /* Save and restore the spelling stack around arbitrary C code. */
4326 #define SAVE_SPELLING_DEPTH(code) \
4328 int __depth = SPELLING_DEPTH (); \
4329 code; \
4330 RESTORE_SPELLING_DEPTH (__depth); \
4333 /* Push an element on the spelling stack with type KIND and assign VALUE
4334 to MEMBER. */
4336 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4338 int depth = SPELLING_DEPTH (); \
4340 if (depth >= spelling_size) \
4342 spelling_size += 10; \
4343 if (spelling_base == 0) \
4344 spelling_base \
4345 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4346 else \
4347 spelling_base \
4348 = (struct spelling *) xrealloc (spelling_base, \
4349 spelling_size * sizeof (struct spelling)); \
4350 RESTORE_SPELLING_DEPTH (depth); \
4353 spelling->kind = (KIND); \
4354 spelling->MEMBER = (VALUE); \
4355 spelling++; \
4358 /* Push STRING on the stack. Printed literally. */
4360 static void
4361 push_string (string)
4362 const char *string;
4364 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4367 /* Push a member name on the stack. Printed as '.' STRING. */
4369 static void
4370 push_member_name (decl)
4371 tree decl;
4374 const char *string
4375 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4376 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4379 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4381 static void
4382 push_array_bounds (bounds)
4383 int bounds;
4385 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4388 /* Compute the maximum size in bytes of the printed spelling. */
4390 static int
4391 spelling_length ()
4393 register int size = 0;
4394 register struct spelling *p;
4396 for (p = spelling_base; p < spelling; p++)
4398 if (p->kind == SPELLING_BOUNDS)
4399 size += 25;
4400 else
4401 size += strlen (p->u.s) + 1;
4404 return size;
4407 /* Print the spelling to BUFFER and return it. */
4409 static char *
4410 print_spelling (buffer)
4411 register char *buffer;
4413 register char *d = buffer;
4414 register struct spelling *p;
4416 for (p = spelling_base; p < spelling; p++)
4417 if (p->kind == SPELLING_BOUNDS)
4419 sprintf (d, "[%d]", p->u.i);
4420 d += strlen (d);
4422 else
4424 register const char *s;
4425 if (p->kind == SPELLING_MEMBER)
4426 *d++ = '.';
4427 for (s = p->u.s; (*d = *s++); d++)
4430 *d++ = '\0';
4431 return buffer;
4434 /* Issue an error message for a bad initializer component.
4435 MSGID identifies the message.
4436 The component name is taken from the spelling stack. */
4438 void
4439 error_init (msgid)
4440 const char *msgid;
4442 char *ofwhat;
4444 error (msgid);
4445 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4446 if (*ofwhat)
4447 error ("(near initialization for `%s')", ofwhat);
4450 /* Issue a pedantic warning for a bad initializer component.
4451 MSGID identifies the message.
4452 The component name is taken from the spelling stack. */
4454 void
4455 pedwarn_init (msgid)
4456 const char *msgid;
4458 char *ofwhat;
4460 pedwarn (msgid);
4461 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4462 if (*ofwhat)
4463 pedwarn ("(near initialization for `%s')", ofwhat);
4466 /* Issue a warning for a bad initializer component.
4467 MSGID identifies the message.
4468 The component name is taken from the spelling stack. */
4470 static void
4471 warning_init (msgid)
4472 const char *msgid;
4474 char *ofwhat;
4476 warning (msgid);
4477 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4478 if (*ofwhat)
4479 warning ("(near initialization for `%s')", ofwhat);
4482 /* Digest the parser output INIT as an initializer for type TYPE.
4483 Return a C expression of type TYPE to represent the initial value.
4485 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4486 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4487 applies only to elements of constructors. */
4489 static tree
4490 digest_init (type, init, require_constant, constructor_constant)
4491 tree type, init;
4492 int require_constant, constructor_constant;
4494 enum tree_code code = TREE_CODE (type);
4495 tree inside_init = init;
4497 if (init == error_mark_node)
4498 return init;
4500 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4501 /* Do not use STRIP_NOPS here. We do not want an enumerator
4502 whose value is 0 to count as a null pointer constant. */
4503 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4504 inside_init = TREE_OPERAND (init, 0);
4506 /* Initialization of an array of chars from a string constant
4507 optionally enclosed in braces. */
4509 if (code == ARRAY_TYPE)
4511 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4512 if ((typ1 == char_type_node
4513 || typ1 == signed_char_type_node
4514 || typ1 == unsigned_char_type_node
4515 || typ1 == unsigned_wchar_type_node
4516 || typ1 == signed_wchar_type_node)
4517 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4519 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4520 TYPE_MAIN_VARIANT (type)))
4521 return inside_init;
4523 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4524 != char_type_node)
4525 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4527 error_init ("char-array initialized from wide string");
4528 return error_mark_node;
4530 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4531 == char_type_node)
4532 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4534 error_init ("int-array initialized from non-wide string");
4535 return error_mark_node;
4538 TREE_TYPE (inside_init) = type;
4539 if (TYPE_DOMAIN (type) != 0
4540 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4542 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4543 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4544 /* Subtract 1 (or sizeof (wchar_t))
4545 because it's ok to ignore the terminating null char
4546 that is counted in the length of the constant. */
4547 if (size < TREE_STRING_LENGTH (inside_init)
4548 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4549 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4550 : 1))
4551 pedwarn_init ("initializer-string for array of chars is too long");
4553 return inside_init;
4557 /* Any type can be initialized
4558 from an expression of the same type, optionally with braces. */
4560 if (inside_init && TREE_TYPE (inside_init) != 0
4561 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4562 TYPE_MAIN_VARIANT (type))
4563 || (code == ARRAY_TYPE
4564 && comptypes (TREE_TYPE (inside_init), type))
4565 || (code == POINTER_TYPE
4566 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4567 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4568 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4569 TREE_TYPE (type)))))
4571 if (code == POINTER_TYPE
4572 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4573 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4574 inside_init = default_conversion (inside_init);
4575 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4576 && TREE_CODE (inside_init) != CONSTRUCTOR)
4578 error_init ("array initialized from non-constant array expression");
4579 return error_mark_node;
4582 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4583 inside_init = decl_constant_value (inside_init);
4585 /* Compound expressions can only occur here if -pedantic or
4586 -pedantic-errors is specified. In the later case, we always want
4587 an error. In the former case, we simply want a warning. */
4588 if (require_constant && pedantic
4589 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4591 inside_init
4592 = valid_compound_expr_initializer (inside_init,
4593 TREE_TYPE (inside_init));
4594 if (inside_init == error_mark_node)
4595 error_init ("initializer element is not constant");
4596 else
4597 pedwarn_init ("initializer element is not constant");
4598 if (flag_pedantic_errors)
4599 inside_init = error_mark_node;
4601 else if (require_constant && ! TREE_CONSTANT (inside_init))
4603 error_init ("initializer element is not constant");
4604 inside_init = error_mark_node;
4606 else if (require_constant
4607 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4609 error_init ("initializer element is not computable at load time");
4610 inside_init = error_mark_node;
4613 return inside_init;
4616 /* Handle scalar types, including conversions. */
4618 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4619 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4621 /* Note that convert_for_assignment calls default_conversion
4622 for arrays and functions. We must not call it in the
4623 case where inside_init is a null pointer constant. */
4624 inside_init
4625 = convert_for_assignment (type, init, _("initialization"),
4626 NULL_TREE, NULL_TREE, 0);
4628 if (require_constant && ! TREE_CONSTANT (inside_init))
4630 error_init ("initializer element is not constant");
4631 inside_init = error_mark_node;
4633 else if (require_constant
4634 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4636 error_init ("initializer element is not computable at load time");
4637 inside_init = error_mark_node;
4640 return inside_init;
4643 /* Come here only for records and arrays. */
4645 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4647 error_init ("variable-sized object may not be initialized");
4648 return error_mark_node;
4651 /* Traditionally, you can write struct foo x = 0;
4652 and it initializes the first element of x to 0. */
4653 if (flag_traditional)
4655 tree top = 0, prev = 0, otype = type;
4656 while (TREE_CODE (type) == RECORD_TYPE
4657 || TREE_CODE (type) == ARRAY_TYPE
4658 || TREE_CODE (type) == QUAL_UNION_TYPE
4659 || TREE_CODE (type) == UNION_TYPE)
4661 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4662 if (prev == 0)
4663 top = temp;
4664 else
4665 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4666 prev = temp;
4667 if (TREE_CODE (type) == ARRAY_TYPE)
4668 type = TREE_TYPE (type);
4669 else if (TYPE_FIELDS (type))
4670 type = TREE_TYPE (TYPE_FIELDS (type));
4671 else
4673 error_init ("invalid initializer");
4674 return error_mark_node;
4678 if (otype != type)
4680 TREE_OPERAND (prev, 1)
4681 = build_tree_list (NULL_TREE,
4682 digest_init (type, init, require_constant,
4683 constructor_constant));
4684 return top;
4686 else
4687 return error_mark_node;
4689 error_init ("invalid initializer");
4690 return error_mark_node;
4693 /* Handle initializers that use braces. */
4695 /* Type of object we are accumulating a constructor for.
4696 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4697 static tree constructor_type;
4699 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4700 left to fill. */
4701 static tree constructor_fields;
4703 /* For an ARRAY_TYPE, this is the specified index
4704 at which to store the next element we get.
4705 This is a special INTEGER_CST node that we modify in place. */
4706 static tree constructor_index;
4708 /* For an ARRAY_TYPE, this is the end index of the range
4709 to initialize with the next element, or NULL in the ordinary case
4710 where the element is used just once. */
4711 static tree constructor_range_end;
4713 /* For an ARRAY_TYPE, this is the maximum index. */
4714 static tree constructor_max_index;
4716 /* For a RECORD_TYPE, this is the first field not yet written out. */
4717 static tree constructor_unfilled_fields;
4719 /* For an ARRAY_TYPE, this is the index of the first element
4720 not yet written out.
4721 This is a special INTEGER_CST node that we modify in place. */
4722 static tree constructor_unfilled_index;
4724 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4725 This is so we can generate gaps between fields, when appropriate.
4726 This is a special INTEGER_CST node that we modify in place. */
4727 static tree constructor_bit_index;
4729 /* If we are saving up the elements rather than allocating them,
4730 this is the list of elements so far (in reverse order,
4731 most recent first). */
4732 static tree constructor_elements;
4734 /* 1 if so far this constructor's elements are all compile-time constants. */
4735 static int constructor_constant;
4737 /* 1 if so far this constructor's elements are all valid address constants. */
4738 static int constructor_simple;
4740 /* 1 if this constructor is erroneous so far. */
4741 static int constructor_erroneous;
4743 /* 1 if have called defer_addressed_constants. */
4744 static int constructor_subconstants_deferred;
4746 /* Structure for managing pending initializer elements, organized as an
4747 AVL tree. */
4749 struct init_node
4751 struct init_node *left, *right;
4752 struct init_node *parent;
4753 int balance;
4754 tree purpose;
4755 tree value;
4758 /* Tree of pending elements at this constructor level.
4759 These are elements encountered out of order
4760 which belong at places we haven't reached yet in actually
4761 writing the output. */
4762 static struct init_node *constructor_pending_elts;
4764 /* The SPELLING_DEPTH of this constructor. */
4765 static int constructor_depth;
4767 /* 0 if implicitly pushing constructor levels is allowed. */
4768 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4770 static int require_constant_value;
4771 static int require_constant_elements;
4773 /* 1 if it is ok to output this constructor as we read it.
4774 0 means must accumulate a CONSTRUCTOR expression. */
4775 static int constructor_incremental;
4777 /* DECL node for which an initializer is being read.
4778 0 means we are reading a constructor expression
4779 such as (struct foo) {...}. */
4780 static tree constructor_decl;
4782 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4783 static char *constructor_asmspec;
4785 /* Nonzero if this is an initializer for a top-level decl. */
4786 static int constructor_top_level;
4789 /* This stack has a level for each implicit or explicit level of
4790 structuring in the initializer, including the outermost one. It
4791 saves the values of most of the variables above. */
4793 struct constructor_stack
4795 struct constructor_stack *next;
4796 tree type;
4797 tree fields;
4798 tree index;
4799 tree range_end;
4800 tree max_index;
4801 tree unfilled_index;
4802 tree unfilled_fields;
4803 tree bit_index;
4804 tree elements;
4805 int offset;
4806 struct init_node *pending_elts;
4807 int depth;
4808 /* If nonzero, this value should replace the entire
4809 constructor at this level. */
4810 tree replacement_value;
4811 char constant;
4812 char simple;
4813 char implicit;
4814 char incremental;
4815 char erroneous;
4816 char outer;
4819 struct constructor_stack *constructor_stack;
4821 /* This stack records separate initializers that are nested.
4822 Nested initializers can't happen in ANSI C, but GNU C allows them
4823 in cases like { ... (struct foo) { ... } ... }. */
4825 struct initializer_stack
4827 struct initializer_stack *next;
4828 tree decl;
4829 char *asmspec;
4830 struct constructor_stack *constructor_stack;
4831 tree elements;
4832 struct spelling *spelling;
4833 struct spelling *spelling_base;
4834 int spelling_size;
4835 char top_level;
4836 char incremental;
4837 char require_constant_value;
4838 char require_constant_elements;
4839 char deferred;
4842 struct initializer_stack *initializer_stack;
4844 /* Prepare to parse and output the initializer for variable DECL. */
4846 void
4847 start_init (decl, asmspec_tree, top_level)
4848 tree decl;
4849 tree asmspec_tree;
4850 int top_level;
4852 const char *locus;
4853 struct initializer_stack *p
4854 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
4855 char *asmspec = 0;
4857 if (asmspec_tree)
4858 asmspec = TREE_STRING_POINTER (asmspec_tree);
4860 p->decl = constructor_decl;
4861 p->asmspec = constructor_asmspec;
4862 p->incremental = constructor_incremental;
4863 p->require_constant_value = require_constant_value;
4864 p->require_constant_elements = require_constant_elements;
4865 p->constructor_stack = constructor_stack;
4866 p->elements = constructor_elements;
4867 p->spelling = spelling;
4868 p->spelling_base = spelling_base;
4869 p->spelling_size = spelling_size;
4870 p->deferred = constructor_subconstants_deferred;
4871 p->top_level = constructor_top_level;
4872 p->next = initializer_stack;
4873 initializer_stack = p;
4875 constructor_decl = decl;
4876 constructor_incremental = top_level;
4877 constructor_asmspec = asmspec;
4878 constructor_subconstants_deferred = 0;
4879 constructor_top_level = top_level;
4881 if (decl != 0)
4883 require_constant_value = TREE_STATIC (decl);
4884 require_constant_elements
4885 = ((TREE_STATIC (decl) || pedantic)
4886 /* For a scalar, you can always use any value to initialize,
4887 even within braces. */
4888 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4889 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4890 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4891 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4892 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4893 constructor_incremental |= TREE_STATIC (decl);
4895 else
4897 require_constant_value = 0;
4898 require_constant_elements = 0;
4899 locus = "(anonymous)";
4902 constructor_stack = 0;
4904 missing_braces_mentioned = 0;
4906 spelling_base = 0;
4907 spelling_size = 0;
4908 RESTORE_SPELLING_DEPTH (0);
4910 if (locus)
4911 push_string (locus);
4914 void
4915 finish_init ()
4917 struct initializer_stack *p = initializer_stack;
4919 /* Output subconstants (string constants, usually)
4920 that were referenced within this initializer and saved up.
4921 Must do this if and only if we called defer_addressed_constants. */
4922 if (constructor_subconstants_deferred)
4923 output_deferred_addressed_constants ();
4925 /* Free the whole constructor stack of this initializer. */
4926 while (constructor_stack)
4928 struct constructor_stack *q = constructor_stack;
4929 constructor_stack = q->next;
4930 free (q);
4933 /* Pop back to the data of the outer initializer (if any). */
4934 constructor_decl = p->decl;
4935 constructor_asmspec = p->asmspec;
4936 constructor_incremental = p->incremental;
4937 require_constant_value = p->require_constant_value;
4938 require_constant_elements = p->require_constant_elements;
4939 constructor_stack = p->constructor_stack;
4940 constructor_elements = p->elements;
4941 spelling = p->spelling;
4942 spelling_base = p->spelling_base;
4943 spelling_size = p->spelling_size;
4944 constructor_subconstants_deferred = p->deferred;
4945 constructor_top_level = p->top_level;
4946 initializer_stack = p->next;
4947 free (p);
4950 /* Call here when we see the initializer is surrounded by braces.
4951 This is instead of a call to push_init_level;
4952 it is matched by a call to pop_init_level.
4954 TYPE is the type to initialize, for a constructor expression.
4955 For an initializer for a decl, TYPE is zero. */
4957 void
4958 really_start_incremental_init (type)
4959 tree type;
4961 struct constructor_stack *p
4962 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
4964 if (type == 0)
4965 type = TREE_TYPE (constructor_decl);
4967 /* Turn off constructor_incremental if type is a struct with bitfields.
4968 Do this before the first push, so that the corrected value
4969 is available in finish_init. */
4970 check_init_type_bitfields (type);
4972 p->type = constructor_type;
4973 p->fields = constructor_fields;
4974 p->index = constructor_index;
4975 p->range_end = constructor_range_end;
4976 p->max_index = constructor_max_index;
4977 p->unfilled_index = constructor_unfilled_index;
4978 p->unfilled_fields = constructor_unfilled_fields;
4979 p->bit_index = constructor_bit_index;
4980 p->elements = constructor_elements;
4981 p->constant = constructor_constant;
4982 p->simple = constructor_simple;
4983 p->erroneous = constructor_erroneous;
4984 p->pending_elts = constructor_pending_elts;
4985 p->depth = constructor_depth;
4986 p->replacement_value = 0;
4987 p->implicit = 0;
4988 p->incremental = constructor_incremental;
4989 p->outer = 0;
4990 p->next = 0;
4991 constructor_stack = p;
4993 constructor_constant = 1;
4994 constructor_simple = 1;
4995 constructor_depth = SPELLING_DEPTH ();
4996 constructor_elements = 0;
4997 constructor_pending_elts = 0;
4998 constructor_type = type;
5000 if (TREE_CODE (constructor_type) == RECORD_TYPE
5001 || TREE_CODE (constructor_type) == UNION_TYPE)
5003 constructor_fields = TYPE_FIELDS (constructor_type);
5004 /* Skip any nameless bit fields at the beginning. */
5005 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5006 && DECL_NAME (constructor_fields) == 0)
5007 constructor_fields = TREE_CHAIN (constructor_fields);
5008 constructor_unfilled_fields = constructor_fields;
5009 constructor_bit_index = copy_node (integer_zero_node);
5010 TREE_TYPE (constructor_bit_index) = sbitsizetype;
5012 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5014 constructor_range_end = 0;
5015 if (TYPE_DOMAIN (constructor_type))
5017 constructor_max_index
5018 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5019 constructor_index
5020 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5022 else
5023 constructor_index = copy_node (integer_zero_node);
5024 constructor_unfilled_index = copy_node (constructor_index);
5026 else
5028 /* Handle the case of int x = {5}; */
5029 constructor_fields = constructor_type;
5030 constructor_unfilled_fields = constructor_type;
5033 if (constructor_incremental)
5035 int momentary = suspend_momentary ();
5036 push_obstacks_nochange ();
5037 if (TREE_PERMANENT (constructor_decl))
5038 end_temporary_allocation ();
5039 make_decl_rtl (constructor_decl, constructor_asmspec,
5040 constructor_top_level);
5041 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5042 pop_obstacks ();
5043 resume_momentary (momentary);
5046 if (constructor_incremental)
5048 defer_addressed_constants ();
5049 constructor_subconstants_deferred = 1;
5053 /* Push down into a subobject, for initialization.
5054 If this is for an explicit set of braces, IMPLICIT is 0.
5055 If it is because the next element belongs at a lower level,
5056 IMPLICIT is 1. */
5058 void
5059 push_init_level (implicit)
5060 int implicit;
5062 struct constructor_stack *p;
5064 /* If we've exhausted any levels that didn't have braces,
5065 pop them now. */
5066 while (constructor_stack->implicit)
5068 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5069 || TREE_CODE (constructor_type) == UNION_TYPE)
5070 && constructor_fields == 0)
5071 process_init_element (pop_init_level (1));
5072 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5073 && tree_int_cst_lt (constructor_max_index, constructor_index))
5074 process_init_element (pop_init_level (1));
5075 else
5076 break;
5079 /* Structure elements may require alignment. Do this now if necessary
5080 for the subaggregate, and if it comes next in sequence. Don't do
5081 this for subaggregates that will go on the pending list. */
5082 if (constructor_incremental && constructor_type != 0
5083 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields
5084 && constructor_fields == constructor_unfilled_fields)
5086 /* Advance to offset of this element. */
5087 if (! tree_int_cst_equal (constructor_bit_index,
5088 DECL_FIELD_BITPOS (constructor_fields)))
5090 /* By using unsigned arithmetic, the result will be correct even
5091 in case of overflows, if BITS_PER_UNIT is a power of two. */
5092 unsigned next = (TREE_INT_CST_LOW
5093 (DECL_FIELD_BITPOS (constructor_fields))
5094 / (unsigned)BITS_PER_UNIT);
5095 unsigned here = (TREE_INT_CST_LOW (constructor_bit_index)
5096 / (unsigned)BITS_PER_UNIT);
5098 assemble_zeros ((next - here)
5099 * (unsigned)BITS_PER_UNIT
5100 / (unsigned)BITS_PER_UNIT);
5102 /* Indicate that we have now filled the structure up to the current
5103 field. */
5104 constructor_unfilled_fields = constructor_fields;
5107 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5108 p->type = constructor_type;
5109 p->fields = constructor_fields;
5110 p->index = constructor_index;
5111 p->range_end = constructor_range_end;
5112 p->max_index = constructor_max_index;
5113 p->unfilled_index = constructor_unfilled_index;
5114 p->unfilled_fields = constructor_unfilled_fields;
5115 p->bit_index = constructor_bit_index;
5116 p->elements = constructor_elements;
5117 p->constant = constructor_constant;
5118 p->simple = constructor_simple;
5119 p->erroneous = constructor_erroneous;
5120 p->pending_elts = constructor_pending_elts;
5121 p->depth = constructor_depth;
5122 p->replacement_value = 0;
5123 p->implicit = implicit;
5124 p->incremental = constructor_incremental;
5125 p->outer = 0;
5126 p->next = constructor_stack;
5127 constructor_stack = p;
5129 constructor_constant = 1;
5130 constructor_simple = 1;
5131 constructor_depth = SPELLING_DEPTH ();
5132 constructor_elements = 0;
5133 constructor_pending_elts = 0;
5135 /* Don't die if an entire brace-pair level is superfluous
5136 in the containing level. */
5137 if (constructor_type == 0)
5139 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5140 || TREE_CODE (constructor_type) == UNION_TYPE)
5142 /* Don't die if there are extra init elts at the end. */
5143 if (constructor_fields == 0)
5144 constructor_type = 0;
5145 else
5147 constructor_type = TREE_TYPE (constructor_fields);
5148 push_member_name (constructor_fields);
5149 constructor_depth++;
5150 if (constructor_fields != constructor_unfilled_fields)
5151 constructor_incremental = 0;
5154 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5156 constructor_type = TREE_TYPE (constructor_type);
5157 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5158 constructor_depth++;
5159 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5160 || constructor_range_end != 0)
5161 constructor_incremental = 0;
5164 if (constructor_type == 0)
5166 error_init ("extra brace group at end of initializer");
5167 constructor_fields = 0;
5168 constructor_unfilled_fields = 0;
5169 return;
5172 /* Turn off constructor_incremental if type is a struct with bitfields. */
5173 check_init_type_bitfields (constructor_type);
5175 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5177 missing_braces_mentioned = 1;
5178 warning_init ("missing braces around initializer");
5181 if (TREE_CODE (constructor_type) == RECORD_TYPE
5182 || TREE_CODE (constructor_type) == UNION_TYPE)
5184 constructor_fields = TYPE_FIELDS (constructor_type);
5185 /* Skip any nameless bit fields at the beginning. */
5186 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5187 && DECL_NAME (constructor_fields) == 0)
5188 constructor_fields = TREE_CHAIN (constructor_fields);
5189 constructor_unfilled_fields = constructor_fields;
5190 constructor_bit_index = copy_node (integer_zero_node);
5191 TREE_TYPE (constructor_bit_index) = sbitsizetype;
5193 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5195 constructor_range_end = 0;
5196 if (TYPE_DOMAIN (constructor_type))
5198 constructor_max_index
5199 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5200 constructor_index
5201 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5203 else
5204 constructor_index = copy_node (integer_zero_node);
5205 constructor_unfilled_index = copy_node (constructor_index);
5207 else
5209 warning_init ("braces around scalar initializer");
5210 constructor_fields = constructor_type;
5211 constructor_unfilled_fields = constructor_type;
5215 /* Don't read a struct incrementally if it has any bitfields,
5216 because the incremental reading code doesn't know how to
5217 handle bitfields yet. */
5219 static void
5220 check_init_type_bitfields (type)
5221 tree type;
5223 if (TREE_CODE (type) == RECORD_TYPE)
5225 tree tail;
5226 for (tail = TYPE_FIELDS (type); tail;
5227 tail = TREE_CHAIN (tail))
5229 if (DECL_C_BIT_FIELD (tail))
5231 constructor_incremental = 0;
5232 break;
5235 check_init_type_bitfields (TREE_TYPE (tail));
5239 else if (TREE_CODE (type) == UNION_TYPE)
5241 tree tail = TYPE_FIELDS (type);
5242 if (tail && DECL_C_BIT_FIELD (tail))
5243 /* We also use the nonincremental algorithm for initiliazation
5244 of unions whose first member is a bitfield, becuase the
5245 incremental algorithm has no code for dealing with
5246 bitfields. */
5247 constructor_incremental = 0;
5250 else if (TREE_CODE (type) == ARRAY_TYPE)
5251 check_init_type_bitfields (TREE_TYPE (type));
5254 /* At the end of an implicit or explicit brace level,
5255 finish up that level of constructor.
5256 If we were outputting the elements as they are read, return 0
5257 from inner levels (process_init_element ignores that),
5258 but return error_mark_node from the outermost level
5259 (that's what we want to put in DECL_INITIAL).
5260 Otherwise, return a CONSTRUCTOR expression. */
5262 tree
5263 pop_init_level (implicit)
5264 int implicit;
5266 struct constructor_stack *p;
5267 int size = 0;
5268 tree constructor = 0;
5270 if (implicit == 0)
5272 /* When we come to an explicit close brace,
5273 pop any inner levels that didn't have explicit braces. */
5274 while (constructor_stack->implicit)
5275 process_init_element (pop_init_level (1));
5278 p = constructor_stack;
5280 if (constructor_type != 0)
5281 size = int_size_in_bytes (constructor_type);
5283 /* Warn when some struct elements are implicitly initialized to zero. */
5284 if (extra_warnings
5285 && constructor_type
5286 && TREE_CODE (constructor_type) == RECORD_TYPE
5287 && constructor_unfilled_fields)
5289 push_member_name (constructor_unfilled_fields);
5290 warning_init ("missing initializer");
5291 RESTORE_SPELLING_DEPTH (constructor_depth);
5294 /* Now output all pending elements. */
5295 output_pending_init_elements (1);
5297 #if 0 /* c-parse.in warns about {}. */
5298 /* In ANSI, each brace level must have at least one element. */
5299 if (! implicit && pedantic
5300 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5301 ? integer_zerop (constructor_unfilled_index)
5302 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5303 pedwarn_init ("empty braces in initializer");
5304 #endif
5306 /* Pad out the end of the structure. */
5308 if (p->replacement_value)
5310 /* If this closes a superfluous brace pair,
5311 just pass out the element between them. */
5312 constructor = p->replacement_value;
5313 /* If this is the top level thing within the initializer,
5314 and it's for a variable, then since we already called
5315 assemble_variable, we must output the value now. */
5316 if (p->next == 0 && constructor_decl != 0
5317 && constructor_incremental)
5319 constructor = digest_init (constructor_type, constructor,
5320 require_constant_value,
5321 require_constant_elements);
5323 /* If initializing an array of unknown size,
5324 determine the size now. */
5325 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5326 && TYPE_DOMAIN (constructor_type) == 0)
5328 int failure;
5329 int momentary_p;
5331 push_obstacks_nochange ();
5332 if (TREE_PERMANENT (constructor_type))
5333 end_temporary_allocation ();
5335 momentary_p = suspend_momentary ();
5337 /* We shouldn't have an incomplete array type within
5338 some other type. */
5339 if (constructor_stack->next)
5340 abort ();
5342 failure
5343 = complete_array_type (constructor_type,
5344 constructor, 0);
5345 if (failure)
5346 abort ();
5348 size = int_size_in_bytes (constructor_type);
5349 resume_momentary (momentary_p);
5350 pop_obstacks ();
5353 output_constant (constructor, size);
5356 else if (constructor_type == 0)
5358 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5359 && TREE_CODE (constructor_type) != UNION_TYPE
5360 && TREE_CODE (constructor_type) != ARRAY_TYPE
5361 && ! constructor_incremental)
5363 /* A nonincremental scalar initializer--just return
5364 the element, after verifying there is just one. */
5365 if (constructor_elements == 0)
5367 error_init ("empty scalar initializer");
5368 constructor = error_mark_node;
5370 else if (TREE_CHAIN (constructor_elements) != 0)
5372 error_init ("extra elements in scalar initializer");
5373 constructor = TREE_VALUE (constructor_elements);
5375 else
5376 constructor = TREE_VALUE (constructor_elements);
5378 else if (! constructor_incremental)
5380 if (constructor_erroneous)
5381 constructor = error_mark_node;
5382 else
5384 int momentary = suspend_momentary ();
5386 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5387 nreverse (constructor_elements));
5388 if (constructor_constant)
5389 TREE_CONSTANT (constructor) = 1;
5390 if (constructor_constant && constructor_simple)
5391 TREE_STATIC (constructor) = 1;
5393 resume_momentary (momentary);
5396 else
5398 tree filled;
5399 int momentary = suspend_momentary ();
5401 if (TREE_CODE (constructor_type) == RECORD_TYPE
5402 || TREE_CODE (constructor_type) == UNION_TYPE)
5404 /* Find the offset of the end of that field. */
5405 filled = size_binop (CEIL_DIV_EXPR,
5406 constructor_bit_index,
5407 size_int (BITS_PER_UNIT));
5409 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5411 /* If initializing an array of unknown size,
5412 determine the size now. */
5413 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5414 && TYPE_DOMAIN (constructor_type) == 0)
5416 tree maxindex
5417 = size_binop (MINUS_EXPR,
5418 constructor_unfilled_index,
5419 integer_one_node);
5421 push_obstacks_nochange ();
5422 if (TREE_PERMANENT (constructor_type))
5423 end_temporary_allocation ();
5424 maxindex = copy_node (maxindex);
5425 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5426 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5428 /* TYPE_MAX_VALUE is always one less than the number of elements
5429 in the array, because we start counting at zero. Therefore,
5430 warn only if the value is less than zero. */
5431 if (pedantic
5432 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5433 < 0))
5434 error_with_decl (constructor_decl,
5435 "zero or negative array size `%s'");
5436 layout_type (constructor_type);
5437 size = int_size_in_bytes (constructor_type);
5438 pop_obstacks ();
5441 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5442 size_in_bytes (TREE_TYPE (constructor_type)));
5444 else
5445 filled = 0;
5447 if (filled != 0)
5448 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5450 resume_momentary (momentary);
5454 constructor_type = p->type;
5455 constructor_fields = p->fields;
5456 constructor_index = p->index;
5457 constructor_range_end = p->range_end;
5458 constructor_max_index = p->max_index;
5459 constructor_unfilled_index = p->unfilled_index;
5460 constructor_unfilled_fields = p->unfilled_fields;
5461 constructor_bit_index = p->bit_index;
5462 constructor_elements = p->elements;
5463 constructor_constant = p->constant;
5464 constructor_simple = p->simple;
5465 constructor_erroneous = p->erroneous;
5466 constructor_pending_elts = p->pending_elts;
5467 constructor_depth = p->depth;
5468 constructor_incremental = p->incremental;
5469 RESTORE_SPELLING_DEPTH (constructor_depth);
5471 constructor_stack = p->next;
5472 free (p);
5474 if (constructor == 0)
5476 if (constructor_stack == 0)
5477 return error_mark_node;
5478 return NULL_TREE;
5480 return constructor;
5483 /* Within an array initializer, specify the next index to be initialized.
5484 FIRST is that index. If LAST is nonzero, then initialize a range
5485 of indices, running from FIRST through LAST. */
5487 void
5488 set_init_index (first, last)
5489 tree first, last;
5491 while ((TREE_CODE (first) == NOP_EXPR
5492 || TREE_CODE (first) == CONVERT_EXPR
5493 || TREE_CODE (first) == NON_LVALUE_EXPR)
5494 && (TYPE_MODE (TREE_TYPE (first))
5495 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5496 (first) = TREE_OPERAND (first, 0);
5497 if (last)
5498 while ((TREE_CODE (last) == NOP_EXPR
5499 || TREE_CODE (last) == CONVERT_EXPR
5500 || TREE_CODE (last) == NON_LVALUE_EXPR)
5501 && (TYPE_MODE (TREE_TYPE (last))
5502 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5503 (last) = TREE_OPERAND (last, 0);
5505 if (TREE_CODE (first) != INTEGER_CST)
5506 error_init ("nonconstant array index in initializer");
5507 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5508 error_init ("nonconstant array index in initializer");
5509 else if (! constructor_unfilled_index)
5510 error_init ("array index in non-array initializer");
5511 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5512 error_init ("duplicate array index in initializer");
5513 else
5515 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (first);
5516 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (first);
5518 if (last != 0 && tree_int_cst_lt (last, first))
5519 error_init ("empty index range in initializer");
5520 else
5522 if (pedantic)
5523 pedwarn ("ANSI C forbids specifying element to initialize");
5524 constructor_range_end = last;
5529 /* Within a struct initializer, specify the next field to be initialized. */
5531 void
5532 set_init_label (fieldname)
5533 tree fieldname;
5535 tree tail;
5536 int passed = 0;
5538 /* Don't die if an entire brace-pair level is superfluous
5539 in the containing level. */
5540 if (constructor_type == 0)
5541 return;
5543 for (tail = TYPE_FIELDS (constructor_type); tail;
5544 tail = TREE_CHAIN (tail))
5546 if (tail == constructor_unfilled_fields)
5547 passed = 1;
5548 if (DECL_NAME (tail) == fieldname)
5549 break;
5552 if (tail == 0)
5553 error ("unknown field `%s' specified in initializer",
5554 IDENTIFIER_POINTER (fieldname));
5555 else if (!passed)
5556 error ("field `%s' already initialized",
5557 IDENTIFIER_POINTER (fieldname));
5558 else
5560 constructor_fields = tail;
5561 if (pedantic)
5562 pedwarn ("ANSI C forbids specifying structure member to initialize");
5566 /* Add a new initializer to the tree of pending initializers. PURPOSE
5567 indentifies the initializer, either array index or field in a structure.
5568 VALUE is the value of that index or field. */
5570 static void
5571 add_pending_init (purpose, value)
5572 tree purpose, value;
5574 struct init_node *p, **q, *r;
5576 q = &constructor_pending_elts;
5577 p = 0;
5579 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5581 while (*q != 0)
5583 p = *q;
5584 if (tree_int_cst_lt (purpose, p->purpose))
5585 q = &p->left;
5586 else if (tree_int_cst_lt (p->purpose, purpose))
5587 q = &p->right;
5588 else
5589 abort ();
5592 else
5594 while (*q != NULL)
5596 p = *q;
5597 if (tree_int_cst_lt (DECL_FIELD_BITPOS (purpose),
5598 DECL_FIELD_BITPOS (p->purpose)))
5599 q = &p->left;
5600 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (p->purpose),
5601 DECL_FIELD_BITPOS (purpose)))
5602 q = &p->right;
5603 else
5604 abort ();
5608 r = (struct init_node *) oballoc (sizeof (struct init_node));
5609 r->purpose = purpose;
5610 r->value = value;
5612 *q = r;
5613 r->parent = p;
5614 r->left = 0;
5615 r->right = 0;
5616 r->balance = 0;
5618 while (p)
5620 struct init_node *s;
5622 if (r == p->left)
5624 if (p->balance == 0)
5625 p->balance = -1;
5626 else if (p->balance < 0)
5628 if (r->balance < 0)
5630 /* L rotation. */
5631 p->left = r->right;
5632 if (p->left)
5633 p->left->parent = p;
5634 r->right = p;
5636 p->balance = 0;
5637 r->balance = 0;
5639 s = p->parent;
5640 p->parent = r;
5641 r->parent = s;
5642 if (s)
5644 if (s->left == p)
5645 s->left = r;
5646 else
5647 s->right = r;
5649 else
5650 constructor_pending_elts = r;
5652 else
5654 /* LR rotation. */
5655 struct init_node *t = r->right;
5657 r->right = t->left;
5658 if (r->right)
5659 r->right->parent = r;
5660 t->left = r;
5662 p->left = t->right;
5663 if (p->left)
5664 p->left->parent = p;
5665 t->right = p;
5667 p->balance = t->balance < 0;
5668 r->balance = -(t->balance > 0);
5669 t->balance = 0;
5671 s = p->parent;
5672 p->parent = t;
5673 r->parent = t;
5674 t->parent = s;
5675 if (s)
5677 if (s->left == p)
5678 s->left = t;
5679 else
5680 s->right = t;
5682 else
5683 constructor_pending_elts = t;
5685 break;
5687 else
5689 /* p->balance == +1; growth of left side balances the node. */
5690 p->balance = 0;
5691 break;
5694 else /* r == p->right */
5696 if (p->balance == 0)
5697 /* Growth propagation from right side. */
5698 p->balance++;
5699 else if (p->balance > 0)
5701 if (r->balance > 0)
5703 /* R rotation. */
5704 p->right = r->left;
5705 if (p->right)
5706 p->right->parent = p;
5707 r->left = p;
5709 p->balance = 0;
5710 r->balance = 0;
5712 s = p->parent;
5713 p->parent = r;
5714 r->parent = s;
5715 if (s)
5717 if (s->left == p)
5718 s->left = r;
5719 else
5720 s->right = r;
5722 else
5723 constructor_pending_elts = r;
5725 else /* r->balance == -1 */
5727 /* RL rotation */
5728 struct init_node *t = r->left;
5730 r->left = t->right;
5731 if (r->left)
5732 r->left->parent = r;
5733 t->right = r;
5735 p->right = t->left;
5736 if (p->right)
5737 p->right->parent = p;
5738 t->left = p;
5740 r->balance = (t->balance < 0);
5741 p->balance = -(t->balance > 0);
5742 t->balance = 0;
5744 s = p->parent;
5745 p->parent = t;
5746 r->parent = t;
5747 t->parent = s;
5748 if (s)
5750 if (s->left == p)
5751 s->left = t;
5752 else
5753 s->right = t;
5755 else
5756 constructor_pending_elts = t;
5758 break;
5760 else
5762 /* p->balance == -1; growth of right side balances the node. */
5763 p->balance = 0;
5764 break;
5768 r = p;
5769 p = p->parent;
5773 /* Return nonzero if FIELD is equal to the index of a pending initializer. */
5775 static int
5776 pending_init_member (field)
5777 tree field;
5779 struct init_node *p;
5781 p = constructor_pending_elts;
5782 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5784 while (p)
5786 if (tree_int_cst_equal (field, p->purpose))
5787 return 1;
5788 else if (tree_int_cst_lt (field, p->purpose))
5789 p = p->left;
5790 else
5791 p = p->right;
5794 else
5796 while (p)
5798 if (field == p->purpose)
5799 return 1;
5800 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (field),
5801 DECL_FIELD_BITPOS (p->purpose)))
5802 p = p->left;
5803 else
5804 p = p->right;
5808 return 0;
5811 /* "Output" the next constructor element.
5812 At top level, really output it to assembler code now.
5813 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5814 TYPE is the data type that the containing data type wants here.
5815 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5817 PENDING if non-nil means output pending elements that belong
5818 right after this element. (PENDING is normally 1;
5819 it is 0 while outputting pending elements, to avoid recursion.) */
5821 static void
5822 output_init_element (value, type, field, pending)
5823 tree value, type, field;
5824 int pending;
5826 int duplicate = 0;
5828 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5829 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5830 && !(TREE_CODE (value) == STRING_CST
5831 && TREE_CODE (type) == ARRAY_TYPE
5832 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5833 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5834 TYPE_MAIN_VARIANT (type))))
5835 value = default_conversion (value);
5837 if (value == error_mark_node)
5838 constructor_erroneous = 1;
5839 else if (!TREE_CONSTANT (value))
5840 constructor_constant = 0;
5841 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5842 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5843 || TREE_CODE (constructor_type) == UNION_TYPE)
5844 && DECL_C_BIT_FIELD (field)
5845 && TREE_CODE (value) != INTEGER_CST))
5846 constructor_simple = 0;
5848 if (require_constant_value && ! TREE_CONSTANT (value))
5850 error_init ("initializer element is not constant");
5851 value = error_mark_node;
5853 else if (require_constant_elements
5854 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5856 error_init ("initializer element is not computable at load time");
5857 value = error_mark_node;
5860 /* If this element duplicates one on constructor_pending_elts,
5861 print a message and ignore it. Don't do this when we're
5862 processing elements taken off constructor_pending_elts,
5863 because we'd always get spurious errors. */
5864 if (pending)
5866 if (TREE_CODE (constructor_type) == RECORD_TYPE
5867 || TREE_CODE (constructor_type) == UNION_TYPE
5868 || TREE_CODE (constructor_type) == ARRAY_TYPE)
5870 if (pending_init_member (field))
5872 error_init ("duplicate initializer");
5873 duplicate = 1;
5878 /* If this element doesn't come next in sequence,
5879 put it on constructor_pending_elts. */
5880 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5881 && !tree_int_cst_equal (field, constructor_unfilled_index))
5883 if (! duplicate)
5884 /* The copy_node is needed in case field is actually
5885 constructor_index, which is modified in place. */
5886 add_pending_init (copy_node (field),
5887 digest_init (type, value, require_constant_value,
5888 require_constant_elements));
5890 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5891 && field != constructor_unfilled_fields)
5893 /* We do this for records but not for unions. In a union,
5894 no matter which field is specified, it can be initialized
5895 right away since it starts at the beginning of the union. */
5896 if (!duplicate)
5897 add_pending_init (field,
5898 digest_init (type, value, require_constant_value,
5899 require_constant_elements));
5901 else
5903 /* Otherwise, output this element either to
5904 constructor_elements or to the assembler file. */
5906 if (!duplicate)
5908 if (! constructor_incremental)
5910 if (field && TREE_CODE (field) == INTEGER_CST)
5911 field = copy_node (field);
5912 constructor_elements
5913 = tree_cons (field, digest_init (type, value,
5914 require_constant_value,
5915 require_constant_elements),
5916 constructor_elements);
5918 else
5920 /* Structure elements may require alignment.
5921 Do this, if necessary. */
5922 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5924 /* Advance to offset of this element. */
5925 if (! tree_int_cst_equal (constructor_bit_index,
5926 DECL_FIELD_BITPOS (field)))
5928 /* By using unsigned arithmetic, the result will be
5929 correct even in case of overflows, if BITS_PER_UNIT
5930 is a power of two. */
5931 unsigned next = (TREE_INT_CST_LOW
5932 (DECL_FIELD_BITPOS (field))
5933 / (unsigned)BITS_PER_UNIT);
5934 unsigned here = (TREE_INT_CST_LOW
5935 (constructor_bit_index)
5936 / (unsigned)BITS_PER_UNIT);
5938 assemble_zeros ((next - here)
5939 * (unsigned)BITS_PER_UNIT
5940 / (unsigned)BITS_PER_UNIT);
5943 output_constant (digest_init (type, value,
5944 require_constant_value,
5945 require_constant_elements),
5946 int_size_in_bytes (type));
5948 /* For a record or union,
5949 keep track of end position of last field. */
5950 if (TREE_CODE (constructor_type) == RECORD_TYPE
5951 || TREE_CODE (constructor_type) == UNION_TYPE)
5953 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
5954 DECL_SIZE (field));
5955 TREE_INT_CST_LOW (constructor_bit_index)
5956 = TREE_INT_CST_LOW (temp);
5957 TREE_INT_CST_HIGH (constructor_bit_index)
5958 = TREE_INT_CST_HIGH (temp);
5963 /* Advance the variable that indicates sequential elements output. */
5964 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5966 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
5967 integer_one_node);
5968 TREE_INT_CST_LOW (constructor_unfilled_index)
5969 = TREE_INT_CST_LOW (tem);
5970 TREE_INT_CST_HIGH (constructor_unfilled_index)
5971 = TREE_INT_CST_HIGH (tem);
5973 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5975 constructor_unfilled_fields =
5976 TREE_CHAIN (constructor_unfilled_fields);
5977 /* Skip any nameless bit fields. */
5978 while (constructor_unfilled_fields != 0
5979 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5980 && DECL_NAME (constructor_unfilled_fields) == 0)
5981 constructor_unfilled_fields =
5982 TREE_CHAIN (constructor_unfilled_fields);
5984 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5985 constructor_unfilled_fields = 0;
5987 /* Now output any pending elements which have become next. */
5988 if (pending)
5989 output_pending_init_elements (0);
5993 /* Output any pending elements which have become next.
5994 As we output elements, constructor_unfilled_{fields,index}
5995 advances, which may cause other elements to become next;
5996 if so, they too are output.
5998 If ALL is 0, we return when there are
5999 no more pending elements to output now.
6001 If ALL is 1, we output space as necessary so that
6002 we can output all the pending elements. */
6004 static void
6005 output_pending_init_elements (all)
6006 int all;
6008 struct init_node *elt = constructor_pending_elts;
6009 tree next;
6011 retry:
6013 /* Look thru the whole pending tree.
6014 If we find an element that should be output now,
6015 output it. Otherwise, set NEXT to the element
6016 that comes first among those still pending. */
6018 next = 0;
6019 while (elt)
6021 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6023 if (tree_int_cst_equal (elt->purpose,
6024 constructor_unfilled_index))
6025 output_init_element (elt->value,
6026 TREE_TYPE (constructor_type),
6027 constructor_unfilled_index, 0);
6028 else if (tree_int_cst_lt (constructor_unfilled_index,
6029 elt->purpose))
6031 /* Advance to the next smaller node. */
6032 if (elt->left)
6033 elt = elt->left;
6034 else
6036 /* We have reached the smallest node bigger than the
6037 current unfilled index. Fill the space first. */
6038 next = elt->purpose;
6039 break;
6042 else
6044 /* Advance to the next bigger node. */
6045 if (elt->right)
6046 elt = elt->right;
6047 else
6049 /* We have reached the biggest node in a subtree. Find
6050 the parent of it, which is the next bigger node. */
6051 while (elt->parent && elt->parent->right == elt)
6052 elt = elt->parent;
6053 elt = elt->parent;
6054 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6055 elt->purpose))
6057 next = elt->purpose;
6058 break;
6063 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6064 || TREE_CODE (constructor_type) == UNION_TYPE)
6066 /* If the current record is complete we are done. */
6067 if (constructor_unfilled_fields == 0)
6068 break;
6069 if (elt->purpose == constructor_unfilled_fields)
6071 output_init_element (elt->value,
6072 TREE_TYPE (constructor_unfilled_fields),
6073 constructor_unfilled_fields,
6076 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6077 DECL_FIELD_BITPOS (elt->purpose)))
6079 /* Advance to the next smaller node. */
6080 if (elt->left)
6081 elt = elt->left;
6082 else
6084 /* We have reached the smallest node bigger than the
6085 current unfilled field. Fill the space first. */
6086 next = elt->purpose;
6087 break;
6090 else
6092 /* Advance to the next bigger node. */
6093 if (elt->right)
6094 elt = elt->right;
6095 else
6097 /* We have reached the biggest node in a subtree. Find
6098 the parent of it, which is the next bigger node. */
6099 while (elt->parent && elt->parent->right == elt)
6100 elt = elt->parent;
6101 elt = elt->parent;
6102 if (elt
6103 && tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6104 DECL_FIELD_BITPOS (elt->purpose)))
6106 next = elt->purpose;
6107 break;
6114 /* Ordinarily return, but not if we want to output all
6115 and there are elements left. */
6116 if (! (all && next != 0))
6117 return;
6119 /* Generate space up to the position of NEXT. */
6120 if (constructor_incremental)
6122 tree filled;
6123 tree nextpos_tree = size_int (0);
6125 if (TREE_CODE (constructor_type) == RECORD_TYPE
6126 || TREE_CODE (constructor_type) == UNION_TYPE)
6128 tree tail;
6129 /* Find the last field written out, if any. */
6130 for (tail = TYPE_FIELDS (constructor_type); tail;
6131 tail = TREE_CHAIN (tail))
6132 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
6133 break;
6135 if (tail)
6136 /* Find the offset of the end of that field. */
6137 filled = size_binop (CEIL_DIV_EXPR,
6138 size_binop (PLUS_EXPR,
6139 DECL_FIELD_BITPOS (tail),
6140 DECL_SIZE (tail)),
6141 size_int (BITS_PER_UNIT));
6142 else
6143 filled = size_int (0);
6145 nextpos_tree = size_binop (CEIL_DIV_EXPR,
6146 DECL_FIELD_BITPOS (next),
6147 size_int (BITS_PER_UNIT));
6149 TREE_INT_CST_HIGH (constructor_bit_index)
6150 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
6151 TREE_INT_CST_LOW (constructor_bit_index)
6152 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
6153 constructor_unfilled_fields = next;
6155 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6157 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
6158 size_in_bytes (TREE_TYPE (constructor_type)));
6159 nextpos_tree
6160 = size_binop (MULT_EXPR, next,
6161 size_in_bytes (TREE_TYPE (constructor_type)));
6162 TREE_INT_CST_LOW (constructor_unfilled_index)
6163 = TREE_INT_CST_LOW (next);
6164 TREE_INT_CST_HIGH (constructor_unfilled_index)
6165 = TREE_INT_CST_HIGH (next);
6167 else
6168 filled = 0;
6170 if (filled)
6172 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
6174 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
6177 else
6179 /* If it's not incremental, just skip over the gap,
6180 so that after jumping to retry we will output the next
6181 successive element. */
6182 if (TREE_CODE (constructor_type) == RECORD_TYPE
6183 || TREE_CODE (constructor_type) == UNION_TYPE)
6184 constructor_unfilled_fields = next;
6185 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6187 TREE_INT_CST_LOW (constructor_unfilled_index)
6188 = TREE_INT_CST_LOW (next);
6189 TREE_INT_CST_HIGH (constructor_unfilled_index)
6190 = TREE_INT_CST_HIGH (next);
6194 /* ELT now points to the node in the pending tree with the next
6195 initializer to output. */
6196 goto retry;
6199 /* Add one non-braced element to the current constructor level.
6200 This adjusts the current position within the constructor's type.
6201 This may also start or terminate implicit levels
6202 to handle a partly-braced initializer.
6204 Once this has found the correct level for the new element,
6205 it calls output_init_element.
6207 Note: if we are incrementally outputting this constructor,
6208 this function may be called with a null argument
6209 representing a sub-constructor that was already incrementally output.
6210 When that happens, we output nothing, but we do the bookkeeping
6211 to skip past that element of the current constructor. */
6213 void
6214 process_init_element (value)
6215 tree value;
6217 tree orig_value = value;
6218 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6220 /* Handle superfluous braces around string cst as in
6221 char x[] = {"foo"}; */
6222 if (string_flag
6223 && constructor_type
6224 && TREE_CODE (constructor_type) == ARRAY_TYPE
6225 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6226 && integer_zerop (constructor_unfilled_index))
6228 if (constructor_stack->replacement_value)
6229 error_init ("excess elements in char array initializer");
6230 constructor_stack->replacement_value = value;
6231 return;
6234 if (constructor_stack->replacement_value != 0)
6236 error_init ("excess elements in struct initializer");
6237 return;
6240 /* Ignore elements of a brace group if it is entirely superfluous
6241 and has already been diagnosed. */
6242 if (constructor_type == 0)
6243 return;
6245 /* If we've exhausted any levels that didn't have braces,
6246 pop them now. */
6247 while (constructor_stack->implicit)
6249 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6250 || TREE_CODE (constructor_type) == UNION_TYPE)
6251 && constructor_fields == 0)
6252 process_init_element (pop_init_level (1));
6253 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6254 && (constructor_max_index == 0
6255 || tree_int_cst_lt (constructor_max_index,
6256 constructor_index)))
6257 process_init_element (pop_init_level (1));
6258 else
6259 break;
6262 while (1)
6264 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6266 tree fieldtype;
6267 enum tree_code fieldcode;
6269 if (constructor_fields == 0)
6271 pedwarn_init ("excess elements in struct initializer");
6272 break;
6275 fieldtype = TREE_TYPE (constructor_fields);
6276 if (fieldtype != error_mark_node)
6277 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6278 fieldcode = TREE_CODE (fieldtype);
6280 /* Accept a string constant to initialize a subarray. */
6281 if (value != 0
6282 && fieldcode == ARRAY_TYPE
6283 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6284 && string_flag)
6285 value = orig_value;
6286 /* Otherwise, if we have come to a subaggregate,
6287 and we don't have an element of its type, push into it. */
6288 else if (value != 0 && !constructor_no_implicit
6289 && value != error_mark_node
6290 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6291 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6292 || fieldcode == UNION_TYPE))
6294 push_init_level (1);
6295 continue;
6298 if (value)
6300 push_member_name (constructor_fields);
6301 output_init_element (value, fieldtype, constructor_fields, 1);
6302 RESTORE_SPELLING_DEPTH (constructor_depth);
6304 else
6305 /* Do the bookkeeping for an element that was
6306 directly output as a constructor. */
6308 /* For a record, keep track of end position of last field. */
6309 tree temp = size_binop (PLUS_EXPR,
6310 DECL_FIELD_BITPOS (constructor_fields),
6311 DECL_SIZE (constructor_fields));
6312 TREE_INT_CST_LOW (constructor_bit_index)
6313 = TREE_INT_CST_LOW (temp);
6314 TREE_INT_CST_HIGH (constructor_bit_index)
6315 = TREE_INT_CST_HIGH (temp);
6317 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6318 /* Skip any nameless bit fields. */
6319 while (constructor_unfilled_fields != 0
6320 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6321 && DECL_NAME (constructor_unfilled_fields) == 0)
6322 constructor_unfilled_fields =
6323 TREE_CHAIN (constructor_unfilled_fields);
6326 constructor_fields = TREE_CHAIN (constructor_fields);
6327 /* Skip any nameless bit fields at the beginning. */
6328 while (constructor_fields != 0
6329 && DECL_C_BIT_FIELD (constructor_fields)
6330 && DECL_NAME (constructor_fields) == 0)
6331 constructor_fields = TREE_CHAIN (constructor_fields);
6332 break;
6334 if (TREE_CODE (constructor_type) == UNION_TYPE)
6336 tree fieldtype;
6337 enum tree_code fieldcode;
6339 if (constructor_fields == 0)
6341 pedwarn_init ("excess elements in union initializer");
6342 break;
6345 fieldtype = TREE_TYPE (constructor_fields);
6346 if (fieldtype != error_mark_node)
6347 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6348 fieldcode = TREE_CODE (fieldtype);
6350 /* Accept a string constant to initialize a subarray. */
6351 if (value != 0
6352 && fieldcode == ARRAY_TYPE
6353 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6354 && string_flag)
6355 value = orig_value;
6356 /* Otherwise, if we have come to a subaggregate,
6357 and we don't have an element of its type, push into it. */
6358 else if (value != 0 && !constructor_no_implicit
6359 && value != error_mark_node
6360 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6361 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6362 || fieldcode == UNION_TYPE))
6364 push_init_level (1);
6365 continue;
6368 if (value)
6370 push_member_name (constructor_fields);
6371 output_init_element (value, fieldtype, constructor_fields, 1);
6372 RESTORE_SPELLING_DEPTH (constructor_depth);
6374 else
6375 /* Do the bookkeeping for an element that was
6376 directly output as a constructor. */
6378 TREE_INT_CST_LOW (constructor_bit_index)
6379 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6380 TREE_INT_CST_HIGH (constructor_bit_index)
6381 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6383 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6386 constructor_fields = 0;
6387 break;
6389 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6391 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6392 enum tree_code eltcode = TREE_CODE (elttype);
6394 /* Accept a string constant to initialize a subarray. */
6395 if (value != 0
6396 && eltcode == ARRAY_TYPE
6397 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6398 && string_flag)
6399 value = orig_value;
6400 /* Otherwise, if we have come to a subaggregate,
6401 and we don't have an element of its type, push into it. */
6402 else if (value != 0 && !constructor_no_implicit
6403 && value != error_mark_node
6404 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6405 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6406 || eltcode == UNION_TYPE))
6408 push_init_level (1);
6409 continue;
6412 if (constructor_max_index != 0
6413 && tree_int_cst_lt (constructor_max_index, constructor_index))
6415 pedwarn_init ("excess elements in array initializer");
6416 break;
6419 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */
6420 if (constructor_range_end)
6422 if (constructor_max_index != 0
6423 && tree_int_cst_lt (constructor_max_index,
6424 constructor_range_end))
6426 pedwarn_init ("excess elements in array initializer");
6427 TREE_INT_CST_HIGH (constructor_range_end)
6428 = TREE_INT_CST_HIGH (constructor_max_index);
6429 TREE_INT_CST_LOW (constructor_range_end)
6430 = TREE_INT_CST_LOW (constructor_max_index);
6433 value = save_expr (value);
6436 /* Now output the actual element.
6437 Ordinarily, output once.
6438 If there is a range, repeat it till we advance past the range. */
6441 tree tem;
6443 if (value)
6445 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6446 output_init_element (value, elttype, constructor_index, 1);
6447 RESTORE_SPELLING_DEPTH (constructor_depth);
6450 tem = size_binop (PLUS_EXPR, constructor_index,
6451 integer_one_node);
6452 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (tem);
6453 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (tem);
6455 if (!value)
6456 /* If we are doing the bookkeeping for an element that was
6457 directly output as a constructor,
6458 we must update constructor_unfilled_index. */
6460 TREE_INT_CST_LOW (constructor_unfilled_index)
6461 = TREE_INT_CST_LOW (constructor_index);
6462 TREE_INT_CST_HIGH (constructor_unfilled_index)
6463 = TREE_INT_CST_HIGH (constructor_index);
6466 while (! (constructor_range_end == 0
6467 || tree_int_cst_lt (constructor_range_end,
6468 constructor_index)));
6470 break;
6473 /* Handle the sole element allowed in a braced initializer
6474 for a scalar variable. */
6475 if (constructor_fields == 0)
6477 pedwarn_init ("excess elements in scalar initializer");
6478 break;
6481 if (value)
6482 output_init_element (value, constructor_type, NULL_TREE, 1);
6483 constructor_fields = 0;
6484 break;
6487 /* If the (lexically) previous elments are not now saved,
6488 we can discard the storage for them. */
6489 if (constructor_incremental && constructor_pending_elts == 0 && value != 0
6490 && constructor_stack == 0)
6491 clear_momentary ();
6494 /* Expand an ASM statement with operands, handling output operands
6495 that are not variables or INDIRECT_REFS by transforming such
6496 cases into cases that expand_asm_operands can handle.
6498 Arguments are same as for expand_asm_operands. */
6500 void
6501 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6502 tree string, outputs, inputs, clobbers;
6503 int vol;
6504 char *filename;
6505 int line;
6507 int noutputs = list_length (outputs);
6508 register int i;
6509 /* o[I] is the place that output number I should be written. */
6510 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6511 register tree tail;
6513 if (TREE_CODE (string) == ADDR_EXPR)
6514 string = TREE_OPERAND (string, 0);
6515 if (TREE_CODE (string) != STRING_CST)
6517 error ("asm template is not a string constant");
6518 return;
6521 /* Record the contents of OUTPUTS before it is modified. */
6522 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6524 tree output = TREE_VALUE (tail);
6526 /* We can remove conversions that just change the type, not the mode. */
6527 STRIP_NOPS (output);
6528 o[i] = output;
6530 /* Allow conversions as LHS here. build_modify_expr as called below
6531 will do the right thing with them. */
6532 while (TREE_CODE (output) == NOP_EXPR
6533 || TREE_CODE (output) == CONVERT_EXPR
6534 || TREE_CODE (output) == FLOAT_EXPR
6535 || TREE_CODE (output) == FIX_TRUNC_EXPR
6536 || TREE_CODE (output) == FIX_FLOOR_EXPR
6537 || TREE_CODE (output) == FIX_ROUND_EXPR
6538 || TREE_CODE (output) == FIX_CEIL_EXPR)
6539 output = TREE_OPERAND (output, 1);
6541 lvalue_or_else (o[i], "invalid lvalue in asm statement");
6544 /* Perform default conversions on array and function inputs. */
6545 /* Don't do this for other types--
6546 it would screw up operands expected to be in memory. */
6547 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6548 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6549 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6550 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6552 /* Generate the ASM_OPERANDS insn;
6553 store into the TREE_VALUEs of OUTPUTS some trees for
6554 where the values were actually stored. */
6555 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6557 /* Copy all the intermediate outputs into the specified outputs. */
6558 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6560 if (o[i] != TREE_VALUE (tail))
6562 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6563 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6564 free_temp_slots ();
6566 /* Detect modification of read-only values.
6567 (Otherwise done by build_modify_expr.) */
6568 else
6570 tree type = TREE_TYPE (o[i]);
6571 if (TREE_READONLY (o[i])
6572 || TYPE_READONLY (type)
6573 || ((TREE_CODE (type) == RECORD_TYPE
6574 || TREE_CODE (type) == UNION_TYPE)
6575 && C_TYPE_FIELDS_READONLY (type)))
6576 readonly_warning (o[i], "modification by `asm'");
6580 /* Those MODIFY_EXPRs could do autoincrements. */
6581 emit_queue ();
6584 /* Expand a C `return' statement.
6585 RETVAL is the expression for what to return,
6586 or a null pointer for `return;' with no value. */
6588 void
6589 c_expand_return (retval)
6590 tree retval;
6592 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6594 if (TREE_THIS_VOLATILE (current_function_decl))
6595 warning ("function declared `noreturn' has a `return' statement");
6597 if (!retval)
6599 current_function_returns_null = 1;
6600 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6601 warning ("`return' with no value, in function returning non-void");
6602 expand_null_return ();
6604 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6606 current_function_returns_null = 1;
6607 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6608 pedwarn ("`return' with a value, in function returning void");
6609 expand_return (retval);
6611 else
6613 tree t = convert_for_assignment (valtype, retval, _("return"),
6614 NULL_TREE, NULL_TREE, 0);
6615 tree res = DECL_RESULT (current_function_decl);
6616 tree inner;
6618 if (t == error_mark_node)
6619 return;
6621 inner = t = convert (TREE_TYPE (res), t);
6623 /* Strip any conversions, additions, and subtractions, and see if
6624 we are returning the address of a local variable. Warn if so. */
6625 while (1)
6627 switch (TREE_CODE (inner))
6629 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6630 case PLUS_EXPR:
6631 inner = TREE_OPERAND (inner, 0);
6632 continue;
6634 case MINUS_EXPR:
6635 /* If the second operand of the MINUS_EXPR has a pointer
6636 type (or is converted from it), this may be valid, so
6637 don't give a warning. */
6639 tree op1 = TREE_OPERAND (inner, 1);
6641 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6642 && (TREE_CODE (op1) == NOP_EXPR
6643 || TREE_CODE (op1) == NON_LVALUE_EXPR
6644 || TREE_CODE (op1) == CONVERT_EXPR))
6645 op1 = TREE_OPERAND (op1, 0);
6647 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6648 break;
6650 inner = TREE_OPERAND (inner, 0);
6651 continue;
6654 case ADDR_EXPR:
6655 inner = TREE_OPERAND (inner, 0);
6657 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6658 inner = TREE_OPERAND (inner, 0);
6660 if (TREE_CODE (inner) == VAR_DECL
6661 && ! DECL_EXTERNAL (inner)
6662 && ! TREE_STATIC (inner)
6663 && DECL_CONTEXT (inner) == current_function_decl)
6664 warning ("function returns address of local variable");
6665 break;
6667 default:
6668 break;
6671 break;
6674 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6675 TREE_SIDE_EFFECTS (t) = 1;
6676 expand_return (t);
6677 current_function_returns_value = 1;
6681 /* Start a C switch statement, testing expression EXP.
6682 Return EXP if it is valid, an error node otherwise. */
6684 tree
6685 c_expand_start_case (exp)
6686 tree exp;
6688 register enum tree_code code;
6689 tree type;
6691 if (TREE_CODE (exp) == ERROR_MARK)
6692 return exp;
6694 code = TREE_CODE (TREE_TYPE (exp));
6695 type = TREE_TYPE (exp);
6697 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6699 error ("switch quantity not an integer");
6700 exp = error_mark_node;
6702 else
6704 tree index;
6705 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6707 if (warn_traditional
6708 && (type == long_integer_type_node
6709 || type == long_unsigned_type_node))
6710 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6712 exp = default_conversion (exp);
6713 type = TREE_TYPE (exp);
6714 index = get_unwidened (exp, NULL_TREE);
6715 /* We can't strip a conversion from a signed type to an unsigned,
6716 because if we did, int_fits_type_p would do the wrong thing
6717 when checking case values for being in range,
6718 and it's too hard to do the right thing. */
6719 if (TREE_UNSIGNED (TREE_TYPE (exp))
6720 == TREE_UNSIGNED (TREE_TYPE (index)))
6721 exp = index;
6724 expand_start_case (1, exp, type, "switch statement");
6726 return exp;