cfglayout.c (fixup_fallthru_exit_predecesor): Make void, rename to ....
[official-gcc.git] / gcc / c-typeck.c
blob64f0f07d89ad6e34e657cf79d3fa9c8768c8336a
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 default_function_array_conversion PARAMS ((tree));
59 static tree lookup_field PARAMS ((tree, tree));
60 static tree convert_arguments PARAMS ((tree, tree, tree, tree));
61 static tree pointer_int_sum PARAMS ((enum tree_code, tree, tree));
62 static tree pointer_diff PARAMS ((tree, tree));
63 static tree unary_complex_lvalue PARAMS ((enum tree_code, tree, int));
64 static void pedantic_lvalue_warning PARAMS ((enum tree_code));
65 static tree internal_build_compound_expr PARAMS ((tree, int));
66 static tree convert_for_assignment PARAMS ((tree, tree, const char *,
67 tree, tree, int));
68 static void warn_for_assignment PARAMS ((const char *, const char *,
69 tree, int));
70 static tree valid_compound_expr_initializer PARAMS ((tree, tree));
71 static void push_string PARAMS ((const char *));
72 static void push_member_name PARAMS ((tree));
73 static void push_array_bounds PARAMS ((int));
74 static int spelling_length PARAMS ((void));
75 static char *print_spelling PARAMS ((char *));
76 static void warning_init PARAMS ((const char *));
77 static tree digest_init PARAMS ((tree, tree, int, int));
78 static void output_init_element PARAMS ((tree, tree, tree, int));
79 static void output_pending_init_elements PARAMS ((int));
80 static int set_designator PARAMS ((int));
81 static void push_range_stack PARAMS ((tree));
82 static void add_pending_init PARAMS ((tree, tree));
83 static void set_nonincremental_init PARAMS ((void));
84 static void set_nonincremental_init_from_string PARAMS ((tree));
85 static tree find_init_member PARAMS ((tree));
87 /* Do `exp = require_complete_type (exp);' to make sure exp
88 does not have an incomplete type. (That includes void types.) */
90 tree
91 require_complete_type (value)
92 tree value;
94 tree type = TREE_TYPE (value);
96 if (TREE_CODE (value) == ERROR_MARK)
97 return error_mark_node;
99 /* First, detect a valid value with a complete type. */
100 if (COMPLETE_TYPE_P (type))
101 return value;
103 incomplete_type_error (value, type);
104 return error_mark_node;
107 /* Print an error message for invalid use of an incomplete type.
108 VALUE is the expression that was used (or 0 if that isn't known)
109 and TYPE is the type that was invalid. */
111 void
112 incomplete_type_error (value, type)
113 tree value;
114 tree type;
116 const char *type_code_string;
118 /* Avoid duplicate error message. */
119 if (TREE_CODE (type) == ERROR_MARK)
120 return;
122 if (value != 0 && (TREE_CODE (value) == VAR_DECL
123 || TREE_CODE (value) == PARM_DECL))
124 error ("`%s' has an incomplete type",
125 IDENTIFIER_POINTER (DECL_NAME (value)));
126 else
128 retry:
129 /* We must print an error message. Be clever about what it says. */
131 switch (TREE_CODE (type))
133 case RECORD_TYPE:
134 type_code_string = "struct";
135 break;
137 case UNION_TYPE:
138 type_code_string = "union";
139 break;
141 case ENUMERAL_TYPE:
142 type_code_string = "enum";
143 break;
145 case VOID_TYPE:
146 error ("invalid use of void expression");
147 return;
149 case ARRAY_TYPE:
150 if (TYPE_DOMAIN (type))
152 type = TREE_TYPE (type);
153 goto retry;
155 error ("invalid use of array with unspecified bounds");
156 return;
158 default:
159 abort ();
162 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
163 error ("invalid use of undefined type `%s %s'",
164 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
165 else
166 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
167 error ("invalid use of incomplete typedef `%s'",
168 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
172 /* Return a variant of TYPE which has all the type qualifiers of LIKE
173 as well as those of TYPE. */
175 static tree
176 qualify_type (type, like)
177 tree type, like;
179 return c_build_qualified_type (type,
180 TYPE_QUALS (type) | TYPE_QUALS (like));
183 /* Return the common type of two types.
184 We assume that comptypes has already been done and returned 1;
185 if that isn't so, this may crash. In particular, we assume that qualifiers
186 match.
188 This is the type for the result of most arithmetic operations
189 if the operands have the given two types. */
191 tree
192 common_type (t1, t2)
193 tree t1, t2;
195 enum tree_code code1;
196 enum tree_code code2;
197 tree attributes;
199 /* Save time if the two types are the same. */
201 if (t1 == t2) return t1;
203 /* If one type is nonsense, use the other. */
204 if (t1 == error_mark_node)
205 return t2;
206 if (t2 == error_mark_node)
207 return t1;
209 /* Merge the attributes. */
210 attributes = (*targetm.merge_type_attributes) (t1, t2);
212 /* Treat an enum type as the unsigned integer type of the same width. */
214 if (TREE_CODE (t1) == ENUMERAL_TYPE)
215 t1 = type_for_size (TYPE_PRECISION (t1), 1);
216 if (TREE_CODE (t2) == ENUMERAL_TYPE)
217 t2 = type_for_size (TYPE_PRECISION (t2), 1);
219 code1 = TREE_CODE (t1);
220 code2 = TREE_CODE (t2);
222 /* If one type is complex, form the common type of the non-complex
223 components, then make that complex. Use T1 or T2 if it is the
224 required type. */
225 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
227 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
228 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
229 tree subtype = common_type (subtype1, subtype2);
231 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
232 return build_type_attribute_variant (t1, attributes);
233 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
234 return build_type_attribute_variant (t2, attributes);
235 else
236 return build_type_attribute_variant (build_complex_type (subtype),
237 attributes);
240 switch (code1)
242 case INTEGER_TYPE:
243 case REAL_TYPE:
244 /* If only one is real, use it as the result. */
246 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
247 return build_type_attribute_variant (t1, attributes);
249 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
250 return build_type_attribute_variant (t2, attributes);
252 /* Both real or both integers; use the one with greater precision. */
254 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
255 return build_type_attribute_variant (t1, attributes);
256 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
257 return build_type_attribute_variant (t2, attributes);
259 /* Same precision. Prefer longs to ints even when same size. */
261 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
262 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
263 return build_type_attribute_variant (long_unsigned_type_node,
264 attributes);
266 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
267 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
269 /* But preserve unsignedness from the other type,
270 since long cannot hold all the values of an unsigned int. */
271 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
272 t1 = long_unsigned_type_node;
273 else
274 t1 = long_integer_type_node;
275 return build_type_attribute_variant (t1, attributes);
278 /* Likewise, prefer long double to double even if same size. */
279 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
280 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
281 return build_type_attribute_variant (long_double_type_node,
282 attributes);
284 /* Otherwise prefer the unsigned one. */
286 if (TREE_UNSIGNED (t1))
287 return build_type_attribute_variant (t1, attributes);
288 else
289 return build_type_attribute_variant (t2, attributes);
291 case POINTER_TYPE:
292 /* For two pointers, do this recursively on the target type,
293 and combine the qualifiers of the two types' targets. */
294 /* This code was turned off; I don't know why.
295 But ANSI C specifies doing this with the qualifiers.
296 So I turned it on again. */
298 tree pointed_to_1 = TREE_TYPE (t1);
299 tree pointed_to_2 = TREE_TYPE (t2);
300 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
301 TYPE_MAIN_VARIANT (pointed_to_2));
302 t1 = build_pointer_type (c_build_qualified_type
303 (target,
304 TYPE_QUALS (pointed_to_1) |
305 TYPE_QUALS (pointed_to_2)));
306 return build_type_attribute_variant (t1, attributes);
308 #if 0
309 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
310 return build_type_attribute_variant (t1, attributes);
311 #endif
313 case ARRAY_TYPE:
315 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
316 /* Save space: see if the result is identical to one of the args. */
317 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
318 return build_type_attribute_variant (t1, attributes);
319 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
320 return build_type_attribute_variant (t2, attributes);
321 /* Merge the element types, and have a size if either arg has one. */
322 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
323 return build_type_attribute_variant (t1, attributes);
326 case FUNCTION_TYPE:
327 /* Function types: prefer the one that specified arg types.
328 If both do, merge the arg types. Also merge the return types. */
330 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
331 tree p1 = TYPE_ARG_TYPES (t1);
332 tree p2 = TYPE_ARG_TYPES (t2);
333 int len;
334 tree newargs, n;
335 int i;
337 /* Save space: see if the result is identical to one of the args. */
338 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
339 return build_type_attribute_variant (t1, attributes);
340 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
341 return build_type_attribute_variant (t2, attributes);
343 /* Simple way if one arg fails to specify argument types. */
344 if (TYPE_ARG_TYPES (t1) == 0)
346 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
347 return build_type_attribute_variant (t1, attributes);
349 if (TYPE_ARG_TYPES (t2) == 0)
351 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
352 return build_type_attribute_variant (t1, attributes);
355 /* If both args specify argument types, we must merge the two
356 lists, argument by argument. */
358 pushlevel (0);
359 declare_parm_level (1);
361 len = list_length (p1);
362 newargs = 0;
364 for (i = 0; i < len; i++)
365 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
367 n = newargs;
369 for (; p1;
370 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
372 /* A null type means arg type is not specified.
373 Take whatever the other function type has. */
374 if (TREE_VALUE (p1) == 0)
376 TREE_VALUE (n) = TREE_VALUE (p2);
377 goto parm_done;
379 if (TREE_VALUE (p2) == 0)
381 TREE_VALUE (n) = TREE_VALUE (p1);
382 goto parm_done;
385 /* Given wait (union {union wait *u; int *i} *)
386 and wait (union wait *),
387 prefer union wait * as type of parm. */
388 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
389 && TREE_VALUE (p1) != TREE_VALUE (p2))
391 tree memb;
392 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
393 memb; memb = TREE_CHAIN (memb))
394 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
396 TREE_VALUE (n) = TREE_VALUE (p2);
397 if (pedantic)
398 pedwarn ("function types not truly compatible in ISO C");
399 goto parm_done;
402 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
403 && TREE_VALUE (p2) != TREE_VALUE (p1))
405 tree memb;
406 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
407 memb; memb = TREE_CHAIN (memb))
408 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
410 TREE_VALUE (n) = TREE_VALUE (p1);
411 if (pedantic)
412 pedwarn ("function types not truly compatible in ISO C");
413 goto parm_done;
416 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
417 parm_done: ;
420 poplevel (0, 0, 0);
422 t1 = build_function_type (valtype, newargs);
423 /* ... falls through ... */
426 default:
427 return build_type_attribute_variant (t1, attributes);
432 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
433 or various other operations. Return 2 if they are compatible
434 but a warning may be needed if you use them together. */
437 comptypes (type1, type2)
438 tree type1, type2;
440 tree t1 = type1;
441 tree t2 = type2;
442 int attrval, val;
444 /* Suppress errors caused by previously reported errors. */
446 if (t1 == t2 || !t1 || !t2
447 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
448 return 1;
450 /* If either type is the internal version of sizetype, return the
451 language version. */
452 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
453 && TYPE_DOMAIN (t1) != 0)
454 t1 = TYPE_DOMAIN (t1);
456 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
457 && TYPE_DOMAIN (t2) != 0)
458 t2 = TYPE_DOMAIN (t2);
460 /* Treat an enum type as the integer type of the same width and
461 signedness. */
463 if (TREE_CODE (t1) == ENUMERAL_TYPE)
464 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
465 if (TREE_CODE (t2) == ENUMERAL_TYPE)
466 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
468 if (t1 == t2)
469 return 1;
471 /* Different classes of types can't be compatible. */
473 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
475 /* Qualifiers must match. */
477 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
478 return 0;
480 /* Allow for two different type nodes which have essentially the same
481 definition. Note that we already checked for equality of the type
482 qualifiers (just above). */
484 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
485 return 1;
487 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
488 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
489 return 0;
491 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
492 val = 0;
494 switch (TREE_CODE (t1))
496 case POINTER_TYPE:
497 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
498 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
499 break;
501 case FUNCTION_TYPE:
502 val = function_types_compatible_p (t1, t2);
503 break;
505 case ARRAY_TYPE:
507 tree d1 = TYPE_DOMAIN (t1);
508 tree d2 = TYPE_DOMAIN (t2);
509 bool d1_variable, d2_variable;
510 bool d1_zero, d2_zero;
511 val = 1;
513 /* Target types must match incl. qualifiers. */
514 if (TREE_TYPE (t1) != TREE_TYPE (t2)
515 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
516 return 0;
518 /* Sizes must match unless one is missing or variable. */
519 if (d1 == 0 || d2 == 0 || d1 == d2)
520 break;
522 d1_zero = ! TYPE_MAX_VALUE (d1);
523 d2_zero = ! TYPE_MAX_VALUE (d2);
525 d1_variable = (! d1_zero
526 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
527 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
528 d2_variable = (! d2_zero
529 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
530 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
532 if (d1_variable || d2_variable)
533 break;
534 if (d1_zero && d2_zero)
535 break;
536 if (d1_zero || d2_zero
537 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
538 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
539 val = 0;
541 break;
544 case RECORD_TYPE:
545 if (maybe_objc_comptypes (t1, t2, 0) == 1)
546 val = 1;
547 break;
549 default:
550 break;
552 return attrval == 2 && val == 1 ? 2 : val;
555 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
556 ignoring their qualifiers. */
558 static int
559 comp_target_types (ttl, ttr)
560 tree ttl, ttr;
562 int val;
564 /* Give maybe_objc_comptypes a crack at letting these types through. */
565 if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0)
566 return val;
568 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
569 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
571 if (val == 2 && pedantic)
572 pedwarn ("types are not quite compatible");
573 return val;
576 /* Subroutines of `comptypes'. */
578 /* Return 1 if two function types F1 and F2 are compatible.
579 If either type specifies no argument types,
580 the other must specify a fixed number of self-promoting arg types.
581 Otherwise, if one type specifies only the number of arguments,
582 the other must specify that number of self-promoting arg types.
583 Otherwise, the argument types must match. */
585 static int
586 function_types_compatible_p (f1, f2)
587 tree f1, f2;
589 tree args1, args2;
590 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
591 int val = 1;
592 int val1;
594 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
595 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
596 return 0;
598 args1 = TYPE_ARG_TYPES (f1);
599 args2 = TYPE_ARG_TYPES (f2);
601 /* An unspecified parmlist matches any specified parmlist
602 whose argument types don't need default promotions. */
604 if (args1 == 0)
606 if (!self_promoting_args_p (args2))
607 return 0;
608 /* If one of these types comes from a non-prototype fn definition,
609 compare that with the other type's arglist.
610 If they don't match, ask for a warning (but no error). */
611 if (TYPE_ACTUAL_ARG_TYPES (f1)
612 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
613 val = 2;
614 return val;
616 if (args2 == 0)
618 if (!self_promoting_args_p (args1))
619 return 0;
620 if (TYPE_ACTUAL_ARG_TYPES (f2)
621 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
622 val = 2;
623 return val;
626 /* Both types have argument lists: compare them and propagate results. */
627 val1 = type_lists_compatible_p (args1, args2);
628 return val1 != 1 ? val1 : val;
631 /* Check two lists of types for compatibility,
632 returning 0 for incompatible, 1 for compatible,
633 or 2 for compatible with warning. */
635 static int
636 type_lists_compatible_p (args1, args2)
637 tree args1, args2;
639 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
640 int val = 1;
641 int newval = 0;
643 while (1)
645 if (args1 == 0 && args2 == 0)
646 return val;
647 /* If one list is shorter than the other,
648 they fail to match. */
649 if (args1 == 0 || args2 == 0)
650 return 0;
651 /* A null pointer instead of a type
652 means there is supposed to be an argument
653 but nothing is specified about what type it has.
654 So match anything that self-promotes. */
655 if (TREE_VALUE (args1) == 0)
657 if (simple_type_promotes_to (TREE_VALUE (args2)) != NULL_TREE)
658 return 0;
660 else if (TREE_VALUE (args2) == 0)
662 if (simple_type_promotes_to (TREE_VALUE (args1)) != NULL_TREE)
663 return 0;
665 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
666 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
668 /* Allow wait (union {union wait *u; int *i} *)
669 and wait (union wait *) to be compatible. */
670 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
671 && (TYPE_NAME (TREE_VALUE (args1)) == 0
672 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
673 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
674 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
675 TYPE_SIZE (TREE_VALUE (args2))))
677 tree memb;
678 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
679 memb; memb = TREE_CHAIN (memb))
680 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
681 break;
682 if (memb == 0)
683 return 0;
685 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
686 && (TYPE_NAME (TREE_VALUE (args2)) == 0
687 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
688 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
689 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
690 TYPE_SIZE (TREE_VALUE (args1))))
692 tree memb;
693 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
694 memb; memb = TREE_CHAIN (memb))
695 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
696 break;
697 if (memb == 0)
698 return 0;
700 else
701 return 0;
704 /* comptypes said ok, but record if it said to warn. */
705 if (newval > val)
706 val = newval;
708 args1 = TREE_CHAIN (args1);
709 args2 = TREE_CHAIN (args2);
713 /* Compute the value of the `sizeof' operator. */
715 tree
716 c_sizeof (type)
717 tree type;
719 enum tree_code code = TREE_CODE (type);
720 tree size;
722 if (code == FUNCTION_TYPE)
724 if (pedantic || warn_pointer_arith)
725 pedwarn ("sizeof applied to a function type");
726 size = size_one_node;
728 else if (code == VOID_TYPE)
730 if (pedantic || warn_pointer_arith)
731 pedwarn ("sizeof applied to a void type");
732 size = size_one_node;
734 else if (code == ERROR_MARK)
735 size = size_one_node;
736 else if (!COMPLETE_TYPE_P (type))
738 error ("sizeof applied to an incomplete type");
739 size = size_zero_node;
741 else
742 /* Convert in case a char is more than one unit. */
743 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
744 size_int (TYPE_PRECISION (char_type_node)
745 / BITS_PER_UNIT));
747 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
748 TYPE_IS_SIZETYPE means that certain things (like overflow) will
749 never happen. However, this node should really have type
750 `size_t', which is just a typedef for an ordinary integer type. */
751 return fold (build1 (NOP_EXPR, c_size_type_node, size));
754 tree
755 c_sizeof_nowarn (type)
756 tree type;
758 enum tree_code code = TREE_CODE (type);
759 tree size;
761 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
762 size = size_one_node;
763 else if (!COMPLETE_TYPE_P (type))
764 size = size_zero_node;
765 else
766 /* Convert in case a char is more than one unit. */
767 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
768 size_int (TYPE_PRECISION (char_type_node)
769 / BITS_PER_UNIT));
771 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
772 TYPE_IS_SIZETYPE means that certain things (like overflow) will
773 never happen. However, this node should really have type
774 `size_t', which is just a typedef for an ordinary integer type. */
775 return fold (build1 (NOP_EXPR, c_size_type_node, size));
778 /* Compute the size to increment a pointer by. */
780 tree
781 c_size_in_bytes (type)
782 tree type;
784 enum tree_code code = TREE_CODE (type);
786 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
787 return size_one_node;
789 if (!COMPLETE_OR_VOID_TYPE_P (type))
791 error ("arithmetic on pointer to an incomplete type");
792 return size_one_node;
795 /* Convert in case a char is more than one unit. */
796 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
797 size_int (TYPE_PRECISION (char_type_node)
798 / BITS_PER_UNIT));
801 /* Return either DECL or its known constant value (if it has one). */
803 tree
804 decl_constant_value (decl)
805 tree decl;
807 if (/* Don't change a variable array bound or initial value to a constant
808 in a place where a variable is invalid. */
809 current_function_decl != 0
810 && ! TREE_THIS_VOLATILE (decl)
811 && TREE_READONLY (decl)
812 && DECL_INITIAL (decl) != 0
813 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
814 /* This is invalid if initial value is not constant.
815 If it has either a function call, a memory reference,
816 or a variable, then re-evaluating it could give different results. */
817 && TREE_CONSTANT (DECL_INITIAL (decl))
818 /* Check for cases where this is sub-optimal, even though valid. */
819 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
820 return DECL_INITIAL (decl);
821 return decl;
824 /* Return either DECL or its known constant value (if it has one), but
825 return DECL if pedantic or DECL has mode BLKmode. This is for
826 bug-compatibility with the old behavior of decl_constant_value
827 (before GCC 3.0); every use of this function is a bug and it should
828 be removed before GCC 3.1. It is not appropriate to use pedantic
829 in a way that affects optimization, and BLKmode is probably not the
830 right test for avoiding misoptimizations either. */
832 static tree
833 decl_constant_value_for_broken_optimization (decl)
834 tree decl;
836 if (pedantic || DECL_MODE (decl) == BLKmode)
837 return decl;
838 else
839 return decl_constant_value (decl);
843 /* Perform the default conversion of arrays and functions to pointers.
844 Return the result of converting EXP. For any other expression, just
845 return EXP. */
847 static tree
848 default_function_array_conversion (exp)
849 tree exp;
851 tree orig_exp;
852 tree type = TREE_TYPE (exp);
853 enum tree_code code = TREE_CODE (type);
854 int not_lvalue = 0;
856 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
857 an lvalue.
859 Do not use STRIP_NOPS here! It will remove conversions from pointer
860 to integer and cause infinite recursion. */
861 orig_exp = exp;
862 while (TREE_CODE (exp) == NON_LVALUE_EXPR
863 || (TREE_CODE (exp) == NOP_EXPR
864 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
866 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
867 not_lvalue = 1;
868 exp = TREE_OPERAND (exp, 0);
871 /* Preserve the original expression code. */
872 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
873 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
875 if (code == FUNCTION_TYPE)
877 return build_unary_op (ADDR_EXPR, exp, 0);
879 if (code == ARRAY_TYPE)
881 tree adr;
882 tree restype = TREE_TYPE (type);
883 tree ptrtype;
884 int constp = 0;
885 int volatilep = 0;
886 int lvalue_array_p;
888 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
890 constp = TREE_READONLY (exp);
891 volatilep = TREE_THIS_VOLATILE (exp);
894 if (TYPE_QUALS (type) || constp || volatilep)
895 restype
896 = c_build_qualified_type (restype,
897 TYPE_QUALS (type)
898 | (constp * TYPE_QUAL_CONST)
899 | (volatilep * TYPE_QUAL_VOLATILE));
901 if (TREE_CODE (exp) == INDIRECT_REF)
902 return convert (TYPE_POINTER_TO (restype),
903 TREE_OPERAND (exp, 0));
905 if (TREE_CODE (exp) == COMPOUND_EXPR)
907 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
908 return build (COMPOUND_EXPR, TREE_TYPE (op1),
909 TREE_OPERAND (exp, 0), op1);
912 lvalue_array_p = !not_lvalue && lvalue_p (exp);
913 if (!flag_isoc99 && !lvalue_array_p
914 && !(TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
916 /* Before C99, non-lvalue arrays do not decay to pointers.
917 Normally, using such an array would be invalid; but it can
918 be used correctly inside sizeof or as a statement expression.
919 Thus, do not give an error here; an error will result later. */
920 return exp;
923 ptrtype = build_pointer_type (restype);
925 if (TREE_CODE (exp) == VAR_DECL)
927 /* ??? This is not really quite correct
928 in that the type of the operand of ADDR_EXPR
929 is not the target type of the type of the ADDR_EXPR itself.
930 Question is, can this lossage be avoided? */
931 adr = build1 (ADDR_EXPR, ptrtype, exp);
932 if (mark_addressable (exp) == 0)
933 return error_mark_node;
934 TREE_CONSTANT (adr) = staticp (exp);
935 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
936 return adr;
938 /* This way is better for a COMPONENT_REF since it can
939 simplify the offset for a component. */
940 adr = build_unary_op (ADDR_EXPR, exp, 1);
941 return convert (ptrtype, adr);
943 return exp;
946 /* Perform default promotions for C data used in expressions.
947 Arrays and functions are converted to pointers;
948 enumeral types or short or char, to int.
949 In addition, manifest constants symbols are replaced by their values. */
951 tree
952 default_conversion (exp)
953 tree exp;
955 tree orig_exp;
956 tree type = TREE_TYPE (exp);
957 enum tree_code code = TREE_CODE (type);
959 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
960 return default_function_array_conversion (exp);
962 /* Constants can be used directly unless they're not loadable. */
963 if (TREE_CODE (exp) == CONST_DECL)
964 exp = DECL_INITIAL (exp);
966 /* Replace a nonvolatile const static variable with its value unless
967 it is an array, in which case we must be sure that taking the
968 address of the array produces consistent results. */
969 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
971 exp = decl_constant_value_for_broken_optimization (exp);
972 type = TREE_TYPE (exp);
975 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
976 an lvalue.
978 Do not use STRIP_NOPS here! It will remove conversions from pointer
979 to integer and cause infinite recursion. */
980 orig_exp = exp;
981 while (TREE_CODE (exp) == NON_LVALUE_EXPR
982 || (TREE_CODE (exp) == NOP_EXPR
983 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
984 exp = TREE_OPERAND (exp, 0);
986 /* Preserve the original expression code. */
987 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
988 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
990 /* Normally convert enums to int,
991 but convert wide enums to something wider. */
992 if (code == ENUMERAL_TYPE)
994 type = type_for_size (MAX (TYPE_PRECISION (type),
995 TYPE_PRECISION (integer_type_node)),
996 ((flag_traditional
997 || (TYPE_PRECISION (type)
998 >= TYPE_PRECISION (integer_type_node)))
999 && TREE_UNSIGNED (type)));
1001 return convert (type, exp);
1004 if (TREE_CODE (exp) == COMPONENT_REF
1005 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1006 /* If it's thinner than an int, promote it like a
1007 c_promoting_integer_type_p, otherwise leave it alone. */
1008 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1009 TYPE_PRECISION (integer_type_node)))
1010 return convert (flag_traditional && TREE_UNSIGNED (type)
1011 ? unsigned_type_node : integer_type_node,
1012 exp);
1014 if (c_promoting_integer_type_p (type))
1016 /* Traditionally, unsignedness is preserved in default promotions.
1017 Also preserve unsignedness if not really getting any wider. */
1018 if (TREE_UNSIGNED (type)
1019 && (flag_traditional
1020 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1021 return convert (unsigned_type_node, exp);
1023 return convert (integer_type_node, exp);
1026 if (flag_traditional && !flag_allow_single_precision
1027 && TYPE_MAIN_VARIANT (type) == float_type_node)
1028 return convert (double_type_node, exp);
1030 if (code == VOID_TYPE)
1032 error ("void value not ignored as it ought to be");
1033 return error_mark_node;
1035 return exp;
1038 /* Look up COMPONENT in a structure or union DECL.
1040 If the component name is not found, returns NULL_TREE. Otherwise,
1041 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1042 stepping down the chain to the component, which is in the last
1043 TREE_VALUE of the list. Normally the list is of length one, but if
1044 the component is embedded within (nested) anonymous structures or
1045 unions, the list steps down the chain to the component. */
1047 static tree
1048 lookup_field (decl, component)
1049 tree decl, component;
1051 tree type = TREE_TYPE (decl);
1052 tree field;
1054 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1055 to the field elements. Use a binary search on this array to quickly
1056 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1057 will always be set for structures which have many elements. */
1059 if (TYPE_LANG_SPECIFIC (type))
1061 int bot, top, half;
1062 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1064 field = TYPE_FIELDS (type);
1065 bot = 0;
1066 top = TYPE_LANG_SPECIFIC (type)->len;
1067 while (top - bot > 1)
1069 half = (top - bot + 1) >> 1;
1070 field = field_array[bot+half];
1072 if (DECL_NAME (field) == NULL_TREE)
1074 /* Step through all anon unions in linear fashion. */
1075 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1077 field = field_array[bot++];
1078 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1079 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1081 tree anon = lookup_field (field, component);
1083 if (anon)
1084 return tree_cons (NULL_TREE, field, anon);
1088 /* Entire record is only anon unions. */
1089 if (bot > top)
1090 return NULL_TREE;
1092 /* Restart the binary search, with new lower bound. */
1093 continue;
1096 if (DECL_NAME (field) == component)
1097 break;
1098 if (DECL_NAME (field) < component)
1099 bot += half;
1100 else
1101 top = bot + half;
1104 if (DECL_NAME (field_array[bot]) == component)
1105 field = field_array[bot];
1106 else if (DECL_NAME (field) != component)
1107 return NULL_TREE;
1109 else
1111 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1113 if (DECL_NAME (field) == NULL_TREE
1114 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1115 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1117 tree anon = lookup_field (field, component);
1119 if (anon)
1120 return tree_cons (NULL_TREE, field, anon);
1123 if (DECL_NAME (field) == component)
1124 break;
1127 if (field == NULL_TREE)
1128 return NULL_TREE;
1131 return tree_cons (NULL_TREE, field, NULL_TREE);
1134 /* Make an expression to refer to the COMPONENT field of
1135 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1137 tree
1138 build_component_ref (datum, component)
1139 tree datum, component;
1141 tree type = TREE_TYPE (datum);
1142 enum tree_code code = TREE_CODE (type);
1143 tree field = NULL;
1144 tree ref;
1146 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1147 If pedantic ensure that the arguments are not lvalues; otherwise,
1148 if the component is an array, it would wrongly decay to a pointer in
1149 C89 mode.
1150 We cannot do this with a COND_EXPR, because in a conditional expression
1151 the default promotions are applied to both sides, and this would yield
1152 the wrong type of the result; for example, if the components have
1153 type "char". */
1154 switch (TREE_CODE (datum))
1156 case COMPOUND_EXPR:
1158 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1159 return build (COMPOUND_EXPR, TREE_TYPE (value),
1160 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
1162 default:
1163 break;
1166 /* See if there is a field or component with name COMPONENT. */
1168 if (code == RECORD_TYPE || code == UNION_TYPE)
1170 if (!COMPLETE_TYPE_P (type))
1172 incomplete_type_error (NULL_TREE, type);
1173 return error_mark_node;
1176 field = lookup_field (datum, component);
1178 if (!field)
1180 error ("%s has no member named `%s'",
1181 code == RECORD_TYPE ? "structure" : "union",
1182 IDENTIFIER_POINTER (component));
1183 return error_mark_node;
1186 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1187 This might be better solved in future the way the C++ front
1188 end does it - by giving the anonymous entities each a
1189 separate name and type, and then have build_component_ref
1190 recursively call itself. We can't do that here. */
1191 for (; field; field = TREE_CHAIN (field))
1193 tree subdatum = TREE_VALUE (field);
1195 if (TREE_TYPE (subdatum) == error_mark_node)
1196 return error_mark_node;
1198 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1199 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1200 TREE_READONLY (ref) = 1;
1201 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1202 TREE_THIS_VOLATILE (ref) = 1;
1203 datum = ref;
1206 return ref;
1208 else if (code != ERROR_MARK)
1209 error ("request for member `%s' in something not a structure or union",
1210 IDENTIFIER_POINTER (component));
1212 return error_mark_node;
1215 /* Given an expression PTR for a pointer, return an expression
1216 for the value pointed to.
1217 ERRORSTRING is the name of the operator to appear in error messages. */
1219 tree
1220 build_indirect_ref (ptr, errorstring)
1221 tree ptr;
1222 const char *errorstring;
1224 tree pointer = default_conversion (ptr);
1225 tree type = TREE_TYPE (pointer);
1227 if (TREE_CODE (type) == POINTER_TYPE)
1229 if (TREE_CODE (pointer) == ADDR_EXPR
1230 && !flag_volatile
1231 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1232 == TREE_TYPE (type)))
1233 return TREE_OPERAND (pointer, 0);
1234 else
1236 tree t = TREE_TYPE (type);
1237 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1239 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1241 error ("dereferencing pointer to incomplete type");
1242 return error_mark_node;
1244 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1245 warning ("dereferencing `void *' pointer");
1247 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1248 so that we get the proper error message if the result is used
1249 to assign to. Also, &* is supposed to be a no-op.
1250 And ANSI C seems to specify that the type of the result
1251 should be the const type. */
1252 /* A de-reference of a pointer to const is not a const. It is valid
1253 to change it via some other pointer. */
1254 TREE_READONLY (ref) = TYPE_READONLY (t);
1255 TREE_SIDE_EFFECTS (ref)
1256 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1257 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1258 return ref;
1261 else if (TREE_CODE (pointer) != ERROR_MARK)
1262 error ("invalid type argument of `%s'", errorstring);
1263 return error_mark_node;
1266 /* This handles expressions of the form "a[i]", which denotes
1267 an array reference.
1269 This is logically equivalent in C to *(a+i), but we may do it differently.
1270 If A is a variable or a member, we generate a primitive ARRAY_REF.
1271 This avoids forcing the array out of registers, and can work on
1272 arrays that are not lvalues (for example, members of structures returned
1273 by functions). */
1275 tree
1276 build_array_ref (array, index)
1277 tree array, index;
1279 if (index == 0)
1281 error ("subscript missing in array reference");
1282 return error_mark_node;
1285 if (TREE_TYPE (array) == error_mark_node
1286 || TREE_TYPE (index) == error_mark_node)
1287 return error_mark_node;
1289 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1290 && TREE_CODE (array) != INDIRECT_REF)
1292 tree rval, type;
1294 /* Subscripting with type char is likely to lose
1295 on a machine where chars are signed.
1296 So warn on any machine, but optionally.
1297 Don't warn for unsigned char since that type is safe.
1298 Don't warn for signed char because anyone who uses that
1299 must have done so deliberately. */
1300 if (warn_char_subscripts
1301 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1302 warning ("array subscript has type `char'");
1304 /* Apply default promotions *after* noticing character types. */
1305 index = default_conversion (index);
1307 /* Require integer *after* promotion, for sake of enums. */
1308 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1310 error ("array subscript is not an integer");
1311 return error_mark_node;
1314 /* An array that is indexed by a non-constant
1315 cannot be stored in a register; we must be able to do
1316 address arithmetic on its address.
1317 Likewise an array of elements of variable size. */
1318 if (TREE_CODE (index) != INTEGER_CST
1319 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1320 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1322 if (mark_addressable (array) == 0)
1323 return error_mark_node;
1325 /* An array that is indexed by a constant value which is not within
1326 the array bounds cannot be stored in a register either; because we
1327 would get a crash in store_bit_field/extract_bit_field when trying
1328 to access a non-existent part of the register. */
1329 if (TREE_CODE (index) == INTEGER_CST
1330 && TYPE_VALUES (TREE_TYPE (array))
1331 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1333 if (mark_addressable (array) == 0)
1334 return error_mark_node;
1337 if (pedantic)
1339 tree foo = array;
1340 while (TREE_CODE (foo) == COMPONENT_REF)
1341 foo = TREE_OPERAND (foo, 0);
1342 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1343 pedwarn ("ISO C forbids subscripting `register' array");
1344 else if (! flag_isoc99 && ! lvalue_p (foo))
1345 pedwarn ("ISO C89 forbids subscripting non-lvalue array");
1348 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1349 rval = build (ARRAY_REF, type, array, index);
1350 /* Array ref is const/volatile if the array elements are
1351 or if the array is. */
1352 TREE_READONLY (rval)
1353 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1354 | TREE_READONLY (array));
1355 TREE_SIDE_EFFECTS (rval)
1356 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1357 | TREE_SIDE_EFFECTS (array));
1358 TREE_THIS_VOLATILE (rval)
1359 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1360 /* This was added by rms on 16 Nov 91.
1361 It fixes vol struct foo *a; a->elts[1]
1362 in an inline function.
1363 Hope it doesn't break something else. */
1364 | TREE_THIS_VOLATILE (array));
1365 return require_complete_type (fold (rval));
1369 tree ar = default_conversion (array);
1370 tree ind = default_conversion (index);
1372 /* Do the same warning check as above, but only on the part that's
1373 syntactically the index and only if it is also semantically
1374 the index. */
1375 if (warn_char_subscripts
1376 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1377 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1378 warning ("subscript has type `char'");
1380 /* Put the integer in IND to simplify error checking. */
1381 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1383 tree temp = ar;
1384 ar = ind;
1385 ind = temp;
1388 if (ar == error_mark_node)
1389 return ar;
1391 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1392 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1394 error ("subscripted value is neither array nor pointer");
1395 return error_mark_node;
1397 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1399 error ("array subscript is not an integer");
1400 return error_mark_node;
1403 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1404 "array indexing");
1408 /* Build an external reference to identifier ID. FUN indicates
1409 whether this will be used for a function call. */
1410 tree
1411 build_external_ref (id, fun)
1412 tree id;
1413 int fun;
1415 tree ref;
1416 tree decl = lookup_name (id);
1417 tree objc_ivar = lookup_objc_ivar (id);
1419 if (!decl || decl == error_mark_node || C_DECL_ANTICIPATED (decl))
1421 if (objc_ivar)
1422 ref = objc_ivar;
1423 else if (fun)
1425 if (!decl || decl == error_mark_node)
1426 /* Ordinary implicit function declaration. */
1427 ref = implicitly_declare (id);
1428 else
1430 /* Implicit declaration of built-in function. Don't
1431 change the built-in declaration, but don't let this
1432 go by silently, either. */
1433 implicit_decl_warning (id);
1435 /* only issue this warning once */
1436 C_DECL_ANTICIPATED (decl) = 0;
1437 ref = decl;
1440 else
1442 /* Reference to undeclared variable, including reference to
1443 builtin outside of function-call context. */
1444 if (current_function_decl == 0)
1445 error ("`%s' undeclared here (not in a function)",
1446 IDENTIFIER_POINTER (id));
1447 else
1449 if (IDENTIFIER_GLOBAL_VALUE (id) != error_mark_node
1450 || IDENTIFIER_ERROR_LOCUS (id) != current_function_decl)
1452 error ("`%s' undeclared (first use in this function)",
1453 IDENTIFIER_POINTER (id));
1455 if (! undeclared_variable_notice)
1457 error ("(Each undeclared identifier is reported only once");
1458 error ("for each function it appears in.)");
1459 undeclared_variable_notice = 1;
1462 IDENTIFIER_GLOBAL_VALUE (id) = error_mark_node;
1463 IDENTIFIER_ERROR_LOCUS (id) = current_function_decl;
1465 return error_mark_node;
1468 else
1470 /* Properly declared variable or function reference. */
1471 if (!objc_ivar)
1472 ref = decl;
1473 else if (decl != objc_ivar && IDENTIFIER_LOCAL_VALUE (id))
1475 warning ("local declaration of `%s' hides instance variable",
1476 IDENTIFIER_POINTER (id));
1477 ref = decl;
1479 else
1480 ref = objc_ivar;
1483 if (TREE_TYPE (ref) == error_mark_node)
1484 return error_mark_node;
1486 assemble_external (ref);
1487 TREE_USED (ref) = 1;
1489 if (TREE_CODE (ref) == CONST_DECL)
1491 ref = DECL_INITIAL (ref);
1492 TREE_CONSTANT (ref) = 1;
1495 return ref;
1498 /* Build a function call to function FUNCTION with parameters PARAMS.
1499 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1500 TREE_VALUE of each node is a parameter-expression.
1501 FUNCTION's data type may be a function type or a pointer-to-function. */
1503 tree
1504 build_function_call (function, params)
1505 tree function, params;
1507 tree fntype, fundecl = 0;
1508 tree coerced_params;
1509 tree name = NULL_TREE, assembler_name = NULL_TREE, result;
1511 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1512 STRIP_TYPE_NOPS (function);
1514 /* Convert anything with function type to a pointer-to-function. */
1515 if (TREE_CODE (function) == FUNCTION_DECL)
1517 name = DECL_NAME (function);
1518 assembler_name = DECL_ASSEMBLER_NAME (function);
1520 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1521 (because calling an inline function does not mean the function
1522 needs to be separately compiled). */
1523 fntype = build_type_variant (TREE_TYPE (function),
1524 TREE_READONLY (function),
1525 TREE_THIS_VOLATILE (function));
1526 fundecl = function;
1527 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1529 else
1530 function = default_conversion (function);
1532 fntype = TREE_TYPE (function);
1534 if (TREE_CODE (fntype) == ERROR_MARK)
1535 return error_mark_node;
1537 if (!(TREE_CODE (fntype) == POINTER_TYPE
1538 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1540 error ("called object is not a function");
1541 return error_mark_node;
1544 /* fntype now gets the type of function pointed to. */
1545 fntype = TREE_TYPE (fntype);
1547 /* Convert the parameters to the types declared in the
1548 function prototype, or apply default promotions. */
1550 coerced_params
1551 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1553 /* Check for errors in format strings. */
1555 if (warn_format)
1556 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
1558 /* Recognize certain built-in functions so we can make tree-codes
1559 other than CALL_EXPR. We do this when it enables fold-const.c
1560 to do something useful. */
1562 if (TREE_CODE (function) == ADDR_EXPR
1563 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1564 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1566 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1567 params, coerced_params);
1568 if (result)
1569 return result;
1572 result = build (CALL_EXPR, TREE_TYPE (fntype),
1573 function, coerced_params, NULL_TREE);
1574 TREE_SIDE_EFFECTS (result) = 1;
1575 result = fold (result);
1577 if (VOID_TYPE_P (TREE_TYPE (result)))
1578 return result;
1579 return require_complete_type (result);
1582 /* Convert the argument expressions in the list VALUES
1583 to the types in the list TYPELIST. The result is a list of converted
1584 argument expressions.
1586 If TYPELIST is exhausted, or when an element has NULL as its type,
1587 perform the default conversions.
1589 PARMLIST is the chain of parm decls for the function being called.
1590 It may be 0, if that info is not available.
1591 It is used only for generating error messages.
1593 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1595 This is also where warnings about wrong number of args are generated.
1597 Both VALUES and the returned value are chains of TREE_LIST nodes
1598 with the elements of the list in the TREE_VALUE slots of those nodes. */
1600 static tree
1601 convert_arguments (typelist, values, name, fundecl)
1602 tree typelist, values, name, fundecl;
1604 tree typetail, valtail;
1605 tree result = NULL;
1606 int parmnum;
1608 /* Scan the given expressions and types, producing individual
1609 converted arguments and pushing them on RESULT in reverse order. */
1611 for (valtail = values, typetail = typelist, parmnum = 0;
1612 valtail;
1613 valtail = TREE_CHAIN (valtail), parmnum++)
1615 tree type = typetail ? TREE_VALUE (typetail) : 0;
1616 tree val = TREE_VALUE (valtail);
1618 if (type == void_type_node)
1620 if (name)
1621 error ("too many arguments to function `%s'",
1622 IDENTIFIER_POINTER (name));
1623 else
1624 error ("too many arguments to function");
1625 break;
1628 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1629 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1630 to convert automatically to a pointer. */
1631 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1632 val = TREE_OPERAND (val, 0);
1634 val = default_function_array_conversion (val);
1636 val = require_complete_type (val);
1638 if (type != 0)
1640 /* Formal parm type is specified by a function prototype. */
1641 tree parmval;
1643 if (!COMPLETE_TYPE_P (type))
1645 error ("type of formal parameter %d is incomplete", parmnum + 1);
1646 parmval = val;
1648 else
1650 /* Optionally warn about conversions that
1651 differ from the default conversions. */
1652 if (warn_conversion || warn_traditional)
1654 int formal_prec = TYPE_PRECISION (type);
1656 if (INTEGRAL_TYPE_P (type)
1657 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1658 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1659 if (INTEGRAL_TYPE_P (type)
1660 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1661 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1662 else if (TREE_CODE (type) == COMPLEX_TYPE
1663 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1664 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1665 else if (TREE_CODE (type) == REAL_TYPE
1666 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1667 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1668 else if (TREE_CODE (type) == COMPLEX_TYPE
1669 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1670 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1671 else if (TREE_CODE (type) == REAL_TYPE
1672 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1673 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1674 /* ??? At some point, messages should be written about
1675 conversions between complex types, but that's too messy
1676 to do now. */
1677 else if (TREE_CODE (type) == REAL_TYPE
1678 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1680 /* Warn if any argument is passed as `float',
1681 since without a prototype it would be `double'. */
1682 if (formal_prec == TYPE_PRECISION (float_type_node))
1683 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1685 /* Detect integer changing in width or signedness.
1686 These warnings are only activated with
1687 -Wconversion, not with -Wtraditional. */
1688 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1689 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1691 tree would_have_been = default_conversion (val);
1692 tree type1 = TREE_TYPE (would_have_been);
1694 if (TREE_CODE (type) == ENUMERAL_TYPE
1695 && (TYPE_MAIN_VARIANT (type)
1696 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1697 /* No warning if function asks for enum
1698 and the actual arg is that enum type. */
1700 else if (formal_prec != TYPE_PRECISION (type1))
1701 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1702 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1704 /* Don't complain if the formal parameter type
1705 is an enum, because we can't tell now whether
1706 the value was an enum--even the same enum. */
1707 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1709 else if (TREE_CODE (val) == INTEGER_CST
1710 && int_fits_type_p (val, type))
1711 /* Change in signedness doesn't matter
1712 if a constant value is unaffected. */
1714 /* Likewise for a constant in a NOP_EXPR. */
1715 else if (TREE_CODE (val) == NOP_EXPR
1716 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1717 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1719 #if 0 /* We never get such tree structure here. */
1720 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1721 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1722 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1723 /* Change in signedness doesn't matter
1724 if an enum value is unaffected. */
1726 #endif
1727 /* If the value is extended from a narrower
1728 unsigned type, it doesn't matter whether we
1729 pass it as signed or unsigned; the value
1730 certainly is the same either way. */
1731 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1732 && TREE_UNSIGNED (TREE_TYPE (val)))
1734 else if (TREE_UNSIGNED (type))
1735 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1736 else
1737 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1741 parmval = convert_for_assignment (type, val,
1742 (char *) 0, /* arg passing */
1743 fundecl, name, parmnum + 1);
1745 if (PROMOTE_PROTOTYPES
1746 && INTEGRAL_TYPE_P (type)
1747 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1748 parmval = default_conversion (parmval);
1750 result = tree_cons (NULL_TREE, parmval, result);
1752 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1753 && (TYPE_PRECISION (TREE_TYPE (val))
1754 < TYPE_PRECISION (double_type_node)))
1755 /* Convert `float' to `double'. */
1756 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1757 else
1758 /* Convert `short' and `char' to full-size `int'. */
1759 result = tree_cons (NULL_TREE, default_conversion (val), result);
1761 if (typetail)
1762 typetail = TREE_CHAIN (typetail);
1765 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1767 if (name)
1768 error ("too few arguments to function `%s'",
1769 IDENTIFIER_POINTER (name));
1770 else
1771 error ("too few arguments to function");
1774 return nreverse (result);
1777 /* This is the entry point used by the parser
1778 for binary operators in the input.
1779 In addition to constructing the expression,
1780 we check for operands that were written with other binary operators
1781 in a way that is likely to confuse the user. */
1783 tree
1784 parser_build_binary_op (code, arg1, arg2)
1785 enum tree_code code;
1786 tree arg1, arg2;
1788 tree result = build_binary_op (code, arg1, arg2, 1);
1790 char class;
1791 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1792 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1793 enum tree_code code1 = ERROR_MARK;
1794 enum tree_code code2 = ERROR_MARK;
1796 if (IS_EXPR_CODE_CLASS (class1))
1797 code1 = C_EXP_ORIGINAL_CODE (arg1);
1798 if (IS_EXPR_CODE_CLASS (class2))
1799 code2 = C_EXP_ORIGINAL_CODE (arg2);
1801 /* Check for cases such as x+y<<z which users are likely
1802 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1803 is cleared to prevent these warnings. */
1804 if (warn_parentheses)
1806 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1808 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1809 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1810 warning ("suggest parentheses around + or - inside shift");
1813 if (code == TRUTH_ORIF_EXPR)
1815 if (code1 == TRUTH_ANDIF_EXPR
1816 || code2 == TRUTH_ANDIF_EXPR)
1817 warning ("suggest parentheses around && within ||");
1820 if (code == BIT_IOR_EXPR)
1822 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1823 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1824 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1825 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1826 warning ("suggest parentheses around arithmetic in operand of |");
1827 /* Check cases like x|y==z */
1828 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1829 warning ("suggest parentheses around comparison in operand of |");
1832 if (code == BIT_XOR_EXPR)
1834 if (code1 == BIT_AND_EXPR
1835 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1836 || code2 == BIT_AND_EXPR
1837 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1838 warning ("suggest parentheses around arithmetic in operand of ^");
1839 /* Check cases like x^y==z */
1840 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1841 warning ("suggest parentheses around comparison in operand of ^");
1844 if (code == BIT_AND_EXPR)
1846 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1847 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1848 warning ("suggest parentheses around + or - in operand of &");
1849 /* Check cases like x&y==z */
1850 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1851 warning ("suggest parentheses around comparison in operand of &");
1855 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1856 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1857 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1858 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1860 unsigned_conversion_warning (result, arg1);
1861 unsigned_conversion_warning (result, arg2);
1862 overflow_warning (result);
1864 class = TREE_CODE_CLASS (TREE_CODE (result));
1866 /* Record the code that was specified in the source,
1867 for the sake of warnings about confusing nesting. */
1868 if (IS_EXPR_CODE_CLASS (class))
1869 C_SET_EXP_ORIGINAL_CODE (result, code);
1870 else
1872 int flag = TREE_CONSTANT (result);
1873 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1874 so that convert_for_assignment wouldn't strip it.
1875 That way, we got warnings for things like p = (1 - 1).
1876 But it turns out we should not get those warnings. */
1877 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1878 C_SET_EXP_ORIGINAL_CODE (result, code);
1879 TREE_CONSTANT (result) = flag;
1882 return result;
1885 /* Build a binary-operation expression without default conversions.
1886 CODE is the kind of expression to build.
1887 This function differs from `build' in several ways:
1888 the data type of the result is computed and recorded in it,
1889 warnings are generated if arg data types are invalid,
1890 special handling for addition and subtraction of pointers is known,
1891 and some optimization is done (operations on narrow ints
1892 are done in the narrower type when that gives the same result).
1893 Constant folding is also done before the result is returned.
1895 Note that the operands will never have enumeral types, or function
1896 or array types, because either they will have the default conversions
1897 performed or they have both just been converted to some other type in which
1898 the arithmetic is to be done. */
1900 tree
1901 build_binary_op (code, orig_op0, orig_op1, convert_p)
1902 enum tree_code code;
1903 tree orig_op0, orig_op1;
1904 int convert_p;
1906 tree type0, type1;
1907 enum tree_code code0, code1;
1908 tree op0, op1;
1910 /* Expression code to give to the expression when it is built.
1911 Normally this is CODE, which is what the caller asked for,
1912 but in some special cases we change it. */
1913 enum tree_code resultcode = code;
1915 /* Data type in which the computation is to be performed.
1916 In the simplest cases this is the common type of the arguments. */
1917 tree result_type = NULL;
1919 /* Nonzero means operands have already been type-converted
1920 in whatever way is necessary.
1921 Zero means they need to be converted to RESULT_TYPE. */
1922 int converted = 0;
1924 /* Nonzero means create the expression with this type, rather than
1925 RESULT_TYPE. */
1926 tree build_type = 0;
1928 /* Nonzero means after finally constructing the expression
1929 convert it to this type. */
1930 tree final_type = 0;
1932 /* Nonzero if this is an operation like MIN or MAX which can
1933 safely be computed in short if both args are promoted shorts.
1934 Also implies COMMON.
1935 -1 indicates a bitwise operation; this makes a difference
1936 in the exact conditions for when it is safe to do the operation
1937 in a narrower mode. */
1938 int shorten = 0;
1940 /* Nonzero if this is a comparison operation;
1941 if both args are promoted shorts, compare the original shorts.
1942 Also implies COMMON. */
1943 int short_compare = 0;
1945 /* Nonzero if this is a right-shift operation, which can be computed on the
1946 original short and then promoted if the operand is a promoted short. */
1947 int short_shift = 0;
1949 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1950 int common = 0;
1952 if (convert_p)
1954 op0 = default_conversion (orig_op0);
1955 op1 = default_conversion (orig_op1);
1957 else
1959 op0 = orig_op0;
1960 op1 = orig_op1;
1963 type0 = TREE_TYPE (op0);
1964 type1 = TREE_TYPE (op1);
1966 /* The expression codes of the data types of the arguments tell us
1967 whether the arguments are integers, floating, pointers, etc. */
1968 code0 = TREE_CODE (type0);
1969 code1 = TREE_CODE (type1);
1971 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1972 STRIP_TYPE_NOPS (op0);
1973 STRIP_TYPE_NOPS (op1);
1975 /* If an error was already reported for one of the arguments,
1976 avoid reporting another error. */
1978 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1979 return error_mark_node;
1981 switch (code)
1983 case PLUS_EXPR:
1984 /* Handle the pointer + int case. */
1985 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1986 return pointer_int_sum (PLUS_EXPR, op0, op1);
1987 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1988 return pointer_int_sum (PLUS_EXPR, op1, op0);
1989 else
1990 common = 1;
1991 break;
1993 case MINUS_EXPR:
1994 /* Subtraction of two similar pointers.
1995 We must subtract them as integers, then divide by object size. */
1996 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1997 && comp_target_types (type0, type1))
1998 return pointer_diff (op0, op1);
1999 /* Handle pointer minus int. Just like pointer plus int. */
2000 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2001 return pointer_int_sum (MINUS_EXPR, op0, op1);
2002 else
2003 common = 1;
2004 break;
2006 case MULT_EXPR:
2007 common = 1;
2008 break;
2010 case TRUNC_DIV_EXPR:
2011 case CEIL_DIV_EXPR:
2012 case FLOOR_DIV_EXPR:
2013 case ROUND_DIV_EXPR:
2014 case EXACT_DIV_EXPR:
2015 /* Floating point division by zero is a legitimate way to obtain
2016 infinities and NaNs. */
2017 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2018 warning ("division by zero");
2020 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2021 || code0 == COMPLEX_TYPE)
2022 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2023 || code1 == COMPLEX_TYPE))
2025 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2026 resultcode = RDIV_EXPR;
2027 else
2028 /* Although it would be tempting to shorten always here, that
2029 loses on some targets, since the modulo instruction is
2030 undefined if the quotient can't be represented in the
2031 computation mode. We shorten only if unsigned or if
2032 dividing by something we know != -1. */
2033 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2034 || (TREE_CODE (op1) == INTEGER_CST
2035 && ! integer_all_onesp (op1)));
2036 common = 1;
2038 break;
2040 case BIT_AND_EXPR:
2041 case BIT_ANDTC_EXPR:
2042 case BIT_IOR_EXPR:
2043 case BIT_XOR_EXPR:
2044 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2045 shorten = -1;
2046 /* If one operand is a constant, and the other is a short type
2047 that has been converted to an int,
2048 really do the work in the short type and then convert the
2049 result to int. If we are lucky, the constant will be 0 or 1
2050 in the short type, making the entire operation go away. */
2051 if (TREE_CODE (op0) == INTEGER_CST
2052 && TREE_CODE (op1) == NOP_EXPR
2053 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2054 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2056 final_type = result_type;
2057 op1 = TREE_OPERAND (op1, 0);
2058 result_type = TREE_TYPE (op1);
2060 if (TREE_CODE (op1) == INTEGER_CST
2061 && TREE_CODE (op0) == NOP_EXPR
2062 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2063 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2065 final_type = result_type;
2066 op0 = TREE_OPERAND (op0, 0);
2067 result_type = TREE_TYPE (op0);
2069 break;
2071 case TRUNC_MOD_EXPR:
2072 case FLOOR_MOD_EXPR:
2073 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2074 warning ("division by zero");
2076 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2078 /* Although it would be tempting to shorten always here, that loses
2079 on some targets, since the modulo instruction is undefined if the
2080 quotient can't be represented in the computation mode. We shorten
2081 only if unsigned or if dividing by something we know != -1. */
2082 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2083 || (TREE_CODE (op1) == INTEGER_CST
2084 && ! integer_all_onesp (op1)));
2085 common = 1;
2087 break;
2089 case TRUTH_ANDIF_EXPR:
2090 case TRUTH_ORIF_EXPR:
2091 case TRUTH_AND_EXPR:
2092 case TRUTH_OR_EXPR:
2093 case TRUTH_XOR_EXPR:
2094 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2095 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2096 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2097 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2099 /* Result of these operations is always an int,
2100 but that does not mean the operands should be
2101 converted to ints! */
2102 result_type = integer_type_node;
2103 op0 = truthvalue_conversion (op0);
2104 op1 = truthvalue_conversion (op1);
2105 converted = 1;
2107 break;
2109 /* Shift operations: result has same type as first operand;
2110 always convert second operand to int.
2111 Also set SHORT_SHIFT if shifting rightward. */
2113 case RSHIFT_EXPR:
2114 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2116 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2118 if (tree_int_cst_sgn (op1) < 0)
2119 warning ("right shift count is negative");
2120 else
2122 if (! integer_zerop (op1))
2123 short_shift = 1;
2125 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2126 warning ("right shift count >= width of type");
2130 /* Use the type of the value to be shifted.
2131 This is what most traditional C compilers do. */
2132 result_type = type0;
2133 /* Unless traditional, convert the shift-count to an integer,
2134 regardless of size of value being shifted. */
2135 if (! flag_traditional)
2137 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2138 op1 = convert (integer_type_node, op1);
2139 /* Avoid converting op1 to result_type later. */
2140 converted = 1;
2143 break;
2145 case LSHIFT_EXPR:
2146 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2148 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2150 if (tree_int_cst_sgn (op1) < 0)
2151 warning ("left shift count is negative");
2153 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2154 warning ("left shift count >= width of type");
2157 /* Use the type of the value to be shifted.
2158 This is what most traditional C compilers do. */
2159 result_type = type0;
2160 /* Unless traditional, convert the shift-count to an integer,
2161 regardless of size of value being shifted. */
2162 if (! flag_traditional)
2164 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2165 op1 = convert (integer_type_node, op1);
2166 /* Avoid converting op1 to result_type later. */
2167 converted = 1;
2170 break;
2172 case RROTATE_EXPR:
2173 case LROTATE_EXPR:
2174 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2176 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2178 if (tree_int_cst_sgn (op1) < 0)
2179 warning ("shift count is negative");
2180 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2181 warning ("shift count >= width of type");
2184 /* Use the type of the value to be shifted.
2185 This is what most traditional C compilers do. */
2186 result_type = type0;
2187 /* Unless traditional, convert the shift-count to an integer,
2188 regardless of size of value being shifted. */
2189 if (! flag_traditional)
2191 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2192 op1 = convert (integer_type_node, op1);
2193 /* Avoid converting op1 to result_type later. */
2194 converted = 1;
2197 break;
2199 case EQ_EXPR:
2200 case NE_EXPR:
2201 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2202 warning ("comparing floating point with == or != is unsafe");
2203 /* Result of comparison is always int,
2204 but don't convert the args to int! */
2205 build_type = integer_type_node;
2206 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2207 || code0 == COMPLEX_TYPE)
2208 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2209 || code1 == COMPLEX_TYPE))
2210 short_compare = 1;
2211 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2213 tree tt0 = TREE_TYPE (type0);
2214 tree tt1 = TREE_TYPE (type1);
2215 /* Anything compares with void *. void * compares with anything.
2216 Otherwise, the targets must be compatible
2217 and both must be object or both incomplete. */
2218 if (comp_target_types (type0, type1))
2219 result_type = common_type (type0, type1);
2220 else if (VOID_TYPE_P (tt0))
2222 /* op0 != orig_op0 detects the case of something
2223 whose value is 0 but which isn't a valid null ptr const. */
2224 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2225 && TREE_CODE (tt1) == FUNCTION_TYPE)
2226 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2228 else if (VOID_TYPE_P (tt1))
2230 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2231 && TREE_CODE (tt0) == FUNCTION_TYPE)
2232 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2234 else
2235 pedwarn ("comparison of distinct pointer types lacks a cast");
2237 if (result_type == NULL_TREE)
2238 result_type = ptr_type_node;
2240 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2241 && integer_zerop (op1))
2242 result_type = type0;
2243 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2244 && integer_zerop (op0))
2245 result_type = type1;
2246 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2248 result_type = type0;
2249 if (! flag_traditional)
2250 pedwarn ("comparison between pointer and integer");
2252 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2254 result_type = type1;
2255 if (! flag_traditional)
2256 pedwarn ("comparison between pointer and integer");
2258 break;
2260 case MAX_EXPR:
2261 case MIN_EXPR:
2262 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2263 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2264 shorten = 1;
2265 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2267 if (comp_target_types (type0, type1))
2269 result_type = common_type (type0, type1);
2270 if (pedantic
2271 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2272 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2274 else
2276 result_type = ptr_type_node;
2277 pedwarn ("comparison of distinct pointer types lacks a cast");
2280 break;
2282 case LE_EXPR:
2283 case GE_EXPR:
2284 case LT_EXPR:
2285 case GT_EXPR:
2286 build_type = integer_type_node;
2287 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2288 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2289 short_compare = 1;
2290 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2292 if (comp_target_types (type0, type1))
2294 result_type = common_type (type0, type1);
2295 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
2296 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
2297 pedwarn ("comparison of complete and incomplete pointers");
2298 else if (pedantic
2299 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2300 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2302 else
2304 result_type = ptr_type_node;
2305 pedwarn ("comparison of distinct pointer types lacks a cast");
2308 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2309 && integer_zerop (op1))
2311 result_type = type0;
2312 if (pedantic || extra_warnings)
2313 pedwarn ("ordered comparison of pointer with integer zero");
2315 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2316 && integer_zerop (op0))
2318 result_type = type1;
2319 if (pedantic)
2320 pedwarn ("ordered comparison of pointer with integer zero");
2322 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2324 result_type = type0;
2325 if (! flag_traditional)
2326 pedwarn ("comparison between pointer and integer");
2328 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2330 result_type = type1;
2331 if (! flag_traditional)
2332 pedwarn ("comparison between pointer and integer");
2334 break;
2336 case UNORDERED_EXPR:
2337 case ORDERED_EXPR:
2338 case UNLT_EXPR:
2339 case UNLE_EXPR:
2340 case UNGT_EXPR:
2341 case UNGE_EXPR:
2342 case UNEQ_EXPR:
2343 build_type = integer_type_node;
2344 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2346 error ("unordered comparison on non-floating point argument");
2347 return error_mark_node;
2349 common = 1;
2350 break;
2352 default:
2353 break;
2356 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2358 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2360 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2362 if (shorten || common || short_compare)
2363 result_type = common_type (type0, type1);
2365 /* For certain operations (which identify themselves by shorten != 0)
2366 if both args were extended from the same smaller type,
2367 do the arithmetic in that type and then extend.
2369 shorten !=0 and !=1 indicates a bitwise operation.
2370 For them, this optimization is safe only if
2371 both args are zero-extended or both are sign-extended.
2372 Otherwise, we might change the result.
2373 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2374 but calculated in (unsigned short) it would be (unsigned short)-1. */
2376 if (shorten && none_complex)
2378 int unsigned0, unsigned1;
2379 tree arg0 = get_narrower (op0, &unsigned0);
2380 tree arg1 = get_narrower (op1, &unsigned1);
2381 /* UNS is 1 if the operation to be done is an unsigned one. */
2382 int uns = TREE_UNSIGNED (result_type);
2383 tree type;
2385 final_type = result_type;
2387 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2388 but it *requires* conversion to FINAL_TYPE. */
2390 if ((TYPE_PRECISION (TREE_TYPE (op0))
2391 == TYPE_PRECISION (TREE_TYPE (arg0)))
2392 && TREE_TYPE (op0) != final_type)
2393 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2394 if ((TYPE_PRECISION (TREE_TYPE (op1))
2395 == TYPE_PRECISION (TREE_TYPE (arg1)))
2396 && TREE_TYPE (op1) != final_type)
2397 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2399 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2401 /* For bitwise operations, signedness of nominal type
2402 does not matter. Consider only how operands were extended. */
2403 if (shorten == -1)
2404 uns = unsigned0;
2406 /* Note that in all three cases below we refrain from optimizing
2407 an unsigned operation on sign-extended args.
2408 That would not be valid. */
2410 /* Both args variable: if both extended in same way
2411 from same width, do it in that width.
2412 Do it unsigned if args were zero-extended. */
2413 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2414 < TYPE_PRECISION (result_type))
2415 && (TYPE_PRECISION (TREE_TYPE (arg1))
2416 == TYPE_PRECISION (TREE_TYPE (arg0)))
2417 && unsigned0 == unsigned1
2418 && (unsigned0 || !uns))
2419 result_type
2420 = signed_or_unsigned_type (unsigned0,
2421 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2422 else if (TREE_CODE (arg0) == INTEGER_CST
2423 && (unsigned1 || !uns)
2424 && (TYPE_PRECISION (TREE_TYPE (arg1))
2425 < TYPE_PRECISION (result_type))
2426 && (type = signed_or_unsigned_type (unsigned1,
2427 TREE_TYPE (arg1)),
2428 int_fits_type_p (arg0, type)))
2429 result_type = type;
2430 else if (TREE_CODE (arg1) == INTEGER_CST
2431 && (unsigned0 || !uns)
2432 && (TYPE_PRECISION (TREE_TYPE (arg0))
2433 < TYPE_PRECISION (result_type))
2434 && (type = signed_or_unsigned_type (unsigned0,
2435 TREE_TYPE (arg0)),
2436 int_fits_type_p (arg1, type)))
2437 result_type = type;
2440 /* Shifts can be shortened if shifting right. */
2442 if (short_shift)
2444 int unsigned_arg;
2445 tree arg0 = get_narrower (op0, &unsigned_arg);
2447 final_type = result_type;
2449 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2450 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2452 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2453 /* We can shorten only if the shift count is less than the
2454 number of bits in the smaller type size. */
2455 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
2456 /* We cannot drop an unsigned shift after sign-extension. */
2457 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
2459 /* Do an unsigned shift if the operand was zero-extended. */
2460 result_type
2461 = signed_or_unsigned_type (unsigned_arg, TREE_TYPE (arg0));
2462 /* Convert value-to-be-shifted to that type. */
2463 if (TREE_TYPE (op0) != result_type)
2464 op0 = convert (result_type, op0);
2465 converted = 1;
2469 /* Comparison operations are shortened too but differently.
2470 They identify themselves by setting short_compare = 1. */
2472 if (short_compare)
2474 /* Don't write &op0, etc., because that would prevent op0
2475 from being kept in a register.
2476 Instead, make copies of the our local variables and
2477 pass the copies by reference, then copy them back afterward. */
2478 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2479 enum tree_code xresultcode = resultcode;
2480 tree val
2481 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2483 if (val != 0)
2484 return val;
2486 op0 = xop0, op1 = xop1;
2487 converted = 1;
2488 resultcode = xresultcode;
2490 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2491 && skip_evaluation == 0)
2493 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2494 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2495 int unsignedp0, unsignedp1;
2496 tree primop0 = get_narrower (op0, &unsignedp0);
2497 tree primop1 = get_narrower (op1, &unsignedp1);
2499 xop0 = orig_op0;
2500 xop1 = orig_op1;
2501 STRIP_TYPE_NOPS (xop0);
2502 STRIP_TYPE_NOPS (xop1);
2504 /* Give warnings for comparisons between signed and unsigned
2505 quantities that may fail.
2507 Do the checking based on the original operand trees, so that
2508 casts will be considered, but default promotions won't be.
2510 Do not warn if the comparison is being done in a signed type,
2511 since the signed type will only be chosen if it can represent
2512 all the values of the unsigned type. */
2513 if (! TREE_UNSIGNED (result_type))
2514 /* OK */;
2515 /* Do not warn if both operands are the same signedness. */
2516 else if (op0_signed == op1_signed)
2517 /* OK */;
2518 else
2520 tree sop, uop;
2522 if (op0_signed)
2523 sop = xop0, uop = xop1;
2524 else
2525 sop = xop1, uop = xop0;
2527 /* Do not warn if the signed quantity is an
2528 unsuffixed integer literal (or some static
2529 constant expression involving such literals or a
2530 conditional expression involving such literals)
2531 and it is non-negative. */
2532 if (tree_expr_nonnegative_p (sop))
2533 /* OK */;
2534 /* Do not warn if the comparison is an equality operation,
2535 the unsigned quantity is an integral constant, and it
2536 would fit in the result if the result were signed. */
2537 else if (TREE_CODE (uop) == INTEGER_CST
2538 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2539 && int_fits_type_p (uop, signed_type (result_type)))
2540 /* OK */;
2541 /* Do not warn if the unsigned quantity is an enumeration
2542 constant and its maximum value would fit in the result
2543 if the result were signed. */
2544 else if (TREE_CODE (uop) == INTEGER_CST
2545 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2546 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2547 signed_type (result_type)))
2548 /* OK */;
2549 else
2550 warning ("comparison between signed and unsigned");
2553 /* Warn if two unsigned values are being compared in a size
2554 larger than their original size, and one (and only one) is the
2555 result of a `~' operator. This comparison will always fail.
2557 Also warn if one operand is a constant, and the constant
2558 does not have all bits set that are set in the ~ operand
2559 when it is extended. */
2561 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2562 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2564 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2565 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2566 &unsignedp0);
2567 else
2568 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2569 &unsignedp1);
2571 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
2573 tree primop;
2574 HOST_WIDE_INT constant, mask;
2575 int unsignedp, bits;
2577 if (host_integerp (primop0, 0))
2579 primop = primop1;
2580 unsignedp = unsignedp1;
2581 constant = tree_low_cst (primop0, 0);
2583 else
2585 primop = primop0;
2586 unsignedp = unsignedp0;
2587 constant = tree_low_cst (primop1, 0);
2590 bits = TYPE_PRECISION (TREE_TYPE (primop));
2591 if (bits < TYPE_PRECISION (result_type)
2592 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
2594 mask = (~ (HOST_WIDE_INT) 0) << bits;
2595 if ((mask & constant) != mask)
2596 warning ("comparison of promoted ~unsigned with constant");
2599 else if (unsignedp0 && unsignedp1
2600 && (TYPE_PRECISION (TREE_TYPE (primop0))
2601 < TYPE_PRECISION (result_type))
2602 && (TYPE_PRECISION (TREE_TYPE (primop1))
2603 < TYPE_PRECISION (result_type)))
2604 warning ("comparison of promoted ~unsigned with unsigned");
2610 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2611 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2612 Then the expression will be built.
2613 It will be given type FINAL_TYPE if that is nonzero;
2614 otherwise, it will be given type RESULT_TYPE. */
2616 if (!result_type)
2618 binary_op_error (code);
2619 return error_mark_node;
2622 if (! converted)
2624 if (TREE_TYPE (op0) != result_type)
2625 op0 = convert (result_type, op0);
2626 if (TREE_TYPE (op1) != result_type)
2627 op1 = convert (result_type, op1);
2630 if (build_type == NULL_TREE)
2631 build_type = result_type;
2634 tree result = build (resultcode, build_type, op0, op1);
2635 tree folded;
2637 folded = fold (result);
2638 if (folded == result)
2639 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2640 if (final_type != 0)
2641 return convert (final_type, folded);
2642 return folded;
2646 /* Return a tree for the sum or difference (RESULTCODE says which)
2647 of pointer PTROP and integer INTOP. */
2649 static tree
2650 pointer_int_sum (resultcode, ptrop, intop)
2651 enum tree_code resultcode;
2652 tree ptrop, intop;
2654 tree size_exp;
2656 tree result;
2657 tree folded;
2659 /* The result is a pointer of the same type that is being added. */
2661 tree result_type = TREE_TYPE (ptrop);
2663 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2665 if (pedantic || warn_pointer_arith)
2666 pedwarn ("pointer of type `void *' used in arithmetic");
2667 size_exp = integer_one_node;
2669 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2671 if (pedantic || warn_pointer_arith)
2672 pedwarn ("pointer to a function used in arithmetic");
2673 size_exp = integer_one_node;
2675 else
2676 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2678 /* If what we are about to multiply by the size of the elements
2679 contains a constant term, apply distributive law
2680 and multiply that constant term separately.
2681 This helps produce common subexpressions. */
2683 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2684 && ! TREE_CONSTANT (intop)
2685 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2686 && TREE_CONSTANT (size_exp)
2687 /* If the constant comes from pointer subtraction,
2688 skip this optimization--it would cause an error. */
2689 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2690 /* If the constant is unsigned, and smaller than the pointer size,
2691 then we must skip this optimization. This is because it could cause
2692 an overflow error if the constant is negative but INTOP is not. */
2693 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2694 || (TYPE_PRECISION (TREE_TYPE (intop))
2695 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2697 enum tree_code subcode = resultcode;
2698 tree int_type = TREE_TYPE (intop);
2699 if (TREE_CODE (intop) == MINUS_EXPR)
2700 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2701 /* Convert both subexpression types to the type of intop,
2702 because weird cases involving pointer arithmetic
2703 can result in a sum or difference with different type args. */
2704 ptrop = build_binary_op (subcode, ptrop,
2705 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2706 intop = convert (int_type, TREE_OPERAND (intop, 0));
2709 /* Convert the integer argument to a type the same size as sizetype
2710 so the multiply won't overflow spuriously. */
2712 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2713 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2714 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2715 TREE_UNSIGNED (sizetype)), intop);
2717 /* Replace the integer argument with a suitable product by the object size.
2718 Do this multiplication as signed, then convert to the appropriate
2719 pointer type (actually unsigned integral). */
2721 intop = convert (result_type,
2722 build_binary_op (MULT_EXPR, intop,
2723 convert (TREE_TYPE (intop), size_exp), 1));
2725 /* Create the sum or difference. */
2727 result = build (resultcode, result_type, ptrop, intop);
2729 folded = fold (result);
2730 if (folded == result)
2731 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2732 return folded;
2735 /* Return a tree for the difference of pointers OP0 and OP1.
2736 The resulting tree has type int. */
2738 static tree
2739 pointer_diff (op0, op1)
2740 tree op0, op1;
2742 tree result, folded;
2743 tree restype = ptrdiff_type_node;
2745 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2746 tree con0, con1, lit0, lit1;
2747 tree orig_op1 = op1;
2749 if (pedantic || warn_pointer_arith)
2751 if (TREE_CODE (target_type) == VOID_TYPE)
2752 pedwarn ("pointer of type `void *' used in subtraction");
2753 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2754 pedwarn ("pointer to a function used in subtraction");
2757 /* If the conversion to ptrdiff_type does anything like widening or
2758 converting a partial to an integral mode, we get a convert_expression
2759 that is in the way to do any simplifications.
2760 (fold-const.c doesn't know that the extra bits won't be needed.
2761 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2762 different mode in place.)
2763 So first try to find a common term here 'by hand'; we want to cover
2764 at least the cases that occur in legal static initializers. */
2765 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2766 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2768 if (TREE_CODE (con0) == PLUS_EXPR)
2770 lit0 = TREE_OPERAND (con0, 1);
2771 con0 = TREE_OPERAND (con0, 0);
2773 else
2774 lit0 = integer_zero_node;
2776 if (TREE_CODE (con1) == PLUS_EXPR)
2778 lit1 = TREE_OPERAND (con1, 1);
2779 con1 = TREE_OPERAND (con1, 0);
2781 else
2782 lit1 = integer_zero_node;
2784 if (operand_equal_p (con0, con1, 0))
2786 op0 = lit0;
2787 op1 = lit1;
2791 /* First do the subtraction as integers;
2792 then drop through to build the divide operator.
2793 Do not do default conversions on the minus operator
2794 in case restype is a short type. */
2796 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2797 convert (restype, op1), 0);
2798 /* This generates an error if op1 is pointer to incomplete type. */
2799 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2800 error ("arithmetic on pointer to an incomplete type");
2802 /* This generates an error if op0 is pointer to incomplete type. */
2803 op1 = c_size_in_bytes (target_type);
2805 /* Divide by the size, in easiest possible way. */
2807 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2809 folded = fold (result);
2810 if (folded == result)
2811 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2812 return folded;
2815 /* Construct and perhaps optimize a tree representation
2816 for a unary operation. CODE, a tree_code, specifies the operation
2817 and XARG is the operand.
2818 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2819 the default promotions (such as from short to int).
2820 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2821 allows non-lvalues; this is only used to handle conversion of non-lvalue
2822 arrays to pointers in C99. */
2824 tree
2825 build_unary_op (code, xarg, flag)
2826 enum tree_code code;
2827 tree xarg;
2828 int flag;
2830 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2831 tree arg = xarg;
2832 tree argtype = 0;
2833 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2834 tree val;
2835 int noconvert = flag;
2837 if (typecode == ERROR_MARK)
2838 return error_mark_node;
2839 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2840 typecode = INTEGER_TYPE;
2842 switch (code)
2844 case CONVERT_EXPR:
2845 /* This is used for unary plus, because a CONVERT_EXPR
2846 is enough to prevent anybody from looking inside for
2847 associativity, but won't generate any code. */
2848 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2849 || typecode == COMPLEX_TYPE))
2851 error ("wrong type argument to unary plus");
2852 return error_mark_node;
2854 else if (!noconvert)
2855 arg = default_conversion (arg);
2856 break;
2858 case NEGATE_EXPR:
2859 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2860 || typecode == COMPLEX_TYPE))
2862 error ("wrong type argument to unary minus");
2863 return error_mark_node;
2865 else if (!noconvert)
2866 arg = default_conversion (arg);
2867 break;
2869 case BIT_NOT_EXPR:
2870 if (typecode == COMPLEX_TYPE)
2872 code = CONJ_EXPR;
2873 if (pedantic)
2874 pedwarn ("ISO C does not support `~' for complex conjugation");
2875 if (!noconvert)
2876 arg = default_conversion (arg);
2878 else if (typecode != INTEGER_TYPE)
2880 error ("wrong type argument to bit-complement");
2881 return error_mark_node;
2883 else if (!noconvert)
2884 arg = default_conversion (arg);
2885 break;
2887 case ABS_EXPR:
2888 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2889 || typecode == COMPLEX_TYPE))
2891 error ("wrong type argument to abs");
2892 return error_mark_node;
2894 else if (!noconvert)
2895 arg = default_conversion (arg);
2896 break;
2898 case CONJ_EXPR:
2899 /* Conjugating a real value is a no-op, but allow it anyway. */
2900 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2901 || typecode == COMPLEX_TYPE))
2903 error ("wrong type argument to conjugation");
2904 return error_mark_node;
2906 else if (!noconvert)
2907 arg = default_conversion (arg);
2908 break;
2910 case TRUTH_NOT_EXPR:
2911 if (typecode != INTEGER_TYPE
2912 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2913 && typecode != COMPLEX_TYPE
2914 /* These will convert to a pointer. */
2915 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2917 error ("wrong type argument to unary exclamation mark");
2918 return error_mark_node;
2920 arg = truthvalue_conversion (arg);
2921 return invert_truthvalue (arg);
2923 case NOP_EXPR:
2924 break;
2926 case REALPART_EXPR:
2927 if (TREE_CODE (arg) == COMPLEX_CST)
2928 return TREE_REALPART (arg);
2929 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2930 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2931 else
2932 return arg;
2934 case IMAGPART_EXPR:
2935 if (TREE_CODE (arg) == COMPLEX_CST)
2936 return TREE_IMAGPART (arg);
2937 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2938 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2939 else
2940 return convert (TREE_TYPE (arg), integer_zero_node);
2942 case PREINCREMENT_EXPR:
2943 case POSTINCREMENT_EXPR:
2944 case PREDECREMENT_EXPR:
2945 case POSTDECREMENT_EXPR:
2946 /* Handle complex lvalues (when permitted)
2947 by reduction to simpler cases. */
2949 val = unary_complex_lvalue (code, arg, 0);
2950 if (val != 0)
2951 return val;
2953 /* Increment or decrement the real part of the value,
2954 and don't change the imaginary part. */
2955 if (typecode == COMPLEX_TYPE)
2957 tree real, imag;
2959 if (pedantic)
2960 pedwarn ("ISO C does not support `++' and `--' on complex types");
2962 arg = stabilize_reference (arg);
2963 real = build_unary_op (REALPART_EXPR, arg, 1);
2964 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2965 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2966 build_unary_op (code, real, 1), imag);
2969 /* Report invalid types. */
2971 if (typecode != POINTER_TYPE
2972 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2974 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2975 error ("wrong type argument to increment");
2976 else
2977 error ("wrong type argument to decrement");
2979 return error_mark_node;
2983 tree inc;
2984 tree result_type = TREE_TYPE (arg);
2986 arg = get_unwidened (arg, 0);
2987 argtype = TREE_TYPE (arg);
2989 /* Compute the increment. */
2991 if (typecode == POINTER_TYPE)
2993 /* If pointer target is an undefined struct,
2994 we just cannot know how to do the arithmetic. */
2995 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2997 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2998 error ("increment of pointer to unknown structure");
2999 else
3000 error ("decrement of pointer to unknown structure");
3002 else if ((pedantic || warn_pointer_arith)
3003 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
3004 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
3006 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3007 pedwarn ("wrong type argument to increment");
3008 else
3009 pedwarn ("wrong type argument to decrement");
3012 inc = c_size_in_bytes (TREE_TYPE (result_type));
3014 else
3015 inc = integer_one_node;
3017 inc = convert (argtype, inc);
3019 /* Handle incrementing a cast-expression. */
3021 while (1)
3022 switch (TREE_CODE (arg))
3024 case NOP_EXPR:
3025 case CONVERT_EXPR:
3026 case FLOAT_EXPR:
3027 case FIX_TRUNC_EXPR:
3028 case FIX_FLOOR_EXPR:
3029 case FIX_ROUND_EXPR:
3030 case FIX_CEIL_EXPR:
3031 pedantic_lvalue_warning (CONVERT_EXPR);
3032 /* If the real type has the same machine representation
3033 as the type it is cast to, we can make better output
3034 by adding directly to the inside of the cast. */
3035 if ((TREE_CODE (TREE_TYPE (arg))
3036 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
3037 && (TYPE_MODE (TREE_TYPE (arg))
3038 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
3039 arg = TREE_OPERAND (arg, 0);
3040 else
3042 tree incremented, modify, value;
3043 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3044 value = boolean_increment (code, arg);
3045 else
3047 arg = stabilize_reference (arg);
3048 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3049 value = arg;
3050 else
3051 value = save_expr (arg);
3052 incremented = build (((code == PREINCREMENT_EXPR
3053 || code == POSTINCREMENT_EXPR)
3054 ? PLUS_EXPR : MINUS_EXPR),
3055 argtype, value, inc);
3056 TREE_SIDE_EFFECTS (incremented) = 1;
3057 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3058 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3060 TREE_USED (value) = 1;
3061 return value;
3063 break;
3065 default:
3066 goto give_up;
3068 give_up:
3070 /* Complain about anything else that is not a true lvalue. */
3071 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3072 || code == POSTINCREMENT_EXPR)
3073 ? "invalid lvalue in increment"
3074 : "invalid lvalue in decrement")))
3075 return error_mark_node;
3077 /* Report a read-only lvalue. */
3078 if (TREE_READONLY (arg))
3079 readonly_warning (arg,
3080 ((code == PREINCREMENT_EXPR
3081 || code == POSTINCREMENT_EXPR)
3082 ? _("increment") : _("decrement")));
3084 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3085 val = boolean_increment (code, arg);
3086 else
3087 val = build (code, TREE_TYPE (arg), arg, inc);
3088 TREE_SIDE_EFFECTS (val) = 1;
3089 val = convert (result_type, val);
3090 if (TREE_CODE (val) != code)
3091 TREE_NO_UNUSED_WARNING (val) = 1;
3092 return val;
3095 case ADDR_EXPR:
3096 /* Note that this operation never does default_conversion. */
3098 /* Let &* cancel out to simplify resulting code. */
3099 if (TREE_CODE (arg) == INDIRECT_REF)
3101 /* Don't let this be an lvalue. */
3102 if (lvalue_p (TREE_OPERAND (arg, 0)))
3103 return non_lvalue (TREE_OPERAND (arg, 0));
3104 return TREE_OPERAND (arg, 0);
3107 /* For &x[y], return x+y */
3108 if (TREE_CODE (arg) == ARRAY_REF)
3110 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
3111 return error_mark_node;
3112 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3113 TREE_OPERAND (arg, 1), 1);
3116 /* Handle complex lvalues (when permitted)
3117 by reduction to simpler cases. */
3118 val = unary_complex_lvalue (code, arg, flag);
3119 if (val != 0)
3120 return val;
3122 #if 0 /* Turned off because inconsistent;
3123 float f; *&(int)f = 3.4 stores in int format
3124 whereas (int)f = 3.4 stores in float format. */
3125 /* Address of a cast is just a cast of the address
3126 of the operand of the cast. */
3127 switch (TREE_CODE (arg))
3129 case NOP_EXPR:
3130 case CONVERT_EXPR:
3131 case FLOAT_EXPR:
3132 case FIX_TRUNC_EXPR:
3133 case FIX_FLOOR_EXPR:
3134 case FIX_ROUND_EXPR:
3135 case FIX_CEIL_EXPR:
3136 if (pedantic)
3137 pedwarn ("ISO C forbids the address of a cast expression");
3138 return convert (build_pointer_type (TREE_TYPE (arg)),
3139 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3140 0));
3142 #endif
3144 /* Allow the address of a constructor if all the elements
3145 are constant. */
3146 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3148 /* Anything not already handled and not a true memory reference
3149 or a non-lvalue array is an error. */
3150 else if (typecode != FUNCTION_TYPE && !flag
3151 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
3152 return error_mark_node;
3154 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3155 argtype = TREE_TYPE (arg);
3157 /* If the lvalue is const or volatile, merge that into the type
3158 to which the address will point. Note that you can't get a
3159 restricted pointer by taking the address of something, so we
3160 only have to deal with `const' and `volatile' here. */
3161 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3162 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3163 argtype = c_build_type_variant (argtype,
3164 TREE_READONLY (arg),
3165 TREE_THIS_VOLATILE (arg));
3167 argtype = build_pointer_type (argtype);
3169 if (mark_addressable (arg) == 0)
3170 return error_mark_node;
3173 tree addr;
3175 if (TREE_CODE (arg) == COMPONENT_REF)
3177 tree field = TREE_OPERAND (arg, 1);
3179 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
3181 if (DECL_C_BIT_FIELD (field))
3183 error ("attempt to take address of bit-field structure member `%s'",
3184 IDENTIFIER_POINTER (DECL_NAME (field)));
3185 return error_mark_node;
3188 addr = fold (build (PLUS_EXPR, argtype,
3189 convert (argtype, addr),
3190 convert (argtype, byte_position (field))));
3192 else
3193 addr = build1 (code, argtype, arg);
3195 /* Address of a static or external variable or
3196 file-scope function counts as a constant. */
3197 if (staticp (arg)
3198 && ! (TREE_CODE (arg) == FUNCTION_DECL
3199 && DECL_CONTEXT (arg) != 0))
3200 TREE_CONSTANT (addr) = 1;
3201 return addr;
3204 default:
3205 break;
3208 if (argtype == 0)
3209 argtype = TREE_TYPE (arg);
3210 return fold (build1 (code, argtype, arg));
3213 #if 0
3214 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3215 convert ARG with the same conversions in the same order
3216 and return the result. */
3218 static tree
3219 convert_sequence (conversions, arg)
3220 tree conversions;
3221 tree arg;
3223 switch (TREE_CODE (conversions))
3225 case NOP_EXPR:
3226 case CONVERT_EXPR:
3227 case FLOAT_EXPR:
3228 case FIX_TRUNC_EXPR:
3229 case FIX_FLOOR_EXPR:
3230 case FIX_ROUND_EXPR:
3231 case FIX_CEIL_EXPR:
3232 return convert (TREE_TYPE (conversions),
3233 convert_sequence (TREE_OPERAND (conversions, 0),
3234 arg));
3236 default:
3237 return arg;
3240 #endif /* 0 */
3242 /* Return nonzero if REF is an lvalue valid for this language.
3243 Lvalues can be assigned, unless their type has TYPE_READONLY.
3244 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3247 lvalue_p (ref)
3248 tree ref;
3250 enum tree_code code = TREE_CODE (ref);
3252 switch (code)
3254 case REALPART_EXPR:
3255 case IMAGPART_EXPR:
3256 case COMPONENT_REF:
3257 return lvalue_p (TREE_OPERAND (ref, 0));
3259 case STRING_CST:
3260 return 1;
3262 case INDIRECT_REF:
3263 case ARRAY_REF:
3264 case VAR_DECL:
3265 case PARM_DECL:
3266 case RESULT_DECL:
3267 case ERROR_MARK:
3268 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3269 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3271 case BIND_EXPR:
3272 case RTL_EXPR:
3273 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3275 default:
3276 return 0;
3280 /* Return nonzero if REF is an lvalue valid for this language;
3281 otherwise, print an error message and return zero. */
3284 lvalue_or_else (ref, msgid)
3285 tree ref;
3286 const char *msgid;
3288 int win = lvalue_p (ref);
3290 if (! win)
3291 error ("%s", msgid);
3293 return win;
3296 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3297 for certain kinds of expressions which are not really lvalues
3298 but which we can accept as lvalues. If FLAG is nonzero, then
3299 non-lvalues are OK since we may be converting a non-lvalue array to
3300 a pointer in C99.
3302 If ARG is not a kind of expression we can handle, return zero. */
3304 static tree
3305 unary_complex_lvalue (code, arg, flag)
3306 enum tree_code code;
3307 tree arg;
3308 int flag;
3310 /* Handle (a, b) used as an "lvalue". */
3311 if (TREE_CODE (arg) == COMPOUND_EXPR)
3313 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3315 /* If this returns a function type, it isn't really being used as
3316 an lvalue, so don't issue a warning about it. */
3317 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3318 pedantic_lvalue_warning (COMPOUND_EXPR);
3320 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3321 TREE_OPERAND (arg, 0), real_result);
3324 /* Handle (a ? b : c) used as an "lvalue". */
3325 if (TREE_CODE (arg) == COND_EXPR)
3327 if (!flag)
3328 pedantic_lvalue_warning (COND_EXPR);
3329 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3330 pedantic_lvalue_warning (COMPOUND_EXPR);
3332 return (build_conditional_expr
3333 (TREE_OPERAND (arg, 0),
3334 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
3335 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
3338 return 0;
3341 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3342 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3344 static void
3345 pedantic_lvalue_warning (code)
3346 enum tree_code code;
3348 if (pedantic)
3349 switch (code)
3351 case COND_EXPR:
3352 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
3353 break;
3354 case COMPOUND_EXPR:
3355 pedwarn ("ISO C forbids use of compound expressions as lvalues");
3356 break;
3357 default:
3358 pedwarn ("ISO C forbids use of cast expressions as lvalues");
3359 break;
3363 /* Warn about storing in something that is `const'. */
3365 void
3366 readonly_warning (arg, msgid)
3367 tree arg;
3368 const char *msgid;
3370 if (TREE_CODE (arg) == COMPONENT_REF)
3372 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3373 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3374 else
3375 pedwarn ("%s of read-only member `%s'", _(msgid),
3376 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3378 else if (TREE_CODE (arg) == VAR_DECL)
3379 pedwarn ("%s of read-only variable `%s'", _(msgid),
3380 IDENTIFIER_POINTER (DECL_NAME (arg)));
3381 else
3382 pedwarn ("%s of read-only location", _(msgid));
3385 /* Mark EXP saying that we need to be able to take the
3386 address of it; it should not be allocated in a register.
3387 Value is 1 if successful. */
3390 mark_addressable (exp)
3391 tree exp;
3393 tree x = exp;
3394 while (1)
3395 switch (TREE_CODE (x))
3397 case COMPONENT_REF:
3398 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3400 error ("cannot take address of bitfield `%s'",
3401 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3402 return 0;
3405 /* ... fall through ... */
3407 case ADDR_EXPR:
3408 case ARRAY_REF:
3409 case REALPART_EXPR:
3410 case IMAGPART_EXPR:
3411 x = TREE_OPERAND (x, 0);
3412 break;
3414 case CONSTRUCTOR:
3415 TREE_ADDRESSABLE (x) = 1;
3416 return 1;
3418 case VAR_DECL:
3419 case CONST_DECL:
3420 case PARM_DECL:
3421 case RESULT_DECL:
3422 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3423 && DECL_NONLOCAL (x))
3425 if (TREE_PUBLIC (x))
3427 error ("global register variable `%s' used in nested function",
3428 IDENTIFIER_POINTER (DECL_NAME (x)));
3429 return 0;
3431 pedwarn ("register variable `%s' used in nested function",
3432 IDENTIFIER_POINTER (DECL_NAME (x)));
3434 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3436 if (TREE_PUBLIC (x))
3438 error ("address of global register variable `%s' requested",
3439 IDENTIFIER_POINTER (DECL_NAME (x)));
3440 return 0;
3443 /* If we are making this addressable due to its having
3444 volatile components, give a different error message. Also
3445 handle the case of an unnamed parameter by not trying
3446 to give the name. */
3448 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3450 error ("cannot put object with volatile field into register");
3451 return 0;
3454 pedwarn ("address of register variable `%s' requested",
3455 IDENTIFIER_POINTER (DECL_NAME (x)));
3457 put_var_into_stack (x);
3459 /* drops in */
3460 case FUNCTION_DECL:
3461 TREE_ADDRESSABLE (x) = 1;
3462 #if 0 /* poplevel deals with this now. */
3463 if (DECL_CONTEXT (x) == 0)
3464 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3465 #endif
3467 default:
3468 return 1;
3472 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3474 tree
3475 build_conditional_expr (ifexp, op1, op2)
3476 tree ifexp, op1, op2;
3478 tree type1;
3479 tree type2;
3480 enum tree_code code1;
3481 enum tree_code code2;
3482 tree result_type = NULL;
3483 tree orig_op1 = op1, orig_op2 = op2;
3485 ifexp = truthvalue_conversion (default_conversion (ifexp));
3487 #if 0 /* Produces wrong result if within sizeof. */
3488 /* Don't promote the operands separately if they promote
3489 the same way. Return the unpromoted type and let the combined
3490 value get promoted if necessary. */
3492 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3493 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3494 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3495 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3497 if (TREE_CODE (ifexp) == INTEGER_CST)
3498 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3500 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3502 #endif
3504 /* Promote both alternatives. */
3506 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3507 op1 = default_conversion (op1);
3508 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3509 op2 = default_conversion (op2);
3511 if (TREE_CODE (ifexp) == ERROR_MARK
3512 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3513 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3514 return error_mark_node;
3516 type1 = TREE_TYPE (op1);
3517 code1 = TREE_CODE (type1);
3518 type2 = TREE_TYPE (op2);
3519 code2 = TREE_CODE (type2);
3521 /* Quickly detect the usual case where op1 and op2 have the same type
3522 after promotion. */
3523 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3525 if (type1 == type2)
3526 result_type = type1;
3527 else
3528 result_type = TYPE_MAIN_VARIANT (type1);
3530 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3531 || code1 == COMPLEX_TYPE)
3532 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3533 || code2 == COMPLEX_TYPE))
3535 result_type = common_type (type1, type2);
3537 /* If -Wsign-compare, warn here if type1 and type2 have
3538 different signedness. We'll promote the signed to unsigned
3539 and later code won't know it used to be different.
3540 Do this check on the original types, so that explicit casts
3541 will be considered, but default promotions won't. */
3542 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare)
3543 && !skip_evaluation)
3545 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3546 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3548 if (unsigned_op1 ^ unsigned_op2)
3550 /* Do not warn if the result type is signed, since the
3551 signed type will only be chosen if it can represent
3552 all the values of the unsigned type. */
3553 if (! TREE_UNSIGNED (result_type))
3554 /* OK */;
3555 /* Do not warn if the signed quantity is an unsuffixed
3556 integer literal (or some static constant expression
3557 involving such literals) and it is non-negative. */
3558 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3559 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3560 /* OK */;
3561 else
3562 warning ("signed and unsigned type in conditional expression");
3566 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3568 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3569 pedwarn ("ISO C forbids conditional expr with only one void side");
3570 result_type = void_type_node;
3572 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3574 if (comp_target_types (type1, type2))
3575 result_type = common_type (type1, type2);
3576 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3577 && TREE_CODE (orig_op1) != NOP_EXPR)
3578 result_type = qualify_type (type2, type1);
3579 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3580 && TREE_CODE (orig_op2) != NOP_EXPR)
3581 result_type = qualify_type (type1, type2);
3582 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3584 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3585 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3586 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3587 TREE_TYPE (type2)));
3589 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3591 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3592 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3593 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3594 TREE_TYPE (type1)));
3596 else
3598 pedwarn ("pointer type mismatch in conditional expression");
3599 result_type = build_pointer_type (void_type_node);
3602 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3604 if (! integer_zerop (op2))
3605 pedwarn ("pointer/integer type mismatch in conditional expression");
3606 else
3608 op2 = null_pointer_node;
3610 result_type = type1;
3612 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3614 if (!integer_zerop (op1))
3615 pedwarn ("pointer/integer type mismatch in conditional expression");
3616 else
3618 op1 = null_pointer_node;
3620 result_type = type2;
3623 if (!result_type)
3625 if (flag_cond_mismatch)
3626 result_type = void_type_node;
3627 else
3629 error ("type mismatch in conditional expression");
3630 return error_mark_node;
3634 /* Merge const and volatile flags of the incoming types. */
3635 result_type
3636 = build_type_variant (result_type,
3637 TREE_READONLY (op1) || TREE_READONLY (op2),
3638 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3640 if (result_type != TREE_TYPE (op1))
3641 op1 = convert_and_check (result_type, op1);
3642 if (result_type != TREE_TYPE (op2))
3643 op2 = convert_and_check (result_type, op2);
3645 if (TREE_CODE (ifexp) == INTEGER_CST)
3646 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3648 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3651 /* Given a list of expressions, return a compound expression
3652 that performs them all and returns the value of the last of them. */
3654 tree
3655 build_compound_expr (list)
3656 tree list;
3658 return internal_build_compound_expr (list, TRUE);
3661 static tree
3662 internal_build_compound_expr (list, first_p)
3663 tree list;
3664 int first_p;
3666 tree rest;
3668 if (TREE_CHAIN (list) == 0)
3670 /* Convert arrays and functions to pointers when there
3671 really is a comma operator. */
3672 if (!first_p)
3673 TREE_VALUE (list)
3674 = default_function_array_conversion (TREE_VALUE (list));
3676 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3677 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3679 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3680 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3681 list = TREE_OPERAND (list, 0);
3682 #endif
3684 /* Don't let (0, 0) be null pointer constant. */
3685 if (!first_p && integer_zerop (TREE_VALUE (list)))
3686 return non_lvalue (TREE_VALUE (list));
3687 return TREE_VALUE (list);
3690 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3692 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3694 /* The left-hand operand of a comma expression is like an expression
3695 statement: with -W or -Wunused, we should warn if it doesn't have
3696 any side-effects, unless it was explicitly cast to (void). */
3697 if ((extra_warnings || warn_unused_value)
3698 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3699 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
3700 warning ("left-hand operand of comma expression has no effect");
3702 /* When pedantic, a compound expression can be neither an lvalue
3703 nor an integer constant expression. */
3704 if (! pedantic)
3705 return rest;
3708 /* With -Wunused, we should also warn if the left-hand operand does have
3709 side-effects, but computes a value which is not used. For example, in
3710 `foo() + bar(), baz()' the result of the `+' operator is not used,
3711 so we should issue a warning. */
3712 else if (warn_unused_value)
3713 warn_if_unused_value (TREE_VALUE (list));
3715 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3718 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3720 tree
3721 build_c_cast (type, expr)
3722 tree type;
3723 tree expr;
3725 tree value = expr;
3727 if (type == error_mark_node || expr == error_mark_node)
3728 return error_mark_node;
3729 type = TYPE_MAIN_VARIANT (type);
3731 #if 0
3732 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3733 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3734 value = TREE_OPERAND (value, 0);
3735 #endif
3737 if (TREE_CODE (type) == ARRAY_TYPE)
3739 error ("cast specifies array type");
3740 return error_mark_node;
3743 if (TREE_CODE (type) == FUNCTION_TYPE)
3745 error ("cast specifies function type");
3746 return error_mark_node;
3749 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3751 if (pedantic)
3753 if (TREE_CODE (type) == RECORD_TYPE
3754 || TREE_CODE (type) == UNION_TYPE)
3755 pedwarn ("ISO C forbids casting nonscalar to the same type");
3758 else if (TREE_CODE (type) == UNION_TYPE)
3760 tree field;
3761 value = default_function_array_conversion (value);
3763 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3764 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3765 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3766 break;
3768 if (field)
3770 const char *name;
3771 tree t;
3773 if (pedantic)
3774 pedwarn ("ISO C forbids casts to union type");
3775 if (TYPE_NAME (type) != 0)
3777 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3778 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3779 else
3780 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3782 else
3783 name = "";
3784 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3785 build_tree_list (field, value)),
3786 0, 0);
3787 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3788 return t;
3790 error ("cast to union type from type not present in union");
3791 return error_mark_node;
3793 else
3795 tree otype, ovalue;
3797 /* If casting to void, avoid the error that would come
3798 from default_conversion in the case of a non-lvalue array. */
3799 if (type == void_type_node)
3800 return build1 (CONVERT_EXPR, type, value);
3802 /* Convert functions and arrays to pointers,
3803 but don't convert any other types. */
3804 value = default_function_array_conversion (value);
3805 otype = TREE_TYPE (value);
3807 /* Optionally warn about potentially worrisome casts. */
3809 if (warn_cast_qual
3810 && TREE_CODE (type) == POINTER_TYPE
3811 && TREE_CODE (otype) == POINTER_TYPE)
3813 tree in_type = type;
3814 tree in_otype = otype;
3815 int warn = 0;
3817 /* Check that the qualifiers on IN_TYPE are a superset of
3818 the qualifiers of IN_OTYPE. The outermost level of
3819 POINTER_TYPE nodes is uninteresting and we stop as soon
3820 as we hit a non-POINTER_TYPE node on either type. */
3823 in_otype = TREE_TYPE (in_otype);
3824 in_type = TREE_TYPE (in_type);
3825 warn |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3827 while (TREE_CODE (in_type) == POINTER_TYPE
3828 && TREE_CODE (in_otype) == POINTER_TYPE);
3830 if (warn)
3831 /* There are qualifiers present in IN_OTYPE that are not
3832 present in IN_TYPE. */
3833 warning ("cast discards qualifiers from pointer target type");
3836 /* Warn about possible alignment problems. */
3837 if (STRICT_ALIGNMENT && warn_cast_align
3838 && TREE_CODE (type) == POINTER_TYPE
3839 && TREE_CODE (otype) == POINTER_TYPE
3840 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3841 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3842 /* Don't warn about opaque types, where the actual alignment
3843 restriction is unknown. */
3844 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3845 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3846 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3847 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3848 warning ("cast increases required alignment of target type");
3850 if (TREE_CODE (type) == INTEGER_TYPE
3851 && TREE_CODE (otype) == POINTER_TYPE
3852 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3853 && !TREE_CONSTANT (value))
3854 warning ("cast from pointer to integer of different size");
3856 if (warn_bad_function_cast
3857 && TREE_CODE (value) == CALL_EXPR
3858 && TREE_CODE (type) != TREE_CODE (otype))
3859 warning ("cast does not match function type");
3861 if (TREE_CODE (type) == POINTER_TYPE
3862 && TREE_CODE (otype) == INTEGER_TYPE
3863 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3864 /* Don't warn about converting any constant. */
3865 && !TREE_CONSTANT (value))
3866 warning ("cast to pointer from integer of different size");
3868 ovalue = value;
3869 value = convert (type, value);
3871 /* Ignore any integer overflow caused by the cast. */
3872 if (TREE_CODE (value) == INTEGER_CST)
3874 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3875 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3879 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3880 if (pedantic && TREE_CODE (value) == INTEGER_CST
3881 && TREE_CODE (expr) == INTEGER_CST
3882 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3883 value = non_lvalue (value);
3885 /* If pedantic, don't let a cast be an lvalue. */
3886 if (value == expr && pedantic)
3887 value = non_lvalue (value);
3889 return value;
3892 /* Interpret a cast of expression EXPR to type TYPE. */
3893 tree
3894 c_cast_expr (type, expr)
3895 tree type, expr;
3897 int saved_wsp = warn_strict_prototypes;
3899 /* This avoids warnings about unprototyped casts on
3900 integers. E.g. "#define SIG_DFL (void(*)())0". */
3901 if (TREE_CODE (expr) == INTEGER_CST)
3902 warn_strict_prototypes = 0;
3903 type = groktypename (type);
3904 warn_strict_prototypes = saved_wsp;
3906 return build_c_cast (type, expr);
3910 /* Build an assignment expression of lvalue LHS from value RHS.
3911 MODIFYCODE is the code for a binary operator that we use
3912 to combine the old value of LHS with RHS to get the new value.
3913 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3915 tree
3916 build_modify_expr (lhs, modifycode, rhs)
3917 tree lhs, rhs;
3918 enum tree_code modifycode;
3920 tree result;
3921 tree newrhs;
3922 tree lhstype = TREE_TYPE (lhs);
3923 tree olhstype = lhstype;
3925 /* Types that aren't fully specified cannot be used in assignments. */
3926 lhs = require_complete_type (lhs);
3928 /* Avoid duplicate error messages from operands that had errors. */
3929 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3930 return error_mark_node;
3932 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3933 /* Do not use STRIP_NOPS here. We do not want an enumerator
3934 whose value is 0 to count as a null pointer constant. */
3935 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3936 rhs = TREE_OPERAND (rhs, 0);
3938 newrhs = rhs;
3940 /* Handle control structure constructs used as "lvalues". */
3942 switch (TREE_CODE (lhs))
3944 /* Handle (a, b) used as an "lvalue". */
3945 case COMPOUND_EXPR:
3946 pedantic_lvalue_warning (COMPOUND_EXPR);
3947 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3948 if (TREE_CODE (newrhs) == ERROR_MARK)
3949 return error_mark_node;
3950 return build (COMPOUND_EXPR, lhstype,
3951 TREE_OPERAND (lhs, 0), newrhs);
3953 /* Handle (a ? b : c) used as an "lvalue". */
3954 case COND_EXPR:
3955 pedantic_lvalue_warning (COND_EXPR);
3956 rhs = save_expr (rhs);
3958 /* Produce (a ? (b = rhs) : (c = rhs))
3959 except that the RHS goes through a save-expr
3960 so the code to compute it is only emitted once. */
3961 tree cond
3962 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3963 build_modify_expr (TREE_OPERAND (lhs, 1),
3964 modifycode, rhs),
3965 build_modify_expr (TREE_OPERAND (lhs, 2),
3966 modifycode, rhs));
3967 if (TREE_CODE (cond) == ERROR_MARK)
3968 return cond;
3969 /* Make sure the code to compute the rhs comes out
3970 before the split. */
3971 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3972 /* But cast it to void to avoid an "unused" error. */
3973 convert (void_type_node, rhs), cond);
3975 default:
3976 break;
3979 /* If a binary op has been requested, combine the old LHS value with the RHS
3980 producing the value we should actually store into the LHS. */
3982 if (modifycode != NOP_EXPR)
3984 lhs = stabilize_reference (lhs);
3985 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3988 /* Handle a cast used as an "lvalue".
3989 We have already performed any binary operator using the value as cast.
3990 Now convert the result to the cast type of the lhs,
3991 and then true type of the lhs and store it there;
3992 then convert result back to the cast type to be the value
3993 of the assignment. */
3995 switch (TREE_CODE (lhs))
3997 case NOP_EXPR:
3998 case CONVERT_EXPR:
3999 case FLOAT_EXPR:
4000 case FIX_TRUNC_EXPR:
4001 case FIX_FLOOR_EXPR:
4002 case FIX_ROUND_EXPR:
4003 case FIX_CEIL_EXPR:
4004 newrhs = default_function_array_conversion (newrhs);
4006 tree inner_lhs = TREE_OPERAND (lhs, 0);
4007 tree result;
4008 result = build_modify_expr (inner_lhs, NOP_EXPR,
4009 convert (TREE_TYPE (inner_lhs),
4010 convert (lhstype, newrhs)));
4011 if (TREE_CODE (result) == ERROR_MARK)
4012 return result;
4013 pedantic_lvalue_warning (CONVERT_EXPR);
4014 return convert (TREE_TYPE (lhs), result);
4017 default:
4018 break;
4021 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
4022 Reject anything strange now. */
4024 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
4025 return error_mark_node;
4027 /* Warn about storing in something that is `const'. */
4029 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
4030 || ((TREE_CODE (lhstype) == RECORD_TYPE
4031 || TREE_CODE (lhstype) == UNION_TYPE)
4032 && C_TYPE_FIELDS_READONLY (lhstype)))
4033 readonly_warning (lhs, "assignment");
4035 /* If storing into a structure or union member,
4036 it has probably been given type `int'.
4037 Compute the type that would go with
4038 the actual amount of storage the member occupies. */
4040 if (TREE_CODE (lhs) == COMPONENT_REF
4041 && (TREE_CODE (lhstype) == INTEGER_TYPE
4042 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4043 || TREE_CODE (lhstype) == REAL_TYPE
4044 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4045 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4047 /* If storing in a field that is in actuality a short or narrower than one,
4048 we must store in the field in its actual type. */
4050 if (lhstype != TREE_TYPE (lhs))
4052 lhs = copy_node (lhs);
4053 TREE_TYPE (lhs) = lhstype;
4056 /* Convert new value to destination type. */
4058 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
4059 NULL_TREE, NULL_TREE, 0);
4060 if (TREE_CODE (newrhs) == ERROR_MARK)
4061 return error_mark_node;
4063 /* Scan operands */
4065 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4066 TREE_SIDE_EFFECTS (result) = 1;
4068 /* If we got the LHS in a different type for storing in,
4069 convert the result back to the nominal type of LHS
4070 so that the value we return always has the same type
4071 as the LHS argument. */
4073 if (olhstype == TREE_TYPE (result))
4074 return result;
4075 return convert_for_assignment (olhstype, result, _("assignment"),
4076 NULL_TREE, NULL_TREE, 0);
4079 /* Convert value RHS to type TYPE as preparation for an assignment
4080 to an lvalue of type TYPE.
4081 The real work of conversion is done by `convert'.
4082 The purpose of this function is to generate error messages
4083 for assignments that are not allowed in C.
4084 ERRTYPE is a string to use in error messages:
4085 "assignment", "return", etc. If it is null, this is parameter passing
4086 for a function call (and different error messages are output).
4088 FUNNAME is the name of the function being called,
4089 as an IDENTIFIER_NODE, or null.
4090 PARMNUM is the number of the argument, for printing in error messages. */
4092 static tree
4093 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
4094 tree type, rhs;
4095 const char *errtype;
4096 tree fundecl, funname;
4097 int parmnum;
4099 enum tree_code codel = TREE_CODE (type);
4100 tree rhstype;
4101 enum tree_code coder;
4103 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4104 /* Do not use STRIP_NOPS here. We do not want an enumerator
4105 whose value is 0 to count as a null pointer constant. */
4106 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4107 rhs = TREE_OPERAND (rhs, 0);
4109 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4110 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4111 rhs = default_conversion (rhs);
4112 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4113 rhs = decl_constant_value_for_broken_optimization (rhs);
4115 rhstype = TREE_TYPE (rhs);
4116 coder = TREE_CODE (rhstype);
4118 if (coder == ERROR_MARK)
4119 return error_mark_node;
4121 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4123 overflow_warning (rhs);
4124 /* Check for Objective-C protocols. This will issue a warning if
4125 there are protocol violations. No need to use the return value. */
4126 maybe_objc_comptypes (type, rhstype, 0);
4127 return rhs;
4130 if (coder == VOID_TYPE)
4132 error ("void value not ignored as it ought to be");
4133 return error_mark_node;
4135 /* A type converts to a reference to it.
4136 This code doesn't fully support references, it's just for the
4137 special case of va_start and va_copy. */
4138 if (codel == REFERENCE_TYPE
4139 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4141 if (mark_addressable (rhs) == 0)
4142 return error_mark_node;
4143 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4145 /* We already know that these two types are compatible, but they
4146 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4147 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4148 likely to be va_list, a typedef to __builtin_va_list, which
4149 is different enough that it will cause problems later. */
4150 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4151 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4153 rhs = build1 (NOP_EXPR, type, rhs);
4154 return rhs;
4156 /* Arithmetic types all interconvert, and enum is treated like int. */
4157 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4158 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4159 || codel == BOOLEAN_TYPE)
4160 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4161 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4162 || coder == BOOLEAN_TYPE))
4163 return convert_and_check (type, rhs);
4165 /* Conversion to a transparent union from its member types.
4166 This applies only to function arguments. */
4167 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4169 tree memb_types;
4170 tree marginal_memb_type = 0;
4172 for (memb_types = TYPE_FIELDS (type); memb_types;
4173 memb_types = TREE_CHAIN (memb_types))
4175 tree memb_type = TREE_TYPE (memb_types);
4177 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4178 TYPE_MAIN_VARIANT (rhstype)))
4179 break;
4181 if (TREE_CODE (memb_type) != POINTER_TYPE)
4182 continue;
4184 if (coder == POINTER_TYPE)
4186 tree ttl = TREE_TYPE (memb_type);
4187 tree ttr = TREE_TYPE (rhstype);
4189 /* Any non-function converts to a [const][volatile] void *
4190 and vice versa; otherwise, targets must be the same.
4191 Meanwhile, the lhs target must have all the qualifiers of
4192 the rhs. */
4193 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4194 || comp_target_types (memb_type, rhstype))
4196 /* If this type won't generate any warnings, use it. */
4197 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4198 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4199 && TREE_CODE (ttl) == FUNCTION_TYPE)
4200 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4201 == TYPE_QUALS (ttr))
4202 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4203 == TYPE_QUALS (ttl))))
4204 break;
4206 /* Keep looking for a better type, but remember this one. */
4207 if (! marginal_memb_type)
4208 marginal_memb_type = memb_type;
4212 /* Can convert integer zero to any pointer type. */
4213 if (integer_zerop (rhs)
4214 || (TREE_CODE (rhs) == NOP_EXPR
4215 && integer_zerop (TREE_OPERAND (rhs, 0))))
4217 rhs = null_pointer_node;
4218 break;
4222 if (memb_types || marginal_memb_type)
4224 if (! memb_types)
4226 /* We have only a marginally acceptable member type;
4227 it needs a warning. */
4228 tree ttl = TREE_TYPE (marginal_memb_type);
4229 tree ttr = TREE_TYPE (rhstype);
4231 /* Const and volatile mean something different for function
4232 types, so the usual warnings are not appropriate. */
4233 if (TREE_CODE (ttr) == FUNCTION_TYPE
4234 && TREE_CODE (ttl) == FUNCTION_TYPE)
4236 /* Because const and volatile on functions are
4237 restrictions that say the function will not do
4238 certain things, it is okay to use a const or volatile
4239 function where an ordinary one is wanted, but not
4240 vice-versa. */
4241 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4242 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4243 errtype, funname, parmnum);
4245 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4246 warn_for_assignment ("%s discards qualifiers from pointer target type",
4247 errtype, funname,
4248 parmnum);
4251 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4252 pedwarn ("ISO C prohibits argument conversion to union type");
4254 return build1 (NOP_EXPR, type, rhs);
4258 /* Conversions among pointers */
4259 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4260 && (coder == POINTER_TYPE || coder == REFERENCE_TYPE))
4262 tree ttl = TREE_TYPE (type);
4263 tree ttr = TREE_TYPE (rhstype);
4265 /* Any non-function converts to a [const][volatile] void *
4266 and vice versa; otherwise, targets must be the same.
4267 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4268 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4269 || comp_target_types (type, rhstype)
4270 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4271 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4273 if (pedantic
4274 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4276 (VOID_TYPE_P (ttr)
4277 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4278 which are not ANSI null ptr constants. */
4279 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4280 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4281 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
4282 errtype, funname, parmnum);
4283 /* Const and volatile mean something different for function types,
4284 so the usual warnings are not appropriate. */
4285 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4286 && TREE_CODE (ttl) != FUNCTION_TYPE)
4288 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4289 warn_for_assignment ("%s discards qualifiers from pointer target type",
4290 errtype, funname, parmnum);
4291 /* If this is not a case of ignoring a mismatch in signedness,
4292 no warning. */
4293 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4294 || comp_target_types (type, rhstype))
4296 /* If there is a mismatch, do warn. */
4297 else if (pedantic)
4298 warn_for_assignment ("pointer targets in %s differ in signedness",
4299 errtype, funname, parmnum);
4301 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4302 && TREE_CODE (ttr) == FUNCTION_TYPE)
4304 /* Because const and volatile on functions are restrictions
4305 that say the function will not do certain things,
4306 it is okay to use a const or volatile function
4307 where an ordinary one is wanted, but not vice-versa. */
4308 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4309 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4310 errtype, funname, parmnum);
4313 else
4314 warn_for_assignment ("%s from incompatible pointer type",
4315 errtype, funname, parmnum);
4316 return convert (type, rhs);
4318 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4320 /* An explicit constant 0 can convert to a pointer,
4321 or one that results from arithmetic, even including
4322 a cast to integer type. */
4323 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4325 ! (TREE_CODE (rhs) == NOP_EXPR
4326 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4327 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4328 && integer_zerop (TREE_OPERAND (rhs, 0))))
4330 warn_for_assignment ("%s makes pointer from integer without a cast",
4331 errtype, funname, parmnum);
4332 return convert (type, rhs);
4334 return null_pointer_node;
4336 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4338 warn_for_assignment ("%s makes integer from pointer without a cast",
4339 errtype, funname, parmnum);
4340 return convert (type, rhs);
4342 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4343 return convert (type, rhs);
4345 if (!errtype)
4347 if (funname)
4349 tree selector = maybe_building_objc_message_expr ();
4351 if (selector && parmnum > 2)
4352 error ("incompatible type for argument %d of `%s'",
4353 parmnum - 2, IDENTIFIER_POINTER (selector));
4354 else
4355 error ("incompatible type for argument %d of `%s'",
4356 parmnum, IDENTIFIER_POINTER (funname));
4358 else
4359 error ("incompatible type for argument %d of indirect function call",
4360 parmnum);
4362 else
4363 error ("incompatible types in %s", errtype);
4365 return error_mark_node;
4368 /* Print a warning using MSGID.
4369 It gets OPNAME as its one parameter.
4370 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4371 FUNCTION and ARGNUM are handled specially if we are building an
4372 Objective-C selector. */
4374 static void
4375 warn_for_assignment (msgid, opname, function, argnum)
4376 const char *msgid;
4377 const char *opname;
4378 tree function;
4379 int argnum;
4381 if (opname == 0)
4383 tree selector = maybe_building_objc_message_expr ();
4384 char * new_opname;
4386 if (selector && argnum > 2)
4388 function = selector;
4389 argnum -= 2;
4391 if (function)
4393 /* Function name is known; supply it. */
4394 const char *const argstring = _("passing arg %d of `%s'");
4395 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4396 + strlen (argstring) + 1 + 25
4397 /*%d*/ + 1);
4398 sprintf (new_opname, argstring, argnum,
4399 IDENTIFIER_POINTER (function));
4401 else
4403 /* Function name unknown (call through ptr); just give arg number.*/
4404 const char *const argnofun = _("passing arg %d of pointer to function");
4405 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4406 sprintf (new_opname, argnofun, argnum);
4408 opname = new_opname;
4410 pedwarn (msgid, opname);
4413 /* If VALUE is a compound expr all of whose expressions are constant, then
4414 return its value. Otherwise, return error_mark_node.
4416 This is for handling COMPOUND_EXPRs as initializer elements
4417 which is allowed with a warning when -pedantic is specified. */
4419 static tree
4420 valid_compound_expr_initializer (value, endtype)
4421 tree value;
4422 tree endtype;
4424 if (TREE_CODE (value) == COMPOUND_EXPR)
4426 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4427 == error_mark_node)
4428 return error_mark_node;
4429 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4430 endtype);
4432 else if (! TREE_CONSTANT (value)
4433 && ! initializer_constant_valid_p (value, endtype))
4434 return error_mark_node;
4435 else
4436 return value;
4439 /* Perform appropriate conversions on the initial value of a variable,
4440 store it in the declaration DECL,
4441 and print any error messages that are appropriate.
4442 If the init is invalid, store an ERROR_MARK. */
4444 void
4445 store_init_value (decl, init)
4446 tree decl, init;
4448 tree value, type;
4450 /* If variable's type was invalidly declared, just ignore it. */
4452 type = TREE_TYPE (decl);
4453 if (TREE_CODE (type) == ERROR_MARK)
4454 return;
4456 /* Digest the specified initializer into an expression. */
4458 value = digest_init (type, init, TREE_STATIC (decl),
4459 TREE_STATIC (decl) || (pedantic && !flag_isoc99));
4461 /* Store the expression if valid; else report error. */
4463 #if 0
4464 /* Note that this is the only place we can detect the error
4465 in a case such as struct foo bar = (struct foo) { x, y };
4466 where there is one initial value which is a constructor expression. */
4467 if (value == error_mark_node)
4469 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4471 error ("initializer for static variable is not constant");
4472 value = error_mark_node;
4474 else if (TREE_STATIC (decl)
4475 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4477 error ("initializer for static variable uses complicated arithmetic");
4478 value = error_mark_node;
4480 else
4482 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4484 if (! TREE_CONSTANT (value))
4485 pedwarn ("aggregate initializer is not constant");
4486 else if (! TREE_STATIC (value))
4487 pedwarn ("aggregate initializer uses complicated arithmetic");
4490 #endif
4492 if (warn_traditional && !in_system_header
4493 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
4494 warning ("traditional C rejects automatic aggregate initialization");
4496 DECL_INITIAL (decl) = value;
4498 /* ANSI wants warnings about out-of-range constant initializers. */
4499 STRIP_TYPE_NOPS (value);
4500 constant_expression_warning (value);
4503 /* Methods for storing and printing names for error messages. */
4505 /* Implement a spelling stack that allows components of a name to be pushed
4506 and popped. Each element on the stack is this structure. */
4508 struct spelling
4510 int kind;
4511 union
4513 int i;
4514 const char *s;
4515 } u;
4518 #define SPELLING_STRING 1
4519 #define SPELLING_MEMBER 2
4520 #define SPELLING_BOUNDS 3
4522 static struct spelling *spelling; /* Next stack element (unused). */
4523 static struct spelling *spelling_base; /* Spelling stack base. */
4524 static int spelling_size; /* Size of the spelling stack. */
4526 /* Macros to save and restore the spelling stack around push_... functions.
4527 Alternative to SAVE_SPELLING_STACK. */
4529 #define SPELLING_DEPTH() (spelling - spelling_base)
4530 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4532 /* Save and restore the spelling stack around arbitrary C code. */
4534 #define SAVE_SPELLING_DEPTH(code) \
4536 int __depth = SPELLING_DEPTH (); \
4537 code; \
4538 RESTORE_SPELLING_DEPTH (__depth); \
4541 /* Push an element on the spelling stack with type KIND and assign VALUE
4542 to MEMBER. */
4544 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4546 int depth = SPELLING_DEPTH (); \
4548 if (depth >= spelling_size) \
4550 spelling_size += 10; \
4551 if (spelling_base == 0) \
4552 spelling_base \
4553 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4554 else \
4555 spelling_base \
4556 = (struct spelling *) xrealloc (spelling_base, \
4557 spelling_size * sizeof (struct spelling)); \
4558 RESTORE_SPELLING_DEPTH (depth); \
4561 spelling->kind = (KIND); \
4562 spelling->MEMBER = (VALUE); \
4563 spelling++; \
4566 /* Push STRING on the stack. Printed literally. */
4568 static void
4569 push_string (string)
4570 const char *string;
4572 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4575 /* Push a member name on the stack. Printed as '.' STRING. */
4577 static void
4578 push_member_name (decl)
4579 tree decl;
4582 const char *const string
4583 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4584 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4587 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4589 static void
4590 push_array_bounds (bounds)
4591 int bounds;
4593 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4596 /* Compute the maximum size in bytes of the printed spelling. */
4598 static int
4599 spelling_length ()
4601 int size = 0;
4602 struct spelling *p;
4604 for (p = spelling_base; p < spelling; p++)
4606 if (p->kind == SPELLING_BOUNDS)
4607 size += 25;
4608 else
4609 size += strlen (p->u.s) + 1;
4612 return size;
4615 /* Print the spelling to BUFFER and return it. */
4617 static char *
4618 print_spelling (buffer)
4619 char *buffer;
4621 char *d = buffer;
4622 struct spelling *p;
4624 for (p = spelling_base; p < spelling; p++)
4625 if (p->kind == SPELLING_BOUNDS)
4627 sprintf (d, "[%d]", p->u.i);
4628 d += strlen (d);
4630 else
4632 const char *s;
4633 if (p->kind == SPELLING_MEMBER)
4634 *d++ = '.';
4635 for (s = p->u.s; (*d = *s++); d++)
4638 *d++ = '\0';
4639 return buffer;
4642 /* Issue an error message for a bad initializer component.
4643 MSGID identifies the message.
4644 The component name is taken from the spelling stack. */
4646 void
4647 error_init (msgid)
4648 const char *msgid;
4650 char *ofwhat;
4652 error ("%s", msgid);
4653 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4654 if (*ofwhat)
4655 error ("(near initialization for `%s')", ofwhat);
4658 /* Issue a pedantic warning for a bad initializer component.
4659 MSGID identifies the message.
4660 The component name is taken from the spelling stack. */
4662 void
4663 pedwarn_init (msgid)
4664 const char *msgid;
4666 char *ofwhat;
4668 pedwarn ("%s", msgid);
4669 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4670 if (*ofwhat)
4671 pedwarn ("(near initialization for `%s')", ofwhat);
4674 /* Issue a warning for a bad initializer component.
4675 MSGID identifies the message.
4676 The component name is taken from the spelling stack. */
4678 static void
4679 warning_init (msgid)
4680 const char *msgid;
4682 char *ofwhat;
4684 warning ("%s", msgid);
4685 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4686 if (*ofwhat)
4687 warning ("(near initialization for `%s')", ofwhat);
4690 /* Digest the parser output INIT as an initializer for type TYPE.
4691 Return a C expression of type TYPE to represent the initial value.
4693 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4694 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4695 applies only to elements of constructors. */
4697 static tree
4698 digest_init (type, init, require_constant, constructor_constant)
4699 tree type, init;
4700 int require_constant, constructor_constant;
4702 enum tree_code code = TREE_CODE (type);
4703 tree inside_init = init;
4705 if (type == error_mark_node
4706 || init == error_mark_node
4707 || TREE_TYPE (init) == error_mark_node)
4708 return error_mark_node;
4710 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4711 /* Do not use STRIP_NOPS here. We do not want an enumerator
4712 whose value is 0 to count as a null pointer constant. */
4713 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4714 inside_init = TREE_OPERAND (init, 0);
4716 inside_init = fold (inside_init);
4718 /* Initialization of an array of chars from a string constant
4719 optionally enclosed in braces. */
4721 if (code == ARRAY_TYPE)
4723 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4724 if ((typ1 == char_type_node
4725 || typ1 == signed_char_type_node
4726 || typ1 == unsigned_char_type_node
4727 || typ1 == unsigned_wchar_type_node
4728 || typ1 == signed_wchar_type_node)
4729 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4731 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4732 TYPE_MAIN_VARIANT (type)))
4733 return inside_init;
4735 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4736 != char_type_node)
4737 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4739 error_init ("char-array initialized from wide string");
4740 return error_mark_node;
4742 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4743 == char_type_node)
4744 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4746 error_init ("int-array initialized from non-wide string");
4747 return error_mark_node;
4750 TREE_TYPE (inside_init) = type;
4751 if (TYPE_DOMAIN (type) != 0
4752 && TYPE_SIZE (type) != 0
4753 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4754 /* Subtract 1 (or sizeof (wchar_t))
4755 because it's ok to ignore the terminating null char
4756 that is counted in the length of the constant. */
4757 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4758 TREE_STRING_LENGTH (inside_init)
4759 - ((TYPE_PRECISION (typ1)
4760 != TYPE_PRECISION (char_type_node))
4761 ? (TYPE_PRECISION (wchar_type_node)
4762 / BITS_PER_UNIT)
4763 : 1)))
4764 pedwarn_init ("initializer-string for array of chars is too long");
4766 return inside_init;
4770 /* Any type can be initialized
4771 from an expression of the same type, optionally with braces. */
4773 if (inside_init && TREE_TYPE (inside_init) != 0
4774 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4775 TYPE_MAIN_VARIANT (type))
4776 || (code == ARRAY_TYPE
4777 && comptypes (TREE_TYPE (inside_init), type))
4778 || (code == POINTER_TYPE
4779 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4780 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4781 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4782 TREE_TYPE (type)))))
4784 if (code == POINTER_TYPE)
4785 inside_init = default_function_array_conversion (inside_init);
4786 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4787 && TREE_CODE (inside_init) != CONSTRUCTOR)
4789 error_init ("array initialized from non-constant array expression");
4790 return error_mark_node;
4793 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4794 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4796 /* Compound expressions can only occur here if -pedantic or
4797 -pedantic-errors is specified. In the later case, we always want
4798 an error. In the former case, we simply want a warning. */
4799 if (require_constant && pedantic
4800 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4802 inside_init
4803 = valid_compound_expr_initializer (inside_init,
4804 TREE_TYPE (inside_init));
4805 if (inside_init == error_mark_node)
4806 error_init ("initializer element is not constant");
4807 else
4808 pedwarn_init ("initializer element is not constant");
4809 if (flag_pedantic_errors)
4810 inside_init = error_mark_node;
4812 else if (require_constant
4813 && (!TREE_CONSTANT (inside_init)
4814 /* This test catches things like `7 / 0' which
4815 result in an expression for which TREE_CONSTANT
4816 is true, but which is not actually something
4817 that is a legal constant. We really should not
4818 be using this function, because it is a part of
4819 the back-end. Instead, the expression should
4820 already have been turned into ERROR_MARK_NODE. */
4821 || !initializer_constant_valid_p (inside_init,
4822 TREE_TYPE (inside_init))))
4824 error_init ("initializer element is not constant");
4825 inside_init = error_mark_node;
4828 return inside_init;
4831 /* Handle scalar types, including conversions. */
4833 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4834 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4836 /* Note that convert_for_assignment calls default_conversion
4837 for arrays and functions. We must not call it in the
4838 case where inside_init is a null pointer constant. */
4839 inside_init
4840 = convert_for_assignment (type, init, _("initialization"),
4841 NULL_TREE, NULL_TREE, 0);
4843 if (require_constant && ! TREE_CONSTANT (inside_init))
4845 error_init ("initializer element is not constant");
4846 inside_init = error_mark_node;
4848 else if (require_constant
4849 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4851 error_init ("initializer element is not computable at load time");
4852 inside_init = error_mark_node;
4855 return inside_init;
4858 /* Come here only for records and arrays. */
4860 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4862 error_init ("variable-sized object may not be initialized");
4863 return error_mark_node;
4866 /* Traditionally, you can write struct foo x = 0;
4867 and it initializes the first element of x to 0. */
4868 if (flag_traditional)
4870 tree top = 0, prev = 0, otype = type;
4871 while (TREE_CODE (type) == RECORD_TYPE
4872 || TREE_CODE (type) == ARRAY_TYPE
4873 || TREE_CODE (type) == QUAL_UNION_TYPE
4874 || TREE_CODE (type) == UNION_TYPE)
4876 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4877 if (prev == 0)
4878 top = temp;
4879 else
4880 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4881 prev = temp;
4882 if (TREE_CODE (type) == ARRAY_TYPE)
4883 type = TREE_TYPE (type);
4884 else if (TYPE_FIELDS (type))
4885 type = TREE_TYPE (TYPE_FIELDS (type));
4886 else
4888 error_init ("invalid initializer");
4889 return error_mark_node;
4893 if (otype != type)
4895 TREE_OPERAND (prev, 1)
4896 = build_tree_list (NULL_TREE,
4897 digest_init (type, init, require_constant,
4898 constructor_constant));
4899 return top;
4901 else
4902 return error_mark_node;
4904 error_init ("invalid initializer");
4905 return error_mark_node;
4908 /* Handle initializers that use braces. */
4910 /* Type of object we are accumulating a constructor for.
4911 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4912 static tree constructor_type;
4914 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4915 left to fill. */
4916 static tree constructor_fields;
4918 /* For an ARRAY_TYPE, this is the specified index
4919 at which to store the next element we get. */
4920 static tree constructor_index;
4922 /* For an ARRAY_TYPE, this is the maximum index. */
4923 static tree constructor_max_index;
4925 /* For a RECORD_TYPE, this is the first field not yet written out. */
4926 static tree constructor_unfilled_fields;
4928 /* For an ARRAY_TYPE, this is the index of the first element
4929 not yet written out. */
4930 static tree constructor_unfilled_index;
4932 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4933 This is so we can generate gaps between fields, when appropriate. */
4934 static tree constructor_bit_index;
4936 /* If we are saving up the elements rather than allocating them,
4937 this is the list of elements so far (in reverse order,
4938 most recent first). */
4939 static tree constructor_elements;
4941 /* 1 if constructor should be incrementally stored into a constructor chain,
4942 0 if all the elements should be kept in AVL tree. */
4943 static int constructor_incremental;
4945 /* 1 if so far this constructor's elements are all compile-time constants. */
4946 static int constructor_constant;
4948 /* 1 if so far this constructor's elements are all valid address constants. */
4949 static int constructor_simple;
4951 /* 1 if this constructor is erroneous so far. */
4952 static int constructor_erroneous;
4954 /* 1 if have called defer_addressed_constants. */
4955 static int constructor_subconstants_deferred;
4957 /* Structure for managing pending initializer elements, organized as an
4958 AVL tree. */
4960 struct init_node
4962 struct init_node *left, *right;
4963 struct init_node *parent;
4964 int balance;
4965 tree purpose;
4966 tree value;
4969 /* Tree of pending elements at this constructor level.
4970 These are elements encountered out of order
4971 which belong at places we haven't reached yet in actually
4972 writing the output.
4973 Will never hold tree nodes across GC runs. */
4974 static struct init_node *constructor_pending_elts;
4976 /* The SPELLING_DEPTH of this constructor. */
4977 static int constructor_depth;
4979 /* 0 if implicitly pushing constructor levels is allowed. */
4980 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4982 static int require_constant_value;
4983 static int require_constant_elements;
4985 /* DECL node for which an initializer is being read.
4986 0 means we are reading a constructor expression
4987 such as (struct foo) {...}. */
4988 static tree constructor_decl;
4990 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4991 static const char *constructor_asmspec;
4993 /* Nonzero if this is an initializer for a top-level decl. */
4994 static int constructor_top_level;
4996 /* Nonzero if there were any member designators in this initializer. */
4997 static int constructor_designated;
4999 /* Nesting depth of designator list. */
5000 static int designator_depth;
5002 /* Nonzero if there were diagnosed errors in this designator list. */
5003 static int designator_errorneous;
5006 /* This stack has a level for each implicit or explicit level of
5007 structuring in the initializer, including the outermost one. It
5008 saves the values of most of the variables above. */
5010 struct constructor_range_stack;
5012 struct constructor_stack
5014 struct constructor_stack *next;
5015 tree type;
5016 tree fields;
5017 tree index;
5018 tree max_index;
5019 tree unfilled_index;
5020 tree unfilled_fields;
5021 tree bit_index;
5022 tree elements;
5023 struct init_node *pending_elts;
5024 int offset;
5025 int depth;
5026 /* If nonzero, this value should replace the entire
5027 constructor at this level. */
5028 tree replacement_value;
5029 struct constructor_range_stack *range_stack;
5030 char constant;
5031 char simple;
5032 char implicit;
5033 char erroneous;
5034 char outer;
5035 char incremental;
5036 char designated;
5039 struct constructor_stack *constructor_stack;
5041 /* This stack represents designators from some range designator up to
5042 the last designator in the list. */
5044 struct constructor_range_stack
5046 struct constructor_range_stack *next, *prev;
5047 struct constructor_stack *stack;
5048 tree range_start;
5049 tree index;
5050 tree range_end;
5051 tree fields;
5054 struct constructor_range_stack *constructor_range_stack;
5056 /* This stack records separate initializers that are nested.
5057 Nested initializers can't happen in ANSI C, but GNU C allows them
5058 in cases like { ... (struct foo) { ... } ... }. */
5060 struct initializer_stack
5062 struct initializer_stack *next;
5063 tree decl;
5064 const char *asmspec;
5065 struct constructor_stack *constructor_stack;
5066 struct constructor_range_stack *constructor_range_stack;
5067 tree elements;
5068 struct spelling *spelling;
5069 struct spelling *spelling_base;
5070 int spelling_size;
5071 char top_level;
5072 char require_constant_value;
5073 char require_constant_elements;
5074 char deferred;
5077 struct initializer_stack *initializer_stack;
5079 /* Prepare to parse and output the initializer for variable DECL. */
5081 void
5082 start_init (decl, asmspec_tree, top_level)
5083 tree decl;
5084 tree asmspec_tree;
5085 int top_level;
5087 const char *locus;
5088 struct initializer_stack *p
5089 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5090 const char *asmspec = 0;
5092 if (asmspec_tree)
5093 asmspec = TREE_STRING_POINTER (asmspec_tree);
5095 p->decl = constructor_decl;
5096 p->asmspec = constructor_asmspec;
5097 p->require_constant_value = require_constant_value;
5098 p->require_constant_elements = require_constant_elements;
5099 p->constructor_stack = constructor_stack;
5100 p->constructor_range_stack = constructor_range_stack;
5101 p->elements = constructor_elements;
5102 p->spelling = spelling;
5103 p->spelling_base = spelling_base;
5104 p->spelling_size = spelling_size;
5105 p->deferred = constructor_subconstants_deferred;
5106 p->top_level = constructor_top_level;
5107 p->next = initializer_stack;
5108 initializer_stack = p;
5110 constructor_decl = decl;
5111 constructor_asmspec = asmspec;
5112 constructor_subconstants_deferred = 0;
5113 constructor_designated = 0;
5114 constructor_top_level = top_level;
5116 if (decl != 0)
5118 require_constant_value = TREE_STATIC (decl);
5119 require_constant_elements
5120 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5121 /* For a scalar, you can always use any value to initialize,
5122 even within braces. */
5123 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5124 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5125 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5126 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5127 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5129 else
5131 require_constant_value = 0;
5132 require_constant_elements = 0;
5133 locus = "(anonymous)";
5136 constructor_stack = 0;
5137 constructor_range_stack = 0;
5139 missing_braces_mentioned = 0;
5141 spelling_base = 0;
5142 spelling_size = 0;
5143 RESTORE_SPELLING_DEPTH (0);
5145 if (locus)
5146 push_string (locus);
5149 void
5150 finish_init ()
5152 struct initializer_stack *p = initializer_stack;
5154 /* Output subconstants (string constants, usually)
5155 that were referenced within this initializer and saved up.
5156 Must do this if and only if we called defer_addressed_constants. */
5157 if (constructor_subconstants_deferred)
5158 output_deferred_addressed_constants ();
5160 /* Free the whole constructor stack of this initializer. */
5161 while (constructor_stack)
5163 struct constructor_stack *q = constructor_stack;
5164 constructor_stack = q->next;
5165 free (q);
5168 if (constructor_range_stack)
5169 abort ();
5171 /* Pop back to the data of the outer initializer (if any). */
5172 constructor_decl = p->decl;
5173 constructor_asmspec = p->asmspec;
5174 require_constant_value = p->require_constant_value;
5175 require_constant_elements = p->require_constant_elements;
5176 constructor_stack = p->constructor_stack;
5177 constructor_range_stack = p->constructor_range_stack;
5178 constructor_elements = p->elements;
5179 spelling = p->spelling;
5180 spelling_base = p->spelling_base;
5181 spelling_size = p->spelling_size;
5182 constructor_subconstants_deferred = p->deferred;
5183 constructor_top_level = p->top_level;
5184 initializer_stack = p->next;
5185 free (p);
5188 /* Call here when we see the initializer is surrounded by braces.
5189 This is instead of a call to push_init_level;
5190 it is matched by a call to pop_init_level.
5192 TYPE is the type to initialize, for a constructor expression.
5193 For an initializer for a decl, TYPE is zero. */
5195 void
5196 really_start_incremental_init (type)
5197 tree type;
5199 struct constructor_stack *p
5200 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5202 if (type == 0)
5203 type = TREE_TYPE (constructor_decl);
5205 p->type = constructor_type;
5206 p->fields = constructor_fields;
5207 p->index = constructor_index;
5208 p->max_index = constructor_max_index;
5209 p->unfilled_index = constructor_unfilled_index;
5210 p->unfilled_fields = constructor_unfilled_fields;
5211 p->bit_index = constructor_bit_index;
5212 p->elements = constructor_elements;
5213 p->constant = constructor_constant;
5214 p->simple = constructor_simple;
5215 p->erroneous = constructor_erroneous;
5216 p->pending_elts = constructor_pending_elts;
5217 p->depth = constructor_depth;
5218 p->replacement_value = 0;
5219 p->implicit = 0;
5220 p->range_stack = 0;
5221 p->outer = 0;
5222 p->incremental = constructor_incremental;
5223 p->designated = constructor_designated;
5224 p->next = 0;
5225 constructor_stack = p;
5227 constructor_constant = 1;
5228 constructor_simple = 1;
5229 constructor_depth = SPELLING_DEPTH ();
5230 constructor_elements = 0;
5231 constructor_pending_elts = 0;
5232 constructor_type = type;
5233 constructor_incremental = 1;
5234 constructor_designated = 0;
5235 designator_depth = 0;
5236 designator_errorneous = 0;
5238 if (TREE_CODE (constructor_type) == RECORD_TYPE
5239 || TREE_CODE (constructor_type) == UNION_TYPE)
5241 constructor_fields = TYPE_FIELDS (constructor_type);
5242 /* Skip any nameless bit fields at the beginning. */
5243 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5244 && DECL_NAME (constructor_fields) == 0)
5245 constructor_fields = TREE_CHAIN (constructor_fields);
5247 constructor_unfilled_fields = constructor_fields;
5248 constructor_bit_index = bitsize_zero_node;
5250 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5252 if (TYPE_DOMAIN (constructor_type))
5254 constructor_max_index
5255 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5257 /* Detect non-empty initializations of zero-length arrays. */
5258 if (constructor_max_index == NULL_TREE
5259 && TYPE_SIZE (constructor_type))
5260 constructor_max_index = build_int_2 (-1, -1);
5262 constructor_index
5263 = convert (bitsizetype,
5264 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5266 else
5267 constructor_index = bitsize_zero_node;
5269 constructor_unfilled_index = constructor_index;
5271 else
5273 /* Handle the case of int x = {5}; */
5274 constructor_fields = constructor_type;
5275 constructor_unfilled_fields = constructor_type;
5279 /* Push down into a subobject, for initialization.
5280 If this is for an explicit set of braces, IMPLICIT is 0.
5281 If it is because the next element belongs at a lower level,
5282 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5284 void
5285 push_init_level (implicit)
5286 int implicit;
5288 struct constructor_stack *p;
5289 tree value = NULL_TREE;
5291 /* If we've exhausted any levels that didn't have braces,
5292 pop them now. */
5293 while (constructor_stack->implicit)
5295 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5296 || TREE_CODE (constructor_type) == UNION_TYPE)
5297 && constructor_fields == 0)
5298 process_init_element (pop_init_level (1));
5299 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5300 && tree_int_cst_lt (constructor_max_index, constructor_index))
5301 process_init_element (pop_init_level (1));
5302 else
5303 break;
5306 /* Unless this is an explicit brace, we need to preserve previous
5307 content if any. */
5308 if (implicit)
5310 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5311 || TREE_CODE (constructor_type) == UNION_TYPE)
5312 && constructor_fields)
5313 value = find_init_member (constructor_fields);
5314 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5315 value = find_init_member (constructor_index);
5318 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5319 p->type = constructor_type;
5320 p->fields = constructor_fields;
5321 p->index = constructor_index;
5322 p->max_index = constructor_max_index;
5323 p->unfilled_index = constructor_unfilled_index;
5324 p->unfilled_fields = constructor_unfilled_fields;
5325 p->bit_index = constructor_bit_index;
5326 p->elements = constructor_elements;
5327 p->constant = constructor_constant;
5328 p->simple = constructor_simple;
5329 p->erroneous = constructor_erroneous;
5330 p->pending_elts = constructor_pending_elts;
5331 p->depth = constructor_depth;
5332 p->replacement_value = 0;
5333 p->implicit = implicit;
5334 p->outer = 0;
5335 p->incremental = constructor_incremental;
5336 p->designated = constructor_designated;
5337 p->next = constructor_stack;
5338 p->range_stack = 0;
5339 constructor_stack = p;
5341 constructor_constant = 1;
5342 constructor_simple = 1;
5343 constructor_depth = SPELLING_DEPTH ();
5344 constructor_elements = 0;
5345 constructor_incremental = 1;
5346 constructor_designated = 0;
5347 constructor_pending_elts = 0;
5348 if (!implicit)
5350 p->range_stack = constructor_range_stack;
5351 constructor_range_stack = 0;
5352 designator_depth = 0;
5353 designator_errorneous = 0;
5356 /* Don't die if an entire brace-pair level is superfluous
5357 in the containing level. */
5358 if (constructor_type == 0)
5360 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5361 || TREE_CODE (constructor_type) == UNION_TYPE)
5363 /* Don't die if there are extra init elts at the end. */
5364 if (constructor_fields == 0)
5365 constructor_type = 0;
5366 else
5368 constructor_type = TREE_TYPE (constructor_fields);
5369 push_member_name (constructor_fields);
5370 constructor_depth++;
5373 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5375 constructor_type = TREE_TYPE (constructor_type);
5376 push_array_bounds (tree_low_cst (constructor_index, 0));
5377 constructor_depth++;
5380 if (constructor_type == 0)
5382 error_init ("extra brace group at end of initializer");
5383 constructor_fields = 0;
5384 constructor_unfilled_fields = 0;
5385 return;
5388 if (value && TREE_CODE (value) == CONSTRUCTOR)
5390 constructor_constant = TREE_CONSTANT (value);
5391 constructor_simple = TREE_STATIC (value);
5392 constructor_elements = TREE_OPERAND (value, 1);
5393 if (constructor_elements
5394 && (TREE_CODE (constructor_type) == RECORD_TYPE
5395 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5396 set_nonincremental_init ();
5399 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5401 missing_braces_mentioned = 1;
5402 warning_init ("missing braces around initializer");
5405 if (TREE_CODE (constructor_type) == RECORD_TYPE
5406 || TREE_CODE (constructor_type) == UNION_TYPE)
5408 constructor_fields = TYPE_FIELDS (constructor_type);
5409 /* Skip any nameless bit fields at the beginning. */
5410 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5411 && DECL_NAME (constructor_fields) == 0)
5412 constructor_fields = TREE_CHAIN (constructor_fields);
5414 constructor_unfilled_fields = constructor_fields;
5415 constructor_bit_index = bitsize_zero_node;
5417 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5419 if (TYPE_DOMAIN (constructor_type))
5421 constructor_max_index
5422 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5424 /* Detect non-empty initializations of zero-length arrays. */
5425 if (constructor_max_index == NULL_TREE
5426 && TYPE_SIZE (constructor_type))
5427 constructor_max_index = build_int_2 (-1, -1);
5429 constructor_index
5430 = convert (bitsizetype,
5431 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5433 else
5434 constructor_index = bitsize_zero_node;
5436 constructor_unfilled_index = constructor_index;
5437 if (value && TREE_CODE (value) == STRING_CST)
5439 /* We need to split the char/wchar array into individual
5440 characters, so that we don't have to special case it
5441 everywhere. */
5442 set_nonincremental_init_from_string (value);
5445 else
5447 warning_init ("braces around scalar initializer");
5448 constructor_fields = constructor_type;
5449 constructor_unfilled_fields = constructor_type;
5453 /* At the end of an implicit or explicit brace level,
5454 finish up that level of constructor.
5455 If we were outputting the elements as they are read, return 0
5456 from inner levels (process_init_element ignores that),
5457 but return error_mark_node from the outermost level
5458 (that's what we want to put in DECL_INITIAL).
5459 Otherwise, return a CONSTRUCTOR expression. */
5461 tree
5462 pop_init_level (implicit)
5463 int implicit;
5465 struct constructor_stack *p;
5466 tree constructor = 0;
5468 if (implicit == 0)
5470 /* When we come to an explicit close brace,
5471 pop any inner levels that didn't have explicit braces. */
5472 while (constructor_stack->implicit)
5473 process_init_element (pop_init_level (1));
5475 if (constructor_range_stack)
5476 abort ();
5479 p = constructor_stack;
5481 /* Error for initializing a flexible array member, or a zero-length
5482 array member in an inappropriate context. */
5483 if (constructor_type && constructor_fields
5484 && TREE_CODE (constructor_type) == ARRAY_TYPE
5485 && TYPE_DOMAIN (constructor_type)
5486 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5488 /* Silently discard empty initializations. The parser will
5489 already have pedwarned for empty brackets. */
5490 if (integer_zerop (constructor_unfilled_index))
5491 constructor_type = NULL_TREE;
5492 else if (! TYPE_SIZE (constructor_type))
5494 if (constructor_depth > 2)
5495 error_init ("initialization of flexible array member in a nested context");
5496 else if (pedantic)
5497 pedwarn_init ("initialization of a flexible array member");
5499 /* We have already issued an error message for the existence
5500 of a flexible array member not at the end of the structure.
5501 Discard the initializer so that we do not abort later. */
5502 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5503 constructor_type = NULL_TREE;
5505 else
5506 /* Zero-length arrays are no longer special, so we should no longer
5507 get here. */
5508 abort();
5511 /* Warn when some struct elements are implicitly initialized to zero. */
5512 if (extra_warnings
5513 && constructor_type
5514 && TREE_CODE (constructor_type) == RECORD_TYPE
5515 && constructor_unfilled_fields)
5517 /* Do not warn for flexible array members or zero-length arrays. */
5518 while (constructor_unfilled_fields
5519 && (! DECL_SIZE (constructor_unfilled_fields)
5520 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5521 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5523 /* Do not warn if this level of the initializer uses member
5524 designators; it is likely to be deliberate. */
5525 if (constructor_unfilled_fields && !constructor_designated)
5527 push_member_name (constructor_unfilled_fields);
5528 warning_init ("missing initializer");
5529 RESTORE_SPELLING_DEPTH (constructor_depth);
5533 /* Now output all pending elements. */
5534 constructor_incremental = 1;
5535 output_pending_init_elements (1);
5537 /* Pad out the end of the structure. */
5538 if (p->replacement_value)
5539 /* If this closes a superfluous brace pair,
5540 just pass out the element between them. */
5541 constructor = p->replacement_value;
5542 else if (constructor_type == 0)
5544 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5545 && TREE_CODE (constructor_type) != UNION_TYPE
5546 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5548 /* A nonincremental scalar initializer--just return
5549 the element, after verifying there is just one. */
5550 if (constructor_elements == 0)
5552 if (!constructor_erroneous)
5553 error_init ("empty scalar initializer");
5554 constructor = error_mark_node;
5556 else if (TREE_CHAIN (constructor_elements) != 0)
5558 error_init ("extra elements in scalar initializer");
5559 constructor = TREE_VALUE (constructor_elements);
5561 else
5562 constructor = TREE_VALUE (constructor_elements);
5564 else
5566 if (constructor_erroneous)
5567 constructor = error_mark_node;
5568 else
5570 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5571 nreverse (constructor_elements));
5572 if (constructor_constant)
5573 TREE_CONSTANT (constructor) = 1;
5574 if (constructor_constant && constructor_simple)
5575 TREE_STATIC (constructor) = 1;
5579 constructor_type = p->type;
5580 constructor_fields = p->fields;
5581 constructor_index = p->index;
5582 constructor_max_index = p->max_index;
5583 constructor_unfilled_index = p->unfilled_index;
5584 constructor_unfilled_fields = p->unfilled_fields;
5585 constructor_bit_index = p->bit_index;
5586 constructor_elements = p->elements;
5587 constructor_constant = p->constant;
5588 constructor_simple = p->simple;
5589 constructor_erroneous = p->erroneous;
5590 constructor_incremental = p->incremental;
5591 constructor_designated = p->designated;
5592 constructor_pending_elts = p->pending_elts;
5593 constructor_depth = p->depth;
5594 if (!p->implicit)
5595 constructor_range_stack = p->range_stack;
5596 RESTORE_SPELLING_DEPTH (constructor_depth);
5598 constructor_stack = p->next;
5599 free (p);
5601 if (constructor == 0)
5603 if (constructor_stack == 0)
5604 return error_mark_node;
5605 return NULL_TREE;
5607 return constructor;
5610 /* Common handling for both array range and field name designators.
5611 ARRAY argument is non-zero for array ranges. Returns zero for success. */
5613 static int
5614 set_designator (array)
5615 int array;
5617 tree subtype;
5618 enum tree_code subcode;
5620 /* Don't die if an entire brace-pair level is superfluous
5621 in the containing level. */
5622 if (constructor_type == 0)
5623 return 1;
5625 /* If there were errors in this designator list already, bail out silently. */
5626 if (designator_errorneous)
5627 return 1;
5629 if (!designator_depth)
5631 if (constructor_range_stack)
5632 abort ();
5634 /* Designator list starts at the level of closest explicit
5635 braces. */
5636 while (constructor_stack->implicit)
5637 process_init_element (pop_init_level (1));
5638 constructor_designated = 1;
5639 return 0;
5642 if (constructor_no_implicit)
5644 error_init ("initialization designators may not nest");
5645 return 1;
5648 if (TREE_CODE (constructor_type) == RECORD_TYPE
5649 || TREE_CODE (constructor_type) == UNION_TYPE)
5651 subtype = TREE_TYPE (constructor_fields);
5652 if (subtype != error_mark_node)
5653 subtype = TYPE_MAIN_VARIANT (subtype);
5655 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5657 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5659 else
5660 abort ();
5662 subcode = TREE_CODE (subtype);
5663 if (array && subcode != ARRAY_TYPE)
5665 error_init ("array index in non-array initializer");
5666 return 1;
5668 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5670 error_init ("field name not in record or union initializer");
5671 return 1;
5674 constructor_designated = 1;
5675 push_init_level (2);
5676 return 0;
5679 /* If there are range designators in designator list, push a new designator
5680 to constructor_range_stack. RANGE_END is end of such stack range or
5681 NULL_TREE if there is no range designator at this level. */
5683 static void
5684 push_range_stack (range_end)
5685 tree range_end;
5687 struct constructor_range_stack *p;
5689 p = (struct constructor_range_stack *)
5690 ggc_alloc (sizeof (struct constructor_range_stack));
5691 p->prev = constructor_range_stack;
5692 p->next = 0;
5693 p->fields = constructor_fields;
5694 p->range_start = constructor_index;
5695 p->index = constructor_index;
5696 p->stack = constructor_stack;
5697 p->range_end = range_end;
5698 if (constructor_range_stack)
5699 constructor_range_stack->next = p;
5700 constructor_range_stack = p;
5703 /* Within an array initializer, specify the next index to be initialized.
5704 FIRST is that index. If LAST is nonzero, then initialize a range
5705 of indices, running from FIRST through LAST. */
5707 void
5708 set_init_index (first, last)
5709 tree first, last;
5711 if (set_designator (1))
5712 return;
5714 designator_errorneous = 1;
5716 while ((TREE_CODE (first) == NOP_EXPR
5717 || TREE_CODE (first) == CONVERT_EXPR
5718 || TREE_CODE (first) == NON_LVALUE_EXPR)
5719 && (TYPE_MODE (TREE_TYPE (first))
5720 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5721 first = TREE_OPERAND (first, 0);
5723 if (last)
5724 while ((TREE_CODE (last) == NOP_EXPR
5725 || TREE_CODE (last) == CONVERT_EXPR
5726 || TREE_CODE (last) == NON_LVALUE_EXPR)
5727 && (TYPE_MODE (TREE_TYPE (last))
5728 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5729 last = TREE_OPERAND (last, 0);
5731 if (TREE_CODE (first) != INTEGER_CST)
5732 error_init ("nonconstant array index in initializer");
5733 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5734 error_init ("nonconstant array index in initializer");
5735 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5736 error_init ("array index in non-array initializer");
5737 else if (constructor_max_index
5738 && tree_int_cst_lt (constructor_max_index, first))
5739 error_init ("array index in initializer exceeds array bounds");
5740 else
5742 constructor_index = convert (bitsizetype, first);
5744 if (last)
5746 if (tree_int_cst_equal (first, last))
5747 last = 0;
5748 else if (tree_int_cst_lt (last, first))
5750 error_init ("empty index range in initializer");
5751 last = 0;
5753 else
5755 last = convert (bitsizetype, last);
5756 if (constructor_max_index != 0
5757 && tree_int_cst_lt (constructor_max_index, last))
5759 error_init ("array index range in initializer exceeds array bounds");
5760 last = 0;
5765 designator_depth++;
5766 designator_errorneous = 0;
5767 if (constructor_range_stack || last)
5768 push_range_stack (last);
5772 /* Within a struct initializer, specify the next field to be initialized. */
5774 void
5775 set_init_label (fieldname)
5776 tree fieldname;
5778 tree tail;
5780 if (set_designator (0))
5781 return;
5783 designator_errorneous = 1;
5785 if (TREE_CODE (constructor_type) != RECORD_TYPE
5786 && TREE_CODE (constructor_type) != UNION_TYPE)
5788 error_init ("field name not in record or union initializer");
5789 return;
5792 for (tail = TYPE_FIELDS (constructor_type); tail;
5793 tail = TREE_CHAIN (tail))
5795 if (DECL_NAME (tail) == fieldname)
5796 break;
5799 if (tail == 0)
5800 error ("unknown field `%s' specified in initializer",
5801 IDENTIFIER_POINTER (fieldname));
5802 else
5804 constructor_fields = tail;
5805 designator_depth++;
5806 designator_errorneous = 0;
5807 if (constructor_range_stack)
5808 push_range_stack (NULL_TREE);
5812 /* Add a new initializer to the tree of pending initializers. PURPOSE
5813 identifies the initializer, either array index or field in a structure.
5814 VALUE is the value of that index or field. */
5816 static void
5817 add_pending_init (purpose, value)
5818 tree purpose, value;
5820 struct init_node *p, **q, *r;
5822 q = &constructor_pending_elts;
5823 p = 0;
5825 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5827 while (*q != 0)
5829 p = *q;
5830 if (tree_int_cst_lt (purpose, p->purpose))
5831 q = &p->left;
5832 else if (tree_int_cst_lt (p->purpose, purpose))
5833 q = &p->right;
5834 else
5836 if (TREE_SIDE_EFFECTS (p->value))
5837 warning_init ("initialized field with side-effects overwritten");
5838 p->value = value;
5839 return;
5843 else
5845 tree bitpos;
5847 bitpos = bit_position (purpose);
5848 while (*q != NULL)
5850 p = *q;
5851 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5852 q = &p->left;
5853 else if (p->purpose != purpose)
5854 q = &p->right;
5855 else
5857 if (TREE_SIDE_EFFECTS (p->value))
5858 warning_init ("initialized field with side-effects overwritten");
5859 p->value = value;
5860 return;
5865 r = (struct init_node *) ggc_alloc (sizeof (struct init_node));
5866 r->purpose = purpose;
5867 r->value = value;
5869 *q = r;
5870 r->parent = p;
5871 r->left = 0;
5872 r->right = 0;
5873 r->balance = 0;
5875 while (p)
5877 struct init_node *s;
5879 if (r == p->left)
5881 if (p->balance == 0)
5882 p->balance = -1;
5883 else if (p->balance < 0)
5885 if (r->balance < 0)
5887 /* L rotation. */
5888 p->left = r->right;
5889 if (p->left)
5890 p->left->parent = p;
5891 r->right = p;
5893 p->balance = 0;
5894 r->balance = 0;
5896 s = p->parent;
5897 p->parent = r;
5898 r->parent = s;
5899 if (s)
5901 if (s->left == p)
5902 s->left = r;
5903 else
5904 s->right = r;
5906 else
5907 constructor_pending_elts = r;
5909 else
5911 /* LR rotation. */
5912 struct init_node *t = r->right;
5914 r->right = t->left;
5915 if (r->right)
5916 r->right->parent = r;
5917 t->left = r;
5919 p->left = t->right;
5920 if (p->left)
5921 p->left->parent = p;
5922 t->right = p;
5924 p->balance = t->balance < 0;
5925 r->balance = -(t->balance > 0);
5926 t->balance = 0;
5928 s = p->parent;
5929 p->parent = t;
5930 r->parent = t;
5931 t->parent = s;
5932 if (s)
5934 if (s->left == p)
5935 s->left = t;
5936 else
5937 s->right = t;
5939 else
5940 constructor_pending_elts = t;
5942 break;
5944 else
5946 /* p->balance == +1; growth of left side balances the node. */
5947 p->balance = 0;
5948 break;
5951 else /* r == p->right */
5953 if (p->balance == 0)
5954 /* Growth propagation from right side. */
5955 p->balance++;
5956 else if (p->balance > 0)
5958 if (r->balance > 0)
5960 /* R rotation. */
5961 p->right = r->left;
5962 if (p->right)
5963 p->right->parent = p;
5964 r->left = p;
5966 p->balance = 0;
5967 r->balance = 0;
5969 s = p->parent;
5970 p->parent = r;
5971 r->parent = s;
5972 if (s)
5974 if (s->left == p)
5975 s->left = r;
5976 else
5977 s->right = r;
5979 else
5980 constructor_pending_elts = r;
5982 else /* r->balance == -1 */
5984 /* RL rotation */
5985 struct init_node *t = r->left;
5987 r->left = t->right;
5988 if (r->left)
5989 r->left->parent = r;
5990 t->right = r;
5992 p->right = t->left;
5993 if (p->right)
5994 p->right->parent = p;
5995 t->left = p;
5997 r->balance = (t->balance < 0);
5998 p->balance = -(t->balance > 0);
5999 t->balance = 0;
6001 s = p->parent;
6002 p->parent = t;
6003 r->parent = t;
6004 t->parent = s;
6005 if (s)
6007 if (s->left == p)
6008 s->left = t;
6009 else
6010 s->right = t;
6012 else
6013 constructor_pending_elts = t;
6015 break;
6017 else
6019 /* p->balance == -1; growth of right side balances the node. */
6020 p->balance = 0;
6021 break;
6025 r = p;
6026 p = p->parent;
6030 /* Build AVL tree from a sorted chain. */
6032 static void
6033 set_nonincremental_init ()
6035 tree chain;
6037 if (TREE_CODE (constructor_type) != RECORD_TYPE
6038 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6039 return;
6041 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
6042 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
6043 constructor_elements = 0;
6044 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6046 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6047 /* Skip any nameless bit fields at the beginning. */
6048 while (constructor_unfilled_fields != 0
6049 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6050 && DECL_NAME (constructor_unfilled_fields) == 0)
6051 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6054 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6056 if (TYPE_DOMAIN (constructor_type))
6057 constructor_unfilled_index
6058 = convert (bitsizetype,
6059 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6060 else
6061 constructor_unfilled_index = bitsize_zero_node;
6063 constructor_incremental = 0;
6066 /* Build AVL tree from a string constant. */
6068 static void
6069 set_nonincremental_init_from_string (str)
6070 tree str;
6072 tree value, purpose, type;
6073 HOST_WIDE_INT val[2];
6074 const char *p, *end;
6075 int byte, wchar_bytes, charwidth, bitpos;
6077 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6078 abort ();
6080 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6081 == TYPE_PRECISION (char_type_node))
6082 wchar_bytes = 1;
6083 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6084 == TYPE_PRECISION (wchar_type_node))
6085 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6086 else
6087 abort ();
6089 charwidth = TYPE_PRECISION (char_type_node);
6090 type = TREE_TYPE (constructor_type);
6091 p = TREE_STRING_POINTER (str);
6092 end = p + TREE_STRING_LENGTH (str);
6094 for (purpose = bitsize_zero_node;
6095 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6096 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6098 if (wchar_bytes == 1)
6100 val[1] = (unsigned char) *p++;
6101 val[0] = 0;
6103 else
6105 val[0] = 0;
6106 val[1] = 0;
6107 for (byte = 0; byte < wchar_bytes; byte++)
6109 if (BYTES_BIG_ENDIAN)
6110 bitpos = (wchar_bytes - byte - 1) * charwidth;
6111 else
6112 bitpos = byte * charwidth;
6113 val[bitpos < HOST_BITS_PER_WIDE_INT]
6114 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6115 << (bitpos % HOST_BITS_PER_WIDE_INT);
6119 if (!TREE_UNSIGNED (type))
6121 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6122 if (bitpos < HOST_BITS_PER_WIDE_INT)
6124 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6126 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6127 val[0] = -1;
6130 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6132 if (val[1] < 0)
6133 val[0] = -1;
6135 else if (val[0] & (((HOST_WIDE_INT) 1)
6136 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6137 val[0] |= ((HOST_WIDE_INT) -1)
6138 << (bitpos - HOST_BITS_PER_WIDE_INT);
6141 value = build_int_2 (val[1], val[0]);
6142 TREE_TYPE (value) = type;
6143 add_pending_init (purpose, value);
6146 constructor_incremental = 0;
6149 /* Return value of FIELD in pending initializer or zero if the field was
6150 not initialized yet. */
6152 static tree
6153 find_init_member (field)
6154 tree field;
6156 struct init_node *p;
6158 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6160 if (constructor_incremental
6161 && tree_int_cst_lt (field, constructor_unfilled_index))
6162 set_nonincremental_init ();
6164 p = constructor_pending_elts;
6165 while (p)
6167 if (tree_int_cst_lt (field, p->purpose))
6168 p = p->left;
6169 else if (tree_int_cst_lt (p->purpose, field))
6170 p = p->right;
6171 else
6172 return p->value;
6175 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6177 tree bitpos = bit_position (field);
6179 if (constructor_incremental
6180 && (!constructor_unfilled_fields
6181 || tree_int_cst_lt (bitpos,
6182 bit_position (constructor_unfilled_fields))))
6183 set_nonincremental_init ();
6185 p = constructor_pending_elts;
6186 while (p)
6188 if (field == p->purpose)
6189 return p->value;
6190 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6191 p = p->left;
6192 else
6193 p = p->right;
6196 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6198 if (constructor_elements
6199 && TREE_PURPOSE (constructor_elements) == field)
6200 return TREE_VALUE (constructor_elements);
6202 return 0;
6205 /* "Output" the next constructor element.
6206 At top level, really output it to assembler code now.
6207 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6208 TYPE is the data type that the containing data type wants here.
6209 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6211 PENDING if non-nil means output pending elements that belong
6212 right after this element. (PENDING is normally 1;
6213 it is 0 while outputting pending elements, to avoid recursion.) */
6215 static void
6216 output_init_element (value, type, field, pending)
6217 tree value, type, field;
6218 int pending;
6220 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6221 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6222 && !(TREE_CODE (value) == STRING_CST
6223 && TREE_CODE (type) == ARRAY_TYPE
6224 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6225 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6226 TYPE_MAIN_VARIANT (type))))
6227 value = default_conversion (value);
6229 if (value == error_mark_node)
6230 constructor_erroneous = 1;
6231 else if (!TREE_CONSTANT (value))
6232 constructor_constant = 0;
6233 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6234 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6235 || TREE_CODE (constructor_type) == UNION_TYPE)
6236 && DECL_C_BIT_FIELD (field)
6237 && TREE_CODE (value) != INTEGER_CST))
6238 constructor_simple = 0;
6240 if (require_constant_value && ! TREE_CONSTANT (value))
6242 error_init ("initializer element is not constant");
6243 value = error_mark_node;
6245 else if (require_constant_elements
6246 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6247 pedwarn ("initializer element is not computable at load time");
6249 /* If this field is empty (and not at the end of structure),
6250 don't do anything other than checking the initializer. */
6251 if (field
6252 && (TREE_TYPE (field) == error_mark_node
6253 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6254 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6255 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6256 || TREE_CHAIN (field)))))
6257 return;
6259 if (value == error_mark_node)
6261 constructor_erroneous = 1;
6262 return;
6265 /* If this element doesn't come next in sequence,
6266 put it on constructor_pending_elts. */
6267 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6268 && (!constructor_incremental
6269 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6271 if (constructor_incremental
6272 && tree_int_cst_lt (field, constructor_unfilled_index))
6273 set_nonincremental_init ();
6275 add_pending_init (field,
6276 digest_init (type, value, require_constant_value,
6277 require_constant_elements));
6278 return;
6280 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6281 && (!constructor_incremental
6282 || field != constructor_unfilled_fields))
6284 /* We do this for records but not for unions. In a union,
6285 no matter which field is specified, it can be initialized
6286 right away since it starts at the beginning of the union. */
6287 if (constructor_incremental)
6289 if (!constructor_unfilled_fields)
6290 set_nonincremental_init ();
6291 else
6293 tree bitpos, unfillpos;
6295 bitpos = bit_position (field);
6296 unfillpos = bit_position (constructor_unfilled_fields);
6298 if (tree_int_cst_lt (bitpos, unfillpos))
6299 set_nonincremental_init ();
6303 add_pending_init (field,
6304 digest_init (type, value, require_constant_value,
6305 require_constant_elements));
6306 return;
6308 else if (TREE_CODE (constructor_type) == UNION_TYPE
6309 && constructor_elements)
6311 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
6312 warning_init ("initialized field with side-effects overwritten");
6314 /* We can have just one union field set. */
6315 constructor_elements = 0;
6318 /* Otherwise, output this element either to
6319 constructor_elements or to the assembler file. */
6321 if (field && TREE_CODE (field) == INTEGER_CST)
6322 field = copy_node (field);
6323 constructor_elements
6324 = tree_cons (field, digest_init (type, value,
6325 require_constant_value,
6326 require_constant_elements),
6327 constructor_elements);
6329 /* Advance the variable that indicates sequential elements output. */
6330 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6331 constructor_unfilled_index
6332 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6333 bitsize_one_node);
6334 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6336 constructor_unfilled_fields
6337 = TREE_CHAIN (constructor_unfilled_fields);
6339 /* Skip any nameless bit fields. */
6340 while (constructor_unfilled_fields != 0
6341 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6342 && DECL_NAME (constructor_unfilled_fields) == 0)
6343 constructor_unfilled_fields =
6344 TREE_CHAIN (constructor_unfilled_fields);
6346 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6347 constructor_unfilled_fields = 0;
6349 /* Now output any pending elements which have become next. */
6350 if (pending)
6351 output_pending_init_elements (0);
6354 /* Output any pending elements which have become next.
6355 As we output elements, constructor_unfilled_{fields,index}
6356 advances, which may cause other elements to become next;
6357 if so, they too are output.
6359 If ALL is 0, we return when there are
6360 no more pending elements to output now.
6362 If ALL is 1, we output space as necessary so that
6363 we can output all the pending elements. */
6365 static void
6366 output_pending_init_elements (all)
6367 int all;
6369 struct init_node *elt = constructor_pending_elts;
6370 tree next;
6372 retry:
6374 /* Look thru the whole pending tree.
6375 If we find an element that should be output now,
6376 output it. Otherwise, set NEXT to the element
6377 that comes first among those still pending. */
6379 next = 0;
6380 while (elt)
6382 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6384 if (tree_int_cst_equal (elt->purpose,
6385 constructor_unfilled_index))
6386 output_init_element (elt->value,
6387 TREE_TYPE (constructor_type),
6388 constructor_unfilled_index, 0);
6389 else if (tree_int_cst_lt (constructor_unfilled_index,
6390 elt->purpose))
6392 /* Advance to the next smaller node. */
6393 if (elt->left)
6394 elt = elt->left;
6395 else
6397 /* We have reached the smallest node bigger than the
6398 current unfilled index. Fill the space first. */
6399 next = elt->purpose;
6400 break;
6403 else
6405 /* Advance to the next bigger node. */
6406 if (elt->right)
6407 elt = elt->right;
6408 else
6410 /* We have reached the biggest node in a subtree. Find
6411 the parent of it, which is the next bigger node. */
6412 while (elt->parent && elt->parent->right == elt)
6413 elt = elt->parent;
6414 elt = elt->parent;
6415 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6416 elt->purpose))
6418 next = elt->purpose;
6419 break;
6424 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6425 || TREE_CODE (constructor_type) == UNION_TYPE)
6427 tree ctor_unfilled_bitpos, elt_bitpos;
6429 /* If the current record is complete we are done. */
6430 if (constructor_unfilled_fields == 0)
6431 break;
6433 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6434 elt_bitpos = bit_position (elt->purpose);
6435 /* We can't compare fields here because there might be empty
6436 fields in between. */
6437 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6439 constructor_unfilled_fields = elt->purpose;
6440 output_init_element (elt->value, TREE_TYPE (elt->purpose),
6441 elt->purpose, 0);
6443 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6445 /* Advance to the next smaller node. */
6446 if (elt->left)
6447 elt = elt->left;
6448 else
6450 /* We have reached the smallest node bigger than the
6451 current unfilled field. Fill the space first. */
6452 next = elt->purpose;
6453 break;
6456 else
6458 /* Advance to the next bigger node. */
6459 if (elt->right)
6460 elt = elt->right;
6461 else
6463 /* We have reached the biggest node in a subtree. Find
6464 the parent of it, which is the next bigger node. */
6465 while (elt->parent && elt->parent->right == elt)
6466 elt = elt->parent;
6467 elt = elt->parent;
6468 if (elt
6469 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6470 bit_position (elt->purpose))))
6472 next = elt->purpose;
6473 break;
6480 /* Ordinarily return, but not if we want to output all
6481 and there are elements left. */
6482 if (! (all && next != 0))
6483 return;
6485 /* If it's not incremental, just skip over the gap, so that after
6486 jumping to retry we will output the next successive element. */
6487 if (TREE_CODE (constructor_type) == RECORD_TYPE
6488 || TREE_CODE (constructor_type) == UNION_TYPE)
6489 constructor_unfilled_fields = next;
6490 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6491 constructor_unfilled_index = next;
6493 /* ELT now points to the node in the pending tree with the next
6494 initializer to output. */
6495 goto retry;
6498 /* Add one non-braced element to the current constructor level.
6499 This adjusts the current position within the constructor's type.
6500 This may also start or terminate implicit levels
6501 to handle a partly-braced initializer.
6503 Once this has found the correct level for the new element,
6504 it calls output_init_element. */
6506 void
6507 process_init_element (value)
6508 tree value;
6510 tree orig_value = value;
6511 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6513 designator_depth = 0;
6514 designator_errorneous = 0;
6516 /* Handle superfluous braces around string cst as in
6517 char x[] = {"foo"}; */
6518 if (string_flag
6519 && constructor_type
6520 && TREE_CODE (constructor_type) == ARRAY_TYPE
6521 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6522 && integer_zerop (constructor_unfilled_index))
6524 if (constructor_stack->replacement_value)
6525 error_init ("excess elements in char array initializer");
6526 constructor_stack->replacement_value = value;
6527 return;
6530 if (constructor_stack->replacement_value != 0)
6532 error_init ("excess elements in struct initializer");
6533 return;
6536 /* Ignore elements of a brace group if it is entirely superfluous
6537 and has already been diagnosed. */
6538 if (constructor_type == 0)
6539 return;
6541 /* If we've exhausted any levels that didn't have braces,
6542 pop them now. */
6543 while (constructor_stack->implicit)
6545 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6546 || TREE_CODE (constructor_type) == UNION_TYPE)
6547 && constructor_fields == 0)
6548 process_init_element (pop_init_level (1));
6549 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6550 && (constructor_max_index == 0
6551 || tree_int_cst_lt (constructor_max_index,
6552 constructor_index)))
6553 process_init_element (pop_init_level (1));
6554 else
6555 break;
6558 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6559 if (constructor_range_stack)
6560 value = save_expr (value);
6562 while (1)
6564 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6566 tree fieldtype;
6567 enum tree_code fieldcode;
6569 if (constructor_fields == 0)
6571 pedwarn_init ("excess elements in struct initializer");
6572 break;
6575 fieldtype = TREE_TYPE (constructor_fields);
6576 if (fieldtype != error_mark_node)
6577 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6578 fieldcode = TREE_CODE (fieldtype);
6580 /* Accept a string constant to initialize a subarray. */
6581 if (value != 0
6582 && fieldcode == ARRAY_TYPE
6583 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6584 && string_flag)
6585 value = orig_value;
6586 /* Otherwise, if we have come to a subaggregate,
6587 and we don't have an element of its type, push into it. */
6588 else if (value != 0 && !constructor_no_implicit
6589 && value != error_mark_node
6590 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6591 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6592 || fieldcode == UNION_TYPE))
6594 push_init_level (1);
6595 continue;
6598 if (value)
6600 push_member_name (constructor_fields);
6601 output_init_element (value, fieldtype, constructor_fields, 1);
6602 RESTORE_SPELLING_DEPTH (constructor_depth);
6604 else
6605 /* Do the bookkeeping for an element that was
6606 directly output as a constructor. */
6608 /* For a record, keep track of end position of last field. */
6609 if (DECL_SIZE (constructor_fields))
6610 constructor_bit_index
6611 = size_binop (PLUS_EXPR,
6612 bit_position (constructor_fields),
6613 DECL_SIZE (constructor_fields));
6615 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6616 /* Skip any nameless bit fields. */
6617 while (constructor_unfilled_fields != 0
6618 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6619 && DECL_NAME (constructor_unfilled_fields) == 0)
6620 constructor_unfilled_fields =
6621 TREE_CHAIN (constructor_unfilled_fields);
6624 constructor_fields = TREE_CHAIN (constructor_fields);
6625 /* Skip any nameless bit fields at the beginning. */
6626 while (constructor_fields != 0
6627 && DECL_C_BIT_FIELD (constructor_fields)
6628 && DECL_NAME (constructor_fields) == 0)
6629 constructor_fields = TREE_CHAIN (constructor_fields);
6631 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6633 tree fieldtype;
6634 enum tree_code fieldcode;
6636 if (constructor_fields == 0)
6638 pedwarn_init ("excess elements in union initializer");
6639 break;
6642 fieldtype = TREE_TYPE (constructor_fields);
6643 if (fieldtype != error_mark_node)
6644 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6645 fieldcode = TREE_CODE (fieldtype);
6647 /* Warn that traditional C rejects initialization of unions.
6648 We skip the warning if the value is zero. This is done
6649 under the assumption that the zero initializer in user
6650 code appears conditioned on e.g. __STDC__ to avoid
6651 "missing initializer" warnings and relies on default
6652 initialization to zero in the traditional C case.
6653 We also skip the warning if the initializer is designated,
6654 again on the assumption that this must be conditional on
6655 __STDC__ anyway (and we've already complained about the
6656 member-designator already). */
6657 if (warn_traditional && !in_system_header && !constructor_designated
6658 && !(value && (integer_zerop (value) || real_zerop (value))))
6659 warning ("traditional C rejects initialization of unions");
6661 /* Accept a string constant to initialize a subarray. */
6662 if (value != 0
6663 && fieldcode == ARRAY_TYPE
6664 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6665 && string_flag)
6666 value = orig_value;
6667 /* Otherwise, if we have come to a subaggregate,
6668 and we don't have an element of its type, push into it. */
6669 else if (value != 0 && !constructor_no_implicit
6670 && value != error_mark_node
6671 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6672 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6673 || fieldcode == UNION_TYPE))
6675 push_init_level (1);
6676 continue;
6679 if (value)
6681 push_member_name (constructor_fields);
6682 output_init_element (value, fieldtype, constructor_fields, 1);
6683 RESTORE_SPELLING_DEPTH (constructor_depth);
6685 else
6686 /* Do the bookkeeping for an element that was
6687 directly output as a constructor. */
6689 constructor_bit_index = DECL_SIZE (constructor_fields);
6690 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6693 constructor_fields = 0;
6695 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6697 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6698 enum tree_code eltcode = TREE_CODE (elttype);
6700 /* Accept a string constant to initialize a subarray. */
6701 if (value != 0
6702 && eltcode == ARRAY_TYPE
6703 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6704 && string_flag)
6705 value = orig_value;
6706 /* Otherwise, if we have come to a subaggregate,
6707 and we don't have an element of its type, push into it. */
6708 else if (value != 0 && !constructor_no_implicit
6709 && value != error_mark_node
6710 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6711 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6712 || eltcode == UNION_TYPE))
6714 push_init_level (1);
6715 continue;
6718 if (constructor_max_index != 0
6719 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6720 || integer_all_onesp (constructor_max_index)))
6722 pedwarn_init ("excess elements in array initializer");
6723 break;
6726 /* Now output the actual element. */
6727 if (value)
6729 push_array_bounds (tree_low_cst (constructor_index, 0));
6730 output_init_element (value, elttype, constructor_index, 1);
6731 RESTORE_SPELLING_DEPTH (constructor_depth);
6734 constructor_index
6735 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6737 if (! value)
6738 /* If we are doing the bookkeeping for an element that was
6739 directly output as a constructor, we must update
6740 constructor_unfilled_index. */
6741 constructor_unfilled_index = constructor_index;
6744 /* Handle the sole element allowed in a braced initializer
6745 for a scalar variable. */
6746 else if (constructor_fields == 0)
6748 pedwarn_init ("excess elements in scalar initializer");
6749 break;
6751 else
6753 if (value)
6754 output_init_element (value, constructor_type, NULL_TREE, 1);
6755 constructor_fields = 0;
6758 /* Handle range initializers either at this level or anywhere higher
6759 in the designator stack. */
6760 if (constructor_range_stack)
6762 struct constructor_range_stack *p, *range_stack;
6763 int finish = 0;
6765 range_stack = constructor_range_stack;
6766 constructor_range_stack = 0;
6767 while (constructor_stack != range_stack->stack)
6769 if (!constructor_stack->implicit)
6770 abort ();
6771 process_init_element (pop_init_level (1));
6773 for (p = range_stack;
6774 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6775 p = p->prev)
6777 if (!constructor_stack->implicit)
6778 abort ();
6779 process_init_element (pop_init_level (1));
6782 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6783 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6784 finish = 1;
6786 while (1)
6788 constructor_index = p->index;
6789 constructor_fields = p->fields;
6790 if (finish && p->range_end && p->index == p->range_start)
6792 finish = 0;
6793 p->prev = 0;
6795 p = p->next;
6796 if (!p)
6797 break;
6798 push_init_level (2);
6799 p->stack = constructor_stack;
6800 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6801 p->index = p->range_start;
6804 if (!finish)
6805 constructor_range_stack = range_stack;
6806 continue;
6809 break;
6812 constructor_range_stack = 0;
6815 /* Build a simple asm-statement, from one string literal. */
6816 tree
6817 simple_asm_stmt (expr)
6818 tree expr;
6820 STRIP_NOPS (expr);
6822 if (TREE_CODE (expr) == ADDR_EXPR)
6823 expr = TREE_OPERAND (expr, 0);
6825 if (TREE_CODE (expr) == STRING_CST)
6827 tree stmt;
6829 if (TREE_CHAIN (expr))
6830 expr = combine_strings (expr);
6831 stmt = add_stmt (build_stmt (ASM_STMT, NULL_TREE, expr,
6832 NULL_TREE, NULL_TREE,
6833 NULL_TREE));
6834 ASM_INPUT_P (stmt) = 1;
6835 return stmt;
6838 error ("argument of `asm' is not a constant string");
6839 return NULL_TREE;
6842 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6843 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6845 tree
6846 build_asm_stmt (cv_qualifier, string, outputs, inputs, clobbers)
6847 tree cv_qualifier;
6848 tree string;
6849 tree outputs;
6850 tree inputs;
6851 tree clobbers;
6853 tree tail;
6855 if (TREE_CHAIN (string))
6856 string = combine_strings (string);
6857 if (TREE_CODE (string) != STRING_CST)
6859 error ("asm template is not a string constant");
6860 return NULL_TREE;
6863 if (cv_qualifier != NULL_TREE
6864 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6866 warning ("%s qualifier ignored on asm",
6867 IDENTIFIER_POINTER (cv_qualifier));
6868 cv_qualifier = NULL_TREE;
6871 /* We can remove output conversions that change the type,
6872 but not the mode. */
6873 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6875 tree output = TREE_VALUE (tail);
6877 STRIP_NOPS (output);
6878 TREE_VALUE (tail) = output;
6880 /* Allow conversions as LHS here. build_modify_expr as called below
6881 will do the right thing with them. */
6882 while (TREE_CODE (output) == NOP_EXPR
6883 || TREE_CODE (output) == CONVERT_EXPR
6884 || TREE_CODE (output) == FLOAT_EXPR
6885 || TREE_CODE (output) == FIX_TRUNC_EXPR
6886 || TREE_CODE (output) == FIX_FLOOR_EXPR
6887 || TREE_CODE (output) == FIX_ROUND_EXPR
6888 || TREE_CODE (output) == FIX_CEIL_EXPR)
6889 output = TREE_OPERAND (output, 0);
6891 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6894 /* Remove output conversions that change the type but not the mode. */
6895 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6897 tree output = TREE_VALUE (tail);
6898 STRIP_NOPS (output);
6899 TREE_VALUE (tail) = output;
6902 /* Perform default conversions on array and function inputs.
6903 Don't do this for other types as it would screw up operands
6904 expected to be in memory. */
6905 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6906 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6908 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6909 outputs, inputs, clobbers));
6912 /* Expand an ASM statement with operands, handling output operands
6913 that are not variables or INDIRECT_REFS by transforming such
6914 cases into cases that expand_asm_operands can handle.
6916 Arguments are same as for expand_asm_operands. */
6918 void
6919 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6920 tree string, outputs, inputs, clobbers;
6921 int vol;
6922 const char *filename;
6923 int line;
6925 int noutputs = list_length (outputs);
6926 int i;
6927 /* o[I] is the place that output number I should be written. */
6928 tree *o = (tree *) alloca (noutputs * sizeof (tree));
6929 tree tail;
6931 /* Record the contents of OUTPUTS before it is modified. */
6932 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6933 o[i] = TREE_VALUE (tail);
6935 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6936 OUTPUTS some trees for where the values were actually stored. */
6937 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6939 /* Copy all the intermediate outputs into the specified outputs. */
6940 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6942 if (o[i] != TREE_VALUE (tail))
6944 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6945 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6946 free_temp_slots ();
6948 /* Restore the original value so that it's correct the next
6949 time we expand this function. */
6950 TREE_VALUE (tail) = o[i];
6952 /* Detect modification of read-only values.
6953 (Otherwise done by build_modify_expr.) */
6954 else
6956 tree type = TREE_TYPE (o[i]);
6957 if (TREE_READONLY (o[i])
6958 || TYPE_READONLY (type)
6959 || ((TREE_CODE (type) == RECORD_TYPE
6960 || TREE_CODE (type) == UNION_TYPE)
6961 && C_TYPE_FIELDS_READONLY (type)))
6962 readonly_warning (o[i], "modification by `asm'");
6966 /* Those MODIFY_EXPRs could do autoincrements. */
6967 emit_queue ();
6970 /* Expand a C `return' statement.
6971 RETVAL is the expression for what to return,
6972 or a null pointer for `return;' with no value. */
6974 tree
6975 c_expand_return (retval)
6976 tree retval;
6978 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6980 if (TREE_THIS_VOLATILE (current_function_decl))
6981 warning ("function declared `noreturn' has a `return' statement");
6983 if (!retval)
6985 current_function_returns_null = 1;
6986 if ((warn_return_type || flag_isoc99)
6987 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6988 pedwarn_c99 ("`return' with no value, in function returning non-void");
6990 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6992 current_function_returns_null = 1;
6993 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6994 pedwarn ("`return' with a value, in function returning void");
6996 else
6998 tree t = convert_for_assignment (valtype, retval, _("return"),
6999 NULL_TREE, NULL_TREE, 0);
7000 tree res = DECL_RESULT (current_function_decl);
7001 tree inner;
7003 if (t == error_mark_node)
7004 return NULL_TREE;
7006 inner = t = convert (TREE_TYPE (res), t);
7008 /* Strip any conversions, additions, and subtractions, and see if
7009 we are returning the address of a local variable. Warn if so. */
7010 while (1)
7012 switch (TREE_CODE (inner))
7014 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
7015 case PLUS_EXPR:
7016 inner = TREE_OPERAND (inner, 0);
7017 continue;
7019 case MINUS_EXPR:
7020 /* If the second operand of the MINUS_EXPR has a pointer
7021 type (or is converted from it), this may be valid, so
7022 don't give a warning. */
7024 tree op1 = TREE_OPERAND (inner, 1);
7026 while (! POINTER_TYPE_P (TREE_TYPE (op1))
7027 && (TREE_CODE (op1) == NOP_EXPR
7028 || TREE_CODE (op1) == NON_LVALUE_EXPR
7029 || TREE_CODE (op1) == CONVERT_EXPR))
7030 op1 = TREE_OPERAND (op1, 0);
7032 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7033 break;
7035 inner = TREE_OPERAND (inner, 0);
7036 continue;
7039 case ADDR_EXPR:
7040 inner = TREE_OPERAND (inner, 0);
7042 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
7043 inner = TREE_OPERAND (inner, 0);
7045 if (TREE_CODE (inner) == VAR_DECL
7046 && ! DECL_EXTERNAL (inner)
7047 && ! TREE_STATIC (inner)
7048 && DECL_CONTEXT (inner) == current_function_decl)
7049 warning ("function returns address of local variable");
7050 break;
7052 default:
7053 break;
7056 break;
7059 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
7060 current_function_returns_value = 1;
7063 return add_stmt (build_return_stmt (retval));
7066 struct c_switch {
7067 /* The SWITCH_STMT being built. */
7068 tree switch_stmt;
7069 /* A splay-tree mapping the low element of a case range to the high
7070 element, or NULL_TREE if there is no high element. Used to
7071 determine whether or not a new case label duplicates an old case
7072 label. We need a tree, rather than simply a hash table, because
7073 of the GNU case range extension. */
7074 splay_tree cases;
7075 /* The next node on the stack. */
7076 struct c_switch *next;
7079 /* A stack of the currently active switch statements. The innermost
7080 switch statement is on the top of the stack. There is no need to
7081 mark the stack for garbage collection because it is only active
7082 during the processing of the body of a function, and we never
7083 collect at that point. */
7085 static struct c_switch *switch_stack;
7087 /* Start a C switch statement, testing expression EXP. Return the new
7088 SWITCH_STMT. */
7090 tree
7091 c_start_case (exp)
7092 tree exp;
7094 enum tree_code code;
7095 tree type;
7096 struct c_switch *cs;
7098 if (exp != error_mark_node)
7100 code = TREE_CODE (TREE_TYPE (exp));
7101 type = TREE_TYPE (exp);
7103 if (! INTEGRAL_TYPE_P (type)
7104 && code != ERROR_MARK)
7106 error ("switch quantity not an integer");
7107 exp = integer_zero_node;
7109 else
7111 tree index;
7112 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7114 if (warn_traditional && !in_system_header
7115 && (type == long_integer_type_node
7116 || type == long_unsigned_type_node))
7117 warning ("`long' switch expression not converted to `int' in ISO C");
7119 exp = default_conversion (exp);
7120 type = TREE_TYPE (exp);
7121 index = get_unwidened (exp, NULL_TREE);
7122 /* We can't strip a conversion from a signed type to an
7123 unsigned, because if we did, int_fits_type_p would do the
7124 wrong thing when checking case values for being in range,
7125 and it's too hard to do the right thing. */
7126 if (TREE_UNSIGNED (TREE_TYPE (exp))
7127 == TREE_UNSIGNED (TREE_TYPE (index)))
7128 exp = index;
7132 /* Add this new SWITCH_STMT to the stack. */
7133 cs = (struct c_switch *) xmalloc (sizeof (*cs));
7134 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, NULL_TREE);
7135 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7136 cs->next = switch_stack;
7137 switch_stack = cs;
7139 return add_stmt (switch_stack->switch_stmt);
7142 /* Process a case label. */
7144 tree
7145 do_case (low_value, high_value)
7146 tree low_value;
7147 tree high_value;
7149 tree label = NULL_TREE;
7151 if (switch_stack)
7153 label = c_add_case_label (switch_stack->cases,
7154 SWITCH_COND (switch_stack->switch_stmt),
7155 low_value, high_value);
7156 if (label == error_mark_node)
7157 label = NULL_TREE;
7159 else if (low_value)
7160 error ("case label not within a switch statement");
7161 else
7162 error ("`default' label not within a switch statement");
7164 return label;
7167 /* Finish the switch statement. */
7169 void
7170 c_finish_case ()
7172 struct c_switch *cs = switch_stack;
7174 RECHAIN_STMTS (cs->switch_stmt, SWITCH_BODY (cs->switch_stmt));
7176 /* Pop the stack. */
7177 switch_stack = switch_stack->next;
7178 splay_tree_delete (cs->cases);
7179 free (cs);