config/sparc/sol2-bi.h: Revert previous delta.
[official-gcc.git] / gcc / c-typeck.c
blobce37eea09f74ebeb1cedef66be4ed4e1f51a81b5
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 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 tree convert_arguments PARAMS ((tree, tree, tree, tree));
63 static tree pointer_diff PARAMS ((tree, tree));
64 static tree unary_complex_lvalue PARAMS ((enum tree_code, tree, int));
65 static void pedantic_lvalue_warning PARAMS ((enum tree_code));
66 static tree internal_build_compound_expr PARAMS ((tree, int));
67 static tree convert_for_assignment PARAMS ((tree, tree, const char *,
68 tree, tree, int));
69 static void warn_for_assignment PARAMS ((const char *, const char *,
70 tree, int));
71 static tree valid_compound_expr_initializer PARAMS ((tree, tree));
72 static void push_string PARAMS ((const char *));
73 static void push_member_name PARAMS ((tree));
74 static void push_array_bounds PARAMS ((int));
75 static int spelling_length PARAMS ((void));
76 static char *print_spelling PARAMS ((char *));
77 static void warning_init PARAMS ((const char *));
78 static tree digest_init PARAMS ((tree, tree, int));
79 static void output_init_element PARAMS ((tree, tree, tree, int));
80 static void output_pending_init_elements PARAMS ((int));
81 static int set_designator PARAMS ((int));
82 static void push_range_stack PARAMS ((tree));
83 static void add_pending_init PARAMS ((tree, tree));
84 static void set_nonincremental_init PARAMS ((void));
85 static void set_nonincremental_init_from_string PARAMS ((tree));
86 static tree find_init_member PARAMS ((tree));
88 /* Do `exp = require_complete_type (exp);' to make sure exp
89 does not have an incomplete type. (That includes void types.) */
91 tree
92 require_complete_type (value)
93 tree value;
95 tree type = TREE_TYPE (value);
97 if (value == error_mark_node || type == error_mark_node)
98 return error_mark_node;
100 /* First, detect a valid value with a complete type. */
101 if (COMPLETE_TYPE_P (type))
102 return value;
104 c_incomplete_type_error (value, type);
105 return error_mark_node;
108 /* Print an error message for invalid use of an incomplete type.
109 VALUE is the expression that was used (or 0 if that isn't known)
110 and TYPE is the type that was invalid. */
112 void
113 c_incomplete_type_error (value, type)
114 tree value;
115 tree type;
117 const char *type_code_string;
119 /* Avoid duplicate error message. */
120 if (TREE_CODE (type) == ERROR_MARK)
121 return;
123 if (value != 0 && (TREE_CODE (value) == VAR_DECL
124 || TREE_CODE (value) == PARM_DECL))
125 error ("`%s' has an incomplete type",
126 IDENTIFIER_POINTER (DECL_NAME (value)));
127 else
129 retry:
130 /* We must print an error message. Be clever about what it says. */
132 switch (TREE_CODE (type))
134 case RECORD_TYPE:
135 type_code_string = "struct";
136 break;
138 case UNION_TYPE:
139 type_code_string = "union";
140 break;
142 case ENUMERAL_TYPE:
143 type_code_string = "enum";
144 break;
146 case VOID_TYPE:
147 error ("invalid use of void expression");
148 return;
150 case ARRAY_TYPE:
151 if (TYPE_DOMAIN (type))
153 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
155 error ("invalid use of flexible array member");
156 return;
158 type = TREE_TYPE (type);
159 goto retry;
161 error ("invalid use of array with unspecified bounds");
162 return;
164 default:
165 abort ();
168 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
169 error ("invalid use of undefined type `%s %s'",
170 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
171 else
172 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
173 error ("invalid use of incomplete typedef `%s'",
174 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
178 /* Given a type, apply default promotions wrt unnamed function
179 arguments and return the new type. */
181 tree
182 c_type_promotes_to (type)
183 tree type;
185 if (TYPE_MAIN_VARIANT (type) == float_type_node)
186 return double_type_node;
188 if (c_promoting_integer_type_p (type))
190 /* Preserve unsignedness if not really getting any wider. */
191 if (TREE_UNSIGNED (type)
192 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
193 return unsigned_type_node;
194 return integer_type_node;
197 return type;
200 /* Return a variant of TYPE which has all the type qualifiers of LIKE
201 as well as those of TYPE. */
203 static tree
204 qualify_type (type, like)
205 tree type, like;
207 return c_build_qualified_type (type,
208 TYPE_QUALS (type) | TYPE_QUALS (like));
211 /* Return the common type of two types.
212 We assume that comptypes has already been done and returned 1;
213 if that isn't so, this may crash. In particular, we assume that qualifiers
214 match.
216 This is the type for the result of most arithmetic operations
217 if the operands have the given two types. */
219 tree
220 common_type (t1, t2)
221 tree t1, t2;
223 enum tree_code code1;
224 enum tree_code code2;
225 tree attributes;
227 /* Save time if the two types are the same. */
229 if (t1 == t2) return t1;
231 /* If one type is nonsense, use the other. */
232 if (t1 == error_mark_node)
233 return t2;
234 if (t2 == error_mark_node)
235 return t1;
237 /* Merge the attributes. */
238 attributes = (*targetm.merge_type_attributes) (t1, t2);
240 /* Treat an enum type as the unsigned integer type of the same width. */
242 if (TREE_CODE (t1) == ENUMERAL_TYPE)
243 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
244 if (TREE_CODE (t2) == ENUMERAL_TYPE)
245 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
247 code1 = TREE_CODE (t1);
248 code2 = TREE_CODE (t2);
250 /* If one type is complex, form the common type of the non-complex
251 components, then make that complex. Use T1 or T2 if it is the
252 required type. */
253 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
255 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
256 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
257 tree subtype = common_type (subtype1, subtype2);
259 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
260 return build_type_attribute_variant (t1, attributes);
261 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
262 return build_type_attribute_variant (t2, attributes);
263 else
264 return build_type_attribute_variant (build_complex_type (subtype),
265 attributes);
268 switch (code1)
270 case INTEGER_TYPE:
271 case REAL_TYPE:
272 /* If only one is real, use it as the result. */
274 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
275 return build_type_attribute_variant (t1, attributes);
277 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
278 return build_type_attribute_variant (t2, attributes);
280 /* Both real or both integers; use the one with greater precision. */
282 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
283 return build_type_attribute_variant (t1, attributes);
284 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
285 return build_type_attribute_variant (t2, attributes);
287 /* Same precision. Prefer longs to ints even when same size. */
289 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
290 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
291 return build_type_attribute_variant (long_unsigned_type_node,
292 attributes);
294 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
295 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
297 /* But preserve unsignedness from the other type,
298 since long cannot hold all the values of an unsigned int. */
299 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
300 t1 = long_unsigned_type_node;
301 else
302 t1 = long_integer_type_node;
303 return build_type_attribute_variant (t1, attributes);
306 /* Likewise, prefer long double to double even if same size. */
307 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
308 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
309 return build_type_attribute_variant (long_double_type_node,
310 attributes);
312 /* Otherwise prefer the unsigned one. */
314 if (TREE_UNSIGNED (t1))
315 return build_type_attribute_variant (t1, attributes);
316 else
317 return build_type_attribute_variant (t2, attributes);
319 case POINTER_TYPE:
320 /* For two pointers, do this recursively on the target type,
321 and combine the qualifiers of the two types' targets. */
322 /* This code was turned off; I don't know why.
323 But ANSI C specifies doing this with the qualifiers.
324 So I turned it on again. */
326 tree pointed_to_1 = TREE_TYPE (t1);
327 tree pointed_to_2 = TREE_TYPE (t2);
328 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
329 TYPE_MAIN_VARIANT (pointed_to_2));
330 t1 = build_pointer_type (c_build_qualified_type
331 (target,
332 TYPE_QUALS (pointed_to_1) |
333 TYPE_QUALS (pointed_to_2)));
334 return build_type_attribute_variant (t1, attributes);
336 #if 0
337 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
338 return build_type_attribute_variant (t1, attributes);
339 #endif
341 case ARRAY_TYPE:
343 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
344 /* Save space: see if the result is identical to one of the args. */
345 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
346 return build_type_attribute_variant (t1, attributes);
347 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
348 return build_type_attribute_variant (t2, attributes);
349 /* Merge the element types, and have a size if either arg has one. */
350 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
351 return build_type_attribute_variant (t1, attributes);
354 case FUNCTION_TYPE:
355 /* Function types: prefer the one that specified arg types.
356 If both do, merge the arg types. Also merge the return types. */
358 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
359 tree p1 = TYPE_ARG_TYPES (t1);
360 tree p2 = TYPE_ARG_TYPES (t2);
361 int len;
362 tree newargs, n;
363 int i;
365 /* Save space: see if the result is identical to one of the args. */
366 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
367 return build_type_attribute_variant (t1, attributes);
368 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
369 return build_type_attribute_variant (t2, attributes);
371 /* Simple way if one arg fails to specify argument types. */
372 if (TYPE_ARG_TYPES (t1) == 0)
374 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
375 return build_type_attribute_variant (t1, attributes);
377 if (TYPE_ARG_TYPES (t2) == 0)
379 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
380 return build_type_attribute_variant (t1, attributes);
383 /* If both args specify argument types, we must merge the two
384 lists, argument by argument. */
386 pushlevel (0);
387 declare_parm_level (1);
389 len = list_length (p1);
390 newargs = 0;
392 for (i = 0; i < len; i++)
393 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
395 n = newargs;
397 for (; p1;
398 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
400 /* A null type means arg type is not specified.
401 Take whatever the other function type has. */
402 if (TREE_VALUE (p1) == 0)
404 TREE_VALUE (n) = TREE_VALUE (p2);
405 goto parm_done;
407 if (TREE_VALUE (p2) == 0)
409 TREE_VALUE (n) = TREE_VALUE (p1);
410 goto parm_done;
413 /* Given wait (union {union wait *u; int *i} *)
414 and wait (union wait *),
415 prefer union wait * as type of parm. */
416 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
417 && TREE_VALUE (p1) != TREE_VALUE (p2))
419 tree memb;
420 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
421 memb; memb = TREE_CHAIN (memb))
422 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
424 TREE_VALUE (n) = TREE_VALUE (p2);
425 if (pedantic)
426 pedwarn ("function types not truly compatible in ISO C");
427 goto parm_done;
430 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
431 && TREE_VALUE (p2) != TREE_VALUE (p1))
433 tree memb;
434 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
435 memb; memb = TREE_CHAIN (memb))
436 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
438 TREE_VALUE (n) = TREE_VALUE (p1);
439 if (pedantic)
440 pedwarn ("function types not truly compatible in ISO C");
441 goto parm_done;
444 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
445 parm_done: ;
448 poplevel (0, 0, 0);
450 t1 = build_function_type (valtype, newargs);
451 /* ... falls through ... */
454 default:
455 return build_type_attribute_variant (t1, attributes);
460 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
461 or various other operations. Return 2 if they are compatible
462 but a warning may be needed if you use them together. */
465 comptypes (type1, type2)
466 tree type1, type2;
468 tree t1 = type1;
469 tree t2 = type2;
470 int attrval, val;
472 /* Suppress errors caused by previously reported errors. */
474 if (t1 == t2 || !t1 || !t2
475 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
476 return 1;
478 /* If either type is the internal version of sizetype, return the
479 language version. */
480 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
481 && TYPE_DOMAIN (t1) != 0)
482 t1 = TYPE_DOMAIN (t1);
484 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
485 && TYPE_DOMAIN (t2) != 0)
486 t2 = TYPE_DOMAIN (t2);
488 /* Treat an enum type as the integer type of the same width and
489 signedness. */
491 if (TREE_CODE (t1) == ENUMERAL_TYPE)
492 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
493 if (TREE_CODE (t2) == ENUMERAL_TYPE)
494 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
496 if (t1 == t2)
497 return 1;
499 /* Different classes of types can't be compatible. */
501 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
503 /* Qualifiers must match. */
505 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
506 return 0;
508 /* Allow for two different type nodes which have essentially the same
509 definition. Note that we already checked for equality of the type
510 qualifiers (just above). */
512 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
513 return 1;
515 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
516 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
517 return 0;
519 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
520 val = 0;
522 switch (TREE_CODE (t1))
524 case POINTER_TYPE:
525 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
526 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
527 break;
529 case FUNCTION_TYPE:
530 val = function_types_compatible_p (t1, t2);
531 break;
533 case ARRAY_TYPE:
535 tree d1 = TYPE_DOMAIN (t1);
536 tree d2 = TYPE_DOMAIN (t2);
537 bool d1_variable, d2_variable;
538 bool d1_zero, d2_zero;
539 val = 1;
541 /* Target types must match incl. qualifiers. */
542 if (TREE_TYPE (t1) != TREE_TYPE (t2)
543 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
544 return 0;
546 /* Sizes must match unless one is missing or variable. */
547 if (d1 == 0 || d2 == 0 || d1 == d2)
548 break;
550 d1_zero = ! TYPE_MAX_VALUE (d1);
551 d2_zero = ! TYPE_MAX_VALUE (d2);
553 d1_variable = (! d1_zero
554 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
555 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
556 d2_variable = (! d2_zero
557 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
558 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
560 if (d1_variable || d2_variable)
561 break;
562 if (d1_zero && d2_zero)
563 break;
564 if (d1_zero || d2_zero
565 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
566 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
567 val = 0;
569 break;
572 case RECORD_TYPE:
573 if (flag_objc && objc_comptypes (t1, t2, 0) == 1)
574 val = 1;
575 break;
577 case VECTOR_TYPE:
578 /* The target might allow certain vector types to be compatible. */
579 val = (*targetm.vector_opaque_p) (t1)
580 || (*targetm.vector_opaque_p) (t2);
581 break;
583 default:
584 break;
586 return attrval == 2 && val == 1 ? 2 : val;
589 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
590 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
591 to 1 or 0 depending if the check of the pointer types is meant to
592 be reflexive or not (typically, assignments are not reflexive,
593 while comparisons are reflexive).
596 static int
597 comp_target_types (ttl, ttr, reflexive)
598 tree ttl, ttr;
599 int reflexive;
601 int val;
603 /* Give objc_comptypes a crack at letting these types through. */
604 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
605 return val;
607 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
608 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
610 if (val == 2 && pedantic)
611 pedwarn ("types are not quite compatible");
612 return val;
615 /* Subroutines of `comptypes'. */
617 /* Return 1 if two function types F1 and F2 are compatible.
618 If either type specifies no argument types,
619 the other must specify a fixed number of self-promoting arg types.
620 Otherwise, if one type specifies only the number of arguments,
621 the other must specify that number of self-promoting arg types.
622 Otherwise, the argument types must match. */
624 static int
625 function_types_compatible_p (f1, f2)
626 tree f1, f2;
628 tree args1, args2;
629 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
630 int val = 1;
631 int val1;
633 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
634 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
635 return 0;
637 args1 = TYPE_ARG_TYPES (f1);
638 args2 = TYPE_ARG_TYPES (f2);
640 /* An unspecified parmlist matches any specified parmlist
641 whose argument types don't need default promotions. */
643 if (args1 == 0)
645 if (!self_promoting_args_p (args2))
646 return 0;
647 /* If one of these types comes from a non-prototype fn definition,
648 compare that with the other type's arglist.
649 If they don't match, ask for a warning (but no error). */
650 if (TYPE_ACTUAL_ARG_TYPES (f1)
651 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
652 val = 2;
653 return val;
655 if (args2 == 0)
657 if (!self_promoting_args_p (args1))
658 return 0;
659 if (TYPE_ACTUAL_ARG_TYPES (f2)
660 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
661 val = 2;
662 return val;
665 /* Both types have argument lists: compare them and propagate results. */
666 val1 = type_lists_compatible_p (args1, args2);
667 return val1 != 1 ? val1 : val;
670 /* Check two lists of types for compatibility,
671 returning 0 for incompatible, 1 for compatible,
672 or 2 for compatible with warning. */
674 static int
675 type_lists_compatible_p (args1, args2)
676 tree args1, args2;
678 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
679 int val = 1;
680 int newval = 0;
682 while (1)
684 if (args1 == 0 && args2 == 0)
685 return val;
686 /* If one list is shorter than the other,
687 they fail to match. */
688 if (args1 == 0 || args2 == 0)
689 return 0;
690 /* A null pointer instead of a type
691 means there is supposed to be an argument
692 but nothing is specified about what type it has.
693 So match anything that self-promotes. */
694 if (TREE_VALUE (args1) == 0)
696 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
697 return 0;
699 else if (TREE_VALUE (args2) == 0)
701 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
702 return 0;
704 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
705 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
707 /* Allow wait (union {union wait *u; int *i} *)
708 and wait (union wait *) to be compatible. */
709 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
710 && (TYPE_NAME (TREE_VALUE (args1)) == 0
711 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
712 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
713 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
714 TYPE_SIZE (TREE_VALUE (args2))))
716 tree memb;
717 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
718 memb; memb = TREE_CHAIN (memb))
719 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
720 break;
721 if (memb == 0)
722 return 0;
724 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
725 && (TYPE_NAME (TREE_VALUE (args2)) == 0
726 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
727 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
728 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
729 TYPE_SIZE (TREE_VALUE (args1))))
731 tree memb;
732 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
733 memb; memb = TREE_CHAIN (memb))
734 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
735 break;
736 if (memb == 0)
737 return 0;
739 else
740 return 0;
743 /* comptypes said ok, but record if it said to warn. */
744 if (newval > val)
745 val = newval;
747 args1 = TREE_CHAIN (args1);
748 args2 = TREE_CHAIN (args2);
752 /* Compute the size to increment a pointer by. */
754 tree
755 c_size_in_bytes (type)
756 tree type;
758 enum tree_code code = TREE_CODE (type);
760 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
761 return size_one_node;
763 if (!COMPLETE_OR_VOID_TYPE_P (type))
765 error ("arithmetic on pointer to an incomplete type");
766 return size_one_node;
769 /* Convert in case a char is more than one unit. */
770 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
771 size_int (TYPE_PRECISION (char_type_node)
772 / BITS_PER_UNIT));
775 /* Return either DECL or its known constant value (if it has one). */
777 tree
778 decl_constant_value (decl)
779 tree decl;
781 if (/* Don't change a variable array bound or initial value to a constant
782 in a place where a variable is invalid. */
783 current_function_decl != 0
784 && ! TREE_THIS_VOLATILE (decl)
785 && TREE_READONLY (decl)
786 && DECL_INITIAL (decl) != 0
787 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
788 /* This is invalid if initial value is not constant.
789 If it has either a function call, a memory reference,
790 or a variable, then re-evaluating it could give different results. */
791 && TREE_CONSTANT (DECL_INITIAL (decl))
792 /* Check for cases where this is sub-optimal, even though valid. */
793 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
794 return DECL_INITIAL (decl);
795 return decl;
798 /* Return either DECL or its known constant value (if it has one), but
799 return DECL if pedantic or DECL has mode BLKmode. This is for
800 bug-compatibility with the old behavior of decl_constant_value
801 (before GCC 3.0); every use of this function is a bug and it should
802 be removed before GCC 3.1. It is not appropriate to use pedantic
803 in a way that affects optimization, and BLKmode is probably not the
804 right test for avoiding misoptimizations either. */
806 static tree
807 decl_constant_value_for_broken_optimization (decl)
808 tree decl;
810 if (pedantic || DECL_MODE (decl) == BLKmode)
811 return decl;
812 else
813 return decl_constant_value (decl);
817 /* Perform the default conversion of arrays and functions to pointers.
818 Return the result of converting EXP. For any other expression, just
819 return EXP. */
821 static tree
822 default_function_array_conversion (exp)
823 tree exp;
825 tree orig_exp;
826 tree type = TREE_TYPE (exp);
827 enum tree_code code = TREE_CODE (type);
828 int not_lvalue = 0;
830 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
831 an lvalue.
833 Do not use STRIP_NOPS here! It will remove conversions from pointer
834 to integer and cause infinite recursion. */
835 orig_exp = exp;
836 while (TREE_CODE (exp) == NON_LVALUE_EXPR
837 || (TREE_CODE (exp) == NOP_EXPR
838 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
840 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
841 not_lvalue = 1;
842 exp = TREE_OPERAND (exp, 0);
845 /* Preserve the original expression code. */
846 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
847 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
849 if (code == FUNCTION_TYPE)
851 return build_unary_op (ADDR_EXPR, exp, 0);
853 if (code == ARRAY_TYPE)
855 tree adr;
856 tree restype = TREE_TYPE (type);
857 tree ptrtype;
858 int constp = 0;
859 int volatilep = 0;
860 int lvalue_array_p;
862 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
864 constp = TREE_READONLY (exp);
865 volatilep = TREE_THIS_VOLATILE (exp);
868 if (TYPE_QUALS (type) || constp || volatilep)
869 restype
870 = c_build_qualified_type (restype,
871 TYPE_QUALS (type)
872 | (constp * TYPE_QUAL_CONST)
873 | (volatilep * TYPE_QUAL_VOLATILE));
875 if (TREE_CODE (exp) == INDIRECT_REF)
876 return convert (TYPE_POINTER_TO (restype),
877 TREE_OPERAND (exp, 0));
879 if (TREE_CODE (exp) == COMPOUND_EXPR)
881 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
882 return build (COMPOUND_EXPR, TREE_TYPE (op1),
883 TREE_OPERAND (exp, 0), op1);
886 lvalue_array_p = !not_lvalue && lvalue_p (exp);
887 if (!flag_isoc99 && !lvalue_array_p)
889 /* Before C99, non-lvalue arrays do not decay to pointers.
890 Normally, using such an array would be invalid; but it can
891 be used correctly inside sizeof or as a statement expression.
892 Thus, do not give an error here; an error will result later. */
893 return exp;
896 ptrtype = build_pointer_type (restype);
898 if (TREE_CODE (exp) == VAR_DECL)
900 /* ??? This is not really quite correct
901 in that the type of the operand of ADDR_EXPR
902 is not the target type of the type of the ADDR_EXPR itself.
903 Question is, can this lossage be avoided? */
904 adr = build1 (ADDR_EXPR, ptrtype, exp);
905 if (!c_mark_addressable (exp))
906 return error_mark_node;
907 TREE_CONSTANT (adr) = staticp (exp);
908 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
909 return adr;
911 /* This way is better for a COMPONENT_REF since it can
912 simplify the offset for a component. */
913 adr = build_unary_op (ADDR_EXPR, exp, 1);
914 return convert (ptrtype, adr);
916 return exp;
919 /* Perform default promotions for C data used in expressions.
920 Arrays and functions are converted to pointers;
921 enumeral types or short or char, to int.
922 In addition, manifest constants symbols are replaced by their values. */
924 tree
925 default_conversion (exp)
926 tree exp;
928 tree orig_exp;
929 tree type = TREE_TYPE (exp);
930 enum tree_code code = TREE_CODE (type);
932 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
933 return default_function_array_conversion (exp);
935 /* Constants can be used directly unless they're not loadable. */
936 if (TREE_CODE (exp) == CONST_DECL)
937 exp = DECL_INITIAL (exp);
939 /* Replace a nonvolatile const static variable with its value unless
940 it is an array, in which case we must be sure that taking the
941 address of the array produces consistent results. */
942 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
944 exp = decl_constant_value_for_broken_optimization (exp);
945 type = TREE_TYPE (exp);
948 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
949 an lvalue.
951 Do not use STRIP_NOPS here! It will remove conversions from pointer
952 to integer and cause infinite recursion. */
953 orig_exp = exp;
954 while (TREE_CODE (exp) == NON_LVALUE_EXPR
955 || (TREE_CODE (exp) == NOP_EXPR
956 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
957 exp = TREE_OPERAND (exp, 0);
959 /* Preserve the original expression code. */
960 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
961 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
963 /* Normally convert enums to int,
964 but convert wide enums to something wider. */
965 if (code == ENUMERAL_TYPE)
967 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
968 TYPE_PRECISION (integer_type_node)),
969 ((TYPE_PRECISION (type)
970 >= TYPE_PRECISION (integer_type_node))
971 && TREE_UNSIGNED (type)));
973 return convert (type, exp);
976 if (TREE_CODE (exp) == COMPONENT_REF
977 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
978 /* If it's thinner than an int, promote it like a
979 c_promoting_integer_type_p, otherwise leave it alone. */
980 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
981 TYPE_PRECISION (integer_type_node)))
982 return convert (integer_type_node, exp);
984 if (c_promoting_integer_type_p (type))
986 /* Preserve unsignedness if not really getting any wider. */
987 if (TREE_UNSIGNED (type)
988 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
989 return convert (unsigned_type_node, exp);
991 return convert (integer_type_node, exp);
994 if (code == VOID_TYPE)
996 error ("void value not ignored as it ought to be");
997 return error_mark_node;
999 return exp;
1002 /* Look up COMPONENT in a structure or union DECL.
1004 If the component name is not found, returns NULL_TREE. Otherwise,
1005 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1006 stepping down the chain to the component, which is in the last
1007 TREE_VALUE of the list. Normally the list is of length one, but if
1008 the component is embedded within (nested) anonymous structures or
1009 unions, the list steps down the chain to the component. */
1011 static tree
1012 lookup_field (decl, component)
1013 tree decl, component;
1015 tree type = TREE_TYPE (decl);
1016 tree field;
1018 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1019 to the field elements. Use a binary search on this array to quickly
1020 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1021 will always be set for structures which have many elements. */
1023 if (TYPE_LANG_SPECIFIC (type))
1025 int bot, top, half;
1026 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1028 field = TYPE_FIELDS (type);
1029 bot = 0;
1030 top = TYPE_LANG_SPECIFIC (type)->len;
1031 while (top - bot > 1)
1033 half = (top - bot + 1) >> 1;
1034 field = field_array[bot+half];
1036 if (DECL_NAME (field) == NULL_TREE)
1038 /* Step through all anon unions in linear fashion. */
1039 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1041 field = field_array[bot++];
1042 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1043 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1045 tree anon = lookup_field (field, component);
1047 if (anon)
1048 return tree_cons (NULL_TREE, field, anon);
1052 /* Entire record is only anon unions. */
1053 if (bot > top)
1054 return NULL_TREE;
1056 /* Restart the binary search, with new lower bound. */
1057 continue;
1060 if (DECL_NAME (field) == component)
1061 break;
1062 if (DECL_NAME (field) < component)
1063 bot += half;
1064 else
1065 top = bot + half;
1068 if (DECL_NAME (field_array[bot]) == component)
1069 field = field_array[bot];
1070 else if (DECL_NAME (field) != component)
1071 return NULL_TREE;
1073 else
1075 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1077 if (DECL_NAME (field) == NULL_TREE
1078 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1079 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1081 tree anon = lookup_field (field, component);
1083 if (anon)
1084 return tree_cons (NULL_TREE, field, anon);
1087 if (DECL_NAME (field) == component)
1088 break;
1091 if (field == NULL_TREE)
1092 return NULL_TREE;
1095 return tree_cons (NULL_TREE, field, NULL_TREE);
1098 /* Make an expression to refer to the COMPONENT field of
1099 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1101 tree
1102 build_component_ref (datum, component)
1103 tree datum, component;
1105 tree type = TREE_TYPE (datum);
1106 enum tree_code code = TREE_CODE (type);
1107 tree field = NULL;
1108 tree ref;
1110 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1111 If pedantic ensure that the arguments are not lvalues; otherwise,
1112 if the component is an array, it would wrongly decay to a pointer in
1113 C89 mode.
1114 We cannot do this with a COND_EXPR, because in a conditional expression
1115 the default promotions are applied to both sides, and this would yield
1116 the wrong type of the result; for example, if the components have
1117 type "char". */
1118 switch (TREE_CODE (datum))
1120 case COMPOUND_EXPR:
1122 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1123 return build (COMPOUND_EXPR, TREE_TYPE (value),
1124 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
1126 default:
1127 break;
1130 /* See if there is a field or component with name COMPONENT. */
1132 if (code == RECORD_TYPE || code == UNION_TYPE)
1134 if (!COMPLETE_TYPE_P (type))
1136 c_incomplete_type_error (NULL_TREE, type);
1137 return error_mark_node;
1140 field = lookup_field (datum, component);
1142 if (!field)
1144 error ("%s has no member named `%s'",
1145 code == RECORD_TYPE ? "structure" : "union",
1146 IDENTIFIER_POINTER (component));
1147 return error_mark_node;
1150 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1151 This might be better solved in future the way the C++ front
1152 end does it - by giving the anonymous entities each a
1153 separate name and type, and then have build_component_ref
1154 recursively call itself. We can't do that here. */
1157 tree subdatum = TREE_VALUE (field);
1159 if (TREE_TYPE (subdatum) == error_mark_node)
1160 return error_mark_node;
1162 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1163 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1164 TREE_READONLY (ref) = 1;
1165 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1166 TREE_THIS_VOLATILE (ref) = 1;
1168 if (TREE_DEPRECATED (subdatum))
1169 warn_deprecated_use (subdatum);
1171 datum = ref;
1173 field = TREE_CHAIN (field);
1175 while (field);
1177 return ref;
1179 else if (code != ERROR_MARK)
1180 error ("request for member `%s' in something not a structure or union",
1181 IDENTIFIER_POINTER (component));
1183 return error_mark_node;
1186 /* Given an expression PTR for a pointer, return an expression
1187 for the value pointed to.
1188 ERRORSTRING is the name of the operator to appear in error messages. */
1190 tree
1191 build_indirect_ref (ptr, errorstring)
1192 tree ptr;
1193 const char *errorstring;
1195 tree pointer = default_conversion (ptr);
1196 tree type = TREE_TYPE (pointer);
1198 if (TREE_CODE (type) == POINTER_TYPE)
1200 if (TREE_CODE (pointer) == ADDR_EXPR
1201 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1202 == TREE_TYPE (type)))
1203 return TREE_OPERAND (pointer, 0);
1204 else
1206 tree t = TREE_TYPE (type);
1207 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1209 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1211 error ("dereferencing pointer to incomplete type");
1212 return error_mark_node;
1214 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1215 warning ("dereferencing `void *' pointer");
1217 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1218 so that we get the proper error message if the result is used
1219 to assign to. Also, &* is supposed to be a no-op.
1220 And ANSI C seems to specify that the type of the result
1221 should be the const type. */
1222 /* A de-reference of a pointer to const is not a const. It is valid
1223 to change it via some other pointer. */
1224 TREE_READONLY (ref) = TYPE_READONLY (t);
1225 TREE_SIDE_EFFECTS (ref)
1226 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1227 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1228 return ref;
1231 else if (TREE_CODE (pointer) != ERROR_MARK)
1232 error ("invalid type argument of `%s'", errorstring);
1233 return error_mark_node;
1236 /* This handles expressions of the form "a[i]", which denotes
1237 an array reference.
1239 This is logically equivalent in C to *(a+i), but we may do it differently.
1240 If A is a variable or a member, we generate a primitive ARRAY_REF.
1241 This avoids forcing the array out of registers, and can work on
1242 arrays that are not lvalues (for example, members of structures returned
1243 by functions). */
1245 tree
1246 build_array_ref (array, index)
1247 tree array, index;
1249 if (index == 0)
1251 error ("subscript missing in array reference");
1252 return error_mark_node;
1255 if (TREE_TYPE (array) == error_mark_node
1256 || TREE_TYPE (index) == error_mark_node)
1257 return error_mark_node;
1259 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1260 && TREE_CODE (array) != INDIRECT_REF)
1262 tree rval, type;
1264 /* Subscripting with type char is likely to lose
1265 on a machine where chars are signed.
1266 So warn on any machine, but optionally.
1267 Don't warn for unsigned char since that type is safe.
1268 Don't warn for signed char because anyone who uses that
1269 must have done so deliberately. */
1270 if (warn_char_subscripts
1271 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1272 warning ("array subscript has type `char'");
1274 /* Apply default promotions *after* noticing character types. */
1275 index = default_conversion (index);
1277 /* Require integer *after* promotion, for sake of enums. */
1278 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1280 error ("array subscript is not an integer");
1281 return error_mark_node;
1284 /* An array that is indexed by a non-constant
1285 cannot be stored in a register; we must be able to do
1286 address arithmetic on its address.
1287 Likewise an array of elements of variable size. */
1288 if (TREE_CODE (index) != INTEGER_CST
1289 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1290 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1292 if (!c_mark_addressable (array))
1293 return error_mark_node;
1295 /* An array that is indexed by a constant value which is not within
1296 the array bounds cannot be stored in a register either; because we
1297 would get a crash in store_bit_field/extract_bit_field when trying
1298 to access a non-existent part of the register. */
1299 if (TREE_CODE (index) == INTEGER_CST
1300 && TYPE_VALUES (TREE_TYPE (array))
1301 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1303 if (!c_mark_addressable (array))
1304 return error_mark_node;
1307 if (pedantic)
1309 tree foo = array;
1310 while (TREE_CODE (foo) == COMPONENT_REF)
1311 foo = TREE_OPERAND (foo, 0);
1312 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1313 pedwarn ("ISO C forbids subscripting `register' array");
1314 else if (! flag_isoc99 && ! lvalue_p (foo))
1315 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1318 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1319 rval = build (ARRAY_REF, type, array, index);
1320 /* Array ref is const/volatile if the array elements are
1321 or if the array is. */
1322 TREE_READONLY (rval)
1323 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1324 | TREE_READONLY (array));
1325 TREE_SIDE_EFFECTS (rval)
1326 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1327 | TREE_SIDE_EFFECTS (array));
1328 TREE_THIS_VOLATILE (rval)
1329 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1330 /* This was added by rms on 16 Nov 91.
1331 It fixes vol struct foo *a; a->elts[1]
1332 in an inline function.
1333 Hope it doesn't break something else. */
1334 | TREE_THIS_VOLATILE (array));
1335 return require_complete_type (fold (rval));
1339 tree ar = default_conversion (array);
1340 tree ind = default_conversion (index);
1342 /* Do the same warning check as above, but only on the part that's
1343 syntactically the index and only if it is also semantically
1344 the index. */
1345 if (warn_char_subscripts
1346 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1347 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1348 warning ("subscript has type `char'");
1350 /* Put the integer in IND to simplify error checking. */
1351 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1353 tree temp = ar;
1354 ar = ind;
1355 ind = temp;
1358 if (ar == error_mark_node)
1359 return ar;
1361 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1362 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1364 error ("subscripted value is neither array nor pointer");
1365 return error_mark_node;
1367 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1369 error ("array subscript is not an integer");
1370 return error_mark_node;
1373 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1374 "array indexing");
1378 /* Build an external reference to identifier ID. FUN indicates
1379 whether this will be used for a function call. */
1380 tree
1381 build_external_ref (id, fun)
1382 tree id;
1383 int fun;
1385 tree ref;
1386 tree decl = lookup_name (id);
1387 tree objc_ivar = lookup_objc_ivar (id);
1389 if (decl && TREE_DEPRECATED (decl))
1390 warn_deprecated_use (decl);
1392 if (!decl || decl == error_mark_node || C_DECL_ANTICIPATED (decl))
1394 if (objc_ivar)
1395 ref = objc_ivar;
1396 else if (fun)
1398 if (!decl || decl == error_mark_node)
1399 /* Ordinary implicit function declaration. */
1400 ref = implicitly_declare (id);
1401 else
1403 /* Implicit declaration of built-in function. Don't
1404 change the built-in declaration, but don't let this
1405 go by silently, either. */
1406 implicit_decl_warning (id);
1408 /* only issue this warning once */
1409 C_DECL_ANTICIPATED (decl) = 0;
1410 ref = decl;
1413 else
1415 /* Don't complain about something that's already been
1416 complained about. */
1417 if (decl == error_mark_node)
1418 return error_mark_node;
1420 /* Reference to undeclared variable, including reference to
1421 builtin outside of function-call context. */
1422 if (current_function_decl == 0)
1423 error ("`%s' undeclared here (not in a function)",
1424 IDENTIFIER_POINTER (id));
1425 else
1427 error ("`%s' undeclared (first use in this function)",
1428 IDENTIFIER_POINTER (id));
1430 if (! undeclared_variable_notice)
1432 error ("(Each undeclared identifier is reported only once");
1433 error ("for each function it appears in.)");
1434 undeclared_variable_notice = 1;
1437 /* Set IDENTIFIER_LOCAL_VALUE (id) to error_mark_node and
1438 add a function-scope shadow entry which will undo that.
1439 This suppresses further warnings about this undeclared
1440 identifier in this function. */
1441 record_function_scope_shadow (id);
1442 IDENTIFIER_LOCAL_VALUE (id) = error_mark_node;
1444 return error_mark_node;
1447 else
1449 /* Properly declared variable or function reference. */
1450 if (!objc_ivar)
1451 ref = decl;
1452 else if (decl != objc_ivar && IDENTIFIER_LOCAL_VALUE (id))
1454 warning ("local declaration of `%s' hides instance variable",
1455 IDENTIFIER_POINTER (id));
1456 ref = decl;
1458 else
1459 ref = objc_ivar;
1462 if (TREE_TYPE (ref) == error_mark_node)
1463 return error_mark_node;
1465 if (!skip_evaluation)
1466 assemble_external (ref);
1467 TREE_USED (ref) = 1;
1469 if (TREE_CODE (ref) == CONST_DECL)
1471 ref = DECL_INITIAL (ref);
1472 TREE_CONSTANT (ref) = 1;
1475 return ref;
1478 /* Build a function call to function FUNCTION with parameters PARAMS.
1479 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1480 TREE_VALUE of each node is a parameter-expression.
1481 FUNCTION's data type may be a function type or a pointer-to-function. */
1483 tree
1484 build_function_call (function, params)
1485 tree function, params;
1487 tree fntype, fundecl = 0;
1488 tree coerced_params;
1489 tree name = NULL_TREE, result;
1491 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1492 STRIP_TYPE_NOPS (function);
1494 /* Convert anything with function type to a pointer-to-function. */
1495 if (TREE_CODE (function) == FUNCTION_DECL)
1497 name = DECL_NAME (function);
1499 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1500 (because calling an inline function does not mean the function
1501 needs to be separately compiled). */
1502 fntype = build_type_variant (TREE_TYPE (function),
1503 TREE_READONLY (function),
1504 TREE_THIS_VOLATILE (function));
1505 fundecl = function;
1506 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1508 else
1509 function = default_conversion (function);
1511 fntype = TREE_TYPE (function);
1513 if (TREE_CODE (fntype) == ERROR_MARK)
1514 return error_mark_node;
1516 if (!(TREE_CODE (fntype) == POINTER_TYPE
1517 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1519 error ("called object is not a function");
1520 return error_mark_node;
1523 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1524 current_function_returns_abnormally = 1;
1526 /* fntype now gets the type of function pointed to. */
1527 fntype = TREE_TYPE (fntype);
1529 /* Convert the parameters to the types declared in the
1530 function prototype, or apply default promotions. */
1532 coerced_params
1533 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1535 /* Check that the arguments to the function are valid. */
1537 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1539 /* Recognize certain built-in functions so we can make tree-codes
1540 other than CALL_EXPR. We do this when it enables fold-const.c
1541 to do something useful. */
1543 if (TREE_CODE (function) == ADDR_EXPR
1544 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1545 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1547 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1548 params, coerced_params);
1549 if (result)
1550 return result;
1553 result = build (CALL_EXPR, TREE_TYPE (fntype),
1554 function, coerced_params, NULL_TREE);
1555 TREE_SIDE_EFFECTS (result) = 1;
1556 result = fold (result);
1558 if (VOID_TYPE_P (TREE_TYPE (result)))
1559 return result;
1560 return require_complete_type (result);
1563 /* Convert the argument expressions in the list VALUES
1564 to the types in the list TYPELIST. The result is a list of converted
1565 argument expressions.
1567 If TYPELIST is exhausted, or when an element has NULL as its type,
1568 perform the default conversions.
1570 PARMLIST is the chain of parm decls for the function being called.
1571 It may be 0, if that info is not available.
1572 It is used only for generating error messages.
1574 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1576 This is also where warnings about wrong number of args are generated.
1578 Both VALUES and the returned value are chains of TREE_LIST nodes
1579 with the elements of the list in the TREE_VALUE slots of those nodes. */
1581 static tree
1582 convert_arguments (typelist, values, name, fundecl)
1583 tree typelist, values, name, fundecl;
1585 tree typetail, valtail;
1586 tree result = NULL;
1587 int parmnum;
1589 /* Scan the given expressions and types, producing individual
1590 converted arguments and pushing them on RESULT in reverse order. */
1592 for (valtail = values, typetail = typelist, parmnum = 0;
1593 valtail;
1594 valtail = TREE_CHAIN (valtail), parmnum++)
1596 tree type = typetail ? TREE_VALUE (typetail) : 0;
1597 tree val = TREE_VALUE (valtail);
1599 if (type == void_type_node)
1601 if (name)
1602 error ("too many arguments to function `%s'",
1603 IDENTIFIER_POINTER (name));
1604 else
1605 error ("too many arguments to function");
1606 break;
1609 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1610 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1611 to convert automatically to a pointer. */
1612 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1613 val = TREE_OPERAND (val, 0);
1615 val = default_function_array_conversion (val);
1617 val = require_complete_type (val);
1619 if (type != 0)
1621 /* Formal parm type is specified by a function prototype. */
1622 tree parmval;
1624 if (!COMPLETE_TYPE_P (type))
1626 error ("type of formal parameter %d is incomplete", parmnum + 1);
1627 parmval = val;
1629 else
1631 /* Optionally warn about conversions that
1632 differ from the default conversions. */
1633 if (warn_conversion || warn_traditional)
1635 int formal_prec = TYPE_PRECISION (type);
1637 if (INTEGRAL_TYPE_P (type)
1638 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1639 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1640 if (INTEGRAL_TYPE_P (type)
1641 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1642 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1643 else if (TREE_CODE (type) == COMPLEX_TYPE
1644 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1645 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1646 else if (TREE_CODE (type) == REAL_TYPE
1647 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1648 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1649 else if (TREE_CODE (type) == COMPLEX_TYPE
1650 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1651 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1652 else if (TREE_CODE (type) == REAL_TYPE
1653 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1654 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1655 /* ??? At some point, messages should be written about
1656 conversions between complex types, but that's too messy
1657 to do now. */
1658 else if (TREE_CODE (type) == REAL_TYPE
1659 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1661 /* Warn if any argument is passed as `float',
1662 since without a prototype it would be `double'. */
1663 if (formal_prec == TYPE_PRECISION (float_type_node))
1664 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1666 /* Detect integer changing in width or signedness.
1667 These warnings are only activated with
1668 -Wconversion, not with -Wtraditional. */
1669 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1670 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1672 tree would_have_been = default_conversion (val);
1673 tree type1 = TREE_TYPE (would_have_been);
1675 if (TREE_CODE (type) == ENUMERAL_TYPE
1676 && (TYPE_MAIN_VARIANT (type)
1677 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1678 /* No warning if function asks for enum
1679 and the actual arg is that enum type. */
1681 else if (formal_prec != TYPE_PRECISION (type1))
1682 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1683 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1685 /* Don't complain if the formal parameter type
1686 is an enum, because we can't tell now whether
1687 the value was an enum--even the same enum. */
1688 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1690 else if (TREE_CODE (val) == INTEGER_CST
1691 && int_fits_type_p (val, type))
1692 /* Change in signedness doesn't matter
1693 if a constant value is unaffected. */
1695 /* Likewise for a constant in a NOP_EXPR. */
1696 else if (TREE_CODE (val) == NOP_EXPR
1697 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1698 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1700 #if 0 /* We never get such tree structure here. */
1701 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1702 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1703 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1704 /* Change in signedness doesn't matter
1705 if an enum value is unaffected. */
1707 #endif
1708 /* If the value is extended from a narrower
1709 unsigned type, it doesn't matter whether we
1710 pass it as signed or unsigned; the value
1711 certainly is the same either way. */
1712 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1713 && TREE_UNSIGNED (TREE_TYPE (val)))
1715 else if (TREE_UNSIGNED (type))
1716 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1717 else
1718 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1722 parmval = convert_for_assignment (type, val,
1723 (char *) 0, /* arg passing */
1724 fundecl, name, parmnum + 1);
1726 if (PROMOTE_PROTOTYPES
1727 && INTEGRAL_TYPE_P (type)
1728 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1729 parmval = default_conversion (parmval);
1731 result = tree_cons (NULL_TREE, parmval, result);
1733 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1734 && (TYPE_PRECISION (TREE_TYPE (val))
1735 < TYPE_PRECISION (double_type_node)))
1736 /* Convert `float' to `double'. */
1737 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1738 else
1739 /* Convert `short' and `char' to full-size `int'. */
1740 result = tree_cons (NULL_TREE, default_conversion (val), result);
1742 if (typetail)
1743 typetail = TREE_CHAIN (typetail);
1746 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1748 if (name)
1749 error ("too few arguments to function `%s'",
1750 IDENTIFIER_POINTER (name));
1751 else
1752 error ("too few arguments to function");
1755 return nreverse (result);
1758 /* This is the entry point used by the parser
1759 for binary operators in the input.
1760 In addition to constructing the expression,
1761 we check for operands that were written with other binary operators
1762 in a way that is likely to confuse the user. */
1764 tree
1765 parser_build_binary_op (code, arg1, arg2)
1766 enum tree_code code;
1767 tree arg1, arg2;
1769 tree result = build_binary_op (code, arg1, arg2, 1);
1771 char class;
1772 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1773 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1774 enum tree_code code1 = ERROR_MARK;
1775 enum tree_code code2 = ERROR_MARK;
1777 if (TREE_CODE (result) == ERROR_MARK)
1778 return error_mark_node;
1780 if (IS_EXPR_CODE_CLASS (class1))
1781 code1 = C_EXP_ORIGINAL_CODE (arg1);
1782 if (IS_EXPR_CODE_CLASS (class2))
1783 code2 = C_EXP_ORIGINAL_CODE (arg2);
1785 /* Check for cases such as x+y<<z which users are likely
1786 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1787 is cleared to prevent these warnings. */
1788 if (warn_parentheses)
1790 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1792 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1793 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1794 warning ("suggest parentheses around + or - inside shift");
1797 if (code == TRUTH_ORIF_EXPR)
1799 if (code1 == TRUTH_ANDIF_EXPR
1800 || code2 == TRUTH_ANDIF_EXPR)
1801 warning ("suggest parentheses around && within ||");
1804 if (code == BIT_IOR_EXPR)
1806 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1807 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1808 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1809 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1810 warning ("suggest parentheses around arithmetic in operand of |");
1811 /* Check cases like x|y==z */
1812 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1813 warning ("suggest parentheses around comparison in operand of |");
1816 if (code == BIT_XOR_EXPR)
1818 if (code1 == BIT_AND_EXPR
1819 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1820 || code2 == BIT_AND_EXPR
1821 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1822 warning ("suggest parentheses around arithmetic in operand of ^");
1823 /* Check cases like x^y==z */
1824 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1825 warning ("suggest parentheses around comparison in operand of ^");
1828 if (code == BIT_AND_EXPR)
1830 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1831 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1832 warning ("suggest parentheses around + or - in operand of &");
1833 /* Check cases like x&y==z */
1834 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1835 warning ("suggest parentheses around comparison in operand of &");
1839 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1840 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1841 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1842 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1844 unsigned_conversion_warning (result, arg1);
1845 unsigned_conversion_warning (result, arg2);
1846 overflow_warning (result);
1848 class = TREE_CODE_CLASS (TREE_CODE (result));
1850 /* Record the code that was specified in the source,
1851 for the sake of warnings about confusing nesting. */
1852 if (IS_EXPR_CODE_CLASS (class))
1853 C_SET_EXP_ORIGINAL_CODE (result, code);
1854 else
1856 int flag = TREE_CONSTANT (result);
1857 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1858 so that convert_for_assignment wouldn't strip it.
1859 That way, we got warnings for things like p = (1 - 1).
1860 But it turns out we should not get those warnings. */
1861 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1862 C_SET_EXP_ORIGINAL_CODE (result, code);
1863 TREE_CONSTANT (result) = flag;
1866 return result;
1869 /* Build a binary-operation expression without default conversions.
1870 CODE is the kind of expression to build.
1871 This function differs from `build' in several ways:
1872 the data type of the result is computed and recorded in it,
1873 warnings are generated if arg data types are invalid,
1874 special handling for addition and subtraction of pointers is known,
1875 and some optimization is done (operations on narrow ints
1876 are done in the narrower type when that gives the same result).
1877 Constant folding is also done before the result is returned.
1879 Note that the operands will never have enumeral types, or function
1880 or array types, because either they will have the default conversions
1881 performed or they have both just been converted to some other type in which
1882 the arithmetic is to be done. */
1884 tree
1885 build_binary_op (code, orig_op0, orig_op1, convert_p)
1886 enum tree_code code;
1887 tree orig_op0, orig_op1;
1888 int convert_p;
1890 tree type0, type1;
1891 enum tree_code code0, code1;
1892 tree op0, op1;
1894 /* Expression code to give to the expression when it is built.
1895 Normally this is CODE, which is what the caller asked for,
1896 but in some special cases we change it. */
1897 enum tree_code resultcode = code;
1899 /* Data type in which the computation is to be performed.
1900 In the simplest cases this is the common type of the arguments. */
1901 tree result_type = NULL;
1903 /* Nonzero means operands have already been type-converted
1904 in whatever way is necessary.
1905 Zero means they need to be converted to RESULT_TYPE. */
1906 int converted = 0;
1908 /* Nonzero means create the expression with this type, rather than
1909 RESULT_TYPE. */
1910 tree build_type = 0;
1912 /* Nonzero means after finally constructing the expression
1913 convert it to this type. */
1914 tree final_type = 0;
1916 /* Nonzero if this is an operation like MIN or MAX which can
1917 safely be computed in short if both args are promoted shorts.
1918 Also implies COMMON.
1919 -1 indicates a bitwise operation; this makes a difference
1920 in the exact conditions for when it is safe to do the operation
1921 in a narrower mode. */
1922 int shorten = 0;
1924 /* Nonzero if this is a comparison operation;
1925 if both args are promoted shorts, compare the original shorts.
1926 Also implies COMMON. */
1927 int short_compare = 0;
1929 /* Nonzero if this is a right-shift operation, which can be computed on the
1930 original short and then promoted if the operand is a promoted short. */
1931 int short_shift = 0;
1933 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1934 int common = 0;
1936 if (convert_p)
1938 op0 = default_conversion (orig_op0);
1939 op1 = default_conversion (orig_op1);
1941 else
1943 op0 = orig_op0;
1944 op1 = orig_op1;
1947 type0 = TREE_TYPE (op0);
1948 type1 = TREE_TYPE (op1);
1950 /* The expression codes of the data types of the arguments tell us
1951 whether the arguments are integers, floating, pointers, etc. */
1952 code0 = TREE_CODE (type0);
1953 code1 = TREE_CODE (type1);
1955 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1956 STRIP_TYPE_NOPS (op0);
1957 STRIP_TYPE_NOPS (op1);
1959 /* If an error was already reported for one of the arguments,
1960 avoid reporting another error. */
1962 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1963 return error_mark_node;
1965 switch (code)
1967 case PLUS_EXPR:
1968 /* Handle the pointer + int case. */
1969 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1970 return pointer_int_sum (PLUS_EXPR, op0, op1);
1971 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1972 return pointer_int_sum (PLUS_EXPR, op1, op0);
1973 else
1974 common = 1;
1975 break;
1977 case MINUS_EXPR:
1978 /* Subtraction of two similar pointers.
1979 We must subtract them as integers, then divide by object size. */
1980 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1981 && comp_target_types (type0, type1, 1))
1982 return pointer_diff (op0, op1);
1983 /* Handle pointer minus int. Just like pointer plus int. */
1984 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1985 return pointer_int_sum (MINUS_EXPR, op0, op1);
1986 else
1987 common = 1;
1988 break;
1990 case MULT_EXPR:
1991 common = 1;
1992 break;
1994 case TRUNC_DIV_EXPR:
1995 case CEIL_DIV_EXPR:
1996 case FLOOR_DIV_EXPR:
1997 case ROUND_DIV_EXPR:
1998 case EXACT_DIV_EXPR:
1999 /* Floating point division by zero is a legitimate way to obtain
2000 infinities and NaNs. */
2001 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2002 warning ("division by zero");
2004 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2005 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
2006 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2007 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
2009 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2010 resultcode = RDIV_EXPR;
2011 else
2012 /* Although it would be tempting to shorten always here, that
2013 loses on some targets, since the modulo instruction is
2014 undefined if the quotient can't be represented in the
2015 computation mode. We shorten only if unsigned or if
2016 dividing by something we know != -1. */
2017 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2018 || (TREE_CODE (op1) == INTEGER_CST
2019 && ! integer_all_onesp (op1)));
2020 common = 1;
2022 break;
2024 case BIT_AND_EXPR:
2025 case BIT_ANDTC_EXPR:
2026 case BIT_IOR_EXPR:
2027 case BIT_XOR_EXPR:
2028 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2029 shorten = -1;
2030 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
2031 common = 1;
2032 break;
2034 case TRUNC_MOD_EXPR:
2035 case FLOOR_MOD_EXPR:
2036 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2037 warning ("division by zero");
2039 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2041 /* Although it would be tempting to shorten always here, that loses
2042 on some targets, since the modulo instruction is undefined if the
2043 quotient can't be represented in the computation mode. We shorten
2044 only if unsigned or if dividing by something we know != -1. */
2045 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2046 || (TREE_CODE (op1) == INTEGER_CST
2047 && ! integer_all_onesp (op1)));
2048 common = 1;
2050 break;
2052 case TRUTH_ANDIF_EXPR:
2053 case TRUTH_ORIF_EXPR:
2054 case TRUTH_AND_EXPR:
2055 case TRUTH_OR_EXPR:
2056 case TRUTH_XOR_EXPR:
2057 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2058 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2059 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2060 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2062 /* Result of these operations is always an int,
2063 but that does not mean the operands should be
2064 converted to ints! */
2065 result_type = integer_type_node;
2066 op0 = c_common_truthvalue_conversion (op0);
2067 op1 = c_common_truthvalue_conversion (op1);
2068 converted = 1;
2070 break;
2072 /* Shift operations: result has same type as first operand;
2073 always convert second operand to int.
2074 Also set SHORT_SHIFT if shifting rightward. */
2076 case RSHIFT_EXPR:
2077 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2079 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2081 if (tree_int_cst_sgn (op1) < 0)
2082 warning ("right shift count is negative");
2083 else
2085 if (! integer_zerop (op1))
2086 short_shift = 1;
2088 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2089 warning ("right shift count >= width of type");
2093 /* Use the type of the value to be shifted. */
2094 result_type = type0;
2095 /* Convert the shift-count to an integer, regardless of size
2096 of value being shifted. */
2097 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2098 op1 = convert (integer_type_node, op1);
2099 /* Avoid converting op1 to result_type later. */
2100 converted = 1;
2102 break;
2104 case LSHIFT_EXPR:
2105 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2107 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2109 if (tree_int_cst_sgn (op1) < 0)
2110 warning ("left shift count is negative");
2112 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2113 warning ("left shift count >= width of type");
2116 /* Use the type of the value to be shifted. */
2117 result_type = type0;
2118 /* Convert the shift-count to an integer, regardless of size
2119 of value being shifted. */
2120 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2121 op1 = convert (integer_type_node, op1);
2122 /* Avoid converting op1 to result_type later. */
2123 converted = 1;
2125 break;
2127 case RROTATE_EXPR:
2128 case LROTATE_EXPR:
2129 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2131 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2133 if (tree_int_cst_sgn (op1) < 0)
2134 warning ("shift count is negative");
2135 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2136 warning ("shift count >= width of type");
2139 /* Use the type of the value to be shifted. */
2140 result_type = type0;
2141 /* Convert the shift-count to an integer, regardless of size
2142 of value being shifted. */
2143 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2144 op1 = convert (integer_type_node, op1);
2145 /* Avoid converting op1 to result_type later. */
2146 converted = 1;
2148 break;
2150 case EQ_EXPR:
2151 case NE_EXPR:
2152 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2153 warning ("comparing floating point with == or != is unsafe");
2154 /* Result of comparison is always int,
2155 but don't convert the args to int! */
2156 build_type = integer_type_node;
2157 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2158 || code0 == COMPLEX_TYPE
2159 || code0 == VECTOR_TYPE)
2160 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2161 || code1 == COMPLEX_TYPE
2162 || code1 == VECTOR_TYPE))
2163 short_compare = 1;
2164 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2166 tree tt0 = TREE_TYPE (type0);
2167 tree tt1 = TREE_TYPE (type1);
2168 /* Anything compares with void *. void * compares with anything.
2169 Otherwise, the targets must be compatible
2170 and both must be object or both incomplete. */
2171 if (comp_target_types (type0, type1, 1))
2172 result_type = common_type (type0, type1);
2173 else if (VOID_TYPE_P (tt0))
2175 /* op0 != orig_op0 detects the case of something
2176 whose value is 0 but which isn't a valid null ptr const. */
2177 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2178 && TREE_CODE (tt1) == FUNCTION_TYPE)
2179 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2181 else if (VOID_TYPE_P (tt1))
2183 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2184 && TREE_CODE (tt0) == FUNCTION_TYPE)
2185 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2187 else
2188 pedwarn ("comparison of distinct pointer types lacks a cast");
2190 if (result_type == NULL_TREE)
2191 result_type = ptr_type_node;
2193 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2194 && integer_zerop (op1))
2195 result_type = type0;
2196 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2197 && integer_zerop (op0))
2198 result_type = type1;
2199 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2201 result_type = type0;
2202 pedwarn ("comparison between pointer and integer");
2204 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2206 result_type = type1;
2207 pedwarn ("comparison between pointer and integer");
2209 break;
2211 case MAX_EXPR:
2212 case MIN_EXPR:
2213 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2214 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2215 shorten = 1;
2216 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2218 if (comp_target_types (type0, type1, 1))
2220 result_type = common_type (type0, type1);
2221 if (pedantic
2222 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2223 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2225 else
2227 result_type = ptr_type_node;
2228 pedwarn ("comparison of distinct pointer types lacks a cast");
2231 break;
2233 case LE_EXPR:
2234 case GE_EXPR:
2235 case LT_EXPR:
2236 case GT_EXPR:
2237 build_type = integer_type_node;
2238 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2239 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2240 short_compare = 1;
2241 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2243 if (comp_target_types (type0, type1, 1))
2245 result_type = common_type (type0, type1);
2246 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
2247 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
2248 pedwarn ("comparison of complete and incomplete pointers");
2249 else if (pedantic
2250 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2251 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2253 else
2255 result_type = ptr_type_node;
2256 pedwarn ("comparison of distinct pointer types lacks a cast");
2259 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2260 && integer_zerop (op1))
2262 result_type = type0;
2263 if (pedantic || extra_warnings)
2264 pedwarn ("ordered comparison of pointer with integer zero");
2266 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2267 && integer_zerop (op0))
2269 result_type = type1;
2270 if (pedantic)
2271 pedwarn ("ordered comparison of pointer with integer zero");
2273 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2275 result_type = type0;
2276 pedwarn ("comparison between pointer and integer");
2278 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2280 result_type = type1;
2281 pedwarn ("comparison between pointer and integer");
2283 break;
2285 case UNORDERED_EXPR:
2286 case ORDERED_EXPR:
2287 case UNLT_EXPR:
2288 case UNLE_EXPR:
2289 case UNGT_EXPR:
2290 case UNGE_EXPR:
2291 case UNEQ_EXPR:
2292 build_type = integer_type_node;
2293 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2295 error ("unordered comparison on non-floating point argument");
2296 return error_mark_node;
2298 common = 1;
2299 break;
2301 default:
2302 break;
2305 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
2306 || code0 == VECTOR_TYPE)
2308 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
2309 || code1 == VECTOR_TYPE))
2311 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2313 if (shorten || common || short_compare)
2314 result_type = common_type (type0, type1);
2316 /* For certain operations (which identify themselves by shorten != 0)
2317 if both args were extended from the same smaller type,
2318 do the arithmetic in that type and then extend.
2320 shorten !=0 and !=1 indicates a bitwise operation.
2321 For them, this optimization is safe only if
2322 both args are zero-extended or both are sign-extended.
2323 Otherwise, we might change the result.
2324 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2325 but calculated in (unsigned short) it would be (unsigned short)-1. */
2327 if (shorten && none_complex)
2329 int unsigned0, unsigned1;
2330 tree arg0 = get_narrower (op0, &unsigned0);
2331 tree arg1 = get_narrower (op1, &unsigned1);
2332 /* UNS is 1 if the operation to be done is an unsigned one. */
2333 int uns = TREE_UNSIGNED (result_type);
2334 tree type;
2336 final_type = result_type;
2338 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2339 but it *requires* conversion to FINAL_TYPE. */
2341 if ((TYPE_PRECISION (TREE_TYPE (op0))
2342 == TYPE_PRECISION (TREE_TYPE (arg0)))
2343 && TREE_TYPE (op0) != final_type)
2344 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2345 if ((TYPE_PRECISION (TREE_TYPE (op1))
2346 == TYPE_PRECISION (TREE_TYPE (arg1)))
2347 && TREE_TYPE (op1) != final_type)
2348 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2350 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2352 /* For bitwise operations, signedness of nominal type
2353 does not matter. Consider only how operands were extended. */
2354 if (shorten == -1)
2355 uns = unsigned0;
2357 /* Note that in all three cases below we refrain from optimizing
2358 an unsigned operation on sign-extended args.
2359 That would not be valid. */
2361 /* Both args variable: if both extended in same way
2362 from same width, do it in that width.
2363 Do it unsigned if args were zero-extended. */
2364 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2365 < TYPE_PRECISION (result_type))
2366 && (TYPE_PRECISION (TREE_TYPE (arg1))
2367 == TYPE_PRECISION (TREE_TYPE (arg0)))
2368 && unsigned0 == unsigned1
2369 && (unsigned0 || !uns))
2370 result_type
2371 = c_common_signed_or_unsigned_type
2372 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2373 else if (TREE_CODE (arg0) == INTEGER_CST
2374 && (unsigned1 || !uns)
2375 && (TYPE_PRECISION (TREE_TYPE (arg1))
2376 < TYPE_PRECISION (result_type))
2377 && (type
2378 = c_common_signed_or_unsigned_type (unsigned1,
2379 TREE_TYPE (arg1)),
2380 int_fits_type_p (arg0, type)))
2381 result_type = type;
2382 else if (TREE_CODE (arg1) == INTEGER_CST
2383 && (unsigned0 || !uns)
2384 && (TYPE_PRECISION (TREE_TYPE (arg0))
2385 < TYPE_PRECISION (result_type))
2386 && (type
2387 = c_common_signed_or_unsigned_type (unsigned0,
2388 TREE_TYPE (arg0)),
2389 int_fits_type_p (arg1, type)))
2390 result_type = type;
2393 /* Shifts can be shortened if shifting right. */
2395 if (short_shift)
2397 int unsigned_arg;
2398 tree arg0 = get_narrower (op0, &unsigned_arg);
2400 final_type = result_type;
2402 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2403 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2405 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2406 /* We can shorten only if the shift count is less than the
2407 number of bits in the smaller type size. */
2408 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
2409 /* We cannot drop an unsigned shift after sign-extension. */
2410 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
2412 /* Do an unsigned shift if the operand was zero-extended. */
2413 result_type
2414 = c_common_signed_or_unsigned_type (unsigned_arg,
2415 TREE_TYPE (arg0));
2416 /* Convert value-to-be-shifted to that type. */
2417 if (TREE_TYPE (op0) != result_type)
2418 op0 = convert (result_type, op0);
2419 converted = 1;
2423 /* Comparison operations are shortened too but differently.
2424 They identify themselves by setting short_compare = 1. */
2426 if (short_compare)
2428 /* Don't write &op0, etc., because that would prevent op0
2429 from being kept in a register.
2430 Instead, make copies of the our local variables and
2431 pass the copies by reference, then copy them back afterward. */
2432 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2433 enum tree_code xresultcode = resultcode;
2434 tree val
2435 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2437 if (val != 0)
2438 return val;
2440 op0 = xop0, op1 = xop1;
2441 converted = 1;
2442 resultcode = xresultcode;
2444 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2445 && skip_evaluation == 0)
2447 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2448 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2449 int unsignedp0, unsignedp1;
2450 tree primop0 = get_narrower (op0, &unsignedp0);
2451 tree primop1 = get_narrower (op1, &unsignedp1);
2453 xop0 = orig_op0;
2454 xop1 = orig_op1;
2455 STRIP_TYPE_NOPS (xop0);
2456 STRIP_TYPE_NOPS (xop1);
2458 /* Give warnings for comparisons between signed and unsigned
2459 quantities that may fail.
2461 Do the checking based on the original operand trees, so that
2462 casts will be considered, but default promotions won't be.
2464 Do not warn if the comparison is being done in a signed type,
2465 since the signed type will only be chosen if it can represent
2466 all the values of the unsigned type. */
2467 if (! TREE_UNSIGNED (result_type))
2468 /* OK */;
2469 /* Do not warn if both operands are the same signedness. */
2470 else if (op0_signed == op1_signed)
2471 /* OK */;
2472 else
2474 tree sop, uop;
2476 if (op0_signed)
2477 sop = xop0, uop = xop1;
2478 else
2479 sop = xop1, uop = xop0;
2481 /* Do not warn if the signed quantity is an
2482 unsuffixed integer literal (or some static
2483 constant expression involving such literals or a
2484 conditional expression involving such literals)
2485 and it is non-negative. */
2486 if (c_tree_expr_nonnegative_p (sop))
2487 /* OK */;
2488 /* Do not warn if the comparison is an equality operation,
2489 the unsigned quantity is an integral constant, and it
2490 would fit in the result if the result were signed. */
2491 else if (TREE_CODE (uop) == INTEGER_CST
2492 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2493 && int_fits_type_p
2494 (uop, c_common_signed_type (result_type)))
2495 /* OK */;
2496 /* Do not warn if the unsigned quantity is an enumeration
2497 constant and its maximum value would fit in the result
2498 if the result were signed. */
2499 else if (TREE_CODE (uop) == INTEGER_CST
2500 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2501 && int_fits_type_p
2502 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2503 c_common_signed_type (result_type)))
2504 /* OK */;
2505 else
2506 warning ("comparison between signed and unsigned");
2509 /* Warn if two unsigned values are being compared in a size
2510 larger than their original size, and one (and only one) is the
2511 result of a `~' operator. This comparison will always fail.
2513 Also warn if one operand is a constant, and the constant
2514 does not have all bits set that are set in the ~ operand
2515 when it is extended. */
2517 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2518 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2520 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2521 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2522 &unsignedp0);
2523 else
2524 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2525 &unsignedp1);
2527 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
2529 tree primop;
2530 HOST_WIDE_INT constant, mask;
2531 int unsignedp, bits;
2533 if (host_integerp (primop0, 0))
2535 primop = primop1;
2536 unsignedp = unsignedp1;
2537 constant = tree_low_cst (primop0, 0);
2539 else
2541 primop = primop0;
2542 unsignedp = unsignedp0;
2543 constant = tree_low_cst (primop1, 0);
2546 bits = TYPE_PRECISION (TREE_TYPE (primop));
2547 if (bits < TYPE_PRECISION (result_type)
2548 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
2550 mask = (~ (HOST_WIDE_INT) 0) << bits;
2551 if ((mask & constant) != mask)
2552 warning ("comparison of promoted ~unsigned with constant");
2555 else if (unsignedp0 && unsignedp1
2556 && (TYPE_PRECISION (TREE_TYPE (primop0))
2557 < TYPE_PRECISION (result_type))
2558 && (TYPE_PRECISION (TREE_TYPE (primop1))
2559 < TYPE_PRECISION (result_type)))
2560 warning ("comparison of promoted ~unsigned with unsigned");
2566 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2567 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2568 Then the expression will be built.
2569 It will be given type FINAL_TYPE if that is nonzero;
2570 otherwise, it will be given type RESULT_TYPE. */
2572 if (!result_type)
2574 binary_op_error (code);
2575 return error_mark_node;
2578 if (! converted)
2580 if (TREE_TYPE (op0) != result_type)
2581 op0 = convert (result_type, op0);
2582 if (TREE_TYPE (op1) != result_type)
2583 op1 = convert (result_type, op1);
2586 if (build_type == NULL_TREE)
2587 build_type = result_type;
2590 tree result = build (resultcode, build_type, op0, op1);
2591 tree folded;
2593 folded = fold (result);
2594 if (folded == result)
2595 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2596 if (final_type != 0)
2597 return convert (final_type, folded);
2598 return folded;
2603 /* Return true if `t' is known to be non-negative. */
2606 c_tree_expr_nonnegative_p (t)
2607 tree t;
2609 if (TREE_CODE (t) == STMT_EXPR)
2611 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2613 /* Find the last statement in the chain, ignoring the final
2614 * scope statement */
2615 while (TREE_CHAIN (t) != NULL_TREE
2616 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2617 t = TREE_CHAIN (t);
2618 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2620 return tree_expr_nonnegative_p (t);
2623 /* Return a tree for the difference of pointers OP0 and OP1.
2624 The resulting tree has type int. */
2626 static tree
2627 pointer_diff (op0, op1)
2628 tree op0, op1;
2630 tree result, folded;
2631 tree restype = ptrdiff_type_node;
2633 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2634 tree con0, con1, lit0, lit1;
2635 tree orig_op1 = op1;
2637 if (pedantic || warn_pointer_arith)
2639 if (TREE_CODE (target_type) == VOID_TYPE)
2640 pedwarn ("pointer of type `void *' used in subtraction");
2641 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2642 pedwarn ("pointer to a function used in subtraction");
2645 /* If the conversion to ptrdiff_type does anything like widening or
2646 converting a partial to an integral mode, we get a convert_expression
2647 that is in the way to do any simplifications.
2648 (fold-const.c doesn't know that the extra bits won't be needed.
2649 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2650 different mode in place.)
2651 So first try to find a common term here 'by hand'; we want to cover
2652 at least the cases that occur in legal static initializers. */
2653 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2654 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2656 if (TREE_CODE (con0) == PLUS_EXPR)
2658 lit0 = TREE_OPERAND (con0, 1);
2659 con0 = TREE_OPERAND (con0, 0);
2661 else
2662 lit0 = integer_zero_node;
2664 if (TREE_CODE (con1) == PLUS_EXPR)
2666 lit1 = TREE_OPERAND (con1, 1);
2667 con1 = TREE_OPERAND (con1, 0);
2669 else
2670 lit1 = integer_zero_node;
2672 if (operand_equal_p (con0, con1, 0))
2674 op0 = lit0;
2675 op1 = lit1;
2679 /* First do the subtraction as integers;
2680 then drop through to build the divide operator.
2681 Do not do default conversions on the minus operator
2682 in case restype is a short type. */
2684 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2685 convert (restype, op1), 0);
2686 /* This generates an error if op1 is pointer to incomplete type. */
2687 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2688 error ("arithmetic on pointer to an incomplete type");
2690 /* This generates an error if op0 is pointer to incomplete type. */
2691 op1 = c_size_in_bytes (target_type);
2693 /* Divide by the size, in easiest possible way. */
2695 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2697 folded = fold (result);
2698 if (folded == result)
2699 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2700 return folded;
2703 /* Construct and perhaps optimize a tree representation
2704 for a unary operation. CODE, a tree_code, specifies the operation
2705 and XARG is the operand.
2706 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2707 the default promotions (such as from short to int).
2708 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2709 allows non-lvalues; this is only used to handle conversion of non-lvalue
2710 arrays to pointers in C99. */
2712 tree
2713 build_unary_op (code, xarg, flag)
2714 enum tree_code code;
2715 tree xarg;
2716 int flag;
2718 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2719 tree arg = xarg;
2720 tree argtype = 0;
2721 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2722 tree val;
2723 int noconvert = flag;
2725 if (typecode == ERROR_MARK)
2726 return error_mark_node;
2727 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2728 typecode = INTEGER_TYPE;
2730 switch (code)
2732 case CONVERT_EXPR:
2733 /* This is used for unary plus, because a CONVERT_EXPR
2734 is enough to prevent anybody from looking inside for
2735 associativity, but won't generate any code. */
2736 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2737 || typecode == COMPLEX_TYPE))
2739 error ("wrong type argument to unary plus");
2740 return error_mark_node;
2742 else if (!noconvert)
2743 arg = default_conversion (arg);
2744 arg = non_lvalue (arg);
2745 break;
2747 case NEGATE_EXPR:
2748 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2749 || typecode == COMPLEX_TYPE
2750 || typecode == VECTOR_TYPE))
2752 error ("wrong type argument to unary minus");
2753 return error_mark_node;
2755 else if (!noconvert)
2756 arg = default_conversion (arg);
2757 break;
2759 case BIT_NOT_EXPR:
2760 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2762 if (!noconvert)
2763 arg = default_conversion (arg);
2765 else if (typecode == COMPLEX_TYPE)
2767 code = CONJ_EXPR;
2768 if (pedantic)
2769 pedwarn ("ISO C does not support `~' for complex conjugation");
2770 if (!noconvert)
2771 arg = default_conversion (arg);
2773 else
2775 error ("wrong type argument to bit-complement");
2776 return error_mark_node;
2778 break;
2780 case ABS_EXPR:
2781 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2782 || typecode == COMPLEX_TYPE))
2784 error ("wrong type argument to abs");
2785 return error_mark_node;
2787 else if (!noconvert)
2788 arg = default_conversion (arg);
2789 break;
2791 case CONJ_EXPR:
2792 /* Conjugating a real value is a no-op, but allow it anyway. */
2793 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2794 || typecode == COMPLEX_TYPE))
2796 error ("wrong type argument to conjugation");
2797 return error_mark_node;
2799 else if (!noconvert)
2800 arg = default_conversion (arg);
2801 break;
2803 case TRUTH_NOT_EXPR:
2804 if (typecode != INTEGER_TYPE
2805 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2806 && typecode != COMPLEX_TYPE
2807 /* These will convert to a pointer. */
2808 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2810 error ("wrong type argument to unary exclamation mark");
2811 return error_mark_node;
2813 arg = c_common_truthvalue_conversion (arg);
2814 return invert_truthvalue (arg);
2816 case NOP_EXPR:
2817 break;
2819 case REALPART_EXPR:
2820 if (TREE_CODE (arg) == COMPLEX_CST)
2821 return TREE_REALPART (arg);
2822 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2823 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2824 else
2825 return arg;
2827 case IMAGPART_EXPR:
2828 if (TREE_CODE (arg) == COMPLEX_CST)
2829 return TREE_IMAGPART (arg);
2830 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2831 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2832 else
2833 return convert (TREE_TYPE (arg), integer_zero_node);
2835 case PREINCREMENT_EXPR:
2836 case POSTINCREMENT_EXPR:
2837 case PREDECREMENT_EXPR:
2838 case POSTDECREMENT_EXPR:
2839 /* Handle complex lvalues (when permitted)
2840 by reduction to simpler cases. */
2842 val = unary_complex_lvalue (code, arg, 0);
2843 if (val != 0)
2844 return val;
2846 /* Increment or decrement the real part of the value,
2847 and don't change the imaginary part. */
2848 if (typecode == COMPLEX_TYPE)
2850 tree real, imag;
2852 if (pedantic)
2853 pedwarn ("ISO C does not support `++' and `--' on complex types");
2855 arg = stabilize_reference (arg);
2856 real = build_unary_op (REALPART_EXPR, arg, 1);
2857 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2858 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2859 build_unary_op (code, real, 1), imag);
2862 /* Report invalid types. */
2864 if (typecode != POINTER_TYPE
2865 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2867 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2868 error ("wrong type argument to increment");
2869 else
2870 error ("wrong type argument to decrement");
2872 return error_mark_node;
2876 tree inc;
2877 tree result_type = TREE_TYPE (arg);
2879 arg = get_unwidened (arg, 0);
2880 argtype = TREE_TYPE (arg);
2882 /* Compute the increment. */
2884 if (typecode == POINTER_TYPE)
2886 /* If pointer target is an undefined struct,
2887 we just cannot know how to do the arithmetic. */
2888 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2890 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2891 error ("increment of pointer to unknown structure");
2892 else
2893 error ("decrement of pointer to unknown structure");
2895 else if ((pedantic || warn_pointer_arith)
2896 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2897 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2899 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2900 pedwarn ("wrong type argument to increment");
2901 else
2902 pedwarn ("wrong type argument to decrement");
2905 inc = c_size_in_bytes (TREE_TYPE (result_type));
2907 else
2908 inc = integer_one_node;
2910 inc = convert (argtype, inc);
2912 /* Handle incrementing a cast-expression. */
2914 while (1)
2915 switch (TREE_CODE (arg))
2917 case NOP_EXPR:
2918 case CONVERT_EXPR:
2919 case FLOAT_EXPR:
2920 case FIX_TRUNC_EXPR:
2921 case FIX_FLOOR_EXPR:
2922 case FIX_ROUND_EXPR:
2923 case FIX_CEIL_EXPR:
2924 pedantic_lvalue_warning (CONVERT_EXPR);
2925 /* If the real type has the same machine representation
2926 as the type it is cast to, we can make better output
2927 by adding directly to the inside of the cast. */
2928 if ((TREE_CODE (TREE_TYPE (arg))
2929 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2930 && (TYPE_MODE (TREE_TYPE (arg))
2931 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2932 arg = TREE_OPERAND (arg, 0);
2933 else
2935 tree incremented, modify, value;
2936 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2937 value = boolean_increment (code, arg);
2938 else
2940 arg = stabilize_reference (arg);
2941 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2942 value = arg;
2943 else
2944 value = save_expr (arg);
2945 incremented = build (((code == PREINCREMENT_EXPR
2946 || code == POSTINCREMENT_EXPR)
2947 ? PLUS_EXPR : MINUS_EXPR),
2948 argtype, value, inc);
2949 TREE_SIDE_EFFECTS (incremented) = 1;
2950 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2951 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2953 TREE_USED (value) = 1;
2954 return value;
2956 break;
2958 default:
2959 goto give_up;
2961 give_up:
2963 /* Complain about anything else that is not a true lvalue. */
2964 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2965 || code == POSTINCREMENT_EXPR)
2966 ? "invalid lvalue in increment"
2967 : "invalid lvalue in decrement")))
2968 return error_mark_node;
2970 /* Report a read-only lvalue. */
2971 if (TREE_READONLY (arg))
2972 readonly_warning (arg,
2973 ((code == PREINCREMENT_EXPR
2974 || code == POSTINCREMENT_EXPR)
2975 ? "increment" : "decrement"));
2977 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2978 val = boolean_increment (code, arg);
2979 else
2980 val = build (code, TREE_TYPE (arg), arg, inc);
2981 TREE_SIDE_EFFECTS (val) = 1;
2982 val = convert (result_type, val);
2983 if (TREE_CODE (val) != code)
2984 TREE_NO_UNUSED_WARNING (val) = 1;
2985 return val;
2988 case ADDR_EXPR:
2989 /* Note that this operation never does default_conversion. */
2991 /* Let &* cancel out to simplify resulting code. */
2992 if (TREE_CODE (arg) == INDIRECT_REF)
2994 /* Don't let this be an lvalue. */
2995 if (lvalue_p (TREE_OPERAND (arg, 0)))
2996 return non_lvalue (TREE_OPERAND (arg, 0));
2997 return TREE_OPERAND (arg, 0);
3000 /* For &x[y], return x+y */
3001 if (TREE_CODE (arg) == ARRAY_REF)
3003 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
3004 return error_mark_node;
3005 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3006 TREE_OPERAND (arg, 1), 1);
3009 /* Handle complex lvalues (when permitted)
3010 by reduction to simpler cases. */
3011 val = unary_complex_lvalue (code, arg, flag);
3012 if (val != 0)
3013 return val;
3015 #if 0 /* Turned off because inconsistent;
3016 float f; *&(int)f = 3.4 stores in int format
3017 whereas (int)f = 3.4 stores in float format. */
3018 /* Address of a cast is just a cast of the address
3019 of the operand of the cast. */
3020 switch (TREE_CODE (arg))
3022 case NOP_EXPR:
3023 case CONVERT_EXPR:
3024 case FLOAT_EXPR:
3025 case FIX_TRUNC_EXPR:
3026 case FIX_FLOOR_EXPR:
3027 case FIX_ROUND_EXPR:
3028 case FIX_CEIL_EXPR:
3029 if (pedantic)
3030 pedwarn ("ISO C forbids the address of a cast expression");
3031 return convert (build_pointer_type (TREE_TYPE (arg)),
3032 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3033 0));
3035 #endif
3037 /* Anything not already handled and not a true memory reference
3038 or a non-lvalue array is an error. */
3039 else if (typecode != FUNCTION_TYPE && !flag
3040 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
3041 return error_mark_node;
3043 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3044 argtype = TREE_TYPE (arg);
3046 /* If the lvalue is const or volatile, merge that into the type
3047 to which the address will point. Note that you can't get a
3048 restricted pointer by taking the address of something, so we
3049 only have to deal with `const' and `volatile' here. */
3050 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3051 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3052 argtype = c_build_type_variant (argtype,
3053 TREE_READONLY (arg),
3054 TREE_THIS_VOLATILE (arg));
3056 argtype = build_pointer_type (argtype);
3058 if (!c_mark_addressable (arg))
3059 return error_mark_node;
3062 tree addr;
3064 if (TREE_CODE (arg) == COMPONENT_REF)
3066 tree field = TREE_OPERAND (arg, 1);
3068 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
3070 if (DECL_C_BIT_FIELD (field))
3072 error ("attempt to take address of bit-field structure member `%s'",
3073 IDENTIFIER_POINTER (DECL_NAME (field)));
3074 return error_mark_node;
3077 addr = fold (build (PLUS_EXPR, argtype,
3078 convert (argtype, addr),
3079 convert (argtype, byte_position (field))));
3081 else
3082 addr = build1 (code, argtype, arg);
3084 /* Address of a static or external variable or
3085 file-scope function counts as a constant. */
3086 if (staticp (arg)
3087 && ! (TREE_CODE (arg) == FUNCTION_DECL
3088 && DECL_CONTEXT (arg) != 0))
3089 TREE_CONSTANT (addr) = 1;
3090 return addr;
3093 default:
3094 break;
3097 if (argtype == 0)
3098 argtype = TREE_TYPE (arg);
3099 return fold (build1 (code, argtype, arg));
3102 #if 0
3103 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3104 convert ARG with the same conversions in the same order
3105 and return the result. */
3107 static tree
3108 convert_sequence (conversions, arg)
3109 tree conversions;
3110 tree arg;
3112 switch (TREE_CODE (conversions))
3114 case NOP_EXPR:
3115 case CONVERT_EXPR:
3116 case FLOAT_EXPR:
3117 case FIX_TRUNC_EXPR:
3118 case FIX_FLOOR_EXPR:
3119 case FIX_ROUND_EXPR:
3120 case FIX_CEIL_EXPR:
3121 return convert (TREE_TYPE (conversions),
3122 convert_sequence (TREE_OPERAND (conversions, 0),
3123 arg));
3125 default:
3126 return arg;
3129 #endif /* 0 */
3131 /* Return nonzero if REF is an lvalue valid for this language.
3132 Lvalues can be assigned, unless their type has TYPE_READONLY.
3133 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3136 lvalue_p (ref)
3137 tree ref;
3139 enum tree_code code = TREE_CODE (ref);
3141 switch (code)
3143 case REALPART_EXPR:
3144 case IMAGPART_EXPR:
3145 case COMPONENT_REF:
3146 return lvalue_p (TREE_OPERAND (ref, 0));
3148 case COMPOUND_LITERAL_EXPR:
3149 case STRING_CST:
3150 return 1;
3152 case INDIRECT_REF:
3153 case ARRAY_REF:
3154 case VAR_DECL:
3155 case PARM_DECL:
3156 case RESULT_DECL:
3157 case ERROR_MARK:
3158 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3159 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3161 case BIND_EXPR:
3162 case RTL_EXPR:
3163 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3165 default:
3166 return 0;
3170 /* Return nonzero if REF is an lvalue valid for this language;
3171 otherwise, print an error message and return zero. */
3174 lvalue_or_else (ref, msgid)
3175 tree ref;
3176 const char *msgid;
3178 int win = lvalue_p (ref);
3180 if (! win)
3181 error ("%s", msgid);
3183 return win;
3186 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3187 for certain kinds of expressions which are not really lvalues
3188 but which we can accept as lvalues. If FLAG is nonzero, then
3189 non-lvalues are OK since we may be converting a non-lvalue array to
3190 a pointer in C99.
3192 If ARG is not a kind of expression we can handle, return zero. */
3194 static tree
3195 unary_complex_lvalue (code, arg, flag)
3196 enum tree_code code;
3197 tree arg;
3198 int flag;
3200 /* Handle (a, b) used as an "lvalue". */
3201 if (TREE_CODE (arg) == COMPOUND_EXPR)
3203 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3205 /* If this returns a function type, it isn't really being used as
3206 an lvalue, so don't issue a warning about it. */
3207 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3208 pedantic_lvalue_warning (COMPOUND_EXPR);
3210 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3211 TREE_OPERAND (arg, 0), real_result);
3214 /* Handle (a ? b : c) used as an "lvalue". */
3215 if (TREE_CODE (arg) == COND_EXPR)
3217 if (!flag)
3218 pedantic_lvalue_warning (COND_EXPR);
3219 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3220 pedantic_lvalue_warning (COMPOUND_EXPR);
3222 return (build_conditional_expr
3223 (TREE_OPERAND (arg, 0),
3224 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
3225 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
3228 return 0;
3231 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3232 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3234 static void
3235 pedantic_lvalue_warning (code)
3236 enum tree_code code;
3238 if (pedantic)
3239 switch (code)
3241 case COND_EXPR:
3242 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
3243 break;
3244 case COMPOUND_EXPR:
3245 pedwarn ("ISO C forbids use of compound expressions as lvalues");
3246 break;
3247 default:
3248 pedwarn ("ISO C forbids use of cast expressions as lvalues");
3249 break;
3253 /* Warn about storing in something that is `const'. */
3255 void
3256 readonly_warning (arg, msgid)
3257 tree arg;
3258 const char *msgid;
3260 if (TREE_CODE (arg) == COMPONENT_REF)
3262 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3263 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3264 else
3265 pedwarn ("%s of read-only member `%s'", _(msgid),
3266 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3268 else if (TREE_CODE (arg) == VAR_DECL)
3269 pedwarn ("%s of read-only variable `%s'", _(msgid),
3270 IDENTIFIER_POINTER (DECL_NAME (arg)));
3271 else
3272 pedwarn ("%s of read-only location", _(msgid));
3275 /* Mark EXP saying that we need to be able to take the
3276 address of it; it should not be allocated in a register.
3277 Returns true if successful. */
3279 bool
3280 c_mark_addressable (exp)
3281 tree exp;
3283 tree x = exp;
3285 while (1)
3286 switch (TREE_CODE (x))
3288 case COMPONENT_REF:
3289 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3291 error ("cannot take address of bit-field `%s'",
3292 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3293 return false;
3296 /* ... fall through ... */
3298 case ADDR_EXPR:
3299 case ARRAY_REF:
3300 case REALPART_EXPR:
3301 case IMAGPART_EXPR:
3302 x = TREE_OPERAND (x, 0);
3303 break;
3305 case COMPOUND_LITERAL_EXPR:
3306 case CONSTRUCTOR:
3307 TREE_ADDRESSABLE (x) = 1;
3308 return true;
3310 case VAR_DECL:
3311 case CONST_DECL:
3312 case PARM_DECL:
3313 case RESULT_DECL:
3314 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3315 && DECL_NONLOCAL (x))
3317 if (TREE_PUBLIC (x))
3319 error ("global register variable `%s' used in nested function",
3320 IDENTIFIER_POINTER (DECL_NAME (x)));
3321 return false;
3323 pedwarn ("register variable `%s' used in nested function",
3324 IDENTIFIER_POINTER (DECL_NAME (x)));
3326 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3328 if (TREE_PUBLIC (x))
3330 error ("address of global register variable `%s' requested",
3331 IDENTIFIER_POINTER (DECL_NAME (x)));
3332 return false;
3335 /* If we are making this addressable due to its having
3336 volatile components, give a different error message. Also
3337 handle the case of an unnamed parameter by not trying
3338 to give the name. */
3340 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3342 error ("cannot put object with volatile field into register");
3343 return false;
3346 pedwarn ("address of register variable `%s' requested",
3347 IDENTIFIER_POINTER (DECL_NAME (x)));
3349 put_var_into_stack (x, /*rescan=*/true);
3351 /* drops in */
3352 case FUNCTION_DECL:
3353 TREE_ADDRESSABLE (x) = 1;
3354 #if 0 /* poplevel deals with this now. */
3355 if (DECL_CONTEXT (x) == 0)
3356 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3357 #endif
3359 default:
3360 return true;
3364 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3366 tree
3367 build_conditional_expr (ifexp, op1, op2)
3368 tree ifexp, op1, op2;
3370 tree type1;
3371 tree type2;
3372 enum tree_code code1;
3373 enum tree_code code2;
3374 tree result_type = NULL;
3375 tree orig_op1 = op1, orig_op2 = op2;
3377 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
3379 #if 0 /* Produces wrong result if within sizeof. */
3380 /* Don't promote the operands separately if they promote
3381 the same way. Return the unpromoted type and let the combined
3382 value get promoted if necessary. */
3384 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3385 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3386 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3387 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3389 if (TREE_CODE (ifexp) == INTEGER_CST)
3390 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3392 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3394 #endif
3396 /* Promote both alternatives. */
3398 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3399 op1 = default_conversion (op1);
3400 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3401 op2 = default_conversion (op2);
3403 if (TREE_CODE (ifexp) == ERROR_MARK
3404 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3405 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3406 return error_mark_node;
3408 type1 = TREE_TYPE (op1);
3409 code1 = TREE_CODE (type1);
3410 type2 = TREE_TYPE (op2);
3411 code2 = TREE_CODE (type2);
3413 /* Quickly detect the usual case where op1 and op2 have the same type
3414 after promotion. */
3415 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3417 if (type1 == type2)
3418 result_type = type1;
3419 else
3420 result_type = TYPE_MAIN_VARIANT (type1);
3422 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3423 || code1 == COMPLEX_TYPE)
3424 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3425 || code2 == COMPLEX_TYPE))
3427 result_type = common_type (type1, type2);
3429 /* If -Wsign-compare, warn here if type1 and type2 have
3430 different signedness. We'll promote the signed to unsigned
3431 and later code won't know it used to be different.
3432 Do this check on the original types, so that explicit casts
3433 will be considered, but default promotions won't. */
3434 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare)
3435 && !skip_evaluation)
3437 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3438 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3440 if (unsigned_op1 ^ unsigned_op2)
3442 /* Do not warn if the result type is signed, since the
3443 signed type will only be chosen if it can represent
3444 all the values of the unsigned type. */
3445 if (! TREE_UNSIGNED (result_type))
3446 /* OK */;
3447 /* Do not warn if the signed quantity is an unsuffixed
3448 integer literal (or some static constant expression
3449 involving such literals) and it is non-negative. */
3450 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
3451 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
3452 /* OK */;
3453 else
3454 warning ("signed and unsigned type in conditional expression");
3458 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3460 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3461 pedwarn ("ISO C forbids conditional expr with only one void side");
3462 result_type = void_type_node;
3464 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3466 if (comp_target_types (type1, type2, 1))
3467 result_type = common_type (type1, type2);
3468 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3469 && TREE_CODE (orig_op1) != NOP_EXPR)
3470 result_type = qualify_type (type2, type1);
3471 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3472 && TREE_CODE (orig_op2) != NOP_EXPR)
3473 result_type = qualify_type (type1, type2);
3474 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3476 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3477 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3478 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3479 TREE_TYPE (type2)));
3481 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3483 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3484 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3485 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3486 TREE_TYPE (type1)));
3488 else
3490 pedwarn ("pointer type mismatch in conditional expression");
3491 result_type = build_pointer_type (void_type_node);
3494 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3496 if (! integer_zerop (op2))
3497 pedwarn ("pointer/integer type mismatch in conditional expression");
3498 else
3500 op2 = null_pointer_node;
3502 result_type = type1;
3504 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3506 if (!integer_zerop (op1))
3507 pedwarn ("pointer/integer type mismatch in conditional expression");
3508 else
3510 op1 = null_pointer_node;
3512 result_type = type2;
3515 if (!result_type)
3517 if (flag_cond_mismatch)
3518 result_type = void_type_node;
3519 else
3521 error ("type mismatch in conditional expression");
3522 return error_mark_node;
3526 /* Merge const and volatile flags of the incoming types. */
3527 result_type
3528 = build_type_variant (result_type,
3529 TREE_READONLY (op1) || TREE_READONLY (op2),
3530 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3532 if (result_type != TREE_TYPE (op1))
3533 op1 = convert_and_check (result_type, op1);
3534 if (result_type != TREE_TYPE (op2))
3535 op2 = convert_and_check (result_type, op2);
3537 if (TREE_CODE (ifexp) == INTEGER_CST)
3538 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3540 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3543 /* Given a list of expressions, return a compound expression
3544 that performs them all and returns the value of the last of them. */
3546 tree
3547 build_compound_expr (list)
3548 tree list;
3550 return internal_build_compound_expr (list, TRUE);
3553 static tree
3554 internal_build_compound_expr (list, first_p)
3555 tree list;
3556 int first_p;
3558 tree rest;
3560 if (TREE_CHAIN (list) == 0)
3562 /* Convert arrays and functions to pointers when there
3563 really is a comma operator. */
3564 if (!first_p)
3565 TREE_VALUE (list)
3566 = default_function_array_conversion (TREE_VALUE (list));
3568 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3569 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3571 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3572 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3573 list = TREE_OPERAND (list, 0);
3574 #endif
3576 /* Don't let (0, 0) be null pointer constant. */
3577 if (!first_p && integer_zerop (TREE_VALUE (list)))
3578 return non_lvalue (TREE_VALUE (list));
3579 return TREE_VALUE (list);
3582 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3584 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3586 /* The left-hand operand of a comma expression is like an expression
3587 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3588 any side-effects, unless it was explicitly cast to (void). */
3589 if ((extra_warnings || warn_unused_value)
3590 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3591 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
3592 warning ("left-hand operand of comma expression has no effect");
3594 /* When pedantic, a compound expression can be neither an lvalue
3595 nor an integer constant expression. */
3596 if (! pedantic)
3597 return rest;
3600 /* With -Wunused, we should also warn if the left-hand operand does have
3601 side-effects, but computes a value which is not used. For example, in
3602 `foo() + bar(), baz()' the result of the `+' operator is not used,
3603 so we should issue a warning. */
3604 else if (warn_unused_value)
3605 warn_if_unused_value (TREE_VALUE (list));
3607 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3610 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3612 tree
3613 build_c_cast (type, expr)
3614 tree type;
3615 tree expr;
3617 tree value = expr;
3619 if (type == error_mark_node || expr == error_mark_node)
3620 return error_mark_node;
3622 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3623 only in <protocol> qualifications. But when constructing cast expressions,
3624 the protocols do matter and must be kept around. */
3625 if (!flag_objc || !objc_is_id (type))
3626 type = TYPE_MAIN_VARIANT (type);
3628 #if 0
3629 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3630 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3631 value = TREE_OPERAND (value, 0);
3632 #endif
3634 if (TREE_CODE (type) == ARRAY_TYPE)
3636 error ("cast specifies array type");
3637 return error_mark_node;
3640 if (TREE_CODE (type) == FUNCTION_TYPE)
3642 error ("cast specifies function type");
3643 return error_mark_node;
3646 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3648 if (pedantic)
3650 if (TREE_CODE (type) == RECORD_TYPE
3651 || TREE_CODE (type) == UNION_TYPE)
3652 pedwarn ("ISO C forbids casting nonscalar to the same type");
3655 else if (TREE_CODE (type) == UNION_TYPE)
3657 tree field;
3658 value = default_function_array_conversion (value);
3660 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3661 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3662 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3663 break;
3665 if (field)
3667 tree t;
3669 if (pedantic)
3670 pedwarn ("ISO C forbids casts to union type");
3671 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3672 build_tree_list (field, value)), 0);
3673 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3674 return t;
3676 error ("cast to union type from type not present in union");
3677 return error_mark_node;
3679 else
3681 tree otype, ovalue;
3683 /* If casting to void, avoid the error that would come
3684 from default_conversion in the case of a non-lvalue array. */
3685 if (type == void_type_node)
3686 return build1 (CONVERT_EXPR, type, value);
3688 /* Convert functions and arrays to pointers,
3689 but don't convert any other types. */
3690 value = default_function_array_conversion (value);
3691 otype = TREE_TYPE (value);
3693 /* Optionally warn about potentially worrisome casts. */
3695 if (warn_cast_qual
3696 && TREE_CODE (type) == POINTER_TYPE
3697 && TREE_CODE (otype) == POINTER_TYPE)
3699 tree in_type = type;
3700 tree in_otype = otype;
3701 int added = 0;
3702 int discarded = 0;
3704 /* Check that the qualifiers on IN_TYPE are a superset of
3705 the qualifiers of IN_OTYPE. The outermost level of
3706 POINTER_TYPE nodes is uninteresting and we stop as soon
3707 as we hit a non-POINTER_TYPE node on either type. */
3710 in_otype = TREE_TYPE (in_otype);
3711 in_type = TREE_TYPE (in_type);
3713 /* GNU C allows cv-qualified function types. 'const'
3714 means the function is very pure, 'volatile' means it
3715 can't return. We need to warn when such qualifiers
3716 are added, not when they're taken away. */
3717 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3718 && TREE_CODE (in_type) == FUNCTION_TYPE)
3719 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3720 else
3721 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3723 while (TREE_CODE (in_type) == POINTER_TYPE
3724 && TREE_CODE (in_otype) == POINTER_TYPE);
3726 if (added)
3727 warning ("cast adds new qualifiers to function type");
3729 if (discarded)
3730 /* There are qualifiers present in IN_OTYPE that are not
3731 present in IN_TYPE. */
3732 warning ("cast discards qualifiers from pointer target type");
3735 /* Warn about possible alignment problems. */
3736 if (STRICT_ALIGNMENT && warn_cast_align
3737 && TREE_CODE (type) == POINTER_TYPE
3738 && TREE_CODE (otype) == POINTER_TYPE
3739 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3740 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3741 /* Don't warn about opaque types, where the actual alignment
3742 restriction is unknown. */
3743 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3744 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3745 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3746 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3747 warning ("cast increases required alignment of target type");
3749 if (TREE_CODE (type) == INTEGER_TYPE
3750 && TREE_CODE (otype) == POINTER_TYPE
3751 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3752 && !TREE_CONSTANT (value))
3753 warning ("cast from pointer to integer of different size");
3755 if (warn_bad_function_cast
3756 && TREE_CODE (value) == CALL_EXPR
3757 && TREE_CODE (type) != TREE_CODE (otype))
3758 warning ("cast does not match function type");
3760 if (TREE_CODE (type) == POINTER_TYPE
3761 && TREE_CODE (otype) == INTEGER_TYPE
3762 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3763 /* Don't warn about converting any constant. */
3764 && !TREE_CONSTANT (value))
3765 warning ("cast to pointer from integer of different size");
3767 if (TREE_CODE (type) == POINTER_TYPE
3768 && TREE_CODE (otype) == POINTER_TYPE
3769 && TREE_CODE (expr) == ADDR_EXPR
3770 && DECL_P (TREE_OPERAND (expr, 0))
3771 && flag_strict_aliasing && warn_strict_aliasing
3772 && !VOID_TYPE_P (TREE_TYPE (type)))
3774 /* Casting the address of a decl to non void pointer. Warn
3775 if the cast breaks type based aliasing. */
3776 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3777 warning ("type-punning to incomplete type might break strict-aliasing rules");
3778 else if (!alias_sets_conflict_p
3779 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
3780 get_alias_set (TREE_TYPE (type))))
3781 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3784 ovalue = value;
3785 /* Replace a nonvolatile const static variable with its value. */
3786 if (optimize && TREE_CODE (value) == VAR_DECL)
3787 value = decl_constant_value (value);
3788 value = convert (type, value);
3790 /* Ignore any integer overflow caused by the cast. */
3791 if (TREE_CODE (value) == INTEGER_CST)
3793 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3794 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3798 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3799 if (pedantic && TREE_CODE (value) == INTEGER_CST
3800 && TREE_CODE (expr) == INTEGER_CST
3801 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3802 value = non_lvalue (value);
3804 /* If pedantic, don't let a cast be an lvalue. */
3805 if (value == expr && pedantic)
3806 value = non_lvalue (value);
3808 return value;
3811 /* Interpret a cast of expression EXPR to type TYPE. */
3812 tree
3813 c_cast_expr (type, expr)
3814 tree type, expr;
3816 int saved_wsp = warn_strict_prototypes;
3818 /* This avoids warnings about unprototyped casts on
3819 integers. E.g. "#define SIG_DFL (void(*)())0". */
3820 if (TREE_CODE (expr) == INTEGER_CST)
3821 warn_strict_prototypes = 0;
3822 type = groktypename (type);
3823 warn_strict_prototypes = saved_wsp;
3825 return build_c_cast (type, expr);
3829 /* Build an assignment expression of lvalue LHS from value RHS.
3830 MODIFYCODE is the code for a binary operator that we use
3831 to combine the old value of LHS with RHS to get the new value.
3832 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3834 tree
3835 build_modify_expr (lhs, modifycode, rhs)
3836 tree lhs, rhs;
3837 enum tree_code modifycode;
3839 tree result;
3840 tree newrhs;
3841 tree lhstype = TREE_TYPE (lhs);
3842 tree olhstype = lhstype;
3844 /* Types that aren't fully specified cannot be used in assignments. */
3845 lhs = require_complete_type (lhs);
3847 /* Avoid duplicate error messages from operands that had errors. */
3848 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3849 return error_mark_node;
3851 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3852 /* Do not use STRIP_NOPS here. We do not want an enumerator
3853 whose value is 0 to count as a null pointer constant. */
3854 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3855 rhs = TREE_OPERAND (rhs, 0);
3857 newrhs = rhs;
3859 /* Handle control structure constructs used as "lvalues". */
3861 switch (TREE_CODE (lhs))
3863 /* Handle (a, b) used as an "lvalue". */
3864 case COMPOUND_EXPR:
3865 pedantic_lvalue_warning (COMPOUND_EXPR);
3866 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3867 if (TREE_CODE (newrhs) == ERROR_MARK)
3868 return error_mark_node;
3869 return build (COMPOUND_EXPR, lhstype,
3870 TREE_OPERAND (lhs, 0), newrhs);
3872 /* Handle (a ? b : c) used as an "lvalue". */
3873 case COND_EXPR:
3874 pedantic_lvalue_warning (COND_EXPR);
3875 rhs = save_expr (rhs);
3877 /* Produce (a ? (b = rhs) : (c = rhs))
3878 except that the RHS goes through a save-expr
3879 so the code to compute it is only emitted once. */
3880 tree cond
3881 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3882 build_modify_expr (TREE_OPERAND (lhs, 1),
3883 modifycode, rhs),
3884 build_modify_expr (TREE_OPERAND (lhs, 2),
3885 modifycode, rhs));
3886 if (TREE_CODE (cond) == ERROR_MARK)
3887 return cond;
3888 /* Make sure the code to compute the rhs comes out
3889 before the split. */
3890 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3891 /* But cast it to void to avoid an "unused" error. */
3892 convert (void_type_node, rhs), cond);
3894 default:
3895 break;
3898 /* If a binary op has been requested, combine the old LHS value with the RHS
3899 producing the value we should actually store into the LHS. */
3901 if (modifycode != NOP_EXPR)
3903 lhs = stabilize_reference (lhs);
3904 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3907 /* Handle a cast used as an "lvalue".
3908 We have already performed any binary operator using the value as cast.
3909 Now convert the result to the cast type of the lhs,
3910 and then true type of the lhs and store it there;
3911 then convert result back to the cast type to be the value
3912 of the assignment. */
3914 switch (TREE_CODE (lhs))
3916 case NOP_EXPR:
3917 case CONVERT_EXPR:
3918 case FLOAT_EXPR:
3919 case FIX_TRUNC_EXPR:
3920 case FIX_FLOOR_EXPR:
3921 case FIX_ROUND_EXPR:
3922 case FIX_CEIL_EXPR:
3923 newrhs = default_function_array_conversion (newrhs);
3925 tree inner_lhs = TREE_OPERAND (lhs, 0);
3926 tree result;
3927 result = build_modify_expr (inner_lhs, NOP_EXPR,
3928 convert (TREE_TYPE (inner_lhs),
3929 convert (lhstype, newrhs)));
3930 if (TREE_CODE (result) == ERROR_MARK)
3931 return result;
3932 pedantic_lvalue_warning (CONVERT_EXPR);
3933 return convert (TREE_TYPE (lhs), result);
3936 default:
3937 break;
3940 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3941 Reject anything strange now. */
3943 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3944 return error_mark_node;
3946 /* Warn about storing in something that is `const'. */
3948 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3949 || ((TREE_CODE (lhstype) == RECORD_TYPE
3950 || TREE_CODE (lhstype) == UNION_TYPE)
3951 && C_TYPE_FIELDS_READONLY (lhstype)))
3952 readonly_warning (lhs, "assignment");
3954 /* If storing into a structure or union member,
3955 it has probably been given type `int'.
3956 Compute the type that would go with
3957 the actual amount of storage the member occupies. */
3959 if (TREE_CODE (lhs) == COMPONENT_REF
3960 && (TREE_CODE (lhstype) == INTEGER_TYPE
3961 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3962 || TREE_CODE (lhstype) == REAL_TYPE
3963 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3964 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3966 /* If storing in a field that is in actuality a short or narrower than one,
3967 we must store in the field in its actual type. */
3969 if (lhstype != TREE_TYPE (lhs))
3971 lhs = copy_node (lhs);
3972 TREE_TYPE (lhs) = lhstype;
3975 /* Convert new value to destination type. */
3977 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3978 NULL_TREE, NULL_TREE, 0);
3979 if (TREE_CODE (newrhs) == ERROR_MARK)
3980 return error_mark_node;
3982 /* Scan operands */
3984 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3985 TREE_SIDE_EFFECTS (result) = 1;
3987 /* If we got the LHS in a different type for storing in,
3988 convert the result back to the nominal type of LHS
3989 so that the value we return always has the same type
3990 as the LHS argument. */
3992 if (olhstype == TREE_TYPE (result))
3993 return result;
3994 return convert_for_assignment (olhstype, result, _("assignment"),
3995 NULL_TREE, NULL_TREE, 0);
3998 /* Convert value RHS to type TYPE as preparation for an assignment
3999 to an lvalue of type TYPE.
4000 The real work of conversion is done by `convert'.
4001 The purpose of this function is to generate error messages
4002 for assignments that are not allowed in C.
4003 ERRTYPE is a string to use in error messages:
4004 "assignment", "return", etc. If it is null, this is parameter passing
4005 for a function call (and different error messages are output).
4007 FUNNAME is the name of the function being called,
4008 as an IDENTIFIER_NODE, or null.
4009 PARMNUM is the number of the argument, for printing in error messages. */
4011 static tree
4012 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
4013 tree type, rhs;
4014 const char *errtype;
4015 tree fundecl, funname;
4016 int parmnum;
4018 enum tree_code codel = TREE_CODE (type);
4019 tree rhstype;
4020 enum tree_code coder;
4022 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4023 /* Do not use STRIP_NOPS here. We do not want an enumerator
4024 whose value is 0 to count as a null pointer constant. */
4025 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4026 rhs = TREE_OPERAND (rhs, 0);
4028 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4029 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4030 rhs = default_conversion (rhs);
4031 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4032 rhs = decl_constant_value_for_broken_optimization (rhs);
4034 rhstype = TREE_TYPE (rhs);
4035 coder = TREE_CODE (rhstype);
4037 if (coder == ERROR_MARK)
4038 return error_mark_node;
4040 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4042 overflow_warning (rhs);
4043 /* Check for Objective-C protocols. This will automatically
4044 issue a warning if there are protocol violations. No need to
4045 use the return value. */
4046 if (flag_objc)
4047 objc_comptypes (type, rhstype, 0);
4048 return rhs;
4051 if (coder == VOID_TYPE)
4053 error ("void value not ignored as it ought to be");
4054 return error_mark_node;
4056 /* A type converts to a reference to it.
4057 This code doesn't fully support references, it's just for the
4058 special case of va_start and va_copy. */
4059 if (codel == REFERENCE_TYPE
4060 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4062 if (!lvalue_p (rhs))
4064 error ("cannot pass rvalue to reference parameter");
4065 return error_mark_node;
4067 if (!c_mark_addressable (rhs))
4068 return error_mark_node;
4069 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4071 /* We already know that these two types are compatible, but they
4072 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4073 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4074 likely to be va_list, a typedef to __builtin_va_list, which
4075 is different enough that it will cause problems later. */
4076 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4077 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4079 rhs = build1 (NOP_EXPR, type, rhs);
4080 return rhs;
4082 /* Some types can interconvert without explicit casts. */
4083 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4084 && ((*targetm.vector_opaque_p) (type)
4085 || (*targetm.vector_opaque_p) (rhstype)))
4086 return convert (type, rhs);
4087 /* Arithmetic types all interconvert, and enum is treated like int. */
4088 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4089 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4090 || codel == BOOLEAN_TYPE)
4091 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4092 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4093 || coder == BOOLEAN_TYPE))
4094 return convert_and_check (type, rhs);
4096 /* Conversion to a transparent union from its member types.
4097 This applies only to function arguments. */
4098 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4100 tree memb_types;
4101 tree marginal_memb_type = 0;
4103 for (memb_types = TYPE_FIELDS (type); memb_types;
4104 memb_types = TREE_CHAIN (memb_types))
4106 tree memb_type = TREE_TYPE (memb_types);
4108 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4109 TYPE_MAIN_VARIANT (rhstype)))
4110 break;
4112 if (TREE_CODE (memb_type) != POINTER_TYPE)
4113 continue;
4115 if (coder == POINTER_TYPE)
4117 tree ttl = TREE_TYPE (memb_type);
4118 tree ttr = TREE_TYPE (rhstype);
4120 /* Any non-function converts to a [const][volatile] void *
4121 and vice versa; otherwise, targets must be the same.
4122 Meanwhile, the lhs target must have all the qualifiers of
4123 the rhs. */
4124 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4125 || comp_target_types (memb_type, rhstype, 0))
4127 /* If this type won't generate any warnings, use it. */
4128 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4129 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4130 && TREE_CODE (ttl) == FUNCTION_TYPE)
4131 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4132 == TYPE_QUALS (ttr))
4133 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4134 == TYPE_QUALS (ttl))))
4135 break;
4137 /* Keep looking for a better type, but remember this one. */
4138 if (! marginal_memb_type)
4139 marginal_memb_type = memb_type;
4143 /* Can convert integer zero to any pointer type. */
4144 if (integer_zerop (rhs)
4145 || (TREE_CODE (rhs) == NOP_EXPR
4146 && integer_zerop (TREE_OPERAND (rhs, 0))))
4148 rhs = null_pointer_node;
4149 break;
4153 if (memb_types || marginal_memb_type)
4155 if (! memb_types)
4157 /* We have only a marginally acceptable member type;
4158 it needs a warning. */
4159 tree ttl = TREE_TYPE (marginal_memb_type);
4160 tree ttr = TREE_TYPE (rhstype);
4162 /* Const and volatile mean something different for function
4163 types, so the usual warnings are not appropriate. */
4164 if (TREE_CODE (ttr) == FUNCTION_TYPE
4165 && TREE_CODE (ttl) == FUNCTION_TYPE)
4167 /* Because const and volatile on functions are
4168 restrictions that say the function will not do
4169 certain things, it is okay to use a const or volatile
4170 function where an ordinary one is wanted, but not
4171 vice-versa. */
4172 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4173 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4174 errtype, funname, parmnum);
4176 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4177 warn_for_assignment ("%s discards qualifiers from pointer target type",
4178 errtype, funname,
4179 parmnum);
4182 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4183 pedwarn ("ISO C prohibits argument conversion to union type");
4185 return build1 (NOP_EXPR, type, rhs);
4189 /* Conversions among pointers */
4190 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4191 && (coder == codel))
4193 tree ttl = TREE_TYPE (type);
4194 tree ttr = TREE_TYPE (rhstype);
4196 /* Any non-function converts to a [const][volatile] void *
4197 and vice versa; otherwise, targets must be the same.
4198 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4199 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4200 || comp_target_types (type, rhstype, 0)
4201 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
4202 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4204 if (pedantic
4205 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4207 (VOID_TYPE_P (ttr)
4208 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4209 which are not ANSI null ptr constants. */
4210 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4211 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4212 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
4213 errtype, funname, parmnum);
4214 /* Const and volatile mean something different for function types,
4215 so the usual warnings are not appropriate. */
4216 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4217 && TREE_CODE (ttl) != FUNCTION_TYPE)
4219 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4220 warn_for_assignment ("%s discards qualifiers from pointer target type",
4221 errtype, funname, parmnum);
4222 /* If this is not a case of ignoring a mismatch in signedness,
4223 no warning. */
4224 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4225 || comp_target_types (type, rhstype, 0))
4227 /* If there is a mismatch, do warn. */
4228 else if (pedantic)
4229 warn_for_assignment ("pointer targets in %s differ in signedness",
4230 errtype, funname, parmnum);
4232 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4233 && TREE_CODE (ttr) == FUNCTION_TYPE)
4235 /* Because const and volatile on functions are restrictions
4236 that say the function will not do certain things,
4237 it is okay to use a const or volatile function
4238 where an ordinary one is wanted, but not vice-versa. */
4239 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4240 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4241 errtype, funname, parmnum);
4244 else
4245 warn_for_assignment ("%s from incompatible pointer type",
4246 errtype, funname, parmnum);
4247 return convert (type, rhs);
4249 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4251 /* An explicit constant 0 can convert to a pointer,
4252 or one that results from arithmetic, even including
4253 a cast to integer type. */
4254 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4256 ! (TREE_CODE (rhs) == NOP_EXPR
4257 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4258 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4259 && integer_zerop (TREE_OPERAND (rhs, 0))))
4261 warn_for_assignment ("%s makes pointer from integer without a cast",
4262 errtype, funname, parmnum);
4263 return convert (type, rhs);
4265 return null_pointer_node;
4267 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4269 warn_for_assignment ("%s makes integer from pointer without a cast",
4270 errtype, funname, parmnum);
4271 return convert (type, rhs);
4273 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4274 return convert (type, rhs);
4276 if (!errtype)
4278 if (funname)
4280 tree selector = objc_message_selector ();
4282 if (selector && parmnum > 2)
4283 error ("incompatible type for argument %d of `%s'",
4284 parmnum - 2, IDENTIFIER_POINTER (selector));
4285 else
4286 error ("incompatible type for argument %d of `%s'",
4287 parmnum, IDENTIFIER_POINTER (funname));
4289 else
4290 error ("incompatible type for argument %d of indirect function call",
4291 parmnum);
4293 else
4294 error ("incompatible types in %s", errtype);
4296 return error_mark_node;
4299 /* Convert VALUE for assignment into inlined parameter PARM. */
4301 tree
4302 c_convert_parm_for_inlining (parm, value, fn)
4303 tree parm, value, fn;
4305 tree ret, type;
4307 /* If FN was prototyped, the value has been converted already
4308 in convert_arguments. */
4309 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
4310 return value;
4312 type = TREE_TYPE (parm);
4313 ret = convert_for_assignment (type, value,
4314 (char *) 0 /* arg passing */, fn,
4315 DECL_NAME (fn), 0);
4316 if (PROMOTE_PROTOTYPES
4317 && INTEGRAL_TYPE_P (type)
4318 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4319 ret = default_conversion (ret);
4320 return ret;
4323 /* Print a warning using MSGID.
4324 It gets OPNAME as its one parameter.
4325 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
4326 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4327 FUNCTION and ARGNUM are handled specially if we are building an
4328 Objective-C selector. */
4330 static void
4331 warn_for_assignment (msgid, opname, function, argnum)
4332 const char *msgid;
4333 const char *opname;
4334 tree function;
4335 int argnum;
4337 if (opname == 0)
4339 tree selector = objc_message_selector ();
4340 char * new_opname;
4342 if (selector && argnum > 2)
4344 function = selector;
4345 argnum -= 2;
4347 if (argnum == 0)
4349 if (function)
4351 /* Function name is known; supply it. */
4352 const char *const argstring = _("passing arg of `%s'");
4353 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4354 + strlen (argstring) + 1
4355 + 1);
4356 sprintf (new_opname, argstring,
4357 IDENTIFIER_POINTER (function));
4359 else
4361 /* Function name unknown (call through ptr). */
4362 const char *const argnofun = _("passing arg of pointer to function");
4363 new_opname = (char *) alloca (strlen (argnofun) + 1 + 1);
4364 sprintf (new_opname, argnofun);
4367 else if (function)
4369 /* Function name is known; supply it. */
4370 const char *const argstring = _("passing arg %d of `%s'");
4371 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4372 + strlen (argstring) + 1 + 25
4373 /*%d*/ + 1);
4374 sprintf (new_opname, argstring, argnum,
4375 IDENTIFIER_POINTER (function));
4377 else
4379 /* Function name unknown (call through ptr); just give arg number. */
4380 const char *const argnofun = _("passing arg %d of pointer to function");
4381 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4382 sprintf (new_opname, argnofun, argnum);
4384 opname = new_opname;
4386 pedwarn (msgid, opname);
4389 /* If VALUE is a compound expr all of whose expressions are constant, then
4390 return its value. Otherwise, return error_mark_node.
4392 This is for handling COMPOUND_EXPRs as initializer elements
4393 which is allowed with a warning when -pedantic is specified. */
4395 static tree
4396 valid_compound_expr_initializer (value, endtype)
4397 tree value;
4398 tree endtype;
4400 if (TREE_CODE (value) == COMPOUND_EXPR)
4402 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4403 == error_mark_node)
4404 return error_mark_node;
4405 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4406 endtype);
4408 else if (! TREE_CONSTANT (value)
4409 && ! initializer_constant_valid_p (value, endtype))
4410 return error_mark_node;
4411 else
4412 return value;
4415 /* Perform appropriate conversions on the initial value of a variable,
4416 store it in the declaration DECL,
4417 and print any error messages that are appropriate.
4418 If the init is invalid, store an ERROR_MARK. */
4420 void
4421 store_init_value (decl, init)
4422 tree decl, init;
4424 tree value, type;
4426 /* If variable's type was invalidly declared, just ignore it. */
4428 type = TREE_TYPE (decl);
4429 if (TREE_CODE (type) == ERROR_MARK)
4430 return;
4432 /* Digest the specified initializer into an expression. */
4434 value = digest_init (type, init, TREE_STATIC (decl));
4436 /* Store the expression if valid; else report error. */
4438 #if 0
4439 /* Note that this is the only place we can detect the error
4440 in a case such as struct foo bar = (struct foo) { x, y };
4441 where there is one initial value which is a constructor expression. */
4442 if (value == error_mark_node)
4444 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4446 error ("initializer for static variable is not constant");
4447 value = error_mark_node;
4449 else if (TREE_STATIC (decl)
4450 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4452 error ("initializer for static variable uses complicated arithmetic");
4453 value = error_mark_node;
4455 else
4457 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4459 if (! TREE_CONSTANT (value))
4460 pedwarn ("aggregate initializer is not constant");
4461 else if (! TREE_STATIC (value))
4462 pedwarn ("aggregate initializer uses complicated arithmetic");
4465 #endif
4467 if (warn_traditional && !in_system_header
4468 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
4469 warning ("traditional C rejects automatic aggregate initialization");
4471 DECL_INITIAL (decl) = value;
4473 /* ANSI wants warnings about out-of-range constant initializers. */
4474 STRIP_TYPE_NOPS (value);
4475 constant_expression_warning (value);
4477 /* Check if we need to set array size from compound literal size. */
4478 if (TREE_CODE (type) == ARRAY_TYPE
4479 && TYPE_DOMAIN (type) == 0
4480 && value != error_mark_node)
4482 tree inside_init = init;
4484 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4485 inside_init = TREE_OPERAND (init, 0);
4486 inside_init = fold (inside_init);
4488 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4490 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4492 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4494 /* For int foo[] = (int [3]){1}; we need to set array size
4495 now since later on array initializer will be just the
4496 brace enclosed list of the compound literal. */
4497 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4498 layout_type (type);
4499 layout_decl (decl, 0);
4505 /* Methods for storing and printing names for error messages. */
4507 /* Implement a spelling stack that allows components of a name to be pushed
4508 and popped. Each element on the stack is this structure. */
4510 struct spelling
4512 int kind;
4513 union
4515 int i;
4516 const char *s;
4517 } u;
4520 #define SPELLING_STRING 1
4521 #define SPELLING_MEMBER 2
4522 #define SPELLING_BOUNDS 3
4524 static struct spelling *spelling; /* Next stack element (unused). */
4525 static struct spelling *spelling_base; /* Spelling stack base. */
4526 static int spelling_size; /* Size of the spelling stack. */
4528 /* Macros to save and restore the spelling stack around push_... functions.
4529 Alternative to SAVE_SPELLING_STACK. */
4531 #define SPELLING_DEPTH() (spelling - spelling_base)
4532 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4534 /* Push an element on the spelling stack with type KIND and assign VALUE
4535 to MEMBER. */
4537 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4539 int depth = SPELLING_DEPTH (); \
4541 if (depth >= spelling_size) \
4543 spelling_size += 10; \
4544 if (spelling_base == 0) \
4545 spelling_base \
4546 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4547 else \
4548 spelling_base \
4549 = (struct spelling *) xrealloc (spelling_base, \
4550 spelling_size * sizeof (struct spelling)); \
4551 RESTORE_SPELLING_DEPTH (depth); \
4554 spelling->kind = (KIND); \
4555 spelling->MEMBER = (VALUE); \
4556 spelling++; \
4559 /* Push STRING on the stack. Printed literally. */
4561 static void
4562 push_string (string)
4563 const char *string;
4565 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4568 /* Push a member name on the stack. Printed as '.' STRING. */
4570 static void
4571 push_member_name (decl)
4572 tree decl;
4575 const char *const string
4576 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4577 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4580 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4582 static void
4583 push_array_bounds (bounds)
4584 int bounds;
4586 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4589 /* Compute the maximum size in bytes of the printed spelling. */
4591 static int
4592 spelling_length ()
4594 int size = 0;
4595 struct spelling *p;
4597 for (p = spelling_base; p < spelling; p++)
4599 if (p->kind == SPELLING_BOUNDS)
4600 size += 25;
4601 else
4602 size += strlen (p->u.s) + 1;
4605 return size;
4608 /* Print the spelling to BUFFER and return it. */
4610 static char *
4611 print_spelling (buffer)
4612 char *buffer;
4614 char *d = buffer;
4615 struct spelling *p;
4617 for (p = spelling_base; p < spelling; p++)
4618 if (p->kind == SPELLING_BOUNDS)
4620 sprintf (d, "[%d]", p->u.i);
4621 d += strlen (d);
4623 else
4625 const char *s;
4626 if (p->kind == SPELLING_MEMBER)
4627 *d++ = '.';
4628 for (s = p->u.s; (*d = *s++); d++)
4631 *d++ = '\0';
4632 return buffer;
4635 /* Issue an error message for a bad initializer component.
4636 MSGID identifies the message.
4637 The component name is taken from the spelling stack. */
4639 void
4640 error_init (msgid)
4641 const char *msgid;
4643 char *ofwhat;
4645 error ("%s", _(msgid));
4646 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4647 if (*ofwhat)
4648 error ("(near initialization for `%s')", ofwhat);
4651 /* Issue a pedantic warning for a bad initializer component.
4652 MSGID identifies the message.
4653 The component name is taken from the spelling stack. */
4655 void
4656 pedwarn_init (msgid)
4657 const char *msgid;
4659 char *ofwhat;
4661 pedwarn ("%s", _(msgid));
4662 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4663 if (*ofwhat)
4664 pedwarn ("(near initialization for `%s')", ofwhat);
4667 /* Issue a warning for a bad initializer component.
4668 MSGID identifies the message.
4669 The component name is taken from the spelling stack. */
4671 static void
4672 warning_init (msgid)
4673 const char *msgid;
4675 char *ofwhat;
4677 warning ("%s", _(msgid));
4678 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4679 if (*ofwhat)
4680 warning ("(near initialization for `%s')", ofwhat);
4683 /* Digest the parser output INIT as an initializer for type TYPE.
4684 Return a C expression of type TYPE to represent the initial value.
4686 REQUIRE_CONSTANT requests an error if non-constant initializers or
4687 elements are seen. */
4689 static tree
4690 digest_init (type, init, require_constant)
4691 tree type, init;
4692 int require_constant;
4694 enum tree_code code = TREE_CODE (type);
4695 tree inside_init = init;
4697 if (type == error_mark_node
4698 || init == error_mark_node
4699 || TREE_TYPE (init) == error_mark_node)
4700 return error_mark_node;
4702 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4703 /* Do not use STRIP_NOPS here. We do not want an enumerator
4704 whose value is 0 to count as a null pointer constant. */
4705 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4706 inside_init = TREE_OPERAND (init, 0);
4708 inside_init = fold (inside_init);
4710 /* Initialization of an array of chars from a string constant
4711 optionally enclosed in braces. */
4713 if (code == ARRAY_TYPE)
4715 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4716 if ((typ1 == char_type_node
4717 || typ1 == signed_char_type_node
4718 || typ1 == unsigned_char_type_node
4719 || typ1 == unsigned_wchar_type_node
4720 || typ1 == signed_wchar_type_node)
4721 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4723 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4724 TYPE_MAIN_VARIANT (type)))
4725 return inside_init;
4727 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4728 != char_type_node)
4729 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4731 error_init ("char-array initialized from wide string");
4732 return error_mark_node;
4734 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4735 == char_type_node)
4736 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4738 error_init ("int-array initialized from non-wide string");
4739 return error_mark_node;
4742 TREE_TYPE (inside_init) = type;
4743 if (TYPE_DOMAIN (type) != 0
4744 && TYPE_SIZE (type) != 0
4745 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4746 /* Subtract 1 (or sizeof (wchar_t))
4747 because it's ok to ignore the terminating null char
4748 that is counted in the length of the constant. */
4749 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4750 TREE_STRING_LENGTH (inside_init)
4751 - ((TYPE_PRECISION (typ1)
4752 != TYPE_PRECISION (char_type_node))
4753 ? (TYPE_PRECISION (wchar_type_node)
4754 / BITS_PER_UNIT)
4755 : 1)))
4756 pedwarn_init ("initializer-string for array of chars is too long");
4758 return inside_init;
4762 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4763 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4764 below and handle as a constructor. */
4765 if (code == VECTOR_TYPE
4766 && comptypes (TREE_TYPE (inside_init), type)
4767 && TREE_CONSTANT (inside_init))
4768 return build_vector (type, TREE_OPERAND (inside_init, 1));
4770 /* Any type can be initialized
4771 from an expression of the same type, optionally with braces. */
4773 if (inside_init && TREE_TYPE (inside_init) != 0
4774 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4775 TYPE_MAIN_VARIANT (type))
4776 || (code == ARRAY_TYPE
4777 && comptypes (TREE_TYPE (inside_init), type))
4778 || (code == VECTOR_TYPE
4779 && comptypes (TREE_TYPE (inside_init), type))
4780 || (code == POINTER_TYPE
4781 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4782 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4783 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4784 TREE_TYPE (type)))))
4786 if (code == POINTER_TYPE)
4787 inside_init = default_function_array_conversion (inside_init);
4789 if (require_constant && !flag_isoc99
4790 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4792 /* As an extension, allow initializing objects with static storage
4793 duration with compound literals (which are then treated just as
4794 the brace enclosed list they contain). */
4795 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4796 inside_init = DECL_INITIAL (decl);
4799 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4800 && TREE_CODE (inside_init) != CONSTRUCTOR)
4802 error_init ("array initialized from non-constant array expression");
4803 return error_mark_node;
4806 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4807 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4809 /* Compound expressions can only occur here if -pedantic or
4810 -pedantic-errors is specified. In the later case, we always want
4811 an error. In the former case, we simply want a warning. */
4812 if (require_constant && pedantic
4813 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4815 inside_init
4816 = valid_compound_expr_initializer (inside_init,
4817 TREE_TYPE (inside_init));
4818 if (inside_init == error_mark_node)
4819 error_init ("initializer element is not constant");
4820 else
4821 pedwarn_init ("initializer element is not constant");
4822 if (flag_pedantic_errors)
4823 inside_init = error_mark_node;
4825 else if (require_constant
4826 && (!TREE_CONSTANT (inside_init)
4827 /* This test catches things like `7 / 0' which
4828 result in an expression for which TREE_CONSTANT
4829 is true, but which is not actually something
4830 that is a legal constant. We really should not
4831 be using this function, because it is a part of
4832 the back-end. Instead, the expression should
4833 already have been turned into ERROR_MARK_NODE. */
4834 || !initializer_constant_valid_p (inside_init,
4835 TREE_TYPE (inside_init))))
4837 error_init ("initializer element is not constant");
4838 inside_init = error_mark_node;
4841 return inside_init;
4844 /* Handle scalar types, including conversions. */
4846 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4847 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4849 /* Note that convert_for_assignment calls default_conversion
4850 for arrays and functions. We must not call it in the
4851 case where inside_init is a null pointer constant. */
4852 inside_init
4853 = convert_for_assignment (type, init, _("initialization"),
4854 NULL_TREE, NULL_TREE, 0);
4856 if (require_constant && ! TREE_CONSTANT (inside_init))
4858 error_init ("initializer element is not constant");
4859 inside_init = error_mark_node;
4861 else if (require_constant
4862 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4864 error_init ("initializer element is not computable at load time");
4865 inside_init = error_mark_node;
4868 return inside_init;
4871 /* Come here only for records and arrays. */
4873 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4875 error_init ("variable-sized object may not be initialized");
4876 return error_mark_node;
4879 error_init ("invalid initializer");
4880 return error_mark_node;
4883 /* Handle initializers that use braces. */
4885 /* Type of object we are accumulating a constructor for.
4886 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4887 static tree constructor_type;
4889 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4890 left to fill. */
4891 static tree constructor_fields;
4893 /* For an ARRAY_TYPE, this is the specified index
4894 at which to store the next element we get. */
4895 static tree constructor_index;
4897 /* For an ARRAY_TYPE, this is the maximum index. */
4898 static tree constructor_max_index;
4900 /* For a RECORD_TYPE, this is the first field not yet written out. */
4901 static tree constructor_unfilled_fields;
4903 /* For an ARRAY_TYPE, this is the index of the first element
4904 not yet written out. */
4905 static tree constructor_unfilled_index;
4907 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4908 This is so we can generate gaps between fields, when appropriate. */
4909 static tree constructor_bit_index;
4911 /* If we are saving up the elements rather than allocating them,
4912 this is the list of elements so far (in reverse order,
4913 most recent first). */
4914 static tree constructor_elements;
4916 /* 1 if constructor should be incrementally stored into a constructor chain,
4917 0 if all the elements should be kept in AVL tree. */
4918 static int constructor_incremental;
4920 /* 1 if so far this constructor's elements are all compile-time constants. */
4921 static int constructor_constant;
4923 /* 1 if so far this constructor's elements are all valid address constants. */
4924 static int constructor_simple;
4926 /* 1 if this constructor is erroneous so far. */
4927 static int constructor_erroneous;
4929 /* 1 if have called defer_addressed_constants. */
4930 static int constructor_subconstants_deferred;
4932 /* Structure for managing pending initializer elements, organized as an
4933 AVL tree. */
4935 struct init_node
4937 struct init_node *left, *right;
4938 struct init_node *parent;
4939 int balance;
4940 tree purpose;
4941 tree value;
4944 /* Tree of pending elements at this constructor level.
4945 These are elements encountered out of order
4946 which belong at places we haven't reached yet in actually
4947 writing the output.
4948 Will never hold tree nodes across GC runs. */
4949 static struct init_node *constructor_pending_elts;
4951 /* The SPELLING_DEPTH of this constructor. */
4952 static int constructor_depth;
4954 /* 0 if implicitly pushing constructor levels is allowed. */
4955 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4957 static int require_constant_value;
4958 static int require_constant_elements;
4960 /* DECL node for which an initializer is being read.
4961 0 means we are reading a constructor expression
4962 such as (struct foo) {...}. */
4963 static tree constructor_decl;
4965 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4966 static const char *constructor_asmspec;
4968 /* Nonzero if this is an initializer for a top-level decl. */
4969 static int constructor_top_level;
4971 /* Nonzero if there were any member designators in this initializer. */
4972 static int constructor_designated;
4974 /* Nesting depth of designator list. */
4975 static int designator_depth;
4977 /* Nonzero if there were diagnosed errors in this designator list. */
4978 static int designator_errorneous;
4981 /* This stack has a level for each implicit or explicit level of
4982 structuring in the initializer, including the outermost one. It
4983 saves the values of most of the variables above. */
4985 struct constructor_range_stack;
4987 struct constructor_stack
4989 struct constructor_stack *next;
4990 tree type;
4991 tree fields;
4992 tree index;
4993 tree max_index;
4994 tree unfilled_index;
4995 tree unfilled_fields;
4996 tree bit_index;
4997 tree elements;
4998 struct init_node *pending_elts;
4999 int offset;
5000 int depth;
5001 /* If nonzero, this value should replace the entire
5002 constructor at this level. */
5003 tree replacement_value;
5004 struct constructor_range_stack *range_stack;
5005 char constant;
5006 char simple;
5007 char implicit;
5008 char erroneous;
5009 char outer;
5010 char incremental;
5011 char designated;
5014 struct constructor_stack *constructor_stack;
5016 /* This stack represents designators from some range designator up to
5017 the last designator in the list. */
5019 struct constructor_range_stack
5021 struct constructor_range_stack *next, *prev;
5022 struct constructor_stack *stack;
5023 tree range_start;
5024 tree index;
5025 tree range_end;
5026 tree fields;
5029 struct constructor_range_stack *constructor_range_stack;
5031 /* This stack records separate initializers that are nested.
5032 Nested initializers can't happen in ANSI C, but GNU C allows them
5033 in cases like { ... (struct foo) { ... } ... }. */
5035 struct initializer_stack
5037 struct initializer_stack *next;
5038 tree decl;
5039 const char *asmspec;
5040 struct constructor_stack *constructor_stack;
5041 struct constructor_range_stack *constructor_range_stack;
5042 tree elements;
5043 struct spelling *spelling;
5044 struct spelling *spelling_base;
5045 int spelling_size;
5046 char top_level;
5047 char require_constant_value;
5048 char require_constant_elements;
5049 char deferred;
5052 struct initializer_stack *initializer_stack;
5054 /* Prepare to parse and output the initializer for variable DECL. */
5056 void
5057 start_init (decl, asmspec_tree, top_level)
5058 tree decl;
5059 tree asmspec_tree;
5060 int top_level;
5062 const char *locus;
5063 struct initializer_stack *p
5064 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5065 const char *asmspec = 0;
5067 if (asmspec_tree)
5068 asmspec = TREE_STRING_POINTER (asmspec_tree);
5070 p->decl = constructor_decl;
5071 p->asmspec = constructor_asmspec;
5072 p->require_constant_value = require_constant_value;
5073 p->require_constant_elements = require_constant_elements;
5074 p->constructor_stack = constructor_stack;
5075 p->constructor_range_stack = constructor_range_stack;
5076 p->elements = constructor_elements;
5077 p->spelling = spelling;
5078 p->spelling_base = spelling_base;
5079 p->spelling_size = spelling_size;
5080 p->deferred = constructor_subconstants_deferred;
5081 p->top_level = constructor_top_level;
5082 p->next = initializer_stack;
5083 initializer_stack = p;
5085 constructor_decl = decl;
5086 constructor_asmspec = asmspec;
5087 constructor_subconstants_deferred = 0;
5088 constructor_designated = 0;
5089 constructor_top_level = top_level;
5091 if (decl != 0)
5093 require_constant_value = TREE_STATIC (decl);
5094 require_constant_elements
5095 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5096 /* For a scalar, you can always use any value to initialize,
5097 even within braces. */
5098 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5099 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5100 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5101 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5102 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5104 else
5106 require_constant_value = 0;
5107 require_constant_elements = 0;
5108 locus = "(anonymous)";
5111 constructor_stack = 0;
5112 constructor_range_stack = 0;
5114 missing_braces_mentioned = 0;
5116 spelling_base = 0;
5117 spelling_size = 0;
5118 RESTORE_SPELLING_DEPTH (0);
5120 if (locus)
5121 push_string (locus);
5124 void
5125 finish_init ()
5127 struct initializer_stack *p = initializer_stack;
5129 /* Output subconstants (string constants, usually)
5130 that were referenced within this initializer and saved up.
5131 Must do this if and only if we called defer_addressed_constants. */
5132 if (constructor_subconstants_deferred)
5133 output_deferred_addressed_constants ();
5135 /* Free the whole constructor stack of this initializer. */
5136 while (constructor_stack)
5138 struct constructor_stack *q = constructor_stack;
5139 constructor_stack = q->next;
5140 free (q);
5143 if (constructor_range_stack)
5144 abort ();
5146 /* Pop back to the data of the outer initializer (if any). */
5147 constructor_decl = p->decl;
5148 constructor_asmspec = p->asmspec;
5149 require_constant_value = p->require_constant_value;
5150 require_constant_elements = p->require_constant_elements;
5151 constructor_stack = p->constructor_stack;
5152 constructor_range_stack = p->constructor_range_stack;
5153 constructor_elements = p->elements;
5154 spelling = p->spelling;
5155 spelling_base = p->spelling_base;
5156 spelling_size = p->spelling_size;
5157 constructor_subconstants_deferred = p->deferred;
5158 constructor_top_level = p->top_level;
5159 initializer_stack = p->next;
5160 free (p);
5163 /* Call here when we see the initializer is surrounded by braces.
5164 This is instead of a call to push_init_level;
5165 it is matched by a call to pop_init_level.
5167 TYPE is the type to initialize, for a constructor expression.
5168 For an initializer for a decl, TYPE is zero. */
5170 void
5171 really_start_incremental_init (type)
5172 tree type;
5174 struct constructor_stack *p
5175 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5177 if (type == 0)
5178 type = TREE_TYPE (constructor_decl);
5180 if ((*targetm.vector_opaque_p) (type))
5181 error ("opaque vector types cannot be initialized");
5183 p->type = constructor_type;
5184 p->fields = constructor_fields;
5185 p->index = constructor_index;
5186 p->max_index = constructor_max_index;
5187 p->unfilled_index = constructor_unfilled_index;
5188 p->unfilled_fields = constructor_unfilled_fields;
5189 p->bit_index = constructor_bit_index;
5190 p->elements = constructor_elements;
5191 p->constant = constructor_constant;
5192 p->simple = constructor_simple;
5193 p->erroneous = constructor_erroneous;
5194 p->pending_elts = constructor_pending_elts;
5195 p->depth = constructor_depth;
5196 p->replacement_value = 0;
5197 p->implicit = 0;
5198 p->range_stack = 0;
5199 p->outer = 0;
5200 p->incremental = constructor_incremental;
5201 p->designated = constructor_designated;
5202 p->next = 0;
5203 constructor_stack = p;
5205 constructor_constant = 1;
5206 constructor_simple = 1;
5207 constructor_depth = SPELLING_DEPTH ();
5208 constructor_elements = 0;
5209 constructor_pending_elts = 0;
5210 constructor_type = type;
5211 constructor_incremental = 1;
5212 constructor_designated = 0;
5213 designator_depth = 0;
5214 designator_errorneous = 0;
5216 if (TREE_CODE (constructor_type) == RECORD_TYPE
5217 || TREE_CODE (constructor_type) == UNION_TYPE)
5219 constructor_fields = TYPE_FIELDS (constructor_type);
5220 /* Skip any nameless bit fields at the beginning. */
5221 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5222 && DECL_NAME (constructor_fields) == 0)
5223 constructor_fields = TREE_CHAIN (constructor_fields);
5225 constructor_unfilled_fields = constructor_fields;
5226 constructor_bit_index = bitsize_zero_node;
5228 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5230 if (TYPE_DOMAIN (constructor_type))
5232 constructor_max_index
5233 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5235 /* Detect non-empty initializations of zero-length arrays. */
5236 if (constructor_max_index == NULL_TREE
5237 && TYPE_SIZE (constructor_type))
5238 constructor_max_index = build_int_2 (-1, -1);
5240 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5241 to initialize VLAs will cause a proper error; avoid tree
5242 checking errors as well by setting a safe value. */
5243 if (constructor_max_index
5244 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5245 constructor_max_index = build_int_2 (-1, -1);
5247 constructor_index
5248 = convert (bitsizetype,
5249 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5251 else
5252 constructor_index = bitsize_zero_node;
5254 constructor_unfilled_index = constructor_index;
5256 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5258 /* Vectors are like simple fixed-size arrays. */
5259 constructor_max_index =
5260 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5261 constructor_index = convert (bitsizetype, bitsize_zero_node);
5262 constructor_unfilled_index = constructor_index;
5264 else
5266 /* Handle the case of int x = {5}; */
5267 constructor_fields = constructor_type;
5268 constructor_unfilled_fields = constructor_type;
5272 /* Push down into a subobject, for initialization.
5273 If this is for an explicit set of braces, IMPLICIT is 0.
5274 If it is because the next element belongs at a lower level,
5275 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5277 void
5278 push_init_level (implicit)
5279 int implicit;
5281 struct constructor_stack *p;
5282 tree value = NULL_TREE;
5284 /* If we've exhausted any levels that didn't have braces,
5285 pop them now. */
5286 while (constructor_stack->implicit)
5288 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5289 || TREE_CODE (constructor_type) == UNION_TYPE)
5290 && constructor_fields == 0)
5291 process_init_element (pop_init_level (1));
5292 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5293 && constructor_max_index
5294 && tree_int_cst_lt (constructor_max_index, constructor_index))
5295 process_init_element (pop_init_level (1));
5296 else
5297 break;
5300 /* Unless this is an explicit brace, we need to preserve previous
5301 content if any. */
5302 if (implicit)
5304 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5305 || TREE_CODE (constructor_type) == UNION_TYPE)
5306 && constructor_fields)
5307 value = find_init_member (constructor_fields);
5308 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5309 value = find_init_member (constructor_index);
5312 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5313 p->type = constructor_type;
5314 p->fields = constructor_fields;
5315 p->index = constructor_index;
5316 p->max_index = constructor_max_index;
5317 p->unfilled_index = constructor_unfilled_index;
5318 p->unfilled_fields = constructor_unfilled_fields;
5319 p->bit_index = constructor_bit_index;
5320 p->elements = constructor_elements;
5321 p->constant = constructor_constant;
5322 p->simple = constructor_simple;
5323 p->erroneous = constructor_erroneous;
5324 p->pending_elts = constructor_pending_elts;
5325 p->depth = constructor_depth;
5326 p->replacement_value = 0;
5327 p->implicit = implicit;
5328 p->outer = 0;
5329 p->incremental = constructor_incremental;
5330 p->designated = constructor_designated;
5331 p->next = constructor_stack;
5332 p->range_stack = 0;
5333 constructor_stack = p;
5335 constructor_constant = 1;
5336 constructor_simple = 1;
5337 constructor_depth = SPELLING_DEPTH ();
5338 constructor_elements = 0;
5339 constructor_incremental = 1;
5340 constructor_designated = 0;
5341 constructor_pending_elts = 0;
5342 if (!implicit)
5344 p->range_stack = constructor_range_stack;
5345 constructor_range_stack = 0;
5346 designator_depth = 0;
5347 designator_errorneous = 0;
5350 /* Don't die if an entire brace-pair level is superfluous
5351 in the containing level. */
5352 if (constructor_type == 0)
5354 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5355 || TREE_CODE (constructor_type) == UNION_TYPE)
5357 /* Don't die if there are extra init elts at the end. */
5358 if (constructor_fields == 0)
5359 constructor_type = 0;
5360 else
5362 constructor_type = TREE_TYPE (constructor_fields);
5363 push_member_name (constructor_fields);
5364 constructor_depth++;
5367 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5369 constructor_type = TREE_TYPE (constructor_type);
5370 push_array_bounds (tree_low_cst (constructor_index, 0));
5371 constructor_depth++;
5374 if (constructor_type == 0)
5376 error_init ("extra brace group at end of initializer");
5377 constructor_fields = 0;
5378 constructor_unfilled_fields = 0;
5379 return;
5382 if (value && TREE_CODE (value) == CONSTRUCTOR)
5384 constructor_constant = TREE_CONSTANT (value);
5385 constructor_simple = TREE_STATIC (value);
5386 constructor_elements = TREE_OPERAND (value, 1);
5387 if (constructor_elements
5388 && (TREE_CODE (constructor_type) == RECORD_TYPE
5389 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5390 set_nonincremental_init ();
5393 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5395 missing_braces_mentioned = 1;
5396 warning_init ("missing braces around initializer");
5399 if (TREE_CODE (constructor_type) == RECORD_TYPE
5400 || TREE_CODE (constructor_type) == UNION_TYPE)
5402 constructor_fields = TYPE_FIELDS (constructor_type);
5403 /* Skip any nameless bit fields at the beginning. */
5404 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5405 && DECL_NAME (constructor_fields) == 0)
5406 constructor_fields = TREE_CHAIN (constructor_fields);
5408 constructor_unfilled_fields = constructor_fields;
5409 constructor_bit_index = bitsize_zero_node;
5411 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5413 /* Vectors are like simple fixed-size arrays. */
5414 constructor_max_index =
5415 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5416 constructor_index = convert (bitsizetype, integer_zero_node);
5417 constructor_unfilled_index = constructor_index;
5419 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5421 if (TYPE_DOMAIN (constructor_type))
5423 constructor_max_index
5424 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5426 /* Detect non-empty initializations of zero-length arrays. */
5427 if (constructor_max_index == NULL_TREE
5428 && TYPE_SIZE (constructor_type))
5429 constructor_max_index = build_int_2 (-1, -1);
5431 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5432 to initialize VLAs will cause a proper error; avoid tree
5433 checking errors as well by setting a safe value. */
5434 if (constructor_max_index
5435 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5436 constructor_max_index = build_int_2 (-1, -1);
5438 constructor_index
5439 = convert (bitsizetype,
5440 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5442 else
5443 constructor_index = bitsize_zero_node;
5445 constructor_unfilled_index = constructor_index;
5446 if (value && TREE_CODE (value) == STRING_CST)
5448 /* We need to split the char/wchar array into individual
5449 characters, so that we don't have to special case it
5450 everywhere. */
5451 set_nonincremental_init_from_string (value);
5454 else
5456 warning_init ("braces around scalar initializer");
5457 constructor_fields = constructor_type;
5458 constructor_unfilled_fields = constructor_type;
5462 /* At the end of an implicit or explicit brace level,
5463 finish up that level of constructor.
5464 If we were outputting the elements as they are read, return 0
5465 from inner levels (process_init_element ignores that),
5466 but return error_mark_node from the outermost level
5467 (that's what we want to put in DECL_INITIAL).
5468 Otherwise, return a CONSTRUCTOR expression. */
5470 tree
5471 pop_init_level (implicit)
5472 int implicit;
5474 struct constructor_stack *p;
5475 tree constructor = 0;
5477 if (implicit == 0)
5479 /* When we come to an explicit close brace,
5480 pop any inner levels that didn't have explicit braces. */
5481 while (constructor_stack->implicit)
5482 process_init_element (pop_init_level (1));
5484 if (constructor_range_stack)
5485 abort ();
5488 p = constructor_stack;
5490 /* Error for initializing a flexible array member, or a zero-length
5491 array member in an inappropriate context. */
5492 if (constructor_type && constructor_fields
5493 && TREE_CODE (constructor_type) == ARRAY_TYPE
5494 && TYPE_DOMAIN (constructor_type)
5495 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5497 /* Silently discard empty initializations. The parser will
5498 already have pedwarned for empty brackets. */
5499 if (integer_zerop (constructor_unfilled_index))
5500 constructor_type = NULL_TREE;
5501 else if (! TYPE_SIZE (constructor_type))
5503 if (constructor_depth > 2)
5504 error_init ("initialization of flexible array member in a nested context");
5505 else if (pedantic)
5506 pedwarn_init ("initialization of a flexible array member");
5508 /* We have already issued an error message for the existence
5509 of a flexible array member not at the end of the structure.
5510 Discard the initializer so that we do not abort later. */
5511 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5512 constructor_type = NULL_TREE;
5514 else
5515 /* Zero-length arrays are no longer special, so we should no longer
5516 get here. */
5517 abort ();
5520 /* Warn when some struct elements are implicitly initialized to zero. */
5521 if (extra_warnings
5522 && constructor_type
5523 && TREE_CODE (constructor_type) == RECORD_TYPE
5524 && constructor_unfilled_fields)
5526 /* Do not warn for flexible array members or zero-length arrays. */
5527 while (constructor_unfilled_fields
5528 && (! DECL_SIZE (constructor_unfilled_fields)
5529 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5530 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5532 /* Do not warn if this level of the initializer uses member
5533 designators; it is likely to be deliberate. */
5534 if (constructor_unfilled_fields && !constructor_designated)
5536 push_member_name (constructor_unfilled_fields);
5537 warning_init ("missing initializer");
5538 RESTORE_SPELLING_DEPTH (constructor_depth);
5542 /* Now output all pending elements. */
5543 constructor_incremental = 1;
5544 output_pending_init_elements (1);
5546 /* Pad out the end of the structure. */
5547 if (p->replacement_value)
5548 /* If this closes a superfluous brace pair,
5549 just pass out the element between them. */
5550 constructor = p->replacement_value;
5551 else if (constructor_type == 0)
5553 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5554 && TREE_CODE (constructor_type) != UNION_TYPE
5555 && TREE_CODE (constructor_type) != ARRAY_TYPE
5556 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5558 /* A nonincremental scalar initializer--just return
5559 the element, after verifying there is just one. */
5560 if (constructor_elements == 0)
5562 if (!constructor_erroneous)
5563 error_init ("empty scalar initializer");
5564 constructor = error_mark_node;
5566 else if (TREE_CHAIN (constructor_elements) != 0)
5568 error_init ("extra elements in scalar initializer");
5569 constructor = TREE_VALUE (constructor_elements);
5571 else
5572 constructor = TREE_VALUE (constructor_elements);
5574 else
5576 if (constructor_erroneous)
5577 constructor = error_mark_node;
5578 else
5580 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5581 nreverse (constructor_elements));
5582 if (constructor_constant)
5583 TREE_CONSTANT (constructor) = 1;
5584 if (constructor_constant && constructor_simple)
5585 TREE_STATIC (constructor) = 1;
5589 constructor_type = p->type;
5590 constructor_fields = p->fields;
5591 constructor_index = p->index;
5592 constructor_max_index = p->max_index;
5593 constructor_unfilled_index = p->unfilled_index;
5594 constructor_unfilled_fields = p->unfilled_fields;
5595 constructor_bit_index = p->bit_index;
5596 constructor_elements = p->elements;
5597 constructor_constant = p->constant;
5598 constructor_simple = p->simple;
5599 constructor_erroneous = p->erroneous;
5600 constructor_incremental = p->incremental;
5601 constructor_designated = p->designated;
5602 constructor_pending_elts = p->pending_elts;
5603 constructor_depth = p->depth;
5604 if (!p->implicit)
5605 constructor_range_stack = p->range_stack;
5606 RESTORE_SPELLING_DEPTH (constructor_depth);
5608 constructor_stack = p->next;
5609 free (p);
5611 if (constructor == 0)
5613 if (constructor_stack == 0)
5614 return error_mark_node;
5615 return NULL_TREE;
5617 return constructor;
5620 /* Common handling for both array range and field name designators.
5621 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5623 static int
5624 set_designator (array)
5625 int array;
5627 tree subtype;
5628 enum tree_code subcode;
5630 /* Don't die if an entire brace-pair level is superfluous
5631 in the containing level. */
5632 if (constructor_type == 0)
5633 return 1;
5635 /* If there were errors in this designator list already, bail out silently. */
5636 if (designator_errorneous)
5637 return 1;
5639 if (!designator_depth)
5641 if (constructor_range_stack)
5642 abort ();
5644 /* Designator list starts at the level of closest explicit
5645 braces. */
5646 while (constructor_stack->implicit)
5647 process_init_element (pop_init_level (1));
5648 constructor_designated = 1;
5649 return 0;
5652 if (constructor_no_implicit)
5654 error_init ("initialization designators may not nest");
5655 return 1;
5658 if (TREE_CODE (constructor_type) == RECORD_TYPE
5659 || TREE_CODE (constructor_type) == UNION_TYPE)
5661 subtype = TREE_TYPE (constructor_fields);
5662 if (subtype != error_mark_node)
5663 subtype = TYPE_MAIN_VARIANT (subtype);
5665 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5667 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5669 else
5670 abort ();
5672 subcode = TREE_CODE (subtype);
5673 if (array && subcode != ARRAY_TYPE)
5675 error_init ("array index in non-array initializer");
5676 return 1;
5678 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5680 error_init ("field name not in record or union initializer");
5681 return 1;
5684 constructor_designated = 1;
5685 push_init_level (2);
5686 return 0;
5689 /* If there are range designators in designator list, push a new designator
5690 to constructor_range_stack. RANGE_END is end of such stack range or
5691 NULL_TREE if there is no range designator at this level. */
5693 static void
5694 push_range_stack (range_end)
5695 tree range_end;
5697 struct constructor_range_stack *p;
5699 p = (struct constructor_range_stack *)
5700 ggc_alloc (sizeof (struct constructor_range_stack));
5701 p->prev = constructor_range_stack;
5702 p->next = 0;
5703 p->fields = constructor_fields;
5704 p->range_start = constructor_index;
5705 p->index = constructor_index;
5706 p->stack = constructor_stack;
5707 p->range_end = range_end;
5708 if (constructor_range_stack)
5709 constructor_range_stack->next = p;
5710 constructor_range_stack = p;
5713 /* Within an array initializer, specify the next index to be initialized.
5714 FIRST is that index. If LAST is nonzero, then initialize a range
5715 of indices, running from FIRST through LAST. */
5717 void
5718 set_init_index (first, last)
5719 tree first, last;
5721 if (set_designator (1))
5722 return;
5724 designator_errorneous = 1;
5726 while ((TREE_CODE (first) == NOP_EXPR
5727 || TREE_CODE (first) == CONVERT_EXPR
5728 || TREE_CODE (first) == NON_LVALUE_EXPR)
5729 && (TYPE_MODE (TREE_TYPE (first))
5730 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5731 first = TREE_OPERAND (first, 0);
5733 if (last)
5734 while ((TREE_CODE (last) == NOP_EXPR
5735 || TREE_CODE (last) == CONVERT_EXPR
5736 || TREE_CODE (last) == NON_LVALUE_EXPR)
5737 && (TYPE_MODE (TREE_TYPE (last))
5738 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5739 last = TREE_OPERAND (last, 0);
5741 if (TREE_CODE (first) != INTEGER_CST)
5742 error_init ("nonconstant array index in initializer");
5743 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5744 error_init ("nonconstant array index in initializer");
5745 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5746 error_init ("array index in non-array initializer");
5747 else if (constructor_max_index
5748 && tree_int_cst_lt (constructor_max_index, first))
5749 error_init ("array index in initializer exceeds array bounds");
5750 else
5752 constructor_index = convert (bitsizetype, first);
5754 if (last)
5756 if (tree_int_cst_equal (first, last))
5757 last = 0;
5758 else if (tree_int_cst_lt (last, first))
5760 error_init ("empty index range in initializer");
5761 last = 0;
5763 else
5765 last = convert (bitsizetype, last);
5766 if (constructor_max_index != 0
5767 && tree_int_cst_lt (constructor_max_index, last))
5769 error_init ("array index range in initializer exceeds array bounds");
5770 last = 0;
5775 designator_depth++;
5776 designator_errorneous = 0;
5777 if (constructor_range_stack || last)
5778 push_range_stack (last);
5782 /* Within a struct initializer, specify the next field to be initialized. */
5784 void
5785 set_init_label (fieldname)
5786 tree fieldname;
5788 tree tail;
5790 if (set_designator (0))
5791 return;
5793 designator_errorneous = 1;
5795 if (TREE_CODE (constructor_type) != RECORD_TYPE
5796 && TREE_CODE (constructor_type) != UNION_TYPE)
5798 error_init ("field name not in record or union initializer");
5799 return;
5802 for (tail = TYPE_FIELDS (constructor_type); tail;
5803 tail = TREE_CHAIN (tail))
5805 if (DECL_NAME (tail) == fieldname)
5806 break;
5809 if (tail == 0)
5810 error ("unknown field `%s' specified in initializer",
5811 IDENTIFIER_POINTER (fieldname));
5812 else
5814 constructor_fields = tail;
5815 designator_depth++;
5816 designator_errorneous = 0;
5817 if (constructor_range_stack)
5818 push_range_stack (NULL_TREE);
5822 /* Add a new initializer to the tree of pending initializers. PURPOSE
5823 identifies the initializer, either array index or field in a structure.
5824 VALUE is the value of that index or field. */
5826 static void
5827 add_pending_init (purpose, value)
5828 tree purpose, value;
5830 struct init_node *p, **q, *r;
5832 q = &constructor_pending_elts;
5833 p = 0;
5835 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5837 while (*q != 0)
5839 p = *q;
5840 if (tree_int_cst_lt (purpose, p->purpose))
5841 q = &p->left;
5842 else if (tree_int_cst_lt (p->purpose, purpose))
5843 q = &p->right;
5844 else
5846 if (TREE_SIDE_EFFECTS (p->value))
5847 warning_init ("initialized field with side-effects overwritten");
5848 p->value = value;
5849 return;
5853 else
5855 tree bitpos;
5857 bitpos = bit_position (purpose);
5858 while (*q != NULL)
5860 p = *q;
5861 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5862 q = &p->left;
5863 else if (p->purpose != purpose)
5864 q = &p->right;
5865 else
5867 if (TREE_SIDE_EFFECTS (p->value))
5868 warning_init ("initialized field with side-effects overwritten");
5869 p->value = value;
5870 return;
5875 r = (struct init_node *) ggc_alloc (sizeof (struct init_node));
5876 r->purpose = purpose;
5877 r->value = value;
5879 *q = r;
5880 r->parent = p;
5881 r->left = 0;
5882 r->right = 0;
5883 r->balance = 0;
5885 while (p)
5887 struct init_node *s;
5889 if (r == p->left)
5891 if (p->balance == 0)
5892 p->balance = -1;
5893 else if (p->balance < 0)
5895 if (r->balance < 0)
5897 /* L rotation. */
5898 p->left = r->right;
5899 if (p->left)
5900 p->left->parent = p;
5901 r->right = p;
5903 p->balance = 0;
5904 r->balance = 0;
5906 s = p->parent;
5907 p->parent = r;
5908 r->parent = s;
5909 if (s)
5911 if (s->left == p)
5912 s->left = r;
5913 else
5914 s->right = r;
5916 else
5917 constructor_pending_elts = r;
5919 else
5921 /* LR rotation. */
5922 struct init_node *t = r->right;
5924 r->right = t->left;
5925 if (r->right)
5926 r->right->parent = r;
5927 t->left = r;
5929 p->left = t->right;
5930 if (p->left)
5931 p->left->parent = p;
5932 t->right = p;
5934 p->balance = t->balance < 0;
5935 r->balance = -(t->balance > 0);
5936 t->balance = 0;
5938 s = p->parent;
5939 p->parent = t;
5940 r->parent = t;
5941 t->parent = s;
5942 if (s)
5944 if (s->left == p)
5945 s->left = t;
5946 else
5947 s->right = t;
5949 else
5950 constructor_pending_elts = t;
5952 break;
5954 else
5956 /* p->balance == +1; growth of left side balances the node. */
5957 p->balance = 0;
5958 break;
5961 else /* r == p->right */
5963 if (p->balance == 0)
5964 /* Growth propagation from right side. */
5965 p->balance++;
5966 else if (p->balance > 0)
5968 if (r->balance > 0)
5970 /* R rotation. */
5971 p->right = r->left;
5972 if (p->right)
5973 p->right->parent = p;
5974 r->left = p;
5976 p->balance = 0;
5977 r->balance = 0;
5979 s = p->parent;
5980 p->parent = r;
5981 r->parent = s;
5982 if (s)
5984 if (s->left == p)
5985 s->left = r;
5986 else
5987 s->right = r;
5989 else
5990 constructor_pending_elts = r;
5992 else /* r->balance == -1 */
5994 /* RL rotation */
5995 struct init_node *t = r->left;
5997 r->left = t->right;
5998 if (r->left)
5999 r->left->parent = r;
6000 t->right = r;
6002 p->right = t->left;
6003 if (p->right)
6004 p->right->parent = p;
6005 t->left = p;
6007 r->balance = (t->balance < 0);
6008 p->balance = -(t->balance > 0);
6009 t->balance = 0;
6011 s = p->parent;
6012 p->parent = t;
6013 r->parent = t;
6014 t->parent = s;
6015 if (s)
6017 if (s->left == p)
6018 s->left = t;
6019 else
6020 s->right = t;
6022 else
6023 constructor_pending_elts = t;
6025 break;
6027 else
6029 /* p->balance == -1; growth of right side balances the node. */
6030 p->balance = 0;
6031 break;
6035 r = p;
6036 p = p->parent;
6040 /* Build AVL tree from a sorted chain. */
6042 static void
6043 set_nonincremental_init ()
6045 tree chain;
6047 if (TREE_CODE (constructor_type) != RECORD_TYPE
6048 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6049 return;
6051 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
6052 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
6053 constructor_elements = 0;
6054 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6056 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6057 /* Skip any nameless bit fields at the beginning. */
6058 while (constructor_unfilled_fields != 0
6059 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6060 && DECL_NAME (constructor_unfilled_fields) == 0)
6061 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6064 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6066 if (TYPE_DOMAIN (constructor_type))
6067 constructor_unfilled_index
6068 = convert (bitsizetype,
6069 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6070 else
6071 constructor_unfilled_index = bitsize_zero_node;
6073 constructor_incremental = 0;
6076 /* Build AVL tree from a string constant. */
6078 static void
6079 set_nonincremental_init_from_string (str)
6080 tree str;
6082 tree value, purpose, type;
6083 HOST_WIDE_INT val[2];
6084 const char *p, *end;
6085 int byte, wchar_bytes, charwidth, bitpos;
6087 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6088 abort ();
6090 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6091 == TYPE_PRECISION (char_type_node))
6092 wchar_bytes = 1;
6093 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6094 == TYPE_PRECISION (wchar_type_node))
6095 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6096 else
6097 abort ();
6099 charwidth = TYPE_PRECISION (char_type_node);
6100 type = TREE_TYPE (constructor_type);
6101 p = TREE_STRING_POINTER (str);
6102 end = p + TREE_STRING_LENGTH (str);
6104 for (purpose = bitsize_zero_node;
6105 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6106 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6108 if (wchar_bytes == 1)
6110 val[1] = (unsigned char) *p++;
6111 val[0] = 0;
6113 else
6115 val[0] = 0;
6116 val[1] = 0;
6117 for (byte = 0; byte < wchar_bytes; byte++)
6119 if (BYTES_BIG_ENDIAN)
6120 bitpos = (wchar_bytes - byte - 1) * charwidth;
6121 else
6122 bitpos = byte * charwidth;
6123 val[bitpos < HOST_BITS_PER_WIDE_INT]
6124 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6125 << (bitpos % HOST_BITS_PER_WIDE_INT);
6129 if (!TREE_UNSIGNED (type))
6131 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6132 if (bitpos < HOST_BITS_PER_WIDE_INT)
6134 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6136 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6137 val[0] = -1;
6140 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6142 if (val[1] < 0)
6143 val[0] = -1;
6145 else if (val[0] & (((HOST_WIDE_INT) 1)
6146 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6147 val[0] |= ((HOST_WIDE_INT) -1)
6148 << (bitpos - HOST_BITS_PER_WIDE_INT);
6151 value = build_int_2 (val[1], val[0]);
6152 TREE_TYPE (value) = type;
6153 add_pending_init (purpose, value);
6156 constructor_incremental = 0;
6159 /* Return value of FIELD in pending initializer or zero if the field was
6160 not initialized yet. */
6162 static tree
6163 find_init_member (field)
6164 tree field;
6166 struct init_node *p;
6168 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6170 if (constructor_incremental
6171 && tree_int_cst_lt (field, constructor_unfilled_index))
6172 set_nonincremental_init ();
6174 p = constructor_pending_elts;
6175 while (p)
6177 if (tree_int_cst_lt (field, p->purpose))
6178 p = p->left;
6179 else if (tree_int_cst_lt (p->purpose, field))
6180 p = p->right;
6181 else
6182 return p->value;
6185 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6187 tree bitpos = bit_position (field);
6189 if (constructor_incremental
6190 && (!constructor_unfilled_fields
6191 || tree_int_cst_lt (bitpos,
6192 bit_position (constructor_unfilled_fields))))
6193 set_nonincremental_init ();
6195 p = constructor_pending_elts;
6196 while (p)
6198 if (field == p->purpose)
6199 return p->value;
6200 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6201 p = p->left;
6202 else
6203 p = p->right;
6206 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6208 if (constructor_elements
6209 && TREE_PURPOSE (constructor_elements) == field)
6210 return TREE_VALUE (constructor_elements);
6212 return 0;
6215 /* "Output" the next constructor element.
6216 At top level, really output it to assembler code now.
6217 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6218 TYPE is the data type that the containing data type wants here.
6219 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6221 PENDING if non-nil means output pending elements that belong
6222 right after this element. (PENDING is normally 1;
6223 it is 0 while outputting pending elements, to avoid recursion.) */
6225 static void
6226 output_init_element (value, type, field, pending)
6227 tree value, type, field;
6228 int pending;
6230 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6231 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6232 && !(TREE_CODE (value) == STRING_CST
6233 && TREE_CODE (type) == ARRAY_TYPE
6234 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6235 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6236 TYPE_MAIN_VARIANT (type))))
6237 value = default_conversion (value);
6239 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6240 && require_constant_value && !flag_isoc99 && pending)
6242 /* As an extension, allow initializing objects with static storage
6243 duration with compound literals (which are then treated just as
6244 the brace enclosed list they contain). */
6245 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6246 value = DECL_INITIAL (decl);
6249 if (value == error_mark_node)
6250 constructor_erroneous = 1;
6251 else if (!TREE_CONSTANT (value))
6252 constructor_constant = 0;
6253 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6254 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6255 || TREE_CODE (constructor_type) == UNION_TYPE)
6256 && DECL_C_BIT_FIELD (field)
6257 && TREE_CODE (value) != INTEGER_CST))
6258 constructor_simple = 0;
6260 if (require_constant_value && ! TREE_CONSTANT (value))
6262 error_init ("initializer element is not constant");
6263 value = error_mark_node;
6265 else if (require_constant_elements
6266 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6267 pedwarn ("initializer element is not computable at load time");
6269 /* If this field is empty (and not at the end of structure),
6270 don't do anything other than checking the initializer. */
6271 if (field
6272 && (TREE_TYPE (field) == error_mark_node
6273 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6274 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6275 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6276 || TREE_CHAIN (field)))))
6277 return;
6279 value = digest_init (type, value, require_constant_value);
6280 if (value == error_mark_node)
6282 constructor_erroneous = 1;
6283 return;
6286 /* If this element doesn't come next in sequence,
6287 put it on constructor_pending_elts. */
6288 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6289 && (!constructor_incremental
6290 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6292 if (constructor_incremental
6293 && tree_int_cst_lt (field, constructor_unfilled_index))
6294 set_nonincremental_init ();
6296 add_pending_init (field, value);
6297 return;
6299 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6300 && (!constructor_incremental
6301 || field != constructor_unfilled_fields))
6303 /* We do this for records but not for unions. In a union,
6304 no matter which field is specified, it can be initialized
6305 right away since it starts at the beginning of the union. */
6306 if (constructor_incremental)
6308 if (!constructor_unfilled_fields)
6309 set_nonincremental_init ();
6310 else
6312 tree bitpos, unfillpos;
6314 bitpos = bit_position (field);
6315 unfillpos = bit_position (constructor_unfilled_fields);
6317 if (tree_int_cst_lt (bitpos, unfillpos))
6318 set_nonincremental_init ();
6322 add_pending_init (field, value);
6323 return;
6325 else if (TREE_CODE (constructor_type) == UNION_TYPE
6326 && constructor_elements)
6328 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
6329 warning_init ("initialized field with side-effects overwritten");
6331 /* We can have just one union field set. */
6332 constructor_elements = 0;
6335 /* Otherwise, output this element either to
6336 constructor_elements or to the assembler file. */
6338 if (field && TREE_CODE (field) == INTEGER_CST)
6339 field = copy_node (field);
6340 constructor_elements
6341 = tree_cons (field, value, constructor_elements);
6343 /* Advance the variable that indicates sequential elements output. */
6344 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6345 constructor_unfilled_index
6346 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6347 bitsize_one_node);
6348 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6350 constructor_unfilled_fields
6351 = TREE_CHAIN (constructor_unfilled_fields);
6353 /* Skip any nameless bit fields. */
6354 while (constructor_unfilled_fields != 0
6355 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6356 && DECL_NAME (constructor_unfilled_fields) == 0)
6357 constructor_unfilled_fields =
6358 TREE_CHAIN (constructor_unfilled_fields);
6360 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6361 constructor_unfilled_fields = 0;
6363 /* Now output any pending elements which have become next. */
6364 if (pending)
6365 output_pending_init_elements (0);
6368 /* Output any pending elements which have become next.
6369 As we output elements, constructor_unfilled_{fields,index}
6370 advances, which may cause other elements to become next;
6371 if so, they too are output.
6373 If ALL is 0, we return when there are
6374 no more pending elements to output now.
6376 If ALL is 1, we output space as necessary so that
6377 we can output all the pending elements. */
6379 static void
6380 output_pending_init_elements (all)
6381 int all;
6383 struct init_node *elt = constructor_pending_elts;
6384 tree next;
6386 retry:
6388 /* Look thru the whole pending tree.
6389 If we find an element that should be output now,
6390 output it. Otherwise, set NEXT to the element
6391 that comes first among those still pending. */
6393 next = 0;
6394 while (elt)
6396 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6398 if (tree_int_cst_equal (elt->purpose,
6399 constructor_unfilled_index))
6400 output_init_element (elt->value,
6401 TREE_TYPE (constructor_type),
6402 constructor_unfilled_index, 0);
6403 else if (tree_int_cst_lt (constructor_unfilled_index,
6404 elt->purpose))
6406 /* Advance to the next smaller node. */
6407 if (elt->left)
6408 elt = elt->left;
6409 else
6411 /* We have reached the smallest node bigger than the
6412 current unfilled index. Fill the space first. */
6413 next = elt->purpose;
6414 break;
6417 else
6419 /* Advance to the next bigger node. */
6420 if (elt->right)
6421 elt = elt->right;
6422 else
6424 /* We have reached the biggest node in a subtree. Find
6425 the parent of it, which is the next bigger node. */
6426 while (elt->parent && elt->parent->right == elt)
6427 elt = elt->parent;
6428 elt = elt->parent;
6429 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6430 elt->purpose))
6432 next = elt->purpose;
6433 break;
6438 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6439 || TREE_CODE (constructor_type) == UNION_TYPE)
6441 tree ctor_unfilled_bitpos, elt_bitpos;
6443 /* If the current record is complete we are done. */
6444 if (constructor_unfilled_fields == 0)
6445 break;
6447 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6448 elt_bitpos = bit_position (elt->purpose);
6449 /* We can't compare fields here because there might be empty
6450 fields in between. */
6451 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6453 constructor_unfilled_fields = elt->purpose;
6454 output_init_element (elt->value, TREE_TYPE (elt->purpose),
6455 elt->purpose, 0);
6457 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6459 /* Advance to the next smaller node. */
6460 if (elt->left)
6461 elt = elt->left;
6462 else
6464 /* We have reached the smallest node bigger than the
6465 current unfilled field. Fill the space first. */
6466 next = elt->purpose;
6467 break;
6470 else
6472 /* Advance to the next bigger node. */
6473 if (elt->right)
6474 elt = elt->right;
6475 else
6477 /* We have reached the biggest node in a subtree. Find
6478 the parent of it, which is the next bigger node. */
6479 while (elt->parent && elt->parent->right == elt)
6480 elt = elt->parent;
6481 elt = elt->parent;
6482 if (elt
6483 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6484 bit_position (elt->purpose))))
6486 next = elt->purpose;
6487 break;
6494 /* Ordinarily return, but not if we want to output all
6495 and there are elements left. */
6496 if (! (all && next != 0))
6497 return;
6499 /* If it's not incremental, just skip over the gap, so that after
6500 jumping to retry we will output the next successive element. */
6501 if (TREE_CODE (constructor_type) == RECORD_TYPE
6502 || TREE_CODE (constructor_type) == UNION_TYPE)
6503 constructor_unfilled_fields = next;
6504 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6505 constructor_unfilled_index = next;
6507 /* ELT now points to the node in the pending tree with the next
6508 initializer to output. */
6509 goto retry;
6512 /* Add one non-braced element to the current constructor level.
6513 This adjusts the current position within the constructor's type.
6514 This may also start or terminate implicit levels
6515 to handle a partly-braced initializer.
6517 Once this has found the correct level for the new element,
6518 it calls output_init_element. */
6520 void
6521 process_init_element (value)
6522 tree value;
6524 tree orig_value = value;
6525 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6527 designator_depth = 0;
6528 designator_errorneous = 0;
6530 /* Handle superfluous braces around string cst as in
6531 char x[] = {"foo"}; */
6532 if (string_flag
6533 && constructor_type
6534 && TREE_CODE (constructor_type) == ARRAY_TYPE
6535 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6536 && integer_zerop (constructor_unfilled_index))
6538 if (constructor_stack->replacement_value)
6539 error_init ("excess elements in char array initializer");
6540 constructor_stack->replacement_value = value;
6541 return;
6544 if (constructor_stack->replacement_value != 0)
6546 error_init ("excess elements in struct initializer");
6547 return;
6550 /* Ignore elements of a brace group if it is entirely superfluous
6551 and has already been diagnosed. */
6552 if (constructor_type == 0)
6553 return;
6555 /* If we've exhausted any levels that didn't have braces,
6556 pop them now. */
6557 while (constructor_stack->implicit)
6559 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6560 || TREE_CODE (constructor_type) == UNION_TYPE)
6561 && constructor_fields == 0)
6562 process_init_element (pop_init_level (1));
6563 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6564 && (constructor_max_index == 0
6565 || tree_int_cst_lt (constructor_max_index,
6566 constructor_index)))
6567 process_init_element (pop_init_level (1));
6568 else
6569 break;
6572 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6573 if (constructor_range_stack)
6575 /* If value is a compound literal and we'll be just using its
6576 content, don't put it into a SAVE_EXPR. */
6577 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
6578 || !require_constant_value
6579 || flag_isoc99)
6580 value = save_expr (value);
6583 while (1)
6585 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6587 tree fieldtype;
6588 enum tree_code fieldcode;
6590 if (constructor_fields == 0)
6592 pedwarn_init ("excess elements in struct initializer");
6593 break;
6596 fieldtype = TREE_TYPE (constructor_fields);
6597 if (fieldtype != error_mark_node)
6598 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6599 fieldcode = TREE_CODE (fieldtype);
6601 /* Error for non-static initialization of a flexible array member. */
6602 if (fieldcode == ARRAY_TYPE
6603 && !require_constant_value
6604 && TYPE_SIZE (fieldtype) == NULL_TREE
6605 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6607 error_init ("non-static initialization of a flexible array member");
6608 break;
6611 /* Accept a string constant to initialize a subarray. */
6612 if (value != 0
6613 && fieldcode == ARRAY_TYPE
6614 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6615 && string_flag)
6616 value = orig_value;
6617 /* Otherwise, if we have come to a subaggregate,
6618 and we don't have an element of its type, push into it. */
6619 else if (value != 0 && !constructor_no_implicit
6620 && value != error_mark_node
6621 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6622 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6623 || fieldcode == UNION_TYPE))
6625 push_init_level (1);
6626 continue;
6629 if (value)
6631 push_member_name (constructor_fields);
6632 output_init_element (value, fieldtype, constructor_fields, 1);
6633 RESTORE_SPELLING_DEPTH (constructor_depth);
6635 else
6636 /* Do the bookkeeping for an element that was
6637 directly output as a constructor. */
6639 /* For a record, keep track of end position of last field. */
6640 if (DECL_SIZE (constructor_fields))
6641 constructor_bit_index
6642 = size_binop (PLUS_EXPR,
6643 bit_position (constructor_fields),
6644 DECL_SIZE (constructor_fields));
6646 /* If the current field was the first one not yet written out,
6647 it isn't now, so update. */
6648 if (constructor_unfilled_fields == constructor_fields)
6650 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6651 /* Skip any nameless bit fields. */
6652 while (constructor_unfilled_fields != 0
6653 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6654 && DECL_NAME (constructor_unfilled_fields) == 0)
6655 constructor_unfilled_fields =
6656 TREE_CHAIN (constructor_unfilled_fields);
6660 constructor_fields = TREE_CHAIN (constructor_fields);
6661 /* Skip any nameless bit fields at the beginning. */
6662 while (constructor_fields != 0
6663 && DECL_C_BIT_FIELD (constructor_fields)
6664 && DECL_NAME (constructor_fields) == 0)
6665 constructor_fields = TREE_CHAIN (constructor_fields);
6667 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6669 tree fieldtype;
6670 enum tree_code fieldcode;
6672 if (constructor_fields == 0)
6674 pedwarn_init ("excess elements in union initializer");
6675 break;
6678 fieldtype = TREE_TYPE (constructor_fields);
6679 if (fieldtype != error_mark_node)
6680 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6681 fieldcode = TREE_CODE (fieldtype);
6683 /* Warn that traditional C rejects initialization of unions.
6684 We skip the warning if the value is zero. This is done
6685 under the assumption that the zero initializer in user
6686 code appears conditioned on e.g. __STDC__ to avoid
6687 "missing initializer" warnings and relies on default
6688 initialization to zero in the traditional C case.
6689 We also skip the warning if the initializer is designated,
6690 again on the assumption that this must be conditional on
6691 __STDC__ anyway (and we've already complained about the
6692 member-designator already). */
6693 if (warn_traditional && !in_system_header && !constructor_designated
6694 && !(value && (integer_zerop (value) || real_zerop (value))))
6695 warning ("traditional C rejects initialization of unions");
6697 /* Accept a string constant to initialize a subarray. */
6698 if (value != 0
6699 && fieldcode == ARRAY_TYPE
6700 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6701 && string_flag)
6702 value = orig_value;
6703 /* Otherwise, if we have come to a subaggregate,
6704 and we don't have an element of its type, push into it. */
6705 else if (value != 0 && !constructor_no_implicit
6706 && value != error_mark_node
6707 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6708 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6709 || fieldcode == UNION_TYPE))
6711 push_init_level (1);
6712 continue;
6715 if (value)
6717 push_member_name (constructor_fields);
6718 output_init_element (value, fieldtype, constructor_fields, 1);
6719 RESTORE_SPELLING_DEPTH (constructor_depth);
6721 else
6722 /* Do the bookkeeping for an element that was
6723 directly output as a constructor. */
6725 constructor_bit_index = DECL_SIZE (constructor_fields);
6726 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6729 constructor_fields = 0;
6731 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6733 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6734 enum tree_code eltcode = TREE_CODE (elttype);
6736 /* Accept a string constant to initialize a subarray. */
6737 if (value != 0
6738 && eltcode == ARRAY_TYPE
6739 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6740 && string_flag)
6741 value = orig_value;
6742 /* Otherwise, if we have come to a subaggregate,
6743 and we don't have an element of its type, push into it. */
6744 else if (value != 0 && !constructor_no_implicit
6745 && value != error_mark_node
6746 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6747 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6748 || eltcode == UNION_TYPE))
6750 push_init_level (1);
6751 continue;
6754 if (constructor_max_index != 0
6755 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6756 || integer_all_onesp (constructor_max_index)))
6758 pedwarn_init ("excess elements in array initializer");
6759 break;
6762 /* Now output the actual element. */
6763 if (value)
6765 push_array_bounds (tree_low_cst (constructor_index, 0));
6766 output_init_element (value, elttype, constructor_index, 1);
6767 RESTORE_SPELLING_DEPTH (constructor_depth);
6770 constructor_index
6771 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6773 if (! value)
6774 /* If we are doing the bookkeeping for an element that was
6775 directly output as a constructor, we must update
6776 constructor_unfilled_index. */
6777 constructor_unfilled_index = constructor_index;
6779 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6781 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6783 /* Do a basic check of initializer size. Note that vectors
6784 always have a fixed size derived from their type. */
6785 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6787 pedwarn_init ("excess elements in vector initializer");
6788 break;
6791 /* Now output the actual element. */
6792 if (value)
6793 output_init_element (value, elttype, constructor_index, 1);
6795 constructor_index
6796 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6798 if (! value)
6799 /* If we are doing the bookkeeping for an element that was
6800 directly output as a constructor, we must update
6801 constructor_unfilled_index. */
6802 constructor_unfilled_index = constructor_index;
6805 /* Handle the sole element allowed in a braced initializer
6806 for a scalar variable. */
6807 else if (constructor_fields == 0)
6809 pedwarn_init ("excess elements in scalar initializer");
6810 break;
6812 else
6814 if (value)
6815 output_init_element (value, constructor_type, NULL_TREE, 1);
6816 constructor_fields = 0;
6819 /* Handle range initializers either at this level or anywhere higher
6820 in the designator stack. */
6821 if (constructor_range_stack)
6823 struct constructor_range_stack *p, *range_stack;
6824 int finish = 0;
6826 range_stack = constructor_range_stack;
6827 constructor_range_stack = 0;
6828 while (constructor_stack != range_stack->stack)
6830 if (!constructor_stack->implicit)
6831 abort ();
6832 process_init_element (pop_init_level (1));
6834 for (p = range_stack;
6835 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6836 p = p->prev)
6838 if (!constructor_stack->implicit)
6839 abort ();
6840 process_init_element (pop_init_level (1));
6843 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6844 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6845 finish = 1;
6847 while (1)
6849 constructor_index = p->index;
6850 constructor_fields = p->fields;
6851 if (finish && p->range_end && p->index == p->range_start)
6853 finish = 0;
6854 p->prev = 0;
6856 p = p->next;
6857 if (!p)
6858 break;
6859 push_init_level (2);
6860 p->stack = constructor_stack;
6861 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6862 p->index = p->range_start;
6865 if (!finish)
6866 constructor_range_stack = range_stack;
6867 continue;
6870 break;
6873 constructor_range_stack = 0;
6876 /* Build a simple asm-statement, from one string literal. */
6877 tree
6878 simple_asm_stmt (expr)
6879 tree expr;
6881 STRIP_NOPS (expr);
6883 if (TREE_CODE (expr) == ADDR_EXPR)
6884 expr = TREE_OPERAND (expr, 0);
6886 if (TREE_CODE (expr) == STRING_CST)
6888 tree stmt;
6890 /* Simple asm statements are treated as volatile. */
6891 stmt = add_stmt (build_stmt (ASM_STMT, ridpointers[(int) RID_VOLATILE],
6892 expr, NULL_TREE, NULL_TREE, NULL_TREE));
6893 ASM_INPUT_P (stmt) = 1;
6894 return stmt;
6897 error ("argument of `asm' is not a constant string");
6898 return NULL_TREE;
6901 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6902 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6904 tree
6905 build_asm_stmt (cv_qualifier, string, outputs, inputs, clobbers)
6906 tree cv_qualifier;
6907 tree string;
6908 tree outputs;
6909 tree inputs;
6910 tree clobbers;
6912 tree tail;
6914 if (TREE_CODE (string) != STRING_CST)
6916 error ("asm template is not a string constant");
6917 return NULL_TREE;
6920 if (cv_qualifier != NULL_TREE
6921 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6923 warning ("%s qualifier ignored on asm",
6924 IDENTIFIER_POINTER (cv_qualifier));
6925 cv_qualifier = NULL_TREE;
6928 /* We can remove output conversions that change the type,
6929 but not the mode. */
6930 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6932 tree output = TREE_VALUE (tail);
6934 STRIP_NOPS (output);
6935 TREE_VALUE (tail) = output;
6937 /* Allow conversions as LHS here. build_modify_expr as called below
6938 will do the right thing with them. */
6939 while (TREE_CODE (output) == NOP_EXPR
6940 || TREE_CODE (output) == CONVERT_EXPR
6941 || TREE_CODE (output) == FLOAT_EXPR
6942 || TREE_CODE (output) == FIX_TRUNC_EXPR
6943 || TREE_CODE (output) == FIX_FLOOR_EXPR
6944 || TREE_CODE (output) == FIX_ROUND_EXPR
6945 || TREE_CODE (output) == FIX_CEIL_EXPR)
6946 output = TREE_OPERAND (output, 0);
6948 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6951 /* Remove output conversions that change the type but not the mode. */
6952 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6954 tree output = TREE_VALUE (tail);
6955 STRIP_NOPS (output);
6956 TREE_VALUE (tail) = output;
6959 /* Perform default conversions on array and function inputs.
6960 Don't do this for other types as it would screw up operands
6961 expected to be in memory. */
6962 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6963 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6965 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6966 outputs, inputs, clobbers));
6969 /* Expand an ASM statement with operands, handling output operands
6970 that are not variables or INDIRECT_REFS by transforming such
6971 cases into cases that expand_asm_operands can handle.
6973 Arguments are same as for expand_asm_operands. */
6975 void
6976 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6977 tree string, outputs, inputs, clobbers;
6978 int vol;
6979 const char *filename;
6980 int line;
6982 int noutputs = list_length (outputs);
6983 int i;
6984 /* o[I] is the place that output number I should be written. */
6985 tree *o = (tree *) alloca (noutputs * sizeof (tree));
6986 tree tail;
6988 /* Record the contents of OUTPUTS before it is modified. */
6989 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6991 o[i] = TREE_VALUE (tail);
6992 if (o[i] == error_mark_node)
6993 return;
6996 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6997 OUTPUTS some trees for where the values were actually stored. */
6998 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
7000 /* Copy all the intermediate outputs into the specified outputs. */
7001 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7003 if (o[i] != TREE_VALUE (tail))
7005 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
7006 NULL_RTX, VOIDmode, EXPAND_NORMAL);
7007 free_temp_slots ();
7009 /* Restore the original value so that it's correct the next
7010 time we expand this function. */
7011 TREE_VALUE (tail) = o[i];
7013 /* Detect modification of read-only values.
7014 (Otherwise done by build_modify_expr.) */
7015 else
7017 tree type = TREE_TYPE (o[i]);
7018 if (TREE_READONLY (o[i])
7019 || TYPE_READONLY (type)
7020 || ((TREE_CODE (type) == RECORD_TYPE
7021 || TREE_CODE (type) == UNION_TYPE)
7022 && C_TYPE_FIELDS_READONLY (type)))
7023 readonly_warning (o[i], "modification by `asm'");
7027 /* Those MODIFY_EXPRs could do autoincrements. */
7028 emit_queue ();
7031 /* Expand a C `return' statement.
7032 RETVAL is the expression for what to return,
7033 or a null pointer for `return;' with no value. */
7035 tree
7036 c_expand_return (retval)
7037 tree retval;
7039 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
7041 if (TREE_THIS_VOLATILE (current_function_decl))
7042 warning ("function declared `noreturn' has a `return' statement");
7044 if (!retval)
7046 current_function_returns_null = 1;
7047 if ((warn_return_type || flag_isoc99)
7048 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7049 pedwarn_c99 ("`return' with no value, in function returning non-void");
7051 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7053 current_function_returns_null = 1;
7054 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7055 pedwarn ("`return' with a value, in function returning void");
7057 else
7059 tree t = convert_for_assignment (valtype, retval, _("return"),
7060 NULL_TREE, NULL_TREE, 0);
7061 tree res = DECL_RESULT (current_function_decl);
7062 tree inner;
7064 current_function_returns_value = 1;
7065 if (t == error_mark_node)
7066 return NULL_TREE;
7068 inner = t = convert (TREE_TYPE (res), t);
7070 /* Strip any conversions, additions, and subtractions, and see if
7071 we are returning the address of a local variable. Warn if so. */
7072 while (1)
7074 switch (TREE_CODE (inner))
7076 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
7077 case PLUS_EXPR:
7078 inner = TREE_OPERAND (inner, 0);
7079 continue;
7081 case MINUS_EXPR:
7082 /* If the second operand of the MINUS_EXPR has a pointer
7083 type (or is converted from it), this may be valid, so
7084 don't give a warning. */
7086 tree op1 = TREE_OPERAND (inner, 1);
7088 while (! POINTER_TYPE_P (TREE_TYPE (op1))
7089 && (TREE_CODE (op1) == NOP_EXPR
7090 || TREE_CODE (op1) == NON_LVALUE_EXPR
7091 || TREE_CODE (op1) == CONVERT_EXPR))
7092 op1 = TREE_OPERAND (op1, 0);
7094 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7095 break;
7097 inner = TREE_OPERAND (inner, 0);
7098 continue;
7101 case ADDR_EXPR:
7102 inner = TREE_OPERAND (inner, 0);
7104 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
7105 inner = TREE_OPERAND (inner, 0);
7107 if (TREE_CODE (inner) == VAR_DECL
7108 && ! DECL_EXTERNAL (inner)
7109 && ! TREE_STATIC (inner)
7110 && DECL_CONTEXT (inner) == current_function_decl)
7111 warning ("function returns address of local variable");
7112 break;
7114 default:
7115 break;
7118 break;
7121 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
7124 return add_stmt (build_return_stmt (retval));
7127 struct c_switch {
7128 /* The SWITCH_STMT being built. */
7129 tree switch_stmt;
7130 /* A splay-tree mapping the low element of a case range to the high
7131 element, or NULL_TREE if there is no high element. Used to
7132 determine whether or not a new case label duplicates an old case
7133 label. We need a tree, rather than simply a hash table, because
7134 of the GNU case range extension. */
7135 splay_tree cases;
7136 /* The next node on the stack. */
7137 struct c_switch *next;
7140 /* A stack of the currently active switch statements. The innermost
7141 switch statement is on the top of the stack. There is no need to
7142 mark the stack for garbage collection because it is only active
7143 during the processing of the body of a function, and we never
7144 collect at that point. */
7146 static struct c_switch *switch_stack;
7148 /* Start a C switch statement, testing expression EXP. Return the new
7149 SWITCH_STMT. */
7151 tree
7152 c_start_case (exp)
7153 tree exp;
7155 enum tree_code code;
7156 tree type, orig_type = error_mark_node;
7157 struct c_switch *cs;
7159 if (exp != error_mark_node)
7161 code = TREE_CODE (TREE_TYPE (exp));
7162 orig_type = TREE_TYPE (exp);
7164 if (! INTEGRAL_TYPE_P (orig_type)
7165 && code != ERROR_MARK)
7167 error ("switch quantity not an integer");
7168 exp = integer_zero_node;
7170 else
7172 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7174 if (warn_traditional && !in_system_header
7175 && (type == long_integer_type_node
7176 || type == long_unsigned_type_node))
7177 warning ("`long' switch expression not converted to `int' in ISO C");
7179 exp = default_conversion (exp);
7180 type = TREE_TYPE (exp);
7184 /* Add this new SWITCH_STMT to the stack. */
7185 cs = (struct c_switch *) xmalloc (sizeof (*cs));
7186 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
7187 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7188 cs->next = switch_stack;
7189 switch_stack = cs;
7191 return add_stmt (switch_stack->switch_stmt);
7194 /* Process a case label. */
7196 tree
7197 do_case (low_value, high_value)
7198 tree low_value;
7199 tree high_value;
7201 tree label = NULL_TREE;
7203 if (switch_stack)
7205 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
7207 label = c_add_case_label (switch_stack->cases,
7208 SWITCH_COND (switch_stack->switch_stmt),
7209 low_value, high_value);
7210 if (label == error_mark_node)
7211 label = NULL_TREE;
7212 else if (switch_was_empty_p)
7214 /* Attach the first case label to the SWITCH_BODY. */
7215 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
7216 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
7219 else if (low_value)
7220 error ("case label not within a switch statement");
7221 else
7222 error ("`default' label not within a switch statement");
7224 return label;
7227 /* Finish the switch statement. */
7229 void
7230 c_finish_case ()
7232 struct c_switch *cs = switch_stack;
7234 /* Rechain the next statements to the SWITCH_STMT. */
7235 last_tree = cs->switch_stmt;
7237 /* Pop the stack. */
7238 switch_stack = switch_stack->next;
7239 splay_tree_delete (cs->cases);
7240 free (cs);