2001-11-01 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / gcc / c-typeck.c
blob37810f610bb6196580b5c252148b8696e4240c23
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 GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 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 enum tree_code code1;
195 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 tree t1 = type1;
440 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 bool d1_variable, d2_variable;
509 bool d1_zero, d2_zero;
510 val = 1;
512 /* Target types must match incl. qualifiers. */
513 if (TREE_TYPE (t1) != TREE_TYPE (t2)
514 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
515 return 0;
517 /* Sizes must match unless one is missing or variable. */
518 if (d1 == 0 || d2 == 0 || d1 == d2)
519 break;
521 d1_zero = ! TYPE_MAX_VALUE (d1);
522 d2_zero = ! TYPE_MAX_VALUE (d2);
524 d1_variable = (! d1_zero
525 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
526 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
527 d2_variable = (! d2_zero
528 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
529 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
531 if (d1_variable || d2_variable)
532 break;
533 if (d1_zero && d2_zero)
534 break;
535 if (d1_zero || d2_zero
536 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
537 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
538 val = 0;
540 break;
543 case RECORD_TYPE:
544 if (maybe_objc_comptypes (t1, t2, 0) == 1)
545 val = 1;
546 break;
548 default:
549 break;
551 return attrval == 2 && val == 1 ? 2 : val;
554 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
555 ignoring their qualifiers. */
557 static int
558 comp_target_types (ttl, ttr)
559 tree ttl, ttr;
561 int val;
563 /* Give maybe_objc_comptypes a crack at letting these types through. */
564 if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0)
565 return val;
567 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
568 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
570 if (val == 2 && pedantic)
571 pedwarn ("types are not quite compatible");
572 return val;
575 /* Subroutines of `comptypes'. */
577 /* Return 1 if two function types F1 and F2 are compatible.
578 If either type specifies no argument types,
579 the other must specify a fixed number of self-promoting arg types.
580 Otherwise, if one type specifies only the number of arguments,
581 the other must specify that number of self-promoting arg types.
582 Otherwise, the argument types must match. */
584 static int
585 function_types_compatible_p (f1, f2)
586 tree f1, f2;
588 tree args1, args2;
589 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
590 int val = 1;
591 int val1;
593 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
594 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
595 return 0;
597 args1 = TYPE_ARG_TYPES (f1);
598 args2 = TYPE_ARG_TYPES (f2);
600 /* An unspecified parmlist matches any specified parmlist
601 whose argument types don't need default promotions. */
603 if (args1 == 0)
605 if (!self_promoting_args_p (args2))
606 return 0;
607 /* If one of these types comes from a non-prototype fn definition,
608 compare that with the other type's arglist.
609 If they don't match, ask for a warning (but no error). */
610 if (TYPE_ACTUAL_ARG_TYPES (f1)
611 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
612 val = 2;
613 return val;
615 if (args2 == 0)
617 if (!self_promoting_args_p (args1))
618 return 0;
619 if (TYPE_ACTUAL_ARG_TYPES (f2)
620 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
621 val = 2;
622 return val;
625 /* Both types have argument lists: compare them and propagate results. */
626 val1 = type_lists_compatible_p (args1, args2);
627 return val1 != 1 ? val1 : val;
630 /* Check two lists of types for compatibility,
631 returning 0 for incompatible, 1 for compatible,
632 or 2 for compatible with warning. */
634 static int
635 type_lists_compatible_p (args1, args2)
636 tree args1, args2;
638 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
639 int val = 1;
640 int newval = 0;
642 while (1)
644 if (args1 == 0 && args2 == 0)
645 return val;
646 /* If one list is shorter than the other,
647 they fail to match. */
648 if (args1 == 0 || args2 == 0)
649 return 0;
650 /* A null pointer instead of a type
651 means there is supposed to be an argument
652 but nothing is specified about what type it has.
653 So match anything that self-promotes. */
654 if (TREE_VALUE (args1) == 0)
656 if (simple_type_promotes_to (TREE_VALUE (args2)) != NULL_TREE)
657 return 0;
659 else if (TREE_VALUE (args2) == 0)
661 if (simple_type_promotes_to (TREE_VALUE (args1)) != NULL_TREE)
662 return 0;
664 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
665 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
667 /* Allow wait (union {union wait *u; int *i} *)
668 and wait (union wait *) to be compatible. */
669 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
670 && (TYPE_NAME (TREE_VALUE (args1)) == 0
671 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
672 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
673 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
674 TYPE_SIZE (TREE_VALUE (args2))))
676 tree memb;
677 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
678 memb; memb = TREE_CHAIN (memb))
679 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
680 break;
681 if (memb == 0)
682 return 0;
684 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
685 && (TYPE_NAME (TREE_VALUE (args2)) == 0
686 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
687 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
688 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
689 TYPE_SIZE (TREE_VALUE (args1))))
691 tree memb;
692 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
693 memb; memb = TREE_CHAIN (memb))
694 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
695 break;
696 if (memb == 0)
697 return 0;
699 else
700 return 0;
703 /* comptypes said ok, but record if it said to warn. */
704 if (newval > val)
705 val = newval;
707 args1 = TREE_CHAIN (args1);
708 args2 = TREE_CHAIN (args2);
712 /* Compute the value of the `sizeof' operator. */
714 tree
715 c_sizeof (type)
716 tree type;
718 enum tree_code code = TREE_CODE (type);
719 tree size;
721 if (code == FUNCTION_TYPE)
723 if (pedantic || warn_pointer_arith)
724 pedwarn ("sizeof applied to a function type");
725 size = size_one_node;
727 else if (code == VOID_TYPE)
729 if (pedantic || warn_pointer_arith)
730 pedwarn ("sizeof applied to a void type");
731 size = size_one_node;
733 else if (code == ERROR_MARK)
734 size = size_one_node;
735 else if (!COMPLETE_TYPE_P (type))
737 error ("sizeof applied to an incomplete type");
738 size = size_zero_node;
740 else
741 /* Convert in case a char is more than one unit. */
742 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
743 size_int (TYPE_PRECISION (char_type_node)
744 / BITS_PER_UNIT));
746 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
747 TYPE_IS_SIZETYPE means that certain things (like overflow) will
748 never happen. However, this node should really have type
749 `size_t', which is just a typedef for an ordinary integer type. */
750 return fold (build1 (NOP_EXPR, c_size_type_node, size));
753 tree
754 c_sizeof_nowarn (type)
755 tree type;
757 enum tree_code code = TREE_CODE (type);
758 tree size;
760 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
761 size = size_one_node;
762 else if (!COMPLETE_TYPE_P (type))
763 size = size_zero_node;
764 else
765 /* Convert in case a char is more than one unit. */
766 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
767 size_int (TYPE_PRECISION (char_type_node)
768 / BITS_PER_UNIT));
770 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
771 TYPE_IS_SIZETYPE means that certain things (like overflow) will
772 never happen. However, this node should really have type
773 `size_t', which is just a typedef for an ordinary integer type. */
774 return fold (build1 (NOP_EXPR, c_size_type_node, size));
777 /* Compute the size to increment a pointer by. */
779 tree
780 c_size_in_bytes (type)
781 tree type;
783 enum tree_code code = TREE_CODE (type);
785 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
786 return size_one_node;
788 if (!COMPLETE_OR_VOID_TYPE_P (type))
790 error ("arithmetic on pointer to an incomplete type");
791 return size_one_node;
794 /* Convert in case a char is more than one unit. */
795 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
796 size_int (TYPE_PRECISION (char_type_node)
797 / BITS_PER_UNIT));
800 /* Return either DECL or its known constant value (if it has one). */
802 tree
803 decl_constant_value (decl)
804 tree decl;
806 if (/* Don't change a variable array bound or initial value to a constant
807 in a place where a variable is invalid. */
808 current_function_decl != 0
809 && ! TREE_THIS_VOLATILE (decl)
810 && TREE_READONLY (decl)
811 && DECL_INITIAL (decl) != 0
812 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
813 /* This is invalid if initial value is not constant.
814 If it has either a function call, a memory reference,
815 or a variable, then re-evaluating it could give different results. */
816 && TREE_CONSTANT (DECL_INITIAL (decl))
817 /* Check for cases where this is sub-optimal, even though valid. */
818 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
819 return DECL_INITIAL (decl);
820 return decl;
823 /* Return either DECL or its known constant value (if it has one), but
824 return DECL if pedantic or DECL has mode BLKmode. This is for
825 bug-compatibility with the old behavior of decl_constant_value
826 (before GCC 3.0); every use of this function is a bug and it should
827 be removed before GCC 3.1. It is not appropriate to use pedantic
828 in a way that affects optimization, and BLKmode is probably not the
829 right test for avoiding misoptimizations either. */
831 static tree
832 decl_constant_value_for_broken_optimization (decl)
833 tree decl;
835 if (pedantic || DECL_MODE (decl) == BLKmode)
836 return decl;
837 else
838 return decl_constant_value (decl);
841 /* Perform default promotions for C data used in expressions.
842 Arrays and functions are converted to pointers;
843 enumeral types or short or char, to int.
844 In addition, manifest constants symbols are replaced by their values. */
846 tree
847 default_conversion (exp)
848 tree exp;
850 tree orig_exp;
851 tree type = TREE_TYPE (exp);
852 enum tree_code code = TREE_CODE (type);
854 /* Constants can be used directly unless they're not loadable. */
855 if (TREE_CODE (exp) == CONST_DECL)
856 exp = DECL_INITIAL (exp);
858 /* Replace a nonvolatile const static variable with its value unless
859 it is an array, in which case we must be sure that taking the
860 address of the array produces consistent results. */
861 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
863 exp = decl_constant_value_for_broken_optimization (exp);
864 type = TREE_TYPE (exp);
867 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
868 an lvalue.
870 Do not use STRIP_NOPS here! It will remove conversions from pointer
871 to integer and cause infinite recursion. */
872 orig_exp = exp;
873 while (TREE_CODE (exp) == NON_LVALUE_EXPR
874 || (TREE_CODE (exp) == NOP_EXPR
875 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
876 exp = TREE_OPERAND (exp, 0);
878 /* Preserve the original expression code. */
879 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
880 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
882 /* Normally convert enums to int,
883 but convert wide enums to something wider. */
884 if (code == ENUMERAL_TYPE)
886 type = type_for_size (MAX (TYPE_PRECISION (type),
887 TYPE_PRECISION (integer_type_node)),
888 ((flag_traditional
889 || (TYPE_PRECISION (type)
890 >= TYPE_PRECISION (integer_type_node)))
891 && TREE_UNSIGNED (type)));
893 return convert (type, exp);
896 if (TREE_CODE (exp) == COMPONENT_REF
897 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
898 /* If it's thinner than an int, promote it like a
899 c_promoting_integer_type_p, otherwise leave it alone. */
900 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
901 TYPE_PRECISION (integer_type_node)))
902 return convert (flag_traditional && TREE_UNSIGNED (type)
903 ? unsigned_type_node : integer_type_node,
904 exp);
906 if (c_promoting_integer_type_p (type))
908 /* Traditionally, unsignedness is preserved in default promotions.
909 Also preserve unsignedness if not really getting any wider. */
910 if (TREE_UNSIGNED (type)
911 && (flag_traditional
912 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
913 return convert (unsigned_type_node, exp);
915 return convert (integer_type_node, exp);
918 if (flag_traditional && !flag_allow_single_precision
919 && TYPE_MAIN_VARIANT (type) == float_type_node)
920 return convert (double_type_node, exp);
922 if (code == VOID_TYPE)
924 error ("void value not ignored as it ought to be");
925 return error_mark_node;
927 if (code == FUNCTION_TYPE)
929 return build_unary_op (ADDR_EXPR, exp, 0);
931 if (code == ARRAY_TYPE)
933 tree adr;
934 tree restype = TREE_TYPE (type);
935 tree ptrtype;
936 int constp = 0;
937 int volatilep = 0;
939 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
941 constp = TREE_READONLY (exp);
942 volatilep = TREE_THIS_VOLATILE (exp);
945 if (TYPE_QUALS (type) || constp || volatilep)
946 restype
947 = c_build_qualified_type (restype,
948 TYPE_QUALS (type)
949 | (constp * TYPE_QUAL_CONST)
950 | (volatilep * TYPE_QUAL_VOLATILE));
952 if (TREE_CODE (exp) == INDIRECT_REF)
953 return convert (TYPE_POINTER_TO (restype),
954 TREE_OPERAND (exp, 0));
956 if (TREE_CODE (exp) == COMPOUND_EXPR)
958 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
959 return build (COMPOUND_EXPR, TREE_TYPE (op1),
960 TREE_OPERAND (exp, 0), op1);
963 if (! lvalue_p (exp)
964 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
966 error ("invalid use of non-lvalue array");
967 return error_mark_node;
970 ptrtype = build_pointer_type (restype);
972 if (TREE_CODE (exp) == VAR_DECL)
974 /* ??? This is not really quite correct
975 in that the type of the operand of ADDR_EXPR
976 is not the target type of the type of the ADDR_EXPR itself.
977 Question is, can this lossage be avoided? */
978 adr = build1 (ADDR_EXPR, ptrtype, exp);
979 if (mark_addressable (exp) == 0)
980 return error_mark_node;
981 TREE_CONSTANT (adr) = staticp (exp);
982 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
983 return adr;
985 /* This way is better for a COMPONENT_REF since it can
986 simplify the offset for a component. */
987 adr = build_unary_op (ADDR_EXPR, exp, 1);
988 return convert (ptrtype, adr);
990 return exp;
993 /* Look up component name in the structure type definition.
995 If this component name is found indirectly within an anonymous union,
996 store in *INDIRECT the component which directly contains
997 that anonymous union. Otherwise, set *INDIRECT to 0. */
999 static tree
1000 lookup_field (type, component, indirect)
1001 tree type, component;
1002 tree *indirect;
1004 tree field;
1006 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1007 to the field elements. Use a binary search on this array to quickly
1008 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1009 will always be set for structures which have many elements. */
1011 if (TYPE_LANG_SPECIFIC (type))
1013 int bot, top, half;
1014 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1016 field = TYPE_FIELDS (type);
1017 bot = 0;
1018 top = TYPE_LANG_SPECIFIC (type)->len;
1019 while (top - bot > 1)
1021 half = (top - bot + 1) >> 1;
1022 field = field_array[bot+half];
1024 if (DECL_NAME (field) == NULL_TREE)
1026 /* Step through all anon unions in linear fashion. */
1027 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1029 tree anon = 0, junk;
1031 field = field_array[bot++];
1032 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1033 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1034 anon = lookup_field (TREE_TYPE (field), component, &junk);
1036 if (anon != NULL_TREE)
1038 *indirect = field;
1039 return anon;
1043 /* Entire record is only anon unions. */
1044 if (bot > top)
1045 return NULL_TREE;
1047 /* Restart the binary search, with new lower bound. */
1048 continue;
1051 if (DECL_NAME (field) == component)
1052 break;
1053 if (DECL_NAME (field) < component)
1054 bot += half;
1055 else
1056 top = bot + half;
1059 if (DECL_NAME (field_array[bot]) == component)
1060 field = field_array[bot];
1061 else if (DECL_NAME (field) != component)
1062 field = 0;
1064 else
1066 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1068 if (DECL_NAME (field) == NULL_TREE)
1070 tree junk;
1071 tree anon = 0;
1073 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1074 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1075 anon = lookup_field (TREE_TYPE (field), component, &junk);
1077 if (anon != NULL_TREE)
1079 *indirect = field;
1080 return anon;
1084 if (DECL_NAME (field) == component)
1085 break;
1089 *indirect = NULL_TREE;
1090 return field;
1093 /* Make an expression to refer to the COMPONENT field of
1094 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1096 tree
1097 build_component_ref (datum, component)
1098 tree datum, component;
1100 tree type = TREE_TYPE (datum);
1101 enum tree_code code = TREE_CODE (type);
1102 tree field = NULL;
1103 tree ref;
1105 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1106 unless we are not to support things not strictly ANSI. */
1107 switch (TREE_CODE (datum))
1109 case COMPOUND_EXPR:
1111 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1112 return build (COMPOUND_EXPR, TREE_TYPE (value),
1113 TREE_OPERAND (datum, 0), value);
1115 case COND_EXPR:
1116 return build_conditional_expr
1117 (TREE_OPERAND (datum, 0),
1118 build_component_ref (TREE_OPERAND (datum, 1), component),
1119 build_component_ref (TREE_OPERAND (datum, 2), component));
1121 default:
1122 break;
1125 /* See if there is a field or component with name COMPONENT. */
1127 if (code == RECORD_TYPE || code == UNION_TYPE)
1129 tree indirect = 0;
1131 if (!COMPLETE_TYPE_P (type))
1133 incomplete_type_error (NULL_TREE, type);
1134 return error_mark_node;
1137 field = lookup_field (type, component, &indirect);
1139 if (!field)
1141 error ("%s has no member named `%s'",
1142 code == RECORD_TYPE ? "structure" : "union",
1143 IDENTIFIER_POINTER (component));
1144 return error_mark_node;
1146 if (TREE_TYPE (field) == error_mark_node)
1147 return error_mark_node;
1149 /* If FIELD was found buried within an anonymous union,
1150 make one COMPONENT_REF to get that anonymous union,
1151 then fall thru to make a second COMPONENT_REF to get FIELD. */
1152 if (indirect != 0)
1154 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1155 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1156 TREE_READONLY (ref) = 1;
1157 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1158 TREE_THIS_VOLATILE (ref) = 1;
1159 datum = ref;
1162 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1164 if (TREE_READONLY (datum) || TREE_READONLY (field))
1165 TREE_READONLY (ref) = 1;
1166 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1167 TREE_THIS_VOLATILE (ref) = 1;
1169 return ref;
1171 else if (code != ERROR_MARK)
1172 error ("request for member `%s' in something not a structure or union",
1173 IDENTIFIER_POINTER (component));
1175 return error_mark_node;
1178 /* Given an expression PTR for a pointer, return an expression
1179 for the value pointed to.
1180 ERRORSTRING is the name of the operator to appear in error messages. */
1182 tree
1183 build_indirect_ref (ptr, errorstring)
1184 tree ptr;
1185 const char *errorstring;
1187 tree pointer = default_conversion (ptr);
1188 tree type = TREE_TYPE (pointer);
1190 if (TREE_CODE (type) == POINTER_TYPE)
1192 if (TREE_CODE (pointer) == ADDR_EXPR
1193 && !flag_volatile
1194 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1195 == TREE_TYPE (type)))
1196 return TREE_OPERAND (pointer, 0);
1197 else
1199 tree t = TREE_TYPE (type);
1200 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1202 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1204 error ("dereferencing pointer to incomplete type");
1205 return error_mark_node;
1207 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1208 warning ("dereferencing `void *' pointer");
1210 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1211 so that we get the proper error message if the result is used
1212 to assign to. Also, &* is supposed to be a no-op.
1213 And ANSI C seems to specify that the type of the result
1214 should be the const type. */
1215 /* A de-reference of a pointer to const is not a const. It is valid
1216 to change it via some other pointer. */
1217 TREE_READONLY (ref) = TYPE_READONLY (t);
1218 TREE_SIDE_EFFECTS (ref)
1219 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1220 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1221 return ref;
1224 else if (TREE_CODE (pointer) != ERROR_MARK)
1225 error ("invalid type argument of `%s'", errorstring);
1226 return error_mark_node;
1229 /* This handles expressions of the form "a[i]", which denotes
1230 an array reference.
1232 This is logically equivalent in C to *(a+i), but we may do it differently.
1233 If A is a variable or a member, we generate a primitive ARRAY_REF.
1234 This avoids forcing the array out of registers, and can work on
1235 arrays that are not lvalues (for example, members of structures returned
1236 by functions). */
1238 tree
1239 build_array_ref (array, index)
1240 tree array, index;
1242 if (index == 0)
1244 error ("subscript missing in array reference");
1245 return error_mark_node;
1248 if (TREE_TYPE (array) == error_mark_node
1249 || TREE_TYPE (index) == error_mark_node)
1250 return error_mark_node;
1252 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1253 && TREE_CODE (array) != INDIRECT_REF)
1255 tree rval, type;
1257 /* Subscripting with type char is likely to lose
1258 on a machine where chars are signed.
1259 So warn on any machine, but optionally.
1260 Don't warn for unsigned char since that type is safe.
1261 Don't warn for signed char because anyone who uses that
1262 must have done so deliberately. */
1263 if (warn_char_subscripts
1264 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1265 warning ("array subscript has type `char'");
1267 /* Apply default promotions *after* noticing character types. */
1268 index = default_conversion (index);
1270 /* Require integer *after* promotion, for sake of enums. */
1271 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1273 error ("array subscript is not an integer");
1274 return error_mark_node;
1277 /* An array that is indexed by a non-constant
1278 cannot be stored in a register; we must be able to do
1279 address arithmetic on its address.
1280 Likewise an array of elements of variable size. */
1281 if (TREE_CODE (index) != INTEGER_CST
1282 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1283 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1285 if (mark_addressable (array) == 0)
1286 return error_mark_node;
1288 /* An array that is indexed by a constant value which is not within
1289 the array bounds cannot be stored in a register either; because we
1290 would get a crash in store_bit_field/extract_bit_field when trying
1291 to access a non-existent part of the register. */
1292 if (TREE_CODE (index) == INTEGER_CST
1293 && TYPE_VALUES (TREE_TYPE (array))
1294 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1296 if (mark_addressable (array) == 0)
1297 return error_mark_node;
1300 if (pedantic)
1302 tree foo = array;
1303 while (TREE_CODE (foo) == COMPONENT_REF)
1304 foo = TREE_OPERAND (foo, 0);
1305 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1306 pedwarn ("ISO C forbids subscripting `register' array");
1307 else if (! flag_isoc99 && ! lvalue_p (foo))
1308 pedwarn ("ISO C89 forbids subscripting non-lvalue array");
1311 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1312 rval = build (ARRAY_REF, type, array, index);
1313 /* Array ref is const/volatile if the array elements are
1314 or if the array is. */
1315 TREE_READONLY (rval)
1316 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1317 | TREE_READONLY (array));
1318 TREE_SIDE_EFFECTS (rval)
1319 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1320 | TREE_SIDE_EFFECTS (array));
1321 TREE_THIS_VOLATILE (rval)
1322 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1323 /* This was added by rms on 16 Nov 91.
1324 It fixes vol struct foo *a; a->elts[1]
1325 in an inline function.
1326 Hope it doesn't break something else. */
1327 | TREE_THIS_VOLATILE (array));
1328 return require_complete_type (fold (rval));
1332 tree ar = default_conversion (array);
1333 tree ind = default_conversion (index);
1335 /* Do the same warning check as above, but only on the part that's
1336 syntactically the index and only if it is also semantically
1337 the index. */
1338 if (warn_char_subscripts
1339 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1340 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1341 warning ("subscript has type `char'");
1343 /* Put the integer in IND to simplify error checking. */
1344 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1346 tree temp = ar;
1347 ar = ind;
1348 ind = temp;
1351 if (ar == error_mark_node)
1352 return ar;
1354 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1355 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1357 error ("subscripted value is neither array nor pointer");
1358 return error_mark_node;
1360 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1362 error ("array subscript is not an integer");
1363 return error_mark_node;
1366 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1367 "array indexing");
1371 /* Build an external reference to identifier ID. FUN indicates
1372 whether this will be used for a function call. */
1373 tree
1374 build_external_ref (id, fun)
1375 tree id;
1376 int fun;
1378 tree ref;
1379 tree decl = lookup_name (id);
1380 tree objc_ivar = lookup_objc_ivar (id);
1382 if (!decl || decl == error_mark_node || C_DECL_ANTICIPATED (decl))
1384 if (objc_ivar)
1385 ref = objc_ivar;
1386 else if (fun)
1388 if (!decl || decl == error_mark_node)
1389 /* Ordinary implicit function declaration. */
1390 ref = implicitly_declare (id);
1391 else
1393 /* Implicit declaration of built-in function. Don't
1394 change the built-in declaration, but don't let this
1395 go by silently, either. */
1396 implicit_decl_warning (id);
1398 /* only issue this warning once */
1399 C_DECL_ANTICIPATED (decl) = 0;
1400 ref = decl;
1403 else
1405 /* Reference to undeclared variable, including reference to
1406 builtin outside of function-call context. */
1407 if (current_function_decl == 0)
1408 error ("`%s' undeclared here (not in a function)",
1409 IDENTIFIER_POINTER (id));
1410 else
1412 if (IDENTIFIER_GLOBAL_VALUE (id) != error_mark_node
1413 || IDENTIFIER_ERROR_LOCUS (id) != current_function_decl)
1415 error ("`%s' undeclared (first use in this function)",
1416 IDENTIFIER_POINTER (id));
1418 if (! undeclared_variable_notice)
1420 error ("(Each undeclared identifier is reported only once");
1421 error ("for each function it appears in.)");
1422 undeclared_variable_notice = 1;
1425 IDENTIFIER_GLOBAL_VALUE (id) = error_mark_node;
1426 IDENTIFIER_ERROR_LOCUS (id) = current_function_decl;
1428 return error_mark_node;
1431 else
1433 /* Properly declared variable or function reference. */
1434 if (!objc_ivar)
1435 ref = decl;
1436 else if (decl != objc_ivar && IDENTIFIER_LOCAL_VALUE (id))
1438 warning ("local declaration of `%s' hides instance variable",
1439 IDENTIFIER_POINTER (id));
1440 ref = decl;
1442 else
1443 ref = objc_ivar;
1446 if (TREE_TYPE (ref) == error_mark_node)
1447 return error_mark_node;
1449 assemble_external (ref);
1450 TREE_USED (ref) = 1;
1452 if (TREE_CODE (ref) == CONST_DECL)
1454 ref = DECL_INITIAL (ref);
1455 TREE_CONSTANT (ref) = 1;
1458 return ref;
1461 /* Build a function call to function FUNCTION with parameters PARAMS.
1462 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1463 TREE_VALUE of each node is a parameter-expression.
1464 FUNCTION's data type may be a function type or a pointer-to-function. */
1466 tree
1467 build_function_call (function, params)
1468 tree function, params;
1470 tree fntype, fundecl = 0;
1471 tree coerced_params;
1472 tree name = NULL_TREE, assembler_name = NULL_TREE, result;
1474 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1475 STRIP_TYPE_NOPS (function);
1477 /* Convert anything with function type to a pointer-to-function. */
1478 if (TREE_CODE (function) == FUNCTION_DECL)
1480 name = DECL_NAME (function);
1481 assembler_name = DECL_ASSEMBLER_NAME (function);
1483 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1484 (because calling an inline function does not mean the function
1485 needs to be separately compiled). */
1486 fntype = build_type_variant (TREE_TYPE (function),
1487 TREE_READONLY (function),
1488 TREE_THIS_VOLATILE (function));
1489 fundecl = function;
1490 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1492 else
1493 function = default_conversion (function);
1495 fntype = TREE_TYPE (function);
1497 if (TREE_CODE (fntype) == ERROR_MARK)
1498 return error_mark_node;
1500 if (!(TREE_CODE (fntype) == POINTER_TYPE
1501 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1503 error ("called object is not a function");
1504 return error_mark_node;
1507 /* fntype now gets the type of function pointed to. */
1508 fntype = TREE_TYPE (fntype);
1510 /* Convert the parameters to the types declared in the
1511 function prototype, or apply default promotions. */
1513 coerced_params
1514 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1516 /* Check for errors in format strings. */
1518 if (warn_format)
1519 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
1521 /* Recognize certain built-in functions so we can make tree-codes
1522 other than CALL_EXPR. We do this when it enables fold-const.c
1523 to do something useful. */
1525 if (TREE_CODE (function) == ADDR_EXPR
1526 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1527 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1529 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1530 params, coerced_params);
1531 if (result)
1532 return result;
1535 result = build (CALL_EXPR, TREE_TYPE (fntype),
1536 function, coerced_params, NULL_TREE);
1537 TREE_SIDE_EFFECTS (result) = 1;
1538 result = fold (result);
1540 if (VOID_TYPE_P (TREE_TYPE (result)))
1541 return result;
1542 return require_complete_type (result);
1545 /* Convert the argument expressions in the list VALUES
1546 to the types in the list TYPELIST. The result is a list of converted
1547 argument expressions.
1549 If TYPELIST is exhausted, or when an element has NULL as its type,
1550 perform the default conversions.
1552 PARMLIST is the chain of parm decls for the function being called.
1553 It may be 0, if that info is not available.
1554 It is used only for generating error messages.
1556 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1558 This is also where warnings about wrong number of args are generated.
1560 Both VALUES and the returned value are chains of TREE_LIST nodes
1561 with the elements of the list in the TREE_VALUE slots of those nodes. */
1563 static tree
1564 convert_arguments (typelist, values, name, fundecl)
1565 tree typelist, values, name, fundecl;
1567 tree typetail, valtail;
1568 tree result = NULL;
1569 int parmnum;
1571 /* Scan the given expressions and types, producing individual
1572 converted arguments and pushing them on RESULT in reverse order. */
1574 for (valtail = values, typetail = typelist, parmnum = 0;
1575 valtail;
1576 valtail = TREE_CHAIN (valtail), parmnum++)
1578 tree type = typetail ? TREE_VALUE (typetail) : 0;
1579 tree val = TREE_VALUE (valtail);
1581 if (type == void_type_node)
1583 if (name)
1584 error ("too many arguments to function `%s'",
1585 IDENTIFIER_POINTER (name));
1586 else
1587 error ("too many arguments to function");
1588 break;
1591 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1592 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1593 to convert automatically to a pointer. */
1594 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1595 val = TREE_OPERAND (val, 0);
1597 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1598 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1599 val = default_conversion (val);
1601 val = require_complete_type (val);
1603 if (type != 0)
1605 /* Formal parm type is specified by a function prototype. */
1606 tree parmval;
1608 if (!COMPLETE_TYPE_P (type))
1610 error ("type of formal parameter %d is incomplete", parmnum + 1);
1611 parmval = val;
1613 else
1615 /* Optionally warn about conversions that
1616 differ from the default conversions. */
1617 if (warn_conversion || warn_traditional)
1619 int formal_prec = TYPE_PRECISION (type);
1621 if (INTEGRAL_TYPE_P (type)
1622 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1623 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1624 if (INTEGRAL_TYPE_P (type)
1625 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1626 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1627 else if (TREE_CODE (type) == COMPLEX_TYPE
1628 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1629 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1630 else if (TREE_CODE (type) == REAL_TYPE
1631 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1632 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1633 else if (TREE_CODE (type) == COMPLEX_TYPE
1634 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1635 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1636 else if (TREE_CODE (type) == REAL_TYPE
1637 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1638 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1639 /* ??? At some point, messages should be written about
1640 conversions between complex types, but that's too messy
1641 to do now. */
1642 else if (TREE_CODE (type) == REAL_TYPE
1643 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1645 /* Warn if any argument is passed as `float',
1646 since without a prototype it would be `double'. */
1647 if (formal_prec == TYPE_PRECISION (float_type_node))
1648 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1650 /* Detect integer changing in width or signedness.
1651 These warnings are only activated with
1652 -Wconversion, not with -Wtraditional. */
1653 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1654 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1656 tree would_have_been = default_conversion (val);
1657 tree type1 = TREE_TYPE (would_have_been);
1659 if (TREE_CODE (type) == ENUMERAL_TYPE
1660 && (TYPE_MAIN_VARIANT (type)
1661 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1662 /* No warning if function asks for enum
1663 and the actual arg is that enum type. */
1665 else if (formal_prec != TYPE_PRECISION (type1))
1666 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1667 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1669 /* Don't complain if the formal parameter type
1670 is an enum, because we can't tell now whether
1671 the value was an enum--even the same enum. */
1672 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1674 else if (TREE_CODE (val) == INTEGER_CST
1675 && int_fits_type_p (val, type))
1676 /* Change in signedness doesn't matter
1677 if a constant value is unaffected. */
1679 /* Likewise for a constant in a NOP_EXPR. */
1680 else if (TREE_CODE (val) == NOP_EXPR
1681 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1682 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1684 #if 0 /* We never get such tree structure here. */
1685 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1686 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1687 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1688 /* Change in signedness doesn't matter
1689 if an enum value is unaffected. */
1691 #endif
1692 /* If the value is extended from a narrower
1693 unsigned type, it doesn't matter whether we
1694 pass it as signed or unsigned; the value
1695 certainly is the same either way. */
1696 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1697 && TREE_UNSIGNED (TREE_TYPE (val)))
1699 else if (TREE_UNSIGNED (type))
1700 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1701 else
1702 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1706 parmval = convert_for_assignment (type, val,
1707 (char *) 0, /* arg passing */
1708 fundecl, name, parmnum + 1);
1710 if (PROMOTE_PROTOTYPES
1711 && INTEGRAL_TYPE_P (type)
1712 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1713 parmval = default_conversion (parmval);
1715 result = tree_cons (NULL_TREE, parmval, result);
1717 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1718 && (TYPE_PRECISION (TREE_TYPE (val))
1719 < TYPE_PRECISION (double_type_node)))
1720 /* Convert `float' to `double'. */
1721 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1722 else
1723 /* Convert `short' and `char' to full-size `int'. */
1724 result = tree_cons (NULL_TREE, default_conversion (val), result);
1726 if (typetail)
1727 typetail = TREE_CHAIN (typetail);
1730 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1732 if (name)
1733 error ("too few arguments to function `%s'",
1734 IDENTIFIER_POINTER (name));
1735 else
1736 error ("too few arguments to function");
1739 return nreverse (result);
1742 /* This is the entry point used by the parser
1743 for binary operators in the input.
1744 In addition to constructing the expression,
1745 we check for operands that were written with other binary operators
1746 in a way that is likely to confuse the user. */
1748 tree
1749 parser_build_binary_op (code, arg1, arg2)
1750 enum tree_code code;
1751 tree arg1, arg2;
1753 tree result = build_binary_op (code, arg1, arg2, 1);
1755 char class;
1756 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1757 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1758 enum tree_code code1 = ERROR_MARK;
1759 enum tree_code code2 = ERROR_MARK;
1761 if (IS_EXPR_CODE_CLASS (class1))
1762 code1 = C_EXP_ORIGINAL_CODE (arg1);
1763 if (IS_EXPR_CODE_CLASS (class2))
1764 code2 = C_EXP_ORIGINAL_CODE (arg2);
1766 /* Check for cases such as x+y<<z which users are likely
1767 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1768 is cleared to prevent these warnings. */
1769 if (warn_parentheses)
1771 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1773 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1774 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1775 warning ("suggest parentheses around + or - inside shift");
1778 if (code == TRUTH_ORIF_EXPR)
1780 if (code1 == TRUTH_ANDIF_EXPR
1781 || code2 == TRUTH_ANDIF_EXPR)
1782 warning ("suggest parentheses around && within ||");
1785 if (code == BIT_IOR_EXPR)
1787 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1788 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1789 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1790 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1791 warning ("suggest parentheses around arithmetic in operand of |");
1792 /* Check cases like x|y==z */
1793 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1794 warning ("suggest parentheses around comparison in operand of |");
1797 if (code == BIT_XOR_EXPR)
1799 if (code1 == BIT_AND_EXPR
1800 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1801 || code2 == BIT_AND_EXPR
1802 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1803 warning ("suggest parentheses around arithmetic in operand of ^");
1804 /* Check cases like x^y==z */
1805 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1806 warning ("suggest parentheses around comparison in operand of ^");
1809 if (code == BIT_AND_EXPR)
1811 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1812 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1813 warning ("suggest parentheses around + or - in operand of &");
1814 /* Check cases like x&y==z */
1815 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1816 warning ("suggest parentheses around comparison in operand of &");
1820 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1821 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1822 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1823 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1825 unsigned_conversion_warning (result, arg1);
1826 unsigned_conversion_warning (result, arg2);
1827 overflow_warning (result);
1829 class = TREE_CODE_CLASS (TREE_CODE (result));
1831 /* Record the code that was specified in the source,
1832 for the sake of warnings about confusing nesting. */
1833 if (IS_EXPR_CODE_CLASS (class))
1834 C_SET_EXP_ORIGINAL_CODE (result, code);
1835 else
1837 int flag = TREE_CONSTANT (result);
1838 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1839 so that convert_for_assignment wouldn't strip it.
1840 That way, we got warnings for things like p = (1 - 1).
1841 But it turns out we should not get those warnings. */
1842 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1843 C_SET_EXP_ORIGINAL_CODE (result, code);
1844 TREE_CONSTANT (result) = flag;
1847 return result;
1850 /* Build a binary-operation expression without default conversions.
1851 CODE is the kind of expression to build.
1852 This function differs from `build' in several ways:
1853 the data type of the result is computed and recorded in it,
1854 warnings are generated if arg data types are invalid,
1855 special handling for addition and subtraction of pointers is known,
1856 and some optimization is done (operations on narrow ints
1857 are done in the narrower type when that gives the same result).
1858 Constant folding is also done before the result is returned.
1860 Note that the operands will never have enumeral types, or function
1861 or array types, because either they will have the default conversions
1862 performed or they have both just been converted to some other type in which
1863 the arithmetic is to be done. */
1865 tree
1866 build_binary_op (code, orig_op0, orig_op1, convert_p)
1867 enum tree_code code;
1868 tree orig_op0, orig_op1;
1869 int convert_p;
1871 tree type0, type1;
1872 enum tree_code code0, code1;
1873 tree op0, op1;
1875 /* Expression code to give to the expression when it is built.
1876 Normally this is CODE, which is what the caller asked for,
1877 but in some special cases we change it. */
1878 enum tree_code resultcode = code;
1880 /* Data type in which the computation is to be performed.
1881 In the simplest cases this is the common type of the arguments. */
1882 tree result_type = NULL;
1884 /* Nonzero means operands have already been type-converted
1885 in whatever way is necessary.
1886 Zero means they need to be converted to RESULT_TYPE. */
1887 int converted = 0;
1889 /* Nonzero means create the expression with this type, rather than
1890 RESULT_TYPE. */
1891 tree build_type = 0;
1893 /* Nonzero means after finally constructing the expression
1894 convert it to this type. */
1895 tree final_type = 0;
1897 /* Nonzero if this is an operation like MIN or MAX which can
1898 safely be computed in short if both args are promoted shorts.
1899 Also implies COMMON.
1900 -1 indicates a bitwise operation; this makes a difference
1901 in the exact conditions for when it is safe to do the operation
1902 in a narrower mode. */
1903 int shorten = 0;
1905 /* Nonzero if this is a comparison operation;
1906 if both args are promoted shorts, compare the original shorts.
1907 Also implies COMMON. */
1908 int short_compare = 0;
1910 /* Nonzero if this is a right-shift operation, which can be computed on the
1911 original short and then promoted if the operand is a promoted short. */
1912 int short_shift = 0;
1914 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1915 int common = 0;
1917 if (convert_p)
1919 op0 = default_conversion (orig_op0);
1920 op1 = default_conversion (orig_op1);
1922 else
1924 op0 = orig_op0;
1925 op1 = orig_op1;
1928 type0 = TREE_TYPE (op0);
1929 type1 = TREE_TYPE (op1);
1931 /* The expression codes of the data types of the arguments tell us
1932 whether the arguments are integers, floating, pointers, etc. */
1933 code0 = TREE_CODE (type0);
1934 code1 = TREE_CODE (type1);
1936 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1937 STRIP_TYPE_NOPS (op0);
1938 STRIP_TYPE_NOPS (op1);
1940 /* If an error was already reported for one of the arguments,
1941 avoid reporting another error. */
1943 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1944 return error_mark_node;
1946 switch (code)
1948 case PLUS_EXPR:
1949 /* Handle the pointer + int case. */
1950 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1951 return pointer_int_sum (PLUS_EXPR, op0, op1);
1952 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1953 return pointer_int_sum (PLUS_EXPR, op1, op0);
1954 else
1955 common = 1;
1956 break;
1958 case MINUS_EXPR:
1959 /* Subtraction of two similar pointers.
1960 We must subtract them as integers, then divide by object size. */
1961 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1962 && comp_target_types (type0, type1))
1963 return pointer_diff (op0, op1);
1964 /* Handle pointer minus int. Just like pointer plus int. */
1965 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1966 return pointer_int_sum (MINUS_EXPR, op0, op1);
1967 else
1968 common = 1;
1969 break;
1971 case MULT_EXPR:
1972 common = 1;
1973 break;
1975 case TRUNC_DIV_EXPR:
1976 case CEIL_DIV_EXPR:
1977 case FLOOR_DIV_EXPR:
1978 case ROUND_DIV_EXPR:
1979 case EXACT_DIV_EXPR:
1980 /* Floating point division by zero is a legitimate way to obtain
1981 infinities and NaNs. */
1982 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
1983 warning ("division by zero");
1985 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1986 || code0 == COMPLEX_TYPE)
1987 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1988 || code1 == COMPLEX_TYPE))
1990 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
1991 resultcode = RDIV_EXPR;
1992 else
1993 /* Although it would be tempting to shorten always here, that
1994 loses on some targets, since the modulo instruction is
1995 undefined if the quotient can't be represented in the
1996 computation mode. We shorten only if unsigned or if
1997 dividing by something we know != -1. */
1998 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
1999 || (TREE_CODE (op1) == INTEGER_CST
2000 && ! integer_all_onesp (op1)));
2001 common = 1;
2003 break;
2005 case BIT_AND_EXPR:
2006 case BIT_ANDTC_EXPR:
2007 case BIT_IOR_EXPR:
2008 case BIT_XOR_EXPR:
2009 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2010 shorten = -1;
2011 /* If one operand is a constant, and the other is a short type
2012 that has been converted to an int,
2013 really do the work in the short type and then convert the
2014 result to int. If we are lucky, the constant will be 0 or 1
2015 in the short type, making the entire operation go away. */
2016 if (TREE_CODE (op0) == INTEGER_CST
2017 && TREE_CODE (op1) == NOP_EXPR
2018 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2019 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2021 final_type = result_type;
2022 op1 = TREE_OPERAND (op1, 0);
2023 result_type = TREE_TYPE (op1);
2025 if (TREE_CODE (op1) == INTEGER_CST
2026 && TREE_CODE (op0) == NOP_EXPR
2027 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2028 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2030 final_type = result_type;
2031 op0 = TREE_OPERAND (op0, 0);
2032 result_type = TREE_TYPE (op0);
2034 break;
2036 case TRUNC_MOD_EXPR:
2037 case FLOOR_MOD_EXPR:
2038 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2039 warning ("division by zero");
2041 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2043 /* Although it would be tempting to shorten always here, that loses
2044 on some targets, since the modulo instruction is undefined if the
2045 quotient can't be represented in the computation mode. We shorten
2046 only if unsigned or if dividing by something we know != -1. */
2047 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2048 || (TREE_CODE (op1) == INTEGER_CST
2049 && ! integer_all_onesp (op1)));
2050 common = 1;
2052 break;
2054 case TRUTH_ANDIF_EXPR:
2055 case TRUTH_ORIF_EXPR:
2056 case TRUTH_AND_EXPR:
2057 case TRUTH_OR_EXPR:
2058 case TRUTH_XOR_EXPR:
2059 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2060 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2061 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2062 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2064 /* Result of these operations is always an int,
2065 but that does not mean the operands should be
2066 converted to ints! */
2067 result_type = integer_type_node;
2068 op0 = truthvalue_conversion (op0);
2069 op1 = truthvalue_conversion (op1);
2070 converted = 1;
2072 break;
2074 /* Shift operations: result has same type as first operand;
2075 always convert second operand to int.
2076 Also set SHORT_SHIFT if shifting rightward. */
2078 case RSHIFT_EXPR:
2079 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2081 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2083 if (tree_int_cst_sgn (op1) < 0)
2084 warning ("right shift count is negative");
2085 else
2087 if (! integer_zerop (op1))
2088 short_shift = 1;
2090 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2091 warning ("right shift count >= width of type");
2095 /* Use the type of the value to be shifted.
2096 This is what most traditional C compilers do. */
2097 result_type = type0;
2098 /* Unless traditional, convert the shift-count to an integer,
2099 regardless of size of value being shifted. */
2100 if (! flag_traditional)
2102 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2103 op1 = convert (integer_type_node, op1);
2104 /* Avoid converting op1 to result_type later. */
2105 converted = 1;
2108 break;
2110 case LSHIFT_EXPR:
2111 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2113 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2115 if (tree_int_cst_sgn (op1) < 0)
2116 warning ("left shift count is negative");
2118 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2119 warning ("left shift count >= width of type");
2122 /* Use the type of the value to be shifted.
2123 This is what most traditional C compilers do. */
2124 result_type = type0;
2125 /* Unless traditional, convert the shift-count to an integer,
2126 regardless of size of value being shifted. */
2127 if (! flag_traditional)
2129 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2130 op1 = convert (integer_type_node, op1);
2131 /* Avoid converting op1 to result_type later. */
2132 converted = 1;
2135 break;
2137 case RROTATE_EXPR:
2138 case LROTATE_EXPR:
2139 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2141 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2143 if (tree_int_cst_sgn (op1) < 0)
2144 warning ("shift count is negative");
2145 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2146 warning ("shift count >= width of type");
2149 /* Use the type of the value to be shifted.
2150 This is what most traditional C compilers do. */
2151 result_type = type0;
2152 /* Unless traditional, convert the shift-count to an integer,
2153 regardless of size of value being shifted. */
2154 if (! flag_traditional)
2156 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2157 op1 = convert (integer_type_node, op1);
2158 /* Avoid converting op1 to result_type later. */
2159 converted = 1;
2162 break;
2164 case EQ_EXPR:
2165 case NE_EXPR:
2166 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2167 warning ("comparing floating point with == or != is unsafe");
2168 /* Result of comparison is always int,
2169 but don't convert the args to int! */
2170 build_type = integer_type_node;
2171 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2172 || code0 == COMPLEX_TYPE)
2173 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2174 || code1 == COMPLEX_TYPE))
2175 short_compare = 1;
2176 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2178 tree tt0 = TREE_TYPE (type0);
2179 tree tt1 = TREE_TYPE (type1);
2180 /* Anything compares with void *. void * compares with anything.
2181 Otherwise, the targets must be compatible
2182 and both must be object or both incomplete. */
2183 if (comp_target_types (type0, type1))
2184 result_type = common_type (type0, type1);
2185 else if (VOID_TYPE_P (tt0))
2187 /* op0 != orig_op0 detects the case of something
2188 whose value is 0 but which isn't a valid null ptr const. */
2189 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2190 && TREE_CODE (tt1) == FUNCTION_TYPE)
2191 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2193 else if (VOID_TYPE_P (tt1))
2195 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2196 && TREE_CODE (tt0) == FUNCTION_TYPE)
2197 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2199 else
2200 pedwarn ("comparison of distinct pointer types lacks a cast");
2202 if (result_type == NULL_TREE)
2203 result_type = ptr_type_node;
2205 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2206 && integer_zerop (op1))
2207 result_type = type0;
2208 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2209 && integer_zerop (op0))
2210 result_type = type1;
2211 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2213 result_type = type0;
2214 if (! flag_traditional)
2215 pedwarn ("comparison between pointer and integer");
2217 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2219 result_type = type1;
2220 if (! flag_traditional)
2221 pedwarn ("comparison between pointer and integer");
2223 break;
2225 case MAX_EXPR:
2226 case MIN_EXPR:
2227 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2228 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2229 shorten = 1;
2230 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2232 if (comp_target_types (type0, type1))
2234 result_type = common_type (type0, type1);
2235 if (pedantic
2236 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2237 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2239 else
2241 result_type = ptr_type_node;
2242 pedwarn ("comparison of distinct pointer types lacks a cast");
2245 break;
2247 case LE_EXPR:
2248 case GE_EXPR:
2249 case LT_EXPR:
2250 case GT_EXPR:
2251 build_type = integer_type_node;
2252 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2253 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2254 short_compare = 1;
2255 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2257 if (comp_target_types (type0, type1))
2259 result_type = common_type (type0, type1);
2260 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
2261 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
2262 pedwarn ("comparison of complete and incomplete pointers");
2263 else if (pedantic
2264 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2265 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2267 else
2269 result_type = ptr_type_node;
2270 pedwarn ("comparison of distinct pointer types lacks a cast");
2273 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2274 && integer_zerop (op1))
2276 result_type = type0;
2277 if (pedantic || extra_warnings)
2278 pedwarn ("ordered comparison of pointer with integer zero");
2280 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2281 && integer_zerop (op0))
2283 result_type = type1;
2284 if (pedantic)
2285 pedwarn ("ordered comparison of pointer with integer zero");
2287 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2289 result_type = type0;
2290 if (! flag_traditional)
2291 pedwarn ("comparison between pointer and integer");
2293 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2295 result_type = type1;
2296 if (! flag_traditional)
2297 pedwarn ("comparison between pointer and integer");
2299 break;
2301 case UNORDERED_EXPR:
2302 case ORDERED_EXPR:
2303 case UNLT_EXPR:
2304 case UNLE_EXPR:
2305 case UNGT_EXPR:
2306 case UNGE_EXPR:
2307 case UNEQ_EXPR:
2308 build_type = integer_type_node;
2309 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2311 error ("unordered comparison on non-floating point argument");
2312 return error_mark_node;
2314 common = 1;
2315 break;
2317 default:
2318 break;
2321 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2323 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2325 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2327 if (shorten || common || short_compare)
2328 result_type = common_type (type0, type1);
2330 /* For certain operations (which identify themselves by shorten != 0)
2331 if both args were extended from the same smaller type,
2332 do the arithmetic in that type and then extend.
2334 shorten !=0 and !=1 indicates a bitwise operation.
2335 For them, this optimization is safe only if
2336 both args are zero-extended or both are sign-extended.
2337 Otherwise, we might change the result.
2338 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2339 but calculated in (unsigned short) it would be (unsigned short)-1. */
2341 if (shorten && none_complex)
2343 int unsigned0, unsigned1;
2344 tree arg0 = get_narrower (op0, &unsigned0);
2345 tree arg1 = get_narrower (op1, &unsigned1);
2346 /* UNS is 1 if the operation to be done is an unsigned one. */
2347 int uns = TREE_UNSIGNED (result_type);
2348 tree type;
2350 final_type = result_type;
2352 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2353 but it *requires* conversion to FINAL_TYPE. */
2355 if ((TYPE_PRECISION (TREE_TYPE (op0))
2356 == TYPE_PRECISION (TREE_TYPE (arg0)))
2357 && TREE_TYPE (op0) != final_type)
2358 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2359 if ((TYPE_PRECISION (TREE_TYPE (op1))
2360 == TYPE_PRECISION (TREE_TYPE (arg1)))
2361 && TREE_TYPE (op1) != final_type)
2362 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2364 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2366 /* For bitwise operations, signedness of nominal type
2367 does not matter. Consider only how operands were extended. */
2368 if (shorten == -1)
2369 uns = unsigned0;
2371 /* Note that in all three cases below we refrain from optimizing
2372 an unsigned operation on sign-extended args.
2373 That would not be valid. */
2375 /* Both args variable: if both extended in same way
2376 from same width, do it in that width.
2377 Do it unsigned if args were zero-extended. */
2378 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2379 < TYPE_PRECISION (result_type))
2380 && (TYPE_PRECISION (TREE_TYPE (arg1))
2381 == TYPE_PRECISION (TREE_TYPE (arg0)))
2382 && unsigned0 == unsigned1
2383 && (unsigned0 || !uns))
2384 result_type
2385 = signed_or_unsigned_type (unsigned0,
2386 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2387 else if (TREE_CODE (arg0) == INTEGER_CST
2388 && (unsigned1 || !uns)
2389 && (TYPE_PRECISION (TREE_TYPE (arg1))
2390 < TYPE_PRECISION (result_type))
2391 && (type = signed_or_unsigned_type (unsigned1,
2392 TREE_TYPE (arg1)),
2393 int_fits_type_p (arg0, type)))
2394 result_type = type;
2395 else if (TREE_CODE (arg1) == INTEGER_CST
2396 && (unsigned0 || !uns)
2397 && (TYPE_PRECISION (TREE_TYPE (arg0))
2398 < TYPE_PRECISION (result_type))
2399 && (type = signed_or_unsigned_type (unsigned0,
2400 TREE_TYPE (arg0)),
2401 int_fits_type_p (arg1, type)))
2402 result_type = type;
2405 /* Shifts can be shortened if shifting right. */
2407 if (short_shift)
2409 int unsigned_arg;
2410 tree arg0 = get_narrower (op0, &unsigned_arg);
2412 final_type = result_type;
2414 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2415 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2417 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2418 /* We can shorten only if the shift count is less than the
2419 number of bits in the smaller type size. */
2420 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
2421 /* We cannot drop an unsigned shift after sign-extension. */
2422 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
2424 /* Do an unsigned shift if the operand was zero-extended. */
2425 result_type
2426 = signed_or_unsigned_type (unsigned_arg, TREE_TYPE (arg0));
2427 /* Convert value-to-be-shifted to that type. */
2428 if (TREE_TYPE (op0) != result_type)
2429 op0 = convert (result_type, op0);
2430 converted = 1;
2434 /* Comparison operations are shortened too but differently.
2435 They identify themselves by setting short_compare = 1. */
2437 if (short_compare)
2439 /* Don't write &op0, etc., because that would prevent op0
2440 from being kept in a register.
2441 Instead, make copies of the our local variables and
2442 pass the copies by reference, then copy them back afterward. */
2443 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2444 enum tree_code xresultcode = resultcode;
2445 tree val
2446 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2448 if (val != 0)
2449 return val;
2451 op0 = xop0, op1 = xop1;
2452 converted = 1;
2453 resultcode = xresultcode;
2455 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2456 && skip_evaluation == 0)
2458 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2459 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2460 int unsignedp0, unsignedp1;
2461 tree primop0 = get_narrower (op0, &unsignedp0);
2462 tree primop1 = get_narrower (op1, &unsignedp1);
2464 xop0 = orig_op0;
2465 xop1 = orig_op1;
2466 STRIP_TYPE_NOPS (xop0);
2467 STRIP_TYPE_NOPS (xop1);
2469 /* Give warnings for comparisons between signed and unsigned
2470 quantities that may fail.
2472 Do the checking based on the original operand trees, so that
2473 casts will be considered, but default promotions won't be.
2475 Do not warn if the comparison is being done in a signed type,
2476 since the signed type will only be chosen if it can represent
2477 all the values of the unsigned type. */
2478 if (! TREE_UNSIGNED (result_type))
2479 /* OK */;
2480 /* Do not warn if both operands are the same signedness. */
2481 else if (op0_signed == op1_signed)
2482 /* OK */;
2483 else
2485 tree sop, uop;
2487 if (op0_signed)
2488 sop = xop0, uop = xop1;
2489 else
2490 sop = xop1, uop = xop0;
2492 /* Do not warn if the signed quantity is an
2493 unsuffixed integer literal (or some static
2494 constant expression involving such literals or a
2495 conditional expression involving such literals)
2496 and it is non-negative. */
2497 if (tree_expr_nonnegative_p (sop))
2498 /* OK */;
2499 /* Do not warn if the comparison is an equality operation,
2500 the unsigned quantity is an integral constant, and it
2501 would fit in the result if the result were signed. */
2502 else if (TREE_CODE (uop) == INTEGER_CST
2503 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2504 && int_fits_type_p (uop, signed_type (result_type)))
2505 /* OK */;
2506 /* Do not warn if the unsigned quantity is an enumeration
2507 constant and its maximum value would fit in the result
2508 if the result were signed. */
2509 else if (TREE_CODE (uop) == INTEGER_CST
2510 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2511 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2512 signed_type (result_type)))
2513 /* OK */;
2514 else
2515 warning ("comparison between signed and unsigned");
2518 /* Warn if two unsigned values are being compared in a size
2519 larger than their original size, and one (and only one) is the
2520 result of a `~' operator. This comparison will always fail.
2522 Also warn if one operand is a constant, and the constant
2523 does not have all bits set that are set in the ~ operand
2524 when it is extended. */
2526 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2527 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2529 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2530 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2531 &unsignedp0);
2532 else
2533 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2534 &unsignedp1);
2536 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
2538 tree primop;
2539 HOST_WIDE_INT constant, mask;
2540 int unsignedp, bits;
2542 if (host_integerp (primop0, 0))
2544 primop = primop1;
2545 unsignedp = unsignedp1;
2546 constant = tree_low_cst (primop0, 0);
2548 else
2550 primop = primop0;
2551 unsignedp = unsignedp0;
2552 constant = tree_low_cst (primop1, 0);
2555 bits = TYPE_PRECISION (TREE_TYPE (primop));
2556 if (bits < TYPE_PRECISION (result_type)
2557 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
2559 mask = (~ (HOST_WIDE_INT) 0) << bits;
2560 if ((mask & constant) != mask)
2561 warning ("comparison of promoted ~unsigned with constant");
2564 else if (unsignedp0 && unsignedp1
2565 && (TYPE_PRECISION (TREE_TYPE (primop0))
2566 < TYPE_PRECISION (result_type))
2567 && (TYPE_PRECISION (TREE_TYPE (primop1))
2568 < TYPE_PRECISION (result_type)))
2569 warning ("comparison of promoted ~unsigned with unsigned");
2575 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2576 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2577 Then the expression will be built.
2578 It will be given type FINAL_TYPE if that is nonzero;
2579 otherwise, it will be given type RESULT_TYPE. */
2581 if (!result_type)
2583 binary_op_error (code);
2584 return error_mark_node;
2587 if (! converted)
2589 if (TREE_TYPE (op0) != result_type)
2590 op0 = convert (result_type, op0);
2591 if (TREE_TYPE (op1) != result_type)
2592 op1 = convert (result_type, op1);
2595 if (build_type == NULL_TREE)
2596 build_type = result_type;
2599 tree result = build (resultcode, build_type, op0, op1);
2600 tree folded;
2602 folded = fold (result);
2603 if (folded == result)
2604 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2605 if (final_type != 0)
2606 return convert (final_type, folded);
2607 return folded;
2611 /* Return a tree for the sum or difference (RESULTCODE says which)
2612 of pointer PTROP and integer INTOP. */
2614 static tree
2615 pointer_int_sum (resultcode, ptrop, intop)
2616 enum tree_code resultcode;
2617 tree ptrop, intop;
2619 tree size_exp;
2621 tree result;
2622 tree folded;
2624 /* The result is a pointer of the same type that is being added. */
2626 tree result_type = TREE_TYPE (ptrop);
2628 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2630 if (pedantic || warn_pointer_arith)
2631 pedwarn ("pointer of type `void *' used in arithmetic");
2632 size_exp = integer_one_node;
2634 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2636 if (pedantic || warn_pointer_arith)
2637 pedwarn ("pointer to a function used in arithmetic");
2638 size_exp = integer_one_node;
2640 else
2641 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2643 /* If what we are about to multiply by the size of the elements
2644 contains a constant term, apply distributive law
2645 and multiply that constant term separately.
2646 This helps produce common subexpressions. */
2648 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2649 && ! TREE_CONSTANT (intop)
2650 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2651 && TREE_CONSTANT (size_exp)
2652 /* If the constant comes from pointer subtraction,
2653 skip this optimization--it would cause an error. */
2654 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2655 /* If the constant is unsigned, and smaller than the pointer size,
2656 then we must skip this optimization. This is because it could cause
2657 an overflow error if the constant is negative but INTOP is not. */
2658 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2659 || (TYPE_PRECISION (TREE_TYPE (intop))
2660 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2662 enum tree_code subcode = resultcode;
2663 tree int_type = TREE_TYPE (intop);
2664 if (TREE_CODE (intop) == MINUS_EXPR)
2665 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2666 /* Convert both subexpression types to the type of intop,
2667 because weird cases involving pointer arithmetic
2668 can result in a sum or difference with different type args. */
2669 ptrop = build_binary_op (subcode, ptrop,
2670 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2671 intop = convert (int_type, TREE_OPERAND (intop, 0));
2674 /* Convert the integer argument to a type the same size as sizetype
2675 so the multiply won't overflow spuriously. */
2677 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2678 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2679 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2680 TREE_UNSIGNED (sizetype)), intop);
2682 /* Replace the integer argument with a suitable product by the object size.
2683 Do this multiplication as signed, then convert to the appropriate
2684 pointer type (actually unsigned integral). */
2686 intop = convert (result_type,
2687 build_binary_op (MULT_EXPR, intop,
2688 convert (TREE_TYPE (intop), size_exp), 1));
2690 /* Create the sum or difference. */
2692 result = build (resultcode, result_type, ptrop, intop);
2694 folded = fold (result);
2695 if (folded == result)
2696 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2697 return folded;
2700 /* Return a tree for the difference of pointers OP0 and OP1.
2701 The resulting tree has type int. */
2703 static tree
2704 pointer_diff (op0, op1)
2705 tree op0, op1;
2707 tree result, folded;
2708 tree restype = ptrdiff_type_node;
2710 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2711 tree con0, con1, lit0, lit1;
2712 tree orig_op1 = op1;
2714 if (pedantic || warn_pointer_arith)
2716 if (TREE_CODE (target_type) == VOID_TYPE)
2717 pedwarn ("pointer of type `void *' used in subtraction");
2718 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2719 pedwarn ("pointer to a function used in subtraction");
2722 /* If the conversion to ptrdiff_type does anything like widening or
2723 converting a partial to an integral mode, we get a convert_expression
2724 that is in the way to do any simplifications.
2725 (fold-const.c doesn't know that the extra bits won't be needed.
2726 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2727 different mode in place.)
2728 So first try to find a common term here 'by hand'; we want to cover
2729 at least the cases that occur in legal static initializers. */
2730 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2731 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2733 if (TREE_CODE (con0) == PLUS_EXPR)
2735 lit0 = TREE_OPERAND (con0, 1);
2736 con0 = TREE_OPERAND (con0, 0);
2738 else
2739 lit0 = integer_zero_node;
2741 if (TREE_CODE (con1) == PLUS_EXPR)
2743 lit1 = TREE_OPERAND (con1, 1);
2744 con1 = TREE_OPERAND (con1, 0);
2746 else
2747 lit1 = integer_zero_node;
2749 if (operand_equal_p (con0, con1, 0))
2751 op0 = lit0;
2752 op1 = lit1;
2756 /* First do the subtraction as integers;
2757 then drop through to build the divide operator.
2758 Do not do default conversions on the minus operator
2759 in case restype is a short type. */
2761 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2762 convert (restype, op1), 0);
2763 /* This generates an error if op1 is pointer to incomplete type. */
2764 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2765 error ("arithmetic on pointer to an incomplete type");
2767 /* This generates an error if op0 is pointer to incomplete type. */
2768 op1 = c_size_in_bytes (target_type);
2770 /* Divide by the size, in easiest possible way. */
2772 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2774 folded = fold (result);
2775 if (folded == result)
2776 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2777 return folded;
2780 /* Construct and perhaps optimize a tree representation
2781 for a unary operation. CODE, a tree_code, specifies the operation
2782 and XARG is the operand. NOCONVERT nonzero suppresses
2783 the default promotions (such as from short to int). */
2785 tree
2786 build_unary_op (code, xarg, noconvert)
2787 enum tree_code code;
2788 tree xarg;
2789 int noconvert;
2791 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2792 tree arg = xarg;
2793 tree argtype = 0;
2794 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2795 tree val;
2797 if (typecode == ERROR_MARK)
2798 return error_mark_node;
2799 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2800 typecode = INTEGER_TYPE;
2802 switch (code)
2804 case CONVERT_EXPR:
2805 /* This is used for unary plus, because a CONVERT_EXPR
2806 is enough to prevent anybody from looking inside for
2807 associativity, but won't generate any code. */
2808 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2809 || typecode == COMPLEX_TYPE))
2811 error ("wrong type argument to unary plus");
2812 return error_mark_node;
2814 else if (!noconvert)
2815 arg = default_conversion (arg);
2816 break;
2818 case NEGATE_EXPR:
2819 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2820 || typecode == COMPLEX_TYPE))
2822 error ("wrong type argument to unary minus");
2823 return error_mark_node;
2825 else if (!noconvert)
2826 arg = default_conversion (arg);
2827 break;
2829 case BIT_NOT_EXPR:
2830 if (typecode == COMPLEX_TYPE)
2832 code = CONJ_EXPR;
2833 if (pedantic)
2834 pedwarn ("ISO C does not support `~' for complex conjugation");
2835 if (!noconvert)
2836 arg = default_conversion (arg);
2838 else if (typecode != INTEGER_TYPE)
2840 error ("wrong type argument to bit-complement");
2841 return error_mark_node;
2843 else if (!noconvert)
2844 arg = default_conversion (arg);
2845 break;
2847 case ABS_EXPR:
2848 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2849 || typecode == COMPLEX_TYPE))
2851 error ("wrong type argument to abs");
2852 return error_mark_node;
2854 else if (!noconvert)
2855 arg = default_conversion (arg);
2856 break;
2858 case CONJ_EXPR:
2859 /* Conjugating a real value is a no-op, but allow it anyway. */
2860 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2861 || typecode == COMPLEX_TYPE))
2863 error ("wrong type argument to conjugation");
2864 return error_mark_node;
2866 else if (!noconvert)
2867 arg = default_conversion (arg);
2868 break;
2870 case TRUTH_NOT_EXPR:
2871 if (typecode != INTEGER_TYPE
2872 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2873 && typecode != COMPLEX_TYPE
2874 /* These will convert to a pointer. */
2875 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2877 error ("wrong type argument to unary exclamation mark");
2878 return error_mark_node;
2880 arg = truthvalue_conversion (arg);
2881 return invert_truthvalue (arg);
2883 case NOP_EXPR:
2884 break;
2886 case REALPART_EXPR:
2887 if (TREE_CODE (arg) == COMPLEX_CST)
2888 return TREE_REALPART (arg);
2889 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2890 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2891 else
2892 return arg;
2894 case IMAGPART_EXPR:
2895 if (TREE_CODE (arg) == COMPLEX_CST)
2896 return TREE_IMAGPART (arg);
2897 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2898 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2899 else
2900 return convert (TREE_TYPE (arg), integer_zero_node);
2902 case PREINCREMENT_EXPR:
2903 case POSTINCREMENT_EXPR:
2904 case PREDECREMENT_EXPR:
2905 case POSTDECREMENT_EXPR:
2906 /* Handle complex lvalues (when permitted)
2907 by reduction to simpler cases. */
2909 val = unary_complex_lvalue (code, arg);
2910 if (val != 0)
2911 return val;
2913 /* Increment or decrement the real part of the value,
2914 and don't change the imaginary part. */
2915 if (typecode == COMPLEX_TYPE)
2917 tree real, imag;
2919 if (pedantic)
2920 pedwarn ("ISO C does not support `++' and `--' on complex types");
2922 arg = stabilize_reference (arg);
2923 real = build_unary_op (REALPART_EXPR, arg, 1);
2924 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2925 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2926 build_unary_op (code, real, 1), imag);
2929 /* Report invalid types. */
2931 if (typecode != POINTER_TYPE
2932 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2934 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2935 error ("wrong type argument to increment");
2936 else
2937 error ("wrong type argument to decrement");
2939 return error_mark_node;
2943 tree inc;
2944 tree result_type = TREE_TYPE (arg);
2946 arg = get_unwidened (arg, 0);
2947 argtype = TREE_TYPE (arg);
2949 /* Compute the increment. */
2951 if (typecode == POINTER_TYPE)
2953 /* If pointer target is an undefined struct,
2954 we just cannot know how to do the arithmetic. */
2955 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2957 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2958 error ("increment of pointer to unknown structure");
2959 else
2960 error ("decrement of pointer to unknown structure");
2962 else if ((pedantic || warn_pointer_arith)
2963 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2964 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2966 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2967 pedwarn ("wrong type argument to increment");
2968 else
2969 pedwarn ("wrong type argument to decrement");
2972 inc = c_size_in_bytes (TREE_TYPE (result_type));
2974 else
2975 inc = integer_one_node;
2977 inc = convert (argtype, inc);
2979 /* Handle incrementing a cast-expression. */
2981 while (1)
2982 switch (TREE_CODE (arg))
2984 case NOP_EXPR:
2985 case CONVERT_EXPR:
2986 case FLOAT_EXPR:
2987 case FIX_TRUNC_EXPR:
2988 case FIX_FLOOR_EXPR:
2989 case FIX_ROUND_EXPR:
2990 case FIX_CEIL_EXPR:
2991 pedantic_lvalue_warning (CONVERT_EXPR);
2992 /* If the real type has the same machine representation
2993 as the type it is cast to, we can make better output
2994 by adding directly to the inside of the cast. */
2995 if ((TREE_CODE (TREE_TYPE (arg))
2996 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2997 && (TYPE_MODE (TREE_TYPE (arg))
2998 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2999 arg = TREE_OPERAND (arg, 0);
3000 else
3002 tree incremented, modify, value;
3003 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3004 value = boolean_increment (code, arg);
3005 else
3007 arg = stabilize_reference (arg);
3008 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3009 value = arg;
3010 else
3011 value = save_expr (arg);
3012 incremented = build (((code == PREINCREMENT_EXPR
3013 || code == POSTINCREMENT_EXPR)
3014 ? PLUS_EXPR : MINUS_EXPR),
3015 argtype, value, inc);
3016 TREE_SIDE_EFFECTS (incremented) = 1;
3017 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3018 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3020 TREE_USED (value) = 1;
3021 return value;
3023 break;
3025 default:
3026 goto give_up;
3028 give_up:
3030 /* Complain about anything else that is not a true lvalue. */
3031 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3032 || code == POSTINCREMENT_EXPR)
3033 ? "invalid lvalue in increment"
3034 : "invalid lvalue in decrement")))
3035 return error_mark_node;
3037 /* Report a read-only lvalue. */
3038 if (TREE_READONLY (arg))
3039 readonly_warning (arg,
3040 ((code == PREINCREMENT_EXPR
3041 || code == POSTINCREMENT_EXPR)
3042 ? _("increment") : _("decrement")));
3044 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3045 val = boolean_increment (code, arg);
3046 else
3047 val = build (code, TREE_TYPE (arg), arg, inc);
3048 TREE_SIDE_EFFECTS (val) = 1;
3049 val = convert (result_type, val);
3050 if (TREE_CODE (val) != code)
3051 TREE_NO_UNUSED_WARNING (val) = 1;
3052 return val;
3055 case ADDR_EXPR:
3056 /* Note that this operation never does default_conversion
3057 regardless of NOCONVERT. */
3059 /* Let &* cancel out to simplify resulting code. */
3060 if (TREE_CODE (arg) == INDIRECT_REF)
3062 /* Don't let this be an lvalue. */
3063 if (lvalue_p (TREE_OPERAND (arg, 0)))
3064 return non_lvalue (TREE_OPERAND (arg, 0));
3065 return TREE_OPERAND (arg, 0);
3068 /* For &x[y], return x+y */
3069 if (TREE_CODE (arg) == ARRAY_REF)
3071 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
3072 return error_mark_node;
3073 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3074 TREE_OPERAND (arg, 1), 1);
3077 /* Handle complex lvalues (when permitted)
3078 by reduction to simpler cases. */
3079 val = unary_complex_lvalue (code, arg);
3080 if (val != 0)
3081 return val;
3083 #if 0 /* Turned off because inconsistent;
3084 float f; *&(int)f = 3.4 stores in int format
3085 whereas (int)f = 3.4 stores in float format. */
3086 /* Address of a cast is just a cast of the address
3087 of the operand of the cast. */
3088 switch (TREE_CODE (arg))
3090 case NOP_EXPR:
3091 case CONVERT_EXPR:
3092 case FLOAT_EXPR:
3093 case FIX_TRUNC_EXPR:
3094 case FIX_FLOOR_EXPR:
3095 case FIX_ROUND_EXPR:
3096 case FIX_CEIL_EXPR:
3097 if (pedantic)
3098 pedwarn ("ISO C forbids the address of a cast expression");
3099 return convert (build_pointer_type (TREE_TYPE (arg)),
3100 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3101 0));
3103 #endif
3105 /* Allow the address of a constructor if all the elements
3106 are constant. */
3107 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3109 /* Anything not already handled and not a true memory reference
3110 is an error. */
3111 else if (typecode != FUNCTION_TYPE
3112 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
3113 return error_mark_node;
3115 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3116 argtype = TREE_TYPE (arg);
3118 /* If the lvalue is const or volatile, merge that into the type
3119 to which the address will point. Note that you can't get a
3120 restricted pointer by taking the address of something, so we
3121 only have to deal with `const' and `volatile' here. */
3122 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3123 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3124 argtype = c_build_type_variant (argtype,
3125 TREE_READONLY (arg),
3126 TREE_THIS_VOLATILE (arg));
3128 argtype = build_pointer_type (argtype);
3130 if (mark_addressable (arg) == 0)
3131 return error_mark_node;
3134 tree addr;
3136 if (TREE_CODE (arg) == COMPONENT_REF)
3138 tree field = TREE_OPERAND (arg, 1);
3140 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3142 if (DECL_C_BIT_FIELD (field))
3144 error ("attempt to take address of bit-field structure member `%s'",
3145 IDENTIFIER_POINTER (DECL_NAME (field)));
3146 return error_mark_node;
3149 addr = fold (build (PLUS_EXPR, argtype,
3150 convert (argtype, addr),
3151 convert (argtype, byte_position (field))));
3153 else
3154 addr = build1 (code, argtype, arg);
3156 /* Address of a static or external variable or
3157 file-scope function counts as a constant. */
3158 if (staticp (arg)
3159 && ! (TREE_CODE (arg) == FUNCTION_DECL
3160 && DECL_CONTEXT (arg) != 0))
3161 TREE_CONSTANT (addr) = 1;
3162 return addr;
3165 default:
3166 break;
3169 if (argtype == 0)
3170 argtype = TREE_TYPE (arg);
3171 return fold (build1 (code, argtype, arg));
3174 #if 0
3175 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3176 convert ARG with the same conversions in the same order
3177 and return the result. */
3179 static tree
3180 convert_sequence (conversions, arg)
3181 tree conversions;
3182 tree arg;
3184 switch (TREE_CODE (conversions))
3186 case NOP_EXPR:
3187 case CONVERT_EXPR:
3188 case FLOAT_EXPR:
3189 case FIX_TRUNC_EXPR:
3190 case FIX_FLOOR_EXPR:
3191 case FIX_ROUND_EXPR:
3192 case FIX_CEIL_EXPR:
3193 return convert (TREE_TYPE (conversions),
3194 convert_sequence (TREE_OPERAND (conversions, 0),
3195 arg));
3197 default:
3198 return arg;
3201 #endif /* 0 */
3203 /* Return nonzero if REF is an lvalue valid for this language.
3204 Lvalues can be assigned, unless their type has TYPE_READONLY.
3205 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3208 lvalue_p (ref)
3209 tree ref;
3211 enum tree_code code = TREE_CODE (ref);
3213 switch (code)
3215 case REALPART_EXPR:
3216 case IMAGPART_EXPR:
3217 case COMPONENT_REF:
3218 return lvalue_p (TREE_OPERAND (ref, 0));
3220 case STRING_CST:
3221 return 1;
3223 case INDIRECT_REF:
3224 case ARRAY_REF:
3225 case VAR_DECL:
3226 case PARM_DECL:
3227 case RESULT_DECL:
3228 case ERROR_MARK:
3229 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3230 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3232 case BIND_EXPR:
3233 case RTL_EXPR:
3234 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3236 default:
3237 return 0;
3241 /* Return nonzero if REF is an lvalue valid for this language;
3242 otherwise, print an error message and return zero. */
3245 lvalue_or_else (ref, msgid)
3246 tree ref;
3247 const char *msgid;
3249 int win = lvalue_p (ref);
3251 if (! win)
3252 error ("%s", msgid);
3254 return win;
3257 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3258 for certain kinds of expressions which are not really lvalues
3259 but which we can accept as lvalues.
3261 If ARG is not a kind of expression we can handle, return zero. */
3263 static tree
3264 unary_complex_lvalue (code, arg)
3265 enum tree_code code;
3266 tree arg;
3268 /* Handle (a, b) used as an "lvalue". */
3269 if (TREE_CODE (arg) == COMPOUND_EXPR)
3271 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3273 /* If this returns a function type, it isn't really being used as
3274 an lvalue, so don't issue a warning about it. */
3275 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3276 pedantic_lvalue_warning (COMPOUND_EXPR);
3278 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3279 TREE_OPERAND (arg, 0), real_result);
3282 /* Handle (a ? b : c) used as an "lvalue". */
3283 if (TREE_CODE (arg) == COND_EXPR)
3285 pedantic_lvalue_warning (COND_EXPR);
3286 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3287 pedantic_lvalue_warning (COMPOUND_EXPR);
3289 return (build_conditional_expr
3290 (TREE_OPERAND (arg, 0),
3291 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3292 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3295 return 0;
3298 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3299 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3301 static void
3302 pedantic_lvalue_warning (code)
3303 enum tree_code code;
3305 if (pedantic)
3306 switch (code)
3308 case COND_EXPR:
3309 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
3310 break;
3311 case COMPOUND_EXPR:
3312 pedwarn ("ISO C forbids use of compound expressions as lvalues");
3313 break;
3314 default:
3315 pedwarn ("ISO C forbids use of cast expressions as lvalues");
3316 break;
3320 /* Warn about storing in something that is `const'. */
3322 void
3323 readonly_warning (arg, msgid)
3324 tree arg;
3325 const char *msgid;
3327 if (TREE_CODE (arg) == COMPONENT_REF)
3329 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3330 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3331 else
3332 pedwarn ("%s of read-only member `%s'", _(msgid),
3333 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3335 else if (TREE_CODE (arg) == VAR_DECL)
3336 pedwarn ("%s of read-only variable `%s'", _(msgid),
3337 IDENTIFIER_POINTER (DECL_NAME (arg)));
3338 else
3339 pedwarn ("%s of read-only location", _(msgid));
3342 /* Mark EXP saying that we need to be able to take the
3343 address of it; it should not be allocated in a register.
3344 Value is 1 if successful. */
3347 mark_addressable (exp)
3348 tree exp;
3350 tree x = exp;
3351 while (1)
3352 switch (TREE_CODE (x))
3354 case COMPONENT_REF:
3355 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3357 error ("cannot take address of bitfield `%s'",
3358 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3359 return 0;
3362 /* ... fall through ... */
3364 case ADDR_EXPR:
3365 case ARRAY_REF:
3366 case REALPART_EXPR:
3367 case IMAGPART_EXPR:
3368 x = TREE_OPERAND (x, 0);
3369 break;
3371 case CONSTRUCTOR:
3372 TREE_ADDRESSABLE (x) = 1;
3373 return 1;
3375 case VAR_DECL:
3376 case CONST_DECL:
3377 case PARM_DECL:
3378 case RESULT_DECL:
3379 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3380 && DECL_NONLOCAL (x))
3382 if (TREE_PUBLIC (x))
3384 error ("global register variable `%s' used in nested function",
3385 IDENTIFIER_POINTER (DECL_NAME (x)));
3386 return 0;
3388 pedwarn ("register variable `%s' used in nested function",
3389 IDENTIFIER_POINTER (DECL_NAME (x)));
3391 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3393 if (TREE_PUBLIC (x))
3395 error ("address of global register variable `%s' requested",
3396 IDENTIFIER_POINTER (DECL_NAME (x)));
3397 return 0;
3400 /* If we are making this addressable due to its having
3401 volatile components, give a different error message. Also
3402 handle the case of an unnamed parameter by not trying
3403 to give the name. */
3405 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3407 error ("cannot put object with volatile field into register");
3408 return 0;
3411 pedwarn ("address of register variable `%s' requested",
3412 IDENTIFIER_POINTER (DECL_NAME (x)));
3414 put_var_into_stack (x);
3416 /* drops in */
3417 case FUNCTION_DECL:
3418 TREE_ADDRESSABLE (x) = 1;
3419 #if 0 /* poplevel deals with this now. */
3420 if (DECL_CONTEXT (x) == 0)
3421 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3422 #endif
3424 default:
3425 return 1;
3429 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3431 tree
3432 build_conditional_expr (ifexp, op1, op2)
3433 tree ifexp, op1, op2;
3435 tree type1;
3436 tree type2;
3437 enum tree_code code1;
3438 enum tree_code code2;
3439 tree result_type = NULL;
3440 tree orig_op1 = op1, orig_op2 = op2;
3442 ifexp = truthvalue_conversion (default_conversion (ifexp));
3444 #if 0 /* Produces wrong result if within sizeof. */
3445 /* Don't promote the operands separately if they promote
3446 the same way. Return the unpromoted type and let the combined
3447 value get promoted if necessary. */
3449 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3450 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3451 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3452 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3454 if (TREE_CODE (ifexp) == INTEGER_CST)
3455 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3457 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3459 #endif
3461 /* Promote both alternatives. */
3463 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3464 op1 = default_conversion (op1);
3465 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3466 op2 = default_conversion (op2);
3468 if (TREE_CODE (ifexp) == ERROR_MARK
3469 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3470 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3471 return error_mark_node;
3473 type1 = TREE_TYPE (op1);
3474 code1 = TREE_CODE (type1);
3475 type2 = TREE_TYPE (op2);
3476 code2 = TREE_CODE (type2);
3478 /* Quickly detect the usual case where op1 and op2 have the same type
3479 after promotion. */
3480 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3482 if (type1 == type2)
3483 result_type = type1;
3484 else
3485 result_type = TYPE_MAIN_VARIANT (type1);
3487 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3488 || code1 == COMPLEX_TYPE)
3489 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3490 || code2 == COMPLEX_TYPE))
3492 result_type = common_type (type1, type2);
3494 /* If -Wsign-compare, warn here if type1 and type2 have
3495 different signedness. We'll promote the signed to unsigned
3496 and later code won't know it used to be different.
3497 Do this check on the original types, so that explicit casts
3498 will be considered, but default promotions won't. */
3499 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare)
3500 && !skip_evaluation)
3502 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3503 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3505 if (unsigned_op1 ^ unsigned_op2)
3507 /* Do not warn if the result type is signed, since the
3508 signed type will only be chosen if it can represent
3509 all the values of the unsigned type. */
3510 if (! TREE_UNSIGNED (result_type))
3511 /* OK */;
3512 /* Do not warn if the signed quantity is an unsuffixed
3513 integer literal (or some static constant expression
3514 involving such literals) and it is non-negative. */
3515 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3516 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3517 /* OK */;
3518 else
3519 warning ("signed and unsigned type in conditional expression");
3523 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3525 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3526 pedwarn ("ISO C forbids conditional expr with only one void side");
3527 result_type = void_type_node;
3529 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3531 if (comp_target_types (type1, type2))
3532 result_type = common_type (type1, type2);
3533 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3534 && TREE_CODE (orig_op1) != NOP_EXPR)
3535 result_type = qualify_type (type2, type1);
3536 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3537 && TREE_CODE (orig_op2) != NOP_EXPR)
3538 result_type = qualify_type (type1, type2);
3539 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3541 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3542 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3543 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3544 TREE_TYPE (type2)));
3546 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3548 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3549 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3550 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3551 TREE_TYPE (type1)));
3553 else
3555 pedwarn ("pointer type mismatch in conditional expression");
3556 result_type = build_pointer_type (void_type_node);
3559 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3561 if (! integer_zerop (op2))
3562 pedwarn ("pointer/integer type mismatch in conditional expression");
3563 else
3565 op2 = null_pointer_node;
3567 result_type = type1;
3569 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3571 if (!integer_zerop (op1))
3572 pedwarn ("pointer/integer type mismatch in conditional expression");
3573 else
3575 op1 = null_pointer_node;
3577 result_type = type2;
3580 if (!result_type)
3582 if (flag_cond_mismatch)
3583 result_type = void_type_node;
3584 else
3586 error ("type mismatch in conditional expression");
3587 return error_mark_node;
3591 /* Merge const and volatile flags of the incoming types. */
3592 result_type
3593 = build_type_variant (result_type,
3594 TREE_READONLY (op1) || TREE_READONLY (op2),
3595 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3597 if (result_type != TREE_TYPE (op1))
3598 op1 = convert_and_check (result_type, op1);
3599 if (result_type != TREE_TYPE (op2))
3600 op2 = convert_and_check (result_type, op2);
3602 if (TREE_CODE (ifexp) == INTEGER_CST)
3603 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3605 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3608 /* Given a list of expressions, return a compound expression
3609 that performs them all and returns the value of the last of them. */
3611 tree
3612 build_compound_expr (list)
3613 tree list;
3615 return internal_build_compound_expr (list, TRUE);
3618 static tree
3619 internal_build_compound_expr (list, first_p)
3620 tree list;
3621 int first_p;
3623 tree rest;
3625 if (TREE_CHAIN (list) == 0)
3627 /* Convert arrays to pointers when there really is a comma operator. */
3628 if (!first_p && TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
3629 TREE_VALUE (list) = default_conversion (TREE_VALUE (list));
3631 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3632 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3634 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3635 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3636 list = TREE_OPERAND (list, 0);
3637 #endif
3639 /* Don't let (0, 0) be null pointer constant. */
3640 if (!first_p && integer_zerop (TREE_VALUE (list)))
3641 return non_lvalue (TREE_VALUE (list));
3642 return TREE_VALUE (list);
3645 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3647 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3649 /* The left-hand operand of a comma expression is like an expression
3650 statement: with -W or -Wunused, we should warn if it doesn't have
3651 any side-effects, unless it was explicitly cast to (void). */
3652 if ((extra_warnings || warn_unused_value)
3653 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3654 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
3655 warning ("left-hand operand of comma expression has no effect");
3657 /* When pedantic, a compound expression can be neither an lvalue
3658 nor an integer constant expression. */
3659 if (! pedantic)
3660 return rest;
3663 /* With -Wunused, we should also warn if the left-hand operand does have
3664 side-effects, but computes a value which is not used. For example, in
3665 `foo() + bar(), baz()' the result of the `+' operator is not used,
3666 so we should issue a warning. */
3667 else if (warn_unused_value)
3668 warn_if_unused_value (TREE_VALUE (list));
3670 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3673 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3675 tree
3676 build_c_cast (type, expr)
3677 tree type;
3678 tree expr;
3680 tree value = expr;
3682 if (type == error_mark_node || expr == error_mark_node)
3683 return error_mark_node;
3684 type = TYPE_MAIN_VARIANT (type);
3686 #if 0
3687 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3688 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3689 value = TREE_OPERAND (value, 0);
3690 #endif
3692 if (TREE_CODE (type) == ARRAY_TYPE)
3694 error ("cast specifies array type");
3695 return error_mark_node;
3698 if (TREE_CODE (type) == FUNCTION_TYPE)
3700 error ("cast specifies function type");
3701 return error_mark_node;
3704 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3706 if (pedantic)
3708 if (TREE_CODE (type) == RECORD_TYPE
3709 || TREE_CODE (type) == UNION_TYPE)
3710 pedwarn ("ISO C forbids casting nonscalar to the same type");
3713 else if (TREE_CODE (type) == UNION_TYPE)
3715 tree field;
3716 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3717 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3718 value = default_conversion (value);
3720 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3721 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3722 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3723 break;
3725 if (field)
3727 const char *name;
3728 tree t;
3730 if (pedantic)
3731 pedwarn ("ISO C forbids casts to union type");
3732 if (TYPE_NAME (type) != 0)
3734 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3735 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3736 else
3737 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3739 else
3740 name = "";
3741 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3742 build_tree_list (field, value)),
3743 0, 0);
3744 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3745 return t;
3747 error ("cast to union type from type not present in union");
3748 return error_mark_node;
3750 else
3752 tree otype, ovalue;
3754 /* If casting to void, avoid the error that would come
3755 from default_conversion in the case of a non-lvalue array. */
3756 if (type == void_type_node)
3757 return build1 (CONVERT_EXPR, type, value);
3759 /* Convert functions and arrays to pointers,
3760 but don't convert any other types. */
3761 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3762 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3763 value = default_conversion (value);
3764 otype = TREE_TYPE (value);
3766 /* Optionally warn about potentially worrisome casts. */
3768 if (warn_cast_qual
3769 && TREE_CODE (type) == POINTER_TYPE
3770 && TREE_CODE (otype) == POINTER_TYPE)
3772 tree in_type = type;
3773 tree in_otype = otype;
3774 int warn = 0;
3776 /* Check that the qualifiers on IN_TYPE are a superset of
3777 the qualifiers of IN_OTYPE. The outermost level of
3778 POINTER_TYPE nodes is uninteresting and we stop as soon
3779 as we hit a non-POINTER_TYPE node on either type. */
3782 in_otype = TREE_TYPE (in_otype);
3783 in_type = TREE_TYPE (in_type);
3784 warn |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3786 while (TREE_CODE (in_type) == POINTER_TYPE
3787 && TREE_CODE (in_otype) == POINTER_TYPE);
3789 if (warn)
3790 /* There are qualifiers present in IN_OTYPE that are not
3791 present in IN_TYPE. */
3792 warning ("cast discards qualifiers from pointer target type");
3795 /* Warn about possible alignment problems. */
3796 if (STRICT_ALIGNMENT && warn_cast_align
3797 && TREE_CODE (type) == POINTER_TYPE
3798 && TREE_CODE (otype) == POINTER_TYPE
3799 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3800 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3801 /* Don't warn about opaque types, where the actual alignment
3802 restriction is unknown. */
3803 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3804 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3805 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3806 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3807 warning ("cast increases required alignment of target type");
3809 if (TREE_CODE (type) == INTEGER_TYPE
3810 && TREE_CODE (otype) == POINTER_TYPE
3811 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3812 && !TREE_CONSTANT (value))
3813 warning ("cast from pointer to integer of different size");
3815 if (warn_bad_function_cast
3816 && TREE_CODE (value) == CALL_EXPR
3817 && TREE_CODE (type) != TREE_CODE (otype))
3818 warning ("cast does not match function type");
3820 if (TREE_CODE (type) == POINTER_TYPE
3821 && TREE_CODE (otype) == INTEGER_TYPE
3822 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3823 /* Don't warn about converting any constant. */
3824 && !TREE_CONSTANT (value))
3825 warning ("cast to pointer from integer of different size");
3827 ovalue = value;
3828 value = convert (type, value);
3830 /* Ignore any integer overflow caused by the cast. */
3831 if (TREE_CODE (value) == INTEGER_CST)
3833 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3834 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3838 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3839 if (pedantic && TREE_CODE (value) == INTEGER_CST
3840 && TREE_CODE (expr) == INTEGER_CST
3841 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3842 value = non_lvalue (value);
3844 /* If pedantic, don't let a cast be an lvalue. */
3845 if (value == expr && pedantic)
3846 value = non_lvalue (value);
3848 return value;
3851 /* Interpret a cast of expression EXPR to type TYPE. */
3852 tree
3853 c_cast_expr (type, expr)
3854 tree type, expr;
3856 int saved_wsp = warn_strict_prototypes;
3858 /* This avoids warnings about unprototyped casts on
3859 integers. E.g. "#define SIG_DFL (void(*)())0". */
3860 if (TREE_CODE (expr) == INTEGER_CST)
3861 warn_strict_prototypes = 0;
3862 type = groktypename (type);
3863 warn_strict_prototypes = saved_wsp;
3865 return build_c_cast (type, expr);
3869 /* Build an assignment expression of lvalue LHS from value RHS.
3870 MODIFYCODE is the code for a binary operator that we use
3871 to combine the old value of LHS with RHS to get the new value.
3872 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3874 tree
3875 build_modify_expr (lhs, modifycode, rhs)
3876 tree lhs, rhs;
3877 enum tree_code modifycode;
3879 tree result;
3880 tree newrhs;
3881 tree lhstype = TREE_TYPE (lhs);
3882 tree olhstype = lhstype;
3884 /* Types that aren't fully specified cannot be used in assignments. */
3885 lhs = require_complete_type (lhs);
3887 /* Avoid duplicate error messages from operands that had errors. */
3888 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3889 return error_mark_node;
3891 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3892 /* Do not use STRIP_NOPS here. We do not want an enumerator
3893 whose value is 0 to count as a null pointer constant. */
3894 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3895 rhs = TREE_OPERAND (rhs, 0);
3897 newrhs = rhs;
3899 /* Handle control structure constructs used as "lvalues". */
3901 switch (TREE_CODE (lhs))
3903 /* Handle (a, b) used as an "lvalue". */
3904 case COMPOUND_EXPR:
3905 pedantic_lvalue_warning (COMPOUND_EXPR);
3906 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3907 if (TREE_CODE (newrhs) == ERROR_MARK)
3908 return error_mark_node;
3909 return build (COMPOUND_EXPR, lhstype,
3910 TREE_OPERAND (lhs, 0), newrhs);
3912 /* Handle (a ? b : c) used as an "lvalue". */
3913 case COND_EXPR:
3914 pedantic_lvalue_warning (COND_EXPR);
3915 rhs = save_expr (rhs);
3917 /* Produce (a ? (b = rhs) : (c = rhs))
3918 except that the RHS goes through a save-expr
3919 so the code to compute it is only emitted once. */
3920 tree cond
3921 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3922 build_modify_expr (TREE_OPERAND (lhs, 1),
3923 modifycode, rhs),
3924 build_modify_expr (TREE_OPERAND (lhs, 2),
3925 modifycode, rhs));
3926 if (TREE_CODE (cond) == ERROR_MARK)
3927 return cond;
3928 /* Make sure the code to compute the rhs comes out
3929 before the split. */
3930 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3931 /* But cast it to void to avoid an "unused" error. */
3932 convert (void_type_node, rhs), cond);
3934 default:
3935 break;
3938 /* If a binary op has been requested, combine the old LHS value with the RHS
3939 producing the value we should actually store into the LHS. */
3941 if (modifycode != NOP_EXPR)
3943 lhs = stabilize_reference (lhs);
3944 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3947 /* Handle a cast used as an "lvalue".
3948 We have already performed any binary operator using the value as cast.
3949 Now convert the result to the cast type of the lhs,
3950 and then true type of the lhs and store it there;
3951 then convert result back to the cast type to be the value
3952 of the assignment. */
3954 switch (TREE_CODE (lhs))
3956 case NOP_EXPR:
3957 case CONVERT_EXPR:
3958 case FLOAT_EXPR:
3959 case FIX_TRUNC_EXPR:
3960 case FIX_FLOOR_EXPR:
3961 case FIX_ROUND_EXPR:
3962 case FIX_CEIL_EXPR:
3963 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3964 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3965 newrhs = default_conversion (newrhs);
3967 tree inner_lhs = TREE_OPERAND (lhs, 0);
3968 tree result;
3969 result = build_modify_expr (inner_lhs, NOP_EXPR,
3970 convert (TREE_TYPE (inner_lhs),
3971 convert (lhstype, newrhs)));
3972 if (TREE_CODE (result) == ERROR_MARK)
3973 return result;
3974 pedantic_lvalue_warning (CONVERT_EXPR);
3975 return convert (TREE_TYPE (lhs), result);
3978 default:
3979 break;
3982 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3983 Reject anything strange now. */
3985 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3986 return error_mark_node;
3988 /* Warn about storing in something that is `const'. */
3990 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3991 || ((TREE_CODE (lhstype) == RECORD_TYPE
3992 || TREE_CODE (lhstype) == UNION_TYPE)
3993 && C_TYPE_FIELDS_READONLY (lhstype)))
3994 readonly_warning (lhs, "assignment");
3996 /* If storing into a structure or union member,
3997 it has probably been given type `int'.
3998 Compute the type that would go with
3999 the actual amount of storage the member occupies. */
4001 if (TREE_CODE (lhs) == COMPONENT_REF
4002 && (TREE_CODE (lhstype) == INTEGER_TYPE
4003 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4004 || TREE_CODE (lhstype) == REAL_TYPE
4005 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4006 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4008 /* If storing in a field that is in actuality a short or narrower than one,
4009 we must store in the field in its actual type. */
4011 if (lhstype != TREE_TYPE (lhs))
4013 lhs = copy_node (lhs);
4014 TREE_TYPE (lhs) = lhstype;
4017 /* Convert new value to destination type. */
4019 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
4020 NULL_TREE, NULL_TREE, 0);
4021 if (TREE_CODE (newrhs) == ERROR_MARK)
4022 return error_mark_node;
4024 /* Scan operands */
4026 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4027 TREE_SIDE_EFFECTS (result) = 1;
4029 /* If we got the LHS in a different type for storing in,
4030 convert the result back to the nominal type of LHS
4031 so that the value we return always has the same type
4032 as the LHS argument. */
4034 if (olhstype == TREE_TYPE (result))
4035 return result;
4036 return convert_for_assignment (olhstype, result, _("assignment"),
4037 NULL_TREE, NULL_TREE, 0);
4040 /* Convert value RHS to type TYPE as preparation for an assignment
4041 to an lvalue of type TYPE.
4042 The real work of conversion is done by `convert'.
4043 The purpose of this function is to generate error messages
4044 for assignments that are not allowed in C.
4045 ERRTYPE is a string to use in error messages:
4046 "assignment", "return", etc. If it is null, this is parameter passing
4047 for a function call (and different error messages are output).
4049 FUNNAME is the name of the function being called,
4050 as an IDENTIFIER_NODE, or null.
4051 PARMNUM is the number of the argument, for printing in error messages. */
4053 static tree
4054 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
4055 tree type, rhs;
4056 const char *errtype;
4057 tree fundecl, funname;
4058 int parmnum;
4060 enum tree_code codel = TREE_CODE (type);
4061 tree rhstype;
4062 enum tree_code coder;
4064 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4065 /* Do not use STRIP_NOPS here. We do not want an enumerator
4066 whose value is 0 to count as a null pointer constant. */
4067 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4068 rhs = TREE_OPERAND (rhs, 0);
4070 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4071 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4072 rhs = default_conversion (rhs);
4073 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4074 rhs = decl_constant_value_for_broken_optimization (rhs);
4076 rhstype = TREE_TYPE (rhs);
4077 coder = TREE_CODE (rhstype);
4079 if (coder == ERROR_MARK)
4080 return error_mark_node;
4082 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4084 overflow_warning (rhs);
4085 /* Check for Objective-C protocols. This will issue a warning if
4086 there are protocol violations. No need to use the return value. */
4087 maybe_objc_comptypes (type, rhstype, 0);
4088 return rhs;
4091 if (coder == VOID_TYPE)
4093 error ("void value not ignored as it ought to be");
4094 return error_mark_node;
4096 /* A type converts to a reference to it.
4097 This code doesn't fully support references, it's just for the
4098 special case of va_start and va_copy. */
4099 if (codel == REFERENCE_TYPE
4100 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4102 if (mark_addressable (rhs) == 0)
4103 return error_mark_node;
4104 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4106 /* We already know that these two types are compatible, but they
4107 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4108 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4109 likely to be va_list, a typedef to __builtin_va_list, which
4110 is different enough that it will cause problems later. */
4111 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4112 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4114 rhs = build1 (NOP_EXPR, type, rhs);
4115 return rhs;
4117 /* Arithmetic types all interconvert, and enum is treated like int. */
4118 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4119 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4120 || codel == BOOLEAN_TYPE)
4121 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4122 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4123 || coder == BOOLEAN_TYPE))
4124 return convert_and_check (type, rhs);
4126 /* Conversion to a transparent union from its member types.
4127 This applies only to function arguments. */
4128 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4130 tree memb_types;
4131 tree marginal_memb_type = 0;
4133 for (memb_types = TYPE_FIELDS (type); memb_types;
4134 memb_types = TREE_CHAIN (memb_types))
4136 tree memb_type = TREE_TYPE (memb_types);
4138 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4139 TYPE_MAIN_VARIANT (rhstype)))
4140 break;
4142 if (TREE_CODE (memb_type) != POINTER_TYPE)
4143 continue;
4145 if (coder == POINTER_TYPE)
4147 tree ttl = TREE_TYPE (memb_type);
4148 tree ttr = TREE_TYPE (rhstype);
4150 /* Any non-function converts to a [const][volatile] void *
4151 and vice versa; otherwise, targets must be the same.
4152 Meanwhile, the lhs target must have all the qualifiers of
4153 the rhs. */
4154 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4155 || comp_target_types (memb_type, rhstype))
4157 /* If this type won't generate any warnings, use it. */
4158 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4159 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4160 && TREE_CODE (ttl) == FUNCTION_TYPE)
4161 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4162 == TYPE_QUALS (ttr))
4163 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4164 == TYPE_QUALS (ttl))))
4165 break;
4167 /* Keep looking for a better type, but remember this one. */
4168 if (! marginal_memb_type)
4169 marginal_memb_type = memb_type;
4173 /* Can convert integer zero to any pointer type. */
4174 if (integer_zerop (rhs)
4175 || (TREE_CODE (rhs) == NOP_EXPR
4176 && integer_zerop (TREE_OPERAND (rhs, 0))))
4178 rhs = null_pointer_node;
4179 break;
4183 if (memb_types || marginal_memb_type)
4185 if (! memb_types)
4187 /* We have only a marginally acceptable member type;
4188 it needs a warning. */
4189 tree ttl = TREE_TYPE (marginal_memb_type);
4190 tree ttr = TREE_TYPE (rhstype);
4192 /* Const and volatile mean something different for function
4193 types, so the usual warnings are not appropriate. */
4194 if (TREE_CODE (ttr) == FUNCTION_TYPE
4195 && TREE_CODE (ttl) == FUNCTION_TYPE)
4197 /* Because const and volatile on functions are
4198 restrictions that say the function will not do
4199 certain things, it is okay to use a const or volatile
4200 function where an ordinary one is wanted, but not
4201 vice-versa. */
4202 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4203 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4204 errtype, funname, parmnum);
4206 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4207 warn_for_assignment ("%s discards qualifiers from pointer target type",
4208 errtype, funname,
4209 parmnum);
4212 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4213 pedwarn ("ISO C prohibits argument conversion to union type");
4215 return build1 (NOP_EXPR, type, rhs);
4219 /* Conversions among pointers */
4220 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4221 && (coder == POINTER_TYPE || coder == REFERENCE_TYPE))
4223 tree ttl = TREE_TYPE (type);
4224 tree ttr = TREE_TYPE (rhstype);
4226 /* Any non-function converts to a [const][volatile] void *
4227 and vice versa; otherwise, targets must be the same.
4228 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4229 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4230 || comp_target_types (type, rhstype)
4231 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4232 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4234 if (pedantic
4235 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4237 (VOID_TYPE_P (ttr)
4238 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4239 which are not ANSI null ptr constants. */
4240 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4241 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4242 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
4243 errtype, funname, parmnum);
4244 /* Const and volatile mean something different for function types,
4245 so the usual warnings are not appropriate. */
4246 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4247 && TREE_CODE (ttl) != FUNCTION_TYPE)
4249 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4250 warn_for_assignment ("%s discards qualifiers from pointer target type",
4251 errtype, funname, parmnum);
4252 /* If this is not a case of ignoring a mismatch in signedness,
4253 no warning. */
4254 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4255 || comp_target_types (type, rhstype))
4257 /* If there is a mismatch, do warn. */
4258 else if (pedantic)
4259 warn_for_assignment ("pointer targets in %s differ in signedness",
4260 errtype, funname, parmnum);
4262 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4263 && TREE_CODE (ttr) == FUNCTION_TYPE)
4265 /* Because const and volatile on functions are restrictions
4266 that say the function will not do certain things,
4267 it is okay to use a const or volatile function
4268 where an ordinary one is wanted, but not vice-versa. */
4269 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4270 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4271 errtype, funname, parmnum);
4274 else
4275 warn_for_assignment ("%s from incompatible pointer type",
4276 errtype, funname, parmnum);
4277 return convert (type, rhs);
4279 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4281 /* An explicit constant 0 can convert to a pointer,
4282 or one that results from arithmetic, even including
4283 a cast to integer type. */
4284 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4286 ! (TREE_CODE (rhs) == NOP_EXPR
4287 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4288 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4289 && integer_zerop (TREE_OPERAND (rhs, 0))))
4291 warn_for_assignment ("%s makes pointer from integer without a cast",
4292 errtype, funname, parmnum);
4293 return convert (type, rhs);
4295 return null_pointer_node;
4297 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4299 warn_for_assignment ("%s makes integer from pointer without a cast",
4300 errtype, funname, parmnum);
4301 return convert (type, rhs);
4303 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4304 return convert (type, rhs);
4306 if (!errtype)
4308 if (funname)
4310 tree selector = maybe_building_objc_message_expr ();
4312 if (selector && parmnum > 2)
4313 error ("incompatible type for argument %d of `%s'",
4314 parmnum - 2, IDENTIFIER_POINTER (selector));
4315 else
4316 error ("incompatible type for argument %d of `%s'",
4317 parmnum, IDENTIFIER_POINTER (funname));
4319 else
4320 error ("incompatible type for argument %d of indirect function call",
4321 parmnum);
4323 else
4324 error ("incompatible types in %s", errtype);
4326 return error_mark_node;
4329 /* Print a warning using MSGID.
4330 It gets OPNAME as its one parameter.
4331 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4332 FUNCTION and ARGNUM are handled specially if we are building an
4333 Objective-C selector. */
4335 static void
4336 warn_for_assignment (msgid, opname, function, argnum)
4337 const char *msgid;
4338 const char *opname;
4339 tree function;
4340 int argnum;
4342 if (opname == 0)
4344 tree selector = maybe_building_objc_message_expr ();
4345 char * new_opname;
4347 if (selector && argnum > 2)
4349 function = selector;
4350 argnum -= 2;
4352 if (function)
4354 /* Function name is known; supply it. */
4355 const char *const argstring = _("passing arg %d of `%s'");
4356 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4357 + strlen (argstring) + 1 + 25
4358 /*%d*/ + 1);
4359 sprintf (new_opname, argstring, argnum,
4360 IDENTIFIER_POINTER (function));
4362 else
4364 /* Function name unknown (call through ptr); just give arg number.*/
4365 const char *const argnofun = _("passing arg %d of pointer to function");
4366 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4367 sprintf (new_opname, argnofun, argnum);
4369 opname = new_opname;
4371 pedwarn (msgid, opname);
4374 /* If VALUE is a compound expr all of whose expressions are constant, then
4375 return its value. Otherwise, return error_mark_node.
4377 This is for handling COMPOUND_EXPRs as initializer elements
4378 which is allowed with a warning when -pedantic is specified. */
4380 static tree
4381 valid_compound_expr_initializer (value, endtype)
4382 tree value;
4383 tree endtype;
4385 if (TREE_CODE (value) == COMPOUND_EXPR)
4387 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4388 == error_mark_node)
4389 return error_mark_node;
4390 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4391 endtype);
4393 else if (! TREE_CONSTANT (value)
4394 && ! initializer_constant_valid_p (value, endtype))
4395 return error_mark_node;
4396 else
4397 return value;
4400 /* Perform appropriate conversions on the initial value of a variable,
4401 store it in the declaration DECL,
4402 and print any error messages that are appropriate.
4403 If the init is invalid, store an ERROR_MARK. */
4405 void
4406 store_init_value (decl, init)
4407 tree decl, init;
4409 tree value, type;
4411 /* If variable's type was invalidly declared, just ignore it. */
4413 type = TREE_TYPE (decl);
4414 if (TREE_CODE (type) == ERROR_MARK)
4415 return;
4417 /* Digest the specified initializer into an expression. */
4419 value = digest_init (type, init, TREE_STATIC (decl),
4420 TREE_STATIC (decl) || (pedantic && !flag_isoc99));
4422 /* Store the expression if valid; else report error. */
4424 #if 0
4425 /* Note that this is the only place we can detect the error
4426 in a case such as struct foo bar = (struct foo) { x, y };
4427 where there is one initial value which is a constructor expression. */
4428 if (value == error_mark_node)
4430 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4432 error ("initializer for static variable is not constant");
4433 value = error_mark_node;
4435 else if (TREE_STATIC (decl)
4436 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4438 error ("initializer for static variable uses complicated arithmetic");
4439 value = error_mark_node;
4441 else
4443 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4445 if (! TREE_CONSTANT (value))
4446 pedwarn ("aggregate initializer is not constant");
4447 else if (! TREE_STATIC (value))
4448 pedwarn ("aggregate initializer uses complicated arithmetic");
4451 #endif
4453 if (warn_traditional && !in_system_header
4454 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
4455 warning ("traditional C rejects automatic aggregate initialization");
4457 DECL_INITIAL (decl) = value;
4459 /* ANSI wants warnings about out-of-range constant initializers. */
4460 STRIP_TYPE_NOPS (value);
4461 constant_expression_warning (value);
4464 /* Methods for storing and printing names for error messages. */
4466 /* Implement a spelling stack that allows components of a name to be pushed
4467 and popped. Each element on the stack is this structure. */
4469 struct spelling
4471 int kind;
4472 union
4474 int i;
4475 const char *s;
4476 } u;
4479 #define SPELLING_STRING 1
4480 #define SPELLING_MEMBER 2
4481 #define SPELLING_BOUNDS 3
4483 static struct spelling *spelling; /* Next stack element (unused). */
4484 static struct spelling *spelling_base; /* Spelling stack base. */
4485 static int spelling_size; /* Size of the spelling stack. */
4487 /* Macros to save and restore the spelling stack around push_... functions.
4488 Alternative to SAVE_SPELLING_STACK. */
4490 #define SPELLING_DEPTH() (spelling - spelling_base)
4491 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4493 /* Save and restore the spelling stack around arbitrary C code. */
4495 #define SAVE_SPELLING_DEPTH(code) \
4497 int __depth = SPELLING_DEPTH (); \
4498 code; \
4499 RESTORE_SPELLING_DEPTH (__depth); \
4502 /* Push an element on the spelling stack with type KIND and assign VALUE
4503 to MEMBER. */
4505 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4507 int depth = SPELLING_DEPTH (); \
4509 if (depth >= spelling_size) \
4511 spelling_size += 10; \
4512 if (spelling_base == 0) \
4513 spelling_base \
4514 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4515 else \
4516 spelling_base \
4517 = (struct spelling *) xrealloc (spelling_base, \
4518 spelling_size * sizeof (struct spelling)); \
4519 RESTORE_SPELLING_DEPTH (depth); \
4522 spelling->kind = (KIND); \
4523 spelling->MEMBER = (VALUE); \
4524 spelling++; \
4527 /* Push STRING on the stack. Printed literally. */
4529 static void
4530 push_string (string)
4531 const char *string;
4533 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4536 /* Push a member name on the stack. Printed as '.' STRING. */
4538 static void
4539 push_member_name (decl)
4540 tree decl;
4543 const char *const string
4544 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4545 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4548 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4550 static void
4551 push_array_bounds (bounds)
4552 int bounds;
4554 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4557 /* Compute the maximum size in bytes of the printed spelling. */
4559 static int
4560 spelling_length ()
4562 int size = 0;
4563 struct spelling *p;
4565 for (p = spelling_base; p < spelling; p++)
4567 if (p->kind == SPELLING_BOUNDS)
4568 size += 25;
4569 else
4570 size += strlen (p->u.s) + 1;
4573 return size;
4576 /* Print the spelling to BUFFER and return it. */
4578 static char *
4579 print_spelling (buffer)
4580 char *buffer;
4582 char *d = buffer;
4583 struct spelling *p;
4585 for (p = spelling_base; p < spelling; p++)
4586 if (p->kind == SPELLING_BOUNDS)
4588 sprintf (d, "[%d]", p->u.i);
4589 d += strlen (d);
4591 else
4593 const char *s;
4594 if (p->kind == SPELLING_MEMBER)
4595 *d++ = '.';
4596 for (s = p->u.s; (*d = *s++); d++)
4599 *d++ = '\0';
4600 return buffer;
4603 /* Issue an error message for a bad initializer component.
4604 MSGID identifies the message.
4605 The component name is taken from the spelling stack. */
4607 void
4608 error_init (msgid)
4609 const char *msgid;
4611 char *ofwhat;
4613 error ("%s", msgid);
4614 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4615 if (*ofwhat)
4616 error ("(near initialization for `%s')", ofwhat);
4619 /* Issue a pedantic warning for a bad initializer component.
4620 MSGID identifies the message.
4621 The component name is taken from the spelling stack. */
4623 void
4624 pedwarn_init (msgid)
4625 const char *msgid;
4627 char *ofwhat;
4629 pedwarn ("%s", msgid);
4630 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4631 if (*ofwhat)
4632 pedwarn ("(near initialization for `%s')", ofwhat);
4635 /* Issue a warning for a bad initializer component.
4636 MSGID identifies the message.
4637 The component name is taken from the spelling stack. */
4639 static void
4640 warning_init (msgid)
4641 const char *msgid;
4643 char *ofwhat;
4645 warning ("%s", msgid);
4646 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4647 if (*ofwhat)
4648 warning ("(near initialization for `%s')", ofwhat);
4651 /* Digest the parser output INIT as an initializer for type TYPE.
4652 Return a C expression of type TYPE to represent the initial value.
4654 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4655 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4656 applies only to elements of constructors. */
4658 static tree
4659 digest_init (type, init, require_constant, constructor_constant)
4660 tree type, init;
4661 int require_constant, constructor_constant;
4663 enum tree_code code = TREE_CODE (type);
4664 tree inside_init = init;
4666 if (type == error_mark_node
4667 || init == error_mark_node
4668 || TREE_TYPE (init) == error_mark_node)
4669 return error_mark_node;
4671 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4672 /* Do not use STRIP_NOPS here. We do not want an enumerator
4673 whose value is 0 to count as a null pointer constant. */
4674 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4675 inside_init = TREE_OPERAND (init, 0);
4677 inside_init = fold (inside_init);
4679 /* Initialization of an array of chars from a string constant
4680 optionally enclosed in braces. */
4682 if (code == ARRAY_TYPE)
4684 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4685 if ((typ1 == char_type_node
4686 || typ1 == signed_char_type_node
4687 || typ1 == unsigned_char_type_node
4688 || typ1 == unsigned_wchar_type_node
4689 || typ1 == signed_wchar_type_node)
4690 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4692 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4693 TYPE_MAIN_VARIANT (type)))
4694 return inside_init;
4696 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4697 != char_type_node)
4698 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4700 error_init ("char-array initialized from wide string");
4701 return error_mark_node;
4703 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4704 == char_type_node)
4705 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4707 error_init ("int-array initialized from non-wide string");
4708 return error_mark_node;
4711 TREE_TYPE (inside_init) = type;
4712 if (TYPE_DOMAIN (type) != 0
4713 && TYPE_SIZE (type) != 0
4714 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4715 /* Subtract 1 (or sizeof (wchar_t))
4716 because it's ok to ignore the terminating null char
4717 that is counted in the length of the constant. */
4718 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4719 TREE_STRING_LENGTH (inside_init)
4720 - ((TYPE_PRECISION (typ1)
4721 != TYPE_PRECISION (char_type_node))
4722 ? (TYPE_PRECISION (wchar_type_node)
4723 / BITS_PER_UNIT)
4724 : 1)))
4725 pedwarn_init ("initializer-string for array of chars is too long");
4727 return inside_init;
4731 /* Any type can be initialized
4732 from an expression of the same type, optionally with braces. */
4734 if (inside_init && TREE_TYPE (inside_init) != 0
4735 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4736 TYPE_MAIN_VARIANT (type))
4737 || (code == ARRAY_TYPE
4738 && comptypes (TREE_TYPE (inside_init), type))
4739 || (code == POINTER_TYPE
4740 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4741 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4742 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4743 TREE_TYPE (type)))))
4745 if (code == POINTER_TYPE
4746 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4747 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4748 inside_init = default_conversion (inside_init);
4749 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4750 && TREE_CODE (inside_init) != CONSTRUCTOR)
4752 error_init ("array initialized from non-constant array expression");
4753 return error_mark_node;
4756 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4757 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4759 /* Compound expressions can only occur here if -pedantic or
4760 -pedantic-errors is specified. In the later case, we always want
4761 an error. In the former case, we simply want a warning. */
4762 if (require_constant && pedantic
4763 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4765 inside_init
4766 = valid_compound_expr_initializer (inside_init,
4767 TREE_TYPE (inside_init));
4768 if (inside_init == error_mark_node)
4769 error_init ("initializer element is not constant");
4770 else
4771 pedwarn_init ("initializer element is not constant");
4772 if (flag_pedantic_errors)
4773 inside_init = error_mark_node;
4775 else if (require_constant
4776 && (!TREE_CONSTANT (inside_init)
4777 /* This test catches things like `7 / 0' which
4778 result in an expression for which TREE_CONSTANT
4779 is true, but which is not actually something
4780 that is a legal constant. We really should not
4781 be using this function, because it is a part of
4782 the back-end. Instead, the expression should
4783 already have been turned into ERROR_MARK_NODE. */
4784 || !initializer_constant_valid_p (inside_init,
4785 TREE_TYPE (inside_init))))
4787 error_init ("initializer element is not constant");
4788 inside_init = error_mark_node;
4791 return inside_init;
4794 /* Handle scalar types, including conversions. */
4796 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4797 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4799 /* Note that convert_for_assignment calls default_conversion
4800 for arrays and functions. We must not call it in the
4801 case where inside_init is a null pointer constant. */
4802 inside_init
4803 = convert_for_assignment (type, init, _("initialization"),
4804 NULL_TREE, NULL_TREE, 0);
4806 if (require_constant && ! TREE_CONSTANT (inside_init))
4808 error_init ("initializer element is not constant");
4809 inside_init = error_mark_node;
4811 else if (require_constant
4812 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4814 error_init ("initializer element is not computable at load time");
4815 inside_init = error_mark_node;
4818 return inside_init;
4821 /* Come here only for records and arrays. */
4823 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4825 error_init ("variable-sized object may not be initialized");
4826 return error_mark_node;
4829 /* Traditionally, you can write struct foo x = 0;
4830 and it initializes the first element of x to 0. */
4831 if (flag_traditional)
4833 tree top = 0, prev = 0, otype = type;
4834 while (TREE_CODE (type) == RECORD_TYPE
4835 || TREE_CODE (type) == ARRAY_TYPE
4836 || TREE_CODE (type) == QUAL_UNION_TYPE
4837 || TREE_CODE (type) == UNION_TYPE)
4839 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4840 if (prev == 0)
4841 top = temp;
4842 else
4843 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4844 prev = temp;
4845 if (TREE_CODE (type) == ARRAY_TYPE)
4846 type = TREE_TYPE (type);
4847 else if (TYPE_FIELDS (type))
4848 type = TREE_TYPE (TYPE_FIELDS (type));
4849 else
4851 error_init ("invalid initializer");
4852 return error_mark_node;
4856 if (otype != type)
4858 TREE_OPERAND (prev, 1)
4859 = build_tree_list (NULL_TREE,
4860 digest_init (type, init, require_constant,
4861 constructor_constant));
4862 return top;
4864 else
4865 return error_mark_node;
4867 error_init ("invalid initializer");
4868 return error_mark_node;
4871 /* Handle initializers that use braces. */
4873 /* Type of object we are accumulating a constructor for.
4874 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4875 static tree constructor_type;
4877 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4878 left to fill. */
4879 static tree constructor_fields;
4881 /* For an ARRAY_TYPE, this is the specified index
4882 at which to store the next element we get. */
4883 static tree constructor_index;
4885 /* For an ARRAY_TYPE, this is the maximum index. */
4886 static tree constructor_max_index;
4888 /* For a RECORD_TYPE, this is the first field not yet written out. */
4889 static tree constructor_unfilled_fields;
4891 /* For an ARRAY_TYPE, this is the index of the first element
4892 not yet written out. */
4893 static tree constructor_unfilled_index;
4895 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4896 This is so we can generate gaps between fields, when appropriate. */
4897 static tree constructor_bit_index;
4899 /* If we are saving up the elements rather than allocating them,
4900 this is the list of elements so far (in reverse order,
4901 most recent first). */
4902 static tree constructor_elements;
4904 /* 1 if constructor should be incrementally stored into a constructor chain,
4905 0 if all the elements should be kept in AVL tree. */
4906 static int constructor_incremental;
4908 /* 1 if so far this constructor's elements are all compile-time constants. */
4909 static int constructor_constant;
4911 /* 1 if so far this constructor's elements are all valid address constants. */
4912 static int constructor_simple;
4914 /* 1 if this constructor is erroneous so far. */
4915 static int constructor_erroneous;
4917 /* 1 if have called defer_addressed_constants. */
4918 static int constructor_subconstants_deferred;
4920 /* Structure for managing pending initializer elements, organized as an
4921 AVL tree. */
4923 struct init_node
4925 struct init_node *left, *right;
4926 struct init_node *parent;
4927 int balance;
4928 tree purpose;
4929 tree value;
4932 /* Tree of pending elements at this constructor level.
4933 These are elements encountered out of order
4934 which belong at places we haven't reached yet in actually
4935 writing the output.
4936 Will never hold tree nodes across GC runs. */
4937 static struct init_node *constructor_pending_elts;
4939 /* The SPELLING_DEPTH of this constructor. */
4940 static int constructor_depth;
4942 /* 0 if implicitly pushing constructor levels is allowed. */
4943 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4945 static int require_constant_value;
4946 static int require_constant_elements;
4948 /* DECL node for which an initializer is being read.
4949 0 means we are reading a constructor expression
4950 such as (struct foo) {...}. */
4951 static tree constructor_decl;
4953 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4954 static const char *constructor_asmspec;
4956 /* Nonzero if this is an initializer for a top-level decl. */
4957 static int constructor_top_level;
4959 /* Nonzero if there were any member designators in this initializer. */
4960 static int constructor_designated;
4962 /* Nesting depth of designator list. */
4963 static int designator_depth;
4965 /* Nonzero if there were diagnosed errors in this designator list. */
4966 static int designator_errorneous;
4969 /* This stack has a level for each implicit or explicit level of
4970 structuring in the initializer, including the outermost one. It
4971 saves the values of most of the variables above. */
4973 struct constructor_range_stack;
4975 struct constructor_stack
4977 struct constructor_stack *next;
4978 tree type;
4979 tree fields;
4980 tree index;
4981 tree max_index;
4982 tree unfilled_index;
4983 tree unfilled_fields;
4984 tree bit_index;
4985 tree elements;
4986 struct init_node *pending_elts;
4987 int offset;
4988 int depth;
4989 /* If nonzero, this value should replace the entire
4990 constructor at this level. */
4991 tree replacement_value;
4992 struct constructor_range_stack *range_stack;
4993 char constant;
4994 char simple;
4995 char implicit;
4996 char erroneous;
4997 char outer;
4998 char incremental;
4999 char designated;
5002 struct constructor_stack *constructor_stack;
5004 /* This stack represents designators from some range designator up to
5005 the last designator in the list. */
5007 struct constructor_range_stack
5009 struct constructor_range_stack *next, *prev;
5010 struct constructor_stack *stack;
5011 tree range_start;
5012 tree index;
5013 tree range_end;
5014 tree fields;
5017 struct constructor_range_stack *constructor_range_stack;
5019 /* This stack records separate initializers that are nested.
5020 Nested initializers can't happen in ANSI C, but GNU C allows them
5021 in cases like { ... (struct foo) { ... } ... }. */
5023 struct initializer_stack
5025 struct initializer_stack *next;
5026 tree decl;
5027 const char *asmspec;
5028 struct constructor_stack *constructor_stack;
5029 struct constructor_range_stack *constructor_range_stack;
5030 tree elements;
5031 struct spelling *spelling;
5032 struct spelling *spelling_base;
5033 int spelling_size;
5034 char top_level;
5035 char require_constant_value;
5036 char require_constant_elements;
5037 char deferred;
5040 struct initializer_stack *initializer_stack;
5042 /* Prepare to parse and output the initializer for variable DECL. */
5044 void
5045 start_init (decl, asmspec_tree, top_level)
5046 tree decl;
5047 tree asmspec_tree;
5048 int top_level;
5050 const char *locus;
5051 struct initializer_stack *p
5052 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5053 const char *asmspec = 0;
5055 if (asmspec_tree)
5056 asmspec = TREE_STRING_POINTER (asmspec_tree);
5058 p->decl = constructor_decl;
5059 p->asmspec = constructor_asmspec;
5060 p->require_constant_value = require_constant_value;
5061 p->require_constant_elements = require_constant_elements;
5062 p->constructor_stack = constructor_stack;
5063 p->constructor_range_stack = constructor_range_stack;
5064 p->elements = constructor_elements;
5065 p->spelling = spelling;
5066 p->spelling_base = spelling_base;
5067 p->spelling_size = spelling_size;
5068 p->deferred = constructor_subconstants_deferred;
5069 p->top_level = constructor_top_level;
5070 p->next = initializer_stack;
5071 initializer_stack = p;
5073 constructor_decl = decl;
5074 constructor_asmspec = asmspec;
5075 constructor_subconstants_deferred = 0;
5076 constructor_designated = 0;
5077 constructor_top_level = top_level;
5079 if (decl != 0)
5081 require_constant_value = TREE_STATIC (decl);
5082 require_constant_elements
5083 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5084 /* For a scalar, you can always use any value to initialize,
5085 even within braces. */
5086 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5087 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5088 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5089 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5090 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5092 else
5094 require_constant_value = 0;
5095 require_constant_elements = 0;
5096 locus = "(anonymous)";
5099 constructor_stack = 0;
5100 constructor_range_stack = 0;
5102 missing_braces_mentioned = 0;
5104 spelling_base = 0;
5105 spelling_size = 0;
5106 RESTORE_SPELLING_DEPTH (0);
5108 if (locus)
5109 push_string (locus);
5112 void
5113 finish_init ()
5115 struct initializer_stack *p = initializer_stack;
5117 /* Output subconstants (string constants, usually)
5118 that were referenced within this initializer and saved up.
5119 Must do this if and only if we called defer_addressed_constants. */
5120 if (constructor_subconstants_deferred)
5121 output_deferred_addressed_constants ();
5123 /* Free the whole constructor stack of this initializer. */
5124 while (constructor_stack)
5126 struct constructor_stack *q = constructor_stack;
5127 constructor_stack = q->next;
5128 free (q);
5131 if (constructor_range_stack)
5132 abort ();
5134 /* Pop back to the data of the outer initializer (if any). */
5135 constructor_decl = p->decl;
5136 constructor_asmspec = p->asmspec;
5137 require_constant_value = p->require_constant_value;
5138 require_constant_elements = p->require_constant_elements;
5139 constructor_stack = p->constructor_stack;
5140 constructor_range_stack = p->constructor_range_stack;
5141 constructor_elements = p->elements;
5142 spelling = p->spelling;
5143 spelling_base = p->spelling_base;
5144 spelling_size = p->spelling_size;
5145 constructor_subconstants_deferred = p->deferred;
5146 constructor_top_level = p->top_level;
5147 initializer_stack = p->next;
5148 free (p);
5151 /* Call here when we see the initializer is surrounded by braces.
5152 This is instead of a call to push_init_level;
5153 it is matched by a call to pop_init_level.
5155 TYPE is the type to initialize, for a constructor expression.
5156 For an initializer for a decl, TYPE is zero. */
5158 void
5159 really_start_incremental_init (type)
5160 tree type;
5162 struct constructor_stack *p
5163 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5165 if (type == 0)
5166 type = TREE_TYPE (constructor_decl);
5168 p->type = constructor_type;
5169 p->fields = constructor_fields;
5170 p->index = constructor_index;
5171 p->max_index = constructor_max_index;
5172 p->unfilled_index = constructor_unfilled_index;
5173 p->unfilled_fields = constructor_unfilled_fields;
5174 p->bit_index = constructor_bit_index;
5175 p->elements = constructor_elements;
5176 p->constant = constructor_constant;
5177 p->simple = constructor_simple;
5178 p->erroneous = constructor_erroneous;
5179 p->pending_elts = constructor_pending_elts;
5180 p->depth = constructor_depth;
5181 p->replacement_value = 0;
5182 p->implicit = 0;
5183 p->range_stack = 0;
5184 p->outer = 0;
5185 p->incremental = constructor_incremental;
5186 p->designated = constructor_designated;
5187 p->next = 0;
5188 constructor_stack = p;
5190 constructor_constant = 1;
5191 constructor_simple = 1;
5192 constructor_depth = SPELLING_DEPTH ();
5193 constructor_elements = 0;
5194 constructor_pending_elts = 0;
5195 constructor_type = type;
5196 constructor_incremental = 1;
5197 constructor_designated = 0;
5198 designator_depth = 0;
5199 designator_errorneous = 0;
5201 if (TREE_CODE (constructor_type) == RECORD_TYPE
5202 || TREE_CODE (constructor_type) == UNION_TYPE)
5204 constructor_fields = TYPE_FIELDS (constructor_type);
5205 /* Skip any nameless bit fields at the beginning. */
5206 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5207 && DECL_NAME (constructor_fields) == 0)
5208 constructor_fields = TREE_CHAIN (constructor_fields);
5210 constructor_unfilled_fields = constructor_fields;
5211 constructor_bit_index = bitsize_zero_node;
5213 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5215 if (TYPE_DOMAIN (constructor_type))
5217 constructor_max_index
5218 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5220 /* Detect non-empty initializations of zero-length arrays. */
5221 if (constructor_max_index == NULL_TREE
5222 && TYPE_SIZE (constructor_type))
5223 constructor_max_index = build_int_2 (-1, -1);
5225 constructor_index
5226 = convert (bitsizetype,
5227 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5229 else
5230 constructor_index = bitsize_zero_node;
5232 constructor_unfilled_index = constructor_index;
5234 else
5236 /* Handle the case of int x = {5}; */
5237 constructor_fields = constructor_type;
5238 constructor_unfilled_fields = constructor_type;
5242 /* Push down into a subobject, for initialization.
5243 If this is for an explicit set of braces, IMPLICIT is 0.
5244 If it is because the next element belongs at a lower level,
5245 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5247 void
5248 push_init_level (implicit)
5249 int implicit;
5251 struct constructor_stack *p;
5252 tree value = NULL_TREE;
5254 /* If we've exhausted any levels that didn't have braces,
5255 pop them now. */
5256 while (constructor_stack->implicit)
5258 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5259 || TREE_CODE (constructor_type) == UNION_TYPE)
5260 && constructor_fields == 0)
5261 process_init_element (pop_init_level (1));
5262 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5263 && tree_int_cst_lt (constructor_max_index, constructor_index))
5264 process_init_element (pop_init_level (1));
5265 else
5266 break;
5269 /* Unless this is an explicit brace, we need to preserve previous
5270 content if any. */
5271 if (implicit)
5273 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5274 || TREE_CODE (constructor_type) == UNION_TYPE)
5275 && constructor_fields)
5276 value = find_init_member (constructor_fields);
5277 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5278 value = find_init_member (constructor_index);
5281 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5282 p->type = constructor_type;
5283 p->fields = constructor_fields;
5284 p->index = constructor_index;
5285 p->max_index = constructor_max_index;
5286 p->unfilled_index = constructor_unfilled_index;
5287 p->unfilled_fields = constructor_unfilled_fields;
5288 p->bit_index = constructor_bit_index;
5289 p->elements = constructor_elements;
5290 p->constant = constructor_constant;
5291 p->simple = constructor_simple;
5292 p->erroneous = constructor_erroneous;
5293 p->pending_elts = constructor_pending_elts;
5294 p->depth = constructor_depth;
5295 p->replacement_value = 0;
5296 p->implicit = implicit;
5297 p->outer = 0;
5298 p->incremental = constructor_incremental;
5299 p->designated = constructor_designated;
5300 p->next = constructor_stack;
5301 p->range_stack = 0;
5302 constructor_stack = p;
5304 constructor_constant = 1;
5305 constructor_simple = 1;
5306 constructor_depth = SPELLING_DEPTH ();
5307 constructor_elements = 0;
5308 constructor_incremental = 1;
5309 constructor_designated = 0;
5310 constructor_pending_elts = 0;
5311 if (!implicit)
5313 p->range_stack = constructor_range_stack;
5314 constructor_range_stack = 0;
5315 designator_depth = 0;
5316 designator_errorneous = 0;
5319 /* Don't die if an entire brace-pair level is superfluous
5320 in the containing level. */
5321 if (constructor_type == 0)
5323 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5324 || TREE_CODE (constructor_type) == UNION_TYPE)
5326 /* Don't die if there are extra init elts at the end. */
5327 if (constructor_fields == 0)
5328 constructor_type = 0;
5329 else
5331 constructor_type = TREE_TYPE (constructor_fields);
5332 push_member_name (constructor_fields);
5333 constructor_depth++;
5336 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5338 constructor_type = TREE_TYPE (constructor_type);
5339 push_array_bounds (tree_low_cst (constructor_index, 0));
5340 constructor_depth++;
5343 if (constructor_type == 0)
5345 error_init ("extra brace group at end of initializer");
5346 constructor_fields = 0;
5347 constructor_unfilled_fields = 0;
5348 return;
5351 if (value && TREE_CODE (value) == CONSTRUCTOR)
5353 constructor_constant = TREE_CONSTANT (value);
5354 constructor_simple = TREE_STATIC (value);
5355 constructor_elements = TREE_OPERAND (value, 1);
5356 if (constructor_elements
5357 && (TREE_CODE (constructor_type) == RECORD_TYPE
5358 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5359 set_nonincremental_init ();
5362 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5364 missing_braces_mentioned = 1;
5365 warning_init ("missing braces around initializer");
5368 if (TREE_CODE (constructor_type) == RECORD_TYPE
5369 || TREE_CODE (constructor_type) == UNION_TYPE)
5371 constructor_fields = TYPE_FIELDS (constructor_type);
5372 /* Skip any nameless bit fields at the beginning. */
5373 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5374 && DECL_NAME (constructor_fields) == 0)
5375 constructor_fields = TREE_CHAIN (constructor_fields);
5377 constructor_unfilled_fields = constructor_fields;
5378 constructor_bit_index = bitsize_zero_node;
5380 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5382 if (TYPE_DOMAIN (constructor_type))
5384 constructor_max_index
5385 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5387 /* Detect non-empty initializations of zero-length arrays. */
5388 if (constructor_max_index == NULL_TREE
5389 && TYPE_SIZE (constructor_type))
5390 constructor_max_index = build_int_2 (-1, -1);
5392 constructor_index
5393 = convert (bitsizetype,
5394 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5396 else
5397 constructor_index = bitsize_zero_node;
5399 constructor_unfilled_index = constructor_index;
5400 if (value && TREE_CODE (value) == STRING_CST)
5402 /* We need to split the char/wchar array into individual
5403 characters, so that we don't have to special case it
5404 everywhere. */
5405 set_nonincremental_init_from_string (value);
5408 else
5410 warning_init ("braces around scalar initializer");
5411 constructor_fields = constructor_type;
5412 constructor_unfilled_fields = constructor_type;
5416 /* At the end of an implicit or explicit brace level,
5417 finish up that level of constructor.
5418 If we were outputting the elements as they are read, return 0
5419 from inner levels (process_init_element ignores that),
5420 but return error_mark_node from the outermost level
5421 (that's what we want to put in DECL_INITIAL).
5422 Otherwise, return a CONSTRUCTOR expression. */
5424 tree
5425 pop_init_level (implicit)
5426 int implicit;
5428 struct constructor_stack *p;
5429 HOST_WIDE_INT size = 0;
5430 tree constructor = 0;
5432 if (implicit == 0)
5434 /* When we come to an explicit close brace,
5435 pop any inner levels that didn't have explicit braces. */
5436 while (constructor_stack->implicit)
5437 process_init_element (pop_init_level (1));
5439 if (constructor_range_stack)
5440 abort ();
5443 p = constructor_stack;
5445 if (constructor_type != 0)
5446 size = int_size_in_bytes (constructor_type);
5448 /* Error for initializing a flexible array member, or a zero-length
5449 array member in an inappropriate context. */
5450 if (constructor_type && constructor_fields
5451 && TREE_CODE (constructor_type) == ARRAY_TYPE
5452 && TYPE_DOMAIN (constructor_type)
5453 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5455 /* Silently discard empty initializations. The parser will
5456 already have pedwarned for empty brackets. */
5457 if (integer_zerop (constructor_unfilled_index))
5458 constructor_type = NULL_TREE;
5459 else if (! TYPE_SIZE (constructor_type))
5461 if (constructor_depth > 2)
5462 error_init ("initialization of flexible array member in a nested context");
5463 else if (pedantic)
5464 pedwarn_init ("initialization of a flexible array member");
5466 /* We have already issued an error message for the existence
5467 of a flexible array member not at the end of the structure.
5468 Discard the initializer so that we do not abort later. */
5469 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5470 constructor_type = NULL_TREE;
5472 else
5473 /* Zero-length arrays are no longer special, so we should no longer
5474 get here. */
5475 abort();
5478 /* Warn when some struct elements are implicitly initialized to zero. */
5479 if (extra_warnings
5480 && constructor_type
5481 && TREE_CODE (constructor_type) == RECORD_TYPE
5482 && constructor_unfilled_fields)
5484 /* Do not warn for flexible array members or zero-length arrays. */
5485 while (constructor_unfilled_fields
5486 && (! DECL_SIZE (constructor_unfilled_fields)
5487 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5488 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5490 /* Do not warn if this level of the initializer uses member
5491 designators; it is likely to be deliberate. */
5492 if (constructor_unfilled_fields && !constructor_designated)
5494 push_member_name (constructor_unfilled_fields);
5495 warning_init ("missing initializer");
5496 RESTORE_SPELLING_DEPTH (constructor_depth);
5500 /* Now output all pending elements. */
5501 constructor_incremental = 1;
5502 output_pending_init_elements (1);
5504 /* Pad out the end of the structure. */
5505 if (p->replacement_value)
5506 /* If this closes a superfluous brace pair,
5507 just pass out the element between them. */
5508 constructor = p->replacement_value;
5509 else if (constructor_type == 0)
5511 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5512 && TREE_CODE (constructor_type) != UNION_TYPE
5513 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5515 /* A nonincremental scalar initializer--just return
5516 the element, after verifying there is just one. */
5517 if (constructor_elements == 0)
5519 if (!constructor_erroneous)
5520 error_init ("empty scalar initializer");
5521 constructor = error_mark_node;
5523 else if (TREE_CHAIN (constructor_elements) != 0)
5525 error_init ("extra elements in scalar initializer");
5526 constructor = TREE_VALUE (constructor_elements);
5528 else
5529 constructor = TREE_VALUE (constructor_elements);
5531 else
5533 if (constructor_erroneous)
5534 constructor = error_mark_node;
5535 else
5537 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5538 nreverse (constructor_elements));
5539 if (constructor_constant)
5540 TREE_CONSTANT (constructor) = 1;
5541 if (constructor_constant && constructor_simple)
5542 TREE_STATIC (constructor) = 1;
5546 constructor_type = p->type;
5547 constructor_fields = p->fields;
5548 constructor_index = p->index;
5549 constructor_max_index = p->max_index;
5550 constructor_unfilled_index = p->unfilled_index;
5551 constructor_unfilled_fields = p->unfilled_fields;
5552 constructor_bit_index = p->bit_index;
5553 constructor_elements = p->elements;
5554 constructor_constant = p->constant;
5555 constructor_simple = p->simple;
5556 constructor_erroneous = p->erroneous;
5557 constructor_incremental = p->incremental;
5558 constructor_designated = p->designated;
5559 constructor_pending_elts = p->pending_elts;
5560 constructor_depth = p->depth;
5561 if (!p->implicit)
5562 constructor_range_stack = p->range_stack;
5563 RESTORE_SPELLING_DEPTH (constructor_depth);
5565 constructor_stack = p->next;
5566 free (p);
5568 if (constructor == 0)
5570 if (constructor_stack == 0)
5571 return error_mark_node;
5572 return NULL_TREE;
5574 return constructor;
5577 /* Common handling for both array range and field name designators.
5578 ARRAY argument is non-zero for array ranges. Returns zero for success. */
5580 static int
5581 set_designator (array)
5582 int array;
5584 tree subtype;
5585 enum tree_code subcode;
5587 /* Don't die if an entire brace-pair level is superfluous
5588 in the containing level. */
5589 if (constructor_type == 0)
5590 return 1;
5592 /* If there were errors in this designator list already, bail out silently. */
5593 if (designator_errorneous)
5594 return 1;
5596 if (!designator_depth)
5598 if (constructor_range_stack)
5599 abort ();
5601 /* Designator list starts at the level of closest explicit
5602 braces. */
5603 while (constructor_stack->implicit)
5604 process_init_element (pop_init_level (1));
5605 constructor_designated = 1;
5606 return 0;
5609 if (constructor_no_implicit)
5611 error_init ("initialization designators may not nest");
5612 return 1;
5615 if (TREE_CODE (constructor_type) == RECORD_TYPE
5616 || TREE_CODE (constructor_type) == UNION_TYPE)
5618 subtype = TREE_TYPE (constructor_fields);
5619 if (subtype != error_mark_node)
5620 subtype = TYPE_MAIN_VARIANT (subtype);
5622 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5624 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5626 else
5627 abort ();
5629 subcode = TREE_CODE (subtype);
5630 if (array && subcode != ARRAY_TYPE)
5632 error_init ("array index in non-array initializer");
5633 return 1;
5635 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5637 error_init ("field name not in record or union initializer");
5638 return 1;
5641 constructor_designated = 1;
5642 push_init_level (2);
5643 return 0;
5646 /* If there are range designators in designator list, push a new designator
5647 to constructor_range_stack. RANGE_END is end of such stack range or
5648 NULL_TREE if there is no range designator at this level. */
5650 static void
5651 push_range_stack (range_end)
5652 tree range_end;
5654 struct constructor_range_stack *p;
5656 p = (struct constructor_range_stack *)
5657 ggc_alloc (sizeof (struct constructor_range_stack));
5658 p->prev = constructor_range_stack;
5659 p->next = 0;
5660 p->fields = constructor_fields;
5661 p->range_start = constructor_index;
5662 p->index = constructor_index;
5663 p->stack = constructor_stack;
5664 p->range_end = range_end;
5665 if (constructor_range_stack)
5666 constructor_range_stack->next = p;
5667 constructor_range_stack = p;
5670 /* Within an array initializer, specify the next index to be initialized.
5671 FIRST is that index. If LAST is nonzero, then initialize a range
5672 of indices, running from FIRST through LAST. */
5674 void
5675 set_init_index (first, last)
5676 tree first, last;
5678 if (set_designator (1))
5679 return;
5681 designator_errorneous = 1;
5683 while ((TREE_CODE (first) == NOP_EXPR
5684 || TREE_CODE (first) == CONVERT_EXPR
5685 || TREE_CODE (first) == NON_LVALUE_EXPR)
5686 && (TYPE_MODE (TREE_TYPE (first))
5687 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5688 first = TREE_OPERAND (first, 0);
5690 if (last)
5691 while ((TREE_CODE (last) == NOP_EXPR
5692 || TREE_CODE (last) == CONVERT_EXPR
5693 || TREE_CODE (last) == NON_LVALUE_EXPR)
5694 && (TYPE_MODE (TREE_TYPE (last))
5695 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5696 last = TREE_OPERAND (last, 0);
5698 if (TREE_CODE (first) != INTEGER_CST)
5699 error_init ("nonconstant array index in initializer");
5700 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5701 error_init ("nonconstant array index in initializer");
5702 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5703 error_init ("array index in non-array initializer");
5704 else if (constructor_max_index
5705 && tree_int_cst_lt (constructor_max_index, first))
5706 error_init ("array index in initializer exceeds array bounds");
5707 else
5709 constructor_index = convert (bitsizetype, first);
5711 if (last)
5713 if (tree_int_cst_equal (first, last))
5714 last = 0;
5715 else if (tree_int_cst_lt (last, first))
5717 error_init ("empty index range in initializer");
5718 last = 0;
5720 else
5722 last = convert (bitsizetype, last);
5723 if (constructor_max_index != 0
5724 && tree_int_cst_lt (constructor_max_index, last))
5726 error_init ("array index range in initializer exceeds array bounds");
5727 last = 0;
5732 designator_depth++;
5733 designator_errorneous = 0;
5734 if (constructor_range_stack || last)
5735 push_range_stack (last);
5739 /* Within a struct initializer, specify the next field to be initialized. */
5741 void
5742 set_init_label (fieldname)
5743 tree fieldname;
5745 tree tail;
5747 if (set_designator (0))
5748 return;
5750 designator_errorneous = 1;
5752 if (TREE_CODE (constructor_type) != RECORD_TYPE
5753 && TREE_CODE (constructor_type) != UNION_TYPE)
5755 error_init ("field name not in record or union initializer");
5756 return;
5759 for (tail = TYPE_FIELDS (constructor_type); tail;
5760 tail = TREE_CHAIN (tail))
5762 if (DECL_NAME (tail) == fieldname)
5763 break;
5766 if (tail == 0)
5767 error ("unknown field `%s' specified in initializer",
5768 IDENTIFIER_POINTER (fieldname));
5769 else
5771 constructor_fields = tail;
5772 designator_depth++;
5773 designator_errorneous = 0;
5774 if (constructor_range_stack)
5775 push_range_stack (NULL_TREE);
5779 /* Add a new initializer to the tree of pending initializers. PURPOSE
5780 identifies the initializer, either array index or field in a structure.
5781 VALUE is the value of that index or field. */
5783 static void
5784 add_pending_init (purpose, value)
5785 tree purpose, value;
5787 struct init_node *p, **q, *r;
5789 q = &constructor_pending_elts;
5790 p = 0;
5792 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5794 while (*q != 0)
5796 p = *q;
5797 if (tree_int_cst_lt (purpose, p->purpose))
5798 q = &p->left;
5799 else if (tree_int_cst_lt (p->purpose, purpose))
5800 q = &p->right;
5801 else
5803 if (TREE_SIDE_EFFECTS (p->value))
5804 warning_init ("initialized field with side-effects overwritten");
5805 p->value = value;
5806 return;
5810 else
5812 tree bitpos;
5814 bitpos = bit_position (purpose);
5815 while (*q != NULL)
5817 p = *q;
5818 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5819 q = &p->left;
5820 else if (p->purpose != purpose)
5821 q = &p->right;
5822 else
5824 if (TREE_SIDE_EFFECTS (p->value))
5825 warning_init ("initialized field with side-effects overwritten");
5826 p->value = value;
5827 return;
5832 r = (struct init_node *) ggc_alloc (sizeof (struct init_node));
5833 r->purpose = purpose;
5834 r->value = value;
5836 *q = r;
5837 r->parent = p;
5838 r->left = 0;
5839 r->right = 0;
5840 r->balance = 0;
5842 while (p)
5844 struct init_node *s;
5846 if (r == p->left)
5848 if (p->balance == 0)
5849 p->balance = -1;
5850 else if (p->balance < 0)
5852 if (r->balance < 0)
5854 /* L rotation. */
5855 p->left = r->right;
5856 if (p->left)
5857 p->left->parent = p;
5858 r->right = p;
5860 p->balance = 0;
5861 r->balance = 0;
5863 s = p->parent;
5864 p->parent = r;
5865 r->parent = s;
5866 if (s)
5868 if (s->left == p)
5869 s->left = r;
5870 else
5871 s->right = r;
5873 else
5874 constructor_pending_elts = r;
5876 else
5878 /* LR rotation. */
5879 struct init_node *t = r->right;
5881 r->right = t->left;
5882 if (r->right)
5883 r->right->parent = r;
5884 t->left = r;
5886 p->left = t->right;
5887 if (p->left)
5888 p->left->parent = p;
5889 t->right = p;
5891 p->balance = t->balance < 0;
5892 r->balance = -(t->balance > 0);
5893 t->balance = 0;
5895 s = p->parent;
5896 p->parent = t;
5897 r->parent = t;
5898 t->parent = s;
5899 if (s)
5901 if (s->left == p)
5902 s->left = t;
5903 else
5904 s->right = t;
5906 else
5907 constructor_pending_elts = t;
5909 break;
5911 else
5913 /* p->balance == +1; growth of left side balances the node. */
5914 p->balance = 0;
5915 break;
5918 else /* r == p->right */
5920 if (p->balance == 0)
5921 /* Growth propagation from right side. */
5922 p->balance++;
5923 else if (p->balance > 0)
5925 if (r->balance > 0)
5927 /* R rotation. */
5928 p->right = r->left;
5929 if (p->right)
5930 p->right->parent = p;
5931 r->left = p;
5933 p->balance = 0;
5934 r->balance = 0;
5936 s = p->parent;
5937 p->parent = r;
5938 r->parent = s;
5939 if (s)
5941 if (s->left == p)
5942 s->left = r;
5943 else
5944 s->right = r;
5946 else
5947 constructor_pending_elts = r;
5949 else /* r->balance == -1 */
5951 /* RL rotation */
5952 struct init_node *t = r->left;
5954 r->left = t->right;
5955 if (r->left)
5956 r->left->parent = r;
5957 t->right = r;
5959 p->right = t->left;
5960 if (p->right)
5961 p->right->parent = p;
5962 t->left = p;
5964 r->balance = (t->balance < 0);
5965 p->balance = -(t->balance > 0);
5966 t->balance = 0;
5968 s = p->parent;
5969 p->parent = t;
5970 r->parent = t;
5971 t->parent = s;
5972 if (s)
5974 if (s->left == p)
5975 s->left = t;
5976 else
5977 s->right = t;
5979 else
5980 constructor_pending_elts = t;
5982 break;
5984 else
5986 /* p->balance == -1; growth of right side balances the node. */
5987 p->balance = 0;
5988 break;
5992 r = p;
5993 p = p->parent;
5997 /* Build AVL tree from a sorted chain. */
5999 static void
6000 set_nonincremental_init ()
6002 tree chain;
6004 if (TREE_CODE (constructor_type) != RECORD_TYPE
6005 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6006 return;
6008 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
6009 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
6010 constructor_elements = 0;
6011 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6013 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6014 /* Skip any nameless bit fields at the beginning. */
6015 while (constructor_unfilled_fields != 0
6016 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6017 && DECL_NAME (constructor_unfilled_fields) == 0)
6018 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6021 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6023 if (TYPE_DOMAIN (constructor_type))
6024 constructor_unfilled_index
6025 = convert (bitsizetype,
6026 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6027 else
6028 constructor_unfilled_index = bitsize_zero_node;
6030 constructor_incremental = 0;
6033 /* Build AVL tree from a string constant. */
6035 static void
6036 set_nonincremental_init_from_string (str)
6037 tree str;
6039 tree value, purpose, type;
6040 HOST_WIDE_INT val[2];
6041 const char *p, *end;
6042 int byte, wchar_bytes, charwidth, bitpos;
6044 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6045 abort ();
6047 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6048 == TYPE_PRECISION (char_type_node))
6049 wchar_bytes = 1;
6050 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6051 == TYPE_PRECISION (wchar_type_node))
6052 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6053 else
6054 abort ();
6056 charwidth = TYPE_PRECISION (char_type_node);
6057 type = TREE_TYPE (constructor_type);
6058 p = TREE_STRING_POINTER (str);
6059 end = p + TREE_STRING_LENGTH (str);
6061 for (purpose = bitsize_zero_node;
6062 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6063 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6065 if (wchar_bytes == 1)
6067 val[1] = (unsigned char) *p++;
6068 val[0] = 0;
6070 else
6072 val[0] = 0;
6073 val[1] = 0;
6074 for (byte = 0; byte < wchar_bytes; byte++)
6076 if (BYTES_BIG_ENDIAN)
6077 bitpos = (wchar_bytes - byte - 1) * charwidth;
6078 else
6079 bitpos = byte * charwidth;
6080 val[bitpos < HOST_BITS_PER_WIDE_INT]
6081 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6082 << (bitpos % HOST_BITS_PER_WIDE_INT);
6086 if (!TREE_UNSIGNED (type))
6088 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6089 if (bitpos < HOST_BITS_PER_WIDE_INT)
6091 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6093 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6094 val[0] = -1;
6097 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6099 if (val[1] < 0)
6100 val[0] = -1;
6102 else if (val[0] & (((HOST_WIDE_INT) 1)
6103 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6104 val[0] |= ((HOST_WIDE_INT) -1)
6105 << (bitpos - HOST_BITS_PER_WIDE_INT);
6108 value = build_int_2 (val[1], val[0]);
6109 TREE_TYPE (value) = type;
6110 add_pending_init (purpose, value);
6113 constructor_incremental = 0;
6116 /* Return value of FIELD in pending initializer or zero if the field was
6117 not initialized yet. */
6119 static tree
6120 find_init_member (field)
6121 tree field;
6123 struct init_node *p;
6125 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6127 if (constructor_incremental
6128 && tree_int_cst_lt (field, constructor_unfilled_index))
6129 set_nonincremental_init ();
6131 p = constructor_pending_elts;
6132 while (p)
6134 if (tree_int_cst_lt (field, p->purpose))
6135 p = p->left;
6136 else if (tree_int_cst_lt (p->purpose, field))
6137 p = p->right;
6138 else
6139 return p->value;
6142 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6144 tree bitpos = bit_position (field);
6146 if (constructor_incremental
6147 && (!constructor_unfilled_fields
6148 || tree_int_cst_lt (bitpos,
6149 bit_position (constructor_unfilled_fields))))
6150 set_nonincremental_init ();
6152 p = constructor_pending_elts;
6153 while (p)
6155 if (field == p->purpose)
6156 return p->value;
6157 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6158 p = p->left;
6159 else
6160 p = p->right;
6163 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6165 if (constructor_elements
6166 && TREE_PURPOSE (constructor_elements) == field)
6167 return TREE_VALUE (constructor_elements);
6169 return 0;
6172 /* "Output" the next constructor element.
6173 At top level, really output it to assembler code now.
6174 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6175 TYPE is the data type that the containing data type wants here.
6176 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6178 PENDING if non-nil means output pending elements that belong
6179 right after this element. (PENDING is normally 1;
6180 it is 0 while outputting pending elements, to avoid recursion.) */
6182 static void
6183 output_init_element (value, type, field, pending)
6184 tree value, type, field;
6185 int pending;
6187 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6188 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6189 && !(TREE_CODE (value) == STRING_CST
6190 && TREE_CODE (type) == ARRAY_TYPE
6191 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6192 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6193 TYPE_MAIN_VARIANT (type))))
6194 value = default_conversion (value);
6196 if (value == error_mark_node)
6197 constructor_erroneous = 1;
6198 else if (!TREE_CONSTANT (value))
6199 constructor_constant = 0;
6200 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6201 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6202 || TREE_CODE (constructor_type) == UNION_TYPE)
6203 && DECL_C_BIT_FIELD (field)
6204 && TREE_CODE (value) != INTEGER_CST))
6205 constructor_simple = 0;
6207 if (require_constant_value && ! TREE_CONSTANT (value))
6209 error_init ("initializer element is not constant");
6210 value = error_mark_node;
6212 else if (require_constant_elements
6213 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6214 pedwarn ("initializer element is not computable at load time");
6216 /* If this field is empty (and not at the end of structure),
6217 don't do anything other than checking the initializer. */
6218 if (field
6219 && (TREE_TYPE (field) == error_mark_node
6220 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6221 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6222 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6223 || TREE_CHAIN (field)))))
6224 return;
6226 if (value == error_mark_node)
6228 constructor_erroneous = 1;
6229 return;
6232 /* If this element doesn't come next in sequence,
6233 put it on constructor_pending_elts. */
6234 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6235 && (!constructor_incremental
6236 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6238 if (constructor_incremental
6239 && tree_int_cst_lt (field, constructor_unfilled_index))
6240 set_nonincremental_init ();
6242 add_pending_init (field,
6243 digest_init (type, value, require_constant_value,
6244 require_constant_elements));
6245 return;
6247 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6248 && (!constructor_incremental
6249 || field != constructor_unfilled_fields))
6251 /* We do this for records but not for unions. In a union,
6252 no matter which field is specified, it can be initialized
6253 right away since it starts at the beginning of the union. */
6254 if (constructor_incremental)
6256 if (!constructor_unfilled_fields)
6257 set_nonincremental_init ();
6258 else
6260 tree bitpos, unfillpos;
6262 bitpos = bit_position (field);
6263 unfillpos = bit_position (constructor_unfilled_fields);
6265 if (tree_int_cst_lt (bitpos, unfillpos))
6266 set_nonincremental_init ();
6270 add_pending_init (field,
6271 digest_init (type, value, require_constant_value,
6272 require_constant_elements));
6273 return;
6275 else if (TREE_CODE (constructor_type) == UNION_TYPE
6276 && constructor_elements)
6278 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
6279 warning_init ("initialized field with side-effects overwritten");
6281 /* We can have just one union field set. */
6282 constructor_elements = 0;
6285 /* Otherwise, output this element either to
6286 constructor_elements or to the assembler file. */
6288 if (field && TREE_CODE (field) == INTEGER_CST)
6289 field = copy_node (field);
6290 constructor_elements
6291 = tree_cons (field, digest_init (type, value,
6292 require_constant_value,
6293 require_constant_elements),
6294 constructor_elements);
6296 /* Advance the variable that indicates sequential elements output. */
6297 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6298 constructor_unfilled_index
6299 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6300 bitsize_one_node);
6301 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6303 constructor_unfilled_fields
6304 = TREE_CHAIN (constructor_unfilled_fields);
6306 /* Skip any nameless bit fields. */
6307 while (constructor_unfilled_fields != 0
6308 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6309 && DECL_NAME (constructor_unfilled_fields) == 0)
6310 constructor_unfilled_fields =
6311 TREE_CHAIN (constructor_unfilled_fields);
6313 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6314 constructor_unfilled_fields = 0;
6316 /* Now output any pending elements which have become next. */
6317 if (pending)
6318 output_pending_init_elements (0);
6321 /* Output any pending elements which have become next.
6322 As we output elements, constructor_unfilled_{fields,index}
6323 advances, which may cause other elements to become next;
6324 if so, they too are output.
6326 If ALL is 0, we return when there are
6327 no more pending elements to output now.
6329 If ALL is 1, we output space as necessary so that
6330 we can output all the pending elements. */
6332 static void
6333 output_pending_init_elements (all)
6334 int all;
6336 struct init_node *elt = constructor_pending_elts;
6337 tree next;
6339 retry:
6341 /* Look thru the whole pending tree.
6342 If we find an element that should be output now,
6343 output it. Otherwise, set NEXT to the element
6344 that comes first among those still pending. */
6346 next = 0;
6347 while (elt)
6349 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6351 if (tree_int_cst_equal (elt->purpose,
6352 constructor_unfilled_index))
6353 output_init_element (elt->value,
6354 TREE_TYPE (constructor_type),
6355 constructor_unfilled_index, 0);
6356 else if (tree_int_cst_lt (constructor_unfilled_index,
6357 elt->purpose))
6359 /* Advance to the next smaller node. */
6360 if (elt->left)
6361 elt = elt->left;
6362 else
6364 /* We have reached the smallest node bigger than the
6365 current unfilled index. Fill the space first. */
6366 next = elt->purpose;
6367 break;
6370 else
6372 /* Advance to the next bigger node. */
6373 if (elt->right)
6374 elt = elt->right;
6375 else
6377 /* We have reached the biggest node in a subtree. Find
6378 the parent of it, which is the next bigger node. */
6379 while (elt->parent && elt->parent->right == elt)
6380 elt = elt->parent;
6381 elt = elt->parent;
6382 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6383 elt->purpose))
6385 next = elt->purpose;
6386 break;
6391 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6392 || TREE_CODE (constructor_type) == UNION_TYPE)
6394 tree ctor_unfilled_bitpos, elt_bitpos;
6396 /* If the current record is complete we are done. */
6397 if (constructor_unfilled_fields == 0)
6398 break;
6400 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6401 elt_bitpos = bit_position (elt->purpose);
6402 /* We can't compare fields here because there might be empty
6403 fields in between. */
6404 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6406 constructor_unfilled_fields = elt->purpose;
6407 output_init_element (elt->value, TREE_TYPE (elt->purpose),
6408 elt->purpose, 0);
6410 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6412 /* Advance to the next smaller node. */
6413 if (elt->left)
6414 elt = elt->left;
6415 else
6417 /* We have reached the smallest node bigger than the
6418 current unfilled field. Fill the space first. */
6419 next = elt->purpose;
6420 break;
6423 else
6425 /* Advance to the next bigger node. */
6426 if (elt->right)
6427 elt = elt->right;
6428 else
6430 /* We have reached the biggest node in a subtree. Find
6431 the parent of it, which is the next bigger node. */
6432 while (elt->parent && elt->parent->right == elt)
6433 elt = elt->parent;
6434 elt = elt->parent;
6435 if (elt
6436 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6437 bit_position (elt->purpose))))
6439 next = elt->purpose;
6440 break;
6447 /* Ordinarily return, but not if we want to output all
6448 and there are elements left. */
6449 if (! (all && next != 0))
6450 return;
6452 /* If it's not incremental, just skip over the gap, so that after
6453 jumping to retry we will output the next successive element. */
6454 if (TREE_CODE (constructor_type) == RECORD_TYPE
6455 || TREE_CODE (constructor_type) == UNION_TYPE)
6456 constructor_unfilled_fields = next;
6457 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6458 constructor_unfilled_index = next;
6460 /* ELT now points to the node in the pending tree with the next
6461 initializer to output. */
6462 goto retry;
6465 /* Add one non-braced element to the current constructor level.
6466 This adjusts the current position within the constructor's type.
6467 This may also start or terminate implicit levels
6468 to handle a partly-braced initializer.
6470 Once this has found the correct level for the new element,
6471 it calls output_init_element. */
6473 void
6474 process_init_element (value)
6475 tree value;
6477 tree orig_value = value;
6478 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6480 designator_depth = 0;
6481 designator_errorneous = 0;
6483 /* Handle superfluous braces around string cst as in
6484 char x[] = {"foo"}; */
6485 if (string_flag
6486 && constructor_type
6487 && TREE_CODE (constructor_type) == ARRAY_TYPE
6488 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6489 && integer_zerop (constructor_unfilled_index))
6491 if (constructor_stack->replacement_value)
6492 error_init ("excess elements in char array initializer");
6493 constructor_stack->replacement_value = value;
6494 return;
6497 if (constructor_stack->replacement_value != 0)
6499 error_init ("excess elements in struct initializer");
6500 return;
6503 /* Ignore elements of a brace group if it is entirely superfluous
6504 and has already been diagnosed. */
6505 if (constructor_type == 0)
6506 return;
6508 /* If we've exhausted any levels that didn't have braces,
6509 pop them now. */
6510 while (constructor_stack->implicit)
6512 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6513 || TREE_CODE (constructor_type) == UNION_TYPE)
6514 && constructor_fields == 0)
6515 process_init_element (pop_init_level (1));
6516 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6517 && (constructor_max_index == 0
6518 || tree_int_cst_lt (constructor_max_index,
6519 constructor_index)))
6520 process_init_element (pop_init_level (1));
6521 else
6522 break;
6525 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6526 if (constructor_range_stack)
6527 value = save_expr (value);
6529 while (1)
6531 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6533 tree fieldtype;
6534 enum tree_code fieldcode;
6536 if (constructor_fields == 0)
6538 pedwarn_init ("excess elements in struct initializer");
6539 break;
6542 fieldtype = TREE_TYPE (constructor_fields);
6543 if (fieldtype != error_mark_node)
6544 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6545 fieldcode = TREE_CODE (fieldtype);
6547 /* Accept a string constant to initialize a subarray. */
6548 if (value != 0
6549 && fieldcode == ARRAY_TYPE
6550 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6551 && string_flag)
6552 value = orig_value;
6553 /* Otherwise, if we have come to a subaggregate,
6554 and we don't have an element of its type, push into it. */
6555 else if (value != 0 && !constructor_no_implicit
6556 && value != error_mark_node
6557 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6558 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6559 || fieldcode == UNION_TYPE))
6561 push_init_level (1);
6562 continue;
6565 if (value)
6567 push_member_name (constructor_fields);
6568 output_init_element (value, fieldtype, constructor_fields, 1);
6569 RESTORE_SPELLING_DEPTH (constructor_depth);
6571 else
6572 /* Do the bookkeeping for an element that was
6573 directly output as a constructor. */
6575 /* For a record, keep track of end position of last field. */
6576 if (DECL_SIZE (constructor_fields))
6577 constructor_bit_index
6578 = size_binop (PLUS_EXPR,
6579 bit_position (constructor_fields),
6580 DECL_SIZE (constructor_fields));
6582 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6583 /* Skip any nameless bit fields. */
6584 while (constructor_unfilled_fields != 0
6585 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6586 && DECL_NAME (constructor_unfilled_fields) == 0)
6587 constructor_unfilled_fields =
6588 TREE_CHAIN (constructor_unfilled_fields);
6591 constructor_fields = TREE_CHAIN (constructor_fields);
6592 /* Skip any nameless bit fields at the beginning. */
6593 while (constructor_fields != 0
6594 && DECL_C_BIT_FIELD (constructor_fields)
6595 && DECL_NAME (constructor_fields) == 0)
6596 constructor_fields = TREE_CHAIN (constructor_fields);
6598 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6600 tree fieldtype;
6601 enum tree_code fieldcode;
6603 if (constructor_fields == 0)
6605 pedwarn_init ("excess elements in union initializer");
6606 break;
6609 fieldtype = TREE_TYPE (constructor_fields);
6610 if (fieldtype != error_mark_node)
6611 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6612 fieldcode = TREE_CODE (fieldtype);
6614 /* Warn that traditional C rejects initialization of unions.
6615 We skip the warning if the value is zero. This is done
6616 under the assumption that the zero initializer in user
6617 code appears conditioned on e.g. __STDC__ to avoid
6618 "missing initializer" warnings and relies on default
6619 initialization to zero in the traditional C case.
6620 We also skip the warning if the initializer is designated,
6621 again on the assumption that this must be conditional on
6622 __STDC__ anyway (and we've already complained about the
6623 member-designator already). */
6624 if (warn_traditional && !in_system_header && !constructor_designated
6625 && !(value && (integer_zerop (value) || real_zerop (value))))
6626 warning ("traditional C rejects initialization of unions");
6628 /* Accept a string constant to initialize a subarray. */
6629 if (value != 0
6630 && fieldcode == ARRAY_TYPE
6631 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6632 && string_flag)
6633 value = orig_value;
6634 /* Otherwise, if we have come to a subaggregate,
6635 and we don't have an element of its type, push into it. */
6636 else if (value != 0 && !constructor_no_implicit
6637 && value != error_mark_node
6638 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6639 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6640 || fieldcode == UNION_TYPE))
6642 push_init_level (1);
6643 continue;
6646 if (value)
6648 push_member_name (constructor_fields);
6649 output_init_element (value, fieldtype, constructor_fields, 1);
6650 RESTORE_SPELLING_DEPTH (constructor_depth);
6652 else
6653 /* Do the bookkeeping for an element that was
6654 directly output as a constructor. */
6656 constructor_bit_index = DECL_SIZE (constructor_fields);
6657 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6660 constructor_fields = 0;
6662 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6664 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6665 enum tree_code eltcode = TREE_CODE (elttype);
6667 /* Accept a string constant to initialize a subarray. */
6668 if (value != 0
6669 && eltcode == ARRAY_TYPE
6670 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6671 && string_flag)
6672 value = orig_value;
6673 /* Otherwise, if we have come to a subaggregate,
6674 and we don't have an element of its type, push into it. */
6675 else if (value != 0 && !constructor_no_implicit
6676 && value != error_mark_node
6677 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6678 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6679 || eltcode == UNION_TYPE))
6681 push_init_level (1);
6682 continue;
6685 if (constructor_max_index != 0
6686 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6687 || integer_all_onesp (constructor_max_index)))
6689 pedwarn_init ("excess elements in array initializer");
6690 break;
6693 /* Now output the actual element. */
6694 if (value)
6696 push_array_bounds (tree_low_cst (constructor_index, 0));
6697 output_init_element (value, elttype, constructor_index, 1);
6698 RESTORE_SPELLING_DEPTH (constructor_depth);
6701 constructor_index
6702 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6704 if (! value)
6705 /* If we are doing the bookkeeping for an element that was
6706 directly output as a constructor, we must update
6707 constructor_unfilled_index. */
6708 constructor_unfilled_index = constructor_index;
6711 /* Handle the sole element allowed in a braced initializer
6712 for a scalar variable. */
6713 else if (constructor_fields == 0)
6715 pedwarn_init ("excess elements in scalar initializer");
6716 break;
6718 else
6720 if (value)
6721 output_init_element (value, constructor_type, NULL_TREE, 1);
6722 constructor_fields = 0;
6725 /* Handle range initializers either at this level or anywhere higher
6726 in the designator stack. */
6727 if (constructor_range_stack)
6729 struct constructor_range_stack *p, *range_stack;
6730 int finish = 0;
6732 range_stack = constructor_range_stack;
6733 constructor_range_stack = 0;
6734 while (constructor_stack != range_stack->stack)
6736 if (!constructor_stack->implicit)
6737 abort ();
6738 process_init_element (pop_init_level (1));
6740 for (p = range_stack;
6741 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6742 p = p->prev)
6744 if (!constructor_stack->implicit)
6745 abort ();
6746 process_init_element (pop_init_level (1));
6749 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6750 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6751 finish = 1;
6753 while (1)
6755 constructor_index = p->index;
6756 constructor_fields = p->fields;
6757 if (finish && p->range_end && p->index == p->range_start)
6759 finish = 0;
6760 p->prev = 0;
6762 p = p->next;
6763 if (!p)
6764 break;
6765 push_init_level (2);
6766 p->stack = constructor_stack;
6767 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6768 p->index = p->range_start;
6771 if (!finish)
6772 constructor_range_stack = range_stack;
6773 continue;
6776 break;
6779 constructor_range_stack = 0;
6782 /* Build a simple asm-statement, from one string literal. */
6783 tree
6784 simple_asm_stmt (expr)
6785 tree expr;
6787 STRIP_NOPS (expr);
6789 if (TREE_CODE (expr) == ADDR_EXPR)
6790 expr = TREE_OPERAND (expr, 0);
6792 if (TREE_CODE (expr) == STRING_CST)
6794 tree stmt;
6796 if (TREE_CHAIN (expr))
6797 expr = combine_strings (expr);
6798 stmt = add_stmt (build_stmt (ASM_STMT, NULL_TREE, expr,
6799 NULL_TREE, NULL_TREE,
6800 NULL_TREE));
6801 ASM_INPUT_P (stmt) = 1;
6802 return stmt;
6805 error ("argument of `asm' is not a constant string");
6806 return NULL_TREE;
6809 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6810 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6812 tree
6813 build_asm_stmt (cv_qualifier, string, outputs, inputs, clobbers)
6814 tree cv_qualifier;
6815 tree string;
6816 tree outputs;
6817 tree inputs;
6818 tree clobbers;
6820 tree tail;
6822 if (TREE_CHAIN (string))
6823 string = combine_strings (string);
6824 if (TREE_CODE (string) != STRING_CST)
6826 error ("asm template is not a string constant");
6827 return NULL_TREE;
6830 if (cv_qualifier != NULL_TREE
6831 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6833 warning ("%s qualifier ignored on asm",
6834 IDENTIFIER_POINTER (cv_qualifier));
6835 cv_qualifier = NULL_TREE;
6838 /* We can remove output conversions that change the type,
6839 but not the mode. */
6840 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6842 tree output = TREE_VALUE (tail);
6844 STRIP_NOPS (output);
6845 TREE_VALUE (tail) = output;
6847 /* Allow conversions as LHS here. build_modify_expr as called below
6848 will do the right thing with them. */
6849 while (TREE_CODE (output) == NOP_EXPR
6850 || TREE_CODE (output) == CONVERT_EXPR
6851 || TREE_CODE (output) == FLOAT_EXPR
6852 || TREE_CODE (output) == FIX_TRUNC_EXPR
6853 || TREE_CODE (output) == FIX_FLOOR_EXPR
6854 || TREE_CODE (output) == FIX_ROUND_EXPR
6855 || TREE_CODE (output) == FIX_CEIL_EXPR)
6856 output = TREE_OPERAND (output, 0);
6858 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6861 /* Remove output conversions that change the type but not the mode. */
6862 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6864 tree output = TREE_VALUE (tail);
6865 STRIP_NOPS (output);
6866 TREE_VALUE (tail) = output;
6869 /* Perform default conversions on array and function inputs.
6870 Don't do this for other types as it would screw up operands
6871 expected to be in memory. */
6872 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6873 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6874 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6875 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6877 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6878 outputs, inputs, clobbers));
6881 /* Expand an ASM statement with operands, handling output operands
6882 that are not variables or INDIRECT_REFS by transforming such
6883 cases into cases that expand_asm_operands can handle.
6885 Arguments are same as for expand_asm_operands. */
6887 void
6888 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6889 tree string, outputs, inputs, clobbers;
6890 int vol;
6891 const char *filename;
6892 int line;
6894 int noutputs = list_length (outputs);
6895 int i;
6896 /* o[I] is the place that output number I should be written. */
6897 tree *o = (tree *) alloca (noutputs * sizeof (tree));
6898 tree tail;
6900 /* Record the contents of OUTPUTS before it is modified. */
6901 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6902 o[i] = TREE_VALUE (tail);
6904 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6905 OUTPUTS some trees for where the values were actually stored. */
6906 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6908 /* Copy all the intermediate outputs into the specified outputs. */
6909 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6911 if (o[i] != TREE_VALUE (tail))
6913 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6914 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6915 free_temp_slots ();
6917 /* Restore the original value so that it's correct the next
6918 time we expand this function. */
6919 TREE_VALUE (tail) = o[i];
6921 /* Detect modification of read-only values.
6922 (Otherwise done by build_modify_expr.) */
6923 else
6925 tree type = TREE_TYPE (o[i]);
6926 if (TREE_READONLY (o[i])
6927 || TYPE_READONLY (type)
6928 || ((TREE_CODE (type) == RECORD_TYPE
6929 || TREE_CODE (type) == UNION_TYPE)
6930 && C_TYPE_FIELDS_READONLY (type)))
6931 readonly_warning (o[i], "modification by `asm'");
6935 /* Those MODIFY_EXPRs could do autoincrements. */
6936 emit_queue ();
6939 /* Expand a C `return' statement.
6940 RETVAL is the expression for what to return,
6941 or a null pointer for `return;' with no value. */
6943 tree
6944 c_expand_return (retval)
6945 tree retval;
6947 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6949 if (TREE_THIS_VOLATILE (current_function_decl))
6950 warning ("function declared `noreturn' has a `return' statement");
6952 if (!retval)
6954 current_function_returns_null = 1;
6955 if ((warn_return_type || flag_isoc99)
6956 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6957 pedwarn_c99 ("`return' with no value, in function returning non-void");
6959 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6961 current_function_returns_null = 1;
6962 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6963 pedwarn ("`return' with a value, in function returning void");
6965 else
6967 tree t = convert_for_assignment (valtype, retval, _("return"),
6968 NULL_TREE, NULL_TREE, 0);
6969 tree res = DECL_RESULT (current_function_decl);
6970 tree inner;
6972 if (t == error_mark_node)
6973 return NULL_TREE;
6975 inner = t = convert (TREE_TYPE (res), t);
6977 /* Strip any conversions, additions, and subtractions, and see if
6978 we are returning the address of a local variable. Warn if so. */
6979 while (1)
6981 switch (TREE_CODE (inner))
6983 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6984 case PLUS_EXPR:
6985 inner = TREE_OPERAND (inner, 0);
6986 continue;
6988 case MINUS_EXPR:
6989 /* If the second operand of the MINUS_EXPR has a pointer
6990 type (or is converted from it), this may be valid, so
6991 don't give a warning. */
6993 tree op1 = TREE_OPERAND (inner, 1);
6995 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6996 && (TREE_CODE (op1) == NOP_EXPR
6997 || TREE_CODE (op1) == NON_LVALUE_EXPR
6998 || TREE_CODE (op1) == CONVERT_EXPR))
6999 op1 = TREE_OPERAND (op1, 0);
7001 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7002 break;
7004 inner = TREE_OPERAND (inner, 0);
7005 continue;
7008 case ADDR_EXPR:
7009 inner = TREE_OPERAND (inner, 0);
7011 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
7012 inner = TREE_OPERAND (inner, 0);
7014 if (TREE_CODE (inner) == VAR_DECL
7015 && ! DECL_EXTERNAL (inner)
7016 && ! TREE_STATIC (inner)
7017 && DECL_CONTEXT (inner) == current_function_decl)
7018 warning ("function returns address of local variable");
7019 break;
7021 default:
7022 break;
7025 break;
7028 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
7029 current_function_returns_value = 1;
7032 return add_stmt (build_return_stmt (retval));
7035 struct c_switch {
7036 /* The SWITCH_STMT being built. */
7037 tree switch_stmt;
7038 /* A splay-tree mapping the low element of a case range to the high
7039 element, or NULL_TREE if there is no high element. Used to
7040 determine whether or not a new case label duplicates an old case
7041 label. We need a tree, rather than simply a hash table, because
7042 of the GNU case range extension. */
7043 splay_tree cases;
7044 /* The next node on the stack. */
7045 struct c_switch *next;
7048 /* A stack of the currently active switch statements. The innermost
7049 switch statement is on the top of the stack. There is no need to
7050 mark the stack for garbage collection because it is only active
7051 during the processing of the body of a function, and we never
7052 collect at that point. */
7054 static struct c_switch *switch_stack;
7056 /* Start a C switch statement, testing expression EXP. Return the new
7057 SWITCH_STMT. */
7059 tree
7060 c_start_case (exp)
7061 tree exp;
7063 enum tree_code code;
7064 tree type;
7065 struct c_switch *cs;
7067 if (exp != error_mark_node)
7069 code = TREE_CODE (TREE_TYPE (exp));
7070 type = TREE_TYPE (exp);
7072 if (! INTEGRAL_TYPE_P (type)
7073 && code != ERROR_MARK)
7075 error ("switch quantity not an integer");
7076 exp = integer_zero_node;
7078 else
7080 tree index;
7081 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7083 if (warn_traditional && !in_system_header
7084 && (type == long_integer_type_node
7085 || type == long_unsigned_type_node))
7086 warning ("`long' switch expression not converted to `int' in ISO C");
7088 exp = default_conversion (exp);
7089 type = TREE_TYPE (exp);
7090 index = get_unwidened (exp, NULL_TREE);
7091 /* We can't strip a conversion from a signed type to an
7092 unsigned, because if we did, int_fits_type_p would do the
7093 wrong thing when checking case values for being in range,
7094 and it's too hard to do the right thing. */
7095 if (TREE_UNSIGNED (TREE_TYPE (exp))
7096 == TREE_UNSIGNED (TREE_TYPE (index)))
7097 exp = index;
7101 /* Add this new SWITCH_STMT to the stack. */
7102 cs = (struct c_switch *) xmalloc (sizeof (*cs));
7103 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, NULL_TREE);
7104 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7105 cs->next = switch_stack;
7106 switch_stack = cs;
7108 return add_stmt (switch_stack->switch_stmt);
7111 /* Process a case label. */
7113 tree
7114 do_case (low_value, high_value)
7115 tree low_value;
7116 tree high_value;
7118 tree label = NULL_TREE;
7120 if (switch_stack)
7122 label = c_add_case_label (switch_stack->cases,
7123 SWITCH_COND (switch_stack->switch_stmt),
7124 low_value, high_value);
7125 if (label == error_mark_node)
7126 label = NULL_TREE;
7128 else if (low_value)
7129 error ("case label not within a switch statement");
7130 else
7131 error ("`default' label not within a switch statement");
7133 return label;
7136 /* Finish the switch statement. */
7138 void
7139 c_finish_case ()
7141 struct c_switch *cs = switch_stack;
7143 RECHAIN_STMTS (cs->switch_stmt, SWITCH_BODY (cs->switch_stmt));
7145 /* Pop the stack. */
7146 switch_stack = switch_stack->next;
7147 splay_tree_delete (cs->cases);
7148 free (cs);