(ARM Built-in Functions): New node. Document ARM builtin functions for iWMMXt
[official-gcc.git] / gcc / c-typeck.c
blob8d6ed0be9fddca702900bb0ae3b7f31ef28df5ef
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, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "rtl.h"
37 #include "tree.h"
38 #include "c-tree.h"
39 #include "tm_p.h"
40 #include "flags.h"
41 #include "output.h"
42 #include "expr.h"
43 #include "toplev.h"
44 #include "intl.h"
45 #include "ggc.h"
46 #include "target.h"
48 /* Nonzero if we've already printed a "missing braces around initializer"
49 message within this initializer. */
50 static int missing_braces_mentioned;
52 /* 1 if we explained undeclared var errors. */
53 static int undeclared_variable_notice;
55 static tree qualify_type PARAMS ((tree, tree));
56 static int comp_target_types PARAMS ((tree, tree, int));
57 static int function_types_compatible_p PARAMS ((tree, tree));
58 static int type_lists_compatible_p PARAMS ((tree, tree));
59 static tree decl_constant_value_for_broken_optimization PARAMS ((tree));
60 static tree default_function_array_conversion PARAMS ((tree));
61 static tree lookup_field PARAMS ((tree, tree));
62 static void undeclared_variable PARAMS ((tree));
63 static tree convert_arguments PARAMS ((tree, tree, tree, tree));
64 static tree pointer_diff PARAMS ((tree, tree));
65 static tree unary_complex_lvalue PARAMS ((enum tree_code, tree, int));
66 static void pedantic_lvalue_warning PARAMS ((enum tree_code));
67 static tree internal_build_compound_expr PARAMS ((tree, int));
68 static tree convert_for_assignment PARAMS ((tree, tree, const char *,
69 tree, tree, int));
70 static void warn_for_assignment PARAMS ((const char *, const char *,
71 tree, int));
72 static tree valid_compound_expr_initializer PARAMS ((tree, tree));
73 static void push_string PARAMS ((const char *));
74 static void push_member_name PARAMS ((tree));
75 static void push_array_bounds PARAMS ((int));
76 static int spelling_length PARAMS ((void));
77 static char *print_spelling PARAMS ((char *));
78 static void warning_init PARAMS ((const char *));
79 static tree digest_init PARAMS ((tree, tree, int));
80 static void output_init_element PARAMS ((tree, tree, tree, int));
81 static void output_pending_init_elements PARAMS ((int));
82 static int set_designator PARAMS ((int));
83 static void push_range_stack PARAMS ((tree));
84 static void add_pending_init PARAMS ((tree, tree));
85 static void set_nonincremental_init PARAMS ((void));
86 static void set_nonincremental_init_from_string PARAMS ((tree));
87 static tree find_init_member PARAMS ((tree));
89 /* Do `exp = require_complete_type (exp);' to make sure exp
90 does not have an incomplete type. (That includes void types.) */
92 tree
93 require_complete_type (value)
94 tree value;
96 tree type = TREE_TYPE (value);
98 if (value == error_mark_node || type == error_mark_node)
99 return error_mark_node;
101 /* First, detect a valid value with a complete type. */
102 if (COMPLETE_TYPE_P (type))
103 return value;
105 c_incomplete_type_error (value, type);
106 return error_mark_node;
109 /* Print an error message for invalid use of an incomplete type.
110 VALUE is the expression that was used (or 0 if that isn't known)
111 and TYPE is the type that was invalid. */
113 void
114 c_incomplete_type_error (value, type)
115 tree value;
116 tree type;
118 const char *type_code_string;
120 /* Avoid duplicate error message. */
121 if (TREE_CODE (type) == ERROR_MARK)
122 return;
124 if (value != 0 && (TREE_CODE (value) == VAR_DECL
125 || TREE_CODE (value) == PARM_DECL))
126 error ("`%s' has an incomplete type",
127 IDENTIFIER_POINTER (DECL_NAME (value)));
128 else
130 retry:
131 /* We must print an error message. Be clever about what it says. */
133 switch (TREE_CODE (type))
135 case RECORD_TYPE:
136 type_code_string = "struct";
137 break;
139 case UNION_TYPE:
140 type_code_string = "union";
141 break;
143 case ENUMERAL_TYPE:
144 type_code_string = "enum";
145 break;
147 case VOID_TYPE:
148 error ("invalid use of void expression");
149 return;
151 case ARRAY_TYPE:
152 if (TYPE_DOMAIN (type))
154 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
156 error ("invalid use of flexible array member");
157 return;
159 type = TREE_TYPE (type);
160 goto retry;
162 error ("invalid use of array with unspecified bounds");
163 return;
165 default:
166 abort ();
169 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
170 error ("invalid use of undefined type `%s %s'",
171 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
172 else
173 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
174 error ("invalid use of incomplete typedef `%s'",
175 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
179 /* Given a type, apply default promotions wrt unnamed function
180 arguments and return the new type. */
182 tree
183 c_type_promotes_to (type)
184 tree type;
186 if (TYPE_MAIN_VARIANT (type) == float_type_node)
187 return double_type_node;
189 if (c_promoting_integer_type_p (type))
191 /* Preserve unsignedness if not really getting any wider. */
192 if (TREE_UNSIGNED (type)
193 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
194 return unsigned_type_node;
195 return integer_type_node;
198 return type;
201 /* Return a variant of TYPE which has all the type qualifiers of LIKE
202 as well as those of TYPE. */
204 static tree
205 qualify_type (type, like)
206 tree type, like;
208 return c_build_qualified_type (type,
209 TYPE_QUALS (type) | TYPE_QUALS (like));
212 /* Return the common type of two types.
213 We assume that comptypes has already been done and returned 1;
214 if that isn't so, this may crash. In particular, we assume that qualifiers
215 match.
217 This is the type for the result of most arithmetic operations
218 if the operands have the given two types. */
220 tree
221 common_type (t1, t2)
222 tree t1, t2;
224 enum tree_code code1;
225 enum tree_code code2;
226 tree attributes;
228 /* Save time if the two types are the same. */
230 if (t1 == t2) return t1;
232 /* If one type is nonsense, use the other. */
233 if (t1 == error_mark_node)
234 return t2;
235 if (t2 == error_mark_node)
236 return t1;
238 /* Merge the attributes. */
239 attributes = (*targetm.merge_type_attributes) (t1, t2);
241 /* Treat an enum type as the unsigned integer type of the same width. */
243 if (TREE_CODE (t1) == ENUMERAL_TYPE)
244 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
245 if (TREE_CODE (t2) == ENUMERAL_TYPE)
246 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
248 code1 = TREE_CODE (t1);
249 code2 = TREE_CODE (t2);
251 /* If one type is complex, form the common type of the non-complex
252 components, then make that complex. Use T1 or T2 if it is the
253 required type. */
254 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
256 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
257 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
258 tree subtype = common_type (subtype1, subtype2);
260 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
261 return build_type_attribute_variant (t1, attributes);
262 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
263 return build_type_attribute_variant (t2, attributes);
264 else
265 return build_type_attribute_variant (build_complex_type (subtype),
266 attributes);
269 switch (code1)
271 case INTEGER_TYPE:
272 case REAL_TYPE:
273 /* If only one is real, use it as the result. */
275 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
276 return build_type_attribute_variant (t1, attributes);
278 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
279 return build_type_attribute_variant (t2, attributes);
281 /* Both real or both integers; use the one with greater precision. */
283 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
284 return build_type_attribute_variant (t1, attributes);
285 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
286 return build_type_attribute_variant (t2, attributes);
288 /* Same precision. Prefer longs to ints even when same size. */
290 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
291 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
292 return build_type_attribute_variant (long_unsigned_type_node,
293 attributes);
295 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
296 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
298 /* But preserve unsignedness from the other type,
299 since long cannot hold all the values of an unsigned int. */
300 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
301 t1 = long_unsigned_type_node;
302 else
303 t1 = long_integer_type_node;
304 return build_type_attribute_variant (t1, attributes);
307 /* Likewise, prefer long double to double even if same size. */
308 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
309 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
310 return build_type_attribute_variant (long_double_type_node,
311 attributes);
313 /* Otherwise prefer the unsigned one. */
315 if (TREE_UNSIGNED (t1))
316 return build_type_attribute_variant (t1, attributes);
317 else
318 return build_type_attribute_variant (t2, attributes);
320 case POINTER_TYPE:
321 /* For two pointers, do this recursively on the target type,
322 and combine the qualifiers of the two types' targets. */
323 /* This code was turned off; I don't know why.
324 But ANSI C specifies doing this with the qualifiers.
325 So I turned it on again. */
327 tree pointed_to_1 = TREE_TYPE (t1);
328 tree pointed_to_2 = TREE_TYPE (t2);
329 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
330 TYPE_MAIN_VARIANT (pointed_to_2));
331 t1 = build_pointer_type (c_build_qualified_type
332 (target,
333 TYPE_QUALS (pointed_to_1) |
334 TYPE_QUALS (pointed_to_2)));
335 return build_type_attribute_variant (t1, attributes);
338 case ARRAY_TYPE:
340 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
341 /* Save space: see if the result is identical to one of the args. */
342 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
343 return build_type_attribute_variant (t1, attributes);
344 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
345 return build_type_attribute_variant (t2, attributes);
346 /* Merge the element types, and have a size if either arg has one. */
347 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
348 return build_type_attribute_variant (t1, attributes);
351 case FUNCTION_TYPE:
352 /* Function types: prefer the one that specified arg types.
353 If both do, merge the arg types. Also merge the return types. */
355 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
356 tree p1 = TYPE_ARG_TYPES (t1);
357 tree p2 = TYPE_ARG_TYPES (t2);
358 int len;
359 tree newargs, n;
360 int i;
362 /* Save space: see if the result is identical to one of the args. */
363 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
364 return build_type_attribute_variant (t1, attributes);
365 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
366 return build_type_attribute_variant (t2, attributes);
368 /* Simple way if one arg fails to specify argument types. */
369 if (TYPE_ARG_TYPES (t1) == 0)
371 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
372 return build_type_attribute_variant (t1, attributes);
374 if (TYPE_ARG_TYPES (t2) == 0)
376 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
377 return build_type_attribute_variant (t1, attributes);
380 /* If both args specify argument types, we must merge the two
381 lists, argument by argument. */
383 pushlevel (0);
384 declare_parm_level (1);
386 len = list_length (p1);
387 newargs = 0;
389 for (i = 0; i < len; i++)
390 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
392 n = newargs;
394 for (; p1;
395 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
397 /* A null type means arg type is not specified.
398 Take whatever the other function type has. */
399 if (TREE_VALUE (p1) == 0)
401 TREE_VALUE (n) = TREE_VALUE (p2);
402 goto parm_done;
404 if (TREE_VALUE (p2) == 0)
406 TREE_VALUE (n) = TREE_VALUE (p1);
407 goto parm_done;
410 /* Given wait (union {union wait *u; int *i} *)
411 and wait (union wait *),
412 prefer union wait * as type of parm. */
413 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
414 && TREE_VALUE (p1) != TREE_VALUE (p2))
416 tree memb;
417 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
418 memb; memb = TREE_CHAIN (memb))
419 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
421 TREE_VALUE (n) = TREE_VALUE (p2);
422 if (pedantic)
423 pedwarn ("function types not truly compatible in ISO C");
424 goto parm_done;
427 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
428 && TREE_VALUE (p2) != TREE_VALUE (p1))
430 tree memb;
431 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
432 memb; memb = TREE_CHAIN (memb))
433 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
435 TREE_VALUE (n) = TREE_VALUE (p1);
436 if (pedantic)
437 pedwarn ("function types not truly compatible in ISO C");
438 goto parm_done;
441 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
442 parm_done: ;
445 poplevel (0, 0, 0);
447 t1 = build_function_type (valtype, newargs);
448 /* ... falls through ... */
451 default:
452 return build_type_attribute_variant (t1, attributes);
457 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
458 or various other operations. Return 2 if they are compatible
459 but a warning may be needed if you use them together. */
462 comptypes (type1, type2)
463 tree type1, type2;
465 tree t1 = type1;
466 tree t2 = type2;
467 int attrval, val;
469 /* Suppress errors caused by previously reported errors. */
471 if (t1 == t2 || !t1 || !t2
472 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
473 return 1;
475 /* If either type is the internal version of sizetype, return the
476 language version. */
477 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
478 && TYPE_DOMAIN (t1) != 0)
479 t1 = TYPE_DOMAIN (t1);
481 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
482 && TYPE_DOMAIN (t2) != 0)
483 t2 = TYPE_DOMAIN (t2);
485 /* Treat an enum type as the integer type of the same width and
486 signedness. */
488 if (TREE_CODE (t1) == ENUMERAL_TYPE)
489 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
490 if (TREE_CODE (t2) == ENUMERAL_TYPE)
491 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
493 if (t1 == t2)
494 return 1;
496 /* Different classes of types can't be compatible. */
498 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
500 /* Qualifiers must match. */
502 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
503 return 0;
505 /* Allow for two different type nodes which have essentially the same
506 definition. Note that we already checked for equality of the type
507 qualifiers (just above). */
509 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
510 return 1;
512 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
513 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
514 return 0;
516 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
517 val = 0;
519 switch (TREE_CODE (t1))
521 case POINTER_TYPE:
522 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
523 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
524 break;
526 case FUNCTION_TYPE:
527 val = function_types_compatible_p (t1, t2);
528 break;
530 case ARRAY_TYPE:
532 tree d1 = TYPE_DOMAIN (t1);
533 tree d2 = TYPE_DOMAIN (t2);
534 bool d1_variable, d2_variable;
535 bool d1_zero, d2_zero;
536 val = 1;
538 /* Target types must match incl. qualifiers. */
539 if (TREE_TYPE (t1) != TREE_TYPE (t2)
540 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
541 return 0;
543 /* Sizes must match unless one is missing or variable. */
544 if (d1 == 0 || d2 == 0 || d1 == d2)
545 break;
547 d1_zero = ! TYPE_MAX_VALUE (d1);
548 d2_zero = ! TYPE_MAX_VALUE (d2);
550 d1_variable = (! d1_zero
551 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
552 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
553 d2_variable = (! d2_zero
554 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
555 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
557 if (d1_variable || d2_variable)
558 break;
559 if (d1_zero && d2_zero)
560 break;
561 if (d1_zero || d2_zero
562 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
563 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
564 val = 0;
566 break;
569 case RECORD_TYPE:
570 if (flag_objc && objc_comptypes (t1, t2, 0) == 1)
571 val = 1;
572 break;
574 case VECTOR_TYPE:
575 /* The target might allow certain vector types to be compatible. */
576 val = (*targetm.vector_opaque_p) (t1)
577 || (*targetm.vector_opaque_p) (t2);
578 break;
580 default:
581 break;
583 return attrval == 2 && val == 1 ? 2 : val;
586 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
587 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
588 to 1 or 0 depending if the check of the pointer types is meant to
589 be reflexive or not (typically, assignments are not reflexive,
590 while comparisons are reflexive).
593 static int
594 comp_target_types (ttl, ttr, reflexive)
595 tree ttl, ttr;
596 int reflexive;
598 int val;
600 /* Give objc_comptypes a crack at letting these types through. */
601 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
602 return val;
604 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
605 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
607 if (val == 2 && pedantic)
608 pedwarn ("types are not quite compatible");
609 return val;
612 /* Subroutines of `comptypes'. */
614 /* Return 1 if two function types F1 and F2 are compatible.
615 If either type specifies no argument types,
616 the other must specify a fixed number of self-promoting arg types.
617 Otherwise, if one type specifies only the number of arguments,
618 the other must specify that number of self-promoting arg types.
619 Otherwise, the argument types must match. */
621 static int
622 function_types_compatible_p (f1, f2)
623 tree f1, f2;
625 tree args1, args2;
626 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
627 int val = 1;
628 int val1;
629 tree ret1, ret2;
631 ret1 = TREE_TYPE (f1);
632 ret2 = TREE_TYPE (f2);
634 /* 'volatile' qualifiers on a function's return type mean the function
635 is noreturn. */
636 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
637 pedwarn ("function return types not compatible due to `volatile'");
638 if (TYPE_VOLATILE (ret1))
639 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
640 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
641 if (TYPE_VOLATILE (ret2))
642 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
643 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
644 val = comptypes (ret1, ret2);
645 if (val == 0)
646 return 0;
648 args1 = TYPE_ARG_TYPES (f1);
649 args2 = TYPE_ARG_TYPES (f2);
651 /* An unspecified parmlist matches any specified parmlist
652 whose argument types don't need default promotions. */
654 if (args1 == 0)
656 if (!self_promoting_args_p (args2))
657 return 0;
658 /* If one of these types comes from a non-prototype fn definition,
659 compare that with the other type's arglist.
660 If they don't match, ask for a warning (but no error). */
661 if (TYPE_ACTUAL_ARG_TYPES (f1)
662 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
663 val = 2;
664 return val;
666 if (args2 == 0)
668 if (!self_promoting_args_p (args1))
669 return 0;
670 if (TYPE_ACTUAL_ARG_TYPES (f2)
671 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
672 val = 2;
673 return val;
676 /* Both types have argument lists: compare them and propagate results. */
677 val1 = type_lists_compatible_p (args1, args2);
678 return val1 != 1 ? val1 : val;
681 /* Check two lists of types for compatibility,
682 returning 0 for incompatible, 1 for compatible,
683 or 2 for compatible with warning. */
685 static int
686 type_lists_compatible_p (args1, args2)
687 tree args1, args2;
689 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
690 int val = 1;
691 int newval = 0;
693 while (1)
695 if (args1 == 0 && args2 == 0)
696 return val;
697 /* If one list is shorter than the other,
698 they fail to match. */
699 if (args1 == 0 || args2 == 0)
700 return 0;
701 /* A null pointer instead of a type
702 means there is supposed to be an argument
703 but nothing is specified about what type it has.
704 So match anything that self-promotes. */
705 if (TREE_VALUE (args1) == 0)
707 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
708 return 0;
710 else if (TREE_VALUE (args2) == 0)
712 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
713 return 0;
715 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
716 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
718 /* Allow wait (union {union wait *u; int *i} *)
719 and wait (union wait *) to be compatible. */
720 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
721 && (TYPE_NAME (TREE_VALUE (args1)) == 0
722 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
723 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
724 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
725 TYPE_SIZE (TREE_VALUE (args2))))
727 tree memb;
728 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
729 memb; memb = TREE_CHAIN (memb))
730 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
731 break;
732 if (memb == 0)
733 return 0;
735 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
736 && (TYPE_NAME (TREE_VALUE (args2)) == 0
737 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
738 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
739 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
740 TYPE_SIZE (TREE_VALUE (args1))))
742 tree memb;
743 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
744 memb; memb = TREE_CHAIN (memb))
745 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
746 break;
747 if (memb == 0)
748 return 0;
750 else
751 return 0;
754 /* comptypes said ok, but record if it said to warn. */
755 if (newval > val)
756 val = newval;
758 args1 = TREE_CHAIN (args1);
759 args2 = TREE_CHAIN (args2);
763 /* Compute the size to increment a pointer by. */
765 tree
766 c_size_in_bytes (type)
767 tree type;
769 enum tree_code code = TREE_CODE (type);
771 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
772 return size_one_node;
774 if (!COMPLETE_OR_VOID_TYPE_P (type))
776 error ("arithmetic on pointer to an incomplete type");
777 return size_one_node;
780 /* Convert in case a char is more than one unit. */
781 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
782 size_int (TYPE_PRECISION (char_type_node)
783 / BITS_PER_UNIT));
786 /* Return either DECL or its known constant value (if it has one). */
788 tree
789 decl_constant_value (decl)
790 tree decl;
792 if (/* Don't change a variable array bound or initial value to a constant
793 in a place where a variable is invalid. */
794 current_function_decl != 0
795 && ! TREE_THIS_VOLATILE (decl)
796 && TREE_READONLY (decl)
797 && DECL_INITIAL (decl) != 0
798 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
799 /* This is invalid if initial value is not constant.
800 If it has either a function call, a memory reference,
801 or a variable, then re-evaluating it could give different results. */
802 && TREE_CONSTANT (DECL_INITIAL (decl))
803 /* Check for cases where this is sub-optimal, even though valid. */
804 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
805 return DECL_INITIAL (decl);
806 return decl;
809 /* Return either DECL or its known constant value (if it has one), but
810 return DECL if pedantic or DECL has mode BLKmode. This is for
811 bug-compatibility with the old behavior of decl_constant_value
812 (before GCC 3.0); every use of this function is a bug and it should
813 be removed before GCC 3.1. It is not appropriate to use pedantic
814 in a way that affects optimization, and BLKmode is probably not the
815 right test for avoiding misoptimizations either. */
817 static tree
818 decl_constant_value_for_broken_optimization (decl)
819 tree decl;
821 if (pedantic || DECL_MODE (decl) == BLKmode)
822 return decl;
823 else
824 return decl_constant_value (decl);
828 /* Perform the default conversion of arrays and functions to pointers.
829 Return the result of converting EXP. For any other expression, just
830 return EXP. */
832 static tree
833 default_function_array_conversion (exp)
834 tree exp;
836 tree orig_exp;
837 tree type = TREE_TYPE (exp);
838 enum tree_code code = TREE_CODE (type);
839 int not_lvalue = 0;
841 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
842 an lvalue.
844 Do not use STRIP_NOPS here! It will remove conversions from pointer
845 to integer and cause infinite recursion. */
846 orig_exp = exp;
847 while (TREE_CODE (exp) == NON_LVALUE_EXPR
848 || (TREE_CODE (exp) == NOP_EXPR
849 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
851 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
852 not_lvalue = 1;
853 exp = TREE_OPERAND (exp, 0);
856 /* Preserve the original expression code. */
857 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
858 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
860 if (code == FUNCTION_TYPE)
862 return build_unary_op (ADDR_EXPR, exp, 0);
864 if (code == ARRAY_TYPE)
866 tree adr;
867 tree restype = TREE_TYPE (type);
868 tree ptrtype;
869 int constp = 0;
870 int volatilep = 0;
871 int lvalue_array_p;
873 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
875 constp = TREE_READONLY (exp);
876 volatilep = TREE_THIS_VOLATILE (exp);
879 if (TYPE_QUALS (type) || constp || volatilep)
880 restype
881 = c_build_qualified_type (restype,
882 TYPE_QUALS (type)
883 | (constp * TYPE_QUAL_CONST)
884 | (volatilep * TYPE_QUAL_VOLATILE));
886 if (TREE_CODE (exp) == INDIRECT_REF)
887 return convert (TYPE_POINTER_TO (restype),
888 TREE_OPERAND (exp, 0));
890 if (TREE_CODE (exp) == COMPOUND_EXPR)
892 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
893 return build (COMPOUND_EXPR, TREE_TYPE (op1),
894 TREE_OPERAND (exp, 0), op1);
897 lvalue_array_p = !not_lvalue && lvalue_p (exp);
898 if (!flag_isoc99 && !lvalue_array_p)
900 /* Before C99, non-lvalue arrays do not decay to pointers.
901 Normally, using such an array would be invalid; but it can
902 be used correctly inside sizeof or as a statement expression.
903 Thus, do not give an error here; an error will result later. */
904 return exp;
907 ptrtype = build_pointer_type (restype);
909 if (TREE_CODE (exp) == VAR_DECL)
911 /* ??? This is not really quite correct
912 in that the type of the operand of ADDR_EXPR
913 is not the target type of the type of the ADDR_EXPR itself.
914 Question is, can this lossage be avoided? */
915 adr = build1 (ADDR_EXPR, ptrtype, exp);
916 if (!c_mark_addressable (exp))
917 return error_mark_node;
918 TREE_CONSTANT (adr) = staticp (exp);
919 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
920 return adr;
922 /* This way is better for a COMPONENT_REF since it can
923 simplify the offset for a component. */
924 adr = build_unary_op (ADDR_EXPR, exp, 1);
925 return convert (ptrtype, adr);
927 return exp;
930 /* Perform default promotions for C data used in expressions.
931 Arrays and functions are converted to pointers;
932 enumeral types or short or char, to int.
933 In addition, manifest constants symbols are replaced by their values. */
935 tree
936 default_conversion (exp)
937 tree exp;
939 tree orig_exp;
940 tree type = TREE_TYPE (exp);
941 enum tree_code code = TREE_CODE (type);
943 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
944 return default_function_array_conversion (exp);
946 /* Constants can be used directly unless they're not loadable. */
947 if (TREE_CODE (exp) == CONST_DECL)
948 exp = DECL_INITIAL (exp);
950 /* Replace a nonvolatile const static variable with its value unless
951 it is an array, in which case we must be sure that taking the
952 address of the array produces consistent results. */
953 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
955 exp = decl_constant_value_for_broken_optimization (exp);
956 type = TREE_TYPE (exp);
959 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
960 an lvalue.
962 Do not use STRIP_NOPS here! It will remove conversions from pointer
963 to integer and cause infinite recursion. */
964 orig_exp = exp;
965 while (TREE_CODE (exp) == NON_LVALUE_EXPR
966 || (TREE_CODE (exp) == NOP_EXPR
967 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
968 exp = TREE_OPERAND (exp, 0);
970 /* Preserve the original expression code. */
971 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
972 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
974 /* Normally convert enums to int,
975 but convert wide enums to something wider. */
976 if (code == ENUMERAL_TYPE)
978 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
979 TYPE_PRECISION (integer_type_node)),
980 ((TYPE_PRECISION (type)
981 >= TYPE_PRECISION (integer_type_node))
982 && TREE_UNSIGNED (type)));
984 return convert (type, exp);
987 if (TREE_CODE (exp) == COMPONENT_REF
988 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
989 /* If it's thinner than an int, promote it like a
990 c_promoting_integer_type_p, otherwise leave it alone. */
991 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
992 TYPE_PRECISION (integer_type_node)))
993 return convert (integer_type_node, exp);
995 if (c_promoting_integer_type_p (type))
997 /* Preserve unsignedness if not really getting any wider. */
998 if (TREE_UNSIGNED (type)
999 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1000 return convert (unsigned_type_node, exp);
1002 return convert (integer_type_node, exp);
1005 if (code == VOID_TYPE)
1007 error ("void value not ignored as it ought to be");
1008 return error_mark_node;
1010 return exp;
1013 /* Look up COMPONENT in a structure or union DECL.
1015 If the component name is not found, returns NULL_TREE. Otherwise,
1016 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1017 stepping down the chain to the component, which is in the last
1018 TREE_VALUE of the list. Normally the list is of length one, but if
1019 the component is embedded within (nested) anonymous structures or
1020 unions, the list steps down the chain to the component. */
1022 static tree
1023 lookup_field (decl, component)
1024 tree decl, component;
1026 tree type = TREE_TYPE (decl);
1027 tree field;
1029 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1030 to the field elements. Use a binary search on this array to quickly
1031 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1032 will always be set for structures which have many elements. */
1034 if (TYPE_LANG_SPECIFIC (type))
1036 int bot, top, half;
1037 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1039 field = TYPE_FIELDS (type);
1040 bot = 0;
1041 top = TYPE_LANG_SPECIFIC (type)->len;
1042 while (top - bot > 1)
1044 half = (top - bot + 1) >> 1;
1045 field = field_array[bot+half];
1047 if (DECL_NAME (field) == NULL_TREE)
1049 /* Step through all anon unions in linear fashion. */
1050 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1052 field = field_array[bot++];
1053 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1054 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1056 tree anon = lookup_field (field, component);
1058 if (anon)
1059 return tree_cons (NULL_TREE, field, anon);
1063 /* Entire record is only anon unions. */
1064 if (bot > top)
1065 return NULL_TREE;
1067 /* Restart the binary search, with new lower bound. */
1068 continue;
1071 if (DECL_NAME (field) == component)
1072 break;
1073 if (DECL_NAME (field) < component)
1074 bot += half;
1075 else
1076 top = bot + half;
1079 if (DECL_NAME (field_array[bot]) == component)
1080 field = field_array[bot];
1081 else if (DECL_NAME (field) != component)
1082 return NULL_TREE;
1084 else
1086 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1088 if (DECL_NAME (field) == NULL_TREE
1089 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1090 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1092 tree anon = lookup_field (field, component);
1094 if (anon)
1095 return tree_cons (NULL_TREE, field, anon);
1098 if (DECL_NAME (field) == component)
1099 break;
1102 if (field == NULL_TREE)
1103 return NULL_TREE;
1106 return tree_cons (NULL_TREE, field, NULL_TREE);
1109 /* Make an expression to refer to the COMPONENT field of
1110 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1112 tree
1113 build_component_ref (datum, component)
1114 tree datum, component;
1116 tree type = TREE_TYPE (datum);
1117 enum tree_code code = TREE_CODE (type);
1118 tree field = NULL;
1119 tree ref;
1121 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1122 If pedantic ensure that the arguments are not lvalues; otherwise,
1123 if the component is an array, it would wrongly decay to a pointer in
1124 C89 mode.
1125 We cannot do this with a COND_EXPR, because in a conditional expression
1126 the default promotions are applied to both sides, and this would yield
1127 the wrong type of the result; for example, if the components have
1128 type "char". */
1129 switch (TREE_CODE (datum))
1131 case COMPOUND_EXPR:
1133 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1134 return build (COMPOUND_EXPR, TREE_TYPE (value),
1135 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
1137 default:
1138 break;
1141 /* See if there is a field or component with name COMPONENT. */
1143 if (code == RECORD_TYPE || code == UNION_TYPE)
1145 if (!COMPLETE_TYPE_P (type))
1147 c_incomplete_type_error (NULL_TREE, type);
1148 return error_mark_node;
1151 field = lookup_field (datum, component);
1153 if (!field)
1155 error ("%s has no member named `%s'",
1156 code == RECORD_TYPE ? "structure" : "union",
1157 IDENTIFIER_POINTER (component));
1158 return error_mark_node;
1161 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1162 This might be better solved in future the way the C++ front
1163 end does it - by giving the anonymous entities each a
1164 separate name and type, and then have build_component_ref
1165 recursively call itself. We can't do that here. */
1168 tree subdatum = TREE_VALUE (field);
1170 if (TREE_TYPE (subdatum) == error_mark_node)
1171 return error_mark_node;
1173 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1174 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1175 TREE_READONLY (ref) = 1;
1176 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1177 TREE_THIS_VOLATILE (ref) = 1;
1179 if (TREE_DEPRECATED (subdatum))
1180 warn_deprecated_use (subdatum);
1182 datum = ref;
1184 field = TREE_CHAIN (field);
1186 while (field);
1188 return ref;
1190 else if (code != ERROR_MARK)
1191 error ("request for member `%s' in something not a structure or union",
1192 IDENTIFIER_POINTER (component));
1194 return error_mark_node;
1197 /* Given an expression PTR for a pointer, return an expression
1198 for the value pointed to.
1199 ERRORSTRING is the name of the operator to appear in error messages. */
1201 tree
1202 build_indirect_ref (ptr, errorstring)
1203 tree ptr;
1204 const char *errorstring;
1206 tree pointer = default_conversion (ptr);
1207 tree type = TREE_TYPE (pointer);
1209 if (TREE_CODE (type) == POINTER_TYPE)
1211 if (TREE_CODE (pointer) == ADDR_EXPR
1212 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1213 == TREE_TYPE (type)))
1214 return TREE_OPERAND (pointer, 0);
1215 else
1217 tree t = TREE_TYPE (type);
1218 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1220 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1222 error ("dereferencing pointer to incomplete type");
1223 return error_mark_node;
1225 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1226 warning ("dereferencing `void *' pointer");
1228 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1229 so that we get the proper error message if the result is used
1230 to assign to. Also, &* is supposed to be a no-op.
1231 And ANSI C seems to specify that the type of the result
1232 should be the const type. */
1233 /* A de-reference of a pointer to const is not a const. It is valid
1234 to change it via some other pointer. */
1235 TREE_READONLY (ref) = TYPE_READONLY (t);
1236 TREE_SIDE_EFFECTS (ref)
1237 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1238 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1239 return ref;
1242 else if (TREE_CODE (pointer) != ERROR_MARK)
1243 error ("invalid type argument of `%s'", errorstring);
1244 return error_mark_node;
1247 /* This handles expressions of the form "a[i]", which denotes
1248 an array reference.
1250 This is logically equivalent in C to *(a+i), but we may do it differently.
1251 If A is a variable or a member, we generate a primitive ARRAY_REF.
1252 This avoids forcing the array out of registers, and can work on
1253 arrays that are not lvalues (for example, members of structures returned
1254 by functions). */
1256 tree
1257 build_array_ref (array, index)
1258 tree array, index;
1260 if (index == 0)
1262 error ("subscript missing in array reference");
1263 return error_mark_node;
1266 if (TREE_TYPE (array) == error_mark_node
1267 || TREE_TYPE (index) == error_mark_node)
1268 return error_mark_node;
1270 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1271 && TREE_CODE (array) != INDIRECT_REF)
1273 tree rval, type;
1275 /* Subscripting with type char is likely to lose
1276 on a machine where chars are signed.
1277 So warn on any machine, but optionally.
1278 Don't warn for unsigned char since that type is safe.
1279 Don't warn for signed char because anyone who uses that
1280 must have done so deliberately. */
1281 if (warn_char_subscripts
1282 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1283 warning ("array subscript has type `char'");
1285 /* Apply default promotions *after* noticing character types. */
1286 index = default_conversion (index);
1288 /* Require integer *after* promotion, for sake of enums. */
1289 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1291 error ("array subscript is not an integer");
1292 return error_mark_node;
1295 /* An array that is indexed by a non-constant
1296 cannot be stored in a register; we must be able to do
1297 address arithmetic on its address.
1298 Likewise an array of elements of variable size. */
1299 if (TREE_CODE (index) != INTEGER_CST
1300 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1301 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1303 if (!c_mark_addressable (array))
1304 return error_mark_node;
1306 /* An array that is indexed by a constant value which is not within
1307 the array bounds cannot be stored in a register either; because we
1308 would get a crash in store_bit_field/extract_bit_field when trying
1309 to access a non-existent part of the register. */
1310 if (TREE_CODE (index) == INTEGER_CST
1311 && TYPE_VALUES (TREE_TYPE (array))
1312 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1314 if (!c_mark_addressable (array))
1315 return error_mark_node;
1318 if (pedantic)
1320 tree foo = array;
1321 while (TREE_CODE (foo) == COMPONENT_REF)
1322 foo = TREE_OPERAND (foo, 0);
1323 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1324 pedwarn ("ISO C forbids subscripting `register' array");
1325 else if (! flag_isoc99 && ! lvalue_p (foo))
1326 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1329 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1330 rval = build (ARRAY_REF, type, array, index);
1331 /* Array ref is const/volatile if the array elements are
1332 or if the array is. */
1333 TREE_READONLY (rval)
1334 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1335 | TREE_READONLY (array));
1336 TREE_SIDE_EFFECTS (rval)
1337 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1338 | TREE_SIDE_EFFECTS (array));
1339 TREE_THIS_VOLATILE (rval)
1340 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1341 /* This was added by rms on 16 Nov 91.
1342 It fixes vol struct foo *a; a->elts[1]
1343 in an inline function.
1344 Hope it doesn't break something else. */
1345 | TREE_THIS_VOLATILE (array));
1346 return require_complete_type (fold (rval));
1350 tree ar = default_conversion (array);
1351 tree ind = default_conversion (index);
1353 /* Do the same warning check as above, but only on the part that's
1354 syntactically the index and only if it is also semantically
1355 the index. */
1356 if (warn_char_subscripts
1357 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1358 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1359 warning ("subscript has type `char'");
1361 /* Put the integer in IND to simplify error checking. */
1362 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1364 tree temp = ar;
1365 ar = ind;
1366 ind = temp;
1369 if (ar == error_mark_node)
1370 return ar;
1372 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1373 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1375 error ("subscripted value is neither array nor pointer");
1376 return error_mark_node;
1378 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1380 error ("array subscript is not an integer");
1381 return error_mark_node;
1384 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1385 "array indexing");
1389 /* Issue an error message for a reference to an undeclared variable ID,
1390 including a reference to a builtin outside of function-call context.
1391 Arrange to suppress further errors for the same identifier. */
1392 static void
1393 undeclared_variable (id)
1394 tree id;
1396 if (current_function_decl == 0)
1398 error ("`%s' undeclared here (not in a function)",
1399 IDENTIFIER_POINTER (id));
1400 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1402 else
1404 error ("`%s' undeclared (first use in this function)",
1405 IDENTIFIER_POINTER (id));
1407 if (! undeclared_variable_notice)
1409 error ("(Each undeclared identifier is reported only once");
1410 error ("for each function it appears in.)");
1411 undeclared_variable_notice = 1;
1414 /* Set IDENTIFIER_SYMBOL_VALUE (id) to error_mark_node
1415 at function scope. This suppresses further warnings
1416 about this undeclared identifier in this function. */
1417 pushdecl_function_level (error_mark_node, id);
1421 /* Build an external reference to identifier ID. FUN indicates
1422 whether this will be used for a function call. */
1423 tree
1424 build_external_ref (id, fun)
1425 tree id;
1426 int fun;
1428 tree ref;
1429 tree decl = lookup_name (id);
1430 tree objc_ivar = lookup_objc_ivar (id);
1432 if (decl && decl != error_mark_node)
1434 /* Properly declared variable or function reference. */
1435 if (!objc_ivar)
1436 ref = decl;
1437 else if (decl != objc_ivar && DECL_CONTEXT (decl) != 0)
1439 warning ("local declaration of `%s' hides instance variable",
1440 IDENTIFIER_POINTER (id));
1441 ref = decl;
1443 else
1444 ref = objc_ivar;
1446 else if (objc_ivar)
1447 ref = objc_ivar;
1448 else if (fun)
1449 /* Implicit function declaration. */
1450 ref = implicitly_declare (id);
1451 else if (decl == error_mark_node)
1452 /* Don't complain about something that's already been
1453 complained about. */
1454 return error_mark_node;
1455 else
1457 undeclared_variable (id);
1458 return error_mark_node;
1461 if (TREE_TYPE (ref) == error_mark_node)
1462 return error_mark_node;
1464 if (TREE_DEPRECATED (ref))
1465 warn_deprecated_use (ref);
1467 if (!skip_evaluation)
1468 assemble_external (ref);
1469 TREE_USED (ref) = 1;
1471 if (TREE_CODE (ref) == CONST_DECL)
1473 ref = DECL_INITIAL (ref);
1474 TREE_CONSTANT (ref) = 1;
1476 else if (current_function_decl != 0
1477 && DECL_CONTEXT (current_function_decl) != 0
1478 && (TREE_CODE (ref) == VAR_DECL
1479 || TREE_CODE (ref) == PARM_DECL
1480 || TREE_CODE (ref) == FUNCTION_DECL))
1482 tree context = decl_function_context (ref);
1484 if (context != 0 && context != current_function_decl)
1485 DECL_NONLOCAL (ref) = 1;
1488 return ref;
1491 /* Build a function call to function FUNCTION with parameters PARAMS.
1492 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1493 TREE_VALUE of each node is a parameter-expression.
1494 FUNCTION's data type may be a function type or a pointer-to-function. */
1496 tree
1497 build_function_call (function, params)
1498 tree function, params;
1500 tree fntype, fundecl = 0;
1501 tree coerced_params;
1502 tree name = NULL_TREE, result;
1504 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1505 STRIP_TYPE_NOPS (function);
1507 /* Convert anything with function type to a pointer-to-function. */
1508 if (TREE_CODE (function) == FUNCTION_DECL)
1510 name = DECL_NAME (function);
1512 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1513 (because calling an inline function does not mean the function
1514 needs to be separately compiled). */
1515 fntype = build_type_variant (TREE_TYPE (function),
1516 TREE_READONLY (function),
1517 TREE_THIS_VOLATILE (function));
1518 fundecl = function;
1519 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1521 else
1522 function = default_conversion (function);
1524 fntype = TREE_TYPE (function);
1526 if (TREE_CODE (fntype) == ERROR_MARK)
1527 return error_mark_node;
1529 if (!(TREE_CODE (fntype) == POINTER_TYPE
1530 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1532 error ("called object is not a function");
1533 return error_mark_node;
1536 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1537 current_function_returns_abnormally = 1;
1539 /* fntype now gets the type of function pointed to. */
1540 fntype = TREE_TYPE (fntype);
1542 /* Convert the parameters to the types declared in the
1543 function prototype, or apply default promotions. */
1545 coerced_params
1546 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1548 /* Check that the arguments to the function are valid. */
1550 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1552 /* Recognize certain built-in functions so we can make tree-codes
1553 other than CALL_EXPR. We do this when it enables fold-const.c
1554 to do something useful. */
1556 if (TREE_CODE (function) == ADDR_EXPR
1557 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1558 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1560 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1561 params, coerced_params);
1562 if (result)
1563 return result;
1566 result = build (CALL_EXPR, TREE_TYPE (fntype),
1567 function, coerced_params, NULL_TREE);
1568 TREE_SIDE_EFFECTS (result) = 1;
1569 result = fold (result);
1571 if (VOID_TYPE_P (TREE_TYPE (result)))
1572 return result;
1573 return require_complete_type (result);
1576 /* Convert the argument expressions in the list VALUES
1577 to the types in the list TYPELIST. The result is a list of converted
1578 argument expressions.
1580 If TYPELIST is exhausted, or when an element has NULL as its type,
1581 perform the default conversions.
1583 PARMLIST is the chain of parm decls for the function being called.
1584 It may be 0, if that info is not available.
1585 It is used only for generating error messages.
1587 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1589 This is also where warnings about wrong number of args are generated.
1591 Both VALUES and the returned value are chains of TREE_LIST nodes
1592 with the elements of the list in the TREE_VALUE slots of those nodes. */
1594 static tree
1595 convert_arguments (typelist, values, name, fundecl)
1596 tree typelist, values, name, fundecl;
1598 tree typetail, valtail;
1599 tree result = NULL;
1600 int parmnum;
1602 /* Scan the given expressions and types, producing individual
1603 converted arguments and pushing them on RESULT in reverse order. */
1605 for (valtail = values, typetail = typelist, parmnum = 0;
1606 valtail;
1607 valtail = TREE_CHAIN (valtail), parmnum++)
1609 tree type = typetail ? TREE_VALUE (typetail) : 0;
1610 tree val = TREE_VALUE (valtail);
1612 if (type == void_type_node)
1614 if (name)
1615 error ("too many arguments to function `%s'",
1616 IDENTIFIER_POINTER (name));
1617 else
1618 error ("too many arguments to function");
1619 break;
1622 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1623 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1624 to convert automatically to a pointer. */
1625 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1626 val = TREE_OPERAND (val, 0);
1628 val = default_function_array_conversion (val);
1630 val = require_complete_type (val);
1632 if (type != 0)
1634 /* Formal parm type is specified by a function prototype. */
1635 tree parmval;
1637 if (!COMPLETE_TYPE_P (type))
1639 error ("type of formal parameter %d is incomplete", parmnum + 1);
1640 parmval = val;
1642 else
1644 /* Optionally warn about conversions that
1645 differ from the default conversions. */
1646 if (warn_conversion || warn_traditional)
1648 int formal_prec = TYPE_PRECISION (type);
1650 if (INTEGRAL_TYPE_P (type)
1651 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1652 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1653 if (INTEGRAL_TYPE_P (type)
1654 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1655 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1656 else if (TREE_CODE (type) == COMPLEX_TYPE
1657 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1658 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1659 else if (TREE_CODE (type) == REAL_TYPE
1660 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1661 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1662 else if (TREE_CODE (type) == COMPLEX_TYPE
1663 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1664 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1665 else if (TREE_CODE (type) == REAL_TYPE
1666 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1667 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1668 /* ??? At some point, messages should be written about
1669 conversions between complex types, but that's too messy
1670 to do now. */
1671 else if (TREE_CODE (type) == REAL_TYPE
1672 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1674 /* Warn if any argument is passed as `float',
1675 since without a prototype it would be `double'. */
1676 if (formal_prec == TYPE_PRECISION (float_type_node))
1677 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1679 /* Detect integer changing in width or signedness.
1680 These warnings are only activated with
1681 -Wconversion, not with -Wtraditional. */
1682 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1683 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1685 tree would_have_been = default_conversion (val);
1686 tree type1 = TREE_TYPE (would_have_been);
1688 if (TREE_CODE (type) == ENUMERAL_TYPE
1689 && (TYPE_MAIN_VARIANT (type)
1690 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1691 /* No warning if function asks for enum
1692 and the actual arg is that enum type. */
1694 else if (formal_prec != TYPE_PRECISION (type1))
1695 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1696 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1698 /* Don't complain if the formal parameter type
1699 is an enum, because we can't tell now whether
1700 the value was an enum--even the same enum. */
1701 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1703 else if (TREE_CODE (val) == INTEGER_CST
1704 && int_fits_type_p (val, type))
1705 /* Change in signedness doesn't matter
1706 if a constant value is unaffected. */
1708 /* Likewise for a constant in a NOP_EXPR. */
1709 else if (TREE_CODE (val) == NOP_EXPR
1710 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1711 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1713 /* If the value is extended from a narrower
1714 unsigned type, it doesn't matter whether we
1715 pass it as signed or unsigned; the value
1716 certainly is the same either way. */
1717 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1718 && TREE_UNSIGNED (TREE_TYPE (val)))
1720 else if (TREE_UNSIGNED (type))
1721 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1722 else
1723 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1727 parmval = convert_for_assignment (type, val,
1728 (char *) 0, /* arg passing */
1729 fundecl, name, parmnum + 1);
1731 if (PROMOTE_PROTOTYPES
1732 && INTEGRAL_TYPE_P (type)
1733 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1734 parmval = default_conversion (parmval);
1736 result = tree_cons (NULL_TREE, parmval, result);
1738 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1739 && (TYPE_PRECISION (TREE_TYPE (val))
1740 < TYPE_PRECISION (double_type_node)))
1741 /* Convert `float' to `double'. */
1742 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1743 else
1744 /* Convert `short' and `char' to full-size `int'. */
1745 result = tree_cons (NULL_TREE, default_conversion (val), result);
1747 if (typetail)
1748 typetail = TREE_CHAIN (typetail);
1751 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1753 if (name)
1754 error ("too few arguments to function `%s'",
1755 IDENTIFIER_POINTER (name));
1756 else
1757 error ("too few arguments to function");
1760 return nreverse (result);
1763 /* This is the entry point used by the parser
1764 for binary operators in the input.
1765 In addition to constructing the expression,
1766 we check for operands that were written with other binary operators
1767 in a way that is likely to confuse the user. */
1769 tree
1770 parser_build_binary_op (code, arg1, arg2)
1771 enum tree_code code;
1772 tree arg1, arg2;
1774 tree result = build_binary_op (code, arg1, arg2, 1);
1776 char class;
1777 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1778 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1779 enum tree_code code1 = ERROR_MARK;
1780 enum tree_code code2 = ERROR_MARK;
1782 if (TREE_CODE (result) == ERROR_MARK)
1783 return error_mark_node;
1785 if (IS_EXPR_CODE_CLASS (class1))
1786 code1 = C_EXP_ORIGINAL_CODE (arg1);
1787 if (IS_EXPR_CODE_CLASS (class2))
1788 code2 = C_EXP_ORIGINAL_CODE (arg2);
1790 /* Check for cases such as x+y<<z which users are likely
1791 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1792 is cleared to prevent these warnings. */
1793 if (warn_parentheses)
1795 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1797 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1798 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1799 warning ("suggest parentheses around + or - inside shift");
1802 if (code == TRUTH_ORIF_EXPR)
1804 if (code1 == TRUTH_ANDIF_EXPR
1805 || code2 == TRUTH_ANDIF_EXPR)
1806 warning ("suggest parentheses around && within ||");
1809 if (code == BIT_IOR_EXPR)
1811 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1812 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1813 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1814 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1815 warning ("suggest parentheses around arithmetic in operand of |");
1816 /* Check cases like x|y==z */
1817 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1818 warning ("suggest parentheses around comparison in operand of |");
1821 if (code == BIT_XOR_EXPR)
1823 if (code1 == BIT_AND_EXPR
1824 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1825 || code2 == BIT_AND_EXPR
1826 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1827 warning ("suggest parentheses around arithmetic in operand of ^");
1828 /* Check cases like x^y==z */
1829 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1830 warning ("suggest parentheses around comparison in operand of ^");
1833 if (code == BIT_AND_EXPR)
1835 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1836 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1837 warning ("suggest parentheses around + or - in operand of &");
1838 /* Check cases like x&y==z */
1839 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1840 warning ("suggest parentheses around comparison in operand of &");
1844 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1845 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1846 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1847 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1849 unsigned_conversion_warning (result, arg1);
1850 unsigned_conversion_warning (result, arg2);
1851 overflow_warning (result);
1853 class = TREE_CODE_CLASS (TREE_CODE (result));
1855 /* Record the code that was specified in the source,
1856 for the sake of warnings about confusing nesting. */
1857 if (IS_EXPR_CODE_CLASS (class))
1858 C_SET_EXP_ORIGINAL_CODE (result, code);
1859 else
1861 int flag = TREE_CONSTANT (result);
1862 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1863 so that convert_for_assignment wouldn't strip it.
1864 That way, we got warnings for things like p = (1 - 1).
1865 But it turns out we should not get those warnings. */
1866 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1867 C_SET_EXP_ORIGINAL_CODE (result, code);
1868 TREE_CONSTANT (result) = flag;
1871 return result;
1874 /* Build a binary-operation expression without default conversions.
1875 CODE is the kind of expression to build.
1876 This function differs from `build' in several ways:
1877 the data type of the result is computed and recorded in it,
1878 warnings are generated if arg data types are invalid,
1879 special handling for addition and subtraction of pointers is known,
1880 and some optimization is done (operations on narrow ints
1881 are done in the narrower type when that gives the same result).
1882 Constant folding is also done before the result is returned.
1884 Note that the operands will never have enumeral types, or function
1885 or array types, because either they will have the default conversions
1886 performed or they have both just been converted to some other type in which
1887 the arithmetic is to be done. */
1889 tree
1890 build_binary_op (code, orig_op0, orig_op1, convert_p)
1891 enum tree_code code;
1892 tree orig_op0, orig_op1;
1893 int convert_p;
1895 tree type0, type1;
1896 enum tree_code code0, code1;
1897 tree op0, op1;
1899 /* Expression code to give to the expression when it is built.
1900 Normally this is CODE, which is what the caller asked for,
1901 but in some special cases we change it. */
1902 enum tree_code resultcode = code;
1904 /* Data type in which the computation is to be performed.
1905 In the simplest cases this is the common type of the arguments. */
1906 tree result_type = NULL;
1908 /* Nonzero means operands have already been type-converted
1909 in whatever way is necessary.
1910 Zero means they need to be converted to RESULT_TYPE. */
1911 int converted = 0;
1913 /* Nonzero means create the expression with this type, rather than
1914 RESULT_TYPE. */
1915 tree build_type = 0;
1917 /* Nonzero means after finally constructing the expression
1918 convert it to this type. */
1919 tree final_type = 0;
1921 /* Nonzero if this is an operation like MIN or MAX which can
1922 safely be computed in short if both args are promoted shorts.
1923 Also implies COMMON.
1924 -1 indicates a bitwise operation; this makes a difference
1925 in the exact conditions for when it is safe to do the operation
1926 in a narrower mode. */
1927 int shorten = 0;
1929 /* Nonzero if this is a comparison operation;
1930 if both args are promoted shorts, compare the original shorts.
1931 Also implies COMMON. */
1932 int short_compare = 0;
1934 /* Nonzero if this is a right-shift operation, which can be computed on the
1935 original short and then promoted if the operand is a promoted short. */
1936 int short_shift = 0;
1938 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1939 int common = 0;
1941 if (convert_p)
1943 op0 = default_conversion (orig_op0);
1944 op1 = default_conversion (orig_op1);
1946 else
1948 op0 = orig_op0;
1949 op1 = orig_op1;
1952 type0 = TREE_TYPE (op0);
1953 type1 = TREE_TYPE (op1);
1955 /* The expression codes of the data types of the arguments tell us
1956 whether the arguments are integers, floating, pointers, etc. */
1957 code0 = TREE_CODE (type0);
1958 code1 = TREE_CODE (type1);
1960 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1961 STRIP_TYPE_NOPS (op0);
1962 STRIP_TYPE_NOPS (op1);
1964 /* If an error was already reported for one of the arguments,
1965 avoid reporting another error. */
1967 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1968 return error_mark_node;
1970 switch (code)
1972 case PLUS_EXPR:
1973 /* Handle the pointer + int case. */
1974 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1975 return pointer_int_sum (PLUS_EXPR, op0, op1);
1976 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1977 return pointer_int_sum (PLUS_EXPR, op1, op0);
1978 else
1979 common = 1;
1980 break;
1982 case MINUS_EXPR:
1983 /* Subtraction of two similar pointers.
1984 We must subtract them as integers, then divide by object size. */
1985 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1986 && comp_target_types (type0, type1, 1))
1987 return pointer_diff (op0, op1);
1988 /* Handle pointer minus int. Just like pointer plus int. */
1989 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1990 return pointer_int_sum (MINUS_EXPR, op0, op1);
1991 else
1992 common = 1;
1993 break;
1995 case MULT_EXPR:
1996 common = 1;
1997 break;
1999 case TRUNC_DIV_EXPR:
2000 case CEIL_DIV_EXPR:
2001 case FLOOR_DIV_EXPR:
2002 case ROUND_DIV_EXPR:
2003 case EXACT_DIV_EXPR:
2004 /* Floating point division by zero is a legitimate way to obtain
2005 infinities and NaNs. */
2006 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2007 warning ("division by zero");
2009 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2010 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
2011 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2012 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
2014 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2015 resultcode = RDIV_EXPR;
2016 else
2017 /* Although it would be tempting to shorten always here, that
2018 loses on some targets, since the modulo instruction is
2019 undefined if the quotient can't be represented in the
2020 computation mode. We shorten only if unsigned or if
2021 dividing by something we know != -1. */
2022 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2023 || (TREE_CODE (op1) == INTEGER_CST
2024 && ! integer_all_onesp (op1)));
2025 common = 1;
2027 break;
2029 case BIT_AND_EXPR:
2030 case BIT_ANDTC_EXPR:
2031 case BIT_IOR_EXPR:
2032 case BIT_XOR_EXPR:
2033 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2034 shorten = -1;
2035 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
2036 common = 1;
2037 break;
2039 case TRUNC_MOD_EXPR:
2040 case FLOOR_MOD_EXPR:
2041 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2042 warning ("division by zero");
2044 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2046 /* Although it would be tempting to shorten always here, that loses
2047 on some targets, since the modulo instruction is undefined if the
2048 quotient can't be represented in the computation mode. We shorten
2049 only if unsigned or if dividing by something we know != -1. */
2050 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2051 || (TREE_CODE (op1) == INTEGER_CST
2052 && ! integer_all_onesp (op1)));
2053 common = 1;
2055 break;
2057 case TRUTH_ANDIF_EXPR:
2058 case TRUTH_ORIF_EXPR:
2059 case TRUTH_AND_EXPR:
2060 case TRUTH_OR_EXPR:
2061 case TRUTH_XOR_EXPR:
2062 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2063 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2064 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2065 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2067 /* Result of these operations is always an int,
2068 but that does not mean the operands should be
2069 converted to ints! */
2070 result_type = integer_type_node;
2071 op0 = c_common_truthvalue_conversion (op0);
2072 op1 = c_common_truthvalue_conversion (op1);
2073 converted = 1;
2075 break;
2077 /* Shift operations: result has same type as first operand;
2078 always convert second operand to int.
2079 Also set SHORT_SHIFT if shifting rightward. */
2081 case RSHIFT_EXPR:
2082 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2084 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2086 if (tree_int_cst_sgn (op1) < 0)
2087 warning ("right shift count is negative");
2088 else
2090 if (! integer_zerop (op1))
2091 short_shift = 1;
2093 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2094 warning ("right shift count >= width of type");
2098 /* Use the type of the value to be shifted. */
2099 result_type = type0;
2100 /* Convert the shift-count to an integer, regardless of size
2101 of value being shifted. */
2102 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2103 op1 = convert (integer_type_node, op1);
2104 /* Avoid converting op1 to result_type later. */
2105 converted = 1;
2107 break;
2109 case LSHIFT_EXPR:
2110 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2112 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2114 if (tree_int_cst_sgn (op1) < 0)
2115 warning ("left shift count is negative");
2117 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2118 warning ("left shift count >= width of type");
2121 /* Use the type of the value to be shifted. */
2122 result_type = type0;
2123 /* Convert the shift-count to an integer, regardless of size
2124 of value being shifted. */
2125 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2126 op1 = convert (integer_type_node, op1);
2127 /* Avoid converting op1 to result_type later. */
2128 converted = 1;
2130 break;
2132 case RROTATE_EXPR:
2133 case LROTATE_EXPR:
2134 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2136 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2138 if (tree_int_cst_sgn (op1) < 0)
2139 warning ("shift count is negative");
2140 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2141 warning ("shift count >= width of type");
2144 /* Use the type of the value to be shifted. */
2145 result_type = type0;
2146 /* Convert the shift-count to an integer, regardless of size
2147 of value being shifted. */
2148 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2149 op1 = convert (integer_type_node, op1);
2150 /* Avoid converting op1 to result_type later. */
2151 converted = 1;
2153 break;
2155 case EQ_EXPR:
2156 case NE_EXPR:
2157 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2158 warning ("comparing floating point with == or != is unsafe");
2159 /* Result of comparison is always int,
2160 but don't convert the args to int! */
2161 build_type = integer_type_node;
2162 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2163 || code0 == COMPLEX_TYPE
2164 || code0 == VECTOR_TYPE)
2165 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2166 || code1 == COMPLEX_TYPE
2167 || code1 == VECTOR_TYPE))
2168 short_compare = 1;
2169 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2171 tree tt0 = TREE_TYPE (type0);
2172 tree tt1 = TREE_TYPE (type1);
2173 /* Anything compares with void *. void * compares with anything.
2174 Otherwise, the targets must be compatible
2175 and both must be object or both incomplete. */
2176 if (comp_target_types (type0, type1, 1))
2177 result_type = common_type (type0, type1);
2178 else if (VOID_TYPE_P (tt0))
2180 /* op0 != orig_op0 detects the case of something
2181 whose value is 0 but which isn't a valid null ptr const. */
2182 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2183 && TREE_CODE (tt1) == FUNCTION_TYPE)
2184 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2186 else if (VOID_TYPE_P (tt1))
2188 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2189 && TREE_CODE (tt0) == FUNCTION_TYPE)
2190 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2192 else
2193 pedwarn ("comparison of distinct pointer types lacks a cast");
2195 if (result_type == NULL_TREE)
2196 result_type = ptr_type_node;
2198 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2199 && integer_zerop (op1))
2200 result_type = type0;
2201 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2202 && integer_zerop (op0))
2203 result_type = type1;
2204 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2206 result_type = type0;
2207 pedwarn ("comparison between pointer and integer");
2209 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2211 result_type = type1;
2212 pedwarn ("comparison between pointer and integer");
2214 break;
2216 case MAX_EXPR:
2217 case MIN_EXPR:
2218 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2219 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2220 shorten = 1;
2221 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2223 if (comp_target_types (type0, type1, 1))
2225 result_type = common_type (type0, type1);
2226 if (pedantic
2227 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2228 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2230 else
2232 result_type = ptr_type_node;
2233 pedwarn ("comparison of distinct pointer types lacks a cast");
2236 break;
2238 case LE_EXPR:
2239 case GE_EXPR:
2240 case LT_EXPR:
2241 case GT_EXPR:
2242 build_type = integer_type_node;
2243 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2244 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2245 short_compare = 1;
2246 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2248 if (comp_target_types (type0, type1, 1))
2250 result_type = common_type (type0, type1);
2251 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
2252 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
2253 pedwarn ("comparison of complete and incomplete pointers");
2254 else if (pedantic
2255 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2256 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2258 else
2260 result_type = ptr_type_node;
2261 pedwarn ("comparison of distinct pointer types lacks a cast");
2264 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2265 && integer_zerop (op1))
2267 result_type = type0;
2268 if (pedantic || extra_warnings)
2269 pedwarn ("ordered comparison of pointer with integer zero");
2271 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2272 && integer_zerop (op0))
2274 result_type = type1;
2275 if (pedantic)
2276 pedwarn ("ordered comparison of pointer with integer zero");
2278 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2280 result_type = type0;
2281 pedwarn ("comparison between pointer and integer");
2283 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2285 result_type = type1;
2286 pedwarn ("comparison between pointer and integer");
2288 break;
2290 case UNORDERED_EXPR:
2291 case ORDERED_EXPR:
2292 case UNLT_EXPR:
2293 case UNLE_EXPR:
2294 case UNGT_EXPR:
2295 case UNGE_EXPR:
2296 case UNEQ_EXPR:
2297 build_type = integer_type_node;
2298 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2300 error ("unordered comparison on non-floating point argument");
2301 return error_mark_node;
2303 common = 1;
2304 break;
2306 default:
2307 break;
2310 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
2311 || code0 == VECTOR_TYPE)
2313 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
2314 || code1 == VECTOR_TYPE))
2316 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2318 if (shorten || common || short_compare)
2319 result_type = common_type (type0, type1);
2321 /* For certain operations (which identify themselves by shorten != 0)
2322 if both args were extended from the same smaller type,
2323 do the arithmetic in that type and then extend.
2325 shorten !=0 and !=1 indicates a bitwise operation.
2326 For them, this optimization is safe only if
2327 both args are zero-extended or both are sign-extended.
2328 Otherwise, we might change the result.
2329 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2330 but calculated in (unsigned short) it would be (unsigned short)-1. */
2332 if (shorten && none_complex)
2334 int unsigned0, unsigned1;
2335 tree arg0 = get_narrower (op0, &unsigned0);
2336 tree arg1 = get_narrower (op1, &unsigned1);
2337 /* UNS is 1 if the operation to be done is an unsigned one. */
2338 int uns = TREE_UNSIGNED (result_type);
2339 tree type;
2341 final_type = result_type;
2343 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2344 but it *requires* conversion to FINAL_TYPE. */
2346 if ((TYPE_PRECISION (TREE_TYPE (op0))
2347 == TYPE_PRECISION (TREE_TYPE (arg0)))
2348 && TREE_TYPE (op0) != final_type)
2349 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2350 if ((TYPE_PRECISION (TREE_TYPE (op1))
2351 == TYPE_PRECISION (TREE_TYPE (arg1)))
2352 && TREE_TYPE (op1) != final_type)
2353 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2355 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2357 /* For bitwise operations, signedness of nominal type
2358 does not matter. Consider only how operands were extended. */
2359 if (shorten == -1)
2360 uns = unsigned0;
2362 /* Note that in all three cases below we refrain from optimizing
2363 an unsigned operation on sign-extended args.
2364 That would not be valid. */
2366 /* Both args variable: if both extended in same way
2367 from same width, do it in that width.
2368 Do it unsigned if args were zero-extended. */
2369 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2370 < TYPE_PRECISION (result_type))
2371 && (TYPE_PRECISION (TREE_TYPE (arg1))
2372 == TYPE_PRECISION (TREE_TYPE (arg0)))
2373 && unsigned0 == unsigned1
2374 && (unsigned0 || !uns))
2375 result_type
2376 = c_common_signed_or_unsigned_type
2377 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2378 else if (TREE_CODE (arg0) == INTEGER_CST
2379 && (unsigned1 || !uns)
2380 && (TYPE_PRECISION (TREE_TYPE (arg1))
2381 < TYPE_PRECISION (result_type))
2382 && (type
2383 = c_common_signed_or_unsigned_type (unsigned1,
2384 TREE_TYPE (arg1)),
2385 int_fits_type_p (arg0, type)))
2386 result_type = type;
2387 else if (TREE_CODE (arg1) == INTEGER_CST
2388 && (unsigned0 || !uns)
2389 && (TYPE_PRECISION (TREE_TYPE (arg0))
2390 < TYPE_PRECISION (result_type))
2391 && (type
2392 = c_common_signed_or_unsigned_type (unsigned0,
2393 TREE_TYPE (arg0)),
2394 int_fits_type_p (arg1, type)))
2395 result_type = type;
2398 /* Shifts can be shortened if shifting right. */
2400 if (short_shift)
2402 int unsigned_arg;
2403 tree arg0 = get_narrower (op0, &unsigned_arg);
2405 final_type = result_type;
2407 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2408 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2410 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2411 /* We can shorten only if the shift count is less than the
2412 number of bits in the smaller type size. */
2413 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
2414 /* We cannot drop an unsigned shift after sign-extension. */
2415 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
2417 /* Do an unsigned shift if the operand was zero-extended. */
2418 result_type
2419 = c_common_signed_or_unsigned_type (unsigned_arg,
2420 TREE_TYPE (arg0));
2421 /* Convert value-to-be-shifted to that type. */
2422 if (TREE_TYPE (op0) != result_type)
2423 op0 = convert (result_type, op0);
2424 converted = 1;
2428 /* Comparison operations are shortened too but differently.
2429 They identify themselves by setting short_compare = 1. */
2431 if (short_compare)
2433 /* Don't write &op0, etc., because that would prevent op0
2434 from being kept in a register.
2435 Instead, make copies of the our local variables and
2436 pass the copies by reference, then copy them back afterward. */
2437 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2438 enum tree_code xresultcode = resultcode;
2439 tree val
2440 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2442 if (val != 0)
2443 return val;
2445 op0 = xop0, op1 = xop1;
2446 converted = 1;
2447 resultcode = xresultcode;
2449 if (warn_sign_compare && skip_evaluation == 0)
2451 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2452 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2453 int unsignedp0, unsignedp1;
2454 tree primop0 = get_narrower (op0, &unsignedp0);
2455 tree primop1 = get_narrower (op1, &unsignedp1);
2457 xop0 = orig_op0;
2458 xop1 = orig_op1;
2459 STRIP_TYPE_NOPS (xop0);
2460 STRIP_TYPE_NOPS (xop1);
2462 /* Give warnings for comparisons between signed and unsigned
2463 quantities that may fail.
2465 Do the checking based on the original operand trees, so that
2466 casts will be considered, but default promotions won't be.
2468 Do not warn if the comparison is being done in a signed type,
2469 since the signed type will only be chosen if it can represent
2470 all the values of the unsigned type. */
2471 if (! TREE_UNSIGNED (result_type))
2472 /* OK */;
2473 /* Do not warn if both operands are the same signedness. */
2474 else if (op0_signed == op1_signed)
2475 /* OK */;
2476 else
2478 tree sop, uop;
2480 if (op0_signed)
2481 sop = xop0, uop = xop1;
2482 else
2483 sop = xop1, uop = xop0;
2485 /* Do not warn if the signed quantity is an
2486 unsuffixed integer literal (or some static
2487 constant expression involving such literals or a
2488 conditional expression involving such literals)
2489 and it is non-negative. */
2490 if (c_tree_expr_nonnegative_p (sop))
2491 /* OK */;
2492 /* Do not warn if the comparison is an equality operation,
2493 the unsigned quantity is an integral constant, and it
2494 would fit in the result if the result were signed. */
2495 else if (TREE_CODE (uop) == INTEGER_CST
2496 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2497 && int_fits_type_p
2498 (uop, c_common_signed_type (result_type)))
2499 /* OK */;
2500 /* Do not warn if the unsigned quantity is an enumeration
2501 constant and its maximum value would fit in the result
2502 if the result were signed. */
2503 else if (TREE_CODE (uop) == INTEGER_CST
2504 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2505 && int_fits_type_p
2506 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2507 c_common_signed_type (result_type)))
2508 /* OK */;
2509 else
2510 warning ("comparison between signed and unsigned");
2513 /* Warn if two unsigned values are being compared in a size
2514 larger than their original size, and one (and only one) is the
2515 result of a `~' operator. This comparison will always fail.
2517 Also warn if one operand is a constant, and the constant
2518 does not have all bits set that are set in the ~ operand
2519 when it is extended. */
2521 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2522 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2524 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2525 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2526 &unsignedp0);
2527 else
2528 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2529 &unsignedp1);
2531 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
2533 tree primop;
2534 HOST_WIDE_INT constant, mask;
2535 int unsignedp, bits;
2537 if (host_integerp (primop0, 0))
2539 primop = primop1;
2540 unsignedp = unsignedp1;
2541 constant = tree_low_cst (primop0, 0);
2543 else
2545 primop = primop0;
2546 unsignedp = unsignedp0;
2547 constant = tree_low_cst (primop1, 0);
2550 bits = TYPE_PRECISION (TREE_TYPE (primop));
2551 if (bits < TYPE_PRECISION (result_type)
2552 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
2554 mask = (~ (HOST_WIDE_INT) 0) << bits;
2555 if ((mask & constant) != mask)
2556 warning ("comparison of promoted ~unsigned with constant");
2559 else if (unsignedp0 && unsignedp1
2560 && (TYPE_PRECISION (TREE_TYPE (primop0))
2561 < TYPE_PRECISION (result_type))
2562 && (TYPE_PRECISION (TREE_TYPE (primop1))
2563 < TYPE_PRECISION (result_type)))
2564 warning ("comparison of promoted ~unsigned with unsigned");
2570 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2571 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2572 Then the expression will be built.
2573 It will be given type FINAL_TYPE if that is nonzero;
2574 otherwise, it will be given type RESULT_TYPE. */
2576 if (!result_type)
2578 binary_op_error (code);
2579 return error_mark_node;
2582 if (! converted)
2584 if (TREE_TYPE (op0) != result_type)
2585 op0 = convert (result_type, op0);
2586 if (TREE_TYPE (op1) != result_type)
2587 op1 = convert (result_type, op1);
2590 if (build_type == NULL_TREE)
2591 build_type = result_type;
2594 tree result = build (resultcode, build_type, op0, op1);
2595 tree folded;
2597 folded = fold (result);
2598 if (folded == result)
2599 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2600 if (final_type != 0)
2601 return convert (final_type, folded);
2602 return folded;
2607 /* Return true if `t' is known to be non-negative. */
2610 c_tree_expr_nonnegative_p (t)
2611 tree t;
2613 if (TREE_CODE (t) == STMT_EXPR)
2615 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2617 /* Find the last statement in the chain, ignoring the final
2618 * scope statement */
2619 while (TREE_CHAIN (t) != NULL_TREE
2620 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2621 t = TREE_CHAIN (t);
2622 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2624 return tree_expr_nonnegative_p (t);
2627 /* Return a tree for the difference of pointers OP0 and OP1.
2628 The resulting tree has type int. */
2630 static tree
2631 pointer_diff (op0, op1)
2632 tree op0, op1;
2634 tree result, folded;
2635 tree restype = ptrdiff_type_node;
2637 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2638 tree con0, con1, lit0, lit1;
2639 tree orig_op1 = op1;
2641 if (pedantic || warn_pointer_arith)
2643 if (TREE_CODE (target_type) == VOID_TYPE)
2644 pedwarn ("pointer of type `void *' used in subtraction");
2645 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2646 pedwarn ("pointer to a function used in subtraction");
2649 /* If the conversion to ptrdiff_type does anything like widening or
2650 converting a partial to an integral mode, we get a convert_expression
2651 that is in the way to do any simplifications.
2652 (fold-const.c doesn't know that the extra bits won't be needed.
2653 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2654 different mode in place.)
2655 So first try to find a common term here 'by hand'; we want to cover
2656 at least the cases that occur in legal static initializers. */
2657 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2658 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2660 if (TREE_CODE (con0) == PLUS_EXPR)
2662 lit0 = TREE_OPERAND (con0, 1);
2663 con0 = TREE_OPERAND (con0, 0);
2665 else
2666 lit0 = integer_zero_node;
2668 if (TREE_CODE (con1) == PLUS_EXPR)
2670 lit1 = TREE_OPERAND (con1, 1);
2671 con1 = TREE_OPERAND (con1, 0);
2673 else
2674 lit1 = integer_zero_node;
2676 if (operand_equal_p (con0, con1, 0))
2678 op0 = lit0;
2679 op1 = lit1;
2683 /* First do the subtraction as integers;
2684 then drop through to build the divide operator.
2685 Do not do default conversions on the minus operator
2686 in case restype is a short type. */
2688 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2689 convert (restype, op1), 0);
2690 /* This generates an error if op1 is pointer to incomplete type. */
2691 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2692 error ("arithmetic on pointer to an incomplete type");
2694 /* This generates an error if op0 is pointer to incomplete type. */
2695 op1 = c_size_in_bytes (target_type);
2697 /* Divide by the size, in easiest possible way. */
2699 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2701 folded = fold (result);
2702 if (folded == result)
2703 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2704 return folded;
2707 /* Construct and perhaps optimize a tree representation
2708 for a unary operation. CODE, a tree_code, specifies the operation
2709 and XARG is the operand.
2710 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2711 the default promotions (such as from short to int).
2712 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2713 allows non-lvalues; this is only used to handle conversion of non-lvalue
2714 arrays to pointers in C99. */
2716 tree
2717 build_unary_op (code, xarg, flag)
2718 enum tree_code code;
2719 tree xarg;
2720 int flag;
2722 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2723 tree arg = xarg;
2724 tree argtype = 0;
2725 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2726 tree val;
2727 int noconvert = flag;
2729 if (typecode == ERROR_MARK)
2730 return error_mark_node;
2731 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2732 typecode = INTEGER_TYPE;
2734 switch (code)
2736 case CONVERT_EXPR:
2737 /* This is used for unary plus, because a CONVERT_EXPR
2738 is enough to prevent anybody from looking inside for
2739 associativity, but won't generate any code. */
2740 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2741 || typecode == COMPLEX_TYPE))
2743 error ("wrong type argument to unary plus");
2744 return error_mark_node;
2746 else if (!noconvert)
2747 arg = default_conversion (arg);
2748 arg = non_lvalue (arg);
2749 break;
2751 case NEGATE_EXPR:
2752 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2753 || typecode == COMPLEX_TYPE
2754 || typecode == VECTOR_TYPE))
2756 error ("wrong type argument to unary minus");
2757 return error_mark_node;
2759 else if (!noconvert)
2760 arg = default_conversion (arg);
2761 break;
2763 case BIT_NOT_EXPR:
2764 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2766 if (!noconvert)
2767 arg = default_conversion (arg);
2769 else if (typecode == COMPLEX_TYPE)
2771 code = CONJ_EXPR;
2772 if (pedantic)
2773 pedwarn ("ISO C does not support `~' for complex conjugation");
2774 if (!noconvert)
2775 arg = default_conversion (arg);
2777 else
2779 error ("wrong type argument to bit-complement");
2780 return error_mark_node;
2782 break;
2784 case ABS_EXPR:
2785 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2786 || typecode == COMPLEX_TYPE))
2788 error ("wrong type argument to abs");
2789 return error_mark_node;
2791 else if (!noconvert)
2792 arg = default_conversion (arg);
2793 break;
2795 case CONJ_EXPR:
2796 /* Conjugating a real value is a no-op, but allow it anyway. */
2797 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2798 || typecode == COMPLEX_TYPE))
2800 error ("wrong type argument to conjugation");
2801 return error_mark_node;
2803 else if (!noconvert)
2804 arg = default_conversion (arg);
2805 break;
2807 case TRUTH_NOT_EXPR:
2808 if (typecode != INTEGER_TYPE
2809 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2810 && typecode != COMPLEX_TYPE
2811 /* These will convert to a pointer. */
2812 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2814 error ("wrong type argument to unary exclamation mark");
2815 return error_mark_node;
2817 arg = c_common_truthvalue_conversion (arg);
2818 return invert_truthvalue (arg);
2820 case NOP_EXPR:
2821 break;
2823 case REALPART_EXPR:
2824 if (TREE_CODE (arg) == COMPLEX_CST)
2825 return TREE_REALPART (arg);
2826 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2827 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2828 else
2829 return arg;
2831 case IMAGPART_EXPR:
2832 if (TREE_CODE (arg) == COMPLEX_CST)
2833 return TREE_IMAGPART (arg);
2834 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2835 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2836 else
2837 return convert (TREE_TYPE (arg), integer_zero_node);
2839 case PREINCREMENT_EXPR:
2840 case POSTINCREMENT_EXPR:
2841 case PREDECREMENT_EXPR:
2842 case POSTDECREMENT_EXPR:
2843 /* Handle complex lvalues (when permitted)
2844 by reduction to simpler cases. */
2846 val = unary_complex_lvalue (code, arg, 0);
2847 if (val != 0)
2848 return val;
2850 /* Increment or decrement the real part of the value,
2851 and don't change the imaginary part. */
2852 if (typecode == COMPLEX_TYPE)
2854 tree real, imag;
2856 if (pedantic)
2857 pedwarn ("ISO C does not support `++' and `--' on complex types");
2859 arg = stabilize_reference (arg);
2860 real = build_unary_op (REALPART_EXPR, arg, 1);
2861 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2862 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2863 build_unary_op (code, real, 1), imag);
2866 /* Report invalid types. */
2868 if (typecode != POINTER_TYPE
2869 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2871 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2872 error ("wrong type argument to increment");
2873 else
2874 error ("wrong type argument to decrement");
2876 return error_mark_node;
2880 tree inc;
2881 tree result_type = TREE_TYPE (arg);
2883 arg = get_unwidened (arg, 0);
2884 argtype = TREE_TYPE (arg);
2886 /* Compute the increment. */
2888 if (typecode == POINTER_TYPE)
2890 /* If pointer target is an undefined struct,
2891 we just cannot know how to do the arithmetic. */
2892 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2894 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2895 error ("increment of pointer to unknown structure");
2896 else
2897 error ("decrement of pointer to unknown structure");
2899 else if ((pedantic || warn_pointer_arith)
2900 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2901 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2903 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2904 pedwarn ("wrong type argument to increment");
2905 else
2906 pedwarn ("wrong type argument to decrement");
2909 inc = c_size_in_bytes (TREE_TYPE (result_type));
2911 else
2912 inc = integer_one_node;
2914 inc = convert (argtype, inc);
2916 /* Handle incrementing a cast-expression. */
2918 while (1)
2919 switch (TREE_CODE (arg))
2921 case NOP_EXPR:
2922 case CONVERT_EXPR:
2923 case FLOAT_EXPR:
2924 case FIX_TRUNC_EXPR:
2925 case FIX_FLOOR_EXPR:
2926 case FIX_ROUND_EXPR:
2927 case FIX_CEIL_EXPR:
2928 pedantic_lvalue_warning (CONVERT_EXPR);
2929 /* If the real type has the same machine representation
2930 as the type it is cast to, we can make better output
2931 by adding directly to the inside of the cast. */
2932 if ((TREE_CODE (TREE_TYPE (arg))
2933 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2934 && (TYPE_MODE (TREE_TYPE (arg))
2935 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2936 arg = TREE_OPERAND (arg, 0);
2937 else
2939 tree incremented, modify, value;
2940 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2941 value = boolean_increment (code, arg);
2942 else
2944 arg = stabilize_reference (arg);
2945 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2946 value = arg;
2947 else
2948 value = save_expr (arg);
2949 incremented = build (((code == PREINCREMENT_EXPR
2950 || code == POSTINCREMENT_EXPR)
2951 ? PLUS_EXPR : MINUS_EXPR),
2952 argtype, value, inc);
2953 TREE_SIDE_EFFECTS (incremented) = 1;
2954 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2955 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2957 TREE_USED (value) = 1;
2958 return value;
2960 break;
2962 default:
2963 goto give_up;
2965 give_up:
2967 /* Complain about anything else that is not a true lvalue. */
2968 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2969 || code == POSTINCREMENT_EXPR)
2970 ? "invalid lvalue in increment"
2971 : "invalid lvalue in decrement")))
2972 return error_mark_node;
2974 /* Report a read-only lvalue. */
2975 if (TREE_READONLY (arg))
2976 readonly_warning (arg,
2977 ((code == PREINCREMENT_EXPR
2978 || code == POSTINCREMENT_EXPR)
2979 ? "increment" : "decrement"));
2981 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2982 val = boolean_increment (code, arg);
2983 else
2984 val = build (code, TREE_TYPE (arg), arg, inc);
2985 TREE_SIDE_EFFECTS (val) = 1;
2986 val = convert (result_type, val);
2987 if (TREE_CODE (val) != code)
2988 TREE_NO_UNUSED_WARNING (val) = 1;
2989 return val;
2992 case ADDR_EXPR:
2993 /* Note that this operation never does default_conversion. */
2995 /* Let &* cancel out to simplify resulting code. */
2996 if (TREE_CODE (arg) == INDIRECT_REF)
2998 /* Don't let this be an lvalue. */
2999 if (lvalue_p (TREE_OPERAND (arg, 0)))
3000 return non_lvalue (TREE_OPERAND (arg, 0));
3001 return TREE_OPERAND (arg, 0);
3004 /* For &x[y], return x+y */
3005 if (TREE_CODE (arg) == ARRAY_REF)
3007 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
3008 return error_mark_node;
3009 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3010 TREE_OPERAND (arg, 1), 1);
3013 /* Handle complex lvalues (when permitted)
3014 by reduction to simpler cases. */
3015 val = unary_complex_lvalue (code, arg, flag);
3016 if (val != 0)
3017 return val;
3019 /* Anything not already handled and not a true memory reference
3020 or a non-lvalue array is an error. */
3021 else if (typecode != FUNCTION_TYPE && !flag
3022 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
3023 return error_mark_node;
3025 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3026 argtype = TREE_TYPE (arg);
3028 /* If the lvalue is const or volatile, merge that into the type
3029 to which the address will point. Note that you can't get a
3030 restricted pointer by taking the address of something, so we
3031 only have to deal with `const' and `volatile' here. */
3032 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3033 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3034 argtype = c_build_type_variant (argtype,
3035 TREE_READONLY (arg),
3036 TREE_THIS_VOLATILE (arg));
3038 argtype = build_pointer_type (argtype);
3040 if (!c_mark_addressable (arg))
3041 return error_mark_node;
3044 tree addr;
3046 if (TREE_CODE (arg) == COMPONENT_REF)
3048 tree field = TREE_OPERAND (arg, 1);
3050 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
3052 if (DECL_C_BIT_FIELD (field))
3054 error ("attempt to take address of bit-field structure member `%s'",
3055 IDENTIFIER_POINTER (DECL_NAME (field)));
3056 return error_mark_node;
3059 addr = fold (build (PLUS_EXPR, argtype,
3060 convert (argtype, addr),
3061 convert (argtype, byte_position (field))));
3063 else
3064 addr = build1 (code, argtype, arg);
3066 /* Address of a static or external variable or
3067 file-scope function counts as a constant. */
3068 if (staticp (arg)
3069 && ! (TREE_CODE (arg) == FUNCTION_DECL
3070 && DECL_CONTEXT (arg) != 0))
3071 TREE_CONSTANT (addr) = 1;
3072 return addr;
3075 default:
3076 break;
3079 if (argtype == 0)
3080 argtype = TREE_TYPE (arg);
3081 return fold (build1 (code, argtype, arg));
3084 /* Return nonzero if REF is an lvalue valid for this language.
3085 Lvalues can be assigned, unless their type has TYPE_READONLY.
3086 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3089 lvalue_p (ref)
3090 tree ref;
3092 enum tree_code code = TREE_CODE (ref);
3094 switch (code)
3096 case REALPART_EXPR:
3097 case IMAGPART_EXPR:
3098 case COMPONENT_REF:
3099 return lvalue_p (TREE_OPERAND (ref, 0));
3101 case COMPOUND_LITERAL_EXPR:
3102 case STRING_CST:
3103 return 1;
3105 case INDIRECT_REF:
3106 case ARRAY_REF:
3107 case VAR_DECL:
3108 case PARM_DECL:
3109 case RESULT_DECL:
3110 case ERROR_MARK:
3111 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3112 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3114 case BIND_EXPR:
3115 case RTL_EXPR:
3116 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3118 default:
3119 return 0;
3123 /* Return nonzero if REF is an lvalue valid for this language;
3124 otherwise, print an error message and return zero. */
3127 lvalue_or_else (ref, msgid)
3128 tree ref;
3129 const char *msgid;
3131 int win = lvalue_p (ref);
3133 if (! win)
3134 error ("%s", msgid);
3136 return win;
3139 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3140 for certain kinds of expressions which are not really lvalues
3141 but which we can accept as lvalues. If FLAG is nonzero, then
3142 non-lvalues are OK since we may be converting a non-lvalue array to
3143 a pointer in C99.
3145 If ARG is not a kind of expression we can handle, return zero. */
3147 static tree
3148 unary_complex_lvalue (code, arg, flag)
3149 enum tree_code code;
3150 tree arg;
3151 int flag;
3153 /* Handle (a, b) used as an "lvalue". */
3154 if (TREE_CODE (arg) == COMPOUND_EXPR)
3156 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3158 /* If this returns a function type, it isn't really being used as
3159 an lvalue, so don't issue a warning about it. */
3160 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3161 pedantic_lvalue_warning (COMPOUND_EXPR);
3163 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3164 TREE_OPERAND (arg, 0), real_result);
3167 /* Handle (a ? b : c) used as an "lvalue". */
3168 if (TREE_CODE (arg) == COND_EXPR)
3170 if (!flag)
3171 pedantic_lvalue_warning (COND_EXPR);
3172 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3173 pedantic_lvalue_warning (COMPOUND_EXPR);
3175 return (build_conditional_expr
3176 (TREE_OPERAND (arg, 0),
3177 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
3178 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
3181 return 0;
3184 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3185 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3187 static void
3188 pedantic_lvalue_warning (code)
3189 enum tree_code code;
3191 if (pedantic)
3192 switch (code)
3194 case COND_EXPR:
3195 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
3196 break;
3197 case COMPOUND_EXPR:
3198 pedwarn ("ISO C forbids use of compound expressions as lvalues");
3199 break;
3200 default:
3201 pedwarn ("ISO C forbids use of cast expressions as lvalues");
3202 break;
3206 /* Warn about storing in something that is `const'. */
3208 void
3209 readonly_warning (arg, msgid)
3210 tree arg;
3211 const char *msgid;
3213 if (TREE_CODE (arg) == COMPONENT_REF)
3215 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3216 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3217 else
3218 pedwarn ("%s of read-only member `%s'", _(msgid),
3219 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3221 else if (TREE_CODE (arg) == VAR_DECL)
3222 pedwarn ("%s of read-only variable `%s'", _(msgid),
3223 IDENTIFIER_POINTER (DECL_NAME (arg)));
3224 else
3225 pedwarn ("%s of read-only location", _(msgid));
3228 /* Mark EXP saying that we need to be able to take the
3229 address of it; it should not be allocated in a register.
3230 Returns true if successful. */
3232 bool
3233 c_mark_addressable (exp)
3234 tree exp;
3236 tree x = exp;
3238 while (1)
3239 switch (TREE_CODE (x))
3241 case COMPONENT_REF:
3242 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3244 error ("cannot take address of bit-field `%s'",
3245 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3246 return false;
3249 /* ... fall through ... */
3251 case ADDR_EXPR:
3252 case ARRAY_REF:
3253 case REALPART_EXPR:
3254 case IMAGPART_EXPR:
3255 x = TREE_OPERAND (x, 0);
3256 break;
3258 case COMPOUND_LITERAL_EXPR:
3259 case CONSTRUCTOR:
3260 TREE_ADDRESSABLE (x) = 1;
3261 return true;
3263 case VAR_DECL:
3264 case CONST_DECL:
3265 case PARM_DECL:
3266 case RESULT_DECL:
3267 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3268 && DECL_NONLOCAL (x))
3270 if (TREE_PUBLIC (x))
3272 error ("global register variable `%s' used in nested function",
3273 IDENTIFIER_POINTER (DECL_NAME (x)));
3274 return false;
3276 pedwarn ("register variable `%s' used in nested function",
3277 IDENTIFIER_POINTER (DECL_NAME (x)));
3279 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3281 if (TREE_PUBLIC (x))
3283 error ("address of global register variable `%s' requested",
3284 IDENTIFIER_POINTER (DECL_NAME (x)));
3285 return false;
3288 /* If we are making this addressable due to its having
3289 volatile components, give a different error message. Also
3290 handle the case of an unnamed parameter by not trying
3291 to give the name. */
3293 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3295 error ("cannot put object with volatile field into register");
3296 return false;
3299 pedwarn ("address of register variable `%s' requested",
3300 IDENTIFIER_POINTER (DECL_NAME (x)));
3302 put_var_into_stack (x, /*rescan=*/true);
3304 /* drops in */
3305 case FUNCTION_DECL:
3306 TREE_ADDRESSABLE (x) = 1;
3307 default:
3308 return true;
3312 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3314 tree
3315 build_conditional_expr (ifexp, op1, op2)
3316 tree ifexp, op1, op2;
3318 tree type1;
3319 tree type2;
3320 enum tree_code code1;
3321 enum tree_code code2;
3322 tree result_type = NULL;
3323 tree orig_op1 = op1, orig_op2 = op2;
3325 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
3327 /* Promote both alternatives. */
3329 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3330 op1 = default_conversion (op1);
3331 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3332 op2 = default_conversion (op2);
3334 if (TREE_CODE (ifexp) == ERROR_MARK
3335 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3336 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3337 return error_mark_node;
3339 type1 = TREE_TYPE (op1);
3340 code1 = TREE_CODE (type1);
3341 type2 = TREE_TYPE (op2);
3342 code2 = TREE_CODE (type2);
3344 /* Quickly detect the usual case where op1 and op2 have the same type
3345 after promotion. */
3346 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3348 if (type1 == type2)
3349 result_type = type1;
3350 else
3351 result_type = TYPE_MAIN_VARIANT (type1);
3353 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3354 || code1 == COMPLEX_TYPE)
3355 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3356 || code2 == COMPLEX_TYPE))
3358 result_type = common_type (type1, type2);
3360 /* If -Wsign-compare, warn here if type1 and type2 have
3361 different signedness. We'll promote the signed to unsigned
3362 and later code won't know it used to be different.
3363 Do this check on the original types, so that explicit casts
3364 will be considered, but default promotions won't. */
3365 if (warn_sign_compare && !skip_evaluation)
3367 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3368 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3370 if (unsigned_op1 ^ unsigned_op2)
3372 /* Do not warn if the result type is signed, since the
3373 signed type will only be chosen if it can represent
3374 all the values of the unsigned type. */
3375 if (! TREE_UNSIGNED (result_type))
3376 /* OK */;
3377 /* Do not warn if the signed quantity is an unsuffixed
3378 integer literal (or some static constant expression
3379 involving such literals) and it is non-negative. */
3380 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
3381 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
3382 /* OK */;
3383 else
3384 warning ("signed and unsigned type in conditional expression");
3388 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3390 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3391 pedwarn ("ISO C forbids conditional expr with only one void side");
3392 result_type = void_type_node;
3394 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3396 if (comp_target_types (type1, type2, 1))
3397 result_type = common_type (type1, type2);
3398 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3399 && TREE_CODE (orig_op1) != NOP_EXPR)
3400 result_type = qualify_type (type2, type1);
3401 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3402 && TREE_CODE (orig_op2) != NOP_EXPR)
3403 result_type = qualify_type (type1, type2);
3404 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3406 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3407 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3408 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3409 TREE_TYPE (type2)));
3411 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3413 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3414 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3415 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3416 TREE_TYPE (type1)));
3418 else
3420 pedwarn ("pointer type mismatch in conditional expression");
3421 result_type = build_pointer_type (void_type_node);
3424 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3426 if (! integer_zerop (op2))
3427 pedwarn ("pointer/integer type mismatch in conditional expression");
3428 else
3430 op2 = null_pointer_node;
3432 result_type = type1;
3434 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3436 if (!integer_zerop (op1))
3437 pedwarn ("pointer/integer type mismatch in conditional expression");
3438 else
3440 op1 = null_pointer_node;
3442 result_type = type2;
3445 if (!result_type)
3447 if (flag_cond_mismatch)
3448 result_type = void_type_node;
3449 else
3451 error ("type mismatch in conditional expression");
3452 return error_mark_node;
3456 /* Merge const and volatile flags of the incoming types. */
3457 result_type
3458 = build_type_variant (result_type,
3459 TREE_READONLY (op1) || TREE_READONLY (op2),
3460 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3462 if (result_type != TREE_TYPE (op1))
3463 op1 = convert_and_check (result_type, op1);
3464 if (result_type != TREE_TYPE (op2))
3465 op2 = convert_and_check (result_type, op2);
3467 if (TREE_CODE (ifexp) == INTEGER_CST)
3468 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3470 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3473 /* Given a list of expressions, return a compound expression
3474 that performs them all and returns the value of the last of them. */
3476 tree
3477 build_compound_expr (list)
3478 tree list;
3480 return internal_build_compound_expr (list, TRUE);
3483 static tree
3484 internal_build_compound_expr (list, first_p)
3485 tree list;
3486 int first_p;
3488 tree rest;
3490 if (TREE_CHAIN (list) == 0)
3492 /* Convert arrays and functions to pointers when there
3493 really is a comma operator. */
3494 if (!first_p)
3495 TREE_VALUE (list)
3496 = default_function_array_conversion (TREE_VALUE (list));
3498 /* Don't let (0, 0) be null pointer constant. */
3499 if (!first_p && integer_zerop (TREE_VALUE (list)))
3500 return non_lvalue (TREE_VALUE (list));
3501 return TREE_VALUE (list);
3504 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3506 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3508 /* The left-hand operand of a comma expression is like an expression
3509 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3510 any side-effects, unless it was explicitly cast to (void). */
3511 if (warn_unused_value
3512 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3513 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
3514 warning ("left-hand operand of comma expression has no effect");
3516 /* When pedantic, a compound expression can be neither an lvalue
3517 nor an integer constant expression. */
3518 if (! pedantic)
3519 return rest;
3522 /* With -Wunused, we should also warn if the left-hand operand does have
3523 side-effects, but computes a value which is not used. For example, in
3524 `foo() + bar(), baz()' the result of the `+' operator is not used,
3525 so we should issue a warning. */
3526 else if (warn_unused_value)
3527 warn_if_unused_value (TREE_VALUE (list));
3529 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3532 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3534 tree
3535 build_c_cast (type, expr)
3536 tree type;
3537 tree expr;
3539 tree value = expr;
3541 if (type == error_mark_node || expr == error_mark_node)
3542 return error_mark_node;
3544 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3545 only in <protocol> qualifications. But when constructing cast expressions,
3546 the protocols do matter and must be kept around. */
3547 if (!flag_objc || !objc_is_id (type))
3548 type = TYPE_MAIN_VARIANT (type);
3550 if (TREE_CODE (type) == ARRAY_TYPE)
3552 error ("cast specifies array type");
3553 return error_mark_node;
3556 if (TREE_CODE (type) == FUNCTION_TYPE)
3558 error ("cast specifies function type");
3559 return error_mark_node;
3562 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3564 if (pedantic)
3566 if (TREE_CODE (type) == RECORD_TYPE
3567 || TREE_CODE (type) == UNION_TYPE)
3568 pedwarn ("ISO C forbids casting nonscalar to the same type");
3571 else if (TREE_CODE (type) == UNION_TYPE)
3573 tree field;
3574 value = default_function_array_conversion (value);
3576 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3577 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3578 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3579 break;
3581 if (field)
3583 tree t;
3585 if (pedantic)
3586 pedwarn ("ISO C forbids casts to union type");
3587 t = digest_init (type,
3588 build_constructor (type,
3589 build_tree_list (field, value)),
3591 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3592 return t;
3594 error ("cast to union type from type not present in union");
3595 return error_mark_node;
3597 else
3599 tree otype, ovalue;
3601 /* If casting to void, avoid the error that would come
3602 from default_conversion in the case of a non-lvalue array. */
3603 if (type == void_type_node)
3604 return build1 (CONVERT_EXPR, type, value);
3606 /* Convert functions and arrays to pointers,
3607 but don't convert any other types. */
3608 value = default_function_array_conversion (value);
3609 otype = TREE_TYPE (value);
3611 /* Optionally warn about potentially worrisome casts. */
3613 if (warn_cast_qual
3614 && TREE_CODE (type) == POINTER_TYPE
3615 && TREE_CODE (otype) == POINTER_TYPE)
3617 tree in_type = type;
3618 tree in_otype = otype;
3619 int added = 0;
3620 int discarded = 0;
3622 /* Check that the qualifiers on IN_TYPE are a superset of
3623 the qualifiers of IN_OTYPE. The outermost level of
3624 POINTER_TYPE nodes is uninteresting and we stop as soon
3625 as we hit a non-POINTER_TYPE node on either type. */
3628 in_otype = TREE_TYPE (in_otype);
3629 in_type = TREE_TYPE (in_type);
3631 /* GNU C allows cv-qualified function types. 'const'
3632 means the function is very pure, 'volatile' means it
3633 can't return. We need to warn when such qualifiers
3634 are added, not when they're taken away. */
3635 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3636 && TREE_CODE (in_type) == FUNCTION_TYPE)
3637 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3638 else
3639 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3641 while (TREE_CODE (in_type) == POINTER_TYPE
3642 && TREE_CODE (in_otype) == POINTER_TYPE);
3644 if (added)
3645 warning ("cast adds new qualifiers to function type");
3647 if (discarded)
3648 /* There are qualifiers present in IN_OTYPE that are not
3649 present in IN_TYPE. */
3650 warning ("cast discards qualifiers from pointer target type");
3653 /* Warn about possible alignment problems. */
3654 if (STRICT_ALIGNMENT && warn_cast_align
3655 && TREE_CODE (type) == POINTER_TYPE
3656 && TREE_CODE (otype) == POINTER_TYPE
3657 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3658 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3659 /* Don't warn about opaque types, where the actual alignment
3660 restriction is unknown. */
3661 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3662 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3663 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3664 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3665 warning ("cast increases required alignment of target type");
3667 if (TREE_CODE (type) == INTEGER_TYPE
3668 && TREE_CODE (otype) == POINTER_TYPE
3669 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3670 && !TREE_CONSTANT (value))
3671 warning ("cast from pointer to integer of different size");
3673 if (warn_bad_function_cast
3674 && TREE_CODE (value) == CALL_EXPR
3675 && TREE_CODE (type) != TREE_CODE (otype))
3676 warning ("cast does not match function type");
3678 if (TREE_CODE (type) == POINTER_TYPE
3679 && TREE_CODE (otype) == INTEGER_TYPE
3680 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3681 /* Don't warn about converting any constant. */
3682 && !TREE_CONSTANT (value))
3683 warning ("cast to pointer from integer of different size");
3685 if (TREE_CODE (type) == POINTER_TYPE
3686 && TREE_CODE (otype) == POINTER_TYPE
3687 && TREE_CODE (expr) == ADDR_EXPR
3688 && DECL_P (TREE_OPERAND (expr, 0))
3689 && flag_strict_aliasing && warn_strict_aliasing
3690 && !VOID_TYPE_P (TREE_TYPE (type)))
3692 /* Casting the address of a decl to non void pointer. Warn
3693 if the cast breaks type based aliasing. */
3694 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3695 warning ("type-punning to incomplete type might break strict-aliasing rules");
3696 else if (!alias_sets_conflict_p
3697 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
3698 get_alias_set (TREE_TYPE (type))))
3699 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3702 ovalue = value;
3703 /* Replace a nonvolatile const static variable with its value. */
3704 if (optimize && TREE_CODE (value) == VAR_DECL)
3705 value = decl_constant_value (value);
3706 value = convert (type, value);
3708 /* Ignore any integer overflow caused by the cast. */
3709 if (TREE_CODE (value) == INTEGER_CST)
3711 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3712 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3716 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3717 if (pedantic && TREE_CODE (value) == INTEGER_CST
3718 && TREE_CODE (expr) == INTEGER_CST
3719 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3720 value = non_lvalue (value);
3722 /* If pedantic, don't let a cast be an lvalue. */
3723 if (value == expr && pedantic)
3724 value = non_lvalue (value);
3726 return value;
3729 /* Interpret a cast of expression EXPR to type TYPE. */
3730 tree
3731 c_cast_expr (type, expr)
3732 tree type, expr;
3734 int saved_wsp = warn_strict_prototypes;
3736 /* This avoids warnings about unprototyped casts on
3737 integers. E.g. "#define SIG_DFL (void(*)())0". */
3738 if (TREE_CODE (expr) == INTEGER_CST)
3739 warn_strict_prototypes = 0;
3740 type = groktypename (type);
3741 warn_strict_prototypes = saved_wsp;
3743 return build_c_cast (type, expr);
3747 /* Build an assignment expression of lvalue LHS from value RHS.
3748 MODIFYCODE is the code for a binary operator that we use
3749 to combine the old value of LHS with RHS to get the new value.
3750 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3752 tree
3753 build_modify_expr (lhs, modifycode, rhs)
3754 tree lhs, rhs;
3755 enum tree_code modifycode;
3757 tree result;
3758 tree newrhs;
3759 tree lhstype = TREE_TYPE (lhs);
3760 tree olhstype = lhstype;
3762 /* Types that aren't fully specified cannot be used in assignments. */
3763 lhs = require_complete_type (lhs);
3765 /* Avoid duplicate error messages from operands that had errors. */
3766 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3767 return error_mark_node;
3769 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3770 /* Do not use STRIP_NOPS here. We do not want an enumerator
3771 whose value is 0 to count as a null pointer constant. */
3772 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3773 rhs = TREE_OPERAND (rhs, 0);
3775 newrhs = rhs;
3777 /* Handle control structure constructs used as "lvalues". */
3779 switch (TREE_CODE (lhs))
3781 /* Handle (a, b) used as an "lvalue". */
3782 case COMPOUND_EXPR:
3783 pedantic_lvalue_warning (COMPOUND_EXPR);
3784 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3785 if (TREE_CODE (newrhs) == ERROR_MARK)
3786 return error_mark_node;
3787 return build (COMPOUND_EXPR, lhstype,
3788 TREE_OPERAND (lhs, 0), newrhs);
3790 /* Handle (a ? b : c) used as an "lvalue". */
3791 case COND_EXPR:
3792 pedantic_lvalue_warning (COND_EXPR);
3793 rhs = save_expr (rhs);
3795 /* Produce (a ? (b = rhs) : (c = rhs))
3796 except that the RHS goes through a save-expr
3797 so the code to compute it is only emitted once. */
3798 tree cond
3799 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3800 build_modify_expr (TREE_OPERAND (lhs, 1),
3801 modifycode, rhs),
3802 build_modify_expr (TREE_OPERAND (lhs, 2),
3803 modifycode, rhs));
3804 if (TREE_CODE (cond) == ERROR_MARK)
3805 return cond;
3806 /* Make sure the code to compute the rhs comes out
3807 before the split. */
3808 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3809 /* But cast it to void to avoid an "unused" error. */
3810 convert (void_type_node, rhs), cond);
3812 default:
3813 break;
3816 /* If a binary op has been requested, combine the old LHS value with the RHS
3817 producing the value we should actually store into the LHS. */
3819 if (modifycode != NOP_EXPR)
3821 lhs = stabilize_reference (lhs);
3822 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3825 /* Handle a cast used as an "lvalue".
3826 We have already performed any binary operator using the value as cast.
3827 Now convert the result to the cast type of the lhs,
3828 and then true type of the lhs and store it there;
3829 then convert result back to the cast type to be the value
3830 of the assignment. */
3832 switch (TREE_CODE (lhs))
3834 case NOP_EXPR:
3835 case CONVERT_EXPR:
3836 case FLOAT_EXPR:
3837 case FIX_TRUNC_EXPR:
3838 case FIX_FLOOR_EXPR:
3839 case FIX_ROUND_EXPR:
3840 case FIX_CEIL_EXPR:
3841 newrhs = default_function_array_conversion (newrhs);
3843 tree inner_lhs = TREE_OPERAND (lhs, 0);
3844 tree result;
3845 result = build_modify_expr (inner_lhs, NOP_EXPR,
3846 convert (TREE_TYPE (inner_lhs),
3847 convert (lhstype, newrhs)));
3848 if (TREE_CODE (result) == ERROR_MARK)
3849 return result;
3850 pedantic_lvalue_warning (CONVERT_EXPR);
3851 return convert (TREE_TYPE (lhs), result);
3854 default:
3855 break;
3858 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3859 Reject anything strange now. */
3861 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3862 return error_mark_node;
3864 /* Warn about storing in something that is `const'. */
3866 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3867 || ((TREE_CODE (lhstype) == RECORD_TYPE
3868 || TREE_CODE (lhstype) == UNION_TYPE)
3869 && C_TYPE_FIELDS_READONLY (lhstype)))
3870 readonly_warning (lhs, "assignment");
3872 /* If storing into a structure or union member,
3873 it has probably been given type `int'.
3874 Compute the type that would go with
3875 the actual amount of storage the member occupies. */
3877 if (TREE_CODE (lhs) == COMPONENT_REF
3878 && (TREE_CODE (lhstype) == INTEGER_TYPE
3879 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3880 || TREE_CODE (lhstype) == REAL_TYPE
3881 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3882 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3884 /* If storing in a field that is in actuality a short or narrower than one,
3885 we must store in the field in its actual type. */
3887 if (lhstype != TREE_TYPE (lhs))
3889 lhs = copy_node (lhs);
3890 TREE_TYPE (lhs) = lhstype;
3893 /* Convert new value to destination type. */
3895 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3896 NULL_TREE, NULL_TREE, 0);
3897 if (TREE_CODE (newrhs) == ERROR_MARK)
3898 return error_mark_node;
3900 /* Scan operands */
3902 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3903 TREE_SIDE_EFFECTS (result) = 1;
3905 /* If we got the LHS in a different type for storing in,
3906 convert the result back to the nominal type of LHS
3907 so that the value we return always has the same type
3908 as the LHS argument. */
3910 if (olhstype == TREE_TYPE (result))
3911 return result;
3912 return convert_for_assignment (olhstype, result, _("assignment"),
3913 NULL_TREE, NULL_TREE, 0);
3916 /* Convert value RHS to type TYPE as preparation for an assignment
3917 to an lvalue of type TYPE.
3918 The real work of conversion is done by `convert'.
3919 The purpose of this function is to generate error messages
3920 for assignments that are not allowed in C.
3921 ERRTYPE is a string to use in error messages:
3922 "assignment", "return", etc. If it is null, this is parameter passing
3923 for a function call (and different error messages are output).
3925 FUNNAME is the name of the function being called,
3926 as an IDENTIFIER_NODE, or null.
3927 PARMNUM is the number of the argument, for printing in error messages. */
3929 static tree
3930 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3931 tree type, rhs;
3932 const char *errtype;
3933 tree fundecl, funname;
3934 int parmnum;
3936 enum tree_code codel = TREE_CODE (type);
3937 tree rhstype;
3938 enum tree_code coder;
3940 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3941 /* Do not use STRIP_NOPS here. We do not want an enumerator
3942 whose value is 0 to count as a null pointer constant. */
3943 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3944 rhs = TREE_OPERAND (rhs, 0);
3946 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3947 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3948 rhs = default_conversion (rhs);
3949 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3950 rhs = decl_constant_value_for_broken_optimization (rhs);
3952 rhstype = TREE_TYPE (rhs);
3953 coder = TREE_CODE (rhstype);
3955 if (coder == ERROR_MARK)
3956 return error_mark_node;
3958 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3960 overflow_warning (rhs);
3961 /* Check for Objective-C protocols. This will automatically
3962 issue a warning if there are protocol violations. No need to
3963 use the return value. */
3964 if (flag_objc)
3965 objc_comptypes (type, rhstype, 0);
3966 return rhs;
3969 if (coder == VOID_TYPE)
3971 error ("void value not ignored as it ought to be");
3972 return error_mark_node;
3974 /* A type converts to a reference to it.
3975 This code doesn't fully support references, it's just for the
3976 special case of va_start and va_copy. */
3977 if (codel == REFERENCE_TYPE
3978 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3980 if (!lvalue_p (rhs))
3982 error ("cannot pass rvalue to reference parameter");
3983 return error_mark_node;
3985 if (!c_mark_addressable (rhs))
3986 return error_mark_node;
3987 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3989 /* We already know that these two types are compatible, but they
3990 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3991 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3992 likely to be va_list, a typedef to __builtin_va_list, which
3993 is different enough that it will cause problems later. */
3994 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3995 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3997 rhs = build1 (NOP_EXPR, type, rhs);
3998 return rhs;
4000 /* Some types can interconvert without explicit casts. */
4001 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4002 && ((*targetm.vector_opaque_p) (type)
4003 || (*targetm.vector_opaque_p) (rhstype)))
4004 return convert (type, rhs);
4005 /* Arithmetic types all interconvert, and enum is treated like int. */
4006 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4007 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4008 || codel == BOOLEAN_TYPE)
4009 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4010 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4011 || coder == BOOLEAN_TYPE))
4012 return convert_and_check (type, rhs);
4014 /* Conversion to a transparent union from its member types.
4015 This applies only to function arguments. */
4016 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4018 tree memb_types;
4019 tree marginal_memb_type = 0;
4021 for (memb_types = TYPE_FIELDS (type); memb_types;
4022 memb_types = TREE_CHAIN (memb_types))
4024 tree memb_type = TREE_TYPE (memb_types);
4026 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4027 TYPE_MAIN_VARIANT (rhstype)))
4028 break;
4030 if (TREE_CODE (memb_type) != POINTER_TYPE)
4031 continue;
4033 if (coder == POINTER_TYPE)
4035 tree ttl = TREE_TYPE (memb_type);
4036 tree ttr = TREE_TYPE (rhstype);
4038 /* Any non-function converts to a [const][volatile] void *
4039 and vice versa; otherwise, targets must be the same.
4040 Meanwhile, the lhs target must have all the qualifiers of
4041 the rhs. */
4042 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4043 || comp_target_types (memb_type, rhstype, 0))
4045 /* If this type won't generate any warnings, use it. */
4046 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4047 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4048 && TREE_CODE (ttl) == FUNCTION_TYPE)
4049 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4050 == TYPE_QUALS (ttr))
4051 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4052 == TYPE_QUALS (ttl))))
4053 break;
4055 /* Keep looking for a better type, but remember this one. */
4056 if (! marginal_memb_type)
4057 marginal_memb_type = memb_type;
4061 /* Can convert integer zero to any pointer type. */
4062 if (integer_zerop (rhs)
4063 || (TREE_CODE (rhs) == NOP_EXPR
4064 && integer_zerop (TREE_OPERAND (rhs, 0))))
4066 rhs = null_pointer_node;
4067 break;
4071 if (memb_types || marginal_memb_type)
4073 if (! memb_types)
4075 /* We have only a marginally acceptable member type;
4076 it needs a warning. */
4077 tree ttl = TREE_TYPE (marginal_memb_type);
4078 tree ttr = TREE_TYPE (rhstype);
4080 /* Const and volatile mean something different for function
4081 types, so the usual warnings are not appropriate. */
4082 if (TREE_CODE (ttr) == FUNCTION_TYPE
4083 && TREE_CODE (ttl) == FUNCTION_TYPE)
4085 /* Because const and volatile on functions are
4086 restrictions that say the function will not do
4087 certain things, it is okay to use a const or volatile
4088 function where an ordinary one is wanted, but not
4089 vice-versa. */
4090 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4091 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4092 errtype, funname, parmnum);
4094 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4095 warn_for_assignment ("%s discards qualifiers from pointer target type",
4096 errtype, funname,
4097 parmnum);
4100 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4101 pedwarn ("ISO C prohibits argument conversion to union type");
4103 return build1 (NOP_EXPR, type, rhs);
4107 /* Conversions among pointers */
4108 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4109 && (coder == codel))
4111 tree ttl = TREE_TYPE (type);
4112 tree ttr = TREE_TYPE (rhstype);
4113 bool is_opaque_pointer;
4115 /* Opaque pointers are treated like void pointers. */
4116 is_opaque_pointer = ((*targetm.vector_opaque_p) (type)
4117 || (*targetm.vector_opaque_p) (rhstype))
4118 && TREE_CODE (ttl) == VECTOR_TYPE
4119 && TREE_CODE (ttr) == VECTOR_TYPE;
4121 /* Any non-function converts to a [const][volatile] void *
4122 and vice versa; otherwise, targets must be the same.
4123 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4124 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4125 || comp_target_types (type, rhstype, 0)
4126 || is_opaque_pointer
4127 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
4128 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4130 if (pedantic
4131 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4133 (VOID_TYPE_P (ttr)
4134 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4135 which are not ANSI null ptr constants. */
4136 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4137 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4138 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
4139 errtype, funname, parmnum);
4140 /* Const and volatile mean something different for function types,
4141 so the usual warnings are not appropriate. */
4142 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4143 && TREE_CODE (ttl) != FUNCTION_TYPE)
4145 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4146 warn_for_assignment ("%s discards qualifiers from pointer target type",
4147 errtype, funname, parmnum);
4148 /* If this is not a case of ignoring a mismatch in signedness,
4149 no warning. */
4150 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4151 || comp_target_types (type, rhstype, 0))
4153 /* If there is a mismatch, do warn. */
4154 else if (pedantic)
4155 warn_for_assignment ("pointer targets in %s differ in signedness",
4156 errtype, funname, parmnum);
4158 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4159 && TREE_CODE (ttr) == FUNCTION_TYPE)
4161 /* Because const and volatile on functions are restrictions
4162 that say the function will not do certain things,
4163 it is okay to use a const or volatile function
4164 where an ordinary one is wanted, but not vice-versa. */
4165 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4166 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4167 errtype, funname, parmnum);
4170 else
4171 warn_for_assignment ("%s from incompatible pointer type",
4172 errtype, funname, parmnum);
4173 return convert (type, rhs);
4175 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4177 /* An explicit constant 0 can convert to a pointer,
4178 or one that results from arithmetic, even including
4179 a cast to integer type. */
4180 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4182 ! (TREE_CODE (rhs) == NOP_EXPR
4183 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4184 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4185 && integer_zerop (TREE_OPERAND (rhs, 0))))
4187 warn_for_assignment ("%s makes pointer from integer without a cast",
4188 errtype, funname, parmnum);
4189 return convert (type, rhs);
4191 return null_pointer_node;
4193 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4195 warn_for_assignment ("%s makes integer from pointer without a cast",
4196 errtype, funname, parmnum);
4197 return convert (type, rhs);
4199 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4200 return convert (type, rhs);
4202 if (!errtype)
4204 if (funname)
4206 tree selector = objc_message_selector ();
4208 if (selector && parmnum > 2)
4209 error ("incompatible type for argument %d of `%s'",
4210 parmnum - 2, IDENTIFIER_POINTER (selector));
4211 else
4212 error ("incompatible type for argument %d of `%s'",
4213 parmnum, IDENTIFIER_POINTER (funname));
4215 else
4216 error ("incompatible type for argument %d of indirect function call",
4217 parmnum);
4219 else
4220 error ("incompatible types in %s", errtype);
4222 return error_mark_node;
4225 /* Convert VALUE for assignment into inlined parameter PARM. */
4227 tree
4228 c_convert_parm_for_inlining (parm, value, fn)
4229 tree parm, value, fn;
4231 tree ret, type;
4233 /* If FN was prototyped, the value has been converted already
4234 in convert_arguments. */
4235 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
4236 return value;
4238 type = TREE_TYPE (parm);
4239 ret = convert_for_assignment (type, value,
4240 (char *) 0 /* arg passing */, fn,
4241 DECL_NAME (fn), 0);
4242 if (PROMOTE_PROTOTYPES
4243 && INTEGRAL_TYPE_P (type)
4244 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4245 ret = default_conversion (ret);
4246 return ret;
4249 /* Print a warning using MSGID.
4250 It gets OPNAME as its one parameter.
4251 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
4252 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4253 FUNCTION and ARGNUM are handled specially if we are building an
4254 Objective-C selector. */
4256 static void
4257 warn_for_assignment (msgid, opname, function, argnum)
4258 const char *msgid;
4259 const char *opname;
4260 tree function;
4261 int argnum;
4263 if (opname == 0)
4265 tree selector = objc_message_selector ();
4266 char * new_opname;
4268 if (selector && argnum > 2)
4270 function = selector;
4271 argnum -= 2;
4273 if (argnum == 0)
4275 if (function)
4277 /* Function name is known; supply it. */
4278 const char *const argstring = _("passing arg of `%s'");
4279 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4280 + strlen (argstring) + 1
4281 + 1);
4282 sprintf (new_opname, argstring,
4283 IDENTIFIER_POINTER (function));
4285 else
4287 /* Function name unknown (call through ptr). */
4288 const char *const argnofun = _("passing arg of pointer to function");
4289 new_opname = (char *) alloca (strlen (argnofun) + 1 + 1);
4290 sprintf (new_opname, argnofun);
4293 else if (function)
4295 /* Function name is known; supply it. */
4296 const char *const argstring = _("passing arg %d of `%s'");
4297 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4298 + strlen (argstring) + 1 + 25
4299 /*%d*/ + 1);
4300 sprintf (new_opname, argstring, argnum,
4301 IDENTIFIER_POINTER (function));
4303 else
4305 /* Function name unknown (call through ptr); just give arg number. */
4306 const char *const argnofun = _("passing arg %d of pointer to function");
4307 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4308 sprintf (new_opname, argnofun, argnum);
4310 opname = new_opname;
4312 pedwarn (msgid, opname);
4315 /* If VALUE is a compound expr all of whose expressions are constant, then
4316 return its value. Otherwise, return error_mark_node.
4318 This is for handling COMPOUND_EXPRs as initializer elements
4319 which is allowed with a warning when -pedantic is specified. */
4321 static tree
4322 valid_compound_expr_initializer (value, endtype)
4323 tree value;
4324 tree endtype;
4326 if (TREE_CODE (value) == COMPOUND_EXPR)
4328 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4329 == error_mark_node)
4330 return error_mark_node;
4331 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4332 endtype);
4334 else if (! TREE_CONSTANT (value)
4335 && ! initializer_constant_valid_p (value, endtype))
4336 return error_mark_node;
4337 else
4338 return value;
4341 /* Perform appropriate conversions on the initial value of a variable,
4342 store it in the declaration DECL,
4343 and print any error messages that are appropriate.
4344 If the init is invalid, store an ERROR_MARK. */
4346 void
4347 store_init_value (decl, init)
4348 tree decl, init;
4350 tree value, type;
4352 /* If variable's type was invalidly declared, just ignore it. */
4354 type = TREE_TYPE (decl);
4355 if (TREE_CODE (type) == ERROR_MARK)
4356 return;
4358 /* Digest the specified initializer into an expression. */
4360 value = digest_init (type, init, TREE_STATIC (decl));
4362 /* Store the expression if valid; else report error. */
4364 if (warn_traditional && !in_system_header
4365 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
4366 warning ("traditional C rejects automatic aggregate initialization");
4368 DECL_INITIAL (decl) = value;
4370 /* ANSI wants warnings about out-of-range constant initializers. */
4371 STRIP_TYPE_NOPS (value);
4372 constant_expression_warning (value);
4374 /* Check if we need to set array size from compound literal size. */
4375 if (TREE_CODE (type) == ARRAY_TYPE
4376 && TYPE_DOMAIN (type) == 0
4377 && value != error_mark_node)
4379 tree inside_init = init;
4381 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4382 inside_init = TREE_OPERAND (init, 0);
4383 inside_init = fold (inside_init);
4385 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4387 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4389 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4391 /* For int foo[] = (int [3]){1}; we need to set array size
4392 now since later on array initializer will be just the
4393 brace enclosed list of the compound literal. */
4394 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4395 layout_type (type);
4396 layout_decl (decl, 0);
4402 /* Methods for storing and printing names for error messages. */
4404 /* Implement a spelling stack that allows components of a name to be pushed
4405 and popped. Each element on the stack is this structure. */
4407 struct spelling
4409 int kind;
4410 union
4412 int i;
4413 const char *s;
4414 } u;
4417 #define SPELLING_STRING 1
4418 #define SPELLING_MEMBER 2
4419 #define SPELLING_BOUNDS 3
4421 static struct spelling *spelling; /* Next stack element (unused). */
4422 static struct spelling *spelling_base; /* Spelling stack base. */
4423 static int spelling_size; /* Size of the spelling stack. */
4425 /* Macros to save and restore the spelling stack around push_... functions.
4426 Alternative to SAVE_SPELLING_STACK. */
4428 #define SPELLING_DEPTH() (spelling - spelling_base)
4429 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4431 /* Push an element on the spelling stack with type KIND and assign VALUE
4432 to MEMBER. */
4434 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4436 int depth = SPELLING_DEPTH (); \
4438 if (depth >= spelling_size) \
4440 spelling_size += 10; \
4441 if (spelling_base == 0) \
4442 spelling_base \
4443 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4444 else \
4445 spelling_base \
4446 = (struct spelling *) xrealloc (spelling_base, \
4447 spelling_size * sizeof (struct spelling)); \
4448 RESTORE_SPELLING_DEPTH (depth); \
4451 spelling->kind = (KIND); \
4452 spelling->MEMBER = (VALUE); \
4453 spelling++; \
4456 /* Push STRING on the stack. Printed literally. */
4458 static void
4459 push_string (string)
4460 const char *string;
4462 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4465 /* Push a member name on the stack. Printed as '.' STRING. */
4467 static void
4468 push_member_name (decl)
4469 tree decl;
4472 const char *const string
4473 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4474 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4477 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4479 static void
4480 push_array_bounds (bounds)
4481 int bounds;
4483 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4486 /* Compute the maximum size in bytes of the printed spelling. */
4488 static int
4489 spelling_length ()
4491 int size = 0;
4492 struct spelling *p;
4494 for (p = spelling_base; p < spelling; p++)
4496 if (p->kind == SPELLING_BOUNDS)
4497 size += 25;
4498 else
4499 size += strlen (p->u.s) + 1;
4502 return size;
4505 /* Print the spelling to BUFFER and return it. */
4507 static char *
4508 print_spelling (buffer)
4509 char *buffer;
4511 char *d = buffer;
4512 struct spelling *p;
4514 for (p = spelling_base; p < spelling; p++)
4515 if (p->kind == SPELLING_BOUNDS)
4517 sprintf (d, "[%d]", p->u.i);
4518 d += strlen (d);
4520 else
4522 const char *s;
4523 if (p->kind == SPELLING_MEMBER)
4524 *d++ = '.';
4525 for (s = p->u.s; (*d = *s++); d++)
4528 *d++ = '\0';
4529 return buffer;
4532 /* Issue an error message for a bad initializer component.
4533 MSGID identifies the message.
4534 The component name is taken from the spelling stack. */
4536 void
4537 error_init (msgid)
4538 const char *msgid;
4540 char *ofwhat;
4542 error ("%s", _(msgid));
4543 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4544 if (*ofwhat)
4545 error ("(near initialization for `%s')", ofwhat);
4548 /* Issue a pedantic warning for a bad initializer component.
4549 MSGID identifies the message.
4550 The component name is taken from the spelling stack. */
4552 void
4553 pedwarn_init (msgid)
4554 const char *msgid;
4556 char *ofwhat;
4558 pedwarn ("%s", _(msgid));
4559 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4560 if (*ofwhat)
4561 pedwarn ("(near initialization for `%s')", ofwhat);
4564 /* Issue a warning for a bad initializer component.
4565 MSGID identifies the message.
4566 The component name is taken from the spelling stack. */
4568 static void
4569 warning_init (msgid)
4570 const char *msgid;
4572 char *ofwhat;
4574 warning ("%s", _(msgid));
4575 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4576 if (*ofwhat)
4577 warning ("(near initialization for `%s')", ofwhat);
4580 /* Digest the parser output INIT as an initializer for type TYPE.
4581 Return a C expression of type TYPE to represent the initial value.
4583 REQUIRE_CONSTANT requests an error if non-constant initializers or
4584 elements are seen. */
4586 static tree
4587 digest_init (type, init, require_constant)
4588 tree type, init;
4589 int require_constant;
4591 enum tree_code code = TREE_CODE (type);
4592 tree inside_init = init;
4594 if (type == error_mark_node
4595 || init == error_mark_node
4596 || TREE_TYPE (init) == error_mark_node)
4597 return error_mark_node;
4599 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4600 /* Do not use STRIP_NOPS here. We do not want an enumerator
4601 whose value is 0 to count as a null pointer constant. */
4602 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4603 inside_init = TREE_OPERAND (init, 0);
4605 inside_init = fold (inside_init);
4607 /* Initialization of an array of chars from a string constant
4608 optionally enclosed in braces. */
4610 if (code == ARRAY_TYPE)
4612 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4613 if ((typ1 == char_type_node
4614 || typ1 == signed_char_type_node
4615 || typ1 == unsigned_char_type_node
4616 || typ1 == unsigned_wchar_type_node
4617 || typ1 == signed_wchar_type_node)
4618 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4620 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4621 TYPE_MAIN_VARIANT (type)))
4622 return inside_init;
4624 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4625 != char_type_node)
4626 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4628 error_init ("char-array initialized from wide string");
4629 return error_mark_node;
4631 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4632 == char_type_node)
4633 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4635 error_init ("int-array initialized from non-wide string");
4636 return error_mark_node;
4639 TREE_TYPE (inside_init) = type;
4640 if (TYPE_DOMAIN (type) != 0
4641 && TYPE_SIZE (type) != 0
4642 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4643 /* Subtract 1 (or sizeof (wchar_t))
4644 because it's ok to ignore the terminating null char
4645 that is counted in the length of the constant. */
4646 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4647 TREE_STRING_LENGTH (inside_init)
4648 - ((TYPE_PRECISION (typ1)
4649 != TYPE_PRECISION (char_type_node))
4650 ? (TYPE_PRECISION (wchar_type_node)
4651 / BITS_PER_UNIT)
4652 : 1)))
4653 pedwarn_init ("initializer-string for array of chars is too long");
4655 return inside_init;
4659 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4660 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4661 below and handle as a constructor. */
4662 if (code == VECTOR_TYPE
4663 && comptypes (TREE_TYPE (inside_init), type)
4664 && TREE_CONSTANT (inside_init))
4666 if (TREE_CODE (inside_init) == VECTOR_CST
4667 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4668 TYPE_MAIN_VARIANT (type)))
4669 return inside_init;
4670 else
4671 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4674 /* Any type can be initialized
4675 from an expression of the same type, optionally with braces. */
4677 if (inside_init && TREE_TYPE (inside_init) != 0
4678 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4679 TYPE_MAIN_VARIANT (type))
4680 || (code == ARRAY_TYPE
4681 && comptypes (TREE_TYPE (inside_init), type))
4682 || (code == VECTOR_TYPE
4683 && comptypes (TREE_TYPE (inside_init), type))
4684 || (code == POINTER_TYPE
4685 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4686 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4687 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4688 TREE_TYPE (type)))))
4690 if (code == POINTER_TYPE)
4691 inside_init = default_function_array_conversion (inside_init);
4693 if (require_constant && !flag_isoc99
4694 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4696 /* As an extension, allow initializing objects with static storage
4697 duration with compound literals (which are then treated just as
4698 the brace enclosed list they contain). */
4699 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4700 inside_init = DECL_INITIAL (decl);
4703 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4704 && TREE_CODE (inside_init) != CONSTRUCTOR)
4706 error_init ("array initialized from non-constant array expression");
4707 return error_mark_node;
4710 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4711 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4713 /* Compound expressions can only occur here if -pedantic or
4714 -pedantic-errors is specified. In the later case, we always want
4715 an error. In the former case, we simply want a warning. */
4716 if (require_constant && pedantic
4717 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4719 inside_init
4720 = valid_compound_expr_initializer (inside_init,
4721 TREE_TYPE (inside_init));
4722 if (inside_init == error_mark_node)
4723 error_init ("initializer element is not constant");
4724 else
4725 pedwarn_init ("initializer element is not constant");
4726 if (flag_pedantic_errors)
4727 inside_init = error_mark_node;
4729 else if (require_constant
4730 && (!TREE_CONSTANT (inside_init)
4731 /* This test catches things like `7 / 0' which
4732 result in an expression for which TREE_CONSTANT
4733 is true, but which is not actually something
4734 that is a legal constant. We really should not
4735 be using this function, because it is a part of
4736 the back-end. Instead, the expression should
4737 already have been turned into ERROR_MARK_NODE. */
4738 || !initializer_constant_valid_p (inside_init,
4739 TREE_TYPE (inside_init))))
4741 error_init ("initializer element is not constant");
4742 inside_init = error_mark_node;
4745 return inside_init;
4748 /* Handle scalar types, including conversions. */
4750 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4751 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4753 /* Note that convert_for_assignment calls default_conversion
4754 for arrays and functions. We must not call it in the
4755 case where inside_init is a null pointer constant. */
4756 inside_init
4757 = convert_for_assignment (type, init, _("initialization"),
4758 NULL_TREE, NULL_TREE, 0);
4760 if (require_constant && ! TREE_CONSTANT (inside_init))
4762 error_init ("initializer element is not constant");
4763 inside_init = error_mark_node;
4765 else if (require_constant
4766 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4768 error_init ("initializer element is not computable at load time");
4769 inside_init = error_mark_node;
4772 return inside_init;
4775 /* Come here only for records and arrays. */
4777 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4779 error_init ("variable-sized object may not be initialized");
4780 return error_mark_node;
4783 error_init ("invalid initializer");
4784 return error_mark_node;
4787 /* Handle initializers that use braces. */
4789 /* Type of object we are accumulating a constructor for.
4790 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4791 static tree constructor_type;
4793 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4794 left to fill. */
4795 static tree constructor_fields;
4797 /* For an ARRAY_TYPE, this is the specified index
4798 at which to store the next element we get. */
4799 static tree constructor_index;
4801 /* For an ARRAY_TYPE, this is the maximum index. */
4802 static tree constructor_max_index;
4804 /* For a RECORD_TYPE, this is the first field not yet written out. */
4805 static tree constructor_unfilled_fields;
4807 /* For an ARRAY_TYPE, this is the index of the first element
4808 not yet written out. */
4809 static tree constructor_unfilled_index;
4811 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4812 This is so we can generate gaps between fields, when appropriate. */
4813 static tree constructor_bit_index;
4815 /* If we are saving up the elements rather than allocating them,
4816 this is the list of elements so far (in reverse order,
4817 most recent first). */
4818 static tree constructor_elements;
4820 /* 1 if constructor should be incrementally stored into a constructor chain,
4821 0 if all the elements should be kept in AVL tree. */
4822 static int constructor_incremental;
4824 /* 1 if so far this constructor's elements are all compile-time constants. */
4825 static int constructor_constant;
4827 /* 1 if so far this constructor's elements are all valid address constants. */
4828 static int constructor_simple;
4830 /* 1 if this constructor is erroneous so far. */
4831 static int constructor_erroneous;
4833 /* Structure for managing pending initializer elements, organized as an
4834 AVL tree. */
4836 struct init_node
4838 struct init_node *left, *right;
4839 struct init_node *parent;
4840 int balance;
4841 tree purpose;
4842 tree value;
4845 /* Tree of pending elements at this constructor level.
4846 These are elements encountered out of order
4847 which belong at places we haven't reached yet in actually
4848 writing the output.
4849 Will never hold tree nodes across GC runs. */
4850 static struct init_node *constructor_pending_elts;
4852 /* The SPELLING_DEPTH of this constructor. */
4853 static int constructor_depth;
4855 /* 0 if implicitly pushing constructor levels is allowed. */
4856 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4858 static int require_constant_value;
4859 static int require_constant_elements;
4861 /* DECL node for which an initializer is being read.
4862 0 means we are reading a constructor expression
4863 such as (struct foo) {...}. */
4864 static tree constructor_decl;
4866 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4867 static const char *constructor_asmspec;
4869 /* Nonzero if this is an initializer for a top-level decl. */
4870 static int constructor_top_level;
4872 /* Nonzero if there were any member designators in this initializer. */
4873 static int constructor_designated;
4875 /* Nesting depth of designator list. */
4876 static int designator_depth;
4878 /* Nonzero if there were diagnosed errors in this designator list. */
4879 static int designator_errorneous;
4882 /* This stack has a level for each implicit or explicit level of
4883 structuring in the initializer, including the outermost one. It
4884 saves the values of most of the variables above. */
4886 struct constructor_range_stack;
4888 struct constructor_stack
4890 struct constructor_stack *next;
4891 tree type;
4892 tree fields;
4893 tree index;
4894 tree max_index;
4895 tree unfilled_index;
4896 tree unfilled_fields;
4897 tree bit_index;
4898 tree elements;
4899 struct init_node *pending_elts;
4900 int offset;
4901 int depth;
4902 /* If nonzero, this value should replace the entire
4903 constructor at this level. */
4904 tree replacement_value;
4905 struct constructor_range_stack *range_stack;
4906 char constant;
4907 char simple;
4908 char implicit;
4909 char erroneous;
4910 char outer;
4911 char incremental;
4912 char designated;
4915 struct constructor_stack *constructor_stack;
4917 /* This stack represents designators from some range designator up to
4918 the last designator in the list. */
4920 struct constructor_range_stack
4922 struct constructor_range_stack *next, *prev;
4923 struct constructor_stack *stack;
4924 tree range_start;
4925 tree index;
4926 tree range_end;
4927 tree fields;
4930 struct constructor_range_stack *constructor_range_stack;
4932 /* This stack records separate initializers that are nested.
4933 Nested initializers can't happen in ANSI C, but GNU C allows them
4934 in cases like { ... (struct foo) { ... } ... }. */
4936 struct initializer_stack
4938 struct initializer_stack *next;
4939 tree decl;
4940 const char *asmspec;
4941 struct constructor_stack *constructor_stack;
4942 struct constructor_range_stack *constructor_range_stack;
4943 tree elements;
4944 struct spelling *spelling;
4945 struct spelling *spelling_base;
4946 int spelling_size;
4947 char top_level;
4948 char require_constant_value;
4949 char require_constant_elements;
4952 struct initializer_stack *initializer_stack;
4954 /* Prepare to parse and output the initializer for variable DECL. */
4956 void
4957 start_init (decl, asmspec_tree, top_level)
4958 tree decl;
4959 tree asmspec_tree;
4960 int top_level;
4962 const char *locus;
4963 struct initializer_stack *p
4964 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
4965 const char *asmspec = 0;
4967 if (asmspec_tree)
4968 asmspec = TREE_STRING_POINTER (asmspec_tree);
4970 p->decl = constructor_decl;
4971 p->asmspec = constructor_asmspec;
4972 p->require_constant_value = require_constant_value;
4973 p->require_constant_elements = require_constant_elements;
4974 p->constructor_stack = constructor_stack;
4975 p->constructor_range_stack = constructor_range_stack;
4976 p->elements = constructor_elements;
4977 p->spelling = spelling;
4978 p->spelling_base = spelling_base;
4979 p->spelling_size = spelling_size;
4980 p->top_level = constructor_top_level;
4981 p->next = initializer_stack;
4982 initializer_stack = p;
4984 constructor_decl = decl;
4985 constructor_asmspec = asmspec;
4986 constructor_designated = 0;
4987 constructor_top_level = top_level;
4989 if (decl != 0)
4991 require_constant_value = TREE_STATIC (decl);
4992 require_constant_elements
4993 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4994 /* For a scalar, you can always use any value to initialize,
4995 even within braces. */
4996 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4997 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4998 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4999 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5000 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5002 else
5004 require_constant_value = 0;
5005 require_constant_elements = 0;
5006 locus = "(anonymous)";
5009 constructor_stack = 0;
5010 constructor_range_stack = 0;
5012 missing_braces_mentioned = 0;
5014 spelling_base = 0;
5015 spelling_size = 0;
5016 RESTORE_SPELLING_DEPTH (0);
5018 if (locus)
5019 push_string (locus);
5022 void
5023 finish_init ()
5025 struct initializer_stack *p = initializer_stack;
5027 /* Free the whole constructor stack of this initializer. */
5028 while (constructor_stack)
5030 struct constructor_stack *q = constructor_stack;
5031 constructor_stack = q->next;
5032 free (q);
5035 if (constructor_range_stack)
5036 abort ();
5038 /* Pop back to the data of the outer initializer (if any). */
5039 constructor_decl = p->decl;
5040 constructor_asmspec = p->asmspec;
5041 require_constant_value = p->require_constant_value;
5042 require_constant_elements = p->require_constant_elements;
5043 constructor_stack = p->constructor_stack;
5044 constructor_range_stack = p->constructor_range_stack;
5045 constructor_elements = p->elements;
5046 spelling = p->spelling;
5047 spelling_base = p->spelling_base;
5048 spelling_size = p->spelling_size;
5049 constructor_top_level = p->top_level;
5050 initializer_stack = p->next;
5051 free (p);
5054 /* Call here when we see the initializer is surrounded by braces.
5055 This is instead of a call to push_init_level;
5056 it is matched by a call to pop_init_level.
5058 TYPE is the type to initialize, for a constructor expression.
5059 For an initializer for a decl, TYPE is zero. */
5061 void
5062 really_start_incremental_init (type)
5063 tree type;
5065 struct constructor_stack *p
5066 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5068 if (type == 0)
5069 type = TREE_TYPE (constructor_decl);
5071 if ((*targetm.vector_opaque_p) (type))
5072 error ("opaque vector types cannot be initialized");
5074 p->type = constructor_type;
5075 p->fields = constructor_fields;
5076 p->index = constructor_index;
5077 p->max_index = constructor_max_index;
5078 p->unfilled_index = constructor_unfilled_index;
5079 p->unfilled_fields = constructor_unfilled_fields;
5080 p->bit_index = constructor_bit_index;
5081 p->elements = constructor_elements;
5082 p->constant = constructor_constant;
5083 p->simple = constructor_simple;
5084 p->erroneous = constructor_erroneous;
5085 p->pending_elts = constructor_pending_elts;
5086 p->depth = constructor_depth;
5087 p->replacement_value = 0;
5088 p->implicit = 0;
5089 p->range_stack = 0;
5090 p->outer = 0;
5091 p->incremental = constructor_incremental;
5092 p->designated = constructor_designated;
5093 p->next = 0;
5094 constructor_stack = p;
5096 constructor_constant = 1;
5097 constructor_simple = 1;
5098 constructor_depth = SPELLING_DEPTH ();
5099 constructor_elements = 0;
5100 constructor_pending_elts = 0;
5101 constructor_type = type;
5102 constructor_incremental = 1;
5103 constructor_designated = 0;
5104 designator_depth = 0;
5105 designator_errorneous = 0;
5107 if (TREE_CODE (constructor_type) == RECORD_TYPE
5108 || TREE_CODE (constructor_type) == UNION_TYPE)
5110 constructor_fields = TYPE_FIELDS (constructor_type);
5111 /* Skip any nameless bit fields at the beginning. */
5112 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5113 && DECL_NAME (constructor_fields) == 0)
5114 constructor_fields = TREE_CHAIN (constructor_fields);
5116 constructor_unfilled_fields = constructor_fields;
5117 constructor_bit_index = bitsize_zero_node;
5119 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5121 if (TYPE_DOMAIN (constructor_type))
5123 constructor_max_index
5124 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5126 /* Detect non-empty initializations of zero-length arrays. */
5127 if (constructor_max_index == NULL_TREE
5128 && TYPE_SIZE (constructor_type))
5129 constructor_max_index = build_int_2 (-1, -1);
5131 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5132 to initialize VLAs will cause a proper error; avoid tree
5133 checking errors as well by setting a safe value. */
5134 if (constructor_max_index
5135 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5136 constructor_max_index = build_int_2 (-1, -1);
5138 constructor_index
5139 = convert (bitsizetype,
5140 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5142 else
5143 constructor_index = bitsize_zero_node;
5145 constructor_unfilled_index = constructor_index;
5147 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5149 /* Vectors are like simple fixed-size arrays. */
5150 constructor_max_index =
5151 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5152 constructor_index = convert (bitsizetype, bitsize_zero_node);
5153 constructor_unfilled_index = constructor_index;
5155 else
5157 /* Handle the case of int x = {5}; */
5158 constructor_fields = constructor_type;
5159 constructor_unfilled_fields = constructor_type;
5163 /* Push down into a subobject, for initialization.
5164 If this is for an explicit set of braces, IMPLICIT is 0.
5165 If it is because the next element belongs at a lower level,
5166 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5168 void
5169 push_init_level (implicit)
5170 int implicit;
5172 struct constructor_stack *p;
5173 tree value = NULL_TREE;
5175 /* If we've exhausted any levels that didn't have braces,
5176 pop them now. */
5177 while (constructor_stack->implicit)
5179 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5180 || TREE_CODE (constructor_type) == UNION_TYPE)
5181 && constructor_fields == 0)
5182 process_init_element (pop_init_level (1));
5183 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5184 && constructor_max_index
5185 && tree_int_cst_lt (constructor_max_index, constructor_index))
5186 process_init_element (pop_init_level (1));
5187 else
5188 break;
5191 /* Unless this is an explicit brace, we need to preserve previous
5192 content if any. */
5193 if (implicit)
5195 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5196 || TREE_CODE (constructor_type) == UNION_TYPE)
5197 && constructor_fields)
5198 value = find_init_member (constructor_fields);
5199 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5200 value = find_init_member (constructor_index);
5203 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5204 p->type = constructor_type;
5205 p->fields = constructor_fields;
5206 p->index = constructor_index;
5207 p->max_index = constructor_max_index;
5208 p->unfilled_index = constructor_unfilled_index;
5209 p->unfilled_fields = constructor_unfilled_fields;
5210 p->bit_index = constructor_bit_index;
5211 p->elements = constructor_elements;
5212 p->constant = constructor_constant;
5213 p->simple = constructor_simple;
5214 p->erroneous = constructor_erroneous;
5215 p->pending_elts = constructor_pending_elts;
5216 p->depth = constructor_depth;
5217 p->replacement_value = 0;
5218 p->implicit = implicit;
5219 p->outer = 0;
5220 p->incremental = constructor_incremental;
5221 p->designated = constructor_designated;
5222 p->next = constructor_stack;
5223 p->range_stack = 0;
5224 constructor_stack = p;
5226 constructor_constant = 1;
5227 constructor_simple = 1;
5228 constructor_depth = SPELLING_DEPTH ();
5229 constructor_elements = 0;
5230 constructor_incremental = 1;
5231 constructor_designated = 0;
5232 constructor_pending_elts = 0;
5233 if (!implicit)
5235 p->range_stack = constructor_range_stack;
5236 constructor_range_stack = 0;
5237 designator_depth = 0;
5238 designator_errorneous = 0;
5241 /* Don't die if an entire brace-pair level is superfluous
5242 in the containing level. */
5243 if (constructor_type == 0)
5245 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5246 || TREE_CODE (constructor_type) == UNION_TYPE)
5248 /* Don't die if there are extra init elts at the end. */
5249 if (constructor_fields == 0)
5250 constructor_type = 0;
5251 else
5253 constructor_type = TREE_TYPE (constructor_fields);
5254 push_member_name (constructor_fields);
5255 constructor_depth++;
5258 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5260 constructor_type = TREE_TYPE (constructor_type);
5261 push_array_bounds (tree_low_cst (constructor_index, 0));
5262 constructor_depth++;
5265 if (constructor_type == 0)
5267 error_init ("extra brace group at end of initializer");
5268 constructor_fields = 0;
5269 constructor_unfilled_fields = 0;
5270 return;
5273 if (value && TREE_CODE (value) == CONSTRUCTOR)
5275 constructor_constant = TREE_CONSTANT (value);
5276 constructor_simple = TREE_STATIC (value);
5277 constructor_elements = CONSTRUCTOR_ELTS (value);
5278 if (constructor_elements
5279 && (TREE_CODE (constructor_type) == RECORD_TYPE
5280 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5281 set_nonincremental_init ();
5284 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5286 missing_braces_mentioned = 1;
5287 warning_init ("missing braces around initializer");
5290 if (TREE_CODE (constructor_type) == RECORD_TYPE
5291 || TREE_CODE (constructor_type) == UNION_TYPE)
5293 constructor_fields = TYPE_FIELDS (constructor_type);
5294 /* Skip any nameless bit fields at the beginning. */
5295 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5296 && DECL_NAME (constructor_fields) == 0)
5297 constructor_fields = TREE_CHAIN (constructor_fields);
5299 constructor_unfilled_fields = constructor_fields;
5300 constructor_bit_index = bitsize_zero_node;
5302 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5304 /* Vectors are like simple fixed-size arrays. */
5305 constructor_max_index =
5306 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5307 constructor_index = convert (bitsizetype, integer_zero_node);
5308 constructor_unfilled_index = constructor_index;
5310 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5312 if (TYPE_DOMAIN (constructor_type))
5314 constructor_max_index
5315 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5317 /* Detect non-empty initializations of zero-length arrays. */
5318 if (constructor_max_index == NULL_TREE
5319 && TYPE_SIZE (constructor_type))
5320 constructor_max_index = build_int_2 (-1, -1);
5322 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5323 to initialize VLAs will cause a proper error; avoid tree
5324 checking errors as well by setting a safe value. */
5325 if (constructor_max_index
5326 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5327 constructor_max_index = build_int_2 (-1, -1);
5329 constructor_index
5330 = convert (bitsizetype,
5331 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5333 else
5334 constructor_index = bitsize_zero_node;
5336 constructor_unfilled_index = constructor_index;
5337 if (value && TREE_CODE (value) == STRING_CST)
5339 /* We need to split the char/wchar array into individual
5340 characters, so that we don't have to special case it
5341 everywhere. */
5342 set_nonincremental_init_from_string (value);
5345 else
5347 warning_init ("braces around scalar initializer");
5348 constructor_fields = constructor_type;
5349 constructor_unfilled_fields = constructor_type;
5353 /* At the end of an implicit or explicit brace level,
5354 finish up that level of constructor.
5355 If we were outputting the elements as they are read, return 0
5356 from inner levels (process_init_element ignores that),
5357 but return error_mark_node from the outermost level
5358 (that's what we want to put in DECL_INITIAL).
5359 Otherwise, return a CONSTRUCTOR expression. */
5361 tree
5362 pop_init_level (implicit)
5363 int implicit;
5365 struct constructor_stack *p;
5366 tree constructor = 0;
5368 if (implicit == 0)
5370 /* When we come to an explicit close brace,
5371 pop any inner levels that didn't have explicit braces. */
5372 while (constructor_stack->implicit)
5373 process_init_element (pop_init_level (1));
5375 if (constructor_range_stack)
5376 abort ();
5379 p = constructor_stack;
5381 /* Error for initializing a flexible array member, or a zero-length
5382 array member in an inappropriate context. */
5383 if (constructor_type && constructor_fields
5384 && TREE_CODE (constructor_type) == ARRAY_TYPE
5385 && TYPE_DOMAIN (constructor_type)
5386 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5388 /* Silently discard empty initializations. The parser will
5389 already have pedwarned for empty brackets. */
5390 if (integer_zerop (constructor_unfilled_index))
5391 constructor_type = NULL_TREE;
5392 else if (! TYPE_SIZE (constructor_type))
5394 if (constructor_depth > 2)
5395 error_init ("initialization of flexible array member in a nested context");
5396 else if (pedantic)
5397 pedwarn_init ("initialization of a flexible array member");
5399 /* We have already issued an error message for the existence
5400 of a flexible array member not at the end of the structure.
5401 Discard the initializer so that we do not abort later. */
5402 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5403 constructor_type = NULL_TREE;
5405 else
5406 /* Zero-length arrays are no longer special, so we should no longer
5407 get here. */
5408 abort ();
5411 /* Warn when some struct elements are implicitly initialized to zero. */
5412 if (extra_warnings
5413 && constructor_type
5414 && TREE_CODE (constructor_type) == RECORD_TYPE
5415 && constructor_unfilled_fields)
5417 /* Do not warn for flexible array members or zero-length arrays. */
5418 while (constructor_unfilled_fields
5419 && (! DECL_SIZE (constructor_unfilled_fields)
5420 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5421 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5423 /* Do not warn if this level of the initializer uses member
5424 designators; it is likely to be deliberate. */
5425 if (constructor_unfilled_fields && !constructor_designated)
5427 push_member_name (constructor_unfilled_fields);
5428 warning_init ("missing initializer");
5429 RESTORE_SPELLING_DEPTH (constructor_depth);
5433 /* Now output all pending elements. */
5434 constructor_incremental = 1;
5435 output_pending_init_elements (1);
5437 /* Pad out the end of the structure. */
5438 if (p->replacement_value)
5439 /* If this closes a superfluous brace pair,
5440 just pass out the element between them. */
5441 constructor = p->replacement_value;
5442 else if (constructor_type == 0)
5444 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5445 && TREE_CODE (constructor_type) != UNION_TYPE
5446 && TREE_CODE (constructor_type) != ARRAY_TYPE
5447 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5449 /* A nonincremental scalar initializer--just return
5450 the element, after verifying there is just one. */
5451 if (constructor_elements == 0)
5453 if (!constructor_erroneous)
5454 error_init ("empty scalar initializer");
5455 constructor = error_mark_node;
5457 else if (TREE_CHAIN (constructor_elements) != 0)
5459 error_init ("extra elements in scalar initializer");
5460 constructor = TREE_VALUE (constructor_elements);
5462 else
5463 constructor = TREE_VALUE (constructor_elements);
5465 else
5467 if (constructor_erroneous)
5468 constructor = error_mark_node;
5469 else
5471 constructor = build_constructor (constructor_type,
5472 nreverse (constructor_elements));
5473 if (constructor_constant)
5474 TREE_CONSTANT (constructor) = 1;
5475 if (constructor_constant && constructor_simple)
5476 TREE_STATIC (constructor) = 1;
5480 constructor_type = p->type;
5481 constructor_fields = p->fields;
5482 constructor_index = p->index;
5483 constructor_max_index = p->max_index;
5484 constructor_unfilled_index = p->unfilled_index;
5485 constructor_unfilled_fields = p->unfilled_fields;
5486 constructor_bit_index = p->bit_index;
5487 constructor_elements = p->elements;
5488 constructor_constant = p->constant;
5489 constructor_simple = p->simple;
5490 constructor_erroneous = p->erroneous;
5491 constructor_incremental = p->incremental;
5492 constructor_designated = p->designated;
5493 constructor_pending_elts = p->pending_elts;
5494 constructor_depth = p->depth;
5495 if (!p->implicit)
5496 constructor_range_stack = p->range_stack;
5497 RESTORE_SPELLING_DEPTH (constructor_depth);
5499 constructor_stack = p->next;
5500 free (p);
5502 if (constructor == 0)
5504 if (constructor_stack == 0)
5505 return error_mark_node;
5506 return NULL_TREE;
5508 return constructor;
5511 /* Common handling for both array range and field name designators.
5512 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5514 static int
5515 set_designator (array)
5516 int array;
5518 tree subtype;
5519 enum tree_code subcode;
5521 /* Don't die if an entire brace-pair level is superfluous
5522 in the containing level. */
5523 if (constructor_type == 0)
5524 return 1;
5526 /* If there were errors in this designator list already, bail out silently. */
5527 if (designator_errorneous)
5528 return 1;
5530 if (!designator_depth)
5532 if (constructor_range_stack)
5533 abort ();
5535 /* Designator list starts at the level of closest explicit
5536 braces. */
5537 while (constructor_stack->implicit)
5538 process_init_element (pop_init_level (1));
5539 constructor_designated = 1;
5540 return 0;
5543 if (constructor_no_implicit)
5545 error_init ("initialization designators may not nest");
5546 return 1;
5549 if (TREE_CODE (constructor_type) == RECORD_TYPE
5550 || TREE_CODE (constructor_type) == UNION_TYPE)
5552 subtype = TREE_TYPE (constructor_fields);
5553 if (subtype != error_mark_node)
5554 subtype = TYPE_MAIN_VARIANT (subtype);
5556 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5558 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5560 else
5561 abort ();
5563 subcode = TREE_CODE (subtype);
5564 if (array && subcode != ARRAY_TYPE)
5566 error_init ("array index in non-array initializer");
5567 return 1;
5569 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5571 error_init ("field name not in record or union initializer");
5572 return 1;
5575 constructor_designated = 1;
5576 push_init_level (2);
5577 return 0;
5580 /* If there are range designators in designator list, push a new designator
5581 to constructor_range_stack. RANGE_END is end of such stack range or
5582 NULL_TREE if there is no range designator at this level. */
5584 static void
5585 push_range_stack (range_end)
5586 tree range_end;
5588 struct constructor_range_stack *p;
5590 p = (struct constructor_range_stack *)
5591 ggc_alloc (sizeof (struct constructor_range_stack));
5592 p->prev = constructor_range_stack;
5593 p->next = 0;
5594 p->fields = constructor_fields;
5595 p->range_start = constructor_index;
5596 p->index = constructor_index;
5597 p->stack = constructor_stack;
5598 p->range_end = range_end;
5599 if (constructor_range_stack)
5600 constructor_range_stack->next = p;
5601 constructor_range_stack = p;
5604 /* Within an array initializer, specify the next index to be initialized.
5605 FIRST is that index. If LAST is nonzero, then initialize a range
5606 of indices, running from FIRST through LAST. */
5608 void
5609 set_init_index (first, last)
5610 tree first, last;
5612 if (set_designator (1))
5613 return;
5615 designator_errorneous = 1;
5617 while ((TREE_CODE (first) == NOP_EXPR
5618 || TREE_CODE (first) == CONVERT_EXPR
5619 || TREE_CODE (first) == NON_LVALUE_EXPR)
5620 && (TYPE_MODE (TREE_TYPE (first))
5621 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5622 first = TREE_OPERAND (first, 0);
5624 if (last)
5625 while ((TREE_CODE (last) == NOP_EXPR
5626 || TREE_CODE (last) == CONVERT_EXPR
5627 || TREE_CODE (last) == NON_LVALUE_EXPR)
5628 && (TYPE_MODE (TREE_TYPE (last))
5629 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5630 last = TREE_OPERAND (last, 0);
5632 if (TREE_CODE (first) != INTEGER_CST)
5633 error_init ("nonconstant array index in initializer");
5634 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5635 error_init ("nonconstant array index in initializer");
5636 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5637 error_init ("array index in non-array initializer");
5638 else if (constructor_max_index
5639 && tree_int_cst_lt (constructor_max_index, first))
5640 error_init ("array index in initializer exceeds array bounds");
5641 else
5643 constructor_index = convert (bitsizetype, first);
5645 if (last)
5647 if (tree_int_cst_equal (first, last))
5648 last = 0;
5649 else if (tree_int_cst_lt (last, first))
5651 error_init ("empty index range in initializer");
5652 last = 0;
5654 else
5656 last = convert (bitsizetype, last);
5657 if (constructor_max_index != 0
5658 && tree_int_cst_lt (constructor_max_index, last))
5660 error_init ("array index range in initializer exceeds array bounds");
5661 last = 0;
5666 designator_depth++;
5667 designator_errorneous = 0;
5668 if (constructor_range_stack || last)
5669 push_range_stack (last);
5673 /* Within a struct initializer, specify the next field to be initialized. */
5675 void
5676 set_init_label (fieldname)
5677 tree fieldname;
5679 tree tail;
5681 if (set_designator (0))
5682 return;
5684 designator_errorneous = 1;
5686 if (TREE_CODE (constructor_type) != RECORD_TYPE
5687 && TREE_CODE (constructor_type) != UNION_TYPE)
5689 error_init ("field name not in record or union initializer");
5690 return;
5693 for (tail = TYPE_FIELDS (constructor_type); tail;
5694 tail = TREE_CHAIN (tail))
5696 if (DECL_NAME (tail) == fieldname)
5697 break;
5700 if (tail == 0)
5701 error ("unknown field `%s' specified in initializer",
5702 IDENTIFIER_POINTER (fieldname));
5703 else
5705 constructor_fields = tail;
5706 designator_depth++;
5707 designator_errorneous = 0;
5708 if (constructor_range_stack)
5709 push_range_stack (NULL_TREE);
5713 /* Add a new initializer to the tree of pending initializers. PURPOSE
5714 identifies the initializer, either array index or field in a structure.
5715 VALUE is the value of that index or field. */
5717 static void
5718 add_pending_init (purpose, value)
5719 tree purpose, value;
5721 struct init_node *p, **q, *r;
5723 q = &constructor_pending_elts;
5724 p = 0;
5726 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5728 while (*q != 0)
5730 p = *q;
5731 if (tree_int_cst_lt (purpose, p->purpose))
5732 q = &p->left;
5733 else if (tree_int_cst_lt (p->purpose, purpose))
5734 q = &p->right;
5735 else
5737 if (TREE_SIDE_EFFECTS (p->value))
5738 warning_init ("initialized field with side-effects overwritten");
5739 p->value = value;
5740 return;
5744 else
5746 tree bitpos;
5748 bitpos = bit_position (purpose);
5749 while (*q != NULL)
5751 p = *q;
5752 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5753 q = &p->left;
5754 else if (p->purpose != purpose)
5755 q = &p->right;
5756 else
5758 if (TREE_SIDE_EFFECTS (p->value))
5759 warning_init ("initialized field with side-effects overwritten");
5760 p->value = value;
5761 return;
5766 r = (struct init_node *) ggc_alloc (sizeof (struct init_node));
5767 r->purpose = purpose;
5768 r->value = value;
5770 *q = r;
5771 r->parent = p;
5772 r->left = 0;
5773 r->right = 0;
5774 r->balance = 0;
5776 while (p)
5778 struct init_node *s;
5780 if (r == p->left)
5782 if (p->balance == 0)
5783 p->balance = -1;
5784 else if (p->balance < 0)
5786 if (r->balance < 0)
5788 /* L rotation. */
5789 p->left = r->right;
5790 if (p->left)
5791 p->left->parent = p;
5792 r->right = p;
5794 p->balance = 0;
5795 r->balance = 0;
5797 s = p->parent;
5798 p->parent = r;
5799 r->parent = s;
5800 if (s)
5802 if (s->left == p)
5803 s->left = r;
5804 else
5805 s->right = r;
5807 else
5808 constructor_pending_elts = r;
5810 else
5812 /* LR rotation. */
5813 struct init_node *t = r->right;
5815 r->right = t->left;
5816 if (r->right)
5817 r->right->parent = r;
5818 t->left = r;
5820 p->left = t->right;
5821 if (p->left)
5822 p->left->parent = p;
5823 t->right = p;
5825 p->balance = t->balance < 0;
5826 r->balance = -(t->balance > 0);
5827 t->balance = 0;
5829 s = p->parent;
5830 p->parent = t;
5831 r->parent = t;
5832 t->parent = s;
5833 if (s)
5835 if (s->left == p)
5836 s->left = t;
5837 else
5838 s->right = t;
5840 else
5841 constructor_pending_elts = t;
5843 break;
5845 else
5847 /* p->balance == +1; growth of left side balances the node. */
5848 p->balance = 0;
5849 break;
5852 else /* r == p->right */
5854 if (p->balance == 0)
5855 /* Growth propagation from right side. */
5856 p->balance++;
5857 else if (p->balance > 0)
5859 if (r->balance > 0)
5861 /* R rotation. */
5862 p->right = r->left;
5863 if (p->right)
5864 p->right->parent = p;
5865 r->left = p;
5867 p->balance = 0;
5868 r->balance = 0;
5870 s = p->parent;
5871 p->parent = r;
5872 r->parent = s;
5873 if (s)
5875 if (s->left == p)
5876 s->left = r;
5877 else
5878 s->right = r;
5880 else
5881 constructor_pending_elts = r;
5883 else /* r->balance == -1 */
5885 /* RL rotation */
5886 struct init_node *t = r->left;
5888 r->left = t->right;
5889 if (r->left)
5890 r->left->parent = r;
5891 t->right = r;
5893 p->right = t->left;
5894 if (p->right)
5895 p->right->parent = p;
5896 t->left = p;
5898 r->balance = (t->balance < 0);
5899 p->balance = -(t->balance > 0);
5900 t->balance = 0;
5902 s = p->parent;
5903 p->parent = t;
5904 r->parent = t;
5905 t->parent = s;
5906 if (s)
5908 if (s->left == p)
5909 s->left = t;
5910 else
5911 s->right = t;
5913 else
5914 constructor_pending_elts = t;
5916 break;
5918 else
5920 /* p->balance == -1; growth of right side balances the node. */
5921 p->balance = 0;
5922 break;
5926 r = p;
5927 p = p->parent;
5931 /* Build AVL tree from a sorted chain. */
5933 static void
5934 set_nonincremental_init ()
5936 tree chain;
5938 if (TREE_CODE (constructor_type) != RECORD_TYPE
5939 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5940 return;
5942 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5943 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5944 constructor_elements = 0;
5945 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5947 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5948 /* Skip any nameless bit fields at the beginning. */
5949 while (constructor_unfilled_fields != 0
5950 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5951 && DECL_NAME (constructor_unfilled_fields) == 0)
5952 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5955 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5957 if (TYPE_DOMAIN (constructor_type))
5958 constructor_unfilled_index
5959 = convert (bitsizetype,
5960 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5961 else
5962 constructor_unfilled_index = bitsize_zero_node;
5964 constructor_incremental = 0;
5967 /* Build AVL tree from a string constant. */
5969 static void
5970 set_nonincremental_init_from_string (str)
5971 tree str;
5973 tree value, purpose, type;
5974 HOST_WIDE_INT val[2];
5975 const char *p, *end;
5976 int byte, wchar_bytes, charwidth, bitpos;
5978 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5979 abort ();
5981 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5982 == TYPE_PRECISION (char_type_node))
5983 wchar_bytes = 1;
5984 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5985 == TYPE_PRECISION (wchar_type_node))
5986 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5987 else
5988 abort ();
5990 charwidth = TYPE_PRECISION (char_type_node);
5991 type = TREE_TYPE (constructor_type);
5992 p = TREE_STRING_POINTER (str);
5993 end = p + TREE_STRING_LENGTH (str);
5995 for (purpose = bitsize_zero_node;
5996 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5997 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5999 if (wchar_bytes == 1)
6001 val[1] = (unsigned char) *p++;
6002 val[0] = 0;
6004 else
6006 val[0] = 0;
6007 val[1] = 0;
6008 for (byte = 0; byte < wchar_bytes; byte++)
6010 if (BYTES_BIG_ENDIAN)
6011 bitpos = (wchar_bytes - byte - 1) * charwidth;
6012 else
6013 bitpos = byte * charwidth;
6014 val[bitpos < HOST_BITS_PER_WIDE_INT]
6015 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6016 << (bitpos % HOST_BITS_PER_WIDE_INT);
6020 if (!TREE_UNSIGNED (type))
6022 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6023 if (bitpos < HOST_BITS_PER_WIDE_INT)
6025 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6027 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6028 val[0] = -1;
6031 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6033 if (val[1] < 0)
6034 val[0] = -1;
6036 else if (val[0] & (((HOST_WIDE_INT) 1)
6037 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6038 val[0] |= ((HOST_WIDE_INT) -1)
6039 << (bitpos - HOST_BITS_PER_WIDE_INT);
6042 value = build_int_2 (val[1], val[0]);
6043 TREE_TYPE (value) = type;
6044 add_pending_init (purpose, value);
6047 constructor_incremental = 0;
6050 /* Return value of FIELD in pending initializer or zero if the field was
6051 not initialized yet. */
6053 static tree
6054 find_init_member (field)
6055 tree field;
6057 struct init_node *p;
6059 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6061 if (constructor_incremental
6062 && tree_int_cst_lt (field, constructor_unfilled_index))
6063 set_nonincremental_init ();
6065 p = constructor_pending_elts;
6066 while (p)
6068 if (tree_int_cst_lt (field, p->purpose))
6069 p = p->left;
6070 else if (tree_int_cst_lt (p->purpose, field))
6071 p = p->right;
6072 else
6073 return p->value;
6076 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6078 tree bitpos = bit_position (field);
6080 if (constructor_incremental
6081 && (!constructor_unfilled_fields
6082 || tree_int_cst_lt (bitpos,
6083 bit_position (constructor_unfilled_fields))))
6084 set_nonincremental_init ();
6086 p = constructor_pending_elts;
6087 while (p)
6089 if (field == p->purpose)
6090 return p->value;
6091 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6092 p = p->left;
6093 else
6094 p = p->right;
6097 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6099 if (constructor_elements
6100 && TREE_PURPOSE (constructor_elements) == field)
6101 return TREE_VALUE (constructor_elements);
6103 return 0;
6106 /* "Output" the next constructor element.
6107 At top level, really output it to assembler code now.
6108 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6109 TYPE is the data type that the containing data type wants here.
6110 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6112 PENDING if non-nil means output pending elements that belong
6113 right after this element. (PENDING is normally 1;
6114 it is 0 while outputting pending elements, to avoid recursion.) */
6116 static void
6117 output_init_element (value, type, field, pending)
6118 tree value, type, field;
6119 int pending;
6121 if (type == error_mark_node)
6123 constructor_erroneous = 1;
6124 return;
6126 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6127 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6128 && !(TREE_CODE (value) == STRING_CST
6129 && TREE_CODE (type) == ARRAY_TYPE
6130 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6131 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6132 TYPE_MAIN_VARIANT (type))))
6133 value = default_conversion (value);
6135 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6136 && require_constant_value && !flag_isoc99 && pending)
6138 /* As an extension, allow initializing objects with static storage
6139 duration with compound literals (which are then treated just as
6140 the brace enclosed list they contain). */
6141 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6142 value = DECL_INITIAL (decl);
6145 if (value == error_mark_node)
6146 constructor_erroneous = 1;
6147 else if (!TREE_CONSTANT (value))
6148 constructor_constant = 0;
6149 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6150 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6151 || TREE_CODE (constructor_type) == UNION_TYPE)
6152 && DECL_C_BIT_FIELD (field)
6153 && TREE_CODE (value) != INTEGER_CST))
6154 constructor_simple = 0;
6156 if (require_constant_value && ! TREE_CONSTANT (value))
6158 error_init ("initializer element is not constant");
6159 value = error_mark_node;
6161 else if (require_constant_elements
6162 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6163 pedwarn ("initializer element is not computable at load time");
6165 /* If this field is empty (and not at the end of structure),
6166 don't do anything other than checking the initializer. */
6167 if (field
6168 && (TREE_TYPE (field) == error_mark_node
6169 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6170 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6171 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6172 || TREE_CHAIN (field)))))
6173 return;
6175 value = digest_init (type, value, require_constant_value);
6176 if (value == error_mark_node)
6178 constructor_erroneous = 1;
6179 return;
6182 /* If this element doesn't come next in sequence,
6183 put it on constructor_pending_elts. */
6184 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6185 && (!constructor_incremental
6186 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6188 if (constructor_incremental
6189 && tree_int_cst_lt (field, constructor_unfilled_index))
6190 set_nonincremental_init ();
6192 add_pending_init (field, value);
6193 return;
6195 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6196 && (!constructor_incremental
6197 || field != constructor_unfilled_fields))
6199 /* We do this for records but not for unions. In a union,
6200 no matter which field is specified, it can be initialized
6201 right away since it starts at the beginning of the union. */
6202 if (constructor_incremental)
6204 if (!constructor_unfilled_fields)
6205 set_nonincremental_init ();
6206 else
6208 tree bitpos, unfillpos;
6210 bitpos = bit_position (field);
6211 unfillpos = bit_position (constructor_unfilled_fields);
6213 if (tree_int_cst_lt (bitpos, unfillpos))
6214 set_nonincremental_init ();
6218 add_pending_init (field, value);
6219 return;
6221 else if (TREE_CODE (constructor_type) == UNION_TYPE
6222 && constructor_elements)
6224 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
6225 warning_init ("initialized field with side-effects overwritten");
6227 /* We can have just one union field set. */
6228 constructor_elements = 0;
6231 /* Otherwise, output this element either to
6232 constructor_elements or to the assembler file. */
6234 if (field && TREE_CODE (field) == INTEGER_CST)
6235 field = copy_node (field);
6236 constructor_elements
6237 = tree_cons (field, value, constructor_elements);
6239 /* Advance the variable that indicates sequential elements output. */
6240 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6241 constructor_unfilled_index
6242 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6243 bitsize_one_node);
6244 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6246 constructor_unfilled_fields
6247 = TREE_CHAIN (constructor_unfilled_fields);
6249 /* Skip any nameless bit fields. */
6250 while (constructor_unfilled_fields != 0
6251 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6252 && DECL_NAME (constructor_unfilled_fields) == 0)
6253 constructor_unfilled_fields =
6254 TREE_CHAIN (constructor_unfilled_fields);
6256 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6257 constructor_unfilled_fields = 0;
6259 /* Now output any pending elements which have become next. */
6260 if (pending)
6261 output_pending_init_elements (0);
6264 /* Output any pending elements which have become next.
6265 As we output elements, constructor_unfilled_{fields,index}
6266 advances, which may cause other elements to become next;
6267 if so, they too are output.
6269 If ALL is 0, we return when there are
6270 no more pending elements to output now.
6272 If ALL is 1, we output space as necessary so that
6273 we can output all the pending elements. */
6275 static void
6276 output_pending_init_elements (all)
6277 int all;
6279 struct init_node *elt = constructor_pending_elts;
6280 tree next;
6282 retry:
6284 /* Look thru the whole pending tree.
6285 If we find an element that should be output now,
6286 output it. Otherwise, set NEXT to the element
6287 that comes first among those still pending. */
6289 next = 0;
6290 while (elt)
6292 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6294 if (tree_int_cst_equal (elt->purpose,
6295 constructor_unfilled_index))
6296 output_init_element (elt->value,
6297 TREE_TYPE (constructor_type),
6298 constructor_unfilled_index, 0);
6299 else if (tree_int_cst_lt (constructor_unfilled_index,
6300 elt->purpose))
6302 /* Advance to the next smaller node. */
6303 if (elt->left)
6304 elt = elt->left;
6305 else
6307 /* We have reached the smallest node bigger than the
6308 current unfilled index. Fill the space first. */
6309 next = elt->purpose;
6310 break;
6313 else
6315 /* Advance to the next bigger node. */
6316 if (elt->right)
6317 elt = elt->right;
6318 else
6320 /* We have reached the biggest node in a subtree. Find
6321 the parent of it, which is the next bigger node. */
6322 while (elt->parent && elt->parent->right == elt)
6323 elt = elt->parent;
6324 elt = elt->parent;
6325 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6326 elt->purpose))
6328 next = elt->purpose;
6329 break;
6334 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6335 || TREE_CODE (constructor_type) == UNION_TYPE)
6337 tree ctor_unfilled_bitpos, elt_bitpos;
6339 /* If the current record is complete we are done. */
6340 if (constructor_unfilled_fields == 0)
6341 break;
6343 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6344 elt_bitpos = bit_position (elt->purpose);
6345 /* We can't compare fields here because there might be empty
6346 fields in between. */
6347 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6349 constructor_unfilled_fields = elt->purpose;
6350 output_init_element (elt->value, TREE_TYPE (elt->purpose),
6351 elt->purpose, 0);
6353 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6355 /* Advance to the next smaller node. */
6356 if (elt->left)
6357 elt = elt->left;
6358 else
6360 /* We have reached the smallest node bigger than the
6361 current unfilled field. Fill the space first. */
6362 next = elt->purpose;
6363 break;
6366 else
6368 /* Advance to the next bigger node. */
6369 if (elt->right)
6370 elt = elt->right;
6371 else
6373 /* We have reached the biggest node in a subtree. Find
6374 the parent of it, which is the next bigger node. */
6375 while (elt->parent && elt->parent->right == elt)
6376 elt = elt->parent;
6377 elt = elt->parent;
6378 if (elt
6379 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6380 bit_position (elt->purpose))))
6382 next = elt->purpose;
6383 break;
6390 /* Ordinarily return, but not if we want to output all
6391 and there are elements left. */
6392 if (! (all && next != 0))
6393 return;
6395 /* If it's not incremental, just skip over the gap, so that after
6396 jumping to retry we will output the next successive element. */
6397 if (TREE_CODE (constructor_type) == RECORD_TYPE
6398 || TREE_CODE (constructor_type) == UNION_TYPE)
6399 constructor_unfilled_fields = next;
6400 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6401 constructor_unfilled_index = next;
6403 /* ELT now points to the node in the pending tree with the next
6404 initializer to output. */
6405 goto retry;
6408 /* Add one non-braced element to the current constructor level.
6409 This adjusts the current position within the constructor's type.
6410 This may also start or terminate implicit levels
6411 to handle a partly-braced initializer.
6413 Once this has found the correct level for the new element,
6414 it calls output_init_element. */
6416 void
6417 process_init_element (value)
6418 tree value;
6420 tree orig_value = value;
6421 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6423 designator_depth = 0;
6424 designator_errorneous = 0;
6426 /* Handle superfluous braces around string cst as in
6427 char x[] = {"foo"}; */
6428 if (string_flag
6429 && constructor_type
6430 && TREE_CODE (constructor_type) == ARRAY_TYPE
6431 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6432 && integer_zerop (constructor_unfilled_index))
6434 if (constructor_stack->replacement_value)
6435 error_init ("excess elements in char array initializer");
6436 constructor_stack->replacement_value = value;
6437 return;
6440 if (constructor_stack->replacement_value != 0)
6442 error_init ("excess elements in struct initializer");
6443 return;
6446 /* Ignore elements of a brace group if it is entirely superfluous
6447 and has already been diagnosed. */
6448 if (constructor_type == 0)
6449 return;
6451 /* If we've exhausted any levels that didn't have braces,
6452 pop them now. */
6453 while (constructor_stack->implicit)
6455 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6456 || TREE_CODE (constructor_type) == UNION_TYPE)
6457 && constructor_fields == 0)
6458 process_init_element (pop_init_level (1));
6459 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6460 && (constructor_max_index == 0
6461 || tree_int_cst_lt (constructor_max_index,
6462 constructor_index)))
6463 process_init_element (pop_init_level (1));
6464 else
6465 break;
6468 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6469 if (constructor_range_stack)
6471 /* If value is a compound literal and we'll be just using its
6472 content, don't put it into a SAVE_EXPR. */
6473 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
6474 || !require_constant_value
6475 || flag_isoc99)
6476 value = save_expr (value);
6479 while (1)
6481 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6483 tree fieldtype;
6484 enum tree_code fieldcode;
6486 if (constructor_fields == 0)
6488 pedwarn_init ("excess elements in struct initializer");
6489 break;
6492 fieldtype = TREE_TYPE (constructor_fields);
6493 if (fieldtype != error_mark_node)
6494 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6495 fieldcode = TREE_CODE (fieldtype);
6497 /* Error for non-static initialization of a flexible array member. */
6498 if (fieldcode == ARRAY_TYPE
6499 && !require_constant_value
6500 && TYPE_SIZE (fieldtype) == NULL_TREE
6501 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6503 error_init ("non-static initialization of a flexible array member");
6504 break;
6507 /* Accept a string constant to initialize a subarray. */
6508 if (value != 0
6509 && fieldcode == ARRAY_TYPE
6510 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6511 && string_flag)
6512 value = orig_value;
6513 /* Otherwise, if we have come to a subaggregate,
6514 and we don't have an element of its type, push into it. */
6515 else if (value != 0 && !constructor_no_implicit
6516 && value != error_mark_node
6517 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6518 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6519 || fieldcode == UNION_TYPE))
6521 push_init_level (1);
6522 continue;
6525 if (value)
6527 push_member_name (constructor_fields);
6528 output_init_element (value, fieldtype, constructor_fields, 1);
6529 RESTORE_SPELLING_DEPTH (constructor_depth);
6531 else
6532 /* Do the bookkeeping for an element that was
6533 directly output as a constructor. */
6535 /* For a record, keep track of end position of last field. */
6536 if (DECL_SIZE (constructor_fields))
6537 constructor_bit_index
6538 = size_binop (PLUS_EXPR,
6539 bit_position (constructor_fields),
6540 DECL_SIZE (constructor_fields));
6542 /* If the current field was the first one not yet written out,
6543 it isn't now, so update. */
6544 if (constructor_unfilled_fields == constructor_fields)
6546 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6547 /* Skip any nameless bit fields. */
6548 while (constructor_unfilled_fields != 0
6549 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6550 && DECL_NAME (constructor_unfilled_fields) == 0)
6551 constructor_unfilled_fields =
6552 TREE_CHAIN (constructor_unfilled_fields);
6556 constructor_fields = TREE_CHAIN (constructor_fields);
6557 /* Skip any nameless bit fields at the beginning. */
6558 while (constructor_fields != 0
6559 && DECL_C_BIT_FIELD (constructor_fields)
6560 && DECL_NAME (constructor_fields) == 0)
6561 constructor_fields = TREE_CHAIN (constructor_fields);
6563 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6565 tree fieldtype;
6566 enum tree_code fieldcode;
6568 if (constructor_fields == 0)
6570 pedwarn_init ("excess elements in union initializer");
6571 break;
6574 fieldtype = TREE_TYPE (constructor_fields);
6575 if (fieldtype != error_mark_node)
6576 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6577 fieldcode = TREE_CODE (fieldtype);
6579 /* Warn that traditional C rejects initialization of unions.
6580 We skip the warning if the value is zero. This is done
6581 under the assumption that the zero initializer in user
6582 code appears conditioned on e.g. __STDC__ to avoid
6583 "missing initializer" warnings and relies on default
6584 initialization to zero in the traditional C case.
6585 We also skip the warning if the initializer is designated,
6586 again on the assumption that this must be conditional on
6587 __STDC__ anyway (and we've already complained about the
6588 member-designator already). */
6589 if (warn_traditional && !in_system_header && !constructor_designated
6590 && !(value && (integer_zerop (value) || real_zerop (value))))
6591 warning ("traditional C rejects initialization of unions");
6593 /* Accept a string constant to initialize a subarray. */
6594 if (value != 0
6595 && fieldcode == ARRAY_TYPE
6596 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6597 && string_flag)
6598 value = orig_value;
6599 /* Otherwise, if we have come to a subaggregate,
6600 and we don't have an element of its type, push into it. */
6601 else if (value != 0 && !constructor_no_implicit
6602 && value != error_mark_node
6603 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6604 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6605 || fieldcode == UNION_TYPE))
6607 push_init_level (1);
6608 continue;
6611 if (value)
6613 push_member_name (constructor_fields);
6614 output_init_element (value, fieldtype, constructor_fields, 1);
6615 RESTORE_SPELLING_DEPTH (constructor_depth);
6617 else
6618 /* Do the bookkeeping for an element that was
6619 directly output as a constructor. */
6621 constructor_bit_index = DECL_SIZE (constructor_fields);
6622 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6625 constructor_fields = 0;
6627 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6629 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6630 enum tree_code eltcode = TREE_CODE (elttype);
6632 /* Accept a string constant to initialize a subarray. */
6633 if (value != 0
6634 && eltcode == ARRAY_TYPE
6635 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6636 && string_flag)
6637 value = orig_value;
6638 /* Otherwise, if we have come to a subaggregate,
6639 and we don't have an element of its type, push into it. */
6640 else if (value != 0 && !constructor_no_implicit
6641 && value != error_mark_node
6642 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6643 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6644 || eltcode == UNION_TYPE))
6646 push_init_level (1);
6647 continue;
6650 if (constructor_max_index != 0
6651 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6652 || integer_all_onesp (constructor_max_index)))
6654 pedwarn_init ("excess elements in array initializer");
6655 break;
6658 /* Now output the actual element. */
6659 if (value)
6661 push_array_bounds (tree_low_cst (constructor_index, 0));
6662 output_init_element (value, elttype, constructor_index, 1);
6663 RESTORE_SPELLING_DEPTH (constructor_depth);
6666 constructor_index
6667 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6669 if (! value)
6670 /* If we are doing the bookkeeping for an element that was
6671 directly output as a constructor, we must update
6672 constructor_unfilled_index. */
6673 constructor_unfilled_index = constructor_index;
6675 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6677 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6679 /* Do a basic check of initializer size. Note that vectors
6680 always have a fixed size derived from their type. */
6681 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6683 pedwarn_init ("excess elements in vector initializer");
6684 break;
6687 /* Now output the actual element. */
6688 if (value)
6689 output_init_element (value, elttype, constructor_index, 1);
6691 constructor_index
6692 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6694 if (! value)
6695 /* If we are doing the bookkeeping for an element that was
6696 directly output as a constructor, we must update
6697 constructor_unfilled_index. */
6698 constructor_unfilled_index = constructor_index;
6701 /* Handle the sole element allowed in a braced initializer
6702 for a scalar variable. */
6703 else if (constructor_fields == 0)
6705 pedwarn_init ("excess elements in scalar initializer");
6706 break;
6708 else
6710 if (value)
6711 output_init_element (value, constructor_type, NULL_TREE, 1);
6712 constructor_fields = 0;
6715 /* Handle range initializers either at this level or anywhere higher
6716 in the designator stack. */
6717 if (constructor_range_stack)
6719 struct constructor_range_stack *p, *range_stack;
6720 int finish = 0;
6722 range_stack = constructor_range_stack;
6723 constructor_range_stack = 0;
6724 while (constructor_stack != range_stack->stack)
6726 if (!constructor_stack->implicit)
6727 abort ();
6728 process_init_element (pop_init_level (1));
6730 for (p = range_stack;
6731 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6732 p = p->prev)
6734 if (!constructor_stack->implicit)
6735 abort ();
6736 process_init_element (pop_init_level (1));
6739 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6740 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6741 finish = 1;
6743 while (1)
6745 constructor_index = p->index;
6746 constructor_fields = p->fields;
6747 if (finish && p->range_end && p->index == p->range_start)
6749 finish = 0;
6750 p->prev = 0;
6752 p = p->next;
6753 if (!p)
6754 break;
6755 push_init_level (2);
6756 p->stack = constructor_stack;
6757 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6758 p->index = p->range_start;
6761 if (!finish)
6762 constructor_range_stack = range_stack;
6763 continue;
6766 break;
6769 constructor_range_stack = 0;
6772 /* Build a simple asm-statement, from one string literal. */
6773 tree
6774 simple_asm_stmt (expr)
6775 tree expr;
6777 STRIP_NOPS (expr);
6779 if (TREE_CODE (expr) == ADDR_EXPR)
6780 expr = TREE_OPERAND (expr, 0);
6782 if (TREE_CODE (expr) == STRING_CST)
6784 tree stmt;
6786 /* Simple asm statements are treated as volatile. */
6787 stmt = add_stmt (build_stmt (ASM_STMT, ridpointers[(int) RID_VOLATILE],
6788 expr, NULL_TREE, NULL_TREE, NULL_TREE));
6789 ASM_INPUT_P (stmt) = 1;
6790 return stmt;
6793 error ("argument of `asm' is not a constant string");
6794 return NULL_TREE;
6797 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6798 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6800 tree
6801 build_asm_stmt (cv_qualifier, string, outputs, inputs, clobbers)
6802 tree cv_qualifier;
6803 tree string;
6804 tree outputs;
6805 tree inputs;
6806 tree clobbers;
6808 tree tail;
6810 if (TREE_CODE (string) != STRING_CST)
6812 error ("asm template is not a string constant");
6813 return NULL_TREE;
6816 if (cv_qualifier != NULL_TREE
6817 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6819 warning ("%s qualifier ignored on asm",
6820 IDENTIFIER_POINTER (cv_qualifier));
6821 cv_qualifier = NULL_TREE;
6824 /* We can remove output conversions that change the type,
6825 but not the mode. */
6826 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6828 tree output = TREE_VALUE (tail);
6830 STRIP_NOPS (output);
6831 TREE_VALUE (tail) = output;
6833 /* Allow conversions as LHS here. build_modify_expr as called below
6834 will do the right thing with them. */
6835 while (TREE_CODE (output) == NOP_EXPR
6836 || TREE_CODE (output) == CONVERT_EXPR
6837 || TREE_CODE (output) == FLOAT_EXPR
6838 || TREE_CODE (output) == FIX_TRUNC_EXPR
6839 || TREE_CODE (output) == FIX_FLOOR_EXPR
6840 || TREE_CODE (output) == FIX_ROUND_EXPR
6841 || TREE_CODE (output) == FIX_CEIL_EXPR)
6842 output = TREE_OPERAND (output, 0);
6844 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6847 /* Remove output conversions that change the type but not the mode. */
6848 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6850 tree output = TREE_VALUE (tail);
6851 STRIP_NOPS (output);
6852 TREE_VALUE (tail) = output;
6855 /* Perform default conversions on array and function inputs.
6856 Don't do this for other types as it would screw up operands
6857 expected to be in memory. */
6858 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6859 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6861 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6862 outputs, inputs, clobbers));
6865 /* Expand an ASM statement with operands, handling output operands
6866 that are not variables or INDIRECT_REFS by transforming such
6867 cases into cases that expand_asm_operands can handle.
6869 Arguments are same as for expand_asm_operands. */
6871 void
6872 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6873 tree string, outputs, inputs, clobbers;
6874 int vol;
6875 const char *filename;
6876 int line;
6878 int noutputs = list_length (outputs);
6879 int i;
6880 /* o[I] is the place that output number I should be written. */
6881 tree *o = (tree *) alloca (noutputs * sizeof (tree));
6882 tree tail;
6884 /* Record the contents of OUTPUTS before it is modified. */
6885 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6887 o[i] = TREE_VALUE (tail);
6888 if (o[i] == error_mark_node)
6889 return;
6892 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6893 OUTPUTS some trees for where the values were actually stored. */
6894 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6896 /* Copy all the intermediate outputs into the specified outputs. */
6897 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6899 if (o[i] != TREE_VALUE (tail))
6901 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6902 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6903 free_temp_slots ();
6905 /* Restore the original value so that it's correct the next
6906 time we expand this function. */
6907 TREE_VALUE (tail) = o[i];
6909 /* Detect modification of read-only values.
6910 (Otherwise done by build_modify_expr.) */
6911 else
6913 tree type = TREE_TYPE (o[i]);
6914 if (TREE_READONLY (o[i])
6915 || TYPE_READONLY (type)
6916 || ((TREE_CODE (type) == RECORD_TYPE
6917 || TREE_CODE (type) == UNION_TYPE)
6918 && C_TYPE_FIELDS_READONLY (type)))
6919 readonly_warning (o[i], "modification by `asm'");
6923 /* Those MODIFY_EXPRs could do autoincrements. */
6924 emit_queue ();
6927 /* Expand a C `return' statement.
6928 RETVAL is the expression for what to return,
6929 or a null pointer for `return;' with no value. */
6931 tree
6932 c_expand_return (retval)
6933 tree retval;
6935 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6937 if (TREE_THIS_VOLATILE (current_function_decl))
6938 warning ("function declared `noreturn' has a `return' statement");
6940 if (!retval)
6942 current_function_returns_null = 1;
6943 if ((warn_return_type || flag_isoc99)
6944 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6945 pedwarn_c99 ("`return' with no value, in function returning non-void");
6947 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6949 current_function_returns_null = 1;
6950 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6951 pedwarn ("`return' with a value, in function returning void");
6953 else
6955 tree t = convert_for_assignment (valtype, retval, _("return"),
6956 NULL_TREE, NULL_TREE, 0);
6957 tree res = DECL_RESULT (current_function_decl);
6958 tree inner;
6960 current_function_returns_value = 1;
6961 if (t == error_mark_node)
6962 return NULL_TREE;
6964 inner = t = convert (TREE_TYPE (res), t);
6966 /* Strip any conversions, additions, and subtractions, and see if
6967 we are returning the address of a local variable. Warn if so. */
6968 while (1)
6970 switch (TREE_CODE (inner))
6972 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6973 case PLUS_EXPR:
6974 inner = TREE_OPERAND (inner, 0);
6975 continue;
6977 case MINUS_EXPR:
6978 /* If the second operand of the MINUS_EXPR has a pointer
6979 type (or is converted from it), this may be valid, so
6980 don't give a warning. */
6982 tree op1 = TREE_OPERAND (inner, 1);
6984 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6985 && (TREE_CODE (op1) == NOP_EXPR
6986 || TREE_CODE (op1) == NON_LVALUE_EXPR
6987 || TREE_CODE (op1) == CONVERT_EXPR))
6988 op1 = TREE_OPERAND (op1, 0);
6990 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6991 break;
6993 inner = TREE_OPERAND (inner, 0);
6994 continue;
6997 case ADDR_EXPR:
6998 inner = TREE_OPERAND (inner, 0);
7000 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
7001 inner = TREE_OPERAND (inner, 0);
7003 if (TREE_CODE (inner) == VAR_DECL
7004 && ! DECL_EXTERNAL (inner)
7005 && ! TREE_STATIC (inner)
7006 && DECL_CONTEXT (inner) == current_function_decl)
7007 warning ("function returns address of local variable");
7008 break;
7010 default:
7011 break;
7014 break;
7017 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
7020 return add_stmt (build_return_stmt (retval));
7023 struct c_switch {
7024 /* The SWITCH_STMT being built. */
7025 tree switch_stmt;
7026 /* A splay-tree mapping the low element of a case range to the high
7027 element, or NULL_TREE if there is no high element. Used to
7028 determine whether or not a new case label duplicates an old case
7029 label. We need a tree, rather than simply a hash table, because
7030 of the GNU case range extension. */
7031 splay_tree cases;
7032 /* The next node on the stack. */
7033 struct c_switch *next;
7036 /* A stack of the currently active switch statements. The innermost
7037 switch statement is on the top of the stack. There is no need to
7038 mark the stack for garbage collection because it is only active
7039 during the processing of the body of a function, and we never
7040 collect at that point. */
7042 static struct c_switch *switch_stack;
7044 /* Start a C switch statement, testing expression EXP. Return the new
7045 SWITCH_STMT. */
7047 tree
7048 c_start_case (exp)
7049 tree exp;
7051 enum tree_code code;
7052 tree type, orig_type = error_mark_node;
7053 struct c_switch *cs;
7055 if (exp != error_mark_node)
7057 code = TREE_CODE (TREE_TYPE (exp));
7058 orig_type = TREE_TYPE (exp);
7060 if (! INTEGRAL_TYPE_P (orig_type)
7061 && code != ERROR_MARK)
7063 error ("switch quantity not an integer");
7064 exp = integer_zero_node;
7066 else
7068 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7070 if (warn_traditional && !in_system_header
7071 && (type == long_integer_type_node
7072 || type == long_unsigned_type_node))
7073 warning ("`long' switch expression not converted to `int' in ISO C");
7075 exp = default_conversion (exp);
7076 type = TREE_TYPE (exp);
7080 /* Add this new SWITCH_STMT to the stack. */
7081 cs = (struct c_switch *) xmalloc (sizeof (*cs));
7082 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
7083 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7084 cs->next = switch_stack;
7085 switch_stack = cs;
7087 return add_stmt (switch_stack->switch_stmt);
7090 /* Process a case label. */
7092 tree
7093 do_case (low_value, high_value)
7094 tree low_value;
7095 tree high_value;
7097 tree label = NULL_TREE;
7099 if (switch_stack)
7101 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
7103 label = c_add_case_label (switch_stack->cases,
7104 SWITCH_COND (switch_stack->switch_stmt),
7105 low_value, high_value);
7106 if (label == error_mark_node)
7107 label = NULL_TREE;
7108 else if (switch_was_empty_p)
7110 /* Attach the first case label to the SWITCH_BODY. */
7111 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
7112 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
7115 else if (low_value)
7116 error ("case label not within a switch statement");
7117 else
7118 error ("`default' label not within a switch statement");
7120 return label;
7123 /* Finish the switch statement. */
7125 void
7126 c_finish_case ()
7128 struct c_switch *cs = switch_stack;
7130 /* Rechain the next statements to the SWITCH_STMT. */
7131 last_tree = cs->switch_stmt;
7133 /* Pop the stack. */
7134 switch_stack = switch_stack->next;
7135 splay_tree_delete (cs->cases);
7136 free (cs);