2001-04-09 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / c-typeck.c
blob508045b497982ce2a392f5d80c7dbd3306a37dc9
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
32 #include "config.h"
33 #include "system.h"
34 #include "tree.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "rtl.h"
40 #include "expr.h"
41 #include "toplev.h"
42 #include "intl.h"
43 #include "ggc.h"
45 /* Nonzero if we've already printed a "missing braces around initializer"
46 message within this initializer. */
47 static int missing_braces_mentioned;
49 /* 1 if we explained undeclared var errors. */
50 static int undeclared_variable_notice;
52 static tree qualify_type PARAMS ((tree, tree));
53 static int comp_target_types PARAMS ((tree, tree));
54 static int function_types_compatible_p PARAMS ((tree, tree));
55 static int type_lists_compatible_p PARAMS ((tree, tree));
56 static tree decl_constant_value_for_broken_optimization PARAMS ((tree));
57 static tree lookup_field PARAMS ((tree, tree, tree *));
58 static tree convert_arguments PARAMS ((tree, tree, tree, tree));
59 static tree pointer_int_sum PARAMS ((enum tree_code, tree, tree));
60 static tree pointer_diff PARAMS ((tree, tree));
61 static tree unary_complex_lvalue PARAMS ((enum tree_code, tree));
62 static void pedantic_lvalue_warning PARAMS ((enum tree_code));
63 static tree internal_build_compound_expr PARAMS ((tree, int));
64 static tree convert_for_assignment PARAMS ((tree, tree, const char *,
65 tree, tree, int));
66 static void warn_for_assignment PARAMS ((const char *, const char *,
67 tree, int));
68 static tree valid_compound_expr_initializer PARAMS ((tree, tree));
69 static void push_string PARAMS ((const char *));
70 static void push_member_name PARAMS ((tree));
71 static void push_array_bounds PARAMS ((int));
72 static int spelling_length PARAMS ((void));
73 static char *print_spelling PARAMS ((char *));
74 static void warning_init PARAMS ((const char *));
75 static tree digest_init PARAMS ((tree, tree, int, int));
76 static void output_init_element PARAMS ((tree, tree, tree, int));
77 static void output_pending_init_elements PARAMS ((int));
78 static int set_designator PARAMS ((int));
79 static void push_range_stack PARAMS ((tree));
80 static void add_pending_init PARAMS ((tree, tree));
81 static void set_nonincremental_init PARAMS ((void));
82 static void set_nonincremental_init_from_string PARAMS ((tree));
83 static tree find_init_member PARAMS ((tree));
85 /* Do `exp = require_complete_type (exp);' to make sure exp
86 does not have an incomplete type. (That includes void types.) */
88 tree
89 require_complete_type (value)
90 tree value;
92 tree type = TREE_TYPE (value);
94 if (TREE_CODE (value) == ERROR_MARK)
95 return error_mark_node;
97 /* First, detect a valid value with a complete type. */
98 if (COMPLETE_TYPE_P (type))
99 return value;
101 incomplete_type_error (value, type);
102 return error_mark_node;
105 /* Print an error message for invalid use of an incomplete type.
106 VALUE is the expression that was used (or 0 if that isn't known)
107 and TYPE is the type that was invalid. */
109 void
110 incomplete_type_error (value, type)
111 tree value;
112 tree type;
114 const char *type_code_string;
116 /* Avoid duplicate error message. */
117 if (TREE_CODE (type) == ERROR_MARK)
118 return;
120 if (value != 0 && (TREE_CODE (value) == VAR_DECL
121 || TREE_CODE (value) == PARM_DECL))
122 error ("`%s' has an incomplete type",
123 IDENTIFIER_POINTER (DECL_NAME (value)));
124 else
126 retry:
127 /* We must print an error message. Be clever about what it says. */
129 switch (TREE_CODE (type))
131 case RECORD_TYPE:
132 type_code_string = "struct";
133 break;
135 case UNION_TYPE:
136 type_code_string = "union";
137 break;
139 case ENUMERAL_TYPE:
140 type_code_string = "enum";
141 break;
143 case VOID_TYPE:
144 error ("invalid use of void expression");
145 return;
147 case ARRAY_TYPE:
148 if (TYPE_DOMAIN (type))
150 type = TREE_TYPE (type);
151 goto retry;
153 error ("invalid use of array with unspecified bounds");
154 return;
156 default:
157 abort ();
160 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
161 error ("invalid use of undefined type `%s %s'",
162 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
163 else
164 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
165 error ("invalid use of incomplete typedef `%s'",
166 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
170 /* Return a variant of TYPE which has all the type qualifiers of LIKE
171 as well as those of TYPE. */
173 static tree
174 qualify_type (type, like)
175 tree type, like;
177 return c_build_qualified_type (type,
178 TYPE_QUALS (type) | TYPE_QUALS (like));
181 /* Return the common type of two types.
182 We assume that comptypes has already been done and returned 1;
183 if that isn't so, this may crash. In particular, we assume that qualifiers
184 match.
186 This is the type for the result of most arithmetic operations
187 if the operands have the given two types. */
189 tree
190 common_type (t1, t2)
191 tree t1, t2;
193 register enum tree_code code1;
194 register enum tree_code code2;
195 tree attributes;
197 /* Save time if the two types are the same. */
199 if (t1 == t2) return t1;
201 /* If one type is nonsense, use the other. */
202 if (t1 == error_mark_node)
203 return t2;
204 if (t2 == error_mark_node)
205 return t1;
207 /* Merge the attributes. */
208 attributes = merge_machine_type_attributes (t1, t2);
210 /* Treat an enum type as the unsigned integer type of the same width. */
212 if (TREE_CODE (t1) == ENUMERAL_TYPE)
213 t1 = type_for_size (TYPE_PRECISION (t1), 1);
214 if (TREE_CODE (t2) == ENUMERAL_TYPE)
215 t2 = type_for_size (TYPE_PRECISION (t2), 1);
217 code1 = TREE_CODE (t1);
218 code2 = TREE_CODE (t2);
220 /* If one type is complex, form the common type of the non-complex
221 components, then make that complex. Use T1 or T2 if it is the
222 required type. */
223 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
225 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
226 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
227 tree subtype = common_type (subtype1, subtype2);
229 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
230 return build_type_attribute_variant (t1, attributes);
231 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
232 return build_type_attribute_variant (t2, attributes);
233 else
234 return build_type_attribute_variant (build_complex_type (subtype),
235 attributes);
238 switch (code1)
240 case INTEGER_TYPE:
241 case REAL_TYPE:
242 /* If only one is real, use it as the result. */
244 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
245 return build_type_attribute_variant (t1, attributes);
247 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
248 return build_type_attribute_variant (t2, attributes);
250 /* Both real or both integers; use the one with greater precision. */
252 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
253 return build_type_attribute_variant (t1, attributes);
254 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
255 return build_type_attribute_variant (t2, attributes);
257 /* Same precision. Prefer longs to ints even when same size. */
259 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
260 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
261 return build_type_attribute_variant (long_unsigned_type_node,
262 attributes);
264 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
265 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
267 /* But preserve unsignedness from the other type,
268 since long cannot hold all the values of an unsigned int. */
269 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
270 t1 = long_unsigned_type_node;
271 else
272 t1 = long_integer_type_node;
273 return build_type_attribute_variant (t1, attributes);
276 /* Likewise, prefer long double to double even if same size. */
277 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
278 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
279 return build_type_attribute_variant (long_double_type_node,
280 attributes);
282 /* Otherwise prefer the unsigned one. */
284 if (TREE_UNSIGNED (t1))
285 return build_type_attribute_variant (t1, attributes);
286 else
287 return build_type_attribute_variant (t2, attributes);
289 case POINTER_TYPE:
290 /* For two pointers, do this recursively on the target type,
291 and combine the qualifiers of the two types' targets. */
292 /* This code was turned off; I don't know why.
293 But ANSI C specifies doing this with the qualifiers.
294 So I turned it on again. */
296 tree pointed_to_1 = TREE_TYPE (t1);
297 tree pointed_to_2 = TREE_TYPE (t2);
298 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
299 TYPE_MAIN_VARIANT (pointed_to_2));
300 t1 = build_pointer_type (c_build_qualified_type
301 (target,
302 TYPE_QUALS (pointed_to_1) |
303 TYPE_QUALS (pointed_to_2)));
304 return build_type_attribute_variant (t1, attributes);
306 #if 0
307 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
308 return build_type_attribute_variant (t1, attributes);
309 #endif
311 case ARRAY_TYPE:
313 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
314 /* Save space: see if the result is identical to one of the args. */
315 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
316 return build_type_attribute_variant (t1, attributes);
317 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
318 return build_type_attribute_variant (t2, attributes);
319 /* Merge the element types, and have a size if either arg has one. */
320 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
321 return build_type_attribute_variant (t1, attributes);
324 case FUNCTION_TYPE:
325 /* Function types: prefer the one that specified arg types.
326 If both do, merge the arg types. Also merge the return types. */
328 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
329 tree p1 = TYPE_ARG_TYPES (t1);
330 tree p2 = TYPE_ARG_TYPES (t2);
331 int len;
332 tree newargs, n;
333 int i;
335 /* Save space: see if the result is identical to one of the args. */
336 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
337 return build_type_attribute_variant (t1, attributes);
338 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
339 return build_type_attribute_variant (t2, attributes);
341 /* Simple way if one arg fails to specify argument types. */
342 if (TYPE_ARG_TYPES (t1) == 0)
344 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
345 return build_type_attribute_variant (t1, attributes);
347 if (TYPE_ARG_TYPES (t2) == 0)
349 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
350 return build_type_attribute_variant (t1, attributes);
353 /* If both args specify argument types, we must merge the two
354 lists, argument by argument. */
356 pushlevel (0);
357 declare_parm_level (1);
359 len = list_length (p1);
360 newargs = 0;
362 for (i = 0; i < len; i++)
363 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
365 n = newargs;
367 for (; p1;
368 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
370 /* A null type means arg type is not specified.
371 Take whatever the other function type has. */
372 if (TREE_VALUE (p1) == 0)
374 TREE_VALUE (n) = TREE_VALUE (p2);
375 goto parm_done;
377 if (TREE_VALUE (p2) == 0)
379 TREE_VALUE (n) = TREE_VALUE (p1);
380 goto parm_done;
383 /* Given wait (union {union wait *u; int *i} *)
384 and wait (union wait *),
385 prefer union wait * as type of parm. */
386 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
387 && TREE_VALUE (p1) != TREE_VALUE (p2))
389 tree memb;
390 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
391 memb; memb = TREE_CHAIN (memb))
392 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
394 TREE_VALUE (n) = TREE_VALUE (p2);
395 if (pedantic)
396 pedwarn ("function types not truly compatible in ISO C");
397 goto parm_done;
400 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
401 && TREE_VALUE (p2) != TREE_VALUE (p1))
403 tree memb;
404 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
405 memb; memb = TREE_CHAIN (memb))
406 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
408 TREE_VALUE (n) = TREE_VALUE (p1);
409 if (pedantic)
410 pedwarn ("function types not truly compatible in ISO C");
411 goto parm_done;
414 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
415 parm_done: ;
418 poplevel (0, 0, 0);
420 t1 = build_function_type (valtype, newargs);
421 /* ... falls through ... */
424 default:
425 return build_type_attribute_variant (t1, attributes);
430 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
431 or various other operations. Return 2 if they are compatible
432 but a warning may be needed if you use them together. */
435 comptypes (type1, type2)
436 tree type1, type2;
438 register tree t1 = type1;
439 register tree t2 = type2;
440 int attrval, val;
442 /* Suppress errors caused by previously reported errors. */
444 if (t1 == t2 || !t1 || !t2
445 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
446 return 1;
448 /* If either type is the internal version of sizetype, return the
449 language version. */
450 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
451 && TYPE_DOMAIN (t1) != 0)
452 t1 = TYPE_DOMAIN (t1);
454 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
455 && TYPE_DOMAIN (t2) != 0)
456 t2 = TYPE_DOMAIN (t2);
458 /* Treat an enum type as the integer type of the same width and
459 signedness. */
461 if (TREE_CODE (t1) == ENUMERAL_TYPE)
462 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
463 if (TREE_CODE (t2) == ENUMERAL_TYPE)
464 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
466 if (t1 == t2)
467 return 1;
469 /* Different classes of types can't be compatible. */
471 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
473 /* Qualifiers must match. */
475 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
476 return 0;
478 /* Allow for two different type nodes which have essentially the same
479 definition. Note that we already checked for equality of the type
480 qualifiers (just above). */
482 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
483 return 1;
485 #ifndef COMP_TYPE_ATTRIBUTES
486 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
487 #endif
489 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
490 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
491 return 0;
493 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
494 val = 0;
496 switch (TREE_CODE (t1))
498 case POINTER_TYPE:
499 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
500 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
501 break;
503 case FUNCTION_TYPE:
504 val = function_types_compatible_p (t1, t2);
505 break;
507 case ARRAY_TYPE:
509 tree d1 = TYPE_DOMAIN (t1);
510 tree d2 = TYPE_DOMAIN (t2);
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 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
521 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
522 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
523 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
524 break;
526 if (! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
527 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
528 val = 0;
530 break;
533 case RECORD_TYPE:
534 if (maybe_objc_comptypes (t1, t2, 0) == 1)
535 val = 1;
536 break;
538 default:
539 break;
541 return attrval == 2 && val == 1 ? 2 : val;
544 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
545 ignoring their qualifiers. */
547 static int
548 comp_target_types (ttl, ttr)
549 tree ttl, ttr;
551 int val;
553 /* Give maybe_objc_comptypes a crack at letting these types through. */
554 if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0)
555 return val;
557 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
558 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
560 if (val == 2 && pedantic)
561 pedwarn ("types are not quite compatible");
562 return val;
565 /* Subroutines of `comptypes'. */
567 /* Return 1 if two function types F1 and F2 are compatible.
568 If either type specifies no argument types,
569 the other must specify a fixed number of self-promoting arg types.
570 Otherwise, if one type specifies only the number of arguments,
571 the other must specify that number of self-promoting arg types.
572 Otherwise, the argument types must match. */
574 static int
575 function_types_compatible_p (f1, f2)
576 tree f1, f2;
578 tree args1, args2;
579 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
580 int val = 1;
581 int val1;
583 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
584 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
585 return 0;
587 args1 = TYPE_ARG_TYPES (f1);
588 args2 = TYPE_ARG_TYPES (f2);
590 /* An unspecified parmlist matches any specified parmlist
591 whose argument types don't need default promotions. */
593 if (args1 == 0)
595 if (!self_promoting_args_p (args2))
596 return 0;
597 /* If one of these types comes from a non-prototype fn definition,
598 compare that with the other type's arglist.
599 If they don't match, ask for a warning (but no error). */
600 if (TYPE_ACTUAL_ARG_TYPES (f1)
601 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
602 val = 2;
603 return val;
605 if (args2 == 0)
607 if (!self_promoting_args_p (args1))
608 return 0;
609 if (TYPE_ACTUAL_ARG_TYPES (f2)
610 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
611 val = 2;
612 return val;
615 /* Both types have argument lists: compare them and propagate results. */
616 val1 = type_lists_compatible_p (args1, args2);
617 return val1 != 1 ? val1 : val;
620 /* Check two lists of types for compatibility,
621 returning 0 for incompatible, 1 for compatible,
622 or 2 for compatible with warning. */
624 static int
625 type_lists_compatible_p (args1, args2)
626 tree args1, args2;
628 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
629 int val = 1;
630 int newval = 0;
632 while (1)
634 if (args1 == 0 && args2 == 0)
635 return val;
636 /* If one list is shorter than the other,
637 they fail to match. */
638 if (args1 == 0 || args2 == 0)
639 return 0;
640 /* A null pointer instead of a type
641 means there is supposed to be an argument
642 but nothing is specified about what type it has.
643 So match anything that self-promotes. */
644 if (TREE_VALUE (args1) == 0)
646 if (simple_type_promotes_to (TREE_VALUE (args2)) != NULL_TREE)
647 return 0;
649 else if (TREE_VALUE (args2) == 0)
651 if (simple_type_promotes_to (TREE_VALUE (args1)) != NULL_TREE)
652 return 0;
654 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
656 /* Allow wait (union {union wait *u; int *i} *)
657 and wait (union wait *) to be compatible. */
658 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
659 && (TYPE_NAME (TREE_VALUE (args1)) == 0
660 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
661 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
662 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
663 TYPE_SIZE (TREE_VALUE (args2))))
665 tree memb;
666 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
667 memb; memb = TREE_CHAIN (memb))
668 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
669 break;
670 if (memb == 0)
671 return 0;
673 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
674 && (TYPE_NAME (TREE_VALUE (args2)) == 0
675 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
676 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
677 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
678 TYPE_SIZE (TREE_VALUE (args1))))
680 tree memb;
681 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
682 memb; memb = TREE_CHAIN (memb))
683 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
684 break;
685 if (memb == 0)
686 return 0;
688 else
689 return 0;
692 /* comptypes said ok, but record if it said to warn. */
693 if (newval > val)
694 val = newval;
696 args1 = TREE_CHAIN (args1);
697 args2 = TREE_CHAIN (args2);
701 /* Compute the value of the `sizeof' operator. */
703 tree
704 c_sizeof (type)
705 tree type;
707 enum tree_code code = TREE_CODE (type);
708 tree size;
710 if (code == FUNCTION_TYPE)
712 if (pedantic || warn_pointer_arith)
713 pedwarn ("sizeof applied to a function type");
714 size = size_one_node;
716 else if (code == VOID_TYPE)
718 if (pedantic || warn_pointer_arith)
719 pedwarn ("sizeof applied to a void type");
720 size = size_one_node;
722 else if (code == ERROR_MARK)
723 size = size_one_node;
724 else if (!COMPLETE_TYPE_P (type))
726 error ("sizeof applied to an incomplete type");
727 size = size_zero_node;
729 else
730 /* Convert in case a char is more than one unit. */
731 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
732 size_int (TYPE_PRECISION (char_type_node)
733 / BITS_PER_UNIT));
735 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
736 TYPE_IS_SIZETYPE means that certain things (like overflow) will
737 never happen. However, this node should really have type
738 `size_t', which is just a typedef for an ordinary integer type. */
739 return fold (build1 (NOP_EXPR, c_size_type_node, size));
742 tree
743 c_sizeof_nowarn (type)
744 tree type;
746 enum tree_code code = TREE_CODE (type);
747 tree size;
749 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
750 size = size_one_node;
751 else if (!COMPLETE_TYPE_P (type))
752 size = size_zero_node;
753 else
754 /* Convert in case a char is more than one unit. */
755 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
756 size_int (TYPE_PRECISION (char_type_node)
757 / BITS_PER_UNIT));
759 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
760 TYPE_IS_SIZETYPE means that certain things (like overflow) will
761 never happen. However, this node should really have type
762 `size_t', which is just a typedef for an ordinary integer type. */
763 return fold (build1 (NOP_EXPR, c_size_type_node, size));
766 /* Compute the size to increment a pointer by. */
768 tree
769 c_size_in_bytes (type)
770 tree type;
772 enum tree_code code = TREE_CODE (type);
774 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
775 return size_one_node;
777 if (!COMPLETE_OR_VOID_TYPE_P (type))
779 error ("arithmetic on pointer to an incomplete type");
780 return size_one_node;
783 /* Convert in case a char is more than one unit. */
784 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
785 size_int (TYPE_PRECISION (char_type_node)
786 / BITS_PER_UNIT));
789 /* Implement the __alignof keyword: Return the minimum required
790 alignment of TYPE, measured in bytes. */
792 tree
793 c_alignof (type)
794 tree type;
796 enum tree_code code = TREE_CODE (type);
797 tree t;
799 if (code == FUNCTION_TYPE)
800 t = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
801 else if (code == VOID_TYPE || code == ERROR_MARK)
802 t = size_one_node;
803 else if (code == ERROR_MARK)
804 t = size_one_node;
805 else if (!COMPLETE_TYPE_P (type))
807 error ("__alignof__ applied to an incomplete type");
808 t = size_zero_node;
810 else
811 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
813 return fold (build1 (NOP_EXPR, c_size_type_node, t));
816 /* Implement the __alignof keyword: Return the minimum required
817 alignment of EXPR, measured in bytes. For VAR_DECL's and
818 FIELD_DECL's return DECL_ALIGN (which can be set from an
819 "aligned" __attribute__ specification). */
821 tree
822 c_alignof_expr (expr)
823 tree expr;
825 tree t;
827 if (TREE_CODE (expr) == VAR_DECL)
828 t = size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
830 else if (TREE_CODE (expr) == COMPONENT_REF
831 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
833 error ("`__alignof' applied to a bit-field");
834 t = size_one_node;
836 else if (TREE_CODE (expr) == COMPONENT_REF
837 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
838 t = size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
840 else if (TREE_CODE (expr) == INDIRECT_REF)
842 tree t = TREE_OPERAND (expr, 0);
843 tree best = t;
844 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
846 while (TREE_CODE (t) == NOP_EXPR
847 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
849 int thisalign;
851 t = TREE_OPERAND (t, 0);
852 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
853 if (thisalign > bestalign)
854 best = t, bestalign = thisalign;
856 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
858 else
859 return c_alignof (TREE_TYPE (expr));
861 return fold (build1 (NOP_EXPR, c_size_type_node, t));
864 /* Return either DECL or its known constant value (if it has one). */
866 tree
867 decl_constant_value (decl)
868 tree decl;
870 if (/* Don't change a variable array bound or initial value to a constant
871 in a place where a variable is invalid. */
872 current_function_decl != 0
873 && ! TREE_THIS_VOLATILE (decl)
874 && TREE_READONLY (decl)
875 && DECL_INITIAL (decl) != 0
876 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
877 /* This is invalid if initial value is not constant.
878 If it has either a function call, a memory reference,
879 or a variable, then re-evaluating it could give different results. */
880 && TREE_CONSTANT (DECL_INITIAL (decl))
881 /* Check for cases where this is sub-optimal, even though valid. */
882 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
883 return DECL_INITIAL (decl);
884 return decl;
887 /* Return either DECL or its known constant value (if it has one), but
888 return DECL if pedantic or DECL has mode BLKmode. This is for
889 bug-compatibility with the old behavior of decl_constant_value
890 (before GCC 3.0); every use of this function is a bug and it should
891 be removed before GCC 3.1. It is not appropriate to use pedantic
892 in a way that affects optimization, and BLKmode is probably not the
893 right test for avoiding misoptimizations either. */
895 static tree
896 decl_constant_value_for_broken_optimization (decl)
897 tree decl;
899 if (pedantic || DECL_MODE (decl) == BLKmode)
900 return decl;
901 else
902 return decl_constant_value (decl);
905 /* Perform default promotions for C data used in expressions.
906 Arrays and functions are converted to pointers;
907 enumeral types or short or char, to int.
908 In addition, manifest constants symbols are replaced by their values. */
910 tree
911 default_conversion (exp)
912 tree exp;
914 register tree type = TREE_TYPE (exp);
915 register enum tree_code code = TREE_CODE (type);
917 /* Constants can be used directly unless they're not loadable. */
918 if (TREE_CODE (exp) == CONST_DECL)
919 exp = DECL_INITIAL (exp);
921 /* Replace a nonvolatile const static variable with its value unless
922 it is an array, in which case we must be sure that taking the
923 address of the array produces consistent results. */
924 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
926 exp = decl_constant_value_for_broken_optimization (exp);
927 type = TREE_TYPE (exp);
930 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
931 an lvalue.
933 Do not use STRIP_NOPS here! It will remove conversions from pointer
934 to integer and cause infinite recursion. */
935 while (TREE_CODE (exp) == NON_LVALUE_EXPR
936 || (TREE_CODE (exp) == NOP_EXPR
937 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
938 exp = TREE_OPERAND (exp, 0);
940 /* Normally convert enums to int,
941 but convert wide enums to something wider. */
942 if (code == ENUMERAL_TYPE)
944 type = type_for_size (MAX (TYPE_PRECISION (type),
945 TYPE_PRECISION (integer_type_node)),
946 ((flag_traditional
947 || (TYPE_PRECISION (type)
948 >= TYPE_PRECISION (integer_type_node)))
949 && TREE_UNSIGNED (type)));
951 return convert (type, exp);
954 if (TREE_CODE (exp) == COMPONENT_REF
955 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
956 /* If it's thinner than an int, promote it like a
957 C_PROMOTING_INTEGER_TYPE_P, otherwise leave it alone. */
958 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
959 TYPE_PRECISION (integer_type_node)))
960 return convert (flag_traditional && TREE_UNSIGNED (type)
961 ? unsigned_type_node : integer_type_node,
962 exp);
964 if (C_PROMOTING_INTEGER_TYPE_P (type))
966 /* Traditionally, unsignedness is preserved in default promotions.
967 Also preserve unsignedness if not really getting any wider. */
968 if (TREE_UNSIGNED (type)
969 && (flag_traditional
970 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
971 return convert (unsigned_type_node, exp);
973 return convert (integer_type_node, exp);
976 if (code == BOOLEAN_TYPE)
977 return convert (integer_type_node, exp);
979 if (flag_traditional && !flag_allow_single_precision
980 && TYPE_MAIN_VARIANT (type) == float_type_node)
981 return convert (double_type_node, exp);
983 if (code == VOID_TYPE)
985 error ("void value not ignored as it ought to be");
986 return error_mark_node;
988 if (code == FUNCTION_TYPE)
990 return build_unary_op (ADDR_EXPR, exp, 0);
992 if (code == ARRAY_TYPE)
994 register tree adr;
995 tree restype = TREE_TYPE (type);
996 tree ptrtype;
997 int constp = 0;
998 int volatilep = 0;
1000 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1002 constp = TREE_READONLY (exp);
1003 volatilep = TREE_THIS_VOLATILE (exp);
1006 if (TYPE_QUALS (type) || constp || volatilep)
1007 restype
1008 = c_build_qualified_type (restype,
1009 TYPE_QUALS (type)
1010 | (constp * TYPE_QUAL_CONST)
1011 | (volatilep * TYPE_QUAL_VOLATILE));
1013 if (TREE_CODE (exp) == INDIRECT_REF)
1014 return convert (TYPE_POINTER_TO (restype),
1015 TREE_OPERAND (exp, 0));
1017 if (TREE_CODE (exp) == COMPOUND_EXPR)
1019 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1020 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1021 TREE_OPERAND (exp, 0), op1);
1024 if (! lvalue_p (exp)
1025 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1027 error ("invalid use of non-lvalue array");
1028 return error_mark_node;
1031 ptrtype = build_pointer_type (restype);
1033 if (TREE_CODE (exp) == VAR_DECL)
1035 /* ??? This is not really quite correct
1036 in that the type of the operand of ADDR_EXPR
1037 is not the target type of the type of the ADDR_EXPR itself.
1038 Question is, can this lossage be avoided? */
1039 adr = build1 (ADDR_EXPR, ptrtype, exp);
1040 if (mark_addressable (exp) == 0)
1041 return error_mark_node;
1042 TREE_CONSTANT (adr) = staticp (exp);
1043 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1044 return adr;
1046 /* This way is better for a COMPONENT_REF since it can
1047 simplify the offset for a component. */
1048 adr = build_unary_op (ADDR_EXPR, exp, 1);
1049 return convert (ptrtype, adr);
1051 return exp;
1054 /* Look up component name in the structure type definition.
1056 If this component name is found indirectly within an anonymous union,
1057 store in *INDIRECT the component which directly contains
1058 that anonymous union. Otherwise, set *INDIRECT to 0. */
1060 static tree
1061 lookup_field (type, component, indirect)
1062 tree type, component;
1063 tree *indirect;
1065 tree field;
1067 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1068 to the field elements. Use a binary search on this array to quickly
1069 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1070 will always be set for structures which have many elements. */
1072 if (TYPE_LANG_SPECIFIC (type))
1074 int bot, top, half;
1075 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1077 field = TYPE_FIELDS (type);
1078 bot = 0;
1079 top = TYPE_LANG_SPECIFIC (type)->len;
1080 while (top - bot > 1)
1082 half = (top - bot + 1) >> 1;
1083 field = field_array[bot+half];
1085 if (DECL_NAME (field) == NULL_TREE)
1087 /* Step through all anon unions in linear fashion. */
1088 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1090 tree anon = 0, junk;
1092 field = field_array[bot++];
1093 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1094 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1095 anon = lookup_field (TREE_TYPE (field), component, &junk);
1097 if (anon != NULL_TREE)
1099 *indirect = field;
1100 return anon;
1104 /* Entire record is only anon unions. */
1105 if (bot > top)
1106 return NULL_TREE;
1108 /* Restart the binary search, with new lower bound. */
1109 continue;
1112 if (DECL_NAME (field) == component)
1113 break;
1114 if (DECL_NAME (field) < component)
1115 bot += half;
1116 else
1117 top = bot + half;
1120 if (DECL_NAME (field_array[bot]) == component)
1121 field = field_array[bot];
1122 else if (DECL_NAME (field) != component)
1123 field = 0;
1125 else
1127 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1129 if (DECL_NAME (field) == NULL_TREE)
1131 tree junk;
1132 tree anon = 0;
1134 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1135 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1136 anon = lookup_field (TREE_TYPE (field), component, &junk);
1138 if (anon != NULL_TREE)
1140 *indirect = field;
1141 return anon;
1145 if (DECL_NAME (field) == component)
1146 break;
1150 *indirect = NULL_TREE;
1151 return field;
1154 /* Make an expression to refer to the COMPONENT field of
1155 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1157 tree
1158 build_component_ref (datum, component)
1159 tree datum, component;
1161 register tree type = TREE_TYPE (datum);
1162 register enum tree_code code = TREE_CODE (type);
1163 register tree field = NULL;
1164 register tree ref;
1166 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1167 unless we are not to support things not strictly ANSI. */
1168 switch (TREE_CODE (datum))
1170 case COMPOUND_EXPR:
1172 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1173 return build (COMPOUND_EXPR, TREE_TYPE (value),
1174 TREE_OPERAND (datum, 0), value);
1176 case COND_EXPR:
1177 return build_conditional_expr
1178 (TREE_OPERAND (datum, 0),
1179 build_component_ref (TREE_OPERAND (datum, 1), component),
1180 build_component_ref (TREE_OPERAND (datum, 2), component));
1182 default:
1183 break;
1186 /* See if there is a field or component with name COMPONENT. */
1188 if (code == RECORD_TYPE || code == UNION_TYPE)
1190 tree indirect = 0;
1192 if (!COMPLETE_TYPE_P (type))
1194 incomplete_type_error (NULL_TREE, type);
1195 return error_mark_node;
1198 field = lookup_field (type, component, &indirect);
1200 if (!field)
1202 error ("%s has no member named `%s'",
1203 code == RECORD_TYPE ? "structure" : "union",
1204 IDENTIFIER_POINTER (component));
1205 return error_mark_node;
1207 if (TREE_TYPE (field) == error_mark_node)
1208 return error_mark_node;
1210 /* If FIELD was found buried within an anonymous union,
1211 make one COMPONENT_REF to get that anonymous union,
1212 then fall thru to make a second COMPONENT_REF to get FIELD. */
1213 if (indirect != 0)
1215 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1216 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1217 TREE_READONLY (ref) = 1;
1218 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1219 TREE_THIS_VOLATILE (ref) = 1;
1220 datum = ref;
1223 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1225 if (TREE_READONLY (datum) || TREE_READONLY (field))
1226 TREE_READONLY (ref) = 1;
1227 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1228 TREE_THIS_VOLATILE (ref) = 1;
1230 return ref;
1232 else if (code != ERROR_MARK)
1233 error ("request for member `%s' in something not a structure or union",
1234 IDENTIFIER_POINTER (component));
1236 return error_mark_node;
1239 /* Given an expression PTR for a pointer, return an expression
1240 for the value pointed to.
1241 ERRORSTRING is the name of the operator to appear in error messages. */
1243 tree
1244 build_indirect_ref (ptr, errorstring)
1245 tree ptr;
1246 const char *errorstring;
1248 register tree pointer = default_conversion (ptr);
1249 register tree type = TREE_TYPE (pointer);
1251 if (TREE_CODE (type) == POINTER_TYPE)
1253 if (TREE_CODE (pointer) == ADDR_EXPR
1254 && !flag_volatile
1255 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1256 == TREE_TYPE (type)))
1257 return TREE_OPERAND (pointer, 0);
1258 else
1260 tree t = TREE_TYPE (type);
1261 register tree ref = build1 (INDIRECT_REF,
1262 TYPE_MAIN_VARIANT (t), pointer);
1264 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1266 error ("dereferencing pointer to incomplete type");
1267 return error_mark_node;
1269 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1270 warning ("dereferencing `void *' pointer");
1272 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1273 so that we get the proper error message if the result is used
1274 to assign to. Also, &* is supposed to be a no-op.
1275 And ANSI C seems to specify that the type of the result
1276 should be the const type. */
1277 /* A de-reference of a pointer to const is not a const. It is valid
1278 to change it via some other pointer. */
1279 TREE_READONLY (ref) = TYPE_READONLY (t);
1280 TREE_SIDE_EFFECTS (ref)
1281 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1282 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1283 return ref;
1286 else if (TREE_CODE (pointer) != ERROR_MARK)
1287 error ("invalid type argument of `%s'", errorstring);
1288 return error_mark_node;
1291 /* This handles expressions of the form "a[i]", which denotes
1292 an array reference.
1294 This is logically equivalent in C to *(a+i), but we may do it differently.
1295 If A is a variable or a member, we generate a primitive ARRAY_REF.
1296 This avoids forcing the array out of registers, and can work on
1297 arrays that are not lvalues (for example, members of structures returned
1298 by functions). */
1300 tree
1301 build_array_ref (array, index)
1302 tree array, index;
1304 if (index == 0)
1306 error ("subscript missing in array reference");
1307 return error_mark_node;
1310 if (TREE_TYPE (array) == error_mark_node
1311 || TREE_TYPE (index) == error_mark_node)
1312 return error_mark_node;
1314 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1315 && TREE_CODE (array) != INDIRECT_REF)
1317 tree rval, type;
1319 /* Subscripting with type char is likely to lose
1320 on a machine where chars are signed.
1321 So warn on any machine, but optionally.
1322 Don't warn for unsigned char since that type is safe.
1323 Don't warn for signed char because anyone who uses that
1324 must have done so deliberately. */
1325 if (warn_char_subscripts
1326 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1327 warning ("array subscript has type `char'");
1329 /* Apply default promotions *after* noticing character types. */
1330 index = default_conversion (index);
1332 /* Require integer *after* promotion, for sake of enums. */
1333 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1335 error ("array subscript is not an integer");
1336 return error_mark_node;
1339 /* An array that is indexed by a non-constant
1340 cannot be stored in a register; we must be able to do
1341 address arithmetic on its address.
1342 Likewise an array of elements of variable size. */
1343 if (TREE_CODE (index) != INTEGER_CST
1344 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1345 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1347 if (mark_addressable (array) == 0)
1348 return error_mark_node;
1350 /* An array that is indexed by a constant value which is not within
1351 the array bounds cannot be stored in a register either; because we
1352 would get a crash in store_bit_field/extract_bit_field when trying
1353 to access a non-existent part of the register. */
1354 if (TREE_CODE (index) == INTEGER_CST
1355 && TYPE_VALUES (TREE_TYPE (array))
1356 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1358 if (mark_addressable (array) == 0)
1359 return error_mark_node;
1362 if (pedantic)
1364 tree foo = array;
1365 while (TREE_CODE (foo) == COMPONENT_REF)
1366 foo = TREE_OPERAND (foo, 0);
1367 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1368 pedwarn ("ISO C forbids subscripting `register' array");
1369 else if (! flag_isoc99 && ! lvalue_p (foo))
1370 pedwarn ("ISO C89 forbids subscripting non-lvalue array");
1373 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1374 rval = build (ARRAY_REF, type, array, index);
1375 /* Array ref is const/volatile if the array elements are
1376 or if the array is. */
1377 TREE_READONLY (rval)
1378 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1379 | TREE_READONLY (array));
1380 TREE_SIDE_EFFECTS (rval)
1381 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1382 | TREE_SIDE_EFFECTS (array));
1383 TREE_THIS_VOLATILE (rval)
1384 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1385 /* This was added by rms on 16 Nov 91.
1386 It fixes vol struct foo *a; a->elts[1]
1387 in an inline function.
1388 Hope it doesn't break something else. */
1389 | TREE_THIS_VOLATILE (array));
1390 return require_complete_type (fold (rval));
1394 tree ar = default_conversion (array);
1395 tree ind = default_conversion (index);
1397 /* Do the same warning check as above, but only on the part that's
1398 syntactically the index and only if it is also semantically
1399 the index. */
1400 if (warn_char_subscripts
1401 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1402 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1403 warning ("subscript has type `char'");
1405 /* Put the integer in IND to simplify error checking. */
1406 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1408 tree temp = ar;
1409 ar = ind;
1410 ind = temp;
1413 if (ar == error_mark_node)
1414 return ar;
1416 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1417 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1419 error ("subscripted value is neither array nor pointer");
1420 return error_mark_node;
1422 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1424 error ("array subscript is not an integer");
1425 return error_mark_node;
1428 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1429 "array indexing");
1433 /* Build an external reference to identifier ID. FUN indicates
1434 whether this will be used for a function call. */
1435 tree
1436 build_external_ref (id, fun)
1437 tree id;
1438 int fun;
1440 tree ref;
1441 tree decl = lookup_name (id);
1442 tree objc_ivar = lookup_objc_ivar (id);
1444 if (!decl || decl == error_mark_node || C_DECL_ANTICIPATED (decl))
1446 if (objc_ivar)
1447 ref = objc_ivar;
1448 else if (fun)
1450 if (!decl || decl == error_mark_node)
1451 /* Ordinary implicit function declaration. */
1452 ref = implicitly_declare (id);
1453 else
1455 /* Implicit declaration of built-in function. Don't
1456 change the built-in declaration, but don't let this
1457 go by silently, either. */
1458 implicit_decl_warning (id);
1460 /* only issue this warning once */
1461 C_DECL_ANTICIPATED (decl) = 0;
1462 ref = decl;
1465 else
1467 /* Reference to undeclared variable, including reference to
1468 builtin outside of function-call context. */
1469 if (current_function_decl == 0)
1470 error ("`%s' undeclared here (not in a function)",
1471 IDENTIFIER_POINTER (id));
1472 else
1474 if (IDENTIFIER_GLOBAL_VALUE (id) != error_mark_node
1475 || IDENTIFIER_ERROR_LOCUS (id) != current_function_decl)
1477 error ("`%s' undeclared (first use in this function)",
1478 IDENTIFIER_POINTER (id));
1480 if (! undeclared_variable_notice)
1482 error ("(Each undeclared identifier is reported only once");
1483 error ("for each function it appears in.)");
1484 undeclared_variable_notice = 1;
1487 IDENTIFIER_GLOBAL_VALUE (id) = error_mark_node;
1488 IDENTIFIER_ERROR_LOCUS (id) = current_function_decl;
1490 return error_mark_node;
1493 else
1495 /* Properly declared variable or function reference. */
1496 if (!objc_ivar)
1497 ref = decl;
1498 else if (decl != objc_ivar && IDENTIFIER_LOCAL_VALUE (id))
1500 warning ("local declaration of `%s' hides instance variable",
1501 IDENTIFIER_POINTER (id));
1502 ref = decl;
1504 else
1505 ref = objc_ivar;
1508 if (TREE_TYPE (ref) == error_mark_node)
1509 return error_mark_node;
1511 assemble_external (ref);
1512 TREE_USED (ref) = 1;
1514 if (TREE_CODE (ref) == CONST_DECL)
1516 ref = DECL_INITIAL (ref);
1517 TREE_CONSTANT (ref) = 1;
1520 return ref;
1523 /* Build a function call to function FUNCTION with parameters PARAMS.
1524 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1525 TREE_VALUE of each node is a parameter-expression.
1526 FUNCTION's data type may be a function type or a pointer-to-function. */
1528 tree
1529 build_function_call (function, params)
1530 tree function, params;
1532 register tree fntype, fundecl = 0;
1533 register tree coerced_params;
1534 tree name = NULL_TREE, assembler_name = NULL_TREE, result;
1536 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1537 STRIP_TYPE_NOPS (function);
1539 /* Convert anything with function type to a pointer-to-function. */
1540 if (TREE_CODE (function) == FUNCTION_DECL)
1542 name = DECL_NAME (function);
1543 assembler_name = DECL_ASSEMBLER_NAME (function);
1545 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1546 (because calling an inline function does not mean the function
1547 needs to be separately compiled). */
1548 fntype = build_type_variant (TREE_TYPE (function),
1549 TREE_READONLY (function),
1550 TREE_THIS_VOLATILE (function));
1551 fundecl = function;
1552 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1554 else
1555 function = default_conversion (function);
1557 fntype = TREE_TYPE (function);
1559 if (TREE_CODE (fntype) == ERROR_MARK)
1560 return error_mark_node;
1562 if (!(TREE_CODE (fntype) == POINTER_TYPE
1563 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1565 error ("called object is not a function");
1566 return error_mark_node;
1569 /* fntype now gets the type of function pointed to. */
1570 fntype = TREE_TYPE (fntype);
1572 /* Convert the parameters to the types declared in the
1573 function prototype, or apply default promotions. */
1575 coerced_params
1576 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1578 /* Check for errors in format strings. */
1580 if (warn_format && (name || assembler_name))
1581 check_function_format (NULL, name, assembler_name, coerced_params);
1583 /* Recognize certain built-in functions so we can make tree-codes
1584 other than CALL_EXPR. We do this when it enables fold-const.c
1585 to do something useful. */
1587 if (TREE_CODE (function) == ADDR_EXPR
1588 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1589 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1591 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1592 params, coerced_params);
1593 if (result)
1594 return result;
1597 result = build (CALL_EXPR, TREE_TYPE (fntype),
1598 function, coerced_params, NULL_TREE);
1599 TREE_SIDE_EFFECTS (result) = 1;
1600 result = fold (result);
1602 if (VOID_TYPE_P (TREE_TYPE (result)))
1603 return result;
1604 return require_complete_type (result);
1607 /* Convert the argument expressions in the list VALUES
1608 to the types in the list TYPELIST. The result is a list of converted
1609 argument expressions.
1611 If TYPELIST is exhausted, or when an element has NULL as its type,
1612 perform the default conversions.
1614 PARMLIST is the chain of parm decls for the function being called.
1615 It may be 0, if that info is not available.
1616 It is used only for generating error messages.
1618 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1620 This is also where warnings about wrong number of args are generated.
1622 Both VALUES and the returned value are chains of TREE_LIST nodes
1623 with the elements of the list in the TREE_VALUE slots of those nodes. */
1625 static tree
1626 convert_arguments (typelist, values, name, fundecl)
1627 tree typelist, values, name, fundecl;
1629 register tree typetail, valtail;
1630 register tree result = NULL;
1631 int parmnum;
1633 /* Scan the given expressions and types, producing individual
1634 converted arguments and pushing them on RESULT in reverse order. */
1636 for (valtail = values, typetail = typelist, parmnum = 0;
1637 valtail;
1638 valtail = TREE_CHAIN (valtail), parmnum++)
1640 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1641 register tree val = TREE_VALUE (valtail);
1643 if (type == void_type_node)
1645 if (name)
1646 error ("too many arguments to function `%s'",
1647 IDENTIFIER_POINTER (name));
1648 else
1649 error ("too many arguments to function");
1650 break;
1653 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1654 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1655 to convert automatically to a pointer. */
1656 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1657 val = TREE_OPERAND (val, 0);
1659 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1660 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1661 val = default_conversion (val);
1663 val = require_complete_type (val);
1665 if (type != 0)
1667 /* Formal parm type is specified by a function prototype. */
1668 tree parmval;
1670 if (!COMPLETE_TYPE_P (type))
1672 error ("type of formal parameter %d is incomplete", parmnum + 1);
1673 parmval = val;
1675 else
1677 /* Optionally warn about conversions that
1678 differ from the default conversions. */
1679 if (warn_conversion)
1681 int formal_prec = TYPE_PRECISION (type);
1683 if (INTEGRAL_TYPE_P (type)
1684 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1685 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1686 else if (TREE_CODE (type) == COMPLEX_TYPE
1687 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1688 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1689 else if (TREE_CODE (type) == REAL_TYPE
1690 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1691 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1692 else if (TREE_CODE (type) == REAL_TYPE
1693 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1694 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1695 /* ??? At some point, messages should be written about
1696 conversions between complex types, but that's too messy
1697 to do now. */
1698 else if (TREE_CODE (type) == REAL_TYPE
1699 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1701 /* Warn if any argument is passed as `float',
1702 since without a prototype it would be `double'. */
1703 if (formal_prec == TYPE_PRECISION (float_type_node))
1704 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1706 /* Detect integer changing in width or signedness. */
1707 else if (INTEGRAL_TYPE_P (type)
1708 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1710 tree would_have_been = default_conversion (val);
1711 tree type1 = TREE_TYPE (would_have_been);
1713 if (TREE_CODE (type) == ENUMERAL_TYPE
1714 && type == TREE_TYPE (val))
1715 /* No warning if function asks for enum
1716 and the actual arg is that enum type. */
1718 else if (formal_prec != TYPE_PRECISION (type1))
1719 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1720 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1722 /* Don't complain if the formal parameter type
1723 is an enum, because we can't tell now whether
1724 the value was an enum--even the same enum. */
1725 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1727 else if (TREE_CODE (val) == INTEGER_CST
1728 && int_fits_type_p (val, type))
1729 /* Change in signedness doesn't matter
1730 if a constant value is unaffected. */
1732 /* Likewise for a constant in a NOP_EXPR. */
1733 else if (TREE_CODE (val) == NOP_EXPR
1734 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1735 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1737 #if 0 /* We never get such tree structure here. */
1738 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1739 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1740 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1741 /* Change in signedness doesn't matter
1742 if an enum value is unaffected. */
1744 #endif
1745 /* If the value is extended from a narrower
1746 unsigned type, it doesn't matter whether we
1747 pass it as signed or unsigned; the value
1748 certainly is the same either way. */
1749 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1750 && TREE_UNSIGNED (TREE_TYPE (val)))
1752 else if (TREE_UNSIGNED (type))
1753 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1754 else
1755 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1759 parmval = convert_for_assignment (type, val,
1760 (char *) 0, /* arg passing */
1761 fundecl, name, parmnum + 1);
1763 if (PROMOTE_PROTOTYPES
1764 && (TREE_CODE (type) == INTEGER_TYPE
1765 || TREE_CODE (type) == ENUMERAL_TYPE
1766 || TREE_CODE (type) == BOOLEAN_TYPE)
1767 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1768 parmval = default_conversion (parmval);
1770 result = tree_cons (NULL_TREE, parmval, result);
1772 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1773 && (TYPE_PRECISION (TREE_TYPE (val))
1774 < TYPE_PRECISION (double_type_node)))
1775 /* Convert `float' to `double'. */
1776 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1777 else
1778 /* Convert `short' and `char' to full-size `int'. */
1779 result = tree_cons (NULL_TREE, default_conversion (val), result);
1781 if (typetail)
1782 typetail = TREE_CHAIN (typetail);
1785 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1787 if (name)
1788 error ("too few arguments to function `%s'",
1789 IDENTIFIER_POINTER (name));
1790 else
1791 error ("too few arguments to function");
1794 return nreverse (result);
1797 /* This is the entry point used by the parser
1798 for binary operators in the input.
1799 In addition to constructing the expression,
1800 we check for operands that were written with other binary operators
1801 in a way that is likely to confuse the user. */
1803 tree
1804 parser_build_binary_op (code, arg1, arg2)
1805 enum tree_code code;
1806 tree arg1, arg2;
1808 tree result = build_binary_op (code, arg1, arg2, 1);
1810 char class;
1811 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1812 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1813 enum tree_code code1 = ERROR_MARK;
1814 enum tree_code code2 = ERROR_MARK;
1816 if (class1 == 'e' || class1 == '1'
1817 || class1 == '2' || class1 == '<')
1818 code1 = C_EXP_ORIGINAL_CODE (arg1);
1819 if (class2 == 'e' || class2 == '1'
1820 || class2 == '2' || class2 == '<')
1821 code2 = C_EXP_ORIGINAL_CODE (arg2);
1823 /* Check for cases such as x+y<<z which users are likely
1824 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1825 is cleared to prevent these warnings. */
1826 if (warn_parentheses)
1828 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1830 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1831 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1832 warning ("suggest parentheses around + or - inside shift");
1835 if (code == TRUTH_ORIF_EXPR)
1837 if (code1 == TRUTH_ANDIF_EXPR
1838 || code2 == TRUTH_ANDIF_EXPR)
1839 warning ("suggest parentheses around && within ||");
1842 if (code == BIT_IOR_EXPR)
1844 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1845 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1846 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1847 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1848 warning ("suggest parentheses around arithmetic 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 |");
1854 if (code == BIT_XOR_EXPR)
1856 if (code1 == BIT_AND_EXPR
1857 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1858 || code2 == BIT_AND_EXPR
1859 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1860 warning ("suggest parentheses around arithmetic in operand of ^");
1861 /* Check cases like x^y==z */
1862 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1863 warning ("suggest parentheses around comparison in operand of ^");
1866 if (code == BIT_AND_EXPR)
1868 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1869 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1870 warning ("suggest parentheses around + or - in operand of &");
1871 /* Check cases like x&y==z */
1872 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1873 warning ("suggest parentheses around comparison in operand of &");
1877 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1878 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1879 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1880 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1882 unsigned_conversion_warning (result, arg1);
1883 unsigned_conversion_warning (result, arg2);
1884 overflow_warning (result);
1886 class = TREE_CODE_CLASS (TREE_CODE (result));
1888 /* Record the code that was specified in the source,
1889 for the sake of warnings about confusing nesting. */
1890 if (class == 'e' || class == '1'
1891 || class == '2' || class == '<')
1892 C_SET_EXP_ORIGINAL_CODE (result, code);
1893 else
1895 int flag = TREE_CONSTANT (result);
1896 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1897 so that convert_for_assignment wouldn't strip it.
1898 That way, we got warnings for things like p = (1 - 1).
1899 But it turns out we should not get those warnings. */
1900 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1901 C_SET_EXP_ORIGINAL_CODE (result, code);
1902 TREE_CONSTANT (result) = flag;
1905 return result;
1908 /* Build a binary-operation expression without default conversions.
1909 CODE is the kind of expression to build.
1910 This function differs from `build' in several ways:
1911 the data type of the result is computed and recorded in it,
1912 warnings are generated if arg data types are invalid,
1913 special handling for addition and subtraction of pointers is known,
1914 and some optimization is done (operations on narrow ints
1915 are done in the narrower type when that gives the same result).
1916 Constant folding is also done before the result is returned.
1918 Note that the operands will never have enumeral types, or function
1919 or array types, because either they will have the default conversions
1920 performed or they have both just been converted to some other type in which
1921 the arithmetic is to be done. */
1923 tree
1924 build_binary_op (code, orig_op0, orig_op1, convert_p)
1925 enum tree_code code;
1926 tree orig_op0, orig_op1;
1927 int convert_p;
1929 tree type0, type1;
1930 register enum tree_code code0, code1;
1931 tree op0, op1;
1933 /* Expression code to give to the expression when it is built.
1934 Normally this is CODE, which is what the caller asked for,
1935 but in some special cases we change it. */
1936 register enum tree_code resultcode = code;
1938 /* Data type in which the computation is to be performed.
1939 In the simplest cases this is the common type of the arguments. */
1940 register tree result_type = NULL;
1942 /* Nonzero means operands have already been type-converted
1943 in whatever way is necessary.
1944 Zero means they need to be converted to RESULT_TYPE. */
1945 int converted = 0;
1947 /* Nonzero means create the expression with this type, rather than
1948 RESULT_TYPE. */
1949 tree build_type = 0;
1951 /* Nonzero means after finally constructing the expression
1952 convert it to this type. */
1953 tree final_type = 0;
1955 /* Nonzero if this is an operation like MIN or MAX which can
1956 safely be computed in short if both args are promoted shorts.
1957 Also implies COMMON.
1958 -1 indicates a bitwise operation; this makes a difference
1959 in the exact conditions for when it is safe to do the operation
1960 in a narrower mode. */
1961 int shorten = 0;
1963 /* Nonzero if this is a comparison operation;
1964 if both args are promoted shorts, compare the original shorts.
1965 Also implies COMMON. */
1966 int short_compare = 0;
1968 /* Nonzero if this is a right-shift operation, which can be computed on the
1969 original short and then promoted if the operand is a promoted short. */
1970 int short_shift = 0;
1972 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1973 int common = 0;
1975 if (convert_p)
1977 op0 = default_conversion (orig_op0);
1978 op1 = default_conversion (orig_op1);
1980 else
1982 op0 = orig_op0;
1983 op1 = orig_op1;
1986 type0 = TREE_TYPE (op0);
1987 type1 = TREE_TYPE (op1);
1989 /* The expression codes of the data types of the arguments tell us
1990 whether the arguments are integers, floating, pointers, etc. */
1991 code0 = TREE_CODE (type0);
1992 code1 = TREE_CODE (type1);
1994 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1995 STRIP_TYPE_NOPS (op0);
1996 STRIP_TYPE_NOPS (op1);
1998 /* If an error was already reported for one of the arguments,
1999 avoid reporting another error. */
2001 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2002 return error_mark_node;
2004 switch (code)
2006 case PLUS_EXPR:
2007 /* Handle the pointer + int case. */
2008 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2009 return pointer_int_sum (PLUS_EXPR, op0, op1);
2010 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2011 return pointer_int_sum (PLUS_EXPR, op1, op0);
2012 else
2013 common = 1;
2014 break;
2016 case MINUS_EXPR:
2017 /* Subtraction of two similar pointers.
2018 We must subtract them as integers, then divide by object size. */
2019 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2020 && comp_target_types (type0, type1))
2021 return pointer_diff (op0, op1);
2022 /* Handle pointer minus int. Just like pointer plus int. */
2023 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2024 return pointer_int_sum (MINUS_EXPR, op0, op1);
2025 else
2026 common = 1;
2027 break;
2029 case MULT_EXPR:
2030 common = 1;
2031 break;
2033 case TRUNC_DIV_EXPR:
2034 case CEIL_DIV_EXPR:
2035 case FLOOR_DIV_EXPR:
2036 case ROUND_DIV_EXPR:
2037 case EXACT_DIV_EXPR:
2038 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2039 || code0 == COMPLEX_TYPE)
2040 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2041 || code1 == COMPLEX_TYPE))
2043 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2044 resultcode = RDIV_EXPR;
2045 else
2046 /* Although it would be tempting to shorten always here, that
2047 loses on some targets, since the modulo instruction is
2048 undefined if the quotient can't be represented in the
2049 computation mode. We shorten only if unsigned or if
2050 dividing by something we know != -1. */
2051 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2052 || (TREE_CODE (op1) == INTEGER_CST
2053 && ! integer_all_onesp (op1)));
2054 common = 1;
2056 break;
2058 case BIT_AND_EXPR:
2059 case BIT_ANDTC_EXPR:
2060 case BIT_IOR_EXPR:
2061 case BIT_XOR_EXPR:
2062 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2063 shorten = -1;
2064 /* If one operand is a constant, and the other is a short type
2065 that has been converted to an int,
2066 really do the work in the short type and then convert the
2067 result to int. If we are lucky, the constant will be 0 or 1
2068 in the short type, making the entire operation go away. */
2069 if (TREE_CODE (op0) == INTEGER_CST
2070 && TREE_CODE (op1) == NOP_EXPR
2071 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2072 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2074 final_type = result_type;
2075 op1 = TREE_OPERAND (op1, 0);
2076 result_type = TREE_TYPE (op1);
2078 if (TREE_CODE (op1) == INTEGER_CST
2079 && TREE_CODE (op0) == NOP_EXPR
2080 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2081 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2083 final_type = result_type;
2084 op0 = TREE_OPERAND (op0, 0);
2085 result_type = TREE_TYPE (op0);
2087 break;
2089 case TRUNC_MOD_EXPR:
2090 case FLOOR_MOD_EXPR:
2091 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2093 /* Although it would be tempting to shorten always here, that loses
2094 on some targets, since the modulo instruction is undefined if the
2095 quotient can't be represented in the computation mode. We shorten
2096 only if unsigned or if dividing by something we know != -1. */
2097 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2098 || (TREE_CODE (op1) == INTEGER_CST
2099 && ! integer_all_onesp (op1)));
2100 common = 1;
2102 break;
2104 case TRUTH_ANDIF_EXPR:
2105 case TRUTH_ORIF_EXPR:
2106 case TRUTH_AND_EXPR:
2107 case TRUTH_OR_EXPR:
2108 case TRUTH_XOR_EXPR:
2109 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2110 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2111 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2112 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2114 /* Result of these operations is always an int,
2115 but that does not mean the operands should be
2116 converted to ints! */
2117 result_type = integer_type_node;
2118 op0 = truthvalue_conversion (op0);
2119 op1 = truthvalue_conversion (op1);
2120 converted = 1;
2122 break;
2124 /* Shift operations: result has same type as first operand;
2125 always convert second operand to int.
2126 Also set SHORT_SHIFT if shifting rightward. */
2128 case RSHIFT_EXPR:
2129 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2131 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2133 if (tree_int_cst_sgn (op1) < 0)
2134 warning ("right shift count is negative");
2135 else
2137 if (! integer_zerop (op1))
2138 short_shift = 1;
2140 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2141 warning ("right shift count >= width of type");
2145 /* Use the type of the value to be shifted.
2146 This is what most traditional C compilers do. */
2147 result_type = type0;
2148 /* Unless traditional, convert the shift-count to an integer,
2149 regardless of size of value being shifted. */
2150 if (! flag_traditional)
2152 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2153 op1 = convert (integer_type_node, op1);
2154 /* Avoid converting op1 to result_type later. */
2155 converted = 1;
2158 break;
2160 case LSHIFT_EXPR:
2161 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2163 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2165 if (tree_int_cst_sgn (op1) < 0)
2166 warning ("left shift count is negative");
2168 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2169 warning ("left shift count >= width of type");
2172 /* Use the type of the value to be shifted.
2173 This is what most traditional C compilers do. */
2174 result_type = type0;
2175 /* Unless traditional, convert the shift-count to an integer,
2176 regardless of size of value being shifted. */
2177 if (! flag_traditional)
2179 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2180 op1 = convert (integer_type_node, op1);
2181 /* Avoid converting op1 to result_type later. */
2182 converted = 1;
2185 break;
2187 case RROTATE_EXPR:
2188 case LROTATE_EXPR:
2189 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2191 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2193 if (tree_int_cst_sgn (op1) < 0)
2194 warning ("shift count is negative");
2195 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2196 warning ("shift count >= width of type");
2199 /* Use the type of the value to be shifted.
2200 This is what most traditional C compilers do. */
2201 result_type = type0;
2202 /* Unless traditional, convert the shift-count to an integer,
2203 regardless of size of value being shifted. */
2204 if (! flag_traditional)
2206 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2207 op1 = convert (integer_type_node, op1);
2208 /* Avoid converting op1 to result_type later. */
2209 converted = 1;
2212 break;
2214 case EQ_EXPR:
2215 case NE_EXPR:
2216 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2217 warning ("comparing floating point with == or != is unsafe");
2218 /* Result of comparison is always int,
2219 but don't convert the args to int! */
2220 build_type = integer_type_node;
2221 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2222 || code0 == COMPLEX_TYPE)
2223 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2224 || code1 == COMPLEX_TYPE))
2225 short_compare = 1;
2226 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2228 register tree tt0 = TREE_TYPE (type0);
2229 register tree tt1 = TREE_TYPE (type1);
2230 /* Anything compares with void *. void * compares with anything.
2231 Otherwise, the targets must be compatible
2232 and both must be object or both incomplete. */
2233 if (comp_target_types (type0, type1))
2234 result_type = common_type (type0, type1);
2235 else if (VOID_TYPE_P (tt0))
2237 /* op0 != orig_op0 detects the case of something
2238 whose value is 0 but which isn't a valid null ptr const. */
2239 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2240 && TREE_CODE (tt1) == FUNCTION_TYPE)
2241 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2243 else if (VOID_TYPE_P (tt1))
2245 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2246 && TREE_CODE (tt0) == FUNCTION_TYPE)
2247 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2249 else
2250 pedwarn ("comparison of distinct pointer types lacks a cast");
2252 if (result_type == NULL_TREE)
2253 result_type = ptr_type_node;
2255 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2256 && integer_zerop (op1))
2257 result_type = type0;
2258 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2259 && integer_zerop (op0))
2260 result_type = type1;
2261 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2263 result_type = type0;
2264 if (! flag_traditional)
2265 pedwarn ("comparison between pointer and integer");
2267 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2269 result_type = type1;
2270 if (! flag_traditional)
2271 pedwarn ("comparison between pointer and integer");
2273 break;
2275 case MAX_EXPR:
2276 case MIN_EXPR:
2277 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2278 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2279 shorten = 1;
2280 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2282 if (comp_target_types (type0, type1))
2284 result_type = common_type (type0, type1);
2285 if (pedantic
2286 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2287 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2289 else
2291 result_type = ptr_type_node;
2292 pedwarn ("comparison of distinct pointer types lacks a cast");
2295 break;
2297 case LE_EXPR:
2298 case GE_EXPR:
2299 case LT_EXPR:
2300 case GT_EXPR:
2301 build_type = integer_type_node;
2302 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2303 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2304 short_compare = 1;
2305 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2307 if (comp_target_types (type0, type1))
2309 result_type = common_type (type0, type1);
2310 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
2311 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
2312 pedwarn ("comparison of complete and incomplete pointers");
2313 else if (pedantic
2314 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2315 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2317 else
2319 result_type = ptr_type_node;
2320 pedwarn ("comparison of distinct pointer types lacks a cast");
2323 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2324 && integer_zerop (op1))
2326 result_type = type0;
2327 if (pedantic || extra_warnings)
2328 pedwarn ("ordered comparison of pointer with integer zero");
2330 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2331 && integer_zerop (op0))
2333 result_type = type1;
2334 if (pedantic)
2335 pedwarn ("ordered comparison of pointer with integer zero");
2337 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2339 result_type = type0;
2340 if (! flag_traditional)
2341 pedwarn ("comparison between pointer and integer");
2343 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2345 result_type = type1;
2346 if (! flag_traditional)
2347 pedwarn ("comparison between pointer and integer");
2349 break;
2351 case UNORDERED_EXPR:
2352 case ORDERED_EXPR:
2353 case UNLT_EXPR:
2354 case UNLE_EXPR:
2355 case UNGT_EXPR:
2356 case UNGE_EXPR:
2357 case UNEQ_EXPR:
2358 build_type = integer_type_node;
2359 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2361 error ("unordered comparison on non-floating point argument");
2362 return error_mark_node;
2364 common = 1;
2365 break;
2367 default:
2368 break;
2371 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2373 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2375 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2377 if (shorten || common || short_compare)
2378 result_type = common_type (type0, type1);
2380 /* For certain operations (which identify themselves by shorten != 0)
2381 if both args were extended from the same smaller type,
2382 do the arithmetic in that type and then extend.
2384 shorten !=0 and !=1 indicates a bitwise operation.
2385 For them, this optimization is safe only if
2386 both args are zero-extended or both are sign-extended.
2387 Otherwise, we might change the result.
2388 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2389 but calculated in (unsigned short) it would be (unsigned short)-1. */
2391 if (shorten && none_complex)
2393 int unsigned0, unsigned1;
2394 tree arg0 = get_narrower (op0, &unsigned0);
2395 tree arg1 = get_narrower (op1, &unsigned1);
2396 /* UNS is 1 if the operation to be done is an unsigned one. */
2397 int uns = TREE_UNSIGNED (result_type);
2398 tree type;
2400 final_type = result_type;
2402 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2403 but it *requires* conversion to FINAL_TYPE. */
2405 if ((TYPE_PRECISION (TREE_TYPE (op0))
2406 == TYPE_PRECISION (TREE_TYPE (arg0)))
2407 && TREE_TYPE (op0) != final_type)
2408 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2409 if ((TYPE_PRECISION (TREE_TYPE (op1))
2410 == TYPE_PRECISION (TREE_TYPE (arg1)))
2411 && TREE_TYPE (op1) != final_type)
2412 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2414 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2416 /* For bitwise operations, signedness of nominal type
2417 does not matter. Consider only how operands were extended. */
2418 if (shorten == -1)
2419 uns = unsigned0;
2421 /* Note that in all three cases below we refrain from optimizing
2422 an unsigned operation on sign-extended args.
2423 That would not be valid. */
2425 /* Both args variable: if both extended in same way
2426 from same width, do it in that width.
2427 Do it unsigned if args were zero-extended. */
2428 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2429 < TYPE_PRECISION (result_type))
2430 && (TYPE_PRECISION (TREE_TYPE (arg1))
2431 == TYPE_PRECISION (TREE_TYPE (arg0)))
2432 && unsigned0 == unsigned1
2433 && (unsigned0 || !uns))
2434 result_type
2435 = signed_or_unsigned_type (unsigned0,
2436 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2437 else if (TREE_CODE (arg0) == INTEGER_CST
2438 && (unsigned1 || !uns)
2439 && (TYPE_PRECISION (TREE_TYPE (arg1))
2440 < TYPE_PRECISION (result_type))
2441 && (type = signed_or_unsigned_type (unsigned1,
2442 TREE_TYPE (arg1)),
2443 int_fits_type_p (arg0, type)))
2444 result_type = type;
2445 else if (TREE_CODE (arg1) == INTEGER_CST
2446 && (unsigned0 || !uns)
2447 && (TYPE_PRECISION (TREE_TYPE (arg0))
2448 < TYPE_PRECISION (result_type))
2449 && (type = signed_or_unsigned_type (unsigned0,
2450 TREE_TYPE (arg0)),
2451 int_fits_type_p (arg1, type)))
2452 result_type = type;
2455 /* Shifts can be shortened if shifting right. */
2457 if (short_shift)
2459 int unsigned_arg;
2460 tree arg0 = get_narrower (op0, &unsigned_arg);
2462 final_type = result_type;
2464 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2465 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2467 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2468 /* We can shorten only if the shift count is less than the
2469 number of bits in the smaller type size. */
2470 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
2471 /* If arg is sign-extended and then unsigned-shifted,
2472 we can simulate this with a signed shift in arg's type
2473 only if the extended result is at least twice as wide
2474 as the arg. Otherwise, the shift could use up all the
2475 ones made by sign-extension and bring in zeros.
2476 We can't optimize that case at all, but in most machines
2477 it never happens because available widths are 2**N. */
2478 && (!TREE_UNSIGNED (final_type)
2479 || unsigned_arg
2480 || (2 * TYPE_PRECISION (TREE_TYPE (arg0))
2481 <= TYPE_PRECISION (result_type))))
2483 /* Do an unsigned shift if the operand was zero-extended. */
2484 result_type
2485 = signed_or_unsigned_type (unsigned_arg,
2486 TREE_TYPE (arg0));
2487 /* Convert value-to-be-shifted to that type. */
2488 if (TREE_TYPE (op0) != result_type)
2489 op0 = convert (result_type, op0);
2490 converted = 1;
2494 /* Comparison operations are shortened too but differently.
2495 They identify themselves by setting short_compare = 1. */
2497 if (short_compare)
2499 /* Don't write &op0, etc., because that would prevent op0
2500 from being kept in a register.
2501 Instead, make copies of the our local variables and
2502 pass the copies by reference, then copy them back afterward. */
2503 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2504 enum tree_code xresultcode = resultcode;
2505 tree val
2506 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2508 if (val != 0)
2509 return val;
2511 op0 = xop0, op1 = xop1;
2512 converted = 1;
2513 resultcode = xresultcode;
2515 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2516 && skip_evaluation == 0)
2518 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2519 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2520 int unsignedp0, unsignedp1;
2521 tree primop0 = get_narrower (op0, &unsignedp0);
2522 tree primop1 = get_narrower (op1, &unsignedp1);
2524 xop0 = orig_op0;
2525 xop1 = orig_op1;
2526 STRIP_TYPE_NOPS (xop0);
2527 STRIP_TYPE_NOPS (xop1);
2529 /* Give warnings for comparisons between signed and unsigned
2530 quantities that may fail.
2532 Do the checking based on the original operand trees, so that
2533 casts will be considered, but default promotions won't be.
2535 Do not warn if the comparison is being done in a signed type,
2536 since the signed type will only be chosen if it can represent
2537 all the values of the unsigned type. */
2538 if (! TREE_UNSIGNED (result_type))
2539 /* OK */;
2540 /* Do not warn if both operands are the same signedness. */
2541 else if (op0_signed == op1_signed)
2542 /* OK */;
2543 else
2545 tree sop, uop;
2547 if (op0_signed)
2548 sop = xop0, uop = xop1;
2549 else
2550 sop = xop1, uop = xop0;
2552 /* Do not warn if the signed quantity is an
2553 unsuffixed integer literal (or some static
2554 constant expression involving such literals or a
2555 conditional expression involving such literals)
2556 and it is non-negative. */
2557 if (tree_expr_nonnegative_p (sop))
2558 /* OK */;
2559 /* Do not warn if the comparison is an equality operation,
2560 the unsigned quantity is an integral constant, and it
2561 would fit in the result if the result were signed. */
2562 else if (TREE_CODE (uop) == INTEGER_CST
2563 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2564 && int_fits_type_p (uop, signed_type (result_type)))
2565 /* OK */;
2566 /* Do not warn if the unsigned quantity is an enumeration
2567 constant and its maximum value would fit in the result
2568 if the result were signed. */
2569 else if (TREE_CODE (uop) == INTEGER_CST
2570 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2571 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2572 signed_type (result_type)))
2573 /* OK */;
2574 else
2575 warning ("comparison between signed and unsigned");
2578 /* Warn if two unsigned values are being compared in a size
2579 larger than their original size, and one (and only one) is the
2580 result of a `~' operator. This comparison will always fail.
2582 Also warn if one operand is a constant, and the constant
2583 does not have all bits set that are set in the ~ operand
2584 when it is extended. */
2586 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2587 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2589 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2590 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2591 &unsignedp0);
2592 else
2593 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2594 &unsignedp1);
2596 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
2598 tree primop;
2599 HOST_WIDE_INT constant, mask;
2600 int unsignedp, bits;
2602 if (host_integerp (primop0, 0))
2604 primop = primop1;
2605 unsignedp = unsignedp1;
2606 constant = tree_low_cst (primop0, 0);
2608 else
2610 primop = primop0;
2611 unsignedp = unsignedp0;
2612 constant = tree_low_cst (primop1, 0);
2615 bits = TYPE_PRECISION (TREE_TYPE (primop));
2616 if (bits < TYPE_PRECISION (result_type)
2617 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
2619 mask = (~ (HOST_WIDE_INT) 0) << bits;
2620 if ((mask & constant) != mask)
2621 warning ("comparison of promoted ~unsigned with constant");
2624 else if (unsignedp0 && unsignedp1
2625 && (TYPE_PRECISION (TREE_TYPE (primop0))
2626 < TYPE_PRECISION (result_type))
2627 && (TYPE_PRECISION (TREE_TYPE (primop1))
2628 < TYPE_PRECISION (result_type)))
2629 warning ("comparison of promoted ~unsigned with unsigned");
2635 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2636 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2637 Then the expression will be built.
2638 It will be given type FINAL_TYPE if that is nonzero;
2639 otherwise, it will be given type RESULT_TYPE. */
2641 if (!result_type)
2643 binary_op_error (code);
2644 return error_mark_node;
2647 if (! converted)
2649 if (TREE_TYPE (op0) != result_type)
2650 op0 = convert (result_type, op0);
2651 if (TREE_TYPE (op1) != result_type)
2652 op1 = convert (result_type, op1);
2655 if (build_type == NULL_TREE)
2656 build_type = result_type;
2659 register tree result = build (resultcode, build_type, op0, op1);
2660 register tree folded;
2662 folded = fold (result);
2663 if (folded == result)
2664 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2665 if (final_type != 0)
2666 return convert (final_type, folded);
2667 return folded;
2671 /* Return a tree for the sum or difference (RESULTCODE says which)
2672 of pointer PTROP and integer INTOP. */
2674 static tree
2675 pointer_int_sum (resultcode, ptrop, intop)
2676 enum tree_code resultcode;
2677 register tree ptrop, intop;
2679 tree size_exp;
2681 register tree result;
2682 register tree folded;
2684 /* The result is a pointer of the same type that is being added. */
2686 register tree result_type = TREE_TYPE (ptrop);
2688 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2690 if (pedantic || warn_pointer_arith)
2691 pedwarn ("pointer of type `void *' used in arithmetic");
2692 size_exp = integer_one_node;
2694 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2696 if (pedantic || warn_pointer_arith)
2697 pedwarn ("pointer to a function used in arithmetic");
2698 size_exp = integer_one_node;
2700 else
2701 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2703 /* If what we are about to multiply by the size of the elements
2704 contains a constant term, apply distributive law
2705 and multiply that constant term separately.
2706 This helps produce common subexpressions. */
2708 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2709 && ! TREE_CONSTANT (intop)
2710 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2711 && TREE_CONSTANT (size_exp)
2712 /* If the constant comes from pointer subtraction,
2713 skip this optimization--it would cause an error. */
2714 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2715 /* If the constant is unsigned, and smaller than the pointer size,
2716 then we must skip this optimization. This is because it could cause
2717 an overflow error if the constant is negative but INTOP is not. */
2718 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2719 || (TYPE_PRECISION (TREE_TYPE (intop))
2720 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2722 enum tree_code subcode = resultcode;
2723 tree int_type = TREE_TYPE (intop);
2724 if (TREE_CODE (intop) == MINUS_EXPR)
2725 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2726 /* Convert both subexpression types to the type of intop,
2727 because weird cases involving pointer arithmetic
2728 can result in a sum or difference with different type args. */
2729 ptrop = build_binary_op (subcode, ptrop,
2730 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2731 intop = convert (int_type, TREE_OPERAND (intop, 0));
2734 /* Convert the integer argument to a type the same size as sizetype
2735 so the multiply won't overflow spuriously. */
2737 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2738 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2739 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2740 TREE_UNSIGNED (sizetype)), intop);
2742 /* Replace the integer argument with a suitable product by the object size.
2743 Do this multiplication as signed, then convert to the appropriate
2744 pointer type (actually unsigned integral). */
2746 intop = convert (result_type,
2747 build_binary_op (MULT_EXPR, intop,
2748 convert (TREE_TYPE (intop), size_exp), 1));
2750 /* Create the sum or difference. */
2752 result = build (resultcode, result_type, ptrop, intop);
2754 folded = fold (result);
2755 if (folded == result)
2756 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2757 return folded;
2760 /* Return a tree for the difference of pointers OP0 and OP1.
2761 The resulting tree has type int. */
2763 static tree
2764 pointer_diff (op0, op1)
2765 register tree op0, op1;
2767 register tree result, folded;
2768 tree restype = ptrdiff_type_node;
2770 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2772 if (pedantic || warn_pointer_arith)
2774 if (TREE_CODE (target_type) == VOID_TYPE)
2775 pedwarn ("pointer of type `void *' used in subtraction");
2776 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2777 pedwarn ("pointer to a function used in subtraction");
2780 /* First do the subtraction as integers;
2781 then drop through to build the divide operator.
2782 Do not do default conversions on the minus operator
2783 in case restype is a short type. */
2785 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2786 convert (restype, op1), 0);
2787 /* This generates an error if op1 is pointer to incomplete type. */
2788 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
2789 error ("arithmetic on pointer to an incomplete type");
2791 /* This generates an error if op0 is pointer to incomplete type. */
2792 op1 = c_size_in_bytes (target_type);
2794 /* Divide by the size, in easiest possible way. */
2796 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2798 folded = fold (result);
2799 if (folded == result)
2800 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2801 return folded;
2804 /* Construct and perhaps optimize a tree representation
2805 for a unary operation. CODE, a tree_code, specifies the operation
2806 and XARG is the operand. NOCONVERT nonzero suppresses
2807 the default promotions (such as from short to int). */
2809 tree
2810 build_unary_op (code, xarg, noconvert)
2811 enum tree_code code;
2812 tree xarg;
2813 int noconvert;
2815 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2816 register tree arg = xarg;
2817 register tree argtype = 0;
2818 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2819 tree val;
2821 if (typecode == ERROR_MARK)
2822 return error_mark_node;
2823 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2824 typecode = INTEGER_TYPE;
2826 switch (code)
2828 case CONVERT_EXPR:
2829 /* This is used for unary plus, because a CONVERT_EXPR
2830 is enough to prevent anybody from looking inside for
2831 associativity, but won't generate any code. */
2832 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2833 || typecode == COMPLEX_TYPE))
2835 error ("wrong type argument to unary plus");
2836 return error_mark_node;
2838 else if (!noconvert)
2839 arg = default_conversion (arg);
2840 break;
2842 case NEGATE_EXPR:
2843 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2844 || typecode == COMPLEX_TYPE))
2846 error ("wrong type argument to unary minus");
2847 return error_mark_node;
2849 else if (!noconvert)
2850 arg = default_conversion (arg);
2851 break;
2853 case BIT_NOT_EXPR:
2854 if (typecode == COMPLEX_TYPE)
2856 code = CONJ_EXPR;
2857 if (pedantic)
2858 pedwarn ("ISO C does not support `~' for complex conjugation");
2859 if (!noconvert)
2860 arg = default_conversion (arg);
2862 else if (typecode != INTEGER_TYPE)
2864 error ("wrong type argument to bit-complement");
2865 return error_mark_node;
2867 else if (!noconvert)
2868 arg = default_conversion (arg);
2869 break;
2871 case ABS_EXPR:
2872 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2873 || typecode == COMPLEX_TYPE))
2875 error ("wrong type argument to abs");
2876 return error_mark_node;
2878 else if (!noconvert)
2879 arg = default_conversion (arg);
2880 break;
2882 case CONJ_EXPR:
2883 /* Conjugating a real value is a no-op, but allow it anyway. */
2884 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2885 || typecode == COMPLEX_TYPE))
2887 error ("wrong type argument to conjugation");
2888 return error_mark_node;
2890 else if (!noconvert)
2891 arg = default_conversion (arg);
2892 break;
2894 case TRUTH_NOT_EXPR:
2895 if (typecode != INTEGER_TYPE
2896 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2897 && typecode != COMPLEX_TYPE
2898 /* These will convert to a pointer. */
2899 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2901 error ("wrong type argument to unary exclamation mark");
2902 return error_mark_node;
2904 arg = truthvalue_conversion (arg);
2905 return invert_truthvalue (arg);
2907 case NOP_EXPR:
2908 break;
2910 case REALPART_EXPR:
2911 if (TREE_CODE (arg) == COMPLEX_CST)
2912 return TREE_REALPART (arg);
2913 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2914 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2915 else
2916 return arg;
2918 case IMAGPART_EXPR:
2919 if (TREE_CODE (arg) == COMPLEX_CST)
2920 return TREE_IMAGPART (arg);
2921 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2922 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2923 else
2924 return convert (TREE_TYPE (arg), integer_zero_node);
2926 case PREINCREMENT_EXPR:
2927 case POSTINCREMENT_EXPR:
2928 case PREDECREMENT_EXPR:
2929 case POSTDECREMENT_EXPR:
2930 /* Handle complex lvalues (when permitted)
2931 by reduction to simpler cases. */
2933 val = unary_complex_lvalue (code, arg);
2934 if (val != 0)
2935 return val;
2937 /* Increment or decrement the real part of the value,
2938 and don't change the imaginary part. */
2939 if (typecode == COMPLEX_TYPE)
2941 tree real, imag;
2943 if (pedantic)
2944 pedwarn ("ISO C does not support `++' and `--' on complex types");
2946 arg = stabilize_reference (arg);
2947 real = build_unary_op (REALPART_EXPR, arg, 1);
2948 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2949 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2950 build_unary_op (code, real, 1), imag);
2953 /* Report invalid types. */
2955 if (typecode != POINTER_TYPE
2956 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2958 error ("wrong type argument to %s",
2959 code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2960 ? "increment" : "decrement");
2961 return error_mark_node;
2965 register tree inc;
2966 tree result_type = TREE_TYPE (arg);
2968 arg = get_unwidened (arg, 0);
2969 argtype = TREE_TYPE (arg);
2971 /* Compute the increment. */
2973 if (typecode == POINTER_TYPE)
2975 /* If pointer target is an undefined struct,
2976 we just cannot know how to do the arithmetic. */
2977 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2978 error ("%s of pointer to unknown structure",
2979 code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2980 ? "increment" : "decrement");
2981 else if ((pedantic || warn_pointer_arith)
2982 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2983 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2984 pedwarn ("wrong type argument to %s",
2985 code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR
2986 ? "increment" : "decrement");
2987 inc = c_size_in_bytes (TREE_TYPE (result_type));
2989 else
2990 inc = integer_one_node;
2992 inc = convert (argtype, inc);
2994 /* Handle incrementing a cast-expression. */
2996 while (1)
2997 switch (TREE_CODE (arg))
2999 case NOP_EXPR:
3000 case CONVERT_EXPR:
3001 case FLOAT_EXPR:
3002 case FIX_TRUNC_EXPR:
3003 case FIX_FLOOR_EXPR:
3004 case FIX_ROUND_EXPR:
3005 case FIX_CEIL_EXPR:
3006 pedantic_lvalue_warning (CONVERT_EXPR);
3007 /* If the real type has the same machine representation
3008 as the type it is cast to, we can make better output
3009 by adding directly to the inside of the cast. */
3010 if ((TREE_CODE (TREE_TYPE (arg))
3011 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
3012 && (TYPE_MODE (TREE_TYPE (arg))
3013 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
3014 arg = TREE_OPERAND (arg, 0);
3015 else
3017 tree incremented, modify, value;
3018 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3019 value = boolean_increment (code, arg);
3020 else
3022 arg = stabilize_reference (arg);
3023 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3024 value = arg;
3025 else
3026 value = save_expr (arg);
3027 incremented = build (((code == PREINCREMENT_EXPR
3028 || code == POSTINCREMENT_EXPR)
3029 ? PLUS_EXPR : MINUS_EXPR),
3030 argtype, value, inc);
3031 TREE_SIDE_EFFECTS (incremented) = 1;
3032 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3033 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3035 TREE_USED (value) = 1;
3036 return value;
3038 break;
3040 default:
3041 goto give_up;
3043 give_up:
3045 /* Complain about anything else that is not a true lvalue. */
3046 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3047 || code == POSTINCREMENT_EXPR)
3048 ? "invalid lvalue in increment"
3049 : "invalid lvalue in decrement")))
3050 return error_mark_node;
3052 /* Report a read-only lvalue. */
3053 if (TREE_READONLY (arg))
3054 readonly_warning (arg,
3055 ((code == PREINCREMENT_EXPR
3056 || code == POSTINCREMENT_EXPR)
3057 ? "increment" : "decrement"));
3059 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3060 val = boolean_increment (code, arg);
3061 else
3062 val = build (code, TREE_TYPE (arg), arg, inc);
3063 TREE_SIDE_EFFECTS (val) = 1;
3064 val = convert (result_type, val);
3065 if (TREE_CODE (val) != code)
3066 TREE_NO_UNUSED_WARNING (val) = 1;
3067 return val;
3070 case ADDR_EXPR:
3071 /* Note that this operation never does default_conversion
3072 regardless of NOCONVERT. */
3074 /* Let &* cancel out to simplify resulting code. */
3075 if (TREE_CODE (arg) == INDIRECT_REF)
3077 /* Don't let this be an lvalue. */
3078 if (lvalue_p (TREE_OPERAND (arg, 0)))
3079 return non_lvalue (TREE_OPERAND (arg, 0));
3080 return TREE_OPERAND (arg, 0);
3083 /* For &x[y], return x+y */
3084 if (TREE_CODE (arg) == ARRAY_REF)
3086 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
3087 return error_mark_node;
3088 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3089 TREE_OPERAND (arg, 1), 1);
3092 /* Handle complex lvalues (when permitted)
3093 by reduction to simpler cases. */
3094 val = unary_complex_lvalue (code, arg);
3095 if (val != 0)
3096 return val;
3098 #if 0 /* Turned off because inconsistent;
3099 float f; *&(int)f = 3.4 stores in int format
3100 whereas (int)f = 3.4 stores in float format. */
3101 /* Address of a cast is just a cast of the address
3102 of the operand of the cast. */
3103 switch (TREE_CODE (arg))
3105 case NOP_EXPR:
3106 case CONVERT_EXPR:
3107 case FLOAT_EXPR:
3108 case FIX_TRUNC_EXPR:
3109 case FIX_FLOOR_EXPR:
3110 case FIX_ROUND_EXPR:
3111 case FIX_CEIL_EXPR:
3112 if (pedantic)
3113 pedwarn ("ISO C forbids the address of a cast expression");
3114 return convert (build_pointer_type (TREE_TYPE (arg)),
3115 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3116 0));
3118 #endif
3120 /* Allow the address of a constructor if all the elements
3121 are constant. */
3122 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3124 /* Anything not already handled and not a true memory reference
3125 is an error. */
3126 else if (typecode != FUNCTION_TYPE
3127 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
3128 return error_mark_node;
3130 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3131 argtype = TREE_TYPE (arg);
3133 /* If the lvalue is const or volatile, merge that into the type
3134 to which the address will point. Note that you can't get a
3135 restricted pointer by taking the address of something, so we
3136 only have to deal with `const' and `volatile' here. */
3137 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3138 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3139 argtype = c_build_type_variant (argtype,
3140 TREE_READONLY (arg),
3141 TREE_THIS_VOLATILE (arg));
3143 argtype = build_pointer_type (argtype);
3145 if (mark_addressable (arg) == 0)
3146 return error_mark_node;
3149 tree addr;
3151 if (TREE_CODE (arg) == COMPONENT_REF)
3153 tree field = TREE_OPERAND (arg, 1);
3155 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3157 if (DECL_C_BIT_FIELD (field))
3159 error ("attempt to take address of bit-field structure member `%s'",
3160 IDENTIFIER_POINTER (DECL_NAME (field)));
3161 return error_mark_node;
3164 addr = fold (build (PLUS_EXPR, argtype,
3165 convert (argtype, addr),
3166 convert (argtype, byte_position (field))));
3168 else
3169 addr = build1 (code, argtype, arg);
3171 /* Address of a static or external variable or
3172 file-scope function counts as a constant. */
3173 if (staticp (arg)
3174 && ! (TREE_CODE (arg) == FUNCTION_DECL
3175 && DECL_CONTEXT (arg) != 0))
3176 TREE_CONSTANT (addr) = 1;
3177 return addr;
3180 default:
3181 break;
3184 if (argtype == 0)
3185 argtype = TREE_TYPE (arg);
3186 return fold (build1 (code, argtype, arg));
3189 #if 0
3190 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3191 convert ARG with the same conversions in the same order
3192 and return the result. */
3194 static tree
3195 convert_sequence (conversions, arg)
3196 tree conversions;
3197 tree arg;
3199 switch (TREE_CODE (conversions))
3201 case NOP_EXPR:
3202 case CONVERT_EXPR:
3203 case FLOAT_EXPR:
3204 case FIX_TRUNC_EXPR:
3205 case FIX_FLOOR_EXPR:
3206 case FIX_ROUND_EXPR:
3207 case FIX_CEIL_EXPR:
3208 return convert (TREE_TYPE (conversions),
3209 convert_sequence (TREE_OPERAND (conversions, 0),
3210 arg));
3212 default:
3213 return arg;
3216 #endif /* 0 */
3218 /* Return nonzero if REF is an lvalue valid for this language.
3219 Lvalues can be assigned, unless their type has TYPE_READONLY.
3220 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3223 lvalue_p (ref)
3224 tree ref;
3226 register enum tree_code code = TREE_CODE (ref);
3228 switch (code)
3230 case REALPART_EXPR:
3231 case IMAGPART_EXPR:
3232 case COMPONENT_REF:
3233 return lvalue_p (TREE_OPERAND (ref, 0));
3235 case STRING_CST:
3236 return 1;
3238 case INDIRECT_REF:
3239 case ARRAY_REF:
3240 case VAR_DECL:
3241 case PARM_DECL:
3242 case RESULT_DECL:
3243 case ERROR_MARK:
3244 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3245 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3247 case BIND_EXPR:
3248 case RTL_EXPR:
3249 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3251 default:
3252 return 0;
3256 /* Return nonzero if REF is an lvalue valid for this language;
3257 otherwise, print an error message and return zero. */
3260 lvalue_or_else (ref, msgid)
3261 tree ref;
3262 const char *msgid;
3264 int win = lvalue_p (ref);
3266 if (! win)
3267 error ("%s", msgid);
3269 return win;
3272 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3273 for certain kinds of expressions which are not really lvalues
3274 but which we can accept as lvalues.
3276 If ARG is not a kind of expression we can handle, return zero. */
3278 static tree
3279 unary_complex_lvalue (code, arg)
3280 enum tree_code code;
3281 tree arg;
3283 /* Handle (a, b) used as an "lvalue". */
3284 if (TREE_CODE (arg) == COMPOUND_EXPR)
3286 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3288 /* If this returns a function type, it isn't really being used as
3289 an lvalue, so don't issue a warning about it. */
3290 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3291 pedantic_lvalue_warning (COMPOUND_EXPR);
3293 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3294 TREE_OPERAND (arg, 0), real_result);
3297 /* Handle (a ? b : c) used as an "lvalue". */
3298 if (TREE_CODE (arg) == COND_EXPR)
3300 pedantic_lvalue_warning (COND_EXPR);
3301 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3302 pedantic_lvalue_warning (COMPOUND_EXPR);
3304 return (build_conditional_expr
3305 (TREE_OPERAND (arg, 0),
3306 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3307 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3310 return 0;
3313 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3314 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3316 static void
3317 pedantic_lvalue_warning (code)
3318 enum tree_code code;
3320 if (pedantic)
3321 switch (code)
3323 case COND_EXPR:
3324 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
3325 break;
3326 case COMPOUND_EXPR:
3327 pedwarn ("ISO C forbids use of compound expressions as lvalues");
3328 break;
3329 default:
3330 pedwarn ("ISO C forbids use of cast expressions as lvalues");
3331 break;
3335 /* Warn about storing in something that is `const'. */
3337 void
3338 readonly_warning (arg, msgid)
3339 tree arg;
3340 const char *msgid;
3342 if (TREE_CODE (arg) == COMPONENT_REF)
3344 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3345 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3346 else
3347 pedwarn ("%s of read-only member `%s'", _(msgid),
3348 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3350 else if (TREE_CODE (arg) == VAR_DECL)
3351 pedwarn ("%s of read-only variable `%s'", _(msgid),
3352 IDENTIFIER_POINTER (DECL_NAME (arg)));
3353 else
3354 pedwarn ("%s of read-only location", _(msgid));
3357 /* Mark EXP saying that we need to be able to take the
3358 address of it; it should not be allocated in a register.
3359 Value is 1 if successful. */
3362 mark_addressable (exp)
3363 tree exp;
3365 register tree x = exp;
3366 while (1)
3367 switch (TREE_CODE (x))
3369 case COMPONENT_REF:
3370 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3372 error ("cannot take address of bitfield `%s'",
3373 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3374 return 0;
3377 /* ... fall through ... */
3379 case ADDR_EXPR:
3380 case ARRAY_REF:
3381 case REALPART_EXPR:
3382 case IMAGPART_EXPR:
3383 x = TREE_OPERAND (x, 0);
3384 break;
3386 case CONSTRUCTOR:
3387 TREE_ADDRESSABLE (x) = 1;
3388 return 1;
3390 case VAR_DECL:
3391 case CONST_DECL:
3392 case PARM_DECL:
3393 case RESULT_DECL:
3394 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3395 && DECL_NONLOCAL (x))
3397 if (TREE_PUBLIC (x))
3399 error ("global register variable `%s' used in nested function",
3400 IDENTIFIER_POINTER (DECL_NAME (x)));
3401 return 0;
3403 pedwarn ("register variable `%s' used in nested function",
3404 IDENTIFIER_POINTER (DECL_NAME (x)));
3406 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3408 if (TREE_PUBLIC (x))
3410 error ("address of global register variable `%s' requested",
3411 IDENTIFIER_POINTER (DECL_NAME (x)));
3412 return 0;
3415 /* If we are making this addressable due to its having
3416 volatile components, give a different error message. Also
3417 handle the case of an unnamed parameter by not trying
3418 to give the name. */
3420 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3422 error ("cannot put object with volatile field into register");
3423 return 0;
3426 pedwarn ("address of register variable `%s' requested",
3427 IDENTIFIER_POINTER (DECL_NAME (x)));
3429 put_var_into_stack (x);
3431 /* drops in */
3432 case FUNCTION_DECL:
3433 TREE_ADDRESSABLE (x) = 1;
3434 #if 0 /* poplevel deals with this now. */
3435 if (DECL_CONTEXT (x) == 0)
3436 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3437 #endif
3439 default:
3440 return 1;
3444 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3446 tree
3447 build_conditional_expr (ifexp, op1, op2)
3448 tree ifexp, op1, op2;
3450 register tree type1;
3451 register tree type2;
3452 register enum tree_code code1;
3453 register enum tree_code code2;
3454 register tree result_type = NULL;
3455 tree orig_op1 = op1, orig_op2 = op2;
3457 ifexp = truthvalue_conversion (default_conversion (ifexp));
3459 #if 0 /* Produces wrong result if within sizeof. */
3460 /* Don't promote the operands separately if they promote
3461 the same way. Return the unpromoted type and let the combined
3462 value get promoted if necessary. */
3464 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3465 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3466 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3467 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3469 if (TREE_CODE (ifexp) == INTEGER_CST)
3470 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3472 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3474 #endif
3476 /* Promote both alternatives. */
3478 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3479 op1 = default_conversion (op1);
3480 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3481 op2 = default_conversion (op2);
3483 if (TREE_CODE (ifexp) == ERROR_MARK
3484 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3485 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3486 return error_mark_node;
3488 type1 = TREE_TYPE (op1);
3489 code1 = TREE_CODE (type1);
3490 type2 = TREE_TYPE (op2);
3491 code2 = TREE_CODE (type2);
3493 /* Quickly detect the usual case where op1 and op2 have the same type
3494 after promotion. */
3495 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3497 if (type1 == type2)
3498 result_type = type1;
3499 else
3500 result_type = TYPE_MAIN_VARIANT (type1);
3502 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3503 || code1 == COMPLEX_TYPE)
3504 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3505 || code2 == COMPLEX_TYPE))
3507 result_type = common_type (type1, type2);
3509 /* If -Wsign-compare, warn here if type1 and type2 have
3510 different signedness. We'll promote the signed to unsigned
3511 and later code won't know it used to be different.
3512 Do this check on the original types, so that explicit casts
3513 will be considered, but default promotions won't. */
3514 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare)
3515 && !skip_evaluation)
3517 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3518 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3520 if (unsigned_op1 ^ unsigned_op2)
3522 /* Do not warn if the result type is signed, since the
3523 signed type will only be chosen if it can represent
3524 all the values of the unsigned type. */
3525 if (! TREE_UNSIGNED (result_type))
3526 /* OK */;
3527 /* Do not warn if the signed quantity is an unsuffixed
3528 integer literal (or some static constant expression
3529 involving such literals) and it is non-negative. */
3530 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3531 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3532 /* OK */;
3533 else
3534 warning ("signed and unsigned type in conditional expression");
3538 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3540 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3541 pedwarn ("ISO C forbids conditional expr with only one void side");
3542 result_type = void_type_node;
3544 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3546 if (comp_target_types (type1, type2))
3547 result_type = common_type (type1, type2);
3548 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3549 && TREE_CODE (orig_op1) != NOP_EXPR)
3550 result_type = qualify_type (type2, type1);
3551 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3552 && TREE_CODE (orig_op2) != NOP_EXPR)
3553 result_type = qualify_type (type1, type2);
3554 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3556 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3557 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3558 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3559 TREE_TYPE (type2)));
3561 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3563 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3564 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3565 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3566 TREE_TYPE (type1)));
3568 else
3570 pedwarn ("pointer type mismatch in conditional expression");
3571 result_type = build_pointer_type (void_type_node);
3574 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3576 if (! integer_zerop (op2))
3577 pedwarn ("pointer/integer type mismatch in conditional expression");
3578 else
3580 op2 = null_pointer_node;
3582 result_type = type1;
3584 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3586 if (!integer_zerop (op1))
3587 pedwarn ("pointer/integer type mismatch in conditional expression");
3588 else
3590 op1 = null_pointer_node;
3592 result_type = type2;
3595 if (!result_type)
3597 if (flag_cond_mismatch)
3598 result_type = void_type_node;
3599 else
3601 error ("type mismatch in conditional expression");
3602 return error_mark_node;
3606 /* Merge const and volatile flags of the incoming types. */
3607 result_type
3608 = build_type_variant (result_type,
3609 TREE_READONLY (op1) || TREE_READONLY (op2),
3610 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3612 if (result_type != TREE_TYPE (op1))
3613 op1 = convert_and_check (result_type, op1);
3614 if (result_type != TREE_TYPE (op2))
3615 op2 = convert_and_check (result_type, op2);
3617 if (TREE_CODE (ifexp) == INTEGER_CST)
3618 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3620 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3623 /* Given a list of expressions, return a compound expression
3624 that performs them all and returns the value of the last of them. */
3626 tree
3627 build_compound_expr (list)
3628 tree list;
3630 return internal_build_compound_expr (list, TRUE);
3633 static tree
3634 internal_build_compound_expr (list, first_p)
3635 tree list;
3636 int first_p;
3638 register tree rest;
3640 if (TREE_CHAIN (list) == 0)
3642 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3643 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3645 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3646 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3647 list = TREE_OPERAND (list, 0);
3648 #endif
3650 /* Don't let (0, 0) be null pointer constant. */
3651 if (!first_p && integer_zerop (TREE_VALUE (list)))
3652 return non_lvalue (TREE_VALUE (list));
3653 return TREE_VALUE (list);
3656 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3658 /* Convert arrays to pointers when there really is a comma operator. */
3659 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3660 TREE_VALUE (TREE_CHAIN (list))
3661 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3664 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3666 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3668 /* The left-hand operand of a comma expression is like an expression
3669 statement: with -W or -Wunused, we should warn if it doesn't have
3670 any side-effects, unless it was explicitly cast to (void). */
3671 if ((extra_warnings || warn_unused_value)
3672 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3673 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
3674 warning ("left-hand operand of comma expression has no effect");
3676 /* When pedantic, a compound expression can be neither an lvalue
3677 nor an integer constant expression. */
3678 if (! pedantic)
3679 return rest;
3682 /* With -Wunused, we should also warn if the left-hand operand does have
3683 side-effects, but computes a value which is not used. For example, in
3684 `foo() + bar(), baz()' the result of the `+' operator is not used,
3685 so we should issue a warning. */
3686 else if (warn_unused_value)
3687 warn_if_unused_value (TREE_VALUE (list));
3689 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3692 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3694 tree
3695 build_c_cast (type, expr)
3696 register tree type;
3697 tree expr;
3699 register tree value = expr;
3701 if (type == error_mark_node || expr == error_mark_node)
3702 return error_mark_node;
3703 type = TYPE_MAIN_VARIANT (type);
3705 #if 0
3706 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3707 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3708 value = TREE_OPERAND (value, 0);
3709 #endif
3711 if (TREE_CODE (type) == ARRAY_TYPE)
3713 error ("cast specifies array type");
3714 return error_mark_node;
3717 if (TREE_CODE (type) == FUNCTION_TYPE)
3719 error ("cast specifies function type");
3720 return error_mark_node;
3723 if (type == TREE_TYPE (value))
3725 if (pedantic)
3727 if (TREE_CODE (type) == RECORD_TYPE
3728 || TREE_CODE (type) == UNION_TYPE)
3729 pedwarn ("ISO C forbids casting nonscalar to the same type");
3732 else if (TREE_CODE (type) == UNION_TYPE)
3734 tree field;
3735 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3736 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3737 value = default_conversion (value);
3739 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3740 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3741 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3742 break;
3744 if (field)
3746 const char *name;
3747 tree t;
3749 if (pedantic)
3750 pedwarn ("ISO C forbids casts to union type");
3751 if (TYPE_NAME (type) != 0)
3753 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3754 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3755 else
3756 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3758 else
3759 name = "";
3760 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3761 build_tree_list (field, value)),
3762 0, 0);
3763 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3764 return t;
3766 error ("cast to union type from type not present in union");
3767 return error_mark_node;
3769 else
3771 tree otype, ovalue;
3773 /* If casting to void, avoid the error that would come
3774 from default_conversion in the case of a non-lvalue array. */
3775 if (type == void_type_node)
3776 return build1 (CONVERT_EXPR, type, value);
3778 /* Convert functions and arrays to pointers,
3779 but don't convert any other types. */
3780 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3781 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3782 value = default_conversion (value);
3783 otype = TREE_TYPE (value);
3785 /* Optionally warn about potentially worrisome casts. */
3787 if (warn_cast_qual
3788 && TREE_CODE (type) == POINTER_TYPE
3789 && TREE_CODE (otype) == POINTER_TYPE)
3791 tree in_type = type;
3792 tree in_otype = otype;
3793 int warn = 0;
3795 /* Check that the qualifiers on IN_TYPE are a superset of
3796 the qualifiers of IN_OTYPE. The outermost level of
3797 POINTER_TYPE nodes is uninteresting and we stop as soon
3798 as we hit a non-POINTER_TYPE node on either type. */
3801 in_otype = TREE_TYPE (in_otype);
3802 in_type = TREE_TYPE (in_type);
3803 warn |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3805 while (TREE_CODE (in_type) == POINTER_TYPE
3806 && TREE_CODE (in_otype) == POINTER_TYPE);
3808 if (warn)
3809 /* There are qualifiers present in IN_OTYPE that are not
3810 present in IN_TYPE. */
3811 warning ("cast discards qualifiers from pointer target type");
3814 /* Warn about possible alignment problems. */
3815 if (STRICT_ALIGNMENT && warn_cast_align
3816 && TREE_CODE (type) == POINTER_TYPE
3817 && TREE_CODE (otype) == POINTER_TYPE
3818 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3819 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3820 /* Don't warn about opaque types, where the actual alignment
3821 restriction is unknown. */
3822 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3823 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3824 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3825 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3826 warning ("cast increases required alignment of target type");
3828 if (TREE_CODE (type) == INTEGER_TYPE
3829 && TREE_CODE (otype) == POINTER_TYPE
3830 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3831 && !TREE_CONSTANT (value))
3832 warning ("cast from pointer to integer of different size");
3834 if (warn_bad_function_cast
3835 && TREE_CODE (value) == CALL_EXPR
3836 && TREE_CODE (type) != TREE_CODE (otype))
3837 warning ("cast does not match function type");
3839 if (TREE_CODE (type) == POINTER_TYPE
3840 && TREE_CODE (otype) == INTEGER_TYPE
3841 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3842 /* Don't warn about converting any constant. */
3843 && !TREE_CONSTANT (value))
3844 warning ("cast to pointer from integer of different size");
3846 ovalue = value;
3847 value = convert (type, value);
3849 /* Ignore any integer overflow caused by the cast. */
3850 if (TREE_CODE (value) == INTEGER_CST)
3852 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3853 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3857 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3858 if (pedantic && TREE_CODE (value) == INTEGER_CST
3859 && TREE_CODE (expr) == INTEGER_CST
3860 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3861 value = non_lvalue (value);
3863 /* If pedantic, don't let a cast be an lvalue. */
3864 if (value == expr && pedantic)
3865 value = non_lvalue (value);
3867 return value;
3870 /* Build an assignment expression of lvalue LHS from value RHS.
3871 MODIFYCODE is the code for a binary operator that we use
3872 to combine the old value of LHS with RHS to get the new value.
3873 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3875 tree
3876 build_modify_expr (lhs, modifycode, rhs)
3877 tree lhs, rhs;
3878 enum tree_code modifycode;
3880 register tree result;
3881 tree newrhs;
3882 tree lhstype = TREE_TYPE (lhs);
3883 tree olhstype = lhstype;
3885 /* Types that aren't fully specified cannot be used in assignments. */
3886 lhs = require_complete_type (lhs);
3888 /* Avoid duplicate error messages from operands that had errors. */
3889 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3890 return error_mark_node;
3892 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3893 /* Do not use STRIP_NOPS here. We do not want an enumerator
3894 whose value is 0 to count as a null pointer constant. */
3895 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3896 rhs = TREE_OPERAND (rhs, 0);
3898 newrhs = rhs;
3900 /* Handle control structure constructs used as "lvalues". */
3902 switch (TREE_CODE (lhs))
3904 /* Handle (a, b) used as an "lvalue". */
3905 case COMPOUND_EXPR:
3906 pedantic_lvalue_warning (COMPOUND_EXPR);
3907 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3908 if (TREE_CODE (newrhs) == ERROR_MARK)
3909 return error_mark_node;
3910 return build (COMPOUND_EXPR, lhstype,
3911 TREE_OPERAND (lhs, 0), newrhs);
3913 /* Handle (a ? b : c) used as an "lvalue". */
3914 case COND_EXPR:
3915 pedantic_lvalue_warning (COND_EXPR);
3916 rhs = save_expr (rhs);
3918 /* Produce (a ? (b = rhs) : (c = rhs))
3919 except that the RHS goes through a save-expr
3920 so the code to compute it is only emitted once. */
3921 tree cond
3922 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3923 build_modify_expr (TREE_OPERAND (lhs, 1),
3924 modifycode, rhs),
3925 build_modify_expr (TREE_OPERAND (lhs, 2),
3926 modifycode, rhs));
3927 if (TREE_CODE (cond) == ERROR_MARK)
3928 return cond;
3929 /* Make sure the code to compute the rhs comes out
3930 before the split. */
3931 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3932 /* But cast it to void to avoid an "unused" error. */
3933 convert (void_type_node, rhs), cond);
3935 default:
3936 break;
3939 /* If a binary op has been requested, combine the old LHS value with the RHS
3940 producing the value we should actually store into the LHS. */
3942 if (modifycode != NOP_EXPR)
3944 lhs = stabilize_reference (lhs);
3945 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3948 /* Handle a cast used as an "lvalue".
3949 We have already performed any binary operator using the value as cast.
3950 Now convert the result to the cast type of the lhs,
3951 and then true type of the lhs and store it there;
3952 then convert result back to the cast type to be the value
3953 of the assignment. */
3955 switch (TREE_CODE (lhs))
3957 case NOP_EXPR:
3958 case CONVERT_EXPR:
3959 case FLOAT_EXPR:
3960 case FIX_TRUNC_EXPR:
3961 case FIX_FLOOR_EXPR:
3962 case FIX_ROUND_EXPR:
3963 case FIX_CEIL_EXPR:
3964 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3965 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3966 newrhs = default_conversion (newrhs);
3968 tree inner_lhs = TREE_OPERAND (lhs, 0);
3969 tree result;
3970 result = build_modify_expr (inner_lhs, NOP_EXPR,
3971 convert (TREE_TYPE (inner_lhs),
3972 convert (lhstype, newrhs)));
3973 if (TREE_CODE (result) == ERROR_MARK)
3974 return result;
3975 pedantic_lvalue_warning (CONVERT_EXPR);
3976 return convert (TREE_TYPE (lhs), result);
3979 default:
3980 break;
3983 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3984 Reject anything strange now. */
3986 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3987 return error_mark_node;
3989 /* Warn about storing in something that is `const'. */
3991 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3992 || ((TREE_CODE (lhstype) == RECORD_TYPE
3993 || TREE_CODE (lhstype) == UNION_TYPE)
3994 && C_TYPE_FIELDS_READONLY (lhstype)))
3995 readonly_warning (lhs, "assignment");
3997 /* If storing into a structure or union member,
3998 it has probably been given type `int'.
3999 Compute the type that would go with
4000 the actual amount of storage the member occupies. */
4002 if (TREE_CODE (lhs) == COMPONENT_REF
4003 && (TREE_CODE (lhstype) == INTEGER_TYPE
4004 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4005 || TREE_CODE (lhstype) == REAL_TYPE
4006 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4007 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4009 /* If storing in a field that is in actuality a short or narrower than one,
4010 we must store in the field in its actual type. */
4012 if (lhstype != TREE_TYPE (lhs))
4014 lhs = copy_node (lhs);
4015 TREE_TYPE (lhs) = lhstype;
4018 /* Convert new value to destination type. */
4020 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
4021 NULL_TREE, NULL_TREE, 0);
4022 if (TREE_CODE (newrhs) == ERROR_MARK)
4023 return error_mark_node;
4025 /* Scan operands */
4027 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4028 TREE_SIDE_EFFECTS (result) = 1;
4030 /* If we got the LHS in a different type for storing in,
4031 convert the result back to the nominal type of LHS
4032 so that the value we return always has the same type
4033 as the LHS argument. */
4035 if (olhstype == TREE_TYPE (result))
4036 return result;
4037 return convert_for_assignment (olhstype, result, _("assignment"),
4038 NULL_TREE, NULL_TREE, 0);
4041 /* Convert value RHS to type TYPE as preparation for an assignment
4042 to an lvalue of type TYPE.
4043 The real work of conversion is done by `convert'.
4044 The purpose of this function is to generate error messages
4045 for assignments that are not allowed in C.
4046 ERRTYPE is a string to use in error messages:
4047 "assignment", "return", etc. If it is null, this is parameter passing
4048 for a function call (and different error messages are output).
4050 FUNNAME is the name of the function being called,
4051 as an IDENTIFIER_NODE, or null.
4052 PARMNUM is the number of the argument, for printing in error messages. */
4054 static tree
4055 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
4056 tree type, rhs;
4057 const char *errtype;
4058 tree fundecl, funname;
4059 int parmnum;
4061 register enum tree_code codel = TREE_CODE (type);
4062 register tree rhstype;
4063 register enum tree_code coder;
4065 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4066 /* Do not use STRIP_NOPS here. We do not want an enumerator
4067 whose value is 0 to count as a null pointer constant. */
4068 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4069 rhs = TREE_OPERAND (rhs, 0);
4071 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4072 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4073 rhs = default_conversion (rhs);
4074 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4075 rhs = decl_constant_value_for_broken_optimization (rhs);
4077 rhstype = TREE_TYPE (rhs);
4078 coder = TREE_CODE (rhstype);
4080 if (coder == ERROR_MARK)
4081 return error_mark_node;
4083 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4085 overflow_warning (rhs);
4086 /* Check for Objective-C protocols. This will issue a warning if
4087 there are protocol violations. No need to use the return value. */
4088 maybe_objc_comptypes (type, rhstype, 0);
4089 return rhs;
4092 if (coder == VOID_TYPE)
4094 error ("void value not ignored as it ought to be");
4095 return error_mark_node;
4097 /* A type converts to a reference to it.
4098 This code doesn't fully support references, it's just for the
4099 special case of va_start and va_copy. */
4100 if (codel == REFERENCE_TYPE
4101 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4103 if (mark_addressable (rhs) == 0)
4104 return error_mark_node;
4105 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4107 /* We already know that these two types are compatible, but they
4108 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4109 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4110 likely to be va_list, a typedef to __builtin_va_list, which
4111 is different enough that it will cause problems later. */
4112 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4113 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4115 rhs = build1 (NOP_EXPR, type, rhs);
4116 return rhs;
4118 /* Arithmetic types all interconvert, and enum is treated like int. */
4119 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4120 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4121 || codel == BOOLEAN_TYPE)
4122 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4123 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4124 || coder == BOOLEAN_TYPE))
4125 return convert_and_check (type, rhs);
4127 /* Conversion to a transparent union from its member types.
4128 This applies only to function arguments. */
4129 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4131 tree memb_types;
4132 tree marginal_memb_type = 0;
4134 for (memb_types = TYPE_FIELDS (type); memb_types;
4135 memb_types = TREE_CHAIN (memb_types))
4137 tree memb_type = TREE_TYPE (memb_types);
4139 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4140 TYPE_MAIN_VARIANT (rhstype)))
4141 break;
4143 if (TREE_CODE (memb_type) != POINTER_TYPE)
4144 continue;
4146 if (coder == POINTER_TYPE)
4148 register tree ttl = TREE_TYPE (memb_type);
4149 register tree ttr = TREE_TYPE (rhstype);
4151 /* Any non-function converts to a [const][volatile] void *
4152 and vice versa; otherwise, targets must be the same.
4153 Meanwhile, the lhs target must have all the qualifiers of
4154 the rhs. */
4155 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4156 || comp_target_types (memb_type, rhstype))
4158 /* If this type won't generate any warnings, use it. */
4159 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4160 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4161 && TREE_CODE (ttl) == FUNCTION_TYPE)
4162 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4163 == TYPE_QUALS (ttr))
4164 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4165 == TYPE_QUALS (ttl))))
4166 break;
4168 /* Keep looking for a better type, but remember this one. */
4169 if (! marginal_memb_type)
4170 marginal_memb_type = memb_type;
4174 /* Can convert integer zero to any pointer type. */
4175 if (integer_zerop (rhs)
4176 || (TREE_CODE (rhs) == NOP_EXPR
4177 && integer_zerop (TREE_OPERAND (rhs, 0))))
4179 rhs = null_pointer_node;
4180 break;
4184 if (memb_types || marginal_memb_type)
4186 if (! memb_types)
4188 /* We have only a marginally acceptable member type;
4189 it needs a warning. */
4190 register tree ttl = TREE_TYPE (marginal_memb_type);
4191 register tree ttr = TREE_TYPE (rhstype);
4193 /* Const and volatile mean something different for function
4194 types, so the usual warnings are not appropriate. */
4195 if (TREE_CODE (ttr) == FUNCTION_TYPE
4196 && TREE_CODE (ttl) == FUNCTION_TYPE)
4198 /* Because const and volatile on functions are
4199 restrictions that say the function will not do
4200 certain things, it is okay to use a const or volatile
4201 function where an ordinary one is wanted, but not
4202 vice-versa. */
4203 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4204 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4205 errtype, funname, parmnum);
4207 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4208 warn_for_assignment ("%s discards qualifiers from pointer target type",
4209 errtype, funname,
4210 parmnum);
4213 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4214 pedwarn ("ISO C prohibits argument conversion to union type");
4216 return build1 (NOP_EXPR, type, rhs);
4220 /* Conversions among pointers */
4221 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4222 && (coder == POINTER_TYPE || coder == REFERENCE_TYPE))
4224 register tree ttl = TREE_TYPE (type);
4225 register tree ttr = TREE_TYPE (rhstype);
4227 /* Any non-function converts to a [const][volatile] void *
4228 and vice versa; otherwise, targets must be the same.
4229 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4230 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4231 || comp_target_types (type, rhstype)
4232 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4233 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4235 if (pedantic
4236 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4238 (VOID_TYPE_P (ttr)
4239 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4240 which are not ANSI null ptr constants. */
4241 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4242 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4243 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
4244 errtype, funname, parmnum);
4245 /* Const and volatile mean something different for function types,
4246 so the usual warnings are not appropriate. */
4247 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4248 && TREE_CODE (ttl) != FUNCTION_TYPE)
4250 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4251 warn_for_assignment ("%s discards qualifiers from pointer target type",
4252 errtype, funname, parmnum);
4253 /* If this is not a case of ignoring a mismatch in signedness,
4254 no warning. */
4255 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4256 || comp_target_types (type, rhstype))
4258 /* If there is a mismatch, do warn. */
4259 else if (pedantic)
4260 warn_for_assignment ("pointer targets in %s differ in signedness",
4261 errtype, funname, parmnum);
4263 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4264 && TREE_CODE (ttr) == FUNCTION_TYPE)
4266 /* Because const and volatile on functions are restrictions
4267 that say the function will not do certain things,
4268 it is okay to use a const or volatile function
4269 where an ordinary one is wanted, but not vice-versa. */
4270 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4271 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4272 errtype, funname, parmnum);
4275 else
4276 warn_for_assignment ("%s from incompatible pointer type",
4277 errtype, funname, parmnum);
4278 return convert (type, rhs);
4280 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4282 /* An explicit constant 0 can convert to a pointer,
4283 or one that results from arithmetic, even including
4284 a cast to integer type. */
4285 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4287 ! (TREE_CODE (rhs) == NOP_EXPR
4288 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4289 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4290 && integer_zerop (TREE_OPERAND (rhs, 0))))
4292 warn_for_assignment ("%s makes pointer from integer without a cast",
4293 errtype, funname, parmnum);
4294 return convert (type, rhs);
4296 return null_pointer_node;
4298 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4300 warn_for_assignment ("%s makes integer from pointer without a cast",
4301 errtype, funname, parmnum);
4302 return convert (type, rhs);
4304 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4305 return convert (type, rhs);
4307 if (!errtype)
4309 if (funname)
4311 tree selector = maybe_building_objc_message_expr ();
4313 if (selector && parmnum > 2)
4314 error ("incompatible type for argument %d of `%s'",
4315 parmnum - 2, IDENTIFIER_POINTER (selector));
4316 else
4317 error ("incompatible type for argument %d of `%s'",
4318 parmnum, IDENTIFIER_POINTER (funname));
4320 else
4321 error ("incompatible type for argument %d of indirect function call",
4322 parmnum);
4324 else
4325 error ("incompatible types in %s", errtype);
4327 return error_mark_node;
4330 /* Print a warning using MSGID.
4331 It gets OPNAME as its one parameter.
4332 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4333 FUNCTION and ARGNUM are handled specially if we are building an
4334 Objective-C selector. */
4336 static void
4337 warn_for_assignment (msgid, opname, function, argnum)
4338 const char *msgid;
4339 const char *opname;
4340 tree function;
4341 int argnum;
4343 if (opname == 0)
4345 tree selector = maybe_building_objc_message_expr ();
4346 char * new_opname;
4348 if (selector && argnum > 2)
4350 function = selector;
4351 argnum -= 2;
4353 if (function)
4355 /* Function name is known; supply it. */
4356 const char *argstring = _("passing arg %d of `%s'");
4357 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4358 + strlen (argstring) + 1 + 25
4359 /*%d*/ + 1);
4360 sprintf (new_opname, argstring, argnum,
4361 IDENTIFIER_POINTER (function));
4363 else
4365 /* Function name unknown (call through ptr); just give arg number.*/
4366 const char *argnofun = _("passing arg %d of pointer to function");
4367 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4368 sprintf (new_opname, argnofun, argnum);
4370 opname = new_opname;
4372 pedwarn (msgid, opname);
4375 /* If VALUE is a compound expr all of whose expressions are constant, then
4376 return its value. Otherwise, return error_mark_node.
4378 This is for handling COMPOUND_EXPRs as initializer elements
4379 which is allowed with a warning when -pedantic is specified. */
4381 static tree
4382 valid_compound_expr_initializer (value, endtype)
4383 tree value;
4384 tree endtype;
4386 if (TREE_CODE (value) == COMPOUND_EXPR)
4388 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4389 == error_mark_node)
4390 return error_mark_node;
4391 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4392 endtype);
4394 else if (! TREE_CONSTANT (value)
4395 && ! initializer_constant_valid_p (value, endtype))
4396 return error_mark_node;
4397 else
4398 return value;
4401 /* Perform appropriate conversions on the initial value of a variable,
4402 store it in the declaration DECL,
4403 and print any error messages that are appropriate.
4404 If the init is invalid, store an ERROR_MARK. */
4406 void
4407 store_init_value (decl, init)
4408 tree decl, init;
4410 register tree value, type;
4412 /* If variable's type was invalidly declared, just ignore it. */
4414 type = TREE_TYPE (decl);
4415 if (TREE_CODE (type) == ERROR_MARK)
4416 return;
4418 /* Digest the specified initializer into an expression. */
4420 value = digest_init (type, init, TREE_STATIC (decl),
4421 TREE_STATIC (decl) || (pedantic && !flag_isoc99));
4423 /* Store the expression if valid; else report error. */
4425 #if 0
4426 /* Note that this is the only place we can detect the error
4427 in a case such as struct foo bar = (struct foo) { x, y };
4428 where there is one initial value which is a constructor expression. */
4429 if (value == error_mark_node)
4431 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4433 error ("initializer for static variable is not constant");
4434 value = error_mark_node;
4436 else if (TREE_STATIC (decl)
4437 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4439 error ("initializer for static variable uses complicated arithmetic");
4440 value = error_mark_node;
4442 else
4444 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4446 if (! TREE_CONSTANT (value))
4447 pedwarn ("aggregate initializer is not constant");
4448 else if (! TREE_STATIC (value))
4449 pedwarn ("aggregate initializer uses complicated arithmetic");
4452 #endif
4454 if (warn_traditional && !in_system_header
4455 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
4456 warning ("traditional C rejects automatic aggregate initialization");
4458 DECL_INITIAL (decl) = value;
4460 /* ANSI wants warnings about out-of-range constant initializers. */
4461 STRIP_TYPE_NOPS (value);
4462 constant_expression_warning (value);
4465 /* Methods for storing and printing names for error messages. */
4467 /* Implement a spelling stack that allows components of a name to be pushed
4468 and popped. Each element on the stack is this structure. */
4470 struct spelling
4472 int kind;
4473 union
4475 int i;
4476 const char *s;
4477 } u;
4480 #define SPELLING_STRING 1
4481 #define SPELLING_MEMBER 2
4482 #define SPELLING_BOUNDS 3
4484 static struct spelling *spelling; /* Next stack element (unused). */
4485 static struct spelling *spelling_base; /* Spelling stack base. */
4486 static int spelling_size; /* Size of the spelling stack. */
4488 /* Macros to save and restore the spelling stack around push_... functions.
4489 Alternative to SAVE_SPELLING_STACK. */
4491 #define SPELLING_DEPTH() (spelling - spelling_base)
4492 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4494 /* Save and restore the spelling stack around arbitrary C code. */
4496 #define SAVE_SPELLING_DEPTH(code) \
4498 int __depth = SPELLING_DEPTH (); \
4499 code; \
4500 RESTORE_SPELLING_DEPTH (__depth); \
4503 /* Push an element on the spelling stack with type KIND and assign VALUE
4504 to MEMBER. */
4506 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4508 int depth = SPELLING_DEPTH (); \
4510 if (depth >= spelling_size) \
4512 spelling_size += 10; \
4513 if (spelling_base == 0) \
4514 spelling_base \
4515 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4516 else \
4517 spelling_base \
4518 = (struct spelling *) xrealloc (spelling_base, \
4519 spelling_size * sizeof (struct spelling)); \
4520 RESTORE_SPELLING_DEPTH (depth); \
4523 spelling->kind = (KIND); \
4524 spelling->MEMBER = (VALUE); \
4525 spelling++; \
4528 /* Push STRING on the stack. Printed literally. */
4530 static void
4531 push_string (string)
4532 const char *string;
4534 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4537 /* Push a member name on the stack. Printed as '.' STRING. */
4539 static void
4540 push_member_name (decl)
4541 tree decl;
4544 const char *string
4545 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4546 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4549 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4551 static void
4552 push_array_bounds (bounds)
4553 int bounds;
4555 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4558 /* Compute the maximum size in bytes of the printed spelling. */
4560 static int
4561 spelling_length ()
4563 register int size = 0;
4564 register struct spelling *p;
4566 for (p = spelling_base; p < spelling; p++)
4568 if (p->kind == SPELLING_BOUNDS)
4569 size += 25;
4570 else
4571 size += strlen (p->u.s) + 1;
4574 return size;
4577 /* Print the spelling to BUFFER and return it. */
4579 static char *
4580 print_spelling (buffer)
4581 register char *buffer;
4583 register char *d = buffer;
4584 register struct spelling *p;
4586 for (p = spelling_base; p < spelling; p++)
4587 if (p->kind == SPELLING_BOUNDS)
4589 sprintf (d, "[%d]", p->u.i);
4590 d += strlen (d);
4592 else
4594 register const char *s;
4595 if (p->kind == SPELLING_MEMBER)
4596 *d++ = '.';
4597 for (s = p->u.s; (*d = *s++); d++)
4600 *d++ = '\0';
4601 return buffer;
4604 /* Issue an error message for a bad initializer component.
4605 MSGID identifies the message.
4606 The component name is taken from the spelling stack. */
4608 void
4609 error_init (msgid)
4610 const char *msgid;
4612 char *ofwhat;
4614 error ("%s", msgid);
4615 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4616 if (*ofwhat)
4617 error ("(near initialization for `%s')", ofwhat);
4620 /* Issue a pedantic warning for a bad initializer component.
4621 MSGID identifies the message.
4622 The component name is taken from the spelling stack. */
4624 void
4625 pedwarn_init (msgid)
4626 const char *msgid;
4628 char *ofwhat;
4630 pedwarn ("%s", msgid);
4631 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4632 if (*ofwhat)
4633 pedwarn ("(near initialization for `%s')", ofwhat);
4636 /* Issue a warning for a bad initializer component.
4637 MSGID identifies the message.
4638 The component name is taken from the spelling stack. */
4640 static void
4641 warning_init (msgid)
4642 const char *msgid;
4644 char *ofwhat;
4646 warning ("%s", msgid);
4647 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4648 if (*ofwhat)
4649 warning ("(near initialization for `%s')", ofwhat);
4652 /* Digest the parser output INIT as an initializer for type TYPE.
4653 Return a C expression of type TYPE to represent the initial value.
4655 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4656 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4657 applies only to elements of constructors. */
4659 static tree
4660 digest_init (type, init, require_constant, constructor_constant)
4661 tree type, init;
4662 int require_constant, constructor_constant;
4664 enum tree_code code = TREE_CODE (type);
4665 tree inside_init = init;
4667 if (type == error_mark_node
4668 || init == error_mark_node
4669 || TREE_TYPE (init) == error_mark_node)
4670 return error_mark_node;
4672 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4673 /* Do not use STRIP_NOPS here. We do not want an enumerator
4674 whose value is 0 to count as a null pointer constant. */
4675 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4676 inside_init = TREE_OPERAND (init, 0);
4678 inside_init = fold (inside_init);
4680 /* Initialization of an array of chars from a string constant
4681 optionally enclosed in braces. */
4683 if (code == ARRAY_TYPE)
4685 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4686 if ((typ1 == char_type_node
4687 || typ1 == signed_char_type_node
4688 || typ1 == unsigned_char_type_node
4689 || typ1 == unsigned_wchar_type_node
4690 || typ1 == signed_wchar_type_node)
4691 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4693 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4694 TYPE_MAIN_VARIANT (type)))
4695 return inside_init;
4697 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4698 != char_type_node)
4699 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4701 error_init ("char-array initialized from wide string");
4702 return error_mark_node;
4704 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4705 == char_type_node)
4706 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4708 error_init ("int-array initialized from non-wide string");
4709 return error_mark_node;
4712 TREE_TYPE (inside_init) = type;
4713 if (TYPE_DOMAIN (type) != 0
4714 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4715 /* Subtract 1 (or sizeof (wchar_t))
4716 because it's ok to ignore the terminating null char
4717 that is counted in the length of the constant. */
4718 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4719 TREE_STRING_LENGTH (inside_init)
4720 - ((TYPE_PRECISION (typ1)
4721 != TYPE_PRECISION (char_type_node))
4722 ? (TYPE_PRECISION (wchar_type_node)
4723 / BITS_PER_UNIT)
4724 : 1)))
4725 pedwarn_init ("initializer-string for array of chars is too long");
4727 return inside_init;
4731 /* Any type can be initialized
4732 from an expression of the same type, optionally with braces. */
4734 if (inside_init && TREE_TYPE (inside_init) != 0
4735 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4736 TYPE_MAIN_VARIANT (type))
4737 || (code == ARRAY_TYPE
4738 && comptypes (TREE_TYPE (inside_init), type))
4739 || (code == POINTER_TYPE
4740 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4741 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4742 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4743 TREE_TYPE (type)))))
4745 if (code == POINTER_TYPE
4746 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4747 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4748 inside_init = default_conversion (inside_init);
4749 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4750 && TREE_CODE (inside_init) != CONSTRUCTOR)
4752 error_init ("array initialized from non-constant array expression");
4753 return error_mark_node;
4756 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4757 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4759 /* Compound expressions can only occur here if -pedantic or
4760 -pedantic-errors is specified. In the later case, we always want
4761 an error. In the former case, we simply want a warning. */
4762 if (require_constant && pedantic
4763 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4765 inside_init
4766 = valid_compound_expr_initializer (inside_init,
4767 TREE_TYPE (inside_init));
4768 if (inside_init == error_mark_node)
4769 error_init ("initializer element is not constant");
4770 else
4771 pedwarn_init ("initializer element is not constant");
4772 if (flag_pedantic_errors)
4773 inside_init = error_mark_node;
4775 else if (require_constant && ! TREE_CONSTANT (inside_init))
4777 error_init ("initializer element is not constant");
4778 inside_init = error_mark_node;
4780 else if (require_constant
4781 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4782 pedwarn ("initializer element is not computable at load time");
4784 return inside_init;
4787 /* Handle scalar types, including conversions. */
4789 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4790 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4792 /* Note that convert_for_assignment calls default_conversion
4793 for arrays and functions. We must not call it in the
4794 case where inside_init is a null pointer constant. */
4795 inside_init
4796 = convert_for_assignment (type, init, _("initialization"),
4797 NULL_TREE, NULL_TREE, 0);
4799 if (require_constant && ! TREE_CONSTANT (inside_init))
4801 error_init ("initializer element is not constant");
4802 inside_init = error_mark_node;
4804 else if (require_constant
4805 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4807 error_init ("initializer element is not computable at load time");
4808 inside_init = error_mark_node;
4811 return inside_init;
4814 /* Come here only for records and arrays. */
4816 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4818 error_init ("variable-sized object may not be initialized");
4819 return error_mark_node;
4822 /* Traditionally, you can write struct foo x = 0;
4823 and it initializes the first element of x to 0. */
4824 if (flag_traditional)
4826 tree top = 0, prev = 0, otype = type;
4827 while (TREE_CODE (type) == RECORD_TYPE
4828 || TREE_CODE (type) == ARRAY_TYPE
4829 || TREE_CODE (type) == QUAL_UNION_TYPE
4830 || TREE_CODE (type) == UNION_TYPE)
4832 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4833 if (prev == 0)
4834 top = temp;
4835 else
4836 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4837 prev = temp;
4838 if (TREE_CODE (type) == ARRAY_TYPE)
4839 type = TREE_TYPE (type);
4840 else if (TYPE_FIELDS (type))
4841 type = TREE_TYPE (TYPE_FIELDS (type));
4842 else
4844 error_init ("invalid initializer");
4845 return error_mark_node;
4849 if (otype != type)
4851 TREE_OPERAND (prev, 1)
4852 = build_tree_list (NULL_TREE,
4853 digest_init (type, init, require_constant,
4854 constructor_constant));
4855 return top;
4857 else
4858 return error_mark_node;
4860 error_init ("invalid initializer");
4861 return error_mark_node;
4864 /* Handle initializers that use braces. */
4866 /* Type of object we are accumulating a constructor for.
4867 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4868 static tree constructor_type;
4870 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4871 left to fill. */
4872 static tree constructor_fields;
4874 /* For an ARRAY_TYPE, this is the specified index
4875 at which to store the next element we get. */
4876 static tree constructor_index;
4878 /* For an ARRAY_TYPE, this is the maximum index. */
4879 static tree constructor_max_index;
4881 /* For a RECORD_TYPE, this is the first field not yet written out. */
4882 static tree constructor_unfilled_fields;
4884 /* For an ARRAY_TYPE, this is the index of the first element
4885 not yet written out. */
4886 static tree constructor_unfilled_index;
4888 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4889 This is so we can generate gaps between fields, when appropriate. */
4890 static tree constructor_bit_index;
4892 /* If we are saving up the elements rather than allocating them,
4893 this is the list of elements so far (in reverse order,
4894 most recent first). */
4895 static tree constructor_elements;
4897 /* 1 if constructor should be incrementally stored into a constructor chain,
4898 0 if all the elements should be kept in AVL tree. */
4899 static int constructor_incremental;
4901 /* 1 if so far this constructor's elements are all compile-time constants. */
4902 static int constructor_constant;
4904 /* 1 if so far this constructor's elements are all valid address constants. */
4905 static int constructor_simple;
4907 /* 1 if this constructor is erroneous so far. */
4908 static int constructor_erroneous;
4910 /* 1 if have called defer_addressed_constants. */
4911 static int constructor_subconstants_deferred;
4913 /* Structure for managing pending initializer elements, organized as an
4914 AVL tree. */
4916 struct init_node
4918 struct init_node *left, *right;
4919 struct init_node *parent;
4920 int balance;
4921 tree purpose;
4922 tree value;
4925 /* Tree of pending elements at this constructor level.
4926 These are elements encountered out of order
4927 which belong at places we haven't reached yet in actually
4928 writing the output.
4929 Will never hold tree nodes across GC runs. */
4930 static struct init_node *constructor_pending_elts;
4932 /* The SPELLING_DEPTH of this constructor. */
4933 static int constructor_depth;
4935 /* 0 if implicitly pushing constructor levels is allowed. */
4936 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4938 static int require_constant_value;
4939 static int require_constant_elements;
4941 /* DECL node for which an initializer is being read.
4942 0 means we are reading a constructor expression
4943 such as (struct foo) {...}. */
4944 static tree constructor_decl;
4946 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4947 static const char *constructor_asmspec;
4949 /* Nonzero if this is an initializer for a top-level decl. */
4950 static int constructor_top_level;
4952 /* Nesting depth of designator list. */
4953 static int designator_depth;
4955 /* Nonzero if there were diagnosed errors in this designator list. */
4956 static int designator_errorneous;
4959 /* This stack has a level for each implicit or explicit level of
4960 structuring in the initializer, including the outermost one. It
4961 saves the values of most of the variables above. */
4963 struct constructor_range_stack;
4965 struct constructor_stack
4967 struct constructor_stack *next;
4968 tree type;
4969 tree fields;
4970 tree index;
4971 tree max_index;
4972 tree unfilled_index;
4973 tree unfilled_fields;
4974 tree bit_index;
4975 tree elements;
4976 struct init_node *pending_elts;
4977 int offset;
4978 int depth;
4979 /* If nonzero, this value should replace the entire
4980 constructor at this level. */
4981 tree replacement_value;
4982 struct constructor_range_stack *range_stack;
4983 char constant;
4984 char simple;
4985 char implicit;
4986 char erroneous;
4987 char outer;
4988 char incremental;
4991 struct constructor_stack *constructor_stack;
4993 /* This stack represents designators from some range designator up to
4994 the last designator in the list. */
4996 struct constructor_range_stack
4998 struct constructor_range_stack *next, *prev;
4999 struct constructor_stack *stack;
5000 tree range_start;
5001 tree index;
5002 tree range_end;
5003 tree fields;
5006 struct constructor_range_stack *constructor_range_stack;
5008 /* This stack records separate initializers that are nested.
5009 Nested initializers can't happen in ANSI C, but GNU C allows them
5010 in cases like { ... (struct foo) { ... } ... }. */
5012 struct initializer_stack
5014 struct initializer_stack *next;
5015 tree decl;
5016 const char *asmspec;
5017 struct constructor_stack *constructor_stack;
5018 struct constructor_range_stack *constructor_range_stack;
5019 tree elements;
5020 struct spelling *spelling;
5021 struct spelling *spelling_base;
5022 int spelling_size;
5023 char top_level;
5024 char require_constant_value;
5025 char require_constant_elements;
5026 char deferred;
5029 struct initializer_stack *initializer_stack;
5031 /* Prepare to parse and output the initializer for variable DECL. */
5033 void
5034 start_init (decl, asmspec_tree, top_level)
5035 tree decl;
5036 tree asmspec_tree;
5037 int top_level;
5039 const char *locus;
5040 struct initializer_stack *p
5041 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5042 const char *asmspec = 0;
5044 if (asmspec_tree)
5045 asmspec = TREE_STRING_POINTER (asmspec_tree);
5047 p->decl = constructor_decl;
5048 p->asmspec = constructor_asmspec;
5049 p->require_constant_value = require_constant_value;
5050 p->require_constant_elements = require_constant_elements;
5051 p->constructor_stack = constructor_stack;
5052 p->constructor_range_stack = constructor_range_stack;
5053 p->elements = constructor_elements;
5054 p->spelling = spelling;
5055 p->spelling_base = spelling_base;
5056 p->spelling_size = spelling_size;
5057 p->deferred = constructor_subconstants_deferred;
5058 p->top_level = constructor_top_level;
5059 p->next = initializer_stack;
5060 initializer_stack = p;
5062 constructor_decl = decl;
5063 constructor_asmspec = asmspec;
5064 constructor_subconstants_deferred = 0;
5065 constructor_top_level = top_level;
5067 if (decl != 0)
5069 require_constant_value = TREE_STATIC (decl);
5070 require_constant_elements
5071 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5072 /* For a scalar, you can always use any value to initialize,
5073 even within braces. */
5074 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5075 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5076 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5077 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5078 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5080 else
5082 require_constant_value = 0;
5083 require_constant_elements = 0;
5084 locus = "(anonymous)";
5087 constructor_stack = 0;
5088 constructor_range_stack = 0;
5090 missing_braces_mentioned = 0;
5092 spelling_base = 0;
5093 spelling_size = 0;
5094 RESTORE_SPELLING_DEPTH (0);
5096 if (locus)
5097 push_string (locus);
5100 void
5101 finish_init ()
5103 struct initializer_stack *p = initializer_stack;
5105 /* Output subconstants (string constants, usually)
5106 that were referenced within this initializer and saved up.
5107 Must do this if and only if we called defer_addressed_constants. */
5108 if (constructor_subconstants_deferred)
5109 output_deferred_addressed_constants ();
5111 /* Free the whole constructor stack of this initializer. */
5112 while (constructor_stack)
5114 struct constructor_stack *q = constructor_stack;
5115 constructor_stack = q->next;
5116 free (q);
5119 if (constructor_range_stack)
5120 abort ();
5122 /* Pop back to the data of the outer initializer (if any). */
5123 constructor_decl = p->decl;
5124 constructor_asmspec = p->asmspec;
5125 require_constant_value = p->require_constant_value;
5126 require_constant_elements = p->require_constant_elements;
5127 constructor_stack = p->constructor_stack;
5128 constructor_range_stack = p->constructor_range_stack;
5129 constructor_elements = p->elements;
5130 spelling = p->spelling;
5131 spelling_base = p->spelling_base;
5132 spelling_size = p->spelling_size;
5133 constructor_subconstants_deferred = p->deferred;
5134 constructor_top_level = p->top_level;
5135 initializer_stack = p->next;
5136 free (p);
5139 /* Call here when we see the initializer is surrounded by braces.
5140 This is instead of a call to push_init_level;
5141 it is matched by a call to pop_init_level.
5143 TYPE is the type to initialize, for a constructor expression.
5144 For an initializer for a decl, TYPE is zero. */
5146 void
5147 really_start_incremental_init (type)
5148 tree type;
5150 struct constructor_stack *p
5151 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5153 if (type == 0)
5154 type = TREE_TYPE (constructor_decl);
5156 p->type = constructor_type;
5157 p->fields = constructor_fields;
5158 p->index = constructor_index;
5159 p->max_index = constructor_max_index;
5160 p->unfilled_index = constructor_unfilled_index;
5161 p->unfilled_fields = constructor_unfilled_fields;
5162 p->bit_index = constructor_bit_index;
5163 p->elements = constructor_elements;
5164 p->constant = constructor_constant;
5165 p->simple = constructor_simple;
5166 p->erroneous = constructor_erroneous;
5167 p->pending_elts = constructor_pending_elts;
5168 p->depth = constructor_depth;
5169 p->replacement_value = 0;
5170 p->implicit = 0;
5171 p->range_stack = 0;
5172 p->outer = 0;
5173 p->incremental = constructor_incremental;
5174 p->next = 0;
5175 constructor_stack = p;
5177 constructor_constant = 1;
5178 constructor_simple = 1;
5179 constructor_depth = SPELLING_DEPTH ();
5180 constructor_elements = 0;
5181 constructor_pending_elts = 0;
5182 constructor_type = type;
5183 constructor_incremental = 1;
5184 designator_depth = 0;
5185 designator_errorneous = 0;
5187 if (TREE_CODE (constructor_type) == RECORD_TYPE
5188 || TREE_CODE (constructor_type) == UNION_TYPE)
5190 constructor_fields = TYPE_FIELDS (constructor_type);
5191 /* Skip any nameless bit fields at the beginning. */
5192 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5193 && DECL_NAME (constructor_fields) == 0)
5194 constructor_fields = TREE_CHAIN (constructor_fields);
5196 constructor_unfilled_fields = constructor_fields;
5197 constructor_bit_index = bitsize_zero_node;
5199 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5201 if (TYPE_DOMAIN (constructor_type))
5203 constructor_max_index
5204 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5206 /* Detect non-empty initializations of zero-length arrays. */
5207 if (constructor_max_index == NULL_TREE)
5208 constructor_max_index = build_int_2 (-1, -1);
5210 constructor_index
5211 = convert (bitsizetype,
5212 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5214 else
5215 constructor_index = bitsize_zero_node;
5217 constructor_unfilled_index = constructor_index;
5219 else
5221 /* Handle the case of int x = {5}; */
5222 constructor_fields = constructor_type;
5223 constructor_unfilled_fields = constructor_type;
5227 /* Push down into a subobject, for initialization.
5228 If this is for an explicit set of braces, IMPLICIT is 0.
5229 If it is because the next element belongs at a lower level,
5230 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5232 void
5233 push_init_level (implicit)
5234 int implicit;
5236 struct constructor_stack *p;
5237 tree value = NULL_TREE;
5239 /* If we've exhausted any levels that didn't have braces,
5240 pop them now. */
5241 while (constructor_stack->implicit)
5243 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5244 || TREE_CODE (constructor_type) == UNION_TYPE)
5245 && constructor_fields == 0)
5246 process_init_element (pop_init_level (1));
5247 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5248 && tree_int_cst_lt (constructor_max_index, constructor_index))
5249 process_init_element (pop_init_level (1));
5250 else
5251 break;
5254 /* Unless this is an explicit brace, we need to preserve previous
5255 content if any. */
5256 if (implicit)
5258 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5259 || TREE_CODE (constructor_type) == UNION_TYPE)
5260 && constructor_fields)
5261 value = find_init_member (constructor_fields);
5262 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5263 value = find_init_member (constructor_index);
5266 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5267 p->type = constructor_type;
5268 p->fields = constructor_fields;
5269 p->index = constructor_index;
5270 p->max_index = constructor_max_index;
5271 p->unfilled_index = constructor_unfilled_index;
5272 p->unfilled_fields = constructor_unfilled_fields;
5273 p->bit_index = constructor_bit_index;
5274 p->elements = constructor_elements;
5275 p->constant = constructor_constant;
5276 p->simple = constructor_simple;
5277 p->erroneous = constructor_erroneous;
5278 p->pending_elts = constructor_pending_elts;
5279 p->depth = constructor_depth;
5280 p->replacement_value = 0;
5281 p->implicit = implicit;
5282 p->outer = 0;
5283 p->incremental = constructor_incremental;
5284 p->next = constructor_stack;
5285 p->range_stack = 0;
5286 constructor_stack = p;
5288 constructor_constant = 1;
5289 constructor_simple = 1;
5290 constructor_depth = SPELLING_DEPTH ();
5291 constructor_elements = 0;
5292 constructor_incremental = 1;
5293 constructor_pending_elts = 0;
5294 if (!implicit)
5296 p->range_stack = constructor_range_stack;
5297 constructor_range_stack = 0;
5298 designator_depth = 0;
5299 designator_errorneous = 0;
5302 /* Don't die if an entire brace-pair level is superfluous
5303 in the containing level. */
5304 if (constructor_type == 0)
5306 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5307 || TREE_CODE (constructor_type) == UNION_TYPE)
5309 /* Don't die if there are extra init elts at the end. */
5310 if (constructor_fields == 0)
5311 constructor_type = 0;
5312 else
5314 constructor_type = TREE_TYPE (constructor_fields);
5315 push_member_name (constructor_fields);
5316 constructor_depth++;
5319 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5321 constructor_type = TREE_TYPE (constructor_type);
5322 push_array_bounds (tree_low_cst (constructor_index, 0));
5323 constructor_depth++;
5326 if (constructor_type == 0)
5328 error_init ("extra brace group at end of initializer");
5329 constructor_fields = 0;
5330 constructor_unfilled_fields = 0;
5331 return;
5334 if (value && TREE_CODE (value) == CONSTRUCTOR)
5336 constructor_constant = TREE_CONSTANT (value);
5337 constructor_simple = TREE_STATIC (value);
5338 constructor_elements = TREE_OPERAND (value, 1);
5339 if (constructor_elements
5340 && (TREE_CODE (constructor_type) == RECORD_TYPE
5341 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5342 set_nonincremental_init ();
5345 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5347 missing_braces_mentioned = 1;
5348 warning_init ("missing braces around initializer");
5351 if (TREE_CODE (constructor_type) == RECORD_TYPE
5352 || TREE_CODE (constructor_type) == UNION_TYPE)
5354 constructor_fields = TYPE_FIELDS (constructor_type);
5355 /* Skip any nameless bit fields at the beginning. */
5356 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5357 && DECL_NAME (constructor_fields) == 0)
5358 constructor_fields = TREE_CHAIN (constructor_fields);
5360 constructor_unfilled_fields = constructor_fields;
5361 constructor_bit_index = bitsize_zero_node;
5363 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5365 if (TYPE_DOMAIN (constructor_type))
5367 constructor_max_index
5368 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5369 constructor_index
5370 = convert (bitsizetype,
5371 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5373 /* ??? For GCC 3.1, remove special case initialization of
5374 zero-length array members from pop_init_level and set
5375 constructor_max_index such that we get the normal
5376 "excess elements" warning. */
5378 else
5379 constructor_index = bitsize_zero_node;
5381 constructor_unfilled_index = constructor_index;
5382 if (value && TREE_CODE (value) == STRING_CST)
5384 /* We need to split the char/wchar array into individual
5385 characters, so that we don't have to special case it
5386 everywhere. */
5387 set_nonincremental_init_from_string (value);
5390 else
5392 warning_init ("braces around scalar initializer");
5393 constructor_fields = constructor_type;
5394 constructor_unfilled_fields = constructor_type;
5398 /* At the end of an implicit or explicit brace level,
5399 finish up that level of constructor.
5400 If we were outputting the elements as they are read, return 0
5401 from inner levels (process_init_element ignores that),
5402 but return error_mark_node from the outermost level
5403 (that's what we want to put in DECL_INITIAL).
5404 Otherwise, return a CONSTRUCTOR expression. */
5406 tree
5407 pop_init_level (implicit)
5408 int implicit;
5410 struct constructor_stack *p;
5411 HOST_WIDE_INT size = 0;
5412 tree constructor = 0;
5414 if (implicit == 0)
5416 /* When we come to an explicit close brace,
5417 pop any inner levels that didn't have explicit braces. */
5418 while (constructor_stack->implicit)
5419 process_init_element (pop_init_level (1));
5421 if (constructor_range_stack)
5422 abort ();
5425 p = constructor_stack;
5427 if (constructor_type != 0)
5428 size = int_size_in_bytes (constructor_type);
5430 /* Error for initializing a flexible array member, or a zero-length
5431 array member in an inappropriate context. */
5432 if (constructor_type && constructor_fields
5433 && TREE_CODE (constructor_type) == ARRAY_TYPE
5434 && TYPE_DOMAIN (constructor_type)
5435 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5437 /* Silently discard empty initializations. The parser will
5438 already have pedwarned for empty brackets. */
5439 if (integer_zerop (constructor_unfilled_index))
5440 constructor_type = NULL_TREE;
5441 else if (! TYPE_SIZE (constructor_type))
5443 if (constructor_depth > 2)
5444 error_init ("initialization of flexible array member in a nested context");
5445 else if (pedantic)
5446 pedwarn_init ("initialization of a flexible array member");
5448 /* We have already issued an error message for the existance
5449 of a flexible array member not at the end of the structure.
5450 Discard the initializer so that we do not abort later. */
5451 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5452 constructor_type = NULL_TREE;
5454 else
5456 warning_init ("deprecated initialization of zero-length array");
5458 /* We must be initializing the last member of a top-level struct. */
5459 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5461 error_init ("initialization of zero-length array before end of structure");
5462 /* Discard the initializer so that we do not abort later. */
5463 constructor_type = NULL_TREE;
5465 else if (constructor_depth > 2)
5466 error_init ("initialization of zero-length array inside a nested context");
5470 /* Warn when some struct elements are implicitly initialized to zero. */
5471 if (extra_warnings
5472 && constructor_type
5473 && TREE_CODE (constructor_type) == RECORD_TYPE
5474 && constructor_unfilled_fields)
5476 /* Do not warn for flexible array members or zero-length arrays. */
5477 while (constructor_unfilled_fields
5478 && (! DECL_SIZE (constructor_unfilled_fields)
5479 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5480 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5482 if (constructor_unfilled_fields)
5484 push_member_name (constructor_unfilled_fields);
5485 warning_init ("missing initializer");
5486 RESTORE_SPELLING_DEPTH (constructor_depth);
5490 /* Now output all pending elements. */
5491 constructor_incremental = 1;
5492 output_pending_init_elements (1);
5494 /* Pad out the end of the structure. */
5495 if (p->replacement_value)
5496 /* If this closes a superfluous brace pair,
5497 just pass out the element between them. */
5498 constructor = p->replacement_value;
5499 else if (constructor_type == 0)
5501 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5502 && TREE_CODE (constructor_type) != UNION_TYPE
5503 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5505 /* A nonincremental scalar initializer--just return
5506 the element, after verifying there is just one. */
5507 if (constructor_elements == 0)
5509 if (!constructor_erroneous)
5510 error_init ("empty scalar initializer");
5511 constructor = error_mark_node;
5513 else if (TREE_CHAIN (constructor_elements) != 0)
5515 error_init ("extra elements in scalar initializer");
5516 constructor = TREE_VALUE (constructor_elements);
5518 else
5519 constructor = TREE_VALUE (constructor_elements);
5521 else
5523 if (constructor_erroneous)
5524 constructor = error_mark_node;
5525 else
5527 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5528 nreverse (constructor_elements));
5529 if (constructor_constant)
5530 TREE_CONSTANT (constructor) = 1;
5531 if (constructor_constant && constructor_simple)
5532 TREE_STATIC (constructor) = 1;
5536 constructor_type = p->type;
5537 constructor_fields = p->fields;
5538 constructor_index = p->index;
5539 constructor_max_index = p->max_index;
5540 constructor_unfilled_index = p->unfilled_index;
5541 constructor_unfilled_fields = p->unfilled_fields;
5542 constructor_bit_index = p->bit_index;
5543 constructor_elements = p->elements;
5544 constructor_constant = p->constant;
5545 constructor_simple = p->simple;
5546 constructor_erroneous = p->erroneous;
5547 constructor_incremental = p->incremental;
5548 constructor_pending_elts = p->pending_elts;
5549 constructor_depth = p->depth;
5550 if (!p->implicit)
5551 constructor_range_stack = p->range_stack;
5552 RESTORE_SPELLING_DEPTH (constructor_depth);
5554 constructor_stack = p->next;
5555 free (p);
5557 if (constructor == 0)
5559 if (constructor_stack == 0)
5560 return error_mark_node;
5561 return NULL_TREE;
5563 return constructor;
5566 /* Common handling for both array range and field name designators.
5567 ARRAY argument is non-zero for array ranges. Returns zero for success. */
5569 static int
5570 set_designator (array)
5571 int array;
5573 tree subtype;
5574 enum tree_code subcode;
5576 /* Don't die if an entire brace-pair level is superfluous
5577 in the containing level. */
5578 if (constructor_type == 0)
5579 return 1;
5581 /* If there were errors in this designator list already, bail out silently. */
5582 if (designator_errorneous)
5583 return 1;
5585 if (!designator_depth)
5587 if (constructor_range_stack)
5588 abort ();
5590 /* Designator list starts at the level of closest explicit
5591 braces. */
5592 while (constructor_stack->implicit)
5593 process_init_element (pop_init_level (1));
5594 return 0;
5597 if (constructor_no_implicit)
5599 error_init ("initialization designators may not nest");
5600 return 1;
5603 if (TREE_CODE (constructor_type) == RECORD_TYPE
5604 || TREE_CODE (constructor_type) == UNION_TYPE)
5606 subtype = TREE_TYPE (constructor_fields);
5607 if (subtype != error_mark_node)
5608 subtype = TYPE_MAIN_VARIANT (subtype);
5610 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5612 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5614 else
5615 abort ();
5617 subcode = TREE_CODE (subtype);
5618 if (array && subcode != ARRAY_TYPE)
5620 error_init ("array index in non-array initializer");
5621 return 1;
5623 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5625 error_init ("field name not in record or union initializer");
5626 return 1;
5629 push_init_level (2);
5630 return 0;
5633 /* If there are range designators in designator list, push a new designator
5634 to constructor_range_stack. RANGE_END is end of such stack range or
5635 NULL_TREE if there is no range designator at this level. */
5637 static void
5638 push_range_stack (range_end)
5639 tree range_end;
5641 struct constructor_range_stack *p;
5643 p = (struct constructor_range_stack *)
5644 ggc_alloc (sizeof (struct constructor_range_stack));
5645 p->prev = constructor_range_stack;
5646 p->next = 0;
5647 p->fields = constructor_fields;
5648 p->range_start = constructor_index;
5649 p->index = constructor_index;
5650 p->stack = constructor_stack;
5651 p->range_end = range_end;
5652 if (constructor_range_stack)
5653 constructor_range_stack->next = p;
5654 constructor_range_stack = p;
5657 /* Within an array initializer, specify the next index to be initialized.
5658 FIRST is that index. If LAST is nonzero, then initialize a range
5659 of indices, running from FIRST through LAST. */
5661 void
5662 set_init_index (first, last)
5663 tree first, last;
5665 if (set_designator (1))
5666 return;
5668 designator_errorneous = 1;
5670 while ((TREE_CODE (first) == NOP_EXPR
5671 || TREE_CODE (first) == CONVERT_EXPR
5672 || TREE_CODE (first) == NON_LVALUE_EXPR)
5673 && (TYPE_MODE (TREE_TYPE (first))
5674 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5675 first = TREE_OPERAND (first, 0);
5677 if (last)
5678 while ((TREE_CODE (last) == NOP_EXPR
5679 || TREE_CODE (last) == CONVERT_EXPR
5680 || TREE_CODE (last) == NON_LVALUE_EXPR)
5681 && (TYPE_MODE (TREE_TYPE (last))
5682 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5683 last = TREE_OPERAND (last, 0);
5685 if (TREE_CODE (first) != INTEGER_CST)
5686 error_init ("nonconstant array index in initializer");
5687 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5688 error_init ("nonconstant array index in initializer");
5689 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5690 error_init ("array index in non-array initializer");
5691 else if (constructor_max_index
5692 && tree_int_cst_lt (constructor_max_index, first))
5693 error_init ("array index in initializer exceeds array bounds");
5694 else
5696 constructor_index = convert (bitsizetype, first);
5698 if (last)
5700 if (tree_int_cst_equal (first, last))
5701 last = 0;
5702 else if (tree_int_cst_lt (last, first))
5704 error_init ("empty index range in initializer");
5705 last = 0;
5707 else
5709 last = convert (bitsizetype, last);
5710 if (constructor_max_index != 0
5711 && tree_int_cst_lt (constructor_max_index, last))
5713 error_init ("array index range in initializer exceeds array bounds");
5714 last = 0;
5719 designator_depth++;
5720 designator_errorneous = 0;
5721 if (constructor_range_stack || last)
5722 push_range_stack (last);
5726 /* Within a struct initializer, specify the next field to be initialized. */
5728 void
5729 set_init_label (fieldname)
5730 tree fieldname;
5732 tree tail;
5734 if (set_designator (0))
5735 return;
5737 designator_errorneous = 1;
5739 if (TREE_CODE (constructor_type) != RECORD_TYPE
5740 && TREE_CODE (constructor_type) != UNION_TYPE)
5742 error_init ("field name not in record or union initializer");
5743 return;
5746 for (tail = TYPE_FIELDS (constructor_type); tail;
5747 tail = TREE_CHAIN (tail))
5749 if (DECL_NAME (tail) == fieldname)
5750 break;
5753 if (tail == 0)
5754 error ("unknown field `%s' specified in initializer",
5755 IDENTIFIER_POINTER (fieldname));
5756 else
5758 constructor_fields = tail;
5759 designator_depth++;
5760 designator_errorneous = 0;
5761 if (constructor_range_stack)
5762 push_range_stack (NULL_TREE);
5766 /* Add a new initializer to the tree of pending initializers. PURPOSE
5767 indentifies the initializer, either array index or field in a structure.
5768 VALUE is the value of that index or field. */
5770 static void
5771 add_pending_init (purpose, value)
5772 tree purpose, value;
5774 struct init_node *p, **q, *r;
5776 q = &constructor_pending_elts;
5777 p = 0;
5779 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5781 while (*q != 0)
5783 p = *q;
5784 if (tree_int_cst_lt (purpose, p->purpose))
5785 q = &p->left;
5786 else if (tree_int_cst_lt (p->purpose, purpose))
5787 q = &p->right;
5788 else
5790 if (TREE_SIDE_EFFECTS (p->value))
5791 warning_init ("initialized field with side-effects overwritten");
5792 p->value = value;
5793 return;
5797 else
5799 tree bitpos;
5801 bitpos = bit_position (purpose);
5802 while (*q != NULL)
5804 p = *q;
5805 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5806 q = &p->left;
5807 else if (p->purpose != purpose)
5808 q = &p->right;
5809 else
5811 if (TREE_SIDE_EFFECTS (p->value))
5812 warning_init ("initialized field with side-effects overwritten");
5813 p->value = value;
5814 return;
5819 r = (struct init_node *) ggc_alloc (sizeof (struct init_node));
5820 r->purpose = purpose;
5821 r->value = value;
5823 *q = r;
5824 r->parent = p;
5825 r->left = 0;
5826 r->right = 0;
5827 r->balance = 0;
5829 while (p)
5831 struct init_node *s;
5833 if (r == p->left)
5835 if (p->balance == 0)
5836 p->balance = -1;
5837 else if (p->balance < 0)
5839 if (r->balance < 0)
5841 /* L rotation. */
5842 p->left = r->right;
5843 if (p->left)
5844 p->left->parent = p;
5845 r->right = p;
5847 p->balance = 0;
5848 r->balance = 0;
5850 s = p->parent;
5851 p->parent = r;
5852 r->parent = s;
5853 if (s)
5855 if (s->left == p)
5856 s->left = r;
5857 else
5858 s->right = r;
5860 else
5861 constructor_pending_elts = r;
5863 else
5865 /* LR rotation. */
5866 struct init_node *t = r->right;
5868 r->right = t->left;
5869 if (r->right)
5870 r->right->parent = r;
5871 t->left = r;
5873 p->left = t->right;
5874 if (p->left)
5875 p->left->parent = p;
5876 t->right = p;
5878 p->balance = t->balance < 0;
5879 r->balance = -(t->balance > 0);
5880 t->balance = 0;
5882 s = p->parent;
5883 p->parent = t;
5884 r->parent = t;
5885 t->parent = s;
5886 if (s)
5888 if (s->left == p)
5889 s->left = t;
5890 else
5891 s->right = t;
5893 else
5894 constructor_pending_elts = t;
5896 break;
5898 else
5900 /* p->balance == +1; growth of left side balances the node. */
5901 p->balance = 0;
5902 break;
5905 else /* r == p->right */
5907 if (p->balance == 0)
5908 /* Growth propagation from right side. */
5909 p->balance++;
5910 else if (p->balance > 0)
5912 if (r->balance > 0)
5914 /* R rotation. */
5915 p->right = r->left;
5916 if (p->right)
5917 p->right->parent = p;
5918 r->left = p;
5920 p->balance = 0;
5921 r->balance = 0;
5923 s = p->parent;
5924 p->parent = r;
5925 r->parent = s;
5926 if (s)
5928 if (s->left == p)
5929 s->left = r;
5930 else
5931 s->right = r;
5933 else
5934 constructor_pending_elts = r;
5936 else /* r->balance == -1 */
5938 /* RL rotation */
5939 struct init_node *t = r->left;
5941 r->left = t->right;
5942 if (r->left)
5943 r->left->parent = r;
5944 t->right = r;
5946 p->right = t->left;
5947 if (p->right)
5948 p->right->parent = p;
5949 t->left = p;
5951 r->balance = (t->balance < 0);
5952 p->balance = -(t->balance > 0);
5953 t->balance = 0;
5955 s = p->parent;
5956 p->parent = t;
5957 r->parent = t;
5958 t->parent = s;
5959 if (s)
5961 if (s->left == p)
5962 s->left = t;
5963 else
5964 s->right = t;
5966 else
5967 constructor_pending_elts = t;
5969 break;
5971 else
5973 /* p->balance == -1; growth of right side balances the node. */
5974 p->balance = 0;
5975 break;
5979 r = p;
5980 p = p->parent;
5984 /* Build AVL tree from a sorted chain. */
5986 static void
5987 set_nonincremental_init ()
5989 tree chain;
5991 if (TREE_CODE (constructor_type) != RECORD_TYPE
5992 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5993 return;
5995 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5996 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5997 constructor_elements = 0;
5998 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6000 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6001 /* Skip any nameless bit fields at the beginning. */
6002 while (constructor_unfilled_fields != 0
6003 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6004 && DECL_NAME (constructor_unfilled_fields) == 0)
6005 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6008 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6010 if (TYPE_DOMAIN (constructor_type))
6011 constructor_unfilled_index
6012 = convert (bitsizetype,
6013 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6014 else
6015 constructor_unfilled_index = bitsize_zero_node;
6017 constructor_incremental = 0;
6020 /* Build AVL tree from a string constant. */
6022 static void
6023 set_nonincremental_init_from_string (str)
6024 tree str;
6026 tree value, purpose, type;
6027 HOST_WIDE_INT val[2];
6028 const char *p, *end;
6029 int byte, wchar_bytes, charwidth, bitpos;
6031 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6032 abort ();
6034 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6035 == TYPE_PRECISION (char_type_node))
6036 wchar_bytes = 1;
6037 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6038 == TYPE_PRECISION (wchar_type_node))
6039 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6040 else
6041 abort ();
6043 charwidth = TYPE_PRECISION (char_type_node);
6044 type = TREE_TYPE (constructor_type);
6045 p = TREE_STRING_POINTER (str);
6046 end = p + TREE_STRING_LENGTH (str);
6048 for (purpose = bitsize_zero_node;
6049 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6050 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6052 if (wchar_bytes == 1)
6054 val[1] = (unsigned char) *p++;
6055 val[0] = 0;
6057 else
6059 val[0] = 0;
6060 val[1] = 0;
6061 for (byte = 0; byte < wchar_bytes; byte++)
6063 if (BYTES_BIG_ENDIAN)
6064 bitpos = (wchar_bytes - byte - 1) * charwidth;
6065 else
6066 bitpos = byte * charwidth;
6067 val[bitpos < HOST_BITS_PER_WIDE_INT]
6068 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6069 << (bitpos % HOST_BITS_PER_WIDE_INT);
6073 if (!TREE_UNSIGNED (type))
6075 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6076 if (bitpos < HOST_BITS_PER_WIDE_INT)
6078 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6080 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6081 val[0] = -1;
6084 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6086 if (val[1] < 0)
6087 val[0] = -1;
6089 else if (val[0] & (((HOST_WIDE_INT) 1)
6090 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6091 val[0] |= ((HOST_WIDE_INT) -1)
6092 << (bitpos - HOST_BITS_PER_WIDE_INT);
6095 value = build_int_2 (val[1], val[0]);
6096 TREE_TYPE (value) = type;
6097 add_pending_init (purpose, value);
6100 constructor_incremental = 0;
6103 /* Return value of FIELD in pending initializer or zero if the field was
6104 not initialized yet. */
6106 static tree
6107 find_init_member (field)
6108 tree field;
6110 struct init_node *p;
6112 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6114 if (constructor_incremental
6115 && tree_int_cst_lt (field, constructor_unfilled_index))
6116 set_nonincremental_init ();
6118 p = constructor_pending_elts;
6119 while (p)
6121 if (tree_int_cst_lt (field, p->purpose))
6122 p = p->left;
6123 else if (tree_int_cst_lt (p->purpose, field))
6124 p = p->right;
6125 else
6126 return p->value;
6129 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6131 tree bitpos = bit_position (field);
6133 if (constructor_incremental
6134 && (!constructor_unfilled_fields
6135 || tree_int_cst_lt (bitpos,
6136 bit_position (constructor_unfilled_fields))))
6137 set_nonincremental_init ();
6139 p = constructor_pending_elts;
6140 while (p)
6142 if (field == p->purpose)
6143 return p->value;
6144 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6145 p = p->left;
6146 else
6147 p = p->right;
6150 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6152 if (constructor_elements
6153 && TREE_PURPOSE (constructor_elements) == field)
6154 return TREE_VALUE (constructor_elements);
6156 return 0;
6159 /* "Output" the next constructor element.
6160 At top level, really output it to assembler code now.
6161 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6162 TYPE is the data type that the containing data type wants here.
6163 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6165 PENDING if non-nil means output pending elements that belong
6166 right after this element. (PENDING is normally 1;
6167 it is 0 while outputting pending elements, to avoid recursion.) */
6169 static void
6170 output_init_element (value, type, field, pending)
6171 tree value, type, field;
6172 int pending;
6174 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6175 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6176 && !(TREE_CODE (value) == STRING_CST
6177 && TREE_CODE (type) == ARRAY_TYPE
6178 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6179 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6180 TYPE_MAIN_VARIANT (type))))
6181 value = default_conversion (value);
6183 if (value == error_mark_node)
6184 constructor_erroneous = 1;
6185 else if (!TREE_CONSTANT (value))
6186 constructor_constant = 0;
6187 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6188 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6189 || TREE_CODE (constructor_type) == UNION_TYPE)
6190 && DECL_C_BIT_FIELD (field)
6191 && TREE_CODE (value) != INTEGER_CST))
6192 constructor_simple = 0;
6194 if (require_constant_value && ! TREE_CONSTANT (value))
6196 error_init ("initializer element is not constant");
6197 value = error_mark_node;
6199 else if (require_constant_elements
6200 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6201 pedwarn ("initializer element is not computable at load time");
6203 /* If this field is empty (and not at the end of structure),
6204 don't do anything other than checking the initializer. */
6205 if (field
6206 && (TREE_TYPE (field) == error_mark_node
6207 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6208 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6209 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6210 || TREE_CHAIN (field)))))
6211 return;
6213 if (value == error_mark_node)
6215 constructor_erroneous = 1;
6216 return;
6219 /* If this element doesn't come next in sequence,
6220 put it on constructor_pending_elts. */
6221 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6222 && (!constructor_incremental
6223 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6225 if (constructor_incremental
6226 && tree_int_cst_lt (field, constructor_unfilled_index))
6227 set_nonincremental_init ();
6229 add_pending_init (field,
6230 digest_init (type, value, require_constant_value,
6231 require_constant_elements));
6232 return;
6234 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6235 && (!constructor_incremental
6236 || field != constructor_unfilled_fields))
6238 /* We do this for records but not for unions. In a union,
6239 no matter which field is specified, it can be initialized
6240 right away since it starts at the beginning of the union. */
6241 if (constructor_incremental)
6243 if (!constructor_unfilled_fields)
6244 set_nonincremental_init ();
6245 else
6247 tree bitpos, unfillpos;
6249 bitpos = bit_position (field);
6250 unfillpos = bit_position (constructor_unfilled_fields);
6252 if (tree_int_cst_lt (bitpos, unfillpos))
6253 set_nonincremental_init ();
6257 add_pending_init (field,
6258 digest_init (type, value, require_constant_value,
6259 require_constant_elements));
6260 return;
6262 else if (TREE_CODE (constructor_type) == UNION_TYPE
6263 && constructor_elements)
6265 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
6266 warning_init ("initialized field with side-effects overwritten");
6268 /* We can have just one union field set. */
6269 constructor_elements = 0;
6272 /* Otherwise, output this element either to
6273 constructor_elements or to the assembler file. */
6275 if (field && TREE_CODE (field) == INTEGER_CST)
6276 field = copy_node (field);
6277 constructor_elements
6278 = tree_cons (field, digest_init (type, value,
6279 require_constant_value,
6280 require_constant_elements),
6281 constructor_elements);
6283 /* Advance the variable that indicates sequential elements output. */
6284 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6285 constructor_unfilled_index
6286 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6287 bitsize_one_node);
6288 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6290 constructor_unfilled_fields
6291 = TREE_CHAIN (constructor_unfilled_fields);
6293 /* Skip any nameless bit fields. */
6294 while (constructor_unfilled_fields != 0
6295 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6296 && DECL_NAME (constructor_unfilled_fields) == 0)
6297 constructor_unfilled_fields =
6298 TREE_CHAIN (constructor_unfilled_fields);
6300 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6301 constructor_unfilled_fields = 0;
6303 /* Now output any pending elements which have become next. */
6304 if (pending)
6305 output_pending_init_elements (0);
6308 /* Output any pending elements which have become next.
6309 As we output elements, constructor_unfilled_{fields,index}
6310 advances, which may cause other elements to become next;
6311 if so, they too are output.
6313 If ALL is 0, we return when there are
6314 no more pending elements to output now.
6316 If ALL is 1, we output space as necessary so that
6317 we can output all the pending elements. */
6319 static void
6320 output_pending_init_elements (all)
6321 int all;
6323 struct init_node *elt = constructor_pending_elts;
6324 tree next;
6326 retry:
6328 /* Look thru the whole pending tree.
6329 If we find an element that should be output now,
6330 output it. Otherwise, set NEXT to the element
6331 that comes first among those still pending. */
6333 next = 0;
6334 while (elt)
6336 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6338 if (tree_int_cst_equal (elt->purpose,
6339 constructor_unfilled_index))
6340 output_init_element (elt->value,
6341 TREE_TYPE (constructor_type),
6342 constructor_unfilled_index, 0);
6343 else if (tree_int_cst_lt (constructor_unfilled_index,
6344 elt->purpose))
6346 /* Advance to the next smaller node. */
6347 if (elt->left)
6348 elt = elt->left;
6349 else
6351 /* We have reached the smallest node bigger than the
6352 current unfilled index. Fill the space first. */
6353 next = elt->purpose;
6354 break;
6357 else
6359 /* Advance to the next bigger node. */
6360 if (elt->right)
6361 elt = elt->right;
6362 else
6364 /* We have reached the biggest node in a subtree. Find
6365 the parent of it, which is the next bigger node. */
6366 while (elt->parent && elt->parent->right == elt)
6367 elt = elt->parent;
6368 elt = elt->parent;
6369 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6370 elt->purpose))
6372 next = elt->purpose;
6373 break;
6378 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6379 || TREE_CODE (constructor_type) == UNION_TYPE)
6381 tree ctor_unfilled_bitpos, elt_bitpos;
6383 /* If the current record is complete we are done. */
6384 if (constructor_unfilled_fields == 0)
6385 break;
6387 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6388 elt_bitpos = bit_position (elt->purpose);
6389 /* We can't compare fields here because there might be empty
6390 fields in between. */
6391 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6393 constructor_unfilled_fields = elt->purpose;
6394 output_init_element (elt->value, TREE_TYPE (elt->purpose),
6395 elt->purpose, 0);
6397 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6399 /* Advance to the next smaller node. */
6400 if (elt->left)
6401 elt = elt->left;
6402 else
6404 /* We have reached the smallest node bigger than the
6405 current unfilled field. Fill the space first. */
6406 next = elt->purpose;
6407 break;
6410 else
6412 /* Advance to the next bigger node. */
6413 if (elt->right)
6414 elt = elt->right;
6415 else
6417 /* We have reached the biggest node in a subtree. Find
6418 the parent of it, which is the next bigger node. */
6419 while (elt->parent && elt->parent->right == elt)
6420 elt = elt->parent;
6421 elt = elt->parent;
6422 if (elt
6423 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6424 bit_position (elt->purpose))))
6426 next = elt->purpose;
6427 break;
6434 /* Ordinarily return, but not if we want to output all
6435 and there are elements left. */
6436 if (! (all && next != 0))
6437 return;
6439 /* If it's not incremental, just skip over the gap, so that after
6440 jumping to retry we will output the next successive element. */
6441 if (TREE_CODE (constructor_type) == RECORD_TYPE
6442 || TREE_CODE (constructor_type) == UNION_TYPE)
6443 constructor_unfilled_fields = next;
6444 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6445 constructor_unfilled_index = next;
6447 /* ELT now points to the node in the pending tree with the next
6448 initializer to output. */
6449 goto retry;
6452 /* Add one non-braced element to the current constructor level.
6453 This adjusts the current position within the constructor's type.
6454 This may also start or terminate implicit levels
6455 to handle a partly-braced initializer.
6457 Once this has found the correct level for the new element,
6458 it calls output_init_element. */
6460 void
6461 process_init_element (value)
6462 tree value;
6464 tree orig_value = value;
6465 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6467 designator_depth = 0;
6468 designator_errorneous = 0;
6470 /* Handle superfluous braces around string cst as in
6471 char x[] = {"foo"}; */
6472 if (string_flag
6473 && constructor_type
6474 && TREE_CODE (constructor_type) == ARRAY_TYPE
6475 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6476 && integer_zerop (constructor_unfilled_index))
6478 if (constructor_stack->replacement_value)
6479 error_init ("excess elements in char array initializer");
6480 constructor_stack->replacement_value = value;
6481 return;
6484 if (constructor_stack->replacement_value != 0)
6486 error_init ("excess elements in struct initializer");
6487 return;
6490 /* Ignore elements of a brace group if it is entirely superfluous
6491 and has already been diagnosed. */
6492 if (constructor_type == 0)
6493 return;
6495 /* If we've exhausted any levels that didn't have braces,
6496 pop them now. */
6497 while (constructor_stack->implicit)
6499 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6500 || TREE_CODE (constructor_type) == UNION_TYPE)
6501 && constructor_fields == 0)
6502 process_init_element (pop_init_level (1));
6503 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6504 && (constructor_max_index == 0
6505 || tree_int_cst_lt (constructor_max_index,
6506 constructor_index)))
6507 process_init_element (pop_init_level (1));
6508 else
6509 break;
6512 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6513 if (constructor_range_stack)
6514 value = save_expr (value);
6516 while (1)
6518 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6520 tree fieldtype;
6521 enum tree_code fieldcode;
6523 if (constructor_fields == 0)
6525 pedwarn_init ("excess elements in struct initializer");
6526 break;
6529 fieldtype = TREE_TYPE (constructor_fields);
6530 if (fieldtype != error_mark_node)
6531 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6532 fieldcode = TREE_CODE (fieldtype);
6534 /* Accept a string constant to initialize a subarray. */
6535 if (value != 0
6536 && fieldcode == ARRAY_TYPE
6537 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6538 && string_flag)
6539 value = orig_value;
6540 /* Otherwise, if we have come to a subaggregate,
6541 and we don't have an element of its type, push into it. */
6542 else if (value != 0 && !constructor_no_implicit
6543 && value != error_mark_node
6544 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6545 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6546 || fieldcode == UNION_TYPE))
6548 push_init_level (1);
6549 continue;
6552 if (value)
6554 push_member_name (constructor_fields);
6555 output_init_element (value, fieldtype, constructor_fields, 1);
6556 RESTORE_SPELLING_DEPTH (constructor_depth);
6558 else
6559 /* Do the bookkeeping for an element that was
6560 directly output as a constructor. */
6562 /* For a record, keep track of end position of last field. */
6563 if (DECL_SIZE (constructor_fields))
6564 constructor_bit_index
6565 = size_binop (PLUS_EXPR,
6566 bit_position (constructor_fields),
6567 DECL_SIZE (constructor_fields));
6569 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6570 /* Skip any nameless bit fields. */
6571 while (constructor_unfilled_fields != 0
6572 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6573 && DECL_NAME (constructor_unfilled_fields) == 0)
6574 constructor_unfilled_fields =
6575 TREE_CHAIN (constructor_unfilled_fields);
6578 constructor_fields = TREE_CHAIN (constructor_fields);
6579 /* Skip any nameless bit fields at the beginning. */
6580 while (constructor_fields != 0
6581 && DECL_C_BIT_FIELD (constructor_fields)
6582 && DECL_NAME (constructor_fields) == 0)
6583 constructor_fields = TREE_CHAIN (constructor_fields);
6585 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6587 tree fieldtype;
6588 enum tree_code fieldcode;
6590 if (constructor_fields == 0)
6592 pedwarn_init ("excess elements in union initializer");
6593 break;
6596 fieldtype = TREE_TYPE (constructor_fields);
6597 if (fieldtype != error_mark_node)
6598 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6599 fieldcode = TREE_CODE (fieldtype);
6601 /* Warn that traditional C rejects initialization of unions.
6602 We skip the warning if the value is zero. This is done
6603 under the assumption that the zero initializer in user
6604 code appears conditioned on e.g. __STDC__ to avoid
6605 "missing initializer" warnings and relies on default
6606 initialization to zero in the traditional C case. */
6607 if (warn_traditional && !in_system_header
6608 && !(value && (integer_zerop (value) || real_zerop (value))))
6609 warning ("traditional C rejects initialization of unions");
6611 /* Accept a string constant to initialize a subarray. */
6612 if (value != 0
6613 && fieldcode == ARRAY_TYPE
6614 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6615 && string_flag)
6616 value = orig_value;
6617 /* Otherwise, if we have come to a subaggregate,
6618 and we don't have an element of its type, push into it. */
6619 else if (value != 0 && !constructor_no_implicit
6620 && value != error_mark_node
6621 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6622 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6623 || fieldcode == UNION_TYPE))
6625 push_init_level (1);
6626 continue;
6629 if (value)
6631 push_member_name (constructor_fields);
6632 output_init_element (value, fieldtype, constructor_fields, 1);
6633 RESTORE_SPELLING_DEPTH (constructor_depth);
6635 else
6636 /* Do the bookkeeping for an element that was
6637 directly output as a constructor. */
6639 constructor_bit_index = DECL_SIZE (constructor_fields);
6640 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6643 constructor_fields = 0;
6645 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6647 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6648 enum tree_code eltcode = TREE_CODE (elttype);
6650 /* Accept a string constant to initialize a subarray. */
6651 if (value != 0
6652 && eltcode == ARRAY_TYPE
6653 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6654 && string_flag)
6655 value = orig_value;
6656 /* Otherwise, if we have come to a subaggregate,
6657 and we don't have an element of its type, push into it. */
6658 else if (value != 0 && !constructor_no_implicit
6659 && value != error_mark_node
6660 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6661 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6662 || eltcode == UNION_TYPE))
6664 push_init_level (1);
6665 continue;
6668 if (constructor_max_index != 0
6669 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6670 || integer_all_onesp (constructor_max_index)))
6672 pedwarn_init ("excess elements in array initializer");
6673 break;
6676 /* Now output the actual element. */
6677 if (value)
6679 push_array_bounds (tree_low_cst (constructor_index, 0));
6680 output_init_element (value, elttype, constructor_index, 1);
6681 RESTORE_SPELLING_DEPTH (constructor_depth);
6684 constructor_index
6685 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6687 if (! value)
6688 /* If we are doing the bookkeeping for an element that was
6689 directly output as a constructor, we must update
6690 constructor_unfilled_index. */
6691 constructor_unfilled_index = constructor_index;
6694 /* Handle the sole element allowed in a braced initializer
6695 for a scalar variable. */
6696 else if (constructor_fields == 0)
6698 pedwarn_init ("excess elements in scalar initializer");
6699 break;
6701 else
6703 if (value)
6704 output_init_element (value, constructor_type, NULL_TREE, 1);
6705 constructor_fields = 0;
6708 /* Handle range initializers either at this level or anywhere higher
6709 in the designator stack. */
6710 if (constructor_range_stack)
6712 struct constructor_range_stack *p, *range_stack;
6713 int finish = 0;
6715 range_stack = constructor_range_stack;
6716 constructor_range_stack = 0;
6717 while (constructor_stack != range_stack->stack)
6719 if (!constructor_stack->implicit)
6720 abort ();
6721 process_init_element (pop_init_level (1));
6723 for (p = range_stack;
6724 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6725 p = p->prev)
6727 if (!constructor_stack->implicit)
6728 abort ();
6729 process_init_element (pop_init_level (1));
6732 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6733 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6734 finish = 1;
6736 while (1)
6738 constructor_index = p->index;
6739 constructor_fields = p->fields;
6740 if (finish && p->range_end && p->index == p->range_start)
6742 finish = 0;
6743 p->prev = 0;
6745 p = p->next;
6746 if (!p)
6747 break;
6748 push_init_level (2);
6749 p->stack = constructor_stack;
6750 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6751 p->index = p->range_start;
6754 if (!finish)
6755 constructor_range_stack = range_stack;
6756 continue;
6759 break;
6762 constructor_range_stack = 0;
6765 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6766 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6768 tree
6769 build_asm_stmt (cv_qualifier, string, outputs, inputs, clobbers)
6770 tree cv_qualifier;
6771 tree string;
6772 tree outputs;
6773 tree inputs;
6774 tree clobbers;
6776 tree tail;
6778 if (TREE_CHAIN (string))
6779 string = combine_strings (string);
6780 if (TREE_CODE (string) != STRING_CST)
6782 error ("asm template is not a string constant");
6783 return NULL_TREE;
6786 if (cv_qualifier != NULL_TREE
6787 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6789 warning ("%s qualifier ignored on asm",
6790 IDENTIFIER_POINTER (cv_qualifier));
6791 cv_qualifier = NULL_TREE;
6794 /* We can remove output conversions that change the type,
6795 but not the mode. */
6796 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6798 tree output = TREE_VALUE (tail);
6800 STRIP_NOPS (output);
6801 TREE_VALUE (tail) = output;
6803 /* Allow conversions as LHS here. build_modify_expr as called below
6804 will do the right thing with them. */
6805 while (TREE_CODE (output) == NOP_EXPR
6806 || TREE_CODE (output) == CONVERT_EXPR
6807 || TREE_CODE (output) == FLOAT_EXPR
6808 || TREE_CODE (output) == FIX_TRUNC_EXPR
6809 || TREE_CODE (output) == FIX_FLOOR_EXPR
6810 || TREE_CODE (output) == FIX_ROUND_EXPR
6811 || TREE_CODE (output) == FIX_CEIL_EXPR)
6812 output = TREE_OPERAND (output, 0);
6814 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6817 /* Remove output conversions that change the type but not the mode. */
6818 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6820 tree output = TREE_VALUE (tail);
6821 STRIP_NOPS (output);
6822 TREE_VALUE (tail) = output;
6825 /* Perform default conversions on array and function inputs.
6826 Don't do this for other types as it would screw up operands
6827 expected to be in memory. */
6828 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6829 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6830 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6831 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6833 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6834 outputs, inputs, clobbers));
6837 /* Expand an ASM statement with operands, handling output operands
6838 that are not variables or INDIRECT_REFS by transforming such
6839 cases into cases that expand_asm_operands can handle.
6841 Arguments are same as for expand_asm_operands. */
6843 void
6844 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6845 tree string, outputs, inputs, clobbers;
6846 int vol;
6847 const char *filename;
6848 int line;
6850 int noutputs = list_length (outputs);
6851 register int i;
6852 /* o[I] is the place that output number I should be written. */
6853 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6854 register tree tail;
6856 /* Record the contents of OUTPUTS before it is modified. */
6857 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6858 o[i] = TREE_VALUE (tail);
6860 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6861 OUTPUTS some trees for where the values were actually stored. */
6862 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6864 /* Copy all the intermediate outputs into the specified outputs. */
6865 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6867 if (o[i] != TREE_VALUE (tail))
6869 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6870 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6871 free_temp_slots ();
6873 /* Restore the original value so that it's correct the next
6874 time we expand this function. */
6875 TREE_VALUE (tail) = o[i];
6877 /* Detect modification of read-only values.
6878 (Otherwise done by build_modify_expr.) */
6879 else
6881 tree type = TREE_TYPE (o[i]);
6882 if (TREE_READONLY (o[i])
6883 || TYPE_READONLY (type)
6884 || ((TREE_CODE (type) == RECORD_TYPE
6885 || TREE_CODE (type) == UNION_TYPE)
6886 && C_TYPE_FIELDS_READONLY (type)))
6887 readonly_warning (o[i], "modification by `asm'");
6891 /* Those MODIFY_EXPRs could do autoincrements. */
6892 emit_queue ();
6895 /* Expand a C `return' statement.
6896 RETVAL is the expression for what to return,
6897 or a null pointer for `return;' with no value. */
6899 tree
6900 c_expand_return (retval)
6901 tree retval;
6903 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6905 if (TREE_THIS_VOLATILE (current_function_decl))
6906 warning ("function declared `noreturn' has a `return' statement");
6908 if (!retval)
6910 current_function_returns_null = 1;
6911 if ((warn_return_type || flag_isoc99)
6912 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6913 pedwarn_c99 ("`return' with no value, in function returning non-void");
6915 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6917 current_function_returns_null = 1;
6918 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6919 pedwarn ("`return' with a value, in function returning void");
6921 else
6923 tree t = convert_for_assignment (valtype, retval, _("return"),
6924 NULL_TREE, NULL_TREE, 0);
6925 tree res = DECL_RESULT (current_function_decl);
6926 tree inner;
6928 if (t == error_mark_node)
6929 return NULL_TREE;
6931 inner = t = convert (TREE_TYPE (res), t);
6933 /* Strip any conversions, additions, and subtractions, and see if
6934 we are returning the address of a local variable. Warn if so. */
6935 while (1)
6937 switch (TREE_CODE (inner))
6939 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6940 case PLUS_EXPR:
6941 inner = TREE_OPERAND (inner, 0);
6942 continue;
6944 case MINUS_EXPR:
6945 /* If the second operand of the MINUS_EXPR has a pointer
6946 type (or is converted from it), this may be valid, so
6947 don't give a warning. */
6949 tree op1 = TREE_OPERAND (inner, 1);
6951 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6952 && (TREE_CODE (op1) == NOP_EXPR
6953 || TREE_CODE (op1) == NON_LVALUE_EXPR
6954 || TREE_CODE (op1) == CONVERT_EXPR))
6955 op1 = TREE_OPERAND (op1, 0);
6957 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6958 break;
6960 inner = TREE_OPERAND (inner, 0);
6961 continue;
6964 case ADDR_EXPR:
6965 inner = TREE_OPERAND (inner, 0);
6967 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6968 inner = TREE_OPERAND (inner, 0);
6970 if (TREE_CODE (inner) == VAR_DECL
6971 && ! DECL_EXTERNAL (inner)
6972 && ! TREE_STATIC (inner)
6973 && DECL_CONTEXT (inner) == current_function_decl)
6974 warning ("function returns address of local variable");
6975 break;
6977 default:
6978 break;
6981 break;
6984 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6985 current_function_returns_value = 1;
6988 return add_stmt (build_return_stmt (retval));
6991 struct c_switch {
6992 /* The SWITCH_STMT being built. */
6993 tree switch_stmt;
6994 /* A splay-tree mapping the low element of a case range to the high
6995 element, or NULL_TREE if there is no high element. Used to
6996 determine whether or not a new case label duplicates an old case
6997 label. We need a tree, rather than simply a hash table, because
6998 of the GNU case range extension. */
6999 splay_tree cases;
7000 /* The next node on the stack. */
7001 struct c_switch *next;
7004 /* A stack of the currently active switch statements. The innermost
7005 switch statement is on the top of the stack. There is no need to
7006 mark the stack for garbage collection because it is only active
7007 during the processing of the body of a function, and we never
7008 collect at that point. */
7010 static struct c_switch *switch_stack;
7012 /* Start a C switch statement, testing expression EXP. Return the new
7013 SWITCH_STMT. */
7015 tree
7016 c_start_case (exp)
7017 tree exp;
7019 register enum tree_code code;
7020 tree type;
7021 struct c_switch *cs;
7023 if (exp != error_mark_node)
7025 code = TREE_CODE (TREE_TYPE (exp));
7026 type = TREE_TYPE (exp);
7028 if (code != INTEGER_TYPE
7029 && code != ENUMERAL_TYPE
7030 && code != ERROR_MARK)
7032 error ("switch quantity not an integer");
7033 exp = integer_zero_node;
7035 else
7037 tree index;
7038 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7040 if (warn_traditional && !in_system_header
7041 && (type == long_integer_type_node
7042 || type == long_unsigned_type_node))
7043 warning ("`long' switch expression not converted to `int' in ISO C");
7045 exp = default_conversion (exp);
7046 type = TREE_TYPE (exp);
7047 index = get_unwidened (exp, NULL_TREE);
7048 /* We can't strip a conversion from a signed type to an
7049 unsigned, because if we did, int_fits_type_p would do the
7050 wrong thing when checking case values for being in range,
7051 and it's too hard to do the right thing. */
7052 if (TREE_UNSIGNED (TREE_TYPE (exp))
7053 == TREE_UNSIGNED (TREE_TYPE (index)))
7054 exp = index;
7058 /* Add this new SWITCH_STMT to the stack. */
7059 cs = (struct c_switch *) xmalloc (sizeof (*cs));
7060 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, NULL_TREE);
7061 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7062 cs->next = switch_stack;
7063 switch_stack = cs;
7065 return add_stmt (switch_stack->switch_stmt);
7068 /* Process a case label. */
7070 tree
7071 do_case (low_value, high_value)
7072 tree low_value;
7073 tree high_value;
7075 tree label = NULL_TREE;
7077 if (switch_stack)
7079 label = c_add_case_label (switch_stack->cases,
7080 SWITCH_COND (switch_stack->switch_stmt),
7081 low_value, high_value);
7082 if (label == error_mark_node)
7083 label = NULL_TREE;
7085 else if (low_value)
7086 error ("case label not within a switch statement");
7087 else
7088 error ("`default' label not within a switch statement");
7090 return label;
7093 /* Finish the switch statement. */
7095 void
7096 c_finish_case ()
7098 struct c_switch *cs = switch_stack;
7100 RECHAIN_STMTS (cs->switch_stmt, SWITCH_BODY (cs->switch_stmt));
7102 /* Pop the stack. */
7103 switch_stack = switch_stack->next;
7104 splay_tree_delete (cs->cases);
7105 free (cs);