* tm.texi: Fix markup.
[official-gcc.git] / gcc / c-typeck.c
blob9e50c37da69f4a95abeec5e332f9663140848ae5
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
32 #include "config.h"
33 #include "system.h"
34 #include "tree.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "rtl.h"
40 #include "expr.h"
41 #include "toplev.h"
42 #include "intl.h"
43 #include "ggc.h"
44 #include "target.h"
46 /* Nonzero if we've already printed a "missing braces around initializer"
47 message within this initializer. */
48 static int missing_braces_mentioned;
50 /* 1 if we explained undeclared var errors. */
51 static int undeclared_variable_notice;
53 static tree qualify_type PARAMS ((tree, tree));
54 static int comp_target_types PARAMS ((tree, tree));
55 static int function_types_compatible_p PARAMS ((tree, tree));
56 static int type_lists_compatible_p PARAMS ((tree, tree));
57 static tree decl_constant_value_for_broken_optimization PARAMS ((tree));
58 static tree lookup_field PARAMS ((tree, tree, tree *));
59 static tree convert_arguments PARAMS ((tree, tree, tree, tree));
60 static tree pointer_int_sum PARAMS ((enum tree_code, tree, tree));
61 static tree pointer_diff PARAMS ((tree, tree));
62 static tree unary_complex_lvalue PARAMS ((enum tree_code, tree));
63 static void pedantic_lvalue_warning PARAMS ((enum tree_code));
64 static tree internal_build_compound_expr PARAMS ((tree, int));
65 static tree convert_for_assignment PARAMS ((tree, tree, const char *,
66 tree, tree, int));
67 static void warn_for_assignment PARAMS ((const char *, const char *,
68 tree, int));
69 static tree valid_compound_expr_initializer PARAMS ((tree, tree));
70 static void push_string PARAMS ((const char *));
71 static void push_member_name PARAMS ((tree));
72 static void push_array_bounds PARAMS ((int));
73 static int spelling_length PARAMS ((void));
74 static char *print_spelling PARAMS ((char *));
75 static void warning_init PARAMS ((const char *));
76 static tree digest_init PARAMS ((tree, tree, int, int));
77 static void output_init_element PARAMS ((tree, tree, tree, int));
78 static void output_pending_init_elements PARAMS ((int));
79 static int set_designator PARAMS ((int));
80 static void push_range_stack PARAMS ((tree));
81 static void add_pending_init PARAMS ((tree, tree));
82 static void set_nonincremental_init PARAMS ((void));
83 static void set_nonincremental_init_from_string PARAMS ((tree));
84 static tree find_init_member PARAMS ((tree));
86 /* Do `exp = require_complete_type (exp);' to make sure exp
87 does not have an incomplete type. (That includes void types.) */
89 tree
90 require_complete_type (value)
91 tree value;
93 tree type = TREE_TYPE (value);
95 if (TREE_CODE (value) == ERROR_MARK)
96 return error_mark_node;
98 /* First, detect a valid value with a complete type. */
99 if (COMPLETE_TYPE_P (type))
100 return value;
102 incomplete_type_error (value, type);
103 return error_mark_node;
106 /* Print an error message for invalid use of an incomplete type.
107 VALUE is the expression that was used (or 0 if that isn't known)
108 and TYPE is the type that was invalid. */
110 void
111 incomplete_type_error (value, type)
112 tree value;
113 tree type;
115 const char *type_code_string;
117 /* Avoid duplicate error message. */
118 if (TREE_CODE (type) == ERROR_MARK)
119 return;
121 if (value != 0 && (TREE_CODE (value) == VAR_DECL
122 || TREE_CODE (value) == PARM_DECL))
123 error ("`%s' has an incomplete type",
124 IDENTIFIER_POINTER (DECL_NAME (value)));
125 else
127 retry:
128 /* We must print an error message. Be clever about what it says. */
130 switch (TREE_CODE (type))
132 case RECORD_TYPE:
133 type_code_string = "struct";
134 break;
136 case UNION_TYPE:
137 type_code_string = "union";
138 break;
140 case ENUMERAL_TYPE:
141 type_code_string = "enum";
142 break;
144 case VOID_TYPE:
145 error ("invalid use of void expression");
146 return;
148 case ARRAY_TYPE:
149 if (TYPE_DOMAIN (type))
151 type = TREE_TYPE (type);
152 goto retry;
154 error ("invalid use of array with unspecified bounds");
155 return;
157 default:
158 abort ();
161 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
162 error ("invalid use of undefined type `%s %s'",
163 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
164 else
165 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
166 error ("invalid use of incomplete typedef `%s'",
167 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
171 /* Return a variant of TYPE which has all the type qualifiers of LIKE
172 as well as those of TYPE. */
174 static tree
175 qualify_type (type, like)
176 tree type, like;
178 return c_build_qualified_type (type,
179 TYPE_QUALS (type) | TYPE_QUALS (like));
182 /* Return the common type of two types.
183 We assume that comptypes has already been done and returned 1;
184 if that isn't so, this may crash. In particular, we assume that qualifiers
185 match.
187 This is the type for the result of most arithmetic operations
188 if the operands have the given two types. */
190 tree
191 common_type (t1, t2)
192 tree t1, t2;
194 register enum tree_code code1;
195 register enum tree_code code2;
196 tree attributes;
198 /* Save time if the two types are the same. */
200 if (t1 == t2) return t1;
202 /* If one type is nonsense, use the other. */
203 if (t1 == error_mark_node)
204 return t2;
205 if (t2 == error_mark_node)
206 return t1;
208 /* Merge the attributes. */
209 attributes = (*targetm.merge_type_attributes) (t1, t2);
211 /* Treat an enum type as the unsigned integer type of the same width. */
213 if (TREE_CODE (t1) == ENUMERAL_TYPE)
214 t1 = type_for_size (TYPE_PRECISION (t1), 1);
215 if (TREE_CODE (t2) == ENUMERAL_TYPE)
216 t2 = type_for_size (TYPE_PRECISION (t2), 1);
218 code1 = TREE_CODE (t1);
219 code2 = TREE_CODE (t2);
221 /* If one type is complex, form the common type of the non-complex
222 components, then make that complex. Use T1 or T2 if it is the
223 required type. */
224 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
226 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
227 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
228 tree subtype = common_type (subtype1, subtype2);
230 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
231 return build_type_attribute_variant (t1, attributes);
232 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
233 return build_type_attribute_variant (t2, attributes);
234 else
235 return build_type_attribute_variant (build_complex_type (subtype),
236 attributes);
239 switch (code1)
241 case INTEGER_TYPE:
242 case REAL_TYPE:
243 /* If only one is real, use it as the result. */
245 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
246 return build_type_attribute_variant (t1, attributes);
248 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
249 return build_type_attribute_variant (t2, attributes);
251 /* Both real or both integers; use the one with greater precision. */
253 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
254 return build_type_attribute_variant (t1, attributes);
255 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
256 return build_type_attribute_variant (t2, attributes);
258 /* Same precision. Prefer longs to ints even when same size. */
260 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
261 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
262 return build_type_attribute_variant (long_unsigned_type_node,
263 attributes);
265 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
266 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
268 /* But preserve unsignedness from the other type,
269 since long cannot hold all the values of an unsigned int. */
270 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
271 t1 = long_unsigned_type_node;
272 else
273 t1 = long_integer_type_node;
274 return build_type_attribute_variant (t1, attributes);
277 /* Likewise, prefer long double to double even if same size. */
278 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
279 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
280 return build_type_attribute_variant (long_double_type_node,
281 attributes);
283 /* Otherwise prefer the unsigned one. */
285 if (TREE_UNSIGNED (t1))
286 return build_type_attribute_variant (t1, attributes);
287 else
288 return build_type_attribute_variant (t2, attributes);
290 case POINTER_TYPE:
291 /* For two pointers, do this recursively on the target type,
292 and combine the qualifiers of the two types' targets. */
293 /* This code was turned off; I don't know why.
294 But ANSI C specifies doing this with the qualifiers.
295 So I turned it on again. */
297 tree pointed_to_1 = TREE_TYPE (t1);
298 tree pointed_to_2 = TREE_TYPE (t2);
299 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
300 TYPE_MAIN_VARIANT (pointed_to_2));
301 t1 = build_pointer_type (c_build_qualified_type
302 (target,
303 TYPE_QUALS (pointed_to_1) |
304 TYPE_QUALS (pointed_to_2)));
305 return build_type_attribute_variant (t1, attributes);
307 #if 0
308 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
309 return build_type_attribute_variant (t1, attributes);
310 #endif
312 case ARRAY_TYPE:
314 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
315 /* Save space: see if the result is identical to one of the args. */
316 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
317 return build_type_attribute_variant (t1, attributes);
318 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
319 return build_type_attribute_variant (t2, attributes);
320 /* Merge the element types, and have a size if either arg has one. */
321 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
322 return build_type_attribute_variant (t1, attributes);
325 case FUNCTION_TYPE:
326 /* Function types: prefer the one that specified arg types.
327 If both do, merge the arg types. Also merge the return types. */
329 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
330 tree p1 = TYPE_ARG_TYPES (t1);
331 tree p2 = TYPE_ARG_TYPES (t2);
332 int len;
333 tree newargs, n;
334 int i;
336 /* Save space: see if the result is identical to one of the args. */
337 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
338 return build_type_attribute_variant (t1, attributes);
339 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
340 return build_type_attribute_variant (t2, attributes);
342 /* Simple way if one arg fails to specify argument types. */
343 if (TYPE_ARG_TYPES (t1) == 0)
345 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
346 return build_type_attribute_variant (t1, attributes);
348 if (TYPE_ARG_TYPES (t2) == 0)
350 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
351 return build_type_attribute_variant (t1, attributes);
354 /* If both args specify argument types, we must merge the two
355 lists, argument by argument. */
357 pushlevel (0);
358 declare_parm_level (1);
360 len = list_length (p1);
361 newargs = 0;
363 for (i = 0; i < len; i++)
364 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
366 n = newargs;
368 for (; p1;
369 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
371 /* A null type means arg type is not specified.
372 Take whatever the other function type has. */
373 if (TREE_VALUE (p1) == 0)
375 TREE_VALUE (n) = TREE_VALUE (p2);
376 goto parm_done;
378 if (TREE_VALUE (p2) == 0)
380 TREE_VALUE (n) = TREE_VALUE (p1);
381 goto parm_done;
384 /* Given wait (union {union wait *u; int *i} *)
385 and wait (union wait *),
386 prefer union wait * as type of parm. */
387 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
388 && TREE_VALUE (p1) != TREE_VALUE (p2))
390 tree memb;
391 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
392 memb; memb = TREE_CHAIN (memb))
393 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
395 TREE_VALUE (n) = TREE_VALUE (p2);
396 if (pedantic)
397 pedwarn ("function types not truly compatible in ISO C");
398 goto parm_done;
401 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
402 && TREE_VALUE (p2) != TREE_VALUE (p1))
404 tree memb;
405 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
406 memb; memb = TREE_CHAIN (memb))
407 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
409 TREE_VALUE (n) = TREE_VALUE (p1);
410 if (pedantic)
411 pedwarn ("function types not truly compatible in ISO C");
412 goto parm_done;
415 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
416 parm_done: ;
419 poplevel (0, 0, 0);
421 t1 = build_function_type (valtype, newargs);
422 /* ... falls through ... */
425 default:
426 return build_type_attribute_variant (t1, attributes);
431 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
432 or various other operations. Return 2 if they are compatible
433 but a warning may be needed if you use them together. */
436 comptypes (type1, type2)
437 tree type1, type2;
439 register tree t1 = type1;
440 register tree t2 = type2;
441 int attrval, val;
443 /* Suppress errors caused by previously reported errors. */
445 if (t1 == t2 || !t1 || !t2
446 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
447 return 1;
449 /* If either type is the internal version of sizetype, return the
450 language version. */
451 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
452 && TYPE_DOMAIN (t1) != 0)
453 t1 = TYPE_DOMAIN (t1);
455 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
456 && TYPE_DOMAIN (t2) != 0)
457 t2 = TYPE_DOMAIN (t2);
459 /* Treat an enum type as the integer type of the same width and
460 signedness. */
462 if (TREE_CODE (t1) == ENUMERAL_TYPE)
463 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
464 if (TREE_CODE (t2) == ENUMERAL_TYPE)
465 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
467 if (t1 == t2)
468 return 1;
470 /* Different classes of types can't be compatible. */
472 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
474 /* Qualifiers must match. */
476 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
477 return 0;
479 /* Allow for two different type nodes which have essentially the same
480 definition. Note that we already checked for equality of the type
481 qualifiers (just above). */
483 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
484 return 1;
486 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
487 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
488 return 0;
490 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
491 val = 0;
493 switch (TREE_CODE (t1))
495 case POINTER_TYPE:
496 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
497 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
498 break;
500 case FUNCTION_TYPE:
501 val = function_types_compatible_p (t1, t2);
502 break;
504 case ARRAY_TYPE:
506 tree d1 = TYPE_DOMAIN (t1);
507 tree d2 = TYPE_DOMAIN (t2);
508 val = 1;
510 /* Target types must match incl. qualifiers. */
511 if (TREE_TYPE (t1) != TREE_TYPE (t2)
512 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
513 return 0;
515 /* Sizes must match unless one is missing or variable. */
516 if (d1 == 0 || d2 == 0 || d1 == d2
517 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
518 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
519 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
520 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
521 break;
523 if (! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
524 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
525 val = 0;
527 break;
530 case RECORD_TYPE:
531 if (maybe_objc_comptypes (t1, t2, 0) == 1)
532 val = 1;
533 break;
535 default:
536 break;
538 return attrval == 2 && val == 1 ? 2 : val;
541 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
542 ignoring their qualifiers. */
544 static int
545 comp_target_types (ttl, ttr)
546 tree ttl, ttr;
548 int val;
550 /* Give maybe_objc_comptypes a crack at letting these types through. */
551 if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0)
552 return val;
554 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
555 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
557 if (val == 2 && pedantic)
558 pedwarn ("types are not quite compatible");
559 return val;
562 /* Subroutines of `comptypes'. */
564 /* Return 1 if two function types F1 and F2 are compatible.
565 If either type specifies no argument types,
566 the other must specify a fixed number of self-promoting arg types.
567 Otherwise, if one type specifies only the number of arguments,
568 the other must specify that number of self-promoting arg types.
569 Otherwise, the argument types must match. */
571 static int
572 function_types_compatible_p (f1, f2)
573 tree f1, f2;
575 tree args1, args2;
576 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
577 int val = 1;
578 int val1;
580 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
581 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
582 return 0;
584 args1 = TYPE_ARG_TYPES (f1);
585 args2 = TYPE_ARG_TYPES (f2);
587 /* An unspecified parmlist matches any specified parmlist
588 whose argument types don't need default promotions. */
590 if (args1 == 0)
592 if (!self_promoting_args_p (args2))
593 return 0;
594 /* If one of these types comes from a non-prototype fn definition,
595 compare that with the other type's arglist.
596 If they don't match, ask for a warning (but no error). */
597 if (TYPE_ACTUAL_ARG_TYPES (f1)
598 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
599 val = 2;
600 return val;
602 if (args2 == 0)
604 if (!self_promoting_args_p (args1))
605 return 0;
606 if (TYPE_ACTUAL_ARG_TYPES (f2)
607 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
608 val = 2;
609 return val;
612 /* Both types have argument lists: compare them and propagate results. */
613 val1 = type_lists_compatible_p (args1, args2);
614 return val1 != 1 ? val1 : val;
617 /* Check two lists of types for compatibility,
618 returning 0 for incompatible, 1 for compatible,
619 or 2 for compatible with warning. */
621 static int
622 type_lists_compatible_p (args1, args2)
623 tree args1, args2;
625 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
626 int val = 1;
627 int newval = 0;
629 while (1)
631 if (args1 == 0 && args2 == 0)
632 return val;
633 /* If one list is shorter than the other,
634 they fail to match. */
635 if (args1 == 0 || args2 == 0)
636 return 0;
637 /* A null pointer instead of a type
638 means there is supposed to be an argument
639 but nothing is specified about what type it has.
640 So match anything that self-promotes. */
641 if (TREE_VALUE (args1) == 0)
643 if (simple_type_promotes_to (TREE_VALUE (args2)) != NULL_TREE)
644 return 0;
646 else if (TREE_VALUE (args2) == 0)
648 if (simple_type_promotes_to (TREE_VALUE (args1)) != NULL_TREE)
649 return 0;
651 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
652 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
654 /* Allow wait (union {union wait *u; int *i} *)
655 and wait (union wait *) to be compatible. */
656 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
657 && (TYPE_NAME (TREE_VALUE (args1)) == 0
658 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
659 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
660 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
661 TYPE_SIZE (TREE_VALUE (args2))))
663 tree memb;
664 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
665 memb; memb = TREE_CHAIN (memb))
666 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
667 break;
668 if (memb == 0)
669 return 0;
671 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
672 && (TYPE_NAME (TREE_VALUE (args2)) == 0
673 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
674 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
675 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
676 TYPE_SIZE (TREE_VALUE (args1))))
678 tree memb;
679 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
680 memb; memb = TREE_CHAIN (memb))
681 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
682 break;
683 if (memb == 0)
684 return 0;
686 else
687 return 0;
690 /* comptypes said ok, but record if it said to warn. */
691 if (newval > val)
692 val = newval;
694 args1 = TREE_CHAIN (args1);
695 args2 = TREE_CHAIN (args2);
699 /* Compute the value of the `sizeof' operator. */
701 tree
702 c_sizeof (type)
703 tree type;
705 enum tree_code code = TREE_CODE (type);
706 tree size;
708 if (code == FUNCTION_TYPE)
710 if (pedantic || warn_pointer_arith)
711 pedwarn ("sizeof applied to a function type");
712 size = size_one_node;
714 else if (code == VOID_TYPE)
716 if (pedantic || warn_pointer_arith)
717 pedwarn ("sizeof applied to a void type");
718 size = size_one_node;
720 else if (code == ERROR_MARK)
721 size = size_one_node;
722 else if (!COMPLETE_TYPE_P (type))
724 error ("sizeof applied to an incomplete type");
725 size = size_zero_node;
727 else
728 /* Convert in case a char is more than one unit. */
729 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
730 size_int (TYPE_PRECISION (char_type_node)
731 / BITS_PER_UNIT));
733 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
734 TYPE_IS_SIZETYPE means that certain things (like overflow) will
735 never happen. However, this node should really have type
736 `size_t', which is just a typedef for an ordinary integer type. */
737 return fold (build1 (NOP_EXPR, c_size_type_node, size));
740 tree
741 c_sizeof_nowarn (type)
742 tree type;
744 enum tree_code code = TREE_CODE (type);
745 tree size;
747 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
748 size = size_one_node;
749 else if (!COMPLETE_TYPE_P (type))
750 size = size_zero_node;
751 else
752 /* Convert in case a char is more than one unit. */
753 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
754 size_int (TYPE_PRECISION (char_type_node)
755 / BITS_PER_UNIT));
757 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
758 TYPE_IS_SIZETYPE means that certain things (like overflow) will
759 never happen. However, this node should really have type
760 `size_t', which is just a typedef for an ordinary integer type. */
761 return fold (build1 (NOP_EXPR, c_size_type_node, size));
764 /* Compute the size to increment a pointer by. */
766 tree
767 c_size_in_bytes (type)
768 tree type;
770 enum tree_code code = TREE_CODE (type);
772 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
773 return size_one_node;
775 if (!COMPLETE_OR_VOID_TYPE_P (type))
777 error ("arithmetic on pointer to an incomplete type");
778 return size_one_node;
781 /* Convert in case a char is more than one unit. */
782 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
783 size_int (TYPE_PRECISION (char_type_node)
784 / BITS_PER_UNIT));
787 /* Implement the __alignof keyword: Return the minimum required
788 alignment of TYPE, measured in bytes. */
790 tree
791 c_alignof (type)
792 tree type;
794 enum tree_code code = TREE_CODE (type);
795 tree t;
797 if (code == FUNCTION_TYPE)
798 t = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
799 else if (code == VOID_TYPE || code == ERROR_MARK)
800 t = size_one_node;
801 else if (code == ERROR_MARK)
802 t = size_one_node;
803 else if (!COMPLETE_TYPE_P (type))
805 error ("__alignof__ applied to an incomplete type");
806 t = size_zero_node;
808 else
809 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
811 return fold (build1 (NOP_EXPR, c_size_type_node, t));
814 /* Implement the __alignof keyword: Return the minimum required
815 alignment of EXPR, measured in bytes. For VAR_DECL's and
816 FIELD_DECL's return DECL_ALIGN (which can be set from an
817 "aligned" __attribute__ specification). */
819 tree
820 c_alignof_expr (expr)
821 tree expr;
823 tree t;
825 if (TREE_CODE (expr) == VAR_DECL)
826 t = size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
828 else if (TREE_CODE (expr) == COMPONENT_REF
829 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
831 error ("`__alignof' applied to a bit-field");
832 t = size_one_node;
834 else if (TREE_CODE (expr) == COMPONENT_REF
835 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
836 t = size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
838 else if (TREE_CODE (expr) == INDIRECT_REF)
840 tree t = TREE_OPERAND (expr, 0);
841 tree best = t;
842 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
844 while (TREE_CODE (t) == NOP_EXPR
845 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
847 int thisalign;
849 t = TREE_OPERAND (t, 0);
850 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
851 if (thisalign > bestalign)
852 best = t, bestalign = thisalign;
854 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
856 else
857 return c_alignof (TREE_TYPE (expr));
859 return fold (build1 (NOP_EXPR, c_size_type_node, t));
862 /* Return either DECL or its known constant value (if it has one). */
864 tree
865 decl_constant_value (decl)
866 tree decl;
868 if (/* Don't change a variable array bound or initial value to a constant
869 in a place where a variable is invalid. */
870 current_function_decl != 0
871 && ! TREE_THIS_VOLATILE (decl)
872 && TREE_READONLY (decl)
873 && DECL_INITIAL (decl) != 0
874 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
875 /* This is invalid if initial value is not constant.
876 If it has either a function call, a memory reference,
877 or a variable, then re-evaluating it could give different results. */
878 && TREE_CONSTANT (DECL_INITIAL (decl))
879 /* Check for cases where this is sub-optimal, even though valid. */
880 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
881 return DECL_INITIAL (decl);
882 return decl;
885 /* Return either DECL or its known constant value (if it has one), but
886 return DECL if pedantic or DECL has mode BLKmode. This is for
887 bug-compatibility with the old behavior of decl_constant_value
888 (before GCC 3.0); every use of this function is a bug and it should
889 be removed before GCC 3.1. It is not appropriate to use pedantic
890 in a way that affects optimization, and BLKmode is probably not the
891 right test for avoiding misoptimizations either. */
893 static tree
894 decl_constant_value_for_broken_optimization (decl)
895 tree decl;
897 if (pedantic || DECL_MODE (decl) == BLKmode)
898 return decl;
899 else
900 return decl_constant_value (decl);
903 /* Perform default promotions for C data used in expressions.
904 Arrays and functions are converted to pointers;
905 enumeral types or short or char, to int.
906 In addition, manifest constants symbols are replaced by their values. */
908 tree
909 default_conversion (exp)
910 tree exp;
912 register tree type = TREE_TYPE (exp);
913 register enum tree_code code = TREE_CODE (type);
915 /* Constants can be used directly unless they're not loadable. */
916 if (TREE_CODE (exp) == CONST_DECL)
917 exp = DECL_INITIAL (exp);
919 /* Replace a nonvolatile const static variable with its value unless
920 it is an array, in which case we must be sure that taking the
921 address of the array produces consistent results. */
922 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
924 exp = decl_constant_value_for_broken_optimization (exp);
925 type = TREE_TYPE (exp);
928 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
929 an lvalue.
931 Do not use STRIP_NOPS here! It will remove conversions from pointer
932 to integer and cause infinite recursion. */
933 while (TREE_CODE (exp) == NON_LVALUE_EXPR
934 || (TREE_CODE (exp) == NOP_EXPR
935 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
936 exp = TREE_OPERAND (exp, 0);
938 /* Normally convert enums to int,
939 but convert wide enums to something wider. */
940 if (code == ENUMERAL_TYPE)
942 type = type_for_size (MAX (TYPE_PRECISION (type),
943 TYPE_PRECISION (integer_type_node)),
944 ((flag_traditional
945 || (TYPE_PRECISION (type)
946 >= TYPE_PRECISION (integer_type_node)))
947 && TREE_UNSIGNED (type)));
949 return convert (type, exp);
952 if (TREE_CODE (exp) == COMPONENT_REF
953 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
954 /* If it's thinner than an int, promote it like a
955 c_promoting_integer_type_p, otherwise leave it alone. */
956 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
957 TYPE_PRECISION (integer_type_node)))
958 return convert (flag_traditional && TREE_UNSIGNED (type)
959 ? unsigned_type_node : integer_type_node,
960 exp);
962 if (c_promoting_integer_type_p (type))
964 /* Traditionally, unsignedness is preserved in default promotions.
965 Also preserve unsignedness if not really getting any wider. */
966 if (TREE_UNSIGNED (type)
967 && (flag_traditional
968 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
969 return convert (unsigned_type_node, exp);
971 return convert (integer_type_node, exp);
974 if (flag_traditional && !flag_allow_single_precision
975 && TYPE_MAIN_VARIANT (type) == float_type_node)
976 return convert (double_type_node, exp);
978 if (code == VOID_TYPE)
980 error ("void value not ignored as it ought to be");
981 return error_mark_node;
983 if (code == FUNCTION_TYPE)
985 return build_unary_op (ADDR_EXPR, exp, 0);
987 if (code == ARRAY_TYPE)
989 register tree adr;
990 tree restype = TREE_TYPE (type);
991 tree ptrtype;
992 int constp = 0;
993 int volatilep = 0;
995 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
997 constp = TREE_READONLY (exp);
998 volatilep = TREE_THIS_VOLATILE (exp);
1001 if (TYPE_QUALS (type) || constp || volatilep)
1002 restype
1003 = c_build_qualified_type (restype,
1004 TYPE_QUALS (type)
1005 | (constp * TYPE_QUAL_CONST)
1006 | (volatilep * TYPE_QUAL_VOLATILE));
1008 if (TREE_CODE (exp) == INDIRECT_REF)
1009 return convert (TYPE_POINTER_TO (restype),
1010 TREE_OPERAND (exp, 0));
1012 if (TREE_CODE (exp) == COMPOUND_EXPR)
1014 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1015 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1016 TREE_OPERAND (exp, 0), op1);
1019 if (! lvalue_p (exp)
1020 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1022 error ("invalid use of non-lvalue array");
1023 return error_mark_node;
1026 ptrtype = build_pointer_type (restype);
1028 if (TREE_CODE (exp) == VAR_DECL)
1030 /* ??? This is not really quite correct
1031 in that the type of the operand of ADDR_EXPR
1032 is not the target type of the type of the ADDR_EXPR itself.
1033 Question is, can this lossage be avoided? */
1034 adr = build1 (ADDR_EXPR, ptrtype, exp);
1035 if (mark_addressable (exp) == 0)
1036 return error_mark_node;
1037 TREE_CONSTANT (adr) = staticp (exp);
1038 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1039 return adr;
1041 /* This way is better for a COMPONENT_REF since it can
1042 simplify the offset for a component. */
1043 adr = build_unary_op (ADDR_EXPR, exp, 1);
1044 return convert (ptrtype, adr);
1046 return exp;
1049 /* Look up component name in the structure type definition.
1051 If this component name is found indirectly within an anonymous union,
1052 store in *INDIRECT the component which directly contains
1053 that anonymous union. Otherwise, set *INDIRECT to 0. */
1055 static tree
1056 lookup_field (type, component, indirect)
1057 tree type, component;
1058 tree *indirect;
1060 tree field;
1062 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1063 to the field elements. Use a binary search on this array to quickly
1064 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1065 will always be set for structures which have many elements. */
1067 if (TYPE_LANG_SPECIFIC (type))
1069 int bot, top, half;
1070 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1072 field = TYPE_FIELDS (type);
1073 bot = 0;
1074 top = TYPE_LANG_SPECIFIC (type)->len;
1075 while (top - bot > 1)
1077 half = (top - bot + 1) >> 1;
1078 field = field_array[bot+half];
1080 if (DECL_NAME (field) == NULL_TREE)
1082 /* Step through all anon unions in linear fashion. */
1083 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1085 tree anon = 0, junk;
1087 field = field_array[bot++];
1088 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1089 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1090 anon = lookup_field (TREE_TYPE (field), component, &junk);
1092 if (anon != NULL_TREE)
1094 *indirect = field;
1095 return anon;
1099 /* Entire record is only anon unions. */
1100 if (bot > top)
1101 return NULL_TREE;
1103 /* Restart the binary search, with new lower bound. */
1104 continue;
1107 if (DECL_NAME (field) == component)
1108 break;
1109 if (DECL_NAME (field) < component)
1110 bot += half;
1111 else
1112 top = bot + half;
1115 if (DECL_NAME (field_array[bot]) == component)
1116 field = field_array[bot];
1117 else if (DECL_NAME (field) != component)
1118 field = 0;
1120 else
1122 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1124 if (DECL_NAME (field) == NULL_TREE)
1126 tree junk;
1127 tree anon = 0;
1129 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1130 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1131 anon = lookup_field (TREE_TYPE (field), component, &junk);
1133 if (anon != NULL_TREE)
1135 *indirect = field;
1136 return anon;
1140 if (DECL_NAME (field) == component)
1141 break;
1145 *indirect = NULL_TREE;
1146 return field;
1149 /* Make an expression to refer to the COMPONENT field of
1150 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1152 tree
1153 build_component_ref (datum, component)
1154 tree datum, component;
1156 register tree type = TREE_TYPE (datum);
1157 register enum tree_code code = TREE_CODE (type);
1158 register tree field = NULL;
1159 register tree ref;
1161 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1162 unless we are not to support things not strictly ANSI. */
1163 switch (TREE_CODE (datum))
1165 case COMPOUND_EXPR:
1167 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1168 return build (COMPOUND_EXPR, TREE_TYPE (value),
1169 TREE_OPERAND (datum, 0), value);
1171 case COND_EXPR:
1172 return build_conditional_expr
1173 (TREE_OPERAND (datum, 0),
1174 build_component_ref (TREE_OPERAND (datum, 1), component),
1175 build_component_ref (TREE_OPERAND (datum, 2), component));
1177 default:
1178 break;
1181 /* See if there is a field or component with name COMPONENT. */
1183 if (code == RECORD_TYPE || code == UNION_TYPE)
1185 tree indirect = 0;
1187 if (!COMPLETE_TYPE_P (type))
1189 incomplete_type_error (NULL_TREE, type);
1190 return error_mark_node;
1193 field = lookup_field (type, component, &indirect);
1195 if (!field)
1197 error ("%s has no member named `%s'",
1198 code == RECORD_TYPE ? "structure" : "union",
1199 IDENTIFIER_POINTER (component));
1200 return error_mark_node;
1202 if (TREE_TYPE (field) == error_mark_node)
1203 return error_mark_node;
1205 /* If FIELD was found buried within an anonymous union,
1206 make one COMPONENT_REF to get that anonymous union,
1207 then fall thru to make a second COMPONENT_REF to get FIELD. */
1208 if (indirect != 0)
1210 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1211 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1212 TREE_READONLY (ref) = 1;
1213 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1214 TREE_THIS_VOLATILE (ref) = 1;
1215 datum = ref;
1218 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1220 if (TREE_READONLY (datum) || TREE_READONLY (field))
1221 TREE_READONLY (ref) = 1;
1222 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1223 TREE_THIS_VOLATILE (ref) = 1;
1225 return ref;
1227 else if (code != ERROR_MARK)
1228 error ("request for member `%s' in something not a structure or union",
1229 IDENTIFIER_POINTER (component));
1231 return error_mark_node;
1234 /* Given an expression PTR for a pointer, return an expression
1235 for the value pointed to.
1236 ERRORSTRING is the name of the operator to appear in error messages. */
1238 tree
1239 build_indirect_ref (ptr, errorstring)
1240 tree ptr;
1241 const char *errorstring;
1243 register tree pointer = default_conversion (ptr);
1244 register tree type = TREE_TYPE (pointer);
1246 if (TREE_CODE (type) == POINTER_TYPE)
1248 if (TREE_CODE (pointer) == ADDR_EXPR
1249 && !flag_volatile
1250 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1251 == TREE_TYPE (type)))
1252 return TREE_OPERAND (pointer, 0);
1253 else
1255 tree t = TREE_TYPE (type);
1256 register tree ref = build1 (INDIRECT_REF,
1257 TYPE_MAIN_VARIANT (t), pointer);
1259 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1261 error ("dereferencing pointer to incomplete type");
1262 return error_mark_node;
1264 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1265 warning ("dereferencing `void *' pointer");
1267 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1268 so that we get the proper error message if the result is used
1269 to assign to. Also, &* is supposed to be a no-op.
1270 And ANSI C seems to specify that the type of the result
1271 should be the const type. */
1272 /* A de-reference of a pointer to const is not a const. It is valid
1273 to change it via some other pointer. */
1274 TREE_READONLY (ref) = TYPE_READONLY (t);
1275 TREE_SIDE_EFFECTS (ref)
1276 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1277 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1278 return ref;
1281 else if (TREE_CODE (pointer) != ERROR_MARK)
1282 error ("invalid type argument of `%s'", errorstring);
1283 return error_mark_node;
1286 /* This handles expressions of the form "a[i]", which denotes
1287 an array reference.
1289 This is logically equivalent in C to *(a+i), but we may do it differently.
1290 If A is a variable or a member, we generate a primitive ARRAY_REF.
1291 This avoids forcing the array out of registers, and can work on
1292 arrays that are not lvalues (for example, members of structures returned
1293 by functions). */
1295 tree
1296 build_array_ref (array, index)
1297 tree array, index;
1299 if (index == 0)
1301 error ("subscript missing in array reference");
1302 return error_mark_node;
1305 if (TREE_TYPE (array) == error_mark_node
1306 || TREE_TYPE (index) == error_mark_node)
1307 return error_mark_node;
1309 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1310 && TREE_CODE (array) != INDIRECT_REF)
1312 tree rval, type;
1314 /* Subscripting with type char is likely to lose
1315 on a machine where chars are signed.
1316 So warn on any machine, but optionally.
1317 Don't warn for unsigned char since that type is safe.
1318 Don't warn for signed char because anyone who uses that
1319 must have done so deliberately. */
1320 if (warn_char_subscripts
1321 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1322 warning ("array subscript has type `char'");
1324 /* Apply default promotions *after* noticing character types. */
1325 index = default_conversion (index);
1327 /* Require integer *after* promotion, for sake of enums. */
1328 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1330 error ("array subscript is not an integer");
1331 return error_mark_node;
1334 /* An array that is indexed by a non-constant
1335 cannot be stored in a register; we must be able to do
1336 address arithmetic on its address.
1337 Likewise an array of elements of variable size. */
1338 if (TREE_CODE (index) != INTEGER_CST
1339 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1340 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1342 if (mark_addressable (array) == 0)
1343 return error_mark_node;
1345 /* An array that is indexed by a constant value which is not within
1346 the array bounds cannot be stored in a register either; because we
1347 would get a crash in store_bit_field/extract_bit_field when trying
1348 to access a non-existent part of the register. */
1349 if (TREE_CODE (index) == INTEGER_CST
1350 && TYPE_VALUES (TREE_TYPE (array))
1351 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1353 if (mark_addressable (array) == 0)
1354 return error_mark_node;
1357 if (pedantic)
1359 tree foo = array;
1360 while (TREE_CODE (foo) == COMPONENT_REF)
1361 foo = TREE_OPERAND (foo, 0);
1362 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1363 pedwarn ("ISO C forbids subscripting `register' array");
1364 else if (! flag_isoc99 && ! lvalue_p (foo))
1365 pedwarn ("ISO C89 forbids subscripting non-lvalue array");
1368 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1369 rval = build (ARRAY_REF, type, array, index);
1370 /* Array ref is const/volatile if the array elements are
1371 or if the array is. */
1372 TREE_READONLY (rval)
1373 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1374 | TREE_READONLY (array));
1375 TREE_SIDE_EFFECTS (rval)
1376 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1377 | TREE_SIDE_EFFECTS (array));
1378 TREE_THIS_VOLATILE (rval)
1379 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1380 /* This was added by rms on 16 Nov 91.
1381 It fixes vol struct foo *a; a->elts[1]
1382 in an inline function.
1383 Hope it doesn't break something else. */
1384 | TREE_THIS_VOLATILE (array));
1385 return require_complete_type (fold (rval));
1389 tree ar = default_conversion (array);
1390 tree ind = default_conversion (index);
1392 /* Do the same warning check as above, but only on the part that's
1393 syntactically the index and only if it is also semantically
1394 the index. */
1395 if (warn_char_subscripts
1396 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1397 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1398 warning ("subscript has type `char'");
1400 /* Put the integer in IND to simplify error checking. */
1401 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1403 tree temp = ar;
1404 ar = ind;
1405 ind = temp;
1408 if (ar == error_mark_node)
1409 return ar;
1411 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1412 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1414 error ("subscripted value is neither array nor pointer");
1415 return error_mark_node;
1417 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1419 error ("array subscript is not an integer");
1420 return error_mark_node;
1423 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1424 "array indexing");
1428 /* Build an external reference to identifier ID. FUN indicates
1429 whether this will be used for a function call. */
1430 tree
1431 build_external_ref (id, fun)
1432 tree id;
1433 int fun;
1435 tree ref;
1436 tree decl = lookup_name (id);
1437 tree objc_ivar = lookup_objc_ivar (id);
1439 if (!decl || decl == error_mark_node || C_DECL_ANTICIPATED (decl))
1441 if (objc_ivar)
1442 ref = objc_ivar;
1443 else if (fun)
1445 if (!decl || decl == error_mark_node)
1446 /* Ordinary implicit function declaration. */
1447 ref = implicitly_declare (id);
1448 else
1450 /* Implicit declaration of built-in function. Don't
1451 change the built-in declaration, but don't let this
1452 go by silently, either. */
1453 implicit_decl_warning (id);
1455 /* only issue this warning once */
1456 C_DECL_ANTICIPATED (decl) = 0;
1457 ref = decl;
1460 else
1462 /* Reference to undeclared variable, including reference to
1463 builtin outside of function-call context. */
1464 if (current_function_decl == 0)
1465 error ("`%s' undeclared here (not in a function)",
1466 IDENTIFIER_POINTER (id));
1467 else
1469 if (IDENTIFIER_GLOBAL_VALUE (id) != error_mark_node
1470 || IDENTIFIER_ERROR_LOCUS (id) != current_function_decl)
1472 error ("`%s' undeclared (first use in this function)",
1473 IDENTIFIER_POINTER (id));
1475 if (! undeclared_variable_notice)
1477 error ("(Each undeclared identifier is reported only once");
1478 error ("for each function it appears in.)");
1479 undeclared_variable_notice = 1;
1482 IDENTIFIER_GLOBAL_VALUE (id) = error_mark_node;
1483 IDENTIFIER_ERROR_LOCUS (id) = current_function_decl;
1485 return error_mark_node;
1488 else
1490 /* Properly declared variable or function reference. */
1491 if (!objc_ivar)
1492 ref = decl;
1493 else if (decl != objc_ivar && IDENTIFIER_LOCAL_VALUE (id))
1495 warning ("local declaration of `%s' hides instance variable",
1496 IDENTIFIER_POINTER (id));
1497 ref = decl;
1499 else
1500 ref = objc_ivar;
1503 if (TREE_TYPE (ref) == error_mark_node)
1504 return error_mark_node;
1506 assemble_external (ref);
1507 TREE_USED (ref) = 1;
1509 if (TREE_CODE (ref) == CONST_DECL)
1511 ref = DECL_INITIAL (ref);
1512 TREE_CONSTANT (ref) = 1;
1515 return ref;
1518 /* Build a function call to function FUNCTION with parameters PARAMS.
1519 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1520 TREE_VALUE of each node is a parameter-expression.
1521 FUNCTION's data type may be a function type or a pointer-to-function. */
1523 tree
1524 build_function_call (function, params)
1525 tree function, params;
1527 register tree fntype, fundecl = 0;
1528 register tree coerced_params;
1529 tree name = NULL_TREE, assembler_name = NULL_TREE, result;
1531 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1532 STRIP_TYPE_NOPS (function);
1534 /* Convert anything with function type to a pointer-to-function. */
1535 if (TREE_CODE (function) == FUNCTION_DECL)
1537 name = DECL_NAME (function);
1538 assembler_name = DECL_ASSEMBLER_NAME (function);
1540 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1541 (because calling an inline function does not mean the function
1542 needs to be separately compiled). */
1543 fntype = build_type_variant (TREE_TYPE (function),
1544 TREE_READONLY (function),
1545 TREE_THIS_VOLATILE (function));
1546 fundecl = function;
1547 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1549 else
1550 function = default_conversion (function);
1552 fntype = TREE_TYPE (function);
1554 if (TREE_CODE (fntype) == ERROR_MARK)
1555 return error_mark_node;
1557 if (!(TREE_CODE (fntype) == POINTER_TYPE
1558 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1560 error ("called object is not a function");
1561 return error_mark_node;
1564 /* fntype now gets the type of function pointed to. */
1565 fntype = TREE_TYPE (fntype);
1567 /* Convert the parameters to the types declared in the
1568 function prototype, or apply default promotions. */
1570 coerced_params
1571 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1573 /* Check for errors in format strings. */
1575 if (warn_format && (name || assembler_name))
1576 check_function_format (NULL, name, assembler_name, coerced_params);
1578 /* Recognize certain built-in functions so we can make tree-codes
1579 other than CALL_EXPR. We do this when it enables fold-const.c
1580 to do something useful. */
1582 if (TREE_CODE (function) == ADDR_EXPR
1583 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1584 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1586 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1587 params, coerced_params);
1588 if (result)
1589 return result;
1592 result = build (CALL_EXPR, TREE_TYPE (fntype),
1593 function, coerced_params, NULL_TREE);
1594 TREE_SIDE_EFFECTS (result) = 1;
1595 result = fold (result);
1597 if (VOID_TYPE_P (TREE_TYPE (result)))
1598 return result;
1599 return require_complete_type (result);
1602 /* Convert the argument expressions in the list VALUES
1603 to the types in the list TYPELIST. The result is a list of converted
1604 argument expressions.
1606 If TYPELIST is exhausted, or when an element has NULL as its type,
1607 perform the default conversions.
1609 PARMLIST is the chain of parm decls for the function being called.
1610 It may be 0, if that info is not available.
1611 It is used only for generating error messages.
1613 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1615 This is also where warnings about wrong number of args are generated.
1617 Both VALUES and the returned value are chains of TREE_LIST nodes
1618 with the elements of the list in the TREE_VALUE slots of those nodes. */
1620 static tree
1621 convert_arguments (typelist, values, name, fundecl)
1622 tree typelist, values, name, fundecl;
1624 register tree typetail, valtail;
1625 register tree result = NULL;
1626 int parmnum;
1628 /* Scan the given expressions and types, producing individual
1629 converted arguments and pushing them on RESULT in reverse order. */
1631 for (valtail = values, typetail = typelist, parmnum = 0;
1632 valtail;
1633 valtail = TREE_CHAIN (valtail), parmnum++)
1635 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1636 register tree val = TREE_VALUE (valtail);
1638 if (type == void_type_node)
1640 if (name)
1641 error ("too many arguments to function `%s'",
1642 IDENTIFIER_POINTER (name));
1643 else
1644 error ("too many arguments to function");
1645 break;
1648 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1649 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1650 to convert automatically to a pointer. */
1651 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1652 val = TREE_OPERAND (val, 0);
1654 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1655 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1656 val = default_conversion (val);
1658 val = require_complete_type (val);
1660 if (type != 0)
1662 /* Formal parm type is specified by a function prototype. */
1663 tree parmval;
1665 if (!COMPLETE_TYPE_P (type))
1667 error ("type of formal parameter %d is incomplete", parmnum + 1);
1668 parmval = val;
1670 else
1672 /* Optionally warn about conversions that
1673 differ from the default conversions. */
1674 if (warn_conversion || warn_traditional)
1676 int formal_prec = TYPE_PRECISION (type);
1678 if (INTEGRAL_TYPE_P (type)
1679 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1680 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1681 if (INTEGRAL_TYPE_P (type)
1682 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1683 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1684 else if (TREE_CODE (type) == COMPLEX_TYPE
1685 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1686 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1687 else if (TREE_CODE (type) == REAL_TYPE
1688 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1689 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1690 else if (TREE_CODE (type) == COMPLEX_TYPE
1691 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1692 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1693 else if (TREE_CODE (type) == REAL_TYPE
1694 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1695 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1696 /* ??? At some point, messages should be written about
1697 conversions between complex types, but that's too messy
1698 to do now. */
1699 else if (TREE_CODE (type) == REAL_TYPE
1700 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1702 /* Warn if any argument is passed as `float',
1703 since without a prototype it would be `double'. */
1704 if (formal_prec == TYPE_PRECISION (float_type_node))
1705 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1707 /* Detect integer changing in width or signedness.
1708 These warnings are only activated with
1709 -Wconversion, not with -Wtraditional. */
1710 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1711 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1713 tree would_have_been = default_conversion (val);
1714 tree type1 = TREE_TYPE (would_have_been);
1716 if (TREE_CODE (type) == ENUMERAL_TYPE
1717 && type == TREE_TYPE (val))
1718 /* No warning if function asks for enum
1719 and the actual arg is that enum type. */
1721 else if (formal_prec != TYPE_PRECISION (type1))
1722 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1723 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1725 /* Don't complain if the formal parameter type
1726 is an enum, because we can't tell now whether
1727 the value was an enum--even the same enum. */
1728 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1730 else if (TREE_CODE (val) == INTEGER_CST
1731 && int_fits_type_p (val, type))
1732 /* Change in signedness doesn't matter
1733 if a constant value is unaffected. */
1735 /* Likewise for a constant in a NOP_EXPR. */
1736 else if (TREE_CODE (val) == NOP_EXPR
1737 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1738 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1740 #if 0 /* We never get such tree structure here. */
1741 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1742 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1743 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1744 /* Change in signedness doesn't matter
1745 if an enum value is unaffected. */
1747 #endif
1748 /* If the value is extended from a narrower
1749 unsigned type, it doesn't matter whether we
1750 pass it as signed or unsigned; the value
1751 certainly is the same either way. */
1752 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1753 && TREE_UNSIGNED (TREE_TYPE (val)))
1755 else if (TREE_UNSIGNED (type))
1756 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1757 else
1758 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1762 parmval = convert_for_assignment (type, val,
1763 (char *) 0, /* arg passing */
1764 fundecl, name, parmnum + 1);
1766 if (PROMOTE_PROTOTYPES
1767 && INTEGRAL_TYPE_P (type)
1768 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1769 parmval = default_conversion (parmval);
1771 result = tree_cons (NULL_TREE, parmval, result);
1773 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1774 && (TYPE_PRECISION (TREE_TYPE (val))
1775 < TYPE_PRECISION (double_type_node)))
1776 /* Convert `float' to `double'. */
1777 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1778 else
1779 /* Convert `short' and `char' to full-size `int'. */
1780 result = tree_cons (NULL_TREE, default_conversion (val), result);
1782 if (typetail)
1783 typetail = TREE_CHAIN (typetail);
1786 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1788 if (name)
1789 error ("too few arguments to function `%s'",
1790 IDENTIFIER_POINTER (name));
1791 else
1792 error ("too few arguments to function");
1795 return nreverse (result);
1798 /* This is the entry point used by the parser
1799 for binary operators in the input.
1800 In addition to constructing the expression,
1801 we check for operands that were written with other binary operators
1802 in a way that is likely to confuse the user. */
1804 tree
1805 parser_build_binary_op (code, arg1, arg2)
1806 enum tree_code code;
1807 tree arg1, arg2;
1809 tree result = build_binary_op (code, arg1, arg2, 1);
1811 char class;
1812 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1813 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1814 enum tree_code code1 = ERROR_MARK;
1815 enum tree_code code2 = ERROR_MARK;
1817 if (class1 == 'e' || class1 == '1'
1818 || class1 == '2' || class1 == '<')
1819 code1 = C_EXP_ORIGINAL_CODE (arg1);
1820 if (class2 == 'e' || class2 == '1'
1821 || class2 == '2' || class2 == '<')
1822 code2 = C_EXP_ORIGINAL_CODE (arg2);
1824 /* Check for cases such as x+y<<z which users are likely
1825 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1826 is cleared to prevent these warnings. */
1827 if (warn_parentheses)
1829 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1831 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1832 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1833 warning ("suggest parentheses around + or - inside shift");
1836 if (code == TRUTH_ORIF_EXPR)
1838 if (code1 == TRUTH_ANDIF_EXPR
1839 || code2 == TRUTH_ANDIF_EXPR)
1840 warning ("suggest parentheses around && within ||");
1843 if (code == BIT_IOR_EXPR)
1845 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1846 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1847 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1848 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1849 warning ("suggest parentheses around arithmetic in operand of |");
1850 /* Check cases like x|y==z */
1851 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1852 warning ("suggest parentheses around comparison in operand of |");
1855 if (code == BIT_XOR_EXPR)
1857 if (code1 == BIT_AND_EXPR
1858 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1859 || code2 == BIT_AND_EXPR
1860 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1861 warning ("suggest parentheses around arithmetic in operand of ^");
1862 /* Check cases like x^y==z */
1863 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1864 warning ("suggest parentheses around comparison in operand of ^");
1867 if (code == BIT_AND_EXPR)
1869 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1870 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1871 warning ("suggest parentheses around + or - in operand of &");
1872 /* Check cases like x&y==z */
1873 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1874 warning ("suggest parentheses around comparison in operand of &");
1878 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1879 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1880 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1881 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1883 unsigned_conversion_warning (result, arg1);
1884 unsigned_conversion_warning (result, arg2);
1885 overflow_warning (result);
1887 class = TREE_CODE_CLASS (TREE_CODE (result));
1889 /* Record the code that was specified in the source,
1890 for the sake of warnings about confusing nesting. */
1891 if (class == 'e' || class == '1'
1892 || class == '2' || class == '<')
1893 C_SET_EXP_ORIGINAL_CODE (result, code);
1894 else
1896 int flag = TREE_CONSTANT (result);
1897 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1898 so that convert_for_assignment wouldn't strip it.
1899 That way, we got warnings for things like p = (1 - 1).
1900 But it turns out we should not get those warnings. */
1901 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1902 C_SET_EXP_ORIGINAL_CODE (result, code);
1903 TREE_CONSTANT (result) = flag;
1906 return result;
1909 /* Build a binary-operation expression without default conversions.
1910 CODE is the kind of expression to build.
1911 This function differs from `build' in several ways:
1912 the data type of the result is computed and recorded in it,
1913 warnings are generated if arg data types are invalid,
1914 special handling for addition and subtraction of pointers is known,
1915 and some optimization is done (operations on narrow ints
1916 are done in the narrower type when that gives the same result).
1917 Constant folding is also done before the result is returned.
1919 Note that the operands will never have enumeral types, or function
1920 or array types, because either they will have the default conversions
1921 performed or they have both just been converted to some other type in which
1922 the arithmetic is to be done. */
1924 tree
1925 build_binary_op (code, orig_op0, orig_op1, convert_p)
1926 enum tree_code code;
1927 tree orig_op0, orig_op1;
1928 int convert_p;
1930 tree type0, type1;
1931 register enum tree_code code0, code1;
1932 tree op0, op1;
1934 /* Expression code to give to the expression when it is built.
1935 Normally this is CODE, which is what the caller asked for,
1936 but in some special cases we change it. */
1937 register enum tree_code resultcode = code;
1939 /* Data type in which the computation is to be performed.
1940 In the simplest cases this is the common type of the arguments. */
1941 register tree result_type = NULL;
1943 /* Nonzero means operands have already been type-converted
1944 in whatever way is necessary.
1945 Zero means they need to be converted to RESULT_TYPE. */
1946 int converted = 0;
1948 /* Nonzero means create the expression with this type, rather than
1949 RESULT_TYPE. */
1950 tree build_type = 0;
1952 /* Nonzero means after finally constructing the expression
1953 convert it to this type. */
1954 tree final_type = 0;
1956 /* Nonzero if this is an operation like MIN or MAX which can
1957 safely be computed in short if both args are promoted shorts.
1958 Also implies COMMON.
1959 -1 indicates a bitwise operation; this makes a difference
1960 in the exact conditions for when it is safe to do the operation
1961 in a narrower mode. */
1962 int shorten = 0;
1964 /* Nonzero if this is a comparison operation;
1965 if both args are promoted shorts, compare the original shorts.
1966 Also implies COMMON. */
1967 int short_compare = 0;
1969 /* Nonzero if this is a right-shift operation, which can be computed on the
1970 original short and then promoted if the operand is a promoted short. */
1971 int short_shift = 0;
1973 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1974 int common = 0;
1976 if (convert_p)
1978 op0 = default_conversion (orig_op0);
1979 op1 = default_conversion (orig_op1);
1981 else
1983 op0 = orig_op0;
1984 op1 = orig_op1;
1987 type0 = TREE_TYPE (op0);
1988 type1 = TREE_TYPE (op1);
1990 /* The expression codes of the data types of the arguments tell us
1991 whether the arguments are integers, floating, pointers, etc. */
1992 code0 = TREE_CODE (type0);
1993 code1 = TREE_CODE (type1);
1995 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1996 STRIP_TYPE_NOPS (op0);
1997 STRIP_TYPE_NOPS (op1);
1999 /* If an error was already reported for one of the arguments,
2000 avoid reporting another error. */
2002 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2003 return error_mark_node;
2005 switch (code)
2007 case PLUS_EXPR:
2008 /* Handle the pointer + int case. */
2009 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2010 return pointer_int_sum (PLUS_EXPR, op0, op1);
2011 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2012 return pointer_int_sum (PLUS_EXPR, op1, op0);
2013 else
2014 common = 1;
2015 break;
2017 case MINUS_EXPR:
2018 /* Subtraction of two similar pointers.
2019 We must subtract them as integers, then divide by object size. */
2020 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2021 && comp_target_types (type0, type1))
2022 return pointer_diff (op0, op1);
2023 /* Handle pointer minus int. Just like pointer plus int. */
2024 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2025 return pointer_int_sum (MINUS_EXPR, op0, op1);
2026 else
2027 common = 1;
2028 break;
2030 case MULT_EXPR:
2031 common = 1;
2032 break;
2034 case TRUNC_DIV_EXPR:
2035 case CEIL_DIV_EXPR:
2036 case FLOOR_DIV_EXPR:
2037 case ROUND_DIV_EXPR:
2038 case EXACT_DIV_EXPR:
2039 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2040 || code0 == COMPLEX_TYPE)
2041 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2042 || code1 == COMPLEX_TYPE))
2044 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2045 resultcode = RDIV_EXPR;
2046 else
2047 /* Although it would be tempting to shorten always here, that
2048 loses on some targets, since the modulo instruction is
2049 undefined if the quotient can't be represented in the
2050 computation mode. We shorten only if unsigned or if
2051 dividing by something we know != -1. */
2052 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2053 || (TREE_CODE (op1) == INTEGER_CST
2054 && ! integer_all_onesp (op1)));
2055 common = 1;
2057 break;
2059 case BIT_AND_EXPR:
2060 case BIT_ANDTC_EXPR:
2061 case BIT_IOR_EXPR:
2062 case BIT_XOR_EXPR:
2063 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2064 shorten = -1;
2065 /* If one operand is a constant, and the other is a short type
2066 that has been converted to an int,
2067 really do the work in the short type and then convert the
2068 result to int. If we are lucky, the constant will be 0 or 1
2069 in the short type, making the entire operation go away. */
2070 if (TREE_CODE (op0) == INTEGER_CST
2071 && TREE_CODE (op1) == NOP_EXPR
2072 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2073 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2075 final_type = result_type;
2076 op1 = TREE_OPERAND (op1, 0);
2077 result_type = TREE_TYPE (op1);
2079 if (TREE_CODE (op1) == INTEGER_CST
2080 && TREE_CODE (op0) == NOP_EXPR
2081 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2082 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2084 final_type = result_type;
2085 op0 = TREE_OPERAND (op0, 0);
2086 result_type = TREE_TYPE (op0);
2088 break;
2090 case TRUNC_MOD_EXPR:
2091 case FLOOR_MOD_EXPR:
2092 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2094 /* Although it would be tempting to shorten always here, that loses
2095 on some targets, since the modulo instruction is undefined if the
2096 quotient can't be represented in the computation mode. We shorten
2097 only if unsigned or if dividing by something we know != -1. */
2098 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2099 || (TREE_CODE (op1) == INTEGER_CST
2100 && ! integer_all_onesp (op1)));
2101 common = 1;
2103 break;
2105 case TRUTH_ANDIF_EXPR:
2106 case TRUTH_ORIF_EXPR:
2107 case TRUTH_AND_EXPR:
2108 case TRUTH_OR_EXPR:
2109 case TRUTH_XOR_EXPR:
2110 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2111 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2112 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2113 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2115 /* Result of these operations is always an int,
2116 but that does not mean the operands should be
2117 converted to ints! */
2118 result_type = integer_type_node;
2119 op0 = truthvalue_conversion (op0);
2120 op1 = truthvalue_conversion (op1);
2121 converted = 1;
2123 break;
2125 /* Shift operations: result has same type as first operand;
2126 always convert second operand to int.
2127 Also set SHORT_SHIFT if shifting rightward. */
2129 case RSHIFT_EXPR:
2130 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2132 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2134 if (tree_int_cst_sgn (op1) < 0)
2135 warning ("right shift count is negative");
2136 else
2138 if (! integer_zerop (op1))
2139 short_shift = 1;
2141 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2142 warning ("right shift count >= width of type");
2146 /* Use the type of the value to be shifted.
2147 This is what most traditional C compilers do. */
2148 result_type = type0;
2149 /* Unless traditional, convert the shift-count to an integer,
2150 regardless of size of value being shifted. */
2151 if (! flag_traditional)
2153 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2154 op1 = convert (integer_type_node, op1);
2155 /* Avoid converting op1 to result_type later. */
2156 converted = 1;
2159 break;
2161 case LSHIFT_EXPR:
2162 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2164 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2166 if (tree_int_cst_sgn (op1) < 0)
2167 warning ("left shift count is negative");
2169 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2170 warning ("left shift count >= width of type");
2173 /* Use the type of the value to be shifted.
2174 This is what most traditional C compilers do. */
2175 result_type = type0;
2176 /* Unless traditional, convert the shift-count to an integer,
2177 regardless of size of value being shifted. */
2178 if (! flag_traditional)
2180 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2181 op1 = convert (integer_type_node, op1);
2182 /* Avoid converting op1 to result_type later. */
2183 converted = 1;
2186 break;
2188 case RROTATE_EXPR:
2189 case LROTATE_EXPR:
2190 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2192 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2194 if (tree_int_cst_sgn (op1) < 0)
2195 warning ("shift count is negative");
2196 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2197 warning ("shift count >= width of type");
2200 /* Use the type of the value to be shifted.
2201 This is what most traditional C compilers do. */
2202 result_type = type0;
2203 /* Unless traditional, convert the shift-count to an integer,
2204 regardless of size of value being shifted. */
2205 if (! flag_traditional)
2207 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2208 op1 = convert (integer_type_node, op1);
2209 /* Avoid converting op1 to result_type later. */
2210 converted = 1;
2213 break;
2215 case EQ_EXPR:
2216 case NE_EXPR:
2217 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2218 warning ("comparing floating point with == or != is unsafe");
2219 /* Result of comparison is always int,
2220 but don't convert the args to int! */
2221 build_type = integer_type_node;
2222 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2223 || code0 == COMPLEX_TYPE)
2224 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2225 || code1 == COMPLEX_TYPE))
2226 short_compare = 1;
2227 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2229 register tree tt0 = TREE_TYPE (type0);
2230 register tree tt1 = TREE_TYPE (type1);
2231 /* Anything compares with void *. void * compares with anything.
2232 Otherwise, the targets must be compatible
2233 and both must be object or both incomplete. */
2234 if (comp_target_types (type0, type1))
2235 result_type = common_type (type0, type1);
2236 else if (VOID_TYPE_P (tt0))
2238 /* op0 != orig_op0 detects the case of something
2239 whose value is 0 but which isn't a valid null ptr const. */
2240 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2241 && TREE_CODE (tt1) == FUNCTION_TYPE)
2242 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2244 else if (VOID_TYPE_P (tt1))
2246 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2247 && TREE_CODE (tt0) == FUNCTION_TYPE)
2248 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2250 else
2251 pedwarn ("comparison of distinct pointer types lacks a cast");
2253 if (result_type == NULL_TREE)
2254 result_type = ptr_type_node;
2256 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2257 && integer_zerop (op1))
2258 result_type = type0;
2259 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2260 && integer_zerop (op0))
2261 result_type = type1;
2262 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2264 result_type = type0;
2265 if (! flag_traditional)
2266 pedwarn ("comparison between pointer and integer");
2268 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2270 result_type = type1;
2271 if (! flag_traditional)
2272 pedwarn ("comparison between pointer and integer");
2274 break;
2276 case MAX_EXPR:
2277 case MIN_EXPR:
2278 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2279 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2280 shorten = 1;
2281 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2283 if (comp_target_types (type0, type1))
2285 result_type = common_type (type0, type1);
2286 if (pedantic
2287 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2288 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2290 else
2292 result_type = ptr_type_node;
2293 pedwarn ("comparison of distinct pointer types lacks a cast");
2296 break;
2298 case LE_EXPR:
2299 case GE_EXPR:
2300 case LT_EXPR:
2301 case GT_EXPR:
2302 build_type = integer_type_node;
2303 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2304 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2305 short_compare = 1;
2306 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2308 if (comp_target_types (type0, type1))
2310 result_type = common_type (type0, type1);
2311 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
2312 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
2313 pedwarn ("comparison of complete and incomplete pointers");
2314 else if (pedantic
2315 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2316 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2318 else
2320 result_type = ptr_type_node;
2321 pedwarn ("comparison of distinct pointer types lacks a cast");
2324 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2325 && integer_zerop (op1))
2327 result_type = type0;
2328 if (pedantic || extra_warnings)
2329 pedwarn ("ordered comparison of pointer with integer zero");
2331 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2332 && integer_zerop (op0))
2334 result_type = type1;
2335 if (pedantic)
2336 pedwarn ("ordered comparison of pointer with integer zero");
2338 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2340 result_type = type0;
2341 if (! flag_traditional)
2342 pedwarn ("comparison between pointer and integer");
2344 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2346 result_type = type1;
2347 if (! flag_traditional)
2348 pedwarn ("comparison between pointer and integer");
2350 break;
2352 case UNORDERED_EXPR:
2353 case ORDERED_EXPR:
2354 case UNLT_EXPR:
2355 case UNLE_EXPR:
2356 case UNGT_EXPR:
2357 case UNGE_EXPR:
2358 case UNEQ_EXPR:
2359 build_type = integer_type_node;
2360 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2362 error ("unordered comparison on non-floating point argument");
2363 return error_mark_node;
2365 common = 1;
2366 break;
2368 default:
2369 break;
2372 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2374 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2376 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2378 if (shorten || common || short_compare)
2379 result_type = common_type (type0, type1);
2381 /* For certain operations (which identify themselves by shorten != 0)
2382 if both args were extended from the same smaller type,
2383 do the arithmetic in that type and then extend.
2385 shorten !=0 and !=1 indicates a bitwise operation.
2386 For them, this optimization is safe only if
2387 both args are zero-extended or both are sign-extended.
2388 Otherwise, we might change the result.
2389 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2390 but calculated in (unsigned short) it would be (unsigned short)-1. */
2392 if (shorten && none_complex)
2394 int unsigned0, unsigned1;
2395 tree arg0 = get_narrower (op0, &unsigned0);
2396 tree arg1 = get_narrower (op1, &unsigned1);
2397 /* UNS is 1 if the operation to be done is an unsigned one. */
2398 int uns = TREE_UNSIGNED (result_type);
2399 tree type;
2401 final_type = result_type;
2403 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2404 but it *requires* conversion to FINAL_TYPE. */
2406 if ((TYPE_PRECISION (TREE_TYPE (op0))
2407 == TYPE_PRECISION (TREE_TYPE (arg0)))
2408 && TREE_TYPE (op0) != final_type)
2409 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2410 if ((TYPE_PRECISION (TREE_TYPE (op1))
2411 == TYPE_PRECISION (TREE_TYPE (arg1)))
2412 && TREE_TYPE (op1) != final_type)
2413 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2415 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2417 /* For bitwise operations, signedness of nominal type
2418 does not matter. Consider only how operands were extended. */
2419 if (shorten == -1)
2420 uns = unsigned0;
2422 /* Note that in all three cases below we refrain from optimizing
2423 an unsigned operation on sign-extended args.
2424 That would not be valid. */
2426 /* Both args variable: if both extended in same way
2427 from same width, do it in that width.
2428 Do it unsigned if args were zero-extended. */
2429 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2430 < TYPE_PRECISION (result_type))
2431 && (TYPE_PRECISION (TREE_TYPE (arg1))
2432 == TYPE_PRECISION (TREE_TYPE (arg0)))
2433 && unsigned0 == unsigned1
2434 && (unsigned0 || !uns))
2435 result_type
2436 = signed_or_unsigned_type (unsigned0,
2437 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2438 else if (TREE_CODE (arg0) == INTEGER_CST
2439 && (unsigned1 || !uns)
2440 && (TYPE_PRECISION (TREE_TYPE (arg1))
2441 < TYPE_PRECISION (result_type))
2442 && (type = signed_or_unsigned_type (unsigned1,
2443 TREE_TYPE (arg1)),
2444 int_fits_type_p (arg0, type)))
2445 result_type = type;
2446 else if (TREE_CODE (arg1) == INTEGER_CST
2447 && (unsigned0 || !uns)
2448 && (TYPE_PRECISION (TREE_TYPE (arg0))
2449 < TYPE_PRECISION (result_type))
2450 && (type = signed_or_unsigned_type (unsigned0,
2451 TREE_TYPE (arg0)),
2452 int_fits_type_p (arg1, type)))
2453 result_type = type;
2456 /* Shifts can be shortened if shifting right. */
2458 if (short_shift)
2460 int unsigned_arg;
2461 tree arg0 = get_narrower (op0, &unsigned_arg);
2463 final_type = result_type;
2465 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2466 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2468 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2469 /* We can shorten only if the shift count is less than the
2470 number of bits in the smaller type size. */
2471 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
2472 /* If arg is sign-extended and then unsigned-shifted,
2473 we can simulate this with a signed shift in arg's type
2474 only if the extended result is at least twice as wide
2475 as the arg. Otherwise, the shift could use up all the
2476 ones made by sign-extension and bring in zeros.
2477 We can't optimize that case at all, but in most machines
2478 it never happens because available widths are 2**N. */
2479 && (!TREE_UNSIGNED (final_type)
2480 || unsigned_arg
2481 || (2 * TYPE_PRECISION (TREE_TYPE (arg0))
2482 <= TYPE_PRECISION (result_type))))
2484 /* Do an unsigned shift if the operand was zero-extended. */
2485 result_type
2486 = signed_or_unsigned_type (unsigned_arg,
2487 TREE_TYPE (arg0));
2488 /* Convert value-to-be-shifted to that type. */
2489 if (TREE_TYPE (op0) != result_type)
2490 op0 = convert (result_type, op0);
2491 converted = 1;
2495 /* Comparison operations are shortened too but differently.
2496 They identify themselves by setting short_compare = 1. */
2498 if (short_compare)
2500 /* Don't write &op0, etc., because that would prevent op0
2501 from being kept in a register.
2502 Instead, make copies of the our local variables and
2503 pass the copies by reference, then copy them back afterward. */
2504 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2505 enum tree_code xresultcode = resultcode;
2506 tree val
2507 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2509 if (val != 0)
2510 return val;
2512 op0 = xop0, op1 = xop1;
2513 converted = 1;
2514 resultcode = xresultcode;
2516 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2517 && skip_evaluation == 0)
2519 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2520 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2521 int unsignedp0, unsignedp1;
2522 tree primop0 = get_narrower (op0, &unsignedp0);
2523 tree primop1 = get_narrower (op1, &unsignedp1);
2525 xop0 = orig_op0;
2526 xop1 = orig_op1;
2527 STRIP_TYPE_NOPS (xop0);
2528 STRIP_TYPE_NOPS (xop1);
2530 /* Give warnings for comparisons between signed and unsigned
2531 quantities that may fail.
2533 Do the checking based on the original operand trees, so that
2534 casts will be considered, but default promotions won't be.
2536 Do not warn if the comparison is being done in a signed type,
2537 since the signed type will only be chosen if it can represent
2538 all the values of the unsigned type. */
2539 if (! TREE_UNSIGNED (result_type))
2540 /* OK */;
2541 /* Do not warn if both operands are the same signedness. */
2542 else if (op0_signed == op1_signed)
2543 /* OK */;
2544 else
2546 tree sop, uop;
2548 if (op0_signed)
2549 sop = xop0, uop = xop1;
2550 else
2551 sop = xop1, uop = xop0;
2553 /* Do not warn if the signed quantity is an
2554 unsuffixed integer literal (or some static
2555 constant expression involving such literals or a
2556 conditional expression involving such literals)
2557 and it is non-negative. */
2558 if (tree_expr_nonnegative_p (sop))
2559 /* OK */;
2560 /* Do not warn if the comparison is an equality operation,
2561 the unsigned quantity is an integral constant, and it
2562 would fit in the result if the result were signed. */
2563 else if (TREE_CODE (uop) == INTEGER_CST
2564 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2565 && int_fits_type_p (uop, signed_type (result_type)))
2566 /* OK */;
2567 /* Do not warn if the unsigned quantity is an enumeration
2568 constant and its maximum value would fit in the result
2569 if the result were signed. */
2570 else if (TREE_CODE (uop) == INTEGER_CST
2571 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2572 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2573 signed_type (result_type)))
2574 /* OK */;
2575 else
2576 warning ("comparison between signed and unsigned");
2579 /* Warn if two unsigned values are being compared in a size
2580 larger than their original size, and one (and only one) is the
2581 result of a `~' operator. This comparison will always fail.
2583 Also warn if one operand is a constant, and the constant
2584 does not have all bits set that are set in the ~ operand
2585 when it is extended. */
2587 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2588 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2590 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2591 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2592 &unsignedp0);
2593 else
2594 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2595 &unsignedp1);
2597 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
2599 tree primop;
2600 HOST_WIDE_INT constant, mask;
2601 int unsignedp, bits;
2603 if (host_integerp (primop0, 0))
2605 primop = primop1;
2606 unsignedp = unsignedp1;
2607 constant = tree_low_cst (primop0, 0);
2609 else
2611 primop = primop0;
2612 unsignedp = unsignedp0;
2613 constant = tree_low_cst (primop1, 0);
2616 bits = TYPE_PRECISION (TREE_TYPE (primop));
2617 if (bits < TYPE_PRECISION (result_type)
2618 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
2620 mask = (~ (HOST_WIDE_INT) 0) << bits;
2621 if ((mask & constant) != mask)
2622 warning ("comparison of promoted ~unsigned with constant");
2625 else if (unsignedp0 && unsignedp1
2626 && (TYPE_PRECISION (TREE_TYPE (primop0))
2627 < TYPE_PRECISION (result_type))
2628 && (TYPE_PRECISION (TREE_TYPE (primop1))
2629 < TYPE_PRECISION (result_type)))
2630 warning ("comparison of promoted ~unsigned with unsigned");
2636 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2637 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2638 Then the expression will be built.
2639 It will be given type FINAL_TYPE if that is nonzero;
2640 otherwise, it will be given type RESULT_TYPE. */
2642 if (!result_type)
2644 binary_op_error (code);
2645 return error_mark_node;
2648 if (! converted)
2650 if (TREE_TYPE (op0) != result_type)
2651 op0 = convert (result_type, op0);
2652 if (TREE_TYPE (op1) != result_type)
2653 op1 = convert (result_type, op1);
2656 if (build_type == NULL_TREE)
2657 build_type = result_type;
2660 register tree result = build (resultcode, build_type, op0, op1);
2661 register tree folded;
2663 folded = fold (result);
2664 if (folded == result)
2665 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2666 if (final_type != 0)
2667 return convert (final_type, folded);
2668 return folded;
2672 /* Return a tree for the sum or difference (RESULTCODE says which)
2673 of pointer PTROP and integer INTOP. */
2675 static tree
2676 pointer_int_sum (resultcode, ptrop, intop)
2677 enum tree_code resultcode;
2678 register tree ptrop, intop;
2680 tree size_exp;
2682 register tree result;
2683 register tree folded;
2685 /* The result is a pointer of the same type that is being added. */
2687 register tree result_type = TREE_TYPE (ptrop);
2689 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2691 if (pedantic || warn_pointer_arith)
2692 pedwarn ("pointer of type `void *' used in arithmetic");
2693 size_exp = integer_one_node;
2695 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2697 if (pedantic || warn_pointer_arith)
2698 pedwarn ("pointer to a function used in arithmetic");
2699 size_exp = integer_one_node;
2701 else
2702 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2704 /* If what we are about to multiply by the size of the elements
2705 contains a constant term, apply distributive law
2706 and multiply that constant term separately.
2707 This helps produce common subexpressions. */
2709 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2710 && ! TREE_CONSTANT (intop)
2711 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2712 && TREE_CONSTANT (size_exp)
2713 /* If the constant comes from pointer subtraction,
2714 skip this optimization--it would cause an error. */
2715 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2716 /* If the constant is unsigned, and smaller than the pointer size,
2717 then we must skip this optimization. This is because it could cause
2718 an overflow error if the constant is negative but INTOP is not. */
2719 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2720 || (TYPE_PRECISION (TREE_TYPE (intop))
2721 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2723 enum tree_code subcode = resultcode;
2724 tree int_type = TREE_TYPE (intop);
2725 if (TREE_CODE (intop) == MINUS_EXPR)
2726 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2727 /* Convert both subexpression types to the type of intop,
2728 because weird cases involving pointer arithmetic
2729 can result in a sum or difference with different type args. */
2730 ptrop = build_binary_op (subcode, ptrop,
2731 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2732 intop = convert (int_type, TREE_OPERAND (intop, 0));
2735 /* Convert the integer argument to a type the same size as sizetype
2736 so the multiply won't overflow spuriously. */
2738 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2739 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2740 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2741 TREE_UNSIGNED (sizetype)), intop);
2743 /* Replace the integer argument with a suitable product by the object size.
2744 Do this multiplication as signed, then convert to the appropriate
2745 pointer type (actually unsigned integral). */
2747 intop = convert (result_type,
2748 build_binary_op (MULT_EXPR, intop,
2749 convert (TREE_TYPE (intop), size_exp), 1));
2751 /* Create the sum or difference. */
2753 result = build (resultcode, result_type, ptrop, intop);
2755 folded = fold (result);
2756 if (folded == result)
2757 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2758 return folded;
2761 /* Return a tree for the difference of pointers OP0 and OP1.
2762 The resulting tree has type int. */
2764 static tree
2765 pointer_diff (op0, op1)
2766 register tree op0, op1;
2768 register tree result, folded;
2769 tree restype = ptrdiff_type_node;
2771 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2773 if (pedantic || warn_pointer_arith)
2775 if (TREE_CODE (target_type) == VOID_TYPE)
2776 pedwarn ("pointer of type `void *' used in subtraction");
2777 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2778 pedwarn ("pointer to a function used in subtraction");
2781 /* First do the subtraction as integers;
2782 then drop through to build the divide operator.
2783 Do not do default conversions on the minus operator
2784 in case restype is a short type. */
2786 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2787 convert (restype, op1), 0);
2788 /* This generates an error if op1 is pointer to incomplete type. */
2789 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
2790 error ("arithmetic on pointer to an incomplete type");
2792 /* This generates an error if op0 is pointer to incomplete type. */
2793 op1 = c_size_in_bytes (target_type);
2795 /* Divide by the size, in easiest possible way. */
2797 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2799 folded = fold (result);
2800 if (folded == result)
2801 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2802 return folded;
2805 /* Construct and perhaps optimize a tree representation
2806 for a unary operation. CODE, a tree_code, specifies the operation
2807 and XARG is the operand. NOCONVERT nonzero suppresses
2808 the default promotions (such as from short to int). */
2810 tree
2811 build_unary_op (code, xarg, noconvert)
2812 enum tree_code code;
2813 tree xarg;
2814 int noconvert;
2816 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2817 register tree arg = xarg;
2818 register tree argtype = 0;
2819 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2820 tree val;
2822 if (typecode == ERROR_MARK)
2823 return error_mark_node;
2824 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2825 typecode = INTEGER_TYPE;
2827 switch (code)
2829 case CONVERT_EXPR:
2830 /* This is used for unary plus, because a CONVERT_EXPR
2831 is enough to prevent anybody from looking inside for
2832 associativity, but won't generate any code. */
2833 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2834 || typecode == COMPLEX_TYPE))
2836 error ("wrong type argument to unary plus");
2837 return error_mark_node;
2839 else if (!noconvert)
2840 arg = default_conversion (arg);
2841 break;
2843 case NEGATE_EXPR:
2844 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2845 || typecode == COMPLEX_TYPE))
2847 error ("wrong type argument to unary minus");
2848 return error_mark_node;
2850 else if (!noconvert)
2851 arg = default_conversion (arg);
2852 break;
2854 case BIT_NOT_EXPR:
2855 if (typecode == COMPLEX_TYPE)
2857 code = CONJ_EXPR;
2858 if (pedantic)
2859 pedwarn ("ISO C does not support `~' for complex conjugation");
2860 if (!noconvert)
2861 arg = default_conversion (arg);
2863 else if (typecode != INTEGER_TYPE)
2865 error ("wrong type argument to bit-complement");
2866 return error_mark_node;
2868 else if (!noconvert)
2869 arg = default_conversion (arg);
2870 break;
2872 case ABS_EXPR:
2873 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2874 || typecode == COMPLEX_TYPE))
2876 error ("wrong type argument to abs");
2877 return error_mark_node;
2879 else if (!noconvert)
2880 arg = default_conversion (arg);
2881 break;
2883 case CONJ_EXPR:
2884 /* Conjugating a real value is a no-op, but allow it anyway. */
2885 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2886 || typecode == COMPLEX_TYPE))
2888 error ("wrong type argument to conjugation");
2889 return error_mark_node;
2891 else if (!noconvert)
2892 arg = default_conversion (arg);
2893 break;
2895 case TRUTH_NOT_EXPR:
2896 if (typecode != INTEGER_TYPE
2897 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2898 && typecode != COMPLEX_TYPE
2899 /* These will convert to a pointer. */
2900 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2902 error ("wrong type argument to unary exclamation mark");
2903 return error_mark_node;
2905 arg = truthvalue_conversion (arg);
2906 return invert_truthvalue (arg);
2908 case NOP_EXPR:
2909 break;
2911 case REALPART_EXPR:
2912 if (TREE_CODE (arg) == COMPLEX_CST)
2913 return TREE_REALPART (arg);
2914 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2915 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2916 else
2917 return arg;
2919 case IMAGPART_EXPR:
2920 if (TREE_CODE (arg) == COMPLEX_CST)
2921 return TREE_IMAGPART (arg);
2922 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2923 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2924 else
2925 return convert (TREE_TYPE (arg), integer_zero_node);
2927 case PREINCREMENT_EXPR:
2928 case POSTINCREMENT_EXPR:
2929 case PREDECREMENT_EXPR:
2930 case POSTDECREMENT_EXPR:
2931 /* Handle complex lvalues (when permitted)
2932 by reduction to simpler cases. */
2934 val = unary_complex_lvalue (code, arg);
2935 if (val != 0)
2936 return val;
2938 /* Increment or decrement the real part of the value,
2939 and don't change the imaginary part. */
2940 if (typecode == COMPLEX_TYPE)
2942 tree real, imag;
2944 if (pedantic)
2945 pedwarn ("ISO C does not support `++' and `--' on complex types");
2947 arg = stabilize_reference (arg);
2948 real = build_unary_op (REALPART_EXPR, arg, 1);
2949 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2950 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2951 build_unary_op (code, real, 1), imag);
2954 /* Report invalid types. */
2956 if (typecode != POINTER_TYPE
2957 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2959 error ("wrong type argument to %s",
2960 code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2961 ? "increment" : "decrement");
2962 return error_mark_node;
2966 register tree inc;
2967 tree result_type = TREE_TYPE (arg);
2969 arg = get_unwidened (arg, 0);
2970 argtype = TREE_TYPE (arg);
2972 /* Compute the increment. */
2974 if (typecode == POINTER_TYPE)
2976 /* If pointer target is an undefined struct,
2977 we just cannot know how to do the arithmetic. */
2978 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2979 error ("%s of pointer to unknown structure",
2980 code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2981 ? "increment" : "decrement");
2982 else if ((pedantic || warn_pointer_arith)
2983 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2984 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2985 pedwarn ("wrong type argument to %s",
2986 code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2987 ? "increment" : "decrement");
2988 inc = c_size_in_bytes (TREE_TYPE (result_type));
2990 else
2991 inc = integer_one_node;
2993 inc = convert (argtype, inc);
2995 /* Handle incrementing a cast-expression. */
2997 while (1)
2998 switch (TREE_CODE (arg))
3000 case NOP_EXPR:
3001 case CONVERT_EXPR:
3002 case FLOAT_EXPR:
3003 case FIX_TRUNC_EXPR:
3004 case FIX_FLOOR_EXPR:
3005 case FIX_ROUND_EXPR:
3006 case FIX_CEIL_EXPR:
3007 pedantic_lvalue_warning (CONVERT_EXPR);
3008 /* If the real type has the same machine representation
3009 as the type it is cast to, we can make better output
3010 by adding directly to the inside of the cast. */
3011 if ((TREE_CODE (TREE_TYPE (arg))
3012 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
3013 && (TYPE_MODE (TREE_TYPE (arg))
3014 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
3015 arg = TREE_OPERAND (arg, 0);
3016 else
3018 tree incremented, modify, value;
3019 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3020 value = boolean_increment (code, arg);
3021 else
3023 arg = stabilize_reference (arg);
3024 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3025 value = arg;
3026 else
3027 value = save_expr (arg);
3028 incremented = build (((code == PREINCREMENT_EXPR
3029 || code == POSTINCREMENT_EXPR)
3030 ? PLUS_EXPR : MINUS_EXPR),
3031 argtype, value, inc);
3032 TREE_SIDE_EFFECTS (incremented) = 1;
3033 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3034 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3036 TREE_USED (value) = 1;
3037 return value;
3039 break;
3041 default:
3042 goto give_up;
3044 give_up:
3046 /* Complain about anything else that is not a true lvalue. */
3047 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3048 || code == POSTINCREMENT_EXPR)
3049 ? "invalid lvalue in increment"
3050 : "invalid lvalue in decrement")))
3051 return error_mark_node;
3053 /* Report a read-only lvalue. */
3054 if (TREE_READONLY (arg))
3055 readonly_warning (arg,
3056 ((code == PREINCREMENT_EXPR
3057 || code == POSTINCREMENT_EXPR)
3058 ? "increment" : "decrement"));
3060 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3061 val = boolean_increment (code, arg);
3062 else
3063 val = build (code, TREE_TYPE (arg), arg, inc);
3064 TREE_SIDE_EFFECTS (val) = 1;
3065 val = convert (result_type, val);
3066 if (TREE_CODE (val) != code)
3067 TREE_NO_UNUSED_WARNING (val) = 1;
3068 return val;
3071 case ADDR_EXPR:
3072 /* Note that this operation never does default_conversion
3073 regardless of NOCONVERT. */
3075 /* Let &* cancel out to simplify resulting code. */
3076 if (TREE_CODE (arg) == INDIRECT_REF)
3078 /* Don't let this be an lvalue. */
3079 if (lvalue_p (TREE_OPERAND (arg, 0)))
3080 return non_lvalue (TREE_OPERAND (arg, 0));
3081 return TREE_OPERAND (arg, 0);
3084 /* For &x[y], return x+y */
3085 if (TREE_CODE (arg) == ARRAY_REF)
3087 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
3088 return error_mark_node;
3089 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3090 TREE_OPERAND (arg, 1), 1);
3093 /* Handle complex lvalues (when permitted)
3094 by reduction to simpler cases. */
3095 val = unary_complex_lvalue (code, arg);
3096 if (val != 0)
3097 return val;
3099 #if 0 /* Turned off because inconsistent;
3100 float f; *&(int)f = 3.4 stores in int format
3101 whereas (int)f = 3.4 stores in float format. */
3102 /* Address of a cast is just a cast of the address
3103 of the operand of the cast. */
3104 switch (TREE_CODE (arg))
3106 case NOP_EXPR:
3107 case CONVERT_EXPR:
3108 case FLOAT_EXPR:
3109 case FIX_TRUNC_EXPR:
3110 case FIX_FLOOR_EXPR:
3111 case FIX_ROUND_EXPR:
3112 case FIX_CEIL_EXPR:
3113 if (pedantic)
3114 pedwarn ("ISO C forbids the address of a cast expression");
3115 return convert (build_pointer_type (TREE_TYPE (arg)),
3116 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3117 0));
3119 #endif
3121 /* Allow the address of a constructor if all the elements
3122 are constant. */
3123 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3125 /* Anything not already handled and not a true memory reference
3126 is an error. */
3127 else if (typecode != FUNCTION_TYPE
3128 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
3129 return error_mark_node;
3131 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3132 argtype = TREE_TYPE (arg);
3134 /* If the lvalue is const or volatile, merge that into the type
3135 to which the address will point. Note that you can't get a
3136 restricted pointer by taking the address of something, so we
3137 only have to deal with `const' and `volatile' here. */
3138 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3139 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3140 argtype = c_build_type_variant (argtype,
3141 TREE_READONLY (arg),
3142 TREE_THIS_VOLATILE (arg));
3144 argtype = build_pointer_type (argtype);
3146 if (mark_addressable (arg) == 0)
3147 return error_mark_node;
3150 tree addr;
3152 if (TREE_CODE (arg) == COMPONENT_REF)
3154 tree field = TREE_OPERAND (arg, 1);
3156 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3158 if (DECL_C_BIT_FIELD (field))
3160 error ("attempt to take address of bit-field structure member `%s'",
3161 IDENTIFIER_POINTER (DECL_NAME (field)));
3162 return error_mark_node;
3165 addr = fold (build (PLUS_EXPR, argtype,
3166 convert (argtype, addr),
3167 convert (argtype, byte_position (field))));
3169 else
3170 addr = build1 (code, argtype, arg);
3172 /* Address of a static or external variable or
3173 file-scope function counts as a constant. */
3174 if (staticp (arg)
3175 && ! (TREE_CODE (arg) == FUNCTION_DECL
3176 && DECL_CONTEXT (arg) != 0))
3177 TREE_CONSTANT (addr) = 1;
3178 return addr;
3181 default:
3182 break;
3185 if (argtype == 0)
3186 argtype = TREE_TYPE (arg);
3187 return fold (build1 (code, argtype, arg));
3190 #if 0
3191 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3192 convert ARG with the same conversions in the same order
3193 and return the result. */
3195 static tree
3196 convert_sequence (conversions, arg)
3197 tree conversions;
3198 tree arg;
3200 switch (TREE_CODE (conversions))
3202 case NOP_EXPR:
3203 case CONVERT_EXPR:
3204 case FLOAT_EXPR:
3205 case FIX_TRUNC_EXPR:
3206 case FIX_FLOOR_EXPR:
3207 case FIX_ROUND_EXPR:
3208 case FIX_CEIL_EXPR:
3209 return convert (TREE_TYPE (conversions),
3210 convert_sequence (TREE_OPERAND (conversions, 0),
3211 arg));
3213 default:
3214 return arg;
3217 #endif /* 0 */
3219 /* Return nonzero if REF is an lvalue valid for this language.
3220 Lvalues can be assigned, unless their type has TYPE_READONLY.
3221 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3224 lvalue_p (ref)
3225 tree ref;
3227 register enum tree_code code = TREE_CODE (ref);
3229 switch (code)
3231 case REALPART_EXPR:
3232 case IMAGPART_EXPR:
3233 case COMPONENT_REF:
3234 return lvalue_p (TREE_OPERAND (ref, 0));
3236 case STRING_CST:
3237 return 1;
3239 case INDIRECT_REF:
3240 case ARRAY_REF:
3241 case VAR_DECL:
3242 case PARM_DECL:
3243 case RESULT_DECL:
3244 case ERROR_MARK:
3245 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3246 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3248 case BIND_EXPR:
3249 case RTL_EXPR:
3250 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3252 default:
3253 return 0;
3257 /* Return nonzero if REF is an lvalue valid for this language;
3258 otherwise, print an error message and return zero. */
3261 lvalue_or_else (ref, msgid)
3262 tree ref;
3263 const char *msgid;
3265 int win = lvalue_p (ref);
3267 if (! win)
3268 error ("%s", msgid);
3270 return win;
3273 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3274 for certain kinds of expressions which are not really lvalues
3275 but which we can accept as lvalues.
3277 If ARG is not a kind of expression we can handle, return zero. */
3279 static tree
3280 unary_complex_lvalue (code, arg)
3281 enum tree_code code;
3282 tree arg;
3284 /* Handle (a, b) used as an "lvalue". */
3285 if (TREE_CODE (arg) == COMPOUND_EXPR)
3287 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3289 /* If this returns a function type, it isn't really being used as
3290 an lvalue, so don't issue a warning about it. */
3291 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3292 pedantic_lvalue_warning (COMPOUND_EXPR);
3294 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3295 TREE_OPERAND (arg, 0), real_result);
3298 /* Handle (a ? b : c) used as an "lvalue". */
3299 if (TREE_CODE (arg) == COND_EXPR)
3301 pedantic_lvalue_warning (COND_EXPR);
3302 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3303 pedantic_lvalue_warning (COMPOUND_EXPR);
3305 return (build_conditional_expr
3306 (TREE_OPERAND (arg, 0),
3307 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3308 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3311 return 0;
3314 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3315 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3317 static void
3318 pedantic_lvalue_warning (code)
3319 enum tree_code code;
3321 if (pedantic)
3322 switch (code)
3324 case COND_EXPR:
3325 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
3326 break;
3327 case COMPOUND_EXPR:
3328 pedwarn ("ISO C forbids use of compound expressions as lvalues");
3329 break;
3330 default:
3331 pedwarn ("ISO C forbids use of cast expressions as lvalues");
3332 break;
3336 /* Warn about storing in something that is `const'. */
3338 void
3339 readonly_warning (arg, msgid)
3340 tree arg;
3341 const char *msgid;
3343 if (TREE_CODE (arg) == COMPONENT_REF)
3345 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3346 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3347 else
3348 pedwarn ("%s of read-only member `%s'", _(msgid),
3349 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3351 else if (TREE_CODE (arg) == VAR_DECL)
3352 pedwarn ("%s of read-only variable `%s'", _(msgid),
3353 IDENTIFIER_POINTER (DECL_NAME (arg)));
3354 else
3355 pedwarn ("%s of read-only location", _(msgid));
3358 /* Mark EXP saying that we need to be able to take the
3359 address of it; it should not be allocated in a register.
3360 Value is 1 if successful. */
3363 mark_addressable (exp)
3364 tree exp;
3366 register tree x = exp;
3367 while (1)
3368 switch (TREE_CODE (x))
3370 case COMPONENT_REF:
3371 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3373 error ("cannot take address of bitfield `%s'",
3374 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3375 return 0;
3378 /* ... fall through ... */
3380 case ADDR_EXPR:
3381 case ARRAY_REF:
3382 case REALPART_EXPR:
3383 case IMAGPART_EXPR:
3384 x = TREE_OPERAND (x, 0);
3385 break;
3387 case CONSTRUCTOR:
3388 TREE_ADDRESSABLE (x) = 1;
3389 return 1;
3391 case VAR_DECL:
3392 case CONST_DECL:
3393 case PARM_DECL:
3394 case RESULT_DECL:
3395 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3396 && DECL_NONLOCAL (x))
3398 if (TREE_PUBLIC (x))
3400 error ("global register variable `%s' used in nested function",
3401 IDENTIFIER_POINTER (DECL_NAME (x)));
3402 return 0;
3404 pedwarn ("register variable `%s' used in nested function",
3405 IDENTIFIER_POINTER (DECL_NAME (x)));
3407 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3409 if (TREE_PUBLIC (x))
3411 error ("address of global register variable `%s' requested",
3412 IDENTIFIER_POINTER (DECL_NAME (x)));
3413 return 0;
3416 /* If we are making this addressable due to its having
3417 volatile components, give a different error message. Also
3418 handle the case of an unnamed parameter by not trying
3419 to give the name. */
3421 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3423 error ("cannot put object with volatile field into register");
3424 return 0;
3427 pedwarn ("address of register variable `%s' requested",
3428 IDENTIFIER_POINTER (DECL_NAME (x)));
3430 put_var_into_stack (x);
3432 /* drops in */
3433 case FUNCTION_DECL:
3434 TREE_ADDRESSABLE (x) = 1;
3435 #if 0 /* poplevel deals with this now. */
3436 if (DECL_CONTEXT (x) == 0)
3437 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3438 #endif
3440 default:
3441 return 1;
3445 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3447 tree
3448 build_conditional_expr (ifexp, op1, op2)
3449 tree ifexp, op1, op2;
3451 register tree type1;
3452 register tree type2;
3453 register enum tree_code code1;
3454 register enum tree_code code2;
3455 register tree result_type = NULL;
3456 tree orig_op1 = op1, orig_op2 = op2;
3458 ifexp = truthvalue_conversion (default_conversion (ifexp));
3460 #if 0 /* Produces wrong result if within sizeof. */
3461 /* Don't promote the operands separately if they promote
3462 the same way. Return the unpromoted type and let the combined
3463 value get promoted if necessary. */
3465 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3466 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3467 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3468 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3470 if (TREE_CODE (ifexp) == INTEGER_CST)
3471 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3473 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3475 #endif
3477 /* Promote both alternatives. */
3479 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3480 op1 = default_conversion (op1);
3481 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3482 op2 = default_conversion (op2);
3484 if (TREE_CODE (ifexp) == ERROR_MARK
3485 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3486 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3487 return error_mark_node;
3489 type1 = TREE_TYPE (op1);
3490 code1 = TREE_CODE (type1);
3491 type2 = TREE_TYPE (op2);
3492 code2 = TREE_CODE (type2);
3494 /* Quickly detect the usual case where op1 and op2 have the same type
3495 after promotion. */
3496 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3498 if (type1 == type2)
3499 result_type = type1;
3500 else
3501 result_type = TYPE_MAIN_VARIANT (type1);
3503 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3504 || code1 == COMPLEX_TYPE)
3505 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3506 || code2 == COMPLEX_TYPE))
3508 result_type = common_type (type1, type2);
3510 /* If -Wsign-compare, warn here if type1 and type2 have
3511 different signedness. We'll promote the signed to unsigned
3512 and later code won't know it used to be different.
3513 Do this check on the original types, so that explicit casts
3514 will be considered, but default promotions won't. */
3515 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare)
3516 && !skip_evaluation)
3518 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3519 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3521 if (unsigned_op1 ^ unsigned_op2)
3523 /* Do not warn if the result type is signed, since the
3524 signed type will only be chosen if it can represent
3525 all the values of the unsigned type. */
3526 if (! TREE_UNSIGNED (result_type))
3527 /* OK */;
3528 /* Do not warn if the signed quantity is an unsuffixed
3529 integer literal (or some static constant expression
3530 involving such literals) and it is non-negative. */
3531 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3532 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3533 /* OK */;
3534 else
3535 warning ("signed and unsigned type in conditional expression");
3539 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3541 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3542 pedwarn ("ISO C forbids conditional expr with only one void side");
3543 result_type = void_type_node;
3545 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3547 if (comp_target_types (type1, type2))
3548 result_type = common_type (type1, type2);
3549 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3550 && TREE_CODE (orig_op1) != NOP_EXPR)
3551 result_type = qualify_type (type2, type1);
3552 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3553 && TREE_CODE (orig_op2) != NOP_EXPR)
3554 result_type = qualify_type (type1, type2);
3555 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3557 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3558 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3559 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3560 TREE_TYPE (type2)));
3562 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3564 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3565 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3566 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3567 TREE_TYPE (type1)));
3569 else
3571 pedwarn ("pointer type mismatch in conditional expression");
3572 result_type = build_pointer_type (void_type_node);
3575 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3577 if (! integer_zerop (op2))
3578 pedwarn ("pointer/integer type mismatch in conditional expression");
3579 else
3581 op2 = null_pointer_node;
3583 result_type = type1;
3585 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3587 if (!integer_zerop (op1))
3588 pedwarn ("pointer/integer type mismatch in conditional expression");
3589 else
3591 op1 = null_pointer_node;
3593 result_type = type2;
3596 if (!result_type)
3598 if (flag_cond_mismatch)
3599 result_type = void_type_node;
3600 else
3602 error ("type mismatch in conditional expression");
3603 return error_mark_node;
3607 /* Merge const and volatile flags of the incoming types. */
3608 result_type
3609 = build_type_variant (result_type,
3610 TREE_READONLY (op1) || TREE_READONLY (op2),
3611 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3613 if (result_type != TREE_TYPE (op1))
3614 op1 = convert_and_check (result_type, op1);
3615 if (result_type != TREE_TYPE (op2))
3616 op2 = convert_and_check (result_type, op2);
3618 if (TREE_CODE (ifexp) == INTEGER_CST)
3619 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3621 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3624 /* Given a list of expressions, return a compound expression
3625 that performs them all and returns the value of the last of them. */
3627 tree
3628 build_compound_expr (list)
3629 tree list;
3631 return internal_build_compound_expr (list, TRUE);
3634 static tree
3635 internal_build_compound_expr (list, first_p)
3636 tree list;
3637 int first_p;
3639 register tree rest;
3641 if (TREE_CHAIN (list) == 0)
3643 /* Convert arrays to pointers when there really is a comma operator. */
3644 if (!first_p && TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
3645 TREE_VALUE (list) = default_conversion (TREE_VALUE (list));
3647 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3648 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3650 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3651 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3652 list = TREE_OPERAND (list, 0);
3653 #endif
3655 /* Don't let (0, 0) be null pointer constant. */
3656 if (!first_p && integer_zerop (TREE_VALUE (list)))
3657 return non_lvalue (TREE_VALUE (list));
3658 return TREE_VALUE (list);
3661 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3663 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3665 /* The left-hand operand of a comma expression is like an expression
3666 statement: with -W or -Wunused, we should warn if it doesn't have
3667 any side-effects, unless it was explicitly cast to (void). */
3668 if ((extra_warnings || warn_unused_value)
3669 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3670 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
3671 warning ("left-hand operand of comma expression has no effect");
3673 /* When pedantic, a compound expression can be neither an lvalue
3674 nor an integer constant expression. */
3675 if (! pedantic)
3676 return rest;
3679 /* With -Wunused, we should also warn if the left-hand operand does have
3680 side-effects, but computes a value which is not used. For example, in
3681 `foo() + bar(), baz()' the result of the `+' operator is not used,
3682 so we should issue a warning. */
3683 else if (warn_unused_value)
3684 warn_if_unused_value (TREE_VALUE (list));
3686 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3689 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3691 tree
3692 build_c_cast (type, expr)
3693 register tree type;
3694 tree expr;
3696 register tree value = expr;
3698 if (type == error_mark_node || expr == error_mark_node)
3699 return error_mark_node;
3700 type = TYPE_MAIN_VARIANT (type);
3702 #if 0
3703 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3704 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3705 value = TREE_OPERAND (value, 0);
3706 #endif
3708 if (TREE_CODE (type) == ARRAY_TYPE)
3710 error ("cast specifies array type");
3711 return error_mark_node;
3714 if (TREE_CODE (type) == FUNCTION_TYPE)
3716 error ("cast specifies function type");
3717 return error_mark_node;
3720 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3722 if (pedantic)
3724 if (TREE_CODE (type) == RECORD_TYPE
3725 || TREE_CODE (type) == UNION_TYPE)
3726 pedwarn ("ISO C forbids casting nonscalar to the same type");
3729 else if (TREE_CODE (type) == UNION_TYPE)
3731 tree field;
3732 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3733 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3734 value = default_conversion (value);
3736 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3737 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3738 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3739 break;
3741 if (field)
3743 const char *name;
3744 tree t;
3746 if (pedantic)
3747 pedwarn ("ISO C forbids casts to union type");
3748 if (TYPE_NAME (type) != 0)
3750 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3751 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3752 else
3753 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3755 else
3756 name = "";
3757 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3758 build_tree_list (field, value)),
3759 0, 0);
3760 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3761 return t;
3763 error ("cast to union type from type not present in union");
3764 return error_mark_node;
3766 else
3768 tree otype, ovalue;
3770 /* If casting to void, avoid the error that would come
3771 from default_conversion in the case of a non-lvalue array. */
3772 if (type == void_type_node)
3773 return build1 (CONVERT_EXPR, type, value);
3775 /* Convert functions and arrays to pointers,
3776 but don't convert any other types. */
3777 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3778 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3779 value = default_conversion (value);
3780 otype = TREE_TYPE (value);
3782 /* Optionally warn about potentially worrisome casts. */
3784 if (warn_cast_qual
3785 && TREE_CODE (type) == POINTER_TYPE
3786 && TREE_CODE (otype) == POINTER_TYPE)
3788 tree in_type = type;
3789 tree in_otype = otype;
3790 int warn = 0;
3792 /* Check that the qualifiers on IN_TYPE are a superset of
3793 the qualifiers of IN_OTYPE. The outermost level of
3794 POINTER_TYPE nodes is uninteresting and we stop as soon
3795 as we hit a non-POINTER_TYPE node on either type. */
3798 in_otype = TREE_TYPE (in_otype);
3799 in_type = TREE_TYPE (in_type);
3800 warn |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3802 while (TREE_CODE (in_type) == POINTER_TYPE
3803 && TREE_CODE (in_otype) == POINTER_TYPE);
3805 if (warn)
3806 /* There are qualifiers present in IN_OTYPE that are not
3807 present in IN_TYPE. */
3808 warning ("cast discards qualifiers from pointer target type");
3811 /* Warn about possible alignment problems. */
3812 if (STRICT_ALIGNMENT && warn_cast_align
3813 && TREE_CODE (type) == POINTER_TYPE
3814 && TREE_CODE (otype) == POINTER_TYPE
3815 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3816 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3817 /* Don't warn about opaque types, where the actual alignment
3818 restriction is unknown. */
3819 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3820 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3821 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3822 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3823 warning ("cast increases required alignment of target type");
3825 if (TREE_CODE (type) == INTEGER_TYPE
3826 && TREE_CODE (otype) == POINTER_TYPE
3827 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3828 && !TREE_CONSTANT (value))
3829 warning ("cast from pointer to integer of different size");
3831 if (warn_bad_function_cast
3832 && TREE_CODE (value) == CALL_EXPR
3833 && TREE_CODE (type) != TREE_CODE (otype))
3834 warning ("cast does not match function type");
3836 if (TREE_CODE (type) == POINTER_TYPE
3837 && TREE_CODE (otype) == INTEGER_TYPE
3838 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3839 /* Don't warn about converting any constant. */
3840 && !TREE_CONSTANT (value))
3841 warning ("cast to pointer from integer of different size");
3843 ovalue = value;
3844 value = convert (type, value);
3846 /* Ignore any integer overflow caused by the cast. */
3847 if (TREE_CODE (value) == INTEGER_CST)
3849 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3850 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3854 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3855 if (pedantic && TREE_CODE (value) == INTEGER_CST
3856 && TREE_CODE (expr) == INTEGER_CST
3857 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3858 value = non_lvalue (value);
3860 /* If pedantic, don't let a cast be an lvalue. */
3861 if (value == expr && pedantic)
3862 value = non_lvalue (value);
3864 return value;
3867 /* Interpret a cast of expression EXPR to type TYPE. */
3868 tree
3869 c_cast_expr (type, expr)
3870 tree type, expr;
3872 int saved_wsp = warn_strict_prototypes;
3874 /* This avoids warnings about unprototyped casts on
3875 integers. E.g. "#define SIG_DFL (void(*)())0". */
3876 if (TREE_CODE (expr) == INTEGER_CST)
3877 warn_strict_prototypes = 0;
3878 type = groktypename (type);
3879 warn_strict_prototypes = saved_wsp;
3881 return build_c_cast (type, expr);
3885 /* Build an assignment expression of lvalue LHS from value RHS.
3886 MODIFYCODE is the code for a binary operator that we use
3887 to combine the old value of LHS with RHS to get the new value.
3888 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3890 tree
3891 build_modify_expr (lhs, modifycode, rhs)
3892 tree lhs, rhs;
3893 enum tree_code modifycode;
3895 register tree result;
3896 tree newrhs;
3897 tree lhstype = TREE_TYPE (lhs);
3898 tree olhstype = lhstype;
3900 /* Types that aren't fully specified cannot be used in assignments. */
3901 lhs = require_complete_type (lhs);
3903 /* Avoid duplicate error messages from operands that had errors. */
3904 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3905 return error_mark_node;
3907 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3908 /* Do not use STRIP_NOPS here. We do not want an enumerator
3909 whose value is 0 to count as a null pointer constant. */
3910 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3911 rhs = TREE_OPERAND (rhs, 0);
3913 newrhs = rhs;
3915 /* Handle control structure constructs used as "lvalues". */
3917 switch (TREE_CODE (lhs))
3919 /* Handle (a, b) used as an "lvalue". */
3920 case COMPOUND_EXPR:
3921 pedantic_lvalue_warning (COMPOUND_EXPR);
3922 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3923 if (TREE_CODE (newrhs) == ERROR_MARK)
3924 return error_mark_node;
3925 return build (COMPOUND_EXPR, lhstype,
3926 TREE_OPERAND (lhs, 0), newrhs);
3928 /* Handle (a ? b : c) used as an "lvalue". */
3929 case COND_EXPR:
3930 pedantic_lvalue_warning (COND_EXPR);
3931 rhs = save_expr (rhs);
3933 /* Produce (a ? (b = rhs) : (c = rhs))
3934 except that the RHS goes through a save-expr
3935 so the code to compute it is only emitted once. */
3936 tree cond
3937 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3938 build_modify_expr (TREE_OPERAND (lhs, 1),
3939 modifycode, rhs),
3940 build_modify_expr (TREE_OPERAND (lhs, 2),
3941 modifycode, rhs));
3942 if (TREE_CODE (cond) == ERROR_MARK)
3943 return cond;
3944 /* Make sure the code to compute the rhs comes out
3945 before the split. */
3946 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3947 /* But cast it to void to avoid an "unused" error. */
3948 convert (void_type_node, rhs), cond);
3950 default:
3951 break;
3954 /* If a binary op has been requested, combine the old LHS value with the RHS
3955 producing the value we should actually store into the LHS. */
3957 if (modifycode != NOP_EXPR)
3959 lhs = stabilize_reference (lhs);
3960 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3963 /* Handle a cast used as an "lvalue".
3964 We have already performed any binary operator using the value as cast.
3965 Now convert the result to the cast type of the lhs,
3966 and then true type of the lhs and store it there;
3967 then convert result back to the cast type to be the value
3968 of the assignment. */
3970 switch (TREE_CODE (lhs))
3972 case NOP_EXPR:
3973 case CONVERT_EXPR:
3974 case FLOAT_EXPR:
3975 case FIX_TRUNC_EXPR:
3976 case FIX_FLOOR_EXPR:
3977 case FIX_ROUND_EXPR:
3978 case FIX_CEIL_EXPR:
3979 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3980 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3981 newrhs = default_conversion (newrhs);
3983 tree inner_lhs = TREE_OPERAND (lhs, 0);
3984 tree result;
3985 result = build_modify_expr (inner_lhs, NOP_EXPR,
3986 convert (TREE_TYPE (inner_lhs),
3987 convert (lhstype, newrhs)));
3988 if (TREE_CODE (result) == ERROR_MARK)
3989 return result;
3990 pedantic_lvalue_warning (CONVERT_EXPR);
3991 return convert (TREE_TYPE (lhs), result);
3994 default:
3995 break;
3998 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3999 Reject anything strange now. */
4001 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
4002 return error_mark_node;
4004 /* Warn about storing in something that is `const'. */
4006 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
4007 || ((TREE_CODE (lhstype) == RECORD_TYPE
4008 || TREE_CODE (lhstype) == UNION_TYPE)
4009 && C_TYPE_FIELDS_READONLY (lhstype)))
4010 readonly_warning (lhs, "assignment");
4012 /* If storing into a structure or union member,
4013 it has probably been given type `int'.
4014 Compute the type that would go with
4015 the actual amount of storage the member occupies. */
4017 if (TREE_CODE (lhs) == COMPONENT_REF
4018 && (TREE_CODE (lhstype) == INTEGER_TYPE
4019 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4020 || TREE_CODE (lhstype) == REAL_TYPE
4021 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4022 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4024 /* If storing in a field that is in actuality a short or narrower than one,
4025 we must store in the field in its actual type. */
4027 if (lhstype != TREE_TYPE (lhs))
4029 lhs = copy_node (lhs);
4030 TREE_TYPE (lhs) = lhstype;
4033 /* Convert new value to destination type. */
4035 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
4036 NULL_TREE, NULL_TREE, 0);
4037 if (TREE_CODE (newrhs) == ERROR_MARK)
4038 return error_mark_node;
4040 /* Scan operands */
4042 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4043 TREE_SIDE_EFFECTS (result) = 1;
4045 /* If we got the LHS in a different type for storing in,
4046 convert the result back to the nominal type of LHS
4047 so that the value we return always has the same type
4048 as the LHS argument. */
4050 if (olhstype == TREE_TYPE (result))
4051 return result;
4052 return convert_for_assignment (olhstype, result, _("assignment"),
4053 NULL_TREE, NULL_TREE, 0);
4056 /* Convert value RHS to type TYPE as preparation for an assignment
4057 to an lvalue of type TYPE.
4058 The real work of conversion is done by `convert'.
4059 The purpose of this function is to generate error messages
4060 for assignments that are not allowed in C.
4061 ERRTYPE is a string to use in error messages:
4062 "assignment", "return", etc. If it is null, this is parameter passing
4063 for a function call (and different error messages are output).
4065 FUNNAME is the name of the function being called,
4066 as an IDENTIFIER_NODE, or null.
4067 PARMNUM is the number of the argument, for printing in error messages. */
4069 static tree
4070 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
4071 tree type, rhs;
4072 const char *errtype;
4073 tree fundecl, funname;
4074 int parmnum;
4076 register enum tree_code codel = TREE_CODE (type);
4077 register tree rhstype;
4078 register enum tree_code coder;
4080 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4081 /* Do not use STRIP_NOPS here. We do not want an enumerator
4082 whose value is 0 to count as a null pointer constant. */
4083 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4084 rhs = TREE_OPERAND (rhs, 0);
4086 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4087 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4088 rhs = default_conversion (rhs);
4089 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4090 rhs = decl_constant_value_for_broken_optimization (rhs);
4092 rhstype = TREE_TYPE (rhs);
4093 coder = TREE_CODE (rhstype);
4095 if (coder == ERROR_MARK)
4096 return error_mark_node;
4098 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4100 overflow_warning (rhs);
4101 /* Check for Objective-C protocols. This will issue a warning if
4102 there are protocol violations. No need to use the return value. */
4103 maybe_objc_comptypes (type, rhstype, 0);
4104 return rhs;
4107 if (coder == VOID_TYPE)
4109 error ("void value not ignored as it ought to be");
4110 return error_mark_node;
4112 /* A type converts to a reference to it.
4113 This code doesn't fully support references, it's just for the
4114 special case of va_start and va_copy. */
4115 if (codel == REFERENCE_TYPE
4116 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4118 if (mark_addressable (rhs) == 0)
4119 return error_mark_node;
4120 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4122 /* We already know that these two types are compatible, but they
4123 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4124 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4125 likely to be va_list, a typedef to __builtin_va_list, which
4126 is different enough that it will cause problems later. */
4127 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4128 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4130 rhs = build1 (NOP_EXPR, type, rhs);
4131 return rhs;
4133 /* Arithmetic types all interconvert, and enum is treated like int. */
4134 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4135 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4136 || codel == BOOLEAN_TYPE)
4137 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4138 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4139 || coder == BOOLEAN_TYPE))
4140 return convert_and_check (type, rhs);
4142 /* Conversion to a transparent union from its member types.
4143 This applies only to function arguments. */
4144 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4146 tree memb_types;
4147 tree marginal_memb_type = 0;
4149 for (memb_types = TYPE_FIELDS (type); memb_types;
4150 memb_types = TREE_CHAIN (memb_types))
4152 tree memb_type = TREE_TYPE (memb_types);
4154 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4155 TYPE_MAIN_VARIANT (rhstype)))
4156 break;
4158 if (TREE_CODE (memb_type) != POINTER_TYPE)
4159 continue;
4161 if (coder == POINTER_TYPE)
4163 register tree ttl = TREE_TYPE (memb_type);
4164 register tree ttr = TREE_TYPE (rhstype);
4166 /* Any non-function converts to a [const][volatile] void *
4167 and vice versa; otherwise, targets must be the same.
4168 Meanwhile, the lhs target must have all the qualifiers of
4169 the rhs. */
4170 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4171 || comp_target_types (memb_type, rhstype))
4173 /* If this type won't generate any warnings, use it. */
4174 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4175 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4176 && TREE_CODE (ttl) == FUNCTION_TYPE)
4177 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4178 == TYPE_QUALS (ttr))
4179 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4180 == TYPE_QUALS (ttl))))
4181 break;
4183 /* Keep looking for a better type, but remember this one. */
4184 if (! marginal_memb_type)
4185 marginal_memb_type = memb_type;
4189 /* Can convert integer zero to any pointer type. */
4190 if (integer_zerop (rhs)
4191 || (TREE_CODE (rhs) == NOP_EXPR
4192 && integer_zerop (TREE_OPERAND (rhs, 0))))
4194 rhs = null_pointer_node;
4195 break;
4199 if (memb_types || marginal_memb_type)
4201 if (! memb_types)
4203 /* We have only a marginally acceptable member type;
4204 it needs a warning. */
4205 register tree ttl = TREE_TYPE (marginal_memb_type);
4206 register tree ttr = TREE_TYPE (rhstype);
4208 /* Const and volatile mean something different for function
4209 types, so the usual warnings are not appropriate. */
4210 if (TREE_CODE (ttr) == FUNCTION_TYPE
4211 && TREE_CODE (ttl) == FUNCTION_TYPE)
4213 /* Because const and volatile on functions are
4214 restrictions that say the function will not do
4215 certain things, it is okay to use a const or volatile
4216 function where an ordinary one is wanted, but not
4217 vice-versa. */
4218 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4219 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4220 errtype, funname, parmnum);
4222 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4223 warn_for_assignment ("%s discards qualifiers from pointer target type",
4224 errtype, funname,
4225 parmnum);
4228 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4229 pedwarn ("ISO C prohibits argument conversion to union type");
4231 return build1 (NOP_EXPR, type, rhs);
4235 /* Conversions among pointers */
4236 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4237 && (coder == POINTER_TYPE || coder == REFERENCE_TYPE))
4239 register tree ttl = TREE_TYPE (type);
4240 register tree ttr = TREE_TYPE (rhstype);
4242 /* Any non-function converts to a [const][volatile] void *
4243 and vice versa; otherwise, targets must be the same.
4244 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4245 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4246 || comp_target_types (type, rhstype)
4247 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4248 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4250 if (pedantic
4251 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4253 (VOID_TYPE_P (ttr)
4254 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4255 which are not ANSI null ptr constants. */
4256 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4257 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4258 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
4259 errtype, funname, parmnum);
4260 /* Const and volatile mean something different for function types,
4261 so the usual warnings are not appropriate. */
4262 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4263 && TREE_CODE (ttl) != FUNCTION_TYPE)
4265 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4266 warn_for_assignment ("%s discards qualifiers from pointer target type",
4267 errtype, funname, parmnum);
4268 /* If this is not a case of ignoring a mismatch in signedness,
4269 no warning. */
4270 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4271 || comp_target_types (type, rhstype))
4273 /* If there is a mismatch, do warn. */
4274 else if (pedantic)
4275 warn_for_assignment ("pointer targets in %s differ in signedness",
4276 errtype, funname, parmnum);
4278 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4279 && TREE_CODE (ttr) == FUNCTION_TYPE)
4281 /* Because const and volatile on functions are restrictions
4282 that say the function will not do certain things,
4283 it is okay to use a const or volatile function
4284 where an ordinary one is wanted, but not vice-versa. */
4285 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4286 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4287 errtype, funname, parmnum);
4290 else
4291 warn_for_assignment ("%s from incompatible pointer type",
4292 errtype, funname, parmnum);
4293 return convert (type, rhs);
4295 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4297 /* An explicit constant 0 can convert to a pointer,
4298 or one that results from arithmetic, even including
4299 a cast to integer type. */
4300 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4302 ! (TREE_CODE (rhs) == NOP_EXPR
4303 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4304 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4305 && integer_zerop (TREE_OPERAND (rhs, 0))))
4307 warn_for_assignment ("%s makes pointer from integer without a cast",
4308 errtype, funname, parmnum);
4309 return convert (type, rhs);
4311 return null_pointer_node;
4313 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4315 warn_for_assignment ("%s makes integer from pointer without a cast",
4316 errtype, funname, parmnum);
4317 return convert (type, rhs);
4319 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4320 return convert (type, rhs);
4322 if (!errtype)
4324 if (funname)
4326 tree selector = maybe_building_objc_message_expr ();
4328 if (selector && parmnum > 2)
4329 error ("incompatible type for argument %d of `%s'",
4330 parmnum - 2, IDENTIFIER_POINTER (selector));
4331 else
4332 error ("incompatible type for argument %d of `%s'",
4333 parmnum, IDENTIFIER_POINTER (funname));
4335 else
4336 error ("incompatible type for argument %d of indirect function call",
4337 parmnum);
4339 else
4340 error ("incompatible types in %s", errtype);
4342 return error_mark_node;
4345 /* Print a warning using MSGID.
4346 It gets OPNAME as its one parameter.
4347 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4348 FUNCTION and ARGNUM are handled specially if we are building an
4349 Objective-C selector. */
4351 static void
4352 warn_for_assignment (msgid, opname, function, argnum)
4353 const char *msgid;
4354 const char *opname;
4355 tree function;
4356 int argnum;
4358 if (opname == 0)
4360 tree selector = maybe_building_objc_message_expr ();
4361 char * new_opname;
4363 if (selector && argnum > 2)
4365 function = selector;
4366 argnum -= 2;
4368 if (function)
4370 /* Function name is known; supply it. */
4371 const char *argstring = _("passing arg %d of `%s'");
4372 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4373 + strlen (argstring) + 1 + 25
4374 /*%d*/ + 1);
4375 sprintf (new_opname, argstring, argnum,
4376 IDENTIFIER_POINTER (function));
4378 else
4380 /* Function name unknown (call through ptr); just give arg number.*/
4381 const char *argnofun = _("passing arg %d of pointer to function");
4382 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4383 sprintf (new_opname, argnofun, argnum);
4385 opname = new_opname;
4387 pedwarn (msgid, opname);
4390 /* If VALUE is a compound expr all of whose expressions are constant, then
4391 return its value. Otherwise, return error_mark_node.
4393 This is for handling COMPOUND_EXPRs as initializer elements
4394 which is allowed with a warning when -pedantic is specified. */
4396 static tree
4397 valid_compound_expr_initializer (value, endtype)
4398 tree value;
4399 tree endtype;
4401 if (TREE_CODE (value) == COMPOUND_EXPR)
4403 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4404 == error_mark_node)
4405 return error_mark_node;
4406 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4407 endtype);
4409 else if (! TREE_CONSTANT (value)
4410 && ! initializer_constant_valid_p (value, endtype))
4411 return error_mark_node;
4412 else
4413 return value;
4416 /* Perform appropriate conversions on the initial value of a variable,
4417 store it in the declaration DECL,
4418 and print any error messages that are appropriate.
4419 If the init is invalid, store an ERROR_MARK. */
4421 void
4422 store_init_value (decl, init)
4423 tree decl, init;
4425 register tree value, type;
4427 /* If variable's type was invalidly declared, just ignore it. */
4429 type = TREE_TYPE (decl);
4430 if (TREE_CODE (type) == ERROR_MARK)
4431 return;
4433 /* Digest the specified initializer into an expression. */
4435 value = digest_init (type, init, TREE_STATIC (decl),
4436 TREE_STATIC (decl) || (pedantic && !flag_isoc99));
4438 /* Store the expression if valid; else report error. */
4440 #if 0
4441 /* Note that this is the only place we can detect the error
4442 in a case such as struct foo bar = (struct foo) { x, y };
4443 where there is one initial value which is a constructor expression. */
4444 if (value == error_mark_node)
4446 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4448 error ("initializer for static variable is not constant");
4449 value = error_mark_node;
4451 else if (TREE_STATIC (decl)
4452 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4454 error ("initializer for static variable uses complicated arithmetic");
4455 value = error_mark_node;
4457 else
4459 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4461 if (! TREE_CONSTANT (value))
4462 pedwarn ("aggregate initializer is not constant");
4463 else if (! TREE_STATIC (value))
4464 pedwarn ("aggregate initializer uses complicated arithmetic");
4467 #endif
4469 if (warn_traditional && !in_system_header
4470 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
4471 warning ("traditional C rejects automatic aggregate initialization");
4473 DECL_INITIAL (decl) = value;
4475 /* ANSI wants warnings about out-of-range constant initializers. */
4476 STRIP_TYPE_NOPS (value);
4477 constant_expression_warning (value);
4480 /* Methods for storing and printing names for error messages. */
4482 /* Implement a spelling stack that allows components of a name to be pushed
4483 and popped. Each element on the stack is this structure. */
4485 struct spelling
4487 int kind;
4488 union
4490 int i;
4491 const char *s;
4492 } u;
4495 #define SPELLING_STRING 1
4496 #define SPELLING_MEMBER 2
4497 #define SPELLING_BOUNDS 3
4499 static struct spelling *spelling; /* Next stack element (unused). */
4500 static struct spelling *spelling_base; /* Spelling stack base. */
4501 static int spelling_size; /* Size of the spelling stack. */
4503 /* Macros to save and restore the spelling stack around push_... functions.
4504 Alternative to SAVE_SPELLING_STACK. */
4506 #define SPELLING_DEPTH() (spelling - spelling_base)
4507 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4509 /* Save and restore the spelling stack around arbitrary C code. */
4511 #define SAVE_SPELLING_DEPTH(code) \
4513 int __depth = SPELLING_DEPTH (); \
4514 code; \
4515 RESTORE_SPELLING_DEPTH (__depth); \
4518 /* Push an element on the spelling stack with type KIND and assign VALUE
4519 to MEMBER. */
4521 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4523 int depth = SPELLING_DEPTH (); \
4525 if (depth >= spelling_size) \
4527 spelling_size += 10; \
4528 if (spelling_base == 0) \
4529 spelling_base \
4530 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4531 else \
4532 spelling_base \
4533 = (struct spelling *) xrealloc (spelling_base, \
4534 spelling_size * sizeof (struct spelling)); \
4535 RESTORE_SPELLING_DEPTH (depth); \
4538 spelling->kind = (KIND); \
4539 spelling->MEMBER = (VALUE); \
4540 spelling++; \
4543 /* Push STRING on the stack. Printed literally. */
4545 static void
4546 push_string (string)
4547 const char *string;
4549 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4552 /* Push a member name on the stack. Printed as '.' STRING. */
4554 static void
4555 push_member_name (decl)
4556 tree decl;
4559 const char *string
4560 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4561 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4564 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4566 static void
4567 push_array_bounds (bounds)
4568 int bounds;
4570 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4573 /* Compute the maximum size in bytes of the printed spelling. */
4575 static int
4576 spelling_length ()
4578 register int size = 0;
4579 register struct spelling *p;
4581 for (p = spelling_base; p < spelling; p++)
4583 if (p->kind == SPELLING_BOUNDS)
4584 size += 25;
4585 else
4586 size += strlen (p->u.s) + 1;
4589 return size;
4592 /* Print the spelling to BUFFER and return it. */
4594 static char *
4595 print_spelling (buffer)
4596 register char *buffer;
4598 register char *d = buffer;
4599 register struct spelling *p;
4601 for (p = spelling_base; p < spelling; p++)
4602 if (p->kind == SPELLING_BOUNDS)
4604 sprintf (d, "[%d]", p->u.i);
4605 d += strlen (d);
4607 else
4609 register const char *s;
4610 if (p->kind == SPELLING_MEMBER)
4611 *d++ = '.';
4612 for (s = p->u.s; (*d = *s++); d++)
4615 *d++ = '\0';
4616 return buffer;
4619 /* Issue an error message for a bad initializer component.
4620 MSGID identifies the message.
4621 The component name is taken from the spelling stack. */
4623 void
4624 error_init (msgid)
4625 const char *msgid;
4627 char *ofwhat;
4629 error ("%s", msgid);
4630 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4631 if (*ofwhat)
4632 error ("(near initialization for `%s')", ofwhat);
4635 /* Issue a pedantic warning for a bad initializer component.
4636 MSGID identifies the message.
4637 The component name is taken from the spelling stack. */
4639 void
4640 pedwarn_init (msgid)
4641 const char *msgid;
4643 char *ofwhat;
4645 pedwarn ("%s", msgid);
4646 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4647 if (*ofwhat)
4648 pedwarn ("(near initialization for `%s')", ofwhat);
4651 /* Issue a warning for a bad initializer component.
4652 MSGID identifies the message.
4653 The component name is taken from the spelling stack. */
4655 static void
4656 warning_init (msgid)
4657 const char *msgid;
4659 char *ofwhat;
4661 warning ("%s", msgid);
4662 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4663 if (*ofwhat)
4664 warning ("(near initialization for `%s')", ofwhat);
4667 /* Digest the parser output INIT as an initializer for type TYPE.
4668 Return a C expression of type TYPE to represent the initial value.
4670 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4671 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4672 applies only to elements of constructors. */
4674 static tree
4675 digest_init (type, init, require_constant, constructor_constant)
4676 tree type, init;
4677 int require_constant, constructor_constant;
4679 enum tree_code code = TREE_CODE (type);
4680 tree inside_init = init;
4682 if (type == error_mark_node
4683 || init == error_mark_node
4684 || TREE_TYPE (init) == error_mark_node)
4685 return error_mark_node;
4687 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4688 /* Do not use STRIP_NOPS here. We do not want an enumerator
4689 whose value is 0 to count as a null pointer constant. */
4690 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4691 inside_init = TREE_OPERAND (init, 0);
4693 inside_init = fold (inside_init);
4695 /* Initialization of an array of chars from a string constant
4696 optionally enclosed in braces. */
4698 if (code == ARRAY_TYPE)
4700 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4701 if ((typ1 == char_type_node
4702 || typ1 == signed_char_type_node
4703 || typ1 == unsigned_char_type_node
4704 || typ1 == unsigned_wchar_type_node
4705 || typ1 == signed_wchar_type_node)
4706 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4708 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4709 TYPE_MAIN_VARIANT (type)))
4710 return inside_init;
4712 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4713 != char_type_node)
4714 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4716 error_init ("char-array initialized from wide string");
4717 return error_mark_node;
4719 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4720 == char_type_node)
4721 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4723 error_init ("int-array initialized from non-wide string");
4724 return error_mark_node;
4727 TREE_TYPE (inside_init) = type;
4728 if (TYPE_DOMAIN (type) != 0
4729 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4730 /* Subtract 1 (or sizeof (wchar_t))
4731 because it's ok to ignore the terminating null char
4732 that is counted in the length of the constant. */
4733 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4734 TREE_STRING_LENGTH (inside_init)
4735 - ((TYPE_PRECISION (typ1)
4736 != TYPE_PRECISION (char_type_node))
4737 ? (TYPE_PRECISION (wchar_type_node)
4738 / BITS_PER_UNIT)
4739 : 1)))
4740 pedwarn_init ("initializer-string for array of chars is too long");
4742 return inside_init;
4746 /* Any type can be initialized
4747 from an expression of the same type, optionally with braces. */
4749 if (inside_init && TREE_TYPE (inside_init) != 0
4750 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4751 TYPE_MAIN_VARIANT (type))
4752 || (code == ARRAY_TYPE
4753 && comptypes (TREE_TYPE (inside_init), type))
4754 || (code == POINTER_TYPE
4755 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4756 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4757 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4758 TREE_TYPE (type)))))
4760 if (code == POINTER_TYPE
4761 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4762 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4763 inside_init = default_conversion (inside_init);
4764 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4765 && TREE_CODE (inside_init) != CONSTRUCTOR)
4767 error_init ("array initialized from non-constant array expression");
4768 return error_mark_node;
4771 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4772 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4774 /* Compound expressions can only occur here if -pedantic or
4775 -pedantic-errors is specified. In the later case, we always want
4776 an error. In the former case, we simply want a warning. */
4777 if (require_constant && pedantic
4778 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4780 inside_init
4781 = valid_compound_expr_initializer (inside_init,
4782 TREE_TYPE (inside_init));
4783 if (inside_init == error_mark_node)
4784 error_init ("initializer element is not constant");
4785 else
4786 pedwarn_init ("initializer element is not constant");
4787 if (flag_pedantic_errors)
4788 inside_init = error_mark_node;
4790 else if (require_constant
4791 && (!TREE_CONSTANT (inside_init)
4792 /* This test catches things like `7 / 0' which
4793 result in an expression for which TREE_CONSTANT
4794 is true, but which is not actually something
4795 that is a legal constant. We really should not
4796 be using this function, because it is a part of
4797 the back-end. Instead, the expression should
4798 already have been turned into ERROR_MARK_NODE. */
4799 || !initializer_constant_valid_p (inside_init,
4800 TREE_TYPE (inside_init))))
4802 error_init ("initializer element is not constant");
4803 inside_init = error_mark_node;
4806 return inside_init;
4809 /* Handle scalar types, including conversions. */
4811 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4812 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4814 /* Note that convert_for_assignment calls default_conversion
4815 for arrays and functions. We must not call it in the
4816 case where inside_init is a null pointer constant. */
4817 inside_init
4818 = convert_for_assignment (type, init, _("initialization"),
4819 NULL_TREE, NULL_TREE, 0);
4821 if (require_constant && ! TREE_CONSTANT (inside_init))
4823 error_init ("initializer element is not constant");
4824 inside_init = error_mark_node;
4826 else if (require_constant
4827 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4829 error_init ("initializer element is not computable at load time");
4830 inside_init = error_mark_node;
4833 return inside_init;
4836 /* Come here only for records and arrays. */
4838 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4840 error_init ("variable-sized object may not be initialized");
4841 return error_mark_node;
4844 /* Traditionally, you can write struct foo x = 0;
4845 and it initializes the first element of x to 0. */
4846 if (flag_traditional)
4848 tree top = 0, prev = 0, otype = type;
4849 while (TREE_CODE (type) == RECORD_TYPE
4850 || TREE_CODE (type) == ARRAY_TYPE
4851 || TREE_CODE (type) == QUAL_UNION_TYPE
4852 || TREE_CODE (type) == UNION_TYPE)
4854 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4855 if (prev == 0)
4856 top = temp;
4857 else
4858 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4859 prev = temp;
4860 if (TREE_CODE (type) == ARRAY_TYPE)
4861 type = TREE_TYPE (type);
4862 else if (TYPE_FIELDS (type))
4863 type = TREE_TYPE (TYPE_FIELDS (type));
4864 else
4866 error_init ("invalid initializer");
4867 return error_mark_node;
4871 if (otype != type)
4873 TREE_OPERAND (prev, 1)
4874 = build_tree_list (NULL_TREE,
4875 digest_init (type, init, require_constant,
4876 constructor_constant));
4877 return top;
4879 else
4880 return error_mark_node;
4882 error_init ("invalid initializer");
4883 return error_mark_node;
4886 /* Handle initializers that use braces. */
4888 /* Type of object we are accumulating a constructor for.
4889 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4890 static tree constructor_type;
4892 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4893 left to fill. */
4894 static tree constructor_fields;
4896 /* For an ARRAY_TYPE, this is the specified index
4897 at which to store the next element we get. */
4898 static tree constructor_index;
4900 /* For an ARRAY_TYPE, this is the maximum index. */
4901 static tree constructor_max_index;
4903 /* For a RECORD_TYPE, this is the first field not yet written out. */
4904 static tree constructor_unfilled_fields;
4906 /* For an ARRAY_TYPE, this is the index of the first element
4907 not yet written out. */
4908 static tree constructor_unfilled_index;
4910 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4911 This is so we can generate gaps between fields, when appropriate. */
4912 static tree constructor_bit_index;
4914 /* If we are saving up the elements rather than allocating them,
4915 this is the list of elements so far (in reverse order,
4916 most recent first). */
4917 static tree constructor_elements;
4919 /* 1 if constructor should be incrementally stored into a constructor chain,
4920 0 if all the elements should be kept in AVL tree. */
4921 static int constructor_incremental;
4923 /* 1 if so far this constructor's elements are all compile-time constants. */
4924 static int constructor_constant;
4926 /* 1 if so far this constructor's elements are all valid address constants. */
4927 static int constructor_simple;
4929 /* 1 if this constructor is erroneous so far. */
4930 static int constructor_erroneous;
4932 /* 1 if have called defer_addressed_constants. */
4933 static int constructor_subconstants_deferred;
4935 /* Structure for managing pending initializer elements, organized as an
4936 AVL tree. */
4938 struct init_node
4940 struct init_node *left, *right;
4941 struct init_node *parent;
4942 int balance;
4943 tree purpose;
4944 tree value;
4947 /* Tree of pending elements at this constructor level.
4948 These are elements encountered out of order
4949 which belong at places we haven't reached yet in actually
4950 writing the output.
4951 Will never hold tree nodes across GC runs. */
4952 static struct init_node *constructor_pending_elts;
4954 /* The SPELLING_DEPTH of this constructor. */
4955 static int constructor_depth;
4957 /* 0 if implicitly pushing constructor levels is allowed. */
4958 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4960 static int require_constant_value;
4961 static int require_constant_elements;
4963 /* DECL node for which an initializer is being read.
4964 0 means we are reading a constructor expression
4965 such as (struct foo) {...}. */
4966 static tree constructor_decl;
4968 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4969 static const char *constructor_asmspec;
4971 /* Nonzero if this is an initializer for a top-level decl. */
4972 static int constructor_top_level;
4974 /* Nesting depth of designator list. */
4975 static int designator_depth;
4977 /* Nonzero if there were diagnosed errors in this designator list. */
4978 static int designator_errorneous;
4981 /* This stack has a level for each implicit or explicit level of
4982 structuring in the initializer, including the outermost one. It
4983 saves the values of most of the variables above. */
4985 struct constructor_range_stack;
4987 struct constructor_stack
4989 struct constructor_stack *next;
4990 tree type;
4991 tree fields;
4992 tree index;
4993 tree max_index;
4994 tree unfilled_index;
4995 tree unfilled_fields;
4996 tree bit_index;
4997 tree elements;
4998 struct init_node *pending_elts;
4999 int offset;
5000 int depth;
5001 /* If nonzero, this value should replace the entire
5002 constructor at this level. */
5003 tree replacement_value;
5004 struct constructor_range_stack *range_stack;
5005 char constant;
5006 char simple;
5007 char implicit;
5008 char erroneous;
5009 char outer;
5010 char incremental;
5013 struct constructor_stack *constructor_stack;
5015 /* This stack represents designators from some range designator up to
5016 the last designator in the list. */
5018 struct constructor_range_stack
5020 struct constructor_range_stack *next, *prev;
5021 struct constructor_stack *stack;
5022 tree range_start;
5023 tree index;
5024 tree range_end;
5025 tree fields;
5028 struct constructor_range_stack *constructor_range_stack;
5030 /* This stack records separate initializers that are nested.
5031 Nested initializers can't happen in ANSI C, but GNU C allows them
5032 in cases like { ... (struct foo) { ... } ... }. */
5034 struct initializer_stack
5036 struct initializer_stack *next;
5037 tree decl;
5038 const char *asmspec;
5039 struct constructor_stack *constructor_stack;
5040 struct constructor_range_stack *constructor_range_stack;
5041 tree elements;
5042 struct spelling *spelling;
5043 struct spelling *spelling_base;
5044 int spelling_size;
5045 char top_level;
5046 char require_constant_value;
5047 char require_constant_elements;
5048 char deferred;
5051 struct initializer_stack *initializer_stack;
5053 /* Prepare to parse and output the initializer for variable DECL. */
5055 void
5056 start_init (decl, asmspec_tree, top_level)
5057 tree decl;
5058 tree asmspec_tree;
5059 int top_level;
5061 const char *locus;
5062 struct initializer_stack *p
5063 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5064 const char *asmspec = 0;
5066 if (asmspec_tree)
5067 asmspec = TREE_STRING_POINTER (asmspec_tree);
5069 p->decl = constructor_decl;
5070 p->asmspec = constructor_asmspec;
5071 p->require_constant_value = require_constant_value;
5072 p->require_constant_elements = require_constant_elements;
5073 p->constructor_stack = constructor_stack;
5074 p->constructor_range_stack = constructor_range_stack;
5075 p->elements = constructor_elements;
5076 p->spelling = spelling;
5077 p->spelling_base = spelling_base;
5078 p->spelling_size = spelling_size;
5079 p->deferred = constructor_subconstants_deferred;
5080 p->top_level = constructor_top_level;
5081 p->next = initializer_stack;
5082 initializer_stack = p;
5084 constructor_decl = decl;
5085 constructor_asmspec = asmspec;
5086 constructor_subconstants_deferred = 0;
5087 constructor_top_level = top_level;
5089 if (decl != 0)
5091 require_constant_value = TREE_STATIC (decl);
5092 require_constant_elements
5093 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5094 /* For a scalar, you can always use any value to initialize,
5095 even within braces. */
5096 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5097 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5098 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5099 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5100 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5102 else
5104 require_constant_value = 0;
5105 require_constant_elements = 0;
5106 locus = "(anonymous)";
5109 constructor_stack = 0;
5110 constructor_range_stack = 0;
5112 missing_braces_mentioned = 0;
5114 spelling_base = 0;
5115 spelling_size = 0;
5116 RESTORE_SPELLING_DEPTH (0);
5118 if (locus)
5119 push_string (locus);
5122 void
5123 finish_init ()
5125 struct initializer_stack *p = initializer_stack;
5127 /* Output subconstants (string constants, usually)
5128 that were referenced within this initializer and saved up.
5129 Must do this if and only if we called defer_addressed_constants. */
5130 if (constructor_subconstants_deferred)
5131 output_deferred_addressed_constants ();
5133 /* Free the whole constructor stack of this initializer. */
5134 while (constructor_stack)
5136 struct constructor_stack *q = constructor_stack;
5137 constructor_stack = q->next;
5138 free (q);
5141 if (constructor_range_stack)
5142 abort ();
5144 /* Pop back to the data of the outer initializer (if any). */
5145 constructor_decl = p->decl;
5146 constructor_asmspec = p->asmspec;
5147 require_constant_value = p->require_constant_value;
5148 require_constant_elements = p->require_constant_elements;
5149 constructor_stack = p->constructor_stack;
5150 constructor_range_stack = p->constructor_range_stack;
5151 constructor_elements = p->elements;
5152 spelling = p->spelling;
5153 spelling_base = p->spelling_base;
5154 spelling_size = p->spelling_size;
5155 constructor_subconstants_deferred = p->deferred;
5156 constructor_top_level = p->top_level;
5157 initializer_stack = p->next;
5158 free (p);
5161 /* Call here when we see the initializer is surrounded by braces.
5162 This is instead of a call to push_init_level;
5163 it is matched by a call to pop_init_level.
5165 TYPE is the type to initialize, for a constructor expression.
5166 For an initializer for a decl, TYPE is zero. */
5168 void
5169 really_start_incremental_init (type)
5170 tree type;
5172 struct constructor_stack *p
5173 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5175 if (type == 0)
5176 type = TREE_TYPE (constructor_decl);
5178 p->type = constructor_type;
5179 p->fields = constructor_fields;
5180 p->index = constructor_index;
5181 p->max_index = constructor_max_index;
5182 p->unfilled_index = constructor_unfilled_index;
5183 p->unfilled_fields = constructor_unfilled_fields;
5184 p->bit_index = constructor_bit_index;
5185 p->elements = constructor_elements;
5186 p->constant = constructor_constant;
5187 p->simple = constructor_simple;
5188 p->erroneous = constructor_erroneous;
5189 p->pending_elts = constructor_pending_elts;
5190 p->depth = constructor_depth;
5191 p->replacement_value = 0;
5192 p->implicit = 0;
5193 p->range_stack = 0;
5194 p->outer = 0;
5195 p->incremental = constructor_incremental;
5196 p->next = 0;
5197 constructor_stack = p;
5199 constructor_constant = 1;
5200 constructor_simple = 1;
5201 constructor_depth = SPELLING_DEPTH ();
5202 constructor_elements = 0;
5203 constructor_pending_elts = 0;
5204 constructor_type = type;
5205 constructor_incremental = 1;
5206 designator_depth = 0;
5207 designator_errorneous = 0;
5209 if (TREE_CODE (constructor_type) == RECORD_TYPE
5210 || TREE_CODE (constructor_type) == UNION_TYPE)
5212 constructor_fields = TYPE_FIELDS (constructor_type);
5213 /* Skip any nameless bit fields at the beginning. */
5214 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5215 && DECL_NAME (constructor_fields) == 0)
5216 constructor_fields = TREE_CHAIN (constructor_fields);
5218 constructor_unfilled_fields = constructor_fields;
5219 constructor_bit_index = bitsize_zero_node;
5221 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5223 if (TYPE_DOMAIN (constructor_type))
5225 constructor_max_index
5226 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5228 /* Detect non-empty initializations of zero-length arrays. */
5229 if (constructor_max_index == NULL_TREE)
5230 constructor_max_index = build_int_2 (-1, -1);
5232 constructor_index
5233 = convert (bitsizetype,
5234 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5236 else
5237 constructor_index = bitsize_zero_node;
5239 constructor_unfilled_index = constructor_index;
5241 else
5243 /* Handle the case of int x = {5}; */
5244 constructor_fields = constructor_type;
5245 constructor_unfilled_fields = constructor_type;
5249 /* Push down into a subobject, for initialization.
5250 If this is for an explicit set of braces, IMPLICIT is 0.
5251 If it is because the next element belongs at a lower level,
5252 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5254 void
5255 push_init_level (implicit)
5256 int implicit;
5258 struct constructor_stack *p;
5259 tree value = NULL_TREE;
5261 /* If we've exhausted any levels that didn't have braces,
5262 pop them now. */
5263 while (constructor_stack->implicit)
5265 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5266 || TREE_CODE (constructor_type) == UNION_TYPE)
5267 && constructor_fields == 0)
5268 process_init_element (pop_init_level (1));
5269 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5270 && tree_int_cst_lt (constructor_max_index, constructor_index))
5271 process_init_element (pop_init_level (1));
5272 else
5273 break;
5276 /* Unless this is an explicit brace, we need to preserve previous
5277 content if any. */
5278 if (implicit)
5280 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5281 || TREE_CODE (constructor_type) == UNION_TYPE)
5282 && constructor_fields)
5283 value = find_init_member (constructor_fields);
5284 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5285 value = find_init_member (constructor_index);
5288 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5289 p->type = constructor_type;
5290 p->fields = constructor_fields;
5291 p->index = constructor_index;
5292 p->max_index = constructor_max_index;
5293 p->unfilled_index = constructor_unfilled_index;
5294 p->unfilled_fields = constructor_unfilled_fields;
5295 p->bit_index = constructor_bit_index;
5296 p->elements = constructor_elements;
5297 p->constant = constructor_constant;
5298 p->simple = constructor_simple;
5299 p->erroneous = constructor_erroneous;
5300 p->pending_elts = constructor_pending_elts;
5301 p->depth = constructor_depth;
5302 p->replacement_value = 0;
5303 p->implicit = implicit;
5304 p->outer = 0;
5305 p->incremental = constructor_incremental;
5306 p->next = constructor_stack;
5307 p->range_stack = 0;
5308 constructor_stack = p;
5310 constructor_constant = 1;
5311 constructor_simple = 1;
5312 constructor_depth = SPELLING_DEPTH ();
5313 constructor_elements = 0;
5314 constructor_incremental = 1;
5315 constructor_pending_elts = 0;
5316 if (!implicit)
5318 p->range_stack = constructor_range_stack;
5319 constructor_range_stack = 0;
5320 designator_depth = 0;
5321 designator_errorneous = 0;
5324 /* Don't die if an entire brace-pair level is superfluous
5325 in the containing level. */
5326 if (constructor_type == 0)
5328 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5329 || TREE_CODE (constructor_type) == UNION_TYPE)
5331 /* Don't die if there are extra init elts at the end. */
5332 if (constructor_fields == 0)
5333 constructor_type = 0;
5334 else
5336 constructor_type = TREE_TYPE (constructor_fields);
5337 push_member_name (constructor_fields);
5338 constructor_depth++;
5341 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5343 constructor_type = TREE_TYPE (constructor_type);
5344 push_array_bounds (tree_low_cst (constructor_index, 0));
5345 constructor_depth++;
5348 if (constructor_type == 0)
5350 error_init ("extra brace group at end of initializer");
5351 constructor_fields = 0;
5352 constructor_unfilled_fields = 0;
5353 return;
5356 if (value && TREE_CODE (value) == CONSTRUCTOR)
5358 constructor_constant = TREE_CONSTANT (value);
5359 constructor_simple = TREE_STATIC (value);
5360 constructor_elements = TREE_OPERAND (value, 1);
5361 if (constructor_elements
5362 && (TREE_CODE (constructor_type) == RECORD_TYPE
5363 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5364 set_nonincremental_init ();
5367 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5369 missing_braces_mentioned = 1;
5370 warning_init ("missing braces around initializer");
5373 if (TREE_CODE (constructor_type) == RECORD_TYPE
5374 || TREE_CODE (constructor_type) == UNION_TYPE)
5376 constructor_fields = TYPE_FIELDS (constructor_type);
5377 /* Skip any nameless bit fields at the beginning. */
5378 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5379 && DECL_NAME (constructor_fields) == 0)
5380 constructor_fields = TREE_CHAIN (constructor_fields);
5382 constructor_unfilled_fields = constructor_fields;
5383 constructor_bit_index = bitsize_zero_node;
5385 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5387 if (TYPE_DOMAIN (constructor_type))
5389 constructor_max_index
5390 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5391 constructor_index
5392 = convert (bitsizetype,
5393 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5395 /* ??? For GCC 3.1, remove special case initialization of
5396 zero-length array members from pop_init_level and set
5397 constructor_max_index such that we get the normal
5398 "excess elements" warning. */
5400 else
5401 constructor_index = bitsize_zero_node;
5403 constructor_unfilled_index = constructor_index;
5404 if (value && TREE_CODE (value) == STRING_CST)
5406 /* We need to split the char/wchar array into individual
5407 characters, so that we don't have to special case it
5408 everywhere. */
5409 set_nonincremental_init_from_string (value);
5412 else
5414 warning_init ("braces around scalar initializer");
5415 constructor_fields = constructor_type;
5416 constructor_unfilled_fields = constructor_type;
5420 /* At the end of an implicit or explicit brace level,
5421 finish up that level of constructor.
5422 If we were outputting the elements as they are read, return 0
5423 from inner levels (process_init_element ignores that),
5424 but return error_mark_node from the outermost level
5425 (that's what we want to put in DECL_INITIAL).
5426 Otherwise, return a CONSTRUCTOR expression. */
5428 tree
5429 pop_init_level (implicit)
5430 int implicit;
5432 struct constructor_stack *p;
5433 HOST_WIDE_INT size = 0;
5434 tree constructor = 0;
5436 if (implicit == 0)
5438 /* When we come to an explicit close brace,
5439 pop any inner levels that didn't have explicit braces. */
5440 while (constructor_stack->implicit)
5441 process_init_element (pop_init_level (1));
5443 if (constructor_range_stack)
5444 abort ();
5447 p = constructor_stack;
5449 if (constructor_type != 0)
5450 size = int_size_in_bytes (constructor_type);
5452 /* Error for initializing a flexible array member, or a zero-length
5453 array member in an inappropriate context. */
5454 if (constructor_type && constructor_fields
5455 && TREE_CODE (constructor_type) == ARRAY_TYPE
5456 && TYPE_DOMAIN (constructor_type)
5457 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5459 /* Silently discard empty initializations. The parser will
5460 already have pedwarned for empty brackets. */
5461 if (integer_zerop (constructor_unfilled_index))
5462 constructor_type = NULL_TREE;
5463 else if (! TYPE_SIZE (constructor_type))
5465 if (constructor_depth > 2)
5466 error_init ("initialization of flexible array member in a nested context");
5467 else if (pedantic)
5468 pedwarn_init ("initialization of a flexible array member");
5470 /* We have already issued an error message for the existance
5471 of a flexible array member not at the end of the structure.
5472 Discard the initializer so that we do not abort later. */
5473 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5474 constructor_type = NULL_TREE;
5476 else
5478 warning_init ("deprecated initialization of zero-length array");
5480 /* We must be initializing the last member of a top-level struct. */
5481 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5483 error_init ("initialization of zero-length array before end of structure");
5484 /* Discard the initializer so that we do not abort later. */
5485 constructor_type = NULL_TREE;
5487 else if (constructor_depth > 2)
5488 error_init ("initialization of zero-length array inside a nested context");
5492 /* Warn when some struct elements are implicitly initialized to zero. */
5493 if (extra_warnings
5494 && constructor_type
5495 && TREE_CODE (constructor_type) == RECORD_TYPE
5496 && constructor_unfilled_fields)
5498 /* Do not warn for flexible array members or zero-length arrays. */
5499 while (constructor_unfilled_fields
5500 && (! DECL_SIZE (constructor_unfilled_fields)
5501 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5502 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5504 if (constructor_unfilled_fields)
5506 push_member_name (constructor_unfilled_fields);
5507 warning_init ("missing initializer");
5508 RESTORE_SPELLING_DEPTH (constructor_depth);
5512 /* Now output all pending elements. */
5513 constructor_incremental = 1;
5514 output_pending_init_elements (1);
5516 /* Pad out the end of the structure. */
5517 if (p->replacement_value)
5518 /* If this closes a superfluous brace pair,
5519 just pass out the element between them. */
5520 constructor = p->replacement_value;
5521 else if (constructor_type == 0)
5523 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5524 && TREE_CODE (constructor_type) != UNION_TYPE
5525 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5527 /* A nonincremental scalar initializer--just return
5528 the element, after verifying there is just one. */
5529 if (constructor_elements == 0)
5531 if (!constructor_erroneous)
5532 error_init ("empty scalar initializer");
5533 constructor = error_mark_node;
5535 else if (TREE_CHAIN (constructor_elements) != 0)
5537 error_init ("extra elements in scalar initializer");
5538 constructor = TREE_VALUE (constructor_elements);
5540 else
5541 constructor = TREE_VALUE (constructor_elements);
5543 else
5545 if (constructor_erroneous)
5546 constructor = error_mark_node;
5547 else
5549 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5550 nreverse (constructor_elements));
5551 if (constructor_constant)
5552 TREE_CONSTANT (constructor) = 1;
5553 if (constructor_constant && constructor_simple)
5554 TREE_STATIC (constructor) = 1;
5558 constructor_type = p->type;
5559 constructor_fields = p->fields;
5560 constructor_index = p->index;
5561 constructor_max_index = p->max_index;
5562 constructor_unfilled_index = p->unfilled_index;
5563 constructor_unfilled_fields = p->unfilled_fields;
5564 constructor_bit_index = p->bit_index;
5565 constructor_elements = p->elements;
5566 constructor_constant = p->constant;
5567 constructor_simple = p->simple;
5568 constructor_erroneous = p->erroneous;
5569 constructor_incremental = p->incremental;
5570 constructor_pending_elts = p->pending_elts;
5571 constructor_depth = p->depth;
5572 if (!p->implicit)
5573 constructor_range_stack = p->range_stack;
5574 RESTORE_SPELLING_DEPTH (constructor_depth);
5576 constructor_stack = p->next;
5577 free (p);
5579 if (constructor == 0)
5581 if (constructor_stack == 0)
5582 return error_mark_node;
5583 return NULL_TREE;
5585 return constructor;
5588 /* Common handling for both array range and field name designators.
5589 ARRAY argument is non-zero for array ranges. Returns zero for success. */
5591 static int
5592 set_designator (array)
5593 int array;
5595 tree subtype;
5596 enum tree_code subcode;
5598 /* Don't die if an entire brace-pair level is superfluous
5599 in the containing level. */
5600 if (constructor_type == 0)
5601 return 1;
5603 /* If there were errors in this designator list already, bail out silently. */
5604 if (designator_errorneous)
5605 return 1;
5607 if (!designator_depth)
5609 if (constructor_range_stack)
5610 abort ();
5612 /* Designator list starts at the level of closest explicit
5613 braces. */
5614 while (constructor_stack->implicit)
5615 process_init_element (pop_init_level (1));
5616 return 0;
5619 if (constructor_no_implicit)
5621 error_init ("initialization designators may not nest");
5622 return 1;
5625 if (TREE_CODE (constructor_type) == RECORD_TYPE
5626 || TREE_CODE (constructor_type) == UNION_TYPE)
5628 subtype = TREE_TYPE (constructor_fields);
5629 if (subtype != error_mark_node)
5630 subtype = TYPE_MAIN_VARIANT (subtype);
5632 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5634 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5636 else
5637 abort ();
5639 subcode = TREE_CODE (subtype);
5640 if (array && subcode != ARRAY_TYPE)
5642 error_init ("array index in non-array initializer");
5643 return 1;
5645 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5647 error_init ("field name not in record or union initializer");
5648 return 1;
5651 push_init_level (2);
5652 return 0;
5655 /* If there are range designators in designator list, push a new designator
5656 to constructor_range_stack. RANGE_END is end of such stack range or
5657 NULL_TREE if there is no range designator at this level. */
5659 static void
5660 push_range_stack (range_end)
5661 tree range_end;
5663 struct constructor_range_stack *p;
5665 p = (struct constructor_range_stack *)
5666 ggc_alloc (sizeof (struct constructor_range_stack));
5667 p->prev = constructor_range_stack;
5668 p->next = 0;
5669 p->fields = constructor_fields;
5670 p->range_start = constructor_index;
5671 p->index = constructor_index;
5672 p->stack = constructor_stack;
5673 p->range_end = range_end;
5674 if (constructor_range_stack)
5675 constructor_range_stack->next = p;
5676 constructor_range_stack = p;
5679 /* Within an array initializer, specify the next index to be initialized.
5680 FIRST is that index. If LAST is nonzero, then initialize a range
5681 of indices, running from FIRST through LAST. */
5683 void
5684 set_init_index (first, last)
5685 tree first, last;
5687 if (set_designator (1))
5688 return;
5690 designator_errorneous = 1;
5692 while ((TREE_CODE (first) == NOP_EXPR
5693 || TREE_CODE (first) == CONVERT_EXPR
5694 || TREE_CODE (first) == NON_LVALUE_EXPR)
5695 && (TYPE_MODE (TREE_TYPE (first))
5696 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5697 first = TREE_OPERAND (first, 0);
5699 if (last)
5700 while ((TREE_CODE (last) == NOP_EXPR
5701 || TREE_CODE (last) == CONVERT_EXPR
5702 || TREE_CODE (last) == NON_LVALUE_EXPR)
5703 && (TYPE_MODE (TREE_TYPE (last))
5704 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5705 last = TREE_OPERAND (last, 0);
5707 if (TREE_CODE (first) != INTEGER_CST)
5708 error_init ("nonconstant array index in initializer");
5709 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5710 error_init ("nonconstant array index in initializer");
5711 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5712 error_init ("array index in non-array initializer");
5713 else if (constructor_max_index
5714 && tree_int_cst_lt (constructor_max_index, first))
5715 error_init ("array index in initializer exceeds array bounds");
5716 else
5718 constructor_index = convert (bitsizetype, first);
5720 if (last)
5722 if (tree_int_cst_equal (first, last))
5723 last = 0;
5724 else if (tree_int_cst_lt (last, first))
5726 error_init ("empty index range in initializer");
5727 last = 0;
5729 else
5731 last = convert (bitsizetype, last);
5732 if (constructor_max_index != 0
5733 && tree_int_cst_lt (constructor_max_index, last))
5735 error_init ("array index range in initializer exceeds array bounds");
5736 last = 0;
5741 designator_depth++;
5742 designator_errorneous = 0;
5743 if (constructor_range_stack || last)
5744 push_range_stack (last);
5748 /* Within a struct initializer, specify the next field to be initialized. */
5750 void
5751 set_init_label (fieldname)
5752 tree fieldname;
5754 tree tail;
5756 if (set_designator (0))
5757 return;
5759 designator_errorneous = 1;
5761 if (TREE_CODE (constructor_type) != RECORD_TYPE
5762 && TREE_CODE (constructor_type) != UNION_TYPE)
5764 error_init ("field name not in record or union initializer");
5765 return;
5768 for (tail = TYPE_FIELDS (constructor_type); tail;
5769 tail = TREE_CHAIN (tail))
5771 if (DECL_NAME (tail) == fieldname)
5772 break;
5775 if (tail == 0)
5776 error ("unknown field `%s' specified in initializer",
5777 IDENTIFIER_POINTER (fieldname));
5778 else
5780 constructor_fields = tail;
5781 designator_depth++;
5782 designator_errorneous = 0;
5783 if (constructor_range_stack)
5784 push_range_stack (NULL_TREE);
5788 /* Add a new initializer to the tree of pending initializers. PURPOSE
5789 indentifies the initializer, either array index or field in a structure.
5790 VALUE is the value of that index or field. */
5792 static void
5793 add_pending_init (purpose, value)
5794 tree purpose, value;
5796 struct init_node *p, **q, *r;
5798 q = &constructor_pending_elts;
5799 p = 0;
5801 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5803 while (*q != 0)
5805 p = *q;
5806 if (tree_int_cst_lt (purpose, p->purpose))
5807 q = &p->left;
5808 else if (tree_int_cst_lt (p->purpose, purpose))
5809 q = &p->right;
5810 else
5812 if (TREE_SIDE_EFFECTS (p->value))
5813 warning_init ("initialized field with side-effects overwritten");
5814 p->value = value;
5815 return;
5819 else
5821 tree bitpos;
5823 bitpos = bit_position (purpose);
5824 while (*q != NULL)
5826 p = *q;
5827 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5828 q = &p->left;
5829 else if (p->purpose != purpose)
5830 q = &p->right;
5831 else
5833 if (TREE_SIDE_EFFECTS (p->value))
5834 warning_init ("initialized field with side-effects overwritten");
5835 p->value = value;
5836 return;
5841 r = (struct init_node *) ggc_alloc (sizeof (struct init_node));
5842 r->purpose = purpose;
5843 r->value = value;
5845 *q = r;
5846 r->parent = p;
5847 r->left = 0;
5848 r->right = 0;
5849 r->balance = 0;
5851 while (p)
5853 struct init_node *s;
5855 if (r == p->left)
5857 if (p->balance == 0)
5858 p->balance = -1;
5859 else if (p->balance < 0)
5861 if (r->balance < 0)
5863 /* L rotation. */
5864 p->left = r->right;
5865 if (p->left)
5866 p->left->parent = p;
5867 r->right = p;
5869 p->balance = 0;
5870 r->balance = 0;
5872 s = p->parent;
5873 p->parent = r;
5874 r->parent = s;
5875 if (s)
5877 if (s->left == p)
5878 s->left = r;
5879 else
5880 s->right = r;
5882 else
5883 constructor_pending_elts = r;
5885 else
5887 /* LR rotation. */
5888 struct init_node *t = r->right;
5890 r->right = t->left;
5891 if (r->right)
5892 r->right->parent = r;
5893 t->left = r;
5895 p->left = t->right;
5896 if (p->left)
5897 p->left->parent = p;
5898 t->right = p;
5900 p->balance = t->balance < 0;
5901 r->balance = -(t->balance > 0);
5902 t->balance = 0;
5904 s = p->parent;
5905 p->parent = t;
5906 r->parent = t;
5907 t->parent = s;
5908 if (s)
5910 if (s->left == p)
5911 s->left = t;
5912 else
5913 s->right = t;
5915 else
5916 constructor_pending_elts = t;
5918 break;
5920 else
5922 /* p->balance == +1; growth of left side balances the node. */
5923 p->balance = 0;
5924 break;
5927 else /* r == p->right */
5929 if (p->balance == 0)
5930 /* Growth propagation from right side. */
5931 p->balance++;
5932 else if (p->balance > 0)
5934 if (r->balance > 0)
5936 /* R rotation. */
5937 p->right = r->left;
5938 if (p->right)
5939 p->right->parent = p;
5940 r->left = p;
5942 p->balance = 0;
5943 r->balance = 0;
5945 s = p->parent;
5946 p->parent = r;
5947 r->parent = s;
5948 if (s)
5950 if (s->left == p)
5951 s->left = r;
5952 else
5953 s->right = r;
5955 else
5956 constructor_pending_elts = r;
5958 else /* r->balance == -1 */
5960 /* RL rotation */
5961 struct init_node *t = r->left;
5963 r->left = t->right;
5964 if (r->left)
5965 r->left->parent = r;
5966 t->right = r;
5968 p->right = t->left;
5969 if (p->right)
5970 p->right->parent = p;
5971 t->left = p;
5973 r->balance = (t->balance < 0);
5974 p->balance = -(t->balance > 0);
5975 t->balance = 0;
5977 s = p->parent;
5978 p->parent = t;
5979 r->parent = t;
5980 t->parent = s;
5981 if (s)
5983 if (s->left == p)
5984 s->left = t;
5985 else
5986 s->right = t;
5988 else
5989 constructor_pending_elts = t;
5991 break;
5993 else
5995 /* p->balance == -1; growth of right side balances the node. */
5996 p->balance = 0;
5997 break;
6001 r = p;
6002 p = p->parent;
6006 /* Build AVL tree from a sorted chain. */
6008 static void
6009 set_nonincremental_init ()
6011 tree chain;
6013 if (TREE_CODE (constructor_type) != RECORD_TYPE
6014 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6015 return;
6017 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
6018 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
6019 constructor_elements = 0;
6020 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6022 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6023 /* Skip any nameless bit fields at the beginning. */
6024 while (constructor_unfilled_fields != 0
6025 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6026 && DECL_NAME (constructor_unfilled_fields) == 0)
6027 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6030 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6032 if (TYPE_DOMAIN (constructor_type))
6033 constructor_unfilled_index
6034 = convert (bitsizetype,
6035 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6036 else
6037 constructor_unfilled_index = bitsize_zero_node;
6039 constructor_incremental = 0;
6042 /* Build AVL tree from a string constant. */
6044 static void
6045 set_nonincremental_init_from_string (str)
6046 tree str;
6048 tree value, purpose, type;
6049 HOST_WIDE_INT val[2];
6050 const char *p, *end;
6051 int byte, wchar_bytes, charwidth, bitpos;
6053 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6054 abort ();
6056 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6057 == TYPE_PRECISION (char_type_node))
6058 wchar_bytes = 1;
6059 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6060 == TYPE_PRECISION (wchar_type_node))
6061 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6062 else
6063 abort ();
6065 charwidth = TYPE_PRECISION (char_type_node);
6066 type = TREE_TYPE (constructor_type);
6067 p = TREE_STRING_POINTER (str);
6068 end = p + TREE_STRING_LENGTH (str);
6070 for (purpose = bitsize_zero_node;
6071 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6072 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6074 if (wchar_bytes == 1)
6076 val[1] = (unsigned char) *p++;
6077 val[0] = 0;
6079 else
6081 val[0] = 0;
6082 val[1] = 0;
6083 for (byte = 0; byte < wchar_bytes; byte++)
6085 if (BYTES_BIG_ENDIAN)
6086 bitpos = (wchar_bytes - byte - 1) * charwidth;
6087 else
6088 bitpos = byte * charwidth;
6089 val[bitpos < HOST_BITS_PER_WIDE_INT]
6090 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6091 << (bitpos % HOST_BITS_PER_WIDE_INT);
6095 if (!TREE_UNSIGNED (type))
6097 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6098 if (bitpos < HOST_BITS_PER_WIDE_INT)
6100 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6102 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6103 val[0] = -1;
6106 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6108 if (val[1] < 0)
6109 val[0] = -1;
6111 else if (val[0] & (((HOST_WIDE_INT) 1)
6112 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6113 val[0] |= ((HOST_WIDE_INT) -1)
6114 << (bitpos - HOST_BITS_PER_WIDE_INT);
6117 value = build_int_2 (val[1], val[0]);
6118 TREE_TYPE (value) = type;
6119 add_pending_init (purpose, value);
6122 constructor_incremental = 0;
6125 /* Return value of FIELD in pending initializer or zero if the field was
6126 not initialized yet. */
6128 static tree
6129 find_init_member (field)
6130 tree field;
6132 struct init_node *p;
6134 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6136 if (constructor_incremental
6137 && tree_int_cst_lt (field, constructor_unfilled_index))
6138 set_nonincremental_init ();
6140 p = constructor_pending_elts;
6141 while (p)
6143 if (tree_int_cst_lt (field, p->purpose))
6144 p = p->left;
6145 else if (tree_int_cst_lt (p->purpose, field))
6146 p = p->right;
6147 else
6148 return p->value;
6151 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6153 tree bitpos = bit_position (field);
6155 if (constructor_incremental
6156 && (!constructor_unfilled_fields
6157 || tree_int_cst_lt (bitpos,
6158 bit_position (constructor_unfilled_fields))))
6159 set_nonincremental_init ();
6161 p = constructor_pending_elts;
6162 while (p)
6164 if (field == p->purpose)
6165 return p->value;
6166 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6167 p = p->left;
6168 else
6169 p = p->right;
6172 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6174 if (constructor_elements
6175 && TREE_PURPOSE (constructor_elements) == field)
6176 return TREE_VALUE (constructor_elements);
6178 return 0;
6181 /* "Output" the next constructor element.
6182 At top level, really output it to assembler code now.
6183 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6184 TYPE is the data type that the containing data type wants here.
6185 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6187 PENDING if non-nil means output pending elements that belong
6188 right after this element. (PENDING is normally 1;
6189 it is 0 while outputting pending elements, to avoid recursion.) */
6191 static void
6192 output_init_element (value, type, field, pending)
6193 tree value, type, field;
6194 int pending;
6196 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6197 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6198 && !(TREE_CODE (value) == STRING_CST
6199 && TREE_CODE (type) == ARRAY_TYPE
6200 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6201 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6202 TYPE_MAIN_VARIANT (type))))
6203 value = default_conversion (value);
6205 if (value == error_mark_node)
6206 constructor_erroneous = 1;
6207 else if (!TREE_CONSTANT (value))
6208 constructor_constant = 0;
6209 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6210 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6211 || TREE_CODE (constructor_type) == UNION_TYPE)
6212 && DECL_C_BIT_FIELD (field)
6213 && TREE_CODE (value) != INTEGER_CST))
6214 constructor_simple = 0;
6216 if (require_constant_value && ! TREE_CONSTANT (value))
6218 error_init ("initializer element is not constant");
6219 value = error_mark_node;
6221 else if (require_constant_elements
6222 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6223 pedwarn ("initializer element is not computable at load time");
6225 /* If this field is empty (and not at the end of structure),
6226 don't do anything other than checking the initializer. */
6227 if (field
6228 && (TREE_TYPE (field) == error_mark_node
6229 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6230 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6231 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6232 || TREE_CHAIN (field)))))
6233 return;
6235 if (value == error_mark_node)
6237 constructor_erroneous = 1;
6238 return;
6241 /* If this element doesn't come next in sequence,
6242 put it on constructor_pending_elts. */
6243 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6244 && (!constructor_incremental
6245 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6247 if (constructor_incremental
6248 && tree_int_cst_lt (field, constructor_unfilled_index))
6249 set_nonincremental_init ();
6251 add_pending_init (field,
6252 digest_init (type, value, require_constant_value,
6253 require_constant_elements));
6254 return;
6256 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6257 && (!constructor_incremental
6258 || field != constructor_unfilled_fields))
6260 /* We do this for records but not for unions. In a union,
6261 no matter which field is specified, it can be initialized
6262 right away since it starts at the beginning of the union. */
6263 if (constructor_incremental)
6265 if (!constructor_unfilled_fields)
6266 set_nonincremental_init ();
6267 else
6269 tree bitpos, unfillpos;
6271 bitpos = bit_position (field);
6272 unfillpos = bit_position (constructor_unfilled_fields);
6274 if (tree_int_cst_lt (bitpos, unfillpos))
6275 set_nonincremental_init ();
6279 add_pending_init (field,
6280 digest_init (type, value, require_constant_value,
6281 require_constant_elements));
6282 return;
6284 else if (TREE_CODE (constructor_type) == UNION_TYPE
6285 && constructor_elements)
6287 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
6288 warning_init ("initialized field with side-effects overwritten");
6290 /* We can have just one union field set. */
6291 constructor_elements = 0;
6294 /* Otherwise, output this element either to
6295 constructor_elements or to the assembler file. */
6297 if (field && TREE_CODE (field) == INTEGER_CST)
6298 field = copy_node (field);
6299 constructor_elements
6300 = tree_cons (field, digest_init (type, value,
6301 require_constant_value,
6302 require_constant_elements),
6303 constructor_elements);
6305 /* Advance the variable that indicates sequential elements output. */
6306 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6307 constructor_unfilled_index
6308 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6309 bitsize_one_node);
6310 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6312 constructor_unfilled_fields
6313 = TREE_CHAIN (constructor_unfilled_fields);
6315 /* Skip any nameless bit fields. */
6316 while (constructor_unfilled_fields != 0
6317 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6318 && DECL_NAME (constructor_unfilled_fields) == 0)
6319 constructor_unfilled_fields =
6320 TREE_CHAIN (constructor_unfilled_fields);
6322 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6323 constructor_unfilled_fields = 0;
6325 /* Now output any pending elements which have become next. */
6326 if (pending)
6327 output_pending_init_elements (0);
6330 /* Output any pending elements which have become next.
6331 As we output elements, constructor_unfilled_{fields,index}
6332 advances, which may cause other elements to become next;
6333 if so, they too are output.
6335 If ALL is 0, we return when there are
6336 no more pending elements to output now.
6338 If ALL is 1, we output space as necessary so that
6339 we can output all the pending elements. */
6341 static void
6342 output_pending_init_elements (all)
6343 int all;
6345 struct init_node *elt = constructor_pending_elts;
6346 tree next;
6348 retry:
6350 /* Look thru the whole pending tree.
6351 If we find an element that should be output now,
6352 output it. Otherwise, set NEXT to the element
6353 that comes first among those still pending. */
6355 next = 0;
6356 while (elt)
6358 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6360 if (tree_int_cst_equal (elt->purpose,
6361 constructor_unfilled_index))
6362 output_init_element (elt->value,
6363 TREE_TYPE (constructor_type),
6364 constructor_unfilled_index, 0);
6365 else if (tree_int_cst_lt (constructor_unfilled_index,
6366 elt->purpose))
6368 /* Advance to the next smaller node. */
6369 if (elt->left)
6370 elt = elt->left;
6371 else
6373 /* We have reached the smallest node bigger than the
6374 current unfilled index. Fill the space first. */
6375 next = elt->purpose;
6376 break;
6379 else
6381 /* Advance to the next bigger node. */
6382 if (elt->right)
6383 elt = elt->right;
6384 else
6386 /* We have reached the biggest node in a subtree. Find
6387 the parent of it, which is the next bigger node. */
6388 while (elt->parent && elt->parent->right == elt)
6389 elt = elt->parent;
6390 elt = elt->parent;
6391 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6392 elt->purpose))
6394 next = elt->purpose;
6395 break;
6400 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6401 || TREE_CODE (constructor_type) == UNION_TYPE)
6403 tree ctor_unfilled_bitpos, elt_bitpos;
6405 /* If the current record is complete we are done. */
6406 if (constructor_unfilled_fields == 0)
6407 break;
6409 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6410 elt_bitpos = bit_position (elt->purpose);
6411 /* We can't compare fields here because there might be empty
6412 fields in between. */
6413 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6415 constructor_unfilled_fields = elt->purpose;
6416 output_init_element (elt->value, TREE_TYPE (elt->purpose),
6417 elt->purpose, 0);
6419 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6421 /* Advance to the next smaller node. */
6422 if (elt->left)
6423 elt = elt->left;
6424 else
6426 /* We have reached the smallest node bigger than the
6427 current unfilled field. Fill the space first. */
6428 next = elt->purpose;
6429 break;
6432 else
6434 /* Advance to the next bigger node. */
6435 if (elt->right)
6436 elt = elt->right;
6437 else
6439 /* We have reached the biggest node in a subtree. Find
6440 the parent of it, which is the next bigger node. */
6441 while (elt->parent && elt->parent->right == elt)
6442 elt = elt->parent;
6443 elt = elt->parent;
6444 if (elt
6445 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6446 bit_position (elt->purpose))))
6448 next = elt->purpose;
6449 break;
6456 /* Ordinarily return, but not if we want to output all
6457 and there are elements left. */
6458 if (! (all && next != 0))
6459 return;
6461 /* If it's not incremental, just skip over the gap, so that after
6462 jumping to retry we will output the next successive element. */
6463 if (TREE_CODE (constructor_type) == RECORD_TYPE
6464 || TREE_CODE (constructor_type) == UNION_TYPE)
6465 constructor_unfilled_fields = next;
6466 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6467 constructor_unfilled_index = next;
6469 /* ELT now points to the node in the pending tree with the next
6470 initializer to output. */
6471 goto retry;
6474 /* Add one non-braced element to the current constructor level.
6475 This adjusts the current position within the constructor's type.
6476 This may also start or terminate implicit levels
6477 to handle a partly-braced initializer.
6479 Once this has found the correct level for the new element,
6480 it calls output_init_element. */
6482 void
6483 process_init_element (value)
6484 tree value;
6486 tree orig_value = value;
6487 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6489 designator_depth = 0;
6490 designator_errorneous = 0;
6492 /* Handle superfluous braces around string cst as in
6493 char x[] = {"foo"}; */
6494 if (string_flag
6495 && constructor_type
6496 && TREE_CODE (constructor_type) == ARRAY_TYPE
6497 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6498 && integer_zerop (constructor_unfilled_index))
6500 if (constructor_stack->replacement_value)
6501 error_init ("excess elements in char array initializer");
6502 constructor_stack->replacement_value = value;
6503 return;
6506 if (constructor_stack->replacement_value != 0)
6508 error_init ("excess elements in struct initializer");
6509 return;
6512 /* Ignore elements of a brace group if it is entirely superfluous
6513 and has already been diagnosed. */
6514 if (constructor_type == 0)
6515 return;
6517 /* If we've exhausted any levels that didn't have braces,
6518 pop them now. */
6519 while (constructor_stack->implicit)
6521 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6522 || TREE_CODE (constructor_type) == UNION_TYPE)
6523 && constructor_fields == 0)
6524 process_init_element (pop_init_level (1));
6525 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6526 && (constructor_max_index == 0
6527 || tree_int_cst_lt (constructor_max_index,
6528 constructor_index)))
6529 process_init_element (pop_init_level (1));
6530 else
6531 break;
6534 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6535 if (constructor_range_stack)
6536 value = save_expr (value);
6538 while (1)
6540 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6542 tree fieldtype;
6543 enum tree_code fieldcode;
6545 if (constructor_fields == 0)
6547 pedwarn_init ("excess elements in struct initializer");
6548 break;
6551 fieldtype = TREE_TYPE (constructor_fields);
6552 if (fieldtype != error_mark_node)
6553 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6554 fieldcode = TREE_CODE (fieldtype);
6556 /* Accept a string constant to initialize a subarray. */
6557 if (value != 0
6558 && fieldcode == ARRAY_TYPE
6559 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6560 && string_flag)
6561 value = orig_value;
6562 /* Otherwise, if we have come to a subaggregate,
6563 and we don't have an element of its type, push into it. */
6564 else if (value != 0 && !constructor_no_implicit
6565 && value != error_mark_node
6566 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6567 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6568 || fieldcode == UNION_TYPE))
6570 push_init_level (1);
6571 continue;
6574 if (value)
6576 push_member_name (constructor_fields);
6577 output_init_element (value, fieldtype, constructor_fields, 1);
6578 RESTORE_SPELLING_DEPTH (constructor_depth);
6580 else
6581 /* Do the bookkeeping for an element that was
6582 directly output as a constructor. */
6584 /* For a record, keep track of end position of last field. */
6585 if (DECL_SIZE (constructor_fields))
6586 constructor_bit_index
6587 = size_binop (PLUS_EXPR,
6588 bit_position (constructor_fields),
6589 DECL_SIZE (constructor_fields));
6591 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6592 /* Skip any nameless bit fields. */
6593 while (constructor_unfilled_fields != 0
6594 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6595 && DECL_NAME (constructor_unfilled_fields) == 0)
6596 constructor_unfilled_fields =
6597 TREE_CHAIN (constructor_unfilled_fields);
6600 constructor_fields = TREE_CHAIN (constructor_fields);
6601 /* Skip any nameless bit fields at the beginning. */
6602 while (constructor_fields != 0
6603 && DECL_C_BIT_FIELD (constructor_fields)
6604 && DECL_NAME (constructor_fields) == 0)
6605 constructor_fields = TREE_CHAIN (constructor_fields);
6607 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6609 tree fieldtype;
6610 enum tree_code fieldcode;
6612 if (constructor_fields == 0)
6614 pedwarn_init ("excess elements in union initializer");
6615 break;
6618 fieldtype = TREE_TYPE (constructor_fields);
6619 if (fieldtype != error_mark_node)
6620 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6621 fieldcode = TREE_CODE (fieldtype);
6623 /* Warn that traditional C rejects initialization of unions.
6624 We skip the warning if the value is zero. This is done
6625 under the assumption that the zero initializer in user
6626 code appears conditioned on e.g. __STDC__ to avoid
6627 "missing initializer" warnings and relies on default
6628 initialization to zero in the traditional C case. */
6629 if (warn_traditional && !in_system_header
6630 && !(value && (integer_zerop (value) || real_zerop (value))))
6631 warning ("traditional C rejects initialization of unions");
6633 /* Accept a string constant to initialize a subarray. */
6634 if (value != 0
6635 && fieldcode == ARRAY_TYPE
6636 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6637 && string_flag)
6638 value = orig_value;
6639 /* Otherwise, if we have come to a subaggregate,
6640 and we don't have an element of its type, push into it. */
6641 else if (value != 0 && !constructor_no_implicit
6642 && value != error_mark_node
6643 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6644 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6645 || fieldcode == UNION_TYPE))
6647 push_init_level (1);
6648 continue;
6651 if (value)
6653 push_member_name (constructor_fields);
6654 output_init_element (value, fieldtype, constructor_fields, 1);
6655 RESTORE_SPELLING_DEPTH (constructor_depth);
6657 else
6658 /* Do the bookkeeping for an element that was
6659 directly output as a constructor. */
6661 constructor_bit_index = DECL_SIZE (constructor_fields);
6662 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6665 constructor_fields = 0;
6667 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6669 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6670 enum tree_code eltcode = TREE_CODE (elttype);
6672 /* Accept a string constant to initialize a subarray. */
6673 if (value != 0
6674 && eltcode == ARRAY_TYPE
6675 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6676 && string_flag)
6677 value = orig_value;
6678 /* Otherwise, if we have come to a subaggregate,
6679 and we don't have an element of its type, push into it. */
6680 else if (value != 0 && !constructor_no_implicit
6681 && value != error_mark_node
6682 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6683 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6684 || eltcode == UNION_TYPE))
6686 push_init_level (1);
6687 continue;
6690 if (constructor_max_index != 0
6691 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6692 || integer_all_onesp (constructor_max_index)))
6694 pedwarn_init ("excess elements in array initializer");
6695 break;
6698 /* Now output the actual element. */
6699 if (value)
6701 push_array_bounds (tree_low_cst (constructor_index, 0));
6702 output_init_element (value, elttype, constructor_index, 1);
6703 RESTORE_SPELLING_DEPTH (constructor_depth);
6706 constructor_index
6707 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6709 if (! value)
6710 /* If we are doing the bookkeeping for an element that was
6711 directly output as a constructor, we must update
6712 constructor_unfilled_index. */
6713 constructor_unfilled_index = constructor_index;
6716 /* Handle the sole element allowed in a braced initializer
6717 for a scalar variable. */
6718 else if (constructor_fields == 0)
6720 pedwarn_init ("excess elements in scalar initializer");
6721 break;
6723 else
6725 if (value)
6726 output_init_element (value, constructor_type, NULL_TREE, 1);
6727 constructor_fields = 0;
6730 /* Handle range initializers either at this level or anywhere higher
6731 in the designator stack. */
6732 if (constructor_range_stack)
6734 struct constructor_range_stack *p, *range_stack;
6735 int finish = 0;
6737 range_stack = constructor_range_stack;
6738 constructor_range_stack = 0;
6739 while (constructor_stack != range_stack->stack)
6741 if (!constructor_stack->implicit)
6742 abort ();
6743 process_init_element (pop_init_level (1));
6745 for (p = range_stack;
6746 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6747 p = p->prev)
6749 if (!constructor_stack->implicit)
6750 abort ();
6751 process_init_element (pop_init_level (1));
6754 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6755 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6756 finish = 1;
6758 while (1)
6760 constructor_index = p->index;
6761 constructor_fields = p->fields;
6762 if (finish && p->range_end && p->index == p->range_start)
6764 finish = 0;
6765 p->prev = 0;
6767 p = p->next;
6768 if (!p)
6769 break;
6770 push_init_level (2);
6771 p->stack = constructor_stack;
6772 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6773 p->index = p->range_start;
6776 if (!finish)
6777 constructor_range_stack = range_stack;
6778 continue;
6781 break;
6784 constructor_range_stack = 0;
6787 /* Build a simple asm-statement, from one string literal. */
6788 tree
6789 simple_asm_stmt (expr)
6790 tree expr;
6792 STRIP_NOPS (expr);
6794 if (TREE_CODE (expr) == ADDR_EXPR)
6795 expr = TREE_OPERAND (expr, 0);
6797 if (TREE_CODE (expr) == STRING_CST)
6799 tree stmt;
6801 if (TREE_CHAIN (expr))
6802 expr = combine_strings (expr);
6803 stmt = add_stmt (build_stmt (ASM_STMT, NULL_TREE, expr,
6804 NULL_TREE, NULL_TREE,
6805 NULL_TREE));
6806 ASM_INPUT_P (stmt) = 1;
6807 return stmt;
6810 error ("argument of `asm' is not a constant string");
6811 return NULL_TREE;
6814 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6815 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6817 tree
6818 build_asm_stmt (cv_qualifier, string, outputs, inputs, clobbers)
6819 tree cv_qualifier;
6820 tree string;
6821 tree outputs;
6822 tree inputs;
6823 tree clobbers;
6825 tree tail;
6827 if (TREE_CHAIN (string))
6828 string = combine_strings (string);
6829 if (TREE_CODE (string) != STRING_CST)
6831 error ("asm template is not a string constant");
6832 return NULL_TREE;
6835 if (cv_qualifier != NULL_TREE
6836 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6838 warning ("%s qualifier ignored on asm",
6839 IDENTIFIER_POINTER (cv_qualifier));
6840 cv_qualifier = NULL_TREE;
6843 /* We can remove output conversions that change the type,
6844 but not the mode. */
6845 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6847 tree output = TREE_VALUE (tail);
6849 STRIP_NOPS (output);
6850 TREE_VALUE (tail) = output;
6852 /* Allow conversions as LHS here. build_modify_expr as called below
6853 will do the right thing with them. */
6854 while (TREE_CODE (output) == NOP_EXPR
6855 || TREE_CODE (output) == CONVERT_EXPR
6856 || TREE_CODE (output) == FLOAT_EXPR
6857 || TREE_CODE (output) == FIX_TRUNC_EXPR
6858 || TREE_CODE (output) == FIX_FLOOR_EXPR
6859 || TREE_CODE (output) == FIX_ROUND_EXPR
6860 || TREE_CODE (output) == FIX_CEIL_EXPR)
6861 output = TREE_OPERAND (output, 0);
6863 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6866 /* Remove output conversions that change the type but not the mode. */
6867 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6869 tree output = TREE_VALUE (tail);
6870 STRIP_NOPS (output);
6871 TREE_VALUE (tail) = output;
6874 /* Perform default conversions on array and function inputs.
6875 Don't do this for other types as it would screw up operands
6876 expected to be in memory. */
6877 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6878 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6879 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6880 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6882 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6883 outputs, inputs, clobbers));
6886 /* Expand an ASM statement with operands, handling output operands
6887 that are not variables or INDIRECT_REFS by transforming such
6888 cases into cases that expand_asm_operands can handle.
6890 Arguments are same as for expand_asm_operands. */
6892 void
6893 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6894 tree string, outputs, inputs, clobbers;
6895 int vol;
6896 const char *filename;
6897 int line;
6899 int noutputs = list_length (outputs);
6900 register int i;
6901 /* o[I] is the place that output number I should be written. */
6902 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6903 register tree tail;
6905 /* Record the contents of OUTPUTS before it is modified. */
6906 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6907 o[i] = TREE_VALUE (tail);
6909 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6910 OUTPUTS some trees for where the values were actually stored. */
6911 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6913 /* Copy all the intermediate outputs into the specified outputs. */
6914 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6916 if (o[i] != TREE_VALUE (tail))
6918 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6919 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6920 free_temp_slots ();
6922 /* Restore the original value so that it's correct the next
6923 time we expand this function. */
6924 TREE_VALUE (tail) = o[i];
6926 /* Detect modification of read-only values.
6927 (Otherwise done by build_modify_expr.) */
6928 else
6930 tree type = TREE_TYPE (o[i]);
6931 if (TREE_READONLY (o[i])
6932 || TYPE_READONLY (type)
6933 || ((TREE_CODE (type) == RECORD_TYPE
6934 || TREE_CODE (type) == UNION_TYPE)
6935 && C_TYPE_FIELDS_READONLY (type)))
6936 readonly_warning (o[i], "modification by `asm'");
6940 /* Those MODIFY_EXPRs could do autoincrements. */
6941 emit_queue ();
6944 /* Expand a C `return' statement.
6945 RETVAL is the expression for what to return,
6946 or a null pointer for `return;' with no value. */
6948 tree
6949 c_expand_return (retval)
6950 tree retval;
6952 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6954 if (TREE_THIS_VOLATILE (current_function_decl))
6955 warning ("function declared `noreturn' has a `return' statement");
6957 if (!retval)
6959 current_function_returns_null = 1;
6960 if ((warn_return_type || flag_isoc99)
6961 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6962 pedwarn_c99 ("`return' with no value, in function returning non-void");
6964 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6966 current_function_returns_null = 1;
6967 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6968 pedwarn ("`return' with a value, in function returning void");
6970 else
6972 tree t = convert_for_assignment (valtype, retval, _("return"),
6973 NULL_TREE, NULL_TREE, 0);
6974 tree res = DECL_RESULT (current_function_decl);
6975 tree inner;
6977 if (t == error_mark_node)
6978 return NULL_TREE;
6980 inner = t = convert (TREE_TYPE (res), t);
6982 /* Strip any conversions, additions, and subtractions, and see if
6983 we are returning the address of a local variable. Warn if so. */
6984 while (1)
6986 switch (TREE_CODE (inner))
6988 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6989 case PLUS_EXPR:
6990 inner = TREE_OPERAND (inner, 0);
6991 continue;
6993 case MINUS_EXPR:
6994 /* If the second operand of the MINUS_EXPR has a pointer
6995 type (or is converted from it), this may be valid, so
6996 don't give a warning. */
6998 tree op1 = TREE_OPERAND (inner, 1);
7000 while (! POINTER_TYPE_P (TREE_TYPE (op1))
7001 && (TREE_CODE (op1) == NOP_EXPR
7002 || TREE_CODE (op1) == NON_LVALUE_EXPR
7003 || TREE_CODE (op1) == CONVERT_EXPR))
7004 op1 = TREE_OPERAND (op1, 0);
7006 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7007 break;
7009 inner = TREE_OPERAND (inner, 0);
7010 continue;
7013 case ADDR_EXPR:
7014 inner = TREE_OPERAND (inner, 0);
7016 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
7017 inner = TREE_OPERAND (inner, 0);
7019 if (TREE_CODE (inner) == VAR_DECL
7020 && ! DECL_EXTERNAL (inner)
7021 && ! TREE_STATIC (inner)
7022 && DECL_CONTEXT (inner) == current_function_decl)
7023 warning ("function returns address of local variable");
7024 break;
7026 default:
7027 break;
7030 break;
7033 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
7034 current_function_returns_value = 1;
7037 return add_stmt (build_return_stmt (retval));
7040 struct c_switch {
7041 /* The SWITCH_STMT being built. */
7042 tree switch_stmt;
7043 /* A splay-tree mapping the low element of a case range to the high
7044 element, or NULL_TREE if there is no high element. Used to
7045 determine whether or not a new case label duplicates an old case
7046 label. We need a tree, rather than simply a hash table, because
7047 of the GNU case range extension. */
7048 splay_tree cases;
7049 /* The next node on the stack. */
7050 struct c_switch *next;
7053 /* A stack of the currently active switch statements. The innermost
7054 switch statement is on the top of the stack. There is no need to
7055 mark the stack for garbage collection because it is only active
7056 during the processing of the body of a function, and we never
7057 collect at that point. */
7059 static struct c_switch *switch_stack;
7061 /* Start a C switch statement, testing expression EXP. Return the new
7062 SWITCH_STMT. */
7064 tree
7065 c_start_case (exp)
7066 tree exp;
7068 register enum tree_code code;
7069 tree type;
7070 struct c_switch *cs;
7072 if (exp != error_mark_node)
7074 code = TREE_CODE (TREE_TYPE (exp));
7075 type = TREE_TYPE (exp);
7077 if (! INTEGRAL_TYPE_P (type)
7078 && code != ERROR_MARK)
7080 error ("switch quantity not an integer");
7081 exp = integer_zero_node;
7083 else
7085 tree index;
7086 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7088 if (warn_traditional && !in_system_header
7089 && (type == long_integer_type_node
7090 || type == long_unsigned_type_node))
7091 warning ("`long' switch expression not converted to `int' in ISO C");
7093 exp = default_conversion (exp);
7094 type = TREE_TYPE (exp);
7095 index = get_unwidened (exp, NULL_TREE);
7096 /* We can't strip a conversion from a signed type to an
7097 unsigned, because if we did, int_fits_type_p would do the
7098 wrong thing when checking case values for being in range,
7099 and it's too hard to do the right thing. */
7100 if (TREE_UNSIGNED (TREE_TYPE (exp))
7101 == TREE_UNSIGNED (TREE_TYPE (index)))
7102 exp = index;
7106 /* Add this new SWITCH_STMT to the stack. */
7107 cs = (struct c_switch *) xmalloc (sizeof (*cs));
7108 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, NULL_TREE);
7109 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7110 cs->next = switch_stack;
7111 switch_stack = cs;
7113 return add_stmt (switch_stack->switch_stmt);
7116 /* Process a case label. */
7118 tree
7119 do_case (low_value, high_value)
7120 tree low_value;
7121 tree high_value;
7123 tree label = NULL_TREE;
7125 if (switch_stack)
7127 label = c_add_case_label (switch_stack->cases,
7128 SWITCH_COND (switch_stack->switch_stmt),
7129 low_value, high_value);
7130 if (label == error_mark_node)
7131 label = NULL_TREE;
7133 else if (low_value)
7134 error ("case label not within a switch statement");
7135 else
7136 error ("`default' label not within a switch statement");
7138 return label;
7141 /* Finish the switch statement. */
7143 void
7144 c_finish_case ()
7146 struct c_switch *cs = switch_stack;
7148 RECHAIN_STMTS (cs->switch_stmt, SWITCH_BODY (cs->switch_stmt));
7150 /* Pop the stack. */
7151 switch_stack = switch_stack->next;
7152 splay_tree_delete (cs->cases);
7153 free (cs);